if_faith.c revision 151266
195023Ssuz/*	$KAME: if_faith.c,v 1.23 2001/12/17 13:55:29 sumikawa Exp $	*/
278064Sume
3139823Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
1654263Sshin *    may be used to endorse or promote products derived from this software
1754263Sshin *    without specific prior written permission.
1854263Sshin *
1954263Sshin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2054263Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2154263Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2254263Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2354263Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2454263Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2554263Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2654263Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2754263Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2854263Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2954263Sshin * SUCH DAMAGE.
3054263Sshin *
3154263Sshin * $FreeBSD: head/sys/net/if_faith.c 151266 2005-10-12 19:52:16Z thompsa $
3254263Sshin */
3354263Sshin/*
3454263Sshin * derived from
3554263Sshin *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
3654263Sshin * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
3754263Sshin */
3854263Sshin
3954263Sshin/*
4054263Sshin * Loopback interface driver for protocol testing and timing.
4154263Sshin */
4278064Sume#include "opt_inet.h"
4378064Sume#include "opt_inet6.h"
4454263Sshin
4554263Sshin#include <sys/param.h>
4654263Sshin#include <sys/systm.h>
4754263Sshin#include <sys/kernel.h>
4854263Sshin#include <sys/mbuf.h>
49129880Sphk#include <sys/module.h>
5054263Sshin#include <sys/socket.h>
5178064Sume#include <sys/errno.h>
5254263Sshin#include <sys/sockio.h>
5378064Sume#include <sys/time.h>
5478064Sume#include <sys/queue.h>
5583934Sbrooks#include <sys/types.h>
5683934Sbrooks#include <sys/malloc.h>
5754263Sshin
5854263Sshin#include <net/if.h>
59130933Sbrooks#include <net/if_clone.h>
6054263Sshin#include <net/if_types.h>
6154263Sshin#include <net/netisr.h>
6254263Sshin#include <net/route.h>
6354263Sshin#include <net/bpf.h>
6454263Sshin
6578064Sume#ifdef	INET
6678064Sume#include <netinet/in.h>
6778064Sume#include <netinet/in_systm.h>
6878064Sume#include <netinet/in_var.h>
6978064Sume#include <netinet/ip.h>
7078064Sume#endif
7178064Sume
7278064Sume#ifdef INET6
7378064Sume#ifndef INET
7478064Sume#include <netinet/in.h>
7578064Sume#endif
7678064Sume#include <netinet6/in6_var.h>
7778064Sume#include <netinet/ip6.h>
7878064Sume#include <netinet6/ip6_var.h>
7978064Sume#endif
8078064Sume
8171991Speter#include <net/net_osdep.h>
8271991Speter
8383934Sbrooks#define FAITHNAME	"faith"
8483934Sbrooks
8583934Sbrooksstruct faith_softc {
86147256Sbrooks	struct ifnet *sc_ifp;
8783934Sbrooks	LIST_ENTRY(faith_softc) sc_list;
8883934Sbrooks};
8983934Sbrooks
9092725Salfredstatic int faithioctl(struct ifnet *, u_long, caddr_t);
9192725Salfredint faithoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
9292725Salfred	struct rtentry *);
9392725Salfredstatic void faithrtrequest(int, struct rtentry *, struct rt_addrinfo *);
9491317Sdillon#ifdef INET6
9592725Salfredstatic int faithprefix(struct in6_addr *);
9691317Sdillon#endif
9754263Sshin
9892725Salfredstatic int faithmodevent(module_t, int, void *);
9978064Sume
100126781Srwatsonstatic struct mtx faith_mtx;
10183934Sbrooksstatic MALLOC_DEFINE(M_FAITH, FAITHNAME, "Firewall Assisted Tunnel Interface");
10289065Smsmithstatic LIST_HEAD(, faith_softc) faith_softc_list;
10354263Sshin
104128209Sbrooksstatic int	faith_clone_create(struct if_clone *, int);
105128209Sbrooksstatic void	faith_clone_destroy(struct ifnet *);
10683934Sbrooks
107130933SbrooksIFC_SIMPLE_DECLARE(faith, 0);
10883934Sbrooks
10954263Sshin#define	FAITHMTU	1500
11054263Sshin
11183934Sbrooksstatic int
11283934Sbrooksfaithmodevent(mod, type, data)
11383934Sbrooks	module_t mod;
11483934Sbrooks	int type;
11583934Sbrooks	void *data;
11654263Sshin{
117126781Srwatson	struct faith_softc *sc;
11854263Sshin
11983934Sbrooks	switch (type) {
12083934Sbrooks	case MOD_LOAD:
121126781Srwatson		mtx_init(&faith_mtx, "faith_mtx", NULL, MTX_DEF);
12283934Sbrooks		LIST_INIT(&faith_softc_list);
12383934Sbrooks		if_clone_attach(&faith_cloner);
12483934Sbrooks
12583934Sbrooks#ifdef INET6
12683934Sbrooks		faithprefix_p = faithprefix;
12778064Sume#endif
12883934Sbrooks
12983934Sbrooks		break;
13083934Sbrooks	case MOD_UNLOAD:
13183934Sbrooks#ifdef INET6
13283934Sbrooks		faithprefix_p = NULL;
13378064Sume#endif
13483934Sbrooks
13583934Sbrooks		if_clone_detach(&faith_cloner);
13683934Sbrooks
137126781Srwatson		mtx_lock(&faith_mtx);
138126781Srwatson		while ((sc = LIST_FIRST(&faith_softc_list)) != NULL) {
139126781Srwatson			mtx_unlock(&faith_mtx);
140151266Sthompsa			ifc_simple_destroy(&faith_cloner, sc->sc_ifp);
141126781Srwatson			mtx_lock(&faith_mtx);
142126781Srwatson		}
143126781Srwatson		mtx_unlock(&faith_mtx);
144126781Srwatson		mtx_destroy(&faith_mtx);
14583934Sbrooks		break;
146132199Sphk	default:
147132199Sphk		return EOPNOTSUPP;
14854263Sshin	}
14983934Sbrooks	return 0;
15054263Sshin}
15178064Sume
15283934Sbrooksstatic moduledata_t faith_mod = {
15383934Sbrooks	"if_faith",
15483934Sbrooks	faithmodevent,
15583934Sbrooks	0
15683934Sbrooks};
15783934Sbrooks
15883934SbrooksDECLARE_MODULE(if_faith, faith_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
15983934SbrooksMODULE_VERSION(if_faith, 1);
16083934Sbrooks
161128209Sbrooksstatic int
16283934Sbrooksfaith_clone_create(ifc, unit)
16383934Sbrooks	struct if_clone *ifc;
16492081Smux	int unit;
16583934Sbrooks{
166147256Sbrooks	struct ifnet *ifp;
16783934Sbrooks	struct faith_softc *sc;
16883934Sbrooks
169131675Sbms	sc = malloc(sizeof(struct faith_softc), M_FAITH, M_WAITOK | M_ZERO);
170147256Sbrooks	ifp = sc->sc_ifp = if_alloc(IFT_FAITH);
171147256Sbrooks	if (ifp == NULL) {
172147256Sbrooks		free(sc, M_FAITH);
173147256Sbrooks		return (ENOSPC);
174147256Sbrooks	}
17583934Sbrooks
176147256Sbrooks	ifp->if_softc = sc;
177147256Sbrooks	if_initname(sc->sc_ifp, ifc->ifc_name, unit);
17883934Sbrooks
179147256Sbrooks	ifp->if_mtu = FAITHMTU;
18083934Sbrooks	/* Change to BROADCAST experimentaly to announce its prefix. */
181147256Sbrooks	ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
182147256Sbrooks	ifp->if_ioctl = faithioctl;
183147256Sbrooks	ifp->if_output = faithoutput;
184147256Sbrooks	ifp->if_hdrlen = 0;
185147256Sbrooks	ifp->if_addrlen = 0;
186147256Sbrooks	ifp->if_snd.ifq_maxlen = ifqmaxlen;
187147256Sbrooks	if_attach(ifp);
188147611Sdwmalone	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
189126781Srwatson	mtx_lock(&faith_mtx);
19083934Sbrooks	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
191126781Srwatson	mtx_unlock(&faith_mtx);
19283934Sbrooks	return (0);
19383934Sbrooks}
19483934Sbrooks
195126781Srwatsonstatic void
19683934Sbrooksfaith_clone_destroy(ifp)
19783934Sbrooks	struct ifnet *ifp;
19883934Sbrooks{
199147256Sbrooks	struct faith_softc *sc = ifp->if_softc;
20083934Sbrooks
201126781Srwatson	mtx_lock(&faith_mtx);
20283934Sbrooks	LIST_REMOVE(sc, sc_list);
203126781Srwatson	mtx_unlock(&faith_mtx);
20483934Sbrooks
205151266Sthompsa	bpfdetach(ifp);
206151266Sthompsa	if_detach(ifp);
207151266Sthompsa	if_free(ifp);
208151266Sthompsa	free(sc, M_FAITH);
20983934Sbrooks}
21083934Sbrooks
21183934Sbrooksint
21278064Sumefaithoutput(ifp, m, dst, rt)
21378064Sume	struct ifnet *ifp;
21478064Sume	struct mbuf *m;
21578064Sume	struct sockaddr *dst;
21678064Sume	struct rtentry *rt;
21778064Sume{
21878064Sume	int isr;
219147611Sdwmalone	u_int32_t af;
22078064Sume
221113255Sdes	M_ASSERTPKTHDR(m);
22283934Sbrooks
223147611Sdwmalone	/* BPF writes need to be handled specially. */
22478064Sume	if (dst->sa_family == AF_UNSPEC) {
225147611Sdwmalone		bcopy(dst->sa_data, &af, sizeof(af));
226147611Sdwmalone		dst->sa_family = af;
22778064Sume	}
22878064Sume
22978064Sume	if (ifp->if_bpf) {
230147611Sdwmalone		af = dst->sa_family;
231123922Ssam		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
23278064Sume	}
23378064Sume
23478064Sume	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
23578064Sume		m_freem(m);
23678064Sume		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
23778064Sume		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
23878064Sume	}
23978064Sume	ifp->if_opackets++;
24078064Sume	ifp->if_obytes += m->m_pkthdr.len;
24178064Sume	switch (dst->sa_family) {
24278064Sume#ifdef INET
24378064Sume	case AF_INET:
24478064Sume		isr = NETISR_IP;
24578064Sume		break;
24678064Sume#endif
24778064Sume#ifdef INET6
24878064Sume	case AF_INET6:
24978064Sume		isr = NETISR_IPV6;
25078064Sume		break;
25178064Sume#endif
25278064Sume	default:
25378064Sume		m_freem(m);
25478064Sume		return EAFNOSUPPORT;
25578064Sume	}
25678064Sume
25778064Sume	/* XXX do we need more sanity checks? */
25878064Sume
25978064Sume	m->m_pkthdr.rcvif = ifp;
26078064Sume	ifp->if_ipackets++;
26178064Sume	ifp->if_ibytes += m->m_pkthdr.len;
262111888Sjlemon	netisr_dispatch(isr, m);
26378064Sume	return (0);
26478064Sume}
26578064Sume
26678064Sume/* ARGSUSED */
26778064Sumestatic void
26885074Srufaithrtrequest(cmd, rt, info)
26978064Sume	int cmd;
27078064Sume	struct rtentry *rt;
27185074Sru	struct rt_addrinfo *info;
27278064Sume{
273120727Ssam	RT_LOCK_ASSERT(rt);
274142352Ssam	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
27578064Sume}
27678064Sume
27778064Sume/*
27878064Sume * Process an ioctl request.
27978064Sume */
28078064Sume/* ARGSUSED */
28178064Sumestatic int
28278064Sumefaithioctl(ifp, cmd, data)
28378064Sume	struct ifnet *ifp;
28478064Sume	u_long cmd;
28578064Sume	caddr_t data;
28678064Sume{
28778064Sume	struct ifaddr *ifa;
28878064Sume	struct ifreq *ifr = (struct ifreq *)data;
28978064Sume	int error = 0;
29078064Sume
29178064Sume	switch (cmd) {
29278064Sume
29378064Sume	case SIOCSIFADDR:
294148887Srwatson		ifp->if_flags |= IFF_UP;
295148887Srwatson		ifp->if_drv_flags |= IFF_DRV_RUNNING;
29678064Sume		ifa = (struct ifaddr *)data;
29778064Sume		ifa->ifa_rtrequest = faithrtrequest;
29878064Sume		/*
29978064Sume		 * Everything else is done at a higher level.
30078064Sume		 */
30178064Sume		break;
30278064Sume
30378064Sume	case SIOCADDMULTI:
30478064Sume	case SIOCDELMULTI:
30578064Sume		if (ifr == 0) {
30678064Sume			error = EAFNOSUPPORT;		/* XXX */
30778064Sume			break;
30878064Sume		}
30978064Sume		switch (ifr->ifr_addr.sa_family) {
31078064Sume#ifdef INET
31178064Sume		case AF_INET:
31278064Sume			break;
31378064Sume#endif
31478064Sume#ifdef INET6
31578064Sume		case AF_INET6:
31678064Sume			break;
31778064Sume#endif
31878064Sume
31978064Sume		default:
32078064Sume			error = EAFNOSUPPORT;
32178064Sume			break;
32278064Sume		}
32378064Sume		break;
32478064Sume
32578064Sume#ifdef SIOCSIFMTU
32678064Sume	case SIOCSIFMTU:
32778064Sume		ifp->if_mtu = ifr->ifr_mtu;
32878064Sume		break;
32978064Sume#endif
33078064Sume
33178064Sume	case SIOCSIFFLAGS:
33278064Sume		break;
33378064Sume
33478064Sume	default:
33578064Sume		error = EINVAL;
33678064Sume	}
33778064Sume	return (error);
33878064Sume}
33978064Sume
34079326Sume#ifdef INET6
34178064Sume/*
34278064Sume * XXX could be slow
34378064Sume * XXX could be layer violation to call sys/net from sys/netinet6
34478064Sume */
34583934Sbrooksstatic int
34678064Sumefaithprefix(in6)
34778064Sume	struct in6_addr *in6;
34878064Sume{
34978064Sume	struct rtentry *rt;
35078064Sume	struct sockaddr_in6 sin6;
35178064Sume	int ret;
35278064Sume
35378064Sume	if (ip6_keepfaith == 0)
35478064Sume		return 0;
35578064Sume
35678064Sume	bzero(&sin6, sizeof(sin6));
35778064Sume	sin6.sin6_family = AF_INET6;
35878064Sume	sin6.sin6_len = sizeof(struct sockaddr_in6);
35978064Sume	sin6.sin6_addr = *in6;
36078064Sume	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
36178064Sume	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
36278064Sume	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
36378064Sume		ret = 1;
36478064Sume	else
36578064Sume		ret = 0;
36678064Sume	if (rt)
367120727Ssam		RTFREE_LOCKED(rt);
36878064Sume	return ret;
36978064Sume}
37079326Sume#endif
371