]>
The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
1 /* $OpenBSD: print-gre.c,v 1.6 2002/10/30 03:04:04 fgsch Exp $ */
4 * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 #define NETDISSECT_REWORKED
44 #include <tcpdump-stdinc.h>
48 #include "interface.h"
50 #include "ethertype.h"
52 static const char tstr
[] = "[|gre]";
54 #define GRE_CP 0x8000 /* checksum present */
55 #define GRE_RP 0x4000 /* routing present */
56 #define GRE_KP 0x2000 /* key present */
57 #define GRE_SP 0x1000 /* sequence# present */
58 #define GRE_sP 0x0800 /* source routing */
59 #define GRE_RECRS 0x0700 /* recursion count */
60 #define GRE_AP 0x0080 /* acknowledgment# present */
62 static const struct tok gre_flag_values
[] = {
63 { GRE_CP
, "checksum present"},
64 { GRE_RP
, "routing present"},
65 { GRE_KP
, "key present"},
66 { GRE_SP
, "sequence# present"},
67 { GRE_sP
, "source routing present"},
68 { GRE_RECRS
, "recursion count"},
69 { GRE_AP
, "ack present"},
73 #define GRE_VERS_MASK 0x0007 /* protocol version */
75 /* source route entry types */
76 #define GRESRE_IP 0x0800 /* IP */
77 #define GRESRE_ASN 0xfffe /* ASN */
79 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
80 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
81 static void gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
82 static void gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
83 static void gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
86 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
88 u_int len
= length
, vers
;
91 ND_PRINT((ndo
, "%s", tstr
));
94 vers
= EXTRACT_16BITS(bp
) & GRE_VERS_MASK
;
95 ND_PRINT((ndo
, "GREv%u",vers
));
99 gre_print_0(ndo
, bp
, len
);
102 gre_print_1(ndo
, bp
, len
);
105 ND_PRINT((ndo
, " ERROR: unknown-version"));
111 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
114 uint16_t flags
, prot
;
116 flags
= EXTRACT_16BITS(bp
);
118 ND_PRINT((ndo
, ", Flags [%s]",
119 bittok2str(gre_flag_values
,"none",flags
)));
126 prot
= EXTRACT_16BITS(bp
);
130 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
134 ND_PRINT((ndo
, ", sum 0x%x", EXTRACT_16BITS(bp
)));
140 ND_PRINT((ndo
, ", off 0x%x", EXTRACT_16BITS(bp
)));
145 if (flags
& GRE_KP
) {
148 ND_PRINT((ndo
, ", key=0x%x", EXTRACT_32BITS(bp
)));
153 if (flags
& GRE_SP
) {
156 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
161 if (flags
& GRE_RP
) {
169 af
= EXTRACT_16BITS(bp
);
175 if (af
== 0 && srelen
== 0)
178 gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
);
188 ND_PRINT((ndo
, ", proto %s (0x%04x)",
189 tok2str(ethertype_values
,"unknown",prot
),
192 ND_PRINT((ndo
, ", length %u",length
));
194 if (ndo
->ndo_vflag
< 1)
195 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
197 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
201 ip_print(ndo
, bp
, len
);
205 ip6_print(ndo
, bp
, len
);
209 mpls_print(ndo
, bp
, len
);
212 ipx_print(ndo
, bp
, len
);
214 case ETHERTYPE_ATALK
:
215 atalk_print(ndo
, bp
, len
);
217 case ETHERTYPE_GRE_ISO
:
218 isoclns_print(ndo
, bp
, len
, len
);
221 ether_print(ndo
, bp
, len
, len
, NULL
, NULL
);
224 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
229 ND_PRINT((ndo
, "%s", tstr
));
233 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
236 uint16_t flags
, prot
;
238 flags
= EXTRACT_16BITS(bp
);
243 ND_PRINT((ndo
, ", Flags [%s]",
244 bittok2str(gre_flag_values
,"none",flags
)));
248 prot
= EXTRACT_16BITS(bp
);
253 if (flags
& GRE_KP
) {
258 k
= EXTRACT_32BITS(bp
);
259 ND_PRINT((ndo
, ", call %d", k
& 0xffff));
264 if (flags
& GRE_SP
) {
267 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
272 if (flags
& GRE_AP
) {
275 ND_PRINT((ndo
, ", ack %u", EXTRACT_32BITS(bp
)));
280 if ((flags
& GRE_SP
) == 0)
281 ND_PRINT((ndo
, ", no-payload"));
284 ND_PRINT((ndo
, ", proto %s (0x%04x)",
285 tok2str(ethertype_values
,"unknown",prot
),
288 ND_PRINT((ndo
, ", length %u",length
));
290 if ((flags
& GRE_SP
) == 0)
293 if (ndo
->ndo_vflag
< 1)
294 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
296 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
300 ppp_print(ndo
, bp
, len
);
303 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
309 ND_PRINT((ndo
, "%s", tstr
));
313 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
314 uint8_t srelen
, const u_char
*bp
, u_int len
)
318 ND_PRINT((ndo
, ", (rtaf=ip"));
319 gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
320 ND_PRINT((ndo
, ") "));
323 ND_PRINT((ndo
, ", (rtaf=asn"));
324 gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
325 ND_PRINT((ndo
, ") "));
328 ND_PRINT((ndo
, ", (rtaf=0x%x) ", af
));
333 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
334 const u_char
*bp
, u_int len
)
337 const u_char
*up
= bp
;
340 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
344 ND_PRINT((ndo
, ", badlength=%u", srelen
));
347 if (sreoff
>= srelen
) {
348 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
353 if (len
< 4 || srelen
== 0)
356 memcpy(&a
, bp
, sizeof(a
));
357 ND_PRINT((ndo
, " %s%s",
358 ((bp
- up
) == sreoff
) ? "*" : "",
368 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
369 const u_char
*bp
, u_int len
)
371 const u_char
*up
= bp
;
374 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
378 ND_PRINT((ndo
, ", badlength=%u", srelen
));
381 if (sreoff
>= srelen
) {
382 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
387 if (len
< 2 || srelen
== 0)
390 ND_PRINT((ndo
, " %s%x",
391 ((bp
- up
) == sreoff
) ? "*" : "",
392 EXTRACT_16BITS(bp
)));