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
31 #include <netdissect-stdinc.h>
35 #include "netdissect.h"
36 #include "addrtoname.h"
40 static const char tstr
[] = "[|cdp]";
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_tlv_values
[] = {
55 { 0x04, "Capability"},
56 { 0x05, "Version String"},
59 { 0x08, "Protocol-Hello option"},
60 { 0x09, "VTP Management Domain"},
61 { 0x0a, "Native VLAN ID"},
63 { 0x0e, "ATA-186 VoIP VLAN request"},
64 { 0x0f, "ATA-186 VoIP VLAN assignment"},
65 { 0x10, "power consumption"},
67 { 0x12, "AVVID trust bitmap"},
68 { 0x13, "AVVID untrusted ports CoS"},
69 { 0x14, "System Name"},
70 { 0x15, "System Object ID (not decoded)"},
71 { 0x16, "Management Addresses"},
72 { 0x17, "Physical Location"},
76 static const struct tok cdp_capability_values
[] = {
78 { 0x02, "Transparent Bridge" },
79 { 0x04, "Source Route Bridge" },
80 { 0x08, "L2 Switch" },
81 { 0x10, "L3 capable" },
82 { 0x20, "IGMP snooping" },
83 { 0x40, "L1 capable" },
87 static int cdp_print_addr(netdissect_options
*, const u_char
*, int);
88 static int cdp_print_prefixes(netdissect_options
*, const u_char
*, int);
89 static unsigned long cdp_get_number(const u_char
*, int);
92 cdp_print(netdissect_options
*ndo
,
93 const u_char
*pptr
, u_int length
, u_int caplen
)
98 if (caplen
< CDP_HEADER_LEN
) {
99 ND_PRINT((ndo
, "%s", tstr
));
103 tptr
= pptr
; /* temporary pointer */
105 ND_TCHECK2(*tptr
, CDP_HEADER_LEN
);
106 ND_PRINT((ndo
, "CDPv%u, ttl: %us", *(tptr
+ CDP_HEADER_VERSION_OFFSET
),
107 *(tptr
+ CDP_HEADER_TTL_OFFSET
)));
109 ND_PRINT((ndo
, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr
+CDP_HEADER_CHECKSUM_OFFSET
), length
));
110 tptr
+= CDP_HEADER_LEN
;
112 while (tptr
< (pptr
+length
)) {
113 ND_TCHECK2(*tptr
, CDP_TLV_HEADER_LEN
); /* read out Type and Length */
114 type
= EXTRACT_16BITS(tptr
+CDP_TLV_TYPE_OFFSET
);
115 len
= EXTRACT_16BITS(tptr
+CDP_TLV_LEN_OFFSET
); /* object length includes the 4 bytes header length */
116 if (len
< CDP_TLV_HEADER_LEN
) {
118 ND_PRINT((ndo
, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
119 tok2str(cdp_tlv_values
,"unknown field type", type
),
122 PLURAL_SUFFIX(len
))); /* plural */
124 ND_PRINT((ndo
, ", %s TLV length %u too short",
125 tok2str(cdp_tlv_values
,"unknown field type", type
),
129 tptr
+= CDP_TLV_HEADER_LEN
;
130 len
-= CDP_TLV_HEADER_LEN
;
132 ND_TCHECK2(*tptr
, len
);
134 if (ndo
->ndo_vflag
|| type
== 1) { /* in non-verbose mode just print Device-ID */
137 ND_PRINT((ndo
, "\n\t%s (0x%02x), value length: %u byte%s: ",
138 tok2str(cdp_tlv_values
,"unknown field type", type
),
141 PLURAL_SUFFIX(len
))); /* plural */
145 case 0x01: /* Device-ID */
147 ND_PRINT((ndo
, ", Device-ID "));
148 ND_PRINT((ndo
, "'"));
149 (void)fn_printn(ndo
, tptr
, len
, NULL
);
150 ND_PRINT((ndo
, "'"));
152 case 0x02: /* Address */
153 if (cdp_print_addr(ndo
, tptr
, len
) < 0)
156 case 0x03: /* Port-ID */
157 ND_PRINT((ndo
, "'"));
158 (void)fn_printn(ndo
, tptr
, len
, NULL
);
159 ND_PRINT((ndo
, "'"));
161 case 0x04: /* Capabilities */
164 ND_PRINT((ndo
, "(0x%08x): %s",
165 EXTRACT_32BITS(tptr
),
166 bittok2str(cdp_capability_values
, "none", EXTRACT_32BITS(tptr
))));
168 case 0x05: /* Version */
169 ND_PRINT((ndo
, "\n\t "));
170 for (i
=0;i
<len
;i
++) {
172 if (j
== '\n') /* lets rework the version string to
173 get a nice indentation */
174 ND_PRINT((ndo
, "\n\t "));
176 fn_print_char(ndo
, j
);
179 case 0x06: /* Platform */
180 ND_PRINT((ndo
, "'"));
181 (void)fn_printn(ndo
, tptr
, len
, NULL
);
182 ND_PRINT((ndo
, "'"));
184 case 0x07: /* Prefixes */
185 if (cdp_print_prefixes(ndo
, tptr
, len
) < 0)
188 case 0x08: /* Protocol Hello Option - not documented */
190 case 0x09: /* VTP Mgmt Domain - CDPv2 */
191 ND_PRINT((ndo
, "'"));
192 (void)fn_printn(ndo
, tptr
, len
, NULL
);
193 ND_PRINT((ndo
, "'"));
195 case 0x0a: /* Native VLAN ID - CDPv2 */
198 ND_PRINT((ndo
, "%d", EXTRACT_16BITS(tptr
)));
200 case 0x0b: /* Duplex - CDPv2 */
203 ND_PRINT((ndo
, "%s", *(tptr
) ? "full": "half"));
206 /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
207 * plus more details from other sources
209 case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
212 ND_PRINT((ndo
, "app %d, vlan %d", *(tptr
), EXTRACT_16BITS(tptr
+ 1)));
214 case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
215 ND_PRINT((ndo
, "%1.2fW", cdp_get_number(tptr
, len
) / 1000.0));
217 case 0x11: /* MTU - not documented */
220 ND_PRINT((ndo
, "%u bytes", EXTRACT_32BITS(tptr
)));
222 case 0x12: /* AVVID trust bitmap - not documented */
225 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
227 case 0x13: /* AVVID untrusted port CoS - not documented */
230 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
232 case 0x14: /* System Name - not documented */
233 ND_PRINT((ndo
, "'"));
234 (void)fn_printn(ndo
, tptr
, len
, NULL
);
235 ND_PRINT((ndo
, "'"));
237 case 0x16: /* System Object ID - not documented */
238 if (cdp_print_addr(ndo
, tptr
, len
) < 0)
241 case 0x17: /* Physical Location - not documented */
244 ND_PRINT((ndo
, "0x%02x", *(tptr
)));
246 ND_PRINT((ndo
, "/"));
247 (void)fn_printn(ndo
, tptr
+ 1, len
- 1, NULL
);
251 print_unknown_data(ndo
, tptr
, "\n\t ", len
);
257 if (ndo
->ndo_vflag
< 1)
258 ND_PRINT((ndo
, ", length %u", caplen
));
262 ND_PRINT((ndo
, "%s", tstr
));
266 * Protocol type values.
268 * PT_NLPID means that the protocol type field contains an OSI NLPID.
270 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
271 * LLC header that specifies that the payload is for that protocol.
273 #define PT_NLPID 1 /* OSI NLPID */
274 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
277 cdp_print_addr(netdissect_options
*ndo
,
278 const u_char
* p
, int l
)
281 const u_char
*endp
= p
+ l
;
282 static const u_char prot_ipv6
[] = {
283 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
289 num
= EXTRACT_32BITS(p
);
292 while (p
< endp
&& num
>= 0) {
296 pt
= p
[0]; /* type of "protocol" field */
297 pl
= p
[1]; /* length of "protocol" field */
300 ND_TCHECK2(p
[pl
], 2);
301 if (p
+ pl
+ 2 > endp
)
303 al
= EXTRACT_16BITS(&p
[pl
]); /* address length */
305 if (pt
== PT_NLPID
&& pl
== 1 && *p
== NLPID_IP
&& al
== 4) {
307 * IPv4: protocol type = NLPID, protocol length = 1
308 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
316 ND_PRINT((ndo
, "IPv4 (%u) %s", num
, ipaddr_string(ndo
, p
)));
319 else if (pt
== PT_IEEE_802_2
&& pl
== 8 &&
320 memcmp(p
, prot_ipv6
, 8) == 0 && al
== 16) {
322 * IPv6: protocol type = IEEE 802.2 header,
323 * protocol length = 8 (size of LLC+SNAP header),
324 * protocol = LLC+SNAP header with the IPv6
325 * Ethertype, address length = 16
332 ND_PRINT((ndo
, "IPv6 (%u) %s", num
, ip6addr_string(ndo
, p
)));
337 * Generic case: just print raw data
342 ND_PRINT((ndo
, "pt=0x%02x, pl=%d, pb=", *(p
- 2), pl
));
344 ND_PRINT((ndo
, " %02x", *p
++));
348 al
= (*p
<< 8) + *(p
+ 1);
349 ND_PRINT((ndo
, ", al=%d, a=", al
));
355 ND_PRINT((ndo
, " %02x", *p
++));
359 ND_PRINT((ndo
, " "));
370 cdp_print_prefixes(netdissect_options
*ndo
,
371 const u_char
* p
, int l
)
376 ND_PRINT((ndo
, " IPv4 Prefixes (%d):", l
/ 5));
379 ND_PRINT((ndo
, " %u.%u.%u.%u/%u", p
[0], p
[1], p
[2], p
[3], p
[4]));
390 /* read in a <n>-byte number, MSB first
391 * (of course this can handle max sizeof(long))
393 static unsigned long cdp_get_number(const u_char
* p
, int l
)