xref: /freebsd/sys/net/if_gif.c (revision 2cb0fce24d64039090dc9243cdf0715ee80c91b1)
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>
36cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
38cfa1ca9dSYoshinobu Inoue 
39cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
420b9f5f8aSAndrey V. Elsukov #include <sys/lock.h>
43cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
455dba30f1SPoul-Henning Kamp #include <sys/module.h>
460b9f5f8aSAndrey V. Elsukov #include <sys/rmlock.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
48cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
490b9f5f8aSAndrey V. Elsukov #include <sys/sx.h>
50cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
51cfa1ca9dSYoshinobu Inoue #include <sys/time.h>
52872f786aSBrooks Davis #include <sys/sysctl.h>
53cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h>
54dbe59260SHiroki Sato #include <sys/priv.h>
558b07e49aSJulian Elischer #include <sys/proc.h>
5653dab5feSBrooks Davis #include <sys/conf.h>
57cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
58cfa1ca9dSYoshinobu Inoue 
59cfa1ca9dSYoshinobu Inoue #include <net/if.h>
6076039bc8SGleb Smirnoff #include <net/if_var.h>
612c2b37adSJustin Hibbits #include <net/if_private.h>
62f889d2efSBrooks Davis #include <net/if_clone.h>
63cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
64cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
65cfa1ca9dSYoshinobu Inoue #include <net/route.h>
66cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
67530c0060SRobert Watson #include <net/vnet.h>
68cfa1ca9dSYoshinobu Inoue 
69cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
70cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
71cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
720b9f5f8aSAndrey V. Elsukov #include <netinet/ip_ecn.h>
7333841545SHajimu UMEMOTO #ifdef	INET
7433841545SHajimu UMEMOTO #include <netinet/in_var.h>
7553dab5feSBrooks Davis #include <netinet/ip_var.h>
76cfa1ca9dSYoshinobu Inoue #endif	/* INET */
77cfa1ca9dSYoshinobu Inoue 
78cfa1ca9dSYoshinobu Inoue #ifdef INET6
79cfa1ca9dSYoshinobu Inoue #ifndef INET
80cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
81cfa1ca9dSYoshinobu Inoue #endif
82cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
83cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
840b9f5f8aSAndrey V. Elsukov #include <netinet6/ip6_ecn.h>
85cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
86cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
87cfa1ca9dSYoshinobu Inoue 
88686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
8973ff045cSAndrew Thompson #include <net/ethernet.h>
9073ff045cSAndrew Thompson #include <net/if_bridgevar.h>
91cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
92cfa1ca9dSYoshinobu Inoue 
93aed55708SRobert Watson #include <security/mac/mac_framework.h>
94aed55708SRobert Watson 
9542a58907SGleb Smirnoff static const char gifname[] = "gif";
96686cdd19SJun-ichiro itojun Hagino 
97b941bc1dSAndrey V. Elsukov MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
980b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx;
990b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
100eddfbb76SRobert Watson 
10194408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
10294408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
10394408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
10494408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
10594408d94SBrooks Davis 
106dd4490fdSAndrey V. Elsukov #ifdef VIMAGE
107dd4490fdSAndrey V. Elsukov static void	gif_reassign(struct ifnet *, struct vnet *, char *);
108dd4490fdSAndrey V. Elsukov #endif
109b941bc1dSAndrey V. Elsukov static void	gif_delete_tunnel(struct gif_softc *);
1100b9f5f8aSAndrey V. Elsukov static int	gif_ioctl(struct ifnet *, u_long, caddr_t);
1110b9f5f8aSAndrey V. Elsukov static int	gif_transmit(struct ifnet *, struct mbuf *);
1120b9f5f8aSAndrey V. Elsukov static void	gif_qflush(struct ifnet *);
1136b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
114bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
1155f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, gif_cloner);
116a7f5886eSHiroki Sato #define	V_gif_cloner	VNET(gif_cloner)
11753dab5feSBrooks Davis 
118872f786aSBrooks Davis SYSCTL_DECL(_net_link);
1197029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
120872f786aSBrooks Davis     "Generic Tunnel Interface");
121686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
122686cdd19SJun-ichiro itojun Hagino /*
123872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
124686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
125686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
126686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
127686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
128686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
129686cdd19SJun-ichiro itojun Hagino  */
130686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
131686cdd19SJun-ichiro itojun Hagino #endif
1325f901c92SAndrew Turner VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST;
133d0728d71SRobert Watson #define	V_max_gif_nesting	VNET(max_gif_nesting)
1346df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
135eddfbb76SRobert Watson     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
1368b615593SMarko Zec 
137bb2bfb4fSBrooks Davis static int
138c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
139cfa1ca9dSYoshinobu Inoue {
14033841545SHajimu UMEMOTO 	struct gif_softc *sc;
141cfa1ca9dSYoshinobu Inoue 
142e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1438b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
144fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
145fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
14642a58907SGleb Smirnoff 	if_initname(GIF2IFP(sc), gifname, unit);
147686cdd19SJun-ichiro itojun Hagino 
148fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
149fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
150fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
151fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
1520b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_transmit = gif_transmit;
1530b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_qflush = gif_qflush;
154fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
155dd4490fdSAndrey V. Elsukov #ifdef VIMAGE
156dd4490fdSAndrey V. Elsukov 	GIF2IFP(sc)->if_reassign = gif_reassign;
157dd4490fdSAndrey V. Elsukov #endif
158f1aaad0cSHiroki Sato 	GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
159f1aaad0cSHiroki Sato 	GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
160fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
16101399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
16294408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
163fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
16425af0bb5SGleb Smirnoff 
16525af0bb5SGleb Smirnoff 	return (0);
166cfa1ca9dSYoshinobu Inoue }
167cfa1ca9dSYoshinobu Inoue 
168dd4490fdSAndrey V. Elsukov #ifdef VIMAGE
169dd4490fdSAndrey V. Elsukov static void
170dd4490fdSAndrey V. Elsukov gif_reassign(struct ifnet *ifp, struct vnet *new_vnet __unused,
171dd4490fdSAndrey V. Elsukov     char *unused __unused)
172dd4490fdSAndrey V. Elsukov {
173dd4490fdSAndrey V. Elsukov 	struct gif_softc *sc;
174dd4490fdSAndrey V. Elsukov 
175dd4490fdSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
176dd4490fdSAndrey V. Elsukov 	sc = ifp->if_softc;
177dd4490fdSAndrey V. Elsukov 	if (sc != NULL)
178dd4490fdSAndrey V. Elsukov 		gif_delete_tunnel(sc);
179dd4490fdSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
180dd4490fdSAndrey V. Elsukov }
181dd4490fdSAndrey V. Elsukov #endif /* VIMAGE */
182dd4490fdSAndrey V. Elsukov 
18317d5cb2dSRobert Watson static void
184c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp)
18553dab5feSBrooks Davis {
1860b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
187febd0759SAndrew Thompson 
1880b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
1890b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
190b941bc1dSAndrey V. Elsukov 	gif_delete_tunnel(sc);
19194408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
19294408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
19353dab5feSBrooks Davis 	bpfdetach(ifp);
19453dab5feSBrooks Davis 	if_detach(ifp);
1950b9f5f8aSAndrey V. Elsukov 	ifp->if_softc = NULL;
1960b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
1970b9f5f8aSAndrey V. Elsukov 
198b941bc1dSAndrey V. Elsukov 	GIF_WAIT();
199fc74a9f9SBrooks Davis 	if_free(ifp);
20053dab5feSBrooks Davis 	free(sc, M_GIF);
20153dab5feSBrooks Davis }
20253dab5feSBrooks Davis 
203d0728d71SRobert Watson static void
204d0728d71SRobert Watson vnet_gif_init(const void *unused __unused)
2051ed81b73SMarko Zec {
2061ed81b73SMarko Zec 
207a7f5886eSHiroki Sato 	V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
208a7f5886eSHiroki Sato 	    gif_clone_destroy, 0);
209b941bc1dSAndrey V. Elsukov #ifdef INET
210b941bc1dSAndrey V. Elsukov 	in_gif_init();
211b941bc1dSAndrey V. Elsukov #endif
212b941bc1dSAndrey V. Elsukov #ifdef INET6
213b941bc1dSAndrey V. Elsukov 	in6_gif_init();
214b941bc1dSAndrey V. Elsukov #endif
2151ed81b73SMarko Zec }
2168ca6c11aSKristof Provost VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
217a7f5886eSHiroki Sato     vnet_gif_init, NULL);
218a7f5886eSHiroki Sato 
219a7f5886eSHiroki Sato static void
220a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused)
221a7f5886eSHiroki Sato {
222a7f5886eSHiroki Sato 
223a7f5886eSHiroki Sato 	if_clone_detach(V_gif_cloner);
224b941bc1dSAndrey V. Elsukov #ifdef INET
225b941bc1dSAndrey V. Elsukov 	in_gif_uninit();
226b941bc1dSAndrey V. Elsukov #endif
227b941bc1dSAndrey V. Elsukov #ifdef INET6
228b941bc1dSAndrey V. Elsukov 	in6_gif_uninit();
229b941bc1dSAndrey V. Elsukov #endif
230a7f5886eSHiroki Sato }
2318ca6c11aSKristof Provost VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
232a7f5886eSHiroki Sato     vnet_gif_uninit, NULL);
2331ed81b73SMarko Zec 
2341ed81b73SMarko Zec static int
235c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data)
23653dab5feSBrooks Davis {
23753dab5feSBrooks Davis 
23853dab5feSBrooks Davis 	switch (type) {
23953dab5feSBrooks Davis 	case MOD_LOAD:
24053dab5feSBrooks Davis 	case MOD_UNLOAD:
24153dab5feSBrooks Davis 		break;
2423e019deaSPoul-Henning Kamp 	default:
243a7f5886eSHiroki Sato 		return (EOPNOTSUPP);
24453dab5feSBrooks Davis 	}
245a7f5886eSHiroki Sato 	return (0);
24653dab5feSBrooks Davis }
24753dab5feSBrooks Davis 
24853dab5feSBrooks Davis static moduledata_t gif_mod = {
24953dab5feSBrooks Davis 	"if_gif",
25053dab5feSBrooks Davis 	gifmodevent,
2519823d527SKevin Lo 	0
25253dab5feSBrooks Davis };
25353dab5feSBrooks Davis 
25453dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
25520af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
256cfa1ca9dSYoshinobu Inoue 
257b941bc1dSAndrey V. Elsukov struct gif_list *
258b941bc1dSAndrey V. Elsukov gif_hashinit(void)
259686cdd19SJun-ichiro itojun Hagino {
260b941bc1dSAndrey V. Elsukov 	struct gif_list *hash;
261b941bc1dSAndrey V. Elsukov 	int i;
262686cdd19SJun-ichiro itojun Hagino 
263b941bc1dSAndrey V. Elsukov 	hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE,
264b941bc1dSAndrey V. Elsukov 	    M_GIF, M_WAITOK);
265b941bc1dSAndrey V. Elsukov 	for (i = 0; i < GIF_HASH_SIZE; i++)
266b941bc1dSAndrey V. Elsukov 		CK_LIST_INIT(&hash[i]);
267686cdd19SJun-ichiro itojun Hagino 
268b941bc1dSAndrey V. Elsukov 	return (hash);
269686cdd19SJun-ichiro itojun Hagino }
270686cdd19SJun-ichiro itojun Hagino 
271b941bc1dSAndrey V. Elsukov void
272b941bc1dSAndrey V. Elsukov gif_hashdestroy(struct gif_list *hash)
273b941bc1dSAndrey V. Elsukov {
2743bb61ca6SHajimu UMEMOTO 
275b941bc1dSAndrey V. Elsukov 	free(hash, M_GIF);
276686cdd19SJun-ichiro itojun Hagino }
277686cdd19SJun-ichiro itojun Hagino 
27898a8fdf6SAndrey V. Elsukov #define	MTAG_GIF	1080679712
2790b9f5f8aSAndrey V. Elsukov static int
2800b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m)
28173ff045cSAndrew Thompson {
28273ff045cSAndrew Thompson 	struct gif_softc *sc;
2830b9f5f8aSAndrey V. Elsukov 	struct etherip_header *eth;
284776b7288SRandall Stewart #ifdef INET
2850b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
286776b7288SRandall Stewart #endif
287776b7288SRandall Stewart #ifdef INET6
2880b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
2890b9f5f8aSAndrey V. Elsukov 	uint32_t t;
290776b7288SRandall Stewart #endif
2910b9f5f8aSAndrey V. Elsukov 	uint32_t af;
2920b9f5f8aSAndrey V. Elsukov 	uint8_t proto, ecn;
2930b9f5f8aSAndrey V. Elsukov 	int error;
2940b9f5f8aSAndrey V. Elsukov 
2959758a507SGleb Smirnoff 	NET_EPOCH_ASSERT();
2969c0265c6SAndrey V. Elsukov #ifdef MAC
2979c0265c6SAndrey V. Elsukov 	error = mac_ifnet_check_transmit(ifp, m);
2989c0265c6SAndrey V. Elsukov 	if (error) {
2999c0265c6SAndrey V. Elsukov 		m_freem(m);
3009c0265c6SAndrey V. Elsukov 		goto err;
3019c0265c6SAndrey V. Elsukov 	}
3029c0265c6SAndrey V. Elsukov #endif
3030b9f5f8aSAndrey V. Elsukov 	error = ENETDOWN;
3040b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
3059c0265c6SAndrey V. Elsukov 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
3069c0265c6SAndrey V. Elsukov 	    (ifp->if_flags & IFF_UP) == 0 ||
307009d82eeSAndrey V. Elsukov 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
3089c0265c6SAndrey V. Elsukov 	    sc->gif_family == 0 ||
30998a8fdf6SAndrey V. Elsukov 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF,
31098a8fdf6SAndrey V. Elsukov 		V_max_gif_nesting)) != 0) {
3110b9f5f8aSAndrey V. Elsukov 		m_freem(m);
3120b9f5f8aSAndrey V. Elsukov 		goto err;
3130b9f5f8aSAndrey V. Elsukov 	}
3140b9f5f8aSAndrey V. Elsukov 	/* Now pull back the af that we stashed in the csum_data. */
3159c0265c6SAndrey V. Elsukov 	if (ifp->if_bridge)
3169c0265c6SAndrey V. Elsukov 		af = AF_LINK;
3179c0265c6SAndrey V. Elsukov 	else
318cef68c63SRandall Stewart 		af = m->m_pkthdr.csum_data;
3199c0265c6SAndrey V. Elsukov 	m->m_flags &= ~(M_BCAST|M_MCAST);
3209c0265c6SAndrey V. Elsukov 	M_SETFIB(m, sc->gif_fibnum);
321776b7288SRandall Stewart 	BPF_MTAP2(ifp, &af, sizeof(af), m);
3223751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3230b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
324776b7288SRandall Stewart 	/* inner AF-specific encapsulation */
3250b9f5f8aSAndrey V. Elsukov 	ecn = 0;
3260b9f5f8aSAndrey V. Elsukov 	switch (af) {
327776b7288SRandall Stewart #ifdef INET
328776b7288SRandall Stewart 	case AF_INET:
3290b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV4;
3300b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
3310b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
3320b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3330b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3340b9f5f8aSAndrey V. Elsukov 			goto err;
3350b9f5f8aSAndrey V. Elsukov 		}
3360b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
3370b9f5f8aSAndrey V. Elsukov 		ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3380b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos);
339776b7288SRandall Stewart 		break;
340776b7288SRandall Stewart #endif
341776b7288SRandall Stewart #ifdef INET6
342776b7288SRandall Stewart 	case AF_INET6:
3430b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV6;
3440b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
3450b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
3460b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3470b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3480b9f5f8aSAndrey V. Elsukov 			goto err;
3490b9f5f8aSAndrey V. Elsukov 		}
3500b9f5f8aSAndrey V. Elsukov 		t = 0;
3510b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
3520b9f5f8aSAndrey V. Elsukov 		ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3530b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow);
3540b9f5f8aSAndrey V. Elsukov 		ecn = (ntohl(t) >> 20) & 0xff;
3550b9f5f8aSAndrey V. Elsukov 		break;
3560b9f5f8aSAndrey V. Elsukov #endif
3570b9f5f8aSAndrey V. Elsukov 	case AF_LINK:
3580b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_ETHERIP;
3590b9f5f8aSAndrey V. Elsukov 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
3600b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3610b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3620b9f5f8aSAndrey V. Elsukov 			goto err;
3630b9f5f8aSAndrey V. Elsukov 		}
3640b9f5f8aSAndrey V. Elsukov 		eth = mtod(m, struct etherip_header *);
3650b9f5f8aSAndrey V. Elsukov 		eth->eip_resvh = 0;
3660b9f5f8aSAndrey V. Elsukov 		eth->eip_ver = ETHERIP_VERSION;
3670b9f5f8aSAndrey V. Elsukov 		eth->eip_resvl = 0;
3680b9f5f8aSAndrey V. Elsukov 		break;
3690b9f5f8aSAndrey V. Elsukov 	default:
3700b9f5f8aSAndrey V. Elsukov 		error = EAFNOSUPPORT;
3710b9f5f8aSAndrey V. Elsukov 		m_freem(m);
3720b9f5f8aSAndrey V. Elsukov 		goto err;
3730b9f5f8aSAndrey V. Elsukov 	}
3740b9f5f8aSAndrey V. Elsukov 	/* XXX should we check if our outer source is legal? */
3750b9f5f8aSAndrey V. Elsukov 	/* dispatch to output logic based on outer AF */
3760b9f5f8aSAndrey V. Elsukov 	switch (sc->gif_family) {
3770b9f5f8aSAndrey V. Elsukov #ifdef INET
3780b9f5f8aSAndrey V. Elsukov 	case AF_INET:
3790b9f5f8aSAndrey V. Elsukov 		error = in_gif_output(ifp, m, proto, ecn);
3800b9f5f8aSAndrey V. Elsukov 		break;
3810b9f5f8aSAndrey V. Elsukov #endif
3820b9f5f8aSAndrey V. Elsukov #ifdef INET6
3830b9f5f8aSAndrey V. Elsukov 	case AF_INET6:
3840b9f5f8aSAndrey V. Elsukov 		error = in6_gif_output(ifp, m, proto, ecn);
385776b7288SRandall Stewart 		break;
386776b7288SRandall Stewart #endif
387776b7288SRandall Stewart 	default:
388776b7288SRandall Stewart 		m_freem(m);
389776b7288SRandall Stewart 	}
3900b9f5f8aSAndrey V. Elsukov err:
391776b7288SRandall Stewart 	if (error)
3923751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3930b9f5f8aSAndrey V. Elsukov 	return (error);
39473ff045cSAndrew Thompson }
3950b9f5f8aSAndrey V. Elsukov 
3960b9f5f8aSAndrey V. Elsukov static void
3970b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused)
3980b9f5f8aSAndrey V. Elsukov {
3990b9f5f8aSAndrey V. Elsukov 
40073ff045cSAndrew Thompson }
40173ff045cSAndrew Thompson 
402cfa1ca9dSYoshinobu Inoue int
40347e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
40447e8d432SGleb Smirnoff 	struct route *ro)
405cfa1ca9dSYoshinobu Inoue {
406776b7288SRandall Stewart 	uint32_t af;
40710722b85SRobert Watson 
4088a030872SMarius Strobl 	KASSERT(ifp->if_bridge == NULL,
4098a030872SMarius Strobl 	    ("%s: unexpectedly called with bridge attached", __func__));
4108a030872SMarius Strobl 
411*2cb0fce2SSeth Hoffert 	/* BPF writes need to be handled specially. */
412*2cb0fce2SSeth Hoffert 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
413e82d7b29SMarius Strobl 		memcpy(&af, dst->sa_data, sizeof(af));
41447e8d432SGleb Smirnoff 	else
41562e1a437SZhenlei Huang 		af = RO_GET_FAMILY(ro, dst);
41640138788SRandall Stewart 	/*
4170b9f5f8aSAndrey V. Elsukov 	 * Now save the af in the inbound pkt csum data, this is a cheat since
4180b9f5f8aSAndrey V. Elsukov 	 * we are using the inbound csum_data field to carry the af over to
4190b9f5f8aSAndrey V. Elsukov 	 * the gif_transmit() routine, avoiding using yet another mtag.
420776b7288SRandall Stewart 	 */
421cef68c63SRandall Stewart 	m->m_pkthdr.csum_data = af;
4220b9f5f8aSAndrey V. Elsukov 	return (ifp->if_transmit(ifp, m));
423cfa1ca9dSYoshinobu Inoue }
424cfa1ca9dSYoshinobu Inoue 
425cfa1ca9dSYoshinobu Inoue void
4260b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
427cfa1ca9dSYoshinobu Inoue {
42873ff045cSAndrew Thompson 	struct etherip_header *eip;
4290b9f5f8aSAndrey V. Elsukov #ifdef INET
4300b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
4310b9f5f8aSAndrey V. Elsukov #endif
4320b9f5f8aSAndrey V. Elsukov #ifdef INET6
4330b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
4340b9f5f8aSAndrey V. Elsukov 	uint32_t t;
4350b9f5f8aSAndrey V. Elsukov #endif
43656abdd33SAndrew Thompson 	struct ether_header *eh;
43756abdd33SAndrew Thompson 	struct ifnet *oldifp;
4380b9f5f8aSAndrey V. Elsukov 	int isr, n, af;
439cfa1ca9dSYoshinobu Inoue 
440b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
441b8a6e03fSGleb Smirnoff 
44221fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
443cfa1ca9dSYoshinobu Inoue 		/* just in case */
444cfa1ca9dSYoshinobu Inoue 		m_freem(m);
445cfa1ca9dSYoshinobu Inoue 		return;
446cfa1ca9dSYoshinobu Inoue 	}
44721fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
4485b7a43f5SAndrey V. Elsukov 	m_clrprotoflags(m);
4490b9f5f8aSAndrey V. Elsukov 	switch (proto) {
4500b9f5f8aSAndrey V. Elsukov #ifdef INET
4510b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV4:
4520b9f5f8aSAndrey V. Elsukov 		af = AF_INET;
4530b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
4540b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
4550b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
4560b9f5f8aSAndrey V. Elsukov 			goto drop;
4570b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
4580b9f5f8aSAndrey V. Elsukov 		if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
4590b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
4600b9f5f8aSAndrey V. Elsukov 			m_freem(m);
4610b9f5f8aSAndrey V. Elsukov 			goto drop;
4620b9f5f8aSAndrey V. Elsukov 		}
4630b9f5f8aSAndrey V. Elsukov 		break;
4640b9f5f8aSAndrey V. Elsukov #endif
4650b9f5f8aSAndrey V. Elsukov #ifdef INET6
4660b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV6:
4670b9f5f8aSAndrey V. Elsukov 		af = AF_INET6;
4680b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
4690b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
4700b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
4710b9f5f8aSAndrey V. Elsukov 			goto drop;
4720b9f5f8aSAndrey V. Elsukov 		t = htonl((uint32_t)ecn << 20);
4730b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
4740b9f5f8aSAndrey V. Elsukov 		if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
4750b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
4760b9f5f8aSAndrey V. Elsukov 			m_freem(m);
4770b9f5f8aSAndrey V. Elsukov 			goto drop;
4780b9f5f8aSAndrey V. Elsukov 		}
4790b9f5f8aSAndrey V. Elsukov 		break;
4800b9f5f8aSAndrey V. Elsukov #endif
4810b9f5f8aSAndrey V. Elsukov 	case IPPROTO_ETHERIP:
4820b9f5f8aSAndrey V. Elsukov 		af = AF_LINK;
4830b9f5f8aSAndrey V. Elsukov 		break;
4840b9f5f8aSAndrey V. Elsukov 	default:
4850b9f5f8aSAndrey V. Elsukov 		m_freem(m);
4860b9f5f8aSAndrey V. Elsukov 		goto drop;
4870b9f5f8aSAndrey V. Elsukov 	}
488cfa1ca9dSYoshinobu Inoue 
48910722b85SRobert Watson #ifdef MAC
49030d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
49110722b85SRobert Watson #endif
49210722b85SRobert Watson 
49316d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
4940b9f5f8aSAndrey V. Elsukov 		uint32_t af1 = af;
495437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
496cfa1ca9dSYoshinobu Inoue 	}
497cfa1ca9dSYoshinobu Inoue 
498e9f947e2SHiroki Sato 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
4993751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
5003751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
501e9f947e2SHiroki Sato 		m_freem(m);
502e9f947e2SHiroki Sato 		return;
503e9f947e2SHiroki Sato 	}
504e9f947e2SHiroki Sato 
50594408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
50621fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
50794408d94SBrooks Davis 		if (m == NULL)
5080b9f5f8aSAndrey V. Elsukov 			goto drop;
50994408d94SBrooks Davis 	}
51094408d94SBrooks Davis 
511cfa1ca9dSYoshinobu Inoue 	/*
512cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
513cfa1ca9dSYoshinobu Inoue 	 * specified address family.
514cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
515cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
516cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
517cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
518cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
51988ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
52088ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
521cfa1ca9dSYoshinobu Inoue 	 */
522cfa1ca9dSYoshinobu Inoue 	switch (af) {
523cfa1ca9dSYoshinobu Inoue #ifdef INET
524cfa1ca9dSYoshinobu Inoue 	case AF_INET:
525cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
526cfa1ca9dSYoshinobu Inoue 		break;
527cfa1ca9dSYoshinobu Inoue #endif
528cfa1ca9dSYoshinobu Inoue #ifdef INET6
529cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
530cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
531cfa1ca9dSYoshinobu Inoue 		break;
532cfa1ca9dSYoshinobu Inoue #endif
53373ff045cSAndrew Thompson 	case AF_LINK:
534b941bc1dSAndrey V. Elsukov 		n = sizeof(struct etherip_header) +
535b941bc1dSAndrey V. Elsukov 		    sizeof(struct ether_header);
5360b9f5f8aSAndrey V. Elsukov 		if (n > m->m_len)
53773ff045cSAndrew Thompson 			m = m_pullup(m, n);
5380b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
5390b9f5f8aSAndrey V. Elsukov 			goto drop;
54073ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
541dbe59260SHiroki Sato 		if (eip->eip_ver != ETHERIP_VERSION) {
542dbe59260SHiroki Sato 			/* discard unknown versions */
543dbe59260SHiroki Sato 			m_freem(m);
5440b9f5f8aSAndrey V. Elsukov 			goto drop;
545dbe59260SHiroki Sato 		}
546e243367bSKonstantin Belousov 
547e243367bSKonstantin Belousov 		m_adj_decap(m, sizeof(struct etherip_header));
54873ff045cSAndrew Thompson 
54973ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
55073ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
55173ff045cSAndrew Thompson 
55256abdd33SAndrew Thompson 		if (ifp->if_bridge) {
55356abdd33SAndrew Thompson 			oldifp = ifp;
55456abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
55556abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
55656abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
55756abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
55856abdd33SAndrew Thompson 				else
55956abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
5603751dddbSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
56156abdd33SAndrew Thompson 			}
56273ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
56373ff045cSAndrew Thompson 
56456abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
56556abdd33SAndrew Thompson 				/*
56656abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
56756abdd33SAndrew Thompson 				 * members for which the frame is addressed.
56856abdd33SAndrew Thompson 				 */
56956abdd33SAndrew Thompson 				ether_demux(ifp, m);
57056abdd33SAndrew Thompson 				return;
57156abdd33SAndrew Thompson 			}
57256abdd33SAndrew Thompson 		}
57373ff045cSAndrew Thompson 		if (m != NULL)
57473ff045cSAndrew Thompson 			m_freem(m);
57573ff045cSAndrew Thompson 		return;
57673ff045cSAndrew Thompson 
577cfa1ca9dSYoshinobu Inoue 	default:
57894408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
57921fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
58094408d94SBrooks Davis 		else
581cfa1ca9dSYoshinobu Inoue 			m_freem(m);
582cfa1ca9dSYoshinobu Inoue 		return;
583cfa1ca9dSYoshinobu Inoue 	}
584cfa1ca9dSYoshinobu Inoue 
5853751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
5863751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
587a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
5881cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
5890b9f5f8aSAndrey V. Elsukov 	return;
5900b9f5f8aSAndrey V. Elsukov drop:
5910b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
592cfa1ca9dSYoshinobu Inoue }
593cfa1ca9dSYoshinobu Inoue 
594b941bc1dSAndrey V. Elsukov static int
595c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
596cfa1ca9dSYoshinobu Inoue {
597cfa1ca9dSYoshinobu Inoue 	struct ifreq *ifr = (struct ifreq*)data;
5980b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
5990b9f5f8aSAndrey V. Elsukov 	u_int options;
6000b9f5f8aSAndrey V. Elsukov 	int error;
601cfa1ca9dSYoshinobu Inoue 
602cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
603cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6049426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
605cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
606cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
607cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
6080b9f5f8aSAndrey V. Elsukov 	case SIOCSIFFLAGS:
6090b9f5f8aSAndrey V. Elsukov 		return (0);
610cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
6110b9f5f8aSAndrey V. Elsukov 		if (ifr->ifr_mtu < GIF_MTU_MIN ||
6120b9f5f8aSAndrey V. Elsukov 		    ifr->ifr_mtu > GIF_MTU_MAX)
613cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
6140b9f5f8aSAndrey V. Elsukov 		else
6150b9f5f8aSAndrey V. Elsukov 			ifp->if_mtu = ifr->ifr_mtu;
6160b9f5f8aSAndrey V. Elsukov 		return (0);
6170b9f5f8aSAndrey V. Elsukov 	}
6180b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
6190b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
6200b9f5f8aSAndrey V. Elsukov 	if (sc == NULL) {
6210b9f5f8aSAndrey V. Elsukov 		error = ENXIO;
6220b9f5f8aSAndrey V. Elsukov 		goto bad;
6230b9f5f8aSAndrey V. Elsukov 	}
6240b9f5f8aSAndrey V. Elsukov 	error = 0;
6250b9f5f8aSAndrey V. Elsukov 	switch (cmd) {
626686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
627b941bc1dSAndrey V. Elsukov 		if (sc->gif_family == 0)
628686cdd19SJun-ichiro itojun Hagino 			break;
629b941bc1dSAndrey V. Elsukov 		gif_delete_tunnel(sc);
6300b9f5f8aSAndrey V. Elsukov 		break;
631cfa1ca9dSYoshinobu Inoue #ifdef INET
632b941bc1dSAndrey V. Elsukov 	case SIOCSIFPHYADDR:
63333841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR:
6340b9f5f8aSAndrey V. Elsukov 	case SIOCGIFPDSTADDR:
635b941bc1dSAndrey V. Elsukov 		error = in_gif_ioctl(sc, cmd, data);
6360b9f5f8aSAndrey V. Elsukov 		break;
6370b9f5f8aSAndrey V. Elsukov #endif
638cfa1ca9dSYoshinobu Inoue #ifdef INET6
639b941bc1dSAndrey V. Elsukov 	case SIOCSIFPHYADDR_IN6:
64033841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR_IN6:
641cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
642b941bc1dSAndrey V. Elsukov 		error = in6_gif_ioctl(sc, cmd, data);
6430b9f5f8aSAndrey V. Elsukov 		break;
6440b9f5f8aSAndrey V. Elsukov #endif
645eccfe69aSAndrey V. Elsukov 	case SIOCGTUNFIB:
646eccfe69aSAndrey V. Elsukov 		ifr->ifr_fib = sc->gif_fibnum;
647eccfe69aSAndrey V. Elsukov 		break;
648eccfe69aSAndrey V. Elsukov 	case SIOCSTUNFIB:
649eccfe69aSAndrey V. Elsukov 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
650eccfe69aSAndrey V. Elsukov 			break;
651eccfe69aSAndrey V. Elsukov 		if (ifr->ifr_fib >= rt_numfibs)
652eccfe69aSAndrey V. Elsukov 			error = EINVAL;
653eccfe69aSAndrey V. Elsukov 		else
654eccfe69aSAndrey V. Elsukov 			sc->gif_fibnum = ifr->ifr_fib;
655eccfe69aSAndrey V. Elsukov 		break;
656dbe59260SHiroki Sato 	case GIFGOPTS:
657dbe59260SHiroki Sato 		options = sc->gif_options;
658541d96aaSBrooks Davis 		error = copyout(&options, ifr_data_get_ptr(ifr),
659541d96aaSBrooks Davis 		    sizeof(options));
660dbe59260SHiroki Sato 		break;
661dbe59260SHiroki Sato 	case GIFSOPTS:
662dbe59260SHiroki Sato 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
663dbe59260SHiroki Sato 			break;
664541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &options,
665541d96aaSBrooks Davis 		    sizeof(options));
6664cd5f57dSHiroki Sato 		if (error)
6674cd5f57dSHiroki Sato 			break;
668b941bc1dSAndrey V. Elsukov 		if (options & ~GIF_OPTMASK) {
669dbe59260SHiroki Sato 			error = EINVAL;
670b941bc1dSAndrey V. Elsukov 			break;
671b941bc1dSAndrey V. Elsukov 		}
672b941bc1dSAndrey V. Elsukov 		if (sc->gif_options != options) {
673b941bc1dSAndrey V. Elsukov 			switch (sc->gif_family) {
674b941bc1dSAndrey V. Elsukov #ifdef INET
675b941bc1dSAndrey V. Elsukov 			case AF_INET:
676b941bc1dSAndrey V. Elsukov 				error = in_gif_setopts(sc, options);
677b941bc1dSAndrey V. Elsukov 				break;
678b941bc1dSAndrey V. Elsukov #endif
679b941bc1dSAndrey V. Elsukov #ifdef INET6
680b941bc1dSAndrey V. Elsukov 			case AF_INET6:
681b941bc1dSAndrey V. Elsukov 				error = in6_gif_setopts(sc, options);
682b941bc1dSAndrey V. Elsukov 				break;
683b941bc1dSAndrey V. Elsukov #endif
684b941bc1dSAndrey V. Elsukov 			default:
685b941bc1dSAndrey V. Elsukov 				/* No need to invoke AF-handler */
6864cd5f57dSHiroki Sato 				sc->gif_options = options;
687b941bc1dSAndrey V. Elsukov 			}
688b941bc1dSAndrey V. Elsukov 		}
689dbe59260SHiroki Sato 		break;
690cfa1ca9dSYoshinobu Inoue 	default:
691cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
692cfa1ca9dSYoshinobu Inoue 		break;
693cfa1ca9dSYoshinobu Inoue 	}
694b941bc1dSAndrey V. Elsukov 	if (error == 0 && sc->gif_family != 0) {
695b941bc1dSAndrey V. Elsukov 		if (
696b941bc1dSAndrey V. Elsukov #ifdef INET
697b941bc1dSAndrey V. Elsukov 		    cmd == SIOCSIFPHYADDR ||
698b941bc1dSAndrey V. Elsukov #endif
699b941bc1dSAndrey V. Elsukov #ifdef INET6
700b941bc1dSAndrey V. Elsukov 		    cmd == SIOCSIFPHYADDR_IN6 ||
701b941bc1dSAndrey V. Elsukov #endif
702b941bc1dSAndrey V. Elsukov 		    0) {
703b941bc1dSAndrey V. Elsukov 			if_link_state_change(ifp, LINK_STATE_UP);
704b941bc1dSAndrey V. Elsukov 		}
705b941bc1dSAndrey V. Elsukov 	}
706cfa1ca9dSYoshinobu Inoue bad:
7070b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
7080b9f5f8aSAndrey V. Elsukov 	return (error);
709cfa1ca9dSYoshinobu Inoue }
71053dab5feSBrooks Davis 
7110b9f5f8aSAndrey V. Elsukov static void
712b941bc1dSAndrey V. Elsukov gif_delete_tunnel(struct gif_softc *sc)
7130b9f5f8aSAndrey V. Elsukov {
7140b9f5f8aSAndrey V. Elsukov 
7150b9f5f8aSAndrey V. Elsukov 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
716b941bc1dSAndrey V. Elsukov 	if (sc->gif_family != 0) {
717009d82eeSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, srchash);
718b941bc1dSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, chain);
719b941bc1dSAndrey V. Elsukov 		/* Wait until it become safe to free gif_hdr */
720b941bc1dSAndrey V. Elsukov 		GIF_WAIT();
7210b9f5f8aSAndrey V. Elsukov 		free(sc->gif_hdr, M_GIF);
722f1aaad0cSHiroki Sato 	}
7230b9f5f8aSAndrey V. Elsukov 	sc->gif_family = 0;
724b941bc1dSAndrey V. Elsukov 	GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
725b941bc1dSAndrey V. Elsukov 	if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN);
72653dab5feSBrooks Davis }
727