]> The Tcpdump Group git mirrors - tcpdump/blob - print-stp.c
From Swaminathan Chandrasekaran <[email protected]>:
[tcpdump] / print-stp.c
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.5 2007-03-08 13:53:35 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 *
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
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_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
193
194 static void
195 stp_print_mstp_bpdu(const struct stp_bpdu_ *stp_bpdu, u_int length)
196 {
197 const u_char *ptr;
198 u_int16_t v3len;
199 u_int16_t len;
200 u_int16_t msti;
201 u_int16_t offset;
202
203 ptr = (const u_char *)stp_bpdu;
204 printf(", CIST Flags [%s]",
205 bittok2str(stp_bpdu_flag_values, "none", stp_bpdu->flags));
206
207 /*
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
210 */
211 if (!vflag) {
212 return;
213 }
214
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);
218
219
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);
226
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));
231
232 printf(", port-role %s",
233 tok2str(rstp_obj_port_role_values, "Unknown",
234 RSTP_EXTRACT_PORT_ROLE(stp_bpdu->flags)));
235
236 printf("\n\tCIST regional-root-id %s",
237 stp_print_bridge_id((const u_char *)&stp_bpdu->bridge_id));
238
239 printf("\n\tMSTP Configuration Name %s",
240 ptr + MST_BPDU_CONFIG_NAME_OFFSET);
241
242 printf("\n\tCIST remaining-hops %d", ptr[MST_BPDU_CIST_REMAIN_HOPS_OFFSET]);
243
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;
253
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]);
267
268 len -= MST_BPDU_MSTI_LENGTH;
269 offset += MST_BPDU_MSTI_LENGTH;
270 }
271 }
272 }
273
274 /*
275 * Print 802.1d / 802.1w / 802.1q (mstp) packets.
276 */
277 void
278 stp_print(const u_char *p, u_int length)
279 {
280 const struct stp_bpdu_ *stp_bpdu;
281 u_int16_t mstp_len;
282
283 stp_bpdu = (struct stp_bpdu_*)p;
284
285 /* Minimum STP Frame size. */
286 if (length < 4)
287 goto trunc;
288
289 if (EXTRACT_16BITS(&stp_bpdu->protocol_id)) {
290 printf("unknown STP version, length %u", length);
291 return;
292 }
293
294 printf("STP %s", tok2str(stp_proto_values, "Unknown STP protocol (0x%02x)",
295 stp_bpdu->protocol_version));
296
297 switch (stp_bpdu->protocol_version) {
298 case STP_PROTO_REGULAR:
299 case STP_PROTO_RAPID:
300 case STP_PROTO_MSTP:
301 break;
302 default:
303 return;
304 }
305
306 printf(", %s", tok2str(stp_bpdu_type_values, "Unknown BPDU Type (0x%02x)",
307 stp_bpdu->bpdu_type));
308
309 switch (stp_bpdu->bpdu_type) {
310 case STP_BPDU_TYPE_CONFIG:
311 if (length < sizeof(struct stp_bpdu_) - 1) {
312 goto trunc;
313 }
314 stp_print_config_bpdu(stp_bpdu, length);
315 break;
316
317 case STP_BPDU_TYPE_RSTP:
318 if (stp_bpdu->protocol_version == STP_PROTO_RAPID) {
319 if (length < sizeof(struct stp_bpdu_)) {
320 goto trunc;
321 }
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) {
325 goto trunc;
326 }
327 if (stp_bpdu->v1_length != 0) {
328 /* FIX ME: Emit a message here ? */
329 goto trunc;
330 }
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)) {
335 goto trunc;
336 }
337 stp_print_mstp_bpdu(stp_bpdu, length);
338 }
339 break;
340
341 case STP_BPDU_TYPE_TOPO_CHANGE:
342 /* always empty message - just break out */
343 break;
344
345 default:
346 break;
347 }
348
349 return;
350 trunc:
351 printf("[|stp %d]", length);
352 }
353
354 /*
355 * Local Variables:
356 * c-style: whitesmith
357 * c-basic-offset: 4
358 * End:
359 */