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