Merge from vendor branch GCC:
[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.40 2008/05/18 05:12:08 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 static void             tapifstop(struct tap_softc *, int);
95
96 /* character device */
97 static d_open_t         tapopen;
98 static d_close_t        tapclose;
99 static d_read_t         tapread;
100 static d_write_t        tapwrite;
101 static d_ioctl_t        tapioctl;
102 static d_poll_t         tappoll;
103 static d_kqfilter_t     tapkqfilter;
104
105 static struct dev_ops   tap_ops = {
106         { CDEV_NAME, CDEV_MAJOR, 0 },
107         .d_open =       tapopen,
108         .d_close =      tapclose,
109         .d_read =       tapread,
110         .d_write =      tapwrite,
111         .d_ioctl =      tapioctl,
112         .d_poll =       tappoll,
113         .d_kqfilter =   tapkqfilter
114 };
115
116 static int              taprefcnt = 0;          /* module ref. counter   */
117 static int              taplastunit = -1;       /* max. open unit number */
118 static int              tapdebug = 0;           /* debug flag            */
119
120 MALLOC_DECLARE(M_TAP);
121 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
122 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
123 DEV_MODULE(if_tap, tapmodevent, NULL);
124
125 /*
126  * tapmodevent
127  *
128  * module event handler
129  */
130 static int
131 tapmodevent(module_t mod, int type, void *data)
132 {
133         static int               attached = 0;
134         struct ifnet            *ifp = NULL;
135         int                      unit;
136
137         switch (type) {
138         case MOD_LOAD:
139                 if (attached)
140                         return (EEXIST);
141
142                 dev_ops_add(&tap_ops, 0, 0);
143                 attached = 1;
144                 break;
145
146         case MOD_UNLOAD:
147                 if (taprefcnt > 0)
148                         return (EBUSY);
149
150                 dev_ops_remove(&tap_ops, 0, 0);
151
152                 /* XXX: maintain tap ifs in a local list */
153                 unit = 0;
154                 while (unit <= taplastunit) {
155                         TAILQ_FOREACH(ifp, &ifnet, if_link) {
156                                 if ((strcmp(ifp->if_dname, TAP) == 0) ||
157                                     (strcmp(ifp->if_dname, VMNET) == 0)) {
158                                         if (ifp->if_dunit == unit)
159                                                 break;
160                                 }
161                         }
162
163                         if (ifp != NULL) {
164                                 struct tap_softc        *tp = ifp->if_softc;
165
166                                 TAPDEBUG(ifp, "detached. minor = %#x, " \
167                                         "taplastunit = %d\n",
168                                         minor(tp->tap_dev), taplastunit);
169
170                                 lwkt_serialize_enter(ifp->if_serializer);
171                                 tapifstop(tp, 1);
172                                 lwkt_serialize_exit(ifp->if_serializer);
173
174                                 ether_ifdetach(ifp);
175                                 destroy_dev(tp->tap_dev);
176                                 kfree(tp, M_TAP);
177                         } else {
178                                 unit++;
179                         }
180                 }
181                 attached = 0;
182                 break;
183
184         default:
185                 return (EOPNOTSUPP);
186         }
187
188         return (0);
189 } /* tapmodevent */
190
191
192 /*
193  * tapcreate
194  *
195  * to create interface
196  */
197 static void
198 tapcreate(cdev_t dev)
199 {
200         struct ifnet            *ifp = NULL;
201         struct tap_softc        *tp = NULL;
202         uint8_t                 ether_addr[ETHER_ADDR_LEN];
203         int                      unit;
204         char                    *name = NULL;
205
206         /* allocate driver storage and create device */
207         MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
208
209         /* select device: tap or vmnet */
210         if (minor(dev) & VMNET_DEV_MASK) {
211                 name = VMNET;
212                 unit = lminor(dev) & 0xff;
213                 tp->tap_flags |= TAP_VMNET;
214         }
215         else {
216                 name = TAP;
217                 unit = lminor(dev);
218         }
219
220         tp->tap_dev = make_dev(&tap_ops, minor(dev), UID_ROOT, GID_WHEEL, 
221                                                 0600, "%s%d", name, unit);
222         tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
223         reference_dev(tp->tap_dev);     /* so we can destroy it later */
224
225         /* generate fake MAC address: 00 bd xx xx xx unit_no */
226         ether_addr[0] = 0x00;
227         ether_addr[1] = 0xbd;
228         bcopy(&ticks, &ether_addr[2], 3);
229         ether_addr[5] = (u_char)unit;
230
231         /* fill the rest and attach interface */        
232         ifp = &tp->tap_if;
233         ifp->if_softc = tp;
234
235         if_initname(ifp, name, unit);
236         if (unit > taplastunit)
237                 taplastunit = unit;
238
239         ifp->if_init = tapifinit;
240         ifp->if_start = tapifstart;
241         ifp->if_ioctl = tapifioctl;
242         ifp->if_mtu = ETHERMTU;
243         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
244         ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
245         ifq_set_ready(&ifp->if_snd);
246
247         ether_ifattach(ifp, ether_addr, NULL);
248
249         tp->tap_flags |= TAP_INITED;
250         tp->tap_devq.ifq_maxlen = ifqmaxlen;
251
252         TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
253 } /* tapcreate */
254
255
256 /*
257  * tapopen 
258  *
259  * to open tunnel. must be superuser
260  */
261 static int
262 tapopen(struct dev_open_args *ap)
263 {
264         cdev_t dev = ap->a_head.a_dev;
265         struct tap_softc *tp = NULL;
266         struct ifnet *ifp = NULL;
267         int error;
268
269         if ((error = suser_cred(ap->a_cred, 0)) != 0)
270                 return (error);
271
272         get_mplock();
273         tp = dev->si_drv1;
274         if (tp == NULL) {
275                 tapcreate(dev);
276                 tp = dev->si_drv1;
277                 ifp = &tp->arpcom.ac_if;
278         } else {
279                 if (tp->tap_flags & TAP_OPEN) {
280                         rel_mplock();
281                         return (EBUSY);
282                 }
283
284                 ifp = &tp->arpcom.ac_if;
285
286                 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
287
288                 /* Announce the return of the interface. */
289                 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
290         }
291
292         bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
293
294         tp->tap_td = curthread;
295         tp->tap_flags |= TAP_OPEN;
296         taprefcnt ++;
297
298         TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
299                  minor(tp->tap_dev), taprefcnt, taplastunit);
300
301         rel_mplock();
302         return (0);
303 }
304
305
306 /*
307  * tapclose
308  *
309  * close the device - mark i/f down & delete routing info
310  */
311 static int
312 tapclose(struct dev_close_args *ap)
313 {
314         cdev_t dev = ap->a_head.a_dev;
315         struct tap_softc        *tp = dev->si_drv1;
316         struct ifnet            *ifp = &tp->tap_if;
317         int clear_flags = 1;
318
319         /* junk all pending output */
320
321         get_mplock();
322         ifq_purge(&ifp->if_snd);
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) {
330                 if (ifp->if_flags & IFF_UP)
331                         if_down(ifp);
332                 clear_flags = 0;
333         }
334         lwkt_serialize_enter(ifp->if_serializer);
335         tapifstop(tp, clear_flags);
336         lwkt_serialize_exit(ifp->if_serializer);
337
338         if_purgeaddrs_nolink(ifp);
339
340         EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
341
342         /* Announce the departure of the interface. */
343         rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
344
345         funsetown(tp->tap_sigio);
346         selwakeup(&tp->tap_rsel);
347
348         tp->tap_flags &= ~TAP_OPEN;
349         tp->tap_td = NULL;
350
351         taprefcnt --;
352         if (taprefcnt < 0) {
353                 taprefcnt = 0;
354                 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
355                         "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
356         }
357
358         TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
359                  minor(tp->tap_dev), taprefcnt, taplastunit);
360
361         rel_mplock();
362         return (0);
363 }
364
365
366 /*
367  * tapifinit
368  *
369  * Network interface initialization function (called with if serializer held)
370  *
371  * MPSAFE
372  */
373 static void
374 tapifinit(void *xtp)
375 {
376         struct tap_softc *tp = xtp;
377         struct ifnet *ifp = &tp->tap_if;
378
379         TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
380
381         ASSERT_SERIALIZED(ifp->if_serializer);
382
383         tapifstop(tp, 1);
384
385         ifp->if_flags |= IFF_RUNNING;
386         ifp->if_flags &= ~IFF_OACTIVE;
387
388         /* attempt to start output */
389         tapifstart(ifp);
390 }
391
392
393 /*
394  * tapifioctl
395  *
396  * Process an ioctl request on network interface (called with if serializer
397  * held).
398  *
399  * MPSAFE
400  */
401 static int
402 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
403 {
404         struct tap_softc        *tp = (struct tap_softc *)(ifp->if_softc);
405         struct ifstat           *ifs = NULL;
406         int                      dummy;
407
408         switch (cmd) {
409                 case SIOCSIFADDR:
410                 case SIOCGIFADDR:
411                 case SIOCSIFMTU:
412                         dummy = ether_ioctl(ifp, cmd, data);
413                         return (dummy);
414
415                 case SIOCSIFFLAGS:
416                         if ((tp->tap_flags & TAP_VMNET) == 0) {
417                                 /*
418                                  * Only for non-vmnet tap(4)
419                                  */
420                                 if (ifp->if_flags & IFF_UP) {
421                                         if ((ifp->if_flags & IFF_RUNNING) == 0)
422                                                 tapifinit(tp);
423                                 } else {
424                                         tapifstop(tp, 1);
425                                 }
426                         } else {
427                                 /* XXX */
428                         }
429                         break;
430                 case SIOCADDMULTI: /* XXX -- just like vmnet does */
431                 case SIOCDELMULTI:
432                         break;
433
434                 case SIOCGIFSTATUS:
435                         ifs = (struct ifstat *)data;
436                         dummy = strlen(ifs->ascii);
437                         if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
438                                 if (tp->tap_td->td_proc) {
439                                     ksnprintf(ifs->ascii + dummy,
440                                         sizeof(ifs->ascii) - dummy,
441                                         "\tOpened by pid %d\n",
442                                         (int)tp->tap_td->td_proc->p_pid);
443                                 } else {
444                                     ksnprintf(ifs->ascii + dummy,
445                                         sizeof(ifs->ascii) - dummy,
446                                         "\tOpened by td %p\n", tp->tap_td);
447                                 }
448                         }
449                         break;
450
451                 default:
452                         return (EINVAL);
453         }
454
455         return (0);
456 }
457
458
459 /*
460  * tapifstart 
461  * 
462  * Queue packets from higher level ready to put out (called with if serializer
463  * held)
464  *
465  * MPSAFE
466  */
467 static void
468 tapifstart(struct ifnet *ifp)
469 {
470         struct tap_softc *tp = ifp->if_softc;
471         struct ifqueue *ifq;
472         struct mbuf *m;
473         int has_data = 0;
474
475         TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
476
477         /*
478          * do not junk pending output if we are in VMnet mode.
479          * XXX: can this do any harm because of queue overflow?
480          */
481
482         if (((tp->tap_flags & TAP_VMNET) == 0) && 
483             ((tp->tap_flags & TAP_READY) != TAP_READY)) {
484                 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
485                          minor(tp->tap_dev), tp->tap_flags);
486                 ifq_purge(&ifp->if_snd);
487                 return;
488         }
489
490         ifp->if_flags |= IFF_OACTIVE;
491
492         ifq = &tp->tap_devq;
493         while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) {
494                 if (IF_QFULL(ifq)) {
495                         IF_DROP(ifq);
496                         ifp->if_oerrors++;
497                         m_freem(m);
498                 } else {
499                         IF_ENQUEUE(ifq, m);
500                         ifp->if_opackets++;
501                         has_data = 1;
502                 }
503         }
504
505         if (has_data) {
506                 if (tp->tap_flags & TAP_RWAIT) {
507                         tp->tap_flags &= ~TAP_RWAIT;
508                         wakeup((caddr_t)tp);
509                 }
510
511                 get_mplock();
512                 KNOTE(&tp->tap_rsel.si_note, 0);
513                 rel_mplock();
514
515                 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
516                         get_mplock();
517                         pgsigio(tp->tap_sigio, SIGIO, 0);
518                         rel_mplock();
519                 }
520
521                 /*
522                  * selwakeup is not MPSAFE.  tapifstart is.
523                  */
524                 get_mplock();
525                 selwakeup(&tp->tap_rsel);
526                 rel_mplock();
527         }
528
529         ifp->if_flags &= ~IFF_OACTIVE;
530 }
531
532
533 /*
534  * tapioctl
535  *
536  * The ops interface is now pretty minimal.  Called via fileops with nothing
537  * held.
538  *
539  * MPSAFE
540  */
541 static int
542 tapioctl(struct dev_ioctl_args *ap)
543 {
544         cdev_t dev = ap->a_head.a_dev;
545         caddr_t data = ap->a_data;
546         struct tap_softc        *tp = dev->si_drv1;
547         struct ifnet            *ifp = &tp->tap_if;
548         struct tapinfo          *tapp = NULL;
549         struct mbuf *mb;
550         short f;
551         int error;
552
553         lwkt_serialize_enter(ifp->if_serializer);
554         error = 0;
555
556         switch (ap->a_cmd) {
557         case TAPSIFINFO:
558                 tapp = (struct tapinfo *)data;
559                 ifp->if_mtu = tapp->mtu;
560                 ifp->if_type = tapp->type;
561                 ifp->if_baudrate = tapp->baudrate;
562                 break;
563
564         case TAPGIFINFO:
565                 tapp = (struct tapinfo *)data;
566                 tapp->mtu = ifp->if_mtu;
567                 tapp->type = ifp->if_type;
568                 tapp->baudrate = ifp->if_baudrate;
569                 break;
570
571         case TAPSDEBUG:
572                 tapdebug = *(int *)data;
573                 break;
574
575         case TAPGDEBUG:
576                 *(int *)data = tapdebug;
577                 break;
578
579         case FIOASYNC:
580                 if (*(int *)data)
581                         tp->tap_flags |= TAP_ASYNC;
582                 else
583                         tp->tap_flags &= ~TAP_ASYNC;
584                 break;
585
586         case FIONREAD:
587                 *(int *)data = 0;
588
589                 /* Take a look at devq first */
590                 IF_POLL(&tp->tap_devq, mb);
591                 if (mb == NULL)
592                         mb = ifq_poll(&ifp->if_snd);
593
594                 if (mb != NULL) {
595                         for(; mb != NULL; mb = mb->m_next)
596                                 *(int *)data += mb->m_len;
597                 } 
598                 break;
599
600         case FIOSETOWN:
601                 error = fsetown(*(int *)data, &tp->tap_sigio);
602                 break;
603
604         case FIOGETOWN:
605                 *(int *)data = fgetown(tp->tap_sigio);
606                 break;
607
608         /* this is deprecated, FIOSETOWN should be used instead */
609         case TIOCSPGRP:
610                 error = fsetown(-(*(int *)data), &tp->tap_sigio);
611                 break;
612
613         /* this is deprecated, FIOGETOWN should be used instead */
614         case TIOCGPGRP:
615                 *(int *)data = -fgetown(tp->tap_sigio);
616                 break;
617
618         /* VMware/VMnet port ioctl's */
619
620         case SIOCGIFFLAGS:      /* get ifnet flags */
621                 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
622                 break;
623
624         case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
625                 f = *(short *)data;
626                 f &= 0x0fff;
627                 f &= ~IFF_CANTCHANGE;
628                 f |= IFF_UP;
629                 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
630                 break;
631
632         case OSIOCGIFADDR:      /* get MAC address of the remote side */
633         case SIOCGIFADDR:
634                 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
635                 break;
636
637         case SIOCSIFADDR:       /* set MAC address of the remote side */
638                 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
639                 break;
640
641         default:
642                 error = ENOTTY;
643                 break;
644         }
645         lwkt_serialize_exit(ifp->if_serializer);
646         return (error);
647 }
648
649
650 /*
651  * tapread
652  *
653  * The ops read interface - reads a packet at a time, or at
654  * least as much of a packet as can be read.
655  *
656  * Called from the fileops interface with nothing held.
657  *
658  * MPSAFE
659  */
660 static int
661 tapread(struct dev_read_args *ap)
662 {
663         cdev_t dev = ap->a_head.a_dev;
664         struct uio *uio = ap->a_uio;
665         struct tap_softc        *tp = dev->si_drv1;
666         struct ifnet            *ifp = &tp->tap_if;
667         struct mbuf             *m0 = NULL;
668         int                      error = 0, len;
669
670         TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
671
672         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
673                 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
674                          minor(tp->tap_dev), tp->tap_flags);
675
676                 return (EHOSTDOWN);
677         }
678
679         tp->tap_flags &= ~TAP_RWAIT;
680
681         /* sleep until we get a packet */
682         do {
683                 lwkt_serialize_enter(ifp->if_serializer);
684                 IF_DEQUEUE(&tp->tap_devq, m0);
685                 if (m0 == NULL) {
686                         if (ap->a_ioflag & IO_NDELAY) {
687                                 lwkt_serialize_exit(ifp->if_serializer);
688                                 return (EWOULDBLOCK);
689                         }
690                         tp->tap_flags |= TAP_RWAIT;
691                         crit_enter();
692                         tsleep_interlock(tp);
693                         lwkt_serialize_exit(ifp->if_serializer);
694                         error = tsleep(tp, PCATCH, "taprd", 0);
695                         crit_exit();
696                         if (error)
697                                 return (error);
698                 } else {
699                         lwkt_serialize_exit(ifp->if_serializer);
700                 }
701         } while (m0 == NULL);
702
703         BPF_MTAP(ifp, m0);
704
705         /* xfer packet to user space */
706         while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
707                 len = min(uio->uio_resid, m0->m_len);
708                 if (len == 0)
709                         break;
710
711                 error = uiomove(mtod(m0, caddr_t), len, uio);
712                 m0 = m_free(m0);
713         }
714
715         if (m0 != NULL) {
716                 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
717                          minor(tp->tap_dev));
718                 m_freem(m0);
719         }
720
721         return (error);
722 }
723
724 /*
725  * tapwrite
726  *
727  * The ops write interface - an atomic write is a packet - or else!
728  *
729  * Called from the fileops interface with nothing held.
730  *
731  * MPSAFE
732  */
733 static int
734 tapwrite(struct dev_write_args *ap)
735 {
736         cdev_t dev = ap->a_head.a_dev;
737         struct uio *uio = ap->a_uio;
738         struct tap_softc        *tp = dev->si_drv1;
739         struct ifnet            *ifp = &tp->tap_if;
740         struct mbuf             *top = NULL, **mp = NULL, *m = NULL;
741         int                      error = 0, tlen, mlen;
742
743         TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
744
745         if (uio->uio_resid == 0)
746                 return (0);
747
748         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
749                 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
750                          uio->uio_resid, minor(tp->tap_dev));
751
752                 return (EIO);
753         }
754         tlen = uio->uio_resid;
755
756         /* get a header mbuf */
757         MGETHDR(m, MB_DONTWAIT, MT_DATA);
758         if (m == NULL)
759                 return (ENOBUFS);
760         mlen = MHLEN;
761
762         top = 0;
763         mp = &top;
764         while ((error == 0) && (uio->uio_resid > 0)) {
765                 m->m_len = min(mlen, uio->uio_resid);
766                 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
767                 *mp = m;
768                 mp = &m->m_next;
769                 if (uio->uio_resid > 0) {
770                         MGET(m, MB_DONTWAIT, MT_DATA);
771                         if (m == NULL) {
772                                 error = ENOBUFS;
773                                 break;
774                         }
775                         mlen = MLEN;
776                 }
777         }
778         if (error) {
779                 ifp->if_ierrors ++;
780                 if (top)
781                         m_freem(top);
782                 return (error);
783         }
784
785         top->m_pkthdr.len = tlen;
786         top->m_pkthdr.rcvif = ifp;
787         
788         /*
789          * Ethernet bridge and bpf are handled in ether_input
790          *
791          * adjust mbuf and give packet to the ether_input
792          */
793         lwkt_serialize_enter(ifp->if_serializer);
794         ifp->if_input(ifp, top);
795         ifp->if_ipackets ++; /* ibytes are counted in ether_input */
796         lwkt_serialize_exit(ifp->if_serializer);
797
798         return (0);
799 }
800
801 /*
802  * tappoll
803  *
804  * The poll interface, this is only useful on reads really. The write
805  * detect always returns true, write never blocks anyway, it either
806  * accepts the packet or drops it
807  *
808  * Called from the fileops interface with nothing held.
809  *
810  * MPSAFE
811  */
812 static int
813 tappoll(struct dev_poll_args *ap)
814 {
815         cdev_t dev = ap->a_head.a_dev;
816         struct tap_softc        *tp = dev->si_drv1;
817         struct ifnet            *ifp = &tp->tap_if;
818         int                      revents = 0;
819
820         TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
821
822         if (ap->a_events & (POLLIN | POLLRDNORM)) {
823                 if (!IF_QEMPTY(&tp->tap_devq)) {
824                         TAPDEBUG(ifp,
825                                  "has data in queue. minor = %#x\n",
826                                  minor(tp->tap_dev));
827
828                         revents |= (ap->a_events & (POLLIN | POLLRDNORM));
829                 } else {
830                         TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
831                                  minor(tp->tap_dev));
832
833                         get_mplock();
834                         selrecord(curthread, &tp->tap_rsel);
835                         rel_mplock();
836                 }
837         }
838
839         if (ap->a_events & (POLLOUT | POLLWRNORM))
840                 revents |= (ap->a_events & (POLLOUT | POLLWRNORM));
841         ap->a_events = revents;
842         return(0);
843 }
844
845 /*
846  * tapkqfilter - called from the fileops interface with nothing held
847  *
848  * MPSAFE
849  */
850 static int filt_tapread(struct knote *kn, long hint);
851 static void filt_tapdetach(struct knote *kn);
852 static struct filterops tapread_filtops =
853         { 1, NULL, filt_tapdetach, filt_tapread };
854
855 static int
856 tapkqfilter(struct dev_kqfilter_args *ap)
857 {
858         cdev_t dev = ap->a_head.a_dev;
859         struct knote *kn = ap->a_kn;
860         struct tap_softc *tp;
861         struct klist *list;
862         struct ifnet *ifp;
863
864         get_mplock();
865         tp = dev->si_drv1;
866         ifp = &tp->tap_if;
867         ap->a_result =0;
868
869         switch(kn->kn_filter) {
870         case EVFILT_READ:
871                 list = &tp->tap_rsel.si_note;
872                 kn->kn_fop = &tapread_filtops;
873                 kn->kn_hook = (void *)tp;
874                 break;
875         case EVFILT_WRITE:
876                 /* fall through */
877         default:
878                 ap->a_result = 1;
879                 rel_mplock();
880                 return(0);
881         }
882         crit_enter();
883         SLIST_INSERT_HEAD(list, kn, kn_selnext);
884         crit_exit();
885         rel_mplock();
886         return(0);
887 }
888
889 static int
890 filt_tapread(struct knote *kn, long hint)
891 {
892         struct tap_softc *tp = (void *)kn->kn_hook;
893
894         if (IF_QEMPTY(&tp->tap_devq) == 0)      /* XXX serializer */
895                 return(1);
896         else
897                 return(0);
898 }
899
900 static void
901 filt_tapdetach(struct knote *kn)
902 {
903         struct tap_softc *tp = (void *)kn->kn_hook;
904
905         SLIST_REMOVE(&tp->tap_rsel.si_note, kn, knote, kn_selnext);
906 }
907
908 static void
909 tapifstop(struct tap_softc *tp, int clear_flags)
910 {
911         struct ifnet *ifp = &tp->tap_if;
912
913         ASSERT_SERIALIZED(ifp->if_serializer);
914         IF_DRAIN(&tp->tap_devq);
915         if (clear_flags)
916                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
917 }