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