HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / dissectors / packet-udld.c
blobde267230891af5b48313ae4079044e9567040015
1 /* packet-udld.c
2 * Routines for the disassembly of the "UniDirectional Link Detection"
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "config.h"
27 #include <glib.h>
29 #include <epan/packet.h>
30 #include <epan/strutil.h>
32 #include <epan/oui.h>
33 #include <epan/nlpid.h>
37 * See
39 * http://www.ietf.org/internet-drafts/draft-foschiano-udld-02.txt
41 * for some information on UDLD.
44 /* Offsets in TLV structure. */
45 #define TLV_TYPE 0
46 #define TLV_LENGTH 2
48 static int proto_udld = -1;
49 static int hf_udld_version = -1;
50 static int hf_udld_opcode = -1;
51 static int hf_udld_flags = -1;
52 static int hf_udld_flags_rt = -1;
53 static int hf_udld_flags_rsy = -1;
54 static int hf_udld_checksum = -1;
55 static int hf_udld_tlvtype = -1;
56 static int hf_udld_tlvlength = -1;
58 static gint ett_udld = -1;
59 static gint ett_udld_flags = -1;
60 static gint ett_udld_tlv = -1;
62 static dissector_handle_t data_handle;
64 #define TYPE_DEVICE_ID 0x0001
65 #define TYPE_PORT_ID 0x0002
66 #define TYPE_ECHO 0x0003
67 #define TYPE_MESSAGE_INTERVAL 0x0004
68 #define TYPE_TIMEOUT_INTERVAL 0x0005
69 #define TYPE_DEVICE_NAME 0x0006
70 #define TYPE_SEQUENCE_NUMBER 0x0007
73 static const value_string type_vals[] = {
74 { TYPE_DEVICE_ID, "Device ID" },
75 { TYPE_PORT_ID, "Port ID" },
76 { TYPE_ECHO, "Echo" },
77 { TYPE_MESSAGE_INTERVAL,"Message interval" },
78 { TYPE_TIMEOUT_INTERVAL,"Timeout interval" },
79 { TYPE_DEVICE_NAME, "Device name" },
80 { TYPE_SEQUENCE_NUMBER, "Sequence number" },
81 { 0, NULL }
84 #define OPCODE_RESERVED 0x00
85 #define OPCODE_PROBE 0x01
86 #define OPCODE_ECHO 0x02
87 #define OPCODE_FLUSH 0x03
89 static const value_string opcode_vals[] = {
90 { OPCODE_RESERVED, "Reserved" },
91 { OPCODE_PROBE, "Probe" },
92 { OPCODE_ECHO, "Echo" },
93 { OPCODE_FLUSH, "Flush" },
94 { 0, NULL }
97 static void
98 dissect_udld(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
100 proto_item *ti;
101 proto_tree *udld_tree = NULL;
102 int offset = 0;
103 guint16 type;
104 guint16 length;
105 proto_item *tlvi;
106 proto_tree *tlv_tree;
107 int real_length;
109 col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDLD");
110 col_clear(pinfo->cinfo, COL_INFO);
112 if (tree) {
113 proto_item *flags_ti;
114 proto_tree *flags_tree;
116 ti = proto_tree_add_item(tree, proto_udld, tvb, offset, -1, ENC_NA);
117 udld_tree = proto_item_add_subtree(ti, ett_udld);
119 /* UDLD header */
120 proto_tree_add_item(udld_tree, hf_udld_version, tvb, offset, 1, ENC_BIG_ENDIAN);
121 proto_tree_add_item(udld_tree, hf_udld_opcode, tvb, offset, 1, ENC_BIG_ENDIAN);
122 offset += 1;
123 flags_ti = proto_tree_add_item(udld_tree, hf_udld_flags, tvb, offset, 1, ENC_BIG_ENDIAN);
124 flags_tree = proto_item_add_subtree(flags_ti, ett_udld_flags);
125 proto_tree_add_item(flags_tree, hf_udld_flags_rt, tvb, offset, 1, ENC_BIG_ENDIAN);
126 proto_tree_add_item(flags_tree, hf_udld_flags_rsy, tvb, offset, 1, ENC_BIG_ENDIAN);
127 offset += 1;
128 proto_tree_add_item(udld_tree, hf_udld_checksum, tvb, offset, 2, ENC_BIG_ENDIAN);
129 offset += 2;
130 } else {
131 offset += 4; /* The version/opcode/flags/checksum fields from above */
134 while (tvb_reported_length_remaining(tvb, offset) != 0) {
135 type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
136 length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
137 if (length < 4) {
138 if (tree) {
139 tlvi = proto_tree_add_text(udld_tree, tvb, offset, 4,
140 "TLV with invalid length %u (< 4)",
141 length);
142 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
143 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
144 offset + TLV_TYPE, 2, type);
145 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
146 offset + TLV_LENGTH, 2, length);
148 offset += 4;
149 break;
152 switch (type) {
154 case TYPE_DEVICE_ID:
155 /* Device ID */
157 col_append_fstr(pinfo->cinfo, COL_INFO,
158 "Device ID: %s ",
159 tvb_format_stringzpad(tvb, offset + 4,
160 length - 4));
162 if (tree) {
163 tlvi = proto_tree_add_text(udld_tree, tvb, offset,
164 length, "Device ID: %s",
165 tvb_format_stringzpad(tvb, offset + 4, length - 4));
166 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
167 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
168 offset + TLV_TYPE, 2, type);
169 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
170 offset + TLV_LENGTH, 2, length);
171 proto_tree_add_text(tlv_tree, tvb, offset + 4,
172 length - 4, "Device ID: %s",
173 tvb_format_stringzpad(tvb, offset + 4, length - 4));
175 offset += length;
176 break;
178 case TYPE_PORT_ID:
179 real_length = length;
180 if (tvb_get_guint8(tvb, offset + real_length) != 0x00) {
181 /* The length in the TLV doesn't appear to be the
182 length of the TLV, as the byte just past it
183 isn't the first byte of a 2-byte big-endian
184 small integer; make the length of the TLV the length
185 in the TLV, plus 4 bytes for the TLV type and length,
186 minus 1 because that's what makes one capture work. */
187 real_length = length + 3;
190 col_append_fstr(pinfo->cinfo, COL_INFO,
191 "Port ID: %s ",
192 tvb_format_stringzpad(tvb, offset + 4, length - 4));
194 if (tree) {
195 tlvi = proto_tree_add_text(udld_tree, tvb, offset,
196 real_length, "Port ID: %s",
197 tvb_format_text(tvb, offset + 4, real_length - 4));
198 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
199 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
200 offset + TLV_TYPE, 2, type);
201 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
202 offset + TLV_LENGTH, 2, length);
203 proto_tree_add_text(tlv_tree, tvb, offset + 4,
204 real_length - 4,
205 "Sent through Interface: %s",
206 tvb_format_text(tvb, offset + 4, real_length - 4));
208 offset += real_length;
209 break;
211 case TYPE_ECHO:
212 case TYPE_MESSAGE_INTERVAL:
213 case TYPE_TIMEOUT_INTERVAL:
214 case TYPE_DEVICE_NAME:
215 case TYPE_SEQUENCE_NUMBER:
216 default:
217 tlvi = proto_tree_add_text(udld_tree, tvb, offset,
218 length, "Type: %s, length: %u",
219 val_to_str(type, type_vals, "Unknown (0x%04x)"),
220 length);
221 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
222 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
223 offset + TLV_TYPE, 2, type);
224 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
225 offset + TLV_LENGTH, 2, length);
226 if (length > 4) {
227 proto_tree_add_text(tlv_tree, tvb, offset + 4,
228 length - 4, "Data");
229 } else {
230 return;
232 offset += length;
236 call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset), pinfo, udld_tree);
239 void
240 proto_register_udld(void)
242 static hf_register_info hf[] = {
243 { &hf_udld_version,
244 { "Version", "udld.version", FT_UINT8, BASE_DEC, NULL, 0xE0,
245 NULL, HFILL }},
247 { &hf_udld_opcode,
248 { "Opcode", "udld.opcode", FT_UINT8, BASE_DEC, VALS(opcode_vals), 0x1F,
249 NULL, HFILL }},
251 { &hf_udld_flags,
252 { "Flags", "udld.flags", FT_UINT8, BASE_DEC, NULL, 0x0,
253 NULL, HFILL }},
255 { &hf_udld_flags_rt,
256 { "Recommended timeout", "udld.flags.rt", FT_UINT8, BASE_HEX, NULL, 0x80,
257 NULL, HFILL }},
259 { &hf_udld_flags_rsy,
260 { "ReSynch", "udld.flags.rsy", FT_UINT8, BASE_HEX, NULL, 0x40,
261 NULL, HFILL }},
263 { &hf_udld_checksum,
264 { "Checksum", "udld.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
265 NULL, HFILL }},
267 { &hf_udld_tlvtype,
268 { "Type", "udld.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
269 NULL, HFILL }},
271 { &hf_udld_tlvlength,
272 { "Length", "udld.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
273 NULL, HFILL }}
275 static gint *ett[] = {
276 &ett_udld,
277 &ett_udld_flags,
278 &ett_udld_tlv
281 proto_udld = proto_register_protocol("Unidirectional Link Detection",
282 "UDLD", "udld");
283 proto_register_field_array(proto_udld, hf, array_length(hf));
284 proto_register_subtree_array(ett, array_length(ett));
287 void
288 proto_reg_handoff_udld(void)
290 dissector_handle_t udld_handle;
292 data_handle = find_dissector("data");
293 udld_handle = create_dissector_handle(dissect_udld, proto_udld);
294 dissector_add_uint("llc.cisco_pid", 0x0111, udld_handle);
295 dissector_add_uint("chdlc.protocol", 0x0111, udld_handle);