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