Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.114
      1 /*	$NetBSD: if_loop.c,v 1.114 2022/07/31 13:14:54 mlelstv 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.114 2022/07/31 13:14:54 mlelstv 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 void	loop_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
    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 #ifndef _MODULE
    150 	loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    151 #endif
    152 }
    153 
    154 void
    155 loopinit(void)
    156 {
    157 
    158 	if (lo0ifp != NULL)	/* can happen in rump kernel */
    159 		return;
    160 
    161 #ifdef _MODULE
    162 	loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    163 #endif
    164 	if_clone_attach(&loop_cloner);
    165 }
    166 
    167 static int
    168 loopdetach(void)
    169 {
    170 	/* no detach for now; we don't allow lo0 to be deleted */
    171 	return EBUSY;
    172 }
    173 
    174 static int
    175 loop_clone_create(struct if_clone *ifc, int unit)
    176 {
    177 	struct ifnet *ifp;
    178 
    179 	ifp = if_alloc(IFT_LOOP);
    180 
    181 	if_initname(ifp, ifc->ifc_name, unit);
    182 
    183 	ifp->if_mtu = LOMTU;
    184 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
    185 #ifdef NET_MPSAFE
    186 	ifp->if_extflags = IFEF_MPSAFE;
    187 #endif
    188 	ifp->if_ioctl = loioctl;
    189 	ifp->if_output = looutput;
    190 #ifdef ALTQ
    191 	ifp->if_start = lostart;
    192 #endif
    193 	ifp->if_type = IFT_LOOP;
    194 	ifp->if_hdrlen = 0;
    195 	ifp->if_addrlen = 0;
    196 	ifp->if_dlt = DLT_NULL;
    197 	IFQ_SET_READY(&ifp->if_snd);
    198 	if (unit == 0)
    199 		lo0ifp = ifp;
    200 	if_initialize(ifp);
    201 	ifp->if_link_state = LINK_STATE_UP;
    202 	if_alloc_sadl(ifp);
    203 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
    204 #ifdef MBUFTRACE
    205 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    206 	    M_WAITOK | M_ZERO);
    207 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    208 	    sizeof(ifp->if_mowner->mo_name));
    209 	MOWNER_ATTACH(ifp->if_mowner);
    210 #endif
    211 
    212 	ifp->if_flags |= IFF_RUNNING;
    213 	if_register(ifp);
    214 
    215 	return (0);
    216 }
    217 
    218 static int
    219 loop_clone_destroy(struct ifnet *ifp)
    220 {
    221 
    222 	if (ifp == lo0ifp)
    223 		return (EPERM);
    224 
    225 	ifp->if_flags &= ~IFF_RUNNING;
    226 
    227 #ifdef MBUFTRACE
    228 	MOWNER_DETACH(ifp->if_mowner);
    229 	free(ifp->if_mowner, M_DEVBUF);
    230 #endif
    231 
    232 	bpf_detach(ifp);
    233 	if_detach(ifp);
    234 
    235 	if_free(ifp);
    236 
    237 	return (0);
    238 }
    239 
    240 int
    241 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    242     const struct rtentry *rt)
    243 {
    244 	pktqueue_t *pktq = NULL;
    245 	struct ifqueue *ifq = NULL;
    246 	int s, isr = -1;
    247 	int csum_flags;
    248 	int error = 0;
    249 	size_t pktlen;
    250 
    251 	MCLAIM(m, ifp->if_mowner);
    252 
    253 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
    254 
    255 	if ((m->m_flags & M_PKTHDR) == 0)
    256 		panic("looutput: no header mbuf");
    257 	if (ifp->if_flags & IFF_LOOPBACK)
    258 		bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
    259 	m_set_rcvif(m, ifp);
    260 
    261 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    262 		m_freem(m);
    263 		error = (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    264 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    265 		goto out;
    266 	}
    267 
    268 	pktlen = m->m_pkthdr.len;
    269 
    270 	if_statadd2(ifp, if_opackets, 1, if_obytes, pktlen);
    271 
    272 #ifdef ALTQ
    273 	/*
    274 	 * ALTQ on the loopback interface is just for debugging.  It's
    275 	 * used only for loopback interfaces, not for a simplex interface.
    276 	 */
    277 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    278 	    ifp->if_start == lostart) {
    279 		/*
    280 		 * If the queueing discipline needs packet classification,
    281 		 * do it before prepending the link headers.
    282 		 */
    283 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    284 
    285 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    286 		if (m == NULL) {
    287 			if_statinc(ifp, if_oerrors);
    288 			error = ENOBUFS;
    289 			goto out;
    290 		}
    291 		*(mtod(m, uint32_t *)) = dst->sa_family;
    292 
    293 		error = if_transmit_lock(ifp, m);
    294 		goto out;
    295 	}
    296 #endif /* ALTQ */
    297 
    298 	m_tag_delete_chain(m);
    299 
    300 #ifdef MPLS
    301 	if (rt != NULL && rt_gettag(rt) != NULL &&
    302 	    rt_gettag(rt)->sa_family == AF_MPLS &&
    303 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
    304 		union mpls_shim msh;
    305 		msh.s_addr = MPLS_GETSADDR(rt);
    306 		if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    307 			ifq = &mplsintrq;
    308 			isr = NETISR_MPLS;
    309 		}
    310 	}
    311 	if (isr != NETISR_MPLS)
    312 #endif
    313 	switch (dst->sa_family) {
    314 
    315 #ifdef INET
    316 	case AF_INET:
    317 		csum_flags = m->m_pkthdr.csum_flags;
    318 		KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
    319 		if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    320 			in_undefer_cksum(m, 0, csum_flags);
    321 			m->m_pkthdr.csum_flags = 0;
    322 		} else {
    323 			/*
    324 			 * Do nothing. Pass M_CSUM_IPv4 and M_CSUM_UDPv4 as
    325 			 * they are to tell those are calculated and good.
    326 			 */
    327 		}
    328 		pktq = ip_pktq;
    329 		break;
    330 #endif
    331 #ifdef INET6
    332 	case AF_INET6:
    333 		csum_flags = m->m_pkthdr.csum_flags;
    334 		KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
    335 		if (csum_flags != 0 &&
    336 		    IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    337 			in6_undefer_cksum(m, 0, csum_flags);
    338 			m->m_pkthdr.csum_flags = 0;
    339 		} else {
    340 			/*
    341 			 * Do nothing. Pass M_CSUM_UDPv6 as
    342 			 * they are to tell those are calculated and good.
    343 			 */
    344 		}
    345 		m->m_flags |= M_LOOP;
    346 		pktq = ip6_pktq;
    347 		break;
    348 #endif
    349 #ifdef NETATALK
    350 	case AF_APPLETALK:
    351 	        ifq = &atintrq2;
    352 		isr = NETISR_ATALK;
    353 		break;
    354 #endif
    355 	default:
    356 		printf("%s: can't handle af%d\n", ifp->if_xname,
    357 		    dst->sa_family);
    358 		m_freem(m);
    359 		error = EAFNOSUPPORT;
    360 		goto out;
    361 	}
    362 
    363 	s = splnet();
    364 	if (__predict_true(pktq)) {
    365 		error = 0;
    366 
    367 		if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    368 			if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    369 		} else {
    370 			m_freem(m);
    371 			if_statinc(ifp, if_oerrors);
    372 			error = ENOBUFS;
    373 		}
    374 		splx(s);
    375 		goto out;
    376 	}
    377 	if (IF_QFULL(ifq)) {
    378 		IF_DROP(ifq);
    379 		m_freem(m);
    380 		splx(s);
    381 		error = ENOBUFS;
    382 		if_statinc(ifp, if_oerrors);
    383 		goto out;
    384 	}
    385 	if_statadd2(ifp, if_ipackets, 1, if_ibytes, m->m_pkthdr.len);
    386 	IF_ENQUEUE(ifq, m);
    387 	schednetisr(isr);
    388 	splx(s);
    389 out:
    390 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    391 	return error;
    392 }
    393 
    394 #ifdef ALTQ
    395 static void
    396 lostart(struct ifnet *ifp)
    397 {
    398 	for (;;) {
    399 		pktqueue_t *pktq = NULL;
    400 		struct ifqueue *ifq = NULL;
    401 		struct mbuf *m;
    402 		size_t pktlen;
    403 		uint32_t af;
    404 		int s, isr = 0;
    405 
    406 		IFQ_DEQUEUE(&ifp->if_snd, m);
    407 		if (m == NULL)
    408 			return;
    409 
    410 		af = *(mtod(m, uint32_t *));
    411 		m_adj(m, sizeof(uint32_t));
    412 
    413 		switch (af) {
    414 #ifdef INET
    415 		case AF_INET:
    416 			pktq = ip_pktq;
    417 			break;
    418 #endif
    419 #ifdef INET6
    420 		case AF_INET6:
    421 			m->m_flags |= M_LOOP;
    422 			pktq = ip6_pktq;
    423 			break;
    424 #endif
    425 #ifdef NETATALK
    426 		case AF_APPLETALK:
    427 			ifq = &atintrq2;
    428 			isr = NETISR_ATALK;
    429 			break;
    430 #endif
    431 		default:
    432 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    433 			m_freem(m);
    434 			return;
    435 		}
    436 		pktlen = m->m_pkthdr.len;
    437 
    438 		s = splnet();
    439 		if (__predict_true(pktq)) {
    440 			if (__predict_false(pktq_enqueue(pktq, m, 0))) {
    441 				m_freem(m);
    442 				splx(s);
    443 				return;
    444 			}
    445 			if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    446 			splx(s);
    447 			continue;
    448 		}
    449 		if (IF_QFULL(ifq)) {
    450 			IF_DROP(ifq);
    451 			splx(s);
    452 			m_freem(m);
    453 			return;
    454 		}
    455 		IF_ENQUEUE(ifq, m);
    456 		schednetisr(isr);
    457 		if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    458 		splx(s);
    459 	}
    460 }
    461 #endif /* ALTQ */
    462 
    463 /* ARGSUSED */
    464 static void
    465 loop_rtrequest(int cmd, struct rtentry *rt,
    466     const struct rt_addrinfo *info)
    467 {
    468 
    469 	if (rt)
    470 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
    471 }
    472 
    473 /*
    474  * Process an ioctl request.
    475  */
    476 /* ARGSUSED */
    477 int
    478 loioctl(struct ifnet *ifp, u_long cmd, void *data)
    479 {
    480 	struct ifaddr *ifa;
    481 	struct ifreq *ifr = data;
    482 	int error = 0;
    483 
    484 	switch (cmd) {
    485 
    486 	case SIOCINITIFADDR:
    487 		ifp->if_flags |= IFF_UP;
    488 		ifa = (struct ifaddr *)data;
    489 		if (ifa != NULL)
    490 			ifa->ifa_rtrequest = loop_rtrequest;
    491 		/*
    492 		 * Everything else is done at a higher level.
    493 		 */
    494 		break;
    495 
    496 	case SIOCSIFMTU:
    497 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
    498 			error = EINVAL;
    499 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
    500 			error = 0;
    501 		}
    502 		break;
    503 
    504 	case SIOCADDMULTI:
    505 	case SIOCDELMULTI:
    506 		if (ifr == NULL) {
    507 			error = EAFNOSUPPORT;		/* XXX */
    508 			break;
    509 		}
    510 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    511 
    512 #ifdef INET
    513 		case AF_INET:
    514 			break;
    515 #endif
    516 #ifdef INET6
    517 		case AF_INET6:
    518 			break;
    519 #endif
    520 
    521 		default:
    522 			error = EAFNOSUPPORT;
    523 			break;
    524 		}
    525 		break;
    526 
    527 	default:
    528 		error = ifioctl_common(ifp, cmd, data);
    529 	}
    530 	return (error);
    531 }
    532 
    533 /*
    534  * Module infrastructure
    535  */
    536 #include "if_module.h"
    537 
    538 IF_MODULE(MODULE_CLASS_DRIVER, loop, NULL)
    539