xref: /freebsd/sys/net/if_loop.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
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  * 3. 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$
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 #include "opt_rss.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/mbuf.h>
47 #include <sys/module.h>
48 #include <machine/bus.h>
49 #include <sys/rman.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_clone.h>
57 #include <net/if_types.h>
58 #include <net/netisr.h>
59 #include <net/route.h>
60 #include <net/bpf.h>
61 #include <net/vnet.h>
62 
63 #ifdef	INET
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #endif
67 
68 #ifdef INET6
69 #ifndef INET
70 #include <netinet/in.h>
71 #endif
72 #include <netinet6/in6_var.h>
73 #include <netinet/ip6.h>
74 #endif
75 
76 #include <security/mac/mac_framework.h>
77 
78 #ifdef TINY_LOMTU
79 #define	LOMTU	(1024+512)
80 #elif defined(LARGE_LOMTU)
81 #define LOMTU	131072
82 #else
83 #define LOMTU	16384
84 #endif
85 
86 #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
87 #define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
88 #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
89 				    CSUM_PSEUDO_HDR | \
90 				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
91 				    CSUM_SCTP_VALID)
92 
93 int		loioctl(struct ifnet *, u_long, caddr_t);
94 int		looutput(struct ifnet *ifp, struct mbuf *m,
95 		    const struct sockaddr *dst, struct route *ro);
96 static int	lo_clone_create(struct if_clone *, int, caddr_t);
97 static void	lo_clone_destroy(struct ifnet *);
98 
99 VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
100 
101 #ifdef VIMAGE
102 VNET_DEFINE_STATIC(struct if_clone *, lo_cloner);
103 #define	V_lo_cloner		VNET(lo_cloner)
104 #endif
105 
106 static struct if_clone *lo_cloner;
107 static const char loname[] = "lo";
108 
109 static void
110 lo_clone_destroy(struct ifnet *ifp)
111 {
112 
113 #ifndef VIMAGE
114 	/* XXX: destroying lo0 will lead to panics. */
115 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
116 #endif
117 
118 	bpfdetach(ifp);
119 	if_detach(ifp);
120 	if_free(ifp);
121 }
122 
123 static int
124 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
125 {
126 	struct ifnet *ifp;
127 
128 	ifp = if_alloc(IFT_LOOP);
129 	if (ifp == NULL)
130 		return (ENOSPC);
131 
132 	if_initname(ifp, loname, unit);
133 	ifp->if_mtu = LOMTU;
134 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
135 	ifp->if_ioctl = loioctl;
136 	ifp->if_output = looutput;
137 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
138 	ifp->if_capabilities = ifp->if_capenable =
139 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | IFCAP_LINKSTATE;
140 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
141 	if_attach(ifp);
142 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
143 	if (V_loif == NULL)
144 		V_loif = ifp;
145 
146 	return (0);
147 }
148 
149 static void
150 vnet_loif_init(const void *unused __unused)
151 {
152 
153 #ifdef VIMAGE
154 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
155 	    1);
156 	V_lo_cloner = lo_cloner;
157 #else
158 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
159 	    1);
160 #endif
161 }
162 VNET_SYSINIT(vnet_loif_init, SI_SUB_PSEUDO, SI_ORDER_ANY,
163     vnet_loif_init, NULL);
164 
165 #ifdef VIMAGE
166 static void
167 vnet_loif_uninit(const void *unused __unused)
168 {
169 
170 	if_clone_detach(V_lo_cloner);
171 	V_loif = NULL;
172 }
173 VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
174     vnet_loif_uninit, NULL);
175 #endif
176 
177 static int
178 loop_modevent(module_t mod, int type, void *data)
179 {
180 
181 	switch (type) {
182 	case MOD_LOAD:
183 		break;
184 
185 	case MOD_UNLOAD:
186 		printf("loop module unload - not possible for this module type\n");
187 		return (EINVAL);
188 
189 	default:
190 		return (EOPNOTSUPP);
191 	}
192 	return (0);
193 }
194 
195 static moduledata_t loop_mod = {
196 	"if_lo",
197 	loop_modevent,
198 	0
199 };
200 
201 DECLARE_MODULE(if_lo, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
202 
203 int
204 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
205     struct route *ro)
206 {
207 	u_int32_t af;
208 #ifdef MAC
209 	int error;
210 #endif
211 
212 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
213 
214 #ifdef MAC
215 	error = mac_ifnet_check_transmit(ifp, m);
216 	if (error) {
217 		m_freem(m);
218 		return (error);
219 	}
220 #endif
221 
222 	if (ro != NULL && ro->ro_flags & (RT_REJECT|RT_BLACKHOLE)) {
223 		m_freem(m);
224 		return (ro->ro_flags & RT_BLACKHOLE ? 0 : EHOSTUNREACH);
225 	}
226 
227 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
228 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len);
229 
230 #ifdef RSS
231 	M_HASHTYPE_CLEAR(m);
232 #endif
233 
234 	/* BPF writes need to be handled specially. */
235 	if (dst->sa_family == AF_UNSPEC || dst->sa_family == pseudo_AF_HDRCMPLT)
236 		bcopy(dst->sa_data, &af, sizeof(af));
237 	else
238 		af = dst->sa_family;
239 
240 #if 1	/* XXX */
241 	switch (af) {
242 	case AF_INET:
243 		if (ifp->if_capenable & IFCAP_RXCSUM) {
244 			m->m_pkthdr.csum_data = 0xffff;
245 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
246 		}
247 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES;
248 		break;
249 	case AF_INET6:
250 #if 0
251 		/*
252 		 * XXX-BZ for now always claim the checksum is good despite
253 		 * any interface flags.   This is a workaround for 9.1-R and
254 		 * a proper solution ought to be sought later.
255 		 */
256 		if (ifp->if_capenable & IFCAP_RXCSUM_IPV6) {
257 			m->m_pkthdr.csum_data = 0xffff;
258 			m->m_pkthdr.csum_flags = LO_CSUM_SET;
259 		}
260 #else
261 		m->m_pkthdr.csum_data = 0xffff;
262 		m->m_pkthdr.csum_flags = LO_CSUM_SET;
263 #endif
264 		m->m_pkthdr.csum_flags &= ~LO_CSUM_FEATURES6;
265 		break;
266 	default:
267 		printf("looutput: af=%d unexpected\n", af);
268 		m_freem(m);
269 		return (EAFNOSUPPORT);
270 	}
271 #endif
272 	return (if_simloop(ifp, m, af, 0));
273 }
274 
275 /*
276  * if_simloop()
277  *
278  * This function is to support software emulation of hardware loopback,
279  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
280  * hear their own broadcasts, we create a copy of the packet that we
281  * would normally receive via a hardware loopback.
282  *
283  * This function expects the packet to include the media header of length hlen.
284  */
285 int
286 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
287 {
288 	int isr;
289 
290 	M_ASSERTPKTHDR(m);
291 	m_tag_delete_nonpersistent(m);
292 	m->m_pkthdr.rcvif = ifp;
293 
294 #ifdef MAC
295 	mac_ifnet_create_mbuf(ifp, m);
296 #endif
297 
298 	/*
299 	 * Let BPF see incoming packet in the following manner:
300 	 *  - Emulated packet loopback for a simplex interface
301 	 *    (net/if_ethersubr.c)
302 	 *	-> passes it to ifp's BPF
303 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
304 	 *	-> not passes it to any BPF
305 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
306 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
307 	 */
308 	if (hlen > 0) {
309 		if (bpf_peers_present(ifp->if_bpf)) {
310 			bpf_mtap(ifp->if_bpf, m);
311 		}
312 	} else {
313 		if (bpf_peers_present(V_loif->if_bpf)) {
314 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
315 				/* XXX beware sizeof(af) != 4 */
316 				u_int32_t af1 = af;
317 
318 				/*
319 				 * We need to prepend the address family.
320 				 */
321 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
322 			}
323 		}
324 	}
325 
326 	/* Strip away media header */
327 	if (hlen > 0) {
328 		m_adj(m, hlen);
329 #ifndef __NO_STRICT_ALIGNMENT
330 		/*
331 		 * Some archs do not like unaligned data, so
332 		 * we move data down in the first mbuf.
333 		 */
334 		if (mtod(m, vm_offset_t) & 3) {
335 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
336 			bcopy(m->m_data,
337 			    (char *)(mtod(m, vm_offset_t)
338 				- (mtod(m, vm_offset_t) & 3)),
339 			    m->m_len);
340 			m->m_data -= (mtod(m,vm_offset_t) & 3);
341 		}
342 #endif
343 	}
344 
345 	/* Deliver to upper layer protocol */
346 	switch (af) {
347 #ifdef INET
348 	case AF_INET:
349 		isr = NETISR_IP;
350 		break;
351 #endif
352 #ifdef INET6
353 	case AF_INET6:
354 		m->m_flags |= M_LOOP;
355 		isr = NETISR_IPV6;
356 		break;
357 #endif
358 	default:
359 		printf("if_simloop: can't handle af=%d\n", af);
360 		m_freem(m);
361 		return (EAFNOSUPPORT);
362 	}
363 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
364 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
365 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
366 	return (0);
367 }
368 
369 /*
370  * Process an ioctl request.
371  */
372 /* ARGSUSED */
373 int
374 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
375 {
376 	struct ifreq *ifr = (struct ifreq *)data;
377 	int error = 0, mask;
378 
379 	switch (cmd) {
380 	case SIOCSIFADDR:
381 		ifp->if_flags |= IFF_UP;
382 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
383 		if_link_state_change(ifp, LINK_STATE_UP);
384 		/*
385 		 * Everything else is done at a higher level.
386 		 */
387 		break;
388 
389 	case SIOCADDMULTI:
390 	case SIOCDELMULTI:
391 		if (ifr == NULL) {
392 			error = EAFNOSUPPORT;		/* XXX */
393 			break;
394 		}
395 		switch (ifr->ifr_addr.sa_family) {
396 
397 #ifdef INET
398 		case AF_INET:
399 			break;
400 #endif
401 #ifdef INET6
402 		case AF_INET6:
403 			break;
404 #endif
405 
406 		default:
407 			error = EAFNOSUPPORT;
408 			break;
409 		}
410 		break;
411 
412 	case SIOCSIFMTU:
413 		ifp->if_mtu = ifr->ifr_mtu;
414 		break;
415 
416 	case SIOCSIFFLAGS:
417 		if_link_state_change(ifp, (ifp->if_flags & IFF_UP) ?
418 		    LINK_STATE_UP: LINK_STATE_DOWN);
419 		break;
420 
421 	case SIOCSIFCAP:
422 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
423 		if ((mask & IFCAP_RXCSUM) != 0)
424 			ifp->if_capenable ^= IFCAP_RXCSUM;
425 		if ((mask & IFCAP_TXCSUM) != 0)
426 			ifp->if_capenable ^= IFCAP_TXCSUM;
427 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
428 #if 0
429 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
430 #else
431 			error = EOPNOTSUPP;
432 			break;
433 #endif
434 		}
435 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
436 #if 0
437 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
438 #else
439 			error = EOPNOTSUPP;
440 			break;
441 #endif
442 		}
443 		ifp->if_hwassist = 0;
444 		if (ifp->if_capenable & IFCAP_TXCSUM)
445 			ifp->if_hwassist = LO_CSUM_FEATURES;
446 #if 0
447 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
448 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
449 #endif
450 		break;
451 
452 	default:
453 		error = EINVAL;
454 	}
455 	return (error);
456 }
457