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