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.
34 * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $
35 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
40 #include <sys/param.h>
42 #include <sys/device.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/ttycom.h>
57 #include <sys/vnode.h>
58 #include <sys/serialize.h>
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
61 #include <sys/devfs.h>
62 #include <sys/queue.h>
65 #include <net/ethernet.h>
67 #include <net/if_types.h>
68 #include <net/if_arp.h>
69 #include <net/if_clone.h>
70 #include <net/if_media.h>
71 #include <net/ifq_var.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
76 #include "if_tapvar.h"
79 #define TAP_IFFLAGS (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST)
82 #define TAPDEBUG if (tapdebug) if_printf
85 static int tapmodevent(module_t, int, void *);
88 static struct tap_softc *tapcreate(cdev_t, int);
89 static void tapdestroy(struct tap_softc *);
92 static int tap_clone_create(struct if_clone *, int,
94 static int tap_clone_destroy(struct ifnet *);
96 /* network interface */
97 static void tapifinit(void *);
98 static void tapifstart(struct ifnet *, struct ifaltq_subque *);
99 static int tapifioctl(struct ifnet *, u_long, caddr_t,
101 static void tapifstop(struct tap_softc *, int);
102 static void tapifflags(struct tap_softc *);
104 /* character device */
105 static d_open_t tapopen;
106 static d_clone_t tapclone;
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_kqfilter_t tapkqfilter;
113 static struct dev_ops tap_ops = {
120 .d_kqfilter = tapkqfilter
124 static void tap_filter_detach(struct knote *);
125 static int tap_filter_read(struct knote *, long);
126 static int tap_filter_write(struct knote *, long);
128 static struct filterops tapread_filtops = {
134 static struct filterops tapwrite_filtops = {
141 static int tapdebug = 0; /* debug flag */
142 static int taprefcnt = 0; /* module ref. counter */
143 static int tapuopen = 0; /* allow user open() */
144 static int tapuponopen = 0; /* IFF_UP when opened */
146 static MALLOC_DEFINE(M_TAP, TAP, "Ethernet tunnel interface");
148 static DEVFS_DEFINE_CLONE_BITMAP(tap);
150 struct if_clone tap_cloner = IF_CLONE_INITIALIZER(
151 TAP, tap_clone_create, tap_clone_destroy, 0, IF_MAXUNIT);
153 static SLIST_HEAD(,tap_softc) tap_listhead =
154 SLIST_HEAD_INITIALIZER(&tap_listhead);
156 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
157 SYSCTL_DECL(_net_link);
158 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
159 "Ethernet tunnel software network interface");
160 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
161 "Allow user to open /dev/tap (based on node permissions)");
162 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
163 "Bring interface up when /dev/tap is opened");
164 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
165 SYSCTL_INT(_net_link_tap, OID_AUTO, refcnt, CTLFLAG_RD, &taprefcnt, 0,
166 "Number of opened devices");
168 DEV_MODULE(if_tap, tapmodevent, NULL);
173 * module event handler
176 tapmodevent(module_t mod, int type, void *data)
178 static cdev_t dev = NULL;
179 struct tap_softc *sc, *sc_tmp;
183 dev = make_autoclone_dev(&tap_ops, &DEVFS_CLONE_BITMAP(tap),
184 tapclone, UID_ROOT, GID_WHEEL,
186 SLIST_INIT(&tap_listhead);
187 if_clone_attach(&tap_cloner);
194 if_clone_detach(&tap_cloner);
196 SLIST_FOREACH_MUTABLE(sc, &tap_listhead, tap_link, sc_tmp)
199 dev_ops_remove_all(&tap_ops);
200 destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tap));
212 * Create or clone an interface
214 static struct tap_softc *
215 tapcreate(cdev_t dev, int flags)
217 struct tap_softc *sc;
219 uint8_t ether_addr[ETHER_ADDR_LEN];
220 int unit = minor(dev);
222 sc = kmalloc(sizeof(*sc), M_TAP, M_WAITOK | M_ZERO);
225 sc->tap_flags |= flags;
227 reference_dev(dev); /* device association */
229 /* generate fake MAC address: 00 bd xx xx xx unit_no */
230 ether_addr[0] = 0x00;
231 ether_addr[1] = 0xbd;
232 bcopy(&ticks, ðer_addr[2], 3);
233 ether_addr[5] = (u_char)unit;
235 /* fill the rest and attach interface */
237 if_initname(ifp, TAP, unit);
238 ifp->if_type = IFT_ETHER;
239 ifp->if_init = tapifinit;
240 ifp->if_start = tapifstart;
241 ifp->if_ioctl = tapifioctl;
242 ifp->if_mtu = ETHERMTU;
243 ifp->if_flags = TAP_IFFLAGS;
245 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
246 ifq_set_ready(&ifp->if_snd);
248 ether_ifattach(ifp, ether_addr, NULL);
250 sc->tap_flags |= TAP_INITED;
251 sc->tap_devq.ifq_maxlen = ifqmaxlen;
253 SLIST_INSERT_HEAD(&tap_listhead, sc, tap_link);
255 TAPDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n",
256 unit, sc->tap_flags);
260 static struct tap_softc *
263 struct tap_softc *sc;
265 SLIST_FOREACH(sc, &tap_listhead, tap_link) {
266 if (minor(sc->tap_dev) == unit)
273 * Create a new tap instance via ifconfig.
276 tap_clone_create(struct if_clone *ifc __unused, int unit,
277 caddr_t params __unused, caddr_t data)
279 struct tap_softc *sc;
280 cdev_t dev = (cdev_t)data;
283 if (tapfind(unit) != NULL)
287 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tap), unit)) {
288 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tap), unit);
289 dev = make_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
290 0600, "%s%d", TAP, unit);
292 dev = devfs_find_device_by_name("%s%d", TAP, unit);
296 flags = TAP_MANUALMAKE;
301 if ((sc = tapcreate(dev, flags)) == NULL)
304 sc->tap_flags |= TAP_CLONE;
306 TAPDEBUG(&sc->tap_if, "clone created, minor = %#x, flags = 0x%x\n",
307 minor(sc->tap_dev), sc->tap_flags);
313 * Open the tap device.
316 tapopen(struct dev_open_args *ap)
319 struct tap_softc *sc = NULL;
320 struct ifnet *ifp = NULL;
324 (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
329 dev = ap->a_head.a_dev;
331 if (sc == NULL && (sc = tapcreate(dev, 0)) == NULL) {
335 if (sc->tap_flags & TAP_OPEN) {
341 bcopy(sc->arpcom.ac_enaddr, sc->ether_addr, sizeof(sc->ether_addr));
343 if (curthread->td_proc)
344 fsetown(curthread->td_proc->p_pid, &sc->tap_sigtd);
345 sc->tap_flags |= TAP_OPEN;
348 if (tapuponopen && (ifp->if_flags & IFF_UP) == 0) {
353 ifnet_serialize_all(ifp);
355 ifnet_deserialize_all(ifp);
357 sc->tap_flags |= TAP_CLOSEDOWN;
360 TAPDEBUG(ifp, "opened, minor = %#x. Module refcnt = %d\n",
361 minor(dev), taprefcnt);
368 tapclone(struct dev_clone_args *ap)
370 char ifname[IFNAMSIZ];
373 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tap), 0);
374 ksnprintf(ifname, IFNAMSIZ, "%s%d", TAP, unit);
375 ap->a_dev = make_only_dev(&tap_ops, unit, UID_ROOT, GID_WHEEL,
379 * Use the if_clone framework to create cloned device/interface,
380 * so the two clone methods (autoclone device /dev/tap; ifconfig
381 * clone) are consistent and can be mix used.
383 * Need to pass the cdev_t because the device created by
384 * 'make_only_dev()' doesn't appear in '/dev' yet so that it can't
385 * be found by 'devfs_find_device_by_name()' in 'tap_clone_create()'.
387 return (if_clone_create(ifname, IFNAMSIZ, NULL, (caddr_t)ap->a_dev));
391 * Close the tap device: bring the interface down & delete routing info.
394 tapclose(struct dev_close_args *ap)
396 cdev_t dev = ap->a_head.a_dev;
397 struct tap_softc *sc = dev->si_drv1;
399 int unit = minor(dev);
401 char ifname[IFNAMSIZ];
404 ("try closing the already destroyed %s%d", TAP, unit));
409 /* Junk all pending output */
410 ifq_purge_all(&ifp->if_snd);
413 * If the interface is not cloned, we always bring it down.
415 * If the interface is cloned, then we bring it down during
416 * closing only if it was brought up during opening.
418 if ((sc->tap_flags & TAP_CLONE) == 0 ||
419 (sc->tap_flags & TAP_CLOSEDOWN)) {
420 if (ifp->if_flags & IFF_UP)
424 ifnet_serialize_all(ifp);
425 tapifstop(sc, clear_flags);
426 ifnet_deserialize_all(ifp);
428 if ((sc->tap_flags & TAP_MANUALMAKE) == 0) {
429 if_purgeaddrs_nolink(ifp);
431 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
433 /* Announce the departure of the interface. */
434 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
437 funsetown(&sc->tap_sigio);
438 sc->tap_sigio = NULL;
439 KNOTE(&sc->tap_rkq.ki_note, 0);
441 sc->tap_flags &= ~TAP_OPEN;
442 funsetown(&sc->tap_sigtd);
443 sc->tap_sigtd = NULL;
448 if_printf(ifp, ". Module refcnt = %d is out of sync! "
449 "Force refcnt to be 0.\n", taprefcnt);
452 TAPDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n",
455 /* Only auto-destroy if the interface was not manually created. */
456 if ((sc->tap_flags & TAP_MANUALMAKE) == 0) {
457 ksnprintf(ifname, IFNAMSIZ, "%s%d", TAP, unit);
458 if_clone_destroy(ifname);
467 * Destroy a tap instance: the interface and the associated device.
470 tapdestroy(struct tap_softc *sc)
472 struct ifnet *ifp = &sc->tap_if;
473 cdev_t dev = sc->tap_dev;
474 int unit = minor(dev);
476 TAPDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n",
479 ifnet_serialize_all(ifp);
481 ifnet_deserialize_all(ifp);
486 release_dev(dev); /* device disassociation */
488 /* Also destroy the cloned device */
490 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tap), unit);
492 SLIST_REMOVE(&tap_listhead, sc, tap_softc, tap_link);
498 * Destroy a tap instance that is created via ifconfig.
501 tap_clone_destroy(struct ifnet *ifp)
503 struct tap_softc *sc = ifp->if_softc;
505 if (sc->tap_flags & TAP_OPEN)
507 if ((sc->tap_flags & TAP_CLONE) == 0)
510 TAPDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n",
511 minor(sc->tap_dev), sc->tap_flags);
521 * Network interface initialization function (called with if serializer held)
528 struct tap_softc *sc = xtp;
529 struct ifnet *ifp = &sc->tap_if;
530 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
532 TAPDEBUG(ifp, "initializing, minor = %#x, flags = 0x%x\n",
533 minor(sc->tap_dev), sc->tap_flags);
535 ASSERT_IFNET_SERIALIZED_ALL(ifp);
539 ifp->if_flags |= IFF_RUNNING;
540 ifsq_clr_oactive(ifsq);
542 /* attempt to start output */
543 tapifstart(ifp, ifsq);
547 tapifflags(struct tap_softc *sc)
549 struct ifnet *ifp = &sc->tap_if;
551 ASSERT_IFNET_SERIALIZED_ALL(ifp);
553 if (ifp->if_flags & IFF_UP) {
554 if ((ifp->if_flags & IFF_RUNNING) == 0)
564 * Process an ioctl request on network interface (called with if serializer
570 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
572 struct tap_softc *sc = ifp->if_softc;
573 struct ifreq *ifr = NULL;
574 struct ifstat *ifs = NULL;
575 struct ifmediareq *ifmr = NULL;
585 ifr = (struct ifreq *)data;
586 ifp->if_mtu = ifr->ifr_mtu;
587 TAPDEBUG(ifp, "mtu set\n");
592 error = ether_ioctl(ifp, cmd, data);
601 * The bridge code needs this when running the
602 * spanning tree protocol.
604 ifmr = (struct ifmediareq *)data;
605 dummy = ifmr->ifm_count;
607 ifmr->ifm_status = IFM_AVALID;
608 ifmr->ifm_active = IFM_ETHER;
609 if (sc->tap_flags & TAP_OPEN)
610 ifmr->ifm_status |= IFM_ACTIVE;
611 ifmr->ifm_current = ifmr->ifm_active;
613 int media = IFM_ETHER;
614 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
619 ifs = (struct ifstat *)data;
620 dummy = strlen(ifs->ascii);
621 if ((sc->tap_flags & TAP_OPEN) && dummy < sizeof(ifs->ascii)) {
622 if (sc->tap_sigtd && sc->tap_sigtd->sio_proc) {
623 ksnprintf(ifs->ascii + dummy,
624 sizeof(ifs->ascii) - dummy,
625 "\tOpened by pid %d\n",
626 (int)sc->tap_sigtd->sio_proc->p_pid);
628 ksnprintf(ifs->ascii + dummy,
629 sizeof(ifs->ascii) - dummy,
630 "\tOpened by <unknown>\n");
646 * Queue packets from higher level ready to put out (called with if serializer
652 tapifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
654 struct tap_softc *sc = ifp->if_softc;
659 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
660 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(sc->tap_dev));
662 if ((sc->tap_flags & TAP_READY) != TAP_READY) {
663 TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
664 minor(sc->tap_dev), sc->tap_flags);
669 ifsq_set_oactive(ifsq);
672 while ((m = ifsq_dequeue(ifsq)) != NULL) {
675 IFNET_STAT_INC(ifp, oerrors, 1);
679 IFNET_STAT_INC(ifp, opackets, 1);
685 if (sc->tap_flags & TAP_RWAIT) {
686 sc->tap_flags &= ~TAP_RWAIT;
690 KNOTE(&sc->tap_rkq.ki_note, 0);
692 if ((sc->tap_flags & TAP_ASYNC) && (sc->tap_sigio != NULL)) {
694 pgsigio(sc->tap_sigio, SIGIO, 0);
699 ifsq_clr_oactive(ifsq);
703 tapifstop(struct tap_softc *sc, int clear_flags)
705 struct ifnet *ifp = &sc->tap_if;
707 ASSERT_IFNET_SERIALIZED_ALL(ifp);
708 IF_DRAIN(&sc->tap_devq);
709 sc->tap_flags &= ~TAP_CLOSEDOWN;
711 ifp->if_flags &= ~IFF_RUNNING;
712 ifsq_clr_oactive(ifq_get_subq_default(&ifp->if_snd));
719 * The ops interface is now pretty minimal. Called via fileops with nothing
725 tapioctl(struct dev_ioctl_args *ap)
727 cdev_t dev = ap->a_head.a_dev;
728 caddr_t data = ap->a_data;
729 struct tap_softc *sc = dev->si_drv1;
730 struct ifnet *ifp = &sc->tap_if;
732 struct tapinfo *tapp = NULL;
736 ifnet_serialize_all(ifp);
741 tapp = (struct tapinfo *)data;
742 if (ifp->if_type != tapp->type)
744 ifp->if_mtu = tapp->mtu;
745 ifp->if_baudrate = tapp->baudrate;
749 tapp = (struct tapinfo *)data;
750 tapp->mtu = ifp->if_mtu;
751 tapp->type = ifp->if_type;
752 tapp->baudrate = ifp->if_baudrate;
756 ifr = (struct ifreq *)data;
757 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
761 tapdebug = *(int *)data;
765 *(int *)data = tapdebug;
770 sc->tap_flags |= TAP_ASYNC;
772 sc->tap_flags &= ~TAP_ASYNC;
776 /* Take a look at devq first */
777 IF_POLL(&sc->tap_devq, mb);
780 for(; mb != NULL; mb = mb->m_next)
781 *(int *)data += mb->m_len;
783 *(int *)data = ifsq_poll_pktlen(
784 ifq_get_subq_default(&ifp->if_snd));
789 error = fsetown(*(int *)data, &sc->tap_sigio);
791 error = fsetown(*(int *)data, &sc->tap_sigtd);
795 *(int *)data = fgetown(&sc->tap_sigio);
798 /* this is deprecated, FIOSETOWN should be used instead */
800 error = fsetown(-(*(int *)data), &sc->tap_sigio);
803 /* this is deprecated, FIOGETOWN should be used instead */
805 *(int *)data = -fgetown(&sc->tap_sigio);
809 * Support basic control of the network interface via the device file.
810 * e.g., vke(4) currently uses the 'SIOCGIFADDR' ioctl.
814 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
818 bcopy(sc->ether_addr, data, sizeof(sc->ether_addr));
822 bcopy(data, sc->ether_addr, sizeof(sc->ether_addr));
830 ifnet_deserialize_all(ifp);
838 * The ops read interface - reads a packet at a time, or at
839 * least as much of a packet as can be read.
841 * Called from the fileops interface with nothing held.
846 tapread(struct dev_read_args *ap)
848 cdev_t dev = ap->a_head.a_dev;
849 struct uio *uio = ap->a_uio;
850 struct tap_softc *sc = dev->si_drv1;
851 struct ifnet *ifp = &sc->tap_if;
852 struct mbuf *m0 = NULL;
855 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(dev));
857 if ((sc->tap_flags & TAP_READY) != TAP_READY) {
858 TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
859 minor(dev), sc->tap_flags);
864 sc->tap_flags &= ~TAP_RWAIT;
866 /* sleep until we get a packet */
868 ifnet_serialize_all(ifp);
869 IF_DEQUEUE(&sc->tap_devq, m0);
871 if (ap->a_ioflag & IO_NDELAY) {
872 ifnet_deserialize_all(ifp);
873 return (EWOULDBLOCK);
875 sc->tap_flags |= TAP_RWAIT;
876 tsleep_interlock(sc, PCATCH);
877 ifnet_deserialize_all(ifp);
878 error = tsleep(sc, PCATCH | PINTERLOCKED, "taprd", 0);
882 ifnet_deserialize_all(ifp);
884 } while (m0 == NULL);
888 /* xfer packet to user space */
889 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
890 len = (int)szmin(uio->uio_resid, m0->m_len);
894 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
899 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n", minor(dev));
909 * The ops write interface - an atomic write is a packet - or else!
911 * Called from the fileops interface with nothing held.
916 tapwrite(struct dev_write_args *ap)
918 cdev_t dev = ap->a_head.a_dev;
919 struct uio *uio = ap->a_uio;
920 struct tap_softc *sc = dev->si_drv1;
921 struct ifnet *ifp = &sc->tap_if;
922 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
926 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(dev));
928 if ((sc->tap_flags & TAP_READY) != TAP_READY) {
929 TAPDEBUG(ifp, "not ready, minor = %#x, flags = 0x%x\n",
930 minor(dev), sc->tap_flags);
934 if (uio->uio_resid == 0)
937 if (uio->uio_resid > TAPMRU) {
938 TAPDEBUG(ifp, "invalid packet len = %zu, minor = %#x\n",
939 uio->uio_resid, minor(dev));
943 tlen = uio->uio_resid;
945 /* get a header mbuf */
946 MGETHDR(m, M_WAITOK, MT_DATA);
955 while (error == 0 && uio->uio_resid > 0) {
956 m->m_len = (int)szmin(mlen, uio->uio_resid);
957 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
960 if (uio->uio_resid > 0) {
961 MGET(m, M_WAITOK, MT_DATA);
970 IFNET_STAT_INC(ifp, ierrors, 1);
976 top->m_pkthdr.len = (int)tlen;
977 top->m_pkthdr.rcvif = ifp;
980 * Ethernet bridge and bpf are handled in ether_input
982 * adjust mbuf and give packet to the ether_input
984 ifnet_serialize_all(ifp);
985 ifp->if_input(ifp, top, NULL, -1);
986 IFNET_STAT_INC(ifp, ipackets, 1); /* ibytes are counted in ether_input */
987 ifnet_deserialize_all(ifp);
994 * tapkqfilter - called from the fileops interface with nothing held
999 tapkqfilter(struct dev_kqfilter_args *ap)
1001 cdev_t dev = ap->a_head.a_dev;
1002 struct knote *kn = ap->a_kn;
1003 struct tap_softc *sc = dev->si_drv1;
1006 list = &sc->tap_rkq.ki_note;
1009 switch (kn->kn_filter) {
1011 kn->kn_fop = &tapread_filtops;
1012 kn->kn_hook = (void *)sc;
1015 kn->kn_fop = &tapwrite_filtops;
1016 kn->kn_hook = (void *)sc;
1019 ap->a_result = EOPNOTSUPP;
1023 knote_insert(list, kn);
1028 tap_filter_read(struct knote *kn, long hint)
1030 struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1032 if (IF_QEMPTY(&sc->tap_devq) == 0) /* XXX serializer */
1039 tap_filter_write(struct knote *kn, long hint)
1041 /* Always ready for a write */
1046 tap_filter_detach(struct knote *kn)
1048 struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1050 knote_remove(&sc->tap_rkq.ki_note, kn);