xref: /freebsd/sys/net/if_loop.c (revision fe6db7c77e1d227f1a7999a0809d034e6b2fb70a)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33fe6db7c7SRuslan Ermilov  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes /*
38df8bae1dSRodney W. Grimes  * Loopback interface driver for protocol testing and timing.
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
417262d3e4SEivind Eklund #include "opt_atalk.h"
421d5e9e22SEivind Eklund #include "opt_inet.h"
43cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
44430df5f4SEivind Eklund #include "opt_ipx.h"
45430df5f4SEivind Eklund 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
4990d9802fSPeter Wemm #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
512b120974SPeter Wemm #include <sys/module.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
5351a53488SBruce Evans #include <sys/sockio.h>
5490d9802fSPeter Wemm #include <sys/sysctl.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/if_types.h>
58df8bae1dSRodney W. Grimes #include <net/netisr.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes #include <net/bpf.h>
6133841545SHajimu UMEMOTO #include <net/bpfdesc.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #ifdef	INET
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #endif
67df8bae1dSRodney W. Grimes 
68cc6a66f2SJulian Elischer #ifdef IPX
69cc6a66f2SJulian Elischer #include <netipx/ipx.h>
70cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
71cc6a66f2SJulian Elischer #endif
72cc6a66f2SJulian Elischer 
7382cd038dSYoshinobu Inoue #ifdef INET6
7482cd038dSYoshinobu Inoue #ifndef INET
7582cd038dSYoshinobu Inoue #include <netinet/in.h>
7682cd038dSYoshinobu Inoue #endif
7782cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
78686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7982cd038dSYoshinobu Inoue #endif
8082cd038dSYoshinobu Inoue 
81df8bae1dSRodney W. Grimes #ifdef NS
82df8bae1dSRodney W. Grimes #include <netns/ns.h>
83df8bae1dSRodney W. Grimes #include <netns/ns_if.h>
84df8bae1dSRodney W. Grimes #endif
85df8bae1dSRodney W. Grimes 
86655929bfSJulian Elischer #ifdef NETATALK
87655929bfSJulian Elischer #include <netatalk/at.h>
88655929bfSJulian Elischer #include <netatalk/at_var.h>
898cdfefbdSPeter Wemm #endif
90655929bfSJulian Elischer 
91cfa1ca9dSYoshinobu Inoue int loioctl __P((struct ifnet *, u_long, caddr_t));
923bda9f9bSPoul-Henning Kamp static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
933bda9f9bSPoul-Henning Kamp 
94cfa1ca9dSYoshinobu Inoue int looutput __P((struct ifnet *ifp,
95ed7509acSJulian Elischer 		struct mbuf *m, struct sockaddr *dst, struct rtentry *rt));
96ed7509acSJulian Elischer 
97db8889c6SDavid Greenman #ifdef TINY_LOMTU
98df8bae1dSRodney W. Grimes #define	LOMTU	(1024+512)
9982cd038dSYoshinobu Inoue #elif defined(LARGE_LOMTU)
10082cd038dSYoshinobu Inoue #define LOMTU	131072
101db8889c6SDavid Greenman #else
102af78195eSDavid Greenman #define LOMTU	16384
103db8889c6SDavid Greenman #endif
104df8bae1dSRodney W. Grimes 
10581930014SPeter Wemm static int nloop = 1;
10690d9802fSPeter Wemm 
10790d9802fSPeter Wemm struct ifnet *loif;			/* Used externally */
10890d9802fSPeter Wemm 
10990d9802fSPeter Wemm static MALLOC_DEFINE(M_LO, "lo", "Loopback Interface");
11090d9802fSPeter Wemm 
11190d9802fSPeter Wemm struct lo_softc {
11290d9802fSPeter Wemm 	struct	ifnet sc_if;		/* network-visible interface */
11390d9802fSPeter Wemm         LIST_ENTRY(lo_softc) sc_next;
11490d9802fSPeter Wemm };
11590d9802fSPeter Wemm static LIST_HEAD(lo_list, lo_softc) lo_list;
11690d9802fSPeter Wemm 
11790d9802fSPeter Wemm static void
11890d9802fSPeter Wemm locreate(int unit)
11990d9802fSPeter Wemm {
12090d9802fSPeter Wemm 	struct lo_softc *sc;
12190d9802fSPeter Wemm 
1221707240dSBoris Popov 	MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO);
12390d9802fSPeter Wemm 
12490d9802fSPeter Wemm 	sc->sc_if.if_name = "lo";
12590d9802fSPeter Wemm 	sc->sc_if.if_unit = unit;
12690d9802fSPeter Wemm 	sc->sc_if.if_mtu = LOMTU;
12790d9802fSPeter Wemm 	sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST;
12890d9802fSPeter Wemm 	sc->sc_if.if_ioctl = loioctl;
12990d9802fSPeter Wemm 	sc->sc_if.if_output = looutput;
13090d9802fSPeter Wemm 	sc->sc_if.if_type = IFT_LOOP;
13190d9802fSPeter Wemm 	sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
13290d9802fSPeter Wemm 	if_attach(&sc->sc_if);
13390d9802fSPeter Wemm 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
13490d9802fSPeter Wemm 	LIST_INSERT_HEAD(&lo_list, sc, sc_next);
13590d9802fSPeter Wemm 	if (loif == NULL)
13690d9802fSPeter Wemm 		loif = &sc->sc_if;
13790d9802fSPeter Wemm }
13890d9802fSPeter Wemm 
13990d9802fSPeter Wemm static void
14090d9802fSPeter Wemm lodestroy(struct lo_softc *sc)
14190d9802fSPeter Wemm {
14290d9802fSPeter Wemm 	bpfdetach(&sc->sc_if);
14390d9802fSPeter Wemm 	if_detach(&sc->sc_if);
14490d9802fSPeter Wemm 	LIST_REMOVE(sc, sc_next);
14590d9802fSPeter Wemm 	FREE(sc, M_LO);
14690d9802fSPeter Wemm }
14790d9802fSPeter Wemm 
14890d9802fSPeter Wemm 
14990d9802fSPeter Wemm static int
15090d9802fSPeter Wemm sysctl_net_nloop(SYSCTL_HANDLER_ARGS)
15190d9802fSPeter Wemm {
15290d9802fSPeter Wemm 	int newnloop;
15390d9802fSPeter Wemm 	int error;
15490d9802fSPeter Wemm 
15590d9802fSPeter Wemm 	newnloop = nloop;
15690d9802fSPeter Wemm 
15790d9802fSPeter Wemm 	error = sysctl_handle_opaque(oidp, &newnloop, sizeof newnloop, req);
15890d9802fSPeter Wemm 	if (error || !req->newptr)
15990d9802fSPeter Wemm 		return (error);
16090d9802fSPeter Wemm 
16190d9802fSPeter Wemm 	if (newnloop < 1)
16290d9802fSPeter Wemm 		return (EINVAL);
16390d9802fSPeter Wemm 	while (newnloop > nloop) {
16490d9802fSPeter Wemm 		locreate(nloop);
16590d9802fSPeter Wemm 		nloop++;
16690d9802fSPeter Wemm 	}
16790d9802fSPeter Wemm 	while (newnloop < nloop) {
16890d9802fSPeter Wemm 		lodestroy(LIST_FIRST(&lo_list));
16990d9802fSPeter Wemm 		nloop--;
17090d9802fSPeter Wemm 	}
17190d9802fSPeter Wemm 	return (0);
17290d9802fSPeter Wemm }
17390d9802fSPeter Wemm SYSCTL_PROC(_net, OID_AUTO, nloop, CTLTYPE_INT | CTLFLAG_RW,
17490d9802fSPeter Wemm 	    0, 0, sysctl_net_nloop, "I", "");
175df8bae1dSRodney W. Grimes 
1762b120974SPeter Wemm static int
1772b120974SPeter Wemm loop_modevent(module_t mod, int type, void *data)
178df8bae1dSRodney W. Grimes {
17990d9802fSPeter Wemm 	int i;
180df8bae1dSRodney W. Grimes 
1812b120974SPeter Wemm 	switch (type) {
1822b120974SPeter Wemm 	case MOD_LOAD:
18309786698SPeter Wemm 		TUNABLE_INT_FETCH("net.nloop", &nloop);
18490d9802fSPeter Wemm 		if (nloop < 1)			/* sanity check */
18590d9802fSPeter Wemm 			nloop = 1;
18690d9802fSPeter Wemm 		for (i = 0; i < nloop; i++)
18790d9802fSPeter Wemm 			locreate(i);
1882b120974SPeter Wemm 		break;
1892b120974SPeter Wemm 	case MOD_UNLOAD:
1902b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
1912b120974SPeter Wemm 		return EINVAL;
192f5fea3ddSPaul Traina 	}
1932b120974SPeter Wemm 	return 0;
1942b120974SPeter Wemm }
1952b120974SPeter Wemm 
1962b120974SPeter Wemm static moduledata_t loop_mod = {
1972b120974SPeter Wemm 	"loop",
1982b120974SPeter Wemm 	loop_modevent,
1992b120974SPeter Wemm 	0
2002b120974SPeter Wemm };
2012b120974SPeter Wemm 
2022b120974SPeter Wemm DECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
203df8bae1dSRodney W. Grimes 
204cfa1ca9dSYoshinobu Inoue int
205df8bae1dSRodney W. Grimes looutput(ifp, m, dst, rt)
206df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
207df8bae1dSRodney W. Grimes 	register struct mbuf *m;
208df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
209df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
210df8bae1dSRodney W. Grimes {
211ed7509acSJulian Elischer 	if ((m->m_flags & M_PKTHDR) == 0)
212ed7509acSJulian Elischer 		panic("looutput no HDR");
213ed7509acSJulian Elischer 
214ed7509acSJulian Elischer 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
215ed7509acSJulian Elischer 		m_freem(m);
216ed7509acSJulian Elischer 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
217ed7509acSJulian Elischer 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
218ed7509acSJulian Elischer 	}
21982cd038dSYoshinobu Inoue 	/*
22082cd038dSYoshinobu Inoue 	 * KAME requires that the packet to be contiguous on the
22182cd038dSYoshinobu Inoue 	 * mbuf.  We need to make that sure.
22282cd038dSYoshinobu Inoue 	 * this kind of code should be avoided.
22382cd038dSYoshinobu Inoue 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
22482cd038dSYoshinobu Inoue 	 */
22582cd038dSYoshinobu Inoue 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
22682cd038dSYoshinobu Inoue 		struct mbuf *n;
22782cd038dSYoshinobu Inoue 
22882cd038dSYoshinobu Inoue 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
22982cd038dSYoshinobu Inoue 		if (!n)
23082cd038dSYoshinobu Inoue 			goto contiguousfail;
23182cd038dSYoshinobu Inoue 		MCLGET(n, M_DONTWAIT);
23282cd038dSYoshinobu Inoue 		if (! (n->m_flags & M_EXT)) {
23382cd038dSYoshinobu Inoue 			m_freem(n);
23482cd038dSYoshinobu Inoue 			goto contiguousfail;
23582cd038dSYoshinobu Inoue 		}
23682cd038dSYoshinobu Inoue 
23782cd038dSYoshinobu Inoue 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
23882cd038dSYoshinobu Inoue 		n->m_pkthdr = m->m_pkthdr;
23982cd038dSYoshinobu Inoue 		n->m_len = m->m_pkthdr.len;
24033841545SHajimu UMEMOTO 		n->m_pkthdr.aux = m->m_pkthdr.aux;
24133841545SHajimu UMEMOTO 		m->m_pkthdr.aux = (struct mbuf *)NULL;
24282cd038dSYoshinobu Inoue 		m_freem(m);
24382cd038dSYoshinobu Inoue 		m = n;
24482cd038dSYoshinobu Inoue 	}
24582cd038dSYoshinobu Inoue 	if (0) {
24682cd038dSYoshinobu Inoue contiguousfail:
24782cd038dSYoshinobu Inoue 		printf("looutput: mbuf allocation failed\n");
24882cd038dSYoshinobu Inoue 	}
24982cd038dSYoshinobu Inoue 
250ed7509acSJulian Elischer 	ifp->if_opackets++;
251ed7509acSJulian Elischer 	ifp->if_obytes += m->m_pkthdr.len;
252201c2527SJulian Elischer #if 1	/* XXX */
253201c2527SJulian Elischer 	switch (dst->sa_family) {
254201c2527SJulian Elischer 	case AF_INET:
25582cd038dSYoshinobu Inoue 	case AF_INET6:
256201c2527SJulian Elischer 	case AF_IPX:
257201c2527SJulian Elischer 	case AF_NS:
258201c2527SJulian Elischer 	case AF_APPLETALK:
25905b3ccefSJulian Elischer 		break;
260201c2527SJulian Elischer 	default:
26171582bf5SRobert Watson 		printf("looutput: af=%d unexpected\n", dst->sa_family);
262201c2527SJulian Elischer 		m_freem(m);
263201c2527SJulian Elischer 		return (EAFNOSUPPORT);
264201c2527SJulian Elischer 	}
265201c2527SJulian Elischer #endif
26606a429a3SArchie Cobbs 	return(if_simloop(ifp, m, dst->sa_family, 0));
267ed7509acSJulian Elischer }
268ed7509acSJulian Elischer 
269ed7509acSJulian Elischer /*
270ed7509acSJulian Elischer  * if_simloop()
271ed7509acSJulian Elischer  *
272ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
273ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
274ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
275ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
276ed7509acSJulian Elischer  *
277ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
278ed7509acSJulian Elischer  */
279ed7509acSJulian Elischer 
280ed7509acSJulian Elischer int
28106a429a3SArchie Cobbs if_simloop(ifp, m, af, hlen)
282ed7509acSJulian Elischer 	struct ifnet *ifp;
28333841545SHajimu UMEMOTO 	struct mbuf *m;
28406a429a3SArchie Cobbs 	int af;
285ed7509acSJulian Elischer 	int hlen;
286ed7509acSJulian Elischer {
287df5e1987SJonathan Lemon 	int isr;
288df5e1987SJonathan Lemon 	struct ifqueue *inq = 0;
289df8bae1dSRodney W. Grimes 
29006a429a3SArchie Cobbs 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
291ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
29206a429a3SArchie Cobbs 
293963e4c2aSGarrett Wollman 	/* BPF write needs to be handled specially */
29406a429a3SArchie Cobbs 	if (af == AF_UNSPEC) {
29506a429a3SArchie Cobbs 		KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
29606a429a3SArchie Cobbs 		af = *(mtod(m, int *));
297963e4c2aSGarrett Wollman 		m->m_len -= sizeof(int);
298963e4c2aSGarrett Wollman 		m->m_pkthdr.len -= sizeof(int);
299963e4c2aSGarrett Wollman 		m->m_data += sizeof(int);
300963e4c2aSGarrett Wollman 	}
301963e4c2aSGarrett Wollman 
30206a429a3SArchie Cobbs 	/* Let BPF see incoming packet */
303f5fea3ddSPaul Traina 	if (ifp->if_bpf) {
304ed7509acSJulian Elischer 		struct mbuf m0, *n = m;
305ed7509acSJulian Elischer 
30633841545SHajimu UMEMOTO 		if (ifp->if_bpf->bif_dlt == DLT_NULL) {
307df8bae1dSRodney W. Grimes 			/*
308df8bae1dSRodney W. Grimes 			 * We need to prepend the address family as
309df8bae1dSRodney W. Grimes 			 * a four byte field.  Cons up a dummy header
310df8bae1dSRodney W. Grimes 			 * to pacify bpf.  This is safe because bpf
311df8bae1dSRodney W. Grimes 			 * will only read from the mbuf (i.e., it won't
312df8bae1dSRodney W. Grimes 			 * try to free it or keep a pointer a to it).
313df8bae1dSRodney W. Grimes 			 */
314df8bae1dSRodney W. Grimes 			m0.m_next = m;
315df8bae1dSRodney W. Grimes 			m0.m_len = 4;
316df8bae1dSRodney W. Grimes 			m0.m_data = (char *)&af;
317ed7509acSJulian Elischer 			n = &m0;
31833841545SHajimu UMEMOTO 		}
319ed7509acSJulian Elischer 		bpf_mtap(ifp, n);
320df8bae1dSRodney W. Grimes 	}
321df8bae1dSRodney W. Grimes 
322ed7509acSJulian Elischer 	/* Strip away media header */
3233a43ad8fSDoug Rabson 	if (hlen > 0) {
324fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
32523620bdeSDoug Rabson #if defined(__alpha__) || defined(__ia64__)
3263a43ad8fSDoug Rabson 		/* The alpha doesn't like unaligned data.
3273a43ad8fSDoug Rabson 		 * We move data down in the first mbuf */
328fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
32960ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
330fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
331fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
332fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
333fe81f64fSAndrew Gallatin 			    m->m_len);
334fe81f64fSAndrew Gallatin 			mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3);
335fe81f64fSAndrew Gallatin 		}
3363a43ad8fSDoug Rabson #endif
3373a43ad8fSDoug Rabson 	}
338ed7509acSJulian Elischer 
33906a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
34006a429a3SArchie Cobbs 	switch (af) {
341df8bae1dSRodney W. Grimes #ifdef INET
342df8bae1dSRodney W. Grimes 	case AF_INET:
343df5e1987SJonathan Lemon 		inq = &ipintrq;
344df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
345df8bae1dSRodney W. Grimes 		break;
346df8bae1dSRodney W. Grimes #endif
34782cd038dSYoshinobu Inoue #ifdef INET6
34882cd038dSYoshinobu Inoue 	case AF_INET6:
34982cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
350df5e1987SJonathan Lemon 		inq = &ip6intrq;
35182cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
35282cd038dSYoshinobu Inoue 		break;
35382cd038dSYoshinobu Inoue #endif
354cc6a66f2SJulian Elischer #ifdef IPX
355cc6a66f2SJulian Elischer 	case AF_IPX:
356df5e1987SJonathan Lemon 		inq = &ipxintrq;
357cc6a66f2SJulian Elischer 		isr = NETISR_IPX;
358cc6a66f2SJulian Elischer 		break;
359cc6a66f2SJulian Elischer #endif
360df8bae1dSRodney W. Grimes #ifdef NS
361df8bae1dSRodney W. Grimes 	case AF_NS:
362df5e1987SJonathan Lemon 		inq = &nsintrq;
363df8bae1dSRodney W. Grimes 		isr = NETISR_NS;
364df8bae1dSRodney W. Grimes 		break;
365df8bae1dSRodney W. Grimes #endif
366655929bfSJulian Elischer #ifdef NETATALK
367655929bfSJulian Elischer 	case AF_APPLETALK:
368df5e1987SJonathan Lemon 	        inq = &atintrq2;
369655929bfSJulian Elischer 		isr = NETISR_ATALK;
370655929bfSJulian Elischer 		break;
3718cdfefbdSPeter Wemm #endif
372df8bae1dSRodney W. Grimes 	default:
37306a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
374df8bae1dSRodney W. Grimes 		m_freem(m);
375df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
376df8bae1dSRodney W. Grimes 	}
377df8bae1dSRodney W. Grimes 	ifp->if_ipackets++;
378df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len;
379df5e1987SJonathan Lemon 	(void) IF_HANDOFF(inq, m, NULL);
380df5e1987SJonathan Lemon 	schednetisr(isr);
381df8bae1dSRodney W. Grimes 	return (0);
382df8bae1dSRodney W. Grimes }
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes /* ARGSUSED */
3853bda9f9bSPoul-Henning Kamp static void
386df8bae1dSRodney W. Grimes lortrequest(cmd, rt, sa)
387df8bae1dSRodney W. Grimes 	int cmd;
388df8bae1dSRodney W. Grimes 	struct rtentry *rt;
389df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
390df8bae1dSRodney W. Grimes {
3915eb1d25aSGarrett Wollman 	if (rt) {
3925eb1d25aSGarrett Wollman 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
3935eb1d25aSGarrett Wollman 		/*
3945eb1d25aSGarrett Wollman 		 * For optimal performance, the send and receive buffers
3955eb1d25aSGarrett Wollman 		 * should be at least twice the MTU plus a little more for
3965eb1d25aSGarrett Wollman 		 * overhead.
3975eb1d25aSGarrett Wollman 		 */
3985eb1d25aSGarrett Wollman 		rt->rt_rmx.rmx_recvpipe =
3995eb1d25aSGarrett Wollman 			rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
4005eb1d25aSGarrett Wollman 	}
401df8bae1dSRodney W. Grimes }
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes /*
404df8bae1dSRodney W. Grimes  * Process an ioctl request.
405df8bae1dSRodney W. Grimes  */
406df8bae1dSRodney W. Grimes /* ARGSUSED */
407cfa1ca9dSYoshinobu Inoue int
408df8bae1dSRodney W. Grimes loioctl(ifp, cmd, data)
409df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
410ecbb00a2SDoug Rabson 	u_long cmd;
411df8bae1dSRodney W. Grimes 	caddr_t data;
412df8bae1dSRodney W. Grimes {
413df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
41490fd8c38SDavid Greenman 	register struct ifreq *ifr = (struct ifreq *)data;
415df8bae1dSRodney W. Grimes 	register int error = 0;
416df8bae1dSRodney W. Grimes 
417df8bae1dSRodney W. Grimes 	switch (cmd) {
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
420b8dc74a3SGarrett Wollman 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
421df8bae1dSRodney W. Grimes 		ifa = (struct ifaddr *)data;
422df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = lortrequest;
423df8bae1dSRodney W. Grimes 		/*
424df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
425df8bae1dSRodney W. Grimes 		 */
426df8bae1dSRodney W. Grimes 		break;
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
429df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
430df8bae1dSRodney W. Grimes 		if (ifr == 0) {
431df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
432df8bae1dSRodney W. Grimes 			break;
433df8bae1dSRodney W. Grimes 		}
434df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
435df8bae1dSRodney W. Grimes 
436df8bae1dSRodney W. Grimes #ifdef INET
437df8bae1dSRodney W. Grimes 		case AF_INET:
438df8bae1dSRodney W. Grimes 			break;
439df8bae1dSRodney W. Grimes #endif
44082cd038dSYoshinobu Inoue #ifdef INET6
44182cd038dSYoshinobu Inoue 		case AF_INET6:
44282cd038dSYoshinobu Inoue 			break;
44382cd038dSYoshinobu Inoue #endif
444df8bae1dSRodney W. Grimes 
445df8bae1dSRodney W. Grimes 		default:
446df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
447df8bae1dSRodney W. Grimes 			break;
448df8bae1dSRodney W. Grimes 		}
449df8bae1dSRodney W. Grimes 		break;
450df8bae1dSRodney W. Grimes 
45190fd8c38SDavid Greenman 	case SIOCSIFMTU:
45290fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
45390fd8c38SDavid Greenman 		break;
45490fd8c38SDavid Greenman 
455ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
456ce42f1fbSPoul-Henning Kamp 		break;
457ce42f1fbSPoul-Henning Kamp 
458df8bae1dSRodney W. Grimes 	default:
459df8bae1dSRodney W. Grimes 		error = EINVAL;
460df8bae1dSRodney W. Grimes 	}
461df8bae1dSRodney W. Grimes 	return (error);
462df8bae1dSRodney W. Grimes }
463