Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.34
      1 /*	$NetBSD: if_loop.c,v 1.34 2000/12/18 21:13:14 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 #ifdef ALTQ
    143 void	lostart(struct ifnet *);
    144 #endif
    145 
    146 void
    147 loopattach(n)
    148 	int n;
    149 {
    150 	int i;
    151 	struct ifnet *ifp;
    152 
    153 	for (i = 0; i < NLOOP; i++) {
    154 		ifp = &loif[i];
    155 		sprintf(ifp->if_xname, "lo%d", i);
    156 		ifp->if_softc = NULL;
    157 		ifp->if_mtu = LOMTU;
    158 		ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
    159 		ifp->if_ioctl = loioctl;
    160 		ifp->if_output = looutput;
    161 #ifdef ALTQ
    162 		ifp->if_start = lostart;
    163 #endif
    164 		ifp->if_type = IFT_LOOP;
    165 		ifp->if_hdrlen = 0;
    166 		ifp->if_addrlen = 0;
    167 		ifp->if_dlt = DLT_NULL;
    168 		IFQ_SET_READY(&ifp->if_snd);
    169 		if_attach(ifp);
    170 #if NBPFILTER > 0
    171 		bpfattach(ifp, DLT_NULL, sizeof(u_int));
    172 #endif
    173 	}
    174 }
    175 
    176 int
    177 looutput(ifp, m, dst, rt)
    178 	struct ifnet *ifp;
    179 	struct mbuf *m;
    180 	struct sockaddr *dst;
    181 	struct rtentry *rt;
    182 {
    183 	int s, isr;
    184 	struct ifqueue *ifq = 0;
    185 
    186 	if ((m->m_flags & M_PKTHDR) == 0)
    187 		panic("looutput: no header mbuf");
    188 	ifp->if_lastchange = time;
    189 #if NBPFILTER > 0
    190 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK)) {
    191 		/*
    192 		 * We need to prepend the address family as
    193 		 * a four byte field.  Cons up a dummy header
    194 		 * to pacify bpf.  This is safe because bpf
    195 		 * will only read from the mbuf (i.e., it won't
    196 		 * try to free it or keep a pointer to it).
    197 		 */
    198 		struct mbuf m0;
    199 		u_int af = dst->sa_family;
    200 
    201 		m0.m_next = m;
    202 		m0.m_len = 4;
    203 		m0.m_data = (char *)&af;
    204 
    205 		bpf_mtap(ifp->if_bpf, &m0);
    206 	}
    207 #endif
    208 	m->m_pkthdr.rcvif = ifp;
    209 
    210 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    211 		m_freem(m);
    212 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    213 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    214 	}
    215 
    216 #ifndef PULLDOWN_TEST
    217 	/*
    218 	 * KAME requires that the packet to be contiguous on the
    219 	 * mbuf.  We need to make that sure.
    220 	 * this kind of code should be avoided.
    221 	 * XXX other conditions to avoid running this part?
    222 	 */
    223 	if (m->m_len != m->m_pkthdr.len) {
    224 		struct mbuf *n = NULL;
    225 		int maxlen;
    226 
    227 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    228 		maxlen = MHLEN;
    229 		if (n)
    230 			M_COPY_PKTHDR(n, m);
    231 		if (n && m->m_pkthdr.len > maxlen) {
    232 			MCLGET(n, M_DONTWAIT);
    233 			maxlen = MCLBYTES;
    234 			if ((n->m_flags & M_EXT) == 0) {
    235 				m_free(n);
    236 				n = NULL;
    237 			}
    238 		}
    239 		if (!n) {
    240 			printf("looutput: mbuf allocation failed\n");
    241 			m_freem(m);
    242 			return ENOBUFS;
    243 		}
    244 
    245 		if (m->m_pkthdr.len <= maxlen) {
    246 			m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
    247 			n->m_len = m->m_pkthdr.len;
    248 			n->m_next = NULL;
    249 			m_freem(m);
    250 		} else {
    251 			m_copydata(m, 0, maxlen, mtod(n, caddr_t));
    252 			m_adj(m, maxlen);
    253 			n->m_len = maxlen;
    254 			n->m_next = m;
    255 			m->m_flags &= ~M_PKTHDR;
    256 		}
    257 		m = n;
    258 	}
    259 #if 0
    260 	if (m && m->m_next != NULL) {
    261 		printf("loop: not contiguous...\n");
    262 		m_freem(m);
    263 		return ENOBUFS;
    264 	}
    265 #endif
    266 #endif
    267 
    268 	ifp->if_opackets++;
    269 	ifp->if_obytes += m->m_pkthdr.len;
    270 
    271 #ifdef ALTQ
    272 	/*
    273 	 * ALTQ on the loopback interface is just for debugging.  It's
    274 	 * used only for loopback interfaces, not for a simplex interface.
    275 	 */
    276 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    277 	    ifp->if_start == lostart) {
    278 		struct altq_pktattr pktattr;
    279 		int error;
    280 
    281 		/*
    282 		 * If the queueing discipline needs packet classification,
    283 		 * do it before prepending the link headers.
    284 		 */
    285 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    286 
    287 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    288 		if (m == NULL)
    289 			return (ENOBUFS);
    290 		*(mtod(m, uint32_t *)) = dst->sa_family;
    291 
    292 		s = splimp();
    293 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    294 		(*ifp->if_start)(ifp);
    295 		splx(s);
    296 		return (error);
    297 	}
    298 #endif /* ALTQ */
    299 
    300 	switch (dst->sa_family) {
    301 
    302 #ifdef INET
    303 	case AF_INET:
    304 		ifq = &ipintrq;
    305 		isr = NETISR_IP;
    306 		break;
    307 #endif
    308 #ifdef INET6
    309 	case AF_INET6:
    310 		m->m_flags |= M_LOOP;
    311 		ifq = &ip6intrq;
    312 		isr = NETISR_IPV6;
    313 		break;
    314 #endif
    315 #ifdef NS
    316 	case AF_NS:
    317 		ifq = &nsintrq;
    318 		isr = NETISR_NS;
    319 		break;
    320 #endif
    321 #ifdef ISO
    322 	case AF_ISO:
    323 		ifq = &clnlintrq;
    324 		isr = NETISR_ISO;
    325 		break;
    326 #endif
    327 #ifdef IPX
    328 	case AF_IPX:
    329 		ifq = &ipxintrq;
    330 		isr = NETISR_IPX;
    331 		break;
    332 #endif
    333 #ifdef NETATALK
    334 	case AF_APPLETALK:
    335 	        ifq = &atintrq2;
    336 		isr = NETISR_ATALK;
    337 		break;
    338 #endif
    339 	default:
    340 		printf("%s: can't handle af%d\n", ifp->if_xname,
    341 		    dst->sa_family);
    342 		m_freem(m);
    343 		return (EAFNOSUPPORT);
    344 	}
    345 	s = splimp();
    346 	if (IF_QFULL(ifq)) {
    347 		IF_DROP(ifq);
    348 		m_freem(m);
    349 		splx(s);
    350 		return (ENOBUFS);
    351 	}
    352 	IF_ENQUEUE(ifq, m);
    353 	schednetisr(isr);
    354 	ifp->if_ipackets++;
    355 	ifp->if_ibytes += m->m_pkthdr.len;
    356 	splx(s);
    357 	return (0);
    358 }
    359 
    360 #ifdef ALTQ
    361 void
    362 lostart(struct ifnet *ifp)
    363 {
    364 	struct ifqueue *ifq;
    365 	struct mbuf *m;
    366 	uint32_t af;
    367 	int s, isr;
    368 
    369 	for (;;) {
    370 		IFQ_DEQUEUE(&ifp->if_snd, m);
    371 		if (m == NULL)
    372 			return;
    373 
    374 		af = *(mtod(m, uint32_t *));
    375 		m_adj(m, sizeof(uint32_t));
    376 
    377 		switch (af) {
    378 #ifdef INET
    379 		case AF_INET:
    380 			ifq = &ipintrq;
    381 			isr = NETISR_IP;
    382 			break;
    383 #endif
    384 #ifdef INET6
    385 		case AF_INET6:
    386 			m->m_flags |= M_LOOP;
    387 			ifq = &ip6intrq;
    388 			isr = NETISR_IPV6;
    389 			break;
    390 #endif
    391 #ifdef IPX
    392 		case AF_IPX:
    393 			ifq = &ipxintrq;
    394 			isr = NETISR_IPX;
    395 			break;
    396 #endif
    397 #ifdef NS
    398 		case AF_NS:
    399 			ifq = &nsintrq;
    400 			isr = NETISR_NS;
    401 			break;
    402 #endif
    403 #ifdef ISO
    404 		case AF_ISO:
    405 			ifq = &clnlintrq;
    406 			isr = NETISR_ISO;
    407 			break;
    408 #endif
    409 #ifdef NETATALK
    410 		case AF_APPLETALK:
    411 			ifq = &atintrq2;
    412 			isr = NETISR_ATALK;
    413 			break;
    414 #endif
    415 		default:
    416 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    417 			m_freem(m);
    418 			return;
    419 		}
    420 
    421 		s = splimp();
    422 		if (IF_QFULL(ifq)) {
    423 			IF_DROP(ifq);
    424 			splx(s);
    425 			m_freem(m);
    426 			return;
    427 		}
    428 		IF_ENQUEUE(ifq, m);
    429 		schednetisr(isr);
    430 		ifp->if_ipackets++;
    431 		ifp->if_ibytes += m->m_pkthdr.len;
    432 		splx(s);
    433 	}
    434 }
    435 #endif /* ALTQ */
    436 
    437 /* ARGSUSED */
    438 void
    439 lortrequest(cmd, rt, sa)
    440 	int cmd;
    441 	struct rtentry *rt;
    442 	struct sockaddr *sa;
    443 {
    444 
    445 	if (rt)
    446 		rt->rt_rmx.rmx_mtu = LOMTU;
    447 }
    448 
    449 /*
    450  * Process an ioctl request.
    451  */
    452 /* ARGSUSED */
    453 int
    454 loioctl(ifp, cmd, data)
    455 	struct ifnet *ifp;
    456 	u_long cmd;
    457 	caddr_t data;
    458 {
    459 	struct ifaddr *ifa;
    460 	struct ifreq *ifr;
    461 	int error = 0;
    462 
    463 	switch (cmd) {
    464 
    465 	case SIOCSIFADDR:
    466 		ifp->if_flags |= IFF_UP;
    467 		ifa = (struct ifaddr *)data;
    468 		if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
    469 			ifa->ifa_rtrequest = lortrequest;
    470 		/*
    471 		 * Everything else is done at a higher level.
    472 		 */
    473 		break;
    474 
    475 	case SIOCADDMULTI:
    476 	case SIOCDELMULTI:
    477 		ifr = (struct ifreq *)data;
    478 		if (ifr == 0) {
    479 			error = EAFNOSUPPORT;		/* XXX */
    480 			break;
    481 		}
    482 		switch (ifr->ifr_addr.sa_family) {
    483 
    484 #ifdef INET
    485 		case AF_INET:
    486 			break;
    487 #endif
    488 #ifdef INET6
    489 		case AF_INET6:
    490 			break;
    491 #endif
    492 
    493 		default:
    494 			error = EAFNOSUPPORT;
    495 			break;
    496 		}
    497 		break;
    498 
    499 	default:
    500 		error = EINVAL;
    501 	}
    502 	return (error);
    503 }
    504