Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.24
      1 /*	$NetBSD: if_faith.c,v 1.24 2001/11/15 07:01:27 thorpej Exp $	*/
      2 /*	$KAME: if_faith.c,v 1.21 2001/02/20 07:59:26 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1982, 1986, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 /*
     37  * derived from
     38  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
     39  * Id: if_loop.c,v 1.22 1996/06/19 16:24:10 wollman Exp
     40  */
     41 
     42 /*
     43  * Loopback interface driver for protocol testing and timing.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: if_faith.c,v 1.24 2001/11/15 07:01:27 thorpej Exp $");
     48 
     49 #include "opt_inet.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/socket.h>
     56 #include <sys/errno.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/time.h>
     59 #include <sys/queue.h>
     60 
     61 #include <machine/cpu.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_types.h>
     65 #include <net/netisr.h>
     66 #include <net/route.h>
     67 #include <net/bpf.h>
     68 #include <net/if_faith.h>
     69 
     70 #ifdef	INET
     71 #include <netinet/in.h>
     72 #include <netinet/in_systm.h>
     73 #include <netinet/in_var.h>
     74 #include <netinet/ip.h>
     75 #endif
     76 
     77 #ifdef INET6
     78 #ifndef INET
     79 #include <netinet/in.h>
     80 #endif
     81 #include <netinet6/in6_var.h>
     82 #include <netinet/ip6.h>
     83 #include <netinet6/ip6_var.h>
     84 #endif
     85 
     86 #include "bpfilter.h"
     87 
     88 #include <net/net_osdep.h>
     89 
     90 struct faith_softc {
     91 	struct ifnet sc_if;	/* must be first */
     92 	LIST_ENTRY(faith_softc) sc_list;
     93 };
     94 
     95 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
     96 int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     97 	struct rtentry *));
     98 static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
     99 
    100 void faithattach __P((int));
    101 
    102 LIST_HEAD(, faith_softc) faith_softc_list;
    103 
    104 int	faith_clone_create __P((struct if_clone *, int));
    105 void	faith_clone_destroy __P((struct ifnet *));
    106 
    107 struct if_clone faith_cloner =
    108     IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
    109 
    110 #define	FAITHMTU	1500
    111 
    112 /* ARGSUSED */
    113 void
    114 faithattach(count)
    115 	int count;
    116 {
    117 
    118 	LIST_INIT(&faith_softc_list);
    119 	if_clone_attach(&faith_cloner);
    120 }
    121 
    122 int
    123 faith_clone_create(ifc, unit)
    124 	struct if_clone *ifc;
    125 	int unit;
    126 {
    127 	struct faith_softc *sc;
    128 
    129 	sc = malloc(sizeof(struct faith_softc), M_DEVBUF, M_WAITOK);
    130 	memset(sc, 0, sizeof(struct faith_softc));
    131 
    132 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    133 
    134 	sc->sc_if.if_mtu = FAITHMTU;
    135 	/* Change to BROADCAST experimentaly to announce its prefix. */
    136 	sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    137 	sc->sc_if.if_ioctl = faithioctl;
    138 	sc->sc_if.if_output = faithoutput;
    139 	sc->sc_if.if_type = IFT_FAITH;
    140 	sc->sc_if.if_hdrlen = 0;
    141 	sc->sc_if.if_addrlen = 0;
    142 	sc->sc_if.if_dlt = DLT_NULL;
    143 	if_attach(&sc->sc_if);
    144 	if_alloc_sadl(&sc->sc_if);
    145 #if NBPFILTER > 0
    146 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    147 #endif
    148 	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
    149 	return (0);
    150 }
    151 
    152 void
    153 faith_clone_destroy(ifp)
    154 	struct ifnet *ifp;
    155 {
    156 	struct faith_softc *sc = (void *) ifp;
    157 
    158 	LIST_REMOVE(sc, sc_list);
    159 #if NBPFILTER > 0
    160 	bpfdetach(ifp);
    161 #endif
    162 	if_detach(ifp);
    163 	free(sc, M_DEVBUF);
    164 }
    165 
    166 int
    167 faithoutput(ifp, m, dst, rt)
    168 	struct ifnet *ifp;
    169 	struct mbuf *m;
    170 	struct sockaddr *dst;
    171 	struct rtentry *rt;
    172 {
    173 	int s, isr;
    174 	struct ifqueue *ifq = 0;
    175 
    176 	if ((m->m_flags & M_PKTHDR) == 0)
    177 		panic("faithoutput no HDR");
    178 #if NBPFILTER > 0
    179 	/* BPF write needs to be handled specially */
    180 	if (dst->sa_family == AF_UNSPEC) {
    181 		dst->sa_family = *(mtod(m, int *));
    182 		m->m_len -= sizeof(int);
    183 		m->m_pkthdr.len -= sizeof(int);
    184 		m->m_data += sizeof(int);
    185 	}
    186 
    187 	if (ifp->if_bpf) {
    188 		/*
    189 		 * We need to prepend the address family as
    190 		 * a four byte field.  Cons up a faith header
    191 		 * to pacify bpf.  This is safe because bpf
    192 		 * will only read from the mbuf (i.e., it won't
    193 		 * try to free it or keep a pointer a to it).
    194 		 */
    195 		struct mbuf m0;
    196 		u_int32_t af = dst->sa_family;
    197 
    198 		m0.m_next = m;
    199 		m0.m_len = 4;
    200 		m0.m_data = (char *)&af;
    201 
    202 #ifdef HAVE_OLD_BPF
    203 		bpf_mtap(ifp, &m0);
    204 #else
    205 		bpf_mtap(ifp->if_bpf, &m0);
    206 #endif
    207 	}
    208 #endif
    209 
    210 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    211 		m_freem(m);
    212 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    213 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    214 	}
    215 	ifp->if_opackets++;
    216 	ifp->if_obytes += m->m_pkthdr.len;
    217 	switch (dst->sa_family) {
    218 #ifdef INET
    219 	case AF_INET:
    220 		ifq = &ipintrq;
    221 		isr = NETISR_IP;
    222 		break;
    223 #endif
    224 #ifdef INET6
    225 	case AF_INET6:
    226 		ifq = &ip6intrq;
    227 		isr = NETISR_IPV6;
    228 		break;
    229 #endif
    230 	default:
    231 		m_freem(m);
    232 		return EAFNOSUPPORT;
    233 	}
    234 
    235 	/* XXX do we need more sanity checks? */
    236 
    237 	m->m_pkthdr.rcvif = ifp;
    238 	s = splnet();
    239 	if (IF_QFULL(ifq)) {
    240 		IF_DROP(ifq);
    241 		m_freem(m);
    242 		splx(s);
    243 		return (ENOBUFS);
    244 	}
    245 	IF_ENQUEUE(ifq, m);
    246 	schednetisr(isr);
    247 	ifp->if_ipackets++;
    248 	ifp->if_ibytes += m->m_pkthdr.len;
    249 	splx(s);
    250 	return (0);
    251 }
    252 
    253 /* ARGSUSED */
    254 static void
    255 faithrtrequest(cmd, rt, info)
    256 	int cmd;
    257 	struct rtentry *rt;
    258 	struct rt_addrinfo *info;
    259 {
    260 	if (rt) {
    261 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    262 		/*
    263 		 * For optimal performance, the send and receive buffers
    264 		 * should be at least twice the MTU plus a little more for
    265 		 * overhead.
    266 		 */
    267 		rt->rt_rmx.rmx_recvpipe =
    268 			rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
    269 	}
    270 }
    271 
    272 /*
    273  * Process an ioctl request.
    274  */
    275 /* ARGSUSED */
    276 static int
    277 faithioctl(ifp, cmd, data)
    278 	struct ifnet *ifp;
    279 	u_long cmd;
    280 	caddr_t data;
    281 {
    282 	struct ifaddr *ifa;
    283 	struct ifreq *ifr = (struct ifreq *)data;
    284 	int error = 0;
    285 
    286 	switch (cmd) {
    287 
    288 	case SIOCSIFADDR:
    289 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    290 		ifa = (struct ifaddr *)data;
    291 		ifa->ifa_rtrequest = faithrtrequest;
    292 		/*
    293 		 * Everything else is done at a higher level.
    294 		 */
    295 		break;
    296 
    297 	case SIOCADDMULTI:
    298 	case SIOCDELMULTI:
    299 		if (ifr == 0) {
    300 			error = EAFNOSUPPORT;		/* XXX */
    301 			break;
    302 		}
    303 		switch (ifr->ifr_addr.sa_family) {
    304 #ifdef INET
    305 		case AF_INET:
    306 			break;
    307 #endif
    308 #ifdef INET6
    309 		case AF_INET6:
    310 			break;
    311 #endif
    312 
    313 		default:
    314 			error = EAFNOSUPPORT;
    315 			break;
    316 		}
    317 		break;
    318 
    319 #ifdef SIOCSIFMTU
    320 	case SIOCSIFMTU:
    321 		ifp->if_mtu = ifr->ifr_mtu;
    322 		break;
    323 #endif
    324 
    325 	case SIOCSIFFLAGS:
    326 		break;
    327 
    328 	default:
    329 		error = EINVAL;
    330 	}
    331 	return (error);
    332 }
    333 
    334 /*
    335  * XXX could be slow
    336  * XXX could be layer violation to call sys/net from sys/netinet6
    337  */
    338 int
    339 faithprefix(in6)
    340 	struct in6_addr *in6;
    341 {
    342 	struct rtentry *rt;
    343 	struct sockaddr_in6 sin6;
    344 	int ret;
    345 
    346 	if (ip6_keepfaith == 0)
    347 		return 0;
    348 
    349 	memset(&sin6, 0, sizeof(sin6));
    350 	sin6.sin6_family = AF_INET6;
    351 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    352 	sin6.sin6_addr = *in6;
    353 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    354 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    355 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    356 		ret = 1;
    357 	else
    358 		ret = 0;
    359 	if (rt)
    360 		RTFREE(rt);
    361 	return ret;
    362 }
    363