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