xref: /freebsd/sys/net/if_gif.c (revision 01399f34a56a26b5520766a47be1a6e1f1df9783)
1686cdd19SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
288ff5695SSUZUKI Shinsuke /*	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $	*/
3686cdd19SJun-ichiro itojun Hagino 
4c398230bSWarner Losh /*-
5cfa1ca9dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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.
31cfa1ca9dSYoshinobu Inoue  */
32cfa1ca9dSYoshinobu Inoue 
33cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
35e0852ce2SRobert Watson #include "opt_mac.h"
36cfa1ca9dSYoshinobu Inoue 
37cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
38cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
4010722b85SRobert Watson #include <sys/mac.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h>
42cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
435dba30f1SPoul-Henning Kamp #include <sys/module.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
46cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/time.h>
48872f786aSBrooks Davis #include <sys/sysctl.h>
49cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h>
50686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5153dab5feSBrooks Davis #include <sys/conf.h>
52cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
53cfa1ca9dSYoshinobu Inoue 
54cfa1ca9dSYoshinobu Inoue #include <net/if.h>
55f889d2efSBrooks Davis #include <net/if_clone.h>
56cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
57cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
58cfa1ca9dSYoshinobu Inoue #include <net/route.h>
59cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
60cfa1ca9dSYoshinobu Inoue 
61cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
62cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
63cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
6433841545SHajimu UMEMOTO #ifdef	INET
6533841545SHajimu UMEMOTO #include <netinet/in_var.h>
66cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
6753dab5feSBrooks Davis #include <netinet/ip_var.h>
68cfa1ca9dSYoshinobu Inoue #endif	/* INET */
69cfa1ca9dSYoshinobu Inoue 
70cfa1ca9dSYoshinobu Inoue #ifdef INET6
71cfa1ca9dSYoshinobu Inoue #ifndef INET
72cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
73cfa1ca9dSYoshinobu Inoue #endif
74cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
76cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
77cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
78686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
79cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
80cfa1ca9dSYoshinobu Inoue 
81686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
82cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
83cfa1ca9dSYoshinobu Inoue 
84cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h>
85cfa1ca9dSYoshinobu Inoue 
8653dab5feSBrooks Davis #define GIFNAME		"gif"
87686cdd19SJun-ichiro itojun Hagino 
8817d5cb2dSRobert Watson /*
898c7e1947SRuslan Ermilov  * gif_mtx protects the global gif_softc_list.
9017d5cb2dSRobert Watson  * XXX: Per-softc locking is still required.
9117d5cb2dSRobert Watson  */
9217d5cb2dSRobert Watson static struct mtx gif_mtx;
9353dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
94e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
9553dab5feSBrooks Davis 
9694408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
9794408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
9894408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
9994408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
10094408d94SBrooks Davis 
101bb2bfb4fSBrooks Davis static int	gif_clone_create(struct if_clone *, int);
102bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
10353dab5feSBrooks Davis 
104f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0);
10553dab5feSBrooks Davis 
106929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
107cfa1ca9dSYoshinobu Inoue 
108872f786aSBrooks Davis SYSCTL_DECL(_net_link);
109872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
110872f786aSBrooks Davis     "Generic Tunnel Interface");
111686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
112686cdd19SJun-ichiro itojun Hagino /*
113872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
114686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
115686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
116686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
117686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
118686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
119686cdd19SJun-ichiro itojun Hagino  */
120686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
121686cdd19SJun-ichiro itojun Hagino #endif
122686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST;
123872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
124872f786aSBrooks Davis     &max_gif_nesting, 0, "Max nested tunnels");
125872f786aSBrooks Davis 
126872f786aSBrooks Davis /*
127872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
128872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
129872f786aSBrooks Davis  * we allow control over this check here.
130872f786aSBrooks Davis  */
131872f786aSBrooks Davis #ifdef XBONEHACK
132872f786aSBrooks Davis static int parallel_tunnels = 1;
133872f786aSBrooks Davis #else
134872f786aSBrooks Davis static int parallel_tunnels = 0;
135872f786aSBrooks Davis #endif
136872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
137872f786aSBrooks Davis     &parallel_tunnels, 0, "Allow parallel tunnels?");
138cfa1ca9dSYoshinobu Inoue 
139bb2bfb4fSBrooks Davis static int
14053dab5feSBrooks Davis gif_clone_create(ifc, unit)
14153dab5feSBrooks Davis 	struct if_clone *ifc;
1423b16e7b2SMaxime Henrion 	int unit;
143cfa1ca9dSYoshinobu Inoue {
14433841545SHajimu UMEMOTO 	struct gif_softc *sc;
145cfa1ca9dSYoshinobu Inoue 
146e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
147fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
148fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
149fc74a9f9SBrooks Davis 		free(sc, M_GIF);
150fc74a9f9SBrooks Davis 		return (ENOSPC);
151fc74a9f9SBrooks Davis 	}
15253dab5feSBrooks Davis 
153fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
154fc74a9f9SBrooks Davis 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
155686cdd19SJun-ichiro itojun Hagino 
1569426aedfSHajimu UMEMOTO 	gifattach0(sc);
157686cdd19SJun-ichiro itojun Hagino 
15817d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
1599426aedfSHajimu UMEMOTO 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
16017d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
1619426aedfSHajimu UMEMOTO 	return (0);
1629426aedfSHajimu UMEMOTO }
1639426aedfSHajimu UMEMOTO 
1649426aedfSHajimu UMEMOTO void
1659426aedfSHajimu UMEMOTO gifattach0(sc)
1669426aedfSHajimu UMEMOTO 	struct gif_softc *sc;
1679426aedfSHajimu UMEMOTO {
1689426aedfSHajimu UMEMOTO 
1699426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1709426aedfSHajimu UMEMOTO 
171fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
172fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
173fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
17433841545SHajimu UMEMOTO #if 0
17533841545SHajimu UMEMOTO 	/* turn off ingress filter */
176fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
17733841545SHajimu UMEMOTO #endif
178fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
179fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
180fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
181fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
18201399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
18394408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
184fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
185cfa1ca9dSYoshinobu Inoue }
186cfa1ca9dSYoshinobu Inoue 
18717d5cb2dSRobert Watson static void
18817d5cb2dSRobert Watson gif_destroy(struct gif_softc *sc)
18953dab5feSBrooks Davis {
190fc74a9f9SBrooks Davis 	struct ifnet *ifp = GIF2IFP(sc);
19153dab5feSBrooks Davis 	int err;
19253dab5feSBrooks Davis 
19317d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
1949426aedfSHajimu UMEMOTO #ifdef INET6
19553dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
19653dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
19753dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
19853dab5feSBrooks Davis 	}
1999426aedfSHajimu UMEMOTO #endif
2009426aedfSHajimu UMEMOTO #ifdef INET
2019426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2029426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2039426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2049426aedfSHajimu UMEMOTO 	}
2059426aedfSHajimu UMEMOTO #endif
20653dab5feSBrooks Davis 
20794408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
20894408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
20953dab5feSBrooks Davis 	bpfdetach(ifp);
21053dab5feSBrooks Davis 	if_detach(ifp);
211fc74a9f9SBrooks Davis 	if_free(ifp);
21253dab5feSBrooks Davis 
21353dab5feSBrooks Davis 	free(sc, M_GIF);
21453dab5feSBrooks Davis }
21553dab5feSBrooks Davis 
216bb2bfb4fSBrooks Davis static void
21717d5cb2dSRobert Watson gif_clone_destroy(ifp)
21817d5cb2dSRobert Watson 	struct ifnet *ifp;
21917d5cb2dSRobert Watson {
22017d5cb2dSRobert Watson 	struct gif_softc *sc = ifp->if_softc;
22117d5cb2dSRobert Watson 
22217d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
22317d5cb2dSRobert Watson 	LIST_REMOVE(sc, gif_list);
22417d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
22517d5cb2dSRobert Watson 	gif_destroy(sc);
22617d5cb2dSRobert Watson }
22717d5cb2dSRobert Watson 
22853dab5feSBrooks Davis static int
22953dab5feSBrooks Davis gifmodevent(mod, type, data)
23053dab5feSBrooks Davis 	module_t mod;
23153dab5feSBrooks Davis 	int type;
23253dab5feSBrooks Davis 	void *data;
23353dab5feSBrooks Davis {
23417d5cb2dSRobert Watson 	struct gif_softc *sc;
23553dab5feSBrooks Davis 
23653dab5feSBrooks Davis 	switch (type) {
23753dab5feSBrooks Davis 	case MOD_LOAD:
23817d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
23920af0ffaSBrooks Davis 		LIST_INIT(&gif_softc_list);
24053dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
24153dab5feSBrooks Davis 
24253dab5feSBrooks Davis #ifdef INET6
24353dab5feSBrooks Davis 		ip6_gif_hlim = GIF_HLIM;
24453dab5feSBrooks Davis #endif
24553dab5feSBrooks Davis 
24653dab5feSBrooks Davis 		break;
24753dab5feSBrooks Davis 	case MOD_UNLOAD:
24853dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
24953dab5feSBrooks Davis 
25017d5cb2dSRobert Watson 		mtx_lock(&gif_mtx);
25117d5cb2dSRobert Watson 		while ((sc = LIST_FIRST(&gif_softc_list)) != NULL) {
25217d5cb2dSRobert Watson 			LIST_REMOVE(sc, gif_list);
25317d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
25417d5cb2dSRobert Watson 			gif_destroy(sc);
25517d5cb2dSRobert Watson 			mtx_lock(&gif_mtx);
25617d5cb2dSRobert Watson 		}
25717d5cb2dSRobert Watson 		mtx_unlock(&gif_mtx);
25817d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
25953dab5feSBrooks Davis #ifdef INET6
26053dab5feSBrooks Davis 		ip6_gif_hlim = 0;
26153dab5feSBrooks Davis #endif
26253dab5feSBrooks Davis 		break;
2633e019deaSPoul-Henning Kamp 	default:
2643e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
26553dab5feSBrooks Davis 	}
26653dab5feSBrooks Davis 	return 0;
26753dab5feSBrooks Davis }
26853dab5feSBrooks Davis 
26953dab5feSBrooks Davis static moduledata_t gif_mod = {
27053dab5feSBrooks Davis 	"if_gif",
27153dab5feSBrooks Davis 	gifmodevent,
27253dab5feSBrooks Davis 	0
27353dab5feSBrooks Davis };
27453dab5feSBrooks Davis 
27553dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
27620af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
277cfa1ca9dSYoshinobu Inoue 
2789426aedfSHajimu UMEMOTO int
279686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
280686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
281686cdd19SJun-ichiro itojun Hagino 	int off;
282686cdd19SJun-ichiro itojun Hagino 	int proto;
283686cdd19SJun-ichiro itojun Hagino 	void *arg;
284686cdd19SJun-ichiro itojun Hagino {
285686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
286686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
287686cdd19SJun-ichiro itojun Hagino 
288686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
289686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
290686cdd19SJun-ichiro itojun Hagino 		return 0;
291686cdd19SJun-ichiro itojun Hagino 
292fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
293686cdd19SJun-ichiro itojun Hagino 		return 0;
294686cdd19SJun-ichiro itojun Hagino 
295686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
296686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
297686cdd19SJun-ichiro itojun Hagino 		return 0;
298686cdd19SJun-ichiro itojun Hagino 
299686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
300686cdd19SJun-ichiro itojun Hagino #ifdef INET
301686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
302686cdd19SJun-ichiro itojun Hagino 		break;
303686cdd19SJun-ichiro itojun Hagino #endif
304686cdd19SJun-ichiro itojun Hagino #ifdef INET6
305686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
306686cdd19SJun-ichiro itojun Hagino 		break;
307686cdd19SJun-ichiro itojun Hagino #endif
308686cdd19SJun-ichiro itojun Hagino 	default:
309686cdd19SJun-ichiro itojun Hagino 		return 0;
310686cdd19SJun-ichiro itojun Hagino 	}
311686cdd19SJun-ichiro itojun Hagino 
3123bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3133bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3143bb61ca6SHajimu UMEMOTO 		return 0;
3153bb61ca6SHajimu UMEMOTO 
3166f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
317686cdd19SJun-ichiro itojun Hagino 
318686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
319686cdd19SJun-ichiro itojun Hagino #ifdef INET
320686cdd19SJun-ichiro itojun Hagino 	case 4:
321686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
322686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
323686cdd19SJun-ichiro itojun Hagino 			return 0;
324686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
325686cdd19SJun-ichiro itojun Hagino #endif
326686cdd19SJun-ichiro itojun Hagino #ifdef INET6
327686cdd19SJun-ichiro itojun Hagino 	case 6:
3289426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3299426aedfSHajimu UMEMOTO 			return 0;
330686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
331686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
332686cdd19SJun-ichiro itojun Hagino 			return 0;
333686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
334686cdd19SJun-ichiro itojun Hagino #endif
335686cdd19SJun-ichiro itojun Hagino 	default:
336686cdd19SJun-ichiro itojun Hagino 		return 0;
337686cdd19SJun-ichiro itojun Hagino 	}
338686cdd19SJun-ichiro itojun Hagino }
339686cdd19SJun-ichiro itojun Hagino 
340cfa1ca9dSYoshinobu Inoue int
341cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
342cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
343cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
344cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
345cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
346cfa1ca9dSYoshinobu Inoue {
347fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
3488c7e1947SRuslan Ermilov 	struct m_tag *mtag;
349cfa1ca9dSYoshinobu Inoue 	int error = 0;
3508c7e1947SRuslan Ermilov 	int gif_called;
35101399f34SDavid Malone 	u_int32_t af;
352cfa1ca9dSYoshinobu Inoue 
35310722b85SRobert Watson #ifdef MAC
35410722b85SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
355e0852ce2SRobert Watson 	if (error) {
356e0852ce2SRobert Watson 		m_freem(m);
357e0852ce2SRobert Watson 		goto end;
358e0852ce2SRobert Watson 	}
35910722b85SRobert Watson #endif
36010722b85SRobert Watson 
361cfa1ca9dSYoshinobu Inoue 	/*
362cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
3638c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
3648c7e1947SRuslan Ermilov 	 *
3658c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
366cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
367cfa1ca9dSYoshinobu Inoue 	 */
3688c7e1947SRuslan Ermilov 	gif_called = 1;
3698c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
3708c7e1947SRuslan Ermilov 	while (mtag != NULL) {
3718c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
3728c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
3738c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
3748c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
3758c7e1947SRuslan Ermilov 			m_freem(m);
3768c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
3778c7e1947SRuslan Ermilov 			goto end;
3788c7e1947SRuslan Ermilov 		}
3798c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
3808c7e1947SRuslan Ermilov 		gif_called++;
3818c7e1947SRuslan Ermilov 	}
3828c7e1947SRuslan Ermilov 	if (gif_called > max_gif_nesting) {
383cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
384cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
385523ebc4eSRobert Watson 		    gif_called);
386cfa1ca9dSYoshinobu Inoue 		m_freem(m);
387cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
388cfa1ca9dSYoshinobu Inoue 		goto end;
389cfa1ca9dSYoshinobu Inoue 	}
3908c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
3918c7e1947SRuslan Ermilov 	    M_NOWAIT);
3928c7e1947SRuslan Ermilov 	if (mtag == NULL) {
3938c7e1947SRuslan Ermilov 		m_freem(m);
3948c7e1947SRuslan Ermilov 		error = ENOMEM;
3958c7e1947SRuslan Ermilov 		goto end;
3968c7e1947SRuslan Ermilov 	}
3978c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
3988c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
399686cdd19SJun-ichiro itojun Hagino 
400cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
401cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
402cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
403cfa1ca9dSYoshinobu Inoue 		m_freem(m);
404cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
405cfa1ca9dSYoshinobu Inoue 		goto end;
406cfa1ca9dSYoshinobu Inoue 	}
407cfa1ca9dSYoshinobu Inoue 
40801399f34SDavid Malone 	/* BPF writes need to be handled specially. */
40901399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
41001399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
41101399f34SDavid Malone 		dst->sa_family = af;
41201399f34SDavid Malone 	}
41301399f34SDavid Malone 
414cfa1ca9dSYoshinobu Inoue 	if (ifp->if_bpf) {
41501399f34SDavid Malone 		af = dst->sa_family;
416437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
417cfa1ca9dSYoshinobu Inoue 	}
418cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
419cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
420cfa1ca9dSYoshinobu Inoue 
42133841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
42233841545SHajimu UMEMOTO 
423686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
424686cdd19SJun-ichiro itojun Hagino 
42533841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
426cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
427cfa1ca9dSYoshinobu Inoue #ifdef INET
428cfa1ca9dSYoshinobu Inoue 	case AF_INET:
429b6e28453SHajimu UMEMOTO 		error = in_gif_output(ifp, dst->sa_family, m);
430cfa1ca9dSYoshinobu Inoue 		break;
431cfa1ca9dSYoshinobu Inoue #endif
432cfa1ca9dSYoshinobu Inoue #ifdef INET6
433cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
434b6e28453SHajimu UMEMOTO 		error = in6_gif_output(ifp, dst->sa_family, m);
435cfa1ca9dSYoshinobu Inoue 		break;
436cfa1ca9dSYoshinobu Inoue #endif
437cfa1ca9dSYoshinobu Inoue 	default:
438cfa1ca9dSYoshinobu Inoue 		m_freem(m);
439cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
44033841545SHajimu UMEMOTO 		goto end;
441cfa1ca9dSYoshinobu Inoue 	}
442cfa1ca9dSYoshinobu Inoue 
443cfa1ca9dSYoshinobu Inoue   end:
44433841545SHajimu UMEMOTO 	if (error)
44533841545SHajimu UMEMOTO 		ifp->if_oerrors++;
446cfa1ca9dSYoshinobu Inoue 	return error;
447cfa1ca9dSYoshinobu Inoue }
448cfa1ca9dSYoshinobu Inoue 
449cfa1ca9dSYoshinobu Inoue void
45021fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
451cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
452cfa1ca9dSYoshinobu Inoue 	int af;
45321fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
454cfa1ca9dSYoshinobu Inoue {
455df5e1987SJonathan Lemon 	int isr;
456cfa1ca9dSYoshinobu Inoue 
45721fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
458cfa1ca9dSYoshinobu Inoue 		/* just in case */
459cfa1ca9dSYoshinobu Inoue 		m_freem(m);
460cfa1ca9dSYoshinobu Inoue 		return;
461cfa1ca9dSYoshinobu Inoue 	}
462cfa1ca9dSYoshinobu Inoue 
46321fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
464cfa1ca9dSYoshinobu Inoue 
46510722b85SRobert Watson #ifdef MAC
46621fb391fSHajimu UMEMOTO 	mac_create_mbuf_from_ifnet(ifp, m);
46710722b85SRobert Watson #endif
46810722b85SRobert Watson 
46921fb391fSHajimu UMEMOTO 	if (ifp->if_bpf) {
47033841545SHajimu UMEMOTO 		u_int32_t af1 = af;
471437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
472cfa1ca9dSYoshinobu Inoue 	}
473cfa1ca9dSYoshinobu Inoue 
47494408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
47521fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
47694408d94SBrooks Davis 		if (m == NULL)
47794408d94SBrooks Davis 			return;
47894408d94SBrooks Davis 	}
47994408d94SBrooks Davis 
480cfa1ca9dSYoshinobu Inoue 	/*
481cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
482cfa1ca9dSYoshinobu Inoue 	 * specified address family.
483cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
484cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
485cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
486cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
487cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
48888ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
48988ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
490cfa1ca9dSYoshinobu Inoue 	 */
491cfa1ca9dSYoshinobu Inoue 	switch (af) {
492cfa1ca9dSYoshinobu Inoue #ifdef INET
493cfa1ca9dSYoshinobu Inoue 	case AF_INET:
494cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
495cfa1ca9dSYoshinobu Inoue 		break;
496cfa1ca9dSYoshinobu Inoue #endif
497cfa1ca9dSYoshinobu Inoue #ifdef INET6
498cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
499cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
500cfa1ca9dSYoshinobu Inoue 		break;
501cfa1ca9dSYoshinobu Inoue #endif
502cfa1ca9dSYoshinobu Inoue 	default:
50394408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
50421fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
50594408d94SBrooks Davis 		else
506cfa1ca9dSYoshinobu Inoue 			m_freem(m);
507cfa1ca9dSYoshinobu Inoue 		return;
508cfa1ca9dSYoshinobu Inoue 	}
509cfa1ca9dSYoshinobu Inoue 
51021fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
51121fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
5121cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
513cfa1ca9dSYoshinobu Inoue }
514cfa1ca9dSYoshinobu Inoue 
515686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
516cfa1ca9dSYoshinobu Inoue int
517cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
518cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
519cfa1ca9dSYoshinobu Inoue 	u_long cmd;
520cfa1ca9dSYoshinobu Inoue 	caddr_t data;
521cfa1ca9dSYoshinobu Inoue {
522fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
523cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
524cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
525686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
5263bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
5273bb61ca6SHajimu UMEMOTO 	u_long mtu;
5283bb61ca6SHajimu UMEMOTO #endif
529cfa1ca9dSYoshinobu Inoue 
530cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
531cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
5329426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
533cfa1ca9dSYoshinobu Inoue 		break;
534cfa1ca9dSYoshinobu Inoue 
535cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
536cfa1ca9dSYoshinobu Inoue 		break;
537cfa1ca9dSYoshinobu Inoue 
538cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
539cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
540cfa1ca9dSYoshinobu Inoue 		break;
541cfa1ca9dSYoshinobu Inoue 
542686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
543cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
544cfa1ca9dSYoshinobu Inoue 		break;
545686cdd19SJun-ichiro itojun Hagino 
546cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
547cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
5483bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
549cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
550cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
551cfa1ca9dSYoshinobu Inoue 		break;
552686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
553cfa1ca9dSYoshinobu Inoue 
5543bb61ca6SHajimu UMEMOTO #ifdef INET
555cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
5563bb61ca6SHajimu UMEMOTO #endif
557cfa1ca9dSYoshinobu Inoue #ifdef INET6
558cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
559cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
56033841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
561686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
56233841545SHajimu UMEMOTO #ifdef INET
563686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
564cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
565cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
566cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
567cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
568cfa1ca9dSYoshinobu Inoue 			break;
56933841545SHajimu UMEMOTO #endif
570cfa1ca9dSYoshinobu Inoue #ifdef INET6
571686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
572cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
573cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
574cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
575cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
576686cdd19SJun-ichiro itojun Hagino 			break;
577686cdd19SJun-ichiro itojun Hagino #endif
57833841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
57933841545SHajimu UMEMOTO 			src = (struct sockaddr *)
58033841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
58133841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
58233841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
5839426aedfSHajimu UMEMOTO 			break;
5846f4ded3aSBrooks Davis 		default:
5859426aedfSHajimu UMEMOTO 			return EINVAL;
58633841545SHajimu UMEMOTO 		}
58733841545SHajimu UMEMOTO 
58833841545SHajimu UMEMOTO 		/* sa_family must be equal */
58933841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
59033841545SHajimu UMEMOTO 			return EINVAL;
59133841545SHajimu UMEMOTO 
59233841545SHajimu UMEMOTO 		/* validate sa_len */
59333841545SHajimu UMEMOTO 		switch (src->sa_family) {
59433841545SHajimu UMEMOTO #ifdef INET
59533841545SHajimu UMEMOTO 		case AF_INET:
59633841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
59733841545SHajimu UMEMOTO 				return EINVAL;
59833841545SHajimu UMEMOTO 			break;
59933841545SHajimu UMEMOTO #endif
60033841545SHajimu UMEMOTO #ifdef INET6
60133841545SHajimu UMEMOTO 		case AF_INET6:
60233841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
60333841545SHajimu UMEMOTO 				return EINVAL;
60433841545SHajimu UMEMOTO 			break;
60533841545SHajimu UMEMOTO #endif
60633841545SHajimu UMEMOTO 		default:
60733841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
60833841545SHajimu UMEMOTO 		}
60933841545SHajimu UMEMOTO 		switch (dst->sa_family) {
61033841545SHajimu UMEMOTO #ifdef INET
61133841545SHajimu UMEMOTO 		case AF_INET:
61233841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
61333841545SHajimu UMEMOTO 				return EINVAL;
61433841545SHajimu UMEMOTO 			break;
61533841545SHajimu UMEMOTO #endif
61633841545SHajimu UMEMOTO #ifdef INET6
61733841545SHajimu UMEMOTO 		case AF_INET6:
61833841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
61933841545SHajimu UMEMOTO 				return EINVAL;
62033841545SHajimu UMEMOTO 			break;
62133841545SHajimu UMEMOTO #endif
62233841545SHajimu UMEMOTO 		default:
62333841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
62433841545SHajimu UMEMOTO 		}
62533841545SHajimu UMEMOTO 
62633841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
62733841545SHajimu UMEMOTO 		switch (cmd) {
62833841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
62933841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
63033841545SHajimu UMEMOTO 				break;
63133841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
63233841545SHajimu UMEMOTO #ifdef INET6
63333841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
63433841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
63533841545SHajimu UMEMOTO 				break;
63633841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
63733841545SHajimu UMEMOTO #endif /* INET6 */
63833841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
63933841545SHajimu UMEMOTO 			/* checks done in the above */
64033841545SHajimu UMEMOTO 			break;
641686cdd19SJun-ichiro itojun Hagino 		}
642cfa1ca9dSYoshinobu Inoue 
643fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
644cfa1ca9dSYoshinobu Inoue 		break;
645cfa1ca9dSYoshinobu Inoue 
646686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
647686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
648fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
649686cdd19SJun-ichiro itojun Hagino 		break;
650686cdd19SJun-ichiro itojun Hagino #endif
651686cdd19SJun-ichiro itojun Hagino 
652cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
653cfa1ca9dSYoshinobu Inoue #ifdef INET6
654cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
655cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
656cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
657cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
658cfa1ca9dSYoshinobu Inoue 			goto bad;
659cfa1ca9dSYoshinobu Inoue 		}
660cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
66133841545SHajimu UMEMOTO 		switch (cmd) {
662cfa1ca9dSYoshinobu Inoue #ifdef INET
66333841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
664cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
66533841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
666cfa1ca9dSYoshinobu Inoue 			break;
667cfa1ca9dSYoshinobu Inoue #endif /* INET */
668cfa1ca9dSYoshinobu Inoue #ifdef INET6
66933841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
670cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
671cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
67233841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
673cfa1ca9dSYoshinobu Inoue 			break;
674cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
675cfa1ca9dSYoshinobu Inoue 		default:
676cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
677cfa1ca9dSYoshinobu Inoue 			goto bad;
678cfa1ca9dSYoshinobu Inoue 		}
67933841545SHajimu UMEMOTO 		if (src->sa_len > size)
68033841545SHajimu UMEMOTO 			return EINVAL;
68133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
682cfa1ca9dSYoshinobu Inoue 		break;
683cfa1ca9dSYoshinobu Inoue 
684cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
685cfa1ca9dSYoshinobu Inoue #ifdef INET6
686cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
687cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
688cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
689cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
690cfa1ca9dSYoshinobu Inoue 			goto bad;
691cfa1ca9dSYoshinobu Inoue 		}
692cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
69333841545SHajimu UMEMOTO 		switch (cmd) {
694cfa1ca9dSYoshinobu Inoue #ifdef INET
69533841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
696cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
69733841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
698cfa1ca9dSYoshinobu Inoue 			break;
699cfa1ca9dSYoshinobu Inoue #endif /* INET */
700cfa1ca9dSYoshinobu Inoue #ifdef INET6
70133841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
702cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
703cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
70433841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
705cfa1ca9dSYoshinobu Inoue 			break;
706cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
707cfa1ca9dSYoshinobu Inoue 		default:
708cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
709cfa1ca9dSYoshinobu Inoue 			goto bad;
710cfa1ca9dSYoshinobu Inoue 		}
71133841545SHajimu UMEMOTO 		if (src->sa_len > size)
71233841545SHajimu UMEMOTO 			return EINVAL;
71333841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
71433841545SHajimu UMEMOTO 		break;
71533841545SHajimu UMEMOTO 
71633841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
71733841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
71833841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
71933841545SHajimu UMEMOTO 			goto bad;
72033841545SHajimu UMEMOTO 		}
72133841545SHajimu UMEMOTO 
72233841545SHajimu UMEMOTO 		/* copy src */
72333841545SHajimu UMEMOTO 		src = sc->gif_psrc;
72433841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
72533841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
72633841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
72733841545SHajimu UMEMOTO 		if (src->sa_len > size)
72833841545SHajimu UMEMOTO 			return EINVAL;
72933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
73033841545SHajimu UMEMOTO 
73133841545SHajimu UMEMOTO 		/* copy dst */
73233841545SHajimu UMEMOTO 		src = sc->gif_pdst;
73333841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
73433841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
73533841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
73633841545SHajimu UMEMOTO 		if (src->sa_len > size)
73733841545SHajimu UMEMOTO 			return EINVAL;
73833841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
739cfa1ca9dSYoshinobu Inoue 		break;
740cfa1ca9dSYoshinobu Inoue 
741cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
742686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
743cfa1ca9dSYoshinobu Inoue 		break;
744cfa1ca9dSYoshinobu Inoue 
745cfa1ca9dSYoshinobu Inoue 	default:
746cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
747cfa1ca9dSYoshinobu Inoue 		break;
748cfa1ca9dSYoshinobu Inoue 	}
749cfa1ca9dSYoshinobu Inoue  bad:
750cfa1ca9dSYoshinobu Inoue 	return error;
751cfa1ca9dSYoshinobu Inoue }
75253dab5feSBrooks Davis 
75317d5cb2dSRobert Watson /*
75417d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
75517d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
75617d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
75717d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
75817d5cb2dSRobert Watson  * a mutex.
75917d5cb2dSRobert Watson  */
7609426aedfSHajimu UMEMOTO int
7619426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
7629426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
7639426aedfSHajimu UMEMOTO 	struct sockaddr *src;
7649426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
76553dab5feSBrooks Davis {
766fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
7679426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
7689426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
7699426aedfSHajimu UMEMOTO 	int s;
7709426aedfSHajimu UMEMOTO 	int error = 0;
7719426aedfSHajimu UMEMOTO 
7729426aedfSHajimu UMEMOTO 	s = splnet();
7739426aedfSHajimu UMEMOTO 
77417d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
7759426aedfSHajimu UMEMOTO 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
7769426aedfSHajimu UMEMOTO 		if (sc2 == sc)
7779426aedfSHajimu UMEMOTO 			continue;
7789426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
7799426aedfSHajimu UMEMOTO 			continue;
7809426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
7819426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
7829426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
7839426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
7849426aedfSHajimu UMEMOTO 			continue;
7859426aedfSHajimu UMEMOTO 
7869426aedfSHajimu UMEMOTO 		/*
7879426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
7889426aedfSHajimu UMEMOTO 		 * otherwise.
7899426aedfSHajimu UMEMOTO 		 */
7909426aedfSHajimu UMEMOTO 		if (!parallel_tunnels &&
7919426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
7929426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
7939426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
79417d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
7959426aedfSHajimu UMEMOTO 			goto bad;
7969426aedfSHajimu UMEMOTO 		}
7979426aedfSHajimu UMEMOTO 
7989426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
7999426aedfSHajimu UMEMOTO 	}
80017d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
8019426aedfSHajimu UMEMOTO 
8029426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
8039426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
8049426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
8059426aedfSHajimu UMEMOTO #ifdef INET
8069426aedfSHajimu UMEMOTO 		case AF_INET:
8079426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
8089426aedfSHajimu UMEMOTO 			break;
8099426aedfSHajimu UMEMOTO #endif
8109426aedfSHajimu UMEMOTO #ifdef INET6
8119426aedfSHajimu UMEMOTO 		case AF_INET6:
8129426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
8139426aedfSHajimu UMEMOTO 			break;
8149426aedfSHajimu UMEMOTO #endif
8159426aedfSHajimu UMEMOTO 		}
8169426aedfSHajimu UMEMOTO 
8179426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
818a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
8199426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
8209426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
8219426aedfSHajimu UMEMOTO 
8229426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
823a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
8249426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
8259426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
8269426aedfSHajimu UMEMOTO 
8279426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
8289426aedfSHajimu UMEMOTO #ifdef INET
8299426aedfSHajimu UMEMOTO 	case AF_INET:
8309426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
8319426aedfSHajimu UMEMOTO 		break;
8329426aedfSHajimu UMEMOTO #endif
8339426aedfSHajimu UMEMOTO #ifdef INET6
8349426aedfSHajimu UMEMOTO 	case AF_INET6:
8359426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
8369426aedfSHajimu UMEMOTO 		break;
8379426aedfSHajimu UMEMOTO #endif
8389426aedfSHajimu UMEMOTO 	}
8399426aedfSHajimu UMEMOTO 	if (error) {
8409426aedfSHajimu UMEMOTO 		/* rollback */
8419426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
8429426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
8439426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
8449426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
8459426aedfSHajimu UMEMOTO 		goto bad;
8469426aedfSHajimu UMEMOTO 	}
8479426aedfSHajimu UMEMOTO 
8489426aedfSHajimu UMEMOTO 	if (osrc)
8499426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
8509426aedfSHajimu UMEMOTO 	if (odst)
8519426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
8529426aedfSHajimu UMEMOTO 
8539426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8549426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8559426aedfSHajimu UMEMOTO 	else
8569426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
8579426aedfSHajimu UMEMOTO 	splx(s);
8589426aedfSHajimu UMEMOTO 
8599426aedfSHajimu UMEMOTO 	return 0;
8609426aedfSHajimu UMEMOTO 
8619426aedfSHajimu UMEMOTO  bad:
8629426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8639426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8649426aedfSHajimu UMEMOTO 	else
8659426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
8669426aedfSHajimu UMEMOTO 	splx(s);
8679426aedfSHajimu UMEMOTO 
8689426aedfSHajimu UMEMOTO 	return error;
8699426aedfSHajimu UMEMOTO }
8709426aedfSHajimu UMEMOTO 
8719426aedfSHajimu UMEMOTO void
8729426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
8739426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8749426aedfSHajimu UMEMOTO {
875fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
8769426aedfSHajimu UMEMOTO 	int s;
8779426aedfSHajimu UMEMOTO 
8789426aedfSHajimu UMEMOTO 	s = splnet();
87953dab5feSBrooks Davis 
88053dab5feSBrooks Davis 	if (sc->gif_psrc) {
88153dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
88253dab5feSBrooks Davis 		sc->gif_psrc = NULL;
88353dab5feSBrooks Davis 	}
88453dab5feSBrooks Davis 	if (sc->gif_pdst) {
88553dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
88653dab5feSBrooks Davis 		sc->gif_pdst = NULL;
88753dab5feSBrooks Davis 	}
8889426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
8899426aedfSHajimu UMEMOTO #ifdef INET
8909426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
8919426aedfSHajimu UMEMOTO #endif
8929426aedfSHajimu UMEMOTO #ifdef INET6
8939426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
8949426aedfSHajimu UMEMOTO #endif
8959426aedfSHajimu UMEMOTO 
8969426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8979426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8989426aedfSHajimu UMEMOTO 	else
8999426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
9009426aedfSHajimu UMEMOTO 	splx(s);
90153dab5feSBrooks Davis }
902