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