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