941de28924a7007d8536f91dc1a4e74ffa69c756
[obnox/wireshark/wip.git] / plugins / docsis / packet-ucd.c
1 /* packet-ucd.c
2  * Routines for UCD Message dissection
3  * Copyright 2002, Anand V. Narwani <anarwani@cisco.com>
4  *
5  * $Id: packet-ucd.c,v 1.4 2002/09/10 19:07:40 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 "plugins/plugin_api.h"
32 #include "plugins/plugin_api_defs.h"
33 #include "moduleinfo.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <gmodule.h>
40
41 #include <epan/packet.h>
42
43 #define UCD_SYMBOL_RATE 1
44 #define UCD_FREQUENCY 2
45 #define UCD_PREAMBLE 3
46 #define UCD_BURST_DESCR 4
47
48 #define UCD_MODULATION 1
49 #define UCD_DIFF_ENCODING 2
50 #define UCD_PREAMBLE_LEN 3
51 #define UCD_PREAMBLE_VAL_OFF 4
52 #define UCD_FEC 5
53 #define UCD_FEC_CODEWORD 6
54 #define UCD_SCRAMBLER_SEED 7
55 #define UCD_MAX_BURST 8
56 #define UCD_GUARD_TIME 9
57 #define UCD_LAST_CW_LEN 10
58 #define UCD_SCRAMBLER_ONOFF 11
59
60 #define IUC_REQUEST 1
61 #define IUC_REQ_DATA 2
62 #define IUC_INIT_MAINT 3
63 #define IUC_STATION_MAINT 4
64 #define IUC_SHORT_DATA_GRANT 5
65 #define IUC_LONG_DATA_GRANT 6
66 #define IUC_NULL_IE 7
67 #define IUC_DATA_ACK 8
68 #define IUC_RESERVED9 9
69 #define IUC_RESERVED10 10
70 #define IUC_RESERVED11 11
71 #define IUC_RESERVED12 12
72 #define IUC_RESERVED13 13
73 #define IUC_RESERVED14 14
74 #define IUC_EXPANSION 15
75
76 /* Initialize the protocol and registered fields */
77 static int proto_docsis_ucd = -1;
78
79 static int hf_docsis_ucd_upstream_chid = -1;
80 static int hf_docsis_ucd_config_ch_cnt = -1;
81 static int hf_docsis_ucd_mini_slot_size = -1;
82 static int hf_docsis_ucd_down_chid = -1;
83 static int hf_docsis_ucd_symbol_rate = -1;
84 static int hf_docsis_ucd_frequency = -1;
85 static int hf_docsis_ucd_preamble_pat = -1;
86 static int hf_docsis_ucd_iuc = -1;
87
88 static int hf_docsis_burst_mod_type = -1;
89 static int hf_docsis_burst_diff_encoding = -1;
90 static int hf_docsis_burst_preamble_len = -1;
91 static int hf_docsis_burst_preamble_val_off = -1;
92 static int hf_docsis_burst_fec = -1;
93 static int hf_docsis_burst_fec_codeword = -1;
94 static int hf_docsis_burst_scrambler_seed = -1;
95 static int hf_docsis_burst_max_burst = -1;
96 static int hf_docsis_burst_guard_time = -1;
97 static int hf_docsis_burst_last_cw_len = -1;
98 static int hf_docsis_burst_scrambler_onoff = -1;
99
100 /* Initialize the subtree pointers */
101 static gint ett_docsis_ucd = -1;
102 static gint ett_burst_descr = -1;
103
104 static const value_string channel_tlv_vals[] = {
105   {UCD_SYMBOL_RATE, "Symbol Rate"},
106   {UCD_FREQUENCY, "Frequency"},
107   {UCD_PREAMBLE, "Preamble Pattern"},
108   {UCD_BURST_DESCR, "Burst Descriptor"},
109   {0, NULL}
110 };
111
112 static const value_string on_off_vals[] = {
113   {1, "On"},
114   {2, "Off"},
115   {0, NULL}
116 };
117
118 static const value_string mod_vals[] = {
119   {1, "QPSK"},
120   {2, "QAM16"},
121   {0, NULL}
122 };
123
124 value_string iuc_vals[] = {
125   {IUC_REQUEST, "Request"},
126   {IUC_REQ_DATA, "REQ/Data"},
127   {IUC_INIT_MAINT, "Initial Maintenance"},
128   {IUC_STATION_MAINT, "Station Maintenance"},
129   {IUC_SHORT_DATA_GRANT, "Short Data Grant"},
130   {IUC_LONG_DATA_GRANT, "Long Data Grant"},
131   {IUC_NULL_IE, "NULL IE"},
132   {IUC_DATA_ACK, "Data Ack"},
133   {IUC_RESERVED9, "Reserved"},
134   {IUC_RESERVED10, "Reserved"},
135   {IUC_RESERVED11, "Reserved"},
136   {IUC_RESERVED12, "Reserved"},
137   {IUC_RESERVED13, "Reserved"},
138   {IUC_RESERVED14, "Reserved"},
139   {IUC_EXPANSION, "Expanded IUC"},
140   {0, NULL}
141 };
142
143 static const value_string last_cw_len_vals[] = {
144   {1, "Fixed"},
145   {2, "Shortened"},
146   {0, NULL}
147 };
148 /* Code to actually dissect the packets */
149 static void
150 dissect_ucd (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
151 {
152   guint16 pos, endtlvpos;
153   guint8 type, length;
154   guint8 tlvlen, tlvtype;
155   proto_tree *burst_descr_tree;
156   proto_item *it;
157   proto_tree *ucd_tree;
158   proto_item *ucd_item;
159   guint16 len;
160   guint8 upchid, symrate;
161
162   len = tvb_length_remaining (tvb, 0);
163   upchid = tvb_get_guint8 (tvb, 0);
164
165   /* if the upstream Channel ID is 0 then this is for Telephony Return) */
166   if (check_col (pinfo->cinfo, COL_INFO))
167     {
168       col_clear (pinfo->cinfo, COL_INFO);
169       if (upchid > 0)
170         col_add_fstr (pinfo->cinfo, COL_INFO,
171                       "UCD Message:  Channel ID = %u (U%u)", upchid,
172                       upchid - 1);
173       else
174         col_add_fstr (pinfo->cinfo, COL_INFO,
175                       "UCD Message:  Channel ID = %u (Telephony Return)",
176                       upchid);
177     }
178
179   if (tree)
180     {
181       ucd_item =
182         proto_tree_add_protocol_format (tree, proto_docsis_ucd, tvb, 0,
183                                         tvb_length_remaining (tvb, 0),
184                                         "UCD Message");
185       ucd_tree = proto_item_add_subtree (ucd_item, ett_docsis_ucd);
186       proto_tree_add_item (ucd_tree, hf_docsis_ucd_upstream_chid, tvb, 0, 1,
187                            FALSE);
188       proto_tree_add_item (ucd_tree, hf_docsis_ucd_config_ch_cnt, tvb, 1, 1,
189                            FALSE);
190       proto_tree_add_item (ucd_tree, hf_docsis_ucd_mini_slot_size, tvb, 2, 1,
191                            FALSE);
192       proto_tree_add_item (ucd_tree, hf_docsis_ucd_down_chid, tvb, 3, 1,
193                            FALSE);
194
195       pos = 4;
196       while (pos < len)
197         {
198           type = tvb_get_guint8 (tvb, pos++);
199           length = tvb_get_guint8 (tvb, pos++);
200           switch (type)
201             {
202             case UCD_SYMBOL_RATE:
203               if (length == 1)
204                 {
205                   symrate = tvb_get_guint8 (tvb, pos);
206                   proto_tree_add_uint (ucd_tree, hf_docsis_ucd_symbol_rate,
207                                        tvb, pos, length, symrate * 160);
208                 }
209               else
210                 {
211                   THROW (ReportedBoundsError);
212                 }
213               pos = pos + length;
214               break;
215             case UCD_FREQUENCY:
216               if (length == 4)
217                 {
218                   proto_tree_add_item (ucd_tree, hf_docsis_ucd_frequency, tvb,
219                                        pos, length, FALSE);
220                   pos = pos + length;
221                 }
222               else
223                 {
224                   THROW (ReportedBoundsError);
225                 }
226               break;
227             case UCD_PREAMBLE:
228               proto_tree_add_item (ucd_tree, hf_docsis_ucd_preamble_pat, tvb,
229                                    pos, length, FALSE);
230               pos = pos + length;
231               break;
232             case UCD_BURST_DESCR:
233               it =
234                 proto_tree_add_text (ucd_tree, tvb, pos, length,
235                                      "4 Burst Descriptor (Length = %u)",
236                                      length);
237               burst_descr_tree = proto_item_add_subtree (it, ett_burst_descr);
238               proto_tree_add_item (burst_descr_tree, hf_docsis_ucd_iuc, tvb,
239                                    pos++, 1, FALSE);
240               endtlvpos = pos + length - 1;
241               while (pos < endtlvpos)
242                 {
243                   tlvtype = tvb_get_guint8 (tvb, pos++);
244                   tlvlen = tvb_get_guint8 (tvb, pos++);
245                   switch (tlvtype)
246                     {
247                     case UCD_MODULATION:
248                       if (tlvlen == 1)
249                         {
250                           proto_tree_add_item (burst_descr_tree,
251                                                hf_docsis_burst_mod_type, tvb,
252                                                pos, tlvlen, FALSE);
253                         }
254                       else
255                         {
256                           THROW (ReportedBoundsError);
257                         }
258                       break;
259                     case UCD_DIFF_ENCODING:
260                       if (tlvlen == 1)
261                         {
262                           proto_tree_add_item (burst_descr_tree,
263                                                hf_docsis_burst_diff_encoding,
264                                                tvb, pos, tlvlen, FALSE);
265                         }
266                       else
267                         {
268                           THROW (ReportedBoundsError);
269                         }
270                       break;
271                     case UCD_PREAMBLE_LEN:
272                       if (tlvlen == 2)
273                         {
274                           proto_tree_add_item (burst_descr_tree,
275                                                hf_docsis_burst_preamble_len,
276                                                tvb, pos, tlvlen, FALSE);
277                         }
278                       else
279                         {
280                           THROW (ReportedBoundsError);
281                         }
282                       break;
283                     case UCD_PREAMBLE_VAL_OFF:
284                       if (tlvlen == 2)
285                         {
286                           proto_tree_add_item (burst_descr_tree,
287                                                hf_docsis_burst_preamble_val_off,
288                                                tvb, pos, tlvlen, FALSE);
289                         }
290                       else
291                         {
292                           THROW (ReportedBoundsError);
293                         }
294                       break;
295                     case UCD_FEC:
296                       if (tlvlen == 1)
297                         {
298                           proto_tree_add_item (burst_descr_tree,
299                                                hf_docsis_burst_fec, tvb, pos,
300                                                tlvlen, FALSE);
301                         }
302                       else
303                         {
304                           THROW (ReportedBoundsError);
305                         }
306                       break;
307                     case UCD_FEC_CODEWORD:
308                       if (tlvlen == 1)
309                         {
310                           proto_tree_add_item (burst_descr_tree,
311                                                hf_docsis_burst_fec_codeword,
312                                                tvb, pos, tlvlen, FALSE);
313                         }
314                       else
315                         {
316                           THROW (ReportedBoundsError);
317                         }
318                       break;
319                     case UCD_SCRAMBLER_SEED:
320                       if (tlvlen == 2)
321                         {
322                           proto_tree_add_item (burst_descr_tree,
323                                                hf_docsis_burst_scrambler_seed,
324                                                tvb, pos, tlvlen, FALSE);
325                         }
326                       else
327                         {
328                           THROW (ReportedBoundsError);
329                         }
330                       break;
331                     case UCD_MAX_BURST:
332                       if (tlvlen == 1)
333                         {
334                           proto_tree_add_item (burst_descr_tree,
335                                                hf_docsis_burst_max_burst, tvb,
336                                                pos, tlvlen, FALSE);
337                         }
338                       else
339                         {
340                           THROW (ReportedBoundsError);
341                         }
342                       break;
343                     case UCD_GUARD_TIME:
344                       if (tlvlen == 1)
345                         {
346                           proto_tree_add_item (burst_descr_tree,
347                                                hf_docsis_burst_guard_time,
348                                                tvb, pos, tlvlen, FALSE);
349                         }
350                       else
351                         {
352                           THROW (ReportedBoundsError);
353                         }
354                       break;
355                     case UCD_LAST_CW_LEN:
356                       if (tlvlen == 1)
357                         {
358                           proto_tree_add_item (burst_descr_tree,
359                                                hf_docsis_burst_last_cw_len,
360                                                tvb, pos, tlvlen, FALSE);
361                         }
362                       else
363                         {
364                           THROW (ReportedBoundsError);
365                         }
366                       break;
367                     case UCD_SCRAMBLER_ONOFF:
368                       if (tlvlen == 1)
369                         {
370                           proto_tree_add_item (burst_descr_tree,
371                                                hf_docsis_burst_scrambler_onoff,
372                                                tvb, pos, tlvlen, FALSE);
373                         }
374                       else
375                         {
376                           THROW (ReportedBoundsError);
377                         }
378                       break;
379                     }           /* switch(tlvtype) */
380                   pos = pos + tlvlen;
381                 }               /* while (pos < endtlvpos) */
382               break;
383             }                   /* switch(type) */
384         }                       /* while (pos < len) */
385     }                           /* if (tree) */
386
387 }
388
389 /* Register the protocol with Ethereal */
390
391 /* this format is require because a script is used to build the C function
392    that calls all the protocol registration.
393 */
394
395
396 void
397 proto_register_docsis_ucd (void)
398 {
399
400 /* Setup list of header fields  See Section 1.6.1 for details*/
401   static hf_register_info hf[] = {
402     {&hf_docsis_ucd_upstream_chid,
403      {"Upstream Channel ID", "docsis.ucd.upchid",
404       FT_UINT8, BASE_DEC, NULL, 0x0,
405       "Upstream Channel ID", HFILL}
406      },
407     {&hf_docsis_ucd_config_ch_cnt,
408      {"Config Change Count", "docsis.ucd.confcngcnt",
409       FT_UINT8, BASE_DEC, NULL, 0x0,
410       "Configuration Change Count", HFILL}
411      },
412     {&hf_docsis_ucd_mini_slot_size,
413      {"Mini Slot Size (6.25us TimeTicks)", "docsis.ucd.mslotsize",
414       FT_UINT8, BASE_DEC, NULL, 0x0,
415       "Mini Slot Size (6.25us TimeTicks)", HFILL}
416      },
417     {&hf_docsis_ucd_down_chid,
418      {"Downstream Channel ID", "docsis.ucd.downchid",
419       FT_UINT8, BASE_DEC, NULL, 0x0,
420       "Management Message", HFILL}
421      },
422     {&hf_docsis_ucd_symbol_rate,
423      {"1 Symbol Rate (ksym/sec)", "docsis.ucd.symrate",
424       FT_UINT8, BASE_DEC, NULL, 0x0,
425       "Symbol Rate", HFILL}
426      },
427     {&hf_docsis_ucd_frequency,
428      {"2 Frequency (Hz)", "docsis.ucd.freq",
429       FT_UINT32, BASE_DEC, NULL, 0x0,
430       "Upstream Center Frequency", HFILL}
431      },
432     {&hf_docsis_ucd_preamble_pat,
433      {"3 Preamble Pattern", "docsis.ucd.preamble",
434       FT_BYTES, BASE_HEX, NULL, 0x0,
435       "Preamble Superstring", HFILL}
436      },
437     {&hf_docsis_ucd_iuc,
438      {"Interval Usage Code", "docsis.ucd.iuc",
439       FT_UINT8, BASE_DEC, VALS (iuc_vals), 0x0,
440       "Interval Usage Code", HFILL}
441      },
442     {&hf_docsis_burst_mod_type,
443      {"1 Modulation Type", "docsis.ucd.burst.modtype",
444       FT_UINT8, BASE_DEC, VALS (mod_vals), 0x0,
445       "Modulation Type", HFILL}
446      },
447     {&hf_docsis_burst_diff_encoding,
448      {"2 Differential Encoding", "docsis.ucd.burst.diffenc",
449       FT_UINT8, BASE_DEC, VALS (on_off_vals), 0x0,
450       "Differential Encoding", HFILL}
451      },
452     {&hf_docsis_burst_preamble_len,
453      {"3 Preamble Length (Bits)", "docsis.ucd.burst.preamble_len",
454       FT_UINT16, BASE_DEC, NULL, 0x0,
455       "Preamble Length (Bits)", HFILL}
456      },
457     {&hf_docsis_burst_preamble_val_off,
458      {"4 Preamble Offset (Bits)", "docsis.ucd.burst.preamble_off",
459       FT_UINT16, BASE_DEC, NULL, 0x0,
460       "Preamble Offset (Bits)", HFILL}
461      },
462     {&hf_docsis_burst_fec,
463      {"5 FEC (T)", "docsis.ucd.burst.fec",
464       FT_UINT8, BASE_DEC, NULL, 0x0,
465       "FEC (T) Codeword Parity Bits = 2^T", HFILL}
466      },
467     {&hf_docsis_burst_fec_codeword,
468      {"6 FEC Codeword Info bytes (k)", "docsis.ucd.burst.fec_codeword",
469       FT_UINT8, BASE_DEC, NULL, 0x0,
470       "FEC Codeword Info Bytes (k)", HFILL}
471      },
472     {&hf_docsis_burst_scrambler_seed,
473      {"7 Scrambler Seed", "docsis.ucd.burst.scrambler_seed",
474       FT_UINT16, BASE_HEX, NULL, 0x0,
475       "Burst Descriptor", HFILL}
476      },
477     {&hf_docsis_burst_max_burst,
478      {"8 Max Burst Size (Minislots)", "docsis.ucd.burst.maxburst",
479       FT_UINT8, BASE_DEC, NULL, 0x0,
480       "Max Burst Size (Minislots)", HFILL}
481      },
482     {&hf_docsis_burst_guard_time,
483      {"9 Guard Time Size (Symbol Times)", "docsis.ucd.burst.guardtime",
484       FT_UINT8, BASE_DEC, NULL, 0x0,
485       "Guard Time Size", HFILL}
486      },
487     {&hf_docsis_burst_last_cw_len,
488      {"10 Last Codeword Length", "docsis.ucd.burst.last_cw_len",
489       FT_UINT8, BASE_DEC, VALS (last_cw_len_vals), 0x0,
490       "Last Codeword Length", HFILL}
491      },
492     {&hf_docsis_burst_scrambler_onoff,
493      {"11 Scrambler On/Off", "docsis.ucd.burst.scrambleronoff",
494       FT_UINT8, BASE_DEC, VALS (on_off_vals), 0x0,
495       "Scrambler On/Off", HFILL}
496      },
497   };
498
499 /* Setup protocol subtree array */
500   static gint *ett[] = {
501     &ett_docsis_ucd,
502     &ett_burst_descr,
503   };
504
505 /* Register the protocol name and description */
506   proto_docsis_ucd =
507     proto_register_protocol ("DOCSIS Upstream Channel Descriptor",
508                              "DOCSIS UCD", "docsis_ucd");
509
510 /* Required function calls to register the header fields and subtrees used */
511   proto_register_field_array (proto_docsis_ucd, hf, array_length (hf));
512   proto_register_subtree_array (ett, array_length (ett));
513
514   register_dissector ("docsis_ucd", dissect_ucd, proto_docsis_ucd);
515 }
516
517
518 /* If this dissector uses sub-dissector registration add a registration routine.
519    This format is required because a script is used to find these routines and
520    create the code that calls these routines.
521 */
522 void
523 proto_reg_handoff_docsis_ucd (void)
524 {
525   dissector_handle_t docsis_ucd_handle;
526
527   docsis_ucd_handle = find_dissector ("docsis_ucd");
528   dissector_add ("docsis_mgmt", 0x02, docsis_ucd_handle);
529
530 }