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