Add a new "prefs_register_protocol()" routine, which is like
[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.18 2001/01/03 06:55:27 guy 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 "resolv.h"
46
47 /* Include this for GVRP dissector */
48 #include "packet-gvrp.h"
49
50 /* Offsets of fields within a BPDU */
51
52 #define BPDU_IDENTIFIER          0
53 #define BPDU_VERSION_IDENTIFIER  2
54 #define BPDU_TYPE                3
55 #define BPDU_FLAGS               4
56 #define BPDU_ROOT_IDENTIFIER     5
57 #define BPDU_ROOT_PATH_COST     13
58 #define BPDU_BRIDGE_IDENTIFIER  17
59 #define BPDU_PORT_IDENTIFIER    25
60 #define BPDU_MESSAGE_AGE        27
61 #define BPDU_MAX_AGE            29
62 #define BPDU_HELLO_TIME         31
63 #define BPDU_FORWARD_DELAY      33
64
65 static int proto_bpdu = -1;
66 static int hf_bpdu_proto_id = -1;
67 static int hf_bpdu_version_id = -1;
68 static int hf_bpdu_type = -1;
69 static int hf_bpdu_flags = -1;
70 static int hf_bpdu_root_mac = -1;
71 static int hf_bpdu_root_cost = -1;
72 static int hf_bpdu_bridge_mac = -1;
73 static int hf_bpdu_port_id = -1;
74 static int hf_bpdu_msg_age = -1;
75 static int hf_bpdu_max_age = -1;
76 static int hf_bpdu_hello_time = -1;
77 static int hf_bpdu_forward_delay = -1;
78
79 static gint ett_bpdu = -1;
80
81 static void
82 dissect_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree) {
83       guint16 protocol_identifier;
84       guint8  protocol_version_identifier;
85       guint8  bpdu_type;
86       guint8  flags;
87       guint16 root_identifier_bridge_priority;
88       guint8  *root_identifier_mac;
89       gchar   *root_identifier_mac_str;
90       guint32 root_path_cost;
91       guint16 bridge_identifier_bridge_priority;
92       guint8  *bridge_identifier_mac;
93       gchar   *bridge_identifier_mac_str;
94       guint16 port_identifier;
95       double message_age;
96       double max_age;
97       double hello_time;
98       double forward_delay;
99       
100       proto_tree *bpdu_tree;
101       proto_item *ti;
102
103       CHECK_DISPLAY_AS_DATA(proto_bpdu, tvb, pinfo, tree);
104
105       /* GARP application frames require special interpretation of the
106          destination address field; otherwise, they will be mistaken as
107          BPDU frames.  
108          Fortunately, they can be recognized by checking the first 6 octets
109          of the destination address, which are in the range from
110          01-80-C2-00-00-20 to 01-80-C2-00-00-2F.
111
112          Yes - we *do* need to check the destination address type;
113          on Linux cooked captures, there *is* no destination address,
114          so it's AT_NONE. */
115       if (pinfo->dl_dst.type == AT_ETHER &&
116           pinfo->dl_dst.data[0] == 0x01 && pinfo->dl_dst.data[1] == 0x80 &&
117           pinfo->dl_dst.data[2] == 0xC2 && pinfo->dl_dst.data[3] == 0x00 &&
118           pinfo->dl_dst.data[4] == 0x00 && ((pinfo->dl_dst.data[5] & 0x20) == 0x20)) {
119
120             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
121
122             switch (pinfo->dl_dst.data[5]) {
123
124             case 0x20:
125                   /* Future expansion for GMRP */
126                   break;
127
128             case 0x21:
129                   /* for GVRP */
130                   dissect_gvrp(tvb, pinfo, tree);
131                   return;
132             }
133
134             pinfo->current_proto = "GARP";
135
136             if (check_col(pinfo->fd, COL_PROTOCOL)) {
137                     col_set_str(pinfo->fd, COL_PROTOCOL, "GARP");
138                     /* Generic Attribute Registration Protocol */
139             }
140
141             if (check_col(pinfo->fd, COL_INFO)) {
142                     col_add_fstr(pinfo->fd, COL_INFO,
143                         "Unknown GARP application (0x%02X)",
144                         pinfo->dl_dst.data[5]);
145             }
146
147             return;
148       }
149
150       pinfo->current_proto = "STP";
151
152       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
153       flags = tvb_get_guint8(tvb, BPDU_FLAGS);
154       root_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_ROOT_IDENTIFIER);
155       root_identifier_mac = tvb_get_ptr(tvb, BPDU_ROOT_IDENTIFIER + 2, 6);
156       root_identifier_mac_str = ether_to_str(root_identifier_mac);
157       root_path_cost = tvb_get_ntohl(tvb, BPDU_ROOT_PATH_COST);
158       port_identifier = tvb_get_ntohs(tvb, BPDU_PORT_IDENTIFIER);
159
160       if (check_col(pinfo->fd, COL_PROTOCOL)) {
161             col_set_str(pinfo->fd, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
162       }
163
164       if (check_col(pinfo->fd, COL_INFO)) {
165             if (bpdu_type == 0)
166                   col_add_fstr(pinfo->fd, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
167                                flags & 0x1 ? "TC + " : "",
168                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
169                                port_identifier);
170             else if (bpdu_type == 0x80)
171                   col_add_fstr(pinfo->fd, COL_INFO, "Topology Change Notification");
172       }
173
174       if (tree) {
175             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
176             protocol_version_identifier = tvb_get_guint8(tvb, BPDU_VERSION_IDENTIFIER);
177
178             ti = proto_tree_add_protocol_format(tree, proto_bpdu, tvb, 0, 35,
179                                 "Spanning Tree Protocol");
180             bpdu_tree = proto_item_add_subtree(ti, ett_bpdu);
181             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_proto_id, tvb,
182                                        BPDU_IDENTIFIER, 2, 
183                                        protocol_identifier,
184                                        "Protocol Identifier: 0x%04x (%s)", 
185                                        protocol_identifier,
186                                        protocol_identifier == 0 ? 
187                                        "Spanning Tree" : "Unknown Protocol");
188
189             proto_tree_add_uint(bpdu_tree, hf_bpdu_version_id, tvb, 
190                                 BPDU_VERSION_IDENTIFIER, 1, 
191                                 protocol_version_identifier);
192             if (protocol_version_identifier != 0)
193                   proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION_IDENTIFIER, 1,
194                   "   (Warning: this version of Ethereal only knows about version = 0)");
195             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_type, tvb,
196                                        BPDU_TYPE, 1, 
197                                        bpdu_type,
198                                        "BPDU Type: 0x%02x (%s)", 
199                                        bpdu_type,
200                                        bpdu_type == 0 ? "Configuration" :
201                                        bpdu_type == 0x80 ? "Topology Change Notification" : "Unknown");
202
203             if (bpdu_type != 0) {
204               dissect_data(tvb, BPDU_TYPE + 1, pinfo, tree);
205               return;
206             }
207
208             bridge_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_BRIDGE_IDENTIFIER);
209             bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_BRIDGE_IDENTIFIER + 2, 6);
210             bridge_identifier_mac_str = ether_to_str(bridge_identifier_mac);
211             message_age = tvb_get_ntohs(tvb, BPDU_MESSAGE_AGE) / 256.0;
212             max_age = tvb_get_ntohs(tvb, BPDU_MAX_AGE) / 256.0;
213             hello_time = tvb_get_ntohs(tvb, BPDU_HELLO_TIME) / 256.0;
214             forward_delay = tvb_get_ntohs(tvb, BPDU_FORWARD_DELAY) / 256.0;
215
216             proto_tree_add_uint(bpdu_tree, hf_bpdu_flags, tvb, 
217                                 BPDU_FLAGS, 1, flags);
218             if (flags & 0x80)
219                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "   1... ....  Topology Change Acknowledgment");
220             if (flags & 0x01)
221                   proto_tree_add_text(bpdu_tree, tvb, BPDU_FLAGS, 1, "   .... ...1  Topology Change");
222
223             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_root_mac, tvb,
224                                        BPDU_ROOT_IDENTIFIER + 2, 6,
225                                        root_identifier_mac);
226             proto_tree_add_text(bpdu_tree, tvb, 
227                                 BPDU_ROOT_IDENTIFIER, 8, 
228                                 "Root Identifier: %d / %s", 
229                                 root_identifier_bridge_priority, 
230                                 root_identifier_mac_str);
231             proto_tree_add_uint(bpdu_tree, hf_bpdu_root_cost, tvb, 
232                                 BPDU_ROOT_PATH_COST, 4, 
233                                 root_path_cost);
234             proto_tree_add_text(bpdu_tree, tvb, 
235                                 BPDU_BRIDGE_IDENTIFIER, 8, 
236                                 "Bridge Identifier: %d / %s", 
237                                 bridge_identifier_bridge_priority, 
238                                 bridge_identifier_mac_str);
239             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_bridge_mac, tvb,
240                                        BPDU_BRIDGE_IDENTIFIER + 2, 6,
241                                        bridge_identifier_mac);
242             proto_tree_add_uint(bpdu_tree, hf_bpdu_port_id, tvb,
243                                 BPDU_PORT_IDENTIFIER, 2, 
244                                 port_identifier);
245             proto_tree_add_double(bpdu_tree, hf_bpdu_msg_age, tvb,
246                                 BPDU_MESSAGE_AGE, 2, 
247                                 message_age);
248             proto_tree_add_double(bpdu_tree, hf_bpdu_max_age, tvb,
249                                 BPDU_MAX_AGE, 2, 
250                                 max_age);
251             proto_tree_add_double(bpdu_tree, hf_bpdu_hello_time, tvb,
252                                 BPDU_HELLO_TIME, 2, 
253                                 hello_time);
254             proto_tree_add_double(bpdu_tree, hf_bpdu_forward_delay, tvb,
255                                 BPDU_FORWARD_DELAY, 2, 
256                                 forward_delay);
257       }
258 }
259
260 void
261 proto_register_bpdu(void)
262 {
263
264   static hf_register_info hf[] = {
265     { &hf_bpdu_proto_id,
266       { "Protocol Identifier",          "stp.protocol",
267         FT_UINT16,      BASE_HEX,       NULL,   0x0,
268         "" }},
269     { &hf_bpdu_version_id,
270       { "Protocol Version Identifier",  "stp.version",
271         FT_UINT8,       BASE_DEC,       NULL,   0x0,
272         "" }},
273     { &hf_bpdu_type,
274       { "BPDU type",                    "stp.type",
275         FT_UINT8,       BASE_HEX,       NULL,   0x0,
276         "" }},
277     { &hf_bpdu_flags,
278       { "BPDU flags",                   "stp.flags",
279         FT_UINT8,       BASE_HEX,       NULL,   0x0,
280         "" }},
281     { &hf_bpdu_root_mac,
282       { "Root Identifier",              "stp.root.hw",
283         FT_ETHER,       BASE_NONE,      NULL,   0x0,
284         "" }},
285     { &hf_bpdu_root_cost,
286       { "Root Path Cost",               "stp.root.cost",
287         FT_UINT32,      BASE_DEC,       NULL,   0x0,
288         "" }},
289     { &hf_bpdu_bridge_mac,
290       { "Bridge Identifier",            "stp.bridge.hw",
291         FT_ETHER,       BASE_NONE,      NULL,   0x0,
292         ""}},
293     { &hf_bpdu_port_id,
294       { "Port identifier",              "stp.port",
295         FT_UINT16,      BASE_HEX,       NULL,   0x0,
296         ""}},
297     { &hf_bpdu_msg_age,
298       { "Message Age",                  "stp.msg_age",
299         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
300         "" }},
301     { &hf_bpdu_max_age,
302       { "Max Age",                      "stp.max_age",
303         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
304         "" }},
305     { &hf_bpdu_hello_time,
306       { "Hello Time",                   "stp.hello",
307         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
308         "" }},
309     { &hf_bpdu_forward_delay,
310       { "Forward Delay",                "stp.forward",
311         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
312         "" }},
313   };
314   static gint *ett[] = {
315     &ett_bpdu,
316   };
317
318   proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP", "stp");
319   proto_register_field_array(proto_bpdu, hf, array_length(hf));
320   proto_register_subtree_array(ett, array_length(ett));
321
322   register_dissector("bpdu", dissect_bpdu);
323 }
324
325 void
326 proto_reg_handoff_bpdu(void)
327 {
328   dissector_add("llc.dsap", SAP_BPDU, dissect_bpdu);
329 }