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