]>
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 /* \summary: Generic Routing Encapsulation (GRE) printer */
32 * netdissect printer for GRE - Generic Routing Encapsulation
33 * RFC1701 (GRE), RFC1702 (GRE IPv4), and RFC2637 (Enhanced GRE)
40 #include "netdissect-stdinc.h"
44 #include "netdissect.h"
45 #include "addrtostr.h"
47 #include "ethertype.h"
50 #define GRE_CP 0x8000 /* checksum present */
51 #define GRE_RP 0x4000 /* routing present */
52 #define GRE_KP 0x2000 /* key present */
53 #define GRE_SP 0x1000 /* sequence# present */
54 #define GRE_sP 0x0800 /* source routing */
55 #define GRE_RECRS 0x0700 /* recursion count */
56 #define GRE_AP 0x0080 /* acknowledgment# present */
58 static const struct tok gre_flag_values
[] = {
59 { GRE_CP
, "checksum present"},
60 { GRE_RP
, "routing present"},
61 { GRE_KP
, "key present"},
62 { GRE_SP
, "sequence# present"},
63 { GRE_sP
, "source routing present"},
64 { GRE_RECRS
, "recursion count"},
65 { GRE_AP
, "ack present"},
69 #define GRE_VERS_MASK 0x0007 /* protocol version */
71 /* source route entry types */
72 #define GRESRE_IP 0x0800 /* IP */
73 #define GRESRE_ASN 0xfffe /* ASN */
75 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
76 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
77 static int gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
78 static int gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
79 static int gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
82 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
84 u_int len
= length
, vers
;
86 ndo
->ndo_protocol
= "gre";
90 vers
= GET_BE_U_2(bp
) & GRE_VERS_MASK
;
91 ND_PRINT("GREv%u",vers
);
95 gre_print_0(ndo
, bp
, len
);
98 gre_print_1(ndo
, bp
, len
);
101 ND_PRINT(" ERROR: unknown-version");
111 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
114 uint16_t flags
, prot
;
116 /* 16 bits ND_TCHECKed in gre_print() */
117 flags
= GET_BE_U_2(bp
);
119 ND_PRINT(", Flags [%s]",
120 bittok2str(gre_flag_values
,"none",flags
));
128 prot
= GET_BE_U_2(bp
);
132 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
137 ND_PRINT(", sum 0x%x", GET_BE_U_2(bp
));
144 ND_PRINT(", off 0x%x", GET_BE_U_2(bp
));
149 if (flags
& GRE_KP
) {
153 ND_PRINT(", key=0x%x", GET_BE_U_4(bp
));
158 if (flags
& GRE_SP
) {
162 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
167 if (flags
& GRE_RP
) {
177 sreoff
= GET_U_1(bp
+ 2);
178 srelen
= GET_U_1(bp
+ 3);
182 if (af
== 0 && srelen
== 0)
185 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
196 ND_PRINT(", proto %s (0x%04x)",
197 tok2str(ethertype_values
,"unknown",prot
), prot
);
199 ND_PRINT(", length %u",length
);
201 if (ndo
->ndo_vflag
< 1)
202 ND_PRINT(": "); /* put in a colon as protocol demarc */
204 ND_PRINT("\n\t"); /* if verbose go multiline */
208 ip_print(ndo
, bp
, len
);
211 ip6_print(ndo
, bp
, len
);
214 mpls_print(ndo
, bp
, len
);
217 ipx_print(ndo
, bp
, len
);
219 case ETHERTYPE_ATALK
:
220 atalk_print(ndo
, bp
, len
);
222 case ETHERTYPE_GRE_ISO
:
223 isoclns_print(ndo
, bp
, len
);
226 ether_print(ndo
, bp
, len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
229 ND_PRINT("gre-proto-0x%x", prot
);
238 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
241 uint16_t flags
, prot
;
243 /* 16 bits ND_TCHECKed in gre_print() */
244 flags
= GET_BE_U_2(bp
);
249 ND_PRINT(", Flags [%s]",
250 bittok2str(gre_flag_values
,"none",flags
));
255 prot
= GET_BE_U_2(bp
);
260 if (flags
& GRE_KP
) {
267 ND_PRINT(", call %u", k
& 0xffff);
272 if (flags
& GRE_SP
) {
276 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
281 if (flags
& GRE_AP
) {
285 ND_PRINT(", ack %u", GET_BE_U_4(bp
));
290 if ((flags
& GRE_SP
) == 0)
291 ND_PRINT(", no-payload");
294 ND_PRINT(", proto %s (0x%04x)",
295 tok2str(ethertype_values
,"unknown",prot
), prot
);
297 ND_PRINT(", length %u",length
);
299 if ((flags
& GRE_SP
) == 0)
302 if (ndo
->ndo_vflag
< 1)
303 ND_PRINT(": "); /* put in a colon as protocol demarc */
305 ND_PRINT("\n\t"); /* if verbose go multiline */
309 ppp_print(ndo
, bp
, len
);
312 ND_PRINT("gre-proto-0x%x", prot
);
322 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
323 uint8_t srelen
, const u_char
*bp
, u_int len
)
329 ND_PRINT(", (rtaf=ip");
330 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
334 ND_PRINT(", (rtaf=asn");
335 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
339 ND_PRINT(", (rtaf=0x%x)", af
);
346 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
347 const u_char
*bp
, u_int len
)
349 const u_char
*up
= bp
;
350 char buf
[INET_ADDRSTRLEN
];
353 ND_PRINT(", badoffset=%u", sreoff
);
357 ND_PRINT(", badlength=%u", srelen
);
360 if (sreoff
>= srelen
) {
361 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
365 while (srelen
!= 0) {
370 addrtostr(bp
, buf
, sizeof(buf
));
372 ((bp
- up
) == sreoff
) ? "*" : "", buf
);
384 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
385 const u_char
*bp
, u_int len
)
387 const u_char
*up
= bp
;
390 ND_PRINT(", badoffset=%u", sreoff
);
394 ND_PRINT(", badlength=%u", srelen
);
397 if (sreoff
>= srelen
) {
398 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
402 while (srelen
!= 0) {
408 ((bp
- up
) == sreoff
) ? "*" : "", GET_BE_U_2(bp
));