xref: /freebsd/sys/net/if_gif.c (revision b8a6e03fac922677455d8e0977831506cf8212e8)
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 
107b941bc1dSAndrey V. Elsukov static void	gif_delete_tunnel(struct gif_softc *);
1080b9f5f8aSAndrey V. Elsukov static int	gif_ioctl(struct ifnet *, u_long, caddr_t);
1090b9f5f8aSAndrey V. Elsukov static int	gif_transmit(struct ifnet *, struct mbuf *);
1100b9f5f8aSAndrey V. Elsukov static void	gif_qflush(struct ifnet *);
1116b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
112bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
1135f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, gif_cloner);
114a7f5886eSHiroki Sato #define	V_gif_cloner	VNET(gif_cloner)
11553dab5feSBrooks Davis 
116872f786aSBrooks Davis SYSCTL_DECL(_net_link);
1176472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
118872f786aSBrooks Davis     "Generic Tunnel Interface");
119686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
120686cdd19SJun-ichiro itojun Hagino /*
121872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
122686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
123686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
124686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
125686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
126686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
127686cdd19SJun-ichiro itojun Hagino  */
128686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
129686cdd19SJun-ichiro itojun Hagino #endif
1305f901c92SAndrew Turner VNET_DEFINE_STATIC(int, max_gif_nesting) = MAX_GIF_NEST;
131d0728d71SRobert Watson #define	V_max_gif_nesting	VNET(max_gif_nesting)
1326df8a710SGleb Smirnoff SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_VNET | CTLFLAG_RW,
133eddfbb76SRobert Watson     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
1348b615593SMarko Zec 
135bb2bfb4fSBrooks Davis static int
136c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
137cfa1ca9dSYoshinobu Inoue {
13833841545SHajimu UMEMOTO 	struct gif_softc *sc;
139cfa1ca9dSYoshinobu Inoue 
140e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1418b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
142fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
143fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
14442a58907SGleb Smirnoff 	if_initname(GIF2IFP(sc), gifname, unit);
145686cdd19SJun-ichiro itojun Hagino 
146fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
147fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
148fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
149fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
1500b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_transmit = gif_transmit;
1510b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_qflush = gif_qflush;
152fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
153f1aaad0cSHiroki Sato 	GIF2IFP(sc)->if_capabilities |= IFCAP_LINKSTATE;
154f1aaad0cSHiroki Sato 	GIF2IFP(sc)->if_capenable |= IFCAP_LINKSTATE;
155fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
15601399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
15794408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
158fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
15925af0bb5SGleb Smirnoff 
16025af0bb5SGleb Smirnoff 	return (0);
161cfa1ca9dSYoshinobu Inoue }
162cfa1ca9dSYoshinobu Inoue 
16317d5cb2dSRobert Watson static void
164c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp)
16553dab5feSBrooks Davis {
1660b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
167febd0759SAndrew Thompson 
1680b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
1690b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
170b941bc1dSAndrey V. Elsukov 	gif_delete_tunnel(sc);
17194408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
17294408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
17353dab5feSBrooks Davis 	bpfdetach(ifp);
17453dab5feSBrooks Davis 	if_detach(ifp);
1750b9f5f8aSAndrey V. Elsukov 	ifp->if_softc = NULL;
1760b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
1770b9f5f8aSAndrey V. Elsukov 
178b941bc1dSAndrey V. Elsukov 	GIF_WAIT();
179fc74a9f9SBrooks Davis 	if_free(ifp);
18053dab5feSBrooks Davis 	free(sc, M_GIF);
18153dab5feSBrooks Davis }
18253dab5feSBrooks Davis 
183d0728d71SRobert Watson static void
184d0728d71SRobert Watson vnet_gif_init(const void *unused __unused)
1851ed81b73SMarko Zec {
1861ed81b73SMarko Zec 
187a7f5886eSHiroki Sato 	V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
188a7f5886eSHiroki Sato 	    gif_clone_destroy, 0);
189b941bc1dSAndrey V. Elsukov #ifdef INET
190b941bc1dSAndrey V. Elsukov 	in_gif_init();
191b941bc1dSAndrey V. Elsukov #endif
192b941bc1dSAndrey V. Elsukov #ifdef INET6
193b941bc1dSAndrey V. Elsukov 	in6_gif_init();
194b941bc1dSAndrey V. Elsukov #endif
1951ed81b73SMarko Zec }
196a7f5886eSHiroki Sato VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
197a7f5886eSHiroki Sato     vnet_gif_init, NULL);
198a7f5886eSHiroki Sato 
199a7f5886eSHiroki Sato static void
200a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused)
201a7f5886eSHiroki Sato {
202a7f5886eSHiroki Sato 
203a7f5886eSHiroki Sato 	if_clone_detach(V_gif_cloner);
204b941bc1dSAndrey V. Elsukov #ifdef INET
205b941bc1dSAndrey V. Elsukov 	in_gif_uninit();
206b941bc1dSAndrey V. Elsukov #endif
207b941bc1dSAndrey V. Elsukov #ifdef INET6
208b941bc1dSAndrey V. Elsukov 	in6_gif_uninit();
209b941bc1dSAndrey V. Elsukov #endif
210a7f5886eSHiroki Sato }
211a7f5886eSHiroki Sato VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
212a7f5886eSHiroki Sato     vnet_gif_uninit, NULL);
2131ed81b73SMarko Zec 
2141ed81b73SMarko Zec static int
215c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data)
21653dab5feSBrooks Davis {
21753dab5feSBrooks Davis 
21853dab5feSBrooks Davis 	switch (type) {
21953dab5feSBrooks Davis 	case MOD_LOAD:
22053dab5feSBrooks Davis 	case MOD_UNLOAD:
22153dab5feSBrooks Davis 		break;
2223e019deaSPoul-Henning Kamp 	default:
223a7f5886eSHiroki Sato 		return (EOPNOTSUPP);
22453dab5feSBrooks Davis 	}
225a7f5886eSHiroki Sato 	return (0);
22653dab5feSBrooks Davis }
22753dab5feSBrooks Davis 
22853dab5feSBrooks Davis static moduledata_t gif_mod = {
22953dab5feSBrooks Davis 	"if_gif",
23053dab5feSBrooks Davis 	gifmodevent,
2319823d527SKevin Lo 	0
23253dab5feSBrooks Davis };
23353dab5feSBrooks Davis 
23453dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
23520af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
236cfa1ca9dSYoshinobu Inoue 
237b941bc1dSAndrey V. Elsukov struct gif_list *
238b941bc1dSAndrey V. Elsukov gif_hashinit(void)
239686cdd19SJun-ichiro itojun Hagino {
240b941bc1dSAndrey V. Elsukov 	struct gif_list *hash;
241b941bc1dSAndrey V. Elsukov 	int i;
242686cdd19SJun-ichiro itojun Hagino 
243b941bc1dSAndrey V. Elsukov 	hash = malloc(sizeof(struct gif_list) * GIF_HASH_SIZE,
244b941bc1dSAndrey V. Elsukov 	    M_GIF, M_WAITOK);
245b941bc1dSAndrey V. Elsukov 	for (i = 0; i < GIF_HASH_SIZE; i++)
246b941bc1dSAndrey V. Elsukov 		CK_LIST_INIT(&hash[i]);
247686cdd19SJun-ichiro itojun Hagino 
248b941bc1dSAndrey V. Elsukov 	return (hash);
249686cdd19SJun-ichiro itojun Hagino }
250686cdd19SJun-ichiro itojun Hagino 
251b941bc1dSAndrey V. Elsukov void
252b941bc1dSAndrey V. Elsukov gif_hashdestroy(struct gif_list *hash)
253b941bc1dSAndrey V. Elsukov {
2543bb61ca6SHajimu UMEMOTO 
255b941bc1dSAndrey V. Elsukov 	free(hash, M_GIF);
256686cdd19SJun-ichiro itojun Hagino }
257686cdd19SJun-ichiro itojun Hagino 
25898a8fdf6SAndrey V. Elsukov #define	MTAG_GIF	1080679712
2590b9f5f8aSAndrey V. Elsukov static int
2600b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m)
26173ff045cSAndrew Thompson {
26273ff045cSAndrew Thompson 	struct gif_softc *sc;
2630b9f5f8aSAndrey V. Elsukov 	struct etherip_header *eth;
264776b7288SRandall Stewart #ifdef INET
2650b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
266776b7288SRandall Stewart #endif
267776b7288SRandall Stewart #ifdef INET6
2680b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
2690b9f5f8aSAndrey V. Elsukov 	uint32_t t;
270776b7288SRandall Stewart #endif
2710b9f5f8aSAndrey V. Elsukov 	uint32_t af;
2720b9f5f8aSAndrey V. Elsukov 	uint8_t proto, ecn;
2730b9f5f8aSAndrey V. Elsukov 	int error;
2740b9f5f8aSAndrey V. Elsukov 
2750a27163fSGleb Smirnoff 	GIF_RLOCK();
2769c0265c6SAndrey V. Elsukov #ifdef MAC
2779c0265c6SAndrey V. Elsukov 	error = mac_ifnet_check_transmit(ifp, m);
2789c0265c6SAndrey V. Elsukov 	if (error) {
2799c0265c6SAndrey V. Elsukov 		m_freem(m);
2809c0265c6SAndrey V. Elsukov 		goto err;
2819c0265c6SAndrey V. Elsukov 	}
2829c0265c6SAndrey V. Elsukov #endif
2830b9f5f8aSAndrey V. Elsukov 	error = ENETDOWN;
2840b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
2859c0265c6SAndrey V. Elsukov 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
2869c0265c6SAndrey V. Elsukov 	    (ifp->if_flags & IFF_UP) == 0 ||
287009d82eeSAndrey V. Elsukov 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
2889c0265c6SAndrey V. Elsukov 	    sc->gif_family == 0 ||
28998a8fdf6SAndrey V. Elsukov 	    (error = if_tunnel_check_nesting(ifp, m, MTAG_GIF,
29098a8fdf6SAndrey V. Elsukov 		V_max_gif_nesting)) != 0) {
2910b9f5f8aSAndrey V. Elsukov 		m_freem(m);
2920b9f5f8aSAndrey V. Elsukov 		goto err;
2930b9f5f8aSAndrey V. Elsukov 	}
2940b9f5f8aSAndrey V. Elsukov 	/* Now pull back the af that we stashed in the csum_data. */
2959c0265c6SAndrey V. Elsukov 	if (ifp->if_bridge)
2969c0265c6SAndrey V. Elsukov 		af = AF_LINK;
2979c0265c6SAndrey V. Elsukov 	else
298cef68c63SRandall Stewart 		af = m->m_pkthdr.csum_data;
2999c0265c6SAndrey V. Elsukov 	m->m_flags &= ~(M_BCAST|M_MCAST);
3009c0265c6SAndrey V. Elsukov 	M_SETFIB(m, sc->gif_fibnum);
301776b7288SRandall Stewart 	BPF_MTAP2(ifp, &af, sizeof(af), m);
3023751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3030b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
304776b7288SRandall Stewart 	/* inner AF-specific encapsulation */
3050b9f5f8aSAndrey V. Elsukov 	ecn = 0;
3060b9f5f8aSAndrey V. Elsukov 	switch (af) {
307776b7288SRandall Stewart #ifdef INET
308776b7288SRandall Stewart 	case AF_INET:
3090b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV4;
3100b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
3110b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
3120b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3130b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3140b9f5f8aSAndrey V. Elsukov 			goto err;
3150b9f5f8aSAndrey V. Elsukov 		}
3160b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
3170b9f5f8aSAndrey V. Elsukov 		ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3180b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos);
319776b7288SRandall Stewart 		break;
320776b7288SRandall Stewart #endif
321776b7288SRandall Stewart #ifdef INET6
322776b7288SRandall Stewart 	case AF_INET6:
3230b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV6;
3240b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
3250b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
3260b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3270b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3280b9f5f8aSAndrey V. Elsukov 			goto err;
3290b9f5f8aSAndrey V. Elsukov 		}
3300b9f5f8aSAndrey V. Elsukov 		t = 0;
3310b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
3320b9f5f8aSAndrey V. Elsukov 		ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3330b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow);
3340b9f5f8aSAndrey V. Elsukov 		ecn = (ntohl(t) >> 20) & 0xff;
3350b9f5f8aSAndrey V. Elsukov 		break;
3360b9f5f8aSAndrey V. Elsukov #endif
3370b9f5f8aSAndrey V. Elsukov 	case AF_LINK:
3380b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_ETHERIP;
3390b9f5f8aSAndrey V. Elsukov 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
3400b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3410b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3420b9f5f8aSAndrey V. Elsukov 			goto err;
3430b9f5f8aSAndrey V. Elsukov 		}
3440b9f5f8aSAndrey V. Elsukov 		eth = mtod(m, struct etherip_header *);
3450b9f5f8aSAndrey V. Elsukov 		eth->eip_resvh = 0;
3460b9f5f8aSAndrey V. Elsukov 		eth->eip_ver = ETHERIP_VERSION;
3470b9f5f8aSAndrey V. Elsukov 		eth->eip_resvl = 0;
3480b9f5f8aSAndrey V. Elsukov 		break;
3490b9f5f8aSAndrey V. Elsukov 	default:
3500b9f5f8aSAndrey V. Elsukov 		error = EAFNOSUPPORT;
3510b9f5f8aSAndrey V. Elsukov 		m_freem(m);
3520b9f5f8aSAndrey V. Elsukov 		goto err;
3530b9f5f8aSAndrey V. Elsukov 	}
3540b9f5f8aSAndrey V. Elsukov 	/* XXX should we check if our outer source is legal? */
3550b9f5f8aSAndrey V. Elsukov 	/* dispatch to output logic based on outer AF */
3560b9f5f8aSAndrey V. Elsukov 	switch (sc->gif_family) {
3570b9f5f8aSAndrey V. Elsukov #ifdef INET
3580b9f5f8aSAndrey V. Elsukov 	case AF_INET:
3590b9f5f8aSAndrey V. Elsukov 		error = in_gif_output(ifp, m, proto, ecn);
3600b9f5f8aSAndrey V. Elsukov 		break;
3610b9f5f8aSAndrey V. Elsukov #endif
3620b9f5f8aSAndrey V. Elsukov #ifdef INET6
3630b9f5f8aSAndrey V. Elsukov 	case AF_INET6:
3640b9f5f8aSAndrey V. Elsukov 		error = in6_gif_output(ifp, m, proto, ecn);
365776b7288SRandall Stewart 		break;
366776b7288SRandall Stewart #endif
367776b7288SRandall Stewart 	default:
368776b7288SRandall Stewart 		m_freem(m);
369776b7288SRandall Stewart 	}
3700b9f5f8aSAndrey V. Elsukov err:
371776b7288SRandall Stewart 	if (error)
3723751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
373b941bc1dSAndrey V. Elsukov 	GIF_RUNLOCK();
3740b9f5f8aSAndrey V. Elsukov 	return (error);
37573ff045cSAndrew Thompson }
3760b9f5f8aSAndrey V. Elsukov 
3770b9f5f8aSAndrey V. Elsukov static void
3780b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused)
3790b9f5f8aSAndrey V. Elsukov {
3800b9f5f8aSAndrey V. Elsukov 
38173ff045cSAndrew Thompson }
38273ff045cSAndrew Thompson 
383c26230adSAndrey V. Elsukov 
384cfa1ca9dSYoshinobu Inoue int
38547e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
38647e8d432SGleb Smirnoff 	struct route *ro)
387cfa1ca9dSYoshinobu Inoue {
388776b7288SRandall Stewart 	uint32_t af;
38910722b85SRobert Watson 
39047e8d432SGleb Smirnoff 	if (dst->sa_family == AF_UNSPEC)
39101399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
39247e8d432SGleb Smirnoff 	else
39301399f34SDavid Malone 		af = dst->sa_family;
39440138788SRandall Stewart 	/*
3950b9f5f8aSAndrey V. Elsukov 	 * Now save the af in the inbound pkt csum data, this is a cheat since
3960b9f5f8aSAndrey V. Elsukov 	 * we are using the inbound csum_data field to carry the af over to
3970b9f5f8aSAndrey V. Elsukov 	 * the gif_transmit() routine, avoiding using yet another mtag.
398776b7288SRandall Stewart 	 */
399cef68c63SRandall Stewart 	m->m_pkthdr.csum_data = af;
4000b9f5f8aSAndrey V. Elsukov 	return (ifp->if_transmit(ifp, m));
401cfa1ca9dSYoshinobu Inoue }
402cfa1ca9dSYoshinobu Inoue 
403cfa1ca9dSYoshinobu Inoue void
4040b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
405cfa1ca9dSYoshinobu Inoue {
40673ff045cSAndrew Thompson 	struct etherip_header *eip;
4070b9f5f8aSAndrey V. Elsukov #ifdef INET
4080b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
4090b9f5f8aSAndrey V. Elsukov #endif
4100b9f5f8aSAndrey V. Elsukov #ifdef INET6
4110b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
4120b9f5f8aSAndrey V. Elsukov 	uint32_t t;
4130b9f5f8aSAndrey V. Elsukov #endif
41456abdd33SAndrew Thompson 	struct ether_header *eh;
41556abdd33SAndrew Thompson 	struct ifnet *oldifp;
4160b9f5f8aSAndrey V. Elsukov 	int isr, n, af;
417cfa1ca9dSYoshinobu Inoue 
418*b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
419*b8a6e03fSGleb Smirnoff 
42021fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
421cfa1ca9dSYoshinobu Inoue 		/* just in case */
422cfa1ca9dSYoshinobu Inoue 		m_freem(m);
423cfa1ca9dSYoshinobu Inoue 		return;
424cfa1ca9dSYoshinobu Inoue 	}
42521fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
4265b7a43f5SAndrey V. Elsukov 	m_clrprotoflags(m);
4270b9f5f8aSAndrey V. Elsukov 	switch (proto) {
4280b9f5f8aSAndrey V. Elsukov #ifdef INET
4290b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV4:
4300b9f5f8aSAndrey V. Elsukov 		af = AF_INET;
4310b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
4320b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
4330b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
4340b9f5f8aSAndrey V. Elsukov 			goto drop;
4350b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
4360b9f5f8aSAndrey V. Elsukov 		if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
4370b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
4380b9f5f8aSAndrey V. Elsukov 			m_freem(m);
4390b9f5f8aSAndrey V. Elsukov 			goto drop;
4400b9f5f8aSAndrey V. Elsukov 		}
4410b9f5f8aSAndrey V. Elsukov 		break;
4420b9f5f8aSAndrey V. Elsukov #endif
4430b9f5f8aSAndrey V. Elsukov #ifdef INET6
4440b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV6:
4450b9f5f8aSAndrey V. Elsukov 		af = AF_INET6;
4460b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
4470b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
4480b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
4490b9f5f8aSAndrey V. Elsukov 			goto drop;
4500b9f5f8aSAndrey V. Elsukov 		t = htonl((uint32_t)ecn << 20);
4510b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
4520b9f5f8aSAndrey V. Elsukov 		if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
4530b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
4540b9f5f8aSAndrey V. Elsukov 			m_freem(m);
4550b9f5f8aSAndrey V. Elsukov 			goto drop;
4560b9f5f8aSAndrey V. Elsukov 		}
4570b9f5f8aSAndrey V. Elsukov 		break;
4580b9f5f8aSAndrey V. Elsukov #endif
4590b9f5f8aSAndrey V. Elsukov 	case IPPROTO_ETHERIP:
4600b9f5f8aSAndrey V. Elsukov 		af = AF_LINK;
4610b9f5f8aSAndrey V. Elsukov 		break;
4620b9f5f8aSAndrey V. Elsukov 	default:
4630b9f5f8aSAndrey V. Elsukov 		m_freem(m);
4640b9f5f8aSAndrey V. Elsukov 		goto drop;
4650b9f5f8aSAndrey V. Elsukov 	}
466cfa1ca9dSYoshinobu Inoue 
46710722b85SRobert Watson #ifdef MAC
46830d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
46910722b85SRobert Watson #endif
47010722b85SRobert Watson 
47116d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
4720b9f5f8aSAndrey V. Elsukov 		uint32_t af1 = af;
473437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
474cfa1ca9dSYoshinobu Inoue 	}
475cfa1ca9dSYoshinobu Inoue 
476e9f947e2SHiroki Sato 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
4773751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
4783751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
479e9f947e2SHiroki Sato 		m_freem(m);
480e9f947e2SHiroki Sato 		return;
481e9f947e2SHiroki Sato 	}
482e9f947e2SHiroki Sato 
48394408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
48421fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
48594408d94SBrooks Davis 		if (m == NULL)
4860b9f5f8aSAndrey V. Elsukov 			goto drop;
48794408d94SBrooks Davis 	}
48894408d94SBrooks Davis 
489cfa1ca9dSYoshinobu Inoue 	/*
490cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
491cfa1ca9dSYoshinobu Inoue 	 * specified address family.
492cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
493cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
494cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
495cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
496cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
49788ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
49888ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
499cfa1ca9dSYoshinobu Inoue 	 */
500cfa1ca9dSYoshinobu Inoue 	switch (af) {
501cfa1ca9dSYoshinobu Inoue #ifdef INET
502cfa1ca9dSYoshinobu Inoue 	case AF_INET:
503cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
504cfa1ca9dSYoshinobu Inoue 		break;
505cfa1ca9dSYoshinobu Inoue #endif
506cfa1ca9dSYoshinobu Inoue #ifdef INET6
507cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
508cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
509cfa1ca9dSYoshinobu Inoue 		break;
510cfa1ca9dSYoshinobu Inoue #endif
51173ff045cSAndrew Thompson 	case AF_LINK:
512b941bc1dSAndrey V. Elsukov 		n = sizeof(struct etherip_header) +
513b941bc1dSAndrey V. Elsukov 		    sizeof(struct ether_header);
5140b9f5f8aSAndrey V. Elsukov 		if (n > m->m_len)
51573ff045cSAndrew Thompson 			m = m_pullup(m, n);
5160b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
5170b9f5f8aSAndrey V. Elsukov 			goto drop;
51873ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
519dbe59260SHiroki Sato 		if (eip->eip_ver != ETHERIP_VERSION) {
520dbe59260SHiroki Sato 			/* discard unknown versions */
521dbe59260SHiroki Sato 			m_freem(m);
5220b9f5f8aSAndrey V. Elsukov 			goto drop;
523dbe59260SHiroki Sato 		}
52473ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
52573ff045cSAndrew Thompson 
52673ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
52773ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
52873ff045cSAndrew Thompson 
52956abdd33SAndrew Thompson 		if (ifp->if_bridge) {
53056abdd33SAndrew Thompson 			oldifp = ifp;
53156abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
53256abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
53356abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
53456abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
53556abdd33SAndrew Thompson 				else
53656abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
5373751dddbSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
53856abdd33SAndrew Thompson 			}
53973ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
54073ff045cSAndrew Thompson 
54156abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
54256abdd33SAndrew Thompson 				/*
54356abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
54456abdd33SAndrew Thompson 				 * members for which the frame is addressed.
54556abdd33SAndrew Thompson 				 */
54656abdd33SAndrew Thompson 				ether_demux(ifp, m);
54756abdd33SAndrew Thompson 				return;
54856abdd33SAndrew Thompson 			}
54956abdd33SAndrew Thompson 		}
55073ff045cSAndrew Thompson 		if (m != NULL)
55173ff045cSAndrew Thompson 			m_freem(m);
55273ff045cSAndrew Thompson 		return;
55373ff045cSAndrew Thompson 
554cfa1ca9dSYoshinobu Inoue 	default:
55594408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
55621fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
55794408d94SBrooks Davis 		else
558cfa1ca9dSYoshinobu Inoue 			m_freem(m);
559cfa1ca9dSYoshinobu Inoue 		return;
560cfa1ca9dSYoshinobu Inoue 	}
561cfa1ca9dSYoshinobu Inoue 
5623751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
5633751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
564a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
5651cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
5660b9f5f8aSAndrey V. Elsukov 	return;
5670b9f5f8aSAndrey V. Elsukov drop:
5680b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
569cfa1ca9dSYoshinobu Inoue }
570cfa1ca9dSYoshinobu Inoue 
571b941bc1dSAndrey V. Elsukov static int
572c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
573cfa1ca9dSYoshinobu Inoue {
574cfa1ca9dSYoshinobu Inoue 	struct ifreq *ifr = (struct ifreq*)data;
5750b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
5760b9f5f8aSAndrey V. Elsukov 	u_int options;
5770b9f5f8aSAndrey V. Elsukov 	int error;
578cfa1ca9dSYoshinobu Inoue 
579cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
580cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
5819426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
582cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
583cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
584cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
5850b9f5f8aSAndrey V. Elsukov 	case SIOCSIFFLAGS:
5860b9f5f8aSAndrey V. Elsukov 		return (0);
587cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
5880b9f5f8aSAndrey V. Elsukov 		if (ifr->ifr_mtu < GIF_MTU_MIN ||
5890b9f5f8aSAndrey V. Elsukov 		    ifr->ifr_mtu > GIF_MTU_MAX)
590cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
5910b9f5f8aSAndrey V. Elsukov 		else
5920b9f5f8aSAndrey V. Elsukov 			ifp->if_mtu = ifr->ifr_mtu;
5930b9f5f8aSAndrey V. Elsukov 		return (0);
5940b9f5f8aSAndrey V. Elsukov 	}
5950b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
5960b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
5970b9f5f8aSAndrey V. Elsukov 	if (sc == NULL) {
5980b9f5f8aSAndrey V. Elsukov 		error = ENXIO;
5990b9f5f8aSAndrey V. Elsukov 		goto bad;
6000b9f5f8aSAndrey V. Elsukov 	}
6010b9f5f8aSAndrey V. Elsukov 	error = 0;
6020b9f5f8aSAndrey V. Elsukov 	switch (cmd) {
603686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
604b941bc1dSAndrey V. Elsukov 		if (sc->gif_family == 0)
605686cdd19SJun-ichiro itojun Hagino 			break;
606b941bc1dSAndrey V. Elsukov 		gif_delete_tunnel(sc);
6070b9f5f8aSAndrey V. Elsukov 		break;
608cfa1ca9dSYoshinobu Inoue #ifdef INET
609b941bc1dSAndrey V. Elsukov 	case SIOCSIFPHYADDR:
61033841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR:
6110b9f5f8aSAndrey V. Elsukov 	case SIOCGIFPDSTADDR:
612b941bc1dSAndrey V. Elsukov 		error = in_gif_ioctl(sc, cmd, data);
6130b9f5f8aSAndrey V. Elsukov 		break;
6140b9f5f8aSAndrey V. Elsukov #endif
615cfa1ca9dSYoshinobu Inoue #ifdef INET6
616b941bc1dSAndrey V. Elsukov 	case SIOCSIFPHYADDR_IN6:
61733841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR_IN6:
618cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
619b941bc1dSAndrey V. Elsukov 		error = in6_gif_ioctl(sc, cmd, data);
6200b9f5f8aSAndrey V. Elsukov 		break;
6210b9f5f8aSAndrey V. Elsukov #endif
622eccfe69aSAndrey V. Elsukov 	case SIOCGTUNFIB:
623eccfe69aSAndrey V. Elsukov 		ifr->ifr_fib = sc->gif_fibnum;
624eccfe69aSAndrey V. Elsukov 		break;
625eccfe69aSAndrey V. Elsukov 	case SIOCSTUNFIB:
626eccfe69aSAndrey V. Elsukov 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
627eccfe69aSAndrey V. Elsukov 			break;
628eccfe69aSAndrey V. Elsukov 		if (ifr->ifr_fib >= rt_numfibs)
629eccfe69aSAndrey V. Elsukov 			error = EINVAL;
630eccfe69aSAndrey V. Elsukov 		else
631eccfe69aSAndrey V. Elsukov 			sc->gif_fibnum = ifr->ifr_fib;
632eccfe69aSAndrey V. Elsukov 		break;
633dbe59260SHiroki Sato 	case GIFGOPTS:
634dbe59260SHiroki Sato 		options = sc->gif_options;
635541d96aaSBrooks Davis 		error = copyout(&options, ifr_data_get_ptr(ifr),
636541d96aaSBrooks Davis 		    sizeof(options));
637dbe59260SHiroki Sato 		break;
638dbe59260SHiroki Sato 	case GIFSOPTS:
639dbe59260SHiroki Sato 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
640dbe59260SHiroki Sato 			break;
641541d96aaSBrooks Davis 		error = copyin(ifr_data_get_ptr(ifr), &options,
642541d96aaSBrooks Davis 		    sizeof(options));
6434cd5f57dSHiroki Sato 		if (error)
6444cd5f57dSHiroki Sato 			break;
645b941bc1dSAndrey V. Elsukov 		if (options & ~GIF_OPTMASK) {
646dbe59260SHiroki Sato 			error = EINVAL;
647b941bc1dSAndrey V. Elsukov 			break;
648b941bc1dSAndrey V. Elsukov 		}
649b941bc1dSAndrey V. Elsukov 		if (sc->gif_options != options) {
650b941bc1dSAndrey V. Elsukov 			switch (sc->gif_family) {
651b941bc1dSAndrey V. Elsukov #ifdef INET
652b941bc1dSAndrey V. Elsukov 			case AF_INET:
653b941bc1dSAndrey V. Elsukov 				error = in_gif_setopts(sc, options);
654b941bc1dSAndrey V. Elsukov 				break;
655b941bc1dSAndrey V. Elsukov #endif
656b941bc1dSAndrey V. Elsukov #ifdef INET6
657b941bc1dSAndrey V. Elsukov 			case AF_INET6:
658b941bc1dSAndrey V. Elsukov 				error = in6_gif_setopts(sc, options);
659b941bc1dSAndrey V. Elsukov 				break;
660b941bc1dSAndrey V. Elsukov #endif
661b941bc1dSAndrey V. Elsukov 			default:
662b941bc1dSAndrey V. Elsukov 				/* No need to invoke AF-handler */
6634cd5f57dSHiroki Sato 				sc->gif_options = options;
664b941bc1dSAndrey V. Elsukov 			}
665b941bc1dSAndrey V. Elsukov 		}
666dbe59260SHiroki Sato 		break;
667cfa1ca9dSYoshinobu Inoue 	default:
668cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
669cfa1ca9dSYoshinobu Inoue 		break;
670cfa1ca9dSYoshinobu Inoue 	}
671b941bc1dSAndrey V. Elsukov 	if (error == 0 && sc->gif_family != 0) {
672b941bc1dSAndrey V. Elsukov 		if (
673b941bc1dSAndrey V. Elsukov #ifdef INET
674b941bc1dSAndrey V. Elsukov 		    cmd == SIOCSIFPHYADDR ||
675b941bc1dSAndrey V. Elsukov #endif
676b941bc1dSAndrey V. Elsukov #ifdef INET6
677b941bc1dSAndrey V. Elsukov 		    cmd == SIOCSIFPHYADDR_IN6 ||
678b941bc1dSAndrey V. Elsukov #endif
679b941bc1dSAndrey V. Elsukov 		    0) {
680b941bc1dSAndrey V. Elsukov 			if_link_state_change(ifp, LINK_STATE_UP);
681b941bc1dSAndrey V. Elsukov 		}
682b941bc1dSAndrey V. Elsukov 	}
683cfa1ca9dSYoshinobu Inoue bad:
6840b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
6850b9f5f8aSAndrey V. Elsukov 	return (error);
686cfa1ca9dSYoshinobu Inoue }
68753dab5feSBrooks Davis 
6880b9f5f8aSAndrey V. Elsukov static void
689b941bc1dSAndrey V. Elsukov gif_delete_tunnel(struct gif_softc *sc)
6900b9f5f8aSAndrey V. Elsukov {
6910b9f5f8aSAndrey V. Elsukov 
6920b9f5f8aSAndrey V. Elsukov 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
693b941bc1dSAndrey V. Elsukov 	if (sc->gif_family != 0) {
694009d82eeSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, srchash);
695b941bc1dSAndrey V. Elsukov 		CK_LIST_REMOVE(sc, chain);
696b941bc1dSAndrey V. Elsukov 		/* Wait until it become safe to free gif_hdr */
697b941bc1dSAndrey V. Elsukov 		GIF_WAIT();
6980b9f5f8aSAndrey V. Elsukov 		free(sc->gif_hdr, M_GIF);
699f1aaad0cSHiroki Sato 	}
7000b9f5f8aSAndrey V. Elsukov 	sc->gif_family = 0;
701b941bc1dSAndrey V. Elsukov 	GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
702b941bc1dSAndrey V. Elsukov 	if_link_state_change(GIF2IFP(sc), LINK_STATE_DOWN);
70353dab5feSBrooks Davis }
704b941bc1dSAndrey V. Elsukov 
705