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> 103f889d2efSBrooks Davis #include <net/if_clone.h> 104686cdd19SJun-ichiro itojun Hagino #include <net/route.h> 1056ad7446cSAlexander V. Chernikov #include <net/route/nhop.h> 106686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h> 10719dc6445SKristof Provost #include <net/if_stf.h> 108686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h> 109530c0060SRobert Watson #include <net/vnet.h> 110686cdd19SJun-ichiro itojun Hagino 111686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h> 1120792bcbbSAlexander V. Chernikov #include <netinet/in_fib.h> 113686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h> 114686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h> 115686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h> 116686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h> 117686cdd19SJun-ichiro itojun Hagino 118686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 11919dc6445SKristof Provost #include <netinet6/in6_fib.h> 120686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h> 121686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h> 122686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h> 123686cdd19SJun-ichiro itojun Hagino 124686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 125686cdd19SJun-ichiro itojun Hagino 126686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h> 127686cdd19SJun-ichiro itojun Hagino 128686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h> 129686cdd19SJun-ichiro itojun Hagino 130aed55708SRobert Watson #include <security/mac/mac_framework.h> 131aed55708SRobert Watson 132b46512f7SKristof Provost SDT_PROVIDER_DEFINE(if_stf); 133b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , encapcheck, in, "struct mbuf *", "int", "int"); 134b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , encapcheck, accept); 135b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , getsrcifa6, in, "struct ifnet *", 136b46512f7SKristof Provost "struct in6_addr *", "struct in6_addr *"); 137b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getsrcifa6, found, "struct in6_addr *", 138b46512f7SKristof Provost "struct in6_addr *"); 139b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , getsrcifa6, notfound); 140b46512f7SKristof Provost 141b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , stf_output, in, "struct ifnet *", "struct mbuf *", 142b46512f7SKristof Provost "struct sockaddr *", "struct route *"); 143b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_output, error, "int", "int"); 144b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , stf_output, out, "int"); 145b46512f7SKristof Provost 146b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , checkaddr6, in, "struct stf_softc *", 147b46512f7SKristof Provost "struct in6_addr *", "struct ifnet *"); 148b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , checkaddr6, out, "int", "int"); 149b46512f7SKristof Provost 150b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , stf_input, in, "struct mbuf *", "int", "int"); 151b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_input, out, "int", "int"); 152b46512f7SKristof Provost 153b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , ioctl, sv4net, "struct in_addr *", 154b46512f7SKristof Provost "struct in_addr *", "int"); 155b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, sdstv4, "struct in_addr *"); 156b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, ifaddr, "struct ifaddr *"); 157b46512f7SKristof Provost 158b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , getin4addr_in6, out, "struct in6_addr *", 159b46512f7SKristof Provost "struct in6_addr *", "struct in6_addr *", "struct sockaddr_in *"); 160b46512f7SKristof Provost 161b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getin4addr, in, "struct in6_addr *", "struct in6_addr *"); 162b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , getin4addr, out, "struct sockaddr_in *"); 163b46512f7SKristof Provost 1640dae32f2SDavid Malone SYSCTL_DECL(_net_link); 1657029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 1667029da5cSPawel Biernacki "6to4 Interface"); 1670dae32f2SDavid Malone 16851743c5fSAndrey V. Elsukov static int stf_permit_rfc1918 = 0; 169af3b2549SHans Petter Selasky SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN, 17051743c5fSAndrey V. Elsukov &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses"); 17151743c5fSAndrey V. Elsukov 172f889d2efSBrooks Davis #define STFUNIT 0 173abb64706SBrooks Davis 174686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 1758d95b0ceSSUZUKI Shinsuke 1768d95b0ceSSUZUKI Shinsuke /* 1778d95b0ceSSUZUKI Shinsuke * XXX: Return a pointer with 16-bit aligned. Don't cast it to 1788d95b0ceSSUZUKI Shinsuke * struct in_addr *; use bcopy() instead. 1798d95b0ceSSUZUKI Shinsuke */ 18047e8d432SGleb Smirnoff #define GET_V4(x) (&(x)->s6_addr16[1]) 181686cdd19SJun-ichiro itojun Hagino 182686cdd19SJun-ichiro itojun Hagino struct stf_softc { 183fc74a9f9SBrooks Davis struct ifnet *sc_ifp; 18419dc6445SKristof Provost in_addr_t braddr; /* Border relay IPv4 address */ 18519dc6445SKristof Provost in_addr_t srcv4_addr; /* Our IPv4 WAN address */ 18619dc6445SKristof Provost u_int v4prefixlen; /* How much of the v4 address to include in our address. */ 1878b07e49aSJulian Elischer u_int sc_fibnum; 188686cdd19SJun-ichiro itojun Hagino const struct encaptab *encap_cookie; 189686cdd19SJun-ichiro itojun Hagino }; 190fc74a9f9SBrooks Davis #define STF2IFP(sc) ((sc)->sc_ifp) 191686cdd19SJun-ichiro itojun Hagino 19242a58907SGleb Smirnoff static const char stfname[] = "stf"; 19342a58907SGleb Smirnoff 19442a58907SGleb Smirnoff static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface"); 195591cf7ceSRobert Watson static const int ip_stf_ttl = 40; 196686cdd19SJun-ichiro itojun Hagino 1976d8fdfa9SAndrey V. Elsukov static int in_stf_input(struct mbuf *, int, int, void *); 198f889d2efSBrooks Davis static char *stfnames[] = {"stf0", "stf", "6to4", NULL}; 199f889d2efSBrooks Davis 200929ddbbbSAlfred Perlstein static int stfmodevent(module_t, int, void *); 201929ddbbbSAlfred Perlstein static int stf_encapcheck(const struct mbuf *, int, int, void *); 202d74b9a2cSAlexander V. Chernikov static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *); 20347e8d432SGleb Smirnoff static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *, 204279aa3d4SKip Macy struct route *); 205ce9d7b2fSHajimu UMEMOTO static int isrfc1918addr(struct in_addr *); 206929ddbbbSAlfred Perlstein static int stf_checkaddr4(struct stf_softc *, struct in_addr *, 207929ddbbbSAlfred Perlstein struct ifnet *); 208929ddbbbSAlfred Perlstein static int stf_checkaddr6(struct stf_softc *, struct in6_addr *, 209929ddbbbSAlfred Perlstein struct ifnet *); 21019dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *, 21119dc6445SKristof Provost struct sockaddr_in *, struct in6_addr, struct in6_addr, 21219dc6445SKristof Provost struct in6_addr); 21319dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr(struct stf_softc *, 21419dc6445SKristof Provost struct sockaddr_in *, struct in6_addr, struct in6_addr); 215929ddbbbSAlfred Perlstein static int stf_ioctl(struct ifnet *, u_long, caddr_t); 216686cdd19SJun-ichiro itojun Hagino 2178e45fed3SKristof Provost VNET_DEFINE_STATIC(struct if_clone *, stf_cloner); 2188e45fed3SKristof Provost #define V_stf_cloner VNET(stf_cloner) 219abb64706SBrooks Davis 2206d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = { 2216d8fdfa9SAndrey V. Elsukov .proto = IPPROTO_IPV6, 2226d8fdfa9SAndrey V. Elsukov .min_length = sizeof(struct ip), 2236d8fdfa9SAndrey V. Elsukov .exact_match = (sizeof(in_addr_t) << 3) + 8, 2246d8fdfa9SAndrey V. Elsukov .check = stf_encapcheck, 2256d8fdfa9SAndrey V. Elsukov .input = in_stf_input 2266d8fdfa9SAndrey V. Elsukov }; 2276d8fdfa9SAndrey V. Elsukov 228bb2bfb4fSBrooks Davis static int 229f889d2efSBrooks Davis stf_clone_match(struct if_clone *ifc, const char *name) 230686cdd19SJun-ichiro itojun Hagino { 231f889d2efSBrooks Davis int i; 232f889d2efSBrooks Davis 233f889d2efSBrooks Davis for(i = 0; stfnames[i] != NULL; i++) { 234f889d2efSBrooks Davis if (strcmp(stfnames[i], name) == 0) 235f889d2efSBrooks Davis return (1); 236f889d2efSBrooks Davis } 237f889d2efSBrooks Davis 238f889d2efSBrooks Davis return (0); 239f889d2efSBrooks Davis } 240f889d2efSBrooks Davis 241f889d2efSBrooks Davis static int 242*91ebcbe0SAlexander V. Chernikov stf_clone_create(struct if_clone *ifc, char *name, size_t len, 243*91ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp) 244f889d2efSBrooks Davis { 245d177868cSLuiz Otavio O Souza char *dp; 246d177868cSLuiz Otavio O Souza int err, unit, wildcard; 247686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 2481861b710SBrooks Davis struct ifnet *ifp; 249686cdd19SJun-ichiro itojun Hagino 250d177868cSLuiz Otavio O Souza err = ifc_name2unit(name, &unit); 251d177868cSLuiz Otavio O Souza if (err != 0) 252d177868cSLuiz Otavio O Souza return (err); 253d177868cSLuiz Otavio O Souza wildcard = (unit < 0); 254d177868cSLuiz Otavio O Souza 255f889d2efSBrooks Davis /* 256f889d2efSBrooks Davis * We can only have one unit, but since unit allocation is 257f889d2efSBrooks Davis * already locked, we use it to keep from allocating extra 258f889d2efSBrooks Davis * interfaces. 259f889d2efSBrooks Davis */ 260f889d2efSBrooks Davis unit = STFUNIT; 261f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit); 262f889d2efSBrooks Davis if (err != 0) 263f889d2efSBrooks Davis return (err); 264f889d2efSBrooks Davis 265a163d034SWarner Losh sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO); 266b03965ddSBrooks Davis ifp = STF2IFP(sc) = if_alloc(IFT_STF); 267fc74a9f9SBrooks Davis if (ifp == NULL) { 268fc74a9f9SBrooks Davis free(sc, M_STF); 269fc74a9f9SBrooks Davis ifc_free_unit(ifc, unit); 270fc74a9f9SBrooks Davis return (ENOSPC); 271fc74a9f9SBrooks Davis } 272b03965ddSBrooks Davis ifp->if_softc = sc; 2738b07e49aSJulian Elischer sc->sc_fibnum = curthread->td_proc->p_fibnum; 274b03965ddSBrooks Davis 275f889d2efSBrooks Davis /* 276f889d2efSBrooks Davis * Set the name manually rather then using if_initname because 277f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces. 278d177868cSLuiz Otavio O Souza * In the wildcard case, we need to update the name. 279f889d2efSBrooks Davis */ 280d177868cSLuiz Otavio O Souza if (wildcard) { 281d177868cSLuiz Otavio O Souza for (dp = name; *dp != '\0'; dp++); 282d177868cSLuiz Otavio O Souza if (snprintf(dp, len - (dp-name), "%d", unit) > 283d177868cSLuiz Otavio O Souza len - (dp-name) - 1) { 284d177868cSLuiz Otavio O Souza /* 285d177868cSLuiz Otavio O Souza * This can only be a programmer error and 286d177868cSLuiz Otavio O Souza * there's no straightforward way to recover if 287d177868cSLuiz Otavio O Souza * it happens. 288d177868cSLuiz Otavio O Souza */ 289d177868cSLuiz Otavio O Souza panic("if_clone_create(): interface name too long"); 290d177868cSLuiz Otavio O Souza } 291d177868cSLuiz Otavio O Souza } 292f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 29342a58907SGleb Smirnoff ifp->if_dname = stfname; 294f889d2efSBrooks Davis ifp->if_dunit = IF_DUNIT_NONE; 295abb64706SBrooks Davis 2966d8fdfa9SAndrey V. Elsukov sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK); 297abb64706SBrooks Davis if (sc->encap_cookie == NULL) { 2981861b710SBrooks Davis if_printf(ifp, "attach failed\n"); 299abb64706SBrooks Davis free(sc, M_STF); 300f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 30153dab5feSBrooks Davis return (ENOMEM); 302686cdd19SJun-ichiro itojun Hagino } 303686cdd19SJun-ichiro itojun Hagino 3041861b710SBrooks Davis ifp->if_mtu = IPV6_MMTU; 3051861b710SBrooks Davis ifp->if_ioctl = stf_ioctl; 3061861b710SBrooks Davis ifp->if_output = stf_output; 307e50d35e6SMaxim Sobolev ifp->if_snd.ifq_maxlen = ifqmaxlen; 3081861b710SBrooks Davis if_attach(ifp); 30901399f34SDavid Malone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 310*91ebcbe0SAlexander V. Chernikov *ifpp = ifp; 311*91ebcbe0SAlexander V. Chernikov 312abb64706SBrooks Davis return (0); 313abb64706SBrooks Davis } 314abb64706SBrooks Davis 315f889d2efSBrooks Davis static int 316*91ebcbe0SAlexander V. Chernikov stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 317abb64706SBrooks Davis { 318fc74a9f9SBrooks Davis struct stf_softc *sc = ifp->if_softc; 31946d0f824SMatt Macy int err __unused; 320abb64706SBrooks Davis 3216d8fdfa9SAndrey V. Elsukov err = ip_encap_detach(sc->encap_cookie); 322febd0759SAndrew Thompson KASSERT(err == 0, ("Unexpected error detaching encap_cookie")); 323febd0759SAndrew Thompson bpfdetach(ifp); 324febd0759SAndrew Thompson if_detach(ifp); 325febd0759SAndrew Thompson if_free(ifp); 326febd0759SAndrew Thompson 327febd0759SAndrew Thompson free(sc, M_STF); 328f889d2efSBrooks Davis ifc_free_unit(ifc, STFUNIT); 329f889d2efSBrooks Davis 330f889d2efSBrooks Davis return (0); 331abb64706SBrooks Davis } 332abb64706SBrooks Davis 3338e45fed3SKristof Provost static void 3348e45fed3SKristof Provost vnet_stf_init(const void *unused __unused) 3358e45fed3SKristof Provost { 336*91ebcbe0SAlexander V. Chernikov struct if_clone_addreq req = { 337*91ebcbe0SAlexander V. Chernikov .match_f = stf_clone_match, 338*91ebcbe0SAlexander V. Chernikov .create_f = stf_clone_create, 339*91ebcbe0SAlexander V. Chernikov .destroy_f = stf_clone_destroy, 340*91ebcbe0SAlexander V. Chernikov }; 341*91ebcbe0SAlexander V. Chernikov V_stf_cloner = ifc_attach_cloner(stfname, &req); 3428e45fed3SKristof Provost } 3438e45fed3SKristof Provost VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL); 3448e45fed3SKristof Provost 3458e45fed3SKristof Provost static void 3468e45fed3SKristof Provost vnet_stf_uninit(const void *unused __unused) 3478e45fed3SKristof Provost { 3488e45fed3SKristof Provost if_clone_detach(V_stf_cloner); 3498e45fed3SKristof Provost V_stf_cloner = NULL; 3508e45fed3SKristof Provost } 3518e45fed3SKristof Provost VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit, 3528e45fed3SKristof Provost NULL); 3538e45fed3SKristof Provost 354abb64706SBrooks Davis static int 355926381e1SAndrey V. Elsukov stfmodevent(module_t mod, int type, void *data) 356abb64706SBrooks Davis { 357abb64706SBrooks Davis 358abb64706SBrooks Davis switch (type) { 359abb64706SBrooks Davis case MOD_LOAD: 3608e45fed3SKristof Provost /* Done in vnet_stf_init() */ 361abb64706SBrooks Davis break; 362abb64706SBrooks Davis case MOD_UNLOAD: 3638e45fed3SKristof Provost /* Done in vnet_stf_uninit() */ 36453dab5feSBrooks Davis break; 3653e019deaSPoul-Henning Kamp default: 3663e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 367686cdd19SJun-ichiro itojun Hagino } 368686cdd19SJun-ichiro itojun Hagino 36953dab5feSBrooks Davis return (0); 37053dab5feSBrooks Davis } 37153dab5feSBrooks Davis 37253dab5feSBrooks Davis static moduledata_t stf_mod = { 37353dab5feSBrooks Davis "if_stf", 37453dab5feSBrooks Davis stfmodevent, 3759823d527SKevin Lo 0 37653dab5feSBrooks Davis }; 37753dab5feSBrooks Davis 37853dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 37919dc6445SKristof Provost MODULE_VERSION(if_stf, 2); 380686cdd19SJun-ichiro itojun Hagino 381686cdd19SJun-ichiro itojun Hagino static int 382926381e1SAndrey V. Elsukov stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 383686cdd19SJun-ichiro itojun Hagino { 384686cdd19SJun-ichiro itojun Hagino struct ip ip; 385686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 386d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6; 38719dc6445SKristof Provost struct sockaddr_in sin4addr, sin4mask; 388686cdd19SJun-ichiro itojun Hagino 389b46512f7SKristof Provost SDT_PROBE3(if_stf, , encapcheck, in, m, off, proto); 390b46512f7SKristof Provost 391686cdd19SJun-ichiro itojun Hagino sc = (struct stf_softc *)arg; 392686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 3933576121cSKristof Provost return (0); 394686cdd19SJun-ichiro itojun Hagino 395fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_UP) == 0) 3963576121cSKristof Provost return (0); 397686cdd19SJun-ichiro itojun Hagino 39833841545SHajimu UMEMOTO /* IFF_LINK0 means "no decapsulation" */ 399fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0) 4003576121cSKristof Provost return (0); 40133841545SHajimu UMEMOTO 402686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6) 4033576121cSKristof Provost return (0); 404686cdd19SJun-ichiro itojun Hagino 405350e6227SAndrey V. Elsukov m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 406686cdd19SJun-ichiro itojun Hagino 407686cdd19SJun-ichiro itojun Hagino if (ip.ip_v != 4) 4083576121cSKristof Provost return (0); 409686cdd19SJun-ichiro itojun Hagino 410d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0) 411d74b9a2cSAlexander V. Chernikov return (0); 412686cdd19SJun-ichiro itojun Hagino 41319dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) { 41419dc6445SKristof Provost sin4addr.sin_addr.s_addr = sc->srcv4_addr; 41519dc6445SKristof Provost sin4addr.sin_family = AF_INET; 41619dc6445SKristof Provost } else 41719dc6445SKristof Provost if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL) 4183576121cSKristof Provost return (0); 419686cdd19SJun-ichiro itojun Hagino 42019dc6445SKristof Provost if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr) 42119dc6445SKristof Provost return (0); 42219dc6445SKristof Provost 42319dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) { 424686cdd19SJun-ichiro itojun Hagino /* 42519dc6445SKristof Provost * 6to4 (RFC 3056). 42619dc6445SKristof Provost * Check if IPv4 src matches the IPv4 address derived 42719dc6445SKristof Provost * from the local 6to4 address masked by prefixmask. 428686cdd19SJun-ichiro itojun Hagino * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 429686cdd19SJun-ichiro itojun Hagino * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 430686cdd19SJun-ichiro itojun Hagino */ 43119dc6445SKristof Provost memcpy(&sin4mask.sin_addr, GET_V4(&mask6), 43219dc6445SKristof Provost sizeof(sin4mask.sin_addr)); 43319dc6445SKristof Provost if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) != 43419dc6445SKristof Provost (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr)) 4353576121cSKristof Provost return (0); 43619dc6445SKristof Provost } else { 43719dc6445SKristof Provost /* 6rd (RFC 5569) */ 43819dc6445SKristof Provost /* 43919dc6445SKristof Provost * No restriction on the src address in the case of 44019dc6445SKristof Provost * 6rd because the stf(4) interface always has a 44119dc6445SKristof Provost * prefix which covers whole of IPv4 src address 44219dc6445SKristof Provost * range. So, stf_output() will catch all of 44319dc6445SKristof Provost * 6rd-capsuled IPv4 traffic with suspicious inner dst 44419dc6445SKristof Provost * IPv4 address (i.e. the IPv6 destination address is 44519dc6445SKristof Provost * one the admin does not like to route to outside), 44619dc6445SKristof Provost * and then it discard them silently. 44719dc6445SKristof Provost */ 44819dc6445SKristof Provost } 449686cdd19SJun-ichiro itojun Hagino 450b46512f7SKristof Provost SDT_PROBE0(if_stf, , encapcheck, accept); 451b46512f7SKristof Provost 452686cdd19SJun-ichiro itojun Hagino /* stf interface makes single side match only */ 4533576121cSKristof Provost return (32); 454686cdd19SJun-ichiro itojun Hagino } 455686cdd19SJun-ichiro itojun Hagino 456d74b9a2cSAlexander V. Chernikov static int 457d74b9a2cSAlexander V. Chernikov stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask) 458686cdd19SJun-ichiro itojun Hagino { 459686cdd19SJun-ichiro itojun Hagino struct ifaddr *ia; 460686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4; 46119dc6445SKristof Provost struct in6_addr addr6, mask6; 46219dc6445SKristof Provost struct sockaddr_in sin4; 46319dc6445SKristof Provost struct stf_softc *sc; 464686cdd19SJun-ichiro itojun Hagino struct in_addr in; 465686cdd19SJun-ichiro itojun Hagino 4664b24e5b1SGleb Smirnoff NET_EPOCH_ASSERT(); 4674b24e5b1SGleb Smirnoff 46819dc6445SKristof Provost sc = ifp->if_softc; 46919dc6445SKristof Provost 470b46512f7SKristof Provost SDT_PROBE3(if_stf, , getsrcifa6, in, ifp, addr, mask); 471b46512f7SKristof Provost 472d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 473686cdd19SJun-ichiro itojun Hagino if (ia->ifa_addr->sa_family != AF_INET6) 474686cdd19SJun-ichiro itojun Hagino continue; 475686cdd19SJun-ichiro itojun Hagino 47619dc6445SKristof Provost addr6 = *IFA_IN6(ia); 47719dc6445SKristof Provost mask6 = *IFA_MASKIN6(ia); 47819dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) 47919dc6445SKristof Provost bcopy(&sc->srcv4_addr, &in, sizeof(in)); 48019dc6445SKristof Provost else { 48119dc6445SKristof Provost if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL) 48219dc6445SKristof Provost continue; 48319dc6445SKristof Provost bcopy(&sin4.sin_addr, &in, sizeof(in)); 48419dc6445SKristof Provost } 48519dc6445SKristof Provost 486c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash) 487686cdd19SJun-ichiro itojun Hagino if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) 488686cdd19SJun-ichiro itojun Hagino break; 489686cdd19SJun-ichiro itojun Hagino if (ia4 == NULL) 490686cdd19SJun-ichiro itojun Hagino continue; 491686cdd19SJun-ichiro itojun Hagino 49219dc6445SKristof Provost *addr = addr6; 49319dc6445SKristof Provost *mask = mask6; 494d74b9a2cSAlexander V. Chernikov 495b46512f7SKristof Provost SDT_PROBE2(if_stf, , getsrcifa6, found, addr, mask); 496b46512f7SKristof Provost 497d74b9a2cSAlexander V. Chernikov return (0); 498686cdd19SJun-ichiro itojun Hagino } 499686cdd19SJun-ichiro itojun Hagino 500b46512f7SKristof Provost SDT_PROBE0(if_stf, , getsrcifa6, notfound); 501b46512f7SKristof Provost 502d74b9a2cSAlexander V. Chernikov return (ENOENT); 503686cdd19SJun-ichiro itojun Hagino } 504686cdd19SJun-ichiro itojun Hagino 505686cdd19SJun-ichiro itojun Hagino static int 50647e8d432SGleb Smirnoff stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 50747e8d432SGleb Smirnoff struct route *ro) 508686cdd19SJun-ichiro itojun Hagino { 509686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc; 51047e8d432SGleb Smirnoff const struct sockaddr_in6 *dst6; 51119dc6445SKristof Provost struct sockaddr_in dst4, src4; 512686cdd19SJun-ichiro itojun Hagino u_int8_t tos; 513686cdd19SJun-ichiro itojun Hagino struct ip *ip; 514686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6; 515d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6; 5166b459e49SRobert Watson int error; 5176b459e49SRobert Watson 518b46512f7SKristof Provost SDT_PROBE4(if_stf, , stf_output, in, ifp, m, dst, ro); 519b46512f7SKristof Provost 5200dae32f2SDavid Malone #ifdef MAC 52130d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 5226b459e49SRobert Watson if (error) { 5236b459e49SRobert Watson m_freem(m); 524b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, error, __LINE__); 5256b459e49SRobert Watson return (error); 5266b459e49SRobert Watson } 5276b459e49SRobert Watson #endif 528686cdd19SJun-ichiro itojun Hagino 529fc74a9f9SBrooks Davis sc = ifp->if_softc; 53047e8d432SGleb Smirnoff dst6 = (const struct sockaddr_in6 *)dst; 531686cdd19SJun-ichiro itojun Hagino 532686cdd19SJun-ichiro itojun Hagino /* just in case */ 533686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_UP) == 0) { 534686cdd19SJun-ichiro itojun Hagino m_freem(m); 5353751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 536b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__); 5373576121cSKristof Provost return (ENETDOWN); 538686cdd19SJun-ichiro itojun Hagino } 539686cdd19SJun-ichiro itojun Hagino 540686cdd19SJun-ichiro itojun Hagino /* 541686cdd19SJun-ichiro itojun Hagino * If we don't have an ip4 address that match my inner ip6 address, 542686cdd19SJun-ichiro itojun Hagino * we shouldn't generate output. Without this check, we'll end up 543686cdd19SJun-ichiro itojun Hagino * using wrong IPv4 source. 544686cdd19SJun-ichiro itojun Hagino */ 545d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) { 546686cdd19SJun-ichiro itojun Hagino m_freem(m); 5473751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 548b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__); 5493576121cSKristof Provost return (ENETDOWN); 550686cdd19SJun-ichiro itojun Hagino } 551686cdd19SJun-ichiro itojun Hagino 552686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) { 553686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6)); 554f26b2d5bSHajimu UMEMOTO if (!m) { 5553751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 556b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, 557b46512f7SKristof Provost __LINE__); 5583576121cSKristof Provost return (ENOBUFS); 559686cdd19SJun-ichiro itojun Hagino } 560f26b2d5bSHajimu UMEMOTO } 561686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *); 562bb4a7d94SKristof Provost tos = IPV6_TRAFFIC_CLASS(ip6); 563686cdd19SJun-ichiro itojun Hagino 56433841545SHajimu UMEMOTO /* 56533841545SHajimu UMEMOTO * Pickup the right outer dst addr from the list of candidates. 56633841545SHajimu UMEMOTO * ip6_dst has priority as it may be able to give us shorter IPv4 hops. 56733841545SHajimu UMEMOTO */ 56819dc6445SKristof Provost if (stf_getin4addr_in6(sc, &dst4, addr6, mask6, 56919dc6445SKristof Provost ip6->ip6_dst) == NULL) { 57019dc6445SKristof Provost if (sc->braddr != INADDR_ANY) 57119dc6445SKristof Provost dst4.sin_addr.s_addr = sc->braddr; 57219dc6445SKristof Provost else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6, 57319dc6445SKristof Provost dst6->sin6_addr) == NULL) { 57433841545SHajimu UMEMOTO m_freem(m); 5753751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 576b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, 577b46512f7SKristof Provost __LINE__); 5783576121cSKristof Provost return (ENETUNREACH); 57933841545SHajimu UMEMOTO } 58019dc6445SKristof Provost } 58133841545SHajimu UMEMOTO 58216d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 5838acb2290SHajimu UMEMOTO /* 5848acb2290SHajimu UMEMOTO * We need to prepend the address family as 5858acb2290SHajimu UMEMOTO * a four byte field. Cons up a dummy header 5868acb2290SHajimu UMEMOTO * to pacify bpf. This is safe because bpf 5878acb2290SHajimu UMEMOTO * will only read from the mbuf (i.e., it won't 5888acb2290SHajimu UMEMOTO * try to free it or keep a pointer a to it). 5898acb2290SHajimu UMEMOTO */ 59047e8d432SGleb Smirnoff u_int af = AF_INET6; 591437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 5928acb2290SHajimu UMEMOTO } 5938acb2290SHajimu UMEMOTO 594eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct ip), M_NOWAIT); 595f26b2d5bSHajimu UMEMOTO if (m == NULL) { 5963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 597b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, __LINE__); 5983576121cSKristof Provost return (ENOBUFS); 599f26b2d5bSHajimu UMEMOTO } 600686cdd19SJun-ichiro itojun Hagino ip = mtod(m, struct ip *); 601686cdd19SJun-ichiro itojun Hagino 602686cdd19SJun-ichiro itojun Hagino bzero(ip, sizeof(*ip)); 603686cdd19SJun-ichiro itojun Hagino 60419dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) 60519dc6445SKristof Provost src4.sin_addr.s_addr = sc->srcv4_addr; 60619dc6445SKristof Provost else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) { 60719dc6445SKristof Provost m_freem(m); 60819dc6445SKristof Provost if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 609b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, __LINE__); 61019dc6445SKristof Provost return (ENETUNREACH); 61119dc6445SKristof Provost } 61219dc6445SKristof Provost bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src)); 61319dc6445SKristof Provost bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)); 61419dc6445SKristof Provost 615686cdd19SJun-ichiro itojun Hagino ip->ip_p = IPPROTO_IPV6; 61653dab5feSBrooks Davis ip->ip_ttl = ip_stf_ttl; 6178f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 618686cdd19SJun-ichiro itojun Hagino if (ifp->if_flags & IFF_LINK1) 619686cdd19SJun-ichiro itojun Hagino ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); 62033841545SHajimu UMEMOTO else 62133841545SHajimu UMEMOTO ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); 622686cdd19SJun-ichiro itojun Hagino 6238b07e49aSJulian Elischer M_SETFIB(m, sc->sc_fibnum); 6243751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 625d74b9a2cSAlexander V. Chernikov error = ip_output(m, NULL, NULL, 0, NULL, NULL); 6260dae32f2SDavid Malone 627b46512f7SKristof Provost SDT_PROBE1(if_stf, , stf_output, out, error); 6283576121cSKristof Provost return (error); 629686cdd19SJun-ichiro itojun Hagino } 630686cdd19SJun-ichiro itojun Hagino 631686cdd19SJun-ichiro itojun Hagino static int 632926381e1SAndrey V. Elsukov isrfc1918addr(struct in_addr *in) 633ce9d7b2fSHajimu UMEMOTO { 634ce9d7b2fSHajimu UMEMOTO /* 635ce9d7b2fSHajimu UMEMOTO * returns 1 if private address range: 636ce9d7b2fSHajimu UMEMOTO * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 637ce9d7b2fSHajimu UMEMOTO */ 63851743c5fSAndrey V. Elsukov if (stf_permit_rfc1918 == 0 && ( 63951743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 || 6408d95b0ceSSUZUKI Shinsuke (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 || 64151743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)) 6423576121cSKristof Provost return (1); 643ce9d7b2fSHajimu UMEMOTO 6443576121cSKristof Provost return (0); 645ce9d7b2fSHajimu UMEMOTO } 646ce9d7b2fSHajimu UMEMOTO 647ce9d7b2fSHajimu UMEMOTO static int 648926381e1SAndrey V. Elsukov stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp) 649686cdd19SJun-ichiro itojun Hagino { 650686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4; 651686cdd19SJun-ichiro itojun Hagino 652686cdd19SJun-ichiro itojun Hagino /* 653686cdd19SJun-ichiro itojun Hagino * reject packets with the following address: 654686cdd19SJun-ichiro itojun Hagino * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 655686cdd19SJun-ichiro itojun Hagino */ 6568d95b0ceSSUZUKI Shinsuke if (IN_MULTICAST(ntohl(in->s_addr))) 6573576121cSKristof Provost return (-1); 6588d95b0ceSSUZUKI Shinsuke switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { 6598d95b0ceSSUZUKI Shinsuke case 0: case 127: case 255: 6603576121cSKristof Provost return (-1); 6618d95b0ceSSUZUKI Shinsuke } 662686cdd19SJun-ichiro itojun Hagino 663686cdd19SJun-ichiro itojun Hagino /* 664686cdd19SJun-ichiro itojun Hagino * reject packets with broadcast 665686cdd19SJun-ichiro itojun Hagino */ 666d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) { 667686cdd19SJun-ichiro itojun Hagino if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 668686cdd19SJun-ichiro itojun Hagino continue; 6692d9cfabaSRobert Watson if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { 6703576121cSKristof Provost return (-1); 671686cdd19SJun-ichiro itojun Hagino } 6722d9cfabaSRobert Watson } 673686cdd19SJun-ichiro itojun Hagino 674686cdd19SJun-ichiro itojun Hagino /* 675686cdd19SJun-ichiro itojun Hagino * perform ingress filter 676686cdd19SJun-ichiro itojun Hagino */ 677fc74a9f9SBrooks Davis if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) { 6786ad7446cSAlexander V. Chernikov struct nhop_object *nh; 679686cdd19SJun-ichiro itojun Hagino 6806ad7446cSAlexander V. Chernikov NET_EPOCH_ASSERT(); 6816ad7446cSAlexander V. Chernikov nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0); 6826ad7446cSAlexander V. Chernikov if (nh == NULL) 6830792bcbbSAlexander V. Chernikov return (-1); 6840792bcbbSAlexander V. Chernikov 6856ad7446cSAlexander V. Chernikov if (nh->nh_ifp != inifp) 6860792bcbbSAlexander V. Chernikov return (-1); 687686cdd19SJun-ichiro itojun Hagino } 688686cdd19SJun-ichiro itojun Hagino 6893576121cSKristof Provost return (0); 690686cdd19SJun-ichiro itojun Hagino } 691686cdd19SJun-ichiro itojun Hagino 692686cdd19SJun-ichiro itojun Hagino static int 693926381e1SAndrey V. Elsukov stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp) 694686cdd19SJun-ichiro itojun Hagino { 695b46512f7SKristof Provost SDT_PROBE3(if_stf, , checkaddr6, in, sc, in6, inifp); 696b46512f7SKristof Provost 697686cdd19SJun-ichiro itojun Hagino /* 698686cdd19SJun-ichiro itojun Hagino * check 6to4 addresses 699686cdd19SJun-ichiro itojun Hagino */ 7008d95b0ceSSUZUKI Shinsuke if (IN6_IS_ADDR_6TO4(in6)) { 7018d95b0ceSSUZUKI Shinsuke struct in_addr in4; 702b46512f7SKristof Provost int ret; 703b46512f7SKristof Provost 7048d95b0ceSSUZUKI Shinsuke bcopy(GET_V4(in6), &in4, sizeof(in4)); 705b46512f7SKristof Provost ret = stf_checkaddr4(sc, &in4, inifp); 706b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, ret, __LINE__); 707b46512f7SKristof Provost return (ret); 7088d95b0ceSSUZUKI Shinsuke } 709686cdd19SJun-ichiro itojun Hagino 710686cdd19SJun-ichiro itojun Hagino /* 711686cdd19SJun-ichiro itojun Hagino * reject anything that look suspicious. the test is implemented 712686cdd19SJun-ichiro itojun Hagino * in ip6_input too, but we check here as well to 713686cdd19SJun-ichiro itojun Hagino * (1) reject bad packets earlier, and 714686cdd19SJun-ichiro itojun Hagino * (2) to be safe against future ip6_input change. 715686cdd19SJun-ichiro itojun Hagino */ 716b46512f7SKristof Provost if (IN6_IS_ADDR_V4COMPAT(in6)) { 717b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__); 7183576121cSKristof Provost return (-1); 719b46512f7SKristof Provost } 720686cdd19SJun-ichiro itojun Hagino 721b46512f7SKristof Provost if (IN6_IS_ADDR_V4MAPPED(in6)) { 722b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__); 723b46512f7SKristof Provost return (-1); 724b46512f7SKristof Provost } 725b46512f7SKristof Provost 726b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, 0, __LINE__); 7273576121cSKristof Provost return (0); 728686cdd19SJun-ichiro itojun Hagino } 729686cdd19SJun-ichiro itojun Hagino 73006cd035aSAndrey V. Elsukov static int 7316d8fdfa9SAndrey V. Elsukov in_stf_input(struct mbuf *m, int off, int proto, void *arg) 732686cdd19SJun-ichiro itojun Hagino { 7336d8fdfa9SAndrey V. Elsukov struct stf_softc *sc = arg; 734439da7f0SKristof Provost struct ip ip; 735686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6; 736686cdd19SJun-ichiro itojun Hagino u_int8_t otos, itos; 737686cdd19SJun-ichiro itojun Hagino struct ifnet *ifp; 73819dc6445SKristof Provost struct nhop_object *nh; 739686cdd19SJun-ichiro itojun Hagino 740b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 741b8a6e03fSGleb Smirnoff 742b46512f7SKristof Provost SDT_PROBE3(if_stf, , stf_input, in, m, off, proto); 743b46512f7SKristof Provost 744686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6) { 745686cdd19SJun-ichiro itojun Hagino m_freem(m); 746b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7478f5a8818SKevin Lo return (IPPROTO_DONE); 748686cdd19SJun-ichiro itojun Hagino } 749686cdd19SJun-ichiro itojun Hagino 750439da7f0SKristof Provost m_copydata(m, 0, sizeof(struct ip), (caddr_t)&ip); 751fc74a9f9SBrooks Davis if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) { 752686cdd19SJun-ichiro itojun Hagino m_freem(m); 753b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7548f5a8818SKevin Lo return (IPPROTO_DONE); 755686cdd19SJun-ichiro itojun Hagino } 756686cdd19SJun-ichiro itojun Hagino 757fc74a9f9SBrooks Davis ifp = STF2IFP(sc); 758686cdd19SJun-ichiro itojun Hagino 7596b459e49SRobert Watson #ifdef MAC 76030d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 7616b459e49SRobert Watson #endif 7626b459e49SRobert Watson 763686cdd19SJun-ichiro itojun Hagino /* 764686cdd19SJun-ichiro itojun Hagino * perform sanity check against outer src/dst. 765686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well. 766686cdd19SJun-ichiro itojun Hagino */ 767439da7f0SKristof Provost if (stf_checkaddr4(sc, &ip.ip_dst, NULL) < 0 || 768439da7f0SKristof Provost stf_checkaddr4(sc, &ip.ip_src, m->m_pkthdr.rcvif) < 0) { 769686cdd19SJun-ichiro itojun Hagino m_freem(m); 770b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7718f5a8818SKevin Lo return (IPPROTO_DONE); 772686cdd19SJun-ichiro itojun Hagino } 773686cdd19SJun-ichiro itojun Hagino 774439da7f0SKristof Provost otos = ip.ip_tos; 775686cdd19SJun-ichiro itojun Hagino m_adj(m, off); 776686cdd19SJun-ichiro itojun Hagino 777686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) { 778686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6)); 779b46512f7SKristof Provost if (!m) { 780b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, 781b46512f7SKristof Provost __LINE__); 7828f5a8818SKevin Lo return (IPPROTO_DONE); 783686cdd19SJun-ichiro itojun Hagino } 784b46512f7SKristof Provost } 785686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *); 786686cdd19SJun-ichiro itojun Hagino 787686cdd19SJun-ichiro itojun Hagino /* 788686cdd19SJun-ichiro itojun Hagino * perform sanity check against inner src/dst. 789686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well. 790686cdd19SJun-ichiro itojun Hagino */ 79133841545SHajimu UMEMOTO if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 || 79233841545SHajimu UMEMOTO stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) { 793686cdd19SJun-ichiro itojun Hagino m_freem(m); 794b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 7958f5a8818SKevin Lo return (IPPROTO_DONE); 796686cdd19SJun-ichiro itojun Hagino } 797686cdd19SJun-ichiro itojun Hagino 79819dc6445SKristof Provost /* 79919dc6445SKristof Provost * reject packets with private address range. 80019dc6445SKristof Provost * (requirement from RFC3056 section 2 1st paragraph) 80119dc6445SKristof Provost */ 802439da7f0SKristof Provost if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip.ip_src)) || 803439da7f0SKristof Provost (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip.ip_dst))) { 80419dc6445SKristof Provost m_freem(m); 805b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 80619dc6445SKristof Provost return (IPPROTO_DONE); 80719dc6445SKristof Provost } 80819dc6445SKristof Provost 80919dc6445SKristof Provost /* 81019dc6445SKristof Provost * Ignore if the destination is the same stf interface because 81119dc6445SKristof Provost * all of valid IPv6 outgoing traffic should go interfaces 81219dc6445SKristof Provost * except for it. 81319dc6445SKristof Provost */ 81419dc6445SKristof Provost nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0); 81519dc6445SKristof Provost if (nh == NULL) { 81619dc6445SKristof Provost m_free(m); 817b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 81819dc6445SKristof Provost return (IPPROTO_DONE); 81919dc6445SKristof Provost } 82019dc6445SKristof Provost if ((nh->nh_ifp == ifp) && 82119dc6445SKristof Provost (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) { 82219dc6445SKristof Provost m_free(m); 823b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 82419dc6445SKristof Provost return (IPPROTO_DONE); 82519dc6445SKristof Provost } 82619dc6445SKristof Provost 827bb4a7d94SKristof Provost itos = IPV6_TRAFFIC_CLASS(ip6); 828686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_LINK1) != 0) 829686cdd19SJun-ichiro itojun Hagino ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 83033841545SHajimu UMEMOTO else 83133841545SHajimu UMEMOTO ip_ecn_egress(ECN_NOCARE, &otos, &itos); 832686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow &= ~htonl(0xff << 20); 833686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 834686cdd19SJun-ichiro itojun Hagino 835686cdd19SJun-ichiro itojun Hagino m->m_pkthdr.rcvif = ifp; 836686cdd19SJun-ichiro itojun Hagino 83716d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 838686cdd19SJun-ichiro itojun Hagino /* 839686cdd19SJun-ichiro itojun Hagino * We need to prepend the address family as 840686cdd19SJun-ichiro itojun Hagino * a four byte field. Cons up a dummy header 841686cdd19SJun-ichiro itojun Hagino * to pacify bpf. This is safe because bpf 842686cdd19SJun-ichiro itojun Hagino * will only read from the mbuf (i.e., it won't 843686cdd19SJun-ichiro itojun Hagino * try to free it or keep a pointer a to it). 844686cdd19SJun-ichiro itojun Hagino */ 84533841545SHajimu UMEMOTO u_int32_t af = AF_INET6; 8469b1a7076SHajimu UMEMOTO bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 847686cdd19SJun-ichiro itojun Hagino } 848686cdd19SJun-ichiro itojun Hagino 849686cdd19SJun-ichiro itojun Hagino /* 850686cdd19SJun-ichiro itojun Hagino * Put the packet to the network layer input queue according to the 851686cdd19SJun-ichiro itojun Hagino * specified address family. 852686cdd19SJun-ichiro itojun Hagino * See net/if_gif.c for possible issues with packet processing 853686cdd19SJun-ichiro itojun Hagino * reorder due to extra queueing. 854686cdd19SJun-ichiro itojun Hagino */ 8553751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 8563751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 857a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 8581cafed39SJonathan Lemon netisr_dispatch(NETISR_IPV6, m); 859b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__); 8608f5a8818SKevin Lo return (IPPROTO_DONE); 861686cdd19SJun-ichiro itojun Hagino } 862686cdd19SJun-ichiro itojun Hagino 86319dc6445SKristof Provost static struct sockaddr_in * 86419dc6445SKristof Provost stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin, 86519dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6) 86619dc6445SKristof Provost { 86719dc6445SKristof Provost int i; 868b46512f7SKristof Provost struct sockaddr_in *out; 86919dc6445SKristof Provost 87019dc6445SKristof Provost /* 87119dc6445SKristof Provost * When (src addr & src mask) != (in6 & src mask), 87219dc6445SKristof Provost * the dst is not in the 6rd domain. The IPv4 address must 87319dc6445SKristof Provost * not be used. 87419dc6445SKristof Provost */ 87519dc6445SKristof Provost for (i = 0; i < sizeof(addr6); i++) { 87619dc6445SKristof Provost if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) != 877b46512f7SKristof Provost (((u_char *)&in6)[i] & ((u_char *)&mask6)[i])) { 878b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, 879b46512f7SKristof Provost &mask6, &in6, NULL); 88019dc6445SKristof Provost return (NULL); 88119dc6445SKristof Provost } 882b46512f7SKristof Provost } 88319dc6445SKristof Provost 88419dc6445SKristof Provost /* After the mask check, use in6 instead of addr6. */ 885b46512f7SKristof Provost out = stf_getin4addr(sc, sin, in6, mask6); 886b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, &mask6, &in6, out); 887b46512f7SKristof Provost return (out); 88819dc6445SKristof Provost } 88919dc6445SKristof Provost 89019dc6445SKristof Provost static struct sockaddr_in * 89119dc6445SKristof Provost stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin, 89219dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6) 89319dc6445SKristof Provost { 89419dc6445SKristof Provost struct in_addr *in; 89519dc6445SKristof Provost 896b46512f7SKristof Provost SDT_PROBE2(if_stf, , getin4addr, in, &addr6, &mask6); 897b46512f7SKristof Provost 89819dc6445SKristof Provost memset(sin, 0, sizeof(*sin)); 89919dc6445SKristof Provost in = &sin->sin_addr; 90019dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) { 90119dc6445SKristof Provost /* 6to4 (RFC 3056) */ 90219dc6445SKristof Provost bcopy(GET_V4(&addr6), in, sizeof(*in)); 90319dc6445SKristof Provost if (isrfc1918addr(in)) 90419dc6445SKristof Provost return (NULL); 90519dc6445SKristof Provost } else { 90619dc6445SKristof Provost /* 6rd (RFC 5569) */ 90719dc6445SKristof Provost in_addr_t v4prefix; 90819dc6445SKristof Provost uint8_t *v6 = (uint8_t*)&addr6; 90919dc6445SKristof Provost uint64_t v6prefix; 91019dc6445SKristof Provost u_int plen; 91119dc6445SKristof Provost u_int v4suffixlen; 91219dc6445SKristof Provost 91319dc6445SKristof Provost v4prefix = 0; 91419dc6445SKristof Provost if (sc->v4prefixlen < 32) { 91519dc6445SKristof Provost v4suffixlen = 32 - sc->v4prefixlen; 91619dc6445SKristof Provost v4prefix = ntohl(sc->srcv4_addr) & 91719dc6445SKristof Provost (0xffffffffU << v4suffixlen); 91819dc6445SKristof Provost } else { 91919dc6445SKristof Provost MPASS(sc->v4prefixlen == 32); 92019dc6445SKristof Provost v4suffixlen = 32; 92119dc6445SKristof Provost } 92219dc6445SKristof Provost 92319dc6445SKristof Provost plen = in6_mask2len(&mask6, NULL); 92419dc6445SKristof Provost if (plen > 64) 92519dc6445SKristof Provost return (NULL); 92619dc6445SKristof Provost 92719dc6445SKristof Provost /* To make this simple we do not support prefixes longer than 92819dc6445SKristof Provost * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64 92919dc6445SKristof Provost * or shorter." so this is a moderately safe assumption. */ 93019dc6445SKristof Provost v6prefix = be64toh(*(uint64_t *)v6); 93119dc6445SKristof Provost 93219dc6445SKristof Provost /* Shift away the v6 prefix itself. */ 93319dc6445SKristof Provost v6prefix <<= plen; 93419dc6445SKristof Provost v6prefix >>= plen; 93519dc6445SKristof Provost 93619dc6445SKristof Provost /* Now shift away everything after the v4 address. */ 93719dc6445SKristof Provost v6prefix >>= 64 - plen - v4suffixlen; 93819dc6445SKristof Provost 93919dc6445SKristof Provost sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix); 94019dc6445SKristof Provost } 94119dc6445SKristof Provost 942b46512f7SKristof Provost SDT_PROBE1(if_stf, , getin4addr, out, sin); 943b46512f7SKristof Provost 94419dc6445SKristof Provost return (sin); 94519dc6445SKristof Provost } 94619dc6445SKristof Provost 947686cdd19SJun-ichiro itojun Hagino static int 948926381e1SAndrey V. Elsukov stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 949686cdd19SJun-ichiro itojun Hagino { 950686cdd19SJun-ichiro itojun Hagino struct ifaddr *ifa; 95119dc6445SKristof Provost struct ifdrv *ifd; 952686cdd19SJun-ichiro itojun Hagino struct ifreq *ifr; 95319dc6445SKristof Provost struct sockaddr_in sin4; 95419dc6445SKristof Provost struct stf_softc *sc_cur; 95519dc6445SKristof Provost struct stfv4args args; 95699ab4b12SAlexander V. Chernikov int error, mtu; 957686cdd19SJun-ichiro itojun Hagino 958686cdd19SJun-ichiro itojun Hagino error = 0; 95919dc6445SKristof Provost sc_cur = ifp->if_softc; 96019dc6445SKristof Provost 961686cdd19SJun-ichiro itojun Hagino switch (cmd) { 96219dc6445SKristof Provost case SIOCSDRVSPEC: 96319dc6445SKristof Provost ifd = (struct ifdrv *)data; 96419dc6445SKristof Provost error = priv_check(curthread, PRIV_NET_ADDIFADDR); 96519dc6445SKristof Provost if (error) 96619dc6445SKristof Provost break; 96719dc6445SKristof Provost if (ifd->ifd_cmd == STF6RD_SV4NET) { 96819dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 96919dc6445SKristof Provost error = EINVAL; 97019dc6445SKristof Provost break; 97119dc6445SKristof Provost } 97219dc6445SKristof Provost bzero(&args, sizeof(args)); 97319dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 97419dc6445SKristof Provost if (error) 97519dc6445SKristof Provost break; 97619dc6445SKristof Provost 97719dc6445SKristof Provost if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) { 97819dc6445SKristof Provost error = EINVAL; 97919dc6445SKristof Provost break; 98019dc6445SKristof Provost } 98119dc6445SKristof Provost 98219dc6445SKristof Provost bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr, 98319dc6445SKristof Provost sizeof(sc_cur->srcv4_addr)); 98419dc6445SKristof Provost sc_cur->v4prefixlen = args.v4_prefixlen; 985b46512f7SKristof Provost SDT_PROBE3(if_stf, , ioctl, sv4net, sc_cur->srcv4_addr, 986b46512f7SKristof Provost sc_cur->srcv4_addr, sc_cur->v4prefixlen); 98719dc6445SKristof Provost } else if (ifd->ifd_cmd == STF6RD_SBR) { 98819dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 98919dc6445SKristof Provost error = EINVAL; 99019dc6445SKristof Provost break; 99119dc6445SKristof Provost } 99219dc6445SKristof Provost bzero(&args, sizeof(args)); 99319dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len); 99419dc6445SKristof Provost if (error) 99519dc6445SKristof Provost break; 99619dc6445SKristof Provost sc_cur->braddr = args.braddr.s_addr; 997b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, sdstv4, 998b46512f7SKristof Provost sc_cur->braddr); 99919dc6445SKristof Provost } else 100019dc6445SKristof Provost error = EINVAL; 100119dc6445SKristof Provost break; 100219dc6445SKristof Provost case SIOCGDRVSPEC: 100319dc6445SKristof Provost ifd = (struct ifdrv *)data; 100419dc6445SKristof Provost if (ifd->ifd_cmd != STF6RD_GV4NET) { 100519dc6445SKristof Provost error = EINVAL; 100619dc6445SKristof Provost break; 100719dc6445SKristof Provost } 100819dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) { 100919dc6445SKristof Provost error = EINVAL; 101019dc6445SKristof Provost break; 101119dc6445SKristof Provost } 101219dc6445SKristof Provost bzero(&args, sizeof(args)); 101319dc6445SKristof Provost args.srcv4_addr.s_addr = sc_cur->srcv4_addr; 101419dc6445SKristof Provost args.braddr.s_addr = sc_cur->braddr; 101519dc6445SKristof Provost args.v4_prefixlen = sc_cur->v4prefixlen; 101619dc6445SKristof Provost error = copyout(&args, ifd->ifd_data, ifd->ifd_len); 101719dc6445SKristof Provost break; 1018686cdd19SJun-ichiro itojun Hagino case SIOCSIFADDR: 1019686cdd19SJun-ichiro itojun Hagino ifa = (struct ifaddr *)data; 1020b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, ifaddr, ifa); 1021686cdd19SJun-ichiro itojun Hagino if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) { 1022686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT; 1023686cdd19SJun-ichiro itojun Hagino break; 1024686cdd19SJun-ichiro itojun Hagino } 102519dc6445SKristof Provost if (stf_getin4addr(sc_cur, &sin4, 102619dc6445SKristof Provost satosin6(ifa->ifa_addr)->sin6_addr, 102719dc6445SKristof Provost satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) { 10288d95b0ceSSUZUKI Shinsuke error = EINVAL; 10298d95b0ceSSUZUKI Shinsuke break; 10308d95b0ceSSUZUKI Shinsuke } 1031686cdd19SJun-ichiro itojun Hagino ifp->if_flags |= IFF_UP; 1032ca1163bdSMark Johnston ifp->if_drv_flags |= IFF_DRV_RUNNING; 1033686cdd19SJun-ichiro itojun Hagino break; 1034686cdd19SJun-ichiro itojun Hagino 1035686cdd19SJun-ichiro itojun Hagino case SIOCADDMULTI: 1036686cdd19SJun-ichiro itojun Hagino case SIOCDELMULTI: 1037686cdd19SJun-ichiro itojun Hagino ifr = (struct ifreq *)data; 1038686cdd19SJun-ichiro itojun Hagino if (ifr && ifr->ifr_addr.sa_family == AF_INET6) 1039686cdd19SJun-ichiro itojun Hagino ; 1040686cdd19SJun-ichiro itojun Hagino else 1041686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT; 1042686cdd19SJun-ichiro itojun Hagino break; 1043686cdd19SJun-ichiro itojun Hagino 104499ab4b12SAlexander V. Chernikov case SIOCGIFMTU: 104599ab4b12SAlexander V. Chernikov break; 104699ab4b12SAlexander V. Chernikov 104799ab4b12SAlexander V. Chernikov case SIOCSIFMTU: 104899ab4b12SAlexander V. Chernikov ifr = (struct ifreq *)data; 104999ab4b12SAlexander V. Chernikov mtu = ifr->ifr_mtu; 105099ab4b12SAlexander V. Chernikov /* RFC 4213 3.2 ideal world MTU */ 105199ab4b12SAlexander V. Chernikov if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20) 105299ab4b12SAlexander V. Chernikov return (EINVAL); 105399ab4b12SAlexander V. Chernikov ifp->if_mtu = mtu; 105499ab4b12SAlexander V. Chernikov break; 105599ab4b12SAlexander V. Chernikov 1056686cdd19SJun-ichiro itojun Hagino default: 1057686cdd19SJun-ichiro itojun Hagino error = EINVAL; 1058686cdd19SJun-ichiro itojun Hagino break; 1059686cdd19SJun-ichiro itojun Hagino } 1060686cdd19SJun-ichiro itojun Hagino 10613576121cSKristof Provost return (error); 1062686cdd19SJun-ichiro itojun Hagino } 1063