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>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
53 #include <sys/selinfo.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59 #include <sys/ttycom.h>
61 #include <sys/queue.h>
64 #include <net/ethernet.h>
66 #include <net/if_clone.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/route.h>
72 #include <netinet/in.h>
74 #include <net/if_tapvar.h>
75 #include <net/if_tap.h>
78 #define CDEV_NAME "tap"
79 #define TAPDEBUG if (tapdebug) printf
83 #define TAPMAXUNIT 0x7fff
84 #define VMNET_DEV_MASK CLONE_FLAG0
87 static int tapmodevent(module_t, int, void *);
90 static void tapclone(void *, struct ucred *, char *, int,
92 static void tapcreate(struct cdev *);
94 /* network interface */
95 static void tapifstart(struct ifnet *);
96 static int tapifioctl(struct ifnet *, u_long, caddr_t);
97 static void tapifinit(void *);
99 static int tap_clone_create(struct if_clone *, int, caddr_t);
100 static void tap_clone_destroy(struct ifnet *);
101 static int vmnet_clone_create(struct if_clone *, int, caddr_t);
102 static void vmnet_clone_destroy(struct ifnet *);
104 IFC_SIMPLE_DECLARE(tap, 0);
105 IFC_SIMPLE_DECLARE(vmnet, 0);
107 /* character device */
108 static d_open_t tapopen;
109 static d_close_t tapclose;
110 static d_read_t tapread;
111 static d_write_t tapwrite;
112 static d_ioctl_t tapioctl;
113 static d_poll_t tappoll;
114 static d_kqfilter_t tapkqfilter;
117 static int tapkqread(struct knote *, long);
118 static int tapkqwrite(struct knote *, long);
119 static void tapkqdetach(struct knote *);
121 static struct filterops tap_read_filterops = {
124 .f_detach = tapkqdetach,
125 .f_event = tapkqread,
128 static struct filterops tap_write_filterops = {
131 .f_detach = tapkqdetach,
132 .f_event = tapkqwrite,
135 static struct cdevsw tap_cdevsw = {
136 .d_version = D_VERSION,
137 .d_flags = D_PSEUDO | D_NEEDMINOR,
145 .d_kqfilter = tapkqfilter,
149 * All global variables in if_tap.c are locked with tapmtx, with the
150 * exception of tapdebug, which is accessed unlocked; tapclones is
153 static struct mtx tapmtx;
154 static int tapdebug = 0; /* debug flag */
155 static int tapuopen = 0; /* allow user open() */
156 static int tapuponopen = 0; /* IFF_UP on open() */
157 static int tapdclone = 1; /* enable devfs cloning */
158 static SLIST_HEAD(, tap_softc) taphead; /* first device */
159 static struct clonedevs *tapclones;
161 MALLOC_DECLARE(M_TAP);
162 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
163 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
165 SYSCTL_DECL(_net_link);
166 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
167 "Ethernet tunnel software network interface");
168 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
169 "Allow user to open /dev/tap (based on node permissions)");
170 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
171 "Bring interface up when /dev/tap is opened");
172 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0,
173 "Enably legacy devfs interface creation");
174 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
176 TUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone);
178 DEV_MODULE(if_tap, tapmodevent, NULL);
181 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
187 if (strcmp(ifc->ifc_name, VMNET) == 0)
188 extra = VMNET_DEV_MASK;
192 /* find any existing device, or allocate new unit number */
193 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, extra);
195 dev = make_dev(&tap_cdevsw, unit | extra,
196 UID_ROOT, GID_WHEEL, 0600, "%s%d", ifc->ifc_name, unit);
203 /* vmnet devices are tap devices in disguise */
205 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
207 return tap_clone_create(ifc, unit, params);
211 tap_destroy(struct tap_softc *tp)
213 struct ifnet *ifp = tp->tap_ifp;
216 KASSERT(!(tp->tap_flags & TAP_OPEN),
217 ("%s flags is out of sync", ifp->if_xname));
219 CURVNET_SET(ifp->if_vnet);
220 seldrain(&tp->tap_rsel);
221 knlist_destroy(&tp->tap_rsel.si_note);
222 destroy_dev(tp->tap_dev);
224 if_free_type(ifp, IFT_ETHER);
226 mtx_destroy(&tp->tap_mtx);
232 tap_clone_destroy(struct ifnet *ifp)
234 struct tap_softc *tp = ifp->if_softc;
237 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
242 /* vmnet devices are tap devices in disguise */
244 vmnet_clone_destroy(struct ifnet *ifp)
246 tap_clone_destroy(ifp);
252 * module event handler
255 tapmodevent(module_t mod, int type, void *data)
257 static eventhandler_tag eh_tag = NULL;
258 struct tap_softc *tp = NULL;
259 struct ifnet *ifp = NULL;
264 /* intitialize device */
266 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
267 SLIST_INIT(&taphead);
269 clone_setup(&tapclones);
270 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
271 if (eh_tag == NULL) {
272 clone_cleanup(&tapclones);
273 mtx_destroy(&tapmtx);
276 if_clone_attach(&tap_cloner);
277 if_clone_attach(&vmnet_cloner);
282 * The EBUSY algorithm here can't quite atomically
283 * guarantee that this is race-free since we have to
284 * release the tap mtx to deregister the clone handler.
287 SLIST_FOREACH(tp, &taphead, tap_next) {
288 mtx_lock(&tp->tap_mtx);
289 if (tp->tap_flags & TAP_OPEN) {
290 mtx_unlock(&tp->tap_mtx);
294 mtx_unlock(&tp->tap_mtx);
298 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
299 if_clone_detach(&tap_cloner);
300 if_clone_detach(&vmnet_cloner);
301 drain_dev_clone_events();
304 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
305 SLIST_REMOVE_HEAD(&taphead, tap_next);
310 TAPDEBUG("detaching %s\n", ifp->if_xname);
316 clone_cleanup(&tapclones);
318 mtx_destroy(&tapmtx);
333 * We need to support two kind of devices - tap and vmnet
336 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
338 char devname[SPECNAMELEN + 1];
339 int i, unit, append_unit;
346 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
353 /* We're interested in only tap/vmnet devices. */
354 if (strcmp(name, TAP) == 0) {
356 } else if (strcmp(name, VMNET) == 0) {
358 extra = VMNET_DEV_MASK;
359 } else if (dev_stdclone(name, NULL, TAP, &unit) != 1) {
360 if (dev_stdclone(name, NULL, VMNET, &unit) != 1) {
363 extra = VMNET_DEV_MASK;
370 CURVNET_SET(CRED_TO_VNET(cred));
371 /* find any existing device, or allocate new unit number */
372 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
376 * We were passed 'tun' or 'tap', with no unit specified
377 * so we'll need to append it now.
379 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
384 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
385 cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
388 if_clone_create(name, namelen, NULL);
396 * to create interface
399 tapcreate(struct cdev *dev)
401 struct ifnet *ifp = NULL;
402 struct tap_softc *tp = NULL;
403 unsigned short macaddr_hi;
404 uint32_t macaddr_mid;
409 dev->si_flags &= ~SI_CHEAPCLONE;
411 /* allocate driver storage and create device */
412 tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
413 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
415 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
418 unit = dev2unit(dev);
420 /* select device: tap or vmnet */
421 if (unit & VMNET_DEV_MASK) {
423 tp->tap_flags |= TAP_VMNET;
429 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
431 /* generate fake MAC address: 00 bd xx xx xx unit_no */
432 macaddr_hi = htons(0x00bd);
433 macaddr_mid = (uint32_t) ticks;
434 bcopy(&macaddr_hi, eaddr, sizeof(short));
435 bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
436 eaddr[5] = (u_char)unit;
438 /* fill the rest and attach interface */
439 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
441 panic("%s%d: can not if_alloc()", name, unit);
443 if_initname(ifp, name, unit);
444 ifp->if_init = tapifinit;
445 ifp->if_start = tapifstart;
446 ifp->if_ioctl = tapifioctl;
447 ifp->if_mtu = ETHERMTU;
448 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
449 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
450 ifp->if_capabilities |= IFCAP_LINKSTATE;
451 ifp->if_capenable |= IFCAP_LINKSTATE;
456 ether_ifattach(ifp, eaddr);
458 mtx_lock(&tp->tap_mtx);
459 tp->tap_flags |= TAP_INITED;
460 mtx_unlock(&tp->tap_mtx);
462 knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
464 TAPDEBUG("interface %s is created. minor = %#x\n",
465 ifp->if_xname, dev2unit(dev));
472 * to open tunnel. must be superuser
475 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
477 struct tap_softc *tp = NULL;
478 struct ifnet *ifp = NULL;
482 error = priv_check(td, PRIV_NET_TAP);
487 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
492 mtx_lock(&tp->tap_mtx);
493 if (tp->tap_flags & TAP_OPEN) {
494 mtx_unlock(&tp->tap_mtx);
498 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
499 tp->tap_pid = td->td_proc->p_pid;
500 tp->tap_flags |= TAP_OPEN;
503 ifp->if_drv_flags |= IFF_DRV_RUNNING;
504 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
506 ifp->if_flags |= IFF_UP;
507 if_link_state_change(ifp, LINK_STATE_UP);
508 mtx_unlock(&tp->tap_mtx);
510 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
519 * close the device - mark i/f down & delete routing info
522 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
525 struct tap_softc *tp = dev->si_drv1;
526 struct ifnet *ifp = tp->tap_ifp;
528 /* junk all pending output */
529 mtx_lock(&tp->tap_mtx);
530 CURVNET_SET(ifp->if_vnet);
531 IF_DRAIN(&ifp->if_snd);
534 * do not bring the interface down, and do not anything with
535 * interface, if we are in VMnet mode. just close the device.
538 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
539 mtx_unlock(&tp->tap_mtx);
541 mtx_lock(&tp->tap_mtx);
542 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
543 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
544 mtx_unlock(&tp->tap_mtx);
545 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
546 rtinit(ifa, (int)RTM_DELETE, 0);
549 mtx_lock(&tp->tap_mtx);
553 if_link_state_change(ifp, LINK_STATE_DOWN);
556 funsetown(&tp->tap_sigio);
557 selwakeuppri(&tp->tap_rsel, PZERO+1);
558 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
560 tp->tap_flags &= ~TAP_OPEN;
562 mtx_unlock(&tp->tap_mtx);
564 TAPDEBUG("%s is closed. minor = %#x\n",
565 ifp->if_xname, dev2unit(dev));
574 * network interface initialization function
579 struct tap_softc *tp = (struct tap_softc *)xtp;
580 struct ifnet *ifp = tp->tap_ifp;
582 TAPDEBUG("initializing %s\n", ifp->if_xname);
584 mtx_lock(&tp->tap_mtx);
585 ifp->if_drv_flags |= IFF_DRV_RUNNING;
586 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
587 mtx_unlock(&tp->tap_mtx);
589 /* attempt to start output */
597 * Process an ioctl request on network interface
600 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
602 struct tap_softc *tp = ifp->if_softc;
603 struct ifreq *ifr = (struct ifreq *)data;
604 struct ifstat *ifs = NULL;
608 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
614 ifp->if_mtu = ifr->ifr_mtu;
618 ifs = (struct ifstat *)data;
619 dummy = strlen(ifs->ascii);
620 mtx_lock(&tp->tap_mtx);
621 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
622 snprintf(ifs->ascii + dummy,
623 sizeof(ifs->ascii) - dummy,
624 "\tOpened by PID %d\n", tp->tap_pid);
625 mtx_unlock(&tp->tap_mtx);
629 return (ether_ioctl(ifp, cmd, data));
640 * queue packets from higher level ready to put out
643 tapifstart(struct ifnet *ifp)
645 struct tap_softc *tp = ifp->if_softc;
647 TAPDEBUG("%s starting\n", ifp->if_xname);
650 * do not junk pending output if we are in VMnet mode.
651 * XXX: can this do any harm because of queue overflow?
654 mtx_lock(&tp->tap_mtx);
655 if (((tp->tap_flags & TAP_VMNET) == 0) &&
656 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
660 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
664 IF_DEQUEUE(&ifp->if_snd, m);
671 mtx_unlock(&tp->tap_mtx);
676 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
678 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
679 if (tp->tap_flags & TAP_RWAIT) {
680 tp->tap_flags &= ~TAP_RWAIT;
684 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
685 mtx_unlock(&tp->tap_mtx);
686 pgsigio(&tp->tap_sigio, SIGIO, 0);
687 mtx_lock(&tp->tap_mtx);
690 selwakeuppri(&tp->tap_rsel, PZERO+1);
691 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
692 ifp->if_opackets ++; /* obytes are counted in ether_output */
695 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
696 mtx_unlock(&tp->tap_mtx);
703 * the cdevsw interface is now pretty minimal
706 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
708 struct tap_softc *tp = dev->si_drv1;
709 struct ifnet *ifp = tp->tap_ifp;
710 struct tapinfo *tapp = NULL;
712 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
713 defined(COMPAT_FREEBSD4)
719 tapp = (struct tapinfo *)data;
720 if (ifp->if_type != tapp->type)
722 mtx_lock(&tp->tap_mtx);
723 ifp->if_mtu = tapp->mtu;
724 ifp->if_baudrate = tapp->baudrate;
725 mtx_unlock(&tp->tap_mtx);
729 tapp = (struct tapinfo *)data;
730 mtx_lock(&tp->tap_mtx);
731 tapp->mtu = ifp->if_mtu;
732 tapp->type = ifp->if_type;
733 tapp->baudrate = ifp->if_baudrate;
734 mtx_unlock(&tp->tap_mtx);
738 tapdebug = *(int *)data;
742 *(int *)data = tapdebug;
746 struct ifreq *ifr = (struct ifreq *) data;
748 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
755 mtx_lock(&tp->tap_mtx);
757 tp->tap_flags |= TAP_ASYNC;
759 tp->tap_flags &= ~TAP_ASYNC;
760 mtx_unlock(&tp->tap_mtx);
764 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
767 IFQ_LOCK(&ifp->if_snd);
768 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
769 for (*(int *)data = 0; mb != NULL;
771 *(int *)data += mb->m_len;
772 IFQ_UNLOCK(&ifp->if_snd);
778 return (fsetown(*(int *)data, &tp->tap_sigio));
781 *(int *)data = fgetown(&tp->tap_sigio);
784 /* this is deprecated, FIOSETOWN should be used instead */
786 return (fsetown(-(*(int *)data), &tp->tap_sigio));
788 /* this is deprecated, FIOGETOWN should be used instead */
790 *(int *)data = -fgetown(&tp->tap_sigio);
793 /* VMware/VMnet port ioctl's */
795 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
796 defined(COMPAT_FREEBSD4)
798 ival = IOCPARM_IVAL(data);
799 data = (caddr_t)&ival;
802 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
805 f &= ~IFF_CANTCHANGE;
808 mtx_lock(&tp->tap_mtx);
809 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
810 mtx_unlock(&tp->tap_mtx);
813 case OSIOCGIFADDR: /* get MAC address of the remote side */
815 mtx_lock(&tp->tap_mtx);
816 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
817 mtx_unlock(&tp->tap_mtx);
820 case SIOCSIFADDR: /* set MAC address of the remote side */
821 mtx_lock(&tp->tap_mtx);
822 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
823 mtx_unlock(&tp->tap_mtx);
836 * the cdevsw read interface - reads a packet at a time, or at
837 * least as much of a packet as can be read
840 tapread(struct cdev *dev, struct uio *uio, int flag)
842 struct tap_softc *tp = dev->si_drv1;
843 struct ifnet *ifp = tp->tap_ifp;
844 struct mbuf *m = NULL;
847 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
849 mtx_lock(&tp->tap_mtx);
850 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
851 mtx_unlock(&tp->tap_mtx);
854 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
855 ifp->if_xname, dev2unit(dev), tp->tap_flags);
860 tp->tap_flags &= ~TAP_RWAIT;
862 /* sleep until we get a packet */
864 IF_DEQUEUE(&ifp->if_snd, m);
867 if (flag & O_NONBLOCK) {
868 mtx_unlock(&tp->tap_mtx);
869 return (EWOULDBLOCK);
872 tp->tap_flags |= TAP_RWAIT;
873 error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
876 mtx_unlock(&tp->tap_mtx);
881 mtx_unlock(&tp->tap_mtx);
883 /* feed packet to bpf */
886 /* xfer packet to user space */
887 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
888 len = min(uio->uio_resid, m->m_len);
892 error = uiomove(mtod(m, void *), len, uio);
897 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
909 * the cdevsw write interface - an atomic write is a packet - or else!
912 tapwrite(struct cdev *dev, struct uio *uio, int flag)
914 struct ether_header *eh;
915 struct tap_softc *tp = dev->si_drv1;
916 struct ifnet *ifp = tp->tap_ifp;
919 TAPDEBUG("%s writting, minor = %#x\n",
920 ifp->if_xname, dev2unit(dev));
922 if (uio->uio_resid == 0)
925 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
926 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
927 ifp->if_xname, uio->uio_resid, dev2unit(dev));
932 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN,
933 M_PKTHDR)) == NULL) {
938 m->m_pkthdr.rcvif = ifp;
941 * Only pass a unicast frame to ether_input(), if it would actually
942 * have been received by non-virtual hardware.
944 if (m->m_len < sizeof(struct ether_header)) {
948 eh = mtod(m, struct ether_header *);
950 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
951 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
952 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
957 /* Pass packet up to parent. */
958 CURVNET_SET(ifp->if_vnet);
959 (*ifp->if_input)(ifp, m);
961 ifp->if_ipackets ++; /* ibytes are counted in parent */
970 * the poll interface, this is only useful on reads
971 * really. the write detect always returns true, write never blocks
972 * anyway, it either accepts the packet or drops it
975 tappoll(struct cdev *dev, int events, struct thread *td)
977 struct tap_softc *tp = dev->si_drv1;
978 struct ifnet *ifp = tp->tap_ifp;
981 TAPDEBUG("%s polling, minor = %#x\n",
982 ifp->if_xname, dev2unit(dev));
984 if (events & (POLLIN | POLLRDNORM)) {
985 IFQ_LOCK(&ifp->if_snd);
986 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
987 TAPDEBUG("%s have data in queue. len = %d, " \
988 "minor = %#x\n", ifp->if_xname,
989 ifp->if_snd.ifq_len, dev2unit(dev));
991 revents |= (events & (POLLIN | POLLRDNORM));
993 TAPDEBUG("%s waiting for data, minor = %#x\n",
994 ifp->if_xname, dev2unit(dev));
996 selrecord(td, &tp->tap_rsel);
998 IFQ_UNLOCK(&ifp->if_snd);
1001 if (events & (POLLOUT | POLLWRNORM))
1002 revents |= (events & (POLLOUT | POLLWRNORM));
1011 * support for kevent() system call
1014 tapkqfilter(struct cdev *dev, struct knote *kn)
1016 struct tap_softc *tp = dev->si_drv1;
1017 struct ifnet *ifp = tp->tap_ifp;
1019 switch (kn->kn_filter) {
1021 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1022 ifp->if_xname, dev2unit(dev));
1023 kn->kn_fop = &tap_read_filterops;
1027 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1028 ifp->if_xname, dev2unit(dev));
1029 kn->kn_fop = &tap_write_filterops;
1033 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1034 ifp->if_xname, dev2unit(dev));
1040 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1049 * Return true if there is data in the interface queue
1052 tapkqread(struct knote *kn, long hint)
1055 struct tap_softc *tp = kn->kn_hook;
1056 struct cdev *dev = tp->tap_dev;
1057 struct ifnet *ifp = tp->tap_ifp;
1059 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1060 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1061 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1064 TAPDEBUG("%s waiting for data, minor = %#x\n",
1065 ifp->if_xname, dev2unit(dev));
1076 * Always can write. Return the MTU in kn->data
1079 tapkqwrite(struct knote *kn, long hint)
1081 struct tap_softc *tp = kn->kn_hook;
1082 struct ifnet *ifp = tp->tap_ifp;
1084 kn->kn_data = ifp->if_mtu;
1091 tapkqdetach(struct knote *kn)
1093 struct tap_softc *tp = kn->kn_hook;
1095 knlist_remove(&tp->tap_rsel.si_note, kn, 0);