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