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>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
53 #include <net/ethernet.h>
55 #include <net/if_var.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_encap.h>
65 #include <netinet/ip_ecn.h>
66 #include <netinet/in_fib.h>
69 #include <netinet/ip6.h>
72 #include <net/if_gif.h>
75 VNET_DEFINE_STATIC(int, ip_gif_ttl) = GIF_TTL;
76 #define V_ip_gif_ttl VNET(ip_gif_ttl)
77 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
78 &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
81 * We keep interfaces in a hash table using src+dst as key.
82 * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
84 VNET_DEFINE_STATIC(struct gif_list *, ipv4_hashtbl) = NULL;
85 VNET_DEFINE_STATIC(struct gif_list *, ipv4_srchashtbl) = NULL;
86 VNET_DEFINE_STATIC(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
87 #define V_ipv4_hashtbl VNET(ipv4_hashtbl)
88 #define V_ipv4_srchashtbl VNET(ipv4_srchashtbl)
89 #define V_ipv4_list VNET(ipv4_list)
91 #define GIF_HASH(src, dst) (V_ipv4_hashtbl[\
92 in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
93 #define GIF_SRCHASH(src) (V_ipv4_srchashtbl[\
94 fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
95 #define GIF_HASH_SC(sc) GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
96 (sc)->gif_iphdr->ip_dst.s_addr)
98 in_gif_hashval(in_addr_t src, in_addr_t dst)
102 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
103 return (fnv_32_buf(&dst, sizeof(dst), ret));
107 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
109 struct gif_softc *tmp;
111 if (sc->gif_family == AF_INET &&
112 sc->gif_iphdr->ip_src.s_addr == src &&
113 sc->gif_iphdr->ip_dst.s_addr == dst)
116 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
119 if (tmp->gif_iphdr->ip_src.s_addr == src &&
120 tmp->gif_iphdr->ip_dst.s_addr == dst)
121 return (EADDRNOTAVAIL);
127 * Check that ingress address belongs to local host.
130 in_gif_set_running(struct gif_softc *sc)
133 if (in_localip(sc->gif_iphdr->ip_src))
134 GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
136 GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
140 * ifaddr_event handler.
141 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
142 * source address spoofing.
145 in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
148 const struct sockaddr_in *sin;
149 struct gif_softc *sc;
151 /* Check that VNET is ready */
152 if (V_ipv4_hashtbl == NULL)
156 sin = (const struct sockaddr_in *)sa;
157 CK_LIST_FOREACH(sc, &GIF_SRCHASH(sin->sin_addr.s_addr), srchash) {
158 if (sc->gif_iphdr->ip_src.s_addr != sin->sin_addr.s_addr)
160 in_gif_set_running(sc);
165 in_gif_attach(struct gif_softc *sc)
168 if (sc->gif_options & GIF_IGNORE_SOURCE)
169 CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
171 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
173 CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
178 in_gif_setopts(struct gif_softc *sc, u_int options)
181 /* NOTE: we are protected with gif_ioctl_sx lock */
182 MPASS(sc->gif_family == AF_INET);
183 MPASS(sc->gif_options != options);
185 if ((options & GIF_IGNORE_SOURCE) !=
186 (sc->gif_options & GIF_IGNORE_SOURCE)) {
187 CK_LIST_REMOVE(sc, srchash);
188 CK_LIST_REMOVE(sc, chain);
189 sc->gif_options = options;
196 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
198 struct ifreq *ifr = (struct ifreq *)data;
199 struct sockaddr_in *dst, *src;
203 /* NOTE: we are protected with gif_ioctl_sx lock */
207 src = &((struct in_aliasreq *)data)->ifra_addr;
208 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
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))
216 if (src->sin_addr.s_addr == INADDR_ANY ||
217 dst->sin_addr.s_addr == INADDR_ANY) {
218 error = EADDRNOTAVAIL;
221 if (V_ipv4_hashtbl == NULL) {
222 V_ipv4_hashtbl = gif_hashinit();
223 V_ipv4_srchashtbl = gif_hashinit();
225 error = in_gif_checkdup(sc, src->sin_addr.s_addr,
226 dst->sin_addr.s_addr);
227 if (error == EADDRNOTAVAIL)
229 if (error == EEXIST) {
230 /* Addresses are the same. Just return. */
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);
242 free(sc->gif_hdr, M_GIF);
243 /* XXX: should we notify about link state change? */
245 sc->gif_family = AF_INET;
248 in_gif_set_running(sc);
250 case SIOCGIFPSRCADDR:
251 case SIOCGIFPDSTADDR:
252 if (sc->gif_family != AF_INET) {
253 error = EADDRNOTAVAIL;
256 src = (struct sockaddr_in *)&ifr->ifr_addr;
257 memset(src, 0, sizeof(*src));
258 src->sin_family = AF_INET;
259 src->sin_len = sizeof(*src);
260 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
261 sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
262 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
264 memset(src, 0, sizeof(*src));
271 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
273 struct gif_softc *sc = ifp->if_softc;
277 /* prepend new IP header */
279 len = sizeof(struct ip);
280 #ifndef __NO_STRICT_ALIGNMENT
281 if (proto == IPPROTO_ETHERIP)
282 len += ETHERIP_ALIGN;
284 M_PREPEND(m, len, M_NOWAIT);
287 #ifndef __NO_STRICT_ALIGNMENT
288 if (proto == IPPROTO_ETHERIP) {
289 len = mtod(m, vm_offset_t) & 3;
290 KASSERT(len == 0 || len == ETHERIP_ALIGN,
291 ("in_gif_output: unexpected misalignment"));
293 m->m_len -= ETHERIP_ALIGN;
296 ip = mtod(m, struct ip *);
298 MPASS(sc->gif_family == AF_INET);
299 bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
301 /* version will be set in ip_output() */
302 ip->ip_ttl = V_ip_gif_ttl;
303 ip->ip_len = htons(m->m_pkthdr.len);
306 return (ip_output(m, NULL, NULL, 0, NULL, NULL));
310 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
312 struct gif_softc *sc = arg;
320 KMOD_IPSTAT_INC(ips_nogif);
321 return (IPPROTO_DONE);
324 if ((gifp->if_flags & IFF_UP) != 0) {
325 ip = mtod(m, struct ip *);
328 gif_input(m, gifp, proto, ecn);
331 KMOD_IPSTAT_INC(ips_nogif);
333 return (IPPROTO_DONE);
337 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
340 struct gif_softc *sc;
343 if (V_ipv4_hashtbl == NULL)
347 ip = mtod(m, const struct ip *);
349 * NOTE: it is safe to iterate without any locking here, because softc
350 * can be reclaimed only when we are not within net_epoch_preempt
351 * section, but ip_encap lookup+input are executed in epoch section.
354 CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
355 ip->ip_src.s_addr), chain) {
357 * This is an inbound packet, its ip_dst is source address
360 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
361 sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
362 ret = ENCAP_DRV_LOOKUP;
368 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
370 CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
371 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
372 ret = 32 + 8; /* src + proto */
378 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
380 /* ingress filters on outer source */
381 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
382 if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
383 m->m_pkthdr.rcvif) == 0)
390 static const struct srcaddrtab *ipv4_srcaddrtab;
392 const struct encap_config encap;
393 const struct encaptab *cookie;
394 } ipv4_encap_cfg[] = {
397 .proto = IPPROTO_IPV4,
398 .min_length = 2 * sizeof(struct ip),
399 .exact_match = ENCAP_DRV_LOOKUP,
400 .lookup = in_gif_lookup,
401 .input = in_gif_input
407 .proto = IPPROTO_IPV6,
408 .min_length = sizeof(struct ip) +
409 sizeof(struct ip6_hdr),
410 .exact_match = ENCAP_DRV_LOOKUP,
411 .lookup = in_gif_lookup,
412 .input = in_gif_input
418 .proto = IPPROTO_ETHERIP,
419 .min_length = sizeof(struct ip) +
420 sizeof(struct etherip_header) +
421 sizeof(struct ether_header),
422 .exact_match = ENCAP_DRV_LOOKUP,
423 .lookup = in_gif_lookup,
424 .input = in_gif_input
434 if (!IS_DEFAULT_VNET(curvnet))
437 ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
439 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
440 ipv4_encap_cfg[i].cookie = ip_encap_attach(
441 &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
449 if (IS_DEFAULT_VNET(curvnet)) {
450 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
451 ip_encap_detach(ipv4_encap_cfg[i].cookie);
452 ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
454 if (V_ipv4_hashtbl != NULL) {
455 gif_hashdestroy(V_ipv4_hashtbl);
456 V_ipv4_hashtbl = NULL;
458 gif_hashdestroy(V_ipv4_srchashtbl);