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