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