if_gif.c revision 236957
162587Sitojun/*	$FreeBSD: head/sys/net/if_gif.c 236957 2012-06-12 13:31:32Z 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
388236957Srrs		/*
389236957Srrs		 * Now pull back the af that we
390236957Srrs		 * stashed in the csum_data.
391236951Srrs		 */
392236955Srrs		af = m->m_pkthdr.csum_data;
393236955Srrs
394236951Srrs		if (ifp->if_bridge)
395236951Srrs			af = AF_LINK;
396153621Sthompsa
397236951Srrs		BPF_MTAP2(ifp, &af, sizeof(af), m);
398236951Srrs		ifp->if_opackets++;
399236951Srrs
400236951Srrs/*              Done by IFQ_HANDOFF */
401236951Srrs/* 		ifp->if_obytes += m->m_pkthdr.len;*/
402236951Srrs		/* override to IPPROTO_ETHERIP for bridged traffic */
403236951Srrs
404236951Srrs		M_SETFIB(m, sc->gif_fibnum);
405236951Srrs		/* inner AF-specific encapsulation */
406236951Srrs		/* XXX should we check if our outer source is legal? */
407236951Srrs		/* dispatch to output logic based on outer AF */
408236951Srrs		switch (sc->gif_psrc->sa_family) {
409236951Srrs#ifdef INET
410236951Srrs		case AF_INET:
411236951Srrs			error = in_gif_output(ifp, af, m);
412236951Srrs			break;
413236951Srrs#endif
414236951Srrs#ifdef INET6
415236951Srrs		case AF_INET6:
416236951Srrs			error = in6_gif_output(ifp, af, m);
417236951Srrs			break;
418236951Srrs#endif
419236951Srrs		default:
420236951Srrs			m_freem(m);
421236951Srrs			error = ENETDOWN;
422236951Srrs		}
423236951Srrs		if (error)
424236951Srrs			ifp->if_oerrors++;
425236951Srrs
426153621Sthompsa	}
427236951Srrs	GIF_LOCK(sc);
428236951Srrs	if (ifp->if_drv_flags & IFF_GIF_WANTED) {
429236951Srrs		/* Someone did a start while
430236951Srrs		 * we were unlocked and processing
431236951Srrs		 * lets clear the flag and try again.
432236951Srrs		 */
433236951Srrs		ifp->if_drv_flags &= ~IFF_GIF_WANTED;
434236951Srrs		GIF_UNLOCK(sc);
435236951Srrs		goto keep_going;
436236951Srrs	}
437153621Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
438236951Srrs	GIF_UNLOCK(sc);
439153621Sthompsa	return;
440153621Sthompsa}
441153621Sthompsa
44254263Sshinint
443191148Skmacygif_output(ifp, m, dst, ro)
44454263Sshin	struct ifnet *ifp;
44554263Sshin	struct mbuf *m;
44654263Sshin	struct sockaddr *dst;
447191148Skmacy	struct route *ro;
44854263Sshin{
449147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
450127898Sru	struct m_tag *mtag;
45154263Sshin	int error = 0;
452127898Sru	int gif_called;
453236951Srrs	uint32_t af;
454101182Srwatson#ifdef MAC
455172930Srwatson	error = mac_ifnet_check_transmit(ifp, m);
456101739Srwatson	if (error) {
457101739Srwatson		m_freem(m);
458101739Srwatson		goto end;
459101739Srwatson	}
460101182Srwatson#endif
461101182Srwatson
46254263Sshin	/*
46354263Sshin	 * gif may cause infinite recursion calls when misconfigured.
464127898Sru	 * We'll prevent this by detecting loops.
465127898Sru	 *
466127898Sru	 * High nesting level may cause stack exhaustion.
46754263Sshin	 * We'll prevent this by introducing upper limit.
46854263Sshin	 */
469127898Sru	gif_called = 1;
470127898Sru	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
471127898Sru	while (mtag != NULL) {
472127898Sru		if (*(struct ifnet **)(mtag + 1) == ifp) {
473127898Sru			log(LOG_NOTICE,
474127898Sru			    "gif_output: loop detected on %s\n",
475127898Sru			    (*(struct ifnet **)(mtag + 1))->if_xname);
476127898Sru			m_freem(m);
477127898Sru			error = EIO;	/* is there better errno? */
478127898Sru			goto end;
479127898Sru		}
480127898Sru		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
481127898Sru		gif_called++;
482127898Sru	}
483181803Sbz	if (gif_called > V_max_gif_nesting) {
48454263Sshin		log(LOG_NOTICE,
48554263Sshin		    "gif_output: recursively called too many times(%d)\n",
486127303Srwatson		    gif_called);
48754263Sshin		m_freem(m);
48854263Sshin		error = EIO;	/* is there better errno? */
48954263Sshin		goto end;
49054263Sshin	}
491127898Sru	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
492127898Sru	    M_NOWAIT);
493127898Sru	if (mtag == NULL) {
494127898Sru		m_freem(m);
495127898Sru		error = ENOMEM;
496127898Sru		goto end;
497127898Sru	}
498127898Sru	*(struct ifnet **)(mtag + 1) = ifp;
499127898Sru	m_tag_prepend(m, mtag);
50062587Sitojun
50154263Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
502236951Srrs	/* BPF writes need to be handled specially. */
503236951Srrs	if (dst->sa_family == AF_UNSPEC) {
504236951Srrs		bcopy(dst->sa_data, &af, sizeof(af));
505236951Srrs		dst->sa_family = af;
506236951Srrs	}
507236951Srrs	af = dst->sa_family;
508236957Srrs	/*
509236957Srrs	 * Now save the af in the inbound pkt csum
510236957Srrs	 * data, this is a cheat since we are using
511236957Srrs	 * the inbound csum_data field to carry the
512236957Srrs	 * af over to the gif_start() routine, avoiding
513236957Srrs	 * using yet another mtag.
514236951Srrs	 */
515236955Srrs	m->m_pkthdr.csum_data = af;
51654263Sshin	if (!(ifp->if_flags & IFF_UP) ||
51754263Sshin	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
51854263Sshin		m_freem(m);
51954263Sshin		error = ENETDOWN;
52054263Sshin		goto end;
52154263Sshin	}
522236951Srrs#ifdef ALTQ
523236957Srrs	/*
524236957Srrs	 * Make altq aware of the bytes we will add
525236951Srrs	 * when we actually send it.
526236951Srrs	 */
52754263Sshin#ifdef INET
528236951Srrs	if (sc->gif_psrc->sa_family == AF_INET)
529236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN;
53054263Sshin#endif
53154263Sshin#ifdef INET6
532236951Srrs	if (sc->gif_psrc->sa_family == AF_INET6)
533236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN6;
53454263Sshin#endif
535236951Srrs#endif
536236951Srrs	/*
537236951Srrs	 * Queue message on interface, update output statistics if
538236951Srrs	 * successful, and start output if interface not yet active.
539236951Srrs	 */
540236951Srrs	IFQ_HANDOFF(ifp, m, error);
54154263Sshin  end:
54278064Sume	if (error)
54378064Sume		ifp->if_oerrors++;
544155037Sglebius	return (error);
54554263Sshin}
54654263Sshin
54754263Sshinvoid
548105338Sumegif_input(m, af, ifp)
54954263Sshin	struct mbuf *m;
55054263Sshin	int af;
551105338Sume	struct ifnet *ifp;
55254263Sshin{
553153621Sthompsa	int isr, n;
554198357Sbrueffer	struct gif_softc *sc;
555153621Sthompsa	struct etherip_header *eip;
556176879Sthompsa	struct ether_header *eh;
557176879Sthompsa	struct ifnet *oldifp;
55854263Sshin
559105338Sume	if (ifp == NULL) {
56054263Sshin		/* just in case */
56154263Sshin		m_freem(m);
56254263Sshin		return;
56354263Sshin	}
564198357Sbrueffer	sc = ifp->if_softc;
565105338Sume	m->m_pkthdr.rcvif = ifp;
566101182Srwatson
567101182Srwatson#ifdef MAC
568172930Srwatson	mac_ifnet_create_mbuf(ifp, m);
569101182Srwatson#endif
570101182Srwatson
571159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
57278064Sume		u_int32_t af1 = af;
573123922Ssam		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
57454263Sshin	}
57554263Sshin
57683998Sbrooks	if (ng_gif_input_p != NULL) {
577105338Sume		(*ng_gif_input_p)(ifp, &m, af);
57883998Sbrooks		if (m == NULL)
57983998Sbrooks			return;
58083998Sbrooks	}
58183998Sbrooks
58254263Sshin	/*
58354263Sshin	 * Put the packet to the network layer input queue according to the
58454263Sshin	 * specified address family.
58554263Sshin	 * Note: older versions of gif_input directly called network layer
58695023Ssuz	 * input functions, e.g. ip6_input, here.  We changed the policy to
58754263Sshin	 * prevent too many recursive calls of such input functions, which
58895023Ssuz	 * might cause kernel panic.  But the change may introduce another
58954263Sshin	 * problem; if the input queue is full, packets are discarded.
59095023Ssuz	 * The kernel stack overflow really happened, and we believed
59195023Ssuz	 * queue-full rarely occurs, so we changed the policy.
59254263Sshin	 */
59354263Sshin	switch (af) {
59454263Sshin#ifdef INET
59554263Sshin	case AF_INET:
59654263Sshin		isr = NETISR_IP;
59754263Sshin		break;
59854263Sshin#endif
59954263Sshin#ifdef INET6
60054263Sshin	case AF_INET6:
60154263Sshin		isr = NETISR_IPV6;
60254263Sshin		break;
60354263Sshin#endif
604153621Sthompsa	case AF_LINK:
605153621Sthompsa		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
606153621Sthompsa		if (n > m->m_len) {
607153621Sthompsa			m = m_pullup(m, n);
608153621Sthompsa			if (m == NULL) {
609153621Sthompsa				ifp->if_ierrors++;
610153621Sthompsa				return;
611153621Sthompsa			}
612153621Sthompsa		}
613153621Sthompsa
614153621Sthompsa		eip = mtod(m, struct etherip_header *);
615193664Shrs		/*
616193664Shrs		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
617193664Shrs		 * accepts an EtherIP packet with revered version field in
618193664Shrs		 * the header.  This is a knob for backward compatibility
619193664Shrs		 * with FreeBSD 7.2R or prior.
620193664Shrs		 */
621193664Shrs		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
622193664Shrs			if (eip->eip_resvl != ETHERIP_VERSION
623193664Shrs			    && eip->eip_ver != ETHERIP_VERSION) {
624193664Shrs				/* discard unknown versions */
625193664Shrs				m_freem(m);
626193664Shrs				return;
627193664Shrs			}
628193664Shrs		} else {
629193664Shrs			if (eip->eip_ver != ETHERIP_VERSION) {
630193664Shrs				/* discard unknown versions */
631193664Shrs				m_freem(m);
632193664Shrs				return;
633193664Shrs			}
634153621Sthompsa		}
635153621Sthompsa		m_adj(m, sizeof(struct etherip_header));
636153621Sthompsa
637153621Sthompsa		m->m_flags &= ~(M_BCAST|M_MCAST);
638153621Sthompsa		m->m_pkthdr.rcvif = ifp;
639153621Sthompsa
640176879Sthompsa		if (ifp->if_bridge) {
641176879Sthompsa			oldifp = ifp;
642176879Sthompsa			eh = mtod(m, struct ether_header *);
643176879Sthompsa			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
644176879Sthompsa				if (ETHER_IS_BROADCAST(eh->ether_dhost))
645176879Sthompsa					m->m_flags |= M_BCAST;
646176879Sthompsa				else
647176879Sthompsa					m->m_flags |= M_MCAST;
648176879Sthompsa				ifp->if_imcasts++;
649176879Sthompsa			}
650153621Sthompsa			BRIDGE_INPUT(ifp, m);
651176879Sthompsa
652176879Sthompsa			if (m != NULL && ifp != oldifp) {
653176879Sthompsa				/*
654176879Sthompsa				 * The bridge gave us back itself or one of the
655176879Sthompsa				 * members for which the frame is addressed.
656176879Sthompsa				 */
657176879Sthompsa				ether_demux(ifp, m);
658176879Sthompsa				return;
659176879Sthompsa			}
660176879Sthompsa		}
661153621Sthompsa		if (m != NULL)
662153621Sthompsa			m_freem(m);
663153621Sthompsa		return;
664153621Sthompsa
66554263Sshin	default:
66683998Sbrooks		if (ng_gif_input_orphan_p != NULL)
667105338Sume			(*ng_gif_input_orphan_p)(ifp, m, af);
66883998Sbrooks		else
66983998Sbrooks			m_freem(m);
67054263Sshin		return;
67154263Sshin	}
67254263Sshin
673105338Sume	ifp->if_ipackets++;
674105338Sume	ifp->if_ibytes += m->m_pkthdr.len;
675223741Sbz	M_SETFIB(m, ifp->if_fib);
676111888Sjlemon	netisr_dispatch(isr, m);
67754263Sshin}
67854263Sshin
67962587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
68054263Sshinint
68154263Sshingif_ioctl(ifp, cmd, data)
68254263Sshin	struct ifnet *ifp;
68354263Sshin	u_long cmd;
68454263Sshin	caddr_t data;
68554263Sshin{
686147256Sbrooks	struct gif_softc *sc  = ifp->if_softc;
68754263Sshin	struct ifreq     *ifr = (struct ifreq*)data;
68854263Sshin	int error = 0, size;
689193664Shrs	u_int	options;
69062587Sitojun	struct sockaddr *dst, *src;
691105339Sume#ifdef	SIOCSIFMTU /* xxx */
692105339Sume	u_long mtu;
693105339Sume#endif
694105339Sume
69554263Sshin	switch (cmd) {
69654263Sshin	case SIOCSIFADDR:
697105293Sume		ifp->if_flags |= IFF_UP;
69854263Sshin		break;
69962587Sitojun
70054263Sshin	case SIOCSIFDSTADDR:
70154263Sshin		break;
70254263Sshin
70354263Sshin	case SIOCADDMULTI:
70454263Sshin	case SIOCDELMULTI:
70554263Sshin		break;
70654263Sshin
70762587Sitojun#ifdef	SIOCSIFMTU /* xxx */
70854263Sshin	case SIOCGIFMTU:
70954263Sshin		break;
71062587Sitojun
71154263Sshin	case SIOCSIFMTU:
712105339Sume		mtu = ifr->ifr_mtu;
713105339Sume		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
714105339Sume			return (EINVAL);
715105339Sume		ifp->if_mtu = mtu;
71654263Sshin		break;
71762587Sitojun#endif /* SIOCSIFMTU */
71854263Sshin
719105339Sume#ifdef INET
72054263Sshin	case SIOCSIFPHYADDR:
721105339Sume#endif
72254263Sshin#ifdef INET6
72354263Sshin	case SIOCSIFPHYADDR_IN6:
72454263Sshin#endif /* INET6 */
72578064Sume	case SIOCSLIFPHYADDR:
72662587Sitojun		switch (cmd) {
72778064Sume#ifdef INET
72862587Sitojun		case SIOCSIFPHYADDR:
72954263Sshin			src = (struct sockaddr *)
73054263Sshin				&(((struct in_aliasreq *)data)->ifra_addr);
73154263Sshin			dst = (struct sockaddr *)
73254263Sshin				&(((struct in_aliasreq *)data)->ifra_dstaddr);
73362587Sitojun			break;
73478064Sume#endif
73562587Sitojun#ifdef INET6
73662587Sitojun		case SIOCSIFPHYADDR_IN6:
73762587Sitojun			src = (struct sockaddr *)
73862587Sitojun				&(((struct in6_aliasreq *)data)->ifra_addr);
73962587Sitojun			dst = (struct sockaddr *)
74062587Sitojun				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
74162587Sitojun			break;
74262587Sitojun#endif
74378064Sume		case SIOCSLIFPHYADDR:
74478064Sume			src = (struct sockaddr *)
74578064Sume				&(((struct if_laddrreq *)data)->addr);
74678064Sume			dst = (struct sockaddr *)
74778064Sume				&(((struct if_laddrreq *)data)->dstaddr);
748105293Sume			break;
74991327Sbrooks		default:
750105293Sume			return EINVAL;
75162587Sitojun		}
75254263Sshin
75378064Sume		/* sa_family must be equal */
75478064Sume		if (src->sa_family != dst->sa_family)
75578064Sume			return EINVAL;
75678064Sume
75778064Sume		/* validate sa_len */
75878064Sume		switch (src->sa_family) {
75978064Sume#ifdef INET
76078064Sume		case AF_INET:
76178064Sume			if (src->sa_len != sizeof(struct sockaddr_in))
76278064Sume				return EINVAL;
76378064Sume			break;
76478064Sume#endif
76578064Sume#ifdef INET6
76678064Sume		case AF_INET6:
76778064Sume			if (src->sa_len != sizeof(struct sockaddr_in6))
76878064Sume				return EINVAL;
76978064Sume			break;
77078064Sume#endif
77178064Sume		default:
77278064Sume			return EAFNOSUPPORT;
77378064Sume		}
77478064Sume		switch (dst->sa_family) {
77578064Sume#ifdef INET
77678064Sume		case AF_INET:
77778064Sume			if (dst->sa_len != sizeof(struct sockaddr_in))
77878064Sume				return EINVAL;
77978064Sume			break;
78078064Sume#endif
78178064Sume#ifdef INET6
78278064Sume		case AF_INET6:
78378064Sume			if (dst->sa_len != sizeof(struct sockaddr_in6))
78478064Sume				return EINVAL;
78578064Sume			break;
78678064Sume#endif
78778064Sume		default:
78878064Sume			return EAFNOSUPPORT;
78978064Sume		}
79078064Sume
79178064Sume		/* check sa_family looks sane for the cmd */
79278064Sume		switch (cmd) {
79378064Sume		case SIOCSIFPHYADDR:
79478064Sume			if (src->sa_family == AF_INET)
79578064Sume				break;
79678064Sume			return EAFNOSUPPORT;
79778064Sume#ifdef INET6
79878064Sume		case SIOCSIFPHYADDR_IN6:
79978064Sume			if (src->sa_family == AF_INET6)
80078064Sume				break;
80178064Sume			return EAFNOSUPPORT;
80278064Sume#endif /* INET6 */
80378064Sume		case SIOCSLIFPHYADDR:
80478064Sume			/* checks done in the above */
80578064Sume			break;
80678064Sume		}
80778064Sume
808147256Sbrooks		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
80962587Sitojun		break;
81062587Sitojun
81162587Sitojun#ifdef SIOCDIFPHYADDR
81262587Sitojun	case SIOCDIFPHYADDR:
813147256Sbrooks		gif_delete_tunnel(GIF2IFP(sc));
81454263Sshin		break;
81562587Sitojun#endif
81662587Sitojun
81754263Sshin	case SIOCGIFPSRCADDR:
81854263Sshin#ifdef INET6
81954263Sshin	case SIOCGIFPSRCADDR_IN6:
82054263Sshin#endif /* INET6 */
82154263Sshin		if (sc->gif_psrc == NULL) {
82254263Sshin			error = EADDRNOTAVAIL;
82354263Sshin			goto bad;
82454263Sshin		}
82554263Sshin		src = sc->gif_psrc;
82678064Sume		switch (cmd) {
82754263Sshin#ifdef INET
82878064Sume		case SIOCGIFPSRCADDR:
82954263Sshin			dst = &ifr->ifr_addr;
83078064Sume			size = sizeof(ifr->ifr_addr);
83154263Sshin			break;
83254263Sshin#endif /* INET */
83354263Sshin#ifdef INET6
83478064Sume		case SIOCGIFPSRCADDR_IN6:
83554263Sshin			dst = (struct sockaddr *)
83654263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
83778064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
83854263Sshin			break;
83954263Sshin#endif /* INET6 */
84054263Sshin		default:
84154263Sshin			error = EADDRNOTAVAIL;
84254263Sshin			goto bad;
84354263Sshin		}
84478064Sume		if (src->sa_len > size)
84578064Sume			return EINVAL;
84678064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
847148385Sume#ifdef INET6
848148385Sume		if (dst->sa_family == AF_INET6) {
849148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
850148385Sume			if (error != 0)
851148385Sume				return (error);
852148385Sume		}
853148385Sume#endif
85454263Sshin		break;
85562587Sitojun
85654263Sshin	case SIOCGIFPDSTADDR:
85754263Sshin#ifdef INET6
85854263Sshin	case SIOCGIFPDSTADDR_IN6:
85954263Sshin#endif /* INET6 */
86054263Sshin		if (sc->gif_pdst == NULL) {
86154263Sshin			error = EADDRNOTAVAIL;
86254263Sshin			goto bad;
86354263Sshin		}
86454263Sshin		src = sc->gif_pdst;
86578064Sume		switch (cmd) {
86654263Sshin#ifdef INET
86778064Sume		case SIOCGIFPDSTADDR:
86854263Sshin			dst = &ifr->ifr_addr;
86978064Sume			size = sizeof(ifr->ifr_addr);
87054263Sshin			break;
87154263Sshin#endif /* INET */
87254263Sshin#ifdef INET6
87378064Sume		case SIOCGIFPDSTADDR_IN6:
87454263Sshin			dst = (struct sockaddr *)
87554263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
87678064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
87754263Sshin			break;
87854263Sshin#endif /* INET6 */
87954263Sshin		default:
88054263Sshin			error = EADDRNOTAVAIL;
88154263Sshin			goto bad;
88254263Sshin		}
88378064Sume		if (src->sa_len > size)
88478064Sume			return EINVAL;
885219206Sbz		error = prison_if(curthread->td_ucred, src);
886219206Sbz		if (error != 0)
887219206Sbz			return (error);
888219206Sbz		error = prison_if(curthread->td_ucred, dst);
889219206Sbz		if (error != 0)
890219206Sbz			return (error);
89178064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
892148385Sume#ifdef INET6
893148385Sume		if (dst->sa_family == AF_INET6) {
894148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
895148385Sume			if (error != 0)
896148385Sume				return (error);
897148385Sume		}
898148385Sume#endif
89954263Sshin		break;
90054263Sshin
90178064Sume	case SIOCGLIFPHYADDR:
90278064Sume		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
90378064Sume			error = EADDRNOTAVAIL;
90478064Sume			goto bad;
90578064Sume		}
90678064Sume
90778064Sume		/* copy src */
90878064Sume		src = sc->gif_psrc;
90978064Sume		dst = (struct sockaddr *)
91078064Sume			&(((struct if_laddrreq *)data)->addr);
91178064Sume		size = sizeof(((struct if_laddrreq *)data)->addr);
91278064Sume		if (src->sa_len > size)
91378064Sume			return EINVAL;
91478064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
91578064Sume
91678064Sume		/* copy dst */
91778064Sume		src = sc->gif_pdst;
91878064Sume		dst = (struct sockaddr *)
91978064Sume			&(((struct if_laddrreq *)data)->dstaddr);
92078064Sume		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
92178064Sume		if (src->sa_len > size)
92278064Sume			return EINVAL;
92378064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
92478064Sume		break;
92578064Sume
92654263Sshin	case SIOCSIFFLAGS:
92762587Sitojun		/* if_ioctl() takes care of it */
92854263Sshin		break;
92954263Sshin
930193664Shrs	case GIFGOPTS:
931193664Shrs		options = sc->gif_options;
932193664Shrs		error = copyout(&options, ifr->ifr_data,
933193664Shrs				sizeof(options));
934193664Shrs		break;
935193664Shrs
936193664Shrs	case GIFSOPTS:
937193664Shrs		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
938193664Shrs			break;
939193815Shrs		error = copyin(ifr->ifr_data, &options, sizeof(options));
940193815Shrs		if (error)
941193815Shrs			break;
942193815Shrs		if (options & ~GIF_OPTMASK)
943193815Shrs			error = EINVAL;
944193815Shrs		else
945193815Shrs			sc->gif_options = options;
946193664Shrs		break;
947193664Shrs
94854263Sshin	default:
94954263Sshin		error = EINVAL;
95054263Sshin		break;
95154263Sshin	}
95254263Sshin bad:
95354263Sshin	return error;
95454263Sshin}
95579106Sbrooks
956127305Srwatson/*
957127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check
958127305Srwatson * if a given tunnel is already present happens before we perform a
959127305Srwatson * potentially blocking setup of the tunnel.  This code needs to be
960127305Srwatson * re-ordered so that the check and replacement can be atomic using
961127305Srwatson * a mutex.
962127305Srwatson */
963105293Sumeint
964105293Sumegif_set_tunnel(ifp, src, dst)
965105293Sume	struct ifnet *ifp;
966105293Sume	struct sockaddr *src;
967105293Sume	struct sockaddr *dst;
968105293Sume{
969147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
970105293Sume	struct gif_softc *sc2;
971105293Sume	struct sockaddr *osrc, *odst, *sa;
972105293Sume	int error = 0;
973105293Sume
974127305Srwatson	mtx_lock(&gif_mtx);
975181803Sbz	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
976105293Sume		if (sc2 == sc)
977105293Sume			continue;
978105293Sume		if (!sc2->gif_pdst || !sc2->gif_psrc)
979105293Sume			continue;
980105293Sume		if (sc2->gif_pdst->sa_family != dst->sa_family ||
981105293Sume		    sc2->gif_pdst->sa_len != dst->sa_len ||
982105293Sume		    sc2->gif_psrc->sa_family != src->sa_family ||
983105293Sume		    sc2->gif_psrc->sa_len != src->sa_len)
984105293Sume			continue;
985105293Sume
986105293Sume		/*
987105293Sume		 * Disallow parallel tunnels unless instructed
988105293Sume		 * otherwise.
989105293Sume		 */
990181803Sbz		if (!V_parallel_tunnels &&
991105293Sume		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
992105293Sume		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
993105293Sume			error = EADDRNOTAVAIL;
994127305Srwatson			mtx_unlock(&gif_mtx);
995105293Sume			goto bad;
996105293Sume		}
997105293Sume
998105293Sume		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
999105293Sume	}
1000127305Srwatson	mtx_unlock(&gif_mtx);
1001105293Sume
1002105293Sume	/* XXX we can detach from both, but be polite just in case */
1003105293Sume	if (sc->gif_psrc)
1004105293Sume		switch (sc->gif_psrc->sa_family) {
1005105293Sume#ifdef INET
1006105293Sume		case AF_INET:
1007105293Sume			(void)in_gif_detach(sc);
1008105293Sume			break;
1009105293Sume#endif
1010105293Sume#ifdef INET6
1011105293Sume		case AF_INET6:
1012105293Sume			(void)in6_gif_detach(sc);
1013105293Sume			break;
1014105293Sume#endif
1015105293Sume		}
1016105293Sume
1017105293Sume	osrc = sc->gif_psrc;
1018111119Simp	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1019105293Sume	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1020105293Sume	sc->gif_psrc = sa;
1021105293Sume
1022105293Sume	odst = sc->gif_pdst;
1023111119Simp	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1024105293Sume	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1025105293Sume	sc->gif_pdst = sa;
1026105293Sume
1027105293Sume	switch (sc->gif_psrc->sa_family) {
1028105293Sume#ifdef INET
1029105293Sume	case AF_INET:
1030105293Sume		error = in_gif_attach(sc);
1031105293Sume		break;
1032105293Sume#endif
1033105293Sume#ifdef INET6
1034105293Sume	case AF_INET6:
1035148385Sume		/*
1036148385Sume		 * Check validity of the scope zone ID of the addresses, and
1037148385Sume		 * convert it into the kernel internal form if necessary.
1038148385Sume		 */
1039148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1040148385Sume		if (error != 0)
1041148385Sume			break;
1042148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1043148385Sume		if (error != 0)
1044148385Sume			break;
1045105293Sume		error = in6_gif_attach(sc);
1046105293Sume		break;
1047105293Sume#endif
1048105293Sume	}
1049105293Sume	if (error) {
1050105293Sume		/* rollback */
1051105293Sume		free((caddr_t)sc->gif_psrc, M_IFADDR);
1052105293Sume		free((caddr_t)sc->gif_pdst, M_IFADDR);
1053105293Sume		sc->gif_psrc = osrc;
1054105293Sume		sc->gif_pdst = odst;
1055105293Sume		goto bad;
1056105293Sume	}
1057105293Sume
1058105293Sume	if (osrc)
1059105293Sume		free((caddr_t)osrc, M_IFADDR);
1060105293Sume	if (odst)
1061105293Sume		free((caddr_t)odst, M_IFADDR);
1062105293Sume
1063105293Sume bad:
1064105293Sume	if (sc->gif_psrc && sc->gif_pdst)
1065148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1066105293Sume	else
1067148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1068105293Sume
1069105293Sume	return error;
1070105293Sume}
1071105293Sume
107279106Sbrooksvoid
1073105293Sumegif_delete_tunnel(ifp)
1074105293Sume	struct ifnet *ifp;
107579106Sbrooks{
1076147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
107779106Sbrooks
107879106Sbrooks	if (sc->gif_psrc) {
107979106Sbrooks		free((caddr_t)sc->gif_psrc, M_IFADDR);
108079106Sbrooks		sc->gif_psrc = NULL;
108179106Sbrooks	}
108279106Sbrooks	if (sc->gif_pdst) {
108379106Sbrooks		free((caddr_t)sc->gif_pdst, M_IFADDR);
108479106Sbrooks		sc->gif_pdst = NULL;
108579106Sbrooks	}
1086105293Sume	/* it is safe to detach from both */
1087105293Sume#ifdef INET
1088105293Sume	(void)in_gif_detach(sc);
1089105293Sume#endif
1090105293Sume#ifdef INET6
1091105293Sume	(void)in6_gif_detach(sc);
1092105293Sume#endif
1093160018Syar	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
109479106Sbrooks}
1095