Document the new Copy Profile button.
[obnox/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     col_set_str(pinfo->cinfo, COL_PROTOCOL, "UDLD");
114     col_clear(pinfo->cinfo, COL_INFO);
115
116     if (tree) {
117         proto_item *flags_ti;
118         proto_tree *flags_tree;
119
120         ti = proto_tree_add_item(tree, proto_udld, tvb, offset, -1, FALSE);
121         udld_tree = proto_item_add_subtree(ti, ett_udld);
122
123         /* UDLD header */
124         proto_tree_add_item(udld_tree, hf_udld_version, tvb, offset, 1, FALSE);
125         proto_tree_add_item(udld_tree, hf_udld_opcode, tvb, offset, 1, FALSE);
126         offset += 1;
127         flags_ti = proto_tree_add_item(udld_tree, hf_udld_flags, tvb, offset, 1, FALSE);
128         flags_tree = proto_item_add_subtree(flags_ti, ett_udld_flags);
129         proto_tree_add_item(flags_tree, hf_udld_flags_rt, tvb, offset, 1, FALSE);
130         proto_tree_add_item(flags_tree, hf_udld_flags_rsy, tvb, offset, 1, FALSE);
131         offset += 1;
132         proto_tree_add_item(udld_tree, hf_udld_checksum, tvb, offset, 2, FALSE);
133         offset += 2;
134     } else {
135         offset += 4; /* The version/opcode/flags/checksum fields from above */
136     }
137
138         while (tvb_reported_length_remaining(tvb, offset) != 0) {
139             type = tvb_get_ntohs(tvb, offset + TLV_TYPE);
140             length = tvb_get_ntohs(tvb, offset + TLV_LENGTH);
141             if (length < 4) {
142                     if (tree) {
143                     tlvi = proto_tree_add_text(udld_tree, tvb, offset, 4,
144                         "TLV with invalid length %u (< 4)",
145                         length);
146                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
147                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
148                                 offset + TLV_TYPE, 2, type);
149                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
150                                 offset + TLV_LENGTH, 2, length);
151                     }
152                     offset += 4;
153                     break;
154             }
155
156             switch (type) {
157
158             case TYPE_DEVICE_ID:
159                 /* Device ID */
160
161                 if (check_col(pinfo->cinfo, COL_INFO))
162                     col_append_fstr(pinfo->cinfo, COL_INFO,
163                                     "Device ID: %s  ",
164                                     tvb_format_stringzpad(tvb, offset + 4,
165                                                           length - 4));
166
167                 if (tree) {
168                     tlvi = proto_tree_add_text(udld_tree, tvb, offset,
169                                 length, "Device ID: %s",
170                                 tvb_format_stringzpad(tvb, offset + 4, length - 4));
171                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
172                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
173                                 offset + TLV_TYPE, 2, type);
174                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
175                                 offset + TLV_LENGTH, 2, length);
176                     proto_tree_add_text(tlv_tree, tvb, offset + 4,
177                                 length - 4, "Device ID: %s",
178                                 tvb_format_stringzpad(tvb, offset + 4, length - 4));
179                     }
180                     offset += length;
181                     break;
182
183             case TYPE_PORT_ID:
184                 real_length = length;
185                 if (tvb_get_guint8(tvb, offset + real_length) != 0x00) {
186                     /* The length in the TLV doesn't appear to be the
187                        length of the TLV, as the byte just past it
188                        isn't the first byte of a 2-byte big-endian
189                        small integer; make the length of the TLV the length
190                        in the TLV, plus 4 bytes for the TLV type and length,
191                        minus 1 because that's what makes one capture work. */
192                     real_length = length + 3;
193                 }
194
195                 if (check_col(pinfo->cinfo, COL_INFO))
196                     col_append_fstr(pinfo->cinfo, COL_INFO,
197                                     "Port ID: %s  ",
198                                     tvb_format_stringzpad(tvb, offset + 4, length - 4));
199
200                 if (tree) { 
201                     tlvi = proto_tree_add_text(udld_tree, tvb, offset,
202                             real_length, "Port ID: %s",
203                             tvb_format_text(tvb, offset + 4, real_length - 4));
204                     tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
205                     proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
206                             offset + TLV_TYPE, 2, type);
207                     proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
208                             offset + TLV_LENGTH, 2, length);
209                     proto_tree_add_text(tlv_tree, tvb, offset + 4,
210                             real_length - 4,
211                             "Sent through Interface: %s",
212                             tvb_format_text(tvb, offset + 4, real_length - 4));
213                 }
214                 offset += real_length;
215                 break;
216
217             case TYPE_ECHO:
218             case TYPE_MESSAGE_INTERVAL:
219             case TYPE_TIMEOUT_INTERVAL:
220             case TYPE_DEVICE_NAME:
221             case TYPE_SEQUENCE_NUMBER:
222             default:
223                 tlvi = proto_tree_add_text(udld_tree, tvb, offset,
224                         length, "Type: %s, length: %u",
225                         val_to_str(type, type_vals, "Unknown (0x%04x)"),
226                         length);
227                 tlv_tree = proto_item_add_subtree(tlvi, ett_udld_tlv);
228                 proto_tree_add_uint(tlv_tree, hf_udld_tlvtype, tvb,
229                         offset + TLV_TYPE, 2, type);
230                 proto_tree_add_uint(tlv_tree, hf_udld_tlvlength, tvb,
231                         offset + TLV_LENGTH, 2, length);
232                 if (length > 4) {
233                         proto_tree_add_text(tlv_tree, tvb, offset + 4,
234                                 length - 4, "Data");
235                 } else {
236                         return;
237                 }
238                 offset += length;
239             }
240         }
241
242     call_dissector(data_handle, tvb_new_subset_remaining(tvb, offset), pinfo, udld_tree);
243 }
244
245 void
246 proto_register_udld(void)
247 {
248     static hf_register_info hf[] = {
249         { &hf_udld_version,
250         { "Version",            "udld.version",  FT_UINT8, BASE_DEC, NULL, 0xE0,
251           NULL, HFILL }},
252
253         { &hf_udld_opcode,
254         { "Opcode",             "udld.opcode", FT_UINT8, BASE_DEC, VALS(opcode_vals), 0x1F,
255           NULL, HFILL }},
256
257         { &hf_udld_flags,
258         { "Flags",              "udld.flags", FT_UINT8, BASE_DEC, NULL, 0x0,
259           NULL, HFILL }},
260
261         { &hf_udld_flags_rt,
262         { "Recommended timeout",                "udld.flags.rt", FT_UINT8, BASE_HEX, NULL, 0x80,
263           NULL, HFILL }},
264
265         { &hf_udld_flags_rsy,
266         { "ReSynch",            "udld.flags.rsy", FT_UINT8, BASE_HEX, NULL, 0x40,
267           NULL, HFILL }},
268
269         { &hf_udld_checksum,
270         { "Checksum",           "udld.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
271           NULL, HFILL }},
272
273         { &hf_udld_tlvtype,
274         { "Type",               "udld.tlv.type", FT_UINT16, BASE_HEX, VALS(type_vals), 0x0,
275           NULL, HFILL }},
276
277         { &hf_udld_tlvlength,
278         { "Length",             "udld.tlv.len", FT_UINT16, BASE_DEC, NULL, 0x0,
279           NULL, HFILL }}
280     };
281     static gint *ett[] = {
282         &ett_udld,
283         &ett_udld_flags,
284         &ett_udld_tlv
285     };
286
287     proto_udld = proto_register_protocol("Unidirectional Link Detection",
288                                         "UDLD", "udld");
289     proto_register_field_array(proto_udld, hf, array_length(hf));
290     proto_register_subtree_array(ett, array_length(ett));
291 }
292
293 void
294 proto_reg_handoff_udld(void)
295 {
296     dissector_handle_t udld_handle;
297
298     data_handle = find_dissector("data");
299     udld_handle = create_dissector_handle(dissect_udld, proto_udld);
300     dissector_add("llc.cisco_pid", 0x0111, udld_handle);
301     dissector_add("chdlctype", 0x0111, udld_handle);
302 }