xref: /dragonfly/sys/net/tun/if_tun.c (revision fe76c4fb)
1 /*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd 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 it's
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  * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
17  * $DragonFly: src/sys/net/tun/if_tun.c,v 1.28 2006/06/13 08:12:03 dillon Exp $
18  */
19 
20 #include "opt_atalk.h"
21 #include "opt_inet.h"
22 #include "opt_inet6.h"
23 #include "opt_ipx.h"
24 
25 #include <sys/param.h>
26 #include <sys/proc.h>
27 #include <sys/systm.h>
28 #include <sys/mbuf.h>
29 #include <sys/socket.h>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/thread2.h>
33 #include <sys/ttycom.h>
34 #include <sys/poll.h>
35 #include <sys/signalvar.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/conf.h>
40 #include <sys/uio.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 
44 #include <net/if.h>
45 #include <net/if_types.h>
46 #include <net/ifq_var.h>
47 #include <net/netisr.h>
48 #include <net/route.h>
49 
50 #ifdef INET
51 #include <netinet/in.h>
52 #endif
53 
54 #include <net/bpf.h>
55 
56 #include "if_tunvar.h"
57 #include "if_tun.h"
58 
59 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
60 
61 static void tunattach (void *);
62 PSEUDO_SET(tunattach, if_tun);
63 
64 static void tuncreate (dev_t dev);
65 
66 #define TUNDEBUG	if (tundebug) if_printf
67 static int tundebug = 0;
68 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
69 
70 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
71 	    struct rtentry *rt);
72 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
73 static int tuninit (struct ifnet *);
74 static void tunstart(struct ifnet *);
75 
76 static	d_open_t	tunopen;
77 static	d_close_t	tunclose;
78 static	d_read_t	tunread;
79 static	d_write_t	tunwrite;
80 static	d_ioctl_t	tunioctl;
81 static	d_poll_t	tunpoll;
82 
83 #define CDEV_MAJOR 52
84 static struct cdevsw tun_cdevsw = {
85 	/* name */	"tun",
86 	/* maj */	CDEV_MAJOR,
87 	/* flags */	0,
88 	/* port */	NULL,
89 	/* clone */	NULL,
90 
91 	/* open */	tunopen,
92 	/* close */	tunclose,
93 	/* read */	tunread,
94 	/* write */	tunwrite,
95 	/* ioctl */	tunioctl,
96 	/* poll */	tunpoll,
97 	/* mmap */	nommap,
98 	/* strategy */	nostrategy,
99 	/* dump */	nodump,
100 	/* psize */	nopsize
101 };
102 
103 static void
104 tunattach(void *dummy)
105 {
106 	cdevsw_add(&tun_cdevsw, 0, 0);
107 }
108 
109 static void
110 tuncreate(dev_t dev)
111 {
112 	struct tun_softc *sc;
113 	struct ifnet *ifp;
114 
115 	dev = make_dev(&tun_cdevsw, minor(dev),
116 	    UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
117 
118 	MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
119 	bzero(sc, sizeof *sc);
120 	sc->tun_flags = TUN_INITED;
121 
122 	ifp = &sc->tun_if;
123 	if_initname(ifp, "tun", lminor(dev));
124 	ifp->if_mtu = TUNMTU;
125 	ifp->if_ioctl = tunifioctl;
126 	ifp->if_output = tunoutput;
127 	ifp->if_start = tunstart;
128 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
129 	ifp->if_type = IFT_PPP;
130 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
131 	ifq_set_ready(&ifp->if_snd);
132 	ifp->if_softc = sc;
133 	if_attach(ifp, NULL);
134 	bpfattach(ifp, DLT_NULL, sizeof(u_int));
135 	dev->si_drv1 = sc;
136 }
137 
138 /*
139  * tunnel open - must be superuser & the device must be
140  * configured in
141  */
142 static	int
143 tunopen(dev_t dev, int flag, int mode, struct thread *td)
144 {
145 	struct ifnet	*ifp;
146 	struct tun_softc *tp;
147 	int	error;
148 
149 	KKASSERT(td->td_proc);
150 	if ((error = suser(td)) != NULL)
151 		return (error);
152 
153 	tp = dev->si_drv1;
154 	if (!tp) {
155 		tuncreate(dev);
156 		tp = dev->si_drv1;
157 	}
158 	if (tp->tun_flags & TUN_OPEN)
159 		return EBUSY;
160 	tp->tun_pid = td->td_proc->p_pid;
161 	ifp = &tp->tun_if;
162 	tp->tun_flags |= TUN_OPEN;
163 	TUNDEBUG(ifp, "open\n");
164 	return (0);
165 }
166 
167 /*
168  * tunclose - close the device - mark i/f down & delete
169  * routing info
170  */
171 static	int
172 tunclose(dev_t dev, int foo, int bar, struct thread *td)
173 {
174 	struct tun_softc *tp;
175 	struct ifnet	*ifp;
176 
177 	tp = dev->si_drv1;
178 	ifp = &tp->tun_if;
179 
180 	tp->tun_flags &= ~TUN_OPEN;
181 	tp->tun_pid = 0;
182 
183 	/* Junk all pending output. */
184 	lwkt_serialize_enter(ifp->if_serializer);
185 	ifq_purge(&ifp->if_snd);
186 	lwkt_serialize_exit(ifp->if_serializer);
187 
188 	if (ifp->if_flags & IFF_UP) {
189 		lwkt_serialize_enter(ifp->if_serializer);
190 		if_down(ifp);
191 		lwkt_serialize_exit(ifp->if_serializer);
192 	}
193 
194 	if (ifp->if_flags & IFF_RUNNING) {
195 		struct ifaddr *ifa;
196 
197 		lwkt_serialize_enter(ifp->if_serializer);
198 		/* find internet addresses and delete routes */
199 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
200 			if (ifa->ifa_addr->sa_family == AF_INET) {
201 				rtinit(ifa, (int)RTM_DELETE,
202 				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
203 			}
204 		}
205 		ifp->if_flags &= ~IFF_RUNNING;
206 		lwkt_serialize_exit(ifp->if_serializer);
207 	}
208 
209 	funsetown(tp->tun_sigio);
210 	selwakeup(&tp->tun_rsel);
211 
212 	TUNDEBUG(ifp, "closed\n");
213 	return (0);
214 }
215 
216 static int
217 tuninit(struct ifnet *ifp)
218 {
219 	struct tun_softc *tp = ifp->if_softc;
220 	struct ifaddr *ifa;
221 	int error = 0;
222 
223 	TUNDEBUG(ifp, "tuninit\n");
224 
225 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
226 	getmicrotime(&ifp->if_lastchange);
227 
228 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
229 	     ifa = TAILQ_NEXT(ifa, ifa_link)) {
230 		if (ifa->ifa_addr == NULL)
231 			error = EFAULT;
232 			/* XXX: Should maybe return straight off? */
233 		else {
234 #ifdef INET
235 			if (ifa->ifa_addr->sa_family == AF_INET) {
236 			    struct sockaddr_in *si;
237 
238 			    si = (struct sockaddr_in *)ifa->ifa_addr;
239 			    if (si->sin_addr.s_addr)
240 				    tp->tun_flags |= TUN_IASET;
241 
242 			    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
243 			    if (si && si->sin_addr.s_addr)
244 				    tp->tun_flags |= TUN_DSTADDR;
245 			}
246 #endif
247 		}
248 	}
249 	return (error);
250 }
251 
252 /*
253  * Process an ioctl request.
254  *
255  * MPSAFE
256  */
257 int
258 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
259 {
260 	struct ifreq *ifr = (struct ifreq *)data;
261 	struct tun_softc *tp = ifp->if_softc;
262 	struct ifstat *ifs;
263 	int error = 0;
264 
265 	switch(cmd) {
266 	case SIOCGIFSTATUS:
267 		ifs = (struct ifstat *)data;
268 		if (tp->tun_pid)
269 			sprintf(ifs->ascii + strlen(ifs->ascii),
270 			    "\tOpened by PID %d\n", tp->tun_pid);
271 		break;
272 	case SIOCSIFADDR:
273 		error = tuninit(ifp);
274 		TUNDEBUG(ifp, "address set, error=%d\n", error);
275 		break;
276 	case SIOCSIFDSTADDR:
277 		error = tuninit(ifp);
278 		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
279 		break;
280 	case SIOCSIFMTU:
281 		ifp->if_mtu = ifr->ifr_mtu;
282 		TUNDEBUG(ifp, "mtu set\n");
283 		break;
284 	case SIOCSIFFLAGS:
285 	case SIOCADDMULTI:
286 	case SIOCDELMULTI:
287 		break;
288 	default:
289 		error = EINVAL;
290 	}
291 	return (error);
292 }
293 
294 /*
295  * tunoutput - queue packets from higher level ready to put out.
296  *
297  * MPSAFE
298  */
299 int
300 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
301 	  struct rtentry *rt)
302 {
303 	struct tun_softc *tp = ifp->if_softc;
304 	int error;
305 	struct altq_pktattr pktattr;
306 
307 	TUNDEBUG(ifp, "tunoutput\n");
308 
309 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
310 		TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
311 		m_freem (m0);
312 		return EHOSTDOWN;
313 	}
314 
315 	/*
316 	 * if the queueing discipline needs packet classification,
317 	 * do it before prepending link headers.
318 	 */
319 	ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
320 
321 	/* BPF write needs to be handled specially */
322 	if (dst->sa_family == AF_UNSPEC) {
323 		dst->sa_family = *(mtod(m0, int *));
324 		m0->m_len -= sizeof(int);
325 		m0->m_pkthdr.len -= sizeof(int);
326 		m0->m_data += sizeof(int);
327 	}
328 
329 	if (ifp->if_bpf) {
330 		/*
331 		 * We need to prepend the address family as
332 		 * a four byte field.
333 		 */
334 		uint32_t af = dst->sa_family;
335 
336 		bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
337 	}
338 
339 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
340 	if (tp->tun_flags & TUN_LMODE) {
341 		/* allocate space for sockaddr */
342 		M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
343 
344 		/* if allocation failed drop packet */
345 		if (m0 == NULL){
346 			IF_DROP(&ifp->if_snd);
347 			ifp->if_oerrors++;
348 			return (ENOBUFS);
349 		} else {
350 			bcopy(dst, m0->m_data, dst->sa_len);
351 		}
352 	}
353 
354 	if (tp->tun_flags & TUN_IFHEAD) {
355 		/* Prepend the address family */
356 		M_PREPEND(m0, 4, MB_DONTWAIT);
357 
358 		/* if allocation failed drop packet */
359 		if (m0 == NULL){
360 			IF_DROP(&ifp->if_snd);
361 			ifp->if_oerrors++;
362 			return ENOBUFS;
363 		} else
364 			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
365 	} else {
366 #ifdef INET
367 		if (dst->sa_family != AF_INET)
368 #endif
369 		{
370 			m_freem(m0);
371 			return EAFNOSUPPORT;
372 		}
373 	}
374 
375 	error = ifq_handoff(ifp, m0, &pktattr);
376 	if (error) {
377 		ifp->if_collisions++;
378 	} else {
379 		ifp->if_opackets++;
380 		if (tp->tun_flags & TUN_RWAIT) {
381 			tp->tun_flags &= ~TUN_RWAIT;
382 			wakeup((caddr_t)tp);
383 		}
384 		get_mplock();
385 		if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
386 			pgsigio(tp->tun_sigio, SIGIO, 0);
387 		selwakeup(&tp->tun_rsel);
388 		rel_mplock();
389 	}
390 	return (error);
391 }
392 
393 /*
394  * the cdevsw interface is now pretty minimal.
395  */
396 static	int
397 tunioctl(dev_t	dev, u_long cmd, caddr_t data, int flag, struct thread *td)
398 {
399 	struct tun_softc *tp = dev->si_drv1;
400  	struct tuninfo *tunp;
401 
402 	switch (cmd) {
403  	case TUNSIFINFO:
404  		tunp = (struct tuninfo *)data;
405 		if (tunp->mtu < IF_MINMTU)
406 			return (EINVAL);
407  		tp->tun_if.if_mtu = tunp->mtu;
408  		tp->tun_if.if_type = tunp->type;
409  		tp->tun_if.if_baudrate = tunp->baudrate;
410  		break;
411  	case TUNGIFINFO:
412  		tunp = (struct tuninfo *)data;
413  		tunp->mtu = tp->tun_if.if_mtu;
414  		tunp->type = tp->tun_if.if_type;
415  		tunp->baudrate = tp->tun_if.if_baudrate;
416  		break;
417 	case TUNSDEBUG:
418 		tundebug = *(int *)data;
419 		break;
420 	case TUNGDEBUG:
421 		*(int *)data = tundebug;
422 		break;
423 	case TUNSLMODE:
424 		if (*(int *)data) {
425 			tp->tun_flags |= TUN_LMODE;
426 			tp->tun_flags &= ~TUN_IFHEAD;
427 		} else
428 			tp->tun_flags &= ~TUN_LMODE;
429 		break;
430 	case TUNSIFHEAD:
431 		if (*(int *)data) {
432 			tp->tun_flags |= TUN_IFHEAD;
433 			tp->tun_flags &= ~TUN_LMODE;
434 		} else
435 			tp->tun_flags &= ~TUN_IFHEAD;
436 		break;
437 	case TUNGIFHEAD:
438 		*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
439 		break;
440 	case TUNSIFMODE:
441 		/* deny this if UP */
442 		if (tp->tun_if.if_flags & IFF_UP)
443 			return(EBUSY);
444 
445 		switch (*(int *)data & ~IFF_MULTICAST) {
446 		case IFF_POINTOPOINT:
447 		case IFF_BROADCAST:
448 			tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
449 			tp->tun_if.if_flags |= *(int *)data;
450 			break;
451 		default:
452 			return(EINVAL);
453 		}
454 		break;
455 	case TUNSIFPID:
456 		tp->tun_pid = curproc->p_pid;
457 		break;
458 	case FIOASYNC:
459 		if (*(int *)data)
460 			tp->tun_flags |= TUN_ASYNC;
461 		else
462 			tp->tun_flags &= ~TUN_ASYNC;
463 		break;
464 	case FIONREAD:
465 		lwkt_serialize_enter(tp->tun_if.if_serializer);
466 		if (!ifq_is_empty(&tp->tun_if.if_snd)) {
467 			struct mbuf *mb;
468 
469 			mb = ifq_poll(&tp->tun_if.if_snd);
470 			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
471 				*(int *)data += mb->m_len;
472 		} else {
473 			*(int *)data = 0;
474 		}
475 		lwkt_serialize_exit(tp->tun_if.if_serializer);
476 		break;
477 	case FIOSETOWN:
478 		return (fsetown(*(int *)data, &tp->tun_sigio));
479 
480 	case FIOGETOWN:
481 		*(int *)data = fgetown(tp->tun_sigio);
482 		return (0);
483 
484 	/* This is deprecated, FIOSETOWN should be used instead. */
485 	case TIOCSPGRP:
486 		return (fsetown(-(*(int *)data), &tp->tun_sigio));
487 
488 	/* This is deprecated, FIOGETOWN should be used instead. */
489 	case TIOCGPGRP:
490 		*(int *)data = -fgetown(tp->tun_sigio);
491 		return (0);
492 
493 	default:
494 		return (ENOTTY);
495 	}
496 	return (0);
497 }
498 
499 /*
500  * The cdevsw read interface - reads a packet at a time, or at
501  * least as much of a packet as can be read.
502  */
503 static	int
504 tunread(dev_t dev, struct uio *uio, int flag)
505 {
506 	struct tun_softc *tp = dev->si_drv1;
507 	struct ifnet	*ifp = &tp->tun_if;
508 	struct mbuf	*m0;
509 	int		error=0, len;
510 
511 	TUNDEBUG(ifp, "read\n");
512 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
513 		TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
514 		return EHOSTDOWN;
515 	}
516 
517 	tp->tun_flags &= ~TUN_RWAIT;
518 
519 	lwkt_serialize_enter(ifp->if_serializer);
520 
521 	while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
522 		if (flag & IO_NDELAY) {
523 			lwkt_serialize_exit(ifp->if_serializer);
524 			return EWOULDBLOCK;
525 		}
526 		tp->tun_flags |= TUN_RWAIT;
527 		lwkt_serialize_exit(ifp->if_serializer);
528 		if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
529 			return error;
530 		lwkt_serialize_enter(ifp->if_serializer);
531 	}
532 
533 	lwkt_serialize_exit(ifp->if_serializer);
534 
535 	while (m0 && uio->uio_resid > 0 && error == 0) {
536 		len = min(uio->uio_resid, m0->m_len);
537 		if (len != 0)
538 			error = uiomove(mtod(m0, caddr_t), len, uio);
539 		m0 = m_free(m0);
540 	}
541 
542 	if (m0) {
543 		TUNDEBUG(ifp, "Dropping mbuf\n");
544 		m_freem(m0);
545 	}
546 	return error;
547 }
548 
549 /*
550  * the cdevsw write interface - an atomic write is a packet - or else!
551  */
552 static	int
553 tunwrite(dev_t dev, struct uio *uio, int flag)
554 {
555 	struct tun_softc *tp = dev->si_drv1;
556 	struct ifnet	*ifp = &tp->tun_if;
557 	struct mbuf	*top, **mp, *m;
558 	int		error=0, tlen, mlen;
559 	uint32_t	family;
560 	int		isr;
561 
562 	TUNDEBUG(ifp, "tunwrite\n");
563 
564 	if (uio->uio_resid == 0)
565 		return 0;
566 
567 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
568 		TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
569 		return EIO;
570 	}
571 	tlen = uio->uio_resid;
572 
573 	/* get a header mbuf */
574 	MGETHDR(m, MB_DONTWAIT, MT_DATA);
575 	if (m == NULL)
576 		return ENOBUFS;
577 	mlen = MHLEN;
578 
579 	top = 0;
580 	mp = &top;
581 	while (error == 0 && uio->uio_resid > 0) {
582 		m->m_len = min(mlen, uio->uio_resid);
583 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
584 		*mp = m;
585 		mp = &m->m_next;
586 		if (uio->uio_resid > 0) {
587 			MGET (m, MB_DONTWAIT, MT_DATA);
588 			if (m == 0) {
589 				error = ENOBUFS;
590 				break;
591 			}
592 			mlen = MLEN;
593 		}
594 	}
595 	if (error) {
596 		if (top)
597 			m_freem (top);
598 		ifp->if_ierrors++;
599 		return error;
600 	}
601 
602 	top->m_pkthdr.len = tlen;
603 	top->m_pkthdr.rcvif = ifp;
604 
605 	if (ifp->if_bpf) {
606 		if (tp->tun_flags & TUN_IFHEAD) {
607 			/*
608 			 * Conveniently, we already have a 4-byte address
609 			 * family prepended to our packet !
610 			 * Inconveniently, it's in the wrong byte order !
611 			 */
612 			if ((top = m_pullup(top, sizeof(family))) == NULL)
613 				return ENOBUFS;
614 			*mtod(top, u_int32_t *) =
615 			    ntohl(*mtod(top, u_int32_t *));
616 			bpf_mtap(ifp->if_bpf, top);
617 			*mtod(top, u_int32_t *) =
618 			    htonl(*mtod(top, u_int32_t *));
619 		} else {
620 			/*
621 			 * We need to prepend the address family as
622 			 * a four byte field.
623 			 */
624 			static const uint32_t af = AF_INET;
625 
626 			bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
627 		}
628 	}
629 
630 	if (tp->tun_flags & TUN_IFHEAD) {
631 		if (top->m_len < sizeof(family) &&
632 		    (top = m_pullup(top, sizeof(family))) == NULL)
633 				return ENOBUFS;
634 		family = ntohl(*mtod(top, u_int32_t *));
635 		m_adj(top, sizeof(family));
636 	} else
637 		family = AF_INET;
638 
639 	ifp->if_ibytes += top->m_pkthdr.len;
640 	ifp->if_ipackets++;
641 
642 	switch (family) {
643 #ifdef INET
644 	case AF_INET:
645 		isr = NETISR_IP;
646 		break;
647 #endif
648 #ifdef INET6
649 	case AF_INET6:
650 		isr = NETISR_IPV6;
651 		break;
652 #endif
653 #ifdef IPX
654 	case AF_IPX:
655 		isr = NETISR_IPX;
656 		break;
657 #endif
658 #ifdef NETATALK
659 	case AF_APPLETALK:
660 		isr = NETISR_ATALK2;
661 		break;
662 #endif
663 	default:
664 		m_freem(m);
665 		return (EAFNOSUPPORT);
666 	}
667 
668 	netisr_dispatch(isr, top);
669 	return (0);
670 }
671 
672 /*
673  * tunpoll - the poll interface, this is only useful on reads
674  * really. The write detect always returns true, write never blocks
675  * anyway, it either accepts the packet or drops it.
676  */
677 static	int
678 tunpoll(dev_t dev, int events, struct thread *td)
679 {
680 	struct tun_softc *tp = dev->si_drv1;
681 	struct ifnet	*ifp = &tp->tun_if;
682 	int		revents = 0;
683 
684 	TUNDEBUG(ifp, "tunpoll\n");
685 
686 	lwkt_serialize_enter(ifp->if_serializer);
687 
688 	if (events & (POLLIN | POLLRDNORM)) {
689 		if (!ifq_is_empty(&ifp->if_snd)) {
690 			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
691 			revents |= events & (POLLIN | POLLRDNORM);
692 		} else {
693 			TUNDEBUG(ifp, "tunpoll waiting\n");
694 			selrecord(td, &tp->tun_rsel);
695 		}
696 	}
697 	if (events & (POLLOUT | POLLWRNORM))
698 		revents |= events & (POLLOUT | POLLWRNORM);
699 
700 	lwkt_serialize_exit(ifp->if_serializer);
701 
702 	return (revents);
703 }
704 
705 /*
706  * Start packet transmission on the interface.
707  * when the interface queue is rate-limited by ALTQ,
708  * if_start is needed to drain packets from the queue in order
709  * to notify readers when outgoing packets become ready.
710  */
711 static void
712 tunstart(struct ifnet *ifp)
713 {
714 	struct tun_softc *tp = ifp->if_softc;
715 	struct mbuf *m;
716 
717 	if (!ifq_is_enabled(&ifp->if_snd))
718 		return;
719 
720 	m = ifq_poll(&ifp->if_snd);
721 	if (m != NULL) {
722 		if (tp->tun_flags & TUN_RWAIT) {
723 			tp->tun_flags &= ~TUN_RWAIT;
724 			wakeup((caddr_t)tp);
725 		}
726 		if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
727 			pgsigio(tp->tun_sigio, SIGIO, 0);
728 		selwakeup(&tp->tun_rsel);
729 	}
730 }
731