HACK: 2nd try to match RowsetProperties
[wireshark-wip.git] / epan / dissectors / packet-mndp.c
blob6e18725768c8c899ff935094615b9bb26cc84396
1 /* packet-mndp.c
2 * Routines for the disassembly of the Mikrotik Neighbor Discovery Protocol
4 * $Id$
6 * Copyright 2011 Joerg Mayer (see AUTHORS file)
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 http://wiki.mikrotik.com/wiki/Manual:IP/Neighbor_discovery
29 TODO:
30 - Find out about first 4 bytes
31 - Find out about additional TLVs
32 - Find out about unpack values
35 #include "config.h"
37 #include <glib.h>
38 #include <epan/packet.h>
39 #include <epan/wmem/wmem.h>
41 /* protocol handles */
42 static int proto_mndp = -1;
44 /* ett handles */
45 static int ett_mndp = -1;
46 static int ett_mndp_tlv_header = -1;
48 /* hf elements */
49 /* tlv generic */
50 static int hf_mndp_tlv_type = -1;
51 static int hf_mndp_tlv_length = -1;
52 static int hf_mndp_tlv_data = -1;
53 /* tunnel header */
54 static int hf_mndp_header_unknown = -1;
55 static int hf_mndp_header_seqno = -1;
56 /* tlvs */
57 static int hf_mndp_mac = -1;
58 static int hf_mndp_softwareid = -1;
59 static int hf_mndp_version = -1;
60 static int hf_mndp_identity = -1;
61 static int hf_mndp_uptime = -1;
62 static int hf_mndp_platform = -1;
63 static int hf_mndp_board = -1;
64 static int hf_mndp_unpack = -1;
65 static int hf_mndp_ipv6address = -1;
67 #define PROTO_SHORT_NAME "MNDP"
68 #define PROTO_LONG_NAME "Mikrotik Neighbor Discovery Protocol"
70 #define PORT_MNDP 5678
72 /* ============= copy/paste/modify from value_string.[hc] ============== */
73 typedef struct _ext_value_string {
74 guint32 value;
75 const gchar *strptr;
76 int* hf_element;
77 int (*specialfunction)(tvbuff_t *, packet_info *, proto_tree *, guint32,
78 guint32, const struct _ext_value_string *);
79 const struct _ext_value_string *evs;
80 } ext_value_string;
83 static const gchar*
84 match_strextval_idx(guint32 val, const ext_value_string *vs, gint *idx) {
85 gint i = 0;
87 if(vs) {
88 while (vs[i].strptr) {
89 if (vs[i].value == val) {
90 if (idx)
91 *idx = i;
92 return(vs[i].strptr);
94 i++;
98 if (idx)
99 *idx = -1;
100 return NULL;
103 static const gchar*
104 extval_to_str_idx(guint32 val, const ext_value_string *vs, gint *idx, const char *fmt) {
105 const gchar *ret;
107 if (!fmt)
108 fmt="Unknown";
110 ret = match_strextval_idx(val, vs, idx);
111 if (ret != NULL)
112 return ret;
114 return wmem_strdup_printf(wmem_packet_scope(), fmt, val);
116 /* ============= end copy/paste/modify ============== */
118 /* Forward decls needed by mndp_tunnel_tlv_vals et al */
119 static int dissect_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *mndp_tree,
120 guint32 offset, guint32 length, const ext_value_string *value_array);
122 static const ext_value_string mndp_body_tlv_vals[] = {
123 { 1, "MAC-Address", &hf_mndp_mac, NULL, NULL },
124 { 5, "Identity", &hf_mndp_identity, NULL, NULL },
125 { 7, "Version", &hf_mndp_version, NULL, NULL },
126 { 8, "Platform", &hf_mndp_platform, NULL, NULL },
127 { 10, "Uptime", &hf_mndp_uptime, NULL, (ext_value_string *)TRUE },
128 { 11, "Software-ID", &hf_mndp_softwareid, NULL, NULL },
129 { 12, "Board", &hf_mndp_board, NULL, NULL },
130 { 14, "Unpack", &hf_mndp_unpack, NULL, NULL },
131 { 15, "IPv6-Address", &hf_mndp_ipv6address, NULL, NULL },
133 { 0, NULL, NULL, NULL, NULL }
136 static const value_string mndp_unpack_vals[] = {
137 /* none|simple|uncompressed-headers|uncompressed-all */
138 { 1, "None" },
140 { 0, NULL }
143 static int
144 dissect_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *mndp_tree,
145 guint32 offset, guint32 length _U_, const ext_value_string *value_array)
147 guint32 tlv_type;
148 guint32 tlv_length;
149 proto_item *tlv_item;
150 proto_item *tlv_tree;
151 proto_item *type_item;
152 int type_index;
153 guint32 tlv_end;
154 guint encoding_info;
156 tlv_type = tvb_get_ntohs(tvb, offset);
157 tlv_length = tvb_get_ntohs(tvb, offset + 2);
158 /* DISSECTOR_ASSERT(tlv_length >= 4); */
159 tlv_item = proto_tree_add_text(mndp_tree, tvb,
160 offset, tlv_length+4,
161 "T %d, L %d: %s",
162 tlv_type,
163 tlv_length,
164 extval_to_str_idx(tlv_type, value_array, NULL, "Unknown"));
165 tlv_tree = proto_item_add_subtree(tlv_item,
166 ett_mndp_tlv_header);
167 type_item = proto_tree_add_item(tlv_tree, hf_mndp_tlv_type,
168 tvb, offset, 2, ENC_BIG_ENDIAN);
169 proto_item_append_text(type_item, " = %s",
170 extval_to_str_idx(tlv_type, value_array,
171 &type_index, "Unknown"));
172 offset += 2;
173 proto_tree_add_item(tlv_tree, hf_mndp_tlv_length,
174 tvb, offset, 2, ENC_BIG_ENDIAN);
175 offset += 2;
177 /* tlv_length -= 4; */
179 if (tlv_length == 0)
180 return offset;
182 tlv_end = offset + tlv_length;
184 /* Make hf_ handling independent of specialfuncion */
185 /* FIXME: Properly handle encoding info */
186 if ( type_index != -1
187 && !value_array[type_index].specialfunction
188 && value_array[type_index].evs != NULL
190 encoding_info = value_array[type_index].evs ? TRUE : FALSE;
191 } else {
192 encoding_info = FALSE;
194 if ( type_index != -1 && value_array[type_index].hf_element) {
195 proto_tree_add_item(tlv_tree,
196 *(value_array[type_index].hf_element),
197 tvb, offset, tlv_length, encoding_info);
198 } else {
199 proto_tree_add_item(tlv_tree, hf_mndp_tlv_data,
200 tvb, offset, tlv_length, ENC_NA);
202 if ( type_index != -1 && value_array[type_index].specialfunction ) {
203 guint32 newoffset;
205 while (offset < tlv_end) {
206 newoffset = value_array[type_index].specialfunction (
207 tvb, pinfo, tlv_tree, offset, tlv_length,
208 value_array[type_index].evs);
209 DISSECTOR_ASSERT(newoffset > offset);
210 offset = newoffset;
213 return tlv_end;
216 static int
217 dissect_mndp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
219 proto_item *ti;
220 proto_tree *mndp_tree;
221 guint32 offset = 0;
222 guint32 packet_length;
224 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
226 packet_length = tvb_length(tvb);
228 /* Header dissection */
229 ti = proto_tree_add_item(tree, proto_mndp, tvb, offset, -1,
230 ENC_NA);
231 mndp_tree = proto_item_add_subtree(ti, ett_mndp);
233 proto_tree_add_item(mndp_tree, hf_mndp_header_unknown, tvb, offset, 2,
234 ENC_NA);
235 offset += 2;
236 proto_tree_add_item(mndp_tree, hf_mndp_header_seqno, tvb, offset, 2,
237 ENC_BIG_ENDIAN);
238 offset += 2;
240 while (offset < packet_length) {
241 offset = dissect_tlv(tvb, pinfo, mndp_tree,
242 offset, 0, mndp_body_tlv_vals);
245 return offset;
248 static gboolean
249 test_mndp(tvbuff_t *tvb)
251 /* Minimum of 8 bytes, 4 Bytes header + 1 TLV-header */
252 if ( tvb_length(tvb) < 8
253 || tvb_get_guint8(tvb, 4) != 0
254 || tvb_get_guint8(tvb, 6) != 0
256 return FALSE;
258 return TRUE;
261 #if 0
262 static gboolean
263 dissect_mndp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
265 if ( !test_mndp(tvb) ) {
266 return FALSE;
268 dissect_mndp(tvb, pinfo, tree);
269 return TRUE;
271 #endif
273 static int
274 dissect_mndp_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
276 if ( !test_mndp(tvb) ) {
277 return 0;
279 return dissect_mndp(tvb, pinfo, tree);
282 void
283 proto_register_mndp(void)
285 static hf_register_info hf[] = {
287 /* TLV fields */
288 { &hf_mndp_tlv_type,
289 { "TlvType", "mndp.tlv.type", FT_UINT16, BASE_DEC, NULL,
290 0x0, NULL, HFILL }},
292 { &hf_mndp_tlv_length,
293 { "TlvLength", "mndp.tlv.length", FT_UINT16, BASE_DEC, NULL,
294 0x0, NULL, HFILL }},
296 { &hf_mndp_tlv_data,
297 { "TlvData", "mndp.tlv.data", FT_BYTES, BASE_NONE, NULL,
298 0x0, NULL, HFILL }},
300 /* MNDP tunnel header */
301 { &hf_mndp_header_unknown,
302 { "Header Unknown", "mndp.header.unknown", FT_BYTES, BASE_NONE, NULL,
303 0x0, NULL, HFILL }},
305 { &hf_mndp_header_seqno,
306 { "SeqNo", "mndp.header.seqno", FT_UINT16, BASE_DEC, NULL,
307 0x0, NULL, HFILL }},
309 /* MNDP tunnel data */
310 { &hf_mndp_mac,
311 { "MAC-Address", "mndp.mac", FT_ETHER, BASE_NONE, NULL,
312 0x0, NULL, HFILL }},
314 { &hf_mndp_softwareid,
315 { "Software-ID", "mndp.softwareid", FT_STRING, BASE_NONE, NULL,
316 0x0, NULL, HFILL }},
318 { &hf_mndp_version,
319 { "Version", "mndp.version", FT_STRING, BASE_NONE, NULL,
320 0x0, NULL, HFILL }},
322 { &hf_mndp_identity,
323 { "Identity", "mndp.identity", FT_STRING, BASE_NONE, NULL,
324 0x0, NULL, HFILL }},
326 { &hf_mndp_uptime,
327 { "Uptime", "mndp.uptime", FT_RELATIVE_TIME, BASE_NONE, NULL,
328 0x0, NULL, HFILL }},
330 { &hf_mndp_platform,
331 { "Platform", "mndp.platform", FT_STRING, BASE_NONE, NULL,
332 0x0, NULL, HFILL }},
334 { &hf_mndp_board,
335 { "Board", "mndp.board", FT_STRING, BASE_NONE, NULL,
336 0x0, NULL, HFILL }},
338 { &hf_mndp_unpack,
339 { "Unpack", "mndp.unpack", FT_UINT8, BASE_DEC, VALS(mndp_unpack_vals),
340 0x0, NULL, HFILL }},
342 { &hf_mndp_ipv6address,
343 { "IPv6-Address", "mndp.ipv6address", FT_IPv6, BASE_NONE, NULL,
344 0x0, NULL, HFILL }},
347 static gint *ett[] = {
348 &ett_mndp,
349 &ett_mndp_tlv_header,
352 proto_mndp = proto_register_protocol(PROTO_LONG_NAME, PROTO_SHORT_NAME, "mndp");
353 proto_register_field_array(proto_mndp, hf, array_length(hf));
354 proto_register_subtree_array(ett, array_length(ett));
357 void
358 proto_reg_handoff_mndp(void)
360 dissector_handle_t mndp_handle;
362 mndp_handle = new_create_dissector_handle(dissect_mndp_static, proto_mndp);
363 dissector_add_uint("udp.port", PORT_MNDP, mndp_handle);
364 /* heur_dissector_add("udp", dissect_mndp_heur, proto_mndp); */