MSWSP: fix dissect_mswsp_smb()
[wireshark-wip.git] / epan / dissectors / packet-foundry.c
blob5c4eb3f8f9532f01446bafeb900701e52c27a58b
1 /* packet-foundry.c
2 * Routines for the disassembly of Foundry LLC messages (currently
3 * Foundry Discovery Protocol - FDP only)
5 * $Id$
7 * Copyright 2012 Joerg Mayer (see AUTHORS file)
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "config.h"
30 #include <glib.h>
31 #include <epan/packet.h>
32 #include <epan/strutil.h>
33 #include <epan/in_cksum.h>
34 #include "packet-llc.h"
35 #include <epan/oui.h>
37 static int hf_llc_foundry_pid = -1;
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;
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;
76 #define PROTO_SHORT_NAME "FDP"
77 #define PROTO_LONG_NAME "Foundry Discovery Protocol"
79 static const value_string foundry_pid_vals[] = {
80 { 0x2000, "FDP" },
82 { 0, NULL }
85 typedef enum {
86 FDP_TYPE_NAME = 1,
87 FDP_TYPE_NET = 2,
88 FDP_TYPE_PORT = 3,
89 FDP_TYPE_CAPABILITIES = 4,
90 FDP_TYPE_VERSION = 5,
91 FDP_TYPE_MODEL = 6,
92 FDP_TYPE_VLANMAP = 0x0101,
93 FDP_TYPE_TAG = 0x0102
94 } fdp_type_t;
96 static const value_string fdp_type_vals[] = {
97 { FDP_TYPE_NAME, "DeviceID"},
98 { FDP_TYPE_NET, "Net?"},
99 { FDP_TYPE_PORT, "Interface"},
100 { FDP_TYPE_CAPABILITIES, "Capabilities"},
101 { FDP_TYPE_VERSION, "Version"},
102 { FDP_TYPE_MODEL, "Platform"},
103 { FDP_TYPE_VLANMAP, "VLAN-Bitmap"},
104 { FDP_TYPE_TAG, "Tagging-Info"},
106 { 0, NULL }
109 static int
110 dissect_tlv_header(tvbuff_t *tvb, packet_info *pinfo _U_, int offset, int length _U_, proto_tree *tree)
112 proto_item *tlv_item;
113 proto_tree *tlv_tree;
114 guint16 tlv_type;
115 guint16 tlv_length;
117 tlv_type = tvb_get_ntohs(tvb, offset);
118 tlv_length = tvb_get_ntohs(tvb, offset + 2);
120 tlv_item = proto_tree_add_text(tree, tvb, offset, 4,
121 "Length %d, type %d = %s",
122 tlv_length, tlv_type,
123 val_to_str(tlv_type, fdp_type_vals, "Unknown (%d)"));
125 tlv_tree = proto_item_add_subtree(tlv_item, ett_fdp_tlv_header);
126 proto_tree_add_uint(tlv_tree, hf_fdp_tlv_type, tvb, offset, 2, tlv_type);
127 offset += 2;
129 proto_tree_add_uint(tlv_tree, hf_fdp_tlv_length, tvb, offset, 2, tlv_length);
130 offset += 2;
132 return offset;
135 static int
136 dissect_string_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree, const char* type_string)
138 proto_item *string_item;
139 proto_tree *string_tree;
140 guint8 *string_value;
142 string_item = proto_tree_add_protocol_format(tree, hf_fdp_string,
143 tvb, offset, length, "%s", type_string);
145 string_tree = proto_item_add_subtree(string_item, ett_fdp_string);
147 dissect_tlv_header(tvb, pinfo, offset, 4, string_tree);
148 offset += 4;
149 length -= 4;
151 string_value = tvb_get_string(wmem_packet_scope(), tvb, offset, length);
152 proto_item_append_text(string_item, ": \"%s\"",
153 format_text(string_value, strlen(string_value)));
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);
158 return offset;
161 static void
162 dissect_net_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
164 proto_item *net_item;
165 proto_tree *net_tree;
167 net_item = proto_tree_add_protocol_format(tree, hf_fdp_net,
168 tvb, offset, length, "Net?");
170 net_tree = proto_item_add_subtree(net_item, ett_fdp_net);
172 dissect_tlv_header(tvb, pinfo, offset, 4, net_tree);
173 offset += 4;
174 length -= 4;
176 proto_tree_add_item(net_tree, hf_fdp_net_unknown, tvb, offset, 7, ENC_NA);
177 offset += 7;
178 length -= 7;
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;
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;
192 static void
193 dissect_vlanmap_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
195 proto_item *vlanmap_item;
196 proto_tree *vlanmap_tree;
197 guint vlan, voffset;
198 guint bitoffset, byteoffset;
200 vlanmap_item = proto_tree_add_protocol_format(tree, hf_fdp_vlanmap,
201 tvb, offset, length, "VLAN-Map");
203 vlanmap_tree = proto_item_add_subtree(vlanmap_item, ett_fdp_vlanmap);
205 dissect_tlv_header(tvb, pinfo, offset, 4, vlanmap_tree);
206 offset += 4;
207 length -= 4;
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)) {
215 proto_tree_add_uint(vlanmap_tree, hf_fdp_vlanmap_vlan, tvb,
216 offset + byteoffset, 1, vlan);
221 static void
222 dissect_tag_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
224 proto_item *tag_item;
225 proto_tree *tag_tree;
227 tag_item = proto_tree_add_protocol_format(tree, hf_fdp_tag,
228 tvb, offset, length, "Port tag");
230 tag_tree = proto_item_add_subtree(tag_item, ett_fdp_tag);
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);
244 static void
245 dissect_unknown_tlv(tvbuff_t *tvb, packet_info *pinfo, int offset, int length, proto_tree *tree)
247 proto_item *unknown_item;
248 proto_tree *unknown_tree;
249 guint16 tlv_type;
251 tlv_type = tvb_get_ntohs(tvb, offset);
253 unknown_item = proto_tree_add_protocol_format(tree, hf_fdp_unknown,
254 tvb, offset, length, "Unknown element [%u]", tlv_type);
256 unknown_tree = proto_item_add_subtree(unknown_item, ett_fdp_unknown);
258 dissect_tlv_header(tvb, pinfo, offset, 4, unknown_tree);
259 offset += 4;
260 length -= 4;
262 proto_tree_add_item(unknown_tree, hf_fdp_unknown_data, tvb, offset, length, ENC_NA);
265 static void
266 dissect_fdp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
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;
276 col_set_str(pinfo->cinfo, COL_PROTOCOL, PROTO_SHORT_NAME);
277 col_set_str(pinfo->cinfo, COL_INFO, PROTO_SHORT_NAME ":");
279 if (tree) {
280 data_length = tvb_reported_length_remaining(tvb, offset);
282 ti = proto_tree_add_item(tree, proto_fdp, tvb, offset, -1, ENC_NA);
283 fdp_tree = proto_item_add_subtree(ti, ett_fdp);
285 proto_tree_add_item(fdp_tree, hf_fdp_version, tvb, offset, 1, ENC_NA);
286 offset += 1;
287 proto_tree_add_item(fdp_tree, hf_fdp_holdtime, tvb, offset, 1, ENC_NA);
288 offset += 1;
289 proto_tree_add_item(fdp_tree, hf_fdp_checksum, tvb, offset, 2, ENC_BIG_ENDIAN);
290 offset += 2;
292 /* Decode the individual TLVs */
293 while (offset < data_length) {
294 if (data_length - offset < 4) {
295 proto_tree_add_text(fdp_tree, tvb, offset, 4,
296 "Too few bytes left for TLV: %u (< 4)",
297 data_length - offset);
298 break;
300 tlv_type = tvb_get_ntohs(tvb, offset);
301 tlv_length = tvb_get_ntohs(tvb, offset + 2);
303 if ((tlv_length < 4) || (tlv_length > (data_length - offset))) {
304 proto_tree_add_text(fdp_tree, tvb, offset, 0,
305 "TLV with invalid length: %u", tlv_length);
306 break;
308 type_string = val_to_str(tlv_type, fdp_type_vals, "[%u]");
309 col_append_fstr(pinfo->cinfo, COL_INFO, " %s", type_string);
311 switch (tlv_type) {
312 case FDP_TYPE_NAME:
313 case FDP_TYPE_PORT:
314 case FDP_TYPE_CAPABILITIES:
315 case FDP_TYPE_VERSION:
316 case FDP_TYPE_MODEL:
317 dissect_string_tlv(tvb, pinfo, offset, tlv_length, fdp_tree, type_string);
318 break;
319 case FDP_TYPE_NET:
320 dissect_net_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
321 break;
322 case FDP_TYPE_TAG:
323 dissect_tag_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
324 break;
325 case FDP_TYPE_VLANMAP:
326 dissect_vlanmap_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
327 break;
328 default:
329 dissect_unknown_tlv(tvb, pinfo, offset, tlv_length, fdp_tree);
330 break;
332 offset += tlv_length;
338 void
339 proto_register_fdp(void)
341 static hf_register_info hf[] = {
343 /* FDP header */
344 { &hf_fdp_version,
345 { "Version?", "fdp.version", FT_UINT8, BASE_DEC, NULL,
346 0x0, NULL, HFILL }},
348 { &hf_fdp_holdtime,
349 { "Holdtime", "fdp.holdtime", FT_UINT16, BASE_DEC, NULL,
350 0x0, NULL, HFILL }},
352 { &hf_fdp_checksum,
353 { "Checksum?", "fdp.checksum", FT_UINT16, BASE_HEX, NULL,
354 0x0, NULL, HFILL }},
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 }},
361 { &hf_fdp_tlv_length,
362 { "TLV length", "fdp.tlv.length", FT_UINT16, BASE_DEC, NULL,
363 0x0, NULL, HFILL }},
365 /* Unknown element */
366 { &hf_fdp_unknown,
367 { "Unknown", "fdp.unknown", FT_PROTOCOL, BASE_NONE, NULL,
368 0x0, NULL, HFILL }},
370 { &hf_fdp_unknown_data,
371 { "Unknown", "fdp.unknown.data", FT_BYTES, BASE_NONE, NULL,
372 0x0, NULL, HFILL }},
374 /* String element */
375 { &hf_fdp_string,
376 { "DeviceID", "fdp.deviceid", FT_PROTOCOL, BASE_NONE, NULL,
377 0x0, NULL, HFILL }},
379 { &hf_fdp_string_data,
380 { "Data", "fdp.string.data", FT_BYTES, BASE_NONE, NULL,
381 0x0, NULL, HFILL }},
383 { &hf_fdp_string_text,
384 { "Text", "fdp.string.text", FT_STRING, BASE_NONE, NULL,
385 0x0, NULL, HFILL }},
387 /* Net? element */
388 { &hf_fdp_net,
389 { "Net?", "fdp.net", FT_PROTOCOL, BASE_NONE, NULL,
390 0x0, NULL, HFILL }},
392 { &hf_fdp_net_unknown,
393 { "Net Unknown?", "fdp.net.unknown", FT_BYTES, BASE_NONE, NULL,
394 0x0, NULL, HFILL }},
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 }},
400 { &hf_fdp_net_ip,
401 { "Net IP Address?", "fdp.net.ip", FT_IPv4, BASE_NONE, NULL,
402 0x0, NULL, HFILL }},
404 /* VLAN Bitmap */
405 { &hf_fdp_vlanmap,
406 { "VLAN Map", "fdp.vlanmap", FT_PROTOCOL, BASE_NONE, NULL,
407 0x0, NULL, HFILL }},
409 { &hf_fdp_vlanmap_vlan,
410 { "VLAN", "fdp.vlanmap.vlan", FT_UINT16, BASE_DEC, NULL,
411 0x0, NULL, HFILL }},
413 /* Port Tag element */
414 { &hf_fdp_tag,
415 { "Tag", "fdp.tag", FT_PROTOCOL, BASE_NONE, NULL,
416 0x0, NULL, HFILL }},
418 { &hf_fdp_tag_native,
419 { "Native", "fdp.tag.native", FT_UINT16, BASE_DEC, NULL,
420 0x0, NULL, HFILL }},
422 { &hf_fdp_tag_type,
423 { "Type", "fdp.tag.type", FT_UINT16, BASE_HEX, NULL,
424 0x0, NULL, HFILL }},
426 { &hf_fdp_tag_unknown,
427 { "Unknown", "fdp.tag.unknown", FT_BYTES, BASE_NONE, NULL,
428 0x0, NULL, HFILL }},
431 static gint *ett[] = {
432 &ett_fdp,
433 &ett_fdp_tlv_header,
434 &ett_fdp_unknown,
435 &ett_fdp_string,
436 &ett_fdp_net,
437 &ett_fdp_tag,
438 &ett_fdp_vlanmap,
441 proto_fdp = proto_register_protocol(PROTO_LONG_NAME,
442 PROTO_SHORT_NAME, "fdp");
443 proto_register_field_array(proto_fdp, hf, array_length(hf));
444 proto_register_subtree_array(ett, array_length(ett));
447 void
448 proto_reg_handoff_fdp(void)
450 dissector_handle_t fdp_handle;
452 fdp_handle = create_dissector_handle(dissect_fdp, proto_fdp);
453 dissector_add_uint("llc.foundry_pid", 0x2000, fdp_handle);
456 void
457 proto_register_foundry_oui(void)
459 static hf_register_info hf[] = {
460 { &hf_llc_foundry_pid,
461 { "PID", "llc.foundry_pid", FT_UINT16, BASE_HEX,
462 VALS(foundry_pid_vals), 0x0, NULL, HFILL }
466 llc_add_oui(OUI_FOUNDRY, "llc.foundry_pid", "LLC Foundry OUI PID", hf);