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