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