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