1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4cfa1ca9dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5b941bc1dSAndrey V. Elsukov * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org> 6cfa1ca9dSYoshinobu Inoue * All rights reserved. 7cfa1ca9dSYoshinobu Inoue * 8cfa1ca9dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 9cfa1ca9dSYoshinobu Inoue * modification, are permitted provided that the following conditions 10cfa1ca9dSYoshinobu Inoue * are met: 11cfa1ca9dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 12cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 13cfa1ca9dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 14cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 15cfa1ca9dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 16cfa1ca9dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 17cfa1ca9dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 18cfa1ca9dSYoshinobu Inoue * without specific prior written permission. 19cfa1ca9dSYoshinobu Inoue * 20cfa1ca9dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21cfa1ca9dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22cfa1ca9dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23cfa1ca9dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24cfa1ca9dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25cfa1ca9dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26cfa1ca9dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27cfa1ca9dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28cfa1ca9dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29cfa1ca9dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30cfa1ca9dSYoshinobu Inoue * SUCH DAMAGE. 310b9f5f8aSAndrey V. Elsukov * 320b9f5f8aSAndrey V. Elsukov * $KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $ 33cfa1ca9dSYoshinobu Inoue */ 34cfa1ca9dSYoshinobu Inoue 350b9f5f8aSAndrey V. Elsukov #include <sys/cdefs.h> 360b9f5f8aSAndrey V. Elsukov __FBSDID("$FreeBSD$"); 370b9f5f8aSAndrey V. Elsukov 38cfa1ca9dSYoshinobu Inoue #include "opt_inet.h" 39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 40cfa1ca9dSYoshinobu Inoue 41cfa1ca9dSYoshinobu Inoue #include <sys/param.h> 42cfa1ca9dSYoshinobu Inoue #include <sys/systm.h> 43cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h> 440b9f5f8aSAndrey V. Elsukov #include <sys/lock.h> 45cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h> 46cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h> 475dba30f1SPoul-Henning Kamp #include <sys/module.h> 480b9f5f8aSAndrey V. Elsukov #include <sys/rmlock.h> 49cfa1ca9dSYoshinobu Inoue #include <sys/socket.h> 50cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h> 510b9f5f8aSAndrey V. Elsukov #include <sys/sx.h> 52cfa1ca9dSYoshinobu Inoue #include <sys/errno.h> 53cfa1ca9dSYoshinobu Inoue #include <sys/time.h> 54872f786aSBrooks Davis #include <sys/sysctl.h> 55cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h> 56dbe59260SHiroki Sato #include <sys/priv.h> 578b07e49aSJulian Elischer #include <sys/proc.h> 5853dab5feSBrooks Davis #include <sys/conf.h> 59cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h> 60cfa1ca9dSYoshinobu Inoue 61cfa1ca9dSYoshinobu Inoue #include <net/if.h> 6276039bc8SGleb Smirnoff #include <net/if_var.h> 63f889d2efSBrooks Davis #include <net/if_clone.h> 64cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 65cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 66cfa1ca9dSYoshinobu Inoue #include <net/route.h> 67cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 68530c0060SRobert Watson #include <net/vnet.h> 69cfa1ca9dSYoshinobu Inoue 70cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 71cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 72cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 730b9f5f8aSAndrey V. Elsukov #include <netinet/ip_ecn.h> 7433841545SHajimu UMEMOTO #ifdef INET 7533841545SHajimu UMEMOTO #include <netinet/in_var.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> 87cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 88cfa1ca9dSYoshinobu Inoue 89686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 9073ff045cSAndrew Thompson #include <net/ethernet.h> 9173ff045cSAndrew Thompson #include <net/if_bridgevar.h> 92cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 93cfa1ca9dSYoshinobu Inoue 94aed55708SRobert Watson #include <security/mac/mac_framework.h> 95aed55708SRobert Watson 9642a58907SGleb Smirnoff static const char gifname[] = "gif"; 97686cdd19SJun-ichiro itojun Hagino 98b941bc1dSAndrey V. Elsukov MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 990b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx; 1000b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl"); 101eddfbb76SRobert Watson 10294408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10394408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10494408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10594408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10694408d94SBrooks Davis 107dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 108dd4490fdSAndrey V. Elsukov static void gif_reassign(struct ifnet *, struct vnet *, char *); 109dd4490fdSAndrey V. Elsukov #endif 110b941bc1dSAndrey V. Elsukov static void gif_delete_tunnel(struct gif_softc *); 1110b9f5f8aSAndrey V. Elsukov static int gif_ioctl(struct ifnet *, u_long, caddr_t); 1120b9f5f8aSAndrey V. Elsukov static int gif_transmit(struct ifnet *, struct mbuf *); 1130b9f5f8aSAndrey V. Elsukov static void gif_qflush(struct ifnet *); 1146b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 115bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 1165f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, gif_cloner); 117a7f5886eSHiroki Sato #define V_gif_cloner VNET(gif_cloner) 11853dab5feSBrooks Davis 119872f786aSBrooks Davis SYSCTL_DECL(_net_link); 1207029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 121872f786aSBrooks Davis "Generic Tunnel Interface"); 122686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 123686cdd19SJun-ichiro itojun Hagino /* 124872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 125686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 126686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 127686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 128686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 129686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 130686cdd19SJun-ichiro itojun Hagino */ 131686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 132686cdd19SJun-ichiro itojun Hagino #endif 1335f901c92SAndrew Turner VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST; 134d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 1356df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 136eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1378b615593SMarko Zec 138bb2bfb4fSBrooks Davis static int 139c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 140cfa1ca9dSYoshinobu Inoue { 14133841545SHajimu UMEMOTO struct gif_softc *sc; 142cfa1ca9dSYoshinobu Inoue 143e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1448b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 145fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 146fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 14742a58907SGleb Smirnoff if_initname(GIF2IFP(sc), gifname, unit); 148686cdd19SJun-ichiro itojun Hagino 149fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 150fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 151fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 152fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 1530b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_transmit = gif_transmit; 1540b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_qflush = gif_qflush; 155fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 156dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 157dd4490fdSAndrey V. Elsukov GIF2IFP(sc)->if_reassign = gif_reassign; 158dd4490fdSAndrey V. Elsukov #endif 159f1aaad0cSHiroki Sato GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE; 160f1aaad0cSHiroki Sato GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE; 161fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 16201399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 16394408d94SBrooks Davis if (ng_gif_attach_p != NULL) 164fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 16525af0bb5SGleb Smirnoff 16625af0bb5SGleb Smirnoff return (0); 167cfa1ca9dSYoshinobu Inoue } 168cfa1ca9dSYoshinobu Inoue 169dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 170dd4490fdSAndrey V. Elsukov static void 171dd4490fdSAndrey V. Elsukov gif_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused, 172dd4490fdSAndrey V. Elsukov char *unused __unused) 173dd4490fdSAndrey V. Elsukov { 174dd4490fdSAndrey V. Elsukov struct gif_softc *sc; 175dd4490fdSAndrey V. Elsukov 176dd4490fdSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 177dd4490fdSAndrey V. Elsukov sc = ifp->if_softc; 178dd4490fdSAndrey V. Elsukov if (sc != NULL) 179dd4490fdSAndrey V. Elsukov gif_delete_tunnel(sc); 180dd4490fdSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 181dd4490fdSAndrey V. Elsukov } 182dd4490fdSAndrey V. Elsukov #endif /* VIMAGE */ 183dd4490fdSAndrey V. Elsukov 18417d5cb2dSRobert Watson static void 185c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp) 18653dab5feSBrooks Davis { 1870b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 188febd0759SAndrew Thompson 1890b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 1900b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 191b941bc1dSAndrey V. Elsukov gif_delete_tunnel(sc); 19294408d94SBrooks Davis if (ng_gif_detach_p != NULL) 19394408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 19453dab5feSBrooks Davis bpfdetach(ifp); 19553dab5feSBrooks Davis if_detach(ifp); 1960b9f5f8aSAndrey V. Elsukov ifp->if_softc = NULL; 1970b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 1980b9f5f8aSAndrey V. Elsukov 199b941bc1dSAndrey V. Elsukov GIF_WAIT(); 200fc74a9f9SBrooks Davis if_free(ifp); 20153dab5feSBrooks Davis free(sc, M_GIF); 20253dab5feSBrooks Davis } 20353dab5feSBrooks Davis 204d0728d71SRobert Watson static void 205d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2061ed81b73SMarko Zec { 2071ed81b73SMarko Zec 208a7f5886eSHiroki Sato V_gif_cloner = if_clone_simple(gifname, gif_clone_create, 209a7f5886eSHiroki Sato gif_clone_destroy, 0); 210b941bc1dSAndrey V. Elsukov #ifdef INET 211b941bc1dSAndrey V. Elsukov in_gif_init(); 212b941bc1dSAndrey V. Elsukov #endif 213b941bc1dSAndrey V. Elsukov #ifdef INET6 214b941bc1dSAndrey V. Elsukov in6_gif_init(); 215b941bc1dSAndrey V. Elsukov #endif 2161ed81b73SMarko Zec } 217a7f5886eSHiroki Sato VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 218a7f5886eSHiroki Sato vnet_gif_init, NULL); 219a7f5886eSHiroki Sato 220a7f5886eSHiroki Sato static void 221a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused) 222a7f5886eSHiroki Sato { 223a7f5886eSHiroki Sato 224a7f5886eSHiroki Sato if_clone_detach(V_gif_cloner); 225b941bc1dSAndrey V. Elsukov #ifdef INET 226b941bc1dSAndrey V. Elsukov in_gif_uninit(); 227b941bc1dSAndrey V. Elsukov #endif 228b941bc1dSAndrey V. Elsukov #ifdef INET6 229b941bc1dSAndrey V. Elsukov in6_gif_uninit(); 230b941bc1dSAndrey V. Elsukov #endif 231a7f5886eSHiroki Sato } 232a7f5886eSHiroki Sato VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY, 233a7f5886eSHiroki Sato vnet_gif_uninit, NULL); 2341ed81b73SMarko Zec 2351ed81b73SMarko Zec static int 236c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data) 23753dab5feSBrooks Davis { 23853dab5feSBrooks Davis 23953dab5feSBrooks Davis switch (type) { 24053dab5feSBrooks Davis case MOD_LOAD: 24153dab5feSBrooks Davis case MOD_UNLOAD: 24253dab5feSBrooks Davis break; 2433e019deaSPoul-Henning Kamp default: 244a7f5886eSHiroki Sato return (EOPNOTSUPP); 24553dab5feSBrooks Davis } 246a7f5886eSHiroki Sato return (0); 24753dab5feSBrooks Davis } 24853dab5feSBrooks Davis 24953dab5feSBrooks Davis static moduledata_t gif_mod = { 25053dab5feSBrooks Davis "if_gif", 25153dab5feSBrooks Davis gifmodevent, 2529823d527SKevin Lo 0 25353dab5feSBrooks Davis }; 25453dab5feSBrooks Davis 25553dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 25620af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 257cfa1ca9dSYoshinobu Inoue 258b941bc1dSAndrey V. Elsukov struct gif_list * 259b941bc1dSAndrey V. Elsukov gif_hashinit(void) 260686cdd19SJun-ichiro itojun Hagino { 261b941bc1dSAndrey V. Elsukov struct gif_list *hash; 262b941bc1dSAndrey V. Elsukov int i; 263686cdd19SJun-ichiro itojun Hagino 264b941bc1dSAndrey V. Elsukov hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE, 265b941bc1dSAndrey V. Elsukov M_GIF, M_WAITOK); 266b941bc1dSAndrey V. Elsukov for (i = 0; i < GIF_HASH_SIZE; i++) 267b941bc1dSAndrey V. Elsukov CK_LIST_INIT(&hash[i]); 268686cdd19SJun-ichiro itojun Hagino 269b941bc1dSAndrey V. Elsukov return (hash); 270686cdd19SJun-ichiro itojun Hagino } 271686cdd19SJun-ichiro itojun Hagino 272b941bc1dSAndrey V. Elsukov void 273b941bc1dSAndrey V. Elsukov gif_hashdestroy(struct gif_list *hash) 274b941bc1dSAndrey V. Elsukov { 2753bb61ca6SHajimu UMEMOTO 276b941bc1dSAndrey V. Elsukov free(hash, M_GIF); 277686cdd19SJun-ichiro itojun Hagino } 278686cdd19SJun-ichiro itojun Hagino 27998a8fdf6SAndrey V. Elsukov #define MTAG_GIF 1080679712 2800b9f5f8aSAndrey V. Elsukov static int 2810b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m) 28273ff045cSAndrew Thompson { 28373ff045cSAndrew Thompson struct gif_softc *sc; 2840b9f5f8aSAndrey V. Elsukov struct etherip_header *eth; 285776b7288SRandall Stewart #ifdef INET 2860b9f5f8aSAndrey V. Elsukov struct ip *ip; 287776b7288SRandall Stewart #endif 288776b7288SRandall Stewart #ifdef INET6 2890b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 2900b9f5f8aSAndrey V. Elsukov uint32_t t; 291776b7288SRandall Stewart #endif 2920b9f5f8aSAndrey V. Elsukov uint32_t af; 2930b9f5f8aSAndrey V. Elsukov uint8_t proto, ecn; 2940b9f5f8aSAndrey V. Elsukov int error; 2950b9f5f8aSAndrey V. Elsukov 2969758a507SGleb Smirnoff NET_EPOCH_ASSERT(); 2979c0265c6SAndrey V. Elsukov #ifdef MAC 2989c0265c6SAndrey V. Elsukov error = mac_ifnet_check_transmit(ifp, m); 2999c0265c6SAndrey V. Elsukov if (error) { 3009c0265c6SAndrey V. Elsukov m_freem(m); 3019c0265c6SAndrey V. Elsukov goto err; 3029c0265c6SAndrey V. Elsukov } 3039c0265c6SAndrey V. Elsukov #endif 3040b9f5f8aSAndrey V. Elsukov error = ENETDOWN; 3050b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 3069c0265c6SAndrey V. Elsukov if ((ifp->if_flags & IFF_MONITOR) != 0 || 3079c0265c6SAndrey V. Elsukov (ifp->if_flags & IFF_UP) == 0 || 308009d82eeSAndrey V. Elsukov (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 3099c0265c6SAndrey V. Elsukov sc->gif_family == 0 || 31098a8fdf6SAndrey V. Elsukov (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF, 31198a8fdf6SAndrey V. Elsukov V_max_gif_nesting)) != 0) { 3120b9f5f8aSAndrey V. Elsukov m_freem(m); 3130b9f5f8aSAndrey V. Elsukov goto err; 3140b9f5f8aSAndrey V. Elsukov } 3150b9f5f8aSAndrey V. Elsukov /* Now pull back the af that we stashed in the csum_data. */ 3169c0265c6SAndrey V. Elsukov if (ifp->if_bridge) 3179c0265c6SAndrey V. Elsukov af = AF_LINK; 3189c0265c6SAndrey V. Elsukov else 319cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 3209c0265c6SAndrey V. Elsukov m->m_flags &= ~(M_BCAST|M_MCAST); 3219c0265c6SAndrey V. Elsukov M_SETFIB(m, sc->gif_fibnum); 322776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 3233751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3240b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 325776b7288SRandall Stewart /* inner AF-specific encapsulation */ 3260b9f5f8aSAndrey V. Elsukov ecn = 0; 3270b9f5f8aSAndrey V. Elsukov switch (af) { 328776b7288SRandall Stewart #ifdef INET 329776b7288SRandall Stewart case AF_INET: 3300b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV4; 3310b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 3320b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 3330b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3340b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3350b9f5f8aSAndrey V. Elsukov goto err; 3360b9f5f8aSAndrey V. Elsukov } 3370b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 3380b9f5f8aSAndrey V. Elsukov ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3390b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos); 340776b7288SRandall Stewart break; 341776b7288SRandall Stewart #endif 342776b7288SRandall Stewart #ifdef INET6 343776b7288SRandall Stewart case AF_INET6: 3440b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV6; 3450b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 3460b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 3470b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3480b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3490b9f5f8aSAndrey V. Elsukov goto err; 3500b9f5f8aSAndrey V. Elsukov } 3510b9f5f8aSAndrey V. Elsukov t = 0; 3520b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 3530b9f5f8aSAndrey V. Elsukov ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3540b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow); 3550b9f5f8aSAndrey V. Elsukov ecn = (ntohl(t) >> 20) & 0xff; 3560b9f5f8aSAndrey V. Elsukov break; 3570b9f5f8aSAndrey V. Elsukov #endif 3580b9f5f8aSAndrey V. Elsukov case AF_LINK: 3590b9f5f8aSAndrey V. Elsukov proto = IPPROTO_ETHERIP; 3600b9f5f8aSAndrey V. Elsukov M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 3610b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3620b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3630b9f5f8aSAndrey V. Elsukov goto err; 3640b9f5f8aSAndrey V. Elsukov } 3650b9f5f8aSAndrey V. Elsukov eth = mtod(m, struct etherip_header *); 3660b9f5f8aSAndrey V. Elsukov eth->eip_resvh = 0; 3670b9f5f8aSAndrey V. Elsukov eth->eip_ver = ETHERIP_VERSION; 3680b9f5f8aSAndrey V. Elsukov eth->eip_resvl = 0; 3690b9f5f8aSAndrey V. Elsukov break; 3700b9f5f8aSAndrey V. Elsukov default: 3710b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 3720b9f5f8aSAndrey V. Elsukov m_freem(m); 3730b9f5f8aSAndrey V. Elsukov goto err; 3740b9f5f8aSAndrey V. Elsukov } 3750b9f5f8aSAndrey V. Elsukov /* XXX should we check if our outer source is legal? */ 3760b9f5f8aSAndrey V. Elsukov /* dispatch to output logic based on outer AF */ 3770b9f5f8aSAndrey V. Elsukov switch (sc->gif_family) { 3780b9f5f8aSAndrey V. Elsukov #ifdef INET 3790b9f5f8aSAndrey V. Elsukov case AF_INET: 3800b9f5f8aSAndrey V. Elsukov error = in_gif_output(ifp, m, proto, ecn); 3810b9f5f8aSAndrey V. Elsukov break; 3820b9f5f8aSAndrey V. Elsukov #endif 3830b9f5f8aSAndrey V. Elsukov #ifdef INET6 3840b9f5f8aSAndrey V. Elsukov case AF_INET6: 3850b9f5f8aSAndrey V. Elsukov error = in6_gif_output(ifp, m, proto, ecn); 386776b7288SRandall Stewart break; 387776b7288SRandall Stewart #endif 388776b7288SRandall Stewart default: 389776b7288SRandall Stewart m_freem(m); 390776b7288SRandall Stewart } 3910b9f5f8aSAndrey V. Elsukov err: 392776b7288SRandall Stewart if (error) 3933751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 3940b9f5f8aSAndrey V. Elsukov return (error); 39573ff045cSAndrew Thompson } 3960b9f5f8aSAndrey V. Elsukov 3970b9f5f8aSAndrey V. Elsukov static void 3980b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused) 3990b9f5f8aSAndrey V. Elsukov { 4000b9f5f8aSAndrey V. Elsukov 40173ff045cSAndrew Thompson } 40273ff045cSAndrew Thompson 403cfa1ca9dSYoshinobu Inoue int 40447e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 40547e8d432SGleb Smirnoff struct route *ro) 406cfa1ca9dSYoshinobu Inoue { 407776b7288SRandall Stewart uint32_t af; 40810722b85SRobert Watson 40947e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 41001399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 41147e8d432SGleb Smirnoff else 412*62e1a437SZhenlei Huang af = RO_GET_FAMILY(ro, dst); 41340138788SRandall Stewart /* 4140b9f5f8aSAndrey V. Elsukov * Now save the af in the inbound pkt csum data, this is a cheat since 4150b9f5f8aSAndrey V. Elsukov * we are using the inbound csum_data field to carry the af over to 4160b9f5f8aSAndrey V. Elsukov * the gif_transmit() routine, avoiding using yet another mtag. 417776b7288SRandall Stewart */ 418cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 4190b9f5f8aSAndrey V. Elsukov return (ifp->if_transmit(ifp, m)); 420cfa1ca9dSYoshinobu Inoue } 421cfa1ca9dSYoshinobu Inoue 422cfa1ca9dSYoshinobu Inoue void 4230b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn) 424cfa1ca9dSYoshinobu Inoue { 42573ff045cSAndrew Thompson struct etherip_header *eip; 4260b9f5f8aSAndrey V. Elsukov #ifdef INET 4270b9f5f8aSAndrey V. Elsukov struct ip *ip; 4280b9f5f8aSAndrey V. Elsukov #endif 4290b9f5f8aSAndrey V. Elsukov #ifdef INET6 4300b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 4310b9f5f8aSAndrey V. Elsukov uint32_t t; 4320b9f5f8aSAndrey V. Elsukov #endif 43356abdd33SAndrew Thompson struct ether_header *eh; 43456abdd33SAndrew Thompson struct ifnet *oldifp; 4350b9f5f8aSAndrey V. Elsukov int isr, n, af; 436cfa1ca9dSYoshinobu Inoue 437b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 438b8a6e03fSGleb Smirnoff 43921fb391fSHajimu UMEMOTO if (ifp == NULL) { 440cfa1ca9dSYoshinobu Inoue /* just in case */ 441cfa1ca9dSYoshinobu Inoue m_freem(m); 442cfa1ca9dSYoshinobu Inoue return; 443cfa1ca9dSYoshinobu Inoue } 44421fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 4455b7a43f5SAndrey V. Elsukov m_clrprotoflags(m); 4460b9f5f8aSAndrey V. Elsukov switch (proto) { 4470b9f5f8aSAndrey V. Elsukov #ifdef INET 4480b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV4: 4490b9f5f8aSAndrey V. Elsukov af = AF_INET; 4500b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 4510b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 4520b9f5f8aSAndrey V. Elsukov if (m == NULL) 4530b9f5f8aSAndrey V. Elsukov goto drop; 4540b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 4550b9f5f8aSAndrey V. Elsukov if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 4560b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos) == 0) { 4570b9f5f8aSAndrey V. Elsukov m_freem(m); 4580b9f5f8aSAndrey V. Elsukov goto drop; 4590b9f5f8aSAndrey V. Elsukov } 4600b9f5f8aSAndrey V. Elsukov break; 4610b9f5f8aSAndrey V. Elsukov #endif 4620b9f5f8aSAndrey V. Elsukov #ifdef INET6 4630b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV6: 4640b9f5f8aSAndrey V. Elsukov af = AF_INET6; 4650b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 4660b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 4670b9f5f8aSAndrey V. Elsukov if (m == NULL) 4680b9f5f8aSAndrey V. Elsukov goto drop; 4690b9f5f8aSAndrey V. Elsukov t = htonl((uint32_t)ecn << 20); 4700b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 4710b9f5f8aSAndrey V. Elsukov if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 4720b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow) == 0) { 4730b9f5f8aSAndrey V. Elsukov m_freem(m); 4740b9f5f8aSAndrey V. Elsukov goto drop; 4750b9f5f8aSAndrey V. Elsukov } 4760b9f5f8aSAndrey V. Elsukov break; 4770b9f5f8aSAndrey V. Elsukov #endif 4780b9f5f8aSAndrey V. Elsukov case IPPROTO_ETHERIP: 4790b9f5f8aSAndrey V. Elsukov af = AF_LINK; 4800b9f5f8aSAndrey V. Elsukov break; 4810b9f5f8aSAndrey V. Elsukov default: 4820b9f5f8aSAndrey V. Elsukov m_freem(m); 4830b9f5f8aSAndrey V. Elsukov goto drop; 4840b9f5f8aSAndrey V. Elsukov } 485cfa1ca9dSYoshinobu Inoue 48610722b85SRobert Watson #ifdef MAC 48730d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 48810722b85SRobert Watson #endif 48910722b85SRobert Watson 49016d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 4910b9f5f8aSAndrey V. Elsukov uint32_t af1 = af; 492437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 493cfa1ca9dSYoshinobu Inoue } 494cfa1ca9dSYoshinobu Inoue 495e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 4963751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 4973751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 498e9f947e2SHiroki Sato m_freem(m); 499e9f947e2SHiroki Sato return; 500e9f947e2SHiroki Sato } 501e9f947e2SHiroki Sato 50294408d94SBrooks Davis if (ng_gif_input_p != NULL) { 50321fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 50494408d94SBrooks Davis if (m == NULL) 5050b9f5f8aSAndrey V. Elsukov goto drop; 50694408d94SBrooks Davis } 50794408d94SBrooks Davis 508cfa1ca9dSYoshinobu Inoue /* 509cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 510cfa1ca9dSYoshinobu Inoue * specified address family. 511cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 512cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 513cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 514cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 515cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 51688ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 51788ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 518cfa1ca9dSYoshinobu Inoue */ 519cfa1ca9dSYoshinobu Inoue switch (af) { 520cfa1ca9dSYoshinobu Inoue #ifdef INET 521cfa1ca9dSYoshinobu Inoue case AF_INET: 522cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 523cfa1ca9dSYoshinobu Inoue break; 524cfa1ca9dSYoshinobu Inoue #endif 525cfa1ca9dSYoshinobu Inoue #ifdef INET6 526cfa1ca9dSYoshinobu Inoue case AF_INET6: 527cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 528cfa1ca9dSYoshinobu Inoue break; 529cfa1ca9dSYoshinobu Inoue #endif 53073ff045cSAndrew Thompson case AF_LINK: 531b941bc1dSAndrey V. Elsukov n = sizeof(struct etherip_header) + 532b941bc1dSAndrey V. Elsukov sizeof(struct ether_header); 5330b9f5f8aSAndrey V. Elsukov if (n > m->m_len) 53473ff045cSAndrew Thompson m = m_pullup(m, n); 5350b9f5f8aSAndrey V. Elsukov if (m == NULL) 5360b9f5f8aSAndrey V. Elsukov goto drop; 53773ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 538dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 539dbe59260SHiroki Sato /* discard unknown versions */ 540dbe59260SHiroki Sato m_freem(m); 5410b9f5f8aSAndrey V. Elsukov goto drop; 542dbe59260SHiroki Sato } 543e243367bSKonstantin Belousov 544e243367bSKonstantin Belousov m_adj_decap(m, sizeof(struct etherip_header)); 54573ff045cSAndrew Thompson 54673ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 54773ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 54873ff045cSAndrew Thompson 54956abdd33SAndrew Thompson if (ifp->if_bridge) { 55056abdd33SAndrew Thompson oldifp = ifp; 55156abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 55256abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 55356abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 55456abdd33SAndrew Thompson m->m_flags |= M_BCAST; 55556abdd33SAndrew Thompson else 55656abdd33SAndrew Thompson m->m_flags |= M_MCAST; 5573751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 55856abdd33SAndrew Thompson } 55973ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 56073ff045cSAndrew Thompson 56156abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 56256abdd33SAndrew Thompson /* 56356abdd33SAndrew Thompson * The bridge gave us back itself or one of the 56456abdd33SAndrew Thompson * members for which the frame is addressed. 56556abdd33SAndrew Thompson */ 56656abdd33SAndrew Thompson ether_demux(ifp, m); 56756abdd33SAndrew Thompson return; 56856abdd33SAndrew Thompson } 56956abdd33SAndrew Thompson } 57073ff045cSAndrew Thompson if (m != NULL) 57173ff045cSAndrew Thompson m_freem(m); 57273ff045cSAndrew Thompson return; 57373ff045cSAndrew Thompson 574cfa1ca9dSYoshinobu Inoue default: 57594408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 57621fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 57794408d94SBrooks Davis else 578cfa1ca9dSYoshinobu Inoue m_freem(m); 579cfa1ca9dSYoshinobu Inoue return; 580cfa1ca9dSYoshinobu Inoue } 581cfa1ca9dSYoshinobu Inoue 5823751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 5833751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 584a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 5851cafed39SJonathan Lemon netisr_dispatch(isr, m); 5860b9f5f8aSAndrey V. Elsukov return; 5870b9f5f8aSAndrey V. Elsukov drop: 5880b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 589cfa1ca9dSYoshinobu Inoue } 590cfa1ca9dSYoshinobu Inoue 591b941bc1dSAndrey V. Elsukov static int 592c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 593cfa1ca9dSYoshinobu Inoue { 594cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 5950b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 5960b9f5f8aSAndrey V. Elsukov u_int options; 5970b9f5f8aSAndrey V. Elsukov int error; 598cfa1ca9dSYoshinobu Inoue 599cfa1ca9dSYoshinobu Inoue switch (cmd) { 600cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 6019426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 602cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 603cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 604cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 6050b9f5f8aSAndrey V. Elsukov case SIOCSIFFLAGS: 6060b9f5f8aSAndrey V. Elsukov return (0); 607cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 6080b9f5f8aSAndrey V. Elsukov if (ifr->ifr_mtu < GIF_MTU_MIN || 6090b9f5f8aSAndrey V. Elsukov ifr->ifr_mtu > GIF_MTU_MAX) 610cfa1ca9dSYoshinobu Inoue return (EINVAL); 6110b9f5f8aSAndrey V. Elsukov else 6120b9f5f8aSAndrey V. Elsukov ifp->if_mtu = ifr->ifr_mtu; 6130b9f5f8aSAndrey V. Elsukov return (0); 6140b9f5f8aSAndrey V. Elsukov } 6150b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 6160b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 6170b9f5f8aSAndrey V. Elsukov if (sc == NULL) { 6180b9f5f8aSAndrey V. Elsukov error = ENXIO; 6190b9f5f8aSAndrey V. Elsukov goto bad; 6200b9f5f8aSAndrey V. Elsukov } 6210b9f5f8aSAndrey V. Elsukov error = 0; 6220b9f5f8aSAndrey V. Elsukov switch (cmd) { 623686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 624b941bc1dSAndrey V. Elsukov if (sc->gif_family == 0) 625686cdd19SJun-ichiro itojun Hagino break; 626b941bc1dSAndrey V. Elsukov gif_delete_tunnel(sc); 6270b9f5f8aSAndrey V. Elsukov break; 628cfa1ca9dSYoshinobu Inoue #ifdef INET 629b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR: 63033841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 6310b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 632b941bc1dSAndrey V. Elsukov error = in_gif_ioctl(sc, cmd, data); 6330b9f5f8aSAndrey V. Elsukov break; 6340b9f5f8aSAndrey V. Elsukov #endif 635cfa1ca9dSYoshinobu Inoue #ifdef INET6 636b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR_IN6: 63733841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 638cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 639b941bc1dSAndrey V. Elsukov error = in6_gif_ioctl(sc, cmd, data); 6400b9f5f8aSAndrey V. Elsukov break; 6410b9f5f8aSAndrey V. Elsukov #endif 642eccfe69aSAndrey V. Elsukov case SIOCGTUNFIB: 643eccfe69aSAndrey V. Elsukov ifr->ifr_fib = sc->gif_fibnum; 644eccfe69aSAndrey V. Elsukov break; 645eccfe69aSAndrey V. Elsukov case SIOCSTUNFIB: 646eccfe69aSAndrey V. Elsukov if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 647eccfe69aSAndrey V. Elsukov break; 648eccfe69aSAndrey V. Elsukov if (ifr->ifr_fib >= rt_numfibs) 649eccfe69aSAndrey V. Elsukov error = EINVAL; 650eccfe69aSAndrey V. Elsukov else 651eccfe69aSAndrey V. Elsukov sc->gif_fibnum = ifr->ifr_fib; 652eccfe69aSAndrey V. Elsukov break; 653dbe59260SHiroki Sato case GIFGOPTS: 654dbe59260SHiroki Sato options = sc->gif_options; 655541d96aaSBrooks Davis error = copyout(&options, ifr_data_get_ptr(ifr), 656541d96aaSBrooks Davis sizeof(options)); 657dbe59260SHiroki Sato break; 658dbe59260SHiroki Sato case GIFSOPTS: 659dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 660dbe59260SHiroki Sato break; 661541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &options, 662541d96aaSBrooks Davis sizeof(options)); 6634cd5f57dSHiroki Sato if (error) 6644cd5f57dSHiroki Sato break; 665b941bc1dSAndrey V. Elsukov if (options & ~GIF_OPTMASK) { 666dbe59260SHiroki Sato error = EINVAL; 667b941bc1dSAndrey V. Elsukov break; 668b941bc1dSAndrey V. Elsukov } 669b941bc1dSAndrey V. Elsukov if (sc->gif_options != options) { 670b941bc1dSAndrey V. Elsukov switch (sc->gif_family) { 671b941bc1dSAndrey V. Elsukov #ifdef INET 672b941bc1dSAndrey V. Elsukov case AF_INET: 673b941bc1dSAndrey V. Elsukov error = in_gif_setopts(sc, options); 674b941bc1dSAndrey V. Elsukov break; 675b941bc1dSAndrey V. Elsukov #endif 676b941bc1dSAndrey V. Elsukov #ifdef INET6 677b941bc1dSAndrey V. Elsukov case AF_INET6: 678b941bc1dSAndrey V. Elsukov error = in6_gif_setopts(sc, options); 679b941bc1dSAndrey V. Elsukov break; 680b941bc1dSAndrey V. Elsukov #endif 681b941bc1dSAndrey V. Elsukov default: 682b941bc1dSAndrey V. Elsukov /* No need to invoke AF-handler */ 6834cd5f57dSHiroki Sato sc->gif_options = options; 684b941bc1dSAndrey V. Elsukov } 685b941bc1dSAndrey V. Elsukov } 686dbe59260SHiroki Sato break; 687cfa1ca9dSYoshinobu Inoue default: 688cfa1ca9dSYoshinobu Inoue error = EINVAL; 689cfa1ca9dSYoshinobu Inoue break; 690cfa1ca9dSYoshinobu Inoue } 691b941bc1dSAndrey V. Elsukov if (error == 0 && sc->gif_family != 0) { 692b941bc1dSAndrey V. Elsukov if ( 693b941bc1dSAndrey V. Elsukov #ifdef INET 694b941bc1dSAndrey V. Elsukov cmd == SIOCSIFPHYADDR || 695b941bc1dSAndrey V. Elsukov #endif 696b941bc1dSAndrey V. Elsukov #ifdef INET6 697b941bc1dSAndrey V. Elsukov cmd == SIOCSIFPHYADDR_IN6 || 698b941bc1dSAndrey V. Elsukov #endif 699b941bc1dSAndrey V. Elsukov 0) { 700b941bc1dSAndrey V. Elsukov if_link_state_change(ifp, LINK_STATE_UP); 701b941bc1dSAndrey V. Elsukov } 702b941bc1dSAndrey V. Elsukov } 703cfa1ca9dSYoshinobu Inoue bad: 7040b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 7050b9f5f8aSAndrey V. Elsukov return (error); 706cfa1ca9dSYoshinobu Inoue } 70753dab5feSBrooks Davis 7080b9f5f8aSAndrey V. Elsukov static void 709b941bc1dSAndrey V. Elsukov gif_delete_tunnel(struct gif_softc *sc) 7100b9f5f8aSAndrey V. Elsukov { 7110b9f5f8aSAndrey V. Elsukov 7120b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 713b941bc1dSAndrey V. Elsukov if (sc->gif_family != 0) { 714009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash); 715b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain); 716b941bc1dSAndrey V. Elsukov /* Wait until it become safe to free gif_hdr */ 717b941bc1dSAndrey V. Elsukov GIF_WAIT(); 7180b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 719f1aaad0cSHiroki Sato } 7200b9f5f8aSAndrey V. Elsukov sc->gif_family = 0; 721b941bc1dSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING; 722b941bc1dSAndrey V. Elsukov if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN); 72353dab5feSBrooks Davis } 724