1686cdd19SJun-ichiro itojun Hagino /* $FreeBSD$ */ 288ff5695SSUZUKI Shinsuke /* $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ */ 3686cdd19SJun-ichiro itojun Hagino 4cfa1ca9dSYoshinobu Inoue /* 5cfa1ca9dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6cfa1ca9dSYoshinobu Inoue * All rights reserved. 7cfa1ca9dSYoshinobu Inoue * 8cfa1ca9dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 9cfa1ca9dSYoshinobu Inoue * modification, are permitted provided that the following conditions 10cfa1ca9dSYoshinobu Inoue * are met: 11cfa1ca9dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 12cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 13cfa1ca9dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 14cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 15cfa1ca9dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 16cfa1ca9dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 17cfa1ca9dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 18cfa1ca9dSYoshinobu Inoue * without specific prior written permission. 19cfa1ca9dSYoshinobu Inoue * 20cfa1ca9dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21cfa1ca9dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22cfa1ca9dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23cfa1ca9dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24cfa1ca9dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25cfa1ca9dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26cfa1ca9dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27cfa1ca9dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28cfa1ca9dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29cfa1ca9dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30cfa1ca9dSYoshinobu Inoue * SUCH DAMAGE. 31cfa1ca9dSYoshinobu Inoue */ 32cfa1ca9dSYoshinobu Inoue 33cfa1ca9dSYoshinobu Inoue #include "opt_inet.h" 34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 35e0852ce2SRobert Watson #include "opt_mac.h" 36cfa1ca9dSYoshinobu Inoue 37cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 38cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 4010722b85SRobert Watson #include <sys/mac.h> 41cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 42cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 435dba30f1SPoul-Henning Kamp #include <sys/module.h> 44cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 45cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 46cfa1ca9dSYoshinobu Inoue #include <sys/errno.h> 47cfa1ca9dSYoshinobu Inoue #include <sys/time.h> 48872f786aSBrooks Davis #include <sys/sysctl.h> 49cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h> 50686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h> 5153dab5feSBrooks Davis #include <sys/conf.h> 52cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 53cfa1ca9dSYoshinobu Inoue 54cfa1ca9dSYoshinobu Inoue #include <net/if.h> 55f889d2efSBrooks Davis #include <net/if_clone.h> 56cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 57cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 58cfa1ca9dSYoshinobu Inoue #include <net/route.h> 59cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 60cfa1ca9dSYoshinobu Inoue 61cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 62cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 63cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 6433841545SHajimu UMEMOTO #ifdef INET 6533841545SHajimu UMEMOTO #include <netinet/in_var.h> 66cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h> 6753dab5feSBrooks Davis #include <netinet/ip_var.h> 68cfa1ca9dSYoshinobu Inoue #endif /* INET */ 69cfa1ca9dSYoshinobu Inoue 70cfa1ca9dSYoshinobu Inoue #ifdef INET6 71cfa1ca9dSYoshinobu Inoue #ifndef INET 72cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 73cfa1ca9dSYoshinobu Inoue #endif 74cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 75cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 76cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 77cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 78686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 79cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 80cfa1ca9dSYoshinobu Inoue 81686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 82cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 83cfa1ca9dSYoshinobu Inoue 84cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h> 85cfa1ca9dSYoshinobu Inoue 8653dab5feSBrooks Davis #define GIFNAME "gif" 87686cdd19SJun-ichiro itojun Hagino 8817d5cb2dSRobert Watson /* 898c7e1947SRuslan Ermilov * gif_mtx protects the global gif_softc_list. 9017d5cb2dSRobert Watson * XXX: Per-softc locking is still required. 9117d5cb2dSRobert Watson */ 9217d5cb2dSRobert Watson static struct mtx gif_mtx; 9353dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 94e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list; 9553dab5feSBrooks Davis 9694408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9794408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 9894408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 9994408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10094408d94SBrooks Davis 101bb2bfb4fSBrooks Davis static int gif_clone_create(struct if_clone *, int); 102bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 10353dab5feSBrooks Davis 104f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0); 10553dab5feSBrooks Davis 106929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 107cfa1ca9dSYoshinobu Inoue 108872f786aSBrooks Davis SYSCTL_DECL(_net_link); 109872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 110872f786aSBrooks Davis "Generic Tunnel Interface"); 111686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 112686cdd19SJun-ichiro itojun Hagino /* 113872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 114686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 115686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 116686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 117686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 118686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 119686cdd19SJun-ichiro itojun Hagino */ 120686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 121686cdd19SJun-ichiro itojun Hagino #endif 122686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST; 123872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 124872f786aSBrooks Davis &max_gif_nesting, 0, "Max nested tunnels"); 125872f786aSBrooks Davis 126872f786aSBrooks Davis /* 127872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 128872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 129872f786aSBrooks Davis * we allow control over this check here. 130872f786aSBrooks Davis */ 131872f786aSBrooks Davis #ifdef XBONEHACK 132872f786aSBrooks Davis static int parallel_tunnels = 1; 133872f786aSBrooks Davis #else 134872f786aSBrooks Davis static int parallel_tunnels = 0; 135872f786aSBrooks Davis #endif 136872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 137872f786aSBrooks Davis ¶llel_tunnels, 0, "Allow parallel tunnels?"); 138cfa1ca9dSYoshinobu Inoue 139bb2bfb4fSBrooks Davis static int 14053dab5feSBrooks Davis gif_clone_create(ifc, unit) 14153dab5feSBrooks Davis struct if_clone *ifc; 1423b16e7b2SMaxime Henrion int unit; 143cfa1ca9dSYoshinobu Inoue { 14433841545SHajimu UMEMOTO struct gif_softc *sc; 145cfa1ca9dSYoshinobu Inoue 146a163d034SWarner Losh sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK); 14753dab5feSBrooks Davis bzero(sc, sizeof(struct gif_softc)); 14853dab5feSBrooks Davis 14953dab5feSBrooks Davis sc->gif_if.if_softc = sc; 1509bf40edeSBrooks Davis if_initname(&sc->gif_if, ifc->ifc_name, unit); 151686cdd19SJun-ichiro itojun Hagino 1529426aedfSHajimu UMEMOTO gifattach0(sc); 153686cdd19SJun-ichiro itojun Hagino 15417d5cb2dSRobert Watson mtx_lock(&gif_mtx); 1559426aedfSHajimu UMEMOTO LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 15617d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 1579426aedfSHajimu UMEMOTO return (0); 1589426aedfSHajimu UMEMOTO } 1599426aedfSHajimu UMEMOTO 1609426aedfSHajimu UMEMOTO void 1619426aedfSHajimu UMEMOTO gifattach0(sc) 1629426aedfSHajimu UMEMOTO struct gif_softc *sc; 1639426aedfSHajimu UMEMOTO { 1649426aedfSHajimu UMEMOTO 1659426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 1669426aedfSHajimu UMEMOTO 1679426aedfSHajimu UMEMOTO sc->gif_if.if_addrlen = 0; 168cfa1ca9dSYoshinobu Inoue sc->gif_if.if_mtu = GIF_MTU; 169cfa1ca9dSYoshinobu Inoue sc->gif_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17033841545SHajimu UMEMOTO #if 0 17133841545SHajimu UMEMOTO /* turn off ingress filter */ 17233841545SHajimu UMEMOTO sc->gif_if.if_flags |= IFF_LINK2; 17333841545SHajimu UMEMOTO #endif 174cfa1ca9dSYoshinobu Inoue sc->gif_if.if_ioctl = gif_ioctl; 175cfa1ca9dSYoshinobu Inoue sc->gif_if.if_output = gif_output; 176cfa1ca9dSYoshinobu Inoue sc->gif_if.if_type = IFT_GIF; 177686cdd19SJun-ichiro itojun Hagino sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN; 178cfa1ca9dSYoshinobu Inoue if_attach(&sc->gif_if); 179cfa1ca9dSYoshinobu Inoue bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int)); 18094408d94SBrooks Davis if (ng_gif_attach_p != NULL) 18194408d94SBrooks Davis (*ng_gif_attach_p)(&sc->gif_if); 182cfa1ca9dSYoshinobu Inoue } 183cfa1ca9dSYoshinobu Inoue 18417d5cb2dSRobert Watson static void 18517d5cb2dSRobert Watson gif_destroy(struct gif_softc *sc) 18653dab5feSBrooks Davis { 18717d5cb2dSRobert Watson struct ifnet *ifp = &sc->gif_if; 18853dab5feSBrooks Davis int err; 18953dab5feSBrooks Davis 19017d5cb2dSRobert Watson gif_delete_tunnel(ifp); 1919426aedfSHajimu UMEMOTO #ifdef INET6 19253dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 19353dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 19453dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 19553dab5feSBrooks Davis } 1969426aedfSHajimu UMEMOTO #endif 1979426aedfSHajimu UMEMOTO #ifdef INET 1989426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 1999426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 2009426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 2019426aedfSHajimu UMEMOTO } 2029426aedfSHajimu UMEMOTO #endif 20353dab5feSBrooks Davis 20494408d94SBrooks Davis if (ng_gif_detach_p != NULL) 20594408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 20653dab5feSBrooks Davis bpfdetach(ifp); 20753dab5feSBrooks Davis if_detach(ifp); 20853dab5feSBrooks Davis 20953dab5feSBrooks Davis free(sc, M_GIF); 21053dab5feSBrooks Davis } 21153dab5feSBrooks Davis 212bb2bfb4fSBrooks Davis static void 21317d5cb2dSRobert Watson gif_clone_destroy(ifp) 21417d5cb2dSRobert Watson struct ifnet *ifp; 21517d5cb2dSRobert Watson { 21617d5cb2dSRobert Watson struct gif_softc *sc = ifp->if_softc; 21717d5cb2dSRobert Watson 21817d5cb2dSRobert Watson mtx_lock(&gif_mtx); 21917d5cb2dSRobert Watson LIST_REMOVE(sc, gif_list); 22017d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 22117d5cb2dSRobert Watson gif_destroy(sc); 22217d5cb2dSRobert Watson } 22317d5cb2dSRobert Watson 22453dab5feSBrooks Davis static int 22553dab5feSBrooks Davis gifmodevent(mod, type, data) 22653dab5feSBrooks Davis module_t mod; 22753dab5feSBrooks Davis int type; 22853dab5feSBrooks Davis void *data; 22953dab5feSBrooks Davis { 23017d5cb2dSRobert Watson struct gif_softc *sc; 23153dab5feSBrooks Davis 23253dab5feSBrooks Davis switch (type) { 23353dab5feSBrooks Davis case MOD_LOAD: 23417d5cb2dSRobert Watson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 23520af0ffaSBrooks Davis LIST_INIT(&gif_softc_list); 23653dab5feSBrooks Davis if_clone_attach(&gif_cloner); 23753dab5feSBrooks Davis 23853dab5feSBrooks Davis #ifdef INET6 23953dab5feSBrooks Davis ip6_gif_hlim = GIF_HLIM; 24053dab5feSBrooks Davis #endif 24153dab5feSBrooks Davis 24253dab5feSBrooks Davis break; 24353dab5feSBrooks Davis case MOD_UNLOAD: 24453dab5feSBrooks Davis if_clone_detach(&gif_cloner); 24553dab5feSBrooks Davis 24617d5cb2dSRobert Watson mtx_lock(&gif_mtx); 24717d5cb2dSRobert Watson while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) { 24817d5cb2dSRobert Watson LIST_REMOVE(sc, gif_list); 24917d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 25017d5cb2dSRobert Watson gif_destroy(sc); 25117d5cb2dSRobert Watson mtx_lock(&gif_mtx); 25217d5cb2dSRobert Watson } 25317d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 25417d5cb2dSRobert Watson mtx_destroy(&gif_mtx); 25553dab5feSBrooks Davis #ifdef INET6 25653dab5feSBrooks Davis ip6_gif_hlim = 0; 25753dab5feSBrooks Davis #endif 25853dab5feSBrooks Davis break; 25953dab5feSBrooks Davis } 26053dab5feSBrooks Davis return 0; 26153dab5feSBrooks Davis } 26253dab5feSBrooks Davis 26353dab5feSBrooks Davis static moduledata_t gif_mod = { 26453dab5feSBrooks Davis "if_gif", 26553dab5feSBrooks Davis gifmodevent, 26653dab5feSBrooks Davis 0 26753dab5feSBrooks Davis }; 26853dab5feSBrooks Davis 26953dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27020af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 271cfa1ca9dSYoshinobu Inoue 2729426aedfSHajimu UMEMOTO int 273686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg) 274686cdd19SJun-ichiro itojun Hagino const struct mbuf *m; 275686cdd19SJun-ichiro itojun Hagino int off; 276686cdd19SJun-ichiro itojun Hagino int proto; 277686cdd19SJun-ichiro itojun Hagino void *arg; 278686cdd19SJun-ichiro itojun Hagino { 279686cdd19SJun-ichiro itojun Hagino struct ip ip; 280686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 281686cdd19SJun-ichiro itojun Hagino 282686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 283686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 284686cdd19SJun-ichiro itojun Hagino return 0; 285686cdd19SJun-ichiro itojun Hagino 286686cdd19SJun-ichiro itojun Hagino if ((sc->gif_if.if_flags & IFF_UP) == 0) 287686cdd19SJun-ichiro itojun Hagino return 0; 288686cdd19SJun-ichiro itojun Hagino 289686cdd19SJun-ichiro itojun Hagino /* no physical address */ 290686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 291686cdd19SJun-ichiro itojun Hagino return 0; 292686cdd19SJun-ichiro itojun Hagino 293686cdd19SJun-ichiro itojun Hagino switch (proto) { 294686cdd19SJun-ichiro itojun Hagino #ifdef INET 295686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 296686cdd19SJun-ichiro itojun Hagino break; 297686cdd19SJun-ichiro itojun Hagino #endif 298686cdd19SJun-ichiro itojun Hagino #ifdef INET6 299686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 300686cdd19SJun-ichiro itojun Hagino break; 301686cdd19SJun-ichiro itojun Hagino #endif 302686cdd19SJun-ichiro itojun Hagino default: 303686cdd19SJun-ichiro itojun Hagino return 0; 304686cdd19SJun-ichiro itojun Hagino } 305686cdd19SJun-ichiro itojun Hagino 3063bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3073bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 3083bb61ca6SHajimu UMEMOTO return 0; 3093bb61ca6SHajimu UMEMOTO 3106f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 311686cdd19SJun-ichiro itojun Hagino 312686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 313686cdd19SJun-ichiro itojun Hagino #ifdef INET 314686cdd19SJun-ichiro itojun Hagino case 4: 315686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 316686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 317686cdd19SJun-ichiro itojun Hagino return 0; 318686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 319686cdd19SJun-ichiro itojun Hagino #endif 320686cdd19SJun-ichiro itojun Hagino #ifdef INET6 321686cdd19SJun-ichiro itojun Hagino case 6: 3229426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3239426aedfSHajimu UMEMOTO return 0; 324686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 325686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 326686cdd19SJun-ichiro itojun Hagino return 0; 327686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 328686cdd19SJun-ichiro itojun Hagino #endif 329686cdd19SJun-ichiro itojun Hagino default: 330686cdd19SJun-ichiro itojun Hagino return 0; 331686cdd19SJun-ichiro itojun Hagino } 332686cdd19SJun-ichiro itojun Hagino } 333686cdd19SJun-ichiro itojun Hagino 334cfa1ca9dSYoshinobu Inoue int 335cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt) 336cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 337cfa1ca9dSYoshinobu Inoue struct mbuf *m; 338cfa1ca9dSYoshinobu Inoue struct sockaddr *dst; 339cfa1ca9dSYoshinobu Inoue struct rtentry *rt; /* added in net2 */ 340cfa1ca9dSYoshinobu Inoue { 34133841545SHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc*)ifp; 3428c7e1947SRuslan Ermilov struct m_tag *mtag; 343cfa1ca9dSYoshinobu Inoue int error = 0; 3448c7e1947SRuslan Ermilov int gif_called; 345cfa1ca9dSYoshinobu Inoue 34610722b85SRobert Watson #ifdef MAC 34710722b85SRobert Watson error = mac_check_ifnet_transmit(ifp, m); 348e0852ce2SRobert Watson if (error) { 349e0852ce2SRobert Watson m_freem(m); 350e0852ce2SRobert Watson goto end; 351e0852ce2SRobert Watson } 35210722b85SRobert Watson #endif 35310722b85SRobert Watson 354cfa1ca9dSYoshinobu Inoue /* 355cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 3568c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 3578c7e1947SRuslan Ermilov * 3588c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 359cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 360cfa1ca9dSYoshinobu Inoue */ 3618c7e1947SRuslan Ermilov gif_called = 1; 3628c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 3638c7e1947SRuslan Ermilov while (mtag != NULL) { 3648c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 3658c7e1947SRuslan Ermilov log(LOG_NOTICE, 3668c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 3678c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 3688c7e1947SRuslan Ermilov m_freem(m); 3698c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 3708c7e1947SRuslan Ermilov goto end; 3718c7e1947SRuslan Ermilov } 3728c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 3738c7e1947SRuslan Ermilov gif_called++; 3748c7e1947SRuslan Ermilov } 3758c7e1947SRuslan Ermilov if (gif_called > max_gif_nesting) { 376cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 377cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 378523ebc4eSRobert Watson gif_called); 379cfa1ca9dSYoshinobu Inoue m_freem(m); 380cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 381cfa1ca9dSYoshinobu Inoue goto end; 382cfa1ca9dSYoshinobu Inoue } 3838c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 3848c7e1947SRuslan Ermilov M_NOWAIT); 3858c7e1947SRuslan Ermilov if (mtag == NULL) { 3868c7e1947SRuslan Ermilov m_freem(m); 3878c7e1947SRuslan Ermilov error = ENOMEM; 3888c7e1947SRuslan Ermilov goto end; 3898c7e1947SRuslan Ermilov } 3908c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 3918c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 392686cdd19SJun-ichiro itojun Hagino 393cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 394cfa1ca9dSYoshinobu Inoue if (!(ifp->if_flags & IFF_UP) || 395cfa1ca9dSYoshinobu Inoue sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 396cfa1ca9dSYoshinobu Inoue m_freem(m); 397cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 398cfa1ca9dSYoshinobu Inoue goto end; 399cfa1ca9dSYoshinobu Inoue } 400cfa1ca9dSYoshinobu Inoue 401cfa1ca9dSYoshinobu Inoue if (ifp->if_bpf) { 40233841545SHajimu UMEMOTO u_int32_t af = dst->sa_family; 403437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 404cfa1ca9dSYoshinobu Inoue } 405cfa1ca9dSYoshinobu Inoue ifp->if_opackets++; 406cfa1ca9dSYoshinobu Inoue ifp->if_obytes += m->m_pkthdr.len; 407cfa1ca9dSYoshinobu Inoue 40833841545SHajimu UMEMOTO /* inner AF-specific encapsulation */ 40933841545SHajimu UMEMOTO 410686cdd19SJun-ichiro itojun Hagino /* XXX should we check if our outer source is legal? */ 411686cdd19SJun-ichiro itojun Hagino 41233841545SHajimu UMEMOTO /* dispatch to output logic based on outer AF */ 413cfa1ca9dSYoshinobu Inoue switch (sc->gif_psrc->sa_family) { 414cfa1ca9dSYoshinobu Inoue #ifdef INET 415cfa1ca9dSYoshinobu Inoue case AF_INET: 416b6e28453SHajimu UMEMOTO error = in_gif_output(ifp, dst->sa_family, m); 417cfa1ca9dSYoshinobu Inoue break; 418cfa1ca9dSYoshinobu Inoue #endif 419cfa1ca9dSYoshinobu Inoue #ifdef INET6 420cfa1ca9dSYoshinobu Inoue case AF_INET6: 421b6e28453SHajimu UMEMOTO error = in6_gif_output(ifp, dst->sa_family, m); 422cfa1ca9dSYoshinobu Inoue break; 423cfa1ca9dSYoshinobu Inoue #endif 424cfa1ca9dSYoshinobu Inoue default: 425cfa1ca9dSYoshinobu Inoue m_freem(m); 426cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 42733841545SHajimu UMEMOTO goto end; 428cfa1ca9dSYoshinobu Inoue } 429cfa1ca9dSYoshinobu Inoue 430cfa1ca9dSYoshinobu Inoue end: 43133841545SHajimu UMEMOTO if (error) 43233841545SHajimu UMEMOTO ifp->if_oerrors++; 433cfa1ca9dSYoshinobu Inoue return error; 434cfa1ca9dSYoshinobu Inoue } 435cfa1ca9dSYoshinobu Inoue 436cfa1ca9dSYoshinobu Inoue void 43721fb391fSHajimu UMEMOTO gif_input(m, af, ifp) 438cfa1ca9dSYoshinobu Inoue struct mbuf *m; 439cfa1ca9dSYoshinobu Inoue int af; 44021fb391fSHajimu UMEMOTO struct ifnet *ifp; 441cfa1ca9dSYoshinobu Inoue { 442df5e1987SJonathan Lemon int isr; 443cfa1ca9dSYoshinobu Inoue 44421fb391fSHajimu UMEMOTO if (ifp == NULL) { 445cfa1ca9dSYoshinobu Inoue /* just in case */ 446cfa1ca9dSYoshinobu Inoue m_freem(m); 447cfa1ca9dSYoshinobu Inoue return; 448cfa1ca9dSYoshinobu Inoue } 449cfa1ca9dSYoshinobu Inoue 45021fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 451cfa1ca9dSYoshinobu Inoue 45210722b85SRobert Watson #ifdef MAC 45321fb391fSHajimu UMEMOTO mac_create_mbuf_from_ifnet(ifp, m); 45410722b85SRobert Watson #endif 45510722b85SRobert Watson 45621fb391fSHajimu UMEMOTO if (ifp->if_bpf) { 45733841545SHajimu UMEMOTO u_int32_t af1 = af; 458437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 459cfa1ca9dSYoshinobu Inoue } 460cfa1ca9dSYoshinobu Inoue 46194408d94SBrooks Davis if (ng_gif_input_p != NULL) { 46221fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 46394408d94SBrooks Davis if (m == NULL) 46494408d94SBrooks Davis return; 46594408d94SBrooks Davis } 46694408d94SBrooks Davis 467cfa1ca9dSYoshinobu Inoue /* 468cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 469cfa1ca9dSYoshinobu Inoue * specified address family. 470cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 471cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 472cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 473cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 474cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 47588ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 47688ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 477cfa1ca9dSYoshinobu Inoue */ 478cfa1ca9dSYoshinobu Inoue switch (af) { 479cfa1ca9dSYoshinobu Inoue #ifdef INET 480cfa1ca9dSYoshinobu Inoue case AF_INET: 481cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 482cfa1ca9dSYoshinobu Inoue break; 483cfa1ca9dSYoshinobu Inoue #endif 484cfa1ca9dSYoshinobu Inoue #ifdef INET6 485cfa1ca9dSYoshinobu Inoue case AF_INET6: 486cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 487cfa1ca9dSYoshinobu Inoue break; 488cfa1ca9dSYoshinobu Inoue #endif 489cfa1ca9dSYoshinobu Inoue default: 49094408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 49121fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 49294408d94SBrooks Davis else 493cfa1ca9dSYoshinobu Inoue m_freem(m); 494cfa1ca9dSYoshinobu Inoue return; 495cfa1ca9dSYoshinobu Inoue } 496cfa1ca9dSYoshinobu Inoue 49721fb391fSHajimu UMEMOTO ifp->if_ipackets++; 49821fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 4991cafed39SJonathan Lemon netisr_dispatch(isr, m); 500cfa1ca9dSYoshinobu Inoue } 501cfa1ca9dSYoshinobu Inoue 502686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 503cfa1ca9dSYoshinobu Inoue int 504cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data) 505cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 506cfa1ca9dSYoshinobu Inoue u_long cmd; 507cfa1ca9dSYoshinobu Inoue caddr_t data; 508cfa1ca9dSYoshinobu Inoue { 509cfa1ca9dSYoshinobu Inoue struct gif_softc *sc = (struct gif_softc*)ifp; 510cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 511cfa1ca9dSYoshinobu Inoue int error = 0, size; 512686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 5133bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 5143bb61ca6SHajimu UMEMOTO u_long mtu; 5153bb61ca6SHajimu UMEMOTO #endif 516cfa1ca9dSYoshinobu Inoue 517cfa1ca9dSYoshinobu Inoue switch (cmd) { 518cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 5199426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 520cfa1ca9dSYoshinobu Inoue break; 521cfa1ca9dSYoshinobu Inoue 522cfa1ca9dSYoshinobu Inoue case SIOCSIFDSTADDR: 523cfa1ca9dSYoshinobu Inoue break; 524cfa1ca9dSYoshinobu Inoue 525cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 526cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 527cfa1ca9dSYoshinobu Inoue break; 528cfa1ca9dSYoshinobu Inoue 529686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 530cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 531cfa1ca9dSYoshinobu Inoue break; 532686cdd19SJun-ichiro itojun Hagino 533cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 534cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 5353bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 536cfa1ca9dSYoshinobu Inoue return (EINVAL); 537cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 538cfa1ca9dSYoshinobu Inoue break; 539686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 540cfa1ca9dSYoshinobu Inoue 5413bb61ca6SHajimu UMEMOTO #ifdef INET 542cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 5433bb61ca6SHajimu UMEMOTO #endif 544cfa1ca9dSYoshinobu Inoue #ifdef INET6 545cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 546cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 54733841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 548686cdd19SJun-ichiro itojun Hagino switch (cmd) { 54933841545SHajimu UMEMOTO #ifdef INET 550686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 551cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 552cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 553cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 554cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 555cfa1ca9dSYoshinobu Inoue break; 55633841545SHajimu UMEMOTO #endif 557cfa1ca9dSYoshinobu Inoue #ifdef INET6 558686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 559cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 560cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 561cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 562cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 563686cdd19SJun-ichiro itojun Hagino break; 564686cdd19SJun-ichiro itojun Hagino #endif 56533841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 56633841545SHajimu UMEMOTO src = (struct sockaddr *) 56733841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 56833841545SHajimu UMEMOTO dst = (struct sockaddr *) 56933841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 5709426aedfSHajimu UMEMOTO break; 5716f4ded3aSBrooks Davis default: 5729426aedfSHajimu UMEMOTO return EINVAL; 57333841545SHajimu UMEMOTO } 57433841545SHajimu UMEMOTO 57533841545SHajimu UMEMOTO /* sa_family must be equal */ 57633841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 57733841545SHajimu UMEMOTO return EINVAL; 57833841545SHajimu UMEMOTO 57933841545SHajimu UMEMOTO /* validate sa_len */ 58033841545SHajimu UMEMOTO switch (src->sa_family) { 58133841545SHajimu UMEMOTO #ifdef INET 58233841545SHajimu UMEMOTO case AF_INET: 58333841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 58433841545SHajimu UMEMOTO return EINVAL; 58533841545SHajimu UMEMOTO break; 58633841545SHajimu UMEMOTO #endif 58733841545SHajimu UMEMOTO #ifdef INET6 58833841545SHajimu UMEMOTO case AF_INET6: 58933841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 59033841545SHajimu UMEMOTO return EINVAL; 59133841545SHajimu UMEMOTO break; 59233841545SHajimu UMEMOTO #endif 59333841545SHajimu UMEMOTO default: 59433841545SHajimu UMEMOTO return EAFNOSUPPORT; 59533841545SHajimu UMEMOTO } 59633841545SHajimu UMEMOTO switch (dst->sa_family) { 59733841545SHajimu UMEMOTO #ifdef INET 59833841545SHajimu UMEMOTO case AF_INET: 59933841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 60033841545SHajimu UMEMOTO return EINVAL; 60133841545SHajimu UMEMOTO break; 60233841545SHajimu UMEMOTO #endif 60333841545SHajimu UMEMOTO #ifdef INET6 60433841545SHajimu UMEMOTO case AF_INET6: 60533841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 60633841545SHajimu UMEMOTO return EINVAL; 60733841545SHajimu UMEMOTO break; 60833841545SHajimu UMEMOTO #endif 60933841545SHajimu UMEMOTO default: 61033841545SHajimu UMEMOTO return EAFNOSUPPORT; 61133841545SHajimu UMEMOTO } 61233841545SHajimu UMEMOTO 61333841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 61433841545SHajimu UMEMOTO switch (cmd) { 61533841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 61633841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 61733841545SHajimu UMEMOTO break; 61833841545SHajimu UMEMOTO return EAFNOSUPPORT; 61933841545SHajimu UMEMOTO #ifdef INET6 62033841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 62133841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 62233841545SHajimu UMEMOTO break; 62333841545SHajimu UMEMOTO return EAFNOSUPPORT; 62433841545SHajimu UMEMOTO #endif /* INET6 */ 62533841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 62633841545SHajimu UMEMOTO /* checks done in the above */ 62733841545SHajimu UMEMOTO break; 628686cdd19SJun-ichiro itojun Hagino } 629cfa1ca9dSYoshinobu Inoue 6309426aedfSHajimu UMEMOTO error = gif_set_tunnel(&sc->gif_if, src, dst); 631cfa1ca9dSYoshinobu Inoue break; 632cfa1ca9dSYoshinobu Inoue 633686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 634686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 6359426aedfSHajimu UMEMOTO gif_delete_tunnel(&sc->gif_if); 636686cdd19SJun-ichiro itojun Hagino break; 637686cdd19SJun-ichiro itojun Hagino #endif 638686cdd19SJun-ichiro itojun Hagino 639cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 640cfa1ca9dSYoshinobu Inoue #ifdef INET6 641cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 642cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 643cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 644cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 645cfa1ca9dSYoshinobu Inoue goto bad; 646cfa1ca9dSYoshinobu Inoue } 647cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 64833841545SHajimu UMEMOTO switch (cmd) { 649cfa1ca9dSYoshinobu Inoue #ifdef INET 65033841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 651cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 65233841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 653cfa1ca9dSYoshinobu Inoue break; 654cfa1ca9dSYoshinobu Inoue #endif /* INET */ 655cfa1ca9dSYoshinobu Inoue #ifdef INET6 65633841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 657cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 658cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 65933841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 660cfa1ca9dSYoshinobu Inoue break; 661cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 662cfa1ca9dSYoshinobu Inoue default: 663cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 664cfa1ca9dSYoshinobu Inoue goto bad; 665cfa1ca9dSYoshinobu Inoue } 66633841545SHajimu UMEMOTO if (src->sa_len > size) 66733841545SHajimu UMEMOTO return EINVAL; 66833841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 669cfa1ca9dSYoshinobu Inoue break; 670cfa1ca9dSYoshinobu Inoue 671cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 672cfa1ca9dSYoshinobu Inoue #ifdef INET6 673cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 674cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 675cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 676cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 677cfa1ca9dSYoshinobu Inoue goto bad; 678cfa1ca9dSYoshinobu Inoue } 679cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 68033841545SHajimu UMEMOTO switch (cmd) { 681cfa1ca9dSYoshinobu Inoue #ifdef INET 68233841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 683cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 68433841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 685cfa1ca9dSYoshinobu Inoue break; 686cfa1ca9dSYoshinobu Inoue #endif /* INET */ 687cfa1ca9dSYoshinobu Inoue #ifdef INET6 68833841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 689cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 690cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 69133841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 692cfa1ca9dSYoshinobu Inoue break; 693cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 694cfa1ca9dSYoshinobu Inoue default: 695cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 696cfa1ca9dSYoshinobu Inoue goto bad; 697cfa1ca9dSYoshinobu Inoue } 69833841545SHajimu UMEMOTO if (src->sa_len > size) 69933841545SHajimu UMEMOTO return EINVAL; 70033841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 70133841545SHajimu UMEMOTO break; 70233841545SHajimu UMEMOTO 70333841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 70433841545SHajimu UMEMOTO if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 70533841545SHajimu UMEMOTO error = EADDRNOTAVAIL; 70633841545SHajimu UMEMOTO goto bad; 70733841545SHajimu UMEMOTO } 70833841545SHajimu UMEMOTO 70933841545SHajimu UMEMOTO /* copy src */ 71033841545SHajimu UMEMOTO src = sc->gif_psrc; 71133841545SHajimu UMEMOTO dst = (struct sockaddr *) 71233841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 71333841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->addr); 71433841545SHajimu UMEMOTO if (src->sa_len > size) 71533841545SHajimu UMEMOTO return EINVAL; 71633841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 71733841545SHajimu UMEMOTO 71833841545SHajimu UMEMOTO /* copy dst */ 71933841545SHajimu UMEMOTO src = sc->gif_pdst; 72033841545SHajimu UMEMOTO dst = (struct sockaddr *) 72133841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 72233841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->dstaddr); 72333841545SHajimu UMEMOTO if (src->sa_len > size) 72433841545SHajimu UMEMOTO return EINVAL; 72533841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 726cfa1ca9dSYoshinobu Inoue break; 727cfa1ca9dSYoshinobu Inoue 728cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 729686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 730cfa1ca9dSYoshinobu Inoue break; 731cfa1ca9dSYoshinobu Inoue 732cfa1ca9dSYoshinobu Inoue default: 733cfa1ca9dSYoshinobu Inoue error = EINVAL; 734cfa1ca9dSYoshinobu Inoue break; 735cfa1ca9dSYoshinobu Inoue } 736cfa1ca9dSYoshinobu Inoue bad: 737cfa1ca9dSYoshinobu Inoue return error; 738cfa1ca9dSYoshinobu Inoue } 73953dab5feSBrooks Davis 74017d5cb2dSRobert Watson /* 74117d5cb2dSRobert Watson * XXXRW: There's a general event-ordering issue here: the code to check 74217d5cb2dSRobert Watson * if a given tunnel is already present happens before we perform a 74317d5cb2dSRobert Watson * potentially blocking setup of the tunnel. This code needs to be 74417d5cb2dSRobert Watson * re-ordered so that the check and replacement can be atomic using 74517d5cb2dSRobert Watson * a mutex. 74617d5cb2dSRobert Watson */ 7479426aedfSHajimu UMEMOTO int 7489426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst) 7499426aedfSHajimu UMEMOTO struct ifnet *ifp; 7509426aedfSHajimu UMEMOTO struct sockaddr *src; 7519426aedfSHajimu UMEMOTO struct sockaddr *dst; 75253dab5feSBrooks Davis { 7539426aedfSHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc *)ifp; 7549426aedfSHajimu UMEMOTO struct gif_softc *sc2; 7559426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 7569426aedfSHajimu UMEMOTO int s; 7579426aedfSHajimu UMEMOTO int error = 0; 7589426aedfSHajimu UMEMOTO 7599426aedfSHajimu UMEMOTO s = splnet(); 7609426aedfSHajimu UMEMOTO 76117d5cb2dSRobert Watson mtx_lock(&gif_mtx); 7629426aedfSHajimu UMEMOTO LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 7639426aedfSHajimu UMEMOTO if (sc2 == sc) 7649426aedfSHajimu UMEMOTO continue; 7659426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 7669426aedfSHajimu UMEMOTO continue; 7679426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 7689426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 7699426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 7709426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 7719426aedfSHajimu UMEMOTO continue; 7729426aedfSHajimu UMEMOTO 7739426aedfSHajimu UMEMOTO /* 7749426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 7759426aedfSHajimu UMEMOTO * otherwise. 7769426aedfSHajimu UMEMOTO */ 7779426aedfSHajimu UMEMOTO if (!parallel_tunnels && 7789426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 7799426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 7809426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 78117d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 7829426aedfSHajimu UMEMOTO goto bad; 7839426aedfSHajimu UMEMOTO } 7849426aedfSHajimu UMEMOTO 7859426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 7869426aedfSHajimu UMEMOTO } 78717d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 7889426aedfSHajimu UMEMOTO 7899426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 7909426aedfSHajimu UMEMOTO if (sc->gif_psrc) 7919426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 7929426aedfSHajimu UMEMOTO #ifdef INET 7939426aedfSHajimu UMEMOTO case AF_INET: 7949426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 7959426aedfSHajimu UMEMOTO break; 7969426aedfSHajimu UMEMOTO #endif 7979426aedfSHajimu UMEMOTO #ifdef INET6 7989426aedfSHajimu UMEMOTO case AF_INET6: 7999426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 8009426aedfSHajimu UMEMOTO break; 8019426aedfSHajimu UMEMOTO #endif 8029426aedfSHajimu UMEMOTO } 8039426aedfSHajimu UMEMOTO 8049426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 805a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 8069426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 8079426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 8089426aedfSHajimu UMEMOTO 8099426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 810a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 8119426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 8129426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 8139426aedfSHajimu UMEMOTO 8149426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 8159426aedfSHajimu UMEMOTO #ifdef INET 8169426aedfSHajimu UMEMOTO case AF_INET: 8179426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 8189426aedfSHajimu UMEMOTO break; 8199426aedfSHajimu UMEMOTO #endif 8209426aedfSHajimu UMEMOTO #ifdef INET6 8219426aedfSHajimu UMEMOTO case AF_INET6: 8229426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 8239426aedfSHajimu UMEMOTO break; 8249426aedfSHajimu UMEMOTO #endif 8259426aedfSHajimu UMEMOTO } 8269426aedfSHajimu UMEMOTO if (error) { 8279426aedfSHajimu UMEMOTO /* rollback */ 8289426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 8299426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 8309426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 8319426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 8329426aedfSHajimu UMEMOTO goto bad; 8339426aedfSHajimu UMEMOTO } 8349426aedfSHajimu UMEMOTO 8359426aedfSHajimu UMEMOTO if (osrc) 8369426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 8379426aedfSHajimu UMEMOTO if (odst) 8389426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 8399426aedfSHajimu UMEMOTO 8409426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8419426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8429426aedfSHajimu UMEMOTO else 8439426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8449426aedfSHajimu UMEMOTO splx(s); 8459426aedfSHajimu UMEMOTO 8469426aedfSHajimu UMEMOTO return 0; 8479426aedfSHajimu UMEMOTO 8489426aedfSHajimu UMEMOTO bad: 8499426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8509426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8519426aedfSHajimu UMEMOTO else 8529426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8539426aedfSHajimu UMEMOTO splx(s); 8549426aedfSHajimu UMEMOTO 8559426aedfSHajimu UMEMOTO return error; 8569426aedfSHajimu UMEMOTO } 8579426aedfSHajimu UMEMOTO 8589426aedfSHajimu UMEMOTO void 8599426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp) 8609426aedfSHajimu UMEMOTO struct ifnet *ifp; 8619426aedfSHajimu UMEMOTO { 8629426aedfSHajimu UMEMOTO struct gif_softc *sc = (struct gif_softc *)ifp; 8639426aedfSHajimu UMEMOTO int s; 8649426aedfSHajimu UMEMOTO 8659426aedfSHajimu UMEMOTO s = splnet(); 86653dab5feSBrooks Davis 86753dab5feSBrooks Davis if (sc->gif_psrc) { 86853dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 86953dab5feSBrooks Davis sc->gif_psrc = NULL; 87053dab5feSBrooks Davis } 87153dab5feSBrooks Davis if (sc->gif_pdst) { 87253dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 87353dab5feSBrooks Davis sc->gif_pdst = NULL; 87453dab5feSBrooks Davis } 8759426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 8769426aedfSHajimu UMEMOTO #ifdef INET 8779426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 8789426aedfSHajimu UMEMOTO #endif 8799426aedfSHajimu UMEMOTO #ifdef INET6 8809426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 8819426aedfSHajimu UMEMOTO #endif 8829426aedfSHajimu UMEMOTO 8839426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 8849426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_RUNNING; 8859426aedfSHajimu UMEMOTO else 8869426aedfSHajimu UMEMOTO ifp->if_flags &= ~IFF_RUNNING; 8879426aedfSHajimu UMEMOTO splx(s); 88853dab5feSBrooks Davis } 889