etc/services - sync with NetBSD-8
[minix.git] / external / bsd / tcpdump / dist / print-cdp.c
blob4ce79ffd15500d2701dcdc758ee5f89f9e597e19
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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.
21 * Code by Gert Doering, SpaceNet GmbH, gert@space.net
23 * Reference documentation:
24 * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: print-cdp.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
30 #endif
32 #define NETDISSECT_REWORKED
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
37 #include <tcpdump-stdinc.h>
39 #include <string.h>
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
44 #include "nlpid.h"
46 static const char tstr[] = "[|cdp]";
48 #define CDP_HEADER_LEN 4
49 #define CDP_HEADER_VERSION_OFFSET 0
50 #define CDP_HEADER_TTL_OFFSET 1
51 #define CDP_HEADER_CHECKSUM_OFFSET 2
53 #define CDP_TLV_HEADER_LEN 4
54 #define CDP_TLV_TYPE_OFFSET 0
55 #define CDP_TLV_LEN_OFFSET 2
57 static const struct tok cdp_tlv_values[] = {
58 { 0x01, "Device-ID"},
59 { 0x02, "Address"},
60 { 0x03, "Port-ID"},
61 { 0x04, "Capability"},
62 { 0x05, "Version String"},
63 { 0x06, "Platform"},
64 { 0x07, "Prefixes"},
65 { 0x08, "Protocol-Hello option"},
66 { 0x09, "VTP Management Domain"},
67 { 0x0a, "Native VLAN ID"},
68 { 0x0b, "Duplex"},
69 { 0x0e, "ATA-186 VoIP VLAN request"},
70 { 0x0f, "ATA-186 VoIP VLAN assignment"},
71 { 0x10, "power consumption"},
72 { 0x11, "MTU"},
73 { 0x12, "AVVID trust bitmap"},
74 { 0x13, "AVVID untrusted ports CoS"},
75 { 0x14, "System Name"},
76 { 0x15, "System Object ID (not decoded)"},
77 { 0x16, "Management Addresses"},
78 { 0x17, "Physical Location"},
79 { 0, NULL}
82 static const struct tok cdp_capability_values[] = {
83 { 0x01, "Router" },
84 { 0x02, "Transparent Bridge" },
85 { 0x04, "Source Route Bridge" },
86 { 0x08, "L2 Switch" },
87 { 0x10, "L3 capable" },
88 { 0x20, "IGMP snooping" },
89 { 0x40, "L1 capable" },
90 { 0, NULL }
93 static int cdp_print_addr(netdissect_options *, const u_char *, int);
94 static int cdp_print_prefixes(netdissect_options *, const u_char *, int);
95 static unsigned long cdp_get_number(const u_char *, int);
97 void
98 cdp_print(netdissect_options *ndo,
99 const u_char *pptr, u_int length, u_int caplen)
101 int type, len, i, j;
102 const u_char *tptr;
104 if (caplen < CDP_HEADER_LEN) {
105 ND_PRINT((ndo, "%s", tstr));
106 return;
109 tptr = pptr; /* temporary pointer */
111 ND_TCHECK2(*tptr, CDP_HEADER_LEN);
112 ND_PRINT((ndo, "CDPv%u, ttl: %us", *(tptr + CDP_HEADER_VERSION_OFFSET),
113 *(tptr + CDP_HEADER_TTL_OFFSET)));
114 if (ndo->ndo_vflag)
115 ND_PRINT((ndo, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr+CDP_HEADER_CHECKSUM_OFFSET), length));
116 tptr += CDP_HEADER_LEN;
118 while (tptr < (pptr+length)) {
119 ND_TCHECK2(*tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
120 type = EXTRACT_16BITS(tptr+CDP_TLV_TYPE_OFFSET);
121 len = EXTRACT_16BITS(tptr+CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
122 if (len < CDP_TLV_HEADER_LEN) {
123 if (ndo->ndo_vflag)
124 ND_PRINT((ndo, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
125 tok2str(cdp_tlv_values,"unknown field type", type),
126 type,
127 len,
128 PLURAL_SUFFIX(len))); /* plural */
129 else
130 ND_PRINT((ndo, ", %s TLV length %u too short",
131 tok2str(cdp_tlv_values,"unknown field type", type),
132 len));
133 break;
135 tptr += CDP_TLV_HEADER_LEN;
136 len -= CDP_TLV_HEADER_LEN;
138 ND_TCHECK2(*tptr, len);
140 if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
142 if (ndo->ndo_vflag)
143 ND_PRINT((ndo, "\n\t%s (0x%02x), value length: %u byte%s: ",
144 tok2str(cdp_tlv_values,"unknown field type", type),
145 type,
146 len,
147 PLURAL_SUFFIX(len))); /* plural */
149 switch (type) {
151 case 0x01: /* Device-ID */
152 if (!ndo->ndo_vflag)
153 ND_PRINT((ndo, ", Device-ID "));
154 ND_PRINT((ndo, "'"));
155 (void)fn_printn(ndo, tptr, len, NULL);
156 ND_PRINT((ndo, "'"));
157 break;
158 case 0x02: /* Address */
159 if (cdp_print_addr(ndo, tptr, len) < 0)
160 goto trunc;
161 break;
162 case 0x03: /* Port-ID */
163 ND_PRINT((ndo, "'"));
164 (void)fn_printn(ndo, tptr, len, NULL);
165 ND_PRINT((ndo, "'"));
166 break;
167 case 0x04: /* Capabilities */
168 if (len < 4)
169 goto trunc;
170 ND_PRINT((ndo, "(0x%08x): %s",
171 EXTRACT_32BITS(tptr),
172 bittok2str(cdp_capability_values, "none", EXTRACT_32BITS(tptr))));
173 break;
174 case 0x05: /* Version */
175 ND_PRINT((ndo, "\n\t "));
176 for (i=0;i<len;i++) {
177 j = *(tptr+i);
178 ND_PRINT((ndo, "%c", j));
179 if (j == 0x0a) /* lets rework the version string to get a nice indentation */
180 ND_PRINT((ndo, "\t "));
182 break;
183 case 0x06: /* Platform */
184 ND_PRINT((ndo, "'"));
185 (void)fn_printn(ndo, tptr, len, NULL);
186 ND_PRINT((ndo, "'"));
187 break;
188 case 0x07: /* Prefixes */
189 if (cdp_print_prefixes(ndo, tptr, len) < 0)
190 goto trunc;
191 break;
192 case 0x08: /* Protocol Hello Option - not documented */
193 break;
194 case 0x09: /* VTP Mgmt Domain - CDPv2 */
195 ND_PRINT((ndo, "'"));
196 (void)fn_printn(ndo, tptr, len, NULL);
197 ND_PRINT((ndo, "'"));
198 break;
199 case 0x0a: /* Native VLAN ID - CDPv2 */
200 if (len < 2)
201 goto trunc;
202 ND_PRINT((ndo, "%d", EXTRACT_16BITS(tptr)));
203 break;
204 case 0x0b: /* Duplex - CDPv2 */
205 if (len < 1)
206 goto trunc;
207 ND_PRINT((ndo, "%s", *(tptr) ? "full": "half"));
208 break;
210 /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
211 * plus more details from other sources
213 case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
214 if (len < 3)
215 goto trunc;
216 ND_PRINT((ndo, "app %d, vlan %d", *(tptr), EXTRACT_16BITS(tptr + 1)));
217 break;
218 case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
219 ND_PRINT((ndo, "%1.2fW", cdp_get_number(tptr, len) / 1000.0));
220 break;
221 case 0x11: /* MTU - not documented */
222 if (len < 4)
223 goto trunc;
224 ND_PRINT((ndo, "%u bytes", EXTRACT_32BITS(tptr)));
225 break;
226 case 0x12: /* AVVID trust bitmap - not documented */
227 if (len < 1)
228 goto trunc;
229 ND_PRINT((ndo, "0x%02x", *(tptr)));
230 break;
231 case 0x13: /* AVVID untrusted port CoS - not documented */
232 if (len < 1)
233 goto trunc;
234 ND_PRINT((ndo, "0x%02x", *(tptr)));
235 break;
236 case 0x14: /* System Name - not documented */
237 ND_PRINT((ndo, "'"));
238 (void)fn_printn(ndo, tptr, len, NULL);
239 ND_PRINT((ndo, "'"));
240 break;
241 case 0x16: /* System Object ID - not documented */
242 if (cdp_print_addr(ndo, tptr, len) < 0)
243 goto trunc;
244 break;
245 case 0x17: /* Physical Location - not documented */
246 if (len < 1)
247 goto trunc;
248 ND_PRINT((ndo, "0x%02x", *(tptr)));
249 if (len > 1) {
250 ND_PRINT((ndo, "/"));
251 (void)fn_printn(ndo, tptr + 1, len - 1, NULL);
253 break;
254 default:
255 print_unknown_data(ndo, tptr, "\n\t ", len);
256 break;
259 tptr = tptr+len;
261 if (ndo->ndo_vflag < 1)
262 ND_PRINT((ndo, ", length %u", caplen));
264 return;
265 trunc:
266 ND_PRINT((ndo, "%s", tstr));
270 * Protocol type values.
272 * PT_NLPID means that the protocol type field contains an OSI NLPID.
274 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
275 * LLC header that specifies that the payload is for that protocol.
277 #define PT_NLPID 1 /* OSI NLPID */
278 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
280 static int
281 cdp_print_addr(netdissect_options *ndo,
282 const u_char * p, int l)
284 int pt, pl, al, num;
285 const u_char *endp = p + l;
286 #ifdef INET6
287 static const u_char prot_ipv6[] = {
288 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
290 #endif
292 ND_TCHECK2(*p, 4);
293 if (p + 4 > endp)
294 goto trunc;
295 num = EXTRACT_32BITS(p);
296 p += 4;
298 while (p < endp && num >= 0) {
299 ND_TCHECK2(*p, 2);
300 if (p + 2 > endp)
301 goto trunc;
302 pt = p[0]; /* type of "protocol" field */
303 pl = p[1]; /* length of "protocol" field */
304 p += 2;
306 ND_TCHECK2(p[pl], 2);
307 if (p + pl + 2 > endp)
308 goto trunc;
309 al = EXTRACT_16BITS(&p[pl]); /* address length */
311 if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
313 * IPv4: protocol type = NLPID, protocol length = 1
314 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
315 * address length = 4
317 p += 3;
319 ND_TCHECK2(*p, 4);
320 if (p + 4 > endp)
321 goto trunc;
322 ND_PRINT((ndo, "IPv4 (%u) %s", num, ipaddr_string(ndo, p)));
323 p += 4;
325 #ifdef INET6
326 else if (pt == PT_IEEE_802_2 && pl == 8 &&
327 memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
329 * IPv6: protocol type = IEEE 802.2 header,
330 * protocol length = 8 (size of LLC+SNAP header),
331 * protocol = LLC+SNAP header with the IPv6
332 * Ethertype, address length = 16
334 p += 10;
335 ND_TCHECK2(*p, al);
336 if (p + al > endp)
337 goto trunc;
339 ND_PRINT((ndo, "IPv6 (%u) %s", num, ip6addr_string(ndo, p)));
340 p += al;
342 #endif
343 else {
345 * Generic case: just print raw data
347 ND_TCHECK2(*p, pl);
348 if (p + pl > endp)
349 goto trunc;
350 ND_PRINT((ndo, "pt=0x%02x, pl=%d, pb=", *(p - 2), pl));
351 while (pl-- > 0)
352 ND_PRINT((ndo, " %02x", *p++));
353 ND_TCHECK2(*p, 2);
354 if (p + 2 > endp)
355 goto trunc;
356 al = (*p << 8) + *(p + 1);
357 ND_PRINT((ndo, ", al=%d, a=", al));
358 p += 2;
359 ND_TCHECK2(*p, al);
360 if (p + al > endp)
361 goto trunc;
362 while (al-- > 0)
363 ND_PRINT((ndo, " %02x", *p++));
365 num--;
366 if (num)
367 ND_PRINT((ndo, " "));
370 return 0;
372 trunc:
373 return -1;
377 static int
378 cdp_print_prefixes(netdissect_options *ndo,
379 const u_char * p, int l)
381 if (l % 5)
382 goto trunc;
384 ND_PRINT((ndo, " IPv4 Prefixes (%d):", l / 5));
386 while (l > 0) {
387 ND_PRINT((ndo, " %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]));
388 l -= 5;
389 p += 5;
392 return 0;
394 trunc:
395 return -1;
398 /* read in a <n>-byte number, MSB first
399 * (of course this can handle max sizeof(long))
401 static unsigned long cdp_get_number(const u_char * p, int l)
403 unsigned long res=0;
404 while( l>0 )
406 res = (res<<8) + *p;
407 p++; l--;
409 return res;