]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
- hack the ethertype_values[] table to accomodate GRE flavours
[tcpdump] / print-gre.c
1 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
2
3 /*
4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jason L. Wright
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * tcpdump filter for GRE - Generic Routing Encapsulation
36 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
37 */
38
39 #ifndef lint
40 static const char rcsid[] _U_ =
41 "@(#) $Header: /tcpdump/master/tcpdump/print-gre.c,v 1.25 2004-06-12 16:32:56 hannes Exp $ (LBL)";
42 #endif
43
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #include <tcpdump-stdinc.h>
49
50 #include <stdio.h>
51 #include <string.h>
52
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "extract.h"
56
57 #include "ip.h"
58 #include "ethertype.h"
59
60 #define GRE_CP 0x8000 /* checksum present */
61 #define GRE_RP 0x4000 /* routing present */
62 #define GRE_KP 0x2000 /* key present */
63 #define GRE_SP 0x1000 /* sequence# present */
64 #define GRE_sP 0x0800 /* source routing */
65 #define GRE_RECRS 0x0700 /* recursion count */
66 #define GRE_AP 0x0080 /* acknowledgment# present */
67 #define GRE_VERS_MASK 0x0007 /* protocol version */
68
69 /* source route entry types */
70 #define GRESRE_IP 0x0800 /* IP */
71 #define GRESRE_ASN 0xfffe /* ASN */
72
73 void gre_print_0(const u_char *, u_int);
74 void gre_print_1(const u_char *, u_int);
75 void gre_sre_print(u_int16_t, u_int8_t, u_int8_t, const u_char *, u_int);
76 void gre_sre_ip_print(u_int8_t, u_int8_t, const u_char *, u_int);
77 void gre_sre_asn_print(u_int8_t, u_int8_t, const u_char *, u_int);
78
79 void
80 gre_print(const u_char *bp, u_int length)
81 {
82 u_int len = length, vers;
83
84 if (len < 2) {
85 printf("[|gre]");
86 return;
87 }
88 vers = EXTRACT_16BITS(bp) & GRE_VERS_MASK;
89 printf("GREv%u",vers);
90
91 switch(vers) {
92 case 0:
93 gre_print_0(bp, len);
94 break;
95 case 1:
96 gre_print_1(bp, len);
97 break;
98 default:
99 printf(" ERROR: unknown-version");
100 break;
101 }
102 return;
103
104 }
105
106 void
107 gre_print_0(const u_char *bp, u_int length)
108 {
109 u_int len = length;
110 u_int16_t flags, prot;
111
112 flags = EXTRACT_16BITS(bp);
113 if (vflag && flags) {
114 printf("[%s%s%s%s%s] ",
115 (flags & GRE_CP) ? "C" : "",
116 (flags & GRE_RP) ? "R" : "",
117 (flags & GRE_KP) ? "K" : "",
118 (flags & GRE_SP) ? "S" : "",
119 (flags & GRE_sP) ? "s" : "");
120 }
121
122 len -= 2;
123 bp += 2;
124
125 if (len < 2)
126 goto trunc;
127 prot = EXTRACT_16BITS(bp);
128 len -= 2;
129 bp += 2;
130
131 if ((flags & GRE_CP) | (flags & GRE_RP)) {
132 if (len < 2)
133 goto trunc;
134 if (vflag)
135 printf("sum 0x%x ", EXTRACT_16BITS(bp));
136 bp += 2;
137 len -= 2;
138
139 if (len < 2)
140 goto trunc;
141 printf("off 0x%x ", EXTRACT_16BITS(bp));
142 bp += 2;
143 len -= 2;
144 }
145
146 if (flags & GRE_KP) {
147 if (len < 4)
148 goto trunc;
149 printf("key=0x%x ", EXTRACT_32BITS(bp));
150 bp += 4;
151 len -= 4;
152 }
153
154 if (flags & GRE_SP) {
155 if (len < 4)
156 goto trunc;
157 printf("seq %u ", EXTRACT_32BITS(bp));
158 bp += 4;
159 len -= 4;
160 }
161
162 if (flags & GRE_RP) {
163 for (;;) {
164 u_int16_t af;
165 u_int8_t sreoff;
166 u_int8_t srelen;
167
168 if (len < 4)
169 goto trunc;
170 af = EXTRACT_16BITS(bp);
171 sreoff = *(bp + 2);
172 srelen = *(bp + 3);
173 bp += 4;
174 len -= 4;
175
176 if (af == 0 && srelen == 0)
177 break;
178
179 gre_sre_print(af, sreoff, srelen, bp, len);
180
181 if (len < srelen)
182 goto trunc;
183 bp += srelen;
184 len -= srelen;
185 }
186 }
187
188 if (eflag)
189 printf(", proto %s (0x%04x)",
190 tok2str(ethertype_values,"unknown",prot),
191 prot);
192 printf(": ");
193
194 switch (prot) {
195 case ETHERTYPE_IP:
196 ip_print(bp, len);
197 break;
198 case ETHERTYPE_IPV6:
199 ip6_print(bp, len);
200 break;
201 case ETHERTYPE_MPLS:
202 mpls_print(bp, len);
203 break;
204 case ETHERTYPE_IPX:
205 ipx_print(bp, len);
206 break;
207 case ETHERTYPE_ATALK:
208 atalk_print(bp, len);
209 break;
210 case ETHERTYPE_GRE_ISO:
211 isoclns_print(bp, len, len);
212 break;
213 default:
214 printf("gre-proto-0x%x", prot);
215 }
216 return;
217
218 trunc:
219 printf("[|gre]");
220 }
221
222 void
223 gre_print_1(const u_char *bp, u_int length)
224 {
225 u_int len = length;
226 u_int16_t flags, prot;
227
228 flags = EXTRACT_16BITS(bp);
229 len -= 2;
230 bp += 2;
231
232 if (vflag) {
233 printf("[%s%s%s%s%s%s] ",
234 (flags & GRE_CP) ? "C" : "",
235 (flags & GRE_RP) ? "R" : "",
236 (flags & GRE_KP) ? "K" : "",
237 (flags & GRE_SP) ? "S" : "",
238 (flags & GRE_sP) ? "s" : "",
239 (flags & GRE_AP) ? "A" : "");
240 }
241
242 if (len < 2)
243 goto trunc;
244 prot = EXTRACT_16BITS(bp);
245 len -= 2;
246 bp += 2;
247
248 if (flags & GRE_CP) {
249 printf("cpset!");
250 return;
251 }
252 if (flags & GRE_RP) {
253 printf("rpset!");
254 return;
255 }
256 if ((flags & GRE_KP) == 0) {
257 printf("kpunset!");
258 return;
259 }
260 if (flags & GRE_sP) {
261 printf("spset!");
262 return;
263 }
264
265 if (flags & GRE_KP) {
266 u_int32_t k;
267
268 if (len < 4)
269 goto trunc;
270 k = EXTRACT_32BITS(bp);
271 printf("call %d ", k & 0xffff);
272 len -= 4;
273 bp += 4;
274 }
275
276 if (flags & GRE_SP) {
277 if (len < 4)
278 goto trunc;
279 printf("seq %u ", EXTRACT_32BITS(bp));
280 bp += 4;
281 len -= 4;
282 }
283
284 if (flags & GRE_AP) {
285 if (len < 4)
286 goto trunc;
287 printf("ack %u ", EXTRACT_32BITS(bp));
288 bp += 4;
289 len -= 4;
290 }
291
292 if ((flags & GRE_SP) == 0) {
293 printf("no-payload");
294 return;
295 }
296
297 switch (prot) {
298 case ETHERTYPE_PPP:
299 printf("gre-ppp-payload");
300 break;
301 default:
302 printf("gre-proto-0x%x", prot);
303 break;
304 }
305 return;
306
307 trunc:
308 printf("[|gre]");
309 }
310
311 void
312 gre_sre_print(u_int16_t af, u_int8_t sreoff, u_int8_t srelen,
313 const u_char *bp, u_int len)
314 {
315 switch (af) {
316 case GRESRE_IP:
317 printf("(rtaf=ip");
318 gre_sre_ip_print(sreoff, srelen, bp, len);
319 printf(") ");
320 break;
321 case GRESRE_ASN:
322 printf("(rtaf=asn");
323 gre_sre_asn_print(sreoff, srelen, bp, len);
324 printf(") ");
325 break;
326 default:
327 printf("(rtaf=0x%x) ", af);
328 }
329 }
330 void
331 gre_sre_ip_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
332 {
333 struct in_addr a;
334 const u_char *up = bp;
335
336 if (sreoff & 3) {
337 printf(" badoffset=%u", sreoff);
338 return;
339 }
340 if (srelen & 3) {
341 printf(" badlength=%u", srelen);
342 return;
343 }
344 if (sreoff >= srelen) {
345 printf(" badoff/len=%u/%u", sreoff, srelen);
346 return;
347 }
348
349 for (;;) {
350 if (len < 4 || srelen == 0)
351 return;
352
353 memcpy(&a, bp, sizeof(a));
354 printf(" %s%s",
355 ((bp - up) == sreoff) ? "*" : "",
356 inet_ntoa(a));
357
358 bp += 4;
359 len -= 4;
360 srelen -= 4;
361 }
362 }
363
364 void
365 gre_sre_asn_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
366 {
367 const u_char *up = bp;
368
369 if (sreoff & 1) {
370 printf(" badoffset=%u", sreoff);
371 return;
372 }
373 if (srelen & 1) {
374 printf(" badlength=%u", srelen);
375 return;
376 }
377 if (sreoff >= srelen) {
378 printf(" badoff/len=%u/%u", sreoff, srelen);
379 return;
380 }
381
382 for (;;) {
383 if (len < 2 || srelen == 0)
384 return;
385
386 printf(" %s%x",
387 ((bp - up) == sreoff) ? "*" : "",
388 EXTRACT_16BITS(bp));
389
390 bp += 2;
391 len -= 2;
392 srelen -= 2;
393 }
394 }