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