xref: /freebsd/sys/net/if_gif.c (revision 138d56556ca1e2abd4f4b7ed860e8cbb11597b83)
1c398230bSWarner Losh /*-
2cfa1ca9dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3cfa1ca9dSYoshinobu Inoue  * All rights reserved.
4cfa1ca9dSYoshinobu Inoue  *
5cfa1ca9dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
6cfa1ca9dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
7cfa1ca9dSYoshinobu Inoue  * are met:
8cfa1ca9dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
9cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
10cfa1ca9dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
11cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
12cfa1ca9dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
13cfa1ca9dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
14cfa1ca9dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
15cfa1ca9dSYoshinobu Inoue  *    without specific prior written permission.
16cfa1ca9dSYoshinobu Inoue  *
17cfa1ca9dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18cfa1ca9dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19cfa1ca9dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20cfa1ca9dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21cfa1ca9dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22cfa1ca9dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23cfa1ca9dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24cfa1ca9dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25cfa1ca9dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26cfa1ca9dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27cfa1ca9dSYoshinobu Inoue  * SUCH DAMAGE.
280b9f5f8aSAndrey V. Elsukov  *
290b9f5f8aSAndrey V. Elsukov  *	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $
30cfa1ca9dSYoshinobu Inoue  */
31cfa1ca9dSYoshinobu Inoue 
320b9f5f8aSAndrey V. Elsukov #include <sys/cdefs.h>
330b9f5f8aSAndrey V. Elsukov __FBSDID("$FreeBSD$");
340b9f5f8aSAndrey V. Elsukov 
35cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
36cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
37cfa1ca9dSYoshinobu Inoue 
38cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
40e3416ab0SBjoern A. Zeeb #include <sys/jail.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
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>
56686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5753dab5feSBrooks Davis #include <sys/conf.h>
58cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
59cfa1ca9dSYoshinobu Inoue 
60cfa1ca9dSYoshinobu Inoue #include <net/if.h>
6176039bc8SGleb Smirnoff #include <net/if_var.h>
62f889d2efSBrooks Davis #include <net/if_clone.h>
63cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
64cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
65cfa1ca9dSYoshinobu Inoue #include <net/route.h>
66cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
67530c0060SRobert Watson #include <net/vnet.h>
68cfa1ca9dSYoshinobu Inoue 
69cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
70cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
71cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
720b9f5f8aSAndrey V. Elsukov #include <netinet/ip_ecn.h>
7333841545SHajimu UMEMOTO #ifdef	INET
7433841545SHajimu UMEMOTO #include <netinet/in_var.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
7653dab5feSBrooks Davis #include <netinet/ip_var.h>
77cfa1ca9dSYoshinobu Inoue #endif	/* INET */
78cfa1ca9dSYoshinobu Inoue 
79cfa1ca9dSYoshinobu Inoue #ifdef INET6
80cfa1ca9dSYoshinobu Inoue #ifndef INET
81cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
82cfa1ca9dSYoshinobu Inoue #endif
83cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
84cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
850b9f5f8aSAndrey V. Elsukov #include <netinet6/ip6_ecn.h>
86cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
87a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
88cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
89686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
90cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
91cfa1ca9dSYoshinobu Inoue 
92686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
9373ff045cSAndrew Thompson #include <net/ethernet.h>
9473ff045cSAndrew Thompson #include <net/if_bridgevar.h>
95cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
96cfa1ca9dSYoshinobu Inoue 
97aed55708SRobert Watson #include <security/mac/mac_framework.h>
98aed55708SRobert Watson 
9942a58907SGleb Smirnoff static const char gifname[] = "gif";
100686cdd19SJun-ichiro itojun Hagino 
10117d5cb2dSRobert Watson /*
102a7f5886eSHiroki Sato  * gif_mtx protects a per-vnet gif_softc_list.
10317d5cb2dSRobert Watson  */
104a7f5886eSHiroki Sato static VNET_DEFINE(struct mtx, gif_mtx);
105a7f5886eSHiroki Sato #define	V_gif_mtx		VNET(gif_mtx)
10653dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
1073e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
1081e77c105SRobert Watson #define	V_gif_softc_list	VNET(gif_softc_list)
1090b9f5f8aSAndrey V. Elsukov static struct sx gif_ioctl_sx;
1100b9f5f8aSAndrey V. Elsukov SX_SYSINIT(gif_ioctl_sx, &gif_ioctl_sx, "gif_ioctl");
111eddfbb76SRobert Watson 
112a7f5886eSHiroki Sato #define	GIF_LIST_LOCK_INIT(x)		mtx_init(&V_gif_mtx, "gif_mtx", \
113a7f5886eSHiroki Sato 					    NULL, MTX_DEF)
114a7f5886eSHiroki Sato #define	GIF_LIST_LOCK_DESTROY(x)	mtx_destroy(&V_gif_mtx)
115a7f5886eSHiroki Sato #define	GIF_LIST_LOCK(x)		mtx_lock(&V_gif_mtx)
116a7f5886eSHiroki Sato #define	GIF_LIST_UNLOCK(x)		mtx_unlock(&V_gif_mtx)
117a7f5886eSHiroki Sato 
11894408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
11994408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
12094408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
12194408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
12294408d94SBrooks Davis 
1230b9f5f8aSAndrey V. Elsukov static int	gif_set_tunnel(struct ifnet *, struct sockaddr *,
1240b9f5f8aSAndrey V. Elsukov     struct sockaddr *);
1250b9f5f8aSAndrey V. Elsukov static void	gif_delete_tunnel(struct ifnet *);
1260b9f5f8aSAndrey V. Elsukov static int	gif_ioctl(struct ifnet *, u_long, caddr_t);
1270b9f5f8aSAndrey V. Elsukov static int	gif_transmit(struct ifnet *, struct mbuf *);
1280b9f5f8aSAndrey V. Elsukov static void	gif_qflush(struct ifnet *);
1296b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
130bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
131a7f5886eSHiroki Sato static VNET_DEFINE(struct if_clone *, gif_cloner);
132a7f5886eSHiroki Sato #define	V_gif_cloner	VNET(gif_cloner)
13353dab5feSBrooks Davis 
134929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
135cfa1ca9dSYoshinobu Inoue 
136872f786aSBrooks Davis SYSCTL_DECL(_net_link);
1376472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
138872f786aSBrooks Davis     "Generic Tunnel Interface");
139686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
140686cdd19SJun-ichiro itojun Hagino /*
141872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
142686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
143686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
144686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
145686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
146686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
147686cdd19SJun-ichiro itojun Hagino  */
148686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
149686cdd19SJun-ichiro itojun Hagino #endif
1503e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
151d0728d71SRobert Watson #define	V_max_gif_nesting	VNET(max_gif_nesting)
152eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
153eddfbb76SRobert Watson     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
1548b615593SMarko Zec 
155872f786aSBrooks Davis /*
156872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
157872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
158872f786aSBrooks Davis  * we allow control over this check here.
159872f786aSBrooks Davis  */
160d0728d71SRobert Watson #ifdef XBONEHACK
1613e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1;
162d0728d71SRobert Watson #else
1633e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0;
164d0728d71SRobert Watson #endif
165d0728d71SRobert Watson #define	V_parallel_tunnels	VNET(parallel_tunnels)
166eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
167eddfbb76SRobert Watson     &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
168cfa1ca9dSYoshinobu Inoue 
16956abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
17056abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
17156abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
17256abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
17356abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
17456abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
17556abdd33SAndrew Thompson #endif
17656abdd33SAndrew Thompson 
177bb2bfb4fSBrooks Davis static int
178c72a5d5dSAndrey V. Elsukov gif_clone_create(struct if_clone *ifc, int unit, caddr_t params)
179cfa1ca9dSYoshinobu Inoue {
18033841545SHajimu UMEMOTO 	struct gif_softc *sc;
181cfa1ca9dSYoshinobu Inoue 
182e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1838b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
184fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
18525af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
186fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
18742a58907SGleb Smirnoff 	if_initname(GIF2IFP(sc), gifname, unit);
188686cdd19SJun-ichiro itojun Hagino 
189fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
190fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
191fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
19233841545SHajimu UMEMOTO #if 0
19333841545SHajimu UMEMOTO 	/* turn off ingress filter */
194fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
19533841545SHajimu UMEMOTO #endif
196fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
1970b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_transmit  = gif_transmit;
1980b9f5f8aSAndrey V. Elsukov 	GIF2IFP(sc)->if_qflush  = gif_qflush;
199fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
200fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
20101399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
20294408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
203fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
20425af0bb5SGleb Smirnoff 
205a7f5886eSHiroki Sato 	GIF_LIST_LOCK();
206603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
207a7f5886eSHiroki Sato 	GIF_LIST_UNLOCK();
20825af0bb5SGleb Smirnoff 	return (0);
209cfa1ca9dSYoshinobu Inoue }
210cfa1ca9dSYoshinobu Inoue 
21117d5cb2dSRobert Watson static void
212c72a5d5dSAndrey V. Elsukov gif_clone_destroy(struct ifnet *ifp)
21353dab5feSBrooks Davis {
2140b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
215febd0759SAndrew Thompson 
2160b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
2170b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
2180b9f5f8aSAndrey V. Elsukov 	gif_delete_tunnel(ifp);
219a7f5886eSHiroki Sato 	GIF_LIST_LOCK();
220febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
221a7f5886eSHiroki Sato 	GIF_LIST_UNLOCK();
22294408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
22394408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
22453dab5feSBrooks Davis 	bpfdetach(ifp);
22553dab5feSBrooks Davis 	if_detach(ifp);
2260b9f5f8aSAndrey V. Elsukov 	ifp->if_softc = NULL;
2270b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
2280b9f5f8aSAndrey V. Elsukov 
229fc74a9f9SBrooks Davis 	if_free(ifp);
23025af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
23153dab5feSBrooks Davis 	free(sc, M_GIF);
23253dab5feSBrooks Davis }
23353dab5feSBrooks Davis 
234d0728d71SRobert Watson static void
235d0728d71SRobert Watson vnet_gif_init(const void *unused __unused)
2361ed81b73SMarko Zec {
2371ed81b73SMarko Zec 
2381ed81b73SMarko Zec 	LIST_INIT(&V_gif_softc_list);
239a7f5886eSHiroki Sato 	GIF_LIST_LOCK_INIT();
240a7f5886eSHiroki Sato 	V_gif_cloner = if_clone_simple(gifname, gif_clone_create,
241a7f5886eSHiroki Sato 	    gif_clone_destroy, 0);
2421ed81b73SMarko Zec }
243a7f5886eSHiroki Sato VNET_SYSINIT(vnet_gif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
244a7f5886eSHiroki Sato     vnet_gif_init, NULL);
245a7f5886eSHiroki Sato 
246a7f5886eSHiroki Sato static void
247a7f5886eSHiroki Sato vnet_gif_uninit(const void *unused __unused)
248a7f5886eSHiroki Sato {
249a7f5886eSHiroki Sato 
250a7f5886eSHiroki Sato 	if_clone_detach(V_gif_cloner);
251a7f5886eSHiroki Sato 	GIF_LIST_LOCK_DESTROY();
252a7f5886eSHiroki Sato }
253a7f5886eSHiroki Sato VNET_SYSUNINIT(vnet_gif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
254a7f5886eSHiroki Sato     vnet_gif_uninit, NULL);
2551ed81b73SMarko Zec 
2561ed81b73SMarko Zec static int
257c72a5d5dSAndrey V. Elsukov gifmodevent(module_t mod, int type, void *data)
25853dab5feSBrooks Davis {
25953dab5feSBrooks Davis 
26053dab5feSBrooks Davis 	switch (type) {
26153dab5feSBrooks Davis 	case MOD_LOAD:
26253dab5feSBrooks Davis 	case MOD_UNLOAD:
26353dab5feSBrooks Davis 		break;
2643e019deaSPoul-Henning Kamp 	default:
265a7f5886eSHiroki Sato 		return (EOPNOTSUPP);
26653dab5feSBrooks Davis 	}
267a7f5886eSHiroki Sato 	return (0);
26853dab5feSBrooks Davis }
26953dab5feSBrooks Davis 
27053dab5feSBrooks Davis static moduledata_t gif_mod = {
27153dab5feSBrooks Davis 	"if_gif",
27253dab5feSBrooks Davis 	gifmodevent,
2739823d527SKevin Lo 	0
27453dab5feSBrooks Davis };
27553dab5feSBrooks Davis 
27653dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
27720af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
278cfa1ca9dSYoshinobu Inoue 
2799426aedfSHajimu UMEMOTO int
280c72a5d5dSAndrey V. Elsukov gif_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
281686cdd19SJun-ichiro itojun Hagino {
2820b9f5f8aSAndrey V. Elsukov 	GIF_RLOCK_TRACKER;
283686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
2840b9f5f8aSAndrey V. Elsukov 	int ret;
2850b9f5f8aSAndrey V. Elsukov 	uint8_t ver;
286686cdd19SJun-ichiro itojun Hagino 
287686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
2880b9f5f8aSAndrey V. Elsukov 	if (sc == NULL || (GIF2IFP(sc)->if_flags & IFF_UP) == 0)
2890b9f5f8aSAndrey V. Elsukov 		return (0);
290686cdd19SJun-ichiro itojun Hagino 
2910b9f5f8aSAndrey V. Elsukov 	ret = 0;
2920b9f5f8aSAndrey V. Elsukov 	GIF_RLOCK(sc);
293686cdd19SJun-ichiro itojun Hagino 
294686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
2950b9f5f8aSAndrey V. Elsukov 	if (sc->gif_family == 0)
2960b9f5f8aSAndrey V. Elsukov 		goto done;
297686cdd19SJun-ichiro itojun Hagino 
298686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
299686cdd19SJun-ichiro itojun Hagino #ifdef INET
300686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
301686cdd19SJun-ichiro itojun Hagino #endif
302686cdd19SJun-ichiro itojun Hagino #ifdef INET6
303686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
304686cdd19SJun-ichiro itojun Hagino #endif
30573ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
30673ff045cSAndrew Thompson 		break;
307686cdd19SJun-ichiro itojun Hagino 	default:
3080b9f5f8aSAndrey V. Elsukov 		goto done;
309686cdd19SJun-ichiro itojun Hagino 	}
310686cdd19SJun-ichiro itojun Hagino 
3113bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3120b9f5f8aSAndrey V. Elsukov 	if (m->m_pkthdr.len < sizeof(struct ip))
3130b9f5f8aSAndrey V. Elsukov 		goto done;
3143bb61ca6SHajimu UMEMOTO 
3150b9f5f8aSAndrey V. Elsukov 	m_copydata(m, 0, 1, &ver);
3160b9f5f8aSAndrey V. Elsukov 	switch (ver >> 4) {
317686cdd19SJun-ichiro itojun Hagino #ifdef INET
318686cdd19SJun-ichiro itojun Hagino 	case 4:
3190b9f5f8aSAndrey V. Elsukov 		if (sc->gif_family != AF_INET)
3200b9f5f8aSAndrey V. Elsukov 			goto done;
3210b9f5f8aSAndrey V. Elsukov 		ret = in_gif_encapcheck(m, off, proto, arg);
3220b9f5f8aSAndrey V. Elsukov 		break;
323686cdd19SJun-ichiro itojun Hagino #endif
324686cdd19SJun-ichiro itojun Hagino #ifdef INET6
325686cdd19SJun-ichiro itojun Hagino 	case 6:
3269426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3270b9f5f8aSAndrey V. Elsukov 			goto done;
3280b9f5f8aSAndrey V. Elsukov 		if (sc->gif_family != AF_INET6)
3290b9f5f8aSAndrey V. Elsukov 			goto done;
3300b9f5f8aSAndrey V. Elsukov 		ret = in6_gif_encapcheck(m, off, proto, arg);
3310b9f5f8aSAndrey V. Elsukov 		break;
332686cdd19SJun-ichiro itojun Hagino #endif
333686cdd19SJun-ichiro itojun Hagino 	}
3340b9f5f8aSAndrey V. Elsukov done:
3350b9f5f8aSAndrey V. Elsukov 	GIF_RUNLOCK(sc);
3360b9f5f8aSAndrey V. Elsukov 	return (ret);
337686cdd19SJun-ichiro itojun Hagino }
338686cdd19SJun-ichiro itojun Hagino 
3390b9f5f8aSAndrey V. Elsukov static int
3400b9f5f8aSAndrey V. Elsukov gif_transmit(struct ifnet *ifp, struct mbuf *m)
34173ff045cSAndrew Thompson {
34273ff045cSAndrew Thompson 	struct gif_softc *sc;
3430b9f5f8aSAndrey V. Elsukov 	struct etherip_header *eth;
344776b7288SRandall Stewart #ifdef INET
3450b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
346776b7288SRandall Stewart #endif
347776b7288SRandall Stewart #ifdef INET6
3480b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
3490b9f5f8aSAndrey V. Elsukov 	uint32_t t;
350776b7288SRandall Stewart #endif
3510b9f5f8aSAndrey V. Elsukov 	uint32_t af;
3520b9f5f8aSAndrey V. Elsukov 	uint8_t proto, ecn;
3530b9f5f8aSAndrey V. Elsukov 	int error;
3540b9f5f8aSAndrey V. Elsukov 
3550b9f5f8aSAndrey V. Elsukov 	error = ENETDOWN;
3560b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
3570b9f5f8aSAndrey V. Elsukov 	if (sc->gif_family == 0) {
3580b9f5f8aSAndrey V. Elsukov 		m_freem(m);
3590b9f5f8aSAndrey V. Elsukov 		goto err;
3600b9f5f8aSAndrey V. Elsukov 	}
3610b9f5f8aSAndrey V. Elsukov 	/* Now pull back the af that we stashed in the csum_data. */
362cef68c63SRandall Stewart 	af = m->m_pkthdr.csum_data;
363776b7288SRandall Stewart 	BPF_MTAP2(ifp, &af, sizeof(af), m);
3643751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3650b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
366776b7288SRandall Stewart 	M_SETFIB(m, sc->gif_fibnum);
367776b7288SRandall Stewart 	/* inner AF-specific encapsulation */
3680b9f5f8aSAndrey V. Elsukov 	ecn = 0;
3690b9f5f8aSAndrey V. Elsukov 	switch (af) {
370776b7288SRandall Stewart #ifdef INET
371776b7288SRandall Stewart 	case AF_INET:
3720b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV4;
3730b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
3740b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
3750b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3760b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3770b9f5f8aSAndrey V. Elsukov 			goto err;
3780b9f5f8aSAndrey V. Elsukov 		}
3790b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
3800b9f5f8aSAndrey V. Elsukov 		ip_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3810b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos);
382776b7288SRandall Stewart 		break;
383776b7288SRandall Stewart #endif
384776b7288SRandall Stewart #ifdef INET6
385776b7288SRandall Stewart 	case AF_INET6:
3860b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_IPV6;
3870b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
3880b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
3890b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
3900b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
3910b9f5f8aSAndrey V. Elsukov 			goto err;
3920b9f5f8aSAndrey V. Elsukov 		}
3930b9f5f8aSAndrey V. Elsukov 		t = 0;
3940b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
3950b9f5f8aSAndrey V. Elsukov 		ip6_ecn_ingress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
3960b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow);
3970b9f5f8aSAndrey V. Elsukov 		ecn = (ntohl(t) >> 20) & 0xff;
3980b9f5f8aSAndrey V. Elsukov 		break;
3990b9f5f8aSAndrey V. Elsukov #endif
4000b9f5f8aSAndrey V. Elsukov 	case AF_LINK:
4010b9f5f8aSAndrey V. Elsukov 		proto = IPPROTO_ETHERIP;
4020b9f5f8aSAndrey V. Elsukov 		M_PREPEND(m, sizeof(struct etherip_header), M_NOWAIT);
4030b9f5f8aSAndrey V. Elsukov 		if (m != NULL && m->m_len < sizeof(struct etherip_header))
4040b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct etherip_header));
4050b9f5f8aSAndrey V. Elsukov 		if (m == NULL) {
4060b9f5f8aSAndrey V. Elsukov 			error = ENOBUFS;
4070b9f5f8aSAndrey V. Elsukov 			goto err;
4080b9f5f8aSAndrey V. Elsukov 		}
4090b9f5f8aSAndrey V. Elsukov 		eth = mtod(m, struct etherip_header *);
4100b9f5f8aSAndrey V. Elsukov 		eth->eip_resvh = 0;
4110b9f5f8aSAndrey V. Elsukov 		if ((sc->gif_options & GIF_SEND_REVETHIP) != 0) {
4120b9f5f8aSAndrey V. Elsukov 			eth->eip_ver = 0;
4130b9f5f8aSAndrey V. Elsukov 			eth->eip_resvl = ETHERIP_VERSION;
4140b9f5f8aSAndrey V. Elsukov 		} else {
4150b9f5f8aSAndrey V. Elsukov 			eth->eip_ver = ETHERIP_VERSION;
4160b9f5f8aSAndrey V. Elsukov 			eth->eip_resvl = 0;
4170b9f5f8aSAndrey V. Elsukov 		}
4180b9f5f8aSAndrey V. Elsukov 		break;
4190b9f5f8aSAndrey V. Elsukov 	default:
4200b9f5f8aSAndrey V. Elsukov 		error = EAFNOSUPPORT;
4210b9f5f8aSAndrey V. Elsukov 		m_freem(m);
4220b9f5f8aSAndrey V. Elsukov 		goto err;
4230b9f5f8aSAndrey V. Elsukov 	}
4240b9f5f8aSAndrey V. Elsukov 	/* XXX should we check if our outer source is legal? */
4250b9f5f8aSAndrey V. Elsukov 	/* dispatch to output logic based on outer AF */
4260b9f5f8aSAndrey V. Elsukov 	switch (sc->gif_family) {
4270b9f5f8aSAndrey V. Elsukov #ifdef INET
4280b9f5f8aSAndrey V. Elsukov 	case AF_INET:
4290b9f5f8aSAndrey V. Elsukov 		error = in_gif_output(ifp, m, proto, ecn);
4300b9f5f8aSAndrey V. Elsukov 		break;
4310b9f5f8aSAndrey V. Elsukov #endif
4320b9f5f8aSAndrey V. Elsukov #ifdef INET6
4330b9f5f8aSAndrey V. Elsukov 	case AF_INET6:
4340b9f5f8aSAndrey V. Elsukov 		error = in6_gif_output(ifp, m, proto, ecn);
435776b7288SRandall Stewart 		break;
436776b7288SRandall Stewart #endif
437776b7288SRandall Stewart 	default:
438776b7288SRandall Stewart 		m_freem(m);
439776b7288SRandall Stewart 	}
4400b9f5f8aSAndrey V. Elsukov err:
441776b7288SRandall Stewart 	if (error)
4423751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4430b9f5f8aSAndrey V. Elsukov 	return (error);
44473ff045cSAndrew Thompson }
4450b9f5f8aSAndrey V. Elsukov 
4460b9f5f8aSAndrey V. Elsukov static void
4470b9f5f8aSAndrey V. Elsukov gif_qflush(struct ifnet *ifp __unused)
4480b9f5f8aSAndrey V. Elsukov {
4490b9f5f8aSAndrey V. Elsukov 
45073ff045cSAndrew Thompson }
45173ff045cSAndrew Thompson 
452cfa1ca9dSYoshinobu Inoue int
45347e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
45447e8d432SGleb Smirnoff 	struct route *ro)
455cfa1ca9dSYoshinobu Inoue {
4568c7e1947SRuslan Ermilov 	struct m_tag *mtag;
457776b7288SRandall Stewart 	uint32_t af;
4580b9f5f8aSAndrey V. Elsukov 	int gif_called;
4590b9f5f8aSAndrey V. Elsukov 	int error = 0;
46010722b85SRobert Watson #ifdef MAC
46130d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
4620b9f5f8aSAndrey V. Elsukov 	if (error)
4630b9f5f8aSAndrey V. Elsukov 		goto err;
46410722b85SRobert Watson #endif
4650b9f5f8aSAndrey V. Elsukov 	if ((ifp->if_flags & IFF_MONITOR) != 0 ||
4660b9f5f8aSAndrey V. Elsukov 	    (ifp->if_flags & IFF_UP) == 0) {
467e9f947e2SHiroki Sato 		error = ENETDOWN;
4680b9f5f8aSAndrey V. Elsukov 		goto err;
469e9f947e2SHiroki Sato 	}
47010722b85SRobert Watson 
471cfa1ca9dSYoshinobu Inoue 	/*
472cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
4738c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
4748c7e1947SRuslan Ermilov 	 *
4758c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
476cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
477cfa1ca9dSYoshinobu Inoue 	 */
4788c7e1947SRuslan Ermilov 	gif_called = 1;
4798c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
4808c7e1947SRuslan Ermilov 	while (mtag != NULL) {
4818c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4828c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
4838c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
4848c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
4858c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
4860b9f5f8aSAndrey V. Elsukov 			goto err;
4878c7e1947SRuslan Ermilov 		}
4888c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
4898c7e1947SRuslan Ermilov 		gif_called++;
4908c7e1947SRuslan Ermilov 	}
491603724d3SBjoern A. Zeeb 	if (gif_called > V_max_gif_nesting) {
492cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
493cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
494523ebc4eSRobert Watson 		    gif_called);
495cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
4960b9f5f8aSAndrey V. Elsukov 		goto err;
497cfa1ca9dSYoshinobu Inoue 	}
4988c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4998c7e1947SRuslan Ermilov 	    M_NOWAIT);
5008c7e1947SRuslan Ermilov 	if (mtag == NULL) {
5018c7e1947SRuslan Ermilov 		error = ENOMEM;
5020b9f5f8aSAndrey V. Elsukov 		goto err;
5038c7e1947SRuslan Ermilov 	}
5048c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
5058c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
506686cdd19SJun-ichiro itojun Hagino 
507cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
50847e8d432SGleb Smirnoff 	if (dst->sa_family == AF_UNSPEC)
50901399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
51047e8d432SGleb Smirnoff 	else
51101399f34SDavid Malone 		af = dst->sa_family;
5120b9f5f8aSAndrey V. Elsukov 	if (ifp->if_bridge)
5130b9f5f8aSAndrey V. Elsukov 		af = AF_LINK;
51440138788SRandall Stewart 	/*
5150b9f5f8aSAndrey V. Elsukov 	 * Now save the af in the inbound pkt csum data, this is a cheat since
5160b9f5f8aSAndrey V. Elsukov 	 * we are using the inbound csum_data field to carry the af over to
5170b9f5f8aSAndrey V. Elsukov 	 * the gif_transmit() routine, avoiding using yet another mtag.
518776b7288SRandall Stewart 	 */
519cef68c63SRandall Stewart 	m->m_pkthdr.csum_data = af;
5200b9f5f8aSAndrey V. Elsukov 	return (ifp->if_transmit(ifp, m));
5210b9f5f8aSAndrey V. Elsukov err:
5223751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
5230b9f5f8aSAndrey V. Elsukov 	m_freem(m);
52425af0bb5SGleb Smirnoff 	return (error);
525cfa1ca9dSYoshinobu Inoue }
526cfa1ca9dSYoshinobu Inoue 
527cfa1ca9dSYoshinobu Inoue void
5280b9f5f8aSAndrey V. Elsukov gif_input(struct mbuf *m, struct ifnet *ifp, int proto, uint8_t ecn)
529cfa1ca9dSYoshinobu Inoue {
53073ff045cSAndrew Thompson 	struct etherip_header *eip;
5310b9f5f8aSAndrey V. Elsukov #ifdef INET
5320b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
5330b9f5f8aSAndrey V. Elsukov #endif
5340b9f5f8aSAndrey V. Elsukov #ifdef INET6
5350b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
5360b9f5f8aSAndrey V. Elsukov 	uint32_t t;
5370b9f5f8aSAndrey V. Elsukov #endif
5380b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
53956abdd33SAndrew Thompson 	struct ether_header *eh;
54056abdd33SAndrew Thompson 	struct ifnet *oldifp;
5410b9f5f8aSAndrey V. Elsukov 	uint32_t gif_options;
5420b9f5f8aSAndrey V. Elsukov 	int isr, n, af;
543cfa1ca9dSYoshinobu Inoue 
54421fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
545cfa1ca9dSYoshinobu Inoue 		/* just in case */
546cfa1ca9dSYoshinobu Inoue 		m_freem(m);
547cfa1ca9dSYoshinobu Inoue 		return;
548cfa1ca9dSYoshinobu Inoue 	}
5494382b068SChristian Brueffer 	sc = ifp->if_softc;
5500b9f5f8aSAndrey V. Elsukov 	gif_options = sc->gif_options;
55121fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
5525b7a43f5SAndrey V. Elsukov 	m_clrprotoflags(m);
5530b9f5f8aSAndrey V. Elsukov 	switch (proto) {
5540b9f5f8aSAndrey V. Elsukov #ifdef INET
5550b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV4:
5560b9f5f8aSAndrey V. Elsukov 		af = AF_INET;
5570b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip))
5580b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip));
5590b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
5600b9f5f8aSAndrey V. Elsukov 			goto drop;
5610b9f5f8aSAndrey V. Elsukov 		ip = mtod(m, struct ip *);
5620b9f5f8aSAndrey V. Elsukov 		if (ip_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
5630b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &ecn, &ip->ip_tos) == 0) {
5640b9f5f8aSAndrey V. Elsukov 			m_freem(m);
5650b9f5f8aSAndrey V. Elsukov 			goto drop;
5660b9f5f8aSAndrey V. Elsukov 		}
5670b9f5f8aSAndrey V. Elsukov 		break;
5680b9f5f8aSAndrey V. Elsukov #endif
5690b9f5f8aSAndrey V. Elsukov #ifdef INET6
5700b9f5f8aSAndrey V. Elsukov 	case IPPROTO_IPV6:
5710b9f5f8aSAndrey V. Elsukov 		af = AF_INET6;
5720b9f5f8aSAndrey V. Elsukov 		if (m->m_len < sizeof(struct ip6_hdr))
5730b9f5f8aSAndrey V. Elsukov 			m = m_pullup(m, sizeof(struct ip6_hdr));
5740b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
5750b9f5f8aSAndrey V. Elsukov 			goto drop;
5760b9f5f8aSAndrey V. Elsukov 		t = htonl((uint32_t)ecn << 20);
5770b9f5f8aSAndrey V. Elsukov 		ip6 = mtod(m, struct ip6_hdr *);
5780b9f5f8aSAndrey V. Elsukov 		if (ip6_ecn_egress((ifp->if_flags & IFF_LINK1) ? ECN_ALLOWED:
5790b9f5f8aSAndrey V. Elsukov 		    ECN_NOCARE, &t, &ip6->ip6_flow) == 0) {
5800b9f5f8aSAndrey V. Elsukov 			m_freem(m);
5810b9f5f8aSAndrey V. Elsukov 			goto drop;
5820b9f5f8aSAndrey V. Elsukov 		}
5830b9f5f8aSAndrey V. Elsukov 		break;
5840b9f5f8aSAndrey V. Elsukov #endif
5850b9f5f8aSAndrey V. Elsukov 	case IPPROTO_ETHERIP:
5860b9f5f8aSAndrey V. Elsukov 		af = AF_LINK;
5870b9f5f8aSAndrey V. Elsukov 		break;
5880b9f5f8aSAndrey V. Elsukov 	default:
5890b9f5f8aSAndrey V. Elsukov 		m_freem(m);
5900b9f5f8aSAndrey V. Elsukov 		goto drop;
5910b9f5f8aSAndrey V. Elsukov 	}
592cfa1ca9dSYoshinobu Inoue 
59310722b85SRobert Watson #ifdef MAC
59430d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
59510722b85SRobert Watson #endif
59610722b85SRobert Watson 
59716d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
5980b9f5f8aSAndrey V. Elsukov 		uint32_t af1 = af;
599437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
600cfa1ca9dSYoshinobu Inoue 	}
601cfa1ca9dSYoshinobu Inoue 
602e9f947e2SHiroki Sato 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
6033751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
6043751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
605e9f947e2SHiroki Sato 		m_freem(m);
606e9f947e2SHiroki Sato 		return;
607e9f947e2SHiroki Sato 	}
608e9f947e2SHiroki Sato 
60994408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
61021fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
61194408d94SBrooks Davis 		if (m == NULL)
6120b9f5f8aSAndrey V. Elsukov 			goto drop;
61394408d94SBrooks Davis 	}
61494408d94SBrooks Davis 
615cfa1ca9dSYoshinobu Inoue 	/*
616cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
617cfa1ca9dSYoshinobu Inoue 	 * specified address family.
618cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
619cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
620cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
621cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
622cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
62388ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
62488ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
625cfa1ca9dSYoshinobu Inoue 	 */
626cfa1ca9dSYoshinobu Inoue 	switch (af) {
627cfa1ca9dSYoshinobu Inoue #ifdef INET
628cfa1ca9dSYoshinobu Inoue 	case AF_INET:
629cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
630cfa1ca9dSYoshinobu Inoue 		break;
631cfa1ca9dSYoshinobu Inoue #endif
632cfa1ca9dSYoshinobu Inoue #ifdef INET6
633cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
634cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
635cfa1ca9dSYoshinobu Inoue 		break;
636cfa1ca9dSYoshinobu Inoue #endif
63773ff045cSAndrew Thompson 	case AF_LINK:
63873ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
6390b9f5f8aSAndrey V. Elsukov 		if (n > m->m_len)
64073ff045cSAndrew Thompson 			m = m_pullup(m, n);
6410b9f5f8aSAndrey V. Elsukov 		if (m == NULL)
6420b9f5f8aSAndrey V. Elsukov 			goto drop;
64373ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
644dbe59260SHiroki Sato 		/*
645dbe59260SHiroki Sato 		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
646dbe59260SHiroki Sato 		 * accepts an EtherIP packet with revered version field in
647dbe59260SHiroki Sato 		 * the header.  This is a knob for backward compatibility
648dbe59260SHiroki Sato 		 * with FreeBSD 7.2R or prior.
649dbe59260SHiroki Sato 		 */
650dbe59260SHiroki Sato 		if (eip->eip_ver != ETHERIP_VERSION) {
6510b9f5f8aSAndrey V. Elsukov 			if ((gif_options & GIF_ACCEPT_REVETHIP) == 0 ||
6520b9f5f8aSAndrey V. Elsukov 			    eip->eip_resvl != ETHERIP_VERSION) {
653dbe59260SHiroki Sato 				/* discard unknown versions */
654dbe59260SHiroki Sato 				m_freem(m);
6550b9f5f8aSAndrey V. Elsukov 				goto drop;
656dbe59260SHiroki Sato 			}
657dbe59260SHiroki Sato 		}
65873ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
65973ff045cSAndrew Thompson 
66073ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
66173ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
66273ff045cSAndrew Thompson 
66356abdd33SAndrew Thompson 		if (ifp->if_bridge) {
66456abdd33SAndrew Thompson 			oldifp = ifp;
66556abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
66656abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
66756abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
66856abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
66956abdd33SAndrew Thompson 				else
67056abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
6713751dddbSGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
67256abdd33SAndrew Thompson 			}
67373ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
67473ff045cSAndrew Thompson 
67556abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
67656abdd33SAndrew Thompson 				/*
67756abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
67856abdd33SAndrew Thompson 				 * members for which the frame is addressed.
67956abdd33SAndrew Thompson 				 */
68056abdd33SAndrew Thompson 				ether_demux(ifp, m);
68156abdd33SAndrew Thompson 				return;
68256abdd33SAndrew Thompson 			}
68356abdd33SAndrew Thompson 		}
68473ff045cSAndrew Thompson 		if (m != NULL)
68573ff045cSAndrew Thompson 			m_freem(m);
68673ff045cSAndrew Thompson 		return;
68773ff045cSAndrew Thompson 
688cfa1ca9dSYoshinobu Inoue 	default:
68994408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
69021fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
69194408d94SBrooks Davis 		else
692cfa1ca9dSYoshinobu Inoue 			m_freem(m);
693cfa1ca9dSYoshinobu Inoue 		return;
694cfa1ca9dSYoshinobu Inoue 	}
695cfa1ca9dSYoshinobu Inoue 
6963751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
6973751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
698a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
6991cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
7000b9f5f8aSAndrey V. Elsukov 	return;
7010b9f5f8aSAndrey V. Elsukov drop:
7020b9f5f8aSAndrey V. Elsukov 	if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
703cfa1ca9dSYoshinobu Inoue }
704cfa1ca9dSYoshinobu Inoue 
705686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
706cfa1ca9dSYoshinobu Inoue int
707c72a5d5dSAndrey V. Elsukov gif_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
708cfa1ca9dSYoshinobu Inoue {
7090b9f5f8aSAndrey V. Elsukov 	GIF_RLOCK_TRACKER;
710cfa1ca9dSYoshinobu Inoue 	struct ifreq *ifr = (struct ifreq*)data;
711686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
7120b9f5f8aSAndrey V. Elsukov 	struct gif_softc *sc;
7130b9f5f8aSAndrey V. Elsukov #ifdef INET
7140b9f5f8aSAndrey V. Elsukov 	struct sockaddr_in *sin = NULL;
7153bb61ca6SHajimu UMEMOTO #endif
7160b9f5f8aSAndrey V. Elsukov #ifdef INET6
7170b9f5f8aSAndrey V. Elsukov 	struct sockaddr_in6 *sin6 = NULL;
7180b9f5f8aSAndrey V. Elsukov #endif
7190b9f5f8aSAndrey V. Elsukov 	u_int options;
7200b9f5f8aSAndrey V. Elsukov 	int error;
721cfa1ca9dSYoshinobu Inoue 
722cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
723cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
7249426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
725cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
726cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
727cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
7280b9f5f8aSAndrey V. Elsukov 	case SIOCSIFFLAGS:
7290b9f5f8aSAndrey V. Elsukov 		return (0);
730cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
7310b9f5f8aSAndrey V. Elsukov 		if (ifr->ifr_mtu < GIF_MTU_MIN ||
7320b9f5f8aSAndrey V. Elsukov 		    ifr->ifr_mtu > GIF_MTU_MAX)
733cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
7340b9f5f8aSAndrey V. Elsukov 		else
7350b9f5f8aSAndrey V. Elsukov 			ifp->if_mtu = ifr->ifr_mtu;
7360b9f5f8aSAndrey V. Elsukov 		return (0);
7370b9f5f8aSAndrey V. Elsukov 	}
7380b9f5f8aSAndrey V. Elsukov 	sx_xlock(&gif_ioctl_sx);
7390b9f5f8aSAndrey V. Elsukov 	sc = ifp->if_softc;
7400b9f5f8aSAndrey V. Elsukov 	if (sc == NULL) {
7410b9f5f8aSAndrey V. Elsukov 		error = ENXIO;
7420b9f5f8aSAndrey V. Elsukov 		goto bad;
7430b9f5f8aSAndrey V. Elsukov 	}
7440b9f5f8aSAndrey V. Elsukov 	error = 0;
7450b9f5f8aSAndrey V. Elsukov 	switch (cmd) {
746cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
747cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
7480b9f5f8aSAndrey V. Elsukov 		error = EINVAL;
749686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
75033841545SHajimu UMEMOTO #ifdef INET
751686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
752cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
753cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
754cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
755cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
756cfa1ca9dSYoshinobu Inoue 			break;
75733841545SHajimu UMEMOTO #endif
758cfa1ca9dSYoshinobu Inoue #ifdef INET6
759686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
760cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
761cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
762cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
763cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
764686cdd19SJun-ichiro itojun Hagino 			break;
765686cdd19SJun-ichiro itojun Hagino #endif
7666f4ded3aSBrooks Davis 		default:
7670b9f5f8aSAndrey V. Elsukov 			goto bad;
76833841545SHajimu UMEMOTO 		}
76933841545SHajimu UMEMOTO 		/* sa_family must be equal */
7700b9f5f8aSAndrey V. Elsukov 		if (src->sa_family != dst->sa_family ||
7710b9f5f8aSAndrey V. Elsukov 		    src->sa_len != dst->sa_len)
7720b9f5f8aSAndrey V. Elsukov 			goto bad;
77333841545SHajimu UMEMOTO 
77433841545SHajimu UMEMOTO 		/* validate sa_len */
77533841545SHajimu UMEMOTO 		switch (src->sa_family) {
77633841545SHajimu UMEMOTO #ifdef INET
77733841545SHajimu UMEMOTO 		case AF_INET:
77833841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
7790b9f5f8aSAndrey V. Elsukov 				goto bad;
78033841545SHajimu UMEMOTO 			break;
78133841545SHajimu UMEMOTO #endif
78233841545SHajimu UMEMOTO #ifdef INET6
78333841545SHajimu UMEMOTO 		case AF_INET6:
78433841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
7850b9f5f8aSAndrey V. Elsukov 				goto bad;
78633841545SHajimu UMEMOTO 			break;
78733841545SHajimu UMEMOTO #endif
78833841545SHajimu UMEMOTO 		default:
7890b9f5f8aSAndrey V. Elsukov 			error = EAFNOSUPPORT;
7900b9f5f8aSAndrey V. Elsukov 			goto bad;
79133841545SHajimu UMEMOTO 		}
79233841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
7930b9f5f8aSAndrey V. Elsukov 		error = EAFNOSUPPORT;
79433841545SHajimu UMEMOTO 		switch (cmd) {
7950b9f5f8aSAndrey V. Elsukov #ifdef INET
79633841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
79733841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
79833841545SHajimu UMEMOTO 				break;
7990b9f5f8aSAndrey V. Elsukov 			goto bad;
8000b9f5f8aSAndrey V. Elsukov #endif
80133841545SHajimu UMEMOTO #ifdef INET6
80233841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
80333841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
80433841545SHajimu UMEMOTO 				break;
8050b9f5f8aSAndrey V. Elsukov 			goto bad;
8060b9f5f8aSAndrey V. Elsukov #endif
807686cdd19SJun-ichiro itojun Hagino 		}
8080b9f5f8aSAndrey V. Elsukov 		error = EADDRNOTAVAIL;
8090b9f5f8aSAndrey V. Elsukov 		switch (src->sa_family) {
8100b9f5f8aSAndrey V. Elsukov #ifdef INET
8110b9f5f8aSAndrey V. Elsukov 		case AF_INET:
8120b9f5f8aSAndrey V. Elsukov 			if (satosin(src)->sin_addr.s_addr == INADDR_ANY ||
8130b9f5f8aSAndrey V. Elsukov 			    satosin(dst)->sin_addr.s_addr == INADDR_ANY)
8140b9f5f8aSAndrey V. Elsukov 				goto bad;
815cfa1ca9dSYoshinobu Inoue 			break;
8160b9f5f8aSAndrey V. Elsukov #endif
8170b9f5f8aSAndrey V. Elsukov #ifdef INET6
8180b9f5f8aSAndrey V. Elsukov 		case AF_INET6:
8190b9f5f8aSAndrey V. Elsukov 			if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr)
8200b9f5f8aSAndrey V. Elsukov 			    ||
8210b9f5f8aSAndrey V. Elsukov 			    IN6_IS_ADDR_UNSPECIFIED(&satosin6(dst)->sin6_addr))
8220b9f5f8aSAndrey V. Elsukov 				goto bad;
8230b9f5f8aSAndrey V. Elsukov 			/*
8240b9f5f8aSAndrey V. Elsukov 			 * Check validity of the scope zone ID of the
8250b9f5f8aSAndrey V. Elsukov 			 * addresses, and convert it into the kernel
8260b9f5f8aSAndrey V. Elsukov 			 * internal form if necessary.
8270b9f5f8aSAndrey V. Elsukov 			 */
8280b9f5f8aSAndrey V. Elsukov 			error = sa6_embedscope(satosin6(src), 0);
8290b9f5f8aSAndrey V. Elsukov 			if (error != 0)
8300b9f5f8aSAndrey V. Elsukov 				goto bad;
8310b9f5f8aSAndrey V. Elsukov 			error = sa6_embedscope(satosin6(dst), 0);
8320b9f5f8aSAndrey V. Elsukov 			if (error != 0)
8330b9f5f8aSAndrey V. Elsukov 				goto bad;
8340b9f5f8aSAndrey V. Elsukov #endif
8350b9f5f8aSAndrey V. Elsukov 		};
8360b9f5f8aSAndrey V. Elsukov 		error = gif_set_tunnel(ifp, src, dst);
8370b9f5f8aSAndrey V. Elsukov 		break;
838686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
8390b9f5f8aSAndrey V. Elsukov 		gif_delete_tunnel(ifp);
840686cdd19SJun-ichiro itojun Hagino 		break;
841cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
8420b9f5f8aSAndrey V. Elsukov 	case SIOCGIFPDSTADDR:
843cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
8440b9f5f8aSAndrey V. Elsukov 	case SIOCGIFPDSTADDR_IN6:
8450b9f5f8aSAndrey V. Elsukov 		if (sc->gif_family == 0) {
846cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
8470b9f5f8aSAndrey V. Elsukov 			break;
848cfa1ca9dSYoshinobu Inoue 		}
8490b9f5f8aSAndrey V. Elsukov 		GIF_RLOCK(sc);
85033841545SHajimu UMEMOTO 		switch (cmd) {
851cfa1ca9dSYoshinobu Inoue #ifdef INET
85233841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
8530b9f5f8aSAndrey V. Elsukov 		case SIOCGIFPDSTADDR:
8540b9f5f8aSAndrey V. Elsukov 			if (sc->gif_family != AF_INET) {
8550b9f5f8aSAndrey V. Elsukov 				error = EADDRNOTAVAIL;
856cfa1ca9dSYoshinobu Inoue 				break;
8570b9f5f8aSAndrey V. Elsukov 			}
8580b9f5f8aSAndrey V. Elsukov 			sin = (struct sockaddr_in *)&ifr->ifr_addr;
8590b9f5f8aSAndrey V. Elsukov 			memset(sin, 0, sizeof(*sin));
8600b9f5f8aSAndrey V. Elsukov 			sin->sin_family = AF_INET;
8610b9f5f8aSAndrey V. Elsukov 			sin->sin_len = sizeof(*sin);
8620b9f5f8aSAndrey V. Elsukov 			break;
8630b9f5f8aSAndrey V. Elsukov #endif
864cfa1ca9dSYoshinobu Inoue #ifdef INET6
86533841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
866cfa1ca9dSYoshinobu Inoue 		case SIOCGIFPDSTADDR_IN6:
8670b9f5f8aSAndrey V. Elsukov 			if (sc->gif_family != AF_INET6) {
868cfa1ca9dSYoshinobu Inoue 				error = EADDRNOTAVAIL;
8690b9f5f8aSAndrey V. Elsukov 				break;
870cfa1ca9dSYoshinobu Inoue 			}
8710b9f5f8aSAndrey V. Elsukov 			sin6 = (struct sockaddr_in6 *)
8720b9f5f8aSAndrey V. Elsukov 				&(((struct in6_ifreq *)data)->ifr_addr);
8730b9f5f8aSAndrey V. Elsukov 			memset(sin6, 0, sizeof(*sin6));
8740b9f5f8aSAndrey V. Elsukov 			sin6->sin6_family = AF_INET6;
8750b9f5f8aSAndrey V. Elsukov 			sin6->sin6_len = sizeof(*sin6);
8760b9f5f8aSAndrey V. Elsukov 			break;
8770b9f5f8aSAndrey V. Elsukov #endif
8780b9f5f8aSAndrey V. Elsukov 		default:
8790b9f5f8aSAndrey V. Elsukov 			error = EAFNOSUPPORT;
8800b9f5f8aSAndrey V. Elsukov 		}
8810b9f5f8aSAndrey V. Elsukov 		if (error == 0) {
88233841545SHajimu UMEMOTO 			switch (cmd) {
883cfa1ca9dSYoshinobu Inoue #ifdef INET
8840b9f5f8aSAndrey V. Elsukov 			case SIOCGIFPSRCADDR:
8850b9f5f8aSAndrey V. Elsukov 				sin->sin_addr = sc->gif_iphdr->ip_src;
8860b9f5f8aSAndrey V. Elsukov 				break;
88733841545SHajimu UMEMOTO 			case SIOCGIFPDSTADDR:
8880b9f5f8aSAndrey V. Elsukov 				sin->sin_addr = sc->gif_iphdr->ip_dst;
889cfa1ca9dSYoshinobu Inoue 				break;
890a1f7e5f8SHajimu UMEMOTO #endif
8910b9f5f8aSAndrey V. Elsukov #ifdef INET6
8920b9f5f8aSAndrey V. Elsukov 			case SIOCGIFPSRCADDR_IN6:
8930b9f5f8aSAndrey V. Elsukov 				sin6->sin6_addr = sc->gif_ip6hdr->ip6_src;
89433841545SHajimu UMEMOTO 				break;
8950b9f5f8aSAndrey V. Elsukov 			case SIOCGIFPDSTADDR_IN6:
8960b9f5f8aSAndrey V. Elsukov 				sin6->sin6_addr = sc->gif_ip6hdr->ip6_dst;
897cfa1ca9dSYoshinobu Inoue 				break;
8980b9f5f8aSAndrey V. Elsukov #endif
8990b9f5f8aSAndrey V. Elsukov 			}
9000b9f5f8aSAndrey V. Elsukov 		}
9010b9f5f8aSAndrey V. Elsukov 		GIF_RUNLOCK(sc);
9020b9f5f8aSAndrey V. Elsukov 		if (error != 0)
9030b9f5f8aSAndrey V. Elsukov 			break;
9040b9f5f8aSAndrey V. Elsukov 		switch (cmd) {
9050b9f5f8aSAndrey V. Elsukov #ifdef INET
9060b9f5f8aSAndrey V. Elsukov 		case SIOCGIFPSRCADDR:
9070b9f5f8aSAndrey V. Elsukov 		case SIOCGIFPDSTADDR:
9080b9f5f8aSAndrey V. Elsukov 			error = prison_if(curthread->td_ucred,
9090b9f5f8aSAndrey V. Elsukov 			    (struct sockaddr *)sin);
9100b9f5f8aSAndrey V. Elsukov 			if (error != 0)
9110b9f5f8aSAndrey V. Elsukov 				memset(sin, 0, sizeof(*sin));
9120b9f5f8aSAndrey V. Elsukov 			break;
9130b9f5f8aSAndrey V. Elsukov #endif
9140b9f5f8aSAndrey V. Elsukov #ifdef INET6
9150b9f5f8aSAndrey V. Elsukov 		case SIOCGIFPSRCADDR_IN6:
9160b9f5f8aSAndrey V. Elsukov 		case SIOCGIFPDSTADDR_IN6:
9170b9f5f8aSAndrey V. Elsukov 			error = prison_if(curthread->td_ucred,
9180b9f5f8aSAndrey V. Elsukov 			    (struct sockaddr *)sin6);
9190b9f5f8aSAndrey V. Elsukov 			if (error == 0)
9200b9f5f8aSAndrey V. Elsukov 				error = sa6_recoverscope(sin6);
9210b9f5f8aSAndrey V. Elsukov 			if (error != 0)
9220b9f5f8aSAndrey V. Elsukov 				memset(sin6, 0, sizeof(*sin6));
923*138d5655SAndrey V. Elsukov #endif
9240b9f5f8aSAndrey V. Elsukov 		}
9250b9f5f8aSAndrey V. Elsukov 		break;
926dbe59260SHiroki Sato 	case GIFGOPTS:
927dbe59260SHiroki Sato 		options = sc->gif_options;
9280b9f5f8aSAndrey V. Elsukov 		error = copyout(&options, ifr->ifr_data, sizeof(options));
929dbe59260SHiroki Sato 		break;
930dbe59260SHiroki Sato 	case GIFSOPTS:
931dbe59260SHiroki Sato 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
932dbe59260SHiroki Sato 			break;
9334cd5f57dSHiroki Sato 		error = copyin(ifr->ifr_data, &options, sizeof(options));
9344cd5f57dSHiroki Sato 		if (error)
9354cd5f57dSHiroki Sato 			break;
9364cd5f57dSHiroki Sato 		if (options & ~GIF_OPTMASK)
937dbe59260SHiroki Sato 			error = EINVAL;
9384cd5f57dSHiroki Sato 		else
9394cd5f57dSHiroki Sato 			sc->gif_options = options;
940dbe59260SHiroki Sato 		break;
941dbe59260SHiroki Sato 
942cfa1ca9dSYoshinobu Inoue 	default:
943cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
944cfa1ca9dSYoshinobu Inoue 		break;
945cfa1ca9dSYoshinobu Inoue 	}
946cfa1ca9dSYoshinobu Inoue bad:
9470b9f5f8aSAndrey V. Elsukov 	sx_xunlock(&gif_ioctl_sx);
9480b9f5f8aSAndrey V. Elsukov 	return (error);
949cfa1ca9dSYoshinobu Inoue }
95053dab5feSBrooks Davis 
9510b9f5f8aSAndrey V. Elsukov static void
9520b9f5f8aSAndrey V. Elsukov gif_detach(struct gif_softc *sc)
9530b9f5f8aSAndrey V. Elsukov {
9540b9f5f8aSAndrey V. Elsukov 
9550b9f5f8aSAndrey V. Elsukov 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
9560b9f5f8aSAndrey V. Elsukov 	if (sc->gif_ecookie != NULL)
9570b9f5f8aSAndrey V. Elsukov 		encap_detach(sc->gif_ecookie);
9580b9f5f8aSAndrey V. Elsukov 	sc->gif_ecookie = NULL;
9590b9f5f8aSAndrey V. Elsukov }
9600b9f5f8aSAndrey V. Elsukov 
9610b9f5f8aSAndrey V. Elsukov static int
9620b9f5f8aSAndrey V. Elsukov gif_attach(struct gif_softc *sc, int af)
9630b9f5f8aSAndrey V. Elsukov {
9640b9f5f8aSAndrey V. Elsukov 
9650b9f5f8aSAndrey V. Elsukov 	sx_assert(&gif_ioctl_sx, SA_XLOCKED);
9660b9f5f8aSAndrey V. Elsukov 	switch (af) {
9670b9f5f8aSAndrey V. Elsukov #ifdef INET
9680b9f5f8aSAndrey V. Elsukov 	case AF_INET:
9690b9f5f8aSAndrey V. Elsukov 		return (in_gif_attach(sc));
9700b9f5f8aSAndrey V. Elsukov #endif
9710b9f5f8aSAndrey V. Elsukov #ifdef INET6
9720b9f5f8aSAndrey V. Elsukov 	case AF_INET6:
9730b9f5f8aSAndrey V. Elsukov 		return (in6_gif_attach(sc));
9740b9f5f8aSAndrey V. Elsukov #endif
9750b9f5f8aSAndrey V. Elsukov 	}
9760b9f5f8aSAndrey V. Elsukov 	return (EAFNOSUPPORT);
9770b9f5f8aSAndrey V. Elsukov }
9780b9f5f8aSAndrey V. Elsukov 
9790b9f5f8aSAndrey V. Elsukov static int
980c72a5d5dSAndrey V. Elsukov gif_set_tunnel(struct ifnet *ifp, struct sockaddr *src, struct sockaddr *dst)
98153dab5feSBrooks Davis {
982fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
9830b9f5f8aSAndrey V. Elsukov 	struct gif_softc *tsc;
9840b9f5f8aSAndrey V. Elsukov #ifdef INET
9850b9f5f8aSAndrey V. Elsukov 	struct ip *ip;
9860b9f5f8aSAndrey V. Elsukov #endif
9870b9f5f8aSAndrey V. Elsukov #ifdef INET6
9880b9f5f8aSAndrey V. Elsukov 	struct ip6_hdr *ip6;
9890b9f5f8aSAndrey V. Elsukov #endif
9900b9f5f8aSAndrey V. Elsukov 	void *hdr;
9919426aedfSHajimu UMEMOTO 	int error = 0;
9929426aedfSHajimu UMEMOTO 
9930b9f5f8aSAndrey V. Elsukov 	if (sc == NULL)
9940b9f5f8aSAndrey V. Elsukov 		return (ENXIO);
9950b9f5f8aSAndrey V. Elsukov 	/* Disallow parallel tunnels unless instructed otherwise. */
9960b9f5f8aSAndrey V. Elsukov 	if (V_parallel_tunnels == 0) {
997a7f5886eSHiroki Sato 		GIF_LIST_LOCK();
9980b9f5f8aSAndrey V. Elsukov 		LIST_FOREACH(tsc, &V_gif_softc_list, gif_list) {
9990b9f5f8aSAndrey V. Elsukov 			if (tsc == sc || tsc->gif_family != src->sa_family)
10009426aedfSHajimu UMEMOTO 				continue;
10010b9f5f8aSAndrey V. Elsukov #ifdef INET
10020b9f5f8aSAndrey V. Elsukov 			if (tsc->gif_family == AF_INET &&
10030b9f5f8aSAndrey V. Elsukov 			    tsc->gif_iphdr->ip_src.s_addr ==
10040b9f5f8aSAndrey V. Elsukov 			    satosin(src)->sin_addr.s_addr &&
10050b9f5f8aSAndrey V. Elsukov 			    tsc->gif_iphdr->ip_dst.s_addr ==
10060b9f5f8aSAndrey V. Elsukov 			    satosin(dst)->sin_addr.s_addr) {
10079426aedfSHajimu UMEMOTO 				error = EADDRNOTAVAIL;
1008a7f5886eSHiroki Sato 				GIF_LIST_UNLOCK();
10099426aedfSHajimu UMEMOTO 				goto bad;
10109426aedfSHajimu UMEMOTO 			}
10110b9f5f8aSAndrey V. Elsukov #endif
10120b9f5f8aSAndrey V. Elsukov #ifdef INET6
10130b9f5f8aSAndrey V. Elsukov 			if (tsc->gif_family == AF_INET6 &&
10140b9f5f8aSAndrey V. Elsukov 			    IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_src,
10150b9f5f8aSAndrey V. Elsukov 			    &satosin6(src)->sin6_addr) &&
10160b9f5f8aSAndrey V. Elsukov 			    IN6_ARE_ADDR_EQUAL(&tsc->gif_ip6hdr->ip6_dst,
10170b9f5f8aSAndrey V. Elsukov 			    &satosin6(dst)->sin6_addr)) {
10180b9f5f8aSAndrey V. Elsukov 				error = EADDRNOTAVAIL;
1019a7f5886eSHiroki Sato 				GIF_LIST_UNLOCK();
10209426aedfSHajimu UMEMOTO 				goto bad;
10219426aedfSHajimu UMEMOTO 			}
10220b9f5f8aSAndrey V. Elsukov #endif
10230b9f5f8aSAndrey V. Elsukov 		}
10240b9f5f8aSAndrey V. Elsukov 		GIF_LIST_UNLOCK();
10250b9f5f8aSAndrey V. Elsukov 	}
10260b9f5f8aSAndrey V. Elsukov 	switch (src->sa_family) {
10270b9f5f8aSAndrey V. Elsukov #ifdef INET
10280b9f5f8aSAndrey V. Elsukov 	case AF_INET:
10290b9f5f8aSAndrey V. Elsukov 		hdr = ip = malloc(sizeof(struct ip), M_GIF,
10300b9f5f8aSAndrey V. Elsukov 		    M_WAITOK | M_ZERO);
10310b9f5f8aSAndrey V. Elsukov 		ip->ip_src.s_addr = satosin(src)->sin_addr.s_addr;
10320b9f5f8aSAndrey V. Elsukov 		ip->ip_dst.s_addr = satosin(dst)->sin_addr.s_addr;
10330b9f5f8aSAndrey V. Elsukov 		break;
10340b9f5f8aSAndrey V. Elsukov #endif
10350b9f5f8aSAndrey V. Elsukov #ifdef INET6
10360b9f5f8aSAndrey V. Elsukov 	case AF_INET6:
10370b9f5f8aSAndrey V. Elsukov 		hdr = ip6 = malloc(sizeof(struct ip6_hdr), M_GIF,
10380b9f5f8aSAndrey V. Elsukov 		    M_WAITOK | M_ZERO);
10390b9f5f8aSAndrey V. Elsukov 		ip6->ip6_src = satosin6(src)->sin6_addr;
10400b9f5f8aSAndrey V. Elsukov 		ip6->ip6_dst = satosin6(dst)->sin6_addr;
10410b9f5f8aSAndrey V. Elsukov 		ip6->ip6_vfc = IPV6_VERSION;
10420b9f5f8aSAndrey V. Elsukov 		break;
10430b9f5f8aSAndrey V. Elsukov #endif
10440b9f5f8aSAndrey V. Elsukov 	default:
10450b9f5f8aSAndrey V. Elsukov 		return (EAFNOSUPPORT);
10460b9f5f8aSAndrey V. Elsukov 	};
10479426aedfSHajimu UMEMOTO 
10480b9f5f8aSAndrey V. Elsukov 	if (sc->gif_family != src->sa_family)
10490b9f5f8aSAndrey V. Elsukov 		gif_detach(sc);
10500b9f5f8aSAndrey V. Elsukov 	if (sc->gif_family == 0 ||
10510b9f5f8aSAndrey V. Elsukov 	    sc->gif_family != src->sa_family)
10520b9f5f8aSAndrey V. Elsukov 		error = gif_attach(sc, src->sa_family);
10539426aedfSHajimu UMEMOTO 
10540b9f5f8aSAndrey V. Elsukov 	GIF_WLOCK(sc);
10550b9f5f8aSAndrey V. Elsukov 	if (sc->gif_family != 0)
10560b9f5f8aSAndrey V. Elsukov 		free(sc->gif_hdr, M_GIF);
10570b9f5f8aSAndrey V. Elsukov 	sc->gif_family = src->sa_family;
10580b9f5f8aSAndrey V. Elsukov 	sc->gif_hdr = hdr;
10590b9f5f8aSAndrey V. Elsukov 	GIF_WUNLOCK(sc);
10609426aedfSHajimu UMEMOTO bad:
10610b9f5f8aSAndrey V. Elsukov 	if (error == 0 && sc->gif_family != 0)
106213f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
10639426aedfSHajimu UMEMOTO 	else
106413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
10650b9f5f8aSAndrey V. Elsukov 	return (error);
10669426aedfSHajimu UMEMOTO }
10679426aedfSHajimu UMEMOTO 
10680b9f5f8aSAndrey V. Elsukov static void
1069c72a5d5dSAndrey V. Elsukov gif_delete_tunnel(struct ifnet *ifp)
10709426aedfSHajimu UMEMOTO {
1071fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
10720b9f5f8aSAndrey V. Elsukov 	int family;
107353dab5feSBrooks Davis 
10740b9f5f8aSAndrey V. Elsukov 	if (sc == NULL)
10750b9f5f8aSAndrey V. Elsukov 		return;
10760b9f5f8aSAndrey V. Elsukov 
10770b9f5f8aSAndrey V. Elsukov 	GIF_WLOCK(sc);
10780b9f5f8aSAndrey V. Elsukov 	family = sc->gif_family;
10790b9f5f8aSAndrey V. Elsukov 	sc->gif_family = 0;
10800b9f5f8aSAndrey V. Elsukov 	GIF_WUNLOCK(sc);
10810b9f5f8aSAndrey V. Elsukov 	if (family != 0) {
10820b9f5f8aSAndrey V. Elsukov 		gif_detach(sc);
10830b9f5f8aSAndrey V. Elsukov 		free(sc->gif_hdr, M_GIF);
108453dab5feSBrooks Davis 	}
108513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
108653dab5feSBrooks Davis }
1087