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