xref: /freebsd/sys/net/if_loop.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_loop.c	8.2 (Berkeley) 1/9/95
34  * $FreeBSD$
35  */
36 
37 /*
38  * Loopback interface driver for protocol testing and timing.
39  */
40 
41 #include "opt_atalk.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 #include "opt_ipx.h"
45 #include "opt_mac.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/mac.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/module.h>
54 #include <machine/bus.h>
55 #include <sys/rman.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64 #include <net/bpf.h>
65 #include <net/bpfdesc.h>
66 
67 #ifdef	INET
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #endif
71 
72 #ifdef IPX
73 #include <netipx/ipx.h>
74 #include <netipx/ipx_if.h>
75 #endif
76 
77 #ifdef INET6
78 #ifndef INET
79 #include <netinet/in.h>
80 #endif
81 #include <netinet6/in6_var.h>
82 #include <netinet/ip6.h>
83 #endif
84 
85 #ifdef NETATALK
86 #include <netatalk/at.h>
87 #include <netatalk/at_var.h>
88 #endif
89 
90 #ifdef TINY_LOMTU
91 #define	LOMTU	(1024+512)
92 #elif defined(LARGE_LOMTU)
93 #define LOMTU	131072
94 #else
95 #define LOMTU	16384
96 #endif
97 
98 #define LONAME	"lo"
99 
100 struct lo_softc {
101 	struct	ifnet sc_if;		/* network-visible interface */
102 	LIST_ENTRY(lo_softc) sc_next;
103 };
104 
105 int		loioctl(struct ifnet *, u_long, caddr_t);
106 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
107 int		looutput(struct ifnet *ifp, struct mbuf *m,
108 		    struct sockaddr *dst, struct rtentry *rt);
109 int		lo_clone_create(struct if_clone *, int);
110 void		lo_clone_destroy(struct ifnet *);
111 
112 struct ifnet *loif = NULL;			/* Used externally */
113 
114 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface");
115 
116 static struct mtx lo_mtx;
117 static LIST_HEAD(lo_list, lo_softc) lo_list;
118 
119 struct if_clone lo_cloner = IF_CLONE_INITIALIZER(LONAME,
120     lo_clone_create, lo_clone_destroy, 1, IF_MAXUNIT);
121 
122 void
123 lo_clone_destroy(ifp)
124 	struct ifnet *ifp;
125 {
126 	struct lo_softc *sc;
127 
128 	sc = ifp->if_softc;
129 
130 	/* XXX: destroying lo0 will lead to panics. */
131 	KASSERT(loif != ifp, ("%s: destroying lo0", __func__));
132 
133 	mtx_lock(&lo_mtx);
134 	LIST_REMOVE(sc, sc_next);
135 	mtx_unlock(&lo_mtx);
136 	bpfdetach(ifp);
137 	if_detach(ifp);
138 	free(sc, M_LO);
139 }
140 
141 int
142 lo_clone_create(ifc, unit)
143 	struct if_clone *ifc;
144 	int unit;
145 {
146 	struct lo_softc *sc;
147 
148 	MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO);
149 
150 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
151 	sc->sc_if.if_mtu = LOMTU;
152 	sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST;
153 	sc->sc_if.if_ioctl = loioctl;
154 	sc->sc_if.if_output = looutput;
155 	sc->sc_if.if_type = IFT_LOOP;
156 	sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
157 	sc->sc_if.if_softc = sc;
158 	if_attach(&sc->sc_if);
159 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
160 	mtx_lock(&lo_mtx);
161 	LIST_INSERT_HEAD(&lo_list, sc, sc_next);
162 	mtx_unlock(&lo_mtx);
163 	if (loif == NULL)
164 		loif = &sc->sc_if;
165 
166 	return (0);
167 }
168 
169 static int
170 loop_modevent(module_t mod, int type, void *data)
171 {
172 	switch (type) {
173 	case MOD_LOAD:
174 		mtx_init(&lo_mtx, "lo_mtx", NULL, MTX_DEF);
175 		LIST_INIT(&lo_list);
176 		if_clone_attach(&lo_cloner);
177 		break;
178 	case MOD_UNLOAD:
179 		printf("loop module unload - not possible for this module type\n");
180 		return EINVAL;
181 	}
182 	return 0;
183 }
184 
185 static moduledata_t loop_mod = {
186 	"loop",
187 	loop_modevent,
188 	0
189 };
190 
191 DECLARE_MODULE(loop, loop_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
192 
193 int
194 looutput(ifp, m, dst, rt)
195 	struct ifnet *ifp;
196 	register struct mbuf *m;
197 	struct sockaddr *dst;
198 	register struct rtentry *rt;
199 {
200 	M_ASSERTPKTHDR(m); /* check if we have the packet header */
201 
202 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
203 		m_freem(m);
204 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
205 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
206 	}
207 
208 	ifp->if_opackets++;
209 	ifp->if_obytes += m->m_pkthdr.len;
210 #if 1	/* XXX */
211 	switch (dst->sa_family) {
212 	case AF_INET:
213 	case AF_INET6:
214 	case AF_IPX:
215 	case AF_APPLETALK:
216 		break;
217 	default:
218 		printf("looutput: af=%d unexpected\n", dst->sa_family);
219 		m_freem(m);
220 		return (EAFNOSUPPORT);
221 	}
222 #endif
223 	return(if_simloop(ifp, m, dst->sa_family, 0));
224 }
225 
226 /*
227  * if_simloop()
228  *
229  * This function is to support software emulation of hardware loopback,
230  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
231  * hear their own broadcasts, we create a copy of the packet that we
232  * would normally receive via a hardware loopback.
233  *
234  * This function expects the packet to include the media header of length hlen.
235  */
236 
237 int
238 if_simloop(ifp, m, af, hlen)
239 	struct ifnet *ifp;
240 	struct mbuf *m;
241 	int af;
242 	int hlen;
243 {
244 	int isr;
245 
246 	M_ASSERTPKTHDR(m);
247 	m_tag_delete_nonpersistent(m);
248 	m->m_pkthdr.rcvif = ifp;
249 
250 	/* BPF write needs to be handled specially */
251 	if (af == AF_UNSPEC) {
252 		KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
253 		af = *(mtod(m, int *));
254 		m->m_len -= sizeof(int);
255 		m->m_pkthdr.len -= sizeof(int);
256 		m->m_data += sizeof(int);
257 	}
258 
259 	/* Let BPF see incoming packet */
260 	if (ifp->if_bpf) {
261 		if (ifp->if_bpf->bif_dlt == DLT_NULL) {
262 			u_int32_t af1 = af;	/* XXX beware sizeof(af) != 4 */
263 			/*
264 			 * We need to prepend the address family.
265 			 */
266 			bpf_mtap2(ifp->if_bpf, &af1, sizeof(af1), m);
267 		} else
268 			bpf_mtap(ifp->if_bpf, m);
269 	}
270 
271 	/* Strip away media header */
272 	if (hlen > 0) {
273 		m_adj(m, hlen);
274 #if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__)
275 		/* The alpha doesn't like unaligned data.
276 		 * We move data down in the first mbuf */
277 		if (mtod(m, vm_offset_t) & 3) {
278 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
279 			bcopy(m->m_data,
280 			    (char *)(mtod(m, vm_offset_t)
281 				- (mtod(m, vm_offset_t) & 3)),
282 			    m->m_len);
283 			mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3);
284 		}
285 #endif
286 	}
287 
288 	/* Deliver to upper layer protocol */
289 	switch (af) {
290 #ifdef INET
291 	case AF_INET:
292 		isr = NETISR_IP;
293 		break;
294 #endif
295 #ifdef INET6
296 	case AF_INET6:
297 		m->m_flags |= M_LOOP;
298 		isr = NETISR_IPV6;
299 		break;
300 #endif
301 #ifdef IPX
302 	case AF_IPX:
303 		isr = NETISR_IPX;
304 		break;
305 #endif
306 #ifdef NETATALK
307 	case AF_APPLETALK:
308 		isr = NETISR_ATALK2;
309 		break;
310 #endif
311 	default:
312 		printf("if_simloop: can't handle af=%d\n", af);
313 		m_freem(m);
314 		return (EAFNOSUPPORT);
315 	}
316 	ifp->if_ipackets++;
317 	ifp->if_ibytes += m->m_pkthdr.len;
318 	netisr_queue(isr, m);
319 	return (0);
320 }
321 
322 /* ARGSUSED */
323 static void
324 lortrequest(cmd, rt, info)
325 	int cmd;
326 	struct rtentry *rt;
327 	struct rt_addrinfo *info;
328 {
329 	RT_LOCK_ASSERT(rt);
330 	if (rt)
331 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu;
332 }
333 
334 /*
335  * Process an ioctl request.
336  */
337 /* ARGSUSED */
338 int
339 loioctl(ifp, cmd, data)
340 	register struct ifnet *ifp;
341 	u_long cmd;
342 	caddr_t data;
343 {
344 	register struct ifaddr *ifa;
345 	register struct ifreq *ifr = (struct ifreq *)data;
346 	register int error = 0;
347 
348 	switch (cmd) {
349 
350 	case SIOCSIFADDR:
351 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
352 		ifa = (struct ifaddr *)data;
353 		ifa->ifa_rtrequest = lortrequest;
354 		/*
355 		 * Everything else is done at a higher level.
356 		 */
357 		break;
358 
359 	case SIOCADDMULTI:
360 	case SIOCDELMULTI:
361 		if (ifr == 0) {
362 			error = EAFNOSUPPORT;		/* XXX */
363 			break;
364 		}
365 		switch (ifr->ifr_addr.sa_family) {
366 
367 #ifdef INET
368 		case AF_INET:
369 			break;
370 #endif
371 #ifdef INET6
372 		case AF_INET6:
373 			break;
374 #endif
375 
376 		default:
377 			error = EAFNOSUPPORT;
378 			break;
379 		}
380 		break;
381 
382 	case SIOCSIFMTU:
383 		ifp->if_mtu = ifr->ifr_mtu;
384 		break;
385 
386 	case SIOCSIFFLAGS:
387 		break;
388 
389 	default:
390 		error = EINVAL;
391 	}
392 	return (error);
393 }
394