]> The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
whitespace changes
[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.28 2005-04-06 21:32:39 mcr 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 static const char tstr[] = "[|gre]";
61
62 #define GRE_CP 0x8000 /* checksum present */
63 #define GRE_RP 0x4000 /* routing present */
64 #define GRE_KP 0x2000 /* key present */
65 #define GRE_SP 0x1000 /* sequence# present */
66 #define GRE_sP 0x0800 /* source routing */
67 #define GRE_RECRS 0x0700 /* recursion count */
68 #define GRE_AP 0x0080 /* acknowledgment# present */
69
70 static const struct tok gre_flag_values[] = {
71 { GRE_CP, "checksum present"},
72 { GRE_RP, "routing present"},
73 { GRE_KP, "key present"},
74 { GRE_SP, "sequence# present"},
75 { GRE_sP, "source routing present"},
76 { GRE_RECRS, "recursion count"},
77 { GRE_AP, "ack present"},
78 { 0, NULL }
79 };
80
81 #define GRE_VERS_MASK 0x0007 /* protocol version */
82
83 /* source route entry types */
84 #define GRESRE_IP 0x0800 /* IP */
85 #define GRESRE_ASN 0xfffe /* ASN */
86
87 void gre_print_0(const u_char *, u_int);
88 void gre_print_1(const u_char *, u_int);
89 void gre_sre_print(u_int16_t, u_int8_t, u_int8_t, const u_char *, u_int);
90 void gre_sre_ip_print(u_int8_t, u_int8_t, const u_char *, u_int);
91 void gre_sre_asn_print(u_int8_t, u_int8_t, const u_char *, u_int);
92
93 void
94 gre_print(const u_char *bp, u_int length)
95 {
96 u_int len = length, vers;
97
98 if (len < 2) {
99 printf("%s", tstr);
100 return;
101 }
102 vers = EXTRACT_16BITS(bp) & GRE_VERS_MASK;
103 printf("GREv%u",vers);
104
105 switch(vers) {
106 case 0:
107 gre_print_0(bp, len);
108 break;
109 case 1:
110 gre_print_1(bp, len);
111 break;
112 default:
113 printf(" ERROR: unknown-version");
114 break;
115 }
116 return;
117
118 }
119
120 void
121 gre_print_0(const u_char *bp, u_int length)
122 {
123 u_int len = length;
124 u_int16_t flags, prot;
125
126 flags = EXTRACT_16BITS(bp);
127 if (vflag)
128 printf(", Flags [%s]",
129 bittok2str(gre_flag_values,"none",flags));
130
131 len -= 2;
132 bp += 2;
133
134 if (len < 2)
135 goto trunc;
136 prot = EXTRACT_16BITS(bp);
137 len -= 2;
138 bp += 2;
139
140 if ((flags & GRE_CP) | (flags & GRE_RP)) {
141 if (len < 2)
142 goto trunc;
143 if (vflag)
144 printf(", sum 0x%x", EXTRACT_16BITS(bp));
145 bp += 2;
146 len -= 2;
147
148 if (len < 2)
149 goto trunc;
150 printf(", off 0x%x", EXTRACT_16BITS(bp));
151 bp += 2;
152 len -= 2;
153 }
154
155 if (flags & GRE_KP) {
156 if (len < 4)
157 goto trunc;
158 printf(", key=0x%x", EXTRACT_32BITS(bp));
159 bp += 4;
160 len -= 4;
161 }
162
163 if (flags & GRE_SP) {
164 if (len < 4)
165 goto trunc;
166 printf(", seq %u", EXTRACT_32BITS(bp));
167 bp += 4;
168 len -= 4;
169 }
170
171 if (flags & GRE_RP) {
172 for (;;) {
173 u_int16_t af;
174 u_int8_t sreoff;
175 u_int8_t srelen;
176
177 if (len < 4)
178 goto trunc;
179 af = EXTRACT_16BITS(bp);
180 sreoff = *(bp + 2);
181 srelen = *(bp + 3);
182 bp += 4;
183 len -= 4;
184
185 if (af == 0 && srelen == 0)
186 break;
187
188 gre_sre_print(af, sreoff, srelen, bp, len);
189
190 if (len < srelen)
191 goto trunc;
192 bp += srelen;
193 len -= srelen;
194 }
195 }
196
197 if (eflag)
198 printf(", proto %s (0x%04x)",
199 tok2str(ethertype_values,"unknown",prot),
200 prot);
201
202 printf(", length %u",length);
203
204 if (vflag < 1)
205 printf(": "); /* put in a colon as protocol demarc */
206 else
207 printf("\n\t"); /* if verbose go multiline */
208
209 switch (prot) {
210 case ETHERTYPE_IP:
211 ip_print(gndo, bp, len);
212 break;
213 #ifdef INET6
214 case ETHERTYPE_IPV6:
215 ip6_print(gndo, bp, len);
216 break;
217 #endif
218 case ETHERTYPE_MPLS:
219 mpls_print(bp, len);
220 break;
221 case ETHERTYPE_IPX:
222 ipx_print(bp, len);
223 break;
224 case ETHERTYPE_ATALK:
225 atalk_print(bp, len);
226 break;
227 case ETHERTYPE_GRE_ISO:
228 isoclns_print(bp, len, len);
229 break;
230 case ETHERTYPE_TEB:
231 ether_print(gndo, bp, len, len, NULL, NULL);
232 break;
233 default:
234 printf("gre-proto-0x%x", prot);
235 }
236 return;
237
238 trunc:
239 printf("%s", tstr);
240 }
241
242 void
243 gre_print_1(const u_char *bp, u_int length)
244 {
245 u_int len = length;
246 u_int16_t flags, prot;
247
248 flags = EXTRACT_16BITS(bp);
249 len -= 2;
250 bp += 2;
251
252 if (vflag)
253 printf(", Flags [%s]",
254 bittok2str(gre_flag_values,"none",flags));
255
256 if (len < 2)
257 goto trunc;
258 prot = EXTRACT_16BITS(bp);
259 len -= 2;
260 bp += 2;
261
262
263 if (flags & GRE_KP) {
264 u_int32_t k;
265
266 if (len < 4)
267 goto trunc;
268 k = EXTRACT_32BITS(bp);
269 printf(", call %d", k & 0xffff);
270 len -= 4;
271 bp += 4;
272 }
273
274 if (flags & GRE_SP) {
275 if (len < 4)
276 goto trunc;
277 printf(", seq %u", EXTRACT_32BITS(bp));
278 bp += 4;
279 len -= 4;
280 }
281
282 if (flags & GRE_AP) {
283 if (len < 4)
284 goto trunc;
285 printf(", ack %u", EXTRACT_32BITS(bp));
286 bp += 4;
287 len -= 4;
288 }
289
290 if ((flags & GRE_SP) == 0)
291 printf(", no-payload");
292
293 if (eflag)
294 printf(", proto %s (0x%04x)",
295 tok2str(ethertype_values,"unknown",prot),
296 prot);
297
298 printf(", length %u",length);
299
300 if ((flags & GRE_SP) == 0)
301 return;
302
303 if (vflag < 1)
304 printf(": "); /* put in a colon as protocol demarc */
305 else
306 printf("\n\t"); /* if verbose go multiline */
307
308 switch (prot) {
309 case ETHERTYPE_PPP:
310 ppp_print(bp, len);
311 break;
312 default:
313 printf("gre-proto-0x%x", prot);
314 break;
315 }
316 return;
317
318 trunc:
319 printf("%s", tstr);
320 }
321
322 void
323 gre_sre_print(u_int16_t af, u_int8_t sreoff, u_int8_t srelen,
324 const u_char *bp, u_int len)
325 {
326 switch (af) {
327 case GRESRE_IP:
328 printf(", (rtaf=ip");
329 gre_sre_ip_print(sreoff, srelen, bp, len);
330 printf(") ");
331 break;
332 case GRESRE_ASN:
333 printf(", (rtaf=asn");
334 gre_sre_asn_print(sreoff, srelen, bp, len);
335 printf(") ");
336 break;
337 default:
338 printf(", (rtaf=0x%x) ", af);
339 }
340 }
341 void
342 gre_sre_ip_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
343 {
344 struct in_addr a;
345 const u_char *up = bp;
346
347 if (sreoff & 3) {
348 printf(", badoffset=%u", sreoff);
349 return;
350 }
351 if (srelen & 3) {
352 printf(", badlength=%u", srelen);
353 return;
354 }
355 if (sreoff >= srelen) {
356 printf(", badoff/len=%u/%u", sreoff, srelen);
357 return;
358 }
359
360 for (;;) {
361 if (len < 4 || srelen == 0)
362 return;
363
364 memcpy(&a, bp, sizeof(a));
365 printf(" %s%s",
366 ((bp - up) == sreoff) ? "*" : "",
367 inet_ntoa(a));
368
369 bp += 4;
370 len -= 4;
371 srelen -= 4;
372 }
373 }
374
375 void
376 gre_sre_asn_print(u_int8_t sreoff, u_int8_t srelen, const u_char *bp, u_int len)
377 {
378 const u_char *up = bp;
379
380 if (sreoff & 1) {
381 printf(", badoffset=%u", sreoff);
382 return;
383 }
384 if (srelen & 1) {
385 printf(", badlength=%u", srelen);
386 return;
387 }
388 if (sreoff >= srelen) {
389 printf(", badoff/len=%u/%u", sreoff, srelen);
390 return;
391 }
392
393 for (;;) {
394 if (len < 2 || srelen == 0)
395 return;
396
397 printf(" %s%x",
398 ((bp - up) == sreoff) ? "*" : "",
399 EXTRACT_16BITS(bp));
400
401 bp += 2;
402 len -= 2;
403 srelen -= 2;
404 }
405 }