Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.24
      1  1.24  jonathan /*	$NetBSD: if_loop.c,v 1.24 1998/07/05 04:37:41 jonathan Exp $	*/
      2  1.12       cgd 
      3   1.1       cgd /*
      4  1.11   mycroft  * Copyright (c) 1982, 1986, 1993
      5  1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  *
     35  1.21      fvdl  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
     36   1.1       cgd  */
     37   1.1       cgd 
     38   1.1       cgd /*
     39   1.1       cgd  * Loopback interface driver for protocol testing and timing.
     40   1.1       cgd  */
     41  1.23  jonathan 
     42  1.23  jonathan #include "opt_inet.h"
     43  1.23  jonathan #include "opt_atalk.h"
     44  1.24  jonathan #include "opt_iso.h"
     45   1.1       cgd 
     46   1.9   mycroft #include "bpfilter.h"
     47   1.4       cgd #include "loop.h"
     48   1.4       cgd 
     49   1.9   mycroft #include <sys/param.h>
     50   1.9   mycroft #include <sys/systm.h>
     51  1.11   mycroft #include <sys/kernel.h>
     52   1.9   mycroft #include <sys/mbuf.h>
     53   1.9   mycroft #include <sys/socket.h>
     54   1.9   mycroft #include <sys/errno.h>
     55   1.9   mycroft #include <sys/ioctl.h>
     56  1.11   mycroft #include <sys/time.h>
     57  1.11   mycroft 
     58  1.11   mycroft #include <machine/cpu.h>
     59   1.9   mycroft 
     60   1.9   mycroft #include <net/if.h>
     61   1.9   mycroft #include <net/if_types.h>
     62   1.9   mycroft #include <net/netisr.h>
     63   1.9   mycroft #include <net/route.h>
     64   1.1       cgd 
     65   1.1       cgd #ifdef	INET
     66   1.9   mycroft #include <netinet/in.h>
     67   1.9   mycroft #include <netinet/in_systm.h>
     68   1.9   mycroft #include <netinet/in_var.h>
     69   1.9   mycroft #include <netinet/ip.h>
     70   1.1       cgd #endif
     71   1.1       cgd 
     72   1.1       cgd #ifdef NS
     73   1.9   mycroft #include <netns/ns.h>
     74   1.9   mycroft #include <netns/ns_if.h>
     75   1.1       cgd #endif
     76   1.1       cgd 
     77  1.22  christos #ifdef IPX
     78  1.22  christos #include <netipx/ipx.h>
     79  1.22  christos #include <netipx/ipx_if.h>
     80  1.22  christos #endif
     81  1.22  christos 
     82   1.1       cgd #ifdef ISO
     83   1.9   mycroft #include <netiso/iso.h>
     84   1.9   mycroft #include <netiso/iso_var.h>
     85   1.1       cgd #endif
     86   1.1       cgd 
     87  1.19  christos #ifdef NETATALK
     88  1.19  christos #include <netatalk/at.h>
     89  1.19  christos #include <netatalk/at_var.h>
     90  1.19  christos #endif
     91  1.19  christos 
     92   1.2       cgd #if NBPFILTER > 0
     93   1.2       cgd #include <net/bpf.h>
     94   1.2       cgd #endif
     95   1.9   mycroft 
     96  1.20  jonathan #define	LOMTU	(32768 +  MHLEN + MLEN)
     97   1.1       cgd 
     98   1.4       cgd struct	ifnet loif[NLOOP];
     99   1.1       cgd 
    100   1.6    andrew void
    101  1.11   mycroft loopattach(n)
    102  1.11   mycroft 	int n;
    103   1.1       cgd {
    104   1.4       cgd 	register int i;
    105   1.4       cgd 	register struct ifnet *ifp;
    106   1.1       cgd 
    107  1.11   mycroft 	for (i = 0; i < NLOOP; i++) {
    108   1.4       cgd 		ifp = &loif[i];
    109  1.17  christos 		sprintf(ifp->if_xname, "lo%d", i);
    110  1.15   thorpej 		ifp->if_softc = NULL;
    111   1.4       cgd 		ifp->if_mtu = LOMTU;
    112  1.11   mycroft 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
    113   1.4       cgd 		ifp->if_ioctl = loioctl;
    114   1.4       cgd 		ifp->if_output = looutput;
    115   1.4       cgd 		ifp->if_type = IFT_LOOP;
    116   1.4       cgd 		ifp->if_hdrlen = 0;
    117   1.4       cgd 		ifp->if_addrlen = 0;
    118   1.4       cgd 		if_attach(ifp);
    119   1.2       cgd #if NBPFILTER > 0
    120  1.11   mycroft 		bpfattach(&ifp->if_bpf, ifp, DLT_NULL, sizeof(u_int));
    121   1.2       cgd #endif
    122   1.4       cgd 	}
    123   1.1       cgd }
    124   1.1       cgd 
    125  1.11   mycroft int
    126   1.1       cgd looutput(ifp, m, dst, rt)
    127   1.1       cgd 	struct ifnet *ifp;
    128   1.1       cgd 	register struct mbuf *m;
    129   1.1       cgd 	struct sockaddr *dst;
    130   1.1       cgd 	register struct rtentry *rt;
    131   1.1       cgd {
    132   1.1       cgd 	int s, isr;
    133   1.1       cgd 	register struct ifqueue *ifq = 0;
    134   1.1       cgd 
    135   1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    136  1.14   mycroft 		panic("looutput: no header mbuf");
    137  1.11   mycroft 	ifp->if_lastchange = time;
    138   1.2       cgd #if NBPFILTER > 0
    139  1.18   mycroft 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
    140   1.2       cgd 		/*
    141   1.2       cgd 		 * We need to prepend the address family as
    142   1.2       cgd 		 * a four byte field.  Cons up a dummy header
    143   1.2       cgd 		 * to pacify bpf.  This is safe because bpf
    144   1.2       cgd 		 * will only read from the mbuf (i.e., it won't
    145   1.2       cgd 		 * try to free it or keep a pointer to it).
    146   1.2       cgd 		 */
    147   1.2       cgd 		struct mbuf m0;
    148   1.2       cgd 		u_int af = dst->sa_family;
    149   1.2       cgd 
    150   1.2       cgd 		m0.m_next = m;
    151   1.2       cgd 		m0.m_len = 4;
    152   1.2       cgd 		m0.m_data = (char *)&af;
    153   1.2       cgd 
    154  1.11   mycroft 		bpf_mtap(ifp->if_bpf, &m0);
    155   1.2       cgd 	}
    156   1.2       cgd #endif
    157   1.1       cgd 	m->m_pkthdr.rcvif = ifp;
    158   1.1       cgd 
    159  1.11   mycroft 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    160   1.1       cgd 		m_freem(m);
    161  1.11   mycroft 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    162  1.11   mycroft 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    163   1.1       cgd 	}
    164   1.1       cgd 	ifp->if_opackets++;
    165   1.1       cgd 	ifp->if_obytes += m->m_pkthdr.len;
    166   1.1       cgd 	switch (dst->sa_family) {
    167   1.1       cgd 
    168   1.1       cgd #ifdef INET
    169   1.1       cgd 	case AF_INET:
    170   1.1       cgd 		ifq = &ipintrq;
    171   1.1       cgd 		isr = NETISR_IP;
    172   1.1       cgd 		break;
    173   1.1       cgd #endif
    174   1.1       cgd #ifdef NS
    175   1.1       cgd 	case AF_NS:
    176   1.1       cgd 		ifq = &nsintrq;
    177   1.1       cgd 		isr = NETISR_NS;
    178   1.1       cgd 		break;
    179   1.1       cgd #endif
    180   1.1       cgd #ifdef ISO
    181   1.1       cgd 	case AF_ISO:
    182   1.1       cgd 		ifq = &clnlintrq;
    183   1.1       cgd 		isr = NETISR_ISO;
    184  1.22  christos 		break;
    185  1.22  christos #endif
    186  1.22  christos #ifdef IPX
    187  1.22  christos 	case AF_IPX:
    188  1.22  christos 		ifq = &ipxintrq;
    189  1.22  christos 		isr = NETISR_IPX;
    190  1.19  christos 		break;
    191  1.19  christos #endif
    192  1.19  christos #ifdef NETATALK
    193  1.19  christos 	case AF_APPLETALK:
    194  1.19  christos 	        ifq = &atintrq2;
    195  1.19  christos 		isr = NETISR_ATALK;
    196   1.1       cgd 		break;
    197   1.1       cgd #endif
    198   1.1       cgd 	default:
    199  1.17  christos 		printf("%s: can't handle af%d\n", ifp->if_xname,
    200  1.16  christos 		    dst->sa_family);
    201   1.1       cgd 		m_freem(m);
    202   1.1       cgd 		return (EAFNOSUPPORT);
    203   1.1       cgd 	}
    204   1.1       cgd 	s = splimp();
    205   1.1       cgd 	if (IF_QFULL(ifq)) {
    206   1.1       cgd 		IF_DROP(ifq);
    207   1.1       cgd 		m_freem(m);
    208   1.1       cgd 		splx(s);
    209   1.1       cgd 		return (ENOBUFS);
    210   1.1       cgd 	}
    211   1.1       cgd 	IF_ENQUEUE(ifq, m);
    212   1.1       cgd 	schednetisr(isr);
    213   1.1       cgd 	ifp->if_ipackets++;
    214   1.1       cgd 	ifp->if_ibytes += m->m_pkthdr.len;
    215   1.1       cgd 	splx(s);
    216   1.1       cgd 	return (0);
    217   1.1       cgd }
    218   1.1       cgd 
    219   1.1       cgd /* ARGSUSED */
    220  1.11   mycroft void
    221   1.1       cgd lortrequest(cmd, rt, sa)
    222  1.11   mycroft 	int cmd;
    223  1.11   mycroft 	struct rtentry *rt;
    224  1.11   mycroft 	struct sockaddr *sa;
    225   1.1       cgd {
    226  1.11   mycroft 
    227   1.1       cgd 	if (rt)
    228   1.1       cgd 		rt->rt_rmx.rmx_mtu = LOMTU;
    229   1.1       cgd }
    230   1.1       cgd 
    231   1.1       cgd /*
    232   1.1       cgd  * Process an ioctl request.
    233   1.1       cgd  */
    234   1.1       cgd /* ARGSUSED */
    235  1.11   mycroft int
    236   1.1       cgd loioctl(ifp, cmd, data)
    237   1.1       cgd 	register struct ifnet *ifp;
    238  1.13       cgd 	u_long cmd;
    239   1.1       cgd 	caddr_t data;
    240   1.1       cgd {
    241   1.1       cgd 	register struct ifaddr *ifa;
    242   1.8   hpeyerl 	register struct ifreq *ifr;
    243  1.11   mycroft 	register int error = 0;
    244   1.1       cgd 
    245   1.1       cgd 	switch (cmd) {
    246   1.1       cgd 
    247   1.1       cgd 	case SIOCSIFADDR:
    248   1.1       cgd 		ifp->if_flags |= IFF_UP;
    249   1.1       cgd 		ifa = (struct ifaddr *)data;
    250   1.1       cgd 		if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
    251   1.1       cgd 			ifa->ifa_rtrequest = lortrequest;
    252   1.1       cgd 		/*
    253   1.1       cgd 		 * Everything else is done at a higher level.
    254   1.1       cgd 		 */
    255   1.1       cgd 		break;
    256   1.1       cgd 
    257   1.8   hpeyerl 	case SIOCADDMULTI:
    258   1.8   hpeyerl 	case SIOCDELMULTI:
    259   1.8   hpeyerl 		ifr = (struct ifreq *)data;
    260   1.8   hpeyerl 		if (ifr == 0) {
    261   1.8   hpeyerl 			error = EAFNOSUPPORT;		/* XXX */
    262   1.8   hpeyerl 			break;
    263   1.8   hpeyerl 		}
    264   1.8   hpeyerl 		switch (ifr->ifr_addr.sa_family) {
    265   1.8   hpeyerl 
    266   1.8   hpeyerl #ifdef INET
    267   1.8   hpeyerl 		case AF_INET:
    268   1.8   hpeyerl 			break;
    269   1.8   hpeyerl #endif
    270  1.11   mycroft 
    271   1.8   hpeyerl 		default:
    272   1.8   hpeyerl 			error = EAFNOSUPPORT;
    273   1.8   hpeyerl 			break;
    274   1.8   hpeyerl 		}
    275   1.8   hpeyerl 		break;
    276  1.11   mycroft 
    277   1.1       cgd 	default:
    278   1.1       cgd 		error = EINVAL;
    279   1.1       cgd 	}
    280   1.1       cgd 	return (error);
    281   1.1       cgd }
    282