Make the "Preferences" dialog box use the new utilities to make the Esc
[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.9 2000/04/17 00:32:37 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 /* Offsets of fields within a BPDU */
48
49 #define BPDU_IDENTIFIER          0
50 #define BPDU_VERSION_IDENTIFIER  2
51 #define BPDU_TYPE                3
52 #define BPDU_FLAGS               4
53 #define BPDU_ROOT_IDENTIFIER     5
54 #define BPDU_ROOT_PATH_COST     13
55 #define BPDU_BRIDGE_IDENTIFIER  17
56 #define BPDU_PORT_IDENTIFIER    25
57 #define BPDU_MESSAGE_AGE        27
58 #define BPDU_MAX_AGE            29
59 #define BPDU_HELLO_TIME         31
60 #define BPDU_FORWARD_DELAY      33
61
62 static int proto_bpdu = -1;
63 static int hf_bpdu_proto_id = -1;
64 static int hf_bpdu_version_id = -1;
65 static int hf_bpdu_type = -1;
66 static int hf_bpdu_flags = -1;
67 static int hf_bpdu_root_mac = -1;
68 static int hf_bpdu_root_cost = -1;
69 static int hf_bpdu_bridge_mac = -1;
70 static int hf_bpdu_port_id = -1;
71 static int hf_bpdu_msg_age = -1;
72 static int hf_bpdu_max_age = -1;
73 static int hf_bpdu_hello_time = -1;
74 static int hf_bpdu_forward_delay = -1;
75
76 static gint ett_bpdu = -1;
77
78 void dissect_bpdu(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
79       guint16 protocol_identifier;
80       guint8  protocol_version_identifier;
81       guint8  bpdu_type;
82       guint8  flags;
83       guint16 root_identifier_bridge_priority;
84       gchar   *root_identifier_mac;
85       guint32 root_path_cost;
86       guint16 bridge_identifier_bridge_priority;
87       gchar   *bridge_identifier_mac;
88       guint16 port_identifier;
89       double message_age;
90       double max_age;
91       double hello_time;
92       double forward_delay;
93       
94       proto_tree *bpdu_tree;
95       proto_item *ti;
96       const u_char *bpdu;
97
98       bpdu = pd + offset;
99       bpdu_type = (guint8) bpdu[BPDU_TYPE];
100       flags = (guint8) bpdu[BPDU_FLAGS];
101       root_identifier_bridge_priority = pntohs(bpdu + BPDU_ROOT_IDENTIFIER);
102       root_identifier_mac = ether_to_str(bpdu + BPDU_ROOT_IDENTIFIER + 2);
103       root_path_cost = pntohl(bpdu + BPDU_ROOT_PATH_COST);
104       port_identifier = pntohs(bpdu + BPDU_PORT_IDENTIFIER);
105
106       if (check_col(fd, COL_PROTOCOL)) {
107             col_add_str(fd, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
108       }
109
110       if (check_col(fd, COL_INFO)) {
111             if (bpdu_type == 0)
112                   col_add_fstr(fd, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x", 
113                                flags & 0x1 ? "TC + " : "",
114                                root_identifier_bridge_priority, root_identifier_mac, root_path_cost,
115                                port_identifier);
116             else if (bpdu_type == 0x80)
117                   col_add_fstr(fd, COL_INFO, "Topology Change Notification");
118       }
119
120       if (tree) {
121             protocol_identifier = pntohs(bpdu + BPDU_IDENTIFIER);
122             protocol_version_identifier = (guint8) bpdu[BPDU_VERSION_IDENTIFIER];
123
124             ti = proto_tree_add_protocol_format(tree, proto_bpdu, offset, 35,
125                                 "Spanning Tree Protocol");
126             bpdu_tree = proto_item_add_subtree(ti, ett_bpdu);
127             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_proto_id,
128                                        offset + BPDU_IDENTIFIER, 2, 
129                                        protocol_identifier,
130                                        "Protocol Identifier: 0x%04x (%s)", 
131                                        protocol_identifier,
132                                        protocol_identifier == 0 ? 
133                                        "Spanning Tree" : "Unknown Protocol");
134
135             proto_tree_add_item(bpdu_tree, hf_bpdu_version_id, 
136                                 offset + BPDU_VERSION_IDENTIFIER, 1, 
137                                 protocol_version_identifier);
138             if (protocol_version_identifier != 0)
139                   proto_tree_add_text(bpdu_tree, offset + BPDU_VERSION_IDENTIFIER, 1, "   (Warning: this version of packet-bpdu only knows about version = 0)");
140             proto_tree_add_uint_format(bpdu_tree, hf_bpdu_type,
141                                        offset + BPDU_TYPE, 1, 
142                                        bpdu_type,
143                                        "BPDU Type: 0x%02x (%s)", 
144                                        bpdu_type,
145                                        bpdu_type == 0 ? "Configuration" :
146                                        bpdu_type == 0x80 ? "Topology Change Notification" : "Unknown");
147
148             if (bpdu_type != 0) {
149               dissect_data(pd, offset + BPDU_TYPE + 1, fd, tree);
150               return;
151             }
152
153             bridge_identifier_bridge_priority = pntohs(bpdu + BPDU_BRIDGE_IDENTIFIER);
154             bridge_identifier_mac = ether_to_str(bpdu + BPDU_BRIDGE_IDENTIFIER + 2);
155             message_age = pntohs(bpdu + BPDU_MESSAGE_AGE) / 256.0;
156             max_age = pntohs(bpdu + BPDU_MAX_AGE) / 256.0;
157             hello_time = pntohs(bpdu + BPDU_HELLO_TIME) / 256.0;
158             forward_delay = pntohs(bpdu + BPDU_FORWARD_DELAY) / 256.0;
159
160             proto_tree_add_item(bpdu_tree, hf_bpdu_flags, 
161                                 offset + BPDU_FLAGS, 1, flags);
162             if (flags & 0x80)
163                   proto_tree_add_text(bpdu_tree, offset + BPDU_FLAGS, 1, "   1... ....  Topology Change Acknowledgment");
164             if (flags & 0x01)
165                   proto_tree_add_text(bpdu_tree, offset + BPDU_FLAGS, 1, "   .... ...1  Topology Change");
166
167             proto_tree_add_item_hidden(bpdu_tree, hf_bpdu_root_mac,
168                                        offset + BPDU_ROOT_IDENTIFIER + 2, 6,
169                                        bpdu + BPDU_ROOT_IDENTIFIER + 2);
170             proto_tree_add_text(bpdu_tree, 
171                                 offset + BPDU_ROOT_IDENTIFIER, 8, 
172                                 "Root Identifier: %d / %s", 
173                                 root_identifier_bridge_priority, 
174                                 root_identifier_mac);
175             proto_tree_add_item(bpdu_tree, hf_bpdu_root_cost, 
176                                 offset + BPDU_ROOT_PATH_COST, 4, 
177                                 root_path_cost);
178             proto_tree_add_text(bpdu_tree, 
179                                 offset + BPDU_BRIDGE_IDENTIFIER, 8, 
180                                 "Bridge Identifier: %d / %s", 
181                                 bridge_identifier_bridge_priority, 
182                                 bridge_identifier_mac);
183             proto_tree_add_item_hidden(bpdu_tree, hf_bpdu_bridge_mac,
184                                        offset + BPDU_BRIDGE_IDENTIFIER + 2, 6,
185                                        bpdu + BPDU_BRIDGE_IDENTIFIER + 2);
186             proto_tree_add_item(bpdu_tree, hf_bpdu_port_id,
187                                 offset + BPDU_PORT_IDENTIFIER, 2, 
188                                 port_identifier);
189             proto_tree_add_item(bpdu_tree, hf_bpdu_msg_age,
190                                 offset + BPDU_MESSAGE_AGE, 2, 
191                                 message_age);
192             proto_tree_add_item(bpdu_tree, hf_bpdu_max_age,
193                                 offset + BPDU_MAX_AGE, 2, 
194                                 max_age);
195             proto_tree_add_item(bpdu_tree, hf_bpdu_hello_time,
196                                 offset + BPDU_HELLO_TIME, 2, 
197                                 hello_time);
198             proto_tree_add_item(bpdu_tree, hf_bpdu_forward_delay,
199                                 offset + BPDU_FORWARD_DELAY, 2, 
200                                 forward_delay);
201       }
202 }
203
204 void
205 proto_register_bpdu(void)
206 {
207
208   static hf_register_info hf[] = {
209     { &hf_bpdu_proto_id,
210       { "Protocol Identifier",          "stp.protocol",
211         FT_UINT16,      BASE_HEX,       NULL,   0x0,
212         "" }},
213     { &hf_bpdu_version_id,
214       { "Protocol Version Identifier",  "stp.version",
215         FT_UINT8,       BASE_DEC,       NULL,   0x0,
216         "" }},
217     { &hf_bpdu_type,
218       { "BPDU type",                    "stp.type",
219         FT_UINT8,       BASE_HEX,       NULL,   0x0,
220         "" }},
221     { &hf_bpdu_flags,
222       { "BPDU flags",                   "stp.flags",
223         FT_UINT8,       BASE_HEX,       NULL,   0x0,
224         "" }},
225     { &hf_bpdu_root_mac,
226       { "Root Identifier",              "stp.root.hw",
227         FT_ETHER,       BASE_NONE,      NULL,   0x0,
228         "" }},
229     { &hf_bpdu_root_cost,
230       { "Root Path Cost",               "stp.root.cost",
231         FT_UINT32,      BASE_DEC,       NULL,   0x0,
232         "" }},
233     { &hf_bpdu_bridge_mac,
234       { "Bridge Identifier",            "stp.bridge.hw",
235         FT_ETHER,       BASE_NONE,      NULL,   0x0,
236         ""}},
237     { &hf_bpdu_port_id,
238       { "Port identifier",              "stp.port",
239         FT_UINT16,      BASE_HEX,       NULL,   0x0,
240         ""}},
241     { &hf_bpdu_msg_age,
242       { "Message Age",                  "stp.msg_age",
243         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
244         "" }},
245     { &hf_bpdu_max_age,
246       { "Max Age",                      "stp.max_age",
247         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
248         "" }},
249     { &hf_bpdu_hello_time,
250       { "Hello Time",                   "stp.hello",
251         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
252         "" }},
253     { &hf_bpdu_forward_delay,
254       { "Forward Delay",                "stp.forward",
255         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
256         "" }},
257   };
258   static gint *ett[] = {
259     &ett_bpdu,
260   };
261
262   proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "stp");
263   proto_register_field_array(proto_bpdu, hf, array_length(hf));
264   proto_register_subtree_array(ett, array_length(ett));
265 }
266
267 void
268 proto_reg_handoff_bpdu(void)
269 {
270   dissector_add("llc.dsap", SAP_BPDU, dissect_bpdu);
271 }