xref: /freebsd/sys/net/if_gif.c (revision 1ed81b739e9e0f59a6195467758f5109d346ca4f)
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");
9744e33a07SMarko Zec 
98385195c0SMarko Zec #ifndef VIMAGE
99385195c0SMarko Zec #ifndef VIMAGE_GLOBALS
100385195c0SMarko Zec struct vnet_gif vnet_gif_0;
101385195c0SMarko Zec #endif
102385195c0SMarko Zec #endif
103385195c0SMarko Zec 
10444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
105e8783c4dSMike Smith static LIST_HEAD(, gif_softc) gif_softc_list;
10644e33a07SMarko Zec static int max_gif_nesting;
10744e33a07SMarko Zec static int parallel_tunnels;
10844e33a07SMarko Zec #ifdef INET
10944e33a07SMarko Zec int ip_gif_ttl;
11044e33a07SMarko Zec #endif
11144e33a07SMarko Zec #ifdef INET6
11244e33a07SMarko Zec int ip6_gif_hlim;
11344e33a07SMarko Zec #endif
11444e33a07SMarko Zec #endif
11553dab5feSBrooks Davis 
11694408d94SBrooks Davis void	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
11794408d94SBrooks Davis void	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
11894408d94SBrooks Davis void	(*ng_gif_attach_p)(struct ifnet *ifp);
11994408d94SBrooks Davis void	(*ng_gif_detach_p)(struct ifnet *ifp);
12094408d94SBrooks Davis 
12173ff045cSAndrew Thompson static void	gif_start(struct ifnet *);
1226b7330e2SSam Leffler static int	gif_clone_create(struct if_clone *, int, caddr_t);
123bb2bfb4fSBrooks Davis static void	gif_clone_destroy(struct ifnet *);
1241ed81b73SMarko Zec static int	vnet_gif_iattach(const void *);
12553dab5feSBrooks Davis 
126f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(gif, 0);
12753dab5feSBrooks Davis 
128929ddbbbSAlfred Perlstein static int gifmodevent(module_t, int, void *);
129cfa1ca9dSYoshinobu Inoue 
130872f786aSBrooks Davis SYSCTL_DECL(_net_link);
131872f786aSBrooks Davis SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
132872f786aSBrooks Davis     "Generic Tunnel Interface");
133686cdd19SJun-ichiro itojun Hagino #ifndef MAX_GIF_NEST
134686cdd19SJun-ichiro itojun Hagino /*
135872f786aSBrooks Davis  * This macro controls the default upper limitation on nesting of gif tunnels.
136686cdd19SJun-ichiro itojun Hagino  * Since, setting a large value to this macro with a careless configuration
137686cdd19SJun-ichiro itojun Hagino  * may introduce system crash, we don't allow any nestings by default.
138686cdd19SJun-ichiro itojun Hagino  * If you need to configure nested gif tunnels, you can define this macro
139686cdd19SJun-ichiro itojun Hagino  * in your kernel configuration file.  However, if you do so, please be
140686cdd19SJun-ichiro itojun Hagino  * careful to configure the tunnels so that it won't make a loop.
141686cdd19SJun-ichiro itojun Hagino  */
142686cdd19SJun-ichiro itojun Hagino #define MAX_GIF_NEST 1
143686cdd19SJun-ichiro itojun Hagino #endif
1448b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, max_nesting,
1458b615593SMarko Zec     CTLFLAG_RW, max_gif_nesting, 0, "Max nested tunnels");
1468b615593SMarko Zec 
1478b615593SMarko Zec #ifdef INET6
1488b615593SMarko Zec SYSCTL_DECL(_net_inet6_ip6);
1498b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_inet6_ip6, IPV6CTL_GIF_HLIM,
1508b615593SMarko Zec     gifhlim, CTLFLAG_RW, ip6_gif_hlim, 0, "");
1518b615593SMarko Zec #endif
152872f786aSBrooks Davis 
153872f786aSBrooks Davis /*
154872f786aSBrooks Davis  * By default, we disallow creation of multiple tunnels between the same
155872f786aSBrooks Davis  * pair of addresses.  Some applications require this functionality so
156872f786aSBrooks Davis  * we allow control over this check here.
157872f786aSBrooks Davis  */
1588b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_gif, _net_link_gif, OID_AUTO, parallel_tunnels,
1598b615593SMarko Zec     CTLFLAG_RW, parallel_tunnels, 0, "Allow parallel tunnels?");
160cfa1ca9dSYoshinobu Inoue 
16156abdd33SAndrew Thompson /* copy from src/sys/net/if_ethersubr.c */
16256abdd33SAndrew Thompson static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
16356abdd33SAndrew Thompson 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
16456abdd33SAndrew Thompson #ifndef ETHER_IS_BROADCAST
16556abdd33SAndrew Thompson #define ETHER_IS_BROADCAST(addr) \
16656abdd33SAndrew Thompson 	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
16756abdd33SAndrew Thompson #endif
16856abdd33SAndrew Thompson 
169bb2bfb4fSBrooks Davis static int
1706b7330e2SSam Leffler gif_clone_create(ifc, unit, params)
17153dab5feSBrooks Davis 	struct if_clone *ifc;
1723b16e7b2SMaxime Henrion 	int unit;
1736b7330e2SSam Leffler 	caddr_t params;
174cfa1ca9dSYoshinobu Inoue {
1758b615593SMarko Zec 	INIT_VNET_GIF(curvnet);
17633841545SHajimu UMEMOTO 	struct gif_softc *sc;
177cfa1ca9dSYoshinobu Inoue 
178e1a8c3dcSBruce M Simpson 	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
1798b07e49aSJulian Elischer 	sc->gif_fibnum = curthread->td_proc->p_fibnum;
180fc74a9f9SBrooks Davis 	GIF2IFP(sc) = if_alloc(IFT_GIF);
181fc74a9f9SBrooks Davis 	if (GIF2IFP(sc) == NULL) {
182fc74a9f9SBrooks Davis 		free(sc, M_GIF);
183fc74a9f9SBrooks Davis 		return (ENOSPC);
184fc74a9f9SBrooks Davis 	}
18553dab5feSBrooks Davis 
18625af0bb5SGleb Smirnoff 	GIF_LOCK_INIT(sc);
18725af0bb5SGleb Smirnoff 
188fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_softc = sc;
189fc74a9f9SBrooks Davis 	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
190686cdd19SJun-ichiro itojun Hagino 
1919426aedfSHajimu UMEMOTO 	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
1929426aedfSHajimu UMEMOTO 
193fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_addrlen = 0;
194fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_mtu    = GIF_MTU;
195fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
19633841545SHajimu UMEMOTO #if 0
19733841545SHajimu UMEMOTO 	/* turn off ingress filter */
198fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
19933841545SHajimu UMEMOTO #endif
200fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
20173ff045cSAndrew Thompson 	GIF2IFP(sc)->if_start  = gif_start;
202fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_output = gif_output;
203fc74a9f9SBrooks Davis 	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
204fc74a9f9SBrooks Davis 	if_attach(GIF2IFP(sc));
20501399f34SDavid Malone 	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
20694408d94SBrooks Davis 	if (ng_gif_attach_p != NULL)
207fc74a9f9SBrooks Davis 		(*ng_gif_attach_p)(GIF2IFP(sc));
20825af0bb5SGleb Smirnoff 
20925af0bb5SGleb Smirnoff 	mtx_lock(&gif_mtx);
210603724d3SBjoern A. Zeeb 	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
21125af0bb5SGleb Smirnoff 	mtx_unlock(&gif_mtx);
21225af0bb5SGleb Smirnoff 
21325af0bb5SGleb Smirnoff 	return (0);
214cfa1ca9dSYoshinobu Inoue }
215cfa1ca9dSYoshinobu Inoue 
21617d5cb2dSRobert Watson static void
217febd0759SAndrew Thompson gif_clone_destroy(ifp)
218febd0759SAndrew Thompson 	struct ifnet *ifp;
21953dab5feSBrooks Davis {
220e0de57f9SBjoern A. Zeeb #if defined(INET) || defined(INET6)
22153dab5feSBrooks Davis 	int err;
222e0de57f9SBjoern A. Zeeb #endif
223febd0759SAndrew Thompson 	struct gif_softc *sc = ifp->if_softc;
224febd0759SAndrew Thompson 
225febd0759SAndrew Thompson 	mtx_lock(&gif_mtx);
226febd0759SAndrew Thompson 	LIST_REMOVE(sc, gif_list);
227febd0759SAndrew Thompson 	mtx_unlock(&gif_mtx);
22853dab5feSBrooks Davis 
22917d5cb2dSRobert Watson 	gif_delete_tunnel(ifp);
2309426aedfSHajimu UMEMOTO #ifdef INET6
23153dab5feSBrooks Davis 	if (sc->encap_cookie6 != NULL) {
23253dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie6);
23353dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
23453dab5feSBrooks Davis 	}
2359426aedfSHajimu UMEMOTO #endif
2369426aedfSHajimu UMEMOTO #ifdef INET
2379426aedfSHajimu UMEMOTO 	if (sc->encap_cookie4 != NULL) {
2389426aedfSHajimu UMEMOTO 		err = encap_detach(sc->encap_cookie4);
2399426aedfSHajimu UMEMOTO 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
2409426aedfSHajimu UMEMOTO 	}
2419426aedfSHajimu UMEMOTO #endif
24253dab5feSBrooks Davis 
24394408d94SBrooks Davis 	if (ng_gif_detach_p != NULL)
24494408d94SBrooks Davis 		(*ng_gif_detach_p)(ifp);
24553dab5feSBrooks Davis 	bpfdetach(ifp);
24653dab5feSBrooks Davis 	if_detach(ifp);
247fc74a9f9SBrooks Davis 	if_free(ifp);
24853dab5feSBrooks Davis 
24925af0bb5SGleb Smirnoff 	GIF_LOCK_DESTROY(sc);
25025af0bb5SGleb Smirnoff 
25153dab5feSBrooks Davis 	free(sc, M_GIF);
25253dab5feSBrooks Davis }
25353dab5feSBrooks Davis 
25453dab5feSBrooks Davis static int
2551ed81b73SMarko Zec vnet_gif_iattach(const void *unused __unused)
2561ed81b73SMarko Zec {
2571ed81b73SMarko Zec 	INIT_VNET_GIF(curvnet);
2581ed81b73SMarko Zec 
2591ed81b73SMarko Zec 	LIST_INIT(&V_gif_softc_list);
2601ed81b73SMarko Zec 	V_max_gif_nesting = MAX_GIF_NEST;
2611ed81b73SMarko Zec #ifdef XBONEHACK
2621ed81b73SMarko Zec 	V_parallel_tunnels = 1;
2631ed81b73SMarko Zec #else
2641ed81b73SMarko Zec 	V_parallel_tunnels = 0;
2651ed81b73SMarko Zec #endif
2661ed81b73SMarko Zec 	V_ip_gif_ttl = GIF_TTL;
2671ed81b73SMarko Zec #ifdef INET6
2681ed81b73SMarko Zec 	V_ip6_gif_hlim = GIF_HLIM;
2691ed81b73SMarko Zec #endif
2701ed81b73SMarko Zec 
2711ed81b73SMarko Zec 	return (0);
2721ed81b73SMarko Zec }
2731ed81b73SMarko Zec 
2741ed81b73SMarko Zec static int
27553dab5feSBrooks Davis gifmodevent(mod, type, data)
27653dab5feSBrooks Davis 	module_t mod;
27753dab5feSBrooks Davis 	int type;
27853dab5feSBrooks Davis 	void *data;
27953dab5feSBrooks Davis {
28053dab5feSBrooks Davis 
28153dab5feSBrooks Davis 	switch (type) {
28253dab5feSBrooks Davis 	case MOD_LOAD:
28317d5cb2dSRobert Watson 		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
28453dab5feSBrooks Davis 
2851ed81b73SMarko Zec 		vnet_gif_iattach(NULL);
28644e33a07SMarko Zec 		if_clone_attach(&gif_cloner);
28753dab5feSBrooks Davis 
28853dab5feSBrooks Davis 		break;
28953dab5feSBrooks Davis 	case MOD_UNLOAD:
29053dab5feSBrooks Davis 		if_clone_detach(&gif_cloner);
29117d5cb2dSRobert Watson 		mtx_destroy(&gif_mtx);
29253dab5feSBrooks Davis #ifdef INET6
2931ed81b73SMarko Zec 		V_ip6_gif_hlim = 0;	/* XXX -> vnet_gif_idetach() */
29453dab5feSBrooks Davis #endif
29553dab5feSBrooks Davis 		break;
2963e019deaSPoul-Henning Kamp 	default:
2973e019deaSPoul-Henning Kamp 		return EOPNOTSUPP;
29853dab5feSBrooks Davis 	}
29953dab5feSBrooks Davis 	return 0;
30053dab5feSBrooks Davis }
30153dab5feSBrooks Davis 
30253dab5feSBrooks Davis static moduledata_t gif_mod = {
30353dab5feSBrooks Davis 	"if_gif",
30453dab5feSBrooks Davis 	gifmodevent,
30553dab5feSBrooks Davis 	0
30653dab5feSBrooks Davis };
30753dab5feSBrooks Davis 
30853dab5feSBrooks Davis DECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
30920af0ffaSBrooks Davis MODULE_VERSION(if_gif, 1);
310cfa1ca9dSYoshinobu Inoue 
3119426aedfSHajimu UMEMOTO int
312686cdd19SJun-ichiro itojun Hagino gif_encapcheck(m, off, proto, arg)
313686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
314686cdd19SJun-ichiro itojun Hagino 	int off;
315686cdd19SJun-ichiro itojun Hagino 	int proto;
316686cdd19SJun-ichiro itojun Hagino 	void *arg;
317686cdd19SJun-ichiro itojun Hagino {
318686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
319686cdd19SJun-ichiro itojun Hagino 	struct gif_softc *sc;
320686cdd19SJun-ichiro itojun Hagino 
321686cdd19SJun-ichiro itojun Hagino 	sc = (struct gif_softc *)arg;
322686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
323686cdd19SJun-ichiro itojun Hagino 		return 0;
324686cdd19SJun-ichiro itojun Hagino 
325fc74a9f9SBrooks Davis 	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
326686cdd19SJun-ichiro itojun Hagino 		return 0;
327686cdd19SJun-ichiro itojun Hagino 
328686cdd19SJun-ichiro itojun Hagino 	/* no physical address */
329686cdd19SJun-ichiro itojun Hagino 	if (!sc->gif_psrc || !sc->gif_pdst)
330686cdd19SJun-ichiro itojun Hagino 		return 0;
331686cdd19SJun-ichiro itojun Hagino 
332686cdd19SJun-ichiro itojun Hagino 	switch (proto) {
333686cdd19SJun-ichiro itojun Hagino #ifdef INET
334686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV4:
335686cdd19SJun-ichiro itojun Hagino 		break;
336686cdd19SJun-ichiro itojun Hagino #endif
337686cdd19SJun-ichiro itojun Hagino #ifdef INET6
338686cdd19SJun-ichiro itojun Hagino 	case IPPROTO_IPV6:
339686cdd19SJun-ichiro itojun Hagino 		break;
340686cdd19SJun-ichiro itojun Hagino #endif
34173ff045cSAndrew Thompson 	case IPPROTO_ETHERIP:
34273ff045cSAndrew Thompson 		break;
34373ff045cSAndrew Thompson 
344686cdd19SJun-ichiro itojun Hagino 	default:
345686cdd19SJun-ichiro itojun Hagino 		return 0;
346686cdd19SJun-ichiro itojun Hagino 	}
347686cdd19SJun-ichiro itojun Hagino 
3483bb61ca6SHajimu UMEMOTO 	/* Bail on short packets */
3493bb61ca6SHajimu UMEMOTO 	if (m->m_pkthdr.len < sizeof(ip))
3503bb61ca6SHajimu UMEMOTO 		return 0;
3513bb61ca6SHajimu UMEMOTO 
3526f4ded3aSBrooks Davis 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
353686cdd19SJun-ichiro itojun Hagino 
354686cdd19SJun-ichiro itojun Hagino 	switch (ip.ip_v) {
355686cdd19SJun-ichiro itojun Hagino #ifdef INET
356686cdd19SJun-ichiro itojun Hagino 	case 4:
357686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET ||
358686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET)
359686cdd19SJun-ichiro itojun Hagino 			return 0;
360686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck4(m, off, proto, arg);
361686cdd19SJun-ichiro itojun Hagino #endif
362686cdd19SJun-ichiro itojun Hagino #ifdef INET6
363686cdd19SJun-ichiro itojun Hagino 	case 6:
3649426aedfSHajimu UMEMOTO 		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
3659426aedfSHajimu UMEMOTO 			return 0;
366686cdd19SJun-ichiro itojun Hagino 		if (sc->gif_psrc->sa_family != AF_INET6 ||
367686cdd19SJun-ichiro itojun Hagino 		    sc->gif_pdst->sa_family != AF_INET6)
368686cdd19SJun-ichiro itojun Hagino 			return 0;
369686cdd19SJun-ichiro itojun Hagino 		return gif_encapcheck6(m, off, proto, arg);
370686cdd19SJun-ichiro itojun Hagino #endif
371686cdd19SJun-ichiro itojun Hagino 	default:
372686cdd19SJun-ichiro itojun Hagino 		return 0;
373686cdd19SJun-ichiro itojun Hagino 	}
374686cdd19SJun-ichiro itojun Hagino }
375686cdd19SJun-ichiro itojun Hagino 
37673ff045cSAndrew Thompson static void
37773ff045cSAndrew Thompson gif_start(struct ifnet *ifp)
37873ff045cSAndrew Thompson {
37973ff045cSAndrew Thompson 	struct gif_softc *sc;
38073ff045cSAndrew Thompson 	struct mbuf *m;
38173ff045cSAndrew Thompson 
38273ff045cSAndrew Thompson 	sc = ifp->if_softc;
38373ff045cSAndrew Thompson 
38473ff045cSAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
38573ff045cSAndrew Thompson 	for (;;) {
38673ff045cSAndrew Thompson 		IFQ_DEQUEUE(&ifp->if_snd, m);
38773ff045cSAndrew Thompson 		if (m == 0)
38873ff045cSAndrew Thompson 			break;
38973ff045cSAndrew Thompson 
39073ff045cSAndrew Thompson 		gif_output(ifp, m, sc->gif_pdst, NULL);
39173ff045cSAndrew Thompson 
39273ff045cSAndrew Thompson 	}
39373ff045cSAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
39473ff045cSAndrew Thompson 
39573ff045cSAndrew Thompson 	return;
39673ff045cSAndrew Thompson }
39773ff045cSAndrew Thompson 
398cfa1ca9dSYoshinobu Inoue int
399cfa1ca9dSYoshinobu Inoue gif_output(ifp, m, dst, rt)
400cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
401cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
402cfa1ca9dSYoshinobu Inoue 	struct sockaddr *dst;
403cfa1ca9dSYoshinobu Inoue 	struct rtentry *rt;	/* added in net2 */
404cfa1ca9dSYoshinobu Inoue {
4058b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
406fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
4078c7e1947SRuslan Ermilov 	struct m_tag *mtag;
408cfa1ca9dSYoshinobu Inoue 	int error = 0;
4098c7e1947SRuslan Ermilov 	int gif_called;
41001399f34SDavid Malone 	u_int32_t af;
411cfa1ca9dSYoshinobu Inoue 
41210722b85SRobert Watson #ifdef MAC
41330d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
414e0852ce2SRobert Watson 	if (error) {
415e0852ce2SRobert Watson 		m_freem(m);
416e0852ce2SRobert Watson 		goto end;
417e0852ce2SRobert Watson 	}
41810722b85SRobert Watson #endif
41910722b85SRobert Watson 
420cfa1ca9dSYoshinobu Inoue 	/*
421cfa1ca9dSYoshinobu Inoue 	 * gif may cause infinite recursion calls when misconfigured.
4228c7e1947SRuslan Ermilov 	 * We'll prevent this by detecting loops.
4238c7e1947SRuslan Ermilov 	 *
4248c7e1947SRuslan Ermilov 	 * High nesting level may cause stack exhaustion.
425cfa1ca9dSYoshinobu Inoue 	 * We'll prevent this by introducing upper limit.
426cfa1ca9dSYoshinobu Inoue 	 */
4278c7e1947SRuslan Ermilov 	gif_called = 1;
4288c7e1947SRuslan Ermilov 	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
4298c7e1947SRuslan Ermilov 	while (mtag != NULL) {
4308c7e1947SRuslan Ermilov 		if (*(struct ifnet **)(mtag + 1) == ifp) {
4318c7e1947SRuslan Ermilov 			log(LOG_NOTICE,
4328c7e1947SRuslan Ermilov 			    "gif_output: loop detected on %s\n",
4338c7e1947SRuslan Ermilov 			    (*(struct ifnet **)(mtag + 1))->if_xname);
4348c7e1947SRuslan Ermilov 			m_freem(m);
4358c7e1947SRuslan Ermilov 			error = EIO;	/* is there better errno? */
4368c7e1947SRuslan Ermilov 			goto end;
4378c7e1947SRuslan Ermilov 		}
4388c7e1947SRuslan Ermilov 		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
4398c7e1947SRuslan Ermilov 		gif_called++;
4408c7e1947SRuslan Ermilov 	}
441603724d3SBjoern A. Zeeb 	if (gif_called > V_max_gif_nesting) {
442cfa1ca9dSYoshinobu Inoue 		log(LOG_NOTICE,
443cfa1ca9dSYoshinobu Inoue 		    "gif_output: recursively called too many times(%d)\n",
444523ebc4eSRobert Watson 		    gif_called);
445cfa1ca9dSYoshinobu Inoue 		m_freem(m);
446cfa1ca9dSYoshinobu Inoue 		error = EIO;	/* is there better errno? */
447cfa1ca9dSYoshinobu Inoue 		goto end;
448cfa1ca9dSYoshinobu Inoue 	}
4498c7e1947SRuslan Ermilov 	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
4508c7e1947SRuslan Ermilov 	    M_NOWAIT);
4518c7e1947SRuslan Ermilov 	if (mtag == NULL) {
4528c7e1947SRuslan Ermilov 		m_freem(m);
4538c7e1947SRuslan Ermilov 		error = ENOMEM;
4548c7e1947SRuslan Ermilov 		goto end;
4558c7e1947SRuslan Ermilov 	}
4568c7e1947SRuslan Ermilov 	*(struct ifnet **)(mtag + 1) = ifp;
4578c7e1947SRuslan Ermilov 	m_tag_prepend(m, mtag);
458686cdd19SJun-ichiro itojun Hagino 
459cfa1ca9dSYoshinobu Inoue 	m->m_flags &= ~(M_BCAST|M_MCAST);
46025af0bb5SGleb Smirnoff 
46125af0bb5SGleb Smirnoff 	GIF_LOCK(sc);
46225af0bb5SGleb Smirnoff 
463cfa1ca9dSYoshinobu Inoue 	if (!(ifp->if_flags & IFF_UP) ||
464cfa1ca9dSYoshinobu Inoue 	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
4656e860629SGleb Smirnoff 		GIF_UNLOCK(sc);
466cfa1ca9dSYoshinobu Inoue 		m_freem(m);
467cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
468cfa1ca9dSYoshinobu Inoue 		goto end;
469cfa1ca9dSYoshinobu Inoue 	}
470cfa1ca9dSYoshinobu Inoue 
47101399f34SDavid Malone 	/* BPF writes need to be handled specially. */
47201399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
47301399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
47401399f34SDavid Malone 		dst->sa_family = af;
47501399f34SDavid Malone 	}
47601399f34SDavid Malone 
47701399f34SDavid Malone 	af = dst->sa_family;
47816d878ccSChristian S.J. Peron 	BPF_MTAP2(ifp, &af, sizeof(af), m);
479cfa1ca9dSYoshinobu Inoue 	ifp->if_opackets++;
480cfa1ca9dSYoshinobu Inoue 	ifp->if_obytes += m->m_pkthdr.len;
481cfa1ca9dSYoshinobu Inoue 
48273ff045cSAndrew Thompson 	/* override to IPPROTO_ETHERIP for bridged traffic */
48373ff045cSAndrew Thompson 	if (ifp->if_bridge)
48473ff045cSAndrew Thompson 		af = AF_LINK;
48573ff045cSAndrew Thompson 
4868b07e49aSJulian Elischer 	M_SETFIB(m, sc->gif_fibnum);
48733841545SHajimu UMEMOTO 	/* inner AF-specific encapsulation */
48833841545SHajimu UMEMOTO 
489686cdd19SJun-ichiro itojun Hagino 	/* XXX should we check if our outer source is legal? */
490686cdd19SJun-ichiro itojun Hagino 
49133841545SHajimu UMEMOTO 	/* dispatch to output logic based on outer AF */
492cfa1ca9dSYoshinobu Inoue 	switch (sc->gif_psrc->sa_family) {
493cfa1ca9dSYoshinobu Inoue #ifdef INET
494cfa1ca9dSYoshinobu Inoue 	case AF_INET:
49573ff045cSAndrew Thompson 		error = in_gif_output(ifp, af, m);
496cfa1ca9dSYoshinobu Inoue 		break;
497cfa1ca9dSYoshinobu Inoue #endif
498cfa1ca9dSYoshinobu Inoue #ifdef INET6
499cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
50073ff045cSAndrew Thompson 		error = in6_gif_output(ifp, af, m);
501cfa1ca9dSYoshinobu Inoue 		break;
502cfa1ca9dSYoshinobu Inoue #endif
503cfa1ca9dSYoshinobu Inoue 	default:
504cfa1ca9dSYoshinobu Inoue 		m_freem(m);
505cfa1ca9dSYoshinobu Inoue 		error = ENETDOWN;
506cfa1ca9dSYoshinobu Inoue 	}
507cfa1ca9dSYoshinobu Inoue 
5086e860629SGleb Smirnoff 	GIF_UNLOCK(sc);
509cfa1ca9dSYoshinobu Inoue   end:
51033841545SHajimu UMEMOTO 	if (error)
51133841545SHajimu UMEMOTO 		ifp->if_oerrors++;
51225af0bb5SGleb Smirnoff 	return (error);
513cfa1ca9dSYoshinobu Inoue }
514cfa1ca9dSYoshinobu Inoue 
515cfa1ca9dSYoshinobu Inoue void
51621fb391fSHajimu UMEMOTO gif_input(m, af, ifp)
517cfa1ca9dSYoshinobu Inoue 	struct mbuf *m;
518cfa1ca9dSYoshinobu Inoue 	int af;
51921fb391fSHajimu UMEMOTO 	struct ifnet *ifp;
520cfa1ca9dSYoshinobu Inoue {
52173ff045cSAndrew Thompson 	int isr, n;
52273ff045cSAndrew Thompson 	struct etherip_header *eip;
52356abdd33SAndrew Thompson 	struct ether_header *eh;
52456abdd33SAndrew Thompson 	struct ifnet *oldifp;
525cfa1ca9dSYoshinobu Inoue 
52621fb391fSHajimu UMEMOTO 	if (ifp == NULL) {
527cfa1ca9dSYoshinobu Inoue 		/* just in case */
528cfa1ca9dSYoshinobu Inoue 		m_freem(m);
529cfa1ca9dSYoshinobu Inoue 		return;
530cfa1ca9dSYoshinobu Inoue 	}
531cfa1ca9dSYoshinobu Inoue 
53221fb391fSHajimu UMEMOTO 	m->m_pkthdr.rcvif = ifp;
533cfa1ca9dSYoshinobu Inoue 
53410722b85SRobert Watson #ifdef MAC
53530d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
53610722b85SRobert Watson #endif
53710722b85SRobert Watson 
53816d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
53933841545SHajimu UMEMOTO 		u_int32_t af1 = af;
540437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
541cfa1ca9dSYoshinobu Inoue 	}
542cfa1ca9dSYoshinobu Inoue 
54394408d94SBrooks Davis 	if (ng_gif_input_p != NULL) {
54421fb391fSHajimu UMEMOTO 		(*ng_gif_input_p)(ifp, &m, af);
54594408d94SBrooks Davis 		if (m == NULL)
54694408d94SBrooks Davis 			return;
54794408d94SBrooks Davis 	}
54894408d94SBrooks Davis 
549cfa1ca9dSYoshinobu Inoue 	/*
550cfa1ca9dSYoshinobu Inoue 	 * Put the packet to the network layer input queue according to the
551cfa1ca9dSYoshinobu Inoue 	 * specified address family.
552cfa1ca9dSYoshinobu Inoue 	 * Note: older versions of gif_input directly called network layer
553cfa1ca9dSYoshinobu Inoue 	 * input functions, e.g. ip6_input, here.  We changed the policy to
554cfa1ca9dSYoshinobu Inoue 	 * prevent too many recursive calls of such input functions, which
555cfa1ca9dSYoshinobu Inoue 	 * might cause kernel panic.  But the change may introduce another
556cfa1ca9dSYoshinobu Inoue 	 * problem; if the input queue is full, packets are discarded.
55788ff5695SSUZUKI Shinsuke 	 * The kernel stack overflow really happened, and we believed
55888ff5695SSUZUKI Shinsuke 	 * queue-full rarely occurs, so we changed the policy.
559cfa1ca9dSYoshinobu Inoue 	 */
560cfa1ca9dSYoshinobu Inoue 	switch (af) {
561cfa1ca9dSYoshinobu Inoue #ifdef INET
562cfa1ca9dSYoshinobu Inoue 	case AF_INET:
563cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IP;
564cfa1ca9dSYoshinobu Inoue 		break;
565cfa1ca9dSYoshinobu Inoue #endif
566cfa1ca9dSYoshinobu Inoue #ifdef INET6
567cfa1ca9dSYoshinobu Inoue 	case AF_INET6:
568cfa1ca9dSYoshinobu Inoue 		isr = NETISR_IPV6;
569cfa1ca9dSYoshinobu Inoue 		break;
570cfa1ca9dSYoshinobu Inoue #endif
57173ff045cSAndrew Thompson 	case AF_LINK:
57273ff045cSAndrew Thompson 		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
57373ff045cSAndrew Thompson 		if (n > m->m_len) {
57473ff045cSAndrew Thompson 			m = m_pullup(m, n);
57573ff045cSAndrew Thompson 			if (m == NULL) {
57673ff045cSAndrew Thompson 				ifp->if_ierrors++;
57773ff045cSAndrew Thompson 				return;
57873ff045cSAndrew Thompson 			}
57973ff045cSAndrew Thompson 		}
58073ff045cSAndrew Thompson 
58173ff045cSAndrew Thompson 		eip = mtod(m, struct etherip_header *);
58273ff045cSAndrew Thompson  		if (eip->eip_ver !=
58373ff045cSAndrew Thompson 		    (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
58473ff045cSAndrew Thompson 			/* discard unknown versions */
58573ff045cSAndrew Thompson 			m_freem(m);
58673ff045cSAndrew Thompson 			return;
58773ff045cSAndrew Thompson 		}
58873ff045cSAndrew Thompson 		m_adj(m, sizeof(struct etherip_header));
58973ff045cSAndrew Thompson 
59073ff045cSAndrew Thompson 		m->m_flags &= ~(M_BCAST|M_MCAST);
59173ff045cSAndrew Thompson 		m->m_pkthdr.rcvif = ifp;
59273ff045cSAndrew Thompson 
59356abdd33SAndrew Thompson 		if (ifp->if_bridge) {
59456abdd33SAndrew Thompson 			oldifp = ifp;
59556abdd33SAndrew Thompson 			eh = mtod(m, struct ether_header *);
59656abdd33SAndrew Thompson 			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
59756abdd33SAndrew Thompson 				if (ETHER_IS_BROADCAST(eh->ether_dhost))
59856abdd33SAndrew Thompson 					m->m_flags |= M_BCAST;
59956abdd33SAndrew Thompson 				else
60056abdd33SAndrew Thompson 					m->m_flags |= M_MCAST;
60156abdd33SAndrew Thompson 				ifp->if_imcasts++;
60256abdd33SAndrew Thompson 			}
60373ff045cSAndrew Thompson 			BRIDGE_INPUT(ifp, m);
60473ff045cSAndrew Thompson 
60556abdd33SAndrew Thompson 			if (m != NULL && ifp != oldifp) {
60656abdd33SAndrew Thompson 				/*
60756abdd33SAndrew Thompson 				 * The bridge gave us back itself or one of the
60856abdd33SAndrew Thompson 				 * members for which the frame is addressed.
60956abdd33SAndrew Thompson 				 */
61056abdd33SAndrew Thompson 				ether_demux(ifp, m);
61156abdd33SAndrew Thompson 				return;
61256abdd33SAndrew Thompson 			}
61356abdd33SAndrew Thompson 		}
61473ff045cSAndrew Thompson 		if (m != NULL)
61573ff045cSAndrew Thompson 			m_freem(m);
61673ff045cSAndrew Thompson 		return;
61773ff045cSAndrew Thompson 
618cfa1ca9dSYoshinobu Inoue 	default:
61994408d94SBrooks Davis 		if (ng_gif_input_orphan_p != NULL)
62021fb391fSHajimu UMEMOTO 			(*ng_gif_input_orphan_p)(ifp, m, af);
62194408d94SBrooks Davis 		else
622cfa1ca9dSYoshinobu Inoue 			m_freem(m);
623cfa1ca9dSYoshinobu Inoue 		return;
624cfa1ca9dSYoshinobu Inoue 	}
625cfa1ca9dSYoshinobu Inoue 
62621fb391fSHajimu UMEMOTO 	ifp->if_ipackets++;
62721fb391fSHajimu UMEMOTO 	ifp->if_ibytes += m->m_pkthdr.len;
6281cafed39SJonathan Lemon 	netisr_dispatch(isr, m);
629cfa1ca9dSYoshinobu Inoue }
630cfa1ca9dSYoshinobu Inoue 
631686cdd19SJun-ichiro itojun Hagino /* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
632cfa1ca9dSYoshinobu Inoue int
633cfa1ca9dSYoshinobu Inoue gif_ioctl(ifp, cmd, data)
634cfa1ca9dSYoshinobu Inoue 	struct ifnet *ifp;
635cfa1ca9dSYoshinobu Inoue 	u_long cmd;
636cfa1ca9dSYoshinobu Inoue 	caddr_t data;
637cfa1ca9dSYoshinobu Inoue {
638fc74a9f9SBrooks Davis 	struct gif_softc *sc  = ifp->if_softc;
639cfa1ca9dSYoshinobu Inoue 	struct ifreq     *ifr = (struct ifreq*)data;
640cfa1ca9dSYoshinobu Inoue 	int error = 0, size;
641686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst, *src;
6423bb61ca6SHajimu UMEMOTO #ifdef	SIOCSIFMTU /* xxx */
6433bb61ca6SHajimu UMEMOTO 	u_long mtu;
6443bb61ca6SHajimu UMEMOTO #endif
645cfa1ca9dSYoshinobu Inoue 
646cfa1ca9dSYoshinobu Inoue 	switch (cmd) {
647cfa1ca9dSYoshinobu Inoue 	case SIOCSIFADDR:
6489426aedfSHajimu UMEMOTO 		ifp->if_flags |= IFF_UP;
649cfa1ca9dSYoshinobu Inoue 		break;
650cfa1ca9dSYoshinobu Inoue 
651cfa1ca9dSYoshinobu Inoue 	case SIOCSIFDSTADDR:
652cfa1ca9dSYoshinobu Inoue 		break;
653cfa1ca9dSYoshinobu Inoue 
654cfa1ca9dSYoshinobu Inoue 	case SIOCADDMULTI:
655cfa1ca9dSYoshinobu Inoue 	case SIOCDELMULTI:
656cfa1ca9dSYoshinobu Inoue 		break;
657cfa1ca9dSYoshinobu Inoue 
658686cdd19SJun-ichiro itojun Hagino #ifdef	SIOCSIFMTU /* xxx */
659cfa1ca9dSYoshinobu Inoue 	case SIOCGIFMTU:
660cfa1ca9dSYoshinobu Inoue 		break;
661686cdd19SJun-ichiro itojun Hagino 
662cfa1ca9dSYoshinobu Inoue 	case SIOCSIFMTU:
663cfa1ca9dSYoshinobu Inoue 		mtu = ifr->ifr_mtu;
6643bb61ca6SHajimu UMEMOTO 		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
665cfa1ca9dSYoshinobu Inoue 			return (EINVAL);
666cfa1ca9dSYoshinobu Inoue 		ifp->if_mtu = mtu;
667cfa1ca9dSYoshinobu Inoue 		break;
668686cdd19SJun-ichiro itojun Hagino #endif /* SIOCSIFMTU */
669cfa1ca9dSYoshinobu Inoue 
6703bb61ca6SHajimu UMEMOTO #ifdef INET
671cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR:
6723bb61ca6SHajimu UMEMOTO #endif
673cfa1ca9dSYoshinobu Inoue #ifdef INET6
674cfa1ca9dSYoshinobu Inoue 	case SIOCSIFPHYADDR_IN6:
675cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
67633841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
677686cdd19SJun-ichiro itojun Hagino 		switch (cmd) {
67833841545SHajimu UMEMOTO #ifdef INET
679686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR:
680cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
681cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_addr);
682cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
683cfa1ca9dSYoshinobu Inoue 				&(((struct in_aliasreq *)data)->ifra_dstaddr);
684cfa1ca9dSYoshinobu Inoue 			break;
68533841545SHajimu UMEMOTO #endif
686cfa1ca9dSYoshinobu Inoue #ifdef INET6
687686cdd19SJun-ichiro itojun Hagino 		case SIOCSIFPHYADDR_IN6:
688cfa1ca9dSYoshinobu Inoue 			src = (struct sockaddr *)
689cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_addr);
690cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
691cfa1ca9dSYoshinobu Inoue 				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
692686cdd19SJun-ichiro itojun Hagino 			break;
693686cdd19SJun-ichiro itojun Hagino #endif
69433841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
69533841545SHajimu UMEMOTO 			src = (struct sockaddr *)
69633841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->addr);
69733841545SHajimu UMEMOTO 			dst = (struct sockaddr *)
69833841545SHajimu UMEMOTO 				&(((struct if_laddrreq *)data)->dstaddr);
6999426aedfSHajimu UMEMOTO 			break;
7006f4ded3aSBrooks Davis 		default:
7019426aedfSHajimu UMEMOTO 			return EINVAL;
70233841545SHajimu UMEMOTO 		}
70333841545SHajimu UMEMOTO 
70433841545SHajimu UMEMOTO 		/* sa_family must be equal */
70533841545SHajimu UMEMOTO 		if (src->sa_family != dst->sa_family)
70633841545SHajimu UMEMOTO 			return EINVAL;
70733841545SHajimu UMEMOTO 
70833841545SHajimu UMEMOTO 		/* validate sa_len */
70933841545SHajimu UMEMOTO 		switch (src->sa_family) {
71033841545SHajimu UMEMOTO #ifdef INET
71133841545SHajimu UMEMOTO 		case AF_INET:
71233841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in))
71333841545SHajimu UMEMOTO 				return EINVAL;
71433841545SHajimu UMEMOTO 			break;
71533841545SHajimu UMEMOTO #endif
71633841545SHajimu UMEMOTO #ifdef INET6
71733841545SHajimu UMEMOTO 		case AF_INET6:
71833841545SHajimu UMEMOTO 			if (src->sa_len != sizeof(struct sockaddr_in6))
71933841545SHajimu UMEMOTO 				return EINVAL;
72033841545SHajimu UMEMOTO 			break;
72133841545SHajimu UMEMOTO #endif
72233841545SHajimu UMEMOTO 		default:
72333841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
72433841545SHajimu UMEMOTO 		}
72533841545SHajimu UMEMOTO 		switch (dst->sa_family) {
72633841545SHajimu UMEMOTO #ifdef INET
72733841545SHajimu UMEMOTO 		case AF_INET:
72833841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in))
72933841545SHajimu UMEMOTO 				return EINVAL;
73033841545SHajimu UMEMOTO 			break;
73133841545SHajimu UMEMOTO #endif
73233841545SHajimu UMEMOTO #ifdef INET6
73333841545SHajimu UMEMOTO 		case AF_INET6:
73433841545SHajimu UMEMOTO 			if (dst->sa_len != sizeof(struct sockaddr_in6))
73533841545SHajimu UMEMOTO 				return EINVAL;
73633841545SHajimu UMEMOTO 			break;
73733841545SHajimu UMEMOTO #endif
73833841545SHajimu UMEMOTO 		default:
73933841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
74033841545SHajimu UMEMOTO 		}
74133841545SHajimu UMEMOTO 
74233841545SHajimu UMEMOTO 		/* check sa_family looks sane for the cmd */
74333841545SHajimu UMEMOTO 		switch (cmd) {
74433841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR:
74533841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET)
74633841545SHajimu UMEMOTO 				break;
74733841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
74833841545SHajimu UMEMOTO #ifdef INET6
74933841545SHajimu UMEMOTO 		case SIOCSIFPHYADDR_IN6:
75033841545SHajimu UMEMOTO 			if (src->sa_family == AF_INET6)
75133841545SHajimu UMEMOTO 				break;
75233841545SHajimu UMEMOTO 			return EAFNOSUPPORT;
75333841545SHajimu UMEMOTO #endif /* INET6 */
75433841545SHajimu UMEMOTO 		case SIOCSLIFPHYADDR:
75533841545SHajimu UMEMOTO 			/* checks done in the above */
75633841545SHajimu UMEMOTO 			break;
757686cdd19SJun-ichiro itojun Hagino 		}
758cfa1ca9dSYoshinobu Inoue 
759fc74a9f9SBrooks Davis 		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
760cfa1ca9dSYoshinobu Inoue 		break;
761cfa1ca9dSYoshinobu Inoue 
762686cdd19SJun-ichiro itojun Hagino #ifdef SIOCDIFPHYADDR
763686cdd19SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
764fc74a9f9SBrooks Davis 		gif_delete_tunnel(GIF2IFP(sc));
765686cdd19SJun-ichiro itojun Hagino 		break;
766686cdd19SJun-ichiro itojun Hagino #endif
767686cdd19SJun-ichiro itojun Hagino 
768cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR:
769cfa1ca9dSYoshinobu Inoue #ifdef INET6
770cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPSRCADDR_IN6:
771cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
772cfa1ca9dSYoshinobu Inoue 		if (sc->gif_psrc == NULL) {
773cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
774cfa1ca9dSYoshinobu Inoue 			goto bad;
775cfa1ca9dSYoshinobu Inoue 		}
776cfa1ca9dSYoshinobu Inoue 		src = sc->gif_psrc;
77733841545SHajimu UMEMOTO 		switch (cmd) {
778cfa1ca9dSYoshinobu Inoue #ifdef INET
77933841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR:
780cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
78133841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
782cfa1ca9dSYoshinobu Inoue 			break;
783cfa1ca9dSYoshinobu Inoue #endif /* INET */
784cfa1ca9dSYoshinobu Inoue #ifdef INET6
78533841545SHajimu UMEMOTO 		case SIOCGIFPSRCADDR_IN6:
786cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
787cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
78833841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
789cfa1ca9dSYoshinobu Inoue 			break;
790cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
791cfa1ca9dSYoshinobu Inoue 		default:
792cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
793cfa1ca9dSYoshinobu Inoue 			goto bad;
794cfa1ca9dSYoshinobu Inoue 		}
79533841545SHajimu UMEMOTO 		if (src->sa_len > size)
79633841545SHajimu UMEMOTO 			return EINVAL;
79733841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
798a1f7e5f8SHajimu UMEMOTO #ifdef INET6
799a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
800a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
801a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
802a1f7e5f8SHajimu UMEMOTO 				return (error);
803a1f7e5f8SHajimu UMEMOTO 		}
804a1f7e5f8SHajimu UMEMOTO #endif
805cfa1ca9dSYoshinobu Inoue 		break;
806cfa1ca9dSYoshinobu Inoue 
807cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR:
808cfa1ca9dSYoshinobu Inoue #ifdef INET6
809cfa1ca9dSYoshinobu Inoue 	case SIOCGIFPDSTADDR_IN6:
810cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
811cfa1ca9dSYoshinobu Inoue 		if (sc->gif_pdst == NULL) {
812cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
813cfa1ca9dSYoshinobu Inoue 			goto bad;
814cfa1ca9dSYoshinobu Inoue 		}
815cfa1ca9dSYoshinobu Inoue 		src = sc->gif_pdst;
81633841545SHajimu UMEMOTO 		switch (cmd) {
817cfa1ca9dSYoshinobu Inoue #ifdef INET
81833841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR:
819cfa1ca9dSYoshinobu Inoue 			dst = &ifr->ifr_addr;
82033841545SHajimu UMEMOTO 			size = sizeof(ifr->ifr_addr);
821cfa1ca9dSYoshinobu Inoue 			break;
822cfa1ca9dSYoshinobu Inoue #endif /* INET */
823cfa1ca9dSYoshinobu Inoue #ifdef INET6
82433841545SHajimu UMEMOTO 		case SIOCGIFPDSTADDR_IN6:
825cfa1ca9dSYoshinobu Inoue 			dst = (struct sockaddr *)
826cfa1ca9dSYoshinobu Inoue 				&(((struct in6_ifreq *)data)->ifr_addr);
82733841545SHajimu UMEMOTO 			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
828cfa1ca9dSYoshinobu Inoue 			break;
829cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
830cfa1ca9dSYoshinobu Inoue 		default:
831cfa1ca9dSYoshinobu Inoue 			error = EADDRNOTAVAIL;
832cfa1ca9dSYoshinobu Inoue 			goto bad;
833cfa1ca9dSYoshinobu Inoue 		}
83433841545SHajimu UMEMOTO 		if (src->sa_len > size)
83533841545SHajimu UMEMOTO 			return EINVAL;
83633841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
837a1f7e5f8SHajimu UMEMOTO #ifdef INET6
838a1f7e5f8SHajimu UMEMOTO 		if (dst->sa_family == AF_INET6) {
839a1f7e5f8SHajimu UMEMOTO 			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
840a1f7e5f8SHajimu UMEMOTO 			if (error != 0)
841a1f7e5f8SHajimu UMEMOTO 				return (error);
842a1f7e5f8SHajimu UMEMOTO 		}
843a1f7e5f8SHajimu UMEMOTO #endif
84433841545SHajimu UMEMOTO 		break;
84533841545SHajimu UMEMOTO 
84633841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
84733841545SHajimu UMEMOTO 		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
84833841545SHajimu UMEMOTO 			error = EADDRNOTAVAIL;
84933841545SHajimu UMEMOTO 			goto bad;
85033841545SHajimu UMEMOTO 		}
85133841545SHajimu UMEMOTO 
85233841545SHajimu UMEMOTO 		/* copy src */
85333841545SHajimu UMEMOTO 		src = sc->gif_psrc;
85433841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
85533841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->addr);
85633841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->addr);
85733841545SHajimu UMEMOTO 		if (src->sa_len > size)
85833841545SHajimu UMEMOTO 			return EINVAL;
85933841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
86033841545SHajimu UMEMOTO 
86133841545SHajimu UMEMOTO 		/* copy dst */
86233841545SHajimu UMEMOTO 		src = sc->gif_pdst;
86333841545SHajimu UMEMOTO 		dst = (struct sockaddr *)
86433841545SHajimu UMEMOTO 			&(((struct if_laddrreq *)data)->dstaddr);
86533841545SHajimu UMEMOTO 		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
86633841545SHajimu UMEMOTO 		if (src->sa_len > size)
86733841545SHajimu UMEMOTO 			return EINVAL;
86833841545SHajimu UMEMOTO 		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
869cfa1ca9dSYoshinobu Inoue 		break;
870cfa1ca9dSYoshinobu Inoue 
871cfa1ca9dSYoshinobu Inoue 	case SIOCSIFFLAGS:
872686cdd19SJun-ichiro itojun Hagino 		/* if_ioctl() takes care of it */
873cfa1ca9dSYoshinobu Inoue 		break;
874cfa1ca9dSYoshinobu Inoue 
875cfa1ca9dSYoshinobu Inoue 	default:
876cfa1ca9dSYoshinobu Inoue 		error = EINVAL;
877cfa1ca9dSYoshinobu Inoue 		break;
878cfa1ca9dSYoshinobu Inoue 	}
879cfa1ca9dSYoshinobu Inoue  bad:
880cfa1ca9dSYoshinobu Inoue 	return error;
881cfa1ca9dSYoshinobu Inoue }
88253dab5feSBrooks Davis 
88317d5cb2dSRobert Watson /*
88417d5cb2dSRobert Watson  * XXXRW: There's a general event-ordering issue here: the code to check
88517d5cb2dSRobert Watson  * if a given tunnel is already present happens before we perform a
88617d5cb2dSRobert Watson  * potentially blocking setup of the tunnel.  This code needs to be
88717d5cb2dSRobert Watson  * re-ordered so that the check and replacement can be atomic using
88817d5cb2dSRobert Watson  * a mutex.
88917d5cb2dSRobert Watson  */
8909426aedfSHajimu UMEMOTO int
8919426aedfSHajimu UMEMOTO gif_set_tunnel(ifp, src, dst)
8929426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
8939426aedfSHajimu UMEMOTO 	struct sockaddr *src;
8949426aedfSHajimu UMEMOTO 	struct sockaddr *dst;
89553dab5feSBrooks Davis {
8968b615593SMarko Zec 	INIT_VNET_GIF(ifp->if_vnet);
897fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
8989426aedfSHajimu UMEMOTO 	struct gif_softc *sc2;
8999426aedfSHajimu UMEMOTO 	struct sockaddr *osrc, *odst, *sa;
9009426aedfSHajimu UMEMOTO 	int error = 0;
9019426aedfSHajimu UMEMOTO 
90217d5cb2dSRobert Watson 	mtx_lock(&gif_mtx);
903603724d3SBjoern A. Zeeb 	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
9049426aedfSHajimu UMEMOTO 		if (sc2 == sc)
9059426aedfSHajimu UMEMOTO 			continue;
9069426aedfSHajimu UMEMOTO 		if (!sc2->gif_pdst || !sc2->gif_psrc)
9079426aedfSHajimu UMEMOTO 			continue;
9089426aedfSHajimu UMEMOTO 		if (sc2->gif_pdst->sa_family != dst->sa_family ||
9099426aedfSHajimu UMEMOTO 		    sc2->gif_pdst->sa_len != dst->sa_len ||
9109426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_family != src->sa_family ||
9119426aedfSHajimu UMEMOTO 		    sc2->gif_psrc->sa_len != src->sa_len)
9129426aedfSHajimu UMEMOTO 			continue;
9139426aedfSHajimu UMEMOTO 
9149426aedfSHajimu UMEMOTO 		/*
9159426aedfSHajimu UMEMOTO 		 * Disallow parallel tunnels unless instructed
9169426aedfSHajimu UMEMOTO 		 * otherwise.
9179426aedfSHajimu UMEMOTO 		 */
918603724d3SBjoern A. Zeeb 		if (!V_parallel_tunnels &&
9199426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
9209426aedfSHajimu UMEMOTO 		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
9219426aedfSHajimu UMEMOTO 			error = EADDRNOTAVAIL;
92217d5cb2dSRobert Watson 			mtx_unlock(&gif_mtx);
9239426aedfSHajimu UMEMOTO 			goto bad;
9249426aedfSHajimu UMEMOTO 		}
9259426aedfSHajimu UMEMOTO 
9269426aedfSHajimu UMEMOTO 		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
9279426aedfSHajimu UMEMOTO 	}
92817d5cb2dSRobert Watson 	mtx_unlock(&gif_mtx);
9299426aedfSHajimu UMEMOTO 
9309426aedfSHajimu UMEMOTO 	/* XXX we can detach from both, but be polite just in case */
9319426aedfSHajimu UMEMOTO 	if (sc->gif_psrc)
9329426aedfSHajimu UMEMOTO 		switch (sc->gif_psrc->sa_family) {
9339426aedfSHajimu UMEMOTO #ifdef INET
9349426aedfSHajimu UMEMOTO 		case AF_INET:
9359426aedfSHajimu UMEMOTO 			(void)in_gif_detach(sc);
9369426aedfSHajimu UMEMOTO 			break;
9379426aedfSHajimu UMEMOTO #endif
9389426aedfSHajimu UMEMOTO #ifdef INET6
9399426aedfSHajimu UMEMOTO 		case AF_INET6:
9409426aedfSHajimu UMEMOTO 			(void)in6_gif_detach(sc);
9419426aedfSHajimu UMEMOTO 			break;
9429426aedfSHajimu UMEMOTO #endif
9439426aedfSHajimu UMEMOTO 		}
9449426aedfSHajimu UMEMOTO 
9459426aedfSHajimu UMEMOTO 	osrc = sc->gif_psrc;
946a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
9479426aedfSHajimu UMEMOTO 	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
9489426aedfSHajimu UMEMOTO 	sc->gif_psrc = sa;
9499426aedfSHajimu UMEMOTO 
9509426aedfSHajimu UMEMOTO 	odst = sc->gif_pdst;
951a163d034SWarner Losh 	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
9529426aedfSHajimu UMEMOTO 	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
9539426aedfSHajimu UMEMOTO 	sc->gif_pdst = sa;
9549426aedfSHajimu UMEMOTO 
9559426aedfSHajimu UMEMOTO 	switch (sc->gif_psrc->sa_family) {
9569426aedfSHajimu UMEMOTO #ifdef INET
9579426aedfSHajimu UMEMOTO 	case AF_INET:
9589426aedfSHajimu UMEMOTO 		error = in_gif_attach(sc);
9599426aedfSHajimu UMEMOTO 		break;
9609426aedfSHajimu UMEMOTO #endif
9619426aedfSHajimu UMEMOTO #ifdef INET6
9629426aedfSHajimu UMEMOTO 	case AF_INET6:
963a1f7e5f8SHajimu UMEMOTO 		/*
964a1f7e5f8SHajimu UMEMOTO 		 * Check validity of the scope zone ID of the addresses, and
965a1f7e5f8SHajimu UMEMOTO 		 * convert it into the kernel internal form if necessary.
966a1f7e5f8SHajimu UMEMOTO 		 */
967a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
968a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
969a1f7e5f8SHajimu UMEMOTO 			break;
970a1f7e5f8SHajimu UMEMOTO 		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
971a1f7e5f8SHajimu UMEMOTO 		if (error != 0)
972a1f7e5f8SHajimu UMEMOTO 			break;
9739426aedfSHajimu UMEMOTO 		error = in6_gif_attach(sc);
9749426aedfSHajimu UMEMOTO 		break;
9759426aedfSHajimu UMEMOTO #endif
9769426aedfSHajimu UMEMOTO 	}
9779426aedfSHajimu UMEMOTO 	if (error) {
9789426aedfSHajimu UMEMOTO 		/* rollback */
9799426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_psrc, M_IFADDR);
9809426aedfSHajimu UMEMOTO 		free((caddr_t)sc->gif_pdst, M_IFADDR);
9819426aedfSHajimu UMEMOTO 		sc->gif_psrc = osrc;
9829426aedfSHajimu UMEMOTO 		sc->gif_pdst = odst;
9839426aedfSHajimu UMEMOTO 		goto bad;
9849426aedfSHajimu UMEMOTO 	}
9859426aedfSHajimu UMEMOTO 
9869426aedfSHajimu UMEMOTO 	if (osrc)
9879426aedfSHajimu UMEMOTO 		free((caddr_t)osrc, M_IFADDR);
9889426aedfSHajimu UMEMOTO 	if (odst)
9899426aedfSHajimu UMEMOTO 		free((caddr_t)odst, M_IFADDR);
9909426aedfSHajimu UMEMOTO 
9919426aedfSHajimu UMEMOTO  bad:
9929426aedfSHajimu UMEMOTO 	if (sc->gif_psrc && sc->gif_pdst)
99313f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
9949426aedfSHajimu UMEMOTO 	else
99513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
9969426aedfSHajimu UMEMOTO 
9979426aedfSHajimu UMEMOTO 	return error;
9989426aedfSHajimu UMEMOTO }
9999426aedfSHajimu UMEMOTO 
10009426aedfSHajimu UMEMOTO void
10019426aedfSHajimu UMEMOTO gif_delete_tunnel(ifp)
10029426aedfSHajimu UMEMOTO 	struct ifnet *ifp;
10039426aedfSHajimu UMEMOTO {
1004fc74a9f9SBrooks Davis 	struct gif_softc *sc = ifp->if_softc;
100553dab5feSBrooks Davis 
100653dab5feSBrooks Davis 	if (sc->gif_psrc) {
100753dab5feSBrooks Davis 		free((caddr_t)sc->gif_psrc, M_IFADDR);
100853dab5feSBrooks Davis 		sc->gif_psrc = NULL;
100953dab5feSBrooks Davis 	}
101053dab5feSBrooks Davis 	if (sc->gif_pdst) {
101153dab5feSBrooks Davis 		free((caddr_t)sc->gif_pdst, M_IFADDR);
101253dab5feSBrooks Davis 		sc->gif_pdst = NULL;
101353dab5feSBrooks Davis 	}
10149426aedfSHajimu UMEMOTO 	/* it is safe to detach from both */
10159426aedfSHajimu UMEMOTO #ifdef INET
10169426aedfSHajimu UMEMOTO 	(void)in_gif_detach(sc);
10179426aedfSHajimu UMEMOTO #endif
10189426aedfSHajimu UMEMOTO #ifdef INET6
10199426aedfSHajimu UMEMOTO 	(void)in6_gif_detach(sc);
10209426aedfSHajimu UMEMOTO #endif
102113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
102253dab5feSBrooks Davis }
1023