Home | History | Annotate | Line # | Download | only in net
if_faith.c revision 1.10
      1 /*	$NetBSD: if_faith.c,v 1.10 2000/02/06 12:49:37 itojun 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 <machine/cpu.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_types.h>
     61 #include <net/netisr.h>
     62 #include <net/route.h>
     63 #include <net/bpf.h>
     64 
     65 #ifdef	INET
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/ip.h>
     70 #endif
     71 
     72 #ifdef INET6
     73 #ifndef INET
     74 #include <netinet/in.h>
     75 #endif
     76 #include <netinet6/in6_var.h>
     77 #include <netinet/ip6.h>
     78 #endif
     79 
     80 #include "bpfilter.h"
     81 
     82 #include <net/net_osdep.h>
     83 
     84 static int faithioctl __P((struct ifnet *, u_long, caddr_t));
     85 int faithoutput __P((struct ifnet *, register struct mbuf *, struct sockaddr *,
     86 	register struct rtentry *));
     87 static void faithrtrequest __P((int, struct rtentry *, struct sockaddr *));
     88 
     89 void faithattach __P((int));
     90 
     91 static struct ifnet faithif[NFAITH];
     92 
     93 #define	FAITHMTU	1500
     94 
     95 /* ARGSUSED */
     96 void
     97 faithattach(faith)
     98 	int faith;
     99 {
    100 	register struct ifnet *ifp;
    101 	register int i;
    102 
    103 	for (i = 0; i < NFAITH; i++) {
    104 		ifp = &faithif[i];
    105 		bzero(ifp, sizeof(faithif[i]));
    106 		sprintf(ifp->if_xname, "faith%d", i);
    107 		ifp->if_mtu = FAITHMTU;
    108 		/* Change to BROADCAST experimentaly to announce its prefix. */
    109 		ifp->if_flags = /* IFF_LOOPBACK */ IFF_BROADCAST | IFF_MULTICAST;
    110 		ifp->if_ioctl = faithioctl;
    111 		ifp->if_output = faithoutput;
    112 		ifp->if_type = IFT_FAITH;
    113 		ifp->if_hdrlen = 0;
    114 		ifp->if_addrlen = 0;
    115 		if_attach(ifp);
    116 #if NBPFILTER > 0
    117 #ifdef HAVE_OLD_BPF
    118 		bpfattach(ifp, DLT_NULL, sizeof(u_int));
    119 #else
    120 		bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
    121 #endif
    122 #endif
    123 	}
    124 }
    125 
    126 int
    127 faithoutput(ifp, m, dst, rt)
    128 	struct ifnet *ifp;
    129 	register struct mbuf *m;
    130 	struct sockaddr *dst;
    131 	register struct rtentry *rt;
    132 {
    133 	int s, isr;
    134 	register struct ifqueue *ifq = 0;
    135 
    136 	if ((m->m_flags & M_PKTHDR) == 0)
    137 		panic("faithoutput no HDR");
    138 #if NBPFILTER > 0
    139 	/* BPF write needs to be handled specially */
    140 	if (dst->sa_family == AF_UNSPEC) {
    141 		dst->sa_family = *(mtod(m, int *));
    142 		m->m_len -= sizeof(int);
    143 		m->m_pkthdr.len -= sizeof(int);
    144 		m->m_data += sizeof(int);
    145 	}
    146 
    147 	if (ifp->if_bpf) {
    148 		/*
    149 		 * We need to prepend the address family as
    150 		 * a four byte field.  Cons up a faith header
    151 		 * to pacify bpf.  This is safe because bpf
    152 		 * will only read from the mbuf (i.e., it won't
    153 		 * try to free it or keep a pointer a to it).
    154 		 */
    155 		struct mbuf m0;
    156 		u_int af = dst->sa_family;
    157 
    158 		m0.m_next = m;
    159 		m0.m_len = 4;
    160 		m0.m_data = (char *)&af;
    161 
    162 #ifdef HAVE_OLD_BPF
    163 		bpf_mtap(ifp, &m0);
    164 #else
    165 		bpf_mtap(ifp->if_bpf, &m0);
    166 #endif
    167 	}
    168 #endif
    169 
    170 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    171 		m_freem(m);
    172 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    173 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    174 	}
    175 	ifp->if_opackets++;
    176 	ifp->if_obytes += m->m_pkthdr.len;
    177 	switch (dst->sa_family) {
    178 #ifdef INET
    179 	case AF_INET:
    180 		ifq = &ipintrq;
    181 		isr = NETISR_IP;
    182 		break;
    183 #endif
    184 #ifdef INET6
    185 	case AF_INET6:
    186 		ifq = &ip6intrq;
    187 		isr = NETISR_IPV6;
    188 		break;
    189 #endif
    190 	default:
    191 		m_freem(m);
    192 		return EAFNOSUPPORT;
    193 	}
    194 
    195 	/* XXX do we need more sanity checks? */
    196 
    197 	m->m_pkthdr.rcvif = ifp;
    198 	s = splimp();
    199 	if (IF_QFULL(ifq)) {
    200 		IF_DROP(ifq);
    201 		m_freem(m);
    202 		splx(s);
    203 		return (ENOBUFS);
    204 	}
    205 	IF_ENQUEUE(ifq, m);
    206 	schednetisr(isr);
    207 	ifp->if_ipackets++;
    208 	ifp->if_ibytes += m->m_pkthdr.len;
    209 	splx(s);
    210 	return (0);
    211 }
    212 
    213 /* ARGSUSED */
    214 static void
    215 faithrtrequest(cmd, rt, sa)
    216 	int cmd;
    217 	struct rtentry *rt;
    218 	struct sockaddr *sa;
    219 {
    220 	if (rt) {
    221 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
    222 		/*
    223 		 * For optimal performance, the send and receive buffers
    224 		 * should be at least twice the MTU plus a little more for
    225 		 * overhead.
    226 		 */
    227 		rt->rt_rmx.rmx_recvpipe =
    228 			rt->rt_rmx.rmx_sendpipe = 3 * FAITHMTU;
    229 	}
    230 }
    231 
    232 /*
    233  * Process an ioctl request.
    234  */
    235 /* ARGSUSED */
    236 static int
    237 faithioctl(ifp, cmd, data)
    238 	register struct ifnet *ifp;
    239 	u_long cmd;
    240 	caddr_t data;
    241 {
    242 	register struct ifaddr *ifa;
    243 	register struct ifreq *ifr = (struct ifreq *)data;
    244 	register int error = 0;
    245 
    246 	switch (cmd) {
    247 
    248 	case SIOCSIFADDR:
    249 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
    250 		ifa = (struct ifaddr *)data;
    251 		ifa->ifa_rtrequest = faithrtrequest;
    252 		/*
    253 		 * Everything else is done at a higher level.
    254 		 */
    255 		break;
    256 
    257 	case SIOCADDMULTI:
    258 	case SIOCDELMULTI:
    259 		if (ifr == 0) {
    260 			error = EAFNOSUPPORT;		/* XXX */
    261 			break;
    262 		}
    263 		switch (ifr->ifr_addr.sa_family) {
    264 #ifdef INET
    265 		case AF_INET:
    266 			break;
    267 #endif
    268 #ifdef INET6
    269 		case AF_INET6:
    270 			break;
    271 #endif
    272 
    273 		default:
    274 			error = EAFNOSUPPORT;
    275 			break;
    276 		}
    277 		break;
    278 
    279 #ifdef SIOCSIFMTU
    280 	case SIOCSIFMTU:
    281 		ifp->if_mtu = ifr->ifr_mtu;
    282 		break;
    283 #endif
    284 
    285 	case SIOCSIFFLAGS:
    286 		break;
    287 
    288 	default:
    289 		error = EINVAL;
    290 	}
    291 	return (error);
    292 }
    293 #endif /* NFAITH > 0 */
    294