if_gif.c revision 159174
162587Sitojun/*	$FreeBSD: head/sys/net/if_gif.c 159174 2006-06-02 14:10:52Z glebius $	*/
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"
35101739Srwatson#include "opt_mac.h"
3654263Sshin
3754263Sshin#include <sys/param.h>
3854263Sshin#include <sys/systm.h>
3954263Sshin#include <sys/kernel.h>
40101182Srwatson#include <sys/mac.h>
4154263Sshin#include <sys/malloc.h>
4254263Sshin#include <sys/mbuf.h>
43129880Sphk#include <sys/module.h>
4454263Sshin#include <sys/socket.h>
4554263Sshin#include <sys/sockio.h>
4654263Sshin#include <sys/errno.h>
4754263Sshin#include <sys/time.h>
4891270Sbrooks#include <sys/sysctl.h>
4954263Sshin#include <sys/syslog.h>
5062587Sitojun#include <sys/protosw.h>
5179106Sbrooks#include <sys/conf.h>
5254263Sshin#include <machine/cpu.h>
5354263Sshin
5454263Sshin#include <net/if.h>
55130933Sbrooks#include <net/if_clone.h>
5654263Sshin#include <net/if_types.h>
5754263Sshin#include <net/netisr.h>
5854263Sshin#include <net/route.h>
5954263Sshin#include <net/bpf.h>
6054263Sshin
6154263Sshin#include <netinet/in.h>
6254263Sshin#include <netinet/in_systm.h>
6378064Sume#include <netinet/ip.h>
6478064Sume#ifdef	INET
6554263Sshin#include <netinet/in_var.h>
6654263Sshin#include <netinet/in_gif.h>
6779106Sbrooks#include <netinet/ip_var.h>
6854263Sshin#endif	/* INET */
6954263Sshin
7054263Sshin#ifdef INET6
7154263Sshin#ifndef INET
7254263Sshin#include <netinet/in.h>
7354263Sshin#endif
7454263Sshin#include <netinet6/in6_var.h>
7554263Sshin#include <netinet/ip6.h>
7654263Sshin#include <netinet6/ip6_var.h>
77148385Sume#include <netinet6/scope6_var.h>
7854263Sshin#include <netinet6/in6_gif.h>
7962587Sitojun#include <netinet6/ip6protosw.h>
8054263Sshin#endif /* INET6 */
8154263Sshin
8262587Sitojun#include <netinet/ip_encap.h>
83153621Sthompsa#include <net/ethernet.h>
84153621Sthompsa#include <net/if_bridgevar.h>
8554263Sshin#include <net/if_gif.h>
8654263Sshin
8754263Sshin#include <net/net_osdep.h>
8854263Sshin
8979106Sbrooks#define GIFNAME		"gif"
9062587Sitojun
91127305Srwatson/*
92127898Sru * gif_mtx protects the global gif_softc_list.
93127305Srwatson */
94127305Srwatsonstatic struct mtx gif_mtx;
9579106Sbrooksstatic MALLOC_DEFINE(M_GIF, "gif", "Generic Tunnel Interface");
9689065Smsmithstatic LIST_HEAD(, gif_softc) gif_softc_list;
9779106Sbrooks
9883998Sbrooksvoid	(*ng_gif_input_p)(struct ifnet *ifp, struct mbuf **mp, int af);
9983998Sbrooksvoid	(*ng_gif_input_orphan_p)(struct ifnet *ifp, struct mbuf *m, int af);
10083998Sbrooksvoid	(*ng_gif_attach_p)(struct ifnet *ifp);
10183998Sbrooksvoid	(*ng_gif_detach_p)(struct ifnet *ifp);
10283998Sbrooks
103153621Sthompsastatic void	gif_start(struct ifnet *);
104128209Sbrooksstatic int	gif_clone_create(struct if_clone *, int);
105128209Sbrooksstatic void	gif_clone_destroy(struct ifnet *);
10679106Sbrooks
107130933SbrooksIFC_SIMPLE_DECLARE(gif, 0);
10879106Sbrooks
10992725Salfredstatic int gifmodevent(module_t, int, void *);
11079106Sbrooks
11191270SbrooksSYSCTL_DECL(_net_link);
11291270SbrooksSYSCTL_NODE(_net_link, IFT_GIF, gif, CTLFLAG_RW, 0,
11391270Sbrooks    "Generic Tunnel Interface");
11462587Sitojun#ifndef MAX_GIF_NEST
11562587Sitojun/*
11691270Sbrooks * This macro controls the default upper limitation on nesting of gif tunnels.
11762587Sitojun * Since, setting a large value to this macro with a careless configuration
11862587Sitojun * may introduce system crash, we don't allow any nestings by default.
11962587Sitojun * If you need to configure nested gif tunnels, you can define this macro
12095023Ssuz * in your kernel configuration file.  However, if you do so, please be
12162587Sitojun * careful to configure the tunnels so that it won't make a loop.
12262587Sitojun */
12362587Sitojun#define MAX_GIF_NEST 1
12462587Sitojun#endif
12562587Sitojunstatic int max_gif_nesting = MAX_GIF_NEST;
12691270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, max_nesting, CTLFLAG_RW,
12791270Sbrooks    &max_gif_nesting, 0, "Max nested tunnels");
12862587Sitojun
12991270Sbrooks/*
13091270Sbrooks * By default, we disallow creation of multiple tunnels between the same
13191270Sbrooks * pair of addresses.  Some applications require this functionality so
13291270Sbrooks * we allow control over this check here.
13391270Sbrooks */
13491270Sbrooks#ifdef XBONEHACK
13591270Sbrooksstatic int parallel_tunnels = 1;
13691270Sbrooks#else
13791270Sbrooksstatic int parallel_tunnels = 0;
13891270Sbrooks#endif
13991270SbrooksSYSCTL_INT(_net_link_gif, OID_AUTO, parallel_tunnels, CTLFLAG_RW,
14091270Sbrooks    &parallel_tunnels, 0, "Allow parallel tunnels?");
14191270Sbrooks
142128209Sbrooksstatic int
14379106Sbrooksgif_clone_create(ifc, unit)
14479106Sbrooks	struct if_clone *ifc;
14592081Smux	int unit;
14654263Sshin{
14778064Sume	struct gif_softc *sc;
14854263Sshin
149131672Sbms	sc = malloc(sizeof(struct gif_softc), M_GIF, M_WAITOK | M_ZERO);
150147256Sbrooks	GIF2IFP(sc) = if_alloc(IFT_GIF);
151147256Sbrooks	if (GIF2IFP(sc) == NULL) {
152147256Sbrooks		free(sc, M_GIF);
153147256Sbrooks		return (ENOSPC);
154147256Sbrooks	}
15579106Sbrooks
156155037Sglebius	GIF_LOCK_INIT(sc);
157155037Sglebius
158147256Sbrooks	GIF2IFP(sc)->if_softc = sc;
159147256Sbrooks	if_initname(GIF2IFP(sc), ifc->ifc_name, unit);
16079106Sbrooks
16179106Sbrooks	sc->encap_cookie4 = sc->encap_cookie6 = NULL;
16262587Sitojun
163147256Sbrooks	GIF2IFP(sc)->if_addrlen = 0;
164147256Sbrooks	GIF2IFP(sc)->if_mtu    = GIF_MTU;
165147256Sbrooks	GIF2IFP(sc)->if_flags  = IFF_POINTOPOINT | IFF_MULTICAST;
16678064Sume#if 0
16779106Sbrooks	/* turn off ingress filter */
168147256Sbrooks	GIF2IFP(sc)->if_flags  |= IFF_LINK2;
16978064Sume#endif
170147256Sbrooks	GIF2IFP(sc)->if_ioctl  = gif_ioctl;
171153621Sthompsa	GIF2IFP(sc)->if_start  = gif_start;
172147256Sbrooks	GIF2IFP(sc)->if_output = gif_output;
173147256Sbrooks	GIF2IFP(sc)->if_snd.ifq_maxlen = IFQ_MAXLEN;
174147256Sbrooks	if_attach(GIF2IFP(sc));
175147611Sdwmalone	bpfattach(GIF2IFP(sc), DLT_NULL, sizeof(u_int32_t));
17683998Sbrooks	if (ng_gif_attach_p != NULL)
177147256Sbrooks		(*ng_gif_attach_p)(GIF2IFP(sc));
178155037Sglebius
179155037Sglebius	mtx_lock(&gif_mtx);
180155037Sglebius	LIST_INSERT_HEAD(&gif_softc_list, sc, gif_list);
181155037Sglebius	mtx_unlock(&gif_mtx);
182155037Sglebius
183155037Sglebius	return (0);
18479106Sbrooks}
18579106Sbrooks
186127305Srwatsonstatic void
187151266Sthompsagif_clone_destroy(ifp)
188151266Sthompsa	struct ifnet *ifp;
18979106Sbrooks{
19079106Sbrooks	int err;
191151266Sthompsa	struct gif_softc *sc = ifp->if_softc;
19279106Sbrooks
193151266Sthompsa	mtx_lock(&gif_mtx);
194151266Sthompsa	LIST_REMOVE(sc, gif_list);
195151266Sthompsa	mtx_unlock(&gif_mtx);
196151266Sthompsa
197127305Srwatson	gif_delete_tunnel(ifp);
198105293Sume#ifdef INET6
199105293Sume	if (sc->encap_cookie6 != NULL) {
200105293Sume		err = encap_detach(sc->encap_cookie6);
201105293Sume		KASSERT(err == 0, ("Unexpected error detaching encap_cookie6"));
202105293Sume	}
203105293Sume#endif
204105293Sume#ifdef INET
20579106Sbrooks	if (sc->encap_cookie4 != NULL) {
20679106Sbrooks		err = encap_detach(sc->encap_cookie4);
20779106Sbrooks		KASSERT(err == 0, ("Unexpected error detaching encap_cookie4"));
20879106Sbrooks	}
209105293Sume#endif
21079106Sbrooks
21183998Sbrooks	if (ng_gif_detach_p != NULL)
21283998Sbrooks		(*ng_gif_detach_p)(ifp);
21379106Sbrooks	bpfdetach(ifp);
21479106Sbrooks	if_detach(ifp);
215147256Sbrooks	if_free(ifp);
21679106Sbrooks
217155037Sglebius	GIF_LOCK_DESTROY(sc);
218155037Sglebius
21979106Sbrooks	free(sc, M_GIF);
22079106Sbrooks}
22179106Sbrooks
22279106Sbrooksstatic int
22379106Sbrooksgifmodevent(mod, type, data)
22479106Sbrooks	module_t mod;
22579106Sbrooks	int type;
22679106Sbrooks	void *data;
22779106Sbrooks{
22879106Sbrooks
22979106Sbrooks	switch (type) {
23079106Sbrooks	case MOD_LOAD:
231127305Srwatson		mtx_init(&gif_mtx, "gif_mtx", NULL, MTX_DEF);
23283997Sbrooks		LIST_INIT(&gif_softc_list);
23379106Sbrooks		if_clone_attach(&gif_cloner);
23479106Sbrooks
23579106Sbrooks#ifdef INET6
23679106Sbrooks		ip6_gif_hlim = GIF_HLIM;
23762587Sitojun#endif
23879106Sbrooks
23979106Sbrooks		break;
24079106Sbrooks	case MOD_UNLOAD:
24179106Sbrooks		if_clone_detach(&gif_cloner);
242127305Srwatson		mtx_destroy(&gif_mtx);
24379106Sbrooks#ifdef INET6
24479106Sbrooks		ip6_gif_hlim = 0;
24562587Sitojun#endif
24679106Sbrooks		break;
247132199Sphk	default:
248132199Sphk		return EOPNOTSUPP;
24954263Sshin	}
25079106Sbrooks	return 0;
25154263Sshin}
25254263Sshin
25379106Sbrooksstatic moduledata_t gif_mod = {
25479106Sbrooks	"if_gif",
25579106Sbrooks	gifmodevent,
25679106Sbrooks	0
25779106Sbrooks};
25854263Sshin
25979106SbrooksDECLARE_MODULE(if_gif, gif_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
26083997SbrooksMODULE_VERSION(if_gif, 1);
26179106Sbrooks
262105293Sumeint
26362587Sitojungif_encapcheck(m, off, proto, arg)
26462587Sitojun	const struct mbuf *m;
26562587Sitojun	int off;
26662587Sitojun	int proto;
26762587Sitojun	void *arg;
26862587Sitojun{
26962587Sitojun	struct ip ip;
27062587Sitojun	struct gif_softc *sc;
27162587Sitojun
27262587Sitojun	sc = (struct gif_softc *)arg;
27362587Sitojun	if (sc == NULL)
27462587Sitojun		return 0;
27562587Sitojun
276147256Sbrooks	if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
27762587Sitojun		return 0;
27862587Sitojun
27962587Sitojun	/* no physical address */
28062587Sitojun	if (!sc->gif_psrc || !sc->gif_pdst)
28162587Sitojun		return 0;
28262587Sitojun
28362587Sitojun	switch (proto) {
28462587Sitojun#ifdef INET
28562587Sitojun	case IPPROTO_IPV4:
28662587Sitojun		break;
28762587Sitojun#endif
28862587Sitojun#ifdef INET6
28962587Sitojun	case IPPROTO_IPV6:
29062587Sitojun		break;
29162587Sitojun#endif
292153621Sthompsa	case IPPROTO_ETHERIP:
293153621Sthompsa		break;
294153621Sthompsa
29562587Sitojun	default:
29662587Sitojun		return 0;
29762587Sitojun	}
29862587Sitojun
299105339Sume	/* Bail on short packets */
300105339Sume	if (m->m_pkthdr.len < sizeof(ip))
301105339Sume		return 0;
302105339Sume
30391327Sbrooks	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
30462587Sitojun
30562587Sitojun	switch (ip.ip_v) {
30662587Sitojun#ifdef INET
30762587Sitojun	case 4:
30862587Sitojun		if (sc->gif_psrc->sa_family != AF_INET ||
30962587Sitojun		    sc->gif_pdst->sa_family != AF_INET)
31062587Sitojun			return 0;
31162587Sitojun		return gif_encapcheck4(m, off, proto, arg);
31262587Sitojun#endif
31362587Sitojun#ifdef INET6
31462587Sitojun	case 6:
315105293Sume		if (m->m_pkthdr.len < sizeof(struct ip6_hdr))
316105293Sume			return 0;
31762587Sitojun		if (sc->gif_psrc->sa_family != AF_INET6 ||
31862587Sitojun		    sc->gif_pdst->sa_family != AF_INET6)
31962587Sitojun			return 0;
32062587Sitojun		return gif_encapcheck6(m, off, proto, arg);
32162587Sitojun#endif
32262587Sitojun	default:
32362587Sitojun		return 0;
32462587Sitojun	}
32562587Sitojun}
32662587Sitojun
327153621Sthompsastatic void
328153621Sthompsagif_start(struct ifnet *ifp)
329153621Sthompsa{
330153621Sthompsa	struct gif_softc *sc;
331153621Sthompsa	struct mbuf *m;
332153621Sthompsa
333153621Sthompsa	sc = ifp->if_softc;
334153621Sthompsa
335153621Sthompsa	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
336153621Sthompsa	for (;;) {
337153621Sthompsa		IFQ_DEQUEUE(&ifp->if_snd, m);
338153621Sthompsa		if (m == 0)
339153621Sthompsa			break;
340153621Sthompsa
341153621Sthompsa		gif_output(ifp, m, sc->gif_pdst, NULL);
342153621Sthompsa
343153621Sthompsa	}
344153621Sthompsa	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
345153621Sthompsa
346153621Sthompsa	return;
347153621Sthompsa}
348153621Sthompsa
34954263Sshinint
35054263Sshingif_output(ifp, m, dst, rt)
35154263Sshin	struct ifnet *ifp;
35254263Sshin	struct mbuf *m;
35354263Sshin	struct sockaddr *dst;
35454263Sshin	struct rtentry *rt;	/* added in net2 */
35554263Sshin{
356147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
357127898Sru	struct m_tag *mtag;
35854263Sshin	int error = 0;
359127898Sru	int gif_called;
360147611Sdwmalone	u_int32_t af;
36154263Sshin
362101182Srwatson#ifdef MAC
363101182Srwatson	error = mac_check_ifnet_transmit(ifp, m);
364101739Srwatson	if (error) {
365101739Srwatson		m_freem(m);
366101739Srwatson		goto end;
367101739Srwatson	}
368101182Srwatson#endif
369101182Srwatson
37054263Sshin	/*
37154263Sshin	 * gif may cause infinite recursion calls when misconfigured.
372127898Sru	 * We'll prevent this by detecting loops.
373127898Sru	 *
374127898Sru	 * High nesting level may cause stack exhaustion.
37554263Sshin	 * We'll prevent this by introducing upper limit.
37654263Sshin	 */
377127898Sru	gif_called = 1;
378127898Sru	mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, NULL);
379127898Sru	while (mtag != NULL) {
380127898Sru		if (*(struct ifnet **)(mtag + 1) == ifp) {
381127898Sru			log(LOG_NOTICE,
382127898Sru			    "gif_output: loop detected on %s\n",
383127898Sru			    (*(struct ifnet **)(mtag + 1))->if_xname);
384127898Sru			m_freem(m);
385127898Sru			error = EIO;	/* is there better errno? */
386127898Sru			goto end;
387127898Sru		}
388127898Sru		mtag = m_tag_locate(m, MTAG_GIF, MTAG_GIF_CALLED, mtag);
389127898Sru		gif_called++;
390127898Sru	}
391127898Sru	if (gif_called > max_gif_nesting) {
39254263Sshin		log(LOG_NOTICE,
39354263Sshin		    "gif_output: recursively called too many times(%d)\n",
394127303Srwatson		    gif_called);
39554263Sshin		m_freem(m);
39654263Sshin		error = EIO;	/* is there better errno? */
39754263Sshin		goto end;
39854263Sshin	}
399127898Sru	mtag = m_tag_alloc(MTAG_GIF, MTAG_GIF_CALLED, sizeof(struct ifnet *),
400127898Sru	    M_NOWAIT);
401127898Sru	if (mtag == NULL) {
402127898Sru		m_freem(m);
403127898Sru		error = ENOMEM;
404127898Sru		goto end;
405127898Sru	}
406127898Sru	*(struct ifnet **)(mtag + 1) = ifp;
407127898Sru	m_tag_prepend(m, mtag);
40862587Sitojun
40954263Sshin	m->m_flags &= ~(M_BCAST|M_MCAST);
410155037Sglebius
411155037Sglebius	GIF_LOCK(sc);
412155037Sglebius
41354263Sshin	if (!(ifp->if_flags & IFF_UP) ||
41454263Sshin	    sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
415159174Sglebius		GIF_UNLOCK(sc);
41654263Sshin		m_freem(m);
41754263Sshin		error = ENETDOWN;
41854263Sshin		goto end;
41954263Sshin	}
42054263Sshin
421147611Sdwmalone	/* BPF writes need to be handled specially. */
422147611Sdwmalone	if (dst->sa_family == AF_UNSPEC) {
423147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
424147611Sdwmalone		dst->sa_family = af;
425147611Sdwmalone	}
426147611Sdwmalone
427153621Sthompsa	af = dst->sa_family;
42854263Sshin	if (ifp->if_bpf) {
429123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
43054263Sshin	}
43162587Sitojun	ifp->if_opackets++;
43254263Sshin	ifp->if_obytes += m->m_pkthdr.len;
43354263Sshin
434153621Sthompsa	/* override to IPPROTO_ETHERIP for bridged traffic */
435153621Sthompsa	if (ifp->if_bridge)
436153621Sthompsa		af = AF_LINK;
437153621Sthompsa
43878064Sume	/* inner AF-specific encapsulation */
43978064Sume
44062587Sitojun	/* XXX should we check if our outer source is legal? */
44162587Sitojun
44278064Sume	/* dispatch to output logic based on outer AF */
44354263Sshin	switch (sc->gif_psrc->sa_family) {
44454263Sshin#ifdef INET
44554263Sshin	case AF_INET:
446153621Sthompsa		error = in_gif_output(ifp, af, m);
44754263Sshin		break;
44854263Sshin#endif
44954263Sshin#ifdef INET6
45054263Sshin	case AF_INET6:
451153621Sthompsa		error = in6_gif_output(ifp, af, m);
45254263Sshin		break;
45354263Sshin#endif
45454263Sshin	default:
45562587Sitojun		m_freem(m);
45654263Sshin		error = ENETDOWN;
45754263Sshin	}
45854263Sshin
459159174Sglebius	GIF_UNLOCK(sc);
46054263Sshin  end:
46178064Sume	if (error)
46278064Sume		ifp->if_oerrors++;
463155037Sglebius	return (error);
46454263Sshin}
46554263Sshin
46654263Sshinvoid
467105338Sumegif_input(m, af, ifp)
46854263Sshin	struct mbuf *m;
46954263Sshin	int af;
470105338Sume	struct ifnet *ifp;
47154263Sshin{
472153621Sthompsa	int isr, n;
473153621Sthompsa	struct etherip_header *eip;
47454263Sshin
475105338Sume	if (ifp == NULL) {
47654263Sshin		/* just in case */
47754263Sshin		m_freem(m);
47854263Sshin		return;
47954263Sshin	}
48054263Sshin
481105338Sume	m->m_pkthdr.rcvif = ifp;
482101182Srwatson
483101182Srwatson#ifdef MAC
484105338Sume	mac_create_mbuf_from_ifnet(ifp, m);
485101182Srwatson#endif
486101182Srwatson
487105338Sume	if (ifp->if_bpf) {
48878064Sume		u_int32_t af1 = af;
489123922Ssam		bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
49054263Sshin	}
49154263Sshin
49283998Sbrooks	if (ng_gif_input_p != NULL) {
493105338Sume		(*ng_gif_input_p)(ifp, &m, af);
49483998Sbrooks		if (m == NULL)
49583998Sbrooks			return;
49683998Sbrooks	}
49783998Sbrooks
49854263Sshin	/*
49954263Sshin	 * Put the packet to the network layer input queue according to the
50054263Sshin	 * specified address family.
50154263Sshin	 * Note: older versions of gif_input directly called network layer
50295023Ssuz	 * input functions, e.g. ip6_input, here.  We changed the policy to
50354263Sshin	 * prevent too many recursive calls of such input functions, which
50495023Ssuz	 * might cause kernel panic.  But the change may introduce another
50554263Sshin	 * problem; if the input queue is full, packets are discarded.
50695023Ssuz	 * The kernel stack overflow really happened, and we believed
50795023Ssuz	 * queue-full rarely occurs, so we changed the policy.
50854263Sshin	 */
50954263Sshin	switch (af) {
51054263Sshin#ifdef INET
51154263Sshin	case AF_INET:
51254263Sshin		isr = NETISR_IP;
51354263Sshin		break;
51454263Sshin#endif
51554263Sshin#ifdef INET6
51654263Sshin	case AF_INET6:
51754263Sshin		isr = NETISR_IPV6;
51854263Sshin		break;
51954263Sshin#endif
520153621Sthompsa	case AF_LINK:
521153621Sthompsa		n = sizeof(struct etherip_header) + sizeof(struct ether_header);
522153621Sthompsa		if (n > m->m_len) {
523153621Sthompsa			m = m_pullup(m, n);
524153621Sthompsa			if (m == NULL) {
525153621Sthompsa				ifp->if_ierrors++;
526153621Sthompsa				return;
527153621Sthompsa			}
528153621Sthompsa		}
529153621Sthompsa
530153621Sthompsa		eip = mtod(m, struct etherip_header *);
531153621Sthompsa 		if (eip->eip_ver !=
532153621Sthompsa		    (ETHERIP_VERSION & ETHERIP_VER_VERS_MASK)) {
533153621Sthompsa			/* discard unknown versions */
534153621Sthompsa			m_freem(m);
535153621Sthompsa			return;
536153621Sthompsa		}
537153621Sthompsa		m_adj(m, sizeof(struct etherip_header));
538153621Sthompsa
539153621Sthompsa		m->m_flags &= ~(M_BCAST|M_MCAST);
540153621Sthompsa		m->m_pkthdr.rcvif = ifp;
541153621Sthompsa
542153621Sthompsa		if (ifp->if_bridge)
543153621Sthompsa			BRIDGE_INPUT(ifp, m);
544153621Sthompsa
545153621Sthompsa		if (m != NULL)
546153621Sthompsa			m_freem(m);
547153621Sthompsa		return;
548153621Sthompsa
54954263Sshin	default:
55083998Sbrooks		if (ng_gif_input_orphan_p != NULL)
551105338Sume			(*ng_gif_input_orphan_p)(ifp, m, af);
55283998Sbrooks		else
55383998Sbrooks			m_freem(m);
55454263Sshin		return;
55554263Sshin	}
55654263Sshin
557105338Sume	ifp->if_ipackets++;
558105338Sume	ifp->if_ibytes += m->m_pkthdr.len;
559111888Sjlemon	netisr_dispatch(isr, m);
56054263Sshin}
56154263Sshin
56262587Sitojun/* XXX how should we handle IPv6 scope on SIOC[GS]IFPHYADDR? */
56354263Sshinint
56454263Sshingif_ioctl(ifp, cmd, data)
56554263Sshin	struct ifnet *ifp;
56654263Sshin	u_long cmd;
56754263Sshin	caddr_t data;
56854263Sshin{
569147256Sbrooks	struct gif_softc *sc  = ifp->if_softc;
57054263Sshin	struct ifreq     *ifr = (struct ifreq*)data;
57154263Sshin	int error = 0, size;
57262587Sitojun	struct sockaddr *dst, *src;
573105339Sume#ifdef	SIOCSIFMTU /* xxx */
574105339Sume	u_long mtu;
575105339Sume#endif
576105339Sume
57754263Sshin	switch (cmd) {
57854263Sshin	case SIOCSIFADDR:
579105293Sume		ifp->if_flags |= IFF_UP;
58054263Sshin		break;
58162587Sitojun
58254263Sshin	case SIOCSIFDSTADDR:
58354263Sshin		break;
58454263Sshin
58554263Sshin	case SIOCADDMULTI:
58654263Sshin	case SIOCDELMULTI:
58754263Sshin		break;
58854263Sshin
58962587Sitojun#ifdef	SIOCSIFMTU /* xxx */
59054263Sshin	case SIOCGIFMTU:
59154263Sshin		break;
59262587Sitojun
59354263Sshin	case SIOCSIFMTU:
594105339Sume		mtu = ifr->ifr_mtu;
595105339Sume		if (mtu < GIF_MTU_MIN || mtu > GIF_MTU_MAX)
596105339Sume			return (EINVAL);
597105339Sume		ifp->if_mtu = mtu;
59854263Sshin		break;
59962587Sitojun#endif /* SIOCSIFMTU */
60054263Sshin
601105339Sume#ifdef INET
60254263Sshin	case SIOCSIFPHYADDR:
603105339Sume#endif
60454263Sshin#ifdef INET6
60554263Sshin	case SIOCSIFPHYADDR_IN6:
60654263Sshin#endif /* INET6 */
60778064Sume	case SIOCSLIFPHYADDR:
60862587Sitojun		switch (cmd) {
60978064Sume#ifdef INET
61062587Sitojun		case SIOCSIFPHYADDR:
61154263Sshin			src = (struct sockaddr *)
61254263Sshin				&(((struct in_aliasreq *)data)->ifra_addr);
61354263Sshin			dst = (struct sockaddr *)
61454263Sshin				&(((struct in_aliasreq *)data)->ifra_dstaddr);
61562587Sitojun			break;
61678064Sume#endif
61762587Sitojun#ifdef INET6
61862587Sitojun		case SIOCSIFPHYADDR_IN6:
61962587Sitojun			src = (struct sockaddr *)
62062587Sitojun				&(((struct in6_aliasreq *)data)->ifra_addr);
62162587Sitojun			dst = (struct sockaddr *)
62262587Sitojun				&(((struct in6_aliasreq *)data)->ifra_dstaddr);
62362587Sitojun			break;
62462587Sitojun#endif
62578064Sume		case SIOCSLIFPHYADDR:
62678064Sume			src = (struct sockaddr *)
62778064Sume				&(((struct if_laddrreq *)data)->addr);
62878064Sume			dst = (struct sockaddr *)
62978064Sume				&(((struct if_laddrreq *)data)->dstaddr);
630105293Sume			break;
63191327Sbrooks		default:
632105293Sume			return EINVAL;
63362587Sitojun		}
63454263Sshin
63578064Sume		/* sa_family must be equal */
63678064Sume		if (src->sa_family != dst->sa_family)
63778064Sume			return EINVAL;
63878064Sume
63978064Sume		/* validate sa_len */
64078064Sume		switch (src->sa_family) {
64178064Sume#ifdef INET
64278064Sume		case AF_INET:
64378064Sume			if (src->sa_len != sizeof(struct sockaddr_in))
64478064Sume				return EINVAL;
64578064Sume			break;
64678064Sume#endif
64778064Sume#ifdef INET6
64878064Sume		case AF_INET6:
64978064Sume			if (src->sa_len != sizeof(struct sockaddr_in6))
65078064Sume				return EINVAL;
65178064Sume			break;
65278064Sume#endif
65378064Sume		default:
65478064Sume			return EAFNOSUPPORT;
65578064Sume		}
65678064Sume		switch (dst->sa_family) {
65778064Sume#ifdef INET
65878064Sume		case AF_INET:
65978064Sume			if (dst->sa_len != sizeof(struct sockaddr_in))
66078064Sume				return EINVAL;
66178064Sume			break;
66278064Sume#endif
66378064Sume#ifdef INET6
66478064Sume		case AF_INET6:
66578064Sume			if (dst->sa_len != sizeof(struct sockaddr_in6))
66678064Sume				return EINVAL;
66778064Sume			break;
66878064Sume#endif
66978064Sume		default:
67078064Sume			return EAFNOSUPPORT;
67178064Sume		}
67278064Sume
67378064Sume		/* check sa_family looks sane for the cmd */
67478064Sume		switch (cmd) {
67578064Sume		case SIOCSIFPHYADDR:
67678064Sume			if (src->sa_family == AF_INET)
67778064Sume				break;
67878064Sume			return EAFNOSUPPORT;
67978064Sume#ifdef INET6
68078064Sume		case SIOCSIFPHYADDR_IN6:
68178064Sume			if (src->sa_family == AF_INET6)
68278064Sume				break;
68378064Sume			return EAFNOSUPPORT;
68478064Sume#endif /* INET6 */
68578064Sume		case SIOCSLIFPHYADDR:
68678064Sume			/* checks done in the above */
68778064Sume			break;
68878064Sume		}
68978064Sume
690147256Sbrooks		error = gif_set_tunnel(GIF2IFP(sc), src, dst);
69162587Sitojun		break;
69262587Sitojun
69362587Sitojun#ifdef SIOCDIFPHYADDR
69462587Sitojun	case SIOCDIFPHYADDR:
695147256Sbrooks		gif_delete_tunnel(GIF2IFP(sc));
69654263Sshin		break;
69762587Sitojun#endif
69862587Sitojun
69954263Sshin	case SIOCGIFPSRCADDR:
70054263Sshin#ifdef INET6
70154263Sshin	case SIOCGIFPSRCADDR_IN6:
70254263Sshin#endif /* INET6 */
70354263Sshin		if (sc->gif_psrc == NULL) {
70454263Sshin			error = EADDRNOTAVAIL;
70554263Sshin			goto bad;
70654263Sshin		}
70754263Sshin		src = sc->gif_psrc;
70878064Sume		switch (cmd) {
70954263Sshin#ifdef INET
71078064Sume		case SIOCGIFPSRCADDR:
71154263Sshin			dst = &ifr->ifr_addr;
71278064Sume			size = sizeof(ifr->ifr_addr);
71354263Sshin			break;
71454263Sshin#endif /* INET */
71554263Sshin#ifdef INET6
71678064Sume		case SIOCGIFPSRCADDR_IN6:
71754263Sshin			dst = (struct sockaddr *)
71854263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
71978064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
72054263Sshin			break;
72154263Sshin#endif /* INET6 */
72254263Sshin		default:
72354263Sshin			error = EADDRNOTAVAIL;
72454263Sshin			goto bad;
72554263Sshin		}
72678064Sume		if (src->sa_len > size)
72778064Sume			return EINVAL;
72878064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
729148385Sume#ifdef INET6
730148385Sume		if (dst->sa_family == AF_INET6) {
731148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
732148385Sume			if (error != 0)
733148385Sume				return (error);
734148385Sume		}
735148385Sume#endif
73654263Sshin		break;
73762587Sitojun
73854263Sshin	case SIOCGIFPDSTADDR:
73954263Sshin#ifdef INET6
74054263Sshin	case SIOCGIFPDSTADDR_IN6:
74154263Sshin#endif /* INET6 */
74254263Sshin		if (sc->gif_pdst == NULL) {
74354263Sshin			error = EADDRNOTAVAIL;
74454263Sshin			goto bad;
74554263Sshin		}
74654263Sshin		src = sc->gif_pdst;
74778064Sume		switch (cmd) {
74854263Sshin#ifdef INET
74978064Sume		case SIOCGIFPDSTADDR:
75054263Sshin			dst = &ifr->ifr_addr;
75178064Sume			size = sizeof(ifr->ifr_addr);
75254263Sshin			break;
75354263Sshin#endif /* INET */
75454263Sshin#ifdef INET6
75578064Sume		case SIOCGIFPDSTADDR_IN6:
75654263Sshin			dst = (struct sockaddr *)
75754263Sshin				&(((struct in6_ifreq *)data)->ifr_addr);
75878064Sume			size = sizeof(((struct in6_ifreq *)data)->ifr_addr);
75954263Sshin			break;
76054263Sshin#endif /* INET6 */
76154263Sshin		default:
76254263Sshin			error = EADDRNOTAVAIL;
76354263Sshin			goto bad;
76454263Sshin		}
76578064Sume		if (src->sa_len > size)
76678064Sume			return EINVAL;
76778064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
768148385Sume#ifdef INET6
769148385Sume		if (dst->sa_family == AF_INET6) {
770148385Sume			error = sa6_recoverscope((struct sockaddr_in6 *)dst);
771148385Sume			if (error != 0)
772148385Sume				return (error);
773148385Sume		}
774148385Sume#endif
77554263Sshin		break;
77654263Sshin
77778064Sume	case SIOCGLIFPHYADDR:
77878064Sume		if (sc->gif_psrc == NULL || sc->gif_pdst == NULL) {
77978064Sume			error = EADDRNOTAVAIL;
78078064Sume			goto bad;
78178064Sume		}
78278064Sume
78378064Sume		/* copy src */
78478064Sume		src = sc->gif_psrc;
78578064Sume		dst = (struct sockaddr *)
78678064Sume			&(((struct if_laddrreq *)data)->addr);
78778064Sume		size = sizeof(((struct if_laddrreq *)data)->addr);
78878064Sume		if (src->sa_len > size)
78978064Sume			return EINVAL;
79078064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
79178064Sume
79278064Sume		/* copy dst */
79378064Sume		src = sc->gif_pdst;
79478064Sume		dst = (struct sockaddr *)
79578064Sume			&(((struct if_laddrreq *)data)->dstaddr);
79678064Sume		size = sizeof(((struct if_laddrreq *)data)->dstaddr);
79778064Sume		if (src->sa_len > size)
79878064Sume			return EINVAL;
79978064Sume		bcopy((caddr_t)src, (caddr_t)dst, src->sa_len);
80078064Sume		break;
80178064Sume
80254263Sshin	case SIOCSIFFLAGS:
80362587Sitojun		/* if_ioctl() takes care of it */
80454263Sshin		break;
80554263Sshin
80654263Sshin	default:
80754263Sshin		error = EINVAL;
80854263Sshin		break;
80954263Sshin	}
81054263Sshin bad:
81154263Sshin	return error;
81254263Sshin}
81379106Sbrooks
814127305Srwatson/*
815127305Srwatson * XXXRW: There's a general event-ordering issue here: the code to check
816127305Srwatson * if a given tunnel is already present happens before we perform a
817127305Srwatson * potentially blocking setup of the tunnel.  This code needs to be
818127305Srwatson * re-ordered so that the check and replacement can be atomic using
819127305Srwatson * a mutex.
820127305Srwatson */
821105293Sumeint
822105293Sumegif_set_tunnel(ifp, src, dst)
823105293Sume	struct ifnet *ifp;
824105293Sume	struct sockaddr *src;
825105293Sume	struct sockaddr *dst;
826105293Sume{
827147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
828105293Sume	struct gif_softc *sc2;
829105293Sume	struct sockaddr *osrc, *odst, *sa;
830105293Sume	int error = 0;
831105293Sume
832127305Srwatson	mtx_lock(&gif_mtx);
833105293Sume	LIST_FOREACH(sc2, &gif_softc_list, gif_list) {
834105293Sume		if (sc2 == sc)
835105293Sume			continue;
836105293Sume		if (!sc2->gif_pdst || !sc2->gif_psrc)
837105293Sume			continue;
838105293Sume		if (sc2->gif_pdst->sa_family != dst->sa_family ||
839105293Sume		    sc2->gif_pdst->sa_len != dst->sa_len ||
840105293Sume		    sc2->gif_psrc->sa_family != src->sa_family ||
841105293Sume		    sc2->gif_psrc->sa_len != src->sa_len)
842105293Sume			continue;
843105293Sume
844105293Sume		/*
845105293Sume		 * Disallow parallel tunnels unless instructed
846105293Sume		 * otherwise.
847105293Sume		 */
848105293Sume		if (!parallel_tunnels &&
849105293Sume		    bcmp(sc2->gif_pdst, dst, dst->sa_len) == 0 &&
850105293Sume		    bcmp(sc2->gif_psrc, src, src->sa_len) == 0) {
851105293Sume			error = EADDRNOTAVAIL;
852127305Srwatson			mtx_unlock(&gif_mtx);
853105293Sume			goto bad;
854105293Sume		}
855105293Sume
856105293Sume		/* XXX both end must be valid? (I mean, not 0.0.0.0) */
857105293Sume	}
858127305Srwatson	mtx_unlock(&gif_mtx);
859105293Sume
860105293Sume	/* XXX we can detach from both, but be polite just in case */
861105293Sume	if (sc->gif_psrc)
862105293Sume		switch (sc->gif_psrc->sa_family) {
863105293Sume#ifdef INET
864105293Sume		case AF_INET:
865105293Sume			(void)in_gif_detach(sc);
866105293Sume			break;
867105293Sume#endif
868105293Sume#ifdef INET6
869105293Sume		case AF_INET6:
870105293Sume			(void)in6_gif_detach(sc);
871105293Sume			break;
872105293Sume#endif
873105293Sume		}
874105293Sume
875105293Sume	osrc = sc->gif_psrc;
876111119Simp	sa = (struct sockaddr *)malloc(src->sa_len, M_IFADDR, M_WAITOK);
877105293Sume	bcopy((caddr_t)src, (caddr_t)sa, src->sa_len);
878105293Sume	sc->gif_psrc = sa;
879105293Sume
880105293Sume	odst = sc->gif_pdst;
881111119Simp	sa = (struct sockaddr *)malloc(dst->sa_len, M_IFADDR, M_WAITOK);
882105293Sume	bcopy((caddr_t)dst, (caddr_t)sa, dst->sa_len);
883105293Sume	sc->gif_pdst = sa;
884105293Sume
885105293Sume	switch (sc->gif_psrc->sa_family) {
886105293Sume#ifdef INET
887105293Sume	case AF_INET:
888105293Sume		error = in_gif_attach(sc);
889105293Sume		break;
890105293Sume#endif
891105293Sume#ifdef INET6
892105293Sume	case AF_INET6:
893148385Sume		/*
894148385Sume		 * Check validity of the scope zone ID of the addresses, and
895148385Sume		 * convert it into the kernel internal form if necessary.
896148385Sume		 */
897148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_psrc, 0);
898148385Sume		if (error != 0)
899148385Sume			break;
900148385Sume		error = sa6_embedscope((struct sockaddr_in6 *)sc->gif_pdst, 0);
901148385Sume		if (error != 0)
902148385Sume			break;
903105293Sume		error = in6_gif_attach(sc);
904105293Sume		break;
905105293Sume#endif
906105293Sume	}
907105293Sume	if (error) {
908105293Sume		/* rollback */
909105293Sume		free((caddr_t)sc->gif_psrc, M_IFADDR);
910105293Sume		free((caddr_t)sc->gif_pdst, M_IFADDR);
911105293Sume		sc->gif_psrc = osrc;
912105293Sume		sc->gif_pdst = odst;
913105293Sume		goto bad;
914105293Sume	}
915105293Sume
916105293Sume	if (osrc)
917105293Sume		free((caddr_t)osrc, M_IFADDR);
918105293Sume	if (odst)
919105293Sume		free((caddr_t)odst, M_IFADDR);
920105293Sume
921105293Sume	if (sc->gif_psrc && sc->gif_pdst)
922148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
923105293Sume	else
924148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
925105293Sume
926105293Sume	return 0;
927105293Sume
928105293Sume bad:
929105293Sume	if (sc->gif_psrc && sc->gif_pdst)
930148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
931105293Sume	else
932148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
933105293Sume
934105293Sume	return error;
935105293Sume}
936105293Sume
93779106Sbrooksvoid
938105293Sumegif_delete_tunnel(ifp)
939105293Sume	struct ifnet *ifp;
94079106Sbrooks{
941147256Sbrooks	struct gif_softc *sc = ifp->if_softc;
94279106Sbrooks
94379106Sbrooks	if (sc->gif_psrc) {
94479106Sbrooks		free((caddr_t)sc->gif_psrc, M_IFADDR);
94579106Sbrooks		sc->gif_psrc = NULL;
94679106Sbrooks	}
94779106Sbrooks	if (sc->gif_pdst) {
94879106Sbrooks		free((caddr_t)sc->gif_pdst, M_IFADDR);
94979106Sbrooks		sc->gif_pdst = NULL;
95079106Sbrooks	}
951105293Sume	/* it is safe to detach from both */
952105293Sume#ifdef INET
953105293Sume	(void)in_gif_detach(sc);
954105293Sume#endif
955105293Sume#ifdef INET6
956105293Sume	(void)in6_gif_detach(sc);
957105293Sume#endif
958105293Sume
959105293Sume	if (sc->gif_psrc && sc->gif_pdst)
960148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
961105293Sume	else
962148887Srwatson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
96379106Sbrooks}
964