if_gif.c revision 253261
162587Sitojun/*	$FreeBSD: head/sys/net/if_gif.c 253261 2013-07-12 12:18:07Z hrs $	*/
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
91241610Sglebiusstatic const char 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 *);
109241610Sglebiusstatic struct if_clone *gif_cloner;
11079106Sbrooks
11192725Salfredstatic int gifmodevent(module_t, int, void *);
11279106Sbrooks
11391270SbrooksSYSCTL_DECL(_net_link);
114227309Sedstatic SYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
11591270Sbrooks    "Generic Tunnel Interface");
11662587Sitojun#ifndef MAX_GIF_NEST
11762587Sitojun/*
11891270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels.
11962587Sitojun * Since, setting a large value to this macro with a careless configuration
12062587Sitojun * may introduce system crash, we don't allow any nestings by default.
12162587Sitojun * If you need to configure nested gif tunnels, you can define this macro
12295023Ssuz * in your kernel configuration file.  However, if you do so, please be
12362587Sitojun * careful to configure the tunnels so that it won't make a loop.
12462587Sitojun */
12562587Sitojun#define MAX_GIF_NEST 1
12662587Sitojun#endif
127215701Sdimstatic VNET_DEFINE(int, max_gif_nesting) = MAX_GIF_NEST;
128195837Srwatson#define	V_max_gif_nesting	VNET(max_gif_nesting)
129195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
130195699Srwatson    &VNET_NAME(max_gif_nesting), 0, "Max nested tunnels");
13162587Sitojun
13291270Sbrooks/*
13391270Sbrooks * By default, we disallow creation of multiple tunnels between the same
13491270Sbrooks * pair of addresses.  Some applications require this functionality so
13591270Sbrooks * we allow control over this check here.
13691270Sbrooks */
137195837Srwatson#ifdef XBONEHACK
138215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 1;
139195837Srwatson#else
140215701Sdimstatic VNET_DEFINE(int, parallel_tunnels) = 0;
141195837Srwatson#endif
142195837Srwatson#define	V_parallel_tunnels	VNET(parallel_tunnels)
143195699SrwatsonSYSCTL_VNET_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
144195699Srwatson    &VNET_NAME(parallel_tunnels), 0, "Allow parallel tunnels?");
14591270Sbrooks
146176879Sthompsa/* copy from src/sys/net/if_ethersubr.c */
147176879Sthompsastatic const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
148176879Sthompsa			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
149176879Sthompsa#ifndef ETHER_IS_BROADCAST
150176879Sthompsa#define ETHER_IS_BROADCAST(addr) \
151176879Sthompsa	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
152176879Sthompsa#endif
153176879Sthompsa
154128209Sbrooksstatic int
155160195Ssamgif_clone_create(ifc, unit, params)
15679106Sbrooks	struct if_clone *ifc;
15792081Smux	int unit;
158160195Ssam	caddr_t params;
15954263Sshin{
16078064Sume	struct gif_softc *sc;
16154263Sshin
162131672Sbms	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
163178888Sjulian	sc->gif_fibnum = curthread->td_proc->p_fibnum;
164147256Sbrooks	GIF2IFP(sc) = if_alloc(IFT_GIF);
165147256Sbrooks	if (GIF2IFP(sc) == NULL) {
166147256Sbrooks		free(sc, M_GIF);
167147256Sbrooks		return (ENOSPC);
168147256Sbrooks	}
16979106Sbrooks
170155037Sglebius	GIF_LOCK_INIT(sc);
171155037Sglebius
172147256Sbrooks	GIF2IFP(sc)->if_softc = sc;
173241610Sglebius	if_initname(GIF2IFP(sc), gifname, unit);
17479106Sbrooks
17579106Sbrooks	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
176253261Shrs	sc->gif_options = 0;
17762587Sitojun
178147256Sbrooks	GIF2IFP(sc)->if_addrlen = 0;
179147256Sbrooks	GIF2IFP(sc)->if_mtu    = GIF_MTU;
180147256Sbrooks	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
18178064Sume#if 0
18279106Sbrooks	/* turn off ingress filter */
183147256Sbrooks	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
18478064Sume#endif
185147256Sbrooks	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
186153621Sthompsa	GIF2IFP(sc)->if_start  = gif_start;
187147256Sbrooks	GIF2IFP(sc)->if_output = gif_output;
188207554Ssobomax	GIF2IFP(sc)->if_snd.ifq_maxlen = ifqmaxlen;
189147256Sbrooks	if_attach(GIF2IFP(sc));
190147611Sdwmalone	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
19183998Sbrooks	if (ng_gif_attach_p != NULL)
192147256Sbrooks		(*ng_gif_attach_p)(GIF2IFP(sc));
193155037Sglebius
194155037Sglebius	mtx_lock(&gif_mtx);
195181803Sbz	LIST_INSERT_HEAD(&V_gif_softc_list, sc, gif_list);
196155037Sglebius	mtx_unlock(&gif_mtx);
197155037Sglebius
198155037Sglebius	return (0);
19979106Sbrooks}
20079106Sbrooks
201127305Srwatsonstatic void
202151266Sthompsagif_clone_destroy(ifp)
203151266Sthompsa	struct ifnet *ifp;
20479106Sbrooks{
205184678Sbz#if defined(INET) || defined(INET6)
20679106Sbrooks	int err;
207184678Sbz#endif
208151266Sthompsa	struct gif_softc *sc = ifp->if_softc;
20979106Sbrooks
210151266Sthompsa	mtx_lock(&gif_mtx);
211151266Sthompsa	LIST_REMOVE(sc, gif_list);
212151266Sthompsa	mtx_unlock(&gif_mtx);
213151266Sthompsa
214127305Srwatson	gif_delete_tunnel(ifp);
215105293Sume#ifdef INET6
216105293Sume	if (sc->encap_cookie6 != NULL) {
217105293Sume		err = encap_detach(sc->encap_cookie6);
218105293Sume		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
219105293Sume	}
220105293Sume#endif
221105293Sume#ifdef INET
22279106Sbrooks	if (sc->encap_cookie4 != NULL) {
22379106Sbrooks		err = encap_detach(sc->encap_cookie4);
22479106Sbrooks		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
22579106Sbrooks	}
226105293Sume#endif
22779106Sbrooks
22883998Sbrooks	if (ng_gif_detach_p != NULL)
22983998Sbrooks		(*ng_gif_detach_p)(ifp);
23079106Sbrooks	bpfdetach(ifp);
23179106Sbrooks	if_detach(ifp);
232147256Sbrooks	if_free(ifp);
23379106Sbrooks
234155037Sglebius	GIF_LOCK_DESTROY(sc);
235155037Sglebius
23679106Sbrooks	free(sc, M_GIF);
23779106Sbrooks}
23879106Sbrooks
239195837Srwatsonstatic void
240195837Srwatsonvnet_gif_init(const void *unused __unused)
241190787Szec{
242190787Szec
243190787Szec	LIST_INIT(&V_gif_softc_list);
244190787Szec}
245195837SrwatsonVNET_SYSINIT(vnet_gif_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, vnet_gif_init,
246195837Srwatson    NULL);
247190787Szec
248190787Szecstatic int
24979106Sbrooksgifmodevent(mod, type, data)
25079106Sbrooks	module_t mod;
25179106Sbrooks	int type;
25279106Sbrooks	void *data;
25379106Sbrooks{
25479106Sbrooks
25579106Sbrooks	switch (type) {
25679106Sbrooks	case MOD_LOAD:
257127305Srwatson		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
258241610Sglebius		gif_cloner = if_clone_simple(gifname, gif_clone_create,
259241610Sglebius		    gif_clone_destroy, 0);
260195837Srwatson		break;
26179106Sbrooks
26279106Sbrooks	case MOD_UNLOAD:
263241610Sglebius		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,
275241394Skevlo	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	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
363236951Srrs	while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
364153621Sthompsa
365236951Srrs		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
366153621Sthompsa		if (m == 0)
367153621Sthompsa			break;
368153621Sthompsa
369236951Srrs#ifdef ALTQ
370236951Srrs		/* Take out those altq bytes we add in gif_output  */
371236951Srrs#ifdef INET
372236951Srrs		if (sc->gif_psrc->sa_family == AF_INET)
373236951Srrs			m->m_pkthdr.len -= GIF_HDR_LEN;
374236951Srrs#endif
375236951Srrs#ifdef INET6
376236951Srrs		if (sc->gif_psrc->sa_family == AF_INET6)
377236951Srrs		    m->m_pkthdr.len -= GIF_HDR_LEN6;
378236951Srrs#endif
379236951Srrs#endif
380236957Srrs		/*
381236957Srrs		 * Now pull back the af that we
382236957Srrs		 * stashed in the csum_data.
383236951Srrs		 */
384236955Srrs		af = m->m_pkthdr.csum_data;
385236955Srrs
386236951Srrs		if (ifp->if_bridge)
387236951Srrs			af = AF_LINK;
388153621Sthompsa
389236951Srrs		BPF_MTAP2(ifp, &af, sizeof(af), m);
390236951Srrs		ifp->if_opackets++;
391236951Srrs
392236951Srrs/*              Done by IFQ_HANDOFF */
393236951Srrs/* 		ifp->if_obytes += m->m_pkthdr.len;*/
394236951Srrs		/* override to IPPROTO_ETHERIP for bridged traffic */
395236951Srrs
396236951Srrs		M_SETFIB(m, sc->gif_fibnum);
397236951Srrs		/* inner AF-specific encapsulation */
398236951Srrs		/* XXX should we check if our outer source is legal? */
399236951Srrs		/* dispatch to output logic based on outer AF */
400236951Srrs		switch (sc->gif_psrc->sa_family) {
401236951Srrs#ifdef INET
402236951Srrs		case AF_INET:
403236951Srrs			error = in_gif_output(ifp, af, m);
404236951Srrs			break;
405236951Srrs#endif
406236951Srrs#ifdef INET6
407236951Srrs		case AF_INET6:
408236951Srrs			error = in6_gif_output(ifp, af, m);
409236951Srrs			break;
410236951Srrs#endif
411236951Srrs		default:
412236951Srrs			m_freem(m);
413236951Srrs			error = ENETDOWN;
414236951Srrs		}
415236951Srrs		if (error)
416236951Srrs			ifp->if_oerrors++;
417236951Srrs
418153621Sthompsa	}
419153621Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
420236951Srrs	GIF_UNLOCK(sc);
421153621Sthompsa	return;
422153621Sthompsa}
423153621Sthompsa
42454263Sshinint
425249925Sglebiusgif_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
426249925Sglebius	struct route *ro)
42754263Sshin{
428147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
429127898Sru	struct m_tag *mtag;
43054263Sshin	int error = 0;
431127898Sru	int gif_called;
432236951Srrs	uint32_t af;
433101182Srwatson#ifdef MAC
434172930Srwatson	error = mac_ifnet_check_transmit(ifp, m);
435101739Srwatson	if (error) {
436101739Srwatson		m_freem(m);
437101739Srwatson		goto end;
438101739Srwatson	}
439101182Srwatson#endif
440253261Shrs	if ((ifp->if_flags & IFF_MONITOR) != 0) {
441253261Shrs		error = ENETDOWN;
442253261Shrs		m_freem(m);
443253261Shrs		goto end;
444253261Shrs	}
445101182Srwatson
44654263Sshin	/*
44754263Sshin	 * gif may cause infinite recursion calls when misconfigured.
448127898Sru	 * We'll prevent this by detecting loops.
449127898Sru	 *
450127898Sru	 * High nesting level may cause stack exhaustion.
45154263Sshin	 * We'll prevent this by introducing upper limit.
45254263Sshin	 */
453127898Sru	gif_called = 1;
454127898Sru	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
455127898Sru	while (mtag != NULL) {
456127898Sru		if (*(struct ifnet **)(mtag + 1) == ifp) {
457127898Sru			log(LOG_NOTICE,
458127898Sru			    "gif_output: loop detected on %s\n",
459127898Sru			    (*(struct ifnet **)(mtag + 1))->if_xname);
460127898Sru			m_freem(m);
461127898Sru			error = EIO;	/* is there better errno? */
462127898Sru			goto end;
463127898Sru		}
464127898Sru		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
465127898Sru		gif_called++;
466127898Sru	}
467181803Sbz	if (gif_called > V_max_gif_nesting) {
46854263Sshin		log(LOG_NOTICE,
46954263Sshin		    "gif_output: recursively called too many times(%d)\n",
470127303Srwatson		    gif_called);
47154263Sshin		m_freem(m);
47254263Sshin		error = EIO;	/* is there better errno? */
47354263Sshin		goto end;
47454263Sshin	}
475127898Sru	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
476127898Sru	    M_NOWAIT);
477127898Sru	if (mtag == NULL) {
478127898Sru		m_freem(m);
479127898Sru		error = ENOMEM;
480127898Sru		goto end;
481127898Sru	}
482127898Sru	*(struct ifnet **)(mtag + 1) = ifp;
483127898Sru	m_tag_prepend(m, mtag);
48462587Sitojun
48554263Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
486236951Srrs	/* BPF writes need to be handled specially. */
487249925Sglebius	if (dst->sa_family == AF_UNSPEC)
488236951Srrs		bcopy(dst->sa_data, &af, sizeof(af));
489249925Sglebius	else
490249925Sglebius		af = dst->sa_family;
491236957Srrs	/*
492236957Srrs	 * Now save the af in the inbound pkt csum
493236957Srrs	 * data, this is a cheat since we are using
494236957Srrs	 * the inbound csum_data field to carry the
495236957Srrs	 * af over to the gif_start() routine, avoiding
496236957Srrs	 * using yet another mtag.
497236951Srrs	 */
498236955Srrs	m->m_pkthdr.csum_data = af;
49954263Sshin	if (!(ifp->if_flags & IFF_UP) ||
50054263Sshin	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
50154263Sshin		m_freem(m);
50254263Sshin		error = ENETDOWN;
50354263Sshin		goto end;
50454263Sshin	}
505236951Srrs#ifdef ALTQ
506236957Srrs	/*
507236957Srrs	 * Make altq aware of the bytes we will add
508236951Srrs	 * when we actually send it.
509236951Srrs	 */
51054263Sshin#ifdef INET
511236951Srrs	if (sc->gif_psrc->sa_family == AF_INET)
512236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN;
51354263Sshin#endif
51454263Sshin#ifdef INET6
515236951Srrs	if (sc->gif_psrc->sa_family == AF_INET6)
516236951Srrs		m->m_pkthdr.len += GIF_HDR_LEN6;
51754263Sshin#endif
518236951Srrs#endif
519236951Srrs	/*
520236951Srrs	 * Queue message on interface, update output statistics if
521236951Srrs	 * successful, and start output if interface not yet active.
522236951Srrs	 */
523236951Srrs	IFQ_HANDOFF(ifp, m, error);
52454263Sshin  end:
52578064Sume	if (error)
52678064Sume		ifp->if_oerrors++;
527155037Sglebius	return (error);
52854263Sshin}
52954263Sshin
53054263Sshinvoid
531105338Sumegif_input(m, af, ifp)
53254263Sshin	struct mbuf *m;
53354263Sshin	int af;
534105338Sume	struct ifnet *ifp;
53554263Sshin{
536153621Sthompsa	int isr, n;
537198357Sbrueffer	struct gif_softc *sc;
538153621Sthompsa	struct etherip_header *eip;
539176879Sthompsa	struct ether_header *eh;
540176879Sthompsa	struct ifnet *oldifp;
54154263Sshin
542105338Sume	if (ifp == NULL) {
54354263Sshin		/* just in case */
54454263Sshin		m_freem(m);
54554263Sshin		return;
54654263Sshin	}
547198357Sbrueffer	sc = ifp->if_softc;
548105338Sume	m->m_pkthdr.rcvif = ifp;
549101182Srwatson
550101182Srwatson#ifdef MAC
551172930Srwatson	mac_ifnet_create_mbuf(ifp, m);
552101182Srwatson#endif
553101182Srwatson
554159180Scsjp	if (bpf_peers_present(ifp->if_bpf)) {
55578064Sume		u_int32_t af1 = af;
556123922Ssam		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
55754263Sshin	}
55854263Sshin
559253261Shrs	if ((ifp->if_flags & IFF_MONITOR) != 0) {
560253261Shrs		ifp->if_ipackets++;
561253261Shrs		ifp->if_ibytes += m->m_pkthdr.len;
562253261Shrs		m_freem(m);
563253261Shrs		return;
564253261Shrs	}
565253261Shrs
56683998Sbrooks	if (ng_gif_input_p != NULL) {
567105338Sume		(*ng_gif_input_p)(ifp, &m, af);
56883998Sbrooks		if (m == NULL)
56983998Sbrooks			return;
57083998Sbrooks	}
57183998Sbrooks
57254263Sshin	/*
57354263Sshin	 * Put the packet to the network layer input queue according to the
57454263Sshin	 * specified address family.
57554263Sshin	 * Note: older versions of gif_input directly called network layer
57695023Ssuz	 * input functions, e.g. ip6_input, here.  We changed the policy to
57754263Sshin	 * prevent too many recursive calls of such input functions, which
57895023Ssuz	 * might cause kernel panic.  But the change may introduce another
57954263Sshin	 * problem; if the input queue is full, packets are discarded.
58095023Ssuz	 * The kernel stack overflow really happened, and we believed
58195023Ssuz	 * queue-full rarely occurs, so we changed the policy.
58254263Sshin	 */
58354263Sshin	switch (af) {
58454263Sshin#ifdef INET
58554263Sshin	case AF_INET:
58654263Sshin		isr = NETISR_IP;
58754263Sshin		break;
58854263Sshin#endif
58954263Sshin#ifdef INET6
59054263Sshin	case AF_INET6:
59154263Sshin		isr = NETISR_IPV6;
59254263Sshin		break;
59354263Sshin#endif
594153621Sthompsa	case AF_LINK:
595153621Sthompsa		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
596153621Sthompsa		if (n > m->m_len) {
597153621Sthompsa			m = m_pullup(m, n);
598153621Sthompsa			if (m == NULL) {
599153621Sthompsa				ifp->if_ierrors++;
600153621Sthompsa				return;
601153621Sthompsa			}
602153621Sthompsa		}
603153621Sthompsa
604153621Sthompsa		eip = mtod(m, struct etherip_header *);
605193664Shrs		/*
606193664Shrs		 * GIF_ACCEPT_REVETHIP (enabled by default) intentionally
607193664Shrs		 * accepts an EtherIP packet with revered version field in
608193664Shrs		 * the header.  This is a knob for backward compatibility
609193664Shrs		 * with FreeBSD 7.2R or prior.
610193664Shrs		 */
611193664Shrs		if (sc->gif_options & GIF_ACCEPT_REVETHIP) {
612193664Shrs			if (eip->eip_resvl != ETHERIP_VERSION
613193664Shrs			    && eip->eip_ver != ETHERIP_VERSION) {
614193664Shrs				/* discard unknown versions */
615193664Shrs				m_freem(m);
616193664Shrs				return;
617193664Shrs			}
618193664Shrs		} else {
619193664Shrs			if (eip->eip_ver != ETHERIP_VERSION) {
620193664Shrs				/* discard unknown versions */
621193664Shrs				m_freem(m);
622193664Shrs				return;
623193664Shrs			}
624153621Sthompsa		}
625153621Sthompsa		m_adj(m, sizeof(struct etherip_header));
626153621Sthompsa
627153621Sthompsa		m->m_flags &= ~(M_BCAST|M_MCAST);
628153621Sthompsa		m->m_pkthdr.rcvif = ifp;
629153621Sthompsa
630176879Sthompsa		if (ifp->if_bridge) {
631176879Sthompsa			oldifp = ifp;
632176879Sthompsa			eh = mtod(m, struct ether_header *);
633176879Sthompsa			if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
634176879Sthompsa				if (ETHER_IS_BROADCAST(eh->ether_dhost))
635176879Sthompsa					m->m_flags |= M_BCAST;
636176879Sthompsa				else
637176879Sthompsa					m->m_flags |= M_MCAST;
638176879Sthompsa				ifp->if_imcasts++;
639176879Sthompsa			}
640153621Sthompsa			BRIDGE_INPUT(ifp, m);
641176879Sthompsa
642176879Sthompsa			if (m != NULL && ifp != oldifp) {
643176879Sthompsa				/*
644176879Sthompsa				 * The bridge gave us back itself or one of the
645176879Sthompsa				 * members for which the frame is addressed.
646176879Sthompsa				 */
647176879Sthompsa				ether_demux(ifp, m);
648176879Sthompsa				return;
649176879Sthompsa			}
650176879Sthompsa		}
651153621Sthompsa		if (m != NULL)
652153621Sthompsa			m_freem(m);
653153621Sthompsa		return;
654153621Sthompsa
65554263Sshin	default:
65683998Sbrooks		if (ng_gif_input_orphan_p != NULL)
657105338Sume			(*ng_gif_input_orphan_p)(ifp, m, af);
65883998Sbrooks		else
65983998Sbrooks			m_freem(m);
66054263Sshin		return;
66154263Sshin	}
66254263Sshin
663105338Sume	ifp->if_ipackets++;
664105338Sume	ifp->if_ibytes += m->m_pkthdr.len;
665223741Sbz	M_SETFIB(m, ifp->if_fib);
666111888Sjlemon	netisr_dispatch(isr, m);
66754263Sshin}
66854263Sshin
66962587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
67054263Sshinint
67154263Sshingif_ioctl(ifp, cmd, data)
67254263Sshin	struct ifnet *ifp;
67354263Sshin	u_long cmd;
67454263Sshin	caddr_t data;
67554263Sshin{
676147256Sbrooks	struct gif_softc *sc  = ifp->if_softc;
67754263Sshin	struct ifreq     *ifr = (struct ifreq*)data;
67854263Sshin	int error = 0, size;
679193664Shrs	u_int	options;
68062587Sitojun	struct sockaddr *dst, *src;
681105339Sume#ifdef	SIOCSIFMTU /* xxx */
682105339Sume	u_long mtu;
683105339Sume#endif
684105339Sume
68554263Sshin	switch (cmd) {
68654263Sshin	case SIOCSIFADDR:
687105293Sume		ifp->if_flags |= IFF_UP;
68854263Sshin		break;
68962587Sitojun
69054263Sshin	case SIOCSIFDSTADDR:
69154263Sshin		break;
69254263Sshin
69354263Sshin	case SIOCADDMULTI:
69454263Sshin	case SIOCDELMULTI:
69554263Sshin		break;
69654263Sshin
69762587Sitojun#ifdef	SIOCSIFMTU /* xxx */
69854263Sshin	case SIOCGIFMTU:
69954263Sshin		break;
70062587Sitojun
70154263Sshin	case SIOCSIFMTU:
702105339Sume		mtu = ifr->ifr_mtu;
703105339Sume		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
704105339Sume			return (EINVAL);
705105339Sume		ifp->if_mtu = mtu;
70654263Sshin		break;
70762587Sitojun#endif /* SIOCSIFMTU */
70854263Sshin
709105339Sume#ifdef INET
71054263Sshin	case SIOCSIFPHYADDR:
711105339Sume#endif
71254263Sshin#ifdef INET6
71354263Sshin	case SIOCSIFPHYADDR_IN6:
71454263Sshin#endif /* INET6 */
71578064Sume	case SIOCSLIFPHYADDR:
71662587Sitojun		switch (cmd) {
71778064Sume#ifdef INET
71862587Sitojun		case SIOCSIFPHYADDR:
71954263Sshin			src = (struct sockaddr *)
72054263Sshin				&(((struct in_aliasreq *)data)->ifra_addr);
72154263Sshin			dst = (struct sockaddr *)
72254263Sshin				&(((struct in_aliasreq *)data)->ifra_dstaddr);
72362587Sitojun			break;
72478064Sume#endif
72562587Sitojun#ifdef INET6
72662587Sitojun		case SIOCSIFPHYADDR_IN6:
72762587Sitojun			src = (struct sockaddr *)
72862587Sitojun				&(((struct in6_aliasreq *)data)->ifra_addr);
72962587Sitojun			dst = (struct sockaddr *)
73062587Sitojun				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
73162587Sitojun			break;
73262587Sitojun#endif
73378064Sume		case SIOCSLIFPHYADDR:
73478064Sume			src = (struct sockaddr *)
73578064Sume				&(((struct if_laddrreq *)data)->addr);
73678064Sume			dst = (struct sockaddr *)
73778064Sume				&(((struct if_laddrreq *)data)->dstaddr);
738105293Sume			break;
73991327Sbrooks		default:
740105293Sume			return EINVAL;
74162587Sitojun		}
74254263Sshin
74378064Sume		/* sa_family must be equal */
74478064Sume		if (src->sa_family != dst->sa_family)
74578064Sume			return EINVAL;
74678064Sume
74778064Sume		/* validate sa_len */
74878064Sume		switch (src->sa_family) {
74978064Sume#ifdef INET
75078064Sume		case AF_INET:
75178064Sume			if (src->sa_len != sizeof(struct sockaddr_in))
75278064Sume				return EINVAL;
75378064Sume			break;
75478064Sume#endif
75578064Sume#ifdef INET6
75678064Sume		case AF_INET6:
75778064Sume			if (src->sa_len != sizeof(struct sockaddr_in6))
75878064Sume				return EINVAL;
75978064Sume			break;
76078064Sume#endif
76178064Sume		default:
76278064Sume			return EAFNOSUPPORT;
76378064Sume		}
76478064Sume		switch (dst->sa_family) {
76578064Sume#ifdef INET
76678064Sume		case AF_INET:
76778064Sume			if (dst->sa_len != sizeof(struct sockaddr_in))
76878064Sume				return EINVAL;
76978064Sume			break;
77078064Sume#endif
77178064Sume#ifdef INET6
77278064Sume		case AF_INET6:
77378064Sume			if (dst->sa_len != sizeof(struct sockaddr_in6))
77478064Sume				return EINVAL;
77578064Sume			break;
77678064Sume#endif
77778064Sume		default:
77878064Sume			return EAFNOSUPPORT;
77978064Sume		}
78078064Sume
78178064Sume		/* check sa_family looks sane for the cmd */
78278064Sume		switch (cmd) {
78378064Sume		case SIOCSIFPHYADDR:
78478064Sume			if (src->sa_family == AF_INET)
78578064Sume				break;
78678064Sume			return EAFNOSUPPORT;
78778064Sume#ifdef INET6
78878064Sume		case SIOCSIFPHYADDR_IN6:
78978064Sume			if (src->sa_family == AF_INET6)
79078064Sume				break;
79178064Sume			return EAFNOSUPPORT;
79278064Sume#endif /* INET6 */
79378064Sume		case SIOCSLIFPHYADDR:
79478064Sume			/* checks done in the above */
79578064Sume			break;
79678064Sume		}
79778064Sume
798147256Sbrooks		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
79962587Sitojun		break;
80062587Sitojun
80162587Sitojun#ifdef SIOCDIFPHYADDR
80262587Sitojun	case SIOCDIFPHYADDR:
803147256Sbrooks		gif_delete_tunnel(GIF2IFP(sc));
80454263Sshin		break;
80562587Sitojun#endif
80662587Sitojun
80754263Sshin	case SIOCGIFPSRCADDR:
80854263Sshin#ifdef INET6
80954263Sshin	case SIOCGIFPSRCADDR_IN6:
81054263Sshin#endif /* INET6 */
81154263Sshin		if (sc->gif_psrc == NULL) {
81254263Sshin			error = EADDRNOTAVAIL;
81354263Sshin			goto bad;
81454263Sshin		}
81554263Sshin		src = sc->gif_psrc;
81678064Sume		switch (cmd) {
81754263Sshin#ifdef INET
81878064Sume		case SIOCGIFPSRCADDR:
81954263Sshin			dst = &ifr->ifr_addr;
82078064Sume			size = sizeof(ifr->ifr_addr);
82154263Sshin			break;
82254263Sshin#endif /* INET */
82354263Sshin#ifdef INET6
82478064Sume		case SIOCGIFPSRCADDR_IN6:
82554263Sshin			dst = (struct sockaddr *)
82654263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
82778064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
82854263Sshin			break;
82954263Sshin#endif /* INET6 */
83054263Sshin		default:
83154263Sshin			error = EADDRNOTAVAIL;
83254263Sshin			goto bad;
83354263Sshin		}
83478064Sume		if (src->sa_len > size)
83578064Sume			return EINVAL;
83678064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
837148385Sume#ifdef INET6
838148385Sume		if (dst->sa_family == AF_INET6) {
839148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
840148385Sume			if (error != 0)
841148385Sume				return (error);
842148385Sume		}
843148385Sume#endif
84454263Sshin		break;
84562587Sitojun
84654263Sshin	case SIOCGIFPDSTADDR:
84754263Sshin#ifdef INET6
84854263Sshin	case SIOCGIFPDSTADDR_IN6:
84954263Sshin#endif /* INET6 */
85054263Sshin		if (sc->gif_pdst == NULL) {
85154263Sshin			error = EADDRNOTAVAIL;
85254263Sshin			goto bad;
85354263Sshin		}
85454263Sshin		src = sc->gif_pdst;
85578064Sume		switch (cmd) {
85654263Sshin#ifdef INET
85778064Sume		case SIOCGIFPDSTADDR:
85854263Sshin			dst = &ifr->ifr_addr;
85978064Sume			size = sizeof(ifr->ifr_addr);
86054263Sshin			break;
86154263Sshin#endif /* INET */
86254263Sshin#ifdef INET6
86378064Sume		case SIOCGIFPDSTADDR_IN6:
86454263Sshin			dst = (struct sockaddr *)
86554263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
86678064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
86754263Sshin			break;
86854263Sshin#endif /* INET6 */
86954263Sshin		default:
87054263Sshin			error = EADDRNOTAVAIL;
87154263Sshin			goto bad;
87254263Sshin		}
87378064Sume		if (src->sa_len > size)
87478064Sume			return EINVAL;
875219206Sbz		error = prison_if(curthread->td_ucred, src);
876219206Sbz		if (error != 0)
877219206Sbz			return (error);
878219206Sbz		error = prison_if(curthread->td_ucred, dst);
879219206Sbz		if (error != 0)
880219206Sbz			return (error);
88178064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
882148385Sume#ifdef INET6
883148385Sume		if (dst->sa_family == AF_INET6) {
884148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
885148385Sume			if (error != 0)
886148385Sume				return (error);
887148385Sume		}
888148385Sume#endif
88954263Sshin		break;
89054263Sshin
89178064Sume	case SIOCGLIFPHYADDR:
89278064Sume		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
89378064Sume			error = EADDRNOTAVAIL;
89478064Sume			goto bad;
89578064Sume		}
89678064Sume
89778064Sume		/* copy src */
89878064Sume		src = sc->gif_psrc;
89978064Sume		dst = (struct sockaddr *)
90078064Sume			&(((struct if_laddrreq *)data)->addr);
90178064Sume		size = sizeof(((struct if_laddrreq *)data)->addr);
90278064Sume		if (src->sa_len > size)
90378064Sume			return EINVAL;
90478064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
90578064Sume
90678064Sume		/* copy dst */
90778064Sume		src = sc->gif_pdst;
90878064Sume		dst = (struct sockaddr *)
90978064Sume			&(((struct if_laddrreq *)data)->dstaddr);
91078064Sume		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
91178064Sume		if (src->sa_len > size)
91278064Sume			return EINVAL;
91378064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
91478064Sume		break;
91578064Sume
91654263Sshin	case SIOCSIFFLAGS:
91762587Sitojun		/* if_ioctl() takes care of it */
91854263Sshin		break;
91954263Sshin
920193664Shrs	case GIFGOPTS:
921193664Shrs		options = sc->gif_options;
922193664Shrs		error = copyout(&options, ifr->ifr_data,
923193664Shrs				sizeof(options));
924193664Shrs		break;
925193664Shrs
926193664Shrs	case GIFSOPTS:
927193664Shrs		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
928193664Shrs			break;
929193815Shrs		error = copyin(ifr->ifr_data, &options, sizeof(options));
930193815Shrs		if (error)
931193815Shrs			break;
932193815Shrs		if (options & ~GIF_OPTMASK)
933193815Shrs			error = EINVAL;
934193815Shrs		else
935193815Shrs			sc->gif_options = options;
936193664Shrs		break;
937193664Shrs
93854263Sshin	default:
93954263Sshin		error = EINVAL;
94054263Sshin		break;
94154263Sshin	}
94254263Sshin bad:
94354263Sshin	return error;
94454263Sshin}
94579106Sbrooks
946127305Srwatson/*
947127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check
948127305Srwatson * if a given tunnel is already present happens before we perform a
949127305Srwatson * potentially blocking setup of the tunnel.  This code needs to be
950127305Srwatson * re-ordered so that the check and replacement can be atomic using
951127305Srwatson * a mutex.
952127305Srwatson */
953105293Sumeint
954105293Sumegif_set_tunnel(ifp, src, dst)
955105293Sume	struct ifnet *ifp;
956105293Sume	struct sockaddr *src;
957105293Sume	struct sockaddr *dst;
958105293Sume{
959147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
960105293Sume	struct gif_softc *sc2;
961105293Sume	struct sockaddr *osrc, *odst, *sa;
962105293Sume	int error = 0;
963105293Sume
964127305Srwatson	mtx_lock(&gif_mtx);
965181803Sbz	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
966105293Sume		if (sc2 == sc)
967105293Sume			continue;
968105293Sume		if (!sc2->gif_pdst || !sc2->gif_psrc)
969105293Sume			continue;
970105293Sume		if (sc2->gif_pdst->sa_family != dst->sa_family ||
971105293Sume		    sc2->gif_pdst->sa_len != dst->sa_len ||
972105293Sume		    sc2->gif_psrc->sa_family != src->sa_family ||
973105293Sume		    sc2->gif_psrc->sa_len != src->sa_len)
974105293Sume			continue;
975105293Sume
976105293Sume		/*
977105293Sume		 * Disallow parallel tunnels unless instructed
978105293Sume		 * otherwise.
979105293Sume		 */
980181803Sbz		if (!V_parallel_tunnels &&
981105293Sume		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
982105293Sume		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
983105293Sume			error = EADDRNOTAVAIL;
984127305Srwatson			mtx_unlock(&gif_mtx);
985105293Sume			goto bad;
986105293Sume		}
987105293Sume
988105293Sume		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
989105293Sume	}
990127305Srwatson	mtx_unlock(&gif_mtx);
991105293Sume
992105293Sume	/* XXX we can detach from both, but be polite just in case */
993105293Sume	if (sc->gif_psrc)
994105293Sume		switch (sc->gif_psrc->sa_family) {
995105293Sume#ifdef INET
996105293Sume		case AF_INET:
997105293Sume			(void)in_gif_detach(sc);
998105293Sume			break;
999105293Sume#endif
1000105293Sume#ifdef INET6
1001105293Sume		case AF_INET6:
1002105293Sume			(void)in6_gif_detach(sc);
1003105293Sume			break;
1004105293Sume#endif
1005105293Sume		}
1006105293Sume
1007105293Sume	osrc = sc->gif_psrc;
1008111119Simp	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1009105293Sume	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1010105293Sume	sc->gif_psrc = sa;
1011105293Sume
1012105293Sume	odst = sc->gif_pdst;
1013111119Simp	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1014105293Sume	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1015105293Sume	sc->gif_pdst = sa;
1016105293Sume
1017105293Sume	switch (sc->gif_psrc->sa_family) {
1018105293Sume#ifdef INET
1019105293Sume	case AF_INET:
1020105293Sume		error = in_gif_attach(sc);
1021105293Sume		break;
1022105293Sume#endif
1023105293Sume#ifdef INET6
1024105293Sume	case AF_INET6:
1025148385Sume		/*
1026148385Sume		 * Check validity of the scope zone ID of the addresses, and
1027148385Sume		 * convert it into the kernel internal form if necessary.
1028148385Sume		 */
1029148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1030148385Sume		if (error != 0)
1031148385Sume			break;
1032148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1033148385Sume		if (error != 0)
1034148385Sume			break;
1035105293Sume		error = in6_gif_attach(sc);
1036105293Sume		break;
1037105293Sume#endif
1038105293Sume	}
1039105293Sume	if (error) {
1040105293Sume		/* rollback */
1041105293Sume		free((caddr_t)sc->gif_psrc, M_IFADDR);
1042105293Sume		free((caddr_t)sc->gif_pdst, M_IFADDR);
1043105293Sume		sc->gif_psrc = osrc;
1044105293Sume		sc->gif_pdst = odst;
1045105293Sume		goto bad;
1046105293Sume	}
1047105293Sume
1048105293Sume	if (osrc)
1049105293Sume		free((caddr_t)osrc, M_IFADDR);
1050105293Sume	if (odst)
1051105293Sume		free((caddr_t)odst, M_IFADDR);
1052105293Sume
1053105293Sume bad:
1054105293Sume	if (sc->gif_psrc && sc->gif_pdst)
1055148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1056105293Sume	else
1057148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1058105293Sume
1059105293Sume	return error;
1060105293Sume}
1061105293Sume
106279106Sbrooksvoid
1063105293Sumegif_delete_tunnel(ifp)
1064105293Sume	struct ifnet *ifp;
106579106Sbrooks{
1066147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
106779106Sbrooks
106879106Sbrooks	if (sc->gif_psrc) {
106979106Sbrooks		free((caddr_t)sc->gif_psrc, M_IFADDR);
107079106Sbrooks		sc->gif_psrc = NULL;
107179106Sbrooks	}
107279106Sbrooks	if (sc->gif_pdst) {
107379106Sbrooks		free((caddr_t)sc->gif_pdst, M_IFADDR);
107479106Sbrooks		sc->gif_pdst = NULL;
107579106Sbrooks	}
1076105293Sume	/* it is safe to detach from both */
1077105293Sume#ifdef INET
1078105293Sume	(void)in_gif_detach(sc);
1079105293Sume#endif
1080105293Sume#ifdef INET6
1081105293Sume	(void)in6_gif_detach(sc);
1082105293Sume#endif
1083160018Syar	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
108479106Sbrooks}
1085