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