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