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