1 /*	$OpenBSD: if_loop.c,v 1.32 2003/12/16 20:33:25 markus 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 NS
142 #include <netns/ns.h>
143 #include <netns/ns_if.h>
144 #endif
145 
146 #ifdef IPX
147 #include <netipx/ipx.h>
148 #include <netipx/ipx_if.h>
149 #endif
150 
151 #ifdef ISO
152 #include <netiso/iso.h>
153 #include <netiso/iso_var.h>
154 #endif
155 
156 #ifdef NETATALK
157 #include <netinet/if_ether.h>
158 #include <netatalk/at.h>
159 #include <netatalk/at_var.h>
160 #endif
161 
162 #if NBPFILTER > 0
163 #include <net/bpf.h>
164 #endif
165 
166 #if defined(LARGE_LOMTU)
167 #define LOMTU	(131072 +  MHLEN + MLEN)
168 #else
169 #define	LOMTU	(32768 +  MHLEN + MLEN)
170 #endif
171 
172 #ifdef ALTQ
173 static void lo_altqstart(struct ifnet *);
174 #endif
175 
176 int	loop_clone_create(struct if_clone *, int);
177 int	loop_clone_destroy(struct ifnet *);
178 
179 struct if_clone loop_cloner =
180     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
181 
182 /* ARGSUSED */
183 void
loopattach(n)184 loopattach(n)
185 	int n;
186 {
187 	(void) loop_clone_create(&loop_cloner, 0);
188 	if_clone_attach(&loop_cloner);
189 }
190 
191 int
loop_clone_create(ifc,unit)192 loop_clone_create(ifc, unit)
193 	struct if_clone *ifc;
194 	int unit;
195 {
196 	struct ifnet *ifp;
197 
198 	MALLOC(ifp, struct ifnet *, sizeof(*ifp), M_DEVBUF, M_NOWAIT);
199 	if (ifp == NULL)
200 		return (ENOMEM);
201 	bzero(ifp, sizeof(struct ifnet));
202 
203 	snprintf(ifp->if_xname, sizeof ifp->if_xname, "lo%d", unit);
204 	ifp->if_softc = NULL;
205 	ifp->if_mtu = LOMTU;
206 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST;
207 	ifp->if_ioctl = loioctl;
208 	ifp->if_output = looutput;
209 	ifp->if_type = IFT_LOOP;
210 	ifp->if_hdrlen = sizeof(u_int32_t);
211 	ifp->if_addrlen = 0;
212 	IFQ_SET_READY(&ifp->if_snd);
213 #ifdef ALTQ
214 	ifp->if_start = lo_altqstart;
215 #endif
216 	if (unit == 0) {
217 		lo0ifp = ifp;
218 		if_attachhead(ifp);
219 	} else
220 		if_attach(ifp);
221 	if_alloc_sadl(ifp);
222 #if NBPFILTER > 0
223 	bpfattach(&ifp->if_bpf, ifp, DLT_LOOP, sizeof(u_int32_t));
224 #endif
225 	return (0);
226 }
227 
228 int
loop_clone_destroy(ifp)229 loop_clone_destroy(ifp)
230 	struct ifnet *ifp;
231 {
232 	if (ifp == lo0ifp)
233 		return (EPERM);
234 
235 #if NBPFILTER > 0
236 	bpfdetach(ifp);
237 #endif
238 	if_detach(ifp);
239 
240 	free(ifp, M_DEVBUF);
241 	return (0);
242 }
243 
244 int
looutput(ifp,m,dst,rt)245 looutput(ifp, m, dst, rt)
246 	struct ifnet *ifp;
247 	struct mbuf *m;
248 	struct sockaddr *dst;
249 	struct rtentry *rt;
250 {
251 	int s, isr;
252 	struct ifqueue *ifq = 0;
253 
254 	if ((m->m_flags & M_PKTHDR) == 0)
255 		panic("looutput: no header mbuf");
256 #if NBPFILTER > 0
257 	/*
258 	 * only send packets to bpf if they are real loopback packets;
259 	 * looutput() is also called for SIMPLEX interfaces to duplicate
260 	 * packets for local use. But don't dup them to bpf.
261 	 */
262 	if (ifp->if_bpf && (ifp->if_flags&IFF_LOOPBACK)) {
263 		/*
264 		 * We need to prepend the address family as
265 		 * a four byte field.  Cons up a dummy header
266 		 * to pacify bpf.  This is safe because bpf
267 		 * will only read from the mbuf (i.e., it won't
268 		 * try to free it or keep a pointer to it).
269 		 */
270 		struct mbuf m0;
271 		u_int32_t af = htonl(dst->sa_family);
272 
273 		m0.m_flags = 0;
274 		m0.m_next = m;
275 		m0.m_len = sizeof(af);
276 		m0.m_data = (char *)&af;
277 
278 		bpf_mtap(ifp->if_bpf, &m0);
279 	}
280 #endif
281 	m->m_pkthdr.rcvif = ifp;
282 
283 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
284 		m_freem(m);
285 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
286 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
287 	}
288 
289 	ifp->if_opackets++;
290 	ifp->if_obytes += m->m_pkthdr.len;
291 #ifdef ALTQ
292 	/*
293 	 * altq for loop is just for debugging.
294 	 * only used when called for loop interface (not for
295 	 * a simplex interface).
296 	 */
297 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd))
298 	    && ifp->if_start == lo_altqstart) {
299 		int32_t *afp;
300 	        int error;
301 
302 		M_PREPEND(m, sizeof(int32_t), M_DONTWAIT);
303 		if (m == 0)
304 			return (ENOBUFS);
305 		afp = mtod(m, int32_t *);
306 		*afp = (int32_t)dst->sa_family;
307 
308 	        s = splimp();
309 		IFQ_ENQUEUE(&ifp->if_snd, m, NULL, error);
310 		(*ifp->if_start)(ifp);
311 		splx(s);
312 		return (error);
313 	}
314 #endif /* ALTQ */
315 	switch (dst->sa_family) {
316 
317 #ifdef INET
318 	case AF_INET:
319 		ifq = &ipintrq;
320 		isr = NETISR_IP;
321 		break;
322 #endif
323 #ifdef INET6
324 	case AF_INET6:
325 		ifq = &ip6intrq;
326 		isr = NETISR_IPV6;
327 		break;
328 #endif /* INET6 */
329 #ifdef NS
330 	case AF_NS:
331 		ifq = &nsintrq;
332 		isr = NETISR_NS;
333 		break;
334 #endif
335 #ifdef IPX
336 	case AF_IPX:
337 		ifq = &ipxintrq;
338 		isr = NETISR_IPX;
339 		break;
340 #endif
341 #ifdef ISO
342 	case AF_ISO:
343 		ifq = &clnlintrq;
344 		isr = NETISR_ISO;
345 		break;
346 #endif
347 #ifdef NETATALK
348 	case AF_APPLETALK:
349 		ifq = &atintrq2;
350 		isr = NETISR_ATALK;
351 		break;
352 #endif /* NETATALK */
353 	default:
354 		printf("%s: can't handle af%d\n", ifp->if_xname,
355 			dst->sa_family);
356 		m_freem(m);
357 		return (EAFNOSUPPORT);
358 	}
359 	s = splimp();
360 	if (IF_QFULL(ifq)) {
361 		IF_DROP(ifq);
362 		m_freem(m);
363 		splx(s);
364 		return (ENOBUFS);
365 	}
366 	IF_ENQUEUE(ifq, m);
367 	schednetisr_virtual(isr);
368 	ifp->if_ipackets++;
369 	ifp->if_ibytes += m->m_pkthdr.len;
370 	splx(s);
371 	return (0);
372 }
373 
374 #ifdef ALTQ
375 static void
lo_altqstart(ifp)376 lo_altqstart(ifp)
377 	struct ifnet *ifp;
378 {
379 	struct ifqueue *ifq;
380 	struct mbuf *m;
381 	int32_t af, *afp;
382 	int s, isr;
383 
384 	while (1) {
385 		s = splimp();
386 		IFQ_DEQUEUE(&ifp->if_snd, m);
387 		splx(s);
388 		if (m == NULL)
389 			return;
390 
391 		afp = mtod(m, int32_t *);
392 		af = *afp;
393 		m_adj(m, sizeof(int32_t));
394 
395 		switch (af) {
396 #ifdef INET
397 		case AF_INET:
398 			ifq = &ipintrq;
399 			isr = NETISR_IP;
400 			break;
401 #endif
402 #ifdef INET6
403 		case AF_INET6:
404 			m->m_flags |= M_LOOP;
405 			ifq = &ip6intrq;
406 			isr = NETISR_IPV6;
407 			break;
408 #endif
409 #ifdef IPX
410 		case AF_IPX:
411 			ifq = &ipxintrq;
412 			isr = NETISR_IPX;
413 			break;
414 #endif
415 #ifdef NS
416 		case AF_NS:
417 			ifq = &nsintrq;
418 			isr = NETISR_NS;
419 			break;
420 #endif
421 #ifdef ISO
422 		case AF_ISO:
423 			ifq = &clnlintrq;
424 			isr = NETISR_ISO;
425 			break;
426 #endif
427 #ifdef NETATALK
428 		case AF_APPLETALK:
429 			ifq = &atintrq2;
430 			isr = NETISR_ATALK;
431 			break;
432 #endif /* NETATALK */
433 		default:
434 			printf("lo_altqstart: can't handle af%d\n", af);
435 			m_freem(m);
436 			return;
437 		}
438 
439 		s = splimp();
440 		if (IF_QFULL(ifq)) {
441 			IF_DROP(ifq);
442 			m_freem(m);
443 			splx(s);
444 			return;
445 		}
446 		IF_ENQUEUE(ifq, m);
447 		schednetisr_virtual(isr);
448 		ifp->if_ipackets++;
449 		ifp->if_ibytes += m->m_pkthdr.len;
450 		splx(s);
451 	}
452 }
453 #endif /* ALTQ */
454 
455 /* ARGSUSED */
456 void
lortrequest(cmd,rt,info)457 lortrequest(cmd, rt, info)
458 	int cmd;
459 	struct rtentry *rt;
460 	struct rt_addrinfo *info;
461 {
462 
463 	if (rt)
464 		rt->rt_rmx.rmx_mtu = LOMTU;
465 }
466 
467 /*
468  * Process an ioctl request.
469  */
470 /* ARGSUSED */
471 int
loioctl(ifp,cmd,data)472 loioctl(ifp, cmd, data)
473 	struct ifnet *ifp;
474 	u_long cmd;
475 	caddr_t data;
476 {
477 	struct ifaddr *ifa;
478 	struct ifreq *ifr;
479 	int error = 0;
480 
481 	switch (cmd) {
482 
483 	case SIOCSIFADDR:
484 		ifp->if_flags |= IFF_UP | IFF_RUNNING;
485 		ifa = (struct ifaddr *)data;
486 		if (ifa != 0 /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
487 			ifa->ifa_rtrequest = lortrequest;
488 		/*
489 		 * Everything else is done at a higher level.
490 		 */
491 		break;
492 
493 	case SIOCADDMULTI:
494 	case SIOCDELMULTI:
495 		ifr = (struct ifreq *)data;
496 		if (ifr == 0) {
497 			error = EAFNOSUPPORT;		/* XXX */
498 			break;
499 		}
500 		switch (ifr->ifr_addr.sa_family) {
501 
502 #ifdef INET
503 		case AF_INET:
504 			break;
505 #endif
506 #ifdef INET6
507 		case AF_INET6:
508 			break;
509 #endif /* INET6 */
510 
511 		default:
512 			error = EAFNOSUPPORT;
513 			break;
514 		}
515 		break;
516 
517 	case SIOCSIFMTU:
518 		ifr = (struct ifreq *)data;
519 		ifp->if_mtu = ifr->ifr_mtu;
520 		break;
521 
522 	default:
523 		error = EINVAL;
524 	}
525 	return (error);
526 }
527