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/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_dev;
110 u_short tun_flags; /* misc flags */
111 #define TUN_OPEN 0x0001
112 #define TUN_INITED 0x0002
113 #define TUN_IASET 0x0008
114 #define TUN_DSTADDR 0x0010
115 #define TUN_LMODE 0x0020
116 #define TUN_RWAIT 0x0040
117 #define TUN_ASYNC 0x0080
118 #define TUN_IFHEAD 0x0100
119 #define TUN_DYING 0x0200
120 #define TUN_L2 0x0400
121 #define TUN_VMNET 0x0800
123 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
124 #define TUN_READY (TUN_OPEN | TUN_INITED)
126 pid_t tun_pid; /* owning pid */
127 struct ifnet *tun_ifp; /* the interface */
128 struct sigio *tun_sigio; /* async I/O info */
129 struct tuntap_driver *tun_drv; /* appropriate driver */
130 struct selinfo tun_rsel; /* read select */
131 struct mtx tun_mtx; /* softc field mutex */
132 struct cv tun_cv; /* for ref'd dev destroy */
133 struct ether_addr tun_ether; /* remote address */
134 int tun_busy; /* busy count */
136 #define TUN2IFP(sc) ((sc)->tun_ifp)
138 #define TUNDEBUG if (tundebug) if_printf
140 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
141 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
142 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
144 #define TUN_VMIO_FLAG_MASK 0x0fff
147 * All mutable global variables in if_tun are locked using tunmtx, with
148 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
149 * which are static after setup.
151 static struct mtx tunmtx;
152 static eventhandler_tag tag;
153 static const char tunname[] = "tun";
154 static const char tapname[] = "tap";
155 static const char vmnetname[] = "vmnet";
156 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
157 static int tundebug = 0;
158 static int tundclone = 1;
159 static int tap_allow_uopen = 0; /* allow user open() */
160 static int tapuponopen = 0; /* IFF_UP on open() */
161 static int tapdclone = 1; /* enable devfs cloning */
163 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
164 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
166 static struct sx tun_ioctl_sx;
167 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
169 SYSCTL_DECL(_net_link);
171 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
172 "IP tunnel software network interface");
173 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
174 "Enable legacy devfs interface creation");
177 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
178 "Ethernet tunnel software network interface");
179 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
180 "Allow user to open /dev/tap (based on node permissions)");
181 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
182 "Bring interface up when /dev/tap is opened");
183 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
184 "Enable legacy devfs interface creation");
185 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
187 static int tun_busy_locked(struct tuntap_softc *tp);
188 static void tun_unbusy_locked(struct tuntap_softc *tp);
189 static int tun_busy(struct tuntap_softc *tp);
190 static void tun_unbusy(struct tuntap_softc *tp);
192 static int tuntap_name2info(const char *name, int *unit, int *flags);
193 static void tunclone(void *arg, struct ucred *cred, char *name,
194 int namelen, struct cdev **dev);
195 static void tuncreate(struct cdev *dev, struct tuntap_driver *);
196 static int tunifioctl(struct ifnet *, u_long, caddr_t);
197 static void tuninit(struct ifnet *);
198 static void tunifinit(void *xtp);
199 static int tuntapmodevent(module_t, int, void *);
200 static int tunoutput(struct ifnet *, struct mbuf *,
201 const struct sockaddr *, struct route *ro);
202 static void tunstart(struct ifnet *);
203 static void tunstart_l2(struct ifnet *);
205 static int tun_clone_match(struct if_clone *ifc, const char *name);
206 static int tap_clone_match(struct if_clone *ifc, const char *name);
207 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
208 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
209 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
211 static d_open_t tunopen;
212 static d_close_t tunclose;
213 static d_read_t tunread;
214 static d_write_t tunwrite;
215 static d_ioctl_t tunioctl;
216 static d_poll_t tunpoll;
217 static d_kqfilter_t tunkqfilter;
219 static int tunkqread(struct knote *, long);
220 static int tunkqwrite(struct knote *, long);
221 static void tunkqdetach(struct knote *);
223 static struct filterops tun_read_filterops = {
226 .f_detach = tunkqdetach,
227 .f_event = tunkqread,
230 static struct filterops tun_write_filterops = {
233 .f_detach = tunkqdetach,
234 .f_event = tunkqwrite,
237 static struct tuntap_driver {
238 struct cdevsw cdevsw;
240 struct unrhdr *unrhdr;
241 struct clonedevs *clones;
242 ifc_match_t *clone_match_fn;
243 ifc_create_t *clone_create_fn;
244 ifc_destroy_t *clone_destroy_fn;
245 } tuntap_drivers[] = {
249 .d_version = D_VERSION,
250 .d_flags = D_NEEDMINOR,
257 .d_kqfilter = tunkqfilter,
260 .clone_match_fn = tun_clone_match,
261 .clone_create_fn = tun_clone_create,
262 .clone_destroy_fn = tun_clone_destroy,
265 .ident_flags = TUN_L2,
267 .d_version = D_VERSION,
268 .d_flags = D_NEEDMINOR,
275 .d_kqfilter = tunkqfilter,
278 .clone_match_fn = tap_clone_match,
279 .clone_create_fn = tun_clone_create,
280 .clone_destroy_fn = tun_clone_destroy,
283 .ident_flags = TUN_L2 | TUN_VMNET,
285 .d_version = D_VERSION,
286 .d_flags = D_NEEDMINOR,
293 .d_kqfilter = tunkqfilter,
296 .clone_match_fn = vmnet_clone_match,
297 .clone_create_fn = tun_clone_create,
298 .clone_destroy_fn = tun_clone_destroy,
302 struct tuntap_driver_cloner {
303 SLIST_ENTRY(tuntap_driver_cloner) link;
304 struct tuntap_driver *drv;
305 struct if_clone *cloner;
308 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
309 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
311 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
314 * Mechanism for marking a tunnel device as busy so that we can safely do some
315 * orthogonal operations (such as operations on devices) without racing against
316 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
317 * open, to be woken up when the condition is alleviated.
320 tun_busy_locked(struct tuntap_softc *tp)
324 if ((tp->tun_flags & TUN_DYING) != 0) {
326 * Perhaps unintuitive, but the device is busy going away.
327 * Other interpretations of EBUSY from tun_busy make little
328 * sense, since making a busy device even more busy doesn't
329 * sound like a problem.
339 tun_unbusy_locked(struct tuntap_softc *tp)
343 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
346 /* Wake up anything that may be waiting on our busy tunnel. */
347 if (tp->tun_busy == 0)
348 cv_broadcast(&tp->tun_cv);
352 tun_busy(struct tuntap_softc *tp)
357 ret = tun_busy_locked(tp);
364 tun_unbusy(struct tuntap_softc *tp)
368 tun_unbusy_locked(tp);
373 * Sets unit and/or flags given the device name. Must be called with correct
377 tuntap_name2info(const char *name, int *outunit, int *outflags)
379 struct tuntap_driver *drv;
380 struct tuntap_driver_cloner *drvc;
389 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
390 * wants to be able to pass back a char * through the second param. We
391 * will always set that as NULL here, so we'll fake it.
393 dname = __DECONST(char *, name);
396 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
397 ("tuntap_driver_cloners failed to initialize"));
398 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
399 KASSERT(drvc->drv != NULL,
400 ("tuntap_driver_cloners entry not properly initialized"));
403 if (strcmp(name, drv->cdevsw.d_name) == 0) {
406 flags = drv->ident_flags;
410 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
412 flags = drv->ident_flags;
422 if (outflags != NULL)
428 * Get driver information from a set of flags specified. Masks the identifying
429 * part of the flags and compares it against all of the available
430 * tuntap_drivers. Must be called with correct vnet context.
432 static struct tuntap_driver *
433 tuntap_driver_from_flags(int tun_flags)
435 struct tuntap_driver *drv;
436 struct tuntap_driver_cloner *drvc;
438 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
439 ("tuntap_driver_cloners failed to initialize"));
440 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
441 KASSERT(drvc->drv != NULL,
442 ("tuntap_driver_cloners entry not properly initialized"));
444 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
454 tun_clone_match(struct if_clone *ifc, const char *name)
458 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
459 if ((tunflags & TUN_L2) == 0)
467 tap_clone_match(struct if_clone *ifc, const char *name)
471 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
472 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
480 vmnet_clone_match(struct if_clone *ifc, const char *name)
484 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
485 if ((tunflags & TUN_VMNET) != 0)
493 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
495 struct tuntap_driver *drv;
497 int err, i, tunflags, unit;
500 /* The name here tells us exactly what we're creating */
501 err = tuntap_name2info(name, &unit, &tunflags);
505 drv = tuntap_driver_from_flags(tunflags);
510 /* If this unit number is still available that's okay. */
511 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
514 unit = alloc_unr(drv->unrhdr);
517 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
519 /* find any existing device, or allocate new unit number */
520 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
522 /* No preexisting struct cdev *, create one */
523 dev = make_dev(&drv->cdevsw, unit, UID_UUCP, GID_DIALER, 0600,
524 "%s%d", drv->cdevsw.d_name, unit);
533 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
536 char devname[SPECNAMELEN + 1];
537 struct tuntap_driver *drv;
538 int append_unit, i, u, tunflags;
545 CURVNET_SET(CRED_TO_VNET(cred));
546 if (tuntap_name2info(name, &u, &tunflags) != 0)
547 goto out; /* Not recognized */
549 if (u != -1 && u > IF_MAXUNIT)
550 goto out; /* Unit number too high */
552 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
553 if ((tunflags & TUN_L2) != 0) {
554 /* tap/vmnet allow user open with a sysctl */
555 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
557 mayclone = mayclone && tundclone;
561 * If tun cloning is enabled, only the superuser can create an
572 drv = tuntap_driver_from_flags(tunflags);
576 /* find any existing device, or allocate new unit number */
577 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
580 namelen = snprintf(devname, sizeof(devname), "%s%d",
584 /* No preexisting struct cdev *, create one */
585 *dev = make_dev_credf(MAKEDEV_REF, &drv->cdevsw, u, cred,
586 UID_UUCP, GID_DIALER, 0600, "%s", 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(tp->tun_dev);
608 seldrain(&tp->tun_rsel);
609 knlist_clear(&tp->tun_rsel.si_note, 0);
610 knlist_destroy(&tp->tun_rsel.si_note);
611 if ((tp->tun_flags & TUN_L2) != 0) {
612 ether_ifdetach(TUN2IFP(tp));
614 bpfdetach(TUN2IFP(tp));
615 if_detach(TUN2IFP(tp));
617 sx_xlock(&tun_ioctl_sx);
618 TUN2IFP(tp)->if_softc = NULL;
619 sx_xunlock(&tun_ioctl_sx);
620 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
621 if_free(TUN2IFP(tp));
622 mtx_destroy(&tp->tun_mtx);
623 cv_destroy(&tp->tun_cv);
629 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
631 struct tuntap_softc *tp = ifp->if_softc;
634 TAILQ_REMOVE(&tunhead, tp, tun_list);
642 vnet_tun_init(const void *unused __unused)
644 struct tuntap_driver *drv;
645 struct tuntap_driver_cloner *drvc;
648 for (i = 0; i < nitems(tuntap_drivers); ++i) {
649 drv = &tuntap_drivers[i];
650 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
653 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
654 drv->clone_match_fn, drv->clone_create_fn,
655 drv->clone_destroy_fn);
656 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
659 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
660 vnet_tun_init, NULL);
663 vnet_tun_uninit(const void *unused __unused)
665 struct tuntap_driver_cloner *drvc;
667 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
668 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
669 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
671 if_clone_detach(drvc->cloner);
675 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
676 vnet_tun_uninit, NULL);
679 tun_uninit(const void *unused __unused)
681 struct tuntap_driver *drv;
682 struct tuntap_softc *tp;
685 EVENTHANDLER_DEREGISTER(dev_clone, tag);
686 drain_dev_clone_events();
689 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
690 TAILQ_REMOVE(&tunhead, tp, tun_list);
696 for (i = 0; i < nitems(tuntap_drivers); ++i) {
697 drv = &tuntap_drivers[i];
698 delete_unrhdr(drv->unrhdr);
699 clone_cleanup(&drv->clones);
701 mtx_destroy(&tunmtx);
703 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
706 tuntapmodevent(module_t mod, int type, void *data)
708 struct tuntap_driver *drv;
713 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
714 for (i = 0; i < nitems(tuntap_drivers); ++i) {
715 drv = &tuntap_drivers[i];
716 clone_setup(&drv->clones);
717 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
719 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
724 /* See tun_uninit, so it's done after the vnet_sysuninit() */
732 static moduledata_t tuntap_mod = {
738 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
739 MODULE_VERSION(if_tuntap, 1);
740 MODULE_VERSION(if_tun, 1);
741 MODULE_VERSION(if_tap, 1);
744 tunstart(struct ifnet *ifp)
746 struct tuntap_softc *tp = ifp->if_softc;
749 TUNDEBUG(ifp, "starting\n");
750 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
751 IFQ_LOCK(&ifp->if_snd);
752 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
754 IFQ_UNLOCK(&ifp->if_snd);
757 IFQ_UNLOCK(&ifp->if_snd);
761 if (tp->tun_flags & TUN_RWAIT) {
762 tp->tun_flags &= ~TUN_RWAIT;
765 selwakeuppri(&tp->tun_rsel, PZERO + 1);
766 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
767 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
769 pgsigio(&tp->tun_sigio, SIGIO, 0);
777 * queue packets from higher level ready to put out
780 tunstart_l2(struct ifnet *ifp)
782 struct tuntap_softc *tp = ifp->if_softc;
784 TUNDEBUG(ifp, "starting\n");
787 * do not junk pending output if we are in VMnet mode.
788 * XXX: can this do any harm because of queue overflow?
792 if (((tp->tun_flags & TUN_VMNET) == 0) &&
793 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
797 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
800 IF_DEQUEUE(&ifp->if_snd, m);
803 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
812 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
814 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
815 if (tp->tun_flags & TUN_RWAIT) {
816 tp->tun_flags &= ~TUN_RWAIT;
820 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
822 pgsigio(&tp->tun_sigio, SIGIO, 0);
826 selwakeuppri(&tp->tun_rsel, PZERO+1);
827 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
828 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
831 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
836 /* XXX: should return an error code so it can fail. */
838 tuncreate(struct cdev *dev, struct tuntap_driver *drv)
840 struct tuntap_softc *sc;
842 struct ether_addr eaddr;
846 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
847 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
848 cv_init(&sc->tun_cv, "tun_condvar");
849 sc->tun_flags = drv->ident_flags;
853 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
856 iflags = IFF_MULTICAST;
857 if ((sc->tun_flags & TUN_L2) != 0) {
859 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
862 iflags |= IFF_POINTOPOINT;
864 ifp = sc->tun_ifp = if_alloc(type);
866 panic("%s%d: failed to if_alloc() interface.\n",
867 drv->cdevsw.d_name, dev2unit(dev));
869 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
870 ifp->if_ioctl = tunifioctl;
871 ifp->if_flags = iflags;
872 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
873 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
874 ifp->if_capabilities |= IFCAP_LINKSTATE;
875 ifp->if_capenable |= IFCAP_LINKSTATE;
877 if ((sc->tun_flags & TUN_L2) != 0) {
878 ifp->if_mtu = ETHERMTU;
879 ifp->if_init = tunifinit;
880 ifp->if_start = tunstart_l2;
882 ether_gen_addr(ifp, &eaddr);
883 ether_ifattach(ifp, eaddr.octet);
885 ifp->if_mtu = TUNMTU;
886 ifp->if_start = tunstart;
887 ifp->if_output = tunoutput;
889 ifp->if_snd.ifq_drv_maxlen = 0;
890 IFQ_SET_READY(&ifp->if_snd);
893 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
898 sc->tun_flags |= TUN_INITED;
901 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
902 ifp->if_xname, dev2unit(dev));
906 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
909 struct tuntap_driver *drv;
910 struct tuntap_softc *tp;
914 CURVNET_SET(TD_TO_VNET(td));
915 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
918 return (error); /* Shouldn't happen */
921 if ((tunflags & TUN_L2) != 0) {
923 if (tap_allow_uopen == 0) {
924 error = priv_check(td, PRIV_NET_TAP);
933 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
938 drv = tuntap_driver_from_flags(tunflags);
948 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
954 error = tun_busy_locked(tp);
955 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
958 if ((tp->tun_flags & TUN_L2) != 0) {
959 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
960 sizeof(tp->tun_ether.octet));
962 ifp->if_drv_flags |= IFF_DRV_RUNNING;
963 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
966 ifp->if_flags |= IFF_UP;
969 tp->tun_pid = td->td_proc->p_pid;
970 tp->tun_flags |= TUN_OPEN;
972 if_link_state_change(ifp, LINK_STATE_UP);
973 TUNDEBUG(ifp, "open\n");
980 * tunclose - close the device - mark i/f down & delete
984 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
986 struct tuntap_softc *tp;
995 * Simply close the device if this isn't the controlling process. This
996 * may happen if, for instance, the tunnel has been handed off to
997 * another process. The original controller should be able to close it
998 * without putting us into an inconsistent state.
1000 if (td->td_proc->p_pid != tp->tun_pid) {
1006 * junk all pending output
1008 CURVNET_SET(ifp->if_vnet);
1011 if ((tp->tun_flags & TUN_L2) != 0) {
1013 IF_DRAIN(&ifp->if_snd);
1015 IFQ_PURGE(&ifp->if_snd);
1018 /* For vmnet, we won't do most of the address/route bits */
1019 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1020 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1023 if (ifp->if_flags & IFF_UP) {
1029 /* Delete all addresses and routes which reference this interface. */
1030 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1033 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1035 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1036 /* deal w/IPv4 PtP destination; unlocked read */
1037 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1038 rtinit(ifa, (int)RTM_DELETE,
1039 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1041 rtinit(ifa, (int)RTM_DELETE, 0);
1049 if_link_state_change(ifp, LINK_STATE_DOWN);
1052 funsetown(&tp->tun_sigio);
1053 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1054 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1055 TUNDEBUG (ifp, "closed\n");
1056 tp->tun_flags &= ~TUN_OPEN;
1059 tun_unbusy_locked(tp);
1065 tuninit(struct ifnet *ifp)
1067 struct tuntap_softc *tp = ifp->if_softc;
1072 TUNDEBUG(ifp, "tuninit\n");
1075 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1076 if ((tp->tun_flags & TUN_L2) == 0) {
1077 ifp->if_flags |= IFF_UP;
1078 getmicrotime(&ifp->if_lastchange);
1081 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1082 if (ifa->ifa_addr->sa_family == AF_INET) {
1083 struct sockaddr_in *si;
1085 si = (struct sockaddr_in *)ifa->ifa_addr;
1086 if (si->sin_addr.s_addr)
1087 tp->tun_flags |= TUN_IASET;
1089 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1090 if (si && si->sin_addr.s_addr)
1091 tp->tun_flags |= TUN_DSTADDR;
1094 if_addr_runlock(ifp);
1098 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1100 /* attempt to start output */
1107 * Used only for l2 tunnel.
1110 tunifinit(void *xtp)
1112 struct tuntap_softc *tp;
1114 tp = (struct tuntap_softc *)xtp;
1115 tuninit(tp->tun_ifp);
1119 * Process an ioctl request.
1122 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1124 struct ifreq *ifr = (struct ifreq *)data;
1125 struct tuntap_softc *tp;
1127 struct ifmediareq *ifmr;
1128 int dummy, error = 0;
1132 sx_xlock(&tun_ioctl_sx);
1138 l2tun = (tp->tun_flags & TUN_L2) != 0;
1141 ifs = (struct ifstat *)data;
1144 snprintf(ifs->ascii, sizeof(ifs->ascii),
1145 "\tOpened by PID %d\n", tp->tun_pid);
1147 ifs->ascii[0] = '\0';
1152 error = ether_ioctl(ifp, cmd, data);
1156 TUNDEBUG(ifp, "address set\n");
1159 ifp->if_mtu = ifr->ifr_mtu;
1160 TUNDEBUG(ifp, "mtu set\n");
1172 ifmr = (struct ifmediareq *)data;
1173 dummy = ifmr->ifm_count;
1174 ifmr->ifm_count = 1;
1175 ifmr->ifm_status = IFM_AVALID;
1176 ifmr->ifm_active = IFM_ETHER;
1177 if (tp->tun_flags & TUN_OPEN)
1178 ifmr->ifm_status |= IFM_ACTIVE;
1179 ifmr->ifm_current = ifmr->ifm_active;
1181 int media = IFM_ETHER;
1182 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1187 error = ether_ioctl(ifp, cmd, data);
1193 sx_xunlock(&tun_ioctl_sx);
1198 * tunoutput - queue packets from higher level ready to put out.
1201 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1204 struct tuntap_softc *tp = ifp->if_softc;
1205 u_short cached_tun_flags;
1209 TUNDEBUG (ifp, "tunoutput\n");
1212 error = mac_ifnet_check_transmit(ifp, m0);
1219 /* Could be unlocked read? */
1221 cached_tun_flags = tp->tun_flags;
1223 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1224 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1229 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1234 /* BPF writes need to be handled specially. */
1235 if (dst->sa_family == AF_UNSPEC)
1236 bcopy(dst->sa_data, &af, sizeof(af));
1238 af = dst->sa_family;
1240 if (bpf_peers_present(ifp->if_bpf))
1241 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1243 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1244 if (cached_tun_flags & TUN_LMODE) {
1245 /* allocate space for sockaddr */
1246 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1248 /* if allocation failed drop packet */
1250 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1251 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1254 bcopy(dst, m0->m_data, dst->sa_len);
1258 if (cached_tun_flags & TUN_IFHEAD) {
1259 /* Prepend the address family */
1260 M_PREPEND(m0, 4, M_NOWAIT);
1262 /* if allocation failed drop packet */
1264 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1265 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1268 *(u_int32_t *)m0->m_data = htonl(af);
1275 return (EAFNOSUPPORT);
1279 error = (ifp->if_transmit)(ifp, m0);
1282 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1287 * the cdevsw interface is now pretty minimal.
1290 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1293 struct ifreq ifr, *ifrp;
1294 struct tuntap_softc *tp = dev->si_drv1;
1295 struct tuninfo *tunp;
1297 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1298 defined(COMPAT_FREEBSD4)
1303 l2tun = (tp->tun_flags & TUN_L2) != 0;
1305 /* tap specific ioctls */
1307 /* VMware/VMnet port ioctl's */
1308 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1309 defined(COMPAT_FREEBSD4)
1311 ival = IOCPARM_IVAL(data);
1312 data = (caddr_t)&ival;
1315 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1316 iflags = *(int *)data;
1317 iflags &= TUN_VMIO_FLAG_MASK;
1318 iflags &= ~IFF_CANTCHANGE;
1322 TUN2IFP(tp)->if_flags = iflags |
1323 (TUN2IFP(tp)->if_flags & IFF_CANTCHANGE);
1327 case SIOCGIFADDR: /* get MAC address of the remote side */
1329 bcopy(&tp->tun_ether.octet, data,
1330 sizeof(tp->tun_ether.octet));
1334 case SIOCSIFADDR: /* set MAC address of the remote side */
1336 bcopy(data, &tp->tun_ether.octet,
1337 sizeof(tp->tun_ether.octet));
1343 /* Fall through to the common ioctls if unhandled */
1349 tp->tun_flags |= TUN_LMODE;
1350 tp->tun_flags &= ~TUN_IFHEAD;
1352 tp->tun_flags &= ~TUN_LMODE;
1359 tp->tun_flags |= TUN_IFHEAD;
1360 tp->tun_flags &= ~TUN_LMODE;
1362 tp->tun_flags &= ~TUN_IFHEAD;
1368 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1373 /* deny this if UP */
1374 if (TUN2IFP(tp)->if_flags & IFF_UP)
1377 switch (*(int *)data & ~IFF_MULTICAST) {
1378 case IFF_POINTOPOINT:
1381 TUN2IFP(tp)->if_flags &=
1382 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1383 TUN2IFP(tp)->if_flags |= *(int *)data;
1394 tp->tun_pid = curthread->td_proc->p_pid;
1399 /* Fall through to the common ioctls if unhandled */
1404 ifrp = (struct ifreq *)data;
1405 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1409 tunp = (struct tuninfo *)data;
1410 if (TUN2IFP(tp)->if_type != tunp->type)
1411 return (EPROTOTYPE);
1413 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1414 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1415 ifr.ifr_mtu = tunp->mtu;
1416 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1417 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1425 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1429 tunp = (struct tuninfo *)data;
1431 tunp->mtu = TUN2IFP(tp)->if_mtu;
1432 tunp->type = TUN2IFP(tp)->if_type;
1433 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1437 tundebug = *(int *)data;
1440 *(int *)data = tundebug;
1447 tp->tun_flags |= TUN_ASYNC;
1449 tp->tun_flags &= ~TUN_ASYNC;
1453 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1455 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1456 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1457 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1458 *(int *)data += mb->m_len;
1459 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1464 return (fsetown(*(int *)data, &tp->tun_sigio));
1467 *(int *)data = fgetown(&tp->tun_sigio);
1470 /* This is deprecated, FIOSETOWN should be used instead. */
1472 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1474 /* This is deprecated, FIOGETOWN should be used instead. */
1476 *(int *)data = -fgetown(&tp->tun_sigio);
1486 * The cdevsw read interface - reads a packet at a time, or at
1487 * least as much of a packet as can be read.
1490 tunread(struct cdev *dev, struct uio *uio, int flag)
1492 struct tuntap_softc *tp = dev->si_drv1;
1493 struct ifnet *ifp = TUN2IFP(tp);
1497 TUNDEBUG (ifp, "read\n");
1499 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1501 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1505 tp->tun_flags &= ~TUN_RWAIT;
1508 IFQ_DEQUEUE(&ifp->if_snd, m);
1511 if (flag & O_NONBLOCK) {
1513 return (EWOULDBLOCK);
1515 tp->tun_flags |= TUN_RWAIT;
1516 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1525 if ((tp->tun_flags & TUN_L2) != 0)
1528 while (m && uio->uio_resid > 0 && error == 0) {
1529 len = min(uio->uio_resid, m->m_len);
1531 error = uiomove(mtod(m, void *), len, uio);
1536 TUNDEBUG(ifp, "Dropping mbuf\n");
1543 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m)
1545 struct ether_header *eh;
1551 * Only pass a unicast frame to ether_input(), if it would
1552 * actually have been received by non-virtual hardware.
1554 if (m->m_len < sizeof(struct ether_header)) {
1559 eh = mtod(m, struct ether_header *);
1561 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1562 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1563 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1568 /* Pass packet up to parent. */
1569 CURVNET_SET(ifp->if_vnet);
1570 (*ifp->if_input)(ifp, m);
1572 /* ibytes are counted in parent */
1573 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1578 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1584 /* Could be unlocked read? */
1586 if (tp->tun_flags & TUN_IFHEAD) {
1588 if (m->m_len < sizeof(family) &&
1589 (m = m_pullup(m, sizeof(family))) == NULL)
1591 family = ntohl(*mtod(m, u_int32_t *));
1592 m_adj(m, sizeof(family));
1598 BPF_MTAP2(ifp, &family, sizeof(family), m);
1613 return (EAFNOSUPPORT);
1615 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1616 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1617 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1618 CURVNET_SET(ifp->if_vnet);
1619 M_SETFIB(m, ifp->if_fib);
1620 netisr_dispatch(isr, m);
1626 * the cdevsw write interface - an atomic write is a packet - or else!
1629 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1631 struct tuntap_softc *tp;
1640 TUNDEBUG(ifp, "tunwrite\n");
1641 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1642 /* ignore silently */
1645 if (uio->uio_resid == 0)
1648 l2tun = (tp->tun_flags & TUN_L2) != 0;
1650 mru = l2tun ? TAPMRU : TUNMRU;
1652 align = ETHER_ALIGN;
1653 else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1654 mru += sizeof(uint32_t); /* family */
1655 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1656 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1660 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1661 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1665 m->m_pkthdr.rcvif = ifp;
1667 mac_ifnet_create_mbuf(ifp, m);
1671 return (tunwrite_l2(tp, m));
1673 return (tunwrite_l3(tp, m));
1677 * tunpoll - the poll interface, this is only useful on reads
1678 * really. The write detect always returns true, write never blocks
1679 * anyway, it either accepts the packet or drops it.
1682 tunpoll(struct cdev *dev, int events, struct thread *td)
1684 struct tuntap_softc *tp = dev->si_drv1;
1685 struct ifnet *ifp = TUN2IFP(tp);
1688 TUNDEBUG(ifp, "tunpoll\n");
1690 if (events & (POLLIN | POLLRDNORM)) {
1691 IFQ_LOCK(&ifp->if_snd);
1692 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1693 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1694 revents |= events & (POLLIN | POLLRDNORM);
1696 TUNDEBUG(ifp, "tunpoll waiting\n");
1697 selrecord(td, &tp->tun_rsel);
1699 IFQ_UNLOCK(&ifp->if_snd);
1701 revents |= events & (POLLOUT | POLLWRNORM);
1707 * tunkqfilter - support for the kevent() system call.
1710 tunkqfilter(struct cdev *dev, struct knote *kn)
1712 struct tuntap_softc *tp = dev->si_drv1;
1713 struct ifnet *ifp = TUN2IFP(tp);
1715 switch(kn->kn_filter) {
1717 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1718 ifp->if_xname, dev2unit(dev));
1719 kn->kn_fop = &tun_read_filterops;
1723 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1724 ifp->if_xname, dev2unit(dev));
1725 kn->kn_fop = &tun_write_filterops;
1729 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1730 ifp->if_xname, dev2unit(dev));
1735 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1741 * Return true of there is data in the interface queue.
1744 tunkqread(struct knote *kn, long hint)
1747 struct tuntap_softc *tp = kn->kn_hook;
1748 struct cdev *dev = tp->tun_dev;
1749 struct ifnet *ifp = TUN2IFP(tp);
1751 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1753 "%s have data in the queue. Len = %d, minor = %#x\n",
1754 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1758 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1767 * Always can write, always return MTU in kn->data.
1770 tunkqwrite(struct knote *kn, long hint)
1772 struct tuntap_softc *tp = kn->kn_hook;
1773 struct ifnet *ifp = TUN2IFP(tp);
1775 kn->kn_data = ifp->if_mtu;
1781 tunkqdetach(struct knote *kn)
1783 struct tuntap_softc *tp = kn->kn_hook;
1785 knlist_remove(&tp->tun_rsel.si_note, kn, 0);