1 /*
2  * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
3  *	The Regents of the University of California.  All rights reserved.
4  *
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
16  * written permission.
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.
20  *
21  * Code by Gert Doering, SpaceNet GmbH, gert@space.net
22  *
23  * Reference documentation:
24  *    http://www.cisco.com/univercd/cc/td/doc/product/lan/trsrb/frames.htm
25  */
26 
27 #define NETDISSECT_REWORKED
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <tcpdump-stdinc.h>
33 
34 #include <string.h>
35 
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "extract.h"			/* must come after interface.h */
39 #include "nlpid.h"
40 
41 static const char tstr[] = "[|cdp]";
42 
43 #define CDP_HEADER_LEN             4
44 #define CDP_HEADER_VERSION_OFFSET  0
45 #define CDP_HEADER_TTL_OFFSET      1
46 #define CDP_HEADER_CHECKSUM_OFFSET 2
47 
48 #define CDP_TLV_HEADER_LEN  4
49 #define CDP_TLV_TYPE_OFFSET 0
50 #define CDP_TLV_LEN_OFFSET  2
51 
52 static const struct tok cdp_tlv_values[] = {
53     { 0x01,             "Device-ID"},
54     { 0x02,             "Address"},
55     { 0x03,             "Port-ID"},
56     { 0x04,             "Capability"},
57     { 0x05,             "Version String"},
58     { 0x06,             "Platform"},
59     { 0x07,             "Prefixes"},
60     { 0x08,             "Protocol-Hello option"},
61     { 0x09,             "VTP Management Domain"},
62     { 0x0a,             "Native VLAN ID"},
63     { 0x0b,             "Duplex"},
64     { 0x0e,             "ATA-186 VoIP VLAN request"},
65     { 0x0f,             "ATA-186 VoIP VLAN assignment"},
66     { 0x10,             "power consumption"},
67     { 0x11,             "MTU"},
68     { 0x12,             "AVVID trust bitmap"},
69     { 0x13,             "AVVID untrusted ports CoS"},
70     { 0x14,             "System Name"},
71     { 0x15,             "System Object ID (not decoded)"},
72     { 0x16,             "Management Addresses"},
73     { 0x17,             "Physical Location"},
74     { 0, NULL}
75 };
76 
77 static const struct tok cdp_capability_values[] = {
78     { 0x01,             "Router" },
79     { 0x02,             "Transparent Bridge" },
80     { 0x04,             "Source Route Bridge" },
81     { 0x08,             "L2 Switch" },
82     { 0x10,             "L3 capable" },
83     { 0x20,             "IGMP snooping" },
84     { 0x40,             "L1 capable" },
85     { 0, NULL }
86 };
87 
88 static int cdp_print_addr(netdissect_options *, const u_char *, int);
89 static int cdp_print_prefixes(netdissect_options *, const u_char *, int);
90 static unsigned long cdp_get_number(const u_char *, int);
91 
92 void
cdp_print(netdissect_options * ndo,const u_char * pptr,u_int length,u_int caplen)93 cdp_print(netdissect_options *ndo,
94           const u_char *pptr, u_int length, u_int caplen)
95 {
96 	int type, len, i, j;
97 	const u_char *tptr;
98 
99 	if (caplen < CDP_HEADER_LEN) {
100 		ND_PRINT((ndo, "%s", tstr));
101 		return;
102 	}
103 
104 	tptr = pptr; /* temporary pointer */
105 
106 	ND_TCHECK2(*tptr, CDP_HEADER_LEN);
107 	ND_PRINT((ndo, "CDPv%u, ttl: %us", *(tptr + CDP_HEADER_VERSION_OFFSET),
108 					   *(tptr + CDP_HEADER_TTL_OFFSET)));
109 	if (ndo->ndo_vflag)
110 		ND_PRINT((ndo, ", checksum: 0x%04x (unverified), length %u", EXTRACT_16BITS(tptr+CDP_HEADER_CHECKSUM_OFFSET), length));
111 	tptr += CDP_HEADER_LEN;
112 
113 	while (tptr < (pptr+length)) {
114 		ND_TCHECK2(*tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
115 		type = EXTRACT_16BITS(tptr+CDP_TLV_TYPE_OFFSET);
116 		len  = EXTRACT_16BITS(tptr+CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
117 		if (len < CDP_TLV_HEADER_LEN) {
118 		    if (ndo->ndo_vflag)
119 			ND_PRINT((ndo, "\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
120 			       tok2str(cdp_tlv_values,"unknown field type", type),
121 			       type,
122 			       len,
123 			       PLURAL_SUFFIX(len))); /* plural */
124 		    else
125 			ND_PRINT((ndo, ", %s TLV length %u too short",
126 			       tok2str(cdp_tlv_values,"unknown field type", type),
127 			       len));
128 		    break;
129 		}
130 		tptr += CDP_TLV_HEADER_LEN;
131 		len -= CDP_TLV_HEADER_LEN;
132 
133 		ND_TCHECK2(*tptr, len);
134 
135 		if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
136 
137 		    if (ndo->ndo_vflag)
138 			ND_PRINT((ndo, "\n\t%s (0x%02x), value length: %u byte%s: ",
139 			       tok2str(cdp_tlv_values,"unknown field type", type),
140 			       type,
141 			       len,
142 			       PLURAL_SUFFIX(len))); /* plural */
143 
144 		    switch (type) {
145 
146 		    case 0x01: /* Device-ID */
147 			if (!ndo->ndo_vflag)
148 			    ND_PRINT((ndo, ", Device-ID "));
149 			ND_PRINT((ndo, "'"));
150 			(void)fn_printn(ndo, tptr, len, NULL);
151 			ND_PRINT((ndo, "'"));
152 			break;
153 		    case 0x02: /* Address */
154 			if (cdp_print_addr(ndo, tptr, len) < 0)
155 			    goto trunc;
156 			break;
157 		    case 0x03: /* Port-ID */
158 			ND_PRINT((ndo, "'"));
159 			(void)fn_printn(ndo, tptr, len, NULL);
160 			ND_PRINT((ndo, "'"));
161 			break;
162 		    case 0x04: /* Capabilities */
163 			if (len < 4)
164 			    goto trunc;
165 			ND_PRINT((ndo, "(0x%08x): %s",
166 			       EXTRACT_32BITS(tptr),
167 			       bittok2str(cdp_capability_values, "none", EXTRACT_32BITS(tptr))));
168 			break;
169 		    case 0x05: /* Version */
170 			ND_PRINT((ndo, "\n\t  "));
171 			for (i=0;i<len;i++) {
172 			    j = *(tptr+i);
173 			    ND_PRINT((ndo, "%c", j));
174 			    if (j == 0x0a) /* lets rework the version string to get a nice indentation */
175 				ND_PRINT((ndo, "\t  "));
176 			}
177 			break;
178 		    case 0x06: /* Platform */
179 			ND_PRINT((ndo, "'"));
180 			(void)fn_printn(ndo, tptr, len, NULL);
181 			ND_PRINT((ndo, "'"));
182 			break;
183 		    case 0x07: /* Prefixes */
184 			if (cdp_print_prefixes(ndo, tptr, len) < 0)
185 			    goto trunc;
186 			break;
187 		    case 0x08: /* Protocol Hello Option - not documented */
188 			break;
189 		    case 0x09: /* VTP Mgmt Domain  - CDPv2 */
190 			ND_PRINT((ndo, "'"));
191 			(void)fn_printn(ndo, tptr, len, NULL);
192 			ND_PRINT((ndo, "'"));
193 			break;
194 		    case 0x0a: /* Native VLAN ID - CDPv2 */
195 			if (len < 2)
196 			    goto trunc;
197 			ND_PRINT((ndo, "%d", EXTRACT_16BITS(tptr)));
198 			break;
199 		    case 0x0b: /* Duplex - CDPv2 */
200 			if (len < 1)
201 			    goto trunc;
202 			ND_PRINT((ndo, "%s", *(tptr) ? "full": "half"));
203 			break;
204 
205 		    /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
206 		     * plus more details from other sources
207 		     */
208 		    case 0x0e: /* ATA-186 VoIP VLAN request - incomplete doc. */
209 			if (len < 3)
210 			    goto trunc;
211 			ND_PRINT((ndo, "app %d, vlan %d", *(tptr), EXTRACT_16BITS(tptr + 1)));
212 			break;
213 		    case 0x10: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
214 			ND_PRINT((ndo, "%1.2fW", cdp_get_number(tptr, len) / 1000.0));
215 			break;
216 		    case 0x11: /* MTU - not documented */
217 			if (len < 4)
218 			    goto trunc;
219 			ND_PRINT((ndo, "%u bytes", EXTRACT_32BITS(tptr)));
220 			break;
221 		    case 0x12: /* AVVID trust bitmap - not documented */
222 			if (len < 1)
223 			    goto trunc;
224 			ND_PRINT((ndo, "0x%02x", *(tptr)));
225 			break;
226 		    case 0x13: /* AVVID untrusted port CoS - not documented */
227 			if (len < 1)
228 			    goto trunc;
229 			ND_PRINT((ndo, "0x%02x", *(tptr)));
230 			break;
231 		    case 0x14: /* System Name - not documented */
232 			ND_PRINT((ndo, "'"));
233 			(void)fn_printn(ndo, tptr, len, NULL);
234 			ND_PRINT((ndo, "'"));
235 			break;
236 		    case 0x16: /* System Object ID - not documented */
237 			if (cdp_print_addr(ndo, tptr, len) < 0)
238 				goto trunc;
239 			break;
240 		    case 0x17: /* Physical Location - not documented */
241 			if (len < 1)
242 			    goto trunc;
243 			ND_PRINT((ndo, "0x%02x", *(tptr)));
244 			if (len > 1) {
245 				ND_PRINT((ndo, "/"));
246 				(void)fn_printn(ndo, tptr + 1, len - 1, NULL);
247 			}
248 			break;
249 		    default:
250 			print_unknown_data(ndo, tptr, "\n\t  ", len);
251 			break;
252 		    }
253 		}
254 		tptr = tptr+len;
255 	}
256 	if (ndo->ndo_vflag < 1)
257 	    ND_PRINT((ndo, ", length %u", caplen));
258 
259 	return;
260 trunc:
261 	ND_PRINT((ndo, "%s", tstr));
262 }
263 
264 /*
265  * Protocol type values.
266  *
267  * PT_NLPID means that the protocol type field contains an OSI NLPID.
268  *
269  * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
270  * LLC header that specifies that the payload is for that protocol.
271  */
272 #define PT_NLPID		1	/* OSI NLPID */
273 #define PT_IEEE_802_2		2	/* IEEE 802.2 LLC header */
274 
275 static int
cdp_print_addr(netdissect_options * ndo,const u_char * p,int l)276 cdp_print_addr(netdissect_options *ndo,
277 	       const u_char * p, int l)
278 {
279 	int pt, pl, al, num;
280 	const u_char *endp = p + l;
281 #ifdef INET6
282 	static const u_char prot_ipv6[] = {
283 		0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
284 	};
285 #endif
286 
287 	ND_TCHECK2(*p, 4);
288 	if (p + 4 > endp)
289 		goto trunc;
290 	num = EXTRACT_32BITS(p);
291 	p += 4;
292 
293 	while (p < endp && num >= 0) {
294 		ND_TCHECK2(*p, 2);
295 		if (p + 2 > endp)
296 			goto trunc;
297 		pt = p[0];		/* type of "protocol" field */
298 		pl = p[1];		/* length of "protocol" field */
299 		p += 2;
300 
301 		ND_TCHECK2(p[pl], 2);
302 		if (p + pl + 2 > endp)
303 			goto trunc;
304 		al = EXTRACT_16BITS(&p[pl]);	/* address length */
305 
306 		if (pt == PT_NLPID && pl == 1 && *p == NLPID_IP && al == 4) {
307 			/*
308 			 * IPv4: protocol type = NLPID, protocol length = 1
309 			 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
310 			 * address length = 4
311 			 */
312 			p += 3;
313 
314 			ND_TCHECK2(*p, 4);
315 			if (p + 4 > endp)
316 				goto trunc;
317 			ND_PRINT((ndo, "IPv4 (%u) %s", num, ipaddr_string(ndo, p)));
318 			p += 4;
319 		}
320 #ifdef INET6
321 		else if (pt == PT_IEEE_802_2 && pl == 8 &&
322 		    memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
323 			/*
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
328 			 */
329 			p += 10;
330 			ND_TCHECK2(*p, al);
331 			if (p + al > endp)
332 				goto trunc;
333 
334 			ND_PRINT((ndo, "IPv6 (%u) %s", num, ip6addr_string(ndo, p)));
335 			p += al;
336 		}
337 #endif
338 		else {
339 			/*
340 			 * Generic case: just print raw data
341 			 */
342 			ND_TCHECK2(*p, pl);
343 			if (p + pl > endp)
344 				goto trunc;
345 			ND_PRINT((ndo, "pt=0x%02x, pl=%d, pb=", *(p - 2), pl));
346 			while (pl-- > 0)
347 				ND_PRINT((ndo, " %02x", *p++));
348 			ND_TCHECK2(*p, 2);
349 			if (p + 2 > endp)
350 				goto trunc;
351 			al = (*p << 8) + *(p + 1);
352 			ND_PRINT((ndo, ", al=%d, a=", al));
353 			p += 2;
354 			ND_TCHECK2(*p, al);
355 			if (p + al > endp)
356 				goto trunc;
357 			while (al-- > 0)
358 				ND_PRINT((ndo, " %02x", *p++));
359 		}
360 		num--;
361 		if (num)
362 			ND_PRINT((ndo, " "));
363 	}
364 
365 	return 0;
366 
367 trunc:
368 	return -1;
369 }
370 
371 
372 static int
cdp_print_prefixes(netdissect_options * ndo,const u_char * p,int l)373 cdp_print_prefixes(netdissect_options *ndo,
374 		   const u_char * p, int l)
375 {
376 	if (l % 5)
377 		goto trunc;
378 
379 	ND_PRINT((ndo, " IPv4 Prefixes (%d):", l / 5));
380 
381 	while (l > 0) {
382 		ND_PRINT((ndo, " %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]));
383 		l -= 5;
384 		p += 5;
385 	}
386 
387 	return 0;
388 
389 trunc:
390 	return -1;
391 }
392 
393 /* read in a <n>-byte number, MSB first
394  * (of course this can handle max sizeof(long))
395  */
cdp_get_number(const u_char * p,int l)396 static unsigned long cdp_get_number(const u_char * p, int l)
397 {
398     unsigned long res=0;
399     while( l>0 )
400     {
401 	res = (res<<8) + *p;
402 	p++; l--;
403     }
404     return res;
405 }
406