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> 8373ff045cSAndrew Thompson #include <net/ethernet.h> 8473ff045cSAndrew Thompson #include <net/if_bridgevar.h> 85cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 86cfa1ca9dSYoshinobu Inoue 87cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h> 88cfa1ca9dSYoshinobu Inoue 8953dab5feSBrooks Davis #define GIFNAME "gif" 90686cdd19SJun-ichiro itojun Hagino 9117d5cb2dSRobert Watson /* 928c7e1947SRuslan Ermilov * gif_mtx protects the global gif_softc_list. 9317d5cb2dSRobert Watson */ 9417d5cb2dSRobert Watson static struct mtx gif_mtx; 9553dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 96e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list; 9753dab5feSBrooks Davis 9894408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 9994408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10094408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10194408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10294408d94SBrooks Davis 10373ff045cSAndrew Thompson static void gif_start(struct ifnet *); 1046b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 105bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 10653dab5feSBrooks Davis 107f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0); 10853dab5feSBrooks Davis 109929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 110cfa1ca9dSYoshinobu Inoue 111872f786aSBrooks Davis SYSCTL_DECL(_net_link); 112872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 113872f786aSBrooks Davis "Generic Tunnel Interface"); 114686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 115686cdd19SJun-ichiro itojun Hagino /* 116872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 117686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 118686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 119686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 120686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 121686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 122686cdd19SJun-ichiro itojun Hagino */ 123686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 124686cdd19SJun-ichiro itojun Hagino #endif 125686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST; 126872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 127872f786aSBrooks Davis &max_gif_nesting, 0, "Max nested tunnels"); 128872f786aSBrooks Davis 129872f786aSBrooks Davis /* 130872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 131872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 132872f786aSBrooks Davis * we allow control over this check here. 133872f786aSBrooks Davis */ 134872f786aSBrooks Davis #ifdef XBONEHACK 135872f786aSBrooks Davis static int parallel_tunnels = 1; 136872f786aSBrooks Davis #else 137872f786aSBrooks Davis static int parallel_tunnels = 0; 138872f786aSBrooks Davis #endif 139872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 140872f786aSBrooks Davis ¶llel_tunnels, 0, "Allow parallel tunnels?"); 141cfa1ca9dSYoshinobu Inoue 142bb2bfb4fSBrooks Davis static int 1436b7330e2SSam Leffler gif_clone_create(ifc, unit, params) 14453dab5feSBrooks Davis struct if_clone *ifc; 1453b16e7b2SMaxime Henrion int unit; 1466b7330e2SSam Leffler caddr_t params; 147cfa1ca9dSYoshinobu Inoue { 14833841545SHajimu UMEMOTO struct gif_softc *sc; 149cfa1ca9dSYoshinobu Inoue 150e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 151fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 152fc74a9f9SBrooks Davis if (GIF2IFP(sc) == NULL) { 153fc74a9f9SBrooks Davis free(sc, M_GIF); 154fc74a9f9SBrooks Davis return (ENOSPC); 155fc74a9f9SBrooks Davis } 15653dab5feSBrooks Davis 15725af0bb5SGleb Smirnoff GIF_LOCK_INIT(sc); 15825af0bb5SGleb Smirnoff 159fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 160fc74a9f9SBrooks Davis if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 161686cdd19SJun-ichiro itojun Hagino 1629426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 1639426aedfSHajimu UMEMOTO 164fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 165fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 166fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 16733841545SHajimu UMEMOTO #if 0 16833841545SHajimu UMEMOTO /* turn off ingress filter */ 169fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 17033841545SHajimu UMEMOTO #endif 171fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 17273ff045cSAndrew Thompson GIF2IFP(sc)->if_start = gif_start; 173fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 174fc74a9f9SBrooks Davis GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN; 175fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 17601399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 17794408d94SBrooks Davis if (ng_gif_attach_p != NULL) 178fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 17925af0bb5SGleb Smirnoff 18025af0bb5SGleb Smirnoff mtx_lock(&gif_mtx); 18125af0bb5SGleb Smirnoff LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list); 18225af0bb5SGleb Smirnoff mtx_unlock(&gif_mtx); 18325af0bb5SGleb Smirnoff 18425af0bb5SGleb Smirnoff return (0); 185cfa1ca9dSYoshinobu Inoue } 186cfa1ca9dSYoshinobu Inoue 18717d5cb2dSRobert Watson static void 188febd0759SAndrew Thompson gif_clone_destroy(ifp) 189febd0759SAndrew Thompson struct ifnet *ifp; 19053dab5feSBrooks Davis { 19153dab5feSBrooks Davis int err; 192febd0759SAndrew Thompson struct gif_softc *sc = ifp->if_softc; 193febd0759SAndrew Thompson 194febd0759SAndrew Thompson mtx_lock(&gif_mtx); 195febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 196febd0759SAndrew Thompson mtx_unlock(&gif_mtx); 19753dab5feSBrooks Davis 19817d5cb2dSRobert Watson gif_delete_tunnel(ifp); 1999426aedfSHajimu UMEMOTO #ifdef INET6 20053dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 20153dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 20253dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 20353dab5feSBrooks Davis } 2049426aedfSHajimu UMEMOTO #endif 2059426aedfSHajimu UMEMOTO #ifdef INET 2069426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 2079426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 2089426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 2099426aedfSHajimu UMEMOTO } 2109426aedfSHajimu UMEMOTO #endif 21153dab5feSBrooks Davis 21294408d94SBrooks Davis if (ng_gif_detach_p != NULL) 21394408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 21453dab5feSBrooks Davis bpfdetach(ifp); 21553dab5feSBrooks Davis if_detach(ifp); 216fc74a9f9SBrooks Davis if_free(ifp); 21753dab5feSBrooks Davis 21825af0bb5SGleb Smirnoff GIF_LOCK_DESTROY(sc); 21925af0bb5SGleb Smirnoff 22053dab5feSBrooks Davis free(sc, M_GIF); 22153dab5feSBrooks Davis } 22253dab5feSBrooks Davis 22353dab5feSBrooks Davis static int 22453dab5feSBrooks Davis gifmodevent(mod, type, data) 22553dab5feSBrooks Davis module_t mod; 22653dab5feSBrooks Davis int type; 22753dab5feSBrooks Davis void *data; 22853dab5feSBrooks Davis { 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); 24317d5cb2dSRobert Watson mtx_destroy(&gif_mtx); 24453dab5feSBrooks Davis #ifdef INET6 24553dab5feSBrooks Davis ip6_gif_hlim = 0; 24653dab5feSBrooks Davis #endif 24753dab5feSBrooks Davis break; 2483e019deaSPoul-Henning Kamp default: 2493e019deaSPoul-Henning Kamp return EOPNOTSUPP; 25053dab5feSBrooks Davis } 25153dab5feSBrooks Davis return 0; 25253dab5feSBrooks Davis } 25353dab5feSBrooks Davis 25453dab5feSBrooks Davis static moduledata_t gif_mod = { 25553dab5feSBrooks Davis "if_gif", 25653dab5feSBrooks Davis gifmodevent, 25753dab5feSBrooks Davis 0 25853dab5feSBrooks Davis }; 25953dab5feSBrooks Davis 26053dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 26120af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 262cfa1ca9dSYoshinobu Inoue 2639426aedfSHajimu UMEMOTO int 264686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg) 265686cdd19SJun-ichiro itojun Hagino const struct mbuf *m; 266686cdd19SJun-ichiro itojun Hagino int off; 267686cdd19SJun-ichiro itojun Hagino int proto; 268686cdd19SJun-ichiro itojun Hagino void *arg; 269686cdd19SJun-ichiro itojun Hagino { 270686cdd19SJun-ichiro itojun Hagino struct ip ip; 271686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 272686cdd19SJun-ichiro itojun Hagino 273686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 274686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 275686cdd19SJun-ichiro itojun Hagino return 0; 276686cdd19SJun-ichiro itojun Hagino 277fc74a9f9SBrooks Davis if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 278686cdd19SJun-ichiro itojun Hagino return 0; 279686cdd19SJun-ichiro itojun Hagino 280686cdd19SJun-ichiro itojun Hagino /* no physical address */ 281686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 282686cdd19SJun-ichiro itojun Hagino return 0; 283686cdd19SJun-ichiro itojun Hagino 284686cdd19SJun-ichiro itojun Hagino switch (proto) { 285686cdd19SJun-ichiro itojun Hagino #ifdef INET 286686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 287686cdd19SJun-ichiro itojun Hagino break; 288686cdd19SJun-ichiro itojun Hagino #endif 289686cdd19SJun-ichiro itojun Hagino #ifdef INET6 290686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 291686cdd19SJun-ichiro itojun Hagino break; 292686cdd19SJun-ichiro itojun Hagino #endif 29373ff045cSAndrew Thompson case IPPROTO_ETHERIP: 29473ff045cSAndrew Thompson break; 29573ff045cSAndrew Thompson 296686cdd19SJun-ichiro itojun Hagino default: 297686cdd19SJun-ichiro itojun Hagino return 0; 298686cdd19SJun-ichiro itojun Hagino } 299686cdd19SJun-ichiro itojun Hagino 3003bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3013bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 3023bb61ca6SHajimu UMEMOTO return 0; 3033bb61ca6SHajimu UMEMOTO 3046f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 305686cdd19SJun-ichiro itojun Hagino 306686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 307686cdd19SJun-ichiro itojun Hagino #ifdef INET 308686cdd19SJun-ichiro itojun Hagino case 4: 309686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 310686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 311686cdd19SJun-ichiro itojun Hagino return 0; 312686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 313686cdd19SJun-ichiro itojun Hagino #endif 314686cdd19SJun-ichiro itojun Hagino #ifdef INET6 315686cdd19SJun-ichiro itojun Hagino case 6: 3169426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3179426aedfSHajimu UMEMOTO return 0; 318686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 319686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 320686cdd19SJun-ichiro itojun Hagino return 0; 321686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 322686cdd19SJun-ichiro itojun Hagino #endif 323686cdd19SJun-ichiro itojun Hagino default: 324686cdd19SJun-ichiro itojun Hagino return 0; 325686cdd19SJun-ichiro itojun Hagino } 326686cdd19SJun-ichiro itojun Hagino } 327686cdd19SJun-ichiro itojun Hagino 32873ff045cSAndrew Thompson static void 32973ff045cSAndrew Thompson gif_start(struct ifnet *ifp) 33073ff045cSAndrew Thompson { 33173ff045cSAndrew Thompson struct gif_softc *sc; 33273ff045cSAndrew Thompson struct mbuf *m; 33373ff045cSAndrew Thompson 33473ff045cSAndrew Thompson sc = ifp->if_softc; 33573ff045cSAndrew Thompson 33673ff045cSAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 33773ff045cSAndrew Thompson for (;;) { 33873ff045cSAndrew Thompson IFQ_DEQUEUE(&ifp->if_snd, m); 33973ff045cSAndrew Thompson if (m == 0) 34073ff045cSAndrew Thompson break; 34173ff045cSAndrew Thompson 34273ff045cSAndrew Thompson gif_output(ifp, m, sc->gif_pdst, NULL); 34373ff045cSAndrew Thompson 34473ff045cSAndrew Thompson } 34573ff045cSAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 34673ff045cSAndrew Thompson 34773ff045cSAndrew Thompson return; 34873ff045cSAndrew Thompson } 34973ff045cSAndrew Thompson 350cfa1ca9dSYoshinobu Inoue int 351cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt) 352cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 353cfa1ca9dSYoshinobu Inoue struct mbuf *m; 354cfa1ca9dSYoshinobu Inoue struct sockaddr *dst; 355cfa1ca9dSYoshinobu Inoue struct rtentry *rt; /* added in net2 */ 356cfa1ca9dSYoshinobu Inoue { 357fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 3588c7e1947SRuslan Ermilov struct m_tag *mtag; 359cfa1ca9dSYoshinobu Inoue int error = 0; 3608c7e1947SRuslan Ermilov int gif_called; 36101399f34SDavid Malone u_int32_t af; 362cfa1ca9dSYoshinobu Inoue 36310722b85SRobert Watson #ifdef MAC 36410722b85SRobert Watson error = mac_check_ifnet_transmit(ifp, m); 365e0852ce2SRobert Watson if (error) { 366e0852ce2SRobert Watson m_freem(m); 367e0852ce2SRobert Watson goto end; 368e0852ce2SRobert Watson } 36910722b85SRobert Watson #endif 37010722b85SRobert Watson 371cfa1ca9dSYoshinobu Inoue /* 372cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 3738c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 3748c7e1947SRuslan Ermilov * 3758c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 376cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 377cfa1ca9dSYoshinobu Inoue */ 3788c7e1947SRuslan Ermilov gif_called = 1; 3798c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 3808c7e1947SRuslan Ermilov while (mtag != NULL) { 3818c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 3828c7e1947SRuslan Ermilov log(LOG_NOTICE, 3838c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 3848c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 3858c7e1947SRuslan Ermilov m_freem(m); 3868c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 3878c7e1947SRuslan Ermilov goto end; 3888c7e1947SRuslan Ermilov } 3898c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 3908c7e1947SRuslan Ermilov gif_called++; 3918c7e1947SRuslan Ermilov } 3928c7e1947SRuslan Ermilov if (gif_called > max_gif_nesting) { 393cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 394cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 395523ebc4eSRobert Watson gif_called); 396cfa1ca9dSYoshinobu Inoue m_freem(m); 397cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 398cfa1ca9dSYoshinobu Inoue goto end; 399cfa1ca9dSYoshinobu Inoue } 4008c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 4018c7e1947SRuslan Ermilov M_NOWAIT); 4028c7e1947SRuslan Ermilov if (mtag == NULL) { 4038c7e1947SRuslan Ermilov m_freem(m); 4048c7e1947SRuslan Ermilov error = ENOMEM; 4058c7e1947SRuslan Ermilov goto end; 4068c7e1947SRuslan Ermilov } 4078c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 4088c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 409686cdd19SJun-ichiro itojun Hagino 410cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 41125af0bb5SGleb Smirnoff 41225af0bb5SGleb Smirnoff GIF_LOCK(sc); 41325af0bb5SGleb Smirnoff 414cfa1ca9dSYoshinobu Inoue if (!(ifp->if_flags & IFF_UP) || 415cfa1ca9dSYoshinobu Inoue sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 4166e860629SGleb Smirnoff GIF_UNLOCK(sc); 417cfa1ca9dSYoshinobu Inoue m_freem(m); 418cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 419cfa1ca9dSYoshinobu Inoue goto end; 420cfa1ca9dSYoshinobu Inoue } 421cfa1ca9dSYoshinobu Inoue 42201399f34SDavid Malone /* BPF writes need to be handled specially. */ 42301399f34SDavid Malone if (dst->sa_family == AF_UNSPEC) { 42401399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 42501399f34SDavid Malone dst->sa_family = af; 42601399f34SDavid Malone } 42701399f34SDavid Malone 42801399f34SDavid Malone af = dst->sa_family; 42916d878ccSChristian S.J. Peron BPF_MTAP2(ifp, &af, sizeof(af), m); 430cfa1ca9dSYoshinobu Inoue ifp->if_opackets++; 431cfa1ca9dSYoshinobu Inoue ifp->if_obytes += m->m_pkthdr.len; 432cfa1ca9dSYoshinobu Inoue 43373ff045cSAndrew Thompson /* override to IPPROTO_ETHERIP for bridged traffic */ 43473ff045cSAndrew Thompson if (ifp->if_bridge) 43573ff045cSAndrew Thompson af = AF_LINK; 43673ff045cSAndrew Thompson 43733841545SHajimu UMEMOTO /* inner AF-specific encapsulation */ 43833841545SHajimu UMEMOTO 439686cdd19SJun-ichiro itojun Hagino /* XXX should we check if our outer source is legal? */ 440686cdd19SJun-ichiro itojun Hagino 44133841545SHajimu UMEMOTO /* dispatch to output logic based on outer AF */ 442cfa1ca9dSYoshinobu Inoue switch (sc->gif_psrc->sa_family) { 443cfa1ca9dSYoshinobu Inoue #ifdef INET 444cfa1ca9dSYoshinobu Inoue case AF_INET: 44573ff045cSAndrew Thompson error = in_gif_output(ifp, af, m); 446cfa1ca9dSYoshinobu Inoue break; 447cfa1ca9dSYoshinobu Inoue #endif 448cfa1ca9dSYoshinobu Inoue #ifdef INET6 449cfa1ca9dSYoshinobu Inoue case AF_INET6: 45073ff045cSAndrew Thompson error = in6_gif_output(ifp, af, m); 451cfa1ca9dSYoshinobu Inoue break; 452cfa1ca9dSYoshinobu Inoue #endif 453cfa1ca9dSYoshinobu Inoue default: 454cfa1ca9dSYoshinobu Inoue m_freem(m); 455cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 456cfa1ca9dSYoshinobu Inoue } 457cfa1ca9dSYoshinobu Inoue 4586e860629SGleb Smirnoff GIF_UNLOCK(sc); 459cfa1ca9dSYoshinobu Inoue end: 46033841545SHajimu UMEMOTO if (error) 46133841545SHajimu UMEMOTO ifp->if_oerrors++; 46225af0bb5SGleb Smirnoff return (error); 463cfa1ca9dSYoshinobu Inoue } 464cfa1ca9dSYoshinobu Inoue 465cfa1ca9dSYoshinobu Inoue void 46621fb391fSHajimu UMEMOTO gif_input(m, af, ifp) 467cfa1ca9dSYoshinobu Inoue struct mbuf *m; 468cfa1ca9dSYoshinobu Inoue int af; 46921fb391fSHajimu UMEMOTO struct ifnet *ifp; 470cfa1ca9dSYoshinobu Inoue { 47173ff045cSAndrew Thompson int isr, n; 47273ff045cSAndrew Thompson struct etherip_header *eip; 473cfa1ca9dSYoshinobu Inoue 47421fb391fSHajimu UMEMOTO if (ifp == NULL) { 475cfa1ca9dSYoshinobu Inoue /* just in case */ 476cfa1ca9dSYoshinobu Inoue m_freem(m); 477cfa1ca9dSYoshinobu Inoue return; 478cfa1ca9dSYoshinobu Inoue } 479cfa1ca9dSYoshinobu Inoue 48021fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 481cfa1ca9dSYoshinobu Inoue 48210722b85SRobert Watson #ifdef MAC 48321fb391fSHajimu UMEMOTO mac_create_mbuf_from_ifnet(ifp, m); 48410722b85SRobert Watson #endif 48510722b85SRobert Watson 48616d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 48733841545SHajimu UMEMOTO u_int32_t af1 = af; 488437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 489cfa1ca9dSYoshinobu Inoue } 490cfa1ca9dSYoshinobu Inoue 49194408d94SBrooks Davis if (ng_gif_input_p != NULL) { 49221fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 49394408d94SBrooks Davis if (m == NULL) 49494408d94SBrooks Davis return; 49594408d94SBrooks Davis } 49694408d94SBrooks Davis 497cfa1ca9dSYoshinobu Inoue /* 498cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 499cfa1ca9dSYoshinobu Inoue * specified address family. 500cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 501cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 502cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 503cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 504cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 50588ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 50688ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 507cfa1ca9dSYoshinobu Inoue */ 508cfa1ca9dSYoshinobu Inoue switch (af) { 509cfa1ca9dSYoshinobu Inoue #ifdef INET 510cfa1ca9dSYoshinobu Inoue case AF_INET: 511cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 512cfa1ca9dSYoshinobu Inoue break; 513cfa1ca9dSYoshinobu Inoue #endif 514cfa1ca9dSYoshinobu Inoue #ifdef INET6 515cfa1ca9dSYoshinobu Inoue case AF_INET6: 516cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 517cfa1ca9dSYoshinobu Inoue break; 518cfa1ca9dSYoshinobu Inoue #endif 51973ff045cSAndrew Thompson case AF_LINK: 52073ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 52173ff045cSAndrew Thompson if (n > m->m_len) { 52273ff045cSAndrew Thompson m = m_pullup(m, n); 52373ff045cSAndrew Thompson if (m == NULL) { 52473ff045cSAndrew Thompson ifp->if_ierrors++; 52573ff045cSAndrew Thompson return; 52673ff045cSAndrew Thompson } 52773ff045cSAndrew Thompson } 52873ff045cSAndrew Thompson 52973ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 53073ff045cSAndrew Thompson if (eip->eip_ver != 53173ff045cSAndrew Thompson (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) { 53273ff045cSAndrew Thompson /* discard unknown versions */ 53373ff045cSAndrew Thompson m_freem(m); 53473ff045cSAndrew Thompson return; 53573ff045cSAndrew Thompson } 53673ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 53773ff045cSAndrew Thompson 53873ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 53973ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 54073ff045cSAndrew Thompson 54173ff045cSAndrew Thompson if (ifp->if_bridge) 54273ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 54373ff045cSAndrew Thompson 54473ff045cSAndrew Thompson if (m != NULL) 54573ff045cSAndrew Thompson m_freem(m); 54673ff045cSAndrew Thompson return; 54773ff045cSAndrew Thompson 548cfa1ca9dSYoshinobu Inoue default: 54994408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 55021fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 55194408d94SBrooks Davis else 552cfa1ca9dSYoshinobu Inoue m_freem(m); 553cfa1ca9dSYoshinobu Inoue return; 554cfa1ca9dSYoshinobu Inoue } 555cfa1ca9dSYoshinobu Inoue 55621fb391fSHajimu UMEMOTO ifp->if_ipackets++; 55721fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 5581cafed39SJonathan Lemon netisr_dispatch(isr, m); 559cfa1ca9dSYoshinobu Inoue } 560cfa1ca9dSYoshinobu Inoue 561686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 562cfa1ca9dSYoshinobu Inoue int 563cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data) 564cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 565cfa1ca9dSYoshinobu Inoue u_long cmd; 566cfa1ca9dSYoshinobu Inoue caddr_t data; 567cfa1ca9dSYoshinobu Inoue { 568fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 569cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 570cfa1ca9dSYoshinobu Inoue int error = 0, size; 571686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 5723bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 5733bb61ca6SHajimu UMEMOTO u_long mtu; 5743bb61ca6SHajimu UMEMOTO #endif 575cfa1ca9dSYoshinobu Inoue 576cfa1ca9dSYoshinobu Inoue switch (cmd) { 577cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 5789426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 579cfa1ca9dSYoshinobu Inoue break; 580cfa1ca9dSYoshinobu Inoue 581cfa1ca9dSYoshinobu Inoue case SIOCSIFDSTADDR: 582cfa1ca9dSYoshinobu Inoue break; 583cfa1ca9dSYoshinobu Inoue 584cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 585cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 586cfa1ca9dSYoshinobu Inoue break; 587cfa1ca9dSYoshinobu Inoue 588686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 589cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 590cfa1ca9dSYoshinobu Inoue break; 591686cdd19SJun-ichiro itojun Hagino 592cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 593cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 5943bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 595cfa1ca9dSYoshinobu Inoue return (EINVAL); 596cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 597cfa1ca9dSYoshinobu Inoue break; 598686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 599cfa1ca9dSYoshinobu Inoue 6003bb61ca6SHajimu UMEMOTO #ifdef INET 601cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 6023bb61ca6SHajimu UMEMOTO #endif 603cfa1ca9dSYoshinobu Inoue #ifdef INET6 604cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 605cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 60633841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 607686cdd19SJun-ichiro itojun Hagino switch (cmd) { 60833841545SHajimu UMEMOTO #ifdef INET 609686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 610cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 611cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 612cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 613cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 614cfa1ca9dSYoshinobu Inoue break; 61533841545SHajimu UMEMOTO #endif 616cfa1ca9dSYoshinobu Inoue #ifdef INET6 617686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 618cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 619cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 620cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 621cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 622686cdd19SJun-ichiro itojun Hagino break; 623686cdd19SJun-ichiro itojun Hagino #endif 62433841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 62533841545SHajimu UMEMOTO src = (struct sockaddr *) 62633841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 62733841545SHajimu UMEMOTO dst = (struct sockaddr *) 62833841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 6299426aedfSHajimu UMEMOTO break; 6306f4ded3aSBrooks Davis default: 6319426aedfSHajimu UMEMOTO return EINVAL; 63233841545SHajimu UMEMOTO } 63333841545SHajimu UMEMOTO 63433841545SHajimu UMEMOTO /* sa_family must be equal */ 63533841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 63633841545SHajimu UMEMOTO return EINVAL; 63733841545SHajimu UMEMOTO 63833841545SHajimu UMEMOTO /* validate sa_len */ 63933841545SHajimu UMEMOTO switch (src->sa_family) { 64033841545SHajimu UMEMOTO #ifdef INET 64133841545SHajimu UMEMOTO case AF_INET: 64233841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 64333841545SHajimu UMEMOTO return EINVAL; 64433841545SHajimu UMEMOTO break; 64533841545SHajimu UMEMOTO #endif 64633841545SHajimu UMEMOTO #ifdef INET6 64733841545SHajimu UMEMOTO case AF_INET6: 64833841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 64933841545SHajimu UMEMOTO return EINVAL; 65033841545SHajimu UMEMOTO break; 65133841545SHajimu UMEMOTO #endif 65233841545SHajimu UMEMOTO default: 65333841545SHajimu UMEMOTO return EAFNOSUPPORT; 65433841545SHajimu UMEMOTO } 65533841545SHajimu UMEMOTO switch (dst->sa_family) { 65633841545SHajimu UMEMOTO #ifdef INET 65733841545SHajimu UMEMOTO case AF_INET: 65833841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 65933841545SHajimu UMEMOTO return EINVAL; 66033841545SHajimu UMEMOTO break; 66133841545SHajimu UMEMOTO #endif 66233841545SHajimu UMEMOTO #ifdef INET6 66333841545SHajimu UMEMOTO case AF_INET6: 66433841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 66533841545SHajimu UMEMOTO return EINVAL; 66633841545SHajimu UMEMOTO break; 66733841545SHajimu UMEMOTO #endif 66833841545SHajimu UMEMOTO default: 66933841545SHajimu UMEMOTO return EAFNOSUPPORT; 67033841545SHajimu UMEMOTO } 67133841545SHajimu UMEMOTO 67233841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 67333841545SHajimu UMEMOTO switch (cmd) { 67433841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 67533841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 67633841545SHajimu UMEMOTO break; 67733841545SHajimu UMEMOTO return EAFNOSUPPORT; 67833841545SHajimu UMEMOTO #ifdef INET6 67933841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 68033841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 68133841545SHajimu UMEMOTO break; 68233841545SHajimu UMEMOTO return EAFNOSUPPORT; 68333841545SHajimu UMEMOTO #endif /* INET6 */ 68433841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 68533841545SHajimu UMEMOTO /* checks done in the above */ 68633841545SHajimu UMEMOTO break; 687686cdd19SJun-ichiro itojun Hagino } 688cfa1ca9dSYoshinobu Inoue 689fc74a9f9SBrooks Davis error = gif_set_tunnel(GIF2IFP(sc), src, dst); 690cfa1ca9dSYoshinobu Inoue break; 691cfa1ca9dSYoshinobu Inoue 692686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 693686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 694fc74a9f9SBrooks Davis gif_delete_tunnel(GIF2IFP(sc)); 695686cdd19SJun-ichiro itojun Hagino break; 696686cdd19SJun-ichiro itojun Hagino #endif 697686cdd19SJun-ichiro itojun Hagino 698cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 699cfa1ca9dSYoshinobu Inoue #ifdef INET6 700cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 701cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 702cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 703cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 704cfa1ca9dSYoshinobu Inoue goto bad; 705cfa1ca9dSYoshinobu Inoue } 706cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 70733841545SHajimu UMEMOTO switch (cmd) { 708cfa1ca9dSYoshinobu Inoue #ifdef INET 70933841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 710cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 71133841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 712cfa1ca9dSYoshinobu Inoue break; 713cfa1ca9dSYoshinobu Inoue #endif /* INET */ 714cfa1ca9dSYoshinobu Inoue #ifdef INET6 71533841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 716cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 717cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 71833841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 719cfa1ca9dSYoshinobu Inoue break; 720cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 721cfa1ca9dSYoshinobu Inoue default: 722cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 723cfa1ca9dSYoshinobu Inoue goto bad; 724cfa1ca9dSYoshinobu Inoue } 72533841545SHajimu UMEMOTO if (src->sa_len > size) 72633841545SHajimu UMEMOTO return EINVAL; 72733841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 728a1f7e5f8SHajimu UMEMOTO #ifdef INET6 729a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 730a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 731a1f7e5f8SHajimu UMEMOTO if (error != 0) 732a1f7e5f8SHajimu UMEMOTO return (error); 733a1f7e5f8SHajimu UMEMOTO } 734a1f7e5f8SHajimu UMEMOTO #endif 735cfa1ca9dSYoshinobu Inoue break; 736cfa1ca9dSYoshinobu Inoue 737cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 738cfa1ca9dSYoshinobu Inoue #ifdef INET6 739cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 740cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 741cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 742cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 743cfa1ca9dSYoshinobu Inoue goto bad; 744cfa1ca9dSYoshinobu Inoue } 745cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 74633841545SHajimu UMEMOTO switch (cmd) { 747cfa1ca9dSYoshinobu Inoue #ifdef INET 74833841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 749cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 75033841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 751cfa1ca9dSYoshinobu Inoue break; 752cfa1ca9dSYoshinobu Inoue #endif /* INET */ 753cfa1ca9dSYoshinobu Inoue #ifdef INET6 75433841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 755cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 756cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 75733841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 758cfa1ca9dSYoshinobu Inoue break; 759cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 760cfa1ca9dSYoshinobu Inoue default: 761cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 762cfa1ca9dSYoshinobu Inoue goto bad; 763cfa1ca9dSYoshinobu Inoue } 76433841545SHajimu UMEMOTO if (src->sa_len > size) 76533841545SHajimu UMEMOTO return EINVAL; 76633841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 767a1f7e5f8SHajimu UMEMOTO #ifdef INET6 768a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 769a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 770a1f7e5f8SHajimu UMEMOTO if (error != 0) 771a1f7e5f8SHajimu UMEMOTO return (error); 772a1f7e5f8SHajimu UMEMOTO } 773a1f7e5f8SHajimu UMEMOTO #endif 77433841545SHajimu UMEMOTO break; 77533841545SHajimu UMEMOTO 77633841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 77733841545SHajimu UMEMOTO if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 77833841545SHajimu UMEMOTO error = EADDRNOTAVAIL; 77933841545SHajimu UMEMOTO goto bad; 78033841545SHajimu UMEMOTO } 78133841545SHajimu UMEMOTO 78233841545SHajimu UMEMOTO /* copy src */ 78333841545SHajimu UMEMOTO src = sc->gif_psrc; 78433841545SHajimu UMEMOTO dst = (struct sockaddr *) 78533841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 78633841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->addr); 78733841545SHajimu UMEMOTO if (src->sa_len > size) 78833841545SHajimu UMEMOTO return EINVAL; 78933841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 79033841545SHajimu UMEMOTO 79133841545SHajimu UMEMOTO /* copy dst */ 79233841545SHajimu UMEMOTO src = sc->gif_pdst; 79333841545SHajimu UMEMOTO dst = (struct sockaddr *) 79433841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 79533841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->dstaddr); 79633841545SHajimu UMEMOTO if (src->sa_len > size) 79733841545SHajimu UMEMOTO return EINVAL; 79833841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 799cfa1ca9dSYoshinobu Inoue break; 800cfa1ca9dSYoshinobu Inoue 801cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 802686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 803cfa1ca9dSYoshinobu Inoue break; 804cfa1ca9dSYoshinobu Inoue 805cfa1ca9dSYoshinobu Inoue default: 806cfa1ca9dSYoshinobu Inoue error = EINVAL; 807cfa1ca9dSYoshinobu Inoue break; 808cfa1ca9dSYoshinobu Inoue } 809cfa1ca9dSYoshinobu Inoue bad: 810cfa1ca9dSYoshinobu Inoue return error; 811cfa1ca9dSYoshinobu Inoue } 81253dab5feSBrooks Davis 81317d5cb2dSRobert Watson /* 81417d5cb2dSRobert Watson * XXXRW: There's a general event-ordering issue here: the code to check 81517d5cb2dSRobert Watson * if a given tunnel is already present happens before we perform a 81617d5cb2dSRobert Watson * potentially blocking setup of the tunnel. This code needs to be 81717d5cb2dSRobert Watson * re-ordered so that the check and replacement can be atomic using 81817d5cb2dSRobert Watson * a mutex. 81917d5cb2dSRobert Watson */ 8209426aedfSHajimu UMEMOTO int 8219426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst) 8229426aedfSHajimu UMEMOTO struct ifnet *ifp; 8239426aedfSHajimu UMEMOTO struct sockaddr *src; 8249426aedfSHajimu UMEMOTO struct sockaddr *dst; 82553dab5feSBrooks Davis { 826fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 8279426aedfSHajimu UMEMOTO struct gif_softc *sc2; 8289426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 8299426aedfSHajimu UMEMOTO int error = 0; 8309426aedfSHajimu UMEMOTO 83117d5cb2dSRobert Watson mtx_lock(&gif_mtx); 8329426aedfSHajimu UMEMOTO LIST_FOREACH(sc2, &gif_softc_list, gif_list) { 8339426aedfSHajimu UMEMOTO if (sc2 == sc) 8349426aedfSHajimu UMEMOTO continue; 8359426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 8369426aedfSHajimu UMEMOTO continue; 8379426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 8389426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 8399426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 8409426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 8419426aedfSHajimu UMEMOTO continue; 8429426aedfSHajimu UMEMOTO 8439426aedfSHajimu UMEMOTO /* 8449426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 8459426aedfSHajimu UMEMOTO * otherwise. 8469426aedfSHajimu UMEMOTO */ 8479426aedfSHajimu UMEMOTO if (!parallel_tunnels && 8489426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 8499426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 8509426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 85117d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 8529426aedfSHajimu UMEMOTO goto bad; 8539426aedfSHajimu UMEMOTO } 8549426aedfSHajimu UMEMOTO 8559426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 8569426aedfSHajimu UMEMOTO } 85717d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 8589426aedfSHajimu UMEMOTO 8599426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 8609426aedfSHajimu UMEMOTO if (sc->gif_psrc) 8619426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 8629426aedfSHajimu UMEMOTO #ifdef INET 8639426aedfSHajimu UMEMOTO case AF_INET: 8649426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 8659426aedfSHajimu UMEMOTO break; 8669426aedfSHajimu UMEMOTO #endif 8679426aedfSHajimu UMEMOTO #ifdef INET6 8689426aedfSHajimu UMEMOTO case AF_INET6: 8699426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 8709426aedfSHajimu UMEMOTO break; 8719426aedfSHajimu UMEMOTO #endif 8729426aedfSHajimu UMEMOTO } 8739426aedfSHajimu UMEMOTO 8749426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 875a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 8769426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 8779426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 8789426aedfSHajimu UMEMOTO 8799426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 880a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 8819426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 8829426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 8839426aedfSHajimu UMEMOTO 8849426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 8859426aedfSHajimu UMEMOTO #ifdef INET 8869426aedfSHajimu UMEMOTO case AF_INET: 8879426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 8889426aedfSHajimu UMEMOTO break; 8899426aedfSHajimu UMEMOTO #endif 8909426aedfSHajimu UMEMOTO #ifdef INET6 8919426aedfSHajimu UMEMOTO case AF_INET6: 892a1f7e5f8SHajimu UMEMOTO /* 893a1f7e5f8SHajimu UMEMOTO * Check validity of the scope zone ID of the addresses, and 894a1f7e5f8SHajimu UMEMOTO * convert it into the kernel internal form if necessary. 895a1f7e5f8SHajimu UMEMOTO */ 896a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 897a1f7e5f8SHajimu UMEMOTO if (error != 0) 898a1f7e5f8SHajimu UMEMOTO break; 899a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 900a1f7e5f8SHajimu UMEMOTO if (error != 0) 901a1f7e5f8SHajimu UMEMOTO break; 9029426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 9039426aedfSHajimu UMEMOTO break; 9049426aedfSHajimu UMEMOTO #endif 9059426aedfSHajimu UMEMOTO } 9069426aedfSHajimu UMEMOTO if (error) { 9079426aedfSHajimu UMEMOTO /* rollback */ 9089426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 9099426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 9109426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 9119426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 9129426aedfSHajimu UMEMOTO goto bad; 9139426aedfSHajimu UMEMOTO } 9149426aedfSHajimu UMEMOTO 9159426aedfSHajimu UMEMOTO if (osrc) 9169426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 9179426aedfSHajimu UMEMOTO if (odst) 9189426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 9199426aedfSHajimu UMEMOTO 9209426aedfSHajimu UMEMOTO bad: 9219426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 92213f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 9239426aedfSHajimu UMEMOTO else 92413f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 9259426aedfSHajimu UMEMOTO 9269426aedfSHajimu UMEMOTO return error; 9279426aedfSHajimu UMEMOTO } 9289426aedfSHajimu UMEMOTO 9299426aedfSHajimu UMEMOTO void 9309426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp) 9319426aedfSHajimu UMEMOTO struct ifnet *ifp; 9329426aedfSHajimu UMEMOTO { 933fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 93453dab5feSBrooks Davis 93553dab5feSBrooks Davis if (sc->gif_psrc) { 93653dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 93753dab5feSBrooks Davis sc->gif_psrc = NULL; 93853dab5feSBrooks Davis } 93953dab5feSBrooks Davis if (sc->gif_pdst) { 94053dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 94153dab5feSBrooks Davis sc->gif_pdst = NULL; 94253dab5feSBrooks Davis } 9439426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 9449426aedfSHajimu UMEMOTO #ifdef INET 9459426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 9469426aedfSHajimu UMEMOTO #endif 9479426aedfSHajimu UMEMOTO #ifdef INET6 9489426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 9499426aedfSHajimu UMEMOTO #endif 95013f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 95153dab5feSBrooks Davis } 952