xref: /netbsd-src/sys/net/if_loop.c (revision 37afb7eb6895c833050f8bfb1d1bb2f99f332539)
1 /*	$NetBSD: if_loop.c,v 1.82 2015/05/25 08:29:01 ozaki-r 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.82 2015/05/25 08:29:01 ozaki-r Exp $");
69 
70 #include "opt_inet.h"
71 #include "opt_atalk.h"
72 #include "opt_mbuftrace.h"
73 #include "opt_mpls.h"
74 #include "opt_net_mpsafe.h"
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/mbuf.h>
80 #include <sys/socket.h>
81 #include <sys/errno.h>
82 #include <sys/ioctl.h>
83 #include <sys/time.h>
84 
85 #include <sys/cpu.h>
86 
87 #include <net/if.h>
88 #include <net/if_types.h>
89 #include <net/netisr.h>
90 #include <net/route.h>
91 
92 #ifdef	INET
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_offload.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 <netinet6/in6_offload.h>
106 #include <netinet/ip6.h>
107 #endif
108 
109 #ifdef MPLS
110 #include <netmpls/mpls.h>
111 #include <netmpls/mpls_var.h>
112 #endif
113 
114 #ifdef NETATALK
115 #include <netatalk/at.h>
116 #include <netatalk/at_var.h>
117 #endif
118 
119 #include <net/bpf.h>
120 
121 #if defined(LARGE_LOMTU)
122 #define LOMTU	(131072 +  MHLEN + MLEN)
123 #define LOMTU_MAX LOMTU
124 #else
125 #define	LOMTU	(32768 +  MHLEN + MLEN)
126 #define	LOMTU_MAX	(65536 +  MHLEN + MLEN)
127 #endif
128 
129 #ifdef ALTQ
130 static void	lostart(struct ifnet *);
131 #endif
132 
133 static int	loop_clone_create(struct if_clone *, int);
134 static int	loop_clone_destroy(struct ifnet *);
135 
136 static struct if_clone loop_cloner =
137     IF_CLONE_INITIALIZER("lo", loop_clone_create, loop_clone_destroy);
138 
139 void
140 loopattach(int n)
141 {
142 
143 	(void)loop_clone_create(&loop_cloner, 0);	/* lo0 always exists */
144 	if_clone_attach(&loop_cloner);
145 }
146 
147 static int
148 loop_clone_create(struct if_clone *ifc, int unit)
149 {
150 	struct ifnet *ifp;
151 
152 	ifp = if_alloc(IFT_LOOP);
153 
154 	if_initname(ifp, ifc->ifc_name, unit);
155 
156 	ifp->if_mtu = LOMTU;
157 	ifp->if_flags = IFF_LOOPBACK | IFF_MULTICAST | IFF_RUNNING;
158 	ifp->if_ioctl = loioctl;
159 	ifp->if_output = looutput;
160 #ifdef ALTQ
161 	ifp->if_start = lostart;
162 #endif
163 	ifp->if_type = IFT_LOOP;
164 	ifp->if_hdrlen = 0;
165 	ifp->if_addrlen = 0;
166 	ifp->if_dlt = DLT_NULL;
167 	IFQ_SET_READY(&ifp->if_snd);
168 	if (unit == 0)
169 		lo0ifp = ifp;
170 	if_attach(ifp);
171 	if_alloc_sadl(ifp);
172 	bpf_attach(ifp, DLT_NULL, sizeof(u_int));
173 #ifdef MBUFTRACE
174 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
175 	    M_WAITOK | M_ZERO);
176 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
177 	    sizeof(ifp->if_mowner->mo_name));
178 	MOWNER_ATTACH(ifp->if_mowner);
179 #endif
180 
181 	return (0);
182 }
183 
184 static int
185 loop_clone_destroy(struct ifnet *ifp)
186 {
187 
188 	if (ifp == lo0ifp)
189 		return (EPERM);
190 
191 #ifdef MBUFTRACE
192 	MOWNER_DETACH(ifp->if_mowner);
193 	free(ifp->if_mowner, M_DEVBUF);
194 #endif
195 
196 	bpf_detach(ifp);
197 	if_detach(ifp);
198 
199 	if_free(ifp);
200 
201 	return (0);
202 }
203 
204 int
205 looutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
206     struct rtentry *rt)
207 {
208 	pktqueue_t *pktq = NULL;
209 	struct ifqueue *ifq = NULL;
210 	int s, isr = -1;
211 	int csum_flags;
212 	size_t pktlen;
213 
214 	MCLAIM(m, ifp->if_mowner);
215 #ifndef NET_MPSAFE
216 	KASSERT(KERNEL_LOCKED_P());
217 #endif
218 
219 	if ((m->m_flags & M_PKTHDR) == 0)
220 		panic("looutput: no header mbuf");
221 	if (ifp->if_flags & IFF_LOOPBACK)
222 		bpf_mtap_af(ifp, dst->sa_family, m);
223 	m->m_pkthdr.rcvif = ifp;
224 
225 	if (rt && rt->rt_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
226 		m_freem(m);
227 		return (rt->rt_flags & RTF_BLACKHOLE ? 0 :
228 			rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
229 	}
230 
231 	pktlen = m->m_pkthdr.len;
232 	ifp->if_opackets++;
233 	ifp->if_obytes += pktlen;
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 	m_tag_delete_nonpersistent(m);
265 
266 #ifdef MPLS
267 	if (rt != NULL && rt_gettag(rt) != NULL &&
268 	    rt_gettag(rt)->sa_family == AF_MPLS &&
269 	    (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
270 		union mpls_shim msh;
271 		msh.s_addr = MPLS_GETSADDR(rt);
272 		if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
273 			ifq = &mplsintrq;
274 			isr = NETISR_MPLS;
275 		}
276 	}
277 	if (isr != NETISR_MPLS)
278 #endif
279 	switch (dst->sa_family) {
280 
281 #ifdef INET
282 	case AF_INET:
283 		csum_flags = m->m_pkthdr.csum_flags;
284 		KASSERT((csum_flags & ~(M_CSUM_IPv4|M_CSUM_UDPv4)) == 0);
285 		if (csum_flags != 0 && IN_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
286 			ip_undefer_csum(m, 0, csum_flags);
287 		}
288 		m->m_pkthdr.csum_flags = 0;
289 		pktq = ip_pktq;
290 		break;
291 #endif
292 #ifdef INET6
293 	case AF_INET6:
294 		csum_flags = m->m_pkthdr.csum_flags;
295 		KASSERT((csum_flags & ~M_CSUM_UDPv6) == 0);
296 		if (csum_flags != 0 &&
297 		    IN6_LOOPBACK_NEED_CHECKSUM(csum_flags)) {
298 			ip6_undefer_csum(m, 0, csum_flags);
299 		}
300 		m->m_pkthdr.csum_flags = 0;
301 		m->m_flags |= M_LOOP;
302 		pktq = ip6_pktq;
303 		break;
304 #endif
305 #ifdef NETATALK
306 	case AF_APPLETALK:
307 	        ifq = &atintrq2;
308 		isr = NETISR_ATALK;
309 		break;
310 #endif
311 	default:
312 		printf("%s: can't handle af%d\n", ifp->if_xname,
313 		    dst->sa_family);
314 		m_freem(m);
315 		return (EAFNOSUPPORT);
316 	}
317 
318 	s = splnet();
319 	if (__predict_true(pktq)) {
320 		int error = 0;
321 
322 		if (__predict_true(pktq_enqueue(pktq, m, 0))) {
323 			ifp->if_ipackets++;
324 			ifp->if_ibytes += pktlen;
325 		} else {
326 			m_freem(m);
327 			error = ENOBUFS;
328 		}
329 		splx(s);
330 		return error;
331 	}
332 	if (IF_QFULL(ifq)) {
333 		IF_DROP(ifq);
334 		m_freem(m);
335 		splx(s);
336 		return (ENOBUFS);
337 	}
338 	IF_ENQUEUE(ifq, m);
339 	schednetisr(isr);
340 	ifp->if_ipackets++;
341 	ifp->if_ibytes += m->m_pkthdr.len;
342 	splx(s);
343 	return (0);
344 }
345 
346 #ifdef ALTQ
347 static void
348 lostart(struct ifnet *ifp)
349 {
350 	for (;;) {
351 		pktqueue_t *pktq = NULL;
352 		struct ifqueue *ifq = NULL;
353 		struct mbuf *m;
354 		size_t pktlen;
355 		uint32_t af;
356 		int s, isr = 0;
357 
358 		IFQ_DEQUEUE(&ifp->if_snd, m);
359 		if (m == NULL)
360 			return;
361 
362 		af = *(mtod(m, uint32_t *));
363 		m_adj(m, sizeof(uint32_t));
364 
365 		switch (af) {
366 #ifdef INET
367 		case AF_INET:
368 			pktq = ip_pktq;
369 			break;
370 #endif
371 #ifdef INET6
372 		case AF_INET6:
373 			m->m_flags |= M_LOOP;
374 			pktq = ip6_pktq;
375 			break;
376 #endif
377 #ifdef NETATALK
378 		case AF_APPLETALK:
379 			ifq = &atintrq2;
380 			isr = NETISR_ATALK;
381 			break;
382 #endif
383 		default:
384 			printf("%s: can't handle af%d\n", ifp->if_xname, af);
385 			m_freem(m);
386 			return;
387 		}
388 		pktlen = m->m_pkthdr.len;
389 
390 		s = splnet();
391 		if (__predict_true(pktq)) {
392 			if (__predict_false(pktq_enqueue(pktq, m, 0))) {
393 				m_freem(m);
394 				splx(s);
395 				return;
396 			}
397 			ifp->if_ipackets++;
398 			ifp->if_ibytes += pktlen;
399 			splx(s);
400 			continue;
401 		}
402 		if (IF_QFULL(ifq)) {
403 			IF_DROP(ifq);
404 			splx(s);
405 			m_freem(m);
406 			return;
407 		}
408 		IF_ENQUEUE(ifq, m);
409 		schednetisr(isr);
410 		ifp->if_ipackets++;
411 		ifp->if_ibytes += pktlen;
412 		splx(s);
413 	}
414 }
415 #endif /* ALTQ */
416 
417 /* ARGSUSED */
418 void
419 lortrequest(int cmd, struct rtentry *rt,
420     const struct rt_addrinfo *info)
421 {
422 
423 	if (rt)
424 		rt->rt_rmx.rmx_mtu = lo0ifp->if_mtu;
425 }
426 
427 /*
428  * Process an ioctl request.
429  */
430 /* ARGSUSED */
431 int
432 loioctl(struct ifnet *ifp, u_long cmd, void *data)
433 {
434 	struct ifaddr *ifa;
435 	struct ifreq *ifr = data;
436 	int error = 0;
437 
438 	switch (cmd) {
439 
440 	case SIOCINITIFADDR:
441 		ifp->if_flags |= IFF_UP;
442 		ifa = (struct ifaddr *)data;
443 		if (ifa != NULL)
444 			ifa->ifa_rtrequest = lortrequest;
445 		/*
446 		 * Everything else is done at a higher level.
447 		 */
448 		break;
449 
450 	case SIOCSIFMTU:
451 		if ((unsigned)ifr->ifr_mtu > LOMTU_MAX)
452 			error = EINVAL;
453 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET){
454 			error = 0;
455 		}
456 		break;
457 
458 	case SIOCADDMULTI:
459 	case SIOCDELMULTI:
460 		if (ifr == NULL) {
461 			error = EAFNOSUPPORT;		/* XXX */
462 			break;
463 		}
464 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
465 
466 #ifdef INET
467 		case AF_INET:
468 			break;
469 #endif
470 #ifdef INET6
471 		case AF_INET6:
472 			break;
473 #endif
474 
475 		default:
476 			error = EAFNOSUPPORT;
477 			break;
478 		}
479 		break;
480 
481 	default:
482 		error = ifioctl_common(ifp, cmd, data);
483 	}
484 	return (error);
485 }
486