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>
71 #include <netinet/in.h>
73 #include "if_tapvar.h"
76 #define TAP_IFFLAGS (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST)
78 #define CDEV_NAME "tap"
79 #define CDEV_MAJOR 149
80 #define TAPDEBUG if (tapdebug) if_printf
84 #define VMNET_DEV_MASK 0x00010000
87 static int tapmodevent (module_t
, int, void *);
90 static void tapcreate (cdev_t
);
91 static void tapdestroy(struct tap_softc
*);
94 static int tap_clone_create(struct if_clone
*, int);
95 static void tap_clone_destroy(struct ifnet
*);
98 /* network interface */
99 static void tapifstart (struct ifnet
*);
100 static int tapifioctl (struct ifnet
*, u_long
, caddr_t
,
102 static void tapifinit (void *);
103 static void tapifstop(struct tap_softc
*, int);
104 static void tapifflags(struct tap_softc
*);
106 /* character device */
107 static d_open_t tapopen
;
108 static d_close_t tapclose
;
109 static d_read_t tapread
;
110 static d_write_t tapwrite
;
111 static d_ioctl_t tapioctl
;
112 static d_poll_t tappoll
;
113 static d_kqfilter_t tapkqfilter
;
115 static struct dev_ops tap_ops
= {
116 { CDEV_NAME
, CDEV_MAJOR
, 0 },
123 .d_kqfilter
= tapkqfilter
126 static int taprefcnt
= 0; /* module ref. counter */
127 static int taplastunit
= -1; /* max. open unit number */
128 static int tapdebug
= 0; /* debug flag */
129 static int tapuopen
= 0; /* all user open() */
130 static int tapuponopen
= 0; /* IFF_UP */
132 MALLOC_DECLARE(M_TAP
);
133 MALLOC_DEFINE(M_TAP
, CDEV_NAME
, "Ethernet tunnel interface");
134 struct if_clone tap_cloner
= IF_CLONE_INITIALIZER("tap",
135 tap_clone_create
, tap_clone_destroy
,
137 static SLIST_HEAD(,tap_softc
) tap_listhead
=
138 SLIST_HEAD_INITIALIZER(&tap_listhead
);
140 SYSCTL_INT(_debug
, OID_AUTO
, if_tap_debug
, CTLFLAG_RW
, &tapdebug
, 0, "");
141 SYSCTL_DECL(_net_link
);
142 SYSCTL_NODE(_net_link
, OID_AUTO
, tap
, CTLFLAG_RW
, 0,
143 "Ethernet tunnel software network interface");
144 SYSCTL_INT(_net_link_tap
, OID_AUTO
, user_open
, CTLFLAG_RW
, &tapuopen
, 0,
145 "Allow user to open /dev/tap (based on node permissions)");
146 SYSCTL_INT(_net_link_tap
, OID_AUTO
, up_on_open
, CTLFLAG_RW
, &tapuponopen
, 0,
147 "Bring interface up when /dev/tap is opened");
148 SYSCTL_INT(_net_link_tap
, OID_AUTO
, debug
, CTLFLAG_RW
, &tapdebug
, 0, "");
150 DEV_MODULE(if_tap
, tapmodevent
, NULL
);
155 * module event handler
158 tapmodevent(module_t mod
, int type
, void *data
)
160 static int attached
= 0;
161 struct tap_softc
*tp
, *ntp
;
168 dev_ops_add(&tap_ops
, 0, 0);
169 SLIST_INIT(&tap_listhead
);
170 if_clone_attach(&tap_cloner
);
179 if_clone_detach(&tap_cloner
);
180 dev_ops_remove(&tap_ops
, 0, 0);
182 /* Maintain tap ifs in a local list */
183 SLIST_FOREACH_MUTABLE(tp
, &tap_listhead
, tap_link
, ntp
)
200 * to create interface
203 tapcreate(cdev_t dev
)
205 struct ifnet
*ifp
= NULL
;
206 struct tap_softc
*tp
= NULL
;
207 uint8_t ether_addr
[ETHER_ADDR_LEN
];
211 /* allocate driver storage and create device */
212 MALLOC(tp
, struct tap_softc
*, sizeof(*tp
), M_TAP
, M_WAITOK
| M_ZERO
);
214 /* select device: tap or vmnet */
215 if (minor(dev
) & VMNET_DEV_MASK
) {
217 unit
= lminor(dev
) & 0xff;
218 tp
->tap_flags
|= TAP_VMNET
;
225 tp
->tap_dev
= make_dev(&tap_ops
, minor(dev
), UID_ROOT
, GID_WHEEL
,
226 0600, "%s%d", name
, unit
);
227 tp
->tap_dev
->si_drv1
= dev
->si_drv1
= tp
;
228 reference_dev(tp
->tap_dev
); /* so we can destroy it later */
230 /* generate fake MAC address: 00 bd xx xx xx unit_no */
231 ether_addr
[0] = 0x00;
232 ether_addr
[1] = 0xbd;
233 bcopy(&ticks
, ðer_addr
[2], 3);
234 ether_addr
[5] = (u_char
)unit
;
236 /* fill the rest and attach interface */
240 if_initname(ifp
, name
, unit
);
241 if (unit
> taplastunit
)
244 ifp
->if_init
= tapifinit
;
245 ifp
->if_start
= tapifstart
;
246 ifp
->if_ioctl
= tapifioctl
;
247 ifp
->if_mtu
= ETHERMTU
;
248 ifp
->if_flags
= TAP_IFFLAGS
;
249 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
250 ifq_set_ready(&ifp
->if_snd
);
252 ether_ifattach(ifp
, ether_addr
, NULL
);
254 tp
->tap_flags
|= TAP_INITED
;
255 tp
->tap_devq
.ifq_maxlen
= ifqmaxlen
;
257 SLIST_INSERT_HEAD(&tap_listhead
, tp
, tap_link
);
259 TAPDEBUG(ifp
, "created. minor = %#x\n", minor(tp
->tap_dev
));
265 * Create a new tap instance.
268 tap_clone_create(struct if_clone
*ifc __unused
, int unit
)
270 struct tap_softc
*tp
= NULL
;
273 dev
= get_dev(CDEV_MAJOR
, unit
);
277 tp
->tap_flags
|= TAP_CLONE
;
278 TAPDEBUG(&tp
->tap_if
, "clone created. minor = %#x tap_flags = 0x%x\n",
279 minor(tp
->tap_dev
), tp
->tap_flags
);
287 * to open tunnel. must be superuser
290 tapopen(struct dev_open_args
*ap
)
293 struct tap_softc
*tp
= NULL
;
294 struct ifnet
*ifp
= NULL
;
298 (error
= priv_check_cred(ap
->a_cred
, PRIV_ROOT
, 0)) != 0)
302 dev
= ap
->a_head
.a_dev
;
307 ifp
= &tp
->arpcom
.ac_if
;
309 if (tp
->tap_flags
& TAP_OPEN
) {
313 ifp
= &tp
->arpcom
.ac_if
;
315 if ((tp
->tap_flags
& TAP_CLONE
) == 0) {
316 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
318 /* Announce the return of the interface. */
319 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
323 bcopy(tp
->arpcom
.ac_enaddr
, tp
->ether_addr
, sizeof(tp
->ether_addr
));
325 if (curthread
->td_proc
)
326 fsetown(curthread
->td_proc
->p_pid
, &tp
->tap_sigtd
);
327 tp
->tap_flags
|= TAP_OPEN
;
330 if (tapuponopen
&& (ifp
->if_flags
& IFF_UP
) == 0) {
335 ifnet_serialize_all(ifp
);
337 ifnet_deserialize_all(ifp
);
339 tp
->tap_flags
|= TAP_CLOSEDOWN
;
342 TAPDEBUG(ifp
, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
343 minor(tp
->tap_dev
), taprefcnt
, taplastunit
);
353 * close the device - mark i/f down & delete routing info
356 tapclose(struct dev_close_args
*ap
)
358 cdev_t dev
= ap
->a_head
.a_dev
;
359 struct tap_softc
*tp
= dev
->si_drv1
;
360 struct ifnet
*ifp
= &tp
->tap_if
;
365 /* Junk all pending output */
366 ifq_purge(&ifp
->if_snd
);
369 * Do not bring the interface down, and do not anything with
370 * interface, if we are in VMnet mode. just close the device.
372 * If the interface is not cloned, we always bring it down.
374 * If the interface is cloned, then we bring it down during
375 * closing only if it was brought up during opening.
377 if ((tp
->tap_flags
& TAP_VMNET
) == 0 &&
378 ((tp
->tap_flags
& TAP_CLONE
) == 0 ||
379 (tp
->tap_flags
& TAP_CLOSEDOWN
))) {
380 if (ifp
->if_flags
& IFF_UP
)
384 ifnet_serialize_all(ifp
);
385 tapifstop(tp
, clear_flags
);
386 ifnet_deserialize_all(ifp
);
388 if ((tp
->tap_flags
& TAP_CLONE
) == 0) {
389 if_purgeaddrs_nolink(ifp
);
391 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
393 /* Announce the departure of the interface. */
394 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
397 funsetown(tp
->tap_sigio
);
398 tp
->tap_sigio
= NULL
;
399 selwakeup(&tp
->tap_rsel
);
401 tp
->tap_flags
&= ~TAP_OPEN
;
402 funsetown(tp
->tap_sigtd
);
403 tp
->tap_sigtd
= NULL
;
408 if_printf(ifp
, "minor = %#x, refcnt = %d is out of sync. "
409 "set refcnt to 0\n", minor(tp
->tap_dev
), taprefcnt
);
412 TAPDEBUG(ifp
, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
413 minor(tp
->tap_dev
), taprefcnt
, taplastunit
);
416 if ((tp
->tap_flags
& TAP_CLONE
) == 0)
427 * Destroy a tap instance.
430 tapdestroy(struct tap_softc
*tp
)
432 struct ifnet
*ifp
= &tp
->arpcom
.ac_if
;
434 TAPDEBUG(ifp
, "destroyed. minor = %#x, refcnt = %d, taplastunit = %d\n",
435 minor(tp
->tap_dev
), taprefcnt
, taplastunit
);
437 ifnet_serialize_all(ifp
);
439 ifnet_deserialize_all(ifp
);
442 SLIST_REMOVE(&tap_listhead
, tp
, tap_softc
, tap_link
);
444 destroy_dev(tp
->tap_dev
);
453 * Destroy a tap instance.
456 tap_clone_destroy(struct ifnet
*ifp
)
458 struct tap_softc
*tp
= ifp
->if_softc
;
460 TAPDEBUG(&tp
->tap_if
, "clone destroyed. minor = %#x tap_flags = 0x%x\n",
461 minor(tp
->tap_dev
), tp
->tap_flags
);
462 if (tp
->tap_flags
& TAP_CLONE
)
469 * Network interface initialization function (called with if serializer held)
476 struct tap_softc
*tp
= xtp
;
477 struct ifnet
*ifp
= &tp
->tap_if
;
479 TAPDEBUG(ifp
, "initializing, minor = %#x tap_flags = 0x%x\n",
480 minor(tp
->tap_dev
), tp
->tap_flags
);
482 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
486 ifp
->if_flags
|= IFF_RUNNING
;
487 ifp
->if_flags
&= ~IFF_OACTIVE
;
489 /* attempt to start output */
497 * Process an ioctl request on network interface (called with if serializer
503 tapifioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
505 struct tap_softc
*tp
= (struct tap_softc
*)(ifp
->if_softc
);
506 struct ifstat
*ifs
= NULL
;
513 dummy
= ether_ioctl(ifp
, cmd
, data
);
520 case SIOCADDMULTI
: /* XXX -- just like vmnet does */
525 ifs
= (struct ifstat
*)data
;
526 dummy
= strlen(ifs
->ascii
);
527 if ((tp
->tap_flags
& TAP_OPEN
) &&
528 dummy
< sizeof(ifs
->ascii
)) {
529 if (tp
->tap_sigtd
&& tp
->tap_sigtd
->sio_proc
) {
530 ksnprintf(ifs
->ascii
+ dummy
,
531 sizeof(ifs
->ascii
) - dummy
,
532 "\tOpened by pid %d\n",
533 (int)tp
->tap_sigtd
->sio_proc
->p_pid
);
535 ksnprintf(ifs
->ascii
+ dummy
,
536 sizeof(ifs
->ascii
) - dummy
,
537 "\tOpened by <unknown>\n");
553 * Queue packets from higher level ready to put out (called with if serializer
559 tapifstart(struct ifnet
*ifp
)
561 struct tap_softc
*tp
= ifp
->if_softc
;
566 TAPDEBUG(ifp
, "starting, minor = %#x\n", minor(tp
->tap_dev
));
569 * do not junk pending output if we are in VMnet mode.
570 * XXX: can this do any harm because of queue overflow?
573 if (((tp
->tap_flags
& TAP_VMNET
) == 0) &&
574 ((tp
->tap_flags
& TAP_READY
) != TAP_READY
)) {
575 TAPDEBUG(ifp
, "not ready. minor = %#x, tap_flags = 0x%x\n",
576 minor(tp
->tap_dev
), tp
->tap_flags
);
577 ifq_purge(&ifp
->if_snd
);
581 ifp
->if_flags
|= IFF_OACTIVE
;
584 while ((m
= ifq_dequeue(&ifp
->if_snd
, NULL
)) != NULL
) {
597 if (tp
->tap_flags
& TAP_RWAIT
) {
598 tp
->tap_flags
&= ~TAP_RWAIT
;
603 KNOTE(&tp
->tap_rsel
.si_note
, 0);
606 if ((tp
->tap_flags
& TAP_ASYNC
) && (tp
->tap_sigio
!= NULL
)) {
608 pgsigio(tp
->tap_sigio
, SIGIO
, 0);
613 * selwakeup is not MPSAFE. tapifstart is.
616 selwakeup(&tp
->tap_rsel
);
620 ifp
->if_flags
&= ~IFF_OACTIVE
;
627 * The ops interface is now pretty minimal. Called via fileops with nothing
633 tapioctl(struct dev_ioctl_args
*ap
)
635 cdev_t dev
= ap
->a_head
.a_dev
;
636 caddr_t data
= ap
->a_data
;
637 struct tap_softc
*tp
= dev
->si_drv1
;
638 struct ifnet
*ifp
= &tp
->tap_if
;
639 struct tapinfo
*tapp
= NULL
;
644 ifnet_serialize_all(ifp
);
649 tapp
= (struct tapinfo
*)data
;
650 ifp
->if_mtu
= tapp
->mtu
;
651 ifp
->if_type
= tapp
->type
;
652 ifp
->if_baudrate
= tapp
->baudrate
;
656 tapp
= (struct tapinfo
*)data
;
657 tapp
->mtu
= ifp
->if_mtu
;
658 tapp
->type
= ifp
->if_type
;
659 tapp
->baudrate
= ifp
->if_baudrate
;
663 tapdebug
= *(int *)data
;
667 *(int *)data
= tapdebug
;
672 tp
->tap_flags
|= TAP_ASYNC
;
674 tp
->tap_flags
&= ~TAP_ASYNC
;
680 /* Take a look at devq first */
681 IF_POLL(&tp
->tap_devq
, mb
);
683 mb
= ifq_poll(&ifp
->if_snd
);
686 for(; mb
!= NULL
; mb
= mb
->m_next
)
687 *(int *)data
+= mb
->m_len
;
692 error
= fsetown(*(int *)data
, &tp
->tap_sigio
);
696 *(int *)data
= fgetown(tp
->tap_sigio
);
699 /* this is deprecated, FIOSETOWN should be used instead */
701 error
= fsetown(-(*(int *)data
), &tp
->tap_sigio
);
704 /* this is deprecated, FIOGETOWN should be used instead */
706 *(int *)data
= -fgetown(tp
->tap_sigio
);
709 /* VMware/VMnet port ioctl's */
711 case SIOCGIFFLAGS
: /* get ifnet flags */
712 bcopy(&ifp
->if_flags
, data
, sizeof(ifp
->if_flags
));
715 case VMIO_SIOCSIFFLAGS
: /* VMware/VMnet SIOCSIFFLAGS */
718 f
&= ~IFF_CANTCHANGE
;
720 ifp
->if_flags
= f
| (ifp
->if_flags
& IFF_CANTCHANGE
);
723 case OSIOCGIFADDR
: /* get MAC address of the remote side */
725 bcopy(tp
->ether_addr
, data
, sizeof(tp
->ether_addr
));
728 case SIOCSIFADDR
: /* set MAC address of the remote side */
729 bcopy(data
, tp
->ether_addr
, sizeof(tp
->ether_addr
));
736 ifnet_deserialize_all(ifp
);
744 * The ops read interface - reads a packet at a time, or at
745 * least as much of a packet as can be read.
747 * Called from the fileops interface with nothing held.
752 tapread(struct dev_read_args
*ap
)
754 cdev_t dev
= ap
->a_head
.a_dev
;
755 struct uio
*uio
= ap
->a_uio
;
756 struct tap_softc
*tp
= dev
->si_drv1
;
757 struct ifnet
*ifp
= &tp
->tap_if
;
758 struct mbuf
*m0
= NULL
;
761 TAPDEBUG(ifp
, "reading, minor = %#x\n", minor(tp
->tap_dev
));
763 if ((tp
->tap_flags
& TAP_READY
) != TAP_READY
) {
764 TAPDEBUG(ifp
, "not ready. minor = %#x, tap_flags = 0x%x\n",
765 minor(tp
->tap_dev
), tp
->tap_flags
);
770 tp
->tap_flags
&= ~TAP_RWAIT
;
772 /* sleep until we get a packet */
774 ifnet_serialize_all(ifp
);
775 IF_DEQUEUE(&tp
->tap_devq
, m0
);
777 if (ap
->a_ioflag
& IO_NDELAY
) {
778 ifnet_deserialize_all(ifp
);
779 return (EWOULDBLOCK
);
781 tp
->tap_flags
|= TAP_RWAIT
;
783 tsleep_interlock(tp
);
784 ifnet_deserialize_all(ifp
);
785 error
= tsleep(tp
, PCATCH
, "taprd", 0);
790 ifnet_deserialize_all(ifp
);
792 } while (m0
== NULL
);
796 /* xfer packet to user space */
797 while ((m0
!= NULL
) && (uio
->uio_resid
> 0) && (error
== 0)) {
798 len
= min(uio
->uio_resid
, m0
->m_len
);
802 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
807 TAPDEBUG(ifp
, "dropping mbuf, minor = %#x\n",
818 * The ops write interface - an atomic write is a packet - or else!
820 * Called from the fileops interface with nothing held.
825 tapwrite(struct dev_write_args
*ap
)
827 cdev_t dev
= ap
->a_head
.a_dev
;
828 struct uio
*uio
= ap
->a_uio
;
829 struct tap_softc
*tp
= dev
->si_drv1
;
830 struct ifnet
*ifp
= &tp
->tap_if
;
831 struct mbuf
*top
= NULL
, **mp
= NULL
, *m
= NULL
;
832 int error
= 0, tlen
, mlen
;
834 TAPDEBUG(ifp
, "writing, minor = %#x\n", minor(tp
->tap_dev
));
836 if ((tp
->tap_flags
& TAP_READY
) != TAP_READY
) {
837 TAPDEBUG(ifp
, "not ready. minor = %#x, tap_flags = 0x%x\n",
838 minor(tp
->tap_dev
), tp
->tap_flags
);
842 if (uio
->uio_resid
== 0)
845 if ((uio
->uio_resid
< 0) || (uio
->uio_resid
> TAPMRU
)) {
846 TAPDEBUG(ifp
, "invalid packet len = %d, minor = %#x\n",
847 uio
->uio_resid
, minor(tp
->tap_dev
));
851 tlen
= uio
->uio_resid
;
853 /* get a header mbuf */
854 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
861 while ((error
== 0) && (uio
->uio_resid
> 0)) {
862 m
->m_len
= min(mlen
, uio
->uio_resid
);
863 error
= uiomove(mtod(m
, caddr_t
), m
->m_len
, uio
);
866 if (uio
->uio_resid
> 0) {
867 MGET(m
, MB_DONTWAIT
, MT_DATA
);
882 top
->m_pkthdr
.len
= tlen
;
883 top
->m_pkthdr
.rcvif
= ifp
;
886 * Ethernet bridge and bpf are handled in ether_input
888 * adjust mbuf and give packet to the ether_input
890 ifnet_serialize_all(ifp
);
891 ifp
->if_input(ifp
, top
);
892 ifp
->if_ipackets
++; /* ibytes are counted in ether_input */
893 ifnet_deserialize_all(ifp
);
901 * The poll interface, this is only useful on reads really. The write
902 * detect always returns true, write never blocks anyway, it either
903 * accepts the packet or drops it
905 * Called from the fileops interface with nothing held.
910 tappoll(struct dev_poll_args
*ap
)
912 cdev_t dev
= ap
->a_head
.a_dev
;
913 struct tap_softc
*tp
= dev
->si_drv1
;
914 struct ifnet
*ifp
= &tp
->tap_if
;
917 TAPDEBUG(ifp
, "polling, minor = %#x\n", minor(tp
->tap_dev
));
919 if (ap
->a_events
& (POLLIN
| POLLRDNORM
)) {
920 if (!IF_QEMPTY(&tp
->tap_devq
)) {
922 "has data in queue. minor = %#x\n",
925 revents
|= (ap
->a_events
& (POLLIN
| POLLRDNORM
));
927 TAPDEBUG(ifp
, "waiting for data, minor = %#x\n",
931 selrecord(curthread
, &tp
->tap_rsel
);
936 if (ap
->a_events
& (POLLOUT
| POLLWRNORM
))
937 revents
|= (ap
->a_events
& (POLLOUT
| POLLWRNORM
));
938 ap
->a_events
= revents
;
943 * tapkqfilter - called from the fileops interface with nothing held
947 static int filt_tapread(struct knote
*kn
, long hint
);
948 static void filt_tapdetach(struct knote
*kn
);
949 static struct filterops tapread_filtops
=
950 { 1, NULL
, filt_tapdetach
, filt_tapread
};
953 tapkqfilter(struct dev_kqfilter_args
*ap
)
955 cdev_t dev
= ap
->a_head
.a_dev
;
956 struct knote
*kn
= ap
->a_kn
;
957 struct tap_softc
*tp
;
966 switch(kn
->kn_filter
) {
968 list
= &tp
->tap_rsel
.si_note
;
969 kn
->kn_fop
= &tapread_filtops
;
970 kn
->kn_hook
= (void *)tp
;
980 SLIST_INSERT_HEAD(list
, kn
, kn_selnext
);
987 filt_tapread(struct knote
*kn
, long hint
)
989 struct tap_softc
*tp
= (void *)kn
->kn_hook
;
991 if (IF_QEMPTY(&tp
->tap_devq
) == 0) /* XXX serializer */
998 filt_tapdetach(struct knote
*kn
)
1000 struct tap_softc
*tp
= (void *)kn
->kn_hook
;
1002 SLIST_REMOVE(&tp
->tap_rsel
.si_note
, kn
, knote
, kn_selnext
);
1006 tapifstop(struct tap_softc
*tp
, int clear_flags
)
1008 struct ifnet
*ifp
= &tp
->tap_if
;
1010 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
1011 IF_DRAIN(&tp
->tap_devq
);
1012 tp
->tap_flags
&= ~TAP_CLOSEDOWN
;
1014 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
1018 tapifflags(struct tap_softc
*tp
)
1020 struct ifnet
*ifp
= &tp
->arpcom
.ac_if
;
1022 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
1023 if ((tp
->tap_flags
& TAP_VMNET
) == 0) {
1025 * Only for non-vmnet tap(4)
1027 if (ifp
->if_flags
& IFF_UP
) {
1028 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)