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