Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.32
      1 /*	$NetBSD: if_loop.c,v 1.32 2000/12/18 19:18:33 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * 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. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
     65  */
     66 
     67 /*
     68  * Loopback interface driver for protocol testing and timing.
     69  */
     70 
     71 #include "opt_inet.h"
     72 #include "opt_atalk.h"
     73 #include "opt_iso.h"
     74 #include "opt_ns.h"
     75 
     76 #include "bpfilter.h"
     77 #include "loop.h"
     78 
     79 #include <sys/param.h>
     80 #include <sys/systm.h>
     81 #include <sys/kernel.h>
     82 #include <sys/mbuf.h>
     83 #include <sys/socket.h>
     84 #include <sys/errno.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/time.h>
     87 
     88 #include <machine/cpu.h>
     89 
     90 #include <net/if.h>
     91 #include <net/if_types.h>
     92 #include <net/netisr.h>
     93 #include <net/route.h>
     94 
     95 #ifdef	INET
     96 #include <netinet/in.h>
     97 #include <netinet/in_systm.h>
     98 #include <netinet/in_var.h>
     99 #include <netinet/ip.h>
    100 #endif
    101 
    102 #ifdef INET6
    103 #ifndef INET
    104 #include <netinet/in.h>
    105 #endif
    106 #include <netinet6/in6_var.h>
    107 #include <netinet/ip6.h>
    108 #endif
    109 
    110 #ifdef NS
    111 #include <netns/ns.h>
    112 #include <netns/ns_if.h>
    113 #endif
    114 
    115 #ifdef IPX
    116 #include <netipx/ipx.h>
    117 #include <netipx/ipx_if.h>
    118 #endif
    119 
    120 #ifdef ISO
    121 #include <netiso/iso.h>
    122 #include <netiso/iso_var.h>
    123 #endif
    124 
    125 #ifdef NETATALK
    126 #include <netatalk/at.h>
    127 #include <netatalk/at_var.h>
    128 #endif
    129 
    130 #if NBPFILTER > 0
    131 #include <net/bpf.h>
    132 #endif
    133 
    134 #if defined(LARGE_LOMTU)
    135 #define LOMTU	(131072 +  MHLEN + MLEN)
    136 #else
    137 #define	LOMTU	(32768 +  MHLEN + MLEN)
    138 #endif
    139 
    140 struct	ifnet loif[NLOOP];
    141 
    142 void	lostart(struct ifnet *);
    143 
    144 void
    145 loopattach(n)
    146 	int n;
    147 {
    148 	int i;
    149 	struct ifnet *ifp;
    150 
    151 	for (i = 0; i < NLOOP; i++) {
    152 		ifp = &loif[i];
    153 		sprintf(ifp->if_xname, "lo%d", i);
    154 		ifp->if_softc = NULL;
    155 		ifp->if_mtu = LOMTU;
    156 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
    157 		ifp->if_ioctl = loioctl;
    158 		ifp->if_output = looutput;
    159 		ifp->if_start = lostart;
    160 		ifp->if_type = IFT_LOOP;
    161 		ifp->if_hdrlen = 0;
    162 		ifp->if_addrlen = 0;
    163 		IFQ_SET_READY(&ifp->if_snd);
    164 		if_attach(ifp);
    165 #if NBPFILTER > 0
    166 		bpfattach(ifp, DLT_NULL, sizeof(u_int));
    167 #endif
    168 	}
    169 }
    170 
    171 int
    172 looutput(ifp, m, dst, rt)
    173 	struct ifnet *ifp;
    174 	struct mbuf *m;
    175 	struct sockaddr *dst;
    176 	struct rtentry *rt;
    177 {
    178 	int s, isr;
    179 	struct ifqueue *ifq = 0;
    180 
    181 	if ((m->m_flags & M_PKTHDR) == 0)
    182 		panic("looutput: no header mbuf");
    183 	ifp->if_lastchange = time;
    184 #if NBPFILTER > 0
    185 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
    186 		/*
    187 		 * We need to prepend the address family as
    188 		 * a four byte field.  Cons up a dummy header
    189 		 * to pacify bpf.  This is safe because bpf
    190 		 * will only read from the mbuf (i.e., it won't
    191 		 * try to free it or keep a pointer to it).
    192 		 */
    193 		struct mbuf m0;
    194 		u_int af = dst->sa_family;
    195 
    196 		m0.m_next = m;
    197 		m0.m_len = 4;
    198 		m0.m_data = (char *)&af;
    199 
    200 		bpf_mtap(ifp->if_bpf, &m0);
    201 	}
    202 #endif
    203 	m->m_pkthdr.rcvif = ifp;
    204 
    205 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    206 		m_freem(m);
    207 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    208 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    209 	}
    210 
    211 #ifndef PULLDOWN_TEST
    212 	/*
    213 	 * KAME requires that the packet to be contiguous on the
    214 	 * mbuf.  We need to make that sure.
    215 	 * this kind of code should be avoided.
    216 	 * XXX other conditions to avoid running this part?
    217 	 */
    218 	if (m->m_len != m->m_pkthdr.len) {
    219 		struct mbuf *n = NULL;
    220 		int maxlen;
    221 
    222 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    223 		maxlen = MHLEN;
    224 		if (n)
    225 			M_COPY_PKTHDR(n, m);
    226 		if (n && m->m_pkthdr.len > maxlen) {
    227 			MCLGET(n, M_DONTWAIT);
    228 			maxlen = MCLBYTES;
    229 			if ((n->m_flags & M_EXT) == 0) {
    230 				m_free(n);
    231 				n = NULL;
    232 			}
    233 		}
    234 		if (!n) {
    235 			printf("looutput: mbuf allocation failed\n");
    236 			m_freem(m);
    237 			return ENOBUFS;
    238 		}
    239 
    240 		if (m->m_pkthdr.len <= maxlen) {
    241 			m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
    242 			n->m_len = m->m_pkthdr.len;
    243 			n->m_next = NULL;
    244 			m_freem(m);
    245 		} else {
    246 			m_copydata(m, 0, maxlen, mtod(n, caddr_t));
    247 			m_adj(m, maxlen);
    248 			n->m_len = maxlen;
    249 			n->m_next = m;
    250 			m->m_flags &= ~M_PKTHDR;
    251 		}
    252 		m = n;
    253 	}
    254 #if 0
    255 	if (m && m->m_next != NULL) {
    256 		printf("loop: not contiguous...\n");
    257 		m_freem(m);
    258 		return ENOBUFS;
    259 	}
    260 #endif
    261 #endif
    262 
    263 	ifp->if_opackets++;
    264 	ifp->if_obytes += m->m_pkthdr.len;
    265 
    266 #ifdef ALTQ
    267 	/*
    268 	 * ALTQ on the loopback interface is just for debugging.  It's
    269 	 * used only for loopback interfaces, not for a simplex interface.
    270 	 */
    271 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    272 	    ifp->if_start == lostart) {
    273 		struct altq_pktattr pktattr;
    274 		int error;
    275 
    276 		/*
    277 		 * If the queueing discipline needs packet classification,
    278 		 * do it before prepending the link headers.
    279 		 */
    280 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    281 
    282 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    283 		if (m == NULL)
    284 			return (ENOBUFS);
    285 		*(mtod(m, uint32_t *)) = dst->sa_family;
    286 
    287 		s = splimp();
    288 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    289 		(*ifp->if_start)(ifp);
    290 		splx(s);
    291 		return (error);
    292 	}
    293 #endif /* ALTQ */
    294 
    295 	switch (dst->sa_family) {
    296 
    297 #ifdef INET
    298 	case AF_INET:
    299 		ifq = &ipintrq;
    300 		isr = NETISR_IP;
    301 		break;
    302 #endif
    303 #ifdef INET6
    304 	case AF_INET6:
    305 		m->m_flags |= M_LOOP;
    306 		ifq = &ip6intrq;
    307 		isr = NETISR_IPV6;
    308 		break;
    309 #endif
    310 #ifdef NS
    311 	case AF_NS:
    312 		ifq = &nsintrq;
    313 		isr = NETISR_NS;
    314 		break;
    315 #endif
    316 #ifdef ISO
    317 	case AF_ISO:
    318 		ifq = &clnlintrq;
    319 		isr = NETISR_ISO;
    320 		break;
    321 #endif
    322 #ifdef IPX
    323 	case AF_IPX:
    324 		ifq = &ipxintrq;
    325 		isr = NETISR_IPX;
    326 		break;
    327 #endif
    328 #ifdef NETATALK
    329 	case AF_APPLETALK:
    330 	        ifq = &atintrq2;
    331 		isr = NETISR_ATALK;
    332 		break;
    333 #endif
    334 	default:
    335 		printf("%s: can't handle af%d\n", ifp->if_xname,
    336 		    dst->sa_family);
    337 		m_freem(m);
    338 		return (EAFNOSUPPORT);
    339 	}
    340 	s = splimp();
    341 	if (IF_QFULL(ifq)) {
    342 		IF_DROP(ifq);
    343 		m_freem(m);
    344 		splx(s);
    345 		return (ENOBUFS);
    346 	}
    347 	IF_ENQUEUE(ifq, m);
    348 	schednetisr(isr);
    349 	ifp->if_ipackets++;
    350 	ifp->if_ibytes += m->m_pkthdr.len;
    351 	splx(s);
    352 	return (0);
    353 }
    354 
    355 #ifdef ALTQ
    356 void
    357 lostart(struct ifnet *ifp)
    358 {
    359 	struct ifqueue *ifq;
    360 	struct mbuf *m;
    361 	uint32_t af;
    362 	int s, isr;
    363 
    364 	for (;;) {
    365 		IFQ_DEQUEUE(&ifp->if_snd, m);
    366 		if (m == NULL)
    367 			return;
    368 
    369 		af = *(mtod(m, uint32_t *));
    370 		m_adj(m, sizeof(uint32_t));
    371 
    372 		switch (af) {
    373 #ifdef INET
    374 		case AF_INET:
    375 			ifq = &ipintrq;
    376 			isr = NETISR_IP;
    377 			break;
    378 #endif
    379 #ifdef INET6
    380 		case AF_INET6:
    381 			m->m_flags |= M_LOOP;
    382 			ifq = &ip6intrq;
    383 			isr = NETISR_IPV6;
    384 			break;
    385 #endif
    386 #ifdef IPX
    387 		case AF_IPX:
    388 			ifq = &ipxintrq;
    389 			isr = NETISR_IPX;
    390 			break;
    391 #endif
    392 #ifdef NS
    393 		case AF_NS:
    394 			ifq = &nsintrq;
    395 			isr = NETISR_NS;
    396 			break;
    397 #endif
    398 #ifdef ISO
    399 		case AF_ISO:
    400 			ifq = &clnlintrq;
    401 			isr = NETISR_ISO;
    402 			break;
    403 #endif
    404 #ifdef NETATALK
    405 		case AF_APPLETALK:
    406 			ifq = &atintrq2;
    407 			isr = NETISR_ATALK;
    408 			break;
    409 #endif
    410 		default:
    411 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    412 			m_freem(m);
    413 			return;
    414 		}
    415 
    416 		s = splimp();
    417 		if (IF_QFULL(ifq)) {
    418 			IF_DROP(ifq);
    419 			splx(s);
    420 			m_freem(m);
    421 			return;
    422 		}
    423 		IF_ENQUEUE(ifq, m);
    424 		schednetisr(isr);
    425 		ifp->if_ipackets++;
    426 		ifp->if_ibytes += m->m_pkthdr.len;
    427 		splx(s);
    428 	}
    429 }
    430 #endif /* ALTQ */
    431 
    432 /* ARGSUSED */
    433 void
    434 lortrequest(cmd, rt, sa)
    435 	int cmd;
    436 	struct rtentry *rt;
    437 	struct sockaddr *sa;
    438 {
    439 
    440 	if (rt)
    441 		rt->rt_rmx.rmx_mtu = LOMTU;
    442 }
    443 
    444 /*
    445  * Process an ioctl request.
    446  */
    447 /* ARGSUSED */
    448 int
    449 loioctl(ifp, cmd, data)
    450 	struct ifnet *ifp;
    451 	u_long cmd;
    452 	caddr_t data;
    453 {
    454 	struct ifaddr *ifa;
    455 	struct ifreq *ifr;
    456 	int error = 0;
    457 
    458 	switch (cmd) {
    459 
    460 	case SIOCSIFADDR:
    461 		ifp->if_flags |= IFF_UP;
    462 		ifa = (struct ifaddr *)data;
    463 		if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
    464 			ifa->ifa_rtrequest = lortrequest;
    465 		/*
    466 		 * Everything else is done at a higher level.
    467 		 */
    468 		break;
    469 
    470 	case SIOCADDMULTI:
    471 	case SIOCDELMULTI:
    472 		ifr = (struct ifreq *)data;
    473 		if (ifr == 0) {
    474 			error = EAFNOSUPPORT;		/* XXX */
    475 			break;
    476 		}
    477 		switch (ifr->ifr_addr.sa_family) {
    478 
    479 #ifdef INET
    480 		case AF_INET:
    481 			break;
    482 #endif
    483 #ifdef INET6
    484 		case AF_INET6:
    485 			break;
    486 #endif
    487 
    488 		default:
    489 			error = EAFNOSUPPORT;
    490 			break;
    491 		}
    492 		break;
    493 
    494 	default:
    495 		error = EINVAL;
    496 	}
    497 	return (error);
    498 }
    499