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 * http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
27 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: print-cdp.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
32 #define NETDISSECT_REWORKED
37 #include <tcpdump-stdinc.h>
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
46 static const char tstr
[] = "[|cdp]";
48 #define CDP_HEADER_LEN 4
49 #define CDP_HEADER_VERSION_OFFSET 0
50 #define CDP_HEADER_TTL_OFFSET 1
51 #define CDP_HEADER_CHECKSUM_OFFSET 2
53 #define CDP_TLV_HEADER_LEN 4
54 #define CDP_TLV_TYPE_OFFSET 0
55 #define CDP_TLV_LEN_OFFSET 2
57 static const struct tok cdp_tlv_values
[] = {
61 { 0x04, "Capability"},
62 { 0x05, "Version String"},
65 { 0x08, "Protocol-Hello option"},
66 { 0x09, "VTP Management Domain"},
67 { 0x0a, "Native VLAN ID"},
69 { 0x0e, "ATA-186 VoIP VLAN request"},
70 { 0x0f, "ATA-186 VoIP VLAN assignment"},
71 { 0x10, "power consumption"},
73 { 0x12, "AVVID trust bitmap"},
74 { 0x13, "AVVID untrusted ports CoS"},
75 { 0x14, "System Name"},
76 { 0x15, "System Object ID (not decoded)"},
77 { 0x16, "Management Addresses"},
78 { 0x17, "Physical Location"},
82 static const struct tok cdp_capability_values
[] = {
84 { 0x02, "Transparent Bridge" },
85 { 0x04, "Source Route Bridge" },
86 { 0x08, "L2 Switch" },
87 { 0x10, "L3 capable" },
88 { 0x20, "IGMP snooping" },
89 { 0x40, "L1 capable" },
93 static int cdp_print_addr(netdissect_options
*, const u_char
*, int);
94 static int cdp_print_prefixes(netdissect_options
*, const u_char
*, int);
95 static unsigned long cdp_get_number(const u_char
*, int);
98 cdp_print(netdissect_options
*ndo
,
99 const u_char
*pptr
, u_int length
, u_int caplen
)
104 if (caplen
< CDP_HEADER_LEN
) {
105 ND_PRINT((ndo
, "%s", tstr
));
109 tptr
= pptr
; /* temporary pointer */
111 ND_TCHECK2(*tptr
, CDP_HEADER_LEN
);
112 ND_PRINT((ndo
, "CDPv%u, ttl: %us", *(tptr
+ CDP_HEADER_VERSION_OFFSET
),
113 *(tptr
+ CDP_HEADER_TTL_OFFSET
)));
115 ND_PRINT((ndo
, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr
+CDP_HEADER_CHECKSUM_OFFSET
), length
));
116 tptr
+= CDP_HEADER_LEN
;
118 while (tptr
< (pptr
+length
)) {
119 ND_TCHECK2(*tptr
, CDP_TLV_HEADER_LEN
); /* read out Type and Length */
120 type
= EXTRACT_16BITS(tptr
+CDP_TLV_TYPE_OFFSET
);
121 len
= EXTRACT_16BITS(tptr
+CDP_TLV_LEN_OFFSET
); /* object length includes the 4 bytes header length */
122 if (len
< CDP_TLV_HEADER_LEN
) {
124 ND_PRINT((ndo
, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
125 tok2str(cdp_tlv_values
,"unknown field type", type
),
128 PLURAL_SUFFIX(len
))); /* plural */
130 ND_PRINT((ndo
, ", %s TLV length %u too short",
131 tok2str(cdp_tlv_values
,"unknown field type", type
),
135 tptr
+= CDP_TLV_HEADER_LEN
;
136 len
-= CDP_TLV_HEADER_LEN
;
138 ND_TCHECK2(*tptr
, len
);
140 if (ndo
->ndo_vflag
|| type
== 1) { /* in non-verbose mode just print Device-ID */
143 ND_PRINT((ndo
, "\n\t%s (0x%02x), value length: %u byte%s: ",
144 tok2str(cdp_tlv_values
,"unknown field type", type
),
147 PLURAL_SUFFIX(len
))); /* plural */
151 case 0x01: /* Device-ID */
153 ND_PRINT((ndo
, ", Device-ID "));
154 ND_PRINT((ndo
, "'"));
155 (void)fn_printn(ndo
, tptr
, len
, NULL
);
156 ND_PRINT((ndo
, "'"));
158 case 0x02: /* Address */
159 if (cdp_print_addr(ndo
, tptr
, len
) < 0)
162 case 0x03: /* Port-ID */
163 ND_PRINT((ndo
, "'"));
164 (void)fn_printn(ndo
, tptr
, len
, NULL
);
165 ND_PRINT((ndo
, "'"));
167 case 0x04: /* Capabilities */
170 ND_PRINT((ndo
, "(0x%08x): %s",
171 EXTRACT_32BITS(tptr
),
172 bittok2str(cdp_capability_values
, "none", EXTRACT_32BITS(tptr
))));
174 case 0x05: /* Version */
175 ND_PRINT((ndo
, "\n\t "));
176 for (i
=0;i
<len
;i
++) {
178 ND_PRINT((ndo
, "%c", j
));
179 if (j
== 0x0a) /* lets rework the version string to get a nice indentation */
180 ND_PRINT((ndo
, "\t "));
183 case 0x06: /* Platform */
184 ND_PRINT((ndo
, "'"));
185 (void)fn_printn(ndo
, tptr
, len
, NULL
);
186 ND_PRINT((ndo
, "'"));
188 case 0x07: /* Prefixes */
189 if (cdp_print_prefixes(ndo
, tptr
, len
) < 0)
192 case 0x08: /* Protocol Hello Option - not documented */
194 case 0x09: /* VTP Mgmt Domain - CDPv2 */
195 ND_PRINT((ndo
, "'"));
196 (void)fn_printn(ndo
, tptr
, len
, NULL
);
197 ND_PRINT((ndo
, "'"));
199 case 0x0a: /* Native VLAN ID - CDPv2 */
202 ND_PRINT((ndo
, "%d", EXTRACT_16BITS(tptr
)));
204 case 0x0b: /* Duplex - CDPv2 */
207 ND_PRINT((ndo
, "%s", *(tptr
) ? "full": "half"));
210 /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
211 * plus more details from other sources
213 case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
216 ND_PRINT((ndo
, "app %d, vlan %d", *(tptr
), EXTRACT_16BITS(tptr
+ 1)));
218 case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
219 ND_PRINT((ndo
, "%1.2fW", cdp_get_number(tptr
, len
) / 1000.0));
221 case 0x11: /* MTU - not documented */
224 ND_PRINT((ndo
, "%u bytes", EXTRACT_32BITS(tptr
)));
226 case 0x12: /* AVVID trust bitmap - not documented */
229 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
231 case 0x13: /* AVVID untrusted port CoS - not documented */
234 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
236 case 0x14: /* System Name - not documented */
237 ND_PRINT((ndo
, "'"));
238 (void)fn_printn(ndo
, tptr
, len
, NULL
);
239 ND_PRINT((ndo
, "'"));
241 case 0x16: /* System Object ID - not documented */
242 if (cdp_print_addr(ndo
, tptr
, len
) < 0)
245 case 0x17: /* Physical Location - not documented */
248 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
250 ND_PRINT((ndo
, "/"));
251 (void)fn_printn(ndo
, tptr
+ 1, len
- 1, NULL
);
255 print_unknown_data(ndo
, tptr
, "\n\t ", len
);
261 if (ndo
->ndo_vflag
< 1)
262 ND_PRINT((ndo
, ", length %u", caplen
));
266 ND_PRINT((ndo
, "%s", tstr
));
270 * Protocol type values.
272 * PT_NLPID means that the protocol type field contains an OSI NLPID.
274 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
275 * LLC header that specifies that the payload is for that protocol.
277 #define PT_NLPID 1 /* OSI NLPID */
278 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
281 cdp_print_addr(netdissect_options
*ndo
,
282 const u_char
* p
, int l
)
285 const u_char
*endp
= p
+ l
;
287 static const u_char prot_ipv6
[] = {
288 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
295 num
= EXTRACT_32BITS(p
);
298 while (p
< endp
&& num
>= 0) {
302 pt
= p
[0]; /* type of "protocol" field */
303 pl
= p
[1]; /* length of "protocol" field */
306 ND_TCHECK2(p
[pl
], 2);
307 if (p
+ pl
+ 2 > endp
)
309 al
= EXTRACT_16BITS(&p
[pl
]); /* address length */
311 if (pt
== PT_NLPID
&& pl
== 1 && *p
== NLPID_IP
&& al
== 4) {
313 * IPv4: protocol type = NLPID, protocol length = 1
314 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
322 ND_PRINT((ndo
, "IPv4 (%u) %s", num
, ipaddr_string(ndo
, p
)));
326 else if (pt
== PT_IEEE_802_2
&& pl
== 8 &&
327 memcmp(p
, prot_ipv6
, 8) == 0 && al
== 16) {
329 * IPv6: protocol type = IEEE 802.2 header,
330 * protocol length = 8 (size of LLC+SNAP header),
331 * protocol = LLC+SNAP header with the IPv6
332 * Ethertype, address length = 16
339 ND_PRINT((ndo
, "IPv6 (%u) %s", num
, ip6addr_string(ndo
, p
)));
345 * Generic case: just print raw data
350 ND_PRINT((ndo
, "pt=0x%02x, pl=%d, pb=", *(p
- 2), pl
));
352 ND_PRINT((ndo
, " %02x", *p
++));
356 al
= (*p
<< 8) + *(p
+ 1);
357 ND_PRINT((ndo
, ", al=%d, a=", al
));
363 ND_PRINT((ndo
, " %02x", *p
++));
367 ND_PRINT((ndo
, " "));
378 cdp_print_prefixes(netdissect_options
*ndo
,
379 const u_char
* p
, int l
)
384 ND_PRINT((ndo
, " IPv4 Prefixes (%d):", l
/ 5));
387 ND_PRINT((ndo
, " %u.%u.%u.%u/%u", p
[0], p
[1], p
[2], p
[3], p
[4]));
398 /* read in a <n>-byte number, MSB first
399 * (of course this can handle max sizeof(long))
401 static unsigned long cdp_get_number(const u_char
* p
, int l
)