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