Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.19
      1 /*	$NetBSD: if_faith.c,v 1.19 2001/05/08 10:15:14 itojun 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 #include "opt_inet.h"
     46 
     47 #include "faith.h"
     48 #if NFAITH > 0
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/kernel.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/socket.h>
     55 #include <sys/errno.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/time.h>
     58 #include <sys/queue.h>
     59 
     60 #include <machine/cpu.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_types.h>
     64 #include <net/netisr.h>
     65 #include <net/route.h>
     66 #include <net/bpf.h>
     67 #include <net/if_faith.h>
     68 
     69 #ifdef	INET
     70 #include <netinet/in.h>
     71 #include <netinet/in_systm.h>
     72 #include <netinet/in_var.h>
     73 #include <netinet/ip.h>
     74 #endif
     75 
     76 #ifdef INET6
     77 #ifndef INET
     78 #include <netinet/in.h>
     79 #endif
     80 #include <netinet6/in6_var.h>
     81 #include <netinet/ip6.h>
     82 #include <netinet6/ip6_var.h>
     83 #endif
     84 
     85 #include "bpfilter.h"
     86 
     87 #include <net/net_osdep.h>
     88 
     89 struct faith_softc {
     90 	struct ifnet sc_if;	/* must be first */
     91 	LIST_ENTRY(faith_softc) sc_list;
     92 };
     93 
     94 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
     95 int faithoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     96 	struct rtentry *));
     97 static void faithrtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
     98 
     99 void faithattach __P((int));
    100 
    101 LIST_HEAD(, faith_softc) faith_softc_list;
    102 
    103 int	faith_clone_create __P((struct if_clone *, int));
    104 void	faith_clone_destroy __P((struct ifnet *));
    105 
    106 struct if_clone faith_cloner =
    107     IF_CLONE_INITIALIZER("faith", faith_clone_create, faith_clone_destroy);
    108 
    109 #define	FAITHMTU	1500
    110 
    111 /* ARGSUSED */
    112 void
    113 faithattach(count)
    114 	int count;
    115 {
    116 
    117 	LIST_INIT(&faith_softc_list);
    118 	if_clone_attach(&faith_cloner);
    119 }
    120 
    121 int
    122 faith_clone_create(ifc, unit)
    123 	struct if_clone *ifc;
    124 	int unit;
    125 {
    126 	struct faith_softc *sc;
    127 
    128 	sc = malloc(sizeof(struct faith_softc), M_DEVBUF, M_WAITOK);
    129 	bzero(sc, sizeof(struct faith_softc));
    130 
    131 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    132 
    133 	sc->sc_if.if_mtu = FAITHMTU;
    134 	/* Change to BROADCAST experimentaly to announce its prefix. */
    135 	sc->sc_if.if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    136 	sc->sc_if.if_ioctl = faithioctl;
    137 	sc->sc_if.if_output = faithoutput;
    138 	sc->sc_if.if_type = IFT_FAITH;
    139 	sc->sc_if.if_hdrlen = 0;
    140 	sc->sc_if.if_addrlen = 0;
    141 	sc->sc_if.if_dlt = DLT_NULL;
    142 	if_attach(&sc->sc_if);
    143 	if_alloc_sadl(&sc->sc_if);
    144 #if NBPFILTER > 0
    145 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    146 #endif
    147 	LIST_INSERT_HEAD(&faith_softc_list, sc, sc_list);
    148 	return (0);
    149 }
    150 
    151 void
    152 faith_clone_destroy(ifp)
    153 	struct ifnet *ifp;
    154 {
    155 	struct faith_softc *sc = (void *) ifp;
    156 
    157 	LIST_REMOVE(sc, sc_list);
    158 #if NBPFILTER > 0
    159 	bpfdetach(ifp);
    160 #endif
    161 	if_detach(ifp);
    162 	free(sc, M_DEVBUF);
    163 }
    164 
    165 int
    166 faithoutput(ifp, m, dst, rt)
    167 	struct ifnet *ifp;
    168 	struct mbuf *m;
    169 	struct sockaddr *dst;
    170 	struct rtentry *rt;
    171 {
    172 	int s, isr;
    173 	struct ifqueue *ifq = 0;
    174 
    175 	if ((m->m_flags & M_PKTHDR) == 0)
    176 		panic("faithoutput no HDR");
    177 #if NBPFILTER > 0
    178 	/* BPF write needs to be handled specially */
    179 	if (dst->sa_family == AF_UNSPEC) {
    180 		dst->sa_family = *(mtod(m, int *));
    181 		m->m_len -= sizeof(int);
    182 		m->m_pkthdr.len -= sizeof(int);
    183 		m->m_data += sizeof(int);
    184 	}
    185 
    186 	if (ifp->if_bpf) {
    187 		/*
    188 		 * We need to prepend the address family as
    189 		 * a four byte field.  Cons up a faith header
    190 		 * to pacify bpf.  This is safe because bpf
    191 		 * will only read from the mbuf (i.e., it won't
    192 		 * try to free it or keep a pointer a to it).
    193 		 */
    194 		struct mbuf m0;
    195 		u_int32_t af = dst->sa_family;
    196 
    197 		m0.m_next = m;
    198 		m0.m_len = 4;
    199 		m0.m_data = (char *)&af;
    200 
    201 #ifdef HAVE_OLD_BPF
    202 		bpf_mtap(ifp, &m0);
    203 #else
    204 		bpf_mtap(ifp->if_bpf, &m0);
    205 #endif
    206 	}
    207 #endif
    208 
    209 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    210 		m_freem(m);
    211 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    212 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    213 	}
    214 	ifp->if_opackets++;
    215 	ifp->if_obytes += m->m_pkthdr.len;
    216 	switch (dst->sa_family) {
    217 #ifdef INET
    218 	case AF_INET:
    219 		ifq = &ipintrq;
    220 		isr = NETISR_IP;
    221 		break;
    222 #endif
    223 #ifdef INET6
    224 	case AF_INET6:
    225 		ifq = &ip6intrq;
    226 		isr = NETISR_IPV6;
    227 		break;
    228 #endif
    229 	default:
    230 		m_freem(m);
    231 		return EAFNOSUPPORT;
    232 	}
    233 
    234 	/* XXX do we need more sanity checks? */
    235 
    236 	m->m_pkthdr.rcvif = ifp;
    237 	s = splnet();
    238 	if (IF_QFULL(ifq)) {
    239 		IF_DROP(ifq);
    240 		m_freem(m);
    241 		splx(s);
    242 		return (ENOBUFS);
    243 	}
    244 	IF_ENQUEUE(ifq, m);
    245 	schednetisr(isr);
    246 	ifp->if_ipackets++;
    247 	ifp->if_ibytes += m->m_pkthdr.len;
    248 	splx(s);
    249 	return (0);
    250 }
    251 
    252 /* ARGSUSED */
    253 static void
    254 faithrtrequest(cmd, rt, info)
    255 	int cmd;
    256 	struct rtentry *rt;
    257 	struct rt_addrinfo *info;
    258 {
    259 	if (rt) {
    260 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    261 		/*
    262 		 * For optimal performance, the send and receive buffers
    263 		 * should be at least twice the MTU plus a little more for
    264 		 * overhead.
    265 		 */
    266 		rt->rt_rmx.rmx_recvpipe =
    267 			rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
    268 	}
    269 }
    270 
    271 /*
    272  * Process an ioctl request.
    273  */
    274 /* ARGSUSED */
    275 static int
    276 faithioctl(ifp, cmd, data)
    277 	struct ifnet *ifp;
    278 	u_long cmd;
    279 	caddr_t data;
    280 {
    281 	struct ifaddr *ifa;
    282 	struct ifreq *ifr = (struct ifreq *)data;
    283 	int error = 0;
    284 
    285 	switch (cmd) {
    286 
    287 	case SIOCSIFADDR:
    288 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    289 		ifa = (struct ifaddr *)data;
    290 		ifa->ifa_rtrequest = faithrtrequest;
    291 		/*
    292 		 * Everything else is done at a higher level.
    293 		 */
    294 		break;
    295 
    296 	case SIOCADDMULTI:
    297 	case SIOCDELMULTI:
    298 		if (ifr == 0) {
    299 			error = EAFNOSUPPORT;		/* XXX */
    300 			break;
    301 		}
    302 		switch (ifr->ifr_addr.sa_family) {
    303 #ifdef INET
    304 		case AF_INET:
    305 			break;
    306 #endif
    307 #ifdef INET6
    308 		case AF_INET6:
    309 			break;
    310 #endif
    311 
    312 		default:
    313 			error = EAFNOSUPPORT;
    314 			break;
    315 		}
    316 		break;
    317 
    318 #ifdef SIOCSIFMTU
    319 	case SIOCSIFMTU:
    320 		ifp->if_mtu = ifr->ifr_mtu;
    321 		break;
    322 #endif
    323 
    324 	case SIOCSIFFLAGS:
    325 		break;
    326 
    327 	default:
    328 		error = EINVAL;
    329 	}
    330 	return (error);
    331 }
    332 
    333 /*
    334  * XXX could be slow
    335  * XXX could be layer violation to call sys/net from sys/netinet6
    336  */
    337 int
    338 faithprefix(in6)
    339 	struct in6_addr *in6;
    340 {
    341 	struct rtentry *rt;
    342 	struct sockaddr_in6 sin6;
    343 	int ret;
    344 
    345 	if (ip6_keepfaith == 0)
    346 		return 0;
    347 
    348 	bzero(&sin6, sizeof(sin6));
    349 	sin6.sin6_family = AF_INET6;
    350 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    351 	sin6.sin6_addr = *in6;
    352 #ifdef __FreeBSD__
    353 	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
    354 #else
    355 	rt = rtalloc1((struct sockaddr *)&sin6, 0);
    356 #endif
    357 	if (rt && rt->rt_ifp && rt->rt_ifp->if_type == IFT_FAITH &&
    358 	    (rt->rt_ifp->if_flags & IFF_UP) != 0)
    359 		ret = 1;
    360 	else
    361 		ret = 0;
    362 	if (rt)
    363 		RTFREE(rt);
    364 	return ret;
    365 }
    366 #endif /* NFAITH > 0 */
    367