]> The Tcpdump Group git mirrors - tcpdump/blob - print-cdp.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[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 * https://web.archive.org/web/20000914194913/http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.pdf
25 */
26
27 /* \summary: Cisco Discovery Protocol (CDP) printer */
28
29 #include <config.h>
30
31 #include "netdissect-stdinc.h"
32
33 #include <string.h>
34
35 #define ND_LONGJMP_FROM_TCHECK
36 #include "netdissect.h"
37 #include "addrtoname.h"
38 #include "extract.h"
39 #include "nlpid.h"
40
41
42 #define CDP_HEADER_LEN 4
43 #define CDP_HEADER_VERSION_OFFSET 0
44 #define CDP_HEADER_TTL_OFFSET 1
45 #define CDP_HEADER_CHECKSUM_OFFSET 2
46
47 #define CDP_TLV_HEADER_LEN 4
48 #define CDP_TLV_TYPE_OFFSET 0
49 #define CDP_TLV_LEN_OFFSET 2
50
51 static const struct tok cdp_capability_values[] = {
52 { 0x01, "Router" },
53 { 0x02, "Transparent Bridge" },
54 { 0x04, "Source Route Bridge" },
55 { 0x08, "L2 Switch" },
56 { 0x10, "L3 capable" },
57 { 0x20, "IGMP snooping" },
58 { 0x40, "L1 capable" },
59 { 0, NULL }
60 };
61
62 static void cdp_print_addr(netdissect_options *, const u_char *, u_int);
63 static void cdp_print_prefixes(netdissect_options *, const u_char *, u_int);
64
65 static void
66 cdp_print_string(netdissect_options *ndo,
67 const u_char *cp, const u_int len)
68 {
69 ND_PRINT("'");
70 nd_printjn(ndo, cp, len);
71 ND_PRINT("'");
72 }
73
74 static void
75 cdp_print_power(netdissect_options *ndo,
76 const u_char *cp, const u_int len)
77 {
78 u_int val = 0;
79
80 switch (len) {
81 case 1:
82 val = GET_U_1(cp);
83 break;
84 case 2:
85 val = GET_BE_U_2(cp);
86 break;
87 case 3:
88 val = GET_BE_U_3(cp);
89 break;
90 }
91 ND_PRINT("%1.2fW", val / 1000.0);
92 }
93
94 static void
95 cdp_print_capability(netdissect_options *ndo,
96 const u_char *cp, const u_int len _U_)
97 {
98 uint32_t val = GET_BE_U_4(cp);
99
100 ND_PRINT("(0x%08x): %s", val,
101 bittok2str(cdp_capability_values, "none", val));
102 }
103
104 /* Rework the version string to get a nice indentation. */
105 static void
106 cdp_print_version(netdissect_options *ndo,
107 const u_char *cp, const u_int len)
108 {
109 unsigned i;
110
111 ND_PRINT("\n\t ");
112 for (i = 0; i < len; i++) {
113 u_char c = GET_U_1(cp + i);
114
115 if (c == '\n')
116 ND_PRINT("\n\t ");
117 else
118 fn_print_char(ndo, c);
119 }
120 }
121
122 static void
123 cdp_print_uint16(netdissect_options *ndo,
124 const u_char *cp, const u_int len _U_)
125 {
126 ND_PRINT("%u", GET_BE_U_2(cp));
127 }
128
129 static void
130 cdp_print_duplex(netdissect_options *ndo,
131 const u_char *cp, const u_int len _U_)
132 {
133 ND_PRINT("%s", GET_U_1(cp) ? "full": "half");
134 }
135
136 /* https://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
137 * plus more details from other sources
138 *
139 * There are apparently versions of the request with both
140 * 2 bytes and 3 bytes of value. The 3 bytes of value
141 * appear to be a 1-byte application type followed by a
142 * 2-byte VLAN ID; the 2 bytes of value are unknown
143 * (they're 0x20 0x00 in some captures I've seen; that
144 * is not a valid VLAN ID, as VLAN IDs are 12 bits).
145 *
146 * The replies all appear to be 3 bytes long.
147 */
148 static void
149 cdp_print_ata186(netdissect_options *ndo,
150 const u_char *cp, const u_int len)
151 {
152 if (len == 2)
153 ND_PRINT("unknown 0x%04x", GET_BE_U_2(cp));
154 else
155 ND_PRINT("app %u, vlan %u", GET_U_1(cp), GET_BE_U_2(cp + 1));
156 }
157
158 static void
159 cdp_print_mtu(netdissect_options *ndo,
160 const u_char *cp, const u_int len _U_)
161 {
162 ND_PRINT("%u bytes", GET_BE_U_4(cp));
163 }
164
165 static void
166 cdp_print_uint8x(netdissect_options *ndo,
167 const u_char *cp, const u_int len _U_)
168 {
169 ND_PRINT("0x%02x", GET_U_1(cp));
170 }
171
172 static void
173 cdp_print_phys_loc(netdissect_options *ndo,
174 const u_char *cp, const u_int len)
175 {
176 ND_PRINT("0x%02x", GET_U_1(cp));
177 if (len > 1) {
178 ND_PRINT("/");
179 nd_printjn(ndo, cp + 1, len - 1);
180 }
181 }
182
183 static void
184 cdp_print_power_avail(netdissect_options *ndo,
185 const u_char *cp, const u_int len)
186 {
187 ND_PRINT("reqid %u, mgmtid %u", GET_BE_U_2(cp), GET_BE_U_2(cp + 2));
188 if (len > 4) {
189 const u_char *powerp;
190 u_int powerlen;
191
192 ND_PRINT(", pwravail");
193 for (powerp = cp + 4, powerlen = len - 4;
194 powerlen >= 4; powerp += 4, powerlen -= 4)
195 ND_PRINT(" %u mW", GET_BE_U_4(powerp));
196 }
197 }
198
199 typedef enum {
200 VERBOSE_OR_NOT_VERBOSE,
201 VERBOSE_ONLY
202 } when_to_print_t;
203
204 struct cdp_tlvinfo {
205 const char *name;
206 void (*printer)(netdissect_options *ndo, const u_char *, u_int);
207 when_to_print_t when_to_print;
208 int min_len, max_len;
209 };
210
211 static const struct cdp_tlvinfo cdptlvs[] = {
212 /* 0x00 */
213 [ 0x01 ] = { "Device-ID", cdp_print_string, VERBOSE_OR_NOT_VERBOSE, -1, -1 },
214 [ 0x02 ] = { "Address", cdp_print_addr, VERBOSE_ONLY, 4, -1 },
215 [ 0x03 ] = { "Port-ID", cdp_print_string, VERBOSE_ONLY, -1, -1 },
216 [ 0x04 ] = { "Capability", cdp_print_capability, VERBOSE_ONLY, 4, 4 },
217 [ 0x05 ] = { "Version String", cdp_print_version, VERBOSE_ONLY, -1, -1 },
218 [ 0x06 ] = { "Platform", cdp_print_string, VERBOSE_ONLY, -1, -1 },
219 [ 0x07 ] = { "Prefixes", cdp_print_prefixes, VERBOSE_ONLY, -1, -1 },
220 /* not documented */
221 [ 0x08 ] = { "Protocol-Hello option", NULL, VERBOSE_ONLY, -1, -1 },
222 /* CDPv2 */
223 [ 0x09 ] = { "VTP Management Domain", cdp_print_string, VERBOSE_ONLY, -1, -1 },
224 /* CDPv2 */
225 [ 0x0a ] = { "Native VLAN ID", cdp_print_uint16, VERBOSE_ONLY, 2, 2 },
226 /* CDPv2 */
227 [ 0x0b ] = { "Duplex", cdp_print_duplex, VERBOSE_ONLY, 1, 1 },
228 /* 0x0c */
229 /* 0x0d */
230 /* incomplete doc. */
231 [ 0x0e ] = { "ATA-186 VoIP VLAN assignment", cdp_print_ata186, VERBOSE_ONLY, 3, 3 },
232 /* incomplete doc. */
233 [ 0x0f ] = { "ATA-186 VoIP VLAN request", cdp_print_ata186, VERBOSE_ONLY, 2, 3 },
234 /* not documented */
235 [ 0x10 ] = { "power consumption", cdp_print_power, VERBOSE_ONLY, 1, 3 },
236 /* not documented */
237 [ 0x11 ] = { "MTU", cdp_print_mtu, VERBOSE_ONLY, 4, 4 },
238 /* not documented */
239 [ 0x12 ] = { "AVVID trust bitmap", cdp_print_uint8x, VERBOSE_ONLY, 1, 1 },
240 /* not documented */
241 [ 0x13 ] = { "AVVID untrusted ports CoS", cdp_print_uint8x, VERBOSE_ONLY, 1, 1 },
242 /* not documented */
243 [ 0x14 ] = { "System Name", cdp_print_string, VERBOSE_ONLY, -1, -1 },
244 /* not documented */
245 [ 0x15 ] = { "System Object ID (not decoded)", NULL, VERBOSE_ONLY, -1, -1 },
246 [ 0x16 ] = { "Management Addresses", cdp_print_addr, VERBOSE_ONLY, 4, -1 },
247 /* not documented */
248 [ 0x17 ] = { "Physical Location", cdp_print_phys_loc, VERBOSE_ONLY, 1, -1 },
249 /* not documented */
250 [ 0x1a ] = { "Power available", cdp_print_power_avail, VERBOSE_ONLY, 8, -1 },
251 };
252
253 #define T_MAX (sizeof cdptlvs / sizeof cdptlvs[0])
254
255 void
256 cdp_print(netdissect_options *ndo,
257 const u_char *tptr, u_int length)
258 {
259 u_int orig_length = length;
260 uint16_t checksum;
261
262 ndo->ndo_protocol = "cdp";
263
264 if (length < CDP_HEADER_LEN) {
265 ND_PRINT(" (packet length %u < %u)", length, CDP_HEADER_LEN);
266 goto invalid;
267 }
268 ND_PRINT("CDPv%u, ttl: %us",
269 GET_U_1(tptr + CDP_HEADER_VERSION_OFFSET),
270 GET_U_1(tptr + CDP_HEADER_TTL_OFFSET));
271 checksum = GET_BE_U_2(tptr + CDP_HEADER_CHECKSUM_OFFSET);
272 if (ndo->ndo_vflag)
273 ND_PRINT(", checksum: 0x%04x (unverified), length %u",
274 checksum, orig_length);
275 tptr += CDP_HEADER_LEN;
276 length -= CDP_HEADER_LEN;
277
278 while (length) {
279 u_int type, len;
280 const struct cdp_tlvinfo *info;
281 const char *name;
282 int print_if_not_verbose;
283 int covered = 0;
284
285 if (length < CDP_TLV_HEADER_LEN) {
286 ND_PRINT(" (remaining packet length %u < %u)",
287 length, CDP_TLV_HEADER_LEN);
288 goto invalid;
289 }
290 type = GET_BE_U_2(tptr + CDP_TLV_TYPE_OFFSET);
291 len = GET_BE_U_2(tptr + CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
292 info = type < T_MAX ? &cdptlvs[type] : NULL;
293 name = (info && info->name) ? info->name : "unknown field type";
294 print_if_not_verbose =
295 (info ? (info->when_to_print == VERBOSE_OR_NOT_VERBOSE) : 0);
296 if (len < CDP_TLV_HEADER_LEN) {
297 if (ndo->ndo_vflag)
298 ND_PRINT("\n\t%s (0x%04x), TLV length: %u byte%s (too short)",
299 name, type, len, PLURAL_SUFFIX(len));
300 else
301 ND_PRINT(", %s TLV length %u too short",
302 name, len);
303 goto invalid;
304 }
305 if (len > length) {
306 ND_PRINT(" (TLV length %u > %u)", len, length);
307 goto invalid;
308 }
309 tptr += CDP_TLV_HEADER_LEN;
310 length -= CDP_TLV_HEADER_LEN;
311 len -= CDP_TLV_HEADER_LEN;
312
313 if (ndo->ndo_vflag) {
314 /* Print all TLVs when in verbose mode */
315 ND_PRINT("\n\t%s (0x%04x), value length: %u byte%s: ",
316 name, type, len, PLURAL_SUFFIX(len));
317 } else {
318 /* Print only some TLVs when not in verbose mode */
319 if (print_if_not_verbose)
320 ND_PRINT(", %s ", name);
321 }
322
323 if (info) {
324 if ((info->min_len > 0 && len < (unsigned)info->min_len) ||
325 (info->max_len > 0 && len > (unsigned)info->max_len))
326 ND_PRINT(" (malformed TLV)");
327 else if (ndo->ndo_vflag || print_if_not_verbose) {
328 if (info->printer)
329 info->printer(ndo, tptr, len);
330 else
331 ND_TCHECK_LEN(tptr, len);
332 /*
333 * When the type is defined without a printer,
334 * do not print the hex dump.
335 */
336 covered = 1;
337 }
338 }
339
340 if (ndo->ndo_vflag && !covered) {
341 ND_TCHECK_LEN(tptr, len);
342 print_unknown_data(ndo, tptr, "\n\t ", len);
343 }
344 tptr += len;
345 length -= len;
346 }
347 if (ndo->ndo_vflag < 1)
348 ND_PRINT(", length %u", orig_length);
349
350 return;
351 invalid:
352 nd_print_invalid(ndo);
353 ND_TCHECK_LEN(tptr, length);
354 }
355
356 /*
357 * Protocol type values.
358 *
359 * PT_NLPID means that the protocol type field contains an OSI NLPID.
360 *
361 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
362 * LLC header that specifies that the payload is for that protocol.
363 */
364 #define PT_NLPID 1 /* OSI NLPID */
365 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
366
367 static void
368 cdp_print_addr(netdissect_options *ndo,
369 const u_char * p, u_int l)
370 {
371 u_int num;
372 static const u_char prot_ipv6[] = {
373 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
374 };
375
376 num = GET_BE_U_4(p);
377 p += 4;
378 l -= 4;
379
380 while (num) {
381 u_int pt, pl, al;
382
383 if (l < 2) {
384 ND_PRINT(" (not enough space for PT+PL)");
385 goto invalid;
386 }
387 pt = GET_U_1(p); /* type of "protocol" field */
388 pl = GET_U_1(p + 1); /* length of "protocol" field */
389 p += 2;
390 l -= 2;
391
392 if (l < pl + 2) {
393 ND_PRINT(" (not enough space for P+AL)");
394 goto invalid;
395 }
396 /* Skip the protocol for now. */
397 al = GET_BE_U_2(p + pl); /* address length */
398
399 if (pt == PT_NLPID && pl == 1 && GET_U_1(p) == NLPID_IP &&
400 al == 4) {
401 /*
402 * IPv4: protocol type = NLPID, protocol length = 1
403 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
404 * address length = 4
405 */
406 p += pl + 2;
407 l -= pl + 2;
408 /* p is just beyond al now. */
409 if (l < al) {
410 ND_PRINT(" (not enough space for A)");
411 goto invalid;
412 }
413 ND_PRINT("IPv4 (%u) %s", num, GET_IPADDR_STRING(p));
414 p += al;
415 l -= al;
416 } else if (pt == PT_IEEE_802_2 && pl == 8 &&
417 memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
418 /*
419 * IPv6: protocol type = IEEE 802.2 header,
420 * protocol length = 8 (size of LLC+SNAP header),
421 * protocol = LLC+SNAP header with the IPv6
422 * Ethertype, address length = 16
423 */
424 p += pl + 2;
425 l -= pl + 2;
426 /* p is just beyond al now. */
427 if (l < al) {
428 ND_PRINT(" (not enough space for A)");
429 goto invalid;
430 }
431 ND_PRINT("IPv6 (%u) %s", num, GET_IP6ADDR_STRING(p));
432 p += al;
433 l -= al;
434 } else {
435 /*
436 * Generic case: just print raw data
437 */
438 ND_PRINT("pt=0x%02x, pl=%u, pb=", pt, pl);
439 while (pl != 0) {
440 ND_PRINT(" %02x", GET_U_1(p));
441 p++;
442 l--;
443 pl--;
444 }
445 ND_PRINT(", al=%u, a=", al);
446 p += 2;
447 l -= 2;
448 /* p is just beyond al now. */
449 if (l < al) {
450 ND_PRINT(" (not enough space for A)");
451 goto invalid;
452 }
453 while (al != 0) {
454 ND_PRINT(" %02x", GET_U_1(p));
455 p++;
456 l--;
457 al--;
458 }
459 }
460 num--;
461 if (num)
462 ND_PRINT(" ");
463 }
464 if (l)
465 ND_PRINT(" (%u bytes of stray data)", l);
466 return;
467
468 invalid:
469 ND_TCHECK_LEN(p, l);
470 }
471
472 static void
473 cdp_print_prefixes(netdissect_options *ndo,
474 const u_char * p, u_int l)
475 {
476 if (l % 5) {
477 ND_PRINT(" [length %u is not a multiple of 5]", l);
478 goto invalid;
479 }
480
481 ND_PRINT(" IPv4 Prefixes (%u):", l / 5);
482
483 while (l != 0) {
484 ND_PRINT(" %u.%u.%u.%u/%u",
485 GET_U_1(p), GET_U_1(p + 1), GET_U_1(p + 2),
486 GET_U_1(p + 3), GET_U_1(p + 4));
487 l -= 5;
488 p += 5;
489 }
490 return;
491
492 invalid:
493 ND_TCHECK_LEN(p, l);
494 }