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>
52 #include <net/ethernet.h>
54 #include <net/if_var.h>
55 #include <net/route.h>
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>
68 #include <netinet/ip6.h>
71 #include <net/if_gif.h>
74 static VNET_DEFINE(int, ip_gif_ttl) = GIF_TTL;
75 #define V_ip_gif_ttl VNET(ip_gif_ttl)
76 SYSCTL_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");
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.
83 static VNET_DEFINE(struct gif_list *, ipv4_hashtbl) = NULL;
84 static VNET_DEFINE(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
85 #define V_ipv4_hashtbl VNET(ipv4_hashtbl)
86 #define V_ipv4_list VNET(ipv4_list)
88 #define GIF_HASH(src, dst) (V_ipv4_hashtbl[\
89 in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
90 #define GIF_HASH_SC(sc) GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
91 (sc)->gif_iphdr->ip_dst.s_addr)
93 in_gif_hashval(in_addr_t src, in_addr_t dst)
97 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
98 return (fnv_32_buf(&dst, sizeof(dst), ret));
102 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
104 struct gif_softc *tmp;
106 if (sc->gif_family == AF_INET &&
107 sc->gif_iphdr->ip_src.s_addr == src &&
108 sc->gif_iphdr->ip_dst.s_addr == dst)
111 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
114 if (tmp->gif_iphdr->ip_src.s_addr == src &&
115 tmp->gif_iphdr->ip_dst.s_addr == dst)
116 return (EADDRNOTAVAIL);
122 in_gif_attach(struct gif_softc *sc)
125 if (sc->gif_options & GIF_IGNORE_SOURCE)
126 CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
128 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
132 in_gif_setopts(struct gif_softc *sc, u_int options)
135 /* NOTE: we are protected with gif_ioctl_sx lock */
136 MPASS(sc->gif_family == AF_INET);
137 MPASS(sc->gif_options != options);
139 if ((options & GIF_IGNORE_SOURCE) !=
140 (sc->gif_options & GIF_IGNORE_SOURCE)) {
141 CK_LIST_REMOVE(sc, chain);
142 sc->gif_options = options;
149 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
151 struct ifreq *ifr = (struct ifreq *)data;
152 struct sockaddr_in *dst, *src;
156 /* NOTE: we are protected with gif_ioctl_sx lock */
160 src = &((struct in_aliasreq *)data)->ifra_addr;
161 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
164 if (src->sin_family != dst->sin_family ||
165 src->sin_family != AF_INET ||
166 src->sin_len != dst->sin_len ||
167 src->sin_len != sizeof(*src))
169 if (src->sin_addr.s_addr == INADDR_ANY ||
170 dst->sin_addr.s_addr == INADDR_ANY) {
171 error = EADDRNOTAVAIL;
174 if (V_ipv4_hashtbl == NULL)
175 V_ipv4_hashtbl = gif_hashinit();
176 error = in_gif_checkdup(sc, src->sin_addr.s_addr,
177 dst->sin_addr.s_addr);
178 if (error == EADDRNOTAVAIL)
180 if (error == EEXIST) {
181 /* Addresses are the same. Just return. */
185 ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
186 ip->ip_src.s_addr = src->sin_addr.s_addr;
187 ip->ip_dst.s_addr = dst->sin_addr.s_addr;
188 if (sc->gif_family != 0) {
189 /* Detach existing tunnel first */
190 CK_LIST_REMOVE(sc, chain);
192 free(sc->gif_hdr, M_GIF);
193 /* XXX: should we notify about link state change? */
195 sc->gif_family = AF_INET;
199 case SIOCGIFPSRCADDR:
200 case SIOCGIFPDSTADDR:
201 if (sc->gif_family != AF_INET) {
202 error = EADDRNOTAVAIL;
205 src = (struct sockaddr_in *)&ifr->ifr_addr;
206 memset(src, 0, sizeof(*src));
207 src->sin_family = AF_INET;
208 src->sin_len = sizeof(*src);
209 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
210 sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
211 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
213 memset(src, 0, sizeof(*src));
220 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
222 struct gif_softc *sc = ifp->if_softc;
226 /* prepend new IP header */
228 len = sizeof(struct ip);
229 #ifndef __NO_STRICT_ALIGNMENT
230 if (proto == IPPROTO_ETHERIP)
231 len += ETHERIP_ALIGN;
233 M_PREPEND(m, len, M_NOWAIT);
236 #ifndef __NO_STRICT_ALIGNMENT
237 if (proto == IPPROTO_ETHERIP) {
238 len = mtod(m, vm_offset_t) & 3;
239 KASSERT(len == 0 || len == ETHERIP_ALIGN,
240 ("in_gif_output: unexpected misalignment"));
242 m->m_len -= ETHERIP_ALIGN;
245 ip = mtod(m, struct ip *);
247 MPASS(sc->gif_family == AF_INET);
248 bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
250 /* version will be set in ip_output() */
251 ip->ip_ttl = V_ip_gif_ttl;
252 ip->ip_len = htons(m->m_pkthdr.len);
255 return (ip_output(m, NULL, NULL, 0, NULL, NULL));
259 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
261 struct gif_softc *sc = arg;
269 KMOD_IPSTAT_INC(ips_nogif);
270 return (IPPROTO_DONE);
273 if ((gifp->if_flags & IFF_UP) != 0) {
274 ip = mtod(m, struct ip *);
277 gif_input(m, gifp, proto, ecn);
280 KMOD_IPSTAT_INC(ips_nogif);
282 return (IPPROTO_DONE);
286 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
289 struct gif_softc *sc;
293 ip = mtod(m, const struct ip *);
295 * NOTE: it is safe to iterate without any locking here, because softc
296 * can be reclaimed only when we are not within net_epoch_preempt
297 * section, but ip_encap lookup+input are executed in epoch section.
300 CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
301 ip->ip_src.s_addr), chain) {
303 * This is an inbound packet, its ip_dst is source address
306 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
307 sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
308 ret = ENCAP_DRV_LOOKUP;
314 * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
316 CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
317 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
318 ret = 32 + 8; /* src + proto */
324 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
326 /* ingress filters on outer source */
327 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
328 struct nhop4_basic nh4;
332 if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0)
334 if (nh4.nh_ifp != m->m_pkthdr.rcvif)
342 const struct encap_config encap;
343 const struct encaptab *cookie;
344 } ipv4_encap_cfg[] = {
347 .proto = IPPROTO_IPV4,
348 .min_length = 2 * sizeof(struct ip),
349 .exact_match = ENCAP_DRV_LOOKUP,
350 .lookup = in_gif_lookup,
351 .input = in_gif_input
357 .proto = IPPROTO_IPV6,
358 .min_length = sizeof(struct ip) +
359 sizeof(struct ip6_hdr),
360 .exact_match = ENCAP_DRV_LOOKUP,
361 .lookup = in_gif_lookup,
362 .input = in_gif_input
368 .proto = IPPROTO_ETHERIP,
369 .min_length = sizeof(struct ip) +
370 sizeof(struct etherip_header) +
371 sizeof(struct ether_header),
372 .exact_match = ENCAP_DRV_LOOKUP,
373 .lookup = in_gif_lookup,
374 .input = in_gif_input
384 if (!IS_DEFAULT_VNET(curvnet))
386 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
387 ipv4_encap_cfg[i].cookie = ip_encap_attach(
388 &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
396 if (IS_DEFAULT_VNET(curvnet)) {
397 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
398 ip_encap_detach(ipv4_encap_cfg[i].cookie);
400 if (V_ipv4_hashtbl != NULL)
401 gif_hashdestroy(V_ipv4_hashtbl);