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