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 __FBSDID("$FreeBSD$"); 37 38 #include "opt_inet.h" 39 #include "opt_inet6.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/jail.h> 44 #include <sys/socket.h> 45 #include <sys/sockio.h> 46 #include <sys/mbuf.h> 47 #include <sys/errno.h> 48 #include <sys/kernel.h> 49 #include <sys/sysctl.h> 50 #include <sys/malloc.h> 51 52 #include <net/ethernet.h> 53 #include <net/if.h> 54 #include <net/if_var.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 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"); 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 */ 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) 87 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) 92 static uint32_t 93 in_gif_hashval(in_addr_t src, in_addr_t dst) 94 { 95 uint32_t ret; 96 97 ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT); 98 return (fnv_32_buf(&dst, sizeof(dst), ret)); 99 } 100 101 static int 102 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst) 103 { 104 struct gif_softc *tmp; 105 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) 109 return (EEXIST); 110 111 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) { 112 if (tmp == sc) 113 continue; 114 if (tmp->gif_iphdr->ip_src.s_addr == src && 115 tmp->gif_iphdr->ip_dst.s_addr == dst) 116 return (EADDRNOTAVAIL); 117 } 118 return (0); 119 } 120 121 static void 122 in_gif_attach(struct gif_softc *sc) 123 { 124 125 if (sc->gif_options & GIF_IGNORE_SOURCE) 126 CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain); 127 else 128 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain); 129 } 130 131 int 132 in_gif_setopts(struct gif_softc *sc, u_int options) 133 { 134 135 /* NOTE: we are protected with gif_ioctl_sx lock */ 136 MPASS(sc->gif_family == AF_INET); 137 MPASS(sc->gif_options != options); 138 139 if ((options & GIF_IGNORE_SOURCE) != 140 (sc->gif_options & GIF_IGNORE_SOURCE)) { 141 CK_LIST_REMOVE(sc, chain); 142 sc->gif_options = options; 143 in_gif_attach(sc); 144 } 145 return (0); 146 } 147 148 int 149 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data) 150 { 151 struct ifreq *ifr = (struct ifreq *)data; 152 struct sockaddr_in *dst, *src; 153 struct ip *ip; 154 int error; 155 156 /* NOTE: we are protected with gif_ioctl_sx lock */ 157 error = EINVAL; 158 switch (cmd) { 159 case SIOCSIFPHYADDR: 160 src = &((struct in_aliasreq *)data)->ifra_addr; 161 dst = &((struct in_aliasreq *)data)->ifra_dstaddr; 162 163 /* sanity checks */ 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)) 168 break; 169 if (src->sin_addr.s_addr == INADDR_ANY || 170 dst->sin_addr.s_addr == INADDR_ANY) { 171 error = EADDRNOTAVAIL; 172 break; 173 } 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) 179 break; 180 if (error == EEXIST) { 181 /* Addresses are the same. Just return. */ 182 error = 0; 183 break; 184 } 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); 191 GIF_WAIT(); 192 free(sc->gif_hdr, M_GIF); 193 /* XXX: should we notify about link state change? */ 194 } 195 sc->gif_family = AF_INET; 196 sc->gif_iphdr = ip; 197 in_gif_attach(sc); 198 break; 199 case SIOCGIFPSRCADDR: 200 case SIOCGIFPDSTADDR: 201 if (sc->gif_family != AF_INET) { 202 error = EADDRNOTAVAIL; 203 break; 204 } 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); 212 if (error != 0) 213 memset(src, 0, sizeof(*src)); 214 break; 215 } 216 return (error); 217 } 218 219 int 220 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn) 221 { 222 struct gif_softc *sc = ifp->if_softc; 223 struct ip *ip; 224 int len; 225 226 /* prepend new IP header */ 227 MPASS(in_epoch()); 228 len = sizeof(struct ip); 229 #ifndef __NO_STRICT_ALIGNMENT 230 if (proto == IPPROTO_ETHERIP) 231 len += ETHERIP_ALIGN; 232 #endif 233 M_PREPEND(m, len, M_NOWAIT); 234 if (m == NULL) 235 return (ENOBUFS); 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")); 241 m->m_data += len; 242 m->m_len -= ETHERIP_ALIGN; 243 } 244 #endif 245 ip = mtod(m, struct ip *); 246 247 MPASS(sc->gif_family == AF_INET); 248 bcopy(sc->gif_iphdr, ip, sizeof(struct ip)); 249 ip->ip_p = proto; 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); 253 ip->ip_tos = ecn; 254 255 return (ip_output(m, NULL, NULL, 0, NULL, NULL)); 256 } 257 258 static int 259 in_gif_input(struct mbuf *m, int off, int proto, void *arg) 260 { 261 struct gif_softc *sc = arg; 262 struct ifnet *gifp; 263 struct ip *ip; 264 uint8_t ecn; 265 266 MPASS(in_epoch()); 267 if (sc == NULL) { 268 m_freem(m); 269 KMOD_IPSTAT_INC(ips_nogif); 270 return (IPPROTO_DONE); 271 } 272 gifp = GIF2IFP(sc); 273 if ((gifp->if_flags & IFF_UP) != 0) { 274 ip = mtod(m, struct ip *); 275 ecn = ip->ip_tos; 276 m_adj(m, off); 277 gif_input(m, gifp, proto, ecn); 278 } else { 279 m_freem(m); 280 KMOD_IPSTAT_INC(ips_nogif); 281 } 282 return (IPPROTO_DONE); 283 } 284 285 static int 286 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg) 287 { 288 const struct ip *ip; 289 struct gif_softc *sc; 290 int ret; 291 292 if (V_ipv4_hashtbl == NULL) 293 return (0); 294 295 MPASS(in_epoch()); 296 ip = mtod(m, const struct ip *); 297 /* 298 * NOTE: it is safe to iterate without any locking here, because softc 299 * can be reclaimed only when we are not within net_epoch_preempt 300 * section, but ip_encap lookup+input are executed in epoch section. 301 */ 302 ret = 0; 303 CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr, 304 ip->ip_src.s_addr), chain) { 305 /* 306 * This is an inbound packet, its ip_dst is source address 307 * in softc. 308 */ 309 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr && 310 sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) { 311 ret = ENCAP_DRV_LOOKUP; 312 goto done; 313 } 314 } 315 /* 316 * No exact match. 317 * Check the list of interfaces with GIF_IGNORE_SOURCE flag. 318 */ 319 CK_LIST_FOREACH(sc, &V_ipv4_list, chain) { 320 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) { 321 ret = 32 + 8; /* src + proto */ 322 goto done; 323 } 324 } 325 return (0); 326 done: 327 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 328 return (0); 329 /* ingress filters on outer source */ 330 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) { 331 struct nhop4_basic nh4; 332 struct in_addr dst; 333 334 dst = ip->ip_src; 335 if (fib4_lookup_nh_basic(sc->gif_fibnum, dst, 0, 0, &nh4) != 0) 336 return (0); 337 if (nh4.nh_ifp != m->m_pkthdr.rcvif) 338 return (0); 339 } 340 *arg = sc; 341 return (ret); 342 } 343 344 static struct { 345 const struct encap_config encap; 346 const struct encaptab *cookie; 347 } ipv4_encap_cfg[] = { 348 { 349 .encap = { 350 .proto = IPPROTO_IPV4, 351 .min_length = 2 * sizeof(struct ip), 352 .exact_match = ENCAP_DRV_LOOKUP, 353 .lookup = in_gif_lookup, 354 .input = in_gif_input 355 }, 356 }, 357 #ifdef INET6 358 { 359 .encap = { 360 .proto = IPPROTO_IPV6, 361 .min_length = sizeof(struct ip) + 362 sizeof(struct ip6_hdr), 363 .exact_match = ENCAP_DRV_LOOKUP, 364 .lookup = in_gif_lookup, 365 .input = in_gif_input 366 }, 367 }, 368 #endif 369 { 370 .encap = { 371 .proto = IPPROTO_ETHERIP, 372 .min_length = sizeof(struct ip) + 373 sizeof(struct etherip_header) + 374 sizeof(struct ether_header), 375 .exact_match = ENCAP_DRV_LOOKUP, 376 .lookup = in_gif_lookup, 377 .input = in_gif_input 378 }, 379 } 380 }; 381 382 void 383 in_gif_init(void) 384 { 385 int i; 386 387 if (!IS_DEFAULT_VNET(curvnet)) 388 return; 389 for (i = 0; i < nitems(ipv4_encap_cfg); i++) 390 ipv4_encap_cfg[i].cookie = ip_encap_attach( 391 &ipv4_encap_cfg[i].encap, NULL, M_WAITOK); 392 } 393 394 void 395 in_gif_uninit(void) 396 { 397 int i; 398 399 if (IS_DEFAULT_VNET(curvnet)) { 400 for (i = 0; i < nitems(ipv4_encap_cfg); i++) 401 ip_encap_detach(ipv4_encap_cfg[i].cookie); 402 } 403 if (V_ipv4_hashtbl != NULL) 404 gif_hashdestroy(V_ipv4_hashtbl); 405 } 406 407