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 4c398230bSWarner Losh /*- 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> 77a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 78cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 79686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 80cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 81cfa1ca9dSYoshinobu Inoue 82686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 83cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 84cfa1ca9dSYoshinobu Inoue 85cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h> 86cfa1ca9dSYoshinobu Inoue 8753dab5feSBrooks Davis #define GIFNAME "gif" 88686cdd19SJun-ichiro itojun Hagino 8917d5cb2dSRobert Watson /* 908c7e1947SRuslan Ermilov * gif_mtx protects the global gif_softc_list. 9117d5cb2dSRobert Watson * XXX: Per-softc locking is still required. 9217d5cb2dSRobert Watson */ 9317d5cb2dSRobert Watson static struct mtx gif_mtx; 9453dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 95e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list; 9653dab5feSBrooks Davis 9794408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9894408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 9994408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10094408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10194408d94SBrooks Davis 102bb2bfb4fSBrooks Davis static int gif_clone_create(struct if_clone *, int); 103bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 10453dab5feSBrooks Davis 105f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0); 10653dab5feSBrooks Davis 107929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 108cfa1ca9dSYoshinobu Inoue 109872f786aSBrooks Davis SYSCTL_DECL(_net_link); 110872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 111872f786aSBrooks Davis "Generic Tunnel Interface"); 112686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 113686cdd19SJun-ichiro itojun Hagino /* 114872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 115686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 116686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 117686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 118686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 119686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 120686cdd19SJun-ichiro itojun Hagino */ 121686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 122686cdd19SJun-ichiro itojun Hagino #endif 123686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST; 124872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 125872f786aSBrooks Davis &max_gif_nesting, 0, "Max nested tunnels"); 126872f786aSBrooks Davis 127872f786aSBrooks Davis /* 128872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 129872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 130872f786aSBrooks Davis * we allow control over this check here. 131872f786aSBrooks Davis */ 132872f786aSBrooks Davis #ifdef XBONEHACK 133872f786aSBrooks Davis static int parallel_tunnels = 1; 134872f786aSBrooks Davis #else 135872f786aSBrooks Davis static int parallel_tunnels = 0; 136872f786aSBrooks Davis #endif 137872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 138872f786aSBrooks Davis ¶llel_tunnels, 0, "Allow parallel tunnels?"); 139cfa1ca9dSYoshinobu Inoue 140bb2bfb4fSBrooks Davis static int 14153dab5feSBrooks Davis gif_clone_create(ifc, unit) 14253dab5feSBrooks Davis struct if_clone *ifc; 1433b16e7b2SMaxime Henrion int unit; 144cfa1ca9dSYoshinobu Inoue { 14533841545SHajimu UMEMOTO struct gif_softc *sc; 146cfa1ca9dSYoshinobu Inoue 147e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 148fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 149fc74a9f9SBrooks Davis if (GIF2IFP(sc) == NULL) { 150fc74a9f9SBrooks Davis free(sc, M_GIF); 151fc74a9f9SBrooks Davis return (ENOSPC); 152fc74a9f9SBrooks Davis } 15353dab5feSBrooks Davis 154fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 155fc74a9f9SBrooks Davis if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 156686cdd19SJun-ichiro itojun Hagino 1579426aedfSHajimu UMEMOTO gifattach0(sc); 158686cdd19SJun-ichiro itojun Hagino 15917d5cb2dSRobert Watson mtx_lock(&gif_mtx); 1609426aedfSHajimu UMEMOTO LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 16117d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 1629426aedfSHajimu UMEMOTO return (0); 1639426aedfSHajimu UMEMOTO } 1649426aedfSHajimu UMEMOTO 1659426aedfSHajimu UMEMOTO void 1669426aedfSHajimu UMEMOTO gifattach0(sc) 1679426aedfSHajimu UMEMOTO struct gif_softc *sc; 1689426aedfSHajimu UMEMOTO { 1699426aedfSHajimu UMEMOTO 1709426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 1719426aedfSHajimu UMEMOTO 172fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 173fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 174fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 17533841545SHajimu UMEMOTO #if 0 17633841545SHajimu UMEMOTO /* turn off ingress filter */ 177fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 17833841545SHajimu UMEMOTO #endif 179fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 180fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 181fc74a9f9SBrooks Davis GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 182fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 18301399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 18494408d94SBrooks Davis if (ng_gif_attach_p != NULL) 185fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 186cfa1ca9dSYoshinobu Inoue } 187cfa1ca9dSYoshinobu Inoue 18817d5cb2dSRobert Watson static void 189febd0759SAndrew Thompson gif_clone_destroy(ifp) 190febd0759SAndrew Thompson struct ifnet *ifp; 19153dab5feSBrooks Davis { 19253dab5feSBrooks Davis int err; 193febd0759SAndrew Thompson struct gif_softc *sc = ifp->if_softc; 194febd0759SAndrew Thompson 195febd0759SAndrew Thompson mtx_lock(&gif_mtx); 196febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 197febd0759SAndrew Thompson mtx_unlock(&gif_mtx); 19853dab5feSBrooks Davis 19917d5cb2dSRobert Watson gif_delete_tunnel(ifp); 2009426aedfSHajimu UMEMOTO #ifdef INET6 20153dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 20253dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 20353dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 20453dab5feSBrooks Davis } 2059426aedfSHajimu UMEMOTO #endif 2069426aedfSHajimu UMEMOTO #ifdef INET 2079426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 2089426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 2099426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 2109426aedfSHajimu UMEMOTO } 2119426aedfSHajimu UMEMOTO #endif 21253dab5feSBrooks Davis 21394408d94SBrooks Davis if (ng_gif_detach_p != NULL) 21494408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 21553dab5feSBrooks Davis bpfdetach(ifp); 21653dab5feSBrooks Davis if_detach(ifp); 217fc74a9f9SBrooks Davis if_free(ifp); 21853dab5feSBrooks Davis 21953dab5feSBrooks Davis free(sc, M_GIF); 22053dab5feSBrooks Davis } 22153dab5feSBrooks Davis 22253dab5feSBrooks Davis static int 22353dab5feSBrooks Davis gifmodevent(mod, type, data) 22453dab5feSBrooks Davis module_t mod; 22553dab5feSBrooks Davis int type; 22653dab5feSBrooks Davis void *data; 22753dab5feSBrooks Davis { 22817d5cb2dSRobert Watson struct gif_softc *sc; 22953dab5feSBrooks Davis 23053dab5feSBrooks Davis switch (type) { 23153dab5feSBrooks Davis case MOD_LOAD: 23217d5cb2dSRobert Watson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 23320af0ffaSBrooks Davis LIST_INIT(&gif_softc_list); 23453dab5feSBrooks Davis if_clone_attach(&gif_cloner); 23553dab5feSBrooks Davis 23653dab5feSBrooks Davis #ifdef INET6 23753dab5feSBrooks Davis ip6_gif_hlim = GIF_HLIM; 23853dab5feSBrooks Davis #endif 23953dab5feSBrooks Davis 24053dab5feSBrooks Davis break; 24153dab5feSBrooks Davis case MOD_UNLOAD: 24253dab5feSBrooks Davis if_clone_detach(&gif_cloner); 24353dab5feSBrooks Davis 24417d5cb2dSRobert Watson mtx_lock(&gif_mtx); 24517d5cb2dSRobert Watson while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) { 24617d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 247febd0759SAndrew Thompson ifc_simple_destroy(&gif_cloner, GIF2IFP(sc)); 24817d5cb2dSRobert Watson mtx_lock(&gif_mtx); 24917d5cb2dSRobert Watson } 25017d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 25117d5cb2dSRobert Watson mtx_destroy(&gif_mtx); 25253dab5feSBrooks Davis #ifdef INET6 25353dab5feSBrooks Davis ip6_gif_hlim = 0; 25453dab5feSBrooks Davis #endif 25553dab5feSBrooks Davis break; 2563e019deaSPoul-Henning Kamp default: 2573e019deaSPoul-Henning Kamp return EOPNOTSUPP; 25853dab5feSBrooks Davis } 25953dab5feSBrooks Davis return 0; 26053dab5feSBrooks Davis } 26153dab5feSBrooks Davis 26253dab5feSBrooks Davis static moduledata_t gif_mod = { 26353dab5feSBrooks Davis "if_gif", 26453dab5feSBrooks Davis gifmodevent, 26553dab5feSBrooks Davis 0 26653dab5feSBrooks Davis }; 26753dab5feSBrooks Davis 26853dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 26920af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 270cfa1ca9dSYoshinobu Inoue 2719426aedfSHajimu UMEMOTO int 272686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg) 273686cdd19SJun-ichiro itojun Hagino const struct mbuf *m; 274686cdd19SJun-ichiro itojun Hagino int off; 275686cdd19SJun-ichiro itojun Hagino int proto; 276686cdd19SJun-ichiro itojun Hagino void *arg; 277686cdd19SJun-ichiro itojun Hagino { 278686cdd19SJun-ichiro itojun Hagino struct ip ip; 279686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 280686cdd19SJun-ichiro itojun Hagino 281686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 282686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 283686cdd19SJun-ichiro itojun Hagino return 0; 284686cdd19SJun-ichiro itojun Hagino 285fc74a9f9SBrooks Davis if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 286686cdd19SJun-ichiro itojun Hagino return 0; 287686cdd19SJun-ichiro itojun Hagino 288686cdd19SJun-ichiro itojun Hagino /* no physical address */ 289686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 290686cdd19SJun-ichiro itojun Hagino return 0; 291686cdd19SJun-ichiro itojun Hagino 292686cdd19SJun-ichiro itojun Hagino switch (proto) { 293686cdd19SJun-ichiro itojun Hagino #ifdef INET 294686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 295686cdd19SJun-ichiro itojun Hagino break; 296686cdd19SJun-ichiro itojun Hagino #endif 297686cdd19SJun-ichiro itojun Hagino #ifdef INET6 298686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 299686cdd19SJun-ichiro itojun Hagino break; 300686cdd19SJun-ichiro itojun Hagino #endif 301686cdd19SJun-ichiro itojun Hagino default: 302686cdd19SJun-ichiro itojun Hagino return 0; 303686cdd19SJun-ichiro itojun Hagino } 304686cdd19SJun-ichiro itojun Hagino 3053bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3063bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 3073bb61ca6SHajimu UMEMOTO return 0; 3083bb61ca6SHajimu UMEMOTO 3096f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 310686cdd19SJun-ichiro itojun Hagino 311686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 312686cdd19SJun-ichiro itojun Hagino #ifdef INET 313686cdd19SJun-ichiro itojun Hagino case 4: 314686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 315686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 316686cdd19SJun-ichiro itojun Hagino return 0; 317686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 318686cdd19SJun-ichiro itojun Hagino #endif 319686cdd19SJun-ichiro itojun Hagino #ifdef INET6 320686cdd19SJun-ichiro itojun Hagino case 6: 3219426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3229426aedfSHajimu UMEMOTO return 0; 323686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 324686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 325686cdd19SJun-ichiro itojun Hagino return 0; 326686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 327686cdd19SJun-ichiro itojun Hagino #endif 328686cdd19SJun-ichiro itojun Hagino default: 329686cdd19SJun-ichiro itojun Hagino return 0; 330686cdd19SJun-ichiro itojun Hagino } 331686cdd19SJun-ichiro itojun Hagino } 332686cdd19SJun-ichiro itojun Hagino 333cfa1ca9dSYoshinobu Inoue int 334cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt) 335cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 336cfa1ca9dSYoshinobu Inoue struct mbuf *m; 337cfa1ca9dSYoshinobu Inoue struct sockaddr *dst; 338cfa1ca9dSYoshinobu Inoue struct rtentry *rt; /* added in net2 */ 339cfa1ca9dSYoshinobu Inoue { 340fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 3418c7e1947SRuslan Ermilov struct m_tag *mtag; 342cfa1ca9dSYoshinobu Inoue int error = 0; 3438c7e1947SRuslan Ermilov int gif_called; 34401399f34SDavid Malone u_int32_t af; 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 40101399f34SDavid Malone /* BPF writes need to be handled specially. */ 40201399f34SDavid Malone if (dst->sa_family == AF_UNSPEC) { 40301399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 40401399f34SDavid Malone dst->sa_family = af; 40501399f34SDavid Malone } 40601399f34SDavid Malone 407cfa1ca9dSYoshinobu Inoue if (ifp->if_bpf) { 40801399f34SDavid Malone af = dst->sa_family; 409437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m); 410cfa1ca9dSYoshinobu Inoue } 411cfa1ca9dSYoshinobu Inoue ifp->if_opackets++; 412cfa1ca9dSYoshinobu Inoue ifp->if_obytes += m->m_pkthdr.len; 413cfa1ca9dSYoshinobu Inoue 41433841545SHajimu UMEMOTO /* inner AF-specific encapsulation */ 41533841545SHajimu UMEMOTO 416686cdd19SJun-ichiro itojun Hagino /* XXX should we check if our outer source is legal? */ 417686cdd19SJun-ichiro itojun Hagino 41833841545SHajimu UMEMOTO /* dispatch to output logic based on outer AF */ 419cfa1ca9dSYoshinobu Inoue switch (sc->gif_psrc->sa_family) { 420cfa1ca9dSYoshinobu Inoue #ifdef INET 421cfa1ca9dSYoshinobu Inoue case AF_INET: 422b6e28453SHajimu UMEMOTO error = in_gif_output(ifp, dst->sa_family, m); 423cfa1ca9dSYoshinobu Inoue break; 424cfa1ca9dSYoshinobu Inoue #endif 425cfa1ca9dSYoshinobu Inoue #ifdef INET6 426cfa1ca9dSYoshinobu Inoue case AF_INET6: 427b6e28453SHajimu UMEMOTO error = in6_gif_output(ifp, dst->sa_family, m); 428cfa1ca9dSYoshinobu Inoue break; 429cfa1ca9dSYoshinobu Inoue #endif 430cfa1ca9dSYoshinobu Inoue default: 431cfa1ca9dSYoshinobu Inoue m_freem(m); 432cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 43333841545SHajimu UMEMOTO goto end; 434cfa1ca9dSYoshinobu Inoue } 435cfa1ca9dSYoshinobu Inoue 436cfa1ca9dSYoshinobu Inoue end: 43733841545SHajimu UMEMOTO if (error) 43833841545SHajimu UMEMOTO ifp->if_oerrors++; 439cfa1ca9dSYoshinobu Inoue return error; 440cfa1ca9dSYoshinobu Inoue } 441cfa1ca9dSYoshinobu Inoue 442cfa1ca9dSYoshinobu Inoue void 44321fb391fSHajimu UMEMOTO gif_input(m, af, ifp) 444cfa1ca9dSYoshinobu Inoue struct mbuf *m; 445cfa1ca9dSYoshinobu Inoue int af; 44621fb391fSHajimu UMEMOTO struct ifnet *ifp; 447cfa1ca9dSYoshinobu Inoue { 448df5e1987SJonathan Lemon int isr; 449cfa1ca9dSYoshinobu Inoue 45021fb391fSHajimu UMEMOTO if (ifp == NULL) { 451cfa1ca9dSYoshinobu Inoue /* just in case */ 452cfa1ca9dSYoshinobu Inoue m_freem(m); 453cfa1ca9dSYoshinobu Inoue return; 454cfa1ca9dSYoshinobu Inoue } 455cfa1ca9dSYoshinobu Inoue 45621fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 457cfa1ca9dSYoshinobu Inoue 45810722b85SRobert Watson #ifdef MAC 45921fb391fSHajimu UMEMOTO mac_create_mbuf_from_ifnet(ifp, m); 46010722b85SRobert Watson #endif 46110722b85SRobert Watson 46221fb391fSHajimu UMEMOTO if (ifp->if_bpf) { 46333841545SHajimu UMEMOTO u_int32_t af1 = af; 464437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 465cfa1ca9dSYoshinobu Inoue } 466cfa1ca9dSYoshinobu Inoue 46794408d94SBrooks Davis if (ng_gif_input_p != NULL) { 46821fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 46994408d94SBrooks Davis if (m == NULL) 47094408d94SBrooks Davis return; 47194408d94SBrooks Davis } 47294408d94SBrooks Davis 473cfa1ca9dSYoshinobu Inoue /* 474cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 475cfa1ca9dSYoshinobu Inoue * specified address family. 476cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 477cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 478cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 479cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 480cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 48188ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 48288ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 483cfa1ca9dSYoshinobu Inoue */ 484cfa1ca9dSYoshinobu Inoue switch (af) { 485cfa1ca9dSYoshinobu Inoue #ifdef INET 486cfa1ca9dSYoshinobu Inoue case AF_INET: 487cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 488cfa1ca9dSYoshinobu Inoue break; 489cfa1ca9dSYoshinobu Inoue #endif 490cfa1ca9dSYoshinobu Inoue #ifdef INET6 491cfa1ca9dSYoshinobu Inoue case AF_INET6: 492cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 493cfa1ca9dSYoshinobu Inoue break; 494cfa1ca9dSYoshinobu Inoue #endif 495cfa1ca9dSYoshinobu Inoue default: 49694408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 49721fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 49894408d94SBrooks Davis else 499cfa1ca9dSYoshinobu Inoue m_freem(m); 500cfa1ca9dSYoshinobu Inoue return; 501cfa1ca9dSYoshinobu Inoue } 502cfa1ca9dSYoshinobu Inoue 50321fb391fSHajimu UMEMOTO ifp->if_ipackets++; 50421fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 5051cafed39SJonathan Lemon netisr_dispatch(isr, m); 506cfa1ca9dSYoshinobu Inoue } 507cfa1ca9dSYoshinobu Inoue 508686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 509cfa1ca9dSYoshinobu Inoue int 510cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data) 511cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 512cfa1ca9dSYoshinobu Inoue u_long cmd; 513cfa1ca9dSYoshinobu Inoue caddr_t data; 514cfa1ca9dSYoshinobu Inoue { 515fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 516cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 517cfa1ca9dSYoshinobu Inoue int error = 0, size; 518686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 5193bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 5203bb61ca6SHajimu UMEMOTO u_long mtu; 5213bb61ca6SHajimu UMEMOTO #endif 522cfa1ca9dSYoshinobu Inoue 523cfa1ca9dSYoshinobu Inoue switch (cmd) { 524cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 5259426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 526cfa1ca9dSYoshinobu Inoue break; 527cfa1ca9dSYoshinobu Inoue 528cfa1ca9dSYoshinobu Inoue case SIOCSIFDSTADDR: 529cfa1ca9dSYoshinobu Inoue break; 530cfa1ca9dSYoshinobu Inoue 531cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 532cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 533cfa1ca9dSYoshinobu Inoue break; 534cfa1ca9dSYoshinobu Inoue 535686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 536cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 537cfa1ca9dSYoshinobu Inoue break; 538686cdd19SJun-ichiro itojun Hagino 539cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 540cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 5413bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 542cfa1ca9dSYoshinobu Inoue return (EINVAL); 543cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 544cfa1ca9dSYoshinobu Inoue break; 545686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 546cfa1ca9dSYoshinobu Inoue 5473bb61ca6SHajimu UMEMOTO #ifdef INET 548cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 5493bb61ca6SHajimu UMEMOTO #endif 550cfa1ca9dSYoshinobu Inoue #ifdef INET6 551cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 552cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 55333841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 554686cdd19SJun-ichiro itojun Hagino switch (cmd) { 55533841545SHajimu UMEMOTO #ifdef INET 556686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 557cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 558cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 559cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 560cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 561cfa1ca9dSYoshinobu Inoue break; 56233841545SHajimu UMEMOTO #endif 563cfa1ca9dSYoshinobu Inoue #ifdef INET6 564686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 565cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 566cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 567cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 568cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 569686cdd19SJun-ichiro itojun Hagino break; 570686cdd19SJun-ichiro itojun Hagino #endif 57133841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 57233841545SHajimu UMEMOTO src = (struct sockaddr *) 57333841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 57433841545SHajimu UMEMOTO dst = (struct sockaddr *) 57533841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 5769426aedfSHajimu UMEMOTO break; 5776f4ded3aSBrooks Davis default: 5789426aedfSHajimu UMEMOTO return EINVAL; 57933841545SHajimu UMEMOTO } 58033841545SHajimu UMEMOTO 58133841545SHajimu UMEMOTO /* sa_family must be equal */ 58233841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 58333841545SHajimu UMEMOTO return EINVAL; 58433841545SHajimu UMEMOTO 58533841545SHajimu UMEMOTO /* validate sa_len */ 58633841545SHajimu UMEMOTO switch (src->sa_family) { 58733841545SHajimu UMEMOTO #ifdef INET 58833841545SHajimu UMEMOTO case AF_INET: 58933841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 59033841545SHajimu UMEMOTO return EINVAL; 59133841545SHajimu UMEMOTO break; 59233841545SHajimu UMEMOTO #endif 59333841545SHajimu UMEMOTO #ifdef INET6 59433841545SHajimu UMEMOTO case AF_INET6: 59533841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 59633841545SHajimu UMEMOTO return EINVAL; 59733841545SHajimu UMEMOTO break; 59833841545SHajimu UMEMOTO #endif 59933841545SHajimu UMEMOTO default: 60033841545SHajimu UMEMOTO return EAFNOSUPPORT; 60133841545SHajimu UMEMOTO } 60233841545SHajimu UMEMOTO switch (dst->sa_family) { 60333841545SHajimu UMEMOTO #ifdef INET 60433841545SHajimu UMEMOTO case AF_INET: 60533841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 60633841545SHajimu UMEMOTO return EINVAL; 60733841545SHajimu UMEMOTO break; 60833841545SHajimu UMEMOTO #endif 60933841545SHajimu UMEMOTO #ifdef INET6 61033841545SHajimu UMEMOTO case AF_INET6: 61133841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 61233841545SHajimu UMEMOTO return EINVAL; 61333841545SHajimu UMEMOTO break; 61433841545SHajimu UMEMOTO #endif 61533841545SHajimu UMEMOTO default: 61633841545SHajimu UMEMOTO return EAFNOSUPPORT; 61733841545SHajimu UMEMOTO } 61833841545SHajimu UMEMOTO 61933841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 62033841545SHajimu UMEMOTO switch (cmd) { 62133841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 62233841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 62333841545SHajimu UMEMOTO break; 62433841545SHajimu UMEMOTO return EAFNOSUPPORT; 62533841545SHajimu UMEMOTO #ifdef INET6 62633841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 62733841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 62833841545SHajimu UMEMOTO break; 62933841545SHajimu UMEMOTO return EAFNOSUPPORT; 63033841545SHajimu UMEMOTO #endif /* INET6 */ 63133841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 63233841545SHajimu UMEMOTO /* checks done in the above */ 63333841545SHajimu UMEMOTO break; 634686cdd19SJun-ichiro itojun Hagino } 635cfa1ca9dSYoshinobu Inoue 636fc74a9f9SBrooks Davis error = gif_set_tunnel(GIF2IFP(sc), src, dst); 637cfa1ca9dSYoshinobu Inoue break; 638cfa1ca9dSYoshinobu Inoue 639686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 640686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 641fc74a9f9SBrooks Davis gif_delete_tunnel(GIF2IFP(sc)); 642686cdd19SJun-ichiro itojun Hagino break; 643686cdd19SJun-ichiro itojun Hagino #endif 644686cdd19SJun-ichiro itojun Hagino 645cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 646cfa1ca9dSYoshinobu Inoue #ifdef INET6 647cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 648cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 649cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 650cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 651cfa1ca9dSYoshinobu Inoue goto bad; 652cfa1ca9dSYoshinobu Inoue } 653cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 65433841545SHajimu UMEMOTO switch (cmd) { 655cfa1ca9dSYoshinobu Inoue #ifdef INET 65633841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 657cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 65833841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 659cfa1ca9dSYoshinobu Inoue break; 660cfa1ca9dSYoshinobu Inoue #endif /* INET */ 661cfa1ca9dSYoshinobu Inoue #ifdef INET6 66233841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 663cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 664cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 66533841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 666cfa1ca9dSYoshinobu Inoue break; 667cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 668cfa1ca9dSYoshinobu Inoue default: 669cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 670cfa1ca9dSYoshinobu Inoue goto bad; 671cfa1ca9dSYoshinobu Inoue } 67233841545SHajimu UMEMOTO if (src->sa_len > size) 67333841545SHajimu UMEMOTO return EINVAL; 67433841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 675a1f7e5f8SHajimu UMEMOTO #ifdef INET6 676a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 677a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 678a1f7e5f8SHajimu UMEMOTO if (error != 0) 679a1f7e5f8SHajimu UMEMOTO return (error); 680a1f7e5f8SHajimu UMEMOTO } 681a1f7e5f8SHajimu UMEMOTO #endif 682cfa1ca9dSYoshinobu Inoue break; 683cfa1ca9dSYoshinobu Inoue 684cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 685cfa1ca9dSYoshinobu Inoue #ifdef INET6 686cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 687cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 688cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 689cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 690cfa1ca9dSYoshinobu Inoue goto bad; 691cfa1ca9dSYoshinobu Inoue } 692cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 69333841545SHajimu UMEMOTO switch (cmd) { 694cfa1ca9dSYoshinobu Inoue #ifdef INET 69533841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 696cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 69733841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 698cfa1ca9dSYoshinobu Inoue break; 699cfa1ca9dSYoshinobu Inoue #endif /* INET */ 700cfa1ca9dSYoshinobu Inoue #ifdef INET6 70133841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 702cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 703cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 70433841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 705cfa1ca9dSYoshinobu Inoue break; 706cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 707cfa1ca9dSYoshinobu Inoue default: 708cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 709cfa1ca9dSYoshinobu Inoue goto bad; 710cfa1ca9dSYoshinobu Inoue } 71133841545SHajimu UMEMOTO if (src->sa_len > size) 71233841545SHajimu UMEMOTO return EINVAL; 71333841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 714a1f7e5f8SHajimu UMEMOTO #ifdef INET6 715a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 716a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 717a1f7e5f8SHajimu UMEMOTO if (error != 0) 718a1f7e5f8SHajimu UMEMOTO return (error); 719a1f7e5f8SHajimu UMEMOTO } 720a1f7e5f8SHajimu UMEMOTO #endif 72133841545SHajimu UMEMOTO break; 72233841545SHajimu UMEMOTO 72333841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 72433841545SHajimu UMEMOTO if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 72533841545SHajimu UMEMOTO error = EADDRNOTAVAIL; 72633841545SHajimu UMEMOTO goto bad; 72733841545SHajimu UMEMOTO } 72833841545SHajimu UMEMOTO 72933841545SHajimu UMEMOTO /* copy src */ 73033841545SHajimu UMEMOTO src = sc->gif_psrc; 73133841545SHajimu UMEMOTO dst = (struct sockaddr *) 73233841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 73333841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->addr); 73433841545SHajimu UMEMOTO if (src->sa_len > size) 73533841545SHajimu UMEMOTO return EINVAL; 73633841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 73733841545SHajimu UMEMOTO 73833841545SHajimu UMEMOTO /* copy dst */ 73933841545SHajimu UMEMOTO src = sc->gif_pdst; 74033841545SHajimu UMEMOTO dst = (struct sockaddr *) 74133841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 74233841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->dstaddr); 74333841545SHajimu UMEMOTO if (src->sa_len > size) 74433841545SHajimu UMEMOTO return EINVAL; 74533841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 746cfa1ca9dSYoshinobu Inoue break; 747cfa1ca9dSYoshinobu Inoue 748cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 749686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 750cfa1ca9dSYoshinobu Inoue break; 751cfa1ca9dSYoshinobu Inoue 752cfa1ca9dSYoshinobu Inoue default: 753cfa1ca9dSYoshinobu Inoue error = EINVAL; 754cfa1ca9dSYoshinobu Inoue break; 755cfa1ca9dSYoshinobu Inoue } 756cfa1ca9dSYoshinobu Inoue bad: 757cfa1ca9dSYoshinobu Inoue return error; 758cfa1ca9dSYoshinobu Inoue } 75953dab5feSBrooks Davis 76017d5cb2dSRobert Watson /* 76117d5cb2dSRobert Watson * XXXRW: There's a general event-ordering issue here: the code to check 76217d5cb2dSRobert Watson * if a given tunnel is already present happens before we perform a 76317d5cb2dSRobert Watson * potentially blocking setup of the tunnel. This code needs to be 76417d5cb2dSRobert Watson * re-ordered so that the check and replacement can be atomic using 76517d5cb2dSRobert Watson * a mutex. 76617d5cb2dSRobert Watson */ 7679426aedfSHajimu UMEMOTO int 7689426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst) 7699426aedfSHajimu UMEMOTO struct ifnet *ifp; 7709426aedfSHajimu UMEMOTO struct sockaddr *src; 7719426aedfSHajimu UMEMOTO struct sockaddr *dst; 77253dab5feSBrooks Davis { 773fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 7749426aedfSHajimu UMEMOTO struct gif_softc *sc2; 7759426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 7769426aedfSHajimu UMEMOTO int s; 7779426aedfSHajimu UMEMOTO int error = 0; 7789426aedfSHajimu UMEMOTO 7799426aedfSHajimu UMEMOTO s = splnet(); 7809426aedfSHajimu UMEMOTO 78117d5cb2dSRobert Watson mtx_lock(&gif_mtx); 7829426aedfSHajimu UMEMOTO LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 7839426aedfSHajimu UMEMOTO if (sc2 == sc) 7849426aedfSHajimu UMEMOTO continue; 7859426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 7869426aedfSHajimu UMEMOTO continue; 7879426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 7889426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 7899426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 7909426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 7919426aedfSHajimu UMEMOTO continue; 7929426aedfSHajimu UMEMOTO 7939426aedfSHajimu UMEMOTO /* 7949426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 7959426aedfSHajimu UMEMOTO * otherwise. 7969426aedfSHajimu UMEMOTO */ 7979426aedfSHajimu UMEMOTO if (!parallel_tunnels && 7989426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 7999426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 8009426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 80117d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 8029426aedfSHajimu UMEMOTO goto bad; 8039426aedfSHajimu UMEMOTO } 8049426aedfSHajimu UMEMOTO 8059426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 8069426aedfSHajimu UMEMOTO } 80717d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 8089426aedfSHajimu UMEMOTO 8099426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 8109426aedfSHajimu UMEMOTO if (sc->gif_psrc) 8119426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 8129426aedfSHajimu UMEMOTO #ifdef INET 8139426aedfSHajimu UMEMOTO case AF_INET: 8149426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 8159426aedfSHajimu UMEMOTO break; 8169426aedfSHajimu UMEMOTO #endif 8179426aedfSHajimu UMEMOTO #ifdef INET6 8189426aedfSHajimu UMEMOTO case AF_INET6: 8199426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 8209426aedfSHajimu UMEMOTO break; 8219426aedfSHajimu UMEMOTO #endif 8229426aedfSHajimu UMEMOTO } 8239426aedfSHajimu UMEMOTO 8249426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 825a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 8269426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 8279426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 8289426aedfSHajimu UMEMOTO 8299426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 830a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 8319426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 8329426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 8339426aedfSHajimu UMEMOTO 8349426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 8359426aedfSHajimu UMEMOTO #ifdef INET 8369426aedfSHajimu UMEMOTO case AF_INET: 8379426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 8389426aedfSHajimu UMEMOTO break; 8399426aedfSHajimu UMEMOTO #endif 8409426aedfSHajimu UMEMOTO #ifdef INET6 8419426aedfSHajimu UMEMOTO case AF_INET6: 842a1f7e5f8SHajimu UMEMOTO /* 843a1f7e5f8SHajimu UMEMOTO * Check validity of the scope zone ID of the addresses, and 844a1f7e5f8SHajimu UMEMOTO * convert it into the kernel internal form if necessary. 845a1f7e5f8SHajimu UMEMOTO */ 846a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 847a1f7e5f8SHajimu UMEMOTO if (error != 0) 848a1f7e5f8SHajimu UMEMOTO break; 849a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 850a1f7e5f8SHajimu UMEMOTO if (error != 0) 851a1f7e5f8SHajimu UMEMOTO break; 8529426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 8539426aedfSHajimu UMEMOTO break; 8549426aedfSHajimu UMEMOTO #endif 8559426aedfSHajimu UMEMOTO } 8569426aedfSHajimu UMEMOTO if (error) { 8579426aedfSHajimu UMEMOTO /* rollback */ 8589426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 8599426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 8609426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 8619426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 8629426aedfSHajimu UMEMOTO goto bad; 8639426aedfSHajimu UMEMOTO } 8649426aedfSHajimu UMEMOTO 8659426aedfSHajimu UMEMOTO if (osrc) 8669426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 8679426aedfSHajimu UMEMOTO if (odst) 8689426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 8699426aedfSHajimu UMEMOTO 8709426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 87113f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 8729426aedfSHajimu UMEMOTO else 87313f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 8749426aedfSHajimu UMEMOTO splx(s); 8759426aedfSHajimu UMEMOTO 8769426aedfSHajimu UMEMOTO return 0; 8779426aedfSHajimu UMEMOTO 8789426aedfSHajimu UMEMOTO bad: 8799426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 88013f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 8819426aedfSHajimu UMEMOTO else 88213f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 8839426aedfSHajimu UMEMOTO splx(s); 8849426aedfSHajimu UMEMOTO 8859426aedfSHajimu UMEMOTO return error; 8869426aedfSHajimu UMEMOTO } 8879426aedfSHajimu UMEMOTO 8889426aedfSHajimu UMEMOTO void 8899426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp) 8909426aedfSHajimu UMEMOTO struct ifnet *ifp; 8919426aedfSHajimu UMEMOTO { 892fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 8939426aedfSHajimu UMEMOTO int s; 8949426aedfSHajimu UMEMOTO 8959426aedfSHajimu UMEMOTO s = splnet(); 89653dab5feSBrooks Davis 89753dab5feSBrooks Davis if (sc->gif_psrc) { 89853dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 89953dab5feSBrooks Davis sc->gif_psrc = NULL; 90053dab5feSBrooks Davis } 90153dab5feSBrooks Davis if (sc->gif_pdst) { 90253dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 90353dab5feSBrooks Davis sc->gif_pdst = NULL; 90453dab5feSBrooks Davis } 9059426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 9069426aedfSHajimu UMEMOTO #ifdef INET 9079426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 9089426aedfSHajimu UMEMOTO #endif 9099426aedfSHajimu UMEMOTO #ifdef INET6 9109426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 9119426aedfSHajimu UMEMOTO #endif 9129426aedfSHajimu UMEMOTO 9139426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 91413f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 9159426aedfSHajimu UMEMOTO else 91613f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 9179426aedfSHajimu UMEMOTO splx(s); 91853dab5feSBrooks Davis } 919