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 /* \summary: Cisco Discovery Protocol (CDP) printer */
33 #include <netdissect-stdinc.h>
37 #include "netdissect.h"
38 #include "addrtoname.h"
42 static const char tstr[] = "[|cdp]";
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_tlv_values[] = {
57 { 0x04, "Capability"},
58 { 0x05, "Version String"},
61 { 0x08, "Protocol-Hello option"},
62 { 0x09, "VTP Management Domain"},
63 { 0x0a, "Native VLAN ID"},
65 { 0x0e, "ATA-186 VoIP VLAN request"},
66 { 0x0f, "ATA-186 VoIP VLAN assignment"},
67 { 0x10, "power consumption"},
69 { 0x12, "AVVID trust bitmap"},
70 { 0x13, "AVVID untrusted ports CoS"},
71 { 0x14, "System Name"},
72 { 0x15, "System Object ID (not decoded)"},
73 { 0x16, "Management Addresses"},
74 { 0x17, "Physical Location"},
78 static const struct tok cdp_capability_values[] = {
80 { 0x02, "Transparent Bridge" },
81 { 0x04, "Source Route Bridge" },
82 { 0x08, "L2 Switch" },
83 { 0x10, "L3 capable" },
84 { 0x20, "IGMP snooping" },
85 { 0x40, "L1 capable" },
89 static int cdp_print_addr(netdissect_options *, const u_char *, int);
90 static int cdp_print_prefixes(netdissect_options *, const u_char *, int);
91 static unsigned long cdp_get_number(const u_char *, int);
94 cdp_print(netdissect_options *ndo,
95 const u_char *pptr, u_int length, u_int caplen)
100 if (caplen < CDP_HEADER_LEN) {
101 ND_PRINT((ndo, "%s", tstr));
105 tptr = pptr; /* temporary pointer */
107 ND_TCHECK2(*tptr, CDP_HEADER_LEN);
108 ND_PRINT((ndo, "CDPv%u, ttl: %us", *(tptr + CDP_HEADER_VERSION_OFFSET),
109 *(tptr + CDP_HEADER_TTL_OFFSET)));
111 ND_PRINT((ndo, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr+CDP_HEADER_CHECKSUM_OFFSET), length));
112 tptr += CDP_HEADER_LEN;
114 while (tptr < (pptr+length)) {
115 ND_TCHECK2(*tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
116 type = EXTRACT_16BITS(tptr+CDP_TLV_TYPE_OFFSET);
117 len = EXTRACT_16BITS(tptr+CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
118 if (len < CDP_TLV_HEADER_LEN) {
120 ND_PRINT((ndo, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
121 tok2str(cdp_tlv_values,"unknown field type", type),
124 PLURAL_SUFFIX(len))); /* plural */
126 ND_PRINT((ndo, ", %s TLV length %u too short",
127 tok2str(cdp_tlv_values,"unknown field type", type),
131 tptr += CDP_TLV_HEADER_LEN;
132 len -= CDP_TLV_HEADER_LEN;
134 ND_TCHECK2(*tptr, len);
136 if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
139 ND_PRINT((ndo, "\n\t%s (0x%02x), value length: %u byte%s: ",
140 tok2str(cdp_tlv_values,"unknown field type", type),
143 PLURAL_SUFFIX(len))); /* plural */
147 case 0x01: /* Device-ID */
149 ND_PRINT((ndo, ", Device-ID "));
150 ND_PRINT((ndo, "'"));
151 (void)fn_printn(ndo, tptr, len, NULL);
152 ND_PRINT((ndo, "'"));
154 case 0x02: /* Address */
155 if (cdp_print_addr(ndo, tptr, len) < 0)
158 case 0x03: /* Port-ID */
159 ND_PRINT((ndo, "'"));
160 (void)fn_printn(ndo, tptr, len, NULL);
161 ND_PRINT((ndo, "'"));
163 case 0x04: /* Capabilities */
166 ND_PRINT((ndo, "(0x%08x): %s",
167 EXTRACT_32BITS(tptr),
168 bittok2str(cdp_capability_values, "none", EXTRACT_32BITS(tptr))));
170 case 0x05: /* Version */
171 ND_PRINT((ndo, "\n\t "));
172 for (i=0;i<len;i++) {
174 if (j == '\n') /* lets rework the version string to
175 get a nice indentation */
176 ND_PRINT((ndo, "\n\t "));
178 fn_print_char(ndo, j);
181 case 0x06: /* Platform */
182 ND_PRINT((ndo, "'"));
183 (void)fn_printn(ndo, tptr, len, NULL);
184 ND_PRINT((ndo, "'"));
186 case 0x07: /* Prefixes */
187 if (cdp_print_prefixes(ndo, tptr, len) < 0)
190 case 0x08: /* Protocol Hello Option - not documented */
192 case 0x09: /* VTP Mgmt Domain - CDPv2 */
193 ND_PRINT((ndo, "'"));
194 (void)fn_printn(ndo, tptr, len, NULL);
195 ND_PRINT((ndo, "'"));
197 case 0x0a: /* Native VLAN ID - CDPv2 */
200 ND_PRINT((ndo, "%d", EXTRACT_16BITS(tptr)));
202 case 0x0b: /* Duplex - CDPv2 */
205 ND_PRINT((ndo, "%s", *(tptr) ? "full": "half"));
208 /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
209 * plus more details from other sources
211 case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
214 ND_PRINT((ndo, "app %d, vlan %d", *(tptr), EXTRACT_16BITS(tptr + 1)));
216 case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
217 ND_PRINT((ndo, "%1.2fW", cdp_get_number(tptr, len) / 1000.0));
219 case 0x11: /* MTU - not documented */
222 ND_PRINT((ndo, "%u bytes", EXTRACT_32BITS(tptr)));
224 case 0x12: /* AVVID trust bitmap - not documented */
227 ND_PRINT((ndo, "0x%02x", *(tptr)));
229 case 0x13: /* AVVID untrusted port CoS - not documented */
232 ND_PRINT((ndo, "0x%02x", *(tptr)));
234 case 0x14: /* System Name - not documented */
235 ND_PRINT((ndo, "'"));
236 (void)fn_printn(ndo, tptr, len, NULL);
237 ND_PRINT((ndo, "'"));
239 case 0x16: /* System Object ID - not documented */
240 if (cdp_print_addr(ndo, tptr, len) < 0)
243 case 0x17: /* Physical Location - not documented */
246 ND_PRINT((ndo, "0x%02x", *(tptr)));
248 ND_PRINT((ndo, "/"));
249 (void)fn_printn(ndo, tptr + 1, len - 1, NULL);
253 print_unknown_data(ndo, tptr, "\n\t ", len);
259 if (ndo->ndo_vflag < 1)
260 ND_PRINT((ndo, ", length %u", caplen));
264 ND_PRINT((ndo, "%s", tstr));
268 * Protocol type values.
270 * PT_NLPID means that the protocol type field contains an OSI NLPID.
272 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
273 * LLC header that specifies that the payload is for that protocol.
275 #define PT_NLPID 1 /* OSI NLPID */
276 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
279 cdp_print_addr(netdissect_options *ndo,
280 const u_char * p, int l)
283 const u_char *endp = p + l;
284 static const u_char prot_ipv6[] = {
285 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
291 num = EXTRACT_32BITS(p);
294 while (p < endp && num >= 0) {
298 pt = p[0]; /* type of "protocol" field */
299 pl = p[1]; /* length of "protocol" field */
302 ND_TCHECK2(p[pl], 2);
303 if (p + pl + 2 > endp)
305 al = EXTRACT_16BITS(&p[pl]); /* address length */
307 if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
309 * IPv4: protocol type = NLPID, protocol length = 1
310 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
318 ND_PRINT((ndo, "IPv4 (%u) %s", num, ipaddr_string(ndo, p)));
321 else if (pt == PT_IEEE_802_2 && pl == 8 &&
322 memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
324 * IPv6: protocol type = IEEE 802.2 header,
325 * protocol length = 8 (size of LLC+SNAP header),
326 * protocol = LLC+SNAP header with the IPv6
327 * Ethertype, address length = 16
334 ND_PRINT((ndo, "IPv6 (%u) %s", num, ip6addr_string(ndo, p)));
339 * Generic case: just print raw data
344 ND_PRINT((ndo, "pt=0x%02x, pl=%d, pb=", *(p - 2), pl));
346 ND_PRINT((ndo, " %02x", *p++));
350 al = (*p << 8) + *(p + 1);
351 ND_PRINT((ndo, ", al=%d, a=", al));
357 ND_PRINT((ndo, " %02x", *p++));
361 ND_PRINT((ndo, " "));
372 cdp_print_prefixes(netdissect_options *ndo,
373 const u_char * p, int l)
378 ND_PRINT((ndo, " IPv4 Prefixes (%d):", l / 5));
381 ND_PRINT((ndo, " %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]));
392 /* read in a <n>-byte number, MSB first
393 * (of course this can handle max sizeof(long))
395 static unsigned long cdp_get_number(const u_char * p, int l)