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