In the hex dump, generate the offset at the beginning of each line in
[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.13 2002/03/19 09:02:01 guy 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 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
37 #endif
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <glib.h>
42 #include <epan/packet.h>
43 #include <epan/resolv.h>
44 #include "etypes.h"
45 #include "ppptypes.h"
46
47 /* Offsets of fields within a BPDU */
48
49 #define BPDU_DEC_CODE            0
50 #define BPDU_TYPE                1
51 #define BPDU_VERSION             2
52 #define BPDU_FLAGS               3
53 #define BPDU_ROOT_PRI            4
54 #define BPDU_ROOT_MAC            6
55 #define BPDU_ROOT_PATH_COST     12
56 #define BPDU_BRIDGE_PRI         14
57 #define BPDU_BRIDGE_MAC         16
58 #define BPDU_PORT_IDENTIFIER    22
59 #define BPDU_MESSAGE_AGE        23
60 #define BPDU_HELLO_TIME         24
61 #define BPDU_MAX_AGE            25
62 #define BPDU_FORWARD_DELAY      26
63
64 #define DEC_BPDU_SIZE           27
65
66 /* Flag bits */
67
68 #define BPDU_FLAGS_SHORT_TIMERS         0x80
69 #define BPDU_FLAGS_TCACK                0x02
70 #define BPDU_FLAGS_TC                   0x01
71
72 static int proto_dec_bpdu = -1;
73 static int hf_dec_bpdu_proto_id = -1;
74 static int hf_dec_bpdu_type = -1;
75 static int hf_dec_bpdu_version_id = -1;
76 static int hf_dec_bpdu_flags = -1;
77 static int hf_dec_bpdu_flags_short_timers = -1;
78 static int hf_dec_bpdu_flags_tcack = -1;
79 static int hf_dec_bpdu_flags_tc = -1;
80 static int hf_dec_bpdu_root_pri = -1;
81 static int hf_dec_bpdu_root_mac = -1;
82 static int hf_dec_bpdu_root_cost = -1;
83 static int hf_dec_bpdu_bridge_pri = -1;
84 static int hf_dec_bpdu_bridge_mac = -1;
85 static int hf_dec_bpdu_port_id = -1;
86 static int hf_dec_bpdu_msg_age = -1;
87 static int hf_dec_bpdu_hello_time = -1;
88 static int hf_dec_bpdu_max_age = -1;
89 static int hf_dec_bpdu_forward_delay = -1;
90
91 static gint ett_dec_bpdu = -1;
92 static gint ett_dec_bpdu_flags = -1;
93
94 static const value_string protocol_id_vals[] = {
95         { 0xe1, "DEC Spanning Tree Protocol" },
96         { 0,    NULL }
97 };
98
99 #define BPDU_TYPE_TOPOLOGY_CHANGE       2
100 #define BPDU_TYPE_HELLO                 25
101
102 static const value_string bpdu_type_vals[] = {
103         { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
104         { BPDU_TYPE_HELLO,           "Hello Packet" },
105         { 0,                         NULL }
106 };
107
108 static const char initial_sep[] = " (";
109 static const char cont_sep[] = ", ";
110
111 #define APPEND_BOOLEAN_FLAG(flag, item, string) \
112         if(flag){                                                       \
113                 if(item)                                                \
114                         proto_item_append_text(item, string, sep);      \
115                 sep = cont_sep;                                         \
116         }
117
118 static void
119 dissect_dec_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
120 {
121       guint8  protocol_version;
122       guint8  bpdu_type;
123       guint8  flags;
124       proto_tree *bpdu_tree;
125       proto_tree *flags_tree;
126       proto_item *ti;
127       const char *sep;
128
129       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
130             col_set_str(pinfo->cinfo, COL_PROTOCOL, "DEC_STP");
131       }
132       if (check_col(pinfo->cinfo, COL_INFO)) {
133             col_clear(pinfo->cinfo, COL_INFO);
134       }
135
136       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
137       
138       if (check_col(pinfo->cinfo, COL_INFO)) {
139             col_add_str(pinfo->cinfo, COL_INFO,
140                         val_to_str(bpdu_type, bpdu_type_vals,
141                                    "Unknown BPDU type (%u)"));
142       }
143       
144       set_actual_length(tvb, DEC_BPDU_SIZE);
145
146       if (tree) {
147             ti = proto_tree_add_item(tree, proto_dec_bpdu, tvb, 0, DEC_BPDU_SIZE,
148                                 FALSE);
149             bpdu_tree = proto_item_add_subtree(ti, ett_dec_bpdu);
150
151             protocol_version = tvb_get_guint8(tvb, BPDU_VERSION);
152
153             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_proto_id, tvb,
154                                 BPDU_DEC_CODE, 1, FALSE);
155
156             proto_tree_add_uint(bpdu_tree, hf_dec_bpdu_type, tvb,
157                                 BPDU_TYPE, 1, bpdu_type);
158
159             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_version_id, tvb,
160                                 BPDU_VERSION, 1, FALSE);
161
162             flags = tvb_get_guint8(tvb, BPDU_FLAGS);
163             ti = proto_tree_add_uint(bpdu_tree, hf_dec_bpdu_flags, tvb,
164                                      BPDU_FLAGS, 1, flags);
165             flags_tree = proto_item_add_subtree(ti, ett_dec_bpdu_flags);
166             sep = initial_sep;
167             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_SHORT_TIMERS, ti,
168                                 "%sUse short timers");
169             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_short_timers, tvb,
170                                    BPDU_FLAGS, 1, flags);
171             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, ti,
172                                 "%sTopology Change Acknowledgment");
173             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_tcack, tvb,
174                                    BPDU_FLAGS, 1, flags);
175             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, ti,
176                                 "%sTopology Change");
177             proto_tree_add_boolean(flags_tree, hf_dec_bpdu_flags_tc, tvb,
178                                    BPDU_FLAGS, 1, flags);
179             if (sep != initial_sep) {
180               /* We put something in; put in the terminating ")" */
181               proto_item_append_text(ti, ")");
182             }
183
184             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_pri, tvb,
185                                 BPDU_ROOT_PRI, 2, FALSE);
186             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_mac, tvb,
187                                 BPDU_ROOT_MAC, 6, FALSE);
188             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_root_cost, tvb,
189                                 BPDU_ROOT_PATH_COST, 2, FALSE);
190             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_pri, tvb,
191                                 BPDU_BRIDGE_PRI, 2, FALSE);
192             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_bridge_mac, tvb,
193                                 BPDU_BRIDGE_MAC, 6, FALSE);
194             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_port_id, tvb,
195                                 BPDU_PORT_IDENTIFIER, 1, FALSE);
196             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_msg_age, tvb,
197                                 BPDU_MESSAGE_AGE, 1, FALSE);
198             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_hello_time, tvb,
199                                 BPDU_HELLO_TIME, 1, FALSE);
200             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_max_age, tvb,
201                                 BPDU_MAX_AGE, 1, FALSE);
202             proto_tree_add_item(bpdu_tree, hf_dec_bpdu_forward_delay, tvb,
203                                 BPDU_FORWARD_DELAY, 1, FALSE);
204
205       }
206 }
207
208 static const true_false_string yesno = {  
209         "Yes",
210         "No"
211 };
212
213 void
214 proto_register_dec_bpdu(void)
215 {
216
217   static hf_register_info hf[] = {
218     { &hf_dec_bpdu_proto_id,
219       { "Protocol Identifier",          "dec_stp.protocol",
220         FT_UINT8,       BASE_HEX,       VALS(&protocol_id_vals), 0x0,
221         "", HFILL }},
222     { &hf_dec_bpdu_type,
223       { "BPDU Type",                    "dec_stp.type",
224         FT_UINT8,       BASE_DEC,       VALS(&bpdu_type_vals),  0x0,
225         "", HFILL }},
226     { &hf_dec_bpdu_version_id,
227       { "BPDU Version",                 "dec_stp.version",
228         FT_UINT8,       BASE_DEC,       NULL,   0x0,
229         "", HFILL }},
230     { &hf_dec_bpdu_flags,
231       { "BPDU flags",                   "dec_stp.flags",
232         FT_UINT8,       BASE_HEX,       NULL,   0x0,
233         "", HFILL }},
234     { &hf_dec_bpdu_flags_short_timers,
235       { "Use short timers",             "dec_stp.flags.short_timers",
236         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_SHORT_TIMERS,
237         "", HFILL }},
238     { &hf_dec_bpdu_flags_tcack,
239       { "Topology Change Acknowledgment",  "dec_stp.flags.tcack",
240         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TCACK,
241         "", HFILL }},
242     { &hf_dec_bpdu_flags_tc,
243       { "Topology Change",              "dec_stp.flags.tc",
244         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TC,
245         "", HFILL }},
246     { &hf_dec_bpdu_root_pri,
247       { "Root Priority",                "dec_stp.root.pri",
248         FT_UINT16,      BASE_DEC,       NULL,   0x0,
249         "", HFILL }},
250     { &hf_dec_bpdu_root_mac,
251       { "Root MAC",                     "dec_stp.root.mac",
252         FT_ETHER,       BASE_NONE,      NULL,   0x0,
253         "", HFILL }},
254     { &hf_dec_bpdu_root_cost,
255       { "Root Path Cost",               "dec_stp.root.cost",
256         FT_UINT16,      BASE_DEC,       NULL,   0x0,
257         "", HFILL }},
258     { &hf_dec_bpdu_bridge_pri,
259       { "Bridge Priority",              "dec_stp.bridge.pri",
260         FT_UINT16,      BASE_DEC,       NULL,   0x0,
261         "", HFILL }},
262     { &hf_dec_bpdu_bridge_mac,
263       { "Bridge MAC",                   "dec_stp.bridge.mac",
264         FT_ETHER,       BASE_NONE,      NULL,   0x0,
265         "", HFILL }},
266     { &hf_dec_bpdu_port_id,
267       { "Port identifier",              "dec_stp.port",
268         FT_UINT8,       BASE_DEC,       NULL,   0x0,
269         "", HFILL }},
270     { &hf_dec_bpdu_msg_age,
271       { "Message Age",                  "dec_stp.msg_age",
272         FT_UINT8,       BASE_DEC,       NULL,   0x0,
273         "", HFILL }},
274     { &hf_dec_bpdu_hello_time,
275       { "Hello Time",                   "dec_stp.hello",
276         FT_UINT8,       BASE_DEC,       NULL,   0x0,
277         "", HFILL }},
278     { &hf_dec_bpdu_max_age,
279       { "Max Age",                      "dec_stp.max_age",
280         FT_UINT8,       BASE_DEC,       NULL,   0x0,
281         "", HFILL }},
282     { &hf_dec_bpdu_forward_delay,
283       { "Forward Delay",                "dec_stp.forward",
284         FT_UINT8,       BASE_DEC,       NULL,   0x0,
285         "", HFILL }},
286   };
287   static gint *ett[] = {
288     &ett_dec_bpdu,
289     &ett_dec_bpdu_flags,
290   };
291
292   proto_dec_bpdu = proto_register_protocol("DEC Spanning Tree Protocol",
293                                            "DEC_STP", "dec_stp");
294   proto_register_field_array(proto_dec_bpdu, hf, array_length(hf));
295   proto_register_subtree_array(ett, array_length(ett));
296 }
297
298 void
299 proto_reg_handoff_dec_bpdu(void)
300 {
301   dissector_handle_t dec_bpdu_handle;
302
303   dec_bpdu_handle = create_dissector_handle(dissect_dec_bpdu,
304                                             proto_dec_bpdu);
305   dissector_add("ethertype", ETHERTYPE_DEC_LB, dec_bpdu_handle);
306   dissector_add("chdlctype", ETHERTYPE_DEC_LB, dec_bpdu_handle);
307   dissector_add("ppp.protocol", PPP_DEC_LB, dec_bpdu_handle);
308 }