xref: /freebsd/sys/net/if_gif.c (revision 4cd5f57d6bbd9c062c32e27a4cc58cd7f26080c2)
1686cdd19SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
288ff5695SSUZUKI Shinsuke /*	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $	*/
3686cdd19SJun-ichiro itojun Hagino 
4c398230bSWarner Losh /*-
5cfa1ca9dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6cfa1ca9dSYoshinobu Inoue  * All rights reserved.
7cfa1ca9dSYoshinobu Inoue  *
8cfa1ca9dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
9cfa1ca9dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
10cfa1ca9dSYoshinobu Inoue  * are met:
11cfa1ca9dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
12cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
13cfa1ca9dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
14cfa1ca9dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
15cfa1ca9dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
16cfa1ca9dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
17cfa1ca9dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
18cfa1ca9dSYoshinobu Inoue  *    without specific prior written permission.
19cfa1ca9dSYoshinobu Inoue  *
20cfa1ca9dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21cfa1ca9dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22cfa1ca9dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23cfa1ca9dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24cfa1ca9dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25cfa1ca9dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26cfa1ca9dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27cfa1ca9dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28cfa1ca9dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29cfa1ca9dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30cfa1ca9dSYoshinobu Inoue  * SUCH DAMAGE.
31cfa1ca9dSYoshinobu Inoue  */
32cfa1ca9dSYoshinobu Inoue 
33cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
35cfa1ca9dSYoshinobu Inoue 
36cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
37cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
38cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
415dba30f1SPoul-Henning Kamp #include <sys/module.h>
42cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
43cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/time.h>
46872f786aSBrooks Davis #include <sys/sysctl.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/syslog.h>
48dbe59260SHiroki Sato #include <sys/priv.h>
498b07e49aSJulian Elischer #include <sys/proc.h>
50686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5153dab5feSBrooks Davis #include <sys/conf.h>
52603724d3SBjoern A. Zeeb #include <sys/vimage.h>
53cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
54cfa1ca9dSYoshinobu Inoue 
55cfa1ca9dSYoshinobu Inoue #include <net/if.h>
56f889d2efSBrooks Davis #include <net/if_clone.h>
57cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
58cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
59cfa1ca9dSYoshinobu Inoue #include <net/route.h>
60cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
61cfa1ca9dSYoshinobu Inoue 
62cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
63cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
64cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
6533841545SHajimu UMEMOTO #ifdef	INET
6633841545SHajimu UMEMOTO #include <netinet/in_var.h>
67cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
6853dab5feSBrooks Davis #include <netinet/ip_var.h>
69cfa1ca9dSYoshinobu Inoue #endif	/* INET */
70cfa1ca9dSYoshinobu Inoue 
71cfa1ca9dSYoshinobu Inoue #ifdef INET6
72cfa1ca9dSYoshinobu Inoue #ifndef INET
73cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
74cfa1ca9dSYoshinobu Inoue #endif
75cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
76cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
77cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
78a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
79cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
80686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
81cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
82cfa1ca9dSYoshinobu Inoue 
83686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
8473ff045cSAndrew Thompson #include <net/ethernet.h>
8573ff045cSAndrew Thompson #include <net/if_bridgevar.h>
86cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
87cfa1ca9dSYoshinobu Inoue 
88aed55708SRobert Watson #include <security/mac/mac_framework.h>
89aed55708SRobert Watson 
9053dab5feSBrooks Davis #define GIFNAME		"gif"
91686cdd19SJun-ichiro itojun Hagino 
9217d5cb2dSRobert Watson /*
938c7e1947SRuslan Ermilov  * gif_mtx protects the global gif_softc_list.
9417d5cb2dSRobert Watson  */
9517d5cb2dSRobert Watson static struct mtx gif_mtx;
9653dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
9744e33a07SMarko Zec 
98385195c0SMarko Zec #ifndef VIMAGE
99385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
100385195c0SMarko Zec struct vnet_gif vnet_gif_0;
101385195c0SMarko Zec #endif
102385195c0SMarko Zec #endif
103385195c0SMarko Zec 
10444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
105e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
10644e33a07SMarko Zec static int max_gif_nesting;
10744e33a07SMarko Zec static int parallel_tunnels;
10844e33a07SMarko Zec #ifdef INET
10944e33a07SMarko Zec int ip_gif_ttl;
11044e33a07SMarko Zec #endif
11144e33a07SMarko Zec #ifdef INET6
11244e33a07SMarko Zec int ip6_gif_hlim;
11344e33a07SMarko Zec #endif
11444e33a07SMarko Zec #endif
11553dab5feSBrooks Davis 
11694408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
11794408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
11894408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
11994408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
12094408d94SBrooks Davis 
12173ff045cSAndrew Thompson static void	gif_start(struct ifnet *);
1226b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
123bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
1241ed81b73SMarko Zec static int	vnet_gif_iattach(const void *);
12553dab5feSBrooks Davis 
126bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
127bfe1aba4SMarko Zec static const vnet_modinfo_t vnet_gif_modinfo = {
128bfe1aba4SMarko Zec 	.vmi_id		= VNET_MOD_GIF,
129bfe1aba4SMarko Zec 	.vmi_name	= "gif",
130f6dfe47aSMarko Zec 	.vmi_size	= sizeof(struct vnet_gif),
131bfe1aba4SMarko Zec 	.vmi_dependson	= VNET_MOD_NET,
132bfe1aba4SMarko Zec 	.vmi_iattach	= vnet_gif_iattach
133bfe1aba4SMarko Zec };
134bfe1aba4SMarko Zec #endif
135bfe1aba4SMarko Zec 
136f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0);
13753dab5feSBrooks Davis 
138929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
139cfa1ca9dSYoshinobu Inoue 
140872f786aSBrooks Davis SYSCTL_DECL(_net_link);
141872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
142872f786aSBrooks Davis     "Generic Tunnel Interface");
143686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
144686cdd19SJun-ichiro itojun Hagino /*
145872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
146686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
147686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
148686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
149686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
150686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
151686cdd19SJun-ichiro itojun Hagino  */
152686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
153686cdd19SJun-ichiro itojun Hagino #endif
1548b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting,
1558b615593SMarko Zec     CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels");
1568b615593SMarko Zec 
1578b615593SMarko Zec #ifdef INET6
1588b615593SMarko Zec SYSCTL_DECL(_net_inet6_ip6);
1598b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM,
1608b615593SMarko Zec     gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, "");
1618b615593SMarko Zec #endif
162872f786aSBrooks Davis 
163872f786aSBrooks Davis /*
164872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
165872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
166872f786aSBrooks Davis  * we allow control over this check here.
167872f786aSBrooks Davis  */
1688b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels,
1698b615593SMarko Zec     CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?");
170cfa1ca9dSYoshinobu Inoue 
17156abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
17256abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
17356abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
17456abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
17556abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
17656abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
17756abdd33SAndrew Thompson #endif
17856abdd33SAndrew Thompson 
179bb2bfb4fSBrooks Davis static int
1806b7330e2SSam Leffler gif_clone_create(ifc, unit, params)
18153dab5feSBrooks Davis 	struct if_clone *ifc;
1823b16e7b2SMaxime Henrion 	int unit;
1836b7330e2SSam Leffler 	caddr_t params;
184cfa1ca9dSYoshinobu Inoue {
1858b615593SMarko Zec 	INIT_VNET_GIF(curvnet);
18633841545SHajimu UMEMOTO 	struct gif_softc *sc;
187cfa1ca9dSYoshinobu Inoue 
188e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1898b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
190fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
191fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
192fc74a9f9SBrooks Davis 		free(sc, M_GIF);
193fc74a9f9SBrooks Davis 		return (ENOSPC);
194fc74a9f9SBrooks Davis 	}
19553dab5feSBrooks Davis 
19625af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
19725af0bb5SGleb Smirnoff 
198fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
199fc74a9f9SBrooks Davis 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
200686cdd19SJun-ichiro itojun Hagino 
2019426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
202dbe59260SHiroki Sato 	sc->gif_options = GIF_ACCEPT_REVETHIP;
2039426aedfSHajimu UMEMOTO 
204fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
205fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
206fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
20733841545SHajimu UMEMOTO #if 0
20833841545SHajimu UMEMOTO 	/* turn off ingress filter */
209fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
21033841545SHajimu UMEMOTO #endif
211fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
21273ff045cSAndrew Thompson 	GIF2IFP(sc)->if_start  = gif_start;
213fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
214fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
215fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
21601399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
21794408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
218fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
21925af0bb5SGleb Smirnoff 
22025af0bb5SGleb Smirnoff 	mtx_lock(&gif_mtx);
221603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
22225af0bb5SGleb Smirnoff 	mtx_unlock(&gif_mtx);
22325af0bb5SGleb Smirnoff 
22425af0bb5SGleb Smirnoff 	return (0);
225cfa1ca9dSYoshinobu Inoue }
226cfa1ca9dSYoshinobu Inoue 
22717d5cb2dSRobert Watson static void
228febd0759SAndrew Thompson gif_clone_destroy(ifp)
229febd0759SAndrew Thompson 	struct ifnet *ifp;
23053dab5feSBrooks Davis {
231e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6)
23253dab5feSBrooks Davis 	int err;
233e0de57f9SBjoern A. Zeeb #endif
234febd0759SAndrew Thompson 	struct gif_softc *sc = ifp->if_softc;
235febd0759SAndrew Thompson 
236febd0759SAndrew Thompson 	mtx_lock(&gif_mtx);
237febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
238febd0759SAndrew Thompson 	mtx_unlock(&gif_mtx);
23953dab5feSBrooks Davis 
24017d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
2419426aedfSHajimu UMEMOTO #ifdef INET6
24253dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
24353dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
24453dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
24553dab5feSBrooks Davis 	}
2469426aedfSHajimu UMEMOTO #endif
2479426aedfSHajimu UMEMOTO #ifdef INET
2489426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2499426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2509426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2519426aedfSHajimu UMEMOTO 	}
2529426aedfSHajimu UMEMOTO #endif
25353dab5feSBrooks Davis 
25494408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
25594408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
25653dab5feSBrooks Davis 	bpfdetach(ifp);
25753dab5feSBrooks Davis 	if_detach(ifp);
258fc74a9f9SBrooks Davis 	if_free(ifp);
25953dab5feSBrooks Davis 
26025af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
26125af0bb5SGleb Smirnoff 
26253dab5feSBrooks Davis 	free(sc, M_GIF);
26353dab5feSBrooks Davis }
26453dab5feSBrooks Davis 
26553dab5feSBrooks Davis static int
2661ed81b73SMarko Zec vnet_gif_iattach(const void *unused __unused)
2671ed81b73SMarko Zec {
2681ed81b73SMarko Zec 	INIT_VNET_GIF(curvnet);
2691ed81b73SMarko Zec 
2701ed81b73SMarko Zec 	LIST_INIT(&V_gif_softc_list);
2711ed81b73SMarko Zec 	V_max_gif_nesting = MAX_GIF_NEST;
2721ed81b73SMarko Zec #ifdef XBONEHACK
2731ed81b73SMarko Zec 	V_parallel_tunnels = 1;
2741ed81b73SMarko Zec #else
2751ed81b73SMarko Zec 	V_parallel_tunnels = 0;
2761ed81b73SMarko Zec #endif
2771ed81b73SMarko Zec 	V_ip_gif_ttl = GIF_TTL;
2781ed81b73SMarko Zec #ifdef INET6
2791ed81b73SMarko Zec 	V_ip6_gif_hlim = GIF_HLIM;
2801ed81b73SMarko Zec #endif
2811ed81b73SMarko Zec 
2821ed81b73SMarko Zec 	return (0);
2831ed81b73SMarko Zec }
2841ed81b73SMarko Zec 
2851ed81b73SMarko Zec static int
28653dab5feSBrooks Davis gifmodevent(mod, type, data)
28753dab5feSBrooks Davis 	module_t mod;
28853dab5feSBrooks Davis 	int type;
28953dab5feSBrooks Davis 	void *data;
29053dab5feSBrooks Davis {
29153dab5feSBrooks Davis 
29253dab5feSBrooks Davis 	switch (type) {
29353dab5feSBrooks Davis 	case MOD_LOAD:
29417d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
29553dab5feSBrooks Davis 
296bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
297bfe1aba4SMarko Zec 		vnet_mod_register(&vnet_gif_modinfo);
298bfe1aba4SMarko Zec #else
2991ed81b73SMarko Zec 		vnet_gif_iattach(NULL);
300bfe1aba4SMarko Zec #endif
30144e33a07SMarko Zec 		if_clone_attach(&gif_cloner);
30253dab5feSBrooks Davis 
30353dab5feSBrooks Davis 		break;
30453dab5feSBrooks Davis 	case MOD_UNLOAD:
30553dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
306bc29160dSMarko Zec #ifdef VIMAGE
307bc29160dSMarko Zec 		vnet_mod_deregister(&vnet_gif_modinfo);
308bc29160dSMarko Zec #endif
30917d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
31053dab5feSBrooks Davis 		break;
3113e019deaSPoul-Henning Kamp 	default:
3123e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
31353dab5feSBrooks Davis 	}
31453dab5feSBrooks Davis 	return 0;
31553dab5feSBrooks Davis }
31653dab5feSBrooks Davis 
31753dab5feSBrooks Davis static moduledata_t gif_mod = {
31853dab5feSBrooks Davis 	"if_gif",
31953dab5feSBrooks Davis 	gifmodevent,
32053dab5feSBrooks Davis 	0
32153dab5feSBrooks Davis };
32253dab5feSBrooks Davis 
32353dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
32420af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
325cfa1ca9dSYoshinobu Inoue 
3269426aedfSHajimu UMEMOTO int
327686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
328686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
329686cdd19SJun-ichiro itojun Hagino 	int off;
330686cdd19SJun-ichiro itojun Hagino 	int proto;
331686cdd19SJun-ichiro itojun Hagino 	void *arg;
332686cdd19SJun-ichiro itojun Hagino {
333686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
334686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
335686cdd19SJun-ichiro itojun Hagino 
336686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
337686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
338686cdd19SJun-ichiro itojun Hagino 		return 0;
339686cdd19SJun-ichiro itojun Hagino 
340fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
341686cdd19SJun-ichiro itojun Hagino 		return 0;
342686cdd19SJun-ichiro itojun Hagino 
343686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
344686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
345686cdd19SJun-ichiro itojun Hagino 		return 0;
346686cdd19SJun-ichiro itojun Hagino 
347686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
348686cdd19SJun-ichiro itojun Hagino #ifdef INET
349686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
350686cdd19SJun-ichiro itojun Hagino 		break;
351686cdd19SJun-ichiro itojun Hagino #endif
352686cdd19SJun-ichiro itojun Hagino #ifdef INET6
353686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
354686cdd19SJun-ichiro itojun Hagino 		break;
355686cdd19SJun-ichiro itojun Hagino #endif
35673ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
35773ff045cSAndrew Thompson 		break;
35873ff045cSAndrew Thompson 
359686cdd19SJun-ichiro itojun Hagino 	default:
360686cdd19SJun-ichiro itojun Hagino 		return 0;
361686cdd19SJun-ichiro itojun Hagino 	}
362686cdd19SJun-ichiro itojun Hagino 
3633bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3643bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3653bb61ca6SHajimu UMEMOTO 		return 0;
3663bb61ca6SHajimu UMEMOTO 
3676f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
368686cdd19SJun-ichiro itojun Hagino 
369686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
370686cdd19SJun-ichiro itojun Hagino #ifdef INET
371686cdd19SJun-ichiro itojun Hagino 	case 4:
372686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
373686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
374686cdd19SJun-ichiro itojun Hagino 			return 0;
375686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
376686cdd19SJun-ichiro itojun Hagino #endif
377686cdd19SJun-ichiro itojun Hagino #ifdef INET6
378686cdd19SJun-ichiro itojun Hagino 	case 6:
3799426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3809426aedfSHajimu UMEMOTO 			return 0;
381686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
382686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
383686cdd19SJun-ichiro itojun Hagino 			return 0;
384686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
385686cdd19SJun-ichiro itojun Hagino #endif
386686cdd19SJun-ichiro itojun Hagino 	default:
387686cdd19SJun-ichiro itojun Hagino 		return 0;
388686cdd19SJun-ichiro itojun Hagino 	}
389686cdd19SJun-ichiro itojun Hagino }
390686cdd19SJun-ichiro itojun Hagino 
39173ff045cSAndrew Thompson static void
39273ff045cSAndrew Thompson gif_start(struct ifnet *ifp)
39373ff045cSAndrew Thompson {
39473ff045cSAndrew Thompson 	struct gif_softc *sc;
39573ff045cSAndrew Thompson 	struct mbuf *m;
39673ff045cSAndrew Thompson 
39773ff045cSAndrew Thompson 	sc = ifp->if_softc;
39873ff045cSAndrew Thompson 
39973ff045cSAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
40073ff045cSAndrew Thompson 	for (;;) {
40173ff045cSAndrew Thompson 		IFQ_DEQUEUE(&ifp->if_snd, m);
40273ff045cSAndrew Thompson 		if (m == 0)
40373ff045cSAndrew Thompson 			break;
40473ff045cSAndrew Thompson 
40573ff045cSAndrew Thompson 		gif_output(ifp, m, sc->gif_pdst, NULL);
40673ff045cSAndrew Thompson 
40773ff045cSAndrew Thompson 	}
40873ff045cSAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
40973ff045cSAndrew Thompson 
41073ff045cSAndrew Thompson 	return;
41173ff045cSAndrew Thompson }
41273ff045cSAndrew Thompson 
413cfa1ca9dSYoshinobu Inoue int
414279aa3d4SKip Macy gif_output(ifp, m, dst, ro)
415cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
416cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
417cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
418279aa3d4SKip Macy 	struct route *ro;
419cfa1ca9dSYoshinobu Inoue {
4208b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
421fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
4228c7e1947SRuslan Ermilov 	struct m_tag *mtag;
423cfa1ca9dSYoshinobu Inoue 	int error = 0;
4248c7e1947SRuslan Ermilov 	int gif_called;
42501399f34SDavid Malone 	u_int32_t af;
426cfa1ca9dSYoshinobu Inoue 
42710722b85SRobert Watson #ifdef MAC
42830d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
429e0852ce2SRobert Watson 	if (error) {
430e0852ce2SRobert Watson 		m_freem(m);
431e0852ce2SRobert Watson 		goto end;
432e0852ce2SRobert Watson 	}
43310722b85SRobert Watson #endif
43410722b85SRobert Watson 
435cfa1ca9dSYoshinobu Inoue 	/*
436cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
4378c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
4388c7e1947SRuslan Ermilov 	 *
4398c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
440cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
441cfa1ca9dSYoshinobu Inoue 	 */
4428c7e1947SRuslan Ermilov 	gif_called = 1;
4438c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
4448c7e1947SRuslan Ermilov 	while (mtag != NULL) {
4458c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4468c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
4478c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
4488c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
4498c7e1947SRuslan Ermilov 			m_freem(m);
4508c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
4518c7e1947SRuslan Ermilov 			goto end;
4528c7e1947SRuslan Ermilov 		}
4538c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
4548c7e1947SRuslan Ermilov 		gif_called++;
4558c7e1947SRuslan Ermilov 	}
456603724d3SBjoern A. Zeeb 	if (gif_called > V_max_gif_nesting) {
457cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
458cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
459523ebc4eSRobert Watson 		    gif_called);
460cfa1ca9dSYoshinobu Inoue 		m_freem(m);
461cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
462cfa1ca9dSYoshinobu Inoue 		goto end;
463cfa1ca9dSYoshinobu Inoue 	}
4648c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4658c7e1947SRuslan Ermilov 	    M_NOWAIT);
4668c7e1947SRuslan Ermilov 	if (mtag == NULL) {
4678c7e1947SRuslan Ermilov 		m_freem(m);
4688c7e1947SRuslan Ermilov 		error = ENOMEM;
4698c7e1947SRuslan Ermilov 		goto end;
4708c7e1947SRuslan Ermilov 	}
4718c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
4728c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
473686cdd19SJun-ichiro itojun Hagino 
474cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
47525af0bb5SGleb Smirnoff 
47625af0bb5SGleb Smirnoff 	GIF_LOCK(sc);
47725af0bb5SGleb Smirnoff 
478cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
479cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
4806e860629SGleb Smirnoff 		GIF_UNLOCK(sc);
481cfa1ca9dSYoshinobu Inoue 		m_freem(m);
482cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
483cfa1ca9dSYoshinobu Inoue 		goto end;
484cfa1ca9dSYoshinobu Inoue 	}
485cfa1ca9dSYoshinobu Inoue 
48601399f34SDavid Malone 	/* BPF writes need to be handled specially. */
48701399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
48801399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
48901399f34SDavid Malone 		dst->sa_family = af;
49001399f34SDavid Malone 	}
49101399f34SDavid Malone 
49201399f34SDavid Malone 	af = dst->sa_family;
49316d878ccSChristian S.J. Peron 	BPF_MTAP2(ifp, &af, sizeof(af), m);
494cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
495cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
496cfa1ca9dSYoshinobu Inoue 
49773ff045cSAndrew Thompson 	/* override to IPPROTO_ETHERIP for bridged traffic */
49873ff045cSAndrew Thompson 	if (ifp->if_bridge)
49973ff045cSAndrew Thompson 		af = AF_LINK;
50073ff045cSAndrew Thompson 
5018b07e49aSJulian Elischer 	M_SETFIB(m, sc->gif_fibnum);
50233841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
50333841545SHajimu UMEMOTO 
504686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
505686cdd19SJun-ichiro itojun Hagino 
50633841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
507cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
508cfa1ca9dSYoshinobu Inoue #ifdef INET
509cfa1ca9dSYoshinobu Inoue 	case AF_INET:
51073ff045cSAndrew Thompson 		error = in_gif_output(ifp, af, m);
511cfa1ca9dSYoshinobu Inoue 		break;
512cfa1ca9dSYoshinobu Inoue #endif
513cfa1ca9dSYoshinobu Inoue #ifdef INET6
514cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
51573ff045cSAndrew Thompson 		error = in6_gif_output(ifp, af, m);
516cfa1ca9dSYoshinobu Inoue 		break;
517cfa1ca9dSYoshinobu Inoue #endif
518cfa1ca9dSYoshinobu Inoue 	default:
519cfa1ca9dSYoshinobu Inoue 		m_freem(m);
520cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
521cfa1ca9dSYoshinobu Inoue 	}
522cfa1ca9dSYoshinobu Inoue 
5236e860629SGleb Smirnoff 	GIF_UNLOCK(sc);
524cfa1ca9dSYoshinobu Inoue   end:
52533841545SHajimu UMEMOTO 	if (error)
52633841545SHajimu UMEMOTO 		ifp->if_oerrors++;
52725af0bb5SGleb Smirnoff 	return (error);
528cfa1ca9dSYoshinobu Inoue }
529cfa1ca9dSYoshinobu Inoue 
530cfa1ca9dSYoshinobu Inoue void
53121fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
532cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
533cfa1ca9dSYoshinobu Inoue 	int af;
53421fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
535cfa1ca9dSYoshinobu Inoue {
53673ff045cSAndrew Thompson 	int isr, n;
537dbe59260SHiroki Sato 	struct gif_softc *sc = ifp->if_softc;
53873ff045cSAndrew Thompson 	struct etherip_header *eip;
53956abdd33SAndrew Thompson 	struct ether_header *eh;
54056abdd33SAndrew Thompson 	struct ifnet *oldifp;
541cfa1ca9dSYoshinobu Inoue 
54221fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
543cfa1ca9dSYoshinobu Inoue 		/* just in case */
544cfa1ca9dSYoshinobu Inoue 		m_freem(m);
545cfa1ca9dSYoshinobu Inoue 		return;
546cfa1ca9dSYoshinobu Inoue 	}
547cfa1ca9dSYoshinobu Inoue 
54821fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
549cfa1ca9dSYoshinobu Inoue 
55010722b85SRobert Watson #ifdef MAC
55130d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
55210722b85SRobert Watson #endif
55310722b85SRobert Watson 
55416d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
55533841545SHajimu UMEMOTO 		u_int32_t af1 = af;
556437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
557cfa1ca9dSYoshinobu Inoue 	}
558cfa1ca9dSYoshinobu Inoue 
55994408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
56021fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
56194408d94SBrooks Davis 		if (m == NULL)
56294408d94SBrooks Davis 			return;
56394408d94SBrooks Davis 	}
56494408d94SBrooks Davis 
565cfa1ca9dSYoshinobu Inoue 	/*
566cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
567cfa1ca9dSYoshinobu Inoue 	 * specified address family.
568cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
569cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
570cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
571cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
572cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
57388ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
57488ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
575cfa1ca9dSYoshinobu Inoue 	 */
576cfa1ca9dSYoshinobu Inoue 	switch (af) {
577cfa1ca9dSYoshinobu Inoue #ifdef INET
578cfa1ca9dSYoshinobu Inoue 	case AF_INET:
579cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
580cfa1ca9dSYoshinobu Inoue 		break;
581cfa1ca9dSYoshinobu Inoue #endif
582cfa1ca9dSYoshinobu Inoue #ifdef INET6
583cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
584cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
585cfa1ca9dSYoshinobu Inoue 		break;
586cfa1ca9dSYoshinobu Inoue #endif
58773ff045cSAndrew Thompson 	case AF_LINK:
58873ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
58973ff045cSAndrew Thompson 		if (n > m->m_len) {
59073ff045cSAndrew Thompson 			m = m_pullup(m, n);
59173ff045cSAndrew Thompson 			if (m == NULL) {
59273ff045cSAndrew Thompson 				ifp->if_ierrors++;
59373ff045cSAndrew Thompson 				return;
59473ff045cSAndrew Thompson 			}
59573ff045cSAndrew Thompson 		}
59673ff045cSAndrew Thompson 
59773ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
598dbe59260SHiroki Sato 		/*
599dbe59260SHiroki Sato 		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
600dbe59260SHiroki Sato 		 * accepts an EtherIP packet with revered version field in
601dbe59260SHiroki Sato 		 * the header.  This is a knob for backward compatibility
602dbe59260SHiroki Sato 		 * with FreeBSD 7.2R or prior.
603dbe59260SHiroki Sato 		 */
604dbe59260SHiroki Sato 		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
605dbe59260SHiroki Sato 			if (eip->eip_resvl != ETHERIP_VERSION
606dbe59260SHiroki Sato 			    && eip->eip_ver != ETHERIP_VERSION) {
60773ff045cSAndrew Thompson 				/* discard unknown versions */
60873ff045cSAndrew Thompson 				m_freem(m);
60973ff045cSAndrew Thompson 				return;
61073ff045cSAndrew Thompson 			}
611dbe59260SHiroki Sato 		} else {
612dbe59260SHiroki Sato 			if (eip->eip_ver != ETHERIP_VERSION) {
613dbe59260SHiroki Sato 				/* discard unknown versions */
614dbe59260SHiroki Sato 				m_freem(m);
615dbe59260SHiroki Sato 				return;
616dbe59260SHiroki Sato 			}
617dbe59260SHiroki Sato 		}
61873ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
61973ff045cSAndrew Thompson 
62073ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
62173ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
62273ff045cSAndrew Thompson 
62356abdd33SAndrew Thompson 		if (ifp->if_bridge) {
62456abdd33SAndrew Thompson 			oldifp = ifp;
62556abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
62656abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
62756abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
62856abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
62956abdd33SAndrew Thompson 				else
63056abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
63156abdd33SAndrew Thompson 				ifp->if_imcasts++;
63256abdd33SAndrew Thompson 			}
63373ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
63473ff045cSAndrew Thompson 
63556abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
63656abdd33SAndrew Thompson 				/*
63756abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
63856abdd33SAndrew Thompson 				 * members for which the frame is addressed.
63956abdd33SAndrew Thompson 				 */
64056abdd33SAndrew Thompson 				ether_demux(ifp, m);
64156abdd33SAndrew Thompson 				return;
64256abdd33SAndrew Thompson 			}
64356abdd33SAndrew Thompson 		}
64473ff045cSAndrew Thompson 		if (m != NULL)
64573ff045cSAndrew Thompson 			m_freem(m);
64673ff045cSAndrew Thompson 		return;
64773ff045cSAndrew Thompson 
648cfa1ca9dSYoshinobu Inoue 	default:
64994408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
65021fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
65194408d94SBrooks Davis 		else
652cfa1ca9dSYoshinobu Inoue 			m_freem(m);
653cfa1ca9dSYoshinobu Inoue 		return;
654cfa1ca9dSYoshinobu Inoue 	}
655cfa1ca9dSYoshinobu Inoue 
65621fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
65721fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
6581cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
659cfa1ca9dSYoshinobu Inoue }
660cfa1ca9dSYoshinobu Inoue 
661686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
662cfa1ca9dSYoshinobu Inoue int
663cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
664cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
665cfa1ca9dSYoshinobu Inoue 	u_long cmd;
666cfa1ca9dSYoshinobu Inoue 	caddr_t data;
667cfa1ca9dSYoshinobu Inoue {
668fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
669cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
670cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
671dbe59260SHiroki Sato 	u_int	options;
672686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
6733bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
6743bb61ca6SHajimu UMEMOTO 	u_long mtu;
6753bb61ca6SHajimu UMEMOTO #endif
676cfa1ca9dSYoshinobu Inoue 
677cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
678cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6799426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
680cfa1ca9dSYoshinobu Inoue 		break;
681cfa1ca9dSYoshinobu Inoue 
682cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
683cfa1ca9dSYoshinobu Inoue 		break;
684cfa1ca9dSYoshinobu Inoue 
685cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
686cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
687cfa1ca9dSYoshinobu Inoue 		break;
688cfa1ca9dSYoshinobu Inoue 
689686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
690cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
691cfa1ca9dSYoshinobu Inoue 		break;
692686cdd19SJun-ichiro itojun Hagino 
693cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
694cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
6953bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
696cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
697cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
698cfa1ca9dSYoshinobu Inoue 		break;
699686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
700cfa1ca9dSYoshinobu Inoue 
7013bb61ca6SHajimu UMEMOTO #ifdef INET
702cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
7033bb61ca6SHajimu UMEMOTO #endif
704cfa1ca9dSYoshinobu Inoue #ifdef INET6
705cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
706cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
70733841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
708686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
70933841545SHajimu UMEMOTO #ifdef INET
710686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
711cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
712cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
713cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
714cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
715cfa1ca9dSYoshinobu Inoue 			break;
71633841545SHajimu UMEMOTO #endif
717cfa1ca9dSYoshinobu Inoue #ifdef INET6
718686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
719cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
720cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
721cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
722cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
723686cdd19SJun-ichiro itojun Hagino 			break;
724686cdd19SJun-ichiro itojun Hagino #endif
72533841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
72633841545SHajimu UMEMOTO 			src = (struct sockaddr *)
72733841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
72833841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
72933841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
7309426aedfSHajimu UMEMOTO 			break;
7316f4ded3aSBrooks Davis 		default:
7329426aedfSHajimu UMEMOTO 			return EINVAL;
73333841545SHajimu UMEMOTO 		}
73433841545SHajimu UMEMOTO 
73533841545SHajimu UMEMOTO 		/* sa_family must be equal */
73633841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
73733841545SHajimu UMEMOTO 			return EINVAL;
73833841545SHajimu UMEMOTO 
73933841545SHajimu UMEMOTO 		/* validate sa_len */
74033841545SHajimu UMEMOTO 		switch (src->sa_family) {
74133841545SHajimu UMEMOTO #ifdef INET
74233841545SHajimu UMEMOTO 		case AF_INET:
74333841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
74433841545SHajimu UMEMOTO 				return EINVAL;
74533841545SHajimu UMEMOTO 			break;
74633841545SHajimu UMEMOTO #endif
74733841545SHajimu UMEMOTO #ifdef INET6
74833841545SHajimu UMEMOTO 		case AF_INET6:
74933841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
75033841545SHajimu UMEMOTO 				return EINVAL;
75133841545SHajimu UMEMOTO 			break;
75233841545SHajimu UMEMOTO #endif
75333841545SHajimu UMEMOTO 		default:
75433841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
75533841545SHajimu UMEMOTO 		}
75633841545SHajimu UMEMOTO 		switch (dst->sa_family) {
75733841545SHajimu UMEMOTO #ifdef INET
75833841545SHajimu UMEMOTO 		case AF_INET:
75933841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
76033841545SHajimu UMEMOTO 				return EINVAL;
76133841545SHajimu UMEMOTO 			break;
76233841545SHajimu UMEMOTO #endif
76333841545SHajimu UMEMOTO #ifdef INET6
76433841545SHajimu UMEMOTO 		case AF_INET6:
76533841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
76633841545SHajimu UMEMOTO 				return EINVAL;
76733841545SHajimu UMEMOTO 			break;
76833841545SHajimu UMEMOTO #endif
76933841545SHajimu UMEMOTO 		default:
77033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
77133841545SHajimu UMEMOTO 		}
77233841545SHajimu UMEMOTO 
77333841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
77433841545SHajimu UMEMOTO 		switch (cmd) {
77533841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
77633841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
77733841545SHajimu UMEMOTO 				break;
77833841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
77933841545SHajimu UMEMOTO #ifdef INET6
78033841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
78133841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
78233841545SHajimu UMEMOTO 				break;
78333841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
78433841545SHajimu UMEMOTO #endif /* INET6 */
78533841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
78633841545SHajimu UMEMOTO 			/* checks done in the above */
78733841545SHajimu UMEMOTO 			break;
788686cdd19SJun-ichiro itojun Hagino 		}
789cfa1ca9dSYoshinobu Inoue 
790fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
791cfa1ca9dSYoshinobu Inoue 		break;
792cfa1ca9dSYoshinobu Inoue 
793686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
794686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
795fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
796686cdd19SJun-ichiro itojun Hagino 		break;
797686cdd19SJun-ichiro itojun Hagino #endif
798686cdd19SJun-ichiro itojun Hagino 
799cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
800cfa1ca9dSYoshinobu Inoue #ifdef INET6
801cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
802cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
803cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
804cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
805cfa1ca9dSYoshinobu Inoue 			goto bad;
806cfa1ca9dSYoshinobu Inoue 		}
807cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
80833841545SHajimu UMEMOTO 		switch (cmd) {
809cfa1ca9dSYoshinobu Inoue #ifdef INET
81033841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
811cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
81233841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
813cfa1ca9dSYoshinobu Inoue 			break;
814cfa1ca9dSYoshinobu Inoue #endif /* INET */
815cfa1ca9dSYoshinobu Inoue #ifdef INET6
81633841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
817cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
818cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
81933841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
820cfa1ca9dSYoshinobu Inoue 			break;
821cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
822cfa1ca9dSYoshinobu Inoue 		default:
823cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
824cfa1ca9dSYoshinobu Inoue 			goto bad;
825cfa1ca9dSYoshinobu Inoue 		}
82633841545SHajimu UMEMOTO 		if (src->sa_len > size)
82733841545SHajimu UMEMOTO 			return EINVAL;
82833841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
829a1f7e5f8SHajimu UMEMOTO #ifdef INET6
830a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
831a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
832a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
833a1f7e5f8SHajimu UMEMOTO 				return (error);
834a1f7e5f8SHajimu UMEMOTO 		}
835a1f7e5f8SHajimu UMEMOTO #endif
836cfa1ca9dSYoshinobu Inoue 		break;
837cfa1ca9dSYoshinobu Inoue 
838cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
839cfa1ca9dSYoshinobu Inoue #ifdef INET6
840cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
841cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
842cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
843cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
844cfa1ca9dSYoshinobu Inoue 			goto bad;
845cfa1ca9dSYoshinobu Inoue 		}
846cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
84733841545SHajimu UMEMOTO 		switch (cmd) {
848cfa1ca9dSYoshinobu Inoue #ifdef INET
84933841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
850cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
85133841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
852cfa1ca9dSYoshinobu Inoue 			break;
853cfa1ca9dSYoshinobu Inoue #endif /* INET */
854cfa1ca9dSYoshinobu Inoue #ifdef INET6
85533841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
856cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
857cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
85833841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
859cfa1ca9dSYoshinobu Inoue 			break;
860cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
861cfa1ca9dSYoshinobu Inoue 		default:
862cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
863cfa1ca9dSYoshinobu Inoue 			goto bad;
864cfa1ca9dSYoshinobu Inoue 		}
86533841545SHajimu UMEMOTO 		if (src->sa_len > size)
86633841545SHajimu UMEMOTO 			return EINVAL;
86733841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
868a1f7e5f8SHajimu UMEMOTO #ifdef INET6
869a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
870a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
871a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
872a1f7e5f8SHajimu UMEMOTO 				return (error);
873a1f7e5f8SHajimu UMEMOTO 		}
874a1f7e5f8SHajimu UMEMOTO #endif
87533841545SHajimu UMEMOTO 		break;
87633841545SHajimu UMEMOTO 
87733841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
87833841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
87933841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
88033841545SHajimu UMEMOTO 			goto bad;
88133841545SHajimu UMEMOTO 		}
88233841545SHajimu UMEMOTO 
88333841545SHajimu UMEMOTO 		/* copy src */
88433841545SHajimu UMEMOTO 		src = sc->gif_psrc;
88533841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
88633841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
88733841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
88833841545SHajimu UMEMOTO 		if (src->sa_len > size)
88933841545SHajimu UMEMOTO 			return EINVAL;
89033841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
89133841545SHajimu UMEMOTO 
89233841545SHajimu UMEMOTO 		/* copy dst */
89333841545SHajimu UMEMOTO 		src = sc->gif_pdst;
89433841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
89533841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
89633841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
89733841545SHajimu UMEMOTO 		if (src->sa_len > size)
89833841545SHajimu UMEMOTO 			return EINVAL;
89933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
900cfa1ca9dSYoshinobu Inoue 		break;
901cfa1ca9dSYoshinobu Inoue 
902cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
903686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
904cfa1ca9dSYoshinobu Inoue 		break;
905cfa1ca9dSYoshinobu Inoue 
906dbe59260SHiroki Sato 	case GIFGOPTS:
907dbe59260SHiroki Sato 		options = sc->gif_options;
908dbe59260SHiroki Sato 		error = copyout(&options, ifr->ifr_data,
909dbe59260SHiroki Sato 				sizeof(options));
910dbe59260SHiroki Sato 		break;
911dbe59260SHiroki Sato 
912dbe59260SHiroki Sato 	case GIFSOPTS:
913dbe59260SHiroki Sato 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
914dbe59260SHiroki Sato 			break;
9154cd5f57dSHiroki Sato 		error = copyin(ifr->ifr_data, &options, sizeof(options));
9164cd5f57dSHiroki Sato 		if (error)
9174cd5f57dSHiroki Sato 			break;
9184cd5f57dSHiroki Sato 		if (options & ~GIF_OPTMASK)
919dbe59260SHiroki Sato 			error = EINVAL;
9204cd5f57dSHiroki Sato 		else
9214cd5f57dSHiroki Sato 			sc->gif_options = options;
922dbe59260SHiroki Sato 		break;
923dbe59260SHiroki Sato 
924cfa1ca9dSYoshinobu Inoue 	default:
925cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
926cfa1ca9dSYoshinobu Inoue 		break;
927cfa1ca9dSYoshinobu Inoue 	}
928cfa1ca9dSYoshinobu Inoue  bad:
929cfa1ca9dSYoshinobu Inoue 	return error;
930cfa1ca9dSYoshinobu Inoue }
93153dab5feSBrooks Davis 
93217d5cb2dSRobert Watson /*
93317d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
93417d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
93517d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
93617d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
93717d5cb2dSRobert Watson  * a mutex.
93817d5cb2dSRobert Watson  */
9399426aedfSHajimu UMEMOTO int
9409426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
9419426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
9429426aedfSHajimu UMEMOTO 	struct sockaddr *src;
9439426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
94453dab5feSBrooks Davis {
9458b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
946fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
9479426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
9489426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
9499426aedfSHajimu UMEMOTO 	int error = 0;
9509426aedfSHajimu UMEMOTO 
95117d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
952603724d3SBjoern A. Zeeb 	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
9539426aedfSHajimu UMEMOTO 		if (sc2 == sc)
9549426aedfSHajimu UMEMOTO 			continue;
9559426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
9569426aedfSHajimu UMEMOTO 			continue;
9579426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
9589426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
9599426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
9609426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
9619426aedfSHajimu UMEMOTO 			continue;
9629426aedfSHajimu UMEMOTO 
9639426aedfSHajimu UMEMOTO 		/*
9649426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
9659426aedfSHajimu UMEMOTO 		 * otherwise.
9669426aedfSHajimu UMEMOTO 		 */
967603724d3SBjoern A. Zeeb 		if (!V_parallel_tunnels &&
9689426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
9699426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
9709426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
97117d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
9729426aedfSHajimu UMEMOTO 			goto bad;
9739426aedfSHajimu UMEMOTO 		}
9749426aedfSHajimu UMEMOTO 
9759426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
9769426aedfSHajimu UMEMOTO 	}
97717d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
9789426aedfSHajimu UMEMOTO 
9799426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
9809426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
9819426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
9829426aedfSHajimu UMEMOTO #ifdef INET
9839426aedfSHajimu UMEMOTO 		case AF_INET:
9849426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
9859426aedfSHajimu UMEMOTO 			break;
9869426aedfSHajimu UMEMOTO #endif
9879426aedfSHajimu UMEMOTO #ifdef INET6
9889426aedfSHajimu UMEMOTO 		case AF_INET6:
9899426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
9909426aedfSHajimu UMEMOTO 			break;
9919426aedfSHajimu UMEMOTO #endif
9929426aedfSHajimu UMEMOTO 		}
9939426aedfSHajimu UMEMOTO 
9949426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
995a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
9969426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
9979426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
9989426aedfSHajimu UMEMOTO 
9999426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
1000a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
10019426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
10029426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
10039426aedfSHajimu UMEMOTO 
10049426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
10059426aedfSHajimu UMEMOTO #ifdef INET
10069426aedfSHajimu UMEMOTO 	case AF_INET:
10079426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
10089426aedfSHajimu UMEMOTO 		break;
10099426aedfSHajimu UMEMOTO #endif
10109426aedfSHajimu UMEMOTO #ifdef INET6
10119426aedfSHajimu UMEMOTO 	case AF_INET6:
1012a1f7e5f8SHajimu UMEMOTO 		/*
1013a1f7e5f8SHajimu UMEMOTO 		 * Check validity of the scope zone ID of the addresses, and
1014a1f7e5f8SHajimu UMEMOTO 		 * convert it into the kernel internal form if necessary.
1015a1f7e5f8SHajimu UMEMOTO 		 */
1016a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1017a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
1018a1f7e5f8SHajimu UMEMOTO 			break;
1019a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1020a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
1021a1f7e5f8SHajimu UMEMOTO 			break;
10229426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
10239426aedfSHajimu UMEMOTO 		break;
10249426aedfSHajimu UMEMOTO #endif
10259426aedfSHajimu UMEMOTO 	}
10269426aedfSHajimu UMEMOTO 	if (error) {
10279426aedfSHajimu UMEMOTO 		/* rollback */
10289426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
10299426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
10309426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
10319426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
10329426aedfSHajimu UMEMOTO 		goto bad;
10339426aedfSHajimu UMEMOTO 	}
10349426aedfSHajimu UMEMOTO 
10359426aedfSHajimu UMEMOTO 	if (osrc)
10369426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
10379426aedfSHajimu UMEMOTO 	if (odst)
10389426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
10399426aedfSHajimu UMEMOTO 
10409426aedfSHajimu UMEMOTO  bad:
10419426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
104213f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
10439426aedfSHajimu UMEMOTO 	else
104413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
10459426aedfSHajimu UMEMOTO 
10469426aedfSHajimu UMEMOTO 	return error;
10479426aedfSHajimu UMEMOTO }
10489426aedfSHajimu UMEMOTO 
10499426aedfSHajimu UMEMOTO void
10509426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
10519426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
10529426aedfSHajimu UMEMOTO {
1053fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
105453dab5feSBrooks Davis 
105553dab5feSBrooks Davis 	if (sc->gif_psrc) {
105653dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
105753dab5feSBrooks Davis 		sc->gif_psrc = NULL;
105853dab5feSBrooks Davis 	}
105953dab5feSBrooks Davis 	if (sc->gif_pdst) {
106053dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
106153dab5feSBrooks Davis 		sc->gif_pdst = NULL;
106253dab5feSBrooks Davis 	}
10639426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
10649426aedfSHajimu UMEMOTO #ifdef INET
10659426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
10669426aedfSHajimu UMEMOTO #endif
10679426aedfSHajimu UMEMOTO #ifdef INET6
10689426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
10699426aedfSHajimu UMEMOTO #endif
107013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
107153dab5feSBrooks Davis }
1072