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