xref: /netbsd/sys/net/if_loop.c (revision 6550d01e)
1 /*	$NetBSD: if_loop.c,v 1.72 2010/04/05 07:22:23 joerg 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.72 2010/04/05 07:22:23 joerg Exp $");
69 
70 #include "opt_inet.h"
71 #include "opt_atalk.h"
72 #include "opt_iso.h"
73 #include "opt_ipx.h"
74 #include "opt_mbuftrace.h"
75 
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/mbuf.h>
81 #include <sys/socket.h>
82 #include <sys/errno.h>
83 #include <sys/ioctl.h>
84 #include <sys/time.h>
85 
86 #include <sys/cpu.h>
87 
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
92 
93 #ifdef	INET
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #endif
99 
100 #ifdef INET6
101 #ifndef INET
102 #include <netinet/in.h>
103 #endif
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip6.h>
106 #endif
107 
108 
109 #ifdef IPX
110 #include <netipx/ipx.h>
111 #include <netipx/ipx_if.h>
112 #endif
113 
114 #ifdef ISO
115 #include <netiso/iso.h>
116 #include <netiso/iso_var.h>
117 #endif
118 
119 #ifdef NETATALK
120 #include <netatalk/at.h>
121 #include <netatalk/at_var.h>
122 #endif
123 
124 #include <net/bpf.h>
125 
126 #if defined(LARGE_LOMTU)
127 #define LOMTU	(131072 +  MHLEN + MLEN)
128 #define LOMTU_MAX LOMTU
129 #else
130 #define	LOMTU	(32768 +  MHLEN + MLEN)
131 #define	LOMTU_MAX	(65536 +  MHLEN + MLEN)
132 #endif
133 
134 #ifdef ALTQ
135 static void	lostart(struct ifnet *);
136 #endif
137 
138 static int	loop_clone_create(struct if_clone *, int);
139 static int	loop_clone_destroy(struct ifnet *);
140 
141 static struct if_clone loop_cloner =
142     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
143 
144 void
145 loopattach(int n)
146 {
147 
148 	(void)loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
149 	if_clone_attach(&loop_cloner);
150 }
151 
152 static int
153 loop_clone_create(struct if_clone *ifc, int unit)
154 {
155 	struct ifnet *ifp;
156 
157 	ifp = if_alloc(IFT_LOOP);
158 
159 	if_initname(ifp, ifc->ifc_name, unit);
160 
161 	ifp->if_mtu = LOMTU;
162 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST | IFF_RUNNING;
163 	ifp->if_ioctl = loioctl;
164 	ifp->if_output = looutput;
165 #ifdef ALTQ
166 	ifp->if_start = lostart;
167 #endif
168 	ifp->if_type = IFT_LOOP;
169 	ifp->if_hdrlen = 0;
170 	ifp->if_addrlen = 0;
171 	ifp->if_dlt = DLT_NULL;
172 	IFQ_SET_READY(&ifp->if_snd);
173 	if (unit == 0)
174 		lo0ifp = ifp;
175 	if_attach(ifp);
176 	if_alloc_sadl(ifp);
177 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
178 #ifdef MBUFTRACE
179 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
180 	    M_WAITOK | M_ZERO);
181 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
182 	    sizeof(ifp->if_mowner->mo_name));
183 	MOWNER_ATTACH(ifp->if_mowner);
184 #endif
185 
186 	return (0);
187 }
188 
189 static int
190 loop_clone_destroy(struct ifnet *ifp)
191 {
192 
193 	if (ifp == lo0ifp)
194 		return (EPERM);
195 
196 #ifdef MBUFTRACE
197 	MOWNER_DETACH(ifp->if_mowner);
198 	free(ifp->if_mowner, M_DEVBUF);
199 #endif
200 
201 	bpf_detach(ifp);
202 	if_detach(ifp);
203 
204 	free(ifp, M_DEVBUF);
205 
206 	return (0);
207 }
208 
209 int
210 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
211     struct rtentry *rt)
212 {
213 	int s, isr;
214 	struct ifqueue *ifq = NULL;
215 
216 	MCLAIM(m, ifp->if_mowner);
217 	if ((m->m_flags & M_PKTHDR) == 0)
218 		panic("looutput: no header mbuf");
219 	if (ifp->if_flags & IFF_LOOPBACK)
220 		bpf_mtap_af(ifp, dst->sa_family, m);
221 	m->m_pkthdr.rcvif = ifp;
222 
223 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
224 		m_freem(m);
225 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
226 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
227 	}
228 
229 	ifp->if_opackets++;
230 	ifp->if_obytes += m->m_pkthdr.len;
231 
232 #ifdef ALTQ
233 	/*
234 	 * ALTQ on the loopback interface is just for debugging.  It's
235 	 * used only for loopback interfaces, not for a simplex interface.
236 	 */
237 	if ((ALTQ_IS_ENABLED(&ifp->if_snd) || TBR_IS_ENABLED(&ifp->if_snd)) &&
238 	    ifp->if_start == lostart) {
239 		struct altq_pktattr pktattr;
240 		int error;
241 
242 		/*
243 		 * If the queueing discipline needs packet classification,
244 		 * do it before prepending the link headers.
245 		 */
246 		IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
247 
248 		M_PREPEND(m, sizeof(uint32_t), M_DONTWAIT);
249 		if (m == NULL)
250 			return (ENOBUFS);
251 		*(mtod(m, uint32_t *)) = dst->sa_family;
252 
253 		s = splnet();
254 		IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
255 		(*ifp->if_start)(ifp);
256 		splx(s);
257 		return (error);
258 	}
259 #endif /* ALTQ */
260 
261 	m_tag_delete_nonpersistent(m);
262 
263 	switch (dst->sa_family) {
264 
265 #ifdef INET
266 	case AF_INET:
267 		ifq = &ipintrq;
268 		isr = NETISR_IP;
269 		break;
270 #endif
271 #ifdef INET6
272 	case AF_INET6:
273 		m->m_flags |= M_LOOP;
274 		ifq = &ip6intrq;
275 		isr = NETISR_IPV6;
276 		break;
277 #endif
278 #ifdef ISO
279 	case AF_ISO:
280 		ifq = &clnlintrq;
281 		isr = NETISR_ISO;
282 		break;
283 #endif
284 #ifdef IPX
285 	case AF_IPX:
286 		ifq = &ipxintrq;
287 		isr = NETISR_IPX;
288 		break;
289 #endif
290 #ifdef NETATALK
291 	case AF_APPLETALK:
292 	        ifq = &atintrq2;
293 		isr = NETISR_ATALK;
294 		break;
295 #endif
296 	default:
297 		printf("%s: can't handle af%d\n", ifp->if_xname,
298 		    dst->sa_family);
299 		m_freem(m);
300 		return (EAFNOSUPPORT);
301 	}
302 	s = splnet();
303 	if (IF_QFULL(ifq)) {
304 		IF_DROP(ifq);
305 		m_freem(m);
306 		splx(s);
307 		return (ENOBUFS);
308 	}
309 	IF_ENQUEUE(ifq, m);
310 	schednetisr(isr);
311 	ifp->if_ipackets++;
312 	ifp->if_ibytes += m->m_pkthdr.len;
313 	splx(s);
314 	return (0);
315 }
316 
317 #ifdef ALTQ
318 static void
319 lostart(struct ifnet *ifp)
320 {
321 	struct ifqueue *ifq;
322 	struct mbuf *m;
323 	uint32_t af;
324 	int s, isr;
325 
326 	for (;;) {
327 		IFQ_DEQUEUE(&ifp->if_snd, m);
328 		if (m == NULL)
329 			return;
330 
331 		af = *(mtod(m, uint32_t *));
332 		m_adj(m, sizeof(uint32_t));
333 
334 		switch (af) {
335 #ifdef INET
336 		case AF_INET:
337 			ifq = &ipintrq;
338 			isr = NETISR_IP;
339 			break;
340 #endif
341 #ifdef INET6
342 		case AF_INET6:
343 			m->m_flags |= M_LOOP;
344 			ifq = &ip6intrq;
345 			isr = NETISR_IPV6;
346 			break;
347 #endif
348 #ifdef IPX
349 		case AF_IPX:
350 			ifq = &ipxintrq;
351 			isr = NETISR_IPX;
352 			break;
353 #endif
354 #ifdef ISO
355 		case AF_ISO:
356 			ifq = &clnlintrq;
357 			isr = NETISR_ISO;
358 			break;
359 #endif
360 #ifdef NETATALK
361 		case AF_APPLETALK:
362 			ifq = &atintrq2;
363 			isr = NETISR_ATALK;
364 			break;
365 #endif
366 		default:
367 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
368 			m_freem(m);
369 			return;
370 		}
371 
372 		s = splnet();
373 		if (IF_QFULL(ifq)) {
374 			IF_DROP(ifq);
375 			splx(s);
376 			m_freem(m);
377 			return;
378 		}
379 		IF_ENQUEUE(ifq, m);
380 		schednetisr(isr);
381 		ifp->if_ipackets++;
382 		ifp->if_ibytes += m->m_pkthdr.len;
383 		splx(s);
384 	}
385 }
386 #endif /* ALTQ */
387 
388 /* ARGSUSED */
389 void
390 lortrequest(int cmd, struct rtentry *rt,
391     const struct rt_addrinfo *info)
392 {
393 
394 	if (rt)
395 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
396 }
397 
398 /*
399  * Process an ioctl request.
400  */
401 /* ARGSUSED */
402 int
403 loioctl(struct ifnet *ifp, u_long cmd, void *data)
404 {
405 	struct ifaddr *ifa;
406 	struct ifreq *ifr = data;
407 	int error = 0;
408 
409 	switch (cmd) {
410 
411 	case SIOCINITIFADDR:
412 		ifp->if_flags |= IFF_UP;
413 		ifa = (struct ifaddr *)data;
414 		if (ifa != NULL /*&& ifa->ifa_addr->sa_family == AF_ISO*/)
415 			ifa->ifa_rtrequest = lortrequest;
416 		/*
417 		 * Everything else is done at a higher level.
418 		 */
419 		break;
420 
421 	case SIOCSIFMTU:
422 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
423 			error = EINVAL;
424 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
425 			/* XXX update rt mtu for AF_ISO? */
426 			error = 0;
427 		}
428 		break;
429 
430 	case SIOCADDMULTI:
431 	case SIOCDELMULTI:
432 		if (ifr == NULL) {
433 			error = EAFNOSUPPORT;		/* XXX */
434 			break;
435 		}
436 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
437 
438 #ifdef INET
439 		case AF_INET:
440 			break;
441 #endif
442 #ifdef INET6
443 		case AF_INET6:
444 			break;
445 #endif
446 
447 		default:
448 			error = EAFNOSUPPORT;
449 			break;
450 		}
451 		break;
452 
453 	default:
454 		error = ifioctl_common(ifp, cmd, data);
455 	}
456 	return (error);
457 }
458