Associate dissector tables and heuristic subdissector lists with a protocol.
[metze/wireshark/wip.git] / epan / dissectors / packet-foundry.c
1 /* packet-foundry.c
2  * Routines for the disassembly of Foundry LLC messages (currently
3  * Foundry Discovery Protocol - FDP only)
4  *
5  * Copyright 2012 Joerg Mayer (see AUTHORS file)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "config.h"
27
28 #include <epan/packet.h>
29 #include <epan/expert.h>
30 #include <epan/strutil.h>
31 #include "packet-llc.h"
32 #include <epan/oui.h>
33
34 void proto_register_fdp(void);
35 void proto_reg_handoff_fdp(void);
36
37 static int hf_llc_foundry_pid = -1;
38
39 static int proto_fdp = -1;
40 /* FDP header */
41 static int hf_fdp_version = -1;
42 static int hf_fdp_holdtime = -1;
43 static int hf_fdp_checksum = -1;
44 /* TLV header */
45 static int hf_fdp_tlv_type = -1;
46 static int hf_fdp_tlv_length = -1;
47 /* Unknown element */
48 static int hf_fdp_unknown = -1;
49 static int hf_fdp_unknown_data = -1;
50 /* Port Tag element */
51 static int hf_fdp_tag = -1;
52 static int hf_fdp_tag_native = -1;
53 static int hf_fdp_tag_type = -1;
54 static int hf_fdp_tag_unknown = -1;
55 /* VLAN Bitmap */
56 static int hf_fdp_vlanmap = -1;
57 static int hf_fdp_vlanmap_vlan = -1;
58 /* String element */
59 static int hf_fdp_string = -1;
60 static int hf_fdp_string_data = -1;
61 static int hf_fdp_string_text = -1;
62 /* Net? element */
63 static int hf_fdp_net = -1;
64 static int hf_fdp_net_unknown = -1;
65 static int hf_fdp_net_ip = -1;
66 static int hf_fdp_net_iplength = -1;
67
68 static gint ett_fdp = -1;
69 static gint ett_fdp_tlv_header = -1;
70 static gint ett_fdp_unknown = -1;
71 static gint ett_fdp_string = -1;
72 static gint ett_fdp_net = -1;
73 static gint ett_fdp_tag = -1;
74 static gint ett_fdp_vlanmap = -1;
75
76 static expert_field ei_fdp_tlv_length = EI_INIT;
77
78 #define PROTO_SHORT_NAME "FDP"
79 #define PROTO_LONG_NAME "Foundry Discovery Protocol"
80
81 static const value_string foundry_pid_vals[] = {
82         { 0x2000,       "FDP" },
83
84         { 0,            NULL }
85 };
86
87 typedef enum {
88         FDP_TYPE_NAME = 1,
89         FDP_TYPE_NET = 2,
90         FDP_TYPE_PORT = 3,
91         FDP_TYPE_CAPABILITIES = 4,
92         FDP_TYPE_VERSION = 5,
93         FDP_TYPE_MODEL = 6,
94         FDP_TYPE_VLANMAP = 0x0101,
95         FDP_TYPE_TAG = 0x0102
96 } fdp_type_t;
97
98 static const value_string fdp_type_vals[] = {
99         { FDP_TYPE_NAME,                "DeviceID"},
100         { FDP_TYPE_NET,                 "Net?"},
101         { FDP_TYPE_PORT,                "Interface"},
102         { FDP_TYPE_CAPABILITIES,        "Capabilities"},
103         { FDP_TYPE_VERSION,             "Version"},
104         { FDP_TYPE_MODEL,               "Platform"},
105         { FDP_TYPE_VLANMAP,             "VLAN-Bitmap"},
106         { FDP_TYPE_TAG,                 "Tagging-Info"},
107
108         { 0,    NULL }
109 };
110
111 static int
112 dissect_tlv_header(tvbuff_t *tvb, packet_info *pinfo _U_, int offset, int length _U_, proto_tree *tree)
113 {
114         proto_tree      *tlv_tree;
115         guint16         tlv_type;
116         guint16         tlv_length;
117
118         tlv_type = tvb_get_ntohs(tvb, offset);
119         tlv_length = tvb_get_ntohs(tvb, offset + 2);
120
121         tlv_tree = proto_tree_add_subtree_format(tree, tvb, offset, 4,
122                 ett_fdp_tlv_header, NULL, "Length %d, type %d = %s",
123                 tlv_length, tlv_type,
124                 val_to_str(tlv_type, fdp_type_vals, "Unknown (%d)"));
125
126         proto_tree_add_uint(tlv_tree, hf_fdp_tlv_type, tvb, offset, 2, tlv_type);
127         offset += 2;
128
129         proto_tree_add_uint(tlv_tree, hf_fdp_tlv_length, tvb, offset, 2, tlv_length);
130         offset += 2;
131
132         return offset;
133 }
134
135 static int
136 dissect_string_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree, const char* type_string)
137 {
138         proto_item      *string_item;
139         proto_tree      *string_tree;
140         guint8          *string_value;
141
142         string_item = proto_tree_add_protocol_format(tree, hf_fdp_string,
143                 tvb, offset, length, "%s", type_string);
144
145         string_tree = proto_item_add_subtree(string_item, ett_fdp_string);
146
147         dissect_tlv_header(tvb, pinfo, offset, 4, string_tree);
148         offset += 4;
149         length -= 4;
150
151         string_value = tvb_get_string_enc(wmem_packet_scope(), tvb, offset, length, ENC_ASCII);
152         proto_item_append_text(string_item, ": \"%s\"",
153                 format_text(string_value, strlen(string_value)));
154
155         proto_tree_add_item(string_tree, hf_fdp_string_data, tvb, offset, length, ENC_NA);
156         proto_tree_add_item(string_tree, hf_fdp_string_text, tvb, offset, length, ENC_ASCII|ENC_NA);
157
158         return offset;
159 }
160
161 static void
162 dissect_net_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
163 {
164         proto_item      *net_item;
165         proto_tree      *net_tree;
166
167         net_item = proto_tree_add_protocol_format(tree, hf_fdp_net,
168                 tvb, offset, length, "Net?");
169
170         net_tree = proto_item_add_subtree(net_item, ett_fdp_net);
171
172         dissect_tlv_header(tvb, pinfo, offset, 4, net_tree);
173         offset += 4;
174         length -= 4;
175
176         proto_tree_add_item(net_tree, hf_fdp_net_unknown, tvb, offset, 7, ENC_NA);
177         offset += 7;
178         length -= 7;
179
180         /* Length of IP address block in bytes */
181         proto_tree_add_item(net_tree, hf_fdp_net_iplength, tvb, offset, 2, ENC_BIG_ENDIAN);
182         offset += 2;
183         length -= 2;
184
185         while (length >= 4) {
186                 proto_tree_add_item(net_tree, hf_fdp_net_ip, tvb, offset, 4, ENC_NA);
187                 offset += 4;
188                 length -= 4;
189         }
190 }
191
192 static void
193 dissect_vlanmap_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
194 {
195         proto_item      *vlanmap_item;
196         proto_tree      *vlanmap_tree;
197         guint           vlan, voffset;
198         guint           bitoffset, byteoffset;
199
200         vlanmap_item = proto_tree_add_protocol_format(tree, hf_fdp_vlanmap,
201                 tvb, offset, length, "VLAN-Map");
202
203         vlanmap_tree = proto_item_add_subtree(vlanmap_item, ett_fdp_vlanmap);
204
205         dissect_tlv_header(tvb, pinfo, offset, 4, vlanmap_tree);
206         offset += 4;
207         length -= 4;
208
209         voffset = 1;
210         for (vlan = 1; vlan <= (guint)length*8; vlan++) {
211                 byteoffset = (vlan - voffset) / 8;
212                 bitoffset = (vlan - voffset) % 8;
213                 if (tvb_get_guint8(tvb, offset + byteoffset) & (1 << bitoffset)) {
214
215                         proto_tree_add_uint(vlanmap_tree, hf_fdp_vlanmap_vlan, tvb,
216                                 offset + byteoffset, 1, vlan);
217                 }
218         }
219 }
220
221 static void
222 dissect_tag_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
223 {
224         proto_item      *tag_item;
225         proto_tree      *tag_tree;
226
227         tag_item = proto_tree_add_protocol_format(tree, hf_fdp_tag,
228                 tvb, offset, length, "Port tag");
229
230         tag_tree = proto_item_add_subtree(tag_item, ett_fdp_tag);
231
232         dissect_tlv_header(tvb, pinfo, offset, 4, tag_tree);
233         offset += 4;
234         length -= 4;
235         proto_tree_add_item(tag_tree, hf_fdp_tag_native, tvb, offset, 2, ENC_BIG_ENDIAN);
236         offset += 2;
237         length -= 2;
238         proto_tree_add_item(tag_tree, hf_fdp_tag_type, tvb, offset, 2, ENC_BIG_ENDIAN);
239         offset += 2;
240         length -= 2;
241         proto_tree_add_item(tag_tree, hf_fdp_tag_unknown, tvb, offset, length, ENC_NA);
242 }
243
244 static void
245 dissect_unknown_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
246 {
247         proto_item      *unknown_item;
248         proto_tree      *unknown_tree;
249         guint16         tlv_type;
250
251         tlv_type = tvb_get_ntohs(tvb, offset);
252
253         unknown_item = proto_tree_add_protocol_format(tree, hf_fdp_unknown,
254                 tvb, offset, length, "Unknown element [%u]", tlv_type);
255
256         unknown_tree = proto_item_add_subtree(unknown_item, ett_fdp_unknown);
257
258         dissect_tlv_header(tvb, pinfo, offset, 4, unknown_tree);
259         offset += 4;
260         length -= 4;
261
262         proto_tree_add_item(unknown_tree, hf_fdp_unknown_data, tvb, offset, length, ENC_NA);
263 }
264
265 static int
266 dissect_fdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
267 {
268         proto_item *ti;
269         proto_tree *fdp_tree = NULL;
270         gint offset = 0;
271         guint16 tlv_type;
272         guint16 tlv_length;
273         gint data_length;
274         const char *type_string;
275
276         col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
277         col_set_str(pinfo->cinfo, COL_INFO, PROTO_SHORT_NAME ":");
278
279         if (tree) {
280                 data_length = tvb_reported_length_remaining(tvb, offset);
281
282                 ti = proto_tree_add_item(tree, proto_fdp, tvb, offset, -1, ENC_NA);
283                 fdp_tree = proto_item_add_subtree(ti, ett_fdp);
284
285                 proto_tree_add_item(fdp_tree, hf_fdp_version, tvb, offset, 1, ENC_BIG_ENDIAN);
286                 offset += 1;
287                 proto_tree_add_item(fdp_tree, hf_fdp_holdtime, tvb, offset, 1, ENC_BIG_ENDIAN);
288                 offset += 1;
289                 proto_tree_add_item(fdp_tree, hf_fdp_checksum, tvb, offset, 2, ENC_BIG_ENDIAN);
290                 offset += 2;
291
292                 /* Decode the individual TLVs */
293                 while (offset < data_length) {
294                         if (data_length - offset < 4) {
295                                 proto_tree_add_expert_format(fdp_tree, pinfo, &ei_fdp_tlv_length, tvb, offset, 4,
296                                         "Too few bytes left for TLV: %u (< 4)", data_length - offset);
297                                 break;
298                         }
299                         tlv_type = tvb_get_ntohs(tvb, offset);
300                         tlv_length = tvb_get_ntohs(tvb, offset + 2);
301
302                         if ((tlv_length < 4) || (tlv_length > (data_length - offset))) {
303                                 proto_tree_add_expert_format(fdp_tree, pinfo, &ei_fdp_tlv_length, tvb, offset, 0,
304                                         "TLV with invalid length: %u", tlv_length);
305                                 break;
306                         }
307                         type_string = val_to_str(tlv_type, fdp_type_vals, "[%u]");
308                         col_append_fstr(pinfo->cinfo, COL_INFO, " %s", type_string);
309
310                         switch (tlv_type) {
311                         case FDP_TYPE_NAME:
312                         case FDP_TYPE_PORT:
313                         case FDP_TYPE_CAPABILITIES:
314                         case FDP_TYPE_VERSION:
315                         case FDP_TYPE_MODEL:
316                                 dissect_string_tlv(tvb, pinfo, offset, tlv_length, fdp_tree, type_string);
317                                 break;
318                         case FDP_TYPE_NET:
319                                 dissect_net_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
320                                 break;
321                         case FDP_TYPE_TAG:
322                                 dissect_tag_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
323                                 break;
324                         case FDP_TYPE_VLANMAP:
325                                 dissect_vlanmap_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
326                                 break;
327                         default:
328                                 dissect_unknown_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
329                                 break;
330                         }
331                         offset += tlv_length;
332                 }
333
334         }
335         return tvb_captured_length(tvb);
336 }
337
338 void
339 proto_register_fdp(void)
340 {
341         static hf_register_info hf[] = {
342
343         /* FDP header */
344                 { &hf_fdp_version,
345                 { "Version?",   "fdp.version", FT_UINT8, BASE_DEC, NULL,
346                         0x0, NULL, HFILL }},
347
348                 { &hf_fdp_holdtime,
349                 { "Holdtime",   "fdp.holdtime", FT_UINT8, BASE_DEC, NULL,
350                         0x0, NULL, HFILL }},
351
352                 { &hf_fdp_checksum,
353                 { "Checksum?",  "fdp.checksum", FT_UINT16, BASE_HEX, NULL,
354                         0x0, NULL, HFILL }},
355
356         /* TLV header */
357                 { &hf_fdp_tlv_type,
358                 { "TLV type",   "fdp.tlv.type", FT_UINT16, BASE_DEC, VALS(fdp_type_vals),
359                         0x0, NULL, HFILL }},
360
361                 { &hf_fdp_tlv_length,
362                 { "TLV length", "fdp.tlv.length", FT_UINT16, BASE_DEC, NULL,
363                         0x0, NULL, HFILL }},
364
365         /* Unknown element */
366                 { &hf_fdp_unknown,
367                 { "Unknown",    "fdp.unknown", FT_PROTOCOL, BASE_NONE, NULL,
368                         0x0, NULL, HFILL }},
369
370                 { &hf_fdp_unknown_data,
371                 { "Unknown",    "fdp.unknown.data", FT_BYTES, BASE_NONE, NULL,
372                         0x0, NULL, HFILL }},
373
374         /* String element */
375                 { &hf_fdp_string,
376                 { "DeviceID",   "fdp.deviceid", FT_PROTOCOL, BASE_NONE, NULL,
377                         0x0, NULL, HFILL }},
378
379                 { &hf_fdp_string_data,
380                 { "Data",       "fdp.string.data", FT_BYTES, BASE_NONE, NULL,
381                         0x0, NULL, HFILL }},
382
383                 { &hf_fdp_string_text,
384                 { "Text",       "fdp.string.text", FT_STRING, BASE_NONE, NULL,
385                         0x0, NULL, HFILL }},
386
387         /* Net? element */
388                 { &hf_fdp_net,
389                 { "Net?",       "fdp.net", FT_PROTOCOL, BASE_NONE, NULL,
390                         0x0, NULL, HFILL }},
391
392                 { &hf_fdp_net_unknown,
393                 { "Net Unknown?",       "fdp.net.unknown", FT_BYTES, BASE_NONE, NULL,
394                         0x0, NULL, HFILL }},
395
396                 { &hf_fdp_net_iplength,
397                 { "Net IP Bytes?",      "fdp.net.iplength", FT_UINT16, BASE_DEC, NULL,
398                         0x0, "Number of bytes carrying IP addresses", HFILL }},
399
400                 { &hf_fdp_net_ip,
401                 { "Net IP Address?",    "fdp.net.ip", FT_IPv4, BASE_NONE, NULL,
402                         0x0, NULL, HFILL }},
403
404         /* VLAN Bitmap */
405                 { &hf_fdp_vlanmap,
406                 { "VLAN Map",   "fdp.vlanmap", FT_PROTOCOL, BASE_NONE, NULL,
407                         0x0, NULL, HFILL }},
408
409                 { &hf_fdp_vlanmap_vlan,
410                 { "VLAN",               "fdp.vlanmap.vlan", FT_UINT16, BASE_DEC, NULL,
411                         0x0, NULL, HFILL }},
412
413         /* Port Tag element */
414                 { &hf_fdp_tag,
415                 { "Tag",        "fdp.tag", FT_PROTOCOL, BASE_NONE, NULL,
416                         0x0, NULL, HFILL }},
417
418                 { &hf_fdp_tag_native,
419                 { "Native",     "fdp.tag.native", FT_UINT16, BASE_DEC, NULL,
420                         0x0, NULL, HFILL }},
421
422                 { &hf_fdp_tag_type,
423                 { "Type",       "fdp.tag.type", FT_UINT16, BASE_HEX, NULL,
424                         0x0, NULL, HFILL }},
425
426                 { &hf_fdp_tag_unknown,
427                 { "Unknown",    "fdp.tag.unknown", FT_BYTES, BASE_NONE, NULL,
428                         0x0, NULL, HFILL }},
429
430         };
431
432         static hf_register_info oui_hf[] = {
433           { &hf_llc_foundry_pid,
434                 { "PID",        "llc.foundry_pid",  FT_UINT16, BASE_HEX,
435                   VALS(foundry_pid_vals), 0x0, NULL, HFILL }
436           }
437         };
438
439         static gint *ett[] = {
440                 &ett_fdp,
441                 &ett_fdp_tlv_header,
442                 &ett_fdp_unknown,
443                 &ett_fdp_string,
444                 &ett_fdp_net,
445                 &ett_fdp_tag,
446                 &ett_fdp_vlanmap,
447         };
448
449         static ei_register_info ei[] = {
450                 { &ei_fdp_tlv_length, { "fdp.tlv.length.invalid", PI_MALFORMED, PI_ERROR, "Invalid length", EXPFILL }},
451         };
452
453         expert_module_t* expert_fdp;
454
455         proto_fdp = proto_register_protocol(PROTO_LONG_NAME, PROTO_SHORT_NAME, "fdp");
456
457         proto_register_field_array(proto_fdp, hf, array_length(hf));
458         proto_register_subtree_array(ett, array_length(ett));
459         expert_fdp = expert_register_protocol(proto_fdp);
460         expert_register_field_array(expert_fdp, ei, array_length(ei));
461
462         llc_add_oui(OUI_FOUNDRY, "llc.foundry_pid", "LLC Foundry OUI PID", oui_hf, proto_fdp);
463 }
464
465 void
466 proto_reg_handoff_fdp(void)
467 {
468         dissector_handle_t fdp_handle;
469
470         fdp_handle = create_dissector_handle(dissect_fdp, proto_fdp);
471         dissector_add_uint("llc.foundry_pid", 0x2000, fdp_handle);
472 }
473
474 /*
475  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
476  *
477  * Local variables:
478  * c-basic-offset: 8
479  * tab-width: 8
480  * indent-tabs-mode: t
481  * End:
482  *
483  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
484  * :indentSize=8:tabSize=8:noTabs=false:
485  */