if_gif.c revision 236951
162587Sitojun/*	$FreeBSD: head/sys/net/if_gif.c 236951 2012-06-12 10:44:09Z rrs $	*/
295023Ssuz/*	$KAME: if_gif.c,v 1.87 2001/10/19 08:50:27 itojun Exp $	*/
362587Sitojun
4139823Simp/*-
554263Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
654263Sshin * All rights reserved.
754263Sshin *
854263Sshin * Redistribution and use in source and binary forms, with or without
954263Sshin * modification, are permitted provided that the following conditions
1054263Sshin * are met:
1154263Sshin * 1. Redistributions of source code must retain the above copyright
1254263Sshin *    notice, this list of conditions and the following disclaimer.
1354263Sshin * 2. Redistributions in binary form must reproduce the above copyright
1454263Sshin *    notice, this list of conditions and the following disclaimer in the
1554263Sshin *    documentation and/or other materials provided with the distribution.
1654263Sshin * 3. Neither the name of the project nor the names of its contributors
1754263Sshin *    may be used to endorse or promote products derived from this software
1854263Sshin *    without specific prior written permission.
1954263Sshin *
2054263Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2154263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2254263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2354263Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2454263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2554263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2654263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2754263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2854263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2954263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3054263Sshin * SUCH DAMAGE.
3154263Sshin */
3254263Sshin
3354263Sshin#include "opt_inet.h"
3454263Sshin#include "opt_inet6.h"
3554263Sshin
3654263Sshin#include <sys/param.h>
3754263Sshin#include <sys/systm.h>
38219206Sbz#include <sys/jail.h>
3954263Sshin#include <sys/kernel.h>
4054263Sshin#include <sys/malloc.h>
4154263Sshin#include <sys/mbuf.h>
42129880Sphk#include <sys/module.h>
4354263Sshin#include <sys/socket.h>
4454263Sshin#include <sys/sockio.h>
4554263Sshin#include <sys/errno.h>
4654263Sshin#include <sys/time.h>
4791270Sbrooks#include <sys/sysctl.h>
4854263Sshin#include <sys/syslog.h>
49193664Shrs#include <sys/priv.h>
50178888Sjulian#include <sys/proc.h>
5162587Sitojun#include <sys/protosw.h>
5279106Sbrooks#include <sys/conf.h>
5354263Sshin#include <machine/cpu.h>
5454263Sshin
5554263Sshin#include <net/if.h>
56130933Sbrooks#include <net/if_clone.h>
5754263Sshin#include <net/if_types.h>
5854263Sshin#include <net/netisr.h>
5954263Sshin#include <net/route.h>
6054263Sshin#include <net/bpf.h>
61196019Srwatson#include <net/vnet.h>
6254263Sshin
6354263Sshin#include <netinet/in.h>
6454263Sshin#include <netinet/in_systm.h>
6578064Sume#include <netinet/ip.h>
6678064Sume#ifdef	INET
6754263Sshin#include <netinet/in_var.h>
6854263Sshin#include <netinet/in_gif.h>
6979106Sbrooks#include <netinet/ip_var.h>
7054263Sshin#endif	/* INET */
7154263Sshin
7254263Sshin#ifdef INET6
7354263Sshin#ifndef INET
7454263Sshin#include <netinet/in.h>
7554263Sshin#endif
7654263Sshin#include <netinet6/in6_var.h>
7754263Sshin#include <netinet/ip6.h>
7854263Sshin#include <netinet6/ip6_var.h>
79148385Sume#include <netinet6/scope6_var.h>
8054263Sshin#include <netinet6/in6_gif.h>
8162587Sitojun#include <netinet6/ip6protosw.h>
8254263Sshin#endif /* INET6 */
8354263Sshin
8462587Sitojun#include <netinet/ip_encap.h>
85153621Sthompsa#include <net/ethernet.h>
86153621Sthompsa#include <net/if_bridgevar.h>
8754263Sshin#include <net/if_gif.h>
8854263Sshin
89163606Srwatson#include <security/mac/mac_framework.h>
90163606Srwatson
9179106Sbrooks#define GIFNAME		"gif"
9262587Sitojun
93127305Srwatson/*
94127898Sru * gif_mtx protects the global gif_softc_list.
95127305Srwatson */
96127305Srwatsonstatic struct mtx gif_mtx;
9779106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
98215701Sdimstatic VNET_DEFINE(LIST_HEAD(, gif_softc), gif_softc_list);
99195727Srwatson#define	V_gif_softc_list	VNET(gif_softc_list)
100195699Srwatson
10183998Sbrooksvoid	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
10283998Sbrooksvoid	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
10383998Sbrooksvoid	(*ng_gif_attach_p)(struct ifnet *ifp);
10483998Sbrooksvoid	(*ng_gif_detach_p)(struct ifnet *ifp);
10583998Sbrooks
106153621Sthompsastatic void	gif_start(struct ifnet *);
107160195Ssamstatic int	gif_clone_create(struct if_clone *, int, caddr_t);
108128209Sbrooksstatic void	gif_clone_destroy(struct ifnet *);
10979106Sbrooks
110130933SbrooksIFC_SIMPLE_DECLARE(gif, 0);
11179106Sbrooks
11292725Salfredstatic int gifmodevent(module_t, int, void *);
11379106Sbrooks
11491270SbrooksSYSCTL_DECL(_net_link);
115227309Sedstatic SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
11691270Sbrooks    "Generic Tunnel Interface");
11762587Sitojun#ifndef MAX_GIF_NEST
11862587Sitojun/*
11991270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels.
12062587Sitojun * Since, setting a large value to this macro with a careless configuration
12162587Sitojun * may introduce system crash, we don't allow any nestings by default.
12262587Sitojun * If you need to configure nested gif tunnels, you can define this macro
12395023Ssuz * in your kernel configuration file.  However, if you do so, please be
12462587Sitojun * careful to configure the tunnels so that it won't make a loop.
12562587Sitojun */
12662587Sitojun#define MAX_GIF_NEST 1
12762587Sitojun#endif
128215701Sdimstatic VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
129195837Srwatson#define	V_max_gif_nesting	VNET(max_gif_nesting)
130195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
131195699Srwatson    &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
13262587Sitojun
13391270Sbrooks/*
13491270Sbrooks * By default, we disallow creation of multiple tunnels between the same
13591270Sbrooks * pair of addresses.  Some applications require this functionality so
13691270Sbrooks * we allow control over this check here.
13791270Sbrooks */
138195837Srwatson#ifdef XBONEHACK
139215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 1;
140195837Srwatson#else
141215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 0;
142195837Srwatson#endif
143195837Srwatson#define	V_parallel_tunnels	VNET(parallel_tunnels)
144195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
145195699Srwatson    &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
14691270Sbrooks
147176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */
148176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
149176879Sthompsa			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
150176879Sthompsa#ifndef ETHER_IS_BROADCAST
151176879Sthompsa#define ETHER_IS_BROADCAST(addr) \
152176879Sthompsa	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
153176879Sthompsa#endif
154176879Sthompsa
155128209Sbrooksstatic int
156160195Ssamgif_clone_create(ifc, unit, params)
15779106Sbrooks	struct if_clone *ifc;
15892081Smux	int unit;
159160195Ssam	caddr_t params;
16054263Sshin{
16178064Sume	struct gif_softc *sc;
16254263Sshin
163131672Sbms	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
164178888Sjulian	sc->gif_fibnum = curthread->td_proc->p_fibnum;
165147256Sbrooks	GIF2IFP(sc) = if_alloc(IFT_GIF);
166147256Sbrooks	if (GIF2IFP(sc) == NULL) {
167147256Sbrooks		free(sc, M_GIF);
168147256Sbrooks		return (ENOSPC);
169147256Sbrooks	}
17079106Sbrooks
171155037Sglebius	GIF_LOCK_INIT(sc);
172155037Sglebius
173147256Sbrooks	GIF2IFP(sc)->if_softc = sc;
174147256Sbrooks	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
17579106Sbrooks
17679106Sbrooks	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
177193664Shrs	sc->gif_options = GIF_ACCEPT_REVETHIP;
17862587Sitojun
179147256Sbrooks	GIF2IFP(sc)->if_addrlen = 0;
180147256Sbrooks	GIF2IFP(sc)->if_mtu    = GIF_MTU;
181147256Sbrooks	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
18278064Sume#if 0
18379106Sbrooks	/* turn off ingress filter */
184147256Sbrooks	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
18578064Sume#endif
186147256Sbrooks	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
187153621Sthompsa	GIF2IFP(sc)->if_start  = gif_start;
188147256Sbrooks	GIF2IFP(sc)->if_output = gif_output;
189207554Ssobomax	GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
190147256Sbrooks	if_attach(GIF2IFP(sc));
191147611Sdwmalone	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
19283998Sbrooks	if (ng_gif_attach_p != NULL)
193147256Sbrooks		(*ng_gif_attach_p)(GIF2IFP(sc));
194155037Sglebius
195155037Sglebius	mtx_lock(&gif_mtx);
196181803Sbz	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
197155037Sglebius	mtx_unlock(&gif_mtx);
198155037Sglebius
199155037Sglebius	return (0);
20079106Sbrooks}
20179106Sbrooks
202127305Srwatsonstatic void
203151266Sthompsagif_clone_destroy(ifp)
204151266Sthompsa	struct ifnet *ifp;
20579106Sbrooks{
206184678Sbz#if defined(INET) || defined(INET6)
20779106Sbrooks	int err;
208184678Sbz#endif
209151266Sthompsa	struct gif_softc *sc = ifp->if_softc;
21079106Sbrooks
211151266Sthompsa	mtx_lock(&gif_mtx);
212151266Sthompsa	LIST_REMOVE(sc, gif_list);
213151266Sthompsa	mtx_unlock(&gif_mtx);
214151266Sthompsa
215127305Srwatson	gif_delete_tunnel(ifp);
216105293Sume#ifdef INET6
217105293Sume	if (sc->encap_cookie6 != NULL) {
218105293Sume		err = encap_detach(sc->encap_cookie6);
219105293Sume		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
220105293Sume	}
221105293Sume#endif
222105293Sume#ifdef INET
22379106Sbrooks	if (sc->encap_cookie4 != NULL) {
22479106Sbrooks		err = encap_detach(sc->encap_cookie4);
22579106Sbrooks		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
22679106Sbrooks	}
227105293Sume#endif
22879106Sbrooks
22983998Sbrooks	if (ng_gif_detach_p != NULL)
23083998Sbrooks		(*ng_gif_detach_p)(ifp);
23179106Sbrooks	bpfdetach(ifp);
23279106Sbrooks	if_detach(ifp);
233147256Sbrooks	if_free(ifp);
23479106Sbrooks
235155037Sglebius	GIF_LOCK_DESTROY(sc);
236155037Sglebius
23779106Sbrooks	free(sc, M_GIF);
23879106Sbrooks}
23979106Sbrooks
240195837Srwatsonstatic void
241195837Srwatsonvnet_gif_init(const void *unused __unused)
242190787Szec{
243190787Szec
244190787Szec	LIST_INIT(&V_gif_softc_list);
245190787Szec}
246195837SrwatsonVNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init,
247195837Srwatson    NULL);
248190787Szec
249190787Szecstatic int
25079106Sbrooksgifmodevent(mod, type, data)
25179106Sbrooks	module_t mod;
25279106Sbrooks	int type;
25379106Sbrooks	void *data;
25479106Sbrooks{
25579106Sbrooks
25679106Sbrooks	switch (type) {
25779106Sbrooks	case MOD_LOAD:
258127305Srwatson		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
259185088Szec		if_clone_attach(&gif_cloner);
260195837Srwatson		break;
26179106Sbrooks
26279106Sbrooks	case MOD_UNLOAD:
26379106Sbrooks		if_clone_detach(&gif_cloner);
264127305Srwatson		mtx_destroy(&gif_mtx);
26579106Sbrooks		break;
266132199Sphk	default:
267132199Sphk		return EOPNOTSUPP;
26854263Sshin	}
26979106Sbrooks	return 0;
27054263Sshin}
27154263Sshin
27279106Sbrooksstatic moduledata_t gif_mod = {
27379106Sbrooks	"if_gif",
27479106Sbrooks	gifmodevent,
27579106Sbrooks	0
27679106Sbrooks};
27754263Sshin
27879106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
27983997SbrooksMODULE_VERSION(if_gif, 1);
28079106Sbrooks
281105293Sumeint
28262587Sitojungif_encapcheck(m, off, proto, arg)
28362587Sitojun	const struct mbuf *m;
28462587Sitojun	int off;
28562587Sitojun	int proto;
28662587Sitojun	void *arg;
28762587Sitojun{
28862587Sitojun	struct ip ip;
28962587Sitojun	struct gif_softc *sc;
29062587Sitojun
29162587Sitojun	sc = (struct gif_softc *)arg;
29262587Sitojun	if (sc == NULL)
29362587Sitojun		return 0;
29462587Sitojun
295147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
29662587Sitojun		return 0;
29762587Sitojun
29862587Sitojun	/* no physical address */
29962587Sitojun	if (!sc->gif_psrc || !sc->gif_pdst)
30062587Sitojun		return 0;
30162587Sitojun
30262587Sitojun	switch (proto) {
30362587Sitojun#ifdef INET
30462587Sitojun	case IPPROTO_IPV4:
30562587Sitojun		break;
30662587Sitojun#endif
30762587Sitojun#ifdef INET6
30862587Sitojun	case IPPROTO_IPV6:
30962587Sitojun		break;
31062587Sitojun#endif
311153621Sthompsa	case IPPROTO_ETHERIP:
312153621Sthompsa		break;
313153621Sthompsa
31462587Sitojun	default:
31562587Sitojun		return 0;
31662587Sitojun	}
31762587Sitojun
318105339Sume	/* Bail on short packets */
319105339Sume	if (m->m_pkthdr.len < sizeof(ip))
320105339Sume		return 0;
321105339Sume
32291327Sbrooks	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
32362587Sitojun
32462587Sitojun	switch (ip.ip_v) {
32562587Sitojun#ifdef INET
32662587Sitojun	case 4:
32762587Sitojun		if (sc->gif_psrc->sa_family != AF_INET ||
32862587Sitojun		    sc->gif_pdst->sa_family != AF_INET)
32962587Sitojun			return 0;
33062587Sitojun		return gif_encapcheck4(m, off, proto, arg);
33162587Sitojun#endif
33262587Sitojun#ifdef INET6
33362587Sitojun	case 6:
334105293Sume		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
335105293Sume			return 0;
33662587Sitojun		if (sc->gif_psrc->sa_family != AF_INET6 ||
33762587Sitojun		    sc->gif_pdst->sa_family != AF_INET6)
33862587Sitojun			return 0;
33962587Sitojun		return gif_encapcheck6(m, off, proto, arg);
34062587Sitojun#endif
34162587Sitojun	default:
34262587Sitojun		return 0;
34362587Sitojun	}
34462587Sitojun}
345236951Srrs#ifdef INET
346236951Srrs#define GIF_HDR_LEN (ETHER_HDR_LEN + sizeof (struct ip))
347236951Srrs#endif
348236951Srrs#ifdef INET6
349236951Srrs#define GIF_HDR_LEN6 (ETHER_HDR_LEN + sizeof (struct ip6_hdr))
350236951Srrs#endif
35162587Sitojun
352153621Sthompsastatic void
353153621Sthompsagif_start(struct ifnet *ifp)
354153621Sthompsa{
355153621Sthompsa	struct gif_softc *sc;
356153621Sthompsa	struct mbuf *m;
357236951Srrs	uint32_t af;
358236951Srrs	int error = 0;
359153621Sthompsa
360153621Sthompsa	sc = ifp->if_softc;
361236951Srrs	GIF_LOCK(sc);
362236951Srrs	if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
363236951Srrs		/* Already active */
364236951Srrs		ifp->if_drv_flags |= IFF_GIF_WANTED;
365236951Srrs		GIF_UNLOCK(sc);
366236951Srrs		return;
367236951Srrs	}
368236951Srrs	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
369236951Srrs	GIF_UNLOCK(sc);
370236951Srrskeep_going:
371236951Srrs	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
372153621Sthompsa
373236951Srrs		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
374153621Sthompsa		if (m == 0)
375153621Sthompsa			break;
376153621Sthompsa
377236951Srrs#ifdef ALTQ
378236951Srrs		/* Take out those altq bytes we add in gif_output  */
379236951Srrs#ifdef INET
380236951Srrs		if (sc->gif_psrc->sa_family == AF_INET)
381236951Srrs			m->m_pkthdr.len -= GIF_HDR_LEN;
382236951Srrs#endif
383236951Srrs#ifdef INET6
384236951Srrs		if (sc->gif_psrc->sa_family == AF_INET6)
385236951Srrs		    m->m_pkthdr.len -= GIF_HDR_LEN6;
386236951Srrs#endif
387236951Srrs#endif
388236951Srrs		/* Now pull back the af in packet that
389236951Srrs		 * was saved in the address location.
390236951Srrs		 */
391236951Srrs		bcopy(m->m_pkthdr.src_mac_addr, &af, sizeof(af));
392236951Srrs		if (ifp->if_bridge)
393236951Srrs			af = AF_LINK;
394153621Sthompsa
395236951Srrs		BPF_MTAP2(ifp, &af, sizeof(af), m);
396236951Srrs		ifp->if_opackets++;
397236951Srrs
398236951Srrs/*              Done by IFQ_HANDOFF */
399236951Srrs/* 		ifp->if_obytes += m->m_pkthdr.len;*/
400236951Srrs		/* override to IPPROTO_ETHERIP for bridged traffic */
401236951Srrs
402236951Srrs		M_SETFIB(m, sc->gif_fibnum);
403236951Srrs		/* inner AF-specific encapsulation */
404236951Srrs		/* XXX should we check if our outer source is legal? */
405236951Srrs		/* dispatch to output logic based on outer AF */
406236951Srrs		switch (sc->gif_psrc->sa_family) {
407236951Srrs#ifdef INET
408236951Srrs		case AF_INET:
409236951Srrs			error = in_gif_output(ifp, af, m);
410236951Srrs			break;
411236951Srrs#endif
412236951Srrs#ifdef INET6
413236951Srrs		case AF_INET6:
414236951Srrs			error = in6_gif_output(ifp, af, m);
415236951Srrs			break;
416236951Srrs#endif
417236951Srrs		default:
418236951Srrs			m_freem(m);
419236951Srrs			error = ENETDOWN;
420236951Srrs		}
421236951Srrs		if (error)
422236951Srrs			ifp->if_oerrors++;
423236951Srrs
424153621Sthompsa	}
425236951Srrs	GIF_LOCK(sc);
426236951Srrs	if (ifp->if_drv_flags & IFF_GIF_WANTED) {
427236951Srrs		/* Someone did a start while
428236951Srrs		 * we were unlocked and processing
429236951Srrs		 * lets clear the flag and try again.
430236951Srrs		 */
431236951Srrs		ifp->if_drv_flags &= ~IFF_GIF_WANTED;
432236951Srrs		GIF_UNLOCK(sc);
433236951Srrs		goto keep_going;
434236951Srrs	}
435153621Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
436236951Srrs	GIF_UNLOCK(sc);
437153621Sthompsa	return;
438153621Sthompsa}
439153621Sthompsa
44054263Sshinint
441191148Skmacygif_output(ifp, m, dst, ro)
44254263Sshin	struct ifnet *ifp;
44354263Sshin	struct mbuf *m;
44454263Sshin	struct sockaddr *dst;
445191148Skmacy	struct route *ro;
44654263Sshin{
447147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
448127898Sru	struct m_tag *mtag;
44954263Sshin	int error = 0;
450127898Sru	int gif_called;
451236951Srrs	uint32_t af;
452101182Srwatson#ifdef MAC
453172930Srwatson	error = mac_ifnet_check_transmit(ifp, m);
454101739Srwatson	if (error) {
455101739Srwatson		m_freem(m);
456101739Srwatson		goto end;
457101739Srwatson	}
458101182Srwatson#endif
459101182Srwatson
46054263Sshin	/*
46154263Sshin	 * gif may cause infinite recursion calls when misconfigured.
462127898Sru	 * We'll prevent this by detecting loops.
463127898Sru	 *
464127898Sru	 * High nesting level may cause stack exhaustion.
46554263Sshin	 * We'll prevent this by introducing upper limit.
46654263Sshin	 */
467127898Sru	gif_called = 1;
468127898Sru	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
469127898Sru	while (mtag != NULL) {
470127898Sru		if (*(struct ifnet **)(mtag + 1) == ifp) {
471127898Sru			log(LOG_NOTICE,
472127898Sru			    "gif_output: loop detected on %s\n",
473127898Sru			    (*(struct ifnet **)(mtag + 1))->if_xname);
474127898Sru			m_freem(m);
475127898Sru			error = EIO;	/* is there better errno? */
476127898Sru			goto end;
477127898Sru		}
478127898Sru		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
479127898Sru		gif_called++;
480127898Sru	}
481181803Sbz	if (gif_called > V_max_gif_nesting) {
48254263Sshin		log(LOG_NOTICE,
48354263Sshin		    "gif_output: recursively called too many times(%d)\n",
484127303Srwatson		    gif_called);
48554263Sshin		m_freem(m);
48654263Sshin		error = EIO;	/* is there better errno? */
48754263Sshin		goto end;
48854263Sshin	}
489127898Sru	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
490127898Sru	    M_NOWAIT);
491127898Sru	if (mtag == NULL) {
492127898Sru		m_freem(m);
493127898Sru		error = ENOMEM;
494127898Sru		goto end;
495127898Sru	}
496127898Sru	*(struct ifnet **)(mtag + 1) = ifp;
497127898Sru	m_tag_prepend(m, mtag);
49862587Sitojun
49954263Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
500236951Srrs	/* BPF writes need to be handled specially. */
501236951Srrs	if (dst->sa_family == AF_UNSPEC) {
502236951Srrs		bcopy(dst->sa_data, &af, sizeof(af));
503236951Srrs		dst->sa_family = af;
504236951Srrs	}
505236951Srrs	af = dst->sa_family;
506236951Srrs	/* Now save the af in the inbound pkt mac
507236951Srrs	 * address location.
508236951Srrs	 */
509236951Srrs	bcopy(&af, m->m_pkthdr.src_mac_addr, sizeof(af));
51054263Sshin	if (!(ifp->if_flags & IFF_UP) ||
51154263Sshin	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
51254263Sshin		m_freem(m);
51354263Sshin		error = ENETDOWN;
51454263Sshin		goto end;
51554263Sshin	}
516236951Srrs#ifdef ALTQ
517236951Srrs	/* Make altq aware of the bytes we will add
518236951Srrs	 * when we actually send it.
519236951Srrs	 */
52054263Sshin#ifdef INET
521236951Srrs	if (sc->gif_psrc->sa_family == AF_INET)
522236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN;
52354263Sshin#endif
52454263Sshin#ifdef INET6
525236951Srrs	if (sc->gif_psrc->sa_family == AF_INET6)
526236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN6;
52754263Sshin#endif
528236951Srrs#endif
529236951Srrs	/*
530236951Srrs	 * Queue message on interface, update output statistics if
531236951Srrs	 * successful, and start output if interface not yet active.
532236951Srrs	 */
533236951Srrs	IFQ_HANDOFF(ifp, m, error);
53454263Sshin  end:
53578064Sume	if (error)
53678064Sume		ifp->if_oerrors++;
537155037Sglebius	return (error);
53854263Sshin}
53954263Sshin
54054263Sshinvoid
541105338Sumegif_input(m, af, ifp)
54254263Sshin	struct mbuf *m;
54354263Sshin	int af;
544105338Sume	struct ifnet *ifp;
54554263Sshin{
546153621Sthompsa	int isr, n;
547198357Sbrueffer	struct gif_softc *sc;
548153621Sthompsa	struct etherip_header *eip;
549176879Sthompsa	struct ether_header *eh;
550176879Sthompsa	struct ifnet *oldifp;
55154263Sshin
552105338Sume	if (ifp == NULL) {
55354263Sshin		/* just in case */
55454263Sshin		m_freem(m);
55554263Sshin		return;
55654263Sshin	}
557198357Sbrueffer	sc = ifp->if_softc;
558105338Sume	m->m_pkthdr.rcvif = ifp;
559101182Srwatson
560101182Srwatson#ifdef MAC
561172930Srwatson	mac_ifnet_create_mbuf(ifp, m);
562101182Srwatson#endif
563101182Srwatson
564159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
56578064Sume		u_int32_t af1 = af;
566123922Ssam		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
56754263Sshin	}
56854263Sshin
56983998Sbrooks	if (ng_gif_input_p != NULL) {
570105338Sume		(*ng_gif_input_p)(ifp, &m, af);
57183998Sbrooks		if (m == NULL)
57283998Sbrooks			return;
57383998Sbrooks	}
57483998Sbrooks
57554263Sshin	/*
57654263Sshin	 * Put the packet to the network layer input queue according to the
57754263Sshin	 * specified address family.
57854263Sshin	 * Note: older versions of gif_input directly called network layer
57995023Ssuz	 * input functions, e.g. ip6_input, here.  We changed the policy to
58054263Sshin	 * prevent too many recursive calls of such input functions, which
58195023Ssuz	 * might cause kernel panic.  But the change may introduce another
58254263Sshin	 * problem; if the input queue is full, packets are discarded.
58395023Ssuz	 * The kernel stack overflow really happened, and we believed
58495023Ssuz	 * queue-full rarely occurs, so we changed the policy.
58554263Sshin	 */
58654263Sshin	switch (af) {
58754263Sshin#ifdef INET
58854263Sshin	case AF_INET:
58954263Sshin		isr = NETISR_IP;
59054263Sshin		break;
59154263Sshin#endif
59254263Sshin#ifdef INET6
59354263Sshin	case AF_INET6:
59454263Sshin		isr = NETISR_IPV6;
59554263Sshin		break;
59654263Sshin#endif
597153621Sthompsa	case AF_LINK:
598153621Sthompsa		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
599153621Sthompsa		if (n > m->m_len) {
600153621Sthompsa			m = m_pullup(m, n);
601153621Sthompsa			if (m == NULL) {
602153621Sthompsa				ifp->if_ierrors++;
603153621Sthompsa				return;
604153621Sthompsa			}
605153621Sthompsa		}
606153621Sthompsa
607153621Sthompsa		eip = mtod(m, struct etherip_header *);
608193664Shrs		/*
609193664Shrs		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
610193664Shrs		 * accepts an EtherIP packet with revered version field in
611193664Shrs		 * the header.  This is a knob for backward compatibility
612193664Shrs		 * with FreeBSD 7.2R or prior.
613193664Shrs		 */
614193664Shrs		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
615193664Shrs			if (eip->eip_resvl != ETHERIP_VERSION
616193664Shrs			    && eip->eip_ver != ETHERIP_VERSION) {
617193664Shrs				/* discard unknown versions */
618193664Shrs				m_freem(m);
619193664Shrs				return;
620193664Shrs			}
621193664Shrs		} else {
622193664Shrs			if (eip->eip_ver != ETHERIP_VERSION) {
623193664Shrs				/* discard unknown versions */
624193664Shrs				m_freem(m);
625193664Shrs				return;
626193664Shrs			}
627153621Sthompsa		}
628153621Sthompsa		m_adj(m, sizeof(struct etherip_header));
629153621Sthompsa
630153621Sthompsa		m->m_flags &= ~(M_BCAST|M_MCAST);
631153621Sthompsa		m->m_pkthdr.rcvif = ifp;
632153621Sthompsa
633176879Sthompsa		if (ifp->if_bridge) {
634176879Sthompsa			oldifp = ifp;
635176879Sthompsa			eh = mtod(m, struct ether_header *);
636176879Sthompsa			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
637176879Sthompsa				if (ETHER_IS_BROADCAST(eh->ether_dhost))
638176879Sthompsa					m->m_flags |= M_BCAST;
639176879Sthompsa				else
640176879Sthompsa					m->m_flags |= M_MCAST;
641176879Sthompsa				ifp->if_imcasts++;
642176879Sthompsa			}
643153621Sthompsa			BRIDGE_INPUT(ifp, m);
644176879Sthompsa
645176879Sthompsa			if (m != NULL && ifp != oldifp) {
646176879Sthompsa				/*
647176879Sthompsa				 * The bridge gave us back itself or one of the
648176879Sthompsa				 * members for which the frame is addressed.
649176879Sthompsa				 */
650176879Sthompsa				ether_demux(ifp, m);
651176879Sthompsa				return;
652176879Sthompsa			}
653176879Sthompsa		}
654153621Sthompsa		if (m != NULL)
655153621Sthompsa			m_freem(m);
656153621Sthompsa		return;
657153621Sthompsa
65854263Sshin	default:
65983998Sbrooks		if (ng_gif_input_orphan_p != NULL)
660105338Sume			(*ng_gif_input_orphan_p)(ifp, m, af);
66183998Sbrooks		else
66283998Sbrooks			m_freem(m);
66354263Sshin		return;
66454263Sshin	}
66554263Sshin
666105338Sume	ifp->if_ipackets++;
667105338Sume	ifp->if_ibytes += m->m_pkthdr.len;
668223741Sbz	M_SETFIB(m, ifp->if_fib);
669111888Sjlemon	netisr_dispatch(isr, m);
67054263Sshin}
67154263Sshin
67262587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
67354263Sshinint
67454263Sshingif_ioctl(ifp, cmd, data)
67554263Sshin	struct ifnet *ifp;
67654263Sshin	u_long cmd;
67754263Sshin	caddr_t data;
67854263Sshin{
679147256Sbrooks	struct gif_softc *sc  = ifp->if_softc;
68054263Sshin	struct ifreq     *ifr = (struct ifreq*)data;
68154263Sshin	int error = 0, size;
682193664Shrs	u_int	options;
68362587Sitojun	struct sockaddr *dst, *src;
684105339Sume#ifdef	SIOCSIFMTU /* xxx */
685105339Sume	u_long mtu;
686105339Sume#endif
687105339Sume
68854263Sshin	switch (cmd) {
68954263Sshin	case SIOCSIFADDR:
690105293Sume		ifp->if_flags |= IFF_UP;
69154263Sshin		break;
69262587Sitojun
69354263Sshin	case SIOCSIFDSTADDR:
69454263Sshin		break;
69554263Sshin
69654263Sshin	case SIOCADDMULTI:
69754263Sshin	case SIOCDELMULTI:
69854263Sshin		break;
69954263Sshin
70062587Sitojun#ifdef	SIOCSIFMTU /* xxx */
70154263Sshin	case SIOCGIFMTU:
70254263Sshin		break;
70362587Sitojun
70454263Sshin	case SIOCSIFMTU:
705105339Sume		mtu = ifr->ifr_mtu;
706105339Sume		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
707105339Sume			return (EINVAL);
708105339Sume		ifp->if_mtu = mtu;
70954263Sshin		break;
71062587Sitojun#endif /* SIOCSIFMTU */
71154263Sshin
712105339Sume#ifdef INET
71354263Sshin	case SIOCSIFPHYADDR:
714105339Sume#endif
71554263Sshin#ifdef INET6
71654263Sshin	case SIOCSIFPHYADDR_IN6:
71754263Sshin#endif /* INET6 */
71878064Sume	case SIOCSLIFPHYADDR:
71962587Sitojun		switch (cmd) {
72078064Sume#ifdef INET
72162587Sitojun		case SIOCSIFPHYADDR:
72254263Sshin			src = (struct sockaddr *)
72354263Sshin				&(((struct in_aliasreq *)data)->ifra_addr);
72454263Sshin			dst = (struct sockaddr *)
72554263Sshin				&(((struct in_aliasreq *)data)->ifra_dstaddr);
72662587Sitojun			break;
72778064Sume#endif
72862587Sitojun#ifdef INET6
72962587Sitojun		case SIOCSIFPHYADDR_IN6:
73062587Sitojun			src = (struct sockaddr *)
73162587Sitojun				&(((struct in6_aliasreq *)data)->ifra_addr);
73262587Sitojun			dst = (struct sockaddr *)
73362587Sitojun				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
73462587Sitojun			break;
73562587Sitojun#endif
73678064Sume		case SIOCSLIFPHYADDR:
73778064Sume			src = (struct sockaddr *)
73878064Sume				&(((struct if_laddrreq *)data)->addr);
73978064Sume			dst = (struct sockaddr *)
74078064Sume				&(((struct if_laddrreq *)data)->dstaddr);
741105293Sume			break;
74291327Sbrooks		default:
743105293Sume			return EINVAL;
74462587Sitojun		}
74554263Sshin
74678064Sume		/* sa_family must be equal */
74778064Sume		if (src->sa_family != dst->sa_family)
74878064Sume			return EINVAL;
74978064Sume
75078064Sume		/* validate sa_len */
75178064Sume		switch (src->sa_family) {
75278064Sume#ifdef INET
75378064Sume		case AF_INET:
75478064Sume			if (src->sa_len != sizeof(struct sockaddr_in))
75578064Sume				return EINVAL;
75678064Sume			break;
75778064Sume#endif
75878064Sume#ifdef INET6
75978064Sume		case AF_INET6:
76078064Sume			if (src->sa_len != sizeof(struct sockaddr_in6))
76178064Sume				return EINVAL;
76278064Sume			break;
76378064Sume#endif
76478064Sume		default:
76578064Sume			return EAFNOSUPPORT;
76678064Sume		}
76778064Sume		switch (dst->sa_family) {
76878064Sume#ifdef INET
76978064Sume		case AF_INET:
77078064Sume			if (dst->sa_len != sizeof(struct sockaddr_in))
77178064Sume				return EINVAL;
77278064Sume			break;
77378064Sume#endif
77478064Sume#ifdef INET6
77578064Sume		case AF_INET6:
77678064Sume			if (dst->sa_len != sizeof(struct sockaddr_in6))
77778064Sume				return EINVAL;
77878064Sume			break;
77978064Sume#endif
78078064Sume		default:
78178064Sume			return EAFNOSUPPORT;
78278064Sume		}
78378064Sume
78478064Sume		/* check sa_family looks sane for the cmd */
78578064Sume		switch (cmd) {
78678064Sume		case SIOCSIFPHYADDR:
78778064Sume			if (src->sa_family == AF_INET)
78878064Sume				break;
78978064Sume			return EAFNOSUPPORT;
79078064Sume#ifdef INET6
79178064Sume		case SIOCSIFPHYADDR_IN6:
79278064Sume			if (src->sa_family == AF_INET6)
79378064Sume				break;
79478064Sume			return EAFNOSUPPORT;
79578064Sume#endif /* INET6 */
79678064Sume		case SIOCSLIFPHYADDR:
79778064Sume			/* checks done in the above */
79878064Sume			break;
79978064Sume		}
80078064Sume
801147256Sbrooks		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
80262587Sitojun		break;
80362587Sitojun
80462587Sitojun#ifdef SIOCDIFPHYADDR
80562587Sitojun	case SIOCDIFPHYADDR:
806147256Sbrooks		gif_delete_tunnel(GIF2IFP(sc));
80754263Sshin		break;
80862587Sitojun#endif
80962587Sitojun
81054263Sshin	case SIOCGIFPSRCADDR:
81154263Sshin#ifdef INET6
81254263Sshin	case SIOCGIFPSRCADDR_IN6:
81354263Sshin#endif /* INET6 */
81454263Sshin		if (sc->gif_psrc == NULL) {
81554263Sshin			error = EADDRNOTAVAIL;
81654263Sshin			goto bad;
81754263Sshin		}
81854263Sshin		src = sc->gif_psrc;
81978064Sume		switch (cmd) {
82054263Sshin#ifdef INET
82178064Sume		case SIOCGIFPSRCADDR:
82254263Sshin			dst = &ifr->ifr_addr;
82378064Sume			size = sizeof(ifr->ifr_addr);
82454263Sshin			break;
82554263Sshin#endif /* INET */
82654263Sshin#ifdef INET6
82778064Sume		case SIOCGIFPSRCADDR_IN6:
82854263Sshin			dst = (struct sockaddr *)
82954263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
83078064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
83154263Sshin			break;
83254263Sshin#endif /* INET6 */
83354263Sshin		default:
83454263Sshin			error = EADDRNOTAVAIL;
83554263Sshin			goto bad;
83654263Sshin		}
83778064Sume		if (src->sa_len > size)
83878064Sume			return EINVAL;
83978064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
840148385Sume#ifdef INET6
841148385Sume		if (dst->sa_family == AF_INET6) {
842148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
843148385Sume			if (error != 0)
844148385Sume				return (error);
845148385Sume		}
846148385Sume#endif
84754263Sshin		break;
84862587Sitojun
84954263Sshin	case SIOCGIFPDSTADDR:
85054263Sshin#ifdef INET6
85154263Sshin	case SIOCGIFPDSTADDR_IN6:
85254263Sshin#endif /* INET6 */
85354263Sshin		if (sc->gif_pdst == NULL) {
85454263Sshin			error = EADDRNOTAVAIL;
85554263Sshin			goto bad;
85654263Sshin		}
85754263Sshin		src = sc->gif_pdst;
85878064Sume		switch (cmd) {
85954263Sshin#ifdef INET
86078064Sume		case SIOCGIFPDSTADDR:
86154263Sshin			dst = &ifr->ifr_addr;
86278064Sume			size = sizeof(ifr->ifr_addr);
86354263Sshin			break;
86454263Sshin#endif /* INET */
86554263Sshin#ifdef INET6
86678064Sume		case SIOCGIFPDSTADDR_IN6:
86754263Sshin			dst = (struct sockaddr *)
86854263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
86978064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
87054263Sshin			break;
87154263Sshin#endif /* INET6 */
87254263Sshin		default:
87354263Sshin			error = EADDRNOTAVAIL;
87454263Sshin			goto bad;
87554263Sshin		}
87678064Sume		if (src->sa_len > size)
87778064Sume			return EINVAL;
878219206Sbz		error = prison_if(curthread->td_ucred, src);
879219206Sbz		if (error != 0)
880219206Sbz			return (error);
881219206Sbz		error = prison_if(curthread->td_ucred, dst);
882219206Sbz		if (error != 0)
883219206Sbz			return (error);
88478064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
885148385Sume#ifdef INET6
886148385Sume		if (dst->sa_family == AF_INET6) {
887148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
888148385Sume			if (error != 0)
889148385Sume				return (error);
890148385Sume		}
891148385Sume#endif
89254263Sshin		break;
89354263Sshin
89478064Sume	case SIOCGLIFPHYADDR:
89578064Sume		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
89678064Sume			error = EADDRNOTAVAIL;
89778064Sume			goto bad;
89878064Sume		}
89978064Sume
90078064Sume		/* copy src */
90178064Sume		src = sc->gif_psrc;
90278064Sume		dst = (struct sockaddr *)
90378064Sume			&(((struct if_laddrreq *)data)->addr);
90478064Sume		size = sizeof(((struct if_laddrreq *)data)->addr);
90578064Sume		if (src->sa_len > size)
90678064Sume			return EINVAL;
90778064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
90878064Sume
90978064Sume		/* copy dst */
91078064Sume		src = sc->gif_pdst;
91178064Sume		dst = (struct sockaddr *)
91278064Sume			&(((struct if_laddrreq *)data)->dstaddr);
91378064Sume		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
91478064Sume		if (src->sa_len > size)
91578064Sume			return EINVAL;
91678064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
91778064Sume		break;
91878064Sume
91954263Sshin	case SIOCSIFFLAGS:
92062587Sitojun		/* if_ioctl() takes care of it */
92154263Sshin		break;
92254263Sshin
923193664Shrs	case GIFGOPTS:
924193664Shrs		options = sc->gif_options;
925193664Shrs		error = copyout(&options, ifr->ifr_data,
926193664Shrs				sizeof(options));
927193664Shrs		break;
928193664Shrs
929193664Shrs	case GIFSOPTS:
930193664Shrs		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
931193664Shrs			break;
932193815Shrs		error = copyin(ifr->ifr_data, &options, sizeof(options));
933193815Shrs		if (error)
934193815Shrs			break;
935193815Shrs		if (options & ~GIF_OPTMASK)
936193815Shrs			error = EINVAL;
937193815Shrs		else
938193815Shrs			sc->gif_options = options;
939193664Shrs		break;
940193664Shrs
94154263Sshin	default:
94254263Sshin		error = EINVAL;
94354263Sshin		break;
94454263Sshin	}
94554263Sshin bad:
94654263Sshin	return error;
94754263Sshin}
94879106Sbrooks
949127305Srwatson/*
950127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check
951127305Srwatson * if a given tunnel is already present happens before we perform a
952127305Srwatson * potentially blocking setup of the tunnel.  This code needs to be
953127305Srwatson * re-ordered so that the check and replacement can be atomic using
954127305Srwatson * a mutex.
955127305Srwatson */
956105293Sumeint
957105293Sumegif_set_tunnel(ifp, src, dst)
958105293Sume	struct ifnet *ifp;
959105293Sume	struct sockaddr *src;
960105293Sume	struct sockaddr *dst;
961105293Sume{
962147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
963105293Sume	struct gif_softc *sc2;
964105293Sume	struct sockaddr *osrc, *odst, *sa;
965105293Sume	int error = 0;
966105293Sume
967127305Srwatson	mtx_lock(&gif_mtx);
968181803Sbz	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
969105293Sume		if (sc2 == sc)
970105293Sume			continue;
971105293Sume		if (!sc2->gif_pdst || !sc2->gif_psrc)
972105293Sume			continue;
973105293Sume		if (sc2->gif_pdst->sa_family != dst->sa_family ||
974105293Sume		    sc2->gif_pdst->sa_len != dst->sa_len ||
975105293Sume		    sc2->gif_psrc->sa_family != src->sa_family ||
976105293Sume		    sc2->gif_psrc->sa_len != src->sa_len)
977105293Sume			continue;
978105293Sume
979105293Sume		/*
980105293Sume		 * Disallow parallel tunnels unless instructed
981105293Sume		 * otherwise.
982105293Sume		 */
983181803Sbz		if (!V_parallel_tunnels &&
984105293Sume		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
985105293Sume		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
986105293Sume			error = EADDRNOTAVAIL;
987127305Srwatson			mtx_unlock(&gif_mtx);
988105293Sume			goto bad;
989105293Sume		}
990105293Sume
991105293Sume		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
992105293Sume	}
993127305Srwatson	mtx_unlock(&gif_mtx);
994105293Sume
995105293Sume	/* XXX we can detach from both, but be polite just in case */
996105293Sume	if (sc->gif_psrc)
997105293Sume		switch (sc->gif_psrc->sa_family) {
998105293Sume#ifdef INET
999105293Sume		case AF_INET:
1000105293Sume			(void)in_gif_detach(sc);
1001105293Sume			break;
1002105293Sume#endif
1003105293Sume#ifdef INET6
1004105293Sume		case AF_INET6:
1005105293Sume			(void)in6_gif_detach(sc);
1006105293Sume			break;
1007105293Sume#endif
1008105293Sume		}
1009105293Sume
1010105293Sume	osrc = sc->gif_psrc;
1011111119Simp	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1012105293Sume	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1013105293Sume	sc->gif_psrc = sa;
1014105293Sume
1015105293Sume	odst = sc->gif_pdst;
1016111119Simp	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1017105293Sume	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1018105293Sume	sc->gif_pdst = sa;
1019105293Sume
1020105293Sume	switch (sc->gif_psrc->sa_family) {
1021105293Sume#ifdef INET
1022105293Sume	case AF_INET:
1023105293Sume		error = in_gif_attach(sc);
1024105293Sume		break;
1025105293Sume#endif
1026105293Sume#ifdef INET6
1027105293Sume	case AF_INET6:
1028148385Sume		/*
1029148385Sume		 * Check validity of the scope zone ID of the addresses, and
1030148385Sume		 * convert it into the kernel internal form if necessary.
1031148385Sume		 */
1032148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1033148385Sume		if (error != 0)
1034148385Sume			break;
1035148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1036148385Sume		if (error != 0)
1037148385Sume			break;
1038105293Sume		error = in6_gif_attach(sc);
1039105293Sume		break;
1040105293Sume#endif
1041105293Sume	}
1042105293Sume	if (error) {
1043105293Sume		/* rollback */
1044105293Sume		free((caddr_t)sc->gif_psrc, M_IFADDR);
1045105293Sume		free((caddr_t)sc->gif_pdst, M_IFADDR);
1046105293Sume		sc->gif_psrc = osrc;
1047105293Sume		sc->gif_pdst = odst;
1048105293Sume		goto bad;
1049105293Sume	}
1050105293Sume
1051105293Sume	if (osrc)
1052105293Sume		free((caddr_t)osrc, M_IFADDR);
1053105293Sume	if (odst)
1054105293Sume		free((caddr_t)odst, M_IFADDR);
1055105293Sume
1056105293Sume bad:
1057105293Sume	if (sc->gif_psrc && sc->gif_pdst)
1058148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1059105293Sume	else
1060148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1061105293Sume
1062105293Sume	return error;
1063105293Sume}
1064105293Sume
106579106Sbrooksvoid
1066105293Sumegif_delete_tunnel(ifp)
1067105293Sume	struct ifnet *ifp;
106879106Sbrooks{
1069147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
107079106Sbrooks
107179106Sbrooks	if (sc->gif_psrc) {
107279106Sbrooks		free((caddr_t)sc->gif_psrc, M_IFADDR);
107379106Sbrooks		sc->gif_psrc = NULL;
107479106Sbrooks	}
107579106Sbrooks	if (sc->gif_pdst) {
107679106Sbrooks		free((caddr_t)sc->gif_pdst, M_IFADDR);
107779106Sbrooks		sc->gif_pdst = NULL;
107879106Sbrooks	}
1079105293Sume	/* it is safe to detach from both */
1080105293Sume#ifdef INET
1081105293Sume	(void)in_gif_detach(sc);
1082105293Sume#endif
1083105293Sume#ifdef INET6
1084105293Sume	(void)in6_gif_detach(sc);
1085105293Sume#endif
1086160018Syar	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
108779106Sbrooks}
1088