print-cdp.c revision 1.6 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos *
21 1.1 christos * Code by Gert Doering, SpaceNet GmbH, gert (at) space.net
22 1.1 christos *
23 1.1 christos * Reference documentation:
24 1.1 christos * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
25 1.1 christos */
26 1.1 christos
27 1.2 christos #include <sys/cdefs.h>
28 1.1 christos #ifndef lint
29 1.6 christos __RCSID("$NetBSD: print-cdp.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
30 1.1 christos #endif
31 1.1 christos
32 1.5 christos #define NETDISSECT_REWORKED
33 1.1 christos #ifdef HAVE_CONFIG_H
34 1.1 christos #include "config.h"
35 1.1 christos #endif
36 1.1 christos
37 1.1 christos #include <tcpdump-stdinc.h>
38 1.1 christos
39 1.1 christos #include <string.h>
40 1.1 christos
41 1.1 christos #include "interface.h"
42 1.1 christos #include "addrtoname.h"
43 1.1 christos #include "extract.h" /* must come after interface.h */
44 1.1 christos #include "nlpid.h"
45 1.1 christos
46 1.5 christos static const char tstr[] = "[|cdp]";
47 1.5 christos
48 1.6 christos #define CDP_HEADER_LEN 4
49 1.6 christos #define CDP_HEADER_VERSION_OFFSET 0
50 1.6 christos #define CDP_HEADER_TTL_OFFSET 1
51 1.6 christos #define CDP_HEADER_CHECKSUM_OFFSET 2
52 1.6 christos
53 1.6 christos #define CDP_TLV_HEADER_LEN 4
54 1.6 christos #define CDP_TLV_TYPE_OFFSET 0
55 1.6 christos #define CDP_TLV_LEN_OFFSET 2
56 1.1 christos
57 1.4 christos static const struct tok cdp_tlv_values[] = {
58 1.1 christos { 0x01, "Device-ID"},
59 1.1 christos { 0x02, "Address"},
60 1.1 christos { 0x03, "Port-ID"},
61 1.1 christos { 0x04, "Capability"},
62 1.1 christos { 0x05, "Version String"},
63 1.1 christos { 0x06, "Platform"},
64 1.1 christos { 0x07, "Prefixes"},
65 1.1 christos { 0x08, "Protocol-Hello option"},
66 1.1 christos { 0x09, "VTP Management Domain"},
67 1.1 christos { 0x0a, "Native VLAN ID"},
68 1.1 christos { 0x0b, "Duplex"},
69 1.1 christos { 0x0e, "ATA-186 VoIP VLAN request"},
70 1.1 christos { 0x0f, "ATA-186 VoIP VLAN assignment"},
71 1.1 christos { 0x10, "power consumption"},
72 1.1 christos { 0x11, "MTU"},
73 1.1 christos { 0x12, "AVVID trust bitmap"},
74 1.1 christos { 0x13, "AVVID untrusted ports CoS"},
75 1.1 christos { 0x14, "System Name"},
76 1.1 christos { 0x15, "System Object ID (not decoded)"},
77 1.1 christos { 0x16, "Management Addresses"},
78 1.1 christos { 0x17, "Physical Location"},
79 1.1 christos { 0, NULL}
80 1.1 christos };
81 1.1 christos
82 1.4 christos static const struct tok cdp_capability_values[] = {
83 1.1 christos { 0x01, "Router" },
84 1.1 christos { 0x02, "Transparent Bridge" },
85 1.1 christos { 0x04, "Source Route Bridge" },
86 1.1 christos { 0x08, "L2 Switch" },
87 1.1 christos { 0x10, "L3 capable" },
88 1.1 christos { 0x20, "IGMP snooping" },
89 1.1 christos { 0x40, "L1 capable" },
90 1.1 christos { 0, NULL }
91 1.1 christos };
92 1.1 christos
93 1.5 christos static int cdp_print_addr(netdissect_options *, const u_char *, int);
94 1.5 christos static int cdp_print_prefixes(netdissect_options *, const u_char *, int);
95 1.1 christos static unsigned long cdp_get_number(const u_char *, int);
96 1.1 christos
97 1.1 christos void
98 1.5 christos cdp_print(netdissect_options *ndo,
99 1.5 christos const u_char *pptr, u_int length, u_int caplen)
100 1.1 christos {
101 1.1 christos int type, len, i, j;
102 1.6 christos const u_char *tptr;
103 1.1 christos
104 1.1 christos if (caplen < CDP_HEADER_LEN) {
105 1.5 christos ND_PRINT((ndo, "%s", tstr));
106 1.1 christos return;
107 1.1 christos }
108 1.1 christos
109 1.6 christos tptr = pptr; /* temporary pointer */
110 1.1 christos
111 1.5 christos ND_TCHECK2(*tptr, CDP_HEADER_LEN);
112 1.6 christos ND_PRINT((ndo, "CDPv%u, ttl: %us", *(tptr + CDP_HEADER_VERSION_OFFSET),
113 1.6 christos *(tptr + CDP_HEADER_TTL_OFFSET)));
114 1.5 christos if (ndo->ndo_vflag)
115 1.6 christos ND_PRINT((ndo, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr+CDP_HEADER_CHECKSUM_OFFSET), length));
116 1.1 christos tptr += CDP_HEADER_LEN;
117 1.1 christos
118 1.1 christos while (tptr < (pptr+length)) {
119 1.6 christos ND_TCHECK2(*tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
120 1.6 christos type = EXTRACT_16BITS(tptr+CDP_TLV_TYPE_OFFSET);
121 1.6 christos len = EXTRACT_16BITS(tptr+CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
122 1.6 christos if (len < CDP_TLV_HEADER_LEN) {
123 1.6 christos if (ndo->ndo_vflag)
124 1.6 christos ND_PRINT((ndo, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
125 1.6 christos tok2str(cdp_tlv_values,"unknown field type", type),
126 1.6 christos type,
127 1.6 christos len,
128 1.6 christos PLURAL_SUFFIX(len))); /* plural */
129 1.6 christos else
130 1.6 christos ND_PRINT((ndo, ", %s TLV length %u too short",
131 1.6 christos tok2str(cdp_tlv_values,"unknown field type", type),
132 1.6 christos len));
133 1.6 christos break;
134 1.6 christos }
135 1.6 christos tptr += CDP_TLV_HEADER_LEN;
136 1.6 christos len -= CDP_TLV_HEADER_LEN;
137 1.1 christos
138 1.5 christos ND_TCHECK2(*tptr, len);
139 1.1 christos
140 1.6 christos if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
141 1.1 christos
142 1.6 christos if (ndo->ndo_vflag)
143 1.6 christos ND_PRINT((ndo, "\n\t%s (0x%02x), value length: %u byte%s: ",
144 1.6 christos tok2str(cdp_tlv_values,"unknown field type", type),
145 1.6 christos type,
146 1.6 christos len,
147 1.6 christos PLURAL_SUFFIX(len))); /* plural */
148 1.6 christos
149 1.6 christos switch (type) {
150 1.6 christos
151 1.6 christos case 0x01: /* Device-ID */
152 1.6 christos if (!ndo->ndo_vflag)
153 1.6 christos ND_PRINT((ndo, ", Device-ID "));
154 1.6 christos ND_PRINT((ndo, "'"));
155 1.6 christos (void)fn_printn(ndo, tptr, len, NULL);
156 1.6 christos ND_PRINT((ndo, "'"));
157 1.6 christos break;
158 1.6 christos case 0x02: /* Address */
159 1.6 christos if (cdp_print_addr(ndo, tptr, len) < 0)
160 1.6 christos goto trunc;
161 1.1 christos break;
162 1.6 christos case 0x03: /* Port-ID */
163 1.6 christos ND_PRINT((ndo, "'"));
164 1.6 christos (void)fn_printn(ndo, tptr, len, NULL);
165 1.6 christos ND_PRINT((ndo, "'"));
166 1.6 christos break;
167 1.6 christos case 0x04: /* Capabilities */
168 1.6 christos if (len < 4)
169 1.6 christos goto trunc;
170 1.5 christos ND_PRINT((ndo, "(0x%08x): %s",
171 1.6 christos EXTRACT_32BITS(tptr),
172 1.6 christos bittok2str(cdp_capability_values, "none", EXTRACT_32BITS(tptr))));
173 1.1 christos break;
174 1.6 christos case 0x05: /* Version */
175 1.6 christos ND_PRINT((ndo, "\n\t "));
176 1.6 christos for (i=0;i<len;i++) {
177 1.6 christos j = *(tptr+i);
178 1.6 christos ND_PRINT((ndo, "%c", j));
179 1.6 christos if (j == 0x0a) /* lets rework the version string to get a nice indentation */
180 1.6 christos ND_PRINT((ndo, "\t "));
181 1.6 christos }
182 1.6 christos break;
183 1.6 christos case 0x06: /* Platform */
184 1.6 christos ND_PRINT((ndo, "'"));
185 1.6 christos (void)fn_printn(ndo, tptr, len, NULL);
186 1.6 christos ND_PRINT((ndo, "'"));
187 1.1 christos break;
188 1.6 christos case 0x07: /* Prefixes */
189 1.5 christos if (cdp_print_prefixes(ndo, tptr, len) < 0)
190 1.6 christos goto trunc;
191 1.1 christos break;
192 1.6 christos case 0x08: /* Protocol Hello Option - not documented */
193 1.1 christos break;
194 1.6 christos case 0x09: /* VTP Mgmt Domain - CDPv2 */
195 1.6 christos ND_PRINT((ndo, "'"));
196 1.6 christos (void)fn_printn(ndo, tptr, len, NULL);
197 1.6 christos ND_PRINT((ndo, "'"));
198 1.6 christos break;
199 1.6 christos case 0x0a: /* Native VLAN ID - CDPv2 */
200 1.6 christos if (len < 2)
201 1.6 christos goto trunc;
202 1.5 christos ND_PRINT((ndo, "%d", EXTRACT_16BITS(tptr)));
203 1.1 christos break;
204 1.6 christos case 0x0b: /* Duplex - CDPv2 */
205 1.6 christos if (len < 1)
206 1.6 christos goto trunc;
207 1.5 christos ND_PRINT((ndo, "%s", *(tptr) ? "full": "half"));
208 1.1 christos break;
209 1.1 christos
210 1.6 christos /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
211 1.6 christos * plus more details from other sources
212 1.6 christos */
213 1.6 christos case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
214 1.6 christos if (len < 3)
215 1.6 christos goto trunc;
216 1.5 christos ND_PRINT((ndo, "app %d, vlan %d", *(tptr), EXTRACT_16BITS(tptr + 1)));
217 1.1 christos break;
218 1.6 christos case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
219 1.5 christos ND_PRINT((ndo, "%1.2fW", cdp_get_number(tptr, len) / 1000.0));
220 1.1 christos break;
221 1.6 christos case 0x11: /* MTU - not documented */
222 1.6 christos if (len < 4)
223 1.6 christos goto trunc;
224 1.5 christos ND_PRINT((ndo, "%u bytes", EXTRACT_32BITS(tptr)));
225 1.1 christos break;
226 1.6 christos case 0x12: /* AVVID trust bitmap - not documented */
227 1.6 christos if (len < 1)
228 1.6 christos goto trunc;
229 1.5 christos ND_PRINT((ndo, "0x%02x", *(tptr)));
230 1.1 christos break;
231 1.6 christos case 0x13: /* AVVID untrusted port CoS - not documented */
232 1.6 christos if (len < 1)
233 1.6 christos goto trunc;
234 1.5 christos ND_PRINT((ndo, "0x%02x", *(tptr)));
235 1.1 christos break;
236 1.6 christos case 0x14: /* System Name - not documented */
237 1.6 christos ND_PRINT((ndo, "'"));
238 1.6 christos (void)fn_printn(ndo, tptr, len, NULL);
239 1.6 christos ND_PRINT((ndo, "'"));
240 1.1 christos break;
241 1.6 christos case 0x16: /* System Object ID - not documented */
242 1.5 christos if (cdp_print_addr(ndo, tptr, len) < 0)
243 1.1 christos goto trunc;
244 1.1 christos break;
245 1.6 christos case 0x17: /* Physical Location - not documented */
246 1.6 christos if (len < 1)
247 1.6 christos goto trunc;
248 1.5 christos ND_PRINT((ndo, "0x%02x", *(tptr)));
249 1.1 christos if (len > 1) {
250 1.5 christos ND_PRINT((ndo, "/"));
251 1.6 christos (void)fn_printn(ndo, tptr + 1, len - 1, NULL);
252 1.6 christos }
253 1.1 christos break;
254 1.6 christos default:
255 1.6 christos print_unknown_data(ndo, tptr, "\n\t ", len);
256 1.1 christos break;
257 1.6 christos }
258 1.6 christos }
259 1.1 christos tptr = tptr+len;
260 1.1 christos }
261 1.6 christos if (ndo->ndo_vflag < 1)
262 1.6 christos ND_PRINT((ndo, ", length %u", caplen));
263 1.1 christos
264 1.1 christos return;
265 1.1 christos trunc:
266 1.5 christos ND_PRINT((ndo, "%s", tstr));
267 1.1 christos }
268 1.1 christos
269 1.1 christos /*
270 1.1 christos * Protocol type values.
271 1.1 christos *
272 1.1 christos * PT_NLPID means that the protocol type field contains an OSI NLPID.
273 1.1 christos *
274 1.1 christos * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
275 1.1 christos * LLC header that specifies that the payload is for that protocol.
276 1.1 christos */
277 1.1 christos #define PT_NLPID 1 /* OSI NLPID */
278 1.1 christos #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
279 1.1 christos
280 1.1 christos static int
281 1.5 christos cdp_print_addr(netdissect_options *ndo,
282 1.6 christos const u_char * p, int l)
283 1.1 christos {
284 1.1 christos int pt, pl, al, num;
285 1.1 christos const u_char *endp = p + l;
286 1.1 christos #ifdef INET6
287 1.5 christos static const u_char prot_ipv6[] = {
288 1.1 christos 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
289 1.1 christos };
290 1.1 christos #endif
291 1.1 christos
292 1.6 christos ND_TCHECK2(*p, 4);
293 1.6 christos if (p + 4 > endp)
294 1.6 christos goto trunc;
295 1.1 christos num = EXTRACT_32BITS(p);
296 1.1 christos p += 4;
297 1.1 christos
298 1.1 christos while (p < endp && num >= 0) {
299 1.5 christos ND_TCHECK2(*p, 2);
300 1.1 christos if (p + 2 > endp)
301 1.1 christos goto trunc;
302 1.1 christos pt = p[0]; /* type of "protocol" field */
303 1.1 christos pl = p[1]; /* length of "protocol" field */
304 1.1 christos p += 2;
305 1.1 christos
306 1.5 christos ND_TCHECK2(p[pl], 2);
307 1.1 christos if (p + pl + 2 > endp)
308 1.1 christos goto trunc;
309 1.1 christos al = EXTRACT_16BITS(&p[pl]); /* address length */
310 1.1 christos
311 1.1 christos if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
312 1.1 christos /*
313 1.1 christos * IPv4: protocol type = NLPID, protocol length = 1
314 1.1 christos * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
315 1.1 christos * address length = 4
316 1.1 christos */
317 1.1 christos p += 3;
318 1.1 christos
319 1.5 christos ND_TCHECK2(*p, 4);
320 1.1 christos if (p + 4 > endp)
321 1.1 christos goto trunc;
322 1.5 christos ND_PRINT((ndo, "IPv4 (%u) %s", num, ipaddr_string(ndo, p)));
323 1.1 christos p += 4;
324 1.1 christos }
325 1.1 christos #ifdef INET6
326 1.1 christos else if (pt == PT_IEEE_802_2 && pl == 8 &&
327 1.1 christos memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
328 1.1 christos /*
329 1.1 christos * IPv6: protocol type = IEEE 802.2 header,
330 1.1 christos * protocol length = 8 (size of LLC+SNAP header),
331 1.1 christos * protocol = LLC+SNAP header with the IPv6
332 1.1 christos * Ethertype, address length = 16
333 1.1 christos */
334 1.1 christos p += 10;
335 1.5 christos ND_TCHECK2(*p, al);
336 1.1 christos if (p + al > endp)
337 1.1 christos goto trunc;
338 1.1 christos
339 1.5 christos ND_PRINT((ndo, "IPv6 (%u) %s", num, ip6addr_string(ndo, p)));
340 1.1 christos p += al;
341 1.1 christos }
342 1.1 christos #endif
343 1.1 christos else {
344 1.1 christos /*
345 1.1 christos * Generic case: just print raw data
346 1.1 christos */
347 1.5 christos ND_TCHECK2(*p, pl);
348 1.1 christos if (p + pl > endp)
349 1.1 christos goto trunc;
350 1.5 christos ND_PRINT((ndo, "pt=0x%02x, pl=%d, pb=", *(p - 2), pl));
351 1.1 christos while (pl-- > 0)
352 1.5 christos ND_PRINT((ndo, " %02x", *p++));
353 1.5 christos ND_TCHECK2(*p, 2);
354 1.1 christos if (p + 2 > endp)
355 1.1 christos goto trunc;
356 1.1 christos al = (*p << 8) + *(p + 1);
357 1.5 christos ND_PRINT((ndo, ", al=%d, a=", al));
358 1.1 christos p += 2;
359 1.5 christos ND_TCHECK2(*p, al);
360 1.1 christos if (p + al > endp)
361 1.1 christos goto trunc;
362 1.1 christos while (al-- > 0)
363 1.5 christos ND_PRINT((ndo, " %02x", *p++));
364 1.1 christos }
365 1.1 christos num--;
366 1.1 christos if (num)
367 1.5 christos ND_PRINT((ndo, " "));
368 1.1 christos }
369 1.1 christos
370 1.1 christos return 0;
371 1.1 christos
372 1.1 christos trunc:
373 1.1 christos return -1;
374 1.1 christos }
375 1.1 christos
376 1.1 christos
377 1.1 christos static int
378 1.5 christos cdp_print_prefixes(netdissect_options *ndo,
379 1.6 christos const u_char * p, int l)
380 1.1 christos {
381 1.1 christos if (l % 5)
382 1.1 christos goto trunc;
383 1.1 christos
384 1.5 christos ND_PRINT((ndo, " IPv4 Prefixes (%d):", l / 5));
385 1.1 christos
386 1.1 christos while (l > 0) {
387 1.5 christos ND_PRINT((ndo, " %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]));
388 1.1 christos l -= 5;
389 1.1 christos p += 5;
390 1.1 christos }
391 1.1 christos
392 1.1 christos return 0;
393 1.1 christos
394 1.1 christos trunc:
395 1.1 christos return -1;
396 1.1 christos }
397 1.1 christos
398 1.1 christos /* read in a <n>-byte number, MSB first
399 1.1 christos * (of course this can handle max sizeof(long))
400 1.1 christos */
401 1.1 christos static unsigned long cdp_get_number(const u_char * p, int l)
402 1.1 christos {
403 1.1 christos unsigned long res=0;
404 1.1 christos while( l>0 )
405 1.1 christos {
406 1.1 christos res = (res<<8) + *p;
407 1.1 christos p++; l--;
408 1.1 christos }
409 1.1 christos return res;
410 1.1 christos }
411