1c398230bSWarner Losh /*- 2cfa1ca9dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3cfa1ca9dSYoshinobu Inoue * All rights reserved. 4cfa1ca9dSYoshinobu Inoue * 5cfa1ca9dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 6cfa1ca9dSYoshinobu Inoue * modification, are permitted provided that the following conditions 7cfa1ca9dSYoshinobu Inoue * are met: 8cfa1ca9dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 9cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 10cfa1ca9dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 11cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 12cfa1ca9dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 13cfa1ca9dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 14cfa1ca9dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 15cfa1ca9dSYoshinobu Inoue * without specific prior written permission. 16cfa1ca9dSYoshinobu Inoue * 17cfa1ca9dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18cfa1ca9dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19cfa1ca9dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20cfa1ca9dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21cfa1ca9dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22cfa1ca9dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23cfa1ca9dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24cfa1ca9dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25cfa1ca9dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26cfa1ca9dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27cfa1ca9dSYoshinobu Inoue * SUCH DAMAGE. 280b9f5f8aSAndrey V. Elsukov * 290b9f5f8aSAndrey V. Elsukov * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ 30cfa1ca9dSYoshinobu Inoue */ 31cfa1ca9dSYoshinobu Inoue 320b9f5f8aSAndrey V. Elsukov #include <sys/cdefs.h> 330b9f5f8aSAndrey V. Elsukov __FBSDID("$FreeBSD$"); 340b9f5f8aSAndrey V. Elsukov 35cfa1ca9dSYoshinobu Inoue #include "opt_inet.h" 36cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 37cfa1ca9dSYoshinobu Inoue 38cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 39cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 40e3416ab0SBjoern A. Zeeb #include <sys/jail.h> 41cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 420b9f5f8aSAndrey V. Elsukov #include <sys/lock.h> 43cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 44cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 455dba30f1SPoul-Henning Kamp #include <sys/module.h> 460b9f5f8aSAndrey V. Elsukov #include <sys/rmlock.h> 47cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 48cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 490b9f5f8aSAndrey V. Elsukov #include <sys/sx.h> 50cfa1ca9dSYoshinobu Inoue #include <sys/errno.h> 51cfa1ca9dSYoshinobu Inoue #include <sys/time.h> 52872f786aSBrooks Davis #include <sys/sysctl.h> 53cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h> 54dbe59260SHiroki Sato #include <sys/priv.h> 558b07e49aSJulian Elischer #include <sys/proc.h> 56686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h> 5753dab5feSBrooks Davis #include <sys/conf.h> 58cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 59cfa1ca9dSYoshinobu Inoue 60cfa1ca9dSYoshinobu Inoue #include <net/if.h> 6176039bc8SGleb Smirnoff #include <net/if_var.h> 62f889d2efSBrooks Davis #include <net/if_clone.h> 63cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 64cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 65cfa1ca9dSYoshinobu Inoue #include <net/route.h> 66cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 67530c0060SRobert Watson #include <net/vnet.h> 68cfa1ca9dSYoshinobu Inoue 69cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 70cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 71cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 720b9f5f8aSAndrey V. Elsukov #include <netinet/ip_ecn.h> 7333841545SHajimu UMEMOTO #ifdef INET 7433841545SHajimu UMEMOTO #include <netinet/in_var.h> 7553dab5feSBrooks Davis #include <netinet/ip_var.h> 76cfa1ca9dSYoshinobu Inoue #endif /* INET */ 77cfa1ca9dSYoshinobu Inoue 78cfa1ca9dSYoshinobu Inoue #ifdef INET6 79cfa1ca9dSYoshinobu Inoue #ifndef INET 80cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 81cfa1ca9dSYoshinobu Inoue #endif 82cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 83cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 840b9f5f8aSAndrey V. Elsukov #include <netinet6/ip6_ecn.h> 85cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 86a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 87686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 88cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 89cfa1ca9dSYoshinobu Inoue 90686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 9173ff045cSAndrew Thompson #include <net/ethernet.h> 9273ff045cSAndrew Thompson #include <net/if_bridgevar.h> 93cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 94cfa1ca9dSYoshinobu Inoue 95aed55708SRobert Watson #include <security/mac/mac_framework.h> 96aed55708SRobert Watson 9742a58907SGleb Smirnoff static const char gifname[] = "gif"; 98686cdd19SJun-ichiro itojun Hagino 9917d5cb2dSRobert Watson /* 100a7f5886eSHiroki Sato * gif_mtx protects a per-vnet gif_softc_list. 10117d5cb2dSRobert Watson */ 102a7f5886eSHiroki Sato static VNET_DEFINE(struct mtx, gif_mtx); 103a7f5886eSHiroki Sato #define V_gif_mtx VNET(gif_mtx) 10453dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 1053e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 1061e77c105SRobert Watson #define V_gif_softc_list VNET(gif_softc_list) 1070b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx; 1080b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl"); 109eddfbb76SRobert Watson 110a7f5886eSHiroki Sato #define GIF_LIST_LOCK_INIT(x) mtx_init(&V_gif_mtx, "gif_mtx", \ 111a7f5886eSHiroki Sato NULL, MTX_DEF) 112a7f5886eSHiroki Sato #define GIF_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gif_mtx) 113a7f5886eSHiroki Sato #define GIF_LIST_LOCK(x) mtx_lock(&V_gif_mtx) 114a7f5886eSHiroki Sato #define GIF_LIST_UNLOCK(x) mtx_unlock(&V_gif_mtx) 115a7f5886eSHiroki Sato 11694408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 11794408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 11894408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 11994408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 12094408d94SBrooks Davis 1219c0265c6SAndrey V. Elsukov static int gif_check_nesting(struct ifnet *, struct mbuf *); 1220b9f5f8aSAndrey V. Elsukov static int gif_set_tunnel(struct ifnet *, struct sockaddr *, 1230b9f5f8aSAndrey V. Elsukov struct sockaddr *); 1240b9f5f8aSAndrey V. Elsukov static void gif_delete_tunnel(struct ifnet *); 1250b9f5f8aSAndrey V. Elsukov static int gif_ioctl(struct ifnet *, u_long, caddr_t); 1260b9f5f8aSAndrey V. Elsukov static int gif_transmit(struct ifnet *, struct mbuf *); 1270b9f5f8aSAndrey V. Elsukov static void gif_qflush(struct ifnet *); 1286b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 129bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 130a7f5886eSHiroki Sato static VNET_DEFINE(struct if_clone *, gif_cloner); 131a7f5886eSHiroki Sato #define V_gif_cloner VNET(gif_cloner) 13253dab5feSBrooks Davis 133929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 134cfa1ca9dSYoshinobu Inoue 135872f786aSBrooks Davis SYSCTL_DECL(_net_link); 1366472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 137872f786aSBrooks Davis "Generic Tunnel Interface"); 138686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 139686cdd19SJun-ichiro itojun Hagino /* 140872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 141686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 142686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 143686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 144686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 145686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 146686cdd19SJun-ichiro itojun Hagino */ 147686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 148686cdd19SJun-ichiro itojun Hagino #endif 1493e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 150d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 1516df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 152eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1538b615593SMarko Zec 154872f786aSBrooks Davis /* 155872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 156872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 157872f786aSBrooks Davis * we allow control over this check here. 158872f786aSBrooks Davis */ 159d0728d71SRobert Watson #ifdef XBONEHACK 1603e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1; 161d0728d71SRobert Watson #else 1623e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0; 163d0728d71SRobert Watson #endif 164d0728d71SRobert Watson #define V_parallel_tunnels VNET(parallel_tunnels) 1656df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, 1666df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(parallel_tunnels), 0, 1676df8a710SGleb Smirnoff "Allow parallel tunnels?"); 168cfa1ca9dSYoshinobu Inoue 16956abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */ 17056abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 17156abdd33SAndrew Thompson { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 17256abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST 17356abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \ 17456abdd33SAndrew Thompson (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 17556abdd33SAndrew Thompson #endif 17656abdd33SAndrew Thompson 177bb2bfb4fSBrooks Davis static int 178c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 179cfa1ca9dSYoshinobu Inoue { 18033841545SHajimu UMEMOTO struct gif_softc *sc; 181cfa1ca9dSYoshinobu Inoue 182e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1838b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 184fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 18525af0bb5SGleb Smirnoff GIF_LOCK_INIT(sc); 186fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 18742a58907SGleb Smirnoff if_initname(GIF2IFP(sc), gifname, unit); 188686cdd19SJun-ichiro itojun Hagino 189fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 190fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 191fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 19233841545SHajimu UMEMOTO #if 0 19333841545SHajimu UMEMOTO /* turn off ingress filter */ 194fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 19533841545SHajimu UMEMOTO #endif 196fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 1970b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_transmit = gif_transmit; 1980b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_qflush = gif_qflush; 199fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 200fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 20101399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 20294408d94SBrooks Davis if (ng_gif_attach_p != NULL) 203fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 20425af0bb5SGleb Smirnoff 205a7f5886eSHiroki Sato GIF_LIST_LOCK(); 206603724d3SBjoern A. Zeeb LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 207a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 20825af0bb5SGleb Smirnoff return (0); 209cfa1ca9dSYoshinobu Inoue } 210cfa1ca9dSYoshinobu Inoue 21117d5cb2dSRobert Watson static void 212c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp) 21353dab5feSBrooks Davis { 2140b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 215febd0759SAndrew Thompson 2160b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 2170b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 2180b9f5f8aSAndrey V. Elsukov gif_delete_tunnel(ifp); 219a7f5886eSHiroki Sato GIF_LIST_LOCK(); 220febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 221a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 22294408d94SBrooks Davis if (ng_gif_detach_p != NULL) 22394408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 22453dab5feSBrooks Davis bpfdetach(ifp); 22553dab5feSBrooks Davis if_detach(ifp); 2260b9f5f8aSAndrey V. Elsukov ifp->if_softc = NULL; 2270b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 2280b9f5f8aSAndrey V. Elsukov 229fc74a9f9SBrooks Davis if_free(ifp); 23025af0bb5SGleb Smirnoff GIF_LOCK_DESTROY(sc); 23153dab5feSBrooks Davis free(sc, M_GIF); 23253dab5feSBrooks Davis } 23353dab5feSBrooks Davis 234d0728d71SRobert Watson static void 235d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2361ed81b73SMarko Zec { 2371ed81b73SMarko Zec 2381ed81b73SMarko Zec LIST_INIT(&V_gif_softc_list); 239a7f5886eSHiroki Sato GIF_LIST_LOCK_INIT(); 240a7f5886eSHiroki Sato V_gif_cloner = if_clone_simple(gifname, gif_clone_create, 241a7f5886eSHiroki Sato gif_clone_destroy, 0); 2421ed81b73SMarko Zec } 243a7f5886eSHiroki Sato VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 244a7f5886eSHiroki Sato vnet_gif_init, NULL); 245a7f5886eSHiroki Sato 246a7f5886eSHiroki Sato static void 247a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused) 248a7f5886eSHiroki Sato { 249a7f5886eSHiroki Sato 250a7f5886eSHiroki Sato if_clone_detach(V_gif_cloner); 251a7f5886eSHiroki Sato GIF_LIST_LOCK_DESTROY(); 252a7f5886eSHiroki Sato } 253a7f5886eSHiroki Sato VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 254a7f5886eSHiroki Sato vnet_gif_uninit, NULL); 2551ed81b73SMarko Zec 2561ed81b73SMarko Zec static int 257c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data) 25853dab5feSBrooks Davis { 25953dab5feSBrooks Davis 26053dab5feSBrooks Davis switch (type) { 26153dab5feSBrooks Davis case MOD_LOAD: 26253dab5feSBrooks Davis case MOD_UNLOAD: 26353dab5feSBrooks Davis break; 2643e019deaSPoul-Henning Kamp default: 265a7f5886eSHiroki Sato return (EOPNOTSUPP); 26653dab5feSBrooks Davis } 267a7f5886eSHiroki Sato return (0); 26853dab5feSBrooks Davis } 26953dab5feSBrooks Davis 27053dab5feSBrooks Davis static moduledata_t gif_mod = { 27153dab5feSBrooks Davis "if_gif", 27253dab5feSBrooks Davis gifmodevent, 2739823d527SKevin Lo 0 27453dab5feSBrooks Davis }; 27553dab5feSBrooks Davis 27653dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27720af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 278cfa1ca9dSYoshinobu Inoue 2799426aedfSHajimu UMEMOTO int 280c72a5d5dSAndrey V. Elsukov gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 281686cdd19SJun-ichiro itojun Hagino { 2820b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 283686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 2840b9f5f8aSAndrey V. Elsukov int ret; 2850b9f5f8aSAndrey V. Elsukov uint8_t ver; 286686cdd19SJun-ichiro itojun Hagino 287686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 2880b9f5f8aSAndrey V. Elsukov if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0) 2890b9f5f8aSAndrey V. Elsukov return (0); 290686cdd19SJun-ichiro itojun Hagino 2910b9f5f8aSAndrey V. Elsukov ret = 0; 2920b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 293686cdd19SJun-ichiro itojun Hagino 294686cdd19SJun-ichiro itojun Hagino /* no physical address */ 2950b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) 2960b9f5f8aSAndrey V. Elsukov goto done; 297686cdd19SJun-ichiro itojun Hagino 298686cdd19SJun-ichiro itojun Hagino switch (proto) { 299686cdd19SJun-ichiro itojun Hagino #ifdef INET 300686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 301686cdd19SJun-ichiro itojun Hagino #endif 302686cdd19SJun-ichiro itojun Hagino #ifdef INET6 303686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 304686cdd19SJun-ichiro itojun Hagino #endif 30573ff045cSAndrew Thompson case IPPROTO_ETHERIP: 30673ff045cSAndrew Thompson break; 307686cdd19SJun-ichiro itojun Hagino default: 3080b9f5f8aSAndrey V. Elsukov goto done; 309686cdd19SJun-ichiro itojun Hagino } 310686cdd19SJun-ichiro itojun Hagino 3113bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3120b9f5f8aSAndrey V. Elsukov if (m->m_pkthdr.len < sizeof(struct ip)) 3130b9f5f8aSAndrey V. Elsukov goto done; 3143bb61ca6SHajimu UMEMOTO 3150b9f5f8aSAndrey V. Elsukov m_copydata(m, 0, 1, &ver); 3160b9f5f8aSAndrey V. Elsukov switch (ver >> 4) { 317686cdd19SJun-ichiro itojun Hagino #ifdef INET 318686cdd19SJun-ichiro itojun Hagino case 4: 3190b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) 3200b9f5f8aSAndrey V. Elsukov goto done; 3210b9f5f8aSAndrey V. Elsukov ret = in_gif_encapcheck(m, off, proto, arg); 3220b9f5f8aSAndrey V. Elsukov break; 323686cdd19SJun-ichiro itojun Hagino #endif 324686cdd19SJun-ichiro itojun Hagino #ifdef INET6 325686cdd19SJun-ichiro itojun Hagino case 6: 3269426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3270b9f5f8aSAndrey V. Elsukov goto done; 3280b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) 3290b9f5f8aSAndrey V. Elsukov goto done; 3300b9f5f8aSAndrey V. Elsukov ret = in6_gif_encapcheck(m, off, proto, arg); 3310b9f5f8aSAndrey V. Elsukov break; 332686cdd19SJun-ichiro itojun Hagino #endif 333686cdd19SJun-ichiro itojun Hagino } 3340b9f5f8aSAndrey V. Elsukov done: 3350b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 3360b9f5f8aSAndrey V. Elsukov return (ret); 337686cdd19SJun-ichiro itojun Hagino } 338686cdd19SJun-ichiro itojun Hagino 3390b9f5f8aSAndrey V. Elsukov static int 3400b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m) 34173ff045cSAndrew Thompson { 34273ff045cSAndrew Thompson struct gif_softc *sc; 3430b9f5f8aSAndrey V. Elsukov struct etherip_header *eth; 344776b7288SRandall Stewart #ifdef INET 3450b9f5f8aSAndrey V. Elsukov struct ip *ip; 346776b7288SRandall Stewart #endif 347776b7288SRandall Stewart #ifdef INET6 3480b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 3490b9f5f8aSAndrey V. Elsukov uint32_t t; 350776b7288SRandall Stewart #endif 3510b9f5f8aSAndrey V. Elsukov uint32_t af; 3520b9f5f8aSAndrey V. Elsukov uint8_t proto, ecn; 3530b9f5f8aSAndrey V. Elsukov int error; 3540b9f5f8aSAndrey V. Elsukov 3559c0265c6SAndrey V. Elsukov #ifdef MAC 3569c0265c6SAndrey V. Elsukov error = mac_ifnet_check_transmit(ifp, m); 3579c0265c6SAndrey V. Elsukov if (error) { 3589c0265c6SAndrey V. Elsukov m_freem(m); 3599c0265c6SAndrey V. Elsukov goto err; 3609c0265c6SAndrey V. Elsukov } 3619c0265c6SAndrey V. Elsukov #endif 3620b9f5f8aSAndrey V. Elsukov error = ENETDOWN; 3630b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 3649c0265c6SAndrey V. Elsukov if ((ifp->if_flags & IFF_MONITOR) != 0 || 3659c0265c6SAndrey V. Elsukov (ifp->if_flags & IFF_UP) == 0 || 3669c0265c6SAndrey V. Elsukov sc->gif_family == 0 || 3679c0265c6SAndrey V. Elsukov (error = gif_check_nesting(ifp, m)) != 0) { 3680b9f5f8aSAndrey V. Elsukov m_freem(m); 3690b9f5f8aSAndrey V. Elsukov goto err; 3700b9f5f8aSAndrey V. Elsukov } 3710b9f5f8aSAndrey V. Elsukov /* Now pull back the af that we stashed in the csum_data. */ 3729c0265c6SAndrey V. Elsukov if (ifp->if_bridge) 3739c0265c6SAndrey V. Elsukov af = AF_LINK; 3749c0265c6SAndrey V. Elsukov else 375cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 3769c0265c6SAndrey V. Elsukov m->m_flags &= ~(M_BCAST|M_MCAST); 3779c0265c6SAndrey V. Elsukov M_SETFIB(m, sc->gif_fibnum); 378776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 3793751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3800b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 381776b7288SRandall Stewart /* inner AF-specific encapsulation */ 3820b9f5f8aSAndrey V. Elsukov ecn = 0; 3830b9f5f8aSAndrey V. Elsukov switch (af) { 384776b7288SRandall Stewart #ifdef INET 385776b7288SRandall Stewart case AF_INET: 3860b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV4; 3870b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 3880b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 3890b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3900b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3910b9f5f8aSAndrey V. Elsukov goto err; 3920b9f5f8aSAndrey V. Elsukov } 3930b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 3940b9f5f8aSAndrey V. Elsukov ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3950b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos); 396776b7288SRandall Stewart break; 397776b7288SRandall Stewart #endif 398776b7288SRandall Stewart #ifdef INET6 399776b7288SRandall Stewart case AF_INET6: 4000b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV6; 4010b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 4020b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 4030b9f5f8aSAndrey V. Elsukov if (m == NULL) { 4040b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 4050b9f5f8aSAndrey V. Elsukov goto err; 4060b9f5f8aSAndrey V. Elsukov } 4070b9f5f8aSAndrey V. Elsukov t = 0; 4080b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 4090b9f5f8aSAndrey V. Elsukov ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 4100b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow); 4110b9f5f8aSAndrey V. Elsukov ecn = (ntohl(t) >> 20) & 0xff; 4120b9f5f8aSAndrey V. Elsukov break; 4130b9f5f8aSAndrey V. Elsukov #endif 4140b9f5f8aSAndrey V. Elsukov case AF_LINK: 4150b9f5f8aSAndrey V. Elsukov proto = IPPROTO_ETHERIP; 4160b9f5f8aSAndrey V. Elsukov M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 4170b9f5f8aSAndrey V. Elsukov if (m == NULL) { 4180b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 4190b9f5f8aSAndrey V. Elsukov goto err; 4200b9f5f8aSAndrey V. Elsukov } 4210b9f5f8aSAndrey V. Elsukov eth = mtod(m, struct etherip_header *); 4220b9f5f8aSAndrey V. Elsukov eth->eip_resvh = 0; 4230b9f5f8aSAndrey V. Elsukov if ((sc->gif_options & GIF_SEND_REVETHIP) != 0) { 4240b9f5f8aSAndrey V. Elsukov eth->eip_ver = 0; 4250b9f5f8aSAndrey V. Elsukov eth->eip_resvl = ETHERIP_VERSION; 4260b9f5f8aSAndrey V. Elsukov } else { 4270b9f5f8aSAndrey V. Elsukov eth->eip_ver = ETHERIP_VERSION; 4280b9f5f8aSAndrey V. Elsukov eth->eip_resvl = 0; 4290b9f5f8aSAndrey V. Elsukov } 4300b9f5f8aSAndrey V. Elsukov break; 4310b9f5f8aSAndrey V. Elsukov default: 4320b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 4330b9f5f8aSAndrey V. Elsukov m_freem(m); 4340b9f5f8aSAndrey V. Elsukov goto err; 4350b9f5f8aSAndrey V. Elsukov } 4360b9f5f8aSAndrey V. Elsukov /* XXX should we check if our outer source is legal? */ 4370b9f5f8aSAndrey V. Elsukov /* dispatch to output logic based on outer AF */ 4380b9f5f8aSAndrey V. Elsukov switch (sc->gif_family) { 4390b9f5f8aSAndrey V. Elsukov #ifdef INET 4400b9f5f8aSAndrey V. Elsukov case AF_INET: 4410b9f5f8aSAndrey V. Elsukov error = in_gif_output(ifp, m, proto, ecn); 4420b9f5f8aSAndrey V. Elsukov break; 4430b9f5f8aSAndrey V. Elsukov #endif 4440b9f5f8aSAndrey V. Elsukov #ifdef INET6 4450b9f5f8aSAndrey V. Elsukov case AF_INET6: 4460b9f5f8aSAndrey V. Elsukov error = in6_gif_output(ifp, m, proto, ecn); 447776b7288SRandall Stewart break; 448776b7288SRandall Stewart #endif 449776b7288SRandall Stewart default: 450776b7288SRandall Stewart m_freem(m); 451776b7288SRandall Stewart } 4520b9f5f8aSAndrey V. Elsukov err: 453776b7288SRandall Stewart if (error) 4543751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 4550b9f5f8aSAndrey V. Elsukov return (error); 45673ff045cSAndrew Thompson } 4570b9f5f8aSAndrey V. Elsukov 4580b9f5f8aSAndrey V. Elsukov static void 4590b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused) 4600b9f5f8aSAndrey V. Elsukov { 4610b9f5f8aSAndrey V. Elsukov 46273ff045cSAndrew Thompson } 46373ff045cSAndrew Thompson 464c26230adSAndrey V. Elsukov #define MTAG_GIF 1080679712 465c26230adSAndrey V. Elsukov static int 466c26230adSAndrey V. Elsukov gif_check_nesting(struct ifnet *ifp, struct mbuf *m) 467c26230adSAndrey V. Elsukov { 468c26230adSAndrey V. Elsukov struct m_tag *mtag; 469c26230adSAndrey V. Elsukov int count; 470c26230adSAndrey V. Elsukov 471c26230adSAndrey V. Elsukov /* 472c26230adSAndrey V. Elsukov * gif may cause infinite recursion calls when misconfigured. 473c26230adSAndrey V. Elsukov * We'll prevent this by detecting loops. 474c26230adSAndrey V. Elsukov * 475c26230adSAndrey V. Elsukov * High nesting level may cause stack exhaustion. 476c26230adSAndrey V. Elsukov * We'll prevent this by introducing upper limit. 477c26230adSAndrey V. Elsukov */ 478c26230adSAndrey V. Elsukov count = 1; 479c26230adSAndrey V. Elsukov mtag = NULL; 480c26230adSAndrey V. Elsukov while ((mtag = m_tag_locate(m, MTAG_GIF, 0, mtag)) != NULL) { 481c26230adSAndrey V. Elsukov if (*(struct ifnet **)(mtag + 1) == ifp) { 4823a9f9af8SAndrey V. Elsukov log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 483c26230adSAndrey V. Elsukov return (EIO); 484c26230adSAndrey V. Elsukov } 485c26230adSAndrey V. Elsukov count++; 486c26230adSAndrey V. Elsukov } 487c26230adSAndrey V. Elsukov if (count > V_max_gif_nesting) { 488c26230adSAndrey V. Elsukov log(LOG_NOTICE, 489c26230adSAndrey V. Elsukov "%s: if_output recursively called too many times(%d)\n", 490c26230adSAndrey V. Elsukov if_name(ifp), count); 491c26230adSAndrey V. Elsukov return (EIO); 492c26230adSAndrey V. Elsukov } 493c26230adSAndrey V. Elsukov mtag = m_tag_alloc(MTAG_GIF, 0, sizeof(struct ifnet *), M_NOWAIT); 494c26230adSAndrey V. Elsukov if (mtag == NULL) 495c26230adSAndrey V. Elsukov return (ENOMEM); 496c26230adSAndrey V. Elsukov *(struct ifnet **)(mtag + 1) = ifp; 497c26230adSAndrey V. Elsukov m_tag_prepend(m, mtag); 498c26230adSAndrey V. Elsukov return (0); 499c26230adSAndrey V. Elsukov } 500c26230adSAndrey V. Elsukov 501cfa1ca9dSYoshinobu Inoue int 50247e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 50347e8d432SGleb Smirnoff struct route *ro) 504cfa1ca9dSYoshinobu Inoue { 505776b7288SRandall Stewart uint32_t af; 50610722b85SRobert Watson 50747e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 50801399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 50947e8d432SGleb Smirnoff else 51001399f34SDavid Malone af = dst->sa_family; 51140138788SRandall Stewart /* 5120b9f5f8aSAndrey V. Elsukov * Now save the af in the inbound pkt csum data, this is a cheat since 5130b9f5f8aSAndrey V. Elsukov * we are using the inbound csum_data field to carry the af over to 5140b9f5f8aSAndrey V. Elsukov * the gif_transmit() routine, avoiding using yet another mtag. 515776b7288SRandall Stewart */ 516cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 5170b9f5f8aSAndrey V. Elsukov return (ifp->if_transmit(ifp, m)); 518cfa1ca9dSYoshinobu Inoue } 519cfa1ca9dSYoshinobu Inoue 520cfa1ca9dSYoshinobu Inoue void 5210b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn) 522cfa1ca9dSYoshinobu Inoue { 52373ff045cSAndrew Thompson struct etherip_header *eip; 5240b9f5f8aSAndrey V. Elsukov #ifdef INET 5250b9f5f8aSAndrey V. Elsukov struct ip *ip; 5260b9f5f8aSAndrey V. Elsukov #endif 5270b9f5f8aSAndrey V. Elsukov #ifdef INET6 5280b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 5290b9f5f8aSAndrey V. Elsukov uint32_t t; 5300b9f5f8aSAndrey V. Elsukov #endif 5310b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 53256abdd33SAndrew Thompson struct ether_header *eh; 53356abdd33SAndrew Thompson struct ifnet *oldifp; 5340b9f5f8aSAndrey V. Elsukov uint32_t gif_options; 5350b9f5f8aSAndrey V. Elsukov int isr, n, af; 536cfa1ca9dSYoshinobu Inoue 53721fb391fSHajimu UMEMOTO if (ifp == NULL) { 538cfa1ca9dSYoshinobu Inoue /* just in case */ 539cfa1ca9dSYoshinobu Inoue m_freem(m); 540cfa1ca9dSYoshinobu Inoue return; 541cfa1ca9dSYoshinobu Inoue } 5424382b068SChristian Brueffer sc = ifp->if_softc; 5430b9f5f8aSAndrey V. Elsukov gif_options = sc->gif_options; 54421fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 5455b7a43f5SAndrey V. Elsukov m_clrprotoflags(m); 5460b9f5f8aSAndrey V. Elsukov switch (proto) { 5470b9f5f8aSAndrey V. Elsukov #ifdef INET 5480b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV4: 5490b9f5f8aSAndrey V. Elsukov af = AF_INET; 5500b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 5510b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 5520b9f5f8aSAndrey V. Elsukov if (m == NULL) 5530b9f5f8aSAndrey V. Elsukov goto drop; 5540b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 5550b9f5f8aSAndrey V. Elsukov if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 5560b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos) == 0) { 5570b9f5f8aSAndrey V. Elsukov m_freem(m); 5580b9f5f8aSAndrey V. Elsukov goto drop; 5590b9f5f8aSAndrey V. Elsukov } 5600b9f5f8aSAndrey V. Elsukov break; 5610b9f5f8aSAndrey V. Elsukov #endif 5620b9f5f8aSAndrey V. Elsukov #ifdef INET6 5630b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV6: 5640b9f5f8aSAndrey V. Elsukov af = AF_INET6; 5650b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 5660b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 5670b9f5f8aSAndrey V. Elsukov if (m == NULL) 5680b9f5f8aSAndrey V. Elsukov goto drop; 5690b9f5f8aSAndrey V. Elsukov t = htonl((uint32_t)ecn << 20); 5700b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5710b9f5f8aSAndrey V. Elsukov if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 5720b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow) == 0) { 5730b9f5f8aSAndrey V. Elsukov m_freem(m); 5740b9f5f8aSAndrey V. Elsukov goto drop; 5750b9f5f8aSAndrey V. Elsukov } 5760b9f5f8aSAndrey V. Elsukov break; 5770b9f5f8aSAndrey V. Elsukov #endif 5780b9f5f8aSAndrey V. Elsukov case IPPROTO_ETHERIP: 5790b9f5f8aSAndrey V. Elsukov af = AF_LINK; 5800b9f5f8aSAndrey V. Elsukov break; 5810b9f5f8aSAndrey V. Elsukov default: 5820b9f5f8aSAndrey V. Elsukov m_freem(m); 5830b9f5f8aSAndrey V. Elsukov goto drop; 5840b9f5f8aSAndrey V. Elsukov } 585cfa1ca9dSYoshinobu Inoue 58610722b85SRobert Watson #ifdef MAC 58730d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 58810722b85SRobert Watson #endif 58910722b85SRobert Watson 59016d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 5910b9f5f8aSAndrey V. Elsukov uint32_t af1 = af; 592437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 593cfa1ca9dSYoshinobu Inoue } 594cfa1ca9dSYoshinobu Inoue 595e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 5963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 5973751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 598e9f947e2SHiroki Sato m_freem(m); 599e9f947e2SHiroki Sato return; 600e9f947e2SHiroki Sato } 601e9f947e2SHiroki Sato 60294408d94SBrooks Davis if (ng_gif_input_p != NULL) { 60321fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 60494408d94SBrooks Davis if (m == NULL) 6050b9f5f8aSAndrey V. Elsukov goto drop; 60694408d94SBrooks Davis } 60794408d94SBrooks Davis 608cfa1ca9dSYoshinobu Inoue /* 609cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 610cfa1ca9dSYoshinobu Inoue * specified address family. 611cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 612cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 613cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 614cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 615cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 61688ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 61788ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 618cfa1ca9dSYoshinobu Inoue */ 619cfa1ca9dSYoshinobu Inoue switch (af) { 620cfa1ca9dSYoshinobu Inoue #ifdef INET 621cfa1ca9dSYoshinobu Inoue case AF_INET: 622cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 623cfa1ca9dSYoshinobu Inoue break; 624cfa1ca9dSYoshinobu Inoue #endif 625cfa1ca9dSYoshinobu Inoue #ifdef INET6 626cfa1ca9dSYoshinobu Inoue case AF_INET6: 627cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 628cfa1ca9dSYoshinobu Inoue break; 629cfa1ca9dSYoshinobu Inoue #endif 63073ff045cSAndrew Thompson case AF_LINK: 63173ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 6320b9f5f8aSAndrey V. Elsukov if (n > m->m_len) 63373ff045cSAndrew Thompson m = m_pullup(m, n); 6340b9f5f8aSAndrey V. Elsukov if (m == NULL) 6350b9f5f8aSAndrey V. Elsukov goto drop; 63673ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 637dbe59260SHiroki Sato /* 638dbe59260SHiroki Sato * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 639dbe59260SHiroki Sato * accepts an EtherIP packet with revered version field in 640dbe59260SHiroki Sato * the header. This is a knob for backward compatibility 641dbe59260SHiroki Sato * with FreeBSD 7.2R or prior. 642dbe59260SHiroki Sato */ 643dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 6440b9f5f8aSAndrey V. Elsukov if ((gif_options & GIF_ACCEPT_REVETHIP) == 0 || 6450b9f5f8aSAndrey V. Elsukov eip->eip_resvl != ETHERIP_VERSION) { 646dbe59260SHiroki Sato /* discard unknown versions */ 647dbe59260SHiroki Sato m_freem(m); 6480b9f5f8aSAndrey V. Elsukov goto drop; 649dbe59260SHiroki Sato } 650dbe59260SHiroki Sato } 65173ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 65273ff045cSAndrew Thompson 65373ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 65473ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 65573ff045cSAndrew Thompson 65656abdd33SAndrew Thompson if (ifp->if_bridge) { 65756abdd33SAndrew Thompson oldifp = ifp; 65856abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 65956abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 66056abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 66156abdd33SAndrew Thompson m->m_flags |= M_BCAST; 66256abdd33SAndrew Thompson else 66356abdd33SAndrew Thompson m->m_flags |= M_MCAST; 6643751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 66556abdd33SAndrew Thompson } 66673ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 66773ff045cSAndrew Thompson 66856abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 66956abdd33SAndrew Thompson /* 67056abdd33SAndrew Thompson * The bridge gave us back itself or one of the 67156abdd33SAndrew Thompson * members for which the frame is addressed. 67256abdd33SAndrew Thompson */ 67356abdd33SAndrew Thompson ether_demux(ifp, m); 67456abdd33SAndrew Thompson return; 67556abdd33SAndrew Thompson } 67656abdd33SAndrew Thompson } 67773ff045cSAndrew Thompson if (m != NULL) 67873ff045cSAndrew Thompson m_freem(m); 67973ff045cSAndrew Thompson return; 68073ff045cSAndrew Thompson 681cfa1ca9dSYoshinobu Inoue default: 68294408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 68321fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 68494408d94SBrooks Davis else 685cfa1ca9dSYoshinobu Inoue m_freem(m); 686cfa1ca9dSYoshinobu Inoue return; 687cfa1ca9dSYoshinobu Inoue } 688cfa1ca9dSYoshinobu Inoue 6893751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 6903751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 691a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 6921cafed39SJonathan Lemon netisr_dispatch(isr, m); 6930b9f5f8aSAndrey V. Elsukov return; 6940b9f5f8aSAndrey V. Elsukov drop: 6950b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 696cfa1ca9dSYoshinobu Inoue } 697cfa1ca9dSYoshinobu Inoue 698686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 699cfa1ca9dSYoshinobu Inoue int 700c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 701cfa1ca9dSYoshinobu Inoue { 7020b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 703cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 704686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 7050b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 7060b9f5f8aSAndrey V. Elsukov #ifdef INET 7070b9f5f8aSAndrey V. Elsukov struct sockaddr_in *sin = NULL; 7083bb61ca6SHajimu UMEMOTO #endif 7090b9f5f8aSAndrey V. Elsukov #ifdef INET6 7100b9f5f8aSAndrey V. Elsukov struct sockaddr_in6 *sin6 = NULL; 7110b9f5f8aSAndrey V. Elsukov #endif 7120b9f5f8aSAndrey V. Elsukov u_int options; 7130b9f5f8aSAndrey V. Elsukov int error; 714cfa1ca9dSYoshinobu Inoue 715cfa1ca9dSYoshinobu Inoue switch (cmd) { 716cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 7179426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 718cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 719cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 720cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 7210b9f5f8aSAndrey V. Elsukov case SIOCSIFFLAGS: 7220b9f5f8aSAndrey V. Elsukov return (0); 723cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 7240b9f5f8aSAndrey V. Elsukov if (ifr->ifr_mtu < GIF_MTU_MIN || 7250b9f5f8aSAndrey V. Elsukov ifr->ifr_mtu > GIF_MTU_MAX) 726cfa1ca9dSYoshinobu Inoue return (EINVAL); 7270b9f5f8aSAndrey V. Elsukov else 7280b9f5f8aSAndrey V. Elsukov ifp->if_mtu = ifr->ifr_mtu; 7290b9f5f8aSAndrey V. Elsukov return (0); 7300b9f5f8aSAndrey V. Elsukov } 7310b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 7320b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 7330b9f5f8aSAndrey V. Elsukov if (sc == NULL) { 7340b9f5f8aSAndrey V. Elsukov error = ENXIO; 7350b9f5f8aSAndrey V. Elsukov goto bad; 7360b9f5f8aSAndrey V. Elsukov } 7370b9f5f8aSAndrey V. Elsukov error = 0; 7380b9f5f8aSAndrey V. Elsukov switch (cmd) { 739cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 740245c40e8SAndrey V. Elsukov #ifdef INET6 741cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 742245c40e8SAndrey V. Elsukov #endif 7430b9f5f8aSAndrey V. Elsukov error = EINVAL; 744686cdd19SJun-ichiro itojun Hagino switch (cmd) { 74533841545SHajimu UMEMOTO #ifdef INET 746686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 747cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 748cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 749cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 750cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 751cfa1ca9dSYoshinobu Inoue break; 75233841545SHajimu UMEMOTO #endif 753cfa1ca9dSYoshinobu Inoue #ifdef INET6 754686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 755cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 756cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 757cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 758cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 759686cdd19SJun-ichiro itojun Hagino break; 760686cdd19SJun-ichiro itojun Hagino #endif 7616f4ded3aSBrooks Davis default: 7620b9f5f8aSAndrey V. Elsukov goto bad; 76333841545SHajimu UMEMOTO } 76433841545SHajimu UMEMOTO /* sa_family must be equal */ 7650b9f5f8aSAndrey V. Elsukov if (src->sa_family != dst->sa_family || 7660b9f5f8aSAndrey V. Elsukov src->sa_len != dst->sa_len) 7670b9f5f8aSAndrey V. Elsukov goto bad; 76833841545SHajimu UMEMOTO 76933841545SHajimu UMEMOTO /* validate sa_len */ 77033841545SHajimu UMEMOTO switch (src->sa_family) { 77133841545SHajimu UMEMOTO #ifdef INET 77233841545SHajimu UMEMOTO case AF_INET: 77333841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 7740b9f5f8aSAndrey V. Elsukov goto bad; 77533841545SHajimu UMEMOTO break; 77633841545SHajimu UMEMOTO #endif 77733841545SHajimu UMEMOTO #ifdef INET6 77833841545SHajimu UMEMOTO case AF_INET6: 77933841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 7800b9f5f8aSAndrey V. Elsukov goto bad; 78133841545SHajimu UMEMOTO break; 78233841545SHajimu UMEMOTO #endif 78333841545SHajimu UMEMOTO default: 7840b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 7850b9f5f8aSAndrey V. Elsukov goto bad; 78633841545SHajimu UMEMOTO } 78733841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 7880b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 78933841545SHajimu UMEMOTO switch (cmd) { 7900b9f5f8aSAndrey V. Elsukov #ifdef INET 79133841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 79233841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 79333841545SHajimu UMEMOTO break; 7940b9f5f8aSAndrey V. Elsukov goto bad; 7950b9f5f8aSAndrey V. Elsukov #endif 79633841545SHajimu UMEMOTO #ifdef INET6 79733841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 79833841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 79933841545SHajimu UMEMOTO break; 8000b9f5f8aSAndrey V. Elsukov goto bad; 8010b9f5f8aSAndrey V. Elsukov #endif 802686cdd19SJun-ichiro itojun Hagino } 8030b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 8040b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 8050b9f5f8aSAndrey V. Elsukov #ifdef INET 8060b9f5f8aSAndrey V. Elsukov case AF_INET: 8070b9f5f8aSAndrey V. Elsukov if (satosin(src)->sin_addr.s_addr == INADDR_ANY || 8080b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr == INADDR_ANY) 8090b9f5f8aSAndrey V. Elsukov goto bad; 810cfa1ca9dSYoshinobu Inoue break; 8110b9f5f8aSAndrey V. Elsukov #endif 8120b9f5f8aSAndrey V. Elsukov #ifdef INET6 8130b9f5f8aSAndrey V. Elsukov case AF_INET6: 8140b9f5f8aSAndrey V. Elsukov if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr) 8150b9f5f8aSAndrey V. Elsukov || 8160b9f5f8aSAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr)) 8170b9f5f8aSAndrey V. Elsukov goto bad; 8180b9f5f8aSAndrey V. Elsukov /* 8190b9f5f8aSAndrey V. Elsukov * Check validity of the scope zone ID of the 8200b9f5f8aSAndrey V. Elsukov * addresses, and convert it into the kernel 8210b9f5f8aSAndrey V. Elsukov * internal form if necessary. 8220b9f5f8aSAndrey V. Elsukov */ 8230b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(src), 0); 8240b9f5f8aSAndrey V. Elsukov if (error != 0) 8250b9f5f8aSAndrey V. Elsukov goto bad; 8260b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(dst), 0); 8270b9f5f8aSAndrey V. Elsukov if (error != 0) 8280b9f5f8aSAndrey V. Elsukov goto bad; 8290b9f5f8aSAndrey V. Elsukov #endif 8300b9f5f8aSAndrey V. Elsukov }; 8310b9f5f8aSAndrey V. Elsukov error = gif_set_tunnel(ifp, src, dst); 8320b9f5f8aSAndrey V. Elsukov break; 833686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 8340b9f5f8aSAndrey V. Elsukov gif_delete_tunnel(ifp); 835686cdd19SJun-ichiro itojun Hagino break; 836cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 8370b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 838245c40e8SAndrey V. Elsukov #ifdef INET6 839cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 8400b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 841245c40e8SAndrey V. Elsukov #endif 8420b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) { 843cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 8440b9f5f8aSAndrey V. Elsukov break; 845cfa1ca9dSYoshinobu Inoue } 8460b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 84733841545SHajimu UMEMOTO switch (cmd) { 848cfa1ca9dSYoshinobu Inoue #ifdef INET 84933841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 8500b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 8510b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) { 8520b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 853cfa1ca9dSYoshinobu Inoue break; 8540b9f5f8aSAndrey V. Elsukov } 8550b9f5f8aSAndrey V. Elsukov sin = (struct sockaddr_in *)&ifr->ifr_addr; 8560b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 8570b9f5f8aSAndrey V. Elsukov sin->sin_family = AF_INET; 8580b9f5f8aSAndrey V. Elsukov sin->sin_len = sizeof(*sin); 8590b9f5f8aSAndrey V. Elsukov break; 8600b9f5f8aSAndrey V. Elsukov #endif 861cfa1ca9dSYoshinobu Inoue #ifdef INET6 86233841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 863cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 8640b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) { 865cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 8660b9f5f8aSAndrey V. Elsukov break; 867cfa1ca9dSYoshinobu Inoue } 8680b9f5f8aSAndrey V. Elsukov sin6 = (struct sockaddr_in6 *) 8690b9f5f8aSAndrey V. Elsukov &(((struct in6_ifreq *)data)->ifr_addr); 8700b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 8710b9f5f8aSAndrey V. Elsukov sin6->sin6_family = AF_INET6; 8720b9f5f8aSAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 8730b9f5f8aSAndrey V. Elsukov break; 8740b9f5f8aSAndrey V. Elsukov #endif 8750b9f5f8aSAndrey V. Elsukov default: 8760b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 8770b9f5f8aSAndrey V. Elsukov } 8780b9f5f8aSAndrey V. Elsukov if (error == 0) { 87933841545SHajimu UMEMOTO switch (cmd) { 880cfa1ca9dSYoshinobu Inoue #ifdef INET 8810b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 8820b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_src; 8830b9f5f8aSAndrey V. Elsukov break; 88433841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 8850b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_dst; 886cfa1ca9dSYoshinobu Inoue break; 887a1f7e5f8SHajimu UMEMOTO #endif 8880b9f5f8aSAndrey V. Elsukov #ifdef INET6 8890b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 8900b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_src; 89133841545SHajimu UMEMOTO break; 8920b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 8930b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst; 894cfa1ca9dSYoshinobu Inoue break; 8950b9f5f8aSAndrey V. Elsukov #endif 8960b9f5f8aSAndrey V. Elsukov } 8970b9f5f8aSAndrey V. Elsukov } 8980b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 8990b9f5f8aSAndrey V. Elsukov if (error != 0) 9000b9f5f8aSAndrey V. Elsukov break; 9010b9f5f8aSAndrey V. Elsukov switch (cmd) { 9020b9f5f8aSAndrey V. Elsukov #ifdef INET 9030b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 9040b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 9050b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 9060b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin); 9070b9f5f8aSAndrey V. Elsukov if (error != 0) 9080b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 9090b9f5f8aSAndrey V. Elsukov break; 9100b9f5f8aSAndrey V. Elsukov #endif 9110b9f5f8aSAndrey V. Elsukov #ifdef INET6 9120b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 9130b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 9140b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 9150b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin6); 9160b9f5f8aSAndrey V. Elsukov if (error == 0) 9170b9f5f8aSAndrey V. Elsukov error = sa6_recoverscope(sin6); 9180b9f5f8aSAndrey V. Elsukov if (error != 0) 9190b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 920138d5655SAndrey V. Elsukov #endif 9210b9f5f8aSAndrey V. Elsukov } 9220b9f5f8aSAndrey V. Elsukov break; 923*eccfe69aSAndrey V. Elsukov case SIOCGTUNFIB: 924*eccfe69aSAndrey V. Elsukov ifr->ifr_fib = sc->gif_fibnum; 925*eccfe69aSAndrey V. Elsukov break; 926*eccfe69aSAndrey V. Elsukov case SIOCSTUNFIB: 927*eccfe69aSAndrey V. Elsukov if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 928*eccfe69aSAndrey V. Elsukov break; 929*eccfe69aSAndrey V. Elsukov if (ifr->ifr_fib >= rt_numfibs) 930*eccfe69aSAndrey V. Elsukov error = EINVAL; 931*eccfe69aSAndrey V. Elsukov else 932*eccfe69aSAndrey V. Elsukov sc->gif_fibnum = ifr->ifr_fib; 933*eccfe69aSAndrey V. Elsukov break; 934dbe59260SHiroki Sato case GIFGOPTS: 935dbe59260SHiroki Sato options = sc->gif_options; 9360b9f5f8aSAndrey V. Elsukov error = copyout(&options, ifr->ifr_data, sizeof(options)); 937dbe59260SHiroki Sato break; 938dbe59260SHiroki Sato case GIFSOPTS: 939dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 940dbe59260SHiroki Sato break; 9414cd5f57dSHiroki Sato error = copyin(ifr->ifr_data, &options, sizeof(options)); 9424cd5f57dSHiroki Sato if (error) 9434cd5f57dSHiroki Sato break; 9444cd5f57dSHiroki Sato if (options & ~GIF_OPTMASK) 945dbe59260SHiroki Sato error = EINVAL; 9464cd5f57dSHiroki Sato else 9474cd5f57dSHiroki Sato sc->gif_options = options; 948dbe59260SHiroki Sato break; 949cfa1ca9dSYoshinobu Inoue default: 950cfa1ca9dSYoshinobu Inoue error = EINVAL; 951cfa1ca9dSYoshinobu Inoue break; 952cfa1ca9dSYoshinobu Inoue } 953cfa1ca9dSYoshinobu Inoue bad: 9540b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 9550b9f5f8aSAndrey V. Elsukov return (error); 956cfa1ca9dSYoshinobu Inoue } 95753dab5feSBrooks Davis 9580b9f5f8aSAndrey V. Elsukov static void 9590b9f5f8aSAndrey V. Elsukov gif_detach(struct gif_softc *sc) 9600b9f5f8aSAndrey V. Elsukov { 9610b9f5f8aSAndrey V. Elsukov 9620b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 9630b9f5f8aSAndrey V. Elsukov if (sc->gif_ecookie != NULL) 9640b9f5f8aSAndrey V. Elsukov encap_detach(sc->gif_ecookie); 9650b9f5f8aSAndrey V. Elsukov sc->gif_ecookie = NULL; 9660b9f5f8aSAndrey V. Elsukov } 9670b9f5f8aSAndrey V. Elsukov 9680b9f5f8aSAndrey V. Elsukov static int 9690b9f5f8aSAndrey V. Elsukov gif_attach(struct gif_softc *sc, int af) 9700b9f5f8aSAndrey V. Elsukov { 9710b9f5f8aSAndrey V. Elsukov 9720b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 9730b9f5f8aSAndrey V. Elsukov switch (af) { 9740b9f5f8aSAndrey V. Elsukov #ifdef INET 9750b9f5f8aSAndrey V. Elsukov case AF_INET: 9760b9f5f8aSAndrey V. Elsukov return (in_gif_attach(sc)); 9770b9f5f8aSAndrey V. Elsukov #endif 9780b9f5f8aSAndrey V. Elsukov #ifdef INET6 9790b9f5f8aSAndrey V. Elsukov case AF_INET6: 9800b9f5f8aSAndrey V. Elsukov return (in6_gif_attach(sc)); 9810b9f5f8aSAndrey V. Elsukov #endif 9820b9f5f8aSAndrey V. Elsukov } 9830b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 9840b9f5f8aSAndrey V. Elsukov } 9850b9f5f8aSAndrey V. Elsukov 9860b9f5f8aSAndrey V. Elsukov static int 987c72a5d5dSAndrey V. Elsukov gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst) 98853dab5feSBrooks Davis { 989fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 9900b9f5f8aSAndrey V. Elsukov struct gif_softc *tsc; 9910b9f5f8aSAndrey V. Elsukov #ifdef INET 9920b9f5f8aSAndrey V. Elsukov struct ip *ip; 9930b9f5f8aSAndrey V. Elsukov #endif 9940b9f5f8aSAndrey V. Elsukov #ifdef INET6 9950b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 9960b9f5f8aSAndrey V. Elsukov #endif 9970b9f5f8aSAndrey V. Elsukov void *hdr; 9989426aedfSHajimu UMEMOTO int error = 0; 9999426aedfSHajimu UMEMOTO 10000b9f5f8aSAndrey V. Elsukov if (sc == NULL) 10010b9f5f8aSAndrey V. Elsukov return (ENXIO); 10020b9f5f8aSAndrey V. Elsukov /* Disallow parallel tunnels unless instructed otherwise. */ 10030b9f5f8aSAndrey V. Elsukov if (V_parallel_tunnels == 0) { 1004a7f5886eSHiroki Sato GIF_LIST_LOCK(); 10050b9f5f8aSAndrey V. Elsukov LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) { 10060b9f5f8aSAndrey V. Elsukov if (tsc == sc || tsc->gif_family != src->sa_family) 10079426aedfSHajimu UMEMOTO continue; 10080b9f5f8aSAndrey V. Elsukov #ifdef INET 10090b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET && 10100b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_src.s_addr == 10110b9f5f8aSAndrey V. Elsukov satosin(src)->sin_addr.s_addr && 10120b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_dst.s_addr == 10130b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr) { 10149426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 1015a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10169426aedfSHajimu UMEMOTO goto bad; 10179426aedfSHajimu UMEMOTO } 10180b9f5f8aSAndrey V. Elsukov #endif 10190b9f5f8aSAndrey V. Elsukov #ifdef INET6 10200b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET6 && 10210b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src, 10220b9f5f8aSAndrey V. Elsukov &satosin6(src)->sin6_addr) && 10230b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst, 10240b9f5f8aSAndrey V. Elsukov &satosin6(dst)->sin6_addr)) { 10250b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 1026a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10279426aedfSHajimu UMEMOTO goto bad; 10289426aedfSHajimu UMEMOTO } 10290b9f5f8aSAndrey V. Elsukov #endif 10300b9f5f8aSAndrey V. Elsukov } 10310b9f5f8aSAndrey V. Elsukov GIF_LIST_UNLOCK(); 10320b9f5f8aSAndrey V. Elsukov } 10330b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 10340b9f5f8aSAndrey V. Elsukov #ifdef INET 10350b9f5f8aSAndrey V. Elsukov case AF_INET: 10360b9f5f8aSAndrey V. Elsukov hdr = ip = malloc(sizeof(struct ip), M_GIF, 10370b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 10380b9f5f8aSAndrey V. Elsukov ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr; 10390b9f5f8aSAndrey V. Elsukov ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr; 10400b9f5f8aSAndrey V. Elsukov break; 10410b9f5f8aSAndrey V. Elsukov #endif 10420b9f5f8aSAndrey V. Elsukov #ifdef INET6 10430b9f5f8aSAndrey V. Elsukov case AF_INET6: 10440b9f5f8aSAndrey V. Elsukov hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF, 10450b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 10460b9f5f8aSAndrey V. Elsukov ip6->ip6_src = satosin6(src)->sin6_addr; 10470b9f5f8aSAndrey V. Elsukov ip6->ip6_dst = satosin6(dst)->sin6_addr; 10480b9f5f8aSAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION; 10490b9f5f8aSAndrey V. Elsukov break; 10500b9f5f8aSAndrey V. Elsukov #endif 10510b9f5f8aSAndrey V. Elsukov default: 10520b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 10530b9f5f8aSAndrey V. Elsukov }; 10549426aedfSHajimu UMEMOTO 10550b9f5f8aSAndrey V. Elsukov if (sc->gif_family != src->sa_family) 10560b9f5f8aSAndrey V. Elsukov gif_detach(sc); 10570b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0 || 10580b9f5f8aSAndrey V. Elsukov sc->gif_family != src->sa_family) 10590b9f5f8aSAndrey V. Elsukov error = gif_attach(sc, src->sa_family); 10609426aedfSHajimu UMEMOTO 10610b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 10620b9f5f8aSAndrey V. Elsukov if (sc->gif_family != 0) 10630b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 10640b9f5f8aSAndrey V. Elsukov sc->gif_family = src->sa_family; 10650b9f5f8aSAndrey V. Elsukov sc->gif_hdr = hdr; 10660b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 1067245c40e8SAndrey V. Elsukov #if defined(INET) || defined(INET6) 10689426aedfSHajimu UMEMOTO bad: 1069245c40e8SAndrey V. Elsukov #endif 10700b9f5f8aSAndrey V. Elsukov if (error == 0 && sc->gif_family != 0) 107113f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 10729426aedfSHajimu UMEMOTO else 107313f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 10740b9f5f8aSAndrey V. Elsukov return (error); 10759426aedfSHajimu UMEMOTO } 10769426aedfSHajimu UMEMOTO 10770b9f5f8aSAndrey V. Elsukov static void 1078c72a5d5dSAndrey V. Elsukov gif_delete_tunnel(struct ifnet *ifp) 10799426aedfSHajimu UMEMOTO { 1080fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 10810b9f5f8aSAndrey V. Elsukov int family; 108253dab5feSBrooks Davis 10830b9f5f8aSAndrey V. Elsukov if (sc == NULL) 10840b9f5f8aSAndrey V. Elsukov return; 10850b9f5f8aSAndrey V. Elsukov 10860b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 10870b9f5f8aSAndrey V. Elsukov family = sc->gif_family; 10880b9f5f8aSAndrey V. Elsukov sc->gif_family = 0; 10890b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 10900b9f5f8aSAndrey V. Elsukov if (family != 0) { 10910b9f5f8aSAndrey V. Elsukov gif_detach(sc); 10920b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 109353dab5feSBrooks Davis } 109413f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 109553dab5feSBrooks Davis } 1096