1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * Copyright (c) 2014 Andrey V. Elsukov <ae@FreeBSD.org>
6 * All rights reserved
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Heiko W.Rupp <hwr@pilhuhn.de>
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * $NetBSD: if_gre.h,v 1.13 2003/11/10 08:51:52 wiz Exp $
33 */
34
35#ifndef _NET_IF_GRE_H_
36#define _NET_IF_GRE_H_
37
38#ifdef _KERNEL
39/* GRE header according to RFC 2784 and RFC 2890 */
40struct grehdr {
41	uint16_t	gre_flags;	/* GRE flags */
42#define	GRE_FLAGS_CP	0x8000		/* checksum present */
43#define	GRE_FLAGS_KP	0x2000		/* key present */
44#define	GRE_FLAGS_SP	0x1000		/* sequence present */
45#define	GRE_FLAGS_MASK	(GRE_FLAGS_CP|GRE_FLAGS_KP|GRE_FLAGS_SP)
46	uint16_t	gre_proto;	/* protocol type */
47	uint32_t	gre_opts[0];	/* optional fields */
48} __packed;
49
50#ifdef INET
51struct greip {
52	struct ip	gi_ip;
53	struct grehdr	gi_gre;
54} __packed;
55
56struct greudp {
57	struct ip	gi_ip;
58	struct udphdr	gi_udp;
59	struct grehdr	gi_gre;
60} __packed;
61#endif /* INET */
62
63#ifdef INET6
64struct greip6 {
65	struct ip6_hdr	gi6_ip6;
66	struct grehdr	gi6_gre;
67} __packed;
68
69struct greudp6 {
70	struct ip6_hdr	gi6_ip6;
71	struct udphdr	gi6_udp;
72	struct grehdr	gi6_gre;
73} __packed;
74#endif /* INET6 */
75
76CK_LIST_HEAD(gre_list, gre_softc);
77CK_LIST_HEAD(gre_sockets, gre_socket);
78struct gre_socket {
79	struct socket		*so;
80	struct gre_list		list;
81	CK_LIST_ENTRY(gre_socket) chain;
82	struct epoch_context	epoch_ctx;
83};
84
85struct gre_softc {
86	struct ifnet		*gre_ifp;
87	int			gre_family;	/* AF of delivery header */
88	uint32_t		gre_iseq;
89	uint32_t		gre_oseq;
90	uint32_t		gre_key;
91	uint32_t		gre_options;
92	uint32_t		gre_csumflags;
93	uint32_t		gre_port;
94	u_int			gre_fibnum;
95	u_int			gre_hlen;	/* header size */
96	union {
97		void		*hdr;
98#ifdef INET
99		struct greip	*iphdr;
100		struct greudp	*udphdr;
101#endif
102#ifdef INET6
103		struct greip6	*ip6hdr;
104		struct greudp6	*udp6hdr;
105#endif
106	} gre_uhdr;
107	struct gre_socket	*gre_so;
108
109	CK_LIST_ENTRY(gre_softc) chain;
110	CK_LIST_ENTRY(gre_softc) srchash;
111};
112MALLOC_DECLARE(M_GRE);
113
114#ifndef GRE_HASH_SIZE
115#define	GRE_HASH_SIZE	(1 << 4)
116#endif
117
118#define	GRE2IFP(sc)		((sc)->gre_ifp)
119#define	GRE_RLOCK_TRACKER	struct epoch_tracker gre_et
120#define	GRE_RLOCK()		epoch_enter_preempt(net_epoch_preempt, &gre_et)
121#define	GRE_RUNLOCK()		epoch_exit_preempt(net_epoch_preempt, &gre_et)
122#define	GRE_WAIT()		epoch_wait_preempt(net_epoch_preempt)
123
124#define	gre_hdr			gre_uhdr.hdr
125#define	gre_iphdr		gre_uhdr.iphdr
126#define	gre_ip6hdr		gre_uhdr.ip6hdr
127#define	gre_udphdr		gre_uhdr.udphdr
128#define	gre_udp6hdr		gre_uhdr.udp6hdr
129
130#define	gre_oip			gre_iphdr->gi_ip
131#define	gre_udp			gre_udphdr->gi_udp
132#define	gre_oip6		gre_ip6hdr->gi6_ip6
133#define	gre_udp6		gre_udp6hdr->gi6_udp
134
135struct gre_list *gre_hashinit(void);
136void gre_hashdestroy(struct gre_list *);
137
138int	gre_input(struct mbuf *, int, int, void *);
139void	gre_update_hdr(struct gre_softc *, struct grehdr *);
140void	gre_update_udphdr(struct gre_softc *, struct udphdr *, uint16_t);
141void	gre_sofree(epoch_context_t);
142
143void	in_gre_init(void);
144void	in_gre_uninit(void);
145int	in_gre_setopts(struct gre_softc *, u_long, uint32_t);
146int	in_gre_ioctl(struct gre_softc *, u_long, caddr_t);
147int	in_gre_output(struct mbuf *, int, int);
148
149void	in6_gre_init(void);
150void	in6_gre_uninit(void);
151int	in6_gre_setopts(struct gre_softc *, u_long, uint32_t);
152int	in6_gre_ioctl(struct gre_softc *, u_long, caddr_t);
153int	in6_gre_output(struct mbuf *, int, int, uint32_t);
154/*
155 * CISCO uses special type for GRE tunnel created as part of WCCP
156 * connection, while in fact those packets are just IPv4 encapsulated
157 * into GRE.
158 */
159#define ETHERTYPE_WCCP		0x883E
160#endif /* _KERNEL */
161
162#define GRESADDRS	_IOW('i', 101, struct ifreq)
163#define GRESADDRD	_IOW('i', 102, struct ifreq)
164#define GREGADDRS	_IOWR('i', 103, struct ifreq)
165#define GREGADDRD	_IOWR('i', 104, struct ifreq)
166#define GRESPROTO	_IOW('i' , 105, struct ifreq)
167#define GREGPROTO	_IOWR('i', 106, struct ifreq)
168
169#define	GREGKEY		_IOWR('i', 107, struct ifreq)
170#define	GRESKEY		_IOW('i', 108, struct ifreq)
171#define	GREGOPTS	_IOWR('i', 109, struct ifreq)
172#define	GRESOPTS	_IOW('i', 110, struct ifreq)
173#define	GREGPORT	_IOWR('i', 111, struct ifreq)
174#define	GRESPORT	_IOW('i', 112, struct ifreq)
175
176/* GRE-in-UDP encapsulation destination port as defined in RFC8086 */
177#define	GRE_UDPPORT		4754
178
179#define	GRE_ENABLE_CSUM		0x0001
180#define	GRE_ENABLE_SEQ		0x0002
181#define	GRE_UDPENCAP		0x0004
182#define	GRE_OPTMASK		(GRE_ENABLE_CSUM|GRE_ENABLE_SEQ|GRE_UDPENCAP)
183
184#endif /* _NET_IF_GRE_H_ */
185