Add packet-ansi_map.h to the distribution.
[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.47 2003/09/21 20:06:00 gerald Exp $
5  *
6  * Copyright 1999 Christophe Tronche <ch.tronche@computer.org>
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 #include <stdio.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <epan/packet.h>
35 #include "llcsaps.h"
36 #include "ppptypes.h"
37 #include "chdlctypes.h"
38 #include <epan/resolv.h>
39
40 /* Offsets of fields within a BPDU */
41
42 #define BPDU_IDENTIFIER          0
43 #define BPDU_VERSION_IDENTIFIER  2
44 #define BPDU_TYPE                3
45 #define BPDU_FLAGS               4
46 #define BPDU_ROOT_IDENTIFIER     5
47 #define BPDU_ROOT_PATH_COST     13
48 #define BPDU_BRIDGE_IDENTIFIER  17
49 #define BPDU_PORT_IDENTIFIER    25
50 #define BPDU_MESSAGE_AGE        27
51 #define BPDU_MAX_AGE            29
52 #define BPDU_HELLO_TIME         31
53 #define BPDU_FORWARD_DELAY      33
54 #define BPDU_VERSION_1_LENGTH   35
55 #define BPDU_VERSION_3_LENGTH   36
56 #define BPDU_MST_CONFIG_FORMAT_SELECTOR 38
57 #define BPDU_MST_CONFIG_NAME 39
58 #define BPDU_MST_CONFIG_REVISION_LEVEL 71
59 #define BPDU_MST_CONFIG_DIGEST 73
60 #define BPDU_CIST_INTERNAL_ROOT_PATH_COST 89
61 #define BPDU_CIST_BRIDGE_IDENTIFIER 93
62 #define BPDU_CIST_REMAINING_HOPS        101
63 #define BPDU_MSTI                       102
64 #define MSTI_FLAGS                      0
65 #define MSTI_REGIONAL_ROOT              1
66 #define MSTI_INTERNAL_ROOT_PATH_COST    9
67 #define MSTI_BRIDGE_IDENTIFIER_PRIORITY 13
68 #define MSTI_PORT_IDENTIFIER_PRIORITY   14
69 #define MSTI_REMAINING_HOPS             15
70
71 #define CONF_BPDU_SIZE          35
72 #define TC_BPDU_SIZE            4
73 #define RST_BPDU_SIZE           36
74 #define VERSION_3_STATIC_LENGTH 64
75 #define MSTI_MESSAGE_SIZE       16
76
77 /* Flag bits */
78
79 #define BPDU_FLAGS_TCACK                0x80
80 #define BPDU_FLAGS_AGREEMENT            0x40
81 #define BPDU_FLAGS_FORWARDING           0x20
82 #define BPDU_FLAGS_LEARNING             0x10
83 #define BPDU_FLAGS_PORT_ROLE_MASK       0x0C
84 #define BPDU_FLAGS_PORT_ROLE_SHIFT      2
85 #define BPDU_FLAGS_PROPOSAL             0x02
86 #define BPDU_FLAGS_TC                   0x01
87
88 static int proto_bpdu = -1;
89 static int hf_bpdu_proto_id = -1;
90 static int hf_bpdu_version_id = -1;
91 static int hf_bpdu_type = -1;
92 static int hf_bpdu_flags = -1;
93 static int hf_bpdu_flags_tcack = -1;
94 static int hf_bpdu_flags_agreement = -1;
95 static int hf_bpdu_flags_forwarding = -1;
96 static int hf_bpdu_flags_learning = -1;
97 static int hf_bpdu_flags_port_role = -1;
98 static int hf_bpdu_flags_proposal = -1;
99 static int hf_bpdu_flags_tc = -1;
100 static int hf_bpdu_root_mac = -1;
101 static int hf_bpdu_root_cost = -1;
102 static int hf_bpdu_bridge_mac = -1;
103 static int hf_bpdu_port_id = -1;
104 static int hf_bpdu_msg_age = -1;
105 static int hf_bpdu_max_age = -1;
106 static int hf_bpdu_hello_time = -1;
107 static int hf_bpdu_forward_delay = -1;
108 static int hf_bpdu_version_1_length = -1;
109 static int hf_bpdu_version_3_length = -1;
110 static int hf_bpdu_mst_config_format_selector = -1;
111 static int hf_bpdu_mst_config_name = -1;
112 static int hf_bpdu_mst_config_revision_level = -1;
113 static int hf_bpdu_mst_config_digest = -1;
114 static int hf_bpdu_cist_internal_root_path_cost = -1;
115 static int hf_bpdu_cist_bridge_identifier_mac = -1;
116 static int hf_bpdu_cist_remaining_hops = -1;
117 static int hf_bpdu_msti_flags = -1;
118 static int hf_bpdu_msti_regional_root_mac = -1;
119 static int hf_bpdu_msti_internal_root_path_cost = -1;
120 static int hf_bpdu_msti_bridge_identifier_priority = -1;
121 static int hf_bpdu_msti_port_identifier_priority = -1;
122 static int hf_bpdu_msti_remaining_hops = -1;
123
124 static gint ett_bpdu = -1;
125 static gint ett_bpdu_flags = -1;
126 static gint ett_mstp = -1;
127 static gint ett_msti = -1;
128
129 static dissector_handle_t gvrp_handle;
130 static dissector_handle_t gmrp_handle;
131 static dissector_handle_t data_handle;
132
133 static const value_string protocol_id_vals[] = {
134         { 0, "Spanning Tree Protocol" },
135         { 0, NULL }
136 };
137
138 #define BPDU_TYPE_CONF                  0x00    /* STP Configuration BPDU */
139 #define BPDU_TYPE_RST                   0x02    /* RST BPDU (or MST) */
140 #define BPDU_TYPE_TOPOLOGY_CHANGE       0x80    /* STP TCN (Topology change notify) BPDU */
141
142 static const value_string bpdu_type_vals[] = {
143         { BPDU_TYPE_CONF,            "Configuration" },
144         { BPDU_TYPE_RST,             "Rapid/Multiple Spanning Tree" },
145         { BPDU_TYPE_TOPOLOGY_CHANGE, "Topology Change Notification" },
146         { 0,                         NULL }
147 };
148
149 #define PROTO_VERSION_STP       0
150 #define PROTO_VERSION_RSTP      2
151 #define PROTO_VERSION_MSTP      3
152
153 static const value_string version_id_vals[] = {
154         { PROTO_VERSION_STP,    "Spanning Tree" },
155         { PROTO_VERSION_RSTP,   "Rapid Spanning Tree" },
156         { PROTO_VERSION_MSTP,   "Multiple Spanning Tree" },
157         { 0,                    NULL}
158 };
159 static const value_string role_vals[] = {
160         { 1, "Alternate or Backup" },
161         { 2, "Root" },
162         { 3, "Designated" },
163         { 0, NULL }
164 };
165
166 static const char initial_sep[] = " (";
167 static const char cont_sep[] = ", ";
168
169 #define APPEND_BOOLEAN_FLAG(flag, item, string) \
170         if(flag){                                                       \
171                 if(item)                                                \
172                         proto_item_append_text(item, string, sep);      \
173                 sep = cont_sep;                                         \
174         }
175
176 static void
177 dissect_bpdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
178 {
179       guint16 protocol_identifier;
180       guint8  protocol_version_identifier;
181       guint8  bpdu_type;
182       guint8  flags;
183       guint16 root_identifier_bridge_priority;
184       const guint8  *root_identifier_mac;
185       gchar   *root_identifier_mac_str;
186       guint32 root_path_cost;
187       guint16 bridge_identifier_bridge_priority;
188       const guint8  *bridge_identifier_mac;
189       gchar   *bridge_identifier_mac_str;
190       guint16 port_identifier;
191       double message_age;
192       double max_age;
193       double hello_time;
194       double forward_delay;
195       guint16 version_3_length;
196       guint32 cist_internal_root_path_cost;
197       guint8 mst_config_format_selector;
198       guint16 cist_bridge_identifier_bridge_priority;
199       const guint8  *cist_bridge_identifier_mac;
200       gchar   *cist_bridge_identifier_mac_str;
201       const guint8 *mst_config_name;
202       guint16 mst_config_revision_level;
203       guint8 cist_remaining_hops, msti_remaining_hops;
204       guint32 msti_internal_root_path_cost;
205       guint32 msti_regional_root_mstid, msti_regional_root_priority;
206       const guint8  *msti_regional_root_mac;
207       gchar   *msti_regional_root_mac_str;
208       guint8 msti_bridge_identifier_priority, msti_port_identifier_priority;
209       int       length, offset, msti;
210
211       proto_tree *bpdu_tree;
212       proto_tree *mstp_tree, *msti_tree;
213       proto_item *bpdu_item;
214       proto_item *mstp_item, *msti_item;
215       proto_tree *flags_tree;
216       proto_item *flags_item;
217       guint8    rstp_bpdu, mstp_bpdu=0;
218       const char *sep;
219
220       /* GARP application frames require special interpretation of the
221          destination address field; otherwise, they will be mistaken as
222          BPDU frames.
223          Fortunately, they can be recognized by checking the first 6 octets
224          of the destination address, which are in the range from
225          01-80-C2-00-00-20 to 01-80-C2-00-00-2F.
226
227          Yes - we *do* need to check the destination address type;
228          on Linux cooked captures, there *is* no destination address,
229          so it's AT_NONE. */
230       if (pinfo->dl_dst.type == AT_ETHER &&
231           pinfo->dl_dst.data[0] == 0x01 && pinfo->dl_dst.data[1] == 0x80 &&
232           pinfo->dl_dst.data[2] == 0xC2 && pinfo->dl_dst.data[3] == 0x00 &&
233           pinfo->dl_dst.data[4] == 0x00 && ((pinfo->dl_dst.data[5] & 0xF0) == 0x20)) {
234
235             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
236
237             switch (pinfo->dl_dst.data[5]) {
238
239             case 0x20:
240                    /* for GMRP */
241                   call_dissector(gmrp_handle, tvb, pinfo, tree);
242                   return;
243
244             case 0x21:
245                   /* for GVRP */
246                   call_dissector(gvrp_handle, tvb, pinfo, tree);
247                   return;
248             }
249
250             pinfo->current_proto = "GARP";
251
252             if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
253                     col_set_str(pinfo->cinfo, COL_PROTOCOL, "GARP");
254                     /* Generic Attribute Registration Protocol */
255             }
256
257             if (check_col(pinfo->cinfo, COL_INFO)) {
258                     col_add_fstr(pinfo->cinfo, COL_INFO,
259                         "Unknown GARP application (0x%02X)",
260                         pinfo->dl_dst.data[5]);
261             }
262
263             return;
264       }
265
266       if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
267             col_set_str(pinfo->cinfo, COL_PROTOCOL, "STP"); /* Spanning Tree Protocol */
268       }
269       if (check_col(pinfo->cinfo, COL_INFO)) {
270             col_clear(pinfo->cinfo, COL_INFO);
271       }
272
273       bpdu_type = tvb_get_guint8(tvb, BPDU_TYPE);
274
275       protocol_version_identifier = tvb_get_guint8(tvb, BPDU_VERSION_IDENTIFIER);
276
277       switch (bpdu_type) {
278
279       case BPDU_TYPE_CONF:
280       case BPDU_TYPE_RST:
281             flags = tvb_get_guint8(tvb, BPDU_FLAGS);
282             root_identifier_bridge_priority = tvb_get_ntohs(tvb,BPDU_ROOT_IDENTIFIER);
283             root_identifier_mac = tvb_get_ptr(tvb, BPDU_ROOT_IDENTIFIER + 2, 6);
284             root_identifier_mac_str = ether_to_str(root_identifier_mac);
285             root_path_cost = tvb_get_ntohl(tvb, BPDU_ROOT_PATH_COST);
286             port_identifier = tvb_get_ntohs(tvb, BPDU_PORT_IDENTIFIER);
287             break;
288
289       default:
290             /* Squelch GCC complaints. */
291             flags = 0;
292             root_identifier_bridge_priority = 0;
293             root_identifier_mac = NULL;
294             root_identifier_mac_str = NULL;
295             root_path_cost = 0;
296             port_identifier = 0;
297             break;
298       }
299
300       if (check_col(pinfo->cinfo, COL_INFO)) {
301             switch (bpdu_type) {
302
303             case BPDU_TYPE_CONF:
304                   col_add_fstr(pinfo->cinfo, COL_INFO, "Conf. %sRoot = %d/%s  Cost = %d  Port = 0x%04x",
305                                flags & 0x1 ? "TC + " : "",
306                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
307                                port_identifier);
308                   break;
309
310             case BPDU_TYPE_TOPOLOGY_CHANGE:
311                   col_add_fstr(pinfo->cinfo, COL_INFO, "Topology Change Notification");
312                   break;
313
314             case BPDU_TYPE_RST:
315                   col_add_fstr(pinfo->cinfo, COL_INFO, "%cST. %sRoot = %d/%s  Cost = %d  Port = 0x%04x",
316                                protocol_version_identifier == 3 ? 'M':'R',
317                                flags & 0x1 ? "TC + " : "",
318                                root_identifier_bridge_priority, root_identifier_mac_str, root_path_cost,
319                                port_identifier);
320                   break;
321
322             default:
323                   col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown BPDU type (%u)",
324                                bpdu_type);
325                   break;
326             }
327       }
328
329       switch (bpdu_type) {
330
331       case BPDU_TYPE_CONF:
332         set_actual_length(tvb, CONF_BPDU_SIZE);
333         break;
334
335       case BPDU_TYPE_TOPOLOGY_CHANGE:
336         set_actual_length(tvb, TC_BPDU_SIZE);
337         break;
338
339       case BPDU_TYPE_RST:
340         if (protocol_version_identifier == 3) {
341             version_3_length = tvb_get_ntohs(tvb, BPDU_VERSION_3_LENGTH);
342             set_actual_length(tvb, RST_BPDU_SIZE + 2 + version_3_length);
343         } else
344             set_actual_length(tvb, RST_BPDU_SIZE);
345         break;
346       }
347
348       if (tree) {
349             bpdu_item = proto_tree_add_protocol_format(tree, proto_bpdu, tvb,
350                                 0, -1, "Spanning Tree Protocol");
351             bpdu_tree = proto_item_add_subtree(bpdu_item, ett_bpdu);
352
353             protocol_identifier = tvb_get_ntohs(tvb, BPDU_IDENTIFIER);
354             proto_tree_add_uint(bpdu_tree, hf_bpdu_proto_id, tvb,
355                                 BPDU_IDENTIFIER, 2, protocol_identifier);
356
357             proto_tree_add_uint(bpdu_tree, hf_bpdu_version_id, tvb,
358                                 BPDU_VERSION_IDENTIFIER, 1,
359                                 protocol_version_identifier);
360             switch (protocol_version_identifier) {
361               case 0:
362                 break;
363               case 2:
364               case 3:
365                 break;
366               default:
367                   proto_tree_add_text(bpdu_tree, tvb, BPDU_VERSION_IDENTIFIER, 1,
368                   "   (Warning: this version of Ethereal only knows about versions 0, 2 & 3)");
369                 break;
370             }
371             proto_tree_add_uint(bpdu_tree, hf_bpdu_type, tvb,
372                                        BPDU_TYPE, 1,
373                                        bpdu_type);
374
375             if (bpdu_type != BPDU_TYPE_CONF && bpdu_type != BPDU_TYPE_RST) {
376               call_dissector(data_handle,
377                              tvb_new_subset(tvb, BPDU_TYPE + 1, -1, -1),
378                              pinfo, tree);
379               return;
380             }
381
382             rstp_bpdu = (bpdu_type == BPDU_TYPE_RST);
383             if (rstp_bpdu) mstp_bpdu = (protocol_version_identifier == 3);
384
385             bridge_identifier_bridge_priority = tvb_get_ntohs(tvb, BPDU_BRIDGE_IDENTIFIER);
386             bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_BRIDGE_IDENTIFIER + 2, 6);
387             bridge_identifier_mac_str = ether_to_str(bridge_identifier_mac);
388
389             flags_item = proto_tree_add_uint(bpdu_tree, hf_bpdu_flags, tvb,
390                                 BPDU_FLAGS, 1, flags);
391             flags_tree = proto_item_add_subtree(flags_item, ett_bpdu_flags);
392             sep = initial_sep;
393             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, flags_item,
394                                 "%sTopology Change Acknowledgment");
395             proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tcack, tvb,
396                                 BPDU_FLAGS, 1, flags);
397             if (rstp_bpdu) {
398               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_AGREEMENT, flags_item,
399                                   "%sAgreement");
400               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_agreement, tvb,
401                                   BPDU_FLAGS, 1, flags);
402               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_FORWARDING, flags_item,
403                                   "%sForwarding");
404               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_forwarding, tvb,
405                                   BPDU_FLAGS, 1, flags);
406               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_LEARNING, flags_item,
407                                   "%sLearning");
408               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_learning, tvb,
409                                   BPDU_FLAGS, 1, flags);
410               if (flags_item) {
411                 guint8 port_role;
412
413                 port_role = (flags & BPDU_FLAGS_PORT_ROLE_MASK) >> BPDU_FLAGS_PORT_ROLE_SHIFT;
414                 proto_item_append_text(flags_item, "%sPort Role: %s", sep,
415                                        val_to_str(port_role, role_vals,
416                                                   "Unknown (%u)"));
417               }
418               sep = cont_sep;
419               proto_tree_add_uint(flags_tree, hf_bpdu_flags_port_role, tvb,
420                                   BPDU_FLAGS, 1, flags);
421               APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_PROPOSAL, flags_item,
422                                   "%sProposal");
423               proto_tree_add_boolean(flags_tree, hf_bpdu_flags_proposal, tvb,
424                                   BPDU_FLAGS, 1, flags);
425             }
426             APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, flags_item,
427                                 "%sTopology Change");
428             proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tc, tvb,
429                                 BPDU_FLAGS, 1, flags);
430             if (sep != initial_sep) {
431               /* We put something in; put in the terminating ")" */
432               proto_item_append_text(flags_item, ")");
433             }
434
435             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_root_mac, tvb,
436                                        BPDU_ROOT_IDENTIFIER + 2, 6,
437                                        root_identifier_mac);
438             proto_tree_add_text(bpdu_tree, tvb,
439                                 BPDU_ROOT_IDENTIFIER, 8,
440                                 "Root Identifier: %d / %s",
441                                 root_identifier_bridge_priority,
442                                 root_identifier_mac_str);
443             proto_tree_add_uint(bpdu_tree, hf_bpdu_root_cost, tvb,
444                                 BPDU_ROOT_PATH_COST, 4,
445                                 root_path_cost);
446             proto_tree_add_text(bpdu_tree, tvb,
447                                 BPDU_BRIDGE_IDENTIFIER, 8,
448                                 "Bridge Identifier: %d / %s",
449                                 bridge_identifier_bridge_priority,
450                                 bridge_identifier_mac_str);
451             proto_tree_add_ether_hidden(bpdu_tree, hf_bpdu_bridge_mac, tvb,
452                                        BPDU_BRIDGE_IDENTIFIER + 2, 6,
453                                        bridge_identifier_mac);
454             proto_tree_add_uint(bpdu_tree, hf_bpdu_port_id, tvb,
455                                 BPDU_PORT_IDENTIFIER, 2,
456                                 port_identifier);
457             message_age = tvb_get_ntohs(tvb, BPDU_MESSAGE_AGE) / 256.0;
458             proto_tree_add_double(bpdu_tree, hf_bpdu_msg_age, tvb,
459                                 BPDU_MESSAGE_AGE, 2,
460                                 message_age);
461             max_age = tvb_get_ntohs(tvb, BPDU_MAX_AGE) / 256.0;
462             proto_tree_add_double(bpdu_tree, hf_bpdu_max_age, tvb,
463                                 BPDU_MAX_AGE, 2,
464                                 max_age);
465             hello_time = tvb_get_ntohs(tvb, BPDU_HELLO_TIME) / 256.0;
466             proto_tree_add_double(bpdu_tree, hf_bpdu_hello_time, tvb,
467                                 BPDU_HELLO_TIME, 2,
468                                 hello_time);
469             forward_delay = tvb_get_ntohs(tvb, BPDU_FORWARD_DELAY) / 256.0;
470             proto_tree_add_double(bpdu_tree, hf_bpdu_forward_delay, tvb,
471                                 BPDU_FORWARD_DELAY, 2,
472                                 forward_delay);
473             if (rstp_bpdu) {
474               proto_tree_add_item(bpdu_tree, hf_bpdu_version_1_length, tvb,
475                                 BPDU_VERSION_1_LENGTH, 1, FALSE);
476             }
477             if (mstp_bpdu) {
478                 version_3_length = tvb_get_ntohs(tvb, BPDU_VERSION_3_LENGTH);
479
480                 mstp_item = proto_tree_add_uint(bpdu_tree, hf_bpdu_version_3_length, tvb,
481                         BPDU_VERSION_3_LENGTH, 2, version_3_length);
482                 mstp_tree = proto_item_add_subtree(mstp_item, ett_mstp);
483
484                 mst_config_format_selector = tvb_get_guint8(tvb, BPDU_MST_CONFIG_FORMAT_SELECTOR);
485                 proto_tree_add_uint(mstp_tree, hf_bpdu_mst_config_format_selector, tvb,
486                         BPDU_MST_CONFIG_FORMAT_SELECTOR, 1, mst_config_format_selector);
487                 mst_config_name =  tvb_get_ptr (tvb, BPDU_MST_CONFIG_NAME, 32);
488                 proto_tree_add_string(mstp_tree, hf_bpdu_mst_config_name, tvb, BPDU_MST_CONFIG_NAME, 32, mst_config_name);
489
490                 mst_config_revision_level = tvb_get_ntohs(tvb, BPDU_MST_CONFIG_REVISION_LEVEL);
491                 proto_tree_add_uint(mstp_tree, hf_bpdu_mst_config_revision_level, tvb,
492                         BPDU_MST_CONFIG_REVISION_LEVEL, 2, mst_config_revision_level);
493                 proto_tree_add_bytes(mstp_tree, hf_bpdu_mst_config_digest, tvb,
494                         BPDU_MST_CONFIG_DIGEST, 16, tvb_get_ptr(tvb, BPDU_MST_CONFIG_DIGEST, 16));
495
496                 cist_internal_root_path_cost = tvb_get_ntohl(tvb, BPDU_CIST_INTERNAL_ROOT_PATH_COST);
497                 proto_tree_add_uint(mstp_tree, hf_bpdu_cist_internal_root_path_cost, tvb,
498                         BPDU_CIST_INTERNAL_ROOT_PATH_COST, 4, cist_internal_root_path_cost);
499
500                 cist_bridge_identifier_bridge_priority = tvb_get_ntohs(tvb,BPDU_CIST_BRIDGE_IDENTIFIER);
501                 cist_bridge_identifier_mac = tvb_get_ptr(tvb, BPDU_CIST_BRIDGE_IDENTIFIER + 2, 6);
502                 cist_bridge_identifier_mac_str = ether_to_str(cist_bridge_identifier_mac);
503                 proto_tree_add_text(mstp_tree, tvb, BPDU_CIST_BRIDGE_IDENTIFIER, 8,
504                                 "CIST Bridge Identifier: %d / %s",
505                                 cist_bridge_identifier_bridge_priority,
506                                 cist_bridge_identifier_mac_str);
507                 proto_tree_add_ether_hidden(mstp_tree, hf_bpdu_cist_bridge_identifier_mac, tvb,
508                                        BPDU_CIST_BRIDGE_IDENTIFIER + 2, 6,
509                                        cist_bridge_identifier_mac);
510
511                 cist_remaining_hops = tvb_get_guint8(tvb, BPDU_CIST_REMAINING_HOPS);
512                 proto_tree_add_uint(mstp_tree, hf_bpdu_cist_remaining_hops, tvb,
513                         BPDU_CIST_REMAINING_HOPS, 1, cist_remaining_hops);
514
515         /* MSTI messages */
516                 length = version_3_length - VERSION_3_STATIC_LENGTH;
517                 offset = BPDU_MSTI;
518                 msti = 1;
519                 while (length >= MSTI_MESSAGE_SIZE) {
520                     msti_regional_root_mstid = tvb_get_guint8(tvb,  offset+ MSTI_REGIONAL_ROOT);
521                     msti_regional_root_priority = (msti_regional_root_mstid &0xf0) << 8;
522                     msti_regional_root_mstid = ((msti_regional_root_mstid & 0x0f) << 8) +
523                                 tvb_get_guint8(tvb,  offset+ MSTI_REGIONAL_ROOT+1);
524                     msti_regional_root_mac = tvb_get_ptr(tvb, offset+ MSTI_REGIONAL_ROOT + 2, 6);
525                     msti_regional_root_mac_str = ether_to_str(msti_regional_root_mac);
526
527                     msti_item = proto_tree_add_text(mstp_tree, tvb, offset, 16,
528                         "MSTID %d, Regional Root Identifier %d / %s",
529                         msti_regional_root_mstid, msti_regional_root_priority,
530                         msti_regional_root_mac_str);
531                     msti_tree = proto_item_add_subtree(msti_item, ett_msti);
532
533                     /* flags */
534                     flags = tvb_get_guint8(tvb, offset+MSTI_FLAGS);
535                     flags_item = proto_tree_add_uint(msti_tree, hf_bpdu_msti_flags, tvb,
536                         offset+MSTI_FLAGS, 1, flags);
537                     flags_tree = proto_item_add_subtree(flags_item, ett_bpdu_flags);
538
539                     sep = initial_sep;
540                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TCACK, flags_item, "%sMaster");
541                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tcack, tvb,
542                         offset+MSTI_FLAGS, 1, flags);
543                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_AGREEMENT, flags_item, "%sAgreement");
544                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_agreement, tvb,
545                         offset+MSTI_FLAGS, 1, flags);
546                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_FORWARDING, flags_item, "%sForwarding");
547                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_forwarding, tvb,
548                         offset+MSTI_FLAGS, 1, flags);
549                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_LEARNING, flags_item, "%sLearning");
550                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_learning, tvb,
551                         offset+MSTI_FLAGS, 1, flags);
552                     if (flags_item) {
553                         guint8 port_role;
554                         port_role = (flags & BPDU_FLAGS_PORT_ROLE_MASK) >> BPDU_FLAGS_PORT_ROLE_SHIFT;
555                         proto_item_append_text(flags_item, "%sPort Role: %s", sep,
556                                        val_to_str(port_role, role_vals, "Unknown (%u)"));
557                     }
558                     proto_tree_add_uint(flags_tree, hf_bpdu_flags_port_role, tvb,
559                         offset+MSTI_FLAGS, 1, flags);
560                     sep = cont_sep;
561                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_PROPOSAL, flags_item, "%sProposal");
562                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_proposal, tvb,
563                         offset+MSTI_FLAGS, 1, flags);
564                     APPEND_BOOLEAN_FLAG(flags & BPDU_FLAGS_TC, flags_item, "%sTopology Change");
565                     proto_tree_add_boolean(flags_tree, hf_bpdu_flags_tc, tvb,
566                         offset+MSTI_FLAGS, 1, flags);
567                     if (sep != initial_sep) {         /* We put something in; put in the terminating ")" */
568                         proto_item_append_text(flags_item, ")");
569                     }
570
571                     /* pri, MSTID, Regional root */
572                     proto_tree_add_ether_hidden(msti_tree, hf_bpdu_msti_regional_root_mac, tvb,
573                                        offset + MSTI_REGIONAL_ROOT + 2, 6,
574                                        msti_regional_root_mac);
575                     proto_tree_add_text(msti_tree, tvb, offset + MSTI_REGIONAL_ROOT, 8,
576                                 "MSTID %d, priority %d Root Identifier %s",
577                                 msti_regional_root_mstid, msti_regional_root_priority,
578                                 msti_regional_root_mac_str);
579
580
581                     msti_internal_root_path_cost = tvb_get_ntohs(tvb, offset+MSTI_INTERNAL_ROOT_PATH_COST);
582                     proto_tree_add_uint(msti_tree, hf_bpdu_msti_internal_root_path_cost, tvb,
583                         offset+MSTI_INTERNAL_ROOT_PATH_COST, 4, msti_internal_root_path_cost);
584
585                     msti_bridge_identifier_priority = tvb_get_guint8(tvb, offset+MSTI_BRIDGE_IDENTIFIER_PRIORITY) >> 4;
586                     msti_port_identifier_priority = tvb_get_guint8(tvb, offset+MSTI_PORT_IDENTIFIER_PRIORITY) >> 4;
587
588                     proto_tree_add_uint(msti_tree, hf_bpdu_msti_bridge_identifier_priority, tvb,
589                         offset+MSTI_BRIDGE_IDENTIFIER_PRIORITY, 1, msti_bridge_identifier_priority);
590                     proto_tree_add_uint(msti_tree, hf_bpdu_msti_port_identifier_priority, tvb,
591                         offset+MSTI_PORT_IDENTIFIER_PRIORITY, 1, msti_port_identifier_priority);
592                     
593                     msti_remaining_hops = tvb_get_guint8(tvb, offset+MSTI_REMAINING_HOPS);
594                     proto_tree_add_uint(msti_tree, hf_bpdu_msti_remaining_hops, tvb,
595                         offset + MSTI_REMAINING_HOPS, 1, msti_remaining_hops);
596
597                     length -= MSTI_MESSAGE_SIZE;
598                     offset += MSTI_MESSAGE_SIZE;
599                     msti++;
600                 }
601                 
602             }
603       }
604 }
605
606 static const true_false_string yesno = {
607         "Yes",
608         "No"
609 };
610
611 void
612 proto_register_bpdu(void)
613 {
614
615   static hf_register_info hf[] = {
616     { &hf_bpdu_proto_id,
617       { "Protocol Identifier",          "stp.protocol",
618         FT_UINT16,      BASE_HEX,       VALS(&protocol_id_vals), 0x0,
619         "", HFILL }},
620     { &hf_bpdu_version_id,
621       { "Protocol Version Identifier",  "stp.version",
622         FT_UINT8,       BASE_DEC,       VALS(&version_id_vals), 0x0,
623         "", HFILL }},
624     { &hf_bpdu_type,
625       { "BPDU Type",                    "stp.type",
626         FT_UINT8,       BASE_HEX,       VALS(&bpdu_type_vals),  0x0,
627         "", HFILL }},
628     { &hf_bpdu_flags,
629       { "BPDU flags",                   "stp.flags",
630         FT_UINT8,       BASE_HEX,       NULL,   0x0,
631         "", HFILL }},
632     { &hf_bpdu_flags_tcack,
633       { "Topology Change Acknowledgment",  "stp.flags.tcack",
634         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TCACK,
635         "", HFILL }},
636     { &hf_bpdu_flags_agreement,
637       { "Agreement",                    "stp.flags.agreement",
638         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_AGREEMENT,
639         "", HFILL }},
640     { &hf_bpdu_flags_forwarding,
641       { "Forwarding",                   "stp.flags.forwarding",
642         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_FORWARDING,
643         "", HFILL }},
644     { &hf_bpdu_flags_learning,
645       { "Learning",                     "stp.flags.learning",
646         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_LEARNING,
647         "", HFILL }},
648     { &hf_bpdu_flags_port_role,
649       { "Port Role",                    "stp.flags.port_role",
650         FT_UINT8,       BASE_DEC,       VALS(role_vals),        BPDU_FLAGS_PORT_ROLE_MASK,
651         "", HFILL }},
652     { &hf_bpdu_flags_proposal,
653       { "Proposal",                     "stp.flags.proposal",
654         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_PROPOSAL,
655         "", HFILL }},
656     { &hf_bpdu_flags_tc,
657       { "Topology Change",              "stp.flags.tc",
658         FT_BOOLEAN,     8,              TFS(&yesno),    BPDU_FLAGS_TC,
659         "", HFILL }},
660     { &hf_bpdu_root_mac,
661       { "Root Identifier",              "stp.root.hw",
662         FT_ETHER,       BASE_NONE,      NULL,   0x0,
663         "", HFILL }},
664     { &hf_bpdu_root_cost,
665       { "Root Path Cost",               "stp.root.cost",
666         FT_UINT32,      BASE_DEC,       NULL,   0x0,
667         "", HFILL }},
668     { &hf_bpdu_bridge_mac,
669       { "Bridge Identifier",            "stp.bridge.hw",
670         FT_ETHER,       BASE_NONE,      NULL,   0x0,
671         "", HFILL }},
672     { &hf_bpdu_port_id,
673       { "Port identifier",              "stp.port",
674         FT_UINT16,      BASE_HEX,       NULL,   0x0,
675         "", HFILL }},
676     { &hf_bpdu_msg_age,
677       { "Message Age",                  "stp.msg_age",
678         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
679         "", HFILL }},
680     { &hf_bpdu_max_age,
681       { "Max Age",                      "stp.max_age",
682         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
683         "", HFILL }},
684     { &hf_bpdu_hello_time,
685       { "Hello Time",                   "stp.hello",
686         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
687         "", HFILL }},
688     { &hf_bpdu_forward_delay,
689       { "Forward Delay",                "stp.forward",
690         FT_DOUBLE,      BASE_NONE,      NULL,   0x0,
691         "", HFILL }},
692     { &hf_bpdu_version_1_length,
693       { "Version 1 Length",             "stp.version_1_length",
694         FT_UINT8,       BASE_DEC,       NULL,   0x0,
695         "", HFILL }},
696     { &hf_bpdu_version_3_length,
697       { "MST Extension, Length",                "mstp.version_3_length",
698         FT_UINT16,      BASE_DEC,       NULL,   0x0,
699         "", HFILL }},
700     { &hf_bpdu_mst_config_format_selector,
701       { "MST Config ID format selector",                "mstp.config_format_selector",
702         FT_UINT8,       BASE_DEC,       NULL,   0x0,
703         "", HFILL }},
704     { &hf_bpdu_mst_config_name,
705       { "MST Config name",              "mstp.config_name",
706         FT_STRING,      BASE_DEC,       NULL,   0x0,
707         "", HFILL }},
708     { &hf_bpdu_mst_config_revision_level,
709       { "MST Config revision",          "mstp.config_revision_level",
710         FT_UINT16,      BASE_DEC,       NULL,   0x0,
711         "", HFILL }},
712     { &hf_bpdu_mst_config_digest,
713       { "MST Config digest",            "mstp.config_digest",
714         FT_BYTES,       BASE_DEC,       NULL,   0x0,
715         "", HFILL }},
716     { &hf_bpdu_cist_internal_root_path_cost,
717       { "CIST Internal Root Path Cost",         "mstp.cist_internal_root_path_cost",
718         FT_UINT32,      BASE_DEC,       NULL,   0x0,
719         "", HFILL }},
720     { &hf_bpdu_cist_bridge_identifier_mac,
721       { "CIST Bridge Identifier",               "mstp.cist_bridge.hw",
722         FT_ETHER,       BASE_DEC,       NULL,   0x0,
723         "", HFILL }},
724     { &hf_bpdu_cist_remaining_hops,
725       { "CIST Remaining hops",          "mstp.cist_remaining_hops",
726         FT_UINT8,       BASE_DEC,       NULL,   0x0,
727         "", HFILL }},
728     { &hf_bpdu_msti_flags,
729       { "MSTI flags",                   "mstp.msti.flags",
730         FT_UINT8,       BASE_HEX,       NULL,   0x0,
731         "", HFILL }},
732     { &hf_bpdu_msti_regional_root_mac,
733       { "Regional Root",                "mstp.msti.root.hw",
734         FT_ETHER,       BASE_DEC,       NULL,   0x0,
735         "", HFILL }},
736     { &hf_bpdu_msti_internal_root_path_cost,
737       { "Internal root path cost",              "mstp.msti.root_cost",
738         FT_UINT32,      BASE_DEC,       NULL,   0x0,
739         "", HFILL }},
740     { &hf_bpdu_msti_bridge_identifier_priority,
741       { "Bridge Identifier Priority",           "mstp.msti.bridge_priority",
742         FT_UINT8,       BASE_DEC,       NULL,   0x0,
743         "", HFILL }},
744     { &hf_bpdu_msti_port_identifier_priority,
745       { "Port identifier prority",              "mstp.msti.port_priority",
746         FT_UINT8,       BASE_DEC,       NULL,   0x0,
747         "", HFILL }},
748     { &hf_bpdu_msti_remaining_hops,
749       { "Remaining hops",               "mstp.msti.remaining_hops",
750         FT_UINT8,       BASE_DEC,       NULL,   0x0,
751         "", HFILL }},
752
753   };
754   static gint *ett[] = {
755     &ett_bpdu,
756     &ett_bpdu_flags,
757     &ett_mstp,
758     &ett_msti
759   };
760
761   proto_bpdu = proto_register_protocol("Spanning Tree Protocol", "STP", "stp");
762   proto_register_field_array(proto_bpdu, hf, array_length(hf));
763   proto_register_subtree_array(ett, array_length(ett));
764
765   register_dissector("bpdu", dissect_bpdu, proto_bpdu);
766 }
767
768 void
769 proto_reg_handoff_bpdu(void)
770 {
771   dissector_handle_t bpdu_handle;
772
773   /*
774    * Get handle for the GVRP dissector.
775    */
776   gvrp_handle = find_dissector("gvrp");
777
778   /*
779    * Get handle for the GMRP dissector.
780    */
781   gmrp_handle = find_dissector("gmrp");
782   data_handle = find_dissector("data");
783
784   bpdu_handle = find_dissector("bpdu");
785   dissector_add("llc.dsap", SAP_BPDU, bpdu_handle);
786   dissector_add("ppp.protocol", PPP_BPDU, bpdu_handle);
787   dissector_add("chdlctype", CHDLCTYPE_BPDU, bpdu_handle);
788 }