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.20 2007-03-18 17:11:46 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
55 #define STP_PROTO_SPB 0x04
57 struct tok stp_proto_values
[] = {
58 { STP_PROTO_REGULAR
, "802.1d" },
59 { STP_PROTO_RAPID
, "802.1w" },
60 { STP_PROTO_MSTP
, "802.1s" },
61 { STP_PROTO_SPB
, "802.1aq" },
65 #define STP_BPDU_TYPE_CONFIG 0x00
66 #define STP_BPDU_TYPE_RSTP 0x02
67 #define STP_BPDU_TYPE_TOPO_CHANGE 0x80
69 struct tok stp_bpdu_flag_values
[] = {
70 { 0x01, "Topology change" },
74 { 0x40, "Agreement" },
75 { 0x80, "Topology change ACK" },
79 struct tok stp_bpdu_type_values
[] = {
80 { STP_BPDU_TYPE_CONFIG
, "Config" },
81 { STP_BPDU_TYPE_RSTP
, "Rapid STP" },
82 { STP_BPDU_TYPE_TOPO_CHANGE
, "Topology Change" },
86 struct tok rstp_obj_port_role_values
[] = {
88 { 0x01, "Alternate" },
90 { 0x03, "Designated" },
95 stp_print_bridge_id(const u_char
*p
)
97 static char bridge_id_str
[sizeof("pppp.aa:bb:cc:dd:ee:ff")];
99 snprintf(bridge_id_str
, sizeof(bridge_id_str
),
100 "%.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
101 p
[0], p
[1], p
[2], p
[3], p
[4], p
[5], p
[6], p
[7]);
103 return bridge_id_str
;
107 stp_print_config_bpdu(const struct stp_bpdu_
*stp_bpdu
, u_int length
)
109 printf(", Flags [%s]",
110 bittok2str(stp_bpdu_flag_values
, "none", stp_bpdu
->flags
));
112 printf(", bridge-id %s.%04x, length %u",
113 stp_print_bridge_id((const u_char
*)&stp_bpdu
->bridge_id
),
114 EXTRACT_16BITS(&stp_bpdu
->port_id
), length
);
116 /* in non-verbose mode just print the bridge-id */
121 printf("\n\tmessage-age %.2fs, max-age %.2fs"
122 ", hello-time %.2fs, forwarding-delay %.2fs",
123 (float)EXTRACT_16BITS(&stp_bpdu
->message_age
) / STP_TIME_BASE
,
124 (float)EXTRACT_16BITS(&stp_bpdu
->max_age
) / STP_TIME_BASE
,
125 (float)EXTRACT_16BITS(&stp_bpdu
->hello_time
) / STP_TIME_BASE
,
126 (float)EXTRACT_16BITS(&stp_bpdu
->forward_delay
) / STP_TIME_BASE
);
128 printf("\n\troot-id %s, root-pathcost %u",
129 stp_print_bridge_id((const u_char
*)&stp_bpdu
->root_id
),
130 EXTRACT_32BITS(&stp_bpdu
->root_path_cost
));
132 /* Port role is only valid for 802.1w */
133 if (stp_bpdu
->protocol_version
== STP_PROTO_RAPID
) {
134 printf(", port-role %s",
135 tok2str(rstp_obj_port_role_values
, "Unknown",
136 RSTP_EXTRACT_PORT_ROLE(stp_bpdu
->flags
)));
142 * Ref. IEEE 802.1Q 2003 Ed. Section 14
146 * 2 - bytes Protocol Id
147 * 1 - byte Protocol Ver.
150 * 8 - bytes CIST Root Identifier
151 * 4 - bytes CIST External Path Cost
152 * 8 - bytes CIST Regional Root Identifier
153 * 2 - bytes CIST Port Identifier
154 * 2 - bytes Message Age
156 * 2 - bytes Hello Time
157 * 2 - bytes Forward delay
158 * 1 - byte Version 1 length. Must be 0
159 * 2 - bytes Version 3 length
160 * 1 - byte Config Identifier
161 * 32 - bytes Config Name
162 * 2 - bytes Revision level
163 * 16 - bytes Config Digest [MD5]
164 * 4 - bytes CIST Internal Root Path Cost
165 * 8 - bytes CIST Bridge Identifier
166 * 1 - byte CIST Remaining Hops
167 * 16 - bytes MSTI information [Max 64 MSTI, each 16 bytes]
173 * 8 - bytes MSTI Regional Root Identifier
174 * 4 - bytes MSTI Regional Path Cost
175 * 1 - byte MSTI Bridge Priority
176 * 1 - byte MSTI Port Priority
177 * 1 - byte MSTI Remaining Hops
180 #define MST_BPDU_MSTI_LENGTH 16
181 #define MST_BPDU_CONFIG_INFO_LENGTH 64
183 /* Offsets of fields from the begginning for the packet */
184 #define MST_BPDU_VER3_LEN_OFFSET 36
185 #define MST_BPDU_CONFIG_NAME_OFFSET 39
186 #define MST_BPDU_CONFIG_DIGEST_OFFSET 73
187 #define MST_BPDU_CIST_INT_PATH_COST_OFFSET 89
188 #define MST_BPDU_CIST_BRIDGE_ID_OFFSET 93
189 #define MST_BPDU_CIST_REMAIN_HOPS_OFFSET 101
190 #define MST_BPDU_MSTI_OFFSET 102
191 /* Offsets within an MSTI */
192 #define MST_BPDU_MSTI_ROOT_PRIO_OFFSET 1
193 #define MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET 9
194 #define MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET 13
195 #define MST_BPDU_MSTI_PORT_PRIO_OFFSET 14
196 #define MST_BPDU_MSTI_REMAIN_HOPS_OFFSET 15
198 #define SPB_BPDU_MIN_LEN 87
199 #define SPB_BPDU_CONFIG_NAME_OFFSET 3
200 #define SPB_BPDU_CONFIG_REV_OFFSET SPB_BPDU_CONFIG_NAME_OFFSET + 32
201 #define SPB_BPDU_CONFIG_DIGEST_OFFSET SPB_BPDU_CONFIG_REV_OFFSET + 2
202 #define SPB_BPDU_AGREEMENT_OFFSET SPB_BPDU_CONFIG_DIGEST_OFFSET + 16
203 #define SPB_BPDU_AGREEMENT_CON_OFFSET SPB_BPDU_AGREEMENT_OFFSET + 1
204 #define SPB_BPDU_AGREEMENT_EDGE_OFFSET SPB_BPDU_AGREEMENT_CON_OFFSET + 1
205 #define SPB_BPDU_AGREEMENT_DIGEST_OFFSET SPB_BPDU_AGREEMENT_EDGE_OFFSET + 10
209 stp_print_mstp_bpdu(const struct stp_bpdu_
*stp_bpdu
, u_int length
)
217 ptr
= (const u_char
*)stp_bpdu
;
218 printf(", CIST Flags [%s]",
219 bittok2str(stp_bpdu_flag_values
, "none", stp_bpdu
->flags
));
222 * in non-verbose mode just print the flags. We dont read that much
223 * of the packet (DEFAULT_SNAPLEN) to print out cist bridge-id
229 printf(", CIST bridge-id %s.%04x, length %u",
230 stp_print_bridge_id(ptr
+ MST_BPDU_CIST_BRIDGE_ID_OFFSET
),
231 EXTRACT_16BITS(&stp_bpdu
->port_id
), length
);
234 printf("\n\tmessage-age %.2fs, max-age %.2fs"
235 ", hello-time %.2fs, forwarding-delay %.2fs",
236 (float)EXTRACT_16BITS(&stp_bpdu
->message_age
) / STP_TIME_BASE
,
237 (float)EXTRACT_16BITS(&stp_bpdu
->max_age
) / STP_TIME_BASE
,
238 (float)EXTRACT_16BITS(&stp_bpdu
->hello_time
) / STP_TIME_BASE
,
239 (float)EXTRACT_16BITS(&stp_bpdu
->forward_delay
) / STP_TIME_BASE
);
241 printf("\n\tCIST root-id %s, ext-pathcost %u int-pathcost %u",
242 stp_print_bridge_id((const u_char
*)&stp_bpdu
->root_id
),
243 EXTRACT_32BITS(&stp_bpdu
->root_path_cost
),
244 EXTRACT_32BITS(ptr
+ MST_BPDU_CIST_INT_PATH_COST_OFFSET
));
246 printf("\n\t\tport-role %s",
247 tok2str(rstp_obj_port_role_values
, "Unknown",
248 RSTP_EXTRACT_PORT_ROLE(stp_bpdu
->flags
)));
250 printf("\n\tCIST regional-root-id %s",
251 stp_print_bridge_id((const u_char
*)&stp_bpdu
->bridge_id
));
253 printf("\n\tMSTP Configuration Name %s, revision %u, \n\t\tdigest %08x%08x%08x%08x",
254 ptr
+ MST_BPDU_CONFIG_NAME_OFFSET
,
255 EXTRACT_16BITS(ptr
+ MST_BPDU_CONFIG_NAME_OFFSET
+ 32),
256 EXTRACT_32BITS(ptr
+ MST_BPDU_CONFIG_DIGEST_OFFSET
),
257 EXTRACT_32BITS(ptr
+ MST_BPDU_CONFIG_DIGEST_OFFSET
+ 4),
258 EXTRACT_32BITS(ptr
+ MST_BPDU_CONFIG_DIGEST_OFFSET
+ 8),
259 EXTRACT_32BITS(ptr
+ MST_BPDU_CONFIG_DIGEST_OFFSET
+ 12));
261 printf("\n\tCIST remaining-hops %d", ptr
[MST_BPDU_CIST_REMAIN_HOPS_OFFSET
]);
263 /* Dump all MSTI's */
264 v3len
= EXTRACT_16BITS(ptr
+ MST_BPDU_VER3_LEN_OFFSET
);
265 if (v3len
> MST_BPDU_CONFIG_INFO_LENGTH
) {
266 len
= v3len
- MST_BPDU_CONFIG_INFO_LENGTH
;
267 offset
= MST_BPDU_MSTI_OFFSET
;
268 while (len
>= MST_BPDU_MSTI_LENGTH
) {
269 msti
= EXTRACT_16BITS(ptr
+ offset
+
270 MST_BPDU_MSTI_ROOT_PRIO_OFFSET
);
271 msti
= msti
& 0x0FFF;
273 printf("\n\tMSTI %d, Flags [%s], port-role %s",
274 msti
, bittok2str(stp_bpdu_flag_values
, "none", ptr
[offset
]),
275 tok2str(rstp_obj_port_role_values
, "Unknown",
276 RSTP_EXTRACT_PORT_ROLE(ptr
[offset
])));
277 printf("\n\t\tMSTI regional-root-id %s, pathcost %u",
278 stp_print_bridge_id(ptr
+ offset
+
279 MST_BPDU_MSTI_ROOT_PRIO_OFFSET
),
280 EXTRACT_32BITS(ptr
+ offset
+
281 MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET
));
282 printf("\n\t\tMSTI bridge-prio %d, port-prio %d, hops %d",
283 ptr
[offset
+ MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET
] >> 4,
284 ptr
[offset
+ MST_BPDU_MSTI_PORT_PRIO_OFFSET
] >> 4,
285 ptr
[offset
+ MST_BPDU_MSTI_REMAIN_HOPS_OFFSET
]);
287 len
-= MST_BPDU_MSTI_LENGTH
;
288 offset
+= MST_BPDU_MSTI_LENGTH
;
292 if ((length
-offset
) == SPB_BPDU_MIN_LEN
)
294 printf("\n\tv4len: %d AUXMCID Name %s, Rev: %u, \n\t\tdigest %08x%08x%08x%08x",
295 EXTRACT_16BITS (ptr
+ offset
),
296 ptr
+ offset
+ SPB_BPDU_CONFIG_NAME_OFFSET
,
297 EXTRACT_16BITS(ptr
+ offset
+ SPB_BPDU_CONFIG_REV_OFFSET
),
298 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_CONFIG_DIGEST_OFFSET
),
299 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_CONFIG_DIGEST_OFFSET
+ 4),
300 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_CONFIG_DIGEST_OFFSET
+ 8),
301 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_CONFIG_DIGEST_OFFSET
+ 12));
303 printf("\n\tFormat ID: %d, Cap: %d, Convention ID: %d, Cap: %d, EdgeCount:%d\n"
304 "\tDigest: %08x%08x%08x%08x%08x\n",
305 ptr
[offset
+ SPB_BPDU_AGREEMENT_OFFSET
]>>4,
306 ptr
[offset
+ SPB_BPDU_AGREEMENT_OFFSET
]&0x00ff,
307 ptr
[offset
+ SPB_BPDU_AGREEMENT_CON_OFFSET
]>>4,
308 ptr
[offset
+ SPB_BPDU_AGREEMENT_CON_OFFSET
]&0x00ff,
309 EXTRACT_16BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_EDGE_OFFSET
),
310 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_DIGEST_OFFSET
),
311 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_DIGEST_OFFSET
)+4,
312 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_DIGEST_OFFSET
)+8,
313 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_DIGEST_OFFSET
)+12,
314 EXTRACT_32BITS(ptr
+ offset
+ SPB_BPDU_AGREEMENT_DIGEST_OFFSET
)+16);
319 * Print 802.1d / 802.1w / 802.1q (mstp) / 802.1aq (spb) packets.
322 stp_print(const u_char
*p
, u_int length
)
324 const struct stp_bpdu_
*stp_bpdu
;
328 stp_bpdu
= (struct stp_bpdu_
*)p
;
330 /* Minimum STP Frame size. */
334 if (EXTRACT_16BITS(&stp_bpdu
->protocol_id
)) {
335 printf("unknown STP version, length %u", length
);
339 printf("STP %s", tok2str(stp_proto_values
, "Unknown STP protocol (0x%02x)",
340 stp_bpdu
->protocol_version
));
342 switch (stp_bpdu
->protocol_version
) {
343 case STP_PROTO_REGULAR
:
344 case STP_PROTO_RAPID
:
352 printf(", %s", tok2str(stp_bpdu_type_values
, "Unknown BPDU Type (0x%02x)",
353 stp_bpdu
->bpdu_type
));
355 switch (stp_bpdu
->bpdu_type
) {
356 case STP_BPDU_TYPE_CONFIG
:
357 if (length
< sizeof(struct stp_bpdu_
) - 1) {
360 stp_print_config_bpdu(stp_bpdu
, length
);
363 case STP_BPDU_TYPE_RSTP
:
364 if (stp_bpdu
->protocol_version
== STP_PROTO_RAPID
) {
365 if (length
< sizeof(struct stp_bpdu_
)) {
368 stp_print_config_bpdu(stp_bpdu
, length
);
369 } else if (stp_bpdu
->protocol_version
== STP_PROTO_MSTP
||
370 stp_bpdu
->protocol_version
== STP_PROTO_SPB
) {
371 if (length
< STP_BPDU_MSTP_MIN_LEN
) {
374 if (stp_bpdu
->v1_length
!= 0) {
375 /* FIX ME: Emit a message here ? */
378 /* Validate v3 length */
379 mstp_len
= EXTRACT_16BITS(p
+ MST_BPDU_VER3_LEN_OFFSET
);
380 mstp_len
+= 2; /* length encoding itself is 2 bytes */
381 if (length
< (sizeof(struct stp_bpdu_
) + mstp_len
)) {
385 if (stp_bpdu
->protocol_version
== STP_PROTO_SPB
)
387 spb_len
= EXTRACT_16BITS (p
+ MST_BPDU_VER3_LEN_OFFSET
+ mstp_len
);
389 if (length
< (sizeof(struct stp_bpdu_
) + mstp_len
+ spb_len
) ||
390 spb_len
< SPB_BPDU_MIN_LEN
) {
395 stp_print_mstp_bpdu(stp_bpdu
, length
);
399 case STP_BPDU_TYPE_TOPO_CHANGE
:
400 /* always empty message - just break out */
409 printf("[|stp %d]", length
);
414 * c-style: whitesmith