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