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/ttycom.h>
67 #include <sys/selinfo.h>
68 #include <sys/signalvar.h>
69 #include <sys/filedesc.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
74 #include <sys/malloc.h>
75 #include <sys/random.h>
76 #include <sys/ctype.h>
78 #include <net/ethernet.h>
80 #include <net/if_var.h>
81 #include <net/if_clone.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 #include <net/if_types.h>
85 #include <net/netisr.h>
86 #include <net/route.h>
89 #include <netinet/in.h>
92 #include <net/if_tap.h>
93 #include <net/if_tun.h>
95 #include <sys/queue.h>
96 #include <sys/condvar.h>
97 #include <security/mac/mac_framework.h>
102 * tun_list is protected by global tunmtx. Other mutable fields are
103 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
104 * static for the duration of a tunnel interface.
106 struct tuntap_softc {
107 TAILQ_ENTRY(tuntap_softc) tun_list;
108 struct cdev *tun_dev;
109 u_short tun_flags; /* misc flags */
110 #define TUN_OPEN 0x0001
111 #define TUN_INITED 0x0002
112 #define TUN_RCOLL 0x0004
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_READY (TUN_OPEN | TUN_INITED)
125 pid_t tun_pid; /* owning pid */
126 struct ifnet *tun_ifp; /* the interface */
127 struct sigio *tun_sigio; /* information for async I/O */
128 struct tuntap_driver *tun_drv; /* appropriate driver */
129 struct selinfo tun_rsel; /* read select */
130 struct mtx tun_mtx; /* protect mutable softc fields */
131 struct cv tun_cv; /* protect against ref'd dev destroy */
132 struct ether_addr tun_ether; /* remote address */
134 #define TUN2IFP(sc) ((sc)->tun_ifp)
136 #define TUNDEBUG if (tundebug) if_printf
138 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
139 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
141 #define TUN_VMIO_FLAG_MASK 0x0fff
144 * All mutable global variables in if_tun are locked using tunmtx, with
145 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
146 * which are static after setup.
148 static struct mtx tunmtx;
149 static eventhandler_tag tag;
150 static const char tunname[] = "tun";
151 static const char tapname[] = "tap";
152 static const char vmnetname[] = "vmnet";
153 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
154 static int tundebug = 0;
155 static int tundclone = 1;
156 static int tap_allow_uopen = 0; /* allow user open() */
157 static int tapuponopen = 0; /* IFF_UP on open() */
158 static int tapdclone = 1; /* enable devfs cloning */
160 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
161 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
163 static struct sx tun_ioctl_sx;
164 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
166 SYSCTL_DECL(_net_link);
168 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
169 "IP tunnel software network interface.");
170 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
171 "Enable legacy devfs interface creation.");
174 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
175 "Ethernet tunnel software network interface");
176 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
177 "Allow user to open /dev/tap (based on node permissions)");
178 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
179 "Bring interface up when /dev/tap is opened");
180 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
181 "Enable legacy devfs interface creation");
182 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
184 static int tuntap_name2info(const char *name, int *unit, int *flags);
185 static void tunclone(void *arg, struct ucred *cred, char *name,
186 int namelen, struct cdev **dev);
187 static void tuncreate(struct cdev *dev, struct tuntap_driver *);
188 static int tunifioctl(struct ifnet *, u_long, caddr_t);
189 static void tuninit(struct ifnet *);
190 static void tunifinit(void *xtp);
191 static int tuntapmodevent(module_t, int, void *);
192 static int tunoutput(struct ifnet *, struct mbuf *,
193 const struct sockaddr *, struct route *ro);
194 static void tunstart(struct ifnet *);
195 static void tunstart_l2(struct ifnet *);
197 static int tun_clone_match(struct if_clone *ifc, const char *name);
198 static int tap_clone_match(struct if_clone *ifc, const char *name);
199 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
200 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
201 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
203 static d_open_t tunopen;
204 static d_close_t tunclose;
205 static d_read_t tunread;
206 static d_write_t tunwrite;
207 static d_ioctl_t tunioctl;
208 static d_poll_t tunpoll;
209 static d_kqfilter_t tunkqfilter;
211 static int tunkqread(struct knote *, long);
212 static int tunkqwrite(struct knote *, long);
213 static void tunkqdetach(struct knote *);
215 static struct filterops tun_read_filterops = {
218 .f_detach = tunkqdetach,
219 .f_event = tunkqread,
222 static struct filterops tun_write_filterops = {
225 .f_detach = tunkqdetach,
226 .f_event = tunkqwrite,
229 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
231 static struct tuntap_driver {
233 struct unrhdr *unrhdr;
234 struct cdevsw cdevsw;
235 struct clonedevs *clones;
236 ifc_match_t *clone_match_fn;
237 ifc_create_t *clone_create_fn;
238 ifc_destroy_t *clone_destroy_fn;
239 } tuntap_drivers[] = {
243 .d_version = D_VERSION,
244 .d_flags = D_NEEDMINOR,
251 .d_kqfilter = tunkqfilter,
254 .clone_match_fn = tun_clone_match,
255 .clone_create_fn = tun_clone_create,
256 .clone_destroy_fn = tun_clone_destroy,
261 .d_version = D_VERSION,
262 .d_flags = D_NEEDMINOR,
269 .d_kqfilter = tunkqfilter,
272 .clone_match_fn = tap_clone_match,
273 .clone_create_fn = tun_clone_create,
274 .clone_destroy_fn = tun_clone_destroy,
277 .tun_flags = TUN_L2 | TUN_VMNET,
279 .d_version = D_VERSION,
280 .d_flags = D_NEEDMINOR,
287 .d_kqfilter = tunkqfilter,
290 .clone_match_fn = vmnet_clone_match,
291 .clone_create_fn = tun_clone_create,
292 .clone_destroy_fn = tun_clone_destroy,
296 struct tuntap_driver_cloner {
297 SLIST_ENTRY(tuntap_driver_cloner) link;
298 struct tuntap_driver *drv;
299 struct if_clone *cloner;
302 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
303 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
305 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
308 * Sets unit and/or flags given the device name. Must be called with correct
312 tuntap_name2info(const char *name, int *outunit, int *outflags)
314 struct tuntap_driver *drv;
315 struct tuntap_driver_cloner *drvc;
324 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
325 * wants to be able to pass back a char * through the second param. We
326 * will always set that as NULL here, so we'll fake it.
328 dname = __DECONST(char *, name);
331 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
332 ("tuntap_driver_cloners failed to initialize"));
333 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
334 KASSERT(drvc->drv != NULL,
335 ("tuntap_driver_cloners entry not properly initialized"));
338 if (strcmp(name, drv->cdevsw.d_name) == 0) {
341 flags = drv->tun_flags;
345 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
347 flags = drv->tun_flags;
357 if (outflags != NULL)
363 * Get driver information from a set of flags specified. Masks the identifying
364 * part of the flags and compares it against all of the available
365 * tuntap_drivers. Must be called with correct vnet context.
367 static struct tuntap_driver *
368 tuntap_driver_from_flags(int tun_flags)
370 struct tuntap_driver *drv;
371 struct tuntap_driver_cloner *drvc;
373 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
374 ("tuntap_driver_cloners failed to initialize"));
375 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
376 KASSERT(drvc->drv != NULL,
377 ("tuntap_driver_cloners entry not properly initialized"));
379 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->tun_flags)
389 tun_clone_match(struct if_clone *ifc, const char *name)
393 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
394 if ((tunflags & TUN_L2) == 0)
402 tap_clone_match(struct if_clone *ifc, const char *name)
406 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
407 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
415 vmnet_clone_match(struct if_clone *ifc, const char *name)
419 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
420 if ((tunflags & TUN_VMNET) != 0)
428 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
430 struct tuntap_driver *drv;
432 int err, i, tunflags, unit;
435 /* The name here tells us exactly what we're creating */
436 err = tuntap_name2info(name, &unit, &tunflags);
440 drv = tuntap_driver_from_flags(tunflags);
445 /* If this unit number is still available that/s okay. */
446 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
449 unit = alloc_unr(drv->unrhdr);
452 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
454 /* find any existing device, or allocate new unit number */
455 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
457 /* No preexisting struct cdev *, create one */
458 dev = make_dev(&drv->cdevsw, unit, UID_UUCP, GID_DIALER, 0600,
459 "%s%d", drv->cdevsw.d_name, unit);
468 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
471 char devname[SPECNAMELEN + 1];
472 struct tuntap_driver *drv;
473 int append_unit, i, u, tunflags;
480 CURVNET_SET(CRED_TO_VNET(cred));
481 if (tuntap_name2info(name, &u, &tunflags) != 0)
482 goto out; /* Not recognized */
484 if (u != -1 && u > IF_MAXUNIT)
485 goto out; /* Unit number too high */
487 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
488 if ((tunflags & TUN_L2) != 0) {
489 /* tap/vmnet allow user open with a sysctl */
490 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
492 mayclone = mayclone && tundclone;
496 * If tun cloning is enabled, only the superuser can create an
507 drv = tuntap_driver_from_flags(tunflags);
511 /* find any existing device, or allocate new unit number */
512 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
515 namelen = snprintf(devname, sizeof(devname), "%s%d",
519 /* No preexisting struct cdev *, create one */
520 *dev = make_dev_credf(MAKEDEV_REF, &drv->cdevsw, u, cred,
521 UID_UUCP, GID_DIALER, 0600, "%s", name);
524 if_clone_create(name, namelen, NULL);
530 tun_destroy(struct tuntap_softc *tp)
535 tp->tun_flags |= TUN_DYING;
536 if ((tp->tun_flags & TUN_OPEN) != 0)
537 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
541 CURVNET_SET(TUN2IFP(tp)->if_vnet);
542 sx_xlock(&tun_ioctl_sx);
543 TUN2IFP(tp)->if_softc = NULL;
544 sx_xunlock(&tun_ioctl_sx);
547 bpfdetach(TUN2IFP(tp));
548 if_detach(TUN2IFP(tp));
549 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
550 if_free(TUN2IFP(tp));
552 seldrain(&tp->tun_rsel);
553 knlist_clear(&tp->tun_rsel.si_note, 0);
554 knlist_destroy(&tp->tun_rsel.si_note);
555 mtx_destroy(&tp->tun_mtx);
556 cv_destroy(&tp->tun_cv);
562 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
564 struct tuntap_softc *tp = ifp->if_softc;
567 TAILQ_REMOVE(&tunhead, tp, tun_list);
575 vnet_tun_init(const void *unused __unused)
577 struct tuntap_driver *drv;
578 struct tuntap_driver_cloner *drvc;
581 for (i = 0; i < nitems(tuntap_drivers); ++i) {
582 drv = &tuntap_drivers[i];
583 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
586 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
587 drv->clone_match_fn, drv->clone_create_fn,
588 drv->clone_destroy_fn);
589 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
592 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
593 vnet_tun_init, NULL);
596 vnet_tun_uninit(const void *unused __unused)
598 struct tuntap_driver_cloner *drvc;
600 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
601 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
602 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
604 if_clone_detach(drvc->cloner);
608 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
609 vnet_tun_uninit, NULL);
612 tun_uninit(const void *unused __unused)
614 struct tuntap_driver *drv;
615 struct tuntap_softc *tp;
618 EVENTHANDLER_DEREGISTER(dev_clone, tag);
619 drain_dev_clone_events();
622 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
623 TAILQ_REMOVE(&tunhead, tp, tun_list);
629 for (i = 0; i < nitems(tuntap_drivers); ++i) {
630 drv = &tuntap_drivers[i];
631 delete_unrhdr(drv->unrhdr);
632 clone_cleanup(&drv->clones);
634 mtx_destroy(&tunmtx);
636 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
639 tuntapmodevent(module_t mod, int type, void *data)
641 struct tuntap_driver *drv;
646 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
647 for (i = 0; i < nitems(tuntap_drivers); ++i) {
648 drv = &tuntap_drivers[i];
649 clone_setup(&drv->clones);
650 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
652 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
657 /* See tun_uninit, so it's done after the vnet_sysuninit() */
665 static moduledata_t tuntap_mod = {
671 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
672 MODULE_VERSION(if_tuntap, 1);
675 tunstart(struct ifnet *ifp)
677 struct tuntap_softc *tp = ifp->if_softc;
680 TUNDEBUG(ifp, "starting\n");
681 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
682 IFQ_LOCK(&ifp->if_snd);
683 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
685 IFQ_UNLOCK(&ifp->if_snd);
688 IFQ_UNLOCK(&ifp->if_snd);
692 if (tp->tun_flags & TUN_RWAIT) {
693 tp->tun_flags &= ~TUN_RWAIT;
696 selwakeuppri(&tp->tun_rsel, PZERO + 1);
697 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
698 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
700 pgsigio(&tp->tun_sigio, SIGIO, 0);
708 * queue packets from higher level ready to put out
711 tunstart_l2(struct ifnet *ifp)
713 struct tuntap_softc *tp = ifp->if_softc;
715 TUNDEBUG(ifp, "starting\n");
718 * do not junk pending output if we are in VMnet mode.
719 * XXX: can this do any harm because of queue overflow?
723 if (((tp->tun_flags & TUN_VMNET) == 0) &&
724 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
728 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
731 IF_DEQUEUE(&ifp->if_snd, m);
734 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
743 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
745 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
746 if (tp->tun_flags & TUN_RWAIT) {
747 tp->tun_flags &= ~TUN_RWAIT;
751 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
753 pgsigio(&tp->tun_sigio, SIGIO, 0);
757 selwakeuppri(&tp->tun_rsel, PZERO+1);
758 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
759 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
762 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
767 /* XXX: should return an error code so it can fail. */
769 tuncreate(struct cdev *dev, struct tuntap_driver *drv)
771 struct tuntap_softc *sc;
773 struct ether_addr eaddr;
777 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
778 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
779 cv_init(&sc->tun_cv, "tun_condvar");
780 sc->tun_flags = drv->tun_flags;
784 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
787 iflags = IFF_MULTICAST;
788 if ((sc->tun_flags & TUN_L2) != 0) {
790 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
793 iflags |= IFF_POINTOPOINT;
795 ifp = sc->tun_ifp = if_alloc(type);
797 panic("%s%d: failed to if_alloc() interface.\n",
798 drv->cdevsw.d_name, dev2unit(dev));
800 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
801 ifp->if_ioctl = tunifioctl;
802 ifp->if_flags = iflags;
803 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
804 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
805 ifp->if_capabilities |= IFCAP_LINKSTATE;
806 ifp->if_capenable |= IFCAP_LINKSTATE;
808 if ((sc->tun_flags & TUN_L2) != 0) {
809 ifp->if_mtu = ETHERMTU;
810 ifp->if_init = tunifinit;
811 ifp->if_start = tunstart_l2;
813 ether_gen_addr(ifp, &eaddr);
814 ether_ifattach(ifp, eaddr.octet);
816 ifp->if_mtu = TUNMTU;
817 ifp->if_start = tunstart;
818 ifp->if_output = tunoutput;
820 ifp->if_snd.ifq_drv_maxlen = 0;
821 IFQ_SET_READY(&ifp->if_snd);
824 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
829 sc->tun_flags |= TUN_INITED;
832 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
833 ifp->if_xname, dev2unit(dev));
837 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
840 struct tuntap_driver *drv;
841 struct tuntap_softc *tp;
845 CURVNET_SET(TD_TO_VNET(td));
846 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
849 return (error); /* Shouldn't happen */
852 if ((tunflags & TUN_L2) != 0) {
854 if (tap_allow_uopen == 0) {
855 error = priv_check(td, PRIV_NET_TAP);
864 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
869 drv = tuntap_driver_from_flags(tunflags);
879 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
887 if ((tp->tun_flags & TUN_L2) != 0) {
888 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
889 sizeof(tp->tun_ether.octet));
891 ifp->if_drv_flags |= IFF_DRV_RUNNING;
892 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
895 ifp->if_flags |= IFF_UP;
898 tp->tun_pid = td->td_proc->p_pid;
899 tp->tun_flags |= TUN_OPEN;
901 if_link_state_change(ifp, LINK_STATE_UP);
902 TUNDEBUG(ifp, "open\n");
909 * tunclose - close the device - mark i/f down & delete
913 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
915 struct tuntap_softc *tp;
924 * Simply close the device if this isn't the controlling process. This
925 * may happen if, for instance, the tunnel has been handed off to
926 * another process. The original controller should be able to close it
927 * without putting us into an inconsistent state.
929 if (td->td_proc->p_pid != tp->tun_pid) {
935 * junk all pending output
937 CURVNET_SET(ifp->if_vnet);
940 if ((tp->tun_flags & TUN_L2) != 0) {
942 IF_DRAIN(&ifp->if_snd);
944 IFQ_PURGE(&ifp->if_snd);
947 /* For vmnet, we won't do most of the address/route bits */
948 if ((tp->tun_flags & TUN_VMNET) != 0)
951 if (ifp->if_flags & IFF_UP) {
957 /* Delete all addresses and routes which reference this interface. */
958 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
961 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
963 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
964 /* deal w/IPv4 PtP destination; unlocked read */
965 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
966 rtinit(ifa, (int)RTM_DELETE,
967 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
969 rtinit(ifa, (int)RTM_DELETE, 0);
977 if_link_state_change(ifp, LINK_STATE_DOWN);
980 funsetown(&tp->tun_sigio);
981 selwakeuppri(&tp->tun_rsel, PZERO + 1);
982 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
983 TUNDEBUG (ifp, "closed\n");
984 tp->tun_flags &= ~TUN_OPEN;
987 cv_broadcast(&tp->tun_cv);
993 tuninit(struct ifnet *ifp)
995 struct tuntap_softc *tp = ifp->if_softc;
1000 TUNDEBUG(ifp, "tuninit\n");
1003 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1004 if ((tp->tun_flags & TUN_L2) == 0) {
1005 ifp->if_flags |= IFF_UP;
1006 getmicrotime(&ifp->if_lastchange);
1009 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1010 if (ifa->ifa_addr->sa_family == AF_INET) {
1011 struct sockaddr_in *si;
1013 si = (struct sockaddr_in *)ifa->ifa_addr;
1014 if (si->sin_addr.s_addr)
1015 tp->tun_flags |= TUN_IASET;
1017 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1018 if (si && si->sin_addr.s_addr)
1019 tp->tun_flags |= TUN_DSTADDR;
1022 if_addr_runlock(ifp);
1026 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1028 /* attempt to start output */
1035 * Used only for l2 tunnel.
1038 tunifinit(void *xtp)
1040 struct tuntap_softc *tp;
1042 tp = (struct tuntap_softc *)xtp;
1043 tuninit(tp->tun_ifp);
1047 * Process an ioctl request.
1050 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1052 struct ifreq *ifr = (struct ifreq *)data;
1053 struct tuntap_softc *tp;
1055 struct ifmediareq *ifmr;
1056 int dummy, error = 0;
1060 sx_xlock(&tun_ioctl_sx);
1066 l2tun = (tp->tun_flags & TUN_L2) != 0;
1069 ifs = (struct ifstat *)data;
1072 snprintf(ifs->ascii, sizeof(ifs->ascii),
1073 "\tOpened by PID %d\n", tp->tun_pid);
1075 ifs->ascii[0] = '\0';
1080 error = ether_ioctl(ifp, cmd, data);
1084 TUNDEBUG(ifp, "address set\n");
1087 ifp->if_mtu = ifr->ifr_mtu;
1088 TUNDEBUG(ifp, "mtu set\n");
1100 ifmr = (struct ifmediareq *)data;
1101 dummy = ifmr->ifm_count;
1102 ifmr->ifm_count = 1;
1103 ifmr->ifm_status = IFM_AVALID;
1104 ifmr->ifm_active = IFM_ETHER;
1105 if (tp->tun_flags & TUN_OPEN)
1106 ifmr->ifm_status |= IFM_ACTIVE;
1107 ifmr->ifm_current = ifmr->ifm_active;
1109 int media = IFM_ETHER;
1110 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1115 error = ether_ioctl(ifp, cmd, data);
1121 sx_xunlock(&tun_ioctl_sx);
1126 * tunoutput - queue packets from higher level ready to put out.
1129 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1132 struct tuntap_softc *tp = ifp->if_softc;
1133 u_short cached_tun_flags;
1137 TUNDEBUG (ifp, "tunoutput\n");
1140 error = mac_ifnet_check_transmit(ifp, m0);
1147 /* Could be unlocked read? */
1149 cached_tun_flags = tp->tun_flags;
1151 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1152 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1157 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1162 /* BPF writes need to be handled specially. */
1163 if (dst->sa_family == AF_UNSPEC)
1164 bcopy(dst->sa_data, &af, sizeof(af));
1166 af = dst->sa_family;
1168 if (bpf_peers_present(ifp->if_bpf))
1169 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1171 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1172 if (cached_tun_flags & TUN_LMODE) {
1173 /* allocate space for sockaddr */
1174 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1176 /* if allocation failed drop packet */
1178 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1179 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1182 bcopy(dst, m0->m_data, dst->sa_len);
1186 if (cached_tun_flags & TUN_IFHEAD) {
1187 /* Prepend the address family */
1188 M_PREPEND(m0, 4, M_NOWAIT);
1190 /* if allocation failed drop packet */
1192 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1193 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1196 *(u_int32_t *)m0->m_data = htonl(af);
1203 return (EAFNOSUPPORT);
1207 error = (ifp->if_transmit)(ifp, m0);
1210 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1215 * the cdevsw interface is now pretty minimal.
1218 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1221 struct ifreq ifr, *ifrp;
1222 struct tuntap_softc *tp = dev->si_drv1;
1223 struct tuninfo *tunp;
1225 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1226 defined(COMPAT_FREEBSD4)
1231 l2tun = (tp->tun_flags & TUN_L2) != 0;
1233 /* tap specific ioctls */
1236 ifrp = (struct ifreq *)data;
1237 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname,
1241 /* VMware/VMnet port ioctl's */
1242 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1243 defined(COMPAT_FREEBSD4)
1245 ival = IOCPARM_IVAL(data);
1246 data = (caddr_t)&ival;
1249 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1250 iflags = *(int *)data;
1251 iflags &= TUN_VMIO_FLAG_MASK;
1252 iflags &= ~IFF_CANTCHANGE;
1256 TUN2IFP(tp)->if_flags = iflags |
1257 (TUN2IFP(tp)->if_flags & IFF_CANTCHANGE);
1261 case SIOCGIFADDR: /* get MAC address of the remote side */
1263 bcopy(&tp->tun_ether.octet, data,
1264 sizeof(tp->tun_ether.octet));
1268 case SIOCSIFADDR: /* set MAC address of the remote side */
1270 bcopy(data, &tp->tun_ether.octet,
1271 sizeof(tp->tun_ether.octet));
1277 /* Fall through to the common ioctls if unhandled */
1283 tp->tun_flags |= TUN_LMODE;
1284 tp->tun_flags &= ~TUN_IFHEAD;
1286 tp->tun_flags &= ~TUN_LMODE;
1293 tp->tun_flags |= TUN_IFHEAD;
1294 tp->tun_flags &= ~TUN_LMODE;
1296 tp->tun_flags &= ~TUN_IFHEAD;
1302 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1307 /* deny this if UP */
1308 if (TUN2IFP(tp)->if_flags & IFF_UP)
1311 switch (*(int *)data & ~IFF_MULTICAST) {
1312 case IFF_POINTOPOINT:
1315 TUN2IFP(tp)->if_flags &=
1316 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1317 TUN2IFP(tp)->if_flags |= *(int *)data;
1328 tp->tun_pid = curthread->td_proc->p_pid;
1333 /* Fall through to the common ioctls if unhandled */
1338 tunp = (struct tuninfo *)data;
1339 if (TUN2IFP(tp)->if_type != tunp->type)
1340 return (EPROTOTYPE);
1342 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1343 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1344 ifr.ifr_mtu = tunp->mtu;
1345 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1346 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1354 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1358 tunp = (struct tuninfo *)data;
1360 tunp->mtu = TUN2IFP(tp)->if_mtu;
1361 tunp->type = TUN2IFP(tp)->if_type;
1362 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1366 tundebug = *(int *)data;
1369 *(int *)data = tundebug;
1376 tp->tun_flags |= TUN_ASYNC;
1378 tp->tun_flags &= ~TUN_ASYNC;
1382 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1384 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1385 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1386 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1387 *(int *)data += mb->m_len;
1388 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1393 return (fsetown(*(int *)data, &tp->tun_sigio));
1396 *(int *)data = fgetown(&tp->tun_sigio);
1399 /* This is deprecated, FIOSETOWN should be used instead. */
1401 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1403 /* This is deprecated, FIOGETOWN should be used instead. */
1405 *(int *)data = -fgetown(&tp->tun_sigio);
1415 * The cdevsw read interface - reads a packet at a time, or at
1416 * least as much of a packet as can be read.
1419 tunread(struct cdev *dev, struct uio *uio, int flag)
1421 struct tuntap_softc *tp = dev->si_drv1;
1422 struct ifnet *ifp = TUN2IFP(tp);
1426 TUNDEBUG (ifp, "read\n");
1428 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1430 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1434 tp->tun_flags &= ~TUN_RWAIT;
1437 IFQ_DEQUEUE(&ifp->if_snd, m);
1439 if (flag & O_NONBLOCK) {
1441 return (EWOULDBLOCK);
1443 tp->tun_flags |= TUN_RWAIT;
1444 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1451 } while (m == NULL);
1454 if ((tp->tun_flags & TUN_L2) != 0)
1457 while (m && uio->uio_resid > 0 && error == 0) {
1458 len = min(uio->uio_resid, m->m_len);
1460 error = uiomove(mtod(m, void *), len, uio);
1465 TUNDEBUG(ifp, "Dropping mbuf\n");
1472 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m)
1474 struct ether_header *eh;
1480 * Only pass a unicast frame to ether_input(), if it would
1481 * actually have been received by non-virtual hardware.
1483 if (m->m_len < sizeof(struct ether_header)) {
1488 eh = mtod(m, struct ether_header *);
1490 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1491 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1492 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1497 /* Pass packet up to parent. */
1498 CURVNET_SET(ifp->if_vnet);
1499 (*ifp->if_input)(ifp, m);
1501 /* ibytes are counted in parent */
1502 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1507 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1513 /* Could be unlocked read? */
1515 if (tp->tun_flags & TUN_IFHEAD) {
1517 if (m->m_len < sizeof(family) &&
1518 (m = m_pullup(m, sizeof(family))) == NULL)
1520 family = ntohl(*mtod(m, u_int32_t *));
1521 m_adj(m, sizeof(family));
1527 BPF_MTAP2(ifp, &family, sizeof(family), m);
1542 return (EAFNOSUPPORT);
1544 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1545 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1546 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1547 CURVNET_SET(ifp->if_vnet);
1548 M_SETFIB(m, ifp->if_fib);
1549 netisr_dispatch(isr, m);
1555 * the cdevsw write interface - an atomic write is a packet - or else!
1558 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1560 struct tuntap_softc *tp;
1569 TUNDEBUG(ifp, "tunwrite\n");
1570 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1571 /* ignore silently */
1574 if (uio->uio_resid == 0)
1577 l2tun = (tp->tun_flags & TUN_L2) != 0;
1579 mru = l2tun ? TAPMRU : TUNMRU;
1581 align = ETHER_ALIGN;
1582 else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1583 mru += sizeof(uint32_t); /* family */
1584 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1585 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1589 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1590 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1594 m->m_pkthdr.rcvif = ifp;
1596 mac_ifnet_create_mbuf(ifp, m);
1600 return (tunwrite_l2(tp, m));
1602 return (tunwrite_l3(tp, m));
1606 * tunpoll - the poll interface, this is only useful on reads
1607 * really. The write detect always returns true, write never blocks
1608 * anyway, it either accepts the packet or drops it.
1611 tunpoll(struct cdev *dev, int events, struct thread *td)
1613 struct tuntap_softc *tp = dev->si_drv1;
1614 struct ifnet *ifp = TUN2IFP(tp);
1617 TUNDEBUG(ifp, "tunpoll\n");
1619 if (events & (POLLIN | POLLRDNORM)) {
1620 IFQ_LOCK(&ifp->if_snd);
1621 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1622 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1623 revents |= events & (POLLIN | POLLRDNORM);
1625 TUNDEBUG(ifp, "tunpoll waiting\n");
1626 selrecord(td, &tp->tun_rsel);
1628 IFQ_UNLOCK(&ifp->if_snd);
1630 if (events & (POLLOUT | POLLWRNORM))
1631 revents |= events & (POLLOUT | POLLWRNORM);
1637 * tunkqfilter - support for the kevent() system call.
1640 tunkqfilter(struct cdev *dev, struct knote *kn)
1642 struct tuntap_softc *tp = dev->si_drv1;
1643 struct ifnet *ifp = TUN2IFP(tp);
1645 switch(kn->kn_filter) {
1647 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1648 ifp->if_xname, dev2unit(dev));
1649 kn->kn_fop = &tun_read_filterops;
1653 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1654 ifp->if_xname, dev2unit(dev));
1655 kn->kn_fop = &tun_write_filterops;
1659 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1660 ifp->if_xname, dev2unit(dev));
1665 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1671 * Return true of there is data in the interface queue.
1674 tunkqread(struct knote *kn, long hint)
1677 struct tuntap_softc *tp = kn->kn_hook;
1678 struct cdev *dev = tp->tun_dev;
1679 struct ifnet *ifp = TUN2IFP(tp);
1681 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1683 "%s have data in the queue. Len = %d, minor = %#x\n",
1684 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1688 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1697 * Always can write, always return MTU in kn->data.
1700 tunkqwrite(struct knote *kn, long hint)
1702 struct tuntap_softc *tp = kn->kn_hook;
1703 struct ifnet *ifp = TUN2IFP(tp);
1705 kn->kn_data = ifp->if_mtu;
1711 tunkqdetach(struct knote *kn)
1713 struct tuntap_softc *tp = kn->kn_hook;
1715 knlist_remove(&tp->tun_rsel.si_note, kn, 0);