]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
rewrite with better license (explicit "with or without modification").
[tcpdump] / print-gre.c
1 /* $OpenBSD: print-gre.c,v 1.5 2002/09/18 20:40:06 jason 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[] =
41 "@(#) $Header: /tcpdump/master/tcpdump/print-gre.c,v 1.17 2002-09-20 22:42:21 itojun 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
59 #define GRE_CP 0x8000 /* checksum present */
60 #define GRE_RP 0x4000 /* routing present */
61 #define GRE_KP 0x2000 /* key present */
62 #define GRE_SP 0x1000 /* sequence# present */
63 #define GRE_sP 0x0800 /* source routing */
64 #define GRE_RECRS 0x0700 /* recursion count */
65 #define GRE_AP 0x0080 /* acknowledgment# present */
66 #define GRE_VERS 0x0007 /* protocol version */
67
68 #define GREPROTO_IP 0x0800 /* IP */
69 #define GREPROTO_PPP 0x880b /* PPTP */
70
71 /* source route entry types */
72 #define GRESRE_IP 0x0800 /* IP */
73 #define GRESRE_ASN 0xfffe /* ASN */
74
75 void gre_print_0(const u_char *, u_int);
76 void gre_print_1(const u_char *, u_int);
77 void gre_sre_print(u_int16_t, u_int8_t, u_int8_t, const u_char *, u_int);
78 void gre_sre_ip_print(u_int8_t, u_int8_t, const u_char *, u_int);
79 void gre_sre_asn_print(u_int8_t, u_int8_t, const u_char *, u_int);
80
81 void
82 gre_print(const u_char *bp, u_int length)
83 {
84 u_int len = length, vers;
85
86 if (len < 2) {
87 printf("[|gre]");
88 return;
89 }
90 vers = EXTRACT_16BITS(bp) & 7;
91
92 if (vers == 0)
93 gre_print_0(bp, len);
94 else if (vers == 1)
95 gre_print_1(bp, len);
96 else
97 printf("gre-unknown-version=%u", vers);
98 return;
99
100 }
101
102 void
103 gre_print_0(const u_char *bp, u_int length)
104 {
105 u_int len = length;
106 u_int16_t flags, prot;
107
108 flags = EXTRACT_16BITS(bp);
109 if (vflag) {
110 printf("[%s%s%s%s%s] ",
111 (flags & GRE_CP) ? "C" : "",
112 (flags & GRE_RP) ? "R" : "",
113 (flags & GRE_KP) ? "K" : "",
114 (flags & GRE_SP) ? "S" : "",
115 (flags & GRE_sP) ? "s" : "");
116 }
117
118 len -= 2;
119 bp += 2;
120
121 if (len < 2)
122 goto trunc;
123 prot = EXTRACT_16BITS(bp);
124 len -= 2;
125 bp += 2;
126
127 if ((flags & GRE_CP) | (flags & GRE_RP)) {
128 if (len < 2)
129 goto trunc;
130 if (vflag)
131 printf("sum 0x%x ", EXTRACT_16BITS(bp));
132 bp += 2;
133 len -= 2;
134
135 if (len < 2)
136 goto trunc;
137 printf("off 0x%x ", EXTRACT_16BITS(bp));
138 bp += 2;
139 len -= 2;
140 }
141
142 if (flags & GRE_KP) {
143 if (len < 4)
144 goto trunc;
145 printf("key=0x%x ", EXTRACT_32BITS(bp));
146 bp += 4;
147 len -= 4;
148 }
149
150 if (flags & GRE_SP) {
151 if (len < 4)
152 goto trunc;
153 printf("seq=0x%x ", EXTRACT_32BITS(bp));
154 bp += 4;
155 len -= 4;
156 }
157
158 if (flags & GRE_RP) {
159 for (;;) {
160 u_int16_t af;
161 u_int8_t sreoff;
162 u_int8_t srelen;
163
164 if (len < 4)
165 goto trunc;
166 af = EXTRACT_16BITS(bp);
167 sreoff = *(bp + 2);
168 srelen = *(bp + 3);
169 bp += 4;
170 len -= 4;
171
172 if (af == 0 && srelen == 0)
173 break;
174
175 gre_sre_print(af, sreoff, srelen, bp, len);
176
177 if (len < srelen)
178 goto trunc;
179 bp += srelen;
180 len -= srelen;
181 }
182 }
183
184 switch (prot) {
185 case GREPROTO_IP:
186 ip_print(bp, len);
187 break;
188 default:
189 printf("gre-proto-0x%x", prot);
190 }
191 return;
192
193 trunc:
194 printf("[|gre]");
195 }
196
197 void
198 gre_print_1(const u_char *bp, u_int length)
199 {
200 u_int len = length;
201 u_int16_t flags, prot;
202
203 flags = EXTRACT_16BITS(bp);
204 len -= 2;
205 bp += 2;
206
207 if (vflag) {
208 printf("[%s%s%s%s%s%s] ",
209 (flags & GRE_CP) ? "C" : "",
210 (flags & GRE_RP) ? "R" : "",
211 (flags & GRE_KP) ? "K" : "",
212 (flags & GRE_SP) ? "S" : "",
213 (flags & GRE_sP) ? "s" : "",
214 (flags & GRE_AP) ? "A" : "");
215 }
216
217 if (len < 2)
218 goto trunc;
219 prot = EXTRACT_16BITS(bp);
220 len -= 2;
221 bp += 2;
222
223 if (flags & GRE_CP) {
224 printf("cpset! ");
225 return;
226 }
227 if (flags & GRE_RP) {
228 printf("rpset! ");
229 return;
230 }
231 if ((flags & GRE_KP) != 0) {
232 printf("kpunset! ");
233 return;
234 }
235 if (flags & GRE_sP) {
236 printf("spset! ");
237 return;
238 }
239
240 if (flags & GRE_KP) {
241 u_int32_t k;
242
243 if (len < 4)
244 goto trunc;
245 k = EXTRACT_32BITS(bp);
246 printf("key=0x%x call=0x%x ", (k >> 16) & 0xffff, k & 0xffff);
247 len -= 4;
248 bp += 4;
249 }
250
251 if (flags & GRE_SP) {
252 if (len < 4)
253 goto trunc;
254 printf("seq=0x%x ", EXTRACT_32BITS(bp));
255 bp += 4;
256 len -= 4;
257 }
258
259 if (flags & GRE_AP) {
260 if (len < 4)
261 goto trunc;
262 printf("ack=0x%x ", EXTRACT_32BITS(bp));
263 bp += 4;
264 len -= 4;
265 }
266
267 if ((flags & GRE_SP) == 0) {
268 printf("no-payload ");
269 return;
270 }
271
272 switch (prot) {
273 case GREPROTO_PPP:
274 printf("gre-ppp-payload ");
275 break;
276 default:
277 printf("gre-proto-0x%x ", prot);
278 break;
279 }
280 return;
281
282 trunc:
283 printf("[|gre]");
284 }
285
286 void
287 gre_sre_print(u_int16_t af, u_int8_t sreoff, u_int8_t srelen,
288 const u_char *bp, u_int len)
289 {
290 switch (af) {
291 case GRESRE_IP:
292 printf("(rtaf=ip");
293 gre_sre_ip_print(sreoff, srelen, bp, len);
294 printf(") ");
295 break;
296 case GRESRE_ASN:
297 printf("(rtaf=asn");
298 gre_sre_asn_print(sreoff, srelen, bp, len);
299 printf(") ");
300 break;
301 default:
302 printf("(rtaf=0x%x) ", af);
303 }
304 }
305 void
306 gre_sre_ip_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
307 {
308 struct in_addr a;
309 const u_char *up = bp;
310
311 if (sreoff & 3) {
312 printf(" badoffset=%u", sreoff);
313 return;
314 }
315 if (srelen & 3) {
316 printf(" badlength=%u", srelen);
317 return;
318 }
319 if (sreoff >= srelen) {
320 printf(" badoff/len=%u/%u", sreoff, srelen);
321 return;
322 }
323
324 for (;;) {
325 if (len < 4 || srelen == 0)
326 return;
327
328 memcpy(&a, bp, sizeof(a));
329 printf(" %s%s",
330 ((bp - up) == sreoff) ? "*" : "",
331 inet_ntoa(a));
332
333 bp += 4;
334 len -= 4;
335 srelen -= 4;
336 }
337 }
338
339 void
340 gre_sre_asn_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
341 {
342 const u_char *up = bp;
343
344 if (sreoff & 1) {
345 printf(" badoffset=%u", sreoff);
346 return;
347 }
348 if (srelen & 1) {
349 printf(" badlength=%u", srelen);
350 return;
351 }
352 if (sreoff >= srelen) {
353 printf(" badoff/len=%u/%u", sreoff, srelen);
354 return;
355 }
356
357 for (;;) {
358 if (len < 2 || srelen == 0)
359 return;
360
361 printf(" %s%x",
362 ((bp - up) == sreoff) ? "*" : "",
363 EXTRACT_16BITS(bp));
364
365 bp += 2;
366 len -= 2;
367 srelen -= 2;
368 }
369 }