xref: /freebsd/sys/net/if_loop.c (revision 44bcc068168815cfc0bb9f1c8b692bef67c0e471)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31fe6db7c7SRuslan Ermilov  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
32c3aac50fSPeter Wemm  * $FreeBSD$
33df8bae1dSRodney W. Grimes  */
34df8bae1dSRodney W. Grimes 
35df8bae1dSRodney W. Grimes /*
36df8bae1dSRodney W. Grimes  * Loopback interface driver for protocol testing and timing.
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
391d5e9e22SEivind Eklund #include "opt_inet.h"
40cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
41eedb4959SAndrew Gallatin #include "opt_rss.h"
42430df5f4SEivind Eklund 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
472b120974SPeter Wemm #include <sys/module.h>
483d4ce33dSBrooks Davis #include <machine/bus.h>
493d4ce33dSBrooks Davis #include <sys/rman.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
5151a53488SBruce Evans #include <sys/sockio.h>
5290d9802fSPeter Wemm #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
5576039bc8SGleb Smirnoff #include <net/if_var.h>
56f889d2efSBrooks Davis #include <net/if_clone.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>
614b79449eSBjoern A. Zeeb #include <net/vnet.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 
6882cd038dSYoshinobu Inoue #ifdef INET6
6982cd038dSYoshinobu Inoue #ifndef INET
7082cd038dSYoshinobu Inoue #include <netinet/in.h>
7182cd038dSYoshinobu Inoue #endif
7282cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7482cd038dSYoshinobu Inoue #endif
7582cd038dSYoshinobu Inoue 
763dc85f8dSRobert Watson #include <security/mac/mac_framework.h>
773dc85f8dSRobert Watson 
78db8889c6SDavid Greenman #ifdef TINY_LOMTU
79df8bae1dSRodney W. Grimes #define	LOMTU	(1024+512)
8082cd038dSYoshinobu Inoue #elif defined(LARGE_LOMTU)
8182cd038dSYoshinobu Inoue #define LOMTU	131072
82db8889c6SDavid Greenman #else
83af78195eSDavid Greenman #define LOMTU	16384
84db8889c6SDavid Greenman #endif
85df8bae1dSRodney W. Grimes 
865bb89930SRobert Watson #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
87a6cff10fSMichael Tuexen #define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
88356ab07eSBjoern A. Zeeb #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
89356ab07eSBjoern A. Zeeb 				    CSUM_PSEUDO_HDR | \
905bb89930SRobert Watson 				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
915bb89930SRobert Watson 				    CSUM_SCTP_VALID)
925bb89930SRobert Watson 
933d4ce33dSBrooks Davis int		loioctl(struct ifnet *, u_long, caddr_t);
943d4ce33dSBrooks Davis int		looutput(struct ifnet *ifp, struct mbuf *m,
9547e8d432SGleb Smirnoff 		    const struct sockaddr *dst, struct route *ro);
966b7330e2SSam Leffler static int	lo_clone_create(struct if_clone *, int, caddr_t);
97bb2bfb4fSBrooks Davis static void	lo_clone_destroy(struct ifnet *);
983d4ce33dSBrooks Davis 
99eddfbb76SRobert Watson VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
1003d4ce33dSBrooks Davis 
10137f17770SMarko Zec #ifdef VIMAGE
10242a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, lo_cloner);
1031e77c105SRobert Watson #define	V_lo_cloner		VNET(lo_cloner)
104eddfbb76SRobert Watson #endif
105bfe1aba4SMarko Zec 
10642a58907SGleb Smirnoff static struct if_clone *lo_cloner;
10742a58907SGleb Smirnoff static const char loname[] = "lo";
1083d4ce33dSBrooks Davis 
109bb2bfb4fSBrooks Davis static void
11008304c16SRobert Watson lo_clone_destroy(struct ifnet *ifp)
1113d4ce33dSBrooks Davis {
1123d4ce33dSBrooks Davis 
113bc29160dSMarko Zec #ifndef VIMAGE
114ae5a19beSBrooks Davis 	/* XXX: destroying lo0 will lead to panics. */
115603724d3SBjoern A. Zeeb 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
116bc29160dSMarko Zec #endif
1173d4ce33dSBrooks Davis 
1183d4ce33dSBrooks Davis 	bpfdetach(ifp);
1193d4ce33dSBrooks Davis 	if_detach(ifp);
120fc74a9f9SBrooks Davis 	if_free(ifp);
1213d4ce33dSBrooks Davis }
1223d4ce33dSBrooks Davis 
123bb2bfb4fSBrooks Davis static int
12408304c16SRobert Watson lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
12590d9802fSPeter Wemm {
126fc74a9f9SBrooks Davis 	struct ifnet *ifp;
12790d9802fSPeter Wemm 
1286db9940fSEd Schouten 	ifp = if_alloc(IFT_LOOP);
1296db9940fSEd Schouten 	if (ifp == NULL)
130fc74a9f9SBrooks Davis 		return (ENOSPC);
13190d9802fSPeter Wemm 
13242a58907SGleb Smirnoff 	if_initname(ifp, loname, unit);
133fc74a9f9SBrooks Davis 	ifp->if_mtu = LOMTU;
134fc74a9f9SBrooks Davis 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
135fc74a9f9SBrooks Davis 	ifp->if_ioctl = loioctl;
136fc74a9f9SBrooks Davis 	ifp->if_output = looutput;
137fc74a9f9SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
138356ab07eSBjoern A. Zeeb 	ifp->if_capabilities = ifp->if_capenable =
1392e4531a1SAndrey V. Elsukov 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | IFCAP_LINKSTATE;
140356ab07eSBjoern A. Zeeb 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
141fc74a9f9SBrooks Davis 	if_attach(ifp);
14201399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
143603724d3SBjoern A. Zeeb 	if (V_loif == NULL)
144603724d3SBjoern A. Zeeb 		V_loif = ifp;
1453b16e7b2SMaxime Henrion 
1463b16e7b2SMaxime Henrion 	return (0);
14790d9802fSPeter Wemm }
14890d9802fSPeter Wemm 
149d0728d71SRobert Watson static void
150d0728d71SRobert Watson vnet_loif_init(const void *unused __unused)
1511ed81b73SMarko Zec {
15237f17770SMarko Zec 
15337f17770SMarko Zec #ifdef VIMAGE
15442a58907SGleb Smirnoff 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
15542a58907SGleb Smirnoff 	    1);
156d0728d71SRobert Watson 	V_lo_cloner = lo_cloner;
15737f17770SMarko Zec #else
15842a58907SGleb Smirnoff 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
15942a58907SGleb Smirnoff 	    1);
16037f17770SMarko Zec #endif
1611ed81b73SMarko Zec }
16289856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_loif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
163d0728d71SRobert Watson     vnet_loif_init, NULL);
1641ed81b73SMarko Zec 
165bc29160dSMarko Zec #ifdef VIMAGE
166d0728d71SRobert Watson static void
167d0728d71SRobert Watson vnet_loif_uninit(const void *unused __unused)
168bc29160dSMarko Zec {
169bc29160dSMarko Zec 
17042a58907SGleb Smirnoff 	if_clone_detach(V_lo_cloner);
171bc29160dSMarko Zec 	V_loif = NULL;
172bc29160dSMarko Zec }
17389856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
174d0728d71SRobert Watson     vnet_loif_uninit, NULL);
175bc29160dSMarko Zec #endif
176bc29160dSMarko Zec 
1772b120974SPeter Wemm static int
1782b120974SPeter Wemm loop_modevent(module_t mod, int type, void *data)
179df8bae1dSRodney W. Grimes {
18008304c16SRobert Watson 
1812b120974SPeter Wemm 	switch (type) {
1822b120974SPeter Wemm 	case MOD_LOAD:
1832b120974SPeter Wemm 		break;
18408304c16SRobert Watson 
1852b120974SPeter Wemm 	case MOD_UNLOAD:
1862b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
18708304c16SRobert Watson 		return (EINVAL);
18808304c16SRobert Watson 
1893e019deaSPoul-Henning Kamp 	default:
19008304c16SRobert Watson 		return (EOPNOTSUPP);
191f5fea3ddSPaul Traina 	}
19208304c16SRobert Watson 	return (0);
1932b120974SPeter Wemm }
1942b120974SPeter Wemm 
1952b120974SPeter Wemm static moduledata_t loop_mod = {
1965702371bSRobert Watson 	"if_lo",
1972b120974SPeter Wemm 	loop_modevent,
1989823d527SKevin Lo 	0
1992b120974SPeter Wemm };
2002b120974SPeter Wemm 
2015702371bSRobert Watson DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
202df8bae1dSRodney W. Grimes 
203cfa1ca9dSYoshinobu Inoue int
20447e8d432SGleb Smirnoff looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
205279aa3d4SKip Macy     struct route *ro)
206df8bae1dSRodney W. Grimes {
20701399f34SDavid Malone 	u_int32_t af;
2083dc85f8dSRobert Watson #ifdef MAC
2093dc85f8dSRobert Watson 	int error;
2103dc85f8dSRobert Watson #endif
21101399f34SDavid Malone 
212fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
213ed7509acSJulian Elischer 
2143dc85f8dSRobert Watson #ifdef MAC
2153dc85f8dSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
2163dc85f8dSRobert Watson 	if (error) {
2173dc85f8dSRobert Watson 		m_freem(m);
2183dc85f8dSRobert Watson 		return (error);
2193dc85f8dSRobert Watson 	}
2203dc85f8dSRobert Watson #endif
2213dc85f8dSRobert Watson 
22236402a68SAlexander V. Chernikov 	if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
223ed7509acSJulian Elischer 		m_freem(m);
22436402a68SAlexander V. Chernikov 		return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
225ed7509acSJulian Elischer 	}
22682cd038dSYoshinobu Inoue 
2273751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2283751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
22901399f34SDavid Malone 
230eedb4959SAndrew Gallatin #ifdef RSS
231eedb4959SAndrew Gallatin 	M_HASHTYPE_CLEAR(m);
232eedb4959SAndrew Gallatin #endif
233eedb4959SAndrew Gallatin 
23401399f34SDavid Malone 	/* BPF writes need to be handled specially. */
235e9617c30SPatrick Kelsey 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
23601399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
23747e8d432SGleb Smirnoff 	else
23847e8d432SGleb Smirnoff 		af = dst->sa_family;
23901399f34SDavid Malone 
240201c2527SJulian Elischer #if 1	/* XXX */
24147e8d432SGleb Smirnoff 	switch (af) {
242201c2527SJulian Elischer 	case AF_INET:
2433cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2443cb73e3dSRobert Watson 			m->m_pkthdr.csum_data = 0xffff;
2455bb89930SRobert Watson 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
2463cb73e3dSRobert Watson 		}
2475bb89930SRobert Watson 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
248356ab07eSBjoern A. Zeeb 		break;
249356ab07eSBjoern A. Zeeb 	case AF_INET6:
2505e5c0e79SBjoern A. Zeeb #if 0
2515e5c0e79SBjoern A. Zeeb 		/*
2525e5c0e79SBjoern A. Zeeb 		 * XXX-BZ for now always claim the checksum is good despite
2535e5c0e79SBjoern A. Zeeb 		 * any interface flags.   This is a workaround for 9.1-R and
2545e5c0e79SBjoern A. Zeeb 		 * a proper solution ought to be sought later.
2555e5c0e79SBjoern A. Zeeb 		 */
256356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
257356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
258356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
259356ab07eSBjoern A. Zeeb 		}
2605e5c0e79SBjoern A. Zeeb #else
2615e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_data = 0xffff;
2625e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_flags = LO_CSUM_SET;
2635e5c0e79SBjoern A. Zeeb #endif
264356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
265356ab07eSBjoern A. Zeeb 		break;
266201c2527SJulian Elischer 	default:
26747e8d432SGleb Smirnoff 		printf("looutput: af=%d unexpected\n", af);
268201c2527SJulian Elischer 		m_freem(m);
269201c2527SJulian Elischer 		return (EAFNOSUPPORT);
270201c2527SJulian Elischer 	}
271201c2527SJulian Elischer #endif
27247e8d432SGleb Smirnoff 	return (if_simloop(ifp, m, af, 0));
273ed7509acSJulian Elischer }
274ed7509acSJulian Elischer 
275ed7509acSJulian Elischer /*
276ed7509acSJulian Elischer  * if_simloop()
277ed7509acSJulian Elischer  *
278ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
279ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
280ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
281ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
282ed7509acSJulian Elischer  *
283ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
284ed7509acSJulian Elischer  */
285ed7509acSJulian Elischer int
28608304c16SRobert Watson if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
287ed7509acSJulian Elischer {
288df5e1987SJonathan Lemon 	int isr;
289df8bae1dSRodney W. Grimes 
290fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
2919c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
292ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
29306a429a3SArchie Cobbs 
2943dc85f8dSRobert Watson #ifdef MAC
2953dc85f8dSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
2963dc85f8dSRobert Watson #endif
2973dc85f8dSRobert Watson 
2988343821bSSUZUKI Shinsuke 	/*
2998343821bSSUZUKI Shinsuke 	 * Let BPF see incoming packet in the following manner:
3008343821bSSUZUKI Shinsuke 	 *  - Emulated packet loopback for a simplex interface
3018343821bSSUZUKI Shinsuke 	 *    (net/if_ethersubr.c)
3028343821bSSUZUKI Shinsuke 	 *	-> passes it to ifp's BPF
3038343821bSSUZUKI Shinsuke 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
3048343821bSSUZUKI Shinsuke 	 *	-> not passes it to any BPF
3058343821bSSUZUKI Shinsuke 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
3068343821bSSUZUKI Shinsuke 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
3078343821bSSUZUKI Shinsuke 	 */
3088343821bSSUZUKI Shinsuke 	if (hlen > 0) {
30916d878ccSChristian S.J. Peron 		if (bpf_peers_present(ifp->if_bpf)) {
3108343821bSSUZUKI Shinsuke 			bpf_mtap(ifp->if_bpf, m);
3118343821bSSUZUKI Shinsuke 		}
3128343821bSSUZUKI Shinsuke 	} else {
313603724d3SBjoern A. Zeeb 		if (bpf_peers_present(V_loif->if_bpf)) {
314603724d3SBjoern A. Zeeb 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
3158343821bSSUZUKI Shinsuke 				/* XXX beware sizeof(af) != 4 */
3168343821bSSUZUKI Shinsuke 				u_int32_t af1 = af;
3178343821bSSUZUKI Shinsuke 
318df8bae1dSRodney W. Grimes 				/*
319437ffe18SSam Leffler 				 * We need to prepend the address family.
320df8bae1dSRodney W. Grimes 				 */
321603724d3SBjoern A. Zeeb 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
3228343821bSSUZUKI Shinsuke 			}
3238343821bSSUZUKI Shinsuke 		}
324df8bae1dSRodney W. Grimes 	}
325df8bae1dSRodney W. Grimes 
326ed7509acSJulian Elischer 	/* Strip away media header */
3273a43ad8fSDoug Rabson 	if (hlen > 0) {
328fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
329f6966ecdSOlivier Houchard #ifndef __NO_STRICT_ALIGNMENT
33073dbd3daSJohn Baldwin 		/*
33173dbd3daSJohn Baldwin 		 * Some archs do not like unaligned data, so
33273dbd3daSJohn Baldwin 		 * we move data down in the first mbuf.
33373dbd3daSJohn Baldwin 		 */
334fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
33560ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
336fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
337fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
338fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
339fe81f64fSAndrew Gallatin 			    m->m_len);
340445e045bSAlexander Kabaev 			m->m_data -= (mtod(m,vm_offset_t) & 3);
341fe81f64fSAndrew Gallatin 		}
3423a43ad8fSDoug Rabson #endif
3433a43ad8fSDoug Rabson 	}
344ed7509acSJulian Elischer 
34506a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
34606a429a3SArchie Cobbs 	switch (af) {
347df8bae1dSRodney W. Grimes #ifdef INET
348df8bae1dSRodney W. Grimes 	case AF_INET:
349df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
350df8bae1dSRodney W. Grimes 		break;
351df8bae1dSRodney W. Grimes #endif
35282cd038dSYoshinobu Inoue #ifdef INET6
35382cd038dSYoshinobu Inoue 	case AF_INET6:
35482cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
35582cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
35682cd038dSYoshinobu Inoue 		break;
35782cd038dSYoshinobu Inoue #endif
358df8bae1dSRodney W. Grimes 	default:
35906a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
360df8bae1dSRodney W. Grimes 		m_freem(m);
361df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
362df8bae1dSRodney W. Grimes 	}
3633751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3643751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
3653161f583SAndre Oppermann 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
366df8bae1dSRodney W. Grimes 	return (0);
367df8bae1dSRodney W. Grimes }
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes /*
370df8bae1dSRodney W. Grimes  * Process an ioctl request.
371df8bae1dSRodney W. Grimes  */
372df8bae1dSRodney W. Grimes /* ARGSUSED */
373cfa1ca9dSYoshinobu Inoue int
37408304c16SRobert Watson loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
375df8bae1dSRodney W. Grimes {
37608304c16SRobert Watson 	struct ifreq *ifr = (struct ifreq *)data;
3773cb73e3dSRobert Watson 	int error = 0, mask;
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes 	switch (cmd) {
380df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
38113f4c340SRobert Watson 		ifp->if_flags |= IFF_UP;
38213f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
383*44bcc068SAndrey V. Elsukov 		if_link_state_change(ifp, LINK_STATE_UP);
384df8bae1dSRodney W. Grimes 		/*
385df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
386df8bae1dSRodney W. Grimes 		 */
387df8bae1dSRodney W. Grimes 		break;
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
390df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
391155d72c4SPedro F. Giffuni 		if (ifr == NULL) {
392df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
393df8bae1dSRodney W. Grimes 			break;
394df8bae1dSRodney W. Grimes 		}
395df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes #ifdef INET
398df8bae1dSRodney W. Grimes 		case AF_INET:
399df8bae1dSRodney W. Grimes 			break;
400df8bae1dSRodney W. Grimes #endif
40182cd038dSYoshinobu Inoue #ifdef INET6
40282cd038dSYoshinobu Inoue 		case AF_INET6:
40382cd038dSYoshinobu Inoue 			break;
40482cd038dSYoshinobu Inoue #endif
405df8bae1dSRodney W. Grimes 
406df8bae1dSRodney W. Grimes 		default:
407df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
408df8bae1dSRodney W. Grimes 			break;
409df8bae1dSRodney W. Grimes 		}
410df8bae1dSRodney W. Grimes 		break;
411df8bae1dSRodney W. Grimes 
41290fd8c38SDavid Greenman 	case SIOCSIFMTU:
41390fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
41490fd8c38SDavid Greenman 		break;
41590fd8c38SDavid Greenman 
416ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
4172e4531a1SAndrey V. Elsukov 		if_link_state_change(ifp, (ifp->if_flags & IFF_UP) ?
4182e4531a1SAndrey V. Elsukov 		    LINK_STATE_UP: LINK_STATE_DOWN);
419ce42f1fbSPoul-Henning Kamp 		break;
420ce42f1fbSPoul-Henning Kamp 
4213cb73e3dSRobert Watson 	case SIOCSIFCAP:
4223cb73e3dSRobert Watson 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
4233cb73e3dSRobert Watson 		if ((mask & IFCAP_RXCSUM) != 0)
4243cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_RXCSUM;
4253cb73e3dSRobert Watson 		if ((mask & IFCAP_TXCSUM) != 0)
4263cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_TXCSUM;
4275e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
4285e5c0e79SBjoern A. Zeeb #if 0
429356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
4305e5c0e79SBjoern A. Zeeb #else
4315e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4325e5c0e79SBjoern A. Zeeb 			break;
4335e5c0e79SBjoern A. Zeeb #endif
4345e5c0e79SBjoern A. Zeeb 		}
4355e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
4365e5c0e79SBjoern A. Zeeb #if 0
437356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
4385e5c0e79SBjoern A. Zeeb #else
4395e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4405e5c0e79SBjoern A. Zeeb 			break;
4415e5c0e79SBjoern A. Zeeb #endif
4425e5c0e79SBjoern A. Zeeb 		}
443356ab07eSBjoern A. Zeeb 		ifp->if_hwassist = 0;
4443cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_TXCSUM)
4455bb89930SRobert Watson 			ifp->if_hwassist = LO_CSUM_FEATURES;
4465e5c0e79SBjoern A. Zeeb #if 0
447356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
448356ab07eSBjoern A. Zeeb 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
4495e5c0e79SBjoern A. Zeeb #endif
4503cb73e3dSRobert Watson 		break;
4513cb73e3dSRobert Watson 
452df8bae1dSRodney W. Grimes 	default:
453df8bae1dSRodney W. Grimes 		error = EINVAL;
454df8bae1dSRodney W. Grimes 	}
455df8bae1dSRodney W. Grimes 	return (error);
456df8bae1dSRodney W. Grimes }
457