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/eventhandler.h>
62 #include <sys/fcntl.h>
63 #include <sys/filio.h>
64 #include <sys/sockio.h>
66 #include <sys/syslog.h>
67 #include <sys/ttycom.h>
69 #include <sys/selinfo.h>
70 #include <sys/signalvar.h>
71 #include <sys/filedesc.h>
72 #include <sys/kernel.h>
73 #include <sys/sysctl.h>
76 #include <sys/malloc.h>
77 #include <sys/random.h>
78 #include <sys/ctype.h>
80 #include <net/ethernet.h>
82 #include <net/if_var.h>
83 #include <net/if_clone.h>
84 #include <net/if_dl.h>
85 #include <net/if_media.h>
86 #include <net/if_types.h>
87 #include <net/netisr.h>
88 #include <net/route.h>
91 #include <netinet/in.h>
94 #include <net/if_tap.h>
95 #include <net/if_tun.h>
97 #include <sys/queue.h>
98 #include <sys/condvar.h>
99 #include <security/mac/mac_framework.h>
101 struct tuntap_driver;
104 * tun_list is protected by global tunmtx. Other mutable fields are
105 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
106 * static for the duration of a tunnel interface.
108 struct tuntap_softc {
109 TAILQ_ENTRY(tuntap_softc) tun_list;
110 struct cdev *tun_alias;
111 struct cdev *tun_dev;
112 u_short tun_flags; /* misc flags */
113 #define TUN_OPEN 0x0001
114 #define TUN_INITED 0x0002
115 #define TUN_IASET 0x0008
116 #define TUN_DSTADDR 0x0010
117 #define TUN_LMODE 0x0020
118 #define TUN_RWAIT 0x0040
119 #define TUN_ASYNC 0x0080
120 #define TUN_IFHEAD 0x0100
121 #define TUN_DYING 0x0200
122 #define TUN_L2 0x0400
123 #define TUN_VMNET 0x0800
125 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
126 #define TUN_READY (TUN_OPEN | TUN_INITED)
128 pid_t tun_pid; /* owning pid */
129 struct ifnet *tun_ifp; /* the interface */
130 struct sigio *tun_sigio; /* async I/O info */
131 struct tuntap_driver *tun_drv; /* appropriate driver */
132 struct selinfo tun_rsel; /* read select */
133 struct mtx tun_mtx; /* softc field mutex */
134 struct cv tun_cv; /* for ref'd dev destroy */
135 struct ether_addr tun_ether; /* remote address */
136 int tun_busy; /* busy count */
138 #define TUN2IFP(sc) ((sc)->tun_ifp)
140 #define TUNDEBUG if (tundebug) if_printf
142 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
143 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
144 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
146 #define TUN_VMIO_FLAG_MASK 0x0fff
149 * All mutable global variables in if_tun are locked using tunmtx, with
150 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
151 * which are static after setup.
153 static struct mtx tunmtx;
154 static eventhandler_tag arrival_tag;
155 static eventhandler_tag clone_tag;
156 static const char tunname[] = "tun";
157 static const char tapname[] = "tap";
158 static const char vmnetname[] = "vmnet";
159 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
160 static int tundebug = 0;
161 static int tundclone = 1;
162 static int tap_allow_uopen = 0; /* allow user open() */
163 static int tapuponopen = 0; /* IFF_UP on open() */
164 static int tapdclone = 1; /* enable devfs cloning */
166 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
167 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
169 static struct sx tun_ioctl_sx;
170 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
172 SYSCTL_DECL(_net_link);
174 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
175 "IP tunnel software network interface");
176 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
177 "Enable legacy devfs interface creation");
180 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
181 "Ethernet tunnel software network interface");
182 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
183 "Allow user to open /dev/tap (based on node permissions)");
184 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
185 "Bring interface up when /dev/tap is opened");
186 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
187 "Enable legacy devfs interface creation");
188 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
190 static int tun_busy_locked(struct tuntap_softc *tp);
191 static void tun_unbusy_locked(struct tuntap_softc *tp);
192 static int tun_busy(struct tuntap_softc *tp);
193 static void tun_unbusy(struct tuntap_softc *tp);
195 static int tuntap_name2info(const char *name, int *unit, int *flags);
196 static void tunclone(void *arg, struct ucred *cred, char *name,
197 int namelen, struct cdev **dev);
198 static void tuncreate(struct cdev *dev, struct tuntap_driver *);
199 static void tunrename(void *arg, struct ifnet *ifp);
200 static int tunifioctl(struct ifnet *, u_long, caddr_t);
201 static void tuninit(struct ifnet *);
202 static void tunifinit(void *xtp);
203 static int tuntapmodevent(module_t, int, void *);
204 static int tunoutput(struct ifnet *, struct mbuf *,
205 const struct sockaddr *, struct route *ro);
206 static void tunstart(struct ifnet *);
207 static void tunstart_l2(struct ifnet *);
209 static int tun_clone_match(struct if_clone *ifc, const char *name);
210 static int tap_clone_match(struct if_clone *ifc, const char *name);
211 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
212 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
213 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
215 static d_open_t tunopen;
216 static d_close_t tunclose;
217 static d_read_t tunread;
218 static d_write_t tunwrite;
219 static d_ioctl_t tunioctl;
220 static d_poll_t tunpoll;
221 static d_kqfilter_t tunkqfilter;
223 static int tunkqread(struct knote *, long);
224 static int tunkqwrite(struct knote *, long);
225 static void tunkqdetach(struct knote *);
227 static struct filterops tun_read_filterops = {
230 .f_detach = tunkqdetach,
231 .f_event = tunkqread,
234 static struct filterops tun_write_filterops = {
237 .f_detach = tunkqdetach,
238 .f_event = tunkqwrite,
241 static struct tuntap_driver {
242 struct cdevsw cdevsw;
244 struct unrhdr *unrhdr;
245 struct clonedevs *clones;
246 ifc_match_t *clone_match_fn;
247 ifc_create_t *clone_create_fn;
248 ifc_destroy_t *clone_destroy_fn;
249 } tuntap_drivers[] = {
253 .d_version = D_VERSION,
254 .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,
279 .d_kqfilter = tunkqfilter,
282 .clone_match_fn = tap_clone_match,
283 .clone_create_fn = tun_clone_create,
284 .clone_destroy_fn = tun_clone_destroy,
287 .ident_flags = TUN_L2 | TUN_VMNET,
289 .d_version = D_VERSION,
290 .d_flags = D_NEEDMINOR,
297 .d_kqfilter = tunkqfilter,
300 .clone_match_fn = vmnet_clone_match,
301 .clone_create_fn = tun_clone_create,
302 .clone_destroy_fn = tun_clone_destroy,
306 struct tuntap_driver_cloner {
307 SLIST_ENTRY(tuntap_driver_cloner) link;
308 struct tuntap_driver *drv;
309 struct if_clone *cloner;
312 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
313 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
315 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
318 * Mechanism for marking a tunnel device as busy so that we can safely do some
319 * orthogonal operations (such as operations on devices) without racing against
320 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
321 * open, to be woken up when the condition is alleviated.
324 tun_busy_locked(struct tuntap_softc *tp)
328 if ((tp->tun_flags & TUN_DYING) != 0) {
330 * Perhaps unintuitive, but the device is busy going away.
331 * Other interpretations of EBUSY from tun_busy make little
332 * sense, since making a busy device even more busy doesn't
333 * sound like a problem.
343 tun_unbusy_locked(struct tuntap_softc *tp)
347 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
350 /* Wake up anything that may be waiting on our busy tunnel. */
351 if (tp->tun_busy == 0)
352 cv_broadcast(&tp->tun_cv);
356 tun_busy(struct tuntap_softc *tp)
361 ret = tun_busy_locked(tp);
368 tun_unbusy(struct tuntap_softc *tp)
372 tun_unbusy_locked(tp);
377 * Sets unit and/or flags given the device name. Must be called with correct
381 tuntap_name2info(const char *name, int *outunit, int *outflags)
383 struct tuntap_driver *drv;
384 struct tuntap_driver_cloner *drvc;
393 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
394 * wants to be able to pass back a char * through the second param. We
395 * will always set that as NULL here, so we'll fake it.
397 dname = __DECONST(char *, name);
400 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
401 ("tuntap_driver_cloners failed to initialize"));
402 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
403 KASSERT(drvc->drv != NULL,
404 ("tuntap_driver_cloners entry not properly initialized"));
407 if (strcmp(name, drv->cdevsw.d_name) == 0) {
410 flags = drv->ident_flags;
414 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
416 flags = drv->ident_flags;
426 if (outflags != NULL)
432 * Get driver information from a set of flags specified. Masks the identifying
433 * part of the flags and compares it against all of the available
434 * tuntap_drivers. Must be called with correct vnet context.
436 static struct tuntap_driver *
437 tuntap_driver_from_flags(int tun_flags)
439 struct tuntap_driver *drv;
440 struct tuntap_driver_cloner *drvc;
442 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
443 ("tuntap_driver_cloners failed to initialize"));
444 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
445 KASSERT(drvc->drv != NULL,
446 ("tuntap_driver_cloners entry not properly initialized"));
448 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
458 tun_clone_match(struct if_clone *ifc, const char *name)
462 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
463 if ((tunflags & TUN_L2) == 0)
471 tap_clone_match(struct if_clone *ifc, const char *name)
475 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
476 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
484 vmnet_clone_match(struct if_clone *ifc, const char *name)
488 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
489 if ((tunflags & TUN_VMNET) != 0)
497 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
499 struct tuntap_driver *drv;
501 int err, i, tunflags, unit;
504 /* The name here tells us exactly what we're creating */
505 err = tuntap_name2info(name, &unit, &tunflags);
509 drv = tuntap_driver_from_flags(tunflags);
514 /* If this unit number is still available that's okay. */
515 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
518 unit = alloc_unr(drv->unrhdr);
521 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
523 /* find any existing device, or allocate new unit number */
524 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
526 /* No preexisting struct cdev *, create one */
527 dev = make_dev(&drv->cdevsw, unit, UID_UUCP, GID_DIALER, 0600,
528 "%s%d", drv->cdevsw.d_name, unit);
537 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
540 char devname[SPECNAMELEN + 1];
541 struct tuntap_driver *drv;
542 int append_unit, i, u, tunflags;
549 CURVNET_SET(CRED_TO_VNET(cred));
550 if (tuntap_name2info(name, &u, &tunflags) != 0)
551 goto out; /* Not recognized */
553 if (u != -1 && u > IF_MAXUNIT)
554 goto out; /* Unit number too high */
556 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
557 if ((tunflags & TUN_L2) != 0) {
558 /* tap/vmnet allow user open with a sysctl */
559 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
561 mayclone = mayclone && tundclone;
565 * If tun cloning is enabled, only the superuser can create an
576 drv = tuntap_driver_from_flags(tunflags);
580 /* find any existing device, or allocate new unit number */
581 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
584 namelen = snprintf(devname, sizeof(devname), "%s%d",
588 /* No preexisting struct cdev *, create one */
589 *dev = make_dev_credf(MAKEDEV_REF, &drv->cdevsw, u, cred,
590 UID_UUCP, GID_DIALER, 0600, "%s", name);
593 if_clone_create(name, namelen, NULL);
599 tun_destroy(struct tuntap_softc *tp)
603 tp->tun_flags |= TUN_DYING;
604 if (tp->tun_busy != 0)
605 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
609 CURVNET_SET(TUN2IFP(tp)->if_vnet);
611 /* destroy_dev will take care of any alias. */
612 destroy_dev(tp->tun_dev);
613 seldrain(&tp->tun_rsel);
614 knlist_clear(&tp->tun_rsel.si_note, 0);
615 knlist_destroy(&tp->tun_rsel.si_note);
616 if ((tp->tun_flags & TUN_L2) != 0) {
617 ether_ifdetach(TUN2IFP(tp));
619 bpfdetach(TUN2IFP(tp));
620 if_detach(TUN2IFP(tp));
622 sx_xlock(&tun_ioctl_sx);
623 TUN2IFP(tp)->if_softc = NULL;
624 sx_xunlock(&tun_ioctl_sx);
625 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
626 if_free(TUN2IFP(tp));
627 mtx_destroy(&tp->tun_mtx);
628 cv_destroy(&tp->tun_cv);
634 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
636 struct tuntap_softc *tp = ifp->if_softc;
639 TAILQ_REMOVE(&tunhead, tp, tun_list);
647 vnet_tun_init(const void *unused __unused)
649 struct tuntap_driver *drv;
650 struct tuntap_driver_cloner *drvc;
653 for (i = 0; i < nitems(tuntap_drivers); ++i) {
654 drv = &tuntap_drivers[i];
655 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
658 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
659 drv->clone_match_fn, drv->clone_create_fn,
660 drv->clone_destroy_fn);
661 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
664 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
665 vnet_tun_init, NULL);
668 vnet_tun_uninit(const void *unused __unused)
670 struct tuntap_driver_cloner *drvc;
672 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
673 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
674 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
676 if_clone_detach(drvc->cloner);
680 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
681 vnet_tun_uninit, NULL);
684 tun_uninit(const void *unused __unused)
686 struct tuntap_driver *drv;
687 struct tuntap_softc *tp;
690 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
691 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
692 drain_dev_clone_events();
695 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
696 TAILQ_REMOVE(&tunhead, tp, tun_list);
702 for (i = 0; i < nitems(tuntap_drivers); ++i) {
703 drv = &tuntap_drivers[i];
704 delete_unrhdr(drv->unrhdr);
705 clone_cleanup(&drv->clones);
707 mtx_destroy(&tunmtx);
709 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
711 static struct tuntap_driver *
712 tuntap_driver_from_ifnet(const struct ifnet *ifp)
714 struct tuntap_driver *drv;
720 for (i = 0; i < nitems(tuntap_drivers); ++i) {
721 drv = &tuntap_drivers[i];
722 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
730 tuntapmodevent(module_t mod, int type, void *data)
732 struct tuntap_driver *drv;
737 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
738 for (i = 0; i < nitems(tuntap_drivers); ++i) {
739 drv = &tuntap_drivers[i];
740 clone_setup(&drv->clones);
741 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
743 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
745 if (arrival_tag == NULL)
747 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
748 if (clone_tag == NULL)
752 /* See tun_uninit, so it's done after the vnet_sysuninit() */
760 static moduledata_t tuntap_mod = {
766 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
767 MODULE_VERSION(if_tuntap, 1);
768 MODULE_VERSION(if_tun, 1);
769 MODULE_VERSION(if_tap, 1);
772 tunstart(struct ifnet *ifp)
774 struct tuntap_softc *tp = ifp->if_softc;
777 TUNDEBUG(ifp, "starting\n");
778 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
779 IFQ_LOCK(&ifp->if_snd);
780 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
782 IFQ_UNLOCK(&ifp->if_snd);
785 IFQ_UNLOCK(&ifp->if_snd);
789 if (tp->tun_flags & TUN_RWAIT) {
790 tp->tun_flags &= ~TUN_RWAIT;
793 selwakeuppri(&tp->tun_rsel, PZERO + 1);
794 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
795 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
797 pgsigio(&tp->tun_sigio, SIGIO, 0);
805 * queue packets from higher level ready to put out
808 tunstart_l2(struct ifnet *ifp)
810 struct tuntap_softc *tp = ifp->if_softc;
812 TUNDEBUG(ifp, "starting\n");
815 * do not junk pending output if we are in VMnet mode.
816 * XXX: can this do any harm because of queue overflow?
820 if (((tp->tun_flags & TUN_VMNET) == 0) &&
821 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
825 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
828 IF_DEQUEUE(&ifp->if_snd, m);
831 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
840 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
842 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
843 if (tp->tun_flags & TUN_RWAIT) {
844 tp->tun_flags &= ~TUN_RWAIT;
848 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
850 pgsigio(&tp->tun_sigio, SIGIO, 0);
854 selwakeuppri(&tp->tun_rsel, PZERO+1);
855 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
856 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
859 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
864 /* XXX: should return an error code so it can fail. */
866 tuncreate(struct cdev *dev, struct tuntap_driver *drv)
868 struct tuntap_softc *sc;
870 struct ether_addr eaddr;
874 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
875 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
876 cv_init(&sc->tun_cv, "tun_condvar");
877 sc->tun_flags = drv->ident_flags;
881 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
884 iflags = IFF_MULTICAST;
885 if ((sc->tun_flags & TUN_L2) != 0) {
887 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
890 iflags |= IFF_POINTOPOINT;
892 ifp = sc->tun_ifp = if_alloc(type);
894 panic("%s%d: failed to if_alloc() interface.\n",
895 drv->cdevsw.d_name, dev2unit(dev));
897 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
898 ifp->if_ioctl = tunifioctl;
899 ifp->if_flags = iflags;
900 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
901 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
902 ifp->if_capabilities |= IFCAP_LINKSTATE;
903 ifp->if_capenable |= IFCAP_LINKSTATE;
905 if ((sc->tun_flags & TUN_L2) != 0) {
906 ifp->if_mtu = ETHERMTU;
907 ifp->if_init = tunifinit;
908 ifp->if_start = tunstart_l2;
910 ether_gen_addr(ifp, &eaddr);
911 ether_ifattach(ifp, eaddr.octet);
913 ifp->if_mtu = TUNMTU;
914 ifp->if_start = tunstart;
915 ifp->if_output = tunoutput;
917 ifp->if_snd.ifq_drv_maxlen = 0;
918 IFQ_SET_READY(&ifp->if_snd);
921 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
926 sc->tun_flags |= TUN_INITED;
929 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
930 ifp->if_xname, dev2unit(dev));
934 tunrename(void *arg __unused, struct ifnet *ifp)
936 struct tuntap_softc *tp;
939 if ((ifp->if_flags & IFF_RENAMING) == 0)
942 if (tuntap_driver_from_ifnet(ifp) == NULL)
946 * We need to grab the ioctl sx long enough to make sure the softc is
947 * still there. If it is, we can safely try to busy the tun device.
948 * The busy may fail if the device is currently dying, in which case
949 * we do nothing. If it doesn't fail, the busy count stops the device
950 * from dying until we've created the alias (that will then be
951 * subsequently destroyed).
953 sx_xlock(&tun_ioctl_sx);
956 sx_xunlock(&tun_ioctl_sx);
959 error = tun_busy(tp);
960 sx_xunlock(&tun_ioctl_sx);
963 if (tp->tun_alias != NULL) {
964 destroy_dev(tp->tun_alias);
965 tp->tun_alias = NULL;
968 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
972 * Failure's ok, aliases are created on a best effort basis. If a
973 * tun user/consumer decides to rename the interface to conflict with
974 * another device (non-ifnet) on the system, we will assume they know
975 * what they are doing. make_dev_alias_p won't touch tun_alias on
976 * failure, so we use it but ignore the return value.
978 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
985 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
988 struct tuntap_driver *drv;
989 struct tuntap_softc *tp;
993 CURVNET_SET(TD_TO_VNET(td));
994 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
997 return (error); /* Shouldn't happen */
1000 if ((tunflags & TUN_L2) != 0) {
1002 if (tap_allow_uopen == 0) {
1003 error = priv_check(td, PRIV_NET_TAP);
1012 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
1017 drv = tuntap_driver_from_flags(tunflags);
1022 tuncreate(dev, drv);
1027 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1033 error = tun_busy_locked(tp);
1034 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1037 if ((tp->tun_flags & TUN_L2) != 0) {
1038 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1039 sizeof(tp->tun_ether.octet));
1041 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1042 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1045 ifp->if_flags |= IFF_UP;
1048 tp->tun_pid = td->td_proc->p_pid;
1049 tp->tun_flags |= TUN_OPEN;
1051 if_link_state_change(ifp, LINK_STATE_UP);
1052 TUNDEBUG(ifp, "open\n");
1059 * tunclose - close the device - mark i/f down & delete
1063 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
1066 struct tuntap_softc *tp;
1077 * Realistically, we can't be obstinate here. This only means that the
1078 * tuntap device was closed out of order, and the last closer wasn't the
1079 * controller. These are still good to know about, though, as software
1080 * should avoid multiple processes with a tuntap device open and
1081 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1084 if (p->p_pid != tp->tun_pid) {
1086 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1087 p->p_pid, p->p_comm, dev->si_name);
1091 * junk all pending output
1093 CURVNET_SET(ifp->if_vnet);
1096 if ((tp->tun_flags & TUN_L2) != 0) {
1098 IF_DRAIN(&ifp->if_snd);
1100 IFQ_PURGE(&ifp->if_snd);
1103 /* For vmnet, we won't do most of the address/route bits */
1104 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1105 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1108 if (ifp->if_flags & IFF_UP) {
1114 /* Delete all addresses and routes which reference this interface. */
1115 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1118 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1120 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1121 /* deal w/IPv4 PtP destination; unlocked read */
1122 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1123 rtinit(ifa, (int)RTM_DELETE,
1124 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1126 rtinit(ifa, (int)RTM_DELETE, 0);
1134 if_link_state_change(ifp, LINK_STATE_DOWN);
1137 funsetown(&tp->tun_sigio);
1138 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1139 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1140 TUNDEBUG (ifp, "closed\n");
1141 tp->tun_flags &= ~TUN_OPEN;
1144 tun_unbusy_locked(tp);
1150 tuninit(struct ifnet *ifp)
1152 struct tuntap_softc *tp = ifp->if_softc;
1157 TUNDEBUG(ifp, "tuninit\n");
1160 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1161 if ((tp->tun_flags & TUN_L2) == 0) {
1162 ifp->if_flags |= IFF_UP;
1163 getmicrotime(&ifp->if_lastchange);
1166 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1167 if (ifa->ifa_addr->sa_family == AF_INET) {
1168 struct sockaddr_in *si;
1170 si = (struct sockaddr_in *)ifa->ifa_addr;
1171 if (si->sin_addr.s_addr)
1172 tp->tun_flags |= TUN_IASET;
1174 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1175 if (si && si->sin_addr.s_addr)
1176 tp->tun_flags |= TUN_DSTADDR;
1179 if_addr_runlock(ifp);
1183 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1185 /* attempt to start output */
1192 * Used only for l2 tunnel.
1195 tunifinit(void *xtp)
1197 struct tuntap_softc *tp;
1199 tp = (struct tuntap_softc *)xtp;
1200 tuninit(tp->tun_ifp);
1204 * Process an ioctl request.
1207 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1209 struct ifreq *ifr = (struct ifreq *)data;
1210 struct tuntap_softc *tp;
1212 struct ifmediareq *ifmr;
1213 int dummy, error = 0;
1217 sx_xlock(&tun_ioctl_sx);
1223 l2tun = (tp->tun_flags & TUN_L2) != 0;
1226 ifs = (struct ifstat *)data;
1229 snprintf(ifs->ascii, sizeof(ifs->ascii),
1230 "\tOpened by PID %d\n", tp->tun_pid);
1232 ifs->ascii[0] = '\0';
1237 error = ether_ioctl(ifp, cmd, data);
1241 TUNDEBUG(ifp, "address set\n");
1244 ifp->if_mtu = ifr->ifr_mtu;
1245 TUNDEBUG(ifp, "mtu set\n");
1257 ifmr = (struct ifmediareq *)data;
1258 dummy = ifmr->ifm_count;
1259 ifmr->ifm_count = 1;
1260 ifmr->ifm_status = IFM_AVALID;
1261 ifmr->ifm_active = IFM_ETHER;
1262 if (tp->tun_flags & TUN_OPEN)
1263 ifmr->ifm_status |= IFM_ACTIVE;
1264 ifmr->ifm_current = ifmr->ifm_active;
1266 int media = IFM_ETHER;
1267 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1272 error = ether_ioctl(ifp, cmd, data);
1278 sx_xunlock(&tun_ioctl_sx);
1283 * tunoutput - queue packets from higher level ready to put out.
1286 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1289 struct tuntap_softc *tp = ifp->if_softc;
1290 u_short cached_tun_flags;
1294 TUNDEBUG (ifp, "tunoutput\n");
1297 error = mac_ifnet_check_transmit(ifp, m0);
1304 /* Could be unlocked read? */
1306 cached_tun_flags = tp->tun_flags;
1308 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1309 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1314 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1319 /* BPF writes need to be handled specially. */
1320 if (dst->sa_family == AF_UNSPEC)
1321 bcopy(dst->sa_data, &af, sizeof(af));
1323 af = dst->sa_family;
1325 if (bpf_peers_present(ifp->if_bpf))
1326 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1328 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1329 if (cached_tun_flags & TUN_LMODE) {
1330 /* allocate space for sockaddr */
1331 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1333 /* if allocation failed drop packet */
1335 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1336 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1339 bcopy(dst, m0->m_data, dst->sa_len);
1343 if (cached_tun_flags & TUN_IFHEAD) {
1344 /* Prepend the address family */
1345 M_PREPEND(m0, 4, M_NOWAIT);
1347 /* if allocation failed drop packet */
1349 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1350 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1353 *(u_int32_t *)m0->m_data = htonl(af);
1360 return (EAFNOSUPPORT);
1364 error = (ifp->if_transmit)(ifp, m0);
1367 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1372 * the cdevsw interface is now pretty minimal.
1375 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1378 struct ifreq ifr, *ifrp;
1379 struct tuntap_softc *tp = dev->si_drv1;
1380 struct tuninfo *tunp;
1382 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1383 defined(COMPAT_FREEBSD4)
1388 l2tun = (tp->tun_flags & TUN_L2) != 0;
1390 /* tap specific ioctls */
1392 /* VMware/VMnet port ioctl's */
1393 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1394 defined(COMPAT_FREEBSD4)
1396 ival = IOCPARM_IVAL(data);
1397 data = (caddr_t)&ival;
1400 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1401 iflags = *(int *)data;
1402 iflags &= TUN_VMIO_FLAG_MASK;
1403 iflags &= ~IFF_CANTCHANGE;
1407 TUN2IFP(tp)->if_flags = iflags |
1408 (TUN2IFP(tp)->if_flags & IFF_CANTCHANGE);
1412 case SIOCGIFADDR: /* get MAC address of the remote side */
1414 bcopy(&tp->tun_ether.octet, data,
1415 sizeof(tp->tun_ether.octet));
1419 case SIOCSIFADDR: /* set MAC address of the remote side */
1421 bcopy(data, &tp->tun_ether.octet,
1422 sizeof(tp->tun_ether.octet));
1428 /* Fall through to the common ioctls if unhandled */
1434 tp->tun_flags |= TUN_LMODE;
1435 tp->tun_flags &= ~TUN_IFHEAD;
1437 tp->tun_flags &= ~TUN_LMODE;
1444 tp->tun_flags |= TUN_IFHEAD;
1445 tp->tun_flags &= ~TUN_LMODE;
1447 tp->tun_flags &= ~TUN_IFHEAD;
1453 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1458 /* deny this if UP */
1459 if (TUN2IFP(tp)->if_flags & IFF_UP)
1462 switch (*(int *)data & ~IFF_MULTICAST) {
1463 case IFF_POINTOPOINT:
1466 TUN2IFP(tp)->if_flags &=
1467 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1468 TUN2IFP(tp)->if_flags |= *(int *)data;
1479 tp->tun_pid = curthread->td_proc->p_pid;
1484 /* Fall through to the common ioctls if unhandled */
1489 ifrp = (struct ifreq *)data;
1490 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1494 tunp = (struct tuninfo *)data;
1495 if (TUN2IFP(tp)->if_type != tunp->type)
1496 return (EPROTOTYPE);
1498 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1499 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1500 ifr.ifr_mtu = tunp->mtu;
1501 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1502 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1510 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1514 tunp = (struct tuninfo *)data;
1516 tunp->mtu = TUN2IFP(tp)->if_mtu;
1517 tunp->type = TUN2IFP(tp)->if_type;
1518 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1522 tundebug = *(int *)data;
1525 *(int *)data = tundebug;
1532 tp->tun_flags |= TUN_ASYNC;
1534 tp->tun_flags &= ~TUN_ASYNC;
1538 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1540 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1541 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1542 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1543 *(int *)data += mb->m_len;
1544 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1549 return (fsetown(*(int *)data, &tp->tun_sigio));
1552 *(int *)data = fgetown(&tp->tun_sigio);
1555 /* This is deprecated, FIOSETOWN should be used instead. */
1557 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1559 /* This is deprecated, FIOGETOWN should be used instead. */
1561 *(int *)data = -fgetown(&tp->tun_sigio);
1571 * The cdevsw read interface - reads a packet at a time, or at
1572 * least as much of a packet as can be read.
1575 tunread(struct cdev *dev, struct uio *uio, int flag)
1577 struct tuntap_softc *tp = dev->si_drv1;
1578 struct ifnet *ifp = TUN2IFP(tp);
1582 TUNDEBUG (ifp, "read\n");
1584 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1586 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1590 tp->tun_flags &= ~TUN_RWAIT;
1593 IFQ_DEQUEUE(&ifp->if_snd, m);
1596 if (flag & O_NONBLOCK) {
1598 return (EWOULDBLOCK);
1600 tp->tun_flags |= TUN_RWAIT;
1601 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1610 if ((tp->tun_flags & TUN_L2) != 0)
1613 while (m && uio->uio_resid > 0 && error == 0) {
1614 len = min(uio->uio_resid, m->m_len);
1616 error = uiomove(mtod(m, void *), len, uio);
1621 TUNDEBUG(ifp, "Dropping mbuf\n");
1628 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m)
1630 struct ether_header *eh;
1636 * Only pass a unicast frame to ether_input(), if it would
1637 * actually have been received by non-virtual hardware.
1639 if (m->m_len < sizeof(struct ether_header)) {
1644 eh = mtod(m, struct ether_header *);
1646 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1647 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1648 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1653 /* Pass packet up to parent. */
1654 CURVNET_SET(ifp->if_vnet);
1655 (*ifp->if_input)(ifp, m);
1657 /* ibytes are counted in parent */
1658 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1663 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1665 struct epoch_tracker et;
1670 /* Could be unlocked read? */
1672 if (tp->tun_flags & TUN_IFHEAD) {
1674 if (m->m_len < sizeof(family) &&
1675 (m = m_pullup(m, sizeof(family))) == NULL)
1677 family = ntohl(*mtod(m, u_int32_t *));
1678 m_adj(m, sizeof(family));
1684 BPF_MTAP2(ifp, &family, sizeof(family), m);
1699 return (EAFNOSUPPORT);
1701 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1702 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1703 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1704 CURVNET_SET(ifp->if_vnet);
1705 M_SETFIB(m, ifp->if_fib);
1706 NET_EPOCH_ENTER(et);
1707 netisr_dispatch(isr, m);
1714 * the cdevsw write interface - an atomic write is a packet - or else!
1717 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1719 struct tuntap_softc *tp;
1728 TUNDEBUG(ifp, "tunwrite\n");
1729 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1730 /* ignore silently */
1733 if (uio->uio_resid == 0)
1736 l2tun = (tp->tun_flags & TUN_L2) != 0;
1738 mru = l2tun ? TAPMRU : TUNMRU;
1740 align = ETHER_ALIGN;
1741 else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1742 mru += sizeof(uint32_t); /* family */
1743 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1744 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1748 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1749 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1753 m->m_pkthdr.rcvif = ifp;
1755 mac_ifnet_create_mbuf(ifp, m);
1759 return (tunwrite_l2(tp, m));
1761 return (tunwrite_l3(tp, m));
1765 * tunpoll - the poll interface, this is only useful on reads
1766 * really. The write detect always returns true, write never blocks
1767 * anyway, it either accepts the packet or drops it.
1770 tunpoll(struct cdev *dev, int events, struct thread *td)
1772 struct tuntap_softc *tp = dev->si_drv1;
1773 struct ifnet *ifp = TUN2IFP(tp);
1776 TUNDEBUG(ifp, "tunpoll\n");
1778 if (events & (POLLIN | POLLRDNORM)) {
1779 IFQ_LOCK(&ifp->if_snd);
1780 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1781 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1782 revents |= events & (POLLIN | POLLRDNORM);
1784 TUNDEBUG(ifp, "tunpoll waiting\n");
1785 selrecord(td, &tp->tun_rsel);
1787 IFQ_UNLOCK(&ifp->if_snd);
1789 revents |= events & (POLLOUT | POLLWRNORM);
1795 * tunkqfilter - support for the kevent() system call.
1798 tunkqfilter(struct cdev *dev, struct knote *kn)
1800 struct tuntap_softc *tp = dev->si_drv1;
1801 struct ifnet *ifp = TUN2IFP(tp);
1803 switch(kn->kn_filter) {
1805 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1806 ifp->if_xname, dev2unit(dev));
1807 kn->kn_fop = &tun_read_filterops;
1811 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1812 ifp->if_xname, dev2unit(dev));
1813 kn->kn_fop = &tun_write_filterops;
1817 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1818 ifp->if_xname, dev2unit(dev));
1823 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1829 * Return true of there is data in the interface queue.
1832 tunkqread(struct knote *kn, long hint)
1835 struct tuntap_softc *tp = kn->kn_hook;
1836 struct cdev *dev = tp->tun_dev;
1837 struct ifnet *ifp = TUN2IFP(tp);
1839 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1841 "%s have data in the queue. Len = %d, minor = %#x\n",
1842 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1846 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1855 * Always can write, always return MTU in kn->data.
1858 tunkqwrite(struct knote *kn, long hint)
1860 struct tuntap_softc *tp = kn->kn_hook;
1861 struct ifnet *ifp = TUN2IFP(tp);
1863 kn->kn_data = ifp->if_mtu;
1869 tunkqdetach(struct knote *kn)
1871 struct tuntap_softc *tp = kn->kn_hook;
1873 knlist_remove(&tp->tun_rsel.si_note, kn, 0);