1686cdd19SJun-ichiro itojun Hagino /* $FreeBSD$ */ 288ff5695SSUZUKI Shinsuke /* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */ 3686cdd19SJun-ichiro itojun Hagino 4c398230bSWarner Losh /*- 551369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 651369649SPedro F. Giffuni * 7686cdd19SJun-ichiro itojun Hagino * Copyright (C) 2000 WIDE Project. 819dc6445SKristof Provost * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org> 919dc6445SKristof Provost * Copyright (c) 2013 Ermal Luci <eri@FreeBSD.org> 1019dc6445SKristof Provost * Copyright (c) 2017-2021 Rubicon Communications, LLC (Netgate) 11686cdd19SJun-ichiro itojun Hagino * All rights reserved. 12686cdd19SJun-ichiro itojun Hagino * 13686cdd19SJun-ichiro itojun Hagino * Redistribution and use in source and binary forms, with or without 14686cdd19SJun-ichiro itojun Hagino * modification, are permitted provided that the following conditions 15686cdd19SJun-ichiro itojun Hagino * are met: 16686cdd19SJun-ichiro itojun Hagino * 1. Redistributions of source code must retain the above copyright 17686cdd19SJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer. 18686cdd19SJun-ichiro itojun Hagino * 2. Redistributions in binary form must reproduce the above copyright 19686cdd19SJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer in the 20686cdd19SJun-ichiro itojun Hagino * documentation and/or other materials provided with the distribution. 21686cdd19SJun-ichiro itojun Hagino * 3. Neither the name of the project nor the names of its contributors 22686cdd19SJun-ichiro itojun Hagino * may be used to endorse or promote products derived from this software 23686cdd19SJun-ichiro itojun Hagino * without specific prior written permission. 24686cdd19SJun-ichiro itojun Hagino * 25686cdd19SJun-ichiro itojun Hagino * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 26686cdd19SJun-ichiro itojun Hagino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27686cdd19SJun-ichiro itojun Hagino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28686cdd19SJun-ichiro itojun Hagino * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 29686cdd19SJun-ichiro itojun Hagino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30686cdd19SJun-ichiro itojun Hagino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31686cdd19SJun-ichiro itojun Hagino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32686cdd19SJun-ichiro itojun Hagino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33686cdd19SJun-ichiro itojun Hagino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34686cdd19SJun-ichiro itojun Hagino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35686cdd19SJun-ichiro itojun Hagino * SUCH DAMAGE. 36686cdd19SJun-ichiro itojun Hagino */ 37686cdd19SJun-ichiro itojun Hagino 38686cdd19SJun-ichiro itojun Hagino /* 3933841545SHajimu UMEMOTO * 6to4 interface, based on RFC3056. 40686cdd19SJun-ichiro itojun Hagino * 41686cdd19SJun-ichiro itojun Hagino * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting. 42686cdd19SJun-ichiro itojun Hagino * There is no address mapping defined from IPv6 multicast address to IPv4 43686cdd19SJun-ichiro itojun Hagino * address. Therefore, we do not have IFF_MULTICAST on the interface. 44686cdd19SJun-ichiro itojun Hagino * 45686cdd19SJun-ichiro itojun Hagino * Due to the lack of address mapping for link-local addresses, we cannot 46686cdd19SJun-ichiro itojun Hagino * throw packets toward link-local addresses (fe80::x). Also, we cannot throw 47686cdd19SJun-ichiro itojun Hagino * packets to link-local multicast addresses (ff02::x). 48686cdd19SJun-ichiro itojun Hagino * 49686cdd19SJun-ichiro itojun Hagino * Here are interesting symptoms due to the lack of link-local address: 50686cdd19SJun-ichiro itojun Hagino * 51686cdd19SJun-ichiro itojun Hagino * Unicast routing exchange: 52686cdd19SJun-ichiro itojun Hagino * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9, 53686cdd19SJun-ichiro itojun Hagino * and link-local addresses as nexthop. 54686cdd19SJun-ichiro itojun Hagino * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address 55686cdd19SJun-ichiro itojun Hagino * assigned to the link, and makes use of them. Also, HELLO packets use 56686cdd19SJun-ichiro itojun Hagino * link-local multicast addresses (ff02::5 and ff02::6). 57686cdd19SJun-ichiro itojun Hagino * - BGP4+: Maybe. You can only use global address as nexthop, and global 58686cdd19SJun-ichiro itojun Hagino * address as TCP endpoint address. 59686cdd19SJun-ichiro itojun Hagino * 60686cdd19SJun-ichiro itojun Hagino * Multicast routing protocols: 61686cdd19SJun-ichiro itojun Hagino * - PIM: Hello packet cannot be used to discover adjacent PIM routers. 62686cdd19SJun-ichiro itojun Hagino * Adjacent PIM routers must be configured manually (is it really spec-wise 63686cdd19SJun-ichiro itojun Hagino * correct thing to do?). 64686cdd19SJun-ichiro itojun Hagino * 65686cdd19SJun-ichiro itojun Hagino * ICMPv6: 66686cdd19SJun-ichiro itojun Hagino * - Redirects cannot be used due to the lack of link-local address. 67686cdd19SJun-ichiro itojun Hagino * 6833841545SHajimu UMEMOTO * stf interface does not have, and will not need, a link-local address. 6933841545SHajimu UMEMOTO * It seems to have no real benefit and does not help the above symptoms much. 7033841545SHajimu UMEMOTO * Even if we assign link-locals to interface, we cannot really 7133841545SHajimu UMEMOTO * use link-local unicast/multicast on top of 6to4 cloud (since there's no 7233841545SHajimu UMEMOTO * encapsulation defined for link-local address), and the above analysis does 7333841545SHajimu UMEMOTO * not change. RFC3056 does not mandate the assignment of link-local address 7433841545SHajimu UMEMOTO * either. 75686cdd19SJun-ichiro itojun Hagino * 76686cdd19SJun-ichiro itojun Hagino * 6to4 interface has security issues. Refer to 77686cdd19SJun-ichiro itojun Hagino * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt 78686cdd19SJun-ichiro itojun Hagino * for details. The code tries to filter out some of malicious packets. 79686cdd19SJun-ichiro itojun Hagino * Note that there is no way to be 100% secure. 80686cdd19SJun-ichiro itojun Hagino */ 81686cdd19SJun-ichiro itojun Hagino 82686cdd19SJun-ichiro itojun Hagino #include <sys/param.h> 83686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h> 84686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h> 85686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h> 86686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h> 8719dc6445SKristof Provost #include <sys/endian.h> 88686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h> 89686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h> 90cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 915dba30f1SPoul-Henning Kamp #include <sys/module.h> 9219dc6445SKristof Provost #include <sys/priv.h> 938b07e49aSJulian Elischer #include <sys/proc.h> 94abb64706SBrooks Davis #include <sys/queue.h> 95b46512f7SKristof Provost #include <sys/sdt.h> 960dae32f2SDavid Malone #include <sys/sysctl.h> 97686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h> 98686cdd19SJun-ichiro itojun Hagino 99686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h> 100686cdd19SJun-ichiro itojun Hagino 101686cdd19SJun-ichiro itojun Hagino #include <net/if.h> 10276039bc8SGleb Smirnoff #include <net/if_var.h> 103*2c2b37adSJustin Hibbits #include <net/if_private.h> 104f889d2efSBrooks Davis #include <net/if_clone.h> 105686cdd19SJun-ichiro itojun Hagino #include <net/route.h> 1066ad7446cSAlexander V. Chernikov #include <net/route/nhop.h> 107686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h> 10819dc6445SKristof Provost #include <net/if_stf.h> 109686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h> 110530c0060SRobert Watson #include <net/vnet.h> 111686cdd19SJun-ichiro itojun Hagino 112686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h> 1130792bcbbSAlexander V. Chernikov #include <netinet/in_fib.h> 114686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h> 115686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h> 116686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h> 117686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h> 118686cdd19SJun-ichiro itojun Hagino 119686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 12019dc6445SKristof Provost #include <netinet6/in6_fib.h> 121686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h> 122686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h> 123686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h> 124686cdd19SJun-ichiro itojun Hagino 125686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 126686cdd19SJun-ichiro itojun Hagino 127686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h> 128686cdd19SJun-ichiro itojun Hagino 129686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h> 130686cdd19SJun-ichiro itojun Hagino 131aed55708SRobert Watson #include <security/mac/mac_framework.h> 132aed55708SRobert Watson 133b46512f7SKristof Provost SDT_PROVIDER_DEFINE(if_stf); 134b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , encapcheck, in, "struct mbuf *", "int", "int"); 135b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , encapcheck, accept); 136b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , getsrcifa6, in, "struct ifnet *", 137b46512f7SKristof Provost "struct in6_addr *", "struct in6_addr *"); 138b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getsrcifa6, found, "struct in6_addr *", 139b46512f7SKristof Provost "struct in6_addr *"); 140b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , getsrcifa6, notfound); 141b46512f7SKristof Provost 142b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , stf_output, in, "struct ifnet *", "struct mbuf *", 143b46512f7SKristof Provost "struct sockaddr *", "struct route *"); 144b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_output, error, "int", "int"); 145b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , stf_output, out, "int"); 146b46512f7SKristof Provost 147b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , checkaddr6, in, "struct stf_softc *", 148b46512f7SKristof Provost "struct in6_addr *", "struct ifnet *"); 149b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , checkaddr6, out, "int", "int"); 150b46512f7SKristof Provost 151b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , stf_input, in, "struct mbuf *", "int", "int"); 152b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_input, out, "int", "int"); 153b46512f7SKristof Provost 154b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , ioctl, sv4net, "struct in_addr *", 155b46512f7SKristof Provost "struct in_addr *", "int"); 156b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, sdstv4, "struct in_addr *"); 157b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, ifaddr, "struct ifaddr *"); 158b46512f7SKristof Provost 159b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , getin4addr_in6, out, "struct in6_addr *", 160b46512f7SKristof Provost "struct in6_addr *", "struct in6_addr *", "struct sockaddr_in *"); 161b46512f7SKristof Provost 162b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getin4addr, in, "struct in6_addr *", "struct in6_addr *"); 163b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , getin4addr, out, "struct sockaddr_in *"); 164b46512f7SKristof Provost 1650dae32f2SDavid Malone SYSCTL_DECL(_net_link); 1667029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1677029da5cSPawel Biernacki "6to4 Interface"); 1680dae32f2SDavid Malone 16951743c5fSAndrey V. Elsukov static int stf_permit_rfc1918 = 0; 170af3b2549SHans Petter Selasky SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN, 17151743c5fSAndrey V. Elsukov &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses"); 17251743c5fSAndrey V. Elsukov 173f889d2efSBrooks Davis #define STFUNIT 0 174abb64706SBrooks Davis 175686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 1768d95b0ceSSUZUKI Shinsuke 1778d95b0ceSSUZUKI Shinsuke /* 1788d95b0ceSSUZUKI Shinsuke * XXX: Return a pointer with 16-bit aligned. Don't cast it to 1798d95b0ceSSUZUKI Shinsuke * struct in_addr *; use bcopy() instead. 1808d95b0ceSSUZUKI Shinsuke */ 18147e8d432SGleb Smirnoff #define GET_V4(x) (&(x)->s6_addr16[1]) 182686cdd19SJun-ichiro itojun Hagino 183686cdd19SJun-ichiro itojun Hagino struct stf_softc { 184fc74a9f9SBrooks Davis struct ifnet *sc_ifp; 18519dc6445SKristof Provost in_addr_t braddr; /* Border relay IPv4 address */ 18619dc6445SKristof Provost in_addr_t srcv4_addr; /* Our IPv4 WAN address */ 18719dc6445SKristof Provost u_int v4prefixlen; /* How much of the v4 address to include in our address. */ 1888b07e49aSJulian Elischer u_int sc_fibnum; 189686cdd19SJun-ichiro itojun Hagino const struct encaptab *encap_cookie; 190686cdd19SJun-ichiro itojun Hagino }; 191fc74a9f9SBrooks Davis #define STF2IFP(sc) ((sc)->sc_ifp) 192686cdd19SJun-ichiro itojun Hagino 19342a58907SGleb Smirnoff static const char stfname[] = "stf"; 19442a58907SGleb Smirnoff 19542a58907SGleb Smirnoff static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface"); 196591cf7ceSRobert Watson static const int ip_stf_ttl = 40; 197686cdd19SJun-ichiro itojun Hagino 1986d8fdfa9SAndrey V. Elsukov static int in_stf_input(struct mbuf *, int, int, void *); 199f889d2efSBrooks Davis static char *stfnames[] = {"stf0", "stf", "6to4", NULL}; 200f889d2efSBrooks Davis 201929ddbbbSAlfred Perlstein static int stfmodevent(module_t, int, void *); 202929ddbbbSAlfred Perlstein static int stf_encapcheck(const struct mbuf *, int, int, void *); 203d74b9a2cSAlexander V. Chernikov static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *); 20447e8d432SGleb Smirnoff static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *, 205279aa3d4SKip Macy struct route *); 206ce9d7b2fSHajimu UMEMOTO static int isrfc1918addr(struct in_addr *); 207929ddbbbSAlfred Perlstein static int stf_checkaddr4(struct stf_softc *, struct in_addr *, 208929ddbbbSAlfred Perlstein struct ifnet *); 209929ddbbbSAlfred Perlstein static int stf_checkaddr6(struct stf_softc *, struct in6_addr *, 210929ddbbbSAlfred Perlstein struct ifnet *); 21119dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *, 21219dc6445SKristof Provost struct sockaddr_in *, struct in6_addr, struct in6_addr, 21319dc6445SKristof Provost struct in6_addr); 21419dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr(struct stf_softc *, 21519dc6445SKristof Provost struct sockaddr_in *, struct in6_addr, struct in6_addr); 216929ddbbbSAlfred Perlstein static int stf_ioctl(struct ifnet *, u_long, caddr_t); 217686cdd19SJun-ichiro itojun Hagino 2188e45fed3SKristof Provost VNET_DEFINE_STATIC(struct if_clone *, stf_cloner); 2198e45fed3SKristof Provost #define V_stf_cloner VNET(stf_cloner) 220abb64706SBrooks Davis 2216d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = { 2226d8fdfa9SAndrey V. Elsukov .proto = IPPROTO_IPV6, 2236d8fdfa9SAndrey V. Elsukov .min_length = sizeof(struct ip), 2246d8fdfa9SAndrey V. Elsukov .exact_match = (sizeof(in_addr_t) << 3) + 8, 2256d8fdfa9SAndrey V. Elsukov .check = stf_encapcheck, 2266d8fdfa9SAndrey V. Elsukov .input = in_stf_input 2276d8fdfa9SAndrey V. Elsukov }; 2286d8fdfa9SAndrey V. Elsukov 229bb2bfb4fSBrooks Davis static int 230f889d2efSBrooks Davis stf_clone_match(struct if_clone *ifc, const char *name) 231686cdd19SJun-ichiro itojun Hagino { 232f889d2efSBrooks Davis int i; 233f889d2efSBrooks Davis 234f889d2efSBrooks Davis for(i = 0; stfnames[i] != NULL; i++) { 235f889d2efSBrooks Davis if (strcmp(stfnames[i], name) == 0) 236f889d2efSBrooks Davis return (1); 237f889d2efSBrooks Davis } 238f889d2efSBrooks Davis 239f889d2efSBrooks Davis return (0); 240f889d2efSBrooks Davis } 241f889d2efSBrooks Davis 242f889d2efSBrooks Davis static int 24391ebcbe0SAlexander V. Chernikov stf_clone_create(struct if_clone *ifc, char *name, size_t len, 24491ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 245f889d2efSBrooks Davis { 246d177868cSLuiz Otavio O Souza char *dp; 247d177868cSLuiz Otavio O Souza int err, unit, wildcard; 248686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 2491861b710SBrooks Davis struct ifnet *ifp; 250686cdd19SJun-ichiro itojun Hagino 251d177868cSLuiz Otavio O Souza err = ifc_name2unit(name, &unit); 252d177868cSLuiz Otavio O Souza if (err != 0) 253d177868cSLuiz Otavio O Souza return (err); 254d177868cSLuiz Otavio O Souza wildcard = (unit < 0); 255d177868cSLuiz Otavio O Souza 256f889d2efSBrooks Davis /* 257f889d2efSBrooks Davis * We can only have one unit, but since unit allocation is 258f889d2efSBrooks Davis * already locked, we use it to keep from allocating extra 259f889d2efSBrooks Davis * interfaces. 260f889d2efSBrooks Davis */ 261f889d2efSBrooks Davis unit = STFUNIT; 262f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit); 263f889d2efSBrooks Davis if (err != 0) 264f889d2efSBrooks Davis return (err); 265f889d2efSBrooks Davis 266a163d034SWarner Losh sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO); 267b03965ddSBrooks Davis ifp = STF2IFP(sc) = if_alloc(IFT_STF); 268fc74a9f9SBrooks Davis if (ifp == NULL) { 269fc74a9f9SBrooks Davis free(sc, M_STF); 270fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 271fc74a9f9SBrooks Davis return (ENOSPC); 272fc74a9f9SBrooks Davis } 273b03965ddSBrooks Davis ifp->if_softc = sc; 2748b07e49aSJulian Elischer sc->sc_fibnum = curthread->td_proc->p_fibnum; 275b03965ddSBrooks Davis 276f889d2efSBrooks Davis /* 277f889d2efSBrooks Davis * Set the name manually rather then using if_initname because 278f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 279d177868cSLuiz Otavio O Souza * In the wildcard case, we need to update the name. 280f889d2efSBrooks Davis */ 281d177868cSLuiz Otavio O Souza if (wildcard) { 282d177868cSLuiz Otavio O Souza for (dp = name; *dp != '\0'; dp++); 283d177868cSLuiz Otavio O Souza if (snprintf(dp, len - (dp-name), "%d", unit) > 284d177868cSLuiz Otavio O Souza len - (dp-name) - 1) { 285d177868cSLuiz Otavio O Souza /* 286d177868cSLuiz Otavio O Souza * This can only be a programmer error and 287d177868cSLuiz Otavio O Souza * there's no straightforward way to recover if 288d177868cSLuiz Otavio O Souza * it happens. 289d177868cSLuiz Otavio O Souza */ 290d177868cSLuiz Otavio O Souza panic("if_clone_create(): interface name too long"); 291d177868cSLuiz Otavio O Souza } 292d177868cSLuiz Otavio O Souza } 293f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 29442a58907SGleb Smirnoff ifp->if_dname = stfname; 295f889d2efSBrooks Davis ifp->if_dunit = IF_DUNIT_NONE; 296abb64706SBrooks Davis 2976d8fdfa9SAndrey V. Elsukov sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK); 298abb64706SBrooks Davis if (sc->encap_cookie == NULL) { 2991861b710SBrooks Davis if_printf(ifp, "attach failed\n"); 300abb64706SBrooks Davis free(sc, M_STF); 301f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 30253dab5feSBrooks Davis return (ENOMEM); 303686cdd19SJun-ichiro itojun Hagino } 304686cdd19SJun-ichiro itojun Hagino 3051861b710SBrooks Davis ifp->if_mtu = IPV6_MMTU; 3061861b710SBrooks Davis ifp->if_ioctl = stf_ioctl; 3071861b710SBrooks Davis ifp->if_output = stf_output; 308e50d35e6SMaxim Sobolev ifp->if_snd.ifq_maxlen = ifqmaxlen; 3091861b710SBrooks Davis if_attach(ifp); 31001399f34SDavid Malone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 31191ebcbe0SAlexander V. Chernikov *ifpp = ifp; 31291ebcbe0SAlexander V. Chernikov 313abb64706SBrooks Davis return (0); 314abb64706SBrooks Davis } 315abb64706SBrooks Davis 316f889d2efSBrooks Davis static int 31791ebcbe0SAlexander V. Chernikov stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 318abb64706SBrooks Davis { 319fc74a9f9SBrooks Davis struct stf_softc *sc = ifp->if_softc; 32046d0f824SMatt Macy int err __unused; 321abb64706SBrooks Davis 3226d8fdfa9SAndrey V. Elsukov err = ip_encap_detach(sc->encap_cookie); 323febd0759SAndrew Thompson KASSERT(err == 0, ("Unexpected error detaching encap_cookie")); 324febd0759SAndrew Thompson bpfdetach(ifp); 325febd0759SAndrew Thompson if_detach(ifp); 326febd0759SAndrew Thompson if_free(ifp); 327febd0759SAndrew Thompson 328febd0759SAndrew Thompson free(sc, M_STF); 329f889d2efSBrooks Davis ifc_free_unit(ifc, STFUNIT); 330f889d2efSBrooks Davis 331f889d2efSBrooks Davis return (0); 332abb64706SBrooks Davis } 333abb64706SBrooks Davis 3348e45fed3SKristof Provost static void 3358e45fed3SKristof Provost vnet_stf_init(const void *unused __unused) 3368e45fed3SKristof Provost { 33791ebcbe0SAlexander V. Chernikov struct if_clone_addreq req = { 33891ebcbe0SAlexander V. Chernikov .match_f = stf_clone_match, 33991ebcbe0SAlexander V. Chernikov .create_f = stf_clone_create, 34091ebcbe0SAlexander V. Chernikov .destroy_f = stf_clone_destroy, 34191ebcbe0SAlexander V. Chernikov }; 34291ebcbe0SAlexander V. Chernikov V_stf_cloner = ifc_attach_cloner(stfname, &req); 3438e45fed3SKristof Provost } 3448e45fed3SKristof Provost VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL); 3458e45fed3SKristof Provost 3468e45fed3SKristof Provost static void 3478e45fed3SKristof Provost vnet_stf_uninit(const void *unused __unused) 3488e45fed3SKristof Provost { 3498e45fed3SKristof Provost if_clone_detach(V_stf_cloner); 3508e45fed3SKristof Provost V_stf_cloner = NULL; 3518e45fed3SKristof Provost } 3528e45fed3SKristof Provost VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit, 3538e45fed3SKristof Provost NULL); 3548e45fed3SKristof Provost 355abb64706SBrooks Davis static int 356926381e1SAndrey V. Elsukov stfmodevent(module_t mod, int type, void *data) 357abb64706SBrooks Davis { 358abb64706SBrooks Davis 359abb64706SBrooks Davis switch (type) { 360abb64706SBrooks Davis case MOD_LOAD: 3618e45fed3SKristof Provost /* Done in vnet_stf_init() */ 362abb64706SBrooks Davis break; 363abb64706SBrooks Davis case MOD_UNLOAD: 3648e45fed3SKristof Provost /* Done in vnet_stf_uninit() */ 36553dab5feSBrooks Davis break; 3663e019deaSPoul-Henning Kamp default: 3673e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 368686cdd19SJun-ichiro itojun Hagino } 369686cdd19SJun-ichiro itojun Hagino 37053dab5feSBrooks Davis return (0); 37153dab5feSBrooks Davis } 37253dab5feSBrooks Davis 37353dab5feSBrooks Davis static moduledata_t stf_mod = { 37453dab5feSBrooks Davis "if_stf", 37553dab5feSBrooks Davis stfmodevent, 3769823d527SKevin Lo 0 37753dab5feSBrooks Davis }; 37853dab5feSBrooks Davis 37953dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 38019dc6445SKristof Provost MODULE_VERSION(if_stf, 2); 381686cdd19SJun-ichiro itojun Hagino 382686cdd19SJun-ichiro itojun Hagino static int 383926381e1SAndrey V. Elsukov stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 384686cdd19SJun-ichiro itojun Hagino { 385686cdd19SJun-ichiro itojun Hagino struct ip ip; 386686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 387d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6; 38819dc6445SKristof Provost struct sockaddr_in sin4addr, sin4mask; 389686cdd19SJun-ichiro itojun Hagino 390b46512f7SKristof Provost SDT_PROBE3(if_stf, , encapcheck, in, m, off, proto); 391b46512f7SKristof Provost 392686cdd19SJun-ichiro itojun Hagino sc = (struct stf_softc *)arg; 393686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 3943576121cSKristof Provost return (0); 395686cdd19SJun-ichiro itojun Hagino 396fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_UP) == 0) 3973576121cSKristof Provost return (0); 398686cdd19SJun-ichiro itojun Hagino 39933841545SHajimu UMEMOTO /* IFF_LINK0 means "no decapsulation" */ 400fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0) 4013576121cSKristof Provost return (0); 40233841545SHajimu UMEMOTO 403686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6) 4043576121cSKristof Provost return (0); 405686cdd19SJun-ichiro itojun Hagino 406350e6227SAndrey V. Elsukov m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 407686cdd19SJun-ichiro itojun Hagino 408686cdd19SJun-ichiro itojun Hagino if (ip.ip_v != 4) 4093576121cSKristof Provost return (0); 410686cdd19SJun-ichiro itojun Hagino 411d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0) 412d74b9a2cSAlexander V. Chernikov return (0); 413686cdd19SJun-ichiro itojun Hagino 41419dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) { 41519dc6445SKristof Provost sin4addr.sin_addr.s_addr = sc->srcv4_addr; 41619dc6445SKristof Provost sin4addr.sin_family = AF_INET; 41719dc6445SKristof Provost } else 41819dc6445SKristof Provost if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL) 4193576121cSKristof Provost return (0); 420686cdd19SJun-ichiro itojun Hagino 42119dc6445SKristof Provost if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr) 42219dc6445SKristof Provost return (0); 42319dc6445SKristof Provost 42419dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) { 425686cdd19SJun-ichiro itojun Hagino /* 42619dc6445SKristof Provost * 6to4 (RFC 3056). 42719dc6445SKristof Provost * Check if IPv4 src matches the IPv4 address derived 42819dc6445SKristof Provost * from the local 6to4 address masked by prefixmask. 429686cdd19SJun-ichiro itojun Hagino * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 430686cdd19SJun-ichiro itojun Hagino * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 431686cdd19SJun-ichiro itojun Hagino */ 43219dc6445SKristof Provost memcpy(&sin4mask.sin_addr, GET_V4(&mask6), 43319dc6445SKristof Provost sizeof(sin4mask.sin_addr)); 43419dc6445SKristof Provost if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) != 43519dc6445SKristof Provost (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr)) 4363576121cSKristof Provost return (0); 43719dc6445SKristof Provost } else { 43819dc6445SKristof Provost /* 6rd (RFC 5569) */ 43919dc6445SKristof Provost /* 44019dc6445SKristof Provost * No restriction on the src address in the case of 44119dc6445SKristof Provost * 6rd because the stf(4) interface always has a 44219dc6445SKristof Provost * prefix which covers whole of IPv4 src address 44319dc6445SKristof Provost * range. So, stf_output() will catch all of 44419dc6445SKristof Provost * 6rd-capsuled IPv4 traffic with suspicious inner dst 44519dc6445SKristof Provost * IPv4 address (i.e. the IPv6 destination address is 44619dc6445SKristof Provost * one the admin does not like to route to outside), 44719dc6445SKristof Provost * and then it discard them silently. 44819dc6445SKristof Provost */ 44919dc6445SKristof Provost } 450686cdd19SJun-ichiro itojun Hagino 451b46512f7SKristof Provost SDT_PROBE0(if_stf, , encapcheck, accept); 452b46512f7SKristof Provost 453686cdd19SJun-ichiro itojun Hagino /* stf interface makes single side match only */ 4543576121cSKristof Provost return (32); 455686cdd19SJun-ichiro itojun Hagino } 456686cdd19SJun-ichiro itojun Hagino 457d74b9a2cSAlexander V. Chernikov static int 458d74b9a2cSAlexander V. Chernikov stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask) 459686cdd19SJun-ichiro itojun Hagino { 460686cdd19SJun-ichiro itojun Hagino struct ifaddr *ia; 461686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4; 46219dc6445SKristof Provost struct in6_addr addr6, mask6; 46319dc6445SKristof Provost struct sockaddr_in sin4; 46419dc6445SKristof Provost struct stf_softc *sc; 465686cdd19SJun-ichiro itojun Hagino struct in_addr in; 466686cdd19SJun-ichiro itojun Hagino 4674b24e5b1SGleb Smirnoff NET_EPOCH_ASSERT(); 4684b24e5b1SGleb Smirnoff 46919dc6445SKristof Provost sc = ifp->if_softc; 47019dc6445SKristof Provost 471b46512f7SKristof Provost SDT_PROBE3(if_stf, , getsrcifa6, in, ifp, addr, mask); 472b46512f7SKristof Provost 473d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 474686cdd19SJun-ichiro itojun Hagino if (ia->ifa_addr->sa_family != AF_INET6) 475686cdd19SJun-ichiro itojun Hagino continue; 476686cdd19SJun-ichiro itojun Hagino 47719dc6445SKristof Provost addr6 = *IFA_IN6(ia); 47819dc6445SKristof Provost mask6 = *IFA_MASKIN6(ia); 47919dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) 48019dc6445SKristof Provost bcopy(&sc->srcv4_addr, &in, sizeof(in)); 48119dc6445SKristof Provost else { 48219dc6445SKristof Provost if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL) 48319dc6445SKristof Provost continue; 48419dc6445SKristof Provost bcopy(&sin4.sin_addr, &in, sizeof(in)); 48519dc6445SKristof Provost } 48619dc6445SKristof Provost 487c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash) 488686cdd19SJun-ichiro itojun Hagino if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) 489686cdd19SJun-ichiro itojun Hagino break; 490686cdd19SJun-ichiro itojun Hagino if (ia4 == NULL) 491686cdd19SJun-ichiro itojun Hagino continue; 492686cdd19SJun-ichiro itojun Hagino 49319dc6445SKristof Provost *addr = addr6; 49419dc6445SKristof Provost *mask = mask6; 495d74b9a2cSAlexander V. Chernikov 496b46512f7SKristof Provost SDT_PROBE2(if_stf, , getsrcifa6, found, addr, mask); 497b46512f7SKristof Provost 498d74b9a2cSAlexander V. Chernikov return (0); 499686cdd19SJun-ichiro itojun Hagino } 500686cdd19SJun-ichiro itojun Hagino 501b46512f7SKristof Provost SDT_PROBE0(if_stf, , getsrcifa6, notfound); 502b46512f7SKristof Provost 503d74b9a2cSAlexander V. Chernikov return (ENOENT); 504686cdd19SJun-ichiro itojun Hagino } 505686cdd19SJun-ichiro itojun Hagino 506686cdd19SJun-ichiro itojun Hagino static int 50747e8d432SGleb Smirnoff stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 50847e8d432SGleb Smirnoff struct route *ro) 509686cdd19SJun-ichiro itojun Hagino { 510686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 51147e8d432SGleb Smirnoff const struct sockaddr_in6 *dst6; 51219dc6445SKristof Provost struct sockaddr_in dst4, src4; 513686cdd19SJun-ichiro itojun Hagino u_int8_t tos; 514686cdd19SJun-ichiro itojun Hagino struct ip *ip; 515686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6; 516d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6; 5176b459e49SRobert Watson int error; 5186b459e49SRobert Watson 519b46512f7SKristof Provost SDT_PROBE4(if_stf, , stf_output, in, ifp, m, dst, ro); 520b46512f7SKristof Provost 5210dae32f2SDavid Malone #ifdef MAC 52230d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 5236b459e49SRobert Watson if (error) { 5246b459e49SRobert Watson m_freem(m); 525b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, error, __LINE__); 5266b459e49SRobert Watson return (error); 5276b459e49SRobert Watson } 5286b459e49SRobert Watson #endif 529686cdd19SJun-ichiro itojun Hagino 530fc74a9f9SBrooks Davis sc = ifp->if_softc; 53147e8d432SGleb Smirnoff dst6 = (const struct sockaddr_in6 *)dst; 532686cdd19SJun-ichiro itojun Hagino 533686cdd19SJun-ichiro itojun Hagino /* just in case */ 534686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_UP) == 0) { 535686cdd19SJun-ichiro itojun Hagino m_freem(m); 5363751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 537b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__); 5383576121cSKristof Provost return (ENETDOWN); 539686cdd19SJun-ichiro itojun Hagino } 540686cdd19SJun-ichiro itojun Hagino 541686cdd19SJun-ichiro itojun Hagino /* 542686cdd19SJun-ichiro itojun Hagino * If we don't have an ip4 address that match my inner ip6 address, 543686cdd19SJun-ichiro itojun Hagino * we shouldn't generate output. Without this check, we'll end up 544686cdd19SJun-ichiro itojun Hagino * using wrong IPv4 source. 545686cdd19SJun-ichiro itojun Hagino */ 546d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) { 547686cdd19SJun-ichiro itojun Hagino m_freem(m); 5483751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 549b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__); 5503576121cSKristof Provost return (ENETDOWN); 551686cdd19SJun-ichiro itojun Hagino } 552686cdd19SJun-ichiro itojun Hagino 553686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) { 554686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6)); 555f26b2d5bSHajimu UMEMOTO if (!m) { 5563751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 557b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, 558b46512f7SKristof Provost __LINE__); 5593576121cSKristof Provost return (ENOBUFS); 560686cdd19SJun-ichiro itojun Hagino } 561f26b2d5bSHajimu UMEMOTO } 562686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *); 563bb4a7d94SKristof Provost tos = IPV6_TRAFFIC_CLASS(ip6); 564686cdd19SJun-ichiro itojun Hagino 56533841545SHajimu UMEMOTO /* 56633841545SHajimu UMEMOTO * Pickup the right outer dst addr from the list of candidates. 56733841545SHajimu UMEMOTO * ip6_dst has priority as it may be able to give us shorter IPv4 hops. 56833841545SHajimu UMEMOTO */ 56919dc6445SKristof Provost if (stf_getin4addr_in6(sc, &dst4, addr6, mask6, 57019dc6445SKristof Provost ip6->ip6_dst) == NULL) { 57119dc6445SKristof Provost if (sc->braddr != INADDR_ANY) 57219dc6445SKristof Provost dst4.sin_addr.s_addr = sc->braddr; 57319dc6445SKristof Provost else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6, 57419dc6445SKristof Provost dst6->sin6_addr) == NULL) { 57533841545SHajimu UMEMOTO m_freem(m); 5763751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 577b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, 578b46512f7SKristof Provost __LINE__); 5793576121cSKristof Provost return (ENETUNREACH); 58033841545SHajimu UMEMOTO } 58119dc6445SKristof Provost } 58233841545SHajimu UMEMOTO 58316d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 5848acb2290SHajimu UMEMOTO /* 5858acb2290SHajimu UMEMOTO * We need to prepend the address family as 5868acb2290SHajimu UMEMOTO * a four byte field. Cons up a dummy header 5878acb2290SHajimu UMEMOTO * to pacify bpf. This is safe because bpf 5888acb2290SHajimu UMEMOTO * will only read from the mbuf (i.e., it won't 5898acb2290SHajimu UMEMOTO * try to free it or keep a pointer a to it). 5908acb2290SHajimu UMEMOTO */ 59147e8d432SGleb Smirnoff u_int af = AF_INET6; 592437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 5938acb2290SHajimu UMEMOTO } 5948acb2290SHajimu UMEMOTO 595eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct ip), M_NOWAIT); 596f26b2d5bSHajimu UMEMOTO if (m == NULL) { 5973751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 598b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, __LINE__); 5993576121cSKristof Provost return (ENOBUFS); 600f26b2d5bSHajimu UMEMOTO } 601686cdd19SJun-ichiro itojun Hagino ip = mtod(m, struct ip *); 602686cdd19SJun-ichiro itojun Hagino 603686cdd19SJun-ichiro itojun Hagino bzero(ip, sizeof(*ip)); 604686cdd19SJun-ichiro itojun Hagino 60519dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) 60619dc6445SKristof Provost src4.sin_addr.s_addr = sc->srcv4_addr; 60719dc6445SKristof Provost else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) { 60819dc6445SKristof Provost m_freem(m); 60919dc6445SKristof Provost if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 610b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, __LINE__); 61119dc6445SKristof Provost return (ENETUNREACH); 61219dc6445SKristof Provost } 61319dc6445SKristof Provost bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src)); 61419dc6445SKristof Provost bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)); 61519dc6445SKristof Provost 616686cdd19SJun-ichiro itojun Hagino ip->ip_p = IPPROTO_IPV6; 61753dab5feSBrooks Davis ip->ip_ttl = ip_stf_ttl; 6188f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 619686cdd19SJun-ichiro itojun Hagino if (ifp->if_flags & IFF_LINK1) 620686cdd19SJun-ichiro itojun Hagino ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); 62133841545SHajimu UMEMOTO else 62233841545SHajimu UMEMOTO ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); 623686cdd19SJun-ichiro itojun Hagino 6248b07e49aSJulian Elischer M_SETFIB(m, sc->sc_fibnum); 6253751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 626d74b9a2cSAlexander V. Chernikov error = ip_output(m, NULL, NULL, 0, NULL, NULL); 6270dae32f2SDavid Malone 628b46512f7SKristof Provost SDT_PROBE1(if_stf, , stf_output, out, error); 6293576121cSKristof Provost return (error); 630686cdd19SJun-ichiro itojun Hagino } 631686cdd19SJun-ichiro itojun Hagino 632686cdd19SJun-ichiro itojun Hagino static int 633926381e1SAndrey V. Elsukov isrfc1918addr(struct in_addr *in) 634ce9d7b2fSHajimu UMEMOTO { 635ce9d7b2fSHajimu UMEMOTO /* 636ce9d7b2fSHajimu UMEMOTO * returns 1 if private address range: 637ce9d7b2fSHajimu UMEMOTO * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 638ce9d7b2fSHajimu UMEMOTO */ 63951743c5fSAndrey V. Elsukov if (stf_permit_rfc1918 == 0 && ( 64051743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 || 6418d95b0ceSSUZUKI Shinsuke (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 || 64251743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)) 6433576121cSKristof Provost return (1); 644ce9d7b2fSHajimu UMEMOTO 6453576121cSKristof Provost return (0); 646ce9d7b2fSHajimu UMEMOTO } 647ce9d7b2fSHajimu UMEMOTO 648ce9d7b2fSHajimu UMEMOTO static int 649926381e1SAndrey V. Elsukov stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp) 650686cdd19SJun-ichiro itojun Hagino { 651686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4; 652686cdd19SJun-ichiro itojun Hagino 653686cdd19SJun-ichiro itojun Hagino /* 654686cdd19SJun-ichiro itojun Hagino * reject packets with the following address: 655686cdd19SJun-ichiro itojun Hagino * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 656686cdd19SJun-ichiro itojun Hagino */ 6578d95b0ceSSUZUKI Shinsuke if (IN_MULTICAST(ntohl(in->s_addr))) 6583576121cSKristof Provost return (-1); 6598d95b0ceSSUZUKI Shinsuke switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { 6608d95b0ceSSUZUKI Shinsuke case 0: case 127: case 255: 6613576121cSKristof Provost return (-1); 6628d95b0ceSSUZUKI Shinsuke } 663686cdd19SJun-ichiro itojun Hagino 664686cdd19SJun-ichiro itojun Hagino /* 665686cdd19SJun-ichiro itojun Hagino * reject packets with broadcast 666686cdd19SJun-ichiro itojun Hagino */ 667d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) { 668686cdd19SJun-ichiro itojun Hagino if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 669686cdd19SJun-ichiro itojun Hagino continue; 6702d9cfabaSRobert Watson if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { 6713576121cSKristof Provost return (-1); 672686cdd19SJun-ichiro itojun Hagino } 6732d9cfabaSRobert Watson } 674686cdd19SJun-ichiro itojun Hagino 675686cdd19SJun-ichiro itojun Hagino /* 676686cdd19SJun-ichiro itojun Hagino * perform ingress filter 677686cdd19SJun-ichiro itojun Hagino */ 678fc74a9f9SBrooks Davis if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) { 6796ad7446cSAlexander V. Chernikov struct nhop_object *nh; 680686cdd19SJun-ichiro itojun Hagino 6816ad7446cSAlexander V. Chernikov NET_EPOCH_ASSERT(); 6826ad7446cSAlexander V. Chernikov nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0); 6836ad7446cSAlexander V. Chernikov if (nh == NULL) 6840792bcbbSAlexander V. Chernikov return (-1); 6850792bcbbSAlexander V. Chernikov 6866ad7446cSAlexander V. Chernikov if (nh->nh_ifp != inifp) 6870792bcbbSAlexander V. Chernikov return (-1); 688686cdd19SJun-ichiro itojun Hagino } 689686cdd19SJun-ichiro itojun Hagino 6903576121cSKristof Provost return (0); 691686cdd19SJun-ichiro itojun Hagino } 692686cdd19SJun-ichiro itojun Hagino 693686cdd19SJun-ichiro itojun Hagino static int 694926381e1SAndrey V. Elsukov stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp) 695686cdd19SJun-ichiro itojun Hagino { 696b46512f7SKristof Provost SDT_PROBE3(if_stf, , checkaddr6, in, sc, in6, inifp); 697b46512f7SKristof Provost 698686cdd19SJun-ichiro itojun Hagino /* 699686cdd19SJun-ichiro itojun Hagino * check 6to4 addresses 700686cdd19SJun-ichiro itojun Hagino */ 7018d95b0ceSSUZUKI Shinsuke if (IN6_IS_ADDR_6TO4(in6)) { 7028d95b0ceSSUZUKI Shinsuke struct in_addr in4; 703b46512f7SKristof Provost int ret; 704b46512f7SKristof Provost 7058d95b0ceSSUZUKI Shinsuke bcopy(GET_V4(in6), &in4, sizeof(in4)); 706b46512f7SKristof Provost ret = stf_checkaddr4(sc, &in4, inifp); 707b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, ret, __LINE__); 708b46512f7SKristof Provost return (ret); 7098d95b0ceSSUZUKI Shinsuke } 710686cdd19SJun-ichiro itojun Hagino 711686cdd19SJun-ichiro itojun Hagino /* 712686cdd19SJun-ichiro itojun Hagino * reject anything that look suspicious. the test is implemented 713686cdd19SJun-ichiro itojun Hagino * in ip6_input too, but we check here as well to 714686cdd19SJun-ichiro itojun Hagino * (1) reject bad packets earlier, and 715686cdd19SJun-ichiro itojun Hagino * (2) to be safe against future ip6_input change. 716686cdd19SJun-ichiro itojun Hagino */ 717b46512f7SKristof Provost if (IN6_IS_ADDR_V4COMPAT(in6)) { 718b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__); 7193576121cSKristof Provost return (-1); 720b46512f7SKristof Provost } 721686cdd19SJun-ichiro itojun Hagino 722b46512f7SKristof Provost if (IN6_IS_ADDR_V4MAPPED(in6)) { 723b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__); 724b46512f7SKristof Provost return (-1); 725b46512f7SKristof Provost } 726b46512f7SKristof Provost 727b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, 0, __LINE__); 7283576121cSKristof Provost return (0); 729686cdd19SJun-ichiro itojun Hagino } 730686cdd19SJun-ichiro itojun Hagino 73106cd035aSAndrey V. Elsukov static int 7326d8fdfa9SAndrey V. Elsukov in_stf_input(struct mbuf *m, int off, int proto, void *arg) 733686cdd19SJun-ichiro itojun Hagino { 7346d8fdfa9SAndrey V. Elsukov struct stf_softc *sc = arg; 735439da7f0SKristof Provost struct ip ip; 736686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6; 737686cdd19SJun-ichiro itojun Hagino u_int8_t otos, itos; 738686cdd19SJun-ichiro itojun Hagino struct ifnet *ifp; 73919dc6445SKristof Provost struct nhop_object *nh; 740686cdd19SJun-ichiro itojun Hagino 741b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 742b8a6e03fSGleb Smirnoff 743b46512f7SKristof Provost SDT_PROBE3(if_stf, , stf_input, in, m, off, proto); 744b46512f7SKristof Provost 745686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6) { 746686cdd19SJun-ichiro itojun Hagino m_freem(m); 747b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7488f5a8818SKevin Lo return (IPPROTO_DONE); 749686cdd19SJun-ichiro itojun Hagino } 750686cdd19SJun-ichiro itojun Hagino 751439da7f0SKristof Provost m_copydata(m, 0, sizeof(struct ip), (caddr_t)&ip); 752fc74a9f9SBrooks Davis if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) { 753686cdd19SJun-ichiro itojun Hagino m_freem(m); 754b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7558f5a8818SKevin Lo return (IPPROTO_DONE); 756686cdd19SJun-ichiro itojun Hagino } 757686cdd19SJun-ichiro itojun Hagino 758fc74a9f9SBrooks Davis ifp = STF2IFP(sc); 759686cdd19SJun-ichiro itojun Hagino 7606b459e49SRobert Watson #ifdef MAC 76130d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 7626b459e49SRobert Watson #endif 7636b459e49SRobert Watson 764686cdd19SJun-ichiro itojun Hagino /* 765686cdd19SJun-ichiro itojun Hagino * perform sanity check against outer src/dst. 766686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well. 767686cdd19SJun-ichiro itojun Hagino */ 768439da7f0SKristof Provost if (stf_checkaddr4(sc, &ip.ip_dst, NULL) < 0 || 769439da7f0SKristof Provost stf_checkaddr4(sc, &ip.ip_src, m->m_pkthdr.rcvif) < 0) { 770686cdd19SJun-ichiro itojun Hagino m_freem(m); 771b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7728f5a8818SKevin Lo return (IPPROTO_DONE); 773686cdd19SJun-ichiro itojun Hagino } 774686cdd19SJun-ichiro itojun Hagino 775439da7f0SKristof Provost otos = ip.ip_tos; 776686cdd19SJun-ichiro itojun Hagino m_adj(m, off); 777686cdd19SJun-ichiro itojun Hagino 778686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) { 779686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6)); 780b46512f7SKristof Provost if (!m) { 781b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, 782b46512f7SKristof Provost __LINE__); 7838f5a8818SKevin Lo return (IPPROTO_DONE); 784686cdd19SJun-ichiro itojun Hagino } 785b46512f7SKristof Provost } 786686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *); 787686cdd19SJun-ichiro itojun Hagino 788686cdd19SJun-ichiro itojun Hagino /* 789686cdd19SJun-ichiro itojun Hagino * perform sanity check against inner src/dst. 790686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well. 791686cdd19SJun-ichiro itojun Hagino */ 79233841545SHajimu UMEMOTO if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 || 79333841545SHajimu UMEMOTO stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) { 794686cdd19SJun-ichiro itojun Hagino m_freem(m); 795b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7968f5a8818SKevin Lo return (IPPROTO_DONE); 797686cdd19SJun-ichiro itojun Hagino } 798686cdd19SJun-ichiro itojun Hagino 79919dc6445SKristof Provost /* 80019dc6445SKristof Provost * reject packets with private address range. 80119dc6445SKristof Provost * (requirement from RFC3056 section 2 1st paragraph) 80219dc6445SKristof Provost */ 803439da7f0SKristof Provost if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip.ip_src)) || 804439da7f0SKristof Provost (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip.ip_dst))) { 80519dc6445SKristof Provost m_freem(m); 806b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 80719dc6445SKristof Provost return (IPPROTO_DONE); 80819dc6445SKristof Provost } 80919dc6445SKristof Provost 81019dc6445SKristof Provost /* 81119dc6445SKristof Provost * Ignore if the destination is the same stf interface because 81219dc6445SKristof Provost * all of valid IPv6 outgoing traffic should go interfaces 81319dc6445SKristof Provost * except for it. 81419dc6445SKristof Provost */ 81519dc6445SKristof Provost nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0); 81619dc6445SKristof Provost if (nh == NULL) { 81719dc6445SKristof Provost m_free(m); 818b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 81919dc6445SKristof Provost return (IPPROTO_DONE); 82019dc6445SKristof Provost } 82119dc6445SKristof Provost if ((nh->nh_ifp == ifp) && 82219dc6445SKristof Provost (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) { 82319dc6445SKristof Provost m_free(m); 824b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 82519dc6445SKristof Provost return (IPPROTO_DONE); 82619dc6445SKristof Provost } 82719dc6445SKristof Provost 828bb4a7d94SKristof Provost itos = IPV6_TRAFFIC_CLASS(ip6); 829686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_LINK1) != 0) 830686cdd19SJun-ichiro itojun Hagino ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 83133841545SHajimu UMEMOTO else 83233841545SHajimu UMEMOTO ip_ecn_egress(ECN_NOCARE, &otos, &itos); 833686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow &= ~htonl(0xff << 20); 834686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 835686cdd19SJun-ichiro itojun Hagino 836686cdd19SJun-ichiro itojun Hagino m->m_pkthdr.rcvif = ifp; 837686cdd19SJun-ichiro itojun Hagino 83816d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 839686cdd19SJun-ichiro itojun Hagino /* 840686cdd19SJun-ichiro itojun Hagino * We need to prepend the address family as 841686cdd19SJun-ichiro itojun Hagino * a four byte field. Cons up a dummy header 842686cdd19SJun-ichiro itojun Hagino * to pacify bpf. This is safe because bpf 843686cdd19SJun-ichiro itojun Hagino * will only read from the mbuf (i.e., it won't 844686cdd19SJun-ichiro itojun Hagino * try to free it or keep a pointer a to it). 845686cdd19SJun-ichiro itojun Hagino */ 84633841545SHajimu UMEMOTO u_int32_t af = AF_INET6; 8479b1a7076SHajimu UMEMOTO bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 848686cdd19SJun-ichiro itojun Hagino } 849686cdd19SJun-ichiro itojun Hagino 850686cdd19SJun-ichiro itojun Hagino /* 851686cdd19SJun-ichiro itojun Hagino * Put the packet to the network layer input queue according to the 852686cdd19SJun-ichiro itojun Hagino * specified address family. 853686cdd19SJun-ichiro itojun Hagino * See net/if_gif.c for possible issues with packet processing 854686cdd19SJun-ichiro itojun Hagino * reorder due to extra queueing. 855686cdd19SJun-ichiro itojun Hagino */ 8563751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 8573751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 858a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 8591cafed39SJonathan Lemon netisr_dispatch(NETISR_IPV6, m); 860b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 8618f5a8818SKevin Lo return (IPPROTO_DONE); 862686cdd19SJun-ichiro itojun Hagino } 863686cdd19SJun-ichiro itojun Hagino 86419dc6445SKristof Provost static struct sockaddr_in * 86519dc6445SKristof Provost stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin, 86619dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6) 86719dc6445SKristof Provost { 86819dc6445SKristof Provost int i; 869b46512f7SKristof Provost struct sockaddr_in *out; 87019dc6445SKristof Provost 87119dc6445SKristof Provost /* 87219dc6445SKristof Provost * When (src addr & src mask) != (in6 & src mask), 87319dc6445SKristof Provost * the dst is not in the 6rd domain. The IPv4 address must 87419dc6445SKristof Provost * not be used. 87519dc6445SKristof Provost */ 87619dc6445SKristof Provost for (i = 0; i < sizeof(addr6); i++) { 87719dc6445SKristof Provost if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) != 878b46512f7SKristof Provost (((u_char *)&in6)[i] & ((u_char *)&mask6)[i])) { 879b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, 880b46512f7SKristof Provost &mask6, &in6, NULL); 88119dc6445SKristof Provost return (NULL); 88219dc6445SKristof Provost } 883b46512f7SKristof Provost } 88419dc6445SKristof Provost 88519dc6445SKristof Provost /* After the mask check, use in6 instead of addr6. */ 886b46512f7SKristof Provost out = stf_getin4addr(sc, sin, in6, mask6); 887b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, &mask6, &in6, out); 888b46512f7SKristof Provost return (out); 88919dc6445SKristof Provost } 89019dc6445SKristof Provost 89119dc6445SKristof Provost static struct sockaddr_in * 89219dc6445SKristof Provost stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin, 89319dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6) 89419dc6445SKristof Provost { 89519dc6445SKristof Provost struct in_addr *in; 89619dc6445SKristof Provost 897b46512f7SKristof Provost SDT_PROBE2(if_stf, , getin4addr, in, &addr6, &mask6); 898b46512f7SKristof Provost 89919dc6445SKristof Provost memset(sin, 0, sizeof(*sin)); 90019dc6445SKristof Provost in = &sin->sin_addr; 90119dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) { 90219dc6445SKristof Provost /* 6to4 (RFC 3056) */ 90319dc6445SKristof Provost bcopy(GET_V4(&addr6), in, sizeof(*in)); 90419dc6445SKristof Provost if (isrfc1918addr(in)) 90519dc6445SKristof Provost return (NULL); 90619dc6445SKristof Provost } else { 90719dc6445SKristof Provost /* 6rd (RFC 5569) */ 90819dc6445SKristof Provost in_addr_t v4prefix; 90919dc6445SKristof Provost uint8_t *v6 = (uint8_t*)&addr6; 91019dc6445SKristof Provost uint64_t v6prefix; 91119dc6445SKristof Provost u_int plen; 91219dc6445SKristof Provost u_int v4suffixlen; 91319dc6445SKristof Provost 91419dc6445SKristof Provost v4prefix = 0; 91519dc6445SKristof Provost if (sc->v4prefixlen < 32) { 91619dc6445SKristof Provost v4suffixlen = 32 - sc->v4prefixlen; 91719dc6445SKristof Provost v4prefix = ntohl(sc->srcv4_addr) & 91819dc6445SKristof Provost (0xffffffffU << v4suffixlen); 91919dc6445SKristof Provost } else { 92019dc6445SKristof Provost MPASS(sc->v4prefixlen == 32); 92119dc6445SKristof Provost v4suffixlen = 32; 92219dc6445SKristof Provost } 92319dc6445SKristof Provost 92419dc6445SKristof Provost plen = in6_mask2len(&mask6, NULL); 92519dc6445SKristof Provost if (plen > 64) 92619dc6445SKristof Provost return (NULL); 92719dc6445SKristof Provost 92819dc6445SKristof Provost /* To make this simple we do not support prefixes longer than 92919dc6445SKristof Provost * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64 93019dc6445SKristof Provost * or shorter." so this is a moderately safe assumption. */ 93119dc6445SKristof Provost v6prefix = be64toh(*(uint64_t *)v6); 93219dc6445SKristof Provost 93319dc6445SKristof Provost /* Shift away the v6 prefix itself. */ 93419dc6445SKristof Provost v6prefix <<= plen; 93519dc6445SKristof Provost v6prefix >>= plen; 93619dc6445SKristof Provost 93719dc6445SKristof Provost /* Now shift away everything after the v4 address. */ 93819dc6445SKristof Provost v6prefix >>= 64 - plen - v4suffixlen; 93919dc6445SKristof Provost 94019dc6445SKristof Provost sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix); 94119dc6445SKristof Provost } 94219dc6445SKristof Provost 943b46512f7SKristof Provost SDT_PROBE1(if_stf, , getin4addr, out, sin); 944b46512f7SKristof Provost 94519dc6445SKristof Provost return (sin); 94619dc6445SKristof Provost } 94719dc6445SKristof Provost 948686cdd19SJun-ichiro itojun Hagino static int 949926381e1SAndrey V. Elsukov stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 950686cdd19SJun-ichiro itojun Hagino { 951686cdd19SJun-ichiro itojun Hagino struct ifaddr *ifa; 95219dc6445SKristof Provost struct ifdrv *ifd; 953686cdd19SJun-ichiro itojun Hagino struct ifreq *ifr; 95419dc6445SKristof Provost struct sockaddr_in sin4; 95519dc6445SKristof Provost struct stf_softc *sc_cur; 95619dc6445SKristof Provost struct stfv4args args; 95799ab4b12SAlexander V. Chernikov int error, mtu; 958686cdd19SJun-ichiro itojun Hagino 959686cdd19SJun-ichiro itojun Hagino error = 0; 96019dc6445SKristof Provost sc_cur = ifp->if_softc; 96119dc6445SKristof Provost 962686cdd19SJun-ichiro itojun Hagino switch (cmd) { 96319dc6445SKristof Provost case SIOCSDRVSPEC: 96419dc6445SKristof Provost ifd = (struct ifdrv *)data; 96519dc6445SKristof Provost error = priv_check(curthread, PRIV_NET_ADDIFADDR); 96619dc6445SKristof Provost if (error) 96719dc6445SKristof Provost break; 96819dc6445SKristof Provost if (ifd->ifd_cmd == STF6RD_SV4NET) { 96919dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 97019dc6445SKristof Provost error = EINVAL; 97119dc6445SKristof Provost break; 97219dc6445SKristof Provost } 97319dc6445SKristof Provost bzero(&args, sizeof(args)); 97419dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 97519dc6445SKristof Provost if (error) 97619dc6445SKristof Provost break; 97719dc6445SKristof Provost 97819dc6445SKristof Provost if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) { 97919dc6445SKristof Provost error = EINVAL; 98019dc6445SKristof Provost break; 98119dc6445SKristof Provost } 98219dc6445SKristof Provost 98319dc6445SKristof Provost bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr, 98419dc6445SKristof Provost sizeof(sc_cur->srcv4_addr)); 98519dc6445SKristof Provost sc_cur->v4prefixlen = args.v4_prefixlen; 986b46512f7SKristof Provost SDT_PROBE3(if_stf, , ioctl, sv4net, sc_cur->srcv4_addr, 987b46512f7SKristof Provost sc_cur->srcv4_addr, sc_cur->v4prefixlen); 98819dc6445SKristof Provost } else if (ifd->ifd_cmd == STF6RD_SBR) { 98919dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 99019dc6445SKristof Provost error = EINVAL; 99119dc6445SKristof Provost break; 99219dc6445SKristof Provost } 99319dc6445SKristof Provost bzero(&args, sizeof(args)); 99419dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 99519dc6445SKristof Provost if (error) 99619dc6445SKristof Provost break; 99719dc6445SKristof Provost sc_cur->braddr = args.braddr.s_addr; 998b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, sdstv4, 999b46512f7SKristof Provost sc_cur->braddr); 100019dc6445SKristof Provost } else 100119dc6445SKristof Provost error = EINVAL; 100219dc6445SKristof Provost break; 100319dc6445SKristof Provost case SIOCGDRVSPEC: 100419dc6445SKristof Provost ifd = (struct ifdrv *)data; 100519dc6445SKristof Provost if (ifd->ifd_cmd != STF6RD_GV4NET) { 100619dc6445SKristof Provost error = EINVAL; 100719dc6445SKristof Provost break; 100819dc6445SKristof Provost } 100919dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 101019dc6445SKristof Provost error = EINVAL; 101119dc6445SKristof Provost break; 101219dc6445SKristof Provost } 101319dc6445SKristof Provost bzero(&args, sizeof(args)); 101419dc6445SKristof Provost args.srcv4_addr.s_addr = sc_cur->srcv4_addr; 101519dc6445SKristof Provost args.braddr.s_addr = sc_cur->braddr; 101619dc6445SKristof Provost args.v4_prefixlen = sc_cur->v4prefixlen; 101719dc6445SKristof Provost error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 101819dc6445SKristof Provost break; 1019686cdd19SJun-ichiro itojun Hagino case SIOCSIFADDR: 1020686cdd19SJun-ichiro itojun Hagino ifa = (struct ifaddr *)data; 1021b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, ifaddr, ifa); 1022686cdd19SJun-ichiro itojun Hagino if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) { 1023686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT; 1024686cdd19SJun-ichiro itojun Hagino break; 1025686cdd19SJun-ichiro itojun Hagino } 102619dc6445SKristof Provost if (stf_getin4addr(sc_cur, &sin4, 102719dc6445SKristof Provost satosin6(ifa->ifa_addr)->sin6_addr, 102819dc6445SKristof Provost satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) { 10298d95b0ceSSUZUKI Shinsuke error = EINVAL; 10308d95b0ceSSUZUKI Shinsuke break; 10318d95b0ceSSUZUKI Shinsuke } 1032686cdd19SJun-ichiro itojun Hagino ifp->if_flags |= IFF_UP; 1033ca1163bdSMark Johnston ifp->if_drv_flags |= IFF_DRV_RUNNING; 1034686cdd19SJun-ichiro itojun Hagino break; 1035686cdd19SJun-ichiro itojun Hagino 1036686cdd19SJun-ichiro itojun Hagino case SIOCADDMULTI: 1037686cdd19SJun-ichiro itojun Hagino case SIOCDELMULTI: 1038686cdd19SJun-ichiro itojun Hagino ifr = (struct ifreq *)data; 1039686cdd19SJun-ichiro itojun Hagino if (ifr && ifr->ifr_addr.sa_family == AF_INET6) 1040686cdd19SJun-ichiro itojun Hagino ; 1041686cdd19SJun-ichiro itojun Hagino else 1042686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT; 1043686cdd19SJun-ichiro itojun Hagino break; 1044686cdd19SJun-ichiro itojun Hagino 104599ab4b12SAlexander V. Chernikov case SIOCGIFMTU: 104699ab4b12SAlexander V. Chernikov break; 104799ab4b12SAlexander V. Chernikov 104899ab4b12SAlexander V. Chernikov case SIOCSIFMTU: 104999ab4b12SAlexander V. Chernikov ifr = (struct ifreq *)data; 105099ab4b12SAlexander V. Chernikov mtu = ifr->ifr_mtu; 105199ab4b12SAlexander V. Chernikov /* RFC 4213 3.2 ideal world MTU */ 105299ab4b12SAlexander V. Chernikov if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20) 105399ab4b12SAlexander V. Chernikov return (EINVAL); 105499ab4b12SAlexander V. Chernikov ifp->if_mtu = mtu; 105599ab4b12SAlexander V. Chernikov break; 105699ab4b12SAlexander V. Chernikov 1057686cdd19SJun-ichiro itojun Hagino default: 1058686cdd19SJun-ichiro itojun Hagino error = EINVAL; 1059686cdd19SJun-ichiro itojun Hagino break; 1060686cdd19SJun-ichiro itojun Hagino } 1061686cdd19SJun-ichiro itojun Hagino 10623576121cSKristof Provost return (error); 1063686cdd19SJun-ichiro itojun Hagino } 1064