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_NEEDGIANT,
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, unit2minor(unit | extra),
194 UID_ROOT, GID_WHEEL, 0600, "%s%d", ifc->ifc_name, unit);
197 dev->si_flags |= SI_CHEAPCLONE;
205 /* vmnet devices are tap devices in disguise */
207 vmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
209 return tap_clone_create(ifc, unit, params);
213 tap_destroy(struct tap_softc *tp)
215 struct ifnet *ifp = tp->tap_ifp;
219 KASSERT(!(tp->tap_flags & TAP_OPEN),
220 ("%s flags is out of sync", ifp->if_xname));
222 knlist_destroy(&tp->tap_rsel.si_note);
223 destroy_dev(tp->tap_dev);
226 if_free_type(ifp, IFT_ETHER);
229 mtx_destroy(&tp->tap_mtx);
234 tap_clone_destroy(struct ifnet *ifp)
236 struct tap_softc *tp = ifp->if_softc;
239 SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
244 /* vmnet devices are tap devices in disguise */
246 vmnet_clone_destroy(struct ifnet *ifp)
248 tap_clone_destroy(ifp);
254 * module event handler
257 tapmodevent(module_t mod, int type, void *data)
259 static eventhandler_tag eh_tag = NULL;
260 struct tap_softc *tp = NULL;
261 struct ifnet *ifp = NULL;
266 /* intitialize device */
268 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
269 SLIST_INIT(&taphead);
271 clone_setup(&tapclones);
272 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
273 if (eh_tag == NULL) {
274 clone_cleanup(&tapclones);
275 mtx_destroy(&tapmtx);
278 if_clone_attach(&tap_cloner);
279 if_clone_attach(&vmnet_cloner);
284 * The EBUSY algorithm here can't quite atomically
285 * guarantee that this is race-free since we have to
286 * release the tap mtx to deregister the clone handler.
289 SLIST_FOREACH(tp, &taphead, tap_next) {
290 mtx_lock(&tp->tap_mtx);
291 if (tp->tap_flags & TAP_OPEN) {
292 mtx_unlock(&tp->tap_mtx);
296 mtx_unlock(&tp->tap_mtx);
300 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
301 if_clone_detach(&tap_cloner);
302 if_clone_detach(&vmnet_cloner);
305 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
306 SLIST_REMOVE_HEAD(&taphead, tap_next);
311 TAPDEBUG("detaching %s\n", ifp->if_xname);
317 clone_cleanup(&tapclones);
319 mtx_destroy(&tapmtx);
334 * We need to support two kind of devices - tap and vmnet
337 tapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
339 char devname[SPECNAMELEN + 1];
340 int i, unit, append_unit;
347 (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
354 /* We're interested in only tap/vmnet devices. */
355 if (strcmp(name, TAP) == 0) {
357 } else if (strcmp(name, VMNET) == 0) {
359 extra = VMNET_DEV_MASK;
360 } else if (dev_stdclone(name, NULL, TAP, &unit) != 1) {
361 if (dev_stdclone(name, NULL, VMNET, &unit) != 1) {
364 extra = VMNET_DEV_MASK;
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(&tap_cdevsw, unit2minor(unit | extra),
385 UID_ROOT, GID_WHEEL, 0600, "%s", name);
388 (*dev)->si_flags |= SI_CHEAPCLONE;
392 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;
412 dev->si_flags &= ~SI_CHEAPCLONE;
414 /* allocate driver storage and create device */
415 MALLOC(tp, struct tap_softc *, 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, minor(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 ifp->if_snd.ifq_maxlen = ifqmaxlen;
458 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(&tp->tap_rsel.si_note, NULL, NULL, NULL, NULL);
467 TAPDEBUG("interface %s is created. minor = %#x\n",
468 ifp->if_xname, minor(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;
505 mtx_unlock(&tp->tap_mtx);
508 ifp->if_drv_flags |= IFF_DRV_RUNNING;
509 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
511 ifp->if_flags |= IFF_UP;
514 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, minor(dev));
523 * close the device - mark i/f down & delete routing info
526 tapclose(struct cdev *dev, int foo, int bar, struct thread *td)
529 struct tap_softc *tp = dev->si_drv1;
530 struct ifnet *ifp = tp->tap_ifp;
533 /* junk all pending output */
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.
541 mtx_lock(&tp->tap_mtx);
542 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
543 mtx_unlock(&tp->tap_mtx);
546 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
547 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
548 rtinit(ifa, (int)RTM_DELETE, 0);
551 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
555 mtx_unlock(&tp->tap_mtx);
557 funsetown(&tp->tap_sigio);
558 selwakeuppri(&tp->tap_rsel, PZERO+1);
559 KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
561 mtx_lock(&tp->tap_mtx);
562 tp->tap_flags &= ~TAP_OPEN;
564 mtx_unlock(&tp->tap_mtx);
566 TAPDEBUG("%s is closed. minor = %#x\n",
567 ifp->if_xname, minor(dev));
576 * network interface initialization function
581 struct tap_softc *tp = (struct tap_softc *)xtp;
582 struct ifnet *ifp = tp->tap_ifp;
584 TAPDEBUG("initializing %s\n", ifp->if_xname);
586 ifp->if_drv_flags |= IFF_DRV_RUNNING;
587 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
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;
619 ifs = (struct ifstat *)data;
620 dummy = strlen(ifs->ascii);
621 mtx_lock(&tp->tap_mtx);
622 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
623 snprintf(ifs->ascii + dummy,
624 sizeof(ifs->ascii) - dummy,
625 "\tOpened by PID %d\n", tp->tap_pid);
626 mtx_unlock(&tp->tap_mtx);
632 dummy = ether_ioctl(ifp, cmd, data);
645 * queue packets from higher level ready to put out
648 tapifstart(struct ifnet *ifp)
650 struct tap_softc *tp = ifp->if_softc;
653 TAPDEBUG("%s starting\n", ifp->if_xname);
656 * do not junk pending output if we are in VMnet mode.
657 * XXX: can this do any harm because of queue overflow?
660 mtx_lock(&tp->tap_mtx);
661 if (((tp->tap_flags & TAP_VMNET) == 0) &&
662 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
663 struct mbuf *m = NULL;
665 mtx_unlock(&tp->tap_mtx);
668 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
673 IF_DEQUEUE(&ifp->if_snd, m);
682 mtx_unlock(&tp->tap_mtx);
685 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
687 if (ifp->if_snd.ifq_len != 0) {
688 mtx_lock(&tp->tap_mtx);
689 if (tp->tap_flags & TAP_RWAIT) {
690 tp->tap_flags &= ~TAP_RWAIT;
694 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
695 mtx_unlock(&tp->tap_mtx);
696 pgsigio(&tp->tap_sigio, SIGIO, 0);
698 mtx_unlock(&tp->tap_mtx);
700 selwakeuppri(&tp->tap_rsel, PZERO+1);
701 KNOTE_UNLOCKED(&tp->tap_rsel.si_note, 0);
702 ifp->if_opackets ++; /* obytes are counted in ether_output */
705 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
713 * the cdevsw interface is now pretty minimal
716 tapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
718 struct tap_softc *tp = dev->si_drv1;
719 struct ifnet *ifp = tp->tap_ifp;
720 struct tapinfo *tapp = NULL;
723 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
724 defined(COMPAT_FREEBSD4)
731 tapp = (struct tapinfo *)data;
732 ifp->if_mtu = tapp->mtu;
733 ifp->if_type = tapp->type;
734 ifp->if_baudrate = tapp->baudrate;
739 tapp = (struct tapinfo *)data;
740 tapp->mtu = ifp->if_mtu;
741 tapp->type = ifp->if_type;
742 tapp->baudrate = ifp->if_baudrate;
746 tapdebug = *(int *)data;
750 *(int *)data = tapdebug;
754 struct ifreq *ifr = (struct ifreq *) data;
756 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
764 mtx_lock(&tp->tap_mtx);
766 tp->tap_flags |= TAP_ASYNC;
768 tp->tap_flags &= ~TAP_ASYNC;
769 mtx_unlock(&tp->tap_mtx);
775 if (ifp->if_snd.ifq_head) {
776 struct mbuf *mb = ifp->if_snd.ifq_head;
778 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
779 *(int *)data += mb->m_len;
786 return (fsetown(*(int *)data, &tp->tap_sigio));
789 *(int *)data = fgetown(&tp->tap_sigio);
792 /* this is deprecated, FIOSETOWN should be used instead */
794 return (fsetown(-(*(int *)data), &tp->tap_sigio));
796 /* this is deprecated, FIOGETOWN should be used instead */
798 *(int *)data = -fgetown(&tp->tap_sigio);
801 /* VMware/VMnet port ioctl's */
803 case SIOCGIFFLAGS: /* get ifnet flags */
804 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
807 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
808 defined(COMPAT_FREEBSD4)
810 ival = IOCPARM_IVAL(data);
811 data = (caddr_t)&ival;
814 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
817 f &= ~IFF_CANTCHANGE;
821 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
825 case OSIOCGIFADDR: /* get MAC address of the remote side */
827 mtx_lock(&tp->tap_mtx);
828 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
829 mtx_unlock(&tp->tap_mtx);
832 case SIOCSIFADDR: /* set MAC address of the remote side */
833 mtx_lock(&tp->tap_mtx);
834 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
835 mtx_unlock(&tp->tap_mtx);
848 * the cdevsw read interface - reads a packet at a time, or at
849 * least as much of a packet as can be read
852 tapread(struct cdev *dev, struct uio *uio, int flag)
854 struct tap_softc *tp = dev->si_drv1;
855 struct ifnet *ifp = tp->tap_ifp;
856 struct mbuf *m = NULL;
857 int error = 0, len, s;
859 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev));
861 mtx_lock(&tp->tap_mtx);
862 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
863 mtx_unlock(&tp->tap_mtx);
866 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
867 ifp->if_xname, minor(dev), tp->tap_flags);
872 tp->tap_flags &= ~TAP_RWAIT;
873 mtx_unlock(&tp->tap_mtx);
875 /* sleep until we get a packet */
878 IF_DEQUEUE(&ifp->if_snd, m);
882 if (flag & O_NONBLOCK)
883 return (EWOULDBLOCK);
885 mtx_lock(&tp->tap_mtx);
886 tp->tap_flags |= TAP_RWAIT;
887 mtx_unlock(&tp->tap_mtx);
888 error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0);
894 /* feed packet to bpf */
897 /* xfer packet to user space */
898 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
899 len = min(uio->uio_resid, m->m_len);
903 error = uiomove(mtod(m, void *), len, uio);
908 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
920 * the cdevsw write interface - an atomic write is a packet - or else!
923 tapwrite(struct cdev *dev, struct uio *uio, int flag)
925 struct ether_header *eh;
926 struct tap_softc *tp = dev->si_drv1;
927 struct ifnet *ifp = tp->tap_ifp;
930 TAPDEBUG("%s writting, minor = %#x\n",
931 ifp->if_xname, minor(dev));
933 if (uio->uio_resid == 0)
936 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
937 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
938 ifp->if_xname, uio->uio_resid, minor(dev));
943 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN,
944 M_PKTHDR)) == NULL) {
949 m->m_pkthdr.rcvif = ifp;
952 * Only pass a unicast frame to ether_input(), if it would actually
953 * have been received by non-virtual hardware.
955 if (m->m_len < sizeof(struct ether_header)) {
959 eh = mtod(m, struct ether_header *);
961 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
962 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
963 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
968 /* Pass packet up to parent. */
969 (*ifp->if_input)(ifp, m);
970 ifp->if_ipackets ++; /* ibytes are counted in parent */
979 * the poll interface, this is only useful on reads
980 * really. the write detect always returns true, write never blocks
981 * anyway, it either accepts the packet or drops it
984 tappoll(struct cdev *dev, int events, struct thread *td)
986 struct tap_softc *tp = dev->si_drv1;
987 struct ifnet *ifp = tp->tap_ifp;
990 TAPDEBUG("%s polling, minor = %#x\n",
991 ifp->if_xname, minor(dev));
994 if (events & (POLLIN | POLLRDNORM)) {
995 if (ifp->if_snd.ifq_len > 0) {
996 TAPDEBUG("%s have data in queue. len = %d, " \
997 "minor = %#x\n", ifp->if_xname,
998 ifp->if_snd.ifq_len, minor(dev));
1000 revents |= (events & (POLLIN | POLLRDNORM));
1002 TAPDEBUG("%s waiting for data, minor = %#x\n",
1003 ifp->if_xname, minor(dev));
1005 selrecord(td, &tp->tap_rsel);
1009 if (events & (POLLOUT | POLLWRNORM))
1010 revents |= (events & (POLLOUT | POLLWRNORM));
1020 * support for kevent() system call
1023 tapkqfilter(struct cdev *dev, struct knote *kn)
1026 struct tap_softc *tp = dev->si_drv1;
1027 struct ifnet *ifp = tp->tap_ifp;
1030 switch (kn->kn_filter) {
1032 TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1033 ifp->if_xname, minor(dev));
1034 kn->kn_fop = &tap_read_filterops;
1038 TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1039 ifp->if_xname, minor(dev));
1040 kn->kn_fop = &tap_write_filterops;
1044 TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1045 ifp->if_xname, minor(dev));
1052 kn->kn_hook = (caddr_t) dev;
1053 knlist_add(&tp->tap_rsel.si_note, kn, 0);
1062 * Return true if there is data in the interface queue
1065 tapkqread(struct knote *kn, long hint)
1068 struct cdev *dev = (struct cdev *)(kn->kn_hook);
1069 struct tap_softc *tp = dev->si_drv1;
1070 struct ifnet *ifp = tp->tap_ifp;
1073 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1074 TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1075 ifp->if_xname, ifp->if_snd.ifq_len, minor(dev));
1078 TAPDEBUG("%s waiting for data, minor = %#x\n",
1079 ifp->if_xname, minor(dev));
1091 * Always can write. Return the MTU in kn->data
1094 tapkqwrite(struct knote *kn, long hint)
1097 struct tap_softc *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
1098 struct ifnet *ifp = tp->tap_ifp;
1101 kn->kn_data = ifp->if_mtu;
1109 tapkqdetach(struct knote *kn)
1111 struct tap_softc *tp = ((struct cdev *) kn->kn_hook)->si_drv1;
1113 knlist_remove(&tp->tap_rsel.si_note, kn, 0);