if_gif.c revision 153621
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 153621 2005-12-21 21:29:45Z thompsa $ */ 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> 40101182Srwatson#include <sys/mac.h> 4154263Sshin#include <sys/malloc.h> 4254263Sshin#include <sys/mbuf.h> 43129880Sphk#include <sys/module.h> 4454263Sshin#include <sys/socket.h> 4554263Sshin#include <sys/sockio.h> 4654263Sshin#include <sys/errno.h> 4754263Sshin#include <sys/time.h> 4891270Sbrooks#include <sys/sysctl.h> 4954263Sshin#include <sys/syslog.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> 6054263Sshin 6154263Sshin#include <netinet/in.h> 6254263Sshin#include <netinet/in_systm.h> 6378064Sume#include <netinet/ip.h> 6478064Sume#ifdef INET 6554263Sshin#include <netinet/in_var.h> 6654263Sshin#include <netinet/in_gif.h> 6779106Sbrooks#include <netinet/ip_var.h> 6854263Sshin#endif /* INET */ 6954263Sshin 7054263Sshin#ifdef INET6 7154263Sshin#ifndef INET 7254263Sshin#include <netinet/in.h> 7354263Sshin#endif 7454263Sshin#include <netinet6/in6_var.h> 7554263Sshin#include <netinet/ip6.h> 7654263Sshin#include <netinet6/ip6_var.h> 77148385Sume#include <netinet6/scope6_var.h> 7854263Sshin#include <netinet6/in6_gif.h> 7962587Sitojun#include <netinet6/ip6protosw.h> 8054263Sshin#endif /* INET6 */ 8154263Sshin 8262587Sitojun#include <netinet/ip_encap.h> 83153621Sthompsa#include <net/ethernet.h> 84153621Sthompsa#include <net/if_bridgevar.h> 8554263Sshin#include <net/if_gif.h> 8654263Sshin 8754263Sshin#include <net/net_osdep.h> 8854263Sshin 8979106Sbrooks#define GIFNAME "gif" 9062587Sitojun 91127305Srwatson/* 92127898Sru * gif_mtx protects the global gif_softc_list. 93127305Srwatson * XXX: Per-softc locking is still required. 94127305Srwatson */ 95127305Srwatsonstatic struct mtx gif_mtx; 9679106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 9789065Smsmithstatic LIST_HEAD(, gif_softc) gif_softc_list; 9879106Sbrooks 9983998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10083998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10183998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 10283998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 10383998Sbrooks 104153621Sthompsastatic void gif_start(struct ifnet *); 105128209Sbrooksstatic int gif_clone_create(struct if_clone *, int); 106128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 10779106Sbrooks 108130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 10979106Sbrooks 11092725Salfredstatic int gifmodevent(module_t, int, void *); 11179106Sbrooks 11291270SbrooksSYSCTL_DECL(_net_link); 11391270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 11491270Sbrooks "Generic Tunnel Interface"); 11562587Sitojun#ifndef MAX_GIF_NEST 11662587Sitojun/* 11791270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 11862587Sitojun * Since, setting a large value to this macro with a careless configuration 11962587Sitojun * may introduce system crash, we don't allow any nestings by default. 12062587Sitojun * If you need to configure nested gif tunnels, you can define this macro 12195023Ssuz * in your kernel configuration file. However, if you do so, please be 12262587Sitojun * careful to configure the tunnels so that it won't make a loop. 12362587Sitojun */ 12462587Sitojun#define MAX_GIF_NEST 1 12562587Sitojun#endif 12662587Sitojunstatic int max_gif_nesting = MAX_GIF_NEST; 12791270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 12891270Sbrooks &max_gif_nesting, 0, "Max nested tunnels"); 12962587Sitojun 13091270Sbrooks/* 13191270Sbrooks * By default, we disallow creation of multiple tunnels between the same 13291270Sbrooks * pair of addresses. Some applications require this functionality so 13391270Sbrooks * we allow control over this check here. 13491270Sbrooks */ 13591270Sbrooks#ifdef XBONEHACK 13691270Sbrooksstatic int parallel_tunnels = 1; 13791270Sbrooks#else 13891270Sbrooksstatic int parallel_tunnels = 0; 13991270Sbrooks#endif 14091270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 14191270Sbrooks ¶llel_tunnels, 0, "Allow parallel tunnels?"); 14291270Sbrooks 143128209Sbrooksstatic int 14479106Sbrooksgif_clone_create(ifc, unit) 14579106Sbrooks struct if_clone *ifc; 14692081Smux int unit; 14754263Sshin{ 14878064Sume struct gif_softc *sc; 14954263Sshin 150131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 151147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 152147256Sbrooks if (GIF2IFP(sc) == NULL) { 153147256Sbrooks free(sc, M_GIF); 154147256Sbrooks return (ENOSPC); 155147256Sbrooks } 15679106Sbrooks 157147256Sbrooks GIF2IFP(sc)->if_softc = sc; 158147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 15979106Sbrooks 160105293Sume gifattach0(sc); 161105293Sume 162127305Srwatson mtx_lock(&gif_mtx); 163105293Sume LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 164127305Srwatson mtx_unlock(&gif_mtx); 165105293Sume return (0); 166105293Sume} 167105293Sume 168105293Sumevoid 169105293Sumegifattach0(sc) 170105293Sume struct gif_softc *sc; 171105293Sume{ 172105293Sume 17379106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 17462587Sitojun 175147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 176147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 177147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17878064Sume#if 0 17979106Sbrooks /* turn off ingress filter */ 180147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 18178064Sume#endif 182147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 183153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 184147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 185147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 186147256Sbrooks if_attach(GIF2IFP(sc)); 187147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 18883998Sbrooks if (ng_gif_attach_p != NULL) 189147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 19079106Sbrooks} 19179106Sbrooks 192127305Srwatsonstatic void 193151266Sthompsagif_clone_destroy(ifp) 194151266Sthompsa struct ifnet *ifp; 19579106Sbrooks{ 19679106Sbrooks int err; 197151266Sthompsa struct gif_softc *sc = ifp->if_softc; 19879106Sbrooks 199151266Sthompsa mtx_lock(&gif_mtx); 200151266Sthompsa LIST_REMOVE(sc, gif_list); 201151266Sthompsa mtx_unlock(&gif_mtx); 202151266Sthompsa 203127305Srwatson gif_delete_tunnel(ifp); 204105293Sume#ifdef INET6 205105293Sume if (sc->encap_cookie6 != NULL) { 206105293Sume err = encap_detach(sc->encap_cookie6); 207105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 208105293Sume } 209105293Sume#endif 210105293Sume#ifdef INET 21179106Sbrooks if (sc->encap_cookie4 != NULL) { 21279106Sbrooks err = encap_detach(sc->encap_cookie4); 21379106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 21479106Sbrooks } 215105293Sume#endif 21679106Sbrooks 21783998Sbrooks if (ng_gif_detach_p != NULL) 21883998Sbrooks (*ng_gif_detach_p)(ifp); 21979106Sbrooks bpfdetach(ifp); 22079106Sbrooks if_detach(ifp); 221147256Sbrooks if_free(ifp); 22279106Sbrooks 22379106Sbrooks free(sc, M_GIF); 22479106Sbrooks} 22579106Sbrooks 22679106Sbrooksstatic int 22779106Sbrooksgifmodevent(mod, type, data) 22879106Sbrooks module_t mod; 22979106Sbrooks int type; 23079106Sbrooks void *data; 23179106Sbrooks{ 23279106Sbrooks 23379106Sbrooks switch (type) { 23479106Sbrooks case MOD_LOAD: 235127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 23683997Sbrooks LIST_INIT(&gif_softc_list); 23779106Sbrooks if_clone_attach(&gif_cloner); 23879106Sbrooks 23979106Sbrooks#ifdef INET6 24079106Sbrooks ip6_gif_hlim = GIF_HLIM; 24162587Sitojun#endif 24279106Sbrooks 24379106Sbrooks break; 24479106Sbrooks case MOD_UNLOAD: 24579106Sbrooks if_clone_detach(&gif_cloner); 246127305Srwatson mtx_destroy(&gif_mtx); 24779106Sbrooks#ifdef INET6 24879106Sbrooks ip6_gif_hlim = 0; 24962587Sitojun#endif 25079106Sbrooks break; 251132199Sphk default: 252132199Sphk return EOPNOTSUPP; 25354263Sshin } 25479106Sbrooks return 0; 25554263Sshin} 25654263Sshin 25779106Sbrooksstatic moduledata_t gif_mod = { 25879106Sbrooks "if_gif", 25979106Sbrooks gifmodevent, 26079106Sbrooks 0 26179106Sbrooks}; 26254263Sshin 26379106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 26483997SbrooksMODULE_VERSION(if_gif, 1); 26579106Sbrooks 266105293Sumeint 26762587Sitojungif_encapcheck(m, off, proto, arg) 26862587Sitojun const struct mbuf *m; 26962587Sitojun int off; 27062587Sitojun int proto; 27162587Sitojun void *arg; 27262587Sitojun{ 27362587Sitojun struct ip ip; 27462587Sitojun struct gif_softc *sc; 27562587Sitojun 27662587Sitojun sc = (struct gif_softc *)arg; 27762587Sitojun if (sc == NULL) 27862587Sitojun return 0; 27962587Sitojun 280147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 28162587Sitojun return 0; 28262587Sitojun 28362587Sitojun /* no physical address */ 28462587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 28562587Sitojun return 0; 28662587Sitojun 28762587Sitojun switch (proto) { 28862587Sitojun#ifdef INET 28962587Sitojun case IPPROTO_IPV4: 29062587Sitojun break; 29162587Sitojun#endif 29262587Sitojun#ifdef INET6 29362587Sitojun case IPPROTO_IPV6: 29462587Sitojun break; 29562587Sitojun#endif 296153621Sthompsa case IPPROTO_ETHERIP: 297153621Sthompsa break; 298153621Sthompsa 29962587Sitojun default: 30062587Sitojun return 0; 30162587Sitojun } 30262587Sitojun 303105339Sume /* Bail on short packets */ 304105339Sume if (m->m_pkthdr.len < sizeof(ip)) 305105339Sume return 0; 306105339Sume 30791327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 30862587Sitojun 30962587Sitojun switch (ip.ip_v) { 31062587Sitojun#ifdef INET 31162587Sitojun case 4: 31262587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 31362587Sitojun sc->gif_pdst->sa_family != AF_INET) 31462587Sitojun return 0; 31562587Sitojun return gif_encapcheck4(m, off, proto, arg); 31662587Sitojun#endif 31762587Sitojun#ifdef INET6 31862587Sitojun case 6: 319105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 320105293Sume return 0; 32162587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 32262587Sitojun sc->gif_pdst->sa_family != AF_INET6) 32362587Sitojun return 0; 32462587Sitojun return gif_encapcheck6(m, off, proto, arg); 32562587Sitojun#endif 32662587Sitojun default: 32762587Sitojun return 0; 32862587Sitojun } 32962587Sitojun} 33062587Sitojun 331153621Sthompsastatic void 332153621Sthompsagif_start(struct ifnet *ifp) 333153621Sthompsa{ 334153621Sthompsa struct gif_softc *sc; 335153621Sthompsa struct mbuf *m; 336153621Sthompsa 337153621Sthompsa sc = ifp->if_softc; 338153621Sthompsa 339153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 340153621Sthompsa for (;;) { 341153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 342153621Sthompsa if (m == 0) 343153621Sthompsa break; 344153621Sthompsa 345153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 346153621Sthompsa 347153621Sthompsa } 348153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 349153621Sthompsa 350153621Sthompsa return; 351153621Sthompsa} 352153621Sthompsa 35354263Sshinint 35454263Sshingif_output(ifp, m, dst, rt) 35554263Sshin struct ifnet *ifp; 35654263Sshin struct mbuf *m; 35754263Sshin struct sockaddr *dst; 35854263Sshin struct rtentry *rt; /* added in net2 */ 35954263Sshin{ 360147256Sbrooks struct gif_softc *sc = ifp->if_softc; 361127898Sru struct m_tag *mtag; 36254263Sshin int error = 0; 363127898Sru int gif_called; 364147611Sdwmalone u_int32_t af; 36554263Sshin 366101182Srwatson#ifdef MAC 367101182Srwatson error = mac_check_ifnet_transmit(ifp, m); 368101739Srwatson if (error) { 369101739Srwatson m_freem(m); 370101739Srwatson goto end; 371101739Srwatson } 372101182Srwatson#endif 373101182Srwatson 37454263Sshin /* 37554263Sshin * gif may cause infinite recursion calls when misconfigured. 376127898Sru * We'll prevent this by detecting loops. 377127898Sru * 378127898Sru * High nesting level may cause stack exhaustion. 37954263Sshin * We'll prevent this by introducing upper limit. 38054263Sshin */ 381127898Sru gif_called = 1; 382127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 383127898Sru while (mtag != NULL) { 384127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 385127898Sru log(LOG_NOTICE, 386127898Sru "gif_output: loop detected on %s\n", 387127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 388127898Sru m_freem(m); 389127898Sru error = EIO; /* is there better errno? */ 390127898Sru goto end; 391127898Sru } 392127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 393127898Sru gif_called++; 394127898Sru } 395127898Sru if (gif_called > max_gif_nesting) { 39654263Sshin log(LOG_NOTICE, 39754263Sshin "gif_output: recursively called too many times(%d)\n", 398127303Srwatson gif_called); 39954263Sshin m_freem(m); 40054263Sshin error = EIO; /* is there better errno? */ 40154263Sshin goto end; 40254263Sshin } 403127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 404127898Sru M_NOWAIT); 405127898Sru if (mtag == NULL) { 406127898Sru m_freem(m); 407127898Sru error = ENOMEM; 408127898Sru goto end; 409127898Sru } 410127898Sru *(struct ifnet **)(mtag + 1) = ifp; 411127898Sru m_tag_prepend(m, mtag); 41262587Sitojun 41354263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 41454263Sshin if (!(ifp->if_flags & IFF_UP) || 41554263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 41654263Sshin m_freem(m); 41754263Sshin error = ENETDOWN; 41854263Sshin goto end; 41954263Sshin } 42054263Sshin 421147611Sdwmalone /* BPF writes need to be handled specially. */ 422147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 423147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 424147611Sdwmalone dst->sa_family = af; 425147611Sdwmalone } 426147611Sdwmalone 427153621Sthompsa af = dst->sa_family; 42854263Sshin if (ifp->if_bpf) { 429123922Ssam bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 43054263Sshin } 43162587Sitojun ifp->if_opackets++; 43254263Sshin ifp->if_obytes += m->m_pkthdr.len; 43354263Sshin 434153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 435153621Sthompsa if (ifp->if_bridge) 436153621Sthompsa af = AF_LINK; 437153621Sthompsa 43878064Sume /* inner AF-specific encapsulation */ 43978064Sume 44062587Sitojun /* XXX should we check if our outer source is legal? */ 44162587Sitojun 44278064Sume /* dispatch to output logic based on outer AF */ 44354263Sshin switch (sc->gif_psrc->sa_family) { 44454263Sshin#ifdef INET 44554263Sshin case AF_INET: 446153621Sthompsa error = in_gif_output(ifp, af, m); 44754263Sshin break; 44854263Sshin#endif 44954263Sshin#ifdef INET6 45054263Sshin case AF_INET6: 451153621Sthompsa error = in6_gif_output(ifp, af, m); 45254263Sshin break; 45354263Sshin#endif 45454263Sshin default: 45562587Sitojun m_freem(m); 45654263Sshin error = ENETDOWN; 45778064Sume goto end; 45854263Sshin } 45954263Sshin 46054263Sshin end: 46178064Sume if (error) 46278064Sume ifp->if_oerrors++; 46354263Sshin return error; 46454263Sshin} 46554263Sshin 46654263Sshinvoid 467105338Sumegif_input(m, af, ifp) 46854263Sshin struct mbuf *m; 46954263Sshin int af; 470105338Sume struct ifnet *ifp; 47154263Sshin{ 472153621Sthompsa int isr, n; 473153621Sthompsa struct etherip_header *eip; 47454263Sshin 475105338Sume if (ifp == NULL) { 47654263Sshin /* just in case */ 47754263Sshin m_freem(m); 47854263Sshin return; 47954263Sshin } 48054263Sshin 481105338Sume m->m_pkthdr.rcvif = ifp; 482101182Srwatson 483101182Srwatson#ifdef MAC 484105338Sume mac_create_mbuf_from_ifnet(ifp, m); 485101182Srwatson#endif 486101182Srwatson 487105338Sume if (ifp->if_bpf) { 48878064Sume u_int32_t af1 = af; 489123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 49054263Sshin } 49154263Sshin 49283998Sbrooks if (ng_gif_input_p != NULL) { 493105338Sume (*ng_gif_input_p)(ifp, &m, af); 49483998Sbrooks if (m == NULL) 49583998Sbrooks return; 49683998Sbrooks } 49783998Sbrooks 49854263Sshin /* 49954263Sshin * Put the packet to the network layer input queue according to the 50054263Sshin * specified address family. 50154263Sshin * Note: older versions of gif_input directly called network layer 50295023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 50354263Sshin * prevent too many recursive calls of such input functions, which 50495023Ssuz * might cause kernel panic. But the change may introduce another 50554263Sshin * problem; if the input queue is full, packets are discarded. 50695023Ssuz * The kernel stack overflow really happened, and we believed 50795023Ssuz * queue-full rarely occurs, so we changed the policy. 50854263Sshin */ 50954263Sshin switch (af) { 51054263Sshin#ifdef INET 51154263Sshin case AF_INET: 51254263Sshin isr = NETISR_IP; 51354263Sshin break; 51454263Sshin#endif 51554263Sshin#ifdef INET6 51654263Sshin case AF_INET6: 51754263Sshin isr = NETISR_IPV6; 51854263Sshin break; 51954263Sshin#endif 520153621Sthompsa case AF_LINK: 521153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 522153621Sthompsa if (n > m->m_len) { 523153621Sthompsa m = m_pullup(m, n); 524153621Sthompsa if (m == NULL) { 525153621Sthompsa ifp->if_ierrors++; 526153621Sthompsa return; 527153621Sthompsa } 528153621Sthompsa } 529153621Sthompsa 530153621Sthompsa eip = mtod(m, struct etherip_header *); 531153621Sthompsa if (eip->eip_ver != 532153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 533153621Sthompsa /* discard unknown versions */ 534153621Sthompsa m_freem(m); 535153621Sthompsa return; 536153621Sthompsa } 537153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 538153621Sthompsa 539153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 540153621Sthompsa m->m_pkthdr.rcvif = ifp; 541153621Sthompsa 542153621Sthompsa if (ifp->if_bridge) 543153621Sthompsa BRIDGE_INPUT(ifp, m); 544153621Sthompsa 545153621Sthompsa if (m != NULL) 546153621Sthompsa m_freem(m); 547153621Sthompsa return; 548153621Sthompsa 54954263Sshin default: 55083998Sbrooks if (ng_gif_input_orphan_p != NULL) 551105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 55283998Sbrooks else 55383998Sbrooks m_freem(m); 55454263Sshin return; 55554263Sshin } 55654263Sshin 557105338Sume ifp->if_ipackets++; 558105338Sume ifp->if_ibytes += m->m_pkthdr.len; 559111888Sjlemon netisr_dispatch(isr, m); 56054263Sshin} 56154263Sshin 56262587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 56354263Sshinint 56454263Sshingif_ioctl(ifp, cmd, data) 56554263Sshin struct ifnet *ifp; 56654263Sshin u_long cmd; 56754263Sshin caddr_t data; 56854263Sshin{ 569147256Sbrooks struct gif_softc *sc = ifp->if_softc; 57054263Sshin struct ifreq *ifr = (struct ifreq*)data; 57154263Sshin int error = 0, size; 57262587Sitojun struct sockaddr *dst, *src; 573105339Sume#ifdef SIOCSIFMTU /* xxx */ 574105339Sume u_long mtu; 575105339Sume#endif 576105339Sume 57754263Sshin switch (cmd) { 57854263Sshin case SIOCSIFADDR: 579105293Sume ifp->if_flags |= IFF_UP; 58054263Sshin break; 58162587Sitojun 58254263Sshin case SIOCSIFDSTADDR: 58354263Sshin break; 58454263Sshin 58554263Sshin case SIOCADDMULTI: 58654263Sshin case SIOCDELMULTI: 58754263Sshin break; 58854263Sshin 58962587Sitojun#ifdef SIOCSIFMTU /* xxx */ 59054263Sshin case SIOCGIFMTU: 59154263Sshin break; 59262587Sitojun 59354263Sshin case SIOCSIFMTU: 594105339Sume mtu = ifr->ifr_mtu; 595105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 596105339Sume return (EINVAL); 597105339Sume ifp->if_mtu = mtu; 59854263Sshin break; 59962587Sitojun#endif /* SIOCSIFMTU */ 60054263Sshin 601105339Sume#ifdef INET 60254263Sshin case SIOCSIFPHYADDR: 603105339Sume#endif 60454263Sshin#ifdef INET6 60554263Sshin case SIOCSIFPHYADDR_IN6: 60654263Sshin#endif /* INET6 */ 60778064Sume case SIOCSLIFPHYADDR: 60862587Sitojun switch (cmd) { 60978064Sume#ifdef INET 61062587Sitojun case SIOCSIFPHYADDR: 61154263Sshin src = (struct sockaddr *) 61254263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 61354263Sshin dst = (struct sockaddr *) 61454263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 61562587Sitojun break; 61678064Sume#endif 61762587Sitojun#ifdef INET6 61862587Sitojun case SIOCSIFPHYADDR_IN6: 61962587Sitojun src = (struct sockaddr *) 62062587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 62162587Sitojun dst = (struct sockaddr *) 62262587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 62362587Sitojun break; 62462587Sitojun#endif 62578064Sume case SIOCSLIFPHYADDR: 62678064Sume src = (struct sockaddr *) 62778064Sume &(((struct if_laddrreq *)data)->addr); 62878064Sume dst = (struct sockaddr *) 62978064Sume &(((struct if_laddrreq *)data)->dstaddr); 630105293Sume break; 63191327Sbrooks default: 632105293Sume return EINVAL; 63362587Sitojun } 63454263Sshin 63578064Sume /* sa_family must be equal */ 63678064Sume if (src->sa_family != dst->sa_family) 63778064Sume return EINVAL; 63878064Sume 63978064Sume /* validate sa_len */ 64078064Sume switch (src->sa_family) { 64178064Sume#ifdef INET 64278064Sume case AF_INET: 64378064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 64478064Sume return EINVAL; 64578064Sume break; 64678064Sume#endif 64778064Sume#ifdef INET6 64878064Sume case AF_INET6: 64978064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 65078064Sume return EINVAL; 65178064Sume break; 65278064Sume#endif 65378064Sume default: 65478064Sume return EAFNOSUPPORT; 65578064Sume } 65678064Sume switch (dst->sa_family) { 65778064Sume#ifdef INET 65878064Sume case AF_INET: 65978064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 66078064Sume return EINVAL; 66178064Sume break; 66278064Sume#endif 66378064Sume#ifdef INET6 66478064Sume case AF_INET6: 66578064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 66678064Sume return EINVAL; 66778064Sume break; 66878064Sume#endif 66978064Sume default: 67078064Sume return EAFNOSUPPORT; 67178064Sume } 67278064Sume 67378064Sume /* check sa_family looks sane for the cmd */ 67478064Sume switch (cmd) { 67578064Sume case SIOCSIFPHYADDR: 67678064Sume if (src->sa_family == AF_INET) 67778064Sume break; 67878064Sume return EAFNOSUPPORT; 67978064Sume#ifdef INET6 68078064Sume case SIOCSIFPHYADDR_IN6: 68178064Sume if (src->sa_family == AF_INET6) 68278064Sume break; 68378064Sume return EAFNOSUPPORT; 68478064Sume#endif /* INET6 */ 68578064Sume case SIOCSLIFPHYADDR: 68678064Sume /* checks done in the above */ 68778064Sume break; 68878064Sume } 68978064Sume 690147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 69162587Sitojun break; 69262587Sitojun 69362587Sitojun#ifdef SIOCDIFPHYADDR 69462587Sitojun case SIOCDIFPHYADDR: 695147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 69654263Sshin break; 69762587Sitojun#endif 69862587Sitojun 69954263Sshin case SIOCGIFPSRCADDR: 70054263Sshin#ifdef INET6 70154263Sshin case SIOCGIFPSRCADDR_IN6: 70254263Sshin#endif /* INET6 */ 70354263Sshin if (sc->gif_psrc == NULL) { 70454263Sshin error = EADDRNOTAVAIL; 70554263Sshin goto bad; 70654263Sshin } 70754263Sshin src = sc->gif_psrc; 70878064Sume switch (cmd) { 70954263Sshin#ifdef INET 71078064Sume case SIOCGIFPSRCADDR: 71154263Sshin dst = &ifr->ifr_addr; 71278064Sume size = sizeof(ifr->ifr_addr); 71354263Sshin break; 71454263Sshin#endif /* INET */ 71554263Sshin#ifdef INET6 71678064Sume case SIOCGIFPSRCADDR_IN6: 71754263Sshin dst = (struct sockaddr *) 71854263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 71978064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 72054263Sshin break; 72154263Sshin#endif /* INET6 */ 72254263Sshin default: 72354263Sshin error = EADDRNOTAVAIL; 72454263Sshin goto bad; 72554263Sshin } 72678064Sume if (src->sa_len > size) 72778064Sume return EINVAL; 72878064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 729148385Sume#ifdef INET6 730148385Sume if (dst->sa_family == AF_INET6) { 731148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 732148385Sume if (error != 0) 733148385Sume return (error); 734148385Sume } 735148385Sume#endif 73654263Sshin break; 73762587Sitojun 73854263Sshin case SIOCGIFPDSTADDR: 73954263Sshin#ifdef INET6 74054263Sshin case SIOCGIFPDSTADDR_IN6: 74154263Sshin#endif /* INET6 */ 74254263Sshin if (sc->gif_pdst == NULL) { 74354263Sshin error = EADDRNOTAVAIL; 74454263Sshin goto bad; 74554263Sshin } 74654263Sshin src = sc->gif_pdst; 74778064Sume switch (cmd) { 74854263Sshin#ifdef INET 74978064Sume case SIOCGIFPDSTADDR: 75054263Sshin dst = &ifr->ifr_addr; 75178064Sume size = sizeof(ifr->ifr_addr); 75254263Sshin break; 75354263Sshin#endif /* INET */ 75454263Sshin#ifdef INET6 75578064Sume case SIOCGIFPDSTADDR_IN6: 75654263Sshin dst = (struct sockaddr *) 75754263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 75878064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 75954263Sshin break; 76054263Sshin#endif /* INET6 */ 76154263Sshin default: 76254263Sshin error = EADDRNOTAVAIL; 76354263Sshin goto bad; 76454263Sshin } 76578064Sume if (src->sa_len > size) 76678064Sume return EINVAL; 76778064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 768148385Sume#ifdef INET6 769148385Sume if (dst->sa_family == AF_INET6) { 770148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 771148385Sume if (error != 0) 772148385Sume return (error); 773148385Sume } 774148385Sume#endif 77554263Sshin break; 77654263Sshin 77778064Sume case SIOCGLIFPHYADDR: 77878064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 77978064Sume error = EADDRNOTAVAIL; 78078064Sume goto bad; 78178064Sume } 78278064Sume 78378064Sume /* copy src */ 78478064Sume src = sc->gif_psrc; 78578064Sume dst = (struct sockaddr *) 78678064Sume &(((struct if_laddrreq *)data)->addr); 78778064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 78878064Sume if (src->sa_len > size) 78978064Sume return EINVAL; 79078064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 79178064Sume 79278064Sume /* copy dst */ 79378064Sume src = sc->gif_pdst; 79478064Sume dst = (struct sockaddr *) 79578064Sume &(((struct if_laddrreq *)data)->dstaddr); 79678064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 79778064Sume if (src->sa_len > size) 79878064Sume return EINVAL; 79978064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 80078064Sume break; 80178064Sume 80254263Sshin case SIOCSIFFLAGS: 80362587Sitojun /* if_ioctl() takes care of it */ 80454263Sshin break; 80554263Sshin 80654263Sshin default: 80754263Sshin error = EINVAL; 80854263Sshin break; 80954263Sshin } 81054263Sshin bad: 81154263Sshin return error; 81254263Sshin} 81379106Sbrooks 814127305Srwatson/* 815127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 816127305Srwatson * if a given tunnel is already present happens before we perform a 817127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 818127305Srwatson * re-ordered so that the check and replacement can be atomic using 819127305Srwatson * a mutex. 820127305Srwatson */ 821105293Sumeint 822105293Sumegif_set_tunnel(ifp, src, dst) 823105293Sume struct ifnet *ifp; 824105293Sume struct sockaddr *src; 825105293Sume struct sockaddr *dst; 826105293Sume{ 827147256Sbrooks struct gif_softc *sc = ifp->if_softc; 828105293Sume struct gif_softc *sc2; 829105293Sume struct sockaddr *osrc, *odst, *sa; 830105293Sume int s; 831105293Sume int error = 0; 832105293Sume 833105293Sume s = splnet(); 834105293Sume 835127305Srwatson mtx_lock(&gif_mtx); 836105293Sume LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 837105293Sume if (sc2 == sc) 838105293Sume continue; 839105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 840105293Sume continue; 841105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 842105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 843105293Sume sc2->gif_psrc->sa_family != src->sa_family || 844105293Sume sc2->gif_psrc->sa_len != src->sa_len) 845105293Sume continue; 846105293Sume 847105293Sume /* 848105293Sume * Disallow parallel tunnels unless instructed 849105293Sume * otherwise. 850105293Sume */ 851105293Sume if (!parallel_tunnels && 852105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 853105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 854105293Sume error = EADDRNOTAVAIL; 855127305Srwatson mtx_unlock(&gif_mtx); 856105293Sume goto bad; 857105293Sume } 858105293Sume 859105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 860105293Sume } 861127305Srwatson mtx_unlock(&gif_mtx); 862105293Sume 863105293Sume /* XXX we can detach from both, but be polite just in case */ 864105293Sume if (sc->gif_psrc) 865105293Sume switch (sc->gif_psrc->sa_family) { 866105293Sume#ifdef INET 867105293Sume case AF_INET: 868105293Sume (void)in_gif_detach(sc); 869105293Sume break; 870105293Sume#endif 871105293Sume#ifdef INET6 872105293Sume case AF_INET6: 873105293Sume (void)in6_gif_detach(sc); 874105293Sume break; 875105293Sume#endif 876105293Sume } 877105293Sume 878105293Sume osrc = sc->gif_psrc; 879111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 880105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 881105293Sume sc->gif_psrc = sa; 882105293Sume 883105293Sume odst = sc->gif_pdst; 884111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 885105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 886105293Sume sc->gif_pdst = sa; 887105293Sume 888105293Sume switch (sc->gif_psrc->sa_family) { 889105293Sume#ifdef INET 890105293Sume case AF_INET: 891105293Sume error = in_gif_attach(sc); 892105293Sume break; 893105293Sume#endif 894105293Sume#ifdef INET6 895105293Sume case AF_INET6: 896148385Sume /* 897148385Sume * Check validity of the scope zone ID of the addresses, and 898148385Sume * convert it into the kernel internal form if necessary. 899148385Sume */ 900148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 901148385Sume if (error != 0) 902148385Sume break; 903148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 904148385Sume if (error != 0) 905148385Sume break; 906105293Sume error = in6_gif_attach(sc); 907105293Sume break; 908105293Sume#endif 909105293Sume } 910105293Sume if (error) { 911105293Sume /* rollback */ 912105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 913105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 914105293Sume sc->gif_psrc = osrc; 915105293Sume sc->gif_pdst = odst; 916105293Sume goto bad; 917105293Sume } 918105293Sume 919105293Sume if (osrc) 920105293Sume free((caddr_t)osrc, M_IFADDR); 921105293Sume if (odst) 922105293Sume free((caddr_t)odst, M_IFADDR); 923105293Sume 924105293Sume if (sc->gif_psrc && sc->gif_pdst) 925148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 926105293Sume else 927148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 928105293Sume splx(s); 929105293Sume 930105293Sume return 0; 931105293Sume 932105293Sume bad: 933105293Sume if (sc->gif_psrc && sc->gif_pdst) 934148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 935105293Sume else 936148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 937105293Sume splx(s); 938105293Sume 939105293Sume return error; 940105293Sume} 941105293Sume 94279106Sbrooksvoid 943105293Sumegif_delete_tunnel(ifp) 944105293Sume struct ifnet *ifp; 94579106Sbrooks{ 946147256Sbrooks struct gif_softc *sc = ifp->if_softc; 947105293Sume int s; 94879106Sbrooks 949105293Sume s = splnet(); 950105293Sume 95179106Sbrooks if (sc->gif_psrc) { 95279106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 95379106Sbrooks sc->gif_psrc = NULL; 95479106Sbrooks } 95579106Sbrooks if (sc->gif_pdst) { 95679106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 95779106Sbrooks sc->gif_pdst = NULL; 95879106Sbrooks } 959105293Sume /* it is safe to detach from both */ 960105293Sume#ifdef INET 961105293Sume (void)in_gif_detach(sc); 962105293Sume#endif 963105293Sume#ifdef INET6 964105293Sume (void)in6_gif_detach(sc); 965105293Sume#endif 966105293Sume 967105293Sume if (sc->gif_psrc && sc->gif_pdst) 968148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 969105293Sume else 970148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 971105293Sume splx(s); 97279106Sbrooks} 973