if_gif.c revision 190787
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 190787 2009-04-06 22:29:41Z 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 *); 124190787Szecstatic int vnet_gif_iattach(const void *); 12579106Sbrooks 126130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 12779106Sbrooks 12892725Salfredstatic int gifmodevent(module_t, int, void *); 12979106Sbrooks 13091270SbrooksSYSCTL_DECL(_net_link); 13191270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 13291270Sbrooks "Generic Tunnel Interface"); 13362587Sitojun#ifndef MAX_GIF_NEST 13462587Sitojun/* 13591270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 13662587Sitojun * Since, setting a large value to this macro with a careless configuration 13762587Sitojun * may introduce system crash, we don't allow any nestings by default. 13862587Sitojun * If you need to configure nested gif tunnels, you can define this macro 13995023Ssuz * in your kernel configuration file. However, if you do so, please be 14062587Sitojun * careful to configure the tunnels so that it won't make a loop. 14162587Sitojun */ 14262587Sitojun#define MAX_GIF_NEST 1 14362587Sitojun#endif 144183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting, 145183550Szec CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels"); 14662587Sitojun 147183550Szec#ifdef INET6 148183550SzecSYSCTL_DECL(_net_inet6_ip6); 149183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM, 150183550Szec gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, ""); 151183550Szec#endif 152183550Szec 15391270Sbrooks/* 15491270Sbrooks * By default, we disallow creation of multiple tunnels between the same 15591270Sbrooks * pair of addresses. Some applications require this functionality so 15691270Sbrooks * we allow control over this check here. 15791270Sbrooks */ 158183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels, 159183550Szec CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?"); 16091270Sbrooks 161176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 162176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 163176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 164176879Sthompsa#ifndef ETHER_IS_BROADCAST 165176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 166176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 167176879Sthompsa#endif 168176879Sthompsa 169128209Sbrooksstatic int 170160195Ssamgif_clone_create(ifc, unit, params) 17179106Sbrooks struct if_clone *ifc; 17292081Smux int unit; 173160195Ssam caddr_t params; 17454263Sshin{ 175183550Szec INIT_VNET_GIF(curvnet); 17678064Sume struct gif_softc *sc; 17754263Sshin 178131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 179178888Sjulian sc->gif_fibnum = curthread->td_proc->p_fibnum; 180147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 181147256Sbrooks if (GIF2IFP(sc) == NULL) { 182147256Sbrooks free(sc, M_GIF); 183147256Sbrooks return (ENOSPC); 184147256Sbrooks } 18579106Sbrooks 186155037Sglebius GIF_LOCK_INIT(sc); 187155037Sglebius 188147256Sbrooks GIF2IFP(sc)->if_softc = sc; 189147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 19079106Sbrooks 19179106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 19262587Sitojun 193147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 194147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 195147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 19678064Sume#if 0 19779106Sbrooks /* turn off ingress filter */ 198147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 19978064Sume#endif 200147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 201153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 202147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 203147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 204147256Sbrooks if_attach(GIF2IFP(sc)); 205147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 20683998Sbrooks if (ng_gif_attach_p != NULL) 207147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 208155037Sglebius 209155037Sglebius mtx_lock(&gif_mtx); 210181803Sbz LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 211155037Sglebius mtx_unlock(&gif_mtx); 212155037Sglebius 213155037Sglebius return (0); 21479106Sbrooks} 21579106Sbrooks 216127305Srwatsonstatic void 217151266Sthompsagif_clone_destroy(ifp) 218151266Sthompsa struct ifnet *ifp; 21979106Sbrooks{ 220184678Sbz#if defined(INET) || defined(INET6) 22179106Sbrooks int err; 222184678Sbz#endif 223151266Sthompsa struct gif_softc *sc = ifp->if_softc; 22479106Sbrooks 225151266Sthompsa mtx_lock(&gif_mtx); 226151266Sthompsa LIST_REMOVE(sc, gif_list); 227151266Sthompsa mtx_unlock(&gif_mtx); 228151266Sthompsa 229127305Srwatson gif_delete_tunnel(ifp); 230105293Sume#ifdef INET6 231105293Sume if (sc->encap_cookie6 != NULL) { 232105293Sume err = encap_detach(sc->encap_cookie6); 233105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 234105293Sume } 235105293Sume#endif 236105293Sume#ifdef INET 23779106Sbrooks if (sc->encap_cookie4 != NULL) { 23879106Sbrooks err = encap_detach(sc->encap_cookie4); 23979106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 24079106Sbrooks } 241105293Sume#endif 24279106Sbrooks 24383998Sbrooks if (ng_gif_detach_p != NULL) 24483998Sbrooks (*ng_gif_detach_p)(ifp); 24579106Sbrooks bpfdetach(ifp); 24679106Sbrooks if_detach(ifp); 247147256Sbrooks if_free(ifp); 24879106Sbrooks 249155037Sglebius GIF_LOCK_DESTROY(sc); 250155037Sglebius 25179106Sbrooks free(sc, M_GIF); 25279106Sbrooks} 25379106Sbrooks 25479106Sbrooksstatic int 255190787Szecvnet_gif_iattach(const void *unused __unused) 256190787Szec{ 257190787Szec INIT_VNET_GIF(curvnet); 258190787Szec 259190787Szec LIST_INIT(&V_gif_softc_list); 260190787Szec V_max_gif_nesting = MAX_GIF_NEST; 261190787Szec#ifdef XBONEHACK 262190787Szec V_parallel_tunnels = 1; 263190787Szec#else 264190787Szec V_parallel_tunnels = 0; 265190787Szec#endif 266190787Szec V_ip_gif_ttl = GIF_TTL; 267190787Szec#ifdef INET6 268190787Szec V_ip6_gif_hlim = GIF_HLIM; 269190787Szec#endif 270190787Szec 271190787Szec return (0); 272190787Szec} 273190787Szec 274190787Szecstatic int 27579106Sbrooksgifmodevent(mod, type, data) 27679106Sbrooks module_t mod; 27779106Sbrooks int type; 27879106Sbrooks void *data; 27979106Sbrooks{ 28079106Sbrooks 28179106Sbrooks switch (type) { 28279106Sbrooks case MOD_LOAD: 283127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 284185088Szec 285190787Szec vnet_gif_iattach(NULL); 286185088Szec if_clone_attach(&gif_cloner); 28779106Sbrooks 28879106Sbrooks break; 28979106Sbrooks case MOD_UNLOAD: 29079106Sbrooks if_clone_detach(&gif_cloner); 291127305Srwatson mtx_destroy(&gif_mtx); 29279106Sbrooks#ifdef INET6 293190787Szec V_ip6_gif_hlim = 0; /* XXX -> vnet_gif_idetach() */ 29462587Sitojun#endif 29579106Sbrooks break; 296132199Sphk default: 297132199Sphk return EOPNOTSUPP; 29854263Sshin } 29979106Sbrooks return 0; 30054263Sshin} 30154263Sshin 30279106Sbrooksstatic moduledata_t gif_mod = { 30379106Sbrooks "if_gif", 30479106Sbrooks gifmodevent, 30579106Sbrooks 0 30679106Sbrooks}; 30754263Sshin 30879106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 30983997SbrooksMODULE_VERSION(if_gif, 1); 31079106Sbrooks 311105293Sumeint 31262587Sitojungif_encapcheck(m, off, proto, arg) 31362587Sitojun const struct mbuf *m; 31462587Sitojun int off; 31562587Sitojun int proto; 31662587Sitojun void *arg; 31762587Sitojun{ 31862587Sitojun struct ip ip; 31962587Sitojun struct gif_softc *sc; 32062587Sitojun 32162587Sitojun sc = (struct gif_softc *)arg; 32262587Sitojun if (sc == NULL) 32362587Sitojun return 0; 32462587Sitojun 325147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 32662587Sitojun return 0; 32762587Sitojun 32862587Sitojun /* no physical address */ 32962587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 33062587Sitojun return 0; 33162587Sitojun 33262587Sitojun switch (proto) { 33362587Sitojun#ifdef INET 33462587Sitojun case IPPROTO_IPV4: 33562587Sitojun break; 33662587Sitojun#endif 33762587Sitojun#ifdef INET6 33862587Sitojun case IPPROTO_IPV6: 33962587Sitojun break; 34062587Sitojun#endif 341153621Sthompsa case IPPROTO_ETHERIP: 342153621Sthompsa break; 343153621Sthompsa 34462587Sitojun default: 34562587Sitojun return 0; 34662587Sitojun } 34762587Sitojun 348105339Sume /* Bail on short packets */ 349105339Sume if (m->m_pkthdr.len < sizeof(ip)) 350105339Sume return 0; 351105339Sume 35291327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 35362587Sitojun 35462587Sitojun switch (ip.ip_v) { 35562587Sitojun#ifdef INET 35662587Sitojun case 4: 35762587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 35862587Sitojun sc->gif_pdst->sa_family != AF_INET) 35962587Sitojun return 0; 36062587Sitojun return gif_encapcheck4(m, off, proto, arg); 36162587Sitojun#endif 36262587Sitojun#ifdef INET6 36362587Sitojun case 6: 364105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 365105293Sume return 0; 36662587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 36762587Sitojun sc->gif_pdst->sa_family != AF_INET6) 36862587Sitojun return 0; 36962587Sitojun return gif_encapcheck6(m, off, proto, arg); 37062587Sitojun#endif 37162587Sitojun default: 37262587Sitojun return 0; 37362587Sitojun } 37462587Sitojun} 37562587Sitojun 376153621Sthompsastatic void 377153621Sthompsagif_start(struct ifnet *ifp) 378153621Sthompsa{ 379153621Sthompsa struct gif_softc *sc; 380153621Sthompsa struct mbuf *m; 381153621Sthompsa 382153621Sthompsa sc = ifp->if_softc; 383153621Sthompsa 384153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 385153621Sthompsa for (;;) { 386153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 387153621Sthompsa if (m == 0) 388153621Sthompsa break; 389153621Sthompsa 390153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 391153621Sthompsa 392153621Sthompsa } 393153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 394153621Sthompsa 395153621Sthompsa return; 396153621Sthompsa} 397153621Sthompsa 39854263Sshinint 39954263Sshingif_output(ifp, m, dst, rt) 40054263Sshin struct ifnet *ifp; 40154263Sshin struct mbuf *m; 40254263Sshin struct sockaddr *dst; 40354263Sshin struct rtentry *rt; /* added in net2 */ 40454263Sshin{ 405183550Szec INIT_VNET_GIF(ifp->if_vnet); 406147256Sbrooks struct gif_softc *sc = ifp->if_softc; 407127898Sru struct m_tag *mtag; 40854263Sshin int error = 0; 409127898Sru int gif_called; 410147611Sdwmalone u_int32_t af; 41154263Sshin 412101182Srwatson#ifdef MAC 413172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 414101739Srwatson if (error) { 415101739Srwatson m_freem(m); 416101739Srwatson goto end; 417101739Srwatson } 418101182Srwatson#endif 419101182Srwatson 42054263Sshin /* 42154263Sshin * gif may cause infinite recursion calls when misconfigured. 422127898Sru * We'll prevent this by detecting loops. 423127898Sru * 424127898Sru * High nesting level may cause stack exhaustion. 42554263Sshin * We'll prevent this by introducing upper limit. 42654263Sshin */ 427127898Sru gif_called = 1; 428127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 429127898Sru while (mtag != NULL) { 430127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 431127898Sru log(LOG_NOTICE, 432127898Sru "gif_output: loop detected on %s\n", 433127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 434127898Sru m_freem(m); 435127898Sru error = EIO; /* is there better errno? */ 436127898Sru goto end; 437127898Sru } 438127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 439127898Sru gif_called++; 440127898Sru } 441181803Sbz if (gif_called > V_max_gif_nesting) { 44254263Sshin log(LOG_NOTICE, 44354263Sshin "gif_output: recursively called too many times(%d)\n", 444127303Srwatson gif_called); 44554263Sshin m_freem(m); 44654263Sshin error = EIO; /* is there better errno? */ 44754263Sshin goto end; 44854263Sshin } 449127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 450127898Sru M_NOWAIT); 451127898Sru if (mtag == NULL) { 452127898Sru m_freem(m); 453127898Sru error = ENOMEM; 454127898Sru goto end; 455127898Sru } 456127898Sru *(struct ifnet **)(mtag + 1) = ifp; 457127898Sru m_tag_prepend(m, mtag); 45862587Sitojun 45954263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 460155037Sglebius 461155037Sglebius GIF_LOCK(sc); 462155037Sglebius 46354263Sshin if (!(ifp->if_flags & IFF_UP) || 46454263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 465159174Sglebius GIF_UNLOCK(sc); 46654263Sshin m_freem(m); 46754263Sshin error = ENETDOWN; 46854263Sshin goto end; 46954263Sshin } 47054263Sshin 471147611Sdwmalone /* BPF writes need to be handled specially. */ 472147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 473147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 474147611Sdwmalone dst->sa_family = af; 475147611Sdwmalone } 476147611Sdwmalone 477153621Sthompsa af = dst->sa_family; 478159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 47962587Sitojun ifp->if_opackets++; 48054263Sshin ifp->if_obytes += m->m_pkthdr.len; 48154263Sshin 482153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 483153621Sthompsa if (ifp->if_bridge) 484153621Sthompsa af = AF_LINK; 485153621Sthompsa 486178888Sjulian M_SETFIB(m, sc->gif_fibnum); 48778064Sume /* inner AF-specific encapsulation */ 48878064Sume 48962587Sitojun /* XXX should we check if our outer source is legal? */ 49062587Sitojun 49178064Sume /* dispatch to output logic based on outer AF */ 49254263Sshin switch (sc->gif_psrc->sa_family) { 49354263Sshin#ifdef INET 49454263Sshin case AF_INET: 495153621Sthompsa error = in_gif_output(ifp, af, m); 49654263Sshin break; 49754263Sshin#endif 49854263Sshin#ifdef INET6 49954263Sshin case AF_INET6: 500153621Sthompsa error = in6_gif_output(ifp, af, m); 50154263Sshin break; 50254263Sshin#endif 50354263Sshin default: 50462587Sitojun m_freem(m); 50554263Sshin error = ENETDOWN; 50654263Sshin } 50754263Sshin 508159174Sglebius GIF_UNLOCK(sc); 50954263Sshin end: 51078064Sume if (error) 51178064Sume ifp->if_oerrors++; 512155037Sglebius return (error); 51354263Sshin} 51454263Sshin 51554263Sshinvoid 516105338Sumegif_input(m, af, ifp) 51754263Sshin struct mbuf *m; 51854263Sshin int af; 519105338Sume struct ifnet *ifp; 52054263Sshin{ 521153621Sthompsa int isr, n; 522153621Sthompsa struct etherip_header *eip; 523176879Sthompsa struct ether_header *eh; 524176879Sthompsa struct ifnet *oldifp; 52554263Sshin 526105338Sume if (ifp == NULL) { 52754263Sshin /* just in case */ 52854263Sshin m_freem(m); 52954263Sshin return; 53054263Sshin } 53154263Sshin 532105338Sume m->m_pkthdr.rcvif = ifp; 533101182Srwatson 534101182Srwatson#ifdef MAC 535172930Srwatson mac_ifnet_create_mbuf(ifp, m); 536101182Srwatson#endif 537101182Srwatson 538159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 53978064Sume u_int32_t af1 = af; 540123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 54154263Sshin } 54254263Sshin 54383998Sbrooks if (ng_gif_input_p != NULL) { 544105338Sume (*ng_gif_input_p)(ifp, &m, af); 54583998Sbrooks if (m == NULL) 54683998Sbrooks return; 54783998Sbrooks } 54883998Sbrooks 54954263Sshin /* 55054263Sshin * Put the packet to the network layer input queue according to the 55154263Sshin * specified address family. 55254263Sshin * Note: older versions of gif_input directly called network layer 55395023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 55454263Sshin * prevent too many recursive calls of such input functions, which 55595023Ssuz * might cause kernel panic. But the change may introduce another 55654263Sshin * problem; if the input queue is full, packets are discarded. 55795023Ssuz * The kernel stack overflow really happened, and we believed 55895023Ssuz * queue-full rarely occurs, so we changed the policy. 55954263Sshin */ 56054263Sshin switch (af) { 56154263Sshin#ifdef INET 56254263Sshin case AF_INET: 56354263Sshin isr = NETISR_IP; 56454263Sshin break; 56554263Sshin#endif 56654263Sshin#ifdef INET6 56754263Sshin case AF_INET6: 56854263Sshin isr = NETISR_IPV6; 56954263Sshin break; 57054263Sshin#endif 571153621Sthompsa case AF_LINK: 572153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 573153621Sthompsa if (n > m->m_len) { 574153621Sthompsa m = m_pullup(m, n); 575153621Sthompsa if (m == NULL) { 576153621Sthompsa ifp->if_ierrors++; 577153621Sthompsa return; 578153621Sthompsa } 579153621Sthompsa } 580153621Sthompsa 581153621Sthompsa eip = mtod(m, struct etherip_header *); 582153621Sthompsa if (eip->eip_ver != 583153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 584153621Sthompsa /* discard unknown versions */ 585153621Sthompsa m_freem(m); 586153621Sthompsa return; 587153621Sthompsa } 588153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 589153621Sthompsa 590153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 591153621Sthompsa m->m_pkthdr.rcvif = ifp; 592153621Sthompsa 593176879Sthompsa if (ifp->if_bridge) { 594176879Sthompsa oldifp = ifp; 595176879Sthompsa eh = mtod(m, struct ether_header *); 596176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 597176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 598176879Sthompsa m->m_flags |= M_BCAST; 599176879Sthompsa else 600176879Sthompsa m->m_flags |= M_MCAST; 601176879Sthompsa ifp->if_imcasts++; 602176879Sthompsa } 603153621Sthompsa BRIDGE_INPUT(ifp, m); 604176879Sthompsa 605176879Sthompsa if (m != NULL && ifp != oldifp) { 606176879Sthompsa /* 607176879Sthompsa * The bridge gave us back itself or one of the 608176879Sthompsa * members for which the frame is addressed. 609176879Sthompsa */ 610176879Sthompsa ether_demux(ifp, m); 611176879Sthompsa return; 612176879Sthompsa } 613176879Sthompsa } 614153621Sthompsa if (m != NULL) 615153621Sthompsa m_freem(m); 616153621Sthompsa return; 617153621Sthompsa 61854263Sshin default: 61983998Sbrooks if (ng_gif_input_orphan_p != NULL) 620105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 62183998Sbrooks else 62283998Sbrooks m_freem(m); 62354263Sshin return; 62454263Sshin } 62554263Sshin 626105338Sume ifp->if_ipackets++; 627105338Sume ifp->if_ibytes += m->m_pkthdr.len; 628111888Sjlemon netisr_dispatch(isr, m); 62954263Sshin} 63054263Sshin 63162587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 63254263Sshinint 63354263Sshingif_ioctl(ifp, cmd, data) 63454263Sshin struct ifnet *ifp; 63554263Sshin u_long cmd; 63654263Sshin caddr_t data; 63754263Sshin{ 638147256Sbrooks struct gif_softc *sc = ifp->if_softc; 63954263Sshin struct ifreq *ifr = (struct ifreq*)data; 64054263Sshin int error = 0, size; 64162587Sitojun struct sockaddr *dst, *src; 642105339Sume#ifdef SIOCSIFMTU /* xxx */ 643105339Sume u_long mtu; 644105339Sume#endif 645105339Sume 64654263Sshin switch (cmd) { 64754263Sshin case SIOCSIFADDR: 648105293Sume ifp->if_flags |= IFF_UP; 64954263Sshin break; 65062587Sitojun 65154263Sshin case SIOCSIFDSTADDR: 65254263Sshin break; 65354263Sshin 65454263Sshin case SIOCADDMULTI: 65554263Sshin case SIOCDELMULTI: 65654263Sshin break; 65754263Sshin 65862587Sitojun#ifdef SIOCSIFMTU /* xxx */ 65954263Sshin case SIOCGIFMTU: 66054263Sshin break; 66162587Sitojun 66254263Sshin case SIOCSIFMTU: 663105339Sume mtu = ifr->ifr_mtu; 664105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 665105339Sume return (EINVAL); 666105339Sume ifp->if_mtu = mtu; 66754263Sshin break; 66862587Sitojun#endif /* SIOCSIFMTU */ 66954263Sshin 670105339Sume#ifdef INET 67154263Sshin case SIOCSIFPHYADDR: 672105339Sume#endif 67354263Sshin#ifdef INET6 67454263Sshin case SIOCSIFPHYADDR_IN6: 67554263Sshin#endif /* INET6 */ 67678064Sume case SIOCSLIFPHYADDR: 67762587Sitojun switch (cmd) { 67878064Sume#ifdef INET 67962587Sitojun case SIOCSIFPHYADDR: 68054263Sshin src = (struct sockaddr *) 68154263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 68254263Sshin dst = (struct sockaddr *) 68354263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 68462587Sitojun break; 68578064Sume#endif 68662587Sitojun#ifdef INET6 68762587Sitojun case SIOCSIFPHYADDR_IN6: 68862587Sitojun src = (struct sockaddr *) 68962587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 69062587Sitojun dst = (struct sockaddr *) 69162587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 69262587Sitojun break; 69362587Sitojun#endif 69478064Sume case SIOCSLIFPHYADDR: 69578064Sume src = (struct sockaddr *) 69678064Sume &(((struct if_laddrreq *)data)->addr); 69778064Sume dst = (struct sockaddr *) 69878064Sume &(((struct if_laddrreq *)data)->dstaddr); 699105293Sume break; 70091327Sbrooks default: 701105293Sume return EINVAL; 70262587Sitojun } 70354263Sshin 70478064Sume /* sa_family must be equal */ 70578064Sume if (src->sa_family != dst->sa_family) 70678064Sume return EINVAL; 70778064Sume 70878064Sume /* validate sa_len */ 70978064Sume switch (src->sa_family) { 71078064Sume#ifdef INET 71178064Sume case AF_INET: 71278064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 71378064Sume return EINVAL; 71478064Sume break; 71578064Sume#endif 71678064Sume#ifdef INET6 71778064Sume case AF_INET6: 71878064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 71978064Sume return EINVAL; 72078064Sume break; 72178064Sume#endif 72278064Sume default: 72378064Sume return EAFNOSUPPORT; 72478064Sume } 72578064Sume switch (dst->sa_family) { 72678064Sume#ifdef INET 72778064Sume case AF_INET: 72878064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 72978064Sume return EINVAL; 73078064Sume break; 73178064Sume#endif 73278064Sume#ifdef INET6 73378064Sume case AF_INET6: 73478064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 73578064Sume return EINVAL; 73678064Sume break; 73778064Sume#endif 73878064Sume default: 73978064Sume return EAFNOSUPPORT; 74078064Sume } 74178064Sume 74278064Sume /* check sa_family looks sane for the cmd */ 74378064Sume switch (cmd) { 74478064Sume case SIOCSIFPHYADDR: 74578064Sume if (src->sa_family == AF_INET) 74678064Sume break; 74778064Sume return EAFNOSUPPORT; 74878064Sume#ifdef INET6 74978064Sume case SIOCSIFPHYADDR_IN6: 75078064Sume if (src->sa_family == AF_INET6) 75178064Sume break; 75278064Sume return EAFNOSUPPORT; 75378064Sume#endif /* INET6 */ 75478064Sume case SIOCSLIFPHYADDR: 75578064Sume /* checks done in the above */ 75678064Sume break; 75778064Sume } 75878064Sume 759147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 76062587Sitojun break; 76162587Sitojun 76262587Sitojun#ifdef SIOCDIFPHYADDR 76362587Sitojun case SIOCDIFPHYADDR: 764147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 76554263Sshin break; 76662587Sitojun#endif 76762587Sitojun 76854263Sshin case SIOCGIFPSRCADDR: 76954263Sshin#ifdef INET6 77054263Sshin case SIOCGIFPSRCADDR_IN6: 77154263Sshin#endif /* INET6 */ 77254263Sshin if (sc->gif_psrc == NULL) { 77354263Sshin error = EADDRNOTAVAIL; 77454263Sshin goto bad; 77554263Sshin } 77654263Sshin src = sc->gif_psrc; 77778064Sume switch (cmd) { 77854263Sshin#ifdef INET 77978064Sume case SIOCGIFPSRCADDR: 78054263Sshin dst = &ifr->ifr_addr; 78178064Sume size = sizeof(ifr->ifr_addr); 78254263Sshin break; 78354263Sshin#endif /* INET */ 78454263Sshin#ifdef INET6 78578064Sume case SIOCGIFPSRCADDR_IN6: 78654263Sshin dst = (struct sockaddr *) 78754263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 78878064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 78954263Sshin break; 79054263Sshin#endif /* INET6 */ 79154263Sshin default: 79254263Sshin error = EADDRNOTAVAIL; 79354263Sshin goto bad; 79454263Sshin } 79578064Sume if (src->sa_len > size) 79678064Sume return EINVAL; 79778064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 798148385Sume#ifdef INET6 799148385Sume if (dst->sa_family == AF_INET6) { 800148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 801148385Sume if (error != 0) 802148385Sume return (error); 803148385Sume } 804148385Sume#endif 80554263Sshin break; 80662587Sitojun 80754263Sshin case SIOCGIFPDSTADDR: 80854263Sshin#ifdef INET6 80954263Sshin case SIOCGIFPDSTADDR_IN6: 81054263Sshin#endif /* INET6 */ 81154263Sshin if (sc->gif_pdst == NULL) { 81254263Sshin error = EADDRNOTAVAIL; 81354263Sshin goto bad; 81454263Sshin } 81554263Sshin src = sc->gif_pdst; 81678064Sume switch (cmd) { 81754263Sshin#ifdef INET 81878064Sume case SIOCGIFPDSTADDR: 81954263Sshin dst = &ifr->ifr_addr; 82078064Sume size = sizeof(ifr->ifr_addr); 82154263Sshin break; 82254263Sshin#endif /* INET */ 82354263Sshin#ifdef INET6 82478064Sume case SIOCGIFPDSTADDR_IN6: 82554263Sshin dst = (struct sockaddr *) 82654263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 82778064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 82854263Sshin break; 82954263Sshin#endif /* INET6 */ 83054263Sshin default: 83154263Sshin error = EADDRNOTAVAIL; 83254263Sshin goto bad; 83354263Sshin } 83478064Sume if (src->sa_len > size) 83578064Sume return EINVAL; 83678064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 837148385Sume#ifdef INET6 838148385Sume if (dst->sa_family == AF_INET6) { 839148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 840148385Sume if (error != 0) 841148385Sume return (error); 842148385Sume } 843148385Sume#endif 84454263Sshin break; 84554263Sshin 84678064Sume case SIOCGLIFPHYADDR: 84778064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 84878064Sume error = EADDRNOTAVAIL; 84978064Sume goto bad; 85078064Sume } 85178064Sume 85278064Sume /* copy src */ 85378064Sume src = sc->gif_psrc; 85478064Sume dst = (struct sockaddr *) 85578064Sume &(((struct if_laddrreq *)data)->addr); 85678064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 85778064Sume if (src->sa_len > size) 85878064Sume return EINVAL; 85978064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 86078064Sume 86178064Sume /* copy dst */ 86278064Sume src = sc->gif_pdst; 86378064Sume dst = (struct sockaddr *) 86478064Sume &(((struct if_laddrreq *)data)->dstaddr); 86578064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 86678064Sume if (src->sa_len > size) 86778064Sume return EINVAL; 86878064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 86978064Sume break; 87078064Sume 87154263Sshin case SIOCSIFFLAGS: 87262587Sitojun /* if_ioctl() takes care of it */ 87354263Sshin break; 87454263Sshin 87554263Sshin default: 87654263Sshin error = EINVAL; 87754263Sshin break; 87854263Sshin } 87954263Sshin bad: 88054263Sshin return error; 88154263Sshin} 88279106Sbrooks 883127305Srwatson/* 884127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 885127305Srwatson * if a given tunnel is already present happens before we perform a 886127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 887127305Srwatson * re-ordered so that the check and replacement can be atomic using 888127305Srwatson * a mutex. 889127305Srwatson */ 890105293Sumeint 891105293Sumegif_set_tunnel(ifp, src, dst) 892105293Sume struct ifnet *ifp; 893105293Sume struct sockaddr *src; 894105293Sume struct sockaddr *dst; 895105293Sume{ 896183550Szec INIT_VNET_GIF(ifp->if_vnet); 897147256Sbrooks struct gif_softc *sc = ifp->if_softc; 898105293Sume struct gif_softc *sc2; 899105293Sume struct sockaddr *osrc, *odst, *sa; 900105293Sume int error = 0; 901105293Sume 902127305Srwatson mtx_lock(&gif_mtx); 903181803Sbz LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 904105293Sume if (sc2 == sc) 905105293Sume continue; 906105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 907105293Sume continue; 908105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 909105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 910105293Sume sc2->gif_psrc->sa_family != src->sa_family || 911105293Sume sc2->gif_psrc->sa_len != src->sa_len) 912105293Sume continue; 913105293Sume 914105293Sume /* 915105293Sume * Disallow parallel tunnels unless instructed 916105293Sume * otherwise. 917105293Sume */ 918181803Sbz if (!V_parallel_tunnels && 919105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 920105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 921105293Sume error = EADDRNOTAVAIL; 922127305Srwatson mtx_unlock(&gif_mtx); 923105293Sume goto bad; 924105293Sume } 925105293Sume 926105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 927105293Sume } 928127305Srwatson mtx_unlock(&gif_mtx); 929105293Sume 930105293Sume /* XXX we can detach from both, but be polite just in case */ 931105293Sume if (sc->gif_psrc) 932105293Sume switch (sc->gif_psrc->sa_family) { 933105293Sume#ifdef INET 934105293Sume case AF_INET: 935105293Sume (void)in_gif_detach(sc); 936105293Sume break; 937105293Sume#endif 938105293Sume#ifdef INET6 939105293Sume case AF_INET6: 940105293Sume (void)in6_gif_detach(sc); 941105293Sume break; 942105293Sume#endif 943105293Sume } 944105293Sume 945105293Sume osrc = sc->gif_psrc; 946111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 947105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 948105293Sume sc->gif_psrc = sa; 949105293Sume 950105293Sume odst = sc->gif_pdst; 951111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 952105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 953105293Sume sc->gif_pdst = sa; 954105293Sume 955105293Sume switch (sc->gif_psrc->sa_family) { 956105293Sume#ifdef INET 957105293Sume case AF_INET: 958105293Sume error = in_gif_attach(sc); 959105293Sume break; 960105293Sume#endif 961105293Sume#ifdef INET6 962105293Sume case AF_INET6: 963148385Sume /* 964148385Sume * Check validity of the scope zone ID of the addresses, and 965148385Sume * convert it into the kernel internal form if necessary. 966148385Sume */ 967148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 968148385Sume if (error != 0) 969148385Sume break; 970148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 971148385Sume if (error != 0) 972148385Sume break; 973105293Sume error = in6_gif_attach(sc); 974105293Sume break; 975105293Sume#endif 976105293Sume } 977105293Sume if (error) { 978105293Sume /* rollback */ 979105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 980105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 981105293Sume sc->gif_psrc = osrc; 982105293Sume sc->gif_pdst = odst; 983105293Sume goto bad; 984105293Sume } 985105293Sume 986105293Sume if (osrc) 987105293Sume free((caddr_t)osrc, M_IFADDR); 988105293Sume if (odst) 989105293Sume free((caddr_t)odst, M_IFADDR); 990105293Sume 991105293Sume bad: 992105293Sume if (sc->gif_psrc && sc->gif_pdst) 993148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 994105293Sume else 995148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 996105293Sume 997105293Sume return error; 998105293Sume} 999105293Sume 100079106Sbrooksvoid 1001105293Sumegif_delete_tunnel(ifp) 1002105293Sume struct ifnet *ifp; 100379106Sbrooks{ 1004147256Sbrooks struct gif_softc *sc = ifp->if_softc; 100579106Sbrooks 100679106Sbrooks if (sc->gif_psrc) { 100779106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 100879106Sbrooks sc->gif_psrc = NULL; 100979106Sbrooks } 101079106Sbrooks if (sc->gif_pdst) { 101179106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 101279106Sbrooks sc->gif_pdst = NULL; 101379106Sbrooks } 1014105293Sume /* it is safe to detach from both */ 1015105293Sume#ifdef INET 1016105293Sume (void)in_gif_detach(sc); 1017105293Sume#endif 1018105293Sume#ifdef INET6 1019105293Sume (void)in6_gif_detach(sc); 1020105293Sume#endif 1021160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 102279106Sbrooks} 1023