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