Convert to critical sections.
[dragonfly.git] / sys / net / tap / if_tap.c
1 /*
2  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * BASED ON:
27  * -------------------------------------------------------------------------
28  *
29  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30  * Nottingham University 1987.
31  */
32
33 /*
34  * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $
35  * $DragonFly: src/sys/net/tap/if_tap.c,v 1.19 2005/06/14 18:30:55 joerg Exp $
36  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
37  */
38
39 #include "opt_inet.h"
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/thread2.h>
56 #include <sys/ttycom.h>
57 #include <sys/uio.h>
58 #include <sys/vnode.h>
59
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67
68 #include "if_tapvar.h"
69 #include "if_tap.h"
70
71
72 #define CDEV_NAME       "tap"
73 #define CDEV_MAJOR      149
74 #define TAPDEBUG        if (tapdebug) if_printf
75
76 #define TAP             "tap"
77 #define VMNET           "vmnet"
78 #define VMNET_DEV_MASK  0x00010000
79
80 /* module */
81 static int              tapmodevent     (module_t, int, void *);
82
83 /* device */
84 static void             tapcreate       (dev_t);
85
86 /* network interface */
87 static void             tapifstart      (struct ifnet *);
88 static int              tapifioctl      (struct ifnet *, u_long, caddr_t,
89                                          struct ucred *);
90 static void             tapifinit       (void *);
91
92 /* character device */
93 static d_open_t         tapopen;
94 static d_close_t        tapclose;
95 static d_read_t         tapread;
96 static d_write_t        tapwrite;
97 static d_ioctl_t        tapioctl;
98 static d_poll_t         tappoll;
99
100 static struct cdevsw    tap_cdevsw = {
101         /* dev name */  CDEV_NAME,
102         /* dev major */ CDEV_MAJOR,
103         /* flags */     0,
104         /* port */      NULL,
105         /* clone */     NULL,
106
107         /* open */      tapopen,
108         /* close */     tapclose,
109         /* read */      tapread,
110         /* write */     tapwrite,
111         /* ioctl */     tapioctl,
112         /* poll */      tappoll,
113         /* mmap */      nommap,
114         /* startegy */  nostrategy,
115         /* dump */      nodump,
116         /* psize */     nopsize
117 };
118
119 static int              taprefcnt = 0;          /* module ref. counter   */
120 static int              taplastunit = -1;       /* max. open unit number */
121 static int              tapdebug = 0;           /* debug flag            */
122
123 MALLOC_DECLARE(M_TAP);
124 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
125 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
126 DEV_MODULE(if_tap, tapmodevent, NULL);
127
128 /*
129  * tapmodevent
130  *
131  * module event handler
132  */
133 static int
134 tapmodevent(mod, type, data)
135         module_t         mod;
136         int              type;
137         void            *data;
138 {
139         static int               attached = 0;
140         struct ifnet            *ifp = NULL;
141         int                      unit;
142
143         switch (type) {
144         case MOD_LOAD:
145                 if (attached)
146                         return (EEXIST);
147
148                 cdevsw_add(&tap_cdevsw, 0, 0);
149                 attached = 1;
150         break;
151
152         case MOD_UNLOAD:
153                 if (taprefcnt > 0)
154                         return (EBUSY);
155
156                 cdevsw_remove(&tap_cdevsw, 0, 0);
157
158                 /* XXX: maintain tap ifs in a local list */
159                 unit = 0;
160                 while (unit <= taplastunit) {
161                         crit_enter();
162                         TAILQ_FOREACH(ifp, &ifnet, if_link)
163                                 if ((strcmp(ifp->if_dname, TAP) == 0) ||
164                                     (strcmp(ifp->if_dname, VMNET) == 0))
165                                         if (ifp->if_dunit == unit)
166                                                 break;
167                         crit_exit();
168
169                         if (ifp != NULL) {
170                                 struct tap_softc        *tp = ifp->if_softc;
171
172                                 TAPDEBUG(ifp, "detached. minor = %#x, " \
173                                         "taplastunit = %d\n",
174                                         minor(tp->tap_dev), taplastunit);
175
176                                 crit_enter();
177                                 ether_ifdetach(ifp);
178                                 crit_exit();
179                                 destroy_dev(tp->tap_dev);
180                                 free(tp, M_TAP);
181                         }
182                         else
183                                 unit ++;
184                 }
185
186                 attached = 0;
187         break;
188
189         default:
190                 return (EOPNOTSUPP);
191         }
192
193         return (0);
194 } /* tapmodevent */
195
196
197 /*
198  * tapcreate
199  *
200  * to create interface
201  */
202 static void
203 tapcreate(dev)
204         dev_t   dev;
205 {
206         struct ifnet            *ifp = NULL;
207         struct tap_softc        *tp = NULL;
208         uint8_t                 ether_addr[ETHER_ADDR_LEN];
209         int                      unit;
210         char                    *name = NULL;
211
212         /* allocate driver storage and create device */
213         MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
214         bzero(tp, sizeof(*tp));
215
216         /* select device: tap or vmnet */
217         if (minor(dev) & VMNET_DEV_MASK) {
218                 name = VMNET;
219                 unit = lminor(dev) & 0xff;
220                 tp->tap_flags |= TAP_VMNET;
221         }
222         else {
223                 name = TAP;
224                 unit = lminor(dev);
225         }
226
227         tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 
228                                                 0600, "%s%d", name, unit);
229         tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
230         reference_dev(tp->tap_dev);     /* so we can destroy it later */
231
232         /* generate fake MAC address: 00 bd xx xx xx unit_no */
233         ether_addr[0] = 0x00;
234         ether_addr[1] = 0xbd;
235         bcopy(&ticks, ether_addr, 4);
236         ether_addr[5] = (u_char)unit;
237
238         /* fill the rest and attach interface */        
239         ifp = &tp->tap_if;
240         ifp->if_softc = tp;
241
242         if_initname(ifp, name, unit);
243         if (unit > taplastunit)
244                 taplastunit = unit;
245
246         ifp->if_init = tapifinit;
247         ifp->if_start = tapifstart;
248         ifp->if_ioctl = tapifioctl;
249         ifp->if_mtu = ETHERMTU;
250         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
251         ifp->if_snd.ifq_maxlen = ifqmaxlen;
252
253         ether_ifattach(ifp, ether_addr);
254
255         tp->tap_flags |= TAP_INITED;
256
257         TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
258 } /* tapcreate */
259
260
261 /*
262  * tapopen 
263  *
264  * to open tunnel. must be superuser
265  */
266 static int
267 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
268 {
269         struct tap_softc        *tp = NULL;
270         int                      error;
271
272         if ((error = suser(td)) != 0)
273                 return (error);
274
275         tp = dev->si_drv1;
276         if (tp == NULL) {
277                 tapcreate(dev);
278                 tp = dev->si_drv1;
279         }
280
281         if (tp->tap_flags & TAP_OPEN)
282                 return (EBUSY);
283
284         bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
285
286         tp->tap_td = td;
287         tp->tap_flags |= TAP_OPEN;
288         taprefcnt ++;
289
290         TAPDEBUG(&tp->arpcom.ac_if,
291                  "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
292                  minor(tp->tap_dev), taprefcnt, taplastunit);
293
294         return (0);
295 } /* tapopen */
296
297
298 /*
299  * tapclose
300  *
301  * close the device - mark i/f down & delete routing info
302  */
303 static int
304 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
305 {
306         struct tap_softc        *tp = dev->si_drv1;
307         struct ifnet            *ifp = &tp->tap_if;
308         struct mbuf             *m = NULL;
309
310         /* junk all pending output */
311
312         crit_enter();
313         do {
314                 IF_DEQUEUE(&ifp->if_snd, m);
315                 if (m != NULL)
316                         m_freem(m);
317         } while (m != NULL);
318         crit_exit();
319
320         /*
321          * do not bring the interface down, and do not anything with
322          * interface, if we are in VMnet mode. just close the device.
323          */
324
325         if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
326                 crit_enter();
327                 if_down(ifp);
328                 if (ifp->if_flags & IFF_RUNNING) {
329                         /* find internet addresses and delete routes */
330                         struct ifaddr   *ifa = NULL;
331
332                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
333                                 if (ifa->ifa_addr->sa_family == AF_INET) {
334                                         rtinit(ifa, (int)RTM_DELETE, 0);
335
336                                         /* remove address from interface */
337                                         bzero(ifa->ifa_addr, 
338                                                    sizeof(*(ifa->ifa_addr)));
339                                         bzero(ifa->ifa_dstaddr, 
340                                                    sizeof(*(ifa->ifa_dstaddr)));
341                                         bzero(ifa->ifa_netmask, 
342                                                    sizeof(*(ifa->ifa_netmask)));
343                                 }
344                         }
345
346                         ifp->if_flags &= ~IFF_RUNNING;
347                 }
348                 crit_exit();
349         }
350
351         funsetown(tp->tap_sigio);
352         selwakeup(&tp->tap_rsel);
353
354         tp->tap_flags &= ~TAP_OPEN;
355         tp->tap_td = NULL;
356
357         taprefcnt --;
358         if (taprefcnt < 0) {
359                 taprefcnt = 0;
360                 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
361                         "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
362         }
363
364         TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
365                  minor(tp->tap_dev), taprefcnt, taplastunit);
366
367         return (0);
368 } /* tapclose */
369
370
371 /*
372  * tapifinit
373  *
374  * network interface initialization function
375  */
376 static void
377 tapifinit(xtp)
378         void    *xtp;
379 {
380         struct tap_softc        *tp = (struct tap_softc *)xtp;
381         struct ifnet            *ifp = &tp->tap_if;
382
383         TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
384
385         ifp->if_flags |= IFF_RUNNING;
386         ifp->if_flags &= ~IFF_OACTIVE;
387
388         /* attempt to start output */
389         tapifstart(ifp);
390 } /* tapifinit */
391
392
393 /*
394  * tapifioctl
395  *
396  * Process an ioctl request on network interface
397  */
398 int
399 tapifioctl(ifp, cmd, data, cr)
400         struct ifnet    *ifp;
401         u_long           cmd;
402         caddr_t          data;
403         struct ucred    *cr;
404 {
405         struct tap_softc        *tp = (struct tap_softc *)(ifp->if_softc);
406         struct ifstat           *ifs = NULL;
407         int                      dummy;
408
409         switch (cmd) {
410                 case SIOCSIFADDR:
411                 case SIOCGIFADDR:
412                 case SIOCSIFMTU:
413                         crit_enter();
414                         dummy = ether_ioctl(ifp, cmd, data);
415                         crit_exit();
416                         return (dummy);
417
418                 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
419                 case SIOCADDMULTI:
420                 case SIOCDELMULTI:
421                         break;
422
423                 case SIOCGIFSTATUS:
424                         crit_enter();
425                         ifs = (struct ifstat *)data;
426                         dummy = strlen(ifs->ascii);
427                         if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
428                                 if (tp->tap_td->td_proc) {
429                                     snprintf(ifs->ascii + dummy,
430                                         sizeof(ifs->ascii) - dummy,
431                                         "\tOpened by pid %d\n",
432                                         (int)tp->tap_td->td_proc->p_pid);
433                                 } else {
434                                     snprintf(ifs->ascii + dummy,
435                                         sizeof(ifs->ascii) - dummy,
436                                         "\tOpened by td %p\n", tp->tap_td);
437                                 }
438                         }
439                         crit_exit();
440                         break;
441
442                 default:
443                         return (EINVAL);
444         }
445
446         return (0);
447 } /* tapifioctl */
448
449
450 /*
451  * tapifstart 
452  * 
453  * queue packets from higher level ready to put out
454  */
455 static void
456 tapifstart(ifp)
457         struct ifnet    *ifp;
458 {
459         struct tap_softc        *tp = ifp->if_softc;
460
461         TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
462
463         /*
464          * do not junk pending output if we are in VMnet mode.
465          * XXX: can this do any harm because of queue overflow?
466          */
467
468         if (((tp->tap_flags & TAP_VMNET) == 0) && 
469             ((tp->tap_flags & TAP_READY) != TAP_READY)) {
470                 struct mbuf     *m = NULL;
471
472                 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
473                          minor(tp->tap_dev), tp->tap_flags);
474
475                 crit_enter();
476                 do {
477                         IF_DEQUEUE(&ifp->if_snd, m);
478                         if (m != NULL)
479                                 m_freem(m);
480                         ifp->if_oerrors ++;
481                 } while (m != NULL);
482
483                 crit_exit();
484                 return;
485         }
486
487         crit_enter();
488
489         ifp->if_flags |= IFF_OACTIVE;
490
491         if (ifp->if_snd.ifq_len != 0) {
492                 if (tp->tap_flags & TAP_RWAIT) {
493                         tp->tap_flags &= ~TAP_RWAIT;
494                         wakeup((caddr_t)tp);
495                 }
496
497                 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
498                         pgsigio(tp->tap_sigio, SIGIO, 0);
499
500                 selwakeup(&tp->tap_rsel);
501                 ifp->if_opackets ++; /* obytes are counted in ether_output */
502         }
503
504         ifp->if_flags &= ~IFF_OACTIVE;
505
506         crit_exit();
507 } /* tapifstart */
508
509
510 /*
511  * tapioctl
512  *
513  * the cdevsw interface is now pretty minimal
514  */
515 static int
516 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
517 {
518         struct tap_softc        *tp = dev->si_drv1;
519         struct ifnet            *ifp = &tp->tap_if;
520         struct tapinfo          *tapp = NULL;
521
522         switch (cmd) {
523                 case TAPSIFINFO:
524                         crit_enter();
525                         tapp = (struct tapinfo *)data;
526                         ifp->if_mtu = tapp->mtu;
527                         ifp->if_type = tapp->type;
528                         ifp->if_baudrate = tapp->baudrate;
529                         crit_exit();
530                 break;
531
532                 case TAPGIFINFO:
533                         tapp = (struct tapinfo *)data;
534                         tapp->mtu = ifp->if_mtu;
535                         tapp->type = ifp->if_type;
536                         tapp->baudrate = ifp->if_baudrate;
537                 break;
538
539                 case TAPSDEBUG:
540                         tapdebug = *(int *)data;
541                 break;
542
543                 case TAPGDEBUG:
544                         *(int *)data = tapdebug;
545                 break;
546
547                 case FIONBIO:
548                 break;
549
550                 case FIOASYNC:
551                         crit_enter();
552                         if (*(int *)data)
553                                 tp->tap_flags |= TAP_ASYNC;
554                         else
555                                 tp->tap_flags &= ~TAP_ASYNC;
556                         crit_exit();
557                 break;
558
559                 case FIONREAD:
560                         crit_enter();
561                         if (ifp->if_snd.ifq_head) {
562                                 struct mbuf     *mb = ifp->if_snd.ifq_head;
563
564                                 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
565                                         *(int *)data += mb->m_len;
566                         } 
567                         else
568                                 *(int *)data = 0;
569                         crit_exit();
570                 break;
571
572                 case FIOSETOWN:
573                         return (fsetown(*(int *)data, &tp->tap_sigio));
574
575                 case FIOGETOWN:
576                         *(int *)data = fgetown(tp->tap_sigio);
577                         return (0);
578
579                 /* this is deprecated, FIOSETOWN should be used instead */
580                 case TIOCSPGRP:
581                         return (fsetown(-(*(int *)data), &tp->tap_sigio));
582
583                 /* this is deprecated, FIOGETOWN should be used instead */
584                 case TIOCGPGRP:
585                         *(int *)data = -fgetown(tp->tap_sigio);
586                         return (0);
587
588                 /* VMware/VMnet port ioctl's */
589
590                 case SIOCGIFFLAGS:      /* get ifnet flags */
591                         bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
592                 break;
593
594                 case VMIO_SIOCSIFFLAGS: { /* VMware/VMnet SIOCSIFFLAGS */
595                         short   f = *(short *)data;
596
597                         f &= 0x0fff;
598                         f &= ~IFF_CANTCHANGE;
599                         f |= IFF_UP;
600
601                         crit_enter();
602                         ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
603                         crit_exit();
604                 } break;
605
606                 case OSIOCGIFADDR:      /* get MAC address of the remote side */
607                 case SIOCGIFADDR:
608                         bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
609                 break;
610
611                 case SIOCSIFADDR:       /* set MAC address of the remote side */
612                         bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
613                 break;
614
615                 default:
616                         return (ENOTTY);
617         }
618         return (0);
619 } /* tapioctl */
620
621
622 /*
623  * tapread
624  *
625  * the cdevsw read interface - reads a packet at a time, or at
626  * least as much of a packet as can be read
627  */
628 static int
629 tapread(dev, uio, flag)
630         dev_t            dev;
631         struct uio      *uio;
632         int              flag;
633 {
634         struct tap_softc        *tp = dev->si_drv1;
635         struct ifnet            *ifp = &tp->tap_if;
636         struct mbuf             *m0 = NULL;
637         int                      error = 0, len;
638
639         TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
640
641         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
642                 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
643                          minor(tp->tap_dev), tp->tap_flags);
644
645                 return (EHOSTDOWN);
646         }
647
648         tp->tap_flags &= ~TAP_RWAIT;
649
650         /* sleep until we get a packet */
651         do {
652                 crit_enter();
653                 IF_DEQUEUE(&ifp->if_snd, m0);
654                 crit_exit();
655
656                 if (m0 == NULL) {
657                         if (flag & IO_NDELAY)
658                                 return (EWOULDBLOCK);
659                         
660                         tp->tap_flags |= TAP_RWAIT;
661                         error = tsleep((caddr_t)tp, PCATCH, "taprd", 0);
662                         if (error)
663                                 return (error);
664                 }
665         } while (m0 == NULL);
666
667         BPF_MTAP(ifp, m0);
668
669         /* xfer packet to user space */
670         while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
671                 len = min(uio->uio_resid, m0->m_len);
672                 if (len == 0)
673                         break;
674
675                 error = uiomove(mtod(m0, caddr_t), len, uio);
676                 m0 = m_free(m0);
677         }
678
679         if (m0 != NULL) {
680                 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
681                          minor(tp->tap_dev));
682                 m_freem(m0);
683         }
684
685         return (error);
686 } /* tapread */
687
688
689 /*
690  * tapwrite
691  *
692  * the cdevsw write interface - an atomic write is a packet - or else!
693  */
694 static int
695 tapwrite(dev, uio, flag)
696         dev_t            dev;
697         struct uio      *uio;
698         int              flag;
699 {
700         struct tap_softc        *tp = dev->si_drv1;
701         struct ifnet            *ifp = &tp->tap_if;
702         struct mbuf             *top = NULL, **mp = NULL, *m = NULL;
703         int                      error = 0, tlen, mlen;
704
705         TAPDEBUG(ifp, "writting, minor = %#x\n", minor(tp->tap_dev));
706
707         if (uio->uio_resid == 0)
708                 return (0);
709
710         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
711                 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
712                          uio->uio_resid, minor(tp->tap_dev));
713
714                 return (EIO);
715         }
716         tlen = uio->uio_resid;
717
718         /* get a header mbuf */
719         MGETHDR(m, MB_DONTWAIT, MT_DATA);
720         if (m == NULL)
721                 return (ENOBUFS);
722         mlen = MHLEN;
723
724         top = 0;
725         mp = &top;
726         while ((error == 0) && (uio->uio_resid > 0)) {
727                 m->m_len = min(mlen, uio->uio_resid);
728                 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
729                 *mp = m;
730                 mp = &m->m_next;
731                 if (uio->uio_resid > 0) {
732                         MGET(m, MB_DONTWAIT, MT_DATA);
733                         if (m == NULL) {
734                                 error = ENOBUFS;
735                                 break;
736                         }
737                         mlen = MLEN;
738                 }
739         }
740         if (error) {
741                 ifp->if_ierrors ++;
742                 if (top)
743                         m_freem(top);
744                 return (error);
745         }
746
747         top->m_pkthdr.len = tlen;
748         top->m_pkthdr.rcvif = ifp;
749         
750         /*
751          * Ethernet bridge and bpf are handled in ether_input
752          *
753          * adjust mbuf and give packet to the ether_input
754          */
755
756         (*ifp->if_input)(ifp, top);
757         ifp->if_ipackets ++; /* ibytes are counted in ether_input */
758
759         return (0);
760 } /* tapwrite */
761
762
763 /*
764  * tappoll
765  *
766  * the poll interface, this is only useful on reads
767  * really. the write detect always returns true, write never blocks
768  * anyway, it either accepts the packet or drops it
769  */
770 static int
771 tappoll(dev_t dev, int events, d_thread_t *td)
772 {
773         struct tap_softc        *tp = dev->si_drv1;
774         struct ifnet            *ifp = &tp->tap_if;
775         int                      revents = 0;
776
777         TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
778
779         crit_enter();
780
781         if (events & (POLLIN | POLLRDNORM)) {
782                 if (ifp->if_snd.ifq_len > 0) {
783                         TAPDEBUG(ifp,
784                                  "has data in queue. len = %d, minor = %#x\n",
785                                  ifp->if_snd.ifq_len, minor(tp->tap_dev));
786
787                         revents |= (events & (POLLIN | POLLRDNORM));
788                 } 
789                 else {
790                         TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
791                                  minor(tp->tap_dev));
792
793                         selrecord(td, &tp->tap_rsel);
794                 }
795         }
796
797         if (events & (POLLOUT | POLLWRNORM))
798                 revents |= (events & (POLLOUT | POLLWRNORM));
799
800         crit_exit();
801         return (revents);
802 } /* tappoll */