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>
91 #include <netinet/in.h>
93 #include <netinet/ip.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
99 #include <netinet/udp.h>
100 #include <netinet/tcp.h>
102 #include <net/if_tap.h>
103 #include <net/if_tun.h>
105 #include <dev/virtio/network/virtio_net.h>
107 #include <sys/queue.h>
108 #include <sys/condvar.h>
109 #include <security/mac/mac_framework.h>
111 struct tuntap_driver;
114 * tun_list is protected by global tunmtx. Other mutable fields are
115 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
116 * static for the duration of a tunnel interface.
118 struct tuntap_softc {
119 TAILQ_ENTRY(tuntap_softc) tun_list;
120 struct cdev *tun_alias;
121 struct cdev *tun_dev;
122 u_short tun_flags; /* misc flags */
123 #define TUN_OPEN 0x0001
124 #define TUN_INITED 0x0002
125 #define TUN_UNUSED1 0x0008
126 #define TUN_DSTADDR 0x0010
127 #define TUN_LMODE 0x0020
128 #define TUN_RWAIT 0x0040
129 #define TUN_ASYNC 0x0080
130 #define TUN_IFHEAD 0x0100
131 #define TUN_DYING 0x0200
132 #define TUN_L2 0x0400
133 #define TUN_VMNET 0x0800
135 #define TUN_DRIVER_IDENT_MASK (TUN_L2 | TUN_VMNET)
136 #define TUN_READY (TUN_OPEN | TUN_INITED)
138 pid_t tun_pid; /* owning pid */
139 struct ifnet *tun_ifp; /* the interface */
140 struct sigio *tun_sigio; /* async I/O info */
141 struct tuntap_driver *tun_drv; /* appropriate driver */
142 struct selinfo tun_rsel; /* read select */
143 struct mtx tun_mtx; /* softc field mutex */
144 struct cv tun_cv; /* for ref'd dev destroy */
145 struct ether_addr tun_ether; /* remote address */
146 int tun_busy; /* busy count */
147 int tun_vhdrlen; /* virtio-net header length */
149 #define TUN2IFP(sc) ((sc)->tun_ifp)
151 #define TUNDEBUG if (tundebug) if_printf
153 #define TUN_LOCK(tp) mtx_lock(&(tp)->tun_mtx)
154 #define TUN_UNLOCK(tp) mtx_unlock(&(tp)->tun_mtx)
155 #define TUN_LOCK_ASSERT(tp) mtx_assert(&(tp)->tun_mtx, MA_OWNED);
157 #define TUN_VMIO_FLAG_MASK 0x0fff
160 * Interface capabilities of a tap device that supports the virtio-net
163 #define TAP_VNET_HDR_CAPS (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 \
164 | IFCAP_VLAN_HWCSUM \
165 | IFCAP_TSO | IFCAP_LRO \
168 #define TAP_ALL_OFFLOAD (CSUM_TSO | CSUM_TCP | CSUM_UDP |\
169 CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
172 * All mutable global variables in if_tun are locked using tunmtx, with
173 * the exception of tundebug, which is used unlocked, and the drivers' *clones,
174 * which are static after setup.
176 static struct mtx tunmtx;
177 static eventhandler_tag arrival_tag;
178 static eventhandler_tag clone_tag;
179 static const char tunname[] = "tun";
180 static const char tapname[] = "tap";
181 static const char vmnetname[] = "vmnet";
182 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
183 static int tundebug = 0;
184 static int tundclone = 1;
185 static int tap_allow_uopen = 0; /* allow user devfs cloning */
186 static int tapuponopen = 0; /* IFF_UP on open() */
187 static int tapdclone = 1; /* enable devfs cloning */
189 static TAILQ_HEAD(,tuntap_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
190 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
192 static struct sx tun_ioctl_sx;
193 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
195 SYSCTL_DECL(_net_link);
197 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
198 "IP tunnel software network interface");
199 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
200 "Enable legacy devfs interface creation");
203 static SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
204 "Ethernet tunnel software network interface");
205 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tap_allow_uopen, 0,
206 "Enable legacy devfs interface creation for all users");
207 SYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
208 "Bring interface up when /dev/tap is opened");
209 SYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tapdclone, 0,
210 "Enable legacy devfs interface creation");
211 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0, "");
213 static int tun_create_device(struct tuntap_driver *drv, int unit,
214 struct ucred *cr, struct cdev **dev, const char *name);
215 static int tun_busy_locked(struct tuntap_softc *tp);
216 static void tun_unbusy_locked(struct tuntap_softc *tp);
217 static int tun_busy(struct tuntap_softc *tp);
218 static void tun_unbusy(struct tuntap_softc *tp);
220 static int tuntap_name2info(const char *name, int *unit, int *flags);
221 static void tunclone(void *arg, struct ucred *cred, char *name,
222 int namelen, struct cdev **dev);
223 static void tuncreate(struct cdev *dev);
224 static void tundtor(void *data);
225 static void tunrename(void *arg, struct ifnet *ifp);
226 static int tunifioctl(struct ifnet *, u_long, caddr_t);
227 static void tuninit(struct ifnet *);
228 static void tunifinit(void *xtp);
229 static int tuntapmodevent(module_t, int, void *);
230 static int tunoutput(struct ifnet *, struct mbuf *,
231 const struct sockaddr *, struct route *ro);
232 static void tunstart(struct ifnet *);
233 static void tunstart_l2(struct ifnet *);
235 static int tun_clone_match(struct if_clone *ifc, const char *name);
236 static int tap_clone_match(struct if_clone *ifc, const char *name);
237 static int vmnet_clone_match(struct if_clone *ifc, const char *name);
238 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
239 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
240 static void tun_vnethdr_set(struct ifnet *ifp, int vhdrlen);
242 static d_open_t tunopen;
243 static d_read_t tunread;
244 static d_write_t tunwrite;
245 static d_ioctl_t tunioctl;
246 static d_poll_t tunpoll;
247 static d_kqfilter_t tunkqfilter;
249 static int tunkqread(struct knote *, long);
250 static int tunkqwrite(struct knote *, long);
251 static void tunkqdetach(struct knote *);
253 static struct filterops tun_read_filterops = {
256 .f_detach = tunkqdetach,
257 .f_event = tunkqread,
260 static struct filterops tun_write_filterops = {
263 .f_detach = tunkqdetach,
264 .f_event = tunkqwrite,
267 static struct tuntap_driver {
268 struct cdevsw cdevsw;
270 struct unrhdr *unrhdr;
271 struct clonedevs *clones;
272 ifc_match_t *clone_match_fn;
273 ifc_create_t *clone_create_fn;
274 ifc_destroy_t *clone_destroy_fn;
275 } tuntap_drivers[] = {
279 .d_version = D_VERSION,
280 .d_flags = D_NEEDMINOR,
286 .d_kqfilter = tunkqfilter,
289 .clone_match_fn = tun_clone_match,
290 .clone_create_fn = tun_clone_create,
291 .clone_destroy_fn = tun_clone_destroy,
294 .ident_flags = TUN_L2,
296 .d_version = D_VERSION,
297 .d_flags = D_NEEDMINOR,
303 .d_kqfilter = tunkqfilter,
306 .clone_match_fn = tap_clone_match,
307 .clone_create_fn = tun_clone_create,
308 .clone_destroy_fn = tun_clone_destroy,
311 .ident_flags = TUN_L2 | TUN_VMNET,
313 .d_version = D_VERSION,
314 .d_flags = D_NEEDMINOR,
320 .d_kqfilter = tunkqfilter,
323 .clone_match_fn = vmnet_clone_match,
324 .clone_create_fn = tun_clone_create,
325 .clone_destroy_fn = tun_clone_destroy,
329 struct tuntap_driver_cloner {
330 SLIST_ENTRY(tuntap_driver_cloner) link;
331 struct tuntap_driver *drv;
332 struct if_clone *cloner;
335 VNET_DEFINE_STATIC(SLIST_HEAD(, tuntap_driver_cloner), tuntap_driver_cloners) =
336 SLIST_HEAD_INITIALIZER(tuntap_driver_cloners);
338 #define V_tuntap_driver_cloners VNET(tuntap_driver_cloners)
341 * Mechanism for marking a tunnel device as busy so that we can safely do some
342 * orthogonal operations (such as operations on devices) without racing against
343 * tun_destroy. tun_destroy will wait on the condvar if we're at all busy or
344 * open, to be woken up when the condition is alleviated.
347 tun_busy_locked(struct tuntap_softc *tp)
351 if ((tp->tun_flags & TUN_DYING) != 0) {
353 * Perhaps unintuitive, but the device is busy going away.
354 * Other interpretations of EBUSY from tun_busy make little
355 * sense, since making a busy device even more busy doesn't
356 * sound like a problem.
366 tun_unbusy_locked(struct tuntap_softc *tp)
370 KASSERT(tp->tun_busy != 0, ("tun_unbusy: called for non-busy tunnel"));
373 /* Wake up anything that may be waiting on our busy tunnel. */
374 if (tp->tun_busy == 0)
375 cv_broadcast(&tp->tun_cv);
379 tun_busy(struct tuntap_softc *tp)
384 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)
478 tun_clone_match(struct if_clone *ifc, const char *name)
482 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
483 if ((tunflags & TUN_L2) == 0)
491 tap_clone_match(struct if_clone *ifc, const char *name)
495 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
496 if ((tunflags & (TUN_L2 | TUN_VMNET)) == TUN_L2)
504 vmnet_clone_match(struct if_clone *ifc, const char *name)
508 if (tuntap_name2info(name, NULL, &tunflags) == 0) {
509 if ((tunflags & TUN_VMNET) != 0)
517 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
519 struct tuntap_driver *drv;
521 int err, i, tunflags, unit;
524 /* The name here tells us exactly what we're creating */
525 err = tuntap_name2info(name, &unit, &tunflags);
529 drv = tuntap_driver_from_flags(tunflags);
534 /* If this unit number is still available that's okay. */
535 if (alloc_unr_specific(drv->unrhdr, unit) == -1)
538 unit = alloc_unr(drv->unrhdr);
541 snprintf(name, IFNAMSIZ, "%s%d", drv->cdevsw.d_name, unit);
543 /* find any existing device, or allocate new unit number */
545 i = clone_create(&drv->clones, &drv->cdevsw, &unit, &dev, 0);
546 /* No preexisting struct cdev *, create one */
548 i = tun_create_device(drv, unit, NULL, &dev, name);
556 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
559 char devname[SPECNAMELEN + 1];
560 struct tuntap_driver *drv;
561 int append_unit, i, u, tunflags;
568 CURVNET_SET(CRED_TO_VNET(cred));
569 if (tuntap_name2info(name, &u, &tunflags) != 0)
570 goto out; /* Not recognized */
572 if (u != -1 && u > IF_MAXUNIT)
573 goto out; /* Unit number too high */
575 mayclone = priv_check_cred(cred, PRIV_NET_IFCREATE) == 0;
576 if ((tunflags & TUN_L2) != 0) {
577 /* tap/vmnet allow user open with a sysctl */
578 mayclone = (mayclone || tap_allow_uopen) && tapdclone;
580 mayclone = mayclone && tundclone;
584 * If tun cloning is enabled, only the superuser can create an
595 drv = tuntap_driver_from_flags(tunflags);
599 /* find any existing device, or allocate new unit number */
600 i = clone_create(&drv->clones, &drv->cdevsw, &u, dev, 0);
603 namelen = snprintf(devname, sizeof(devname), "%s%d",
608 i = tun_create_device(drv, u, cred, dev, name);
611 if_clone_create(name, namelen, NULL);
617 tun_destroy(struct tuntap_softc *tp)
621 tp->tun_flags |= TUN_DYING;
622 if (tp->tun_busy != 0)
623 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
627 CURVNET_SET(TUN2IFP(tp)->if_vnet);
629 /* destroy_dev will take care of any alias. */
630 destroy_dev(tp->tun_dev);
631 seldrain(&tp->tun_rsel);
632 knlist_clear(&tp->tun_rsel.si_note, 0);
633 knlist_destroy(&tp->tun_rsel.si_note);
634 if ((tp->tun_flags & TUN_L2) != 0) {
635 ether_ifdetach(TUN2IFP(tp));
637 bpfdetach(TUN2IFP(tp));
638 if_detach(TUN2IFP(tp));
640 sx_xlock(&tun_ioctl_sx);
641 TUN2IFP(tp)->if_softc = NULL;
642 sx_xunlock(&tun_ioctl_sx);
643 free_unr(tp->tun_drv->unrhdr, TUN2IFP(tp)->if_dunit);
644 if_free(TUN2IFP(tp));
645 mtx_destroy(&tp->tun_mtx);
646 cv_destroy(&tp->tun_cv);
652 tun_clone_destroy(struct if_clone *ifc __unused, struct ifnet *ifp)
654 struct tuntap_softc *tp = ifp->if_softc;
657 TAILQ_REMOVE(&tunhead, tp, tun_list);
665 vnet_tun_init(const void *unused __unused)
667 struct tuntap_driver *drv;
668 struct tuntap_driver_cloner *drvc;
671 for (i = 0; i < nitems(tuntap_drivers); ++i) {
672 drv = &tuntap_drivers[i];
673 drvc = malloc(sizeof(*drvc), M_TUN, M_WAITOK | M_ZERO);
676 drvc->cloner = if_clone_advanced(drv->cdevsw.d_name, 0,
677 drv->clone_match_fn, drv->clone_create_fn,
678 drv->clone_destroy_fn);
679 SLIST_INSERT_HEAD(&V_tuntap_driver_cloners, drvc, link);
682 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
683 vnet_tun_init, NULL);
686 vnet_tun_uninit(const void *unused __unused)
688 struct tuntap_driver_cloner *drvc;
690 while (!SLIST_EMPTY(&V_tuntap_driver_cloners)) {
691 drvc = SLIST_FIRST(&V_tuntap_driver_cloners);
692 SLIST_REMOVE_HEAD(&V_tuntap_driver_cloners, link);
694 if_clone_detach(drvc->cloner);
698 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
699 vnet_tun_uninit, NULL);
702 tun_uninit(const void *unused __unused)
704 struct tuntap_driver *drv;
705 struct tuntap_softc *tp;
708 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, arrival_tag);
709 EVENTHANDLER_DEREGISTER(dev_clone, clone_tag);
710 drain_dev_clone_events();
713 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
714 TAILQ_REMOVE(&tunhead, tp, tun_list);
720 for (i = 0; i < nitems(tuntap_drivers); ++i) {
721 drv = &tuntap_drivers[i];
722 delete_unrhdr(drv->unrhdr);
723 clone_cleanup(&drv->clones);
725 mtx_destroy(&tunmtx);
727 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
729 static struct tuntap_driver *
730 tuntap_driver_from_ifnet(const struct ifnet *ifp)
732 struct tuntap_driver *drv;
738 for (i = 0; i < nitems(tuntap_drivers); ++i) {
739 drv = &tuntap_drivers[i];
740 if (strcmp(ifp->if_dname, drv->cdevsw.d_name) == 0)
748 tuntapmodevent(module_t mod, int type, void *data)
750 struct tuntap_driver *drv;
755 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
756 for (i = 0; i < nitems(tuntap_drivers); ++i) {
757 drv = &tuntap_drivers[i];
758 clone_setup(&drv->clones);
759 drv->unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
761 arrival_tag = EVENTHANDLER_REGISTER(ifnet_arrival_event,
763 if (arrival_tag == NULL)
765 clone_tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
766 if (clone_tag == NULL)
770 /* See tun_uninit, so it's done after the vnet_sysuninit() */
778 static moduledata_t tuntap_mod = {
784 /* We'll only ever have these two, so no need for a macro. */
785 static moduledata_t tun_mod = { "if_tun", NULL, 0 };
786 static moduledata_t tap_mod = { "if_tap", NULL, 0 };
788 DECLARE_MODULE(if_tuntap, tuntap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
789 MODULE_VERSION(if_tuntap, 1);
790 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
791 MODULE_VERSION(if_tun, 1);
792 DECLARE_MODULE(if_tap, tap_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
793 MODULE_VERSION(if_tap, 1);
796 tun_create_device(struct tuntap_driver *drv, int unit, struct ucred *cr,
797 struct cdev **dev, const char *name)
799 struct make_dev_args args;
800 struct tuntap_softc *tp;
803 tp = malloc(sizeof(*tp), M_TUN, M_WAITOK | M_ZERO);
804 mtx_init(&tp->tun_mtx, "tun_mtx", NULL, MTX_DEF);
805 cv_init(&tp->tun_cv, "tun_condvar");
806 tp->tun_flags = drv->ident_flags;
809 make_dev_args_init(&args);
811 args.mda_flags = MAKEDEV_REF;
812 args.mda_devsw = &drv->cdevsw;
814 args.mda_uid = UID_UUCP;
815 args.mda_gid = GID_DIALER;
816 args.mda_mode = 0600;
817 args.mda_unit = unit;
818 args.mda_si_drv1 = tp;
819 error = make_dev_s(&args, dev, "%s", name);
825 KASSERT((*dev)->si_drv1 != NULL,
826 ("Failed to set si_drv1 at %s creation", name));
828 knlist_init_mtx(&tp->tun_rsel.si_note, &tp->tun_mtx);
830 TAILQ_INSERT_TAIL(&tunhead, tp, tun_list);
836 tunstart(struct ifnet *ifp)
838 struct tuntap_softc *tp = ifp->if_softc;
841 TUNDEBUG(ifp, "starting\n");
842 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
843 IFQ_LOCK(&ifp->if_snd);
844 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
846 IFQ_UNLOCK(&ifp->if_snd);
849 IFQ_UNLOCK(&ifp->if_snd);
853 if (tp->tun_flags & TUN_RWAIT) {
854 tp->tun_flags &= ~TUN_RWAIT;
857 selwakeuppri(&tp->tun_rsel, PZERO + 1);
858 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
859 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
861 pgsigio(&tp->tun_sigio, SIGIO, 0);
869 * queue packets from higher level ready to put out
872 tunstart_l2(struct ifnet *ifp)
874 struct tuntap_softc *tp = ifp->if_softc;
876 TUNDEBUG(ifp, "starting\n");
879 * do not junk pending output if we are in VMnet mode.
880 * XXX: can this do any harm because of queue overflow?
884 if (((tp->tun_flags & TUN_VMNET) == 0) &&
885 ((tp->tun_flags & TUN_READY) != TUN_READY)) {
889 TUNDEBUG(ifp, "not ready, tun_flags = 0x%x\n", tp->tun_flags);
892 IF_DEQUEUE(&ifp->if_snd, m);
895 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
904 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
906 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
907 if (tp->tun_flags & TUN_RWAIT) {
908 tp->tun_flags &= ~TUN_RWAIT;
912 if ((tp->tun_flags & TUN_ASYNC) && (tp->tun_sigio != NULL)) {
914 pgsigio(&tp->tun_sigio, SIGIO, 0);
918 selwakeuppri(&tp->tun_rsel, PZERO+1);
919 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
920 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); /* obytes are counted in ether_output */
923 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
927 /* XXX: should return an error code so it can fail. */
929 tuncreate(struct cdev *dev)
931 struct tuntap_driver *drv;
932 struct tuntap_softc *tp;
934 struct ether_addr eaddr;
940 ("si_drv1 should have been initialized at creation"));
943 iflags = IFF_MULTICAST;
944 if ((tp->tun_flags & TUN_L2) != 0) {
946 iflags |= IFF_BROADCAST | IFF_SIMPLEX;
949 iflags |= IFF_POINTOPOINT;
951 ifp = tp->tun_ifp = if_alloc(type);
953 panic("%s%d: failed to if_alloc() interface.\n",
954 drv->cdevsw.d_name, dev2unit(dev));
956 if_initname(ifp, drv->cdevsw.d_name, dev2unit(dev));
957 ifp->if_ioctl = tunifioctl;
958 ifp->if_flags = iflags;
959 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
960 ifp->if_capabilities |= IFCAP_LINKSTATE;
961 ifp->if_capenable |= IFCAP_LINKSTATE;
963 if ((tp->tun_flags & TUN_L2) != 0) {
964 ifp->if_init = tunifinit;
965 ifp->if_start = tunstart_l2;
967 ether_gen_addr(ifp, &eaddr);
968 ether_ifattach(ifp, eaddr.octet);
970 ifp->if_mtu = TUNMTU;
971 ifp->if_start = tunstart;
972 ifp->if_output = tunoutput;
974 ifp->if_snd.ifq_drv_maxlen = 0;
975 IFQ_SET_READY(&ifp->if_snd);
978 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
982 tp->tun_flags |= TUN_INITED;
985 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
986 ifp->if_xname, dev2unit(dev));
990 tunrename(void *arg __unused, struct ifnet *ifp)
992 struct tuntap_softc *tp;
995 if ((ifp->if_flags & IFF_RENAMING) == 0)
998 if (tuntap_driver_from_ifnet(ifp) == NULL)
1002 * We need to grab the ioctl sx long enough to make sure the softc is
1003 * still there. If it is, we can safely try to busy the tun device.
1004 * The busy may fail if the device is currently dying, in which case
1005 * we do nothing. If it doesn't fail, the busy count stops the device
1006 * from dying until we've created the alias (that will then be
1007 * subsequently destroyed).
1009 sx_xlock(&tun_ioctl_sx);
1012 sx_xunlock(&tun_ioctl_sx);
1015 error = tun_busy(tp);
1016 sx_xunlock(&tun_ioctl_sx);
1019 if (tp->tun_alias != NULL) {
1020 destroy_dev(tp->tun_alias);
1021 tp->tun_alias = NULL;
1024 if (strcmp(ifp->if_xname, tp->tun_dev->si_name) == 0)
1028 * Failure's ok, aliases are created on a best effort basis. If a
1029 * tun user/consumer decides to rename the interface to conflict with
1030 * another device (non-ifnet) on the system, we will assume they know
1031 * what they are doing. make_dev_alias_p won't touch tun_alias on
1032 * failure, so we use it but ignore the return value.
1034 make_dev_alias_p(MAKEDEV_CHECKNAME, &tp->tun_alias, tp->tun_dev, "%s",
1041 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
1044 struct tuntap_softc *tp;
1045 int error, tunflags;
1048 CURVNET_SET(TD_TO_VNET(td));
1049 error = tuntap_name2info(dev->si_name, NULL, &tunflags);
1052 return (error); /* Shouldn't happen */
1057 ("si_drv1 should have been initialized at creation"));
1060 if ((tp->tun_flags & TUN_INITED) == 0) {
1065 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
1071 error = tun_busy_locked(tp);
1072 KASSERT(error == 0, ("Must be able to busy an unopen tunnel"));
1075 if ((tp->tun_flags & TUN_L2) != 0) {
1076 bcopy(IF_LLADDR(ifp), tp->tun_ether.octet,
1077 sizeof(tp->tun_ether.octet));
1079 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1080 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1083 ifp->if_flags |= IFF_UP;
1086 tp->tun_pid = td->td_proc->p_pid;
1087 tp->tun_flags |= TUN_OPEN;
1089 if_link_state_change(ifp, LINK_STATE_UP);
1090 TUNDEBUG(ifp, "open\n");
1094 * This can fail with either ENOENT or EBUSY. This is in the middle of
1095 * d_open, so ENOENT should not be possible. EBUSY is possible, but
1096 * the only cdevpriv dtor being set will be tundtor and the softc being
1097 * passed is constant for a given cdev. We ignore the possible error
1098 * because of this as either "unlikely" or "not actually a problem."
1100 (void)devfs_set_cdevpriv(tp, tundtor);
1106 * tundtor - tear down the device - mark i/f down & delete
1113 struct tuntap_softc *tp;
1124 * Realistically, we can't be obstinate here. This only means that the
1125 * tuntap device was closed out of order, and the last closer wasn't the
1126 * controller. These are still good to know about, though, as software
1127 * should avoid multiple processes with a tuntap device open and
1128 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
1131 if (p->p_pid != tp->tun_pid) {
1133 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
1134 p->p_pid, p->p_comm, tp->tun_dev->si_name);
1138 * junk all pending output
1140 CURVNET_SET(ifp->if_vnet);
1143 if ((tp->tun_flags & TUN_L2) != 0) {
1145 IF_DRAIN(&ifp->if_snd);
1147 IFQ_PURGE(&ifp->if_snd);
1150 /* For vmnet, we won't do most of the address/route bits */
1151 if ((tp->tun_flags & TUN_VMNET) != 0 ||
1152 (l2tun && (ifp->if_flags & IFF_LINK0) != 0))
1155 if (ifp->if_flags & IFF_UP) {
1161 /* Delete all addresses and routes which reference this interface. */
1162 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1165 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1167 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1168 /* deal w/IPv4 PtP destination; unlocked read */
1169 if (!l2tun && ifa->ifa_addr->sa_family == AF_INET) {
1170 rtinit(ifa, (int)RTM_DELETE,
1171 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
1173 rtinit(ifa, (int)RTM_DELETE, 0);
1181 if_link_state_change(ifp, LINK_STATE_DOWN);
1184 funsetown(&tp->tun_sigio);
1185 selwakeuppri(&tp->tun_rsel, PZERO + 1);
1186 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
1187 TUNDEBUG (ifp, "closed\n");
1188 tp->tun_flags &= ~TUN_OPEN;
1190 tun_vnethdr_set(ifp, 0);
1192 tun_unbusy_locked(tp);
1197 tuninit(struct ifnet *ifp)
1199 struct tuntap_softc *tp = ifp->if_softc;
1201 struct epoch_tracker et;
1205 TUNDEBUG(ifp, "tuninit\n");
1208 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1209 if ((tp->tun_flags & TUN_L2) == 0) {
1210 ifp->if_flags |= IFF_UP;
1211 getmicrotime(&ifp->if_lastchange);
1213 NET_EPOCH_ENTER(et);
1214 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1215 if (ifa->ifa_addr->sa_family == AF_INET) {
1216 struct sockaddr_in *si;
1218 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
1219 if (si && si->sin_addr.s_addr) {
1220 tp->tun_flags |= TUN_DSTADDR;
1229 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1231 /* attempt to start output */
1238 * Used only for l2 tunnel.
1241 tunifinit(void *xtp)
1243 struct tuntap_softc *tp;
1245 tp = (struct tuntap_softc *)xtp;
1246 tuninit(tp->tun_ifp);
1250 * To be called under TUN_LOCK. Update ifp->if_hwassist according to the
1251 * current value of ifp->if_capenable.
1254 tun_caps_changed(struct ifnet *ifp)
1256 uint64_t hwassist = 0;
1258 TUN_LOCK_ASSERT((struct tuntap_softc *)ifp->if_softc);
1259 if (ifp->if_capenable & IFCAP_TXCSUM)
1260 hwassist |= CSUM_TCP | CSUM_UDP;
1261 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
1262 hwassist |= CSUM_TCP_IPV6
1264 if (ifp->if_capenable & IFCAP_TSO4)
1265 hwassist |= CSUM_IP_TSO;
1266 if (ifp->if_capenable & IFCAP_TSO6)
1267 hwassist |= CSUM_IP6_TSO;
1268 ifp->if_hwassist = hwassist;
1272 * To be called under TUN_LOCK. Update tp->tun_vhdrlen and adjust
1273 * if_capabilities and if_capenable as needed.
1276 tun_vnethdr_set(struct ifnet *ifp, int vhdrlen)
1278 struct tuntap_softc *tp = ifp->if_softc;
1280 TUN_LOCK_ASSERT(tp);
1282 if (tp->tun_vhdrlen == vhdrlen)
1286 * Update if_capabilities to reflect the
1287 * functionalities offered by the virtio-net
1291 ifp->if_capabilities |=
1294 ifp->if_capabilities &=
1297 * Disable any capabilities that we don't
1300 ifp->if_capenable &= ifp->if_capabilities;
1301 tun_caps_changed(ifp);
1302 tp->tun_vhdrlen = vhdrlen;
1304 TUNDEBUG(ifp, "vnet_hdr_len=%d, if_capabilities=%x\n",
1305 vhdrlen, ifp->if_capabilities);
1309 * Process an ioctl request.
1312 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1314 struct ifreq *ifr = (struct ifreq *)data;
1315 struct tuntap_softc *tp;
1317 struct ifmediareq *ifmr;
1318 int dummy, error = 0;
1322 sx_xlock(&tun_ioctl_sx);
1328 l2tun = (tp->tun_flags & TUN_L2) != 0;
1331 ifs = (struct ifstat *)data;
1334 snprintf(ifs->ascii, sizeof(ifs->ascii),
1335 "\tOpened by PID %d\n", tp->tun_pid);
1337 ifs->ascii[0] = '\0';
1342 error = ether_ioctl(ifp, cmd, data);
1346 TUNDEBUG(ifp, "address set\n");
1349 ifp->if_mtu = ifr->ifr_mtu;
1350 TUNDEBUG(ifp, "mtu set\n");
1362 ifmr = (struct ifmediareq *)data;
1363 dummy = ifmr->ifm_count;
1364 ifmr->ifm_count = 1;
1365 ifmr->ifm_status = IFM_AVALID;
1366 ifmr->ifm_active = IFM_ETHER;
1367 if (tp->tun_flags & TUN_OPEN)
1368 ifmr->ifm_status |= IFM_ACTIVE;
1369 ifmr->ifm_current = ifmr->ifm_active;
1371 int media = IFM_ETHER;
1372 error = copyout(&media, ifmr->ifm_ulist, sizeof(int));
1377 ifp->if_capenable = ifr->ifr_reqcap;
1378 tun_caps_changed(ifp);
1380 VLAN_CAPABILITIES(ifp);
1384 error = ether_ioctl(ifp, cmd, data);
1390 sx_xunlock(&tun_ioctl_sx);
1395 * tunoutput - queue packets from higher level ready to put out.
1398 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
1401 struct tuntap_softc *tp = ifp->if_softc;
1402 u_short cached_tun_flags;
1406 TUNDEBUG (ifp, "tunoutput\n");
1409 error = mac_ifnet_check_transmit(ifp, m0);
1416 /* Could be unlocked read? */
1418 cached_tun_flags = tp->tun_flags;
1420 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
1421 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1426 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
1431 /* BPF writes need to be handled specially. */
1432 if (dst->sa_family == AF_UNSPEC)
1433 bcopy(dst->sa_data, &af, sizeof(af));
1435 af = dst->sa_family;
1437 if (bpf_peers_present(ifp->if_bpf))
1438 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
1440 /* prepend sockaddr? this may abort if the mbuf allocation fails */
1441 if (cached_tun_flags & TUN_LMODE) {
1442 /* allocate space for sockaddr */
1443 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
1445 /* if allocation failed drop packet */
1447 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1448 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1451 bcopy(dst, m0->m_data, dst->sa_len);
1455 if (cached_tun_flags & TUN_IFHEAD) {
1456 /* Prepend the address family */
1457 M_PREPEND(m0, 4, M_NOWAIT);
1459 /* if allocation failed drop packet */
1461 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1462 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1465 *(u_int32_t *)m0->m_data = htonl(af);
1472 return (EAFNOSUPPORT);
1476 error = (ifp->if_transmit)(ifp, m0);
1479 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1484 * the cdevsw interface is now pretty minimal.
1487 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
1490 struct ifreq ifr, *ifrp;
1491 struct tuntap_softc *tp = dev->si_drv1;
1492 struct ifnet *ifp = TUN2IFP(tp);
1493 struct tuninfo *tunp;
1494 int error, iflags, ival;
1497 l2tun = (tp->tun_flags & TUN_L2) != 0;
1499 /* tap specific ioctls */
1501 /* VMware/VMnet port ioctl's */
1502 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
1503 defined(COMPAT_FREEBSD4)
1505 ival = IOCPARM_IVAL(data);
1506 data = (caddr_t)&ival;
1509 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
1510 iflags = *(int *)data;
1511 iflags &= TUN_VMIO_FLAG_MASK;
1512 iflags &= ~IFF_CANTCHANGE;
1516 ifp->if_flags = iflags |
1517 (ifp->if_flags & IFF_CANTCHANGE);
1521 case SIOCGIFADDR: /* get MAC address of the remote side */
1523 bcopy(&tp->tun_ether.octet, data,
1524 sizeof(tp->tun_ether.octet));
1528 case SIOCSIFADDR: /* set MAC address of the remote side */
1530 bcopy(data, &tp->tun_ether.octet,
1531 sizeof(tp->tun_ether.octet));
1536 ival = *(int *)data;
1538 ival != sizeof(struct virtio_net_hdr) &&
1539 ival != sizeof(struct virtio_net_hdr_mrg_rxbuf)) {
1543 tun_vnethdr_set(ifp, ival);
1549 *(int *)data = tp->tun_vhdrlen;
1555 /* Fall through to the common ioctls if unhandled */
1561 tp->tun_flags |= TUN_LMODE;
1562 tp->tun_flags &= ~TUN_IFHEAD;
1564 tp->tun_flags &= ~TUN_LMODE;
1571 tp->tun_flags |= TUN_IFHEAD;
1572 tp->tun_flags &= ~TUN_LMODE;
1574 tp->tun_flags &= ~TUN_IFHEAD;
1580 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
1585 /* deny this if UP */
1586 if (TUN2IFP(tp)->if_flags & IFF_UP)
1589 switch (*(int *)data & ~IFF_MULTICAST) {
1590 case IFF_POINTOPOINT:
1593 TUN2IFP(tp)->if_flags &=
1594 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
1595 TUN2IFP(tp)->if_flags |= *(int *)data;
1606 tp->tun_pid = curthread->td_proc->p_pid;
1611 /* Fall through to the common ioctls if unhandled */
1616 ifrp = (struct ifreq *)data;
1617 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
1621 tunp = (struct tuninfo *)data;
1622 if (TUN2IFP(tp)->if_type != tunp->type)
1623 return (EPROTOTYPE);
1625 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
1626 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
1627 ifr.ifr_mtu = tunp->mtu;
1628 CURVNET_SET(TUN2IFP(tp)->if_vnet);
1629 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
1637 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
1641 tunp = (struct tuninfo *)data;
1643 tunp->mtu = TUN2IFP(tp)->if_mtu;
1644 tunp->type = TUN2IFP(tp)->if_type;
1645 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
1649 tundebug = *(int *)data;
1652 *(int *)data = tundebug;
1659 tp->tun_flags |= TUN_ASYNC;
1661 tp->tun_flags &= ~TUN_ASYNC;
1665 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
1667 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
1668 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
1669 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
1670 *(int *)data += mb->m_len;
1671 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
1676 return (fsetown(*(int *)data, &tp->tun_sigio));
1679 *(int *)data = fgetown(&tp->tun_sigio);
1682 /* This is deprecated, FIOSETOWN should be used instead. */
1684 return (fsetown(-(*(int *)data), &tp->tun_sigio));
1686 /* This is deprecated, FIOGETOWN should be used instead. */
1688 *(int *)data = -fgetown(&tp->tun_sigio);
1698 * The cdevsw read interface - reads a packet at a time, or at
1699 * least as much of a packet as can be read.
1702 tunread(struct cdev *dev, struct uio *uio, int flag)
1704 struct tuntap_softc *tp = dev->si_drv1;
1705 struct ifnet *ifp = TUN2IFP(tp);
1710 TUNDEBUG (ifp, "read\n");
1712 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
1714 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
1718 tp->tun_flags &= ~TUN_RWAIT;
1721 IFQ_DEQUEUE(&ifp->if_snd, m);
1724 if (flag & O_NONBLOCK) {
1726 return (EWOULDBLOCK);
1728 tp->tun_flags |= TUN_RWAIT;
1729 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
1738 if ((tp->tun_flags & TUN_L2) != 0)
1741 len = min(tp->tun_vhdrlen, uio->uio_resid);
1743 struct virtio_net_hdr_mrg_rxbuf vhdr;
1745 bzero(&vhdr, sizeof(vhdr));
1746 if (m->m_pkthdr.csum_flags & TAP_ALL_OFFLOAD) {
1747 m = virtio_net_tx_offload(ifp, m, false, &vhdr.hdr);
1750 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1751 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1752 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1753 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1754 vhdr.hdr.csum_offset);
1755 error = uiomove(&vhdr, len, uio);
1758 while (m && uio->uio_resid > 0 && error == 0) {
1759 len = min(uio->uio_resid, m->m_len);
1761 error = uiomove(mtod(m, void *), len, uio);
1766 TUNDEBUG(ifp, "Dropping mbuf\n");
1773 tunwrite_l2(struct tuntap_softc *tp, struct mbuf *m,
1774 struct virtio_net_hdr_mrg_rxbuf *vhdr)
1776 struct epoch_tracker et;
1777 struct ether_header *eh;
1783 * Only pass a unicast frame to ether_input(), if it would
1784 * actually have been received by non-virtual hardware.
1786 if (m->m_len < sizeof(struct ether_header)) {
1791 eh = mtod(m, struct ether_header *);
1793 if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
1794 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1795 bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
1800 if (vhdr != NULL && virtio_net_rx_csum(m, &vhdr->hdr)) {
1805 /* Pass packet up to parent. */
1806 CURVNET_SET(ifp->if_vnet);
1807 NET_EPOCH_ENTER(et);
1808 (*ifp->if_input)(ifp, m);
1811 /* ibytes are counted in parent */
1812 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1817 tunwrite_l3(struct tuntap_softc *tp, struct mbuf *m)
1819 struct epoch_tracker et;
1824 /* Could be unlocked read? */
1826 if (tp->tun_flags & TUN_IFHEAD) {
1828 if (m->m_len < sizeof(family) &&
1829 (m = m_pullup(m, sizeof(family))) == NULL)
1831 family = ntohl(*mtod(m, u_int32_t *));
1832 m_adj(m, sizeof(family));
1838 BPF_MTAP2(ifp, &family, sizeof(family), m);
1853 return (EAFNOSUPPORT);
1855 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1856 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1857 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1858 CURVNET_SET(ifp->if_vnet);
1859 M_SETFIB(m, ifp->if_fib);
1860 NET_EPOCH_ENTER(et);
1861 netisr_dispatch(isr, m);
1868 * the cdevsw write interface - an atomic write is a packet - or else!
1871 tunwrite(struct cdev *dev, struct uio *uio, int flag)
1873 struct virtio_net_hdr_mrg_rxbuf vhdr;
1874 struct tuntap_softc *tp;
1878 int align, vhdrlen, error;
1883 TUNDEBUG(ifp, "tunwrite\n");
1884 if ((ifp->if_flags & IFF_UP) != IFF_UP)
1885 /* ignore silently */
1888 if (uio->uio_resid == 0)
1891 l2tun = (tp->tun_flags & TUN_L2) != 0;
1892 mru = l2tun ? TAPMRU : TUNMRU;
1893 vhdrlen = tp->tun_vhdrlen;
1896 align = ETHER_ALIGN;
1898 } else if ((tp->tun_flags & TUN_IFHEAD) != 0)
1899 mru += sizeof(uint32_t); /* family */
1900 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
1901 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
1906 error = uiomove(&vhdr, vhdrlen, uio);
1909 TUNDEBUG(ifp, "txvhdr: f %u, gt %u, hl %u, "
1910 "gs %u, cs %u, co %u\n", vhdr.hdr.flags,
1911 vhdr.hdr.gso_type, vhdr.hdr.hdr_len,
1912 vhdr.hdr.gso_size, vhdr.hdr.csum_start,
1913 vhdr.hdr.csum_offset);
1916 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, align, M_PKTHDR)) == NULL) {
1917 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1921 m->m_pkthdr.rcvif = ifp;
1923 mac_ifnet_create_mbuf(ifp, m);
1927 return (tunwrite_l2(tp, m, vhdrlen > 0 ? &vhdr : NULL));
1929 return (tunwrite_l3(tp, m));
1933 * tunpoll - the poll interface, this is only useful on reads
1934 * really. The write detect always returns true, write never blocks
1935 * anyway, it either accepts the packet or drops it.
1938 tunpoll(struct cdev *dev, int events, struct thread *td)
1940 struct tuntap_softc *tp = dev->si_drv1;
1941 struct ifnet *ifp = TUN2IFP(tp);
1944 TUNDEBUG(ifp, "tunpoll\n");
1946 if (events & (POLLIN | POLLRDNORM)) {
1947 IFQ_LOCK(&ifp->if_snd);
1948 if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1949 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1950 revents |= events & (POLLIN | POLLRDNORM);
1952 TUNDEBUG(ifp, "tunpoll waiting\n");
1953 selrecord(td, &tp->tun_rsel);
1955 IFQ_UNLOCK(&ifp->if_snd);
1957 revents |= events & (POLLOUT | POLLWRNORM);
1963 * tunkqfilter - support for the kevent() system call.
1966 tunkqfilter(struct cdev *dev, struct knote *kn)
1968 struct tuntap_softc *tp = dev->si_drv1;
1969 struct ifnet *ifp = TUN2IFP(tp);
1971 switch(kn->kn_filter) {
1973 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1974 ifp->if_xname, dev2unit(dev));
1975 kn->kn_fop = &tun_read_filterops;
1979 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1980 ifp->if_xname, dev2unit(dev));
1981 kn->kn_fop = &tun_write_filterops;
1985 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1986 ifp->if_xname, dev2unit(dev));
1991 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1997 * Return true of there is data in the interface queue.
2000 tunkqread(struct knote *kn, long hint)
2003 struct tuntap_softc *tp = kn->kn_hook;
2004 struct cdev *dev = tp->tun_dev;
2005 struct ifnet *ifp = TUN2IFP(tp);
2007 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
2009 "%s have data in the queue. Len = %d, minor = %#x\n",
2010 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
2014 "%s waiting for data, minor = %#x\n", ifp->if_xname,
2023 * Always can write, always return MTU in kn->data.
2026 tunkqwrite(struct knote *kn, long hint)
2028 struct tuntap_softc *tp = kn->kn_hook;
2029 struct ifnet *ifp = TUN2IFP(tp);
2031 kn->kn_data = ifp->if_mtu;
2037 tunkqdetach(struct knote *kn)
2039 struct tuntap_softc *tp = kn->kn_hook;
2041 knlist_remove(&tp->tun_rsel.si_note, kn, 0);