xref: /freebsd/sys/net/if_gif.c (revision 9426aedf7fc8385964f246f023f42295880f7b46)
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 
14053dab5feSBrooks Davis 	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;
14453dab5feSBrooks Davis 	sc->gif_if.if_name = GIFNAME;
1453b16e7b2SMaxime Henrion 	sc->gif_if.if_unit = unit;
146686cdd19SJun-ichiro itojun Hagino 
1479426aedfSHajimu UMEMOTO 	gifattach0(sc);
148686cdd19SJun-ichiro itojun Hagino 
1499426aedfSHajimu UMEMOTO 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
1509426aedfSHajimu UMEMOTO 	return (0);
1519426aedfSHajimu UMEMOTO }
1529426aedfSHajimu UMEMOTO 
1539426aedfSHajimu UMEMOTO void
1549426aedfSHajimu UMEMOTO gifattach0(sc)
1559426aedfSHajimu UMEMOTO 	struct gif_softc *sc;
1569426aedfSHajimu UMEMOTO {
1579426aedfSHajimu UMEMOTO 
1589426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1599426aedfSHajimu UMEMOTO 
1609426aedfSHajimu UMEMOTO 	sc->gif_if.if_addrlen = 0;
161cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_mtu    = GIF_MTU;
162cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
16333841545SHajimu UMEMOTO #if 0
16433841545SHajimu UMEMOTO 	/* turn off ingress filter */
16533841545SHajimu UMEMOTO 	sc->gif_if.if_flags  |= IFF_LINK2;
16633841545SHajimu UMEMOTO #endif
167cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_ioctl  = gif_ioctl;
168cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_output = gif_output;
169cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_type   = IFT_GIF;
170686cdd19SJun-ichiro itojun Hagino 	sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
171cfa1ca9dSYoshinobu Inoue 	if_attach(&sc->gif_if);
172cfa1ca9dSYoshinobu Inoue 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
17394408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
17494408d94SBrooks Davis 		(*ng_gif_attach_p)(&sc->gif_if);
175cfa1ca9dSYoshinobu Inoue }
176cfa1ca9dSYoshinobu Inoue 
177ae5a19beSBrooks Davis void
17853dab5feSBrooks Davis gif_clone_destroy(ifp)
17953dab5feSBrooks Davis 	struct ifnet *ifp;
18053dab5feSBrooks Davis {
18153dab5feSBrooks Davis 	int err;
18253dab5feSBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
18353dab5feSBrooks Davis 
1849426aedfSHajimu UMEMOTO 	gif_delete_tunnel(&sc->gif_if);
1859426aedfSHajimu UMEMOTO 	LIST_REMOVE(sc, gif_list);
1869426aedfSHajimu UMEMOTO #ifdef INET6
18753dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
18853dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
18953dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
19053dab5feSBrooks Davis 	}
1919426aedfSHajimu UMEMOTO #endif
1929426aedfSHajimu UMEMOTO #ifdef INET
1939426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
1949426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
1959426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
1969426aedfSHajimu UMEMOTO 	}
1979426aedfSHajimu UMEMOTO #endif
19853dab5feSBrooks Davis 
19994408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
20094408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
20153dab5feSBrooks Davis 	bpfdetach(ifp);
20253dab5feSBrooks Davis 	if_detach(ifp);
20353dab5feSBrooks Davis 
20453dab5feSBrooks Davis 	free(sc, M_GIF);
20553dab5feSBrooks Davis }
20653dab5feSBrooks Davis 
20753dab5feSBrooks Davis static int
20853dab5feSBrooks Davis gifmodevent(mod, type, data)
20953dab5feSBrooks Davis 	module_t mod;
21053dab5feSBrooks Davis 	int type;
21153dab5feSBrooks Davis 	void *data;
21253dab5feSBrooks Davis {
21353dab5feSBrooks Davis 
21453dab5feSBrooks Davis 	switch (type) {
21553dab5feSBrooks Davis 	case MOD_LOAD:
21620af0ffaSBrooks Davis 		LIST_INIT(&gif_softc_list);
21753dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
21853dab5feSBrooks Davis 
21953dab5feSBrooks Davis #ifdef INET6
22053dab5feSBrooks Davis 		ip6_gif_hlim = GIF_HLIM;
22153dab5feSBrooks Davis #endif
22253dab5feSBrooks Davis 
22353dab5feSBrooks Davis 		break;
22453dab5feSBrooks Davis 	case MOD_UNLOAD:
22553dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
22653dab5feSBrooks Davis 
22720af0ffaSBrooks Davis 		while (!LIST_EMPTY(&gif_softc_list))
22820af0ffaSBrooks Davis 			gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
22953dab5feSBrooks Davis 
23053dab5feSBrooks Davis #ifdef INET6
23153dab5feSBrooks Davis 		ip6_gif_hlim = 0;
23253dab5feSBrooks Davis #endif
23353dab5feSBrooks Davis 		break;
23453dab5feSBrooks Davis 	}
23553dab5feSBrooks Davis 	return 0;
23653dab5feSBrooks Davis }
23753dab5feSBrooks Davis 
23853dab5feSBrooks Davis static moduledata_t gif_mod = {
23953dab5feSBrooks Davis 	"if_gif",
24053dab5feSBrooks Davis 	gifmodevent,
24153dab5feSBrooks Davis 	0
24253dab5feSBrooks Davis };
24353dab5feSBrooks Davis 
24453dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
24520af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
246cfa1ca9dSYoshinobu Inoue 
2479426aedfSHajimu UMEMOTO int
248686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
249686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
250686cdd19SJun-ichiro itojun Hagino 	int off;
251686cdd19SJun-ichiro itojun Hagino 	int proto;
252686cdd19SJun-ichiro itojun Hagino 	void *arg;
253686cdd19SJun-ichiro itojun Hagino {
254686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
255686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
256686cdd19SJun-ichiro itojun Hagino 
257686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
258686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
259686cdd19SJun-ichiro itojun Hagino 		return 0;
260686cdd19SJun-ichiro itojun Hagino 
261686cdd19SJun-ichiro itojun Hagino 	if ((sc->gif_if.if_flags & IFF_UP) == 0)
262686cdd19SJun-ichiro itojun Hagino 		return 0;
263686cdd19SJun-ichiro itojun Hagino 
264686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
265686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
266686cdd19SJun-ichiro itojun Hagino 		return 0;
267686cdd19SJun-ichiro itojun Hagino 
268686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
269686cdd19SJun-ichiro itojun Hagino #ifdef INET
270686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
271686cdd19SJun-ichiro itojun Hagino 		break;
272686cdd19SJun-ichiro itojun Hagino #endif
273686cdd19SJun-ichiro itojun Hagino #ifdef INET6
274686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
275686cdd19SJun-ichiro itojun Hagino 		break;
276686cdd19SJun-ichiro itojun Hagino #endif
277686cdd19SJun-ichiro itojun Hagino 	default:
278686cdd19SJun-ichiro itojun Hagino 		return 0;
279686cdd19SJun-ichiro itojun Hagino 	}
280686cdd19SJun-ichiro itojun Hagino 
2816f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
282686cdd19SJun-ichiro itojun Hagino 
283686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
284686cdd19SJun-ichiro itojun Hagino #ifdef INET
285686cdd19SJun-ichiro itojun Hagino 	case 4:
286686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
287686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
288686cdd19SJun-ichiro itojun Hagino 			return 0;
289686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
290686cdd19SJun-ichiro itojun Hagino #endif
291686cdd19SJun-ichiro itojun Hagino #ifdef INET6
292686cdd19SJun-ichiro itojun Hagino 	case 6:
2939426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
2949426aedfSHajimu UMEMOTO 			return 0;
295686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
296686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
297686cdd19SJun-ichiro itojun Hagino 			return 0;
298686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
299686cdd19SJun-ichiro itojun Hagino #endif
300686cdd19SJun-ichiro itojun Hagino 	default:
301686cdd19SJun-ichiro itojun Hagino 		return 0;
302686cdd19SJun-ichiro itojun Hagino 	}
303686cdd19SJun-ichiro itojun Hagino }
304686cdd19SJun-ichiro itojun Hagino 
305cfa1ca9dSYoshinobu Inoue int
306cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
307cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
308cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
309cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
310cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
311cfa1ca9dSYoshinobu Inoue {
31233841545SHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc*)ifp;
313cfa1ca9dSYoshinobu Inoue 	int error = 0;
314f0133454SMaxim Sobolev 	static int called = 0;	/* XXX: MUTEX */
315cfa1ca9dSYoshinobu Inoue 
31610722b85SRobert Watson #ifdef MAC
31710722b85SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
318e0852ce2SRobert Watson 	if (error) {
319e0852ce2SRobert Watson 		m_freem(m);
320e0852ce2SRobert Watson 		goto end;
321e0852ce2SRobert Watson 	}
32210722b85SRobert Watson #endif
32310722b85SRobert Watson 
324cfa1ca9dSYoshinobu Inoue 	/*
325cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
326cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
327f0133454SMaxim Sobolev 	 * XXX: this mechanism may introduce another problem about
328f0133454SMaxim Sobolev 	 *      mutual exclusion of the variable CALLED, especially if we
329f0133454SMaxim Sobolev 	 *      use kernel thread.
330cfa1ca9dSYoshinobu Inoue 	 */
331f0133454SMaxim Sobolev 	if (++called > max_gif_nesting) {
332cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
333cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
334f0133454SMaxim Sobolev 		    called);
335cfa1ca9dSYoshinobu Inoue 		m_freem(m);
336cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
337cfa1ca9dSYoshinobu Inoue 		goto end;
338cfa1ca9dSYoshinobu Inoue 	}
339686cdd19SJun-ichiro itojun Hagino 
340cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
341cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
342cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
343cfa1ca9dSYoshinobu Inoue 		m_freem(m);
344cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
345cfa1ca9dSYoshinobu Inoue 		goto end;
346cfa1ca9dSYoshinobu Inoue 	}
347cfa1ca9dSYoshinobu Inoue 
348cfa1ca9dSYoshinobu Inoue 	if (ifp->if_bpf) {
349cfa1ca9dSYoshinobu Inoue 		/*
350cfa1ca9dSYoshinobu Inoue 		 * We need to prepend the address family as
351cfa1ca9dSYoshinobu Inoue 		 * a four byte field.  Cons up a dummy header
352cfa1ca9dSYoshinobu Inoue 		 * to pacify bpf.  This is safe because bpf
353cfa1ca9dSYoshinobu Inoue 		 * will only read from the mbuf (i.e., it won't
354cfa1ca9dSYoshinobu Inoue 		 * try to free it or keep a pointer a to it).
355cfa1ca9dSYoshinobu Inoue 		 */
356cfa1ca9dSYoshinobu Inoue 		struct mbuf m0;
35733841545SHajimu UMEMOTO 		u_int32_t af = dst->sa_family;
358cfa1ca9dSYoshinobu Inoue 
359cfa1ca9dSYoshinobu Inoue 		m0.m_next = m;
360cfa1ca9dSYoshinobu Inoue 		m0.m_len = 4;
361cfa1ca9dSYoshinobu Inoue 		m0.m_data = (char *)&af;
362cfa1ca9dSYoshinobu Inoue 
363cfa1ca9dSYoshinobu Inoue 		bpf_mtap(ifp, &m0);
364cfa1ca9dSYoshinobu Inoue 	}
365cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
366cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
367cfa1ca9dSYoshinobu Inoue 
36833841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
36933841545SHajimu UMEMOTO 
370686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
371686cdd19SJun-ichiro itojun Hagino 
37233841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
373cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
374cfa1ca9dSYoshinobu Inoue #ifdef INET
375cfa1ca9dSYoshinobu Inoue 	case AF_INET:
376cfa1ca9dSYoshinobu Inoue 		error = in_gif_output(ifp, dst->sa_family, m, rt);
377cfa1ca9dSYoshinobu Inoue 		break;
378cfa1ca9dSYoshinobu Inoue #endif
379cfa1ca9dSYoshinobu Inoue #ifdef INET6
380cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
381cfa1ca9dSYoshinobu Inoue 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
382cfa1ca9dSYoshinobu Inoue 		break;
383cfa1ca9dSYoshinobu Inoue #endif
384cfa1ca9dSYoshinobu Inoue 	default:
385cfa1ca9dSYoshinobu Inoue 		m_freem(m);
386cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
38733841545SHajimu UMEMOTO 		goto end;
388cfa1ca9dSYoshinobu Inoue 	}
389cfa1ca9dSYoshinobu Inoue 
390cfa1ca9dSYoshinobu Inoue   end:
391f0133454SMaxim Sobolev 	called = 0;		/* reset recursion counter */
39233841545SHajimu UMEMOTO 	if (error)
39333841545SHajimu UMEMOTO 		ifp->if_oerrors++;
394cfa1ca9dSYoshinobu Inoue 	return error;
395cfa1ca9dSYoshinobu Inoue }
396cfa1ca9dSYoshinobu Inoue 
397cfa1ca9dSYoshinobu Inoue void
398cfa1ca9dSYoshinobu Inoue gif_input(m, af, gifp)
399cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
400cfa1ca9dSYoshinobu Inoue 	int af;
401cfa1ca9dSYoshinobu Inoue 	struct ifnet *gifp;
402cfa1ca9dSYoshinobu Inoue {
403df5e1987SJonathan Lemon 	int isr;
40433841545SHajimu UMEMOTO 	struct ifqueue *ifq = 0;
405cfa1ca9dSYoshinobu Inoue 
406cfa1ca9dSYoshinobu Inoue 	if (gifp == NULL) {
407cfa1ca9dSYoshinobu Inoue 		/* just in case */
408cfa1ca9dSYoshinobu Inoue 		m_freem(m);
409cfa1ca9dSYoshinobu Inoue 		return;
410cfa1ca9dSYoshinobu Inoue 	}
411cfa1ca9dSYoshinobu Inoue 
412cfa1ca9dSYoshinobu Inoue 	m->m_pkthdr.rcvif = gifp;
413cfa1ca9dSYoshinobu Inoue 
41410722b85SRobert Watson #ifdef MAC
41510722b85SRobert Watson 	mac_create_mbuf_from_ifnet(gifp, m);
41610722b85SRobert Watson #endif
41710722b85SRobert Watson 
418cfa1ca9dSYoshinobu Inoue 	if (gifp->if_bpf) {
419cfa1ca9dSYoshinobu Inoue 		/*
420cfa1ca9dSYoshinobu Inoue 		 * We need to prepend the address family as
421cfa1ca9dSYoshinobu Inoue 		 * a four byte field.  Cons up a dummy header
422cfa1ca9dSYoshinobu Inoue 		 * to pacify bpf.  This is safe because bpf
423cfa1ca9dSYoshinobu Inoue 		 * will only read from the mbuf (i.e., it won't
424cfa1ca9dSYoshinobu Inoue 		 * try to free it or keep a pointer a to it).
425cfa1ca9dSYoshinobu Inoue 		 */
426cfa1ca9dSYoshinobu Inoue 		struct mbuf m0;
42733841545SHajimu UMEMOTO 		u_int32_t af1 = af;
428cfa1ca9dSYoshinobu Inoue 
429cfa1ca9dSYoshinobu Inoue 		m0.m_next = m;
430cfa1ca9dSYoshinobu Inoue 		m0.m_len = 4;
43133841545SHajimu UMEMOTO 		m0.m_data = (char *)&af1;
432cfa1ca9dSYoshinobu Inoue 
433cfa1ca9dSYoshinobu Inoue 		bpf_mtap(gifp, &m0);
434cfa1ca9dSYoshinobu Inoue 	}
435cfa1ca9dSYoshinobu Inoue 
43694408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
43794408d94SBrooks Davis 		(*ng_gif_input_p)(gifp, &m, af);
43894408d94SBrooks Davis 		if (m == NULL)
43994408d94SBrooks Davis 			return;
44094408d94SBrooks Davis 	}
44194408d94SBrooks Davis 
442cfa1ca9dSYoshinobu Inoue 	/*
443cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
444cfa1ca9dSYoshinobu Inoue 	 * specified address family.
445cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
446cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
447cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
448cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
449cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
45088ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
45188ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
452cfa1ca9dSYoshinobu Inoue 	 */
453cfa1ca9dSYoshinobu Inoue 	switch (af) {
454cfa1ca9dSYoshinobu Inoue #ifdef INET
455cfa1ca9dSYoshinobu Inoue 	case AF_INET:
456cfa1ca9dSYoshinobu Inoue 		ifq = &ipintrq;
457cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
458cfa1ca9dSYoshinobu Inoue 		break;
459cfa1ca9dSYoshinobu Inoue #endif
460cfa1ca9dSYoshinobu Inoue #ifdef INET6
461cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
462cfa1ca9dSYoshinobu Inoue 		ifq = &ip6intrq;
463cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
464cfa1ca9dSYoshinobu Inoue 		break;
465cfa1ca9dSYoshinobu Inoue #endif
466cfa1ca9dSYoshinobu Inoue 	default:
46794408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
46894408d94SBrooks Davis 			(*ng_gif_input_orphan_p)(gifp, m, af);
46994408d94SBrooks Davis 		else
470cfa1ca9dSYoshinobu Inoue 			m_freem(m);
471cfa1ca9dSYoshinobu Inoue 		return;
472cfa1ca9dSYoshinobu Inoue 	}
473cfa1ca9dSYoshinobu Inoue 
474cfa1ca9dSYoshinobu Inoue 	gifp->if_ipackets++;
475cfa1ca9dSYoshinobu Inoue 	gifp->if_ibytes += m->m_pkthdr.len;
476df5e1987SJonathan Lemon 	(void) IF_HANDOFF(ifq, m, NULL);
477df5e1987SJonathan Lemon 	/* we need schednetisr since the address family may change */
478df5e1987SJonathan Lemon 	schednetisr(isr);
479cfa1ca9dSYoshinobu Inoue 
480cfa1ca9dSYoshinobu Inoue 	return;
481cfa1ca9dSYoshinobu Inoue }
482cfa1ca9dSYoshinobu Inoue 
483686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
484cfa1ca9dSYoshinobu Inoue int
485cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
486cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
487cfa1ca9dSYoshinobu Inoue 	u_long cmd;
488cfa1ca9dSYoshinobu Inoue 	caddr_t data;
489cfa1ca9dSYoshinobu Inoue {
490cfa1ca9dSYoshinobu Inoue 	struct gif_softc *sc  = (struct gif_softc*)ifp;
491cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
492cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
493686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
494cfa1ca9dSYoshinobu Inoue 
495cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
496cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
4979426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
498cfa1ca9dSYoshinobu Inoue 		break;
499cfa1ca9dSYoshinobu Inoue 
500cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
501cfa1ca9dSYoshinobu Inoue 		break;
502cfa1ca9dSYoshinobu Inoue 
503cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
504cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
505cfa1ca9dSYoshinobu Inoue 		break;
506cfa1ca9dSYoshinobu Inoue 
507686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
508cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
509cfa1ca9dSYoshinobu Inoue 		break;
510686cdd19SJun-ichiro itojun Hagino 
511cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
512cfa1ca9dSYoshinobu Inoue 		{
513cfa1ca9dSYoshinobu Inoue 			u_long mtu;
514cfa1ca9dSYoshinobu Inoue 			mtu = ifr->ifr_mtu;
515cfa1ca9dSYoshinobu Inoue 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
516cfa1ca9dSYoshinobu Inoue 				return (EINVAL);
517cfa1ca9dSYoshinobu Inoue 			}
518cfa1ca9dSYoshinobu Inoue 			ifp->if_mtu = mtu;
519cfa1ca9dSYoshinobu Inoue 		}
520cfa1ca9dSYoshinobu Inoue 		break;
521686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
522cfa1ca9dSYoshinobu Inoue 
523cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
524cfa1ca9dSYoshinobu Inoue #ifdef INET6
525cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
526cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
52733841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
528686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
52933841545SHajimu UMEMOTO #ifdef INET
530686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
531cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
532cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
533cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
534cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
535cfa1ca9dSYoshinobu Inoue 			break;
53633841545SHajimu UMEMOTO #endif
537cfa1ca9dSYoshinobu Inoue #ifdef INET6
538686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
539cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
540cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
541cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
542cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
543686cdd19SJun-ichiro itojun Hagino 			break;
544686cdd19SJun-ichiro itojun Hagino #endif
54533841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
54633841545SHajimu UMEMOTO 			src = (struct sockaddr *)
54733841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
54833841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
54933841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
5509426aedfSHajimu UMEMOTO 			break;
5516f4ded3aSBrooks Davis 		default:
5529426aedfSHajimu UMEMOTO 			return EINVAL;
55333841545SHajimu UMEMOTO 		}
55433841545SHajimu UMEMOTO 
55533841545SHajimu UMEMOTO 		/* sa_family must be equal */
55633841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
55733841545SHajimu UMEMOTO 			return EINVAL;
55833841545SHajimu UMEMOTO 
55933841545SHajimu UMEMOTO 		/* validate sa_len */
56033841545SHajimu UMEMOTO 		switch (src->sa_family) {
56133841545SHajimu UMEMOTO #ifdef INET
56233841545SHajimu UMEMOTO 		case AF_INET:
56333841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
56433841545SHajimu UMEMOTO 				return EINVAL;
56533841545SHajimu UMEMOTO 			break;
56633841545SHajimu UMEMOTO #endif
56733841545SHajimu UMEMOTO #ifdef INET6
56833841545SHajimu UMEMOTO 		case AF_INET6:
56933841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
57033841545SHajimu UMEMOTO 				return EINVAL;
57133841545SHajimu UMEMOTO 			break;
57233841545SHajimu UMEMOTO #endif
57333841545SHajimu UMEMOTO 		default:
57433841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
57533841545SHajimu UMEMOTO 		}
57633841545SHajimu UMEMOTO 		switch (dst->sa_family) {
57733841545SHajimu UMEMOTO #ifdef INET
57833841545SHajimu UMEMOTO 		case AF_INET:
57933841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
58033841545SHajimu UMEMOTO 				return EINVAL;
58133841545SHajimu UMEMOTO 			break;
58233841545SHajimu UMEMOTO #endif
58333841545SHajimu UMEMOTO #ifdef INET6
58433841545SHajimu UMEMOTO 		case AF_INET6:
58533841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
58633841545SHajimu UMEMOTO 				return EINVAL;
58733841545SHajimu UMEMOTO 			break;
58833841545SHajimu UMEMOTO #endif
58933841545SHajimu UMEMOTO 		default:
59033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
59133841545SHajimu UMEMOTO 		}
59233841545SHajimu UMEMOTO 
59333841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
59433841545SHajimu UMEMOTO 		switch (cmd) {
59533841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
59633841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
59733841545SHajimu UMEMOTO 				break;
59833841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
59933841545SHajimu UMEMOTO #ifdef INET6
60033841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
60133841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
60233841545SHajimu UMEMOTO 				break;
60333841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
60433841545SHajimu UMEMOTO #endif /* INET6 */
60533841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
60633841545SHajimu UMEMOTO 			/* checks done in the above */
60733841545SHajimu UMEMOTO 			break;
608686cdd19SJun-ichiro itojun Hagino 		}
609cfa1ca9dSYoshinobu Inoue 
6109426aedfSHajimu UMEMOTO 		error = gif_set_tunnel(&sc->gif_if, src, dst);
611cfa1ca9dSYoshinobu Inoue 		break;
612cfa1ca9dSYoshinobu Inoue 
613686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
614686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
6159426aedfSHajimu UMEMOTO 		gif_delete_tunnel(&sc->gif_if);
616686cdd19SJun-ichiro itojun Hagino 		break;
617686cdd19SJun-ichiro itojun Hagino #endif
618686cdd19SJun-ichiro itojun Hagino 
619cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
620cfa1ca9dSYoshinobu Inoue #ifdef INET6
621cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
622cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
623cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
624cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
625cfa1ca9dSYoshinobu Inoue 			goto bad;
626cfa1ca9dSYoshinobu Inoue 		}
627cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
62833841545SHajimu UMEMOTO 		switch (cmd) {
629cfa1ca9dSYoshinobu Inoue #ifdef INET
63033841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
631cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
63233841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
633cfa1ca9dSYoshinobu Inoue 			break;
634cfa1ca9dSYoshinobu Inoue #endif /* INET */
635cfa1ca9dSYoshinobu Inoue #ifdef INET6
63633841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
637cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
638cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
63933841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
640cfa1ca9dSYoshinobu Inoue 			break;
641cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
642cfa1ca9dSYoshinobu Inoue 		default:
643cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
644cfa1ca9dSYoshinobu Inoue 			goto bad;
645cfa1ca9dSYoshinobu Inoue 		}
64633841545SHajimu UMEMOTO 		if (src->sa_len > size)
64733841545SHajimu UMEMOTO 			return EINVAL;
64833841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
649cfa1ca9dSYoshinobu Inoue 		break;
650cfa1ca9dSYoshinobu Inoue 
651cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
652cfa1ca9dSYoshinobu Inoue #ifdef INET6
653cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
654cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
655cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
656cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
657cfa1ca9dSYoshinobu Inoue 			goto bad;
658cfa1ca9dSYoshinobu Inoue 		}
659cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
66033841545SHajimu UMEMOTO 		switch (cmd) {
661cfa1ca9dSYoshinobu Inoue #ifdef INET
66233841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
663cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
66433841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
665cfa1ca9dSYoshinobu Inoue 			break;
666cfa1ca9dSYoshinobu Inoue #endif /* INET */
667cfa1ca9dSYoshinobu Inoue #ifdef INET6
66833841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
669cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
670cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
67133841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
672cfa1ca9dSYoshinobu Inoue 			break;
673cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
674cfa1ca9dSYoshinobu Inoue 		default:
675cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
676cfa1ca9dSYoshinobu Inoue 			goto bad;
677cfa1ca9dSYoshinobu Inoue 		}
67833841545SHajimu UMEMOTO 		if (src->sa_len > size)
67933841545SHajimu UMEMOTO 			return EINVAL;
68033841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
68133841545SHajimu UMEMOTO 		break;
68233841545SHajimu UMEMOTO 
68333841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
68433841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
68533841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
68633841545SHajimu UMEMOTO 			goto bad;
68733841545SHajimu UMEMOTO 		}
68833841545SHajimu UMEMOTO 
68933841545SHajimu UMEMOTO 		/* copy src */
69033841545SHajimu UMEMOTO 		src = sc->gif_psrc;
69133841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
69233841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
69333841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
69433841545SHajimu UMEMOTO 		if (src->sa_len > size)
69533841545SHajimu UMEMOTO 			return EINVAL;
69633841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
69733841545SHajimu UMEMOTO 
69833841545SHajimu UMEMOTO 		/* copy dst */
69933841545SHajimu UMEMOTO 		src = sc->gif_pdst;
70033841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
70133841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
70233841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
70333841545SHajimu UMEMOTO 		if (src->sa_len > size)
70433841545SHajimu UMEMOTO 			return EINVAL;
70533841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
706cfa1ca9dSYoshinobu Inoue 		break;
707cfa1ca9dSYoshinobu Inoue 
708cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
709686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
710cfa1ca9dSYoshinobu Inoue 		break;
711cfa1ca9dSYoshinobu Inoue 
712cfa1ca9dSYoshinobu Inoue 	default:
713cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
714cfa1ca9dSYoshinobu Inoue 		break;
715cfa1ca9dSYoshinobu Inoue 	}
716cfa1ca9dSYoshinobu Inoue  bad:
717cfa1ca9dSYoshinobu Inoue 	return error;
718cfa1ca9dSYoshinobu Inoue }
71953dab5feSBrooks Davis 
7209426aedfSHajimu UMEMOTO int
7219426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
7229426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
7239426aedfSHajimu UMEMOTO 	struct sockaddr *src;
7249426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
72553dab5feSBrooks Davis {
7269426aedfSHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc *)ifp;
7279426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
7289426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
7299426aedfSHajimu UMEMOTO 	int s;
7309426aedfSHajimu UMEMOTO 	int error = 0;
7319426aedfSHajimu UMEMOTO 
7329426aedfSHajimu UMEMOTO 	s = splnet();
7339426aedfSHajimu UMEMOTO 
7349426aedfSHajimu UMEMOTO 	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
7359426aedfSHajimu UMEMOTO 		if (sc2 == sc)
7369426aedfSHajimu UMEMOTO 			continue;
7379426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
7389426aedfSHajimu UMEMOTO 			continue;
7399426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
7409426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
7419426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
7429426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
7439426aedfSHajimu UMEMOTO 			continue;
7449426aedfSHajimu UMEMOTO 
7459426aedfSHajimu UMEMOTO 		/*
7469426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
7479426aedfSHajimu UMEMOTO 		 * otherwise.
7489426aedfSHajimu UMEMOTO 		 */
7499426aedfSHajimu UMEMOTO 		if (!parallel_tunnels &&
7509426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
7519426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
7529426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
7539426aedfSHajimu UMEMOTO 			goto bad;
7549426aedfSHajimu UMEMOTO 		}
7559426aedfSHajimu UMEMOTO 
7569426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
7579426aedfSHajimu UMEMOTO 	}
7589426aedfSHajimu UMEMOTO 
7599426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
7609426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
7619426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
7629426aedfSHajimu UMEMOTO #ifdef INET
7639426aedfSHajimu UMEMOTO 		case AF_INET:
7649426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
7659426aedfSHajimu UMEMOTO 			break;
7669426aedfSHajimu UMEMOTO #endif
7679426aedfSHajimu UMEMOTO #ifdef INET6
7689426aedfSHajimu UMEMOTO 		case AF_INET6:
7699426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
7709426aedfSHajimu UMEMOTO 			break;
7719426aedfSHajimu UMEMOTO #endif
7729426aedfSHajimu UMEMOTO 		}
7739426aedfSHajimu UMEMOTO 
7749426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
7759426aedfSHajimu UMEMOTO 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
7769426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
7779426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
7789426aedfSHajimu UMEMOTO 
7799426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
7809426aedfSHajimu UMEMOTO 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
7819426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
7829426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
7839426aedfSHajimu UMEMOTO 
7849426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
7859426aedfSHajimu UMEMOTO #ifdef INET
7869426aedfSHajimu UMEMOTO 	case AF_INET:
7879426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
7889426aedfSHajimu UMEMOTO 		break;
7899426aedfSHajimu UMEMOTO #endif
7909426aedfSHajimu UMEMOTO #ifdef INET6
7919426aedfSHajimu UMEMOTO 	case AF_INET6:
7929426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
7939426aedfSHajimu UMEMOTO 		break;
7949426aedfSHajimu UMEMOTO #endif
7959426aedfSHajimu UMEMOTO 	}
7969426aedfSHajimu UMEMOTO 	if (error) {
7979426aedfSHajimu UMEMOTO 		/* rollback */
7989426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
7999426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
8009426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
8019426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
8029426aedfSHajimu UMEMOTO 		goto bad;
8039426aedfSHajimu UMEMOTO 	}
8049426aedfSHajimu UMEMOTO 
8059426aedfSHajimu UMEMOTO 	if (osrc)
8069426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
8079426aedfSHajimu UMEMOTO 	if (odst)
8089426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
8099426aedfSHajimu UMEMOTO 
8109426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8119426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8129426aedfSHajimu UMEMOTO 	else
8139426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
8149426aedfSHajimu UMEMOTO 	splx(s);
8159426aedfSHajimu UMEMOTO 
8169426aedfSHajimu UMEMOTO 	return 0;
8179426aedfSHajimu UMEMOTO 
8189426aedfSHajimu UMEMOTO  bad:
8199426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
8209426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_RUNNING;
8219426aedfSHajimu UMEMOTO 	else
8229426aedfSHajimu UMEMOTO 		ifp->if_flags &= ~IFF_RUNNING;
8239426aedfSHajimu UMEMOTO 	splx(s);
8249426aedfSHajimu UMEMOTO 
8259426aedfSHajimu UMEMOTO 	return error;
8269426aedfSHajimu UMEMOTO }
8279426aedfSHajimu UMEMOTO 
8289426aedfSHajimu UMEMOTO void
8299426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
8309426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8319426aedfSHajimu UMEMOTO {
8329426aedfSHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc *)ifp;
8339426aedfSHajimu UMEMOTO 	int s;
8349426aedfSHajimu UMEMOTO 
8359426aedfSHajimu UMEMOTO 	s = splnet();
83653dab5feSBrooks Davis 
83753dab5feSBrooks Davis 	if (sc->gif_psrc) {
83853dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
83953dab5feSBrooks Davis 		sc->gif_psrc = NULL;
84053dab5feSBrooks Davis 	}
84153dab5feSBrooks Davis 	if (sc->gif_pdst) {
84253dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
84353dab5feSBrooks Davis 		sc->gif_pdst = NULL;
84453dab5feSBrooks Davis 	}
8459426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
8469426aedfSHajimu UMEMOTO #ifdef INET
8479426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
8489426aedfSHajimu UMEMOTO #endif
8499426aedfSHajimu UMEMOTO #ifdef INET6
8509426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
8519426aedfSHajimu UMEMOTO #endif
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);
85853dab5feSBrooks Davis }
859