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