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