]>
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 * RFC 1701 (GRE), RFC 1702 (GRE IPv4), RFC 2637 (PPTP, which
34 * has an extended form of GRE), RFC 2784 (revised GRE, with
35 * R, K, S, and s bits and Recur and Offset fields now reserved
36 * in the header, and no optional Key or Sequence number in the
37 * header), and RFC 2890 (proposal to add back the K and S bits
38 * and the optional Key and Sequence number).
40 * The RFC 2637 PPTP GRE repurposes the Key field to hold a
41 * 16-bit Payload Length and a 16-bit Call ID.
43 * RFC 7637 (NVGRE) repurposes the Key field to hold a 24-bit
44 * Virtual Subnet ID (VSID) and an 8-bit FlowID.
51 #include "netdissect-stdinc.h"
53 #define ND_LONGJMP_FROM_TCHECK
54 #include "netdissect.h"
55 #include "addrtostr.h"
57 #include "ethertype.h"
60 #define GRE_CP 0x8000 /* checksum present */
61 #define GRE_RP 0x4000 /* routing present */
62 #define GRE_KP 0x2000 /* key present */
63 #define GRE_SP 0x1000 /* sequence# present */
64 #define GRE_sP 0x0800 /* source routing */
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_AP
, "ack present"},
77 #define GRE_RECRS_MASK 0x0700 /* recursion count */
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 int gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
87 static int gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
88 static int 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
)
95 ndo
->ndo_protocol
= "gre";
96 nd_print_protocol_caps(ndo
);
97 ND_ICHECK_U(length
, <, 2);
98 vers
= GET_BE_U_2(bp
) & GRE_VERS_MASK
;
103 gre_print_0(ndo
, bp
, length
);
106 gre_print_1(ndo
, bp
, length
);
109 ND_PRINT(" ERROR: unknown-version");
115 nd_print_invalid(ndo
);
119 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
122 uint16_t flags
, prot
;
124 ND_ICHECK_U(len
, <, 2);
125 flags
= GET_BE_U_2(bp
);
127 ND_PRINT(", Flags [%s]",
128 bittok2str(gre_flag_values
,"none",flags
));
133 ND_ICHECK_U(len
, <, 2);
134 prot
= GET_BE_U_2(bp
);
138 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
141 ND_ICHECK_U(len
, <, 2);
142 sum
= GET_BE_U_2(bp
);
144 ND_PRINT(", sum 0x%x", sum
);
148 ND_ICHECK_U(len
, <, 2);
149 ND_PRINT(", off 0x%x", GET_BE_U_2(bp
));
154 if (flags
& GRE_KP
) {
157 ND_ICHECK_U(len
, <, 4);
158 key
= GET_BE_U_4(bp
);
163 * OpenBSD shows this as both a 32-bit
164 * (decimal) key value and a VSID+FlowID
165 * pair, with the VSID in decimal and
166 * the FlowID in hex, as key=<Key>|<VSID>+<FlowID>,
167 * in case this is NVGRE.
169 ND_PRINT(", key=0x%x", key
);
172 if (flags
& GRE_SP
) {
173 ND_ICHECK_U(len
, <, 4);
174 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
179 if (flags
& GRE_RP
) {
185 ND_ICHECK_U(len
, <, 4);
187 sreoff
= GET_U_1(bp
+ 2);
188 srelen
= GET_U_1(bp
+ 3);
192 if (af
== 0 && srelen
== 0)
195 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
198 ND_ICHECK_U(len
, <, srelen
);
205 ND_PRINT(", proto %s (0x%04x)",
206 tok2str(ethertype_values
,"unknown",prot
), prot
);
208 ND_PRINT(", length %u",length
);
210 if (ndo
->ndo_vflag
< 1)
211 ND_PRINT(": "); /* put in a colon as protocol demarc */
213 ND_PRINT("\n\t"); /* if verbose go multiline */
217 ip_print(ndo
, bp
, len
);
220 ip6_print(ndo
, bp
, len
);
223 mpls_print(ndo
, bp
, len
);
226 ipx_print(ndo
, bp
, len
);
228 case ETHERTYPE_ATALK
:
229 atalk_print(ndo
, bp
, len
);
231 case ETHERTYPE_GRE_ISO
:
232 isoclns_print(ndo
, bp
, len
);
235 ether_print(ndo
, bp
, len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
238 ND_PRINT("gre-proto-0x%x", prot
);
243 nd_print_invalid(ndo
);
247 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
250 uint16_t flags
, prot
;
252 ND_ICHECK_U(len
, <, 2);
253 flags
= GET_BE_U_2(bp
);
258 ND_PRINT(", Flags [%s]",
259 bittok2str(gre_flag_values
,"none",flags
));
261 ND_ICHECK_U(len
, <, 2);
262 prot
= GET_BE_U_2(bp
);
267 if (flags
& GRE_KP
) {
270 ND_ICHECK_U(len
, <, 4);
272 ND_PRINT(", call %u", k
& 0xffff);
277 if (flags
& GRE_SP
) {
278 ND_ICHECK_U(len
, <, 4);
279 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
284 if (flags
& GRE_AP
) {
285 ND_ICHECK_U(len
, <, 4);
286 ND_PRINT(", ack %u", GET_BE_U_4(bp
));
291 if ((flags
& GRE_SP
) == 0)
292 ND_PRINT(", no-payload");
295 ND_PRINT(", proto %s (0x%04x)",
296 tok2str(ethertype_values
,"unknown",prot
), prot
);
298 ND_PRINT(", length %u",length
);
300 if ((flags
& GRE_SP
) == 0)
303 if (ndo
->ndo_vflag
< 1)
304 ND_PRINT(": "); /* put in a colon as protocol demarc */
306 ND_PRINT("\n\t"); /* if verbose go multiline */
310 ppp_print(ndo
, bp
, len
);
313 ND_PRINT("gre-proto-0x%x", prot
);
319 nd_print_invalid(ndo
);
323 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
324 uint8_t srelen
, const u_char
*bp
, u_int len
)
330 ND_PRINT(", (rtaf=ip");
331 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
335 ND_PRINT(", (rtaf=asn");
336 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
340 ND_PRINT(", (rtaf=0x%x)", af
);
347 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
348 const u_char
*bp
, u_int len
)
350 const u_char
*up
= bp
;
351 char buf
[INET_ADDRSTRLEN
];
354 ND_PRINT(", badoffset=%u", sreoff
);
358 ND_PRINT(", badlength=%u", srelen
);
361 if (sreoff
>= srelen
) {
362 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
366 while (srelen
!= 0) {
367 ND_ICHECK_U(len
, <, 4);
369 ND_TCHECK_LEN(bp
, sizeof(nd_ipv4
));
370 addrtostr(bp
, buf
, sizeof(buf
));
372 ((bp
- up
) == sreoff
) ? "*" : "", buf
);
385 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
386 const u_char
*bp
, u_int len
)
388 const u_char
*up
= bp
;
391 ND_PRINT(", badoffset=%u", sreoff
);
395 ND_PRINT(", badlength=%u", srelen
);
398 if (sreoff
>= srelen
) {
399 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
403 while (srelen
!= 0) {
404 ND_ICHECK_U(len
, <, 2);
407 ((bp
- up
) == sreoff
) ? "*" : "", GET_BE_U_2(bp
));