Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.80
      1 /*	$NetBSD: if_loop.c,v 1.80 2014/06/07 11:00:29 rmind 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. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
     61  */
     62 
     63 /*
     64  * Loopback interface driver for protocol testing and timing.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.80 2014/06/07 11:00:29 rmind Exp $");
     69 
     70 #include "opt_inet.h"
     71 #include "opt_atalk.h"
     72 #include "opt_ipx.h"
     73 #include "opt_mbuftrace.h"
     74 #include "opt_mpls.h"
     75 
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/kernel.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/socket.h>
     82 #include <sys/errno.h>
     83 #include <sys/ioctl.h>
     84 #include <sys/time.h>
     85 
     86 #include <sys/cpu.h>
     87 
     88 #include <net/if.h>
     89 #include <net/if_types.h>
     90 #include <net/netisr.h>
     91 #include <net/route.h>
     92 
     93 #ifdef	INET
     94 #include <netinet/in.h>
     95 #include <netinet/in_systm.h>
     96 #include <netinet/in_var.h>
     97 #include <netinet/in_offload.h>
     98 #include <netinet/ip.h>
     99 #endif
    100 
    101 #ifdef INET6
    102 #ifndef INET
    103 #include <netinet/in.h>
    104 #endif
    105 #include <netinet6/in6_var.h>
    106 #include <netinet6/in6_offload.h>
    107 #include <netinet/ip6.h>
    108 #endif
    109 
    110 #ifdef IPX
    111 #include <netipx/ipx.h>
    112 #include <netipx/ipx_if.h>
    113 #endif
    114 
    115 #ifdef MPLS
    116 #include <netmpls/mpls.h>
    117 #include <netmpls/mpls_var.h>
    118 #endif
    119 
    120 #ifdef NETATALK
    121 #include <netatalk/at.h>
    122 #include <netatalk/at_var.h>
    123 #endif
    124 
    125 #include <net/bpf.h>
    126 
    127 #if defined(LARGE_LOMTU)
    128 #define LOMTU	(131072 +  MHLEN + MLEN)
    129 #define LOMTU_MAX LOMTU
    130 #else
    131 #define	LOMTU	(32768 +  MHLEN + MLEN)
    132 #define	LOMTU_MAX	(65536 +  MHLEN + MLEN)
    133 #endif
    134 
    135 #ifdef ALTQ
    136 static void	lostart(struct ifnet *);
    137 #endif
    138 
    139 static int	loop_clone_create(struct if_clone *, int);
    140 static int	loop_clone_destroy(struct ifnet *);
    141 
    142 static struct if_clone loop_cloner =
    143     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
    144 
    145 void
    146 loopattach(int n)
    147 {
    148 
    149 	(void)loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    150 	if_clone_attach(&loop_cloner);
    151 }
    152 
    153 static int
    154 loop_clone_create(struct if_clone *ifc, int unit)
    155 {
    156 	struct ifnet *ifp;
    157 
    158 	ifp = if_alloc(IFT_LOOP);
    159 
    160 	if_initname(ifp, ifc->ifc_name, unit);
    161 
    162 	ifp->if_mtu = LOMTU;
    163 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST | IFF_RUNNING;
    164 	ifp->if_ioctl = loioctl;
    165 	ifp->if_output = looutput;
    166 #ifdef ALTQ
    167 	ifp->if_start = lostart;
    168 #endif
    169 	ifp->if_type = IFT_LOOP;
    170 	ifp->if_hdrlen = 0;
    171 	ifp->if_addrlen = 0;
    172 	ifp->if_dlt = DLT_NULL;
    173 	IFQ_SET_READY(&ifp->if_snd);
    174 	if (unit == 0)
    175 		lo0ifp = ifp;
    176 	if_attach(ifp);
    177 	if_alloc_sadl(ifp);
    178 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
    179 #ifdef MBUFTRACE
    180 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    181 	    M_WAITOK | M_ZERO);
    182 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    183 	    sizeof(ifp->if_mowner->mo_name));
    184 	MOWNER_ATTACH(ifp->if_mowner);
    185 #endif
    186 
    187 	return (0);
    188 }
    189 
    190 static int
    191 loop_clone_destroy(struct ifnet *ifp)
    192 {
    193 
    194 	if (ifp == lo0ifp)
    195 		return (EPERM);
    196 
    197 #ifdef MBUFTRACE
    198 	MOWNER_DETACH(ifp->if_mowner);
    199 	free(ifp->if_mowner, M_DEVBUF);
    200 #endif
    201 
    202 	bpf_detach(ifp);
    203 	if_detach(ifp);
    204 
    205 	if_free(ifp);
    206 
    207 	return (0);
    208 }
    209 
    210 int
    211 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    212     struct rtentry *rt)
    213 {
    214 	pktqueue_t *pktq = NULL;
    215 	struct ifqueue *ifq = NULL;
    216 	int s, isr = -1;
    217 	int csum_flags;
    218 	size_t pktlen;
    219 
    220 	MCLAIM(m, ifp->if_mowner);
    221 	KASSERT(KERNEL_LOCKED_P());
    222 
    223 	if ((m->m_flags & M_PKTHDR) == 0)
    224 		panic("looutput: no header mbuf");
    225 	if (ifp->if_flags & IFF_LOOPBACK)
    226 		bpf_mtap_af(ifp, dst->sa_family, m);
    227 	m->m_pkthdr.rcvif = ifp;
    228 
    229 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    230 		m_freem(m);
    231 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    232 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    233 	}
    234 
    235 	pktlen = m->m_pkthdr.len;
    236 	ifp->if_opackets++;
    237 	ifp->if_obytes += pktlen;
    238 
    239 #ifdef ALTQ
    240 	/*
    241 	 * ALTQ on the loopback interface is just for debugging.  It's
    242 	 * used only for loopback interfaces, not for a simplex interface.
    243 	 */
    244 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    245 	    ifp->if_start == lostart) {
    246 		struct altq_pktattr pktattr;
    247 		int error;
    248 
    249 		/*
    250 		 * If the queueing discipline needs packet classification,
    251 		 * do it before prepending the link headers.
    252 		 */
    253 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
    254 
    255 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    256 		if (m == NULL)
    257 			return (ENOBUFS);
    258 		*(mtod(m, uint32_t *)) = dst->sa_family;
    259 
    260 		s = splnet();
    261 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
    262 		(*ifp->if_start)(ifp);
    263 		splx(s);
    264 		return (error);
    265 	}
    266 #endif /* ALTQ */
    267 
    268 	m_tag_delete_nonpersistent(m);
    269 
    270 #ifdef MPLS
    271 	if (rt != NULL && rt_gettag(rt) != NULL &&
    272 	    rt_gettag(rt)->sa_family == AF_MPLS &&
    273 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
    274 		union mpls_shim msh;
    275 		msh.s_addr = MPLS_GETSADDR(rt);
    276 		if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    277 			ifq = &mplsintrq;
    278 			isr = NETISR_MPLS;
    279 		}
    280 	}
    281 	if (isr != NETISR_MPLS)
    282 #endif
    283 	switch (dst->sa_family) {
    284 
    285 #ifdef INET
    286 	case AF_INET:
    287 		csum_flags = m->m_pkthdr.csum_flags;
    288 		KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
    289 		if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    290 			ip_undefer_csum(m, 0, csum_flags);
    291 		}
    292 		m->m_pkthdr.csum_flags = 0;
    293 		pktq = ip_pktq;
    294 		break;
    295 #endif
    296 #ifdef INET6
    297 	case AF_INET6:
    298 		csum_flags = m->m_pkthdr.csum_flags;
    299 		KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
    300 		if (csum_flags != 0 &&
    301 		    IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    302 			ip6_undefer_csum(m, 0, csum_flags);
    303 		}
    304 		m->m_pkthdr.csum_flags = 0;
    305 		m->m_flags |= M_LOOP;
    306 		pktq = ip6_pktq;
    307 		break;
    308 #endif
    309 #ifdef IPX
    310 	case AF_IPX:
    311 		ifq = &ipxintrq;
    312 		isr = NETISR_IPX;
    313 		break;
    314 #endif
    315 #ifdef NETATALK
    316 	case AF_APPLETALK:
    317 	        ifq = &atintrq2;
    318 		isr = NETISR_ATALK;
    319 		break;
    320 #endif
    321 	default:
    322 		printf("%s: can't handle af%d\n", ifp->if_xname,
    323 		    dst->sa_family);
    324 		m_freem(m);
    325 		return (EAFNOSUPPORT);
    326 	}
    327 
    328 	s = splnet();
    329 	if (__predict_true(pktq)) {
    330 		int error = 0;
    331 
    332 		if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    333 			ifp->if_ipackets++;
    334 			ifp->if_ibytes += pktlen;
    335 		} else {
    336 			m_freem(m);
    337 			error = ENOBUFS;
    338 		}
    339 		splx(s);
    340 		return error;
    341 	}
    342 	if (IF_QFULL(ifq)) {
    343 		IF_DROP(ifq);
    344 		m_freem(m);
    345 		splx(s);
    346 		return (ENOBUFS);
    347 	}
    348 	IF_ENQUEUE(ifq, m);
    349 	schednetisr(isr);
    350 	ifp->if_ipackets++;
    351 	ifp->if_ibytes += m->m_pkthdr.len;
    352 	splx(s);
    353 	return (0);
    354 }
    355 
    356 #ifdef ALTQ
    357 static void
    358 lostart(struct ifnet *ifp)
    359 {
    360 	for (;;) {
    361 		pktqueue_t *pktq = NULL;
    362 		struct ifqueue *ifq = NULL;
    363 		struct mbuf *m;
    364 		size_t pktlen;
    365 		uint32_t af;
    366 		int s, isr = 0;
    367 
    368 		IFQ_DEQUEUE(&ifp->if_snd, m);
    369 		if (m == NULL)
    370 			return;
    371 
    372 		af = *(mtod(m, uint32_t *));
    373 		m_adj(m, sizeof(uint32_t));
    374 
    375 		switch (af) {
    376 #ifdef INET
    377 		case AF_INET:
    378 			pktq = ip_pktq;
    379 			break;
    380 #endif
    381 #ifdef INET6
    382 		case AF_INET6:
    383 			m->m_flags |= M_LOOP;
    384 			pktq = ip6_pktq;
    385 			break;
    386 #endif
    387 #ifdef IPX
    388 		case AF_IPX:
    389 			ifq = &ipxintrq;
    390 			isr = NETISR_IPX;
    391 			break;
    392 #endif
    393 #ifdef NETATALK
    394 		case AF_APPLETALK:
    395 			ifq = &atintrq2;
    396 			isr = NETISR_ATALK;
    397 			break;
    398 #endif
    399 		default:
    400 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    401 			m_freem(m);
    402 			return;
    403 		}
    404 		pktlen = m->m_pkthdr.len;
    405 
    406 		s = splnet();
    407 		if (__predict_true(pktq)) {
    408 			if (__predict_false(pktq_enqueue(pktq, m, 0))) {
    409 				m_freem(m);
    410 				splx(s);
    411 				return;
    412 			}
    413 			ifp->if_ipackets++;
    414 			ifp->if_ibytes += pktlen;
    415 			splx(s);
    416 			continue;
    417 		}
    418 		if (IF_QFULL(ifq)) {
    419 			IF_DROP(ifq);
    420 			splx(s);
    421 			m_freem(m);
    422 			return;
    423 		}
    424 		IF_ENQUEUE(ifq, m);
    425 		schednetisr(isr);
    426 		ifp->if_ipackets++;
    427 		ifp->if_ibytes += pktlen;
    428 		splx(s);
    429 	}
    430 }
    431 #endif /* ALTQ */
    432 
    433 /* ARGSUSED */
    434 void
    435 lortrequest(int cmd, struct rtentry *rt,
    436     const struct rt_addrinfo *info)
    437 {
    438 
    439 	if (rt)
    440 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
    441 }
    442 
    443 /*
    444  * Process an ioctl request.
    445  */
    446 /* ARGSUSED */
    447 int
    448 loioctl(struct ifnet *ifp, u_long cmd, void *data)
    449 {
    450 	struct ifaddr *ifa;
    451 	struct ifreq *ifr = data;
    452 	int error = 0;
    453 
    454 	switch (cmd) {
    455 
    456 	case SIOCINITIFADDR:
    457 		ifp->if_flags |= IFF_UP;
    458 		ifa = (struct ifaddr *)data;
    459 		if (ifa != NULL)
    460 			ifa->ifa_rtrequest = lortrequest;
    461 		/*
    462 		 * Everything else is done at a higher level.
    463 		 */
    464 		break;
    465 
    466 	case SIOCSIFMTU:
    467 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
    468 			error = EINVAL;
    469 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
    470 			error = 0;
    471 		}
    472 		break;
    473 
    474 	case SIOCADDMULTI:
    475 	case SIOCDELMULTI:
    476 		if (ifr == NULL) {
    477 			error = EAFNOSUPPORT;		/* XXX */
    478 			break;
    479 		}
    480 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    481 
    482 #ifdef INET
    483 		case AF_INET:
    484 			break;
    485 #endif
    486 #ifdef INET6
    487 		case AF_INET6:
    488 			break;
    489 #endif
    490 
    491 		default:
    492 			error = EAFNOSUPPORT;
    493 			break;
    494 		}
    495 		break;
    496 
    497 	default:
    498 		error = ifioctl_common(ifp, cmd, data);
    499 	}
    500 	return (error);
    501 }
    502