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