Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.9
      1 /*
      2  * Copyright (c) 1982, 1986 Regents of the University of California.
      3  * 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  *	from: @(#)if_loop.c	7.13 (Berkeley) 4/26/91
     34  *	$Id: if_loop.c,v 1.9 1993/12/17 00:12:13 mycroft Exp $
     35  */
     36 
     37 /*
     38  * Loopback interface driver for protocol testing and timing.
     39  */
     40 
     41 #include "bpfilter.h"
     42 #include "loop.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/socket.h>
     48 #include <sys/errno.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/protosw.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_types.h>
     54 #include <net/netisr.h>
     55 #include <net/route.h>
     56 
     57 #ifdef	INET
     58 #include <netinet/in.h>
     59 #include <netinet/in_systm.h>
     60 #include <netinet/in_var.h>
     61 #include <netinet/ip.h>
     62 #endif
     63 
     64 #ifdef NS
     65 #include <netns/ns.h>
     66 #include <netns/ns_if.h>
     67 #endif
     68 
     69 #ifdef ISO
     70 #include <netiso/iso.h>
     71 #include <netiso/iso_var.h>
     72 #endif
     73 
     74 #if NBPFILTER > 0
     75 #include <sys/time.h>
     76 #include <net/bpf.h>
     77 static caddr_t lo_bpf;
     78 #endif
     79 
     80 #include <machine/cpu.h>
     81 
     82 #define	LOMTU	(1024+512)
     83 
     84 struct	ifnet loif[NLOOP];
     85 int	looutput(), loioctl();
     86 
     87 void
     88 loopattach()
     89 {
     90 	register int i;
     91 	register struct ifnet *ifp;
     92 
     93 	for (i = 0; i < NLOOP; i++)
     94 	{
     95 		ifp = &loif[i];
     96 		ifp->if_unit = i;
     97 		ifp->if_name = "lo";
     98 		ifp->if_mtu = LOMTU;
     99 #ifdef MULTICAST
    100 		ifp->if_flags = IFF_LOOPBACK|IFF_MULTICAST;
    101 #else
    102 		ifp->if_flags = IFF_LOOPBACK;
    103 #endif
    104 		ifp->if_ioctl = loioctl;
    105 		ifp->if_output = looutput;
    106 		ifp->if_type = IFT_LOOP;
    107 		ifp->if_hdrlen = 0;
    108 		ifp->if_addrlen = 0;
    109 		if_attach(ifp);
    110 #if NBPFILTER > 0
    111 		bpfattach(&lo_bpf, ifp, DLT_NULL, sizeof(u_int));
    112 #endif
    113 	}
    114 }
    115 
    116 looutput(ifp, m, dst, rt)
    117 	struct ifnet *ifp;
    118 	register struct mbuf *m;
    119 	struct sockaddr *dst;
    120 	register struct rtentry *rt;
    121 {
    122 	int s, isr;
    123 	register struct ifqueue *ifq = 0;
    124 
    125 	if ((m->m_flags & M_PKTHDR) == 0)
    126 		panic("looutput no HDR");
    127 #if NBPFILTER > 0
    128 	if (lo_bpf) {
    129 		/*
    130 		 * We need to prepend the address family as
    131 		 * a four byte field.  Cons up a dummy header
    132 		 * to pacify bpf.  This is safe because bpf
    133 		 * will only read from the mbuf (i.e., it won't
    134 		 * try to free it or keep a pointer to it).
    135 		 */
    136 		struct mbuf m0;
    137 		u_int af = dst->sa_family;
    138 
    139 		m0.m_next = m;
    140 		m0.m_len = 4;
    141 		m0.m_data = (char *)&af;
    142 
    143 		bpf_mtap(lo_bpf, &m0);
    144 	}
    145 #endif
    146 	m->m_pkthdr.rcvif = ifp;
    147 
    148 	if (rt && rt->rt_flags & RTF_REJECT) {
    149 		m_freem(m);
    150 		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    151 	}
    152 	ifp->if_opackets++;
    153 	ifp->if_obytes += m->m_pkthdr.len;
    154 	switch (dst->sa_family) {
    155 
    156 #ifdef INET
    157 	case AF_INET:
    158 		ifq = &ipintrq;
    159 		isr = NETISR_IP;
    160 		break;
    161 #endif
    162 #ifdef NS
    163 	case AF_NS:
    164 		ifq = &nsintrq;
    165 		isr = NETISR_NS;
    166 		break;
    167 #endif
    168 #ifdef ISO
    169 	case AF_ISO:
    170 		ifq = &clnlintrq;
    171 		isr = NETISR_ISO;
    172 		break;
    173 #endif
    174 	default:
    175 		printf("lo%d: can't handle af%d\n", ifp->if_unit,
    176 			dst->sa_family);
    177 		m_freem(m);
    178 		return (EAFNOSUPPORT);
    179 	}
    180 	s = splimp();
    181 	if (IF_QFULL(ifq)) {
    182 		IF_DROP(ifq);
    183 		m_freem(m);
    184 		splx(s);
    185 		return (ENOBUFS);
    186 	}
    187 	IF_ENQUEUE(ifq, m);
    188 	schednetisr(isr);
    189 	ifp->if_ipackets++;
    190 	ifp->if_ibytes += m->m_pkthdr.len;
    191 	splx(s);
    192 	return (0);
    193 }
    194 
    195 /* ARGSUSED */
    196 lortrequest(cmd, rt, sa)
    197 struct rtentry *rt;
    198 struct sockaddr *sa;
    199 {
    200 	if (rt)
    201 		rt->rt_rmx.rmx_mtu = LOMTU;
    202 }
    203 
    204 /*
    205  * Process an ioctl request.
    206  */
    207 /* ARGSUSED */
    208 loioctl(ifp, cmd, data)
    209 	register struct ifnet *ifp;
    210 	int cmd;
    211 	caddr_t data;
    212 {
    213 	register struct ifaddr *ifa;
    214 #ifdef MULTICAST
    215 	register struct ifreq *ifr;
    216 #endif
    217 	int error = 0;
    218 
    219 	switch (cmd) {
    220 
    221 	case SIOCSIFADDR:
    222 		ifp->if_flags |= IFF_UP;
    223 		ifa = (struct ifaddr *)data;
    224 		if (ifa != 0 && ifa->ifa_addr->sa_family == AF_ISO)
    225 			ifa->ifa_rtrequest = lortrequest;
    226 		/*
    227 		 * Everything else is done at a higher level.
    228 		 */
    229 		break;
    230 
    231 #ifdef MULTICAST
    232 	case SIOCADDMULTI:
    233 	case SIOCDELMULTI:
    234 		ifr = (struct ifreq *)data;
    235 		if (ifr == 0) {
    236 			error = EAFNOSUPPORT;		/* XXX */
    237 			break;
    238 		}
    239 		switch (ifr->ifr_addr.sa_family) {
    240 
    241 #ifdef INET
    242 		case AF_INET:
    243 			break;
    244 #endif
    245 		default:
    246 			error = EAFNOSUPPORT;
    247 			break;
    248 		}
    249 		break;
    250 #endif
    251 	default:
    252 		error = EINVAL;
    253 	}
    254 	return (error);
    255 }
    256