proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[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.3 2003/06/23 17:55:45 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/ttycom.h>
56 #include <sys/uio.h>
57 #include <sys/vnode.h>
58
59 #include <net/bpf.h>
60 #include <net/ethernet.h>
61 #include <net/if.h>
62 #include <net/if_arp.h>
63 #include <net/route.h>
64
65 #include <netinet/in.h>
66
67 #include <net/if_tapvar.h>
68 #include <net/if_tap.h>
69
70
71 #define CDEV_NAME       "tap"
72 #define CDEV_MAJOR      149
73 #define TAPDEBUG        if (tapdebug) printf
74
75 #define TAP             "tap"
76 #define VMNET           "vmnet"
77 #define VMNET_DEV_MASK  0x00010000
78
79 /* module */
80 static int              tapmodevent     __P((module_t, int, void *));
81
82 /* device */
83 static void             tapcreate       __P((dev_t));
84
85 /* network interface */
86 static void             tapifstart      __P((struct ifnet *));
87 static int              tapifioctl      __P((struct ifnet *, u_long, caddr_t));
88 static void             tapifinit       __P((void *));
89
90 /* character device */
91 static d_open_t         tapopen;
92 static d_close_t        tapclose;
93 static d_read_t         tapread;
94 static d_write_t        tapwrite;
95 static d_ioctl_t        tapioctl;
96 static d_poll_t         tappoll;
97
98 static struct cdevsw    tap_cdevsw = {
99         /* open */      tapopen,
100         /* close */     tapclose,
101         /* read */      tapread,
102         /* write */     tapwrite,
103         /* ioctl */     tapioctl,
104         /* poll */      tappoll,
105         /* mmap */      nommap,
106         /* startegy */  nostrategy,
107         /* dev name */  CDEV_NAME,
108         /* dev major */ CDEV_MAJOR,
109         /* dump */      nodump,
110         /* psize */     nopsize,
111         /* flags */     0,
112         /* bmaj */      -1
113 };
114
115 static int              taprefcnt = 0;          /* module ref. counter   */
116 static int              taplastunit = -1;       /* max. open unit number */
117 static int              tapdebug = 0;           /* debug flag            */
118
119 MALLOC_DECLARE(M_TAP);
120 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
121 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
122 DEV_MODULE(if_tap, tapmodevent, NULL);
123
124 /*
125  * tapmodevent
126  *
127  * module event handler
128  */
129 static int
130 tapmodevent(mod, type, data)
131         module_t         mod;
132         int              type;
133         void            *data;
134 {
135         static int               attached = 0;
136         struct ifnet            *ifp = NULL;
137         int                      unit, s;
138
139         switch (type) {
140         case MOD_LOAD:
141                 if (attached)
142                         return (EEXIST);
143
144                 cdevsw_add(&tap_cdevsw);
145                 attached = 1;
146         break;
147
148         case MOD_UNLOAD:
149                 if (taprefcnt > 0)
150                         return (EBUSY);
151
152                 cdevsw_remove(&tap_cdevsw);
153
154                 unit = 0;
155                 while (unit <= taplastunit) {
156                         s = splimp();
157                         TAILQ_FOREACH(ifp, &ifnet, if_link)
158                                 if ((strcmp(ifp->if_name, TAP) == 0) ||
159                                     (strcmp(ifp->if_name, VMNET) == 0))
160                                         if (ifp->if_unit == unit)
161                                                 break;
162                         splx(s);
163
164                         if (ifp != NULL) {
165                                 struct tap_softc        *tp = ifp->if_softc;
166
167                                 TAPDEBUG("detaching %s%d. minor = %#x, " \
168                                         "taplastunit = %d\n",
169                                         ifp->if_name, unit, minor(tp->tap_dev),
170                                         taplastunit);
171
172                                 s = splimp();
173                                 ether_ifdetach(ifp, 1);
174                                 splx(s);
175                                 destroy_dev(tp->tap_dev);
176                                 free(tp, M_TAP);
177                         }
178                         else
179                                 unit ++;
180                 }
181
182                 attached = 0;
183         break;
184
185         default:
186                 return (EOPNOTSUPP);
187         }
188
189         return (0);
190 } /* tapmodevent */
191
192
193 /*
194  * tapcreate
195  *
196  * to create interface
197  */
198 static void
199 tapcreate(dev)
200         dev_t   dev;
201 {
202         struct ifnet            *ifp = NULL;
203         struct tap_softc        *tp = NULL;
204         unsigned short           macaddr_hi;
205         int                      unit, s;
206         char                    *name = NULL;
207
208         /* allocate driver storage and create device */
209         MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
210         bzero(tp, sizeof(*tp));
211
212         /* select device: tap or vmnet */
213         if (minor(dev) & VMNET_DEV_MASK) {
214                 name = VMNET;
215                 unit = lminor(dev) & 0xff;
216                 tp->tap_flags |= TAP_VMNET;
217         }
218         else {
219                 name = TAP;
220                 unit = lminor(dev);
221         }
222
223         tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 
224                                                 0600, "%s%d", name, unit);
225         tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
226
227         /* generate fake MAC address: 00 bd xx xx xx unit_no */
228         macaddr_hi = htons(0x00bd);
229         bcopy(&macaddr_hi, &tp->arpcom.ac_enaddr[0], sizeof(short));
230         bcopy(&ticks, &tp->arpcom.ac_enaddr[2], sizeof(long));
231         tp->arpcom.ac_enaddr[5] = (u_char)unit;
232
233         /* fill the rest and attach interface */        
234         ifp = &tp->tap_if;
235         ifp->if_softc = tp;
236
237         ifp->if_unit = unit;
238         if (unit > taplastunit)
239                 taplastunit = unit;
240
241         ifp->if_name = name;
242         ifp->if_init = tapifinit;
243         ifp->if_output = ether_output;
244         ifp->if_start = tapifstart;
245         ifp->if_ioctl = tapifioctl;
246         ifp->if_mtu = ETHERMTU;
247         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
248         ifp->if_snd.ifq_maxlen = ifqmaxlen;
249
250         s = splimp();
251         ether_ifattach(ifp, 1);
252         splx(s);
253
254         tp->tap_flags |= TAP_INITED;
255
256         TAPDEBUG("interface %s%d created. minor = %#x\n",
257                         ifp->if_name, ifp->if_unit, 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         struct proc *p = td->td_proc;
272
273         KKASSERT(p != NULL);
274
275         if ((error = suser_xxx(p->p_ucred, 0)) != 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_pid = p->p_pid;
290         tp->tap_flags |= TAP_OPEN;
291         taprefcnt ++;
292
293         TAPDEBUG("%s%d is open. minor = %#x, refcnt = %d, taplastunit = %d\n",
294                 tp->tap_if.if_name, tp->tap_if.if_unit,
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         int                      s;
310         struct tap_softc        *tp = dev->si_drv1;
311         struct ifnet            *ifp = &tp->tap_if;
312         struct mbuf             *m = NULL;
313
314         /* junk all pending output */
315
316         s = splimp();
317         do {
318                 IF_DEQUEUE(&ifp->if_snd, m);
319                 if (m != NULL)
320                         m_freem(m);
321         } while (m != NULL);
322         splx(s);
323
324         /*
325          * do not bring the interface down, and do not anything with
326          * interface, if we are in VMnet mode. just close the device.
327          */
328
329         if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
330                 s = splimp();
331                 if_down(ifp);
332                 if (ifp->if_flags & IFF_RUNNING) {
333                         /* find internet addresses and delete routes */
334                         struct ifaddr   *ifa = NULL;
335
336                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
337                                 if (ifa->ifa_addr->sa_family == AF_INET) {
338                                         rtinit(ifa, (int)RTM_DELETE, 0);
339
340                                         /* remove address from interface */
341                                         bzero(ifa->ifa_addr, 
342                                                    sizeof(*(ifa->ifa_addr)));
343                                         bzero(ifa->ifa_dstaddr, 
344                                                    sizeof(*(ifa->ifa_dstaddr)));
345                                         bzero(ifa->ifa_netmask, 
346                                                    sizeof(*(ifa->ifa_netmask)));
347                                 }
348                         }
349
350                         ifp->if_flags &= ~IFF_RUNNING;
351                 }
352                 splx(s);
353         }
354
355         funsetown(tp->tap_sigio);
356         selwakeup(&tp->tap_rsel);
357
358         tp->tap_flags &= ~TAP_OPEN;
359         tp->tap_pid = 0;
360
361         taprefcnt --;
362         if (taprefcnt < 0) {
363                 taprefcnt = 0;
364                 printf("%s%d minor = %#x, refcnt = %d is out of sync. " \
365                         "set refcnt to 0\n", ifp->if_name, ifp->if_unit, 
366                         minor(tp->tap_dev), taprefcnt);
367         }
368
369         TAPDEBUG("%s%d is closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
370                         ifp->if_name, ifp->if_unit, minor(tp->tap_dev),
371                         taprefcnt, taplastunit);
372
373         return (0);
374 } /* tapclose */
375
376
377 /*
378  * tapifinit
379  *
380  * network interface initialization function
381  */
382 static void
383 tapifinit(xtp)
384         void    *xtp;
385 {
386         struct tap_softc        *tp = (struct tap_softc *)xtp;
387         struct ifnet            *ifp = &tp->tap_if;
388
389         TAPDEBUG("initializing %s%d, minor = %#x\n",
390                         ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
391
392         ifp->if_flags |= IFF_RUNNING;
393         ifp->if_flags &= ~IFF_OACTIVE;
394
395         /* attempt to start output */
396         tapifstart(ifp);
397 } /* tapifinit */
398
399
400 /*
401  * tapifioctl
402  *
403  * Process an ioctl request on network interface
404  */
405 int
406 tapifioctl(ifp, cmd, data)
407         struct ifnet    *ifp;
408         u_long           cmd;
409         caddr_t          data;
410 {
411         struct tap_softc        *tp = (struct tap_softc *)(ifp->if_softc);
412         struct ifstat           *ifs = NULL;
413         int                      s, dummy;
414
415         switch (cmd) {
416                 case SIOCSIFADDR:
417                 case SIOCGIFADDR:
418                 case SIOCSIFMTU:
419                         s = splimp();
420                         dummy = ether_ioctl(ifp, cmd, data);
421                         splx(s);
422                         return (dummy);
423
424                 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
425                 case SIOCADDMULTI:
426                 case SIOCDELMULTI:
427                 break;
428
429                 case SIOCGIFSTATUS:
430                         s = splimp();
431                         ifs = (struct ifstat *)data;
432                         dummy = strlen(ifs->ascii);
433                         if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
434                                 snprintf(ifs->ascii + dummy,
435                                         sizeof(ifs->ascii) - dummy,
436                                         "\tOpened by PID %d\n", tp->tap_pid);
437                         splx(s);
438                 break;
439
440                 default:
441                         return (EINVAL);
442         }
443
444         return (0);
445 } /* tapifioctl */
446
447
448 /*
449  * tapifstart 
450  * 
451  * queue packets from higher level ready to put out
452  */
453 static void
454 tapifstart(ifp)
455         struct ifnet    *ifp;
456 {
457         struct tap_softc        *tp = ifp->if_softc;
458         int                      s;
459
460         TAPDEBUG("%s%d starting, minor = %#x\n", 
461                         ifp->if_name, ifp->if_unit, 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("%s%d not ready. minor = %#x, tap_flags = 0x%x\n",
473                         ifp->if_name, ifp->if_unit,
474                         minor(tp->tap_dev), tp->tap_flags);
475
476                 s = splimp();
477                 do {
478                         IF_DEQUEUE(&ifp->if_snd, m);
479                         if (m != NULL)
480                                 m_freem(m);
481                         ifp->if_oerrors ++;
482                 } while (m != NULL);
483                 splx(s);
484
485                 return;
486         }
487
488         s = splimp();
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         splx(s);
506 } /* tapifstart */
507
508
509 /*
510  * tapioctl
511  *
512  * the cdevsw interface is now pretty minimal
513  */
514 static int
515 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
516 {
517         struct tap_softc        *tp = dev->si_drv1;
518         struct ifnet            *ifp = &tp->tap_if;
519         struct tapinfo          *tapp = NULL;
520         int                      s;
521
522         switch (cmd) {
523                 case TAPSIFINFO:
524                         s = splimp();
525                         tapp = (struct tapinfo *)data;
526                         ifp->if_mtu = tapp->mtu;
527                         ifp->if_type = tapp->type;
528                         ifp->if_baudrate = tapp->baudrate;
529                         splx(s);
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                         s = splimp();
552                         if (*(int *)data)
553                                 tp->tap_flags |= TAP_ASYNC;
554                         else
555                                 tp->tap_flags &= ~TAP_ASYNC;
556                         splx(s);
557                 break;
558
559                 case FIONREAD:
560                         s = splimp();
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                         splx(s);
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                         s = splimp();
602                         ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
603                         splx(s);
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, s;
638
639         TAPDEBUG("%s%d reading, minor = %#x\n",
640                         ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
641
642         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
643                 TAPDEBUG("%s%d not ready. minor = %#x, tap_flags = 0x%x\n",
644                                 ifp->if_name, ifp->if_unit, 
645                                 minor(tp->tap_dev), tp->tap_flags);
646
647                 return (EHOSTDOWN);
648         }
649
650         tp->tap_flags &= ~TAP_RWAIT;
651
652         /* sleep until we get a packet */
653         do {
654                 s = splimp();
655                 IF_DEQUEUE(&ifp->if_snd, m0);
656                 splx(s);
657
658                 if (m0 == NULL) {
659                         if (flag & IO_NDELAY)
660                                 return (EWOULDBLOCK);
661                         
662                         tp->tap_flags |= TAP_RWAIT;
663                         error = tsleep((caddr_t)tp,PCATCH|(PZERO+1),"taprd",0);
664                         if (error)
665                                 return (error);
666                 }
667         } while (m0 == NULL);
668
669         /* feed packet to bpf */
670         if (ifp->if_bpf != NULL)
671                 bpf_mtap(ifp, m0);
672
673         /* xfer packet to user space */
674         while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
675                 len = min(uio->uio_resid, m0->m_len);
676                 if (len == 0)
677                         break;
678
679                 error = uiomove(mtod(m0, caddr_t), len, uio);
680                 m0 = m_free(m0);
681         }
682
683         if (m0 != NULL) {
684                 TAPDEBUG("%s%d dropping mbuf, minor = %#x\n",
685                                 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
686                 m_freem(m0);
687         }
688
689         return (error);
690 } /* tapread */
691
692
693 /*
694  * tapwrite
695  *
696  * the cdevsw write interface - an atomic write is a packet - or else!
697  */
698 static int
699 tapwrite(dev, uio, flag)
700         dev_t            dev;
701         struct uio      *uio;
702         int              flag;
703 {
704         struct tap_softc        *tp = dev->si_drv1;
705         struct ifnet            *ifp = &tp->tap_if;
706         struct mbuf             *top = NULL, **mp = NULL, *m = NULL;
707         struct ether_header     *eh = NULL;
708         int                      error = 0, tlen, mlen;
709
710         TAPDEBUG("%s%d writting, minor = %#x\n",
711                                 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
712
713         if (uio->uio_resid == 0)
714                 return (0);
715
716         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
717                 TAPDEBUG("%s%d invalid packet len = %d, minor = %#x\n",
718                                 ifp->if_name, ifp->if_unit, 
719                                 uio->uio_resid, minor(tp->tap_dev));
720
721                 return (EIO);
722         }
723         tlen = uio->uio_resid;
724
725         /* get a header mbuf */
726         MGETHDR(m, M_DONTWAIT, MT_DATA);
727         if (m == NULL)
728                 return (ENOBUFS);
729         mlen = MHLEN;
730
731         top = 0;
732         mp = &top;
733         while ((error == 0) && (uio->uio_resid > 0)) {
734                 m->m_len = min(mlen, uio->uio_resid);
735                 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
736                 *mp = m;
737                 mp = &m->m_next;
738                 if (uio->uio_resid > 0) {
739                         MGET(m, M_DONTWAIT, MT_DATA);
740                         if (m == NULL) {
741                                 error = ENOBUFS;
742                                 break;
743                         }
744                         mlen = MLEN;
745                 }
746         }
747         if (error) {
748                 ifp->if_ierrors ++;
749                 if (top)
750                         m_freem(top);
751                 return (error);
752         }
753
754         top->m_pkthdr.len = tlen;
755         top->m_pkthdr.rcvif = ifp;
756         
757         /*
758          * Ethernet bridge and bpf are handled in ether_input
759          *
760          * adjust mbuf and give packet to the ether_input
761          */
762
763         eh = mtod(top, struct ether_header *);
764         m_adj(top, sizeof(struct ether_header));
765         ether_input(ifp, eh, top);
766         ifp->if_ipackets ++; /* ibytes are counted in ether_input */
767
768         return (0);
769 } /* tapwrite */
770
771
772 /*
773  * tappoll
774  *
775  * the poll interface, this is only useful on reads
776  * really. the write detect always returns true, write never blocks
777  * anyway, it either accepts the packet or drops it
778  */
779 static int
780 tappoll(dev_t dev, int events, d_thread_t *td)
781 {
782         struct tap_softc        *tp = dev->si_drv1;
783         struct ifnet            *ifp = &tp->tap_if;
784         int                      s, revents = 0;
785
786         TAPDEBUG("%s%d polling, minor = %#x\n",
787                                 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
788
789         s = splimp();
790         if (events & (POLLIN | POLLRDNORM)) {
791                 if (ifp->if_snd.ifq_len > 0) {
792                         TAPDEBUG("%s%d have data in queue. len = %d, " \
793                                 "minor = %#x\n", ifp->if_name, ifp->if_unit,
794                                 ifp->if_snd.ifq_len, minor(tp->tap_dev));
795
796                         revents |= (events & (POLLIN | POLLRDNORM));
797                 } 
798                 else {
799                         TAPDEBUG("%s%d waiting for data, minor = %#x\n",
800                                 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
801
802                         selrecord(td, &tp->tap_rsel);
803                 }
804         }
805
806         if (events & (POLLOUT | POLLWRNORM))
807                 revents |= (events & (POLLOUT | POLLWRNORM));
808
809         splx(s);
810         return (revents);
811 } /* tappoll */