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