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