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