if_gif.c revision 185895
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 185895 2008-12-10 23:12:39Z zec $ */ 295023Ssuz/* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 362587Sitojun 4139823Simp/*- 554263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 654263Sshin * All rights reserved. 754263Sshin * 854263Sshin * Redistribution and use in source and binary forms, with or without 954263Sshin * modification, are permitted provided that the following conditions 1054263Sshin * are met: 1154263Sshin * 1. Redistributions of source code must retain the above copyright 1254263Sshin * notice, this list of conditions and the following disclaimer. 1354263Sshin * 2. Redistributions in binary form must reproduce the above copyright 1454263Sshin * notice, this list of conditions and the following disclaimer in the 1554263Sshin * documentation and/or other materials provided with the distribution. 1654263Sshin * 3. Neither the name of the project nor the names of its contributors 1754263Sshin * may be used to endorse or promote products derived from this software 1854263Sshin * without specific prior written permission. 1954263Sshin * 2054263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2154263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2254263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2354263Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2454263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2554263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2654263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2754263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2854263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2954263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3054263Sshin * SUCH DAMAGE. 3154263Sshin */ 3254263Sshin 3354263Sshin#include "opt_inet.h" 3454263Sshin#include "opt_inet6.h" 35101739Srwatson#include "opt_mac.h" 3654263Sshin 3754263Sshin#include <sys/param.h> 3854263Sshin#include <sys/systm.h> 3954263Sshin#include <sys/kernel.h> 4054263Sshin#include <sys/malloc.h> 4154263Sshin#include <sys/mbuf.h> 42129880Sphk#include <sys/module.h> 4354263Sshin#include <sys/socket.h> 4454263Sshin#include <sys/sockio.h> 4554263Sshin#include <sys/errno.h> 4654263Sshin#include <sys/time.h> 4791270Sbrooks#include <sys/sysctl.h> 4854263Sshin#include <sys/syslog.h> 49178888Sjulian#include <sys/proc.h> 5062587Sitojun#include <sys/protosw.h> 5179106Sbrooks#include <sys/conf.h> 52181803Sbz#include <sys/vimage.h> 5354263Sshin#include <machine/cpu.h> 5454263Sshin 5554263Sshin#include <net/if.h> 56130933Sbrooks#include <net/if_clone.h> 5754263Sshin#include <net/if_types.h> 5854263Sshin#include <net/netisr.h> 5954263Sshin#include <net/route.h> 6054263Sshin#include <net/bpf.h> 6154263Sshin 6254263Sshin#include <netinet/in.h> 6354263Sshin#include <netinet/in_systm.h> 6478064Sume#include <netinet/ip.h> 6578064Sume#ifdef INET 6654263Sshin#include <netinet/in_var.h> 6754263Sshin#include <netinet/in_gif.h> 6879106Sbrooks#include <netinet/ip_var.h> 6954263Sshin#endif /* INET */ 7054263Sshin 7154263Sshin#ifdef INET6 7254263Sshin#ifndef INET 7354263Sshin#include <netinet/in.h> 7454263Sshin#endif 7554263Sshin#include <netinet6/in6_var.h> 7654263Sshin#include <netinet/ip6.h> 7754263Sshin#include <netinet6/ip6_var.h> 78148385Sume#include <netinet6/scope6_var.h> 7954263Sshin#include <netinet6/in6_gif.h> 8062587Sitojun#include <netinet6/ip6protosw.h> 8154263Sshin#endif /* INET6 */ 8254263Sshin 8362587Sitojun#include <netinet/ip_encap.h> 84153621Sthompsa#include <net/ethernet.h> 85153621Sthompsa#include <net/if_bridgevar.h> 8654263Sshin#include <net/if_gif.h> 8754263Sshin 88163606Srwatson#include <security/mac/mac_framework.h> 89163606Srwatson 9079106Sbrooks#define GIFNAME "gif" 9162587Sitojun 92127305Srwatson/* 93127898Sru * gif_mtx protects the global gif_softc_list. 94127305Srwatson */ 95127305Srwatsonstatic struct mtx gif_mtx; 9679106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 97185088Szec 98185895Szec#ifndef VIMAGE 99185895Szec#ifndef VIMAGE_GLOBALS 100185895Szecstruct vnet_gif vnet_gif_0; 101185895Szec#endif 102185895Szec#endif 103185895Szec 104185088Szec#ifdef VIMAGE_GLOBALS 10589065Smsmithstatic LIST_HEAD(, gif_softc) gif_softc_list; 106185088Szecstatic int max_gif_nesting; 107185088Szecstatic int parallel_tunnels; 108185088Szec#ifdef INET 109185088Szecint ip_gif_ttl; 110185088Szec#endif 111185088Szec#ifdef INET6 112185088Szecint ip6_gif_hlim; 113185088Szec#endif 114185088Szec#endif 11579106Sbrooks 11683998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 11783998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 11883998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 11983998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 12083998Sbrooks 121153621Sthompsastatic void gif_start(struct ifnet *); 122160195Ssamstatic int gif_clone_create(struct if_clone *, int, caddr_t); 123128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 12479106Sbrooks 125130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 12679106Sbrooks 12792725Salfredstatic int gifmodevent(module_t, int, void *); 12879106Sbrooks 12991270SbrooksSYSCTL_DECL(_net_link); 13091270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 13191270Sbrooks "Generic Tunnel Interface"); 13262587Sitojun#ifndef MAX_GIF_NEST 13362587Sitojun/* 13491270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 13562587Sitojun * Since, setting a large value to this macro with a careless configuration 13662587Sitojun * may introduce system crash, we don't allow any nestings by default. 13762587Sitojun * If you need to configure nested gif tunnels, you can define this macro 13895023Ssuz * in your kernel configuration file. However, if you do so, please be 13962587Sitojun * careful to configure the tunnels so that it won't make a loop. 14062587Sitojun */ 14162587Sitojun#define MAX_GIF_NEST 1 14262587Sitojun#endif 143183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting, 144183550Szec CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels"); 14562587Sitojun 146183550Szec#ifdef INET6 147183550SzecSYSCTL_DECL(_net_inet6_ip6); 148183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM, 149183550Szec gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, ""); 150183550Szec#endif 151183550Szec 15291270Sbrooks/* 15391270Sbrooks * By default, we disallow creation of multiple tunnels between the same 15491270Sbrooks * pair of addresses. Some applications require this functionality so 15591270Sbrooks * we allow control over this check here. 15691270Sbrooks */ 157183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels, 158183550Szec CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?"); 15991270Sbrooks 160176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 161176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 162176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 163176879Sthompsa#ifndef ETHER_IS_BROADCAST 164176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 165176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 166176879Sthompsa#endif 167176879Sthompsa 168128209Sbrooksstatic int 169160195Ssamgif_clone_create(ifc, unit, params) 17079106Sbrooks struct if_clone *ifc; 17192081Smux int unit; 172160195Ssam caddr_t params; 17354263Sshin{ 174183550Szec INIT_VNET_GIF(curvnet); 17578064Sume struct gif_softc *sc; 17654263Sshin 177131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 178178888Sjulian sc->gif_fibnum = curthread->td_proc->p_fibnum; 179147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 180147256Sbrooks if (GIF2IFP(sc) == NULL) { 181147256Sbrooks free(sc, M_GIF); 182147256Sbrooks return (ENOSPC); 183147256Sbrooks } 18479106Sbrooks 185155037Sglebius GIF_LOCK_INIT(sc); 186155037Sglebius 187147256Sbrooks GIF2IFP(sc)->if_softc = sc; 188147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 18979106Sbrooks 19079106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 19162587Sitojun 192147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 193147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 194147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 19578064Sume#if 0 19679106Sbrooks /* turn off ingress filter */ 197147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 19878064Sume#endif 199147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 200153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 201147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 202147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 203147256Sbrooks if_attach(GIF2IFP(sc)); 204147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 20583998Sbrooks if (ng_gif_attach_p != NULL) 206147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 207155037Sglebius 208155037Sglebius mtx_lock(&gif_mtx); 209181803Sbz LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 210155037Sglebius mtx_unlock(&gif_mtx); 211155037Sglebius 212155037Sglebius return (0); 21379106Sbrooks} 21479106Sbrooks 215127305Srwatsonstatic void 216151266Sthompsagif_clone_destroy(ifp) 217151266Sthompsa struct ifnet *ifp; 21879106Sbrooks{ 219184678Sbz#if defined(INET) || defined(INET6) 22079106Sbrooks int err; 221184678Sbz#endif 222151266Sthompsa struct gif_softc *sc = ifp->if_softc; 22379106Sbrooks 224151266Sthompsa mtx_lock(&gif_mtx); 225151266Sthompsa LIST_REMOVE(sc, gif_list); 226151266Sthompsa mtx_unlock(&gif_mtx); 227151266Sthompsa 228127305Srwatson gif_delete_tunnel(ifp); 229105293Sume#ifdef INET6 230105293Sume if (sc->encap_cookie6 != NULL) { 231105293Sume err = encap_detach(sc->encap_cookie6); 232105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 233105293Sume } 234105293Sume#endif 235105293Sume#ifdef INET 23679106Sbrooks if (sc->encap_cookie4 != NULL) { 23779106Sbrooks err = encap_detach(sc->encap_cookie4); 23879106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 23979106Sbrooks } 240105293Sume#endif 24179106Sbrooks 24283998Sbrooks if (ng_gif_detach_p != NULL) 24383998Sbrooks (*ng_gif_detach_p)(ifp); 24479106Sbrooks bpfdetach(ifp); 24579106Sbrooks if_detach(ifp); 246147256Sbrooks if_free(ifp); 24779106Sbrooks 248155037Sglebius GIF_LOCK_DESTROY(sc); 249155037Sglebius 25079106Sbrooks free(sc, M_GIF); 25179106Sbrooks} 25279106Sbrooks 25379106Sbrooksstatic int 25479106Sbrooksgifmodevent(mod, type, data) 25579106Sbrooks module_t mod; 25679106Sbrooks int type; 25779106Sbrooks void *data; 25879106Sbrooks{ 25979106Sbrooks 26079106Sbrooks switch (type) { 26179106Sbrooks case MOD_LOAD: 262127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 263185088Szec 264181803Sbz LIST_INIT(&V_gif_softc_list); 265185088Szec V_max_gif_nesting = MAX_GIF_NEST; 266185088Szec#ifdef XBONEHACK 267185088Szec V_parallel_tunnels = 1; 268185088Szec#else 269185088Szec V_parallel_tunnels = 0; 270185088Szec#endif 271185088Szec#ifdef INET 272185088Szec V_ip_gif_ttl = GIF_TTL; 273185088Szec#endif 27479106Sbrooks#ifdef INET6 275181803Sbz V_ip6_gif_hlim = GIF_HLIM; 27662587Sitojun#endif 277185088Szec if_clone_attach(&gif_cloner); 27879106Sbrooks 27979106Sbrooks break; 28079106Sbrooks case MOD_UNLOAD: 28179106Sbrooks if_clone_detach(&gif_cloner); 282127305Srwatson mtx_destroy(&gif_mtx); 28379106Sbrooks#ifdef INET6 284181803Sbz V_ip6_gif_hlim = 0; 28562587Sitojun#endif 28679106Sbrooks break; 287132199Sphk default: 288132199Sphk return EOPNOTSUPP; 28954263Sshin } 29079106Sbrooks return 0; 29154263Sshin} 29254263Sshin 29379106Sbrooksstatic moduledata_t gif_mod = { 29479106Sbrooks "if_gif", 29579106Sbrooks gifmodevent, 29679106Sbrooks 0 29779106Sbrooks}; 29854263Sshin 29979106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 30083997SbrooksMODULE_VERSION(if_gif, 1); 30179106Sbrooks 302105293Sumeint 30362587Sitojungif_encapcheck(m, off, proto, arg) 30462587Sitojun const struct mbuf *m; 30562587Sitojun int off; 30662587Sitojun int proto; 30762587Sitojun void *arg; 30862587Sitojun{ 30962587Sitojun struct ip ip; 31062587Sitojun struct gif_softc *sc; 31162587Sitojun 31262587Sitojun sc = (struct gif_softc *)arg; 31362587Sitojun if (sc == NULL) 31462587Sitojun return 0; 31562587Sitojun 316147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 31762587Sitojun return 0; 31862587Sitojun 31962587Sitojun /* no physical address */ 32062587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 32162587Sitojun return 0; 32262587Sitojun 32362587Sitojun switch (proto) { 32462587Sitojun#ifdef INET 32562587Sitojun case IPPROTO_IPV4: 32662587Sitojun break; 32762587Sitojun#endif 32862587Sitojun#ifdef INET6 32962587Sitojun case IPPROTO_IPV6: 33062587Sitojun break; 33162587Sitojun#endif 332153621Sthompsa case IPPROTO_ETHERIP: 333153621Sthompsa break; 334153621Sthompsa 33562587Sitojun default: 33662587Sitojun return 0; 33762587Sitojun } 33862587Sitojun 339105339Sume /* Bail on short packets */ 340105339Sume if (m->m_pkthdr.len < sizeof(ip)) 341105339Sume return 0; 342105339Sume 34391327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 34462587Sitojun 34562587Sitojun switch (ip.ip_v) { 34662587Sitojun#ifdef INET 34762587Sitojun case 4: 34862587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 34962587Sitojun sc->gif_pdst->sa_family != AF_INET) 35062587Sitojun return 0; 35162587Sitojun return gif_encapcheck4(m, off, proto, arg); 35262587Sitojun#endif 35362587Sitojun#ifdef INET6 35462587Sitojun case 6: 355105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 356105293Sume return 0; 35762587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 35862587Sitojun sc->gif_pdst->sa_family != AF_INET6) 35962587Sitojun return 0; 36062587Sitojun return gif_encapcheck6(m, off, proto, arg); 36162587Sitojun#endif 36262587Sitojun default: 36362587Sitojun return 0; 36462587Sitojun } 36562587Sitojun} 36662587Sitojun 367153621Sthompsastatic void 368153621Sthompsagif_start(struct ifnet *ifp) 369153621Sthompsa{ 370153621Sthompsa struct gif_softc *sc; 371153621Sthompsa struct mbuf *m; 372153621Sthompsa 373153621Sthompsa sc = ifp->if_softc; 374153621Sthompsa 375153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 376153621Sthompsa for (;;) { 377153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 378153621Sthompsa if (m == 0) 379153621Sthompsa break; 380153621Sthompsa 381153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 382153621Sthompsa 383153621Sthompsa } 384153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 385153621Sthompsa 386153621Sthompsa return; 387153621Sthompsa} 388153621Sthompsa 38954263Sshinint 39054263Sshingif_output(ifp, m, dst, rt) 39154263Sshin struct ifnet *ifp; 39254263Sshin struct mbuf *m; 39354263Sshin struct sockaddr *dst; 39454263Sshin struct rtentry *rt; /* added in net2 */ 39554263Sshin{ 396183550Szec INIT_VNET_GIF(ifp->if_vnet); 397147256Sbrooks struct gif_softc *sc = ifp->if_softc; 398127898Sru struct m_tag *mtag; 39954263Sshin int error = 0; 400127898Sru int gif_called; 401147611Sdwmalone u_int32_t af; 40254263Sshin 403101182Srwatson#ifdef MAC 404172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 405101739Srwatson if (error) { 406101739Srwatson m_freem(m); 407101739Srwatson goto end; 408101739Srwatson } 409101182Srwatson#endif 410101182Srwatson 41154263Sshin /* 41254263Sshin * gif may cause infinite recursion calls when misconfigured. 413127898Sru * We'll prevent this by detecting loops. 414127898Sru * 415127898Sru * High nesting level may cause stack exhaustion. 41654263Sshin * We'll prevent this by introducing upper limit. 41754263Sshin */ 418127898Sru gif_called = 1; 419127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 420127898Sru while (mtag != NULL) { 421127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 422127898Sru log(LOG_NOTICE, 423127898Sru "gif_output: loop detected on %s\n", 424127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 425127898Sru m_freem(m); 426127898Sru error = EIO; /* is there better errno? */ 427127898Sru goto end; 428127898Sru } 429127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 430127898Sru gif_called++; 431127898Sru } 432181803Sbz if (gif_called > V_max_gif_nesting) { 43354263Sshin log(LOG_NOTICE, 43454263Sshin "gif_output: recursively called too many times(%d)\n", 435127303Srwatson gif_called); 43654263Sshin m_freem(m); 43754263Sshin error = EIO; /* is there better errno? */ 43854263Sshin goto end; 43954263Sshin } 440127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 441127898Sru M_NOWAIT); 442127898Sru if (mtag == NULL) { 443127898Sru m_freem(m); 444127898Sru error = ENOMEM; 445127898Sru goto end; 446127898Sru } 447127898Sru *(struct ifnet **)(mtag + 1) = ifp; 448127898Sru m_tag_prepend(m, mtag); 44962587Sitojun 45054263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 451155037Sglebius 452155037Sglebius GIF_LOCK(sc); 453155037Sglebius 45454263Sshin if (!(ifp->if_flags & IFF_UP) || 45554263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 456159174Sglebius GIF_UNLOCK(sc); 45754263Sshin m_freem(m); 45854263Sshin error = ENETDOWN; 45954263Sshin goto end; 46054263Sshin } 46154263Sshin 462147611Sdwmalone /* BPF writes need to be handled specially. */ 463147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 464147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 465147611Sdwmalone dst->sa_family = af; 466147611Sdwmalone } 467147611Sdwmalone 468153621Sthompsa af = dst->sa_family; 469159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 47062587Sitojun ifp->if_opackets++; 47154263Sshin ifp->if_obytes += m->m_pkthdr.len; 47254263Sshin 473153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 474153621Sthompsa if (ifp->if_bridge) 475153621Sthompsa af = AF_LINK; 476153621Sthompsa 477178888Sjulian M_SETFIB(m, sc->gif_fibnum); 47878064Sume /* inner AF-specific encapsulation */ 47978064Sume 48062587Sitojun /* XXX should we check if our outer source is legal? */ 48162587Sitojun 48278064Sume /* dispatch to output logic based on outer AF */ 48354263Sshin switch (sc->gif_psrc->sa_family) { 48454263Sshin#ifdef INET 48554263Sshin case AF_INET: 486153621Sthompsa error = in_gif_output(ifp, af, m); 48754263Sshin break; 48854263Sshin#endif 48954263Sshin#ifdef INET6 49054263Sshin case AF_INET6: 491153621Sthompsa error = in6_gif_output(ifp, af, m); 49254263Sshin break; 49354263Sshin#endif 49454263Sshin default: 49562587Sitojun m_freem(m); 49654263Sshin error = ENETDOWN; 49754263Sshin } 49854263Sshin 499159174Sglebius GIF_UNLOCK(sc); 50054263Sshin end: 50178064Sume if (error) 50278064Sume ifp->if_oerrors++; 503155037Sglebius return (error); 50454263Sshin} 50554263Sshin 50654263Sshinvoid 507105338Sumegif_input(m, af, ifp) 50854263Sshin struct mbuf *m; 50954263Sshin int af; 510105338Sume struct ifnet *ifp; 51154263Sshin{ 512153621Sthompsa int isr, n; 513153621Sthompsa struct etherip_header *eip; 514176879Sthompsa struct ether_header *eh; 515176879Sthompsa struct ifnet *oldifp; 51654263Sshin 517105338Sume if (ifp == NULL) { 51854263Sshin /* just in case */ 51954263Sshin m_freem(m); 52054263Sshin return; 52154263Sshin } 52254263Sshin 523105338Sume m->m_pkthdr.rcvif = ifp; 524101182Srwatson 525101182Srwatson#ifdef MAC 526172930Srwatson mac_ifnet_create_mbuf(ifp, m); 527101182Srwatson#endif 528101182Srwatson 529159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 53078064Sume u_int32_t af1 = af; 531123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 53254263Sshin } 53354263Sshin 53483998Sbrooks if (ng_gif_input_p != NULL) { 535105338Sume (*ng_gif_input_p)(ifp, &m, af); 53683998Sbrooks if (m == NULL) 53783998Sbrooks return; 53883998Sbrooks } 53983998Sbrooks 54054263Sshin /* 54154263Sshin * Put the packet to the network layer input queue according to the 54254263Sshin * specified address family. 54354263Sshin * Note: older versions of gif_input directly called network layer 54495023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 54554263Sshin * prevent too many recursive calls of such input functions, which 54695023Ssuz * might cause kernel panic. But the change may introduce another 54754263Sshin * problem; if the input queue is full, packets are discarded. 54895023Ssuz * The kernel stack overflow really happened, and we believed 54995023Ssuz * queue-full rarely occurs, so we changed the policy. 55054263Sshin */ 55154263Sshin switch (af) { 55254263Sshin#ifdef INET 55354263Sshin case AF_INET: 55454263Sshin isr = NETISR_IP; 55554263Sshin break; 55654263Sshin#endif 55754263Sshin#ifdef INET6 55854263Sshin case AF_INET6: 55954263Sshin isr = NETISR_IPV6; 56054263Sshin break; 56154263Sshin#endif 562153621Sthompsa case AF_LINK: 563153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 564153621Sthompsa if (n > m->m_len) { 565153621Sthompsa m = m_pullup(m, n); 566153621Sthompsa if (m == NULL) { 567153621Sthompsa ifp->if_ierrors++; 568153621Sthompsa return; 569153621Sthompsa } 570153621Sthompsa } 571153621Sthompsa 572153621Sthompsa eip = mtod(m, struct etherip_header *); 573153621Sthompsa if (eip->eip_ver != 574153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 575153621Sthompsa /* discard unknown versions */ 576153621Sthompsa m_freem(m); 577153621Sthompsa return; 578153621Sthompsa } 579153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 580153621Sthompsa 581153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 582153621Sthompsa m->m_pkthdr.rcvif = ifp; 583153621Sthompsa 584176879Sthompsa if (ifp->if_bridge) { 585176879Sthompsa oldifp = ifp; 586176879Sthompsa eh = mtod(m, struct ether_header *); 587176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 588176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 589176879Sthompsa m->m_flags |= M_BCAST; 590176879Sthompsa else 591176879Sthompsa m->m_flags |= M_MCAST; 592176879Sthompsa ifp->if_imcasts++; 593176879Sthompsa } 594153621Sthompsa BRIDGE_INPUT(ifp, m); 595176879Sthompsa 596176879Sthompsa if (m != NULL && ifp != oldifp) { 597176879Sthompsa /* 598176879Sthompsa * The bridge gave us back itself or one of the 599176879Sthompsa * members for which the frame is addressed. 600176879Sthompsa */ 601176879Sthompsa ether_demux(ifp, m); 602176879Sthompsa return; 603176879Sthompsa } 604176879Sthompsa } 605153621Sthompsa if (m != NULL) 606153621Sthompsa m_freem(m); 607153621Sthompsa return; 608153621Sthompsa 60954263Sshin default: 61083998Sbrooks if (ng_gif_input_orphan_p != NULL) 611105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 61283998Sbrooks else 61383998Sbrooks m_freem(m); 61454263Sshin return; 61554263Sshin } 61654263Sshin 617105338Sume ifp->if_ipackets++; 618105338Sume ifp->if_ibytes += m->m_pkthdr.len; 619111888Sjlemon netisr_dispatch(isr, m); 62054263Sshin} 62154263Sshin 62262587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 62354263Sshinint 62454263Sshingif_ioctl(ifp, cmd, data) 62554263Sshin struct ifnet *ifp; 62654263Sshin u_long cmd; 62754263Sshin caddr_t data; 62854263Sshin{ 629147256Sbrooks struct gif_softc *sc = ifp->if_softc; 63054263Sshin struct ifreq *ifr = (struct ifreq*)data; 63154263Sshin int error = 0, size; 63262587Sitojun struct sockaddr *dst, *src; 633105339Sume#ifdef SIOCSIFMTU /* xxx */ 634105339Sume u_long mtu; 635105339Sume#endif 636105339Sume 63754263Sshin switch (cmd) { 63854263Sshin case SIOCSIFADDR: 639105293Sume ifp->if_flags |= IFF_UP; 64054263Sshin break; 64162587Sitojun 64254263Sshin case SIOCSIFDSTADDR: 64354263Sshin break; 64454263Sshin 64554263Sshin case SIOCADDMULTI: 64654263Sshin case SIOCDELMULTI: 64754263Sshin break; 64854263Sshin 64962587Sitojun#ifdef SIOCSIFMTU /* xxx */ 65054263Sshin case SIOCGIFMTU: 65154263Sshin break; 65262587Sitojun 65354263Sshin case SIOCSIFMTU: 654105339Sume mtu = ifr->ifr_mtu; 655105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 656105339Sume return (EINVAL); 657105339Sume ifp->if_mtu = mtu; 65854263Sshin break; 65962587Sitojun#endif /* SIOCSIFMTU */ 66054263Sshin 661105339Sume#ifdef INET 66254263Sshin case SIOCSIFPHYADDR: 663105339Sume#endif 66454263Sshin#ifdef INET6 66554263Sshin case SIOCSIFPHYADDR_IN6: 66654263Sshin#endif /* INET6 */ 66778064Sume case SIOCSLIFPHYADDR: 66862587Sitojun switch (cmd) { 66978064Sume#ifdef INET 67062587Sitojun case SIOCSIFPHYADDR: 67154263Sshin src = (struct sockaddr *) 67254263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 67354263Sshin dst = (struct sockaddr *) 67454263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 67562587Sitojun break; 67678064Sume#endif 67762587Sitojun#ifdef INET6 67862587Sitojun case SIOCSIFPHYADDR_IN6: 67962587Sitojun src = (struct sockaddr *) 68062587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 68162587Sitojun dst = (struct sockaddr *) 68262587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 68362587Sitojun break; 68462587Sitojun#endif 68578064Sume case SIOCSLIFPHYADDR: 68678064Sume src = (struct sockaddr *) 68778064Sume &(((struct if_laddrreq *)data)->addr); 68878064Sume dst = (struct sockaddr *) 68978064Sume &(((struct if_laddrreq *)data)->dstaddr); 690105293Sume break; 69191327Sbrooks default: 692105293Sume return EINVAL; 69362587Sitojun } 69454263Sshin 69578064Sume /* sa_family must be equal */ 69678064Sume if (src->sa_family != dst->sa_family) 69778064Sume return EINVAL; 69878064Sume 69978064Sume /* validate sa_len */ 70078064Sume switch (src->sa_family) { 70178064Sume#ifdef INET 70278064Sume case AF_INET: 70378064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 70478064Sume return EINVAL; 70578064Sume break; 70678064Sume#endif 70778064Sume#ifdef INET6 70878064Sume case AF_INET6: 70978064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 71078064Sume return EINVAL; 71178064Sume break; 71278064Sume#endif 71378064Sume default: 71478064Sume return EAFNOSUPPORT; 71578064Sume } 71678064Sume switch (dst->sa_family) { 71778064Sume#ifdef INET 71878064Sume case AF_INET: 71978064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 72078064Sume return EINVAL; 72178064Sume break; 72278064Sume#endif 72378064Sume#ifdef INET6 72478064Sume case AF_INET6: 72578064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 72678064Sume return EINVAL; 72778064Sume break; 72878064Sume#endif 72978064Sume default: 73078064Sume return EAFNOSUPPORT; 73178064Sume } 73278064Sume 73378064Sume /* check sa_family looks sane for the cmd */ 73478064Sume switch (cmd) { 73578064Sume case SIOCSIFPHYADDR: 73678064Sume if (src->sa_family == AF_INET) 73778064Sume break; 73878064Sume return EAFNOSUPPORT; 73978064Sume#ifdef INET6 74078064Sume case SIOCSIFPHYADDR_IN6: 74178064Sume if (src->sa_family == AF_INET6) 74278064Sume break; 74378064Sume return EAFNOSUPPORT; 74478064Sume#endif /* INET6 */ 74578064Sume case SIOCSLIFPHYADDR: 74678064Sume /* checks done in the above */ 74778064Sume break; 74878064Sume } 74978064Sume 750147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 75162587Sitojun break; 75262587Sitojun 75362587Sitojun#ifdef SIOCDIFPHYADDR 75462587Sitojun case SIOCDIFPHYADDR: 755147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 75654263Sshin break; 75762587Sitojun#endif 75862587Sitojun 75954263Sshin case SIOCGIFPSRCADDR: 76054263Sshin#ifdef INET6 76154263Sshin case SIOCGIFPSRCADDR_IN6: 76254263Sshin#endif /* INET6 */ 76354263Sshin if (sc->gif_psrc == NULL) { 76454263Sshin error = EADDRNOTAVAIL; 76554263Sshin goto bad; 76654263Sshin } 76754263Sshin src = sc->gif_psrc; 76878064Sume switch (cmd) { 76954263Sshin#ifdef INET 77078064Sume case SIOCGIFPSRCADDR: 77154263Sshin dst = &ifr->ifr_addr; 77278064Sume size = sizeof(ifr->ifr_addr); 77354263Sshin break; 77454263Sshin#endif /* INET */ 77554263Sshin#ifdef INET6 77678064Sume case SIOCGIFPSRCADDR_IN6: 77754263Sshin dst = (struct sockaddr *) 77854263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 77978064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 78054263Sshin break; 78154263Sshin#endif /* INET6 */ 78254263Sshin default: 78354263Sshin error = EADDRNOTAVAIL; 78454263Sshin goto bad; 78554263Sshin } 78678064Sume if (src->sa_len > size) 78778064Sume return EINVAL; 78878064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 789148385Sume#ifdef INET6 790148385Sume if (dst->sa_family == AF_INET6) { 791148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 792148385Sume if (error != 0) 793148385Sume return (error); 794148385Sume } 795148385Sume#endif 79654263Sshin break; 79762587Sitojun 79854263Sshin case SIOCGIFPDSTADDR: 79954263Sshin#ifdef INET6 80054263Sshin case SIOCGIFPDSTADDR_IN6: 80154263Sshin#endif /* INET6 */ 80254263Sshin if (sc->gif_pdst == NULL) { 80354263Sshin error = EADDRNOTAVAIL; 80454263Sshin goto bad; 80554263Sshin } 80654263Sshin src = sc->gif_pdst; 80778064Sume switch (cmd) { 80854263Sshin#ifdef INET 80978064Sume case SIOCGIFPDSTADDR: 81054263Sshin dst = &ifr->ifr_addr; 81178064Sume size = sizeof(ifr->ifr_addr); 81254263Sshin break; 81354263Sshin#endif /* INET */ 81454263Sshin#ifdef INET6 81578064Sume case SIOCGIFPDSTADDR_IN6: 81654263Sshin dst = (struct sockaddr *) 81754263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 81878064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 81954263Sshin break; 82054263Sshin#endif /* INET6 */ 82154263Sshin default: 82254263Sshin error = EADDRNOTAVAIL; 82354263Sshin goto bad; 82454263Sshin } 82578064Sume if (src->sa_len > size) 82678064Sume return EINVAL; 82778064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 828148385Sume#ifdef INET6 829148385Sume if (dst->sa_family == AF_INET6) { 830148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 831148385Sume if (error != 0) 832148385Sume return (error); 833148385Sume } 834148385Sume#endif 83554263Sshin break; 83654263Sshin 83778064Sume case SIOCGLIFPHYADDR: 83878064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 83978064Sume error = EADDRNOTAVAIL; 84078064Sume goto bad; 84178064Sume } 84278064Sume 84378064Sume /* copy src */ 84478064Sume src = sc->gif_psrc; 84578064Sume dst = (struct sockaddr *) 84678064Sume &(((struct if_laddrreq *)data)->addr); 84778064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 84878064Sume if (src->sa_len > size) 84978064Sume return EINVAL; 85078064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 85178064Sume 85278064Sume /* copy dst */ 85378064Sume src = sc->gif_pdst; 85478064Sume dst = (struct sockaddr *) 85578064Sume &(((struct if_laddrreq *)data)->dstaddr); 85678064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 85778064Sume if (src->sa_len > size) 85878064Sume return EINVAL; 85978064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 86078064Sume break; 86178064Sume 86254263Sshin case SIOCSIFFLAGS: 86362587Sitojun /* if_ioctl() takes care of it */ 86454263Sshin break; 86554263Sshin 86654263Sshin default: 86754263Sshin error = EINVAL; 86854263Sshin break; 86954263Sshin } 87054263Sshin bad: 87154263Sshin return error; 87254263Sshin} 87379106Sbrooks 874127305Srwatson/* 875127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 876127305Srwatson * if a given tunnel is already present happens before we perform a 877127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 878127305Srwatson * re-ordered so that the check and replacement can be atomic using 879127305Srwatson * a mutex. 880127305Srwatson */ 881105293Sumeint 882105293Sumegif_set_tunnel(ifp, src, dst) 883105293Sume struct ifnet *ifp; 884105293Sume struct sockaddr *src; 885105293Sume struct sockaddr *dst; 886105293Sume{ 887183550Szec INIT_VNET_GIF(ifp->if_vnet); 888147256Sbrooks struct gif_softc *sc = ifp->if_softc; 889105293Sume struct gif_softc *sc2; 890105293Sume struct sockaddr *osrc, *odst, *sa; 891105293Sume int error = 0; 892105293Sume 893127305Srwatson mtx_lock(&gif_mtx); 894181803Sbz LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 895105293Sume if (sc2 == sc) 896105293Sume continue; 897105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 898105293Sume continue; 899105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 900105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 901105293Sume sc2->gif_psrc->sa_family != src->sa_family || 902105293Sume sc2->gif_psrc->sa_len != src->sa_len) 903105293Sume continue; 904105293Sume 905105293Sume /* 906105293Sume * Disallow parallel tunnels unless instructed 907105293Sume * otherwise. 908105293Sume */ 909181803Sbz if (!V_parallel_tunnels && 910105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 911105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 912105293Sume error = EADDRNOTAVAIL; 913127305Srwatson mtx_unlock(&gif_mtx); 914105293Sume goto bad; 915105293Sume } 916105293Sume 917105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 918105293Sume } 919127305Srwatson mtx_unlock(&gif_mtx); 920105293Sume 921105293Sume /* XXX we can detach from both, but be polite just in case */ 922105293Sume if (sc->gif_psrc) 923105293Sume switch (sc->gif_psrc->sa_family) { 924105293Sume#ifdef INET 925105293Sume case AF_INET: 926105293Sume (void)in_gif_detach(sc); 927105293Sume break; 928105293Sume#endif 929105293Sume#ifdef INET6 930105293Sume case AF_INET6: 931105293Sume (void)in6_gif_detach(sc); 932105293Sume break; 933105293Sume#endif 934105293Sume } 935105293Sume 936105293Sume osrc = sc->gif_psrc; 937111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 938105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 939105293Sume sc->gif_psrc = sa; 940105293Sume 941105293Sume odst = sc->gif_pdst; 942111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 943105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 944105293Sume sc->gif_pdst = sa; 945105293Sume 946105293Sume switch (sc->gif_psrc->sa_family) { 947105293Sume#ifdef INET 948105293Sume case AF_INET: 949105293Sume error = in_gif_attach(sc); 950105293Sume break; 951105293Sume#endif 952105293Sume#ifdef INET6 953105293Sume case AF_INET6: 954148385Sume /* 955148385Sume * Check validity of the scope zone ID of the addresses, and 956148385Sume * convert it into the kernel internal form if necessary. 957148385Sume */ 958148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 959148385Sume if (error != 0) 960148385Sume break; 961148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 962148385Sume if (error != 0) 963148385Sume break; 964105293Sume error = in6_gif_attach(sc); 965105293Sume break; 966105293Sume#endif 967105293Sume } 968105293Sume if (error) { 969105293Sume /* rollback */ 970105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 971105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 972105293Sume sc->gif_psrc = osrc; 973105293Sume sc->gif_pdst = odst; 974105293Sume goto bad; 975105293Sume } 976105293Sume 977105293Sume if (osrc) 978105293Sume free((caddr_t)osrc, M_IFADDR); 979105293Sume if (odst) 980105293Sume free((caddr_t)odst, M_IFADDR); 981105293Sume 982105293Sume bad: 983105293Sume if (sc->gif_psrc && sc->gif_pdst) 984148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 985105293Sume else 986148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 987105293Sume 988105293Sume return error; 989105293Sume} 990105293Sume 99179106Sbrooksvoid 992105293Sumegif_delete_tunnel(ifp) 993105293Sume struct ifnet *ifp; 99479106Sbrooks{ 995147256Sbrooks struct gif_softc *sc = ifp->if_softc; 99679106Sbrooks 99779106Sbrooks if (sc->gif_psrc) { 99879106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 99979106Sbrooks sc->gif_psrc = NULL; 100079106Sbrooks } 100179106Sbrooks if (sc->gif_pdst) { 100279106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 100379106Sbrooks sc->gif_pdst = NULL; 100479106Sbrooks } 1005105293Sume /* it is safe to detach from both */ 1006105293Sume#ifdef INET 1007105293Sume (void)in_gif_detach(sc); 1008105293Sume#endif 1009105293Sume#ifdef INET6 1010105293Sume (void)in6_gif_detach(sc); 1011105293Sume#endif 1012160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 101379106Sbrooks} 1014