xref: /freebsd/contrib/tcpdump/print-stp.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
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 #define NETDISSECT_REWORKED
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 
17 #include <tcpdump-stdinc.h>
18 
19 #include <stdio.h>
20 
21 #include "interface.h"
22 #include "extract.h"
23 
24 #define	RSTP_EXTRACT_PORT_ROLE(x) (((x)&0x0C)>>2)
25 /* STP timers are expressed in multiples of 1/256th second */
26 #define STP_TIME_BASE 256
27 #define STP_BPDU_MSTP_MIN_LEN 102
28 
29 struct stp_bpdu_ {
30     uint8_t protocol_id[2];
31     uint8_t protocol_version;
32     uint8_t bpdu_type;
33     uint8_t flags;
34     uint8_t root_id[8];
35     uint8_t root_path_cost[4];
36     uint8_t bridge_id[8];
37     uint8_t port_id[2];
38     uint8_t message_age[2];
39     uint8_t max_age[2];
40     uint8_t hello_time[2];
41     uint8_t forward_delay[2];
42     uint8_t v1_length;
43 };
44 
45 #define STP_PROTO_REGULAR 0x00
46 #define STP_PROTO_RAPID   0x02
47 #define STP_PROTO_MSTP    0x03
48 #define STP_PROTO_SPB     0x04
49 
50 static const struct tok stp_proto_values[] = {
51     { STP_PROTO_REGULAR, "802.1d" },
52     { STP_PROTO_RAPID, "802.1w" },
53     { STP_PROTO_MSTP, "802.1s" },
54     { STP_PROTO_SPB, "802.1aq" },
55     { 0, NULL}
56 };
57 
58 #define STP_BPDU_TYPE_CONFIG      0x00
59 #define STP_BPDU_TYPE_RSTP        0x02
60 #define STP_BPDU_TYPE_TOPO_CHANGE 0x80
61 
62 static const struct tok stp_bpdu_flag_values[] = {
63     { 0x01, "Topology change" },
64     { 0x02, "Proposal" },
65     { 0x10, "Learn" },
66     { 0x20, "Forward" },
67     { 0x40, "Agreement" },
68     { 0x80, "Topology change ACK" },
69     { 0, NULL}
70 };
71 
72 static const struct tok stp_bpdu_type_values[] = {
73     { STP_BPDU_TYPE_CONFIG, "Config" },
74     { STP_BPDU_TYPE_RSTP, "Rapid STP" },
75     { STP_BPDU_TYPE_TOPO_CHANGE, "Topology Change" },
76     { 0, NULL}
77 };
78 
79 static const struct tok rstp_obj_port_role_values[] = {
80     { 0x00, "Unknown" },
81     { 0x01, "Alternate" },
82     { 0x02, "Root" },
83     { 0x03, "Designated" },
84     { 0, NULL}
85 };
86 
87 static char *
88 stp_print_bridge_id(const u_char *p)
89 {
90     static char bridge_id_str[sizeof("pppp.aa:bb:cc:dd:ee:ff")];
91 
92     snprintf(bridge_id_str, sizeof(bridge_id_str),
93              "%.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
94              p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
95 
96     return bridge_id_str;
97 }
98 
99 static void
100 stp_print_config_bpdu(netdissect_options *ndo, const struct stp_bpdu_ *stp_bpdu,
101                       u_int length)
102 {
103     ND_PRINT((ndo, ", Flags [%s]",
104            bittok2str(stp_bpdu_flag_values, "none", stp_bpdu->flags)));
105 
106     ND_PRINT((ndo, ", bridge-id %s.%04x, length %u",
107            stp_print_bridge_id((const u_char *)&stp_bpdu->bridge_id),
108            EXTRACT_16BITS(&stp_bpdu->port_id), length));
109 
110     /* in non-verbose mode just print the bridge-id */
111     if (!ndo->ndo_vflag) {
112         return;
113     }
114 
115     ND_PRINT((ndo, "\n\tmessage-age %.2fs, max-age %.2fs"
116            ", hello-time %.2fs, forwarding-delay %.2fs",
117            (float)EXTRACT_16BITS(&stp_bpdu->message_age) / STP_TIME_BASE,
118            (float)EXTRACT_16BITS(&stp_bpdu->max_age) / STP_TIME_BASE,
119            (float)EXTRACT_16BITS(&stp_bpdu->hello_time) / STP_TIME_BASE,
120            (float)EXTRACT_16BITS(&stp_bpdu->forward_delay) / STP_TIME_BASE));
121 
122     ND_PRINT((ndo, "\n\troot-id %s, root-pathcost %u",
123            stp_print_bridge_id((const u_char *)&stp_bpdu->root_id),
124            EXTRACT_32BITS(&stp_bpdu->root_path_cost)));
125 
126     /* Port role is only valid for 802.1w */
127     if (stp_bpdu->protocol_version == STP_PROTO_RAPID) {
128         ND_PRINT((ndo, ", port-role %s",
129                tok2str(rstp_obj_port_role_values, "Unknown",
130                        RSTP_EXTRACT_PORT_ROLE(stp_bpdu->flags))));
131     }
132 }
133 
134 /*
135  * MSTP packet format
136  * Ref. IEEE 802.1Q 2003 Ed. Section 14
137  *
138  * MSTP BPDU
139  *
140  * 2 -  bytes Protocol Id
141  * 1 -  byte  Protocol Ver.
142  * 1 -  byte  BPDU tye
143  * 1 -  byte  Flags
144  * 8 -  bytes CIST Root Identifier
145  * 4 -  bytes CIST External Path Cost
146  * 8 -  bytes CIST Regional Root Identifier
147  * 2 -  bytes CIST Port Identifier
148  * 2 -  bytes Message Age
149  * 2 -  bytes Max age
150  * 2 -  bytes Hello Time
151  * 2 -  bytes Forward delay
152  * 1 -  byte  Version 1 length. Must be 0
153  * 2 -  bytes Version 3 length
154  * 1 -  byte  Config Identifier
155  * 32 - bytes Config Name
156  * 2 -  bytes Revision level
157  * 16 - bytes Config Digest [MD5]
158  * 4 -  bytes CIST Internal Root Path Cost
159  * 8 -  bytes CIST Bridge Identifier
160  * 1 -  byte  CIST Remaining Hops
161  * 16 - bytes MSTI information [Max 64 MSTI, each 16 bytes]
162  *
163  *
164  * SPB BPDU
165  * Ref. IEEE 802.1aq. Section 14
166  *
167  * 2 -  bytes Version 4 length
168  * 1 -  byte  Aux Config Identifier
169  * 32 - bytes Aux Config Name
170  * 2 -  bytes Aux Revision level
171  * 16 - bytes Aux Config Digest [MD5]
172  * 1 -  byte  (1 - 2) Agreement Number
173  *            (3 - 4) Discarded Agreement Number
174  *            (5) Agreement Valid Flag
175  *            (6) Restricted Role Flag
176  *            (7 - 8) Unused sent zero
177  * 1 -  byte Unused
178  * 1 -  byte (1 - 4) Agreement Digest Format Identifier
179  *           (5 - 8) Agreement Digest Format Capabilities
180  * 1 -  byte (1 - 4) Agreement Digest Convention Identifier
181  *           (5 - 8) Agreement Digest Convention Capabilities
182  * 2 -  bytes Agreement Digest Edge Count
183  * 8 -  byte Reserved Set
184  * 20 - bytes Computed Topology Digest
185  *
186  *
187  * MSTI Payload
188  *
189  * 1 - byte  MSTI flag
190  * 8 - bytes MSTI Regional Root Identifier
191  * 4 - bytes MSTI Regional Path Cost
192  * 1 - byte  MSTI Bridge Priority
193  * 1 - byte  MSTI Port Priority
194  * 1 - byte  MSTI Remaining Hops
195  *
196  */
197 
198 #define MST_BPDU_MSTI_LENGTH		    16
199 #define MST_BPDU_CONFIG_INFO_LENGTH	    64
200 
201 /* Offsets of fields from the begginning for the packet */
202 #define MST_BPDU_VER3_LEN_OFFSET	    36
203 #define MST_BPDU_CONFIG_NAME_OFFSET	    39
204 #define MST_BPDU_CONFIG_DIGEST_OFFSET	    73
205 #define MST_BPDU_CIST_INT_PATH_COST_OFFSET  89
206 #define MST_BPDU_CIST_BRIDGE_ID_OFFSET	    93
207 #define MST_BPDU_CIST_REMAIN_HOPS_OFFSET    101
208 #define MST_BPDU_MSTI_OFFSET		    102
209 /* Offsets within  an MSTI */
210 #define MST_BPDU_MSTI_ROOT_PRIO_OFFSET	    1
211 #define MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET 9
212 #define MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET    13
213 #define MST_BPDU_MSTI_PORT_PRIO_OFFSET	    14
214 #define MST_BPDU_MSTI_REMAIN_HOPS_OFFSET    15
215 
216 #define SPB_BPDU_MIN_LEN                  87
217 #define SPB_BPDU_CONFIG_NAME_OFFSET       3
218 #define SPB_BPDU_CONFIG_REV_OFFSET        SPB_BPDU_CONFIG_NAME_OFFSET + 32
219 #define SPB_BPDU_CONFIG_DIGEST_OFFSET     SPB_BPDU_CONFIG_REV_OFFSET + 2
220 #define SPB_BPDU_AGREEMENT_OFFSET         SPB_BPDU_CONFIG_DIGEST_OFFSET + 16
221 #define SPB_BPDU_AGREEMENT_UNUSED_OFFSET  SPB_BPDU_AGREEMENT_OFFSET + 1
222 #define SPB_BPDU_AGREEMENT_FORMAT_OFFSET  SPB_BPDU_AGREEMENT_UNUSED_OFFSET + 1
223 #define SPB_BPDU_AGREEMENT_CON_OFFSET     SPB_BPDU_AGREEMENT_FORMAT_OFFSET + 1
224 #define SPB_BPDU_AGREEMENT_EDGE_OFFSET    SPB_BPDU_AGREEMENT_CON_OFFSET + 1
225 #define SPB_BPDU_AGREEMENT_RES1_OFFSET    SPB_BPDU_AGREEMENT_EDGE_OFFSET + 2
226 #define SPB_BPDU_AGREEMENT_RES2_OFFSET    SPB_BPDU_AGREEMENT_RES1_OFFSET + 4
227 #define SPB_BPDU_AGREEMENT_DIGEST_OFFSET  SPB_BPDU_AGREEMENT_RES2_OFFSET + 4
228 
229 
230 static void
231 stp_print_mstp_bpdu(netdissect_options *ndo, const struct stp_bpdu_ *stp_bpdu,
232                     u_int length)
233 {
234     const u_char *ptr;
235     uint16_t	    v3len;
236     uint16_t	    len;
237     uint16_t	    msti;
238     u_int	    offset;
239 
240     ptr = (const u_char *)stp_bpdu;
241     ND_PRINT((ndo, ", CIST Flags [%s], length %u",
242            bittok2str(stp_bpdu_flag_values, "none", stp_bpdu->flags), length));
243 
244     /*
245      * in non-verbose mode just print the flags.
246      */
247     if (!ndo->ndo_vflag) {
248         return;
249     }
250 
251     ND_PRINT((ndo, "\n\tport-role %s, ",
252            tok2str(rstp_obj_port_role_values, "Unknown",
253                    RSTP_EXTRACT_PORT_ROLE(stp_bpdu->flags))));
254 
255     ND_PRINT((ndo, "CIST root-id %s, CIST ext-pathcost %u ",
256            stp_print_bridge_id((const u_char *)&stp_bpdu->root_id),
257            EXTRACT_32BITS(&stp_bpdu->root_path_cost)));
258 
259     ND_PRINT((ndo, "\n\tCIST regional-root-id %s, ",
260            stp_print_bridge_id((const u_char *)&stp_bpdu->bridge_id)));
261 
262     ND_PRINT((ndo, "CIST port-id %04x, ", EXTRACT_16BITS(&stp_bpdu->port_id)));
263 
264     ND_PRINT((ndo, "\n\tmessage-age %.2fs, max-age %.2fs"
265            ", hello-time %.2fs, forwarding-delay %.2fs",
266            (float)EXTRACT_16BITS(&stp_bpdu->message_age) / STP_TIME_BASE,
267            (float)EXTRACT_16BITS(&stp_bpdu->max_age) / STP_TIME_BASE,
268            (float)EXTRACT_16BITS(&stp_bpdu->hello_time) / STP_TIME_BASE,
269            (float)EXTRACT_16BITS(&stp_bpdu->forward_delay) / STP_TIME_BASE));
270 
271     ND_PRINT((ndo, "\n\tv3len %d, ", EXTRACT_16BITS(ptr + MST_BPDU_VER3_LEN_OFFSET)));
272     ND_PRINT((ndo, "MCID Name %s, rev %u, "
273             "\n\t\tdigest %08x%08x%08x%08x, ",
274             ptr + MST_BPDU_CONFIG_NAME_OFFSET,
275 	          EXTRACT_16BITS(ptr + MST_BPDU_CONFIG_NAME_OFFSET + 32),
276       	    EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET),
277         	  EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 4),
278 	          EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 8),
279 	          EXTRACT_32BITS(ptr + MST_BPDU_CONFIG_DIGEST_OFFSET + 12)));
280 
281     ND_PRINT((ndo, "CIST int-root-pathcost %u, ",
282             EXTRACT_32BITS(ptr + MST_BPDU_CIST_INT_PATH_COST_OFFSET)));
283 
284     ND_PRINT((ndo, "\n\tCIST bridge-id %s, ",
285            stp_print_bridge_id(ptr + MST_BPDU_CIST_BRIDGE_ID_OFFSET)));
286 
287     ND_PRINT((ndo, "CIST remaining-hops %d", ptr[MST_BPDU_CIST_REMAIN_HOPS_OFFSET]));
288 
289     /* Dump all MSTI's */
290     v3len = EXTRACT_16BITS(ptr + MST_BPDU_VER3_LEN_OFFSET);
291     if (v3len > MST_BPDU_CONFIG_INFO_LENGTH) {
292         len = v3len - MST_BPDU_CONFIG_INFO_LENGTH;
293         offset = MST_BPDU_MSTI_OFFSET;
294         while (len >= MST_BPDU_MSTI_LENGTH) {
295             msti = EXTRACT_16BITS(ptr + offset +
296                                   MST_BPDU_MSTI_ROOT_PRIO_OFFSET);
297             msti = msti & 0x0FFF;
298 
299             ND_PRINT((ndo, "\n\tMSTI %d, Flags [%s], port-role %s",
300                    msti, bittok2str(stp_bpdu_flag_values, "none", ptr[offset]),
301                    tok2str(rstp_obj_port_role_values, "Unknown",
302                            RSTP_EXTRACT_PORT_ROLE(ptr[offset]))));
303             ND_PRINT((ndo, "\n\t\tMSTI regional-root-id %s, pathcost %u",
304                    stp_print_bridge_id(ptr + offset +
305                                        MST_BPDU_MSTI_ROOT_PRIO_OFFSET),
306                    EXTRACT_32BITS(ptr + offset +
307                                   MST_BPDU_MSTI_ROOT_PATH_COST_OFFSET)));
308             ND_PRINT((ndo, "\n\t\tMSTI bridge-prio %d, port-prio %d, hops %d",
309                    ptr[offset + MST_BPDU_MSTI_BRIDGE_PRIO_OFFSET] >> 4,
310                    ptr[offset + MST_BPDU_MSTI_PORT_PRIO_OFFSET] >> 4,
311                    ptr[offset + MST_BPDU_MSTI_REMAIN_HOPS_OFFSET]));
312 
313             len -= MST_BPDU_MSTI_LENGTH;
314             offset += MST_BPDU_MSTI_LENGTH;
315         }
316     }
317 }
318 
319 static void
320 stp_print_spb_bpdu(netdissect_options *ndo, const struct stp_bpdu_ *stp_bpdu,
321                    u_int offset)
322 {
323     const u_char *ptr;
324 
325     /*
326      * in non-verbose mode don't print anything.
327      */
328     if (!ndo->ndo_vflag) {
329         return;
330     }
331 
332     ptr = (const u_char *)stp_bpdu;
333     ND_PRINT((ndo, "\n\tv4len %d AUXMCID Name %s, Rev %u, \n\t\tdigest %08x%08x%08x%08x",
334             EXTRACT_16BITS (ptr + offset),
335             ptr + offset + SPB_BPDU_CONFIG_NAME_OFFSET,
336             EXTRACT_16BITS(ptr + offset + SPB_BPDU_CONFIG_REV_OFFSET),
337             EXTRACT_32BITS(ptr + offset + SPB_BPDU_CONFIG_DIGEST_OFFSET),
338             EXTRACT_32BITS(ptr + offset + SPB_BPDU_CONFIG_DIGEST_OFFSET + 4),
339             EXTRACT_32BITS(ptr + offset + SPB_BPDU_CONFIG_DIGEST_OFFSET + 8),
340             EXTRACT_32BITS(ptr + offset + SPB_BPDU_CONFIG_DIGEST_OFFSET + 12)));
341 
342     ND_PRINT((ndo, "\n\tAgreement num %d, Discarded Agreement num %d, Agreement valid-"
343             "flag %d, \n\tRestricted role-flag: %d, Format id %d cap %d, "
344             "Convention id %d cap %d, \n\tEdge count %d, "
345             "Agreement digest %08x%08x%08x%08x%08x\n",
346             ptr[offset + SPB_BPDU_AGREEMENT_OFFSET]>>6,
347             ptr[offset + SPB_BPDU_AGREEMENT_OFFSET]>>4 & 0x3,
348             ptr[offset + SPB_BPDU_AGREEMENT_OFFSET]>>3 & 0x1,
349             ptr[offset + SPB_BPDU_AGREEMENT_OFFSET]>>2 & 0x1,
350             ptr[offset + SPB_BPDU_AGREEMENT_FORMAT_OFFSET]>>4,
351             ptr[offset + SPB_BPDU_AGREEMENT_FORMAT_OFFSET]&0x00ff,
352             ptr[offset + SPB_BPDU_AGREEMENT_CON_OFFSET]>>4,
353             ptr[offset + SPB_BPDU_AGREEMENT_CON_OFFSET]&0x00ff,
354             EXTRACT_16BITS(ptr + offset + SPB_BPDU_AGREEMENT_EDGE_OFFSET),
355             EXTRACT_32BITS(ptr + offset + SPB_BPDU_AGREEMENT_DIGEST_OFFSET),
356             EXTRACT_32BITS(ptr + offset + SPB_BPDU_AGREEMENT_DIGEST_OFFSET)+4,
357             EXTRACT_32BITS(ptr + offset + SPB_BPDU_AGREEMENT_DIGEST_OFFSET)+8,
358             EXTRACT_32BITS(ptr + offset + SPB_BPDU_AGREEMENT_DIGEST_OFFSET)+12,
359             EXTRACT_32BITS(ptr + offset + SPB_BPDU_AGREEMENT_DIGEST_OFFSET)+16));
360 }
361 
362 /*
363  * Print 802.1d / 802.1w / 802.1q (mstp) / 802.1aq (spb) packets.
364  */
365 void
366 stp_print(netdissect_options *ndo, const u_char *p, u_int length)
367 {
368     const struct stp_bpdu_ *stp_bpdu;
369     u_int                  mstp_len;
370     u_int                  spb_len;
371 
372     stp_bpdu = (struct stp_bpdu_*)p;
373 
374     /* Minimum STP Frame size. */
375     if (length < 4)
376         goto trunc;
377 
378     if (EXTRACT_16BITS(&stp_bpdu->protocol_id)) {
379         ND_PRINT((ndo, "unknown STP version, length %u", length));
380         return;
381     }
382 
383     ND_PRINT((ndo, "STP %s", tok2str(stp_proto_values, "Unknown STP protocol (0x%02x)",
384                          stp_bpdu->protocol_version)));
385 
386     switch (stp_bpdu->protocol_version) {
387     case STP_PROTO_REGULAR:
388     case STP_PROTO_RAPID:
389     case STP_PROTO_MSTP:
390     case STP_PROTO_SPB:
391         break;
392     default:
393         return;
394     }
395 
396     ND_PRINT((ndo, ", %s", tok2str(stp_bpdu_type_values, "Unknown BPDU Type (0x%02x)",
397                            stp_bpdu->bpdu_type)));
398 
399     switch (stp_bpdu->bpdu_type) {
400     case STP_BPDU_TYPE_CONFIG:
401         if (length < sizeof(struct stp_bpdu_) - 1) {
402             goto trunc;
403         }
404         stp_print_config_bpdu(ndo, stp_bpdu, length);
405         break;
406 
407     case STP_BPDU_TYPE_RSTP:
408         if (stp_bpdu->protocol_version == STP_PROTO_RAPID) {
409             if (length < sizeof(struct stp_bpdu_)) {
410                 goto trunc;
411             }
412             stp_print_config_bpdu(ndo, stp_bpdu, length);
413         } else if (stp_bpdu->protocol_version == STP_PROTO_MSTP ||
414                    stp_bpdu->protocol_version == STP_PROTO_SPB) {
415             if (length < STP_BPDU_MSTP_MIN_LEN) {
416                 goto trunc;
417             }
418 
419             if (stp_bpdu->v1_length != 0) {
420                 /* FIX ME: Emit a message here ? */
421                 goto trunc;
422             }
423 
424             /* Validate v3 length */
425             mstp_len = EXTRACT_16BITS(p + MST_BPDU_VER3_LEN_OFFSET);
426             mstp_len += 2;  /* length encoding itself is 2 bytes */
427             if (length < (sizeof(struct stp_bpdu_) + mstp_len)) {
428                 goto trunc;
429             }
430             stp_print_mstp_bpdu(ndo, stp_bpdu, length);
431 
432             if (stp_bpdu->protocol_version == STP_PROTO_SPB)
433             {
434               /* Validate v4 length */
435               spb_len = EXTRACT_16BITS (p + MST_BPDU_VER3_LEN_OFFSET + mstp_len);
436               spb_len += 2;
437               if (length < (sizeof(struct stp_bpdu_) + mstp_len + spb_len) ||
438                   spb_len < SPB_BPDU_MIN_LEN) {
439                 goto trunc;
440               }
441               stp_print_spb_bpdu(ndo, stp_bpdu, (sizeof(struct stp_bpdu_) + mstp_len));
442             }
443         }
444         break;
445 
446     case STP_BPDU_TYPE_TOPO_CHANGE:
447         /* always empty message - just break out */
448         break;
449 
450     default:
451         break;
452     }
453 
454     return;
455  trunc:
456     ND_PRINT((ndo, "[|stp %d]", length));
457 }
458 
459 /*
460  * Local Variables:
461  * c-style: whitesmith
462  * c-basic-offset: 4
463  * End:
464  */
465