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