2 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 * -------------------------------------------------------------------------
29 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30 * Nottingham University 1987.
35 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
38 #include "opt_compat.h"
41 #include <sys/param.h>
43 #include <sys/fcntl.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/module.h>
52 #include <sys/selinfo.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
58 #include <sys/ttycom.h>
60 #include <sys/queue.h>
63 #include <net/ethernet.h>
65 #include <net/if_clone.h>
66 #include <net/if_dl.h>
67 #include <net/route.h>
68 #include <net/if_types.h>
70 #include <netinet/in.h>
72 #include <net/if_tapvar.h>
73 #include <net/if_tap.h>
76 #define CDEV_NAME "tap"
77 #define TAPDEBUG if (tapdebug) printf
81 #define TAPMAXUNIT 0x7fff
82 #define VMNET_DEV_MASK CLONE_FLAG0
85 static int tapmodevent(module_t, int, void *);
88 static void tapclone(void *, struct ucred *, char *, int,
90 static void tapcreate(struct cdev *);
92 /* network interface */
93 static void tapifstart(struct ifnet *);
94 static int tapifioctl(struct ifnet *, u_long, caddr_t);
95 static void tapifinit(void *);
97 static int tap_clone_create(struct if_clone *, int, caddr_t);
98 static void tap_clone_destroy(struct ifnet *);
99 static int vmnet_clone_create(struct if_clone *, int, caddr_t);
100 static void vmnet_clone_destroy(struct ifnet *);
102 IFC_SIMPLE_DECLARE(tap, 0);
103 IFC_SIMPLE_DECLARE(vmnet, 0);
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;
115 static int tapkqread(struct knote *, long);
116 static int tapkqwrite(struct knote *, long);
117 static void tapkqdetach(struct knote *);
119 static struct filterops tap_read_filterops = {
122 .f_detach = tapkqdetach,
123 .f_event = tapkqread,
126 static struct filterops tap_write_filterops = {
129 .f_detach = tapkqdetach,
130 .f_event = tapkqwrite,
133 static struct cdevsw tap_cdevsw = {
134 .d_version = D_VERSION,
135 .d_flags = D_PSEUDO | D_NEEDGIANT,
143 .d_kqfilter = tapkqfilter,
147 * All global variables in if_tap.c are locked with tapmtx, with the
148 * exception of tapdebug, which is accessed unlocked; tapclones is
151 static struct mtx tapmtx;
152 static int tapdebug = 0; /* debug flag */
153 static int tapuopen = 0; /* allow user open() */
154 static int tapdclone = 1; /* enable devfs cloning */
155 static SLIST_HEAD(, tap_softc) taphead; /* first device */
156 static struct clonedevs *tapclones;
158 MALLOC_DECLARE(M_TAP);
159 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
160 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
162 SYSCTL_DECL(_net_link);
163 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
164 "Ethernet tunnel software network interface");
165 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
166 "Allow user to open /dev/tap (based on node permissions)");
167 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0,
168 "Enably legacy devfs interface creation");
169 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
171 TUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone);
173 DEV_MODULE(if_tap, tapmodevent, NULL);
176 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
182 if (strcmp(ifc->ifc_name, VMNET) == 0)
183 extra = VMNET_DEV_MASK;
187 /* find any existing device, or allocate new unit number */
188 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, extra);
190 dev = make_dev(&tap_cdevsw, unit2minor(unit | extra),
191 UID_ROOT, GID_WHEEL, 0600, "%s%d", ifc->ifc_name, unit);
194 dev->si_flags |= SI_CHEAPCLONE;
202 /* vmnet devices are tap devices in disguise */
204 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
206 return tap_clone_create(ifc, unit, params);
210 tap_destroy(struct tap_softc *tp)
212 struct ifnet *ifp = tp->tap_ifp;
216 KASSERT(!(tp->tap_flags & TAP_OPEN),
217 ("%s flags is out of sync", ifp->if_xname));
219 knlist_destroy(&tp->tap_rsel.si_note);
220 destroy_dev(tp->tap_dev);
223 if_free_type(ifp, IFT_ETHER);
226 mtx_destroy(&tp->tap_mtx);
231 tap_clone_destroy(struct ifnet *ifp)
233 struct tap_softc *tp = ifp->if_softc;
236 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
241 /* vmnet devices are tap devices in disguise */
243 vmnet_clone_destroy(struct ifnet *ifp)
245 tap_clone_destroy(ifp);
251 * module event handler
254 tapmodevent(module_t mod, int type, void *data)
256 static eventhandler_tag eh_tag = NULL;
257 struct tap_softc *tp = NULL;
258 struct ifnet *ifp = NULL;
263 /* intitialize device */
265 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
266 SLIST_INIT(&taphead);
268 clone_setup(&tapclones);
269 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
270 if (eh_tag == NULL) {
271 clone_cleanup(&tapclones);
272 mtx_destroy(&tapmtx);
275 if_clone_attach(&tap_cloner);
276 if_clone_attach(&vmnet_cloner);
281 * The EBUSY algorithm here can't quite atomically
282 * guarantee that this is race-free since we have to
283 * release the tap mtx to deregister the clone handler.
286 SLIST_FOREACH(tp, &taphead, tap_next) {
287 mtx_lock(&tp->tap_mtx);
288 if (tp->tap_flags & TAP_OPEN) {
289 mtx_unlock(&tp->tap_mtx);
293 mtx_unlock(&tp->tap_mtx);
297 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
298 if_clone_detach(&tap_cloner);
299 if_clone_detach(&vmnet_cloner);
302 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
303 SLIST_REMOVE_HEAD(&taphead, tap_next);
308 TAPDEBUG("detaching %s\n", ifp->if_xname);
314 clone_cleanup(&tapclones);
316 mtx_destroy(&tapmtx);
331 * We need to support two kind of devices - tap and vmnet
334 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
336 char devname[SPECNAMELEN + 1];
337 int i, unit, append_unit;
344 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
351 /* We're interested in only tap/vmnet devices. */
352 if (strcmp(name, TAP) == 0) {
354 } else if (strcmp(name, VMNET) == 0) {
356 extra = VMNET_DEV_MASK;
357 } else if (dev_stdclone(name, NULL, TAP, &unit) != 1) {
358 if (dev_stdclone(name, NULL, VMNET, &unit) != 1) {
361 extra = VMNET_DEV_MASK;
368 /* find any existing device, or allocate new unit number */
369 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
373 * We were passed 'tun' or 'tap', with no unit specified
374 * so we'll need to append it now.
376 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
381 *dev = make_dev(&tap_cdevsw, unit2minor(unit | extra),
382 UID_ROOT, GID_WHEEL, 0600, "%s", name);
385 (*dev)->si_flags |= SI_CHEAPCLONE;
389 if_clone_create(name, namelen, NULL);
396 * to create interface
399 tapcreate(struct cdev *dev)
401 struct ifnet *ifp = NULL;
402 struct tap_softc *tp = NULL;
403 unsigned short macaddr_hi;
408 dev->si_flags &= ~SI_CHEAPCLONE;
410 /* allocate driver storage and create device */
411 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
412 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
414 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
417 unit = dev2unit(dev);
419 /* select device: tap or vmnet */
420 if (unit & VMNET_DEV_MASK) {
422 tp->tap_flags |= TAP_VMNET;
428 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, minor(dev));
430 /* generate fake MAC address: 00 bd xx xx xx unit_no */
431 macaddr_hi = htons(0x00bd);
432 bcopy(&macaddr_hi, eaddr, sizeof(short));
433 bcopy(&ticks, &eaddr[2], sizeof(long));
434 eaddr[5] = (u_char)unit;
436 /* fill the rest and attach interface */
437 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
439 panic("%s%d: can not if_alloc()", name, unit);
441 if_initname(ifp, name, unit);
442 ifp->if_init = tapifinit;
443 ifp->if_start = tapifstart;
444 ifp->if_ioctl = tapifioctl;
445 ifp->if_mtu = ETHERMTU;
446 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
447 ifp->if_snd.ifq_maxlen = ifqmaxlen;
453 ether_ifattach(ifp, eaddr);
456 mtx_lock(&tp->tap_mtx);
457 tp->tap_flags |= TAP_INITED;
458 mtx_unlock(&tp->tap_mtx);
460 knlist_init(&tp->tap_rsel.si_note, NULL, NULL, NULL, NULL);
462 TAPDEBUG("interface %s is created. minor = %#x\n",
463 ifp->if_xname, minor(dev));
470 * to open tunnel. must be superuser
473 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
475 struct tap_softc *tp = NULL;
476 struct ifnet *ifp = NULL;
480 error = priv_check(td, PRIV_NET_TAP);
485 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
490 mtx_lock(&tp->tap_mtx);
491 if (tp->tap_flags & TAP_OPEN) {
492 mtx_unlock(&tp->tap_mtx);
496 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
497 tp->tap_pid = td->td_proc->p_pid;
498 tp->tap_flags |= TAP_OPEN;
500 mtx_unlock(&tp->tap_mtx);
503 ifp->if_drv_flags |= IFF_DRV_RUNNING;
504 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
507 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, minor(dev));
516 * close the device - mark i/f down & delete routing info
519 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
522 struct tap_softc *tp = dev->si_drv1;
523 struct ifnet *ifp = tp->tap_ifp;
526 /* junk all pending output */
527 IF_DRAIN(&ifp->if_snd);
530 * do not bring the interface down, and do not anything with
531 * interface, if we are in VMnet mode. just close the device.
534 mtx_lock(&tp->tap_mtx);
535 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
536 mtx_unlock(&tp->tap_mtx);
539 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
540 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
541 rtinit(ifa, (int)RTM_DELETE, 0);
544 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
548 mtx_unlock(&tp->tap_mtx);
550 funsetown(&tp->tap_sigio);
551 selwakeuppri(&tp->tap_rsel, PZERO+1);
552 KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
554 mtx_lock(&tp->tap_mtx);
555 tp->tap_flags &= ~TAP_OPEN;
557 mtx_unlock(&tp->tap_mtx);
559 TAPDEBUG("%s is closed. minor = %#x\n",
560 ifp->if_xname, minor(dev));
569 * network interface initialization function
574 struct tap_softc *tp = (struct tap_softc *)xtp;
575 struct ifnet *ifp = tp->tap_ifp;
577 TAPDEBUG("initializing %s\n", ifp->if_xname);
579 ifp->if_drv_flags |= IFF_DRV_RUNNING;
580 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
582 /* attempt to start output */
590 * Process an ioctl request on network interface
593 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
595 struct tap_softc *tp = ifp->if_softc;
596 struct ifstat *ifs = NULL;
600 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
607 ifs = (struct ifstat *)data;
608 dummy = strlen(ifs->ascii);
609 mtx_lock(&tp->tap_mtx);
610 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
611 snprintf(ifs->ascii + dummy,
612 sizeof(ifs->ascii) - dummy,
613 "\tOpened by PID %d\n", tp->tap_pid);
614 mtx_unlock(&tp->tap_mtx);
620 dummy = ether_ioctl(ifp, cmd, data);
633 * queue packets from higher level ready to put out
636 tapifstart(struct ifnet *ifp)
638 struct tap_softc *tp = ifp->if_softc;
641 TAPDEBUG("%s starting\n", ifp->if_xname);
644 * do not junk pending output if we are in VMnet mode.
645 * XXX: can this do any harm because of queue overflow?
648 mtx_lock(&tp->tap_mtx);
649 if (((tp->tap_flags & TAP_VMNET) == 0) &&
650 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
651 struct mbuf *m = NULL;
653 mtx_unlock(&tp->tap_mtx);
656 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
661 IF_DEQUEUE(&ifp->if_snd, m);
670 mtx_unlock(&tp->tap_mtx);
673 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
675 if (ifp->if_snd.ifq_len != 0) {
676 mtx_lock(&tp->tap_mtx);
677 if (tp->tap_flags & TAP_RWAIT) {
678 tp->tap_flags &= ~TAP_RWAIT;
682 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
683 mtx_unlock(&tp->tap_mtx);
684 pgsigio(&tp->tap_sigio, SIGIO, 0);
686 mtx_unlock(&tp->tap_mtx);
688 selwakeuppri(&tp->tap_rsel, PZERO+1);
689 KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
690 ifp->if_opackets ++; /* obytes are counted in ether_output */
693 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
701 * the cdevsw interface is now pretty minimal
704 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
706 struct tap_softc *tp = dev->si_drv1;
707 struct ifnet *ifp = tp->tap_ifp;
708 struct tapinfo *tapp = NULL;
711 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
712 defined(COMPAT_FREEBSD4)
719 tapp = (struct tapinfo *)data;
720 ifp->if_mtu = tapp->mtu;
721 ifp->if_type = tapp->type;
722 ifp->if_baudrate = tapp->baudrate;
727 tapp = (struct tapinfo *)data;
728 tapp->mtu = ifp->if_mtu;
729 tapp->type = ifp->if_type;
730 tapp->baudrate = ifp->if_baudrate;
734 tapdebug = *(int *)data;
738 *(int *)data = tapdebug;
746 mtx_lock(&tp->tap_mtx);
748 tp->tap_flags |= TAP_ASYNC;
750 tp->tap_flags &= ~TAP_ASYNC;
751 mtx_unlock(&tp->tap_mtx);
757 if (ifp->if_snd.ifq_head) {
758 struct mbuf *mb = ifp->if_snd.ifq_head;
760 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
761 *(int *)data += mb->m_len;
768 return (fsetown(*(int *)data, &tp->tap_sigio));
771 *(int *)data = fgetown(&tp->tap_sigio);
774 /* this is deprecated, FIOSETOWN should be used instead */
776 return (fsetown(-(*(int *)data), &tp->tap_sigio));
778 /* this is deprecated, FIOGETOWN should be used instead */
780 *(int *)data = -fgetown(&tp->tap_sigio);
783 /* VMware/VMnet port ioctl's */
785 case SIOCGIFFLAGS: /* get ifnet flags */
786 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
789 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
790 defined(COMPAT_FREEBSD4)
792 ival = IOCPARM_IVAL(data);
793 data = (caddr_t)&ival;
796 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
799 f &= ~IFF_CANTCHANGE;
803 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
807 case OSIOCGIFADDR: /* get MAC address of the remote side */
809 mtx_lock(&tp->tap_mtx);
810 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
811 mtx_unlock(&tp->tap_mtx);
814 case SIOCSIFADDR: /* set MAC address of the remote side */
815 mtx_lock(&tp->tap_mtx);
816 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
817 mtx_unlock(&tp->tap_mtx);
830 * the cdevsw read interface - reads a packet at a time, or at
831 * least as much of a packet as can be read
834 tapread(struct cdev *dev, struct uio *uio, int flag)
836 struct tap_softc *tp = dev->si_drv1;
837 struct ifnet *ifp = tp->tap_ifp;
838 struct mbuf *m = NULL;
839 int error = 0, len, s;
841 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev));
843 mtx_lock(&tp->tap_mtx);
844 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
845 mtx_unlock(&tp->tap_mtx);
848 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
849 ifp->if_xname, minor(dev), tp->tap_flags);
854 tp->tap_flags &= ~TAP_RWAIT;
855 mtx_unlock(&tp->tap_mtx);
857 /* sleep until we get a packet */
860 IF_DEQUEUE(&ifp->if_snd, m);
864 if (flag & O_NONBLOCK)
865 return (EWOULDBLOCK);
867 mtx_lock(&tp->tap_mtx);
868 tp->tap_flags |= TAP_RWAIT;
869 mtx_unlock(&tp->tap_mtx);
870 error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0);
876 /* feed packet to bpf */
879 /* xfer packet to user space */
880 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
881 len = min(uio->uio_resid, m->m_len);
885 error = uiomove(mtod(m, void *), len, uio);
890 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
902 * the cdevsw write interface - an atomic write is a packet - or else!
905 tapwrite(struct cdev *dev, struct uio *uio, int flag)
907 struct ether_header *eh;
908 struct tap_softc *tp = dev->si_drv1;
909 struct ifnet *ifp = tp->tap_ifp;
912 TAPDEBUG("%s writting, minor = %#x\n",
913 ifp->if_xname, minor(dev));
915 if (uio->uio_resid == 0)
918 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
919 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
920 ifp->if_xname, uio->uio_resid, minor(dev));
925 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN,
926 M_PKTHDR)) == NULL) {
931 m->m_pkthdr.rcvif = ifp;
934 * Only pass a unicast frame to ether_input(), if it would actually
935 * have been received by non-virtual hardware.
937 if (m->m_len < sizeof(struct ether_header)) {
941 eh = mtod(m, struct ether_header *);
943 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
944 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
945 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
950 /* Pass packet up to parent. */
951 (*ifp->if_input)(ifp, m);
952 ifp->if_ipackets ++; /* ibytes are counted in parent */
961 * the poll interface, this is only useful on reads
962 * really. the write detect always returns true, write never blocks
963 * anyway, it either accepts the packet or drops it
966 tappoll(struct cdev *dev, int events, struct thread *td)
968 struct tap_softc *tp = dev->si_drv1;
969 struct ifnet *ifp = tp->tap_ifp;
972 TAPDEBUG("%s polling, minor = %#x\n",
973 ifp->if_xname, minor(dev));
976 if (events & (POLLIN | POLLRDNORM)) {
977 if (ifp->if_snd.ifq_len > 0) {
978 TAPDEBUG("%s have data in queue. len = %d, " \
979 "minor = %#x\n", ifp->if_xname,
980 ifp->if_snd.ifq_len, minor(dev));
982 revents |= (events & (POLLIN | POLLRDNORM));
984 TAPDEBUG("%s waiting for data, minor = %#x\n",
985 ifp->if_xname, minor(dev));
987 selrecord(td, &tp->tap_rsel);
991 if (events & (POLLOUT | POLLWRNORM))
992 revents |= (events & (POLLOUT | POLLWRNORM));
1002 * support for kevent() system call
1005 tapkqfilter(struct cdev *dev, struct knote *kn)
1008 struct tap_softc *tp = dev->si_drv1;
1009 struct ifnet *ifp = tp->tap_ifp;
1012 switch (kn->kn_filter) {
1014 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1015 ifp->if_xname, minor(dev));
1016 kn->kn_fop = &tap_read_filterops;
1020 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1021 ifp->if_xname, minor(dev));
1022 kn->kn_fop = &tap_write_filterops;
1026 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1027 ifp->if_xname, minor(dev));
1034 kn->kn_hook = (caddr_t) dev;
1035 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1044 * Return true if there is data in the interface queue
1047 tapkqread(struct knote *kn, long hint)
1050 struct cdev *dev = (struct cdev *)(kn->kn_hook);
1051 struct tap_softc *tp = dev->si_drv1;
1052 struct ifnet *ifp = tp->tap_ifp;
1055 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1056 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1057 ifp->if_xname, ifp->if_snd.ifq_len, minor(dev));
1060 TAPDEBUG("%s waiting for data, minor = %#x\n",
1061 ifp->if_xname, minor(dev));
1073 * Always can write. Return the MTU in kn->data
1076 tapkqwrite(struct knote *kn, long hint)
1079 struct tap_softc *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
1080 struct ifnet *ifp = tp->tap_ifp;
1083 kn->kn_data = ifp->if_mtu;
1091 tapkqdetach(struct knote *kn)
1093 struct tap_softc *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
1095 knlist_remove(&tp->tap_rsel.si_note, kn, 0);