]>
The Tcpdump Group git mirrors - tcpdump/blob - print-gre.c
368bf8bb9e0666058d99f91717a81a6606012122
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 static const struct tok gre_flag_values
[] = {
61 { GRE_CP
, "checksum present"},
62 { GRE_RP
, "routing present"},
63 { GRE_KP
, "key present"},
64 { GRE_SP
, "sequence# present"},
65 { GRE_sP
, "source routing present"},
66 { GRE_AP
, "ack present"},
70 #define GRE_RECRS_MASK 0x0700 /* recursion count */
71 #define GRE_VERS_MASK 0x0007 /* protocol version */
73 /* source route entry types */
74 #define GRESRE_IP 0x0800 /* IP */
75 #define GRESRE_ASN 0xfffe /* ASN */
78 * Ethertype values used for GRE (but not elsewhere?).
80 #define GRE_CDP 0x2000 /* Cisco Discovery Protocol */
81 #define GRE_NHRP 0x2001 /* Next Hop Resolution Protocol */
82 #define GRE_MIKROTIK_EOIP 0x6400 /* MikroTik RouterBoard Ethernet over IP (EoIP) */
83 #define GRE_ERSPAN_III 0x22eb
84 #define GRE_WCCP 0x883e /* Web Cache C* Protocol */
85 #define GRE_ERSPAN_I_II 0x88be
87 struct wccp_redirect
{
89 #define WCCP_T (1 << 7)
90 #define WCCP_A (1 << 6)
91 #define WCCP_U (1 << 5)
97 static void gre_print_0(netdissect_options
*, const u_char
*, u_int
);
98 static void gre_print_1(netdissect_options
*, const u_char
*, u_int
);
99 static int gre_sre_print(netdissect_options
*, uint16_t, uint8_t, uint8_t, const u_char
*, u_int
);
100 static int gre_sre_ip_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
101 static int gre_sre_asn_print(netdissect_options
*, uint8_t, uint8_t, const u_char
*, u_int
);
104 gre_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
108 ndo
->ndo_protocol
= "gre";
109 nd_print_protocol_caps(ndo
);
110 ND_ICHECK_U(length
, <, 2);
111 vers
= GET_BE_U_2(bp
) & GRE_VERS_MASK
;
112 ND_PRINT("v%u",vers
);
116 gre_print_0(ndo
, bp
, length
);
119 gre_print_1(ndo
, bp
, length
);
122 ND_PRINT(" ERROR: unknown-version");
128 nd_print_invalid(ndo
);
132 gre_print_0(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
135 uint16_t flags
, prot
;
137 ND_ICHECK_U(len
, <, 2);
138 flags
= GET_BE_U_2(bp
);
140 ND_PRINT(", Flags [%s]",
141 bittok2str(gre_flag_values
,"none",flags
));
146 ND_ICHECK_U(len
, <, 2);
147 prot
= GET_BE_U_2(bp
);
151 if ((flags
& GRE_CP
) | (flags
& GRE_RP
)) {
154 ND_ICHECK_U(len
, <, 2);
155 sum
= GET_BE_U_2(bp
);
157 ND_PRINT(", sum 0x%x", sum
);
161 ND_ICHECK_U(len
, <, 2);
162 ND_PRINT(", off 0x%x", GET_BE_U_2(bp
));
167 if (flags
& GRE_KP
) {
170 ND_ICHECK_U(len
, <, 4);
171 key
= GET_BE_U_4(bp
);
176 * OpenBSD shows this as both a 32-bit
177 * (decimal) key value and a VSID+FlowID
178 * pair, with the VSID in decimal and
179 * the FlowID in hex, as key=<Key>|<VSID>+<FlowID>,
180 * in case this is NVGRE.
182 ND_PRINT(", key=0x%x", key
);
185 if (flags
& GRE_SP
) {
186 ND_ICHECK_U(len
, <, 4);
187 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
192 if (flags
& GRE_RP
) {
198 ND_ICHECK_U(len
, <, 4);
200 sreoff
= GET_U_1(bp
+ 2);
201 srelen
= GET_U_1(bp
+ 3);
205 if (af
== 0 && srelen
== 0)
208 if (!gre_sre_print(ndo
, af
, sreoff
, srelen
, bp
, len
))
211 ND_ICHECK_U(len
, <, srelen
);
218 ND_PRINT(", proto %s (0x%04x)",
219 tok2str(ethertype_values
,"unknown",prot
), prot
);
221 ND_PRINT(", length %u",length
);
223 if (ndo
->ndo_vflag
< 1)
224 ND_PRINT(": "); /* put in a colon as protocol demarc */
226 ND_PRINT("\n\t"); /* if verbose go multiline */
231 * 0x0000 is reserved, but Cisco, at least, appears to
232 * use it for keep-alives; see, for example,
233 * https://www.cisco.com/c/en/us/support/docs/ip/generic-routing-encapsulation-gre/118370-technote-gre-00.html#anc1
235 ND_PRINT("keep-alive");
239 * This is a bit weird.
241 * This may either just mean "IPv4" or it may mean
242 * "IPv4 preceded by a WCCP redirect header". We
243 * check to see if the first octet looks like the
244 * beginning of an IPv4 header and, if not, dissect
245 * it "IPv4 preceded by a WCCP redirect header",
246 * otherwise we dissect it as just IPv4.
248 * See "Packet redirection" in draft-forster-wrec-wccp-v1-00,
249 * section 4.12 "Traffic Forwarding" in
250 * draft-wilson-wrec-wccp-v2-01, and section 3.12.1
251 * "Forwarding using GRE Encapsulation" in
252 * draft-param-wccp-v2rev1-01.
256 ND_ICHECK_U(len
, <, 1);
257 if (GET_U_1(bp
) >> 4 != 4) {
259 * First octet isn't 0x4*, so it's not IPv4.
261 const struct wccp_redirect
*wccp
;
264 ND_ICHECK_ZU(len
, <, sizeof(*wccp
));
265 wccp
= (const struct wccp_redirect
*)bp
;
266 wccp_flags
= GET_U_1(wccp
->flags
);
268 ND_PRINT("T:%c A:%c U:%c SId:%u Alt:%u Pri:%u",
269 (wccp_flags
& WCCP_T
) ? '1' : '0',
270 (wccp_flags
& WCCP_A
) ? '1' : '0',
271 (wccp_flags
& WCCP_U
) ? '1' : '0',
272 GET_U_1(wccp
->ServiceId
),
273 GET_U_1(wccp
->AltBucket
),
274 GET_U_1(wccp
->PriBucket
));
277 len
-= sizeof(*wccp
);
283 ip_print(ndo
, bp
, len
);
286 ip6_print(ndo
, bp
, len
);
289 case ETHERTYPE_MPLS_MULTI
:
290 mpls_print(ndo
, bp
, len
);
293 ipx_print(ndo
, bp
, len
);
295 case ETHERTYPE_ATALK
:
296 atalk_print(ndo
, bp
, len
);
298 case ETHERTYPE_GRE_ISO
:
299 isoclns_print(ndo
, bp
, len
);
302 ether_print(ndo
, bp
, len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
305 nsh_print(ndo
, bp
, len
);
307 case GRE_ERSPAN_I_II
:
308 erspan_print(ndo
, flags
, bp
, len
);
311 cdp_print(ndo
, bp
, len
);
314 nhrp_print(ndo
, bp
, len
);
317 ND_PRINT("gre-proto-0x%x", prot
);
322 nd_print_invalid(ndo
);
326 gre_print_1(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
329 uint16_t flags
, prot
;
331 ND_ICHECK_U(len
, <, 2);
332 flags
= GET_BE_U_2(bp
);
337 ND_PRINT(", Flags [%s]",
338 bittok2str(gre_flag_values
,"none",flags
));
340 ND_ICHECK_U(len
, <, 2);
341 prot
= GET_BE_U_2(bp
);
346 * This version is used for two purposes:
349 * Some Mikrotik Ethernet-over-IP hack.
352 case GRE_MIKROTIK_EOIP
:
354 * The MikroTik hack uses only the key field, and uses it
355 * for its own purposes. If anything other than the version
356 * and K bit are set, report an error and give up.
358 if ((flags
& ~GRE_VERS_MASK
) != GRE_KP
) {
359 ND_PRINT(" unknown-eoip-flags-%04x!", flags
);
365 * XXX - what should we do if it's not ETHERTYPE_PPP?
370 if (flags
& GRE_KP
) {
371 /* Skip payload length? */
372 ND_ICHECK_U(len
, <, 2);
373 ND_TCHECK_LEN(bp
, 2);
377 ND_ICHECK_U(len
, <, 2);
378 if (prot
== GRE_MIKROTIK_EOIP
) {
380 ND_PRINT(", tunnel-id %u", GET_BE_U_2(bp
));
382 ND_PRINT(", call %u", GET_BE_U_2(bp
));
386 ND_PRINT(", (ERROR: K flag not set)");
388 if (flags
& GRE_SP
) {
389 ND_ICHECK_U(len
, <, 4);
390 ND_PRINT(", seq %u", GET_BE_U_4(bp
));
395 if (flags
& GRE_AP
) {
396 ND_ICHECK_U(len
, <, 4);
397 ND_PRINT(", ack %u", GET_BE_U_4(bp
));
403 * More non-standard EoIP behavior.
405 if (prot
!= GRE_MIKROTIK_EOIP
&& (flags
& GRE_SP
) == 0)
406 ND_PRINT(", no-payload");
409 ND_PRINT(", proto %s (0x%04x)",
410 tok2str(ethertype_values
,"unknown",prot
), prot
);
412 ND_PRINT(", length %u",length
);
415 * More non-standard EoIP behavior.
417 if (prot
!= GRE_MIKROTIK_EOIP
&& (flags
& GRE_SP
) == 0)
420 if (ndo
->ndo_vflag
< 1)
421 ND_PRINT(": "); /* put in a colon as protocol demarc */
423 ND_PRINT("\n\t"); /* if verbose go multiline */
427 ppp_print(ndo
, bp
, len
);
429 case GRE_MIKROTIK_EOIP
:
430 /* MikroTik RouterBoard Ethernet over IP (EoIP) */
432 ND_PRINT("keepalive");
434 ether_print(ndo
, bp
, len
, ND_BYTES_AVAILABLE_AFTER(bp
), NULL
, NULL
);
437 ND_PRINT("gre-proto-0x%x", prot
);
443 nd_print_invalid(ndo
);
447 gre_sre_print(netdissect_options
*ndo
, uint16_t af
, uint8_t sreoff
,
448 uint8_t srelen
, const u_char
*bp
, u_int len
)
454 ND_PRINT(", (rtaf=ip");
455 ret
= gre_sre_ip_print(ndo
, sreoff
, srelen
, bp
, len
);
459 ND_PRINT(", (rtaf=asn");
460 ret
= gre_sre_asn_print(ndo
, sreoff
, srelen
, bp
, len
);
464 ND_PRINT(", (rtaf=0x%x)", af
);
471 gre_sre_ip_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
472 const u_char
*bp
, u_int len
)
474 const u_char
*up
= bp
;
475 char buf
[INET_ADDRSTRLEN
];
478 ND_PRINT(", badoffset=%u", sreoff
);
482 ND_PRINT(", badlength=%u", srelen
);
485 if (sreoff
>= srelen
) {
486 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
490 while (srelen
!= 0) {
491 ND_ICHECK_U(len
, <, 4);
493 ND_TCHECK_LEN(bp
, sizeof(nd_ipv4
));
494 addrtostr(bp
, buf
, sizeof(buf
));
496 ((bp
- up
) == sreoff
) ? "*" : "", buf
);
509 gre_sre_asn_print(netdissect_options
*ndo
, uint8_t sreoff
, uint8_t srelen
,
510 const u_char
*bp
, u_int len
)
512 const u_char
*up
= bp
;
515 ND_PRINT(", badoffset=%u", sreoff
);
519 ND_PRINT(", badlength=%u", srelen
);
522 if (sreoff
>= srelen
) {
523 ND_PRINT(", badoff/len=%u/%u", sreoff
, srelen
);
527 while (srelen
!= 0) {
528 ND_ICHECK_U(len
, <, 2);
531 ((bp
- up
) == sreoff
) ? "*" : "", GET_BE_U_2(bp
));