xref: /freebsd/sys/net/if_loop.c (revision 2c284d9395713ea0217230e077e70d2df8a9900c)
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_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.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 #ifdef NETATALK
75 #include <netatalk/at.h>
76 #include <netatalk/at_var.h>
77 #endif
78 
79 #include <security/mac/mac_framework.h>
80 
81 #ifdef TINY_LOMTU
82 #define	LOMTU	(1024+512)
83 #elif defined(LARGE_LOMTU)
84 #define LOMTU	131072
85 #else
86 #define LOMTU	16384
87 #endif
88 
89 #define	LO_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP)
90 #define	LO_CSUM_FEATURES6	(CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6)
91 #define	LO_CSUM_SET		(CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \
92 				    CSUM_PSEUDO_HDR | \
93 				    CSUM_IP_CHECKED | CSUM_IP_VALID | \
94 				    CSUM_SCTP_VALID)
95 
96 int		loioctl(struct ifnet *, u_long, caddr_t);
97 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
98 int		looutput(struct ifnet *ifp, struct mbuf *m,
99 		    const struct sockaddr *dst, struct route *ro);
100 static int	lo_clone_create(struct if_clone *, int, caddr_t);
101 static void	lo_clone_destroy(struct ifnet *);
102 
103 VNET_DEFINE(struct ifnet *, loif);	/* Used externally */
104 
105 #ifdef VIMAGE
106 static VNET_DEFINE(struct if_clone *, lo_cloner);
107 #define	V_lo_cloner		VNET(lo_cloner)
108 #endif
109 
110 static struct if_clone *lo_cloner;
111 static const char loname[] = "lo";
112 
113 static void
114 lo_clone_destroy(struct ifnet *ifp)
115 {
116 
117 #ifndef VIMAGE
118 	/* XXX: destroying lo0 will lead to panics. */
119 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
120 #endif
121 
122 	bpfdetach(ifp);
123 	if_detach(ifp);
124 	if_free(ifp);
125 }
126 
127 static int
128 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
129 {
130 	struct ifnet *ifp;
131 
132 	ifp = if_alloc(IFT_LOOP);
133 	if (ifp == NULL)
134 		return (ENOSPC);
135 
136 	if_initname(ifp, loname, unit);
137 	ifp->if_mtu = LOMTU;
138 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
139 	ifp->if_ioctl = loioctl;
140 	ifp->if_output = looutput;
141 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
142 	ifp->if_capabilities = ifp->if_capenable =
143 	    IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
144 	ifp->if_hwassist = LO_CSUM_FEATURES | LO_CSUM_FEATURES6;
145 	if_attach(ifp);
146 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
147 	if (V_loif == NULL)
148 		V_loif = ifp;
149 
150 	return (0);
151 }
152 
153 static void
154 vnet_loif_init(const void *unused __unused)
155 {
156 
157 #ifdef VIMAGE
158 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
159 	    1);
160 	V_lo_cloner = lo_cloner;
161 #else
162 	lo_cloner = if_clone_simple(loname, lo_clone_create, lo_clone_destroy,
163 	    1);
164 #endif
165 }
166 VNET_SYSINIT(vnet_loif_init, SI_SUB_PROTO_IFATTACHDOMAIN, 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 	if_clone_detach(V_lo_cloner);
175 	V_loif = NULL;
176 }
177 VNET_SYSUNINIT(vnet_loif_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
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 int
208 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
209     struct route *ro)
210 {
211 	u_int32_t af;
212 	struct rtentry *rt = NULL;
213 #ifdef MAC
214 	int error;
215 #endif
216 
217 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
218 
219 	if (ro != NULL)
220 		rt = ro->ro_rt;
221 #ifdef MAC
222 	error = mac_ifnet_check_transmit(ifp, m);
223 	if (error) {
224 		m_freem(m);
225 		return (error);
226 	}
227 #endif
228 
229 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
230 		m_freem(m);
231 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
232 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
233 	}
234 
235 	ifp->if_opackets++;
236 	ifp->if_obytes += m->m_pkthdr.len;
237 
238 	/* BPF writes need to be handled specially. */
239 	if (dst->sa_family == AF_UNSPEC)
240 		bcopy(dst->sa_data, &af, sizeof(af));
241 	else
242 		af = dst->sa_family;
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 	case AF_APPLETALK:
271 		break;
272 	default:
273 		printf("looutput: af=%d unexpected\n", af);
274 		m_freem(m);
275 		return (EAFNOSUPPORT);
276 	}
277 #endif
278 	return (if_simloop(ifp, m, af, 0));
279 }
280 
281 /*
282  * if_simloop()
283  *
284  * This function is to support software emulation of hardware loopback,
285  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
286  * hear their own broadcasts, we create a copy of the packet that we
287  * would normally receive via a hardware loopback.
288  *
289  * This function expects the packet to include the media header of length hlen.
290  */
291 int
292 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
293 {
294 	int isr;
295 
296 	M_ASSERTPKTHDR(m);
297 	m_tag_delete_nonpersistent(m);
298 	m->m_pkthdr.rcvif = ifp;
299 
300 #ifdef MAC
301 	mac_ifnet_create_mbuf(ifp, m);
302 #endif
303 
304 	/*
305 	 * Let BPF see incoming packet in the following manner:
306 	 *  - Emulated packet loopback for a simplex interface
307 	 *    (net/if_ethersubr.c)
308 	 *	-> passes it to ifp's BPF
309 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
310 	 *	-> not passes it to any BPF
311 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
312 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
313 	 */
314 	if (hlen > 0) {
315 		if (bpf_peers_present(ifp->if_bpf)) {
316 			bpf_mtap(ifp->if_bpf, m);
317 		}
318 	} else {
319 		if (bpf_peers_present(V_loif->if_bpf)) {
320 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
321 				/* XXX beware sizeof(af) != 4 */
322 				u_int32_t af1 = af;
323 
324 				/*
325 				 * We need to prepend the address family.
326 				 */
327 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
328 			}
329 		}
330 	}
331 
332 	/* Strip away media header */
333 	if (hlen > 0) {
334 		m_adj(m, hlen);
335 #ifndef __NO_STRICT_ALIGNMENT
336 		/*
337 		 * Some archs do not like unaligned data, so
338 		 * we move data down in the first mbuf.
339 		 */
340 		if (mtod(m, vm_offset_t) & 3) {
341 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
342 			bcopy(m->m_data,
343 			    (char *)(mtod(m, vm_offset_t)
344 				- (mtod(m, vm_offset_t) & 3)),
345 			    m->m_len);
346 			m->m_data -= (mtod(m,vm_offset_t) & 3);
347 		}
348 #endif
349 	}
350 
351 	/* Deliver to upper layer protocol */
352 	switch (af) {
353 #ifdef INET
354 	case AF_INET:
355 		isr = NETISR_IP;
356 		break;
357 #endif
358 #ifdef INET6
359 	case AF_INET6:
360 		m->m_flags |= M_LOOP;
361 		isr = NETISR_IPV6;
362 		break;
363 #endif
364 #ifdef NETATALK
365 	case AF_APPLETALK:
366 		isr = NETISR_ATALK2;
367 		break;
368 #endif
369 	default:
370 		printf("if_simloop: can't handle af=%d\n", af);
371 		m_freem(m);
372 		return (EAFNOSUPPORT);
373 	}
374 	ifp->if_ipackets++;
375 	ifp->if_ibytes += m->m_pkthdr.len;
376 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
377 	return (0);
378 }
379 
380 /* ARGSUSED */
381 static void
382 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
383 {
384 
385 	RT_LOCK_ASSERT(rt);
386 	rt->rt_mtu = rt->rt_ifp->if_mtu;
387 }
388 
389 /*
390  * Process an ioctl request.
391  */
392 /* ARGSUSED */
393 int
394 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
395 {
396 	struct ifaddr *ifa;
397 	struct ifreq *ifr = (struct ifreq *)data;
398 	int error = 0, mask;
399 
400 	switch (cmd) {
401 	case SIOCSIFADDR:
402 		ifp->if_flags |= IFF_UP;
403 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
404 		ifa = (struct ifaddr *)data;
405 		ifa->ifa_rtrequest = lortrequest;
406 		/*
407 		 * Everything else is done at a higher level.
408 		 */
409 		break;
410 
411 	case SIOCADDMULTI:
412 	case SIOCDELMULTI:
413 		if (ifr == 0) {
414 			error = EAFNOSUPPORT;		/* XXX */
415 			break;
416 		}
417 		switch (ifr->ifr_addr.sa_family) {
418 
419 #ifdef INET
420 		case AF_INET:
421 			break;
422 #endif
423 #ifdef INET6
424 		case AF_INET6:
425 			break;
426 #endif
427 
428 		default:
429 			error = EAFNOSUPPORT;
430 			break;
431 		}
432 		break;
433 
434 	case SIOCSIFMTU:
435 		ifp->if_mtu = ifr->ifr_mtu;
436 		break;
437 
438 	case SIOCSIFFLAGS:
439 		break;
440 
441 	case SIOCSIFCAP:
442 		mask = ifp->if_capenable ^ ifr->ifr_reqcap;
443 		if ((mask & IFCAP_RXCSUM) != 0)
444 			ifp->if_capenable ^= IFCAP_RXCSUM;
445 		if ((mask & IFCAP_TXCSUM) != 0)
446 			ifp->if_capenable ^= IFCAP_TXCSUM;
447 		if ((mask & IFCAP_RXCSUM_IPV6) != 0) {
448 #if 0
449 			ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
450 #else
451 			error = EOPNOTSUPP;
452 			break;
453 #endif
454 		}
455 		if ((mask & IFCAP_TXCSUM_IPV6) != 0) {
456 #if 0
457 			ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
458 #else
459 			error = EOPNOTSUPP;
460 			break;
461 #endif
462 		}
463 		ifp->if_hwassist = 0;
464 		if (ifp->if_capenable & IFCAP_TXCSUM)
465 			ifp->if_hwassist = LO_CSUM_FEATURES;
466 #if 0
467 		if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
468 			ifp->if_hwassist |= LO_CSUM_FEATURES6;
469 #endif
470 		break;
471 
472 	default:
473 		error = EINVAL;
474 	}
475 	return (error);
476 }
477