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