Replace the types from sys/types.h and netinet/in.h by their glib.h
[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 <anarwani@cisco.com>
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28
29 #include "plugins/plugin_api.h"
30 #include "plugins/plugin_api_defs.h"
31 #include "moduleinfo.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38
39 #include <epan/packet.h>
40
41 #define MGT_SYNC 1
42 #define MGT_UCD 2
43 #define MGT_MAP 3
44 #define MGT_RNG_REQ 4
45 #define MGT_RNG_RSP 5
46 #define MGT_REG_REQ 6
47 #define MGT_REG_RSP 7
48 #define MGT_UCC_REQ 8
49 #define MGT_UCC_RSP 9
50 #define MGT_TRI_TCD 10
51 #define MGT_TRI_TSI 11
52 #define MGT_BPKM_REQ 12
53 #define MGT_BPKM_RSP 13
54 #define MGT_REG_ACK 14
55 #define MGT_DSA_REQ 15
56 #define MGT_DSA_RSP 16
57 #define MGT_DSA_ACK 17
58 #define MGT_DSC_REQ 18
59 #define MGT_DSC_RSP 19
60 #define MGT_DSC_ACK 20
61 #define MGT_DSD_REQ 21
62 #define MGT_DSD_RSP 22
63 #define MGT_DCC_REQ 23
64 #define MGT_DCC_RSP 24
65 #define MGT_DCC_ACK 25
66 #define MGT_DCI_REQ 26
67 #define MGT_DCI_RSP 27
68 #define MGT_UP_DIS 28
69
70
71 /* Initialize the protocol and registered fields */
72 static int proto_docsis_mgmt = -1;
73 static int hf_docsis_mgt_hdr = -1;
74 static int hf_docsis_mgt_dst_addr = -1;
75 static int hf_docsis_mgt_src_addr = -1;
76 static int hf_docsis_mgt_msg_len = -1;
77 static int hf_docsis_mgt_dsap = -1;
78 static int hf_docsis_mgt_ssap = -1;
79 static int hf_docsis_mgt_control = -1;
80 static int hf_docsis_mgt_version = -1;
81 static int hf_docsis_mgt_type = -1;
82 static int hf_docsis_mgt_rsvd = -1;
83
84 static dissector_table_t docsis_mgmt_dissector_table;
85 static dissector_handle_t data_handle;
86
87 /* Initialize the subtree pointers */
88 static gint ett_docsis_mgmt = -1;
89 static gint ett_mgmt_pay = -1;
90
91
92 static const value_string mgmt_type_vals[] = {
93   {MGT_SYNC, "Timing Synchronisation"},
94   {MGT_UCD, "Upstream Channel Descriptor"},
95   {MGT_MAP, "Upstream Bandwidth Allocation"},
96   {MGT_RNG_REQ, "Ranging Request"},
97   {MGT_RNG_RSP, "Ranging Response"},
98   {MGT_REG_REQ, "Registration Request"},
99   {MGT_REG_RSP, "Registration Response"},
100   {MGT_UCC_REQ, "Upstream Channel Change Request"},
101   {MGT_UCC_RSP, "Upstream Channel Change Response"},
102   {MGT_TRI_TCD, "Telephony Channel Descriptor"},
103   {MGT_TRI_TSI, "Termination System Information"},
104   {MGT_BPKM_REQ, "Privacy Key Management Request"},
105   {MGT_BPKM_RSP, "Privacy Key Management Response"},
106   {MGT_REG_ACK, "Registration Acknowledge"},
107   {MGT_DSA_REQ, "Dynamic Service Addition Request"},
108   {MGT_DSA_RSP, "Dynamic Service Addition Response"},
109   {MGT_DSA_ACK, "Dynamic Service Addition  Acknowledge"},
110   {MGT_DSC_REQ, "Dynamic Service Change Request"},
111   {MGT_DSC_RSP, "Dynamic Service Change Response"},
112   {MGT_DSC_ACK, "Dynamic Service Change Acknowledge"},
113   {MGT_DSD_REQ, "Dynamic Service Delete Request"},
114   {MGT_DSD_RSP, "Dynamic Service Delete Response"},
115   {MGT_DCC_REQ, "Dynamic Channel Change Request"},
116   {MGT_DCC_RSP, "Dynamic Channel Change Response"},
117   {MGT_DCC_ACK, "Dynamic Channel Change Acknowledge"},
118   {MGT_DCI_REQ, "Device Class Identification Request"},
119   {MGT_DCI_RSP, "Device Class Identification Response"},
120   {MGT_UP_DIS, "Upstream Channel Disable"},
121   {0, NULL}
122 };
123
124 /* Code to actually dissect the packets */
125 static void
126 dissect_macmgmt (tvbuff_t * tvb, packet_info * pinfo, proto_tree * tree)
127 {
128
129   const guint8 *src, *dst;
130   guint16 msg_len;
131   proto_item *mgt_hdr_it;
132   proto_tree *mgt_hdr_tree;
133   tvbuff_t *payload_tvb;
134   guint8 type;
135   if (check_col (pinfo->cinfo, COL_PROTOCOL))
136     col_set_str (pinfo->cinfo, COL_PROTOCOL, "DOCSIS MGMT");
137
138   if (check_col (pinfo->cinfo, COL_INFO))
139     col_clear (pinfo->cinfo, COL_INFO);
140
141
142   src = tvb_get_ptr (tvb, 6, 6);
143   dst = tvb_get_ptr (tvb, 0, 6);
144   SET_ADDRESS (&pinfo->dl_src, AT_ETHER, 6, src);
145   SET_ADDRESS (&pinfo->src, AT_ETHER, 6, src);
146   SET_ADDRESS (&pinfo->dl_dst, AT_ETHER, 6, dst);
147   SET_ADDRESS (&pinfo->dst, AT_ETHER, 6, dst);
148
149   if (tree)
150     {
151       mgt_hdr_it =
152         proto_tree_add_protocol_format (tree, proto_docsis_mgmt, tvb, 0, 20,
153                                         "Mac Management");
154       mgt_hdr_tree = proto_item_add_subtree (mgt_hdr_it, ett_docsis_mgmt);
155       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_dst_addr, tvb, 0, 6,
156                            FALSE);
157       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_src_addr, tvb, 6, 6,
158                            FALSE);
159       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_msg_len, tvb, 12, 2,
160                            FALSE);
161       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_dsap, tvb, 14, 1,
162                            FALSE);
163       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_ssap, tvb, 15, 1,
164                            FALSE);
165       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_control, tvb, 16, 1,
166                            FALSE);
167       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_version, tvb, 17, 1,
168                            FALSE);
169       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_type, tvb, 18, 1,
170                            FALSE);
171       proto_tree_add_item (mgt_hdr_tree, hf_docsis_mgt_rsvd, tvb, 19, 1,
172                            FALSE);
173
174     }
175   /* Code to Call subdissector */
176   /* sub-dissectors are based on the type field */
177   type = tvb_get_guint8 (tvb, 18);
178   msg_len = tvb_get_ntohs (tvb, 12);
179   payload_tvb = tvb_new_subset (tvb, 20, msg_len - 6, msg_len - 6);
180
181   if (dissector_try_port
182       (docsis_mgmt_dissector_table, type, payload_tvb, pinfo, tree))
183     return;
184   else
185     call_dissector (data_handle, payload_tvb, pinfo, tree);
186
187
188 }
189
190
191 /* Register the protocol with Ethereal */
192
193 /* this format is require because a script is used to build the C function
194    that calls all the protocol registration.
195 */
196
197
198 void
199 proto_register_docsis_mgmt (void)
200 {
201
202 /* Setup list of header fields  See Section 1.6.1 for details*/
203   static hf_register_info hf[] = {
204     {&hf_docsis_mgt_hdr,
205      {"Mac Management Header", "docsis.mgmt",
206       FT_BYTES, BASE_HEX, NULL, 0x0,
207       "Mac Management Header", HFILL}
208      },
209     {&hf_docsis_mgt_dst_addr,
210      {"Destination Address", "docsis.mgmt.dst",
211       FT_ETHER, BASE_HEX, NULL, 0x0,
212       "Destination Address", HFILL}
213      },
214     {&hf_docsis_mgt_src_addr,
215      {"Source Address", "docsis.mgmt.src",
216       FT_ETHER, BASE_HEX, NULL, 0x0,
217       "Source Address", HFILL}
218      },
219     {&hf_docsis_mgt_msg_len,
220      {"Message Length - DSAP to End (Bytes)", "docsis.mgmt.msglen",
221       FT_UINT16, BASE_DEC, NULL, 0x0,
222       "Message Length", HFILL}
223      },
224     {&hf_docsis_mgt_dsap,
225      {"DSAP [0x00]", "docsis.mgmt.dsap",
226       FT_UINT8, BASE_HEX, NULL, 0x0,
227       "Destination SAP", HFILL}
228      },
229     {&hf_docsis_mgt_ssap,
230      {"SSAP [0x00]", "docsis.mgmt.ssap",
231       FT_UINT8, BASE_HEX, NULL, 0x0,
232       "Source SAP", HFILL}
233      },
234     {&hf_docsis_mgt_control,
235      {"Control [0x03]", "docsis.mgmt.control",
236       FT_UINT8, BASE_HEX, NULL, 0x0,
237       "Control", HFILL}
238      },
239     {&hf_docsis_mgt_version,
240      {"Version", "docsis.mgmt.version",
241       FT_UINT8, BASE_DEC, NULL, 0x0,
242       "Version", HFILL}
243      },
244     {&hf_docsis_mgt_type,
245      {"Type", "docsis.mgmt.type",
246       FT_UINT8, BASE_DEC, VALS (mgmt_type_vals), 0x0,
247       "Type", HFILL}
248      },
249     {&hf_docsis_mgt_rsvd,
250      {"Reserved [0x00]", "docsis.mgmt.rsvd",
251       FT_UINT8, BASE_DEC, NULL, 0x0,
252       "Reserved", HFILL}
253      },
254
255   };
256
257 /* Setup protocol subtree array */
258   static gint *ett[] = {
259     &ett_docsis_mgmt,
260     &ett_mgmt_pay,
261   };
262
263   docsis_mgmt_dissector_table = register_dissector_table ("docsis_mgmt",
264                                                           "DOCSIS Mac Management",
265                                                           FT_UINT8, BASE_DEC);
266
267
268 /* Register the protocol name and description */
269   proto_docsis_mgmt = proto_register_protocol ("DOCSIS Mac Management",
270                                                "DOCSIS MAC MGMT",
271                                                "docsis_mgmt");
272
273 /* Required function calls to register the header fields and subtrees used */
274   proto_register_field_array (proto_docsis_mgmt, hf, array_length (hf));
275   proto_register_subtree_array (ett, array_length (ett));
276
277   register_dissector ("docsis_mgmt", dissect_macmgmt, proto_docsis_mgmt);
278 }
279
280
281 /* If this dissector uses sub-dissector registration add a registration routine.
282    This format is required because a script is used to find these routines and
283    create the code that calls these routines.
284 */
285 void
286 proto_reg_handoff_docsis_mgmt (void)
287 {
288   dissector_handle_t docsis_mgmt_handle;
289
290   docsis_mgmt_handle = find_dissector ("docsis_mgmt");
291   data_handle = find_dissector ("data");
292
293   dissector_add ("docsis", 0x03, docsis_mgmt_handle);
294 }