xref: /freebsd/sys/netinet6/ip6_gre.c (revision 2da0fcde21e0b90384f61fd50b87ea7dbd820233)
1 /*-
2  * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 
33 #include <sys/param.h>
34 #include <sys/jail.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
38 #include <sys/mbuf.h>
39 #include <sys/errno.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/vnet.h>
47 
48 #include <netinet/in.h>
49 #ifdef INET
50 #include <net/ethernet.h>
51 #include <netinet/ip.h>
52 #endif
53 #include <netinet/ip_encap.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet6/in6_var.h>
57 #include <netinet6/scope6_var.h>
58 #include <net/if_gre.h>
59 
60 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
61 #define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
62 
63 SYSCTL_DECL(_net_inet6_ip6);
64 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
65     &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
66 
67 static VNET_DEFINE(struct gre_list *, ipv6_hashtbl) = NULL;
68 #define	V_ipv6_hashtbl		VNET(ipv6_hashtbl)
69 #define	GRE_HASH(src, dst)	(V_ipv6_hashtbl[\
70     in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
71 #define	GRE_HASH_SC(sc)		GRE_HASH(&(sc)->gre_oip6.ip6_src,\
72     &(sc)->gre_oip6.ip6_dst)
73 
74 static uint32_t
75 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
76 {
77 	uint32_t ret;
78 
79 	ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
80 	return (fnv_32_buf(dst, sizeof(*dst), ret));
81 }
82 
83 static int
84 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
85     const struct in6_addr *dst)
86 {
87 	struct gre_softc *tmp;
88 
89 	if (sc->gre_family == AF_INET6 &&
90 	    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
91 	    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst))
92 		return (EEXIST);
93 
94 	CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) {
95 		if (tmp == sc)
96 			continue;
97 		if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
98 		    IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
99 			return (EADDRNOTAVAIL);
100 	}
101 	return (0);
102 }
103 
104 static int
105 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
106 {
107 	const struct ip6_hdr *ip6;
108 	struct gre_softc *sc;
109 
110 	if (V_ipv6_hashtbl == NULL)
111 		return (0);
112 
113 	MPASS(in_epoch());
114 	ip6 = mtod(m, const struct ip6_hdr *);
115 	CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
116 		/*
117 		 * This is an inbound packet, its ip6_dst is source address
118 		 * in softc.
119 		 */
120 		if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
121 		    &ip6->ip6_dst) &&
122 		    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
123 		    &ip6->ip6_src)) {
124 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
125 				return (0);
126 			*arg = sc;
127 			return (ENCAP_DRV_LOOKUP);
128 		}
129 	}
130 	return (0);
131 }
132 
133 static void
134 in6_gre_attach(struct gre_softc *sc)
135 {
136 
137 	sc->gre_hlen = sizeof(struct greip6);
138 	sc->gre_oip6.ip6_vfc = IPV6_VERSION;
139 	sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
140 	gre_updatehdr(sc, &sc->gre_gi6hdr->gi6_gre);
141 	CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
142 }
143 
144 void
145 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
146 {
147 
148 	MPASS(cmd == GRESKEY || cmd == GRESOPTS);
149 
150 	/* NOTE: we are protected with gre_ioctl_sx lock */
151 	MPASS(sc->gre_family == AF_INET6);
152 	CK_LIST_REMOVE(sc, chain);
153 	GRE_WAIT();
154 	if (cmd == GRESKEY)
155 		sc->gre_key = value;
156 	else
157 		sc->gre_options = value;
158 	in6_gre_attach(sc);
159 }
160 
161 int
162 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
163 {
164 	struct in6_ifreq *ifr = (struct in6_ifreq *)data;
165 	struct sockaddr_in6 *dst, *src;
166 	struct ip6_hdr *ip6;
167 	int error;
168 
169 	/* NOTE: we are protected with gre_ioctl_sx lock */
170 	error = EINVAL;
171 	switch (cmd) {
172 	case SIOCSIFPHYADDR_IN6:
173 		src = &((struct in6_aliasreq *)data)->ifra_addr;
174 		dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
175 
176 		/* sanity checks */
177 		if (src->sin6_family != dst->sin6_family ||
178 		    src->sin6_family != AF_INET6 ||
179 		    src->sin6_len != dst->sin6_len ||
180 		    src->sin6_len != sizeof(*src))
181 			break;
182 		if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
183 		    IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
184 			error = EADDRNOTAVAIL;
185 			break;
186 		}
187 		/*
188 		 * Check validity of the scope zone ID of the
189 		 * addresses, and convert it into the kernel
190 		 * internal form if necessary.
191 		 */
192 		if ((error = sa6_embedscope(src, 0)) != 0 ||
193 		    (error = sa6_embedscope(dst, 0)) != 0)
194 			break;
195 
196 		if (V_ipv6_hashtbl == NULL)
197 			V_ipv6_hashtbl = gre_hashinit();
198 		error = in6_gre_checkdup(sc, &src->sin6_addr,
199 		    &dst->sin6_addr);
200 		if (error == EADDRNOTAVAIL)
201 			break;
202 		if (error == EEXIST) {
203 			/* Addresses are the same. Just return. */
204 			error = 0;
205 			break;
206 		}
207 		ip6 = malloc(sizeof(struct greip6) + 3 * sizeof(uint32_t),
208 		    M_GRE, M_WAITOK | M_ZERO);
209 		ip6->ip6_src = src->sin6_addr;
210 		ip6->ip6_dst = dst->sin6_addr;
211 		if (sc->gre_family != 0) {
212 			/* Detach existing tunnel first */
213 			CK_LIST_REMOVE(sc, chain);
214 			GRE_WAIT();
215 			free(sc->gre_hdr, M_GRE);
216 			/* XXX: should we notify about link state change? */
217 		}
218 		sc->gre_family = AF_INET6;
219 		sc->gre_hdr = ip6;
220 		sc->gre_oseq = 0;
221 		sc->gre_iseq = UINT32_MAX;
222 		in6_gre_attach(sc);
223 		break;
224 	case SIOCGIFPSRCADDR_IN6:
225 	case SIOCGIFPDSTADDR_IN6:
226 		if (sc->gre_family != AF_INET6) {
227 			error = EADDRNOTAVAIL;
228 			break;
229 		}
230 		src = (struct sockaddr_in6 *)&ifr->ifr_addr;
231 		memset(src, 0, sizeof(*src));
232 		src->sin6_family = AF_INET6;
233 		src->sin6_len = sizeof(*src);
234 		src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
235 		    sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
236 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
237 		if (error == 0)
238 			error = sa6_recoverscope(src);
239 		if (error != 0)
240 			memset(src, 0, sizeof(*src));
241 		break;
242 	}
243 	return (error);
244 }
245 
246 int
247 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused)
248 {
249 	struct greip6 *gi6;
250 
251 	gi6 = mtod(m, struct greip6 *);
252 	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
253 	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
254 }
255 
256 static const struct encaptab *ecookie = NULL;
257 static const struct encap_config ipv6_encap_cfg = {
258 	.proto = IPPROTO_GRE,
259 	.min_length = sizeof(struct greip6) +
260 #ifdef INET
261 	    sizeof(struct ip),
262 #else
263 	    sizeof(struct ip6_hdr),
264 #endif
265 	.exact_match = ENCAP_DRV_LOOKUP,
266 	.lookup = in6_gre_lookup,
267 	.input = gre_input
268 };
269 
270 void
271 in6_gre_init(void)
272 {
273 
274 	if (!IS_DEFAULT_VNET(curvnet))
275 		return;
276 	ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
277 }
278 
279 void
280 in6_gre_uninit(void)
281 {
282 
283 	if (IS_DEFAULT_VNET(curvnet))
284 		ip6_encap_detach(ecookie);
285 	if (V_ipv6_hashtbl != NULL)
286 		gre_hashdestroy(V_ipv6_hashtbl);
287 }
288