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 */
33 #include "netdissect-stdinc.h"
37 #define ND_LONGJMP_FROM_TCHECK
38 #include "netdissect.h"
39 #include "addrtoname.h"
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
49 #define CDP_TLV_HEADER_LEN 4
50 #define CDP_TLV_TYPE_OFFSET 0
51 #define CDP_TLV_LEN_OFFSET 2
53 static const struct tok cdp_capability_values
[] = {
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" },
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
);
68 cdp_print_string(netdissect_options
*ndo
,
69 const u_char
*cp
, const u_int len
)
72 nd_printjn(ndo
, cp
, len
);
77 cdp_print_power(netdissect_options
*ndo
,
78 const u_char
*cp
, const u_int len
)
93 ND_PRINT("%1.2fW", val
/ 1000.0);
97 cdp_print_capability(netdissect_options
*ndo
,
98 const u_char
*cp
, const u_int len _U_
)
100 uint32_t val
= GET_BE_U_4(cp
);
102 ND_PRINT("(0x%08x): %s", val
,
103 bittok2str(cdp_capability_values
, "none", val
));
106 /* Rework the version string to get a nice indentation. */
108 cdp_print_version(netdissect_options
*ndo
,
109 const u_char
*cp
, const u_int len
)
114 for (i
= 0; i
< len
; i
++) {
115 u_char c
= GET_U_1(cp
+ i
);
120 fn_print_char(ndo
, c
);
125 cdp_print_uint16(netdissect_options
*ndo
,
126 const u_char
*cp
, const u_int len _U_
)
128 ND_PRINT("%u", GET_BE_U_2(cp
));
132 cdp_print_duplex(netdissect_options
*ndo
,
133 const u_char
*cp
, const u_int len _U_
)
135 ND_PRINT("%s", GET_U_1(cp
) ? "full": "half");
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
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).
148 * The replies all appear to be 3 bytes long.
151 cdp_print_ata186(netdissect_options
*ndo
,
152 const u_char
*cp
, const u_int len
)
155 ND_PRINT("unknown 0x%04x", GET_BE_U_2(cp
));
157 ND_PRINT("app %u, vlan %u", GET_U_1(cp
), GET_BE_U_2(cp
+ 1));
161 cdp_print_mtu(netdissect_options
*ndo
,
162 const u_char
*cp
, const u_int len _U_
)
164 ND_PRINT("%u bytes", GET_BE_U_4(cp
));
168 cdp_print_uint8x(netdissect_options
*ndo
,
169 const u_char
*cp
, const u_int len _U_
)
171 ND_PRINT("0x%02x", GET_U_1(cp
));
175 cdp_print_phys_loc(netdissect_options
*ndo
,
176 const u_char
*cp
, const u_int len
)
178 ND_PRINT("0x%02x", GET_U_1(cp
));
181 nd_printjn(ndo
, cp
+ 1, len
- 1);
187 void (*printer
)(netdissect_options
*ndo
, const u_char
*, u_int
);
188 int min_len
, max_len
;
191 #define T_DEV_ID 0x01
193 static const struct cdp_tlvinfo cdptlvs
[T_MAX
+ 1] = {
195 [ T_DEV_ID
] = { "Device-ID", cdp_print_string
, -1, -1 },
196 [ 0x02 ] = { "Address", cdp_print_addr
, -1, -1 },
197 [ 0x03 ] = { "Port-ID", cdp_print_string
, -1, -1 },
198 [ 0x04 ] = { "Capability", cdp_print_capability
, 4, 4 },
199 [ 0x05 ] = { "Version String", cdp_print_version
, -1, -1 },
200 [ 0x06 ] = { "Platform", cdp_print_string
, -1, -1 },
201 [ 0x07 ] = { "Prefixes", cdp_print_prefixes
, -1, -1 },
203 [ 0x08 ] = { "Protocol-Hello option", NULL
, -1, -1 },
205 [ 0x09 ] = { "VTP Management Domain", cdp_print_string
, -1, -1 },
207 [ 0x0a ] = { "Native VLAN ID", cdp_print_uint16
, 2, 2 },
209 [ 0x0b ] = { "Duplex", cdp_print_duplex
, 1, 1 },
212 /* incomplete doc. */
213 [ 0x0e ] = { "ATA-186 VoIP VLAN assignment", cdp_print_ata186
, 3, 3 },
214 /* incomplete doc. */
215 [ 0x0f ] = { "ATA-186 VoIP VLAN request", cdp_print_ata186
, 2, 3 },
217 [ 0x10 ] = { "power consumption", cdp_print_power
, 1, 3 },
219 [ 0x11 ] = { "MTU", cdp_print_mtu
, 4, 4 },
221 [ 0x12 ] = { "AVVID trust bitmap", cdp_print_uint8x
, 1, 1 },
223 [ 0x13 ] = { "AVVID untrusted ports CoS", cdp_print_uint8x
, 1, 1 },
225 [ 0x14 ] = { "System Name", cdp_print_string
, -1, -1 },
227 [ 0x15 ] = { "System Object ID (not decoded)", NULL
, -1, -1 },
228 [ 0x16 ] = { "Management Addresses", cdp_print_addr
, 4, -1 },
230 [ 0x17 ] = { "Physical Location", cdp_print_phys_loc
, 1, -1 },
234 cdp_print(netdissect_options
*ndo
,
235 const u_char
*tptr
, u_int length
)
237 u_int orig_length
= length
;
240 ndo
->ndo_protocol
= "cdp";
242 if (length
< CDP_HEADER_LEN
) {
243 ND_PRINT(" (packet length %u < %u)", length
, CDP_HEADER_LEN
);
246 ND_PRINT("CDPv%u, ttl: %us",
247 GET_U_1(tptr
+ CDP_HEADER_VERSION_OFFSET
),
248 GET_U_1(tptr
+ CDP_HEADER_TTL_OFFSET
));
249 checksum
= GET_BE_U_2(tptr
+ CDP_HEADER_CHECKSUM_OFFSET
);
251 ND_PRINT(", checksum: 0x%04x (unverified), length %u",
252 checksum
, orig_length
);
253 tptr
+= CDP_HEADER_LEN
;
254 length
-= CDP_HEADER_LEN
;
258 const struct cdp_tlvinfo
*info
;
262 if (length
< CDP_TLV_HEADER_LEN
) {
263 ND_PRINT(" (remaining packet length %u < %u)",
264 length
, CDP_TLV_HEADER_LEN
);
267 type
= GET_BE_U_2(tptr
+ CDP_TLV_TYPE_OFFSET
);
268 len
= GET_BE_U_2(tptr
+ CDP_TLV_LEN_OFFSET
); /* object length includes the 4 bytes header length */
269 info
= type
<= T_MAX
? &cdptlvs
[type
] : NULL
;
270 name
= (info
&& info
->name
) ? info
->name
: "unknown field type";
271 if (len
< CDP_TLV_HEADER_LEN
) {
273 ND_PRINT("\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
274 name
, type
, len
, PLURAL_SUFFIX(len
));
276 ND_PRINT(", %s TLV length %u too short",
281 ND_PRINT(" (TLV length %u > %u)", len
, length
);
284 tptr
+= CDP_TLV_HEADER_LEN
;
285 length
-= CDP_TLV_HEADER_LEN
;
286 len
-= CDP_TLV_HEADER_LEN
;
288 /* In non-verbose mode just print Device-ID. */
289 if (!ndo
->ndo_vflag
&& type
== T_DEV_ID
)
290 ND_PRINT(", Device-ID ");
291 else if (ndo
->ndo_vflag
)
292 ND_PRINT("\n\t%s (0x%02x), value length: %u byte%s: ",
293 name
, type
, len
, PLURAL_SUFFIX(len
));
296 if ((info
->min_len
> 0 && len
< (unsigned)info
->min_len
) ||
297 (info
->max_len
> 0 && len
> (unsigned)info
->max_len
))
298 ND_PRINT(" (malformed TLV)");
299 else if (ndo
->ndo_vflag
|| type
== T_DEV_ID
) {
301 info
->printer(ndo
, tptr
, len
);
303 ND_TCHECK_LEN(tptr
, len
);
305 * When the type is defined without a printer,
306 * do not print the hex dump.
312 if (ndo
->ndo_vflag
&& !covered
) {
313 ND_TCHECK_LEN(tptr
, len
);
314 print_unknown_data(ndo
, tptr
, "\n\t ", len
);
319 if (ndo
->ndo_vflag
< 1)
320 ND_PRINT(", length %u", orig_length
);
324 nd_print_invalid(ndo
);
325 ND_TCHECK_LEN(tptr
, length
);
329 * Protocol type values.
331 * PT_NLPID means that the protocol type field contains an OSI NLPID.
333 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
334 * LLC header that specifies that the payload is for that protocol.
336 #define PT_NLPID 1 /* OSI NLPID */
337 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
340 cdp_print_addr(netdissect_options
*ndo
,
341 const u_char
* p
, u_int l
)
344 static const u_char prot_ipv6
[] = {
345 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
349 ND_PRINT(" (not enough space for num)");
360 ND_PRINT(" (not enough space for PT+PL)");
363 pt
= GET_U_1(p
); /* type of "protocol" field */
364 pl
= GET_U_1(p
+ 1); /* length of "protocol" field */
369 ND_PRINT(" (not enough space for P+AL)");
372 /* Skip the protocol for now. */
373 al
= GET_BE_U_2(p
+ pl
); /* address length */
375 if (pt
== PT_NLPID
&& pl
== 1 && GET_U_1(p
) == NLPID_IP
&&
378 * IPv4: protocol type = NLPID, protocol length = 1
379 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
384 /* p is just beyond al now. */
386 ND_PRINT(" (not enough space for A)");
389 ND_PRINT("IPv4 (%u) %s", num
, GET_IPADDR_STRING(p
));
393 else if (pt
== PT_IEEE_802_2
&& pl
== 8 &&
394 memcmp(p
, prot_ipv6
, 8) == 0 && al
== 16) {
396 * IPv6: protocol type = IEEE 802.2 header,
397 * protocol length = 8 (size of LLC+SNAP header),
398 * protocol = LLC+SNAP header with the IPv6
399 * Ethertype, address length = 16
403 /* p is just beyond al now. */
405 ND_PRINT(" (not enough space for A)");
408 ND_PRINT("IPv6 (%u) %s", num
, GET_IP6ADDR_STRING(p
));
414 * Generic case: just print raw data
416 ND_PRINT("pt=0x%02x, pl=%u, pb=", pt
, pl
);
418 ND_PRINT(" %02x", GET_U_1(p
));
423 ND_PRINT(", al=%u, a=", al
);
426 /* p is just beyond al now. */
428 ND_PRINT(" (not enough space for A)");
432 ND_PRINT(" %02x", GET_U_1(p
));
443 ND_PRINT(" (%u bytes of stray data)", l
);
451 cdp_print_prefixes(netdissect_options
*ndo
,
452 const u_char
* p
, u_int l
)
455 ND_PRINT(" [length %u is not a multiple of 5]", l
);
459 ND_PRINT(" IPv4 Prefixes (%u):", l
/ 5);
462 ND_PRINT(" %u.%u.%u.%u/%u",
463 GET_U_1(p
), GET_U_1(p
+ 1), GET_U_1(p
+ 2),
464 GET_U_1(p
+ 3), GET_U_1(p
+ 4));