- Factor out if_purgeaddrs_nolink(), which frees all non-link ifaddrs no
[dragonfly.git] / sys / net / tun / if_tun.c
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.32 2007/12/31 04:58:53 sephe 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);
111         bzero(sc, sizeof *sc);
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 = suser_cred(ap->a_cred, 0)) != NULL)
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         lwkt_serialize_enter(ifp->if_serializer);
178         ifq_purge(&ifp->if_snd);
179         lwkt_serialize_exit(ifp->if_serializer);
180
181         if (ifp->if_flags & IFF_UP) {
182                 lwkt_serialize_enter(ifp->if_serializer);
183                 if_down(ifp);
184                 lwkt_serialize_exit(ifp->if_serializer);
185         }
186         ifp->if_flags &= ~IFF_RUNNING;
187         if_purgeaddrs_nolink(ifp);
188
189         funsetown(tp->tun_sigio);
190         selwakeup(&tp->tun_rsel);
191
192         TUNDEBUG(ifp, "closed\n");
193         return (0);
194 }
195
196 static int
197 tuninit(struct ifnet *ifp)
198 {
199         struct tun_softc *tp = ifp->if_softc;
200         struct ifaddr *ifa;
201         int error = 0;
202
203         TUNDEBUG(ifp, "tuninit\n");
204
205         ifp->if_flags |= IFF_UP | IFF_RUNNING;
206         getmicrotime(&ifp->if_lastchange);
207
208         for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 
209              ifa = TAILQ_NEXT(ifa, ifa_link)) {
210                 if (ifa->ifa_addr == NULL)
211                         error = EFAULT;
212                         /* XXX: Should maybe return straight off? */
213                 else {
214 #ifdef INET
215                         if (ifa->ifa_addr->sa_family == AF_INET) {
216                             struct sockaddr_in *si;
217
218                             si = (struct sockaddr_in *)ifa->ifa_addr;
219                             if (si->sin_addr.s_addr)
220                                     tp->tun_flags |= TUN_IASET;
221                         }
222 #endif
223                 }
224         }
225         return (error);
226 }
227
228 /*
229  * Process an ioctl request.
230  *
231  * MPSAFE
232  */
233 int
234 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
235 {
236         struct ifreq *ifr = (struct ifreq *)data;
237         struct tun_softc *tp = ifp->if_softc;
238         struct ifstat *ifs;
239         int error = 0;
240
241         switch(cmd) {
242         case SIOCGIFSTATUS:
243                 ifs = (struct ifstat *)data;
244                 if (tp->tun_pid)
245                         ksprintf(ifs->ascii + strlen(ifs->ascii),
246                             "\tOpened by PID %d\n", tp->tun_pid);
247                 break;
248         case SIOCSIFADDR:
249                 error = tuninit(ifp);
250                 TUNDEBUG(ifp, "address set, error=%d\n", error);
251                 break;
252         case SIOCSIFDSTADDR:
253                 error = tuninit(ifp);
254                 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
255                 break;
256         case SIOCSIFMTU:
257                 ifp->if_mtu = ifr->ifr_mtu;
258                 TUNDEBUG(ifp, "mtu set\n");
259                 break;
260         case SIOCSIFFLAGS:
261         case SIOCADDMULTI:
262         case SIOCDELMULTI:
263                 break;
264         default:
265                 error = EINVAL;
266         }
267         return (error);
268 }
269
270 /*
271  * tunoutput - queue packets from higher level ready to put out.
272  *
273  * MPSAFE
274  */
275 int
276 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
277           struct rtentry *rt)
278 {
279         struct tun_softc *tp = ifp->if_softc;
280         int error;
281         struct altq_pktattr pktattr;
282
283         TUNDEBUG(ifp, "tunoutput\n");
284
285         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
286                 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
287                 m_freem (m0);
288                 return EHOSTDOWN;
289         }
290
291         /*
292          * if the queueing discipline needs packet classification,
293          * do it before prepending link headers.
294          */
295         ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
296
297         /* BPF write needs to be handled specially */
298         if (dst->sa_family == AF_UNSPEC) {
299                 dst->sa_family = *(mtod(m0, int *));
300                 m0->m_len -= sizeof(int);
301                 m0->m_pkthdr.len -= sizeof(int);
302                 m0->m_data += sizeof(int);
303         }
304
305         if (ifp->if_bpf) {
306                 /*
307                  * We need to prepend the address family as
308                  * a four byte field.
309                  */
310                 uint32_t af = dst->sa_family;
311
312                 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
313         }
314
315         /* prepend sockaddr? this may abort if the mbuf allocation fails */
316         if (tp->tun_flags & TUN_LMODE) {
317                 /* allocate space for sockaddr */
318                 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
319
320                 /* if allocation failed drop packet */
321                 if (m0 == NULL){
322                         IF_DROP(&ifp->if_snd);
323                         ifp->if_oerrors++;
324                         return (ENOBUFS);
325                 } else {
326                         bcopy(dst, m0->m_data, dst->sa_len);
327                 }
328         }
329
330         if (tp->tun_flags & TUN_IFHEAD) {
331                 /* Prepend the address family */
332                 M_PREPEND(m0, 4, MB_DONTWAIT);
333
334                 /* if allocation failed drop packet */
335                 if (m0 == NULL){
336                         IF_DROP(&ifp->if_snd);
337                         ifp->if_oerrors++;
338                         return ENOBUFS;
339                 } else
340                         *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
341         } else {
342 #ifdef INET
343                 if (dst->sa_family != AF_INET)
344 #endif
345                 {
346                         m_freem(m0);
347                         return EAFNOSUPPORT;
348                 }
349         }
350
351         error = ifq_handoff(ifp, m0, &pktattr);
352         if (error) {
353                 ifp->if_collisions++;
354         } else {
355                 ifp->if_opackets++;
356                 if (tp->tun_flags & TUN_RWAIT) {
357                         tp->tun_flags &= ~TUN_RWAIT;
358                         wakeup((caddr_t)tp);
359                 }
360                 get_mplock();
361                 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
362                         pgsigio(tp->tun_sigio, SIGIO, 0);
363                 selwakeup(&tp->tun_rsel);
364                 rel_mplock();
365         }
366         return (error);
367 }
368
369 /*
370  * the ops interface is now pretty minimal.
371  */
372 static  int
373 tunioctl(struct dev_ioctl_args *ap)
374 {
375         cdev_t dev = ap->a_head.a_dev;
376         struct tun_softc *tp = dev->si_drv1;
377         struct tuninfo *tunp;
378
379         switch (ap->a_cmd) {
380         case TUNSIFINFO:
381                 tunp = (struct tuninfo *)ap->a_data;
382                 if (tunp->mtu < IF_MINMTU)
383                         return (EINVAL);
384                 tp->tun_if.if_mtu = tunp->mtu;
385                 tp->tun_if.if_type = tunp->type;
386                 tp->tun_if.if_baudrate = tunp->baudrate;
387                 break;
388         case TUNGIFINFO:
389                 tunp = (struct tuninfo *)ap->a_data;
390                 tunp->mtu = tp->tun_if.if_mtu;
391                 tunp->type = tp->tun_if.if_type;
392                 tunp->baudrate = tp->tun_if.if_baudrate;
393                 break;
394         case TUNSDEBUG:
395                 tundebug = *(int *)ap->a_data;
396                 break;
397         case TUNGDEBUG:
398                 *(int *)ap->a_data = tundebug;
399                 break;
400         case TUNSLMODE:
401                 if (*(int *)ap->a_data) {
402                         tp->tun_flags |= TUN_LMODE;
403                         tp->tun_flags &= ~TUN_IFHEAD;
404                 } else
405                         tp->tun_flags &= ~TUN_LMODE;
406                 break;
407         case TUNSIFHEAD:
408                 if (*(int *)ap->a_data) {
409                         tp->tun_flags |= TUN_IFHEAD;
410                         tp->tun_flags &= ~TUN_LMODE;
411                 } else 
412                         tp->tun_flags &= ~TUN_IFHEAD;
413                 break;
414         case TUNGIFHEAD:
415                 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
416                 break;
417         case TUNSIFMODE:
418                 /* deny this if UP */
419                 if (tp->tun_if.if_flags & IFF_UP)
420                         return(EBUSY);
421
422                 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
423                 case IFF_POINTOPOINT:
424                 case IFF_BROADCAST:
425                         tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
426                         tp->tun_if.if_flags |= *(int *)ap->a_data;
427                         break;
428                 default:
429                         return(EINVAL);
430                 }
431                 break;
432         case TUNSIFPID:
433                 tp->tun_pid = curproc->p_pid;
434                 break;
435         case FIOASYNC:
436                 if (*(int *)ap->a_data)
437                         tp->tun_flags |= TUN_ASYNC;
438                 else
439                         tp->tun_flags &= ~TUN_ASYNC;
440                 break;
441         case FIONREAD:
442                 lwkt_serialize_enter(tp->tun_if.if_serializer);
443                 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
444                         struct mbuf *mb;
445
446                         mb = ifq_poll(&tp->tun_if.if_snd);
447                         for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next) 
448                                 *(int *)ap->a_data += mb->m_len;
449                 } else {
450                         *(int *)ap->a_data = 0;
451                 }
452                 lwkt_serialize_exit(tp->tun_if.if_serializer);
453                 break;
454         case FIOSETOWN:
455                 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
456
457         case FIOGETOWN:
458                 *(int *)ap->a_data = fgetown(tp->tun_sigio);
459                 return (0);
460
461         /* This is deprecated, FIOSETOWN should be used instead. */
462         case TIOCSPGRP:
463                 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
464
465         /* This is deprecated, FIOGETOWN should be used instead. */
466         case TIOCGPGRP:
467                 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
468                 return (0);
469
470         default:
471                 return (ENOTTY);
472         }
473         return (0);
474 }
475
476 /*
477  * The ops read interface - reads a packet at a time, or at
478  * least as much of a packet as can be read.
479  */
480 static  int
481 tunread(struct dev_read_args *ap)
482 {
483         cdev_t dev = ap->a_head.a_dev;
484         struct uio *uio = ap->a_uio;
485         struct tun_softc *tp = dev->si_drv1;
486         struct ifnet    *ifp = &tp->tun_if;
487         struct mbuf     *m0;
488         int             error=0, len;
489
490         TUNDEBUG(ifp, "read\n");
491         if ((tp->tun_flags & TUN_READY) != TUN_READY) {
492                 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
493                 return EHOSTDOWN;
494         }
495
496         tp->tun_flags &= ~TUN_RWAIT;
497
498         lwkt_serialize_enter(ifp->if_serializer);
499
500         while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
501                 if (ap->a_ioflag & IO_NDELAY) {
502                         lwkt_serialize_exit(ifp->if_serializer);
503                         return EWOULDBLOCK;
504                 }
505                 tp->tun_flags |= TUN_RWAIT;
506                 lwkt_serialize_exit(ifp->if_serializer);
507                 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
508                         return error;
509                 lwkt_serialize_enter(ifp->if_serializer);
510         }
511
512         lwkt_serialize_exit(ifp->if_serializer);
513
514         while (m0 && uio->uio_resid > 0 && error == 0) {
515                 len = min(uio->uio_resid, m0->m_len);
516                 if (len != 0)
517                         error = uiomove(mtod(m0, caddr_t), len, uio);
518                 m0 = m_free(m0);
519         }
520
521         if (m0) {
522                 TUNDEBUG(ifp, "Dropping mbuf\n");
523                 m_freem(m0);
524         }
525         return error;
526 }
527
528 /*
529  * the ops write interface - an atomic write is a packet - or else!
530  */
531 static  int
532 tunwrite(struct dev_write_args *ap)
533 {
534         cdev_t dev = ap->a_head.a_dev;
535         struct uio *uio = ap->a_uio;
536         struct tun_softc *tp = dev->si_drv1;
537         struct ifnet    *ifp = &tp->tun_if;
538         struct mbuf     *top, **mp, *m;
539         int             error=0, tlen, mlen;
540         uint32_t        family;
541         int             isr;
542
543         TUNDEBUG(ifp, "tunwrite\n");
544
545         if (uio->uio_resid == 0)
546                 return 0;
547
548         if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
549                 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
550                 return EIO;
551         }
552         tlen = uio->uio_resid;
553
554         /* get a header mbuf */
555         MGETHDR(m, MB_DONTWAIT, MT_DATA);
556         if (m == NULL)
557                 return ENOBUFS;
558         mlen = MHLEN;
559
560         top = 0;
561         mp = &top;
562         while (error == 0 && uio->uio_resid > 0) {
563                 m->m_len = min(mlen, uio->uio_resid);
564                 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
565                 *mp = m;
566                 mp = &m->m_next;
567                 if (uio->uio_resid > 0) {
568                         MGET (m, MB_DONTWAIT, MT_DATA);
569                         if (m == 0) {
570                                 error = ENOBUFS;
571                                 break;
572                         }
573                         mlen = MLEN;
574                 }
575         }
576         if (error) {
577                 if (top)
578                         m_freem (top);
579                 ifp->if_ierrors++;
580                 return error;
581         }
582
583         top->m_pkthdr.len = tlen;
584         top->m_pkthdr.rcvif = ifp;
585
586         if (ifp->if_bpf) {
587                 if (tp->tun_flags & TUN_IFHEAD) {
588                         /*
589                          * Conveniently, we already have a 4-byte address
590                          * family prepended to our packet !
591                          * Inconveniently, it's in the wrong byte order !
592                          */
593                         if ((top = m_pullup(top, sizeof(family))) == NULL)
594                                 return ENOBUFS;
595                         *mtod(top, u_int32_t *) =
596                             ntohl(*mtod(top, u_int32_t *));
597                         bpf_mtap(ifp->if_bpf, top);
598                         *mtod(top, u_int32_t *) =
599                             htonl(*mtod(top, u_int32_t *));
600                 } else {
601                         /*
602                          * We need to prepend the address family as
603                          * a four byte field.
604                          */
605                         static const uint32_t af = AF_INET;
606
607                         bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
608                 }
609         }
610
611         if (tp->tun_flags & TUN_IFHEAD) {
612                 if (top->m_len < sizeof(family) &&
613                     (top = m_pullup(top, sizeof(family))) == NULL)
614                                 return ENOBUFS;
615                 family = ntohl(*mtod(top, u_int32_t *));
616                 m_adj(top, sizeof(family));
617         } else
618                 family = AF_INET;
619
620         ifp->if_ibytes += top->m_pkthdr.len;
621         ifp->if_ipackets++;
622
623         switch (family) {
624 #ifdef INET
625         case AF_INET:
626                 isr = NETISR_IP;
627                 break;
628 #endif
629 #ifdef INET6
630         case AF_INET6:
631                 isr = NETISR_IPV6;
632                 break;
633 #endif
634 #ifdef IPX
635         case AF_IPX:
636                 isr = NETISR_IPX;
637                 break;
638 #endif
639 #ifdef NETATALK
640         case AF_APPLETALK:
641                 isr = NETISR_ATALK2;
642                 break;
643 #endif
644         default:
645                 m_freem(m);
646                 return (EAFNOSUPPORT);
647         }
648
649         netisr_dispatch(isr, top);
650         return (0);
651 }
652
653 /*
654  * tunpoll - the poll interface, this is only useful on reads
655  * really. The write detect always returns true, write never blocks
656  * anyway, it either accepts the packet or drops it.
657  */
658 static  int
659 tunpoll(struct dev_poll_args *ap)
660 {
661         cdev_t dev = ap->a_head.a_dev;
662         struct tun_softc *tp = dev->si_drv1;
663         struct ifnet    *ifp = &tp->tun_if;
664         int             revents = 0;
665
666         TUNDEBUG(ifp, "tunpoll\n");
667
668         lwkt_serialize_enter(ifp->if_serializer);
669
670         if (ap->a_events & (POLLIN | POLLRDNORM)) {
671                 if (!ifq_is_empty(&ifp->if_snd)) {
672                         TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
673                         revents |= ap->a_events & (POLLIN | POLLRDNORM);
674                 } else {
675                         TUNDEBUG(ifp, "tunpoll waiting\n");
676                         selrecord(curthread, &tp->tun_rsel);
677                 }
678         }
679         if (ap->a_events & (POLLOUT | POLLWRNORM))
680                 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
681
682         lwkt_serialize_exit(ifp->if_serializer);
683         ap->a_events = revents;
684         return(0);
685 }
686
687 /*
688  * Start packet transmission on the interface.
689  * when the interface queue is rate-limited by ALTQ,
690  * if_start is needed to drain packets from the queue in order
691  * to notify readers when outgoing packets become ready.
692  */
693 static void
694 tunstart(struct ifnet *ifp)
695 {
696         struct tun_softc *tp = ifp->if_softc;
697         struct mbuf *m;
698
699         if (!ifq_is_enabled(&ifp->if_snd))
700                 return;
701
702         m = ifq_poll(&ifp->if_snd);
703         if (m != NULL) {
704                 if (tp->tun_flags & TUN_RWAIT) {
705                         tp->tun_flags &= ~TUN_RWAIT;
706                         wakeup((caddr_t)tp);
707                 }
708                 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
709                         pgsigio(tp->tun_sigio, SIGIO, 0);
710                 selwakeup(&tp->tun_rsel);
711         }
712 }