xref: /freebsd/sys/netinet/ip_gre.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * Copyright (c) 2014, 2018 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  * IPv6-over-GRE contributed by Gert Doering <gert@greenie.muc.de>
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $NetBSD: ip_gre.c,v 1.29 2003/09/05 23:02:43 itojun Exp $
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_inet.h"
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/jail.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/errno.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/malloc.h>
53 #include <sys/proc.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/vnet.h>
58 
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_var.h>
64 
65 #ifdef INET6
66 #include <netinet/ip6.h>
67 #endif
68 
69 #include <net/if_gre.h>
70 
71 #define	GRE_TTL			30
72 VNET_DEFINE(int, ip_gre_ttl) = GRE_TTL;
73 #define	V_ip_gre_ttl		VNET(ip_gre_ttl)
74 SYSCTL_INT(_net_inet_ip, OID_AUTO, grettl, CTLFLAG_VNET | CTLFLAG_RW,
75     &VNET_NAME(ip_gre_ttl), 0, "Default TTL value for encapsulated packets");
76 
77 VNET_DEFINE_STATIC(struct gre_list *, ipv4_hashtbl) = NULL;
78 VNET_DEFINE_STATIC(struct gre_list *, ipv4_srchashtbl) = NULL;
79 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
80 #define	V_ipv4_srchashtbl	VNET(ipv4_srchashtbl)
81 #define	GRE_HASH(src, dst)	(V_ipv4_hashtbl[\
82     in_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
83 #define	GRE_SRCHASH(src)	(V_ipv4_srchashtbl[\
84     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)])
85 #define	GRE_HASH_SC(sc)		GRE_HASH((sc)->gre_oip.ip_src.s_addr,\
86     (sc)->gre_oip.ip_dst.s_addr)
87 
88 static uint32_t
89 in_gre_hashval(in_addr_t src, in_addr_t dst)
90 {
91 	uint32_t ret;
92 
93 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
94 	return (fnv_32_buf(&dst, sizeof(dst), ret));
95 }
96 
97 static int
98 in_gre_checkdup(const struct gre_softc *sc, in_addr_t src, in_addr_t dst)
99 {
100 	struct gre_softc *tmp;
101 
102 	if (sc->gre_family == AF_INET &&
103 	    sc->gre_oip.ip_src.s_addr == src &&
104 	    sc->gre_oip.ip_dst.s_addr == dst)
105 		return (EEXIST);
106 
107 	CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) {
108 		if (tmp == sc)
109 			continue;
110 		if (tmp->gre_oip.ip_src.s_addr == src &&
111 		    tmp->gre_oip.ip_dst.s_addr == dst)
112 			return (EADDRNOTAVAIL);
113 	}
114 	return (0);
115 }
116 
117 static int
118 in_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
119 {
120 	const struct ip *ip;
121 	struct gre_softc *sc;
122 
123 	if (V_ipv4_hashtbl == NULL)
124 		return (0);
125 
126 	MPASS(in_epoch(net_epoch_preempt));
127 	ip = mtod(m, const struct ip *);
128 	CK_LIST_FOREACH(sc, &GRE_HASH(ip->ip_dst.s_addr,
129 	    ip->ip_src.s_addr), chain) {
130 		/*
131 		 * This is an inbound packet, its ip_dst is source address
132 		 * in softc.
133 		 */
134 		if (sc->gre_oip.ip_src.s_addr == ip->ip_dst.s_addr &&
135 		    sc->gre_oip.ip_dst.s_addr == ip->ip_src.s_addr) {
136 			if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
137 				return (0);
138 			*arg = sc;
139 			return (ENCAP_DRV_LOOKUP);
140 		}
141 	}
142 	return (0);
143 }
144 
145 /*
146  * Check that ingress address belongs to local host.
147  */
148 static void
149 in_gre_set_running(struct gre_softc *sc)
150 {
151 
152 	if (in_localip(sc->gre_oip.ip_src))
153 		GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
154 	else
155 		GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
156 }
157 
158 /*
159  * ifaddr_event handler.
160  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
161  * source address spoofing.
162  */
163 static void
164 in_gre_srcaddr(void *arg __unused, const struct sockaddr *sa,
165     int event __unused)
166 {
167 	const struct sockaddr_in *sin;
168 	struct gre_softc *sc;
169 
170 	/* Check that VNET is ready */
171 	if (V_ipv4_hashtbl == NULL)
172 		return;
173 
174 	MPASS(in_epoch(net_epoch_preempt));
175 	sin = (const struct sockaddr_in *)sa;
176 	CK_LIST_FOREACH(sc, &GRE_SRCHASH(sin->sin_addr.s_addr), srchash) {
177 		if (sc->gre_oip.ip_src.s_addr != sin->sin_addr.s_addr)
178 			continue;
179 		in_gre_set_running(sc);
180 	}
181 }
182 
183 static void
184 in_gre_attach(struct gre_softc *sc)
185 {
186 
187 	sc->gre_hlen = sizeof(struct greip);
188 	sc->gre_oip.ip_v = IPVERSION;
189 	sc->gre_oip.ip_hl = sizeof(struct ip) >> 2;
190 	sc->gre_oip.ip_p = IPPROTO_GRE;
191 	gre_updatehdr(sc, &sc->gre_gihdr->gi_gre);
192 	CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
193 	CK_LIST_INSERT_HEAD(&GRE_SRCHASH(sc->gre_oip.ip_src.s_addr),
194 	    sc, srchash);
195 }
196 
197 void
198 in_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
199 {
200 
201 	MPASS(cmd == GRESKEY || cmd == GRESOPTS);
202 
203 	/* NOTE: we are protected with gre_ioctl_sx lock */
204 	MPASS(sc->gre_family == AF_INET);
205 	CK_LIST_REMOVE(sc, chain);
206 	CK_LIST_REMOVE(sc, srchash);
207 	GRE_WAIT();
208 	if (cmd == GRESKEY)
209 		sc->gre_key = value;
210 	else
211 		sc->gre_options = value;
212 	in_gre_attach(sc);
213 }
214 
215 int
216 in_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
217 {
218 	struct ifreq *ifr = (struct ifreq *)data;
219 	struct sockaddr_in *dst, *src;
220 	struct ip *ip;
221 	int error;
222 
223 	/* NOTE: we are protected with gre_ioctl_sx lock */
224 	error = EINVAL;
225 	switch (cmd) {
226 	case SIOCSIFPHYADDR:
227 		src = &((struct in_aliasreq *)data)->ifra_addr;
228 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
229 
230 		/* sanity checks */
231 		if (src->sin_family != dst->sin_family ||
232 		    src->sin_family != AF_INET ||
233 		    src->sin_len != dst->sin_len ||
234 		    src->sin_len != sizeof(*src))
235 			break;
236 		if (src->sin_addr.s_addr == INADDR_ANY ||
237 		    dst->sin_addr.s_addr == INADDR_ANY) {
238 			error = EADDRNOTAVAIL;
239 			break;
240 		}
241 		if (V_ipv4_hashtbl == NULL) {
242 			V_ipv4_hashtbl = gre_hashinit();
243 			V_ipv4_srchashtbl = gre_hashinit();
244 		}
245 		error = in_gre_checkdup(sc, src->sin_addr.s_addr,
246 		    dst->sin_addr.s_addr);
247 		if (error == EADDRNOTAVAIL)
248 			break;
249 		if (error == EEXIST) {
250 			/* Addresses are the same. Just return. */
251 			error = 0;
252 			break;
253 		}
254 		ip = malloc(sizeof(struct greip) + 3 * sizeof(uint32_t),
255 		    M_GRE, M_WAITOK | M_ZERO);
256 		ip->ip_src.s_addr = src->sin_addr.s_addr;
257 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
258 		if (sc->gre_family != 0) {
259 			/* Detach existing tunnel first */
260 			CK_LIST_REMOVE(sc, chain);
261 			CK_LIST_REMOVE(sc, srchash);
262 			GRE_WAIT();
263 			free(sc->gre_hdr, M_GRE);
264 			/* XXX: should we notify about link state change? */
265 		}
266 		sc->gre_family = AF_INET;
267 		sc->gre_hdr = ip;
268 		sc->gre_oseq = 0;
269 		sc->gre_iseq = UINT32_MAX;
270 		in_gre_attach(sc);
271 		in_gre_set_running(sc);
272 		break;
273 	case SIOCGIFPSRCADDR:
274 	case SIOCGIFPDSTADDR:
275 		if (sc->gre_family != AF_INET) {
276 			error = EADDRNOTAVAIL;
277 			break;
278 		}
279 		src = (struct sockaddr_in *)&ifr->ifr_addr;
280 		memset(src, 0, sizeof(*src));
281 		src->sin_family = AF_INET;
282 		src->sin_len = sizeof(*src);
283 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
284 		    sc->gre_oip.ip_src: sc->gre_oip.ip_dst;
285 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
286 		if (error != 0)
287 			memset(src, 0, sizeof(*src));
288 		break;
289 	}
290 	return (error);
291 }
292 
293 int
294 in_gre_output(struct mbuf *m, int af, int hlen)
295 {
296 	struct greip *gi;
297 
298 	gi = mtod(m, struct greip *);
299 	switch (af) {
300 	case AF_INET:
301 		/*
302 		 * gre_transmit() has used M_PREPEND() that doesn't guarantee
303 		 * m_data is contiguous more than hlen bytes. Use m_copydata()
304 		 * here to avoid m_pullup().
305 		 */
306 		m_copydata(m, hlen + offsetof(struct ip, ip_tos),
307 		    sizeof(u_char), &gi->gi_ip.ip_tos);
308 		m_copydata(m, hlen + offsetof(struct ip, ip_id),
309 		    sizeof(u_short), (caddr_t)&gi->gi_ip.ip_id);
310 		break;
311 #ifdef INET6
312 	case AF_INET6:
313 		gi->gi_ip.ip_tos = 0; /* XXX */
314 		ip_fillid(&gi->gi_ip);
315 		break;
316 #endif
317 	}
318 	gi->gi_ip.ip_ttl = V_ip_gre_ttl;
319 	gi->gi_ip.ip_len = htons(m->m_pkthdr.len);
320 	return (ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL));
321 }
322 
323 static const struct srcaddrtab *ipv4_srcaddrtab = NULL;
324 static const struct encaptab *ecookie = NULL;
325 static const struct encap_config ipv4_encap_cfg = {
326 	.proto = IPPROTO_GRE,
327 	.min_length = sizeof(struct greip) + sizeof(struct ip),
328 	.exact_match = ENCAP_DRV_LOOKUP,
329 	.lookup = in_gre_lookup,
330 	.input = gre_input
331 };
332 
333 void
334 in_gre_init(void)
335 {
336 
337 	if (!IS_DEFAULT_VNET(curvnet))
338 		return;
339 	ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gre_srcaddr,
340 	    NULL, M_WAITOK);
341 	ecookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
342 }
343 
344 void
345 in_gre_uninit(void)
346 {
347 
348 	if (IS_DEFAULT_VNET(curvnet)) {
349 		ip_encap_detach(ecookie);
350 		ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
351 	}
352 	if (V_ipv4_hashtbl != NULL) {
353 		gre_hashdestroy(V_ipv4_hashtbl);
354 		V_ipv4_hashtbl = NULL;
355 		GRE_WAIT();
356 		gre_hashdestroy(V_ipv4_srchashtbl);
357 	}
358 }
359