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