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