drm: const'ify ioctls table (v2)
[dragonfly.git] / contrib / tcpdump / print-gre.c
blobb6fa52236695daa3df5c19d5e8cddec89799bb0b
1 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
3 /*
4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
5 * All rights reserved.
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.
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.
35 * tcpdump filter for GRE - Generic Routing Encapsulation
36 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
39 #ifndef lint
40 static const char rcsid[] _U_ =
41 "@(#) $Header: /tcpdump/master/tcpdump/print-gre.c,v 1.28 2005-04-06 21:32:39 mcr Exp $ (LBL)";
42 #endif
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
48 #include <tcpdump-stdinc.h>
50 #include <stdio.h>
51 #include <string.h>
53 #include "interface.h"
54 #include "addrtoname.h"
55 #include "extract.h"
57 #include "ip.h"
58 #include "ethertype.h"
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 */
68 struct tok gre_flag_values[] = {
69 { GRE_CP, "checksum present"},
70 { GRE_RP, "routing present"},
71 { GRE_KP, "key present"},
72 { GRE_SP, "sequence# present"},
73 { GRE_sP, "source routing present"},
74 { GRE_RECRS, "recursion count"},
75 { GRE_AP, "ack present"},
76 { 0, NULL }
79 #define GRE_VERS_MASK 0x0007 /* protocol version */
81 /* source route entry types */
82 #define GRESRE_IP 0x0800 /* IP */
83 #define GRESRE_ASN 0xfffe /* ASN */
85 void gre_print_0(const u_char *, u_int);
86 void gre_print_1(const u_char *, u_int);
87 void gre_sre_print(u_int16_t, u_int8_t, u_int8_t, const u_char *, u_int);
88 void gre_sre_ip_print(u_int8_t, u_int8_t, const u_char *, u_int);
89 void gre_sre_asn_print(u_int8_t, u_int8_t, const u_char *, u_int);
91 void
92 gre_print(const u_char *bp, u_int length)
94 u_int len = length, vers;
96 if (len < 2) {
97 printf("[|gre]");
98 return;
100 vers = EXTRACT_16BITS(bp) & GRE_VERS_MASK;
101 printf("GREv%u",vers);
103 switch(vers) {
104 case 0:
105 gre_print_0(bp, len);
106 break;
107 case 1:
108 gre_print_1(bp, len);
109 break;
110 default:
111 printf(" ERROR: unknown-version");
112 break;
114 return;
118 void
119 gre_print_0(const u_char *bp, u_int length)
121 u_int len = length;
122 u_int16_t flags, prot;
124 flags = EXTRACT_16BITS(bp);
125 if (vflag)
126 printf(", Flags [%s]",
127 bittok2str(gre_flag_values,"none",flags));
129 len -= 2;
130 bp += 2;
132 if (len < 2)
133 goto trunc;
134 prot = EXTRACT_16BITS(bp);
135 len -= 2;
136 bp += 2;
138 if ((flags & GRE_CP) | (flags & GRE_RP)) {
139 if (len < 2)
140 goto trunc;
141 if (vflag)
142 printf(", sum 0x%x", EXTRACT_16BITS(bp));
143 bp += 2;
144 len -= 2;
146 if (len < 2)
147 goto trunc;
148 printf(", off 0x%x", EXTRACT_16BITS(bp));
149 bp += 2;
150 len -= 2;
153 if (flags & GRE_KP) {
154 if (len < 4)
155 goto trunc;
156 printf(", key=0x%x", EXTRACT_32BITS(bp));
157 bp += 4;
158 len -= 4;
161 if (flags & GRE_SP) {
162 if (len < 4)
163 goto trunc;
164 printf(", seq %u", EXTRACT_32BITS(bp));
165 bp += 4;
166 len -= 4;
169 if (flags & GRE_RP) {
170 for (;;) {
171 u_int16_t af;
172 u_int8_t sreoff;
173 u_int8_t srelen;
175 if (len < 4)
176 goto trunc;
177 af = EXTRACT_16BITS(bp);
178 sreoff = *(bp + 2);
179 srelen = *(bp + 3);
180 bp += 4;
181 len -= 4;
183 if (af == 0 && srelen == 0)
184 break;
186 gre_sre_print(af, sreoff, srelen, bp, len);
188 if (len < srelen)
189 goto trunc;
190 bp += srelen;
191 len -= srelen;
195 if (eflag)
196 printf(", proto %s (0x%04x)",
197 tok2str(ethertype_values,"unknown",prot),
198 prot);
200 printf(", length %u",length);
202 if (vflag < 1)
203 printf(": "); /* put in a colon as protocol demarc */
204 else
205 printf("\n\t"); /* if verbose go multiline */
207 switch (prot) {
208 case ETHERTYPE_IP:
209 ip_print(gndo, bp, len);
210 break;
211 #ifdef INET6
212 case ETHERTYPE_IPV6:
213 ip6_print(gndo, bp, len);
214 break;
215 #endif
216 case ETHERTYPE_MPLS:
217 mpls_print(bp, len);
218 break;
219 case ETHERTYPE_IPX:
220 ipx_print(bp, len);
221 break;
222 case ETHERTYPE_ATALK:
223 atalk_print(bp, len);
224 break;
225 case ETHERTYPE_GRE_ISO:
226 isoclns_print(bp, len, len);
227 break;
228 case ETHERTYPE_TEB:
229 ether_print(gndo, bp, len, len, NULL, NULL);
230 break;
231 default:
232 printf("gre-proto-0x%x", prot);
234 return;
236 trunc:
237 printf("[|gre]");
240 void
241 gre_print_1(const u_char *bp, u_int length)
243 u_int len = length;
244 u_int16_t flags, prot;
246 flags = EXTRACT_16BITS(bp);
247 len -= 2;
248 bp += 2;
250 if (vflag)
251 printf(", Flags [%s]",
252 bittok2str(gre_flag_values,"none",flags));
254 if (len < 2)
255 goto trunc;
256 prot = EXTRACT_16BITS(bp);
257 len -= 2;
258 bp += 2;
261 if (flags & GRE_KP) {
262 u_int32_t k;
264 if (len < 4)
265 goto trunc;
266 k = EXTRACT_32BITS(bp);
267 printf(", call %d", k & 0xffff);
268 len -= 4;
269 bp += 4;
272 if (flags & GRE_SP) {
273 if (len < 4)
274 goto trunc;
275 printf(", seq %u", EXTRACT_32BITS(bp));
276 bp += 4;
277 len -= 4;
280 if (flags & GRE_AP) {
281 if (len < 4)
282 goto trunc;
283 printf(", ack %u", EXTRACT_32BITS(bp));
284 bp += 4;
285 len -= 4;
288 if ((flags & GRE_SP) == 0)
289 printf(", no-payload");
291 if (eflag)
292 printf(", proto %s (0x%04x)",
293 tok2str(ethertype_values,"unknown",prot),
294 prot);
296 printf(", length %u",length);
298 if ((flags & GRE_SP) == 0)
299 return;
301 if (vflag < 1)
302 printf(": "); /* put in a colon as protocol demarc */
303 else
304 printf("\n\t"); /* if verbose go multiline */
306 switch (prot) {
307 case ETHERTYPE_PPP:
308 ppp_print(bp, len);
309 break;
310 default:
311 printf("gre-proto-0x%x", prot);
312 break;
314 return;
316 trunc:
317 printf("[|gre]");
320 void
321 gre_sre_print(u_int16_t af, u_int8_t sreoff, u_int8_t srelen,
322 const u_char *bp, u_int len)
324 switch (af) {
325 case GRESRE_IP:
326 printf(", (rtaf=ip");
327 gre_sre_ip_print(sreoff, srelen, bp, len);
328 printf(") ");
329 break;
330 case GRESRE_ASN:
331 printf(", (rtaf=asn");
332 gre_sre_asn_print(sreoff, srelen, bp, len);
333 printf(") ");
334 break;
335 default:
336 printf(", (rtaf=0x%x) ", af);
339 void
340 gre_sre_ip_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
342 struct in_addr a;
343 const u_char *up = bp;
345 if (sreoff & 3) {
346 printf(", badoffset=%u", sreoff);
347 return;
349 if (srelen & 3) {
350 printf(", badlength=%u", srelen);
351 return;
353 if (sreoff >= srelen) {
354 printf(", badoff/len=%u/%u", sreoff, srelen);
355 return;
358 for (;;) {
359 if (len < 4 || srelen == 0)
360 return;
362 memcpy(&a, bp, sizeof(a));
363 printf(" %s%s",
364 ((bp - up) == sreoff) ? "*" : "",
365 inet_ntoa(a));
367 bp += 4;
368 len -= 4;
369 srelen -= 4;
373 void
374 gre_sre_asn_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
376 const u_char *up = bp;
378 if (sreoff & 1) {
379 printf(", badoffset=%u", sreoff);
380 return;
382 if (srelen & 1) {
383 printf(", badlength=%u", srelen);
384 return;
386 if (sreoff >= srelen) {
387 printf(", badoff/len=%u/%u", sreoff, srelen);
388 return;
391 for (;;) {
392 if (len < 2 || srelen == 0)
393 return;
395 printf(" %s%x",
396 ((bp - up) == sreoff) ? "*" : "",
397 EXTRACT_16BITS(bp));
399 bp += 2;
400 len -= 2;
401 srelen -= 2;