xref: /freebsd/sys/net/if_loop.c (revision 76039bc84fae9915788b54ff28fe0cc4876952d2)
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"
41430df5f4SEivind Eklund 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
462b120974SPeter Wemm #include <sys/module.h>
473d4ce33dSBrooks Davis #include <machine/bus.h>
483d4ce33dSBrooks Davis #include <sys/rman.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
5051a53488SBruce Evans #include <sys/sockio.h>
5190d9802fSPeter Wemm #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <net/if.h>
54*76039bc8SGleb Smirnoff #include <net/if_var.h>
55f889d2efSBrooks Davis #include <net/if_clone.h>
56df8bae1dSRodney W. Grimes #include <net/if_types.h>
57df8bae1dSRodney W. Grimes #include <net/netisr.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59df8bae1dSRodney W. Grimes #include <net/bpf.h>
604b79449eSBjoern A. Zeeb #include <net/vnet.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #ifdef	INET
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #endif
66df8bae1dSRodney W. Grimes 
67cc6a66f2SJulian Elischer #ifdef IPX
68cc6a66f2SJulian Elischer #include <netipx/ipx.h>
69cc6a66f2SJulian Elischer #include <netipx/ipx_if.h>
70cc6a66f2SJulian Elischer #endif
71cc6a66f2SJulian Elischer 
7282cd038dSYoshinobu Inoue #ifdef INET6
7382cd038dSYoshinobu Inoue #ifndef INET
7482cd038dSYoshinobu Inoue #include <netinet/in.h>
7582cd038dSYoshinobu Inoue #endif
7682cd038dSYoshinobu Inoue #include <netinet6/in6_var.h>
77686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
7882cd038dSYoshinobu Inoue #endif
7982cd038dSYoshinobu Inoue 
80655929bfSJulian Elischer #ifdef NETATALK
81655929bfSJulian Elischer #include <netatalk/at.h>
82655929bfSJulian Elischer #include <netatalk/at_var.h>
838cdfefbdSPeter Wemm #endif
84655929bfSJulian Elischer 
853dc85f8dSRobert Watson #include <security/mac/mac_framework.h>
863dc85f8dSRobert Watson 
87db8889c6SDavid Greenman #ifdef TINY_LOMTU
88df8bae1dSRodney W. Grimes #define	LOMTU	(1024+512)
8982cd038dSYoshinobu Inoue #elif defined(LARGE_LOMTU)
9082cd038dSYoshinobu Inoue #define LOMTU	131072
91db8889c6SDavid Greenman #else
92af78195eSDavid Greenman #define LOMTU	16384
93db8889c6SDavid Greenman #endif
94df8bae1dSRodney W. Grimes 
955bb89930SRobert Watson #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
96a6cff10fSMichael Tuexen #define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
97356ab07eSBjoern A. Zeeb #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
98356ab07eSBjoern A. Zeeb 				    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,
10547e8d432SGleb Smirnoff 		    const 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 *);
1083d4ce33dSBrooks Davis 
109eddfbb76SRobert Watson VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
1103d4ce33dSBrooks Davis 
11137f17770SMarko Zec #ifdef VIMAGE
11242a58907SGleb Smirnoff static VNET_DEFINE(struct if_clone *, lo_cloner);
1131e77c105SRobert Watson #define	V_lo_cloner		VNET(lo_cloner)
114eddfbb76SRobert Watson #endif
115bfe1aba4SMarko Zec 
11642a58907SGleb Smirnoff static struct if_clone *lo_cloner;
11742a58907SGleb Smirnoff static const char loname[] = "lo";
1183d4ce33dSBrooks Davis 
119bb2bfb4fSBrooks Davis static void
12008304c16SRobert Watson lo_clone_destroy(struct ifnet *ifp)
1213d4ce33dSBrooks Davis {
1223d4ce33dSBrooks Davis 
123bc29160dSMarko Zec #ifndef VIMAGE
124ae5a19beSBrooks Davis 	/* XXX: destroying lo0 will lead to panics. */
125603724d3SBjoern A. Zeeb 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
126bc29160dSMarko Zec #endif
1273d4ce33dSBrooks Davis 
1283d4ce33dSBrooks Davis 	bpfdetach(ifp);
1293d4ce33dSBrooks Davis 	if_detach(ifp);
130fc74a9f9SBrooks Davis 	if_free(ifp);
1313d4ce33dSBrooks Davis }
1323d4ce33dSBrooks Davis 
133bb2bfb4fSBrooks Davis static int
13408304c16SRobert Watson lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
13590d9802fSPeter Wemm {
136fc74a9f9SBrooks Davis 	struct ifnet *ifp;
13790d9802fSPeter Wemm 
1386db9940fSEd Schouten 	ifp = if_alloc(IFT_LOOP);
1396db9940fSEd Schouten 	if (ifp == NULL)
140fc74a9f9SBrooks Davis 		return (ENOSPC);
14190d9802fSPeter Wemm 
14242a58907SGleb Smirnoff 	if_initname(ifp, loname, unit);
143fc74a9f9SBrooks Davis 	ifp->if_mtu = LOMTU;
144fc74a9f9SBrooks Davis 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
145fc74a9f9SBrooks Davis 	ifp->if_ioctl = loioctl;
146fc74a9f9SBrooks Davis 	ifp->if_output = looutput;
147fc74a9f9SBrooks Davis 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
148356ab07eSBjoern A. Zeeb 	ifp->if_capabilities = ifp->if_capenable =
149356ab07eSBjoern A. Zeeb 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
150356ab07eSBjoern A. Zeeb 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
151fc74a9f9SBrooks Davis 	if_attach(ifp);
15201399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
153603724d3SBjoern A. Zeeb 	if (V_loif == NULL)
154603724d3SBjoern A. Zeeb 		V_loif = ifp;
1553b16e7b2SMaxime Henrion 
1563b16e7b2SMaxime Henrion 	return (0);
15790d9802fSPeter Wemm }
15890d9802fSPeter Wemm 
159d0728d71SRobert Watson static void
160d0728d71SRobert Watson vnet_loif_init(const void *unused __unused)
1611ed81b73SMarko Zec {
16237f17770SMarko Zec 
16337f17770SMarko Zec #ifdef VIMAGE
16442a58907SGleb Smirnoff 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
16542a58907SGleb Smirnoff 	    1);
166d0728d71SRobert Watson 	V_lo_cloner = lo_cloner;
16737f17770SMarko Zec #else
16842a58907SGleb Smirnoff 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
16942a58907SGleb Smirnoff 	    1);
17037f17770SMarko Zec #endif
1711ed81b73SMarko Zec }
172d0728d71SRobert Watson VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
173d0728d71SRobert Watson     vnet_loif_init, NULL);
1741ed81b73SMarko Zec 
175bc29160dSMarko Zec #ifdef VIMAGE
176d0728d71SRobert Watson static void
177d0728d71SRobert Watson vnet_loif_uninit(const void *unused __unused)
178bc29160dSMarko Zec {
179bc29160dSMarko Zec 
18042a58907SGleb Smirnoff 	if_clone_detach(V_lo_cloner);
181bc29160dSMarko Zec 	V_loif = NULL;
182bc29160dSMarko Zec }
183d0728d71SRobert Watson VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
184d0728d71SRobert Watson     vnet_loif_uninit, NULL);
185bc29160dSMarko Zec #endif
186bc29160dSMarko 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:
1932b120974SPeter Wemm 		break;
19408304c16SRobert Watson 
1952b120974SPeter Wemm 	case MOD_UNLOAD:
1962b120974SPeter Wemm 		printf("loop module unload - not possible for this module type\n");
19708304c16SRobert Watson 		return (EINVAL);
19808304c16SRobert Watson 
1993e019deaSPoul-Henning Kamp 	default:
20008304c16SRobert Watson 		return (EOPNOTSUPP);
201f5fea3ddSPaul Traina 	}
20208304c16SRobert Watson 	return (0);
2032b120974SPeter Wemm }
2042b120974SPeter Wemm 
2052b120974SPeter Wemm static moduledata_t loop_mod = {
2065702371bSRobert Watson 	"if_lo",
2072b120974SPeter Wemm 	loop_modevent,
2089823d527SKevin Lo 	0
2092b120974SPeter Wemm };
2102b120974SPeter Wemm 
2115702371bSRobert Watson DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
212df8bae1dSRodney W. Grimes 
213cfa1ca9dSYoshinobu Inoue int
21447e8d432SGleb Smirnoff looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
215279aa3d4SKip Macy     struct route *ro)
216df8bae1dSRodney W. Grimes {
21701399f34SDavid Malone 	u_int32_t af;
218279aa3d4SKip Macy 	struct rtentry *rt = NULL;
2193dc85f8dSRobert Watson #ifdef MAC
2203dc85f8dSRobert Watson 	int error;
2213dc85f8dSRobert Watson #endif
22201399f34SDavid Malone 
223fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
224ed7509acSJulian Elischer 
225279aa3d4SKip Macy 	if (ro != NULL)
226279aa3d4SKip Macy 		rt = ro->ro_rt;
2273dc85f8dSRobert Watson #ifdef MAC
2283dc85f8dSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
2293dc85f8dSRobert Watson 	if (error) {
2303dc85f8dSRobert Watson 		m_freem(m);
2313dc85f8dSRobert Watson 		return (error);
2323dc85f8dSRobert Watson 	}
2333dc85f8dSRobert Watson #endif
2343dc85f8dSRobert Watson 
235ed7509acSJulian Elischer 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
236ed7509acSJulian Elischer 		m_freem(m);
237ed7509acSJulian Elischer 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
238ed7509acSJulian Elischer 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
239ed7509acSJulian Elischer 	}
24082cd038dSYoshinobu Inoue 
241ed7509acSJulian Elischer 	ifp->if_opackets++;
242ed7509acSJulian Elischer 	ifp->if_obytes += m->m_pkthdr.len;
24301399f34SDavid Malone 
24401399f34SDavid Malone 	/* BPF writes need to be handled specially. */
24547e8d432SGleb Smirnoff 	if (dst->sa_family == AF_UNSPEC)
24601399f34SDavid Malone 		bcopy(dst->sa_data, &af, sizeof(af));
24747e8d432SGleb Smirnoff 	else
24847e8d432SGleb Smirnoff 		af = dst->sa_family;
24901399f34SDavid Malone 
250201c2527SJulian Elischer #if 1	/* XXX */
25147e8d432SGleb Smirnoff 	switch (af) {
252201c2527SJulian Elischer 	case AF_INET:
2533cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2543cb73e3dSRobert Watson 			m->m_pkthdr.csum_data = 0xffff;
2555bb89930SRobert Watson 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
2563cb73e3dSRobert Watson 		}
2575bb89930SRobert Watson 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
258356ab07eSBjoern A. Zeeb 		break;
259356ab07eSBjoern A. Zeeb 	case AF_INET6:
2605e5c0e79SBjoern A. Zeeb #if 0
2615e5c0e79SBjoern A. Zeeb 		/*
2625e5c0e79SBjoern A. Zeeb 		 * XXX-BZ for now always claim the checksum is good despite
2635e5c0e79SBjoern A. Zeeb 		 * any interface flags.   This is a workaround for 9.1-R and
2645e5c0e79SBjoern A. Zeeb 		 * a proper solution ought to be sought later.
2655e5c0e79SBjoern A. Zeeb 		 */
266356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
267356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_data = 0xffff;
268356ab07eSBjoern A. Zeeb 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
269356ab07eSBjoern A. Zeeb 		}
2705e5c0e79SBjoern A. Zeeb #else
2715e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_data = 0xffff;
2725e5c0e79SBjoern A. Zeeb 		m->m_pkthdr.csum_flags = LO_CSUM_SET;
2735e5c0e79SBjoern A. Zeeb #endif
274356ab07eSBjoern A. Zeeb 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
275356ab07eSBjoern A. Zeeb 		break;
276201c2527SJulian Elischer 	case AF_IPX:
277201c2527SJulian Elischer 	case AF_APPLETALK:
27805b3ccefSJulian Elischer 		break;
279201c2527SJulian Elischer 	default:
28047e8d432SGleb Smirnoff 		printf("looutput: af=%d unexpected\n", af);
281201c2527SJulian Elischer 		m_freem(m);
282201c2527SJulian Elischer 		return (EAFNOSUPPORT);
283201c2527SJulian Elischer 	}
284201c2527SJulian Elischer #endif
28547e8d432SGleb Smirnoff 	return (if_simloop(ifp, m, af, 0));
286ed7509acSJulian Elischer }
287ed7509acSJulian Elischer 
288ed7509acSJulian Elischer /*
289ed7509acSJulian Elischer  * if_simloop()
290ed7509acSJulian Elischer  *
291ed7509acSJulian Elischer  * This function is to support software emulation of hardware loopback,
292ed7509acSJulian Elischer  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
293ed7509acSJulian Elischer  * hear their own broadcasts, we create a copy of the packet that we
294ed7509acSJulian Elischer  * would normally receive via a hardware loopback.
295ed7509acSJulian Elischer  *
296ed7509acSJulian Elischer  * This function expects the packet to include the media header of length hlen.
297ed7509acSJulian Elischer  */
298ed7509acSJulian Elischer int
29908304c16SRobert Watson if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
300ed7509acSJulian Elischer {
301df5e1987SJonathan Lemon 	int isr;
302df8bae1dSRodney W. Grimes 
303fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
3049c855a36SSam Leffler 	m_tag_delete_nonpersistent(m);
305ed7509acSJulian Elischer 	m->m_pkthdr.rcvif = ifp;
30606a429a3SArchie Cobbs 
3073dc85f8dSRobert Watson #ifdef MAC
3083dc85f8dSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
3093dc85f8dSRobert Watson #endif
3103dc85f8dSRobert Watson 
3118343821bSSUZUKI Shinsuke 	/*
3128343821bSSUZUKI Shinsuke 	 * Let BPF see incoming packet in the following manner:
3138343821bSSUZUKI Shinsuke 	 *  - Emulated packet loopback for a simplex interface
3148343821bSSUZUKI Shinsuke 	 *    (net/if_ethersubr.c)
3158343821bSSUZUKI Shinsuke 	 *	-> passes it to ifp's BPF
3168343821bSSUZUKI Shinsuke 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
3178343821bSSUZUKI Shinsuke 	 *	-> not passes it to any BPF
3188343821bSSUZUKI Shinsuke 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
3198343821bSSUZUKI Shinsuke 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
3208343821bSSUZUKI Shinsuke 	 */
3218343821bSSUZUKI Shinsuke 	if (hlen > 0) {
32216d878ccSChristian S.J. Peron 		if (bpf_peers_present(ifp->if_bpf)) {
3238343821bSSUZUKI Shinsuke 			bpf_mtap(ifp->if_bpf, m);
3248343821bSSUZUKI Shinsuke 		}
3258343821bSSUZUKI Shinsuke 	} else {
326603724d3SBjoern A. Zeeb 		if (bpf_peers_present(V_loif->if_bpf)) {
327603724d3SBjoern A. Zeeb 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
3288343821bSSUZUKI Shinsuke 				/* XXX beware sizeof(af) != 4 */
3298343821bSSUZUKI Shinsuke 				u_int32_t af1 = af;
3308343821bSSUZUKI Shinsuke 
331df8bae1dSRodney W. Grimes 				/*
332437ffe18SSam Leffler 				 * We need to prepend the address family.
333df8bae1dSRodney W. Grimes 				 */
334603724d3SBjoern A. Zeeb 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
3358343821bSSUZUKI Shinsuke 			}
3368343821bSSUZUKI Shinsuke 		}
337df8bae1dSRodney W. Grimes 	}
338df8bae1dSRodney W. Grimes 
339ed7509acSJulian Elischer 	/* Strip away media header */
3403a43ad8fSDoug Rabson 	if (hlen > 0) {
341fe81f64fSAndrew Gallatin 		m_adj(m, hlen);
342f6966ecdSOlivier Houchard #ifndef __NO_STRICT_ALIGNMENT
34373dbd3daSJohn Baldwin 		/*
34473dbd3daSJohn Baldwin 		 * Some archs do not like unaligned data, so
34573dbd3daSJohn Baldwin 		 * we move data down in the first mbuf.
34673dbd3daSJohn Baldwin 		 */
347fe81f64fSAndrew Gallatin 		if (mtod(m, vm_offset_t) & 3) {
34860ed92ddSMatt Jacob 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
349fe81f64fSAndrew Gallatin 			bcopy(m->m_data,
350fe81f64fSAndrew Gallatin 			    (char *)(mtod(m, vm_offset_t)
351fe81f64fSAndrew Gallatin 				- (mtod(m, vm_offset_t) & 3)),
352fe81f64fSAndrew Gallatin 			    m->m_len);
353445e045bSAlexander Kabaev 			m->m_data -= (mtod(m,vm_offset_t) & 3);
354fe81f64fSAndrew Gallatin 		}
3553a43ad8fSDoug Rabson #endif
3563a43ad8fSDoug Rabson 	}
357ed7509acSJulian Elischer 
35806a429a3SArchie Cobbs 	/* Deliver to upper layer protocol */
35906a429a3SArchie Cobbs 	switch (af) {
360df8bae1dSRodney W. Grimes #ifdef INET
361df8bae1dSRodney W. Grimes 	case AF_INET:
362df8bae1dSRodney W. Grimes 		isr = NETISR_IP;
363df8bae1dSRodney W. Grimes 		break;
364df8bae1dSRodney W. Grimes #endif
36582cd038dSYoshinobu Inoue #ifdef INET6
36682cd038dSYoshinobu Inoue 	case AF_INET6:
36782cd038dSYoshinobu Inoue 		m->m_flags |= M_LOOP;
36882cd038dSYoshinobu Inoue 		isr = NETISR_IPV6;
36982cd038dSYoshinobu Inoue 		break;
37082cd038dSYoshinobu Inoue #endif
371cc6a66f2SJulian Elischer #ifdef IPX
372cc6a66f2SJulian Elischer 	case AF_IPX:
373cc6a66f2SJulian Elischer 		isr = NETISR_IPX;
374cc6a66f2SJulian Elischer 		break;
375cc6a66f2SJulian Elischer #endif
376655929bfSJulian Elischer #ifdef NETATALK
377655929bfSJulian Elischer 	case AF_APPLETALK:
3781cafed39SJonathan Lemon 		isr = NETISR_ATALK2;
379655929bfSJulian Elischer 		break;
3808cdfefbdSPeter Wemm #endif
381df8bae1dSRodney W. Grimes 	default:
38206a429a3SArchie Cobbs 		printf("if_simloop: can't handle af=%d\n", af);
383df8bae1dSRodney W. Grimes 		m_freem(m);
384df8bae1dSRodney W. Grimes 		return (EAFNOSUPPORT);
385df8bae1dSRodney W. Grimes 	}
386df8bae1dSRodney W. Grimes 	ifp->if_ipackets++;
387df8bae1dSRodney W. Grimes 	ifp->if_ibytes += m->m_pkthdr.len;
3883161f583SAndre Oppermann 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
389df8bae1dSRodney W. Grimes 	return (0);
390df8bae1dSRodney W. Grimes }
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes /* ARGSUSED */
3933bda9f9bSPoul-Henning Kamp static void
39408304c16SRobert Watson lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
395df8bae1dSRodney W. Grimes {
39608304c16SRobert Watson 
397d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
39897d8d152SAndre Oppermann 	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
399df8bae1dSRodney W. Grimes }
400df8bae1dSRodney W. Grimes 
401df8bae1dSRodney W. Grimes /*
402df8bae1dSRodney W. Grimes  * Process an ioctl request.
403df8bae1dSRodney W. Grimes  */
404df8bae1dSRodney W. Grimes /* ARGSUSED */
405cfa1ca9dSYoshinobu Inoue int
40608304c16SRobert Watson loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
407df8bae1dSRodney W. Grimes {
40808304c16SRobert Watson 	struct ifaddr *ifa;
40908304c16SRobert Watson 	struct ifreq *ifr = (struct ifreq *)data;
4103cb73e3dSRobert Watson 	int error = 0, mask;
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes 	switch (cmd) {
413df8bae1dSRodney W. Grimes 	case SIOCSIFADDR:
41413f4c340SRobert Watson 		ifp->if_flags |= IFF_UP;
41513f4c340SRobert Watson 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
416df8bae1dSRodney W. Grimes 		ifa = (struct ifaddr *)data;
417df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = lortrequest;
418df8bae1dSRodney W. Grimes 		/*
419df8bae1dSRodney W. Grimes 		 * Everything else is done at a higher level.
420df8bae1dSRodney W. Grimes 		 */
421df8bae1dSRodney W. Grimes 		break;
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
424df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
425df8bae1dSRodney W. Grimes 		if (ifr == 0) {
426df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;		/* XXX */
427df8bae1dSRodney W. Grimes 			break;
428df8bae1dSRodney W. Grimes 		}
429df8bae1dSRodney W. Grimes 		switch (ifr->ifr_addr.sa_family) {
430df8bae1dSRodney W. Grimes 
431df8bae1dSRodney W. Grimes #ifdef INET
432df8bae1dSRodney W. Grimes 		case AF_INET:
433df8bae1dSRodney W. Grimes 			break;
434df8bae1dSRodney W. Grimes #endif
43582cd038dSYoshinobu Inoue #ifdef INET6
43682cd038dSYoshinobu Inoue 		case AF_INET6:
43782cd038dSYoshinobu Inoue 			break;
43882cd038dSYoshinobu Inoue #endif
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 		default:
441df8bae1dSRodney W. Grimes 			error = EAFNOSUPPORT;
442df8bae1dSRodney W. Grimes 			break;
443df8bae1dSRodney W. Grimes 		}
444df8bae1dSRodney W. Grimes 		break;
445df8bae1dSRodney W. Grimes 
44690fd8c38SDavid Greenman 	case SIOCSIFMTU:
44790fd8c38SDavid Greenman 		ifp->if_mtu = ifr->ifr_mtu;
44890fd8c38SDavid Greenman 		break;
44990fd8c38SDavid Greenman 
450ce42f1fbSPoul-Henning Kamp 	case SIOCSIFFLAGS:
451ce42f1fbSPoul-Henning Kamp 		break;
452ce42f1fbSPoul-Henning Kamp 
4533cb73e3dSRobert Watson 	case SIOCSIFCAP:
4543cb73e3dSRobert Watson 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
4553cb73e3dSRobert Watson 		if ((mask & IFCAP_RXCSUM) != 0)
4563cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_RXCSUM;
4573cb73e3dSRobert Watson 		if ((mask & IFCAP_TXCSUM) != 0)
4583cb73e3dSRobert Watson 			ifp->if_capenable ^= IFCAP_TXCSUM;
4595e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
4605e5c0e79SBjoern A. Zeeb #if 0
461356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
4625e5c0e79SBjoern A. Zeeb #else
4635e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4645e5c0e79SBjoern A. Zeeb 			break;
4655e5c0e79SBjoern A. Zeeb #endif
4665e5c0e79SBjoern A. Zeeb 		}
4675e5c0e79SBjoern A. Zeeb 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
4685e5c0e79SBjoern A. Zeeb #if 0
469356ab07eSBjoern A. Zeeb 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
4705e5c0e79SBjoern A. Zeeb #else
4715e5c0e79SBjoern A. Zeeb 			error = EOPNOTSUPP;
4725e5c0e79SBjoern A. Zeeb 			break;
4735e5c0e79SBjoern A. Zeeb #endif
4745e5c0e79SBjoern A. Zeeb 		}
475356ab07eSBjoern A. Zeeb 		ifp->if_hwassist = 0;
4763cb73e3dSRobert Watson 		if (ifp->if_capenable & IFCAP_TXCSUM)
4775bb89930SRobert Watson 			ifp->if_hwassist = LO_CSUM_FEATURES;
4785e5c0e79SBjoern A. Zeeb #if 0
479356ab07eSBjoern A. Zeeb 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
480356ab07eSBjoern A. Zeeb 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
4815e5c0e79SBjoern A. Zeeb #endif
4823cb73e3dSRobert Watson 		break;
4833cb73e3dSRobert Watson 
484df8bae1dSRodney W. Grimes 	default:
485df8bae1dSRodney W. Grimes 		error = EINVAL;
486df8bae1dSRodney W. Grimes 	}
487df8bae1dSRodney W. Grimes 	return (error);
488df8bae1dSRodney W. Grimes }
489