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