xref: /freebsd/sys/net/if_gif.c (revision 437ffe182391fbcd9826ce327abbd175515b687d)
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 
4cfa1ca9dSYoshinobu Inoue /*
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>
43cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
46cfa1ca9dSYoshinobu Inoue #include <sys/time.h>
47872f786aSBrooks Davis #include <sys/sysctl.h>
48cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h>
49686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5053dab5feSBrooks Davis #include <sys/conf.h>
51cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
52cfa1ca9dSYoshinobu Inoue 
53cfa1ca9dSYoshinobu Inoue #include <net/if.h>
54cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
55cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
56cfa1ca9dSYoshinobu Inoue #include <net/route.h>
57cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
58cfa1ca9dSYoshinobu Inoue 
59cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
60cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
61cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
6233841545SHajimu UMEMOTO #ifdef	INET
6333841545SHajimu UMEMOTO #include <netinet/in_var.h>
64cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
6553dab5feSBrooks Davis #include <netinet/ip_var.h>
66cfa1ca9dSYoshinobu Inoue #endif	/* INET */
67cfa1ca9dSYoshinobu Inoue 
68cfa1ca9dSYoshinobu Inoue #ifdef INET6
69cfa1ca9dSYoshinobu Inoue #ifndef INET
70cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
71cfa1ca9dSYoshinobu Inoue #endif
72cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
73cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
74cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
75cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
76686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
77cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
78cfa1ca9dSYoshinobu Inoue 
79686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
80cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
81cfa1ca9dSYoshinobu Inoue 
82cfa1ca9dSYoshinobu Inoue #include <net/net_osdep.h>
83cfa1ca9dSYoshinobu Inoue 
8453dab5feSBrooks Davis #define GIFNAME		"gif"
85686cdd19SJun-ichiro itojun Hagino 
8653dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
87e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
8853dab5feSBrooks Davis 
8994408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
9094408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
9194408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
9294408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
9394408d94SBrooks Davis 
94929ddbbbSAlfred Perlstein int	gif_clone_create(struct if_clone *, int);
95ae5a19beSBrooks Davis void	gif_clone_destroy(struct ifnet *);
9653dab5feSBrooks Davis 
973b16e7b2SMaxime Henrion struct if_clone gif_cloner = IF_CLONE_INITIALIZER("gif",
98ae5a19beSBrooks Davis     gif_clone_create, gif_clone_destroy, 0, IF_MAXUNIT);
9953dab5feSBrooks Davis 
100929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
101cfa1ca9dSYoshinobu Inoue 
102872f786aSBrooks Davis SYSCTL_DECL(_net_link);
103872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
104872f786aSBrooks Davis     "Generic Tunnel Interface");
105686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
106686cdd19SJun-ichiro itojun Hagino /*
107872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
108686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
109686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
110686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
111686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
112686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
113686cdd19SJun-ichiro itojun Hagino  */
114686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
115686cdd19SJun-ichiro itojun Hagino #endif
116686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST;
117872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
118872f786aSBrooks Davis     &max_gif_nesting, 0, "Max nested tunnels");
119872f786aSBrooks Davis 
120872f786aSBrooks Davis /*
121872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
122872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
123872f786aSBrooks Davis  * we allow control over this check here.
124872f786aSBrooks Davis  */
125872f786aSBrooks Davis #ifdef XBONEHACK
126872f786aSBrooks Davis static int parallel_tunnels = 1;
127872f786aSBrooks Davis #else
128872f786aSBrooks Davis static int parallel_tunnels = 0;
129872f786aSBrooks Davis #endif
130872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
131872f786aSBrooks Davis     &parallel_tunnels, 0, "Allow parallel tunnels?");
132cfa1ca9dSYoshinobu Inoue 
13353dab5feSBrooks Davis int
13453dab5feSBrooks Davis gif_clone_create(ifc, unit)
13553dab5feSBrooks Davis 	struct if_clone *ifc;
1363b16e7b2SMaxime Henrion 	int unit;
137cfa1ca9dSYoshinobu Inoue {
13833841545SHajimu UMEMOTO 	struct gif_softc *sc;
139cfa1ca9dSYoshinobu Inoue 
140a163d034SWarner Losh 	sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
14153dab5feSBrooks Davis 	bzero(sc, sizeof(struct gif_softc));
14253dab5feSBrooks Davis 
14353dab5feSBrooks Davis 	sc->gif_if.if_softc = sc;
1449bf40edeSBrooks Davis 	if_initname(&sc->gif_if, ifc->ifc_name, unit);
145686cdd19SJun-ichiro itojun Hagino 
1469426aedfSHajimu UMEMOTO 	gifattach0(sc);
147686cdd19SJun-ichiro itojun Hagino 
1489426aedfSHajimu UMEMOTO 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
1499426aedfSHajimu UMEMOTO 	return (0);
1509426aedfSHajimu UMEMOTO }
1519426aedfSHajimu UMEMOTO 
1529426aedfSHajimu UMEMOTO void
1539426aedfSHajimu UMEMOTO gifattach0(sc)
1549426aedfSHajimu UMEMOTO 	struct gif_softc *sc;
1559426aedfSHajimu UMEMOTO {
1569426aedfSHajimu UMEMOTO 
1579426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1589426aedfSHajimu UMEMOTO 
1599426aedfSHajimu UMEMOTO 	sc->gif_if.if_addrlen = 0;
160cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_mtu    = GIF_MTU;
161cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
16233841545SHajimu UMEMOTO #if 0
16333841545SHajimu UMEMOTO 	/* turn off ingress filter */
16433841545SHajimu UMEMOTO 	sc->gif_if.if_flags  |= IFF_LINK2;
16533841545SHajimu UMEMOTO #endif
166cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_ioctl  = gif_ioctl;
167cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_output = gif_output;
168cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_type   = IFT_GIF;
169686cdd19SJun-ichiro itojun Hagino 	sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
170cfa1ca9dSYoshinobu Inoue 	if_attach(&sc->gif_if);
171cfa1ca9dSYoshinobu Inoue 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
17294408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
17394408d94SBrooks Davis 		(*ng_gif_attach_p)(&sc->gif_if);
174cfa1ca9dSYoshinobu Inoue }
175cfa1ca9dSYoshinobu Inoue 
176ae5a19beSBrooks Davis void
17753dab5feSBrooks Davis gif_clone_destroy(ifp)
17853dab5feSBrooks Davis 	struct ifnet *ifp;
17953dab5feSBrooks Davis {
18053dab5feSBrooks Davis 	int err;
18153dab5feSBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
18253dab5feSBrooks Davis 
1839426aedfSHajimu UMEMOTO 	gif_delete_tunnel(&sc->gif_if);
1849426aedfSHajimu UMEMOTO 	LIST_REMOVE(sc, gif_list);
1859426aedfSHajimu UMEMOTO #ifdef INET6
18653dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
18753dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
18853dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
18953dab5feSBrooks Davis 	}
1909426aedfSHajimu UMEMOTO #endif
1919426aedfSHajimu UMEMOTO #ifdef INET
1929426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
1939426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
1949426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
1959426aedfSHajimu UMEMOTO 	}
1969426aedfSHajimu UMEMOTO #endif
19753dab5feSBrooks Davis 
19894408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
19994408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
20053dab5feSBrooks Davis 	bpfdetach(ifp);
20153dab5feSBrooks Davis 	if_detach(ifp);
20253dab5feSBrooks Davis 
20353dab5feSBrooks Davis 	free(sc, M_GIF);
20453dab5feSBrooks Davis }
20553dab5feSBrooks Davis 
20653dab5feSBrooks Davis static int
20753dab5feSBrooks Davis gifmodevent(mod, type, data)
20853dab5feSBrooks Davis 	module_t mod;
20953dab5feSBrooks Davis 	int type;
21053dab5feSBrooks Davis 	void *data;
21153dab5feSBrooks Davis {
21253dab5feSBrooks Davis 
21353dab5feSBrooks Davis 	switch (type) {
21453dab5feSBrooks Davis 	case MOD_LOAD:
21520af0ffaSBrooks Davis 		LIST_INIT(&gif_softc_list);
21653dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
21753dab5feSBrooks Davis 
21853dab5feSBrooks Davis #ifdef INET6
21953dab5feSBrooks Davis 		ip6_gif_hlim = GIF_HLIM;
22053dab5feSBrooks Davis #endif
22153dab5feSBrooks Davis 
22253dab5feSBrooks Davis 		break;
22353dab5feSBrooks Davis 	case MOD_UNLOAD:
22453dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
22553dab5feSBrooks Davis 
22620af0ffaSBrooks Davis 		while (!LIST_EMPTY(&gif_softc_list))
22720af0ffaSBrooks Davis 			gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
22853dab5feSBrooks Davis 
22953dab5feSBrooks Davis #ifdef INET6
23053dab5feSBrooks Davis 		ip6_gif_hlim = 0;
23153dab5feSBrooks Davis #endif
23253dab5feSBrooks Davis 		break;
23353dab5feSBrooks Davis 	}
23453dab5feSBrooks Davis 	return 0;
23553dab5feSBrooks Davis }
23653dab5feSBrooks Davis 
23753dab5feSBrooks Davis static moduledata_t gif_mod = {
23853dab5feSBrooks Davis 	"if_gif",
23953dab5feSBrooks Davis 	gifmodevent,
24053dab5feSBrooks Davis 	0
24153dab5feSBrooks Davis };
24253dab5feSBrooks Davis 
24353dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
24420af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
245cfa1ca9dSYoshinobu Inoue 
2469426aedfSHajimu UMEMOTO int
247686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
248686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
249686cdd19SJun-ichiro itojun Hagino 	int off;
250686cdd19SJun-ichiro itojun Hagino 	int proto;
251686cdd19SJun-ichiro itojun Hagino 	void *arg;
252686cdd19SJun-ichiro itojun Hagino {
253686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
254686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
255686cdd19SJun-ichiro itojun Hagino 
256686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
257686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
258686cdd19SJun-ichiro itojun Hagino 		return 0;
259686cdd19SJun-ichiro itojun Hagino 
260686cdd19SJun-ichiro itojun Hagino 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
261686cdd19SJun-ichiro itojun Hagino 		return 0;
262686cdd19SJun-ichiro itojun Hagino 
263686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
264686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
265686cdd19SJun-ichiro itojun Hagino 		return 0;
266686cdd19SJun-ichiro itojun Hagino 
267686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
268686cdd19SJun-ichiro itojun Hagino #ifdef INET
269686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
270686cdd19SJun-ichiro itojun Hagino 		break;
271686cdd19SJun-ichiro itojun Hagino #endif
272686cdd19SJun-ichiro itojun Hagino #ifdef INET6
273686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
274686cdd19SJun-ichiro itojun Hagino 		break;
275686cdd19SJun-ichiro itojun Hagino #endif
276686cdd19SJun-ichiro itojun Hagino 	default:
277686cdd19SJun-ichiro itojun Hagino 		return 0;
278686cdd19SJun-ichiro itojun Hagino 	}
279686cdd19SJun-ichiro itojun Hagino 
2803bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
2813bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
2823bb61ca6SHajimu UMEMOTO 		return 0;
2833bb61ca6SHajimu UMEMOTO 
2846f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
285686cdd19SJun-ichiro itojun Hagino 
286686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
287686cdd19SJun-ichiro itojun Hagino #ifdef INET
288686cdd19SJun-ichiro itojun Hagino 	case 4:
289686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
290686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
291686cdd19SJun-ichiro itojun Hagino 			return 0;
292686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
293686cdd19SJun-ichiro itojun Hagino #endif
294686cdd19SJun-ichiro itojun Hagino #ifdef INET6
295686cdd19SJun-ichiro itojun Hagino 	case 6:
2969426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
2979426aedfSHajimu UMEMOTO 			return 0;
298686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
299686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
300686cdd19SJun-ichiro itojun Hagino 			return 0;
301686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
302686cdd19SJun-ichiro itojun Hagino #endif
303686cdd19SJun-ichiro itojun Hagino 	default:
304686cdd19SJun-ichiro itojun Hagino 		return 0;
305686cdd19SJun-ichiro itojun Hagino 	}
306686cdd19SJun-ichiro itojun Hagino }
307686cdd19SJun-ichiro itojun Hagino 
308cfa1ca9dSYoshinobu Inoue int
309cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
310cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
311cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
312cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
313cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
314cfa1ca9dSYoshinobu Inoue {
31533841545SHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc*)ifp;
316cfa1ca9dSYoshinobu Inoue 	int error = 0;
317f0133454SMaxim Sobolev 	static int called = 0;	/* XXX: MUTEX */
318cfa1ca9dSYoshinobu Inoue 
31910722b85SRobert Watson #ifdef MAC
32010722b85SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
321e0852ce2SRobert Watson 	if (error) {
322e0852ce2SRobert Watson 		m_freem(m);
323e0852ce2SRobert Watson 		goto end;
324e0852ce2SRobert Watson 	}
32510722b85SRobert Watson #endif
32610722b85SRobert Watson 
327cfa1ca9dSYoshinobu Inoue 	/*
328cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
329cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
330f0133454SMaxim Sobolev 	 * XXX: this mechanism may introduce another problem about
331f0133454SMaxim Sobolev 	 *      mutual exclusion of the variable CALLED, especially if we
332f0133454SMaxim Sobolev 	 *      use kernel thread.
333cfa1ca9dSYoshinobu Inoue 	 */
334f0133454SMaxim Sobolev 	if (++called > max_gif_nesting) {
335cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
336cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
337f0133454SMaxim Sobolev 		    called);
338cfa1ca9dSYoshinobu Inoue 		m_freem(m);
339cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
340cfa1ca9dSYoshinobu Inoue 		goto end;
341cfa1ca9dSYoshinobu Inoue 	}
342686cdd19SJun-ichiro itojun Hagino 
343cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
344cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
345cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
346cfa1ca9dSYoshinobu Inoue 		m_freem(m);
347cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
348cfa1ca9dSYoshinobu Inoue 		goto end;
349cfa1ca9dSYoshinobu Inoue 	}
350cfa1ca9dSYoshinobu Inoue 
351cfa1ca9dSYoshinobu Inoue 	if (ifp->if_bpf) {
35233841545SHajimu UMEMOTO 		u_int32_t af = dst->sa_family;
353437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
354cfa1ca9dSYoshinobu Inoue 	}
355cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
356cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
357cfa1ca9dSYoshinobu Inoue 
35833841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
35933841545SHajimu UMEMOTO 
360686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
361686cdd19SJun-ichiro itojun Hagino 
36233841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
363cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
364cfa1ca9dSYoshinobu Inoue #ifdef INET
365cfa1ca9dSYoshinobu Inoue 	case AF_INET:
366b6e28453SHajimu UMEMOTO 		error = in_gif_output(ifp, dst->sa_family, m);
367cfa1ca9dSYoshinobu Inoue 		break;
368cfa1ca9dSYoshinobu Inoue #endif
369cfa1ca9dSYoshinobu Inoue #ifdef INET6
370cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
371b6e28453SHajimu UMEMOTO 		error = in6_gif_output(ifp, dst->sa_family, m);
372cfa1ca9dSYoshinobu Inoue 		break;
373cfa1ca9dSYoshinobu Inoue #endif
374cfa1ca9dSYoshinobu Inoue 	default:
375cfa1ca9dSYoshinobu Inoue 		m_freem(m);
376cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
37733841545SHajimu UMEMOTO 		goto end;
378cfa1ca9dSYoshinobu Inoue 	}
379cfa1ca9dSYoshinobu Inoue 
380cfa1ca9dSYoshinobu Inoue   end:
381f0133454SMaxim Sobolev 	called = 0;		/* reset recursion counter */
38233841545SHajimu UMEMOTO 	if (error)
38333841545SHajimu UMEMOTO 		ifp->if_oerrors++;
384cfa1ca9dSYoshinobu Inoue 	return error;
385cfa1ca9dSYoshinobu Inoue }
386cfa1ca9dSYoshinobu Inoue 
387cfa1ca9dSYoshinobu Inoue void
38821fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
389cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
390cfa1ca9dSYoshinobu Inoue 	int af;
39121fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
392cfa1ca9dSYoshinobu Inoue {
393df5e1987SJonathan Lemon 	int isr;
394cfa1ca9dSYoshinobu Inoue 
39521fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
396cfa1ca9dSYoshinobu Inoue 		/* just in case */
397cfa1ca9dSYoshinobu Inoue 		m_freem(m);
398cfa1ca9dSYoshinobu Inoue 		return;
399cfa1ca9dSYoshinobu Inoue 	}
400cfa1ca9dSYoshinobu Inoue 
40121fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
402cfa1ca9dSYoshinobu Inoue 
40310722b85SRobert Watson #ifdef MAC
40421fb391fSHajimu UMEMOTO 	mac_create_mbuf_from_ifnet(ifp, m);
40510722b85SRobert Watson #endif
40610722b85SRobert Watson 
40721fb391fSHajimu UMEMOTO 	if (ifp->if_bpf) {
40833841545SHajimu UMEMOTO 		u_int32_t af1 = af;
409437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
410cfa1ca9dSYoshinobu Inoue 	}
411cfa1ca9dSYoshinobu Inoue 
41294408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
41321fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
41494408d94SBrooks Davis 		if (m == NULL)
41594408d94SBrooks Davis 			return;
41694408d94SBrooks Davis 	}
41794408d94SBrooks Davis 
418cfa1ca9dSYoshinobu Inoue 	/*
419cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
420cfa1ca9dSYoshinobu Inoue 	 * specified address family.
421cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
422cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
423cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
424cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
425cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
42688ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
42788ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
428cfa1ca9dSYoshinobu Inoue 	 */
429cfa1ca9dSYoshinobu Inoue 	switch (af) {
430cfa1ca9dSYoshinobu Inoue #ifdef INET
431cfa1ca9dSYoshinobu Inoue 	case AF_INET:
432cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
433cfa1ca9dSYoshinobu Inoue 		break;
434cfa1ca9dSYoshinobu Inoue #endif
435cfa1ca9dSYoshinobu Inoue #ifdef INET6
436cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
437cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
438cfa1ca9dSYoshinobu Inoue 		break;
439cfa1ca9dSYoshinobu Inoue #endif
440cfa1ca9dSYoshinobu Inoue 	default:
44194408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
44221fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
44394408d94SBrooks Davis 		else
444cfa1ca9dSYoshinobu Inoue 			m_freem(m);
445cfa1ca9dSYoshinobu Inoue 		return;
446cfa1ca9dSYoshinobu Inoue 	}
447cfa1ca9dSYoshinobu Inoue 
44821fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
44921fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
4501cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
451cfa1ca9dSYoshinobu Inoue }
452cfa1ca9dSYoshinobu Inoue 
453686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
454cfa1ca9dSYoshinobu Inoue int
455cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
456cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
457cfa1ca9dSYoshinobu Inoue 	u_long cmd;
458cfa1ca9dSYoshinobu Inoue 	caddr_t data;
459cfa1ca9dSYoshinobu Inoue {
460cfa1ca9dSYoshinobu Inoue 	struct gif_softc *sc  = (struct gif_softc*)ifp;
461cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
462cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
463686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
4643bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
4653bb61ca6SHajimu UMEMOTO 	u_long mtu;
4663bb61ca6SHajimu UMEMOTO #endif
467cfa1ca9dSYoshinobu Inoue 
468cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
469cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
4709426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
471cfa1ca9dSYoshinobu Inoue 		break;
472cfa1ca9dSYoshinobu Inoue 
473cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
474cfa1ca9dSYoshinobu Inoue 		break;
475cfa1ca9dSYoshinobu Inoue 
476cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
477cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
478cfa1ca9dSYoshinobu Inoue 		break;
479cfa1ca9dSYoshinobu Inoue 
480686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
481cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
482cfa1ca9dSYoshinobu Inoue 		break;
483686cdd19SJun-ichiro itojun Hagino 
484cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
485cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
4863bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
487cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
488cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
489cfa1ca9dSYoshinobu Inoue 		break;
490686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
491cfa1ca9dSYoshinobu Inoue 
4923bb61ca6SHajimu UMEMOTO #ifdef INET
493cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
4943bb61ca6SHajimu UMEMOTO #endif
495cfa1ca9dSYoshinobu Inoue #ifdef INET6
496cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
497cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
49833841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
499686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
50033841545SHajimu UMEMOTO #ifdef INET
501686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
502cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
503cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
504cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
505cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
506cfa1ca9dSYoshinobu Inoue 			break;
50733841545SHajimu UMEMOTO #endif
508cfa1ca9dSYoshinobu Inoue #ifdef INET6
509686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
510cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
511cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
512cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
513cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
514686cdd19SJun-ichiro itojun Hagino 			break;
515686cdd19SJun-ichiro itojun Hagino #endif
51633841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
51733841545SHajimu UMEMOTO 			src = (struct sockaddr *)
51833841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
51933841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
52033841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
5219426aedfSHajimu UMEMOTO 			break;
5226f4ded3aSBrooks Davis 		default:
5239426aedfSHajimu UMEMOTO 			return EINVAL;
52433841545SHajimu UMEMOTO 		}
52533841545SHajimu UMEMOTO 
52633841545SHajimu UMEMOTO 		/* sa_family must be equal */
52733841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
52833841545SHajimu UMEMOTO 			return EINVAL;
52933841545SHajimu UMEMOTO 
53033841545SHajimu UMEMOTO 		/* validate sa_len */
53133841545SHajimu UMEMOTO 		switch (src->sa_family) {
53233841545SHajimu UMEMOTO #ifdef INET
53333841545SHajimu UMEMOTO 		case AF_INET:
53433841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
53533841545SHajimu UMEMOTO 				return EINVAL;
53633841545SHajimu UMEMOTO 			break;
53733841545SHajimu UMEMOTO #endif
53833841545SHajimu UMEMOTO #ifdef INET6
53933841545SHajimu UMEMOTO 		case AF_INET6:
54033841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
54133841545SHajimu UMEMOTO 				return EINVAL;
54233841545SHajimu UMEMOTO 			break;
54333841545SHajimu UMEMOTO #endif
54433841545SHajimu UMEMOTO 		default:
54533841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
54633841545SHajimu UMEMOTO 		}
54733841545SHajimu UMEMOTO 		switch (dst->sa_family) {
54833841545SHajimu UMEMOTO #ifdef INET
54933841545SHajimu UMEMOTO 		case AF_INET:
55033841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
55133841545SHajimu UMEMOTO 				return EINVAL;
55233841545SHajimu UMEMOTO 			break;
55333841545SHajimu UMEMOTO #endif
55433841545SHajimu UMEMOTO #ifdef INET6
55533841545SHajimu UMEMOTO 		case AF_INET6:
55633841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
55733841545SHajimu UMEMOTO 				return EINVAL;
55833841545SHajimu UMEMOTO 			break;
55933841545SHajimu UMEMOTO #endif
56033841545SHajimu UMEMOTO 		default:
56133841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
56233841545SHajimu UMEMOTO 		}
56333841545SHajimu UMEMOTO 
56433841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
56533841545SHajimu UMEMOTO 		switch (cmd) {
56633841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
56733841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
56833841545SHajimu UMEMOTO 				break;
56933841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
57033841545SHajimu UMEMOTO #ifdef INET6
57133841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
57233841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
57333841545SHajimu UMEMOTO 				break;
57433841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
57533841545SHajimu UMEMOTO #endif /* INET6 */
57633841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
57733841545SHajimu UMEMOTO 			/* checks done in the above */
57833841545SHajimu UMEMOTO 			break;
579686cdd19SJun-ichiro itojun Hagino 		}
580cfa1ca9dSYoshinobu Inoue 
5819426aedfSHajimu UMEMOTO 		error = gif_set_tunnel(&sc->gif_if, src, dst);
582cfa1ca9dSYoshinobu Inoue 		break;
583cfa1ca9dSYoshinobu Inoue 
584686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
585686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
5869426aedfSHajimu UMEMOTO 		gif_delete_tunnel(&sc->gif_if);
587686cdd19SJun-ichiro itojun Hagino 		break;
588686cdd19SJun-ichiro itojun Hagino #endif
589686cdd19SJun-ichiro itojun Hagino 
590cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
591cfa1ca9dSYoshinobu Inoue #ifdef INET6
592cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
593cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
594cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
595cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
596cfa1ca9dSYoshinobu Inoue 			goto bad;
597cfa1ca9dSYoshinobu Inoue 		}
598cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
59933841545SHajimu UMEMOTO 		switch (cmd) {
600cfa1ca9dSYoshinobu Inoue #ifdef INET
60133841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
602cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
60333841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
604cfa1ca9dSYoshinobu Inoue 			break;
605cfa1ca9dSYoshinobu Inoue #endif /* INET */
606cfa1ca9dSYoshinobu Inoue #ifdef INET6
60733841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
608cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
609cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
61033841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
611cfa1ca9dSYoshinobu Inoue 			break;
612cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
613cfa1ca9dSYoshinobu Inoue 		default:
614cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
615cfa1ca9dSYoshinobu Inoue 			goto bad;
616cfa1ca9dSYoshinobu Inoue 		}
61733841545SHajimu UMEMOTO 		if (src->sa_len > size)
61833841545SHajimu UMEMOTO 			return EINVAL;
61933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
620cfa1ca9dSYoshinobu Inoue 		break;
621cfa1ca9dSYoshinobu Inoue 
622cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
623cfa1ca9dSYoshinobu Inoue #ifdef INET6
624cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
625cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
626cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
627cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
628cfa1ca9dSYoshinobu Inoue 			goto bad;
629cfa1ca9dSYoshinobu Inoue 		}
630cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
63133841545SHajimu UMEMOTO 		switch (cmd) {
632cfa1ca9dSYoshinobu Inoue #ifdef INET
63333841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
634cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
63533841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
636cfa1ca9dSYoshinobu Inoue 			break;
637cfa1ca9dSYoshinobu Inoue #endif /* INET */
638cfa1ca9dSYoshinobu Inoue #ifdef INET6
63933841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
640cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
641cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
64233841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
643cfa1ca9dSYoshinobu Inoue 			break;
644cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
645cfa1ca9dSYoshinobu Inoue 		default:
646cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
647cfa1ca9dSYoshinobu Inoue 			goto bad;
648cfa1ca9dSYoshinobu Inoue 		}
64933841545SHajimu UMEMOTO 		if (src->sa_len > size)
65033841545SHajimu UMEMOTO 			return EINVAL;
65133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
65233841545SHajimu UMEMOTO 		break;
65333841545SHajimu UMEMOTO 
65433841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
65533841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
65633841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
65733841545SHajimu UMEMOTO 			goto bad;
65833841545SHajimu UMEMOTO 		}
65933841545SHajimu UMEMOTO 
66033841545SHajimu UMEMOTO 		/* copy src */
66133841545SHajimu UMEMOTO 		src = sc->gif_psrc;
66233841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
66333841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
66433841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
66533841545SHajimu UMEMOTO 		if (src->sa_len > size)
66633841545SHajimu UMEMOTO 			return EINVAL;
66733841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
66833841545SHajimu UMEMOTO 
66933841545SHajimu UMEMOTO 		/* copy dst */
67033841545SHajimu UMEMOTO 		src = sc->gif_pdst;
67133841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
67233841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
67333841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
67433841545SHajimu UMEMOTO 		if (src->sa_len > size)
67533841545SHajimu UMEMOTO 			return EINVAL;
67633841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
677cfa1ca9dSYoshinobu Inoue 		break;
678cfa1ca9dSYoshinobu Inoue 
679cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
680686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
681cfa1ca9dSYoshinobu Inoue 		break;
682cfa1ca9dSYoshinobu Inoue 
683cfa1ca9dSYoshinobu Inoue 	default:
684cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
685cfa1ca9dSYoshinobu Inoue 		break;
686cfa1ca9dSYoshinobu Inoue 	}
687cfa1ca9dSYoshinobu Inoue  bad:
688cfa1ca9dSYoshinobu Inoue 	return error;
689cfa1ca9dSYoshinobu Inoue }
69053dab5feSBrooks Davis 
6919426aedfSHajimu UMEMOTO int
6929426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
6939426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
6949426aedfSHajimu UMEMOTO 	struct sockaddr *src;
6959426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
69653dab5feSBrooks Davis {
6979426aedfSHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc *)ifp;
6989426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
6999426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
7009426aedfSHajimu UMEMOTO 	int s;
7019426aedfSHajimu UMEMOTO 	int error = 0;
7029426aedfSHajimu UMEMOTO 
7039426aedfSHajimu UMEMOTO 	s = splnet();
7049426aedfSHajimu UMEMOTO 
7059426aedfSHajimu UMEMOTO 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
7069426aedfSHajimu UMEMOTO 		if (sc2 == sc)
7079426aedfSHajimu UMEMOTO 			continue;
7089426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
7099426aedfSHajimu UMEMOTO 			continue;
7109426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
7119426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
7129426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
7139426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
7149426aedfSHajimu UMEMOTO 			continue;
7159426aedfSHajimu UMEMOTO 
7169426aedfSHajimu UMEMOTO 		/*
7179426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
7189426aedfSHajimu UMEMOTO 		 * otherwise.
7199426aedfSHajimu UMEMOTO 		 */
7209426aedfSHajimu UMEMOTO 		if (!parallel_tunnels &&
7219426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
7229426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
7239426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
7249426aedfSHajimu UMEMOTO 			goto bad;
7259426aedfSHajimu UMEMOTO 		}
7269426aedfSHajimu UMEMOTO 
7279426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
7289426aedfSHajimu UMEMOTO 	}
7299426aedfSHajimu UMEMOTO 
7309426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
7319426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
7329426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
7339426aedfSHajimu UMEMOTO #ifdef INET
7349426aedfSHajimu UMEMOTO 		case AF_INET:
7359426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
7369426aedfSHajimu UMEMOTO 			break;
7379426aedfSHajimu UMEMOTO #endif
7389426aedfSHajimu UMEMOTO #ifdef INET6
7399426aedfSHajimu UMEMOTO 		case AF_INET6:
7409426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
7419426aedfSHajimu UMEMOTO 			break;
7429426aedfSHajimu UMEMOTO #endif
7439426aedfSHajimu UMEMOTO 		}
7449426aedfSHajimu UMEMOTO 
7459426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
746a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
7479426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
7489426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
7499426aedfSHajimu UMEMOTO 
7509426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
751a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
7529426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
7539426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
7549426aedfSHajimu UMEMOTO 
7559426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
7569426aedfSHajimu UMEMOTO #ifdef INET
7579426aedfSHajimu UMEMOTO 	case AF_INET:
7589426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
7599426aedfSHajimu UMEMOTO 		break;
7609426aedfSHajimu UMEMOTO #endif
7619426aedfSHajimu UMEMOTO #ifdef INET6
7629426aedfSHajimu UMEMOTO 	case AF_INET6:
7639426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
7649426aedfSHajimu UMEMOTO 		break;
7659426aedfSHajimu UMEMOTO #endif
7669426aedfSHajimu UMEMOTO 	}
7679426aedfSHajimu UMEMOTO 	if (error) {
7689426aedfSHajimu UMEMOTO 		/* rollback */
7699426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
7709426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
7719426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
7729426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
7739426aedfSHajimu UMEMOTO 		goto bad;
7749426aedfSHajimu UMEMOTO 	}
7759426aedfSHajimu UMEMOTO 
7769426aedfSHajimu UMEMOTO 	if (osrc)
7779426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
7789426aedfSHajimu UMEMOTO 	if (odst)
7799426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
7809426aedfSHajimu UMEMOTO 
7819426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
7829426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
7839426aedfSHajimu UMEMOTO 	else
7849426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
7859426aedfSHajimu UMEMOTO 	splx(s);
7869426aedfSHajimu UMEMOTO 
7879426aedfSHajimu UMEMOTO 	return 0;
7889426aedfSHajimu UMEMOTO 
7899426aedfSHajimu UMEMOTO  bad:
7909426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
7919426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
7929426aedfSHajimu UMEMOTO 	else
7939426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
7949426aedfSHajimu UMEMOTO 	splx(s);
7959426aedfSHajimu UMEMOTO 
7969426aedfSHajimu UMEMOTO 	return error;
7979426aedfSHajimu UMEMOTO }
7989426aedfSHajimu UMEMOTO 
7999426aedfSHajimu UMEMOTO void
8009426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
8019426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8029426aedfSHajimu UMEMOTO {
8039426aedfSHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc *)ifp;
8049426aedfSHajimu UMEMOTO 	int s;
8059426aedfSHajimu UMEMOTO 
8069426aedfSHajimu UMEMOTO 	s = splnet();
80753dab5feSBrooks Davis 
80853dab5feSBrooks Davis 	if (sc->gif_psrc) {
80953dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
81053dab5feSBrooks Davis 		sc->gif_psrc = NULL;
81153dab5feSBrooks Davis 	}
81253dab5feSBrooks Davis 	if (sc->gif_pdst) {
81353dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
81453dab5feSBrooks Davis 		sc->gif_pdst = NULL;
81553dab5feSBrooks Davis 	}
8169426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
8179426aedfSHajimu UMEMOTO #ifdef INET
8189426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
8199426aedfSHajimu UMEMOTO #endif
8209426aedfSHajimu UMEMOTO #ifdef INET6
8219426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
8229426aedfSHajimu UMEMOTO #endif
8239426aedfSHajimu UMEMOTO 
8249426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8259426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8269426aedfSHajimu UMEMOTO 	else
8279426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
8289426aedfSHajimu UMEMOTO 	splx(s);
82953dab5feSBrooks Davis }
830