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: in6_gif.c,v 1.49 2001/05/14 14:02:17 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/syslog.h> 50 #include <sys/sysctl.h> 51 #include <sys/malloc.h> 52 #include <sys/proc.h> 53 54 #include <net/ethernet.h> 55 #include <net/if.h> 56 #include <net/if_var.h> 57 #include <net/route.h> 58 #include <net/vnet.h> 59 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #ifdef INET 63 #include <netinet/ip.h> 64 #include <netinet/ip_ecn.h> 65 #endif 66 #include <netinet/ip_encap.h> 67 #include <netinet/ip6.h> 68 #include <netinet6/ip6_var.h> 69 #include <netinet6/in6_var.h> 70 #include <netinet6/scope6_var.h> 71 #include <netinet6/ip6_ecn.h> 72 #include <netinet6/in6_fib.h> 73 74 #include <net/if_gif.h> 75 76 #define GIF_HLIM 30 77 VNET_DEFINE_STATIC(int, ip6_gif_hlim) = GIF_HLIM; 78 #define V_ip6_gif_hlim VNET(ip6_gif_hlim) 79 80 SYSCTL_DECL(_net_inet6_ip6); 81 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim, 82 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0, 83 "Default hop limit for encapsulated packets"); 84 85 /* 86 * We keep interfaces in a hash table using src+dst as key. 87 * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list. 88 */ 89 VNET_DEFINE_STATIC(struct gif_list *, ipv6_hashtbl) = NULL; 90 VNET_DEFINE_STATIC(struct gif_list, ipv6_list) = CK_LIST_HEAD_INITIALIZER(); 91 #define V_ipv6_hashtbl VNET(ipv6_hashtbl) 92 #define V_ipv6_list VNET(ipv6_list) 93 94 #define GIF_HASH(src, dst) (V_ipv6_hashtbl[\ 95 in6_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)]) 96 #define GIF_HASH_SC(sc) GIF_HASH(&(sc)->gif_ip6hdr->ip6_src,\ 97 &(sc)->gif_ip6hdr->ip6_dst) 98 static uint32_t 99 in6_gif_hashval(const struct in6_addr *src, const struct in6_addr *dst) 100 { 101 uint32_t ret; 102 103 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT); 104 return (fnv_32_buf(dst, sizeof(*dst), ret)); 105 } 106 107 static int 108 in6_gif_checkdup(const struct gif_softc *sc, const struct in6_addr *src, 109 const struct in6_addr *dst) 110 { 111 struct gif_softc *tmp; 112 113 if (sc->gif_family == AF_INET6 && 114 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, src) && 115 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, dst)) 116 return (EEXIST); 117 118 CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) { 119 if (tmp == sc) 120 continue; 121 if (IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_src, src) && 122 IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_dst, dst)) 123 return (EADDRNOTAVAIL); 124 } 125 return (0); 126 } 127 128 static void 129 in6_gif_attach(struct gif_softc *sc) 130 { 131 132 if (sc->gif_options & GIF_IGNORE_SOURCE) 133 CK_LIST_INSERT_HEAD(&V_ipv6_list, sc, chain); 134 else 135 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain); 136 } 137 138 int 139 in6_gif_setopts(struct gif_softc *sc, u_int options) 140 { 141 142 /* NOTE: we are protected with gif_ioctl_sx lock */ 143 MPASS(sc->gif_family == AF_INET6); 144 MPASS(sc->gif_options != options); 145 146 if ((options & GIF_IGNORE_SOURCE) != 147 (sc->gif_options & GIF_IGNORE_SOURCE)) { 148 CK_LIST_REMOVE(sc, chain); 149 sc->gif_options = options; 150 in6_gif_attach(sc); 151 } 152 return (0); 153 } 154 155 int 156 in6_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data) 157 { 158 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 159 struct sockaddr_in6 *dst, *src; 160 struct ip6_hdr *ip6; 161 int error; 162 163 /* NOTE: we are protected with gif_ioctl_sx lock */ 164 error = EINVAL; 165 switch (cmd) { 166 case SIOCSIFPHYADDR_IN6: 167 src = &((struct in6_aliasreq *)data)->ifra_addr; 168 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr; 169 170 /* sanity checks */ 171 if (src->sin6_family != dst->sin6_family || 172 src->sin6_family != AF_INET6 || 173 src->sin6_len != dst->sin6_len || 174 src->sin6_len != sizeof(*src)) 175 break; 176 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) || 177 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) { 178 error = EADDRNOTAVAIL; 179 break; 180 } 181 /* 182 * Check validity of the scope zone ID of the 183 * addresses, and convert it into the kernel 184 * internal form if necessary. 185 */ 186 if ((error = sa6_embedscope(src, 0)) != 0 || 187 (error = sa6_embedscope(dst, 0)) != 0) 188 break; 189 190 if (V_ipv6_hashtbl == NULL) 191 V_ipv6_hashtbl = gif_hashinit(); 192 error = in6_gif_checkdup(sc, &src->sin6_addr, 193 &dst->sin6_addr); 194 if (error == EADDRNOTAVAIL) 195 break; 196 if (error == EEXIST) { 197 /* Addresses are the same. Just return. */ 198 error = 0; 199 break; 200 } 201 ip6 = malloc(sizeof(*ip6), M_GIF, M_WAITOK | M_ZERO); 202 ip6->ip6_src = src->sin6_addr; 203 ip6->ip6_dst = dst->sin6_addr; 204 ip6->ip6_vfc = IPV6_VERSION; 205 if (sc->gif_family != 0) { 206 /* Detach existing tunnel first */ 207 CK_LIST_REMOVE(sc, chain); 208 GIF_WAIT(); 209 free(sc->gif_hdr, M_GIF); 210 /* XXX: should we notify about link state change? */ 211 } 212 sc->gif_family = AF_INET6; 213 sc->gif_ip6hdr = ip6; 214 in6_gif_attach(sc); 215 break; 216 case SIOCGIFPSRCADDR_IN6: 217 case SIOCGIFPDSTADDR_IN6: 218 if (sc->gif_family != AF_INET6) { 219 error = EADDRNOTAVAIL; 220 break; 221 } 222 src = (struct sockaddr_in6 *)&ifr->ifr_addr; 223 memset(src, 0, sizeof(*src)); 224 src->sin6_family = AF_INET6; 225 src->sin6_len = sizeof(*src); 226 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ? 227 sc->gif_ip6hdr->ip6_src: sc->gif_ip6hdr->ip6_dst; 228 error = prison_if(curthread->td_ucred, (struct sockaddr *)src); 229 if (error == 0) 230 error = sa6_recoverscope(src); 231 if (error != 0) 232 memset(src, 0, sizeof(*src)); 233 break; 234 } 235 return (error); 236 } 237 238 int 239 in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn) 240 { 241 struct gif_softc *sc = ifp->if_softc; 242 struct ip6_hdr *ip6; 243 int len; 244 245 /* prepend new IP header */ 246 MPASS(in_epoch(net_epoch_preempt)); 247 len = sizeof(struct ip6_hdr); 248 #ifndef __NO_STRICT_ALIGNMENT 249 if (proto == IPPROTO_ETHERIP) 250 len += ETHERIP_ALIGN; 251 #endif 252 M_PREPEND(m, len, M_NOWAIT); 253 if (m == NULL) 254 return (ENOBUFS); 255 #ifndef __NO_STRICT_ALIGNMENT 256 if (proto == IPPROTO_ETHERIP) { 257 len = mtod(m, vm_offset_t) & 3; 258 KASSERT(len == 0 || len == ETHERIP_ALIGN, 259 ("in6_gif_output: unexpected misalignment")); 260 m->m_data += len; 261 m->m_len -= ETHERIP_ALIGN; 262 } 263 #endif 264 265 ip6 = mtod(m, struct ip6_hdr *); 266 MPASS(sc->gif_family == AF_INET6); 267 bcopy(sc->gif_ip6hdr, ip6, sizeof(struct ip6_hdr)); 268 269 ip6->ip6_flow |= htonl((uint32_t)ecn << 20); 270 ip6->ip6_nxt = proto; 271 ip6->ip6_hlim = V_ip6_gif_hlim; 272 /* 273 * force fragmentation to minimum MTU, to avoid path MTU discovery. 274 * it is too painful to ask for resend of inner packet, to achieve 275 * path MTU discovery for encapsulated packets. 276 */ 277 return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL)); 278 } 279 280 static int 281 in6_gif_input(struct mbuf *m, int off, int proto, void *arg) 282 { 283 struct gif_softc *sc = arg; 284 struct ifnet *gifp; 285 struct ip6_hdr *ip6; 286 uint8_t ecn; 287 288 MPASS(in_epoch(net_epoch_preempt)); 289 if (sc == NULL) { 290 m_freem(m); 291 IP6STAT_INC(ip6s_nogif); 292 return (IPPROTO_DONE); 293 } 294 gifp = GIF2IFP(sc); 295 if ((gifp->if_flags & IFF_UP) != 0) { 296 ip6 = mtod(m, struct ip6_hdr *); 297 ecn = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 298 m_adj(m, off); 299 gif_input(m, gifp, proto, ecn); 300 } else { 301 m_freem(m); 302 IP6STAT_INC(ip6s_nogif); 303 } 304 return (IPPROTO_DONE); 305 } 306 307 static int 308 in6_gif_lookup(const struct mbuf *m, int off, int proto, void **arg) 309 { 310 const struct ip6_hdr *ip6; 311 struct gif_softc *sc; 312 int ret; 313 314 if (V_ipv6_hashtbl == NULL) 315 return (0); 316 317 MPASS(in_epoch(net_epoch_preempt)); 318 /* 319 * NOTE: it is safe to iterate without any locking here, because softc 320 * can be reclaimed only when we are not within net_epoch_preempt 321 * section, but ip_encap lookup+input are executed in epoch section. 322 */ 323 ip6 = mtod(m, const struct ip6_hdr *); 324 ret = 0; 325 CK_LIST_FOREACH(sc, &GIF_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) { 326 /* 327 * This is an inbound packet, its ip6_dst is source address 328 * in softc. 329 */ 330 if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, 331 &ip6->ip6_dst) && 332 IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, 333 &ip6->ip6_src)) { 334 ret = ENCAP_DRV_LOOKUP; 335 goto done; 336 } 337 } 338 /* 339 * No exact match. 340 * Check the list of interfaces with GIF_IGNORE_SOURCE flag. 341 */ 342 CK_LIST_FOREACH(sc, &V_ipv6_list, chain) { 343 if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, 344 &ip6->ip6_dst)) { 345 ret = 128 + 8; /* src + proto */ 346 goto done; 347 } 348 } 349 return (0); 350 done: 351 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 352 return (0); 353 /* ingress filters on outer source */ 354 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) { 355 struct nhop6_basic nh6; 356 357 if (fib6_lookup_nh_basic(sc->gif_fibnum, &ip6->ip6_src, 358 ntohs(in6_getscope(&ip6->ip6_src)), 0, 0, &nh6) != 0) 359 return (0); 360 361 if (nh6.nh_ifp != m->m_pkthdr.rcvif) 362 return (0); 363 } 364 *arg = sc; 365 return (ret); 366 } 367 368 static struct { 369 const struct encap_config encap; 370 const struct encaptab *cookie; 371 } ipv6_encap_cfg[] = { 372 #ifdef INET 373 { 374 .encap = { 375 .proto = IPPROTO_IPV4, 376 .min_length = sizeof(struct ip6_hdr) + 377 sizeof(struct ip), 378 .exact_match = ENCAP_DRV_LOOKUP, 379 .lookup = in6_gif_lookup, 380 .input = in6_gif_input 381 }, 382 }, 383 #endif 384 { 385 .encap = { 386 .proto = IPPROTO_IPV6, 387 .min_length = 2 * sizeof(struct ip6_hdr), 388 .exact_match = ENCAP_DRV_LOOKUP, 389 .lookup = in6_gif_lookup, 390 .input = in6_gif_input 391 }, 392 }, 393 { 394 .encap = { 395 .proto = IPPROTO_ETHERIP, 396 .min_length = sizeof(struct ip6_hdr) + 397 sizeof(struct etherip_header) + 398 sizeof(struct ether_header), 399 .exact_match = ENCAP_DRV_LOOKUP, 400 .lookup = in6_gif_lookup, 401 .input = in6_gif_input 402 }, 403 } 404 }; 405 406 void 407 in6_gif_init(void) 408 { 409 int i; 410 411 if (!IS_DEFAULT_VNET(curvnet)) 412 return; 413 for (i = 0; i < nitems(ipv6_encap_cfg); i++) 414 ipv6_encap_cfg[i].cookie = ip6_encap_attach( 415 &ipv6_encap_cfg[i].encap, NULL, M_WAITOK); 416 } 417 418 void 419 in6_gif_uninit(void) 420 { 421 int i; 422 423 if (IS_DEFAULT_VNET(curvnet)) { 424 for (i = 0; i < nitems(ipv6_encap_cfg); i++) 425 ip6_encap_detach(ipv6_encap_cfg[i].cookie); 426 } 427 if (V_ipv6_hashtbl != NULL) 428 gif_hashdestroy(V_ipv6_hashtbl); 429 } 430