1 /*
2 * Copyright (c) 2000 Lennert Buytenhek
3 *
4 * This software may be distributed either under the terms of the
5 * BSD-style license that accompanies tcpdump or the GNU General
6 * Public License
7 *
8 * Format and print IEEE 802.1d spanning tree protocol packets.
9 * Contributed by Lennert Buytenhek <buytenh@gnu.org>
10 */
11
12 #ifndef lint
13 static const char rcsid[] _U_ =
14 "@(#) $Header: /tcpdump/master/tcpdump/print-stp.c,v 1.13.2.7 2007/03/18 17:12:36 hannes Exp $";
15 #endif
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <tcpdump-stdinc.h>
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "interface.h"
28 #include "addrtoname.h"
29 #include "extract.h"
30
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
35
36 struct stp_bpdu_ {
37 u_int8_t protocol_id[2];
38 u_int8_t protocol_version;
39 u_int8_t bpdu_type;
40 u_int8_t flags;
41 u_int8_t root_id[8];
42 u_int8_t root_path_cost[4];
43 u_int8_t bridge_id[8];
44 u_int8_t port_id[2];
45 u_int8_t message_age[2];
46 u_int8_t max_age[2];
47 u_int8_t hello_time[2];
48 u_int8_t forward_delay[2];
49 u_int8_t v1_length;
50 };
51
52 #define STP_PROTO_REGULAR 0x00
53 #define STP_PROTO_RAPID 0x02
54 #define STP_PROTO_MSTP 0x03
55
56 struct tok stp_proto_values[] = {
57 { STP_PROTO_REGULAR, "802.1d" },
58 { STP_PROTO_RAPID, "802.1w" },
59 { STP_PROTO_MSTP, "802.1s" },
60 { 0, NULL}
61 };
62
63 #define STP_BPDU_TYPE_CONFIG 0x00
64 #define STP_BPDU_TYPE_RSTP 0x02
65 #define STP_BPDU_TYPE_TOPO_CHANGE 0x80
66
67 struct tok stp_bpdu_flag_values[] = {
68 { 0x01, "Topology change" },
69 { 0x02, "Proposal" },
70 { 0x10, "Learn" },
71 { 0x20, "Forward" },
72 { 0x40, "Agreement" },
73 { 0x80, "Topology change ACK" },
74 { 0, NULL}
75 };
76
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" },
81 { 0, NULL}
82 };
83
84 struct tok rstp_obj_port_role_values[] = {
85 { 0x00, "Unknown" },
86 { 0x01, "Alternate" },
87 { 0x02, "Root" },
88 { 0x03, "Designated" },
89 { 0, NULL}
90 };
91
92 static char *
stp_print_bridge_id(const u_char * p)93 stp_print_bridge_id(const u_char *p)
94 {
95 static char bridge_id_str[sizeof("pppp.aa:bb:cc:dd:ee:ff")];
96
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]);
100
101 return bridge_id_str;
102 }
103
104 static void
stp_print_config_bpdu(const struct stp_bpdu_ * stp_bpdu,u_int length)105 stp_print_config_bpdu(const struct stp_bpdu_ *stp_bpdu, u_int length)
106 {
107 printf(", Flags [%s]",
108 bittok2str(stp_bpdu_flag_values, "none", stp_bpdu->flags));
109
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);
113
114 /* in non-verbose mode just print the bridge-id */
115 if (!vflag) {
116 return;
117 }
118
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);
125
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));
129
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)));
135 }
136 }
137
138 /*
139 * MSTP packet format
140 * Ref. IEEE 802.1Q 2003 Ed. Section 14
141 *
142 * MSTP BPDU
143 *
144 * 2 - bytes Protocol Id
145 * 1 - byte Protocol Ver.
146 * 1 - byte BPDU tye
147 * 1 - byte Flags
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
153 * 2 - bytes Max 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]
166 *
167 * MSTI Payload
168 *
169 * 1 - byte MSTI flag
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
175 */
176
177 #define MST_BPDU_MSTI_LENGTH 16
178 #define MST_BPDU_CONFIG_INFO_LENGTH 64
179
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_CONFIG_DIGEST_OFFSET 73
184 #define MST_BPDU_CIST_INT_PATH_COST_OFFSET 89
185 #define MST_BPDU_CIST_BRIDGE_ID_OFFSET 93
186 #define MST_BPDU_CIST_REMAIN_HOPS_OFFSET 101
187 #define MST_BPDU_MSTI_OFFSET 102
188 /* Offsets within an MSTI */
189 #define MST_BPDU_MSTI_ROOT_PRIO_OFFSET 1
190 #define MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET 9
191 #define MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET 13
192 #define MST_BPDU_MSTI_PORT_PRIO_OFFSET 14
193 #define MST_BPDU_MSTI_REMAIN_HOPS_OFFSET 15
194
195 static void
stp_print_mstp_bpdu(const struct stp_bpdu_ * stp_bpdu,u_int length)196 stp_print_mstp_bpdu(const struct stp_bpdu_ *stp_bpdu, u_int length)
197 {
198 const u_char *ptr;
199 u_int16_t v3len;
200 u_int16_t len;
201 u_int16_t msti;
202 u_int16_t offset;
203
204 ptr = (const u_char *)stp_bpdu;
205 printf(", CIST Flags [%s]",
206 bittok2str(stp_bpdu_flag_values, "none", stp_bpdu->flags));
207
208 /*
209 * in non-verbose mode just print the flags. We dont read that much
210 * of the packet (DEFAULT_SNAPLEN) to print out cist bridge-id
211 */
212 if (!vflag) {
213 return;
214 }
215
216 printf(", CIST bridge-id %s.%04x, length %u",
217 stp_print_bridge_id(ptr + MST_BPDU_CIST_BRIDGE_ID_OFFSET),
218 EXTRACT_16BITS(&stp_bpdu->port_id), length);
219
220
221 printf("\n\tmessage-age %.2fs, max-age %.2fs"
222 ", hello-time %.2fs, forwarding-delay %.2fs",
223 (float)EXTRACT_16BITS(&stp_bpdu->message_age) / STP_TIME_BASE,
224 (float)EXTRACT_16BITS(&stp_bpdu->max_age) / STP_TIME_BASE,
225 (float)EXTRACT_16BITS(&stp_bpdu->hello_time) / STP_TIME_BASE,
226 (float)EXTRACT_16BITS(&stp_bpdu->forward_delay) / STP_TIME_BASE);
227
228 printf("\n\tCIST root-id %s, ext-pathcost %u int-pathcost %u",
229 stp_print_bridge_id((const u_char *)&stp_bpdu->root_id),
230 EXTRACT_32BITS(&stp_bpdu->root_path_cost),
231 EXTRACT_32BITS(ptr + MST_BPDU_CIST_INT_PATH_COST_OFFSET));
232
233 printf(", port-role %s",
234 tok2str(rstp_obj_port_role_values, "Unknown",
235 RSTP_EXTRACT_PORT_ROLE(stp_bpdu->flags)));
236
237 printf("\n\tCIST regional-root-id %s",
238 stp_print_bridge_id((const u_char *)&stp_bpdu->bridge_id));
239
240 printf("\n\tMSTP Configuration Name %s, revision %u, digest %08x%08x%08x%08x",
241 ptr + MST_BPDU_CONFIG_NAME_OFFSET,
242 EXTRACT_16BITS(ptr + MST_BPDU_CONFIG_NAME_OFFSET + 32),
243 EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET),
244 EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 4),
245 EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 8),
246 EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 12));
247
248 printf("\n\tCIST remaining-hops %d", ptr[MST_BPDU_CIST_REMAIN_HOPS_OFFSET]);
249
250 /* Dump all MSTI's */
251 v3len = EXTRACT_16BITS(ptr + MST_BPDU_VER3_LEN_OFFSET);
252 if (v3len > MST_BPDU_CONFIG_INFO_LENGTH) {
253 len = v3len - MST_BPDU_CONFIG_INFO_LENGTH;
254 offset = MST_BPDU_MSTI_OFFSET;
255 while (len >= MST_BPDU_MSTI_LENGTH) {
256 msti = EXTRACT_16BITS(ptr + offset +
257 MST_BPDU_MSTI_ROOT_PRIO_OFFSET);
258 msti = msti & 0x0FFF;
259
260 printf("\n\tMSTI %d, Flags [%s], port-role %s",
261 msti, bittok2str(stp_bpdu_flag_values, "none", ptr[offset]),
262 tok2str(rstp_obj_port_role_values, "Unknown",
263 RSTP_EXTRACT_PORT_ROLE(ptr[offset])));
264 printf("\n\t\tMSTI regional-root-id %s, pathcost %u",
265 stp_print_bridge_id(ptr + offset +
266 MST_BPDU_MSTI_ROOT_PRIO_OFFSET),
267 EXTRACT_32BITS(ptr + offset +
268 MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET));
269 printf("\n\t\tMSTI bridge-prio %d, port-prio %d, hops %d",
270 ptr[offset + MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET] >> 4,
271 ptr[offset + MST_BPDU_MSTI_PORT_PRIO_OFFSET] >> 4,
272 ptr[offset + MST_BPDU_MSTI_REMAIN_HOPS_OFFSET]);
273
274 len -= MST_BPDU_MSTI_LENGTH;
275 offset += MST_BPDU_MSTI_LENGTH;
276 }
277 }
278 }
279
280 /*
281 * Print 802.1d / 802.1w / 802.1q (mstp) packets.
282 */
283 void
stp_print(const u_char * p,u_int length)284 stp_print(const u_char *p, u_int length)
285 {
286 const struct stp_bpdu_ *stp_bpdu;
287 u_int16_t mstp_len;
288
289 stp_bpdu = (struct stp_bpdu_*)p;
290
291 /* Minimum STP Frame size. */
292 if (length < 4)
293 goto trunc;
294
295 if (EXTRACT_16BITS(&stp_bpdu->protocol_id)) {
296 printf("unknown STP version, length %u", length);
297 return;
298 }
299
300 printf("STP %s", tok2str(stp_proto_values, "Unknown STP protocol (0x%02x)",
301 stp_bpdu->protocol_version));
302
303 switch (stp_bpdu->protocol_version) {
304 case STP_PROTO_REGULAR:
305 case STP_PROTO_RAPID:
306 case STP_PROTO_MSTP:
307 break;
308 default:
309 return;
310 }
311
312 printf(", %s", tok2str(stp_bpdu_type_values, "Unknown BPDU Type (0x%02x)",
313 stp_bpdu->bpdu_type));
314
315 switch (stp_bpdu->bpdu_type) {
316 case STP_BPDU_TYPE_CONFIG:
317 if (length < sizeof(struct stp_bpdu_) - 1) {
318 goto trunc;
319 }
320 stp_print_config_bpdu(stp_bpdu, length);
321 break;
322
323 case STP_BPDU_TYPE_RSTP:
324 if (stp_bpdu->protocol_version == STP_PROTO_RAPID) {
325 if (length < sizeof(struct stp_bpdu_)) {
326 goto trunc;
327 }
328 stp_print_config_bpdu(stp_bpdu, length);
329 } else if (stp_bpdu->protocol_version == STP_PROTO_MSTP) {
330 if (length < STP_BPDU_MSTP_MIN_LEN) {
331 goto trunc;
332 }
333 if (stp_bpdu->v1_length != 0) {
334 /* FIX ME: Emit a message here ? */
335 goto trunc;
336 }
337 /* Validate v3 length */
338 mstp_len = EXTRACT_16BITS(p + MST_BPDU_VER3_LEN_OFFSET);
339 mstp_len += 2; /* length encoding itself is 2 bytes */
340 if (length < (sizeof(struct stp_bpdu_) + mstp_len)) {
341 goto trunc;
342 }
343 stp_print_mstp_bpdu(stp_bpdu, length);
344 }
345 break;
346
347 case STP_BPDU_TYPE_TOPO_CHANGE:
348 /* always empty message - just break out */
349 break;
350
351 default:
352 break;
353 }
354
355 return;
356 trunc:
357 printf("[|stp %d]", length);
358 }
359
360 /*
361 * Local Variables:
362 * c-style: whitesmith
363 * c-basic-offset: 4
364 * End:
365 */
366