Convert 'encoding' parameter of certain proto_tree_add_item() calls in plugin dissectors:
[obnox/wireshark/wip.git] / plugins / docsis / packet-macmgmt.c
1 /* packet-macmgmt.c
2  * Routines for docsis Mac Management Header dissection
3  * Copyright 2002, Anand V. Narwani <anand[AT]narwani.org>
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 MGT_SYNC 1
33 #define MGT_UCD 2
34 #define MGT_MAP 3
35 #define MGT_RNG_REQ 4
36 #define MGT_RNG_RSP 5
37 #define MGT_REG_REQ 6
38 #define MGT_REG_RSP 7
39 #define MGT_UCC_REQ 8
40 #define MGT_UCC_RSP 9
41 #define MGT_TRI_TCD 10
42 #define MGT_TRI_TSI 11
43 #define MGT_BPKM_REQ 12
44 #define MGT_BPKM_RSP 13
45 #define MGT_REG_ACK 14
46 #define MGT_DSA_REQ 15
47 #define MGT_DSA_RSP 16
48 #define MGT_DSA_ACK 17
49 #define MGT_DSC_REQ 18
50 #define MGT_DSC_RSP 19
51 #define MGT_DSC_ACK 20
52 #define MGT_DSD_REQ 21
53 #define MGT_DSD_RSP 22
54 #define MGT_DCC_REQ 23
55 #define MGT_DCC_RSP 24
56 #define MGT_DCC_ACK 25
57 #define MGT_DCI_REQ 26
58 #define MGT_DCI_RSP 27
59 #define MGT_UP_DIS 28
60 #define MGT_TYPE29UCD 29
61 #define MGT_INIT_RNG_REQ 30
62 #define MGT_TEST_REQ 31
63 #define MGT_DS_CH_DESC 32
64 #define MGT_MDD 33
65 #define MGT_B_INIT_RNG_REQ 34
66 #define MGT_TYPE35UCD 35
67 #define MGT_DBC_REQ 36
68 #define MGT_DBC_RSP 37
69 #define MGT_DBC_ACK 38
70 #define MGT_DPV_REQ 39
71 #define MGT_DPV_RSP 40
72 #define MGT_CM_STATUS 41
73 #define MGT_CM_CTRL_REQ 42
74 #define MGT_CM_CTRL_RSP 43
75 #define MGT_REG_REQ_MP 44
76 #define MGT_REG_RSP_MP 45
77
78
79 /* Initialize the protocol and registered fields */
80 static int proto_docsis_mgmt = -1;
81 static int hf_docsis_mgt_dst_addr = -1;
82 static int hf_docsis_mgt_src_addr = -1;
83 static int hf_docsis_mgt_msg_len = -1;
84 static int hf_docsis_mgt_dsap = -1;
85 static int hf_docsis_mgt_ssap = -1;
86 static int hf_docsis_mgt_control = -1;
87 static int hf_docsis_mgt_version = -1;
88 static int hf_docsis_mgt_type = -1;
89 static int hf_docsis_mgt_rsvd = -1;
90
91 static dissector_table_t docsis_mgmt_dissector_table;
92 static dissector_handle_t data_handle;
93
94 /* Initialize the subtree pointers */
95 static gint ett_docsis_mgmt = -1;
96 static gint ett_mgmt_pay = -1;
97
98
99 static const value_string mgmt_type_vals[] = {
100   {MGT_SYNC, "Timing Synchronisation"},
101   {MGT_UCD, "Upstream Channel Descriptor"},
102   {MGT_TYPE29UCD, "Upstream Channel Descriptor Type 29"},
103   {MGT_TYPE35UCD, "Upstream Channel Descriptor Type 35"},
104   {MGT_MAP, "Upstream Bandwidth Allocation"},
105   {MGT_RNG_REQ, "Ranging Request"},
106   {MGT_RNG_RSP, "Ranging Response"},
107   {MGT_REG_REQ, "Registration Request"},
108   {MGT_REG_RSP, "Registration Response"},
109   {MGT_UCC_REQ, "Upstream Channel Change Request"},
110   {MGT_UCC_RSP, "Upstream Channel Change Response"},
111   {MGT_TRI_TCD, "Telephony Channel Descriptor"},
112   {MGT_TRI_TSI, "Termination System Information"},
113   {MGT_BPKM_REQ, "Privacy Key Management Request"},
114   {MGT_BPKM_RSP, "Privacy Key Management Response"},
115   {MGT_REG_ACK, "Registration Acknowledge"},
116   {MGT_DSA_REQ, "Dynamic Service Addition Request"},
117   {MGT_DSA_RSP, "Dynamic Service Addition Response"},
118   {MGT_DSA_ACK, "Dynamic Service Addition  Acknowledge"},
119   {MGT_DSC_REQ, "Dynamic Service Change Request"},
120   {MGT_DSC_RSP, "Dynamic Service Change Response"},
121   {MGT_DSC_ACK, "Dynamic Service Change Acknowledge"},
122   {MGT_DSD_REQ, "Dynamic Service Delete Request"},
123   {MGT_DSD_RSP, "Dynamic Service Delete Response"},
124   {MGT_DCC_REQ, "Dynamic Channel Change Request"},
125   {MGT_DCC_RSP, "Dynamic Channel Change Response"},
126   {MGT_DCC_ACK, "Dynamic Channel Change Acknowledge"},
127   {MGT_DCI_REQ, "Device Class Identification Request"},
128   {MGT_DCI_RSP, "Device Class Identification Response"},
129   {MGT_UP_DIS, "Upstream Channel Disable"},
130   {MGT_INIT_RNG_REQ, "Initial Ranging Request"},
131   {MGT_TEST_REQ, "Test Request Message"},
132   {MGT_DS_CH_DESC, "Downstream Channel Descriptor"},
133   {MGT_MDD, "MAC Domain Descriptor"},
134   {MGT_B_INIT_RNG_REQ, "Bonded Initial Ranging Request"},
135   {MGT_DBC_REQ, "Dynamic Bonding Change Request"},
136   {MGT_DBC_RSP, "Dynamic Bonding Change Response"},
137   {MGT_DBC_ACK, "Dynamic Bonding Change Acknowledge"},
138   {MGT_DPV_REQ, "DOCSIS Path Verify Request"},
139   {MGT_DPV_RSP, "DOCSIS Path Verify Response"},
140   {MGT_CM_STATUS, "CM Status Report"},
141   {MGT_CM_CTRL_REQ, "CM Control Request"},
142   {MGT_CM_CTRL_RSP, "CM Control Response"},
143   {MGT_REG_REQ_MP, "Multipart Registration Request"},
144   {MGT_REG_RSP_MP, "Multipart Registration Response"},
145   {0, NULL}
146 };
147
148 /* Code to actually dissect the packets */
149 static void
150 dissect_macmgmt (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
151 {
152
153   const guint8 *src, *dst;
154   guint16 msg_len;
155   proto_item *mgt_hdr_it;
156   proto_tree *mgt_hdr_tree;
157   tvbuff_t *payload_tvb;
158   guint8 type;
159   col_set_str (pinfo->cinfo, COL_PROTOCOL, "DOCSIS MGMT");
160
161   col_clear(pinfo->cinfo, COL_INFO);
162
163
164   src = tvb_get_ptr (tvb, 6, 6);
165   dst = tvb_get_ptr (tvb, 0, 6);
166   SET_ADDRESS (&pinfo->dl_src, AT_ETHER, 6, src);
167   SET_ADDRESS (&pinfo->src, AT_ETHER, 6, src);
168   SET_ADDRESS (&pinfo->dl_dst, AT_ETHER, 6, dst);
169   SET_ADDRESS (&pinfo->dst, AT_ETHER, 6, dst);
170
171   if (tree)
172     {
173       mgt_hdr_it =
174         proto_tree_add_protocol_format (tree, proto_docsis_mgmt, tvb, 0, 20,
175                                         "Mac Management");
176       mgt_hdr_tree = proto_item_add_subtree (mgt_hdr_it, ett_docsis_mgmt);
177       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_dst_addr, tvb, 0, 6,
178                            FALSE);
179       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_src_addr, tvb, 6, 6,
180                            FALSE);
181       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_msg_len, tvb, 12, 2,
182                            ENC_BIG_ENDIAN);
183       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_dsap, tvb, 14, 1,
184                            FALSE);
185       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_ssap, tvb, 15, 1,
186                            FALSE);
187       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_control, tvb, 16, 1,
188                            FALSE);
189       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_version, tvb, 17, 1,
190                            ENC_BIG_ENDIAN);
191       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_type, tvb, 18, 1,
192                            ENC_BIG_ENDIAN);
193       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_rsvd, tvb, 19, 1,
194                            FALSE);
195
196     }
197   /* Code to Call subdissector */
198   /* sub-dissectors are based on the type field */
199   type = tvb_get_guint8 (tvb, 18);
200   msg_len = tvb_get_ntohs (tvb, 12);
201   payload_tvb = tvb_new_subset (tvb, 20, msg_len - 6, msg_len - 6);
202
203   if (dissector_try_uint
204       (docsis_mgmt_dissector_table, type, payload_tvb, pinfo, tree))
205     return;
206   else
207     call_dissector (data_handle, payload_tvb, pinfo, tree);
208 }
209
210
211 /* Register the protocol with Wireshark */
212
213 /* this format is require because a script is used to build the C function
214    that calls all the protocol registration.
215 */
216
217
218 void
219 proto_register_docsis_mgmt (void)
220 {
221
222 /* Setup list of header fields  See Section 1.6.1 for details*/
223   static hf_register_info hf[] = {
224     {&hf_docsis_mgt_dst_addr,
225      {"Destination Address", "docsis_mgmt.dst",
226       FT_ETHER, BASE_NONE, NULL, 0x0,
227       NULL, HFILL}
228      },
229     {&hf_docsis_mgt_src_addr,
230      {"Source Address", "docsis_mgmt.src",
231       FT_ETHER, BASE_NONE, NULL, 0x0,
232       NULL, HFILL}
233      },
234     {&hf_docsis_mgt_msg_len,
235      {"Message Length - DSAP to End (Bytes)", "docsis_mgmt.msglen",
236       FT_UINT16, BASE_DEC, NULL, 0x0,
237       "Message Length", HFILL}
238      },
239     {&hf_docsis_mgt_dsap,
240      {"DSAP [0x00]", "docsis_mgmt.dsap",
241       FT_UINT8, BASE_HEX, NULL, 0x0,
242       "Destination SAP", HFILL}
243      },
244     {&hf_docsis_mgt_ssap,
245      {"SSAP [0x00]", "docsis_mgmt.ssap",
246       FT_UINT8, BASE_HEX, NULL, 0x0,
247       "Source SAP", HFILL}
248      },
249     {&hf_docsis_mgt_control,
250      {"Control [0x03]", "docsis_mgmt.control",
251       FT_UINT8, BASE_HEX, NULL, 0x0,
252       "Control", HFILL}
253      },
254     {&hf_docsis_mgt_version,
255      {"Version", "docsis_mgmt.version",
256       FT_UINT8, BASE_DEC, NULL, 0x0,
257       NULL, HFILL}
258      },
259     {&hf_docsis_mgt_type,
260      {"Type", "docsis_mgmt.type",
261       FT_UINT8, BASE_DEC, VALS (mgmt_type_vals), 0x0,
262       NULL, HFILL}
263      },
264     {&hf_docsis_mgt_rsvd,
265      {"Reserved [0x00]", "docsis_mgmt.rsvd",
266       FT_UINT8, BASE_DEC, NULL, 0x0,
267       "Reserved", HFILL}
268      },
269
270   };
271
272 /* Setup protocol subtree array */
273   static gint *ett[] = {
274     &ett_docsis_mgmt,
275     &ett_mgmt_pay,
276   };
277
278   docsis_mgmt_dissector_table = register_dissector_table ("docsis_mgmt",
279                                                           "DOCSIS Mac Management",
280                                                           FT_UINT8, BASE_DEC);
281
282
283 /* Register the protocol name and description */
284   proto_docsis_mgmt = proto_register_protocol ("DOCSIS Mac Management",
285                                                "DOCSIS MAC MGMT",
286                                                "docsis_mgmt");
287
288 /* Required function calls to register the header fields and subtrees used */
289   proto_register_field_array (proto_docsis_mgmt, hf, array_length (hf));
290   proto_register_subtree_array (ett, array_length (ett));
291
292   register_dissector ("docsis_mgmt", dissect_macmgmt, proto_docsis_mgmt);
293 }
294
295
296 /* If this dissector uses sub-dissector registration add a registration routine.
297    This format is required because a script is used to find these routines and
298    create the code that calls these routines.
299 */
300 void
301 proto_reg_handoff_docsis_mgmt (void)
302 {
303 #if 0
304   dissector_handle_t docsis_mgmt_handle;
305
306   docsis_mgmt_handle = find_dissector ("docsis_mgmt");
307   dissector_add_uint ("docsis", 0x03, docsis_mgmt_handle);
308 #endif
309
310   data_handle = find_dissector ("data");
311 }