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