]> The Tcpdump Group git mirrors - tcpdump/blob - print-cdp.c
Merge in CDP changes from Terry Kennedy <[email protected]>.
[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 #ifndef lint
28 static const char rcsid[] =
29 "@(#) $Header: /tcpdump/master/tcpdump/print-cdp.c,v 1.16 2002-08-07 05:21:07 guy Exp $";
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <tcpdump-stdinc.h>
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
44
45 static int cdp_print_addr(const u_char *, int);
46 static int cdp_print_prefixes(const u_char *, int);
47 static unsigned long cdp_get_number(const u_char *, int);
48
49 void
50 cdp_print(const u_char *p, u_int length, u_int caplen,
51 const u_char *esrc, const u_char *edst)
52 {
53 u_int i;
54 int type, len;
55
56 /* Cisco Discovery Protocol */
57
58 if (caplen < 4) {
59 (void)printf("[|cdp]");
60 return;
61 }
62
63 i = 0; /* CDP data starts at offset 0 */
64 printf("CDP v%u, ttl=%us", p[i], p[i + 1]);
65 i += 4; /* skip version, TTL and chksum */
66
67 while (i < length) {
68 if (i + 4 > caplen)
69 goto trunc;
70 type = (p[i] << 8) + p[i + 1];
71 len = (p[i + 2] << 8) + p[i + 3];
72
73 if (vflag > 1)
74 printf("\n\t");
75
76 if (vflag)
77 printf(" %02x/%02x", type, len);
78
79 if (i + len > caplen)
80 goto trunc;
81
82 switch (type) {
83 case 0x00:
84 printf(" Goodbye");
85 break;
86 case 0x01:
87 printf(" DevID '%.*s'", len - 4, p + i + 4);
88 break;
89 case 0x02:
90 printf(" Addr");
91 if (cdp_print_addr(p + i + 4, len - 4) < 0)
92 goto trunc;
93 break;
94 case 0x03:
95 printf(" PortID '%.*s'", len - 4, p + i + 4);
96 break;
97 case 0x04:
98 printf(" CAP 0x%02x", (unsigned) p[i + 7]);
99 break;
100 case 0x05:
101 if (vflag > 1)
102 printf(" Version:\n%.*s", len - 4, p + i + 4);
103 else
104 printf(" Version: (suppressed)");
105 break;
106 case 0x06:
107 printf(" Platform: '%.*s'", len - 4, p + i + 4);
108 break;
109 case 0x07:
110 if (cdp_print_prefixes(p + i + 4, len - 4) < 0)
111 goto trunc;
112 break;
113 case 0x08: /* guess - not documented */
114 printf(" Protocol-Hello option" );
115 break;
116 case 0x09: /* guess - not documented */
117 printf(" VTP Management Domain: '%.*s'", len - 4,
118 p + i + 4);
119 break;
120 case 0x0a: /* guess - not documented */
121 printf(" Native VLAN ID: %d",
122 (p[i + 4] << 8) + p[i + 4 + 1] );
123 break;
124 case 0x0b: /* guess - not documented */
125 printf(" Duplex: %s", p[i + 4] ? "full": "half");
126 break;
127 /* http://www.cisco.com/univercd/cc/td/doc/product/voice/ata/atarn/186rn21m.htm
128 * plus more details from other sources
129 */
130 case 0x0e: /* incomplete doc. */
131 printf(" ATA-186 VoIP VLAN request, app %d, vlan %d",
132 p[i + 4], EXTRACT_16BITS(&p[i+4+1]));
133 break;
134 case 0x0f: /* incomplete doc. */
135 printf(" ATA-186 VoIP VLAN assignment" );
136 break;
137 case 0x10: /* incomplete doc. */
138 printf(" power consumption: %1.2fW",
139 cdp_get_number(p+i+4, len-4)/1000.0 );
140 break;
141 case 0x11: /* guess - not documented */
142 printf(" MTU %u bytes", EXTRACT_32BITS(&p[i+4]));
143 break;
144 case 0x12: /* guess - not documented */
145 printf(" AVVID trust bitmap 0x%02x", p[i+4] );
146 break;
147 case 0x13: /* guess - not documented */
148 printf(" AVVID untrusted ports CoS: 0x%02x", p[i+4]);
149 break;
150 case 0x14: /* guess - not documented */
151 printf(" System Name: '%.*s'", len - 4,
152 p + i + 4);
153 break;
154 case 0x15: /* guess - not documented */
155 printf(" System Object ID (not decoded) "); /* TODO */
156 break;
157 case 0x16: /* guess - not documented */
158 printf(" Management Addresses: ");
159 if (cdp_print_addr(p + i + 4, len - 4) < 0)
160 goto trunc;
161 break;
162 case 0x17: /* guess - not documented */
163 printf(" Physical Location: 0x%02x/%.*s",
164 p[i+4], len - 5, p + i + 5 );
165 break;
166 default:
167 printf(" unknown field type %02x, len %d", type, len);
168 break;
169 }
170
171 /* avoid infinite loop */
172 if (len == 0)
173 break;
174 i += len;
175 }
176
177 return;
178
179 trunc:
180 printf("[|cdp]");
181 }
182
183 /*
184 * Protocol type values.
185 *
186 * PT_NLPID means that the protocol type field contains an OSI NLPID.
187 *
188 * PT_IEEE_802_2 means that the protocol type field contains an IEEE 802.2
189 * LLC header that specifies that the payload is for that protocol.
190 */
191 #define PT_NLPID 1 /* OSI NLPID */
192 #define PT_IEEE_802_2 2 /* IEEE 802.2 LLC header */
193
194 static int
195 cdp_print_addr(const u_char * p, int l)
196 {
197 int pt, pl, al, num;
198 const u_char *endp = p + l;
199 #ifdef INET6
200 static u_char prot_ipv6[] = {
201 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x86, 0xdd
202 };
203 #endif
204
205 num = EXTRACT_32BITS(p);
206 p += 4;
207
208 printf(" (%d): ", num);
209
210 while (p < endp && num >= 0) {
211 if (p + 2 > endp)
212 goto trunc;
213 pt = p[0]; /* type of "protocol" field */
214 pl = p[1]; /* length of "protocol" field */
215 p += 2;
216
217 if (p + pl + 2 > endp)
218 goto trunc;
219 al = EXTRACT_16BITS(&p[pl]); /* address length */
220
221 if (pt == PT_NLPID && pl == 1 && *p == 0xcc && al == 4) {
222 /*
223 * IPv4: protocol type = NLPID, protocol length = 1
224 * (1-byte NLPID), protocol = 0xcc (NLPID for IPv4),
225 * address length = 4
226 */
227 p += 3;
228
229 if (p + 4 > endp)
230 goto trunc;
231 printf("IPv4 %u.%u.%u.%u", p[0], p[1], p[2], p[3]);
232 p += 4;
233 }
234 #ifdef INET6
235 else if (pt == PT_IEEE_802_2 && pl == 8 &&
236 memcmp(p, prot_ipv6, 8) == 0 && al == 16) {
237 /*
238 * IPv6: protocol type = IEEE 802.2 header,
239 * protocol length = 8 (size of LLC+SNAP header),
240 * protocol = LLC+SNAP header with the IPv6
241 * Ethertype, address length = 16
242 */
243 p += 10;
244 if (p + al > endp)
245 goto trunc;
246
247 printf("IPv6 %s", ip6addr_string(p));
248 p += al;
249 }
250 #endif
251 else {
252 /*
253 * Generic case: just print raw data
254 */
255 if (p + pl > endp)
256 goto trunc;
257 printf("pt=0x%02x, pl=%d, pb=", *(p - 2), pl);
258 while (pl-- > 0)
259 printf(" %02x", *p++);
260 if (p + 2 > endp)
261 goto trunc;
262 al = (*p << 8) + *(p + 1);
263 printf(", al=%d, a=", al);
264 p += 2;
265 if (p + al > endp)
266 goto trunc;
267 while (al-- > 0)
268 printf(" %02x", *p++);
269 }
270 num--;
271 if (num)
272 printf(" ");
273 }
274
275 return 0;
276
277 trunc:
278 return -1;
279 }
280
281
282 static int
283 cdp_print_prefixes(const u_char * p, int l)
284 {
285 if (l % 5)
286 goto trunc;
287
288 printf(" IPv4 Prefixes (%d):", l / 5);
289
290 while (l > 0) {
291 printf(" %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]);
292 l -= 5;
293 p += 5;
294 }
295
296 return 0;
297
298 trunc:
299 return -1;
300 }
301
302 /* read in a <n>-byte number, MSB first
303 * (of course this can handle max sizeof(long))
304 */
305 static unsigned long cdp_get_number(const u_char * p, int l)
306 {
307 unsigned long res=0;
308 while( l>0 )
309 {
310 res = (res<<8) + *p;
311 p++; l--;
312 }
313 return res;
314 }