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 * $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>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/ttycom.h>
58 #include <sys/vnode.h>
59 #include <sys/serialize.h>
61 #include <sys/thread2.h>
62 #include <sys/mplock2.h>
65 #include <net/ethernet.h>
67 #include <net/ifq_var.h>
68 #include <net/if_arp.h>
69 #include <net/if_clone.h>
70 #include <net/if_media.h>
71 #include <net/route.h>
72 #include <sys/devfs.h>
74 #include <netinet/in.h>
76 #include "if_tapvar.h"
79 #define TAP_IFFLAGS (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST)
82 #define TAP_PREALLOCATED_UNITS 4
84 #define TAP_PREALLOCATED_UNITS NTAP
87 #define CDEV_NAME "tap"
88 #define TAPDEBUG if (tapdebug) if_printf
92 #define VMNET_DEV_MASK 0x00010000
94 DEVFS_DECLARE_CLONE_BITMAP(tap);
97 static int tapmodevent (module_t, int, void *);
100 static struct tap_softc *tapcreate(int, cdev_t);
101 static void tapdestroy(struct tap_softc *);
104 static int tap_clone_create(struct if_clone *, int, caddr_t);
105 static int tap_clone_destroy(struct ifnet *);
108 /* network interface */
109 static void tapifstart (struct ifnet *,
110 struct ifaltq_subque *);
111 static int tapifioctl (struct ifnet *, u_long, caddr_t,
113 static void tapifinit (void *);
114 static void tapifstop(struct tap_softc *, int);
115 static void tapifflags(struct tap_softc *);
118 /* character device */
119 static d_open_t tapopen;
120 static d_clone_t tapclone;
121 static d_close_t tapclose;
122 static d_read_t tapread;
123 static d_write_t tapwrite;
124 static d_ioctl_t tapioctl;
125 static d_kqfilter_t tapkqfilter;
127 static struct dev_ops tap_ops = {
134 .d_kqfilter = tapkqfilter
137 static int taprefcnt = 0; /* module ref. counter */
138 static int taplastunit = -1; /* max. open unit number */
139 static int tapdebug = 0; /* debug flag */
140 static int tapuopen = 0; /* all user open() */
141 static int tapuponopen = 0; /* IFF_UP */
143 MALLOC_DECLARE(M_TAP);
144 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
145 struct if_clone tap_cloner = IF_CLONE_INITIALIZER("tap",
146 tap_clone_create, tap_clone_destroy,
148 static SLIST_HEAD(,tap_softc) tap_listhead =
149 SLIST_HEAD_INITIALIZER(&tap_listhead);
151 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
152 SYSCTL_DECL(_net_link);
153 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
154 "Ethernet tunnel software network interface");
155 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
156 "Allow user to open /dev/tap (based on node permissions)");
157 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
158 "Bring interface up when /dev/tap is opened");
159 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
161 DEV_MODULE(if_tap, tapmodevent, NULL);
166 * module event handler
169 tapmodevent(module_t mod, int type, void *data)
171 static int attached = 0;
172 struct tap_softc *tp, *ntp;
180 make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap), tapclone,
181 UID_ROOT, GID_WHEEL, 0600, "tap");
182 SLIST_INIT(&tap_listhead);
183 if_clone_attach(&tap_cloner);
185 for (i = 0; i < TAP_PREALLOCATED_UNITS; ++i) {
186 make_dev(&tap_ops, i, UID_ROOT, GID_WHEEL,
188 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), i);
198 if_clone_detach(&tap_cloner);
200 /* Maintain tap ifs in a local list */
201 SLIST_FOREACH_MUTABLE(tp, &tap_listhead, tap_link, ntp)
206 devfs_clone_handler_del("tap");
207 dev_ops_remove_all(&tap_ops);
208 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(tap));
220 * tapcreate - create or clone an interface
222 static struct tap_softc *
223 tapcreate(int unit, cdev_t dev)
225 const char *name = TAP;
227 struct tap_softc *tp;
228 uint8_t ether_addr[ETHER_ADDR_LEN];
230 tp = kmalloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
235 reference_dev(dev); /* tp association */
237 /* generate fake MAC address: 00 bd xx xx xx unit_no */
238 ether_addr[0] = 0x00;
239 ether_addr[1] = 0xbd;
240 bcopy(&ticks, ðer_addr[2], 3);
241 ether_addr[5] = (u_char)unit;
243 /* fill the rest and attach interface */
247 if_initname(ifp, name, unit);
248 if (unit > taplastunit)
251 ifp->if_init = tapifinit;
252 ifp->if_start = tapifstart;
253 ifp->if_ioctl = tapifioctl;
254 ifp->if_mtu = ETHERMTU;
255 ifp->if_flags = TAP_IFFLAGS;
256 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
257 ifq_set_ready(&ifp->if_snd);
259 ether_ifattach(ifp, ether_addr, NULL);
261 tp->tap_flags |= TAP_INITED;
262 tp->tap_devq.ifq_maxlen = ifqmaxlen;
264 SLIST_INSERT_HEAD(&tap_listhead, tp, tap_link);
266 TAPDEBUG(ifp, "created. minor = %#x\n", minor(dev));
274 struct tap_softc *tp;
276 SLIST_FOREACH(tp, &tap_listhead, tap_link) {
277 if (tp->tap_unit == unit)
286 * Create a new tap instance via ifconfig.
289 tap_clone_create(struct if_clone *ifc __unused, int unit,
290 caddr_t param __unused)
292 struct tap_softc *tp;
297 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tap), unit)) {
298 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit);
299 dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
300 0600, "%s%d", TAP, unit);
302 dev = devfs_find_device_by_name("%s%d", TAP, unit);
305 KKASSERT(dev != NULL);
306 tp = tapcreate(unit, dev);
308 tp->tap_flags |= TAP_CLONE;
309 TAPDEBUG(&tp->tap_if, "clone created. minor = %#x tap_flags = 0x%x\n",
310 minor(tp->tap_dev), tp->tap_flags);
318 * to open tunnel. must be superuser
321 tapopen(struct dev_open_args *ap)
324 struct tap_softc *tp = NULL;
325 struct ifnet *ifp = NULL;
329 (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
333 dev = ap->a_head.a_dev;
336 tp = tapcreate(minor(dev), dev);
337 if (tp->tap_flags & TAP_OPEN) {
341 ifp = &tp->arpcom.ac_if;
343 if ((tp->tap_flags & TAP_CLONE) == 0) {
344 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
346 /* Announce the return of the interface. */
347 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
350 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
352 if (curthread->td_proc)
353 fsetown(curthread->td_proc->p_pid, &tp->tap_sigtd);
354 tp->tap_flags |= TAP_OPEN;
357 if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) {
362 ifnet_serialize_all(ifp);
364 ifnet_deserialize_all(ifp);
366 tp->tap_flags |= TAP_CLOSEDOWN;
369 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
370 minor(tp->tap_dev), taprefcnt, taplastunit);
377 tapclone(struct dev_clone_args *ap)
381 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0);
382 ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
383 0600, "%s%d", TAP, unit);
384 tapcreate(unit, ap->a_dev);
391 * close the device - mark i/f down & delete routing info
394 tapclose(struct dev_close_args *ap)
396 cdev_t dev = ap->a_head.a_dev;
397 struct tap_softc *tp = dev->si_drv1;
398 struct ifnet *ifp = &tp->tap_if;
403 /* Junk all pending output */
404 ifq_purge_all(&ifp->if_snd);
407 * Do not bring the interface down, and do not anything with
408 * interface, if we are in VMnet mode. just close the device.
410 * If the interface is not cloned, we always bring it down.
412 * If the interface is cloned, then we bring it down during
413 * closing only if it was brought up during opening.
415 if ((tp->tap_flags & TAP_VMNET) == 0 &&
416 ((tp->tap_flags & TAP_CLONE) == 0 ||
417 (tp->tap_flags & TAP_CLOSEDOWN))) {
418 if (ifp->if_flags & IFF_UP)
422 ifnet_serialize_all(ifp);
423 tapifstop(tp, clear_flags);
424 ifnet_deserialize_all(ifp);
426 if ((tp->tap_flags & TAP_CLONE) == 0) {
427 if_purgeaddrs_nolink(ifp);
429 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
431 /* Announce the departure of the interface. */
432 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
435 funsetown(&tp->tap_sigio);
436 tp->tap_sigio = NULL;
437 KNOTE(&tp->tap_rkq.ki_note, 0);
439 tp->tap_flags &= ~TAP_OPEN;
440 funsetown(&tp->tap_sigtd);
441 tp->tap_sigtd = NULL;
446 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
447 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
450 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
451 minor(tp->tap_dev), taprefcnt, taplastunit);
453 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS)
463 * Destroy a tap instance.
466 tapdestroy(struct tap_softc *tp)
468 struct ifnet *ifp = &tp->arpcom.ac_if;
471 TAPDEBUG(ifp, "destroyed. minor = %#x, refcnt = %d, taplastunit = %d\n",
472 minor(tp->tap_dev), taprefcnt, taplastunit);
474 ifnet_serialize_all(ifp);
476 ifnet_deserialize_all(ifp);
479 SLIST_REMOVE(&tap_listhead, tp, tap_softc, tap_link);
485 release_dev(dev); /* tp association */
488 * Also destroy the cloned device
490 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS) {
492 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), tp->tap_unit);
503 * Destroy a tap instance.
506 tap_clone_destroy(struct ifnet *ifp)
508 struct tap_softc *tp = ifp->if_softc;
510 if ((tp->tap_flags & TAP_CLONE) == 0)
513 TAPDEBUG(&tp->tap_if, "clone destroyed. minor = %#x tap_flags = 0x%x\n",
514 minor(tp->tap_dev), tp->tap_flags);
523 * Network interface initialization function (called with if serializer held)
530 struct tap_softc *tp = xtp;
531 struct ifnet *ifp = &tp->tap_if;
532 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
534 TAPDEBUG(ifp, "initializing, minor = %#x tap_flags = 0x%x\n",
535 minor(tp->tap_dev), tp->tap_flags);
537 ASSERT_IFNET_SERIALIZED_ALL(ifp);
541 ifp->if_flags |= IFF_RUNNING;
542 ifsq_clr_oactive(ifsq);
544 /* attempt to start output */
545 tapifstart(ifp, ifsq);
552 * Process an ioctl request on network interface (called with if serializer
558 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
560 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
561 struct ifstat *ifs = NULL;
562 struct ifmediareq *ifmr = NULL;
570 error = ether_ioctl(ifp, cmd, data);
577 case SIOCADDMULTI: /* XXX -- just like vmnet does */
583 * The bridge code needs this when running the
584 * spanning tree protocol.
586 ifmr = (struct ifmediareq *)data;
587 dummy = ifmr->ifm_count;
589 ifmr->ifm_status = IFM_AVALID;
590 ifmr->ifm_active = IFM_ETHER;
591 if (tp->tap_flags & TAP_OPEN)
592 ifmr->ifm_status |= IFM_ACTIVE;
593 ifmr->ifm_current = ifmr->ifm_active;
595 int media = IFM_ETHER;
596 error = copyout(&media,
603 ifs = (struct ifstat *)data;
604 dummy = strlen(ifs->ascii);
605 if ((tp->tap_flags & TAP_OPEN) &&
606 dummy < sizeof(ifs->ascii)) {
607 if (tp->tap_sigtd && tp->tap_sigtd->sio_proc) {
608 ksnprintf(ifs->ascii + dummy,
609 sizeof(ifs->ascii) - dummy,
610 "\tOpened by pid %d\n",
611 (int)tp->tap_sigtd->sio_proc->p_pid);
613 ksnprintf(ifs->ascii + dummy,
614 sizeof(ifs->ascii) - dummy,
615 "\tOpened by <unknown>\n");
632 * Queue packets from higher level ready to put out (called with if serializer
638 tapifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
640 struct tap_softc *tp = ifp->if_softc;
645 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
646 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
649 * do not junk pending output if we are in VMnet mode.
650 * XXX: can this do any harm because of queue overflow?
653 if (((tp->tap_flags & TAP_VMNET) == 0) &&
654 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
655 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
656 minor(tp->tap_dev), tp->tap_flags);
661 ifsq_set_oactive(ifsq);
664 while ((m = ifsq_dequeue(ifsq, NULL)) != NULL) {
677 if (tp->tap_flags & TAP_RWAIT) {
678 tp->tap_flags &= ~TAP_RWAIT;
682 KNOTE(&tp->tap_rkq.ki_note, 0);
684 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
686 pgsigio(tp->tap_sigio, SIGIO, 0);
691 ifsq_clr_oactive(ifsq);
698 * The ops interface is now pretty minimal. Called via fileops with nothing
704 tapioctl(struct dev_ioctl_args *ap)
706 cdev_t dev = ap->a_head.a_dev;
707 caddr_t data = ap->a_data;
708 struct tap_softc *tp = dev->si_drv1;
709 struct ifnet *ifp = &tp->tap_if;
710 struct tapinfo *tapp = NULL;
715 ifnet_serialize_all(ifp);
720 tapp = (struct tapinfo *)data;
721 ifp->if_mtu = tapp->mtu;
722 ifp->if_type = tapp->type;
723 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;
743 tp->tap_flags |= TAP_ASYNC;
745 tp->tap_flags &= ~TAP_ASYNC;
751 /* Take a look at devq first */
752 IF_POLL(&tp->tap_devq, mb);
754 mb = ifsq_poll(ifq_get_subq_default(&ifp->if_snd));
757 for(; mb != NULL; mb = mb->m_next)
758 *(int *)data += mb->m_len;
763 error = fsetown(*(int *)data, &tp->tap_sigio);
767 *(int *)data = fgetown(&tp->tap_sigio);
770 /* this is deprecated, FIOSETOWN should be used instead */
772 error = fsetown(-(*(int *)data), &tp->tap_sigio);
775 /* this is deprecated, FIOGETOWN should be used instead */
777 *(int *)data = -fgetown(&tp->tap_sigio);
780 /* VMware/VMnet port ioctl's */
782 case SIOCGIFFLAGS: /* get ifnet flags */
783 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
786 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
789 f &= ~IFF_CANTCHANGE;
791 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
794 case OSIOCGIFADDR: /* get MAC address of the remote side */
796 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
799 case SIOCSIFADDR: /* set MAC address of the remote side */
800 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
807 ifnet_deserialize_all(ifp);
815 * The ops read interface - reads a packet at a time, or at
816 * least as much of a packet as can be read.
818 * Called from the fileops interface with nothing held.
823 tapread(struct dev_read_args *ap)
825 cdev_t dev = ap->a_head.a_dev;
826 struct uio *uio = ap->a_uio;
827 struct tap_softc *tp = dev->si_drv1;
828 struct ifnet *ifp = &tp->tap_if;
829 struct mbuf *m0 = NULL;
832 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
834 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
835 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
836 minor(tp->tap_dev), tp->tap_flags);
841 tp->tap_flags &= ~TAP_RWAIT;
843 /* sleep until we get a packet */
845 ifnet_serialize_all(ifp);
846 IF_DEQUEUE(&tp->tap_devq, m0);
848 if (ap->a_ioflag & IO_NDELAY) {
849 ifnet_deserialize_all(ifp);
850 return (EWOULDBLOCK);
852 tp->tap_flags |= TAP_RWAIT;
853 tsleep_interlock(tp, PCATCH);
854 ifnet_deserialize_all(ifp);
855 error = tsleep(tp, PCATCH | PINTERLOCKED, "taprd", 0);
859 ifnet_deserialize_all(ifp);
861 } while (m0 == NULL);
865 /* xfer packet to user space */
866 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
867 len = (int)szmin(uio->uio_resid, m0->m_len);
871 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
876 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
887 * The ops write interface - an atomic write is a packet - or else!
889 * Called from the fileops interface with nothing held.
894 tapwrite(struct dev_write_args *ap)
896 cdev_t dev = ap->a_head.a_dev;
897 struct uio *uio = ap->a_uio;
898 struct tap_softc *tp = dev->si_drv1;
899 struct ifnet *ifp = &tp->tap_if;
900 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
904 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
906 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
907 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
908 minor(tp->tap_dev), tp->tap_flags);
912 if (uio->uio_resid == 0)
915 if (uio->uio_resid > TAPMRU) {
916 TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
917 uio->uio_resid, minor(tp->tap_dev));
921 tlen = uio->uio_resid;
923 /* get a header mbuf */
924 MGETHDR(m, MB_WAIT, MT_DATA);
933 while (error == 0 && uio->uio_resid > 0) {
934 m->m_len = (int)szmin(mlen, uio->uio_resid);
935 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
938 if (uio->uio_resid > 0) {
939 MGET(m, MB_WAIT, MT_DATA);
954 top->m_pkthdr.len = (int)tlen;
955 top->m_pkthdr.rcvif = ifp;
958 * Ethernet bridge and bpf are handled in ether_input
960 * adjust mbuf and give packet to the ether_input
962 ifnet_serialize_all(ifp);
963 ifp->if_input(ifp, top);
964 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
965 ifnet_deserialize_all(ifp);
971 * tapkqfilter - called from the fileops interface with nothing held
975 static int filt_tapread(struct knote *kn, long hint);
976 static int filt_tapwrite(struct knote *kn, long hint);
977 static void filt_tapdetach(struct knote *kn);
978 static struct filterops tapread_filtops =
979 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapread };
980 static struct filterops tapwrite_filtops =
981 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapwrite };
984 tapkqfilter(struct dev_kqfilter_args *ap)
986 cdev_t dev = ap->a_head.a_dev;
987 struct knote *kn = ap->a_kn;
988 struct tap_softc *tp;
992 list = &tp->tap_rkq.ki_note;
995 switch(kn->kn_filter) {
997 kn->kn_fop = &tapread_filtops;
998 kn->kn_hook = (void *)tp;
1001 kn->kn_fop = &tapwrite_filtops;
1002 kn->kn_hook = (void *)tp;
1005 ap->a_result = EOPNOTSUPP;
1009 knote_insert(list, kn);
1014 filt_tapread(struct knote *kn, long hint)
1016 struct tap_softc *tp = (void *)kn->kn_hook;
1018 if (IF_QEMPTY(&tp->tap_devq) == 0) /* XXX serializer */
1025 filt_tapwrite(struct knote *kn, long hint)
1027 /* Always ready for a write */
1032 filt_tapdetach(struct knote *kn)
1034 struct tap_softc *tp = (void *)kn->kn_hook;
1036 knote_remove(&tp->tap_rkq.ki_note, kn);
1040 tapifstop(struct tap_softc *tp, int clear_flags)
1042 struct ifnet *ifp = &tp->tap_if;
1044 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1045 IF_DRAIN(&tp->tap_devq);
1046 tp->tap_flags &= ~TAP_CLOSEDOWN;
1048 ifp->if_flags &= ~IFF_RUNNING;
1049 ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
1054 tapifflags(struct tap_softc *tp)
1056 struct ifnet *ifp = &tp->arpcom.ac_if;
1058 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1059 if ((tp->tap_flags & TAP_VMNET) == 0) {
1061 * Only for non-vmnet tap(4)
1063 if (ifp->if_flags & IFF_UP) {
1064 if ((ifp->if_flags & IFF_RUNNING) == 0)