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 static int tapifioctl (struct ifnet *, u_long, caddr_t,
112 static void tapifinit (void *);
113 static void tapifstop(struct tap_softc *, int);
114 static void tapifflags(struct tap_softc *);
117 /* character device */
118 static d_open_t tapopen;
119 static d_clone_t tapclone;
120 static d_close_t tapclose;
121 static d_read_t tapread;
122 static d_write_t tapwrite;
123 static d_ioctl_t tapioctl;
124 static d_kqfilter_t tapkqfilter;
126 static struct dev_ops tap_ops = {
133 .d_kqfilter = tapkqfilter
136 static int taprefcnt = 0; /* module ref. counter */
137 static int taplastunit = -1; /* max. open unit number */
138 static int tapdebug = 0; /* debug flag */
139 static int tapuopen = 0; /* all user open() */
140 static int tapuponopen = 0; /* IFF_UP */
142 MALLOC_DECLARE(M_TAP);
143 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
144 struct if_clone tap_cloner = IF_CLONE_INITIALIZER("tap",
145 tap_clone_create, tap_clone_destroy,
147 static SLIST_HEAD(,tap_softc) tap_listhead =
148 SLIST_HEAD_INITIALIZER(&tap_listhead);
150 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
151 SYSCTL_DECL(_net_link);
152 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
153 "Ethernet tunnel software network interface");
154 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
155 "Allow user to open /dev/tap (based on node permissions)");
156 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
157 "Bring interface up when /dev/tap is opened");
158 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
160 DEV_MODULE(if_tap, tapmodevent, NULL);
165 * module event handler
168 tapmodevent(module_t mod, int type, void *data)
170 static int attached = 0;
171 struct tap_softc *tp, *ntp;
179 make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap), tapclone,
180 UID_ROOT, GID_WHEEL, 0600, "tap");
181 SLIST_INIT(&tap_listhead);
182 if_clone_attach(&tap_cloner);
184 for (i = 0; i < TAP_PREALLOCATED_UNITS; ++i) {
185 make_dev(&tap_ops, i, UID_ROOT, GID_WHEEL,
187 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), i);
197 if_clone_detach(&tap_cloner);
199 /* Maintain tap ifs in a local list */
200 SLIST_FOREACH_MUTABLE(tp, &tap_listhead, tap_link, ntp)
205 devfs_clone_handler_del("tap");
206 dev_ops_remove_all(&tap_ops);
207 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(tap));
219 * tapcreate - create or clone an interface
221 static struct tap_softc *
222 tapcreate(int unit, cdev_t dev)
224 const char *name = TAP;
226 struct tap_softc *tp;
227 uint8_t ether_addr[ETHER_ADDR_LEN];
229 tp = kmalloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
234 reference_dev(dev); /* tp association */
236 /* generate fake MAC address: 00 bd xx xx xx unit_no */
237 ether_addr[0] = 0x00;
238 ether_addr[1] = 0xbd;
239 bcopy(&ticks, ðer_addr[2], 3);
240 ether_addr[5] = (u_char)unit;
242 /* fill the rest and attach interface */
246 if_initname(ifp, name, unit);
247 if (unit > taplastunit)
250 ifp->if_init = tapifinit;
251 ifp->if_start = tapifstart;
252 ifp->if_ioctl = tapifioctl;
253 ifp->if_mtu = ETHERMTU;
254 ifp->if_flags = TAP_IFFLAGS;
255 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
256 ifq_set_ready(&ifp->if_snd);
258 ether_ifattach(ifp, ether_addr, NULL);
260 tp->tap_flags |= TAP_INITED;
261 tp->tap_devq.ifq_maxlen = ifqmaxlen;
263 SLIST_INSERT_HEAD(&tap_listhead, tp, tap_link);
265 TAPDEBUG(ifp, "created. minor = %#x\n", minor(dev));
273 struct tap_softc *tp;
275 SLIST_FOREACH(tp, &tap_listhead, tap_link) {
276 if (tp->tap_unit == unit)
285 * Create a new tap instance via ifconfig.
288 tap_clone_create(struct if_clone *ifc __unused, int unit,
289 caddr_t param __unused)
291 struct tap_softc *tp;
296 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tap), unit)) {
297 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit);
298 dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
299 0600, "%s%d", TAP, unit);
301 dev = devfs_find_device_by_name("%s%d", TAP, unit);
304 KKASSERT(dev != NULL);
305 tp = tapcreate(unit, dev);
307 tp->tap_flags |= TAP_CLONE;
308 TAPDEBUG(&tp->tap_if, "clone created. minor = %#x tap_flags = 0x%x\n",
309 minor(tp->tap_dev), tp->tap_flags);
317 * to open tunnel. must be superuser
320 tapopen(struct dev_open_args *ap)
323 struct tap_softc *tp = NULL;
324 struct ifnet *ifp = NULL;
328 (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
332 dev = ap->a_head.a_dev;
335 tp = tapcreate(minor(dev), dev);
336 if (tp->tap_flags & TAP_OPEN) {
340 ifp = &tp->arpcom.ac_if;
342 if ((tp->tap_flags & TAP_CLONE) == 0) {
343 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
345 /* Announce the return of the interface. */
346 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
349 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
351 if (curthread->td_proc)
352 fsetown(curthread->td_proc->p_pid, &tp->tap_sigtd);
353 tp->tap_flags |= TAP_OPEN;
356 if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) {
361 ifnet_serialize_all(ifp);
363 ifnet_deserialize_all(ifp);
365 tp->tap_flags |= TAP_CLOSEDOWN;
368 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
369 minor(tp->tap_dev), taprefcnt, taplastunit);
376 tapclone(struct dev_clone_args *ap)
380 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0);
381 ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
382 0600, "%s%d", TAP, unit);
383 tapcreate(unit, ap->a_dev);
390 * close the device - mark i/f down & delete routing info
393 tapclose(struct dev_close_args *ap)
395 cdev_t dev = ap->a_head.a_dev;
396 struct tap_softc *tp = dev->si_drv1;
397 struct ifnet *ifp = &tp->tap_if;
402 /* Junk all pending output */
403 ifq_purge(&ifp->if_snd);
406 * Do not bring the interface down, and do not anything with
407 * interface, if we are in VMnet mode. just close the device.
409 * If the interface is not cloned, we always bring it down.
411 * If the interface is cloned, then we bring it down during
412 * closing only if it was brought up during opening.
414 if ((tp->tap_flags & TAP_VMNET) == 0 &&
415 ((tp->tap_flags & TAP_CLONE) == 0 ||
416 (tp->tap_flags & TAP_CLOSEDOWN))) {
417 if (ifp->if_flags & IFF_UP)
421 ifnet_serialize_all(ifp);
422 tapifstop(tp, clear_flags);
423 ifnet_deserialize_all(ifp);
425 if ((tp->tap_flags & TAP_CLONE) == 0) {
426 if_purgeaddrs_nolink(ifp);
428 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
430 /* Announce the departure of the interface. */
431 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
434 funsetown(&tp->tap_sigio);
435 tp->tap_sigio = NULL;
436 KNOTE(&tp->tap_rkq.ki_note, 0);
438 tp->tap_flags &= ~TAP_OPEN;
439 funsetown(&tp->tap_sigtd);
440 tp->tap_sigtd = NULL;
445 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
446 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
449 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
450 minor(tp->tap_dev), taprefcnt, taplastunit);
452 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS)
462 * Destroy a tap instance.
465 tapdestroy(struct tap_softc *tp)
467 struct ifnet *ifp = &tp->arpcom.ac_if;
470 TAPDEBUG(ifp, "destroyed. minor = %#x, refcnt = %d, taplastunit = %d\n",
471 minor(tp->tap_dev), taprefcnt, taplastunit);
473 ifnet_serialize_all(ifp);
475 ifnet_deserialize_all(ifp);
478 SLIST_REMOVE(&tap_listhead, tp, tap_softc, tap_link);
484 release_dev(dev); /* tp association */
487 * Also destroy the cloned device
489 if (tp->tap_unit >= TAP_PREALLOCATED_UNITS) {
491 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), tp->tap_unit);
502 * Destroy a tap instance.
505 tap_clone_destroy(struct ifnet *ifp)
507 struct tap_softc *tp = ifp->if_softc;
509 if ((tp->tap_flags & TAP_CLONE) == 0)
512 TAPDEBUG(&tp->tap_if, "clone destroyed. minor = %#x tap_flags = 0x%x\n",
513 minor(tp->tap_dev), tp->tap_flags);
522 * Network interface initialization function (called with if serializer held)
529 struct tap_softc *tp = xtp;
530 struct ifnet *ifp = &tp->tap_if;
532 TAPDEBUG(ifp, "initializing, minor = %#x tap_flags = 0x%x\n",
533 minor(tp->tap_dev), tp->tap_flags);
535 ASSERT_IFNET_SERIALIZED_ALL(ifp);
539 ifp->if_flags |= IFF_RUNNING;
540 ifp->if_flags &= ~IFF_OACTIVE;
542 /* attempt to start output */
550 * Process an ioctl request on network interface (called with if serializer
556 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
558 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
559 struct ifstat *ifs = NULL;
560 struct ifmediareq *ifmr = NULL;
568 error = ether_ioctl(ifp, cmd, data);
575 case SIOCADDMULTI: /* XXX -- just like vmnet does */
581 * The bridge code needs this when running the
582 * spanning tree protocol.
584 ifmr = (struct ifmediareq *)data;
585 dummy = ifmr->ifm_count;
587 ifmr->ifm_status = IFM_AVALID;
588 ifmr->ifm_active = IFM_ETHER;
589 if (tp->tap_flags & TAP_OPEN)
590 ifmr->ifm_status |= IFM_ACTIVE;
591 ifmr->ifm_current = ifmr->ifm_active;
593 int media = IFM_ETHER;
594 error = copyout(&media,
601 ifs = (struct ifstat *)data;
602 dummy = strlen(ifs->ascii);
603 if ((tp->tap_flags & TAP_OPEN) &&
604 dummy < sizeof(ifs->ascii)) {
605 if (tp->tap_sigtd && tp->tap_sigtd->sio_proc) {
606 ksnprintf(ifs->ascii + dummy,
607 sizeof(ifs->ascii) - dummy,
608 "\tOpened by pid %d\n",
609 (int)tp->tap_sigtd->sio_proc->p_pid);
611 ksnprintf(ifs->ascii + dummy,
612 sizeof(ifs->ascii) - dummy,
613 "\tOpened by <unknown>\n");
630 * Queue packets from higher level ready to put out (called with if serializer
636 tapifstart(struct ifnet *ifp)
638 struct tap_softc *tp = ifp->if_softc;
643 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
646 * do not junk pending output if we are in VMnet mode.
647 * XXX: can this do any harm because of queue overflow?
650 if (((tp->tap_flags & TAP_VMNET) == 0) &&
651 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
652 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
653 minor(tp->tap_dev), tp->tap_flags);
654 ifq_purge(&ifp->if_snd);
658 ifp->if_flags |= IFF_OACTIVE;
661 while ((m = ifq_dequeue(&ifp->if_snd, NULL)) != NULL) {
674 if (tp->tap_flags & TAP_RWAIT) {
675 tp->tap_flags &= ~TAP_RWAIT;
679 KNOTE(&tp->tap_rkq.ki_note, 0);
681 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
683 pgsigio(tp->tap_sigio, SIGIO, 0);
688 ifp->if_flags &= ~IFF_OACTIVE;
695 * The ops interface is now pretty minimal. Called via fileops with nothing
701 tapioctl(struct dev_ioctl_args *ap)
703 cdev_t dev = ap->a_head.a_dev;
704 caddr_t data = ap->a_data;
705 struct tap_softc *tp = dev->si_drv1;
706 struct ifnet *ifp = &tp->tap_if;
707 struct tapinfo *tapp = NULL;
712 ifnet_serialize_all(ifp);
717 tapp = (struct tapinfo *)data;
718 ifp->if_mtu = tapp->mtu;
719 ifp->if_type = tapp->type;
720 ifp->if_baudrate = tapp->baudrate;
724 tapp = (struct tapinfo *)data;
725 tapp->mtu = ifp->if_mtu;
726 tapp->type = ifp->if_type;
727 tapp->baudrate = ifp->if_baudrate;
731 tapdebug = *(int *)data;
735 *(int *)data = tapdebug;
740 tp->tap_flags |= TAP_ASYNC;
742 tp->tap_flags &= ~TAP_ASYNC;
748 /* Take a look at devq first */
749 IF_POLL(&tp->tap_devq, mb);
751 mb = ifq_poll(&ifp->if_snd);
754 for(; mb != NULL; mb = mb->m_next)
755 *(int *)data += mb->m_len;
760 error = fsetown(*(int *)data, &tp->tap_sigio);
764 *(int *)data = fgetown(&tp->tap_sigio);
767 /* this is deprecated, FIOSETOWN should be used instead */
769 error = fsetown(-(*(int *)data), &tp->tap_sigio);
772 /* this is deprecated, FIOGETOWN should be used instead */
774 *(int *)data = -fgetown(&tp->tap_sigio);
777 /* VMware/VMnet port ioctl's */
779 case SIOCGIFFLAGS: /* get ifnet flags */
780 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
783 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
786 f &= ~IFF_CANTCHANGE;
788 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
791 case OSIOCGIFADDR: /* get MAC address of the remote side */
793 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
796 case SIOCSIFADDR: /* set MAC address of the remote side */
797 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
804 ifnet_deserialize_all(ifp);
812 * The ops read interface - reads a packet at a time, or at
813 * least as much of a packet as can be read.
815 * Called from the fileops interface with nothing held.
820 tapread(struct dev_read_args *ap)
822 cdev_t dev = ap->a_head.a_dev;
823 struct uio *uio = ap->a_uio;
824 struct tap_softc *tp = dev->si_drv1;
825 struct ifnet *ifp = &tp->tap_if;
826 struct mbuf *m0 = NULL;
829 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
831 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
832 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
833 minor(tp->tap_dev), tp->tap_flags);
838 tp->tap_flags &= ~TAP_RWAIT;
840 /* sleep until we get a packet */
842 ifnet_serialize_all(ifp);
843 IF_DEQUEUE(&tp->tap_devq, m0);
845 if (ap->a_ioflag & IO_NDELAY) {
846 ifnet_deserialize_all(ifp);
847 return (EWOULDBLOCK);
849 tp->tap_flags |= TAP_RWAIT;
850 tsleep_interlock(tp, PCATCH);
851 ifnet_deserialize_all(ifp);
852 error = tsleep(tp, PCATCH | PINTERLOCKED, "taprd", 0);
856 ifnet_deserialize_all(ifp);
858 } while (m0 == NULL);
862 /* xfer packet to user space */
863 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
864 len = (int)szmin(uio->uio_resid, m0->m_len);
868 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
873 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
884 * The ops write interface - an atomic write is a packet - or else!
886 * Called from the fileops interface with nothing held.
891 tapwrite(struct dev_write_args *ap)
893 cdev_t dev = ap->a_head.a_dev;
894 struct uio *uio = ap->a_uio;
895 struct tap_softc *tp = dev->si_drv1;
896 struct ifnet *ifp = &tp->tap_if;
897 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
901 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
903 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
904 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
905 minor(tp->tap_dev), tp->tap_flags);
909 if (uio->uio_resid == 0)
912 if (uio->uio_resid > TAPMRU) {
913 TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
914 uio->uio_resid, minor(tp->tap_dev));
918 tlen = uio->uio_resid;
920 /* get a header mbuf */
921 MGETHDR(m, MB_WAIT, MT_DATA);
930 while (error == 0 && uio->uio_resid > 0) {
931 m->m_len = (int)szmin(mlen, uio->uio_resid);
932 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
935 if (uio->uio_resid > 0) {
936 MGET(m, MB_WAIT, MT_DATA);
951 top->m_pkthdr.len = (int)tlen;
952 top->m_pkthdr.rcvif = ifp;
955 * Ethernet bridge and bpf are handled in ether_input
957 * adjust mbuf and give packet to the ether_input
959 ifnet_serialize_all(ifp);
960 ifp->if_input(ifp, top);
961 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
962 ifnet_deserialize_all(ifp);
968 * tapkqfilter - called from the fileops interface with nothing held
972 static int filt_tapread(struct knote *kn, long hint);
973 static int filt_tapwrite(struct knote *kn, long hint);
974 static void filt_tapdetach(struct knote *kn);
975 static struct filterops tapread_filtops =
976 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapread };
977 static struct filterops tapwrite_filtops =
978 { FILTEROP_ISFD, NULL, filt_tapdetach, filt_tapwrite };
981 tapkqfilter(struct dev_kqfilter_args *ap)
983 cdev_t dev = ap->a_head.a_dev;
984 struct knote *kn = ap->a_kn;
985 struct tap_softc *tp;
989 list = &tp->tap_rkq.ki_note;
992 switch(kn->kn_filter) {
994 kn->kn_fop = &tapread_filtops;
995 kn->kn_hook = (void *)tp;
998 kn->kn_fop = &tapwrite_filtops;
999 kn->kn_hook = (void *)tp;
1002 ap->a_result = EOPNOTSUPP;
1006 knote_insert(list, kn);
1011 filt_tapread(struct knote *kn, long hint)
1013 struct tap_softc *tp = (void *)kn->kn_hook;
1015 if (IF_QEMPTY(&tp->tap_devq) == 0) /* XXX serializer */
1022 filt_tapwrite(struct knote *kn, long hint)
1024 /* Always ready for a write */
1029 filt_tapdetach(struct knote *kn)
1031 struct tap_softc *tp = (void *)kn->kn_hook;
1033 knote_remove(&tp->tap_rkq.ki_note, kn);
1037 tapifstop(struct tap_softc *tp, int clear_flags)
1039 struct ifnet *ifp = &tp->tap_if;
1041 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1042 IF_DRAIN(&tp->tap_devq);
1043 tp->tap_flags &= ~TAP_CLOSEDOWN;
1045 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1049 tapifflags(struct tap_softc *tp)
1051 struct ifnet *ifp = &tp->arpcom.ac_if;
1053 ASSERT_IFNET_SERIALIZED_ALL(ifp);
1054 if ((tp->tap_flags & TAP_VMNET) == 0) {
1056 * Only for non-vmnet tap(4)
1058 if (ifp->if_flags & IFF_UP) {
1059 if ((ifp->if_flags & IFF_RUNNING) == 0)