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