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