xref: /freebsd/sys/netinet/in_gif.c (revision 7b71f57f4e514a2ab7308ce4147e14d90e099ad0)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
33  */
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/jail.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/mbuf.h>
44 #include <sys/errno.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49 
50 #include <net/ethernet.h>
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_private.h>
54 #include <net/route.h>
55 #include <net/vnet.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip_encap.h>
63 #include <netinet/ip_ecn.h>
64 #include <netinet/in_fib.h>
65 
66 #ifdef INET6
67 #include <netinet/ip6.h>
68 #endif
69 
70 #include <net/if_gif.h>
71 
72 #define GIF_TTL		30
73 VNET_DEFINE_STATIC(int, ip_gif_ttl) = GIF_TTL;
74 #define	V_ip_gif_ttl		VNET(ip_gif_ttl)
75 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
76     &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
77 
78 /*
79  * We keep interfaces in a hash table using src+dst as key.
80  * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
81  */
82 VNET_DEFINE_STATIC(struct gif_list *, ipv4_hashtbl) = NULL;
83 VNET_DEFINE_STATIC(struct gif_list *, ipv4_srchashtbl) = NULL;
84 VNET_DEFINE_STATIC(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
85 #define	V_ipv4_hashtbl		VNET(ipv4_hashtbl)
86 #define	V_ipv4_srchashtbl	VNET(ipv4_srchashtbl)
87 #define	V_ipv4_list		VNET(ipv4_list)
88 
89 #define	GIF_HASH(src, dst)	(V_ipv4_hashtbl[\
90     in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
91 #define	GIF_SRCHASH(src)	(V_ipv4_srchashtbl[\
92     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
93 #define	GIF_HASH_SC(sc)		GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
94     (sc)->gif_iphdr->ip_dst.s_addr)
95 static uint32_t
in_gif_hashval(in_addr_t src,in_addr_t dst)96 in_gif_hashval(in_addr_t src, in_addr_t dst)
97 {
98 	uint32_t ret;
99 
100 	ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
101 	return (fnv_32_buf(&dst, sizeof(dst), ret));
102 }
103 
104 static int
in_gif_checkdup(const struct gif_softc * sc,in_addr_t src,in_addr_t dst)105 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
106 {
107 	struct gif_softc *tmp;
108 
109 	if (sc->gif_family == AF_INET &&
110 	    sc->gif_iphdr->ip_src.s_addr == src &&
111 	    sc->gif_iphdr->ip_dst.s_addr == dst)
112 		return (EEXIST);
113 
114 	CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
115 		if (tmp == sc)
116 			continue;
117 		if (tmp->gif_iphdr->ip_src.s_addr == src &&
118 		    tmp->gif_iphdr->ip_dst.s_addr == dst)
119 			return (EADDRNOTAVAIL);
120 	}
121 	return (0);
122 }
123 
124 /*
125  * Check that ingress address belongs to local host.
126  */
127 static void
in_gif_set_running(struct gif_softc * sc)128 in_gif_set_running(struct gif_softc *sc)
129 {
130 
131 	if (in_localip(sc->gif_iphdr->ip_src))
132 		GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
133 	else
134 		GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
135 }
136 
137 /*
138  * ifaddr_event handler.
139  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
140  * source address spoofing.
141  */
142 static void
in_gif_srcaddr(void * arg __unused,const struct sockaddr * sa,int event __unused)143 in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
144     int event __unused)
145 {
146 	const struct sockaddr_in *sin;
147 	struct gif_softc *sc;
148 
149 	/* Check that VNET is ready */
150 	if (V_ipv4_hashtbl == NULL)
151 		return;
152 
153 	NET_EPOCH_ASSERT();
154 	sin = (const struct sockaddr_in *)sa;
155 	CK_LIST_FOREACH(sc, &GIF_SRCHASH(sin->sin_addr.s_addr), srchash) {
156 		if (sc->gif_iphdr->ip_src.s_addr != sin->sin_addr.s_addr)
157 			continue;
158 		in_gif_set_running(sc);
159 	}
160 }
161 
162 static void
in_gif_attach(struct gif_softc * sc)163 in_gif_attach(struct gif_softc *sc)
164 {
165 
166 	if (sc->gif_options & GIF_IGNORE_SOURCE)
167 		CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
168 	else
169 		CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
170 
171 	CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
172 	    sc, srchash);
173 }
174 
175 int
in_gif_setopts(struct gif_softc * sc,u_int options)176 in_gif_setopts(struct gif_softc *sc, u_int options)
177 {
178 
179 	/* NOTE: we are protected with gif_ioctl_sx lock */
180 	MPASS(sc->gif_family == AF_INET);
181 	MPASS(sc->gif_options != options);
182 
183 	if ((options & GIF_IGNORE_SOURCE) !=
184 	    (sc->gif_options & GIF_IGNORE_SOURCE)) {
185 		CK_LIST_REMOVE(sc, srchash);
186 		CK_LIST_REMOVE(sc, chain);
187 		sc->gif_options = options;
188 		in_gif_attach(sc);
189 	}
190 	return (0);
191 }
192 
193 int
in_gif_ioctl(struct gif_softc * sc,u_long cmd,caddr_t data)194 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
195 {
196 	struct ifreq *ifr = (struct ifreq *)data;
197 	struct epoch_tracker et;
198 	struct sockaddr_in *dst, *src;
199 	struct ip *ip;
200 	int error;
201 
202 	/* NOTE: we are protected with gif_ioctl_sx lock */
203 	error = EINVAL;
204 	switch (cmd) {
205 	case SIOCSIFPHYADDR:
206 		src = &((struct in_aliasreq *)data)->ifra_addr;
207 		dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
208 
209 		/* sanity checks */
210 		if (src->sin_family != dst->sin_family ||
211 		    src->sin_family != AF_INET ||
212 		    src->sin_len != dst->sin_len ||
213 		    src->sin_len != sizeof(*src))
214 			break;
215 		if (src->sin_addr.s_addr == INADDR_ANY ||
216 		    dst->sin_addr.s_addr == INADDR_ANY) {
217 			error = EADDRNOTAVAIL;
218 			break;
219 		}
220 		if (V_ipv4_hashtbl == NULL) {
221 			V_ipv4_hashtbl = gif_hashinit();
222 			V_ipv4_srchashtbl = gif_hashinit();
223 		}
224 		error = in_gif_checkdup(sc, src->sin_addr.s_addr,
225 		    dst->sin_addr.s_addr);
226 		if (error == EADDRNOTAVAIL)
227 			break;
228 		if (error == EEXIST) {
229 			/* Addresses are the same. Just return. */
230 			error = 0;
231 			break;
232 		}
233 		ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
234 		ip->ip_src.s_addr = src->sin_addr.s_addr;
235 		ip->ip_dst.s_addr = dst->sin_addr.s_addr;
236 		if (sc->gif_family != 0) {
237 			/* Detach existing tunnel first */
238 			CK_LIST_REMOVE(sc, srchash);
239 			CK_LIST_REMOVE(sc, chain);
240 			GIF_WAIT();
241 			free(sc->gif_hdr, M_GIF);
242 			/* XXX: should we notify about link state change? */
243 		}
244 		sc->gif_family = AF_INET;
245 		sc->gif_iphdr = ip;
246 		in_gif_attach(sc);
247 		NET_EPOCH_ENTER(et);
248 		in_gif_set_running(sc);
249 		NET_EPOCH_EXIT(et);
250 		break;
251 	case SIOCGIFPSRCADDR:
252 	case SIOCGIFPDSTADDR:
253 		if (sc->gif_family != AF_INET) {
254 			error = EADDRNOTAVAIL;
255 			break;
256 		}
257 		src = (struct sockaddr_in *)&ifr->ifr_addr;
258 		memset(src, 0, sizeof(*src));
259 		src->sin_family = AF_INET;
260 		src->sin_len = sizeof(*src);
261 		src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
262 		    sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
263 		error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
264 		if (error != 0)
265 			memset(src, 0, sizeof(*src));
266 		break;
267 	}
268 	return (error);
269 }
270 
271 int
in_gif_output(struct ifnet * ifp,struct mbuf * m,int proto,uint8_t ecn)272 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
273 {
274 	struct gif_softc *sc = ifp->if_softc;
275 	struct ip *ip;
276 
277 	/* prepend new IP header */
278 	NET_EPOCH_ASSERT();
279 	M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
280 	if (m == NULL)
281 		return (ENOBUFS);
282 	ip = mtod(m, struct ip *);
283 
284 	MPASS(sc->gif_family == AF_INET);
285 	memcpy(ip, sc->gif_iphdr, sizeof(struct ip));
286 	ip->ip_p = proto;
287 	/* version will be set in ip_output() */
288 	ip->ip_ttl = V_ip_gif_ttl;
289 	ip->ip_len = htons(m->m_pkthdr.len);
290 	ip->ip_tos = ecn;
291 
292 	return (ip_output(m, NULL, NULL, 0, NULL, NULL));
293 }
294 
295 static int
in_gif_input(struct mbuf * m,int off,int proto,void * arg)296 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
297 {
298 	struct gif_softc *sc = arg;
299 	struct ifnet *gifp;
300 	struct ip *ip;
301 	uint8_t ecn;
302 
303 	NET_EPOCH_ASSERT();
304 	if (sc == NULL) {
305 		m_freem(m);
306 		KMOD_IPSTAT_INC(ips_nogif);
307 		return (IPPROTO_DONE);
308 	}
309 	gifp = GIF2IFP(sc);
310 	if ((gifp->if_flags & IFF_UP) != 0) {
311 		ip = mtod(m, struct ip *);
312 		ecn = ip->ip_tos;
313 		m_adj(m, off);
314 		gif_input(m, gifp, proto, ecn);
315 	} else {
316 		m_freem(m);
317 		KMOD_IPSTAT_INC(ips_nogif);
318 	}
319 	return (IPPROTO_DONE);
320 }
321 
322 static int
in_gif_lookup(const struct mbuf * m,int off,int proto,void ** arg)323 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
324 {
325 	const struct ip *ip;
326 	struct gif_softc *sc;
327 	int ret;
328 
329 	if (V_ipv4_hashtbl == NULL)
330 		return (0);
331 
332 	NET_EPOCH_ASSERT();
333 	ip = mtod(m, const struct ip *);
334 	/*
335 	 * NOTE: it is safe to iterate without any locking here, because softc
336 	 * can be reclaimed only when we are not within net_epoch_preempt
337 	 * section, but ip_encap lookup+input are executed in epoch section.
338 	 */
339 	ret = 0;
340 	CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
341 	    ip->ip_src.s_addr), chain) {
342 		/*
343 		 * This is an inbound packet, its ip_dst is source address
344 		 * in softc.
345 		 */
346 		if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
347 		    sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
348 			ret = ENCAP_DRV_LOOKUP;
349 			goto done;
350 		}
351 	}
352 	/*
353 	 * No exact match.
354 	 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
355 	 */
356 	CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
357 		if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
358 			ret = 32 + 8; /* src + proto */
359 			goto done;
360 		}
361 	}
362 	return (0);
363 done:
364 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
365 		return (0);
366 	/* ingress filters on outer source */
367 	if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
368 		if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
369 					m->m_pkthdr.rcvif) == 0)
370 			return (0);
371 	}
372 	*arg = sc;
373 	return (ret);
374 }
375 
376 static const struct srcaddrtab *ipv4_srcaddrtab;
377 static struct {
378 	const struct encap_config encap;
379 	const struct encaptab *cookie;
380 } ipv4_encap_cfg[] = {
381 	{
382 		.encap = {
383 			.proto = IPPROTO_IPV4,
384 			.min_length = 2 * sizeof(struct ip),
385 			.exact_match = ENCAP_DRV_LOOKUP,
386 			.lookup = in_gif_lookup,
387 			.input = in_gif_input
388 		},
389 	},
390 #ifdef INET6
391 	{
392 		.encap = {
393 			.proto = IPPROTO_IPV6,
394 			.min_length = sizeof(struct ip) +
395 			    sizeof(struct ip6_hdr),
396 			.exact_match = ENCAP_DRV_LOOKUP,
397 			.lookup = in_gif_lookup,
398 			.input = in_gif_input
399 		},
400 	},
401 #endif
402 	{
403 		.encap = {
404 			.proto = IPPROTO_ETHERIP,
405 			.min_length = sizeof(struct ip) +
406 			    sizeof(struct etherip_header) +
407 			    sizeof(struct ether_header),
408 			.exact_match = ENCAP_DRV_LOOKUP,
409 			.lookup = in_gif_lookup,
410 			.input = in_gif_input
411 		},
412 	}
413 };
414 
415 void
in_gif_init(void)416 in_gif_init(void)
417 {
418 	int i;
419 
420 	if (!IS_DEFAULT_VNET(curvnet))
421 		return;
422 
423 	ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
424 	    NULL, M_WAITOK);
425 	for (i = 0; i < nitems(ipv4_encap_cfg); i++)
426 		ipv4_encap_cfg[i].cookie = ip_encap_attach(
427 		    &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
428 }
429 
430 void
in_gif_uninit(void)431 in_gif_uninit(void)
432 {
433 	int i;
434 
435 	if (IS_DEFAULT_VNET(curvnet)) {
436 		for (i = 0; i < nitems(ipv4_encap_cfg); i++)
437 			ip_encap_detach(ipv4_encap_cfg[i].cookie);
438 		ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
439 	}
440 	if (V_ipv4_hashtbl != NULL) {
441 		gif_hashdestroy(V_ipv4_hashtbl);
442 		V_ipv4_hashtbl = NULL;
443 		GIF_WAIT();
444 		gif_hashdestroy(V_ipv4_srchashtbl);
445 	}
446 }
447