2 * Copyright (c) 2000 Lennert Buytenhek
4 * This software may be distributed either under the terms of the
5 * BSD-style license that accompanies tcpdump or the GNU General
8 * Format and print IEEE 802.1d spanning tree protocol packets.
9 * Contributed by Lennert Buytenhek <buytenh@gnu.org>
13 static const char rcsid
[] _U_
=
14 "@(#) $Header: /tcpdump/master/tcpdump/print-stp.c,v 1.13.2.5 2007-03-08 13:53:35 hannes Exp $";
21 #include <tcpdump-stdinc.h>
27 #include "interface.h"
28 #include "addrtoname.h"
31 #define RSTP_EXTRACT_PORT_ROLE(x) (((x)&0x0C)>>2)
32 /* STP timers are expressed in multiples of 1/256th second */
33 #define STP_TIME_BASE 256
34 #define STP_BPDU_MSTP_MIN_LEN 102
37 u_int8_t protocol_id
[2];
38 u_int8_t protocol_version
;
42 u_int8_t root_path_cost
[4];
43 u_int8_t bridge_id
[8];
45 u_int8_t message_age
[2];
47 u_int8_t hello_time
[2];
48 u_int8_t forward_delay
[2];
52 #define STP_PROTO_REGULAR 0x00
53 #define STP_PROTO_RAPID 0x02
54 #define STP_PROTO_MSTP 0x03
56 struct tok stp_proto_values
[] = {
57 { STP_PROTO_REGULAR
, "802.1d" },
58 { STP_PROTO_RAPID
, "802.1w" },
59 { STP_PROTO_MSTP
, "802.1s" },
63 #define STP_BPDU_TYPE_CONFIG 0x00
64 #define STP_BPDU_TYPE_RSTP 0x02
65 #define STP_BPDU_TYPE_TOPO_CHANGE 0x80
67 struct tok stp_bpdu_flag_values
[] = {
68 { 0x01, "Topology change" },
72 { 0x40, "Agreement" },
73 { 0x80, "Topology change ACK" },
77 struct tok stp_bpdu_type_values
[] = {
78 { STP_BPDU_TYPE_CONFIG
, "Config" },
79 { STP_BPDU_TYPE_RSTP
, "Rapid STP" },
80 { STP_BPDU_TYPE_TOPO_CHANGE
, "Topology Change" },
84 struct tok rstp_obj_port_role_values
[] = {
86 { 0x01, "Alternate" },
88 { 0x03, "Designated" },
93 stp_print_bridge_id(const u_char
*p
)
95 static char bridge_id_str
[sizeof("pppp.aa:bb:cc:dd:ee:ff")];
97 snprintf(bridge_id_str
, sizeof(bridge_id_str
),
98 "%.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
99 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
101 return bridge_id_str
;
105 stp_print_config_bpdu(const struct stp_bpdu_
*stp_bpdu
, u_int length
)
107 printf(", Flags [%s]",
108 bittok2str(stp_bpdu_flag_values
, "none", stp_bpdu
->flags
));
110 printf(", bridge-id %s.%04x, length %u",
111 stp_print_bridge_id((const u_char
*)&stp_bpdu
->bridge_id
),
112 EXTRACT_16BITS(&stp_bpdu
->port_id
), length
);
114 /* in non-verbose mode just print the bridge-id */
119 printf("\n\tmessage-age %.2fs, max-age %.2fs"
120 ", hello-time %.2fs, forwarding-delay %.2fs",
121 (float)EXTRACT_16BITS(&stp_bpdu
->message_age
) / STP_TIME_BASE
,
122 (float)EXTRACT_16BITS(&stp_bpdu
->max_age
) / STP_TIME_BASE
,
123 (float)EXTRACT_16BITS(&stp_bpdu
->hello_time
) / STP_TIME_BASE
,
124 (float)EXTRACT_16BITS(&stp_bpdu
->forward_delay
) / STP_TIME_BASE
);
126 printf("\n\troot-id %s, root-pathcost %u",
127 stp_print_bridge_id((const u_char
*)&stp_bpdu
->root_id
),
128 EXTRACT_32BITS(&stp_bpdu
->root_path_cost
));
130 /* Port role is only valid for 802.1w */
131 if (stp_bpdu
->protocol_version
== STP_PROTO_RAPID
) {
132 printf(", port-role %s",
133 tok2str(rstp_obj_port_role_values
, "Unknown",
134 RSTP_EXTRACT_PORT_ROLE(stp_bpdu
->flags
)));
140 * Ref. IEEE 802.1Q 2003 Ed. Section 14
144 * 2 - bytes Protocol Id
145 * 1 - byte Protocol Ver.
148 * 8 - bytes CIST Root Identifier
149 * 4 - bytes CIST External Path Cost
150 * 8 - bytes CIST Regional Root Identifier
151 * 2 - bytes CIST Port Identifier
152 * 2 - bytes Message Age
154 * 2 - bytes Hello Time
155 * 2 - bytes Forward delay
156 * 1 - byte Version 1 length. Must be 0
157 * 2 - bytes Version 3 length
158 * 1 - byte Config Identifier
159 * 32 - bytes Config Name
160 * 2 - bytes Revision level
161 * 16 - bytes Config Digest [MD5]
162 * 4 - bytes CIST Internal Root Path Cost
163 * 8 - bytes CIST Bridge Identifier
164 * 1 - byte CIST Remaining Hops
165 * 16 - bytes MSTI information [Max 64 MSTI, each 16 bytes]
170 * 8 - bytes MSTI Regional Root Identifier
171 * 4 - bytes MSTI Regional Path Cost
172 * 1 - byte MSTI Bridge Priority
173 * 1 - byte MSTI Port Priority
174 * 1 - byte MSTI Remaining Hops
177 #define MST_BPDU_MSTI_LENGTH 16
178 #define MST_BPDU_CONFIG_INFO_LENGTH 64
180 /* Offsets of fields from the begginning for the packet */
181 #define MST_BPDU_VER3_LEN_OFFSET 36
182 #define MST_BPDU_CONFIG_NAME_OFFSET 39
183 #define MST_BPDU_CIST_INT_PATH_COST_OFFSET 89
184 #define MST_BPDU_CIST_BRIDGE_ID_OFFSET 93
185 #define MST_BPDU_CIST_REMAIN_HOPS_OFFSET 101
186 #define MST_BPDU_MSTI_OFFSET 102
187 /* Offsets within an MSTI */
188 #define MST_BPDU_MSTI_ROOT_PRIO_OFFSET 1
189 #define MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET 9
190 #define MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET 13
191 #define MST_BPDU_MSTI_PORT_PRIO_OFFSET 14
192 #define MST_BPDU_MSTI_REMAIN_HOPS_OFFSET 15
195 stp_print_mstp_bpdu(const struct stp_bpdu_
*stp_bpdu
, u_int length
)
203 ptr
= (const u_char
*)stp_bpdu
;
204 printf(", CIST Flags [%s]",
205 bittok2str(stp_bpdu_flag_values
, "none", stp_bpdu
->flags
));
208 * in non-verbose mode just print the flags. We dont read that much
209 * of the packet (DEFAULT_SNAPLEN) to print out cist bridge-id
215 printf(", CIST bridge-id %s.%04x, length %u",
216 stp_print_bridge_id(ptr
+ MST_BPDU_CIST_BRIDGE_ID_OFFSET
),
217 EXTRACT_16BITS(&stp_bpdu
->port_id
), length
);
220 printf("\n\tmessage-age %.2fs, max-age %.2fs"
221 ", hello-time %.2fs, forwarding-delay %.2fs",
222 (float)EXTRACT_16BITS(&stp_bpdu
->message_age
) / STP_TIME_BASE
,
223 (float)EXTRACT_16BITS(&stp_bpdu
->max_age
) / STP_TIME_BASE
,
224 (float)EXTRACT_16BITS(&stp_bpdu
->hello_time
) / STP_TIME_BASE
,
225 (float)EXTRACT_16BITS(&stp_bpdu
->forward_delay
) / STP_TIME_BASE
);
227 printf("\n\tCIST root-id %s, ext-pathcost %u int-pathcost %u",
228 stp_print_bridge_id((const u_char
*)&stp_bpdu
->root_id
),
229 EXTRACT_32BITS(&stp_bpdu
->root_path_cost
),
230 EXTRACT_32BITS(ptr
+ MST_BPDU_CIST_INT_PATH_COST_OFFSET
));
232 printf(", port-role %s",
233 tok2str(rstp_obj_port_role_values
, "Unknown",
234 RSTP_EXTRACT_PORT_ROLE(stp_bpdu
->flags
)));
236 printf("\n\tCIST regional-root-id %s",
237 stp_print_bridge_id((const u_char
*)&stp_bpdu
->bridge_id
));
239 printf("\n\tMSTP Configuration Name %s",
240 ptr
+ MST_BPDU_CONFIG_NAME_OFFSET
);
242 printf("\n\tCIST remaining-hops %d", ptr
[MST_BPDU_CIST_REMAIN_HOPS_OFFSET
]);
244 /* Dump all MSTI's */
245 v3len
= EXTRACT_16BITS(ptr
+ MST_BPDU_VER3_LEN_OFFSET
);
246 if (v3len
> MST_BPDU_CONFIG_INFO_LENGTH
) {
247 len
= v3len
- MST_BPDU_CONFIG_INFO_LENGTH
;
248 offset
= MST_BPDU_MSTI_OFFSET
;
249 while (len
>= MST_BPDU_MSTI_LENGTH
) {
250 msti
= EXTRACT_16BITS(ptr
+ offset
+
251 MST_BPDU_MSTI_ROOT_PRIO_OFFSET
);
252 msti
= msti
& 0x0FFF;
254 printf("\n\tMSTI %d, Flags [%s], port-role %s",
255 msti
, bittok2str(stp_bpdu_flag_values
, "none", ptr
[offset
]),
256 tok2str(rstp_obj_port_role_values
, "Unknown",
257 RSTP_EXTRACT_PORT_ROLE(ptr
[offset
])));
258 printf("\n\t\tMSTI regional-root-id %s, pathcost %u",
259 stp_print_bridge_id(ptr
+ offset
+
260 MST_BPDU_MSTI_ROOT_PRIO_OFFSET
),
261 EXTRACT_32BITS(ptr
+ offset
+
262 MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET
));
263 printf("\n\t\tMSTI bridge-prio %d, port-prio %d, hops %d",
264 ptr
[offset
+ MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET
],
265 ptr
[offset
+ MST_BPDU_MSTI_PORT_PRIO_OFFSET
],
266 ptr
[offset
+ MST_BPDU_MSTI_REMAIN_HOPS_OFFSET
]);
268 len
-= MST_BPDU_MSTI_LENGTH
;
269 offset
+= MST_BPDU_MSTI_LENGTH
;
275 * Print 802.1d / 802.1w / 802.1q (mstp) packets.
278 stp_print(const u_char
*p
, u_int length
)
280 const struct stp_bpdu_
*stp_bpdu
;
283 stp_bpdu
= (struct stp_bpdu_
*)p
;
285 /* Minimum STP Frame size. */
289 if (EXTRACT_16BITS(&stp_bpdu
->protocol_id
)) {
290 printf("unknown STP version, length %u", length
);
294 printf("STP %s", tok2str(stp_proto_values
, "Unknown STP protocol (0x%02x)",
295 stp_bpdu
->protocol_version
));
297 switch (stp_bpdu
->protocol_version
) {
298 case STP_PROTO_REGULAR
:
299 case STP_PROTO_RAPID
:
306 printf(", %s", tok2str(stp_bpdu_type_values
, "Unknown BPDU Type (0x%02x)",
307 stp_bpdu
->bpdu_type
));
309 switch (stp_bpdu
->bpdu_type
) {
310 case STP_BPDU_TYPE_CONFIG
:
311 if (length
< sizeof(struct stp_bpdu_
) - 1) {
314 stp_print_config_bpdu(stp_bpdu
, length
);
317 case STP_BPDU_TYPE_RSTP
:
318 if (stp_bpdu
->protocol_version
== STP_PROTO_RAPID
) {
319 if (length
< sizeof(struct stp_bpdu_
)) {
322 stp_print_config_bpdu(stp_bpdu
, length
);
323 } else if (stp_bpdu
->protocol_version
== STP_PROTO_MSTP
) {
324 if (length
< STP_BPDU_MSTP_MIN_LEN
) {
327 if (stp_bpdu
->v1_length
!= 0) {
328 /* FIX ME: Emit a message here ? */
331 /* Validate v3 length */
332 mstp_len
= EXTRACT_16BITS(p
+ MST_BPDU_VER3_LEN_OFFSET
);
333 mstp_len
+= 2; /* length encoding itself is 2 bytes */
334 if (length
< (sizeof(struct stp_bpdu_
) + mstp_len
)) {
337 stp_print_mstp_bpdu(stp_bpdu
, length
);
341 case STP_BPDU_TYPE_TOPO_CHANGE
:
342 /* always empty message - just break out */
351 printf("[|stp %d]", length
);
356 * c-style: whitesmith