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