if_gif.c revision 163606
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 163606 2006-10-22 11:52:19Z rwatson $ */ 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> 4962587Sitojun#include <sys/protosw.h> 5079106Sbrooks#include <sys/conf.h> 5154263Sshin#include <machine/cpu.h> 5254263Sshin 5354263Sshin#include <net/if.h> 54130933Sbrooks#include <net/if_clone.h> 5554263Sshin#include <net/if_types.h> 5654263Sshin#include <net/netisr.h> 5754263Sshin#include <net/route.h> 5854263Sshin#include <net/bpf.h> 5954263Sshin 6054263Sshin#include <netinet/in.h> 6154263Sshin#include <netinet/in_systm.h> 6278064Sume#include <netinet/ip.h> 6378064Sume#ifdef INET 6454263Sshin#include <netinet/in_var.h> 6554263Sshin#include <netinet/in_gif.h> 6679106Sbrooks#include <netinet/ip_var.h> 6754263Sshin#endif /* INET */ 6854263Sshin 6954263Sshin#ifdef INET6 7054263Sshin#ifndef INET 7154263Sshin#include <netinet/in.h> 7254263Sshin#endif 7354263Sshin#include <netinet6/in6_var.h> 7454263Sshin#include <netinet/ip6.h> 7554263Sshin#include <netinet6/ip6_var.h> 76148385Sume#include <netinet6/scope6_var.h> 7754263Sshin#include <netinet6/in6_gif.h> 7862587Sitojun#include <netinet6/ip6protosw.h> 7954263Sshin#endif /* INET6 */ 8054263Sshin 8162587Sitojun#include <netinet/ip_encap.h> 82153621Sthompsa#include <net/ethernet.h> 83153621Sthompsa#include <net/if_bridgevar.h> 8454263Sshin#include <net/if_gif.h> 8554263Sshin 86163606Srwatson#include <security/mac/mac_framework.h> 87163606Srwatson 8879106Sbrooks#define GIFNAME "gif" 8962587Sitojun 90127305Srwatson/* 91127898Sru * gif_mtx protects the global gif_softc_list. 92127305Srwatson */ 93127305Srwatsonstatic struct mtx gif_mtx; 9479106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 9589065Smsmithstatic LIST_HEAD(, gif_softc) gif_softc_list; 9679106Sbrooks 9783998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9883998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 9983998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 10083998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 10183998Sbrooks 102153621Sthompsastatic void gif_start(struct ifnet *); 103160195Ssamstatic int gif_clone_create(struct if_clone *, int, caddr_t); 104128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 10579106Sbrooks 106130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 10779106Sbrooks 10892725Salfredstatic int gifmodevent(module_t, int, void *); 10979106Sbrooks 11091270SbrooksSYSCTL_DECL(_net_link); 11191270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 11291270Sbrooks "Generic Tunnel Interface"); 11362587Sitojun#ifndef MAX_GIF_NEST 11462587Sitojun/* 11591270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 11662587Sitojun * Since, setting a large value to this macro with a careless configuration 11762587Sitojun * may introduce system crash, we don't allow any nestings by default. 11862587Sitojun * If you need to configure nested gif tunnels, you can define this macro 11995023Ssuz * in your kernel configuration file. However, if you do so, please be 12062587Sitojun * careful to configure the tunnels so that it won't make a loop. 12162587Sitojun */ 12262587Sitojun#define MAX_GIF_NEST 1 12362587Sitojun#endif 12462587Sitojunstatic int max_gif_nesting = MAX_GIF_NEST; 12591270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 12691270Sbrooks &max_gif_nesting, 0, "Max nested tunnels"); 12762587Sitojun 12891270Sbrooks/* 12991270Sbrooks * By default, we disallow creation of multiple tunnels between the same 13091270Sbrooks * pair of addresses. Some applications require this functionality so 13191270Sbrooks * we allow control over this check here. 13291270Sbrooks */ 13391270Sbrooks#ifdef XBONEHACK 13491270Sbrooksstatic int parallel_tunnels = 1; 13591270Sbrooks#else 13691270Sbrooksstatic int parallel_tunnels = 0; 13791270Sbrooks#endif 13891270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 13991270Sbrooks ¶llel_tunnels, 0, "Allow parallel tunnels?"); 14091270Sbrooks 141128209Sbrooksstatic int 142160195Ssamgif_clone_create(ifc, unit, params) 14379106Sbrooks struct if_clone *ifc; 14492081Smux int unit; 145160195Ssam caddr_t params; 14654263Sshin{ 14778064Sume struct gif_softc *sc; 14854263Sshin 149131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 150147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 151147256Sbrooks if (GIF2IFP(sc) == NULL) { 152147256Sbrooks free(sc, M_GIF); 153147256Sbrooks return (ENOSPC); 154147256Sbrooks } 15579106Sbrooks 156155037Sglebius GIF_LOCK_INIT(sc); 157155037Sglebius 158147256Sbrooks GIF2IFP(sc)->if_softc = sc; 159147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 16079106Sbrooks 16179106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 16262587Sitojun 163147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 164147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 165147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 16678064Sume#if 0 16779106Sbrooks /* turn off ingress filter */ 168147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 16978064Sume#endif 170147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 171153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 172147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 173147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 174147256Sbrooks if_attach(GIF2IFP(sc)); 175147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 17683998Sbrooks if (ng_gif_attach_p != NULL) 177147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 178155037Sglebius 179155037Sglebius mtx_lock(&gif_mtx); 180155037Sglebius LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 181155037Sglebius mtx_unlock(&gif_mtx); 182155037Sglebius 183155037Sglebius return (0); 18479106Sbrooks} 18579106Sbrooks 186127305Srwatsonstatic void 187151266Sthompsagif_clone_destroy(ifp) 188151266Sthompsa struct ifnet *ifp; 18979106Sbrooks{ 19079106Sbrooks int err; 191151266Sthompsa struct gif_softc *sc = ifp->if_softc; 19279106Sbrooks 193151266Sthompsa mtx_lock(&gif_mtx); 194151266Sthompsa LIST_REMOVE(sc, gif_list); 195151266Sthompsa mtx_unlock(&gif_mtx); 196151266Sthompsa 197127305Srwatson gif_delete_tunnel(ifp); 198105293Sume#ifdef INET6 199105293Sume if (sc->encap_cookie6 != NULL) { 200105293Sume err = encap_detach(sc->encap_cookie6); 201105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 202105293Sume } 203105293Sume#endif 204105293Sume#ifdef INET 20579106Sbrooks if (sc->encap_cookie4 != NULL) { 20679106Sbrooks err = encap_detach(sc->encap_cookie4); 20779106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 20879106Sbrooks } 209105293Sume#endif 21079106Sbrooks 21183998Sbrooks if (ng_gif_detach_p != NULL) 21283998Sbrooks (*ng_gif_detach_p)(ifp); 21379106Sbrooks bpfdetach(ifp); 21479106Sbrooks if_detach(ifp); 215147256Sbrooks if_free(ifp); 21679106Sbrooks 217155037Sglebius GIF_LOCK_DESTROY(sc); 218155037Sglebius 21979106Sbrooks free(sc, M_GIF); 22079106Sbrooks} 22179106Sbrooks 22279106Sbrooksstatic int 22379106Sbrooksgifmodevent(mod, type, data) 22479106Sbrooks module_t mod; 22579106Sbrooks int type; 22679106Sbrooks void *data; 22779106Sbrooks{ 22879106Sbrooks 22979106Sbrooks switch (type) { 23079106Sbrooks case MOD_LOAD: 231127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 23283997Sbrooks LIST_INIT(&gif_softc_list); 23379106Sbrooks if_clone_attach(&gif_cloner); 23479106Sbrooks 23579106Sbrooks#ifdef INET6 23679106Sbrooks ip6_gif_hlim = GIF_HLIM; 23762587Sitojun#endif 23879106Sbrooks 23979106Sbrooks break; 24079106Sbrooks case MOD_UNLOAD: 24179106Sbrooks if_clone_detach(&gif_cloner); 242127305Srwatson mtx_destroy(&gif_mtx); 24379106Sbrooks#ifdef INET6 24479106Sbrooks ip6_gif_hlim = 0; 24562587Sitojun#endif 24679106Sbrooks break; 247132199Sphk default: 248132199Sphk return EOPNOTSUPP; 24954263Sshin } 25079106Sbrooks return 0; 25154263Sshin} 25254263Sshin 25379106Sbrooksstatic moduledata_t gif_mod = { 25479106Sbrooks "if_gif", 25579106Sbrooks gifmodevent, 25679106Sbrooks 0 25779106Sbrooks}; 25854263Sshin 25979106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 26083997SbrooksMODULE_VERSION(if_gif, 1); 26179106Sbrooks 262105293Sumeint 26362587Sitojungif_encapcheck(m, off, proto, arg) 26462587Sitojun const struct mbuf *m; 26562587Sitojun int off; 26662587Sitojun int proto; 26762587Sitojun void *arg; 26862587Sitojun{ 26962587Sitojun struct ip ip; 27062587Sitojun struct gif_softc *sc; 27162587Sitojun 27262587Sitojun sc = (struct gif_softc *)arg; 27362587Sitojun if (sc == NULL) 27462587Sitojun return 0; 27562587Sitojun 276147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 27762587Sitojun return 0; 27862587Sitojun 27962587Sitojun /* no physical address */ 28062587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 28162587Sitojun return 0; 28262587Sitojun 28362587Sitojun switch (proto) { 28462587Sitojun#ifdef INET 28562587Sitojun case IPPROTO_IPV4: 28662587Sitojun break; 28762587Sitojun#endif 28862587Sitojun#ifdef INET6 28962587Sitojun case IPPROTO_IPV6: 29062587Sitojun break; 29162587Sitojun#endif 292153621Sthompsa case IPPROTO_ETHERIP: 293153621Sthompsa break; 294153621Sthompsa 29562587Sitojun default: 29662587Sitojun return 0; 29762587Sitojun } 29862587Sitojun 299105339Sume /* Bail on short packets */ 300105339Sume if (m->m_pkthdr.len < sizeof(ip)) 301105339Sume return 0; 302105339Sume 30391327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 30462587Sitojun 30562587Sitojun switch (ip.ip_v) { 30662587Sitojun#ifdef INET 30762587Sitojun case 4: 30862587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 30962587Sitojun sc->gif_pdst->sa_family != AF_INET) 31062587Sitojun return 0; 31162587Sitojun return gif_encapcheck4(m, off, proto, arg); 31262587Sitojun#endif 31362587Sitojun#ifdef INET6 31462587Sitojun case 6: 315105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 316105293Sume return 0; 31762587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 31862587Sitojun sc->gif_pdst->sa_family != AF_INET6) 31962587Sitojun return 0; 32062587Sitojun return gif_encapcheck6(m, off, proto, arg); 32162587Sitojun#endif 32262587Sitojun default: 32362587Sitojun return 0; 32462587Sitojun } 32562587Sitojun} 32662587Sitojun 327153621Sthompsastatic void 328153621Sthompsagif_start(struct ifnet *ifp) 329153621Sthompsa{ 330153621Sthompsa struct gif_softc *sc; 331153621Sthompsa struct mbuf *m; 332153621Sthompsa 333153621Sthompsa sc = ifp->if_softc; 334153621Sthompsa 335153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 336153621Sthompsa for (;;) { 337153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 338153621Sthompsa if (m == 0) 339153621Sthompsa break; 340153621Sthompsa 341153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 342153621Sthompsa 343153621Sthompsa } 344153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 345153621Sthompsa 346153621Sthompsa return; 347153621Sthompsa} 348153621Sthompsa 34954263Sshinint 35054263Sshingif_output(ifp, m, dst, rt) 35154263Sshin struct ifnet *ifp; 35254263Sshin struct mbuf *m; 35354263Sshin struct sockaddr *dst; 35454263Sshin struct rtentry *rt; /* added in net2 */ 35554263Sshin{ 356147256Sbrooks struct gif_softc *sc = ifp->if_softc; 357127898Sru struct m_tag *mtag; 35854263Sshin int error = 0; 359127898Sru int gif_called; 360147611Sdwmalone u_int32_t af; 36154263Sshin 362101182Srwatson#ifdef MAC 363101182Srwatson error = mac_check_ifnet_transmit(ifp, m); 364101739Srwatson if (error) { 365101739Srwatson m_freem(m); 366101739Srwatson goto end; 367101739Srwatson } 368101182Srwatson#endif 369101182Srwatson 37054263Sshin /* 37154263Sshin * gif may cause infinite recursion calls when misconfigured. 372127898Sru * We'll prevent this by detecting loops. 373127898Sru * 374127898Sru * High nesting level may cause stack exhaustion. 37554263Sshin * We'll prevent this by introducing upper limit. 37654263Sshin */ 377127898Sru gif_called = 1; 378127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 379127898Sru while (mtag != NULL) { 380127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 381127898Sru log(LOG_NOTICE, 382127898Sru "gif_output: loop detected on %s\n", 383127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 384127898Sru m_freem(m); 385127898Sru error = EIO; /* is there better errno? */ 386127898Sru goto end; 387127898Sru } 388127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 389127898Sru gif_called++; 390127898Sru } 391127898Sru if (gif_called > max_gif_nesting) { 39254263Sshin log(LOG_NOTICE, 39354263Sshin "gif_output: recursively called too many times(%d)\n", 394127303Srwatson gif_called); 39554263Sshin m_freem(m); 39654263Sshin error = EIO; /* is there better errno? */ 39754263Sshin goto end; 39854263Sshin } 399127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 400127898Sru M_NOWAIT); 401127898Sru if (mtag == NULL) { 402127898Sru m_freem(m); 403127898Sru error = ENOMEM; 404127898Sru goto end; 405127898Sru } 406127898Sru *(struct ifnet **)(mtag + 1) = ifp; 407127898Sru m_tag_prepend(m, mtag); 40862587Sitojun 40954263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 410155037Sglebius 411155037Sglebius GIF_LOCK(sc); 412155037Sglebius 41354263Sshin if (!(ifp->if_flags & IFF_UP) || 41454263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 415159174Sglebius GIF_UNLOCK(sc); 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; 428159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 42962587Sitojun ifp->if_opackets++; 43054263Sshin ifp->if_obytes += m->m_pkthdr.len; 43154263Sshin 432153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 433153621Sthompsa if (ifp->if_bridge) 434153621Sthompsa af = AF_LINK; 435153621Sthompsa 43678064Sume /* inner AF-specific encapsulation */ 43778064Sume 43862587Sitojun /* XXX should we check if our outer source is legal? */ 43962587Sitojun 44078064Sume /* dispatch to output logic based on outer AF */ 44154263Sshin switch (sc->gif_psrc->sa_family) { 44254263Sshin#ifdef INET 44354263Sshin case AF_INET: 444153621Sthompsa error = in_gif_output(ifp, af, m); 44554263Sshin break; 44654263Sshin#endif 44754263Sshin#ifdef INET6 44854263Sshin case AF_INET6: 449153621Sthompsa error = in6_gif_output(ifp, af, m); 45054263Sshin break; 45154263Sshin#endif 45254263Sshin default: 45362587Sitojun m_freem(m); 45454263Sshin error = ENETDOWN; 45554263Sshin } 45654263Sshin 457159174Sglebius GIF_UNLOCK(sc); 45854263Sshin end: 45978064Sume if (error) 46078064Sume ifp->if_oerrors++; 461155037Sglebius return (error); 46254263Sshin} 46354263Sshin 46454263Sshinvoid 465105338Sumegif_input(m, af, ifp) 46654263Sshin struct mbuf *m; 46754263Sshin int af; 468105338Sume struct ifnet *ifp; 46954263Sshin{ 470153621Sthompsa int isr, n; 471153621Sthompsa struct etherip_header *eip; 47254263Sshin 473105338Sume if (ifp == NULL) { 47454263Sshin /* just in case */ 47554263Sshin m_freem(m); 47654263Sshin return; 47754263Sshin } 47854263Sshin 479105338Sume m->m_pkthdr.rcvif = ifp; 480101182Srwatson 481101182Srwatson#ifdef MAC 482105338Sume mac_create_mbuf_from_ifnet(ifp, m); 483101182Srwatson#endif 484101182Srwatson 485159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 48678064Sume u_int32_t af1 = af; 487123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 48854263Sshin } 48954263Sshin 49083998Sbrooks if (ng_gif_input_p != NULL) { 491105338Sume (*ng_gif_input_p)(ifp, &m, af); 49283998Sbrooks if (m == NULL) 49383998Sbrooks return; 49483998Sbrooks } 49583998Sbrooks 49654263Sshin /* 49754263Sshin * Put the packet to the network layer input queue according to the 49854263Sshin * specified address family. 49954263Sshin * Note: older versions of gif_input directly called network layer 50095023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 50154263Sshin * prevent too many recursive calls of such input functions, which 50295023Ssuz * might cause kernel panic. But the change may introduce another 50354263Sshin * problem; if the input queue is full, packets are discarded. 50495023Ssuz * The kernel stack overflow really happened, and we believed 50595023Ssuz * queue-full rarely occurs, so we changed the policy. 50654263Sshin */ 50754263Sshin switch (af) { 50854263Sshin#ifdef INET 50954263Sshin case AF_INET: 51054263Sshin isr = NETISR_IP; 51154263Sshin break; 51254263Sshin#endif 51354263Sshin#ifdef INET6 51454263Sshin case AF_INET6: 51554263Sshin isr = NETISR_IPV6; 51654263Sshin break; 51754263Sshin#endif 518153621Sthompsa case AF_LINK: 519153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 520153621Sthompsa if (n > m->m_len) { 521153621Sthompsa m = m_pullup(m, n); 522153621Sthompsa if (m == NULL) { 523153621Sthompsa ifp->if_ierrors++; 524153621Sthompsa return; 525153621Sthompsa } 526153621Sthompsa } 527153621Sthompsa 528153621Sthompsa eip = mtod(m, struct etherip_header *); 529153621Sthompsa if (eip->eip_ver != 530153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 531153621Sthompsa /* discard unknown versions */ 532153621Sthompsa m_freem(m); 533153621Sthompsa return; 534153621Sthompsa } 535153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 536153621Sthompsa 537153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 538153621Sthompsa m->m_pkthdr.rcvif = ifp; 539153621Sthompsa 540153621Sthompsa if (ifp->if_bridge) 541153621Sthompsa BRIDGE_INPUT(ifp, m); 542153621Sthompsa 543153621Sthompsa if (m != NULL) 544153621Sthompsa m_freem(m); 545153621Sthompsa return; 546153621Sthompsa 54754263Sshin default: 54883998Sbrooks if (ng_gif_input_orphan_p != NULL) 549105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 55083998Sbrooks else 55183998Sbrooks m_freem(m); 55254263Sshin return; 55354263Sshin } 55454263Sshin 555105338Sume ifp->if_ipackets++; 556105338Sume ifp->if_ibytes += m->m_pkthdr.len; 557111888Sjlemon netisr_dispatch(isr, m); 55854263Sshin} 55954263Sshin 56062587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 56154263Sshinint 56254263Sshingif_ioctl(ifp, cmd, data) 56354263Sshin struct ifnet *ifp; 56454263Sshin u_long cmd; 56554263Sshin caddr_t data; 56654263Sshin{ 567147256Sbrooks struct gif_softc *sc = ifp->if_softc; 56854263Sshin struct ifreq *ifr = (struct ifreq*)data; 56954263Sshin int error = 0, size; 57062587Sitojun struct sockaddr *dst, *src; 571105339Sume#ifdef SIOCSIFMTU /* xxx */ 572105339Sume u_long mtu; 573105339Sume#endif 574105339Sume 57554263Sshin switch (cmd) { 57654263Sshin case SIOCSIFADDR: 577105293Sume ifp->if_flags |= IFF_UP; 57854263Sshin break; 57962587Sitojun 58054263Sshin case SIOCSIFDSTADDR: 58154263Sshin break; 58254263Sshin 58354263Sshin case SIOCADDMULTI: 58454263Sshin case SIOCDELMULTI: 58554263Sshin break; 58654263Sshin 58762587Sitojun#ifdef SIOCSIFMTU /* xxx */ 58854263Sshin case SIOCGIFMTU: 58954263Sshin break; 59062587Sitojun 59154263Sshin case SIOCSIFMTU: 592105339Sume mtu = ifr->ifr_mtu; 593105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 594105339Sume return (EINVAL); 595105339Sume ifp->if_mtu = mtu; 59654263Sshin break; 59762587Sitojun#endif /* SIOCSIFMTU */ 59854263Sshin 599105339Sume#ifdef INET 60054263Sshin case SIOCSIFPHYADDR: 601105339Sume#endif 60254263Sshin#ifdef INET6 60354263Sshin case SIOCSIFPHYADDR_IN6: 60454263Sshin#endif /* INET6 */ 60578064Sume case SIOCSLIFPHYADDR: 60662587Sitojun switch (cmd) { 60778064Sume#ifdef INET 60862587Sitojun case SIOCSIFPHYADDR: 60954263Sshin src = (struct sockaddr *) 61054263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 61154263Sshin dst = (struct sockaddr *) 61254263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 61362587Sitojun break; 61478064Sume#endif 61562587Sitojun#ifdef INET6 61662587Sitojun case SIOCSIFPHYADDR_IN6: 61762587Sitojun src = (struct sockaddr *) 61862587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 61962587Sitojun dst = (struct sockaddr *) 62062587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 62162587Sitojun break; 62262587Sitojun#endif 62378064Sume case SIOCSLIFPHYADDR: 62478064Sume src = (struct sockaddr *) 62578064Sume &(((struct if_laddrreq *)data)->addr); 62678064Sume dst = (struct sockaddr *) 62778064Sume &(((struct if_laddrreq *)data)->dstaddr); 628105293Sume break; 62991327Sbrooks default: 630105293Sume return EINVAL; 63162587Sitojun } 63254263Sshin 63378064Sume /* sa_family must be equal */ 63478064Sume if (src->sa_family != dst->sa_family) 63578064Sume return EINVAL; 63678064Sume 63778064Sume /* validate sa_len */ 63878064Sume switch (src->sa_family) { 63978064Sume#ifdef INET 64078064Sume case AF_INET: 64178064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 64278064Sume return EINVAL; 64378064Sume break; 64478064Sume#endif 64578064Sume#ifdef INET6 64678064Sume case AF_INET6: 64778064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 64878064Sume return EINVAL; 64978064Sume break; 65078064Sume#endif 65178064Sume default: 65278064Sume return EAFNOSUPPORT; 65378064Sume } 65478064Sume switch (dst->sa_family) { 65578064Sume#ifdef INET 65678064Sume case AF_INET: 65778064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 65878064Sume return EINVAL; 65978064Sume break; 66078064Sume#endif 66178064Sume#ifdef INET6 66278064Sume case AF_INET6: 66378064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 66478064Sume return EINVAL; 66578064Sume break; 66678064Sume#endif 66778064Sume default: 66878064Sume return EAFNOSUPPORT; 66978064Sume } 67078064Sume 67178064Sume /* check sa_family looks sane for the cmd */ 67278064Sume switch (cmd) { 67378064Sume case SIOCSIFPHYADDR: 67478064Sume if (src->sa_family == AF_INET) 67578064Sume break; 67678064Sume return EAFNOSUPPORT; 67778064Sume#ifdef INET6 67878064Sume case SIOCSIFPHYADDR_IN6: 67978064Sume if (src->sa_family == AF_INET6) 68078064Sume break; 68178064Sume return EAFNOSUPPORT; 68278064Sume#endif /* INET6 */ 68378064Sume case SIOCSLIFPHYADDR: 68478064Sume /* checks done in the above */ 68578064Sume break; 68678064Sume } 68778064Sume 688147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 68962587Sitojun break; 69062587Sitojun 69162587Sitojun#ifdef SIOCDIFPHYADDR 69262587Sitojun case SIOCDIFPHYADDR: 693147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 69454263Sshin break; 69562587Sitojun#endif 69662587Sitojun 69754263Sshin case SIOCGIFPSRCADDR: 69854263Sshin#ifdef INET6 69954263Sshin case SIOCGIFPSRCADDR_IN6: 70054263Sshin#endif /* INET6 */ 70154263Sshin if (sc->gif_psrc == NULL) { 70254263Sshin error = EADDRNOTAVAIL; 70354263Sshin goto bad; 70454263Sshin } 70554263Sshin src = sc->gif_psrc; 70678064Sume switch (cmd) { 70754263Sshin#ifdef INET 70878064Sume case SIOCGIFPSRCADDR: 70954263Sshin dst = &ifr->ifr_addr; 71078064Sume size = sizeof(ifr->ifr_addr); 71154263Sshin break; 71254263Sshin#endif /* INET */ 71354263Sshin#ifdef INET6 71478064Sume case SIOCGIFPSRCADDR_IN6: 71554263Sshin dst = (struct sockaddr *) 71654263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 71778064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 71854263Sshin break; 71954263Sshin#endif /* INET6 */ 72054263Sshin default: 72154263Sshin error = EADDRNOTAVAIL; 72254263Sshin goto bad; 72354263Sshin } 72478064Sume if (src->sa_len > size) 72578064Sume return EINVAL; 72678064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 727148385Sume#ifdef INET6 728148385Sume if (dst->sa_family == AF_INET6) { 729148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 730148385Sume if (error != 0) 731148385Sume return (error); 732148385Sume } 733148385Sume#endif 73454263Sshin break; 73562587Sitojun 73654263Sshin case SIOCGIFPDSTADDR: 73754263Sshin#ifdef INET6 73854263Sshin case SIOCGIFPDSTADDR_IN6: 73954263Sshin#endif /* INET6 */ 74054263Sshin if (sc->gif_pdst == NULL) { 74154263Sshin error = EADDRNOTAVAIL; 74254263Sshin goto bad; 74354263Sshin } 74454263Sshin src = sc->gif_pdst; 74578064Sume switch (cmd) { 74654263Sshin#ifdef INET 74778064Sume case SIOCGIFPDSTADDR: 74854263Sshin dst = &ifr->ifr_addr; 74978064Sume size = sizeof(ifr->ifr_addr); 75054263Sshin break; 75154263Sshin#endif /* INET */ 75254263Sshin#ifdef INET6 75378064Sume case SIOCGIFPDSTADDR_IN6: 75454263Sshin dst = (struct sockaddr *) 75554263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 75678064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 75754263Sshin break; 75854263Sshin#endif /* INET6 */ 75954263Sshin default: 76054263Sshin error = EADDRNOTAVAIL; 76154263Sshin goto bad; 76254263Sshin } 76378064Sume if (src->sa_len > size) 76478064Sume return EINVAL; 76578064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 766148385Sume#ifdef INET6 767148385Sume if (dst->sa_family == AF_INET6) { 768148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 769148385Sume if (error != 0) 770148385Sume return (error); 771148385Sume } 772148385Sume#endif 77354263Sshin break; 77454263Sshin 77578064Sume case SIOCGLIFPHYADDR: 77678064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 77778064Sume error = EADDRNOTAVAIL; 77878064Sume goto bad; 77978064Sume } 78078064Sume 78178064Sume /* copy src */ 78278064Sume src = sc->gif_psrc; 78378064Sume dst = (struct sockaddr *) 78478064Sume &(((struct if_laddrreq *)data)->addr); 78578064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 78678064Sume if (src->sa_len > size) 78778064Sume return EINVAL; 78878064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 78978064Sume 79078064Sume /* copy dst */ 79178064Sume src = sc->gif_pdst; 79278064Sume dst = (struct sockaddr *) 79378064Sume &(((struct if_laddrreq *)data)->dstaddr); 79478064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 79578064Sume if (src->sa_len > size) 79678064Sume return EINVAL; 79778064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 79878064Sume break; 79978064Sume 80054263Sshin case SIOCSIFFLAGS: 80162587Sitojun /* if_ioctl() takes care of it */ 80254263Sshin break; 80354263Sshin 80454263Sshin default: 80554263Sshin error = EINVAL; 80654263Sshin break; 80754263Sshin } 80854263Sshin bad: 80954263Sshin return error; 81054263Sshin} 81179106Sbrooks 812127305Srwatson/* 813127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 814127305Srwatson * if a given tunnel is already present happens before we perform a 815127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 816127305Srwatson * re-ordered so that the check and replacement can be atomic using 817127305Srwatson * a mutex. 818127305Srwatson */ 819105293Sumeint 820105293Sumegif_set_tunnel(ifp, src, dst) 821105293Sume struct ifnet *ifp; 822105293Sume struct sockaddr *src; 823105293Sume struct sockaddr *dst; 824105293Sume{ 825147256Sbrooks struct gif_softc *sc = ifp->if_softc; 826105293Sume struct gif_softc *sc2; 827105293Sume struct sockaddr *osrc, *odst, *sa; 828105293Sume int error = 0; 829105293Sume 830127305Srwatson mtx_lock(&gif_mtx); 831105293Sume LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 832105293Sume if (sc2 == sc) 833105293Sume continue; 834105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 835105293Sume continue; 836105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 837105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 838105293Sume sc2->gif_psrc->sa_family != src->sa_family || 839105293Sume sc2->gif_psrc->sa_len != src->sa_len) 840105293Sume continue; 841105293Sume 842105293Sume /* 843105293Sume * Disallow parallel tunnels unless instructed 844105293Sume * otherwise. 845105293Sume */ 846105293Sume if (!parallel_tunnels && 847105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 848105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 849105293Sume error = EADDRNOTAVAIL; 850127305Srwatson mtx_unlock(&gif_mtx); 851105293Sume goto bad; 852105293Sume } 853105293Sume 854105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 855105293Sume } 856127305Srwatson mtx_unlock(&gif_mtx); 857105293Sume 858105293Sume /* XXX we can detach from both, but be polite just in case */ 859105293Sume if (sc->gif_psrc) 860105293Sume switch (sc->gif_psrc->sa_family) { 861105293Sume#ifdef INET 862105293Sume case AF_INET: 863105293Sume (void)in_gif_detach(sc); 864105293Sume break; 865105293Sume#endif 866105293Sume#ifdef INET6 867105293Sume case AF_INET6: 868105293Sume (void)in6_gif_detach(sc); 869105293Sume break; 870105293Sume#endif 871105293Sume } 872105293Sume 873105293Sume osrc = sc->gif_psrc; 874111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 875105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 876105293Sume sc->gif_psrc = sa; 877105293Sume 878105293Sume odst = sc->gif_pdst; 879111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 880105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 881105293Sume sc->gif_pdst = sa; 882105293Sume 883105293Sume switch (sc->gif_psrc->sa_family) { 884105293Sume#ifdef INET 885105293Sume case AF_INET: 886105293Sume error = in_gif_attach(sc); 887105293Sume break; 888105293Sume#endif 889105293Sume#ifdef INET6 890105293Sume case AF_INET6: 891148385Sume /* 892148385Sume * Check validity of the scope zone ID of the addresses, and 893148385Sume * convert it into the kernel internal form if necessary. 894148385Sume */ 895148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 896148385Sume if (error != 0) 897148385Sume break; 898148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 899148385Sume if (error != 0) 900148385Sume break; 901105293Sume error = in6_gif_attach(sc); 902105293Sume break; 903105293Sume#endif 904105293Sume } 905105293Sume if (error) { 906105293Sume /* rollback */ 907105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 908105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 909105293Sume sc->gif_psrc = osrc; 910105293Sume sc->gif_pdst = odst; 911105293Sume goto bad; 912105293Sume } 913105293Sume 914105293Sume if (osrc) 915105293Sume free((caddr_t)osrc, M_IFADDR); 916105293Sume if (odst) 917105293Sume free((caddr_t)odst, M_IFADDR); 918105293Sume 919105293Sume bad: 920105293Sume if (sc->gif_psrc && sc->gif_pdst) 921148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 922105293Sume else 923148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 924105293Sume 925105293Sume return error; 926105293Sume} 927105293Sume 92879106Sbrooksvoid 929105293Sumegif_delete_tunnel(ifp) 930105293Sume struct ifnet *ifp; 93179106Sbrooks{ 932147256Sbrooks struct gif_softc *sc = ifp->if_softc; 93379106Sbrooks 93479106Sbrooks if (sc->gif_psrc) { 93579106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 93679106Sbrooks sc->gif_psrc = NULL; 93779106Sbrooks } 93879106Sbrooks if (sc->gif_pdst) { 93979106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 94079106Sbrooks sc->gif_pdst = NULL; 94179106Sbrooks } 942105293Sume /* it is safe to detach from both */ 943105293Sume#ifdef INET 944105293Sume (void)in_gif_detach(sc); 945105293Sume#endif 946105293Sume#ifdef INET6 947105293Sume (void)in6_gif_detach(sc); 948105293Sume#endif 949160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 95079106Sbrooks} 951