]> The Tcpdump Group git mirrors - tcpdump/blob - print-cdp.c
d0e636883118bbb1ecefe7e0f40010f40e33bcb7
[tcpdump] / print-cdp.c
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 /* \summary: Cisco Discovery Protocol (CDP) printer */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #include "netdissect-stdinc.h"
34
35 #include <string.h>
36
37 #include "netdissect.h"
38 #include "addrtoname.h"
39 #include "extract.h"
40 #include "nlpid.h"
41
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 assignment"},
65 { 0x0f, "ATA-186 VoIP VLAN request"},
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 *, u_int);
89 static int cdp_print_prefixes(netdissect_options *, const u_char *, u_int);
90 static unsigned int cdp_get_number(netdissect_options *, const u_char *, u_int);
91
92 void
93 cdp_print(netdissect_options *ndo,
94 const u_char *pptr, u_int length, u_int caplen)
95 {
96 u_int type, len, i;
97 const u_char *tptr;
98
99 ndo->ndo_protocol = "cdp";
100 if (caplen < CDP_HEADER_LEN) {
101 nd_print_trunc(ndo);
102 return;
103 }
104
105 tptr = pptr; /* temporary pointer */
106
107 ND_TCHECK_LEN(tptr, CDP_HEADER_LEN);
108 ND_PRINT("CDPv%u, ttl: %us",
109 GET_U_1((tptr + CDP_HEADER_VERSION_OFFSET)),
110 GET_U_1(tptr + CDP_HEADER_TTL_OFFSET));
111 if (ndo->ndo_vflag)
112 ND_PRINT(", checksum: 0x%04x (unverified), length %u",
113 GET_BE_U_2(tptr + CDP_HEADER_CHECKSUM_OFFSET),
114 length);
115 tptr += CDP_HEADER_LEN;
116
117 while (tptr < (pptr+length)) {
118 ND_TCHECK_LEN(tptr, CDP_TLV_HEADER_LEN); /* read out Type and Length */
119 type = GET_BE_U_2(tptr + CDP_TLV_TYPE_OFFSET);
120 len = GET_BE_U_2(tptr + CDP_TLV_LEN_OFFSET); /* object length includes the 4 bytes header length */
121 if (len < CDP_TLV_HEADER_LEN) {
122 if (ndo->ndo_vflag)
123 ND_PRINT("\n\t%s (0x%02x), TLV length: %u byte%s (too short)",
124 tok2str(cdp_tlv_values,"unknown field type", type),
125 type,
126 len,
127 PLURAL_SUFFIX(len)); /* plural */
128 else
129 ND_PRINT(", %s TLV length %u too short",
130 tok2str(cdp_tlv_values,"unknown field type", type),
131 len);
132 break;
133 }
134 tptr += CDP_TLV_HEADER_LEN;
135 len -= CDP_TLV_HEADER_LEN;
136
137 ND_TCHECK_LEN(tptr, len);
138
139 if (ndo->ndo_vflag || type == 1) { /* in non-verbose mode just print Device-ID */
140
141 if (ndo->ndo_vflag)
142 ND_PRINT("\n\t%s (0x%02x), value length: %u byte%s: ",
143 tok2str(cdp_tlv_values,"unknown field type", type),
144 type,
145 len,
146 PLURAL_SUFFIX(len)); /* plural */
147
148 switch (type) {
149
150 case 0x01: /* Device-ID */
151 if (!ndo->ndo_vflag)
152 ND_PRINT(", Device-ID ");
153 ND_PRINT("'");
154 (void)nd_printn(ndo, tptr, len, NULL);
155 ND_PRINT("'");
156 break;
157 case 0x02: /* Address */
158 if (cdp_print_addr(ndo, tptr, len) < 0)
159 goto trunc;
160 break;
161 case 0x03: /* Port-ID */
162 ND_PRINT("'");
163 (void)nd_printn(ndo, tptr, len, NULL);
164 ND_PRINT("'");
165 break;
166 case 0x04: /* Capabilities */
167 if (len < 4)
168 goto trunc;
169 ND_PRINT("(0x%08x): %s",
170 GET_BE_U_4(tptr),
171 bittok2str(cdp_capability_values, "none", GET_BE_U_4(tptr)));
172 break;
173 case 0x05: /* Version */
174 ND_PRINT("\n\t ");
175 for (i=0;i<len;i++) {
176 u_char c;
177
178 c = GET_U_1(tptr + i);
179 if (c == '\n') /* lets rework the version string to
180 get a nice indentation */
181 ND_PRINT("\n\t ");
182 else
183 fn_print_char(ndo, c);
184 }
185 break;
186 case 0x06: /* Platform */
187 ND_PRINT("'");
188 (void)nd_printn(ndo, tptr, len, NULL);
189 ND_PRINT("'");
190 break;
191 case 0x07: /* Prefixes */
192 if (cdp_print_prefixes(ndo, tptr, len) < 0)
193 goto trunc;
194 break;
195 case 0x08: /* Protocol Hello Option - not documented */
196 break;
197 case 0x09: /* VTP Mgmt Domain - CDPv2 */
198 ND_PRINT("'");
199 (void)nd_printn(ndo, tptr, len, NULL);
200 ND_PRINT("'");
201 break;
202 case 0x0a: /* Native VLAN ID - CDPv2 */
203 if (len < 2)
204 goto trunc;
205 ND_PRINT("%u", GET_BE_U_2(tptr));
206 break;
207 case 0x0b: /* Duplex - CDPv2 */
208 if (len < 1)
209 goto trunc;
210 ND_PRINT("%s", GET_U_1(tptr) ? "full": "half");
211 break;
212
213 /* http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cata/186/2_12_m/english/release/notes/186rn21m.html
214 * plus more details from other sources
215 *
216 * There are apparently versions of the request with both
217 * 2 bytes and 3 bytes of value. The 3 bytes of value
218 * appear to be a 1-byte application type followed by a
219 * 2-byte VLAN ID; the 2 bytes of value are unknown
220 * (they're 0x20 0x00 in some captures I've seen; that
221 * is not a valid VLAN ID, as VLAN IDs are 12 bits).
222 *
223 * The replies all appear to be 3 bytes long.
224 */
225 case 0x0e: /* ATA-186 VoIP VLAN assignment - incomplete doc. */
226 if (len < 3)
227 goto trunc;
228 ND_PRINT("app %u, vlan %u", GET_U_1(tptr),
229 GET_BE_U_2(tptr + 1));
230 break;
231 case 0x0f: /* ATA-186 VoIP VLAN request - incomplete doc. */
232 if (len < 2)
233 goto trunc;
234 if (len == 2)
235 ND_PRINT("unknown 0x%04x", GET_BE_U_2(tptr));
236 else
237 ND_PRINT("app %u, vlan %u", GET_U_1(tptr),
238 GET_BE_U_2(tptr + 1));
239 break;
240 case 0x10: /* Power - not documented */
241 ND_PRINT("%1.2fW", cdp_get_number(ndo, tptr, len) / 1000.0);
242 break;
243 case 0x11: /* MTU - not documented */
244 if (len < 4)
245 goto trunc;
246 ND_PRINT("%u bytes", GET_BE_U_4(tptr));
247 break;
248 case 0x12: /* AVVID trust bitmap - not documented */
249 if (len < 1)
250 goto trunc;
251 ND_PRINT("0x%02x", GET_U_1(tptr));
252 break;
253 case 0x13: /* AVVID untrusted port CoS - not documented */
254 if (len < 1)
255 goto trunc;
256 ND_PRINT("0x%02x", GET_U_1(tptr));
257 break;
258 case 0x14: /* System Name - not documented */
259 ND_PRINT("'");
260 (void)nd_printn(ndo, tptr, len, NULL);
261 ND_PRINT("'");
262 break;
263 case 0x16: /* System Object ID - not documented */
264 if (cdp_print_addr(ndo, tptr, len) < 0)
265 goto trunc;
266 break;
267 case 0x17: /* Physical Location - not documented */
268 if (len < 1)
269 goto trunc;
270 ND_PRINT("0x%02x", GET_U_1(tptr));
271 if (len > 1) {
272 ND_PRINT("/");
273 (void)nd_printn(ndo, tptr + 1, len - 1, NULL);
274 }
275 break;
276 default:
277 print_unknown_data(ndo, tptr, "\n\t ", len);
278 break;
279 }
280 }
281 tptr = tptr+len;
282 }
283 if (ndo->ndo_vflag < 1)
284 ND_PRINT(", length %u", caplen);
285
286 return;
287 trunc:
288 nd_print_trunc(ndo);
289 }
290
291 /*
292 * Protocol type values.
293 *
294 * PT_NLPID means that the protocol type field contains an OSI NLPID.
295 *
296 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
297 * LLC header that specifies that the payload is for that protocol.
298 */
299 #define PT_NLPID 1 /* OSI NLPID */
300 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
301
302 static int
303 cdp_print_addr(netdissect_options *ndo,
304 const u_char * p, u_int l)
305 {
306 u_int pt, pl, al, num;
307 const u_char *endp = p + l;
308 static const u_char prot_ipv6[] = {
309 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
310 };
311
312 ND_TCHECK_4(p);
313 if (p + 4 > endp)
314 goto trunc;
315 num = GET_BE_U_4(p);
316 p += 4;
317
318 while (p < endp && num != 0) {
319 ND_TCHECK_2(p);
320 if (p + 2 > endp)
321 goto trunc;
322 pt = GET_U_1(p); /* type of "protocol" field */
323 pl = GET_U_1(p + 1); /* length of "protocol" field */
324 p += 2;
325
326 ND_TCHECK_2(p + pl);
327 if (p + pl + 2 > endp)
328 goto trunc;
329 al = GET_BE_U_2(p + pl); /* address length */
330
331 if (pt == PT_NLPID && pl == 1 && GET_U_1(p) == NLPID_IP &&
332 al == 4) {
333 /*
334 * IPv4: protocol type = NLPID, protocol length = 1
335 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
336 * address length = 4
337 */
338 p += 3;
339
340 ND_TCHECK_4(p);
341 if (p + 4 > endp)
342 goto trunc;
343 ND_PRINT("IPv4 (%u) %s", num, GET_IPADDR_STRING(p));
344 p += 4;
345 }
346 else if (pt == PT_IEEE_802_2 && pl == 8 &&
347 memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
348 /*
349 * IPv6: protocol type = IEEE 802.2 header,
350 * protocol length = 8 (size of LLC+SNAP header),
351 * protocol = LLC+SNAP header with the IPv6
352 * Ethertype, address length = 16
353 */
354 p += 10;
355 ND_TCHECK_LEN(p, al);
356 if (p + al > endp)
357 goto trunc;
358
359 ND_PRINT("IPv6 (%u) %s", num, GET_IP6ADDR_STRING(p));
360 p += al;
361 }
362 else {
363 /*
364 * Generic case: just print raw data
365 */
366 ND_TCHECK_LEN(p, pl);
367 if (p + pl > endp)
368 goto trunc;
369 ND_PRINT("pt=0x%02x, pl=%u, pb=", GET_U_1((p - 2)),
370 pl);
371 while (pl != 0) {
372 ND_PRINT(" %02x", GET_U_1(p));
373 p++;
374 pl--;
375 }
376 ND_TCHECK_2(p);
377 if (p + 2 > endp)
378 goto trunc;
379 ND_PRINT(", al=%u, a=", al);
380 p += 2;
381 ND_TCHECK_LEN(p, al);
382 if (p + al > endp)
383 goto trunc;
384 while (al != 0) {
385 ND_PRINT(" %02x", GET_U_1(p));
386 p++;
387 al--;
388 }
389 }
390 num--;
391 if (num)
392 ND_PRINT(" ");
393 }
394
395 return 0;
396
397 trunc:
398 return -1;
399 }
400
401
402 static int
403 cdp_print_prefixes(netdissect_options *ndo,
404 const u_char * p, u_int l)
405 {
406 if (l % 5)
407 goto trunc;
408
409 ND_PRINT(" IPv4 Prefixes (%u):", l / 5);
410
411 while (l > 0) {
412 ND_PRINT(" %u.%u.%u.%u/%u",
413 GET_U_1(p), GET_U_1(p + 1), GET_U_1(p + 2),
414 GET_U_1(p + 3), GET_U_1(p + 4));
415 l -= 5;
416 p += 5;
417 }
418
419 return 0;
420
421 trunc:
422 return -1;
423 }
424
425 /* read in a <n>-byte number, MSB first
426 * (of course this can handle max sizeof(int))
427 */
428 static unsigned int
429 cdp_get_number(netdissect_options *ndo, const u_char * p, u_int l)
430 {
431 unsigned int res=0;
432 while( l>0 )
433 {
434 res = (res<<8) + GET_U_1(p);
435 p++; l--;
436 }
437 return res;
438 }