Update Free Software Foundation address.
[metze/wireshark/wip.git] / epan / dissectors / packet-udld.c
1 /* packet-udld.c
2  * Routines for the disassembly of the "UniDirectional Link Detection"
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30
31 #include <epan/packet.h>
32 #include <epan/strutil.h>
33
34 #include <epan/oui.h>
35 #include <epan/nlpid.h>
36
37
38 /*
39  * See
40  *
41  *      http://www.ietf.org/internet-drafts/draft-foschiano-udld-02.txt
42  *
43  * for some information on UDLD.
44  */
45
46 /* Offsets in TLV structure. */
47 #define TLV_TYPE        0
48 #define TLV_LENGTH      2
49
50 static int proto_udld = -1;
51 static int hf_udld_version = -1;
52 static int hf_udld_opcode = -1;
53 static int hf_udld_flags = -1;
54 static int hf_udld_flags_rt = -1;
55 static int hf_udld_flags_rsy = -1;
56 static int hf_udld_checksum = -1;
57 static int hf_udld_tlvtype = -1;
58 static int hf_udld_tlvlength = -1;
59
60 static gint ett_udld = -1;
61 static gint ett_udld_flags = -1;
62 static gint ett_udld_tlv = -1;
63
64 static dissector_handle_t data_handle;
65
66 #define TYPE_DEVICE_ID          0x0001
67 #define TYPE_PORT_ID            0x0002
68 #define TYPE_ECHO               0x0003
69 #define TYPE_MESSAGE_INTERVAL   0x0004
70 #define TYPE_TIMEOUT_INTERVAL   0x0005
71 #define TYPE_DEVICE_NAME        0x0006
72 #define TYPE_SEQUENCE_NUMBER    0x0007
73
74
75 static const value_string type_vals[] = {
76         { TYPE_DEVICE_ID,       "Device ID" },
77         { TYPE_PORT_ID,         "Port ID" },
78         { TYPE_ECHO,            "Echo" },
79         { TYPE_MESSAGE_INTERVAL,"Message interval" },
80         { TYPE_TIMEOUT_INTERVAL,"Timeout interval" },
81         { TYPE_DEVICE_NAME,     "Device name" },
82         { TYPE_SEQUENCE_NUMBER, "Sequence number" },
83         { 0,                    NULL }
84 };
85
86 #define OPCODE_RESERVED         0x00
87 #define OPCODE_PROBE            0x01
88 #define OPCODE_ECHO             0x02
89 #define OPCODE_FLUSH            0x03
90
91 static const value_string opcode_vals[] = {
92         { OPCODE_RESERVED,      "Reserved" },
93         { OPCODE_PROBE,         "Probe" },
94         { OPCODE_ECHO,          "Echo" },
95         { OPCODE_FLUSH,         "Flush" },
96         { 0,                    NULL }
97 };
98
99 static void
100 dissect_udld(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
101 {
102     proto_item *ti;
103     proto_tree *udld_tree = NULL;
104     int offset = 0;
105     guint16 type;
106     guint16 length;
107     proto_item *tlvi;
108     proto_tree *tlv_tree;
109     int real_length;
110
111     col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDLD");
112     col_clear(pinfo->cinfo, COL_INFO);
113
114     if (tree) {
115         proto_item *flags_ti;
116         proto_tree *flags_tree;
117
118         ti = proto_tree_add_item(tree, proto_udld, tvb, offset, -1, ENC_NA);
119         udld_tree = proto_item_add_subtree(ti, ett_udld);
120
121         /* UDLD header */
122         proto_tree_add_item(udld_tree, hf_udld_version, tvb, offset, 1, ENC_BIG_ENDIAN);
123         proto_tree_add_item(udld_tree, hf_udld_opcode, tvb, offset, 1, ENC_BIG_ENDIAN);
124         offset += 1;
125         flags_ti = proto_tree_add_item(udld_tree, hf_udld_flags, tvb, offset, 1, ENC_BIG_ENDIAN);
126         flags_tree = proto_item_add_subtree(flags_ti, ett_udld_flags);
127         proto_tree_add_item(flags_tree, hf_udld_flags_rt, tvb, offset, 1, ENC_BIG_ENDIAN);
128         proto_tree_add_item(flags_tree, hf_udld_flags_rsy, tvb, offset, 1, ENC_BIG_ENDIAN);
129         offset += 1;
130         proto_tree_add_item(udld_tree, hf_udld_checksum, tvb, offset, 2, ENC_BIG_ENDIAN);
131         offset += 2;
132     } else {
133         offset += 4; /* The version/opcode/flags/checksum fields from above */
134     }
135
136         while (tvb_reported_length_remaining(tvb, offset) != 0) {
137             type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
138             length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
139             if (length < 4) {
140                     if (tree) {
141                     tlvi = proto_tree_add_text(udld_tree, tvb, offset, 4,
142                         "TLV with invalid length %u (< 4)",
143                         length);
144                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
145                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
146                                 offset + TLV_TYPE, 2, type);
147                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
148                                 offset + TLV_LENGTH, 2, length);
149                     }
150                     offset += 4;
151                     break;
152             }
153
154             switch (type) {
155
156             case TYPE_DEVICE_ID:
157                 /* Device ID */
158
159                 if (check_col(pinfo->cinfo, COL_INFO))
160                     col_append_fstr(pinfo->cinfo, COL_INFO,
161                                     "Device ID: %s  ",
162                                     tvb_format_stringzpad(tvb, offset + 4,
163                                                           length - 4));
164
165                 if (tree) {
166                     tlvi = proto_tree_add_text(udld_tree, tvb, offset,
167                                 length, "Device ID: %s",
168                                 tvb_format_stringzpad(tvb, offset + 4, length - 4));
169                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
170                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
171                                 offset + TLV_TYPE, 2, type);
172                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
173                                 offset + TLV_LENGTH, 2, length);
174                     proto_tree_add_text(tlv_tree, tvb, offset + 4,
175                                 length - 4, "Device ID: %s",
176                                 tvb_format_stringzpad(tvb, offset + 4, length - 4));
177                     }
178                     offset += length;
179                     break;
180
181             case TYPE_PORT_ID:
182                 real_length = length;
183                 if (tvb_get_guint8(tvb, offset + real_length) != 0x00) {
184                     /* The length in the TLV doesn't appear to be the
185                        length of the TLV, as the byte just past it
186                        isn't the first byte of a 2-byte big-endian
187                        small integer; make the length of the TLV the length
188                        in the TLV, plus 4 bytes for the TLV type and length,
189                        minus 1 because that's what makes one capture work. */
190                     real_length = length + 3;
191                 }
192
193                 if (check_col(pinfo->cinfo, COL_INFO))
194                     col_append_fstr(pinfo->cinfo, COL_INFO,
195                                     "Port ID: %s  ",
196                                     tvb_format_stringzpad(tvb, offset + 4, length - 4));
197
198                 if (tree) {
199                     tlvi = proto_tree_add_text(udld_tree, tvb, offset,
200                             real_length, "Port ID: %s",
201                             tvb_format_text(tvb, offset + 4, real_length - 4));
202                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
203                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
204                             offset + TLV_TYPE, 2, type);
205                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
206                             offset + TLV_LENGTH, 2, length);
207                     proto_tree_add_text(tlv_tree, tvb, offset + 4,
208                             real_length - 4,
209                             "Sent through Interface: %s",
210                             tvb_format_text(tvb, offset + 4, real_length - 4));
211                 }
212                 offset += real_length;
213                 break;
214
215             case TYPE_ECHO:
216             case TYPE_MESSAGE_INTERVAL:
217             case TYPE_TIMEOUT_INTERVAL:
218             case TYPE_DEVICE_NAME:
219             case TYPE_SEQUENCE_NUMBER:
220             default:
221                 tlvi = proto_tree_add_text(udld_tree, tvb, offset,
222                         length, "Type: %s, length: %u",
223                         val_to_str(type, type_vals, "Unknown (0x%04x)"),
224                         length);
225                 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
226                 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
227                         offset + TLV_TYPE, 2, type);
228                 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
229                         offset + TLV_LENGTH, 2, length);
230                 if (length > 4) {
231                         proto_tree_add_text(tlv_tree, tvb, offset + 4,
232                                 length - 4, "Data");
233                 } else {
234                         return;
235                 }
236                 offset += length;
237             }
238         }
239
240     call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset), pinfo, udld_tree);
241 }
242
243 void
244 proto_register_udld(void)
245 {
246     static hf_register_info hf[] = {
247         { &hf_udld_version,
248         { "Version",            "udld.version",  FT_UINT8, BASE_DEC, NULL, 0xE0,
249           NULL, HFILL }},
250
251         { &hf_udld_opcode,
252         { "Opcode",             "udld.opcode", FT_UINT8, BASE_DEC, VALS(opcode_vals), 0x1F,
253           NULL, HFILL }},
254
255         { &hf_udld_flags,
256         { "Flags",              "udld.flags", FT_UINT8, BASE_DEC, NULL, 0x0,
257           NULL, HFILL }},
258
259         { &hf_udld_flags_rt,
260         { "Recommended timeout",                "udld.flags.rt", FT_UINT8, BASE_HEX, NULL, 0x80,
261           NULL, HFILL }},
262
263         { &hf_udld_flags_rsy,
264         { "ReSynch",            "udld.flags.rsy", FT_UINT8, BASE_HEX, NULL, 0x40,
265           NULL, HFILL }},
266
267         { &hf_udld_checksum,
268         { "Checksum",           "udld.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
269           NULL, HFILL }},
270
271         { &hf_udld_tlvtype,
272         { "Type",               "udld.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
273           NULL, HFILL }},
274
275         { &hf_udld_tlvlength,
276         { "Length",             "udld.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
277           NULL, HFILL }}
278     };
279     static gint *ett[] = {
280         &ett_udld,
281         &ett_udld_flags,
282         &ett_udld_tlv
283     };
284
285     proto_udld = proto_register_protocol("Unidirectional Link Detection",
286                                         "UDLD", "udld");
287     proto_register_field_array(proto_udld, hf, array_length(hf));
288     proto_register_subtree_array(ett, array_length(ett));
289 }
290
291 void
292 proto_reg_handoff_udld(void)
293 {
294     dissector_handle_t udld_handle;
295
296     data_handle = find_dissector("data");
297     udld_handle = create_dissector_handle(dissect_udld, proto_udld);
298     dissector_add_uint("llc.cisco_pid", 0x0111, udld_handle);
299     dissector_add_uint("chdlctype", 0x0111, udld_handle);
300 }