xref: /freebsd/sys/net/if_loop.c (revision aa3860851b9f6a6002d135b1cac7736e0995eedc)
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  */
31df8bae1dSRodney W. Grimes 
32df8bae1dSRodney W. Grimes /*
33df8bae1dSRodney W. Grimes  * Loopback interface driver for protocol testing and timing.
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
361d5e9e22SEivind Eklund #include "opt_inet.h"
37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
38eedb4959SAndrew Gallatin #include "opt_rss.h"
39430df5f4SEivind Eklund 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
41df8bae1dSRodney W. Grimes #include <sys/systm.h>
42df8bae1dSRodney W. Grimes #include <sys/kernel.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
442b120974SPeter Wemm #include <sys/module.h>
453d4ce33dSBrooks Davis #include <machine/bus.h>
463d4ce33dSBrooks Davis #include <sys/rman.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
4851a53488SBruce Evans #include <sys/sockio.h>
4990d9802fSPeter Wemm #include <sys/sysctl.h>
50df8bae1dSRodney W. Grimes 
51df8bae1dSRodney W. Grimes #include <net/if.h>
5276039bc8SGleb Smirnoff #include <net/if_var.h>
532c2b37adSJustin Hibbits #include <net/if_private.h>
54f889d2efSBrooks Davis #include <net/if_clone.h>
55df8bae1dSRodney W. Grimes #include <net/if_types.h>
56df8bae1dSRodney W. Grimes #include <net/netisr.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes #include <net/bpf.h>
594b79449eSBjoern A. Zeeb #include <net/vnet.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #ifdef	INET
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #endif
65df8bae1dSRodney W. Grimes 
6682cd038dSYoshinobu Inoue #ifdef INET6
6782cd038dSYoshinobu Inoue #ifndef INET
6882cd038dSYoshinobu Inoue #include <netinet/in.h>
6982cd038dSYoshinobu Inoue #endif
7082cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7282cd038dSYoshinobu Inoue #endif
7382cd038dSYoshinobu Inoue 
743dc85f8dSRobert Watson #include <security/mac/mac_framework.h>
753dc85f8dSRobert Watson 
76db8889c6SDavid Greenman #ifdef TINY_LOMTU
77df8bae1dSRodney W. Grimes #define	LOMTU	(1024+512)
7882cd038dSYoshinobu Inoue #elif defined(LARGE_LOMTU)
7982cd038dSYoshinobu Inoue #define LOMTU	131072
80db8889c6SDavid Greenman #else
81af78195eSDavid Greenman #define LOMTU	16384
82db8889c6SDavid Greenman #endif
83df8bae1dSRodney W. Grimes 
845bb89930SRobert Watson #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
85a6cff10fSMichael Tuexen #define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
86356ab07eSBjoern A. Zeeb #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
87356ab07eSBjoern A. Zeeb 				    CSUM_PSEUDO_HDR | \
885bb89930SRobert Watson 				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
895bb89930SRobert Watson 				    CSUM_SCTP_VALID)
905bb89930SRobert Watson 
91ddc0ed58SGleb Smirnoff static int	loioctl(struct ifnet *, u_long, caddr_t);
92ddc0ed58SGleb Smirnoff static int	looutput(struct ifnet *ifp, struct mbuf *m,
9347e8d432SGleb Smirnoff 		    const struct sockaddr *dst, struct route *ro);
943d4ce33dSBrooks Davis 
95eddfbb76SRobert Watson VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
965f901c92SAndrew Turner VNET_DEFINE_STATIC(struct if_clone *, lo_cloner);
971e77c105SRobert Watson #define	V_lo_cloner		VNET(lo_cloner)
98bfe1aba4SMarko Zec 
9942a58907SGleb Smirnoff static const char loname[] = "lo";
1003d4ce33dSBrooks Davis 
10191ebcbe0SAlexander V. Chernikov static int
lo_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)10291ebcbe0SAlexander V. Chernikov lo_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
1033d4ce33dSBrooks Davis {
10491ebcbe0SAlexander V. Chernikov 	if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
10591ebcbe0SAlexander V. Chernikov 		return (EINVAL);
1063d4ce33dSBrooks Davis 
107bc29160dSMarko Zec #ifndef VIMAGE
108ae5a19beSBrooks Davis 	/* XXX: destroying lo0 will lead to panics. */
109603724d3SBjoern A. Zeeb 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
110bc29160dSMarko Zec #endif
1113d4ce33dSBrooks Davis 
1123d4ce33dSBrooks Davis 	bpfdetach(ifp);
1133d4ce33dSBrooks Davis 	if_detach(ifp);
114fc74a9f9SBrooks Davis 	if_free(ifp);
11591ebcbe0SAlexander V. Chernikov 
11691ebcbe0SAlexander V. Chernikov 	return (0);
1173d4ce33dSBrooks Davis }
1183d4ce33dSBrooks Davis 
119bb2bfb4fSBrooks Davis static int
lo_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)12091ebcbe0SAlexander V. Chernikov lo_clone_create(struct if_clone *ifc, char *name, size_t len,
12191ebcbe0SAlexander V. Chernikov     struct ifc_data *ifd, struct ifnet **ifpp)
12290d9802fSPeter Wemm {
123fc74a9f9SBrooks Davis 	struct ifnet *ifp;
12490d9802fSPeter Wemm 
1256db9940fSEd Schouten 	ifp = if_alloc(IFT_LOOP);
12691ebcbe0SAlexander V. Chernikov 	if_initname(ifp, loname, ifd->unit);
127fc74a9f9SBrooks Davis 	ifp->if_mtu = LOMTU;
128fc74a9f9SBrooks Davis 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
129fc74a9f9SBrooks Davis 	ifp->if_ioctl = loioctl;
130fc74a9f9SBrooks Davis 	ifp->if_output = looutput;
131fc74a9f9SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
132356ab07eSBjoern A. Zeeb 	ifp->if_capabilities = ifp->if_capenable =
1332e4531a1SAndrey V. Elsukov 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | IFCAP_LINKSTATE;
134356ab07eSBjoern A. Zeeb 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
135fc74a9f9SBrooks Davis 	if_attach(ifp);
13601399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
13791ebcbe0SAlexander V. Chernikov 	*ifpp = ifp;
1383b16e7b2SMaxime Henrion 
1393b16e7b2SMaxime Henrion 	return (0);
14090d9802fSPeter Wemm }
14190d9802fSPeter Wemm 
142d0728d71SRobert Watson static void
vnet_loif_init(const void * unused __unused)143d0728d71SRobert Watson vnet_loif_init(const void *unused __unused)
1441ed81b73SMarko Zec {
14591ebcbe0SAlexander V. Chernikov 	struct if_clone_addreq req = {
14691ebcbe0SAlexander V. Chernikov 		.create_f = lo_clone_create,
14791ebcbe0SAlexander V. Chernikov 		.destroy_f = lo_clone_destroy,
14891ebcbe0SAlexander V. Chernikov 		.flags = IFC_F_AUTOUNIT,
14991ebcbe0SAlexander V. Chernikov 	};
150*2cb7605aSZhenlei Huang 	V_lo_cloner = ifc_attach_cloner(loname, &req);
15191ebcbe0SAlexander V. Chernikov 	struct ifc_data ifd = { .unit = 0 };
152*2cb7605aSZhenlei Huang 	ifc_create_ifp(loname, &ifd, &V_loif);
1531ed81b73SMarko Zec }
15489856f7eSBjoern A. Zeeb VNET_SYSINIT(vnet_loif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
155d0728d71SRobert Watson     vnet_loif_init, NULL);
1561ed81b73SMarko Zec 
157bc29160dSMarko Zec #ifdef VIMAGE
158d0728d71SRobert Watson static void
vnet_loif_uninit(const void * unused __unused)159d0728d71SRobert Watson vnet_loif_uninit(const void *unused __unused)
160bc29160dSMarko Zec {
161bc29160dSMarko Zec 
16291ebcbe0SAlexander V. Chernikov 	ifc_detach_cloner(V_lo_cloner);
163bc29160dSMarko Zec 	V_loif = NULL;
164bc29160dSMarko Zec }
16589856f7eSBjoern A. Zeeb VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
166d0728d71SRobert Watson     vnet_loif_uninit, NULL);
167bc29160dSMarko Zec #endif
168bc29160dSMarko Zec 
1692b120974SPeter Wemm static int
loop_modevent(module_t mod,int type,void * data)1702b120974SPeter Wemm loop_modevent(module_t mod, int type, void *data)
171df8bae1dSRodney W. Grimes {
17208304c16SRobert Watson 
1732b120974SPeter Wemm 	switch (type) {
1742b120974SPeter Wemm 	case MOD_LOAD:
1752b120974SPeter Wemm 		break;
17608304c16SRobert Watson 
1772b120974SPeter Wemm 	case MOD_UNLOAD:
1782b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
17908304c16SRobert Watson 		return (EINVAL);
18008304c16SRobert Watson 
1813e019deaSPoul-Henning Kamp 	default:
18208304c16SRobert Watson 		return (EOPNOTSUPP);
183f5fea3ddSPaul Traina 	}
18408304c16SRobert Watson 	return (0);
1852b120974SPeter Wemm }
1862b120974SPeter Wemm 
1872b120974SPeter Wemm static moduledata_t loop_mod = {
1885702371bSRobert Watson 	"if_lo",
1892b120974SPeter Wemm 	loop_modevent,
1909823d527SKevin Lo 	0
1912b120974SPeter Wemm };
1922b120974SPeter Wemm 
1935702371bSRobert Watson DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
194df8bae1dSRodney W. Grimes 
195ddc0ed58SGleb Smirnoff static int
looutput(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)19647e8d432SGleb Smirnoff looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
197279aa3d4SKip Macy     struct route *ro)
198df8bae1dSRodney W. Grimes {
19901399f34SDavid Malone 	u_int32_t af;
2003dc85f8dSRobert Watson #ifdef MAC
2013dc85f8dSRobert Watson 	int error;
2023dc85f8dSRobert Watson #endif
20301399f34SDavid Malone 
204fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
205ed7509acSJulian Elischer 
2063dc85f8dSRobert Watson #ifdef MAC
2073dc85f8dSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
2083dc85f8dSRobert Watson 	if (error) {
2093dc85f8dSRobert Watson 		m_freem(m);
2103dc85f8dSRobert Watson 		return (error);
2113dc85f8dSRobert Watson 	}
2123dc85f8dSRobert Watson #endif
2133dc85f8dSRobert Watson 
21436402a68SAlexander V. Chernikov 	if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
215ed7509acSJulian Elischer 		m_freem(m);
21636402a68SAlexander V. Chernikov 		return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
217ed7509acSJulian Elischer 	}
21882cd038dSYoshinobu Inoue 
2193751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2203751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
22101399f34SDavid Malone 
222eedb4959SAndrew Gallatin #ifdef RSS
223eedb4959SAndrew Gallatin 	M_HASHTYPE_CLEAR(m);
224eedb4959SAndrew Gallatin #endif
225eedb4959SAndrew Gallatin 
22601399f34SDavid Malone 	/* BPF writes need to be handled specially. */
227e9617c30SPatrick Kelsey 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
22801399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
22947e8d432SGleb Smirnoff 	else
23062e1a437SZhenlei Huang 		af = RO_GET_FAMILY(ro, dst);
23101399f34SDavid Malone 
232201c2527SJulian Elischer #if 1	/* XXX */
23347e8d432SGleb Smirnoff 	switch (af) {
234201c2527SJulian Elischer 	case AF_INET:
2353cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2363cb73e3dSRobert Watson 			m->m_pkthdr.csum_data = 0xffff;
2375bb89930SRobert Watson 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
2383cb73e3dSRobert Watson 		}
2395bb89930SRobert Watson 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
240356ab07eSBjoern A. Zeeb 		break;
241356ab07eSBjoern A. Zeeb 	case AF_INET6:
2425e5c0e79SBjoern A. Zeeb #if 0
2435e5c0e79SBjoern A. Zeeb 		/*
2445e5c0e79SBjoern A. Zeeb 		 * XXX-BZ for now always claim the checksum is good despite
2455e5c0e79SBjoern A. Zeeb 		 * any interface flags.   This is a workaround for 9.1-R and
2465e5c0e79SBjoern A. Zeeb 		 * a proper solution ought to be sought later.
2475e5c0e79SBjoern A. Zeeb 		 */
248356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
249356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
250356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
251356ab07eSBjoern A. Zeeb 		}
2525e5c0e79SBjoern A. Zeeb #else
2535e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_data = 0xffff;
2545e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_flags = LO_CSUM_SET;
2555e5c0e79SBjoern A. Zeeb #endif
256356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
257356ab07eSBjoern A. Zeeb 		break;
258201c2527SJulian Elischer 	default:
25947e8d432SGleb Smirnoff 		printf("looutput: af=%d unexpected\n", af);
260201c2527SJulian Elischer 		m_freem(m);
261201c2527SJulian Elischer 		return (EAFNOSUPPORT);
262201c2527SJulian Elischer 	}
263201c2527SJulian Elischer #endif
26447e8d432SGleb Smirnoff 	return (if_simloop(ifp, m, af, 0));
265ed7509acSJulian Elischer }
266ed7509acSJulian Elischer 
267ed7509acSJulian Elischer /*
268ed7509acSJulian Elischer  * if_simloop()
269ed7509acSJulian Elischer  *
270ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
271ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
272ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
273ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
274ed7509acSJulian Elischer  *
275ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
276ed7509acSJulian Elischer  */
277ed7509acSJulian Elischer int
if_simloop(struct ifnet * ifp,struct mbuf * m,int af,int hlen)27808304c16SRobert Watson if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
279ed7509acSJulian Elischer {
280df5e1987SJonathan Lemon 	int isr;
281df8bae1dSRodney W. Grimes 
282fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
2839c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
284ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
28506a429a3SArchie Cobbs 
2863dc85f8dSRobert Watson #ifdef MAC
2873dc85f8dSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
2883dc85f8dSRobert Watson #endif
2893dc85f8dSRobert Watson 
2908343821bSSUZUKI Shinsuke 	/*
2918343821bSSUZUKI Shinsuke 	 * Let BPF see incoming packet in the following manner:
2928343821bSSUZUKI Shinsuke 	 *  - Emulated packet loopback for a simplex interface
2938343821bSSUZUKI Shinsuke 	 *    (net/if_ethersubr.c)
2948343821bSSUZUKI Shinsuke 	 *	-> passes it to ifp's BPF
2958343821bSSUZUKI Shinsuke 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
2968343821bSSUZUKI Shinsuke 	 *	-> not passes it to any BPF
2978343821bSSUZUKI Shinsuke 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
2988343821bSSUZUKI Shinsuke 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
2998343821bSSUZUKI Shinsuke 	 */
3008343821bSSUZUKI Shinsuke 	if (hlen > 0) {
30116d878ccSChristian S.J. Peron 		if (bpf_peers_present(ifp->if_bpf)) {
3028343821bSSUZUKI Shinsuke 			bpf_mtap(ifp->if_bpf, m);
3038343821bSSUZUKI Shinsuke 		}
3048343821bSSUZUKI Shinsuke 	} else {
305603724d3SBjoern A. Zeeb 		if (bpf_peers_present(V_loif->if_bpf)) {
306603724d3SBjoern A. Zeeb 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
3078343821bSSUZUKI Shinsuke 				/* XXX beware sizeof(af) != 4 */
3088343821bSSUZUKI Shinsuke 				u_int32_t af1 = af;
3098343821bSSUZUKI Shinsuke 
310df8bae1dSRodney W. Grimes 				/*
311437ffe18SSam Leffler 				 * We need to prepend the address family.
312df8bae1dSRodney W. Grimes 				 */
313603724d3SBjoern A. Zeeb 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
3148343821bSSUZUKI Shinsuke 			}
3158343821bSSUZUKI Shinsuke 		}
316df8bae1dSRodney W. Grimes 	}
317df8bae1dSRodney W. Grimes 
318ed7509acSJulian Elischer 	/* Strip away media header */
3193a43ad8fSDoug Rabson 	if (hlen > 0) {
320fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
321f6966ecdSOlivier Houchard #ifndef __NO_STRICT_ALIGNMENT
32273dbd3daSJohn Baldwin 		/*
32373dbd3daSJohn Baldwin 		 * Some archs do not like unaligned data, so
32473dbd3daSJohn Baldwin 		 * we move data down in the first mbuf.
32573dbd3daSJohn Baldwin 		 */
326fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
32760ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
328fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
329fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
330fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
331fe81f64fSAndrew Gallatin 			    m->m_len);
332445e045bSAlexander Kabaev 			m->m_data -= (mtod(m,vm_offset_t) & 3);
333fe81f64fSAndrew Gallatin 		}
3343a43ad8fSDoug Rabson #endif
3353a43ad8fSDoug Rabson 	}
336ed7509acSJulian Elischer 
33706a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
33806a429a3SArchie Cobbs 	switch (af) {
339df8bae1dSRodney W. Grimes #ifdef INET
340df8bae1dSRodney W. Grimes 	case AF_INET:
341df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
342df8bae1dSRodney W. Grimes 		break;
343df8bae1dSRodney W. Grimes #endif
34482cd038dSYoshinobu Inoue #ifdef INET6
34582cd038dSYoshinobu Inoue 	case AF_INET6:
34682cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
34782cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
34882cd038dSYoshinobu Inoue 		break;
34982cd038dSYoshinobu Inoue #endif
350df8bae1dSRodney W. Grimes 	default:
35106a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
352df8bae1dSRodney W. Grimes 		m_freem(m);
353df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
354df8bae1dSRodney W. Grimes 	}
3553751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
3563751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
3573161f583SAndre Oppermann 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
358df8bae1dSRodney W. Grimes 	return (0);
359df8bae1dSRodney W. Grimes }
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes /*
362df8bae1dSRodney W. Grimes  * Process an ioctl request.
363df8bae1dSRodney W. Grimes  */
364ddc0ed58SGleb Smirnoff static int
loioctl(struct ifnet * ifp,u_long cmd,caddr_t data)36508304c16SRobert Watson loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
366df8bae1dSRodney W. Grimes {
36708304c16SRobert Watson 	struct ifreq *ifr = (struct ifreq *)data;
3683cb73e3dSRobert Watson 	int error = 0, mask;
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	switch (cmd) {
371df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
37213f4c340SRobert Watson 		ifp->if_flags |= IFF_UP;
37313f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
37444bcc068SAndrey V. Elsukov 		if_link_state_change(ifp, LINK_STATE_UP);
375df8bae1dSRodney W. Grimes 		/*
376df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
377df8bae1dSRodney W. Grimes 		 */
378df8bae1dSRodney W. Grimes 		break;
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
381df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
382155d72c4SPedro F. Giffuni 		if (ifr == NULL) {
383df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
384df8bae1dSRodney W. Grimes 			break;
385df8bae1dSRodney W. Grimes 		}
386df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
387df8bae1dSRodney W. Grimes #ifdef INET
388df8bae1dSRodney W. Grimes 		case AF_INET:
389df8bae1dSRodney W. Grimes 			break;
390df8bae1dSRodney W. Grimes #endif
39182cd038dSYoshinobu Inoue #ifdef INET6
39282cd038dSYoshinobu Inoue 		case AF_INET6:
39382cd038dSYoshinobu Inoue 			break;
39482cd038dSYoshinobu Inoue #endif
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes 		default:
397df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
398df8bae1dSRodney W. Grimes 			break;
399df8bae1dSRodney W. Grimes 		}
400df8bae1dSRodney W. Grimes 		break;
401df8bae1dSRodney W. Grimes 
40290fd8c38SDavid Greenman 	case SIOCSIFMTU:
40390fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
40490fd8c38SDavid Greenman 		break;
40590fd8c38SDavid Greenman 
406ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
4072e4531a1SAndrey V. Elsukov 		if_link_state_change(ifp, (ifp->if_flags & IFF_UP) ?
4082e4531a1SAndrey V. Elsukov 		    LINK_STATE_UP: LINK_STATE_DOWN);
409ce42f1fbSPoul-Henning Kamp 		break;
410ce42f1fbSPoul-Henning Kamp 
4113cb73e3dSRobert Watson 	case SIOCSIFCAP:
4123cb73e3dSRobert Watson 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
4133cb73e3dSRobert Watson 		if ((mask & IFCAP_RXCSUM) != 0)
4143cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_RXCSUM;
4153cb73e3dSRobert Watson 		if ((mask & IFCAP_TXCSUM) != 0)
4163cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_TXCSUM;
4175e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
4185e5c0e79SBjoern A. Zeeb #if 0
419356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
4205e5c0e79SBjoern A. Zeeb #else
4215e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4225e5c0e79SBjoern A. Zeeb 			break;
4235e5c0e79SBjoern A. Zeeb #endif
4245e5c0e79SBjoern A. Zeeb 		}
4255e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
4265e5c0e79SBjoern A. Zeeb #if 0
427356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
4285e5c0e79SBjoern A. Zeeb #else
4295e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4305e5c0e79SBjoern A. Zeeb 			break;
4315e5c0e79SBjoern A. Zeeb #endif
4325e5c0e79SBjoern A. Zeeb 		}
433356ab07eSBjoern A. Zeeb 		ifp->if_hwassist = 0;
4343cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_TXCSUM)
4355bb89930SRobert Watson 			ifp->if_hwassist = LO_CSUM_FEATURES;
4365e5c0e79SBjoern A. Zeeb #if 0
437356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
438356ab07eSBjoern A. Zeeb 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
4395e5c0e79SBjoern A. Zeeb #endif
4403cb73e3dSRobert Watson 		break;
4413cb73e3dSRobert Watson 
442df8bae1dSRodney W. Grimes 	default:
443df8bae1dSRodney W. Grimes 		error = EINVAL;
444df8bae1dSRodney W. Grimes 	}
445df8bae1dSRodney W. Grimes 	return (error);
446df8bae1dSRodney W. Grimes }
447