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 $
42 #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>
60 #include <sys/sysctl.h>
61 #include <sys/systm.h>
62 #include <sys/ttycom.h>
64 #include <sys/queue.h>
67 #include <net/ethernet.h>
69 #include <net/if_var.h>
70 #include <net/if_clone.h>
71 #include <net/if_dl.h>
72 #include <net/if_media.h>
73 #include <net/if_types.h>
74 #include <net/route.h>
77 #include <netinet/in.h>
79 #include <net/if_tapvar.h>
80 #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 static struct sx tap_ioctl_sx;
169 SX_SYSINIT(tap_ioctl_sx, &tap_ioctl_sx, "tap_ioctl");
171 SYSCTL_DECL(_net_link);
172 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
173 "Ethernet tunnel software network interface");
174 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
175 "Allow user to open /dev/tap (based on node permissions)");
176 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
177 "Bring interface up when /dev/tap is opened");
178 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
179 "Enable legacy devfs interface creation");
180 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
182 DEV_MODULE(if_tap, tapmodevent, NULL);
183 MODULE_VERSION(if_tap, 1);
186 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
191 /* Find any existing device, or allocate new unit number. */
192 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0);
194 dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
195 "%s%d", tapname, unit);
202 /* vmnet devices are tap devices in disguise */
204 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
209 /* Find any existing device, or allocate new unit number. */
210 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK);
212 dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT,
213 GID_WHEEL, 0600, "%s%d", vmnetname, unit);
221 tap_destroy(struct tap_softc *tp)
223 struct ifnet *ifp = tp->tap_ifp;
225 CURVNET_SET(ifp->if_vnet);
227 destroy_dev(tp->tap_dev);
228 seldrain(&tp->tap_rsel);
229 knlist_clear(&tp->tap_rsel.si_note, 0);
230 knlist_destroy(&tp->tap_rsel.si_note);
233 sx_xlock(&tap_ioctl_sx);
234 ifp->if_softc = NULL;
235 sx_xunlock(&tap_ioctl_sx);
239 mtx_destroy(&tp->tap_mtx);
245 tap_clone_destroy(struct ifnet *ifp)
247 struct tap_softc *tp = ifp->if_softc;
250 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
255 /* vmnet devices are tap devices in disguise */
257 vmnet_clone_destroy(struct ifnet *ifp)
259 tap_clone_destroy(ifp);
265 * module event handler
268 tapmodevent(module_t mod, int type, void *data)
270 static eventhandler_tag eh_tag = NULL;
271 struct tap_softc *tp = NULL;
272 struct ifnet *ifp = NULL;
277 /* intitialize device */
279 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
280 SLIST_INIT(&taphead);
282 clone_setup(&tapclones);
283 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
284 if (eh_tag == NULL) {
285 clone_cleanup(&tapclones);
286 mtx_destroy(&tapmtx);
289 tap_cloner = if_clone_simple(tapname, tap_clone_create,
290 tap_clone_destroy, 0);
291 vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create,
292 vmnet_clone_destroy, 0);
297 * The EBUSY algorithm here can't quite atomically
298 * guarantee that this is race-free since we have to
299 * release the tap mtx to deregister the clone handler.
302 SLIST_FOREACH(tp, &taphead, tap_next) {
303 mtx_lock(&tp->tap_mtx);
304 if (tp->tap_flags & TAP_OPEN) {
305 mtx_unlock(&tp->tap_mtx);
309 mtx_unlock(&tp->tap_mtx);
313 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
314 if_clone_detach(tap_cloner);
315 if_clone_detach(vmnet_cloner);
316 drain_dev_clone_events();
319 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
320 SLIST_REMOVE_HEAD(&taphead, tap_next);
325 TAPDEBUG("detaching %s\n", ifp->if_xname);
331 clone_cleanup(&tapclones);
333 mtx_destroy(&tapmtx);
348 * We need to support two kind of devices - tap and vmnet
351 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
353 char devname[SPECNAMELEN + 1];
354 int i, unit, append_unit;
361 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
368 /* We're interested in only tap/vmnet devices. */
369 if (strcmp(name, tapname) == 0) {
371 } else if (strcmp(name, vmnetname) == 0) {
373 extra = VMNET_DEV_MASK;
374 } else if (dev_stdclone(name, NULL, tapname, &unit) != 1) {
375 if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) {
378 extra = VMNET_DEV_MASK;
385 CURVNET_SET(CRED_TO_VNET(cred));
386 /* find any existing device, or allocate new unit number */
387 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
391 * We were passed 'tun' or 'tap', with no unit specified
392 * so we'll need to append it now.
394 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
399 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
400 cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
403 if_clone_create(name, namelen, NULL);
411 * to create interface
414 tapcreate(struct cdev *dev)
416 struct ifnet *ifp = NULL;
417 struct tap_softc *tp = NULL;
418 unsigned short macaddr_hi;
419 uint32_t macaddr_mid;
421 const char *name = NULL;
424 /* allocate driver storage and create device */
425 tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
426 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
428 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
431 unit = dev2unit(dev);
433 /* select device: tap or vmnet */
434 if (unit & VMNET_DEV_MASK) {
436 tp->tap_flags |= TAP_VMNET;
442 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
444 /* generate fake MAC address: 00 bd xx xx xx unit_no */
445 macaddr_hi = htons(0x00bd);
446 macaddr_mid = (uint32_t) ticks;
447 bcopy(&macaddr_hi, eaddr, sizeof(short));
448 bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
449 eaddr[5] = (u_char)unit;
451 /* fill the rest and attach interface */
452 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
454 panic("%s%d: can not if_alloc()", name, unit);
456 if_initname(ifp, name, unit);
457 ifp->if_init = tapifinit;
458 ifp->if_start = tapifstart;
459 ifp->if_ioctl = tapifioctl;
460 ifp->if_mtu = ETHERMTU;
461 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
462 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
463 ifp->if_capabilities |= IFCAP_LINKSTATE;
464 ifp->if_capenable |= IFCAP_LINKSTATE;
469 ether_ifattach(ifp, eaddr);
471 mtx_lock(&tp->tap_mtx);
472 tp->tap_flags |= TAP_INITED;
473 mtx_unlock(&tp->tap_mtx);
475 knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
477 TAPDEBUG("interface %s is created. minor = %#x\n",
478 ifp->if_xname, dev2unit(dev));
485 * to open tunnel. must be superuser
488 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
490 struct tap_softc *tp = NULL;
491 struct ifnet *ifp = NULL;
495 error = priv_check(td, PRIV_NET_TAP);
500 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
505 mtx_lock(&tp->tap_mtx);
506 if (tp->tap_flags & TAP_OPEN) {
507 mtx_unlock(&tp->tap_mtx);
511 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
512 tp->tap_pid = td->td_proc->p_pid;
513 tp->tap_flags |= TAP_OPEN;
516 ifp->if_drv_flags |= IFF_DRV_RUNNING;
517 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
519 ifp->if_flags |= IFF_UP;
520 if_link_state_change(ifp, LINK_STATE_UP);
521 mtx_unlock(&tp->tap_mtx);
523 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
532 * close the device - mark i/f down & delete routing info
535 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
538 struct tap_softc *tp = dev->si_drv1;
539 struct ifnet *ifp = tp->tap_ifp;
541 /* junk all pending output */
542 mtx_lock(&tp->tap_mtx);
543 CURVNET_SET(ifp->if_vnet);
544 IF_DRAIN(&ifp->if_snd);
547 * Do not bring the interface down, and do not anything with
548 * interface, if we are in VMnet mode. Just close the device.
550 if (((tp->tap_flags & TAP_VMNET) == 0) &&
551 (ifp->if_flags & (IFF_UP | IFF_LINK0)) == IFF_UP) {
552 mtx_unlock(&tp->tap_mtx);
554 mtx_lock(&tp->tap_mtx);
555 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
556 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
557 mtx_unlock(&tp->tap_mtx);
558 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
559 rtinit(ifa, (int)RTM_DELETE, 0);
562 mtx_lock(&tp->tap_mtx);
566 if_link_state_change(ifp, LINK_STATE_DOWN);
569 funsetown(&tp->tap_sigio);
570 selwakeuppri(&tp->tap_rsel, PZERO+1);
571 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
573 tp->tap_flags &= ~TAP_OPEN;
575 mtx_unlock(&tp->tap_mtx);
577 TAPDEBUG("%s is closed. minor = %#x\n",
578 ifp->if_xname, dev2unit(dev));
587 * network interface initialization function
592 struct tap_softc *tp = (struct tap_softc *)xtp;
593 struct ifnet *ifp = tp->tap_ifp;
595 TAPDEBUG("initializing %s\n", ifp->if_xname);
597 mtx_lock(&tp->tap_mtx);
598 ifp->if_drv_flags |= IFF_DRV_RUNNING;
599 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
600 mtx_unlock(&tp->tap_mtx);
602 /* attempt to start output */
610 * Process an ioctl request on network interface
613 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
615 struct tap_softc *tp;
616 struct ifreq *ifr = (struct ifreq *)data;
617 struct ifstat *ifs = NULL;
618 struct ifmediareq *ifmr = NULL;
619 int dummy, error = 0;
621 sx_xlock(&tap_ioctl_sx);
628 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
634 ifmr = (struct ifmediareq *)data;
635 dummy = ifmr->ifm_count;
637 ifmr->ifm_status = IFM_AVALID;
638 ifmr->ifm_active = IFM_ETHER;
639 if (tp->tap_flags & TAP_OPEN)
640 ifmr->ifm_status |= IFM_ACTIVE;
641 ifmr->ifm_current = ifmr->ifm_active;
643 int media = IFM_ETHER;
644 error = copyout(&media, ifmr->ifm_ulist,
650 ifp->if_mtu = ifr->ifr_mtu;
654 ifs = (struct ifstat *)data;
655 mtx_lock(&tp->tap_mtx);
656 if (tp->tap_pid != 0)
657 snprintf(ifs->ascii, sizeof(ifs->ascii),
658 "\tOpened by PID %d\n", tp->tap_pid);
660 ifs->ascii[0] = '\0';
661 mtx_unlock(&tp->tap_mtx);
665 error = ether_ioctl(ifp, cmd, data);
670 sx_xunlock(&tap_ioctl_sx);
678 * queue packets from higher level ready to put out
681 tapifstart(struct ifnet *ifp)
683 struct tap_softc *tp = ifp->if_softc;
685 TAPDEBUG("%s starting\n", ifp->if_xname);
688 * do not junk pending output if we are in VMnet mode.
689 * XXX: can this do any harm because of queue overflow?
692 mtx_lock(&tp->tap_mtx);
693 if (((tp->tap_flags & TAP_VMNET) == 0) &&
694 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
698 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
702 IF_DEQUEUE(&ifp->if_snd, m);
705 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
709 mtx_unlock(&tp->tap_mtx);
714 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
716 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
717 if (tp->tap_flags & TAP_RWAIT) {
718 tp->tap_flags &= ~TAP_RWAIT;
722 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
723 mtx_unlock(&tp->tap_mtx);
724 pgsigio(&tp->tap_sigio, SIGIO, 0);
725 mtx_lock(&tp->tap_mtx);
728 selwakeuppri(&tp->tap_rsel, PZERO+1);
729 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
730 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
733 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
734 mtx_unlock(&tp->tap_mtx);
741 * the cdevsw interface is now pretty minimal
744 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
747 struct tap_softc *tp = dev->si_drv1;
748 struct ifnet *ifp = tp->tap_ifp;
749 struct tapinfo *tapp = NULL;
752 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
753 defined(COMPAT_FREEBSD4)
759 tapp = (struct tapinfo *)data;
760 if (ifp->if_type != tapp->type)
762 mtx_lock(&tp->tap_mtx);
763 if (ifp->if_mtu != tapp->mtu) {
764 strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
765 ifr.ifr_mtu = tapp->mtu;
766 CURVNET_SET(ifp->if_vnet);
767 error = ifhwioctl(SIOCSIFMTU, ifp,
771 mtx_unlock(&tp->tap_mtx);
775 ifp->if_baudrate = tapp->baudrate;
776 mtx_unlock(&tp->tap_mtx);
780 tapp = (struct tapinfo *)data;
781 mtx_lock(&tp->tap_mtx);
782 tapp->mtu = ifp->if_mtu;
783 tapp->type = ifp->if_type;
784 tapp->baudrate = ifp->if_baudrate;
785 mtx_unlock(&tp->tap_mtx);
789 tapdebug = *(int *)data;
793 *(int *)data = tapdebug;
797 struct ifreq *ifr = (struct ifreq *) data;
799 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
806 mtx_lock(&tp->tap_mtx);
808 tp->tap_flags |= TAP_ASYNC;
810 tp->tap_flags &= ~TAP_ASYNC;
811 mtx_unlock(&tp->tap_mtx);
815 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
818 IFQ_LOCK(&ifp->if_snd);
819 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
820 for (*(int *)data = 0; mb != NULL;
822 *(int *)data += mb->m_len;
823 IFQ_UNLOCK(&ifp->if_snd);
829 return (fsetown(*(int *)data, &tp->tap_sigio));
832 *(int *)data = fgetown(&tp->tap_sigio);
835 /* this is deprecated, FIOSETOWN should be used instead */
837 return (fsetown(-(*(int *)data), &tp->tap_sigio));
839 /* this is deprecated, FIOGETOWN should be used instead */
841 *(int *)data = -fgetown(&tp->tap_sigio);
844 /* VMware/VMnet port ioctl's */
846 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
847 defined(COMPAT_FREEBSD4)
849 ival = IOCPARM_IVAL(data);
850 data = (caddr_t)&ival;
853 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
856 f &= ~IFF_CANTCHANGE;
859 mtx_lock(&tp->tap_mtx);
860 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
861 mtx_unlock(&tp->tap_mtx);
864 case SIOCGIFADDR: /* get MAC address of the remote side */
865 mtx_lock(&tp->tap_mtx);
866 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
867 mtx_unlock(&tp->tap_mtx);
870 case SIOCSIFADDR: /* set MAC address of the remote side */
871 mtx_lock(&tp->tap_mtx);
872 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
873 mtx_unlock(&tp->tap_mtx);
886 * the cdevsw read interface - reads a packet at a time, or at
887 * least as much of a packet as can be read
890 tapread(struct cdev *dev, struct uio *uio, int flag)
892 struct tap_softc *tp = dev->si_drv1;
893 struct ifnet *ifp = tp->tap_ifp;
894 struct mbuf *m = NULL;
897 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
899 mtx_lock(&tp->tap_mtx);
900 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
901 mtx_unlock(&tp->tap_mtx);
904 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
905 ifp->if_xname, dev2unit(dev), tp->tap_flags);
910 tp->tap_flags &= ~TAP_RWAIT;
912 /* sleep until we get a packet */
914 IF_DEQUEUE(&ifp->if_snd, m);
917 if (flag & O_NONBLOCK) {
918 mtx_unlock(&tp->tap_mtx);
919 return (EWOULDBLOCK);
922 tp->tap_flags |= TAP_RWAIT;
923 error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
926 mtx_unlock(&tp->tap_mtx);
931 mtx_unlock(&tp->tap_mtx);
933 /* feed packet to bpf */
936 /* xfer packet to user space */
937 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
938 len = min(uio->uio_resid, m->m_len);
942 error = uiomove(mtod(m, void *), len, uio);
947 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
959 * the cdevsw write interface - an atomic write is a packet - or else!
962 tapwrite(struct cdev *dev, struct uio *uio, int flag)
964 struct ether_header *eh;
965 struct tap_softc *tp = dev->si_drv1;
966 struct ifnet *ifp = tp->tap_ifp;
969 TAPDEBUG("%s writing, minor = %#x\n",
970 ifp->if_xname, dev2unit(dev));
972 if (uio->uio_resid == 0)
975 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
976 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
977 ifp->if_xname, uio->uio_resid, dev2unit(dev));
982 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN,
983 M_PKTHDR)) == NULL) {
984 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
988 m->m_pkthdr.rcvif = ifp;
991 * Only pass a unicast frame to ether_input(), if it would actually
992 * have been received by non-virtual hardware.
994 if (m->m_len < sizeof(struct ether_header)) {
998 eh = mtod(m, struct ether_header *);
1000 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1001 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1002 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1007 /* Pass packet up to parent. */
1008 CURVNET_SET(ifp->if_vnet);
1009 (*ifp->if_input)(ifp, m);
1011 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); /* ibytes are counted in parent */
1020 * the poll interface, this is only useful on reads
1021 * really. the write detect always returns true, write never blocks
1022 * anyway, it either accepts the packet or drops it
1025 tappoll(struct cdev *dev, int events, struct thread *td)
1027 struct tap_softc *tp = dev->si_drv1;
1028 struct ifnet *ifp = tp->tap_ifp;
1031 TAPDEBUG("%s polling, minor = %#x\n",
1032 ifp->if_xname, dev2unit(dev));
1034 if (events & (POLLIN | POLLRDNORM)) {
1035 IFQ_LOCK(&ifp->if_snd);
1036 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1037 TAPDEBUG("%s have data in queue. len = %d, " \
1038 "minor = %#x\n", ifp->if_xname,
1039 ifp->if_snd.ifq_len, dev2unit(dev));
1041 revents |= (events & (POLLIN | POLLRDNORM));
1043 TAPDEBUG("%s waiting for data, minor = %#x\n",
1044 ifp->if_xname, dev2unit(dev));
1046 selrecord(td, &tp->tap_rsel);
1048 IFQ_UNLOCK(&ifp->if_snd);
1051 if (events & (POLLOUT | POLLWRNORM))
1052 revents |= (events & (POLLOUT | POLLWRNORM));
1061 * support for kevent() system call
1064 tapkqfilter(struct cdev *dev, struct knote *kn)
1066 struct tap_softc *tp = dev->si_drv1;
1067 struct ifnet *ifp = tp->tap_ifp;
1069 switch (kn->kn_filter) {
1071 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1072 ifp->if_xname, dev2unit(dev));
1073 kn->kn_fop = &tap_read_filterops;
1077 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1078 ifp->if_xname, dev2unit(dev));
1079 kn->kn_fop = &tap_write_filterops;
1083 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1084 ifp->if_xname, dev2unit(dev));
1090 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1099 * Return true if there is data in the interface queue
1102 tapkqread(struct knote *kn, long hint)
1105 struct tap_softc *tp = kn->kn_hook;
1106 struct cdev *dev = tp->tap_dev;
1107 struct ifnet *ifp = tp->tap_ifp;
1109 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1110 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1111 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1114 TAPDEBUG("%s waiting for data, minor = %#x\n",
1115 ifp->if_xname, dev2unit(dev));
1126 * Always can write. Return the MTU in kn->data
1129 tapkqwrite(struct knote *kn, long hint)
1131 struct tap_softc *tp = kn->kn_hook;
1132 struct ifnet *ifp = tp->tap_ifp;
1134 kn->kn_data = ifp->if_mtu;
1141 tapkqdetach(struct knote *kn)
1143 struct tap_softc *tp = kn->kn_hook;
1145 knlist_remove(&tp->tap_rsel.si_note, kn, 0);