xref: /freebsd/sys/net/if_gif.c (revision 76039bc84fae9915788b54ff28fe0cc4876952d2)
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>
38e3416ab0SBjoern A. Zeeb #include <sys/jail.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/malloc.h>
41cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
425dba30f1SPoul-Henning Kamp #include <sys/module.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>
49dbe59260SHiroki Sato #include <sys/priv.h>
508b07e49aSJulian Elischer #include <sys/proc.h>
51686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
5253dab5feSBrooks Davis #include <sys/conf.h>
53cfa1ca9dSYoshinobu Inoue #include <machine/cpu.h>
54cfa1ca9dSYoshinobu Inoue 
55cfa1ca9dSYoshinobu Inoue #include <net/if.h>
56*76039bc8SGleb Smirnoff #include <net/if_var.h>
57f889d2efSBrooks Davis #include <net/if_clone.h>
58cfa1ca9dSYoshinobu Inoue #include <net/if_types.h>
59cfa1ca9dSYoshinobu Inoue #include <net/netisr.h>
60cfa1ca9dSYoshinobu Inoue #include <net/route.h>
61cfa1ca9dSYoshinobu Inoue #include <net/bpf.h>
62530c0060SRobert Watson #include <net/vnet.h>
63cfa1ca9dSYoshinobu Inoue 
64cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
65cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
66cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
6733841545SHajimu UMEMOTO #ifdef	INET
6833841545SHajimu UMEMOTO #include <netinet/in_var.h>
69cfa1ca9dSYoshinobu Inoue #include <netinet/in_gif.h>
7053dab5feSBrooks Davis #include <netinet/ip_var.h>
71cfa1ca9dSYoshinobu Inoue #endif	/* INET */
72cfa1ca9dSYoshinobu Inoue 
73cfa1ca9dSYoshinobu Inoue #ifdef INET6
74cfa1ca9dSYoshinobu Inoue #ifndef INET
75cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
76cfa1ca9dSYoshinobu Inoue #endif
77cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_var.h>
78cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
79cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
80a1f7e5f8SHajimu UMEMOTO #include <netinet6/scope6_var.h>
81cfa1ca9dSYoshinobu Inoue #include <netinet6/in6_gif.h>
82686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h>
83cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
84cfa1ca9dSYoshinobu Inoue 
85686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
8673ff045cSAndrew Thompson #include <net/ethernet.h>
8773ff045cSAndrew Thompson #include <net/if_bridgevar.h>
88cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
89cfa1ca9dSYoshinobu Inoue 
90aed55708SRobert Watson #include <security/mac/mac_framework.h>
91aed55708SRobert Watson 
9242a58907SGleb Smirnoff static const char gifname[] = "gif";
93686cdd19SJun-ichiro itojun Hagino 
9417d5cb2dSRobert Watson /*
958c7e1947SRuslan Ermilov  * gif_mtx protects the global gif_softc_list.
9617d5cb2dSRobert Watson  */
9717d5cb2dSRobert Watson static struct mtx gif_mtx;
9853dab5feSBrooks Davis static MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
993e288e62SDimitry Andric static VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
1001e77c105SRobert Watson #define	V_gif_softc_list	VNET(gif_softc_list)
101eddfbb76SRobert Watson 
10294408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
10394408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
10494408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
10594408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
10694408d94SBrooks Davis 
10773ff045cSAndrew Thompson static void	gif_start(struct ifnet *);
1086b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
109bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
11042a58907SGleb Smirnoff static struct if_clone *gif_cloner;
11153dab5feSBrooks Davis 
112929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
113cfa1ca9dSYoshinobu Inoue 
114872f786aSBrooks Davis SYSCTL_DECL(_net_link);
1156472ac3dSEd Schouten static SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
116872f786aSBrooks Davis     "Generic Tunnel Interface");
117686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
118686cdd19SJun-ichiro itojun Hagino /*
119872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
120686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
121686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
122686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
123686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
124686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
125686cdd19SJun-ichiro itojun Hagino  */
126686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
127686cdd19SJun-ichiro itojun Hagino #endif
1283e288e62SDimitry Andric static VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
129d0728d71SRobert Watson #define	V_max_gif_nesting	VNET(max_gif_nesting)
130eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
131eddfbb76SRobert Watson     &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
1328b615593SMarko Zec 
133872f786aSBrooks Davis /*
134872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
135872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
136872f786aSBrooks Davis  * we allow control over this check here.
137872f786aSBrooks Davis  */
138d0728d71SRobert Watson #ifdef XBONEHACK
1393e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 1;
140d0728d71SRobert Watson #else
1413e288e62SDimitry Andric static VNET_DEFINE(int, parallel_tunnels) = 0;
142d0728d71SRobert Watson #endif
143d0728d71SRobert Watson #define	V_parallel_tunnels	VNET(parallel_tunnels)
144eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
145eddfbb76SRobert Watson     &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
146cfa1ca9dSYoshinobu Inoue 
14756abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
14856abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
14956abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
15056abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
15156abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
15256abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
15356abdd33SAndrew Thompson #endif
15456abdd33SAndrew Thompson 
155bb2bfb4fSBrooks Davis static int
1566b7330e2SSam Leffler gif_clone_create(ifc, unit, params)
15753dab5feSBrooks Davis 	struct if_clone *ifc;
1583b16e7b2SMaxime Henrion 	int unit;
1596b7330e2SSam Leffler 	caddr_t params;
160cfa1ca9dSYoshinobu Inoue {
16133841545SHajimu UMEMOTO 	struct gif_softc *sc;
162cfa1ca9dSYoshinobu Inoue 
163e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1648b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
165fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
166fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
167fc74a9f9SBrooks Davis 		free(sc, M_GIF);
168fc74a9f9SBrooks Davis 		return (ENOSPC);
169fc74a9f9SBrooks Davis 	}
17053dab5feSBrooks Davis 
17125af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
17225af0bb5SGleb Smirnoff 
173fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
17442a58907SGleb Smirnoff 	if_initname(GIF2IFP(sc), gifname, unit);
175686cdd19SJun-ichiro itojun Hagino 
1769426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
177e9f947e2SHiroki Sato 	sc->gif_options = 0;
1789426aedfSHajimu UMEMOTO 
179fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
180fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
181fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
18233841545SHajimu UMEMOTO #if 0
18333841545SHajimu UMEMOTO 	/* turn off ingress filter */
184fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
18533841545SHajimu UMEMOTO #endif
186fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
18773ff045cSAndrew Thompson 	GIF2IFP(sc)->if_start  = gif_start;
188fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
189e50d35e6SMaxim Sobolev 	GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
190fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
19101399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
19294408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
193fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
19425af0bb5SGleb Smirnoff 
19525af0bb5SGleb Smirnoff 	mtx_lock(&gif_mtx);
196603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
19725af0bb5SGleb Smirnoff 	mtx_unlock(&gif_mtx);
19825af0bb5SGleb Smirnoff 
19925af0bb5SGleb Smirnoff 	return (0);
200cfa1ca9dSYoshinobu Inoue }
201cfa1ca9dSYoshinobu Inoue 
20217d5cb2dSRobert Watson static void
203febd0759SAndrew Thompson gif_clone_destroy(ifp)
204febd0759SAndrew Thompson 	struct ifnet *ifp;
20553dab5feSBrooks Davis {
206e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6)
20753dab5feSBrooks Davis 	int err;
208e0de57f9SBjoern A. Zeeb #endif
209febd0759SAndrew Thompson 	struct gif_softc *sc = ifp->if_softc;
210febd0759SAndrew Thompson 
211febd0759SAndrew Thompson 	mtx_lock(&gif_mtx);
212febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
213febd0759SAndrew Thompson 	mtx_unlock(&gif_mtx);
21453dab5feSBrooks Davis 
21517d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
2169426aedfSHajimu UMEMOTO #ifdef INET6
21753dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
21853dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
21953dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
22053dab5feSBrooks Davis 	}
2219426aedfSHajimu UMEMOTO #endif
2229426aedfSHajimu UMEMOTO #ifdef INET
2239426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2249426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2259426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2269426aedfSHajimu UMEMOTO 	}
2279426aedfSHajimu UMEMOTO #endif
22853dab5feSBrooks Davis 
22994408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
23094408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
23153dab5feSBrooks Davis 	bpfdetach(ifp);
23253dab5feSBrooks Davis 	if_detach(ifp);
233fc74a9f9SBrooks Davis 	if_free(ifp);
23453dab5feSBrooks Davis 
23525af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
23625af0bb5SGleb Smirnoff 
23753dab5feSBrooks Davis 	free(sc, M_GIF);
23853dab5feSBrooks Davis }
23953dab5feSBrooks Davis 
240d0728d71SRobert Watson static void
241d0728d71SRobert Watson vnet_gif_init(const void *unused __unused)
2421ed81b73SMarko Zec {
2431ed81b73SMarko Zec 
2441ed81b73SMarko Zec 	LIST_INIT(&V_gif_softc_list);
2451ed81b73SMarko Zec }
246d0728d71SRobert Watson VNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init,
247d0728d71SRobert Watson     NULL);
2481ed81b73SMarko Zec 
2491ed81b73SMarko Zec static int
25053dab5feSBrooks Davis gifmodevent(mod, type, data)
25153dab5feSBrooks Davis 	module_t mod;
25253dab5feSBrooks Davis 	int type;
25353dab5feSBrooks Davis 	void *data;
25453dab5feSBrooks Davis {
25553dab5feSBrooks Davis 
25653dab5feSBrooks Davis 	switch (type) {
25753dab5feSBrooks Davis 	case MOD_LOAD:
25817d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
25942a58907SGleb Smirnoff 		gif_cloner = if_clone_simple(gifname, gif_clone_create,
26042a58907SGleb Smirnoff 		    gif_clone_destroy, 0);
26153dab5feSBrooks Davis 		break;
262d0728d71SRobert Watson 
26353dab5feSBrooks Davis 	case MOD_UNLOAD:
26442a58907SGleb Smirnoff 		if_clone_detach(gif_cloner);
26517d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
26653dab5feSBrooks Davis 		break;
2673e019deaSPoul-Henning Kamp 	default:
2683e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
26953dab5feSBrooks Davis 	}
27053dab5feSBrooks Davis 	return 0;
27153dab5feSBrooks Davis }
27253dab5feSBrooks Davis 
27353dab5feSBrooks Davis static moduledata_t gif_mod = {
27453dab5feSBrooks Davis 	"if_gif",
27553dab5feSBrooks Davis 	gifmodevent,
2769823d527SKevin Lo 	0
27753dab5feSBrooks Davis };
27853dab5feSBrooks Davis 
27953dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
28020af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
281cfa1ca9dSYoshinobu Inoue 
2829426aedfSHajimu UMEMOTO int
283686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
284686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
285686cdd19SJun-ichiro itojun Hagino 	int off;
286686cdd19SJun-ichiro itojun Hagino 	int proto;
287686cdd19SJun-ichiro itojun Hagino 	void *arg;
288686cdd19SJun-ichiro itojun Hagino {
289686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
290686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
291686cdd19SJun-ichiro itojun Hagino 
292686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
293686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
294686cdd19SJun-ichiro itojun Hagino 		return 0;
295686cdd19SJun-ichiro itojun Hagino 
296fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
297686cdd19SJun-ichiro itojun Hagino 		return 0;
298686cdd19SJun-ichiro itojun Hagino 
299686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
300686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
301686cdd19SJun-ichiro itojun Hagino 		return 0;
302686cdd19SJun-ichiro itojun Hagino 
303686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
304686cdd19SJun-ichiro itojun Hagino #ifdef INET
305686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
306686cdd19SJun-ichiro itojun Hagino 		break;
307686cdd19SJun-ichiro itojun Hagino #endif
308686cdd19SJun-ichiro itojun Hagino #ifdef INET6
309686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
310686cdd19SJun-ichiro itojun Hagino 		break;
311686cdd19SJun-ichiro itojun Hagino #endif
31273ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
31373ff045cSAndrew Thompson 		break;
31473ff045cSAndrew Thompson 
315686cdd19SJun-ichiro itojun Hagino 	default:
316686cdd19SJun-ichiro itojun Hagino 		return 0;
317686cdd19SJun-ichiro itojun Hagino 	}
318686cdd19SJun-ichiro itojun Hagino 
3193bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3203bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3213bb61ca6SHajimu UMEMOTO 		return 0;
3223bb61ca6SHajimu UMEMOTO 
3236f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
324686cdd19SJun-ichiro itojun Hagino 
325686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
326686cdd19SJun-ichiro itojun Hagino #ifdef INET
327686cdd19SJun-ichiro itojun Hagino 	case 4:
328686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
329686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
330686cdd19SJun-ichiro itojun Hagino 			return 0;
331686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
332686cdd19SJun-ichiro itojun Hagino #endif
333686cdd19SJun-ichiro itojun Hagino #ifdef INET6
334686cdd19SJun-ichiro itojun Hagino 	case 6:
3359426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3369426aedfSHajimu UMEMOTO 			return 0;
337686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
338686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
339686cdd19SJun-ichiro itojun Hagino 			return 0;
340686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
341686cdd19SJun-ichiro itojun Hagino #endif
342686cdd19SJun-ichiro itojun Hagino 	default:
343686cdd19SJun-ichiro itojun Hagino 		return 0;
344686cdd19SJun-ichiro itojun Hagino 	}
345686cdd19SJun-ichiro itojun Hagino }
346776b7288SRandall Stewart #ifdef INET
347776b7288SRandall Stewart #define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip))
348776b7288SRandall Stewart #endif
349776b7288SRandall Stewart #ifdef INET6
350776b7288SRandall Stewart #define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr))
351776b7288SRandall Stewart #endif
352686cdd19SJun-ichiro itojun Hagino 
35373ff045cSAndrew Thompson static void
35473ff045cSAndrew Thompson gif_start(struct ifnet *ifp)
35573ff045cSAndrew Thompson {
35673ff045cSAndrew Thompson 	struct gif_softc *sc;
35773ff045cSAndrew Thompson 	struct mbuf *m;
358776b7288SRandall Stewart 	uint32_t af;
359776b7288SRandall Stewart 	int error = 0;
36073ff045cSAndrew Thompson 
36173ff045cSAndrew Thompson 	sc = ifp->if_softc;
362776b7288SRandall Stewart 	GIF_LOCK(sc);
36373ff045cSAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
364776b7288SRandall Stewart 	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
365776b7288SRandall Stewart 
366776b7288SRandall Stewart 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
36773ff045cSAndrew Thompson 		if (m == 0)
36873ff045cSAndrew Thompson 			break;
36973ff045cSAndrew Thompson 
370776b7288SRandall Stewart #ifdef ALTQ
371776b7288SRandall Stewart 		/* Take out those altq bytes we add in gif_output  */
372776b7288SRandall Stewart #ifdef INET
373776b7288SRandall Stewart 		if (sc->gif_psrc->sa_family == AF_INET)
374776b7288SRandall Stewart 			m->m_pkthdr.len -= GIF_HDR_LEN;
375776b7288SRandall Stewart #endif
376776b7288SRandall Stewart #ifdef INET6
377776b7288SRandall Stewart 		if (sc->gif_psrc->sa_family == AF_INET6)
378776b7288SRandall Stewart 		    m->m_pkthdr.len -= GIF_HDR_LEN6;
379776b7288SRandall Stewart #endif
380776b7288SRandall Stewart #endif
38140138788SRandall Stewart 		/*
38240138788SRandall Stewart 		 * Now pull back the af that we
38340138788SRandall Stewart 		 * stashed in the csum_data.
384776b7288SRandall Stewart 		 */
385cef68c63SRandall Stewart 		af = m->m_pkthdr.csum_data;
386cef68c63SRandall Stewart 
387776b7288SRandall Stewart 		if (ifp->if_bridge)
388776b7288SRandall Stewart 			af = AF_LINK;
389776b7288SRandall Stewart 
390776b7288SRandall Stewart 		BPF_MTAP2(ifp, &af, sizeof(af), m);
391776b7288SRandall Stewart 		ifp->if_opackets++;
392776b7288SRandall Stewart 
393776b7288SRandall Stewart /*              Done by IFQ_HANDOFF */
394776b7288SRandall Stewart /* 		ifp->if_obytes += m->m_pkthdr.len;*/
395776b7288SRandall Stewart 		/* override to IPPROTO_ETHERIP for bridged traffic */
396776b7288SRandall Stewart 
397776b7288SRandall Stewart 		M_SETFIB(m, sc->gif_fibnum);
398776b7288SRandall Stewart 		/* inner AF-specific encapsulation */
399776b7288SRandall Stewart 		/* XXX should we check if our outer source is legal? */
400776b7288SRandall Stewart 		/* dispatch to output logic based on outer AF */
401776b7288SRandall Stewart 		switch (sc->gif_psrc->sa_family) {
402776b7288SRandall Stewart #ifdef INET
403776b7288SRandall Stewart 		case AF_INET:
404776b7288SRandall Stewart 			error = in_gif_output(ifp, af, m);
405776b7288SRandall Stewart 			break;
406776b7288SRandall Stewart #endif
407776b7288SRandall Stewart #ifdef INET6
408776b7288SRandall Stewart 		case AF_INET6:
409776b7288SRandall Stewart 			error = in6_gif_output(ifp, af, m);
410776b7288SRandall Stewart 			break;
411776b7288SRandall Stewart #endif
412776b7288SRandall Stewart 		default:
413776b7288SRandall Stewart 			m_freem(m);
414776b7288SRandall Stewart 			error = ENETDOWN;
415776b7288SRandall Stewart 		}
416776b7288SRandall Stewart 		if (error)
417776b7288SRandall Stewart 			ifp->if_oerrors++;
41873ff045cSAndrew Thompson 
41973ff045cSAndrew Thompson 	}
42073ff045cSAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
421776b7288SRandall Stewart 	GIF_UNLOCK(sc);
42273ff045cSAndrew Thompson 	return;
42373ff045cSAndrew Thompson }
42473ff045cSAndrew Thompson 
425cfa1ca9dSYoshinobu Inoue int
42647e8d432SGleb Smirnoff gif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
42747e8d432SGleb Smirnoff 	struct route *ro)
428cfa1ca9dSYoshinobu Inoue {
429fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
4308c7e1947SRuslan Ermilov 	struct m_tag *mtag;
431cfa1ca9dSYoshinobu Inoue 	int error = 0;
4328c7e1947SRuslan Ermilov 	int gif_called;
433776b7288SRandall Stewart 	uint32_t af;
43410722b85SRobert Watson #ifdef MAC
43530d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
436e0852ce2SRobert Watson 	if (error) {
437e0852ce2SRobert Watson 		m_freem(m);
438e0852ce2SRobert Watson 		goto end;
439e0852ce2SRobert Watson 	}
44010722b85SRobert Watson #endif
441e9f947e2SHiroki Sato 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
442e9f947e2SHiroki Sato 		error = ENETDOWN;
443e9f947e2SHiroki Sato 		m_freem(m);
444e9f947e2SHiroki Sato 		goto end;
445e9f947e2SHiroki Sato 	}
44610722b85SRobert Watson 
447cfa1ca9dSYoshinobu Inoue 	/*
448cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
4498c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
4508c7e1947SRuslan Ermilov 	 *
4518c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
452cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
453cfa1ca9dSYoshinobu Inoue 	 */
4548c7e1947SRuslan Ermilov 	gif_called = 1;
4558c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
4568c7e1947SRuslan Ermilov 	while (mtag != NULL) {
4578c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4588c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
4598c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
4608c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
4618c7e1947SRuslan Ermilov 			m_freem(m);
4628c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
4638c7e1947SRuslan Ermilov 			goto end;
4648c7e1947SRuslan Ermilov 		}
4658c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
4668c7e1947SRuslan Ermilov 		gif_called++;
4678c7e1947SRuslan Ermilov 	}
468603724d3SBjoern A. Zeeb 	if (gif_called > V_max_gif_nesting) {
469cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
470cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
471523ebc4eSRobert Watson 		    gif_called);
472cfa1ca9dSYoshinobu Inoue 		m_freem(m);
473cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
474cfa1ca9dSYoshinobu Inoue 		goto end;
475cfa1ca9dSYoshinobu Inoue 	}
4768c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4778c7e1947SRuslan Ermilov 	    M_NOWAIT);
4788c7e1947SRuslan Ermilov 	if (mtag == NULL) {
4798c7e1947SRuslan Ermilov 		m_freem(m);
4808c7e1947SRuslan Ermilov 		error = ENOMEM;
4818c7e1947SRuslan Ermilov 		goto end;
4828c7e1947SRuslan Ermilov 	}
4838c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
4848c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
485686cdd19SJun-ichiro itojun Hagino 
486cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
48701399f34SDavid Malone 	/* BPF writes need to be handled specially. */
48847e8d432SGleb Smirnoff 	if (dst->sa_family == AF_UNSPEC)
48901399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
49047e8d432SGleb Smirnoff 	else
49101399f34SDavid Malone 		af = dst->sa_family;
49240138788SRandall Stewart 	/*
49340138788SRandall Stewart 	 * Now save the af in the inbound pkt csum
49440138788SRandall Stewart 	 * data, this is a cheat since we are using
49540138788SRandall Stewart 	 * the inbound csum_data field to carry the
49640138788SRandall Stewart 	 * af over to the gif_start() routine, avoiding
49740138788SRandall Stewart 	 * using yet another mtag.
498776b7288SRandall Stewart 	 */
499cef68c63SRandall Stewart 	m->m_pkthdr.csum_data = af;
500776b7288SRandall Stewart 	if (!(ifp->if_flags & IFF_UP) ||
501776b7288SRandall Stewart 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
502cfa1ca9dSYoshinobu Inoue 		m_freem(m);
503cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
504776b7288SRandall Stewart 		goto end;
505cfa1ca9dSYoshinobu Inoue 	}
506776b7288SRandall Stewart #ifdef ALTQ
50740138788SRandall Stewart 	/*
50840138788SRandall Stewart 	 * Make altq aware of the bytes we will add
509776b7288SRandall Stewart 	 * when we actually send it.
510776b7288SRandall Stewart 	 */
511776b7288SRandall Stewart #ifdef INET
512776b7288SRandall Stewart 	if (sc->gif_psrc->sa_family == AF_INET)
513776b7288SRandall Stewart 		m->m_pkthdr.len += GIF_HDR_LEN;
514776b7288SRandall Stewart #endif
515776b7288SRandall Stewart #ifdef INET6
516776b7288SRandall Stewart 	if (sc->gif_psrc->sa_family == AF_INET6)
517776b7288SRandall Stewart 		m->m_pkthdr.len += GIF_HDR_LEN6;
518776b7288SRandall Stewart #endif
519776b7288SRandall Stewart #endif
520776b7288SRandall Stewart 	/*
521776b7288SRandall Stewart 	 * Queue message on interface, update output statistics if
522776b7288SRandall Stewart 	 * successful, and start output if interface not yet active.
523776b7288SRandall Stewart 	 */
524776b7288SRandall Stewart 	IFQ_HANDOFF(ifp, m, error);
525cfa1ca9dSYoshinobu Inoue   end:
52633841545SHajimu UMEMOTO 	if (error)
52733841545SHajimu UMEMOTO 		ifp->if_oerrors++;
52825af0bb5SGleb Smirnoff 	return (error);
529cfa1ca9dSYoshinobu Inoue }
530cfa1ca9dSYoshinobu Inoue 
531cfa1ca9dSYoshinobu Inoue void
53221fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
533cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
534cfa1ca9dSYoshinobu Inoue 	int af;
53521fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
536cfa1ca9dSYoshinobu Inoue {
53773ff045cSAndrew Thompson 	int isr, n;
5384382b068SChristian Brueffer 	struct gif_softc *sc;
53973ff045cSAndrew Thompson 	struct etherip_header *eip;
54056abdd33SAndrew Thompson 	struct ether_header *eh;
54156abdd33SAndrew Thompson 	struct ifnet *oldifp;
542cfa1ca9dSYoshinobu Inoue 
54321fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
544cfa1ca9dSYoshinobu Inoue 		/* just in case */
545cfa1ca9dSYoshinobu Inoue 		m_freem(m);
546cfa1ca9dSYoshinobu Inoue 		return;
547cfa1ca9dSYoshinobu Inoue 	}
5484382b068SChristian Brueffer 	sc = ifp->if_softc;
54921fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
550cfa1ca9dSYoshinobu Inoue 
55110722b85SRobert Watson #ifdef MAC
55230d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
55310722b85SRobert Watson #endif
55410722b85SRobert Watson 
55516d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
55633841545SHajimu UMEMOTO 		u_int32_t af1 = af;
557437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
558cfa1ca9dSYoshinobu Inoue 	}
559cfa1ca9dSYoshinobu Inoue 
560e9f947e2SHiroki Sato 	if ((ifp->if_flags & IFF_MONITOR) != 0) {
561e9f947e2SHiroki Sato 		ifp->if_ipackets++;
562e9f947e2SHiroki Sato 		ifp->if_ibytes += m->m_pkthdr.len;
563e9f947e2SHiroki Sato 		m_freem(m);
564e9f947e2SHiroki Sato 		return;
565e9f947e2SHiroki Sato 	}
566e9f947e2SHiroki Sato 
56794408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
56821fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
56994408d94SBrooks Davis 		if (m == NULL)
57094408d94SBrooks Davis 			return;
57194408d94SBrooks Davis 	}
57294408d94SBrooks Davis 
573cfa1ca9dSYoshinobu Inoue 	/*
574cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
575cfa1ca9dSYoshinobu Inoue 	 * specified address family.
576cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
577cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
578cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
579cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
580cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
58188ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
58288ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
583cfa1ca9dSYoshinobu Inoue 	 */
584cfa1ca9dSYoshinobu Inoue 	switch (af) {
585cfa1ca9dSYoshinobu Inoue #ifdef INET
586cfa1ca9dSYoshinobu Inoue 	case AF_INET:
587cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
588cfa1ca9dSYoshinobu Inoue 		break;
589cfa1ca9dSYoshinobu Inoue #endif
590cfa1ca9dSYoshinobu Inoue #ifdef INET6
591cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
592cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
593cfa1ca9dSYoshinobu Inoue 		break;
594cfa1ca9dSYoshinobu Inoue #endif
59573ff045cSAndrew Thompson 	case AF_LINK:
59673ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
59773ff045cSAndrew Thompson 		if (n > m->m_len) {
59873ff045cSAndrew Thompson 			m = m_pullup(m, n);
59973ff045cSAndrew Thompson 			if (m == NULL) {
60073ff045cSAndrew Thompson 				ifp->if_ierrors++;
60173ff045cSAndrew Thompson 				return;
60273ff045cSAndrew Thompson 			}
60373ff045cSAndrew Thompson 		}
60473ff045cSAndrew Thompson 
60573ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
606dbe59260SHiroki Sato 		/*
607dbe59260SHiroki Sato 		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
608dbe59260SHiroki Sato 		 * accepts an EtherIP packet with revered version field in
609dbe59260SHiroki Sato 		 * the header.  This is a knob for backward compatibility
610dbe59260SHiroki Sato 		 * with FreeBSD 7.2R or prior.
611dbe59260SHiroki Sato 		 */
612dbe59260SHiroki Sato 		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
613dbe59260SHiroki Sato 			if (eip->eip_resvl != ETHERIP_VERSION
614dbe59260SHiroki Sato 			    && eip->eip_ver != ETHERIP_VERSION) {
61573ff045cSAndrew Thompson 				/* discard unknown versions */
61673ff045cSAndrew Thompson 				m_freem(m);
61773ff045cSAndrew Thompson 				return;
61873ff045cSAndrew Thompson 			}
619dbe59260SHiroki Sato 		} else {
620dbe59260SHiroki Sato 			if (eip->eip_ver != ETHERIP_VERSION) {
621dbe59260SHiroki Sato 				/* discard unknown versions */
622dbe59260SHiroki Sato 				m_freem(m);
623dbe59260SHiroki Sato 				return;
624dbe59260SHiroki Sato 			}
625dbe59260SHiroki Sato 		}
62673ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
62773ff045cSAndrew Thompson 
62873ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
62973ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
63073ff045cSAndrew Thompson 
63156abdd33SAndrew Thompson 		if (ifp->if_bridge) {
63256abdd33SAndrew Thompson 			oldifp = ifp;
63356abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
63456abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
63556abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
63656abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
63756abdd33SAndrew Thompson 				else
63856abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
63956abdd33SAndrew Thompson 				ifp->if_imcasts++;
64056abdd33SAndrew Thompson 			}
64173ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
64273ff045cSAndrew Thompson 
64356abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
64456abdd33SAndrew Thompson 				/*
64556abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
64656abdd33SAndrew Thompson 				 * members for which the frame is addressed.
64756abdd33SAndrew Thompson 				 */
64856abdd33SAndrew Thompson 				ether_demux(ifp, m);
64956abdd33SAndrew Thompson 				return;
65056abdd33SAndrew Thompson 			}
65156abdd33SAndrew Thompson 		}
65273ff045cSAndrew Thompson 		if (m != NULL)
65373ff045cSAndrew Thompson 			m_freem(m);
65473ff045cSAndrew Thompson 		return;
65573ff045cSAndrew Thompson 
656cfa1ca9dSYoshinobu Inoue 	default:
65794408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
65821fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
65994408d94SBrooks Davis 		else
660cfa1ca9dSYoshinobu Inoue 			m_freem(m);
661cfa1ca9dSYoshinobu Inoue 		return;
662cfa1ca9dSYoshinobu Inoue 	}
663cfa1ca9dSYoshinobu Inoue 
66421fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
66521fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
666a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
6671cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
668cfa1ca9dSYoshinobu Inoue }
669cfa1ca9dSYoshinobu Inoue 
670686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
671cfa1ca9dSYoshinobu Inoue int
672cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
673cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
674cfa1ca9dSYoshinobu Inoue 	u_long cmd;
675cfa1ca9dSYoshinobu Inoue 	caddr_t data;
676cfa1ca9dSYoshinobu Inoue {
677fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
678cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
679cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
680dbe59260SHiroki Sato 	u_int	options;
681686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
6823bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
6833bb61ca6SHajimu UMEMOTO 	u_long mtu;
6843bb61ca6SHajimu UMEMOTO #endif
685cfa1ca9dSYoshinobu Inoue 
686cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
687cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6889426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
689cfa1ca9dSYoshinobu Inoue 		break;
690cfa1ca9dSYoshinobu Inoue 
691cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
692cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
693cfa1ca9dSYoshinobu Inoue 		break;
694cfa1ca9dSYoshinobu Inoue 
695686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
696cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
697cfa1ca9dSYoshinobu Inoue 		break;
698686cdd19SJun-ichiro itojun Hagino 
699cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
700cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
7013bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
702cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
703cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
704cfa1ca9dSYoshinobu Inoue 		break;
705686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
706cfa1ca9dSYoshinobu Inoue 
7073bb61ca6SHajimu UMEMOTO #ifdef INET
708cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
7093bb61ca6SHajimu UMEMOTO #endif
710cfa1ca9dSYoshinobu Inoue #ifdef INET6
711cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
712cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
71333841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
714686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
71533841545SHajimu UMEMOTO #ifdef INET
716686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
717cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
718cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
719cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
720cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
721cfa1ca9dSYoshinobu Inoue 			break;
72233841545SHajimu UMEMOTO #endif
723cfa1ca9dSYoshinobu Inoue #ifdef INET6
724686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
725cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
726cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
727cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
728cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
729686cdd19SJun-ichiro itojun Hagino 			break;
730686cdd19SJun-ichiro itojun Hagino #endif
73133841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
73233841545SHajimu UMEMOTO 			src = (struct sockaddr *)
73333841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
73433841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
73533841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
7369426aedfSHajimu UMEMOTO 			break;
7376f4ded3aSBrooks Davis 		default:
7389426aedfSHajimu UMEMOTO 			return EINVAL;
73933841545SHajimu UMEMOTO 		}
74033841545SHajimu UMEMOTO 
74133841545SHajimu UMEMOTO 		/* sa_family must be equal */
74233841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
74333841545SHajimu UMEMOTO 			return EINVAL;
74433841545SHajimu UMEMOTO 
74533841545SHajimu UMEMOTO 		/* validate sa_len */
74633841545SHajimu UMEMOTO 		switch (src->sa_family) {
74733841545SHajimu UMEMOTO #ifdef INET
74833841545SHajimu UMEMOTO 		case AF_INET:
74933841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
75033841545SHajimu UMEMOTO 				return EINVAL;
75133841545SHajimu UMEMOTO 			break;
75233841545SHajimu UMEMOTO #endif
75333841545SHajimu UMEMOTO #ifdef INET6
75433841545SHajimu UMEMOTO 		case AF_INET6:
75533841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
75633841545SHajimu UMEMOTO 				return EINVAL;
75733841545SHajimu UMEMOTO 			break;
75833841545SHajimu UMEMOTO #endif
75933841545SHajimu UMEMOTO 		default:
76033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
76133841545SHajimu UMEMOTO 		}
76233841545SHajimu UMEMOTO 		switch (dst->sa_family) {
76333841545SHajimu UMEMOTO #ifdef INET
76433841545SHajimu UMEMOTO 		case AF_INET:
76533841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
76633841545SHajimu UMEMOTO 				return EINVAL;
76733841545SHajimu UMEMOTO 			break;
76833841545SHajimu UMEMOTO #endif
76933841545SHajimu UMEMOTO #ifdef INET6
77033841545SHajimu UMEMOTO 		case AF_INET6:
77133841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
77233841545SHajimu UMEMOTO 				return EINVAL;
77333841545SHajimu UMEMOTO 			break;
77433841545SHajimu UMEMOTO #endif
77533841545SHajimu UMEMOTO 		default:
77633841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
77733841545SHajimu UMEMOTO 		}
77833841545SHajimu UMEMOTO 
77933841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
78033841545SHajimu UMEMOTO 		switch (cmd) {
78133841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
78233841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
78333841545SHajimu UMEMOTO 				break;
78433841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
78533841545SHajimu UMEMOTO #ifdef INET6
78633841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
78733841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
78833841545SHajimu UMEMOTO 				break;
78933841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
79033841545SHajimu UMEMOTO #endif /* INET6 */
79133841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
79233841545SHajimu UMEMOTO 			/* checks done in the above */
79333841545SHajimu UMEMOTO 			break;
794686cdd19SJun-ichiro itojun Hagino 		}
795cfa1ca9dSYoshinobu Inoue 
796fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
797cfa1ca9dSYoshinobu Inoue 		break;
798cfa1ca9dSYoshinobu Inoue 
799686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
800686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
801fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
802686cdd19SJun-ichiro itojun Hagino 		break;
803686cdd19SJun-ichiro itojun Hagino #endif
804686cdd19SJun-ichiro itojun Hagino 
805cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
806cfa1ca9dSYoshinobu Inoue #ifdef INET6
807cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
808cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
809cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
810cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
811cfa1ca9dSYoshinobu Inoue 			goto bad;
812cfa1ca9dSYoshinobu Inoue 		}
813cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
81433841545SHajimu UMEMOTO 		switch (cmd) {
815cfa1ca9dSYoshinobu Inoue #ifdef INET
81633841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
817cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
81833841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
819cfa1ca9dSYoshinobu Inoue 			break;
820cfa1ca9dSYoshinobu Inoue #endif /* INET */
821cfa1ca9dSYoshinobu Inoue #ifdef INET6
82233841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
823cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
824cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
82533841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
826cfa1ca9dSYoshinobu Inoue 			break;
827cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
828cfa1ca9dSYoshinobu Inoue 		default:
829cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
830cfa1ca9dSYoshinobu Inoue 			goto bad;
831cfa1ca9dSYoshinobu Inoue 		}
83233841545SHajimu UMEMOTO 		if (src->sa_len > size)
83333841545SHajimu UMEMOTO 			return EINVAL;
83433841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
835a1f7e5f8SHajimu UMEMOTO #ifdef INET6
836a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
837a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
838a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
839a1f7e5f8SHajimu UMEMOTO 				return (error);
840a1f7e5f8SHajimu UMEMOTO 		}
841a1f7e5f8SHajimu UMEMOTO #endif
842cfa1ca9dSYoshinobu Inoue 		break;
843cfa1ca9dSYoshinobu Inoue 
844cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
845cfa1ca9dSYoshinobu Inoue #ifdef INET6
846cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
847cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
848cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
849cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
850cfa1ca9dSYoshinobu Inoue 			goto bad;
851cfa1ca9dSYoshinobu Inoue 		}
852cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
85333841545SHajimu UMEMOTO 		switch (cmd) {
854cfa1ca9dSYoshinobu Inoue #ifdef INET
85533841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
856cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
85733841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
858cfa1ca9dSYoshinobu Inoue 			break;
859cfa1ca9dSYoshinobu Inoue #endif /* INET */
860cfa1ca9dSYoshinobu Inoue #ifdef INET6
86133841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
862cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
863cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
86433841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
865cfa1ca9dSYoshinobu Inoue 			break;
866cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
867cfa1ca9dSYoshinobu Inoue 		default:
868cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
869cfa1ca9dSYoshinobu Inoue 			goto bad;
870cfa1ca9dSYoshinobu Inoue 		}
87133841545SHajimu UMEMOTO 		if (src->sa_len > size)
87233841545SHajimu UMEMOTO 			return EINVAL;
873e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, src);
874e3416ab0SBjoern A. Zeeb 		if (error != 0)
875e3416ab0SBjoern A. Zeeb 			return (error);
876e3416ab0SBjoern A. Zeeb 		error = prison_if(curthread->td_ucred, dst);
877e3416ab0SBjoern A. Zeeb 		if (error != 0)
878e3416ab0SBjoern A. Zeeb 			return (error);
87933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
880a1f7e5f8SHajimu UMEMOTO #ifdef INET6
881a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
882a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
883a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
884a1f7e5f8SHajimu UMEMOTO 				return (error);
885a1f7e5f8SHajimu UMEMOTO 		}
886a1f7e5f8SHajimu UMEMOTO #endif
88733841545SHajimu UMEMOTO 		break;
88833841545SHajimu UMEMOTO 
88933841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
89033841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
89133841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
89233841545SHajimu UMEMOTO 			goto bad;
89333841545SHajimu UMEMOTO 		}
89433841545SHajimu UMEMOTO 
89533841545SHajimu UMEMOTO 		/* copy src */
89633841545SHajimu UMEMOTO 		src = sc->gif_psrc;
89733841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
89833841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
89933841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
90033841545SHajimu UMEMOTO 		if (src->sa_len > size)
90133841545SHajimu UMEMOTO 			return EINVAL;
90233841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
90333841545SHajimu UMEMOTO 
90433841545SHajimu UMEMOTO 		/* copy dst */
90533841545SHajimu UMEMOTO 		src = sc->gif_pdst;
90633841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
90733841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
90833841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
90933841545SHajimu UMEMOTO 		if (src->sa_len > size)
91033841545SHajimu UMEMOTO 			return EINVAL;
91133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
912cfa1ca9dSYoshinobu Inoue 		break;
913cfa1ca9dSYoshinobu Inoue 
914cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
915686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
916cfa1ca9dSYoshinobu Inoue 		break;
917cfa1ca9dSYoshinobu Inoue 
918dbe59260SHiroki Sato 	case GIFGOPTS:
919dbe59260SHiroki Sato 		options = sc->gif_options;
920dbe59260SHiroki Sato 		error = copyout(&options, ifr->ifr_data,
921dbe59260SHiroki Sato 				sizeof(options));
922dbe59260SHiroki Sato 		break;
923dbe59260SHiroki Sato 
924dbe59260SHiroki Sato 	case GIFSOPTS:
925dbe59260SHiroki Sato 		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
926dbe59260SHiroki Sato 			break;
9274cd5f57dSHiroki Sato 		error = copyin(ifr->ifr_data, &options, sizeof(options));
9284cd5f57dSHiroki Sato 		if (error)
9294cd5f57dSHiroki Sato 			break;
9304cd5f57dSHiroki Sato 		if (options & ~GIF_OPTMASK)
931dbe59260SHiroki Sato 			error = EINVAL;
9324cd5f57dSHiroki Sato 		else
9334cd5f57dSHiroki Sato 			sc->gif_options = options;
934dbe59260SHiroki Sato 		break;
935dbe59260SHiroki Sato 
936cfa1ca9dSYoshinobu Inoue 	default:
937cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
938cfa1ca9dSYoshinobu Inoue 		break;
939cfa1ca9dSYoshinobu Inoue 	}
940cfa1ca9dSYoshinobu Inoue  bad:
941cfa1ca9dSYoshinobu Inoue 	return error;
942cfa1ca9dSYoshinobu Inoue }
94353dab5feSBrooks Davis 
94417d5cb2dSRobert Watson /*
94517d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
94617d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
94717d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
94817d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
94917d5cb2dSRobert Watson  * a mutex.
95017d5cb2dSRobert Watson  */
9519426aedfSHajimu UMEMOTO int
9529426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
9539426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
9549426aedfSHajimu UMEMOTO 	struct sockaddr *src;
9559426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
95653dab5feSBrooks Davis {
957fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
9589426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
9599426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
9609426aedfSHajimu UMEMOTO 	int error = 0;
9619426aedfSHajimu UMEMOTO 
96217d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
963603724d3SBjoern A. Zeeb 	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
9649426aedfSHajimu UMEMOTO 		if (sc2 == sc)
9659426aedfSHajimu UMEMOTO 			continue;
9669426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
9679426aedfSHajimu UMEMOTO 			continue;
9689426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
9699426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
9709426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
9719426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
9729426aedfSHajimu UMEMOTO 			continue;
9739426aedfSHajimu UMEMOTO 
9749426aedfSHajimu UMEMOTO 		/*
9759426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
9769426aedfSHajimu UMEMOTO 		 * otherwise.
9779426aedfSHajimu UMEMOTO 		 */
978603724d3SBjoern A. Zeeb 		if (!V_parallel_tunnels &&
9799426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
9809426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
9819426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
98217d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
9839426aedfSHajimu UMEMOTO 			goto bad;
9849426aedfSHajimu UMEMOTO 		}
9859426aedfSHajimu UMEMOTO 
9869426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
9879426aedfSHajimu UMEMOTO 	}
98817d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
9899426aedfSHajimu UMEMOTO 
9909426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
9919426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
9929426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
9939426aedfSHajimu UMEMOTO #ifdef INET
9949426aedfSHajimu UMEMOTO 		case AF_INET:
9959426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
9969426aedfSHajimu UMEMOTO 			break;
9979426aedfSHajimu UMEMOTO #endif
9989426aedfSHajimu UMEMOTO #ifdef INET6
9999426aedfSHajimu UMEMOTO 		case AF_INET6:
10009426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
10019426aedfSHajimu UMEMOTO 			break;
10029426aedfSHajimu UMEMOTO #endif
10039426aedfSHajimu UMEMOTO 		}
10049426aedfSHajimu UMEMOTO 
10059426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
1006a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
10079426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
10089426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
10099426aedfSHajimu UMEMOTO 
10109426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
1011a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
10129426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
10139426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
10149426aedfSHajimu UMEMOTO 
10159426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
10169426aedfSHajimu UMEMOTO #ifdef INET
10179426aedfSHajimu UMEMOTO 	case AF_INET:
10189426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
10199426aedfSHajimu UMEMOTO 		break;
10209426aedfSHajimu UMEMOTO #endif
10219426aedfSHajimu UMEMOTO #ifdef INET6
10229426aedfSHajimu UMEMOTO 	case AF_INET6:
1023a1f7e5f8SHajimu UMEMOTO 		/*
1024a1f7e5f8SHajimu UMEMOTO 		 * Check validity of the scope zone ID of the addresses, and
1025a1f7e5f8SHajimu UMEMOTO 		 * convert it into the kernel internal form if necessary.
1026a1f7e5f8SHajimu UMEMOTO 		 */
1027a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1028a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
1029a1f7e5f8SHajimu UMEMOTO 			break;
1030a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1031a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
1032a1f7e5f8SHajimu UMEMOTO 			break;
10339426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
10349426aedfSHajimu UMEMOTO 		break;
10359426aedfSHajimu UMEMOTO #endif
10369426aedfSHajimu UMEMOTO 	}
10379426aedfSHajimu UMEMOTO 	if (error) {
10389426aedfSHajimu UMEMOTO 		/* rollback */
10399426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
10409426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
10419426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
10429426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
10439426aedfSHajimu UMEMOTO 		goto bad;
10449426aedfSHajimu UMEMOTO 	}
10459426aedfSHajimu UMEMOTO 
10469426aedfSHajimu UMEMOTO 	if (osrc)
10479426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
10489426aedfSHajimu UMEMOTO 	if (odst)
10499426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
10509426aedfSHajimu UMEMOTO 
10519426aedfSHajimu UMEMOTO  bad:
10529426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
105313f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
10549426aedfSHajimu UMEMOTO 	else
105513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
10569426aedfSHajimu UMEMOTO 
10579426aedfSHajimu UMEMOTO 	return error;
10589426aedfSHajimu UMEMOTO }
10599426aedfSHajimu UMEMOTO 
10609426aedfSHajimu UMEMOTO void
10619426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
10629426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
10639426aedfSHajimu UMEMOTO {
1064fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
106553dab5feSBrooks Davis 
106653dab5feSBrooks Davis 	if (sc->gif_psrc) {
106753dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
106853dab5feSBrooks Davis 		sc->gif_psrc = NULL;
106953dab5feSBrooks Davis 	}
107053dab5feSBrooks Davis 	if (sc->gif_pdst) {
107153dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
107253dab5feSBrooks Davis 		sc->gif_pdst = NULL;
107353dab5feSBrooks Davis 	}
10749426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
10759426aedfSHajimu UMEMOTO #ifdef INET
10769426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
10779426aedfSHajimu UMEMOTO #endif
10789426aedfSHajimu UMEMOTO #ifdef INET6
10799426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
10809426aedfSHajimu UMEMOTO #endif
108113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
108253dab5feSBrooks Davis }
1083