if_gif.c revision 215701
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 215701 2010-11-22 19:32:54Z dim $ */ 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" 3554263Sshin 3654263Sshin#include <sys/param.h> 3754263Sshin#include <sys/systm.h> 3854263Sshin#include <sys/kernel.h> 3954263Sshin#include <sys/malloc.h> 4054263Sshin#include <sys/mbuf.h> 41129880Sphk#include <sys/module.h> 4254263Sshin#include <sys/socket.h> 4354263Sshin#include <sys/sockio.h> 4454263Sshin#include <sys/errno.h> 4554263Sshin#include <sys/time.h> 4691270Sbrooks#include <sys/sysctl.h> 4754263Sshin#include <sys/syslog.h> 48193664Shrs#include <sys/priv.h> 49178888Sjulian#include <sys/proc.h> 5062587Sitojun#include <sys/protosw.h> 5179106Sbrooks#include <sys/conf.h> 5254263Sshin#include <machine/cpu.h> 5354263Sshin 5454263Sshin#include <net/if.h> 55130933Sbrooks#include <net/if_clone.h> 5654263Sshin#include <net/if_types.h> 5754263Sshin#include <net/netisr.h> 5854263Sshin#include <net/route.h> 5954263Sshin#include <net/bpf.h> 60196019Srwatson#include <net/vnet.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"); 97215701Sdimstatic VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 98195727Srwatson#define V_gif_softc_list VNET(gif_softc_list) 99195699Srwatson 10083998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10183998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10283998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 10383998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 10483998Sbrooks 105153621Sthompsastatic void gif_start(struct ifnet *); 106160195Ssamstatic int gif_clone_create(struct if_clone *, int, caddr_t); 107128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 10879106Sbrooks 109130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 11079106Sbrooks 11192725Salfredstatic int gifmodevent(module_t, int, void *); 11279106Sbrooks 11391270SbrooksSYSCTL_DECL(_net_link); 11491270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 11591270Sbrooks "Generic Tunnel Interface"); 11662587Sitojun#ifndef MAX_GIF_NEST 11762587Sitojun/* 11891270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 11962587Sitojun * Since, setting a large value to this macro with a careless configuration 12062587Sitojun * may introduce system crash, we don't allow any nestings by default. 12162587Sitojun * If you need to configure nested gif tunnels, you can define this macro 12295023Ssuz * in your kernel configuration file. However, if you do so, please be 12362587Sitojun * careful to configure the tunnels so that it won't make a loop. 12462587Sitojun */ 12562587Sitojun#define MAX_GIF_NEST 1 12662587Sitojun#endif 127215701Sdimstatic VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 128195837Srwatson#define V_max_gif_nesting VNET(max_gif_nesting) 129195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 130195699Srwatson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 13162587Sitojun 13291270Sbrooks/* 13391270Sbrooks * By default, we disallow creation of multiple tunnels between the same 13491270Sbrooks * pair of addresses. Some applications require this functionality so 13591270Sbrooks * we allow control over this check here. 13691270Sbrooks */ 137195837Srwatson#ifdef XBONEHACK 138215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 1; 139195837Srwatson#else 140215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 0; 141195837Srwatson#endif 142195837Srwatson#define V_parallel_tunnels VNET(parallel_tunnels) 143195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 144195699Srwatson &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 14591270Sbrooks 146176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 147176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 148176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 149176879Sthompsa#ifndef ETHER_IS_BROADCAST 150176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 151176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 152176879Sthompsa#endif 153176879Sthompsa 154128209Sbrooksstatic int 155160195Ssamgif_clone_create(ifc, unit, params) 15679106Sbrooks struct if_clone *ifc; 15792081Smux int unit; 158160195Ssam caddr_t params; 15954263Sshin{ 16078064Sume struct gif_softc *sc; 16154263Sshin 162131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 163178888Sjulian sc->gif_fibnum = curthread->td_proc->p_fibnum; 164147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 165147256Sbrooks if (GIF2IFP(sc) == NULL) { 166147256Sbrooks free(sc, M_GIF); 167147256Sbrooks return (ENOSPC); 168147256Sbrooks } 16979106Sbrooks 170155037Sglebius GIF_LOCK_INIT(sc); 171155037Sglebius 172147256Sbrooks GIF2IFP(sc)->if_softc = sc; 173147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 17479106Sbrooks 17579106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 176193664Shrs sc->gif_options = GIF_ACCEPT_REVETHIP; 17762587Sitojun 178147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 179147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 180147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 18178064Sume#if 0 18279106Sbrooks /* turn off ingress filter */ 183147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 18478064Sume#endif 185147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 186153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 187147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 188207554Ssobomax GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 189147256Sbrooks if_attach(GIF2IFP(sc)); 190147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 19183998Sbrooks if (ng_gif_attach_p != NULL) 192147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 193155037Sglebius 194155037Sglebius mtx_lock(&gif_mtx); 195181803Sbz LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 196155037Sglebius mtx_unlock(&gif_mtx); 197155037Sglebius 198155037Sglebius return (0); 19979106Sbrooks} 20079106Sbrooks 201127305Srwatsonstatic void 202151266Sthompsagif_clone_destroy(ifp) 203151266Sthompsa struct ifnet *ifp; 20479106Sbrooks{ 205184678Sbz#if defined(INET) || defined(INET6) 20679106Sbrooks int err; 207184678Sbz#endif 208151266Sthompsa struct gif_softc *sc = ifp->if_softc; 20979106Sbrooks 210151266Sthompsa mtx_lock(&gif_mtx); 211151266Sthompsa LIST_REMOVE(sc, gif_list); 212151266Sthompsa mtx_unlock(&gif_mtx); 213151266Sthompsa 214127305Srwatson gif_delete_tunnel(ifp); 215105293Sume#ifdef INET6 216105293Sume if (sc->encap_cookie6 != NULL) { 217105293Sume err = encap_detach(sc->encap_cookie6); 218105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 219105293Sume } 220105293Sume#endif 221105293Sume#ifdef INET 22279106Sbrooks if (sc->encap_cookie4 != NULL) { 22379106Sbrooks err = encap_detach(sc->encap_cookie4); 22479106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 22579106Sbrooks } 226105293Sume#endif 22779106Sbrooks 22883998Sbrooks if (ng_gif_detach_p != NULL) 22983998Sbrooks (*ng_gif_detach_p)(ifp); 23079106Sbrooks bpfdetach(ifp); 23179106Sbrooks if_detach(ifp); 232147256Sbrooks if_free(ifp); 23379106Sbrooks 234155037Sglebius GIF_LOCK_DESTROY(sc); 235155037Sglebius 23679106Sbrooks free(sc, M_GIF); 23779106Sbrooks} 23879106Sbrooks 239195837Srwatsonstatic void 240195837Srwatsonvnet_gif_init(const void *unused __unused) 241190787Szec{ 242190787Szec 243190787Szec LIST_INIT(&V_gif_softc_list); 244190787Szec} 245195837SrwatsonVNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 246195837Srwatson NULL); 247190787Szec 248190787Szecstatic int 24979106Sbrooksgifmodevent(mod, type, data) 25079106Sbrooks module_t mod; 25179106Sbrooks int type; 25279106Sbrooks void *data; 25379106Sbrooks{ 25479106Sbrooks 25579106Sbrooks switch (type) { 25679106Sbrooks case MOD_LOAD: 257127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 258185088Szec if_clone_attach(&gif_cloner); 259195837Srwatson break; 26079106Sbrooks 26179106Sbrooks case MOD_UNLOAD: 26279106Sbrooks if_clone_detach(&gif_cloner); 263127305Srwatson mtx_destroy(&gif_mtx); 26479106Sbrooks break; 265132199Sphk default: 266132199Sphk return EOPNOTSUPP; 26754263Sshin } 26879106Sbrooks return 0; 26954263Sshin} 27054263Sshin 27179106Sbrooksstatic moduledata_t gif_mod = { 27279106Sbrooks "if_gif", 27379106Sbrooks gifmodevent, 27479106Sbrooks 0 27579106Sbrooks}; 27654263Sshin 27779106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27883997SbrooksMODULE_VERSION(if_gif, 1); 27979106Sbrooks 280105293Sumeint 28162587Sitojungif_encapcheck(m, off, proto, arg) 28262587Sitojun const struct mbuf *m; 28362587Sitojun int off; 28462587Sitojun int proto; 28562587Sitojun void *arg; 28662587Sitojun{ 28762587Sitojun struct ip ip; 28862587Sitojun struct gif_softc *sc; 28962587Sitojun 29062587Sitojun sc = (struct gif_softc *)arg; 29162587Sitojun if (sc == NULL) 29262587Sitojun return 0; 29362587Sitojun 294147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 29562587Sitojun return 0; 29662587Sitojun 29762587Sitojun /* no physical address */ 29862587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 29962587Sitojun return 0; 30062587Sitojun 30162587Sitojun switch (proto) { 30262587Sitojun#ifdef INET 30362587Sitojun case IPPROTO_IPV4: 30462587Sitojun break; 30562587Sitojun#endif 30662587Sitojun#ifdef INET6 30762587Sitojun case IPPROTO_IPV6: 30862587Sitojun break; 30962587Sitojun#endif 310153621Sthompsa case IPPROTO_ETHERIP: 311153621Sthompsa break; 312153621Sthompsa 31362587Sitojun default: 31462587Sitojun return 0; 31562587Sitojun } 31662587Sitojun 317105339Sume /* Bail on short packets */ 318105339Sume if (m->m_pkthdr.len < sizeof(ip)) 319105339Sume return 0; 320105339Sume 32191327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 32262587Sitojun 32362587Sitojun switch (ip.ip_v) { 32462587Sitojun#ifdef INET 32562587Sitojun case 4: 32662587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 32762587Sitojun sc->gif_pdst->sa_family != AF_INET) 32862587Sitojun return 0; 32962587Sitojun return gif_encapcheck4(m, off, proto, arg); 33062587Sitojun#endif 33162587Sitojun#ifdef INET6 33262587Sitojun case 6: 333105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 334105293Sume return 0; 33562587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 33662587Sitojun sc->gif_pdst->sa_family != AF_INET6) 33762587Sitojun return 0; 33862587Sitojun return gif_encapcheck6(m, off, proto, arg); 33962587Sitojun#endif 34062587Sitojun default: 34162587Sitojun return 0; 34262587Sitojun } 34362587Sitojun} 34462587Sitojun 345153621Sthompsastatic void 346153621Sthompsagif_start(struct ifnet *ifp) 347153621Sthompsa{ 348153621Sthompsa struct gif_softc *sc; 349153621Sthompsa struct mbuf *m; 350153621Sthompsa 351153621Sthompsa sc = ifp->if_softc; 352153621Sthompsa 353153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 354153621Sthompsa for (;;) { 355153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 356153621Sthompsa if (m == 0) 357153621Sthompsa break; 358153621Sthompsa 359153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 360153621Sthompsa 361153621Sthompsa } 362153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 363153621Sthompsa 364153621Sthompsa return; 365153621Sthompsa} 366153621Sthompsa 36754263Sshinint 368191148Skmacygif_output(ifp, m, dst, ro) 36954263Sshin struct ifnet *ifp; 37054263Sshin struct mbuf *m; 37154263Sshin struct sockaddr *dst; 372191148Skmacy struct route *ro; 37354263Sshin{ 374147256Sbrooks struct gif_softc *sc = ifp->if_softc; 375127898Sru struct m_tag *mtag; 37654263Sshin int error = 0; 377127898Sru int gif_called; 378147611Sdwmalone u_int32_t af; 37954263Sshin 380101182Srwatson#ifdef MAC 381172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 382101739Srwatson if (error) { 383101739Srwatson m_freem(m); 384101739Srwatson goto end; 385101739Srwatson } 386101182Srwatson#endif 387101182Srwatson 38854263Sshin /* 38954263Sshin * gif may cause infinite recursion calls when misconfigured. 390127898Sru * We'll prevent this by detecting loops. 391127898Sru * 392127898Sru * High nesting level may cause stack exhaustion. 39354263Sshin * We'll prevent this by introducing upper limit. 39454263Sshin */ 395127898Sru gif_called = 1; 396127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 397127898Sru while (mtag != NULL) { 398127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 399127898Sru log(LOG_NOTICE, 400127898Sru "gif_output: loop detected on %s\n", 401127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 402127898Sru m_freem(m); 403127898Sru error = EIO; /* is there better errno? */ 404127898Sru goto end; 405127898Sru } 406127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 407127898Sru gif_called++; 408127898Sru } 409181803Sbz if (gif_called > V_max_gif_nesting) { 41054263Sshin log(LOG_NOTICE, 41154263Sshin "gif_output: recursively called too many times(%d)\n", 412127303Srwatson gif_called); 41354263Sshin m_freem(m); 41454263Sshin error = EIO; /* is there better errno? */ 41554263Sshin goto end; 41654263Sshin } 417127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 418127898Sru M_NOWAIT); 419127898Sru if (mtag == NULL) { 420127898Sru m_freem(m); 421127898Sru error = ENOMEM; 422127898Sru goto end; 423127898Sru } 424127898Sru *(struct ifnet **)(mtag + 1) = ifp; 425127898Sru m_tag_prepend(m, mtag); 42662587Sitojun 42754263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 428155037Sglebius 429155037Sglebius GIF_LOCK(sc); 430155037Sglebius 43154263Sshin if (!(ifp->if_flags & IFF_UP) || 43254263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 433159174Sglebius GIF_UNLOCK(sc); 43454263Sshin m_freem(m); 43554263Sshin error = ENETDOWN; 43654263Sshin goto end; 43754263Sshin } 43854263Sshin 439147611Sdwmalone /* BPF writes need to be handled specially. */ 440147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 441147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 442147611Sdwmalone dst->sa_family = af; 443147611Sdwmalone } 444147611Sdwmalone 445153621Sthompsa af = dst->sa_family; 446159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 44762587Sitojun ifp->if_opackets++; 44854263Sshin ifp->if_obytes += m->m_pkthdr.len; 44954263Sshin 450153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 451153621Sthompsa if (ifp->if_bridge) 452153621Sthompsa af = AF_LINK; 453153621Sthompsa 454178888Sjulian M_SETFIB(m, sc->gif_fibnum); 45578064Sume /* inner AF-specific encapsulation */ 45678064Sume 45762587Sitojun /* XXX should we check if our outer source is legal? */ 45862587Sitojun 45978064Sume /* dispatch to output logic based on outer AF */ 46054263Sshin switch (sc->gif_psrc->sa_family) { 46154263Sshin#ifdef INET 46254263Sshin case AF_INET: 463153621Sthompsa error = in_gif_output(ifp, af, m); 46454263Sshin break; 46554263Sshin#endif 46654263Sshin#ifdef INET6 46754263Sshin case AF_INET6: 468153621Sthompsa error = in6_gif_output(ifp, af, m); 46954263Sshin break; 47054263Sshin#endif 47154263Sshin default: 47262587Sitojun m_freem(m); 47354263Sshin error = ENETDOWN; 47454263Sshin } 47554263Sshin 476159174Sglebius GIF_UNLOCK(sc); 47754263Sshin end: 47878064Sume if (error) 47978064Sume ifp->if_oerrors++; 480155037Sglebius return (error); 48154263Sshin} 48254263Sshin 48354263Sshinvoid 484105338Sumegif_input(m, af, ifp) 48554263Sshin struct mbuf *m; 48654263Sshin int af; 487105338Sume struct ifnet *ifp; 48854263Sshin{ 489153621Sthompsa int isr, n; 490198357Sbrueffer struct gif_softc *sc; 491153621Sthompsa struct etherip_header *eip; 492176879Sthompsa struct ether_header *eh; 493176879Sthompsa struct ifnet *oldifp; 49454263Sshin 495105338Sume if (ifp == NULL) { 49654263Sshin /* just in case */ 49754263Sshin m_freem(m); 49854263Sshin return; 49954263Sshin } 500198357Sbrueffer sc = ifp->if_softc; 501105338Sume m->m_pkthdr.rcvif = ifp; 502101182Srwatson 503101182Srwatson#ifdef MAC 504172930Srwatson mac_ifnet_create_mbuf(ifp, m); 505101182Srwatson#endif 506101182Srwatson 507159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 50878064Sume u_int32_t af1 = af; 509123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 51054263Sshin } 51154263Sshin 51283998Sbrooks if (ng_gif_input_p != NULL) { 513105338Sume (*ng_gif_input_p)(ifp, &m, af); 51483998Sbrooks if (m == NULL) 51583998Sbrooks return; 51683998Sbrooks } 51783998Sbrooks 51854263Sshin /* 51954263Sshin * Put the packet to the network layer input queue according to the 52054263Sshin * specified address family. 52154263Sshin * Note: older versions of gif_input directly called network layer 52295023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 52354263Sshin * prevent too many recursive calls of such input functions, which 52495023Ssuz * might cause kernel panic. But the change may introduce another 52554263Sshin * problem; if the input queue is full, packets are discarded. 52695023Ssuz * The kernel stack overflow really happened, and we believed 52795023Ssuz * queue-full rarely occurs, so we changed the policy. 52854263Sshin */ 52954263Sshin switch (af) { 53054263Sshin#ifdef INET 53154263Sshin case AF_INET: 53254263Sshin isr = NETISR_IP; 53354263Sshin break; 53454263Sshin#endif 53554263Sshin#ifdef INET6 53654263Sshin case AF_INET6: 53754263Sshin isr = NETISR_IPV6; 53854263Sshin break; 53954263Sshin#endif 540153621Sthompsa case AF_LINK: 541153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 542153621Sthompsa if (n > m->m_len) { 543153621Sthompsa m = m_pullup(m, n); 544153621Sthompsa if (m == NULL) { 545153621Sthompsa ifp->if_ierrors++; 546153621Sthompsa return; 547153621Sthompsa } 548153621Sthompsa } 549153621Sthompsa 550153621Sthompsa eip = mtod(m, struct etherip_header *); 551193664Shrs /* 552193664Shrs * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 553193664Shrs * accepts an EtherIP packet with revered version field in 554193664Shrs * the header. This is a knob for backward compatibility 555193664Shrs * with FreeBSD 7.2R or prior. 556193664Shrs */ 557193664Shrs if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 558193664Shrs if (eip->eip_resvl != ETHERIP_VERSION 559193664Shrs && eip->eip_ver != ETHERIP_VERSION) { 560193664Shrs /* discard unknown versions */ 561193664Shrs m_freem(m); 562193664Shrs return; 563193664Shrs } 564193664Shrs } else { 565193664Shrs if (eip->eip_ver != ETHERIP_VERSION) { 566193664Shrs /* discard unknown versions */ 567193664Shrs m_freem(m); 568193664Shrs return; 569193664Shrs } 570153621Sthompsa } 571153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 572153621Sthompsa 573153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 574153621Sthompsa m->m_pkthdr.rcvif = ifp; 575153621Sthompsa 576176879Sthompsa if (ifp->if_bridge) { 577176879Sthompsa oldifp = ifp; 578176879Sthompsa eh = mtod(m, struct ether_header *); 579176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 580176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 581176879Sthompsa m->m_flags |= M_BCAST; 582176879Sthompsa else 583176879Sthompsa m->m_flags |= M_MCAST; 584176879Sthompsa ifp->if_imcasts++; 585176879Sthompsa } 586153621Sthompsa BRIDGE_INPUT(ifp, m); 587176879Sthompsa 588176879Sthompsa if (m != NULL && ifp != oldifp) { 589176879Sthompsa /* 590176879Sthompsa * The bridge gave us back itself or one of the 591176879Sthompsa * members for which the frame is addressed. 592176879Sthompsa */ 593176879Sthompsa ether_demux(ifp, m); 594176879Sthompsa return; 595176879Sthompsa } 596176879Sthompsa } 597153621Sthompsa if (m != NULL) 598153621Sthompsa m_freem(m); 599153621Sthompsa return; 600153621Sthompsa 60154263Sshin default: 60283998Sbrooks if (ng_gif_input_orphan_p != NULL) 603105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 60483998Sbrooks else 60583998Sbrooks m_freem(m); 60654263Sshin return; 60754263Sshin } 60854263Sshin 609105338Sume ifp->if_ipackets++; 610105338Sume ifp->if_ibytes += m->m_pkthdr.len; 611111888Sjlemon netisr_dispatch(isr, m); 61254263Sshin} 61354263Sshin 61462587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 61554263Sshinint 61654263Sshingif_ioctl(ifp, cmd, data) 61754263Sshin struct ifnet *ifp; 61854263Sshin u_long cmd; 61954263Sshin caddr_t data; 62054263Sshin{ 621147256Sbrooks struct gif_softc *sc = ifp->if_softc; 62254263Sshin struct ifreq *ifr = (struct ifreq*)data; 62354263Sshin int error = 0, size; 624193664Shrs u_int options; 62562587Sitojun struct sockaddr *dst, *src; 626105339Sume#ifdef SIOCSIFMTU /* xxx */ 627105339Sume u_long mtu; 628105339Sume#endif 629105339Sume 63054263Sshin switch (cmd) { 63154263Sshin case SIOCSIFADDR: 632105293Sume ifp->if_flags |= IFF_UP; 63354263Sshin break; 63462587Sitojun 63554263Sshin case SIOCSIFDSTADDR: 63654263Sshin break; 63754263Sshin 63854263Sshin case SIOCADDMULTI: 63954263Sshin case SIOCDELMULTI: 64054263Sshin break; 64154263Sshin 64262587Sitojun#ifdef SIOCSIFMTU /* xxx */ 64354263Sshin case SIOCGIFMTU: 64454263Sshin break; 64562587Sitojun 64654263Sshin case SIOCSIFMTU: 647105339Sume mtu = ifr->ifr_mtu; 648105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 649105339Sume return (EINVAL); 650105339Sume ifp->if_mtu = mtu; 65154263Sshin break; 65262587Sitojun#endif /* SIOCSIFMTU */ 65354263Sshin 654105339Sume#ifdef INET 65554263Sshin case SIOCSIFPHYADDR: 656105339Sume#endif 65754263Sshin#ifdef INET6 65854263Sshin case SIOCSIFPHYADDR_IN6: 65954263Sshin#endif /* INET6 */ 66078064Sume case SIOCSLIFPHYADDR: 66162587Sitojun switch (cmd) { 66278064Sume#ifdef INET 66362587Sitojun case SIOCSIFPHYADDR: 66454263Sshin src = (struct sockaddr *) 66554263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 66654263Sshin dst = (struct sockaddr *) 66754263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 66862587Sitojun break; 66978064Sume#endif 67062587Sitojun#ifdef INET6 67162587Sitojun case SIOCSIFPHYADDR_IN6: 67262587Sitojun src = (struct sockaddr *) 67362587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 67462587Sitojun dst = (struct sockaddr *) 67562587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 67662587Sitojun break; 67762587Sitojun#endif 67878064Sume case SIOCSLIFPHYADDR: 67978064Sume src = (struct sockaddr *) 68078064Sume &(((struct if_laddrreq *)data)->addr); 68178064Sume dst = (struct sockaddr *) 68278064Sume &(((struct if_laddrreq *)data)->dstaddr); 683105293Sume break; 68491327Sbrooks default: 685105293Sume return EINVAL; 68662587Sitojun } 68754263Sshin 68878064Sume /* sa_family must be equal */ 68978064Sume if (src->sa_family != dst->sa_family) 69078064Sume return EINVAL; 69178064Sume 69278064Sume /* validate sa_len */ 69378064Sume switch (src->sa_family) { 69478064Sume#ifdef INET 69578064Sume case AF_INET: 69678064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 69778064Sume return EINVAL; 69878064Sume break; 69978064Sume#endif 70078064Sume#ifdef INET6 70178064Sume case AF_INET6: 70278064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 70378064Sume return EINVAL; 70478064Sume break; 70578064Sume#endif 70678064Sume default: 70778064Sume return EAFNOSUPPORT; 70878064Sume } 70978064Sume switch (dst->sa_family) { 71078064Sume#ifdef INET 71178064Sume case AF_INET: 71278064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 71378064Sume return EINVAL; 71478064Sume break; 71578064Sume#endif 71678064Sume#ifdef INET6 71778064Sume case AF_INET6: 71878064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 71978064Sume return EINVAL; 72078064Sume break; 72178064Sume#endif 72278064Sume default: 72378064Sume return EAFNOSUPPORT; 72478064Sume } 72578064Sume 72678064Sume /* check sa_family looks sane for the cmd */ 72778064Sume switch (cmd) { 72878064Sume case SIOCSIFPHYADDR: 72978064Sume if (src->sa_family == AF_INET) 73078064Sume break; 73178064Sume return EAFNOSUPPORT; 73278064Sume#ifdef INET6 73378064Sume case SIOCSIFPHYADDR_IN6: 73478064Sume if (src->sa_family == AF_INET6) 73578064Sume break; 73678064Sume return EAFNOSUPPORT; 73778064Sume#endif /* INET6 */ 73878064Sume case SIOCSLIFPHYADDR: 73978064Sume /* checks done in the above */ 74078064Sume break; 74178064Sume } 74278064Sume 743147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 74462587Sitojun break; 74562587Sitojun 74662587Sitojun#ifdef SIOCDIFPHYADDR 74762587Sitojun case SIOCDIFPHYADDR: 748147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 74954263Sshin break; 75062587Sitojun#endif 75162587Sitojun 75254263Sshin case SIOCGIFPSRCADDR: 75354263Sshin#ifdef INET6 75454263Sshin case SIOCGIFPSRCADDR_IN6: 75554263Sshin#endif /* INET6 */ 75654263Sshin if (sc->gif_psrc == NULL) { 75754263Sshin error = EADDRNOTAVAIL; 75854263Sshin goto bad; 75954263Sshin } 76054263Sshin src = sc->gif_psrc; 76178064Sume switch (cmd) { 76254263Sshin#ifdef INET 76378064Sume case SIOCGIFPSRCADDR: 76454263Sshin dst = &ifr->ifr_addr; 76578064Sume size = sizeof(ifr->ifr_addr); 76654263Sshin break; 76754263Sshin#endif /* INET */ 76854263Sshin#ifdef INET6 76978064Sume case SIOCGIFPSRCADDR_IN6: 77054263Sshin dst = (struct sockaddr *) 77154263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 77278064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 77354263Sshin break; 77454263Sshin#endif /* INET6 */ 77554263Sshin default: 77654263Sshin error = EADDRNOTAVAIL; 77754263Sshin goto bad; 77854263Sshin } 77978064Sume if (src->sa_len > size) 78078064Sume return EINVAL; 78178064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 782148385Sume#ifdef INET6 783148385Sume if (dst->sa_family == AF_INET6) { 784148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 785148385Sume if (error != 0) 786148385Sume return (error); 787148385Sume } 788148385Sume#endif 78954263Sshin break; 79062587Sitojun 79154263Sshin case SIOCGIFPDSTADDR: 79254263Sshin#ifdef INET6 79354263Sshin case SIOCGIFPDSTADDR_IN6: 79454263Sshin#endif /* INET6 */ 79554263Sshin if (sc->gif_pdst == NULL) { 79654263Sshin error = EADDRNOTAVAIL; 79754263Sshin goto bad; 79854263Sshin } 79954263Sshin src = sc->gif_pdst; 80078064Sume switch (cmd) { 80154263Sshin#ifdef INET 80278064Sume case SIOCGIFPDSTADDR: 80354263Sshin dst = &ifr->ifr_addr; 80478064Sume size = sizeof(ifr->ifr_addr); 80554263Sshin break; 80654263Sshin#endif /* INET */ 80754263Sshin#ifdef INET6 80878064Sume case SIOCGIFPDSTADDR_IN6: 80954263Sshin dst = (struct sockaddr *) 81054263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 81178064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 81254263Sshin break; 81354263Sshin#endif /* INET6 */ 81454263Sshin default: 81554263Sshin error = EADDRNOTAVAIL; 81654263Sshin goto bad; 81754263Sshin } 81878064Sume if (src->sa_len > size) 81978064Sume return EINVAL; 82078064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 821148385Sume#ifdef INET6 822148385Sume if (dst->sa_family == AF_INET6) { 823148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 824148385Sume if (error != 0) 825148385Sume return (error); 826148385Sume } 827148385Sume#endif 82854263Sshin break; 82954263Sshin 83078064Sume case SIOCGLIFPHYADDR: 83178064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 83278064Sume error = EADDRNOTAVAIL; 83378064Sume goto bad; 83478064Sume } 83578064Sume 83678064Sume /* copy src */ 83778064Sume src = sc->gif_psrc; 83878064Sume dst = (struct sockaddr *) 83978064Sume &(((struct if_laddrreq *)data)->addr); 84078064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 84178064Sume if (src->sa_len > size) 84278064Sume return EINVAL; 84378064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 84478064Sume 84578064Sume /* copy dst */ 84678064Sume src = sc->gif_pdst; 84778064Sume dst = (struct sockaddr *) 84878064Sume &(((struct if_laddrreq *)data)->dstaddr); 84978064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 85078064Sume if (src->sa_len > size) 85178064Sume return EINVAL; 85278064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 85378064Sume break; 85478064Sume 85554263Sshin case SIOCSIFFLAGS: 85662587Sitojun /* if_ioctl() takes care of it */ 85754263Sshin break; 85854263Sshin 859193664Shrs case GIFGOPTS: 860193664Shrs options = sc->gif_options; 861193664Shrs error = copyout(&options, ifr->ifr_data, 862193664Shrs sizeof(options)); 863193664Shrs break; 864193664Shrs 865193664Shrs case GIFSOPTS: 866193664Shrs if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 867193664Shrs break; 868193815Shrs error = copyin(ifr->ifr_data, &options, sizeof(options)); 869193815Shrs if (error) 870193815Shrs break; 871193815Shrs if (options & ~GIF_OPTMASK) 872193815Shrs error = EINVAL; 873193815Shrs else 874193815Shrs sc->gif_options = options; 875193664Shrs break; 876193664Shrs 87754263Sshin default: 87854263Sshin error = EINVAL; 87954263Sshin break; 88054263Sshin } 88154263Sshin bad: 88254263Sshin return error; 88354263Sshin} 88479106Sbrooks 885127305Srwatson/* 886127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 887127305Srwatson * if a given tunnel is already present happens before we perform a 888127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 889127305Srwatson * re-ordered so that the check and replacement can be atomic using 890127305Srwatson * a mutex. 891127305Srwatson */ 892105293Sumeint 893105293Sumegif_set_tunnel(ifp, src, dst) 894105293Sume struct ifnet *ifp; 895105293Sume struct sockaddr *src; 896105293Sume struct sockaddr *dst; 897105293Sume{ 898147256Sbrooks struct gif_softc *sc = ifp->if_softc; 899105293Sume struct gif_softc *sc2; 900105293Sume struct sockaddr *osrc, *odst, *sa; 901105293Sume int error = 0; 902105293Sume 903127305Srwatson mtx_lock(&gif_mtx); 904181803Sbz LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 905105293Sume if (sc2 == sc) 906105293Sume continue; 907105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 908105293Sume continue; 909105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 910105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 911105293Sume sc2->gif_psrc->sa_family != src->sa_family || 912105293Sume sc2->gif_psrc->sa_len != src->sa_len) 913105293Sume continue; 914105293Sume 915105293Sume /* 916105293Sume * Disallow parallel tunnels unless instructed 917105293Sume * otherwise. 918105293Sume */ 919181803Sbz if (!V_parallel_tunnels && 920105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 921105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 922105293Sume error = EADDRNOTAVAIL; 923127305Srwatson mtx_unlock(&gif_mtx); 924105293Sume goto bad; 925105293Sume } 926105293Sume 927105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 928105293Sume } 929127305Srwatson mtx_unlock(&gif_mtx); 930105293Sume 931105293Sume /* XXX we can detach from both, but be polite just in case */ 932105293Sume if (sc->gif_psrc) 933105293Sume switch (sc->gif_psrc->sa_family) { 934105293Sume#ifdef INET 935105293Sume case AF_INET: 936105293Sume (void)in_gif_detach(sc); 937105293Sume break; 938105293Sume#endif 939105293Sume#ifdef INET6 940105293Sume case AF_INET6: 941105293Sume (void)in6_gif_detach(sc); 942105293Sume break; 943105293Sume#endif 944105293Sume } 945105293Sume 946105293Sume osrc = sc->gif_psrc; 947111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 948105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 949105293Sume sc->gif_psrc = sa; 950105293Sume 951105293Sume odst = sc->gif_pdst; 952111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 953105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 954105293Sume sc->gif_pdst = sa; 955105293Sume 956105293Sume switch (sc->gif_psrc->sa_family) { 957105293Sume#ifdef INET 958105293Sume case AF_INET: 959105293Sume error = in_gif_attach(sc); 960105293Sume break; 961105293Sume#endif 962105293Sume#ifdef INET6 963105293Sume case AF_INET6: 964148385Sume /* 965148385Sume * Check validity of the scope zone ID of the addresses, and 966148385Sume * convert it into the kernel internal form if necessary. 967148385Sume */ 968148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 969148385Sume if (error != 0) 970148385Sume break; 971148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 972148385Sume if (error != 0) 973148385Sume break; 974105293Sume error = in6_gif_attach(sc); 975105293Sume break; 976105293Sume#endif 977105293Sume } 978105293Sume if (error) { 979105293Sume /* rollback */ 980105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 981105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 982105293Sume sc->gif_psrc = osrc; 983105293Sume sc->gif_pdst = odst; 984105293Sume goto bad; 985105293Sume } 986105293Sume 987105293Sume if (osrc) 988105293Sume free((caddr_t)osrc, M_IFADDR); 989105293Sume if (odst) 990105293Sume free((caddr_t)odst, M_IFADDR); 991105293Sume 992105293Sume bad: 993105293Sume if (sc->gif_psrc && sc->gif_pdst) 994148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 995105293Sume else 996148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 997105293Sume 998105293Sume return error; 999105293Sume} 1000105293Sume 100179106Sbrooksvoid 1002105293Sumegif_delete_tunnel(ifp) 1003105293Sume struct ifnet *ifp; 100479106Sbrooks{ 1005147256Sbrooks struct gif_softc *sc = ifp->if_softc; 100679106Sbrooks 100779106Sbrooks if (sc->gif_psrc) { 100879106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 100979106Sbrooks sc->gif_psrc = NULL; 101079106Sbrooks } 101179106Sbrooks if (sc->gif_pdst) { 101279106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 101379106Sbrooks sc->gif_pdst = NULL; 101479106Sbrooks } 1015105293Sume /* it is safe to detach from both */ 1016105293Sume#ifdef INET 1017105293Sume (void)in_gif_detach(sc); 1018105293Sume#endif 1019105293Sume#ifdef INET6 1020105293Sume (void)in6_gif_detach(sc); 1021105293Sume#endif 1022160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 102379106Sbrooks} 1024