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> 63*2c2b37adSJustin Hibbits #include <net/if_private.h> 64f889d2efSBrooks Davis #include <net/if_clone.h> 65cfa1ca9dSYoshinobu Inoue #include <net/if_types.h> 66cfa1ca9dSYoshinobu Inoue #include <net/netisr.h> 67cfa1ca9dSYoshinobu Inoue #include <net/route.h> 68cfa1ca9dSYoshinobu Inoue #include <net/bpf.h> 69530c0060SRobert Watson #include <net/vnet.h> 70cfa1ca9dSYoshinobu Inoue 71cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 72cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h> 73cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h> 740b9f5f8aSAndrey V. Elsukov #include <netinet/ip_ecn.h> 7533841545SHajimu UMEMOTO #ifdef INET 7633841545SHajimu UMEMOTO #include <netinet/in_var.h> 7753dab5feSBrooks Davis #include <netinet/ip_var.h> 78cfa1ca9dSYoshinobu Inoue #endif /* INET */ 79cfa1ca9dSYoshinobu Inoue 80cfa1ca9dSYoshinobu Inoue #ifdef INET6 81cfa1ca9dSYoshinobu Inoue #ifndef INET 82cfa1ca9dSYoshinobu Inoue #include <netinet/in.h> 83cfa1ca9dSYoshinobu Inoue #endif 84cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h> 85cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h> 860b9f5f8aSAndrey V. Elsukov #include <netinet6/ip6_ecn.h> 87cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h> 88cfa1ca9dSYoshinobu Inoue #endif /* INET6 */ 89cfa1ca9dSYoshinobu Inoue 90686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h> 9173ff045cSAndrew Thompson #include <net/ethernet.h> 9273ff045cSAndrew Thompson #include <net/if_bridgevar.h> 93cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h> 94cfa1ca9dSYoshinobu Inoue 95aed55708SRobert Watson #include <security/mac/mac_framework.h> 96aed55708SRobert Watson 9742a58907SGleb Smirnoff static const char gifname[] = "gif"; 98686cdd19SJun-ichiro itojun Hagino 99b941bc1dSAndrey V. Elsukov MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface"); 1000b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx; 1010b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl"); 102eddfbb76SRobert Watson 10394408d94SBrooks Davis void (*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af); 10494408d94SBrooks Davis void (*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af); 10594408d94SBrooks Davis void (*ng_gif_attach_p)(struct ifnet *ifp); 10694408d94SBrooks Davis void (*ng_gif_detach_p)(struct ifnet *ifp); 10794408d94SBrooks Davis 108dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 109dd4490fdSAndrey V. Elsukov static void gif_reassign(struct ifnet *, struct vnet *, char *); 110dd4490fdSAndrey V. Elsukov #endif 111b941bc1dSAndrey V. Elsukov static void gif_delete_tunnel(struct gif_softc *); 1120b9f5f8aSAndrey V. Elsukov static int gif_ioctl(struct ifnet *, u_long, caddr_t); 1130b9f5f8aSAndrey V. Elsukov static int gif_transmit(struct ifnet *, struct mbuf *); 1140b9f5f8aSAndrey V. Elsukov static void gif_qflush(struct ifnet *); 1156b7330e2SSam Leffler static int gif_clone_create(struct if_clone *, int, caddr_t); 116bb2bfb4fSBrooks Davis static void gif_clone_destroy(struct ifnet *); 1175f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, gif_cloner); 118a7f5886eSHiroki Sato #define V_gif_cloner VNET(gif_cloner) 11953dab5feSBrooks Davis 120872f786aSBrooks Davis SYSCTL_DECL(_net_link); 1217029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 122872f786aSBrooks Davis "Generic Tunnel Interface"); 123686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST 124686cdd19SJun-ichiro itojun Hagino /* 125872f786aSBrooks Davis * This macro controls the default upper limitation on nesting of gif tunnels. 126686cdd19SJun-ichiro itojun Hagino * Since, setting a large value to this macro with a careless configuration 127686cdd19SJun-ichiro itojun Hagino * may introduce system crash, we don't allow any nestings by default. 128686cdd19SJun-ichiro itojun Hagino * If you need to configure nested gif tunnels, you can define this macro 129686cdd19SJun-ichiro itojun Hagino * in your kernel configuration file. However, if you do so, please be 130686cdd19SJun-ichiro itojun Hagino * careful to configure the tunnels so that it won't make a loop. 131686cdd19SJun-ichiro itojun Hagino */ 132686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1 133686cdd19SJun-ichiro itojun Hagino #endif 1345f901c92SAndrew Turner VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST; 135d0728d71SRobert Watson #define V_max_gif_nesting VNET(max_gif_nesting) 1366df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW, 137eddfbb76SRobert Watson &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels"); 1388b615593SMarko Zec 139bb2bfb4fSBrooks Davis static int 140c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params) 141cfa1ca9dSYoshinobu Inoue { 14233841545SHajimu UMEMOTO struct gif_softc *sc; 143cfa1ca9dSYoshinobu Inoue 144e1a8c3dcSBruce M Simpson sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO); 1458b07e49aSJulian Elischer sc->gif_fibnum = curthread->td_proc->p_fibnum; 146fc74a9f9SBrooks Davis GIF2IFP(sc) = if_alloc(IFT_GIF); 147fc74a9f9SBrooks Davis GIF2IFP(sc)->if_softc = sc; 14842a58907SGleb Smirnoff if_initname(GIF2IFP(sc), gifname, unit); 149686cdd19SJun-ichiro itojun Hagino 150fc74a9f9SBrooks Davis GIF2IFP(sc)->if_addrlen = 0; 151fc74a9f9SBrooks Davis GIF2IFP(sc)->if_mtu = GIF_MTU; 152fc74a9f9SBrooks Davis GIF2IFP(sc)->if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 153fc74a9f9SBrooks Davis GIF2IFP(sc)->if_ioctl = gif_ioctl; 1540b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_transmit = gif_transmit; 1550b9f5f8aSAndrey V. Elsukov GIF2IFP(sc)->if_qflush = gif_qflush; 156fc74a9f9SBrooks Davis GIF2IFP(sc)->if_output = gif_output; 157dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 158dd4490fdSAndrey V. Elsukov GIF2IFP(sc)->if_reassign = gif_reassign; 159dd4490fdSAndrey V. Elsukov #endif 160f1aaad0cSHiroki Sato GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE; 161f1aaad0cSHiroki Sato GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE; 162fc74a9f9SBrooks Davis if_attach(GIF2IFP(sc)); 16301399f34SDavid Malone bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t)); 16494408d94SBrooks Davis if (ng_gif_attach_p != NULL) 165fc74a9f9SBrooks Davis (*ng_gif_attach_p)(GIF2IFP(sc)); 16625af0bb5SGleb Smirnoff 16725af0bb5SGleb Smirnoff return (0); 168cfa1ca9dSYoshinobu Inoue } 169cfa1ca9dSYoshinobu Inoue 170dd4490fdSAndrey V. Elsukov #ifdef VIMAGE 171dd4490fdSAndrey V. Elsukov static void 172dd4490fdSAndrey V. Elsukov gif_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused, 173dd4490fdSAndrey V. Elsukov char *unused __unused) 174dd4490fdSAndrey V. Elsukov { 175dd4490fdSAndrey V. Elsukov struct gif_softc *sc; 176dd4490fdSAndrey V. Elsukov 177dd4490fdSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 178dd4490fdSAndrey V. Elsukov sc = ifp->if_softc; 179dd4490fdSAndrey V. Elsukov if (sc != NULL) 180dd4490fdSAndrey V. Elsukov gif_delete_tunnel(sc); 181dd4490fdSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 182dd4490fdSAndrey V. Elsukov } 183dd4490fdSAndrey V. Elsukov #endif /* VIMAGE */ 184dd4490fdSAndrey V. Elsukov 18517d5cb2dSRobert Watson static void 186c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp) 18753dab5feSBrooks Davis { 1880b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 189febd0759SAndrew Thompson 1900b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 1910b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 192b941bc1dSAndrey V. Elsukov gif_delete_tunnel(sc); 19394408d94SBrooks Davis if (ng_gif_detach_p != NULL) 19494408d94SBrooks Davis (*ng_gif_detach_p)(ifp); 19553dab5feSBrooks Davis bpfdetach(ifp); 19653dab5feSBrooks Davis if_detach(ifp); 1970b9f5f8aSAndrey V. Elsukov ifp->if_softc = NULL; 1980b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 1990b9f5f8aSAndrey V. Elsukov 200b941bc1dSAndrey V. Elsukov GIF_WAIT(); 201fc74a9f9SBrooks Davis if_free(ifp); 20253dab5feSBrooks Davis free(sc, M_GIF); 20353dab5feSBrooks Davis } 20453dab5feSBrooks Davis 205d0728d71SRobert Watson static void 206d0728d71SRobert Watson vnet_gif_init(const void *unused __unused) 2071ed81b73SMarko Zec { 2081ed81b73SMarko Zec 209a7f5886eSHiroki Sato V_gif_cloner = if_clone_simple(gifname, gif_clone_create, 210a7f5886eSHiroki Sato gif_clone_destroy, 0); 211b941bc1dSAndrey V. Elsukov #ifdef INET 212b941bc1dSAndrey V. Elsukov in_gif_init(); 213b941bc1dSAndrey V. Elsukov #endif 214b941bc1dSAndrey V. Elsukov #ifdef INET6 215b941bc1dSAndrey V. Elsukov in6_gif_init(); 216b941bc1dSAndrey V. Elsukov #endif 2171ed81b73SMarko Zec } 2188ca6c11aSKristof Provost VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_ANY, 219a7f5886eSHiroki Sato vnet_gif_init, NULL); 220a7f5886eSHiroki Sato 221a7f5886eSHiroki Sato static void 222a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused) 223a7f5886eSHiroki Sato { 224a7f5886eSHiroki Sato 225a7f5886eSHiroki Sato if_clone_detach(V_gif_cloner); 226b941bc1dSAndrey V. Elsukov #ifdef INET 227b941bc1dSAndrey V. Elsukov in_gif_uninit(); 228b941bc1dSAndrey V. Elsukov #endif 229b941bc1dSAndrey V. Elsukov #ifdef INET6 230b941bc1dSAndrey V. Elsukov in6_gif_uninit(); 231b941bc1dSAndrey V. Elsukov #endif 232a7f5886eSHiroki Sato } 2338ca6c11aSKristof Provost VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, 234a7f5886eSHiroki Sato vnet_gif_uninit, NULL); 2351ed81b73SMarko Zec 2361ed81b73SMarko Zec static int 237c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data) 23853dab5feSBrooks Davis { 23953dab5feSBrooks Davis 24053dab5feSBrooks Davis switch (type) { 24153dab5feSBrooks Davis case MOD_LOAD: 24253dab5feSBrooks Davis case MOD_UNLOAD: 24353dab5feSBrooks Davis break; 2443e019deaSPoul-Henning Kamp default: 245a7f5886eSHiroki Sato return (EOPNOTSUPP); 24653dab5feSBrooks Davis } 247a7f5886eSHiroki Sato return (0); 24853dab5feSBrooks Davis } 24953dab5feSBrooks Davis 25053dab5feSBrooks Davis static moduledata_t gif_mod = { 25153dab5feSBrooks Davis "if_gif", 25253dab5feSBrooks Davis gifmodevent, 2539823d527SKevin Lo 0 25453dab5feSBrooks Davis }; 25553dab5feSBrooks Davis 25653dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY); 25720af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1); 258cfa1ca9dSYoshinobu Inoue 259b941bc1dSAndrey V. Elsukov struct gif_list * 260b941bc1dSAndrey V. Elsukov gif_hashinit(void) 261686cdd19SJun-ichiro itojun Hagino { 262b941bc1dSAndrey V. Elsukov struct gif_list *hash; 263b941bc1dSAndrey V. Elsukov int i; 264686cdd19SJun-ichiro itojun Hagino 265b941bc1dSAndrey V. Elsukov hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE, 266b941bc1dSAndrey V. Elsukov M_GIF, M_WAITOK); 267b941bc1dSAndrey V. Elsukov for (i = 0; i < GIF_HASH_SIZE; i++) 268b941bc1dSAndrey V. Elsukov CK_LIST_INIT(&hash[i]); 269686cdd19SJun-ichiro itojun Hagino 270b941bc1dSAndrey V. Elsukov return (hash); 271686cdd19SJun-ichiro itojun Hagino } 272686cdd19SJun-ichiro itojun Hagino 273b941bc1dSAndrey V. Elsukov void 274b941bc1dSAndrey V. Elsukov gif_hashdestroy(struct gif_list *hash) 275b941bc1dSAndrey V. Elsukov { 2763bb61ca6SHajimu UMEMOTO 277b941bc1dSAndrey V. Elsukov free(hash, M_GIF); 278686cdd19SJun-ichiro itojun Hagino } 279686cdd19SJun-ichiro itojun Hagino 28098a8fdf6SAndrey V. Elsukov #define MTAG_GIF 1080679712 2810b9f5f8aSAndrey V. Elsukov static int 2820b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m) 28373ff045cSAndrew Thompson { 28473ff045cSAndrew Thompson struct gif_softc *sc; 2850b9f5f8aSAndrey V. Elsukov struct etherip_header *eth; 286776b7288SRandall Stewart #ifdef INET 2870b9f5f8aSAndrey V. Elsukov struct ip *ip; 288776b7288SRandall Stewart #endif 289776b7288SRandall Stewart #ifdef INET6 2900b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 2910b9f5f8aSAndrey V. Elsukov uint32_t t; 292776b7288SRandall Stewart #endif 2930b9f5f8aSAndrey V. Elsukov uint32_t af; 2940b9f5f8aSAndrey V. Elsukov uint8_t proto, ecn; 2950b9f5f8aSAndrey V. Elsukov int error; 2960b9f5f8aSAndrey V. Elsukov 2979758a507SGleb Smirnoff NET_EPOCH_ASSERT(); 2989c0265c6SAndrey V. Elsukov #ifdef MAC 2999c0265c6SAndrey V. Elsukov error = mac_ifnet_check_transmit(ifp, m); 3009c0265c6SAndrey V. Elsukov if (error) { 3019c0265c6SAndrey V. Elsukov m_freem(m); 3029c0265c6SAndrey V. Elsukov goto err; 3039c0265c6SAndrey V. Elsukov } 3049c0265c6SAndrey V. Elsukov #endif 3050b9f5f8aSAndrey V. Elsukov error = ENETDOWN; 3060b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 3079c0265c6SAndrey V. Elsukov if ((ifp->if_flags & IFF_MONITOR) != 0 || 3089c0265c6SAndrey V. Elsukov (ifp->if_flags & IFF_UP) == 0 || 309009d82eeSAndrey V. Elsukov (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 3109c0265c6SAndrey V. Elsukov sc->gif_family == 0 || 31198a8fdf6SAndrey V. Elsukov (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF, 31298a8fdf6SAndrey V. Elsukov V_max_gif_nesting)) != 0) { 3130b9f5f8aSAndrey V. Elsukov m_freem(m); 3140b9f5f8aSAndrey V. Elsukov goto err; 3150b9f5f8aSAndrey V. Elsukov } 3160b9f5f8aSAndrey V. Elsukov /* Now pull back the af that we stashed in the csum_data. */ 3179c0265c6SAndrey V. Elsukov if (ifp->if_bridge) 3189c0265c6SAndrey V. Elsukov af = AF_LINK; 3199c0265c6SAndrey V. Elsukov else 320cef68c63SRandall Stewart af = m->m_pkthdr.csum_data; 3219c0265c6SAndrey V. Elsukov m->m_flags &= ~(M_BCAST|M_MCAST); 3229c0265c6SAndrey V. Elsukov M_SETFIB(m, sc->gif_fibnum); 323776b7288SRandall Stewart BPF_MTAP2(ifp, &af, sizeof(af), m); 3243751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3250b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len); 326776b7288SRandall Stewart /* inner AF-specific encapsulation */ 3270b9f5f8aSAndrey V. Elsukov ecn = 0; 3280b9f5f8aSAndrey V. Elsukov switch (af) { 329776b7288SRandall Stewart #ifdef INET 330776b7288SRandall Stewart case AF_INET: 3310b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV4; 3320b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 3330b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 3340b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3350b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3360b9f5f8aSAndrey V. Elsukov goto err; 3370b9f5f8aSAndrey V. Elsukov } 3380b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 3390b9f5f8aSAndrey V. Elsukov ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3400b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos); 341776b7288SRandall Stewart break; 342776b7288SRandall Stewart #endif 343776b7288SRandall Stewart #ifdef INET6 344776b7288SRandall Stewart case AF_INET6: 3450b9f5f8aSAndrey V. Elsukov proto = IPPROTO_IPV6; 3460b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 3470b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 3480b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3490b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3500b9f5f8aSAndrey V. Elsukov goto err; 3510b9f5f8aSAndrey V. Elsukov } 3520b9f5f8aSAndrey V. Elsukov t = 0; 3530b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 3540b9f5f8aSAndrey V. Elsukov ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 3550b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow); 3560b9f5f8aSAndrey V. Elsukov ecn = (ntohl(t) >> 20) & 0xff; 3570b9f5f8aSAndrey V. Elsukov break; 3580b9f5f8aSAndrey V. Elsukov #endif 3590b9f5f8aSAndrey V. Elsukov case AF_LINK: 3600b9f5f8aSAndrey V. Elsukov proto = IPPROTO_ETHERIP; 3610b9f5f8aSAndrey V. Elsukov M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT); 3620b9f5f8aSAndrey V. Elsukov if (m == NULL) { 3630b9f5f8aSAndrey V. Elsukov error = ENOBUFS; 3640b9f5f8aSAndrey V. Elsukov goto err; 3650b9f5f8aSAndrey V. Elsukov } 3660b9f5f8aSAndrey V. Elsukov eth = mtod(m, struct etherip_header *); 3670b9f5f8aSAndrey V. Elsukov eth->eip_resvh = 0; 3680b9f5f8aSAndrey V. Elsukov eth->eip_ver = ETHERIP_VERSION; 3690b9f5f8aSAndrey V. Elsukov eth->eip_resvl = 0; 3700b9f5f8aSAndrey V. Elsukov break; 3710b9f5f8aSAndrey V. Elsukov default: 3720b9f5f8aSAndrey V. Elsukov error = EAFNOSUPPORT; 3730b9f5f8aSAndrey V. Elsukov m_freem(m); 3740b9f5f8aSAndrey V. Elsukov goto err; 3750b9f5f8aSAndrey V. Elsukov } 3760b9f5f8aSAndrey V. Elsukov /* XXX should we check if our outer source is legal? */ 3770b9f5f8aSAndrey V. Elsukov /* dispatch to output logic based on outer AF */ 3780b9f5f8aSAndrey V. Elsukov switch (sc->gif_family) { 3790b9f5f8aSAndrey V. Elsukov #ifdef INET 3800b9f5f8aSAndrey V. Elsukov case AF_INET: 3810b9f5f8aSAndrey V. Elsukov error = in_gif_output(ifp, m, proto, ecn); 3820b9f5f8aSAndrey V. Elsukov break; 3830b9f5f8aSAndrey V. Elsukov #endif 3840b9f5f8aSAndrey V. Elsukov #ifdef INET6 3850b9f5f8aSAndrey V. Elsukov case AF_INET6: 3860b9f5f8aSAndrey V. Elsukov error = in6_gif_output(ifp, m, proto, ecn); 387776b7288SRandall Stewart break; 388776b7288SRandall Stewart #endif 389776b7288SRandall Stewart default: 390776b7288SRandall Stewart m_freem(m); 391776b7288SRandall Stewart } 3920b9f5f8aSAndrey V. Elsukov err: 393776b7288SRandall Stewart if (error) 3943751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 3950b9f5f8aSAndrey V. Elsukov return (error); 39673ff045cSAndrew Thompson } 3970b9f5f8aSAndrey V. Elsukov 3980b9f5f8aSAndrey V. Elsukov static void 3990b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused) 4000b9f5f8aSAndrey V. Elsukov { 4010b9f5f8aSAndrey V. Elsukov 40273ff045cSAndrew Thompson } 40373ff045cSAndrew Thompson 404cfa1ca9dSYoshinobu Inoue int 40547e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 40647e8d432SGleb Smirnoff struct route *ro) 407cfa1ca9dSYoshinobu Inoue { 408776b7288SRandall Stewart uint32_t af; 40910722b85SRobert Watson 41047e8d432SGleb Smirnoff if (dst->sa_family == AF_UNSPEC) 41101399f34SDavid Malone bcopy(dst->sa_data, &af, sizeof(af)); 41247e8d432SGleb Smirnoff else 41362e1a437SZhenlei Huang af = RO_GET_FAMILY(ro, dst); 41440138788SRandall Stewart /* 4150b9f5f8aSAndrey V. Elsukov * Now save the af in the inbound pkt csum data, this is a cheat since 4160b9f5f8aSAndrey V. Elsukov * we are using the inbound csum_data field to carry the af over to 4170b9f5f8aSAndrey V. Elsukov * the gif_transmit() routine, avoiding using yet another mtag. 418776b7288SRandall Stewart */ 419cef68c63SRandall Stewart m->m_pkthdr.csum_data = af; 4200b9f5f8aSAndrey V. Elsukov return (ifp->if_transmit(ifp, m)); 421cfa1ca9dSYoshinobu Inoue } 422cfa1ca9dSYoshinobu Inoue 423cfa1ca9dSYoshinobu Inoue void 4240b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn) 425cfa1ca9dSYoshinobu Inoue { 42673ff045cSAndrew Thompson struct etherip_header *eip; 4270b9f5f8aSAndrey V. Elsukov #ifdef INET 4280b9f5f8aSAndrey V. Elsukov struct ip *ip; 4290b9f5f8aSAndrey V. Elsukov #endif 4300b9f5f8aSAndrey V. Elsukov #ifdef INET6 4310b9f5f8aSAndrey V. Elsukov struct ip6_hdr *ip6; 4320b9f5f8aSAndrey V. Elsukov uint32_t t; 4330b9f5f8aSAndrey V. Elsukov #endif 43456abdd33SAndrew Thompson struct ether_header *eh; 43556abdd33SAndrew Thompson struct ifnet *oldifp; 4360b9f5f8aSAndrey V. Elsukov int isr, n, af; 437cfa1ca9dSYoshinobu Inoue 438b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT(); 439b8a6e03fSGleb Smirnoff 44021fb391fSHajimu UMEMOTO if (ifp == NULL) { 441cfa1ca9dSYoshinobu Inoue /* just in case */ 442cfa1ca9dSYoshinobu Inoue m_freem(m); 443cfa1ca9dSYoshinobu Inoue return; 444cfa1ca9dSYoshinobu Inoue } 44521fb391fSHajimu UMEMOTO m->m_pkthdr.rcvif = ifp; 4465b7a43f5SAndrey V. Elsukov m_clrprotoflags(m); 4470b9f5f8aSAndrey V. Elsukov switch (proto) { 4480b9f5f8aSAndrey V. Elsukov #ifdef INET 4490b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV4: 4500b9f5f8aSAndrey V. Elsukov af = AF_INET; 4510b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip)) 4520b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip)); 4530b9f5f8aSAndrey V. Elsukov if (m == NULL) 4540b9f5f8aSAndrey V. Elsukov goto drop; 4550b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *); 4560b9f5f8aSAndrey V. Elsukov if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 4570b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &ecn, &ip->ip_tos) == 0) { 4580b9f5f8aSAndrey V. Elsukov m_freem(m); 4590b9f5f8aSAndrey V. Elsukov goto drop; 4600b9f5f8aSAndrey V. Elsukov } 4610b9f5f8aSAndrey V. Elsukov break; 4620b9f5f8aSAndrey V. Elsukov #endif 4630b9f5f8aSAndrey V. Elsukov #ifdef INET6 4640b9f5f8aSAndrey V. Elsukov case IPPROTO_IPV6: 4650b9f5f8aSAndrey V. Elsukov af = AF_INET6; 4660b9f5f8aSAndrey V. Elsukov if (m->m_len < sizeof(struct ip6_hdr)) 4670b9f5f8aSAndrey V. Elsukov m = m_pullup(m, sizeof(struct ip6_hdr)); 4680b9f5f8aSAndrey V. Elsukov if (m == NULL) 4690b9f5f8aSAndrey V. Elsukov goto drop; 4700b9f5f8aSAndrey V. Elsukov t = htonl((uint32_t)ecn << 20); 4710b9f5f8aSAndrey V. Elsukov ip6 = mtod(m, struct ip6_hdr *); 4720b9f5f8aSAndrey V. Elsukov if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED: 4730b9f5f8aSAndrey V. Elsukov ECN_NOCARE, &t, &ip6->ip6_flow) == 0) { 4740b9f5f8aSAndrey V. Elsukov m_freem(m); 4750b9f5f8aSAndrey V. Elsukov goto drop; 4760b9f5f8aSAndrey V. Elsukov } 4770b9f5f8aSAndrey V. Elsukov break; 4780b9f5f8aSAndrey V. Elsukov #endif 4790b9f5f8aSAndrey V. Elsukov case IPPROTO_ETHERIP: 4800b9f5f8aSAndrey V. Elsukov af = AF_LINK; 4810b9f5f8aSAndrey V. Elsukov break; 4820b9f5f8aSAndrey V. Elsukov default: 4830b9f5f8aSAndrey V. Elsukov m_freem(m); 4840b9f5f8aSAndrey V. Elsukov goto drop; 4850b9f5f8aSAndrey V. Elsukov } 486cfa1ca9dSYoshinobu Inoue 48710722b85SRobert Watson #ifdef MAC 48830d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m); 48910722b85SRobert Watson #endif 49010722b85SRobert Watson 49116d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) { 4920b9f5f8aSAndrey V. Elsukov uint32_t af1 = af; 493437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m); 494cfa1ca9dSYoshinobu Inoue } 495cfa1ca9dSYoshinobu Inoue 496e9f947e2SHiroki Sato if ((ifp->if_flags & IFF_MONITOR) != 0) { 4973751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 4983751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 499e9f947e2SHiroki Sato m_freem(m); 500e9f947e2SHiroki Sato return; 501e9f947e2SHiroki Sato } 502e9f947e2SHiroki Sato 50394408d94SBrooks Davis if (ng_gif_input_p != NULL) { 50421fb391fSHajimu UMEMOTO (*ng_gif_input_p)(ifp, &m, af); 50594408d94SBrooks Davis if (m == NULL) 5060b9f5f8aSAndrey V. Elsukov goto drop; 50794408d94SBrooks Davis } 50894408d94SBrooks Davis 509cfa1ca9dSYoshinobu Inoue /* 510cfa1ca9dSYoshinobu Inoue * Put the packet to the network layer input queue according to the 511cfa1ca9dSYoshinobu Inoue * specified address family. 512cfa1ca9dSYoshinobu Inoue * Note: older versions of gif_input directly called network layer 513cfa1ca9dSYoshinobu Inoue * input functions, e.g. ip6_input, here. We changed the policy to 514cfa1ca9dSYoshinobu Inoue * prevent too many recursive calls of such input functions, which 515cfa1ca9dSYoshinobu Inoue * might cause kernel panic. But the change may introduce another 516cfa1ca9dSYoshinobu Inoue * problem; if the input queue is full, packets are discarded. 51788ff5695SSUZUKI Shinsuke * The kernel stack overflow really happened, and we believed 51888ff5695SSUZUKI Shinsuke * queue-full rarely occurs, so we changed the policy. 519cfa1ca9dSYoshinobu Inoue */ 520cfa1ca9dSYoshinobu Inoue switch (af) { 521cfa1ca9dSYoshinobu Inoue #ifdef INET 522cfa1ca9dSYoshinobu Inoue case AF_INET: 523cfa1ca9dSYoshinobu Inoue isr = NETISR_IP; 524cfa1ca9dSYoshinobu Inoue break; 525cfa1ca9dSYoshinobu Inoue #endif 526cfa1ca9dSYoshinobu Inoue #ifdef INET6 527cfa1ca9dSYoshinobu Inoue case AF_INET6: 528cfa1ca9dSYoshinobu Inoue isr = NETISR_IPV6; 529cfa1ca9dSYoshinobu Inoue break; 530cfa1ca9dSYoshinobu Inoue #endif 53173ff045cSAndrew Thompson case AF_LINK: 532b941bc1dSAndrey V. Elsukov n = sizeof(struct etherip_header) + 533b941bc1dSAndrey V. Elsukov sizeof(struct ether_header); 5340b9f5f8aSAndrey V. Elsukov if (n > m->m_len) 53573ff045cSAndrew Thompson m = m_pullup(m, n); 5360b9f5f8aSAndrey V. Elsukov if (m == NULL) 5370b9f5f8aSAndrey V. Elsukov goto drop; 53873ff045cSAndrew Thompson eip = mtod(m, struct etherip_header *); 539dbe59260SHiroki Sato if (eip->eip_ver != ETHERIP_VERSION) { 540dbe59260SHiroki Sato /* discard unknown versions */ 541dbe59260SHiroki Sato m_freem(m); 5420b9f5f8aSAndrey V. Elsukov goto drop; 543dbe59260SHiroki Sato } 544e243367bSKonstantin Belousov 545e243367bSKonstantin Belousov m_adj_decap(m, sizeof(struct etherip_header)); 54673ff045cSAndrew Thompson 54773ff045cSAndrew Thompson m->m_flags &= ~(M_BCAST|M_MCAST); 54873ff045cSAndrew Thompson m->m_pkthdr.rcvif = ifp; 54973ff045cSAndrew Thompson 55056abdd33SAndrew Thompson if (ifp->if_bridge) { 55156abdd33SAndrew Thompson oldifp = ifp; 55256abdd33SAndrew Thompson eh = mtod(m, struct ether_header *); 55356abdd33SAndrew Thompson if (ETHER_IS_MULTICAST(eh->ether_dhost)) { 55456abdd33SAndrew Thompson if (ETHER_IS_BROADCAST(eh->ether_dhost)) 55556abdd33SAndrew Thompson m->m_flags |= M_BCAST; 55656abdd33SAndrew Thompson else 55756abdd33SAndrew Thompson m->m_flags |= M_MCAST; 5583751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 55956abdd33SAndrew Thompson } 56073ff045cSAndrew Thompson BRIDGE_INPUT(ifp, m); 56173ff045cSAndrew Thompson 56256abdd33SAndrew Thompson if (m != NULL && ifp != oldifp) { 56356abdd33SAndrew Thompson /* 56456abdd33SAndrew Thompson * The bridge gave us back itself or one of the 56556abdd33SAndrew Thompson * members for which the frame is addressed. 56656abdd33SAndrew Thompson */ 56756abdd33SAndrew Thompson ether_demux(ifp, m); 56856abdd33SAndrew Thompson return; 56956abdd33SAndrew Thompson } 57056abdd33SAndrew Thompson } 57173ff045cSAndrew Thompson if (m != NULL) 57273ff045cSAndrew Thompson m_freem(m); 57373ff045cSAndrew Thompson return; 57473ff045cSAndrew Thompson 575cfa1ca9dSYoshinobu Inoue default: 57694408d94SBrooks Davis if (ng_gif_input_orphan_p != NULL) 57721fb391fSHajimu UMEMOTO (*ng_gif_input_orphan_p)(ifp, m, af); 57894408d94SBrooks Davis else 579cfa1ca9dSYoshinobu Inoue m_freem(m); 580cfa1ca9dSYoshinobu Inoue return; 581cfa1ca9dSYoshinobu Inoue } 582cfa1ca9dSYoshinobu Inoue 5833751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 5843751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 585a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib); 5861cafed39SJonathan Lemon netisr_dispatch(isr, m); 5870b9f5f8aSAndrey V. Elsukov return; 5880b9f5f8aSAndrey V. Elsukov drop: 5890b9f5f8aSAndrey V. Elsukov if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 590cfa1ca9dSYoshinobu Inoue } 591cfa1ca9dSYoshinobu Inoue 592b941bc1dSAndrey V. Elsukov static int 593c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 594cfa1ca9dSYoshinobu Inoue { 595cfa1ca9dSYoshinobu Inoue struct ifreq *ifr = (struct ifreq*)data; 5960b9f5f8aSAndrey V. Elsukov struct gif_softc *sc; 5970b9f5f8aSAndrey V. Elsukov u_int options; 5980b9f5f8aSAndrey V. Elsukov int error; 599cfa1ca9dSYoshinobu Inoue 600cfa1ca9dSYoshinobu Inoue switch (cmd) { 601cfa1ca9dSYoshinobu Inoue case SIOCSIFADDR: 6029426aedfSHajimu UMEMOTO ifp->if_flags |= IFF_UP; 603cfa1ca9dSYoshinobu Inoue case SIOCADDMULTI: 604cfa1ca9dSYoshinobu Inoue case SIOCDELMULTI: 605cfa1ca9dSYoshinobu Inoue case SIOCGIFMTU: 6060b9f5f8aSAndrey V. Elsukov case SIOCSIFFLAGS: 6070b9f5f8aSAndrey V. Elsukov return (0); 608cfa1ca9dSYoshinobu Inoue case SIOCSIFMTU: 6090b9f5f8aSAndrey V. Elsukov if (ifr->ifr_mtu < GIF_MTU_MIN || 6100b9f5f8aSAndrey V. Elsukov ifr->ifr_mtu > GIF_MTU_MAX) 611cfa1ca9dSYoshinobu Inoue return (EINVAL); 6120b9f5f8aSAndrey V. Elsukov else 6130b9f5f8aSAndrey V. Elsukov ifp->if_mtu = ifr->ifr_mtu; 6140b9f5f8aSAndrey V. Elsukov return (0); 6150b9f5f8aSAndrey V. Elsukov } 6160b9f5f8aSAndrey V. Elsukov sx_xlock(&gif_ioctl_sx); 6170b9f5f8aSAndrey V. Elsukov sc = ifp->if_softc; 6180b9f5f8aSAndrey V. Elsukov if (sc == NULL) { 6190b9f5f8aSAndrey V. Elsukov error = ENXIO; 6200b9f5f8aSAndrey V. Elsukov goto bad; 6210b9f5f8aSAndrey V. Elsukov } 6220b9f5f8aSAndrey V. Elsukov error = 0; 6230b9f5f8aSAndrey V. Elsukov switch (cmd) { 624686cdd19SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 625b941bc1dSAndrey V. Elsukov if (sc->gif_family == 0) 626686cdd19SJun-ichiro itojun Hagino break; 627b941bc1dSAndrey V. Elsukov gif_delete_tunnel(sc); 6280b9f5f8aSAndrey V. Elsukov break; 629cfa1ca9dSYoshinobu Inoue #ifdef INET 630b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR: 63133841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 6320b9f5f8aSAndrey V. Elsukov case SIOCGIFPDSTADDR: 633b941bc1dSAndrey V. Elsukov error = in_gif_ioctl(sc, cmd, data); 6340b9f5f8aSAndrey V. Elsukov break; 6350b9f5f8aSAndrey V. Elsukov #endif 636cfa1ca9dSYoshinobu Inoue #ifdef INET6 637b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR_IN6: 63833841545SHajimu UMEMOTO case SIOCGIFPSRCADDR_IN6: 639cfa1ca9dSYoshinobu Inoue case SIOCGIFPDSTADDR_IN6: 640b941bc1dSAndrey V. Elsukov error = in6_gif_ioctl(sc, cmd, data); 6410b9f5f8aSAndrey V. Elsukov break; 6420b9f5f8aSAndrey V. Elsukov #endif 643eccfe69aSAndrey V. Elsukov case SIOCGTUNFIB: 644eccfe69aSAndrey V. Elsukov ifr->ifr_fib = sc->gif_fibnum; 645eccfe69aSAndrey V. Elsukov break; 646eccfe69aSAndrey V. Elsukov case SIOCSTUNFIB: 647eccfe69aSAndrey V. Elsukov if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 648eccfe69aSAndrey V. Elsukov break; 649eccfe69aSAndrey V. Elsukov if (ifr->ifr_fib >= rt_numfibs) 650eccfe69aSAndrey V. Elsukov error = EINVAL; 651eccfe69aSAndrey V. Elsukov else 652eccfe69aSAndrey V. Elsukov sc->gif_fibnum = ifr->ifr_fib; 653eccfe69aSAndrey V. Elsukov break; 654dbe59260SHiroki Sato case GIFGOPTS: 655dbe59260SHiroki Sato options = sc->gif_options; 656541d96aaSBrooks Davis error = copyout(&options, ifr_data_get_ptr(ifr), 657541d96aaSBrooks Davis sizeof(options)); 658dbe59260SHiroki Sato break; 659dbe59260SHiroki Sato case GIFSOPTS: 660dbe59260SHiroki Sato if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0) 661dbe59260SHiroki Sato break; 662541d96aaSBrooks Davis error = copyin(ifr_data_get_ptr(ifr), &options, 663541d96aaSBrooks Davis sizeof(options)); 6644cd5f57dSHiroki Sato if (error) 6654cd5f57dSHiroki Sato break; 666b941bc1dSAndrey V. Elsukov if (options & ~GIF_OPTMASK) { 667dbe59260SHiroki Sato error = EINVAL; 668b941bc1dSAndrey V. Elsukov break; 669b941bc1dSAndrey V. Elsukov } 670b941bc1dSAndrey V. Elsukov if (sc->gif_options != options) { 671b941bc1dSAndrey V. Elsukov switch (sc->gif_family) { 672b941bc1dSAndrey V. Elsukov #ifdef INET 673b941bc1dSAndrey V. Elsukov case AF_INET: 674b941bc1dSAndrey V. Elsukov error = in_gif_setopts(sc, options); 675b941bc1dSAndrey V. Elsukov break; 676b941bc1dSAndrey V. Elsukov #endif 677b941bc1dSAndrey V. Elsukov #ifdef INET6 678b941bc1dSAndrey V. Elsukov case AF_INET6: 679b941bc1dSAndrey V. Elsukov error = in6_gif_setopts(sc, options); 680b941bc1dSAndrey V. Elsukov break; 681b941bc1dSAndrey V. Elsukov #endif 682b941bc1dSAndrey V. Elsukov default: 683b941bc1dSAndrey V. Elsukov /* No need to invoke AF-handler */ 6844cd5f57dSHiroki Sato sc->gif_options = options; 685b941bc1dSAndrey V. Elsukov } 686b941bc1dSAndrey V. Elsukov } 687dbe59260SHiroki Sato break; 688cfa1ca9dSYoshinobu Inoue default: 689cfa1ca9dSYoshinobu Inoue error = EINVAL; 690cfa1ca9dSYoshinobu Inoue break; 691cfa1ca9dSYoshinobu Inoue } 692b941bc1dSAndrey V. Elsukov if (error == 0 && sc->gif_family != 0) { 693b941bc1dSAndrey V. Elsukov if ( 694b941bc1dSAndrey V. Elsukov #ifdef INET 695b941bc1dSAndrey V. Elsukov cmd == SIOCSIFPHYADDR || 696b941bc1dSAndrey V. Elsukov #endif 697b941bc1dSAndrey V. Elsukov #ifdef INET6 698b941bc1dSAndrey V. Elsukov cmd == SIOCSIFPHYADDR_IN6 || 699b941bc1dSAndrey V. Elsukov #endif 700b941bc1dSAndrey V. Elsukov 0) { 701b941bc1dSAndrey V. Elsukov if_link_state_change(ifp, LINK_STATE_UP); 702b941bc1dSAndrey V. Elsukov } 703b941bc1dSAndrey V. Elsukov } 704cfa1ca9dSYoshinobu Inoue bad: 7050b9f5f8aSAndrey V. Elsukov sx_xunlock(&gif_ioctl_sx); 7060b9f5f8aSAndrey V. Elsukov return (error); 707cfa1ca9dSYoshinobu Inoue } 70853dab5feSBrooks Davis 7090b9f5f8aSAndrey V. Elsukov static void 710b941bc1dSAndrey V. Elsukov gif_delete_tunnel(struct gif_softc *sc) 7110b9f5f8aSAndrey V. Elsukov { 7120b9f5f8aSAndrey V. Elsukov 7130b9f5f8aSAndrey V. Elsukov sx_assert(&gif_ioctl_sx, SA_XLOCKED); 714b941bc1dSAndrey V. Elsukov if (sc->gif_family != 0) { 715009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash); 716b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain); 717b941bc1dSAndrey V. Elsukov /* Wait until it become safe to free gif_hdr */ 718b941bc1dSAndrey V. Elsukov GIF_WAIT(); 7190b9f5f8aSAndrey V. Elsukov free(sc->gif_hdr, M_GIF); 720f1aaad0cSHiroki Sato } 7210b9f5f8aSAndrey V. Elsukov sc->gif_family = 0; 722b941bc1dSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING; 723b941bc1dSAndrey V. Elsukov if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN); 72453dab5feSBrooks Davis } 725