Home | History | Annotate | Line # | Download | only in net
if_loop.c revision 1.111
      1  1.111       roy /*	$NetBSD: if_loop.c,v 1.111 2020/10/14 15:55:49 roy Exp $	*/
      2   1.26    itojun 
      3   1.26    itojun /*
      4   1.26    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.26    itojun  * All rights reserved.
      6   1.55     perry  *
      7   1.26    itojun  * Redistribution and use in source and binary forms, with or without
      8   1.26    itojun  * modification, are permitted provided that the following conditions
      9   1.26    itojun  * are met:
     10   1.26    itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.26    itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.26    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.26    itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.26    itojun  *    documentation and/or other materials provided with the distribution.
     15   1.26    itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.26    itojun  *    may be used to endorse or promote products derived from this software
     17   1.26    itojun  *    without specific prior written permission.
     18   1.55     perry  *
     19   1.26    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.26    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.26    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.26    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.26    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.26    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.26    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.26    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.26    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.26    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.26    itojun  * SUCH DAMAGE.
     30   1.26    itojun  */
     31   1.12       cgd 
     32    1.1       cgd /*
     33   1.11   mycroft  * Copyright (c) 1982, 1986, 1993
     34   1.11   mycroft  *	The Regents of the University of California.  All rights reserved.
     35    1.1       cgd  *
     36    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     37    1.1       cgd  * modification, are permitted provided that the following conditions
     38    1.1       cgd  * are met:
     39    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     40    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     41    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     42    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     43    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     44   1.47       agc  * 3. Neither the name of the University nor the names of its contributors
     45    1.1       cgd  *    may be used to endorse or promote products derived from this software
     46    1.1       cgd  *    without specific prior written permission.
     47    1.1       cgd  *
     48    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58    1.1       cgd  * SUCH DAMAGE.
     59    1.1       cgd  *
     60   1.21      fvdl  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
     61    1.1       cgd  */
     62    1.1       cgd 
     63    1.1       cgd /*
     64    1.1       cgd  * Loopback interface driver for protocol testing and timing.
     65    1.1       cgd  */
     66   1.40     lukem 
     67   1.40     lukem #include <sys/cdefs.h>
     68  1.111       roy __KERNEL_RCSID(0, "$NetBSD: if_loop.c,v 1.111 2020/10/14 15:55:49 roy Exp $");
     69   1.23  jonathan 
     70   1.83     pooka #ifdef _KERNEL_OPT
     71   1.23  jonathan #include "opt_inet.h"
     72   1.23  jonathan #include "opt_atalk.h"
     73   1.46    martin #include "opt_mbuftrace.h"
     74   1.74    kefren #include "opt_mpls.h"
     75   1.81     ozaki #include "opt_net_mpsafe.h"
     76   1.83     pooka #endif
     77    1.4       cgd 
     78    1.9   mycroft #include <sys/param.h>
     79    1.9   mycroft #include <sys/systm.h>
     80   1.11   mycroft #include <sys/kernel.h>
     81    1.9   mycroft #include <sys/mbuf.h>
     82    1.9   mycroft #include <sys/socket.h>
     83    1.9   mycroft #include <sys/errno.h>
     84    1.9   mycroft #include <sys/ioctl.h>
     85   1.11   mycroft #include <sys/time.h>
     86   1.90  christos #include <sys/device.h>
     87   1.90  christos #include <sys/module.h>
     88   1.11   mycroft 
     89   1.66        ad #include <sys/cpu.h>
     90    1.9   mycroft 
     91    1.9   mycroft #include <net/if.h>
     92    1.9   mycroft #include <net/if_types.h>
     93    1.9   mycroft #include <net/netisr.h>
     94    1.9   mycroft #include <net/route.h>
     95    1.1       cgd 
     96    1.1       cgd #ifdef	INET
     97    1.9   mycroft #include <netinet/in.h>
     98    1.9   mycroft #include <netinet/in_systm.h>
     99    1.9   mycroft #include <netinet/in_var.h>
    100   1.73      yamt #include <netinet/in_offload.h>
    101    1.9   mycroft #include <netinet/ip.h>
    102    1.1       cgd #endif
    103    1.1       cgd 
    104   1.26    itojun #ifdef INET6
    105   1.26    itojun #ifndef INET
    106   1.26    itojun #include <netinet/in.h>
    107   1.26    itojun #endif
    108   1.26    itojun #include <netinet6/in6_var.h>
    109   1.73      yamt #include <netinet6/in6_offload.h>
    110   1.29    itojun #include <netinet/ip6.h>
    111   1.26    itojun #endif
    112   1.26    itojun 
    113   1.74    kefren #ifdef MPLS
    114   1.74    kefren #include <netmpls/mpls.h>
    115   1.74    kefren #include <netmpls/mpls_var.h>
    116   1.74    kefren #endif
    117   1.74    kefren 
    118   1.19  christos #ifdef NETATALK
    119   1.19  christos #include <netatalk/at.h>
    120   1.19  christos #include <netatalk/at_var.h>
    121   1.19  christos #endif
    122   1.19  christos 
    123    1.2       cgd #include <net/bpf.h>
    124    1.9   mycroft 
    125   1.26    itojun #if defined(LARGE_LOMTU)
    126   1.26    itojun #define LOMTU	(131072 +  MHLEN + MLEN)
    127   1.48  jonathan #define LOMTU_MAX LOMTU
    128   1.26    itojun #else
    129   1.20  jonathan #define	LOMTU	(32768 +  MHLEN + MLEN)
    130   1.48  jonathan #define	LOMTU_MAX	(65536 +  MHLEN + MLEN)
    131   1.26    itojun #endif
    132    1.1       cgd 
    133   1.34   thorpej #ifdef ALTQ
    134   1.57   thorpej static void	lostart(struct ifnet *);
    135   1.34   thorpej #endif
    136   1.32   thorpej 
    137   1.57   thorpej static int	loop_clone_create(struct if_clone *, int);
    138   1.57   thorpej static int	loop_clone_destroy(struct ifnet *);
    139   1.52     peter 
    140   1.93     ozaki static void	loop_rtrequest(int, struct rtentry *, const struct rt_addrinfo *);
    141   1.93     ozaki 
    142   1.57   thorpej static struct if_clone loop_cloner =
    143   1.52     peter     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
    144   1.52     peter 
    145    1.6    andrew void
    146   1.62  christos loopattach(int n)
    147    1.1       cgd {
    148   1.52     peter 
    149   1.95  knakahar #ifndef _MODULE
    150   1.95  knakahar 	loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    151   1.95  knakahar #endif
    152   1.90  christos }
    153   1.90  christos 
    154   1.91       kre void
    155   1.90  christos loopinit(void)
    156   1.90  christos {
    157   1.90  christos 
    158   1.92       kre 	if (lo0ifp != NULL)	/* can happen in rump kernel */
    159   1.92       kre 		return;
    160   1.92       kre 
    161   1.95  knakahar #ifdef _MODULE
    162   1.95  knakahar 	loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
    163   1.95  knakahar #endif
    164   1.52     peter 	if_clone_attach(&loop_cloner);
    165   1.52     peter }
    166   1.52     peter 
    167   1.57   thorpej static int
    168   1.90  christos loopdetach(void)
    169   1.90  christos {
    170   1.90  christos 	/* no detach for now; we don't allow lo0 to be deleted */
    171   1.90  christos 	return EBUSY;
    172   1.90  christos }
    173   1.90  christos 
    174   1.90  christos static int
    175   1.52     peter loop_clone_create(struct if_clone *ifc, int unit)
    176   1.52     peter {
    177   1.60     peter 	struct ifnet *ifp;
    178   1.96   msaitoh 	int rv;
    179   1.52     peter 
    180   1.68  christos 	ifp = if_alloc(IFT_LOOP);
    181   1.52     peter 
    182   1.68  christos 	if_initname(ifp, ifc->ifc_name, unit);
    183    1.1       cgd 
    184   1.60     peter 	ifp->if_mtu = LOMTU;
    185  1.100     ozaki 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
    186  1.101     ozaki #ifdef NET_MPSAFE
    187  1.111       roy 	ifp->if_extflags = IFEF_MPSAFE;
    188  1.101     ozaki #endif
    189   1.60     peter 	ifp->if_ioctl = loioctl;
    190   1.60     peter 	ifp->if_output = looutput;
    191   1.34   thorpej #ifdef ALTQ
    192   1.60     peter 	ifp->if_start = lostart;
    193   1.34   thorpej #endif
    194   1.60     peter 	ifp->if_type = IFT_LOOP;
    195   1.60     peter 	ifp->if_hdrlen = 0;
    196   1.60     peter 	ifp->if_addrlen = 0;
    197   1.60     peter 	ifp->if_dlt = DLT_NULL;
    198   1.60     peter 	IFQ_SET_READY(&ifp->if_snd);
    199   1.52     peter 	if (unit == 0)
    200   1.60     peter 		lo0ifp = ifp;
    201  1.111       roy 	rv = if_initialize(ifp);
    202   1.96   msaitoh 	if (rv != 0) {
    203   1.96   msaitoh 		if_free(ifp);
    204   1.96   msaitoh 		return rv;
    205   1.96   msaitoh 	}
    206   1.60     peter 	if_alloc_sadl(ifp);
    207   1.72     joerg 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
    208   1.42      matt #ifdef MBUFTRACE
    209   1.60     peter 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    210   1.52     peter 	    M_WAITOK | M_ZERO);
    211   1.60     peter 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    212   1.60     peter 	    sizeof(ifp->if_mowner->mo_name));
    213   1.60     peter 	MOWNER_ATTACH(ifp->if_mowner);
    214   1.42      matt #endif
    215   1.52     peter 
    216  1.100     ozaki 	ifp->if_flags |= IFF_RUNNING;
    217  1.111       roy 	ifp->if_link_state = LINK_STATE_UP;
    218  1.111       roy 	if_register(ifp);
    219  1.100     ozaki 
    220   1.52     peter 	return (0);
    221   1.52     peter }
    222   1.52     peter 
    223   1.57   thorpej static int
    224   1.52     peter loop_clone_destroy(struct ifnet *ifp)
    225   1.52     peter {
    226   1.52     peter 
    227   1.53     peter 	if (ifp == lo0ifp)
    228   1.53     peter 		return (EPERM);
    229   1.52     peter 
    230  1.100     ozaki 	ifp->if_flags &= ~IFF_RUNNING;
    231  1.100     ozaki 
    232   1.52     peter #ifdef MBUFTRACE
    233   1.52     peter 	MOWNER_DETACH(ifp->if_mowner);
    234   1.52     peter 	free(ifp->if_mowner, M_DEVBUF);
    235   1.52     peter #endif
    236   1.52     peter 
    237   1.72     joerg 	bpf_detach(ifp);
    238   1.52     peter 	if_detach(ifp);
    239   1.52     peter 
    240   1.78     pooka 	if_free(ifp);
    241   1.53     peter 
    242   1.53     peter 	return (0);
    243    1.1       cgd }
    244    1.1       cgd 
    245   1.11   mycroft int
    246   1.63    dyoung looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    247   1.85     ozaki     const struct rtentry *rt)
    248    1.1       cgd {
    249   1.79     rmind 	pktqueue_t *pktq = NULL;
    250   1.79     rmind 	struct ifqueue *ifq = NULL;
    251   1.75    kefren 	int s, isr = -1;
    252   1.73      yamt 	int csum_flags;
    253   1.88  knakahar 	int error = 0;
    254   1.79     rmind 	size_t pktlen;
    255    1.1       cgd 
    256   1.42      matt 	MCLAIM(m, ifp->if_mowner);
    257   1.88  knakahar 
    258   1.99     ozaki 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
    259   1.77    bouyer 
    260    1.1       cgd 	if ((m->m_flags & M_PKTHDR) == 0)
    261   1.14   mycroft 		panic("looutput: no header mbuf");
    262   1.72     joerg 	if (ifp->if_flags & IFF_LOOPBACK)
    263  1.102   msaitoh 		bpf_mtap_af(ifp, dst->sa_family, m, BPF_D_OUT);
    264   1.87     ozaki 	m_set_rcvif(m, ifp);
    265    1.1       cgd 
    266   1.11   mycroft 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
    267    1.1       cgd 		m_freem(m);
    268   1.88  knakahar 		error = (rt->rt_flags & RTF_BLACKHOLE ? 0 :
    269   1.11   mycroft 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
    270   1.88  knakahar 		goto out;
    271    1.1       cgd 	}
    272   1.26    itojun 
    273   1.79     rmind 	pktlen = m->m_pkthdr.len;
    274  1.110   thorpej 
    275  1.110   thorpej 	if_statadd2(ifp, if_opackets, 1, if_obytes, pktlen);
    276   1.32   thorpej 
    277   1.32   thorpej #ifdef ALTQ
    278   1.32   thorpej 	/*
    279   1.32   thorpej 	 * ALTQ on the loopback interface is just for debugging.  It's
    280   1.32   thorpej 	 * used only for loopback interfaces, not for a simplex interface.
    281   1.32   thorpej 	 */
    282   1.32   thorpej 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
    283   1.32   thorpej 	    ifp->if_start == lostart) {
    284   1.32   thorpej 		/*
    285   1.32   thorpej 		 * If the queueing discipline needs packet classification,
    286   1.32   thorpej 		 * do it before prepending the link headers.
    287   1.32   thorpej 		 */
    288   1.84  knakahar 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    289   1.32   thorpej 
    290   1.32   thorpej 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
    291   1.88  knakahar 		if (m == NULL) {
    292   1.88  knakahar 			error = ENOBUFS;
    293   1.88  knakahar 			goto out;
    294   1.88  knakahar 		}
    295   1.32   thorpej 		*(mtod(m, uint32_t *)) = dst->sa_family;
    296   1.32   thorpej 
    297   1.89  knakahar 		error = if_transmit_lock(ifp, m);
    298   1.88  knakahar 		goto out;
    299   1.32   thorpej 	}
    300   1.32   thorpej #endif /* ALTQ */
    301   1.49  jonathan 
    302  1.106      maxv 	m_tag_delete_chain(m);
    303   1.32   thorpej 
    304   1.75    kefren #ifdef MPLS
    305   1.75    kefren 	if (rt != NULL && rt_gettag(rt) != NULL &&
    306   1.75    kefren 	    rt_gettag(rt)->sa_family == AF_MPLS &&
    307   1.75    kefren 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
    308   1.75    kefren 		union mpls_shim msh;
    309   1.75    kefren 		msh.s_addr = MPLS_GETSADDR(rt);
    310   1.75    kefren 		if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    311   1.75    kefren 			ifq = &mplsintrq;
    312   1.75    kefren 			isr = NETISR_MPLS;
    313   1.75    kefren 		}
    314   1.75    kefren 	}
    315   1.75    kefren 	if (isr != NETISR_MPLS)
    316   1.75    kefren #endif
    317    1.1       cgd 	switch (dst->sa_family) {
    318    1.1       cgd 
    319    1.1       cgd #ifdef INET
    320    1.1       cgd 	case AF_INET:
    321   1.73      yamt 		csum_flags = m->m_pkthdr.csum_flags;
    322   1.73      yamt 		KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
    323   1.73      yamt 		if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    324  1.104      maxv 			in_undefer_cksum(m, 0, csum_flags);
    325  1.108   msaitoh 			m->m_pkthdr.csum_flags = 0;
    326  1.108   msaitoh 		} else {
    327  1.108   msaitoh 			/*
    328  1.108   msaitoh 			 * Do nothing. Pass M_CSUM_IPv4 and M_CSUM_UDPv4 as
    329  1.108   msaitoh 			 * they are to tell those are calculated and good.
    330  1.108   msaitoh 			 */
    331   1.73      yamt 		}
    332   1.79     rmind 		pktq = ip_pktq;
    333    1.1       cgd 		break;
    334    1.1       cgd #endif
    335   1.26    itojun #ifdef INET6
    336   1.26    itojun 	case AF_INET6:
    337   1.73      yamt 		csum_flags = m->m_pkthdr.csum_flags;
    338   1.73      yamt 		KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
    339   1.73      yamt 		if (csum_flags != 0 &&
    340   1.73      yamt 		    IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
    341  1.105      maxv 			in6_undefer_cksum(m, 0, csum_flags);
    342  1.108   msaitoh 			m->m_pkthdr.csum_flags = 0;
    343  1.108   msaitoh 		} else {
    344  1.108   msaitoh 			/*
    345  1.109   msaitoh 			 * Do nothing. Pass M_CSUM_UDPv6 as
    346  1.108   msaitoh 			 * they are to tell those are calculated and good.
    347  1.108   msaitoh 			 */
    348   1.73      yamt 		}
    349   1.26    itojun 		m->m_flags |= M_LOOP;
    350   1.79     rmind 		pktq = ip6_pktq;
    351   1.26    itojun 		break;
    352   1.26    itojun #endif
    353   1.19  christos #ifdef NETATALK
    354   1.19  christos 	case AF_APPLETALK:
    355   1.19  christos 	        ifq = &atintrq2;
    356   1.19  christos 		isr = NETISR_ATALK;
    357    1.1       cgd 		break;
    358    1.1       cgd #endif
    359    1.1       cgd 	default:
    360   1.17  christos 		printf("%s: can't handle af%d\n", ifp->if_xname,
    361   1.16  christos 		    dst->sa_family);
    362    1.1       cgd 		m_freem(m);
    363   1.88  knakahar 		error = EAFNOSUPPORT;
    364   1.88  knakahar 		goto out;
    365    1.1       cgd 	}
    366   1.79     rmind 
    367   1.38   thorpej 	s = splnet();
    368   1.79     rmind 	if (__predict_true(pktq)) {
    369   1.88  knakahar 		error = 0;
    370   1.79     rmind 
    371   1.79     rmind 		if (__predict_true(pktq_enqueue(pktq, m, 0))) {
    372  1.110   thorpej 			if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    373   1.79     rmind 		} else {
    374   1.79     rmind 			m_freem(m);
    375   1.79     rmind 			error = ENOBUFS;
    376   1.79     rmind 		}
    377   1.79     rmind 		splx(s);
    378   1.88  knakahar 		goto out;
    379   1.79     rmind 	}
    380    1.1       cgd 	if (IF_QFULL(ifq)) {
    381    1.1       cgd 		IF_DROP(ifq);
    382    1.1       cgd 		m_freem(m);
    383    1.1       cgd 		splx(s);
    384   1.88  knakahar 		error = ENOBUFS;
    385   1.88  knakahar 		goto out;
    386    1.1       cgd 	}
    387  1.110   thorpej 	if_statadd2(ifp, if_ipackets, 1, if_ibytes, m->m_pkthdr.len);
    388    1.1       cgd 	IF_ENQUEUE(ifq, m);
    389    1.1       cgd 	schednetisr(isr);
    390    1.1       cgd 	splx(s);
    391   1.88  knakahar out:
    392   1.99     ozaki 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    393   1.88  knakahar 	return error;
    394    1.1       cgd }
    395   1.32   thorpej 
    396   1.32   thorpej #ifdef ALTQ
    397   1.57   thorpej static void
    398   1.32   thorpej lostart(struct ifnet *ifp)
    399   1.32   thorpej {
    400   1.79     rmind 	for (;;) {
    401   1.79     rmind 		pktqueue_t *pktq = NULL;
    402   1.80     rmind 		struct ifqueue *ifq = NULL;
    403   1.79     rmind 		struct mbuf *m;
    404   1.79     rmind 		size_t pktlen;
    405   1.79     rmind 		uint32_t af;
    406   1.80     rmind 		int s, isr = 0;
    407   1.32   thorpej 
    408   1.32   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    409   1.32   thorpej 		if (m == NULL)
    410   1.32   thorpej 			return;
    411   1.32   thorpej 
    412   1.32   thorpej 		af = *(mtod(m, uint32_t *));
    413   1.32   thorpej 		m_adj(m, sizeof(uint32_t));
    414   1.32   thorpej 
    415   1.32   thorpej 		switch (af) {
    416   1.32   thorpej #ifdef INET
    417   1.32   thorpej 		case AF_INET:
    418   1.79     rmind 			pktq = ip_pktq;
    419   1.32   thorpej 			break;
    420   1.32   thorpej #endif
    421   1.32   thorpej #ifdef INET6
    422   1.32   thorpej 		case AF_INET6:
    423   1.32   thorpej 			m->m_flags |= M_LOOP;
    424   1.79     rmind 			pktq = ip6_pktq;
    425   1.32   thorpej 			break;
    426   1.32   thorpej #endif
    427   1.32   thorpej #ifdef NETATALK
    428   1.32   thorpej 		case AF_APPLETALK:
    429   1.32   thorpej 			ifq = &atintrq2;
    430   1.32   thorpej 			isr = NETISR_ATALK;
    431   1.32   thorpej 			break;
    432   1.32   thorpej #endif
    433   1.32   thorpej 		default:
    434   1.32   thorpej 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
    435   1.32   thorpej 			m_freem(m);
    436   1.32   thorpej 			return;
    437   1.32   thorpej 		}
    438   1.79     rmind 		pktlen = m->m_pkthdr.len;
    439   1.32   thorpej 
    440   1.38   thorpej 		s = splnet();
    441   1.79     rmind 		if (__predict_true(pktq)) {
    442   1.79     rmind 			if (__predict_false(pktq_enqueue(pktq, m, 0))) {
    443   1.79     rmind 				m_freem(m);
    444   1.79     rmind 				splx(s);
    445   1.79     rmind 				return;
    446   1.79     rmind 			}
    447  1.110   thorpej 			if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    448   1.79     rmind 			splx(s);
    449   1.79     rmind 			continue;
    450   1.79     rmind 		}
    451   1.32   thorpej 		if (IF_QFULL(ifq)) {
    452   1.32   thorpej 			IF_DROP(ifq);
    453   1.32   thorpej 			splx(s);
    454   1.32   thorpej 			m_freem(m);
    455   1.32   thorpej 			return;
    456   1.32   thorpej 		}
    457   1.32   thorpej 		IF_ENQUEUE(ifq, m);
    458   1.32   thorpej 		schednetisr(isr);
    459  1.110   thorpej 		if_statadd2(ifp, if_ipackets, 1, if_ibytes, pktlen);
    460   1.32   thorpej 		splx(s);
    461   1.32   thorpej 	}
    462   1.32   thorpej }
    463   1.32   thorpej #endif /* ALTQ */
    464    1.1       cgd 
    465    1.1       cgd /* ARGSUSED */
    466   1.93     ozaki static void
    467   1.93     ozaki loop_rtrequest(int cmd, struct rtentry *rt,
    468   1.69    dyoung     const struct rt_addrinfo *info)
    469    1.1       cgd {
    470   1.11   mycroft 
    471    1.1       cgd 	if (rt)
    472   1.52     peter 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
    473    1.1       cgd }
    474    1.1       cgd 
    475    1.1       cgd /*
    476    1.1       cgd  * Process an ioctl request.
    477    1.1       cgd  */
    478    1.1       cgd /* ARGSUSED */
    479   1.11   mycroft int
    480   1.64  christos loioctl(struct ifnet *ifp, u_long cmd, void *data)
    481    1.1       cgd {
    482   1.30  augustss 	struct ifaddr *ifa;
    483   1.67    dyoung 	struct ifreq *ifr = data;
    484   1.30  augustss 	int error = 0;
    485    1.1       cgd 
    486    1.1       cgd 	switch (cmd) {
    487    1.1       cgd 
    488   1.70    dyoung 	case SIOCINITIFADDR:
    489    1.1       cgd 		ifp->if_flags |= IFF_UP;
    490    1.1       cgd 		ifa = (struct ifaddr *)data;
    491   1.76     joerg 		if (ifa != NULL)
    492   1.93     ozaki 			ifa->ifa_rtrequest = loop_rtrequest;
    493    1.1       cgd 		/*
    494    1.1       cgd 		 * Everything else is done at a higher level.
    495    1.1       cgd 		 */
    496   1.48  jonathan 		break;
    497   1.48  jonathan 
    498   1.48  jonathan 	case SIOCSIFMTU:
    499   1.48  jonathan 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
    500   1.48  jonathan 			error = EINVAL;
    501   1.67    dyoung 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
    502   1.67    dyoung 			error = 0;
    503   1.48  jonathan 		}
    504    1.1       cgd 		break;
    505    1.1       cgd 
    506    1.8   hpeyerl 	case SIOCADDMULTI:
    507    1.8   hpeyerl 	case SIOCDELMULTI:
    508   1.54     peter 		if (ifr == NULL) {
    509    1.8   hpeyerl 			error = EAFNOSUPPORT;		/* XXX */
    510    1.8   hpeyerl 			break;
    511    1.8   hpeyerl 		}
    512   1.65    dyoung 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    513    1.8   hpeyerl 
    514    1.8   hpeyerl #ifdef INET
    515    1.8   hpeyerl 		case AF_INET:
    516   1.26    itojun 			break;
    517   1.26    itojun #endif
    518   1.26    itojun #ifdef INET6
    519   1.26    itojun 		case AF_INET6:
    520    1.8   hpeyerl 			break;
    521    1.8   hpeyerl #endif
    522   1.11   mycroft 
    523    1.8   hpeyerl 		default:
    524    1.8   hpeyerl 			error = EAFNOSUPPORT;
    525    1.8   hpeyerl 			break;
    526    1.8   hpeyerl 		}
    527    1.8   hpeyerl 		break;
    528   1.11   mycroft 
    529    1.1       cgd 	default:
    530   1.70    dyoung 		error = ifioctl_common(ifp, cmd, data);
    531    1.1       cgd 	}
    532    1.1       cgd 	return (error);
    533    1.1       cgd }
    534   1.90  christos 
    535   1.90  christos /*
    536   1.90  christos  * Module infrastructure
    537   1.90  christos  */
    538   1.90  christos #include "if_module.h"
    539   1.90  christos 
    540  1.107  pgoyette IF_MODULE(MODULE_CLASS_DRIVER, loop, NULL)
    541