162587Sitojun/* $FreeBSD: stable/11/sys/net/if_stf.c 340951 2018-11-26 11:17:12Z eugen $ */ 295023Ssuz/* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */ 362587Sitojun 4139823Simp/*- 562587Sitojun * Copyright (C) 2000 WIDE Project. 662587Sitojun * All rights reserved. 762587Sitojun * 862587Sitojun * Redistribution and use in source and binary forms, with or without 962587Sitojun * modification, are permitted provided that the following conditions 1062587Sitojun * are met: 1162587Sitojun * 1. Redistributions of source code must retain the above copyright 1262587Sitojun * notice, this list of conditions and the following disclaimer. 1362587Sitojun * 2. Redistributions in binary form must reproduce the above copyright 1462587Sitojun * notice, this list of conditions and the following disclaimer in the 1562587Sitojun * documentation and/or other materials provided with the distribution. 1662587Sitojun * 3. Neither the name of the project nor the names of its contributors 1762587Sitojun * may be used to endorse or promote products derived from this software 1862587Sitojun * without specific prior written permission. 1962587Sitojun * 2062587Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2162587Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2262587Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2362587Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2462587Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2562587Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2662587Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2762587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2862587Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2962587Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3062587Sitojun * SUCH DAMAGE. 3162587Sitojun */ 3262587Sitojun 3362587Sitojun/* 3478064Sume * 6to4 interface, based on RFC3056. 3562587Sitojun * 3662587Sitojun * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting. 3762587Sitojun * There is no address mapping defined from IPv6 multicast address to IPv4 3862587Sitojun * address. Therefore, we do not have IFF_MULTICAST on the interface. 3962587Sitojun * 4062587Sitojun * Due to the lack of address mapping for link-local addresses, we cannot 4162587Sitojun * throw packets toward link-local addresses (fe80::x). Also, we cannot throw 4262587Sitojun * packets to link-local multicast addresses (ff02::x). 4362587Sitojun * 4462587Sitojun * Here are interesting symptoms due to the lack of link-local address: 4562587Sitojun * 4662587Sitojun * Unicast routing exchange: 4762587Sitojun * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9, 4862587Sitojun * and link-local addresses as nexthop. 4962587Sitojun * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address 5062587Sitojun * assigned to the link, and makes use of them. Also, HELLO packets use 5162587Sitojun * link-local multicast addresses (ff02::5 and ff02::6). 5262587Sitojun * - BGP4+: Maybe. You can only use global address as nexthop, and global 5362587Sitojun * address as TCP endpoint address. 5462587Sitojun * 5562587Sitojun * Multicast routing protocols: 5662587Sitojun * - PIM: Hello packet cannot be used to discover adjacent PIM routers. 5762587Sitojun * Adjacent PIM routers must be configured manually (is it really spec-wise 5862587Sitojun * correct thing to do?). 5962587Sitojun * 6062587Sitojun * ICMPv6: 6162587Sitojun * - Redirects cannot be used due to the lack of link-local address. 6262587Sitojun * 6378064Sume * stf interface does not have, and will not need, a link-local address. 6478064Sume * It seems to have no real benefit and does not help the above symptoms much. 6578064Sume * Even if we assign link-locals to interface, we cannot really 6678064Sume * use link-local unicast/multicast on top of 6to4 cloud (since there's no 6778064Sume * encapsulation defined for link-local address), and the above analysis does 6878064Sume * not change. RFC3056 does not mandate the assignment of link-local address 6978064Sume * either. 7062587Sitojun * 7162587Sitojun * 6to4 interface has security issues. Refer to 7262587Sitojun * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt 7362587Sitojun * for details. The code tries to filter out some of malicious packets. 7462587Sitojun * Note that there is no way to be 100% secure. 7562587Sitojun */ 7662587Sitojun 7762587Sitojun#include <sys/param.h> 7862587Sitojun#include <sys/systm.h> 7962587Sitojun#include <sys/socket.h> 8062587Sitojun#include <sys/sockio.h> 8162587Sitojun#include <sys/mbuf.h> 8262587Sitojun#include <sys/errno.h> 8383655Sbrooks#include <sys/kernel.h> 84286001Sae#include <sys/lock.h> 85129880Sphk#include <sys/module.h> 8662587Sitojun#include <sys/protosw.h> 87178888Sjulian#include <sys/proc.h> 8883655Sbrooks#include <sys/queue.h> 89286001Sae#include <sys/rmlock.h> 90183351Sdwmalone#include <sys/sysctl.h> 9162587Sitojun#include <machine/cpu.h> 9262587Sitojun 9362587Sitojun#include <sys/malloc.h> 9462587Sitojun 9562587Sitojun#include <net/if.h> 96257176Sglebius#include <net/if_var.h> 97130933Sbrooks#include <net/if_clone.h> 9862587Sitojun#include <net/route.h> 9962587Sitojun#include <net/netisr.h> 10062587Sitojun#include <net/if_types.h> 101196019Srwatson#include <net/vnet.h> 10262587Sitojun 10362587Sitojun#include <netinet/in.h> 104292331Smelifaro#include <netinet/in_fib.h> 10562587Sitojun#include <netinet/in_systm.h> 10662587Sitojun#include <netinet/ip.h> 10762587Sitojun#include <netinet/ip_var.h> 10862587Sitojun#include <netinet/in_var.h> 10962587Sitojun 11062587Sitojun#include <netinet/ip6.h> 11162587Sitojun#include <netinet6/ip6_var.h> 11262587Sitojun#include <netinet6/in6_var.h> 11362587Sitojun#include <netinet/ip_ecn.h> 11462587Sitojun 11562587Sitojun#include <netinet/ip_encap.h> 11662587Sitojun 11762587Sitojun#include <machine/stdarg.h> 11862587Sitojun 11962587Sitojun#include <net/bpf.h> 12062587Sitojun 121163606Srwatson#include <security/mac/mac_framework.h> 122163606Srwatson 123183351SdwmaloneSYSCTL_DECL(_net_link); 124227309Sedstatic SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW, 0, "6to4 Interface"); 125183351Sdwmalone 126244750Saestatic int stf_permit_rfc1918 = 0; 127267992ShselaskySYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN, 128244750Sae &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses"); 129244750Sae 130130933Sbrooks#define STFUNIT 0 13183655Sbrooks 13262587Sitojun#define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002) 13362587Sitojun 134109322Ssuz/* 135109322Ssuz * XXX: Return a pointer with 16-bit aligned. Don't cast it to 136109322Ssuz * struct in_addr *; use bcopy() instead. 137109322Ssuz */ 138249925Sglebius#define GET_V4(x) (&(x)->s6_addr16[1]) 139109322Ssuz 14062587Sitojunstruct stf_softc { 141147256Sbrooks struct ifnet *sc_ifp; 142178888Sjulian u_int sc_fibnum; 14362587Sitojun const struct encaptab *encap_cookie; 14462587Sitojun}; 145147256Sbrooks#define STF2IFP(sc) ((sc)->sc_ifp) 14662587Sitojun 147241610Sglebiusstatic const char stfname[] = "stf"; 148241610Sglebius 149241610Sglebiusstatic MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface"); 150126709Srwatsonstatic const int ip_stf_ttl = 40; 15162587Sitojun 15279106Sbrooksextern struct domain inetdomain; 153276152Saestatic int in_stf_input(struct mbuf **, int *, int); 154276152Saestatic struct protosw in_stf_protosw = { 155152242Sru .pr_type = SOCK_RAW, 156152242Sru .pr_domain = &inetdomain, 157152242Sru .pr_protocol = IPPROTO_IPV6, 158152242Sru .pr_flags = PR_ATOMIC|PR_ADDR, 159152242Sru .pr_input = in_stf_input, 160270008Skevlo .pr_output = rip_output, 161152242Sru .pr_ctloutput = rip_ctloutput, 162152242Sru .pr_usrreqs = &rip_usrreqs 16379106Sbrooks}; 16462587Sitojun 165130933Sbrooksstatic char *stfnames[] = {"stf0", "stf", "6to4", NULL}; 166130933Sbrooks 16792725Salfredstatic int stfmodevent(module_t, int, void *); 16892725Salfredstatic int stf_encapcheck(const struct mbuf *, int, int, void *); 169273220Smelifarostatic int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *); 170249925Sglebiusstatic int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *, 171191148Skmacy struct route *); 172103475Sumestatic int isrfc1918addr(struct in_addr *); 17392725Salfredstatic int stf_checkaddr4(struct stf_softc *, struct in_addr *, 17492725Salfred struct ifnet *); 17592725Salfredstatic int stf_checkaddr6(struct stf_softc *, struct in6_addr *, 17692725Salfred struct ifnet *); 17792725Salfredstatic int stf_ioctl(struct ifnet *, u_long, caddr_t); 17862587Sitojun 179130933Sbrooksstatic int stf_clone_match(struct if_clone *, const char *); 180160195Ssamstatic int stf_clone_create(struct if_clone *, char *, size_t, caddr_t); 181130933Sbrooksstatic int stf_clone_destroy(struct if_clone *, struct ifnet *); 182241610Sglebiusstatic struct if_clone *stf_cloner; 18383655Sbrooks 184130933Sbrooksstatic int 185130933Sbrooksstf_clone_match(struct if_clone *ifc, const char *name) 186130933Sbrooks{ 187130933Sbrooks int i; 18883655Sbrooks 189130933Sbrooks for(i = 0; stfnames[i] != NULL; i++) { 190130933Sbrooks if (strcmp(stfnames[i], name) == 0) 191130933Sbrooks return (1); 192130933Sbrooks } 193130933Sbrooks 194130933Sbrooks return (0); 195130933Sbrooks} 196130933Sbrooks 197128209Sbrooksstatic int 198160195Ssamstf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 19983655Sbrooks{ 200337855Sloos char *dp; 201337855Sloos int err, unit, wildcard; 20283655Sbrooks struct stf_softc *sc; 203128417Sbrooks struct ifnet *ifp; 20483655Sbrooks 205337855Sloos err = ifc_name2unit(name, &unit); 206337855Sloos if (err != 0) 207337855Sloos return (err); 208337855Sloos wildcard = (unit < 0); 209337855Sloos 210130933Sbrooks /* 211130933Sbrooks * We can only have one unit, but since unit allocation is 212130933Sbrooks * already locked, we use it to keep from allocating extra 213130933Sbrooks * interfaces. 214130933Sbrooks */ 215130933Sbrooks unit = STFUNIT; 216130933Sbrooks err = ifc_alloc_unit(ifc, &unit); 217130933Sbrooks if (err != 0) 218130933Sbrooks return (err); 219130933Sbrooks 220111119Simp sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO); 221147346Sbrooks ifp = STF2IFP(sc) = if_alloc(IFT_STF); 222147256Sbrooks if (ifp == NULL) { 223147256Sbrooks free(sc, M_STF); 224147256Sbrooks ifc_free_unit(ifc, unit); 225147256Sbrooks return (ENOSPC); 226147256Sbrooks } 227147346Sbrooks ifp->if_softc = sc; 228178888Sjulian sc->sc_fibnum = curthread->td_proc->p_fibnum; 229147346Sbrooks 230130933Sbrooks /* 231130933Sbrooks * Set the name manually rather then using if_initname because 232130933Sbrooks * we don't conform to the default naming convention for interfaces. 233337855Sloos * In the wildcard case, we need to update the name. 234130933Sbrooks */ 235337855Sloos if (wildcard) { 236337855Sloos for (dp = name; *dp != '\0'; dp++); 237337855Sloos if (snprintf(dp, len - (dp-name), "%d", unit) > 238337855Sloos len - (dp-name) - 1) { 239337855Sloos /* 240337855Sloos * This can only be a programmer error and 241337855Sloos * there's no straightforward way to recover if 242337855Sloos * it happens. 243337855Sloos */ 244337855Sloos panic("if_clone_create(): interface name too long"); 245337855Sloos } 246337855Sloos } 247130933Sbrooks strlcpy(ifp->if_xname, name, IFNAMSIZ); 248241610Sglebius ifp->if_dname = stfname; 249130933Sbrooks ifp->if_dunit = IF_DUNIT_NONE; 25083655Sbrooks 25183655Sbrooks sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6, 25283655Sbrooks stf_encapcheck, &in_stf_protosw, sc); 25383655Sbrooks if (sc->encap_cookie == NULL) { 254128417Sbrooks if_printf(ifp, "attach failed\n"); 25583655Sbrooks free(sc, M_STF); 256130933Sbrooks ifc_free_unit(ifc, unit); 25783655Sbrooks return (ENOMEM); 25883655Sbrooks } 25983655Sbrooks 260128417Sbrooks ifp->if_mtu = IPV6_MMTU; 261128417Sbrooks ifp->if_ioctl = stf_ioctl; 262128417Sbrooks ifp->if_output = stf_output; 263207554Ssobomax ifp->if_snd.ifq_maxlen = ifqmaxlen; 264128417Sbrooks if_attach(ifp); 265147611Sdwmalone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t)); 26683655Sbrooks return (0); 26783655Sbrooks} 26883655Sbrooks 269130933Sbrooksstatic int 270130933Sbrooksstf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 27183655Sbrooks{ 272147256Sbrooks struct stf_softc *sc = ifp->if_softc; 273151266Sthompsa int err; 27483655Sbrooks 275151266Sthompsa err = encap_detach(sc->encap_cookie); 276151266Sthompsa KASSERT(err == 0, ("Unexpected error detaching encap_cookie")); 277151266Sthompsa bpfdetach(ifp); 278151266Sthompsa if_detach(ifp); 279151266Sthompsa if_free(ifp); 280151266Sthompsa 281151266Sthompsa free(sc, M_STF); 282130933Sbrooks ifc_free_unit(ifc, STFUNIT); 283130933Sbrooks 284130933Sbrooks return (0); 28583655Sbrooks} 28683655Sbrooks 28779106Sbrooksstatic int 288286114Saestfmodevent(module_t mod, int type, void *data) 28962587Sitojun{ 29062587Sitojun 29179106Sbrooks switch (type) { 29279106Sbrooks case MOD_LOAD: 293241610Sglebius stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match, 294241610Sglebius stf_clone_create, stf_clone_destroy); 29579106Sbrooks break; 29679106Sbrooks case MOD_UNLOAD: 297241610Sglebius if_clone_detach(stf_cloner); 29879106Sbrooks break; 299132199Sphk default: 300132199Sphk return (EOPNOTSUPP); 30162587Sitojun } 30279106Sbrooks 30379106Sbrooks return (0); 30462587Sitojun} 30562587Sitojun 30679106Sbrooksstatic moduledata_t stf_mod = { 30779106Sbrooks "if_stf", 30879106Sbrooks stfmodevent, 309241394Skevlo 0 31079106Sbrooks}; 31162587Sitojun 31279106SbrooksDECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 31379106Sbrooks 31462587Sitojunstatic int 315286114Saestf_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 31662587Sitojun{ 31762587Sitojun struct ip ip; 31862587Sitojun struct stf_softc *sc; 319108710Sfenner struct in_addr a, b, mask; 320273220Smelifaro struct in6_addr addr6, mask6; 32162587Sitojun 32262587Sitojun sc = (struct stf_softc *)arg; 32362587Sitojun if (sc == NULL) 32462587Sitojun return 0; 32562587Sitojun 326147256Sbrooks if ((STF2IFP(sc)->if_flags & IFF_UP) == 0) 32762587Sitojun return 0; 32862587Sitojun 32978064Sume /* IFF_LINK0 means "no decapsulation" */ 330147256Sbrooks if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0) 33178064Sume return 0; 33278064Sume 33362587Sitojun if (proto != IPPROTO_IPV6) 33462587Sitojun return 0; 33562587Sitojun 33662587Sitojun /* LINTED const cast */ 33791452Speter m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip); 33862587Sitojun 33962587Sitojun if (ip.ip_v != 4) 34062587Sitojun return 0; 34162587Sitojun 342273220Smelifaro if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0) 343273220Smelifaro return (0); 34462587Sitojun 34562587Sitojun /* 34662587Sitojun * check if IPv4 dst matches the IPv4 address derived from the 34762587Sitojun * local 6to4 address. 34862587Sitojun * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:... 34962587Sitojun */ 350273220Smelifaro if (bcmp(GET_V4(&addr6), &ip.ip_dst, sizeof(ip.ip_dst)) != 0) 35162587Sitojun return 0; 35262587Sitojun 35362587Sitojun /* 35462587Sitojun * check if IPv4 src matches the IPv4 address derived from the 35562587Sitojun * local 6to4 address masked by prefixmask. 35662587Sitojun * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24 35762587Sitojun * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24 35862587Sitojun */ 35962587Sitojun bzero(&a, sizeof(a)); 360273220Smelifaro bcopy(GET_V4(&addr6), &a, sizeof(a)); 361273220Smelifaro bcopy(GET_V4(&mask6), &mask, sizeof(mask)); 362108710Sfenner a.s_addr &= mask.s_addr; 36362587Sitojun b = ip.ip_src; 364108710Sfenner b.s_addr &= mask.s_addr; 36562587Sitojun if (a.s_addr != b.s_addr) 36662587Sitojun return 0; 36762587Sitojun 36862587Sitojun /* stf interface makes single side match only */ 36962587Sitojun return 32; 37062587Sitojun} 37162587Sitojun 372273220Smelifarostatic int 373273220Smelifarostf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask) 37462587Sitojun{ 375340951Seugen struct rm_priotracker in_ifa_tracker; 37662587Sitojun struct ifaddr *ia; 37762587Sitojun struct in_ifaddr *ia4; 378273220Smelifaro struct in6_ifaddr *ia6; 37962587Sitojun struct sockaddr_in6 *sin6; 38062587Sitojun struct in_addr in; 38162587Sitojun 382195022Srwatson if_addr_rlock(ifp); 383191339Srwatson TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 38462587Sitojun if (ia->ifa_addr->sa_family != AF_INET6) 38562587Sitojun continue; 38662587Sitojun sin6 = (struct sockaddr_in6 *)ia->ifa_addr; 38762587Sitojun if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) 38862587Sitojun continue; 38962587Sitojun 39062587Sitojun bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in)); 391340951Seugen IN_IFADDR_RLOCK(&in_ifa_tracker); 39284104Sjlemon LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash) 39362587Sitojun if (ia4->ia_addr.sin_addr.s_addr == in.s_addr) 39462587Sitojun break; 395340951Seugen IN_IFADDR_RUNLOCK(&in_ifa_tracker); 39662587Sitojun if (ia4 == NULL) 39762587Sitojun continue; 39862587Sitojun 399273220Smelifaro ia6 = (struct in6_ifaddr *)ia; 400273220Smelifaro 401273220Smelifaro *addr = sin6->sin6_addr; 402273220Smelifaro *mask = ia6->ia_prefixmask.sin6_addr; 403195022Srwatson if_addr_runlock(ifp); 404273220Smelifaro return (0); 40562587Sitojun } 406195022Srwatson if_addr_runlock(ifp); 40762587Sitojun 408273220Smelifaro return (ENOENT); 40962587Sitojun} 41062587Sitojun 41162587Sitojunstatic int 412249925Sglebiusstf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 413286114Sae struct route *ro) 41462587Sitojun{ 41562587Sitojun struct stf_softc *sc; 416249925Sglebius const struct sockaddr_in6 *dst6; 417109322Ssuz struct in_addr in4; 418249925Sglebius const void *ptr; 41962587Sitojun u_int8_t tos; 42062587Sitojun struct ip *ip; 42162587Sitojun struct ip6_hdr *ip6; 422273220Smelifaro struct in6_addr addr6, mask6; 423105580Srwatson int error; 42462587Sitojun 425183351Sdwmalone#ifdef MAC 426172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 427105580Srwatson if (error) { 428105580Srwatson m_freem(m); 429105580Srwatson return (error); 430105580Srwatson } 431105580Srwatson#endif 432105580Srwatson 433147256Sbrooks sc = ifp->if_softc; 434249925Sglebius dst6 = (const struct sockaddr_in6 *)dst; 43562587Sitojun 43662587Sitojun /* just in case */ 43762587Sitojun if ((ifp->if_flags & IFF_UP) == 0) { 43862587Sitojun m_freem(m); 439271867Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 44062587Sitojun return ENETDOWN; 44162587Sitojun } 44262587Sitojun 44362587Sitojun /* 44462587Sitojun * If we don't have an ip4 address that match my inner ip6 address, 44562587Sitojun * we shouldn't generate output. Without this check, we'll end up 44662587Sitojun * using wrong IPv4 source. 44762587Sitojun */ 448273220Smelifaro if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) { 44962587Sitojun m_freem(m); 450271867Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 45162587Sitojun return ENETDOWN; 45262587Sitojun } 45362587Sitojun 45462587Sitojun if (m->m_len < sizeof(*ip6)) { 45562587Sitojun m = m_pullup(m, sizeof(*ip6)); 456103487Sume if (!m) { 457271867Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 45862587Sitojun return ENOBUFS; 459103487Sume } 46062587Sitojun } 46162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 46262587Sitojun tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 46362587Sitojun 46478064Sume /* 46578064Sume * Pickup the right outer dst addr from the list of candidates. 46678064Sume * ip6_dst has priority as it may be able to give us shorter IPv4 hops. 46778064Sume */ 468109322Ssuz ptr = NULL; 46978064Sume if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst)) 470109322Ssuz ptr = GET_V4(&ip6->ip6_dst); 47178064Sume else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr)) 472109322Ssuz ptr = GET_V4(&dst6->sin6_addr); 47378064Sume else { 47478064Sume m_freem(m); 475271867Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 47678064Sume return ENETUNREACH; 47778064Sume } 478109322Ssuz bcopy(ptr, &in4, sizeof(in4)); 47978064Sume 480159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 48178701Sume /* 48278701Sume * We need to prepend the address family as 48378701Sume * a four byte field. Cons up a dummy header 48478701Sume * to pacify bpf. This is safe because bpf 48578701Sume * will only read from the mbuf (i.e., it won't 48678701Sume * try to free it or keep a pointer a to it). 48778701Sume */ 488249925Sglebius u_int af = AF_INET6; 489123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 49078701Sume } 49178701Sume 492243882Sglebius M_PREPEND(m, sizeof(struct ip), M_NOWAIT); 493103487Sume if (m == NULL) { 494271867Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 49562587Sitojun return ENOBUFS; 496103487Sume } 49762587Sitojun ip = mtod(m, struct ip *); 49862587Sitojun 49962587Sitojun bzero(ip, sizeof(*ip)); 50062587Sitojun 501273220Smelifaro bcopy(GET_V4(&addr6), &ip->ip_src, sizeof(ip->ip_src)); 502109322Ssuz bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst)); 50362587Sitojun ip->ip_p = IPPROTO_IPV6; 50479106Sbrooks ip->ip_ttl = ip_stf_ttl; 505241913Sglebius ip->ip_len = htons(m->m_pkthdr.len); 50662587Sitojun if (ifp->if_flags & IFF_LINK1) 50762587Sitojun ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos); 50878064Sume else 50978064Sume ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos); 51062587Sitojun 511178888Sjulian M_SETFIB(m, sc->sc_fibnum); 512271867Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 513273220Smelifaro error = ip_output(m, NULL, NULL, 0, NULL, NULL); 514183351Sdwmalone 515183351Sdwmalone return error; 51662587Sitojun} 51762587Sitojun 51862587Sitojunstatic int 519286114Saeisrfc1918addr(struct in_addr *in) 520103475Sume{ 521103475Sume /* 522103475Sume * returns 1 if private address range: 523103475Sume * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 524103475Sume */ 525244750Sae if (stf_permit_rfc1918 == 0 && ( 526244750Sae (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 || 527109322Ssuz (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 || 528244750Sae (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)) 529103475Sume return 1; 530103475Sume 531103475Sume return 0; 532103475Sume} 533103475Sume 534103475Sumestatic int 535286114Saestf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp) 53662587Sitojun{ 537286001Sae struct rm_priotracker in_ifa_tracker; 53862587Sitojun struct in_ifaddr *ia4; 53962587Sitojun 54062587Sitojun /* 54162587Sitojun * reject packets with the following address: 54262587Sitojun * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8 54362587Sitojun */ 544109322Ssuz if (IN_MULTICAST(ntohl(in->s_addr))) 54562587Sitojun return -1; 546109322Ssuz switch ((ntohl(in->s_addr) & 0xff000000) >> 24) { 547109322Ssuz case 0: case 127: case 255: 54862587Sitojun return -1; 549109322Ssuz } 55062587Sitojun 55162587Sitojun /* 552103475Sume * reject packets with private address range. 553103475Sume * (requirement from RFC3056 section 2 1st paragraph) 554103475Sume */ 555103475Sume if (isrfc1918addr(in)) 556103475Sume return -1; 557103475Sume 558103475Sume /* 55962587Sitojun * reject packets with broadcast 56062587Sitojun */ 561286001Sae IN_IFADDR_RLOCK(&in_ifa_tracker); 562239357Sjhb TAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) { 56362587Sitojun if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0) 56462587Sitojun continue; 565194951Srwatson if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) { 566286001Sae IN_IFADDR_RUNLOCK(&in_ifa_tracker); 56762587Sitojun return -1; 568194951Srwatson } 56962587Sitojun } 570286001Sae IN_IFADDR_RUNLOCK(&in_ifa_tracker); 57162587Sitojun 57262587Sitojun /* 57362587Sitojun * perform ingress filter 57462587Sitojun */ 575147256Sbrooks if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) { 576292331Smelifaro struct nhop4_basic nh4; 57762587Sitojun 578292331Smelifaro if (fib4_lookup_nh_basic(sc->sc_fibnum, *in, 0, 0, &nh4) != 0) 579292331Smelifaro return (-1); 580292331Smelifaro 581292331Smelifaro if (nh4.nh_ifp != inifp) 582292331Smelifaro return (-1); 58362587Sitojun } 58462587Sitojun 58562587Sitojun return 0; 58662587Sitojun} 58762587Sitojun 58862587Sitojunstatic int 589286114Saestf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp) 59062587Sitojun{ 59162587Sitojun /* 59262587Sitojun * check 6to4 addresses 59362587Sitojun */ 594109322Ssuz if (IN6_IS_ADDR_6TO4(in6)) { 595109322Ssuz struct in_addr in4; 596109322Ssuz bcopy(GET_V4(in6), &in4, sizeof(in4)); 597109322Ssuz return stf_checkaddr4(sc, &in4, inifp); 598109322Ssuz } 59962587Sitojun 60062587Sitojun /* 60162587Sitojun * reject anything that look suspicious. the test is implemented 60262587Sitojun * in ip6_input too, but we check here as well to 60362587Sitojun * (1) reject bad packets earlier, and 60462587Sitojun * (2) to be safe against future ip6_input change. 60562587Sitojun */ 60662587Sitojun if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6)) 60762587Sitojun return -1; 60862587Sitojun 60962587Sitojun return 0; 61062587Sitojun} 61162587Sitojun 612276152Saestatic int 613269699Skevloin_stf_input(struct mbuf **mp, int *offp, int proto) 61462587Sitojun{ 61562587Sitojun struct stf_softc *sc; 61662587Sitojun struct ip *ip; 61762587Sitojun struct ip6_hdr *ip6; 618269699Skevlo struct mbuf *m; 61962587Sitojun u_int8_t otos, itos; 62062587Sitojun struct ifnet *ifp; 621269699Skevlo int off; 62262587Sitojun 623269699Skevlo m = *mp; 624269699Skevlo off = *offp; 62562587Sitojun 62662587Sitojun if (proto != IPPROTO_IPV6) { 62762587Sitojun m_freem(m); 628269699Skevlo return (IPPROTO_DONE); 62962587Sitojun } 63062587Sitojun 63162587Sitojun ip = mtod(m, struct ip *); 63262587Sitojun 63362587Sitojun sc = (struct stf_softc *)encap_getarg(m); 63462587Sitojun 635147256Sbrooks if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) { 63662587Sitojun m_freem(m); 637269699Skevlo return (IPPROTO_DONE); 63862587Sitojun } 63962587Sitojun 640147256Sbrooks ifp = STF2IFP(sc); 64162587Sitojun 642105580Srwatson#ifdef MAC 643172930Srwatson mac_ifnet_create_mbuf(ifp, m); 644105580Srwatson#endif 645105580Srwatson 64662587Sitojun /* 64762587Sitojun * perform sanity check against outer src/dst. 64862587Sitojun * for source, perform ingress filter as well. 64962587Sitojun */ 65078064Sume if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 || 65178064Sume stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) { 65262587Sitojun m_freem(m); 653269699Skevlo return (IPPROTO_DONE); 65462587Sitojun } 65562587Sitojun 65662587Sitojun otos = ip->ip_tos; 65762587Sitojun m_adj(m, off); 65862587Sitojun 65962587Sitojun if (m->m_len < sizeof(*ip6)) { 66062587Sitojun m = m_pullup(m, sizeof(*ip6)); 66162587Sitojun if (!m) 662269699Skevlo return (IPPROTO_DONE); 66362587Sitojun } 66462587Sitojun ip6 = mtod(m, struct ip6_hdr *); 66562587Sitojun 66662587Sitojun /* 66762587Sitojun * perform sanity check against inner src/dst. 66862587Sitojun * for source, perform ingress filter as well. 66962587Sitojun */ 67078064Sume if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 || 67178064Sume stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) { 67262587Sitojun m_freem(m); 673269699Skevlo return (IPPROTO_DONE); 67462587Sitojun } 67562587Sitojun 67662587Sitojun itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 67762587Sitojun if ((ifp->if_flags & IFF_LINK1) != 0) 67862587Sitojun ip_ecn_egress(ECN_ALLOWED, &otos, &itos); 67978064Sume else 68078064Sume ip_ecn_egress(ECN_NOCARE, &otos, &itos); 68162587Sitojun ip6->ip6_flow &= ~htonl(0xff << 20); 68262587Sitojun ip6->ip6_flow |= htonl((u_int32_t)itos << 20); 68362587Sitojun 68462587Sitojun m->m_pkthdr.rcvif = ifp; 68562587Sitojun 686159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 68762587Sitojun /* 68862587Sitojun * We need to prepend the address family as 68962587Sitojun * a four byte field. Cons up a dummy header 69062587Sitojun * to pacify bpf. This is safe because bpf 69162587Sitojun * will only read from the mbuf (i.e., it won't 69262587Sitojun * try to free it or keep a pointer a to it). 69362587Sitojun */ 694123922Ssam u_int32_t af = AF_INET6; 695140042Sume bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 69662587Sitojun } 69762587Sitojun 69862587Sitojun /* 69962587Sitojun * Put the packet to the network layer input queue according to the 70062587Sitojun * specified address family. 70162587Sitojun * See net/if_gif.c for possible issues with packet processing 70262587Sitojun * reorder due to extra queueing. 70362587Sitojun */ 704271867Sglebius if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 705271867Sglebius if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 706223741Sbz M_SETFIB(m, ifp->if_fib); 707111888Sjlemon netisr_dispatch(NETISR_IPV6, m); 708269699Skevlo return (IPPROTO_DONE); 70962587Sitojun} 71062587Sitojun 71162587Sitojunstatic int 712286114Saestf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 71362587Sitojun{ 71462587Sitojun struct ifaddr *ifa; 71562587Sitojun struct ifreq *ifr; 71662587Sitojun struct sockaddr_in6 *sin6; 717109322Ssuz struct in_addr addr; 718238492Smelifaro int error, mtu; 71962587Sitojun 72062587Sitojun error = 0; 72162587Sitojun switch (cmd) { 72262587Sitojun case SIOCSIFADDR: 72362587Sitojun ifa = (struct ifaddr *)data; 72462587Sitojun if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) { 72562587Sitojun error = EAFNOSUPPORT; 72662587Sitojun break; 72762587Sitojun } 72862587Sitojun sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 729109322Ssuz if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) { 73062587Sitojun error = EINVAL; 731109322Ssuz break; 732109322Ssuz } 733109322Ssuz bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr)); 734109322Ssuz if (isrfc1918addr(&addr)) { 735109322Ssuz error = EINVAL; 736109322Ssuz break; 737109322Ssuz } 738109322Ssuz 739109322Ssuz ifp->if_flags |= IFF_UP; 74062587Sitojun break; 74162587Sitojun 74262587Sitojun case SIOCADDMULTI: 74362587Sitojun case SIOCDELMULTI: 74462587Sitojun ifr = (struct ifreq *)data; 74562587Sitojun if (ifr && ifr->ifr_addr.sa_family == AF_INET6) 74662587Sitojun ; 74762587Sitojun else 74862587Sitojun error = EAFNOSUPPORT; 74962587Sitojun break; 75062587Sitojun 751238492Smelifaro case SIOCGIFMTU: 752238492Smelifaro break; 753238492Smelifaro 754238492Smelifaro case SIOCSIFMTU: 755238492Smelifaro ifr = (struct ifreq *)data; 756238492Smelifaro mtu = ifr->ifr_mtu; 757238492Smelifaro /* RFC 4213 3.2 ideal world MTU */ 758238492Smelifaro if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20) 759238492Smelifaro return (EINVAL); 760238492Smelifaro ifp->if_mtu = mtu; 761238492Smelifaro break; 762238492Smelifaro 76362587Sitojun default: 76462587Sitojun error = EINVAL; 76562587Sitojun break; 76662587Sitojun } 76762587Sitojun 76862587Sitojun return error; 76962587Sitojun} 770