]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/net/if_tap.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / net / 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$
35  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
36  */
37
38 #include "opt_compat.h"
39 #include "opt_inet.h"
40
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/fcntl.h>
44 #include <sys/filio.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/module.h>
50 #include <sys/poll.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/selinfo.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59 #include <sys/ttycom.h>
60 #include <sys/uio.h>
61 #include <sys/queue.h>
62
63 #include <net/bpf.h>
64 #include <net/ethernet.h>
65 #include <net/if.h>
66 #include <net/if_clone.h>
67 #include <net/if_dl.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
71 #include <net/vnet.h>
72
73 #include <netinet/in.h>
74
75 #include <net/if_tapvar.h>
76 #include <net/if_tap.h>
77
78
79 #define CDEV_NAME       "tap"
80 #define TAPDEBUG        if (tapdebug) printf
81
82 #define TAP             "tap"
83 #define VMNET           "vmnet"
84 #define TAPMAXUNIT      0x7fff
85 #define VMNET_DEV_MASK  CLONE_FLAG0
86
87 /* module */
88 static int              tapmodevent(module_t, int, void *);
89
90 /* device */
91 static void             tapclone(void *, struct ucred *, char *, int,
92                             struct cdev **);
93 static void             tapcreate(struct cdev *);
94
95 /* network interface */
96 static void             tapifstart(struct ifnet *);
97 static int              tapifioctl(struct ifnet *, u_long, caddr_t);
98 static void             tapifinit(void *);
99
100 static int              tap_clone_create(struct if_clone *, int, caddr_t);
101 static void             tap_clone_destroy(struct ifnet *);
102 static int              vmnet_clone_create(struct if_clone *, int, caddr_t);
103 static void             vmnet_clone_destroy(struct ifnet *);
104
105 IFC_SIMPLE_DECLARE(tap, 0);
106 IFC_SIMPLE_DECLARE(vmnet, 0);
107
108 /* character device */
109 static d_open_t         tapopen;
110 static d_close_t        tapclose;
111 static d_read_t         tapread;
112 static d_write_t        tapwrite;
113 static d_ioctl_t        tapioctl;
114 static d_poll_t         tappoll;
115 static d_kqfilter_t     tapkqfilter;
116
117 /* kqueue(2) */
118 static int              tapkqread(struct knote *, long);
119 static int              tapkqwrite(struct knote *, long);
120 static void             tapkqdetach(struct knote *);
121
122 static struct filterops tap_read_filterops = {
123         .f_isfd =       1,
124         .f_attach =     NULL,
125         .f_detach =     tapkqdetach,
126         .f_event =      tapkqread,
127 };
128
129 static struct filterops tap_write_filterops = {
130         .f_isfd =       1,
131         .f_attach =     NULL,
132         .f_detach =     tapkqdetach,
133         .f_event =      tapkqwrite,
134 };
135
136 static struct cdevsw    tap_cdevsw = {
137         .d_version =    D_VERSION,
138         .d_flags =      D_PSEUDO | D_NEEDMINOR,
139         .d_open =       tapopen,
140         .d_close =      tapclose,
141         .d_read =       tapread,
142         .d_write =      tapwrite,
143         .d_ioctl =      tapioctl,
144         .d_poll =       tappoll,
145         .d_name =       CDEV_NAME,
146         .d_kqfilter =   tapkqfilter,
147 };
148
149 /*
150  * All global variables in if_tap.c are locked with tapmtx, with the
151  * exception of tapdebug, which is accessed unlocked; tapclones is
152  * static at runtime.
153  */
154 static struct mtx               tapmtx;
155 static int                      tapdebug = 0;        /* debug flag   */
156 static int                      tapuopen = 0;        /* allow user open() */
157 static int                      tapuponopen = 0;    /* IFF_UP on open() */
158 static int                      tapdclone = 1;  /* enable devfs cloning */
159 static SLIST_HEAD(, tap_softc)  taphead;             /* first device */
160 static struct clonedevs         *tapclones;
161
162 MALLOC_DECLARE(M_TAP);
163 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
164 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
165
166 SYSCTL_DECL(_net_link);
167 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
168     "Ethernet tunnel software network interface");
169 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
170         "Allow user to open /dev/tap (based on node permissions)");
171 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
172         "Bring interface up when /dev/tap is opened");
173 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0,
174         "Enably legacy devfs interface creation");
175 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
176
177 TUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone);
178
179 DEV_MODULE(if_tap, tapmodevent, NULL);
180
181 static int
182 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
183 {
184         struct cdev *dev;
185         int i;
186         int extra;
187
188         if (strcmp(ifc->ifc_name, VMNET) == 0)
189                 extra = VMNET_DEV_MASK;
190         else
191                 extra = 0;
192
193         /* find any existing device, or allocate new unit number */
194         i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, extra);
195         if (i) {
196                 dev = make_dev(&tap_cdevsw, unit | extra,
197                      UID_ROOT, GID_WHEEL, 0600, "%s%d", ifc->ifc_name, unit);
198         }
199
200         tapcreate(dev);
201         return (0);
202 }
203
204 /* vmnet devices are tap devices in disguise */
205 static int
206 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
207 {
208         return tap_clone_create(ifc, unit, params);
209 }
210
211 static void
212 tap_destroy(struct tap_softc *tp)
213 {
214         struct ifnet *ifp = tp->tap_ifp;
215
216         /* Unlocked read. */
217         KASSERT(!(tp->tap_flags & TAP_OPEN),
218                 ("%s flags is out of sync", ifp->if_xname));
219
220         CURVNET_SET(ifp->if_vnet);
221         seldrain(&tp->tap_rsel);
222         knlist_destroy(&tp->tap_rsel.si_note);
223         destroy_dev(tp->tap_dev);
224         ether_ifdetach(ifp);
225         if_free_type(ifp, IFT_ETHER);
226
227         mtx_destroy(&tp->tap_mtx);
228         free(tp, M_TAP);
229         CURVNET_RESTORE();
230 }
231
232 static void
233 tap_clone_destroy(struct ifnet *ifp)
234 {
235         struct tap_softc *tp = ifp->if_softc;
236
237         mtx_lock(&tapmtx);
238         SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
239         mtx_unlock(&tapmtx);
240         tap_destroy(tp);
241 }
242
243 /* vmnet devices are tap devices in disguise */
244 static void
245 vmnet_clone_destroy(struct ifnet *ifp)
246 {
247         tap_clone_destroy(ifp);
248 }
249
250 /*
251  * tapmodevent
252  *
253  * module event handler
254  */
255 static int
256 tapmodevent(module_t mod, int type, void *data)
257 {
258         static eventhandler_tag  eh_tag = NULL;
259         struct tap_softc        *tp = NULL;
260         struct ifnet            *ifp = NULL;
261
262         switch (type) {
263         case MOD_LOAD:
264
265                 /* intitialize device */
266
267                 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
268                 SLIST_INIT(&taphead);
269
270                 clone_setup(&tapclones);
271                 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
272                 if (eh_tag == NULL) {
273                         clone_cleanup(&tapclones);
274                         mtx_destroy(&tapmtx);
275                         return (ENOMEM);
276                 }
277                 if_clone_attach(&tap_cloner);
278                 if_clone_attach(&vmnet_cloner);
279                 return (0);
280
281         case MOD_UNLOAD:
282                 /*
283                  * The EBUSY algorithm here can't quite atomically
284                  * guarantee that this is race-free since we have to
285                  * release the tap mtx to deregister the clone handler.
286                  */
287                 mtx_lock(&tapmtx);
288                 SLIST_FOREACH(tp, &taphead, tap_next) {
289                         mtx_lock(&tp->tap_mtx);
290                         if (tp->tap_flags & TAP_OPEN) {
291                                 mtx_unlock(&tp->tap_mtx);
292                                 mtx_unlock(&tapmtx);
293                                 return (EBUSY);
294                         }
295                         mtx_unlock(&tp->tap_mtx);
296                 }
297                 mtx_unlock(&tapmtx);
298
299                 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
300                 if_clone_detach(&tap_cloner);
301                 if_clone_detach(&vmnet_cloner);
302                 drain_dev_clone_events();
303
304                 mtx_lock(&tapmtx);
305                 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
306                         SLIST_REMOVE_HEAD(&taphead, tap_next);
307                         mtx_unlock(&tapmtx);
308
309                         ifp = tp->tap_ifp;
310
311                         TAPDEBUG("detaching %s\n", ifp->if_xname);
312
313                         tap_destroy(tp);
314                         mtx_lock(&tapmtx);
315                 }
316                 mtx_unlock(&tapmtx);
317                 clone_cleanup(&tapclones);
318
319                 mtx_destroy(&tapmtx);
320
321                 break;
322
323         default:
324                 return (EOPNOTSUPP);
325         }
326
327         return (0);
328 } /* tapmodevent */
329
330
331 /*
332  * DEVFS handler
333  *
334  * We need to support two kind of devices - tap and vmnet
335  */
336 static void
337 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
338 {
339         char            devname[SPECNAMELEN + 1];
340         int             i, unit, append_unit;
341         int             extra;
342
343         if (*dev != NULL)
344                 return;
345
346         if (!tapdclone ||
347             (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
348                 return;
349
350         unit = 0;
351         append_unit = 0;
352         extra = 0;
353
354         /* We're interested in only tap/vmnet devices. */
355         if (strcmp(name, TAP) == 0) {
356                 unit = -1;
357         } else if (strcmp(name, VMNET) == 0) {
358                 unit = -1;
359                 extra = VMNET_DEV_MASK;
360         } else if (dev_stdclone(name, NULL, TAP, &unit) != 1) {
361                 if (dev_stdclone(name, NULL, VMNET, &unit) != 1) {
362                         return;
363                 } else {
364                         extra = VMNET_DEV_MASK;
365                 }
366         }
367
368         if (unit == -1)
369                 append_unit = 1;
370
371         CURVNET_SET(CRED_TO_VNET(cred));
372         /* find any existing device, or allocate new unit number */
373         i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
374         if (i) {
375                 if (append_unit) {
376                         /*
377                          * We were passed 'tun' or 'tap', with no unit specified
378                          * so we'll need to append it now.
379                          */
380                         namelen = snprintf(devname, sizeof(devname), "%s%d", name,
381                             unit);
382                         name = devname;
383                 }
384
385                 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
386                      cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
387         }
388
389         if_clone_create(name, namelen, NULL);
390         CURVNET_RESTORE();
391 } /* tapclone */
392
393
394 /*
395  * tapcreate
396  *
397  * to create interface
398  */
399 static void
400 tapcreate(struct cdev *dev)
401 {
402         struct ifnet            *ifp = NULL;
403         struct tap_softc        *tp = NULL;
404         unsigned short           macaddr_hi;
405         uint32_t                 macaddr_mid;
406         int                      unit;
407         char                    *name = NULL;
408         u_char                  eaddr[6];
409
410         dev->si_flags &= ~SI_CHEAPCLONE;
411
412         /* allocate driver storage and create device */
413         tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
414         mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
415         mtx_lock(&tapmtx);
416         SLIST_INSERT_HEAD(&taphead, tp, tap_next);
417         mtx_unlock(&tapmtx);
418
419         unit = dev2unit(dev);
420
421         /* select device: tap or vmnet */
422         if (unit & VMNET_DEV_MASK) {
423                 name = VMNET;
424                 tp->tap_flags |= TAP_VMNET;
425         } else
426                 name = TAP;
427
428         unit &= TAPMAXUNIT;
429
430         TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
431
432         /* generate fake MAC address: 00 bd xx xx xx unit_no */
433         macaddr_hi = htons(0x00bd);
434         macaddr_mid = (uint32_t) ticks;
435         bcopy(&macaddr_hi, eaddr, sizeof(short));
436         bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
437         eaddr[5] = (u_char)unit;
438
439         /* fill the rest and attach interface */
440         ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
441         if (ifp == NULL)
442                 panic("%s%d: can not if_alloc()", name, unit);
443         ifp->if_softc = tp;
444         if_initname(ifp, name, unit);
445         ifp->if_init = tapifinit;
446         ifp->if_start = tapifstart;
447         ifp->if_ioctl = tapifioctl;
448         ifp->if_mtu = ETHERMTU;
449         ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
450         IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
451         ifp->if_capabilities |= IFCAP_LINKSTATE;
452         ifp->if_capenable |= IFCAP_LINKSTATE;
453
454         dev->si_drv1 = tp;
455         tp->tap_dev = dev;
456
457         ether_ifattach(ifp, eaddr);
458
459         mtx_lock(&tp->tap_mtx);
460         tp->tap_flags |= TAP_INITED;
461         mtx_unlock(&tp->tap_mtx);
462
463         knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
464
465         TAPDEBUG("interface %s is created. minor = %#x\n", 
466                 ifp->if_xname, dev2unit(dev));
467 } /* tapcreate */
468
469
470 /*
471  * tapopen
472  *
473  * to open tunnel. must be superuser
474  */
475 static int
476 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
477 {
478         struct tap_softc        *tp = NULL;
479         struct ifnet            *ifp = NULL;
480         int                      error;
481
482         if (tapuopen == 0) {
483                 error = priv_check(td, PRIV_NET_TAP);
484                 if (error)
485                         return (error);
486         }
487
488         if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
489                 return (ENXIO);
490
491         tp = dev->si_drv1;
492
493         mtx_lock(&tp->tap_mtx);
494         if (tp->tap_flags & TAP_OPEN) {
495                 mtx_unlock(&tp->tap_mtx);
496                 return (EBUSY);
497         }
498
499         bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
500         tp->tap_pid = td->td_proc->p_pid;
501         tp->tap_flags |= TAP_OPEN;
502         ifp = tp->tap_ifp;
503
504         ifp->if_drv_flags |= IFF_DRV_RUNNING;
505         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
506         if (tapuponopen)
507                 ifp->if_flags |= IFF_UP;
508         if_link_state_change(ifp, LINK_STATE_UP);
509         mtx_unlock(&tp->tap_mtx);
510
511         TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
512
513         return (0);
514 } /* tapopen */
515
516
517 /*
518  * tapclose
519  *
520  * close the device - mark i/f down & delete routing info
521  */
522 static int
523 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
524 {
525         struct ifaddr           *ifa;
526         struct tap_softc        *tp = dev->si_drv1;
527         struct ifnet            *ifp = tp->tap_ifp;
528
529         /* junk all pending output */
530         mtx_lock(&tp->tap_mtx);
531         CURVNET_SET(ifp->if_vnet);
532         IF_DRAIN(&ifp->if_snd);
533
534         /*
535          * do not bring the interface down, and do not anything with
536          * interface, if we are in VMnet mode. just close the device.
537          */
538
539         if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
540                 mtx_unlock(&tp->tap_mtx);
541                 if_down(ifp);
542                 mtx_lock(&tp->tap_mtx);
543                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
544                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
545                         mtx_unlock(&tp->tap_mtx);
546                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
547                                 rtinit(ifa, (int)RTM_DELETE, 0);
548                         }
549                         if_purgeaddrs(ifp);
550                         mtx_lock(&tp->tap_mtx);
551                 }
552         }
553
554         if_link_state_change(ifp, LINK_STATE_DOWN);
555         CURVNET_RESTORE();
556
557         funsetown(&tp->tap_sigio);
558         selwakeuppri(&tp->tap_rsel, PZERO+1);
559         KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
560
561         tp->tap_flags &= ~TAP_OPEN;
562         tp->tap_pid = 0;
563         mtx_unlock(&tp->tap_mtx);
564
565         TAPDEBUG("%s is closed. minor = %#x\n", 
566                 ifp->if_xname, dev2unit(dev));
567
568         return (0);
569 } /* tapclose */
570
571
572 /*
573  * tapifinit
574  *
575  * network interface initialization function
576  */
577 static void
578 tapifinit(void *xtp)
579 {
580         struct tap_softc        *tp = (struct tap_softc *)xtp;
581         struct ifnet            *ifp = tp->tap_ifp;
582
583         TAPDEBUG("initializing %s\n", ifp->if_xname);
584
585         mtx_lock(&tp->tap_mtx);
586         ifp->if_drv_flags |= IFF_DRV_RUNNING;
587         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
588         mtx_unlock(&tp->tap_mtx);
589
590         /* attempt to start output */
591         tapifstart(ifp);
592 } /* tapifinit */
593
594
595 /*
596  * tapifioctl
597  *
598  * Process an ioctl request on network interface
599  */
600 static int
601 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
602 {
603         struct tap_softc        *tp = ifp->if_softc;
604         struct ifreq            *ifr = (struct ifreq *)data;
605         struct ifstat           *ifs = NULL;
606         struct ifmediareq       *ifmr = NULL;
607         int                      dummy, error = 0;
608
609         switch (cmd) {
610                 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
611                 case SIOCADDMULTI:
612                 case SIOCDELMULTI:
613                         break;
614
615                 case SIOCGIFMEDIA:
616                         ifmr = (struct ifmediareq *)data;
617                         dummy = ifmr->ifm_count;
618                         ifmr->ifm_count = 1;
619                         ifmr->ifm_status = IFM_AVALID;
620                         ifmr->ifm_active = IFM_ETHER;
621                         if (tp->tap_flags & TAP_OPEN)
622                                 ifmr->ifm_status |= IFM_ACTIVE;
623                         ifmr->ifm_current = ifmr->ifm_active;
624                         if (dummy >= 1) {
625                                 int media = IFM_ETHER;
626                                 error = copyout(&media, ifmr->ifm_ulist,
627                                     sizeof(int));
628                         }
629                         break;
630
631                 case SIOCSIFMTU:
632                         ifp->if_mtu = ifr->ifr_mtu;
633                         break;
634
635                 case SIOCGIFSTATUS:
636                         ifs = (struct ifstat *)data;
637                         dummy = strlen(ifs->ascii);
638                         mtx_lock(&tp->tap_mtx);
639                         if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
640                                 snprintf(ifs->ascii + dummy,
641                                         sizeof(ifs->ascii) - dummy,
642                                         "\tOpened by PID %d\n", tp->tap_pid);
643                         mtx_unlock(&tp->tap_mtx);
644                         break;
645
646                 default:
647                         error = ether_ioctl(ifp, cmd, data);
648                         break;
649         }
650
651         return (error);
652 } /* tapifioctl */
653
654
655 /*
656  * tapifstart
657  *
658  * queue packets from higher level ready to put out
659  */
660 static void
661 tapifstart(struct ifnet *ifp)
662 {
663         struct tap_softc        *tp = ifp->if_softc;
664
665         TAPDEBUG("%s starting\n", ifp->if_xname);
666
667         /*
668          * do not junk pending output if we are in VMnet mode.
669          * XXX: can this do any harm because of queue overflow?
670          */
671
672         mtx_lock(&tp->tap_mtx);
673         if (((tp->tap_flags & TAP_VMNET) == 0) &&
674             ((tp->tap_flags & TAP_READY) != TAP_READY)) {
675                 struct mbuf *m;
676
677                 /* Unlocked read. */
678                 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname, 
679                     tp->tap_flags);
680
681                 for (;;) {
682                         IF_DEQUEUE(&ifp->if_snd, m);
683                         if (m != NULL) {
684                                 m_freem(m);
685                                 ifp->if_oerrors++;
686                         } else
687                                 break;
688                 }
689                 mtx_unlock(&tp->tap_mtx);
690
691                 return;
692         }
693
694         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
695
696         if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
697                 if (tp->tap_flags & TAP_RWAIT) {
698                         tp->tap_flags &= ~TAP_RWAIT;
699                         wakeup(tp);
700                 }
701
702                 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
703                         mtx_unlock(&tp->tap_mtx);
704                         pgsigio(&tp->tap_sigio, SIGIO, 0);
705                         mtx_lock(&tp->tap_mtx);
706                 }
707
708                 selwakeuppri(&tp->tap_rsel, PZERO+1);
709                 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
710                 ifp->if_opackets ++; /* obytes are counted in ether_output */
711         }
712
713         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
714         mtx_unlock(&tp->tap_mtx);
715 } /* tapifstart */
716
717
718 /*
719  * tapioctl
720  *
721  * the cdevsw interface is now pretty minimal
722  */
723 static int
724 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
725 {
726         struct tap_softc        *tp = dev->si_drv1;
727         struct ifnet            *ifp = tp->tap_ifp;
728         struct tapinfo          *tapp = NULL;
729         int                      f;
730 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
731     defined(COMPAT_FREEBSD4)
732         int                      ival;
733 #endif
734
735         switch (cmd) {
736                 case TAPSIFINFO:
737                         tapp = (struct tapinfo *)data;
738                         mtx_lock(&tp->tap_mtx);
739                         ifp->if_mtu = tapp->mtu;
740                         ifp->if_type = tapp->type;
741                         ifp->if_baudrate = tapp->baudrate;
742                         mtx_unlock(&tp->tap_mtx);
743                         break;
744
745                 case TAPGIFINFO:
746                         tapp = (struct tapinfo *)data;
747                         mtx_lock(&tp->tap_mtx);
748                         tapp->mtu = ifp->if_mtu;
749                         tapp->type = ifp->if_type;
750                         tapp->baudrate = ifp->if_baudrate;
751                         mtx_unlock(&tp->tap_mtx);
752                         break;
753
754                 case TAPSDEBUG:
755                         tapdebug = *(int *)data;
756                         break;
757
758                 case TAPGDEBUG:
759                         *(int *)data = tapdebug;
760                         break;
761
762                 case TAPGIFNAME: {
763                         struct ifreq    *ifr = (struct ifreq *) data;
764
765                         strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
766                         } break;
767
768                 case FIONBIO:
769                         break;
770
771                 case FIOASYNC:
772                         mtx_lock(&tp->tap_mtx);
773                         if (*(int *)data)
774                                 tp->tap_flags |= TAP_ASYNC;
775                         else
776                                 tp->tap_flags &= ~TAP_ASYNC;
777                         mtx_unlock(&tp->tap_mtx);
778                         break;
779
780                 case FIONREAD:
781                         if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
782                                 struct mbuf *mb;
783
784                                 IFQ_LOCK(&ifp->if_snd);
785                                 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
786                                 for (*(int *)data = 0; mb != NULL;
787                                      mb = mb->m_next)
788                                         *(int *)data += mb->m_len;
789                                 IFQ_UNLOCK(&ifp->if_snd);
790                         } else
791                                 *(int *)data = 0;
792                         break;
793
794                 case FIOSETOWN:
795                         return (fsetown(*(int *)data, &tp->tap_sigio));
796
797                 case FIOGETOWN:
798                         *(int *)data = fgetown(&tp->tap_sigio);
799                         return (0);
800
801                 /* this is deprecated, FIOSETOWN should be used instead */
802                 case TIOCSPGRP:
803                         return (fsetown(-(*(int *)data), &tp->tap_sigio));
804
805                 /* this is deprecated, FIOGETOWN should be used instead */
806                 case TIOCGPGRP:
807                         *(int *)data = -fgetown(&tp->tap_sigio);
808                         return (0);
809
810                 /* VMware/VMnet port ioctl's */
811
812 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
813     defined(COMPAT_FREEBSD4)
814                 case _IO('V', 0):
815                         ival = IOCPARM_IVAL(data);
816                         data = (caddr_t)&ival;
817                         /* FALLTHROUGH */
818 #endif
819                 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
820                         f = *(int *)data;
821                         f &= 0x0fff;
822                         f &= ~IFF_CANTCHANGE;
823                         f |= IFF_UP;
824
825                         mtx_lock(&tp->tap_mtx);
826                         ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
827                         mtx_unlock(&tp->tap_mtx);
828                         break;
829
830                 case OSIOCGIFADDR:      /* get MAC address of the remote side */
831                 case SIOCGIFADDR:
832                         mtx_lock(&tp->tap_mtx);
833                         bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
834                         mtx_unlock(&tp->tap_mtx);
835                         break;
836
837                 case SIOCSIFADDR:       /* set MAC address of the remote side */
838                         mtx_lock(&tp->tap_mtx);
839                         bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
840                         mtx_unlock(&tp->tap_mtx);
841                         break;
842
843                 default:
844                         return (ENOTTY);
845         }
846         return (0);
847 } /* tapioctl */
848
849
850 /*
851  * tapread
852  *
853  * the cdevsw read interface - reads a packet at a time, or at
854  * least as much of a packet as can be read
855  */
856 static int
857 tapread(struct cdev *dev, struct uio *uio, int flag)
858 {
859         struct tap_softc        *tp = dev->si_drv1;
860         struct ifnet            *ifp = tp->tap_ifp;
861         struct mbuf             *m = NULL;
862         int                      error = 0, len;
863
864         TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
865
866         mtx_lock(&tp->tap_mtx);
867         if ((tp->tap_flags & TAP_READY) != TAP_READY) {
868                 mtx_unlock(&tp->tap_mtx);
869
870                 /* Unlocked read. */
871                 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
872                         ifp->if_xname, dev2unit(dev), tp->tap_flags);
873
874                 return (EHOSTDOWN);
875         }
876
877         tp->tap_flags &= ~TAP_RWAIT;
878
879         /* sleep until we get a packet */
880         do {
881                 IF_DEQUEUE(&ifp->if_snd, m);
882
883                 if (m == NULL) {
884                         if (flag & O_NONBLOCK) {
885                                 mtx_unlock(&tp->tap_mtx);
886                                 return (EWOULDBLOCK);
887                         }
888
889                         tp->tap_flags |= TAP_RWAIT;
890                         error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
891                             "taprd", 0);
892                         if (error) {
893                                 mtx_unlock(&tp->tap_mtx);
894                                 return (error);
895                         }
896                 }
897         } while (m == NULL);
898         mtx_unlock(&tp->tap_mtx);
899
900         /* feed packet to bpf */
901         BPF_MTAP(ifp, m);
902
903         /* xfer packet to user space */
904         while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
905                 len = min(uio->uio_resid, m->m_len);
906                 if (len == 0)
907                         break;
908
909                 error = uiomove(mtod(m, void *), len, uio);
910                 m = m_free(m);
911         }
912
913         if (m != NULL) {
914                 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname, 
915                         dev2unit(dev));
916                 m_freem(m);
917         }
918
919         return (error);
920 } /* tapread */
921
922
923 /*
924  * tapwrite
925  *
926  * the cdevsw write interface - an atomic write is a packet - or else!
927  */
928 static int
929 tapwrite(struct cdev *dev, struct uio *uio, int flag)
930 {
931         struct ether_header     *eh;
932         struct tap_softc        *tp = dev->si_drv1;
933         struct ifnet            *ifp = tp->tap_ifp;
934         struct mbuf             *m;
935
936         TAPDEBUG("%s writing, minor = %#x\n", 
937                 ifp->if_xname, dev2unit(dev));
938
939         if (uio->uio_resid == 0)
940                 return (0);
941
942         if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
943                 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
944                         ifp->if_xname, uio->uio_resid, dev2unit(dev));
945
946                 return (EIO);
947         }
948
949         if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN,
950             M_PKTHDR)) == NULL) {
951                 ifp->if_ierrors ++;
952                 return (ENOBUFS);
953         }
954
955         m->m_pkthdr.rcvif = ifp;
956
957         /*
958          * Only pass a unicast frame to ether_input(), if it would actually
959          * have been received by non-virtual hardware.
960          */
961         if (m->m_len < sizeof(struct ether_header)) {
962                 m_freem(m);
963                 return (0);
964         }
965         eh = mtod(m, struct ether_header *);
966
967         if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
968             !ETHER_IS_MULTICAST(eh->ether_dhost) &&
969             bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
970                 m_freem(m);
971                 return (0);
972         }
973
974         /* Pass packet up to parent. */
975         CURVNET_SET(ifp->if_vnet);
976         (*ifp->if_input)(ifp, m);
977         CURVNET_RESTORE();
978         ifp->if_ipackets ++; /* ibytes are counted in parent */
979
980         return (0);
981 } /* tapwrite */
982
983
984 /*
985  * tappoll
986  *
987  * the poll interface, this is only useful on reads
988  * really. the write detect always returns true, write never blocks
989  * anyway, it either accepts the packet or drops it
990  */
991 static int
992 tappoll(struct cdev *dev, int events, struct thread *td)
993 {
994         struct tap_softc        *tp = dev->si_drv1;
995         struct ifnet            *ifp = tp->tap_ifp;
996         int                      revents = 0;
997
998         TAPDEBUG("%s polling, minor = %#x\n", 
999                 ifp->if_xname, dev2unit(dev));
1000
1001         if (events & (POLLIN | POLLRDNORM)) {
1002                 IFQ_LOCK(&ifp->if_snd);
1003                 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1004                         TAPDEBUG("%s have data in queue. len = %d, " \
1005                                 "minor = %#x\n", ifp->if_xname,
1006                                 ifp->if_snd.ifq_len, dev2unit(dev));
1007
1008                         revents |= (events & (POLLIN | POLLRDNORM));
1009                 } else {
1010                         TAPDEBUG("%s waiting for data, minor = %#x\n",
1011                                 ifp->if_xname, dev2unit(dev));
1012
1013                         selrecord(td, &tp->tap_rsel);
1014                 }
1015                 IFQ_UNLOCK(&ifp->if_snd);
1016         }
1017
1018         if (events & (POLLOUT | POLLWRNORM))
1019                 revents |= (events & (POLLOUT | POLLWRNORM));
1020
1021         return (revents);
1022 } /* tappoll */
1023
1024
1025 /*
1026  * tap_kqfilter
1027  *
1028  * support for kevent() system call
1029  */
1030 static int
1031 tapkqfilter(struct cdev *dev, struct knote *kn)
1032 {
1033         struct tap_softc        *tp = dev->si_drv1;
1034         struct ifnet            *ifp = tp->tap_ifp;
1035
1036         switch (kn->kn_filter) {
1037         case EVFILT_READ:
1038                 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1039                         ifp->if_xname, dev2unit(dev));
1040                 kn->kn_fop = &tap_read_filterops;
1041                 break;
1042
1043         case EVFILT_WRITE:
1044                 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1045                         ifp->if_xname, dev2unit(dev));
1046                 kn->kn_fop = &tap_write_filterops;
1047                 break;
1048
1049         default:
1050                 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1051                         ifp->if_xname, dev2unit(dev));
1052                 return (EINVAL);
1053                 /* NOT REACHED */
1054         }
1055
1056         kn->kn_hook = tp;
1057         knlist_add(&tp->tap_rsel.si_note, kn, 0);
1058
1059         return (0);
1060 } /* tapkqfilter */
1061
1062
1063 /*
1064  * tap_kqread
1065  * 
1066  * Return true if there is data in the interface queue
1067  */
1068 static int
1069 tapkqread(struct knote *kn, long hint)
1070 {
1071         int                      ret;
1072         struct tap_softc        *tp = kn->kn_hook;
1073         struct cdev             *dev = tp->tap_dev;
1074         struct ifnet            *ifp = tp->tap_ifp;
1075
1076         if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1077                 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1078                         ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1079                 ret = 1;
1080         } else {
1081                 TAPDEBUG("%s waiting for data, minor = %#x\n",
1082                         ifp->if_xname, dev2unit(dev));
1083                 ret = 0;
1084         }
1085
1086         return (ret);
1087 } /* tapkqread */
1088
1089
1090 /*
1091  * tap_kqwrite
1092  *
1093  * Always can write. Return the MTU in kn->data
1094  */
1095 static int
1096 tapkqwrite(struct knote *kn, long hint)
1097 {
1098         struct tap_softc        *tp = kn->kn_hook;
1099         struct ifnet            *ifp = tp->tap_ifp;
1100
1101         kn->kn_data = ifp->if_mtu;
1102
1103         return (1);
1104 } /* tapkqwrite */
1105
1106
1107 static void
1108 tapkqdetach(struct knote *kn)
1109 {
1110         struct tap_softc        *tp = kn->kn_hook;
1111
1112         knlist_remove(&tp->tap_rsel.si_note, kn, 0);
1113 } /* tapkqdetach */
1114