Home | History | Annotate | Line # | Download | only in dist
print-cdp.c revision 1.1
      1  1.1  christos /*
      2  1.1  christos  * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
      3  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Redistribution and use in source and binary forms, with or without
      6  1.1  christos  * modification, are permitted provided that: (1) source code distributions
      7  1.1  christos  * retain the above copyright notice and this paragraph in its entirety, (2)
      8  1.1  christos  * distributions including binary code include the above copyright notice and
      9  1.1  christos  * this paragraph in its entirety in the documentation or other materials
     10  1.1  christos  * provided with the distribution, and (3) all advertising materials mentioning
     11  1.1  christos  * features or use of this software display the following acknowledgement:
     12  1.1  christos  * ``This product includes software developed by the University of California,
     13  1.1  christos  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
     14  1.1  christos  * the University nor the names of its contributors may be used to endorse
     15  1.1  christos  * or promote products derived from this software without specific prior
     16  1.1  christos  * written permission.
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
     18  1.1  christos  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
     19  1.1  christos  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     20  1.1  christos  *
     21  1.1  christos  * Code by Gert Doering, SpaceNet GmbH, gert (at) space.net
     22  1.1  christos  *
     23  1.1  christos  * Reference documentation:
     24  1.1  christos  *    http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
     25  1.1  christos  */
     26  1.1  christos 
     27  1.1  christos #ifndef lint
     28  1.1  christos static const char rcsid[] _U_ =
     29  1.1  christos     "@(#) Header: /tcpdump/master/tcpdump/print-cdp.c,v 1.25 2004-10-07 14:53:11 hannes Exp";
     30  1.1  christos #endif
     31  1.1  christos 
     32  1.1  christos #ifdef HAVE_CONFIG_H
     33  1.1  christos #include "config.h"
     34  1.1  christos #endif
     35  1.1  christos 
     36  1.1  christos #include <tcpdump-stdinc.h>
     37  1.1  christos 
     38  1.1  christos #include <stdio.h>
     39  1.1  christos #include <string.h>
     40  1.1  christos 
     41  1.1  christos #include "interface.h"
     42  1.1  christos #include "addrtoname.h"
     43  1.1  christos #include "extract.h"			/* must come after interface.h */
     44  1.1  christos #include "nlpid.h"
     45  1.1  christos 
     46  1.1  christos #define CDP_HEADER_LEN  4
     47  1.1  christos 
     48  1.1  christos static struct tok cdp_tlv_values[] = {
     49  1.1  christos     { 0x01,             "Device-ID"},
     50  1.1  christos     { 0x02,             "Address"},
     51  1.1  christos     { 0x03,             "Port-ID"},
     52  1.1  christos     { 0x04,             "Capability"},
     53  1.1  christos     { 0x05,             "Version String"},
     54  1.1  christos     { 0x06,             "Platform"},
     55  1.1  christos     { 0x07,             "Prefixes"},
     56  1.1  christos     { 0x08,             "Protocol-Hello option"},
     57  1.1  christos     { 0x09,             "VTP Management Domain"},
     58  1.1  christos     { 0x0a,             "Native VLAN ID"},
     59  1.1  christos     { 0x0b,             "Duplex"},
     60  1.1  christos     { 0x0e,             "ATA-186 VoIP VLAN request"},
     61  1.1  christos     { 0x0f,             "ATA-186 VoIP VLAN assignment"},
     62  1.1  christos     { 0x10,             "power consumption"},
     63  1.1  christos     { 0x11,             "MTU"},
     64  1.1  christos     { 0x12,             "AVVID trust bitmap"},
     65  1.1  christos     { 0x13,             "AVVID untrusted ports CoS"},
     66  1.1  christos     { 0x14,             "System Name"},
     67  1.1  christos     { 0x15,             "System Object ID (not decoded)"},
     68  1.1  christos     { 0x16,             "Management Addresses"},
     69  1.1  christos     { 0x17,             "Physical Location"},
     70  1.1  christos     { 0, NULL}
     71  1.1  christos };
     72  1.1  christos 
     73  1.1  christos static struct tok cdp_capability_values[] = {
     74  1.1  christos     { 0x01,             "Router" },
     75  1.1  christos     { 0x02,             "Transparent Bridge" },
     76  1.1  christos     { 0x04,             "Source Route Bridge" },
     77  1.1  christos     { 0x08,             "L2 Switch" },
     78  1.1  christos     { 0x10,             "L3 capable" },
     79  1.1  christos     { 0x20,             "IGMP snooping" },
     80  1.1  christos     { 0x40,             "L1 capable" },
     81  1.1  christos     { 0, NULL }
     82  1.1  christos };
     83  1.1  christos 
     84  1.1  christos static int cdp_print_addr(const u_char *, int);
     85  1.1  christos static int cdp_print_prefixes(const u_char *, int);
     86  1.1  christos static unsigned long cdp_get_number(const u_char *, int);
     87  1.1  christos 
     88  1.1  christos void
     89  1.1  christos cdp_print(const u_char *pptr, u_int length, u_int caplen)
     90  1.1  christos {
     91  1.1  christos 	int type, len, i, j;
     92  1.1  christos         const u_char *tptr;
     93  1.1  christos 
     94  1.1  christos 	if (caplen < CDP_HEADER_LEN) {
     95  1.1  christos 		(void)printf("[|cdp]");
     96  1.1  christos 		return;
     97  1.1  christos 	}
     98  1.1  christos 
     99  1.1  christos         tptr = pptr; /* temporary pointer */
    100  1.1  christos 
    101  1.1  christos         if (!TTEST2(*tptr, CDP_HEADER_LEN))
    102  1.1  christos                 goto trunc;
    103  1.1  christos 	printf("CDPv%u, ttl: %us", *tptr, *(tptr+1));
    104  1.1  christos         if (vflag)
    105  1.1  christos                 printf(", checksum: %u (unverified), length %u", EXTRACT_16BITS(tptr), length);
    106  1.1  christos 	tptr += CDP_HEADER_LEN;
    107  1.1  christos 
    108  1.1  christos 	while (tptr < (pptr+length)) {
    109  1.1  christos 
    110  1.1  christos                 if (!TTEST2(*tptr, 4)) /* read out Type and Length */
    111  1.1  christos                     goto trunc;
    112  1.1  christos 		type = EXTRACT_16BITS(tptr);
    113  1.1  christos 		len  = EXTRACT_16BITS(tptr+2); /* object length includes the 4 bytes header length */
    114  1.1  christos                 tptr += 4;
    115  1.1  christos                 len -= 4;
    116  1.1  christos 
    117  1.1  christos 		if (!TTEST2(*tptr, len))
    118  1.1  christos 			goto trunc;
    119  1.1  christos 
    120  1.1  christos                 if (vflag || type == 1) { /* in non-verbose mode just print Device-ID */
    121  1.1  christos 
    122  1.1  christos                     if (vflag)
    123  1.1  christos                         printf("\n\t%s (0x%02x), length: %u byte%s: ",
    124  1.1  christos                                tok2str(cdp_tlv_values,"unknown field type", type),
    125  1.1  christos                                type,
    126  1.1  christos                                len,
    127  1.1  christos                                len>1 ? "s" : ""); /* plural */
    128  1.1  christos 
    129  1.1  christos                     switch (type) {
    130  1.1  christos 
    131  1.1  christos                     case 0x01: /* Device-ID */
    132  1.1  christos                         if (!vflag)
    133  1.1  christos                             printf(", Device-ID ");
    134  1.1  christos                         printf("'");
    135  1.1  christos                         fn_printn(tptr, len, NULL);
    136  1.1  christos                         printf("'");
    137  1.1  christos 			break;
    138  1.1  christos                     case 0x02: /* Address */
    139  1.1  christos                         if (cdp_print_addr(tptr, len) < 0)
    140  1.1  christos                             goto trunc;
    141  1.1  christos 			break;
    142  1.1  christos                     case 0x03: /* Port-ID */
    143  1.1  christos                         printf("'");
    144  1.1  christos                         fn_printn(tptr, len, NULL);
    145  1.1  christos                         printf("'");
    146  1.1  christos 			break;
    147  1.1  christos                     case 0x04: /* Capabilities */
    148  1.1  christos 			printf("(0x%08x): %s",
    149  1.1  christos                                EXTRACT_32BITS(tptr),
    150  1.1  christos                                bittok2str(cdp_capability_values, "none",EXTRACT_32BITS(tptr)));
    151  1.1  christos 			break;
    152  1.1  christos                     case 0x05: /* Version */
    153  1.1  christos                         printf("\n\t  ");
    154  1.1  christos                         for (i=0;i<len;i++) {
    155  1.1  christos                             j = *(tptr+i);
    156  1.1  christos                             putchar(j);
    157  1.1  christos                             if (j == 0x0a) /* lets rework the version string to get a nice identation */
    158  1.1  christos                                 printf("\t  ");
    159  1.1  christos                         }
    160  1.1  christos 			break;
    161  1.1  christos                     case 0x06: /* Platform */
    162  1.1  christos                         printf("'");
    163  1.1  christos                         fn_printn(tptr, len, NULL);
    164  1.1  christos                         printf("'");
    165  1.1  christos 			break;
    166  1.1  christos                     case 0x07: /* Prefixes */
    167  1.1  christos 			if (cdp_print_prefixes(tptr, len) < 0)
    168  1.1  christos                             goto trunc;
    169  1.1  christos 			break;
    170  1.1  christos                     case 0x08: /* Protocol Hello Option - not documented */
    171  1.1  christos 			break;
    172  1.1  christos                     case 0x09: /* VTP Mgmt Domain  - not documented */
    173  1.1  christos                         printf("'");
    174  1.1  christos                         fn_printn(tptr, len, NULL);
    175  1.1  christos                         printf("'");
    176  1.1  christos 			break;
    177  1.1  christos                     case 0x0a: /* Native VLAN ID - not documented */
    178  1.1  christos 			printf("%d",EXTRACT_16BITS(tptr));
    179  1.1  christos 			break;
    180  1.1  christos                     case 0x0b: /* Duplex - not documented */
    181  1.1  christos 			printf("%s", *(tptr) ? "full": "half");
    182  1.1  christos 			break;
    183  1.1  christos 
    184  1.1  christos                     /* http://www.cisco.com/univercd/cc/td/doc/product/voice/ata/atarn/186rn21m.htm
    185  1.1  christos                      * plus more details from other sources
    186  1.1  christos                      */
    187  1.1  christos                     case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
    188  1.1  christos 			printf("app %d, vlan %d",
    189  1.1  christos                                *(tptr), EXTRACT_16BITS(tptr+1));
    190  1.1  christos 			break;
    191  1.1  christos                     case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
    192  1.1  christos 			printf("%1.2fW",
    193  1.1  christos                                cdp_get_number(tptr, len)/1000.0 );
    194  1.1  christos 			break;
    195  1.1  christos                     case 0x11: /* MTU - not documented */
    196  1.1  christos 			printf("%u bytes", EXTRACT_32BITS(tptr));
    197  1.1  christos 			break;
    198  1.1  christos                     case 0x12: /* AVVID trust bitmap - not documented */
    199  1.1  christos 			printf("0x%02x", *(tptr) );
    200  1.1  christos 			break;
    201  1.1  christos                     case 0x13: /* AVVID untrusted port CoS - not documented */
    202  1.1  christos 			printf("0x%02x", *(tptr));
    203  1.1  christos 			break;
    204  1.1  christos                     case 0x14: /* System Name - not documented */
    205  1.1  christos                         printf("'");
    206  1.1  christos                         fn_printn(tptr, len, NULL);
    207  1.1  christos                         printf("'");
    208  1.1  christos 			break;
    209  1.1  christos                     case 0x16: /* System Object ID - not documented */
    210  1.1  christos 			if (cdp_print_addr(tptr, len) < 0)
    211  1.1  christos 				goto trunc;
    212  1.1  christos 			break;
    213  1.1  christos                     case 0x17: /* Physical Location - not documented */
    214  1.1  christos 			printf("0x%02x", *(tptr));
    215  1.1  christos 			if (len > 1) {
    216  1.1  christos 				printf("/");
    217  1.1  christos 	                        fn_printn(tptr + 1, len - 1, NULL);
    218  1.1  christos 	                }
    219  1.1  christos 			break;
    220  1.1  christos                     default:
    221  1.1  christos                         print_unknown_data(tptr,"\n\t  ",len);
    222  1.1  christos 			break;
    223  1.1  christos                     }
    224  1.1  christos                 }
    225  1.1  christos 		/* avoid infinite loop */
    226  1.1  christos 		if (len == 0)
    227  1.1  christos 			break;
    228  1.1  christos 		tptr = tptr+len;
    229  1.1  christos 	}
    230  1.1  christos         if (vflag < 1)
    231  1.1  christos             printf(", length %u",caplen);
    232  1.1  christos 
    233  1.1  christos 	return;
    234  1.1  christos trunc:
    235  1.1  christos 	printf("[|cdp]");
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos /*
    239  1.1  christos  * Protocol type values.
    240  1.1  christos  *
    241  1.1  christos  * PT_NLPID means that the protocol type field contains an OSI NLPID.
    242  1.1  christos  *
    243  1.1  christos  * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
    244  1.1  christos  * LLC header that specifies that the payload is for that protocol.
    245  1.1  christos  */
    246  1.1  christos #define PT_NLPID		1	/* OSI NLPID */
    247  1.1  christos #define PT_IEEE_802_2		2	/* IEEE 802.2 LLC header */
    248  1.1  christos 
    249  1.1  christos static int
    250  1.1  christos cdp_print_addr(const u_char * p, int l)
    251  1.1  christos {
    252  1.1  christos 	int pt, pl, al, num;
    253  1.1  christos 	const u_char *endp = p + l;
    254  1.1  christos #ifdef INET6
    255  1.1  christos 	static u_char prot_ipv6[] = {
    256  1.1  christos 		0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
    257  1.1  christos 	};
    258  1.1  christos #endif
    259  1.1  christos 
    260  1.1  christos 	TCHECK2(*p, 2);
    261  1.1  christos 	num = EXTRACT_32BITS(p);
    262  1.1  christos 	p += 4;
    263  1.1  christos 
    264  1.1  christos 	while (p < endp && num >= 0) {
    265  1.1  christos 		TCHECK2(*p, 2);
    266  1.1  christos 		if (p + 2 > endp)
    267  1.1  christos 			goto trunc;
    268  1.1  christos 		pt = p[0];		/* type of "protocol" field */
    269  1.1  christos 		pl = p[1];		/* length of "protocol" field */
    270  1.1  christos 		p += 2;
    271  1.1  christos 
    272  1.1  christos 		TCHECK2(p[pl], 2);
    273  1.1  christos 		if (p + pl + 2 > endp)
    274  1.1  christos 			goto trunc;
    275  1.1  christos 		al = EXTRACT_16BITS(&p[pl]);	/* address length */
    276  1.1  christos 
    277  1.1  christos 		if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
    278  1.1  christos 			/*
    279  1.1  christos 			 * IPv4: protocol type = NLPID, protocol length = 1
    280  1.1  christos 			 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
    281  1.1  christos 			 * address length = 4
    282  1.1  christos 			 */
    283  1.1  christos 			p += 3;
    284  1.1  christos 
    285  1.1  christos 			TCHECK2(*p, 4);
    286  1.1  christos 			if (p + 4 > endp)
    287  1.1  christos 				goto trunc;
    288  1.1  christos 			printf("IPv4 (%u) %s",
    289  1.1  christos                                num,
    290  1.1  christos                                ipaddr_string(p));
    291  1.1  christos 			p += 4;
    292  1.1  christos 		}
    293  1.1  christos #ifdef INET6
    294  1.1  christos 		else if (pt == PT_IEEE_802_2 && pl == 8 &&
    295  1.1  christos 		    memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
    296  1.1  christos 			/*
    297  1.1  christos 			 * IPv6: protocol type = IEEE 802.2 header,
    298  1.1  christos 			 * protocol length = 8 (size of LLC+SNAP header),
    299  1.1  christos 			 * protocol = LLC+SNAP header with the IPv6
    300  1.1  christos 			 * Ethertype, address length = 16
    301  1.1  christos 			 */
    302  1.1  christos 			p += 10;
    303  1.1  christos 			TCHECK2(*p, al);
    304  1.1  christos 			if (p + al > endp)
    305  1.1  christos 				goto trunc;
    306  1.1  christos 
    307  1.1  christos 			printf("IPv6 (%u) %s",
    308  1.1  christos                                num,
    309  1.1  christos                                ip6addr_string(p));
    310  1.1  christos 			p += al;
    311  1.1  christos 		}
    312  1.1  christos #endif
    313  1.1  christos 		else {
    314  1.1  christos 			/*
    315  1.1  christos 			 * Generic case: just print raw data
    316  1.1  christos 			 */
    317  1.1  christos 			TCHECK2(*p, pl);
    318  1.1  christos 			if (p + pl > endp)
    319  1.1  christos 				goto trunc;
    320  1.1  christos 			printf("pt=0x%02x, pl=%d, pb=", *(p - 2), pl);
    321  1.1  christos 			while (pl-- > 0)
    322  1.1  christos 				printf(" %02x", *p++);
    323  1.1  christos 			TCHECK2(*p, 2);
    324  1.1  christos 			if (p + 2 > endp)
    325  1.1  christos 				goto trunc;
    326  1.1  christos 			al = (*p << 8) + *(p + 1);
    327  1.1  christos 			printf(", al=%d, a=", al);
    328  1.1  christos 			p += 2;
    329  1.1  christos 			TCHECK2(*p, al);
    330  1.1  christos 			if (p + al > endp)
    331  1.1  christos 				goto trunc;
    332  1.1  christos 			while (al-- > 0)
    333  1.1  christos 				printf(" %02x", *p++);
    334  1.1  christos 		}
    335  1.1  christos 		num--;
    336  1.1  christos 		if (num)
    337  1.1  christos 			printf(" ");
    338  1.1  christos 	}
    339  1.1  christos 
    340  1.1  christos 	return 0;
    341  1.1  christos 
    342  1.1  christos trunc:
    343  1.1  christos 	return -1;
    344  1.1  christos }
    345  1.1  christos 
    346  1.1  christos 
    347  1.1  christos static int
    348  1.1  christos cdp_print_prefixes(const u_char * p, int l)
    349  1.1  christos {
    350  1.1  christos 	if (l % 5)
    351  1.1  christos 		goto trunc;
    352  1.1  christos 
    353  1.1  christos 	printf(" IPv4 Prefixes (%d):", l / 5);
    354  1.1  christos 
    355  1.1  christos 	while (l > 0) {
    356  1.1  christos 		printf(" %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]);
    357  1.1  christos 		l -= 5;
    358  1.1  christos 		p += 5;
    359  1.1  christos 	}
    360  1.1  christos 
    361  1.1  christos 	return 0;
    362  1.1  christos 
    363  1.1  christos trunc:
    364  1.1  christos 	return -1;
    365  1.1  christos }
    366  1.1  christos 
    367  1.1  christos /* read in a <n>-byte number, MSB first
    368  1.1  christos  * (of course this can handle max sizeof(long))
    369  1.1  christos  */
    370  1.1  christos static unsigned long cdp_get_number(const u_char * p, int l)
    371  1.1  christos {
    372  1.1  christos     unsigned long res=0;
    373  1.1  christos     while( l>0 )
    374  1.1  christos     {
    375  1.1  christos 	res = (res<<8) + *p;
    376  1.1  christos 	p++; l--;
    377  1.1  christos     }
    378  1.1  christos     return res;
    379  1.1  christos }
    380