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