xref: /freebsd/sys/net/if_gif.c (revision e0852ce2dec0c2116ead3cceb5f9f73e21092f05)
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 *);
101929ddbbbSAlfred Perlstein void gif_delete_tunnel(struct gif_softc *);
102929ddbbbSAlfred Perlstein static int gif_encapcheck(const struct mbuf *, int, int, void *);
10353dab5feSBrooks Davis 
104686cdd19SJun-ichiro itojun Hagino #ifdef INET
10553dab5feSBrooks Davis extern  struct domain inetdomain;
106f0ffb944SJulian Elischer struct protosw in_gif_protosw =
10753dab5feSBrooks Davis { SOCK_RAW,	&inetdomain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
1086f4ded3aSBrooks Davis   in_gif_input,	(pr_output_t*)rip_output, 0,		rip_ctloutput,
10953dab5feSBrooks Davis   0,
11053dab5feSBrooks Davis   0,		0,		0,		0,
11153dab5feSBrooks Davis   &rip_usrreqs
11253dab5feSBrooks Davis };
113686cdd19SJun-ichiro itojun Hagino #endif
114686cdd19SJun-ichiro itojun Hagino #ifdef INET6
1156f4ded3aSBrooks Davis extern  struct domain inet6domain;
11653dab5feSBrooks Davis struct ip6protosw in6_gif_protosw =
11753dab5feSBrooks Davis { SOCK_RAW,	&inet6domain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
11853dab5feSBrooks Davis   in6_gif_input, rip6_output,	0,		rip6_ctloutput,
11953dab5feSBrooks Davis   0,
12053dab5feSBrooks Davis   0,		0,		0,		0,
12153dab5feSBrooks Davis   &rip6_usrreqs
12253dab5feSBrooks Davis };
123686cdd19SJun-ichiro itojun Hagino #endif
124cfa1ca9dSYoshinobu Inoue 
125872f786aSBrooks Davis SYSCTL_DECL(_net_link);
126872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
127872f786aSBrooks Davis     "Generic Tunnel Interface");
128686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
129686cdd19SJun-ichiro itojun Hagino /*
130872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
131686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
132686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
133686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
134686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
135686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
136686cdd19SJun-ichiro itojun Hagino  */
137686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
138686cdd19SJun-ichiro itojun Hagino #endif
139686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST;
140872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
141872f786aSBrooks Davis     &max_gif_nesting, 0, "Max nested tunnels");
142872f786aSBrooks Davis 
143872f786aSBrooks Davis /*
144872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
145872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
146872f786aSBrooks Davis  * we allow control over this check here.
147872f786aSBrooks Davis  */
148872f786aSBrooks Davis #ifdef XBONEHACK
149872f786aSBrooks Davis static int parallel_tunnels = 1;
150872f786aSBrooks Davis #else
151872f786aSBrooks Davis static int parallel_tunnels = 0;
152872f786aSBrooks Davis #endif
153872f786aSBrooks Davis SYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
154872f786aSBrooks Davis     &parallel_tunnels, 0, "Allow parallel tunnels?");
155cfa1ca9dSYoshinobu Inoue 
15653dab5feSBrooks Davis int
15753dab5feSBrooks Davis gif_clone_create(ifc, unit)
15853dab5feSBrooks Davis 	struct if_clone *ifc;
1593b16e7b2SMaxime Henrion 	int unit;
160cfa1ca9dSYoshinobu Inoue {
16133841545SHajimu UMEMOTO 	struct gif_softc *sc;
162cfa1ca9dSYoshinobu Inoue 
16353dab5feSBrooks Davis 	sc = malloc (sizeof(struct gif_softc), M_GIF, M_WAITOK);
16453dab5feSBrooks Davis 	bzero(sc, sizeof(struct gif_softc));
16553dab5feSBrooks Davis 
16653dab5feSBrooks Davis 	sc->gif_if.if_softc = sc;
16753dab5feSBrooks Davis 	sc->gif_if.if_name = GIFNAME;
1683b16e7b2SMaxime Henrion 	sc->gif_if.if_unit = unit;
169686cdd19SJun-ichiro itojun Hagino 
170686cdd19SJun-ichiro itojun Hagino 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
171686cdd19SJun-ichiro itojun Hagino #ifdef INET
172686cdd19SJun-ichiro itojun Hagino 	sc->encap_cookie4 = encap_attach_func(AF_INET, -1,
17353dab5feSBrooks Davis 	    gif_encapcheck, (struct protosw*)&in_gif_protosw, sc);
174686cdd19SJun-ichiro itojun Hagino 	if (sc->encap_cookie4 == NULL) {
17553dab5feSBrooks Davis 		printf("%s: unable to attach encap4\n", if_name(&sc->gif_if));
17653dab5feSBrooks Davis 		free(sc, M_GIF);
17753dab5feSBrooks Davis 		return (EIO);	/* XXX */
178686cdd19SJun-ichiro itojun Hagino 	}
179686cdd19SJun-ichiro itojun Hagino #endif
180686cdd19SJun-ichiro itojun Hagino #ifdef INET6
181686cdd19SJun-ichiro itojun Hagino 	sc->encap_cookie6 = encap_attach_func(AF_INET6, -1,
182686cdd19SJun-ichiro itojun Hagino 	    gif_encapcheck, (struct protosw *)&in6_gif_protosw, sc);
183686cdd19SJun-ichiro itojun Hagino 	if (sc->encap_cookie6 == NULL) {
184686cdd19SJun-ichiro itojun Hagino 		if (sc->encap_cookie4) {
185686cdd19SJun-ichiro itojun Hagino 			encap_detach(sc->encap_cookie4);
186686cdd19SJun-ichiro itojun Hagino 			sc->encap_cookie4 = NULL;
187686cdd19SJun-ichiro itojun Hagino 		}
18853dab5feSBrooks Davis 		printf("%s: unable to attach encap6\n", if_name(&sc->gif_if));
18953dab5feSBrooks Davis 		free(sc, M_GIF);
19053dab5feSBrooks Davis 		return (EIO);	/* XXX */
191686cdd19SJun-ichiro itojun Hagino 	}
192686cdd19SJun-ichiro itojun Hagino #endif
193686cdd19SJun-ichiro itojun Hagino 
194cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_mtu    = GIF_MTU;
195cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
19633841545SHajimu UMEMOTO #if 0
19733841545SHajimu UMEMOTO 	/* turn off ingress filter */
19833841545SHajimu UMEMOTO 	sc->gif_if.if_flags  |= IFF_LINK2;
19933841545SHajimu UMEMOTO #endif
200cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_ioctl  = gif_ioctl;
201cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_output = gif_output;
202cfa1ca9dSYoshinobu Inoue 	sc->gif_if.if_type   = IFT_GIF;
203686cdd19SJun-ichiro itojun Hagino 	sc->gif_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
204cfa1ca9dSYoshinobu Inoue 	if_attach(&sc->gif_if);
205cfa1ca9dSYoshinobu Inoue 	bpfattach(&sc->gif_if, DLT_NULL, sizeof(u_int));
20694408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
20794408d94SBrooks Davis 		(*ng_gif_attach_p)(&sc->gif_if);
20820af0ffaSBrooks Davis 	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_link);
20953dab5feSBrooks Davis 	return (0);
210cfa1ca9dSYoshinobu Inoue }
211cfa1ca9dSYoshinobu Inoue 
212ae5a19beSBrooks Davis void
21353dab5feSBrooks Davis gif_clone_destroy(ifp)
21453dab5feSBrooks Davis 	struct ifnet *ifp;
21553dab5feSBrooks Davis {
21653dab5feSBrooks Davis 	int err;
21753dab5feSBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
21853dab5feSBrooks Davis 
21953dab5feSBrooks Davis 	gif_delete_tunnel(sc);
22020af0ffaSBrooks Davis 	LIST_REMOVE(sc, gif_link);
22153dab5feSBrooks Davis 	if (sc->encap_cookie4 != NULL) {
22253dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie4);
22353dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
22453dab5feSBrooks Davis 	}
22553dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
22653dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
22753dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
22853dab5feSBrooks Davis 	}
22953dab5feSBrooks Davis 
23094408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
23194408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
23253dab5feSBrooks Davis 	bpfdetach(ifp);
23353dab5feSBrooks Davis 	if_detach(ifp);
23453dab5feSBrooks Davis 
23553dab5feSBrooks Davis 	free(sc, M_GIF);
23653dab5feSBrooks Davis }
23753dab5feSBrooks Davis 
23853dab5feSBrooks Davis static int
23953dab5feSBrooks Davis gifmodevent(mod, type, data)
24053dab5feSBrooks Davis 	module_t mod;
24153dab5feSBrooks Davis 	int type;
24253dab5feSBrooks Davis 	void *data;
24353dab5feSBrooks Davis {
24453dab5feSBrooks Davis 
24553dab5feSBrooks Davis 	switch (type) {
24653dab5feSBrooks Davis 	case MOD_LOAD:
24720af0ffaSBrooks Davis 		LIST_INIT(&gif_softc_list);
24853dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
24953dab5feSBrooks Davis 
25053dab5feSBrooks Davis #ifdef INET6
25153dab5feSBrooks Davis 		ip6_gif_hlim = GIF_HLIM;
25253dab5feSBrooks Davis #endif
25353dab5feSBrooks Davis 
25453dab5feSBrooks Davis 		break;
25553dab5feSBrooks Davis 	case MOD_UNLOAD:
25653dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
25753dab5feSBrooks Davis 
25820af0ffaSBrooks Davis 		while (!LIST_EMPTY(&gif_softc_list))
25920af0ffaSBrooks Davis 			gif_clone_destroy(&LIST_FIRST(&gif_softc_list)->gif_if);
26053dab5feSBrooks Davis 
26153dab5feSBrooks Davis #ifdef INET6
26253dab5feSBrooks Davis 		ip6_gif_hlim = 0;
26353dab5feSBrooks Davis #endif
26453dab5feSBrooks Davis 		break;
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 
278686cdd19SJun-ichiro itojun Hagino static 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 
292686cdd19SJun-ichiro itojun Hagino 	if ((sc->gif_if.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 
3126f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
313686cdd19SJun-ichiro itojun Hagino 
314686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
315686cdd19SJun-ichiro itojun Hagino #ifdef INET
316686cdd19SJun-ichiro itojun Hagino 	case 4:
317686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
318686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
319686cdd19SJun-ichiro itojun Hagino 			return 0;
320686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
321686cdd19SJun-ichiro itojun Hagino #endif
322686cdd19SJun-ichiro itojun Hagino #ifdef INET6
323686cdd19SJun-ichiro itojun Hagino 	case 6:
324686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
325686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
326686cdd19SJun-ichiro itojun Hagino 			return 0;
327686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
328686cdd19SJun-ichiro itojun Hagino #endif
329686cdd19SJun-ichiro itojun Hagino 	default:
330686cdd19SJun-ichiro itojun Hagino 		return 0;
331686cdd19SJun-ichiro itojun Hagino 	}
332686cdd19SJun-ichiro itojun Hagino }
333686cdd19SJun-ichiro itojun Hagino 
334cfa1ca9dSYoshinobu Inoue int
335cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
336cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
337cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
338cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
339cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
340cfa1ca9dSYoshinobu Inoue {
34133841545SHajimu UMEMOTO 	struct gif_softc *sc = (struct gif_softc*)ifp;
342cfa1ca9dSYoshinobu Inoue 	int error = 0;
343cfa1ca9dSYoshinobu Inoue 	static int called = 0;	/* XXX: MUTEX */
344cfa1ca9dSYoshinobu Inoue 
34510722b85SRobert Watson #ifdef MAC
34610722b85SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
347e0852ce2SRobert Watson 	if (error) {
348e0852ce2SRobert Watson 		m_freem(m);
349e0852ce2SRobert Watson 		goto end;
350e0852ce2SRobert Watson 	}
35110722b85SRobert Watson #endif
35210722b85SRobert Watson 
353cfa1ca9dSYoshinobu Inoue 	/*
354cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
355cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
356cfa1ca9dSYoshinobu Inoue 	 * XXX: this mechanism may introduce another problem about
357cfa1ca9dSYoshinobu Inoue 	 *      mutual exclusion of the variable CALLED, especially if we
358cfa1ca9dSYoshinobu Inoue 	 *      use kernel thread.
359cfa1ca9dSYoshinobu Inoue 	 */
360686cdd19SJun-ichiro itojun Hagino 	if (++called > max_gif_nesting) {
361cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
362cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
363cfa1ca9dSYoshinobu Inoue 		    called);
364cfa1ca9dSYoshinobu Inoue 		m_freem(m);
365cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
366cfa1ca9dSYoshinobu Inoue 		goto end;
367cfa1ca9dSYoshinobu Inoue 	}
368686cdd19SJun-ichiro itojun Hagino 
369cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
370cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
371cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
372cfa1ca9dSYoshinobu Inoue 		m_freem(m);
373cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
374cfa1ca9dSYoshinobu Inoue 		goto end;
375cfa1ca9dSYoshinobu Inoue 	}
376cfa1ca9dSYoshinobu Inoue 
377cfa1ca9dSYoshinobu Inoue 	if (ifp->if_bpf) {
378cfa1ca9dSYoshinobu Inoue 		/*
379cfa1ca9dSYoshinobu Inoue 		 * We need to prepend the address family as
380cfa1ca9dSYoshinobu Inoue 		 * a four byte field.  Cons up a dummy header
381cfa1ca9dSYoshinobu Inoue 		 * to pacify bpf.  This is safe because bpf
382cfa1ca9dSYoshinobu Inoue 		 * will only read from the mbuf (i.e., it won't
383cfa1ca9dSYoshinobu Inoue 		 * try to free it or keep a pointer a to it).
384cfa1ca9dSYoshinobu Inoue 		 */
385cfa1ca9dSYoshinobu Inoue 		struct mbuf m0;
38633841545SHajimu UMEMOTO 		u_int32_t af = dst->sa_family;
387cfa1ca9dSYoshinobu Inoue 
388cfa1ca9dSYoshinobu Inoue 		m0.m_next = m;
389cfa1ca9dSYoshinobu Inoue 		m0.m_len = 4;
390cfa1ca9dSYoshinobu Inoue 		m0.m_data = (char *)&af;
391cfa1ca9dSYoshinobu Inoue 
392cfa1ca9dSYoshinobu Inoue 		bpf_mtap(ifp, &m0);
393cfa1ca9dSYoshinobu Inoue 	}
394cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
395cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
396cfa1ca9dSYoshinobu Inoue 
39733841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
39833841545SHajimu UMEMOTO 
399686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
400686cdd19SJun-ichiro itojun Hagino 
40133841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
402cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
403cfa1ca9dSYoshinobu Inoue #ifdef INET
404cfa1ca9dSYoshinobu Inoue 	case AF_INET:
405cfa1ca9dSYoshinobu Inoue 		error = in_gif_output(ifp, dst->sa_family, m, rt);
406cfa1ca9dSYoshinobu Inoue 		break;
407cfa1ca9dSYoshinobu Inoue #endif
408cfa1ca9dSYoshinobu Inoue #ifdef INET6
409cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
410cfa1ca9dSYoshinobu Inoue 		error = in6_gif_output(ifp, dst->sa_family, m, rt);
411cfa1ca9dSYoshinobu Inoue 		break;
412cfa1ca9dSYoshinobu Inoue #endif
413cfa1ca9dSYoshinobu Inoue 	default:
414cfa1ca9dSYoshinobu Inoue 		m_freem(m);
415cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
41633841545SHajimu UMEMOTO 		goto end;
417cfa1ca9dSYoshinobu Inoue 	}
418cfa1ca9dSYoshinobu Inoue 
419cfa1ca9dSYoshinobu Inoue   end:
420cfa1ca9dSYoshinobu Inoue 	called = 0;		/* reset recursion counter */
42133841545SHajimu UMEMOTO 	if (error)
42233841545SHajimu UMEMOTO 		ifp->if_oerrors++;
423cfa1ca9dSYoshinobu Inoue 	return error;
424cfa1ca9dSYoshinobu Inoue }
425cfa1ca9dSYoshinobu Inoue 
426cfa1ca9dSYoshinobu Inoue void
427cfa1ca9dSYoshinobu Inoue gif_input(m, af, gifp)
428cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
429cfa1ca9dSYoshinobu Inoue 	int af;
430cfa1ca9dSYoshinobu Inoue 	struct ifnet *gifp;
431cfa1ca9dSYoshinobu Inoue {
432df5e1987SJonathan Lemon 	int isr;
43333841545SHajimu UMEMOTO 	struct ifqueue *ifq = 0;
434cfa1ca9dSYoshinobu Inoue 
435cfa1ca9dSYoshinobu Inoue 	if (gifp == NULL) {
436cfa1ca9dSYoshinobu Inoue 		/* just in case */
437cfa1ca9dSYoshinobu Inoue 		m_freem(m);
438cfa1ca9dSYoshinobu Inoue 		return;
439cfa1ca9dSYoshinobu Inoue 	}
440cfa1ca9dSYoshinobu Inoue 
441cfa1ca9dSYoshinobu Inoue 	m->m_pkthdr.rcvif = gifp;
442cfa1ca9dSYoshinobu Inoue 
44310722b85SRobert Watson #ifdef MAC
44410722b85SRobert Watson 	mac_create_mbuf_from_ifnet(gifp, m);
44510722b85SRobert Watson #endif
44610722b85SRobert Watson 
447cfa1ca9dSYoshinobu Inoue 	if (gifp->if_bpf) {
448cfa1ca9dSYoshinobu Inoue 		/*
449cfa1ca9dSYoshinobu Inoue 		 * We need to prepend the address family as
450cfa1ca9dSYoshinobu Inoue 		 * a four byte field.  Cons up a dummy header
451cfa1ca9dSYoshinobu Inoue 		 * to pacify bpf.  This is safe because bpf
452cfa1ca9dSYoshinobu Inoue 		 * will only read from the mbuf (i.e., it won't
453cfa1ca9dSYoshinobu Inoue 		 * try to free it or keep a pointer a to it).
454cfa1ca9dSYoshinobu Inoue 		 */
455cfa1ca9dSYoshinobu Inoue 		struct mbuf m0;
45633841545SHajimu UMEMOTO 		u_int32_t af1 = af;
457cfa1ca9dSYoshinobu Inoue 
458cfa1ca9dSYoshinobu Inoue 		m0.m_next = m;
459cfa1ca9dSYoshinobu Inoue 		m0.m_len = 4;
46033841545SHajimu UMEMOTO 		m0.m_data = (char *)&af1;
461cfa1ca9dSYoshinobu Inoue 
462cfa1ca9dSYoshinobu Inoue 		bpf_mtap(gifp, &m0);
463cfa1ca9dSYoshinobu Inoue 	}
464cfa1ca9dSYoshinobu Inoue 
46594408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
46694408d94SBrooks Davis 		(*ng_gif_input_p)(gifp, &m, af);
46794408d94SBrooks Davis 		if (m == NULL)
46894408d94SBrooks Davis 			return;
46994408d94SBrooks Davis 	}
47094408d94SBrooks Davis 
471cfa1ca9dSYoshinobu Inoue 	/*
472cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
473cfa1ca9dSYoshinobu Inoue 	 * specified address family.
474cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
475cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
476cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
477cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
478cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
47988ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
48088ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
481cfa1ca9dSYoshinobu Inoue 	 */
482cfa1ca9dSYoshinobu Inoue 	switch (af) {
483cfa1ca9dSYoshinobu Inoue #ifdef INET
484cfa1ca9dSYoshinobu Inoue 	case AF_INET:
485cfa1ca9dSYoshinobu Inoue 		ifq = &ipintrq;
486cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
487cfa1ca9dSYoshinobu Inoue 		break;
488cfa1ca9dSYoshinobu Inoue #endif
489cfa1ca9dSYoshinobu Inoue #ifdef INET6
490cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
491cfa1ca9dSYoshinobu Inoue 		ifq = &ip6intrq;
492cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
493cfa1ca9dSYoshinobu Inoue 		break;
494cfa1ca9dSYoshinobu Inoue #endif
495cfa1ca9dSYoshinobu Inoue 	default:
49694408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
49794408d94SBrooks Davis 			(*ng_gif_input_orphan_p)(gifp, m, af);
49894408d94SBrooks Davis 		else
499cfa1ca9dSYoshinobu Inoue 			m_freem(m);
500cfa1ca9dSYoshinobu Inoue 		return;
501cfa1ca9dSYoshinobu Inoue 	}
502cfa1ca9dSYoshinobu Inoue 
503cfa1ca9dSYoshinobu Inoue 	gifp->if_ipackets++;
504cfa1ca9dSYoshinobu Inoue 	gifp->if_ibytes += m->m_pkthdr.len;
505df5e1987SJonathan Lemon 	(void) IF_HANDOFF(ifq, m, NULL);
506df5e1987SJonathan Lemon 	/* we need schednetisr since the address family may change */
507df5e1987SJonathan Lemon 	schednetisr(isr);
508cfa1ca9dSYoshinobu Inoue 
509cfa1ca9dSYoshinobu Inoue 	return;
510cfa1ca9dSYoshinobu Inoue }
511cfa1ca9dSYoshinobu Inoue 
512686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
513cfa1ca9dSYoshinobu Inoue int
514cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
515cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
516cfa1ca9dSYoshinobu Inoue 	u_long cmd;
517cfa1ca9dSYoshinobu Inoue 	caddr_t data;
518cfa1ca9dSYoshinobu Inoue {
519cfa1ca9dSYoshinobu Inoue 	struct gif_softc *sc  = (struct gif_softc*)ifp;
520cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
521cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
522686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
523686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *sa;
524a98dc5e6SYaroslav Tykhiy 	int s;
52553dab5feSBrooks Davis 	struct ifnet *ifp2;
526686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc2;
527cfa1ca9dSYoshinobu Inoue 
528cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
529cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
530cfa1ca9dSYoshinobu Inoue 		break;
531cfa1ca9dSYoshinobu Inoue 
532cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
533cfa1ca9dSYoshinobu Inoue 		break;
534cfa1ca9dSYoshinobu Inoue 
535cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
536cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
537cfa1ca9dSYoshinobu Inoue 		break;
538cfa1ca9dSYoshinobu Inoue 
539686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
540cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
541cfa1ca9dSYoshinobu Inoue 		break;
542686cdd19SJun-ichiro itojun Hagino 
543cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
544cfa1ca9dSYoshinobu Inoue 		{
545cfa1ca9dSYoshinobu Inoue 			u_long mtu;
546cfa1ca9dSYoshinobu Inoue 			mtu = ifr->ifr_mtu;
547cfa1ca9dSYoshinobu Inoue 			if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX) {
548cfa1ca9dSYoshinobu Inoue 				return (EINVAL);
549cfa1ca9dSYoshinobu Inoue 			}
550cfa1ca9dSYoshinobu Inoue 			ifp->if_mtu = mtu;
551cfa1ca9dSYoshinobu Inoue 		}
552cfa1ca9dSYoshinobu Inoue 		break;
553686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
554cfa1ca9dSYoshinobu Inoue 
555cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
556cfa1ca9dSYoshinobu Inoue #ifdef INET6
557cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
558cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
55933841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
560686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
56133841545SHajimu UMEMOTO #ifdef INET
562686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
563cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
564cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
565cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
566cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
567cfa1ca9dSYoshinobu Inoue 			break;
56833841545SHajimu UMEMOTO #endif
569cfa1ca9dSYoshinobu Inoue #ifdef INET6
570686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
571cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
572cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
573cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
574cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
575686cdd19SJun-ichiro itojun Hagino 			break;
576686cdd19SJun-ichiro itojun Hagino #endif
57733841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
57833841545SHajimu UMEMOTO 			src = (struct sockaddr *)
57933841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
58033841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
58133841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
5826f4ded3aSBrooks Davis 		default:
5836f4ded3aSBrooks Davis 			error = EADDRNOTAVAIL;
5846f4ded3aSBrooks Davis 			goto bad;
58533841545SHajimu UMEMOTO 		}
58633841545SHajimu UMEMOTO 
58733841545SHajimu UMEMOTO 		/* sa_family must be equal */
58833841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
58933841545SHajimu UMEMOTO 			return EINVAL;
59033841545SHajimu UMEMOTO 
59133841545SHajimu UMEMOTO 		/* validate sa_len */
59233841545SHajimu UMEMOTO 		switch (src->sa_family) {
59333841545SHajimu UMEMOTO #ifdef INET
59433841545SHajimu UMEMOTO 		case AF_INET:
59533841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
59633841545SHajimu UMEMOTO 				return EINVAL;
59733841545SHajimu UMEMOTO 			break;
59833841545SHajimu UMEMOTO #endif
59933841545SHajimu UMEMOTO #ifdef INET6
60033841545SHajimu UMEMOTO 		case AF_INET6:
60133841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
60233841545SHajimu UMEMOTO 				return EINVAL;
60333841545SHajimu UMEMOTO 			break;
60433841545SHajimu UMEMOTO #endif
60533841545SHajimu UMEMOTO 		default:
60633841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
60733841545SHajimu UMEMOTO 		}
60833841545SHajimu UMEMOTO 		switch (dst->sa_family) {
60933841545SHajimu UMEMOTO #ifdef INET
61033841545SHajimu UMEMOTO 		case AF_INET:
61133841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
61233841545SHajimu UMEMOTO 				return EINVAL;
61333841545SHajimu UMEMOTO 			break;
61433841545SHajimu UMEMOTO #endif
61533841545SHajimu UMEMOTO #ifdef INET6
61633841545SHajimu UMEMOTO 		case AF_INET6:
61733841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
61833841545SHajimu UMEMOTO 				return EINVAL;
61933841545SHajimu UMEMOTO 			break;
62033841545SHajimu UMEMOTO #endif
62133841545SHajimu UMEMOTO 		default:
62233841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
62333841545SHajimu UMEMOTO 		}
62433841545SHajimu UMEMOTO 
62533841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
62633841545SHajimu UMEMOTO 		switch (cmd) {
62733841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
62833841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
62933841545SHajimu UMEMOTO 				break;
63033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
63133841545SHajimu UMEMOTO #ifdef INET6
63233841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
63333841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
63433841545SHajimu UMEMOTO 				break;
63533841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
63633841545SHajimu UMEMOTO #endif /* INET6 */
63733841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
63833841545SHajimu UMEMOTO 			/* checks done in the above */
63933841545SHajimu UMEMOTO 			break;
640686cdd19SJun-ichiro itojun Hagino 		}
641cfa1ca9dSYoshinobu Inoue 
64253dab5feSBrooks Davis 		TAILQ_FOREACH(ifp2, &ifnet, if_link) {
64353dab5feSBrooks Davis 			if (strcmp(ifp2->if_name, GIFNAME) != 0)
64453dab5feSBrooks Davis 				continue;
64553dab5feSBrooks Davis 			sc2 = ifp2->if_softc;
646686cdd19SJun-ichiro itojun Hagino 			if (sc2 == sc)
647686cdd19SJun-ichiro itojun Hagino 				continue;
648686cdd19SJun-ichiro itojun Hagino 			if (!sc2->gif_pdst || !sc2->gif_psrc)
649686cdd19SJun-ichiro itojun Hagino 				continue;
650686cdd19SJun-ichiro itojun Hagino 			if (sc2->gif_pdst->sa_family != dst->sa_family ||
651686cdd19SJun-ichiro itojun Hagino 			    sc2->gif_pdst->sa_len != dst->sa_len ||
652686cdd19SJun-ichiro itojun Hagino 			    sc2->gif_psrc->sa_family != src->sa_family ||
653686cdd19SJun-ichiro itojun Hagino 			    sc2->gif_psrc->sa_len != src->sa_len)
654686cdd19SJun-ichiro itojun Hagino 				continue;
655872f786aSBrooks Davis 
656872f786aSBrooks Davis 			/*
657872f786aSBrooks Davis 			 * Disallow parallel tunnels unless instructed
658872f786aSBrooks Davis 			 * otherwise.
659872f786aSBrooks Davis 			 */
660872f786aSBrooks Davis 			if (!parallel_tunnels &&
661872f786aSBrooks Davis 			    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
662686cdd19SJun-ichiro itojun Hagino 			    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
663cfa1ca9dSYoshinobu Inoue 				error = EADDRNOTAVAIL;
664cfa1ca9dSYoshinobu Inoue 				goto bad;
665cfa1ca9dSYoshinobu Inoue 			}
666686cdd19SJun-ichiro itojun Hagino 
667686cdd19SJun-ichiro itojun Hagino 			/* can't configure multiple multi-dest interfaces */
668686cdd19SJun-ichiro itojun Hagino #define multidest(x) \
669686cdd19SJun-ichiro itojun Hagino 	(((struct sockaddr_in *)(x))->sin_addr.s_addr == INADDR_ANY)
670686cdd19SJun-ichiro itojun Hagino #ifdef INET6
671686cdd19SJun-ichiro itojun Hagino #define multidest6(x) \
672686cdd19SJun-ichiro itojun Hagino 	(IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(x))->sin6_addr))
673686cdd19SJun-ichiro itojun Hagino #endif
674686cdd19SJun-ichiro itojun Hagino 			if (dst->sa_family == AF_INET &&
675686cdd19SJun-ichiro itojun Hagino 			    multidest(dst) && multidest(sc2->gif_pdst)) {
676686cdd19SJun-ichiro itojun Hagino 				error = EADDRNOTAVAIL;
677686cdd19SJun-ichiro itojun Hagino 				goto bad;
678cfa1ca9dSYoshinobu Inoue 			}
679686cdd19SJun-ichiro itojun Hagino #ifdef INET6
680686cdd19SJun-ichiro itojun Hagino 			if (dst->sa_family == AF_INET6 &&
681686cdd19SJun-ichiro itojun Hagino 			    multidest6(dst) && multidest6(sc2->gif_pdst)) {
682686cdd19SJun-ichiro itojun Hagino 				error = EADDRNOTAVAIL;
683686cdd19SJun-ichiro itojun Hagino 				goto bad;
684cfa1ca9dSYoshinobu Inoue 			}
685686cdd19SJun-ichiro itojun Hagino #endif
686686cdd19SJun-ichiro itojun Hagino 		}
687686cdd19SJun-ichiro itojun Hagino 
688686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc)
689686cdd19SJun-ichiro itojun Hagino 			free((caddr_t)sc->gif_psrc, M_IFADDR);
69033841545SHajimu UMEMOTO 		sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
69133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
692cfa1ca9dSYoshinobu Inoue 		sc->gif_psrc = sa;
693cfa1ca9dSYoshinobu Inoue 
694686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_pdst)
695686cdd19SJun-ichiro itojun Hagino 			free((caddr_t)sc->gif_pdst, M_IFADDR);
69633841545SHajimu UMEMOTO 		sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
69733841545SHajimu UMEMOTO 		bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
698cfa1ca9dSYoshinobu Inoue 		sc->gif_pdst = sa;
699cfa1ca9dSYoshinobu Inoue 
700a98dc5e6SYaroslav Tykhiy 		ifp->if_flags |= IFF_RUNNING;
701a98dc5e6SYaroslav Tykhiy 		s = splimp();
702a98dc5e6SYaroslav Tykhiy 		if_up(ifp);	/* mark interface UP and send up RTM_IFINFO */
703a98dc5e6SYaroslav Tykhiy 		splx(s);
704cfa1ca9dSYoshinobu Inoue 
705686cdd19SJun-ichiro itojun Hagino 		error = 0;
706cfa1ca9dSYoshinobu Inoue 		break;
707cfa1ca9dSYoshinobu Inoue 
708686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
709686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
710686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc) {
711686cdd19SJun-ichiro itojun Hagino 			free((caddr_t)sc->gif_psrc, M_IFADDR);
712686cdd19SJun-ichiro itojun Hagino 			sc->gif_psrc = NULL;
713686cdd19SJun-ichiro itojun Hagino 		}
714686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_pdst) {
715686cdd19SJun-ichiro itojun Hagino 			free((caddr_t)sc->gif_pdst, M_IFADDR);
716686cdd19SJun-ichiro itojun Hagino 			sc->gif_pdst = NULL;
717686cdd19SJun-ichiro itojun Hagino 		}
71833841545SHajimu UMEMOTO 		/* change the IFF_{UP, RUNNING} flag as well? */
719686cdd19SJun-ichiro itojun Hagino 		break;
720686cdd19SJun-ichiro itojun Hagino #endif
721686cdd19SJun-ichiro itojun Hagino 
722cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
723cfa1ca9dSYoshinobu Inoue #ifdef INET6
724cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
725cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
726cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
727cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
728cfa1ca9dSYoshinobu Inoue 			goto bad;
729cfa1ca9dSYoshinobu Inoue 		}
730cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
73133841545SHajimu UMEMOTO 		switch (cmd) {
732cfa1ca9dSYoshinobu Inoue #ifdef INET
73333841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
734cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
73533841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
736cfa1ca9dSYoshinobu Inoue 			break;
737cfa1ca9dSYoshinobu Inoue #endif /* INET */
738cfa1ca9dSYoshinobu Inoue #ifdef INET6
73933841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
740cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
741cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
74233841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
743cfa1ca9dSYoshinobu Inoue 			break;
744cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
745cfa1ca9dSYoshinobu Inoue 		default:
746cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
747cfa1ca9dSYoshinobu Inoue 			goto bad;
748cfa1ca9dSYoshinobu Inoue 		}
74933841545SHajimu UMEMOTO 		if (src->sa_len > size)
75033841545SHajimu UMEMOTO 			return EINVAL;
75133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
752cfa1ca9dSYoshinobu Inoue 		break;
753cfa1ca9dSYoshinobu Inoue 
754cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
755cfa1ca9dSYoshinobu Inoue #ifdef INET6
756cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
757cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
758cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
759cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
760cfa1ca9dSYoshinobu Inoue 			goto bad;
761cfa1ca9dSYoshinobu Inoue 		}
762cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
76333841545SHajimu UMEMOTO 		switch (cmd) {
764cfa1ca9dSYoshinobu Inoue #ifdef INET
76533841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
766cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
76733841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
768cfa1ca9dSYoshinobu Inoue 			break;
769cfa1ca9dSYoshinobu Inoue #endif /* INET */
770cfa1ca9dSYoshinobu Inoue #ifdef INET6
77133841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
772cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
773cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
77433841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
775cfa1ca9dSYoshinobu Inoue 			break;
776cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
777cfa1ca9dSYoshinobu Inoue 		default:
778cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
779cfa1ca9dSYoshinobu Inoue 			goto bad;
780cfa1ca9dSYoshinobu Inoue 		}
78133841545SHajimu UMEMOTO 		if (src->sa_len > size)
78233841545SHajimu UMEMOTO 			return EINVAL;
78333841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
78433841545SHajimu UMEMOTO 		break;
78533841545SHajimu UMEMOTO 
78633841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
78733841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
78833841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
78933841545SHajimu UMEMOTO 			goto bad;
79033841545SHajimu UMEMOTO 		}
79133841545SHajimu UMEMOTO 
79233841545SHajimu UMEMOTO 		/* copy src */
79333841545SHajimu UMEMOTO 		src = sc->gif_psrc;
79433841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
79533841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
79633841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
79733841545SHajimu UMEMOTO 		if (src->sa_len > size)
79833841545SHajimu UMEMOTO 			return EINVAL;
79933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
80033841545SHajimu UMEMOTO 
80133841545SHajimu UMEMOTO 		/* copy dst */
80233841545SHajimu UMEMOTO 		src = sc->gif_pdst;
80333841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
80433841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
80533841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
80633841545SHajimu UMEMOTO 		if (src->sa_len > size)
80733841545SHajimu UMEMOTO 			return EINVAL;
80833841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
809cfa1ca9dSYoshinobu Inoue 		break;
810cfa1ca9dSYoshinobu Inoue 
811cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
812686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
813cfa1ca9dSYoshinobu Inoue 		break;
814cfa1ca9dSYoshinobu Inoue 
815cfa1ca9dSYoshinobu Inoue 	default:
816cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
817cfa1ca9dSYoshinobu Inoue 		break;
818cfa1ca9dSYoshinobu Inoue 	}
819cfa1ca9dSYoshinobu Inoue  bad:
820cfa1ca9dSYoshinobu Inoue 	return error;
821cfa1ca9dSYoshinobu Inoue }
82253dab5feSBrooks Davis 
82353dab5feSBrooks Davis void
82453dab5feSBrooks Davis gif_delete_tunnel(sc)
82553dab5feSBrooks Davis 	struct gif_softc *sc;
82653dab5feSBrooks Davis {
82753dab5feSBrooks Davis 	/* XXX: NetBSD protects this function with splsoftnet() */
82853dab5feSBrooks Davis 
82953dab5feSBrooks Davis 	if (sc->gif_psrc) {
83053dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
83153dab5feSBrooks Davis 		sc->gif_psrc = NULL;
83253dab5feSBrooks Davis 	}
83353dab5feSBrooks Davis 	if (sc->gif_pdst) {
83453dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
83553dab5feSBrooks Davis 		sc->gif_pdst = NULL;
83653dab5feSBrooks Davis 	}
83753dab5feSBrooks Davis 	/* change the IFF_UP flag as well? */
83853dab5feSBrooks Davis }
839