xref: /openbsd-src/sys/net/if_loop.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: if_loop.c,v 1.47 2012/04/14 09:39:47 yasuoka 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 MPLS
142 #include <netmpls/mpls.h>
143 #endif
144 
145 #if NBPFILTER > 0
146 #include <net/bpf.h>
147 #endif
148 
149 #if defined(LARGE_LOMTU)
150 #define LOMTU	(131072 +  MHLEN + MLEN)
151 #else
152 #define	LOMTU	(32768 +  MHLEN + MLEN)
153 #endif
154 
155 #ifdef ALTQ
156 static void lo_altqstart(struct ifnet *);
157 #endif
158 
159 int	loop_clone_create(struct if_clone *, int);
160 int	loop_clone_destroy(struct ifnet *);
161 
162 struct if_clone loop_cloner =
163     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
164 
165 /* ARGSUSED */
166 void
167 loopattach(n)
168 	int n;
169 {
170 	(void) loop_clone_create(&loop_cloner, 0);
171 	if_clone_attach(&loop_cloner);
172 }
173 
174 int
175 loop_clone_create(ifc, unit)
176 	struct if_clone *ifc;
177 	int unit;
178 {
179 	struct ifnet *ifp;
180 
181 	ifp = malloc(sizeof(*ifp), M_DEVBUF, M_NOWAIT|M_ZERO);
182 	if (ifp == NULL)
183 		return (ENOMEM);
184 
185 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
186 	ifp->if_softc = NULL;
187 	ifp->if_mtu = LOMTU;
188 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
189 	ifp->if_ioctl = loioctl;
190 	ifp->if_output = looutput;
191 	ifp->if_type = IFT_LOOP;
192 	ifp->if_hdrlen = sizeof(u_int32_t);
193 	ifp->if_addrlen = 0;
194 	IFQ_SET_READY(&ifp->if_snd);
195 #ifdef ALTQ
196 	ifp->if_start = lo_altqstart;
197 #endif
198 	if (unit == 0) {
199 		lo0ifp = ifp;
200 		if_attachhead(ifp);
201 		if_addgroup(lo0ifp, ifc->ifc_name);
202 	} else
203 		if_attach(ifp);
204 	if_alloc_sadl(ifp);
205 #if NBPFILTER > 0
206 	bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
207 #endif
208 	return (0);
209 }
210 
211 int
212 loop_clone_destroy(ifp)
213 	struct ifnet *ifp;
214 {
215 	if (ifp == lo0ifp)
216 		return (EPERM);
217 
218 	if_detach(ifp);
219 
220 	free(ifp, M_DEVBUF);
221 	return (0);
222 }
223 
224 int
225 looutput(ifp, m, dst, rt)
226 	struct ifnet *ifp;
227 	struct mbuf *m;
228 	struct sockaddr *dst;
229 	struct rtentry *rt;
230 {
231 	int s, isr;
232 	struct ifqueue *ifq = 0;
233 
234 	if ((m->m_flags & M_PKTHDR) == 0)
235 		panic("looutput: no header mbuf");
236 #if NBPFILTER > 0
237 	/*
238 	 * only send packets to bpf if they are real loopback packets;
239 	 * looutput() is also called for SIMPLEX interfaces to duplicate
240 	 * packets for local use. But don't dup them to bpf.
241 	 */
242 	if (ifp->if_bpf && (ifp->if_flags & IFF_LOOPBACK))
243 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m, BPF_DIRECTION_OUT);
244 #endif
245 	m->m_pkthdr.rcvif = ifp;
246 
247 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
248 		m_freem(m);
249 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
250 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
251 	}
252 
253 	ifp->if_opackets++;
254 	ifp->if_obytes += m->m_pkthdr.len;
255 #ifdef ALTQ
256 	/*
257 	 * altq for loop is just for debugging.
258 	 * only used when called for loop interface (not for
259 	 * a simplex interface).
260 	 */
261 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd))
262 	    && ifp->if_start == lo_altqstart) {
263 		int32_t *afp;
264 	        int error;
265 
266 		M_PREPEND(m, sizeof(int32_t), M_DONTWAIT);
267 		if (m == 0)
268 			return (ENOBUFS);
269 		afp = mtod(m, int32_t *);
270 		*afp = (int32_t)dst->sa_family;
271 
272 	        s = splnet();
273 		IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
274 		(*ifp->if_start)(ifp);
275 		splx(s);
276 		return (error);
277 	}
278 #endif /* ALTQ */
279 	switch (dst->sa_family) {
280 
281 #ifdef INET
282 	case AF_INET:
283 		ifq = &ipintrq;
284 		isr = NETISR_IP;
285 		break;
286 #endif
287 #ifdef INET6
288 	case AF_INET6:
289 		ifq = &ip6intrq;
290 		isr = NETISR_IPV6;
291 		break;
292 #endif /* INET6 */
293 #ifdef MPLS
294 	case AF_MPLS:
295 		ifq = &mplsintrq;
296 		isr = NETISR_MPLS;
297 		break;
298 #endif /* MPLS */
299 	default:
300 		printf("%s: can't handle af%d\n", ifp->if_xname,
301 			dst->sa_family);
302 		m_freem(m);
303 		return (EAFNOSUPPORT);
304 	}
305 	s = splnet();
306 	if (IF_QFULL(ifq)) {
307 		IF_DROP(ifq);
308 		m_freem(m);
309 		splx(s);
310 		return (ENOBUFS);
311 	}
312 	IF_ENQUEUE(ifq, m);
313 	schednetisr(isr);
314 	ifp->if_ipackets++;
315 	ifp->if_ibytes += m->m_pkthdr.len;
316 	splx(s);
317 	return (0);
318 }
319 
320 #ifdef ALTQ
321 static void
322 lo_altqstart(ifp)
323 	struct ifnet *ifp;
324 {
325 	struct ifqueue *ifq;
326 	struct mbuf *m;
327 	int32_t af, *afp;
328 	int s, isr;
329 
330 	while (1) {
331 		s = splnet();
332 		IFQ_DEQUEUE(&ifp->if_snd, m);
333 		splx(s);
334 		if (m == NULL)
335 			return;
336 
337 		afp = mtod(m, int32_t *);
338 		af = *afp;
339 		m_adj(m, sizeof(int32_t));
340 
341 		switch (af) {
342 #ifdef INET
343 		case AF_INET:
344 			ifq = &ipintrq;
345 			isr = NETISR_IP;
346 			break;
347 #endif
348 #ifdef INET6
349 		case AF_INET6:
350 			m->m_flags |= M_LOOP;
351 			ifq = &ip6intrq;
352 			isr = NETISR_IPV6;
353 			break;
354 #endif
355 #ifdef MPLS
356 		case AF_MPLS:
357 			ifq = &mplsintrq;
358 			isr = NETISR_MPLS;
359 			break;
360 #endif
361 		default:
362 			printf("lo_altqstart: can't handle af%d\n", af);
363 			m_freem(m);
364 			return;
365 		}
366 
367 		s = splnet();
368 		if (IF_QFULL(ifq)) {
369 			IF_DROP(ifq);
370 			m_freem(m);
371 			splx(s);
372 			return;
373 		}
374 		IF_ENQUEUE(ifq, m);
375 		schednetisr(isr);
376 		ifp->if_ipackets++;
377 		ifp->if_ibytes += m->m_pkthdr.len;
378 		splx(s);
379 	}
380 }
381 #endif /* ALTQ */
382 
383 /* ARGSUSED */
384 void
385 lortrequest(cmd, rt, info)
386 	int cmd;
387 	struct rtentry *rt;
388 	struct rt_addrinfo *info;
389 {
390 
391 	if (rt)
392 		rt->rt_rmx.rmx_mtu = LOMTU;
393 }
394 
395 /*
396  * Process an ioctl request.
397  */
398 /* ARGSUSED */
399 int
400 loioctl(ifp, cmd, data)
401 	struct ifnet *ifp;
402 	u_long cmd;
403 	caddr_t data;
404 {
405 	struct ifaddr *ifa;
406 	struct ifreq *ifr;
407 	int s, error = 0;
408 
409 	switch (cmd) {
410 
411 	case SIOCSIFADDR:
412 		s = splnet();
413 		ifp->if_flags |= IFF_RUNNING;
414 		if_up(ifp);		/* send up RTM_IFINFO */
415 		splx(s);
416 
417 		ifa = (struct ifaddr *)data;
418 		if (ifa != 0)
419 			ifa->ifa_rtrequest = lortrequest;
420 		/*
421 		 * Everything else is done at a higher level.
422 		 */
423 		break;
424 
425 	case SIOCADDMULTI:
426 	case SIOCDELMULTI:
427 		ifr = (struct ifreq *)data;
428 		if (ifr == 0) {
429 			error = EAFNOSUPPORT;		/* XXX */
430 			break;
431 		}
432 		switch (ifr->ifr_addr.sa_family) {
433 
434 #ifdef INET
435 		case AF_INET:
436 			break;
437 #endif
438 #ifdef INET6
439 		case AF_INET6:
440 			break;
441 #endif /* INET6 */
442 
443 		default:
444 			error = EAFNOSUPPORT;
445 			break;
446 		}
447 		break;
448 
449 	case SIOCSIFMTU:
450 		ifr = (struct ifreq *)data;
451 		ifp->if_mtu = ifr->ifr_mtu;
452 		break;
453 
454 	default:
455 		error = ENOTTY;
456 	}
457 	return (error);
458 }
459