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