if_gif.c revision 191148
162587Sitojun/* $FreeBSD: head/sys/net/if_gif.c 191148 2009-04-16 20:30:28Z kmacy $ */ 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> 49178888Sjulian#include <sys/proc.h> 5062587Sitojun#include <sys/protosw.h> 5179106Sbrooks#include <sys/conf.h> 52181803Sbz#include <sys/vimage.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> 6154263Sshin 6254263Sshin#include <netinet/in.h> 6354263Sshin#include <netinet/in_systm.h> 6478064Sume#include <netinet/ip.h> 6578064Sume#ifdef INET 6654263Sshin#include <netinet/in_var.h> 6754263Sshin#include <netinet/in_gif.h> 6879106Sbrooks#include <netinet/ip_var.h> 6954263Sshin#endif /* INET */ 7054263Sshin 7154263Sshin#ifdef INET6 7254263Sshin#ifndef INET 7354263Sshin#include <netinet/in.h> 7454263Sshin#endif 7554263Sshin#include <netinet6/in6_var.h> 7654263Sshin#include <netinet/ip6.h> 7754263Sshin#include <netinet6/ip6_var.h> 78148385Sume#include <netinet6/scope6_var.h> 7954263Sshin#include <netinet6/in6_gif.h> 8062587Sitojun#include <netinet6/ip6protosw.h> 8154263Sshin#endif /* INET6 */ 8254263Sshin 8362587Sitojun#include <netinet/ip_encap.h> 84153621Sthompsa#include <net/ethernet.h> 85153621Sthompsa#include <net/if_bridgevar.h> 8654263Sshin#include <net/if_gif.h> 8754263Sshin 88163606Srwatson#include <security/mac/mac_framework.h> 89163606Srwatson 9079106Sbrooks#define GIFNAME "gif" 9162587Sitojun 92127305Srwatson/* 93127898Sru * gif_mtx protects the global gif_softc_list. 94127305Srwatson */ 95127305Srwatsonstatic struct mtx gif_mtx; 9679106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 97185088Szec 98185895Szec#ifndef VIMAGE 99185895Szec#ifndef VIMAGE_GLOBALS 100185895Szecstruct vnet_gif vnet_gif_0; 101185895Szec#endif 102185895Szec#endif 103185895Szec 104185088Szec#ifdef VIMAGE_GLOBALS 10589065Smsmithstatic LIST_HEAD(, gif_softc) gif_softc_list; 106185088Szecstatic int max_gif_nesting; 107185088Szecstatic int parallel_tunnels; 108185088Szec#ifdef INET 109185088Szecint ip_gif_ttl; 110185088Szec#endif 111185088Szec#ifdef INET6 112185088Szecint ip6_gif_hlim; 113185088Szec#endif 114185088Szec#endif 11579106Sbrooks 11683998Sbrooksvoid (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 11783998Sbrooksvoid (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 11883998Sbrooksvoid (*ng_gif_attach_p)(struct ifnet *ifp); 11983998Sbrooksvoid (*ng_gif_detach_p)(struct ifnet *ifp); 12083998Sbrooks 121153621Sthompsastatic void gif_start(struct ifnet *); 122160195Ssamstatic int gif_clone_create(struct if_clone *, int, caddr_t); 123128209Sbrooksstatic void gif_clone_destroy(struct ifnet *); 124190787Szecstatic int vnet_gif_iattach(const void *); 12579106Sbrooks 126190909Szec#ifndef VIMAGE_GLOBALS 127190909Szecstatic const vnet_modinfo_t vnet_gif_modinfo = { 128190909Szec .vmi_id = VNET_MOD_GIF, 129190909Szec .vmi_name = "gif", 130190909Szec .vmi_dependson = VNET_MOD_NET, 131190909Szec .vmi_iattach = vnet_gif_iattach 132190909Szec}; 133190909Szec#endif 134190909Szec 135130933SbrooksIFC_SIMPLE_DECLARE(gif, 0); 13679106Sbrooks 13792725Salfredstatic int gifmodevent(module_t, int, void *); 13879106Sbrooks 13991270SbrooksSYSCTL_DECL(_net_link); 14091270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 14191270Sbrooks "Generic Tunnel Interface"); 14262587Sitojun#ifndef MAX_GIF_NEST 14362587Sitojun/* 14491270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels. 14562587Sitojun * Since, setting a large value to this macro with a careless configuration 14662587Sitojun * may introduce system crash, we don't allow any nestings by default. 14762587Sitojun * If you need to configure nested gif tunnels, you can define this macro 14895023Ssuz * in your kernel configuration file. However, if you do so, please be 14962587Sitojun * careful to configure the tunnels so that it won't make a loop. 15062587Sitojun */ 15162587Sitojun#define MAX_GIF_NEST 1 15262587Sitojun#endif 153183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting, 154183550Szec CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels"); 15562587Sitojun 156183550Szec#ifdef INET6 157183550SzecSYSCTL_DECL(_net_inet6_ip6); 158183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM, 159183550Szec gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, ""); 160183550Szec#endif 161183550Szec 16291270Sbrooks/* 16391270Sbrooks * By default, we disallow creation of multiple tunnels between the same 16491270Sbrooks * pair of addresses. Some applications require this functionality so 16591270Sbrooks * we allow control over this check here. 16691270Sbrooks */ 167183550SzecSYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels, 168183550Szec CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?"); 16991270Sbrooks 170176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */ 171176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 172176879Sthompsa { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 173176879Sthompsa#ifndef ETHER_IS_BROADCAST 174176879Sthompsa#define ETHER_IS_BROADCAST(addr) \ 175176879Sthompsa (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 176176879Sthompsa#endif 177176879Sthompsa 178128209Sbrooksstatic int 179160195Ssamgif_clone_create(ifc, unit, params) 18079106Sbrooks struct if_clone *ifc; 18192081Smux int unit; 182160195Ssam caddr_t params; 18354263Sshin{ 184183550Szec INIT_VNET_GIF(curvnet); 18578064Sume struct gif_softc *sc; 18654263Sshin 187131672Sbms sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 188178888Sjulian sc->gif_fibnum = curthread->td_proc->p_fibnum; 189147256Sbrooks GIF2IFP(sc) = if_alloc(IFT_GIF); 190147256Sbrooks if (GIF2IFP(sc) == NULL) { 191147256Sbrooks free(sc, M_GIF); 192147256Sbrooks return (ENOSPC); 193147256Sbrooks } 19479106Sbrooks 195155037Sglebius GIF_LOCK_INIT(sc); 196155037Sglebius 197147256Sbrooks GIF2IFP(sc)->if_softc = sc; 198147256Sbrooks if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 19979106Sbrooks 20079106Sbrooks sc->encap_cookie4 = sc->encap_cookie6 = NULL; 20162587Sitojun 202147256Sbrooks GIF2IFP(sc)->if_addrlen = 0; 203147256Sbrooks GIF2IFP(sc)->if_mtu = GIF_MTU; 204147256Sbrooks GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 20578064Sume#if 0 20679106Sbrooks /* turn off ingress filter */ 207147256Sbrooks GIF2IFP(sc)->if_flags |= IFF_LINK2; 20878064Sume#endif 209147256Sbrooks GIF2IFP(sc)->if_ioctl = gif_ioctl; 210153621Sthompsa GIF2IFP(sc)->if_start = gif_start; 211147256Sbrooks GIF2IFP(sc)->if_output = gif_output; 212147256Sbrooks GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 213147256Sbrooks if_attach(GIF2IFP(sc)); 214147611Sdwmalone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 21583998Sbrooks if (ng_gif_attach_p != NULL) 216147256Sbrooks (*ng_gif_attach_p)(GIF2IFP(sc)); 217155037Sglebius 218155037Sglebius mtx_lock(&gif_mtx); 219181803Sbz LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 220155037Sglebius mtx_unlock(&gif_mtx); 221155037Sglebius 222155037Sglebius return (0); 22379106Sbrooks} 22479106Sbrooks 225127305Srwatsonstatic void 226151266Sthompsagif_clone_destroy(ifp) 227151266Sthompsa struct ifnet *ifp; 22879106Sbrooks{ 229184678Sbz#if defined(INET) || defined(INET6) 23079106Sbrooks int err; 231184678Sbz#endif 232151266Sthompsa struct gif_softc *sc = ifp->if_softc; 23379106Sbrooks 234151266Sthompsa mtx_lock(&gif_mtx); 235151266Sthompsa LIST_REMOVE(sc, gif_list); 236151266Sthompsa mtx_unlock(&gif_mtx); 237151266Sthompsa 238127305Srwatson gif_delete_tunnel(ifp); 239105293Sume#ifdef INET6 240105293Sume if (sc->encap_cookie6 != NULL) { 241105293Sume err = encap_detach(sc->encap_cookie6); 242105293Sume KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 243105293Sume } 244105293Sume#endif 245105293Sume#ifdef INET 24679106Sbrooks if (sc->encap_cookie4 != NULL) { 24779106Sbrooks err = encap_detach(sc->encap_cookie4); 24879106Sbrooks KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 24979106Sbrooks } 250105293Sume#endif 25179106Sbrooks 25283998Sbrooks if (ng_gif_detach_p != NULL) 25383998Sbrooks (*ng_gif_detach_p)(ifp); 25479106Sbrooks bpfdetach(ifp); 25579106Sbrooks if_detach(ifp); 256147256Sbrooks if_free(ifp); 25779106Sbrooks 258155037Sglebius GIF_LOCK_DESTROY(sc); 259155037Sglebius 26079106Sbrooks free(sc, M_GIF); 26179106Sbrooks} 26279106Sbrooks 26379106Sbrooksstatic int 264190787Szecvnet_gif_iattach(const void *unused __unused) 265190787Szec{ 266190787Szec INIT_VNET_GIF(curvnet); 267190787Szec 268190787Szec LIST_INIT(&V_gif_softc_list); 269190787Szec V_max_gif_nesting = MAX_GIF_NEST; 270190787Szec#ifdef XBONEHACK 271190787Szec V_parallel_tunnels = 1; 272190787Szec#else 273190787Szec V_parallel_tunnels = 0; 274190787Szec#endif 275190787Szec V_ip_gif_ttl = GIF_TTL; 276190787Szec#ifdef INET6 277190787Szec V_ip6_gif_hlim = GIF_HLIM; 278190787Szec#endif 279190787Szec 280190787Szec return (0); 281190787Szec} 282190787Szec 283190787Szecstatic int 28479106Sbrooksgifmodevent(mod, type, data) 28579106Sbrooks module_t mod; 28679106Sbrooks int type; 28779106Sbrooks void *data; 28879106Sbrooks{ 28979106Sbrooks 29079106Sbrooks switch (type) { 29179106Sbrooks case MOD_LOAD: 292127305Srwatson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 293185088Szec 294190909Szec#ifndef VIMAGE_GLOBALS 295190909Szec vnet_mod_register(&vnet_gif_modinfo); 296190909Szec#else 297190787Szec vnet_gif_iattach(NULL); 298190909Szec#endif 299185088Szec if_clone_attach(&gif_cloner); 30079106Sbrooks 30179106Sbrooks break; 30279106Sbrooks case MOD_UNLOAD: 30379106Sbrooks if_clone_detach(&gif_cloner); 304127305Srwatson mtx_destroy(&gif_mtx); 30579106Sbrooks#ifdef INET6 306190787Szec V_ip6_gif_hlim = 0; /* XXX -> vnet_gif_idetach() */ 30762587Sitojun#endif 30879106Sbrooks break; 309132199Sphk default: 310132199Sphk return EOPNOTSUPP; 31154263Sshin } 31279106Sbrooks return 0; 31354263Sshin} 31454263Sshin 31579106Sbrooksstatic moduledata_t gif_mod = { 31679106Sbrooks "if_gif", 31779106Sbrooks gifmodevent, 31879106Sbrooks 0 31979106Sbrooks}; 32054263Sshin 32179106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 32283997SbrooksMODULE_VERSION(if_gif, 1); 32379106Sbrooks 324105293Sumeint 32562587Sitojungif_encapcheck(m, off, proto, arg) 32662587Sitojun const struct mbuf *m; 32762587Sitojun int off; 32862587Sitojun int proto; 32962587Sitojun void *arg; 33062587Sitojun{ 33162587Sitojun struct ip ip; 33262587Sitojun struct gif_softc *sc; 33362587Sitojun 33462587Sitojun sc = (struct gif_softc *)arg; 33562587Sitojun if (sc == NULL) 33662587Sitojun return 0; 33762587Sitojun 338147256Sbrooks if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 33962587Sitojun return 0; 34062587Sitojun 34162587Sitojun /* no physical address */ 34262587Sitojun if (!sc->gif_psrc || !sc->gif_pdst) 34362587Sitojun return 0; 34462587Sitojun 34562587Sitojun switch (proto) { 34662587Sitojun#ifdef INET 34762587Sitojun case IPPROTO_IPV4: 34862587Sitojun break; 34962587Sitojun#endif 35062587Sitojun#ifdef INET6 35162587Sitojun case IPPROTO_IPV6: 35262587Sitojun break; 35362587Sitojun#endif 354153621Sthompsa case IPPROTO_ETHERIP: 355153621Sthompsa break; 356153621Sthompsa 35762587Sitojun default: 35862587Sitojun return 0; 35962587Sitojun } 36062587Sitojun 361105339Sume /* Bail on short packets */ 362105339Sume if (m->m_pkthdr.len < sizeof(ip)) 363105339Sume return 0; 364105339Sume 36591327Sbrooks m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 36662587Sitojun 36762587Sitojun switch (ip.ip_v) { 36862587Sitojun#ifdef INET 36962587Sitojun case 4: 37062587Sitojun if (sc->gif_psrc->sa_family != AF_INET || 37162587Sitojun sc->gif_pdst->sa_family != AF_INET) 37262587Sitojun return 0; 37362587Sitojun return gif_encapcheck4(m, off, proto, arg); 37462587Sitojun#endif 37562587Sitojun#ifdef INET6 37662587Sitojun case 6: 377105293Sume if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 378105293Sume return 0; 37962587Sitojun if (sc->gif_psrc->sa_family != AF_INET6 || 38062587Sitojun sc->gif_pdst->sa_family != AF_INET6) 38162587Sitojun return 0; 38262587Sitojun return gif_encapcheck6(m, off, proto, arg); 38362587Sitojun#endif 38462587Sitojun default: 38562587Sitojun return 0; 38662587Sitojun } 38762587Sitojun} 38862587Sitojun 389153621Sthompsastatic void 390153621Sthompsagif_start(struct ifnet *ifp) 391153621Sthompsa{ 392153621Sthompsa struct gif_softc *sc; 393153621Sthompsa struct mbuf *m; 394153621Sthompsa 395153621Sthompsa sc = ifp->if_softc; 396153621Sthompsa 397153621Sthompsa ifp->if_drv_flags |= IFF_DRV_OACTIVE; 398153621Sthompsa for (;;) { 399153621Sthompsa IFQ_DEQUEUE(&ifp->if_snd, m); 400153621Sthompsa if (m == 0) 401153621Sthompsa break; 402153621Sthompsa 403153621Sthompsa gif_output(ifp, m, sc->gif_pdst, NULL); 404153621Sthompsa 405153621Sthompsa } 406153621Sthompsa ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 407153621Sthompsa 408153621Sthompsa return; 409153621Sthompsa} 410153621Sthompsa 41154263Sshinint 412191148Skmacygif_output(ifp, m, dst, ro) 41354263Sshin struct ifnet *ifp; 41454263Sshin struct mbuf *m; 41554263Sshin struct sockaddr *dst; 416191148Skmacy struct route *ro; 41754263Sshin{ 418183550Szec INIT_VNET_GIF(ifp->if_vnet); 419147256Sbrooks struct gif_softc *sc = ifp->if_softc; 420127898Sru struct m_tag *mtag; 42154263Sshin int error = 0; 422127898Sru int gif_called; 423147611Sdwmalone u_int32_t af; 42454263Sshin 425101182Srwatson#ifdef MAC 426172930Srwatson error = mac_ifnet_check_transmit(ifp, m); 427101739Srwatson if (error) { 428101739Srwatson m_freem(m); 429101739Srwatson goto end; 430101739Srwatson } 431101182Srwatson#endif 432101182Srwatson 43354263Sshin /* 43454263Sshin * gif may cause infinite recursion calls when misconfigured. 435127898Sru * We'll prevent this by detecting loops. 436127898Sru * 437127898Sru * High nesting level may cause stack exhaustion. 43854263Sshin * We'll prevent this by introducing upper limit. 43954263Sshin */ 440127898Sru gif_called = 1; 441127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 442127898Sru while (mtag != NULL) { 443127898Sru if (*(struct ifnet **)(mtag + 1) == ifp) { 444127898Sru log(LOG_NOTICE, 445127898Sru "gif_output: loop detected on %s\n", 446127898Sru (*(struct ifnet **)(mtag + 1))->if_xname); 447127898Sru m_freem(m); 448127898Sru error = EIO; /* is there better errno? */ 449127898Sru goto end; 450127898Sru } 451127898Sru mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 452127898Sru gif_called++; 453127898Sru } 454181803Sbz if (gif_called > V_max_gif_nesting) { 45554263Sshin log(LOG_NOTICE, 45654263Sshin "gif_output: recursively called too many times(%d)\n", 457127303Srwatson gif_called); 45854263Sshin m_freem(m); 45954263Sshin error = EIO; /* is there better errno? */ 46054263Sshin goto end; 46154263Sshin } 462127898Sru mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 463127898Sru M_NOWAIT); 464127898Sru if (mtag == NULL) { 465127898Sru m_freem(m); 466127898Sru error = ENOMEM; 467127898Sru goto end; 468127898Sru } 469127898Sru *(struct ifnet **)(mtag + 1) = ifp; 470127898Sru m_tag_prepend(m, mtag); 47162587Sitojun 47254263Sshin m->m_flags &= ~(M_BCAST|M_MCAST); 473155037Sglebius 474155037Sglebius GIF_LOCK(sc); 475155037Sglebius 47654263Sshin if (!(ifp->if_flags & IFF_UP) || 47754263Sshin sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 478159174Sglebius GIF_UNLOCK(sc); 47954263Sshin m_freem(m); 48054263Sshin error = ENETDOWN; 48154263Sshin goto end; 48254263Sshin } 48354263Sshin 484147611Sdwmalone /* BPF writes need to be handled specially. */ 485147611Sdwmalone if (dst->sa_family == AF_UNSPEC) { 486147611Sdwmalone bcopy(dst->sa_data, &af, sizeof(af)); 487147611Sdwmalone dst->sa_family = af; 488147611Sdwmalone } 489147611Sdwmalone 490153621Sthompsa af = dst->sa_family; 491159180Scsjp BPF_MTAP2(ifp, &af, sizeof(af), m); 49262587Sitojun ifp->if_opackets++; 49354263Sshin ifp->if_obytes += m->m_pkthdr.len; 49454263Sshin 495153621Sthompsa /* override to IPPROTO_ETHERIP for bridged traffic */ 496153621Sthompsa if (ifp->if_bridge) 497153621Sthompsa af = AF_LINK; 498153621Sthompsa 499178888Sjulian M_SETFIB(m, sc->gif_fibnum); 50078064Sume /* inner AF-specific encapsulation */ 50178064Sume 50262587Sitojun /* XXX should we check if our outer source is legal? */ 50362587Sitojun 50478064Sume /* dispatch to output logic based on outer AF */ 50554263Sshin switch (sc->gif_psrc->sa_family) { 50654263Sshin#ifdef INET 50754263Sshin case AF_INET: 508153621Sthompsa error = in_gif_output(ifp, af, m); 50954263Sshin break; 51054263Sshin#endif 51154263Sshin#ifdef INET6 51254263Sshin case AF_INET6: 513153621Sthompsa error = in6_gif_output(ifp, af, m); 51454263Sshin break; 51554263Sshin#endif 51654263Sshin default: 51762587Sitojun m_freem(m); 51854263Sshin error = ENETDOWN; 51954263Sshin } 52054263Sshin 521159174Sglebius GIF_UNLOCK(sc); 52254263Sshin end: 52378064Sume if (error) 52478064Sume ifp->if_oerrors++; 525155037Sglebius return (error); 52654263Sshin} 52754263Sshin 52854263Sshinvoid 529105338Sumegif_input(m, af, ifp) 53054263Sshin struct mbuf *m; 53154263Sshin int af; 532105338Sume struct ifnet *ifp; 53354263Sshin{ 534153621Sthompsa int isr, n; 535153621Sthompsa struct etherip_header *eip; 536176879Sthompsa struct ether_header *eh; 537176879Sthompsa struct ifnet *oldifp; 53854263Sshin 539105338Sume if (ifp == NULL) { 54054263Sshin /* just in case */ 54154263Sshin m_freem(m); 54254263Sshin return; 54354263Sshin } 54454263Sshin 545105338Sume m->m_pkthdr.rcvif = ifp; 546101182Srwatson 547101182Srwatson#ifdef MAC 548172930Srwatson mac_ifnet_create_mbuf(ifp, m); 549101182Srwatson#endif 550101182Srwatson 551159180Scsjp if (bpf_peers_present(ifp->if_bpf)) { 55278064Sume u_int32_t af1 = af; 553123922Ssam bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 55454263Sshin } 55554263Sshin 55683998Sbrooks if (ng_gif_input_p != NULL) { 557105338Sume (*ng_gif_input_p)(ifp, &m, af); 55883998Sbrooks if (m == NULL) 55983998Sbrooks return; 56083998Sbrooks } 56183998Sbrooks 56254263Sshin /* 56354263Sshin * Put the packet to the network layer input queue according to the 56454263Sshin * specified address family. 56554263Sshin * Note: older versions of gif_input directly called network layer 56695023Ssuz * input functions, e.g. ip6_input, here. We changed the policy to 56754263Sshin * prevent too many recursive calls of such input functions, which 56895023Ssuz * might cause kernel panic. But the change may introduce another 56954263Sshin * problem; if the input queue is full, packets are discarded. 57095023Ssuz * The kernel stack overflow really happened, and we believed 57195023Ssuz * queue-full rarely occurs, so we changed the policy. 57254263Sshin */ 57354263Sshin switch (af) { 57454263Sshin#ifdef INET 57554263Sshin case AF_INET: 57654263Sshin isr = NETISR_IP; 57754263Sshin break; 57854263Sshin#endif 57954263Sshin#ifdef INET6 58054263Sshin case AF_INET6: 58154263Sshin isr = NETISR_IPV6; 58254263Sshin break; 58354263Sshin#endif 584153621Sthompsa case AF_LINK: 585153621Sthompsa n = sizeof(struct etherip_header) + sizeof(struct ether_header); 586153621Sthompsa if (n > m->m_len) { 587153621Sthompsa m = m_pullup(m, n); 588153621Sthompsa if (m == NULL) { 589153621Sthompsa ifp->if_ierrors++; 590153621Sthompsa return; 591153621Sthompsa } 592153621Sthompsa } 593153621Sthompsa 594153621Sthompsa eip = mtod(m, struct etherip_header *); 595153621Sthompsa if (eip->eip_ver != 596153621Sthompsa (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 597153621Sthompsa /* discard unknown versions */ 598153621Sthompsa m_freem(m); 599153621Sthompsa return; 600153621Sthompsa } 601153621Sthompsa m_adj(m, sizeof(struct etherip_header)); 602153621Sthompsa 603153621Sthompsa m->m_flags &= ~(M_BCAST|M_MCAST); 604153621Sthompsa m->m_pkthdr.rcvif = ifp; 605153621Sthompsa 606176879Sthompsa if (ifp->if_bridge) { 607176879Sthompsa oldifp = ifp; 608176879Sthompsa eh = mtod(m, struct ether_header *); 609176879Sthompsa if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 610176879Sthompsa if (ETHER_IS_BROADCAST(eh->ether_dhost)) 611176879Sthompsa m->m_flags |= M_BCAST; 612176879Sthompsa else 613176879Sthompsa m->m_flags |= M_MCAST; 614176879Sthompsa ifp->if_imcasts++; 615176879Sthompsa } 616153621Sthompsa BRIDGE_INPUT(ifp, m); 617176879Sthompsa 618176879Sthompsa if (m != NULL && ifp != oldifp) { 619176879Sthompsa /* 620176879Sthompsa * The bridge gave us back itself or one of the 621176879Sthompsa * members for which the frame is addressed. 622176879Sthompsa */ 623176879Sthompsa ether_demux(ifp, m); 624176879Sthompsa return; 625176879Sthompsa } 626176879Sthompsa } 627153621Sthompsa if (m != NULL) 628153621Sthompsa m_freem(m); 629153621Sthompsa return; 630153621Sthompsa 63154263Sshin default: 63283998Sbrooks if (ng_gif_input_orphan_p != NULL) 633105338Sume (*ng_gif_input_orphan_p)(ifp, m, af); 63483998Sbrooks else 63583998Sbrooks m_freem(m); 63654263Sshin return; 63754263Sshin } 63854263Sshin 639105338Sume ifp->if_ipackets++; 640105338Sume ifp->if_ibytes += m->m_pkthdr.len; 641111888Sjlemon netisr_dispatch(isr, m); 64254263Sshin} 64354263Sshin 64462587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 64554263Sshinint 64654263Sshingif_ioctl(ifp, cmd, data) 64754263Sshin struct ifnet *ifp; 64854263Sshin u_long cmd; 64954263Sshin caddr_t data; 65054263Sshin{ 651147256Sbrooks struct gif_softc *sc = ifp->if_softc; 65254263Sshin struct ifreq *ifr = (struct ifreq*)data; 65354263Sshin int error = 0, size; 65462587Sitojun struct sockaddr *dst, *src; 655105339Sume#ifdef SIOCSIFMTU /* xxx */ 656105339Sume u_long mtu; 657105339Sume#endif 658105339Sume 65954263Sshin switch (cmd) { 66054263Sshin case SIOCSIFADDR: 661105293Sume ifp->if_flags |= IFF_UP; 66254263Sshin break; 66362587Sitojun 66454263Sshin case SIOCSIFDSTADDR: 66554263Sshin break; 66654263Sshin 66754263Sshin case SIOCADDMULTI: 66854263Sshin case SIOCDELMULTI: 66954263Sshin break; 67054263Sshin 67162587Sitojun#ifdef SIOCSIFMTU /* xxx */ 67254263Sshin case SIOCGIFMTU: 67354263Sshin break; 67462587Sitojun 67554263Sshin case SIOCSIFMTU: 676105339Sume mtu = ifr->ifr_mtu; 677105339Sume if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 678105339Sume return (EINVAL); 679105339Sume ifp->if_mtu = mtu; 68054263Sshin break; 68162587Sitojun#endif /* SIOCSIFMTU */ 68254263Sshin 683105339Sume#ifdef INET 68454263Sshin case SIOCSIFPHYADDR: 685105339Sume#endif 68654263Sshin#ifdef INET6 68754263Sshin case SIOCSIFPHYADDR_IN6: 68854263Sshin#endif /* INET6 */ 68978064Sume case SIOCSLIFPHYADDR: 69062587Sitojun switch (cmd) { 69178064Sume#ifdef INET 69262587Sitojun case SIOCSIFPHYADDR: 69354263Sshin src = (struct sockaddr *) 69454263Sshin &(((struct in_aliasreq *)data)->ifra_addr); 69554263Sshin dst = (struct sockaddr *) 69654263Sshin &(((struct in_aliasreq *)data)->ifra_dstaddr); 69762587Sitojun break; 69878064Sume#endif 69962587Sitojun#ifdef INET6 70062587Sitojun case SIOCSIFPHYADDR_IN6: 70162587Sitojun src = (struct sockaddr *) 70262587Sitojun &(((struct in6_aliasreq *)data)->ifra_addr); 70362587Sitojun dst = (struct sockaddr *) 70462587Sitojun &(((struct in6_aliasreq *)data)->ifra_dstaddr); 70562587Sitojun break; 70662587Sitojun#endif 70778064Sume case SIOCSLIFPHYADDR: 70878064Sume src = (struct sockaddr *) 70978064Sume &(((struct if_laddrreq *)data)->addr); 71078064Sume dst = (struct sockaddr *) 71178064Sume &(((struct if_laddrreq *)data)->dstaddr); 712105293Sume break; 71391327Sbrooks default: 714105293Sume return EINVAL; 71562587Sitojun } 71654263Sshin 71778064Sume /* sa_family must be equal */ 71878064Sume if (src->sa_family != dst->sa_family) 71978064Sume return EINVAL; 72078064Sume 72178064Sume /* validate sa_len */ 72278064Sume switch (src->sa_family) { 72378064Sume#ifdef INET 72478064Sume case AF_INET: 72578064Sume if (src->sa_len != sizeof(struct sockaddr_in)) 72678064Sume return EINVAL; 72778064Sume break; 72878064Sume#endif 72978064Sume#ifdef INET6 73078064Sume case AF_INET6: 73178064Sume if (src->sa_len != sizeof(struct sockaddr_in6)) 73278064Sume return EINVAL; 73378064Sume break; 73478064Sume#endif 73578064Sume default: 73678064Sume return EAFNOSUPPORT; 73778064Sume } 73878064Sume switch (dst->sa_family) { 73978064Sume#ifdef INET 74078064Sume case AF_INET: 74178064Sume if (dst->sa_len != sizeof(struct sockaddr_in)) 74278064Sume return EINVAL; 74378064Sume break; 74478064Sume#endif 74578064Sume#ifdef INET6 74678064Sume case AF_INET6: 74778064Sume if (dst->sa_len != sizeof(struct sockaddr_in6)) 74878064Sume return EINVAL; 74978064Sume break; 75078064Sume#endif 75178064Sume default: 75278064Sume return EAFNOSUPPORT; 75378064Sume } 75478064Sume 75578064Sume /* check sa_family looks sane for the cmd */ 75678064Sume switch (cmd) { 75778064Sume case SIOCSIFPHYADDR: 75878064Sume if (src->sa_family == AF_INET) 75978064Sume break; 76078064Sume return EAFNOSUPPORT; 76178064Sume#ifdef INET6 76278064Sume case SIOCSIFPHYADDR_IN6: 76378064Sume if (src->sa_family == AF_INET6) 76478064Sume break; 76578064Sume return EAFNOSUPPORT; 76678064Sume#endif /* INET6 */ 76778064Sume case SIOCSLIFPHYADDR: 76878064Sume /* checks done in the above */ 76978064Sume break; 77078064Sume } 77178064Sume 772147256Sbrooks error = gif_set_tunnel(GIF2IFP(sc), src, dst); 77362587Sitojun break; 77462587Sitojun 77562587Sitojun#ifdef SIOCDIFPHYADDR 77662587Sitojun case SIOCDIFPHYADDR: 777147256Sbrooks gif_delete_tunnel(GIF2IFP(sc)); 77854263Sshin break; 77962587Sitojun#endif 78062587Sitojun 78154263Sshin case SIOCGIFPSRCADDR: 78254263Sshin#ifdef INET6 78354263Sshin case SIOCGIFPSRCADDR_IN6: 78454263Sshin#endif /* INET6 */ 78554263Sshin if (sc->gif_psrc == NULL) { 78654263Sshin error = EADDRNOTAVAIL; 78754263Sshin goto bad; 78854263Sshin } 78954263Sshin src = sc->gif_psrc; 79078064Sume switch (cmd) { 79154263Sshin#ifdef INET 79278064Sume case SIOCGIFPSRCADDR: 79354263Sshin dst = &ifr->ifr_addr; 79478064Sume size = sizeof(ifr->ifr_addr); 79554263Sshin break; 79654263Sshin#endif /* INET */ 79754263Sshin#ifdef INET6 79878064Sume case SIOCGIFPSRCADDR_IN6: 79954263Sshin dst = (struct sockaddr *) 80054263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 80178064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 80254263Sshin break; 80354263Sshin#endif /* INET6 */ 80454263Sshin default: 80554263Sshin error = EADDRNOTAVAIL; 80654263Sshin goto bad; 80754263Sshin } 80878064Sume if (src->sa_len > size) 80978064Sume return EINVAL; 81078064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 811148385Sume#ifdef INET6 812148385Sume if (dst->sa_family == AF_INET6) { 813148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 814148385Sume if (error != 0) 815148385Sume return (error); 816148385Sume } 817148385Sume#endif 81854263Sshin break; 81962587Sitojun 82054263Sshin case SIOCGIFPDSTADDR: 82154263Sshin#ifdef INET6 82254263Sshin case SIOCGIFPDSTADDR_IN6: 82354263Sshin#endif /* INET6 */ 82454263Sshin if (sc->gif_pdst == NULL) { 82554263Sshin error = EADDRNOTAVAIL; 82654263Sshin goto bad; 82754263Sshin } 82854263Sshin src = sc->gif_pdst; 82978064Sume switch (cmd) { 83054263Sshin#ifdef INET 83178064Sume case SIOCGIFPDSTADDR: 83254263Sshin dst = &ifr->ifr_addr; 83378064Sume size = sizeof(ifr->ifr_addr); 83454263Sshin break; 83554263Sshin#endif /* INET */ 83654263Sshin#ifdef INET6 83778064Sume case SIOCGIFPDSTADDR_IN6: 83854263Sshin dst = (struct sockaddr *) 83954263Sshin &(((struct in6_ifreq *)data)->ifr_addr); 84078064Sume size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 84154263Sshin break; 84254263Sshin#endif /* INET6 */ 84354263Sshin default: 84454263Sshin error = EADDRNOTAVAIL; 84554263Sshin goto bad; 84654263Sshin } 84778064Sume if (src->sa_len > size) 84878064Sume return EINVAL; 84978064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 850148385Sume#ifdef INET6 851148385Sume if (dst->sa_family == AF_INET6) { 852148385Sume error = sa6_recoverscope((struct sockaddr_in6 *)dst); 853148385Sume if (error != 0) 854148385Sume return (error); 855148385Sume } 856148385Sume#endif 85754263Sshin break; 85854263Sshin 85978064Sume case SIOCGLIFPHYADDR: 86078064Sume if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 86178064Sume error = EADDRNOTAVAIL; 86278064Sume goto bad; 86378064Sume } 86478064Sume 86578064Sume /* copy src */ 86678064Sume src = sc->gif_psrc; 86778064Sume dst = (struct sockaddr *) 86878064Sume &(((struct if_laddrreq *)data)->addr); 86978064Sume size = sizeof(((struct if_laddrreq *)data)->addr); 87078064Sume if (src->sa_len > size) 87178064Sume return EINVAL; 87278064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 87378064Sume 87478064Sume /* copy dst */ 87578064Sume src = sc->gif_pdst; 87678064Sume dst = (struct sockaddr *) 87778064Sume &(((struct if_laddrreq *)data)->dstaddr); 87878064Sume size = sizeof(((struct if_laddrreq *)data)->dstaddr); 87978064Sume if (src->sa_len > size) 88078064Sume return EINVAL; 88178064Sume bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 88278064Sume break; 88378064Sume 88454263Sshin case SIOCSIFFLAGS: 88562587Sitojun /* if_ioctl() takes care of it */ 88654263Sshin break; 88754263Sshin 88854263Sshin default: 88954263Sshin error = EINVAL; 89054263Sshin break; 89154263Sshin } 89254263Sshin bad: 89354263Sshin return error; 89454263Sshin} 89579106Sbrooks 896127305Srwatson/* 897127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check 898127305Srwatson * if a given tunnel is already present happens before we perform a 899127305Srwatson * potentially blocking setup of the tunnel. This code needs to be 900127305Srwatson * re-ordered so that the check and replacement can be atomic using 901127305Srwatson * a mutex. 902127305Srwatson */ 903105293Sumeint 904105293Sumegif_set_tunnel(ifp, src, dst) 905105293Sume struct ifnet *ifp; 906105293Sume struct sockaddr *src; 907105293Sume struct sockaddr *dst; 908105293Sume{ 909183550Szec INIT_VNET_GIF(ifp->if_vnet); 910147256Sbrooks struct gif_softc *sc = ifp->if_softc; 911105293Sume struct gif_softc *sc2; 912105293Sume struct sockaddr *osrc, *odst, *sa; 913105293Sume int error = 0; 914105293Sume 915127305Srwatson mtx_lock(&gif_mtx); 916181803Sbz LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 917105293Sume if (sc2 == sc) 918105293Sume continue; 919105293Sume if (!sc2->gif_pdst || !sc2->gif_psrc) 920105293Sume continue; 921105293Sume if (sc2->gif_pdst->sa_family != dst->sa_family || 922105293Sume sc2->gif_pdst->sa_len != dst->sa_len || 923105293Sume sc2->gif_psrc->sa_family != src->sa_family || 924105293Sume sc2->gif_psrc->sa_len != src->sa_len) 925105293Sume continue; 926105293Sume 927105293Sume /* 928105293Sume * Disallow parallel tunnels unless instructed 929105293Sume * otherwise. 930105293Sume */ 931181803Sbz if (!V_parallel_tunnels && 932105293Sume bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 933105293Sume bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 934105293Sume error = EADDRNOTAVAIL; 935127305Srwatson mtx_unlock(&gif_mtx); 936105293Sume goto bad; 937105293Sume } 938105293Sume 939105293Sume /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 940105293Sume } 941127305Srwatson mtx_unlock(&gif_mtx); 942105293Sume 943105293Sume /* XXX we can detach from both, but be polite just in case */ 944105293Sume if (sc->gif_psrc) 945105293Sume switch (sc->gif_psrc->sa_family) { 946105293Sume#ifdef INET 947105293Sume case AF_INET: 948105293Sume (void)in_gif_detach(sc); 949105293Sume break; 950105293Sume#endif 951105293Sume#ifdef INET6 952105293Sume case AF_INET6: 953105293Sume (void)in6_gif_detach(sc); 954105293Sume break; 955105293Sume#endif 956105293Sume } 957105293Sume 958105293Sume osrc = sc->gif_psrc; 959111119Simp sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 960105293Sume bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 961105293Sume sc->gif_psrc = sa; 962105293Sume 963105293Sume odst = sc->gif_pdst; 964111119Simp sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 965105293Sume bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 966105293Sume sc->gif_pdst = sa; 967105293Sume 968105293Sume switch (sc->gif_psrc->sa_family) { 969105293Sume#ifdef INET 970105293Sume case AF_INET: 971105293Sume error = in_gif_attach(sc); 972105293Sume break; 973105293Sume#endif 974105293Sume#ifdef INET6 975105293Sume case AF_INET6: 976148385Sume /* 977148385Sume * Check validity of the scope zone ID of the addresses, and 978148385Sume * convert it into the kernel internal form if necessary. 979148385Sume */ 980148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 981148385Sume if (error != 0) 982148385Sume break; 983148385Sume error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 984148385Sume if (error != 0) 985148385Sume break; 986105293Sume error = in6_gif_attach(sc); 987105293Sume break; 988105293Sume#endif 989105293Sume } 990105293Sume if (error) { 991105293Sume /* rollback */ 992105293Sume free((caddr_t)sc->gif_psrc, M_IFADDR); 993105293Sume free((caddr_t)sc->gif_pdst, M_IFADDR); 994105293Sume sc->gif_psrc = osrc; 995105293Sume sc->gif_pdst = odst; 996105293Sume goto bad; 997105293Sume } 998105293Sume 999105293Sume if (osrc) 1000105293Sume free((caddr_t)osrc, M_IFADDR); 1001105293Sume if (odst) 1002105293Sume free((caddr_t)odst, M_IFADDR); 1003105293Sume 1004105293Sume bad: 1005105293Sume if (sc->gif_psrc && sc->gif_pdst) 1006148887Srwatson ifp->if_drv_flags |= IFF_DRV_RUNNING; 1007105293Sume else 1008148887Srwatson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1009105293Sume 1010105293Sume return error; 1011105293Sume} 1012105293Sume 101379106Sbrooksvoid 1014105293Sumegif_delete_tunnel(ifp) 1015105293Sume struct ifnet *ifp; 101679106Sbrooks{ 1017147256Sbrooks struct gif_softc *sc = ifp->if_softc; 101879106Sbrooks 101979106Sbrooks if (sc->gif_psrc) { 102079106Sbrooks free((caddr_t)sc->gif_psrc, M_IFADDR); 102179106Sbrooks sc->gif_psrc = NULL; 102279106Sbrooks } 102379106Sbrooks if (sc->gif_pdst) { 102479106Sbrooks free((caddr_t)sc->gif_pdst, M_IFADDR); 102579106Sbrooks sc->gif_pdst = NULL; 102679106Sbrooks } 1027105293Sume /* it is safe to detach from both */ 1028105293Sume#ifdef INET 1029105293Sume (void)in_gif_detach(sc); 1030105293Sume#endif 1031105293Sume#ifdef INET6 1032105293Sume (void)in6_gif_detach(sc); 1033105293Sume#endif 1034160018Syar ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 103579106Sbrooks} 1036