Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / print-gre.c
blob13efbca1ce542391029d1e80936062afda4cdcf8
1 /* $NetBSD$ */
3 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
5 /*
6 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Jason L. Wright
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * tcpdump filter for GRE - Generic Routing Encapsulation
38 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
41 #include <sys/cdefs.h>
42 #ifndef lint
43 #if 0
44 static const char rcsid[] _U_ =
45 "@(#) Header: /tcpdump/master/tcpdump/print-gre.c,v 1.28 2005/04/06 21:32:39 mcr Exp (LBL)";
46 #else
47 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
48 #endif
49 #endif
51 #ifdef HAVE_CONFIG_H
52 #include "config.h"
53 #endif
55 #include <tcpdump-stdinc.h>
57 #include <stdio.h>
58 #include <string.h>
60 #include "interface.h"
61 #include "addrtoname.h"
62 #include "extract.h"
64 #include "ip.h"
65 #include "ethertype.h"
67 #define GRE_CP 0x8000 /* checksum present */
68 #define GRE_RP 0x4000 /* routing present */
69 #define GRE_KP 0x2000 /* key present */
70 #define GRE_SP 0x1000 /* sequence# present */
71 #define GRE_sP 0x0800 /* source routing */
72 #define GRE_RECRS 0x0700 /* recursion count */
73 #define GRE_AP 0x0080 /* acknowledgment# present */
75 struct tok gre_flag_values[] = {
76 { GRE_CP, "checksum present"},
77 { GRE_RP, "routing present"},
78 { GRE_KP, "key present"},
79 { GRE_SP, "sequence# present"},
80 { GRE_sP, "source routing present"},
81 { GRE_RECRS, "recursion count"},
82 { GRE_AP, "ack present"},
83 { 0, NULL }
86 #define GRE_VERS_MASK 0x0007 /* protocol version */
88 /* source route entry types */
89 #define GRESRE_IP 0x0800 /* IP */
90 #define GRESRE_ASN 0xfffe /* ASN */
92 void gre_print_0(const u_char *, u_int);
93 void gre_print_1(const u_char *, u_int);
94 void gre_sre_print(u_int16_t, u_int8_t, u_int8_t, const u_char *, u_int);
95 void gre_sre_ip_print(u_int8_t, u_int8_t, const u_char *, u_int);
96 void gre_sre_asn_print(u_int8_t, u_int8_t, const u_char *, u_int);
98 void
99 gre_print(const u_char *bp, u_int length)
101 u_int len = length, vers;
103 if (len < 2) {
104 printf("[|gre]");
105 return;
107 vers = EXTRACT_16BITS(bp) & GRE_VERS_MASK;
108 printf("GREv%u",vers);
110 switch(vers) {
111 case 0:
112 gre_print_0(bp, len);
113 break;
114 case 1:
115 gre_print_1(bp, len);
116 break;
117 default:
118 printf(" ERROR: unknown-version");
119 break;
121 return;
125 void
126 gre_print_0(const u_char *bp, u_int length)
128 u_int len = length;
129 u_int16_t flags, prot;
131 flags = EXTRACT_16BITS(bp);
132 if (vflag)
133 printf(", Flags [%s]",
134 bittok2str(gre_flag_values,"none",flags));
136 len -= 2;
137 bp += 2;
139 if (len < 2)
140 goto trunc;
141 prot = EXTRACT_16BITS(bp);
142 len -= 2;
143 bp += 2;
145 if ((flags & GRE_CP) | (flags & GRE_RP)) {
146 if (len < 2)
147 goto trunc;
148 if (vflag)
149 printf(", sum 0x%x", EXTRACT_16BITS(bp));
150 bp += 2;
151 len -= 2;
153 if (len < 2)
154 goto trunc;
155 printf(", off 0x%x", EXTRACT_16BITS(bp));
156 bp += 2;
157 len -= 2;
160 if (flags & GRE_KP) {
161 if (len < 4)
162 goto trunc;
163 printf(", key=0x%x", EXTRACT_32BITS(bp));
164 bp += 4;
165 len -= 4;
168 if (flags & GRE_SP) {
169 if (len < 4)
170 goto trunc;
171 printf(", seq %u", EXTRACT_32BITS(bp));
172 bp += 4;
173 len -= 4;
176 if (flags & GRE_RP) {
177 for (;;) {
178 u_int16_t af;
179 u_int8_t sreoff;
180 u_int8_t srelen;
182 if (len < 4)
183 goto trunc;
184 af = EXTRACT_16BITS(bp);
185 sreoff = *(bp + 2);
186 srelen = *(bp + 3);
187 bp += 4;
188 len -= 4;
190 if (af == 0 && srelen == 0)
191 break;
193 gre_sre_print(af, sreoff, srelen, bp, len);
195 if (len < srelen)
196 goto trunc;
197 bp += srelen;
198 len -= srelen;
202 if (eflag)
203 printf(", proto %s (0x%04x)",
204 tok2str(ethertype_values,"unknown",prot),
205 prot);
207 printf(", length %u",length);
209 if (vflag < 1)
210 printf(": "); /* put in a colon as protocol demarc */
211 else
212 printf("\n\t"); /* if verbose go multiline */
214 switch (prot) {
215 case ETHERTYPE_IP:
216 ip_print(gndo, bp, len);
217 break;
218 #ifdef INET6
219 case ETHERTYPE_IPV6:
220 ip6_print(bp, len);
221 break;
222 #endif
223 case ETHERTYPE_MPLS:
224 mpls_print(bp, len);
225 break;
226 case ETHERTYPE_IPX:
227 ipx_print(bp, len);
228 break;
229 case ETHERTYPE_ATALK:
230 atalk_print(bp, len);
231 break;
232 case ETHERTYPE_GRE_ISO:
233 isoclns_print(bp, len, len);
234 break;
235 default:
236 printf("gre-proto-0x%x", prot);
238 return;
240 trunc:
241 printf("[|gre]");
244 void
245 gre_print_1(const u_char *bp, u_int length)
247 u_int len = length;
248 u_int16_t flags, prot;
250 flags = EXTRACT_16BITS(bp);
251 len -= 2;
252 bp += 2;
254 if (vflag)
255 printf(", Flags [%s]",
256 bittok2str(gre_flag_values,"none",flags));
258 if (len < 2)
259 goto trunc;
260 prot = EXTRACT_16BITS(bp);
261 len -= 2;
262 bp += 2;
265 if (flags & GRE_KP) {
266 u_int32_t k;
268 if (len < 4)
269 goto trunc;
270 k = EXTRACT_32BITS(bp);
271 printf(", call %d", k & 0xffff);
272 len -= 4;
273 bp += 4;
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;
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;
292 if ((flags & GRE_SP) == 0)
293 printf(", no-payload");
295 if (eflag)
296 printf(", proto %s (0x%04x)",
297 tok2str(ethertype_values,"unknown",prot),
298 prot);
300 printf(", length %u",length);
302 if ((flags & GRE_SP) == 0)
303 return;
305 if (vflag < 1)
306 printf(": "); /* put in a colon as protocol demarc */
307 else
308 printf("\n\t"); /* if verbose go multiline */
310 switch (prot) {
311 case ETHERTYPE_PPP:
312 ppp_print(bp, len);
313 break;
314 default:
315 printf("gre-proto-0x%x", prot);
316 break;
318 return;
320 trunc:
321 printf("[|gre]");
324 void
325 gre_sre_print(u_int16_t af, u_int8_t sreoff, u_int8_t srelen,
326 const u_char *bp, u_int len)
328 switch (af) {
329 case GRESRE_IP:
330 printf(", (rtaf=ip");
331 gre_sre_ip_print(sreoff, srelen, bp, len);
332 printf(") ");
333 break;
334 case GRESRE_ASN:
335 printf(", (rtaf=asn");
336 gre_sre_asn_print(sreoff, srelen, bp, len);
337 printf(") ");
338 break;
339 default:
340 printf(", (rtaf=0x%x) ", af);
343 void
344 gre_sre_ip_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
346 struct in_addr a;
347 const u_char *up = bp;
349 if (sreoff & 3) {
350 printf(", badoffset=%u", sreoff);
351 return;
353 if (srelen & 3) {
354 printf(", badlength=%u", srelen);
355 return;
357 if (sreoff >= srelen) {
358 printf(", badoff/len=%u/%u", sreoff, srelen);
359 return;
362 for (;;) {
363 if (len < 4 || srelen == 0)
364 return;
366 memcpy(&a, bp, sizeof(a));
367 printf(" %s%s",
368 ((bp - up) == sreoff) ? "*" : "",
369 inet_ntoa(a));
371 bp += 4;
372 len -= 4;
373 srelen -= 4;
377 void
378 gre_sre_asn_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
380 const u_char *up = bp;
382 if (sreoff & 1) {
383 printf(", badoffset=%u", sreoff);
384 return;
386 if (srelen & 1) {
387 printf(", badlength=%u", srelen);
388 return;
390 if (sreoff >= srelen) {
391 printf(", badoff/len=%u/%u", sreoff, srelen);
392 return;
395 for (;;) {
396 if (len < 2 || srelen == 0)
397 return;
399 printf(" %s%x",
400 ((bp - up) == sreoff) ? "*" : "",
401 EXTRACT_16BITS(bp));
403 bp += 2;
404 len -= 2;
405 srelen -= 2;