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