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