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