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