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