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 epoch_tracker et;
200 struct sockaddr_in *dst, *src;
204 /* NOTE: we are protected with gif_ioctl_sx lock */
208 src = &((struct in_aliasreq *)data)->ifra_addr;
209 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
212 if (src->sin_family != dst->sin_family ||
213 src->sin_family != AF_INET ||
214 src->sin_len != dst->sin_len ||
215 src->sin_len != sizeof(*src))
217 if (src->sin_addr.s_addr == INADDR_ANY ||
218 dst->sin_addr.s_addr == INADDR_ANY) {
219 error = EADDRNOTAVAIL;
222 if (V_ipv4_hashtbl == NULL) {
223 V_ipv4_hashtbl = gif_hashinit();
224 V_ipv4_srchashtbl = gif_hashinit();
226 error = in_gif_checkdup(sc, src->sin_addr.s_addr,
227 dst->sin_addr.s_addr);
228 if (error == EADDRNOTAVAIL)
230 if (error == EEXIST) {
231 /* Addresses are the same. Just return. */
235 ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
236 ip->ip_src.s_addr = src->sin_addr.s_addr;
237 ip->ip_dst.s_addr = dst->sin_addr.s_addr;
238 if (sc->gif_family != 0) {
239 /* Detach existing tunnel first */
240 CK_LIST_REMOVE(sc, srchash);
241 CK_LIST_REMOVE(sc, chain);
243 free(sc->gif_hdr, M_GIF);
244 /* XXX: should we notify about link state change? */
246 sc->gif_family = AF_INET;
250 in_gif_set_running(sc);
253 case SIOCGIFPSRCADDR:
254 case SIOCGIFPDSTADDR:
255 if (sc->gif_family != AF_INET) {
256 error = EADDRNOTAVAIL;
259 src = (struct sockaddr_in *)&ifr->ifr_addr;
260 memset(src, 0, sizeof(*src));
261 src->sin_family = AF_INET;
262 src->sin_len = sizeof(*src);
263 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
264 sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
265 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
267 memset(src, 0, sizeof(*src));
274 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
276 struct gif_softc *sc = ifp->if_softc;
280 /* prepend new IP header */
282 len = sizeof(struct ip);
283 #ifndef __NO_STRICT_ALIGNMENT
284 if (proto == IPPROTO_ETHERIP)
285 len += ETHERIP_ALIGN;
287 M_PREPEND(m, len, M_NOWAIT);
290 #ifndef __NO_STRICT_ALIGNMENT
291 if (proto == IPPROTO_ETHERIP) {
292 len = mtod(m, vm_offset_t) & 3;
293 KASSERT(len == 0 || len == ETHERIP_ALIGN,
294 ("in_gif_output: unexpected misalignment"));
296 m->m_len -= ETHERIP_ALIGN;
299 ip = mtod(m, struct ip *);
301 MPASS(sc->gif_family == AF_INET);
302 bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
304 /* version will be set in ip_output() */
305 ip->ip_ttl = V_ip_gif_ttl;
306 ip->ip_len = htons(m->m_pkthdr.len);
309 return (ip_output(m, NULL, NULL, 0, NULL, NULL));
313 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
315 struct gif_softc *sc = arg;
323 KMOD_IPSTAT_INC(ips_nogif);
324 return (IPPROTO_DONE);
327 if ((gifp->if_flags & IFF_UP) != 0) {
328 ip = mtod(m, struct ip *);
331 gif_input(m, gifp, proto, ecn);
334 KMOD_IPSTAT_INC(ips_nogif);
336 return (IPPROTO_DONE);
340 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
343 struct gif_softc *sc;
346 if (V_ipv4_hashtbl == NULL)
350 ip = mtod(m, const struct ip *);
352 * NOTE: it is safe to iterate without any locking here, because softc
353 * can be reclaimed only when we are not within net_epoch_preempt
354 * section, but ip_encap lookup+input are executed in epoch section.
357 CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
358 ip->ip_src.s_addr), chain) {
360 * This is an inbound packet, its ip_dst is source address
363 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
364 sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
365 ret = ENCAP_DRV_LOOKUP;
371 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
373 CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
374 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
375 ret = 32 + 8; /* src + proto */
381 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
383 /* ingress filters on outer source */
384 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
385 if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
386 m->m_pkthdr.rcvif) == 0)
393 static const struct srcaddrtab *ipv4_srcaddrtab;
395 const struct encap_config encap;
396 const struct encaptab *cookie;
397 } ipv4_encap_cfg[] = {
400 .proto = IPPROTO_IPV4,
401 .min_length = 2 * sizeof(struct ip),
402 .exact_match = ENCAP_DRV_LOOKUP,
403 .lookup = in_gif_lookup,
404 .input = in_gif_input
410 .proto = IPPROTO_IPV6,
411 .min_length = sizeof(struct ip) +
412 sizeof(struct ip6_hdr),
413 .exact_match = ENCAP_DRV_LOOKUP,
414 .lookup = in_gif_lookup,
415 .input = in_gif_input
421 .proto = IPPROTO_ETHERIP,
422 .min_length = sizeof(struct ip) +
423 sizeof(struct etherip_header) +
424 sizeof(struct ether_header),
425 .exact_match = ENCAP_DRV_LOOKUP,
426 .lookup = in_gif_lookup,
427 .input = in_gif_input
437 if (!IS_DEFAULT_VNET(curvnet))
440 ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
442 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
443 ipv4_encap_cfg[i].cookie = ip_encap_attach(
444 &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
452 if (IS_DEFAULT_VNET(curvnet)) {
453 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
454 ip_encap_detach(ipv4_encap_cfg[i].cookie);
455 ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
457 if (V_ipv4_hashtbl != NULL) {
458 gif_hashdestroy(V_ipv4_hashtbl);
459 V_ipv4_hashtbl = NULL;
461 gif_hashdestroy(V_ipv4_srchashtbl);