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