2 * Routines for the disassembly of the Mikrotik Neighbor Discovery Protocol
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 http://wiki.mikrotik.com/wiki/Manual:IP/Neighbor_discovery
30 - Find out about first 4 bytes
31 - Find out about additional TLVs
32 - Find out about unpack values
40 #include <epan/packet.h>
41 #include <epan/emem.h>
42 #include <epan/expert.h>
44 /* protocol handles */
45 static int proto_mndp = -1;
48 static int ett_mndp = -1;
49 static int ett_mndp_tlv_header = -1;
53 static int hf_mndp_tlv_type = -1;
54 static int hf_mndp_tlv_length = -1;
55 static int hf_mndp_tlv_data = -1;
57 static int hf_mndp_header_unknown = -1;
58 static int hf_mndp_header_seqno = -1;
60 static int hf_mndp_mac = -1;
61 static int hf_mndp_softwareid = -1;
62 static int hf_mndp_version = -1;
63 static int hf_mndp_identity = -1;
64 static int hf_mndp_uptime = -1;
65 static int hf_mndp_platform = -1;
66 static int hf_mndp_board = -1;
67 static int hf_mndp_unpack = -1;
68 static int hf_mndp_ipv6address = -1;
70 #define PROTO_SHORT_NAME "MNDP"
71 #define PROTO_LONG_NAME "Mikrotik Neighbor Discovery Protocol"
73 #define PORT_MNDP 5678
75 /* ============= copy/paste/modify from value_string.[hc] ============== */
76 typedef struct _ext_value_string {
80 int (*specialfunction)(tvbuff_t *, packet_info *, proto_tree *, guint32,
81 guint32, const struct _ext_value_string *);
82 const struct _ext_value_string *evs;
87 match_strextval_idx(guint32 val, const ext_value_string *vs, gint *idx) {
91 while (vs[i].strptr) {
92 if (vs[i].value == val) {
107 extval_to_str_idx(guint32 val, const ext_value_string *vs, gint *idx, const char *fmt) {
113 ret = match_strextval_idx(val, vs, idx);
117 return ep_strdup_printf(fmt, val);
119 /* ============= end copy/paste/modify ============== */
121 /* Forward decls needed by mndp_tunnel_tlv_vals et al */
122 static int dissect_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *mndp_tree,
123 guint32 offset, guint32 length, const ext_value_string *value_array);
125 static const ext_value_string mndp_body_tlv_vals[] = {
126 { 1, "MAC-Address", &hf_mndp_mac, NULL, NULL },
127 { 5, "Identity", &hf_mndp_identity, NULL, NULL },
128 { 7, "Version", &hf_mndp_version, NULL, NULL },
129 { 8, "Platform", &hf_mndp_platform, NULL, NULL },
130 { 10, "Uptime", &hf_mndp_uptime, NULL, (void *)TRUE },
131 { 11, "Software-ID", &hf_mndp_softwareid, NULL, NULL },
132 { 12, "Board", &hf_mndp_board, NULL, NULL },
133 { 14, "Unpack", &hf_mndp_unpack, NULL, NULL },
134 { 15, "IPv6-Address", &hf_mndp_ipv6address, NULL, NULL },
136 { 0, NULL, NULL, NULL, NULL }
139 static const value_string mndp_unpack_vals[] = {
140 /* none|simple|uncompressed-headers|uncompressed-all */
147 dissect_tlv(tvbuff_t *tvb, packet_info *pinfo, proto_tree *mndp_tree,
148 guint32 offset, guint32 length _U_, const ext_value_string *value_array)
152 proto_item *tlv_item;
153 proto_item *tlv_tree;
154 proto_item *type_item;
159 tlv_type = tvb_get_ntohs(tvb, offset);
160 tlv_length = tvb_get_ntohs(tvb, offset + 2);
161 /* DISSECTOR_ASSERT(tlv_length >= 4); */
162 tlv_item = proto_tree_add_text(mndp_tree, tvb,
163 offset, tlv_length+4,
167 extval_to_str_idx(tlv_type, value_array, NULL, "Unknown"));
168 tlv_tree = proto_item_add_subtree(tlv_item,
169 ett_mndp_tlv_header);
170 type_item = proto_tree_add_item(tlv_tree, hf_mndp_tlv_type,
171 tvb, offset, 2, ENC_BIG_ENDIAN);
172 proto_item_append_text(type_item, " = %s",
173 extval_to_str_idx(tlv_type, value_array,
174 &type_index, "Unknown"));
176 proto_tree_add_item(tlv_tree, hf_mndp_tlv_length,
177 tvb, offset, 2, ENC_BIG_ENDIAN);
180 /* tlv_length -= 4; */
185 tlv_end = offset + tlv_length;
187 /* Make hf_ handling independent of specialfuncion */
188 /* FIXME: Properly handle encoding info */
189 if ( type_index != -1
190 && !value_array[type_index].specialfunction
191 && value_array[type_index].evs != NULL
193 encoding_info = value_array[type_index].evs ? TRUE : FALSE;
195 encoding_info = FALSE;
197 if ( type_index != -1 && value_array[type_index].hf_element) {
198 proto_tree_add_item(tlv_tree,
199 *(value_array[type_index].hf_element),
200 tvb, offset, tlv_length, encoding_info);
202 proto_tree_add_item(tlv_tree, hf_mndp_tlv_data,
203 tvb, offset, tlv_length, ENC_NA);
205 if ( type_index != -1 && value_array[type_index].specialfunction ) {
208 while (offset < tlv_end) {
209 newoffset = value_array[type_index].specialfunction (
210 tvb, pinfo, tlv_tree, offset, tlv_length,
211 value_array[type_index].evs);
212 DISSECTOR_ASSERT(newoffset > offset);
220 dissect_mndp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
223 proto_tree *mndp_tree = NULL;
225 guint32 packet_length;
227 if (check_col(pinfo->cinfo, COL_PROTOCOL))
228 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
230 packet_length = tvb_length(tvb);
233 /* Header dissection */
234 ti = proto_tree_add_item(tree, proto_mndp, tvb, offset, -1,
236 mndp_tree = proto_item_add_subtree(ti, ett_mndp);
238 proto_tree_add_item(mndp_tree, hf_mndp_header_unknown, tvb, offset, 2,
241 proto_tree_add_item(mndp_tree, hf_mndp_header_seqno, tvb, offset, 2,
245 while (offset < packet_length)
246 offset = dissect_tlv(tvb, pinfo, mndp_tree,
247 offset, 0, mndp_body_tlv_vals);
253 test_mndp(tvbuff_t *tvb)
255 /* Minimum of 8 bytes, 4 Bytes header + 1 TLV-header */
256 if ( tvb_length(tvb) < 8
257 || tvb_get_guint8(tvb, 4) != 0
258 || tvb_get_guint8(tvb, 6) != 0
267 dissect_mndp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
269 if ( !test_mndp(tvb) ) {
272 dissect_mndp(tvb, pinfo, tree);
278 dissect_mndp_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
280 if ( !test_mndp(tvb) ) {
283 return dissect_mndp(tvb, pinfo, tree);
287 proto_register_mndp(void)
289 static hf_register_info hf[] = {
293 { "TlvType", "mndp.tlv.type", FT_UINT16, BASE_DEC, NULL,
296 { &hf_mndp_tlv_length,
297 { "TlvLength", "mndp.tlv.length", FT_UINT16, BASE_DEC, NULL,
301 { "TlvData", "mndp.tlv.data", FT_BYTES, BASE_NONE, NULL,
304 /* MNDP tunnel header */
305 { &hf_mndp_header_unknown,
306 { "Header Unknown", "mndp.header.unknown", FT_BYTES, BASE_NONE, NULL,
309 { &hf_mndp_header_seqno,
310 { "SeqNo", "mndp.header.seqno", FT_UINT16, BASE_DEC, NULL,
313 /* MNDP tunnel data */
315 { "MAC-Address", "mndp.mac", FT_ETHER, BASE_NONE, NULL,
318 { &hf_mndp_softwareid,
319 { "Software-ID", "mndp.softwareid", FT_STRING, BASE_NONE, NULL,
323 { "Version", "mndp.version", FT_STRING, BASE_NONE, NULL,
327 { "Identity", "mndp.identity", FT_STRING, BASE_NONE, NULL,
331 { "Uptime", "mndp.uptime", FT_RELATIVE_TIME, BASE_NONE, NULL,
335 { "Platform", "mndp.platform", FT_STRING, BASE_NONE, NULL,
339 { "Board", "mndp.board", FT_STRING, BASE_NONE, NULL,
343 { "Unpack", "mndp.unpack", FT_UINT8, BASE_NONE, VALS(mndp_unpack_vals),
346 { &hf_mndp_ipv6address,
347 { "IPv6-Address", "mndp.ipv6address", FT_IPv6, BASE_NONE, NULL,
351 static gint *ett[] = {
353 &ett_mndp_tlv_header,
356 proto_mndp = proto_register_protocol(PROTO_LONG_NAME, PROTO_SHORT_NAME, "mndp");
357 proto_register_field_array(proto_mndp, hf, array_length(hf));
358 proto_register_subtree_array(ett, array_length(ett));
362 proto_reg_handoff_mndp(void)
364 dissector_handle_t mndp_handle;
367 mndp_handle = new_create_dissector_handle(dissect_mndp_static, proto_mndp);
368 dissector_add_uint("udp.port", PORT_MNDP, mndp_handle);
369 /* heur_dissector_add("udp", dissect_mndp_heur, proto_mndp); */