Use ENC_NA as proto_tree_add_item() encoding arg for FT_ETHER hf[] field type.
[obnox/wireshark/wip.git] / plugins / docsis / packet-dccreq.c
1 /* packet-dccreq.c
2  * Routines for DCC Request Message  dissection
3  * Copyright 2004, Darryl Hymel <darryl.hymel[AT]arrisi.com>
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <epan/packet.h>
31
32 #define DCCREQ_UP_CHAN_ID 1
33 #define DCCREQ_DS_PARAMS 2
34 #define DCCREQ_INIT_TECH 3
35 #define DCCREQ_UCD_SUB 4
36 #define DCCREQ_SAID_SUB 6
37 #define DCCREQ_SF_SUB 7
38 #define DCCREQ_CMTS_MAC_ADDR 8
39 #define DCCREQ_KEY_SEQ_NUM 31
40 #define DCCREQ_HMAC_DIGEST 27
41
42 /* Define Downstrean Parameters subtypes
43  * These are subtype of DCCREQ_DS_PARAMS (2)
44  */
45
46 #define DCCREQ_DS_FREQ 1
47 #define DCCREQ_DS_MOD_TYPE 2
48 #define DCCREQ_DS_SYM_RATE 3
49 #define DCCREQ_DS_INTLV_DEPTH 4
50 #define DCCREQ_DS_CHAN_ID 5
51 #define DCCREQ_DS_SYNC_SUB 6
52
53 /* Define Service Flow Substitution subtypes
54  * These are subtypes of DCCREQ_SF_SUB (7)
55  */
56 #define DCCREQ_SF_SFID 1
57 #define DCCREQ_SF_SID 2
58 #define DCCREQ_SF_UNSOL_GRANT_TREF 5
59
60 /* Initialize the protocol and registered fields */
61 static int proto_docsis_dccreq = -1;
62
63 static int hf_docsis_dccreq_tran_id = -1;
64 static int hf_docsis_dccreq_up_chan_id = -1;
65 static int hf_docsis_dccreq_ds_freq = -1;
66 static int hf_docsis_dccreq_ds_mod_type = -1;
67 static int hf_docsis_dccreq_ds_sym_rate = -1;
68 static int hf_docsis_dccreq_ds_intlv_depth_i = -1;
69 static int hf_docsis_dccreq_ds_intlv_depth_j = -1;
70 static int hf_docsis_dccreq_ds_chan_id = -1;
71 static int hf_docsis_dccreq_ds_sync_sub = -1;
72 static int hf_docsis_dccreq_init_tech = -1;
73 static int hf_docsis_dccreq_ucd_sub = -1;
74 static int hf_docsis_dccreq_said_sub_cur = -1;
75 static int hf_docsis_dccreq_said_sub_new = -1;
76 static int hf_docsis_dccreq_sf_sfid_cur = -1;
77 static int hf_docsis_dccreq_sf_sfid_new = -1;
78 static int hf_docsis_dccreq_sf_sid_cur = -1;
79 static int hf_docsis_dccreq_sf_sid_new = -1;
80 static int hf_docsis_dccreq_sf_unsol_grant_tref = -1;
81 static int hf_docsis_dccreq_cmts_mac_addr = -1;
82 static int hf_docsis_dccreq_key_seq_num = -1;
83 static int hf_docsis_dccreq_hmac_digest = -1;
84
85 /* Initialize the subtree pointers */
86 static gint ett_docsis_dccreq = -1;
87 static gint ett_docsis_dccreq_ds_params = -1;
88 static gint ett_docsis_dccreq_sf_sub = -1;
89
90
91 value_string ds_mod_type_vals[] = {
92      {0 , "64 QAM"},
93      {1 , "256 QAM"},
94      {0, NULL}
95 };
96
97 value_string ds_sym_rate_vals[] = {
98      {0 , "5.056941 Msym/sec"},
99      {1 , "5.360537 Msym/sec"},
100      {2 , "6.952 Msym/sec"},
101      {0, NULL}
102 };
103 value_string init_tech_vals[] = {
104      {0 , "Reinitialize MAC"},
105      {1 , "Broadcast Init RNG on new chanbefore normal op"},
106      {2 , "Unicast RNG on new chan before normal op"},
107      {3 , "Either Unicast or broadcast RNG on new chan before normal op"},
108      {4 , "Use new chan directly without re-init or RNG"},
109      {0, NULL}
110 };
111 /* Code to actually dissect the packets */
112 static void
113 dissect_dccreq_ds_params (tvbuff_t * tvb, proto_tree * tree, int start, guint16 len)
114 {
115   guint8 type, length;
116   proto_item *dcc_item;
117   proto_tree *dcc_tree;
118   int pos;
119
120   pos = start;
121   dcc_item = proto_tree_add_text ( tree, tvb, start, len, "2 DCC-REQ Downstream Params Encodings (Length = %u)", len);
122   dcc_tree = proto_item_add_subtree ( dcc_item , ett_docsis_dccreq_ds_params);
123   
124   while ( pos < ( start + len) ) 
125     {
126         type = tvb_get_guint8 (tvb, pos++);
127         length = tvb_get_guint8 (tvb, pos++);
128         
129         switch (type)
130           {
131             case DCCREQ_DS_FREQ:
132               if (length == 4)
133                 {
134                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_freq, tvb,
135                                        pos, length, ENC_BIG_ENDIAN);
136                 }
137               else
138                 {
139                   THROW (ReportedBoundsError);
140                 }
141               break;
142             case DCCREQ_DS_MOD_TYPE:
143               if (length == 1)
144                 {
145                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_mod_type, tvb,
146                                    pos, length, ENC_BIG_ENDIAN);
147                 }
148               else 
149                 {
150                   THROW (ReportedBoundsError);
151                 }
152               break;
153             case DCCREQ_DS_SYM_RATE:
154               if (length == 1)
155                 {
156                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_sym_rate, tvb,
157                                    pos, length, ENC_BIG_ENDIAN);
158                 }
159               else 
160                 {
161                   THROW (ReportedBoundsError);
162                 }
163               break;
164             case DCCREQ_DS_INTLV_DEPTH:
165               if (length == 2)
166                 {
167                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_intlv_depth_i, tvb,
168                                    pos, 1, ENC_BIG_ENDIAN);
169                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_intlv_depth_j, tvb,
170                                    pos + 1, 1, ENC_BIG_ENDIAN);
171                 }
172               else 
173                 {
174                   THROW (ReportedBoundsError);
175                 }
176               break;
177             case DCCREQ_DS_CHAN_ID:
178               if (length == 1)
179                 {
180                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_chan_id, tvb,
181                                    pos, length, ENC_BIG_ENDIAN);
182                 }
183               else 
184                 {
185                   THROW (ReportedBoundsError);
186                 }
187               break;
188             case DCCREQ_DS_SYNC_SUB:
189                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ds_sync_sub, tvb,
190                                    pos, length, ENC_BIG_ENDIAN);
191               break;
192
193             }
194           pos = pos + length;
195       }
196 }
197 static void
198 dissect_dccreq_sf_sub (tvbuff_t * tvb, proto_tree * tree, int start, guint16 len)
199 {
200   guint8 type, length;
201   proto_item *dcc_item;
202   proto_tree *dcc_tree;
203   int pos;
204    
205   pos = start;
206   dcc_item = proto_tree_add_text ( tree, tvb, start, len, "7 DCC-REQ Service Flow Substitution Encodings (Length = %u)", len);
207   dcc_tree = proto_item_add_subtree ( dcc_item , ett_docsis_dccreq_sf_sub);
208   
209   while ( pos < ( start + len) ) 
210     {
211         type = tvb_get_guint8 (tvb, pos++);
212         length = tvb_get_guint8 (tvb, pos++);
213         
214         switch (type)
215           {
216             case DCCREQ_SF_SFID:
217               if (length == 8)
218                 {
219                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_sf_sfid_cur, tvb,
220                                        pos, 4, ENC_BIG_ENDIAN);
221                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_sf_sfid_new, tvb,
222                                        pos + 4, 4, ENC_BIG_ENDIAN);
223                 }
224               else
225                 {
226                   THROW (ReportedBoundsError);
227                 }
228               break;
229             case DCCREQ_SF_SID:
230               if (length == 4)
231                 {
232                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_sf_sid_cur, tvb,
233                                        pos, 2, ENC_BIG_ENDIAN);
234                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_sf_sid_new, tvb,
235                                        pos + 2, 2, ENC_BIG_ENDIAN);
236                 }
237               else
238                 {
239                   THROW (ReportedBoundsError);
240                 }
241               break;
242             case DCCREQ_SF_UNSOL_GRANT_TREF:
243               if (length == 4)
244                 {
245                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_sf_unsol_grant_tref, tvb,
246                                        pos, length, ENC_BIG_ENDIAN);
247                 }
248               else
249                 {
250                   THROW (ReportedBoundsError);
251                 }
252                 break;
253             }
254           pos = pos + length;
255       }
256 }
257 static void
258 dissect_dccreq (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
259 {
260   guint16 pos;
261   guint8 type, length;
262   proto_tree *dcc_tree;
263   proto_item *dcc_item;
264   guint16 len;
265
266   len = tvb_length_remaining (tvb, 0);
267
268   col_set_str(pinfo->cinfo, COL_INFO, "DCC-REQ Message: ");
269
270   if (tree)
271     {
272       dcc_item =
273         proto_tree_add_protocol_format (tree, proto_docsis_dccreq, tvb, 0,
274                                         tvb_length_remaining (tvb, 0),
275                                         "DCC-REQ Message");
276       dcc_tree = proto_item_add_subtree (dcc_item, ett_docsis_dccreq);
277       proto_tree_add_item (dcc_tree, hf_docsis_dccreq_tran_id, tvb, 0, 2, ENC_BIG_ENDIAN);
278
279       pos = 2;
280       while (pos < len)
281         {
282           type = tvb_get_guint8 (tvb, pos++);
283           length = tvb_get_guint8 (tvb, pos++);
284
285           switch (type)
286             {
287             case DCCREQ_UP_CHAN_ID:
288               if (length == 1)
289                 {
290                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_up_chan_id, tvb,
291                                        pos, length, ENC_BIG_ENDIAN);
292                 }
293               else
294                 {
295                   THROW (ReportedBoundsError);
296                 }
297               break;
298             case DCCREQ_DS_PARAMS:
299               dissect_dccreq_ds_params (tvb , dcc_tree , pos , length );
300               break;
301             case DCCREQ_INIT_TECH:
302               if (length == 1)
303                 {
304                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_init_tech, tvb,
305                                        pos, length, ENC_BIG_ENDIAN);
306                 }
307               else
308                 {
309                   THROW (ReportedBoundsError);
310                 }
311               break;
312             case DCCREQ_UCD_SUB:
313                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_ucd_sub, tvb,
314                                        pos, length, ENC_NA);
315               break;
316             case DCCREQ_SAID_SUB:
317               if (length == 4)
318                 {
319                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_said_sub_cur, tvb,
320                                        pos, 2, ENC_BIG_ENDIAN);
321                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_said_sub_new, tvb,
322                                        pos + 2, 2, ENC_BIG_ENDIAN);
323                 }
324               else
325                 {
326                   THROW (ReportedBoundsError);
327                 }
328               break;
329             case DCCREQ_SF_SUB:
330               dissect_dccreq_sf_sub (tvb , dcc_tree , pos , length );
331               break;
332             case DCCREQ_CMTS_MAC_ADDR:
333               if (length == 6)
334                 {
335                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_cmts_mac_addr, tvb,
336                                        pos, length, ENC_NA);
337                 }
338               else
339                 {
340                   THROW (ReportedBoundsError);
341                 }
342               break;
343             case DCCREQ_KEY_SEQ_NUM:
344               if (length == 1)
345                 {
346                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_key_seq_num, tvb,
347                                        pos, length, ENC_BIG_ENDIAN);
348                 }
349               else
350                 {
351                   THROW (ReportedBoundsError);
352                 }
353               break;
354             case DCCREQ_HMAC_DIGEST:
355               if (length == 20)
356                 {
357                   proto_tree_add_item (dcc_tree, hf_docsis_dccreq_hmac_digest, tvb,
358                                        pos, length, ENC_NA);
359                 }
360               else
361                 {
362                   THROW (ReportedBoundsError);
363                 }
364               break;
365             }                   /* switch(type) */
366           pos = pos + length;
367         }                       /* while (pos < len) */
368     }                           /* if (tree) */
369
370 }
371 /* Register the protocol with Wireshark */
372
373 /* this format is require because a script is used to build the C function
374    that calls all the protocol registration.
375 */
376
377
378 void
379 proto_register_docsis_dccreq (void)
380 {
381 /* Setup list of header fields  See Section 1.6.1 for details*/
382   static hf_register_info hf[] = {
383     {&hf_docsis_dccreq_tran_id ,
384       {
385       "Transaction ID", 
386       "docsis_dccreq.tran_id",
387       FT_UINT16, BASE_DEC, NULL, 0x0,
388       NULL, 
389       HFILL
390       }
391     },
392     {&hf_docsis_dccreq_up_chan_id ,
393       {
394       "Up Channel ID", 
395       "docsis_dccreq.up_chan_id",
396       FT_UINT8, BASE_DEC, NULL, 0x0,
397       NULL, 
398       HFILL
399       }
400     },
401     {&hf_docsis_dccreq_ds_freq ,
402       {
403       "Frequency", 
404       "docsis_dccreq.ds_freq",
405       FT_UINT32, BASE_DEC, NULL, 0x0,
406       NULL, 
407       HFILL
408       }
409     },
410     {&hf_docsis_dccreq_ds_mod_type ,
411       {
412       "Modulation Type", 
413       "docsis_dccreq.ds_mod_type",
414       FT_UINT8, BASE_DEC, VALS (ds_mod_type_vals), 0x0,
415       NULL, 
416       HFILL
417       }
418     },
419     {&hf_docsis_dccreq_ds_sym_rate ,
420       {
421       "Symbol Rate", 
422       "docsis_dccreq.ds_sym_rate",
423       FT_UINT8, BASE_DEC, VALS (ds_sym_rate_vals), 0x0,
424       NULL, 
425       HFILL
426       }
427     },
428     {&hf_docsis_dccreq_ds_intlv_depth_i ,
429       {
430       "Interleaver Depth I Value", 
431       "docsis_dccreq.ds_intlv_depth_i",
432       FT_UINT8, BASE_DEC, NULL, 0x0,
433       NULL, 
434       HFILL
435       }
436     },
437     {&hf_docsis_dccreq_ds_intlv_depth_j ,
438       {
439       "Interleaver Depth J Value", 
440       "docsis_dccreq.ds_intlv_depth_j",
441       FT_UINT8, BASE_DEC, NULL, 0x0,
442       NULL, 
443       HFILL
444       }
445     },
446     {&hf_docsis_dccreq_ds_chan_id ,
447       {
448       "Downstream Channel ID",
449       "docsis_dccreq.ds_chan_id",
450       FT_UINT8, BASE_DEC, NULL, 0x0,
451       NULL,
452       HFILL
453       }
454     },
455     {&hf_docsis_dccreq_ds_sync_sub ,
456       {
457       "SYNC Substitution",
458       "docsis_dccreq.ds_sync_sub",
459       FT_UINT8, BASE_DEC, NULL, 0x0,
460       NULL,
461       HFILL
462       }
463     },
464     {&hf_docsis_dccreq_init_tech ,
465       {
466       "Initialization Technique", 
467       "docsis_dccreq.init_tech",
468       FT_UINT8, BASE_DEC, VALS (init_tech_vals), 0x0,
469       NULL, 
470       HFILL
471       }
472     },
473     {&hf_docsis_dccreq_ucd_sub ,
474       {
475       "UCD Substitution", 
476       "docsis_dccreq.ucd_sub",
477       FT_BYTES, BASE_NONE, NULL, 0x0,
478       NULL, 
479       HFILL
480       }
481     },
482     {&hf_docsis_dccreq_said_sub_cur ,
483       {
484       "SAID Sub - Current Value",
485       "docsis_dccreq.said_sub_cur",
486       FT_UINT16, BASE_DEC, NULL, 0x0,
487       NULL,
488       HFILL
489       }
490     },
491     {&hf_docsis_dccreq_said_sub_new ,
492       {
493       "SAID Sub - New Value",
494       "docsis_dccreq.said_sub_new",
495       FT_UINT16, BASE_DEC, NULL, 0x0,
496       NULL,
497       HFILL
498       }
499     },
500     {&hf_docsis_dccreq_sf_sfid_cur ,
501       {
502       "SF Sub - SFID Current Value",
503       "docsis_dccreq.sf_sfid_cur",
504       FT_UINT32, BASE_DEC, NULL, 0x0,
505       NULL,
506       HFILL
507       }
508     },
509     {&hf_docsis_dccreq_sf_sfid_new ,
510       {
511       "SF Sub - SFID New Value",
512       "docsis_dccreq.sf_sfid_new",
513       FT_UINT32, BASE_DEC, NULL, 0x0,
514       NULL,
515       HFILL
516       }
517     },
518     {&hf_docsis_dccreq_sf_sid_cur ,
519       {
520       "SF Sub - SID Current Value",
521       "docsis_dccreq.sf_sid_cur",
522       FT_UINT16, BASE_DEC, NULL, 0x0,
523       NULL,
524       HFILL
525       }
526     },
527     {&hf_docsis_dccreq_sf_sid_new ,
528       {
529       "SF Sub - SID New Value",
530       "docsis_dccreq.sf_sid_new",
531       FT_UINT16, BASE_DEC, NULL, 0x0,
532       NULL,
533       HFILL
534       }
535     },
536     {&hf_docsis_dccreq_sf_unsol_grant_tref ,
537       {
538       "SF Sub - Unsolicited Grant Time Reference",
539       "docsis_dccreq.sf_unsol_grant_tref",
540       FT_UINT32, BASE_DEC, NULL, 0x0,
541       NULL,
542       HFILL
543       }
544     },
545     {&hf_docsis_dccreq_cmts_mac_addr ,
546       {
547       "CMTS Mac Address",
548       "docsis_dccreq.cmts_mac_addr",
549       FT_ETHER, BASE_NONE, NULL, 0x0,
550       NULL,
551       HFILL
552       }
553     },
554     {&hf_docsis_dccreq_key_seq_num ,
555       {
556       "Auth Key Sequence Number",
557       "docsis_dccreq.key_seq_num",
558       FT_UINT8, BASE_DEC, NULL, 0x0,
559       NULL,
560       HFILL
561       }
562     },
563     {&hf_docsis_dccreq_hmac_digest ,
564       {
565       "HMAC-DigestNumber",
566       "docsis_dccreq.hmac_digest",
567       FT_BYTES, BASE_NONE, NULL, 0x0,
568       NULL,
569       HFILL
570       }
571     },
572
573   };
574
575 /* Setup protocol subtree array */
576   static gint *ett[] = {
577     &ett_docsis_dccreq,
578     &ett_docsis_dccreq_sf_sub,
579     &ett_docsis_dccreq_ds_params,
580   };
581
582 /* Register the protocol name and description */
583   proto_docsis_dccreq =
584     proto_register_protocol ("DOCSIS Downstream Channel Change Request",
585                              "DOCSIS DCC-REQ", "docsis_dccreq");
586
587 /* Required function calls to register the header fields and subtrees used */
588   proto_register_field_array (proto_docsis_dccreq, hf, array_length (hf));
589   proto_register_subtree_array (ett, array_length (ett));
590
591   register_dissector ("docsis_dccreq", dissect_dccreq, proto_docsis_dccreq);
592 }
593
594
595 /* If this dissector uses sub-dissector registration add a registration routine.
596    This format is required because a script is used to find these routines and
597    create the code that calls these routines.
598 */
599 void
600 proto_reg_handoff_docsis_dccreq (void)
601 {
602   dissector_handle_t docsis_dccreq_handle;
603
604   docsis_dccreq_handle = find_dissector ("docsis_dccreq");
605   dissector_add_uint ("docsis_mgmt", 0x17, docsis_dccreq_handle);
606
607 }