xref: /freebsd/sys/net/if_loop.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 #include "loop.h"
41 
42 #include "opt_atalk.h"
43 #include "opt_inet.h"
44 #include "opt_inet6.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 INET6
71 #ifndef INET
72 #include <netinet/in.h>
73 #endif
74 #include <netinet6/in6_var.h>
75 #include <netinet6/ip6.h>
76 #endif
77 
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82 
83 #ifdef NETATALK
84 #include <netatalk/at.h>
85 #include <netatalk/at_var.h>
86 #endif NETATALK
87 
88 int loioctl __P((struct ifnet *, u_long, caddr_t));
89 static void lortrequest __P((int, struct rtentry *, struct sockaddr *));
90 
91 static void loopattach __P((void *));
92 PSEUDO_SET(loopattach, if_loop);
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 struct	ifnet loif[NLOOP];
106 
107 /* ARGSUSED */
108 static void
109 loopattach(dummy)
110 	void *dummy;
111 {
112 	register struct ifnet *ifp;
113 	register int i = 0;
114 
115 	for (ifp = loif; i < NLOOP; ifp++) {
116 	    ifp->if_name = "lo";
117 	    ifp->if_unit = i++;
118 	    ifp->if_mtu = LOMTU;
119 	    ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
120 	    ifp->if_ioctl = loioctl;
121 	    ifp->if_output = looutput;
122 	    ifp->if_type = IFT_LOOP;
123 	    ifp->if_snd.ifq_maxlen = ifqmaxlen;
124 	    if_attach(ifp);
125 	    bpfattach(ifp, DLT_NULL, sizeof(u_int));
126 	}
127 }
128 
129 int
130 looutput(ifp, m, dst, rt)
131 	struct ifnet *ifp;
132 	register struct mbuf *m;
133 	struct sockaddr *dst;
134 	register struct rtentry *rt;
135 {
136 	if ((m->m_flags & M_PKTHDR) == 0)
137 		panic("looutput no HDR");
138 
139 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
140 		m_freem(m);
141 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
142 		        rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
143 	}
144 	/*
145 	 * KAME requires that the packet to be contiguous on the
146 	 * mbuf.  We need to make that sure.
147 	 * this kind of code should be avoided.
148 	 * XXX: fails to join if interface MTU > MCLBYTES.  jumbogram?
149 	 */
150 	if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) {
151 		struct mbuf *n;
152 
153 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
154 		if (!n)
155 			goto contiguousfail;
156 		MCLGET(n, M_DONTWAIT);
157 		if (! (n->m_flags & M_EXT)) {
158 			m_freem(n);
159 			goto contiguousfail;
160 		}
161 
162 		m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t));
163 		n->m_pkthdr = m->m_pkthdr;
164 		n->m_len = m->m_pkthdr.len;
165 		m_freem(m);
166 		m = n;
167 	}
168 	if (0) {
169 contiguousfail:
170 		printf("looutput: mbuf allocation failed\n");
171 	}
172 
173 	ifp->if_opackets++;
174 	ifp->if_obytes += m->m_pkthdr.len;
175 #if 1	/* XXX */
176 	switch (dst->sa_family) {
177 	case AF_INET:
178 	case AF_INET6:
179 	case AF_IPX:
180 	case AF_NS:
181 	case AF_APPLETALK:
182 		break;
183 	default:
184 		printf("looutput: af=%d unexpected", dst->sa_family);
185 		m_freem(m);
186 		return (EAFNOSUPPORT);
187 	}
188 #endif
189 	return(if_simloop(ifp, m, dst, 0));
190 }
191 
192 /*
193  * if_simloop()
194  *
195  * This function is to support software emulation of hardware loopback,
196  * i.e., for interfaces with the IFF_SIMPLEX attribute. Since they can't
197  * hear their own broadcasts, we create a copy of the packet that we
198  * would normally receive via a hardware loopback.
199  *
200  * This function expects the packet to include the media header of length hlen.
201  */
202 
203 int
204 if_simloop(ifp, m, dst, hlen)
205 	struct ifnet *ifp;
206 	register struct mbuf *m;
207 	struct sockaddr *dst;
208 	int hlen;
209 {
210 	int s, isr;
211 	register struct ifqueue *ifq = 0;
212 
213 	if ((m->m_flags & M_PKTHDR) == 0)
214 		panic("if_simloop: no HDR");
215 	m->m_pkthdr.rcvif = ifp;
216 	/* BPF write needs to be handled specially */
217 	if (dst->sa_family == AF_UNSPEC) {
218 		dst->sa_family = *(mtod(m, int *));
219 		m->m_len -= sizeof(int);
220 		m->m_pkthdr.len -= sizeof(int);
221 		m->m_data += sizeof(int);
222 	}
223 
224 	if (ifp->if_bpf) {
225 		struct mbuf m0, *n = m;
226 		u_int af = dst->sa_family;
227 
228 		/*
229 		 * We need to prepend the address family as
230 		 * a four byte field.  Cons up a dummy header
231 		 * to pacify bpf.  This is safe because bpf
232 		 * will only read from the mbuf (i.e., it won't
233 		 * try to free it or keep a pointer a to it).
234 		 */
235 		m0.m_next = m;
236 		m0.m_len = 4;
237 		m0.m_data = (char *)&af;
238 		n = &m0;
239 		bpf_mtap(ifp, n);
240 	}
241 
242 	/* Strip away media header */
243 	if (hlen > 0) {
244 #ifdef __alpha__
245 		/* The alpha doesn't like unaligned data.
246 		 * We move data down in the first mbuf */
247 		if (hlen & 3) {
248 			bcopy(m->m_data + hlen, m->m_data, m->m_len - hlen);
249 			m->m_len -= hlen;
250 			if (m->m_flags & M_PKTHDR)
251 				m->m_pkthdr.len -= hlen;
252 		} else
253 #endif
254 		m_adj(m, hlen);
255 	}
256 
257 	switch (dst->sa_family) {
258 #ifdef INET
259 	case AF_INET:
260 		ifq = &ipintrq;
261 		isr = NETISR_IP;
262 		break;
263 #endif
264 #ifdef INET6
265 	case AF_INET6:
266 		m->m_flags |= M_LOOP;
267 		ifq = &ip6intrq;
268 		isr = NETISR_IPV6;
269 		break;
270 #endif
271 #ifdef IPX
272 	case AF_IPX:
273 		ifq = &ipxintrq;
274 		isr = NETISR_IPX;
275 		break;
276 #endif
277 #ifdef NS
278 	case AF_NS:
279 		ifq = &nsintrq;
280 		isr = NETISR_NS;
281 		break;
282 #endif
283 #ifdef NETATALK
284 	case AF_APPLETALK:
285 	        ifq = &atintrq2;
286 		isr = NETISR_ATALK;
287 		break;
288 #endif NETATALK
289 	default:
290 		printf("if_simloop: can't handle af=%d\n", dst->sa_family);
291 		m_freem(m);
292 		return (EAFNOSUPPORT);
293 	}
294 	s = splimp();
295 	if (IF_QFULL(ifq)) {
296 		IF_DROP(ifq);
297 		m_freem(m);
298 		splx(s);
299 		return (ENOBUFS);
300 	}
301 	IF_ENQUEUE(ifq, m);
302 	schednetisr(isr);
303 	ifp->if_ipackets++;
304 	ifp->if_ibytes += m->m_pkthdr.len;
305 	splx(s);
306 	return (0);
307 }
308 
309 /* ARGSUSED */
310 static void
311 lortrequest(cmd, rt, sa)
312 	int cmd;
313 	struct rtentry *rt;
314 	struct sockaddr *sa;
315 {
316 	if (rt) {
317 		rt->rt_rmx.rmx_mtu = rt->rt_ifp->if_mtu; /* for ISO */
318 		/*
319 		 * For optimal performance, the send and receive buffers
320 		 * should be at least twice the MTU plus a little more for
321 		 * overhead.
322 		 */
323 		rt->rt_rmx.rmx_recvpipe =
324 			rt->rt_rmx.rmx_sendpipe = 3 * LOMTU;
325 	}
326 }
327 
328 /*
329  * Process an ioctl request.
330  */
331 /* ARGSUSED */
332 int
333 loioctl(ifp, cmd, data)
334 	register struct ifnet *ifp;
335 	u_long cmd;
336 	caddr_t data;
337 {
338 	register struct ifaddr *ifa;
339 	register struct ifreq *ifr = (struct ifreq *)data;
340 	register int error = 0;
341 
342 	switch (cmd) {
343 
344 	case SIOCSIFADDR:
345 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
346 		ifa = (struct ifaddr *)data;
347 		ifa->ifa_rtrequest = lortrequest;
348 		/*
349 		 * Everything else is done at a higher level.
350 		 */
351 		break;
352 
353 	case SIOCADDMULTI:
354 	case SIOCDELMULTI:
355 		if (ifr == 0) {
356 			error = EAFNOSUPPORT;		/* XXX */
357 			break;
358 		}
359 		switch (ifr->ifr_addr.sa_family) {
360 
361 #ifdef INET
362 		case AF_INET:
363 			break;
364 #endif
365 #ifdef INET6
366 		case AF_INET6:
367 			break;
368 #endif
369 
370 		default:
371 			error = EAFNOSUPPORT;
372 			break;
373 		}
374 		break;
375 
376 	case SIOCSIFMTU:
377 		ifp->if_mtu = ifr->ifr_mtu;
378 		break;
379 
380 	case SIOCSIFFLAGS:
381 		break;
382 
383 	default:
384 		error = EINVAL;
385 	}
386 	return (error);
387 }
388