2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
35 #include <sys/cdefs.h>
37 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
45 #include <sys/errno.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
51 #include <net/ethernet.h>
53 #include <net/if_var.h>
54 #include <net/route.h>
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>
67 #include <netinet/ip6.h>
70 #include <net/if_gif.h>
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");
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.
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)
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)
96 in_gif_hashval(in_addr_t src, in_addr_t dst)
100 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
101 return (fnv_32_buf(&dst, sizeof(dst), ret));
105 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
107 struct gif_softc *tmp;
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)
114 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
117 if (tmp->gif_iphdr->ip_src.s_addr == src &&
118 tmp->gif_iphdr->ip_dst.s_addr == dst)
119 return (EADDRNOTAVAIL);
125 * Check that ingress address belongs to local host.
128 in_gif_set_running(struct gif_softc *sc)
131 if (in_localip(sc->gif_iphdr->ip_src))
132 GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
134 GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
138 * ifaddr_event handler.
139 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
140 * source address spoofing.
143 in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
146 const struct sockaddr_in *sin;
147 struct gif_softc *sc;
149 /* Check that VNET is ready */
150 if (V_ipv4_hashtbl == NULL)
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)
158 in_gif_set_running(sc);
163 in_gif_attach(struct gif_softc *sc)
166 if (sc->gif_options & GIF_IGNORE_SOURCE)
167 CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
169 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
171 CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
176 in_gif_setopts(struct gif_softc *sc, u_int options)
179 /* NOTE: we are protected with gif_ioctl_sx lock */
180 MPASS(sc->gif_family == AF_INET);
181 MPASS(sc->gif_options != options);
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;
194 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
196 struct ifreq *ifr = (struct ifreq *)data;
197 struct sockaddr_in *dst, *src;
201 /* NOTE: we are protected with gif_ioctl_sx lock */
205 src = &((struct in_aliasreq *)data)->ifra_addr;
206 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
209 if (src->sin_family != dst->sin_family ||
210 src->sin_family != AF_INET ||
211 src->sin_len != dst->sin_len ||
212 src->sin_len != sizeof(*src))
214 if (src->sin_addr.s_addr == INADDR_ANY ||
215 dst->sin_addr.s_addr == INADDR_ANY) {
216 error = EADDRNOTAVAIL;
219 if (V_ipv4_hashtbl == NULL) {
220 V_ipv4_hashtbl = gif_hashinit();
221 V_ipv4_srchashtbl = gif_hashinit();
223 error = in_gif_checkdup(sc, src->sin_addr.s_addr,
224 dst->sin_addr.s_addr);
225 if (error == EADDRNOTAVAIL)
227 if (error == EEXIST) {
228 /* Addresses are the same. Just return. */
232 ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
233 ip->ip_src.s_addr = src->sin_addr.s_addr;
234 ip->ip_dst.s_addr = dst->sin_addr.s_addr;
235 if (sc->gif_family != 0) {
236 /* Detach existing tunnel first */
237 CK_LIST_REMOVE(sc, srchash);
238 CK_LIST_REMOVE(sc, chain);
240 free(sc->gif_hdr, M_GIF);
241 /* XXX: should we notify about link state change? */
243 sc->gif_family = AF_INET;
246 in_gif_set_running(sc);
248 case SIOCGIFPSRCADDR:
249 case SIOCGIFPDSTADDR:
250 if (sc->gif_family != AF_INET) {
251 error = EADDRNOTAVAIL;
254 src = (struct sockaddr_in *)&ifr->ifr_addr;
255 memset(src, 0, sizeof(*src));
256 src->sin_family = AF_INET;
257 src->sin_len = sizeof(*src);
258 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
259 sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
260 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
262 memset(src, 0, sizeof(*src));
269 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
271 struct gif_softc *sc = ifp->if_softc;
274 /* prepend new IP header */
276 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
279 ip = mtod(m, struct ip *);
281 MPASS(sc->gif_family == AF_INET);
282 memcpy(ip, sc->gif_iphdr, sizeof(struct ip));
284 /* version will be set in ip_output() */
285 ip->ip_ttl = V_ip_gif_ttl;
286 ip->ip_len = htons(m->m_pkthdr.len);
289 return (ip_output(m, NULL, NULL, 0, NULL, NULL));
293 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
295 struct gif_softc *sc = arg;
303 KMOD_IPSTAT_INC(ips_nogif);
304 return (IPPROTO_DONE);
307 if ((gifp->if_flags & IFF_UP) != 0) {
308 ip = mtod(m, struct ip *);
311 gif_input(m, gifp, proto, ecn);
314 KMOD_IPSTAT_INC(ips_nogif);
316 return (IPPROTO_DONE);
320 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
323 struct gif_softc *sc;
326 if (V_ipv4_hashtbl == NULL)
330 ip = mtod(m, const struct ip *);
332 * NOTE: it is safe to iterate without any locking here, because softc
333 * can be reclaimed only when we are not within net_epoch_preempt
334 * section, but ip_encap lookup+input are executed in epoch section.
337 CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
338 ip->ip_src.s_addr), chain) {
340 * This is an inbound packet, its ip_dst is source address
343 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
344 sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
345 ret = ENCAP_DRV_LOOKUP;
351 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
353 CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
354 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
355 ret = 32 + 8; /* src + proto */
361 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
363 /* ingress filters on outer source */
364 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
365 if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
366 m->m_pkthdr.rcvif) == 0)
373 static const struct srcaddrtab *ipv4_srcaddrtab;
375 const struct encap_config encap;
376 const struct encaptab *cookie;
377 } ipv4_encap_cfg[] = {
380 .proto = IPPROTO_IPV4,
381 .min_length = 2 * sizeof(struct ip),
382 .exact_match = ENCAP_DRV_LOOKUP,
383 .lookup = in_gif_lookup,
384 .input = in_gif_input
390 .proto = IPPROTO_IPV6,
391 .min_length = sizeof(struct ip) +
392 sizeof(struct ip6_hdr),
393 .exact_match = ENCAP_DRV_LOOKUP,
394 .lookup = in_gif_lookup,
395 .input = in_gif_input
401 .proto = IPPROTO_ETHERIP,
402 .min_length = sizeof(struct ip) +
403 sizeof(struct etherip_header) +
404 sizeof(struct ether_header),
405 .exact_match = ENCAP_DRV_LOOKUP,
406 .lookup = in_gif_lookup,
407 .input = in_gif_input
417 if (!IS_DEFAULT_VNET(curvnet))
420 ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
422 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
423 ipv4_encap_cfg[i].cookie = ip_encap_attach(
424 &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
432 if (IS_DEFAULT_VNET(curvnet)) {
433 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
434 ip_encap_detach(ipv4_encap_cfg[i].cookie);
435 ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
437 if (V_ipv4_hashtbl != NULL) {
438 gif_hashdestroy(V_ipv4_hashtbl);
439 V_ipv4_hashtbl = NULL;
441 gif_hashdestroy(V_ipv4_srchashtbl);