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 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: print-gre.c,v 1.6 2015/03/31 21:59:35 christos Exp $");
44 #define NETDISSECT_REWORKED
49 #include <tcpdump-stdinc.h>
53 #include "interface.h"
55 #include "ethertype.h"
57 static const char tstr
[] = "[|gre]";
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 */
67 static const struct tok gre_flag_values
[] = {
68 { GRE_CP
, "checksum present"},
69 { GRE_RP
, "routing present"},
70 { GRE_KP
, "key present"},
71 { GRE_SP
, "sequence# present"},
72 { GRE_sP
, "source routing present"},
73 { GRE_RECRS
, "recursion count"},
74 { GRE_AP
, "ack present"},
78 #define GRE_VERS_MASK 0x0007 /* protocol version */
80 /* source route entry types */
81 #define GRESRE_IP 0x0800 /* IP */
82 #define GRESRE_ASN 0xfffe /* ASN */
84 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
85 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
86 static void gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
87 static void gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
88 static void gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
91 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
93 u_int len
= length
, vers
;
96 ND_PRINT((ndo
, "%s", tstr
));
99 vers
= EXTRACT_16BITS(bp
) & GRE_VERS_MASK
;
100 ND_PRINT((ndo
, "GREv%u",vers
));
104 gre_print_0(ndo
, bp
, len
);
107 gre_print_1(ndo
, bp
, len
);
110 ND_PRINT((ndo
, " ERROR: unknown-version"));
116 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
119 uint16_t flags
, prot
;
121 flags
= EXTRACT_16BITS(bp
);
123 ND_PRINT((ndo
, ", Flags [%s]",
124 bittok2str(gre_flag_values
,"none",flags
)));
131 prot
= EXTRACT_16BITS(bp
);
135 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
139 ND_PRINT((ndo
, ", sum 0x%x", EXTRACT_16BITS(bp
)));
145 ND_PRINT((ndo
, ", off 0x%x", EXTRACT_16BITS(bp
)));
150 if (flags
& GRE_KP
) {
153 ND_PRINT((ndo
, ", key=0x%x", EXTRACT_32BITS(bp
)));
158 if (flags
& GRE_SP
) {
161 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
166 if (flags
& GRE_RP
) {
174 af
= EXTRACT_16BITS(bp
);
180 if (af
== 0 && srelen
== 0)
183 gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
);
193 ND_PRINT((ndo
, ", proto %s (0x%04x)",
194 tok2str(ethertype_values
,"unknown",prot
),
197 ND_PRINT((ndo
, ", length %u",length
));
199 if (ndo
->ndo_vflag
< 1)
200 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
202 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
206 ip_print(ndo
, bp
, len
);
209 ip6_print(ndo
, bp
, len
);
212 mpls_print(ndo
, bp
, len
);
215 ipx_print(ndo
, bp
, len
);
217 case ETHERTYPE_ATALK
:
218 atalk_print(ndo
, bp
, len
);
220 case ETHERTYPE_GRE_ISO
:
221 isoclns_print(ndo
, bp
, len
, len
);
224 ether_print(ndo
, bp
, len
, len
, NULL
, NULL
);
227 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
232 ND_PRINT((ndo
, "%s", tstr
));
236 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
239 uint16_t flags
, prot
;
241 flags
= EXTRACT_16BITS(bp
);
246 ND_PRINT((ndo
, ", Flags [%s]",
247 bittok2str(gre_flag_values
,"none",flags
)));
251 prot
= EXTRACT_16BITS(bp
);
256 if (flags
& GRE_KP
) {
261 k
= EXTRACT_32BITS(bp
);
262 ND_PRINT((ndo
, ", call %d", k
& 0xffff));
267 if (flags
& GRE_SP
) {
270 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
275 if (flags
& GRE_AP
) {
278 ND_PRINT((ndo
, ", ack %u", EXTRACT_32BITS(bp
)));
283 if ((flags
& GRE_SP
) == 0)
284 ND_PRINT((ndo
, ", no-payload"));
287 ND_PRINT((ndo
, ", proto %s (0x%04x)",
288 tok2str(ethertype_values
,"unknown",prot
),
291 ND_PRINT((ndo
, ", length %u",length
));
293 if ((flags
& GRE_SP
) == 0)
296 if (ndo
->ndo_vflag
< 1)
297 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
299 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
303 ppp_print(ndo
, bp
, len
);
306 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
312 ND_PRINT((ndo
, "%s", tstr
));
316 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
317 uint8_t srelen
, const u_char
*bp
, u_int len
)
321 ND_PRINT((ndo
, ", (rtaf=ip"));
322 gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
323 ND_PRINT((ndo
, ") "));
326 ND_PRINT((ndo
, ", (rtaf=asn"));
327 gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
328 ND_PRINT((ndo
, ") "));
331 ND_PRINT((ndo
, ", (rtaf=0x%x) ", af
));
336 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
337 const u_char
*bp
, u_int len
)
340 const u_char
*up
= bp
;
343 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
347 ND_PRINT((ndo
, ", badlength=%u", srelen
));
350 if (sreoff
>= srelen
) {
351 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
356 if (len
< 4 || srelen
== 0)
359 memcpy(&a
, bp
, sizeof(a
));
360 ND_PRINT((ndo
, " %s%s",
361 ((bp
- up
) == sreoff
) ? "*" : "",
371 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
372 const u_char
*bp
, u_int len
)
374 const u_char
*up
= bp
;
377 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
381 ND_PRINT((ndo
, ", badlength=%u", srelen
));
384 if (sreoff
>= srelen
) {
385 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
390 if (len
< 2 || srelen
== 0)
393 ND_PRINT((ndo
, " %s%x",
394 ((bp
- up
) == sreoff
) ? "*" : "",
395 EXTRACT_16BITS(bp
)));