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/if_vlan_var.h>
88 #include <net/netisr.h>
89 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet/udp.h>
97 #include <netinet/tcp.h>
100 #include <net/if_tap.h>
101 #include <net/if_tun.h>
103 #include <dev/virtio/network/virtio_net.h>
105 #include <sys/queue.h>
106 #include <sys/condvar.h>
107 #include <security/mac/mac_framework.h>
109 struct tuntap_driver;
112 * tun_list is protected by global tunmtx. Other mutable fields are
113 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
114 * static for the duration of a tunnel interface.
116 struct tuntap_softc {
117 TAILQ_ENTRY(tuntap_softc) tun_list;
118 struct cdev *tun_alias;
119 struct cdev *tun_dev;
120 u_short tun_flags; /* misc flags */
121 #define TUN_OPEN 0x0001
122 #define TUN_INITED 0x0002
123 #define TUN_UNUSED1 0x0008
124 #define TUN_DSTADDR 0x0010
125 #define TUN_LMODE 0x0020
126 #define TUN_RWAIT 0x0040
127 #define TUN_ASYNC 0x0080
128 #define TUN_IFHEAD 0x0100
129 #define TUN_DYING 0x0200
130 #define TUN_L2 0x0400
131 #define TUN_VMNET 0x0800
133 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
134 #define TUN_READY (TUN_OPEN | TUN_INITED)
136 pid_t tun_pid; /* owning pid */
137 struct ifnet *tun_ifp; /* the interface */
138 struct sigio *tun_sigio; /* async I/O info */
139 struct tuntap_driver *tun_drv; /* appropriate driver */
140 struct selinfo tun_rsel; /* read select */
141 struct mtx tun_mtx; /* softc field mutex */
142 struct cv tun_cv; /* for ref'd dev destroy */
143 struct ether_addr tun_ether; /* remote address */
144 int tun_busy; /* busy count */
145 int tun_vhdrlen; /* virtio-net header length */
147 #define TUN2IFP(sc) ((sc)->tun_ifp)
149 #define TUNDEBUG if (tundebug) if_printf
151 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
152 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
153 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
155 #define TUN_VMIO_FLAG_MASK 0x0fff
158 * Interface capabilities of a tap device that supports the virtio-net
161 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
162 | IFCAP_VLAN_HWCSUM \
163 | IFCAP_TSO | IFCAP_LRO \
166 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
167 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
171 * All mutable global variables in if_tun are locked using tunmtx, with
172 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
173 * which are static after setup.
175 static struct mtx tunmtx;
176 static eventhandler_tag arrival_tag;
177 static eventhandler_tag clone_tag;
178 static const char tunname[] = "tun";
179 static const char tapname[] = "tap";
180 static const char vmnetname[] = "vmnet";
181 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
182 static int tundebug = 0;
183 static int tundclone = 1;
184 static int tap_allow_uopen = 0; /* allow user devfs cloning */
185 static int tapuponopen = 0; /* IFF_UP on open() */
186 static int tapdclone = 1; /* enable devfs cloning */
188 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
189 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
191 static struct sx tun_ioctl_sx;
192 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
194 SYSCTL_DECL(_net_link);
196 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
197 "IP tunnel software network interface");
198 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
199 "Enable legacy devfs interface creation");
202 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
203 "Ethernet tunnel software network interface");
204 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
205 "Enable legacy devfs interface creation for all users");
206 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
207 "Bring interface up when /dev/tap is opened");
208 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
209 "Enable legacy devfs interface creation");
210 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
212 static int tun_create_device(struct tuntap_driver *drv, int unit,
213 struct ucred *cr, struct cdev **dev, const char *name);
214 static int tun_busy_locked(struct tuntap_softc *tp);
215 static void tun_unbusy_locked(struct tuntap_softc *tp);
216 static int tun_busy(struct tuntap_softc *tp);
217 static void tun_unbusy(struct tuntap_softc *tp);
219 static int tuntap_name2info(const char *name, int *unit, int *flags);
220 static void tunclone(void *arg, struct ucred *cred, char *name,
221 int namelen, struct cdev **dev);
222 static void tuncreate(struct cdev *dev);
223 static void tundtor(void *data);
224 static void tunrename(void *arg, struct ifnet *ifp);
225 static int tunifioctl(struct ifnet *, u_long, caddr_t);
226 static void tuninit(struct ifnet *);
227 static void tunifinit(void *xtp);
228 static int tuntapmodevent(module_t, int, void *);
229 static int tunoutput(struct ifnet *, struct mbuf *,
230 const struct sockaddr *, struct route *ro);
231 static void tunstart(struct ifnet *);
232 static void tunstart_l2(struct ifnet *);
234 static int tun_clone_match(struct if_clone *ifc, const char *name);
235 static int tap_clone_match(struct if_clone *ifc, const char *name);
236 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
237 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
238 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
239 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
241 static d_open_t tunopen;
242 static d_read_t tunread;
243 static d_write_t tunwrite;
244 static d_ioctl_t tunioctl;
245 static d_poll_t tunpoll;
246 static d_kqfilter_t tunkqfilter;
248 static int tunkqread(struct knote *, long);
249 static int tunkqwrite(struct knote *, long);
250 static void tunkqdetach(struct knote *);
252 static struct filterops tun_read_filterops = {
255 .f_detach = tunkqdetach,
256 .f_event = tunkqread,
259 static struct filterops tun_write_filterops = {
262 .f_detach = tunkqdetach,
263 .f_event = tunkqwrite,
266 static struct tuntap_driver {
267 struct cdevsw cdevsw;
269 struct unrhdr *unrhdr;
270 struct clonedevs *clones;
271 ifc_match_t *clone_match_fn;
272 ifc_create_t *clone_create_fn;
273 ifc_destroy_t *clone_destroy_fn;
274 } tuntap_drivers[] = {
278 .d_version = D_VERSION,
279 .d_flags = D_NEEDMINOR,
285 .d_kqfilter = tunkqfilter,
288 .clone_match_fn = tun_clone_match,
289 .clone_create_fn = tun_clone_create,
290 .clone_destroy_fn = tun_clone_destroy,
293 .ident_flags = TUN_L2,
295 .d_version = D_VERSION,
296 .d_flags = D_NEEDMINOR,
302 .d_kqfilter = tunkqfilter,
305 .clone_match_fn = tap_clone_match,
306 .clone_create_fn = tun_clone_create,
307 .clone_destroy_fn = tun_clone_destroy,
310 .ident_flags = TUN_L2 | TUN_VMNET,
312 .d_version = D_VERSION,
313 .d_flags = D_NEEDMINOR,
319 .d_kqfilter = tunkqfilter,
322 .clone_match_fn = vmnet_clone_match,
323 .clone_create_fn = tun_clone_create,
324 .clone_destroy_fn = tun_clone_destroy,
328 struct tuntap_driver_cloner {
329 SLIST_ENTRY(tuntap_driver_cloner) link;
330 struct tuntap_driver *drv;
331 struct if_clone *cloner;
334 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
335 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
337 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
340 * Mechanism for marking a tunnel device as busy so that we can safely do some
341 * orthogonal operations (such as operations on devices) without racing against
342 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
343 * open, to be woken up when the condition is alleviated.
346 tun_busy_locked(struct tuntap_softc *tp)
350 if ((tp->tun_flags & TUN_DYING) != 0) {
352 * Perhaps unintuitive, but the device is busy going away.
353 * Other interpretations of EBUSY from tun_busy make little
354 * sense, since making a busy device even more busy doesn't
355 * sound like a problem.
365 tun_unbusy_locked(struct tuntap_softc *tp)
369 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
372 /* Wake up anything that may be waiting on our busy tunnel. */
373 if (tp->tun_busy == 0)
374 cv_broadcast(&tp->tun_cv);
378 tun_busy(struct tuntap_softc *tp)
383 ret = tun_busy_locked(tp);
390 tun_unbusy(struct tuntap_softc *tp)
394 tun_unbusy_locked(tp);
399 * Sets unit and/or flags given the device name. Must be called with correct
403 tuntap_name2info(const char *name, int *outunit, int *outflags)
405 struct tuntap_driver *drv;
406 struct tuntap_driver_cloner *drvc;
415 * Needed for dev_stdclone, but dev_stdclone will not modify, it just
416 * wants to be able to pass back a char * through the second param. We
417 * will always set that as NULL here, so we'll fake it.
419 dname = __DECONST(char *, name);
422 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
423 ("tuntap_driver_cloners failed to initialize"));
424 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
425 KASSERT(drvc->drv != NULL,
426 ("tuntap_driver_cloners entry not properly initialized"));
429 if (strcmp(name, drv->cdevsw.d_name) == 0) {
432 flags = drv->ident_flags;
436 if (dev_stdclone(dname, NULL, drv->cdevsw.d_name, &unit) == 1) {
438 flags = drv->ident_flags;
448 if (outflags != NULL)
454 * Get driver information from a set of flags specified. Masks the identifying
455 * part of the flags and compares it against all of the available
456 * tuntap_drivers. Must be called with correct vnet context.
458 static struct tuntap_driver *
459 tuntap_driver_from_flags(int tun_flags)
461 struct tuntap_driver *drv;
462 struct tuntap_driver_cloner *drvc;
464 KASSERT(!SLIST_EMPTY(&V_tuntap_driver_cloners),
465 ("tuntap_driver_cloners failed to initialize"));
466 SLIST_FOREACH(drvc, &V_tuntap_driver_cloners, link) {
467 KASSERT(drvc->drv != NULL,
468 ("tuntap_driver_cloners entry not properly initialized"));
470 if ((tun_flags & TUN_DRIVER_IDENT_MASK) == drv->ident_flags)
480 tun_clone_match(struct if_clone *ifc, const char *name)
484 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
485 if ((tunflags & TUN_L2) == 0)
493 tap_clone_match(struct if_clone *ifc, const char *name)
497 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
498 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
506 vmnet_clone_match(struct if_clone *ifc, const char *name)
510 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
511 if ((tunflags & TUN_VMNET) != 0)
519 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
521 struct tuntap_driver *drv;
523 int err, i, tunflags, unit;
526 /* The name here tells us exactly what we're creating */
527 err = tuntap_name2info(name, &unit, &tunflags);
531 drv = tuntap_driver_from_flags(tunflags);
536 /* If this unit number is still available that's okay. */
537 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
540 unit = alloc_unr(drv->unrhdr);
543 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
545 /* find any existing device, or allocate new unit number */
547 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
548 /* No preexisting struct cdev *, create one */
550 i = tun_create_device(drv, unit, NULL, &dev, name);
558 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
561 char devname[SPECNAMELEN + 1];
562 struct tuntap_driver *drv;
563 int append_unit, i, u, tunflags;
570 CURVNET_SET(CRED_TO_VNET(cred));
571 if (tuntap_name2info(name, &u, &tunflags) != 0)
572 goto out; /* Not recognized */
574 if (u != -1 && u > IF_MAXUNIT)
575 goto out; /* Unit number too high */
577 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
578 if ((tunflags & TUN_L2) != 0) {
579 /* tap/vmnet allow user open with a sysctl */
580 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
582 mayclone = mayclone && tundclone;
586 * If tun cloning is enabled, only the superuser can create an
597 drv = tuntap_driver_from_flags(tunflags);
601 /* find any existing device, or allocate new unit number */
602 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
605 namelen = snprintf(devname, sizeof(devname), "%s%d",
610 i = tun_create_device(drv, u, cred, dev, name);
613 if_clone_create(name, namelen, NULL);
619 tun_destroy(struct tuntap_softc *tp)
623 tp->tun_flags |= TUN_DYING;
624 if (tp->tun_busy != 0)
625 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
629 CURVNET_SET(TUN2IFP(tp)->if_vnet);
631 /* destroy_dev will take care of any alias. */
632 destroy_dev(tp->tun_dev);
633 seldrain(&tp->tun_rsel);
634 knlist_clear(&tp->tun_rsel.si_note, 0);
635 knlist_destroy(&tp->tun_rsel.si_note);
636 if ((tp->tun_flags & TUN_L2) != 0) {
637 ether_ifdetach(TUN2IFP(tp));
639 bpfdetach(TUN2IFP(tp));
640 if_detach(TUN2IFP(tp));
642 sx_xlock(&tun_ioctl_sx);
643 TUN2IFP(tp)->if_softc = NULL;
644 sx_xunlock(&tun_ioctl_sx);
645 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
646 if_free(TUN2IFP(tp));
647 mtx_destroy(&tp->tun_mtx);
648 cv_destroy(&tp->tun_cv);
654 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
656 struct tuntap_softc *tp = ifp->if_softc;
659 TAILQ_REMOVE(&tunhead, tp, tun_list);
667 vnet_tun_init(const void *unused __unused)
669 struct tuntap_driver *drv;
670 struct tuntap_driver_cloner *drvc;
673 for (i = 0; i < nitems(tuntap_drivers); ++i) {
674 drv = &tuntap_drivers[i];
675 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
678 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
679 drv->clone_match_fn, drv->clone_create_fn,
680 drv->clone_destroy_fn);
681 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
684 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
685 vnet_tun_init, NULL);
688 vnet_tun_uninit(const void *unused __unused)
690 struct tuntap_driver_cloner *drvc;
692 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
693 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
694 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
696 if_clone_detach(drvc->cloner);
700 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
701 vnet_tun_uninit, NULL);
704 tun_uninit(const void *unused __unused)
706 struct tuntap_driver *drv;
707 struct tuntap_softc *tp;
710 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
711 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
712 drain_dev_clone_events();
715 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
716 TAILQ_REMOVE(&tunhead, tp, tun_list);
722 for (i = 0; i < nitems(tuntap_drivers); ++i) {
723 drv = &tuntap_drivers[i];
724 delete_unrhdr(drv->unrhdr);
725 clone_cleanup(&drv->clones);
727 mtx_destroy(&tunmtx);
729 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
731 static struct tuntap_driver *
732 tuntap_driver_from_ifnet(const struct ifnet *ifp)
734 struct tuntap_driver *drv;
740 for (i = 0; i < nitems(tuntap_drivers); ++i) {
741 drv = &tuntap_drivers[i];
742 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
750 tuntapmodevent(module_t mod, int type, void *data)
752 struct tuntap_driver *drv;
757 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
758 for (i = 0; i < nitems(tuntap_drivers); ++i) {
759 drv = &tuntap_drivers[i];
760 clone_setup(&drv->clones);
761 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
763 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
765 if (arrival_tag == NULL)
767 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
768 if (clone_tag == NULL)
772 /* See tun_uninit, so it's done after the vnet_sysuninit() */
780 static moduledata_t tuntap_mod = {
786 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
787 MODULE_VERSION(if_tuntap, 1);
788 MODULE_VERSION(if_tun, 1);
789 MODULE_VERSION(if_tap, 1);
792 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
793 struct cdev **dev, const char *name)
795 struct make_dev_args args;
796 struct tuntap_softc *tp;
799 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
800 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
801 cv_init(&tp->tun_cv, "tun_condvar");
802 tp->tun_flags = drv->ident_flags;
805 make_dev_args_init(&args);
807 args.mda_flags = MAKEDEV_REF;
808 args.mda_devsw = &drv->cdevsw;
810 args.mda_uid = UID_UUCP;
811 args.mda_gid = GID_DIALER;
812 args.mda_mode = 0600;
813 args.mda_unit = unit;
814 args.mda_si_drv1 = tp;
815 error = make_dev_s(&args, dev, "%s", name);
821 KASSERT((*dev)->si_drv1 != NULL,
822 ("Failed to set si_drv1 at %s creation", name));
824 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
826 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
832 tunstart(struct ifnet *ifp)
834 struct tuntap_softc *tp = ifp->if_softc;
837 TUNDEBUG(ifp, "starting\n");
838 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
839 IFQ_LOCK(&ifp->if_snd);
840 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
842 IFQ_UNLOCK(&ifp->if_snd);
845 IFQ_UNLOCK(&ifp->if_snd);
849 if (tp->tun_flags & TUN_RWAIT) {
850 tp->tun_flags &= ~TUN_RWAIT;
853 selwakeuppri(&tp->tun_rsel, PZERO + 1);
854 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
855 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
857 pgsigio(&tp->tun_sigio, SIGIO, 0);
865 * queue packets from higher level ready to put out
868 tunstart_l2(struct ifnet *ifp)
870 struct tuntap_softc *tp = ifp->if_softc;
872 TUNDEBUG(ifp, "starting\n");
875 * do not junk pending output if we are in VMnet mode.
876 * XXX: can this do any harm because of queue overflow?
880 if (((tp->tun_flags & TUN_VMNET) == 0) &&
881 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
885 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
888 IF_DEQUEUE(&ifp->if_snd, m);
891 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
900 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
902 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
903 if (tp->tun_flags & TUN_RWAIT) {
904 tp->tun_flags &= ~TUN_RWAIT;
908 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
910 pgsigio(&tp->tun_sigio, SIGIO, 0);
914 selwakeuppri(&tp->tun_rsel, PZERO+1);
915 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
916 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
919 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
923 /* XXX: should return an error code so it can fail. */
925 tuncreate(struct cdev *dev)
927 struct tuntap_driver *drv;
928 struct tuntap_softc *tp;
930 struct ether_addr eaddr;
936 ("si_drv1 should have been initialized at creation"));
939 iflags = IFF_MULTICAST;
940 if ((tp->tun_flags & TUN_L2) != 0) {
942 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
945 iflags |= IFF_POINTOPOINT;
947 ifp = tp->tun_ifp = if_alloc(type);
949 panic("%s%d: failed to if_alloc() interface.\n",
950 drv->cdevsw.d_name, dev2unit(dev));
952 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
953 ifp->if_ioctl = tunifioctl;
954 ifp->if_flags = iflags;
955 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
956 ifp->if_capabilities |= IFCAP_LINKSTATE;
957 ifp->if_capenable |= IFCAP_LINKSTATE;
959 if ((tp->tun_flags & TUN_L2) != 0) {
960 ifp->if_mtu = ETHERMTU;
961 ifp->if_init = tunifinit;
962 ifp->if_start = tunstart_l2;
964 ether_gen_addr(ifp, &eaddr);
965 ether_ifattach(ifp, eaddr.octet);
967 ifp->if_mtu = TUNMTU;
968 ifp->if_start = tunstart;
969 ifp->if_output = tunoutput;
971 ifp->if_snd.ifq_drv_maxlen = 0;
972 IFQ_SET_READY(&ifp->if_snd);
975 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
979 tp->tun_flags |= TUN_INITED;
982 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
983 ifp->if_xname, dev2unit(dev));
987 tunrename(void *arg __unused, struct ifnet *ifp)
989 struct tuntap_softc *tp;
992 if ((ifp->if_flags & IFF_RENAMING) == 0)
995 if (tuntap_driver_from_ifnet(ifp) == NULL)
999 * We need to grab the ioctl sx long enough to make sure the softc is
1000 * still there. If it is, we can safely try to busy the tun device.
1001 * The busy may fail if the device is currently dying, in which case
1002 * we do nothing. If it doesn't fail, the busy count stops the device
1003 * from dying until we've created the alias (that will then be
1004 * subsequently destroyed).
1006 sx_xlock(&tun_ioctl_sx);
1009 sx_xunlock(&tun_ioctl_sx);
1012 error = tun_busy(tp);
1013 sx_xunlock(&tun_ioctl_sx);
1016 if (tp->tun_alias != NULL) {
1017 destroy_dev(tp->tun_alias);
1018 tp->tun_alias = NULL;
1021 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1025 * Failure's ok, aliases are created on a best effort basis. If a
1026 * tun user/consumer decides to rename the interface to conflict with
1027 * another device (non-ifnet) on the system, we will assume they know
1028 * what they are doing. make_dev_alias_p won't touch tun_alias on
1029 * failure, so we use it but ignore the return value.
1031 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1038 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1041 struct tuntap_softc *tp;
1042 int error, tunflags;
1045 CURVNET_SET(TD_TO_VNET(td));
1046 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1049 return (error); /* Shouldn't happen */
1054 ("si_drv1 should have been initialized at creation"));
1057 if ((tp->tun_flags & TUN_INITED) == 0) {
1062 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1068 error = tun_busy_locked(tp);
1069 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1072 if ((tp->tun_flags & TUN_L2) != 0) {
1073 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1074 sizeof(tp->tun_ether.octet));
1076 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1077 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1080 ifp->if_flags |= IFF_UP;
1083 tp->tun_pid = td->td_proc->p_pid;
1084 tp->tun_flags |= TUN_OPEN;
1086 if_link_state_change(ifp, LINK_STATE_UP);
1087 TUNDEBUG(ifp, "open\n");
1091 * This can fail with either ENOENT or EBUSY. This is in the middle of
1092 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1093 * the only cdevpriv dtor being set will be tundtor and the softc being
1094 * passed is constant for a given cdev. We ignore the possible error
1095 * because of this as either "unlikely" or "not actually a problem."
1097 (void)devfs_set_cdevpriv(tp, tundtor);
1103 * tundtor - tear down the device - mark i/f down & delete
1110 struct tuntap_softc *tp;
1121 * Realistically, we can't be obstinate here. This only means that the
1122 * tuntap device was closed out of order, and the last closer wasn't the
1123 * controller. These are still good to know about, though, as software
1124 * should avoid multiple processes with a tuntap device open and
1125 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1128 if (p->p_pid != tp->tun_pid) {
1130 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1131 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1135 * junk all pending output
1137 CURVNET_SET(ifp->if_vnet);
1140 if ((tp->tun_flags & TUN_L2) != 0) {
1142 IF_DRAIN(&ifp->if_snd);
1144 IFQ_PURGE(&ifp->if_snd);
1147 /* For vmnet, we won't do most of the address/route bits */
1148 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1149 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1152 if (ifp->if_flags & IFF_UP) {
1158 /* Delete all addresses and routes which reference this interface. */
1159 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1162 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1164 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1165 /* deal w/IPv4 PtP destination; unlocked read */
1166 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1167 rtinit(ifa, (int)RTM_DELETE,
1168 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1170 rtinit(ifa, (int)RTM_DELETE, 0);
1178 if_link_state_change(ifp, LINK_STATE_DOWN);
1181 funsetown(&tp->tun_sigio);
1182 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1183 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1184 TUNDEBUG (ifp, "closed\n");
1185 tp->tun_flags &= ~TUN_OPEN;
1187 tun_vnethdr_set(ifp, 0);
1189 tun_unbusy_locked(tp);
1194 tuninit(struct ifnet *ifp)
1196 struct tuntap_softc *tp = ifp->if_softc;
1198 struct epoch_tracker et;
1202 TUNDEBUG(ifp, "tuninit\n");
1205 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1206 if ((tp->tun_flags & TUN_L2) == 0) {
1207 ifp->if_flags |= IFF_UP;
1208 getmicrotime(&ifp->if_lastchange);
1210 NET_EPOCH_ENTER(et);
1211 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1212 if (ifa->ifa_addr->sa_family == AF_INET) {
1213 struct sockaddr_in *si;
1215 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1216 if (si && si->sin_addr.s_addr) {
1217 tp->tun_flags |= TUN_DSTADDR;
1226 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1228 /* attempt to start output */
1235 * Used only for l2 tunnel.
1238 tunifinit(void *xtp)
1240 struct tuntap_softc *tp;
1242 tp = (struct tuntap_softc *)xtp;
1243 tuninit(tp->tun_ifp);
1247 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1248 * current value of ifp->if_capenable.
1251 tun_caps_changed(struct ifnet *ifp)
1253 uint64_t hwassist = 0;
1255 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1256 if (ifp->if_capenable & IFCAP_TXCSUM)
1257 hwassist |= CSUM_TCP | CSUM_UDP;
1258 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1259 hwassist |= CSUM_TCP_IPV6
1261 if (ifp->if_capenable & IFCAP_TSO4)
1262 hwassist |= CSUM_IP_TSO;
1263 if (ifp->if_capenable & IFCAP_TSO6)
1264 hwassist |= CSUM_IP6_TSO;
1265 ifp->if_hwassist = hwassist;
1269 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1270 * if_capabilities and if_capenable as needed.
1273 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1275 struct tuntap_softc *tp = ifp->if_softc;
1277 TUN_LOCK_ASSERT(tp);
1279 if (tp->tun_vhdrlen == vhdrlen)
1283 * Update if_capabilities to reflect the
1284 * functionalities offered by the virtio-net
1288 ifp->if_capabilities |=
1291 ifp->if_capabilities &=
1294 * Disable any capabilities that we don't
1297 ifp->if_capenable &= ifp->if_capabilities;
1298 tun_caps_changed(ifp);
1299 tp->tun_vhdrlen = vhdrlen;
1301 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1302 vhdrlen, ifp->if_capabilities);
1306 * Process an ioctl request.
1309 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1311 struct ifreq *ifr = (struct ifreq *)data;
1312 struct tuntap_softc *tp;
1314 struct ifmediareq *ifmr;
1315 int dummy, error = 0;
1319 sx_xlock(&tun_ioctl_sx);
1325 l2tun = (tp->tun_flags & TUN_L2) != 0;
1328 ifs = (struct ifstat *)data;
1331 snprintf(ifs->ascii, sizeof(ifs->ascii),
1332 "\tOpened by PID %d\n", tp->tun_pid);
1334 ifs->ascii[0] = '\0';
1339 error = ether_ioctl(ifp, cmd, data);
1343 TUNDEBUG(ifp, "address set\n");
1346 ifp->if_mtu = ifr->ifr_mtu;
1347 TUNDEBUG(ifp, "mtu set\n");
1359 ifmr = (struct ifmediareq *)data;
1360 dummy = ifmr->ifm_count;
1361 ifmr->ifm_count = 1;
1362 ifmr->ifm_status = IFM_AVALID;
1363 ifmr->ifm_active = IFM_ETHER;
1364 if (tp->tun_flags & TUN_OPEN)
1365 ifmr->ifm_status |= IFM_ACTIVE;
1366 ifmr->ifm_current = ifmr->ifm_active;
1368 int media = IFM_ETHER;
1369 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1374 ifp->if_capenable = ifr->ifr_reqcap;
1375 tun_caps_changed(ifp);
1377 VLAN_CAPABILITIES(ifp);
1381 error = ether_ioctl(ifp, cmd, data);
1387 sx_xunlock(&tun_ioctl_sx);
1392 * tunoutput - queue packets from higher level ready to put out.
1395 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1398 struct tuntap_softc *tp = ifp->if_softc;
1399 u_short cached_tun_flags;
1403 TUNDEBUG (ifp, "tunoutput\n");
1406 error = mac_ifnet_check_transmit(ifp, m0);
1413 /* Could be unlocked read? */
1415 cached_tun_flags = tp->tun_flags;
1417 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1418 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1423 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1428 /* BPF writes need to be handled specially. */
1429 if (dst->sa_family == AF_UNSPEC)
1430 bcopy(dst->sa_data, &af, sizeof(af));
1432 af = dst->sa_family;
1434 if (bpf_peers_present(ifp->if_bpf))
1435 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1437 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1438 if (cached_tun_flags & TUN_LMODE) {
1439 /* allocate space for sockaddr */
1440 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1442 /* if allocation failed drop packet */
1444 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1445 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1448 bcopy(dst, m0->m_data, dst->sa_len);
1452 if (cached_tun_flags & TUN_IFHEAD) {
1453 /* Prepend the address family */
1454 M_PREPEND(m0, 4, M_NOWAIT);
1456 /* if allocation failed drop packet */
1458 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1459 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1462 *(u_int32_t *)m0->m_data = htonl(af);
1469 return (EAFNOSUPPORT);
1473 error = (ifp->if_transmit)(ifp, m0);
1476 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1481 * the cdevsw interface is now pretty minimal.
1484 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1487 struct ifreq ifr, *ifrp;
1488 struct tuntap_softc *tp = dev->si_drv1;
1489 struct ifnet *ifp = TUN2IFP(tp);
1490 struct tuninfo *tunp;
1491 int error, iflags, ival;
1494 l2tun = (tp->tun_flags & TUN_L2) != 0;
1496 /* tap specific ioctls */
1498 /* VMware/VMnet port ioctl's */
1499 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1500 defined(COMPAT_FREEBSD4)
1502 ival = IOCPARM_IVAL(data);
1503 data = (caddr_t)&ival;
1506 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1507 iflags = *(int *)data;
1508 iflags &= TUN_VMIO_FLAG_MASK;
1509 iflags &= ~IFF_CANTCHANGE;
1513 ifp->if_flags = iflags |
1514 (ifp->if_flags & IFF_CANTCHANGE);
1518 case SIOCGIFADDR: /* get MAC address of the remote side */
1520 bcopy(&tp->tun_ether.octet, data,
1521 sizeof(tp->tun_ether.octet));
1525 case SIOCSIFADDR: /* set MAC address of the remote side */
1527 bcopy(data, &tp->tun_ether.octet,
1528 sizeof(tp->tun_ether.octet));
1533 ival = *(int *)data;
1535 ival != sizeof(struct virtio_net_hdr) &&
1536 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1540 tun_vnethdr_set(ifp, ival);
1546 *(int *)data = tp->tun_vhdrlen;
1552 /* Fall through to the common ioctls if unhandled */
1558 tp->tun_flags |= TUN_LMODE;
1559 tp->tun_flags &= ~TUN_IFHEAD;
1561 tp->tun_flags &= ~TUN_LMODE;
1568 tp->tun_flags |= TUN_IFHEAD;
1569 tp->tun_flags &= ~TUN_LMODE;
1571 tp->tun_flags &= ~TUN_IFHEAD;
1577 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1582 /* deny this if UP */
1583 if (TUN2IFP(tp)->if_flags & IFF_UP)
1586 switch (*(int *)data & ~IFF_MULTICAST) {
1587 case IFF_POINTOPOINT:
1590 TUN2IFP(tp)->if_flags &=
1591 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1592 TUN2IFP(tp)->if_flags |= *(int *)data;
1603 tp->tun_pid = curthread->td_proc->p_pid;
1608 /* Fall through to the common ioctls if unhandled */
1613 ifrp = (struct ifreq *)data;
1614 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1618 tunp = (struct tuninfo *)data;
1619 if (TUN2IFP(tp)->if_type != tunp->type)
1620 return (EPROTOTYPE);
1622 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1623 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1624 ifr.ifr_mtu = tunp->mtu;
1625 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1626 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1634 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1638 tunp = (struct tuninfo *)data;
1640 tunp->mtu = TUN2IFP(tp)->if_mtu;
1641 tunp->type = TUN2IFP(tp)->if_type;
1642 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1646 tundebug = *(int *)data;
1649 *(int *)data = tundebug;
1656 tp->tun_flags |= TUN_ASYNC;
1658 tp->tun_flags &= ~TUN_ASYNC;
1662 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1664 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1665 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1666 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1667 *(int *)data += mb->m_len;
1668 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1673 return (fsetown(*(int *)data, &tp->tun_sigio));
1676 *(int *)data = fgetown(&tp->tun_sigio);
1679 /* This is deprecated, FIOSETOWN should be used instead. */
1681 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1683 /* This is deprecated, FIOGETOWN should be used instead. */
1685 *(int *)data = -fgetown(&tp->tun_sigio);
1695 * The cdevsw read interface - reads a packet at a time, or at
1696 * least as much of a packet as can be read.
1699 tunread(struct cdev *dev, struct uio *uio, int flag)
1701 struct tuntap_softc *tp = dev->si_drv1;
1702 struct ifnet *ifp = TUN2IFP(tp);
1707 TUNDEBUG (ifp, "read\n");
1709 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1711 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1715 tp->tun_flags &= ~TUN_RWAIT;
1718 IFQ_DEQUEUE(&ifp->if_snd, m);
1721 if (flag & O_NONBLOCK) {
1723 return (EWOULDBLOCK);
1725 tp->tun_flags |= TUN_RWAIT;
1726 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1735 if ((tp->tun_flags & TUN_L2) != 0)
1738 len = min(tp->tun_vhdrlen, uio->uio_resid);
1740 struct virtio_net_hdr_mrg_rxbuf vhdr;
1742 bzero(&vhdr, sizeof(vhdr));
1743 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1744 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1747 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1748 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1749 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1750 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1751 vhdr.hdr.csum_offset);
1752 error = uiomove(&vhdr, len, uio);
1755 while (m && uio->uio_resid > 0 && error == 0) {
1756 len = min(uio->uio_resid, m->m_len);
1758 error = uiomove(mtod(m, void *), len, uio);
1763 TUNDEBUG(ifp, "Dropping mbuf\n");
1770 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1771 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1773 struct ether_header *eh;
1779 * Only pass a unicast frame to ether_input(), if it would
1780 * actually have been received by non-virtual hardware.
1782 if (m->m_len < sizeof(struct ether_header)) {
1787 eh = mtod(m, struct ether_header *);
1789 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1790 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1791 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1796 if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1801 /* Pass packet up to parent. */
1802 CURVNET_SET(ifp->if_vnet);
1803 (*ifp->if_input)(ifp, m);
1805 /* ibytes are counted in parent */
1806 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1811 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1813 struct epoch_tracker et;
1818 /* Could be unlocked read? */
1820 if (tp->tun_flags & TUN_IFHEAD) {
1822 if (m->m_len < sizeof(family) &&
1823 (m = m_pullup(m, sizeof(family))) == NULL)
1825 family = ntohl(*mtod(m, u_int32_t *));
1826 m_adj(m, sizeof(family));
1832 BPF_MTAP2(ifp, &family, sizeof(family), m);
1847 return (EAFNOSUPPORT);
1849 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1850 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1851 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1852 CURVNET_SET(ifp->if_vnet);
1853 M_SETFIB(m, ifp->if_fib);
1854 NET_EPOCH_ENTER(et);
1855 netisr_dispatch(isr, m);
1862 * the cdevsw write interface - an atomic write is a packet - or else!
1865 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1867 struct virtio_net_hdr_mrg_rxbuf vhdr;
1868 struct tuntap_softc *tp;
1872 int align, vhdrlen, error;
1877 TUNDEBUG(ifp, "tunwrite\n");
1878 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1879 /* ignore silently */
1882 if (uio->uio_resid == 0)
1885 l2tun = (tp->tun_flags & TUN_L2) != 0;
1886 mru = l2tun ? TAPMRU : TUNMRU;
1887 vhdrlen = tp->tun_vhdrlen;
1890 align = ETHER_ALIGN;
1892 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1893 mru += sizeof(uint32_t); /* family */
1894 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1895 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1900 error = uiomove(&vhdr, vhdrlen, uio);
1903 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1904 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1905 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1906 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1907 vhdr.hdr.csum_offset);
1910 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1911 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1915 m->m_pkthdr.rcvif = ifp;
1917 mac_ifnet_create_mbuf(ifp, m);
1921 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1923 return (tunwrite_l3(tp, m));
1927 * tunpoll - the poll interface, this is only useful on reads
1928 * really. The write detect always returns true, write never blocks
1929 * anyway, it either accepts the packet or drops it.
1932 tunpoll(struct cdev *dev, int events, struct thread *td)
1934 struct tuntap_softc *tp = dev->si_drv1;
1935 struct ifnet *ifp = TUN2IFP(tp);
1938 TUNDEBUG(ifp, "tunpoll\n");
1940 if (events & (POLLIN | POLLRDNORM)) {
1941 IFQ_LOCK(&ifp->if_snd);
1942 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1943 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1944 revents |= events & (POLLIN | POLLRDNORM);
1946 TUNDEBUG(ifp, "tunpoll waiting\n");
1947 selrecord(td, &tp->tun_rsel);
1949 IFQ_UNLOCK(&ifp->if_snd);
1951 revents |= events & (POLLOUT | POLLWRNORM);
1957 * tunkqfilter - support for the kevent() system call.
1960 tunkqfilter(struct cdev *dev, struct knote *kn)
1962 struct tuntap_softc *tp = dev->si_drv1;
1963 struct ifnet *ifp = TUN2IFP(tp);
1965 switch(kn->kn_filter) {
1967 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1968 ifp->if_xname, dev2unit(dev));
1969 kn->kn_fop = &tun_read_filterops;
1973 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1974 ifp->if_xname, dev2unit(dev));
1975 kn->kn_fop = &tun_write_filterops;
1979 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1980 ifp->if_xname, dev2unit(dev));
1985 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1991 * Return true of there is data in the interface queue.
1994 tunkqread(struct knote *kn, long hint)
1997 struct tuntap_softc *tp = kn->kn_hook;
1998 struct cdev *dev = tp->tun_dev;
1999 struct ifnet *ifp = TUN2IFP(tp);
2001 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2003 "%s have data in the queue. Len = %d, minor = %#x\n",
2004 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2008 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2017 * Always can write, always return MTU in kn->data.
2020 tunkqwrite(struct knote *kn, long hint)
2022 struct tuntap_softc *tp = kn->kn_hook;
2023 struct ifnet *ifp = TUN2IFP(tp);
2025 kn->kn_data = ifp->if_mtu;
2031 tunkqdetach(struct knote *kn)
2033 struct tuntap_softc *tp = kn->kn_hook;
2035 knlist_remove(&tp->tun_rsel.si_note, kn, 0);