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