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