xref: /freebsd/sys/netinet6/ip6_gre.c (revision f4f33ea0c752ff0f9bfad34991d5bbb54e71133d)
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 #include <sys/proc.h>
44 
45 #include <net/if.h>
46 #include <net/if_var.h>
47 #include <net/vnet.h>
48 
49 #include <netinet/in.h>
50 #ifdef INET
51 #include <net/ethernet.h>
52 #include <netinet/ip.h>
53 #endif
54 #include <netinet/ip_encap.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet6/in6_var.h>
58 #include <netinet6/scope6_var.h>
59 #include <net/if_gre.h>
60 
61 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
62 #define	V_ip6_gre_hlim		VNET(ip6_gre_hlim)
63 
64 SYSCTL_DECL(_net_inet6_ip6);
65 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
66     &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
67 
68 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL;
69 #define	V_ipv6_hashtbl		VNET(ipv6_hashtbl)
70 #define	GRE_HASH(src, dst)	(V_ipv6_hashtbl[\
71     in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
72 #define	GRE_HASH_SC(sc)		GRE_HASH(&(sc)->gre_oip6.ip6_src,\
73     &(sc)->gre_oip6.ip6_dst)
74 
75 static uint32_t
76 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
77 {
78 	uint32_t ret;
79 
80 	ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
81 	return (fnv_32_buf(dst, sizeof(*dst), ret));
82 }
83 
84 static int
85 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
86     const struct in6_addr *dst)
87 {
88 	struct gre_softc *tmp;
89 
90 	if (sc->gre_family == AF_INET6 &&
91 	    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
92 	    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst))
93 		return (EEXIST);
94 
95 	CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) {
96 		if (tmp == sc)
97 			continue;
98 		if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
99 		    IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
100 			return (EADDRNOTAVAIL);
101 	}
102 	return (0);
103 }
104 
105 static int
106 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
107 {
108 	const struct ip6_hdr *ip6;
109 	struct gre_softc *sc;
110 
111 	if (V_ipv6_hashtbl == NULL)
112 		return (0);
113 
114 	MPASS(in_epoch(net_epoch_preempt));
115 	ip6 = mtod(m, const struct ip6_hdr *);
116 	CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
117 		/*
118 		 * This is an inbound packet, its ip6_dst is source address
119 		 * in softc.
120 		 */
121 		if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
122 		    &ip6->ip6_dst) &&
123 		    IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
124 		    &ip6->ip6_src)) {
125 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
126 				return (0);
127 			*arg = sc;
128 			return (ENCAP_DRV_LOOKUP);
129 		}
130 	}
131 	return (0);
132 }
133 
134 static void
135 in6_gre_attach(struct gre_softc *sc)
136 {
137 
138 	sc->gre_hlen = sizeof(struct greip6);
139 	sc->gre_oip6.ip6_vfc = IPV6_VERSION;
140 	sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
141 	gre_updatehdr(sc, &sc->gre_gi6hdr->gi6_gre);
142 	CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
143 }
144 
145 void
146 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
147 {
148 
149 	MPASS(cmd == GRESKEY || cmd == GRESOPTS);
150 
151 	/* NOTE: we are protected with gre_ioctl_sx lock */
152 	MPASS(sc->gre_family == AF_INET6);
153 	CK_LIST_REMOVE(sc, chain);
154 	GRE_WAIT();
155 	if (cmd == GRESKEY)
156 		sc->gre_key = value;
157 	else
158 		sc->gre_options = value;
159 	in6_gre_attach(sc);
160 }
161 
162 int
163 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
164 {
165 	struct in6_ifreq *ifr = (struct in6_ifreq *)data;
166 	struct sockaddr_in6 *dst, *src;
167 	struct ip6_hdr *ip6;
168 	int error;
169 
170 	/* NOTE: we are protected with gre_ioctl_sx lock */
171 	error = EINVAL;
172 	switch (cmd) {
173 	case SIOCSIFPHYADDR_IN6:
174 		src = &((struct in6_aliasreq *)data)->ifra_addr;
175 		dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
176 
177 		/* sanity checks */
178 		if (src->sin6_family != dst->sin6_family ||
179 		    src->sin6_family != AF_INET6 ||
180 		    src->sin6_len != dst->sin6_len ||
181 		    src->sin6_len != sizeof(*src))
182 			break;
183 		if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
184 		    IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
185 			error = EADDRNOTAVAIL;
186 			break;
187 		}
188 		/*
189 		 * Check validity of the scope zone ID of the
190 		 * addresses, and convert it into the kernel
191 		 * internal form if necessary.
192 		 */
193 		if ((error = sa6_embedscope(src, 0)) != 0 ||
194 		    (error = sa6_embedscope(dst, 0)) != 0)
195 			break;
196 
197 		if (V_ipv6_hashtbl == NULL)
198 			V_ipv6_hashtbl = gre_hashinit();
199 		error = in6_gre_checkdup(sc, &src->sin6_addr,
200 		    &dst->sin6_addr);
201 		if (error == EADDRNOTAVAIL)
202 			break;
203 		if (error == EEXIST) {
204 			/* Addresses are the same. Just return. */
205 			error = 0;
206 			break;
207 		}
208 		ip6 = malloc(sizeof(struct greip6) + 3 * sizeof(uint32_t),
209 		    M_GRE, M_WAITOK | M_ZERO);
210 		ip6->ip6_src = src->sin6_addr;
211 		ip6->ip6_dst = dst->sin6_addr;
212 		if (sc->gre_family != 0) {
213 			/* Detach existing tunnel first */
214 			CK_LIST_REMOVE(sc, chain);
215 			GRE_WAIT();
216 			free(sc->gre_hdr, M_GRE);
217 			/* XXX: should we notify about link state change? */
218 		}
219 		sc->gre_family = AF_INET6;
220 		sc->gre_hdr = ip6;
221 		sc->gre_oseq = 0;
222 		sc->gre_iseq = UINT32_MAX;
223 		in6_gre_attach(sc);
224 		break;
225 	case SIOCGIFPSRCADDR_IN6:
226 	case SIOCGIFPDSTADDR_IN6:
227 		if (sc->gre_family != AF_INET6) {
228 			error = EADDRNOTAVAIL;
229 			break;
230 		}
231 		src = (struct sockaddr_in6 *)&ifr->ifr_addr;
232 		memset(src, 0, sizeof(*src));
233 		src->sin6_family = AF_INET6;
234 		src->sin6_len = sizeof(*src);
235 		src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
236 		    sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
237 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
238 		if (error == 0)
239 			error = sa6_recoverscope(src);
240 		if (error != 0)
241 			memset(src, 0, sizeof(*src));
242 		break;
243 	}
244 	return (error);
245 }
246 
247 int
248 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused)
249 {
250 	struct greip6 *gi6;
251 
252 	gi6 = mtod(m, struct greip6 *);
253 	gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
254 	return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
255 }
256 
257 static const struct encaptab *ecookie = NULL;
258 static const struct encap_config ipv6_encap_cfg = {
259 	.proto = IPPROTO_GRE,
260 	.min_length = sizeof(struct greip6) +
261 #ifdef INET
262 	    sizeof(struct ip),
263 #else
264 	    sizeof(struct ip6_hdr),
265 #endif
266 	.exact_match = ENCAP_DRV_LOOKUP,
267 	.lookup = in6_gre_lookup,
268 	.input = gre_input
269 };
270 
271 void
272 in6_gre_init(void)
273 {
274 
275 	if (!IS_DEFAULT_VNET(curvnet))
276 		return;
277 	ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
278 }
279 
280 void
281 in6_gre_uninit(void)
282 {
283 
284 	if (IS_DEFAULT_VNET(curvnet))
285 		ip6_encap_detach(ecookie);
286 	if (V_ipv6_hashtbl != NULL)
287 		gre_hashdestroy(V_ipv6_hashtbl);
288 }
289