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