162587Sitojun/*	$FreeBSD$	*/
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 SIOCADDMULTI:
69154263Sshin	case SIOCDELMULTI:
69254263Sshin		break;
69354263Sshin
69462587Sitojun#ifdef	SIOCSIFMTU /* xxx */
69554263Sshin	case SIOCGIFMTU:
69654263Sshin		break;
69762587Sitojun
69854263Sshin	case SIOCSIFMTU:
699105339Sume		mtu = ifr->ifr_mtu;
700105339Sume		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
701105339Sume			return (EINVAL);
702105339Sume		ifp->if_mtu = mtu;
70354263Sshin		break;
70462587Sitojun#endif /* SIOCSIFMTU */
70554263Sshin
706105339Sume#ifdef INET
70754263Sshin	case SIOCSIFPHYADDR:
708105339Sume#endif
70954263Sshin#ifdef INET6
71054263Sshin	case SIOCSIFPHYADDR_IN6:
71154263Sshin#endif /* INET6 */
71278064Sume	case SIOCSLIFPHYADDR:
71362587Sitojun		switch (cmd) {
71478064Sume#ifdef INET
71562587Sitojun		case SIOCSIFPHYADDR:
71654263Sshin			src = (struct sockaddr *)
71754263Sshin				&(((struct in_aliasreq *)data)->ifra_addr);
71854263Sshin			dst = (struct sockaddr *)
71954263Sshin				&(((struct in_aliasreq *)data)->ifra_dstaddr);
72062587Sitojun			break;
72178064Sume#endif
72262587Sitojun#ifdef INET6
72362587Sitojun		case SIOCSIFPHYADDR_IN6:
72462587Sitojun			src = (struct sockaddr *)
72562587Sitojun				&(((struct in6_aliasreq *)data)->ifra_addr);
72662587Sitojun			dst = (struct sockaddr *)
72762587Sitojun				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
72862587Sitojun			break;
72962587Sitojun#endif
73078064Sume		case SIOCSLIFPHYADDR:
73178064Sume			src = (struct sockaddr *)
73278064Sume				&(((struct if_laddrreq *)data)->addr);
73378064Sume			dst = (struct sockaddr *)
73478064Sume				&(((struct if_laddrreq *)data)->dstaddr);
735105293Sume			break;
73691327Sbrooks		default:
737105293Sume			return EINVAL;
73862587Sitojun		}
73954263Sshin
74078064Sume		/* sa_family must be equal */
74178064Sume		if (src->sa_family != dst->sa_family)
74278064Sume			return EINVAL;
74378064Sume
74478064Sume		/* validate sa_len */
74578064Sume		switch (src->sa_family) {
74678064Sume#ifdef INET
74778064Sume		case AF_INET:
74878064Sume			if (src->sa_len != sizeof(struct sockaddr_in))
74978064Sume				return EINVAL;
75078064Sume			break;
75178064Sume#endif
75278064Sume#ifdef INET6
75378064Sume		case AF_INET6:
75478064Sume			if (src->sa_len != sizeof(struct sockaddr_in6))
75578064Sume				return EINVAL;
75678064Sume			break;
75778064Sume#endif
75878064Sume		default:
75978064Sume			return EAFNOSUPPORT;
76078064Sume		}
76178064Sume		switch (dst->sa_family) {
76278064Sume#ifdef INET
76378064Sume		case AF_INET:
76478064Sume			if (dst->sa_len != sizeof(struct sockaddr_in))
76578064Sume				return EINVAL;
76678064Sume			break;
76778064Sume#endif
76878064Sume#ifdef INET6
76978064Sume		case AF_INET6:
77078064Sume			if (dst->sa_len != sizeof(struct sockaddr_in6))
77178064Sume				return EINVAL;
77278064Sume			break;
77378064Sume#endif
77478064Sume		default:
77578064Sume			return EAFNOSUPPORT;
77678064Sume		}
77778064Sume
77878064Sume		/* check sa_family looks sane for the cmd */
77978064Sume		switch (cmd) {
78078064Sume		case SIOCSIFPHYADDR:
78178064Sume			if (src->sa_family == AF_INET)
78278064Sume				break;
78378064Sume			return EAFNOSUPPORT;
78478064Sume#ifdef INET6
78578064Sume		case SIOCSIFPHYADDR_IN6:
78678064Sume			if (src->sa_family == AF_INET6)
78778064Sume				break;
78878064Sume			return EAFNOSUPPORT;
78978064Sume#endif /* INET6 */
79078064Sume		case SIOCSLIFPHYADDR:
79178064Sume			/* checks done in the above */
79278064Sume			break;
79378064Sume		}
79478064Sume
795147256Sbrooks		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
79662587Sitojun		break;
79762587Sitojun
79862587Sitojun#ifdef SIOCDIFPHYADDR
79962587Sitojun	case SIOCDIFPHYADDR:
800147256Sbrooks		gif_delete_tunnel(GIF2IFP(sc));
80154263Sshin		break;
80262587Sitojun#endif
80362587Sitojun
80454263Sshin	case SIOCGIFPSRCADDR:
80554263Sshin#ifdef INET6
80654263Sshin	case SIOCGIFPSRCADDR_IN6:
80754263Sshin#endif /* INET6 */
80854263Sshin		if (sc->gif_psrc == NULL) {
80954263Sshin			error = EADDRNOTAVAIL;
81054263Sshin			goto bad;
81154263Sshin		}
81254263Sshin		src = sc->gif_psrc;
81378064Sume		switch (cmd) {
81454263Sshin#ifdef INET
81578064Sume		case SIOCGIFPSRCADDR:
81654263Sshin			dst = &ifr->ifr_addr;
81778064Sume			size = sizeof(ifr->ifr_addr);
81854263Sshin			break;
81954263Sshin#endif /* INET */
82054263Sshin#ifdef INET6
82178064Sume		case SIOCGIFPSRCADDR_IN6:
82254263Sshin			dst = (struct sockaddr *)
82354263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
82478064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
82554263Sshin			break;
82654263Sshin#endif /* INET6 */
82754263Sshin		default:
82854263Sshin			error = EADDRNOTAVAIL;
82954263Sshin			goto bad;
83054263Sshin		}
83178064Sume		if (src->sa_len > size)
83278064Sume			return EINVAL;
83378064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
834148385Sume#ifdef INET6
835148385Sume		if (dst->sa_family == AF_INET6) {
836148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
837148385Sume			if (error != 0)
838148385Sume				return (error);
839148385Sume		}
840148385Sume#endif
84154263Sshin		break;
84262587Sitojun
84354263Sshin	case SIOCGIFPDSTADDR:
84454263Sshin#ifdef INET6
84554263Sshin	case SIOCGIFPDSTADDR_IN6:
84654263Sshin#endif /* INET6 */
84754263Sshin		if (sc->gif_pdst == NULL) {
84854263Sshin			error = EADDRNOTAVAIL;
84954263Sshin			goto bad;
85054263Sshin		}
85154263Sshin		src = sc->gif_pdst;
85278064Sume		switch (cmd) {
85354263Sshin#ifdef INET
85478064Sume		case SIOCGIFPDSTADDR:
85554263Sshin			dst = &ifr->ifr_addr;
85678064Sume			size = sizeof(ifr->ifr_addr);
85754263Sshin			break;
85854263Sshin#endif /* INET */
85954263Sshin#ifdef INET6
86078064Sume		case SIOCGIFPDSTADDR_IN6:
86154263Sshin			dst = (struct sockaddr *)
86254263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
86378064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
86454263Sshin			break;
86554263Sshin#endif /* INET6 */
86654263Sshin		default:
86754263Sshin			error = EADDRNOTAVAIL;
86854263Sshin			goto bad;
86954263Sshin		}
87078064Sume		if (src->sa_len > size)
87178064Sume			return EINVAL;
872219206Sbz		error = prison_if(curthread->td_ucred, src);
873219206Sbz		if (error != 0)
874219206Sbz			return (error);
875219206Sbz		error = prison_if(curthread->td_ucred, dst);
876219206Sbz		if (error != 0)
877219206Sbz			return (error);
87878064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
879148385Sume#ifdef INET6
880148385Sume		if (dst->sa_family == AF_INET6) {
881148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
882148385Sume			if (error != 0)
883148385Sume				return (error);
884148385Sume		}
885148385Sume#endif
88654263Sshin		break;
88754263Sshin
88878064Sume	case SIOCGLIFPHYADDR:
88978064Sume		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
89078064Sume			error = EADDRNOTAVAIL;
89178064Sume			goto bad;
89278064Sume		}
89378064Sume
89478064Sume		/* copy src */
89578064Sume		src = sc->gif_psrc;
89678064Sume		dst = (struct sockaddr *)
89778064Sume			&(((struct if_laddrreq *)data)->addr);
89878064Sume		size = sizeof(((struct if_laddrreq *)data)->addr);
89978064Sume		if (src->sa_len > size)
90078064Sume			return EINVAL;
90178064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
90278064Sume
90378064Sume		/* copy dst */
90478064Sume		src = sc->gif_pdst;
90578064Sume		dst = (struct sockaddr *)
90678064Sume			&(((struct if_laddrreq *)data)->dstaddr);
90778064Sume		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
90878064Sume		if (src->sa_len > size)
90978064Sume			return EINVAL;
91078064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
91178064Sume		break;
91278064Sume
91354263Sshin	case SIOCSIFFLAGS:
91462587Sitojun		/* if_ioctl() takes care of it */
91554263Sshin		break;
91654263Sshin
917193664Shrs	case GIFGOPTS:
918193664Shrs		options = sc->gif_options;
919193664Shrs		error = copyout(&options, ifr->ifr_data,
920193664Shrs				sizeof(options));
921193664Shrs		break;
922193664Shrs
923193664Shrs	case GIFSOPTS:
924193664Shrs		if ((error = priv_check(curthread, PRIV_NET_GIF)) != 0)
925193664Shrs			break;
926193815Shrs		error = copyin(ifr->ifr_data, &options, sizeof(options));
927193815Shrs		if (error)
928193815Shrs			break;
929193815Shrs		if (options & ~GIF_OPTMASK)
930193815Shrs			error = EINVAL;
931193815Shrs		else
932193815Shrs			sc->gif_options = options;
933193664Shrs		break;
934193664Shrs
93554263Sshin	default:
93654263Sshin		error = EINVAL;
93754263Sshin		break;
93854263Sshin	}
93954263Sshin bad:
94054263Sshin	return error;
94154263Sshin}
94279106Sbrooks
943127305Srwatson/*
944127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check
945127305Srwatson * if a given tunnel is already present happens before we perform a
946127305Srwatson * potentially blocking setup of the tunnel.  This code needs to be
947127305Srwatson * re-ordered so that the check and replacement can be atomic using
948127305Srwatson * a mutex.
949127305Srwatson */
950105293Sumeint
951105293Sumegif_set_tunnel(ifp, src, dst)
952105293Sume	struct ifnet *ifp;
953105293Sume	struct sockaddr *src;
954105293Sume	struct sockaddr *dst;
955105293Sume{
956147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
957105293Sume	struct gif_softc *sc2;
958105293Sume	struct sockaddr *osrc, *odst, *sa;
959105293Sume	int error = 0;
960105293Sume
961127305Srwatson	mtx_lock(&gif_mtx);
962181803Sbz	LIST_FOREACH(sc2, &V_gif_softc_list, gif_list) {
963105293Sume		if (sc2 == sc)
964105293Sume			continue;
965105293Sume		if (!sc2->gif_pdst || !sc2->gif_psrc)
966105293Sume			continue;
967105293Sume		if (sc2->gif_pdst->sa_family != dst->sa_family ||
968105293Sume		    sc2->gif_pdst->sa_len != dst->sa_len ||
969105293Sume		    sc2->gif_psrc->sa_family != src->sa_family ||
970105293Sume		    sc2->gif_psrc->sa_len != src->sa_len)
971105293Sume			continue;
972105293Sume
973105293Sume		/*
974105293Sume		 * Disallow parallel tunnels unless instructed
975105293Sume		 * otherwise.
976105293Sume		 */
977181803Sbz		if (!V_parallel_tunnels &&
978105293Sume		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
979105293Sume		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
980105293Sume			error = EADDRNOTAVAIL;
981127305Srwatson			mtx_unlock(&gif_mtx);
982105293Sume			goto bad;
983105293Sume		}
984105293Sume
985105293Sume		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
986105293Sume	}
987127305Srwatson	mtx_unlock(&gif_mtx);
988105293Sume
989105293Sume	/* XXX we can detach from both, but be polite just in case */
990105293Sume	if (sc->gif_psrc)
991105293Sume		switch (sc->gif_psrc->sa_family) {
992105293Sume#ifdef INET
993105293Sume		case AF_INET:
994105293Sume			(void)in_gif_detach(sc);
995105293Sume			break;
996105293Sume#endif
997105293Sume#ifdef INET6
998105293Sume		case AF_INET6:
999105293Sume			(void)in6_gif_detach(sc);
1000105293Sume			break;
1001105293Sume#endif
1002105293Sume		}
1003105293Sume
1004105293Sume	osrc = sc->gif_psrc;
1005111119Simp	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
1006105293Sume	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
1007105293Sume	sc->gif_psrc = sa;
1008105293Sume
1009105293Sume	odst = sc->gif_pdst;
1010111119Simp	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
1011105293Sume	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
1012105293Sume	sc->gif_pdst = sa;
1013105293Sume
1014105293Sume	switch (sc->gif_psrc->sa_family) {
1015105293Sume#ifdef INET
1016105293Sume	case AF_INET:
1017105293Sume		error = in_gif_attach(sc);
1018105293Sume		break;
1019105293Sume#endif
1020105293Sume#ifdef INET6
1021105293Sume	case AF_INET6:
1022148385Sume		/*
1023148385Sume		 * Check validity of the scope zone ID of the addresses, and
1024148385Sume		 * convert it into the kernel internal form if necessary.
1025148385Sume		 */
1026148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
1027148385Sume		if (error != 0)
1028148385Sume			break;
1029148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
1030148385Sume		if (error != 0)
1031148385Sume			break;
1032105293Sume		error = in6_gif_attach(sc);
1033105293Sume		break;
1034105293Sume#endif
1035105293Sume	}
1036105293Sume	if (error) {
1037105293Sume		/* rollback */
1038105293Sume		free((caddr_t)sc->gif_psrc, M_IFADDR);
1039105293Sume		free((caddr_t)sc->gif_pdst, M_IFADDR);
1040105293Sume		sc->gif_psrc = osrc;
1041105293Sume		sc->gif_pdst = odst;
1042105293Sume		goto bad;
1043105293Sume	}
1044105293Sume
1045105293Sume	if (osrc)
1046105293Sume		free((caddr_t)osrc, M_IFADDR);
1047105293Sume	if (odst)
1048105293Sume		free((caddr_t)odst, M_IFADDR);
1049105293Sume
1050105293Sume bad:
1051105293Sume	if (sc->gif_psrc && sc->gif_pdst)
1052148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1053105293Sume	else
1054148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1055105293Sume
1056105293Sume	return error;
1057105293Sume}
1058105293Sume
105979106Sbrooksvoid
1060105293Sumegif_delete_tunnel(ifp)
1061105293Sume	struct ifnet *ifp;
106279106Sbrooks{
1063147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
106479106Sbrooks
106579106Sbrooks	if (sc->gif_psrc) {
106679106Sbrooks		free((caddr_t)sc->gif_psrc, M_IFADDR);
106779106Sbrooks		sc->gif_psrc = NULL;
106879106Sbrooks	}
106979106Sbrooks	if (sc->gif_pdst) {
107079106Sbrooks		free((caddr_t)sc->gif_pdst, M_IFADDR);
107179106Sbrooks		sc->gif_pdst = NULL;
107279106Sbrooks	}
1073105293Sume	/* it is safe to detach from both */
1074105293Sume#ifdef INET
1075105293Sume	(void)in_gif_detach(sc);
1076105293Sume#endif
1077105293Sume#ifdef INET6
1078105293Sume	(void)in6_gif_detach(sc);
1079105293Sume#endif
1080160018Syar	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
108179106Sbrooks}
1082