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