]> The Tcpdump Group git mirrors - tcpdump/blob - print-cdp.c
more overrun sensitivity
[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.7 2001-06-15 07:58:28 itojun 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
53 void
54 cdp_print(const u_char *p, u_int length, u_int caplen,
55 const u_char *esrc, const u_char *edst)
56 {
57 int i;
58 int type, len;
59
60 /* Cisco Discovery Protocol */
61
62 if (caplen < 4) {
63 (void)printf("[|cdp]");
64 return;
65 }
66
67 i = 0; /* CDP data starts at offset 0 */
68 printf("CDP v%u, ttl=%us", p[i], p[i + 1]);
69 i += 4; /* skip version, TTL and chksum */
70
71 while (i < length) {
72 if (i + 4 > caplen)
73 goto trunc;
74 type = (p[i] << 8) + p[i + 1];
75 len = (p[i + 2] << 8) + p[i + 3];
76
77 if (vflag > 1)
78 printf("\n\t");
79
80 if (vflag)
81 printf(" %02x/%02x", type, len);
82
83 if (i + len > caplen)
84 goto trunc;
85
86 switch (type) {
87 case 0x01:
88 printf(" DevID '%.*s'", len - 4, p + i + 4);
89 break;
90 case 0x02:
91 printf(" Addr");
92 if (cdp_print_addr(p + i + 4, len - 4) < 0)
93 goto trunc;
94 break;
95 case 0x03:
96 printf(" PortID '%.*s'", len - 4, p + i + 4);
97 break;
98 case 0x04:
99 printf(" CAP 0x%02x", (unsigned) p[i + 7]);
100 break;
101 case 0x05:
102 if (vflag > 1)
103 printf(" Version:\n%.*s", len - 4, p + i + 4);
104 else
105 printf(" Version: (suppressed)");
106 break;
107 case 0x06:
108 printf(" Platform: '%.*s'", len - 4, p + i + 4);
109 break;
110 case 0x07:
111 if (cdp_print_prefixes(p + i + 4, len - 4) < 0)
112 goto trunc;
113 break;
114 case 0x09: /* guess - not documented */
115 printf(" VTP Management Domain: '%.*s'", len - 4,
116 p + i + 4);
117 break;
118 case 0x0a: /* guess - not documented */
119 printf(" Native VLAN ID: %d",
120 (p[i + 4] << 8) + p[i + 4 + 1] - 1);
121 break;
122 case 0x0b: /* guess - not documented */
123 printf(" Duplex: %s", p[i + 4] ? "full": "half");
124 break;
125 default:
126 printf(" unknown field type %02x, len %d", type, len);
127 break;
128 }
129
130 /* avoid infinite loop */
131 if (len == 0)
132 break;
133 i += len;
134 }
135
136 return;
137
138 trunc:
139 printf("[|cdp]");
140 }
141
142 static int
143 cdp_print_addr(const u_char * p, int l)
144 {
145 int pl, al, num;
146 const u_char *endp = p + l;
147
148 num = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
149 p += 4;
150
151 printf(" (%d): ", num);
152
153 while (p < endp && num >= 0) {
154 if (p + 2 >= endp)
155 goto trunc;
156 pl = p[1];
157 p += 2;
158
159 /* special case: IPv4, protocol type=0xcc, addr. length=4 */
160 if (p + 3 >= endp)
161 goto trunc;
162 if (pl == 1 && *p == 0xcc && p[1] == 0 && p[2] == 4) {
163 if (p + 7 >= endp)
164 goto trunc;
165 p += 3;
166
167 printf("IPv4 %u.%u.%u.%u ", p[0], p[1], p[2], p[3]);
168 p += 4;
169 } else { /* generic case: just print raw data */
170 if (p + pl >= endp)
171 goto trunc;
172 printf("pt=0x%02x, pl=%d, pb=", *(p - 2), pl);
173 while (pl-- > 0)
174 printf(" %02x", *p++);
175 al = (*p << 8) + *(p + 1);
176 if (p + 2 + al >= endp)
177 goto trunc;
178 printf(", al=%d, a=", al);
179 p += 2;
180 while (al-- > 0)
181 printf(" %02x", *p++);
182 }
183 printf(" ");
184 num--;
185 }
186
187 return 0;
188
189 trunc:
190 return -1;
191 }
192
193
194 static int
195 cdp_print_prefixes(const u_char * p, int l)
196 {
197 if (l % 5)
198 goto trunc;
199
200 printf(" IPv4 Prefixes (%d):", l / 5);
201
202 while (l > 0) {
203 printf(" %u.%u.%u.%u/%u", p[0], p[1], p[2], p[3], p[4]);
204 l -= 5;
205 p += 5;
206 }
207
208 return 0;
209
210 trunc:
211 return -1;
212 }