Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.90
      1 /*	$NetBSD: if_loop.c,v 1.90 2016/08/07 17:38:34 christos 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.90 2016/08/07 17:38:34 christos Exp $");
     69 
     70 #ifdef _KERNEL_OPT
     71 #include "opt_inet.h"
     72 #include "opt_atalk.h"
     73 #include "opt_mbuftrace.h"
     74 #include "opt_mpls.h"
     75 #include "opt_net_mpsafe.h"
     76 #endif
     77 
     78 #include <sys/param.h>
     79 #include <sys/systm.h>
     80 #include <sys/kernel.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/socket.h>
     83 #include <sys/errno.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/time.h>
     86 #include <sys/device.h>
     87 #include <sys/module.h>
     88 
     89 #include <sys/cpu.h>
     90 
     91 #include <net/if.h>
     92 #include <net/if_types.h>
     93 #include <net/netisr.h>
     94 #include <net/route.h>
     95 
     96 #ifdef	INET
     97 #include <netinet/in.h>
     98 #include <netinet/in_systm.h>
     99 #include <netinet/in_var.h>
    100 #include <netinet/in_offload.h>
    101 #include <netinet/ip.h>
    102 #endif
    103 
    104 #ifdef INET6
    105 #ifndef INET
    106 #include <netinet/in.h>
    107 #endif
    108 #include <netinet6/in6_var.h>
    109 #include <netinet6/in6_offload.h>
    110 #include <netinet/ip6.h>
    111 #endif
    112 
    113 #ifdef MPLS
    114 #include <netmpls/mpls.h>
    115 #include <netmpls/mpls_var.h>
    116 #endif
    117 
    118 #ifdef NETATALK
    119 #include <netatalk/at.h>
    120 #include <netatalk/at_var.h>
    121 #endif
    122 
    123 #include <net/bpf.h>
    124 
    125 #if defined(LARGE_LOMTU)
    126 #define LOMTU	(131072 +  MHLEN + MLEN)
    127 #define LOMTU_MAX LOMTU
    128 #else
    129 #define	LOMTU	(32768 +  MHLEN + MLEN)
    130 #define	LOMTU_MAX	(65536 +  MHLEN + MLEN)
    131 #endif
    132 
    133 #ifdef ALTQ
    134 static void	lostart(struct ifnet *);
    135 #endif
    136 
    137 static int	loop_clone_create(struct if_clone *, int);
    138 static int	loop_clone_destroy(struct ifnet *);
    139 
    140 static struct if_clone loop_cloner =
    141     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
    142 
    143 void
    144 loopattach(int n)
    145 {
    146 
    147 	/*
    148 	 * Nothing to do here, initialization is handled by the
    149 	 * module initialization code in loopnit() below).
    150 	 */
    151 }
    152 
    153 static void
    154 loopinit(void)
    155 {
    156 
    157 	(void)loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    158 	if_clone_attach(&loop_cloner);
    159 }
    160 
    161 static int
    162 loopdetach(void)
    163 {
    164 	/* no detach for now; we don't allow lo0 to be deleted */
    165 	return EBUSY;
    166 }
    167 
    168 static int
    169 loop_clone_create(struct if_clone *ifc, int unit)
    170 {
    171 	struct ifnet *ifp;
    172 
    173 	ifp = if_alloc(IFT_LOOP);
    174 
    175 	if_initname(ifp, ifc->ifc_name, unit);
    176 
    177 	ifp->if_mtu = LOMTU;
    178 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST | IFF_RUNNING;
    179 	ifp->if_extflags = IFEF_OUTPUT_MPSAFE;
    180 	ifp->if_ioctl = loioctl;
    181 	ifp->if_output = looutput;
    182 #ifdef ALTQ
    183 	ifp->if_start = lostart;
    184 #endif
    185 	ifp->if_type = IFT_LOOP;
    186 	ifp->if_hdrlen = 0;
    187 	ifp->if_addrlen = 0;
    188 	ifp->if_dlt = DLT_NULL;
    189 	IFQ_SET_READY(&ifp->if_snd);
    190 	if (unit == 0)
    191 		lo0ifp = ifp;
    192 	if_attach(ifp);
    193 	if_alloc_sadl(ifp);
    194 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
    195 #ifdef MBUFTRACE
    196 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    197 	    M_WAITOK | M_ZERO);
    198 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    199 	    sizeof(ifp->if_mowner->mo_name));
    200 	MOWNER_ATTACH(ifp->if_mowner);
    201 #endif
    202 
    203 	return (0);
    204 }
    205 
    206 static int
    207 loop_clone_destroy(struct ifnet *ifp)
    208 {
    209 
    210 	if (ifp == lo0ifp)
    211 		return (EPERM);
    212 
    213 #ifdef MBUFTRACE
    214 	MOWNER_DETACH(ifp->if_mowner);
    215 	free(ifp->if_mowner, M_DEVBUF);
    216 #endif
    217 
    218 	bpf_detach(ifp);
    219 	if_detach(ifp);
    220 
    221 	if_free(ifp);
    222 
    223 	return (0);
    224 }
    225 
    226 int
    227 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    228     const struct rtentry *rt)
    229 {
    230 	pktqueue_t *pktq = NULL;
    231 	struct ifqueue *ifq = NULL;
    232 	int s, isr = -1;
    233 	int csum_flags;
    234 	int error = 0;
    235 	size_t pktlen;
    236 
    237 	MCLAIM(m, ifp->if_mowner);
    238 
    239 	KERNEL_LOCK(1, NULL);
    240 
    241 	if ((m->m_flags & M_PKTHDR) == 0)
    242 		panic("looutput: no header mbuf");
    243 	if (ifp->if_flags & IFF_LOOPBACK)
    244 		bpf_mtap_af(ifp, dst->sa_family, m);
    245 	m_set_rcvif(m, ifp);
    246 
    247 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    248 		m_freem(m);
    249 		error = (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    250 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    251 		goto out;
    252 	}
    253 
    254 	pktlen = m->m_pkthdr.len;
    255 	ifp->if_opackets++;
    256 	ifp->if_obytes += pktlen;
    257 
    258 #ifdef ALTQ
    259 	/*
    260 	 * ALTQ on the loopback interface is just for debugging.  It's
    261 	 * used only for loopback interfaces, not for a simplex interface.
    262 	 */
    263 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    264 	    ifp->if_start == lostart) {
    265 		/*
    266 		 * If the queueing discipline needs packet classification,
    267 		 * do it before prepending the link headers.
    268 		 */
    269 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    270 
    271 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    272 		if (m == NULL) {
    273 			error = ENOBUFS;
    274 			goto out;
    275 		}
    276 		*(mtod(m, uint32_t *)) = dst->sa_family;
    277 
    278 		error = if_transmit_lock(ifp, m);
    279 		goto out;
    280 	}
    281 #endif /* ALTQ */
    282 
    283 	m_tag_delete_nonpersistent(m);
    284 
    285 #ifdef MPLS
    286 	if (rt != NULL && rt_gettag(rt) != NULL &&
    287 	    rt_gettag(rt)->sa_family == AF_MPLS &&
    288 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
    289 		union mpls_shim msh;
    290 		msh.s_addr = MPLS_GETSADDR(rt);
    291 		if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    292 			ifq = &mplsintrq;
    293 			isr = NETISR_MPLS;
    294 		}
    295 	}
    296 	if (isr != NETISR_MPLS)
    297 #endif
    298 	switch (dst->sa_family) {
    299 
    300 #ifdef INET
    301 	case AF_INET:
    302 		csum_flags = m->m_pkthdr.csum_flags;
    303 		KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
    304 		if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    305 			ip_undefer_csum(m, 0, csum_flags);
    306 		}
    307 		m->m_pkthdr.csum_flags = 0;
    308 		pktq = ip_pktq;
    309 		break;
    310 #endif
    311 #ifdef INET6
    312 	case AF_INET6:
    313 		csum_flags = m->m_pkthdr.csum_flags;
    314 		KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
    315 		if (csum_flags != 0 &&
    316 		    IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    317 			ip6_undefer_csum(m, 0, csum_flags);
    318 		}
    319 		m->m_pkthdr.csum_flags = 0;
    320 		m->m_flags |= M_LOOP;
    321 		pktq = ip6_pktq;
    322 		break;
    323 #endif
    324 #ifdef NETATALK
    325 	case AF_APPLETALK:
    326 	        ifq = &atintrq2;
    327 		isr = NETISR_ATALK;
    328 		break;
    329 #endif
    330 	default:
    331 		printf("%s: can't handle af%d\n", ifp->if_xname,
    332 		    dst->sa_family);
    333 		m_freem(m);
    334 		error = EAFNOSUPPORT;
    335 		goto out;
    336 	}
    337 
    338 	s = splnet();
    339 	if (__predict_true(pktq)) {
    340 		error = 0;
    341 
    342 		if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    343 			ifp->if_ipackets++;
    344 			ifp->if_ibytes += pktlen;
    345 		} else {
    346 			m_freem(m);
    347 			error = ENOBUFS;
    348 		}
    349 		splx(s);
    350 		goto out;
    351 	}
    352 	if (IF_QFULL(ifq)) {
    353 		IF_DROP(ifq);
    354 		m_freem(m);
    355 		splx(s);
    356 		error = ENOBUFS;
    357 		goto out;
    358 	}
    359 	IF_ENQUEUE(ifq, m);
    360 	schednetisr(isr);
    361 	ifp->if_ipackets++;
    362 	ifp->if_ibytes += m->m_pkthdr.len;
    363 	splx(s);
    364 out:
    365 	KERNEL_UNLOCK_ONE(NULL);
    366 	return error;
    367 }
    368 
    369 #ifdef ALTQ
    370 static void
    371 lostart(struct ifnet *ifp)
    372 {
    373 	for (;;) {
    374 		pktqueue_t *pktq = NULL;
    375 		struct ifqueue *ifq = NULL;
    376 		struct mbuf *m;
    377 		size_t pktlen;
    378 		uint32_t af;
    379 		int s, isr = 0;
    380 
    381 		IFQ_DEQUEUE(&ifp->if_snd, m);
    382 		if (m == NULL)
    383 			return;
    384 
    385 		af = *(mtod(m, uint32_t *));
    386 		m_adj(m, sizeof(uint32_t));
    387 
    388 		switch (af) {
    389 #ifdef INET
    390 		case AF_INET:
    391 			pktq = ip_pktq;
    392 			break;
    393 #endif
    394 #ifdef INET6
    395 		case AF_INET6:
    396 			m->m_flags |= M_LOOP;
    397 			pktq = ip6_pktq;
    398 			break;
    399 #endif
    400 #ifdef NETATALK
    401 		case AF_APPLETALK:
    402 			ifq = &atintrq2;
    403 			isr = NETISR_ATALK;
    404 			break;
    405 #endif
    406 		default:
    407 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    408 			m_freem(m);
    409 			return;
    410 		}
    411 		pktlen = m->m_pkthdr.len;
    412 
    413 		s = splnet();
    414 		if (__predict_true(pktq)) {
    415 			if (__predict_false(pktq_enqueue(pktq, m, 0))) {
    416 				m_freem(m);
    417 				splx(s);
    418 				return;
    419 			}
    420 			ifp->if_ipackets++;
    421 			ifp->if_ibytes += pktlen;
    422 			splx(s);
    423 			continue;
    424 		}
    425 		if (IF_QFULL(ifq)) {
    426 			IF_DROP(ifq);
    427 			splx(s);
    428 			m_freem(m);
    429 			return;
    430 		}
    431 		IF_ENQUEUE(ifq, m);
    432 		schednetisr(isr);
    433 		ifp->if_ipackets++;
    434 		ifp->if_ibytes += pktlen;
    435 		splx(s);
    436 	}
    437 }
    438 #endif /* ALTQ */
    439 
    440 /* ARGSUSED */
    441 void
    442 lortrequest(int cmd, struct rtentry *rt,
    443     const struct rt_addrinfo *info)
    444 {
    445 
    446 	if (rt)
    447 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
    448 }
    449 
    450 /*
    451  * Process an ioctl request.
    452  */
    453 /* ARGSUSED */
    454 int
    455 loioctl(struct ifnet *ifp, u_long cmd, void *data)
    456 {
    457 	struct ifaddr *ifa;
    458 	struct ifreq *ifr = data;
    459 	int error = 0;
    460 
    461 	switch (cmd) {
    462 
    463 	case SIOCINITIFADDR:
    464 		ifp->if_flags |= IFF_UP;
    465 		ifa = (struct ifaddr *)data;
    466 		if (ifa != NULL)
    467 			ifa->ifa_rtrequest = lortrequest;
    468 		/*
    469 		 * Everything else is done at a higher level.
    470 		 */
    471 		break;
    472 
    473 	case SIOCSIFMTU:
    474 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
    475 			error = EINVAL;
    476 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
    477 			error = 0;
    478 		}
    479 		break;
    480 
    481 	case SIOCADDMULTI:
    482 	case SIOCDELMULTI:
    483 		if (ifr == NULL) {
    484 			error = EAFNOSUPPORT;		/* XXX */
    485 			break;
    486 		}
    487 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    488 
    489 #ifdef INET
    490 		case AF_INET:
    491 			break;
    492 #endif
    493 #ifdef INET6
    494 		case AF_INET6:
    495 			break;
    496 #endif
    497 
    498 		default:
    499 			error = EAFNOSUPPORT;
    500 			break;
    501 		}
    502 		break;
    503 
    504 	default:
    505 		error = ifioctl_common(ifp, cmd, data);
    506 	}
    507 	return (error);
    508 }
    509 
    510 /*
    511  * Module infrastructure
    512  */
    513 #include "if_module.h"
    514 
    515 IF_MODULE(MODULE_CLASS_DRIVER, loop, "")
    516