2 * Routines for BPDU (Spanning Tree Protocol) disassembly
4 * $Id: packet-bpdu.c,v 1.37 2002/08/02 23:35:47 jmayer Exp $
6 * Copyright 1999 Christophe Tronche <ch.tronche@computer.org>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@ethereal.com>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
34 #include <epan/packet.h>
37 #include <epan/resolv.h>
39 /* Offsets of fields within a BPDU */
41 #define BPDU_IDENTIFIER 0
42 #define BPDU_VERSION_IDENTIFIER 2
45 #define BPDU_ROOT_IDENTIFIER 5
46 #define BPDU_ROOT_PATH_COST 13
47 #define BPDU_BRIDGE_IDENTIFIER 17
48 #define BPDU_PORT_IDENTIFIER 25
49 #define BPDU_MESSAGE_AGE 27
50 #define BPDU_MAX_AGE 29
51 #define BPDU_HELLO_TIME 31
52 #define BPDU_FORWARD_DELAY 33
53 #define BPDU_VERSION_1_LENGTH 35
55 #define CONF_BPDU_SIZE 35
56 #define TC_BPDU_SIZE 4
57 #define RST_BPDU_SIZE 36
61 #define BPDU_FLAGS_TCACK 0x80
62 #define BPDU_FLAGS_AGREEMENT 0x40
63 #define BPDU_FLAGS_FORWARDING 0x20
64 #define BPDU_FLAGS_LEARNING 0x10
65 #define BPDU_FLAGS_PORT_ROLE_MASK 0x0C
66 #define BPDU_FLAGS_PORT_ROLE_SHIFT 2
67 #define BPDU_FLAGS_PROPOSAL 0x02
68 #define BPDU_FLAGS_TC 0x01
70 static int proto_bpdu = -1;
71 static int hf_bpdu_proto_id = -1;
72 static int hf_bpdu_version_id = -1;
73 static int hf_bpdu_type = -1;
74 static int hf_bpdu_flags = -1;
75 static int hf_bpdu_flags_tcack = -1;
76 static int hf_bpdu_flags_agreement = -1;
77 static int hf_bpdu_flags_forwarding = -1;
78 static int hf_bpdu_flags_learning = -1;
79 static int hf_bpdu_flags_port_role = -1;
80 static int hf_bpdu_flags_proposal = -1;
81 static int hf_bpdu_flags_tc = -1;
82 static int hf_bpdu_root_mac = -1;
83 static int hf_bpdu_root_cost = -1;
84 static int hf_bpdu_bridge_mac = -1;
85 static int hf_bpdu_port_id = -1;
86 static int hf_bpdu_msg_age = -1;
87 static int hf_bpdu_max_age = -1;
88 static int hf_bpdu_hello_time = -1;
89 static int hf_bpdu_forward_delay = -1;
90 static int hf_bpdu_version_1_length = -1;
92 static gint ett_bpdu = -1;
93 static gint ett_bpdu_flags = -1;
95 static dissector_handle_t gvrp_handle;
96 static dissector_handle_t gmrp_handle;
97 static dissector_handle_t data_handle;
99 static const value_string protocol_id_vals[] = {
100 { 0, "Spanning Tree Protocol" },
104 #define BPDU_TYPE_CONF 0x00
105 #define BPDU_TYPE_RST 0x02
106 #define BPDU_TYPE_TOPOLOGY_CHANGE 0x80
108 static const value_string bpdu_type_vals[] = {
109 { BPDU_TYPE_CONF, "Configuration" },
110 { BPDU_TYPE_RST, "Rapid Spanning Tree" },
111 { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
115 static const value_string role_vals[] = {
116 { 1, "Alternate or Backup" },
122 static const char initial_sep[] = " (";
123 static const char cont_sep[] = ", ";
125 #define APPEND_BOOLEAN_FLAG(flag, item, string) \
128 proto_item_append_text(item, string, sep); \
133 dissect_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
135 guint16 protocol_identifier;
136 guint8 protocol_version_identifier;
139 guint16 root_identifier_bridge_priority;
140 const guint8 *root_identifier_mac;
141 gchar *root_identifier_mac_str;
142 guint32 root_path_cost;
143 guint16 bridge_identifier_bridge_priority;
144 const guint8 *bridge_identifier_mac;
145 gchar *bridge_identifier_mac_str;
146 guint16 port_identifier;
150 double forward_delay;
152 proto_tree *bpdu_tree;
153 proto_item *bpdu_item;
154 proto_tree *flags_tree;
155 proto_item *flags_item;
159 /* GARP application frames require special interpretation of the
160 destination address field; otherwise, they will be mistaken as
162 Fortunately, they can be recognized by checking the first 6 octets
163 of the destination address, which are in the range from
164 01-80-C2-00-00-20 to 01-80-C2-00-00-2F.
166 Yes - we *do* need to check the destination address type;
167 on Linux cooked captures, there *is* no destination address,
169 if (pinfo->dl_dst.type == AT_ETHER &&
170 pinfo->dl_dst.data[0] == 0x01 && pinfo->dl_dst.data[1] == 0x80 &&
171 pinfo->dl_dst.data[2] == 0xC2 && pinfo->dl_dst.data[3] == 0x00 &&
172 pinfo->dl_dst.data[4] == 0x00 && ((pinfo->dl_dst.data[5] & 0x20) == 0x20)) {
174 protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
176 switch (pinfo->dl_dst.data[5]) {
180 call_dissector(gmrp_handle, tvb, pinfo, tree);
185 call_dissector(gvrp_handle, tvb, pinfo, tree);
189 pinfo->current_proto = "GARP";
191 if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
192 col_set_str(pinfo->cinfo, COL_PROTOCOL, "GARP");
193 /* Generic Attribute Registration Protocol */
196 if (check_col(pinfo->cinfo, COL_INFO)) {
197 col_add_fstr(pinfo->cinfo, COL_INFO,
198 "Unknown GARP application (0x%02X)",
199 pinfo->dl_dst.data[5]);
205 if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
206 col_set_str(pinfo->cinfo, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
208 if (check_col(pinfo->cinfo, COL_INFO)) {
209 col_clear(pinfo->cinfo, COL_INFO);
212 bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
217 flags = tvb_get_guint8(tvb, BPDU_FLAGS);
218 root_identifier_bridge_priority = tvb_get_ntohs(tvb,
219 BPDU_ROOT_IDENTIFIER);
220 root_identifier_mac = tvb_get_ptr(tvb, BPDU_ROOT_IDENTIFIER + 2, 6);
221 root_identifier_mac_str = ether_to_str(root_identifier_mac);
222 root_path_cost = tvb_get_ntohl(tvb, BPDU_ROOT_PATH_COST);
223 port_identifier = tvb_get_ntohs(tvb, BPDU_PORT_IDENTIFIER);
227 /* Squelch GCC complaints. */
229 root_identifier_bridge_priority = 0;
230 root_identifier_mac = NULL;
231 root_identifier_mac_str = NULL;
237 if (check_col(pinfo->cinfo, COL_INFO)) {
241 col_add_fstr(pinfo->cinfo, COL_INFO, "Conf. %sRoot = %d/%s Cost = %d Port = 0x%04x",
242 flags & 0x1 ? "TC + " : "",
243 root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
247 case BPDU_TYPE_TOPOLOGY_CHANGE:
248 col_add_fstr(pinfo->cinfo, COL_INFO, "Topology Change Notification");
252 col_add_fstr(pinfo->cinfo, COL_INFO, "RST. %sRoot = %d/%s Cost = %d Port = 0x%04x",
253 flags & 0x1 ? "TC + " : "",
254 root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
259 col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown BPDU type (%u)",
268 set_actual_length(tvb, CONF_BPDU_SIZE);
271 case BPDU_TYPE_TOPOLOGY_CHANGE:
272 set_actual_length(tvb, TC_BPDU_SIZE);
276 set_actual_length(tvb, RST_BPDU_SIZE);
281 bpdu_item = proto_tree_add_protocol_format(tree, proto_bpdu, tvb,
282 0, -1, "Spanning Tree Protocol");
283 bpdu_tree = proto_item_add_subtree(bpdu_item, ett_bpdu);
285 protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
286 proto_tree_add_uint(bpdu_tree, hf_bpdu_proto_id, tvb,
287 BPDU_IDENTIFIER, 2, protocol_identifier);
289 protocol_version_identifier = tvb_get_guint8(tvb, BPDU_VERSION_IDENTIFIER);
290 proto_tree_add_uint(bpdu_tree, hf_bpdu_version_id, tvb,
291 BPDU_VERSION_IDENTIFIER, 1,
292 protocol_version_identifier);
293 switch (protocol_version_identifier) {
299 proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION_IDENTIFIER, 1,
300 " (Warning: this version of Ethereal only knows about versions 0 & 2)");
303 proto_tree_add_uint(bpdu_tree, hf_bpdu_type, tvb,
307 if (bpdu_type != BPDU_TYPE_CONF && bpdu_type != BPDU_TYPE_RST) {
308 call_dissector(data_handle,
309 tvb_new_subset(tvb, BPDU_TYPE + 1, -1, -1),
314 rstp_bpdu = (bpdu_type == BPDU_TYPE_RST);
315 bridge_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_BRIDGE_IDENTIFIER);
316 bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_BRIDGE_IDENTIFIER + 2, 6);
317 bridge_identifier_mac_str = ether_to_str(bridge_identifier_mac);
319 flags_item = proto_tree_add_uint(bpdu_tree, hf_bpdu_flags, tvb,
320 BPDU_FLAGS, 1, flags);
321 flags_tree = proto_item_add_subtree(flags_item, ett_bpdu_flags);
323 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, flags_item,
324 "%sTopology Change Acknowledgment");
325 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tcack, tvb,
326 BPDU_FLAGS, 1, flags);
328 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_AGREEMENT, flags_item,
330 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_agreement, tvb,
331 BPDU_FLAGS, 1, flags);
332 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_FORWARDING, flags_item,
334 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_forwarding, tvb,
335 BPDU_FLAGS, 1, flags);
336 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_LEARNING, flags_item,
338 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_learning, tvb,
339 BPDU_FLAGS, 1, flags);
343 port_role = (flags & BPDU_FLAGS_PORT_ROLE_MASK) >> BPDU_FLAGS_PORT_ROLE_SHIFT;
344 proto_item_append_text(flags_item, "%sPort Role: %s", sep,
345 val_to_str(port_role, role_vals,
349 proto_tree_add_uint(flags_tree, hf_bpdu_flags_port_role, tvb,
350 BPDU_FLAGS, 1, flags);
351 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_PROPOSAL, flags_item,
353 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_proposal, tvb,
354 BPDU_FLAGS, 1, flags);
356 APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, flags_item,
357 "%sTopology Change");
358 proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tc, tvb,
359 BPDU_FLAGS, 1, flags);
360 if (sep != initial_sep) {
361 /* We put something in; put in the terminating ")" */
362 proto_item_append_text(flags_item, ")");
365 proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_root_mac, tvb,
366 BPDU_ROOT_IDENTIFIER + 2, 6,
367 root_identifier_mac);
368 proto_tree_add_text(bpdu_tree, tvb,
369 BPDU_ROOT_IDENTIFIER, 8,
370 "Root Identifier: %d / %s",
371 root_identifier_bridge_priority,
372 root_identifier_mac_str);
373 proto_tree_add_uint(bpdu_tree, hf_bpdu_root_cost, tvb,
374 BPDU_ROOT_PATH_COST, 4,
376 proto_tree_add_text(bpdu_tree, tvb,
377 BPDU_BRIDGE_IDENTIFIER, 8,
378 "Bridge Identifier: %d / %s",
379 bridge_identifier_bridge_priority,
380 bridge_identifier_mac_str);
381 proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_bridge_mac, tvb,
382 BPDU_BRIDGE_IDENTIFIER + 2, 6,
383 bridge_identifier_mac);
384 proto_tree_add_uint(bpdu_tree, hf_bpdu_port_id, tvb,
385 BPDU_PORT_IDENTIFIER, 2,
387 message_age = tvb_get_ntohs(tvb, BPDU_MESSAGE_AGE) / 256.0;
388 proto_tree_add_double(bpdu_tree, hf_bpdu_msg_age, tvb,
391 max_age = tvb_get_ntohs(tvb, BPDU_MAX_AGE) / 256.0;
392 proto_tree_add_double(bpdu_tree, hf_bpdu_max_age, tvb,
395 hello_time = tvb_get_ntohs(tvb, BPDU_HELLO_TIME) / 256.0;
396 proto_tree_add_double(bpdu_tree, hf_bpdu_hello_time, tvb,
399 forward_delay = tvb_get_ntohs(tvb, BPDU_FORWARD_DELAY) / 256.0;
400 proto_tree_add_double(bpdu_tree, hf_bpdu_forward_delay, tvb,
401 BPDU_FORWARD_DELAY, 2,
404 proto_tree_add_item(bpdu_tree, hf_bpdu_version_1_length, tvb,
405 BPDU_VERSION_1_LENGTH, 1, FALSE);
410 static const true_false_string yesno = {
416 proto_register_bpdu(void)
419 static hf_register_info hf[] = {
421 { "Protocol Identifier", "stp.protocol",
422 FT_UINT16, BASE_HEX, VALS(&protocol_id_vals), 0x0,
424 { &hf_bpdu_version_id,
425 { "Protocol Version Identifier", "stp.version",
426 FT_UINT8, BASE_DEC, NULL, 0x0,
429 { "BPDU Type", "stp.type",
430 FT_UINT8, BASE_HEX, VALS(&bpdu_type_vals), 0x0,
433 { "BPDU flags", "stp.flags",
434 FT_UINT8, BASE_HEX, NULL, 0x0,
436 { &hf_bpdu_flags_tcack,
437 { "Topology Change Acknowledgment", "stp.flags.tcack",
438 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_TCACK,
440 { &hf_bpdu_flags_agreement,
441 { "Agreement", "stp.flags.agreement",
442 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_AGREEMENT,
444 { &hf_bpdu_flags_forwarding,
445 { "Forwarding", "stp.flags.forwarding",
446 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_FORWARDING,
448 { &hf_bpdu_flags_learning,
449 { "Learning", "stp.flags.learning",
450 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_LEARNING,
452 { &hf_bpdu_flags_port_role,
453 { "Port Role", "stp.flags.port_role",
454 FT_UINT8, BASE_DEC, VALS(role_vals), BPDU_FLAGS_PORT_ROLE_MASK,
456 { &hf_bpdu_flags_proposal,
457 { "Proposal", "stp.flags.proposal",
458 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_PROPOSAL,
461 { "Topology Change", "stp.flags.tc",
462 FT_BOOLEAN, 8, TFS(&yesno), BPDU_FLAGS_TC,
465 { "Root Identifier", "stp.root.hw",
466 FT_ETHER, BASE_NONE, NULL, 0x0,
468 { &hf_bpdu_root_cost,
469 { "Root Path Cost", "stp.root.cost",
470 FT_UINT32, BASE_DEC, NULL, 0x0,
472 { &hf_bpdu_bridge_mac,
473 { "Bridge Identifier", "stp.bridge.hw",
474 FT_ETHER, BASE_NONE, NULL, 0x0,
477 { "Port identifier", "stp.port",
478 FT_UINT16, BASE_HEX, NULL, 0x0,
481 { "Message Age", "stp.msg_age",
482 FT_DOUBLE, BASE_NONE, NULL, 0x0,
485 { "Max Age", "stp.max_age",
486 FT_DOUBLE, BASE_NONE, NULL, 0x0,
488 { &hf_bpdu_hello_time,
489 { "Hello Time", "stp.hello",
490 FT_DOUBLE, BASE_NONE, NULL, 0x0,
492 { &hf_bpdu_forward_delay,
493 { "Forward Delay", "stp.forward",
494 FT_DOUBLE, BASE_NONE, NULL, 0x0,
496 { &hf_bpdu_version_1_length,
497 { "Version 1 Length", "stp.version_1_length",
498 FT_UINT8, BASE_DEC, NULL, 0x0,
501 static gint *ett[] = {
506 proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP", "stp");
507 proto_register_field_array(proto_bpdu, hf, array_length(hf));
508 proto_register_subtree_array(ett, array_length(ett));
510 register_dissector("bpdu", dissect_bpdu, proto_bpdu);
514 proto_reg_handoff_bpdu(void)
516 dissector_handle_t bpdu_handle;
519 * Get handle for the GVRP dissector.
521 gvrp_handle = find_dissector("gvrp");
524 * Get handle for the GMRP dissector.
526 gmrp_handle = find_dissector("gmrp");
527 data_handle = find_dissector("data");
529 bpdu_handle = find_dissector("bpdu");
530 dissector_add("llc.dsap", SAP_BPDU, bpdu_handle);
531 dissector_add("ppp.protocol", PPP_BPDU, bpdu_handle);