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>
44 #include <sys/fcntl.h>
45 #include <sys/filio.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
54 #include <sys/selinfo.h>
55 #include <sys/signalvar.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/ttycom.h>
62 #include <sys/queue.h>
65 #include <net/ethernet.h>
67 #include <net/if_var.h>
68 #include <net/if_clone.h>
69 #include <net/if_dl.h>
70 #include <net/if_media.h>
71 #include <net/if_types.h>
72 #include <net/route.h>
75 #include <netinet/in.h>
77 #include <net/if_tapvar.h>
78 #include <net/if_tap.h>
80 #define CDEV_NAME "tap"
81 #define TAPDEBUG if (tapdebug) printf
83 static const char tapname[] = "tap";
84 static const char vmnetname[] = "vmnet";
85 #define TAPMAXUNIT 0x7fff
86 #define VMNET_DEV_MASK CLONE_FLAG0
89 static int tapmodevent(module_t, int, void *);
92 static void tapclone(void *, struct ucred *, char *, int,
94 static void tapcreate(struct cdev *);
96 /* network interface */
97 static void tapifstart(struct ifnet *);
98 static int tapifioctl(struct ifnet *, u_long, caddr_t);
99 static void tapifinit(void *);
101 static int tap_clone_create(struct if_clone *, int, caddr_t);
102 static void tap_clone_destroy(struct ifnet *);
103 static struct if_clone *tap_cloner;
104 static int vmnet_clone_create(struct if_clone *, int, caddr_t);
105 static void vmnet_clone_destroy(struct ifnet *);
106 static struct if_clone *vmnet_cloner;
108 /* character device */
109 static d_open_t tapopen;
110 static d_close_t tapclose;
111 static d_read_t tapread;
112 static d_write_t tapwrite;
113 static d_ioctl_t tapioctl;
114 static d_poll_t tappoll;
115 static d_kqfilter_t tapkqfilter;
118 static int tapkqread(struct knote *, long);
119 static int tapkqwrite(struct knote *, long);
120 static void tapkqdetach(struct knote *);
122 static struct filterops tap_read_filterops = {
125 .f_detach = tapkqdetach,
126 .f_event = tapkqread,
129 static struct filterops tap_write_filterops = {
132 .f_detach = tapkqdetach,
133 .f_event = tapkqwrite,
136 static struct cdevsw tap_cdevsw = {
137 .d_version = D_VERSION,
138 .d_flags = D_NEEDMINOR,
146 .d_kqfilter = tapkqfilter,
150 * All global variables in if_tap.c are locked with tapmtx, with the
151 * exception of tapdebug, which is accessed unlocked; tapclones is
154 static struct mtx tapmtx;
155 static int tapdebug = 0; /* debug flag */
156 static int tapuopen = 0; /* allow user open() */
157 static int tapuponopen = 0; /* IFF_UP on open() */
158 static int tapdclone = 1; /* enable devfs cloning */
159 static SLIST_HEAD(, tap_softc) taphead; /* first device */
160 static struct clonedevs *tapclones;
162 MALLOC_DECLARE(M_TAP);
163 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
164 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
166 SYSCTL_DECL(_net_link);
167 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
168 "Ethernet tunnel software network interface");
169 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
170 "Allow user to open /dev/tap (based on node permissions)");
171 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
172 "Bring interface up when /dev/tap is opened");
173 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
174 "Enable legacy devfs interface creation");
175 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
177 DEV_MODULE(if_tap, tapmodevent, NULL);
180 tap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
185 /* Find any existing device, or allocate new unit number. */
186 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0);
188 dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
189 "%s%d", tapname, unit);
196 /* vmnet devices are tap devices in disguise */
198 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
203 /* Find any existing device, or allocate new unit number. */
204 i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK);
206 dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT,
207 GID_WHEEL, 0600, "%s%d", vmnetname, unit);
215 tap_destroy(struct tap_softc *tp)
217 struct ifnet *ifp = tp->tap_ifp;
219 CURVNET_SET(ifp->if_vnet);
220 destroy_dev(tp->tap_dev);
221 seldrain(&tp->tap_rsel);
222 knlist_clear(&tp->tap_rsel.si_note, 0);
223 knlist_destroy(&tp->tap_rsel.si_note);
227 mtx_destroy(&tp->tap_mtx);
233 tap_clone_destroy(struct ifnet *ifp)
235 struct tap_softc *tp = ifp->if_softc;
238 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
243 /* vmnet devices are tap devices in disguise */
245 vmnet_clone_destroy(struct ifnet *ifp)
247 tap_clone_destroy(ifp);
253 * module event handler
256 tapmodevent(module_t mod, int type, void *data)
258 static eventhandler_tag eh_tag = NULL;
259 struct tap_softc *tp = NULL;
260 struct ifnet *ifp = NULL;
265 /* intitialize device */
267 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
268 SLIST_INIT(&taphead);
270 clone_setup(&tapclones);
271 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
272 if (eh_tag == NULL) {
273 clone_cleanup(&tapclones);
274 mtx_destroy(&tapmtx);
277 tap_cloner = if_clone_simple(tapname, tap_clone_create,
278 tap_clone_destroy, 0);
279 vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create,
280 vmnet_clone_destroy, 0);
285 * The EBUSY algorithm here can't quite atomically
286 * guarantee that this is race-free since we have to
287 * release the tap mtx to deregister the clone handler.
290 SLIST_FOREACH(tp, &taphead, tap_next) {
291 mtx_lock(&tp->tap_mtx);
292 if (tp->tap_flags & TAP_OPEN) {
293 mtx_unlock(&tp->tap_mtx);
297 mtx_unlock(&tp->tap_mtx);
301 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
302 if_clone_detach(tap_cloner);
303 if_clone_detach(vmnet_cloner);
304 drain_dev_clone_events();
307 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
308 SLIST_REMOVE_HEAD(&taphead, tap_next);
313 TAPDEBUG("detaching %s\n", ifp->if_xname);
319 clone_cleanup(&tapclones);
321 mtx_destroy(&tapmtx);
336 * We need to support two kind of devices - tap and vmnet
339 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
341 char devname[SPECNAMELEN + 1];
342 int i, unit, append_unit;
349 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE) != 0))
356 /* We're interested in only tap/vmnet devices. */
357 if (strcmp(name, tapname) == 0) {
359 } else if (strcmp(name, vmnetname) == 0) {
361 extra = VMNET_DEV_MASK;
362 } else if (dev_stdclone(name, NULL, tapname, &unit) != 1) {
363 if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) {
366 extra = VMNET_DEV_MASK;
373 CURVNET_SET(CRED_TO_VNET(cred));
374 /* find any existing device, or allocate new unit number */
375 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
379 * We were passed 'tun' or 'tap', with no unit specified
380 * so we'll need to append it now.
382 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
387 *dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
388 cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
391 if_clone_create(name, namelen, NULL);
399 * to create interface
402 tapcreate(struct cdev *dev)
404 struct ifnet *ifp = NULL;
405 struct tap_softc *tp = NULL;
406 unsigned short macaddr_hi;
407 uint32_t macaddr_mid;
409 const char *name = NULL;
412 /* allocate driver storage and create device */
413 tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
414 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
416 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
419 unit = dev2unit(dev);
421 /* select device: tap or vmnet */
422 if (unit & VMNET_DEV_MASK) {
424 tp->tap_flags |= TAP_VMNET;
430 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
432 /* generate fake MAC address: 00 bd xx xx xx unit_no */
433 macaddr_hi = htons(0x00bd);
434 macaddr_mid = (uint32_t) ticks;
435 bcopy(&macaddr_hi, eaddr, sizeof(short));
436 bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
437 eaddr[5] = (u_char)unit;
439 /* fill the rest and attach interface */
440 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
442 panic("%s%d: can not if_alloc()", name, unit);
444 if_initname(ifp, name, unit);
445 ifp->if_init = tapifinit;
446 ifp->if_start = tapifstart;
447 ifp->if_ioctl = tapifioctl;
448 ifp->if_mtu = ETHERMTU;
449 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
450 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
451 ifp->if_capabilities |= IFCAP_LINKSTATE;
452 ifp->if_capenable |= IFCAP_LINKSTATE;
457 ether_ifattach(ifp, eaddr);
459 mtx_lock(&tp->tap_mtx);
460 tp->tap_flags |= TAP_INITED;
461 mtx_unlock(&tp->tap_mtx);
463 knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
465 TAPDEBUG("interface %s is created. minor = %#x\n",
466 ifp->if_xname, dev2unit(dev));
473 * to open tunnel. must be superuser
476 tapopen(struct cdev *dev, int flag, int mode, struct thread *td)
478 struct tap_softc *tp = NULL;
479 struct ifnet *ifp = NULL;
483 error = priv_check(td, PRIV_NET_TAP);
488 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
493 mtx_lock(&tp->tap_mtx);
494 if (tp->tap_flags & TAP_OPEN) {
495 mtx_unlock(&tp->tap_mtx);
499 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
500 tp->tap_pid = td->td_proc->p_pid;
501 tp->tap_flags |= TAP_OPEN;
504 ifp->if_drv_flags |= IFF_DRV_RUNNING;
505 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
507 ifp->if_flags |= IFF_UP;
508 if_link_state_change(ifp, LINK_STATE_UP);
509 mtx_unlock(&tp->tap_mtx);
511 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
520 * close the device - mark i/f down & delete routing info
523 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
526 struct tap_softc *tp = dev->si_drv1;
527 struct ifnet *ifp = tp->tap_ifp;
529 /* junk all pending output */
530 mtx_lock(&tp->tap_mtx);
531 CURVNET_SET(ifp->if_vnet);
532 IF_DRAIN(&ifp->if_snd);
535 * Do not bring the interface down, and do not anything with
536 * interface, if we are in VMnet mode. Just close the device.
538 if (((tp->tap_flags & TAP_VMNET) == 0) &&
539 (ifp->if_flags & (IFF_UP | IFF_LINK0)) == IFF_UP) {
540 mtx_unlock(&tp->tap_mtx);
542 mtx_lock(&tp->tap_mtx);
543 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
544 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
545 mtx_unlock(&tp->tap_mtx);
546 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
547 rtinit(ifa, (int)RTM_DELETE, 0);
550 mtx_lock(&tp->tap_mtx);
554 if_link_state_change(ifp, LINK_STATE_DOWN);
557 funsetown(&tp->tap_sigio);
558 selwakeuppri(&tp->tap_rsel, PZERO+1);
559 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
561 tp->tap_flags &= ~TAP_OPEN;
563 mtx_unlock(&tp->tap_mtx);
565 TAPDEBUG("%s is closed. minor = %#x\n",
566 ifp->if_xname, dev2unit(dev));
575 * network interface initialization function
580 struct tap_softc *tp = (struct tap_softc *)xtp;
581 struct ifnet *ifp = tp->tap_ifp;
583 TAPDEBUG("initializing %s\n", ifp->if_xname);
585 mtx_lock(&tp->tap_mtx);
586 ifp->if_drv_flags |= IFF_DRV_RUNNING;
587 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
588 mtx_unlock(&tp->tap_mtx);
590 /* attempt to start output */
598 * Process an ioctl request on network interface
601 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
603 struct tap_softc *tp = ifp->if_softc;
604 struct ifreq *ifr = (struct ifreq *)data;
605 struct ifstat *ifs = NULL;
606 struct ifmediareq *ifmr = NULL;
607 int dummy, error = 0;
610 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
616 ifmr = (struct ifmediareq *)data;
617 dummy = ifmr->ifm_count;
619 ifmr->ifm_status = IFM_AVALID;
620 ifmr->ifm_active = IFM_ETHER;
621 if (tp->tap_flags & TAP_OPEN)
622 ifmr->ifm_status |= IFM_ACTIVE;
623 ifmr->ifm_current = ifmr->ifm_active;
625 int media = IFM_ETHER;
626 error = copyout(&media, ifmr->ifm_ulist,
632 ifp->if_mtu = ifr->ifr_mtu;
636 ifs = (struct ifstat *)data;
637 mtx_lock(&tp->tap_mtx);
638 if (tp->tap_pid != 0)
639 snprintf(ifs->ascii, sizeof(ifs->ascii),
640 "\tOpened by PID %d\n", tp->tap_pid);
642 ifs->ascii[0] = '\0';
643 mtx_unlock(&tp->tap_mtx);
647 error = ether_ioctl(ifp, cmd, data);
658 * queue packets from higher level ready to put out
661 tapifstart(struct ifnet *ifp)
663 struct tap_softc *tp = ifp->if_softc;
665 TAPDEBUG("%s starting\n", ifp->if_xname);
668 * do not junk pending output if we are in VMnet mode.
669 * XXX: can this do any harm because of queue overflow?
672 mtx_lock(&tp->tap_mtx);
673 if (((tp->tap_flags & TAP_VMNET) == 0) &&
674 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
678 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
682 IF_DEQUEUE(&ifp->if_snd, m);
685 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
689 mtx_unlock(&tp->tap_mtx);
694 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
696 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
697 if (tp->tap_flags & TAP_RWAIT) {
698 tp->tap_flags &= ~TAP_RWAIT;
702 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
703 mtx_unlock(&tp->tap_mtx);
704 pgsigio(&tp->tap_sigio, SIGIO, 0);
705 mtx_lock(&tp->tap_mtx);
708 selwakeuppri(&tp->tap_rsel, PZERO+1);
709 KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
710 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
713 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
714 mtx_unlock(&tp->tap_mtx);
721 * the cdevsw interface is now pretty minimal
724 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
727 struct tap_softc *tp = dev->si_drv1;
728 struct ifnet *ifp = tp->tap_ifp;
729 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 if (ifp->if_mtu != tapp->mtu) {
744 strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
745 ifr.ifr_mtu = tapp->mtu;
746 CURVNET_SET(ifp->if_vnet);
747 error = ifhwioctl(SIOCSIFMTU, ifp,
751 mtx_unlock(&tp->tap_mtx);
755 ifp->if_baudrate = tapp->baudrate;
756 mtx_unlock(&tp->tap_mtx);
760 tapp = (struct tapinfo *)data;
761 mtx_lock(&tp->tap_mtx);
762 tapp->mtu = ifp->if_mtu;
763 tapp->type = ifp->if_type;
764 tapp->baudrate = ifp->if_baudrate;
765 mtx_unlock(&tp->tap_mtx);
769 tapdebug = *(int *)data;
773 *(int *)data = tapdebug;
777 struct ifreq *ifr = (struct ifreq *) data;
779 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
786 mtx_lock(&tp->tap_mtx);
788 tp->tap_flags |= TAP_ASYNC;
790 tp->tap_flags &= ~TAP_ASYNC;
791 mtx_unlock(&tp->tap_mtx);
795 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
798 IFQ_LOCK(&ifp->if_snd);
799 IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
800 for (*(int *)data = 0; mb != NULL;
802 *(int *)data += mb->m_len;
803 IFQ_UNLOCK(&ifp->if_snd);
809 return (fsetown(*(int *)data, &tp->tap_sigio));
812 *(int *)data = fgetown(&tp->tap_sigio);
815 /* this is deprecated, FIOSETOWN should be used instead */
817 return (fsetown(-(*(int *)data), &tp->tap_sigio));
819 /* this is deprecated, FIOGETOWN should be used instead */
821 *(int *)data = -fgetown(&tp->tap_sigio);
824 /* VMware/VMnet port ioctl's */
826 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
827 defined(COMPAT_FREEBSD4)
829 ival = IOCPARM_IVAL(data);
830 data = (caddr_t)&ival;
833 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
836 f &= ~IFF_CANTCHANGE;
839 mtx_lock(&tp->tap_mtx);
840 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
841 mtx_unlock(&tp->tap_mtx);
844 case SIOCGIFADDR: /* get MAC address of the remote side */
845 mtx_lock(&tp->tap_mtx);
846 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
847 mtx_unlock(&tp->tap_mtx);
850 case SIOCSIFADDR: /* set MAC address of the remote side */
851 mtx_lock(&tp->tap_mtx);
852 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
853 mtx_unlock(&tp->tap_mtx);
866 * the cdevsw read interface - reads a packet at a time, or at
867 * least as much of a packet as can be read
870 tapread(struct cdev *dev, struct uio *uio, int flag)
872 struct tap_softc *tp = dev->si_drv1;
873 struct ifnet *ifp = tp->tap_ifp;
874 struct mbuf *m = NULL;
877 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
879 mtx_lock(&tp->tap_mtx);
880 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
881 mtx_unlock(&tp->tap_mtx);
884 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
885 ifp->if_xname, dev2unit(dev), tp->tap_flags);
890 tp->tap_flags &= ~TAP_RWAIT;
892 /* sleep until we get a packet */
894 IF_DEQUEUE(&ifp->if_snd, m);
897 if (flag & O_NONBLOCK) {
898 mtx_unlock(&tp->tap_mtx);
899 return (EWOULDBLOCK);
902 tp->tap_flags |= TAP_RWAIT;
903 error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
906 mtx_unlock(&tp->tap_mtx);
911 mtx_unlock(&tp->tap_mtx);
913 /* feed packet to bpf */
916 /* xfer packet to user space */
917 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
918 len = min(uio->uio_resid, m->m_len);
922 error = uiomove(mtod(m, void *), len, uio);
927 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
939 * the cdevsw write interface - an atomic write is a packet - or else!
942 tapwrite(struct cdev *dev, struct uio *uio, int flag)
944 struct ether_header *eh;
945 struct tap_softc *tp = dev->si_drv1;
946 struct ifnet *ifp = tp->tap_ifp;
949 TAPDEBUG("%s writing, minor = %#x\n",
950 ifp->if_xname, dev2unit(dev));
952 if (uio->uio_resid == 0)
955 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
956 TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
957 ifp->if_xname, uio->uio_resid, dev2unit(dev));
962 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN,
963 M_PKTHDR)) == NULL) {
964 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
968 m->m_pkthdr.rcvif = ifp;
971 * Only pass a unicast frame to ether_input(), if it would actually
972 * have been received by non-virtual hardware.
974 if (m->m_len < sizeof(struct ether_header)) {
978 eh = mtod(m, struct ether_header *);
980 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
981 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
982 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
987 /* Pass packet up to parent. */
988 CURVNET_SET(ifp->if_vnet);
989 (*ifp->if_input)(ifp, m);
991 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); /* ibytes are counted in parent */
1000 * the poll interface, this is only useful on reads
1001 * really. the write detect always returns true, write never blocks
1002 * anyway, it either accepts the packet or drops it
1005 tappoll(struct cdev *dev, int events, struct thread *td)
1007 struct tap_softc *tp = dev->si_drv1;
1008 struct ifnet *ifp = tp->tap_ifp;
1011 TAPDEBUG("%s polling, minor = %#x\n",
1012 ifp->if_xname, dev2unit(dev));
1014 if (events & (POLLIN | POLLRDNORM)) {
1015 IFQ_LOCK(&ifp->if_snd);
1016 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1017 TAPDEBUG("%s have data in queue. len = %d, " \
1018 "minor = %#x\n", ifp->if_xname,
1019 ifp->if_snd.ifq_len, dev2unit(dev));
1021 revents |= (events & (POLLIN | POLLRDNORM));
1023 TAPDEBUG("%s waiting for data, minor = %#x\n",
1024 ifp->if_xname, dev2unit(dev));
1026 selrecord(td, &tp->tap_rsel);
1028 IFQ_UNLOCK(&ifp->if_snd);
1031 if (events & (POLLOUT | POLLWRNORM))
1032 revents |= (events & (POLLOUT | POLLWRNORM));
1041 * support for kevent() system call
1044 tapkqfilter(struct cdev *dev, struct knote *kn)
1046 struct tap_softc *tp = dev->si_drv1;
1047 struct ifnet *ifp = tp->tap_ifp;
1049 switch (kn->kn_filter) {
1051 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1052 ifp->if_xname, dev2unit(dev));
1053 kn->kn_fop = &tap_read_filterops;
1057 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1058 ifp->if_xname, dev2unit(dev));
1059 kn->kn_fop = &tap_write_filterops;
1063 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1064 ifp->if_xname, dev2unit(dev));
1070 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1079 * Return true if there is data in the interface queue
1082 tapkqread(struct knote *kn, long hint)
1085 struct tap_softc *tp = kn->kn_hook;
1086 struct cdev *dev = tp->tap_dev;
1087 struct ifnet *ifp = tp->tap_ifp;
1089 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1090 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1091 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1094 TAPDEBUG("%s waiting for data, minor = %#x\n",
1095 ifp->if_xname, dev2unit(dev));
1106 * Always can write. Return the MTU in kn->data
1109 tapkqwrite(struct knote *kn, long hint)
1111 struct tap_softc *tp = kn->kn_hook;
1112 struct ifnet *ifp = tp->tap_ifp;
1114 kn->kn_data = ifp->if_mtu;
1121 tapkqdetach(struct knote *kn)
1123 struct tap_softc *tp = kn->kn_hook;
1125 knlist_remove(&tp->tap_rsel.si_note, kn, 0);