xref: /freebsd/sys/net/if_loop.c (revision 91ebcbe02a48ebd40edb49283b90f38d246da15a)
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 
93ddc0ed58SGleb Smirnoff static int	loioctl(struct ifnet *, u_long, caddr_t);
94ddc0ed58SGleb Smirnoff static int	looutput(struct ifnet *ifp, struct mbuf *m,
9547e8d432SGleb Smirnoff 		    const struct sockaddr *dst, struct route *ro);
963d4ce33dSBrooks Davis 
97eddfbb76SRobert Watson VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
983d4ce33dSBrooks Davis 
9937f17770SMarko Zec #ifdef VIMAGE
1005f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, lo_cloner);
1011e77c105SRobert Watson #define	V_lo_cloner		VNET(lo_cloner)
102eddfbb76SRobert Watson #endif
103bfe1aba4SMarko Zec 
10442a58907SGleb Smirnoff static struct if_clone *lo_cloner;
10542a58907SGleb Smirnoff static const char loname[] = "lo";
1063d4ce33dSBrooks Davis 
107*91ebcbe0SAlexander V. Chernikov static int
108*91ebcbe0SAlexander V. Chernikov lo_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1093d4ce33dSBrooks Davis {
110*91ebcbe0SAlexander V. Chernikov 	if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
111*91ebcbe0SAlexander V. Chernikov 		return (EINVAL);
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);
121*91ebcbe0SAlexander V. Chernikov 
122*91ebcbe0SAlexander V. Chernikov 	return (0);
1233d4ce33dSBrooks Davis }
1243d4ce33dSBrooks Davis 
125bb2bfb4fSBrooks Davis static int
126*91ebcbe0SAlexander V. Chernikov lo_clone_create(struct if_clone *ifc, char *name, size_t len,
127*91ebcbe0SAlexander V. Chernikov     struct ifc_data *ifd, struct ifnet **ifpp)
12890d9802fSPeter Wemm {
129fc74a9f9SBrooks Davis 	struct ifnet *ifp;
13090d9802fSPeter Wemm 
1316db9940fSEd Schouten 	ifp = if_alloc(IFT_LOOP);
1326db9940fSEd Schouten 	if (ifp == NULL)
133fc74a9f9SBrooks Davis 		return (ENOSPC);
13490d9802fSPeter Wemm 
135*91ebcbe0SAlexander V. Chernikov 	if_initname(ifp, loname, ifd->unit);
136fc74a9f9SBrooks Davis 	ifp->if_mtu = LOMTU;
137fc74a9f9SBrooks Davis 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
138fc74a9f9SBrooks Davis 	ifp->if_ioctl = loioctl;
139fc74a9f9SBrooks Davis 	ifp->if_output = looutput;
140fc74a9f9SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
141356ab07eSBjoern A. Zeeb 	ifp->if_capabilities = ifp->if_capenable =
1422e4531a1SAndrey V. Elsukov 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | IFCAP_LINKSTATE;
143356ab07eSBjoern A. Zeeb 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
144fc74a9f9SBrooks Davis 	if_attach(ifp);
14501399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
146603724d3SBjoern A. Zeeb 	if (V_loif == NULL)
147603724d3SBjoern A. Zeeb 		V_loif = ifp;
148*91ebcbe0SAlexander V. Chernikov 	*ifpp = ifp;
1493b16e7b2SMaxime Henrion 
1503b16e7b2SMaxime Henrion 	return (0);
15190d9802fSPeter Wemm }
15290d9802fSPeter Wemm 
153d0728d71SRobert Watson static void
154d0728d71SRobert Watson vnet_loif_init(const void *unused __unused)
1551ed81b73SMarko Zec {
156*91ebcbe0SAlexander V. Chernikov 	struct if_clone_addreq req = {
157*91ebcbe0SAlexander V. Chernikov 		.create_f = lo_clone_create,
158*91ebcbe0SAlexander V. Chernikov 		.destroy_f = lo_clone_destroy,
159*91ebcbe0SAlexander V. Chernikov 		.flags = IFC_F_AUTOUNIT,
160*91ebcbe0SAlexander V. Chernikov 	};
161*91ebcbe0SAlexander V. Chernikov 	lo_cloner = ifc_attach_cloner(loname, &req);
16237f17770SMarko Zec #ifdef VIMAGE
163d0728d71SRobert Watson 	V_lo_cloner = lo_cloner;
16437f17770SMarko Zec #endif
165*91ebcbe0SAlexander V. Chernikov 	struct ifc_data ifd = { .unit = 0 };
166*91ebcbe0SAlexander V. Chernikov 	ifc_create_ifp(loname, &ifd, NULL);
1671ed81b73SMarko Zec }
16889856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_loif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
169d0728d71SRobert Watson     vnet_loif_init, NULL);
1701ed81b73SMarko Zec 
171bc29160dSMarko Zec #ifdef VIMAGE
172d0728d71SRobert Watson static void
173d0728d71SRobert Watson vnet_loif_uninit(const void *unused __unused)
174bc29160dSMarko Zec {
175bc29160dSMarko Zec 
176*91ebcbe0SAlexander V. Chernikov 	ifc_detach_cloner(V_lo_cloner);
177bc29160dSMarko Zec 	V_loif = NULL;
178bc29160dSMarko Zec }
17989856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
180d0728d71SRobert Watson     vnet_loif_uninit, NULL);
181bc29160dSMarko Zec #endif
182bc29160dSMarko Zec 
1832b120974SPeter Wemm static int
1842b120974SPeter Wemm loop_modevent(module_t mod, int type, void *data)
185df8bae1dSRodney W. Grimes {
18608304c16SRobert Watson 
1872b120974SPeter Wemm 	switch (type) {
1882b120974SPeter Wemm 	case MOD_LOAD:
1892b120974SPeter Wemm 		break;
19008304c16SRobert Watson 
1912b120974SPeter Wemm 	case MOD_UNLOAD:
1922b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
19308304c16SRobert Watson 		return (EINVAL);
19408304c16SRobert Watson 
1953e019deaSPoul-Henning Kamp 	default:
19608304c16SRobert Watson 		return (EOPNOTSUPP);
197f5fea3ddSPaul Traina 	}
19808304c16SRobert Watson 	return (0);
1992b120974SPeter Wemm }
2002b120974SPeter Wemm 
2012b120974SPeter Wemm static moduledata_t loop_mod = {
2025702371bSRobert Watson 	"if_lo",
2032b120974SPeter Wemm 	loop_modevent,
2049823d527SKevin Lo 	0
2052b120974SPeter Wemm };
2062b120974SPeter Wemm 
2075702371bSRobert Watson DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
208df8bae1dSRodney W. Grimes 
209ddc0ed58SGleb Smirnoff static int
21047e8d432SGleb Smirnoff looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
211279aa3d4SKip Macy     struct route *ro)
212df8bae1dSRodney W. Grimes {
21301399f34SDavid Malone 	u_int32_t af;
2143dc85f8dSRobert Watson #ifdef MAC
2153dc85f8dSRobert Watson 	int error;
2163dc85f8dSRobert Watson #endif
21701399f34SDavid Malone 
218fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
219ed7509acSJulian Elischer 
2203dc85f8dSRobert Watson #ifdef MAC
2213dc85f8dSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
2223dc85f8dSRobert Watson 	if (error) {
2233dc85f8dSRobert Watson 		m_freem(m);
2243dc85f8dSRobert Watson 		return (error);
2253dc85f8dSRobert Watson 	}
2263dc85f8dSRobert Watson #endif
2273dc85f8dSRobert Watson 
22836402a68SAlexander V. Chernikov 	if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
229ed7509acSJulian Elischer 		m_freem(m);
23036402a68SAlexander V. Chernikov 		return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
231ed7509acSJulian Elischer 	}
23282cd038dSYoshinobu Inoue 
2333751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2343751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
23501399f34SDavid Malone 
236eedb4959SAndrew Gallatin #ifdef RSS
237eedb4959SAndrew Gallatin 	M_HASHTYPE_CLEAR(m);
238eedb4959SAndrew Gallatin #endif
239eedb4959SAndrew Gallatin 
24001399f34SDavid Malone 	/* BPF writes need to be handled specially. */
241e9617c30SPatrick Kelsey 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
24201399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
24347e8d432SGleb Smirnoff 	else
24462e1a437SZhenlei Huang 		af = RO_GET_FAMILY(ro, dst);
24501399f34SDavid Malone 
246201c2527SJulian Elischer #if 1	/* XXX */
24747e8d432SGleb Smirnoff 	switch (af) {
248201c2527SJulian Elischer 	case AF_INET:
2493cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2503cb73e3dSRobert Watson 			m->m_pkthdr.csum_data = 0xffff;
2515bb89930SRobert Watson 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
2523cb73e3dSRobert Watson 		}
2535bb89930SRobert Watson 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
254356ab07eSBjoern A. Zeeb 		break;
255356ab07eSBjoern A. Zeeb 	case AF_INET6:
2565e5c0e79SBjoern A. Zeeb #if 0
2575e5c0e79SBjoern A. Zeeb 		/*
2585e5c0e79SBjoern A. Zeeb 		 * XXX-BZ for now always claim the checksum is good despite
2595e5c0e79SBjoern A. Zeeb 		 * any interface flags.   This is a workaround for 9.1-R and
2605e5c0e79SBjoern A. Zeeb 		 * a proper solution ought to be sought later.
2615e5c0e79SBjoern A. Zeeb 		 */
262356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
263356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
264356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
265356ab07eSBjoern A. Zeeb 		}
2665e5c0e79SBjoern A. Zeeb #else
2675e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_data = 0xffff;
2685e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_flags = LO_CSUM_SET;
2695e5c0e79SBjoern A. Zeeb #endif
270356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
271356ab07eSBjoern A. Zeeb 		break;
272201c2527SJulian Elischer 	default:
27347e8d432SGleb Smirnoff 		printf("looutput: af=%d unexpected\n", af);
274201c2527SJulian Elischer 		m_freem(m);
275201c2527SJulian Elischer 		return (EAFNOSUPPORT);
276201c2527SJulian Elischer 	}
277201c2527SJulian Elischer #endif
27847e8d432SGleb Smirnoff 	return (if_simloop(ifp, m, af, 0));
279ed7509acSJulian Elischer }
280ed7509acSJulian Elischer 
281ed7509acSJulian Elischer /*
282ed7509acSJulian Elischer  * if_simloop()
283ed7509acSJulian Elischer  *
284ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
285ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
286ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
287ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
288ed7509acSJulian Elischer  *
289ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
290ed7509acSJulian Elischer  */
291ed7509acSJulian Elischer int
29208304c16SRobert Watson if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
293ed7509acSJulian Elischer {
294df5e1987SJonathan Lemon 	int isr;
295df8bae1dSRodney W. Grimes 
296fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
2979c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
298ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
29906a429a3SArchie Cobbs 
3003dc85f8dSRobert Watson #ifdef MAC
3013dc85f8dSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
3023dc85f8dSRobert Watson #endif
3033dc85f8dSRobert Watson 
3048343821bSSUZUKI Shinsuke 	/*
3058343821bSSUZUKI Shinsuke 	 * Let BPF see incoming packet in the following manner:
3068343821bSSUZUKI Shinsuke 	 *  - Emulated packet loopback for a simplex interface
3078343821bSSUZUKI Shinsuke 	 *    (net/if_ethersubr.c)
3088343821bSSUZUKI Shinsuke 	 *	-> passes it to ifp's BPF
3098343821bSSUZUKI Shinsuke 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
3108343821bSSUZUKI Shinsuke 	 *	-> not passes it to any BPF
3118343821bSSUZUKI Shinsuke 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
3128343821bSSUZUKI Shinsuke 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
3138343821bSSUZUKI Shinsuke 	 */
3148343821bSSUZUKI Shinsuke 	if (hlen > 0) {
31516d878ccSChristian S.J. Peron 		if (bpf_peers_present(ifp->if_bpf)) {
3168343821bSSUZUKI Shinsuke 			bpf_mtap(ifp->if_bpf, m);
3178343821bSSUZUKI Shinsuke 		}
3188343821bSSUZUKI Shinsuke 	} else {
319603724d3SBjoern A. Zeeb 		if (bpf_peers_present(V_loif->if_bpf)) {
320603724d3SBjoern A. Zeeb 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
3218343821bSSUZUKI Shinsuke 				/* XXX beware sizeof(af) != 4 */
3228343821bSSUZUKI Shinsuke 				u_int32_t af1 = af;
3238343821bSSUZUKI Shinsuke 
324df8bae1dSRodney W. Grimes 				/*
325437ffe18SSam Leffler 				 * We need to prepend the address family.
326df8bae1dSRodney W. Grimes 				 */
327603724d3SBjoern A. Zeeb 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
3288343821bSSUZUKI Shinsuke 			}
3298343821bSSUZUKI Shinsuke 		}
330df8bae1dSRodney W. Grimes 	}
331df8bae1dSRodney W. Grimes 
332ed7509acSJulian Elischer 	/* Strip away media header */
3333a43ad8fSDoug Rabson 	if (hlen > 0) {
334fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
335f6966ecdSOlivier Houchard #ifndef __NO_STRICT_ALIGNMENT
33673dbd3daSJohn Baldwin 		/*
33773dbd3daSJohn Baldwin 		 * Some archs do not like unaligned data, so
33873dbd3daSJohn Baldwin 		 * we move data down in the first mbuf.
33973dbd3daSJohn Baldwin 		 */
340fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
34160ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
342fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
343fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
344fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
345fe81f64fSAndrew Gallatin 			    m->m_len);
346445e045bSAlexander Kabaev 			m->m_data -= (mtod(m,vm_offset_t) & 3);
347fe81f64fSAndrew Gallatin 		}
3483a43ad8fSDoug Rabson #endif
3493a43ad8fSDoug Rabson 	}
350ed7509acSJulian Elischer 
35106a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
35206a429a3SArchie Cobbs 	switch (af) {
353df8bae1dSRodney W. Grimes #ifdef INET
354df8bae1dSRodney W. Grimes 	case AF_INET:
355df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
356df8bae1dSRodney W. Grimes 		break;
357df8bae1dSRodney W. Grimes #endif
35882cd038dSYoshinobu Inoue #ifdef INET6
35982cd038dSYoshinobu Inoue 	case AF_INET6:
36082cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
36182cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
36282cd038dSYoshinobu Inoue 		break;
36382cd038dSYoshinobu Inoue #endif
364df8bae1dSRodney W. Grimes 	default:
36506a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
366df8bae1dSRodney W. Grimes 		m_freem(m);
367df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
368df8bae1dSRodney W. Grimes 	}
3693751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3703751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
3713161f583SAndre Oppermann 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
372df8bae1dSRodney W. Grimes 	return (0);
373df8bae1dSRodney W. Grimes }
374df8bae1dSRodney W. Grimes 
375df8bae1dSRodney W. Grimes /*
376df8bae1dSRodney W. Grimes  * Process an ioctl request.
377df8bae1dSRodney W. Grimes  */
378ddc0ed58SGleb Smirnoff static int
37908304c16SRobert Watson loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
380df8bae1dSRodney W. Grimes {
38108304c16SRobert Watson 	struct ifreq *ifr = (struct ifreq *)data;
3823cb73e3dSRobert Watson 	int error = 0, mask;
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	switch (cmd) {
385df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
38613f4c340SRobert Watson 		ifp->if_flags |= IFF_UP;
38713f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
38844bcc068SAndrey V. Elsukov 		if_link_state_change(ifp, LINK_STATE_UP);
389df8bae1dSRodney W. Grimes 		/*
390df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
391df8bae1dSRodney W. Grimes 		 */
392df8bae1dSRodney W. Grimes 		break;
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
395df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
396155d72c4SPedro F. Giffuni 		if (ifr == NULL) {
397df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
398df8bae1dSRodney W. Grimes 			break;
399df8bae1dSRodney W. Grimes 		}
400df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
401df8bae1dSRodney W. Grimes #ifdef INET
402df8bae1dSRodney W. Grimes 		case AF_INET:
403df8bae1dSRodney W. Grimes 			break;
404df8bae1dSRodney W. Grimes #endif
40582cd038dSYoshinobu Inoue #ifdef INET6
40682cd038dSYoshinobu Inoue 		case AF_INET6:
40782cd038dSYoshinobu Inoue 			break;
40882cd038dSYoshinobu Inoue #endif
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes 		default:
411df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
412df8bae1dSRodney W. Grimes 			break;
413df8bae1dSRodney W. Grimes 		}
414df8bae1dSRodney W. Grimes 		break;
415df8bae1dSRodney W. Grimes 
41690fd8c38SDavid Greenman 	case SIOCSIFMTU:
41790fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
41890fd8c38SDavid Greenman 		break;
41990fd8c38SDavid Greenman 
420ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
4212e4531a1SAndrey V. Elsukov 		if_link_state_change(ifp, (ifp->if_flags & IFF_UP) ?
4222e4531a1SAndrey V. Elsukov 		    LINK_STATE_UP: LINK_STATE_DOWN);
423ce42f1fbSPoul-Henning Kamp 		break;
424ce42f1fbSPoul-Henning Kamp 
4253cb73e3dSRobert Watson 	case SIOCSIFCAP:
4263cb73e3dSRobert Watson 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
4273cb73e3dSRobert Watson 		if ((mask & IFCAP_RXCSUM) != 0)
4283cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_RXCSUM;
4293cb73e3dSRobert Watson 		if ((mask & IFCAP_TXCSUM) != 0)
4303cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_TXCSUM;
4315e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
4325e5c0e79SBjoern A. Zeeb #if 0
433356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
4345e5c0e79SBjoern A. Zeeb #else
4355e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4365e5c0e79SBjoern A. Zeeb 			break;
4375e5c0e79SBjoern A. Zeeb #endif
4385e5c0e79SBjoern A. Zeeb 		}
4395e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
4405e5c0e79SBjoern A. Zeeb #if 0
441356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
4425e5c0e79SBjoern A. Zeeb #else
4435e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4445e5c0e79SBjoern A. Zeeb 			break;
4455e5c0e79SBjoern A. Zeeb #endif
4465e5c0e79SBjoern A. Zeeb 		}
447356ab07eSBjoern A. Zeeb 		ifp->if_hwassist = 0;
4483cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_TXCSUM)
4495bb89930SRobert Watson 			ifp->if_hwassist = LO_CSUM_FEATURES;
4505e5c0e79SBjoern A. Zeeb #if 0
451356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
452356ab07eSBjoern A. Zeeb 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
4535e5c0e79SBjoern A. Zeeb #endif
4543cb73e3dSRobert Watson 		break;
4553cb73e3dSRobert Watson 
456df8bae1dSRodney W. Grimes 	default:
457df8bae1dSRodney W. Grimes 		error = EINVAL;
458df8bae1dSRodney W. Grimes 	}
459df8bae1dSRodney W. Grimes 	return (error);
460df8bae1dSRodney W. Grimes }
461