if_gif.c revision 176879
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 176879 2008-03-06 19:02:37Z 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> 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 141176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 142176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 143176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 144176879Sthompsa#ifndef ETHER_IS_BROADCAST 145176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 146176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 147176879Sthompsa#endif 148176879Sthompsa 149128209Sbrooksstatic int 150160195Ssamgif_clone_create(ifc, unit, params) 15179106Sbrooks struct if_clone *ifc; 15292081Smux int unit; 153160195Ssam caddr_t params; 15454263Sshin{ 15578064Sume struct gif_softc *sc; 15654263Sshin 157131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 158147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 159147256Sbrooks if (GIF2IFP(sc) == NULL) { 160147256Sbrooks free(sc, M_GIF); 161147256Sbrooks return (ENOSPC); 162147256Sbrooks } 16379106Sbrooks 164155037Sglebius GIF_LOCK_INIT(sc); 165155037Sglebius 166147256Sbrooks GIF2IFP(sc)->if_softc = sc; 167147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 16879106Sbrooks 16979106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 17062587Sitojun 171147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 172147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 173147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17478064Sume#if 0 17579106Sbrooks /* turn off ingress filter */ 176147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 17778064Sume#endif 178147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 179153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 180147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 181147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 182147256Sbrooks if_attach(GIF2IFP(sc)); 183147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 18483998Sbrooks if (ng_gif_attach_p != NULL) 185147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 186155037Sglebius 187155037Sglebius mtx_lock(&gif_mtx); 188155037Sglebius LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 189155037Sglebius mtx_unlock(&gif_mtx); 190155037Sglebius 191155037Sglebius return (0); 19279106Sbrooks} 19379106Sbrooks 194127305Srwatsonstatic void 195151266Sthompsagif_clone_destroy(ifp) 196151266Sthompsa struct ifnet *ifp; 19779106Sbrooks{ 19879106Sbrooks int err; 199151266Sthompsa struct gif_softc *sc = ifp->if_softc; 20079106Sbrooks 201151266Sthompsa mtx_lock(&gif_mtx); 202151266Sthompsa LIST_REMOVE(sc, gif_list); 203151266Sthompsa mtx_unlock(&gif_mtx); 204151266Sthompsa 205127305Srwatson gif_delete_tunnel(ifp); 206105293Sume#ifdef INET6 207105293Sume if (sc->encap_cookie6 != NULL) { 208105293Sume err = encap_detach(sc->encap_cookie6); 209105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 210105293Sume } 211105293Sume#endif 212105293Sume#ifdef INET 21379106Sbrooks if (sc->encap_cookie4 != NULL) { 21479106Sbrooks err = encap_detach(sc->encap_cookie4); 21579106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 21679106Sbrooks } 217105293Sume#endif 21879106Sbrooks 21983998Sbrooks if (ng_gif_detach_p != NULL) 22083998Sbrooks (*ng_gif_detach_p)(ifp); 22179106Sbrooks bpfdetach(ifp); 22279106Sbrooks if_detach(ifp); 223147256Sbrooks if_free(ifp); 22479106Sbrooks 225155037Sglebius GIF_LOCK_DESTROY(sc); 226155037Sglebius 22779106Sbrooks free(sc, M_GIF); 22879106Sbrooks} 22979106Sbrooks 23079106Sbrooksstatic int 23179106Sbrooksgifmodevent(mod, type, data) 23279106Sbrooks module_t mod; 23379106Sbrooks int type; 23479106Sbrooks void *data; 23579106Sbrooks{ 23679106Sbrooks 23779106Sbrooks switch (type) { 23879106Sbrooks case MOD_LOAD: 239127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 24083997Sbrooks LIST_INIT(&gif_softc_list); 24179106Sbrooks if_clone_attach(&gif_cloner); 24279106Sbrooks 24379106Sbrooks#ifdef INET6 24479106Sbrooks ip6_gif_hlim = GIF_HLIM; 24562587Sitojun#endif 24679106Sbrooks 24779106Sbrooks break; 24879106Sbrooks case MOD_UNLOAD: 24979106Sbrooks if_clone_detach(&gif_cloner); 250127305Srwatson mtx_destroy(&gif_mtx); 25179106Sbrooks#ifdef INET6 25279106Sbrooks ip6_gif_hlim = 0; 25362587Sitojun#endif 25479106Sbrooks break; 255132199Sphk default: 256132199Sphk return EOPNOTSUPP; 25754263Sshin } 25879106Sbrooks return 0; 25954263Sshin} 26054263Sshin 26179106Sbrooksstatic moduledata_t gif_mod = { 26279106Sbrooks "if_gif", 26379106Sbrooks gifmodevent, 26479106Sbrooks 0 26579106Sbrooks}; 26654263Sshin 26779106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 26883997SbrooksMODULE_VERSION(if_gif, 1); 26979106Sbrooks 270105293Sumeint 27162587Sitojungif_encapcheck(m, off, proto, arg) 27262587Sitojun const struct mbuf *m; 27362587Sitojun int off; 27462587Sitojun int proto; 27562587Sitojun void *arg; 27662587Sitojun{ 27762587Sitojun struct ip ip; 27862587Sitojun struct gif_softc *sc; 27962587Sitojun 28062587Sitojun sc = (struct gif_softc *)arg; 28162587Sitojun if (sc == NULL) 28262587Sitojun return 0; 28362587Sitojun 284147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 28562587Sitojun return 0; 28662587Sitojun 28762587Sitojun /* no physical address */ 28862587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 28962587Sitojun return 0; 29062587Sitojun 29162587Sitojun switch (proto) { 29262587Sitojun#ifdef INET 29362587Sitojun case IPPROTO_IPV4: 29462587Sitojun break; 29562587Sitojun#endif 29662587Sitojun#ifdef INET6 29762587Sitojun case IPPROTO_IPV6: 29862587Sitojun break; 29962587Sitojun#endif 300153621Sthompsa case IPPROTO_ETHERIP: 301153621Sthompsa break; 302153621Sthompsa 30362587Sitojun default: 30462587Sitojun return 0; 30562587Sitojun } 30662587Sitojun 307105339Sume /* Bail on short packets */ 308105339Sume if (m->m_pkthdr.len < sizeof(ip)) 309105339Sume return 0; 310105339Sume 31191327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 31262587Sitojun 31362587Sitojun switch (ip.ip_v) { 31462587Sitojun#ifdef INET 31562587Sitojun case 4: 31662587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 31762587Sitojun sc->gif_pdst->sa_family != AF_INET) 31862587Sitojun return 0; 31962587Sitojun return gif_encapcheck4(m, off, proto, arg); 32062587Sitojun#endif 32162587Sitojun#ifdef INET6 32262587Sitojun case 6: 323105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 324105293Sume return 0; 32562587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 32662587Sitojun sc->gif_pdst->sa_family != AF_INET6) 32762587Sitojun return 0; 32862587Sitojun return gif_encapcheck6(m, off, proto, arg); 32962587Sitojun#endif 33062587Sitojun default: 33162587Sitojun return 0; 33262587Sitojun } 33362587Sitojun} 33462587Sitojun 335153621Sthompsastatic void 336153621Sthompsagif_start(struct ifnet *ifp) 337153621Sthompsa{ 338153621Sthompsa struct gif_softc *sc; 339153621Sthompsa struct mbuf *m; 340153621Sthompsa 341153621Sthompsa sc = ifp->if_softc; 342153621Sthompsa 343153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 344153621Sthompsa for (;;) { 345153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 346153621Sthompsa if (m == 0) 347153621Sthompsa break; 348153621Sthompsa 349153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 350153621Sthompsa 351153621Sthompsa } 352153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 353153621Sthompsa 354153621Sthompsa return; 355153621Sthompsa} 356153621Sthompsa 35754263Sshinint 35854263Sshingif_output(ifp, m, dst, rt) 35954263Sshin struct ifnet *ifp; 36054263Sshin struct mbuf *m; 36154263Sshin struct sockaddr *dst; 36254263Sshin struct rtentry *rt; /* added in net2 */ 36354263Sshin{ 364147256Sbrooks struct gif_softc *sc = ifp->if_softc; 365127898Sru struct m_tag *mtag; 36654263Sshin int error = 0; 367127898Sru int gif_called; 368147611Sdwmalone u_int32_t af; 36954263Sshin 370101182Srwatson#ifdef MAC 371172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 372101739Srwatson if (error) { 373101739Srwatson m_freem(m); 374101739Srwatson goto end; 375101739Srwatson } 376101182Srwatson#endif 377101182Srwatson 37854263Sshin /* 37954263Sshin * gif may cause infinite recursion calls when misconfigured. 380127898Sru * We'll prevent this by detecting loops. 381127898Sru * 382127898Sru * High nesting level may cause stack exhaustion. 38354263Sshin * We'll prevent this by introducing upper limit. 38454263Sshin */ 385127898Sru gif_called = 1; 386127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 387127898Sru while (mtag != NULL) { 388127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 389127898Sru log(LOG_NOTICE, 390127898Sru "gif_output: loop detected on %s\n", 391127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 392127898Sru m_freem(m); 393127898Sru error = EIO; /* is there better errno? */ 394127898Sru goto end; 395127898Sru } 396127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 397127898Sru gif_called++; 398127898Sru } 399127898Sru if (gif_called > max_gif_nesting) { 40054263Sshin log(LOG_NOTICE, 40154263Sshin "gif_output: recursively called too many times(%d)\n", 402127303Srwatson gif_called); 40354263Sshin m_freem(m); 40454263Sshin error = EIO; /* is there better errno? */ 40554263Sshin goto end; 40654263Sshin } 407127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 408127898Sru M_NOWAIT); 409127898Sru if (mtag == NULL) { 410127898Sru m_freem(m); 411127898Sru error = ENOMEM; 412127898Sru goto end; 413127898Sru } 414127898Sru *(struct ifnet **)(mtag + 1) = ifp; 415127898Sru m_tag_prepend(m, mtag); 41662587Sitojun 41754263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 418155037Sglebius 419155037Sglebius GIF_LOCK(sc); 420155037Sglebius 42154263Sshin if (!(ifp->if_flags & IFF_UP) || 42254263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 423159174Sglebius GIF_UNLOCK(sc); 42454263Sshin m_freem(m); 42554263Sshin error = ENETDOWN; 42654263Sshin goto end; 42754263Sshin } 42854263Sshin 429147611Sdwmalone /* BPF writes need to be handled specially. */ 430147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 431147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 432147611Sdwmalone dst->sa_family = af; 433147611Sdwmalone } 434147611Sdwmalone 435153621Sthompsa af = dst->sa_family; 436159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 43762587Sitojun ifp->if_opackets++; 43854263Sshin ifp->if_obytes += m->m_pkthdr.len; 43954263Sshin 440153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 441153621Sthompsa if (ifp->if_bridge) 442153621Sthompsa af = AF_LINK; 443153621Sthompsa 44478064Sume /* inner AF-specific encapsulation */ 44578064Sume 44662587Sitojun /* XXX should we check if our outer source is legal? */ 44762587Sitojun 44878064Sume /* dispatch to output logic based on outer AF */ 44954263Sshin switch (sc->gif_psrc->sa_family) { 45054263Sshin#ifdef INET 45154263Sshin case AF_INET: 452153621Sthompsa error = in_gif_output(ifp, af, m); 45354263Sshin break; 45454263Sshin#endif 45554263Sshin#ifdef INET6 45654263Sshin case AF_INET6: 457153621Sthompsa error = in6_gif_output(ifp, af, m); 45854263Sshin break; 45954263Sshin#endif 46054263Sshin default: 46162587Sitojun m_freem(m); 46254263Sshin error = ENETDOWN; 46354263Sshin } 46454263Sshin 465159174Sglebius GIF_UNLOCK(sc); 46654263Sshin end: 46778064Sume if (error) 46878064Sume ifp->if_oerrors++; 469155037Sglebius return (error); 47054263Sshin} 47154263Sshin 47254263Sshinvoid 473105338Sumegif_input(m, af, ifp) 47454263Sshin struct mbuf *m; 47554263Sshin int af; 476105338Sume struct ifnet *ifp; 47754263Sshin{ 478153621Sthompsa int isr, n; 479153621Sthompsa struct etherip_header *eip; 480176879Sthompsa struct ether_header *eh; 481176879Sthompsa struct ifnet *oldifp; 48254263Sshin 483105338Sume if (ifp == NULL) { 48454263Sshin /* just in case */ 48554263Sshin m_freem(m); 48654263Sshin return; 48754263Sshin } 48854263Sshin 489105338Sume m->m_pkthdr.rcvif = ifp; 490101182Srwatson 491101182Srwatson#ifdef MAC 492172930Srwatson mac_ifnet_create_mbuf(ifp, m); 493101182Srwatson#endif 494101182Srwatson 495159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 49678064Sume u_int32_t af1 = af; 497123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 49854263Sshin } 49954263Sshin 50083998Sbrooks if (ng_gif_input_p != NULL) { 501105338Sume (*ng_gif_input_p)(ifp, &m, af); 50283998Sbrooks if (m == NULL) 50383998Sbrooks return; 50483998Sbrooks } 50583998Sbrooks 50654263Sshin /* 50754263Sshin * Put the packet to the network layer input queue according to the 50854263Sshin * specified address family. 50954263Sshin * Note: older versions of gif_input directly called network layer 51095023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 51154263Sshin * prevent too many recursive calls of such input functions, which 51295023Ssuz * might cause kernel panic. But the change may introduce another 51354263Sshin * problem; if the input queue is full, packets are discarded. 51495023Ssuz * The kernel stack overflow really happened, and we believed 51595023Ssuz * queue-full rarely occurs, so we changed the policy. 51654263Sshin */ 51754263Sshin switch (af) { 51854263Sshin#ifdef INET 51954263Sshin case AF_INET: 52054263Sshin isr = NETISR_IP; 52154263Sshin break; 52254263Sshin#endif 52354263Sshin#ifdef INET6 52454263Sshin case AF_INET6: 52554263Sshin isr = NETISR_IPV6; 52654263Sshin break; 52754263Sshin#endif 528153621Sthompsa case AF_LINK: 529153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 530153621Sthompsa if (n > m->m_len) { 531153621Sthompsa m = m_pullup(m, n); 532153621Sthompsa if (m == NULL) { 533153621Sthompsa ifp->if_ierrors++; 534153621Sthompsa return; 535153621Sthompsa } 536153621Sthompsa } 537153621Sthompsa 538153621Sthompsa eip = mtod(m, struct etherip_header *); 539153621Sthompsa if (eip->eip_ver != 540153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 541153621Sthompsa /* discard unknown versions */ 542153621Sthompsa m_freem(m); 543153621Sthompsa return; 544153621Sthompsa } 545153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 546153621Sthompsa 547153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 548153621Sthompsa m->m_pkthdr.rcvif = ifp; 549153621Sthompsa 550176879Sthompsa if (ifp->if_bridge) { 551176879Sthompsa oldifp = ifp; 552176879Sthompsa eh = mtod(m, struct ether_header *); 553176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 554176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 555176879Sthompsa m->m_flags |= M_BCAST; 556176879Sthompsa else 557176879Sthompsa m->m_flags |= M_MCAST; 558176879Sthompsa ifp->if_imcasts++; 559176879Sthompsa } 560153621Sthompsa BRIDGE_INPUT(ifp, m); 561176879Sthompsa 562176879Sthompsa if (m != NULL && ifp != oldifp) { 563176879Sthompsa /* 564176879Sthompsa * The bridge gave us back itself or one of the 565176879Sthompsa * members for which the frame is addressed. 566176879Sthompsa */ 567176879Sthompsa ether_demux(ifp, m); 568176879Sthompsa return; 569176879Sthompsa } 570176879Sthompsa } 571153621Sthompsa if (m != NULL) 572153621Sthompsa m_freem(m); 573153621Sthompsa return; 574153621Sthompsa 57554263Sshin default: 57683998Sbrooks if (ng_gif_input_orphan_p != NULL) 577105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 57883998Sbrooks else 57983998Sbrooks m_freem(m); 58054263Sshin return; 58154263Sshin } 58254263Sshin 583105338Sume ifp->if_ipackets++; 584105338Sume ifp->if_ibytes += m->m_pkthdr.len; 585111888Sjlemon netisr_dispatch(isr, m); 58654263Sshin} 58754263Sshin 58862587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 58954263Sshinint 59054263Sshingif_ioctl(ifp, cmd, data) 59154263Sshin struct ifnet *ifp; 59254263Sshin u_long cmd; 59354263Sshin caddr_t data; 59454263Sshin{ 595147256Sbrooks struct gif_softc *sc = ifp->if_softc; 59654263Sshin struct ifreq *ifr = (struct ifreq*)data; 59754263Sshin int error = 0, size; 59862587Sitojun struct sockaddr *dst, *src; 599105339Sume#ifdef SIOCSIFMTU /* xxx */ 600105339Sume u_long mtu; 601105339Sume#endif 602105339Sume 60354263Sshin switch (cmd) { 60454263Sshin case SIOCSIFADDR: 605105293Sume ifp->if_flags |= IFF_UP; 60654263Sshin break; 60762587Sitojun 60854263Sshin case SIOCSIFDSTADDR: 60954263Sshin break; 61054263Sshin 61154263Sshin case SIOCADDMULTI: 61254263Sshin case SIOCDELMULTI: 61354263Sshin break; 61454263Sshin 61562587Sitojun#ifdef SIOCSIFMTU /* xxx */ 61654263Sshin case SIOCGIFMTU: 61754263Sshin break; 61862587Sitojun 61954263Sshin case SIOCSIFMTU: 620105339Sume mtu = ifr->ifr_mtu; 621105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 622105339Sume return (EINVAL); 623105339Sume ifp->if_mtu = mtu; 62454263Sshin break; 62562587Sitojun#endif /* SIOCSIFMTU */ 62654263Sshin 627105339Sume#ifdef INET 62854263Sshin case SIOCSIFPHYADDR: 629105339Sume#endif 63054263Sshin#ifdef INET6 63154263Sshin case SIOCSIFPHYADDR_IN6: 63254263Sshin#endif /* INET6 */ 63378064Sume case SIOCSLIFPHYADDR: 63462587Sitojun switch (cmd) { 63578064Sume#ifdef INET 63662587Sitojun case SIOCSIFPHYADDR: 63754263Sshin src = (struct sockaddr *) 63854263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 63954263Sshin dst = (struct sockaddr *) 64054263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 64162587Sitojun break; 64278064Sume#endif 64362587Sitojun#ifdef INET6 64462587Sitojun case SIOCSIFPHYADDR_IN6: 64562587Sitojun src = (struct sockaddr *) 64662587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 64762587Sitojun dst = (struct sockaddr *) 64862587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 64962587Sitojun break; 65062587Sitojun#endif 65178064Sume case SIOCSLIFPHYADDR: 65278064Sume src = (struct sockaddr *) 65378064Sume &(((struct if_laddrreq *)data)->addr); 65478064Sume dst = (struct sockaddr *) 65578064Sume &(((struct if_laddrreq *)data)->dstaddr); 656105293Sume break; 65791327Sbrooks default: 658105293Sume return EINVAL; 65962587Sitojun } 66054263Sshin 66178064Sume /* sa_family must be equal */ 66278064Sume if (src->sa_family != dst->sa_family) 66378064Sume return EINVAL; 66478064Sume 66578064Sume /* validate sa_len */ 66678064Sume switch (src->sa_family) { 66778064Sume#ifdef INET 66878064Sume case AF_INET: 66978064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 67078064Sume return EINVAL; 67178064Sume break; 67278064Sume#endif 67378064Sume#ifdef INET6 67478064Sume case AF_INET6: 67578064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 67678064Sume return EINVAL; 67778064Sume break; 67878064Sume#endif 67978064Sume default: 68078064Sume return EAFNOSUPPORT; 68178064Sume } 68278064Sume switch (dst->sa_family) { 68378064Sume#ifdef INET 68478064Sume case AF_INET: 68578064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 68678064Sume return EINVAL; 68778064Sume break; 68878064Sume#endif 68978064Sume#ifdef INET6 69078064Sume case AF_INET6: 69178064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 69278064Sume return EINVAL; 69378064Sume break; 69478064Sume#endif 69578064Sume default: 69678064Sume return EAFNOSUPPORT; 69778064Sume } 69878064Sume 69978064Sume /* check sa_family looks sane for the cmd */ 70078064Sume switch (cmd) { 70178064Sume case SIOCSIFPHYADDR: 70278064Sume if (src->sa_family == AF_INET) 70378064Sume break; 70478064Sume return EAFNOSUPPORT; 70578064Sume#ifdef INET6 70678064Sume case SIOCSIFPHYADDR_IN6: 70778064Sume if (src->sa_family == AF_INET6) 70878064Sume break; 70978064Sume return EAFNOSUPPORT; 71078064Sume#endif /* INET6 */ 71178064Sume case SIOCSLIFPHYADDR: 71278064Sume /* checks done in the above */ 71378064Sume break; 71478064Sume } 71578064Sume 716147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 71762587Sitojun break; 71862587Sitojun 71962587Sitojun#ifdef SIOCDIFPHYADDR 72062587Sitojun case SIOCDIFPHYADDR: 721147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 72254263Sshin break; 72362587Sitojun#endif 72462587Sitojun 72554263Sshin case SIOCGIFPSRCADDR: 72654263Sshin#ifdef INET6 72754263Sshin case SIOCGIFPSRCADDR_IN6: 72854263Sshin#endif /* INET6 */ 72954263Sshin if (sc->gif_psrc == NULL) { 73054263Sshin error = EADDRNOTAVAIL; 73154263Sshin goto bad; 73254263Sshin } 73354263Sshin src = sc->gif_psrc; 73478064Sume switch (cmd) { 73554263Sshin#ifdef INET 73678064Sume case SIOCGIFPSRCADDR: 73754263Sshin dst = &ifr->ifr_addr; 73878064Sume size = sizeof(ifr->ifr_addr); 73954263Sshin break; 74054263Sshin#endif /* INET */ 74154263Sshin#ifdef INET6 74278064Sume case SIOCGIFPSRCADDR_IN6: 74354263Sshin dst = (struct sockaddr *) 74454263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 74578064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 74654263Sshin break; 74754263Sshin#endif /* INET6 */ 74854263Sshin default: 74954263Sshin error = EADDRNOTAVAIL; 75054263Sshin goto bad; 75154263Sshin } 75278064Sume if (src->sa_len > size) 75378064Sume return EINVAL; 75478064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 755148385Sume#ifdef INET6 756148385Sume if (dst->sa_family == AF_INET6) { 757148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 758148385Sume if (error != 0) 759148385Sume return (error); 760148385Sume } 761148385Sume#endif 76254263Sshin break; 76362587Sitojun 76454263Sshin case SIOCGIFPDSTADDR: 76554263Sshin#ifdef INET6 76654263Sshin case SIOCGIFPDSTADDR_IN6: 76754263Sshin#endif /* INET6 */ 76854263Sshin if (sc->gif_pdst == NULL) { 76954263Sshin error = EADDRNOTAVAIL; 77054263Sshin goto bad; 77154263Sshin } 77254263Sshin src = sc->gif_pdst; 77378064Sume switch (cmd) { 77454263Sshin#ifdef INET 77578064Sume case SIOCGIFPDSTADDR: 77654263Sshin dst = &ifr->ifr_addr; 77778064Sume size = sizeof(ifr->ifr_addr); 77854263Sshin break; 77954263Sshin#endif /* INET */ 78054263Sshin#ifdef INET6 78178064Sume case SIOCGIFPDSTADDR_IN6: 78254263Sshin dst = (struct sockaddr *) 78354263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 78478064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 78554263Sshin break; 78654263Sshin#endif /* INET6 */ 78754263Sshin default: 78854263Sshin error = EADDRNOTAVAIL; 78954263Sshin goto bad; 79054263Sshin } 79178064Sume if (src->sa_len > size) 79278064Sume return EINVAL; 79378064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 794148385Sume#ifdef INET6 795148385Sume if (dst->sa_family == AF_INET6) { 796148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 797148385Sume if (error != 0) 798148385Sume return (error); 799148385Sume } 800148385Sume#endif 80154263Sshin break; 80254263Sshin 80378064Sume case SIOCGLIFPHYADDR: 80478064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 80578064Sume error = EADDRNOTAVAIL; 80678064Sume goto bad; 80778064Sume } 80878064Sume 80978064Sume /* copy src */ 81078064Sume src = sc->gif_psrc; 81178064Sume dst = (struct sockaddr *) 81278064Sume &(((struct if_laddrreq *)data)->addr); 81378064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 81478064Sume if (src->sa_len > size) 81578064Sume return EINVAL; 81678064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 81778064Sume 81878064Sume /* copy dst */ 81978064Sume src = sc->gif_pdst; 82078064Sume dst = (struct sockaddr *) 82178064Sume &(((struct if_laddrreq *)data)->dstaddr); 82278064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 82378064Sume if (src->sa_len > size) 82478064Sume return EINVAL; 82578064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 82678064Sume break; 82778064Sume 82854263Sshin case SIOCSIFFLAGS: 82962587Sitojun /* if_ioctl() takes care of it */ 83054263Sshin break; 83154263Sshin 83254263Sshin default: 83354263Sshin error = EINVAL; 83454263Sshin break; 83554263Sshin } 83654263Sshin bad: 83754263Sshin return error; 83854263Sshin} 83979106Sbrooks 840127305Srwatson/* 841127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 842127305Srwatson * if a given tunnel is already present happens before we perform a 843127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 844127305Srwatson * re-ordered so that the check and replacement can be atomic using 845127305Srwatson * a mutex. 846127305Srwatson */ 847105293Sumeint 848105293Sumegif_set_tunnel(ifp, src, dst) 849105293Sume struct ifnet *ifp; 850105293Sume struct sockaddr *src; 851105293Sume struct sockaddr *dst; 852105293Sume{ 853147256Sbrooks struct gif_softc *sc = ifp->if_softc; 854105293Sume struct gif_softc *sc2; 855105293Sume struct sockaddr *osrc, *odst, *sa; 856105293Sume int error = 0; 857105293Sume 858127305Srwatson mtx_lock(&gif_mtx); 859105293Sume LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 860105293Sume if (sc2 == sc) 861105293Sume continue; 862105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 863105293Sume continue; 864105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 865105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 866105293Sume sc2->gif_psrc->sa_family != src->sa_family || 867105293Sume sc2->gif_psrc->sa_len != src->sa_len) 868105293Sume continue; 869105293Sume 870105293Sume /* 871105293Sume * Disallow parallel tunnels unless instructed 872105293Sume * otherwise. 873105293Sume */ 874105293Sume if (!parallel_tunnels && 875105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 876105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 877105293Sume error = EADDRNOTAVAIL; 878127305Srwatson mtx_unlock(&gif_mtx); 879105293Sume goto bad; 880105293Sume } 881105293Sume 882105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 883105293Sume } 884127305Srwatson mtx_unlock(&gif_mtx); 885105293Sume 886105293Sume /* XXX we can detach from both, but be polite just in case */ 887105293Sume if (sc->gif_psrc) 888105293Sume switch (sc->gif_psrc->sa_family) { 889105293Sume#ifdef INET 890105293Sume case AF_INET: 891105293Sume (void)in_gif_detach(sc); 892105293Sume break; 893105293Sume#endif 894105293Sume#ifdef INET6 895105293Sume case AF_INET6: 896105293Sume (void)in6_gif_detach(sc); 897105293Sume break; 898105293Sume#endif 899105293Sume } 900105293Sume 901105293Sume osrc = sc->gif_psrc; 902111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 903105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 904105293Sume sc->gif_psrc = sa; 905105293Sume 906105293Sume odst = sc->gif_pdst; 907111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 908105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 909105293Sume sc->gif_pdst = sa; 910105293Sume 911105293Sume switch (sc->gif_psrc->sa_family) { 912105293Sume#ifdef INET 913105293Sume case AF_INET: 914105293Sume error = in_gif_attach(sc); 915105293Sume break; 916105293Sume#endif 917105293Sume#ifdef INET6 918105293Sume case AF_INET6: 919148385Sume /* 920148385Sume * Check validity of the scope zone ID of the addresses, and 921148385Sume * convert it into the kernel internal form if necessary. 922148385Sume */ 923148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 924148385Sume if (error != 0) 925148385Sume break; 926148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 927148385Sume if (error != 0) 928148385Sume break; 929105293Sume error = in6_gif_attach(sc); 930105293Sume break; 931105293Sume#endif 932105293Sume } 933105293Sume if (error) { 934105293Sume /* rollback */ 935105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 936105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 937105293Sume sc->gif_psrc = osrc; 938105293Sume sc->gif_pdst = odst; 939105293Sume goto bad; 940105293Sume } 941105293Sume 942105293Sume if (osrc) 943105293Sume free((caddr_t)osrc, M_IFADDR); 944105293Sume if (odst) 945105293Sume free((caddr_t)odst, M_IFADDR); 946105293Sume 947105293Sume bad: 948105293Sume if (sc->gif_psrc && sc->gif_pdst) 949148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 950105293Sume else 951148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 952105293Sume 953105293Sume return error; 954105293Sume} 955105293Sume 95679106Sbrooksvoid 957105293Sumegif_delete_tunnel(ifp) 958105293Sume struct ifnet *ifp; 95979106Sbrooks{ 960147256Sbrooks struct gif_softc *sc = ifp->if_softc; 96179106Sbrooks 96279106Sbrooks if (sc->gif_psrc) { 96379106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 96479106Sbrooks sc->gif_psrc = NULL; 96579106Sbrooks } 96679106Sbrooks if (sc->gif_pdst) { 96779106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 96879106Sbrooks sc->gif_pdst = NULL; 96979106Sbrooks } 970105293Sume /* it is safe to detach from both */ 971105293Sume#ifdef INET 972105293Sume (void)in_gif_detach(sc); 973105293Sume#endif 974105293Sume#ifdef INET6 975105293Sume (void)in6_gif_detach(sc); 976105293Sume#endif 977160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 97879106Sbrooks} 979