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