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" 35cfa1ca9dSYoshinobu Inoue 36cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 37cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 38*e3416ab0SBjoern A. Zeeb #include <sys/jail.h> 39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 40cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 41cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 425dba30f1SPoul-Henning Kamp #include <sys/module.h> 43cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 44cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h> 46cfa1ca9dSYoshinobu Inoue #include <sys/time.h> 47872f786aSBrooks Davis #include <sys/sysctl.h> 48cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h> 49dbe59260SHiroki Sato #include <sys/priv.h> 508b07e49aSJulian Elischer #include <sys/proc.h> 51686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h> 5253dab5feSBrooks Davis #include <sys/conf.h> 53cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 54cfa1ca9dSYoshinobu Inoue 55cfa1ca9dSYoshinobu Inoue #include <net/if.h> 56f889d2efSBrooks Davis #include <net/if_clone.h> 57cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 58cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 59cfa1ca9dSYoshinobu Inoue #include <net/route.h> 60cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 61530c0060SRobert Watson #include <net/vnet.h> 62cfa1ca9dSYoshinobu Inoue 63cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 64cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 65cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 6633841545SHajimu UMEMOTO #ifdef INET 6733841545SHajimu UMEMOTO #include <netinet/in_var.h> 68cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h> 6953dab5feSBrooks Davis #include <netinet/ip_var.h> 70cfa1ca9dSYoshinobu Inoue #endif /* INET */ 71cfa1ca9dSYoshinobu Inoue 72cfa1ca9dSYoshinobu Inoue #ifdef INET6 73cfa1ca9dSYoshinobu Inoue #ifndef INET 74cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 75cfa1ca9dSYoshinobu Inoue #endif 76cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 77cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 78cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 79a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 80cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 81686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 82cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 83cfa1ca9dSYoshinobu Inoue 84686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 8573ff045cSAndrew Thompson #include <net/ethernet.h> 8673ff045cSAndrew Thompson #include <net/if_bridgevar.h> 87cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 88cfa1ca9dSYoshinobu Inoue 89aed55708SRobert Watson #include <security/mac/mac_framework.h> 90aed55708SRobert Watson 9153dab5feSBrooks Davis #define GIFNAME "gif" 92686cdd19SJun-ichiro itojun Hagino 9317d5cb2dSRobert Watson /* 948c7e1947SRuslan Ermilov * gif_mtx protects the global gif_softc_list. 9517d5cb2dSRobert Watson */ 9617d5cb2dSRobert Watson static struct mtx gif_mtx; 9753dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 983e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 991e77c105SRobert Watson #define V_gif_softc_list VNET(gif_softc_list) 100eddfbb76SRobert Watson 10194408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10294408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10394408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10494408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10594408d94SBrooks Davis 10673ff045cSAndrew Thompson static void gif_start(struct ifnet *); 1076b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 108bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 109bfe1aba4SMarko Zec 110f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0); 11153dab5feSBrooks Davis 112929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 113cfa1ca9dSYoshinobu Inoue 114872f786aSBrooks Davis SYSCTL_DECL(_net_link); 115872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 116872f786aSBrooks Davis "Generic Tunnel Interface"); 117686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 118686cdd19SJun-ichiro itojun Hagino /* 119872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 120686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 121686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 122686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 123686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 124686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 125686cdd19SJun-ichiro itojun Hagino */ 126686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 127686cdd19SJun-ichiro itojun Hagino #endif 1283e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 129d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 130eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1328b615593SMarko Zec 133872f786aSBrooks Davis /* 134872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 135872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 136872f786aSBrooks Davis * we allow control over this check here. 137872f786aSBrooks Davis */ 138d0728d71SRobert Watson #ifdef XBONEHACK 1393e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1; 140d0728d71SRobert Watson #else 1413e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0; 142d0728d71SRobert Watson #endif 143d0728d71SRobert Watson #define V_parallel_tunnels VNET(parallel_tunnels) 144eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 145eddfbb76SRobert Watson &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?"); 146cfa1ca9dSYoshinobu Inoue 14756abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */ 14856abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 14956abdd33SAndrew Thompson { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 15056abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST 15156abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \ 15256abdd33SAndrew Thompson (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 15356abdd33SAndrew Thompson #endif 15456abdd33SAndrew Thompson 155bb2bfb4fSBrooks Davis static int 1566b7330e2SSam Leffler gif_clone_create(ifc, unit, params) 15753dab5feSBrooks Davis struct if_clone *ifc; 1583b16e7b2SMaxime Henrion int unit; 1596b7330e2SSam Leffler caddr_t params; 160cfa1ca9dSYoshinobu Inoue { 16133841545SHajimu UMEMOTO struct gif_softc *sc; 162cfa1ca9dSYoshinobu Inoue 163e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1648b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 165fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 166fc74a9f9SBrooks Davis if (GIF2IFP(sc) == NULL) { 167fc74a9f9SBrooks Davis free(sc, M_GIF); 168fc74a9f9SBrooks Davis return (ENOSPC); 169fc74a9f9SBrooks Davis } 17053dab5feSBrooks Davis 17125af0bb5SGleb Smirnoff GIF_LOCK_INIT(sc); 17225af0bb5SGleb Smirnoff 173fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 174fc74a9f9SBrooks Davis if_initname(GIF2IFP(sc), ifc->ifc_name, unit); 175686cdd19SJun-ichiro itojun Hagino 1769426aedfSHajimu UMEMOTO sc->encap_cookie4 = sc->encap_cookie6 = NULL; 177dbe59260SHiroki Sato sc->gif_options = GIF_ACCEPT_REVETHIP; 1789426aedfSHajimu UMEMOTO 179fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 180fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 181fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 18233841545SHajimu UMEMOTO #if 0 18333841545SHajimu UMEMOTO /* turn off ingress filter */ 184fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 18533841545SHajimu UMEMOTO #endif 186fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 18773ff045cSAndrew Thompson GIF2IFP(sc)->if_start = gif_start; 188fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 189e50d35e6SMaxim Sobolev GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen; 190fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 19101399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 19294408d94SBrooks Davis if (ng_gif_attach_p != NULL) 193fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 19425af0bb5SGleb Smirnoff 19525af0bb5SGleb Smirnoff mtx_lock(&gif_mtx); 196603724d3SBjoern A. Zeeb LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 19725af0bb5SGleb Smirnoff mtx_unlock(&gif_mtx); 19825af0bb5SGleb Smirnoff 19925af0bb5SGleb Smirnoff return (0); 200cfa1ca9dSYoshinobu Inoue } 201cfa1ca9dSYoshinobu Inoue 20217d5cb2dSRobert Watson static void 203febd0759SAndrew Thompson gif_clone_destroy(ifp) 204febd0759SAndrew Thompson struct ifnet *ifp; 20553dab5feSBrooks Davis { 206e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6) 20753dab5feSBrooks Davis int err; 208e0de57f9SBjoern A. Zeeb #endif 209febd0759SAndrew Thompson struct gif_softc *sc = ifp->if_softc; 210febd0759SAndrew Thompson 211febd0759SAndrew Thompson mtx_lock(&gif_mtx); 212febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 213febd0759SAndrew Thompson mtx_unlock(&gif_mtx); 21453dab5feSBrooks Davis 21517d5cb2dSRobert Watson gif_delete_tunnel(ifp); 2169426aedfSHajimu UMEMOTO #ifdef INET6 21753dab5feSBrooks Davis if (sc->encap_cookie6 != NULL) { 21853dab5feSBrooks Davis err = encap_detach(sc->encap_cookie6); 21953dab5feSBrooks Davis KASSERT(err == 0, ("Unexpected error detaching encap_cookie6")); 22053dab5feSBrooks Davis } 2219426aedfSHajimu UMEMOTO #endif 2229426aedfSHajimu UMEMOTO #ifdef INET 2239426aedfSHajimu UMEMOTO if (sc->encap_cookie4 != NULL) { 2249426aedfSHajimu UMEMOTO err = encap_detach(sc->encap_cookie4); 2259426aedfSHajimu UMEMOTO KASSERT(err == 0, ("Unexpected error detaching encap_cookie4")); 2269426aedfSHajimu UMEMOTO } 2279426aedfSHajimu UMEMOTO #endif 22853dab5feSBrooks Davis 22994408d94SBrooks Davis if (ng_gif_detach_p != NULL) 23094408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 23153dab5feSBrooks Davis bpfdetach(ifp); 23253dab5feSBrooks Davis if_detach(ifp); 233fc74a9f9SBrooks Davis if_free(ifp); 23453dab5feSBrooks Davis 23525af0bb5SGleb Smirnoff GIF_LOCK_DESTROY(sc); 23625af0bb5SGleb Smirnoff 23753dab5feSBrooks Davis free(sc, M_GIF); 23853dab5feSBrooks Davis } 23953dab5feSBrooks Davis 240d0728d71SRobert Watson static void 241d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2421ed81b73SMarko Zec { 2431ed81b73SMarko Zec 2441ed81b73SMarko Zec LIST_INIT(&V_gif_softc_list); 2451ed81b73SMarko Zec } 246d0728d71SRobert Watson VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init, 247d0728d71SRobert Watson NULL); 2481ed81b73SMarko Zec 2491ed81b73SMarko Zec static int 25053dab5feSBrooks Davis gifmodevent(mod, type, data) 25153dab5feSBrooks Davis module_t mod; 25253dab5feSBrooks Davis int type; 25353dab5feSBrooks Davis void *data; 25453dab5feSBrooks Davis { 25553dab5feSBrooks Davis 25653dab5feSBrooks Davis switch (type) { 25753dab5feSBrooks Davis case MOD_LOAD: 25817d5cb2dSRobert Watson mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF); 25944e33a07SMarko Zec if_clone_attach(&gif_cloner); 26053dab5feSBrooks Davis break; 261d0728d71SRobert Watson 26253dab5feSBrooks Davis case MOD_UNLOAD: 26353dab5feSBrooks Davis if_clone_detach(&gif_cloner); 26417d5cb2dSRobert Watson mtx_destroy(&gif_mtx); 26553dab5feSBrooks Davis break; 2663e019deaSPoul-Henning Kamp default: 2673e019deaSPoul-Henning Kamp return EOPNOTSUPP; 26853dab5feSBrooks Davis } 26953dab5feSBrooks Davis return 0; 27053dab5feSBrooks Davis } 27153dab5feSBrooks Davis 27253dab5feSBrooks Davis static moduledata_t gif_mod = { 27353dab5feSBrooks Davis "if_gif", 27453dab5feSBrooks Davis gifmodevent, 27553dab5feSBrooks Davis 0 27653dab5feSBrooks Davis }; 27753dab5feSBrooks Davis 27853dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27920af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 280cfa1ca9dSYoshinobu Inoue 2819426aedfSHajimu UMEMOTO int 282686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg) 283686cdd19SJun-ichiro itojun Hagino const struct mbuf *m; 284686cdd19SJun-ichiro itojun Hagino int off; 285686cdd19SJun-ichiro itojun Hagino int proto; 286686cdd19SJun-ichiro itojun Hagino void *arg; 287686cdd19SJun-ichiro itojun Hagino { 288686cdd19SJun-ichiro itojun Hagino struct ip ip; 289686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 290686cdd19SJun-ichiro itojun Hagino 291686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 292686cdd19SJun-ichiro itojun Hagino if (sc == NULL) 293686cdd19SJun-ichiro itojun Hagino return 0; 294686cdd19SJun-ichiro itojun Hagino 295fc74a9f9SBrooks Davis if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0) 296686cdd19SJun-ichiro itojun Hagino return 0; 297686cdd19SJun-ichiro itojun Hagino 298686cdd19SJun-ichiro itojun Hagino /* no physical address */ 299686cdd19SJun-ichiro itojun Hagino if (!sc->gif_psrc || !sc->gif_pdst) 300686cdd19SJun-ichiro itojun Hagino return 0; 301686cdd19SJun-ichiro itojun Hagino 302686cdd19SJun-ichiro itojun Hagino switch (proto) { 303686cdd19SJun-ichiro itojun Hagino #ifdef INET 304686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 305686cdd19SJun-ichiro itojun Hagino break; 306686cdd19SJun-ichiro itojun Hagino #endif 307686cdd19SJun-ichiro itojun Hagino #ifdef INET6 308686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 309686cdd19SJun-ichiro itojun Hagino break; 310686cdd19SJun-ichiro itojun Hagino #endif 31173ff045cSAndrew Thompson case IPPROTO_ETHERIP: 31273ff045cSAndrew Thompson break; 31373ff045cSAndrew Thompson 314686cdd19SJun-ichiro itojun Hagino default: 315686cdd19SJun-ichiro itojun Hagino return 0; 316686cdd19SJun-ichiro itojun Hagino } 317686cdd19SJun-ichiro itojun Hagino 3183bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3193bb61ca6SHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(ip)) 3203bb61ca6SHajimu UMEMOTO return 0; 3213bb61ca6SHajimu UMEMOTO 3226f4ded3aSBrooks Davis m_copydata(m, 0, sizeof(ip), (caddr_t)&ip); 323686cdd19SJun-ichiro itojun Hagino 324686cdd19SJun-ichiro itojun Hagino switch (ip.ip_v) { 325686cdd19SJun-ichiro itojun Hagino #ifdef INET 326686cdd19SJun-ichiro itojun Hagino case 4: 327686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET || 328686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET) 329686cdd19SJun-ichiro itojun Hagino return 0; 330686cdd19SJun-ichiro itojun Hagino return gif_encapcheck4(m, off, proto, arg); 331686cdd19SJun-ichiro itojun Hagino #endif 332686cdd19SJun-ichiro itojun Hagino #ifdef INET6 333686cdd19SJun-ichiro itojun Hagino case 6: 3349426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3359426aedfSHajimu UMEMOTO return 0; 336686cdd19SJun-ichiro itojun Hagino if (sc->gif_psrc->sa_family != AF_INET6 || 337686cdd19SJun-ichiro itojun Hagino sc->gif_pdst->sa_family != AF_INET6) 338686cdd19SJun-ichiro itojun Hagino return 0; 339686cdd19SJun-ichiro itojun Hagino return gif_encapcheck6(m, off, proto, arg); 340686cdd19SJun-ichiro itojun Hagino #endif 341686cdd19SJun-ichiro itojun Hagino default: 342686cdd19SJun-ichiro itojun Hagino return 0; 343686cdd19SJun-ichiro itojun Hagino } 344686cdd19SJun-ichiro itojun Hagino } 345686cdd19SJun-ichiro itojun Hagino 34673ff045cSAndrew Thompson static void 34773ff045cSAndrew Thompson gif_start(struct ifnet *ifp) 34873ff045cSAndrew Thompson { 34973ff045cSAndrew Thompson struct gif_softc *sc; 35073ff045cSAndrew Thompson struct mbuf *m; 35173ff045cSAndrew Thompson 35273ff045cSAndrew Thompson sc = ifp->if_softc; 35373ff045cSAndrew Thompson 35473ff045cSAndrew Thompson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 35573ff045cSAndrew Thompson for (;;) { 35673ff045cSAndrew Thompson IFQ_DEQUEUE(&ifp->if_snd, m); 35773ff045cSAndrew Thompson if (m == 0) 35873ff045cSAndrew Thompson break; 35973ff045cSAndrew Thompson 36073ff045cSAndrew Thompson gif_output(ifp, m, sc->gif_pdst, NULL); 36173ff045cSAndrew Thompson 36273ff045cSAndrew Thompson } 36373ff045cSAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 36473ff045cSAndrew Thompson 36573ff045cSAndrew Thompson return; 36673ff045cSAndrew Thompson } 36773ff045cSAndrew Thompson 368cfa1ca9dSYoshinobu Inoue int 369279aa3d4SKip Macy gif_output(ifp, m, dst, ro) 370cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 371cfa1ca9dSYoshinobu Inoue struct mbuf *m; 372cfa1ca9dSYoshinobu Inoue struct sockaddr *dst; 373279aa3d4SKip Macy struct route *ro; 374cfa1ca9dSYoshinobu Inoue { 375fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 3768c7e1947SRuslan Ermilov struct m_tag *mtag; 377cfa1ca9dSYoshinobu Inoue int error = 0; 3788c7e1947SRuslan Ermilov int gif_called; 37901399f34SDavid Malone u_int32_t af; 380cfa1ca9dSYoshinobu Inoue 38110722b85SRobert Watson #ifdef MAC 38230d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 383e0852ce2SRobert Watson if (error) { 384e0852ce2SRobert Watson m_freem(m); 385e0852ce2SRobert Watson goto end; 386e0852ce2SRobert Watson } 38710722b85SRobert Watson #endif 38810722b85SRobert Watson 389cfa1ca9dSYoshinobu Inoue /* 390cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 3918c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 3928c7e1947SRuslan Ermilov * 3938c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 394cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 395cfa1ca9dSYoshinobu Inoue */ 3968c7e1947SRuslan Ermilov gif_called = 1; 3978c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 3988c7e1947SRuslan Ermilov while (mtag != NULL) { 3998c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 4008c7e1947SRuslan Ermilov log(LOG_NOTICE, 4018c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 4028c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 4038c7e1947SRuslan Ermilov m_freem(m); 4048c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 4058c7e1947SRuslan Ermilov goto end; 4068c7e1947SRuslan Ermilov } 4078c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 4088c7e1947SRuslan Ermilov gif_called++; 4098c7e1947SRuslan Ermilov } 410603724d3SBjoern A. Zeeb if (gif_called > V_max_gif_nesting) { 411cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 412cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 413523ebc4eSRobert Watson gif_called); 414cfa1ca9dSYoshinobu Inoue m_freem(m); 415cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 416cfa1ca9dSYoshinobu Inoue goto end; 417cfa1ca9dSYoshinobu Inoue } 4188c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 4198c7e1947SRuslan Ermilov M_NOWAIT); 4208c7e1947SRuslan Ermilov if (mtag == NULL) { 4218c7e1947SRuslan Ermilov m_freem(m); 4228c7e1947SRuslan Ermilov error = ENOMEM; 4238c7e1947SRuslan Ermilov goto end; 4248c7e1947SRuslan Ermilov } 4258c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 4268c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 427686cdd19SJun-ichiro itojun Hagino 428cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 42925af0bb5SGleb Smirnoff 43025af0bb5SGleb Smirnoff GIF_LOCK(sc); 43125af0bb5SGleb Smirnoff 432cfa1ca9dSYoshinobu Inoue if (!(ifp->if_flags & IFF_UP) || 433cfa1ca9dSYoshinobu Inoue sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 4346e860629SGleb Smirnoff GIF_UNLOCK(sc); 435cfa1ca9dSYoshinobu Inoue m_freem(m); 436cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 437cfa1ca9dSYoshinobu Inoue goto end; 438cfa1ca9dSYoshinobu Inoue } 439cfa1ca9dSYoshinobu Inoue 44001399f34SDavid Malone /* BPF writes need to be handled specially. */ 44101399f34SDavid Malone if (dst->sa_family == AF_UNSPEC) { 44201399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 44301399f34SDavid Malone dst->sa_family = af; 44401399f34SDavid Malone } 44501399f34SDavid Malone 44601399f34SDavid Malone af = dst->sa_family; 44716d878ccSChristian S.J. Peron BPF_MTAP2(ifp, &af, sizeof(af), m); 448cfa1ca9dSYoshinobu Inoue ifp->if_opackets++; 449cfa1ca9dSYoshinobu Inoue ifp->if_obytes += m->m_pkthdr.len; 450cfa1ca9dSYoshinobu Inoue 45173ff045cSAndrew Thompson /* override to IPPROTO_ETHERIP for bridged traffic */ 45273ff045cSAndrew Thompson if (ifp->if_bridge) 45373ff045cSAndrew Thompson af = AF_LINK; 45473ff045cSAndrew Thompson 4558b07e49aSJulian Elischer M_SETFIB(m, sc->gif_fibnum); 45633841545SHajimu UMEMOTO /* inner AF-specific encapsulation */ 45733841545SHajimu UMEMOTO 458686cdd19SJun-ichiro itojun Hagino /* XXX should we check if our outer source is legal? */ 459686cdd19SJun-ichiro itojun Hagino 46033841545SHajimu UMEMOTO /* dispatch to output logic based on outer AF */ 461cfa1ca9dSYoshinobu Inoue switch (sc->gif_psrc->sa_family) { 462cfa1ca9dSYoshinobu Inoue #ifdef INET 463cfa1ca9dSYoshinobu Inoue case AF_INET: 46473ff045cSAndrew Thompson error = in_gif_output(ifp, af, m); 465cfa1ca9dSYoshinobu Inoue break; 466cfa1ca9dSYoshinobu Inoue #endif 467cfa1ca9dSYoshinobu Inoue #ifdef INET6 468cfa1ca9dSYoshinobu Inoue case AF_INET6: 46973ff045cSAndrew Thompson error = in6_gif_output(ifp, af, m); 470cfa1ca9dSYoshinobu Inoue break; 471cfa1ca9dSYoshinobu Inoue #endif 472cfa1ca9dSYoshinobu Inoue default: 473cfa1ca9dSYoshinobu Inoue m_freem(m); 474cfa1ca9dSYoshinobu Inoue error = ENETDOWN; 475cfa1ca9dSYoshinobu Inoue } 476cfa1ca9dSYoshinobu Inoue 4776e860629SGleb Smirnoff GIF_UNLOCK(sc); 478cfa1ca9dSYoshinobu Inoue end: 47933841545SHajimu UMEMOTO if (error) 48033841545SHajimu UMEMOTO ifp->if_oerrors++; 48125af0bb5SGleb Smirnoff return (error); 482cfa1ca9dSYoshinobu Inoue } 483cfa1ca9dSYoshinobu Inoue 484cfa1ca9dSYoshinobu Inoue void 48521fb391fSHajimu UMEMOTO gif_input(m, af, ifp) 486cfa1ca9dSYoshinobu Inoue struct mbuf *m; 487cfa1ca9dSYoshinobu Inoue int af; 48821fb391fSHajimu UMEMOTO struct ifnet *ifp; 489cfa1ca9dSYoshinobu Inoue { 49073ff045cSAndrew Thompson int isr, n; 4914382b068SChristian Brueffer struct gif_softc *sc; 49273ff045cSAndrew Thompson struct etherip_header *eip; 49356abdd33SAndrew Thompson struct ether_header *eh; 49456abdd33SAndrew Thompson struct ifnet *oldifp; 495cfa1ca9dSYoshinobu Inoue 49621fb391fSHajimu UMEMOTO if (ifp == NULL) { 497cfa1ca9dSYoshinobu Inoue /* just in case */ 498cfa1ca9dSYoshinobu Inoue m_freem(m); 499cfa1ca9dSYoshinobu Inoue return; 500cfa1ca9dSYoshinobu Inoue } 5014382b068SChristian Brueffer sc = ifp->if_softc; 50221fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 503cfa1ca9dSYoshinobu Inoue 50410722b85SRobert Watson #ifdef MAC 50530d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 50610722b85SRobert Watson #endif 50710722b85SRobert Watson 50816d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 50933841545SHajimu UMEMOTO u_int32_t af1 = af; 510437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 511cfa1ca9dSYoshinobu Inoue } 512cfa1ca9dSYoshinobu Inoue 51394408d94SBrooks Davis if (ng_gif_input_p != NULL) { 51421fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 51594408d94SBrooks Davis if (m == NULL) 51694408d94SBrooks Davis return; 51794408d94SBrooks Davis } 51894408d94SBrooks Davis 519cfa1ca9dSYoshinobu Inoue /* 520cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 521cfa1ca9dSYoshinobu Inoue * specified address family. 522cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 523cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 524cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 525cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 526cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 52788ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 52888ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 529cfa1ca9dSYoshinobu Inoue */ 530cfa1ca9dSYoshinobu Inoue switch (af) { 531cfa1ca9dSYoshinobu Inoue #ifdef INET 532cfa1ca9dSYoshinobu Inoue case AF_INET: 533cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 534cfa1ca9dSYoshinobu Inoue break; 535cfa1ca9dSYoshinobu Inoue #endif 536cfa1ca9dSYoshinobu Inoue #ifdef INET6 537cfa1ca9dSYoshinobu Inoue case AF_INET6: 538cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 539cfa1ca9dSYoshinobu Inoue break; 540cfa1ca9dSYoshinobu Inoue #endif 54173ff045cSAndrew Thompson case AF_LINK: 54273ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 54373ff045cSAndrew Thompson if (n > m->m_len) { 54473ff045cSAndrew Thompson m = m_pullup(m, n); 54573ff045cSAndrew Thompson if (m == NULL) { 54673ff045cSAndrew Thompson ifp->if_ierrors++; 54773ff045cSAndrew Thompson return; 54873ff045cSAndrew Thompson } 54973ff045cSAndrew Thompson } 55073ff045cSAndrew Thompson 55173ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 552dbe59260SHiroki Sato /* 553dbe59260SHiroki Sato * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 554dbe59260SHiroki Sato * accepts an EtherIP packet with revered version field in 555dbe59260SHiroki Sato * the header. This is a knob for backward compatibility 556dbe59260SHiroki Sato * with FreeBSD 7.2R or prior. 557dbe59260SHiroki Sato */ 558dbe59260SHiroki Sato if (sc->gif_options & GIF_ACCEPT_REVETHIP) { 559dbe59260SHiroki Sato if (eip->eip_resvl != ETHERIP_VERSION 560dbe59260SHiroki Sato && eip->eip_ver != ETHERIP_VERSION) { 56173ff045cSAndrew Thompson /* discard unknown versions */ 56273ff045cSAndrew Thompson m_freem(m); 56373ff045cSAndrew Thompson return; 56473ff045cSAndrew Thompson } 565dbe59260SHiroki Sato } else { 566dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 567dbe59260SHiroki Sato /* discard unknown versions */ 568dbe59260SHiroki Sato m_freem(m); 569dbe59260SHiroki Sato return; 570dbe59260SHiroki Sato } 571dbe59260SHiroki Sato } 57273ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 57373ff045cSAndrew Thompson 57473ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 57573ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 57673ff045cSAndrew Thompson 57756abdd33SAndrew Thompson if (ifp->if_bridge) { 57856abdd33SAndrew Thompson oldifp = ifp; 57956abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 58056abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 58156abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 58256abdd33SAndrew Thompson m->m_flags |= M_BCAST; 58356abdd33SAndrew Thompson else 58456abdd33SAndrew Thompson m->m_flags |= M_MCAST; 58556abdd33SAndrew Thompson ifp->if_imcasts++; 58656abdd33SAndrew Thompson } 58773ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 58873ff045cSAndrew Thompson 58956abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 59056abdd33SAndrew Thompson /* 59156abdd33SAndrew Thompson * The bridge gave us back itself or one of the 59256abdd33SAndrew Thompson * members for which the frame is addressed. 59356abdd33SAndrew Thompson */ 59456abdd33SAndrew Thompson ether_demux(ifp, m); 59556abdd33SAndrew Thompson return; 59656abdd33SAndrew Thompson } 59756abdd33SAndrew Thompson } 59873ff045cSAndrew Thompson if (m != NULL) 59973ff045cSAndrew Thompson m_freem(m); 60073ff045cSAndrew Thompson return; 60173ff045cSAndrew Thompson 602cfa1ca9dSYoshinobu Inoue default: 60394408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 60421fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 60594408d94SBrooks Davis else 606cfa1ca9dSYoshinobu Inoue m_freem(m); 607cfa1ca9dSYoshinobu Inoue return; 608cfa1ca9dSYoshinobu Inoue } 609cfa1ca9dSYoshinobu Inoue 61021fb391fSHajimu UMEMOTO ifp->if_ipackets++; 61121fb391fSHajimu UMEMOTO ifp->if_ibytes += m->m_pkthdr.len; 6121cafed39SJonathan Lemon netisr_dispatch(isr, m); 613cfa1ca9dSYoshinobu Inoue } 614cfa1ca9dSYoshinobu Inoue 615686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 616cfa1ca9dSYoshinobu Inoue int 617cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data) 618cfa1ca9dSYoshinobu Inoue struct ifnet *ifp; 619cfa1ca9dSYoshinobu Inoue u_long cmd; 620cfa1ca9dSYoshinobu Inoue caddr_t data; 621cfa1ca9dSYoshinobu Inoue { 622fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 623cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 624cfa1ca9dSYoshinobu Inoue int error = 0, size; 625dbe59260SHiroki Sato u_int options; 626686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 6273bb61ca6SHajimu UMEMOTO #ifdef SIOCSIFMTU /* xxx */ 6283bb61ca6SHajimu UMEMOTO u_long mtu; 6293bb61ca6SHajimu UMEMOTO #endif 630cfa1ca9dSYoshinobu Inoue 631cfa1ca9dSYoshinobu Inoue switch (cmd) { 632cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 6339426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 634cfa1ca9dSYoshinobu Inoue break; 635cfa1ca9dSYoshinobu Inoue 636cfa1ca9dSYoshinobu Inoue case SIOCSIFDSTADDR: 637cfa1ca9dSYoshinobu Inoue break; 638cfa1ca9dSYoshinobu Inoue 639cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 640cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 641cfa1ca9dSYoshinobu Inoue break; 642cfa1ca9dSYoshinobu Inoue 643686cdd19SJun-ichiro itojun Hagino #ifdef SIOCSIFMTU /* xxx */ 644cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 645cfa1ca9dSYoshinobu Inoue break; 646686cdd19SJun-ichiro itojun Hagino 647cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 648cfa1ca9dSYoshinobu Inoue mtu = ifr->ifr_mtu; 6493bb61ca6SHajimu UMEMOTO if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) 650cfa1ca9dSYoshinobu Inoue return (EINVAL); 651cfa1ca9dSYoshinobu Inoue ifp->if_mtu = mtu; 652cfa1ca9dSYoshinobu Inoue break; 653686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */ 654cfa1ca9dSYoshinobu Inoue 6553bb61ca6SHajimu UMEMOTO #ifdef INET 656cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 6573bb61ca6SHajimu UMEMOTO #endif 658cfa1ca9dSYoshinobu Inoue #ifdef INET6 659cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 660cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 66133841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 662686cdd19SJun-ichiro itojun Hagino switch (cmd) { 66333841545SHajimu UMEMOTO #ifdef INET 664686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 665cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 666cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 667cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 668cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 669cfa1ca9dSYoshinobu Inoue break; 67033841545SHajimu UMEMOTO #endif 671cfa1ca9dSYoshinobu Inoue #ifdef INET6 672686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 673cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 674cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 675cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 676cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 677686cdd19SJun-ichiro itojun Hagino break; 678686cdd19SJun-ichiro itojun Hagino #endif 67933841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 68033841545SHajimu UMEMOTO src = (struct sockaddr *) 68133841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 68233841545SHajimu UMEMOTO dst = (struct sockaddr *) 68333841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 6849426aedfSHajimu UMEMOTO break; 6856f4ded3aSBrooks Davis default: 6869426aedfSHajimu UMEMOTO return EINVAL; 68733841545SHajimu UMEMOTO } 68833841545SHajimu UMEMOTO 68933841545SHajimu UMEMOTO /* sa_family must be equal */ 69033841545SHajimu UMEMOTO if (src->sa_family != dst->sa_family) 69133841545SHajimu UMEMOTO return EINVAL; 69233841545SHajimu UMEMOTO 69333841545SHajimu UMEMOTO /* validate sa_len */ 69433841545SHajimu UMEMOTO switch (src->sa_family) { 69533841545SHajimu UMEMOTO #ifdef INET 69633841545SHajimu UMEMOTO case AF_INET: 69733841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 69833841545SHajimu UMEMOTO return EINVAL; 69933841545SHajimu UMEMOTO break; 70033841545SHajimu UMEMOTO #endif 70133841545SHajimu UMEMOTO #ifdef INET6 70233841545SHajimu UMEMOTO case AF_INET6: 70333841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 70433841545SHajimu UMEMOTO return EINVAL; 70533841545SHajimu UMEMOTO break; 70633841545SHajimu UMEMOTO #endif 70733841545SHajimu UMEMOTO default: 70833841545SHajimu UMEMOTO return EAFNOSUPPORT; 70933841545SHajimu UMEMOTO } 71033841545SHajimu UMEMOTO switch (dst->sa_family) { 71133841545SHajimu UMEMOTO #ifdef INET 71233841545SHajimu UMEMOTO case AF_INET: 71333841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in)) 71433841545SHajimu UMEMOTO return EINVAL; 71533841545SHajimu UMEMOTO break; 71633841545SHajimu UMEMOTO #endif 71733841545SHajimu UMEMOTO #ifdef INET6 71833841545SHajimu UMEMOTO case AF_INET6: 71933841545SHajimu UMEMOTO if (dst->sa_len != sizeof(struct sockaddr_in6)) 72033841545SHajimu UMEMOTO return EINVAL; 72133841545SHajimu UMEMOTO break; 72233841545SHajimu UMEMOTO #endif 72333841545SHajimu UMEMOTO default: 72433841545SHajimu UMEMOTO return EAFNOSUPPORT; 72533841545SHajimu UMEMOTO } 72633841545SHajimu UMEMOTO 72733841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 72833841545SHajimu UMEMOTO switch (cmd) { 72933841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 73033841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 73133841545SHajimu UMEMOTO break; 73233841545SHajimu UMEMOTO return EAFNOSUPPORT; 73333841545SHajimu UMEMOTO #ifdef INET6 73433841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 73533841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 73633841545SHajimu UMEMOTO break; 73733841545SHajimu UMEMOTO return EAFNOSUPPORT; 73833841545SHajimu UMEMOTO #endif /* INET6 */ 73933841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 74033841545SHajimu UMEMOTO /* checks done in the above */ 74133841545SHajimu UMEMOTO break; 742686cdd19SJun-ichiro itojun Hagino } 743cfa1ca9dSYoshinobu Inoue 744fc74a9f9SBrooks Davis error = gif_set_tunnel(GIF2IFP(sc), src, dst); 745cfa1ca9dSYoshinobu Inoue break; 746cfa1ca9dSYoshinobu Inoue 747686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR 748686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 749fc74a9f9SBrooks Davis gif_delete_tunnel(GIF2IFP(sc)); 750686cdd19SJun-ichiro itojun Hagino break; 751686cdd19SJun-ichiro itojun Hagino #endif 752686cdd19SJun-ichiro itojun Hagino 753cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 754cfa1ca9dSYoshinobu Inoue #ifdef INET6 755cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 756cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 757cfa1ca9dSYoshinobu Inoue if (sc->gif_psrc == NULL) { 758cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 759cfa1ca9dSYoshinobu Inoue goto bad; 760cfa1ca9dSYoshinobu Inoue } 761cfa1ca9dSYoshinobu Inoue src = sc->gif_psrc; 76233841545SHajimu UMEMOTO switch (cmd) { 763cfa1ca9dSYoshinobu Inoue #ifdef INET 76433841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 765cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 76633841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 767cfa1ca9dSYoshinobu Inoue break; 768cfa1ca9dSYoshinobu Inoue #endif /* INET */ 769cfa1ca9dSYoshinobu Inoue #ifdef INET6 77033841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 771cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 772cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 77333841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 774cfa1ca9dSYoshinobu Inoue break; 775cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 776cfa1ca9dSYoshinobu Inoue default: 777cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 778cfa1ca9dSYoshinobu Inoue goto bad; 779cfa1ca9dSYoshinobu Inoue } 78033841545SHajimu UMEMOTO if (src->sa_len > size) 78133841545SHajimu UMEMOTO return EINVAL; 78233841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 783a1f7e5f8SHajimu UMEMOTO #ifdef INET6 784a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 785a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 786a1f7e5f8SHajimu UMEMOTO if (error != 0) 787a1f7e5f8SHajimu UMEMOTO return (error); 788a1f7e5f8SHajimu UMEMOTO } 789a1f7e5f8SHajimu UMEMOTO #endif 790cfa1ca9dSYoshinobu Inoue break; 791cfa1ca9dSYoshinobu Inoue 792cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR: 793cfa1ca9dSYoshinobu Inoue #ifdef INET6 794cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 795cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 796cfa1ca9dSYoshinobu Inoue if (sc->gif_pdst == NULL) { 797cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 798cfa1ca9dSYoshinobu Inoue goto bad; 799cfa1ca9dSYoshinobu Inoue } 800cfa1ca9dSYoshinobu Inoue src = sc->gif_pdst; 80133841545SHajimu UMEMOTO switch (cmd) { 802cfa1ca9dSYoshinobu Inoue #ifdef INET 80333841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 804cfa1ca9dSYoshinobu Inoue dst = &ifr->ifr_addr; 80533841545SHajimu UMEMOTO size = sizeof(ifr->ifr_addr); 806cfa1ca9dSYoshinobu Inoue break; 807cfa1ca9dSYoshinobu Inoue #endif /* INET */ 808cfa1ca9dSYoshinobu Inoue #ifdef INET6 80933841545SHajimu UMEMOTO case SIOCGIFPDSTADDR_IN6: 810cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 811cfa1ca9dSYoshinobu Inoue &(((struct in6_ifreq *)data)->ifr_addr); 81233841545SHajimu UMEMOTO size = sizeof(((struct in6_ifreq *)data)->ifr_addr); 813cfa1ca9dSYoshinobu Inoue break; 814cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 815cfa1ca9dSYoshinobu Inoue default: 816cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 817cfa1ca9dSYoshinobu Inoue goto bad; 818cfa1ca9dSYoshinobu Inoue } 81933841545SHajimu UMEMOTO if (src->sa_len > size) 82033841545SHajimu UMEMOTO return EINVAL; 821*e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, src); 822*e3416ab0SBjoern A. Zeeb if (error != 0) 823*e3416ab0SBjoern A. Zeeb return (error); 824*e3416ab0SBjoern A. Zeeb error = prison_if(curthread->td_ucred, dst); 825*e3416ab0SBjoern A. Zeeb if (error != 0) 826*e3416ab0SBjoern A. Zeeb return (error); 82733841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 828a1f7e5f8SHajimu UMEMOTO #ifdef INET6 829a1f7e5f8SHajimu UMEMOTO if (dst->sa_family == AF_INET6) { 830a1f7e5f8SHajimu UMEMOTO error = sa6_recoverscope((struct sockaddr_in6 *)dst); 831a1f7e5f8SHajimu UMEMOTO if (error != 0) 832a1f7e5f8SHajimu UMEMOTO return (error); 833a1f7e5f8SHajimu UMEMOTO } 834a1f7e5f8SHajimu UMEMOTO #endif 83533841545SHajimu UMEMOTO break; 83633841545SHajimu UMEMOTO 83733841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 83833841545SHajimu UMEMOTO if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) { 83933841545SHajimu UMEMOTO error = EADDRNOTAVAIL; 84033841545SHajimu UMEMOTO goto bad; 84133841545SHajimu UMEMOTO } 84233841545SHajimu UMEMOTO 84333841545SHajimu UMEMOTO /* copy src */ 84433841545SHajimu UMEMOTO src = sc->gif_psrc; 84533841545SHajimu UMEMOTO dst = (struct sockaddr *) 84633841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->addr); 84733841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->addr); 84833841545SHajimu UMEMOTO if (src->sa_len > size) 84933841545SHajimu UMEMOTO return EINVAL; 85033841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 85133841545SHajimu UMEMOTO 85233841545SHajimu UMEMOTO /* copy dst */ 85333841545SHajimu UMEMOTO src = sc->gif_pdst; 85433841545SHajimu UMEMOTO dst = (struct sockaddr *) 85533841545SHajimu UMEMOTO &(((struct if_laddrreq *)data)->dstaddr); 85633841545SHajimu UMEMOTO size = sizeof(((struct if_laddrreq *)data)->dstaddr); 85733841545SHajimu UMEMOTO if (src->sa_len > size) 85833841545SHajimu UMEMOTO return EINVAL; 85933841545SHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)dst, src->sa_len); 860cfa1ca9dSYoshinobu Inoue break; 861cfa1ca9dSYoshinobu Inoue 862cfa1ca9dSYoshinobu Inoue case SIOCSIFFLAGS: 863686cdd19SJun-ichiro itojun Hagino /* if_ioctl() takes care of it */ 864cfa1ca9dSYoshinobu Inoue break; 865cfa1ca9dSYoshinobu Inoue 866dbe59260SHiroki Sato case GIFGOPTS: 867dbe59260SHiroki Sato options = sc->gif_options; 868dbe59260SHiroki Sato error = copyout(&options, ifr->ifr_data, 869dbe59260SHiroki Sato sizeof(options)); 870dbe59260SHiroki Sato break; 871dbe59260SHiroki Sato 872dbe59260SHiroki Sato case GIFSOPTS: 873dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 874dbe59260SHiroki Sato break; 8754cd5f57dSHiroki Sato error = copyin(ifr->ifr_data, &options, sizeof(options)); 8764cd5f57dSHiroki Sato if (error) 8774cd5f57dSHiroki Sato break; 8784cd5f57dSHiroki Sato if (options & ~GIF_OPTMASK) 879dbe59260SHiroki Sato error = EINVAL; 8804cd5f57dSHiroki Sato else 8814cd5f57dSHiroki Sato sc->gif_options = options; 882dbe59260SHiroki Sato break; 883dbe59260SHiroki Sato 884cfa1ca9dSYoshinobu Inoue default: 885cfa1ca9dSYoshinobu Inoue error = EINVAL; 886cfa1ca9dSYoshinobu Inoue break; 887cfa1ca9dSYoshinobu Inoue } 888cfa1ca9dSYoshinobu Inoue bad: 889cfa1ca9dSYoshinobu Inoue return error; 890cfa1ca9dSYoshinobu Inoue } 89153dab5feSBrooks Davis 89217d5cb2dSRobert Watson /* 89317d5cb2dSRobert Watson * XXXRW: There's a general event-ordering issue here: the code to check 89417d5cb2dSRobert Watson * if a given tunnel is already present happens before we perform a 89517d5cb2dSRobert Watson * potentially blocking setup of the tunnel. This code needs to be 89617d5cb2dSRobert Watson * re-ordered so that the check and replacement can be atomic using 89717d5cb2dSRobert Watson * a mutex. 89817d5cb2dSRobert Watson */ 8999426aedfSHajimu UMEMOTO int 9009426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst) 9019426aedfSHajimu UMEMOTO struct ifnet *ifp; 9029426aedfSHajimu UMEMOTO struct sockaddr *src; 9039426aedfSHajimu UMEMOTO struct sockaddr *dst; 90453dab5feSBrooks Davis { 905fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 9069426aedfSHajimu UMEMOTO struct gif_softc *sc2; 9079426aedfSHajimu UMEMOTO struct sockaddr *osrc, *odst, *sa; 9089426aedfSHajimu UMEMOTO int error = 0; 9099426aedfSHajimu UMEMOTO 91017d5cb2dSRobert Watson mtx_lock(&gif_mtx); 911603724d3SBjoern A. Zeeb LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) { 9129426aedfSHajimu UMEMOTO if (sc2 == sc) 9139426aedfSHajimu UMEMOTO continue; 9149426aedfSHajimu UMEMOTO if (!sc2->gif_pdst || !sc2->gif_psrc) 9159426aedfSHajimu UMEMOTO continue; 9169426aedfSHajimu UMEMOTO if (sc2->gif_pdst->sa_family != dst->sa_family || 9179426aedfSHajimu UMEMOTO sc2->gif_pdst->sa_len != dst->sa_len || 9189426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_family != src->sa_family || 9199426aedfSHajimu UMEMOTO sc2->gif_psrc->sa_len != src->sa_len) 9209426aedfSHajimu UMEMOTO continue; 9219426aedfSHajimu UMEMOTO 9229426aedfSHajimu UMEMOTO /* 9239426aedfSHajimu UMEMOTO * Disallow parallel tunnels unless instructed 9249426aedfSHajimu UMEMOTO * otherwise. 9259426aedfSHajimu UMEMOTO */ 926603724d3SBjoern A. Zeeb if (!V_parallel_tunnels && 9279426aedfSHajimu UMEMOTO bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 && 9289426aedfSHajimu UMEMOTO bcmp(sc2->gif_psrc, src, src->sa_len) == 0) { 9299426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 93017d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 9319426aedfSHajimu UMEMOTO goto bad; 9329426aedfSHajimu UMEMOTO } 9339426aedfSHajimu UMEMOTO 9349426aedfSHajimu UMEMOTO /* XXX both end must be valid? (I mean, not 0.0.0.0) */ 9359426aedfSHajimu UMEMOTO } 93617d5cb2dSRobert Watson mtx_unlock(&gif_mtx); 9379426aedfSHajimu UMEMOTO 9389426aedfSHajimu UMEMOTO /* XXX we can detach from both, but be polite just in case */ 9399426aedfSHajimu UMEMOTO if (sc->gif_psrc) 9409426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 9419426aedfSHajimu UMEMOTO #ifdef INET 9429426aedfSHajimu UMEMOTO case AF_INET: 9439426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 9449426aedfSHajimu UMEMOTO break; 9459426aedfSHajimu UMEMOTO #endif 9469426aedfSHajimu UMEMOTO #ifdef INET6 9479426aedfSHajimu UMEMOTO case AF_INET6: 9489426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 9499426aedfSHajimu UMEMOTO break; 9509426aedfSHajimu UMEMOTO #endif 9519426aedfSHajimu UMEMOTO } 9529426aedfSHajimu UMEMOTO 9539426aedfSHajimu UMEMOTO osrc = sc->gif_psrc; 954a163d034SWarner Losh sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK); 9559426aedfSHajimu UMEMOTO bcopy((caddr_t)src, (caddr_t)sa, src->sa_len); 9569426aedfSHajimu UMEMOTO sc->gif_psrc = sa; 9579426aedfSHajimu UMEMOTO 9589426aedfSHajimu UMEMOTO odst = sc->gif_pdst; 959a163d034SWarner Losh sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK); 9609426aedfSHajimu UMEMOTO bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len); 9619426aedfSHajimu UMEMOTO sc->gif_pdst = sa; 9629426aedfSHajimu UMEMOTO 9639426aedfSHajimu UMEMOTO switch (sc->gif_psrc->sa_family) { 9649426aedfSHajimu UMEMOTO #ifdef INET 9659426aedfSHajimu UMEMOTO case AF_INET: 9669426aedfSHajimu UMEMOTO error = in_gif_attach(sc); 9679426aedfSHajimu UMEMOTO break; 9689426aedfSHajimu UMEMOTO #endif 9699426aedfSHajimu UMEMOTO #ifdef INET6 9709426aedfSHajimu UMEMOTO case AF_INET6: 971a1f7e5f8SHajimu UMEMOTO /* 972a1f7e5f8SHajimu UMEMOTO * Check validity of the scope zone ID of the addresses, and 973a1f7e5f8SHajimu UMEMOTO * convert it into the kernel internal form if necessary. 974a1f7e5f8SHajimu UMEMOTO */ 975a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0); 976a1f7e5f8SHajimu UMEMOTO if (error != 0) 977a1f7e5f8SHajimu UMEMOTO break; 978a1f7e5f8SHajimu UMEMOTO error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0); 979a1f7e5f8SHajimu UMEMOTO if (error != 0) 980a1f7e5f8SHajimu UMEMOTO break; 9819426aedfSHajimu UMEMOTO error = in6_gif_attach(sc); 9829426aedfSHajimu UMEMOTO break; 9839426aedfSHajimu UMEMOTO #endif 9849426aedfSHajimu UMEMOTO } 9859426aedfSHajimu UMEMOTO if (error) { 9869426aedfSHajimu UMEMOTO /* rollback */ 9879426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_psrc, M_IFADDR); 9889426aedfSHajimu UMEMOTO free((caddr_t)sc->gif_pdst, M_IFADDR); 9899426aedfSHajimu UMEMOTO sc->gif_psrc = osrc; 9909426aedfSHajimu UMEMOTO sc->gif_pdst = odst; 9919426aedfSHajimu UMEMOTO goto bad; 9929426aedfSHajimu UMEMOTO } 9939426aedfSHajimu UMEMOTO 9949426aedfSHajimu UMEMOTO if (osrc) 9959426aedfSHajimu UMEMOTO free((caddr_t)osrc, M_IFADDR); 9969426aedfSHajimu UMEMOTO if (odst) 9979426aedfSHajimu UMEMOTO free((caddr_t)odst, M_IFADDR); 9989426aedfSHajimu UMEMOTO 9999426aedfSHajimu UMEMOTO bad: 10009426aedfSHajimu UMEMOTO if (sc->gif_psrc && sc->gif_pdst) 100113f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 10029426aedfSHajimu UMEMOTO else 100313f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 10049426aedfSHajimu UMEMOTO 10059426aedfSHajimu UMEMOTO return error; 10069426aedfSHajimu UMEMOTO } 10079426aedfSHajimu UMEMOTO 10089426aedfSHajimu UMEMOTO void 10099426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp) 10109426aedfSHajimu UMEMOTO struct ifnet *ifp; 10119426aedfSHajimu UMEMOTO { 1012fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 101353dab5feSBrooks Davis 101453dab5feSBrooks Davis if (sc->gif_psrc) { 101553dab5feSBrooks Davis free((caddr_t)sc->gif_psrc, M_IFADDR); 101653dab5feSBrooks Davis sc->gif_psrc = NULL; 101753dab5feSBrooks Davis } 101853dab5feSBrooks Davis if (sc->gif_pdst) { 101953dab5feSBrooks Davis free((caddr_t)sc->gif_pdst, M_IFADDR); 102053dab5feSBrooks Davis sc->gif_pdst = NULL; 102153dab5feSBrooks Davis } 10229426aedfSHajimu UMEMOTO /* it is safe to detach from both */ 10239426aedfSHajimu UMEMOTO #ifdef INET 10249426aedfSHajimu UMEMOTO (void)in_gif_detach(sc); 10259426aedfSHajimu UMEMOTO #endif 10269426aedfSHajimu UMEMOTO #ifdef INET6 10279426aedfSHajimu UMEMOTO (void)in6_gif_detach(sc); 10289426aedfSHajimu UMEMOTO #endif 102913f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 103053dab5feSBrooks Davis } 1031