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