if_loop.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
32 * $FreeBSD: stable/11/sys/net/if_loop.c 330897 2018-03-14 03:19:51Z eadler $
33 */
34
35/*
36 * Loopback interface driver for protocol testing and timing.
37 */
38
39#include "opt_inet.h"
40#include "opt_inet6.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/mbuf.h>
46#include <sys/module.h>
47#include <machine/bus.h>
48#include <sys/rman.h>
49#include <sys/socket.h>
50#include <sys/sockio.h>
51#include <sys/sysctl.h>
52
53#include <net/if.h>
54#include <net/if_var.h>
55#include <net/if_clone.h>
56#include <net/if_types.h>
57#include <net/netisr.h>
58#include <net/route.h>
59#include <net/bpf.h>
60#include <net/vnet.h>
61
62#ifdef	INET
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#endif
66
67#ifdef INET6
68#ifndef INET
69#include <netinet/in.h>
70#endif
71#include <netinet6/in6_var.h>
72#include <netinet/ip6.h>
73#endif
74
75#include <security/mac/mac_framework.h>
76
77#ifdef TINY_LOMTU
78#define	LOMTU	(1024+512)
79#elif defined(LARGE_LOMTU)
80#define LOMTU	131072
81#else
82#define LOMTU	16384
83#endif
84
85#define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
86#define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
87#define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
88				    CSUM_PSEUDO_HDR | \
89				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
90				    CSUM_SCTP_VALID)
91
92int		loioctl(struct ifnet *, u_long, caddr_t);
93int		looutput(struct ifnet *ifp, struct mbuf *m,
94		    const struct sockaddr *dst, struct route *ro);
95static int	lo_clone_create(struct if_clone *, int, caddr_t);
96static void	lo_clone_destroy(struct ifnet *);
97
98VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
99
100#ifdef VIMAGE
101static VNET_DEFINE(struct if_clone *, lo_cloner);
102#define	V_lo_cloner		VNET(lo_cloner)
103#endif
104
105static struct if_clone *lo_cloner;
106static const char loname[] = "lo";
107
108static void
109lo_clone_destroy(struct ifnet *ifp)
110{
111
112#ifndef VIMAGE
113	/* XXX: destroying lo0 will lead to panics. */
114	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
115#endif
116
117	bpfdetach(ifp);
118	if_detach(ifp);
119	if_free(ifp);
120}
121
122static int
123lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
124{
125	struct ifnet *ifp;
126
127	ifp = if_alloc(IFT_LOOP);
128	if (ifp == NULL)
129		return (ENOSPC);
130
131	if_initname(ifp, loname, unit);
132	ifp->if_mtu = LOMTU;
133	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
134	ifp->if_ioctl = loioctl;
135	ifp->if_output = looutput;
136	ifp->if_snd.ifq_maxlen = ifqmaxlen;
137	ifp->if_capabilities = ifp->if_capenable =
138	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
139	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
140	if_attach(ifp);
141	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
142	if (V_loif == NULL)
143		V_loif = ifp;
144
145	return (0);
146}
147
148static void
149vnet_loif_init(const void *unused __unused)
150{
151
152#ifdef VIMAGE
153	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
154	    1);
155	V_lo_cloner = lo_cloner;
156#else
157	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
158	    1);
159#endif
160}
161VNET_SYSINIT(vnet_loif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
162    vnet_loif_init, NULL);
163
164#ifdef VIMAGE
165static void
166vnet_loif_uninit(const void *unused __unused)
167{
168
169	if_clone_detach(V_lo_cloner);
170	V_loif = NULL;
171}
172VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
173    vnet_loif_uninit, NULL);
174#endif
175
176static int
177loop_modevent(module_t mod, int type, void *data)
178{
179
180	switch (type) {
181	case MOD_LOAD:
182		break;
183
184	case MOD_UNLOAD:
185		printf("loop module unload - not possible for this module type\n");
186		return (EINVAL);
187
188	default:
189		return (EOPNOTSUPP);
190	}
191	return (0);
192}
193
194static moduledata_t loop_mod = {
195	"if_lo",
196	loop_modevent,
197	0
198};
199
200DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
201
202int
203looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
204    struct route *ro)
205{
206	u_int32_t af;
207#ifdef MAC
208	int error;
209#endif
210
211	M_ASSERTPKTHDR(m); /* check if we have the packet header */
212
213#ifdef MAC
214	error = mac_ifnet_check_transmit(ifp, m);
215	if (error) {
216		m_freem(m);
217		return (error);
218	}
219#endif
220
221	if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
222		m_freem(m);
223		return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
224	}
225
226	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
227	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
228
229	/* BPF writes need to be handled specially. */
230	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
231		bcopy(dst->sa_data, &af, sizeof(af));
232	else
233		af = dst->sa_family;
234
235#if 1	/* XXX */
236	switch (af) {
237	case AF_INET:
238		if (ifp->if_capenable & IFCAP_RXCSUM) {
239			m->m_pkthdr.csum_data = 0xffff;
240			m->m_pkthdr.csum_flags = LO_CSUM_SET;
241		}
242		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
243		break;
244	case AF_INET6:
245#if 0
246		/*
247		 * XXX-BZ for now always claim the checksum is good despite
248		 * any interface flags.   This is a workaround for 9.1-R and
249		 * a proper solution ought to be sought later.
250		 */
251		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
252			m->m_pkthdr.csum_data = 0xffff;
253			m->m_pkthdr.csum_flags = LO_CSUM_SET;
254		}
255#else
256		m->m_pkthdr.csum_data = 0xffff;
257		m->m_pkthdr.csum_flags = LO_CSUM_SET;
258#endif
259		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
260		break;
261	default:
262		printf("looutput: af=%d unexpected\n", af);
263		m_freem(m);
264		return (EAFNOSUPPORT);
265	}
266#endif
267	return (if_simloop(ifp, m, af, 0));
268}
269
270/*
271 * if_simloop()
272 *
273 * This function is to support software emulation of hardware loopback,
274 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
275 * hear their own broadcasts, we create a copy of the packet that we
276 * would normally receive via a hardware loopback.
277 *
278 * This function expects the packet to include the media header of length hlen.
279 */
280int
281if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
282{
283	int isr;
284
285	M_ASSERTPKTHDR(m);
286	m_tag_delete_nonpersistent(m);
287	m->m_pkthdr.rcvif = ifp;
288
289#ifdef MAC
290	mac_ifnet_create_mbuf(ifp, m);
291#endif
292
293	/*
294	 * Let BPF see incoming packet in the following manner:
295	 *  - Emulated packet loopback for a simplex interface
296	 *    (net/if_ethersubr.c)
297	 *	-> passes it to ifp's BPF
298	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
299	 *	-> not passes it to any BPF
300	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
301	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
302	 */
303	if (hlen > 0) {
304		if (bpf_peers_present(ifp->if_bpf)) {
305			bpf_mtap(ifp->if_bpf, m);
306		}
307	} else {
308		if (bpf_peers_present(V_loif->if_bpf)) {
309			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
310				/* XXX beware sizeof(af) != 4 */
311				u_int32_t af1 = af;
312
313				/*
314				 * We need to prepend the address family.
315				 */
316				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
317			}
318		}
319	}
320
321	/* Strip away media header */
322	if (hlen > 0) {
323		m_adj(m, hlen);
324#ifndef __NO_STRICT_ALIGNMENT
325		/*
326		 * Some archs do not like unaligned data, so
327		 * we move data down in the first mbuf.
328		 */
329		if (mtod(m, vm_offset_t) & 3) {
330			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
331			bcopy(m->m_data,
332			    (char *)(mtod(m, vm_offset_t)
333				- (mtod(m, vm_offset_t) & 3)),
334			    m->m_len);
335			m->m_data -= (mtod(m,vm_offset_t) & 3);
336		}
337#endif
338	}
339
340	/* Deliver to upper layer protocol */
341	switch (af) {
342#ifdef INET
343	case AF_INET:
344		isr = NETISR_IP;
345		break;
346#endif
347#ifdef INET6
348	case AF_INET6:
349		m->m_flags |= M_LOOP;
350		isr = NETISR_IPV6;
351		break;
352#endif
353	default:
354		printf("if_simloop: can't handle af=%d\n", af);
355		m_freem(m);
356		return (EAFNOSUPPORT);
357	}
358	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
359	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
360	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
361	return (0);
362}
363
364/*
365 * Process an ioctl request.
366 */
367/* ARGSUSED */
368int
369loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
370{
371	struct ifreq *ifr = (struct ifreq *)data;
372	int error = 0, mask;
373
374	switch (cmd) {
375	case SIOCSIFADDR:
376		ifp->if_flags |= IFF_UP;
377		ifp->if_drv_flags |= IFF_DRV_RUNNING;
378		/*
379		 * Everything else is done at a higher level.
380		 */
381		break;
382
383	case SIOCADDMULTI:
384	case SIOCDELMULTI:
385		if (ifr == NULL) {
386			error = EAFNOSUPPORT;		/* XXX */
387			break;
388		}
389		switch (ifr->ifr_addr.sa_family) {
390
391#ifdef INET
392		case AF_INET:
393			break;
394#endif
395#ifdef INET6
396		case AF_INET6:
397			break;
398#endif
399
400		default:
401			error = EAFNOSUPPORT;
402			break;
403		}
404		break;
405
406	case SIOCSIFMTU:
407		ifp->if_mtu = ifr->ifr_mtu;
408		break;
409
410	case SIOCSIFFLAGS:
411		break;
412
413	case SIOCSIFCAP:
414		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
415		if ((mask & IFCAP_RXCSUM) != 0)
416			ifp->if_capenable ^= IFCAP_RXCSUM;
417		if ((mask & IFCAP_TXCSUM) != 0)
418			ifp->if_capenable ^= IFCAP_TXCSUM;
419		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
420#if 0
421			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
422#else
423			error = EOPNOTSUPP;
424			break;
425#endif
426		}
427		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
428#if 0
429			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
430#else
431			error = EOPNOTSUPP;
432			break;
433#endif
434		}
435		ifp->if_hwassist = 0;
436		if (ifp->if_capenable & IFCAP_TXCSUM)
437			ifp->if_hwassist = LO_CSUM_FEATURES;
438#if 0
439		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
440			ifp->if_hwassist |= LO_CSUM_FEATURES6;
441#endif
442		break;
443
444	default:
445		error = EINVAL;
446	}
447	return (error);
448}
449