xref: /freebsd/sys/net/if_loop.c (revision 8655c70597b0e0918c82114b1186df5669b83eb6)
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 #include "opt_ipx.h"
41 #include "opt_route.h"
42 #include "opt_mac.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/module.h>
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/vimage.h>
55 
56 #include <net/if.h>
57 #include <net/if_clone.h>
58 #include <net/if_types.h>
59 #include <net/netisr.h>
60 #include <net/route.h>
61 #include <net/bpf.h>
62 #include <net/vnet.h>
63 
64 #ifdef	INET
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #endif
68 
69 #ifdef IPX
70 #include <netipx/ipx.h>
71 #include <netipx/ipx_if.h>
72 #endif
73 
74 #ifdef INET6
75 #ifndef INET
76 #include <netinet/in.h>
77 #endif
78 #include <netinet6/in6_var.h>
79 #include <netinet/ip6.h>
80 #endif
81 
82 #ifdef NETATALK
83 #include <netatalk/at.h>
84 #include <netatalk/at_var.h>
85 #endif
86 
87 #include <security/mac/mac_framework.h>
88 
89 #ifdef TINY_LOMTU
90 #define	LOMTU	(1024+512)
91 #elif defined(LARGE_LOMTU)
92 #define LOMTU	131072
93 #else
94 #define LOMTU	16384
95 #endif
96 
97 int		loioctl(struct ifnet *, u_long, caddr_t);
98 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
99 int		looutput(struct ifnet *ifp, struct mbuf *m,
100 		    struct sockaddr *dst, struct rtentry *rt);
101 static int	lo_clone_create(struct if_clone *, int, caddr_t);
102 static void	lo_clone_destroy(struct ifnet *);
103 
104 #ifdef VIMAGE_GLOBALS
105 struct ifnet *loif;			/* Used externally */
106 #endif
107 
108 IFC_SIMPLE_DECLARE(lo, 1);
109 
110 static void
111 lo_clone_destroy(struct ifnet *ifp)
112 {
113 #ifdef INVARIANTS
114 	INIT_VNET_NET(ifp->if_vnet);
115 #endif
116 
117 	/* XXX: destroying lo0 will lead to panics. */
118 	KASSERT(V_loif != ifp, ("%s: destroying lo0", __func__));
119 
120 	bpfdetach(ifp);
121 	if_detach(ifp);
122 	if_free(ifp);
123 }
124 
125 static int
126 lo_clone_create(struct if_clone *ifc, int unit, caddr_t params)
127 {
128 	INIT_VNET_NET(curvnet);
129 	struct ifnet *ifp;
130 
131 	ifp = if_alloc(IFT_LOOP);
132 	if (ifp == NULL)
133 		return (ENOSPC);
134 
135 	if_initname(ifp, ifc->ifc_name, unit);
136 	ifp->if_mtu = LOMTU;
137 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
138 	ifp->if_ioctl = loioctl;
139 	ifp->if_output = looutput;
140 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
141 	if_attach(ifp);
142 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
143 	if (V_loif == NULL)
144 		V_loif = ifp;
145 
146 	return (0);
147 }
148 
149 static int
150 loop_modevent(module_t mod, int type, void *data)
151 {
152 	INIT_VNET_NET(curvnet);
153 
154 	switch (type) {
155 	case MOD_LOAD:
156 		V_loif = NULL;
157 		if_clone_attach(&lo_cloner);
158 		break;
159 
160 	case MOD_UNLOAD:
161 		printf("loop module unload - not possible for this module type\n");
162 		return (EINVAL);
163 
164 	default:
165 		return (EOPNOTSUPP);
166 	}
167 	return (0);
168 }
169 
170 static moduledata_t loop_mod = {
171 	"loop",
172 	loop_modevent,
173 	0
174 };
175 
176 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
177 
178 int
179 looutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
180     struct rtentry *rt)
181 {
182 	u_int32_t af;
183 #ifdef MAC
184 	int error;
185 #endif
186 
187 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
188 
189 #ifdef MAC
190 	error = mac_ifnet_check_transmit(ifp, m);
191 	if (error) {
192 		m_freem(m);
193 		return (error);
194 	}
195 #endif
196 
197 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
198 		m_freem(m);
199 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
200 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
201 	}
202 
203 	ifp->if_opackets++;
204 	ifp->if_obytes += m->m_pkthdr.len;
205 
206 	/* BPF writes need to be handled specially. */
207 	if (dst->sa_family == AF_UNSPEC) {
208 		bcopy(dst->sa_data, &af, sizeof(af));
209 		dst->sa_family = af;
210 	}
211 
212 #if 1	/* XXX */
213 	switch (dst->sa_family) {
214 	case AF_INET:
215 	case AF_INET6:
216 	case AF_IPX:
217 	case AF_APPLETALK:
218 		break;
219 	default:
220 		printf("looutput: af=%d unexpected\n", dst->sa_family);
221 		m_freem(m);
222 		return (EAFNOSUPPORT);
223 	}
224 #endif
225 	return (if_simloop(ifp, m, dst->sa_family, 0));
226 }
227 
228 /*
229  * if_simloop()
230  *
231  * This function is to support software emulation of hardware loopback,
232  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
233  * hear their own broadcasts, we create a copy of the packet that we
234  * would normally receive via a hardware loopback.
235  *
236  * This function expects the packet to include the media header of length hlen.
237  */
238 int
239 if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen)
240 {
241 	INIT_VNET_NET(ifp->if_vnet);
242 	int isr;
243 
244 	M_ASSERTPKTHDR(m);
245 	m_tag_delete_nonpersistent(m);
246 	m->m_pkthdr.rcvif = ifp;
247 
248 #ifdef MAC
249 	mac_ifnet_create_mbuf(ifp, m);
250 #endif
251 
252 	/*
253 	 * Let BPF see incoming packet in the following manner:
254 	 *  - Emulated packet loopback for a simplex interface
255 	 *    (net/if_ethersubr.c)
256 	 *	-> passes it to ifp's BPF
257 	 *  - IPv4/v6 multicast packet loopback (netinet(6)/ip(6)_output.c)
258 	 *	-> not passes it to any BPF
259 	 *  - Normal packet loopback from myself to myself (net/if_loop.c)
260 	 *	-> passes to lo0's BPF (even in case of IPv6, where ifp!=lo0)
261 	 */
262 	if (hlen > 0) {
263 		if (bpf_peers_present(ifp->if_bpf)) {
264 			bpf_mtap(ifp->if_bpf, m);
265 		}
266 	} else {
267 		if (bpf_peers_present(V_loif->if_bpf)) {
268 			if ((m->m_flags & M_MCAST) == 0 || V_loif == ifp) {
269 				/* XXX beware sizeof(af) != 4 */
270 				u_int32_t af1 = af;
271 
272 				/*
273 				 * We need to prepend the address family.
274 				 */
275 				bpf_mtap2(V_loif->if_bpf, &af1, sizeof(af1), m);
276 			}
277 		}
278 	}
279 
280 	/* Strip away media header */
281 	if (hlen > 0) {
282 		m_adj(m, hlen);
283 #ifndef __NO_STRICT_ALIGNMENT
284 		/*
285 		 * Some archs do not like unaligned data, so
286 		 * we move data down in the first mbuf.
287 		 */
288 		if (mtod(m, vm_offset_t) & 3) {
289 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
290 			bcopy(m->m_data,
291 			    (char *)(mtod(m, vm_offset_t)
292 				- (mtod(m, vm_offset_t) & 3)),
293 			    m->m_len);
294 			m->m_data -= (mtod(m,vm_offset_t) & 3);
295 		}
296 #endif
297 	}
298 
299 	/* Deliver to upper layer protocol */
300 	switch (af) {
301 #ifdef INET
302 	case AF_INET:
303 		isr = NETISR_IP;
304 		break;
305 #endif
306 #ifdef INET6
307 	case AF_INET6:
308 		m->m_flags |= M_LOOP;
309 		isr = NETISR_IPV6;
310 		break;
311 #endif
312 #ifdef IPX
313 	case AF_IPX:
314 		isr = NETISR_IPX;
315 		break;
316 #endif
317 #ifdef NETATALK
318 	case AF_APPLETALK:
319 		isr = NETISR_ATALK2;
320 		break;
321 #endif
322 	default:
323 		printf("if_simloop: can't handle af=%d\n", af);
324 		m_freem(m);
325 		return (EAFNOSUPPORT);
326 	}
327 	ifp->if_ipackets++;
328 	ifp->if_ibytes += m->m_pkthdr.len;
329 	netisr_queue(isr, m);	/* mbuf is free'd on failure. */
330 	return (0);
331 }
332 
333 /* ARGSUSED */
334 static void
335 lortrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
336 {
337 
338 	RT_LOCK_ASSERT(rt);
339 	rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
340 }
341 
342 /*
343  * Process an ioctl request.
344  */
345 /* ARGSUSED */
346 int
347 loioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
348 {
349 	struct ifaddr *ifa;
350 	struct ifreq *ifr = (struct ifreq *)data;
351 	int error = 0;
352 
353 	switch (cmd) {
354 	case SIOCSIFADDR:
355 		ifp->if_flags |= IFF_UP;
356 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
357 		ifa = (struct ifaddr *)data;
358 		ifa->ifa_rtrequest = lortrequest;
359 		/*
360 		 * Everything else is done at a higher level.
361 		 */
362 		break;
363 
364 	case SIOCADDMULTI:
365 	case SIOCDELMULTI:
366 		if (ifr == 0) {
367 			error = EAFNOSUPPORT;		/* XXX */
368 			break;
369 		}
370 		switch (ifr->ifr_addr.sa_family) {
371 
372 #ifdef INET
373 		case AF_INET:
374 			break;
375 #endif
376 #ifdef INET6
377 		case AF_INET6:
378 			break;
379 #endif
380 
381 		default:
382 			error = EAFNOSUPPORT;
383 			break;
384 		}
385 		break;
386 
387 	case SIOCSIFMTU:
388 		ifp->if_mtu = ifr->ifr_mtu;
389 		break;
390 
391 	case SIOCSIFFLAGS:
392 		break;
393 
394 	default:
395 		error = EINVAL;
396 	}
397 	return (error);
398 }
399