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