]>
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 * netdissect printer for GRE - Generic Routing Encapsulation
36 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
43 #include <netdissect-stdinc.h>
47 #include "netdissect.h"
49 #include "ethertype.h"
51 static const char tstr
[] = "[|gre]";
53 #define GRE_CP 0x8000 /* checksum present */
54 #define GRE_RP 0x4000 /* routing present */
55 #define GRE_KP 0x2000 /* key present */
56 #define GRE_SP 0x1000 /* sequence# present */
57 #define GRE_sP 0x0800 /* source routing */
58 #define GRE_RECRS 0x0700 /* recursion count */
59 #define GRE_AP 0x0080 /* acknowledgment# present */
61 static const struct tok gre_flag_values
[] = {
62 { GRE_CP
, "checksum present"},
63 { GRE_RP
, "routing present"},
64 { GRE_KP
, "key present"},
65 { GRE_SP
, "sequence# present"},
66 { GRE_sP
, "source routing present"},
67 { GRE_RECRS
, "recursion count"},
68 { GRE_AP
, "ack present"},
72 #define GRE_VERS_MASK 0x0007 /* protocol version */
74 /* source route entry types */
75 #define GRESRE_IP 0x0800 /* IP */
76 #define GRESRE_ASN 0xfffe /* ASN */
78 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
79 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
80 static void gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
81 static void gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
82 static void gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
85 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
87 u_int len
= length
, vers
;
90 ND_PRINT((ndo
, "%s", tstr
));
93 vers
= EXTRACT_16BITS(bp
) & GRE_VERS_MASK
;
94 ND_PRINT((ndo
, "GREv%u",vers
));
98 gre_print_0(ndo
, bp
, len
);
101 gre_print_1(ndo
, bp
, len
);
104 ND_PRINT((ndo
, " ERROR: unknown-version"));
110 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
113 uint16_t flags
, prot
;
115 flags
= EXTRACT_16BITS(bp
);
117 ND_PRINT((ndo
, ", Flags [%s]",
118 bittok2str(gre_flag_values
,"none",flags
)));
125 prot
= EXTRACT_16BITS(bp
);
129 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
133 ND_PRINT((ndo
, ", sum 0x%x", EXTRACT_16BITS(bp
)));
139 ND_PRINT((ndo
, ", off 0x%x", EXTRACT_16BITS(bp
)));
144 if (flags
& GRE_KP
) {
147 ND_PRINT((ndo
, ", key=0x%x", EXTRACT_32BITS(bp
)));
152 if (flags
& GRE_SP
) {
155 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
160 if (flags
& GRE_RP
) {
168 af
= EXTRACT_16BITS(bp
);
174 if (af
== 0 && srelen
== 0)
177 gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
);
187 ND_PRINT((ndo
, ", proto %s (0x%04x)",
188 tok2str(ethertype_values
,"unknown",prot
),
191 ND_PRINT((ndo
, ", length %u",length
));
193 if (ndo
->ndo_vflag
< 1)
194 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
196 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
200 ip_print(ndo
, bp
, len
);
203 ip6_print(ndo
, bp
, len
);
206 mpls_print(ndo
, bp
, len
);
209 ipx_print(ndo
, bp
, len
);
211 case ETHERTYPE_ATALK
:
212 atalk_print(ndo
, bp
, len
);
214 case ETHERTYPE_GRE_ISO
:
215 isoclns_print(ndo
, bp
, len
, len
);
218 ether_print(ndo
, bp
, len
, len
, NULL
, NULL
);
221 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
226 ND_PRINT((ndo
, "%s", tstr
));
230 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
233 uint16_t flags
, prot
;
235 flags
= EXTRACT_16BITS(bp
);
240 ND_PRINT((ndo
, ", Flags [%s]",
241 bittok2str(gre_flag_values
,"none",flags
)));
245 prot
= EXTRACT_16BITS(bp
);
250 if (flags
& GRE_KP
) {
255 k
= EXTRACT_32BITS(bp
);
256 ND_PRINT((ndo
, ", call %d", k
& 0xffff));
261 if (flags
& GRE_SP
) {
264 ND_PRINT((ndo
, ", seq %u", EXTRACT_32BITS(bp
)));
269 if (flags
& GRE_AP
) {
272 ND_PRINT((ndo
, ", ack %u", EXTRACT_32BITS(bp
)));
277 if ((flags
& GRE_SP
) == 0)
278 ND_PRINT((ndo
, ", no-payload"));
281 ND_PRINT((ndo
, ", proto %s (0x%04x)",
282 tok2str(ethertype_values
,"unknown",prot
),
285 ND_PRINT((ndo
, ", length %u",length
));
287 if ((flags
& GRE_SP
) == 0)
290 if (ndo
->ndo_vflag
< 1)
291 ND_PRINT((ndo
, ": ")); /* put in a colon as protocol demarc */
293 ND_PRINT((ndo
, "\n\t")); /* if verbose go multiline */
297 ppp_print(ndo
, bp
, len
);
300 ND_PRINT((ndo
, "gre-proto-0x%x", prot
));
306 ND_PRINT((ndo
, "%s", tstr
));
310 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
311 uint8_t srelen
, const u_char
*bp
, u_int len
)
315 ND_PRINT((ndo
, ", (rtaf=ip"));
316 gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
317 ND_PRINT((ndo
, ")"));
320 ND_PRINT((ndo
, ", (rtaf=asn"));
321 gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
322 ND_PRINT((ndo
, ")"));
325 ND_PRINT((ndo
, ", (rtaf=0x%x)", af
));
330 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
331 const u_char
*bp
, u_int len
)
334 const u_char
*up
= bp
;
337 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
341 ND_PRINT((ndo
, ", badlength=%u", srelen
));
344 if (sreoff
>= srelen
) {
345 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
350 if (len
< 4 || srelen
== 0)
353 memcpy(&a
, bp
, sizeof(a
));
354 ND_PRINT((ndo
, " %s%s",
355 ((bp
- up
) == sreoff
) ? "*" : "",
365 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
366 const u_char
*bp
, u_int len
)
368 const u_char
*up
= bp
;
371 ND_PRINT((ndo
, ", badoffset=%u", sreoff
));
375 ND_PRINT((ndo
, ", badlength=%u", srelen
));
378 if (sreoff
>= srelen
) {
379 ND_PRINT((ndo
, ", badoff/len=%u/%u", sreoff
, srelen
));
384 if (len
< 2 || srelen
== 0)
387 ND_PRINT((ndo
, " %s%x",
388 ((bp
- up
) == sreoff
) ? "*" : "",
389 EXTRACT_16BITS(bp
)));