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. 28*0b9f5f8aSAndrey V. Elsukov * 29*0b9f5f8aSAndrey V. Elsukov * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ 30cfa1ca9dSYoshinobu Inoue */ 31cfa1ca9dSYoshinobu Inoue 32*0b9f5f8aSAndrey V. Elsukov #include <sys/cdefs.h> 33*0b9f5f8aSAndrey V. Elsukov __FBSDID("$FreeBSD$"); 34*0b9f5f8aSAndrey 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> 42*0b9f5f8aSAndrey 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> 46*0b9f5f8aSAndrey V. Elsukov #include <sys/rmlock.h> 47cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 48cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 49*0b9f5f8aSAndrey 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> 72*0b9f5f8aSAndrey 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> 85*0b9f5f8aSAndrey 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) 109*0b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx; 110*0b9f5f8aSAndrey 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 123*0b9f5f8aSAndrey V. Elsukov static int gif_set_tunnel(struct ifnet *, struct sockaddr *, 124*0b9f5f8aSAndrey V. Elsukov struct sockaddr *); 125*0b9f5f8aSAndrey V. Elsukov static void gif_delete_tunnel(struct ifnet *); 126*0b9f5f8aSAndrey V. Elsukov static int gif_ioctl(struct ifnet *, u_long, caddr_t); 127*0b9f5f8aSAndrey V. Elsukov static int gif_transmit(struct ifnet *, struct mbuf *); 128*0b9f5f8aSAndrey 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) 152eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, 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) 166eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW, 167eddfbb76SRobert Watson &VNET_NAME(parallel_tunnels), 0, "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; 197*0b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_transmit = gif_transmit; 198*0b9f5f8aSAndrey 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 { 214*0b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 215febd0759SAndrew Thompson 216*0b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 217*0b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 218*0b9f5f8aSAndrey 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); 226*0b9f5f8aSAndrey V. Elsukov ifp->if_softc = NULL; 227*0b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 228*0b9f5f8aSAndrey 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 { 282*0b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 283686cdd19SJun-ichiro itojun Hagino struct gif_softc *sc; 284*0b9f5f8aSAndrey V. Elsukov int ret; 285*0b9f5f8aSAndrey V. Elsukov uint8_t ver; 286686cdd19SJun-ichiro itojun Hagino 287686cdd19SJun-ichiro itojun Hagino sc = (struct gif_softc *)arg; 288*0b9f5f8aSAndrey V. Elsukov if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0) 289*0b9f5f8aSAndrey V. Elsukov return (0); 290686cdd19SJun-ichiro itojun Hagino 291*0b9f5f8aSAndrey V. Elsukov ret = 0; 292*0b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 293686cdd19SJun-ichiro itojun Hagino 294686cdd19SJun-ichiro itojun Hagino /* no physical address */ 295*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) 296*0b9f5f8aSAndrey 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: 308*0b9f5f8aSAndrey V. Elsukov goto done; 309686cdd19SJun-ichiro itojun Hagino } 310686cdd19SJun-ichiro itojun Hagino 3113bb61ca6SHajimu UMEMOTO /* Bail on short packets */ 312*0b9f5f8aSAndrey V. Elsukov if (m->m_pkthdr.len < sizeof(struct ip)) 313*0b9f5f8aSAndrey V. Elsukov goto done; 3143bb61ca6SHajimu UMEMOTO 315*0b9f5f8aSAndrey V. Elsukov m_copydata(m, 0, 1, &ver); 316*0b9f5f8aSAndrey V. Elsukov switch (ver >> 4) { 317686cdd19SJun-ichiro itojun Hagino #ifdef INET 318686cdd19SJun-ichiro itojun Hagino case 4: 319*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) 320*0b9f5f8aSAndrey V. Elsukov goto done; 321*0b9f5f8aSAndrey V. Elsukov ret = in_gif_encapcheck(m, off, proto, arg); 322*0b9f5f8aSAndrey 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)) 327*0b9f5f8aSAndrey V. Elsukov goto done; 328*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) 329*0b9f5f8aSAndrey V. Elsukov goto done; 330*0b9f5f8aSAndrey V. Elsukov ret = in6_gif_encapcheck(m, off, proto, arg); 331*0b9f5f8aSAndrey V. Elsukov break; 332686cdd19SJun-ichiro itojun Hagino #endif 333686cdd19SJun-ichiro itojun Hagino } 334*0b9f5f8aSAndrey V. Elsukov done: 335*0b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 336*0b9f5f8aSAndrey V. Elsukov return (ret); 337686cdd19SJun-ichiro itojun Hagino } 338686cdd19SJun-ichiro itojun Hagino 339*0b9f5f8aSAndrey V. Elsukov static int 340*0b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m) 34173ff045cSAndrew Thompson { 34273ff045cSAndrew Thompson struct gif_softc *sc; 343*0b9f5f8aSAndrey V. Elsukov struct etherip_header *eth; 344776b7288SRandall Stewart #ifdef INET 345*0b9f5f8aSAndrey V. Elsukov struct ip *ip; 346776b7288SRandall Stewart #endif 347776b7288SRandall Stewart #ifdef INET6 348*0b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 349*0b9f5f8aSAndrey V. Elsukov uint32_t t; 350776b7288SRandall Stewart #endif 351*0b9f5f8aSAndrey V. Elsukov uint32_t af; 352*0b9f5f8aSAndrey V. Elsukov uint8_t proto, ecn; 353*0b9f5f8aSAndrey V. Elsukov int error; 354*0b9f5f8aSAndrey V. Elsukov 355*0b9f5f8aSAndrey V. Elsukov error = ENETDOWN; 356*0b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 357*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) { 358*0b9f5f8aSAndrey V. Elsukov m_freem(m); 359*0b9f5f8aSAndrey V. Elsukov goto err; 360*0b9f5f8aSAndrey V. Elsukov } 361*0b9f5f8aSAndrey V. Elsukov /* Now pull back the af that we stashed in the csum_data. */ 362cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 363776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 3643751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 365*0b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 366776b7288SRandall Stewart M_SETFIB(m, sc->gif_fibnum); 367776b7288SRandall Stewart /* inner AF-specific encapsulation */ 368*0b9f5f8aSAndrey V. Elsukov ecn = 0; 369*0b9f5f8aSAndrey V. Elsukov switch (af) { 370776b7288SRandall Stewart #ifdef INET 371776b7288SRandall Stewart case AF_INET: 372*0b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV4; 373*0b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 374*0b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 375*0b9f5f8aSAndrey V. Elsukov if (m == NULL) { 376*0b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 377*0b9f5f8aSAndrey V. Elsukov goto err; 378*0b9f5f8aSAndrey V. Elsukov } 379*0b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 380*0b9f5f8aSAndrey V. Elsukov ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 381*0b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos); 382776b7288SRandall Stewart break; 383776b7288SRandall Stewart #endif 384776b7288SRandall Stewart #ifdef INET6 385776b7288SRandall Stewart case AF_INET6: 386*0b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV6; 387*0b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 388*0b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 389*0b9f5f8aSAndrey V. Elsukov if (m == NULL) { 390*0b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 391*0b9f5f8aSAndrey V. Elsukov goto err; 392*0b9f5f8aSAndrey V. Elsukov } 393*0b9f5f8aSAndrey V. Elsukov t = 0; 394*0b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 395*0b9f5f8aSAndrey V. Elsukov ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 396*0b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow); 397*0b9f5f8aSAndrey V. Elsukov ecn = (ntohl(t) >> 20) & 0xff; 398*0b9f5f8aSAndrey V. Elsukov break; 399*0b9f5f8aSAndrey V. Elsukov #endif 400*0b9f5f8aSAndrey V. Elsukov case AF_LINK: 401*0b9f5f8aSAndrey V. Elsukov proto = IPPROTO_ETHERIP; 402*0b9f5f8aSAndrey V. Elsukov M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 403*0b9f5f8aSAndrey V. Elsukov if (m != NULL && m->m_len < sizeof(struct etherip_header)) 404*0b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct etherip_header)); 405*0b9f5f8aSAndrey V. Elsukov if (m == NULL) { 406*0b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 407*0b9f5f8aSAndrey V. Elsukov goto err; 408*0b9f5f8aSAndrey V. Elsukov } 409*0b9f5f8aSAndrey V. Elsukov eth = mtod(m, struct etherip_header *); 410*0b9f5f8aSAndrey V. Elsukov eth->eip_resvh = 0; 411*0b9f5f8aSAndrey V. Elsukov if ((sc->gif_options & GIF_SEND_REVETHIP) != 0) { 412*0b9f5f8aSAndrey V. Elsukov eth->eip_ver = 0; 413*0b9f5f8aSAndrey V. Elsukov eth->eip_resvl = ETHERIP_VERSION; 414*0b9f5f8aSAndrey V. Elsukov } else { 415*0b9f5f8aSAndrey V. Elsukov eth->eip_ver = ETHERIP_VERSION; 416*0b9f5f8aSAndrey V. Elsukov eth->eip_resvl = 0; 417*0b9f5f8aSAndrey V. Elsukov } 418*0b9f5f8aSAndrey V. Elsukov break; 419*0b9f5f8aSAndrey V. Elsukov default: 420*0b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 421*0b9f5f8aSAndrey V. Elsukov m_freem(m); 422*0b9f5f8aSAndrey V. Elsukov goto err; 423*0b9f5f8aSAndrey V. Elsukov } 424*0b9f5f8aSAndrey V. Elsukov /* XXX should we check if our outer source is legal? */ 425*0b9f5f8aSAndrey V. Elsukov /* dispatch to output logic based on outer AF */ 426*0b9f5f8aSAndrey V. Elsukov switch (sc->gif_family) { 427*0b9f5f8aSAndrey V. Elsukov #ifdef INET 428*0b9f5f8aSAndrey V. Elsukov case AF_INET: 429*0b9f5f8aSAndrey V. Elsukov error = in_gif_output(ifp, m, proto, ecn); 430*0b9f5f8aSAndrey V. Elsukov break; 431*0b9f5f8aSAndrey V. Elsukov #endif 432*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 433*0b9f5f8aSAndrey V. Elsukov case AF_INET6: 434*0b9f5f8aSAndrey V. Elsukov error = in6_gif_output(ifp, m, proto, ecn); 435776b7288SRandall Stewart break; 436776b7288SRandall Stewart #endif 437776b7288SRandall Stewart default: 438776b7288SRandall Stewart m_freem(m); 439776b7288SRandall Stewart } 440*0b9f5f8aSAndrey V. Elsukov err: 441776b7288SRandall Stewart if (error) 4423751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 443*0b9f5f8aSAndrey V. Elsukov return (error); 44473ff045cSAndrew Thompson } 445*0b9f5f8aSAndrey V. Elsukov 446*0b9f5f8aSAndrey V. Elsukov static void 447*0b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused) 448*0b9f5f8aSAndrey V. Elsukov { 449*0b9f5f8aSAndrey V. Elsukov 45073ff045cSAndrew Thompson } 45173ff045cSAndrew Thompson 452cfa1ca9dSYoshinobu Inoue int 45347e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 45447e8d432SGleb Smirnoff struct route *ro) 455cfa1ca9dSYoshinobu Inoue { 4568c7e1947SRuslan Ermilov struct m_tag *mtag; 457776b7288SRandall Stewart uint32_t af; 458*0b9f5f8aSAndrey V. Elsukov int gif_called; 459*0b9f5f8aSAndrey V. Elsukov int error = 0; 46010722b85SRobert Watson #ifdef MAC 46130d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m); 462*0b9f5f8aSAndrey V. Elsukov if (error) 463*0b9f5f8aSAndrey V. Elsukov goto err; 46410722b85SRobert Watson #endif 465*0b9f5f8aSAndrey V. Elsukov if ((ifp->if_flags & IFF_MONITOR) != 0 || 466*0b9f5f8aSAndrey V. Elsukov (ifp->if_flags & IFF_UP) == 0) { 467e9f947e2SHiroki Sato error = ENETDOWN; 468*0b9f5f8aSAndrey V. Elsukov goto err; 469e9f947e2SHiroki Sato } 47010722b85SRobert Watson 471cfa1ca9dSYoshinobu Inoue /* 472cfa1ca9dSYoshinobu Inoue * gif may cause infinite recursion calls when misconfigured. 4738c7e1947SRuslan Ermilov * We'll prevent this by detecting loops. 4748c7e1947SRuslan Ermilov * 4758c7e1947SRuslan Ermilov * High nesting level may cause stack exhaustion. 476cfa1ca9dSYoshinobu Inoue * We'll prevent this by introducing upper limit. 477cfa1ca9dSYoshinobu Inoue */ 4788c7e1947SRuslan Ermilov gif_called = 1; 4798c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL); 4808c7e1947SRuslan Ermilov while (mtag != NULL) { 4818c7e1947SRuslan Ermilov if (*(struct ifnet **)(mtag + 1) == ifp) { 4828c7e1947SRuslan Ermilov log(LOG_NOTICE, 4838c7e1947SRuslan Ermilov "gif_output: loop detected on %s\n", 4848c7e1947SRuslan Ermilov (*(struct ifnet **)(mtag + 1))->if_xname); 4858c7e1947SRuslan Ermilov error = EIO; /* is there better errno? */ 486*0b9f5f8aSAndrey V. Elsukov goto err; 4878c7e1947SRuslan Ermilov } 4888c7e1947SRuslan Ermilov mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag); 4898c7e1947SRuslan Ermilov gif_called++; 4908c7e1947SRuslan Ermilov } 491603724d3SBjoern A. Zeeb if (gif_called > V_max_gif_nesting) { 492cfa1ca9dSYoshinobu Inoue log(LOG_NOTICE, 493cfa1ca9dSYoshinobu Inoue "gif_output: recursively called too many times(%d)\n", 494523ebc4eSRobert Watson gif_called); 495cfa1ca9dSYoshinobu Inoue error = EIO; /* is there better errno? */ 496*0b9f5f8aSAndrey V. Elsukov goto err; 497cfa1ca9dSYoshinobu Inoue } 4988c7e1947SRuslan Ermilov mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *), 4998c7e1947SRuslan Ermilov M_NOWAIT); 5008c7e1947SRuslan Ermilov if (mtag == NULL) { 5018c7e1947SRuslan Ermilov error = ENOMEM; 502*0b9f5f8aSAndrey V. Elsukov goto err; 5038c7e1947SRuslan Ermilov } 5048c7e1947SRuslan Ermilov *(struct ifnet **)(mtag + 1) = ifp; 5058c7e1947SRuslan Ermilov m_tag_prepend(m, mtag); 506686cdd19SJun-ichiro itojun Hagino 507cfa1ca9dSYoshinobu Inoue m->m_flags &= ~(M_BCAST|M_MCAST); 50847e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 50901399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 51047e8d432SGleb Smirnoff else 51101399f34SDavid Malone af = dst->sa_family; 512*0b9f5f8aSAndrey V. Elsukov if (ifp->if_bridge) 513*0b9f5f8aSAndrey V. Elsukov af = AF_LINK; 51440138788SRandall Stewart /* 515*0b9f5f8aSAndrey V. Elsukov * Now save the af in the inbound pkt csum data, this is a cheat since 516*0b9f5f8aSAndrey V. Elsukov * we are using the inbound csum_data field to carry the af over to 517*0b9f5f8aSAndrey V. Elsukov * the gif_transmit() routine, avoiding using yet another mtag. 518776b7288SRandall Stewart */ 519cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 520*0b9f5f8aSAndrey V. Elsukov return (ifp->if_transmit(ifp, m)); 521*0b9f5f8aSAndrey V. Elsukov err: 5223751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 523*0b9f5f8aSAndrey V. Elsukov m_freem(m); 52425af0bb5SGleb Smirnoff return (error); 525cfa1ca9dSYoshinobu Inoue } 526cfa1ca9dSYoshinobu Inoue 527cfa1ca9dSYoshinobu Inoue void 528*0b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn) 529cfa1ca9dSYoshinobu Inoue { 53073ff045cSAndrew Thompson struct etherip_header *eip; 531*0b9f5f8aSAndrey V. Elsukov #ifdef INET 532*0b9f5f8aSAndrey V. Elsukov struct ip *ip; 533*0b9f5f8aSAndrey V. Elsukov #endif 534*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 535*0b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 536*0b9f5f8aSAndrey V. Elsukov uint32_t t; 537*0b9f5f8aSAndrey V. Elsukov #endif 538*0b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 53956abdd33SAndrew Thompson struct ether_header *eh; 54056abdd33SAndrew Thompson struct ifnet *oldifp; 541*0b9f5f8aSAndrey V. Elsukov uint32_t gif_options; 542*0b9f5f8aSAndrey V. Elsukov int isr, n, af; 543cfa1ca9dSYoshinobu Inoue 54421fb391fSHajimu UMEMOTO if (ifp == NULL) { 545cfa1ca9dSYoshinobu Inoue /* just in case */ 546cfa1ca9dSYoshinobu Inoue m_freem(m); 547cfa1ca9dSYoshinobu Inoue return; 548cfa1ca9dSYoshinobu Inoue } 5494382b068SChristian Brueffer sc = ifp->if_softc; 550*0b9f5f8aSAndrey V. Elsukov gif_options = sc->gif_options; 55121fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 5525b7a43f5SAndrey V. Elsukov m_clrprotoflags(m); 553*0b9f5f8aSAndrey V. Elsukov switch (proto) { 554*0b9f5f8aSAndrey V. Elsukov #ifdef INET 555*0b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV4: 556*0b9f5f8aSAndrey V. Elsukov af = AF_INET; 557*0b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 558*0b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 559*0b9f5f8aSAndrey V. Elsukov if (m == NULL) 560*0b9f5f8aSAndrey V. Elsukov goto drop; 561*0b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 562*0b9f5f8aSAndrey V. Elsukov if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 563*0b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos) == 0) { 564*0b9f5f8aSAndrey V. Elsukov m_freem(m); 565*0b9f5f8aSAndrey V. Elsukov goto drop; 566*0b9f5f8aSAndrey V. Elsukov } 567*0b9f5f8aSAndrey V. Elsukov break; 568*0b9f5f8aSAndrey V. Elsukov #endif 569*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 570*0b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV6: 571*0b9f5f8aSAndrey V. Elsukov af = AF_INET6; 572*0b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 573*0b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 574*0b9f5f8aSAndrey V. Elsukov if (m == NULL) 575*0b9f5f8aSAndrey V. Elsukov goto drop; 576*0b9f5f8aSAndrey V. Elsukov t = htonl((uint32_t)ecn << 20); 577*0b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 578*0b9f5f8aSAndrey V. Elsukov if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 579*0b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow) == 0) { 580*0b9f5f8aSAndrey V. Elsukov m_freem(m); 581*0b9f5f8aSAndrey V. Elsukov goto drop; 582*0b9f5f8aSAndrey V. Elsukov } 583*0b9f5f8aSAndrey V. Elsukov break; 584*0b9f5f8aSAndrey V. Elsukov #endif 585*0b9f5f8aSAndrey V. Elsukov case IPPROTO_ETHERIP: 586*0b9f5f8aSAndrey V. Elsukov af = AF_LINK; 587*0b9f5f8aSAndrey V. Elsukov break; 588*0b9f5f8aSAndrey V. Elsukov default: 589*0b9f5f8aSAndrey V. Elsukov m_freem(m); 590*0b9f5f8aSAndrey V. Elsukov goto drop; 591*0b9f5f8aSAndrey V. Elsukov } 592cfa1ca9dSYoshinobu Inoue 59310722b85SRobert Watson #ifdef MAC 59430d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 59510722b85SRobert Watson #endif 59610722b85SRobert Watson 59716d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 598*0b9f5f8aSAndrey V. Elsukov uint32_t af1 = af; 599437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 600cfa1ca9dSYoshinobu Inoue } 601cfa1ca9dSYoshinobu Inoue 602e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 6033751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 6043751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 605e9f947e2SHiroki Sato m_freem(m); 606e9f947e2SHiroki Sato return; 607e9f947e2SHiroki Sato } 608e9f947e2SHiroki Sato 60994408d94SBrooks Davis if (ng_gif_input_p != NULL) { 61021fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 61194408d94SBrooks Davis if (m == NULL) 612*0b9f5f8aSAndrey V. Elsukov goto drop; 61394408d94SBrooks Davis } 61494408d94SBrooks Davis 615cfa1ca9dSYoshinobu Inoue /* 616cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 617cfa1ca9dSYoshinobu Inoue * specified address family. 618cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 619cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 620cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 621cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 622cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 62388ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 62488ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 625cfa1ca9dSYoshinobu Inoue */ 626cfa1ca9dSYoshinobu Inoue switch (af) { 627cfa1ca9dSYoshinobu Inoue #ifdef INET 628cfa1ca9dSYoshinobu Inoue case AF_INET: 629cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 630cfa1ca9dSYoshinobu Inoue break; 631cfa1ca9dSYoshinobu Inoue #endif 632cfa1ca9dSYoshinobu Inoue #ifdef INET6 633cfa1ca9dSYoshinobu Inoue case AF_INET6: 634cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 635cfa1ca9dSYoshinobu Inoue break; 636cfa1ca9dSYoshinobu Inoue #endif 63773ff045cSAndrew Thompson case AF_LINK: 63873ff045cSAndrew Thompson n = sizeof(struct etherip_header) + sizeof(struct ether_header); 639*0b9f5f8aSAndrey V. Elsukov if (n > m->m_len) 64073ff045cSAndrew Thompson m = m_pullup(m, n); 641*0b9f5f8aSAndrey V. Elsukov if (m == NULL) 642*0b9f5f8aSAndrey V. Elsukov goto drop; 64373ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 644dbe59260SHiroki Sato /* 645dbe59260SHiroki Sato * GIF_ACCEPT_REVETHIP (enabled by default) intentionally 646dbe59260SHiroki Sato * accepts an EtherIP packet with revered version field in 647dbe59260SHiroki Sato * the header. This is a knob for backward compatibility 648dbe59260SHiroki Sato * with FreeBSD 7.2R or prior. 649dbe59260SHiroki Sato */ 650dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 651*0b9f5f8aSAndrey V. Elsukov if ((gif_options & GIF_ACCEPT_REVETHIP) == 0 || 652*0b9f5f8aSAndrey V. Elsukov eip->eip_resvl != ETHERIP_VERSION) { 653dbe59260SHiroki Sato /* discard unknown versions */ 654dbe59260SHiroki Sato m_freem(m); 655*0b9f5f8aSAndrey V. Elsukov goto drop; 656dbe59260SHiroki Sato } 657dbe59260SHiroki Sato } 65873ff045cSAndrew Thompson m_adj(m, sizeof(struct etherip_header)); 65973ff045cSAndrew Thompson 66073ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 66173ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 66273ff045cSAndrew Thompson 66356abdd33SAndrew Thompson if (ifp->if_bridge) { 66456abdd33SAndrew Thompson oldifp = ifp; 66556abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 66656abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 66756abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 66856abdd33SAndrew Thompson m->m_flags |= M_BCAST; 66956abdd33SAndrew Thompson else 67056abdd33SAndrew Thompson m->m_flags |= M_MCAST; 6713751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 67256abdd33SAndrew Thompson } 67373ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 67473ff045cSAndrew Thompson 67556abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 67656abdd33SAndrew Thompson /* 67756abdd33SAndrew Thompson * The bridge gave us back itself or one of the 67856abdd33SAndrew Thompson * members for which the frame is addressed. 67956abdd33SAndrew Thompson */ 68056abdd33SAndrew Thompson ether_demux(ifp, m); 68156abdd33SAndrew Thompson return; 68256abdd33SAndrew Thompson } 68356abdd33SAndrew Thompson } 68473ff045cSAndrew Thompson if (m != NULL) 68573ff045cSAndrew Thompson m_freem(m); 68673ff045cSAndrew Thompson return; 68773ff045cSAndrew Thompson 688cfa1ca9dSYoshinobu Inoue default: 68994408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 69021fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 69194408d94SBrooks Davis else 692cfa1ca9dSYoshinobu Inoue m_freem(m); 693cfa1ca9dSYoshinobu Inoue return; 694cfa1ca9dSYoshinobu Inoue } 695cfa1ca9dSYoshinobu Inoue 6963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 6973751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 698a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 6991cafed39SJonathan Lemon netisr_dispatch(isr, m); 700*0b9f5f8aSAndrey V. Elsukov return; 701*0b9f5f8aSAndrey V. Elsukov drop: 702*0b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 703cfa1ca9dSYoshinobu Inoue } 704cfa1ca9dSYoshinobu Inoue 705686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */ 706cfa1ca9dSYoshinobu Inoue int 707c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 708cfa1ca9dSYoshinobu Inoue { 709*0b9f5f8aSAndrey V. Elsukov GIF_RLOCK_TRACKER; 710cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 711686cdd19SJun-ichiro itojun Hagino struct sockaddr *dst, *src; 712*0b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 713*0b9f5f8aSAndrey V. Elsukov #ifdef INET 714*0b9f5f8aSAndrey V. Elsukov struct sockaddr_in *sin = NULL; 7153bb61ca6SHajimu UMEMOTO #endif 716*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 717*0b9f5f8aSAndrey V. Elsukov struct sockaddr_in6 *sin6 = NULL; 718*0b9f5f8aSAndrey V. Elsukov #endif 719*0b9f5f8aSAndrey V. Elsukov u_int options; 720*0b9f5f8aSAndrey V. Elsukov int error; 721cfa1ca9dSYoshinobu Inoue 722cfa1ca9dSYoshinobu Inoue switch (cmd) { 723cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 7249426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 725cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 726cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 727cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 728*0b9f5f8aSAndrey V. Elsukov case SIOCSIFFLAGS: 729*0b9f5f8aSAndrey V. Elsukov return (0); 730cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 731*0b9f5f8aSAndrey V. Elsukov if (ifr->ifr_mtu < GIF_MTU_MIN || 732*0b9f5f8aSAndrey V. Elsukov ifr->ifr_mtu > GIF_MTU_MAX) 733cfa1ca9dSYoshinobu Inoue return (EINVAL); 734*0b9f5f8aSAndrey V. Elsukov else 735*0b9f5f8aSAndrey V. Elsukov ifp->if_mtu = ifr->ifr_mtu; 736*0b9f5f8aSAndrey V. Elsukov return (0); 737*0b9f5f8aSAndrey V. Elsukov } 738*0b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 739*0b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 740*0b9f5f8aSAndrey V. Elsukov if (sc == NULL) { 741*0b9f5f8aSAndrey V. Elsukov error = ENXIO; 742*0b9f5f8aSAndrey V. Elsukov goto bad; 743*0b9f5f8aSAndrey V. Elsukov } 744*0b9f5f8aSAndrey V. Elsukov error = 0; 745*0b9f5f8aSAndrey V. Elsukov switch (cmd) { 746cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR: 747cfa1ca9dSYoshinobu Inoue case SIOCSIFPHYADDR_IN6: 748*0b9f5f8aSAndrey V. Elsukov error = EINVAL; 749686cdd19SJun-ichiro itojun Hagino switch (cmd) { 75033841545SHajimu UMEMOTO #ifdef INET 751686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 752cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 753cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_addr); 754cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 755cfa1ca9dSYoshinobu Inoue &(((struct in_aliasreq *)data)->ifra_dstaddr); 756cfa1ca9dSYoshinobu Inoue break; 75733841545SHajimu UMEMOTO #endif 758cfa1ca9dSYoshinobu Inoue #ifdef INET6 759686cdd19SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 760cfa1ca9dSYoshinobu Inoue src = (struct sockaddr *) 761cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_addr); 762cfa1ca9dSYoshinobu Inoue dst = (struct sockaddr *) 763cfa1ca9dSYoshinobu Inoue &(((struct in6_aliasreq *)data)->ifra_dstaddr); 764686cdd19SJun-ichiro itojun Hagino break; 765686cdd19SJun-ichiro itojun Hagino #endif 7666f4ded3aSBrooks Davis default: 767*0b9f5f8aSAndrey V. Elsukov goto bad; 76833841545SHajimu UMEMOTO } 76933841545SHajimu UMEMOTO /* sa_family must be equal */ 770*0b9f5f8aSAndrey V. Elsukov if (src->sa_family != dst->sa_family || 771*0b9f5f8aSAndrey V. Elsukov src->sa_len != dst->sa_len) 772*0b9f5f8aSAndrey V. Elsukov goto bad; 77333841545SHajimu UMEMOTO 77433841545SHajimu UMEMOTO /* validate sa_len */ 77533841545SHajimu UMEMOTO switch (src->sa_family) { 77633841545SHajimu UMEMOTO #ifdef INET 77733841545SHajimu UMEMOTO case AF_INET: 77833841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in)) 779*0b9f5f8aSAndrey V. Elsukov goto bad; 78033841545SHajimu UMEMOTO break; 78133841545SHajimu UMEMOTO #endif 78233841545SHajimu UMEMOTO #ifdef INET6 78333841545SHajimu UMEMOTO case AF_INET6: 78433841545SHajimu UMEMOTO if (src->sa_len != sizeof(struct sockaddr_in6)) 785*0b9f5f8aSAndrey V. Elsukov goto bad; 78633841545SHajimu UMEMOTO break; 78733841545SHajimu UMEMOTO #endif 78833841545SHajimu UMEMOTO default: 789*0b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 790*0b9f5f8aSAndrey V. Elsukov goto bad; 79133841545SHajimu UMEMOTO } 79233841545SHajimu UMEMOTO /* check sa_family looks sane for the cmd */ 793*0b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 79433841545SHajimu UMEMOTO switch (cmd) { 795*0b9f5f8aSAndrey V. Elsukov #ifdef INET 79633841545SHajimu UMEMOTO case SIOCSIFPHYADDR: 79733841545SHajimu UMEMOTO if (src->sa_family == AF_INET) 79833841545SHajimu UMEMOTO break; 799*0b9f5f8aSAndrey V. Elsukov goto bad; 800*0b9f5f8aSAndrey V. Elsukov #endif 80133841545SHajimu UMEMOTO #ifdef INET6 80233841545SHajimu UMEMOTO case SIOCSIFPHYADDR_IN6: 80333841545SHajimu UMEMOTO if (src->sa_family == AF_INET6) 80433841545SHajimu UMEMOTO break; 805*0b9f5f8aSAndrey V. Elsukov goto bad; 806*0b9f5f8aSAndrey V. Elsukov #endif 807686cdd19SJun-ichiro itojun Hagino } 808*0b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 809*0b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 810*0b9f5f8aSAndrey V. Elsukov #ifdef INET 811*0b9f5f8aSAndrey V. Elsukov case AF_INET: 812*0b9f5f8aSAndrey V. Elsukov if (satosin(src)->sin_addr.s_addr == INADDR_ANY || 813*0b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr == INADDR_ANY) 814*0b9f5f8aSAndrey V. Elsukov goto bad; 815cfa1ca9dSYoshinobu Inoue break; 816*0b9f5f8aSAndrey V. Elsukov #endif 817*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 818*0b9f5f8aSAndrey V. Elsukov case AF_INET6: 819*0b9f5f8aSAndrey V. Elsukov if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr) 820*0b9f5f8aSAndrey V. Elsukov || 821*0b9f5f8aSAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr)) 822*0b9f5f8aSAndrey V. Elsukov goto bad; 823*0b9f5f8aSAndrey V. Elsukov /* 824*0b9f5f8aSAndrey V. Elsukov * Check validity of the scope zone ID of the 825*0b9f5f8aSAndrey V. Elsukov * addresses, and convert it into the kernel 826*0b9f5f8aSAndrey V. Elsukov * internal form if necessary. 827*0b9f5f8aSAndrey V. Elsukov */ 828*0b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(src), 0); 829*0b9f5f8aSAndrey V. Elsukov if (error != 0) 830*0b9f5f8aSAndrey V. Elsukov goto bad; 831*0b9f5f8aSAndrey V. Elsukov error = sa6_embedscope(satosin6(dst), 0); 832*0b9f5f8aSAndrey V. Elsukov if (error != 0) 833*0b9f5f8aSAndrey V. Elsukov goto bad; 834*0b9f5f8aSAndrey V. Elsukov #endif 835*0b9f5f8aSAndrey V. Elsukov }; 836*0b9f5f8aSAndrey V. Elsukov error = gif_set_tunnel(ifp, src, dst); 837*0b9f5f8aSAndrey V. Elsukov break; 838686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 839*0b9f5f8aSAndrey V. Elsukov gif_delete_tunnel(ifp); 840686cdd19SJun-ichiro itojun Hagino break; 841cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR: 842*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 843cfa1ca9dSYoshinobu Inoue case SIOCGIFPSRCADDR_IN6: 844*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 845*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0) { 846cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 847*0b9f5f8aSAndrey V. Elsukov break; 848cfa1ca9dSYoshinobu Inoue } 849*0b9f5f8aSAndrey V. Elsukov GIF_RLOCK(sc); 85033841545SHajimu UMEMOTO switch (cmd) { 851cfa1ca9dSYoshinobu Inoue #ifdef INET 85233841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 853*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 854*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET) { 855*0b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 856cfa1ca9dSYoshinobu Inoue break; 857*0b9f5f8aSAndrey V. Elsukov } 858*0b9f5f8aSAndrey V. Elsukov sin = (struct sockaddr_in *)&ifr->ifr_addr; 859*0b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 860*0b9f5f8aSAndrey V. Elsukov sin->sin_family = AF_INET; 861*0b9f5f8aSAndrey V. Elsukov sin->sin_len = sizeof(*sin); 862*0b9f5f8aSAndrey V. Elsukov break; 863*0b9f5f8aSAndrey V. Elsukov #endif 864cfa1ca9dSYoshinobu Inoue #ifdef INET6 86533841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 866cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 867*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != AF_INET6) { 868cfa1ca9dSYoshinobu Inoue error = EADDRNOTAVAIL; 869*0b9f5f8aSAndrey V. Elsukov break; 870cfa1ca9dSYoshinobu Inoue } 871*0b9f5f8aSAndrey V. Elsukov sin6 = (struct sockaddr_in6 *) 872*0b9f5f8aSAndrey V. Elsukov &(((struct in6_ifreq *)data)->ifr_addr); 873*0b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 874*0b9f5f8aSAndrey V. Elsukov sin6->sin6_family = AF_INET6; 875*0b9f5f8aSAndrey V. Elsukov sin6->sin6_len = sizeof(*sin6); 876*0b9f5f8aSAndrey V. Elsukov break; 877*0b9f5f8aSAndrey V. Elsukov #endif 878*0b9f5f8aSAndrey V. Elsukov default: 879*0b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 880*0b9f5f8aSAndrey V. Elsukov } 881*0b9f5f8aSAndrey V. Elsukov if (error == 0) { 88233841545SHajimu UMEMOTO switch (cmd) { 883cfa1ca9dSYoshinobu Inoue #ifdef INET 884*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 885*0b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_src; 886*0b9f5f8aSAndrey V. Elsukov break; 88733841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 888*0b9f5f8aSAndrey V. Elsukov sin->sin_addr = sc->gif_iphdr->ip_dst; 889cfa1ca9dSYoshinobu Inoue break; 890a1f7e5f8SHajimu UMEMOTO #endif 891*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 892*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 893*0b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_src; 89433841545SHajimu UMEMOTO break; 895*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 896*0b9f5f8aSAndrey V. Elsukov sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst; 897cfa1ca9dSYoshinobu Inoue break; 898*0b9f5f8aSAndrey V. Elsukov #endif 899*0b9f5f8aSAndrey V. Elsukov } 900*0b9f5f8aSAndrey V. Elsukov } 901*0b9f5f8aSAndrey V. Elsukov GIF_RUNLOCK(sc); 902*0b9f5f8aSAndrey V. Elsukov if (error != 0) 903*0b9f5f8aSAndrey V. Elsukov break; 904*0b9f5f8aSAndrey V. Elsukov switch (cmd) { 905*0b9f5f8aSAndrey V. Elsukov #ifdef INET 906*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR: 907*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 908*0b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 909*0b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin); 910*0b9f5f8aSAndrey V. Elsukov if (error != 0) 911*0b9f5f8aSAndrey V. Elsukov memset(sin, 0, sizeof(*sin)); 912*0b9f5f8aSAndrey V. Elsukov break; 913*0b9f5f8aSAndrey V. Elsukov #endif 914*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 915*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6: 916*0b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6: 917*0b9f5f8aSAndrey V. Elsukov error = prison_if(curthread->td_ucred, 918*0b9f5f8aSAndrey V. Elsukov (struct sockaddr *)sin6); 919*0b9f5f8aSAndrey V. Elsukov if (error == 0) 920*0b9f5f8aSAndrey V. Elsukov error = sa6_recoverscope(sin6); 921*0b9f5f8aSAndrey V. Elsukov #endif 922*0b9f5f8aSAndrey V. Elsukov if (error != 0) 923*0b9f5f8aSAndrey V. Elsukov memset(sin6, 0, sizeof(*sin6)); 924*0b9f5f8aSAndrey V. Elsukov } 925*0b9f5f8aSAndrey V. Elsukov break; 926dbe59260SHiroki Sato case GIFGOPTS: 927dbe59260SHiroki Sato options = sc->gif_options; 928*0b9f5f8aSAndrey V. Elsukov error = copyout(&options, ifr->ifr_data, sizeof(options)); 929dbe59260SHiroki Sato break; 930dbe59260SHiroki Sato case GIFSOPTS: 931dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 932dbe59260SHiroki Sato break; 9334cd5f57dSHiroki Sato error = copyin(ifr->ifr_data, &options, sizeof(options)); 9344cd5f57dSHiroki Sato if (error) 9354cd5f57dSHiroki Sato break; 9364cd5f57dSHiroki Sato if (options & ~GIF_OPTMASK) 937dbe59260SHiroki Sato error = EINVAL; 9384cd5f57dSHiroki Sato else 9394cd5f57dSHiroki Sato sc->gif_options = options; 940dbe59260SHiroki Sato break; 941dbe59260SHiroki Sato 942cfa1ca9dSYoshinobu Inoue default: 943cfa1ca9dSYoshinobu Inoue error = EINVAL; 944cfa1ca9dSYoshinobu Inoue break; 945cfa1ca9dSYoshinobu Inoue } 946cfa1ca9dSYoshinobu Inoue bad: 947*0b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 948*0b9f5f8aSAndrey V. Elsukov return (error); 949cfa1ca9dSYoshinobu Inoue } 95053dab5feSBrooks Davis 951*0b9f5f8aSAndrey V. Elsukov static void 952*0b9f5f8aSAndrey V. Elsukov gif_detach(struct gif_softc *sc) 953*0b9f5f8aSAndrey V. Elsukov { 954*0b9f5f8aSAndrey V. Elsukov 955*0b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 956*0b9f5f8aSAndrey V. Elsukov if (sc->gif_ecookie != NULL) 957*0b9f5f8aSAndrey V. Elsukov encap_detach(sc->gif_ecookie); 958*0b9f5f8aSAndrey V. Elsukov sc->gif_ecookie = NULL; 959*0b9f5f8aSAndrey V. Elsukov } 960*0b9f5f8aSAndrey V. Elsukov 961*0b9f5f8aSAndrey V. Elsukov static int 962*0b9f5f8aSAndrey V. Elsukov gif_attach(struct gif_softc *sc, int af) 963*0b9f5f8aSAndrey V. Elsukov { 964*0b9f5f8aSAndrey V. Elsukov 965*0b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 966*0b9f5f8aSAndrey V. Elsukov switch (af) { 967*0b9f5f8aSAndrey V. Elsukov #ifdef INET 968*0b9f5f8aSAndrey V. Elsukov case AF_INET: 969*0b9f5f8aSAndrey V. Elsukov return (in_gif_attach(sc)); 970*0b9f5f8aSAndrey V. Elsukov #endif 971*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 972*0b9f5f8aSAndrey V. Elsukov case AF_INET6: 973*0b9f5f8aSAndrey V. Elsukov return (in6_gif_attach(sc)); 974*0b9f5f8aSAndrey V. Elsukov #endif 975*0b9f5f8aSAndrey V. Elsukov } 976*0b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 977*0b9f5f8aSAndrey V. Elsukov } 978*0b9f5f8aSAndrey V. Elsukov 979*0b9f5f8aSAndrey V. Elsukov static int 980c72a5d5dSAndrey V. Elsukov gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst) 98153dab5feSBrooks Davis { 982fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 983*0b9f5f8aSAndrey V. Elsukov struct gif_softc *tsc; 984*0b9f5f8aSAndrey V. Elsukov #ifdef INET 985*0b9f5f8aSAndrey V. Elsukov struct ip *ip; 986*0b9f5f8aSAndrey V. Elsukov #endif 987*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 988*0b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 989*0b9f5f8aSAndrey V. Elsukov #endif 990*0b9f5f8aSAndrey V. Elsukov void *hdr; 9919426aedfSHajimu UMEMOTO int error = 0; 9929426aedfSHajimu UMEMOTO 993*0b9f5f8aSAndrey V. Elsukov if (sc == NULL) 994*0b9f5f8aSAndrey V. Elsukov return (ENXIO); 995*0b9f5f8aSAndrey V. Elsukov /* Disallow parallel tunnels unless instructed otherwise. */ 996*0b9f5f8aSAndrey V. Elsukov if (V_parallel_tunnels == 0) { 997a7f5886eSHiroki Sato GIF_LIST_LOCK(); 998*0b9f5f8aSAndrey V. Elsukov LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) { 999*0b9f5f8aSAndrey V. Elsukov if (tsc == sc || tsc->gif_family != src->sa_family) 10009426aedfSHajimu UMEMOTO continue; 1001*0b9f5f8aSAndrey V. Elsukov #ifdef INET 1002*0b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET && 1003*0b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_src.s_addr == 1004*0b9f5f8aSAndrey V. Elsukov satosin(src)->sin_addr.s_addr && 1005*0b9f5f8aSAndrey V. Elsukov tsc->gif_iphdr->ip_dst.s_addr == 1006*0b9f5f8aSAndrey V. Elsukov satosin(dst)->sin_addr.s_addr) { 10079426aedfSHajimu UMEMOTO error = EADDRNOTAVAIL; 1008a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10099426aedfSHajimu UMEMOTO goto bad; 10109426aedfSHajimu UMEMOTO } 1011*0b9f5f8aSAndrey V. Elsukov #endif 1012*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 1013*0b9f5f8aSAndrey V. Elsukov if (tsc->gif_family == AF_INET6 && 1014*0b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src, 1015*0b9f5f8aSAndrey V. Elsukov &satosin6(src)->sin6_addr) && 1016*0b9f5f8aSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst, 1017*0b9f5f8aSAndrey V. Elsukov &satosin6(dst)->sin6_addr)) { 1018*0b9f5f8aSAndrey V. Elsukov error = EADDRNOTAVAIL; 1019a7f5886eSHiroki Sato GIF_LIST_UNLOCK(); 10209426aedfSHajimu UMEMOTO goto bad; 10219426aedfSHajimu UMEMOTO } 1022*0b9f5f8aSAndrey V. Elsukov #endif 1023*0b9f5f8aSAndrey V. Elsukov } 1024*0b9f5f8aSAndrey V. Elsukov GIF_LIST_UNLOCK(); 1025*0b9f5f8aSAndrey V. Elsukov } 1026*0b9f5f8aSAndrey V. Elsukov switch (src->sa_family) { 1027*0b9f5f8aSAndrey V. Elsukov #ifdef INET 1028*0b9f5f8aSAndrey V. Elsukov case AF_INET: 1029*0b9f5f8aSAndrey V. Elsukov hdr = ip = malloc(sizeof(struct ip), M_GIF, 1030*0b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 1031*0b9f5f8aSAndrey V. Elsukov ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr; 1032*0b9f5f8aSAndrey V. Elsukov ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr; 1033*0b9f5f8aSAndrey V. Elsukov break; 1034*0b9f5f8aSAndrey V. Elsukov #endif 1035*0b9f5f8aSAndrey V. Elsukov #ifdef INET6 1036*0b9f5f8aSAndrey V. Elsukov case AF_INET6: 1037*0b9f5f8aSAndrey V. Elsukov hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF, 1038*0b9f5f8aSAndrey V. Elsukov M_WAITOK | M_ZERO); 1039*0b9f5f8aSAndrey V. Elsukov ip6->ip6_src = satosin6(src)->sin6_addr; 1040*0b9f5f8aSAndrey V. Elsukov ip6->ip6_dst = satosin6(dst)->sin6_addr; 1041*0b9f5f8aSAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION; 1042*0b9f5f8aSAndrey V. Elsukov break; 1043*0b9f5f8aSAndrey V. Elsukov #endif 1044*0b9f5f8aSAndrey V. Elsukov default: 1045*0b9f5f8aSAndrey V. Elsukov return (EAFNOSUPPORT); 1046*0b9f5f8aSAndrey V. Elsukov }; 10479426aedfSHajimu UMEMOTO 1048*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != src->sa_family) 1049*0b9f5f8aSAndrey V. Elsukov gif_detach(sc); 1050*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family == 0 || 1051*0b9f5f8aSAndrey V. Elsukov sc->gif_family != src->sa_family) 1052*0b9f5f8aSAndrey V. Elsukov error = gif_attach(sc, src->sa_family); 10539426aedfSHajimu UMEMOTO 1054*0b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 1055*0b9f5f8aSAndrey V. Elsukov if (sc->gif_family != 0) 1056*0b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 1057*0b9f5f8aSAndrey V. Elsukov sc->gif_family = src->sa_family; 1058*0b9f5f8aSAndrey V. Elsukov sc->gif_hdr = hdr; 1059*0b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 10609426aedfSHajimu UMEMOTO bad: 1061*0b9f5f8aSAndrey V. Elsukov if (error == 0 && sc->gif_family != 0) 106213f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 10639426aedfSHajimu UMEMOTO else 106413f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1065*0b9f5f8aSAndrey V. Elsukov return (error); 10669426aedfSHajimu UMEMOTO } 10679426aedfSHajimu UMEMOTO 1068*0b9f5f8aSAndrey V. Elsukov static void 1069c72a5d5dSAndrey V. Elsukov gif_delete_tunnel(struct ifnet *ifp) 10709426aedfSHajimu UMEMOTO { 1071fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc; 1072*0b9f5f8aSAndrey V. Elsukov int family; 107353dab5feSBrooks Davis 1074*0b9f5f8aSAndrey V. Elsukov if (sc == NULL) 1075*0b9f5f8aSAndrey V. Elsukov return; 1076*0b9f5f8aSAndrey V. Elsukov 1077*0b9f5f8aSAndrey V. Elsukov GIF_WLOCK(sc); 1078*0b9f5f8aSAndrey V. Elsukov family = sc->gif_family; 1079*0b9f5f8aSAndrey V. Elsukov sc->gif_family = 0; 1080*0b9f5f8aSAndrey V. Elsukov GIF_WUNLOCK(sc); 1081*0b9f5f8aSAndrey V. Elsukov if (family != 0) { 1082*0b9f5f8aSAndrey V. Elsukov gif_detach(sc); 1083*0b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 108453dab5feSBrooks Davis } 108513f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 108653dab5feSBrooks Davis } 1087