xref: /openbsd-src/sys/net/if_loop.c (revision cf2525843d483a385de106a1361b2b9c18d96583)
1 /*	$OpenBSD: if_loop.c,v 1.40 2006/03/25 22:41:47 djm Exp $	*/
2 /*	$NetBSD: if_loop.c,v 1.15 1996/05/07 02:40:33 thorpej Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)if_loop.c	8.1 (Berkeley) 6/10/93
62  */
63 
64 /*
65  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
66  *
67  * NRL grants permission for redistribution and use in source and binary
68  * forms, with or without modification, of the software and documentation
69  * created at NRL provided that the following conditions are met:
70  *
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. All advertising materials mentioning features or use of this software
77  *    must display the following acknowledgements:
78  * 	This product includes software developed by the University of
79  * 	California, Berkeley and its contributors.
80  * 	This product includes software developed at the Information
81  * 	Technology Division, US Naval Research Laboratory.
82  * 4. Neither the name of the NRL nor the names of its contributors
83  *    may be used to endorse or promote products derived from this software
84  *    without specific prior written permission.
85  *
86  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
87  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
88  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
89  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
90  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
91  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
92  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
93  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
94  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
95  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
96  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  *
98  * The views and conclusions contained in the software and documentation
99  * are those of the authors and should not be interpreted as representing
100  * official policies, either expressed or implied, of the US Naval
101  * Research Laboratory (NRL).
102  */
103 
104 /*
105  * Loopback interface driver for protocol testing and timing.
106  */
107 
108 #include "bpfilter.h"
109 
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/kernel.h>
113 #include <sys/mbuf.h>
114 #include <sys/socket.h>
115 #include <sys/errno.h>
116 #include <sys/ioctl.h>
117 #include <sys/time.h>
118 
119 #include <machine/cpu.h>
120 
121 #include <net/if.h>
122 #include <net/if_types.h>
123 #include <net/netisr.h>
124 #include <net/route.h>
125 
126 #ifdef	INET
127 #include <netinet/in.h>
128 #include <netinet/in_systm.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip.h>
131 #endif
132 
133 #ifdef INET6
134 #ifndef INET
135 #include <netinet/in.h>
136 #endif
137 #include <netinet6/in6_var.h>
138 #include <netinet/ip6.h>
139 #endif
140 
141 #ifdef IPX
142 #include <netipx/ipx.h>
143 #include <netipx/ipx_if.h>
144 #endif
145 
146 #ifdef NETATALK
147 #include <netinet/if_ether.h>
148 #include <netatalk/at.h>
149 #include <netatalk/at_var.h>
150 #endif
151 
152 #if NBPFILTER > 0
153 #include <net/bpf.h>
154 #endif
155 
156 #if defined(LARGE_LOMTU)
157 #define LOMTU	(131072 +  MHLEN + MLEN)
158 #else
159 #define	LOMTU	(32768 +  MHLEN + MLEN)
160 #endif
161 
162 #ifdef ALTQ
163 static void lo_altqstart(struct ifnet *);
164 #endif
165 
166 int	loop_clone_create(struct if_clone *, int);
167 int	loop_clone_destroy(struct ifnet *);
168 
169 struct if_clone loop_cloner =
170     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
171 
172 /* ARGSUSED */
173 void
174 loopattach(n)
175 	int n;
176 {
177 	(void) loop_clone_create(&loop_cloner, 0);
178 	if_clone_attach(&loop_cloner);
179 }
180 
181 int
182 loop_clone_create(ifc, unit)
183 	struct if_clone *ifc;
184 	int unit;
185 {
186 	struct ifnet *ifp;
187 
188 	MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_DEVBUF, M_NOWAIT);
189 	if (ifp == NULL)
190 		return (ENOMEM);
191 	bzero(ifp, sizeof(struct ifnet));
192 
193 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
194 	ifp->if_softc = NULL;
195 	ifp->if_mtu = LOMTU;
196 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
197 	ifp->if_ioctl = loioctl;
198 	ifp->if_output = looutput;
199 	ifp->if_type = IFT_LOOP;
200 	ifp->if_hdrlen = sizeof(u_int32_t);
201 	ifp->if_addrlen = 0;
202 	IFQ_SET_READY(&ifp->if_snd);
203 #ifdef ALTQ
204 	ifp->if_start = lo_altqstart;
205 #endif
206 	if (unit == 0) {
207 		lo0ifp = ifp;
208 		if_attachhead(ifp);
209 		if_addgroup(lo0ifp, ifc->ifc_name);
210 	} else
211 		if_attach(ifp);
212 	if_alloc_sadl(ifp);
213 #if NBPFILTER > 0
214 	bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
215 #endif
216 	return (0);
217 }
218 
219 int
220 loop_clone_destroy(ifp)
221 	struct ifnet *ifp;
222 {
223 	if (ifp == lo0ifp)
224 		return (EPERM);
225 
226 	if_detach(ifp);
227 
228 	free(ifp, M_DEVBUF);
229 	return (0);
230 }
231 
232 int
233 looutput(ifp, m, dst, rt)
234 	struct ifnet *ifp;
235 	struct mbuf *m;
236 	struct sockaddr *dst;
237 	struct rtentry *rt;
238 {
239 	int s, isr;
240 	struct ifqueue *ifq = 0;
241 
242 	if ((m->m_flags & M_PKTHDR) == 0)
243 		panic("looutput: no header mbuf");
244 #if NBPFILTER > 0
245 	/*
246 	 * only send packets to bpf if they are real loopback packets;
247 	 * looutput() is also called for SIMPLEX interfaces to duplicate
248 	 * packets for local use. But don't dup them to bpf.
249 	 */
250 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK))
251 		bpf_mtap_af(ifp->if_bpf, htonl(dst->sa_family), m,
252 		    BPF_DIRECTION_OUT);
253 #endif
254 	m->m_pkthdr.rcvif = ifp;
255 
256 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
257 		m_freem(m);
258 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
259 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
260 	}
261 
262 	ifp->if_opackets++;
263 	ifp->if_obytes += m->m_pkthdr.len;
264 #ifdef ALTQ
265 	/*
266 	 * altq for loop is just for debugging.
267 	 * only used when called for loop interface (not for
268 	 * a simplex interface).
269 	 */
270 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd))
271 	    && ifp->if_start == lo_altqstart) {
272 		int32_t *afp;
273 	        int error;
274 
275 		M_PREPEND(m, sizeof(int32_t), M_DONTWAIT);
276 		if (m == 0)
277 			return (ENOBUFS);
278 		afp = mtod(m, int32_t *);
279 		*afp = (int32_t)dst->sa_family;
280 
281 	        s = splnet();
282 		IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
283 		(*ifp->if_start)(ifp);
284 		splx(s);
285 		return (error);
286 	}
287 #endif /* ALTQ */
288 	switch (dst->sa_family) {
289 
290 #ifdef INET
291 	case AF_INET:
292 		ifq = &ipintrq;
293 		isr = NETISR_IP;
294 		break;
295 #endif
296 #ifdef INET6
297 	case AF_INET6:
298 		ifq = &ip6intrq;
299 		isr = NETISR_IPV6;
300 		break;
301 #endif /* INET6 */
302 #ifdef IPX
303 	case AF_IPX:
304 		ifq = &ipxintrq;
305 		isr = NETISR_IPX;
306 		break;
307 #endif
308 #ifdef NETATALK
309 	case AF_APPLETALK:
310 		ifq = &atintrq2;
311 		isr = NETISR_ATALK;
312 		break;
313 #endif /* NETATALK */
314 	default:
315 		printf("%s: can't handle af%d\n", ifp->if_xname,
316 			dst->sa_family);
317 		m_freem(m);
318 		return (EAFNOSUPPORT);
319 	}
320 	s = splnet();
321 	if (IF_QFULL(ifq)) {
322 		IF_DROP(ifq);
323 		m_freem(m);
324 		splx(s);
325 		return (ENOBUFS);
326 	}
327 	IF_ENQUEUE(ifq, m);
328 	schednetisr(isr);
329 	ifp->if_ipackets++;
330 	ifp->if_ibytes += m->m_pkthdr.len;
331 	splx(s);
332 	return (0);
333 }
334 
335 #ifdef ALTQ
336 static void
337 lo_altqstart(ifp)
338 	struct ifnet *ifp;
339 {
340 	struct ifqueue *ifq;
341 	struct mbuf *m;
342 	int32_t af, *afp;
343 	int s, isr;
344 
345 	while (1) {
346 		s = splnet();
347 		IFQ_DEQUEUE(&ifp->if_snd, m);
348 		splx(s);
349 		if (m == NULL)
350 			return;
351 
352 		afp = mtod(m, int32_t *);
353 		af = *afp;
354 		m_adj(m, sizeof(int32_t));
355 
356 		switch (af) {
357 #ifdef INET
358 		case AF_INET:
359 			ifq = &ipintrq;
360 			isr = NETISR_IP;
361 			break;
362 #endif
363 #ifdef INET6
364 		case AF_INET6:
365 			m->m_flags |= M_LOOP;
366 			ifq = &ip6intrq;
367 			isr = NETISR_IPV6;
368 			break;
369 #endif
370 #ifdef IPX
371 		case AF_IPX:
372 			ifq = &ipxintrq;
373 			isr = NETISR_IPX;
374 			break;
375 #endif
376 #ifdef NETATALK
377 		case AF_APPLETALK:
378 			ifq = &atintrq2;
379 			isr = NETISR_ATALK;
380 			break;
381 #endif /* NETATALK */
382 		default:
383 			printf("lo_altqstart: can't handle af%d\n", af);
384 			m_freem(m);
385 			return;
386 		}
387 
388 		s = splnet();
389 		if (IF_QFULL(ifq)) {
390 			IF_DROP(ifq);
391 			m_freem(m);
392 			splx(s);
393 			return;
394 		}
395 		IF_ENQUEUE(ifq, m);
396 		schednetisr(isr);
397 		ifp->if_ipackets++;
398 		ifp->if_ibytes += m->m_pkthdr.len;
399 		splx(s);
400 	}
401 }
402 #endif /* ALTQ */
403 
404 /* ARGSUSED */
405 void
406 lortrequest(cmd, rt, info)
407 	int cmd;
408 	struct rtentry *rt;
409 	struct rt_addrinfo *info;
410 {
411 
412 	if (rt)
413 		rt->rt_rmx.rmx_mtu = LOMTU;
414 }
415 
416 /*
417  * Process an ioctl request.
418  */
419 /* ARGSUSED */
420 int
421 loioctl(ifp, cmd, data)
422 	struct ifnet *ifp;
423 	u_long cmd;
424 	caddr_t data;
425 {
426 	struct ifaddr *ifa;
427 	struct ifreq *ifr;
428 	int error = 0;
429 
430 	switch (cmd) {
431 
432 	case SIOCSIFADDR:
433 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
434 		ifa = (struct ifaddr *)data;
435 		if (ifa != 0)
436 			ifa->ifa_rtrequest = lortrequest;
437 		/*
438 		 * Everything else is done at a higher level.
439 		 */
440 		break;
441 
442 	case SIOCADDMULTI:
443 	case SIOCDELMULTI:
444 		ifr = (struct ifreq *)data;
445 		if (ifr == 0) {
446 			error = EAFNOSUPPORT;		/* XXX */
447 			break;
448 		}
449 		switch (ifr->ifr_addr.sa_family) {
450 
451 #ifdef INET
452 		case AF_INET:
453 			break;
454 #endif
455 #ifdef INET6
456 		case AF_INET6:
457 			break;
458 #endif /* INET6 */
459 
460 		default:
461 			error = EAFNOSUPPORT;
462 			break;
463 		}
464 		break;
465 
466 	case SIOCSIFMTU:
467 		ifr = (struct ifreq *)data;
468 		ifp->if_mtu = ifr->ifr_mtu;
469 		break;
470 
471 	default:
472 		error = EINVAL;
473 	}
474 	return (error);
475 }
476