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
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 * https://web.archive.org/web/20000914194913/http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.pdf
27 /* \summary: Cisco Discovery Protocol (CDP) printer */
31 #include "netdissect-stdinc.h"
35 #define ND_LONGJMP_FROM_TCHECK
36 #include "netdissect.h"
37 #include "addrtoname.h"
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
47 #define CDP_TLV_HEADER_LEN 4
48 #define CDP_TLV_TYPE_OFFSET 0
49 #define CDP_TLV_LEN_OFFSET 2
51 static const struct tok cdp_capability_values
[] = {
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" },
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
);
66 cdp_print_string(netdissect_options
*ndo
,
67 const u_char
*cp
, const u_int len
)
70 nd_printjn(ndo
, cp
, len
);
75 cdp_print_power(netdissect_options
*ndo
,
76 const u_char
*cp
, const u_int len
)
91 ND_PRINT("%1.2fW", val
/ 1000.0);
95 cdp_print_capability(netdissect_options
*ndo
,
96 const u_char
*cp
, const u_int len _U_
)
98 uint32_t val
= GET_BE_U_4(cp
);
100 ND_PRINT("(0x%08x): %s", val
,
101 bittok2str(cdp_capability_values
, "none", val
));
104 /* Rework the version string to get a nice indentation. */
106 cdp_print_version(netdissect_options
*ndo
,
107 const u_char
*cp
, const u_int len
)
112 for (i
= 0; i
< len
; i
++) {
113 u_char c
= GET_U_1(cp
+ i
);
118 fn_print_char(ndo
, c
);
123 cdp_print_uint16(netdissect_options
*ndo
,
124 const u_char
*cp
, const u_int len _U_
)
126 ND_PRINT("%u", GET_BE_U_2(cp
));
130 cdp_print_duplex(netdissect_options
*ndo
,
131 const u_char
*cp
, const u_int len _U_
)
133 ND_PRINT("%s", GET_U_1(cp
) ? "full": "half");
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
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).
146 * The replies all appear to be 3 bytes long.
149 cdp_print_ata186(netdissect_options
*ndo
,
150 const u_char
*cp
, const u_int len
)
153 ND_PRINT("unknown 0x%04x", GET_BE_U_2(cp
));
155 ND_PRINT("app %u, vlan %u", GET_U_1(cp
), GET_BE_U_2(cp
+ 1));
159 cdp_print_mtu(netdissect_options
*ndo
,
160 const u_char
*cp
, const u_int len _U_
)
162 ND_PRINT("%u bytes", GET_BE_U_4(cp
));
166 cdp_print_uint8x(netdissect_options
*ndo
,
167 const u_char
*cp
, const u_int len _U_
)
169 ND_PRINT("0x%02x", GET_U_1(cp
));
173 cdp_print_phys_loc(netdissect_options
*ndo
,
174 const u_char
*cp
, const u_int len
)
176 ND_PRINT("0x%02x", GET_U_1(cp
));
179 nd_printjn(ndo
, cp
+ 1, len
- 1);
184 cdp_print_power_avail(netdissect_options
*ndo
,
185 const u_char
*cp
, const u_int len
)
187 ND_PRINT("reqid %u, mgmtid %u", GET_BE_U_2(cp
), GET_BE_U_2(cp
+ 2));
189 const u_char
*powerp
;
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
));
200 VERBOSE_OR_NOT_VERBOSE
,
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
;
211 static const struct cdp_tlvinfo cdptlvs
[] = {
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 },
221 [ 0x08 ] = { "Protocol-Hello option", NULL
, VERBOSE_ONLY
, -1, -1 },
223 [ 0x09 ] = { "VTP Management Domain", cdp_print_string
, VERBOSE_ONLY
, -1, -1 },
225 [ 0x0a ] = { "Native VLAN ID", cdp_print_uint16
, VERBOSE_ONLY
, 2, 2 },
227 [ 0x0b ] = { "Duplex", cdp_print_duplex
, VERBOSE_ONLY
, 1, 1 },
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 },
235 [ 0x10 ] = { "power consumption", cdp_print_power
, VERBOSE_ONLY
, 1, 3 },
237 [ 0x11 ] = { "MTU", cdp_print_mtu
, VERBOSE_ONLY
, 4, 4 },
239 [ 0x12 ] = { "AVVID trust bitmap", cdp_print_uint8x
, VERBOSE_ONLY
, 1, 1 },
241 [ 0x13 ] = { "AVVID untrusted ports CoS", cdp_print_uint8x
, VERBOSE_ONLY
, 1, 1 },
243 [ 0x14 ] = { "System Name", cdp_print_string
, VERBOSE_ONLY
, -1, -1 },
245 [ 0x15 ] = { "System Object ID (not decoded)", NULL
, VERBOSE_ONLY
, -1, -1 },
246 [ 0x16 ] = { "Management Addresses", cdp_print_addr
, VERBOSE_ONLY
, 4, -1 },
248 [ 0x17 ] = { "Physical Location", cdp_print_phys_loc
, VERBOSE_ONLY
, 1, -1 },
250 [ 0x1a ] = { "Power available", cdp_print_power_avail
, VERBOSE_ONLY
, 8, -1 },
253 #define T_MAX (sizeof cdptlvs / sizeof cdptlvs[0])
256 cdp_print(netdissect_options
*ndo
,
257 const u_char
*tptr
, u_int length
)
259 u_int orig_length
= length
;
262 ndo
->ndo_protocol
= "cdp";
264 if (length
< CDP_HEADER_LEN
) {
265 ND_PRINT(" (packet length %u < %u)", length
, CDP_HEADER_LEN
);
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
);
273 ND_PRINT(", checksum: 0x%04x (unverified), length %u",
274 checksum
, orig_length
);
275 tptr
+= CDP_HEADER_LEN
;
276 length
-= CDP_HEADER_LEN
;
280 const struct cdp_tlvinfo
*info
;
282 int print_if_not_verbose
;
285 if (length
< CDP_TLV_HEADER_LEN
) {
286 ND_PRINT(" (remaining packet length %u < %u)",
287 length
, CDP_TLV_HEADER_LEN
);
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
) {
298 ND_PRINT("\n\t%s (0x%04x), TLV length: %u byte%s (too short)",
299 name
, type
, len
, PLURAL_SUFFIX(len
));
301 ND_PRINT(", %s TLV length %u too short",
306 ND_PRINT(" (TLV length %u > %u)", len
, length
);
309 tptr
+= CDP_TLV_HEADER_LEN
;
310 length
-= CDP_TLV_HEADER_LEN
;
311 len
-= CDP_TLV_HEADER_LEN
;
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
));
318 /* Print only some TLVs when not in verbose mode */
319 if (print_if_not_verbose
)
320 ND_PRINT(", %s ", name
);
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
) {
329 info
->printer(ndo
, tptr
, len
);
331 ND_TCHECK_LEN(tptr
, len
);
333 * When the type is defined without a printer,
334 * do not print the hex dump.
340 if (ndo
->ndo_vflag
&& !covered
) {
341 ND_TCHECK_LEN(tptr
, len
);
342 print_unknown_data(ndo
, tptr
, "\n\t ", len
);
347 if (ndo
->ndo_vflag
< 1)
348 ND_PRINT(", length %u", orig_length
);
352 nd_print_invalid(ndo
);
353 ND_TCHECK_LEN(tptr
, length
);
357 * Protocol type values.
359 * PT_NLPID means that the protocol type field contains an OSI NLPID.
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.
364 #define PT_NLPID 1 /* OSI NLPID */
365 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
368 cdp_print_addr(netdissect_options
*ndo
,
369 const u_char
* p
, u_int l
)
372 static const u_char prot_ipv6
[] = {
373 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
384 ND_PRINT(" (not enough space for PT+PL)");
387 pt
= GET_U_1(p
); /* type of "protocol" field */
388 pl
= GET_U_1(p
+ 1); /* length of "protocol" field */
393 ND_PRINT(" (not enough space for P+AL)");
396 /* Skip the protocol for now. */
397 al
= GET_BE_U_2(p
+ pl
); /* address length */
399 if (pt
== PT_NLPID
&& pl
== 1 && GET_U_1(p
) == NLPID_IP
&&
402 * IPv4: protocol type = NLPID, protocol length = 1
403 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
408 /* p is just beyond al now. */
410 ND_PRINT(" (not enough space for A)");
413 ND_PRINT("IPv4 (%u) %s", num
, GET_IPADDR_STRING(p
));
416 } else if (pt
== PT_IEEE_802_2
&& pl
== 8 &&
417 memcmp(p
, prot_ipv6
, 8) == 0 && al
== 16) {
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
426 /* p is just beyond al now. */
428 ND_PRINT(" (not enough space for A)");
431 ND_PRINT("IPv6 (%u) %s", num
, GET_IP6ADDR_STRING(p
));
436 * Generic case: just print raw data
438 ND_PRINT("pt=0x%02x, pl=%u, pb=", pt
, pl
);
440 ND_PRINT(" %02x", GET_U_1(p
));
445 ND_PRINT(", al=%u, a=", al
);
448 /* p is just beyond al now. */
450 ND_PRINT(" (not enough space for A)");
454 ND_PRINT(" %02x", GET_U_1(p
));
465 ND_PRINT(" (%u bytes of stray data)", l
);
473 cdp_print_prefixes(netdissect_options
*ndo
,
474 const u_char
* p
, u_int l
)
477 ND_PRINT(" [length %u is not a multiple of 5]", l
);
481 ND_PRINT(" IPv4 Prefixes (%u):", l
/ 5);
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));