if_gif.c revision 241394
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 241394 2012-10-10 08:36:38Z kevlo $ */ 295023Ssuz/* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 362587Sitojun 4139823Simp/*- 554263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 654263Sshin * All rights reserved. 754263Sshin * 854263Sshin * Redistribution and use in source and binary forms, with or without 954263Sshin * modification, are permitted provided that the following conditions 1054263Sshin * are met: 1154263Sshin * 1. Redistributions of source code must retain the above copyright 1254263Sshin * notice, this list of conditions and the following disclaimer. 1354263Sshin * 2. Redistributions in binary form must reproduce the above copyright 1454263Sshin * notice, this list of conditions and the following disclaimer in the 1554263Sshin * documentation and/or other materials provided with the distribution. 1654263Sshin * 3. Neither the name of the project nor the names of its contributors 1754263Sshin * may be used to endorse or promote products derived from this software 1854263Sshin * without specific prior written permission. 1954263Sshin * 2054263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2154263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2254263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2354263Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2454263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2554263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2654263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2754263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2854263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2954263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3054263Sshin * SUCH DAMAGE. 3154263Sshin */ 3254263Sshin 3354263Sshin#include "opt_inet.h" 3454263Sshin#include "opt_inet6.h" 3554263Sshin 3654263Sshin#include <sys/param.h> 3754263Sshin#include <sys/systm.h> 38219206Sbz#include <sys/jail.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> 49193664Shrs#include <sys/priv.h> 50178888Sjulian#include <sys/proc.h> 5162587Sitojun#include <sys/protosw.h> 5279106Sbrooks#include <sys/conf.h> 5354263Sshin#include <machine/cpu.h> 5454263Sshin 5554263Sshin#include <net/if.h> 56130933Sbrooks#include <net/if_clone.h> 5754263Sshin#include <net/if_types.h> 5854263Sshin#include <net/netisr.h> 5954263Sshin#include <net/route.h> 6054263Sshin#include <net/bpf.h> 61196019Srwatson#include <net/vnet.h> 6254263Sshin 6354263Sshin#include <netinet/in.h> 6454263Sshin#include <netinet/in_systm.h> 6578064Sume#include <netinet/ip.h> 6678064Sume#ifdef INET 6754263Sshin#include <netinet/in_var.h> 6854263Sshin#include <netinet/in_gif.h> 6979106Sbrooks#include <netinet/ip_var.h> 7054263Sshin#endif /* INET */ 7154263Sshin 7254263Sshin#ifdef INET6 7354263Sshin#ifndef INET 7454263Sshin#include <netinet/in.h> 7554263Sshin#endif 7654263Sshin#include <netinet6/in6_var.h> 7754263Sshin#include <netinet/ip6.h> 7854263Sshin#include <netinet6/ip6_var.h> 79148385Sume#include <netinet6/scope6_var.h> 8054263Sshin#include <netinet6/in6_gif.h> 8162587Sitojun#include <netinet6/ip6protosw.h> 8254263Sshin#endif /* INET6 */ 8354263Sshin 8462587Sitojun#include <netinet/ip_encap.h> 85153621Sthompsa#include <net/ethernet.h> 86153621Sthompsa#include <net/if_bridgevar.h> 8754263Sshin#include <net/if_gif.h> 8854263Sshin 89163606Srwatson#include <security/mac/mac_framework.h> 90163606Srwatson 9179106Sbrooks#define GIFNAME "gif" 9262587Sitojun 93127305Srwatson/* 94127898Sru * gif_mtx protects the global gif_softc_list. 95127305Srwatson */ 96127305Srwatsonstatic struct mtx gif_mtx; 9779106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 98215701Sdimstatic VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 99195727Srwatson#define V_gif_softc_list VNET(gif_softc_list) 100195699Srwatson 10183998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10283998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10383998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 10483998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 10583998Sbrooks 106153621Sthompsastatic void gif_start(struct ifnet *); 107160195Ssamstatic int gif_clone_create(struct if_clone *, int, caddr_t); 108128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 10979106Sbrooks 110130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 11179106Sbrooks 11292725Salfredstatic int gifmodevent(module_t, int, void *); 11379106Sbrooks 11491270SbrooksSYSCTL_DECL(_net_link); 115227309Sedstatic SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 11691270Sbrooks "Generic Tunnel Interface"); 11762587Sitojun#ifndef MAX_GIF_NEST 11862587Sitojun/* 11991270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 12062587Sitojun * Since, setting a large value to this macro with a careless configuration 12162587Sitojun * may introduce system crash, we don't allow any nestings by default. 12262587Sitojun * If you need to configure nested gif tunnels, you can define this macro 12395023Ssuz * in your kernel configuration file. However, if you do so, please be 12462587Sitojun * careful to configure the tunnels so that it won't make a loop. 12562587Sitojun */ 12662587Sitojun#define MAX_GIF_NEST 1 12762587Sitojun#endif 128215701Sdimstatic VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 129195837Srwatson#define V_max_gif_nesting VNET(max_gif_nesting) 130195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 131195699Srwatson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 13262587Sitojun 13391270Sbrooks/* 13491270Sbrooks * By default, we disallow creation of multiple tunnels between the same 13591270Sbrooks * pair of addresses. Some applications require this functionality so 13691270Sbrooks * we allow control over this check here. 13791270Sbrooks */ 138195837Srwatson#ifdef XBONEHACK 139215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 1; 140195837Srwatson#else 141215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 0; 142195837Srwatson#endif 143195837Srwatson#define V_parallel_tunnels VNET(parallel_tunnels) 144195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 145195699Srwatson &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 14691270Sbrooks 147176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 148176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 149176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 150176879Sthompsa#ifndef ETHER_IS_BROADCAST 151176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 152176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 153176879Sthompsa#endif 154176879Sthompsa 155128209Sbrooksstatic int 156160195Ssamgif_clone_create(ifc, unit, params) 15779106Sbrooks struct if_clone *ifc; 15892081Smux int unit; 159160195Ssam caddr_t params; 16054263Sshin{ 16178064Sume struct gif_softc *sc; 16254263Sshin 163131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 164178888Sjulian sc->gif_fibnum = curthread->td_proc->p_fibnum; 165147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 166147256Sbrooks if (GIF2IFP(sc) == NULL) { 167147256Sbrooks free(sc, M_GIF); 168147256Sbrooks return (ENOSPC); 169147256Sbrooks } 17079106Sbrooks 171155037Sglebius GIF_LOCK_INIT(sc); 172155037Sglebius 173147256Sbrooks GIF2IFP(sc)->if_softc = sc; 174147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 17579106Sbrooks 17679106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 177193664Shrs sc->gif_options = GIF_ACCEPT_REVETHIP; 17862587Sitojun 179147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 180147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 181147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 18278064Sume#if 0 18379106Sbrooks /* turn off ingress filter */ 184147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 18578064Sume#endif 186147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 187153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 188147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 189207554Ssobomax GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 190147256Sbrooks if_attach(GIF2IFP(sc)); 191147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 19283998Sbrooks if (ng_gif_attach_p != NULL) 193147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 194155037Sglebius 195155037Sglebius mtx_lock(&gif_mtx); 196181803Sbz LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 197155037Sglebius mtx_unlock(&gif_mtx); 198155037Sglebius 199155037Sglebius return (0); 20079106Sbrooks} 20179106Sbrooks 202127305Srwatsonstatic void 203151266Sthompsagif_clone_destroy(ifp) 204151266Sthompsa struct ifnet *ifp; 20579106Sbrooks{ 206184678Sbz#if defined(INET) || defined(INET6) 20779106Sbrooks int err; 208184678Sbz#endif 209151266Sthompsa struct gif_softc *sc = ifp->if_softc; 21079106Sbrooks 211151266Sthompsa mtx_lock(&gif_mtx); 212151266Sthompsa LIST_REMOVE(sc, gif_list); 213151266Sthompsa mtx_unlock(&gif_mtx); 214151266Sthompsa 215127305Srwatson gif_delete_tunnel(ifp); 216105293Sume#ifdef INET6 217105293Sume if (sc->encap_cookie6 != NULL) { 218105293Sume err = encap_detach(sc->encap_cookie6); 219105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 220105293Sume } 221105293Sume#endif 222105293Sume#ifdef INET 22379106Sbrooks if (sc->encap_cookie4 != NULL) { 22479106Sbrooks err = encap_detach(sc->encap_cookie4); 22579106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 22679106Sbrooks } 227105293Sume#endif 22879106Sbrooks 22983998Sbrooks if (ng_gif_detach_p != NULL) 23083998Sbrooks (*ng_gif_detach_p)(ifp); 23179106Sbrooks bpfdetach(ifp); 23279106Sbrooks if_detach(ifp); 233147256Sbrooks if_free(ifp); 23479106Sbrooks 235155037Sglebius GIF_LOCK_DESTROY(sc); 236155037Sglebius 23779106Sbrooks free(sc, M_GIF); 23879106Sbrooks} 23979106Sbrooks 240195837Srwatsonstatic void 241195837Srwatsonvnet_gif_init(const void *unused __unused) 242190787Szec{ 243190787Szec 244190787Szec LIST_INIT(&V_gif_softc_list); 245190787Szec} 246195837SrwatsonVNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 247195837Srwatson NULL); 248190787Szec 249190787Szecstatic int 25079106Sbrooksgifmodevent(mod, type, data) 25179106Sbrooks module_t mod; 25279106Sbrooks int type; 25379106Sbrooks void *data; 25479106Sbrooks{ 25579106Sbrooks 25679106Sbrooks switch (type) { 25779106Sbrooks case MOD_LOAD: 258127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 259185088Szec if_clone_attach(&gif_cloner); 260195837Srwatson break; 26179106Sbrooks 26279106Sbrooks case MOD_UNLOAD: 26379106Sbrooks if_clone_detach(&gif_cloner); 264127305Srwatson mtx_destroy(&gif_mtx); 26579106Sbrooks break; 266132199Sphk default: 267132199Sphk return EOPNOTSUPP; 26854263Sshin } 26979106Sbrooks return 0; 27054263Sshin} 27154263Sshin 27279106Sbrooksstatic moduledata_t gif_mod = { 27379106Sbrooks "if_gif", 27479106Sbrooks gifmodevent, 275241394Skevlo 0 27679106Sbrooks}; 27754263Sshin 27879106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27983997SbrooksMODULE_VERSION(if_gif, 1); 28079106Sbrooks 281105293Sumeint 28262587Sitojungif_encapcheck(m, off, proto, arg) 28362587Sitojun const struct mbuf *m; 28462587Sitojun int off; 28562587Sitojun int proto; 28662587Sitojun void *arg; 28762587Sitojun{ 28862587Sitojun struct ip ip; 28962587Sitojun struct gif_softc *sc; 29062587Sitojun 29162587Sitojun sc = (struct gif_softc *)arg; 29262587Sitojun if (sc == NULL) 29362587Sitojun return 0; 29462587Sitojun 295147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 29662587Sitojun return 0; 29762587Sitojun 29862587Sitojun /* no physical address */ 29962587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 30062587Sitojun return 0; 30162587Sitojun 30262587Sitojun switch (proto) { 30362587Sitojun#ifdef INET 30462587Sitojun case IPPROTO_IPV4: 30562587Sitojun break; 30662587Sitojun#endif 30762587Sitojun#ifdef INET6 30862587Sitojun case IPPROTO_IPV6: 30962587Sitojun break; 31062587Sitojun#endif 311153621Sthompsa case IPPROTO_ETHERIP: 312153621Sthompsa break; 313153621Sthompsa 31462587Sitojun default: 31562587Sitojun return 0; 31662587Sitojun } 31762587Sitojun 318105339Sume /* Bail on short packets */ 319105339Sume if (m->m_pkthdr.len < sizeof(ip)) 320105339Sume return 0; 321105339Sume 32291327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 32362587Sitojun 32462587Sitojun switch (ip.ip_v) { 32562587Sitojun#ifdef INET 32662587Sitojun case 4: 32762587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 32862587Sitojun sc->gif_pdst->sa_family != AF_INET) 32962587Sitojun return 0; 33062587Sitojun return gif_encapcheck4(m, off, proto, arg); 33162587Sitojun#endif 33262587Sitojun#ifdef INET6 33362587Sitojun case 6: 334105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 335105293Sume return 0; 33662587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 33762587Sitojun sc->gif_pdst->sa_family != AF_INET6) 33862587Sitojun return 0; 33962587Sitojun return gif_encapcheck6(m, off, proto, arg); 34062587Sitojun#endif 34162587Sitojun default: 34262587Sitojun return 0; 34362587Sitojun } 34462587Sitojun} 345236951Srrs#ifdef INET 346236951Srrs#define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip)) 347236951Srrs#endif 348236951Srrs#ifdef INET6 349236951Srrs#define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr)) 350236951Srrs#endif 35162587Sitojun 352153621Sthompsastatic void 353153621Sthompsagif_start(struct ifnet *ifp) 354153621Sthompsa{ 355153621Sthompsa struct gif_softc *sc; 356153621Sthompsa struct mbuf *m; 357236951Srrs uint32_t af; 358236951Srrs int error = 0; 359153621Sthompsa 360153621Sthompsa sc = ifp->if_softc; 361236951Srrs GIF_LOCK(sc); 362236951Srrs ifp->if_drv_flags |= IFF_DRV_OACTIVE; 363236951Srrs while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 364153621Sthompsa 365236951Srrs IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 366153621Sthompsa if (m == 0) 367153621Sthompsa break; 368153621Sthompsa 369236951Srrs#ifdef ALTQ 370236951Srrs /* Take out those altq bytes we add in gif_output */ 371236951Srrs#ifdef INET 372236951Srrs if (sc->gif_psrc->sa_family == AF_INET) 373236951Srrs m->m_pkthdr.len -= GIF_HDR_LEN; 374236951Srrs#endif 375236951Srrs#ifdef INET6 376236951Srrs if (sc->gif_psrc->sa_family == AF_INET6) 377236951Srrs m->m_pkthdr.len -= GIF_HDR_LEN6; 378236951Srrs#endif 379236951Srrs#endif 380236957Srrs /* 381236957Srrs * Now pull back the af that we 382236957Srrs * stashed in the csum_data. 383236951Srrs */ 384236955Srrs af = m->m_pkthdr.csum_data; 385236955Srrs 386236951Srrs if (ifp->if_bridge) 387236951Srrs af = AF_LINK; 388153621Sthompsa 389236951Srrs BPF_MTAP2(ifp, &af, sizeof(af), m); 390236951Srrs ifp->if_opackets++; 391236951Srrs 392236951Srrs/* Done by IFQ_HANDOFF */ 393236951Srrs/* ifp->if_obytes += m->m_pkthdr.len;*/ 394236951Srrs /* override to IPPROTO_ETHERIP for bridged traffic */ 395236951Srrs 396236951Srrs M_SETFIB(m, sc->gif_fibnum); 397236951Srrs /* inner AF-specific encapsulation */ 398236951Srrs /* XXX should we check if our outer source is legal? */ 399236951Srrs /* dispatch to output logic based on outer AF */ 400236951Srrs switch (sc->gif_psrc->sa_family) { 401236951Srrs#ifdef INET 402236951Srrs case AF_INET: 403236951Srrs error = in_gif_output(ifp, af, m); 404236951Srrs break; 405236951Srrs#endif 406236951Srrs#ifdef INET6 407236951Srrs case AF_INET6: 408236951Srrs error = in6_gif_output(ifp, af, m); 409236951Srrs break; 410236951Srrs#endif 411236951Srrs default: 412236951Srrs m_freem(m); 413236951Srrs error = ENETDOWN; 414236951Srrs } 415236951Srrs if (error) 416236951Srrs ifp->if_oerrors++; 417236951Srrs 418153621Sthompsa } 419153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 420236951Srrs GIF_UNLOCK(sc); 421153621Sthompsa return; 422153621Sthompsa} 423153621Sthompsa 42454263Sshinint 425191148Skmacygif_output(ifp, m, dst, ro) 42654263Sshin struct ifnet *ifp; 42754263Sshin struct mbuf *m; 42854263Sshin struct sockaddr *dst; 429191148Skmacy struct route *ro; 43054263Sshin{ 431147256Sbrooks struct gif_softc *sc = ifp->if_softc; 432127898Sru struct m_tag *mtag; 43354263Sshin int error = 0; 434127898Sru int gif_called; 435236951Srrs uint32_t af; 436101182Srwatson#ifdef MAC 437172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 438101739Srwatson if (error) { 439101739Srwatson m_freem(m); 440101739Srwatson goto end; 441101739Srwatson } 442101182Srwatson#endif 443101182Srwatson 44454263Sshin /* 44554263Sshin * gif may cause infinite recursion calls when misconfigured. 446127898Sru * We'll prevent this by detecting loops. 447127898Sru * 448127898Sru * High nesting level may cause stack exhaustion. 44954263Sshin * We'll prevent this by introducing upper limit. 45054263Sshin */ 451127898Sru gif_called = 1; 452127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 453127898Sru while (mtag != NULL) { 454127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 455127898Sru log(LOG_NOTICE, 456127898Sru "gif_output: loop detected on %s\n", 457127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 458127898Sru m_freem(m); 459127898Sru error = EIO; /* is there better errno? */ 460127898Sru goto end; 461127898Sru } 462127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 463127898Sru gif_called++; 464127898Sru } 465181803Sbz if (gif_called > V_max_gif_nesting) { 46654263Sshin log(LOG_NOTICE, 46754263Sshin "gif_output: recursively called too many times(%d)\n", 468127303Srwatson gif_called); 46954263Sshin m_freem(m); 47054263Sshin error = EIO; /* is there better errno? */ 47154263Sshin goto end; 47254263Sshin } 473127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 474127898Sru M_NOWAIT); 475127898Sru if (mtag == NULL) { 476127898Sru m_freem(m); 477127898Sru error = ENOMEM; 478127898Sru goto end; 479127898Sru } 480127898Sru *(struct ifnet **)(mtag + 1) = ifp; 481127898Sru m_tag_prepend(m, mtag); 48262587Sitojun 48354263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 484236951Srrs /* BPF writes need to be handled specially. */ 485236951Srrs if (dst->sa_family == AF_UNSPEC) { 486236951Srrs bcopy(dst->sa_data, &af, sizeof(af)); 487236951Srrs dst->sa_family = af; 488236951Srrs } 489236951Srrs af = dst->sa_family; 490236957Srrs /* 491236957Srrs * Now save the af in the inbound pkt csum 492236957Srrs * data, this is a cheat since we are using 493236957Srrs * the inbound csum_data field to carry the 494236957Srrs * af over to the gif_start() routine, avoiding 495236957Srrs * using yet another mtag. 496236951Srrs */ 497236955Srrs m->m_pkthdr.csum_data = af; 49854263Sshin if (!(ifp->if_flags & IFF_UP) || 49954263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 50054263Sshin m_freem(m); 50154263Sshin error = ENETDOWN; 50254263Sshin goto end; 50354263Sshin } 504236951Srrs#ifdef ALTQ 505236957Srrs /* 506236957Srrs * Make altq aware of the bytes we will add 507236951Srrs * when we actually send it. 508236951Srrs */ 50954263Sshin#ifdef INET 510236951Srrs if (sc->gif_psrc->sa_family == AF_INET) 511236951Srrs m->m_pkthdr.len += GIF_HDR_LEN; 51254263Sshin#endif 51354263Sshin#ifdef INET6 514236951Srrs if (sc->gif_psrc->sa_family == AF_INET6) 515236951Srrs m->m_pkthdr.len += GIF_HDR_LEN6; 51654263Sshin#endif 517236951Srrs#endif 518236951Srrs /* 519236951Srrs * Queue message on interface, update output statistics if 520236951Srrs * successful, and start output if interface not yet active. 521236951Srrs */ 522236951Srrs IFQ_HANDOFF(ifp, m, error); 52354263Sshin end: 52478064Sume if (error) 52578064Sume ifp->if_oerrors++; 526155037Sglebius return (error); 52754263Sshin} 52854263Sshin 52954263Sshinvoid 530105338Sumegif_input(m, af, ifp) 53154263Sshin struct mbuf *m; 53254263Sshin int af; 533105338Sume struct ifnet *ifp; 53454263Sshin{ 535153621Sthompsa int isr, n; 536198357Sbrueffer struct gif_softc *sc; 537153621Sthompsa struct etherip_header *eip; 538176879Sthompsa struct ether_header *eh; 539176879Sthompsa struct ifnet *oldifp; 54054263Sshin 541105338Sume if (ifp == NULL) { 54254263Sshin /* just in case */ 54354263Sshin m_freem(m); 54454263Sshin return; 54554263Sshin } 546198357Sbrueffer sc = ifp->if_softc; 547105338Sume m->m_pkthdr.rcvif = ifp; 548101182Srwatson 549101182Srwatson#ifdef MAC 550172930Srwatson mac_ifnet_create_mbuf(ifp, m); 551101182Srwatson#endif 552101182Srwatson 553159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 55478064Sume u_int32_t af1 = af; 555123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 55654263Sshin } 55754263Sshin 55883998Sbrooks if (ng_gif_input_p != NULL) { 559105338Sume (*ng_gif_input_p)(ifp, &m, af); 56083998Sbrooks if (m == NULL) 56183998Sbrooks return; 56283998Sbrooks } 56383998Sbrooks 56454263Sshin /* 56554263Sshin * Put the packet to the network layer input queue according to the 56654263Sshin * specified address family. 56754263Sshin * Note: older versions of gif_input directly called network layer 56895023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 56954263Sshin * prevent too many recursive calls of such input functions, which 57095023Ssuz * might cause kernel panic. But the change may introduce another 57154263Sshin * problem; if the input queue is full, packets are discarded. 57295023Ssuz * The kernel stack overflow really happened, and we believed 57395023Ssuz * queue-full rarely occurs, so we changed the policy. 57454263Sshin */ 57554263Sshin switch (af) { 57654263Sshin#ifdef INET 57754263Sshin case AF_INET: 57854263Sshin isr = NETISR_IP; 57954263Sshin break; 58054263Sshin#endif 58154263Sshin#ifdef INET6 58254263Sshin case AF_INET6: 58354263Sshin isr = NETISR_IPV6; 58454263Sshin break; 58554263Sshin#endif 586153621Sthompsa case AF_LINK: 587153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 588153621Sthompsa if (n > m->m_len) { 589153621Sthompsa m = m_pullup(m, n); 590153621Sthompsa if (m == NULL) { 591153621Sthompsa ifp->if_ierrors++; 592153621Sthompsa return; 593153621Sthompsa } 594153621Sthompsa } 595153621Sthompsa 596153621Sthompsa eip = mtod(m, struct etherip_header *); 597193664Shrs /* 598193664Shrs * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 599193664Shrs * accepts an EtherIP packet with revered version field in 600193664Shrs * the header. This is a knob for backward compatibility 601193664Shrs * with FreeBSD 7.2R or prior. 602193664Shrs */ 603193664Shrs if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 604193664Shrs if (eip->eip_resvl != ETHERIP_VERSION 605193664Shrs && eip->eip_ver != ETHERIP_VERSION) { 606193664Shrs /* discard unknown versions */ 607193664Shrs m_freem(m); 608193664Shrs return; 609193664Shrs } 610193664Shrs } else { 611193664Shrs if (eip->eip_ver != ETHERIP_VERSION) { 612193664Shrs /* discard unknown versions */ 613193664Shrs m_freem(m); 614193664Shrs return; 615193664Shrs } 616153621Sthompsa } 617153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 618153621Sthompsa 619153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 620153621Sthompsa m->m_pkthdr.rcvif = ifp; 621153621Sthompsa 622176879Sthompsa if (ifp->if_bridge) { 623176879Sthompsa oldifp = ifp; 624176879Sthompsa eh = mtod(m, struct ether_header *); 625176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 626176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 627176879Sthompsa m->m_flags |= M_BCAST; 628176879Sthompsa else 629176879Sthompsa m->m_flags |= M_MCAST; 630176879Sthompsa ifp->if_imcasts++; 631176879Sthompsa } 632153621Sthompsa BRIDGE_INPUT(ifp, m); 633176879Sthompsa 634176879Sthompsa if (m != NULL && ifp != oldifp) { 635176879Sthompsa /* 636176879Sthompsa * The bridge gave us back itself or one of the 637176879Sthompsa * members for which the frame is addressed. 638176879Sthompsa */ 639176879Sthompsa ether_demux(ifp, m); 640176879Sthompsa return; 641176879Sthompsa } 642176879Sthompsa } 643153621Sthompsa if (m != NULL) 644153621Sthompsa m_freem(m); 645153621Sthompsa return; 646153621Sthompsa 64754263Sshin default: 64883998Sbrooks if (ng_gif_input_orphan_p != NULL) 649105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 65083998Sbrooks else 65183998Sbrooks m_freem(m); 65254263Sshin return; 65354263Sshin } 65454263Sshin 655105338Sume ifp->if_ipackets++; 656105338Sume ifp->if_ibytes += m->m_pkthdr.len; 657223741Sbz M_SETFIB(m, ifp->if_fib); 658111888Sjlemon netisr_dispatch(isr, m); 65954263Sshin} 66054263Sshin 66162587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 66254263Sshinint 66354263Sshingif_ioctl(ifp, cmd, data) 66454263Sshin struct ifnet *ifp; 66554263Sshin u_long cmd; 66654263Sshin caddr_t data; 66754263Sshin{ 668147256Sbrooks struct gif_softc *sc = ifp->if_softc; 66954263Sshin struct ifreq *ifr = (struct ifreq*)data; 67054263Sshin int error = 0, size; 671193664Shrs u_int options; 67262587Sitojun struct sockaddr *dst, *src; 673105339Sume#ifdef SIOCSIFMTU /* xxx */ 674105339Sume u_long mtu; 675105339Sume#endif 676105339Sume 67754263Sshin switch (cmd) { 67854263Sshin case SIOCSIFADDR: 679105293Sume ifp->if_flags |= IFF_UP; 68054263Sshin break; 68162587Sitojun 68254263Sshin case SIOCSIFDSTADDR: 68354263Sshin break; 68454263Sshin 68554263Sshin case SIOCADDMULTI: 68654263Sshin case SIOCDELMULTI: 68754263Sshin break; 68854263Sshin 68962587Sitojun#ifdef SIOCSIFMTU /* xxx */ 69054263Sshin case SIOCGIFMTU: 69154263Sshin break; 69262587Sitojun 69354263Sshin case SIOCSIFMTU: 694105339Sume mtu = ifr->ifr_mtu; 695105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 696105339Sume return (EINVAL); 697105339Sume ifp->if_mtu = mtu; 69854263Sshin break; 69962587Sitojun#endif /* SIOCSIFMTU */ 70054263Sshin 701105339Sume#ifdef INET 70254263Sshin case SIOCSIFPHYADDR: 703105339Sume#endif 70454263Sshin#ifdef INET6 70554263Sshin case SIOCSIFPHYADDR_IN6: 70654263Sshin#endif /* INET6 */ 70778064Sume case SIOCSLIFPHYADDR: 70862587Sitojun switch (cmd) { 70978064Sume#ifdef INET 71062587Sitojun case SIOCSIFPHYADDR: 71154263Sshin src = (struct sockaddr *) 71254263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 71354263Sshin dst = (struct sockaddr *) 71454263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 71562587Sitojun break; 71678064Sume#endif 71762587Sitojun#ifdef INET6 71862587Sitojun case SIOCSIFPHYADDR_IN6: 71962587Sitojun src = (struct sockaddr *) 72062587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 72162587Sitojun dst = (struct sockaddr *) 72262587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 72362587Sitojun break; 72462587Sitojun#endif 72578064Sume case SIOCSLIFPHYADDR: 72678064Sume src = (struct sockaddr *) 72778064Sume &(((struct if_laddrreq *)data)->addr); 72878064Sume dst = (struct sockaddr *) 72978064Sume &(((struct if_laddrreq *)data)->dstaddr); 730105293Sume break; 73191327Sbrooks default: 732105293Sume return EINVAL; 73362587Sitojun } 73454263Sshin 73578064Sume /* sa_family must be equal */ 73678064Sume if (src->sa_family != dst->sa_family) 73778064Sume return EINVAL; 73878064Sume 73978064Sume /* validate sa_len */ 74078064Sume switch (src->sa_family) { 74178064Sume#ifdef INET 74278064Sume case AF_INET: 74378064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 74478064Sume return EINVAL; 74578064Sume break; 74678064Sume#endif 74778064Sume#ifdef INET6 74878064Sume case AF_INET6: 74978064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 75078064Sume return EINVAL; 75178064Sume break; 75278064Sume#endif 75378064Sume default: 75478064Sume return EAFNOSUPPORT; 75578064Sume } 75678064Sume switch (dst->sa_family) { 75778064Sume#ifdef INET 75878064Sume case AF_INET: 75978064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 76078064Sume return EINVAL; 76178064Sume break; 76278064Sume#endif 76378064Sume#ifdef INET6 76478064Sume case AF_INET6: 76578064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 76678064Sume return EINVAL; 76778064Sume break; 76878064Sume#endif 76978064Sume default: 77078064Sume return EAFNOSUPPORT; 77178064Sume } 77278064Sume 77378064Sume /* check sa_family looks sane for the cmd */ 77478064Sume switch (cmd) { 77578064Sume case SIOCSIFPHYADDR: 77678064Sume if (src->sa_family == AF_INET) 77778064Sume break; 77878064Sume return EAFNOSUPPORT; 77978064Sume#ifdef INET6 78078064Sume case SIOCSIFPHYADDR_IN6: 78178064Sume if (src->sa_family == AF_INET6) 78278064Sume break; 78378064Sume return EAFNOSUPPORT; 78478064Sume#endif /* INET6 */ 78578064Sume case SIOCSLIFPHYADDR: 78678064Sume /* checks done in the above */ 78778064Sume break; 78878064Sume } 78978064Sume 790147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 79162587Sitojun break; 79262587Sitojun 79362587Sitojun#ifdef SIOCDIFPHYADDR 79462587Sitojun case SIOCDIFPHYADDR: 795147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 79654263Sshin break; 79762587Sitojun#endif 79862587Sitojun 79954263Sshin case SIOCGIFPSRCADDR: 80054263Sshin#ifdef INET6 80154263Sshin case SIOCGIFPSRCADDR_IN6: 80254263Sshin#endif /* INET6 */ 80354263Sshin if (sc->gif_psrc == NULL) { 80454263Sshin error = EADDRNOTAVAIL; 80554263Sshin goto bad; 80654263Sshin } 80754263Sshin src = sc->gif_psrc; 80878064Sume switch (cmd) { 80954263Sshin#ifdef INET 81078064Sume case SIOCGIFPSRCADDR: 81154263Sshin dst = &ifr->ifr_addr; 81278064Sume size = sizeof(ifr->ifr_addr); 81354263Sshin break; 81454263Sshin#endif /* INET */ 81554263Sshin#ifdef INET6 81678064Sume case SIOCGIFPSRCADDR_IN6: 81754263Sshin dst = (struct sockaddr *) 81854263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 81978064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 82054263Sshin break; 82154263Sshin#endif /* INET6 */ 82254263Sshin default: 82354263Sshin error = EADDRNOTAVAIL; 82454263Sshin goto bad; 82554263Sshin } 82678064Sume if (src->sa_len > size) 82778064Sume return EINVAL; 82878064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 829148385Sume#ifdef INET6 830148385Sume if (dst->sa_family == AF_INET6) { 831148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 832148385Sume if (error != 0) 833148385Sume return (error); 834148385Sume } 835148385Sume#endif 83654263Sshin break; 83762587Sitojun 83854263Sshin case SIOCGIFPDSTADDR: 83954263Sshin#ifdef INET6 84054263Sshin case SIOCGIFPDSTADDR_IN6: 84154263Sshin#endif /* INET6 */ 84254263Sshin if (sc->gif_pdst == NULL) { 84354263Sshin error = EADDRNOTAVAIL; 84454263Sshin goto bad; 84554263Sshin } 84654263Sshin src = sc->gif_pdst; 84778064Sume switch (cmd) { 84854263Sshin#ifdef INET 84978064Sume case SIOCGIFPDSTADDR: 85054263Sshin dst = &ifr->ifr_addr; 85178064Sume size = sizeof(ifr->ifr_addr); 85254263Sshin break; 85354263Sshin#endif /* INET */ 85454263Sshin#ifdef INET6 85578064Sume case SIOCGIFPDSTADDR_IN6: 85654263Sshin dst = (struct sockaddr *) 85754263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 85878064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 85954263Sshin break; 86054263Sshin#endif /* INET6 */ 86154263Sshin default: 86254263Sshin error = EADDRNOTAVAIL; 86354263Sshin goto bad; 86454263Sshin } 86578064Sume if (src->sa_len > size) 86678064Sume return EINVAL; 867219206Sbz error = prison_if(curthread->td_ucred, src); 868219206Sbz if (error != 0) 869219206Sbz return (error); 870219206Sbz error = prison_if(curthread->td_ucred, dst); 871219206Sbz if (error != 0) 872219206Sbz return (error); 87378064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 874148385Sume#ifdef INET6 875148385Sume if (dst->sa_family == AF_INET6) { 876148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 877148385Sume if (error != 0) 878148385Sume return (error); 879148385Sume } 880148385Sume#endif 88154263Sshin break; 88254263Sshin 88378064Sume case SIOCGLIFPHYADDR: 88478064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 88578064Sume error = EADDRNOTAVAIL; 88678064Sume goto bad; 88778064Sume } 88878064Sume 88978064Sume /* copy src */ 89078064Sume src = sc->gif_psrc; 89178064Sume dst = (struct sockaddr *) 89278064Sume &(((struct if_laddrreq *)data)->addr); 89378064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 89478064Sume if (src->sa_len > size) 89578064Sume return EINVAL; 89678064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 89778064Sume 89878064Sume /* copy dst */ 89978064Sume src = sc->gif_pdst; 90078064Sume dst = (struct sockaddr *) 90178064Sume &(((struct if_laddrreq *)data)->dstaddr); 90278064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 90378064Sume if (src->sa_len > size) 90478064Sume return EINVAL; 90578064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 90678064Sume break; 90778064Sume 90854263Sshin case SIOCSIFFLAGS: 90962587Sitojun /* if_ioctl() takes care of it */ 91054263Sshin break; 91154263Sshin 912193664Shrs case GIFGOPTS: 913193664Shrs options = sc->gif_options; 914193664Shrs error = copyout(&options, ifr->ifr_data, 915193664Shrs sizeof(options)); 916193664Shrs break; 917193664Shrs 918193664Shrs case GIFSOPTS: 919193664Shrs if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 920193664Shrs break; 921193815Shrs error = copyin(ifr->ifr_data, &options, sizeof(options)); 922193815Shrs if (error) 923193815Shrs break; 924193815Shrs if (options & ~GIF_OPTMASK) 925193815Shrs error = EINVAL; 926193815Shrs else 927193815Shrs sc->gif_options = options; 928193664Shrs break; 929193664Shrs 93054263Sshin default: 93154263Sshin error = EINVAL; 93254263Sshin break; 93354263Sshin } 93454263Sshin bad: 93554263Sshin return error; 93654263Sshin} 93779106Sbrooks 938127305Srwatson/* 939127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 940127305Srwatson * if a given tunnel is already present happens before we perform a 941127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 942127305Srwatson * re-ordered so that the check and replacement can be atomic using 943127305Srwatson * a mutex. 944127305Srwatson */ 945105293Sumeint 946105293Sumegif_set_tunnel(ifp, src, dst) 947105293Sume struct ifnet *ifp; 948105293Sume struct sockaddr *src; 949105293Sume struct sockaddr *dst; 950105293Sume{ 951147256Sbrooks struct gif_softc *sc = ifp->if_softc; 952105293Sume struct gif_softc *sc2; 953105293Sume struct sockaddr *osrc, *odst, *sa; 954105293Sume int error = 0; 955105293Sume 956127305Srwatson mtx_lock(&gif_mtx); 957181803Sbz LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 958105293Sume if (sc2 == sc) 959105293Sume continue; 960105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 961105293Sume continue; 962105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 963105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 964105293Sume sc2->gif_psrc->sa_family != src->sa_family || 965105293Sume sc2->gif_psrc->sa_len != src->sa_len) 966105293Sume continue; 967105293Sume 968105293Sume /* 969105293Sume * Disallow parallel tunnels unless instructed 970105293Sume * otherwise. 971105293Sume */ 972181803Sbz if (!V_parallel_tunnels && 973105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 974105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 975105293Sume error = EADDRNOTAVAIL; 976127305Srwatson mtx_unlock(&gif_mtx); 977105293Sume goto bad; 978105293Sume } 979105293Sume 980105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 981105293Sume } 982127305Srwatson mtx_unlock(&gif_mtx); 983105293Sume 984105293Sume /* XXX we can detach from both, but be polite just in case */ 985105293Sume if (sc->gif_psrc) 986105293Sume switch (sc->gif_psrc->sa_family) { 987105293Sume#ifdef INET 988105293Sume case AF_INET: 989105293Sume (void)in_gif_detach(sc); 990105293Sume break; 991105293Sume#endif 992105293Sume#ifdef INET6 993105293Sume case AF_INET6: 994105293Sume (void)in6_gif_detach(sc); 995105293Sume break; 996105293Sume#endif 997105293Sume } 998105293Sume 999105293Sume osrc = sc->gif_psrc; 1000111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 1001105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 1002105293Sume sc->gif_psrc = sa; 1003105293Sume 1004105293Sume odst = sc->gif_pdst; 1005111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 1006105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 1007105293Sume sc->gif_pdst = sa; 1008105293Sume 1009105293Sume switch (sc->gif_psrc->sa_family) { 1010105293Sume#ifdef INET 1011105293Sume case AF_INET: 1012105293Sume error = in_gif_attach(sc); 1013105293Sume break; 1014105293Sume#endif 1015105293Sume#ifdef INET6 1016105293Sume case AF_INET6: 1017148385Sume /* 1018148385Sume * Check validity of the scope zone ID of the addresses, and 1019148385Sume * convert it into the kernel internal form if necessary. 1020148385Sume */ 1021148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 1022148385Sume if (error != 0) 1023148385Sume break; 1024148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 1025148385Sume if (error != 0) 1026148385Sume break; 1027105293Sume error = in6_gif_attach(sc); 1028105293Sume break; 1029105293Sume#endif 1030105293Sume } 1031105293Sume if (error) { 1032105293Sume /* rollback */ 1033105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 1034105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 1035105293Sume sc->gif_psrc = osrc; 1036105293Sume sc->gif_pdst = odst; 1037105293Sume goto bad; 1038105293Sume } 1039105293Sume 1040105293Sume if (osrc) 1041105293Sume free((caddr_t)osrc, M_IFADDR); 1042105293Sume if (odst) 1043105293Sume free((caddr_t)odst, M_IFADDR); 1044105293Sume 1045105293Sume bad: 1046105293Sume if (sc->gif_psrc && sc->gif_pdst) 1047148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 1048105293Sume else 1049148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1050105293Sume 1051105293Sume return error; 1052105293Sume} 1053105293Sume 105479106Sbrooksvoid 1055105293Sumegif_delete_tunnel(ifp) 1056105293Sume struct ifnet *ifp; 105779106Sbrooks{ 1058147256Sbrooks struct gif_softc *sc = ifp->if_softc; 105979106Sbrooks 106079106Sbrooks if (sc->gif_psrc) { 106179106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 106279106Sbrooks sc->gif_psrc = NULL; 106379106Sbrooks } 106479106Sbrooks if (sc->gif_pdst) { 106579106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 106679106Sbrooks sc->gif_pdst = NULL; 106779106Sbrooks } 1068105293Sume /* it is safe to detach from both */ 1069105293Sume#ifdef INET 1070105293Sume (void)in_gif_detach(sc); 1071105293Sume#endif 1072105293Sume#ifdef INET6 1073105293Sume (void)in6_gif_detach(sc); 1074105293Sume#endif 1075160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 107679106Sbrooks} 1077