xref: /freebsd/sys/net/if_loop.c (revision 37f17770e03aae332c6d186042e31ce8f226a0b5)
1c398230bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29fe6db7c7SRuslan Ermilov  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33df8bae1dSRodney W. Grimes /*
34df8bae1dSRodney W. Grimes  * Loopback interface driver for protocol testing and timing.
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
377262d3e4SEivind Eklund #include "opt_atalk.h"
381d5e9e22SEivind Eklund #include "opt_inet.h"
39cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
40430df5f4SEivind Eklund #include "opt_ipx.h"
4133553d6eSBjoern A. Zeeb #include "opt_route.h"
423dc85f8dSRobert Watson #include "opt_mac.h"
43430df5f4SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
482b120974SPeter Wemm #include <sys/module.h>
493d4ce33dSBrooks Davis #include <machine/bus.h>
503d4ce33dSBrooks Davis #include <sys/rman.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
5251a53488SBruce Evans #include <sys/sockio.h>
5390d9802fSPeter Wemm #include <sys/sysctl.h>
54603724d3SBjoern A. Zeeb #include <sys/vimage.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57f889d2efSBrooks Davis #include <net/if_clone.h>
58df8bae1dSRodney W. Grimes #include <net/if_types.h>
59df8bae1dSRodney W. Grimes #include <net/netisr.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes #include <net/bpf.h>
624b79449eSBjoern A. Zeeb #include <net/vnet.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #ifdef	INET
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #endif
68df8bae1dSRodney W. Grimes 
69cc6a66f2SJulian Elischer #ifdef IPX
70cc6a66f2SJulian Elischer #include <netipx/ipx.h>
71cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
72cc6a66f2SJulian Elischer #endif
73cc6a66f2SJulian Elischer 
7482cd038dSYoshinobu Inoue #ifdef INET6
7582cd038dSYoshinobu Inoue #ifndef INET
7682cd038dSYoshinobu Inoue #include <netinet/in.h>
7782cd038dSYoshinobu Inoue #endif
7882cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
79686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
8082cd038dSYoshinobu Inoue #endif
8182cd038dSYoshinobu Inoue 
82655929bfSJulian Elischer #ifdef NETATALK
83655929bfSJulian Elischer #include <netatalk/at.h>
84655929bfSJulian Elischer #include <netatalk/at_var.h>
858cdfefbdSPeter Wemm #endif
86655929bfSJulian Elischer 
873dc85f8dSRobert Watson #include <security/mac/mac_framework.h>
883dc85f8dSRobert Watson 
89db8889c6SDavid Greenman #ifdef TINY_LOMTU
90df8bae1dSRodney W. Grimes #define	LOMTU	(1024+512)
9182cd038dSYoshinobu Inoue #elif defined(LARGE_LOMTU)
9282cd038dSYoshinobu Inoue #define LOMTU	131072
93db8889c6SDavid Greenman #else
94af78195eSDavid Greenman #define LOMTU	16384
95db8889c6SDavid Greenman #endif
96df8bae1dSRodney W. Grimes 
975bb89930SRobert Watson #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
985bb89930SRobert Watson #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_PSEUDO_HDR | \
995bb89930SRobert Watson 				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
1005bb89930SRobert Watson 				    CSUM_SCTP_VALID)
1015bb89930SRobert Watson 
1023d4ce33dSBrooks Davis int		loioctl(struct ifnet *, u_long, caddr_t);
1033d4ce33dSBrooks Davis static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
1043d4ce33dSBrooks Davis int		looutput(struct ifnet *ifp, struct mbuf *m,
105279aa3d4SKip Macy 		    struct sockaddr *dst, struct route *ro);
1066b7330e2SSam Leffler static int	lo_clone_create(struct if_clone *, int, caddr_t);
107bb2bfb4fSBrooks Davis static void	lo_clone_destroy(struct ifnet *);
1081ed81b73SMarko Zec static int	vnet_loif_iattach(const void *);
1093d4ce33dSBrooks Davis 
11044e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
11144e33a07SMarko Zec struct ifnet *loif;			/* Used externally */
11244e33a07SMarko Zec #endif
1133d4ce33dSBrooks Davis 
11437f17770SMarko Zec #ifdef VIMAGE
11537f17770SMarko Zec MALLOC_DEFINE(M_LO_CLONER, "lo_cloner", "lo_cloner");
11637f17770SMarko Zec #endif
11737f17770SMarko Zec 
118bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
119bfe1aba4SMarko Zec static const vnet_modinfo_t vnet_loif_modinfo = {
120bfe1aba4SMarko Zec 	.vmi_id		= VNET_MOD_LOIF,
12137f17770SMarko Zec 	.vmi_dependson	= VNET_MOD_IF_CLONE,
122bfe1aba4SMarko Zec 	.vmi_name	= "loif",
123bfe1aba4SMarko Zec 	.vmi_iattach	= vnet_loif_iattach
124bfe1aba4SMarko Zec };
125bfe1aba4SMarko Zec #endif /* !VIMAGE_GLOBALS */
126bfe1aba4SMarko Zec 
127f889d2efSBrooks Davis IFC_SIMPLE_DECLARE(lo, 1);
1283d4ce33dSBrooks Davis 
129bb2bfb4fSBrooks Davis static void
13008304c16SRobert Watson lo_clone_destroy(struct ifnet *ifp)
1313d4ce33dSBrooks Davis {
13297021c24SMarko Zec #ifdef INVARIANTS
13397021c24SMarko Zec 	INIT_VNET_NET(ifp->if_vnet);
13497021c24SMarko Zec #endif
1353d4ce33dSBrooks Davis 
136ae5a19beSBrooks Davis 	/* XXX: destroying lo0 will lead to panics. */
137603724d3SBjoern A. Zeeb 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
1383d4ce33dSBrooks Davis 
1393d4ce33dSBrooks Davis 	bpfdetach(ifp);
1403d4ce33dSBrooks Davis 	if_detach(ifp);
141fc74a9f9SBrooks Davis 	if_free(ifp);
1423d4ce33dSBrooks Davis }
1433d4ce33dSBrooks Davis 
144bb2bfb4fSBrooks Davis static int
14508304c16SRobert Watson lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
14690d9802fSPeter Wemm {
1478b615593SMarko Zec 	INIT_VNET_NET(curvnet);
148fc74a9f9SBrooks Davis 	struct ifnet *ifp;
14990d9802fSPeter Wemm 
1506db9940fSEd Schouten 	ifp = if_alloc(IFT_LOOP);
1516db9940fSEd Schouten 	if (ifp == NULL)
152fc74a9f9SBrooks Davis 		return (ENOSPC);
15390d9802fSPeter Wemm 
154fc74a9f9SBrooks Davis 	if_initname(ifp, ifc->ifc_name, unit);
155fc74a9f9SBrooks Davis 	ifp->if_mtu = LOMTU;
156fc74a9f9SBrooks Davis 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
157fc74a9f9SBrooks Davis 	ifp->if_ioctl = loioctl;
158fc74a9f9SBrooks Davis 	ifp->if_output = looutput;
159fc74a9f9SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
160fe89fae1SRobert Watson 	ifp->if_capabilities = ifp->if_capenable = IFCAP_HWCSUM;
1615bb89930SRobert Watson 	ifp->if_hwassist = LO_CSUM_FEATURES;
162fc74a9f9SBrooks Davis 	if_attach(ifp);
16301399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
164603724d3SBjoern A. Zeeb 	if (V_loif == NULL)
165603724d3SBjoern A. Zeeb 		V_loif = ifp;
1663b16e7b2SMaxime Henrion 
1673b16e7b2SMaxime Henrion 	return (0);
16890d9802fSPeter Wemm }
16990d9802fSPeter Wemm 
1701ed81b73SMarko Zec static int vnet_loif_iattach(const void *unused __unused)
1711ed81b73SMarko Zec {
1721ed81b73SMarko Zec 	INIT_VNET_NET(curvnet);
1731ed81b73SMarko Zec 
1741ed81b73SMarko Zec 	V_loif = NULL;
17537f17770SMarko Zec 
17637f17770SMarko Zec #ifdef VIMAGE
17737f17770SMarko Zec 	V_lo_cloner = malloc(sizeof(*V_lo_cloner), M_LO_CLONER,
17837f17770SMarko Zec 	    M_WAITOK | M_ZERO);
17937f17770SMarko Zec 	bcopy(&lo_cloner, V_lo_cloner, sizeof(*V_lo_cloner));
18037f17770SMarko Zec 	if_clone_attach(V_lo_cloner);
18137f17770SMarko Zec #else
1821ed81b73SMarko Zec 	if_clone_attach(&lo_cloner);
18337f17770SMarko Zec #endif
1841ed81b73SMarko Zec 	return (0);
1851ed81b73SMarko Zec }
1861ed81b73SMarko Zec 
1872b120974SPeter Wemm static int
1882b120974SPeter Wemm loop_modevent(module_t mod, int type, void *data)
189df8bae1dSRodney W. Grimes {
19008304c16SRobert Watson 
1912b120974SPeter Wemm 	switch (type) {
1922b120974SPeter Wemm 	case MOD_LOAD:
193bfe1aba4SMarko Zec #ifndef VIMAGE_GLOBALS
194bfe1aba4SMarko Zec 		vnet_mod_register(&vnet_loif_modinfo);
195bfe1aba4SMarko Zec #else
1961ed81b73SMarko Zec 		vnet_loif_iattach(NULL);
197bfe1aba4SMarko Zec #endif
1982b120974SPeter Wemm 		break;
19908304c16SRobert Watson 
2002b120974SPeter Wemm 	case MOD_UNLOAD:
2012b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
20208304c16SRobert Watson 		return (EINVAL);
20308304c16SRobert Watson 
2043e019deaSPoul-Henning Kamp 	default:
20508304c16SRobert Watson 		return (EOPNOTSUPP);
206f5fea3ddSPaul Traina 	}
20708304c16SRobert Watson 	return (0);
2082b120974SPeter Wemm }
2092b120974SPeter Wemm 
2102b120974SPeter Wemm static moduledata_t loop_mod = {
2112b120974SPeter Wemm 	"loop",
2122b120974SPeter Wemm 	loop_modevent,
2132b120974SPeter Wemm 	0
2142b120974SPeter Wemm };
2152b120974SPeter Wemm 
21678ece553SAlexander Kabaev DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
217df8bae1dSRodney W. Grimes 
218cfa1ca9dSYoshinobu Inoue int
21908304c16SRobert Watson looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
220279aa3d4SKip Macy     struct route *ro)
221df8bae1dSRodney W. Grimes {
22201399f34SDavid Malone 	u_int32_t af;
223279aa3d4SKip Macy 	struct rtentry *rt = NULL;
2243dc85f8dSRobert Watson #ifdef MAC
2253dc85f8dSRobert Watson 	int error;
2263dc85f8dSRobert Watson #endif
22701399f34SDavid Malone 
228fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
229ed7509acSJulian Elischer 
230279aa3d4SKip Macy 	if (ro != NULL)
231279aa3d4SKip Macy 		rt = ro->ro_rt;
2323dc85f8dSRobert Watson #ifdef MAC
2333dc85f8dSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
2343dc85f8dSRobert Watson 	if (error) {
2353dc85f8dSRobert Watson 		m_freem(m);
2363dc85f8dSRobert Watson 		return (error);
2373dc85f8dSRobert Watson 	}
2383dc85f8dSRobert Watson #endif
2393dc85f8dSRobert Watson 
240ed7509acSJulian Elischer 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
241ed7509acSJulian Elischer 		m_freem(m);
242ed7509acSJulian Elischer 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
243ed7509acSJulian Elischer 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
244ed7509acSJulian Elischer 	}
24582cd038dSYoshinobu Inoue 
246ed7509acSJulian Elischer 	ifp->if_opackets++;
247ed7509acSJulian Elischer 	ifp->if_obytes += m->m_pkthdr.len;
24801399f34SDavid Malone 
24901399f34SDavid Malone 	/* BPF writes need to be handled specially. */
25001399f34SDavid Malone 	if (dst->sa_family == AF_UNSPEC) {
25101399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
25201399f34SDavid Malone 		dst->sa_family = af;
25301399f34SDavid Malone 	}
25401399f34SDavid Malone 
255201c2527SJulian Elischer #if 1	/* XXX */
256201c2527SJulian Elischer 	switch (dst->sa_family) {
257201c2527SJulian Elischer 	case AF_INET:
2583cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2593cb73e3dSRobert Watson 			m->m_pkthdr.csum_data = 0xffff;
2605bb89930SRobert Watson 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
2613cb73e3dSRobert Watson 		}
2625bb89930SRobert Watson 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
26382cd038dSYoshinobu Inoue 	case AF_INET6:
264201c2527SJulian Elischer 	case AF_IPX:
265201c2527SJulian Elischer 	case AF_APPLETALK:
26605b3ccefSJulian Elischer 		break;
267201c2527SJulian Elischer 	default:
26871582bf5SRobert Watson 		printf("looutput: af=%d unexpected\n", dst->sa_family);
269201c2527SJulian Elischer 		m_freem(m);
270201c2527SJulian Elischer 		return (EAFNOSUPPORT);
271201c2527SJulian Elischer 	}
272201c2527SJulian Elischer #endif
27306a429a3SArchie Cobbs 	return (if_simloop(ifp, m, dst->sa_family, 0));
274ed7509acSJulian Elischer }
275ed7509acSJulian Elischer 
276ed7509acSJulian Elischer /*
277ed7509acSJulian Elischer  * if_simloop()
278ed7509acSJulian Elischer  *
279ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
280ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
281ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
282ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
283ed7509acSJulian Elischer  *
284ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
285ed7509acSJulian Elischer  */
286ed7509acSJulian Elischer int
28708304c16SRobert Watson if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
288ed7509acSJulian Elischer {
2898b615593SMarko Zec 	INIT_VNET_NET(ifp->if_vnet);
290df5e1987SJonathan Lemon 	int isr;
291df8bae1dSRodney W. Grimes 
292fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
2939c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
294ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
29506a429a3SArchie Cobbs 
2963dc85f8dSRobert Watson #ifdef MAC
2973dc85f8dSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
2983dc85f8dSRobert Watson #endif
2993dc85f8dSRobert Watson 
3008343821bSSUZUKI Shinsuke 	/*
3018343821bSSUZUKI Shinsuke 	 * Let BPF see incoming packet in the following manner:
3028343821bSSUZUKI Shinsuke 	 *  - Emulated packet loopback for a simplex interface
3038343821bSSUZUKI Shinsuke 	 *    (net/if_ethersubr.c)
3048343821bSSUZUKI Shinsuke 	 *	-> passes it to ifp's BPF
3058343821bSSUZUKI Shinsuke 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
3068343821bSSUZUKI Shinsuke 	 *	-> not passes it to any BPF
3078343821bSSUZUKI Shinsuke 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
3088343821bSSUZUKI Shinsuke 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
3098343821bSSUZUKI Shinsuke 	 */
3108343821bSSUZUKI Shinsuke 	if (hlen > 0) {
31116d878ccSChristian S.J. Peron 		if (bpf_peers_present(ifp->if_bpf)) {
3128343821bSSUZUKI Shinsuke 			bpf_mtap(ifp->if_bpf, m);
3138343821bSSUZUKI Shinsuke 		}
3148343821bSSUZUKI Shinsuke 	} else {
315603724d3SBjoern A. Zeeb 		if (bpf_peers_present(V_loif->if_bpf)) {
316603724d3SBjoern A. Zeeb 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
3178343821bSSUZUKI Shinsuke 				/* XXX beware sizeof(af) != 4 */
3188343821bSSUZUKI Shinsuke 				u_int32_t af1 = af;
3198343821bSSUZUKI Shinsuke 
320df8bae1dSRodney W. Grimes 				/*
321437ffe18SSam Leffler 				 * We need to prepend the address family.
322df8bae1dSRodney W. Grimes 				 */
323603724d3SBjoern A. Zeeb 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
3248343821bSSUZUKI Shinsuke 			}
3258343821bSSUZUKI Shinsuke 		}
326df8bae1dSRodney W. Grimes 	}
327df8bae1dSRodney W. Grimes 
328ed7509acSJulian Elischer 	/* Strip away media header */
3293a43ad8fSDoug Rabson 	if (hlen > 0) {
330fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
331f6966ecdSOlivier Houchard #ifndef __NO_STRICT_ALIGNMENT
33273dbd3daSJohn Baldwin 		/*
33373dbd3daSJohn Baldwin 		 * Some archs do not like unaligned data, so
33473dbd3daSJohn Baldwin 		 * we move data down in the first mbuf.
33573dbd3daSJohn Baldwin 		 */
336fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
33760ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
338fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
339fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
340fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
341fe81f64fSAndrew Gallatin 			    m->m_len);
342445e045bSAlexander Kabaev 			m->m_data -= (mtod(m,vm_offset_t) & 3);
343fe81f64fSAndrew Gallatin 		}
3443a43ad8fSDoug Rabson #endif
3453a43ad8fSDoug Rabson 	}
346ed7509acSJulian Elischer 
34706a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
34806a429a3SArchie Cobbs 	switch (af) {
349df8bae1dSRodney W. Grimes #ifdef INET
350df8bae1dSRodney W. Grimes 	case AF_INET:
351df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
352df8bae1dSRodney W. Grimes 		break;
353df8bae1dSRodney W. Grimes #endif
35482cd038dSYoshinobu Inoue #ifdef INET6
35582cd038dSYoshinobu Inoue 	case AF_INET6:
35682cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
35782cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
35882cd038dSYoshinobu Inoue 		break;
35982cd038dSYoshinobu Inoue #endif
360cc6a66f2SJulian Elischer #ifdef IPX
361cc6a66f2SJulian Elischer 	case AF_IPX:
362cc6a66f2SJulian Elischer 		isr = NETISR_IPX;
363cc6a66f2SJulian Elischer 		break;
364cc6a66f2SJulian Elischer #endif
365655929bfSJulian Elischer #ifdef NETATALK
366655929bfSJulian Elischer 	case AF_APPLETALK:
3671cafed39SJonathan Lemon 		isr = NETISR_ATALK2;
368655929bfSJulian Elischer 		break;
3698cdfefbdSPeter Wemm #endif
370df8bae1dSRodney W. Grimes 	default:
37106a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
372df8bae1dSRodney W. Grimes 		m_freem(m);
373df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
374df8bae1dSRodney W. Grimes 	}
375df8bae1dSRodney W. Grimes 	ifp->if_ipackets++;
376df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len;
3773161f583SAndre Oppermann 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
378df8bae1dSRodney W. Grimes 	return (0);
379df8bae1dSRodney W. Grimes }
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes /* ARGSUSED */
3823bda9f9bSPoul-Henning Kamp static void
38308304c16SRobert Watson lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
384df8bae1dSRodney W. Grimes {
38508304c16SRobert Watson 
386d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
38797d8d152SAndre Oppermann 	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
388df8bae1dSRodney W. Grimes }
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes /*
391df8bae1dSRodney W. Grimes  * Process an ioctl request.
392df8bae1dSRodney W. Grimes  */
393df8bae1dSRodney W. Grimes /* ARGSUSED */
394cfa1ca9dSYoshinobu Inoue int
39508304c16SRobert Watson loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
396df8bae1dSRodney W. Grimes {
39708304c16SRobert Watson 	struct ifaddr *ifa;
39808304c16SRobert Watson 	struct ifreq *ifr = (struct ifreq *)data;
3993cb73e3dSRobert Watson 	int error = 0, mask;
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes 	switch (cmd) {
402df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
40313f4c340SRobert Watson 		ifp->if_flags |= IFF_UP;
40413f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
405df8bae1dSRodney W. Grimes 		ifa = (struct ifaddr *)data;
406df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = lortrequest;
407df8bae1dSRodney W. Grimes 		/*
408df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
409df8bae1dSRodney W. Grimes 		 */
410df8bae1dSRodney W. Grimes 		break;
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
413df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
414df8bae1dSRodney W. Grimes 		if (ifr == 0) {
415df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
416df8bae1dSRodney W. Grimes 			break;
417df8bae1dSRodney W. Grimes 		}
418df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
419df8bae1dSRodney W. Grimes 
420df8bae1dSRodney W. Grimes #ifdef INET
421df8bae1dSRodney W. Grimes 		case AF_INET:
422df8bae1dSRodney W. Grimes 			break;
423df8bae1dSRodney W. Grimes #endif
42482cd038dSYoshinobu Inoue #ifdef INET6
42582cd038dSYoshinobu Inoue 		case AF_INET6:
42682cd038dSYoshinobu Inoue 			break;
42782cd038dSYoshinobu Inoue #endif
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 		default:
430df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
431df8bae1dSRodney W. Grimes 			break;
432df8bae1dSRodney W. Grimes 		}
433df8bae1dSRodney W. Grimes 		break;
434df8bae1dSRodney W. Grimes 
43590fd8c38SDavid Greenman 	case SIOCSIFMTU:
43690fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
43790fd8c38SDavid Greenman 		break;
43890fd8c38SDavid Greenman 
439ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
440ce42f1fbSPoul-Henning Kamp 		break;
441ce42f1fbSPoul-Henning Kamp 
4423cb73e3dSRobert Watson 	case SIOCSIFCAP:
4433cb73e3dSRobert Watson 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
4443cb73e3dSRobert Watson 		if ((mask & IFCAP_RXCSUM) != 0)
4453cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_RXCSUM;
4463cb73e3dSRobert Watson 		if ((mask & IFCAP_TXCSUM) != 0)
4473cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_TXCSUM;
4483cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_TXCSUM)
4495bb89930SRobert Watson 			ifp->if_hwassist = LO_CSUM_FEATURES;
4503cb73e3dSRobert Watson 		else
4513cb73e3dSRobert Watson 			ifp->if_hwassist = 0;
4523cb73e3dSRobert Watson 		break;
4533cb73e3dSRobert Watson 
454df8bae1dSRodney W. Grimes 	default:
455df8bae1dSRodney W. Grimes 		error = EINVAL;
456df8bae1dSRodney W. Grimes 	}
457df8bae1dSRodney W. Grimes 	return (error);
458df8bae1dSRodney W. Grimes }
459