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_NEEDMINOR,
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 tapuponopen = 0; /* IFF_UP on open() */
155 static int tapdclone = 1; /* enable devfs cloning */
156 static SLIST_HEAD(, tap_softc) taphead; /* first device */
157 static struct clonedevs *tapclones;
159 MALLOC_DECLARE(M_TAP);
160 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
161 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
163 SYSCTL_DECL(_net_link);
164 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
165 "Ethernet tunnel software network interface");
166 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
167 "Allow user to open /dev/tap (based on node permissions)");
168 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
169 "Bring interface up when /dev/tap is opened");
170 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0,
171 "Enably legacy devfs interface creation");
172 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
174 TUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone);
176 DEV_MODULE(if_tap, tapmodevent, NULL);
179 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
185 if (strcmp(ifc->ifc_name, VMNET) == 0)
186 extra = VMNET_DEV_MASK;
190 /* find any existing device, or allocate new unit number */
191 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, extra);
193 dev = make_dev(&tap_cdevsw, unit | extra,
194 UID_ROOT, GID_WHEEL, 0600, "%s%d", ifc->ifc_name, unit);
201 /* vmnet devices are tap devices in disguise */
203 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
205 return tap_clone_create(ifc, unit, params);
209 tap_destroy(struct tap_softc *tp)
211 struct ifnet *ifp = tp->tap_ifp;
214 KASSERT(!(tp->tap_flags & TAP_OPEN),
215 ("%s flags is out of sync", ifp->if_xname));
217 seldrain(&tp->tap_rsel);
218 knlist_destroy(&tp->tap_rsel.si_note);
219 destroy_dev(tp->tap_dev);
221 if_free_type(ifp, IFT_ETHER);
223 mtx_destroy(&tp->tap_mtx);
228 tap_clone_destroy(struct ifnet *ifp)
230 struct tap_softc *tp = ifp->if_softc;
233 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
238 /* vmnet devices are tap devices in disguise */
240 vmnet_clone_destroy(struct ifnet *ifp)
242 tap_clone_destroy(ifp);
248 * module event handler
251 tapmodevent(module_t mod, int type, void *data)
253 static eventhandler_tag eh_tag = NULL;
254 struct tap_softc *tp = NULL;
255 struct ifnet *ifp = NULL;
260 /* intitialize device */
262 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
263 SLIST_INIT(&taphead);
265 clone_setup(&tapclones);
266 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
267 if (eh_tag == NULL) {
268 clone_cleanup(&tapclones);
269 mtx_destroy(&tapmtx);
272 if_clone_attach(&tap_cloner);
273 if_clone_attach(&vmnet_cloner);
278 * The EBUSY algorithm here can't quite atomically
279 * guarantee that this is race-free since we have to
280 * release the tap mtx to deregister the clone handler.
283 SLIST_FOREACH(tp, &taphead, tap_next) {
284 mtx_lock(&tp->tap_mtx);
285 if (tp->tap_flags & TAP_OPEN) {
286 mtx_unlock(&tp->tap_mtx);
290 mtx_unlock(&tp->tap_mtx);
294 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
295 if_clone_detach(&tap_cloner);
296 if_clone_detach(&vmnet_cloner);
297 drain_dev_clone_events();
300 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
301 SLIST_REMOVE_HEAD(&taphead, tap_next);
306 TAPDEBUG("detaching %s\n", ifp->if_xname);
312 clone_cleanup(&tapclones);
314 mtx_destroy(&tapmtx);
329 * We need to support two kind of devices - tap and vmnet
332 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
334 char devname[SPECNAMELEN + 1];
335 int i, unit, append_unit;
342 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
349 /* We're interested in only tap/vmnet devices. */
350 if (strcmp(name, TAP) == 0) {
352 } else if (strcmp(name, VMNET) == 0) {
354 extra = VMNET_DEV_MASK;
355 } else if (dev_stdclone(name, NULL, TAP, &unit) != 1) {
356 if (dev_stdclone(name, NULL, VMNET, &unit) != 1) {
359 extra = VMNET_DEV_MASK;
366 /* find any existing device, or allocate new unit number */
367 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
371 * We were passed 'tun' or 'tap', with no unit specified
372 * so we'll need to append it now.
374 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
379 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
380 cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
383 if_clone_create(name, namelen, NULL);
390 * to create interface
393 tapcreate(struct cdev *dev)
395 struct ifnet *ifp = NULL;
396 struct tap_softc *tp = NULL;
397 unsigned short macaddr_hi;
398 uint32_t macaddr_mid;
403 dev->si_flags &= ~SI_CHEAPCLONE;
405 /* allocate driver storage and create device */
406 tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
407 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
409 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
412 unit = dev2unit(dev);
414 /* select device: tap or vmnet */
415 if (unit & VMNET_DEV_MASK) {
417 tp->tap_flags |= TAP_VMNET;
423 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
425 /* generate fake MAC address: 00 bd xx xx xx unit_no */
426 macaddr_hi = htons(0x00bd);
427 macaddr_mid = (uint32_t) ticks;
428 bcopy(&macaddr_hi, eaddr, sizeof(short));
429 bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
430 eaddr[5] = (u_char)unit;
432 /* fill the rest and attach interface */
433 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
435 panic("%s%d: can not if_alloc()", name, unit);
437 if_initname(ifp, name, unit);
438 ifp->if_init = tapifinit;
439 ifp->if_start = tapifstart;
440 ifp->if_ioctl = tapifioctl;
441 ifp->if_mtu = ETHERMTU;
442 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
443 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
444 ifp->if_capabilities |= IFCAP_LINKSTATE;
445 ifp->if_capenable |= IFCAP_LINKSTATE;
450 ether_ifattach(ifp, eaddr);
452 mtx_lock(&tp->tap_mtx);
453 tp->tap_flags |= TAP_INITED;
454 mtx_unlock(&tp->tap_mtx);
456 knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
458 TAPDEBUG("interface %s is created. minor = %#x\n",
459 ifp->if_xname, dev2unit(dev));
466 * to open tunnel. must be superuser
469 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
471 struct tap_softc *tp = NULL;
472 struct ifnet *ifp = NULL;
476 error = priv_check(td, PRIV_NET_TAP);
481 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
486 mtx_lock(&tp->tap_mtx);
487 if (tp->tap_flags & TAP_OPEN) {
488 mtx_unlock(&tp->tap_mtx);
492 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
493 tp->tap_pid = td->td_proc->p_pid;
494 tp->tap_flags |= TAP_OPEN;
497 ifp->if_drv_flags |= IFF_DRV_RUNNING;
498 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
500 ifp->if_flags |= IFF_UP;
501 if_link_state_change(ifp, LINK_STATE_UP);
502 mtx_unlock(&tp->tap_mtx);
504 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
513 * close the device - mark i/f down & delete routing info
516 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
519 struct tap_softc *tp = dev->si_drv1;
520 struct ifnet *ifp = tp->tap_ifp;
522 /* junk all pending output */
523 mtx_lock(&tp->tap_mtx);
524 IF_DRAIN(&ifp->if_snd);
527 * do not bring the interface down, and do not anything with
528 * interface, if we are in VMnet mode. just close the device.
531 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
532 mtx_unlock(&tp->tap_mtx);
534 mtx_lock(&tp->tap_mtx);
535 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
536 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
537 mtx_unlock(&tp->tap_mtx);
538 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
539 rtinit(ifa, (int)RTM_DELETE, 0);
542 mtx_lock(&tp->tap_mtx);
546 if_link_state_change(ifp, LINK_STATE_DOWN);
547 funsetown(&tp->tap_sigio);
548 selwakeuppri(&tp->tap_rsel, PZERO+1);
549 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
551 tp->tap_flags &= ~TAP_OPEN;
553 mtx_unlock(&tp->tap_mtx);
555 TAPDEBUG("%s is closed. minor = %#x\n",
556 ifp->if_xname, dev2unit(dev));
565 * network interface initialization function
570 struct tap_softc *tp = (struct tap_softc *)xtp;
571 struct ifnet *ifp = tp->tap_ifp;
573 TAPDEBUG("initializing %s\n", ifp->if_xname);
575 mtx_lock(&tp->tap_mtx);
576 ifp->if_drv_flags |= IFF_DRV_RUNNING;
577 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
578 mtx_unlock(&tp->tap_mtx);
580 /* attempt to start output */
588 * Process an ioctl request on network interface
591 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
593 struct tap_softc *tp = ifp->if_softc;
594 struct ifreq *ifr = (struct ifreq *)data;
595 struct ifstat *ifs = NULL;
599 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
605 ifp->if_mtu = ifr->ifr_mtu;
609 ifs = (struct ifstat *)data;
610 dummy = strlen(ifs->ascii);
611 mtx_lock(&tp->tap_mtx);
612 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
613 snprintf(ifs->ascii + dummy,
614 sizeof(ifs->ascii) - dummy,
615 "\tOpened by PID %d\n", tp->tap_pid);
616 mtx_unlock(&tp->tap_mtx);
620 return (ether_ioctl(ifp, cmd, data));
631 * queue packets from higher level ready to put out
634 tapifstart(struct ifnet *ifp)
636 struct tap_softc *tp = ifp->if_softc;
638 TAPDEBUG("%s starting\n", ifp->if_xname);
641 * do not junk pending output if we are in VMnet mode.
642 * XXX: can this do any harm because of queue overflow?
645 mtx_lock(&tp->tap_mtx);
646 if (((tp->tap_flags & TAP_VMNET) == 0) &&
647 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
651 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
655 IF_DEQUEUE(&ifp->if_snd, m);
662 mtx_unlock(&tp->tap_mtx);
667 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
669 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
670 if (tp->tap_flags & TAP_RWAIT) {
671 tp->tap_flags &= ~TAP_RWAIT;
675 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
676 mtx_unlock(&tp->tap_mtx);
677 pgsigio(&tp->tap_sigio, SIGIO, 0);
678 mtx_lock(&tp->tap_mtx);
681 selwakeuppri(&tp->tap_rsel, PZERO+1);
682 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
683 ifp->if_opackets ++; /* obytes are counted in ether_output */
686 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
687 mtx_unlock(&tp->tap_mtx);
694 * the cdevsw interface is now pretty minimal
697 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
699 struct tap_softc *tp = dev->si_drv1;
700 struct ifnet *ifp = tp->tap_ifp;
701 struct tapinfo *tapp = NULL;
703 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
704 defined(COMPAT_FREEBSD4)
710 tapp = (struct tapinfo *)data;
711 mtx_lock(&tp->tap_mtx);
712 ifp->if_mtu = tapp->mtu;
713 ifp->if_type = tapp->type;
714 ifp->if_baudrate = tapp->baudrate;
715 mtx_unlock(&tp->tap_mtx);
719 tapp = (struct tapinfo *)data;
720 mtx_lock(&tp->tap_mtx);
721 tapp->mtu = ifp->if_mtu;
722 tapp->type = ifp->if_type;
723 tapp->baudrate = ifp->if_baudrate;
724 mtx_unlock(&tp->tap_mtx);
728 tapdebug = *(int *)data;
732 *(int *)data = tapdebug;
736 struct ifreq *ifr = (struct ifreq *) data;
738 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
745 mtx_lock(&tp->tap_mtx);
747 tp->tap_flags |= TAP_ASYNC;
749 tp->tap_flags &= ~TAP_ASYNC;
750 mtx_unlock(&tp->tap_mtx);
754 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
757 IFQ_LOCK(&ifp->if_snd);
758 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
759 for (*(int *)data = 0; mb != NULL;
761 *(int *)data += mb->m_len;
762 IFQ_UNLOCK(&ifp->if_snd);
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 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
786 defined(COMPAT_FREEBSD4)
788 ival = IOCPARM_IVAL(data);
789 data = (caddr_t)&ival;
792 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
795 f &= ~IFF_CANTCHANGE;
798 mtx_lock(&tp->tap_mtx);
799 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
800 mtx_unlock(&tp->tap_mtx);
803 case OSIOCGIFADDR: /* get MAC address of the remote side */
805 mtx_lock(&tp->tap_mtx);
806 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
807 mtx_unlock(&tp->tap_mtx);
810 case SIOCSIFADDR: /* set MAC address of the remote side */
811 mtx_lock(&tp->tap_mtx);
812 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
813 mtx_unlock(&tp->tap_mtx);
826 * the cdevsw read interface - reads a packet at a time, or at
827 * least as much of a packet as can be read
830 tapread(struct cdev *dev, struct uio *uio, int flag)
832 struct tap_softc *tp = dev->si_drv1;
833 struct ifnet *ifp = tp->tap_ifp;
834 struct mbuf *m = NULL;
837 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
839 mtx_lock(&tp->tap_mtx);
840 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
841 mtx_unlock(&tp->tap_mtx);
844 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
845 ifp->if_xname, dev2unit(dev), tp->tap_flags);
850 tp->tap_flags &= ~TAP_RWAIT;
852 /* sleep until we get a packet */
854 IF_DEQUEUE(&ifp->if_snd, m);
857 if (flag & O_NONBLOCK) {
858 mtx_unlock(&tp->tap_mtx);
859 return (EWOULDBLOCK);
862 tp->tap_flags |= TAP_RWAIT;
863 error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
866 mtx_unlock(&tp->tap_mtx);
871 mtx_unlock(&tp->tap_mtx);
873 /* feed packet to bpf */
876 /* xfer packet to user space */
877 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
878 len = min(uio->uio_resid, m->m_len);
882 error = uiomove(mtod(m, void *), len, uio);
887 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
899 * the cdevsw write interface - an atomic write is a packet - or else!
902 tapwrite(struct cdev *dev, struct uio *uio, int flag)
904 struct ether_header *eh;
905 struct tap_softc *tp = dev->si_drv1;
906 struct ifnet *ifp = tp->tap_ifp;
909 TAPDEBUG("%s writting, minor = %#x\n",
910 ifp->if_xname, dev2unit(dev));
912 if (uio->uio_resid == 0)
915 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
916 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
917 ifp->if_xname, uio->uio_resid, dev2unit(dev));
922 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN,
923 M_PKTHDR)) == NULL) {
928 m->m_pkthdr.rcvif = ifp;
931 * Only pass a unicast frame to ether_input(), if it would actually
932 * have been received by non-virtual hardware.
934 if (m->m_len < sizeof(struct ether_header)) {
938 eh = mtod(m, struct ether_header *);
940 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
941 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
942 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
947 /* Pass packet up to parent. */
948 (*ifp->if_input)(ifp, m);
949 ifp->if_ipackets ++; /* ibytes are counted in parent */
958 * the poll interface, this is only useful on reads
959 * really. the write detect always returns true, write never blocks
960 * anyway, it either accepts the packet or drops it
963 tappoll(struct cdev *dev, int events, struct thread *td)
965 struct tap_softc *tp = dev->si_drv1;
966 struct ifnet *ifp = tp->tap_ifp;
969 TAPDEBUG("%s polling, minor = %#x\n",
970 ifp->if_xname, dev2unit(dev));
972 if (events & (POLLIN | POLLRDNORM)) {
973 IFQ_LOCK(&ifp->if_snd);
974 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
975 TAPDEBUG("%s have data in queue. len = %d, " \
976 "minor = %#x\n", ifp->if_xname,
977 ifp->if_snd.ifq_len, dev2unit(dev));
979 revents |= (events & (POLLIN | POLLRDNORM));
981 TAPDEBUG("%s waiting for data, minor = %#x\n",
982 ifp->if_xname, dev2unit(dev));
984 selrecord(td, &tp->tap_rsel);
986 IFQ_UNLOCK(&ifp->if_snd);
989 if (events & (POLLOUT | POLLWRNORM))
990 revents |= (events & (POLLOUT | POLLWRNORM));
999 * support for kevent() system call
1002 tapkqfilter(struct cdev *dev, struct knote *kn)
1004 struct tap_softc *tp = dev->si_drv1;
1005 struct ifnet *ifp = tp->tap_ifp;
1007 switch (kn->kn_filter) {
1009 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1010 ifp->if_xname, dev2unit(dev));
1011 kn->kn_fop = &tap_read_filterops;
1015 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1016 ifp->if_xname, dev2unit(dev));
1017 kn->kn_fop = &tap_write_filterops;
1021 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1022 ifp->if_xname, dev2unit(dev));
1028 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1037 * Return true if there is data in the interface queue
1040 tapkqread(struct knote *kn, long hint)
1043 struct tap_softc *tp = kn->kn_hook;
1044 struct cdev *dev = tp->tap_dev;
1045 struct ifnet *ifp = tp->tap_ifp;
1047 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1048 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1049 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1052 TAPDEBUG("%s waiting for data, minor = %#x\n",
1053 ifp->if_xname, dev2unit(dev));
1064 * Always can write. Return the MTU in kn->data
1067 tapkqwrite(struct knote *kn, long hint)
1069 struct tap_softc *tp = kn->kn_hook;
1070 struct ifnet *ifp = tp->tap_ifp;
1072 kn->kn_data = ifp->if_mtu;
1079 tapkqdetach(struct knote *kn)
1081 struct tap_softc *tp = kn->kn_hook;
1083 knlist_remove(&tp->tap_rsel.si_note, kn, 0);