Make tvb_get_ptr() return 'const guint8*', and clean up all the
[obnox/wireshark/wip.git] / packet-bpdu.c
1 /* packet-bpdu.c
2  * Routines for BPDU (Spanning Tree Protocol) disassembly
3  *
4  * $Id: packet-bpdu.c,v 1.24 2001/03/13 21:34:23 gram Exp $
5  *
6  * Copyright 1999 Christophe Tronche <ch.tronche@computer.org>
7  * 
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  * 
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  * 
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <glib.h>
43 #include "packet.h"
44 #include "llcsaps.h"
45 #include "ppptypes.h"
46 #include "resolv.h"
47
48 /* Include this for GVRP dissector */
49 #include "packet-gvrp.h"
50
51 /* Offsets of fields within a BPDU */
52
53 #define BPDU_IDENTIFIER          0
54 #define BPDU_VERSION_IDENTIFIER  2
55 #define BPDU_TYPE                3
56 #define BPDU_FLAGS               4
57 #define BPDU_ROOT_IDENTIFIER     5
58 #define BPDU_ROOT_PATH_COST     13
59 #define BPDU_BRIDGE_IDENTIFIER  17
60 #define BPDU_PORT_IDENTIFIER    25
61 #define BPDU_MESSAGE_AGE        27
62 #define BPDU_MAX_AGE            29
63 #define BPDU_HELLO_TIME         31
64 #define BPDU_FORWARD_DELAY      33
65
66 static int proto_bpdu = -1;
67 static int hf_bpdu_proto_id = -1;
68 static int hf_bpdu_version_id = -1;
69 static int hf_bpdu_type = -1;
70 static int hf_bpdu_flags = -1;
71 static int hf_bpdu_root_mac = -1;
72 static int hf_bpdu_root_cost = -1;
73 static int hf_bpdu_bridge_mac = -1;
74 static int hf_bpdu_port_id = -1;
75 static int hf_bpdu_msg_age = -1;
76 static int hf_bpdu_max_age = -1;
77 static int hf_bpdu_hello_time = -1;
78 static int hf_bpdu_forward_delay = -1;
79
80 static gint ett_bpdu = -1;
81
82 static void
83 dissect_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
84       guint16 protocol_identifier;
85       guint8  protocol_version_identifier;
86       guint8  bpdu_type;
87       guint8  flags;
88       guint16 root_identifier_bridge_priority;
89       const guint8  *root_identifier_mac;
90       gchar   *root_identifier_mac_str;
91       guint32 root_path_cost;
92       guint16 bridge_identifier_bridge_priority;
93       const guint8  *bridge_identifier_mac;
94       gchar   *bridge_identifier_mac_str;
95       guint16 port_identifier;
96       double message_age;
97       double max_age;
98       double hello_time;
99       double forward_delay;
100       
101       proto_tree *bpdu_tree;
102       proto_item *ti;
103
104       /* GARP application frames require special interpretation of the
105          destination address field; otherwise, they will be mistaken as
106          BPDU frames.  
107          Fortunately, they can be recognized by checking the first 6 octets
108          of the destination address, which are in the range from
109          01-80-C2-00-00-20 to 01-80-C2-00-00-2F.
110
111          Yes - we *do* need to check the destination address type;
112          on Linux cooked captures, there *is* no destination address,
113          so it's AT_NONE. */
114       if (pinfo->dl_dst.type == AT_ETHER &&
115           pinfo->dl_dst.data[0] == 0x01 && pinfo->dl_dst.data[1] == 0x80 &&
116           pinfo->dl_dst.data[2] == 0xC2 && pinfo->dl_dst.data[3] == 0x00 &&
117           pinfo->dl_dst.data[4] == 0x00 && ((pinfo->dl_dst.data[5] & 0x20) == 0x20)) {
118
119             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
120
121             switch (pinfo->dl_dst.data[5]) {
122
123             case 0x20:
124                   /* Future expansion for GMRP */
125                   break;
126
127             case 0x21:
128                   /* for GVRP */
129                   dissect_gvrp(tvb, pinfo, tree);
130                   return;
131             }
132
133             pinfo->current_proto = "GARP";
134
135             if (check_col(pinfo->fd, COL_PROTOCOL)) {
136                     col_set_str(pinfo->fd, COL_PROTOCOL, "GARP");
137                     /* Generic Attribute Registration Protocol */
138             }
139
140             if (check_col(pinfo->fd, COL_INFO)) {
141                     col_add_fstr(pinfo->fd, COL_INFO,
142                         "Unknown GARP application (0x%02X)",
143                         pinfo->dl_dst.data[5]);
144             }
145
146             return;
147       }
148
149       if (check_col(pinfo->fd, COL_PROTOCOL)) {
150             col_set_str(pinfo->fd, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
151       }
152       if (check_col(pinfo->fd, COL_INFO)) {
153             col_clear(pinfo->fd, COL_INFO);
154       }
155
156       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
157       if (bpdu_type == 0) {
158             flags = tvb_get_guint8(tvb, BPDU_FLAGS);
159             root_identifier_bridge_priority = tvb_get_ntohs(tvb,
160                 BPDU_ROOT_IDENTIFIER);
161             root_identifier_mac = tvb_get_ptr(tvb, BPDU_ROOT_IDENTIFIER + 2, 6);
162             root_identifier_mac_str = ether_to_str(root_identifier_mac);
163             root_path_cost = tvb_get_ntohl(tvb, BPDU_ROOT_PATH_COST);
164             port_identifier = tvb_get_ntohs(tvb, BPDU_PORT_IDENTIFIER);
165       } else {
166             /* Squelch GCC complaints. */
167             flags = 0;
168             root_identifier_bridge_priority = 0;
169             root_identifier_mac = NULL;
170             root_identifier_mac_str = NULL;
171             root_path_cost = 0;
172             port_identifier = 0;
173       }
174
175       if (check_col(pinfo->fd, COL_INFO)) {
176             if (bpdu_type == 0)
177                   col_add_fstr(pinfo->fd, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
178                                flags & 0x1 ? "TC + " : "",
179                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
180                                port_identifier);
181             else if (bpdu_type == 0x80)
182                   col_add_fstr(pinfo->fd, COL_INFO, "Topology Change Notification");
183       }
184
185       if (tree) {
186             ti = proto_tree_add_protocol_format(tree, proto_bpdu, tvb, 0, 35,
187                                 "Spanning Tree Protocol");
188             bpdu_tree = proto_item_add_subtree(ti, ett_bpdu);
189
190             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
191             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_proto_id, tvb,
192                                        BPDU_IDENTIFIER, 2, 
193                                        protocol_identifier,
194                                        "Protocol Identifier: 0x%04x (%s)", 
195                                        protocol_identifier,
196                                        protocol_identifier == 0 ? 
197                                        "Spanning Tree" : "Unknown Protocol");
198
199             protocol_version_identifier = tvb_get_guint8(tvb, BPDU_VERSION_IDENTIFIER);
200             proto_tree_add_uint(bpdu_tree, hf_bpdu_version_id, tvb, 
201                                 BPDU_VERSION_IDENTIFIER, 1, 
202                                 protocol_version_identifier);
203             if (protocol_version_identifier != 0)
204                   proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION_IDENTIFIER, 1,
205                   "   (Warning: this version of Ethereal only knows about version = 0)");
206             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_type, tvb,
207                                        BPDU_TYPE, 1, 
208                                        bpdu_type,
209                                        "BPDU Type: 0x%02x (%s)", 
210                                        bpdu_type,
211                                        bpdu_type == 0 ? "Configuration" :
212                                        bpdu_type == 0x80 ? "Topology Change Notification" : "Unknown");
213
214             if (bpdu_type != 0) {
215               dissect_data(tvb, BPDU_TYPE + 1, pinfo, tree);
216               return;
217             }
218
219             bridge_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_BRIDGE_IDENTIFIER);
220             bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_BRIDGE_IDENTIFIER + 2, 6);
221             bridge_identifier_mac_str = ether_to_str(bridge_identifier_mac);
222             message_age = tvb_get_ntohs(tvb, BPDU_MESSAGE_AGE) / 256.0;
223             max_age = tvb_get_ntohs(tvb, BPDU_MAX_AGE) / 256.0;
224             hello_time = tvb_get_ntohs(tvb, BPDU_HELLO_TIME) / 256.0;
225             forward_delay = tvb_get_ntohs(tvb, BPDU_FORWARD_DELAY) / 256.0;
226
227             proto_tree_add_uint(bpdu_tree, hf_bpdu_flags, tvb, 
228                                 BPDU_FLAGS, 1, flags);
229             if (flags & 0x80)
230                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "   1... ....  Topology Change Acknowledgment");
231             if (flags & 0x01)
232                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "   .... ...1  Topology Change");
233
234             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_root_mac, tvb,
235                                        BPDU_ROOT_IDENTIFIER + 2, 6,
236                                        root_identifier_mac);
237             proto_tree_add_text(bpdu_tree, tvb, 
238                                 BPDU_ROOT_IDENTIFIER, 8, 
239                                 "Root Identifier: %d / %s", 
240                                 root_identifier_bridge_priority, 
241                                 root_identifier_mac_str);
242             proto_tree_add_uint(bpdu_tree, hf_bpdu_root_cost, tvb, 
243                                 BPDU_ROOT_PATH_COST, 4, 
244                                 root_path_cost);
245             proto_tree_add_text(bpdu_tree, tvb, 
246                                 BPDU_BRIDGE_IDENTIFIER, 8, 
247                                 "Bridge Identifier: %d / %s", 
248                                 bridge_identifier_bridge_priority, 
249                                 bridge_identifier_mac_str);
250             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_bridge_mac, tvb,
251                                        BPDU_BRIDGE_IDENTIFIER + 2, 6,
252                                        bridge_identifier_mac);
253             proto_tree_add_uint(bpdu_tree, hf_bpdu_port_id, tvb,
254                                 BPDU_PORT_IDENTIFIER, 2, 
255                                 port_identifier);
256             proto_tree_add_double(bpdu_tree, hf_bpdu_msg_age, tvb,
257                                 BPDU_MESSAGE_AGE, 2, 
258                                 message_age);
259             proto_tree_add_double(bpdu_tree, hf_bpdu_max_age, tvb,
260                                 BPDU_MAX_AGE, 2, 
261                                 max_age);
262             proto_tree_add_double(bpdu_tree, hf_bpdu_hello_time, tvb,
263                                 BPDU_HELLO_TIME, 2, 
264                                 hello_time);
265             proto_tree_add_double(bpdu_tree, hf_bpdu_forward_delay, tvb,
266                                 BPDU_FORWARD_DELAY, 2, 
267                                 forward_delay);
268       }
269 }
270
271 void
272 proto_register_bpdu(void)
273 {
274
275   static hf_register_info hf[] = {
276     { &hf_bpdu_proto_id,
277       { "Protocol Identifier",          "stp.protocol",
278         FT_UINT16,      BASE_HEX,       NULL,   0x0,
279         "" }},
280     { &hf_bpdu_version_id,
281       { "Protocol Version Identifier",  "stp.version",
282         FT_UINT8,       BASE_DEC,       NULL,   0x0,
283         "" }},
284     { &hf_bpdu_type,
285       { "BPDU type",                    "stp.type",
286         FT_UINT8,       BASE_HEX,       NULL,   0x0,
287         "" }},
288     { &hf_bpdu_flags,
289       { "BPDU flags",                   "stp.flags",
290         FT_UINT8,       BASE_HEX,       NULL,   0x0,
291         "" }},
292     { &hf_bpdu_root_mac,
293       { "Root Identifier",              "stp.root.hw",
294         FT_ETHER,       BASE_NONE,      NULL,   0x0,
295         "" }},
296     { &hf_bpdu_root_cost,
297       { "Root Path Cost",               "stp.root.cost",
298         FT_UINT32,      BASE_DEC,       NULL,   0x0,
299         "" }},
300     { &hf_bpdu_bridge_mac,
301       { "Bridge Identifier",            "stp.bridge.hw",
302         FT_ETHER,       BASE_NONE,      NULL,   0x0,
303         ""}},
304     { &hf_bpdu_port_id,
305       { "Port identifier",              "stp.port",
306         FT_UINT16,      BASE_HEX,       NULL,   0x0,
307         ""}},
308     { &hf_bpdu_msg_age,
309       { "Message Age",                  "stp.msg_age",
310         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
311         "" }},
312     { &hf_bpdu_max_age,
313       { "Max Age",                      "stp.max_age",
314         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
315         "" }},
316     { &hf_bpdu_hello_time,
317       { "Hello Time",                   "stp.hello",
318         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
319         "" }},
320     { &hf_bpdu_forward_delay,
321       { "Forward Delay",                "stp.forward",
322         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
323         "" }},
324   };
325   static gint *ett[] = {
326     &ett_bpdu,
327   };
328
329   proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP", "stp");
330   proto_register_field_array(proto_bpdu, hf, array_length(hf));
331   proto_register_subtree_array(ett, array_length(ett));
332
333   register_dissector("bpdu", dissect_bpdu, proto_bpdu);
334 }
335
336 void
337 proto_reg_handoff_bpdu(void)
338 {
339   dissector_add("llc.dsap", SAP_BPDU, dissect_bpdu, proto_bpdu);
340   dissector_add("ppp.protocol", PPP_BPDU, dissect_bpdu, proto_bpdu);
341 }