xref: /freebsd/sys/net/if_gif.c (revision e0de57f9eac285d337262b661379d4b886bed4de)
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"
35e0852ce2SRobert Watson #include "opt_mac.h"
36cfa1ca9dSYoshinobu Inoue 
37cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
38cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
39cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
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>
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");
97e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
9853dab5feSBrooks Davis 
9994408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
10094408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
10194408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
10294408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
10394408d94SBrooks Davis 
10473ff045cSAndrew Thompson static void	gif_start(struct ifnet *);
1056b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
106bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
10753dab5feSBrooks Davis 
108f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0);
10953dab5feSBrooks Davis 
110929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
111cfa1ca9dSYoshinobu Inoue 
112872f786aSBrooks Davis SYSCTL_DECL(_net_link);
113872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
114872f786aSBrooks Davis     "Generic Tunnel Interface");
115686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
116686cdd19SJun-ichiro itojun Hagino /*
117872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
118686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
119686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
120686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
121686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
122686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
123686cdd19SJun-ichiro itojun Hagino  */
124686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
125686cdd19SJun-ichiro itojun Hagino #endif
1268b615593SMarko Zec #ifndef VIMAGE
127686cdd19SJun-ichiro itojun Hagino static int max_gif_nesting = MAX_GIF_NEST;
1288b615593SMarko Zec #endif
1298b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting,
1308b615593SMarko Zec     CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels");
1318b615593SMarko Zec 
1328b615593SMarko Zec #ifdef INET6
1338b615593SMarko Zec SYSCTL_DECL(_net_inet6_ip6);
1348b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM,
1358b615593SMarko Zec     gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, "");
1368b615593SMarko Zec #endif
137872f786aSBrooks Davis 
138872f786aSBrooks Davis /*
139872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
140872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
141872f786aSBrooks Davis  * we allow control over this check here.
142872f786aSBrooks Davis  */
143872f786aSBrooks Davis #ifdef XBONEHACK
144872f786aSBrooks Davis static int parallel_tunnels = 1;
145872f786aSBrooks Davis #else
146872f786aSBrooks Davis static int parallel_tunnels = 0;
147872f786aSBrooks Davis #endif
1488b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels,
1498b615593SMarko Zec     CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?");
150cfa1ca9dSYoshinobu Inoue 
15156abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
15256abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
15356abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
15456abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
15556abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
15656abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
15756abdd33SAndrew Thompson #endif
15856abdd33SAndrew Thompson 
159bb2bfb4fSBrooks Davis static int
1606b7330e2SSam Leffler gif_clone_create(ifc, unit, params)
16153dab5feSBrooks Davis 	struct if_clone *ifc;
1623b16e7b2SMaxime Henrion 	int unit;
1636b7330e2SSam Leffler 	caddr_t params;
164cfa1ca9dSYoshinobu Inoue {
1658b615593SMarko Zec 	INIT_VNET_GIF(curvnet);
16633841545SHajimu UMEMOTO 	struct gif_softc *sc;
167cfa1ca9dSYoshinobu Inoue 
168e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1698b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
170fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
171fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
172fc74a9f9SBrooks Davis 		free(sc, M_GIF);
173fc74a9f9SBrooks Davis 		return (ENOSPC);
174fc74a9f9SBrooks Davis 	}
17553dab5feSBrooks Davis 
17625af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
17725af0bb5SGleb Smirnoff 
178fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
179fc74a9f9SBrooks Davis 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
180686cdd19SJun-ichiro itojun Hagino 
1819426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1829426aedfSHajimu UMEMOTO 
183fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
184fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
185fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
18633841545SHajimu UMEMOTO #if 0
18733841545SHajimu UMEMOTO 	/* turn off ingress filter */
188fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
18933841545SHajimu UMEMOTO #endif
190fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
19173ff045cSAndrew Thompson 	GIF2IFP(sc)->if_start  = gif_start;
192fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
193fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
194fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
19501399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
19694408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
197fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
19825af0bb5SGleb Smirnoff 
19925af0bb5SGleb Smirnoff 	mtx_lock(&gif_mtx);
200603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
20125af0bb5SGleb Smirnoff 	mtx_unlock(&gif_mtx);
20225af0bb5SGleb Smirnoff 
20325af0bb5SGleb Smirnoff 	return (0);
204cfa1ca9dSYoshinobu Inoue }
205cfa1ca9dSYoshinobu Inoue 
20617d5cb2dSRobert Watson static void
207febd0759SAndrew Thompson gif_clone_destroy(ifp)
208febd0759SAndrew Thompson 	struct ifnet *ifp;
20953dab5feSBrooks Davis {
210e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6)
21153dab5feSBrooks Davis 	int err;
212e0de57f9SBjoern A. Zeeb #endif
213febd0759SAndrew Thompson 	struct gif_softc *sc = ifp->if_softc;
214febd0759SAndrew Thompson 
215febd0759SAndrew Thompson 	mtx_lock(&gif_mtx);
216febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
217febd0759SAndrew Thompson 	mtx_unlock(&gif_mtx);
21853dab5feSBrooks Davis 
21917d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
2209426aedfSHajimu UMEMOTO #ifdef INET6
22153dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
22253dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
22353dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
22453dab5feSBrooks Davis 	}
2259426aedfSHajimu UMEMOTO #endif
2269426aedfSHajimu UMEMOTO #ifdef INET
2279426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2289426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2299426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2309426aedfSHajimu UMEMOTO 	}
2319426aedfSHajimu UMEMOTO #endif
23253dab5feSBrooks Davis 
23394408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
23494408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
23553dab5feSBrooks Davis 	bpfdetach(ifp);
23653dab5feSBrooks Davis 	if_detach(ifp);
237fc74a9f9SBrooks Davis 	if_free(ifp);
23853dab5feSBrooks Davis 
23925af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
24025af0bb5SGleb Smirnoff 
24153dab5feSBrooks Davis 	free(sc, M_GIF);
24253dab5feSBrooks Davis }
24353dab5feSBrooks Davis 
24453dab5feSBrooks Davis static int
24553dab5feSBrooks Davis gifmodevent(mod, type, data)
24653dab5feSBrooks Davis 	module_t mod;
24753dab5feSBrooks Davis 	int type;
24853dab5feSBrooks Davis 	void *data;
24953dab5feSBrooks Davis {
25053dab5feSBrooks Davis 
25153dab5feSBrooks Davis 	switch (type) {
25253dab5feSBrooks Davis 	case MOD_LOAD:
25317d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
254603724d3SBjoern A. Zeeb 		LIST_INIT(&V_gif_softc_list);
25553dab5feSBrooks Davis 		if_clone_attach(&gif_cloner);
25653dab5feSBrooks Davis 
25753dab5feSBrooks Davis #ifdef INET6
258603724d3SBjoern A. Zeeb 		V_ip6_gif_hlim = GIF_HLIM;
25953dab5feSBrooks Davis #endif
26053dab5feSBrooks Davis 
26153dab5feSBrooks Davis 		break;
26253dab5feSBrooks Davis 	case MOD_UNLOAD:
26353dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
26417d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
26553dab5feSBrooks Davis #ifdef INET6
266603724d3SBjoern A. Zeeb 		V_ip6_gif_hlim = 0;
26753dab5feSBrooks Davis #endif
26853dab5feSBrooks Davis 		break;
2693e019deaSPoul-Henning Kamp 	default:
2703e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
27153dab5feSBrooks Davis 	}
27253dab5feSBrooks Davis 	return 0;
27353dab5feSBrooks Davis }
27453dab5feSBrooks Davis 
27553dab5feSBrooks Davis static moduledata_t gif_mod = {
27653dab5feSBrooks Davis 	"if_gif",
27753dab5feSBrooks Davis 	gifmodevent,
27853dab5feSBrooks Davis 	0
27953dab5feSBrooks Davis };
28053dab5feSBrooks Davis 
28153dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
28220af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
283cfa1ca9dSYoshinobu Inoue 
2849426aedfSHajimu UMEMOTO int
285686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
286686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
287686cdd19SJun-ichiro itojun Hagino 	int off;
288686cdd19SJun-ichiro itojun Hagino 	int proto;
289686cdd19SJun-ichiro itojun Hagino 	void *arg;
290686cdd19SJun-ichiro itojun Hagino {
291686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
292686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
293686cdd19SJun-ichiro itojun Hagino 
294686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
295686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
296686cdd19SJun-ichiro itojun Hagino 		return 0;
297686cdd19SJun-ichiro itojun Hagino 
298fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
299686cdd19SJun-ichiro itojun Hagino 		return 0;
300686cdd19SJun-ichiro itojun Hagino 
301686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
302686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
303686cdd19SJun-ichiro itojun Hagino 		return 0;
304686cdd19SJun-ichiro itojun Hagino 
305686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
306686cdd19SJun-ichiro itojun Hagino #ifdef INET
307686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
308686cdd19SJun-ichiro itojun Hagino 		break;
309686cdd19SJun-ichiro itojun Hagino #endif
310686cdd19SJun-ichiro itojun Hagino #ifdef INET6
311686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
312686cdd19SJun-ichiro itojun Hagino 		break;
313686cdd19SJun-ichiro itojun Hagino #endif
31473ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
31573ff045cSAndrew Thompson 		break;
31673ff045cSAndrew Thompson 
317686cdd19SJun-ichiro itojun Hagino 	default:
318686cdd19SJun-ichiro itojun Hagino 		return 0;
319686cdd19SJun-ichiro itojun Hagino 	}
320686cdd19SJun-ichiro itojun Hagino 
3213bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3223bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3233bb61ca6SHajimu UMEMOTO 		return 0;
3243bb61ca6SHajimu UMEMOTO 
3256f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
326686cdd19SJun-ichiro itojun Hagino 
327686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
328686cdd19SJun-ichiro itojun Hagino #ifdef INET
329686cdd19SJun-ichiro itojun Hagino 	case 4:
330686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
331686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
332686cdd19SJun-ichiro itojun Hagino 			return 0;
333686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
334686cdd19SJun-ichiro itojun Hagino #endif
335686cdd19SJun-ichiro itojun Hagino #ifdef INET6
336686cdd19SJun-ichiro itojun Hagino 	case 6:
3379426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3389426aedfSHajimu UMEMOTO 			return 0;
339686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
340686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
341686cdd19SJun-ichiro itojun Hagino 			return 0;
342686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
343686cdd19SJun-ichiro itojun Hagino #endif
344686cdd19SJun-ichiro itojun Hagino 	default:
345686cdd19SJun-ichiro itojun Hagino 		return 0;
346686cdd19SJun-ichiro itojun Hagino 	}
347686cdd19SJun-ichiro itojun Hagino }
348686cdd19SJun-ichiro itojun Hagino 
34973ff045cSAndrew Thompson static void
35073ff045cSAndrew Thompson gif_start(struct ifnet *ifp)
35173ff045cSAndrew Thompson {
35273ff045cSAndrew Thompson 	struct gif_softc *sc;
35373ff045cSAndrew Thompson 	struct mbuf *m;
35473ff045cSAndrew Thompson 
35573ff045cSAndrew Thompson 	sc = ifp->if_softc;
35673ff045cSAndrew Thompson 
35773ff045cSAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35873ff045cSAndrew Thompson 	for (;;) {
35973ff045cSAndrew Thompson 		IFQ_DEQUEUE(&ifp->if_snd, m);
36073ff045cSAndrew Thompson 		if (m == 0)
36173ff045cSAndrew Thompson 			break;
36273ff045cSAndrew Thompson 
36373ff045cSAndrew Thompson 		gif_output(ifp, m, sc->gif_pdst, NULL);
36473ff045cSAndrew Thompson 
36573ff045cSAndrew Thompson 	}
36673ff045cSAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
36773ff045cSAndrew Thompson 
36873ff045cSAndrew Thompson 	return;
36973ff045cSAndrew Thompson }
37073ff045cSAndrew Thompson 
371cfa1ca9dSYoshinobu Inoue int
372cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
373cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
374cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
375cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
376cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
377cfa1ca9dSYoshinobu Inoue {
3788b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
379fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
3808c7e1947SRuslan Ermilov 	struct m_tag *mtag;
381cfa1ca9dSYoshinobu Inoue 	int error = 0;
3828c7e1947SRuslan Ermilov 	int gif_called;
38301399f34SDavid Malone 	u_int32_t af;
384cfa1ca9dSYoshinobu Inoue 
38510722b85SRobert Watson #ifdef MAC
38630d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
387e0852ce2SRobert Watson 	if (error) {
388e0852ce2SRobert Watson 		m_freem(m);
389e0852ce2SRobert Watson 		goto end;
390e0852ce2SRobert Watson 	}
39110722b85SRobert Watson #endif
39210722b85SRobert Watson 
393cfa1ca9dSYoshinobu Inoue 	/*
394cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
3958c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
3968c7e1947SRuslan Ermilov 	 *
3978c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
398cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
399cfa1ca9dSYoshinobu Inoue 	 */
4008c7e1947SRuslan Ermilov 	gif_called = 1;
4018c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
4028c7e1947SRuslan Ermilov 	while (mtag != NULL) {
4038c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4048c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
4058c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
4068c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
4078c7e1947SRuslan Ermilov 			m_freem(m);
4088c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
4098c7e1947SRuslan Ermilov 			goto end;
4108c7e1947SRuslan Ermilov 		}
4118c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
4128c7e1947SRuslan Ermilov 		gif_called++;
4138c7e1947SRuslan Ermilov 	}
414603724d3SBjoern A. Zeeb 	if (gif_called > V_max_gif_nesting) {
415cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
416cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
417523ebc4eSRobert Watson 		    gif_called);
418cfa1ca9dSYoshinobu Inoue 		m_freem(m);
419cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
420cfa1ca9dSYoshinobu Inoue 		goto end;
421cfa1ca9dSYoshinobu Inoue 	}
4228c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4238c7e1947SRuslan Ermilov 	    M_NOWAIT);
4248c7e1947SRuslan Ermilov 	if (mtag == NULL) {
4258c7e1947SRuslan Ermilov 		m_freem(m);
4268c7e1947SRuslan Ermilov 		error = ENOMEM;
4278c7e1947SRuslan Ermilov 		goto end;
4288c7e1947SRuslan Ermilov 	}
4298c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
4308c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
431686cdd19SJun-ichiro itojun Hagino 
432cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
43325af0bb5SGleb Smirnoff 
43425af0bb5SGleb Smirnoff 	GIF_LOCK(sc);
43525af0bb5SGleb Smirnoff 
436cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
437cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
4386e860629SGleb Smirnoff 		GIF_UNLOCK(sc);
439cfa1ca9dSYoshinobu Inoue 		m_freem(m);
440cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
441cfa1ca9dSYoshinobu Inoue 		goto end;
442cfa1ca9dSYoshinobu Inoue 	}
443cfa1ca9dSYoshinobu Inoue 
44401399f34SDavid Malone 	/* BPF writes need to be handled specially. */
44501399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
44601399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
44701399f34SDavid Malone 		dst->sa_family = af;
44801399f34SDavid Malone 	}
44901399f34SDavid Malone 
45001399f34SDavid Malone 	af = dst->sa_family;
45116d878ccSChristian S.J. Peron 	BPF_MTAP2(ifp, &af, sizeof(af), m);
452cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
453cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
454cfa1ca9dSYoshinobu Inoue 
45573ff045cSAndrew Thompson 	/* override to IPPROTO_ETHERIP for bridged traffic */
45673ff045cSAndrew Thompson 	if (ifp->if_bridge)
45773ff045cSAndrew Thompson 		af = AF_LINK;
45873ff045cSAndrew Thompson 
4598b07e49aSJulian Elischer 	M_SETFIB(m, sc->gif_fibnum);
46033841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
46133841545SHajimu UMEMOTO 
462686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
463686cdd19SJun-ichiro itojun Hagino 
46433841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
465cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
466cfa1ca9dSYoshinobu Inoue #ifdef INET
467cfa1ca9dSYoshinobu Inoue 	case AF_INET:
46873ff045cSAndrew Thompson 		error = in_gif_output(ifp, af, m);
469cfa1ca9dSYoshinobu Inoue 		break;
470cfa1ca9dSYoshinobu Inoue #endif
471cfa1ca9dSYoshinobu Inoue #ifdef INET6
472cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
47373ff045cSAndrew Thompson 		error = in6_gif_output(ifp, af, m);
474cfa1ca9dSYoshinobu Inoue 		break;
475cfa1ca9dSYoshinobu Inoue #endif
476cfa1ca9dSYoshinobu Inoue 	default:
477cfa1ca9dSYoshinobu Inoue 		m_freem(m);
478cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
479cfa1ca9dSYoshinobu Inoue 	}
480cfa1ca9dSYoshinobu Inoue 
4816e860629SGleb Smirnoff 	GIF_UNLOCK(sc);
482cfa1ca9dSYoshinobu Inoue   end:
48333841545SHajimu UMEMOTO 	if (error)
48433841545SHajimu UMEMOTO 		ifp->if_oerrors++;
48525af0bb5SGleb Smirnoff 	return (error);
486cfa1ca9dSYoshinobu Inoue }
487cfa1ca9dSYoshinobu Inoue 
488cfa1ca9dSYoshinobu Inoue void
48921fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
490cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
491cfa1ca9dSYoshinobu Inoue 	int af;
49221fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
493cfa1ca9dSYoshinobu Inoue {
49473ff045cSAndrew Thompson 	int isr, n;
49573ff045cSAndrew Thompson 	struct etherip_header *eip;
49656abdd33SAndrew Thompson 	struct ether_header *eh;
49756abdd33SAndrew Thompson 	struct ifnet *oldifp;
498cfa1ca9dSYoshinobu Inoue 
49921fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
500cfa1ca9dSYoshinobu Inoue 		/* just in case */
501cfa1ca9dSYoshinobu Inoue 		m_freem(m);
502cfa1ca9dSYoshinobu Inoue 		return;
503cfa1ca9dSYoshinobu Inoue 	}
504cfa1ca9dSYoshinobu Inoue 
50521fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
506cfa1ca9dSYoshinobu Inoue 
50710722b85SRobert Watson #ifdef MAC
50830d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
50910722b85SRobert Watson #endif
51010722b85SRobert Watson 
51116d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
51233841545SHajimu UMEMOTO 		u_int32_t af1 = af;
513437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
514cfa1ca9dSYoshinobu Inoue 	}
515cfa1ca9dSYoshinobu Inoue 
51694408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
51721fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
51894408d94SBrooks Davis 		if (m == NULL)
51994408d94SBrooks Davis 			return;
52094408d94SBrooks Davis 	}
52194408d94SBrooks Davis 
522cfa1ca9dSYoshinobu Inoue 	/*
523cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
524cfa1ca9dSYoshinobu Inoue 	 * specified address family.
525cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
526cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
527cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
528cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
529cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
53088ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
53188ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
532cfa1ca9dSYoshinobu Inoue 	 */
533cfa1ca9dSYoshinobu Inoue 	switch (af) {
534cfa1ca9dSYoshinobu Inoue #ifdef INET
535cfa1ca9dSYoshinobu Inoue 	case AF_INET:
536cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
537cfa1ca9dSYoshinobu Inoue 		break;
538cfa1ca9dSYoshinobu Inoue #endif
539cfa1ca9dSYoshinobu Inoue #ifdef INET6
540cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
541cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
542cfa1ca9dSYoshinobu Inoue 		break;
543cfa1ca9dSYoshinobu Inoue #endif
54473ff045cSAndrew Thompson 	case AF_LINK:
54573ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
54673ff045cSAndrew Thompson 		if (n > m->m_len) {
54773ff045cSAndrew Thompson 			m = m_pullup(m, n);
54873ff045cSAndrew Thompson 			if (m == NULL) {
54973ff045cSAndrew Thompson 				ifp->if_ierrors++;
55073ff045cSAndrew Thompson 				return;
55173ff045cSAndrew Thompson 			}
55273ff045cSAndrew Thompson 		}
55373ff045cSAndrew Thompson 
55473ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
55573ff045cSAndrew Thompson  		if (eip->eip_ver !=
55673ff045cSAndrew Thompson 		    (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
55773ff045cSAndrew Thompson 			/* discard unknown versions */
55873ff045cSAndrew Thompson 			m_freem(m);
55973ff045cSAndrew Thompson 			return;
56073ff045cSAndrew Thompson 		}
56173ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
56273ff045cSAndrew Thompson 
56373ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
56473ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
56573ff045cSAndrew Thompson 
56656abdd33SAndrew Thompson 		if (ifp->if_bridge) {
56756abdd33SAndrew Thompson 			oldifp = ifp;
56856abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
56956abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
57056abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
57156abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
57256abdd33SAndrew Thompson 				else
57356abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
57456abdd33SAndrew Thompson 				ifp->if_imcasts++;
57556abdd33SAndrew Thompson 			}
57673ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
57773ff045cSAndrew Thompson 
57856abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
57956abdd33SAndrew Thompson 				/*
58056abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
58156abdd33SAndrew Thompson 				 * members for which the frame is addressed.
58256abdd33SAndrew Thompson 				 */
58356abdd33SAndrew Thompson 				ether_demux(ifp, m);
58456abdd33SAndrew Thompson 				return;
58556abdd33SAndrew Thompson 			}
58656abdd33SAndrew Thompson 		}
58773ff045cSAndrew Thompson 		if (m != NULL)
58873ff045cSAndrew Thompson 			m_freem(m);
58973ff045cSAndrew Thompson 		return;
59073ff045cSAndrew Thompson 
591cfa1ca9dSYoshinobu Inoue 	default:
59294408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
59321fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
59494408d94SBrooks Davis 		else
595cfa1ca9dSYoshinobu Inoue 			m_freem(m);
596cfa1ca9dSYoshinobu Inoue 		return;
597cfa1ca9dSYoshinobu Inoue 	}
598cfa1ca9dSYoshinobu Inoue 
59921fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
60021fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
6011cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
602cfa1ca9dSYoshinobu Inoue }
603cfa1ca9dSYoshinobu Inoue 
604686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
605cfa1ca9dSYoshinobu Inoue int
606cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
607cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
608cfa1ca9dSYoshinobu Inoue 	u_long cmd;
609cfa1ca9dSYoshinobu Inoue 	caddr_t data;
610cfa1ca9dSYoshinobu Inoue {
611fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
612cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
613cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
614686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
6153bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
6163bb61ca6SHajimu UMEMOTO 	u_long mtu;
6173bb61ca6SHajimu UMEMOTO #endif
618cfa1ca9dSYoshinobu Inoue 
619cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
620cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6219426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
622cfa1ca9dSYoshinobu Inoue 		break;
623cfa1ca9dSYoshinobu Inoue 
624cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
625cfa1ca9dSYoshinobu Inoue 		break;
626cfa1ca9dSYoshinobu Inoue 
627cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
628cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
629cfa1ca9dSYoshinobu Inoue 		break;
630cfa1ca9dSYoshinobu Inoue 
631686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
632cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
633cfa1ca9dSYoshinobu Inoue 		break;
634686cdd19SJun-ichiro itojun Hagino 
635cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
636cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
6373bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
638cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
639cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
640cfa1ca9dSYoshinobu Inoue 		break;
641686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
642cfa1ca9dSYoshinobu Inoue 
6433bb61ca6SHajimu UMEMOTO #ifdef INET
644cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
6453bb61ca6SHajimu UMEMOTO #endif
646cfa1ca9dSYoshinobu Inoue #ifdef INET6
647cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
648cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
64933841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
650686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
65133841545SHajimu UMEMOTO #ifdef INET
652686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
653cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
654cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
655cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
656cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
657cfa1ca9dSYoshinobu Inoue 			break;
65833841545SHajimu UMEMOTO #endif
659cfa1ca9dSYoshinobu Inoue #ifdef INET6
660686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
661cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
662cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
663cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
664cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
665686cdd19SJun-ichiro itojun Hagino 			break;
666686cdd19SJun-ichiro itojun Hagino #endif
66733841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
66833841545SHajimu UMEMOTO 			src = (struct sockaddr *)
66933841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
67033841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
67133841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
6729426aedfSHajimu UMEMOTO 			break;
6736f4ded3aSBrooks Davis 		default:
6749426aedfSHajimu UMEMOTO 			return EINVAL;
67533841545SHajimu UMEMOTO 		}
67633841545SHajimu UMEMOTO 
67733841545SHajimu UMEMOTO 		/* sa_family must be equal */
67833841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
67933841545SHajimu UMEMOTO 			return EINVAL;
68033841545SHajimu UMEMOTO 
68133841545SHajimu UMEMOTO 		/* validate sa_len */
68233841545SHajimu UMEMOTO 		switch (src->sa_family) {
68333841545SHajimu UMEMOTO #ifdef INET
68433841545SHajimu UMEMOTO 		case AF_INET:
68533841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
68633841545SHajimu UMEMOTO 				return EINVAL;
68733841545SHajimu UMEMOTO 			break;
68833841545SHajimu UMEMOTO #endif
68933841545SHajimu UMEMOTO #ifdef INET6
69033841545SHajimu UMEMOTO 		case AF_INET6:
69133841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
69233841545SHajimu UMEMOTO 				return EINVAL;
69333841545SHajimu UMEMOTO 			break;
69433841545SHajimu UMEMOTO #endif
69533841545SHajimu UMEMOTO 		default:
69633841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
69733841545SHajimu UMEMOTO 		}
69833841545SHajimu UMEMOTO 		switch (dst->sa_family) {
69933841545SHajimu UMEMOTO #ifdef INET
70033841545SHajimu UMEMOTO 		case AF_INET:
70133841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
70233841545SHajimu UMEMOTO 				return EINVAL;
70333841545SHajimu UMEMOTO 			break;
70433841545SHajimu UMEMOTO #endif
70533841545SHajimu UMEMOTO #ifdef INET6
70633841545SHajimu UMEMOTO 		case AF_INET6:
70733841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
70833841545SHajimu UMEMOTO 				return EINVAL;
70933841545SHajimu UMEMOTO 			break;
71033841545SHajimu UMEMOTO #endif
71133841545SHajimu UMEMOTO 		default:
71233841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
71333841545SHajimu UMEMOTO 		}
71433841545SHajimu UMEMOTO 
71533841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
71633841545SHajimu UMEMOTO 		switch (cmd) {
71733841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
71833841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
71933841545SHajimu UMEMOTO 				break;
72033841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
72133841545SHajimu UMEMOTO #ifdef INET6
72233841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
72333841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
72433841545SHajimu UMEMOTO 				break;
72533841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
72633841545SHajimu UMEMOTO #endif /* INET6 */
72733841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
72833841545SHajimu UMEMOTO 			/* checks done in the above */
72933841545SHajimu UMEMOTO 			break;
730686cdd19SJun-ichiro itojun Hagino 		}
731cfa1ca9dSYoshinobu Inoue 
732fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
733cfa1ca9dSYoshinobu Inoue 		break;
734cfa1ca9dSYoshinobu Inoue 
735686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
736686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
737fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
738686cdd19SJun-ichiro itojun Hagino 		break;
739686cdd19SJun-ichiro itojun Hagino #endif
740686cdd19SJun-ichiro itojun Hagino 
741cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
742cfa1ca9dSYoshinobu Inoue #ifdef INET6
743cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
744cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
745cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
746cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
747cfa1ca9dSYoshinobu Inoue 			goto bad;
748cfa1ca9dSYoshinobu Inoue 		}
749cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
75033841545SHajimu UMEMOTO 		switch (cmd) {
751cfa1ca9dSYoshinobu Inoue #ifdef INET
75233841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
753cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
75433841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
755cfa1ca9dSYoshinobu Inoue 			break;
756cfa1ca9dSYoshinobu Inoue #endif /* INET */
757cfa1ca9dSYoshinobu Inoue #ifdef INET6
75833841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
759cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
760cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
76133841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
762cfa1ca9dSYoshinobu Inoue 			break;
763cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
764cfa1ca9dSYoshinobu Inoue 		default:
765cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
766cfa1ca9dSYoshinobu Inoue 			goto bad;
767cfa1ca9dSYoshinobu Inoue 		}
76833841545SHajimu UMEMOTO 		if (src->sa_len > size)
76933841545SHajimu UMEMOTO 			return EINVAL;
77033841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
771a1f7e5f8SHajimu UMEMOTO #ifdef INET6
772a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
773a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
774a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
775a1f7e5f8SHajimu UMEMOTO 				return (error);
776a1f7e5f8SHajimu UMEMOTO 		}
777a1f7e5f8SHajimu UMEMOTO #endif
778cfa1ca9dSYoshinobu Inoue 		break;
779cfa1ca9dSYoshinobu Inoue 
780cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
781cfa1ca9dSYoshinobu Inoue #ifdef INET6
782cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
783cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
784cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
785cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
786cfa1ca9dSYoshinobu Inoue 			goto bad;
787cfa1ca9dSYoshinobu Inoue 		}
788cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
78933841545SHajimu UMEMOTO 		switch (cmd) {
790cfa1ca9dSYoshinobu Inoue #ifdef INET
79133841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
792cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
79333841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
794cfa1ca9dSYoshinobu Inoue 			break;
795cfa1ca9dSYoshinobu Inoue #endif /* INET */
796cfa1ca9dSYoshinobu Inoue #ifdef INET6
79733841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
798cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
799cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
80033841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
801cfa1ca9dSYoshinobu Inoue 			break;
802cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
803cfa1ca9dSYoshinobu Inoue 		default:
804cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
805cfa1ca9dSYoshinobu Inoue 			goto bad;
806cfa1ca9dSYoshinobu Inoue 		}
80733841545SHajimu UMEMOTO 		if (src->sa_len > size)
80833841545SHajimu UMEMOTO 			return EINVAL;
80933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
810a1f7e5f8SHajimu UMEMOTO #ifdef INET6
811a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
812a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
813a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
814a1f7e5f8SHajimu UMEMOTO 				return (error);
815a1f7e5f8SHajimu UMEMOTO 		}
816a1f7e5f8SHajimu UMEMOTO #endif
81733841545SHajimu UMEMOTO 		break;
81833841545SHajimu UMEMOTO 
81933841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
82033841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
82133841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
82233841545SHajimu UMEMOTO 			goto bad;
82333841545SHajimu UMEMOTO 		}
82433841545SHajimu UMEMOTO 
82533841545SHajimu UMEMOTO 		/* copy src */
82633841545SHajimu UMEMOTO 		src = sc->gif_psrc;
82733841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
82833841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
82933841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
83033841545SHajimu UMEMOTO 		if (src->sa_len > size)
83133841545SHajimu UMEMOTO 			return EINVAL;
83233841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
83333841545SHajimu UMEMOTO 
83433841545SHajimu UMEMOTO 		/* copy dst */
83533841545SHajimu UMEMOTO 		src = sc->gif_pdst;
83633841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
83733841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
83833841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
83933841545SHajimu UMEMOTO 		if (src->sa_len > size)
84033841545SHajimu UMEMOTO 			return EINVAL;
84133841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
842cfa1ca9dSYoshinobu Inoue 		break;
843cfa1ca9dSYoshinobu Inoue 
844cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
845686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
846cfa1ca9dSYoshinobu Inoue 		break;
847cfa1ca9dSYoshinobu Inoue 
848cfa1ca9dSYoshinobu Inoue 	default:
849cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
850cfa1ca9dSYoshinobu Inoue 		break;
851cfa1ca9dSYoshinobu Inoue 	}
852cfa1ca9dSYoshinobu Inoue  bad:
853cfa1ca9dSYoshinobu Inoue 	return error;
854cfa1ca9dSYoshinobu Inoue }
85553dab5feSBrooks Davis 
85617d5cb2dSRobert Watson /*
85717d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
85817d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
85917d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
86017d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
86117d5cb2dSRobert Watson  * a mutex.
86217d5cb2dSRobert Watson  */
8639426aedfSHajimu UMEMOTO int
8649426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
8659426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8669426aedfSHajimu UMEMOTO 	struct sockaddr *src;
8679426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
86853dab5feSBrooks Davis {
8698b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
870fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
8719426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
8729426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
8739426aedfSHajimu UMEMOTO 	int error = 0;
8749426aedfSHajimu UMEMOTO 
87517d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
876603724d3SBjoern A. Zeeb 	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
8779426aedfSHajimu UMEMOTO 		if (sc2 == sc)
8789426aedfSHajimu UMEMOTO 			continue;
8799426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
8809426aedfSHajimu UMEMOTO 			continue;
8819426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
8829426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
8839426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
8849426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
8859426aedfSHajimu UMEMOTO 			continue;
8869426aedfSHajimu UMEMOTO 
8879426aedfSHajimu UMEMOTO 		/*
8889426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
8899426aedfSHajimu UMEMOTO 		 * otherwise.
8909426aedfSHajimu UMEMOTO 		 */
891603724d3SBjoern A. Zeeb 		if (!V_parallel_tunnels &&
8929426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
8939426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
8949426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
89517d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
8969426aedfSHajimu UMEMOTO 			goto bad;
8979426aedfSHajimu UMEMOTO 		}
8989426aedfSHajimu UMEMOTO 
8999426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
9009426aedfSHajimu UMEMOTO 	}
90117d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
9029426aedfSHajimu UMEMOTO 
9039426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
9049426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
9059426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
9069426aedfSHajimu UMEMOTO #ifdef INET
9079426aedfSHajimu UMEMOTO 		case AF_INET:
9089426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
9099426aedfSHajimu UMEMOTO 			break;
9109426aedfSHajimu UMEMOTO #endif
9119426aedfSHajimu UMEMOTO #ifdef INET6
9129426aedfSHajimu UMEMOTO 		case AF_INET6:
9139426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
9149426aedfSHajimu UMEMOTO 			break;
9159426aedfSHajimu UMEMOTO #endif
9169426aedfSHajimu UMEMOTO 		}
9179426aedfSHajimu UMEMOTO 
9189426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
919a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
9209426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
9219426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
9229426aedfSHajimu UMEMOTO 
9239426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
924a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
9259426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
9269426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
9279426aedfSHajimu UMEMOTO 
9289426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
9299426aedfSHajimu UMEMOTO #ifdef INET
9309426aedfSHajimu UMEMOTO 	case AF_INET:
9319426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
9329426aedfSHajimu UMEMOTO 		break;
9339426aedfSHajimu UMEMOTO #endif
9349426aedfSHajimu UMEMOTO #ifdef INET6
9359426aedfSHajimu UMEMOTO 	case AF_INET6:
936a1f7e5f8SHajimu UMEMOTO 		/*
937a1f7e5f8SHajimu UMEMOTO 		 * Check validity of the scope zone ID of the addresses, and
938a1f7e5f8SHajimu UMEMOTO 		 * convert it into the kernel internal form if necessary.
939a1f7e5f8SHajimu UMEMOTO 		 */
940a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
941a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
942a1f7e5f8SHajimu UMEMOTO 			break;
943a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
944a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
945a1f7e5f8SHajimu UMEMOTO 			break;
9469426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
9479426aedfSHajimu UMEMOTO 		break;
9489426aedfSHajimu UMEMOTO #endif
9499426aedfSHajimu UMEMOTO 	}
9509426aedfSHajimu UMEMOTO 	if (error) {
9519426aedfSHajimu UMEMOTO 		/* rollback */
9529426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
9539426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
9549426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
9559426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
9569426aedfSHajimu UMEMOTO 		goto bad;
9579426aedfSHajimu UMEMOTO 	}
9589426aedfSHajimu UMEMOTO 
9599426aedfSHajimu UMEMOTO 	if (osrc)
9609426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
9619426aedfSHajimu UMEMOTO 	if (odst)
9629426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
9639426aedfSHajimu UMEMOTO 
9649426aedfSHajimu UMEMOTO  bad:
9659426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
96613f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
9679426aedfSHajimu UMEMOTO 	else
96813f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
9699426aedfSHajimu UMEMOTO 
9709426aedfSHajimu UMEMOTO 	return error;
9719426aedfSHajimu UMEMOTO }
9729426aedfSHajimu UMEMOTO 
9739426aedfSHajimu UMEMOTO void
9749426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
9759426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
9769426aedfSHajimu UMEMOTO {
977fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
97853dab5feSBrooks Davis 
97953dab5feSBrooks Davis 	if (sc->gif_psrc) {
98053dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
98153dab5feSBrooks Davis 		sc->gif_psrc = NULL;
98253dab5feSBrooks Davis 	}
98353dab5feSBrooks Davis 	if (sc->gif_pdst) {
98453dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
98553dab5feSBrooks Davis 		sc->gif_pdst = NULL;
98653dab5feSBrooks Davis 	}
9879426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
9889426aedfSHajimu UMEMOTO #ifdef INET
9899426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
9909426aedfSHajimu UMEMOTO #endif
9919426aedfSHajimu UMEMOTO #ifdef INET6
9929426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
9939426aedfSHajimu UMEMOTO #endif
99413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
99553dab5feSBrooks Davis }
996