if_faith.c revision 89065
178064Sume/*	$KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $	*/
278064Sume
354263Sshin/*
454263Sshin * Copyright (c) 1982, 1986, 1993
554263Sshin *	The Regents of the University of California.  All rights reserved.
654263Sshin *
754263Sshin * Redistribution and use in source and binary forms, with or without
854263Sshin * modification, are permitted provided that the following conditions
954263Sshin * are met:
1054263Sshin * 1. Redistributions of source code must retain the above copyright
1154263Sshin *    notice, this list of conditions and the following disclaimer.
1254263Sshin * 2. Redistributions in binary form must reproduce the above copyright
1354263Sshin *    notice, this list of conditions and the following disclaimer in the
1454263Sshin *    documentation and/or other materials provided with the distribution.
1554263Sshin * 3. All advertising materials mentioning features or use of this software
1654263Sshin *    must display the following acknowledgement:
1754263Sshin *	This product includes software developed by the University of
1854263Sshin *	California, Berkeley and its contributors.
1954263Sshin * 4. Neither the name of the University nor the names of its contributors
2054263Sshin *    may be used to endorse or promote products derived from this software
2154263Sshin *    without specific prior written permission.
2254263Sshin *
2354263Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2454263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2554263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2654263Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2754263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2854263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2954263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3054263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3154263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3254263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3354263Sshin * SUCH DAMAGE.
3454263Sshin *
3554263Sshin * $FreeBSD: head/sys/net/if_faith.c 89065 2002-01-08 10:30:09Z msmith $
3654263Sshin */
3754263Sshin/*
3854263Sshin * derived from
3954263Sshin *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
4054263Sshin * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
4154263Sshin */
4254263Sshin
4354263Sshin/*
4454263Sshin * Loopback interface driver for protocol testing and timing.
4554263Sshin */
4678064Sume#include "opt_inet.h"
4778064Sume#include "opt_inet6.h"
4854263Sshin
4954263Sshin#include <sys/param.h>
5054263Sshin#include <sys/systm.h>
5154263Sshin#include <sys/kernel.h>
5254263Sshin#include <sys/mbuf.h>
5354263Sshin#include <sys/socket.h>
5478064Sume#include <sys/errno.h>
5554263Sshin#include <sys/sockio.h>
5678064Sume#include <sys/time.h>
5778064Sume#include <sys/queue.h>
5883934Sbrooks#include <sys/types.h>
5983934Sbrooks#include <sys/malloc.h>
6083934Sbrooks#include <machine/bus.h>	/* XXX: Shouldn't really be required! */
6183934Sbrooks#include <sys/rman.h>
6254263Sshin
6354263Sshin#include <net/if.h>
6454263Sshin#include <net/if_types.h>
6554263Sshin#include <net/netisr.h>
6654263Sshin#include <net/route.h>
6754263Sshin#include <net/bpf.h>
6854263Sshin
6978064Sume#ifdef	INET
7078064Sume#include <netinet/in.h>
7178064Sume#include <netinet/in_systm.h>
7278064Sume#include <netinet/in_var.h>
7378064Sume#include <netinet/ip.h>
7478064Sume#endif
7578064Sume
7678064Sume#ifdef INET6
7778064Sume#ifndef INET
7878064Sume#include <netinet/in.h>
7978064Sume#endif
8078064Sume#include <netinet6/in6_var.h>
8178064Sume#include <netinet/ip6.h>
8278064Sume#include <netinet6/ip6_var.h>
8378064Sume#endif
8478064Sume
8571991Speter#include <net/net_osdep.h>
8671991Speter
8783934Sbrooks#define FAITHNAME	"faith"
8883934Sbrooks#define FAITH_MAXUNIT	0x7fff	/* ifp->if_unit is only 15 bits */
8983934Sbrooks
9083934Sbrooksstruct faith_softc {
9183934Sbrooks	struct ifnet sc_if;	/* must be first */
9283934Sbrooks	struct resource *r_unit;
9383934Sbrooks	LIST_ENTRY(faith_softc) sc_list;
9483934Sbrooks};
9583934Sbrooks
9678064Sumestatic int faithioctl __P((struct ifnet *, u_long, caddr_t));
9778064Sumeint faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
9878064Sume	struct rtentry *));
9985074Srustatic void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
10083934Sbrooksstatic int faithprefix __P((struct in6_addr *));
10154263Sshin
10283934Sbrooksstatic int faithmodevent __P((module_t, int, void *));
10378064Sume
10483934Sbrooksstatic MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
10583934Sbrooksstatic struct rman faithunits[1];
10689065Smsmithstatic LIST_HEAD(, faith_softc) faith_softc_list;
10754263Sshin
10883934Sbrooksint	faith_clone_create __P((struct if_clone *, int *));
10983934Sbrooksvoid	faith_clone_destroy __P((struct ifnet *));
11083934Sbrooks
11183934Sbrooksstruct if_clone faith_cloner =
11283934Sbrooks    IF_CLONE_INITIALIZER(FAITHNAME, faith_clone_create, faith_clone_destroy);
11383934Sbrooks
11454263Sshin#define	FAITHMTU	1500
11554263Sshin
11683934Sbrooksstatic int
11783934Sbrooksfaithmodevent(mod, type, data)
11883934Sbrooks	module_t mod;
11983934Sbrooks	int type;
12083934Sbrooks	void *data;
12154263Sshin{
12283934Sbrooks	int err;
12354263Sshin
12483934Sbrooks	switch (type) {
12583934Sbrooks	case MOD_LOAD:
12683934Sbrooks		faithunits->rm_type = RMAN_ARRAY;
12783934Sbrooks		faithunits->rm_descr = "configurable if_faith units";
12883934Sbrooks		err = rman_init(faithunits);
12983934Sbrooks		if (err != 0)
13083934Sbrooks			return (err);
13183934Sbrooks		err = rman_manage_region(faithunits, 0, FAITH_MAXUNIT);
13283934Sbrooks		if (err != 0) {
13383934Sbrooks			printf("%s: faithunits: rman_manage_region: "
13483934Sbrooks			    "Failed %d\n", FAITHNAME, err);
13583934Sbrooks			rman_fini(faithunits);
13683934Sbrooks			return (err);
13783934Sbrooks		}
13883934Sbrooks		LIST_INIT(&faith_softc_list);
13983934Sbrooks		if_clone_attach(&faith_cloner);
14083934Sbrooks
14183934Sbrooks#ifdef INET6
14283934Sbrooks		faithprefix_p = faithprefix;
14378064Sume#endif
14483934Sbrooks
14583934Sbrooks		break;
14683934Sbrooks	case MOD_UNLOAD:
14783934Sbrooks#ifdef INET6
14883934Sbrooks		faithprefix_p = NULL;
14978064Sume#endif
15083934Sbrooks
15183934Sbrooks		if_clone_detach(&faith_cloner);
15283934Sbrooks
15383934Sbrooks		while (!LIST_EMPTY(&faith_softc_list))
15483934Sbrooks			faith_clone_destroy(
15583934Sbrooks			    &LIST_FIRST(&faith_softc_list)->sc_if);
15683934Sbrooks
15783934Sbrooks		err = rman_fini(faithunits);
15883934Sbrooks		if (err != 0)
15983934Sbrooks			return (err);
16083934Sbrooks
16183934Sbrooks		break;
16254263Sshin	}
16383934Sbrooks	return 0;
16454263Sshin}
16578064Sume
16683934Sbrooksstatic moduledata_t faith_mod = {
16783934Sbrooks	"if_faith",
16883934Sbrooks	faithmodevent,
16983934Sbrooks	0
17083934Sbrooks};
17183934Sbrooks
17283934SbrooksDECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
17383934SbrooksMODULE_VERSION(if_faith, 1);
17483934Sbrooks
17578064Sumeint
17683934Sbrooksfaith_clone_create(ifc, unit)
17783934Sbrooks	struct if_clone *ifc;
17883934Sbrooks	int *unit;
17983934Sbrooks{
18083934Sbrooks	struct resource *r;
18183934Sbrooks	struct faith_softc *sc;
18283934Sbrooks
18383934Sbrooks	if (*unit > FAITH_MAXUNIT)
18483934Sbrooks		return (ENXIO);
18583934Sbrooks
18683934Sbrooks	if (*unit < 0) {
18783934Sbrooks		r = rman_reserve_resource(faithunits, 0, FAITH_MAXUNIT, 1,
18883934Sbrooks		    RF_ALLOCATED | RF_ACTIVE, NULL);
18983934Sbrooks		if (r == NULL)
19083934Sbrooks			return (ENOSPC);
19183934Sbrooks		*unit = rman_get_start(r);
19283934Sbrooks	} else {
19383934Sbrooks		r = rman_reserve_resource(faithunits, *unit, *unit, 1,
19483934Sbrooks		    RF_ALLOCATED | RF_ACTIVE, NULL);
19583934Sbrooks		if (r == NULL)
19683934Sbrooks			return (ENOSPC);
19783934Sbrooks	}
19883934Sbrooks
19983934Sbrooks	sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK);
20083934Sbrooks	bzero(sc, sizeof(struct faith_softc));
20183934Sbrooks
20283934Sbrooks	sc->sc_if.if_softc = sc;
20383934Sbrooks	sc->sc_if.if_name = FAITHNAME;
20483934Sbrooks	sc->sc_if.if_unit = *unit;
20583934Sbrooks	sc->r_unit = r;
20683934Sbrooks
20783934Sbrooks	sc->sc_if.if_mtu = FAITHMTU;
20883934Sbrooks	/* Change to BROADCAST experimentaly to announce its prefix. */
20983934Sbrooks	sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
21083934Sbrooks	sc->sc_if.if_ioctl = faithioctl;
21183934Sbrooks	sc->sc_if.if_output = faithoutput;
21283934Sbrooks	sc->sc_if.if_type = IFT_FAITH;
21383934Sbrooks	sc->sc_if.if_hdrlen = 0;
21483934Sbrooks	sc->sc_if.if_addrlen = 0;
21588034Sbrooks	sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
21683934Sbrooks	if_attach(&sc->sc_if);
21783934Sbrooks	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
21883934Sbrooks	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
21983934Sbrooks	return (0);
22083934Sbrooks}
22183934Sbrooks
22283934Sbrooksvoid
22383934Sbrooksfaith_clone_destroy(ifp)
22483934Sbrooks	struct ifnet *ifp;
22583934Sbrooks{
22683934Sbrooks	int err;
22783934Sbrooks	struct faith_softc *sc = (void *) ifp;
22883934Sbrooks
22983934Sbrooks	LIST_REMOVE(sc, sc_list);
23083934Sbrooks	bpfdetach(ifp);
23183934Sbrooks	if_detach(ifp);
23283934Sbrooks
23383934Sbrooks	err = rman_release_resource(sc->r_unit);
23483934Sbrooks	KASSERT(err == 0, ("Unexpected error freeing resource"));
23583934Sbrooks
23683934Sbrooks	free(sc, M_FAITH);
23783934Sbrooks}
23883934Sbrooks
23983934Sbrooksint
24078064Sumefaithoutput(ifp, m, dst, rt)
24178064Sume	struct ifnet *ifp;
24278064Sume	struct mbuf *m;
24378064Sume	struct sockaddr *dst;
24478064Sume	struct rtentry *rt;
24578064Sume{
24678064Sume	int isr;
24778064Sume	struct ifqueue *ifq = 0;
24878064Sume
24978064Sume	if ((m->m_flags & M_PKTHDR) == 0)
25078064Sume		panic("faithoutput no HDR");
25183934Sbrooks
25278064Sume	/* BPF write needs to be handled specially */
25378064Sume	if (dst->sa_family == AF_UNSPEC) {
25478064Sume		dst->sa_family = *(mtod(m, int *));
25578064Sume		m->m_len -= sizeof(int);
25678064Sume		m->m_pkthdr.len -= sizeof(int);
25778064Sume		m->m_data += sizeof(int);
25878064Sume	}
25978064Sume
26078064Sume	if (ifp->if_bpf) {
26178064Sume		/*
26278064Sume		 * We need to prepend the address family as
26378064Sume		 * a four byte field.  Cons up a faith header
26478064Sume		 * to pacify bpf.  This is safe because bpf
26578064Sume		 * will only read from the mbuf (i.e., it won't
26678064Sume		 * try to free it or keep a pointer a to it).
26778064Sume		 */
26878064Sume		struct mbuf m0;
26978064Sume		u_int32_t af = dst->sa_family;
27078064Sume
27178064Sume		m0.m_next = m;
27278064Sume		m0.m_len = 4;
27378064Sume		m0.m_data = (char *)&af;
27478064Sume
27578064Sume		bpf_mtap(ifp, &m0);
27678064Sume	}
27778064Sume
27878064Sume	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
27978064Sume		m_freem(m);
28078064Sume		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
28178064Sume		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
28278064Sume	}
28378064Sume	ifp->if_opackets++;
28478064Sume	ifp->if_obytes += m->m_pkthdr.len;
28578064Sume	switch (dst->sa_family) {
28678064Sume#ifdef INET
28778064Sume	case AF_INET:
28878064Sume		ifq = &ipintrq;
28978064Sume		isr = NETISR_IP;
29078064Sume		break;
29178064Sume#endif
29278064Sume#ifdef INET6
29378064Sume	case AF_INET6:
29478064Sume		ifq = &ip6intrq;
29578064Sume		isr = NETISR_IPV6;
29678064Sume		break;
29778064Sume#endif
29878064Sume	default:
29978064Sume		m_freem(m);
30078064Sume		return EAFNOSUPPORT;
30178064Sume	}
30278064Sume
30378064Sume	/* XXX do we need more sanity checks? */
30478064Sume
30578064Sume	m->m_pkthdr.rcvif = ifp;
30678064Sume	ifp->if_ipackets++;
30778064Sume	ifp->if_ibytes += m->m_pkthdr.len;
30878064Sume	(void) IF_HANDOFF(ifq, m, NULL);
30978064Sume	schednetisr(isr);
31078064Sume	return (0);
31178064Sume}
31278064Sume
31378064Sume/* ARGSUSED */
31478064Sumestatic void
31585074Srufaithrtrequest(cmd, rt, info)
31678064Sume	int cmd;
31778064Sume	struct rtentry *rt;
31885074Sru	struct rt_addrinfo *info;
31978064Sume{
32078064Sume	if (rt) {
32178064Sume		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
32278064Sume		/*
32378064Sume		 * For optimal performance, the send and receive buffers
32478064Sume		 * should be at least twice the MTU plus a little more for
32578064Sume		 * overhead.
32678064Sume		 */
32778064Sume		rt->rt_rmx.rmx_recvpipe =
32878064Sume			rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
32978064Sume	}
33078064Sume}
33178064Sume
33278064Sume/*
33378064Sume * Process an ioctl request.
33478064Sume */
33578064Sume/* ARGSUSED */
33678064Sumestatic int
33778064Sumefaithioctl(ifp, cmd, data)
33878064Sume	struct ifnet *ifp;
33978064Sume	u_long cmd;
34078064Sume	caddr_t data;
34178064Sume{
34278064Sume	struct ifaddr *ifa;
34378064Sume	struct ifreq *ifr = (struct ifreq *)data;
34478064Sume	int error = 0;
34578064Sume
34678064Sume	switch (cmd) {
34778064Sume
34878064Sume	case SIOCSIFADDR:
34978064Sume		ifp->if_flags |= IFF_UP | IFF_RUNNING;
35078064Sume		ifa = (struct ifaddr *)data;
35178064Sume		ifa->ifa_rtrequest = faithrtrequest;
35278064Sume		/*
35378064Sume		 * Everything else is done at a higher level.
35478064Sume		 */
35578064Sume		break;
35678064Sume
35778064Sume	case SIOCADDMULTI:
35878064Sume	case SIOCDELMULTI:
35978064Sume		if (ifr == 0) {
36078064Sume			error = EAFNOSUPPORT;		/* XXX */
36178064Sume			break;
36278064Sume		}
36378064Sume		switch (ifr->ifr_addr.sa_family) {
36478064Sume#ifdef INET
36578064Sume		case AF_INET:
36678064Sume			break;
36778064Sume#endif
36878064Sume#ifdef INET6
36978064Sume		case AF_INET6:
37078064Sume			break;
37178064Sume#endif
37278064Sume
37378064Sume		default:
37478064Sume			error = EAFNOSUPPORT;
37578064Sume			break;
37678064Sume		}
37778064Sume		break;
37878064Sume
37978064Sume#ifdef SIOCSIFMTU
38078064Sume	case SIOCSIFMTU:
38178064Sume		ifp->if_mtu = ifr->ifr_mtu;
38278064Sume		break;
38378064Sume#endif
38478064Sume
38578064Sume	case SIOCSIFFLAGS:
38678064Sume		break;
38778064Sume
38878064Sume	default:
38978064Sume		error = EINVAL;
39078064Sume	}
39178064Sume	return (error);
39278064Sume}
39378064Sume
39479326Sume#ifdef INET6
39578064Sume/*
39678064Sume * XXX could be slow
39778064Sume * XXX could be layer violation to call sys/net from sys/netinet6
39878064Sume */
39983934Sbrooksstatic int
40078064Sumefaithprefix(in6)
40178064Sume	struct in6_addr *in6;
40278064Sume{
40378064Sume	struct rtentry *rt;
40478064Sume	struct sockaddr_in6 sin6;
40578064Sume	int ret;
40678064Sume
40778064Sume	if (ip6_keepfaith == 0)
40878064Sume		return 0;
40978064Sume
41078064Sume	bzero(&sin6, sizeof(sin6));
41178064Sume	sin6.sin6_family = AF_INET6;
41278064Sume	sin6.sin6_len = sizeof(struct sockaddr_in6);
41378064Sume	sin6.sin6_addr = *in6;
41478064Sume	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
41578064Sume	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
41678064Sume	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
41778064Sume		ret = 1;
41878064Sume	else
41978064Sume		ret = 0;
42078064Sume	if (rt)
42178064Sume		RTFREE(rt);
42278064Sume	return ret;
42378064Sume}
42479326Sume#endif
425