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);
204 ip6_print(ndo, bp, len);
207 mpls_print(ndo, bp, len);
210 ipx_print(ndo, bp, len);
212 case ETHERTYPE_ATALK:
213 atalk_print(ndo, bp, len);
215 case ETHERTYPE_GRE_ISO:
216 isoclns_print(ndo, bp, len, len);
219 ether_print(ndo, bp, len, len, NULL, NULL);
222 ND_PRINT((ndo, "gre-proto-0x%x", prot));
227 ND_PRINT((ndo, "%s", tstr));
231 gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)
234 uint16_t flags, prot;
236 flags = EXTRACT_16BITS(bp);
241 ND_PRINT((ndo, ", Flags [%s]",
242 bittok2str(gre_flag_values,"none",flags)));
246 prot = EXTRACT_16BITS(bp);
251 if (flags & GRE_KP) {
256 k = EXTRACT_32BITS(bp);
257 ND_PRINT((ndo, ", call %d", k & 0xffff));
262 if (flags & GRE_SP) {
265 ND_PRINT((ndo, ", seq %u", EXTRACT_32BITS(bp)));
270 if (flags & GRE_AP) {
273 ND_PRINT((ndo, ", ack %u", EXTRACT_32BITS(bp)));
278 if ((flags & GRE_SP) == 0)
279 ND_PRINT((ndo, ", no-payload"));
282 ND_PRINT((ndo, ", proto %s (0x%04x)",
283 tok2str(ethertype_values,"unknown",prot),
286 ND_PRINT((ndo, ", length %u",length));
288 if ((flags & GRE_SP) == 0)
291 if (ndo->ndo_vflag < 1)
292 ND_PRINT((ndo, ": ")); /* put in a colon as protocol demarc */
294 ND_PRINT((ndo, "\n\t")); /* if verbose go multiline */
298 ppp_print(ndo, bp, len);
301 ND_PRINT((ndo, "gre-proto-0x%x", prot));
307 ND_PRINT((ndo, "%s", tstr));
311 gre_sre_print(netdissect_options *ndo, uint16_t af, uint8_t sreoff,
312 uint8_t srelen, const u_char *bp, u_int len)
316 ND_PRINT((ndo, ", (rtaf=ip"));
317 gre_sre_ip_print(ndo, sreoff, srelen, bp, len);
318 ND_PRINT((ndo, ") "));
321 ND_PRINT((ndo, ", (rtaf=asn"));
322 gre_sre_asn_print(ndo, sreoff, srelen, bp, len);
323 ND_PRINT((ndo, ") "));
326 ND_PRINT((ndo, ", (rtaf=0x%x) ", af));
331 gre_sre_ip_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
332 const u_char *bp, u_int len)
335 const u_char *up = bp;
338 ND_PRINT((ndo, ", badoffset=%u", sreoff));
342 ND_PRINT((ndo, ", badlength=%u", srelen));
345 if (sreoff >= srelen) {
346 ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
351 if (len < 4 || srelen == 0)
354 memcpy(&a, bp, sizeof(a));
355 ND_PRINT((ndo, " %s%s",
356 ((bp - up) == sreoff) ? "*" : "",
366 gre_sre_asn_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
367 const u_char *bp, u_int len)
369 const u_char *up = bp;
372 ND_PRINT((ndo, ", badoffset=%u", sreoff));
376 ND_PRINT((ndo, ", badlength=%u", srelen));
379 if (sreoff >= srelen) {
380 ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
385 if (len < 2 || srelen == 0)
388 ND_PRINT((ndo, " %s%x",
389 ((bp - up) == sreoff) ? "*" : "",
390 EXTRACT_16BITS(bp)));