xref: /freebsd/sys/net/if_loop.c (revision c11e094d96120a2e0e726ed9705ae0ec08db49b6)
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 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
53 #include <sys/rman.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 
58 #include <net/if.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/bpf.h>
63 #include <net/bpfdesc.h>
64 
65 #ifdef	INET
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #endif
69 
70 #ifdef IPX
71 #include <netipx/ipx.h>
72 #include <netipx/ipx_if.h>
73 #endif
74 
75 #ifdef INET6
76 #ifndef INET
77 #include <netinet/in.h>
78 #endif
79 #include <netinet6/in6_var.h>
80 #include <netinet/ip6.h>
81 #endif
82 
83 #ifdef NS
84 #include <netns/ns.h>
85 #include <netns/ns_if.h>
86 #endif
87 
88 #ifdef NETATALK
89 #include <netatalk/at.h>
90 #include <netatalk/at_var.h>
91 #endif
92 
93 #ifdef TINY_LOMTU
94 #define	LOMTU	(1024+512)
95 #elif defined(LARGE_LOMTU)
96 #define LOMTU	131072
97 #else
98 #define LOMTU	16384
99 #endif
100 
101 #define LONAME	"lo"
102 
103 struct lo_softc {
104 	struct	ifnet sc_if;		/* network-visible interface */
105 	LIST_ENTRY(lo_softc) sc_next;
106 };
107 
108 int		loioctl(struct ifnet *, u_long, caddr_t);
109 static void	lortrequest(int, struct rtentry *, struct rt_addrinfo *);
110 int		looutput(struct ifnet *ifp, struct mbuf *m,
111 		    struct sockaddr *dst, struct rtentry *rt);
112 int		lo_clone_create(struct if_clone *, int);
113 int		lo_clone_destroy(struct ifnet *);
114 
115 struct ifnet *loif = NULL;			/* Used externally */
116 
117 static MALLOC_DEFINE(M_LO, LONAME, "Loopback Interface");
118 
119 static LIST_HEAD(lo_list, lo_softc) lo_list;
120 
121 struct if_clone lo_cloner =
122     IF_CLONE_INITIALIZER(LONAME, lo_clone_create, lo_clone_destroy, IF_MAXUNIT);
123 
124 int
125 lo_clone_destroy(ifp)
126 	struct ifnet *ifp;
127 {
128 	struct lo_softc *sc;
129 
130 	sc = ifp->if_softc;
131 
132 	/*
133 	 * Prevent lo0 from being destroyed.
134 	 */
135 	if (loif == ifp)
136 		return (EINVAL);
137 
138 	bpfdetach(ifp);
139 	if_detach(ifp);
140 	LIST_REMOVE(sc, sc_next);
141 	free(sc, M_LO);
142 	return (0);
143 }
144 
145 int
146 lo_clone_create(ifc, unit)
147 	struct if_clone *ifc;
148 	int unit;
149 {
150 	struct lo_softc *sc;
151 
152 	MALLOC(sc, struct lo_softc *, sizeof(*sc), M_LO, M_WAITOK | M_ZERO);
153 
154 	sc->sc_if.if_name = LONAME;
155 	sc->sc_if.if_unit = unit;
156 	sc->sc_if.if_mtu = LOMTU;
157 	sc->sc_if.if_flags = IFF_LOOPBACK | IFF_MULTICAST;
158 	sc->sc_if.if_ioctl = loioctl;
159 	sc->sc_if.if_output = looutput;
160 	sc->sc_if.if_type = IFT_LOOP;
161 	sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
162 	sc->sc_if.if_softc = sc;
163 	if_attach(&sc->sc_if);
164 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
165 	LIST_INSERT_HEAD(&lo_list, sc, sc_next);
166 	if (loif == NULL)
167 		loif = &sc->sc_if;
168 
169 	return (0);
170 }
171 
172 static int
173 loop_modevent(module_t mod, int type, void *data)
174 {
175 	int err;
176 
177 	switch (type) {
178 	case MOD_LOAD:
179 		LIST_INIT(&lo_list);
180 		if_clone_attach(&lo_cloner);
181 
182 		/* Create lo0 */
183 		err = if_clone_create("lo0", sizeof ("lo0"));
184 		KASSERT(err == 0, ("%s: can't create lo0", __func__));
185 		break;
186 	case MOD_UNLOAD:
187 		printf("loop module unload - not possible for this module type\n");
188 		return EINVAL;
189 	}
190 	return 0;
191 }
192 
193 static moduledata_t loop_mod = {
194 	"loop",
195 	loop_modevent,
196 	0
197 };
198 
199 DECLARE_MODULE(loop, loop_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
200 
201 int
202 looutput(ifp, m, dst, rt)
203 	struct ifnet *ifp;
204 	register struct mbuf *m;
205 	struct sockaddr *dst;
206 	register struct rtentry *rt;
207 {
208 	if ((m->m_flags & M_PKTHDR) == 0)
209 		panic("looutput no HDR");
210 
211 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
212 		m_freem(m);
213 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
214 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
215 	}
216 	/*
217 	 * KAME requires that the packet to be contiguous on the
218 	 * mbuf.  We need to make that sure.
219 	 * this kind of code should be avoided.
220 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
221 	 */
222 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
223 		struct mbuf *n;
224 
225 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
226 		if (!n)
227 			goto contiguousfail;
228 		MCLGET(n, M_DONTWAIT);
229 		if (! (n->m_flags & M_EXT)) {
230 			m_freem(n);
231 			goto contiguousfail;
232 		}
233 
234 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
235 		n->m_pkthdr = m->m_pkthdr;
236 		n->m_len = m->m_pkthdr.len;
237 		n->m_pkthdr.aux = m->m_pkthdr.aux;
238 		m->m_pkthdr.aux = (struct mbuf *)NULL;
239 		m_freem(m);
240 		m = n;
241 	}
242 	if (0) {
243 contiguousfail:
244 		printf("looutput: mbuf allocation failed\n");
245 	}
246 
247 	ifp->if_opackets++;
248 	ifp->if_obytes += m->m_pkthdr.len;
249 #if 1	/* XXX */
250 	switch (dst->sa_family) {
251 	case AF_INET:
252 	case AF_INET6:
253 	case AF_IPX:
254 	case AF_NS:
255 	case AF_APPLETALK:
256 		break;
257 	default:
258 		printf("looutput: af=%d unexpected\n", dst->sa_family);
259 		m_freem(m);
260 		return (EAFNOSUPPORT);
261 	}
262 #endif
263 	return(if_simloop(ifp, m, dst->sa_family, 0));
264 }
265 
266 /*
267  * if_simloop()
268  *
269  * This function is to support software emulation of hardware loopback,
270  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
271  * hear their own broadcasts, we create a copy of the packet that we
272  * would normally receive via a hardware loopback.
273  *
274  * This function expects the packet to include the media header of length hlen.
275  */
276 
277 int
278 if_simloop(ifp, m, af, hlen)
279 	struct ifnet *ifp;
280 	struct mbuf *m;
281 	int af;
282 	int hlen;
283 {
284 	int isr;
285 	struct ifqueue *inq = 0;
286 
287 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("if_simloop: no HDR"));
288 	m->m_pkthdr.rcvif = ifp;
289 
290 	/* BPF write needs to be handled specially */
291 	if (af == AF_UNSPEC) {
292 		KASSERT(m->m_len >= sizeof(int), ("if_simloop: m_len"));
293 		af = *(mtod(m, int *));
294 		m->m_len -= sizeof(int);
295 		m->m_pkthdr.len -= sizeof(int);
296 		m->m_data += sizeof(int);
297 	}
298 
299 	/* Let BPF see incoming packet */
300 	if (ifp->if_bpf) {
301 		struct mbuf m0, *n = m;
302 
303 		if (ifp->if_bpf->bif_dlt == DLT_NULL) {
304 			/*
305 			 * We need to prepend the address family as
306 			 * a four byte field.  Cons up a dummy header
307 			 * to pacify bpf.  This is safe because bpf
308 			 * will only read from the mbuf (i.e., it won't
309 			 * try to free it or keep a pointer a to it).
310 			 */
311 			m0.m_next = m;
312 			m0.m_len = 4;
313 			m0.m_data = (char *)&af;
314 			n = &m0;
315 		}
316 		bpf_mtap(ifp, n);
317 	}
318 
319 	/* Strip away media header */
320 	if (hlen > 0) {
321 		m_adj(m, hlen);
322 #if defined(__alpha__) || defined(__ia64__) || defined(__sparc64__)
323 		/* The alpha doesn't like unaligned data.
324 		 * We move data down in the first mbuf */
325 		if (mtod(m, vm_offset_t) & 3) {
326 			KASSERT(hlen >= 3, ("if_simloop: hlen too small"));
327 			bcopy(m->m_data,
328 			    (char *)(mtod(m, vm_offset_t)
329 				- (mtod(m, vm_offset_t) & 3)),
330 			    m->m_len);
331 			mtod(m,vm_offset_t) -= (mtod(m, vm_offset_t) & 3);
332 		}
333 #endif
334 	}
335 
336 	/* Deliver to upper layer protocol */
337 	switch (af) {
338 #ifdef INET
339 	case AF_INET:
340 		inq = &ipintrq;
341 		isr = NETISR_IP;
342 		break;
343 #endif
344 #ifdef INET6
345 	case AF_INET6:
346 		m->m_flags |= M_LOOP;
347 		inq = &ip6intrq;
348 		isr = NETISR_IPV6;
349 		break;
350 #endif
351 #ifdef IPX
352 	case AF_IPX:
353 		inq = &ipxintrq;
354 		isr = NETISR_IPX;
355 		break;
356 #endif
357 #ifdef NS
358 	case AF_NS:
359 		inq = &nsintrq;
360 		isr = NETISR_NS;
361 		break;
362 #endif
363 #ifdef NETATALK
364 	case AF_APPLETALK:
365 	        inq = &atintrq2;
366 		isr = NETISR_ATALK;
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 	(void) IF_HANDOFF(inq, m, NULL);
377 	schednetisr(isr);
378 	return (0);
379 }
380 
381 /* ARGSUSED */
382 static void
383 lortrequest(cmd, rt, info)
384 	int cmd;
385 	struct rtentry *rt;
386 	struct rt_addrinfo *info;
387 {
388 	if (rt) {
389 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
390 		/*
391 		 * For optimal performance, the send and receive buffers
392 		 * should be at least twice the MTU plus a little more for
393 		 * overhead.
394 		 */
395 		rt->rt_rmx.rmx_recvpipe =
396 			rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
397 	}
398 }
399 
400 /*
401  * Process an ioctl request.
402  */
403 /* ARGSUSED */
404 int
405 loioctl(ifp, cmd, data)
406 	register struct ifnet *ifp;
407 	u_long cmd;
408 	caddr_t data;
409 {
410 	register struct ifaddr *ifa;
411 	register struct ifreq *ifr = (struct ifreq *)data;
412 	register int error = 0;
413 
414 	switch (cmd) {
415 
416 	case SIOCSIFADDR:
417 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
418 		ifa = (struct ifaddr *)data;
419 		ifa->ifa_rtrequest = lortrequest;
420 		/*
421 		 * Everything else is done at a higher level.
422 		 */
423 		break;
424 
425 	case SIOCADDMULTI:
426 	case SIOCDELMULTI:
427 		if (ifr == 0) {
428 			error = EAFNOSUPPORT;		/* XXX */
429 			break;
430 		}
431 		switch (ifr->ifr_addr.sa_family) {
432 
433 #ifdef INET
434 		case AF_INET:
435 			break;
436 #endif
437 #ifdef INET6
438 		case AF_INET6:
439 			break;
440 #endif
441 
442 		default:
443 			error = EAFNOSUPPORT;
444 			break;
445 		}
446 		break;
447 
448 	case SIOCSIFMTU:
449 		ifp->if_mtu = ifr->ifr_mtu;
450 		break;
451 
452 	case SIOCSIFFLAGS:
453 		break;
454 
455 	default:
456 		error = EINVAL;
457 	}
458 	return (error);
459 }
460