New tap for tethereal: io statistics that provides frames/bytes counts for frames...
[obnox/wireshark/wip.git] / packet-dec-bpdu.c
1 /* packet-dec-bpdu.c
2  * Routines for DEC BPDU (DEC Spanning Tree Protocol) disassembly
3  *
4  * $Id: packet-dec-bpdu.c,v 1.15 2002/08/28 21:00:12 jmayer Exp $
5  *
6  * Copyright 2001 Paul Ionescu <paul@acorp.ro>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include <epan/resolv.h>
36 #include "etypes.h"
37 #include "ppptypes.h"
38
39 /* Offsets of fields within a BPDU */
40
41 #define BPDU_DEC_CODE            0
42 #define BPDU_TYPE                1
43 #define BPDU_VERSION             2
44 #define BPDU_FLAGS               3
45 #define BPDU_ROOT_PRI            4
46 #define BPDU_ROOT_MAC            6
47 #define BPDU_ROOT_PATH_COST     12
48 #define BPDU_BRIDGE_PRI         14
49 #define BPDU_BRIDGE_MAC         16
50 #define BPDU_PORT_IDENTIFIER    22
51 #define BPDU_MESSAGE_AGE        23
52 #define BPDU_HELLO_TIME         24
53 #define BPDU_MAX_AGE            25
54 #define BPDU_FORWARD_DELAY      26
55
56 #define DEC_BPDU_SIZE           27
57
58 /* Flag bits */
59
60 #define BPDU_FLAGS_SHORT_TIMERS         0x80
61 #define BPDU_FLAGS_TCACK                0x02
62 #define BPDU_FLAGS_TC                   0x01
63
64 static int proto_dec_bpdu = -1;
65 static int hf_dec_bpdu_proto_id = -1;
66 static int hf_dec_bpdu_type = -1;
67 static int hf_dec_bpdu_version_id = -1;
68 static int hf_dec_bpdu_flags = -1;
69 static int hf_dec_bpdu_flags_short_timers = -1;
70 static int hf_dec_bpdu_flags_tcack = -1;
71 static int hf_dec_bpdu_flags_tc = -1;
72 static int hf_dec_bpdu_root_pri = -1;
73 static int hf_dec_bpdu_root_mac = -1;
74 static int hf_dec_bpdu_root_cost = -1;
75 static int hf_dec_bpdu_bridge_pri = -1;
76 static int hf_dec_bpdu_bridge_mac = -1;
77 static int hf_dec_bpdu_port_id = -1;
78 static int hf_dec_bpdu_msg_age = -1;
79 static int hf_dec_bpdu_hello_time = -1;
80 static int hf_dec_bpdu_max_age = -1;
81 static int hf_dec_bpdu_forward_delay = -1;
82
83 static gint ett_dec_bpdu = -1;
84 static gint ett_dec_bpdu_flags = -1;
85
86 static const value_string protocol_id_vals[] = {
87         { 0xe1, "DEC Spanning Tree Protocol" },
88         { 0,    NULL }
89 };
90
91 #define BPDU_TYPE_TOPOLOGY_CHANGE       2
92 #define BPDU_TYPE_HELLO                 25
93
94 static const value_string bpdu_type_vals[] = {
95         { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
96         { BPDU_TYPE_HELLO,           "Hello Packet" },
97         { 0,                         NULL }
98 };
99
100 static const char initial_sep[] = " (";
101 static const char cont_sep[] = ", ";
102
103 #define APPEND_BOOLEAN_FLAG(flag, item, string) \
104         if(flag){                                                       \
105                 if(item)                                                \
106                         proto_item_append_text(item, string, sep);      \
107                 sep = cont_sep;                                         \
108         }
109
110 static void
111 dissect_dec_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
112 {
113       guint8  protocol_version;
114       guint8  bpdu_type;
115       guint8  flags;
116       proto_tree *bpdu_tree;
117       proto_tree *flags_tree;
118       proto_item *ti;
119       const char *sep;
120
121       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
122             col_set_str(pinfo->cinfo, COL_PROTOCOL, "DEC_STP");
123       }
124       if (check_col(pinfo->cinfo, COL_INFO)) {
125             col_clear(pinfo->cinfo, COL_INFO);
126       }
127
128       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
129
130       if (check_col(pinfo->cinfo, COL_INFO)) {
131             col_add_str(pinfo->cinfo, COL_INFO,
132                         val_to_str(bpdu_type, bpdu_type_vals,
133                                    "Unknown BPDU type (%u)"));
134       }
135
136       set_actual_length(tvb, DEC_BPDU_SIZE);
137
138       if (tree) {
139             ti = proto_tree_add_item(tree, proto_dec_bpdu, tvb, 0, DEC_BPDU_SIZE,
140                                 FALSE);
141             bpdu_tree = proto_item_add_subtree(ti, ett_dec_bpdu);
142
143             protocol_version = tvb_get_guint8(tvb, BPDU_VERSION);
144
145             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_proto_id, tvb,
146                                 BPDU_DEC_CODE, 1, FALSE);
147
148             proto_tree_add_uint(bpdu_tree, hf_dec_bpdu_type, tvb,
149                                 BPDU_TYPE, 1, bpdu_type);
150
151             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_version_id, tvb,
152                                 BPDU_VERSION, 1, FALSE);
153
154             flags = tvb_get_guint8(tvb, BPDU_FLAGS);
155             ti = proto_tree_add_uint(bpdu_tree, hf_dec_bpdu_flags, tvb,
156                                      BPDU_FLAGS, 1, flags);
157             flags_tree = proto_item_add_subtree(ti, ett_dec_bpdu_flags);
158             sep = initial_sep;
159             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_SHORT_TIMERS, ti,
160                                 "%sUse short timers");
161             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_short_timers, tvb,
162                                    BPDU_FLAGS, 1, flags);
163             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, ti,
164                                 "%sTopology Change Acknowledgment");
165             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_tcack, tvb,
166                                    BPDU_FLAGS, 1, flags);
167             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, ti,
168                                 "%sTopology Change");
169             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_tc, tvb,
170                                    BPDU_FLAGS, 1, flags);
171             if (sep != initial_sep) {
172               /* We put something in; put in the terminating ")" */
173               proto_item_append_text(ti, ")");
174             }
175
176             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_pri, tvb,
177                                 BPDU_ROOT_PRI, 2, FALSE);
178             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_mac, tvb,
179                                 BPDU_ROOT_MAC, 6, FALSE);
180             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_cost, tvb,
181                                 BPDU_ROOT_PATH_COST, 2, FALSE);
182             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_pri, tvb,
183                                 BPDU_BRIDGE_PRI, 2, FALSE);
184             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_mac, tvb,
185                                 BPDU_BRIDGE_MAC, 6, FALSE);
186             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_port_id, tvb,
187                                 BPDU_PORT_IDENTIFIER, 1, FALSE);
188             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_msg_age, tvb,
189                                 BPDU_MESSAGE_AGE, 1, FALSE);
190             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_hello_time, tvb,
191                                 BPDU_HELLO_TIME, 1, FALSE);
192             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_max_age, tvb,
193                                 BPDU_MAX_AGE, 1, FALSE);
194             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_forward_delay, tvb,
195                                 BPDU_FORWARD_DELAY, 1, FALSE);
196
197       }
198 }
199
200 static const true_false_string yesno = {
201         "Yes",
202         "No"
203 };
204
205 void
206 proto_register_dec_bpdu(void)
207 {
208
209   static hf_register_info hf[] = {
210     { &hf_dec_bpdu_proto_id,
211       { "Protocol Identifier",          "dec_stp.protocol",
212         FT_UINT8,       BASE_HEX,       VALS(&protocol_id_vals), 0x0,
213         "", HFILL }},
214     { &hf_dec_bpdu_type,
215       { "BPDU Type",                    "dec_stp.type",
216         FT_UINT8,       BASE_DEC,       VALS(&bpdu_type_vals),  0x0,
217         "", HFILL }},
218     { &hf_dec_bpdu_version_id,
219       { "BPDU Version",                 "dec_stp.version",
220         FT_UINT8,       BASE_DEC,       NULL,   0x0,
221         "", HFILL }},
222     { &hf_dec_bpdu_flags,
223       { "BPDU flags",                   "dec_stp.flags",
224         FT_UINT8,       BASE_HEX,       NULL,   0x0,
225         "", HFILL }},
226     { &hf_dec_bpdu_flags_short_timers,
227       { "Use short timers",             "dec_stp.flags.short_timers",
228         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_SHORT_TIMERS,
229         "", HFILL }},
230     { &hf_dec_bpdu_flags_tcack,
231       { "Topology Change Acknowledgment",  "dec_stp.flags.tcack",
232         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TCACK,
233         "", HFILL }},
234     { &hf_dec_bpdu_flags_tc,
235       { "Topology Change",              "dec_stp.flags.tc",
236         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TC,
237         "", HFILL }},
238     { &hf_dec_bpdu_root_pri,
239       { "Root Priority",                "dec_stp.root.pri",
240         FT_UINT16,      BASE_DEC,       NULL,   0x0,
241         "", HFILL }},
242     { &hf_dec_bpdu_root_mac,
243       { "Root MAC",                     "dec_stp.root.mac",
244         FT_ETHER,       BASE_NONE,      NULL,   0x0,
245         "", HFILL }},
246     { &hf_dec_bpdu_root_cost,
247       { "Root Path Cost",               "dec_stp.root.cost",
248         FT_UINT16,      BASE_DEC,       NULL,   0x0,
249         "", HFILL }},
250     { &hf_dec_bpdu_bridge_pri,
251       { "Bridge Priority",              "dec_stp.bridge.pri",
252         FT_UINT16,      BASE_DEC,       NULL,   0x0,
253         "", HFILL }},
254     { &hf_dec_bpdu_bridge_mac,
255       { "Bridge MAC",                   "dec_stp.bridge.mac",
256         FT_ETHER,       BASE_NONE,      NULL,   0x0,
257         "", HFILL }},
258     { &hf_dec_bpdu_port_id,
259       { "Port identifier",              "dec_stp.port",
260         FT_UINT8,       BASE_DEC,       NULL,   0x0,
261         "", HFILL }},
262     { &hf_dec_bpdu_msg_age,
263       { "Message Age",                  "dec_stp.msg_age",
264         FT_UINT8,       BASE_DEC,       NULL,   0x0,
265         "", HFILL }},
266     { &hf_dec_bpdu_hello_time,
267       { "Hello Time",                   "dec_stp.hello",
268         FT_UINT8,       BASE_DEC,       NULL,   0x0,
269         "", HFILL }},
270     { &hf_dec_bpdu_max_age,
271       { "Max Age",                      "dec_stp.max_age",
272         FT_UINT8,       BASE_DEC,       NULL,   0x0,
273         "", HFILL }},
274     { &hf_dec_bpdu_forward_delay,
275       { "Forward Delay",                "dec_stp.forward",
276         FT_UINT8,       BASE_DEC,       NULL,   0x0,
277         "", HFILL }},
278   };
279   static gint *ett[] = {
280     &ett_dec_bpdu,
281     &ett_dec_bpdu_flags,
282   };
283
284   proto_dec_bpdu = proto_register_protocol("DEC Spanning Tree Protocol",
285                                            "DEC_STP", "dec_stp");
286   proto_register_field_array(proto_dec_bpdu, hf, array_length(hf));
287   proto_register_subtree_array(ett, array_length(ett));
288 }
289
290 void
291 proto_reg_handoff_dec_bpdu(void)
292 {
293   dissector_handle_t dec_bpdu_handle;
294
295   dec_bpdu_handle = create_dissector_handle(dissect_dec_bpdu,
296                                             proto_dec_bpdu);
297   dissector_add("ethertype", ETHERTYPE_DEC_LB, dec_bpdu_handle);
298   dissector_add("chdlctype", ETHERTYPE_DEC_LB, dec_bpdu_handle);
299   dissector_add("ppp.protocol", PPP_DEC_LB, dec_bpdu_handle);
300 }