1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * -------------------------------------------------------------------------
34 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
35 * Nottingham University 1987.
37 * This source may be freely distributed, however I would be interested
38 * in any changes that are made.
40 * This driver takes packets off the IP i/f and hands them up to a
41 * user process to have its wicked way with. This driver has it's
42 * roots in a similar driver written by Phil Cockcroft (formerly) at
43 * UCL. This driver is based much more on read/write/poll mode of
50 #include "opt_inet6.h"
52 #include <sys/param.h>
56 #include <sys/systm.h>
59 #include <sys/module.h>
60 #include <sys/socket.h>
61 #include <sys/fcntl.h>
62 #include <sys/filio.h>
63 #include <sys/sockio.h>
65 #include <sys/syslog.h>
66 #include <sys/ttycom.h>
68 #include <sys/selinfo.h>
69 #include <sys/signalvar.h>
70 #include <sys/filedesc.h>
71 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
75 #include <sys/malloc.h>
76 #include <sys/random.h>
77 #include <sys/ctype.h>
79 #include <net/ethernet.h>
81 #include <net/if_var.h>
82 #include <net/if_clone.h>
83 #include <net/if_dl.h>
84 #include <net/if_media.h>
85 #include <net/if_types.h>
86 #include <net/netisr.h>
87 #include <net/route.h>
90 #include <netinet/in.h>
93 #include <net/if_tap.h>
94 #include <net/if_tun.h>
96 #include <sys/queue.h>
97 #include <sys/condvar.h>
98 #include <security/mac/mac_framework.h>
100 struct tuntap_driver;
103 * tun_list is protected by global tunmtx. Other mutable fields are
104 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
105 * static for the duration of a tunnel interface.
107 struct tuntap_softc {
108 TAILQ_ENTRY(tuntap_softc) tun_list;
109 struct cdev *tun_alias;
110 struct cdev *tun_dev;
111 u_short tun_flags; /* misc flags */
112 #define TUN_OPEN 0x0001
113 #define TUN_INITED 0x0002
114 #define TUN_UNUSED1 0x0008
115 #define TUN_DSTADDR 0x0010
116 #define TUN_LMODE 0x0020
117 #define TUN_RWAIT 0x0040
118 #define TUN_ASYNC 0x0080
119 #define TUN_IFHEAD 0x0100
120 #define TUN_DYING 0x0200
121 #define TUN_L2 0x0400
122 #define TUN_VMNET 0x0800
124 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
125 #define TUN_READY (TUN_OPEN | TUN_INITED)
127 pid_t tun_pid; /* owning pid */
128 struct ifnet *tun_ifp; /* the interface */
129 struct sigio *tun_sigio; /* async I/O info */
130 struct tuntap_driver *tun_drv; /* appropriate driver */
131 struct selinfo tun_rsel; /* read select */
132 struct mtx tun_mtx; /* softc field mutex */
133 struct cv tun_cv; /* for ref'd dev destroy */
134 struct ether_addr tun_ether; /* remote address */
135 int tun_busy; /* busy count */
137 #define TUN2IFP(sc) ((sc)->tun_ifp)
139 #define TUNDEBUG if (tundebug) if_printf
141 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
142 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
143 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
145 #define TUN_VMIO_FLAG_MASK 0x0fff
148 * All mutable global variables in if_tun are locked using tunmtx, with
149 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
150 * which are static after setup.
152 static struct mtx tunmtx;
153 static eventhandler_tag arrival_tag;
154 static eventhandler_tag clone_tag;
155 static const char tunname[] = "tun";
156 static const char tapname[] = "tap";
157 static const char vmnetname[] = "vmnet";
158 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
159 static int tundebug = 0;
160 static int tundclone = 1;
161 static int tap_allow_uopen = 0; /* allow user open() */
162 static int tapuponopen = 0; /* IFF_UP on open() */
163 static int tapdclone = 1; /* enable devfs cloning */
165 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
166 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
168 static struct sx tun_ioctl_sx;
169 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
171 SYSCTL_DECL(_net_link);
173 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
174 "IP tunnel software network interface");
175 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
176 "Enable legacy devfs interface creation");
179 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
180 "Ethernet tunnel software network interface");
181 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
182 "Allow user to open /dev/tap (based on node permissions)");
183 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
184 "Bring interface up when /dev/tap is opened");
185 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
186 "Enable legacy devfs interface creation");
187 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
189 static int tun_create_device(struct tuntap_driver *drv, int unit,
190 struct ucred *cr, struct cdev **dev, const char *name);
191 static int tun_busy_locked(struct tuntap_softc *tp);
192 static void tun_unbusy_locked(struct tuntap_softc *tp);
193 static int tun_busy(struct tuntap_softc *tp);
194 static void tun_unbusy(struct tuntap_softc *tp);
196 static int tuntap_name2info(const char *name, int *unit, int *flags);
197 static void tunclone(void *arg, struct ucred *cred, char *name,
198 int namelen, struct cdev **dev);
199 static void tuncreate(struct cdev *dev);
200 static void tundtor(void *data);
201 static void tunrename(void *arg, struct ifnet *ifp);
202 static int tunifioctl(struct ifnet *, u_long, caddr_t);
203 static void tuninit(struct ifnet *);
204 static void tunifinit(void *xtp);
205 static int tuntapmodevent(module_t, int, void *);
206 static int tunoutput(struct ifnet *, struct mbuf *,
207 const struct sockaddr *, struct route *ro);
208 static void tunstart(struct ifnet *);
209 static void tunstart_l2(struct ifnet *);
211 static int tun_clone_match(struct if_clone *ifc, const char *name);
212 static int tap_clone_match(struct if_clone *ifc, const char *name);
213 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
214 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
215 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
217 static d_open_t tunopen;
218 static d_read_t tunread;
219 static d_write_t tunwrite;
220 static d_ioctl_t tunioctl;
221 static d_poll_t tunpoll;
222 static d_kqfilter_t tunkqfilter;
224 static int tunkqread(struct knote *, long);
225 static int tunkqwrite(struct knote *, long);
226 static void tunkqdetach(struct knote *);
228 static struct filterops tun_read_filterops = {
231 .f_detach = tunkqdetach,
232 .f_event = tunkqread,
235 static struct filterops tun_write_filterops = {
238 .f_detach = tunkqdetach,
239 .f_event = tunkqwrite,
242 static struct tuntap_driver {
243 struct cdevsw cdevsw;
245 struct unrhdr *unrhdr;
246 struct clonedevs *clones;
247 ifc_match_t *clone_match_fn;
248 ifc_create_t *clone_create_fn;
249 ifc_destroy_t *clone_destroy_fn;
250 } tuntap_drivers[] = {
254 .d_version = D_VERSION,
255 .d_flags = D_NEEDMINOR,
261 .d_kqfilter = tunkqfilter,
264 .clone_match_fn = tun_clone_match,
265 .clone_create_fn = tun_clone_create,
266 .clone_destroy_fn = tun_clone_destroy,
269 .ident_flags = TUN_L2,
271 .d_version = D_VERSION,
272 .d_flags = D_NEEDMINOR,
278 .d_kqfilter = tunkqfilter,
281 .clone_match_fn = tap_clone_match,
282 .clone_create_fn = tun_clone_create,
283 .clone_destroy_fn = tun_clone_destroy,
286 .ident_flags = TUN_L2 | TUN_VMNET,
288 .d_version = D_VERSION,
289 .d_flags = D_NEEDMINOR,
295 .d_kqfilter = tunkqfilter,
298 .clone_match_fn = vmnet_clone_match,
299 .clone_create_fn = tun_clone_create,
300 .clone_destroy_fn = tun_clone_destroy,
304 struct tuntap_driver_cloner {
305 SLIST_ENTRY(tuntap_driver_cloner) link;
306 struct tuntap_driver *drv;
307 struct if_clone *cloner;
310 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
311 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
313 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
316 * Mechanism for marking a tunnel device as busy so that we can safely do some
317 * orthogonal operations (such as operations on devices) without racing against
318 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
319 * open, to be woken up when the condition is alleviated.
322 tun_busy_locked(struct tuntap_softc *tp)
326 if ((tp->tun_flags & TUN_DYING) != 0) {
328 * Perhaps unintuitive, but the device is busy going away.
329 * Other interpretations of EBUSY from tun_busy make little
330 * sense, since making a busy device even more busy doesn't
331 * sound like a problem.
341 tun_unbusy_locked(struct tuntap_softc *tp)
345 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
348 /* Wake up anything that may be waiting on our busy tunnel. */
349 if (tp->tun_busy == 0)
350 cv_broadcast(&tp->tun_cv);
354 tun_busy(struct tuntap_softc *tp)
359 ret = tun_busy_locked(tp);
366 tun_unbusy(struct tuntap_softc *tp)
370 tun_unbusy_locked(tp);
375 * Sets unit and/or flags given the device name. Must be called with correct
379 tuntap_name2info(const char *name, int *outunit, int *outflags)
381 struct tuntap_driver *drv;
382 struct tuntap_driver_cloner *drvc;
391 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
392 * wants to be able to pass back a char * through the second param. We
393 * will always set that as NULL here, so we'll fake it.
395 dname = __DECONST(char *, name);
398 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
399 ("tuntap_driver_cloners failed to initialize"));
400 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
401 KASSERT(drvc->drv != NULL,
402 ("tuntap_driver_cloners entry not properly initialized"));
405 if (strcmp(name, drv->cdevsw.d_name) == 0) {
408 flags = drv->ident_flags;
412 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
414 flags = drv->ident_flags;
424 if (outflags != NULL)
430 * Get driver information from a set of flags specified. Masks the identifying
431 * part of the flags and compares it against all of the available
432 * tuntap_drivers. Must be called with correct vnet context.
434 static struct tuntap_driver *
435 tuntap_driver_from_flags(int tun_flags)
437 struct tuntap_driver *drv;
438 struct tuntap_driver_cloner *drvc;
440 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
441 ("tuntap_driver_cloners failed to initialize"));
442 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
443 KASSERT(drvc->drv != NULL,
444 ("tuntap_driver_cloners entry not properly initialized"));
446 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
456 tun_clone_match(struct if_clone *ifc, const char *name)
460 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
461 if ((tunflags & TUN_L2) == 0)
469 tap_clone_match(struct if_clone *ifc, const char *name)
473 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
474 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
482 vmnet_clone_match(struct if_clone *ifc, const char *name)
486 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
487 if ((tunflags & TUN_VMNET) != 0)
495 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
497 struct tuntap_driver *drv;
499 int err, i, tunflags, unit;
502 /* The name here tells us exactly what we're creating */
503 err = tuntap_name2info(name, &unit, &tunflags);
507 drv = tuntap_driver_from_flags(tunflags);
512 /* If this unit number is still available that's okay. */
513 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
516 unit = alloc_unr(drv->unrhdr);
519 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
521 /* find any existing device, or allocate new unit number */
523 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
524 /* No preexisting struct cdev *, create one */
526 i = tun_create_device(drv, unit, NULL, &dev, name);
534 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
537 char devname[SPECNAMELEN + 1];
538 struct tuntap_driver *drv;
539 int append_unit, i, u, tunflags;
546 CURVNET_SET(CRED_TO_VNET(cred));
547 if (tuntap_name2info(name, &u, &tunflags) != 0)
548 goto out; /* Not recognized */
550 if (u != -1 && u > IF_MAXUNIT)
551 goto out; /* Unit number too high */
553 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE, 0) == 0;
554 if ((tunflags & TUN_L2) != 0) {
555 /* tap/vmnet allow user open with a sysctl */
556 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
558 mayclone = mayclone && tundclone;
562 * If tun cloning is enabled, only the superuser can create an
573 drv = tuntap_driver_from_flags(tunflags);
577 /* find any existing device, or allocate new unit number */
578 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
581 namelen = snprintf(devname, sizeof(devname), "%s%d",
586 i = tun_create_device(drv, u, cred, dev, name);
589 if_clone_create(name, namelen, NULL);
595 tun_destroy(struct tuntap_softc *tp)
599 tp->tun_flags |= TUN_DYING;
600 if (tp->tun_busy != 0)
601 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
605 CURVNET_SET(TUN2IFP(tp)->if_vnet);
607 /* destroy_dev will take care of any alias. */
608 destroy_dev(tp->tun_dev);
609 seldrain(&tp->tun_rsel);
610 knlist_clear(&tp->tun_rsel.si_note, 0);
611 knlist_destroy(&tp->tun_rsel.si_note);
612 if ((tp->tun_flags & TUN_L2) != 0) {
613 ether_ifdetach(TUN2IFP(tp));
615 bpfdetach(TUN2IFP(tp));
616 if_detach(TUN2IFP(tp));
618 sx_xlock(&tun_ioctl_sx);
619 TUN2IFP(tp)->if_softc = NULL;
620 sx_xunlock(&tun_ioctl_sx);
621 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
622 if_free(TUN2IFP(tp));
623 mtx_destroy(&tp->tun_mtx);
624 cv_destroy(&tp->tun_cv);
630 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
632 struct tuntap_softc *tp = ifp->if_softc;
635 TAILQ_REMOVE(&tunhead, tp, tun_list);
643 vnet_tun_init(const void *unused __unused)
645 struct tuntap_driver *drv;
646 struct tuntap_driver_cloner *drvc;
649 for (i = 0; i < nitems(tuntap_drivers); ++i) {
650 drv = &tuntap_drivers[i];
651 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
654 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
655 drv->clone_match_fn, drv->clone_create_fn,
656 drv->clone_destroy_fn);
657 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
660 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
661 vnet_tun_init, NULL);
664 vnet_tun_uninit(const void *unused __unused)
666 struct tuntap_driver_cloner *drvc;
668 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
669 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
670 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
672 if_clone_detach(drvc->cloner);
676 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
677 vnet_tun_uninit, NULL);
680 tun_uninit(const void *unused __unused)
682 struct tuntap_driver *drv;
683 struct tuntap_softc *tp;
686 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
687 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
688 drain_dev_clone_events();
691 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
692 TAILQ_REMOVE(&tunhead, tp, tun_list);
698 for (i = 0; i < nitems(tuntap_drivers); ++i) {
699 drv = &tuntap_drivers[i];
700 delete_unrhdr(drv->unrhdr);
701 clone_cleanup(&drv->clones);
703 mtx_destroy(&tunmtx);
705 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
707 static struct tuntap_driver *
708 tuntap_driver_from_ifnet(const struct ifnet *ifp)
710 struct tuntap_driver *drv;
716 for (i = 0; i < nitems(tuntap_drivers); ++i) {
717 drv = &tuntap_drivers[i];
718 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
726 tuntapmodevent(module_t mod, int type, void *data)
728 struct tuntap_driver *drv;
733 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
734 for (i = 0; i < nitems(tuntap_drivers); ++i) {
735 drv = &tuntap_drivers[i];
736 clone_setup(&drv->clones);
737 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
739 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
741 if (arrival_tag == NULL)
743 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
744 if (clone_tag == NULL)
748 /* See tun_uninit, so it's done after the vnet_sysuninit() */
756 static moduledata_t tuntap_mod = {
762 /* We'll only ever have these two, so no need for a macro. */
763 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
764 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
766 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
767 MODULE_VERSION(if_tuntap, 1);
768 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
769 MODULE_VERSION(if_tun, 1);
770 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
771 MODULE_VERSION(if_tap, 1);
774 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
775 struct cdev **dev, const char *name)
777 struct make_dev_args args;
778 struct tuntap_softc *tp;
781 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
782 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
783 cv_init(&tp->tun_cv, "tun_condvar");
784 tp->tun_flags = drv->ident_flags;
787 make_dev_args_init(&args);
789 args.mda_flags = MAKEDEV_REF;
790 args.mda_devsw = &drv->cdevsw;
792 args.mda_uid = UID_UUCP;
793 args.mda_gid = GID_DIALER;
794 args.mda_mode = 0600;
795 args.mda_unit = unit;
796 args.mda_si_drv1 = tp;
797 error = make_dev_s(&args, dev, "%s", name);
803 KASSERT((*dev)->si_drv1 != NULL,
804 ("Failed to set si_drv1 at %s creation", name));
806 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
808 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
814 tunstart(struct ifnet *ifp)
816 struct tuntap_softc *tp = ifp->if_softc;
819 TUNDEBUG(ifp, "starting\n");
820 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
821 IFQ_LOCK(&ifp->if_snd);
822 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
824 IFQ_UNLOCK(&ifp->if_snd);
827 IFQ_UNLOCK(&ifp->if_snd);
831 if (tp->tun_flags & TUN_RWAIT) {
832 tp->tun_flags &= ~TUN_RWAIT;
835 selwakeuppri(&tp->tun_rsel, PZERO + 1);
836 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
837 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
839 pgsigio(&tp->tun_sigio, SIGIO, 0);
847 * queue packets from higher level ready to put out
850 tunstart_l2(struct ifnet *ifp)
852 struct tuntap_softc *tp = ifp->if_softc;
854 TUNDEBUG(ifp, "starting\n");
857 * do not junk pending output if we are in VMnet mode.
858 * XXX: can this do any harm because of queue overflow?
862 if (((tp->tun_flags & TUN_VMNET) == 0) &&
863 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
867 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
870 IF_DEQUEUE(&ifp->if_snd, m);
873 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
882 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
884 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
885 if (tp->tun_flags & TUN_RWAIT) {
886 tp->tun_flags &= ~TUN_RWAIT;
890 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
892 pgsigio(&tp->tun_sigio, SIGIO, 0);
896 selwakeuppri(&tp->tun_rsel, PZERO+1);
897 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
898 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
901 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
905 /* XXX: should return an error code so it can fail. */
907 tuncreate(struct cdev *dev)
909 struct tuntap_driver *drv;
910 struct tuntap_softc *tp;
912 struct ether_addr eaddr;
918 ("si_drv1 should have been initialized at creation"));
921 iflags = IFF_MULTICAST;
922 if ((tp->tun_flags & TUN_L2) != 0) {
924 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
927 iflags |= IFF_POINTOPOINT;
929 ifp = tp->tun_ifp = if_alloc(type);
931 panic("%s%d: failed to if_alloc() interface.\n",
932 drv->cdevsw.d_name, dev2unit(dev));
934 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
935 ifp->if_ioctl = tunifioctl;
936 ifp->if_flags = iflags;
937 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
938 ifp->if_capabilities |= IFCAP_LINKSTATE;
939 ifp->if_capenable |= IFCAP_LINKSTATE;
941 if ((tp->tun_flags & TUN_L2) != 0) {
942 ifp->if_init = tunifinit;
943 ifp->if_start = tunstart_l2;
945 ether_gen_addr(ifp, &eaddr);
946 ether_ifattach(ifp, eaddr.octet);
948 ifp->if_mtu = TUNMTU;
949 ifp->if_start = tunstart;
950 ifp->if_output = tunoutput;
952 ifp->if_snd.ifq_drv_maxlen = 0;
953 IFQ_SET_READY(&ifp->if_snd);
956 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
960 tp->tun_flags |= TUN_INITED;
963 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
964 ifp->if_xname, dev2unit(dev));
968 tunrename(void *arg __unused, struct ifnet *ifp)
970 struct tuntap_softc *tp;
973 if ((ifp->if_flags & IFF_RENAMING) == 0)
976 if (tuntap_driver_from_ifnet(ifp) == NULL)
980 * We need to grab the ioctl sx long enough to make sure the softc is
981 * still there. If it is, we can safely try to busy the tun device.
982 * The busy may fail if the device is currently dying, in which case
983 * we do nothing. If it doesn't fail, the busy count stops the device
984 * from dying until we've created the alias (that will then be
985 * subsequently destroyed).
987 sx_xlock(&tun_ioctl_sx);
990 sx_xunlock(&tun_ioctl_sx);
993 error = tun_busy(tp);
994 sx_xunlock(&tun_ioctl_sx);
997 if (tp->tun_alias != NULL) {
998 destroy_dev(tp->tun_alias);
999 tp->tun_alias = NULL;
1002 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1006 * Failure's ok, aliases are created on a best effort basis. If a
1007 * tun user/consumer decides to rename the interface to conflict with
1008 * another device (non-ifnet) on the system, we will assume they know
1009 * what they are doing. make_dev_alias_p won't touch tun_alias on
1010 * failure, so we use it but ignore the return value.
1012 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1019 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1022 struct tuntap_softc *tp;
1023 int error, tunflags;
1026 CURVNET_SET(TD_TO_VNET(td));
1027 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1030 return (error); /* Shouldn't happen */
1033 if ((tunflags & TUN_L2) != 0) {
1035 if (tap_allow_uopen == 0) {
1036 error = priv_check(td, PRIV_NET_TAP);
1046 ("si_drv1 should have been initialized at creation"));
1049 if ((tp->tun_flags & TUN_INITED) == 0) {
1054 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1060 error = tun_busy_locked(tp);
1061 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1064 if ((tp->tun_flags & TUN_L2) != 0) {
1065 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1066 sizeof(tp->tun_ether.octet));
1068 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1069 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1072 ifp->if_flags |= IFF_UP;
1075 tp->tun_pid = td->td_proc->p_pid;
1076 tp->tun_flags |= TUN_OPEN;
1078 if_link_state_change(ifp, LINK_STATE_UP);
1079 TUNDEBUG(ifp, "open\n");
1083 * This can fail with either ENOENT or EBUSY. This is in the middle of
1084 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1085 * the only cdevpriv dtor being set will be tundtor and the softc being
1086 * passed is constant for a given cdev. We ignore the possible error
1087 * because of this as either "unlikely" or "not actually a problem."
1089 (void)devfs_set_cdevpriv(tp, tundtor);
1095 * tundtor - tear down the device - mark i/f down & delete
1102 struct tuntap_softc *tp;
1113 * Realistically, we can't be obstinate here. This only means that the
1114 * tuntap device was closed out of order, and the last closer wasn't the
1115 * controller. These are still good to know about, though, as software
1116 * should avoid multiple processes with a tuntap device open and
1117 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1120 if (p->p_pid != tp->tun_pid) {
1122 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1123 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1127 * junk all pending output
1129 CURVNET_SET(ifp->if_vnet);
1132 if ((tp->tun_flags & TUN_L2) != 0) {
1134 IF_DRAIN(&ifp->if_snd);
1136 IFQ_PURGE(&ifp->if_snd);
1139 /* For vmnet, we won't do most of the address/route bits */
1140 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1141 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1144 if (ifp->if_flags & IFF_UP) {
1150 /* Delete all addresses and routes which reference this interface. */
1151 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1154 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1156 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1157 /* deal w/IPv4 PtP destination; unlocked read */
1158 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1159 rtinit(ifa, (int)RTM_DELETE,
1160 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1162 rtinit(ifa, (int)RTM_DELETE, 0);
1170 if_link_state_change(ifp, LINK_STATE_DOWN);
1173 funsetown(&tp->tun_sigio);
1174 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1175 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1176 TUNDEBUG (ifp, "closed\n");
1177 tp->tun_flags &= ~TUN_OPEN;
1180 tun_unbusy_locked(tp);
1185 tuninit(struct ifnet *ifp)
1187 struct tuntap_softc *tp = ifp->if_softc;
1192 TUNDEBUG(ifp, "tuninit\n");
1195 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1196 if ((tp->tun_flags & TUN_L2) == 0) {
1197 ifp->if_flags |= IFF_UP;
1198 getmicrotime(&ifp->if_lastchange);
1201 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1202 if (ifa->ifa_addr->sa_family == AF_INET) {
1203 struct sockaddr_in *si;
1205 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1206 if (si && si->sin_addr.s_addr) {
1207 tp->tun_flags |= TUN_DSTADDR;
1212 if_addr_runlock(ifp);
1216 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1218 /* attempt to start output */
1225 * Used only for l2 tunnel.
1228 tunifinit(void *xtp)
1230 struct tuntap_softc *tp;
1232 tp = (struct tuntap_softc *)xtp;
1233 tuninit(tp->tun_ifp);
1237 * Process an ioctl request.
1240 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1242 struct ifreq *ifr = (struct ifreq *)data;
1243 struct tuntap_softc *tp;
1245 struct ifmediareq *ifmr;
1246 int dummy, error = 0;
1250 sx_xlock(&tun_ioctl_sx);
1256 l2tun = (tp->tun_flags & TUN_L2) != 0;
1259 ifs = (struct ifstat *)data;
1262 snprintf(ifs->ascii, sizeof(ifs->ascii),
1263 "\tOpened by PID %d\n", tp->tun_pid);
1265 ifs->ascii[0] = '\0';
1270 error = ether_ioctl(ifp, cmd, data);
1274 TUNDEBUG(ifp, "address set\n");
1277 ifp->if_mtu = ifr->ifr_mtu;
1278 TUNDEBUG(ifp, "mtu set\n");
1290 ifmr = (struct ifmediareq *)data;
1291 dummy = ifmr->ifm_count;
1292 ifmr->ifm_count = 1;
1293 ifmr->ifm_status = IFM_AVALID;
1294 ifmr->ifm_active = IFM_ETHER;
1295 if (tp->tun_flags & TUN_OPEN)
1296 ifmr->ifm_status |= IFM_ACTIVE;
1297 ifmr->ifm_current = ifmr->ifm_active;
1299 int media = IFM_ETHER;
1300 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1305 error = ether_ioctl(ifp, cmd, data);
1311 sx_xunlock(&tun_ioctl_sx);
1316 * tunoutput - queue packets from higher level ready to put out.
1319 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1322 struct tuntap_softc *tp = ifp->if_softc;
1323 u_short cached_tun_flags;
1327 TUNDEBUG (ifp, "tunoutput\n");
1330 error = mac_ifnet_check_transmit(ifp, m0);
1337 /* Could be unlocked read? */
1339 cached_tun_flags = tp->tun_flags;
1341 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1342 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1347 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1352 /* BPF writes need to be handled specially. */
1353 if (dst->sa_family == AF_UNSPEC)
1354 bcopy(dst->sa_data, &af, sizeof(af));
1356 af = dst->sa_family;
1358 if (bpf_peers_present(ifp->if_bpf))
1359 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1361 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1362 if (cached_tun_flags & TUN_LMODE) {
1363 /* allocate space for sockaddr */
1364 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1366 /* if allocation failed drop packet */
1368 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1369 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1372 bcopy(dst, m0->m_data, dst->sa_len);
1376 if (cached_tun_flags & TUN_IFHEAD) {
1377 /* Prepend the address family */
1378 M_PREPEND(m0, 4, M_NOWAIT);
1380 /* if allocation failed drop packet */
1382 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1383 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1386 *(u_int32_t *)m0->m_data = htonl(af);
1393 return (EAFNOSUPPORT);
1397 error = (ifp->if_transmit)(ifp, m0);
1400 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1405 * the cdevsw interface is now pretty minimal.
1408 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1411 struct ifreq ifr, *ifrp;
1412 struct tuntap_softc *tp = dev->si_drv1;
1413 struct tuninfo *tunp;
1415 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1416 defined(COMPAT_FREEBSD4)
1421 l2tun = (tp->tun_flags & TUN_L2) != 0;
1423 /* tap specific ioctls */
1425 /* VMware/VMnet port ioctl's */
1426 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1427 defined(COMPAT_FREEBSD4)
1429 ival = IOCPARM_IVAL(data);
1430 data = (caddr_t)&ival;
1433 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1434 iflags = *(int *)data;
1435 iflags &= TUN_VMIO_FLAG_MASK;
1436 iflags &= ~IFF_CANTCHANGE;
1440 TUN2IFP(tp)->if_flags = iflags |
1441 (TUN2IFP(tp)->if_flags & IFF_CANTCHANGE);
1445 case SIOCGIFADDR: /* get MAC address of the remote side */
1447 bcopy(&tp->tun_ether.octet, data,
1448 sizeof(tp->tun_ether.octet));
1452 case SIOCSIFADDR: /* set MAC address of the remote side */
1454 bcopy(data, &tp->tun_ether.octet,
1455 sizeof(tp->tun_ether.octet));
1461 /* Fall through to the common ioctls if unhandled */
1467 tp->tun_flags |= TUN_LMODE;
1468 tp->tun_flags &= ~TUN_IFHEAD;
1470 tp->tun_flags &= ~TUN_LMODE;
1477 tp->tun_flags |= TUN_IFHEAD;
1478 tp->tun_flags &= ~TUN_LMODE;
1480 tp->tun_flags &= ~TUN_IFHEAD;
1486 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1491 /* deny this if UP */
1492 if (TUN2IFP(tp)->if_flags & IFF_UP)
1495 switch (*(int *)data & ~IFF_MULTICAST) {
1496 case IFF_POINTOPOINT:
1499 TUN2IFP(tp)->if_flags &=
1500 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1501 TUN2IFP(tp)->if_flags |= *(int *)data;
1512 tp->tun_pid = curthread->td_proc->p_pid;
1517 /* Fall through to the common ioctls if unhandled */
1522 ifrp = (struct ifreq *)data;
1523 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1527 tunp = (struct tuninfo *)data;
1528 if (TUN2IFP(tp)->if_type != tunp->type)
1529 return (EPROTOTYPE);
1531 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1532 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1533 ifr.ifr_mtu = tunp->mtu;
1534 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1535 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1543 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1547 tunp = (struct tuninfo *)data;
1549 tunp->mtu = TUN2IFP(tp)->if_mtu;
1550 tunp->type = TUN2IFP(tp)->if_type;
1551 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1555 tundebug = *(int *)data;
1558 *(int *)data = tundebug;
1565 tp->tun_flags |= TUN_ASYNC;
1567 tp->tun_flags &= ~TUN_ASYNC;
1571 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1573 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1574 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1575 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1576 *(int *)data += mb->m_len;
1577 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1582 return (fsetown(*(int *)data, &tp->tun_sigio));
1585 *(int *)data = fgetown(&tp->tun_sigio);
1588 /* This is deprecated, FIOSETOWN should be used instead. */
1590 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1592 /* This is deprecated, FIOGETOWN should be used instead. */
1594 *(int *)data = -fgetown(&tp->tun_sigio);
1604 * The cdevsw read interface - reads a packet at a time, or at
1605 * least as much of a packet as can be read.
1608 tunread(struct cdev *dev, struct uio *uio, int flag)
1610 struct tuntap_softc *tp = dev->si_drv1;
1611 struct ifnet *ifp = TUN2IFP(tp);
1615 TUNDEBUG (ifp, "read\n");
1617 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1619 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1623 tp->tun_flags &= ~TUN_RWAIT;
1626 IFQ_DEQUEUE(&ifp->if_snd, m);
1629 if (flag & O_NONBLOCK) {
1631 return (EWOULDBLOCK);
1633 tp->tun_flags |= TUN_RWAIT;
1634 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1643 if ((tp->tun_flags & TUN_L2) != 0)
1646 while (m && uio->uio_resid > 0 && error == 0) {
1647 len = min(uio->uio_resid, m->m_len);
1649 error = uiomove(mtod(m, void *), len, uio);
1654 TUNDEBUG(ifp, "Dropping mbuf\n");
1661 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m)
1663 struct ether_header *eh;
1669 * Only pass a unicast frame to ether_input(), if it would
1670 * actually have been received by non-virtual hardware.
1672 if (m->m_len < sizeof(struct ether_header)) {
1677 eh = mtod(m, struct ether_header *);
1679 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1680 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1681 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1686 /* Pass packet up to parent. */
1687 CURVNET_SET(ifp->if_vnet);
1688 (*ifp->if_input)(ifp, m);
1690 /* ibytes are counted in parent */
1691 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1696 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1702 /* Could be unlocked read? */
1704 if (tp->tun_flags & TUN_IFHEAD) {
1706 if (m->m_len < sizeof(family) &&
1707 (m = m_pullup(m, sizeof(family))) == NULL)
1709 family = ntohl(*mtod(m, u_int32_t *));
1710 m_adj(m, sizeof(family));
1716 BPF_MTAP2(ifp, &family, sizeof(family), m);
1731 return (EAFNOSUPPORT);
1733 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1734 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1735 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1736 CURVNET_SET(ifp->if_vnet);
1737 M_SETFIB(m, ifp->if_fib);
1738 netisr_dispatch(isr, m);
1744 * the cdevsw write interface - an atomic write is a packet - or else!
1747 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1749 struct tuntap_softc *tp;
1758 TUNDEBUG(ifp, "tunwrite\n");
1759 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1760 /* ignore silently */
1763 if (uio->uio_resid == 0)
1766 l2tun = (tp->tun_flags & TUN_L2) != 0;
1768 mru = l2tun ? TAPMRU : TUNMRU;
1770 align = ETHER_ALIGN;
1771 else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1772 mru += sizeof(uint32_t); /* family */
1773 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1774 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1778 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1779 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1783 m->m_pkthdr.rcvif = ifp;
1785 mac_ifnet_create_mbuf(ifp, m);
1789 return (tunwrite_l2(tp, m));
1791 return (tunwrite_l3(tp, m));
1795 * tunpoll - the poll interface, this is only useful on reads
1796 * really. The write detect always returns true, write never blocks
1797 * anyway, it either accepts the packet or drops it.
1800 tunpoll(struct cdev *dev, int events, struct thread *td)
1802 struct tuntap_softc *tp = dev->si_drv1;
1803 struct ifnet *ifp = TUN2IFP(tp);
1806 TUNDEBUG(ifp, "tunpoll\n");
1808 if (events & (POLLIN | POLLRDNORM)) {
1809 IFQ_LOCK(&ifp->if_snd);
1810 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1811 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1812 revents |= events & (POLLIN | POLLRDNORM);
1814 TUNDEBUG(ifp, "tunpoll waiting\n");
1815 selrecord(td, &tp->tun_rsel);
1817 IFQ_UNLOCK(&ifp->if_snd);
1819 revents |= events & (POLLOUT | POLLWRNORM);
1825 * tunkqfilter - support for the kevent() system call.
1828 tunkqfilter(struct cdev *dev, struct knote *kn)
1830 struct tuntap_softc *tp = dev->si_drv1;
1831 struct ifnet *ifp = TUN2IFP(tp);
1833 switch(kn->kn_filter) {
1835 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1836 ifp->if_xname, dev2unit(dev));
1837 kn->kn_fop = &tun_read_filterops;
1841 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1842 ifp->if_xname, dev2unit(dev));
1843 kn->kn_fop = &tun_write_filterops;
1847 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1848 ifp->if_xname, dev2unit(dev));
1853 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1859 * Return true of there is data in the interface queue.
1862 tunkqread(struct knote *kn, long hint)
1865 struct tuntap_softc *tp = kn->kn_hook;
1866 struct cdev *dev = tp->tun_dev;
1867 struct ifnet *ifp = TUN2IFP(tp);
1869 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1871 "%s have data in the queue. Len = %d, minor = %#x\n",
1872 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1876 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1885 * Always can write, always return MTU in kn->data.
1888 tunkqwrite(struct knote *kn, long hint)
1890 struct tuntap_softc *tp = kn->kn_hook;
1891 struct ifnet *ifp = TUN2IFP(tp);
1893 kn->kn_data = ifp->if_mtu;
1899 tunkqdetach(struct knote *kn)
1901 struct tuntap_softc *tp = kn->kn_hook;
1903 knlist_remove(&tp->tun_rsel.si_note, kn, 0);