2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * -------------------------------------------------------------------------
31 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
32 * Nottingham University 1987.
37 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
40 #include "opt_compat.h"
43 #include <sys/param.h>
45 #include <sys/fcntl.h>
46 #include <sys/filio.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
55 #include <sys/selinfo.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/ttycom.h>
63 #include <sys/queue.h>
66 #include <net/ethernet.h>
68 #include <net/if_var.h>
69 #include <net/if_clone.h>
70 #include <net/if_dl.h>
71 #include <net/if_media.h>
72 #include <net/if_types.h>
73 #include <net/route.h>
76 #include <netinet/in.h>
78 #include <net/if_tapvar.h>
79 #include <net/if_tap.h>
82 #define CDEV_NAME "tap"
83 #define TAPDEBUG if (tapdebug) printf
85 static const char tapname[] = "tap";
86 static const char vmnetname[] = "vmnet";
87 #define TAPMAXUNIT 0x7fff
88 #define VMNET_DEV_MASK CLONE_FLAG0
91 static int tapmodevent(module_t, int, void *);
94 static void tapclone(void *, struct ucred *, char *, int,
96 static void tapcreate(struct cdev *);
98 /* network interface */
99 static void tapifstart(struct ifnet *);
100 static int tapifioctl(struct ifnet *, u_long, caddr_t);
101 static void tapifinit(void *);
103 static int tap_clone_create(struct if_clone *, int, caddr_t);
104 static void tap_clone_destroy(struct ifnet *);
105 static struct if_clone *tap_cloner;
106 static int vmnet_clone_create(struct if_clone *, int, caddr_t);
107 static void vmnet_clone_destroy(struct ifnet *);
108 static struct if_clone *vmnet_cloner;
110 /* character device */
111 static d_open_t tapopen;
112 static d_close_t tapclose;
113 static d_read_t tapread;
114 static d_write_t tapwrite;
115 static d_ioctl_t tapioctl;
116 static d_poll_t tappoll;
117 static d_kqfilter_t tapkqfilter;
120 static int tapkqread(struct knote *, long);
121 static int tapkqwrite(struct knote *, long);
122 static void tapkqdetach(struct knote *);
124 static struct filterops tap_read_filterops = {
127 .f_detach = tapkqdetach,
128 .f_event = tapkqread,
131 static struct filterops tap_write_filterops = {
134 .f_detach = tapkqdetach,
135 .f_event = tapkqwrite,
138 static struct cdevsw tap_cdevsw = {
139 .d_version = D_VERSION,
140 .d_flags = D_NEEDMINOR,
148 .d_kqfilter = tapkqfilter,
152 * All global variables in if_tap.c are locked with tapmtx, with the
153 * exception of tapdebug, which is accessed unlocked; tapclones is
156 static struct mtx tapmtx;
157 static int tapdebug = 0; /* debug flag */
158 static int tapuopen = 0; /* allow user open() */
159 static int tapuponopen = 0; /* IFF_UP on open() */
160 static int tapdclone = 1; /* enable devfs cloning */
161 static SLIST_HEAD(, tap_softc) taphead; /* first device */
162 static struct clonedevs *tapclones;
164 MALLOC_DECLARE(M_TAP);
165 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
166 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
168 SYSCTL_DECL(_net_link);
169 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
170 "Ethernet tunnel software network interface");
171 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
172 "Allow user to open /dev/tap (based on node permissions)");
173 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
174 "Bring interface up when /dev/tap is opened");
175 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
176 "Enable legacy devfs interface creation");
177 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
179 DEV_MODULE(if_tap, tapmodevent, NULL);
182 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
187 /* Find any existing device, or allocate new unit number. */
188 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0);
190 dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
191 "%s%d", tapname, unit);
198 /* vmnet devices are tap devices in disguise */
200 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
205 /* Find any existing device, or allocate new unit number. */
206 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK);
208 dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT,
209 GID_WHEEL, 0600, "%s%d", vmnetname, unit);
217 tap_destroy(struct tap_softc *tp)
219 struct ifnet *ifp = tp->tap_ifp;
221 CURVNET_SET(ifp->if_vnet);
222 destroy_dev(tp->tap_dev);
223 seldrain(&tp->tap_rsel);
224 knlist_clear(&tp->tap_rsel.si_note, 0);
225 knlist_destroy(&tp->tap_rsel.si_note);
229 mtx_destroy(&tp->tap_mtx);
235 tap_clone_destroy(struct ifnet *ifp)
237 struct tap_softc *tp = ifp->if_softc;
240 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
245 /* vmnet devices are tap devices in disguise */
247 vmnet_clone_destroy(struct ifnet *ifp)
249 tap_clone_destroy(ifp);
255 * module event handler
258 tapmodevent(module_t mod, int type, void *data)
260 static eventhandler_tag eh_tag = NULL;
261 struct tap_softc *tp = NULL;
262 struct ifnet *ifp = NULL;
267 /* intitialize device */
269 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
270 SLIST_INIT(&taphead);
272 clone_setup(&tapclones);
273 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
274 if (eh_tag == NULL) {
275 clone_cleanup(&tapclones);
276 mtx_destroy(&tapmtx);
279 tap_cloner = if_clone_simple(tapname, tap_clone_create,
280 tap_clone_destroy, 0);
281 vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create,
282 vmnet_clone_destroy, 0);
287 * The EBUSY algorithm here can't quite atomically
288 * guarantee that this is race-free since we have to
289 * release the tap mtx to deregister the clone handler.
292 SLIST_FOREACH(tp, &taphead, tap_next) {
293 mtx_lock(&tp->tap_mtx);
294 if (tp->tap_flags & TAP_OPEN) {
295 mtx_unlock(&tp->tap_mtx);
299 mtx_unlock(&tp->tap_mtx);
303 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
304 if_clone_detach(tap_cloner);
305 if_clone_detach(vmnet_cloner);
306 drain_dev_clone_events();
309 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
310 SLIST_REMOVE_HEAD(&taphead, tap_next);
315 TAPDEBUG("detaching %s\n", ifp->if_xname);
321 clone_cleanup(&tapclones);
323 mtx_destroy(&tapmtx);
338 * We need to support two kind of devices - tap and vmnet
341 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
343 char devname[SPECNAMELEN + 1];
344 int i, unit, append_unit;
351 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
358 /* We're interested in only tap/vmnet devices. */
359 if (strcmp(name, tapname) == 0) {
361 } else if (strcmp(name, vmnetname) == 0) {
363 extra = VMNET_DEV_MASK;
364 } else if (dev_stdclone(name, NULL, tapname, &unit) != 1) {
365 if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) {
368 extra = VMNET_DEV_MASK;
375 CURVNET_SET(CRED_TO_VNET(cred));
376 /* find any existing device, or allocate new unit number */
377 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
381 * We were passed 'tun' or 'tap', with no unit specified
382 * so we'll need to append it now.
384 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
389 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
390 cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
393 if_clone_create(name, namelen, NULL);
401 * to create interface
404 tapcreate(struct cdev *dev)
406 struct ifnet *ifp = NULL;
407 struct tap_softc *tp = NULL;
408 unsigned short macaddr_hi;
409 uint32_t macaddr_mid;
411 const char *name = NULL;
414 /* allocate driver storage and create device */
415 tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
416 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
418 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
421 unit = dev2unit(dev);
423 /* select device: tap or vmnet */
424 if (unit & VMNET_DEV_MASK) {
426 tp->tap_flags |= TAP_VMNET;
432 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
434 /* generate fake MAC address: 00 bd xx xx xx unit_no */
435 macaddr_hi = htons(0x00bd);
436 macaddr_mid = (uint32_t) ticks;
437 bcopy(&macaddr_hi, eaddr, sizeof(short));
438 bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
439 eaddr[5] = (u_char)unit;
441 /* fill the rest and attach interface */
442 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
444 panic("%s%d: can not if_alloc()", name, unit);
446 if_initname(ifp, name, unit);
447 ifp->if_init = tapifinit;
448 ifp->if_start = tapifstart;
449 ifp->if_ioctl = tapifioctl;
450 ifp->if_mtu = ETHERMTU;
451 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
452 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
453 ifp->if_capabilities |= IFCAP_LINKSTATE;
454 ifp->if_capenable |= IFCAP_LINKSTATE;
459 ether_ifattach(ifp, eaddr);
461 mtx_lock(&tp->tap_mtx);
462 tp->tap_flags |= TAP_INITED;
463 mtx_unlock(&tp->tap_mtx);
465 knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
467 TAPDEBUG("interface %s is created. minor = %#x\n",
468 ifp->if_xname, dev2unit(dev));
475 * to open tunnel. must be superuser
478 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
480 struct tap_softc *tp = NULL;
481 struct ifnet *ifp = NULL;
485 error = priv_check(td, PRIV_NET_TAP);
490 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
495 mtx_lock(&tp->tap_mtx);
496 if (tp->tap_flags & TAP_OPEN) {
497 mtx_unlock(&tp->tap_mtx);
501 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
502 tp->tap_pid = td->td_proc->p_pid;
503 tp->tap_flags |= TAP_OPEN;
506 ifp->if_drv_flags |= IFF_DRV_RUNNING;
507 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
509 ifp->if_flags |= IFF_UP;
510 if_link_state_change(ifp, LINK_STATE_UP);
511 mtx_unlock(&tp->tap_mtx);
513 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
522 * close the device - mark i/f down & delete routing info
525 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
528 struct tap_softc *tp = dev->si_drv1;
529 struct ifnet *ifp = tp->tap_ifp;
531 /* junk all pending output */
532 mtx_lock(&tp->tap_mtx);
533 CURVNET_SET(ifp->if_vnet);
534 IF_DRAIN(&ifp->if_snd);
537 * Do not bring the interface down, and do not anything with
538 * interface, if we are in VMnet mode. Just close the device.
540 if (((tp->tap_flags & TAP_VMNET) == 0) &&
541 (ifp->if_flags & (IFF_UP | IFF_LINK0)) == IFF_UP) {
542 mtx_unlock(&tp->tap_mtx);
544 mtx_lock(&tp->tap_mtx);
545 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
546 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
547 mtx_unlock(&tp->tap_mtx);
548 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
549 rtinit(ifa, (int)RTM_DELETE, 0);
552 mtx_lock(&tp->tap_mtx);
556 if_link_state_change(ifp, LINK_STATE_DOWN);
559 funsetown(&tp->tap_sigio);
560 selwakeuppri(&tp->tap_rsel, PZERO+1);
561 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
563 tp->tap_flags &= ~TAP_OPEN;
565 mtx_unlock(&tp->tap_mtx);
567 TAPDEBUG("%s is closed. minor = %#x\n",
568 ifp->if_xname, dev2unit(dev));
577 * network interface initialization function
582 struct tap_softc *tp = (struct tap_softc *)xtp;
583 struct ifnet *ifp = tp->tap_ifp;
585 TAPDEBUG("initializing %s\n", ifp->if_xname);
587 mtx_lock(&tp->tap_mtx);
588 ifp->if_drv_flags |= IFF_DRV_RUNNING;
589 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
590 mtx_unlock(&tp->tap_mtx);
592 /* attempt to start output */
600 * Process an ioctl request on network interface
603 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
605 struct tap_softc *tp = ifp->if_softc;
606 struct ifreq *ifr = (struct ifreq *)data;
607 struct ifstat *ifs = NULL;
608 struct ifmediareq *ifmr = NULL;
609 int dummy, error = 0;
612 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
618 ifmr = (struct ifmediareq *)data;
619 dummy = ifmr->ifm_count;
621 ifmr->ifm_status = IFM_AVALID;
622 ifmr->ifm_active = IFM_ETHER;
623 if (tp->tap_flags & TAP_OPEN)
624 ifmr->ifm_status |= IFM_ACTIVE;
625 ifmr->ifm_current = ifmr->ifm_active;
627 int media = IFM_ETHER;
628 error = copyout(&media, ifmr->ifm_ulist,
634 ifp->if_mtu = ifr->ifr_mtu;
638 ifs = (struct ifstat *)data;
639 mtx_lock(&tp->tap_mtx);
640 if (tp->tap_pid != 0)
641 snprintf(ifs->ascii, sizeof(ifs->ascii),
642 "\tOpened by PID %d\n", tp->tap_pid);
644 ifs->ascii[0] = '\0';
645 mtx_unlock(&tp->tap_mtx);
649 error = ether_ioctl(ifp, cmd, data);
660 * queue packets from higher level ready to put out
663 tapifstart(struct ifnet *ifp)
665 struct tap_softc *tp = ifp->if_softc;
667 TAPDEBUG("%s starting\n", ifp->if_xname);
670 * do not junk pending output if we are in VMnet mode.
671 * XXX: can this do any harm because of queue overflow?
674 mtx_lock(&tp->tap_mtx);
675 if (((tp->tap_flags & TAP_VMNET) == 0) &&
676 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
680 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
684 IF_DEQUEUE(&ifp->if_snd, m);
687 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
691 mtx_unlock(&tp->tap_mtx);
696 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
698 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
699 if (tp->tap_flags & TAP_RWAIT) {
700 tp->tap_flags &= ~TAP_RWAIT;
704 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
705 mtx_unlock(&tp->tap_mtx);
706 pgsigio(&tp->tap_sigio, SIGIO, 0);
707 mtx_lock(&tp->tap_mtx);
710 selwakeuppri(&tp->tap_rsel, PZERO+1);
711 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
712 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
715 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
716 mtx_unlock(&tp->tap_mtx);
723 * the cdevsw interface is now pretty minimal
726 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
728 struct tap_softc *tp = dev->si_drv1;
729 struct ifnet *ifp = tp->tap_ifp;
730 struct tapinfo *tapp = NULL;
732 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
733 defined(COMPAT_FREEBSD4)
739 tapp = (struct tapinfo *)data;
740 if (ifp->if_type != tapp->type)
742 mtx_lock(&tp->tap_mtx);
743 ifp->if_mtu = tapp->mtu;
744 ifp->if_baudrate = tapp->baudrate;
745 mtx_unlock(&tp->tap_mtx);
749 tapp = (struct tapinfo *)data;
750 mtx_lock(&tp->tap_mtx);
751 tapp->mtu = ifp->if_mtu;
752 tapp->type = ifp->if_type;
753 tapp->baudrate = ifp->if_baudrate;
754 mtx_unlock(&tp->tap_mtx);
758 tapdebug = *(int *)data;
762 *(int *)data = tapdebug;
766 struct ifreq *ifr = (struct ifreq *) data;
768 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
775 mtx_lock(&tp->tap_mtx);
777 tp->tap_flags |= TAP_ASYNC;
779 tp->tap_flags &= ~TAP_ASYNC;
780 mtx_unlock(&tp->tap_mtx);
784 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
787 IFQ_LOCK(&ifp->if_snd);
788 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
789 for (*(int *)data = 0; mb != NULL;
791 *(int *)data += mb->m_len;
792 IFQ_UNLOCK(&ifp->if_snd);
798 return (fsetown(*(int *)data, &tp->tap_sigio));
801 *(int *)data = fgetown(&tp->tap_sigio);
804 /* this is deprecated, FIOSETOWN should be used instead */
806 return (fsetown(-(*(int *)data), &tp->tap_sigio));
808 /* this is deprecated, FIOGETOWN should be used instead */
810 *(int *)data = -fgetown(&tp->tap_sigio);
813 /* VMware/VMnet port ioctl's */
815 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
816 defined(COMPAT_FREEBSD4)
818 ival = IOCPARM_IVAL(data);
819 data = (caddr_t)&ival;
822 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
825 f &= ~IFF_CANTCHANGE;
828 mtx_lock(&tp->tap_mtx);
829 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
830 mtx_unlock(&tp->tap_mtx);
833 case SIOCGIFADDR: /* get MAC address of the remote side */
834 mtx_lock(&tp->tap_mtx);
835 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
836 mtx_unlock(&tp->tap_mtx);
839 case SIOCSIFADDR: /* set MAC address of the remote side */
840 mtx_lock(&tp->tap_mtx);
841 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
842 mtx_unlock(&tp->tap_mtx);
855 * the cdevsw read interface - reads a packet at a time, or at
856 * least as much of a packet as can be read
859 tapread(struct cdev *dev, struct uio *uio, int flag)
861 struct tap_softc *tp = dev->si_drv1;
862 struct ifnet *ifp = tp->tap_ifp;
863 struct mbuf *m = NULL;
866 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
868 mtx_lock(&tp->tap_mtx);
869 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
870 mtx_unlock(&tp->tap_mtx);
873 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
874 ifp->if_xname, dev2unit(dev), tp->tap_flags);
879 tp->tap_flags &= ~TAP_RWAIT;
881 /* sleep until we get a packet */
883 IF_DEQUEUE(&ifp->if_snd, m);
886 if (flag & O_NONBLOCK) {
887 mtx_unlock(&tp->tap_mtx);
888 return (EWOULDBLOCK);
891 tp->tap_flags |= TAP_RWAIT;
892 error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
895 mtx_unlock(&tp->tap_mtx);
900 mtx_unlock(&tp->tap_mtx);
902 /* feed packet to bpf */
905 /* xfer packet to user space */
906 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
907 len = min(uio->uio_resid, m->m_len);
911 error = uiomove(mtod(m, void *), len, uio);
916 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
928 * the cdevsw write interface - an atomic write is a packet - or else!
931 tapwrite(struct cdev *dev, struct uio *uio, int flag)
933 struct ether_header *eh;
934 struct tap_softc *tp = dev->si_drv1;
935 struct ifnet *ifp = tp->tap_ifp;
938 TAPDEBUG("%s writing, minor = %#x\n",
939 ifp->if_xname, dev2unit(dev));
941 if (uio->uio_resid == 0)
944 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
945 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
946 ifp->if_xname, uio->uio_resid, dev2unit(dev));
951 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN,
952 M_PKTHDR)) == NULL) {
953 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
957 m->m_pkthdr.rcvif = ifp;
960 * Only pass a unicast frame to ether_input(), if it would actually
961 * have been received by non-virtual hardware.
963 if (m->m_len < sizeof(struct ether_header)) {
967 eh = mtod(m, struct ether_header *);
969 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
970 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
971 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
976 /* Pass packet up to parent. */
977 CURVNET_SET(ifp->if_vnet);
978 (*ifp->if_input)(ifp, m);
980 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); /* ibytes are counted in parent */
989 * the poll interface, this is only useful on reads
990 * really. the write detect always returns true, write never blocks
991 * anyway, it either accepts the packet or drops it
994 tappoll(struct cdev *dev, int events, struct thread *td)
996 struct tap_softc *tp = dev->si_drv1;
997 struct ifnet *ifp = tp->tap_ifp;
1000 TAPDEBUG("%s polling, minor = %#x\n",
1001 ifp->if_xname, dev2unit(dev));
1003 if (events & (POLLIN | POLLRDNORM)) {
1004 IFQ_LOCK(&ifp->if_snd);
1005 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1006 TAPDEBUG("%s have data in queue. len = %d, " \
1007 "minor = %#x\n", ifp->if_xname,
1008 ifp->if_snd.ifq_len, dev2unit(dev));
1010 revents |= (events & (POLLIN | POLLRDNORM));
1012 TAPDEBUG("%s waiting for data, minor = %#x\n",
1013 ifp->if_xname, dev2unit(dev));
1015 selrecord(td, &tp->tap_rsel);
1017 IFQ_UNLOCK(&ifp->if_snd);
1020 if (events & (POLLOUT | POLLWRNORM))
1021 revents |= (events & (POLLOUT | POLLWRNORM));
1030 * support for kevent() system call
1033 tapkqfilter(struct cdev *dev, struct knote *kn)
1035 struct tap_softc *tp = dev->si_drv1;
1036 struct ifnet *ifp = tp->tap_ifp;
1038 switch (kn->kn_filter) {
1040 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1041 ifp->if_xname, dev2unit(dev));
1042 kn->kn_fop = &tap_read_filterops;
1046 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1047 ifp->if_xname, dev2unit(dev));
1048 kn->kn_fop = &tap_write_filterops;
1052 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1053 ifp->if_xname, dev2unit(dev));
1059 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1068 * Return true if there is data in the interface queue
1071 tapkqread(struct knote *kn, long hint)
1074 struct tap_softc *tp = kn->kn_hook;
1075 struct cdev *dev = tp->tap_dev;
1076 struct ifnet *ifp = tp->tap_ifp;
1078 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1079 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1080 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1083 TAPDEBUG("%s waiting for data, minor = %#x\n",
1084 ifp->if_xname, dev2unit(dev));
1095 * Always can write. Return the MTU in kn->data
1098 tapkqwrite(struct knote *kn, long hint)
1100 struct tap_softc *tp = kn->kn_hook;
1101 struct ifnet *ifp = tp->tap_ifp;
1103 kn->kn_data = ifp->if_mtu;
1110 tapkqdetach(struct knote *kn)
1112 struct tap_softc *tp = kn->kn_hook;
1114 knlist_remove(&tp->tap_rsel.si_note, kn, 0);