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