For proto_tree_add_item(..., proto_xxx, ...)use ENC_NA as the encoding arg.
[obnox/wireshark/wip.git] / epan / dissectors / packet-mndp.c
1 /* packet-mndp.c
2  * Routines for the disassembly of the Mikrotik Neighbor Discovery Protocol
3  *
4  * $Id$
5  *
6  * Copyright 2011 Joerg Mayer (see AUTHORS file)
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*
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
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #  include "config.h"
37 #endif
38
39 #include <glib.h>
40 #include <epan/packet.h>
41 #include <epan/emem.h>
42 #include <epan/expert.h>
43
44 /* protocol handles */
45 static int proto_mndp = -1;
46
47 /* ett handles */
48 static int ett_mndp = -1;
49 static int ett_mndp_tlv_header = -1;
50
51 /* hf elements */
52 /* tlv generic */
53 static int hf_mndp_tlv_type = -1;
54 static int hf_mndp_tlv_length = -1;
55 static int hf_mndp_tlv_data = -1;
56 /* tunnel header */
57 static int hf_mndp_header_unknown = -1;
58 static int hf_mndp_header_seqno = -1;
59 /* tlvs */
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;
69
70 #define PROTO_SHORT_NAME "MNDP"
71 #define PROTO_LONG_NAME "Mikrotik Neighbor Discovery Protocol"
72
73 #define PORT_MNDP       5678
74
75 /* ============= copy/paste/modify from value_string.[hc] ============== */
76 typedef struct _ext_value_string {
77   guint32  value;
78   const gchar   *strptr;
79   int* hf_element;
80   int (*specialfunction)(tvbuff_t *, packet_info *, proto_tree *, guint32,
81         guint32, const struct _ext_value_string *);
82   const struct _ext_value_string *evs;
83 } ext_value_string;
84
85
86 static const gchar*
87 match_strextval_idx(guint32 val, const ext_value_string *vs, gint *idx) {
88   gint i = 0;
89
90   if(vs) {
91     while (vs[i].strptr) {
92       if (vs[i].value == val) {
93         if (idx)
94           *idx = i;
95         return(vs[i].strptr);
96       }
97       i++;
98     }
99   }
100
101   if (idx)
102     *idx = -1;
103   return NULL;
104 }
105
106 static const gchar*
107 extval_to_str_idx(guint32 val, const ext_value_string *vs, gint *idx, const char *fmt) {
108   const gchar *ret;
109
110   if (!fmt)
111     fmt="Unknown";
112
113   ret = match_strextval_idx(val, vs, idx);
114   if (ret != NULL)
115     return ret;
116
117   return ep_strdup_printf(fmt, val);
118 }
119 /* ============= end copy/paste/modify  ============== */
120
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);
124
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 },
135
136         { 0,    NULL, NULL, NULL, NULL }
137 };
138
139 static const value_string mndp_unpack_vals[] = {
140         /* none|simple|uncompressed-headers|uncompressed-all */
141         { 1,    "None" },
142
143         { 0,    NULL }
144 };
145
146 static int
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)
149 {
150         guint32 tlv_type;
151         guint32 tlv_length;
152         proto_item *tlv_item;
153         proto_item *tlv_tree;
154         proto_item *type_item;
155         int type_index;
156         guint32 tlv_end;
157         guint encoding_info;
158
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,
164                 "T %d, L %d: %s",
165                 tlv_type,
166                 tlv_length,
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"));
175         offset += 2;
176         proto_tree_add_item(tlv_tree, hf_mndp_tlv_length,
177                 tvb, offset, 2, ENC_BIG_ENDIAN);
178         offset += 2;
179
180         /* tlv_length -= 4; */
181
182         if (tlv_length == 0)
183                 return offset;
184
185         tlv_end = offset + tlv_length;
186
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
192         ) {
193                 encoding_info = value_array[type_index].evs ? TRUE : FALSE;
194         } else {
195                 encoding_info = FALSE;
196         }
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);
201         } else {
202                 proto_tree_add_item(tlv_tree, hf_mndp_tlv_data,
203                         tvb, offset, tlv_length, ENC_NA);
204         }
205         if ( type_index != -1 && value_array[type_index].specialfunction ) {
206                 guint32 newoffset;
207
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);
213                         offset = newoffset;
214                 }
215         }
216         return tlv_end;
217 }
218
219 static int
220 dissect_mndp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
221 {
222         proto_item *ti;
223         proto_tree *mndp_tree = NULL;
224         guint32 offset = 0;
225         guint32 packet_length;
226
227         if (check_col(pinfo->cinfo, COL_PROTOCOL))
228                 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
229
230         packet_length = tvb_length(tvb);
231
232         if (tree) {
233                 /* Header dissection */
234                 ti = proto_tree_add_item(tree, proto_mndp, tvb, offset, -1,
235                     ENC_NA);
236                 mndp_tree = proto_item_add_subtree(ti, ett_mndp);
237
238                 proto_tree_add_item(mndp_tree, hf_mndp_header_unknown, tvb, offset, 2,
239                         ENC_NA);
240                 offset += 2;
241                 proto_tree_add_item(mndp_tree, hf_mndp_header_seqno, tvb, offset, 2,
242                         ENC_BIG_ENDIAN);
243                 offset += 2;
244
245                 while (offset < packet_length)
246                         offset = dissect_tlv(tvb, pinfo, mndp_tree,
247                                 offset, 0, mndp_body_tlv_vals);
248         }
249         return offset;
250 }
251
252 static gboolean
253 test_mndp(tvbuff_t *tvb)
254 {
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
259         ) {
260                 return FALSE;
261         }
262         return TRUE;
263 }
264
265 #if 0
266 static gboolean
267 dissect_mndp_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
268 {
269         if ( !test_mndp(tvb) ) {
270                 return FALSE;
271         }
272         dissect_mndp(tvb, pinfo, tree);
273         return TRUE;
274 }
275 #endif
276
277 static int
278 dissect_mndp_static(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
279 {
280         if ( !test_mndp(tvb) ) {
281                 return 0;
282         }
283         return dissect_mndp(tvb, pinfo, tree);
284 }
285
286 void
287 proto_register_mndp(void)
288 {
289         static hf_register_info hf[] = {
290
291         /* TLV fields */
292                 { &hf_mndp_tlv_type,
293                 { "TlvType",    "mndp.tlv.type", FT_UINT16, BASE_DEC, NULL,
294                         0x0, NULL, HFILL }},
295
296                 { &hf_mndp_tlv_length,
297                 { "TlvLength",  "mndp.tlv.length", FT_UINT16, BASE_DEC, NULL,
298                         0x0, NULL, HFILL }},
299
300                 { &hf_mndp_tlv_data,
301                 { "TlvData",   "mndp.tlv.data", FT_BYTES, BASE_NONE, NULL,
302                         0x0, NULL, HFILL }},
303
304         /* MNDP tunnel header */
305                 { &hf_mndp_header_unknown,
306                 { "Header Unknown",     "mndp.header.unknown", FT_BYTES, BASE_NONE, NULL,
307                         0x0, NULL, HFILL }},
308
309                 { &hf_mndp_header_seqno,
310                 { "SeqNo",      "mndp.header.seqno", FT_UINT16, BASE_DEC, NULL,
311                         0x0, NULL, HFILL }},
312
313         /* MNDP tunnel data */
314                 { &hf_mndp_mac,
315                 { "MAC-Address",        "mndp.mac", FT_ETHER, BASE_NONE, NULL,
316                         0x0, NULL, HFILL }},
317
318                 { &hf_mndp_softwareid,
319                 { "Software-ID", "mndp.softwareid", FT_STRING, BASE_NONE, NULL,
320                                 0x0, NULL, HFILL }},
321
322                 { &hf_mndp_version,
323                 { "Version", "mndp.version", FT_STRING, BASE_NONE, NULL,
324                                 0x0, NULL, HFILL }},
325
326                 { &hf_mndp_identity,
327                 { "Identity", "mndp.identity", FT_STRING, BASE_NONE, NULL,
328                                 0x0, NULL, HFILL }},
329
330                 { &hf_mndp_uptime,
331                 { "Uptime", "mndp.uptime", FT_RELATIVE_TIME, BASE_NONE, NULL,
332                                 0x0, NULL, HFILL }},
333
334                 { &hf_mndp_platform,
335                 { "Platform", "mndp.platform", FT_STRING, BASE_NONE, NULL,
336                                 0x0, NULL, HFILL }},
337
338                 { &hf_mndp_board,
339                 { "Board", "mndp.board", FT_STRING, BASE_NONE, NULL,
340                                 0x0, NULL, HFILL }},
341
342                 { &hf_mndp_unpack,
343                 { "Unpack", "mndp.unpack", FT_UINT8, BASE_NONE, VALS(mndp_unpack_vals),
344                                 0x0, NULL, HFILL }},
345
346                 { &hf_mndp_ipv6address,
347                 { "IPv6-Address", "mndp.ipv6address", FT_IPv6, BASE_NONE, NULL,
348                                 0x0, NULL, HFILL }},
349
350         };
351         static gint *ett[] = {
352                 &ett_mndp,
353                 &ett_mndp_tlv_header,
354         };
355
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));
359 }
360
361 void
362 proto_reg_handoff_mndp(void)
363 {
364         dissector_handle_t mndp_handle;
365
366
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); */
370 }
371