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.
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 $
41 #include <sys/param.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>
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>
60 #include <sys/vnode.h>
61 #include <sys/serialize.h>
64 #include <net/ethernet.h>
66 #include <net/ifq_var.h>
67 #include <net/if_arp.h>
68 #include <net/if_clone.h>
69 #include <net/route.h>
70 #include <sys/devfs.h>
72 #include <netinet/in.h>
74 #include "if_tapvar.h"
77 #define TAP_IFFLAGS (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST)
79 #define TAP_PREALLOCATED_UNITS 4
81 #define CDEV_NAME "tap"
82 #define CDEV_MAJOR 149
83 #define TAPDEBUG if (tapdebug) if_printf
87 #define VMNET_DEV_MASK 0x00010000
89 DEVFS_DECLARE_CLONE_BITMAP(tap);
92 static int tapmodevent (module_t, int, void *);
95 static struct tap_softc *tapcreate(int, cdev_t);
96 static void tapdestroy(struct tap_softc *);
99 static int tap_clone_create(struct if_clone *, int);
100 static void tap_clone_destroy(struct ifnet *);
103 /* network interface */
104 static void tapifstart (struct ifnet *);
105 static int tapifioctl (struct ifnet *, u_long, caddr_t,
107 static void tapifinit (void *);
108 static void tapifstop(struct tap_softc *, int);
109 static void tapifflags(struct tap_softc *);
111 /* character device */
112 static d_open_t tapopen;
113 static d_clone_t tapclone;
114 static d_close_t tapclose;
115 static d_read_t tapread;
116 static d_write_t tapwrite;
117 static d_ioctl_t tapioctl;
118 static d_poll_t tappoll;
119 static d_kqfilter_t tapkqfilter;
121 static struct dev_ops tap_ops = {
122 { CDEV_NAME, CDEV_MAJOR, 0 },
129 .d_kqfilter = tapkqfilter
132 static int taprefcnt = 0; /* module ref. counter */
133 static int taplastunit = -1; /* max. open unit number */
134 static int tapdebug = 0; /* debug flag */
135 static int tapuopen = 0; /* all user open() */
136 static int tapuponopen = 0; /* IFF_UP */
138 MALLOC_DECLARE(M_TAP);
139 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
140 struct if_clone tap_cloner = IF_CLONE_INITIALIZER("tap",
141 tap_clone_create, tap_clone_destroy,
143 static SLIST_HEAD(,tap_softc) tap_listhead =
144 SLIST_HEAD_INITIALIZER(&tap_listhead);
146 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
147 SYSCTL_DECL(_net_link);
148 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
149 "Ethernet tunnel software network interface");
150 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
151 "Allow user to open /dev/tap (based on node permissions)");
152 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
153 "Bring interface up when /dev/tap is opened");
154 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
156 DEV_MODULE(if_tap, tapmodevent, NULL);
161 * module event handler
164 tapmodevent(module_t mod, int type, void *data)
166 static int attached = 0;
167 struct tap_softc *tp, *ntp;
175 make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap), tapclone,
176 UID_ROOT, GID_WHEEL, 0600, "tap");
177 SLIST_INIT(&tap_listhead);
178 if_clone_attach(&tap_cloner);
180 for (i = 0; i < TAP_PREALLOCATED_UNITS; ++i) {
181 make_dev(&tap_ops, i, UID_ROOT, GID_WHEEL,
183 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), i);
193 if_clone_detach(&tap_cloner);
195 /* Maintain tap ifs in a local list */
196 SLIST_FOREACH_MUTABLE(tp, &tap_listhead, tap_link, ntp)
201 devfs_clone_handler_del("tap");
202 dev_ops_remove_all(&tap_ops);
203 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(tap));
215 * tapcreate - create or clone an interface
217 static struct tap_softc *
218 tapcreate(int unit, cdev_t dev)
220 const char *name = TAP;
222 struct tap_softc *tp;
223 uint8_t ether_addr[ETHER_ADDR_LEN];
225 tp = kmalloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
230 reference_dev(dev); /* tp association */
232 /* generate fake MAC address: 00 bd xx xx xx unit_no */
233 ether_addr[0] = 0x00;
234 ether_addr[1] = 0xbd;
235 bcopy(&ticks, ðer_addr[2], 3);
236 ether_addr[5] = (u_char)unit;
238 /* fill the rest and attach interface */
242 if_initname(ifp, name, unit);
243 if (unit > taplastunit)
246 ifp->if_init = tapifinit;
247 ifp->if_start = tapifstart;
248 ifp->if_ioctl = tapifioctl;
249 ifp->if_mtu = ETHERMTU;
250 ifp->if_flags = TAP_IFFLAGS;
251 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
252 ifq_set_ready(&ifp->if_snd);
254 ether_ifattach(ifp, ether_addr, NULL);
256 tp->tap_flags |= TAP_INITED;
257 tp->tap_devq.ifq_maxlen = ifqmaxlen;
259 SLIST_INSERT_HEAD(&tap_listhead, tp, tap_link);
261 TAPDEBUG(ifp, "created. minor = %#x\n", minor(dev));
269 struct tap_softc *tp;
271 SLIST_FOREACH(tp, &tap_listhead, tap_link) {
272 if (tp->tap_unit == unit)
281 * Create a new tap instance via ifconfig.
284 tap_clone_create(struct if_clone *ifc __unused, int unit)
286 struct tap_softc *tp;
291 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit);
292 dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
293 0600, "%s%d", TAP, unit);
294 tp = tapcreate(unit, dev);
296 tp->tap_flags |= TAP_CLONE;
297 TAPDEBUG(&tp->tap_if, "clone created. minor = %#x tap_flags = 0x%x\n",
298 minor(tp->tap_dev), tp->tap_flags);
306 * to open tunnel. must be superuser
309 tapopen(struct dev_open_args *ap)
312 struct tap_softc *tp = NULL;
313 struct ifnet *ifp = NULL;
317 (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
321 dev = ap->a_head.a_dev;
324 tp = tapcreate(minor(dev), dev);
325 if (tp->tap_flags & TAP_OPEN) {
329 ifp = &tp->arpcom.ac_if;
331 if ((tp->tap_flags & TAP_CLONE) == 0) {
332 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
334 /* Announce the return of the interface. */
335 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
338 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
340 if (curthread->td_proc)
341 fsetown(curthread->td_proc->p_pid, &tp->tap_sigtd);
342 tp->tap_flags |= TAP_OPEN;
345 if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) {
350 ifnet_serialize_all(ifp);
352 ifnet_deserialize_all(ifp);
354 tp->tap_flags |= TAP_CLOSEDOWN;
357 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
358 minor(tp->tap_dev), taprefcnt, taplastunit);
365 tapclone(struct dev_clone_args *ap)
369 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0);
370 ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
371 0600, "%s%d", TAP, unit);
372 tapcreate(unit, ap->a_dev);
379 * close the device - mark i/f down & delete routing info
382 tapclose(struct dev_close_args *ap)
384 cdev_t dev = ap->a_head.a_dev;
385 struct tap_softc *tp = dev->si_drv1;
386 struct ifnet *ifp = &tp->tap_if;
391 /* Junk all pending output */
392 ifq_purge(&ifp->if_snd);
395 * Do not bring the interface down, and do not anything with
396 * interface, if we are in VMnet mode. just close the device.
398 * If the interface is not cloned, we always bring it down.
400 * If the interface is cloned, then we bring it down during
401 * closing only if it was brought up during opening.
403 if ((tp->tap_flags & TAP_VMNET) == 0 &&
404 ((tp->tap_flags & TAP_CLONE) == 0 ||
405 (tp->tap_flags & TAP_CLOSEDOWN))) {
406 if (ifp->if_flags & IFF_UP)
410 ifnet_serialize_all(ifp);
411 tapifstop(tp, clear_flags);
412 ifnet_deserialize_all(ifp);
414 if ((tp->tap_flags & TAP_CLONE) == 0) {
415 if_purgeaddrs_nolink(ifp);
417 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
419 /* Announce the departure of the interface. */
420 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
423 funsetown(tp->tap_sigio);
424 tp->tap_sigio = NULL;
425 selwakeup(&tp->tap_rsel);
427 tp->tap_flags &= ~TAP_OPEN;
428 funsetown(tp->tap_sigtd);
429 tp->tap_sigtd = NULL;
434 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
435 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
438 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
439 minor(tp->tap_dev), taprefcnt, taplastunit);
441 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS)
451 * Destroy a tap instance.
454 tapdestroy(struct tap_softc *tp)
456 struct ifnet *ifp = &tp->arpcom.ac_if;
459 TAPDEBUG(ifp, "destroyed. minor = %#x, refcnt = %d, taplastunit = %d\n",
460 minor(tp->tap_dev), taprefcnt, taplastunit);
462 ifnet_serialize_all(ifp);
464 ifnet_deserialize_all(ifp);
467 SLIST_REMOVE(&tap_listhead, tp, tap_softc, tap_link);
473 release_dev(dev); /* tp association */
476 * Also destroy the cloned device
478 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS) {
480 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), tp->tap_unit);
491 * Destroy a tap instance.
494 tap_clone_destroy(struct ifnet *ifp)
496 struct tap_softc *tp = ifp->if_softc;
498 TAPDEBUG(&tp->tap_if, "clone destroyed. minor = %#x tap_flags = 0x%x\n",
499 minor(tp->tap_dev), tp->tap_flags);
500 if (tp->tap_flags & TAP_CLONE)
507 * Network interface initialization function (called with if serializer held)
514 struct tap_softc *tp = xtp;
515 struct ifnet *ifp = &tp->tap_if;
517 TAPDEBUG(ifp, "initializing, minor = %#x tap_flags = 0x%x\n",
518 minor(tp->tap_dev), tp->tap_flags);
520 ASSERT_IFNET_SERIALIZED_ALL(ifp);
524 ifp->if_flags |= IFF_RUNNING;
525 ifp->if_flags &= ~IFF_OACTIVE;
527 /* attempt to start output */
535 * Process an ioctl request on network interface (called with if serializer
541 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
543 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
544 struct ifstat *ifs = NULL;
551 dummy = ether_ioctl(ifp, cmd, data);
558 case SIOCADDMULTI: /* XXX -- just like vmnet does */
563 ifs = (struct ifstat *)data;
564 dummy = strlen(ifs->ascii);
565 if ((tp->tap_flags & TAP_OPEN) &&
566 dummy < sizeof(ifs->ascii)) {
567 if (tp->tap_sigtd && tp->tap_sigtd->sio_proc) {
568 ksnprintf(ifs->ascii + dummy,
569 sizeof(ifs->ascii) - dummy,
570 "\tOpened by pid %d\n",
571 (int)tp->tap_sigtd->sio_proc->p_pid);
573 ksnprintf(ifs->ascii + dummy,
574 sizeof(ifs->ascii) - dummy,
575 "\tOpened by <unknown>\n");
591 * Queue packets from higher level ready to put out (called with if serializer
597 tapifstart(struct ifnet *ifp)
599 struct tap_softc *tp = ifp->if_softc;
604 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
607 * do not junk pending output if we are in VMnet mode.
608 * XXX: can this do any harm because of queue overflow?
611 if (((tp->tap_flags & TAP_VMNET) == 0) &&
612 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
613 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
614 minor(tp->tap_dev), tp->tap_flags);
615 ifq_purge(&ifp->if_snd);
619 ifp->if_flags |= IFF_OACTIVE;
622 while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) {
635 if (tp->tap_flags & TAP_RWAIT) {
636 tp->tap_flags &= ~TAP_RWAIT;
641 KNOTE(&tp->tap_rsel.si_note, 0);
644 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
646 pgsigio(tp->tap_sigio, SIGIO, 0);
651 * selwakeup is not MPSAFE. tapifstart is.
654 selwakeup(&tp->tap_rsel);
658 ifp->if_flags &= ~IFF_OACTIVE;
665 * The ops interface is now pretty minimal. Called via fileops with nothing
671 tapioctl(struct dev_ioctl_args *ap)
673 cdev_t dev = ap->a_head.a_dev;
674 caddr_t data = ap->a_data;
675 struct tap_softc *tp = dev->si_drv1;
676 struct ifnet *ifp = &tp->tap_if;
677 struct tapinfo *tapp = NULL;
682 ifnet_serialize_all(ifp);
687 tapp = (struct tapinfo *)data;
688 ifp->if_mtu = tapp->mtu;
689 ifp->if_type = tapp->type;
690 ifp->if_baudrate = tapp->baudrate;
694 tapp = (struct tapinfo *)data;
695 tapp->mtu = ifp->if_mtu;
696 tapp->type = ifp->if_type;
697 tapp->baudrate = ifp->if_baudrate;
701 tapdebug = *(int *)data;
705 *(int *)data = tapdebug;
710 tp->tap_flags |= TAP_ASYNC;
712 tp->tap_flags &= ~TAP_ASYNC;
718 /* Take a look at devq first */
719 IF_POLL(&tp->tap_devq, mb);
721 mb = ifq_poll(&ifp->if_snd);
724 for(; mb != NULL; mb = mb->m_next)
725 *(int *)data += mb->m_len;
730 error = fsetown(*(int *)data, &tp->tap_sigio);
734 *(int *)data = fgetown(tp->tap_sigio);
737 /* this is deprecated, FIOSETOWN should be used instead */
739 error = fsetown(-(*(int *)data), &tp->tap_sigio);
742 /* this is deprecated, FIOGETOWN should be used instead */
744 *(int *)data = -fgetown(tp->tap_sigio);
747 /* VMware/VMnet port ioctl's */
749 case SIOCGIFFLAGS: /* get ifnet flags */
750 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
753 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
756 f &= ~IFF_CANTCHANGE;
758 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
761 case OSIOCGIFADDR: /* get MAC address of the remote side */
763 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
766 case SIOCSIFADDR: /* set MAC address of the remote side */
767 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
774 ifnet_deserialize_all(ifp);
782 * The ops read interface - reads a packet at a time, or at
783 * least as much of a packet as can be read.
785 * Called from the fileops interface with nothing held.
790 tapread(struct dev_read_args *ap)
792 cdev_t dev = ap->a_head.a_dev;
793 struct uio *uio = ap->a_uio;
794 struct tap_softc *tp = dev->si_drv1;
795 struct ifnet *ifp = &tp->tap_if;
796 struct mbuf *m0 = NULL;
799 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
801 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
802 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
803 minor(tp->tap_dev), tp->tap_flags);
808 tp->tap_flags &= ~TAP_RWAIT;
810 /* sleep until we get a packet */
812 ifnet_serialize_all(ifp);
813 IF_DEQUEUE(&tp->tap_devq, m0);
815 if (ap->a_ioflag & IO_NDELAY) {
816 ifnet_deserialize_all(ifp);
817 return (EWOULDBLOCK);
819 tp->tap_flags |= TAP_RWAIT;
820 tsleep_interlock(tp, PCATCH);
821 ifnet_deserialize_all(ifp);
822 error = tsleep(tp, PCATCH | PINTERLOCKED, "taprd", 0);
826 ifnet_deserialize_all(ifp);
828 } while (m0 == NULL);
832 /* xfer packet to user space */
833 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
834 len = (int)szmin(uio->uio_resid, m0->m_len);
838 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
843 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
854 * The ops write interface - an atomic write is a packet - or else!
856 * Called from the fileops interface with nothing held.
861 tapwrite(struct dev_write_args *ap)
863 cdev_t dev = ap->a_head.a_dev;
864 struct uio *uio = ap->a_uio;
865 struct tap_softc *tp = dev->si_drv1;
866 struct ifnet *ifp = &tp->tap_if;
867 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
871 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
873 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
874 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
875 minor(tp->tap_dev), tp->tap_flags);
879 if (uio->uio_resid == 0)
882 if (uio->uio_resid > TAPMRU) {
883 TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
884 uio->uio_resid, minor(tp->tap_dev));
888 tlen = uio->uio_resid;
890 /* get a header mbuf */
891 MGETHDR(m, MB_DONTWAIT, MT_DATA);
898 while ((error == 0) && (uio->uio_resid > 0)) {
899 m->m_len = (int)szmin(mlen, uio->uio_resid);
900 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
903 if (uio->uio_resid > 0) {
904 MGET(m, MB_DONTWAIT, MT_DATA);
919 top->m_pkthdr.len = (int)tlen;
920 top->m_pkthdr.rcvif = ifp;
923 * Ethernet bridge and bpf are handled in ether_input
925 * adjust mbuf and give packet to the ether_input
927 ifnet_serialize_all(ifp);
928 ifp->if_input(ifp, top);
929 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
930 ifnet_deserialize_all(ifp);
938 * The poll interface, this is only useful on reads really. The write
939 * detect always returns true, write never blocks anyway, it either
940 * accepts the packet or drops it
942 * Called from the fileops interface with nothing held.
947 tappoll(struct dev_poll_args *ap)
949 cdev_t dev = ap->a_head.a_dev;
950 struct tap_softc *tp = dev->si_drv1;
951 struct ifnet *ifp = &tp->tap_if;
954 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
956 if (ap->a_events & (POLLIN | POLLRDNORM)) {
957 if (!IF_QEMPTY(&tp->tap_devq)) {
959 "has data in queue. minor = %#x\n",
962 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
964 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
968 selrecord(curthread, &tp->tap_rsel);
973 if (ap->a_events & (POLLOUT | POLLWRNORM))
974 revents |= (ap->a_events & (POLLOUT | POLLWRNORM));
975 ap->a_events = revents;
980 * tapkqfilter - called from the fileops interface with nothing held
984 static int filt_tapread(struct knote *kn, long hint);
985 static void filt_tapdetach(struct knote *kn);
986 static struct filterops tapread_filtops =
987 { 1, NULL, filt_tapdetach, filt_tapread };
990 tapkqfilter(struct dev_kqfilter_args *ap)
992 cdev_t dev = ap->a_head.a_dev;
993 struct knote *kn = ap->a_kn;
994 struct tap_softc *tp;
1003 switch(kn->kn_filter) {
1005 list = &tp->tap_rsel.si_note;
1006 kn->kn_fop = &tapread_filtops;
1007 kn->kn_hook = (void *)tp;
1017 SLIST_INSERT_HEAD(list, kn, kn_selnext);
1024 filt_tapread(struct knote *kn, long hint)
1026 struct tap_softc *tp = (void *)kn->kn_hook;
1028 if (IF_QEMPTY(&tp->tap_devq) == 0) /* XXX serializer */
1035 filt_tapdetach(struct knote *kn)
1037 struct tap_softc *tp = (void *)kn->kn_hook;
1039 SLIST_REMOVE(&tp->tap_rsel.si_note, kn, knote, kn_selnext);
1043 tapifstop(struct tap_softc *tp, int clear_flags)
1045 struct ifnet *ifp = &tp->tap_if;
1047 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1048 IF_DRAIN(&tp->tap_devq);
1049 tp->tap_flags &= ~TAP_CLOSEDOWN;
1051 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1055 tapifflags(struct tap_softc *tp)
1057 struct ifnet *ifp = &tp->arpcom.ac_if;
1059 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1060 if ((tp->tap_flags & TAP_VMNET) == 0) {
1062 * Only for non-vmnet tap(4)
1064 if (ifp->if_flags & IFF_UP) {
1065 if ((ifp->if_flags & IFF_RUNNING) == 0)