xref: /netbsd-src/sys/net/if_tun.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: if_tun.c,v 1.141 2017/10/30 16:01:19 ozaki-r Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
5  * Nottingham University 1987.
6  *
7  * This source may be freely distributed, however I would be interested
8  * in any changes that are made.
9  *
10  * This driver takes packets off the IP i/f and hands them up to a
11  * user process to have its wicked way with. This driver has its
12  * roots in a similar driver written by Phil Cockcroft (formerly) at
13  * UCL. This driver is based much more on read/write/poll mode of
14  * operation though.
15  */
16 
17 /*
18  * tun - tunnel software network interface.
19  */
20 
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.141 2017/10/30 16:01:19 ozaki-r Exp $");
23 
24 #ifdef _KERNEL_OPT
25 #include "opt_inet.h"
26 #endif
27 
28 #include <sys/param.h>
29 
30 #include <sys/buf.h>
31 #include <sys/conf.h>
32 #include <sys/cpu.h>
33 #include <sys/device.h>
34 #include <sys/file.h>
35 #include <sys/ioctl.h>
36 #include <sys/kauth.h>
37 #include <sys/kmem.h>
38 #include <sys/lwp.h>
39 #include <sys/mbuf.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/poll.h>
43 #include <sys/select.h>
44 #include <sys/signalvar.h>
45 #include <sys/socket.h>
46 
47 #include <net/bpf.h>
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/if_inarp.h>
58 #endif
59 
60 #include <net/if_tun.h>
61 
62 #include "ioconf.h"
63 
64 #define TUNDEBUG	if (tundebug) printf
65 int	tundebug = 0;
66 
67 extern int ifqmaxlen;
68 
69 static LIST_HEAD(, tun_softc) tun_softc_list;
70 static LIST_HEAD(, tun_softc) tunz_softc_list;
71 static kmutex_t tun_softc_lock;
72 
73 static int	tun_ioctl(struct ifnet *, u_long, void *);
74 static int	tun_output(struct ifnet *, struct mbuf *,
75 			const struct sockaddr *, const struct rtentry *rt);
76 static int	tun_clone_create(struct if_clone *, int);
77 static int	tun_clone_destroy(struct ifnet *);
78 
79 static struct if_clone tun_cloner =
80     IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
81 
82 static void tunattach0(struct tun_softc *);
83 static void tun_enable(struct tun_softc *, const struct ifaddr *);
84 static void tun_i_softintr(void *);
85 static void tun_o_softintr(void *);
86 #ifdef ALTQ
87 static void tunstart(struct ifnet *);
88 #endif
89 static struct tun_softc *tun_find_unit(dev_t);
90 static struct tun_softc *tun_find_zunit(int);
91 
92 static dev_type_open(tunopen);
93 static dev_type_close(tunclose);
94 static dev_type_read(tunread);
95 static dev_type_write(tunwrite);
96 static dev_type_ioctl(tunioctl);
97 static dev_type_poll(tunpoll);
98 static dev_type_kqfilter(tunkqfilter);
99 
100 const struct cdevsw tun_cdevsw = {
101 	.d_open = tunopen,
102 	.d_close = tunclose,
103 	.d_read = tunread,
104 	.d_write = tunwrite,
105 	.d_ioctl = tunioctl,
106 	.d_stop = nostop,
107 	.d_tty = notty,
108 	.d_poll = tunpoll,
109 	.d_mmap = nommap,
110 	.d_kqfilter = tunkqfilter,
111 	.d_discard = nodiscard,
112 	.d_flag = D_OTHER | D_MPSAFE
113 };
114 
115 #ifdef _MODULE
116 devmajor_t tun_bmajor = -1, tun_cmajor = -1;
117 #endif
118 
119 void
120 tunattach(int unused)
121 {
122 
123 	/*
124 	 * Nothing to do here, initialization is handled by the
125 	 * module initialization code in tuninit() below).
126 	 */
127 }
128 
129 static void
130 tuninit(void)
131 {
132 
133 	mutex_init(&tun_softc_lock, MUTEX_DEFAULT, IPL_NET);
134 	LIST_INIT(&tun_softc_list);
135 	LIST_INIT(&tunz_softc_list);
136 	if_clone_attach(&tun_cloner);
137 #ifdef _MODULE
138 	devsw_attach("tun", NULL, &tun_bmajor, &tun_cdevsw, &tun_cmajor);
139 #endif
140 }
141 
142 static int
143 tundetach(void)
144 {
145 	int error = 0;
146 
147 	if (!LIST_EMPTY(&tun_softc_list) || !LIST_EMPTY(&tunz_softc_list))
148 		error = EBUSY;
149 
150 #ifdef _MODULE
151 	if (error == 0)
152 		error = devsw_detach(NULL, &tun_cdevsw);
153 #endif
154 	if (error == 0) {
155 		if_clone_detach(&tun_cloner);
156 		mutex_destroy(&tun_softc_lock);
157 	}
158 
159 	return error;
160 }
161 
162 /*
163  * Find driver instance from dev_t.
164  * Returns with tp locked (if found).
165  */
166 static struct tun_softc *
167 tun_find_unit(dev_t dev)
168 {
169 	struct tun_softc *tp;
170 	int unit = minor(dev);
171 
172 	mutex_enter(&tun_softc_lock);
173 	LIST_FOREACH(tp, &tun_softc_list, tun_list)
174 		if (unit == tp->tun_unit)
175 			break;
176 	if (tp)
177 		mutex_enter(&tp->tun_lock);
178 	mutex_exit(&tun_softc_lock);
179 
180 	return tp;
181 }
182 
183 /*
184  * Find zombie driver instance by unit number.
185  * Remove tp from list and return it unlocked (if found).
186  */
187 static struct tun_softc *
188 tun_find_zunit(int unit)
189 {
190 	struct tun_softc *tp;
191 
192 	mutex_enter(&tun_softc_lock);
193 	LIST_FOREACH(tp, &tunz_softc_list, tun_list)
194 		if (unit == tp->tun_unit)
195 			break;
196 	if (tp)
197 		LIST_REMOVE(tp, tun_list);
198 	mutex_exit(&tun_softc_lock);
199 	KASSERTMSG(!tp || (tp->tun_flags & (TUN_INITED|TUN_OPEN)) == TUN_OPEN,
200 	    "tun%d: inconsistent flags: %x", unit, tp->tun_flags);
201 
202 	return tp;
203 }
204 
205 static int
206 tun_clone_create(struct if_clone *ifc, int unit)
207 {
208 	struct tun_softc *tp;
209 
210 	if ((tp = tun_find_zunit(unit)) == NULL) {
211 		tp = kmem_zalloc(sizeof(*tp), KM_SLEEP);
212 
213 		tp->tun_unit = unit;
214 		mutex_init(&tp->tun_lock, MUTEX_DEFAULT, IPL_NET);
215 		cv_init(&tp->tun_cv, "tunread");
216 		selinit(&tp->tun_rsel);
217 		selinit(&tp->tun_wsel);
218 	} else {
219 		/* Revive tunnel instance; clear ifp part */
220 		(void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
221 	}
222 
223 	if_initname(&tp->tun_if, ifc->ifc_name, unit);
224 	tunattach0(tp);
225 	tp->tun_flags |= TUN_INITED;
226 	tp->tun_osih = softint_establish(SOFTINT_CLOCK, tun_o_softintr, tp);
227 	tp->tun_isih = softint_establish(SOFTINT_CLOCK, tun_i_softintr, tp);
228 
229 	mutex_enter(&tun_softc_lock);
230 	LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
231 	mutex_exit(&tun_softc_lock);
232 
233 	return 0;
234 }
235 
236 static void
237 tunattach0(struct tun_softc *tp)
238 {
239 	struct ifnet *ifp;
240 
241 	ifp = &tp->tun_if;
242 	ifp->if_softc = tp;
243 	ifp->if_mtu = TUNMTU;
244 	ifp->if_ioctl = tun_ioctl;
245 	ifp->if_output = tun_output;
246 #ifdef ALTQ
247 	ifp->if_start = tunstart;
248 #endif
249 	ifp->if_flags = IFF_POINTOPOINT;
250 	ifp->if_extflags = IFEF_NO_LINK_STATE_CHANGE;
251 	ifp->if_type = IFT_TUNNEL;
252 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
253 	ifp->if_collisions = 0;
254 	ifp->if_ierrors = 0;
255 	ifp->if_oerrors = 0;
256 	ifp->if_ipackets = 0;
257 	ifp->if_opackets = 0;
258 	ifp->if_ibytes   = 0;
259 	ifp->if_obytes   = 0;
260 	ifp->if_dlt = DLT_NULL;
261 	IFQ_SET_READY(&ifp->if_snd);
262 	if_attach(ifp);
263 	if_alloc_sadl(ifp);
264 	bpf_attach(ifp, DLT_NULL, sizeof(uint32_t));
265 }
266 
267 static int
268 tun_clone_destroy(struct ifnet *ifp)
269 {
270 	struct tun_softc *tp = (void *)ifp;
271 	bool zombie = false;
272 
273 	IF_PURGE(&ifp->if_snd);
274 	ifp->if_flags &= ~IFF_RUNNING;
275 
276 	mutex_enter(&tun_softc_lock);
277 	mutex_enter(&tp->tun_lock);
278 	LIST_REMOVE(tp, tun_list);
279 	if (tp->tun_flags & TUN_OPEN) {
280 		/* Hang on to storage until last close. */
281 		tp->tun_flags &= ~TUN_INITED;
282 		LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
283 		zombie = true;
284 	}
285 	mutex_exit(&tun_softc_lock);
286 
287 	if (tp->tun_flags & TUN_RWAIT) {
288 		tp->tun_flags &= ~TUN_RWAIT;
289 		cv_broadcast(&tp->tun_cv);
290 	}
291 	selnotify(&tp->tun_rsel, 0, 0);
292 
293 	mutex_exit(&tp->tun_lock);
294 
295 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
296 		fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
297 
298 	bpf_detach(ifp);
299 	if_detach(ifp);
300 
301 	if (!zombie) {
302 		seldestroy(&tp->tun_rsel);
303 		seldestroy(&tp->tun_wsel);
304 		softint_disestablish(tp->tun_osih);
305 		softint_disestablish(tp->tun_isih);
306 		mutex_destroy(&tp->tun_lock);
307 		cv_destroy(&tp->tun_cv);
308 		kmem_free(tp, sizeof(*tp));
309 	}
310 
311 	return 0;
312 }
313 
314 /*
315  * tunnel open - must be superuser & the device must be
316  * configured in
317  */
318 static int
319 tunopen(dev_t dev, int flag, int mode, struct lwp *l)
320 {
321 	struct ifnet	*ifp;
322 	struct tun_softc *tp;
323 	int	error;
324 
325 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE_TUN,
326 	    KAUTH_REQ_NETWORK_INTERFACE_TUN_ADD, NULL, NULL, NULL);
327 	if (error)
328 		return error;
329 
330 	tp = tun_find_unit(dev);
331 
332 	if (tp == NULL) {
333 		(void)tun_clone_create(&tun_cloner, minor(dev));
334 		tp = tun_find_unit(dev);
335 		if (tp == NULL) {
336 			return ENXIO;
337 		}
338 	}
339 
340 	if (tp->tun_flags & TUN_OPEN) {
341 		mutex_exit(&tp->tun_lock);
342 		return EBUSY;
343 	}
344 
345 	ifp = &tp->tun_if;
346 	tp->tun_flags |= TUN_OPEN;
347 	TUNDEBUG("%s: open\n", ifp->if_xname);
348 
349 	mutex_exit(&tp->tun_lock);
350 
351 	return error;
352 }
353 
354 /*
355  * tunclose - close the device - mark i/f down & delete
356  * routing info
357  */
358 int
359 tunclose(dev_t dev, int flag, int mode,
360     struct lwp *l)
361 {
362 	struct tun_softc *tp;
363 	struct ifnet	*ifp;
364 
365 	if ((tp = tun_find_zunit(minor(dev))) != NULL) {
366 		/* interface was "destroyed" before the close */
367 		seldestroy(&tp->tun_rsel);
368 		seldestroy(&tp->tun_wsel);
369 		softint_disestablish(tp->tun_osih);
370 		softint_disestablish(tp->tun_isih);
371 		mutex_destroy(&tp->tun_lock);
372 		kmem_free(tp, sizeof(*tp));
373 		return 0;
374 	}
375 
376 	if ((tp = tun_find_unit(dev)) == NULL)
377 		goto out_nolock;
378 
379 	ifp = &tp->tun_if;
380 
381 	tp->tun_flags &= ~TUN_OPEN;
382 
383 	tp->tun_pgid = 0;
384 	selnotify(&tp->tun_rsel, 0, 0);
385 
386 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
387 	mutex_exit(&tp->tun_lock);
388 
389 	/*
390 	 * junk all pending output
391 	 */
392 	IFQ_PURGE(&ifp->if_snd);
393 
394 	if (ifp->if_flags & IFF_UP) {
395 		if_down(ifp);
396 		if (ifp->if_flags & IFF_RUNNING) {
397 			/* find internet addresses and delete routes */
398 			struct ifaddr *ifa;
399 			IFADDR_READER_FOREACH(ifa, ifp) {
400 #if defined(INET) || defined(INET6)
401 				if (ifa->ifa_addr->sa_family == AF_INET ||
402 				    ifa->ifa_addr->sa_family == AF_INET6) {
403 					rtinit(ifa, (int)RTM_DELETE,
404 					       tp->tun_flags & TUN_DSTADDR
405 							? RTF_HOST
406 							: 0);
407 				}
408 #endif
409 			}
410 		}
411 	}
412 out_nolock:
413 	return 0;
414 }
415 
416 static void
417 tun_enable(struct tun_softc *tp, const struct ifaddr *ifa)
418 {
419 	struct ifnet	*ifp = &tp->tun_if;
420 
421 	TUNDEBUG("%s: %s\n", __func__, ifp->if_xname);
422 
423 	mutex_enter(&tp->tun_lock);
424 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
425 
426 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
427 
428 	switch (ifa->ifa_addr->sa_family) {
429 #ifdef INET
430 	case AF_INET: {
431 		struct sockaddr_in *sin;
432 
433 		sin = satosin(ifa->ifa_addr);
434 		if (sin && sin->sin_addr.s_addr)
435 			tp->tun_flags |= TUN_IASET;
436 
437 		if (ifp->if_flags & IFF_POINTOPOINT) {
438 			sin = satosin(ifa->ifa_dstaddr);
439 			if (sin && sin->sin_addr.s_addr)
440 				tp->tun_flags |= TUN_DSTADDR;
441 		}
442 		break;
443 	    }
444 #endif
445 #ifdef INET6
446 	case AF_INET6: {
447 		struct sockaddr_in6 *sin;
448 
449 		sin = satosin6(ifa->ifa_addr);
450 		if (!IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
451 			tp->tun_flags |= TUN_IASET;
452 
453 		if (ifp->if_flags & IFF_POINTOPOINT) {
454 			sin = satosin6(ifa->ifa_dstaddr);
455 			if (sin && !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr))
456 				tp->tun_flags |= TUN_DSTADDR;
457 		} else
458 			tp->tun_flags &= ~TUN_DSTADDR;
459 		break;
460 	    }
461 #endif /* INET6 */
462 	default:
463 		break;
464 	}
465 	mutex_exit(&tp->tun_lock);
466 }
467 
468 /*
469  * Process an ioctl request.
470  */
471 static int
472 tun_ioctl(struct ifnet *ifp, u_long cmd, void *data)
473 {
474 	struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
475 	struct ifreq *ifr = (struct ifreq *)data;
476 	struct ifaddr *ifa = (struct ifaddr *)data;
477 	int error = 0;
478 
479 	switch (cmd) {
480 	case SIOCINITIFADDR:
481 		tun_enable(tp, ifa);
482 		ifa->ifa_rtrequest = p2p_rtrequest;
483 		TUNDEBUG("%s: address set\n", ifp->if_xname);
484 		break;
485 	case SIOCSIFBRDADDR:
486 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
487 		break;
488 	case SIOCSIFMTU:
489 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
490 			error = EINVAL;
491 			break;
492 		}
493 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
494 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
495 			error = 0;
496 		break;
497 	case SIOCADDMULTI:
498 	case SIOCDELMULTI:
499 		if (ifr == NULL) {
500 	        	error = EAFNOSUPPORT;           /* XXX */
501 			break;
502 		}
503 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
504 #ifdef INET
505 		case AF_INET:
506 			break;
507 #endif
508 #ifdef INET6
509 		case AF_INET6:
510 			break;
511 #endif
512 		default:
513 			error = EAFNOSUPPORT;
514 			break;
515 		}
516 		break;
517 	default:
518 		error = ifioctl_common(ifp, cmd, data);
519 	}
520 
521 	return error;
522 }
523 
524 /*
525  * tun_output - queue packets from higher level ready to put out.
526  */
527 static int
528 tun_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
529     const struct rtentry *rt)
530 {
531 	struct tun_softc *tp = ifp->if_softc;
532 	int		error;
533 #if defined(INET) || defined(INET6)
534 	int		mlen;
535 	uint32_t	*af;
536 #endif
537 
538 	mutex_enter(&tp->tun_lock);
539 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
540 
541 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
542 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
543 			  tp->tun_flags);
544 		error = EHOSTDOWN;
545 		mutex_exit(&tp->tun_lock);
546 		goto out;
547 	}
548 	// XXXrmind
549 	mutex_exit(&tp->tun_lock);
550 
551 	/*
552 	 * if the queueing discipline needs packet classification,
553 	 * do it before prepending link headers.
554 	 */
555 	IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family);
556 
557 	bpf_mtap_af(ifp, dst->sa_family, m0);
558 
559 	switch(dst->sa_family) {
560 #ifdef INET6
561 	case AF_INET6:
562 #endif
563 #ifdef INET
564 	case AF_INET:
565 #endif
566 #if defined(INET) || defined(INET6)
567 		if (tp->tun_flags & TUN_PREPADDR) {
568 			/* Simple link-layer header */
569 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
570 			if (m0 == NULL) {
571 				IF_DROP(&ifp->if_snd);
572 				error = ENOBUFS;
573 				goto out;
574 			}
575 			memcpy(mtod(m0, char *), dst, dst->sa_len);
576 		}
577 
578 		if (tp->tun_flags & TUN_IFHEAD) {
579 			/* Prepend the address family */
580 			M_PREPEND(m0, sizeof(*af), M_DONTWAIT);
581 			if (m0 == NULL) {
582 				IF_DROP(&ifp->if_snd);
583 				error = ENOBUFS;
584 				goto out;
585 			}
586 			af = mtod(m0,uint32_t *);
587 			*af = htonl(dst->sa_family);
588 		} else {
589 #ifdef INET
590 			if (dst->sa_family != AF_INET)
591 #endif
592 			{
593 				error = EAFNOSUPPORT;
594 				goto out;
595 			}
596 		}
597 		/* FALLTHROUGH */
598 	case AF_UNSPEC:
599 		IFQ_ENQUEUE(&ifp->if_snd, m0, error);
600 		if (error) {
601 			ifp->if_collisions++;
602 			error = EAFNOSUPPORT;
603 			m0 = NULL;
604 			goto out;
605 		}
606 		mlen = m0->m_pkthdr.len;
607 		ifp->if_opackets++;
608 		ifp->if_obytes += mlen;
609 		break;
610 #endif
611 	default:
612 		error = EAFNOSUPPORT;
613 		goto out;
614 	}
615 
616 	mutex_enter(&tp->tun_lock);
617 	if (tp->tun_flags & TUN_RWAIT) {
618 		tp->tun_flags &= ~TUN_RWAIT;
619 		cv_broadcast(&tp->tun_cv);
620 	}
621 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
622 		softint_schedule(tp->tun_isih);
623 
624 	selnotify(&tp->tun_rsel, 0, 0);
625 
626 	mutex_exit(&tp->tun_lock);
627 out:
628 	if (error && m0) {
629 		m_freem(m0);
630 	}
631 	return 0;
632 }
633 
634 static void
635 tun_i_softintr(void *cookie)
636 {
637 	struct tun_softc *tp = cookie;
638 
639 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
640 		fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
641 		    NULL);
642 }
643 
644 static void
645 tun_o_softintr(void *cookie)
646 {
647 	struct tun_softc *tp = cookie;
648 
649 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
650 		fownsignal(tp->tun_pgid, SIGIO, POLL_OUT, POLLOUT|POLLWRNORM,
651 		    NULL);
652 }
653 
654 /*
655  * the cdevsw interface is now pretty minimal.
656  */
657 int
658 tunioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
659 {
660 	struct tun_softc *tp;
661 	int error = 0;
662 
663 	tp = tun_find_unit(dev);
664 
665 	/* interface was "destroyed" already */
666 	if (tp == NULL) {
667 		return ENXIO;
668 	}
669 
670 	switch (cmd) {
671 	case TUNSDEBUG:
672 		tundebug = *(int *)data;
673 		break;
674 
675 	case TUNGDEBUG:
676 		*(int *)data = tundebug;
677 		break;
678 
679 	case TUNSIFMODE:
680 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
681 		case IFF_POINTOPOINT:
682 		case IFF_BROADCAST:
683 			if (tp->tun_if.if_flags & IFF_UP) {
684 				error = EBUSY;
685 				goto out;
686 			}
687 			tp->tun_if.if_flags &=
688 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
689 			tp->tun_if.if_flags |= *(int *)data;
690 			break;
691 		default:
692 			error = EINVAL;
693 			goto out;
694 		}
695 		break;
696 
697 	case TUNSLMODE:
698 		if (*(int *)data) {
699 			tp->tun_flags |= TUN_PREPADDR;
700 			tp->tun_flags &= ~TUN_IFHEAD;
701 		} else
702 			tp->tun_flags &= ~TUN_PREPADDR;
703 		break;
704 
705 	case TUNSIFHEAD:
706 		if (*(int *)data) {
707 			tp->tun_flags |= TUN_IFHEAD;
708 			tp->tun_flags &= ~TUN_PREPADDR;
709 		} else
710 			tp->tun_flags &= ~TUN_IFHEAD;
711 		break;
712 
713 	case TUNGIFHEAD:
714 		*(int *)data = (tp->tun_flags & TUN_IFHEAD);
715 		break;
716 
717 	case FIONBIO:
718 		if (*(int *)data)
719 			tp->tun_flags |= TUN_NBIO;
720 		else
721 			tp->tun_flags &= ~TUN_NBIO;
722 		break;
723 
724 	case FIOASYNC:
725 		if (*(int *)data)
726 			tp->tun_flags |= TUN_ASYNC;
727 		else
728 			tp->tun_flags &= ~TUN_ASYNC;
729 		break;
730 
731 	case FIONREAD:
732 		if (tp->tun_if.if_snd.ifq_head)
733 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
734 		else
735 			*(int *)data = 0;
736 		break;
737 
738 	case TIOCSPGRP:
739 	case FIOSETOWN:
740 		error = fsetown(&tp->tun_pgid, cmd, data);
741 		break;
742 
743 	case TIOCGPGRP:
744 	case FIOGETOWN:
745 		error = fgetown(tp->tun_pgid, cmd, data);
746 		break;
747 
748 	default:
749 		error = ENOTTY;
750 	}
751 
752 out:
753 	mutex_exit(&tp->tun_lock);
754 
755 	return error;
756 }
757 
758 /*
759  * The cdevsw read interface - reads a packet at a time, or at
760  * least as much of a packet as can be read.
761  */
762 int
763 tunread(dev_t dev, struct uio *uio, int ioflag)
764 {
765 	struct tun_softc *tp;
766 	struct ifnet	*ifp;
767 	struct mbuf	*m, *m0;
768 	int		error = 0, len;
769 
770 	tp = tun_find_unit(dev);
771 
772 	/* interface was "destroyed" already */
773 	if (tp == NULL) {
774 		return ENXIO;
775 	}
776 
777 	ifp = &tp->tun_if;
778 
779 	TUNDEBUG ("%s: read\n", ifp->if_xname);
780 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
781 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
782 		error = EHOSTDOWN;
783 		goto out;
784 	}
785 
786 	tp->tun_flags &= ~TUN_RWAIT;
787 
788 	do {
789 		IFQ_DEQUEUE(&ifp->if_snd, m0);
790 		if (m0 == 0) {
791 			if (tp->tun_flags & TUN_NBIO) {
792 				error = EWOULDBLOCK;
793 				goto out;
794 			}
795 			tp->tun_flags |= TUN_RWAIT;
796 			if (cv_wait_sig(&tp->tun_cv, &tp->tun_lock)) {
797 				error = EINTR;
798 				goto out;
799 			}
800 		}
801 	} while (m0 == 0);
802 
803 	mutex_exit(&tp->tun_lock);
804 
805 	/* Copy the mbuf chain */
806 	while (m0 && uio->uio_resid > 0 && error == 0) {
807 		len = min(uio->uio_resid, m0->m_len);
808 		if (len != 0)
809 			error = uiomove(mtod(m0, void *), len, uio);
810 		m0 = m = m_free(m0);
811 	}
812 
813 	if (m0) {
814 		TUNDEBUG("Dropping mbuf\n");
815 		m_freem(m0);
816 	}
817 	if (error)
818 		ifp->if_ierrors++;
819 
820 	return error;
821 
822 out:
823 	mutex_exit(&tp->tun_lock);
824 
825 	return error;
826 }
827 
828 /*
829  * the cdevsw write interface - an atomic write is a packet - or else!
830  */
831 int
832 tunwrite(dev_t dev, struct uio *uio, int ioflag)
833 {
834 	struct tun_softc *tp;
835 	struct ifnet	*ifp;
836 	struct mbuf	*top, **mp, *m;
837 	pktqueue_t	*pktq;
838 	struct sockaddr	dst;
839 	int		error = 0, tlen, mlen;
840 	uint32_t	family;
841 
842 	tp = tun_find_unit(dev);
843 	if (tp == NULL) {
844 		/* Interface was "destroyed" already. */
845 		return ENXIO;
846 	}
847 
848 	/* Unlock until we've got the data */
849 	mutex_exit(&tp->tun_lock);
850 
851 	ifp = &tp->tun_if;
852 
853 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
854 
855 	if (tp->tun_flags & TUN_PREPADDR) {
856 		if (uio->uio_resid < sizeof(dst)) {
857 			error = EIO;
858 			goto out0;
859 		}
860 		error = uiomove((void *)&dst, sizeof(dst), uio);
861 		if (dst.sa_len > sizeof(dst)) {
862 			/* Duh.. */
863 			int n = dst.sa_len - sizeof(dst);
864 			while (n--) {
865 				char discard;
866 				error = uiomove(&discard, 1, uio);
867 				if (error) {
868 					goto out0;
869 				}
870 			}
871 		}
872 	} else if (tp->tun_flags & TUN_IFHEAD) {
873 		if (uio->uio_resid < sizeof(family)){
874 			error = EIO;
875 			goto out0;
876 		}
877 		error = uiomove((void *)&family, sizeof(family), uio);
878 		dst.sa_family = ntohl(family);
879 	} else {
880 #ifdef INET
881 		dst.sa_family = AF_INET;
882 #endif
883 	}
884 
885 	if (uio->uio_resid > TUNMTU) {
886 		TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
887 		    (unsigned long)uio->uio_resid);
888 		error = EIO;
889 		goto out0;
890 	}
891 
892 	switch (dst.sa_family) {
893 #ifdef INET
894 	case AF_INET:
895 		pktq = ip_pktq;
896 		break;
897 #endif
898 #ifdef INET6
899 	case AF_INET6:
900 		pktq = ip6_pktq;
901 		break;
902 #endif
903 	default:
904 		error = EAFNOSUPPORT;
905 		goto out0;
906 	}
907 
908 	tlen = uio->uio_resid;
909 
910 	/* get a header mbuf */
911 	MGETHDR(m, M_DONTWAIT, MT_DATA);
912 	if (m == NULL) {
913 		return ENOBUFS;
914 	}
915 	mlen = MHLEN;
916 
917 	top = NULL;
918 	mp = &top;
919 	while (error == 0 && uio->uio_resid > 0) {
920 		m->m_len = min(mlen, uio->uio_resid);
921 		error = uiomove(mtod(m, void *), m->m_len, uio);
922 		*mp = m;
923 		mp = &m->m_next;
924 		if (error == 0 && uio->uio_resid > 0) {
925 			MGET(m, M_DONTWAIT, MT_DATA);
926 			if (m == NULL) {
927 				error = ENOBUFS;
928 				break;
929 			}
930 			mlen = MLEN;
931 		}
932 	}
933 	if (error) {
934 		if (top != NULL)
935 			m_freem(top);
936 		ifp->if_ierrors++;
937 		goto out0;
938 	}
939 
940 	top->m_pkthdr.len = tlen;
941 	m_set_rcvif(top, ifp);
942 
943 	bpf_mtap_af(ifp, dst.sa_family, top);
944 
945 	mutex_enter(&tp->tun_lock);
946 	if ((tp->tun_flags & TUN_INITED) == 0) {
947 		/* Interface was destroyed */
948 		error = ENXIO;
949 		goto out;
950 	}
951 	if (__predict_false(!pktq_enqueue(pktq, top, 0))) {
952 		ifp->if_collisions++;
953 		mutex_exit(&tp->tun_lock);
954 		error = ENOBUFS;
955 		m_freem(top);
956 		goto out0;
957 	}
958 	ifp->if_ipackets++;
959 	ifp->if_ibytes += tlen;
960 out:
961 	mutex_exit(&tp->tun_lock);
962 out0:
963 	return error;
964 }
965 
966 #ifdef ALTQ
967 /*
968  * Start packet transmission on the interface.
969  * when the interface queue is rate-limited by ALTQ or TBR,
970  * if_start is needed to drain packets from the queue in order
971  * to notify readers when outgoing packets become ready.
972  */
973 static void
974 tunstart(struct ifnet *ifp)
975 {
976 	struct tun_softc *tp = ifp->if_softc;
977 
978 	if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
979 		return;
980 
981 	mutex_enter(&tp->tun_lock);
982 	if (!IF_IS_EMPTY(&ifp->if_snd)) {
983 		if (tp->tun_flags & TUN_RWAIT) {
984 			tp->tun_flags &= ~TUN_RWAIT;
985 			cv_broadcast(&tp->tun_cv);
986 		}
987 		if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
988 			softint_schedule(tp->tun_osih);
989 
990 		selnotify(&tp->tun_rsel, 0, 0);
991 	}
992 	mutex_exit(&tp->tun_lock);
993 }
994 #endif /* ALTQ */
995 /*
996  * tunpoll - the poll interface, this is only useful on reads
997  * really. The write detect always returns true, write never blocks
998  * anyway, it either accepts the packet or drops it.
999  */
1000 int
1001 tunpoll(dev_t dev, int events, struct lwp *l)
1002 {
1003 	struct tun_softc *tp;
1004 	struct ifnet	*ifp;
1005 	int revents = 0;
1006 
1007 	tp = tun_find_unit(dev);
1008 	if (tp == NULL) {
1009 		/* Interface was "destroyed" already. */
1010 		return 0;
1011 	}
1012 	ifp = &tp->tun_if;
1013 
1014 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1015 
1016 	if (events & (POLLIN | POLLRDNORM)) {
1017 		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1018 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1019 			    ifp->if_snd.ifq_len);
1020 			revents |= events & (POLLIN | POLLRDNORM);
1021 		} else {
1022 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1023 			selrecord(l, &tp->tun_rsel);
1024 		}
1025 	}
1026 
1027 	if (events & (POLLOUT | POLLWRNORM))
1028 		revents |= events & (POLLOUT | POLLWRNORM);
1029 
1030 	mutex_exit(&tp->tun_lock);
1031 
1032 	return revents;
1033 }
1034 
1035 static void
1036 filt_tunrdetach(struct knote *kn)
1037 {
1038 	struct tun_softc *tp = kn->kn_hook;
1039 
1040 	mutex_enter(&tp->tun_lock);
1041 	SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1042 	mutex_exit(&tp->tun_lock);
1043 }
1044 
1045 static int
1046 filt_tunread(struct knote *kn, long hint)
1047 {
1048 	struct tun_softc *tp = kn->kn_hook;
1049 	struct ifnet *ifp = &tp->tun_if;
1050 	struct mbuf *m;
1051 
1052 	mutex_enter(&tp->tun_lock);
1053 	IF_POLL(&ifp->if_snd, m);
1054 	if (m == NULL) {
1055 		mutex_exit(&tp->tun_lock);
1056 		return 0;
1057 	}
1058 
1059 	for (kn->kn_data = 0; m != NULL; m = m->m_next)
1060 		kn->kn_data += m->m_len;
1061 
1062 	mutex_exit(&tp->tun_lock);
1063 
1064 	return 1;
1065 }
1066 
1067 static const struct filterops tunread_filtops = {
1068 	.f_isfd = 1,
1069 	.f_attach = NULL,
1070 	.f_detach = filt_tunrdetach,
1071 	.f_event = filt_tunread,
1072 };
1073 
1074 static const struct filterops tun_seltrue_filtops = {
1075 	.f_isfd = 1,
1076 	.f_attach = NULL,
1077 	.f_detach = filt_tunrdetach,
1078 	.f_event = filt_seltrue,
1079 };
1080 
1081 int
1082 tunkqfilter(dev_t dev, struct knote *kn)
1083 {
1084 	struct tun_softc *tp;
1085 	struct klist *klist;
1086 	int rv = 0;
1087 
1088 	tp = tun_find_unit(dev);
1089 	if (tp == NULL)
1090 		goto out_nolock;
1091 
1092 	switch (kn->kn_filter) {
1093 	case EVFILT_READ:
1094 		klist = &tp->tun_rsel.sel_klist;
1095 		kn->kn_fop = &tunread_filtops;
1096 		break;
1097 
1098 	case EVFILT_WRITE:
1099 		klist = &tp->tun_rsel.sel_klist;
1100 		kn->kn_fop = &tun_seltrue_filtops;
1101 		break;
1102 
1103 	default:
1104 		rv = EINVAL;
1105 		goto out;
1106 	}
1107 
1108 	kn->kn_hook = tp;
1109 
1110 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1111 
1112 out:
1113 	mutex_exit(&tp->tun_lock);
1114 out_nolock:
1115 	return rv;
1116 }
1117 
1118 /*
1119  * Module infrastructure
1120  */
1121 #include "if_module.h"
1122 
1123 IF_MODULE(MODULE_CLASS_DRIVER, tun, "")
1124