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> 75cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h> 7653dab5feSBrooks Davis #include <netinet/ip_var.h> 77cfa1ca9dSYoshinobu Inoue #endif /* INET */ 78cfa1ca9dSYoshinobu Inoue 79cfa1ca9dSYoshinobu Inoue #ifdef INET6 80cfa1ca9dSYoshinobu Inoue #ifndef INET 81cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 82cfa1ca9dSYoshinobu Inoue #endif 83cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 84cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 850b9f5f8aSAndrey V. Elsukov #include <netinet6/ip6_ecn.h> 86cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 87a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h> 88cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h> 89686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 90cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 91cfa1ca9dSYoshinobu Inoue 92686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 9373ff045cSAndrew Thompson #include <net/ethernet.h> 9473ff045cSAndrew Thompson #include <net/if_bridgevar.h> 95cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 96cfa1ca9dSYoshinobu Inoue 97aed55708SRobert Watson #include <security/mac/mac_framework.h> 98aed55708SRobert Watson 9942a58907SGleb Smirnoff static const char gifname[] = "gif"; 100686cdd19SJun-ichiro itojun Hagino 10117d5cb2dSRobert Watson /* 102a7f5886eSHiroki Sato * gif_mtx protects a per-vnet gif_softc_list. 10317d5cb2dSRobert Watson */ 104a7f5886eSHiroki Sato static VNET_DEFINE(struct mtx, gif_mtx); 105a7f5886eSHiroki Sato #define V_gif_mtx VNET(gif_mtx) 10653dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 1073e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list); 1081e77c105SRobert Watson #define V_gif_softc_list VNET(gif_softc_list) 1090b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx; 1100b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl"); 111eddfbb76SRobert Watson 112a7f5886eSHiroki Sato #define GIF_LIST_LOCK_INIT(x) mtx_init(&V_gif_mtx, "gif_mtx", \ 113a7f5886eSHiroki Sato NULL, MTX_DEF) 114a7f5886eSHiroki Sato #define GIF_LIST_LOCK_DESTROY(x) mtx_destroy(&V_gif_mtx) 115a7f5886eSHiroki Sato #define GIF_LIST_LOCK(x) mtx_lock(&V_gif_mtx) 116a7f5886eSHiroki Sato #define GIF_LIST_UNLOCK(x) mtx_unlock(&V_gif_mtx) 117a7f5886eSHiroki Sato 11894408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 11994408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 12094408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 12194408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 12294408d94SBrooks Davis 1230b9f5f8aSAndrey V. Elsukov static int gif_set_tunnel(struct ifnet *, struct sockaddr *, 1240b9f5f8aSAndrey V. Elsukov struct sockaddr *); 1250b9f5f8aSAndrey V. Elsukov static void gif_delete_tunnel(struct ifnet *); 1260b9f5f8aSAndrey V. Elsukov static int gif_ioctl(struct ifnet *, u_long, caddr_t); 1270b9f5f8aSAndrey V. Elsukov static int gif_transmit(struct ifnet *, struct mbuf *); 1280b9f5f8aSAndrey V. Elsukov static void gif_qflush(struct ifnet *); 1296b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 130bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 131a7f5886eSHiroki Sato static VNET_DEFINE(struct if_clone *, gif_cloner); 132a7f5886eSHiroki Sato #define V_gif_cloner VNET(gif_cloner) 13353dab5feSBrooks Davis 134929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *); 135cfa1ca9dSYoshinobu Inoue 136872f786aSBrooks Davis SYSCTL_DECL(_net_link); 1376472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0, 138872f786aSBrooks Davis "Generic Tunnel Interface"); 139686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 140686cdd19SJun-ichiro itojun Hagino /* 141872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 142686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 143686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 144686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 145686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 146686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 147686cdd19SJun-ichiro itojun Hagino */ 148686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 149686cdd19SJun-ichiro itojun Hagino #endif 1503e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST; 151d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 152*6df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 153eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1548b615593SMarko Zec 155872f786aSBrooks Davis /* 156872f786aSBrooks Davis * By default, we disallow creation of multiple tunnels between the same 157872f786aSBrooks Davis * pair of addresses. Some applications require this functionality so 158872f786aSBrooks Davis * we allow control over this check here. 159872f786aSBrooks Davis */ 160d0728d71SRobert Watson #ifdef XBONEHACK 1613e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1; 162d0728d71SRobert Watson #else 1633e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0; 164d0728d71SRobert Watson #endif 165d0728d71SRobert Watson #define V_parallel_tunnels VNET(parallel_tunnels) 166*6df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, 167*6df8a710SGleb Smirnoff CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(parallel_tunnels), 0, 168*6df8a710SGleb Smirnoff "Allow parallel tunnels?"); 169cfa1ca9dSYoshinobu Inoue 17056abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */ 17156abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] = 17256abdd33SAndrew Thompson { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 17356abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST 17456abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \ 17556abdd33SAndrew Thompson (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0) 17656abdd33SAndrew Thompson #endif 17756abdd33SAndrew Thompson 178bb2bfb4fSBrooks Davis static int 179c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 180cfa1ca9dSYoshinobu Inoue { 18133841545SHajimu UMEMOTO struct gif_softc *sc; 182cfa1ca9dSYoshinobu Inoue 183e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1848b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 185fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 18625af0bb5SGleb Smirnoff GIF_LOCK_INIT(sc); 187fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 18842a58907SGleb Smirnoff if_initname(GIF2IFP(sc), gifname, unit); 189686cdd19SJun-ichiro itojun Hagino 190fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 191fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 192fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 19333841545SHajimu UMEMOTO #if 0 19433841545SHajimu UMEMOTO /* turn off ingress filter */ 195fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags |= IFF_LINK2; 19633841545SHajimu UMEMOTO #endif 197fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 1980b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_transmit = gif_transmit; 1990b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_qflush = gif_qflush; 200fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 201fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 20201399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 20394408d94SBrooks Davis if (ng_gif_attach_p != NULL) 204fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 20525af0bb5SGleb Smirnoff 206a7f5886eSHiroki Sato GIF_LIST_LOCK(); 207603724d3SBjoern A. Zeeb LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list); 208a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 20925af0bb5SGleb Smirnoff return (0); 210cfa1ca9dSYoshinobu Inoue } 211cfa1ca9dSYoshinobu Inoue 21217d5cb2dSRobert Watson static void 213c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp) 21453dab5feSBrooks Davis { 2150b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 216febd0759SAndrew Thompson 2170b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 2180b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 2190b9f5f8aSAndrey V. Elsukov gif_delete_tunnel(ifp); 220a7f5886eSHiroki Sato GIF_LIST_LOCK(); 221febd0759SAndrew Thompson LIST_REMOVE(sc, gif_list); 222a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 22394408d94SBrooks Davis if (ng_gif_detach_p != NULL) 22494408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 22553dab5feSBrooks Davis bpfdetach(ifp); 22653dab5feSBrooks Davis if_detach(ifp); 2270b9f5f8aSAndrey V. Elsukov ifp->if_softc = NULL; 2280b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 2290b9f5f8aSAndrey V. Elsukov 230fc74a9f9SBrooks Davis if_free(ifp); 23125af0bb5SGleb Smirnoff GIF_LOCK_DESTROY(sc); 23253dab5feSBrooks Davis free(sc, M_GIF); 23353dab5feSBrooks Davis } 23453dab5feSBrooks Davis 235d0728d71SRobert Watson static void 236d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2371ed81b73SMarko Zec { 2381ed81b73SMarko Zec 2391ed81b73SMarko Zec LIST_INIT(&V_gif_softc_list); 240a7f5886eSHiroki Sato GIF_LIST_LOCK_INIT(); 241a7f5886eSHiroki Sato V_gif_cloner = if_clone_simple(gifname, gif_clone_create, 242a7f5886eSHiroki Sato gif_clone_destroy, 0); 2431ed81b73SMarko Zec } 244a7f5886eSHiroki Sato VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 245a7f5886eSHiroki Sato vnet_gif_init, NULL); 246a7f5886eSHiroki Sato 247a7f5886eSHiroki Sato static void 248a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused) 249a7f5886eSHiroki Sato { 250a7f5886eSHiroki Sato 251a7f5886eSHiroki Sato if_clone_detach(V_gif_cloner); 252a7f5886eSHiroki Sato GIF_LIST_LOCK_DESTROY(); 253a7f5886eSHiroki Sato } 254a7f5886eSHiroki Sato VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 255a7f5886eSHiroki Sato vnet_gif_uninit, NULL); 2561ed81b73SMarko Zec 2571ed81b73SMarko Zec static int 258c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data) 25953dab5feSBrooks Davis { 26053dab5feSBrooks Davis 26153dab5feSBrooks Davis switch (type) { 26253dab5feSBrooks Davis case MOD_LOAD: 26353dab5feSBrooks Davis case MOD_UNLOAD: 26453dab5feSBrooks Davis break; 2653e019deaSPoul-Henning Kamp default: 266a7f5886eSHiroki Sato return (EOPNOTSUPP); 26753dab5feSBrooks Davis } 268a7f5886eSHiroki Sato return (0); 26953dab5feSBrooks Davis } 27053dab5feSBrooks Davis 27153dab5feSBrooks Davis static moduledata_t gif_mod = { 27253dab5feSBrooks Davis "if_gif", 27353dab5feSBrooks Davis gifmodevent, 2749823d527SKevin Lo 0 27553dab5feSBrooks Davis }; 27653dab5feSBrooks Davis 27753dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 27820af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 279cfa1ca9dSYoshinobu Inoue 2809426aedfSHajimu UMEMOTO int 281c72a5d5dSAndrey V. Elsukov gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 282686cdd19SJun-ichiro itojun Hagino { 2830b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 284686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 2850b9f5f8aSAndrey V. Elsukov int ret; 2860b9f5f8aSAndrey V. Elsukov uint8_t ver; 287686cdd19SJun-ichiro itojun Hagino 288686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 2890b9f5f8aSAndrey V. Elsukov if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0) 2900b9f5f8aSAndrey V. Elsukov return (0); 291686cdd19SJun-ichiro itojun Hagino 2920b9f5f8aSAndrey V. Elsukov ret = 0; 2930b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 294686cdd19SJun-ichiro itojun Hagino 295686cdd19SJun-ichiro itojun Hagino /* no physical address */ 2960b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) 2970b9f5f8aSAndrey V. Elsukov goto done; 298686cdd19SJun-ichiro itojun Hagino 299686cdd19SJun-ichiro itojun Hagino switch (proto) { 300686cdd19SJun-ichiro itojun Hagino #ifdef INET 301686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV4: 302686cdd19SJun-ichiro itojun Hagino #endif 303686cdd19SJun-ichiro itojun Hagino #ifdef INET6 304686cdd19SJun-ichiro itojun Hagino case IPPROTO_IPV6: 305686cdd19SJun-ichiro itojun Hagino #endif 30673ff045cSAndrew Thompson case IPPROTO_ETHERIP: 30773ff045cSAndrew Thompson break; 308686cdd19SJun-ichiro itojun Hagino default: 3090b9f5f8aSAndrey V. Elsukov goto done; 310686cdd19SJun-ichiro itojun Hagino } 311686cdd19SJun-ichiro itojun Hagino 3123bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 3130b9f5f8aSAndrey V. Elsukov if (m->m_pkthdr.len < sizeof(struct ip)) 3140b9f5f8aSAndrey V. Elsukov goto done; 3153bb61ca6SHajimu UMEMOTO 3160b9f5f8aSAndrey V. Elsukov m_copydata(m, 0, 1, &ver); 3170b9f5f8aSAndrey V. Elsukov switch (ver >> 4) { 318686cdd19SJun-ichiro itojun Hagino #ifdef INET 319686cdd19SJun-ichiro itojun Hagino case 4: 3200b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) 3210b9f5f8aSAndrey V. Elsukov goto done; 3220b9f5f8aSAndrey V. Elsukov ret = in_gif_encapcheck(m, off, proto, arg); 3230b9f5f8aSAndrey V. Elsukov break; 324686cdd19SJun-ichiro itojun Hagino #endif 325686cdd19SJun-ichiro itojun Hagino #ifdef INET6 326686cdd19SJun-ichiro itojun Hagino case 6: 3279426aedfSHajimu UMEMOTO if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) 3280b9f5f8aSAndrey V. Elsukov goto done; 3290b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) 3300b9f5f8aSAndrey V. Elsukov goto done; 3310b9f5f8aSAndrey V. Elsukov ret = in6_gif_encapcheck(m, off, proto, arg); 3320b9f5f8aSAndrey V. Elsukov break; 333686cdd19SJun-ichiro itojun Hagino #endif 334686cdd19SJun-ichiro itojun Hagino } 3350b9f5f8aSAndrey V. Elsukov done: 3360b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 3370b9f5f8aSAndrey V. Elsukov return (ret); 338686cdd19SJun-ichiro itojun Hagino } 339686cdd19SJun-ichiro itojun Hagino 3400b9f5f8aSAndrey V. Elsukov static int 3410b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m) 34273ff045cSAndrew Thompson { 34373ff045cSAndrew Thompson struct gif_softc *sc; 3440b9f5f8aSAndrey V. Elsukov struct etherip_header *eth; 345776b7288SRandall Stewart #ifdef INET 3460b9f5f8aSAndrey V. Elsukov struct ip *ip; 347776b7288SRandall Stewart #endif 348776b7288SRandall Stewart #ifdef INET6 3490b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 3500b9f5f8aSAndrey V. Elsukov uint32_t t; 351776b7288SRandall Stewart #endif 3520b9f5f8aSAndrey V. Elsukov uint32_t af; 3530b9f5f8aSAndrey V. Elsukov uint8_t proto, ecn; 3540b9f5f8aSAndrey V. Elsukov int error; 3550b9f5f8aSAndrey V. Elsukov 3560b9f5f8aSAndrey V. Elsukov error = ENETDOWN; 3570b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 3580b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) { 3590b9f5f8aSAndrey V. Elsukov m_freem(m); 3600b9f5f8aSAndrey V. Elsukov goto err; 3610b9f5f8aSAndrey V. Elsukov } 3620b9f5f8aSAndrey V. Elsukov /* Now pull back the af that we stashed in the csum_data. */ 363cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 364776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 3653751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3660b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 367776b7288SRandall Stewart M_SETFIB(m, sc->gif_fibnum); 368776b7288SRandall Stewart /* inner AF-specific encapsulation */ 3690b9f5f8aSAndrey V. Elsukov ecn = 0; 3700b9f5f8aSAndrey V. Elsukov switch (af) { 371776b7288SRandall Stewart #ifdef INET 372776b7288SRandall Stewart case AF_INET: 3730b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV4; 3740b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 3750b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 3760b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3770b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3780b9f5f8aSAndrey V. Elsukov goto err; 3790b9f5f8aSAndrey V. Elsukov } 3800b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 3810b9f5f8aSAndrey V. Elsukov ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3820b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos); 383776b7288SRandall Stewart break; 384776b7288SRandall Stewart #endif 385776b7288SRandall Stewart #ifdef INET6 386776b7288SRandall Stewart case AF_INET6: 3870b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV6; 3880b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 3890b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 3900b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3910b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3920b9f5f8aSAndrey V. Elsukov goto err; 3930b9f5f8aSAndrey V. Elsukov } 3940b9f5f8aSAndrey V. Elsukov t = 0; 3950b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 3960b9f5f8aSAndrey V. Elsukov ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3970b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow); 3980b9f5f8aSAndrey V. Elsukov ecn = (ntohl(t) >> 20) & 0xff; 3990b9f5f8aSAndrey V. Elsukov break; 4000b9f5f8aSAndrey V. Elsukov #endif 4010b9f5f8aSAndrey V. Elsukov case AF_LINK: 4020b9f5f8aSAndrey V. Elsukov proto = IPPROTO_ETHERIP; 4030b9f5f8aSAndrey V. Elsukov M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 4040b9f5f8aSAndrey V. Elsukov if (m == NULL) { 4050b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 4060b9f5f8aSAndrey V. Elsukov goto err; 4070b9f5f8aSAndrey V. Elsukov } 4080b9f5f8aSAndrey V. Elsukov eth = mtod(m, struct etherip_header *); 4090b9f5f8aSAndrey V. Elsukov eth->eip_resvh = 0; 4100b9f5f8aSAndrey V. Elsukov if ((sc->gif_options & GIF_SEND_REVETHIP) != 0) { 4110b9f5f8aSAndrey V. Elsukov eth->eip_ver = 0; 4120b9f5f8aSAndrey V. Elsukov eth->eip_resvl = ETHERIP_VERSION; 4130b9f5f8aSAndrey V. Elsukov } else { 4140b9f5f8aSAndrey V. Elsukov eth->eip_ver = ETHERIP_VERSION; 4150b9f5f8aSAndrey V. Elsukov eth->eip_resvl = 0; 4160b9f5f8aSAndrey V. Elsukov } 4170b9f5f8aSAndrey V. Elsukov break; 4180b9f5f8aSAndrey V. Elsukov default: 4190b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 4200b9f5f8aSAndrey V. Elsukov m_freem(m); 4210b9f5f8aSAndrey V. Elsukov goto err; 4220b9f5f8aSAndrey V. Elsukov } 4230b9f5f8aSAndrey V. Elsukov /* XXX should we check if our outer source is legal? */ 4240b9f5f8aSAndrey V. Elsukov /* dispatch to output logic based on outer AF */ 4250b9f5f8aSAndrey V. Elsukov switch (sc->gif_family) { 4260b9f5f8aSAndrey V. Elsukov #ifdef INET 4270b9f5f8aSAndrey V. Elsukov case AF_INET: 4280b9f5f8aSAndrey V. Elsukov error = in_gif_output(ifp, m, proto, ecn); 4290b9f5f8aSAndrey V. Elsukov break; 4300b9f5f8aSAndrey V. Elsukov #endif 4310b9f5f8aSAndrey V. Elsukov #ifdef INET6 4320b9f5f8aSAndrey V. Elsukov case AF_INET6: 4330b9f5f8aSAndrey V. Elsukov error = in6_gif_output(ifp, m, proto, ecn); 434776b7288SRandall Stewart break; 435776b7288SRandall Stewart #endif 436776b7288SRandall Stewart default: 437776b7288SRandall Stewart m_freem(m); 438776b7288SRandall Stewart } 4390b9f5f8aSAndrey V. Elsukov err: 440776b7288SRandall Stewart if (error) 4413751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 4420b9f5f8aSAndrey V. Elsukov return (error); 44373ff045cSAndrew Thompson } 4440b9f5f8aSAndrey V. Elsukov 4450b9f5f8aSAndrey V. Elsukov static void 4460b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused) 4470b9f5f8aSAndrey V. Elsukov { 4480b9f5f8aSAndrey V. Elsukov 44973ff045cSAndrew Thompson } 45073ff045cSAndrew Thompson 451cfa1ca9dSYoshinobu Inoue int 45247e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 45347e8d432SGleb Smirnoff struct route *ro) 454cfa1ca9dSYoshinobu Inoue { 4558c7e1947SRuslan Ermilov struct m_tag *mtag; 456776b7288SRandall Stewart uint32_t af; 4570b9f5f8aSAndrey V. Elsukov int gif_called; 4580b9f5f8aSAndrey V. Elsukov int error = 0; 45910722b85SRobert Watson #ifdef MAC 46030d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 4610b9f5f8aSAndrey V. Elsukov if (error) 4620b9f5f8aSAndrey V. Elsukov goto err; 46310722b85SRobert Watson #endif 4640b9f5f8aSAndrey V. Elsukov if ((ifp->if_flags & IFF_MONITOR) != 0 || 4650b9f5f8aSAndrey V. Elsukov (ifp->if_flags & IFF_UP) == 0) { 466e9f947e2SHiroki Sato error = ENETDOWN; 4670b9f5f8aSAndrey V. Elsukov goto err; 468e9f947e2SHiroki Sato } 46910722b85SRobert Watson 470cfa1ca9dSYoshinobu Inoue /* 471cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 4728c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 4738c7e1947SRuslan Ermilov * 4748c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 475cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 476cfa1ca9dSYoshinobu Inoue */ 4778c7e1947SRuslan Ermilov gif_called = 1; 4788c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 4798c7e1947SRuslan Ermilov while (mtag != NULL) { 4808c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 4818c7e1947SRuslan Ermilov log(LOG_NOTICE, 4828c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 4838c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 4848c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 4850b9f5f8aSAndrey V. Elsukov goto err; 4868c7e1947SRuslan Ermilov } 4878c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 4888c7e1947SRuslan Ermilov gif_called++; 4898c7e1947SRuslan Ermilov } 490603724d3SBjoern A. Zeeb if (gif_called > V_max_gif_nesting) { 491cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 492cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 493523ebc4eSRobert Watson gif_called); 494cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 4950b9f5f8aSAndrey V. Elsukov goto err; 496cfa1ca9dSYoshinobu Inoue } 4978c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 4988c7e1947SRuslan Ermilov M_NOWAIT); 4998c7e1947SRuslan Ermilov if (mtag == NULL) { 5008c7e1947SRuslan Ermilov error = ENOMEM; 5010b9f5f8aSAndrey V. Elsukov goto err; 5028c7e1947SRuslan Ermilov } 5038c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 5048c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 505686cdd19SJun-ichiro itojun Hagino 506cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 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; 5110b9f5f8aSAndrey V. Elsukov if (ifp->if_bridge) 5120b9f5f8aSAndrey V. Elsukov af = AF_LINK; 51340138788SRandall Stewart /* 5140b9f5f8aSAndrey V. Elsukov * Now save the af in the inbound pkt csum data, this is a cheat since 5150b9f5f8aSAndrey V. Elsukov * we are using the inbound csum_data field to carry the af over to 5160b9f5f8aSAndrey V. Elsukov * the gif_transmit() routine, avoiding using yet another mtag. 517776b7288SRandall Stewart */ 518cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 5190b9f5f8aSAndrey V. Elsukov return (ifp->if_transmit(ifp, m)); 5200b9f5f8aSAndrey V. Elsukov err: 5213751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 5220b9f5f8aSAndrey V. Elsukov m_freem(m); 52325af0bb5SGleb Smirnoff return (error); 524cfa1ca9dSYoshinobu Inoue } 525cfa1ca9dSYoshinobu Inoue 526cfa1ca9dSYoshinobu Inoue void 5270b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn) 528cfa1ca9dSYoshinobu Inoue { 52973ff045cSAndrew Thompson struct etherip_header *eip; 5300b9f5f8aSAndrey V. Elsukov #ifdef INET 5310b9f5f8aSAndrey V. Elsukov struct ip *ip; 5320b9f5f8aSAndrey V. Elsukov #endif 5330b9f5f8aSAndrey V. Elsukov #ifdef INET6 5340b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 5350b9f5f8aSAndrey V. Elsukov uint32_t t; 5360b9f5f8aSAndrey V. Elsukov #endif 5370b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 53856abdd33SAndrew Thompson struct ether_header *eh; 53956abdd33SAndrew Thompson struct ifnet *oldifp; 5400b9f5f8aSAndrey V. Elsukov uint32_t gif_options; 5410b9f5f8aSAndrey V. Elsukov int isr, n, af; 542cfa1ca9dSYoshinobu Inoue 54321fb391fSHajimu UMEMOTO if (ifp == NULL) { 544cfa1ca9dSYoshinobu Inoue /* just in case */ 545cfa1ca9dSYoshinobu Inoue m_freem(m); 546cfa1ca9dSYoshinobu Inoue return; 547cfa1ca9dSYoshinobu Inoue } 5484382b068SChristian Brueffer sc = ifp->if_softc; 5490b9f5f8aSAndrey V. Elsukov gif_options = sc->gif_options; 55021fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 5515b7a43f5SAndrey V. Elsukov m_clrprotoflags(m); 5520b9f5f8aSAndrey V. Elsukov switch (proto) { 5530b9f5f8aSAndrey V. Elsukov #ifdef INET 5540b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV4: 5550b9f5f8aSAndrey V. Elsukov af = AF_INET; 5560b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 5570b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 5580b9f5f8aSAndrey V. Elsukov if (m == NULL) 5590b9f5f8aSAndrey V. Elsukov goto drop; 5600b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 5610b9f5f8aSAndrey V. Elsukov if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 5620b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos) == 0) { 5630b9f5f8aSAndrey V. Elsukov m_freem(m); 5640b9f5f8aSAndrey V. Elsukov goto drop; 5650b9f5f8aSAndrey V. Elsukov } 5660b9f5f8aSAndrey V. Elsukov break; 5670b9f5f8aSAndrey V. Elsukov #endif 5680b9f5f8aSAndrey V. Elsukov #ifdef INET6 5690b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV6: 5700b9f5f8aSAndrey V. Elsukov af = AF_INET6; 5710b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 5720b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 5730b9f5f8aSAndrey V. Elsukov if (m == NULL) 5740b9f5f8aSAndrey V. Elsukov goto drop; 5750b9f5f8aSAndrey V. Elsukov t = htonl((uint32_t)ecn << 20); 5760b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 5770b9f5f8aSAndrey V. Elsukov if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 5780b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow) == 0) { 5790b9f5f8aSAndrey V. Elsukov m_freem(m); 5800b9f5f8aSAndrey V. Elsukov goto drop; 5810b9f5f8aSAndrey V. Elsukov } 5820b9f5f8aSAndrey V. Elsukov break; 5830b9f5f8aSAndrey V. Elsukov #endif 5840b9f5f8aSAndrey V. Elsukov case IPPROTO_ETHERIP: 5850b9f5f8aSAndrey V. Elsukov af = AF_LINK; 5860b9f5f8aSAndrey V. Elsukov break; 5870b9f5f8aSAndrey V. Elsukov default: 5880b9f5f8aSAndrey V. Elsukov m_freem(m); 5890b9f5f8aSAndrey V. Elsukov goto drop; 5900b9f5f8aSAndrey V. Elsukov } 591cfa1ca9dSYoshinobu Inoue 59210722b85SRobert Watson #ifdef MAC 59330d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 59410722b85SRobert Watson #endif 59510722b85SRobert Watson 59616d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 5970b9f5f8aSAndrey V. Elsukov uint32_t af1 = af; 598437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 599cfa1ca9dSYoshinobu Inoue } 600cfa1ca9dSYoshinobu Inoue 601e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 6023751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 6033751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 604e9f947e2SHiroki Sato m_freem(m); 605e9f947e2SHiroki Sato return; 606e9f947e2SHiroki Sato } 607e9f947e2SHiroki Sato 60894408d94SBrooks Davis if (ng_gif_input_p != NULL) { 60921fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 61094408d94SBrooks Davis if (m == NULL) 6110b9f5f8aSAndrey V. Elsukov goto drop; 61294408d94SBrooks Davis } 61394408d94SBrooks Davis 614cfa1ca9dSYoshinobu Inoue /* 615cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 616cfa1ca9dSYoshinobu Inoue * specified address family. 617cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 618cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 619cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 620cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 621cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 62288ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 62388ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 624cfa1ca9dSYoshinobu Inoue */ 625cfa1ca9dSYoshinobu Inoue switch (af) { 626cfa1ca9dSYoshinobu Inoue #ifdef INET 627cfa1ca9dSYoshinobu Inoue case AF_INET: 628cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 629cfa1ca9dSYoshinobu Inoue break; 630cfa1ca9dSYoshinobu Inoue #endif 631cfa1ca9dSYoshinobu Inoue #ifdef INET6 632cfa1ca9dSYoshinobu Inoue case AF_INET6: 633cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 634cfa1ca9dSYoshinobu Inoue break; 635cfa1ca9dSYoshinobu Inoue #endif 63673ff045cSAndrew Thompson case AF_LINK: 63773ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 6380b9f5f8aSAndrey V. Elsukov if (n > m->m_len) 63973ff045cSAndrew Thompson m = m_pullup(m, n); 6400b9f5f8aSAndrey V. Elsukov if (m == NULL) 6410b9f5f8aSAndrey V. Elsukov goto drop; 64273ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 643dbe59260SHiroki Sato /* 644dbe59260SHiroki Sato * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 645dbe59260SHiroki Sato * accepts an EtherIP packet with revered version field in 646dbe59260SHiroki Sato * the header. This is a knob for backward compatibility 647dbe59260SHiroki Sato * with FreeBSD 7.2R or prior. 648dbe59260SHiroki Sato */ 649dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 6500b9f5f8aSAndrey V. Elsukov if ((gif_options & GIF_ACCEPT_REVETHIP) == 0 || 6510b9f5f8aSAndrey V. Elsukov eip->eip_resvl != ETHERIP_VERSION) { 652dbe59260SHiroki Sato /* discard unknown versions */ 653dbe59260SHiroki Sato m_freem(m); 6540b9f5f8aSAndrey V. Elsukov goto drop; 655dbe59260SHiroki Sato } 656dbe59260SHiroki Sato } 65773ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 65873ff045cSAndrew Thompson 65973ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 66073ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 66173ff045cSAndrew Thompson 66256abdd33SAndrew Thompson if (ifp->if_bridge) { 66356abdd33SAndrew Thompson oldifp = ifp; 66456abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 66556abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 66656abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 66756abdd33SAndrew Thompson m->m_flags |= M_BCAST; 66856abdd33SAndrew Thompson else 66956abdd33SAndrew Thompson m->m_flags |= M_MCAST; 6703751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 67156abdd33SAndrew Thompson } 67273ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 67373ff045cSAndrew Thompson 67456abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 67556abdd33SAndrew Thompson /* 67656abdd33SAndrew Thompson * The bridge gave us back itself or one of the 67756abdd33SAndrew Thompson * members for which the frame is addressed. 67856abdd33SAndrew Thompson */ 67956abdd33SAndrew Thompson ether_demux(ifp, m); 68056abdd33SAndrew Thompson return; 68156abdd33SAndrew Thompson } 68256abdd33SAndrew Thompson } 68373ff045cSAndrew Thompson if (m != NULL) 68473ff045cSAndrew Thompson m_freem(m); 68573ff045cSAndrew Thompson return; 68673ff045cSAndrew Thompson 687cfa1ca9dSYoshinobu Inoue default: 68894408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 68921fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 69094408d94SBrooks Davis else 691cfa1ca9dSYoshinobu Inoue m_freem(m); 692cfa1ca9dSYoshinobu Inoue return; 693cfa1ca9dSYoshinobu Inoue } 694cfa1ca9dSYoshinobu Inoue 6953751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 6963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 697a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 6981cafed39SJonathan Lemon netisr_dispatch(isr, m); 6990b9f5f8aSAndrey V. Elsukov return; 7000b9f5f8aSAndrey V. Elsukov drop: 7010b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 702cfa1ca9dSYoshinobu Inoue } 703cfa1ca9dSYoshinobu Inoue 704686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 705cfa1ca9dSYoshinobu Inoue int 706c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 707cfa1ca9dSYoshinobu Inoue { 7080b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 709cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 710686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 7110b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 7120b9f5f8aSAndrey V. Elsukov #ifdef INET 7130b9f5f8aSAndrey V. Elsukov struct sockaddr_in *sin = NULL; 7143bb61ca6SHajimu UMEMOTO #endif 7150b9f5f8aSAndrey V. Elsukov #ifdef INET6 7160b9f5f8aSAndrey V. Elsukov struct sockaddr_in6 *sin6 = NULL; 7170b9f5f8aSAndrey V. Elsukov #endif 7180b9f5f8aSAndrey V. Elsukov u_int options; 7190b9f5f8aSAndrey V. Elsukov int error; 720cfa1ca9dSYoshinobu Inoue 721cfa1ca9dSYoshinobu Inoue switch (cmd) { 722cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 7239426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 724cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 725cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 726cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 7270b9f5f8aSAndrey V. Elsukov case SIOCSIFFLAGS: 7280b9f5f8aSAndrey V. Elsukov return (0); 729cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 7300b9f5f8aSAndrey V. Elsukov if (ifr->ifr_mtu < GIF_MTU_MIN || 7310b9f5f8aSAndrey V. Elsukov ifr->ifr_mtu > GIF_MTU_MAX) 732cfa1ca9dSYoshinobu Inoue return (EINVAL); 7330b9f5f8aSAndrey V. Elsukov else 7340b9f5f8aSAndrey V. Elsukov ifp->if_mtu = ifr->ifr_mtu; 7350b9f5f8aSAndrey V. Elsukov return (0); 7360b9f5f8aSAndrey V. Elsukov } 7370b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 7380b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 7390b9f5f8aSAndrey V. Elsukov if (sc == NULL) { 7400b9f5f8aSAndrey V. Elsukov error = ENXIO; 7410b9f5f8aSAndrey V. Elsukov goto bad; 7420b9f5f8aSAndrey V. Elsukov } 7430b9f5f8aSAndrey V. Elsukov error = 0; 7440b9f5f8aSAndrey V. Elsukov switch (cmd) { 745cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 746245c40e8SAndrey V. Elsukov #ifdef INET6 747cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 748245c40e8SAndrey V. Elsukov #endif 7490b9f5f8aSAndrey V. Elsukov error = EINVAL; 750686cdd19SJun-ichiro itojun Hagino switch (cmd) { 75133841545SHajimu UMEMOTO #ifdef INET 752686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 753cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 754cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 755cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 756cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 757cfa1ca9dSYoshinobu Inoue break; 75833841545SHajimu UMEMOTO #endif 759cfa1ca9dSYoshinobu Inoue #ifdef INET6 760686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 761cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 762cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 763cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 764cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 765686cdd19SJun-ichiro itojun Hagino break; 766686cdd19SJun-ichiro itojun Hagino #endif 7676f4ded3aSBrooks Davis default: 7680b9f5f8aSAndrey V. Elsukov goto bad; 76933841545SHajimu UMEMOTO } 77033841545SHajimu UMEMOTO /* sa_family must be equal */ 7710b9f5f8aSAndrey V. Elsukov if (src->sa_family != dst->sa_family || 7720b9f5f8aSAndrey V. Elsukov src->sa_len != dst->sa_len) 7730b9f5f8aSAndrey V. Elsukov goto bad; 77433841545SHajimu UMEMOTO 77533841545SHajimu UMEMOTO /* validate sa_len */ 77633841545SHajimu UMEMOTO switch (src->sa_family) { 77733841545SHajimu UMEMOTO #ifdef INET 77833841545SHajimu UMEMOTO case AF_INET: 77933841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 7800b9f5f8aSAndrey V. Elsukov goto bad; 78133841545SHajimu UMEMOTO break; 78233841545SHajimu UMEMOTO #endif 78333841545SHajimu UMEMOTO #ifdef INET6 78433841545SHajimu UMEMOTO case AF_INET6: 78533841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 7860b9f5f8aSAndrey V. Elsukov goto bad; 78733841545SHajimu UMEMOTO break; 78833841545SHajimu UMEMOTO #endif 78933841545SHajimu UMEMOTO default: 7900b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 7910b9f5f8aSAndrey V. Elsukov goto bad; 79233841545SHajimu UMEMOTO } 79333841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 7940b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 79533841545SHajimu UMEMOTO switch (cmd) { 7960b9f5f8aSAndrey V. Elsukov #ifdef INET 79733841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 79833841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 79933841545SHajimu UMEMOTO break; 8000b9f5f8aSAndrey V. Elsukov goto bad; 8010b9f5f8aSAndrey V. Elsukov #endif 80233841545SHajimu UMEMOTO #ifdef INET6 80333841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 80433841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 80533841545SHajimu UMEMOTO break; 8060b9f5f8aSAndrey V. Elsukov goto bad; 8070b9f5f8aSAndrey V. Elsukov #endif 808686cdd19SJun-ichiro itojun Hagino } 8090b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 8100b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 8110b9f5f8aSAndrey V. Elsukov #ifdef INET 8120b9f5f8aSAndrey V. Elsukov case AF_INET: 8130b9f5f8aSAndrey V. Elsukov if (satosin(src)->sin_addr.s_addr == INADDR_ANY || 8140b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr == INADDR_ANY) 8150b9f5f8aSAndrey V. Elsukov goto bad; 816cfa1ca9dSYoshinobu Inoue break; 8170b9f5f8aSAndrey V. Elsukov #endif 8180b9f5f8aSAndrey V. Elsukov #ifdef INET6 8190b9f5f8aSAndrey V. Elsukov case AF_INET6: 8200b9f5f8aSAndrey V. Elsukov if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr) 8210b9f5f8aSAndrey V. Elsukov || 8220b9f5f8aSAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr)) 8230b9f5f8aSAndrey V. Elsukov goto bad; 8240b9f5f8aSAndrey V. Elsukov /* 8250b9f5f8aSAndrey V. Elsukov * Check validity of the scope zone ID of the 8260b9f5f8aSAndrey V. Elsukov * addresses, and convert it into the kernel 8270b9f5f8aSAndrey V. Elsukov * internal form if necessary. 8280b9f5f8aSAndrey V. Elsukov */ 8290b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(src), 0); 8300b9f5f8aSAndrey V. Elsukov if (error != 0) 8310b9f5f8aSAndrey V. Elsukov goto bad; 8320b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(dst), 0); 8330b9f5f8aSAndrey V. Elsukov if (error != 0) 8340b9f5f8aSAndrey V. Elsukov goto bad; 8350b9f5f8aSAndrey V. Elsukov #endif 8360b9f5f8aSAndrey V. Elsukov }; 8370b9f5f8aSAndrey V. Elsukov error = gif_set_tunnel(ifp, src, dst); 8380b9f5f8aSAndrey V. Elsukov break; 839686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 8400b9f5f8aSAndrey V. Elsukov gif_delete_tunnel(ifp); 841686cdd19SJun-ichiro itojun Hagino break; 842cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 8430b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 844245c40e8SAndrey V. Elsukov #ifdef INET6 845cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 8460b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 847245c40e8SAndrey V. Elsukov #endif 8480b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) { 849cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 8500b9f5f8aSAndrey V. Elsukov break; 851cfa1ca9dSYoshinobu Inoue } 8520b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 85333841545SHajimu UMEMOTO switch (cmd) { 854cfa1ca9dSYoshinobu Inoue #ifdef INET 85533841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 8560b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 8570b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) { 8580b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 859cfa1ca9dSYoshinobu Inoue break; 8600b9f5f8aSAndrey V. Elsukov } 8610b9f5f8aSAndrey V. Elsukov sin = (struct sockaddr_in *)&ifr->ifr_addr; 8620b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 8630b9f5f8aSAndrey V. Elsukov sin->sin_family = AF_INET; 8640b9f5f8aSAndrey V. Elsukov sin->sin_len = sizeof(*sin); 8650b9f5f8aSAndrey V. Elsukov break; 8660b9f5f8aSAndrey V. Elsukov #endif 867cfa1ca9dSYoshinobu Inoue #ifdef INET6 86833841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 869cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 8700b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) { 871cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 8720b9f5f8aSAndrey V. Elsukov break; 873cfa1ca9dSYoshinobu Inoue } 8740b9f5f8aSAndrey V. Elsukov sin6 = (struct sockaddr_in6 *) 8750b9f5f8aSAndrey V. Elsukov &(((struct in6_ifreq *)data)->ifr_addr); 8760b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 8770b9f5f8aSAndrey V. Elsukov sin6->sin6_family = AF_INET6; 8780b9f5f8aSAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 8790b9f5f8aSAndrey V. Elsukov break; 8800b9f5f8aSAndrey V. Elsukov #endif 8810b9f5f8aSAndrey V. Elsukov default: 8820b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 8830b9f5f8aSAndrey V. Elsukov } 8840b9f5f8aSAndrey V. Elsukov if (error == 0) { 88533841545SHajimu UMEMOTO switch (cmd) { 886cfa1ca9dSYoshinobu Inoue #ifdef INET 8870b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 8880b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_src; 8890b9f5f8aSAndrey V. Elsukov break; 89033841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 8910b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_dst; 892cfa1ca9dSYoshinobu Inoue break; 893a1f7e5f8SHajimu UMEMOTO #endif 8940b9f5f8aSAndrey V. Elsukov #ifdef INET6 8950b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 8960b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_src; 89733841545SHajimu UMEMOTO break; 8980b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 8990b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst; 900cfa1ca9dSYoshinobu Inoue break; 9010b9f5f8aSAndrey V. Elsukov #endif 9020b9f5f8aSAndrey V. Elsukov } 9030b9f5f8aSAndrey V. Elsukov } 9040b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 9050b9f5f8aSAndrey V. Elsukov if (error != 0) 9060b9f5f8aSAndrey V. Elsukov break; 9070b9f5f8aSAndrey V. Elsukov switch (cmd) { 9080b9f5f8aSAndrey V. Elsukov #ifdef INET 9090b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 9100b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 9110b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 9120b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin); 9130b9f5f8aSAndrey V. Elsukov if (error != 0) 9140b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 9150b9f5f8aSAndrey V. Elsukov break; 9160b9f5f8aSAndrey V. Elsukov #endif 9170b9f5f8aSAndrey V. Elsukov #ifdef INET6 9180b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 9190b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 9200b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 9210b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin6); 9220b9f5f8aSAndrey V. Elsukov if (error == 0) 9230b9f5f8aSAndrey V. Elsukov error = sa6_recoverscope(sin6); 9240b9f5f8aSAndrey V. Elsukov if (error != 0) 9250b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 926138d5655SAndrey V. Elsukov #endif 9270b9f5f8aSAndrey V. Elsukov } 9280b9f5f8aSAndrey V. Elsukov break; 929dbe59260SHiroki Sato case GIFGOPTS: 930dbe59260SHiroki Sato options = sc->gif_options; 9310b9f5f8aSAndrey V. Elsukov error = copyout(&options, ifr->ifr_data, sizeof(options)); 932dbe59260SHiroki Sato break; 933dbe59260SHiroki Sato case GIFSOPTS: 934dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 935dbe59260SHiroki Sato break; 9364cd5f57dSHiroki Sato error = copyin(ifr->ifr_data, &options, sizeof(options)); 9374cd5f57dSHiroki Sato if (error) 9384cd5f57dSHiroki Sato break; 9394cd5f57dSHiroki Sato if (options & ~GIF_OPTMASK) 940dbe59260SHiroki Sato error = EINVAL; 9414cd5f57dSHiroki Sato else 9424cd5f57dSHiroki Sato sc->gif_options = options; 943dbe59260SHiroki Sato break; 944dbe59260SHiroki Sato 945cfa1ca9dSYoshinobu Inoue default: 946cfa1ca9dSYoshinobu Inoue error = EINVAL; 947cfa1ca9dSYoshinobu Inoue break; 948cfa1ca9dSYoshinobu Inoue } 949cfa1ca9dSYoshinobu Inoue bad: 9500b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 9510b9f5f8aSAndrey V. Elsukov return (error); 952cfa1ca9dSYoshinobu Inoue } 95353dab5feSBrooks Davis 9540b9f5f8aSAndrey V. Elsukov static void 9550b9f5f8aSAndrey V. Elsukov gif_detach(struct gif_softc *sc) 9560b9f5f8aSAndrey V. Elsukov { 9570b9f5f8aSAndrey V. Elsukov 9580b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 9590b9f5f8aSAndrey V. Elsukov if (sc->gif_ecookie != NULL) 9600b9f5f8aSAndrey V. Elsukov encap_detach(sc->gif_ecookie); 9610b9f5f8aSAndrey V. Elsukov sc->gif_ecookie = NULL; 9620b9f5f8aSAndrey V. Elsukov } 9630b9f5f8aSAndrey V. Elsukov 9640b9f5f8aSAndrey V. Elsukov static int 9650b9f5f8aSAndrey V. Elsukov gif_attach(struct gif_softc *sc, int af) 9660b9f5f8aSAndrey V. Elsukov { 9670b9f5f8aSAndrey V. Elsukov 9680b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 9690b9f5f8aSAndrey V. Elsukov switch (af) { 9700b9f5f8aSAndrey V. Elsukov #ifdef INET 9710b9f5f8aSAndrey V. Elsukov case AF_INET: 9720b9f5f8aSAndrey V. Elsukov return (in_gif_attach(sc)); 9730b9f5f8aSAndrey V. Elsukov #endif 9740b9f5f8aSAndrey V. Elsukov #ifdef INET6 9750b9f5f8aSAndrey V. Elsukov case AF_INET6: 9760b9f5f8aSAndrey V. Elsukov return (in6_gif_attach(sc)); 9770b9f5f8aSAndrey V. Elsukov #endif 9780b9f5f8aSAndrey V. Elsukov } 9790b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 9800b9f5f8aSAndrey V. Elsukov } 9810b9f5f8aSAndrey V. Elsukov 9820b9f5f8aSAndrey V. Elsukov static int 983c72a5d5dSAndrey V. Elsukov gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst) 98453dab5feSBrooks Davis { 985fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 9860b9f5f8aSAndrey V. Elsukov struct gif_softc *tsc; 9870b9f5f8aSAndrey V. Elsukov #ifdef INET 9880b9f5f8aSAndrey V. Elsukov struct ip *ip; 9890b9f5f8aSAndrey V. Elsukov #endif 9900b9f5f8aSAndrey V. Elsukov #ifdef INET6 9910b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 9920b9f5f8aSAndrey V. Elsukov #endif 9930b9f5f8aSAndrey V. Elsukov void *hdr; 9949426aedfSHajimu UMEMOTO int error = 0; 9959426aedfSHajimu UMEMOTO 9960b9f5f8aSAndrey V. Elsukov if (sc == NULL) 9970b9f5f8aSAndrey V. Elsukov return (ENXIO); 9980b9f5f8aSAndrey V. Elsukov /* Disallow parallel tunnels unless instructed otherwise. */ 9990b9f5f8aSAndrey V. Elsukov if (V_parallel_tunnels == 0) { 1000a7f5886eSHiroki Sato GIF_LIST_LOCK(); 10010b9f5f8aSAndrey V. Elsukov LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) { 10020b9f5f8aSAndrey V. Elsukov if (tsc == sc || tsc->gif_family != src->sa_family) 10039426aedfSHajimu UMEMOTO continue; 10040b9f5f8aSAndrey V. Elsukov #ifdef INET 10050b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET && 10060b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_src.s_addr == 10070b9f5f8aSAndrey V. Elsukov satosin(src)->sin_addr.s_addr && 10080b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_dst.s_addr == 10090b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr) { 10109426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 1011a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10129426aedfSHajimu UMEMOTO goto bad; 10139426aedfSHajimu UMEMOTO } 10140b9f5f8aSAndrey V. Elsukov #endif 10150b9f5f8aSAndrey V. Elsukov #ifdef INET6 10160b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET6 && 10170b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src, 10180b9f5f8aSAndrey V. Elsukov &satosin6(src)->sin6_addr) && 10190b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst, 10200b9f5f8aSAndrey V. Elsukov &satosin6(dst)->sin6_addr)) { 10210b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 1022a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10239426aedfSHajimu UMEMOTO goto bad; 10249426aedfSHajimu UMEMOTO } 10250b9f5f8aSAndrey V. Elsukov #endif 10260b9f5f8aSAndrey V. Elsukov } 10270b9f5f8aSAndrey V. Elsukov GIF_LIST_UNLOCK(); 10280b9f5f8aSAndrey V. Elsukov } 10290b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 10300b9f5f8aSAndrey V. Elsukov #ifdef INET 10310b9f5f8aSAndrey V. Elsukov case AF_INET: 10320b9f5f8aSAndrey V. Elsukov hdr = ip = malloc(sizeof(struct ip), M_GIF, 10330b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 10340b9f5f8aSAndrey V. Elsukov ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr; 10350b9f5f8aSAndrey V. Elsukov ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr; 10360b9f5f8aSAndrey V. Elsukov break; 10370b9f5f8aSAndrey V. Elsukov #endif 10380b9f5f8aSAndrey V. Elsukov #ifdef INET6 10390b9f5f8aSAndrey V. Elsukov case AF_INET6: 10400b9f5f8aSAndrey V. Elsukov hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF, 10410b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 10420b9f5f8aSAndrey V. Elsukov ip6->ip6_src = satosin6(src)->sin6_addr; 10430b9f5f8aSAndrey V. Elsukov ip6->ip6_dst = satosin6(dst)->sin6_addr; 10440b9f5f8aSAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION; 10450b9f5f8aSAndrey V. Elsukov break; 10460b9f5f8aSAndrey V. Elsukov #endif 10470b9f5f8aSAndrey V. Elsukov default: 10480b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 10490b9f5f8aSAndrey V. Elsukov }; 10509426aedfSHajimu UMEMOTO 10510b9f5f8aSAndrey V. Elsukov if (sc->gif_family != src->sa_family) 10520b9f5f8aSAndrey V. Elsukov gif_detach(sc); 10530b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0 || 10540b9f5f8aSAndrey V. Elsukov sc->gif_family != src->sa_family) 10550b9f5f8aSAndrey V. Elsukov error = gif_attach(sc, src->sa_family); 10569426aedfSHajimu UMEMOTO 10570b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 10580b9f5f8aSAndrey V. Elsukov if (sc->gif_family != 0) 10590b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 10600b9f5f8aSAndrey V. Elsukov sc->gif_family = src->sa_family; 10610b9f5f8aSAndrey V. Elsukov sc->gif_hdr = hdr; 10620b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 1063245c40e8SAndrey V. Elsukov #if defined(INET) || defined(INET6) 10649426aedfSHajimu UMEMOTO bad: 1065245c40e8SAndrey V. Elsukov #endif 10660b9f5f8aSAndrey V. Elsukov if (error == 0 && sc->gif_family != 0) 106713f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 10689426aedfSHajimu UMEMOTO else 106913f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 10700b9f5f8aSAndrey V. Elsukov return (error); 10719426aedfSHajimu UMEMOTO } 10729426aedfSHajimu UMEMOTO 10730b9f5f8aSAndrey V. Elsukov static void 1074c72a5d5dSAndrey V. Elsukov gif_delete_tunnel(struct ifnet *ifp) 10759426aedfSHajimu UMEMOTO { 1076fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 10770b9f5f8aSAndrey V. Elsukov int family; 107853dab5feSBrooks Davis 10790b9f5f8aSAndrey V. Elsukov if (sc == NULL) 10800b9f5f8aSAndrey V. Elsukov return; 10810b9f5f8aSAndrey V. Elsukov 10820b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 10830b9f5f8aSAndrey V. Elsukov family = sc->gif_family; 10840b9f5f8aSAndrey V. Elsukov sc->gif_family = 0; 10850b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 10860b9f5f8aSAndrey V. Elsukov if (family != 0) { 10870b9f5f8aSAndrey V. Elsukov gif_detach(sc); 10880b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 108953dab5feSBrooks Davis } 109013f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 109153dab5feSBrooks Davis } 1092