if_loop.c revision 51646
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: head/sys/net/if_loop.c 51646 1999-09-25 12:06:01Z phk $
35 */
36
37/*
38 * Loopback interface driver for protocol testing and timing.
39 */
40#include "loop.h"
41#if NLOOP > 0
42
43#include "opt_atalk.h"
44#include "opt_inet.h"
45#include "opt_ipx.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/mbuf.h>
51#include <sys/socket.h>
52#include <sys/sockio.h>
53
54#include <net/if.h>
55#include <net/if_types.h>
56#include <net/netisr.h>
57#include <net/route.h>
58#include <net/bpf.h>
59
60#ifdef	INET
61#include <netinet/in.h>
62#include <netinet/in_var.h>
63#endif
64
65#ifdef IPX
66#include <netipx/ipx.h>
67#include <netipx/ipx_if.h>
68#endif
69
70#ifdef NS
71#include <netns/ns.h>
72#include <netns/ns_if.h>
73#endif
74
75#ifdef ISO
76#include <netiso/iso.h>
77#include <netiso/iso_var.h>
78#endif
79
80#ifdef NETATALK
81#include <netatalk/at.h>
82#include <netatalk/at_var.h>
83#endif NETATALK
84
85static int loioctl __P((struct ifnet *, u_long, caddr_t));
86static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
87
88static void loopattach __P((void *));
89PSEUDO_SET(loopattach, if_loop);
90
91static int looutput __P((struct ifnet *ifp,
92		struct mbuf *m, struct sockaddr *dst, struct rtentry *rt));
93
94#ifdef TINY_LOMTU
95#define	LOMTU	(1024+512)
96#else
97#define LOMTU	16384
98#endif
99
100struct	ifnet loif[NLOOP];
101
102/* ARGSUSED */
103static void
104loopattach(dummy)
105	void *dummy;
106{
107	register struct ifnet *ifp;
108	register int i = 0;
109
110	for (ifp = loif; i < NLOOP; ifp++) {
111	    ifp->if_name = "lo";
112	    ifp->if_unit = i++;
113	    ifp->if_mtu = LOMTU;
114	    ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
115	    ifp->if_ioctl = loioctl;
116	    ifp->if_output = looutput;
117	    ifp->if_type = IFT_LOOP;
118	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
119	    if_attach(ifp);
120	    bpfattach(ifp, DLT_NULL, sizeof(u_int));
121	}
122}
123
124static int
125looutput(ifp, m, dst, rt)
126	struct ifnet *ifp;
127	register struct mbuf *m;
128	struct sockaddr *dst;
129	register struct rtentry *rt;
130{
131	if ((m->m_flags & M_PKTHDR) == 0)
132		panic("looutput no HDR");
133
134	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
135		m_freem(m);
136		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
137		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
138	}
139	ifp->if_opackets++;
140	ifp->if_obytes += m->m_pkthdr.len;
141#if 1	/* XXX */
142	switch (dst->sa_family) {
143	case AF_INET:
144	case AF_IPX:
145	case AF_NS:
146	case AF_ISO:
147	case AF_APPLETALK:
148		break;
149	default:
150		printf("looutput: af=%d unexpected", dst->sa_family);
151		m_freem(m);
152		return (EAFNOSUPPORT);
153	}
154#endif
155	return(if_simloop(ifp, m, dst, 0));
156}
157
158/*
159 * if_simloop()
160 *
161 * This function is to support software emulation of hardware loopback,
162 * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
163 * hear their own broadcasts, we create a copy of the packet that we
164 * would normally receive via a hardware loopback.
165 *
166 * This function expects the packet to include the media header of length hlen.
167 */
168
169int
170if_simloop(ifp, m, dst, hlen)
171	struct ifnet *ifp;
172	register struct mbuf *m;
173	struct sockaddr *dst;
174	int hlen;
175{
176	int s, isr;
177	register struct ifqueue *ifq = 0;
178
179	if ((m->m_flags & M_PKTHDR) == 0)
180		panic("if_simloop: no HDR");
181	m->m_pkthdr.rcvif = ifp;
182	/* BPF write needs to be handled specially */
183	if (dst->sa_family == AF_UNSPEC) {
184		dst->sa_family = *(mtod(m, int *));
185		m->m_len -= sizeof(int);
186		m->m_pkthdr.len -= sizeof(int);
187		m->m_data += sizeof(int);
188	}
189
190	if (ifp->if_bpf) {
191		struct mbuf m0, *n = m;
192		u_int af = dst->sa_family;
193
194		/*
195		 * We need to prepend the address family as
196		 * a four byte field.  Cons up a dummy header
197		 * to pacify bpf.  This is safe because bpf
198		 * will only read from the mbuf (i.e., it won't
199		 * try to free it or keep a pointer a to it).
200		 */
201		m0.m_next = m;
202		m0.m_len = 4;
203		m0.m_data = (char *)&af;
204		n = &m0;
205		bpf_mtap(ifp, n);
206	}
207
208	/* Strip away media header */
209	if (hlen > 0) {
210#ifdef __alpha__
211		/* The alpha doesn't like unaligned data.
212		 * We move data down in the first mbuf */
213		if (hlen & 3) {
214			bcopy(m->m_data + hlen, m->m_data, m->m_len - hlen);
215			m->m_len -= hlen;
216			if (m->m_flags & M_PKTHDR)
217				m->m_pkthdr.len -= hlen;
218		} else
219#endif
220		m_adj(m, hlen);
221	}
222
223	switch (dst->sa_family) {
224#ifdef INET
225	case AF_INET:
226		ifq = &ipintrq;
227		isr = NETISR_IP;
228		break;
229#endif
230#ifdef IPX
231	case AF_IPX:
232		ifq = &ipxintrq;
233		isr = NETISR_IPX;
234		break;
235#endif
236#ifdef NS
237	case AF_NS:
238		ifq = &nsintrq;
239		isr = NETISR_NS;
240		break;
241#endif
242#ifdef ISO
243	case AF_ISO:
244		ifq = &clnlintrq;
245		isr = NETISR_ISO;
246		break;
247#endif
248#ifdef NETATALK
249	case AF_APPLETALK:
250	        ifq = &atintrq2;
251		isr = NETISR_ATALK;
252		break;
253#endif NETATALK
254	default:
255		printf("if_simloop: can't handle af=%d\n", dst->sa_family);
256		m_freem(m);
257		return (EAFNOSUPPORT);
258	}
259	s = splimp();
260	if (IF_QFULL(ifq)) {
261		IF_DROP(ifq);
262		m_freem(m);
263		splx(s);
264		return (ENOBUFS);
265	}
266	IF_ENQUEUE(ifq, m);
267	schednetisr(isr);
268	ifp->if_ipackets++;
269	ifp->if_ibytes += m->m_pkthdr.len;
270	splx(s);
271	return (0);
272}
273
274/* ARGSUSED */
275static void
276lortrequest(cmd, rt, sa)
277	int cmd;
278	struct rtentry *rt;
279	struct sockaddr *sa;
280{
281	if (rt) {
282		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
283		/*
284		 * For optimal performance, the send and receive buffers
285		 * should be at least twice the MTU plus a little more for
286		 * overhead.
287		 */
288		rt->rt_rmx.rmx_recvpipe =
289			rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
290	}
291}
292
293/*
294 * Process an ioctl request.
295 */
296/* ARGSUSED */
297static int
298loioctl(ifp, cmd, data)
299	register struct ifnet *ifp;
300	u_long cmd;
301	caddr_t data;
302{
303	register struct ifaddr *ifa;
304	register struct ifreq *ifr = (struct ifreq *)data;
305	register int error = 0;
306
307	switch (cmd) {
308
309	case SIOCSIFADDR:
310		ifp->if_flags |= IFF_UP | IFF_RUNNING;
311		ifa = (struct ifaddr *)data;
312		ifa->ifa_rtrequest = lortrequest;
313		/*
314		 * Everything else is done at a higher level.
315		 */
316		break;
317
318	case SIOCADDMULTI:
319	case SIOCDELMULTI:
320		if (ifr == 0) {
321			error = EAFNOSUPPORT;		/* XXX */
322			break;
323		}
324		switch (ifr->ifr_addr.sa_family) {
325
326#ifdef INET
327		case AF_INET:
328			break;
329#endif
330
331		default:
332			error = EAFNOSUPPORT;
333			break;
334		}
335		break;
336
337	case SIOCSIFMTU:
338		ifp->if_mtu = ifr->ifr_mtu;
339		break;
340
341	case SIOCSIFFLAGS:
342		break;
343
344	default:
345		error = EINVAL;
346	}
347	return (error);
348}
349#endif /* NLOOP > 0 */
350