1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
5 * Nottingham University 1987.
7 * This source may be freely distributed, however I would be interested
8 * in any changes that are made.
10 * This driver takes packets off the IP i/f and hands them up to a
11 * user process to have its wicked way with. This driver has it's
12 * roots in a similar driver written by Phil Cockcroft (formerly) at
13 * UCL. This driver is based much more on read/write/poll mode of
20 #include "opt_inet6.h"
22 #include <sys/param.h>
25 #include <sys/systm.h>
28 #include <sys/module.h>
29 #include <sys/socket.h>
30 #include <sys/fcntl.h>
31 #include <sys/filio.h>
32 #include <sys/sockio.h>
33 #include <sys/ttycom.h>
35 #include <sys/selinfo.h>
36 #include <sys/signalvar.h>
37 #include <sys/filedesc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <sys/random.h>
44 #include <sys/ctype.h>
47 #include <net/if_var.h>
48 #include <net/if_clone.h>
49 #include <net/if_types.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
54 #include <netinet/in.h>
57 #include <net/if_tun.h>
59 #include <sys/queue.h>
60 #include <sys/condvar.h>
62 #include <security/mac/mac_framework.h>
65 * tun_list is protected by global tunmtx. Other mutable fields are
66 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
67 * static for the duration of a tunnel interface.
70 TAILQ_ENTRY(tun_softc) tun_list;
72 u_short tun_flags; /* misc flags */
73 #define TUN_OPEN 0x0001
74 #define TUN_INITED 0x0002
75 #define TUN_RCOLL 0x0004
76 #define TUN_IASET 0x0008
77 #define TUN_DSTADDR 0x0010
78 #define TUN_LMODE 0x0020
79 #define TUN_RWAIT 0x0040
80 #define TUN_ASYNC 0x0080
81 #define TUN_IFHEAD 0x0100
83 #define TUN_READY (TUN_OPEN | TUN_INITED)
86 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
87 * actually needed? Can we just return EBUSY if already open?
88 * Problem is that this involved inherent races when a tun device
89 * is handed off from one process to another, as opposed to just
90 * being slightly stale informationally.
92 pid_t tun_pid; /* owning pid */
93 struct ifnet *tun_ifp; /* the interface */
94 struct sigio *tun_sigio; /* information for async I/O */
95 struct selinfo tun_rsel; /* read select */
96 struct mtx tun_mtx; /* protect mutable softc fields */
97 struct cv tun_cv; /* protect against ref'd dev destroy */
99 #define TUN2IFP(sc) ((sc)->tun_ifp)
101 #define TUNDEBUG if (tundebug) if_printf
104 * All mutable global variables in if_tun are locked using tunmtx, with
105 * the exception of tundebug, which is used unlocked, and tunclones,
106 * which is static after setup.
108 static struct mtx tunmtx;
109 static eventhandler_tag tag;
110 static const char tunname[] = "tun";
111 static MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
112 static int tundebug = 0;
113 static int tundclone = 1;
114 static struct clonedevs *tunclones;
115 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
116 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
118 SYSCTL_DECL(_net_link);
119 static SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
120 "IP tunnel software network interface.");
121 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RWTUN, &tundclone, 0,
122 "Enable legacy devfs interface creation.");
124 static void tunclone(void *arg, struct ucred *cred, char *name,
125 int namelen, struct cdev **dev);
126 static void tuncreate(const char *name, struct cdev *dev);
127 static int tunifioctl(struct ifnet *, u_long, caddr_t);
128 static void tuninit(struct ifnet *);
129 static int tunmodevent(module_t, int, void *);
130 static int tunoutput(struct ifnet *, struct mbuf *,
131 const struct sockaddr *, struct route *ro);
132 static void tunstart(struct ifnet *);
134 static int tun_clone_match(struct if_clone *ifc, const char *name);
135 static int tun_clone_create(struct if_clone *, char *, size_t, caddr_t);
136 static int tun_clone_destroy(struct if_clone *, struct ifnet *);
137 static struct unrhdr *tun_unrhdr;
138 VNET_DEFINE_STATIC(struct if_clone *, tun_cloner);
139 #define V_tun_cloner VNET(tun_cloner)
141 static d_open_t tunopen;
142 static d_close_t tunclose;
143 static d_read_t tunread;
144 static d_write_t tunwrite;
145 static d_ioctl_t tunioctl;
146 static d_poll_t tunpoll;
147 static d_kqfilter_t tunkqfilter;
149 static int tunkqread(struct knote *, long);
150 static int tunkqwrite(struct knote *, long);
151 static void tunkqdetach(struct knote *);
153 static struct filterops tun_read_filterops = {
156 .f_detach = tunkqdetach,
157 .f_event = tunkqread,
160 static struct filterops tun_write_filterops = {
163 .f_detach = tunkqdetach,
164 .f_event = tunkqwrite,
167 static struct cdevsw tun_cdevsw = {
168 .d_version = D_VERSION,
169 .d_flags = D_NEEDMINOR,
176 .d_kqfilter = tunkqfilter,
181 tun_clone_match(struct if_clone *ifc, const char *name)
183 if (strncmp(tunname, name, 3) == 0 &&
184 (name[3] == '\0' || isdigit(name[3])))
191 tun_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
196 err = ifc_name2unit(name, &unit);
201 /* If this unit number is still available that/s okay. */
202 if (alloc_unr_specific(tun_unrhdr, unit) == -1)
205 unit = alloc_unr(tun_unrhdr);
208 snprintf(name, IFNAMSIZ, "%s%d", tunname, unit);
210 /* find any existing device, or allocate new unit number */
211 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
213 /* No preexisting struct cdev *, create one */
214 dev = make_dev(&tun_cdevsw, unit,
215 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
217 tuncreate(tunname, dev);
223 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
226 char devname[SPECNAMELEN + 1];
227 int u, i, append_unit;
233 * If tun cloning is enabled, only the superuser can create an
236 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE) != 0)
239 if (strcmp(name, tunname) == 0) {
241 } else if (dev_stdclone(name, NULL, tunname, &u) != 1)
242 return; /* Don't recognise the name */
243 if (u != -1 && u > IF_MAXUNIT)
244 return; /* Unit number too high */
251 CURVNET_SET(CRED_TO_VNET(cred));
252 /* find any existing device, or allocate new unit number */
253 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
256 namelen = snprintf(devname, sizeof(devname), "%s%d",
260 /* No preexisting struct cdev *, create one */
261 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
262 UID_UUCP, GID_DIALER, 0600, "%s", name);
265 if_clone_create(name, namelen, NULL);
270 tun_destroy(struct tun_softc *tp)
274 mtx_lock(&tp->tun_mtx);
275 if ((tp->tun_flags & TUN_OPEN) != 0)
276 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
278 mtx_unlock(&tp->tun_mtx);
280 CURVNET_SET(TUN2IFP(tp)->if_vnet);
282 bpfdetach(TUN2IFP(tp));
283 if_detach(TUN2IFP(tp));
284 free_unr(tun_unrhdr, TUN2IFP(tp)->if_dunit);
285 if_free(TUN2IFP(tp));
287 seldrain(&tp->tun_rsel);
288 knlist_clear(&tp->tun_rsel.si_note, 0);
289 knlist_destroy(&tp->tun_rsel.si_note);
290 mtx_destroy(&tp->tun_mtx);
291 cv_destroy(&tp->tun_cv);
297 tun_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
299 struct tun_softc *tp = ifp->if_softc;
302 TAILQ_REMOVE(&tunhead, tp, tun_list);
310 vnet_tun_init(const void *unused __unused)
312 V_tun_cloner = if_clone_advanced(tunname, 0, tun_clone_match,
313 tun_clone_create, tun_clone_destroy);
315 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
316 vnet_tun_init, NULL);
319 vnet_tun_uninit(const void *unused __unused)
321 if_clone_detach(V_tun_cloner);
323 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
324 vnet_tun_uninit, NULL);
327 tun_uninit(const void *unused __unused)
329 struct tun_softc *tp;
331 EVENTHANDLER_DEREGISTER(dev_clone, tag);
332 drain_dev_clone_events();
335 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
336 TAILQ_REMOVE(&tunhead, tp, tun_list);
342 delete_unrhdr(tun_unrhdr);
343 clone_cleanup(&tunclones);
344 mtx_destroy(&tunmtx);
346 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
349 tunmodevent(module_t mod, int type, void *data)
354 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
355 clone_setup(&tunclones);
356 tun_unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
357 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
362 /* See tun_uninit, so it's done after the vnet_sysuninit() */
370 static moduledata_t tun_mod = {
376 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
377 MODULE_VERSION(if_tun, 1);
380 tunstart(struct ifnet *ifp)
382 struct tun_softc *tp = ifp->if_softc;
385 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
386 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
387 IFQ_LOCK(&ifp->if_snd);
388 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
390 IFQ_UNLOCK(&ifp->if_snd);
393 IFQ_UNLOCK(&ifp->if_snd);
396 mtx_lock(&tp->tun_mtx);
397 if (tp->tun_flags & TUN_RWAIT) {
398 tp->tun_flags &= ~TUN_RWAIT;
401 selwakeuppri(&tp->tun_rsel, PZERO + 1);
402 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
403 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
404 mtx_unlock(&tp->tun_mtx);
405 pgsigio(&tp->tun_sigio, SIGIO, 0);
407 mtx_unlock(&tp->tun_mtx);
410 /* XXX: should return an error code so it can fail. */
412 tuncreate(const char *name, struct cdev *dev)
414 struct tun_softc *sc;
417 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
418 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
419 cv_init(&sc->tun_cv, "tun_condvar");
420 sc->tun_flags = TUN_INITED;
423 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
426 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
428 panic("%s%d: failed to if_alloc() interface.\n",
429 name, dev2unit(dev));
430 if_initname(ifp, name, dev2unit(dev));
431 ifp->if_mtu = TUNMTU;
432 ifp->if_ioctl = tunifioctl;
433 ifp->if_output = tunoutput;
434 ifp->if_start = tunstart;
435 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
437 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
438 ifp->if_snd.ifq_drv_maxlen = 0;
439 IFQ_SET_READY(&ifp->if_snd);
440 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
441 ifp->if_capabilities |= IFCAP_LINKSTATE;
442 ifp->if_capenable |= IFCAP_LINKSTATE;
445 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
447 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
448 ifp->if_xname, dev2unit(dev));
452 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
455 struct tun_softc *tp;
458 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
463 tuncreate(tunname, dev);
468 * XXXRW: This use of tun_pid is subject to error due to the
469 * fact that a reference to the tunnel can live beyond the
470 * death of the process that created it. Can we replace this
471 * with a simple busy flag?
473 mtx_lock(&tp->tun_mtx);
474 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
475 mtx_unlock(&tp->tun_mtx);
478 tp->tun_pid = td->td_proc->p_pid;
480 tp->tun_flags |= TUN_OPEN;
482 if_link_state_change(ifp, LINK_STATE_UP);
483 TUNDEBUG(ifp, "open\n");
484 mtx_unlock(&tp->tun_mtx);
490 * tunclose - close the device - mark i/f down & delete
494 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
496 struct tun_softc *tp;
502 mtx_lock(&tp->tun_mtx);
505 * junk all pending output
507 CURVNET_SET(ifp->if_vnet);
508 IFQ_PURGE(&ifp->if_snd);
510 if (ifp->if_flags & IFF_UP) {
511 mtx_unlock(&tp->tun_mtx);
513 mtx_lock(&tp->tun_mtx);
516 /* Delete all addresses and routes which reference this interface. */
517 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
520 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
521 mtx_unlock(&tp->tun_mtx);
522 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
523 /* deal w/IPv4 PtP destination; unlocked read */
524 if (ifa->ifa_addr->sa_family == AF_INET) {
525 rtinit(ifa, (int)RTM_DELETE,
526 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
528 rtinit(ifa, (int)RTM_DELETE, 0);
532 mtx_lock(&tp->tun_mtx);
534 if_link_state_change(ifp, LINK_STATE_DOWN);
537 funsetown(&tp->tun_sigio);
538 selwakeuppri(&tp->tun_rsel, PZERO + 1);
539 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
540 TUNDEBUG (ifp, "closed\n");
541 tp->tun_flags &= ~TUN_OPEN;
544 cv_broadcast(&tp->tun_cv);
545 mtx_unlock(&tp->tun_mtx);
550 tuninit(struct ifnet *ifp)
552 struct tun_softc *tp = ifp->if_softc;
557 TUNDEBUG(ifp, "tuninit\n");
559 mtx_lock(&tp->tun_mtx);
560 ifp->if_flags |= IFF_UP;
561 ifp->if_drv_flags |= IFF_DRV_RUNNING;
562 getmicrotime(&ifp->if_lastchange);
566 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
567 if (ifa->ifa_addr->sa_family == AF_INET) {
568 struct sockaddr_in *si;
570 si = (struct sockaddr_in *)ifa->ifa_addr;
571 if (si->sin_addr.s_addr)
572 tp->tun_flags |= TUN_IASET;
574 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
575 if (si && si->sin_addr.s_addr)
576 tp->tun_flags |= TUN_DSTADDR;
579 if_addr_runlock(ifp);
581 mtx_unlock(&tp->tun_mtx);
585 * Process an ioctl request.
588 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
590 struct ifreq *ifr = (struct ifreq *)data;
591 struct tun_softc *tp = ifp->if_softc;
597 ifs = (struct ifstat *)data;
598 mtx_lock(&tp->tun_mtx);
600 snprintf(ifs->ascii, sizeof(ifs->ascii),
601 "\tOpened by PID %d\n", tp->tun_pid);
603 ifs->ascii[0] = '\0';
604 mtx_unlock(&tp->tun_mtx);
608 TUNDEBUG(ifp, "address set\n");
611 ifp->if_mtu = ifr->ifr_mtu;
612 TUNDEBUG(ifp, "mtu set\n");
625 * tunoutput - queue packets from higher level ready to put out.
628 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
631 struct tun_softc *tp = ifp->if_softc;
632 u_short cached_tun_flags;
636 TUNDEBUG (ifp, "tunoutput\n");
639 error = mac_ifnet_check_transmit(ifp, m0);
646 /* Could be unlocked read? */
647 mtx_lock(&tp->tun_mtx);
648 cached_tun_flags = tp->tun_flags;
649 mtx_unlock(&tp->tun_mtx);
650 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
651 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
656 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
661 /* BPF writes need to be handled specially. */
662 if (dst->sa_family == AF_UNSPEC)
663 bcopy(dst->sa_data, &af, sizeof(af));
667 if (bpf_peers_present(ifp->if_bpf))
668 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
670 /* prepend sockaddr? this may abort if the mbuf allocation fails */
671 if (cached_tun_flags & TUN_LMODE) {
672 /* allocate space for sockaddr */
673 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
675 /* if allocation failed drop packet */
677 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
678 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
681 bcopy(dst, m0->m_data, dst->sa_len);
685 if (cached_tun_flags & TUN_IFHEAD) {
686 /* Prepend the address family */
687 M_PREPEND(m0, 4, M_NOWAIT);
689 /* if allocation failed drop packet */
691 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
692 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
695 *(u_int32_t *)m0->m_data = htonl(af);
702 return (EAFNOSUPPORT);
706 error = (ifp->if_transmit)(ifp, m0);
709 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
714 * the cdevsw interface is now pretty minimal.
717 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
721 struct tun_softc *tp = dev->si_drv1;
722 struct tuninfo *tunp;
727 tunp = (struct tuninfo *)data;
728 if (TUN2IFP(tp)->if_type != tunp->type)
730 mtx_lock(&tp->tun_mtx);
731 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
732 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
733 ifr.ifr_mtu = tunp->mtu;
734 CURVNET_SET(TUN2IFP(tp)->if_vnet);
735 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
739 mtx_unlock(&tp->tun_mtx);
743 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
744 mtx_unlock(&tp->tun_mtx);
747 tunp = (struct tuninfo *)data;
748 mtx_lock(&tp->tun_mtx);
749 tunp->mtu = TUN2IFP(tp)->if_mtu;
750 tunp->type = TUN2IFP(tp)->if_type;
751 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
752 mtx_unlock(&tp->tun_mtx);
755 tundebug = *(int *)data;
758 *(int *)data = tundebug;
761 mtx_lock(&tp->tun_mtx);
763 tp->tun_flags |= TUN_LMODE;
764 tp->tun_flags &= ~TUN_IFHEAD;
766 tp->tun_flags &= ~TUN_LMODE;
767 mtx_unlock(&tp->tun_mtx);
770 mtx_lock(&tp->tun_mtx);
772 tp->tun_flags |= TUN_IFHEAD;
773 tp->tun_flags &= ~TUN_LMODE;
775 tp->tun_flags &= ~TUN_IFHEAD;
776 mtx_unlock(&tp->tun_mtx);
779 mtx_lock(&tp->tun_mtx);
780 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
781 mtx_unlock(&tp->tun_mtx);
784 /* deny this if UP */
785 if (TUN2IFP(tp)->if_flags & IFF_UP)
788 switch (*(int *)data & ~IFF_MULTICAST) {
789 case IFF_POINTOPOINT:
791 mtx_lock(&tp->tun_mtx);
792 TUN2IFP(tp)->if_flags &=
793 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
794 TUN2IFP(tp)->if_flags |= *(int *)data;
795 mtx_unlock(&tp->tun_mtx);
802 mtx_lock(&tp->tun_mtx);
803 tp->tun_pid = curthread->td_proc->p_pid;
804 mtx_unlock(&tp->tun_mtx);
809 mtx_lock(&tp->tun_mtx);
811 tp->tun_flags |= TUN_ASYNC;
813 tp->tun_flags &= ~TUN_ASYNC;
814 mtx_unlock(&tp->tun_mtx);
817 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
819 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
820 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
821 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
822 *(int *)data += mb->m_len;
823 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
828 return (fsetown(*(int *)data, &tp->tun_sigio));
831 *(int *)data = fgetown(&tp->tun_sigio);
834 /* This is deprecated, FIOSETOWN should be used instead. */
836 return (fsetown(-(*(int *)data), &tp->tun_sigio));
838 /* This is deprecated, FIOGETOWN should be used instead. */
840 *(int *)data = -fgetown(&tp->tun_sigio);
850 * The cdevsw read interface - reads a packet at a time, or at
851 * least as much of a packet as can be read.
854 tunread(struct cdev *dev, struct uio *uio, int flag)
856 struct tun_softc *tp = dev->si_drv1;
857 struct ifnet *ifp = TUN2IFP(tp);
861 TUNDEBUG (ifp, "read\n");
862 mtx_lock(&tp->tun_mtx);
863 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
864 mtx_unlock(&tp->tun_mtx);
865 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
869 tp->tun_flags &= ~TUN_RWAIT;
872 IFQ_DEQUEUE(&ifp->if_snd, m);
874 if (flag & O_NONBLOCK) {
875 mtx_unlock(&tp->tun_mtx);
876 return (EWOULDBLOCK);
878 tp->tun_flags |= TUN_RWAIT;
879 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
882 mtx_unlock(&tp->tun_mtx);
887 mtx_unlock(&tp->tun_mtx);
889 while (m && uio->uio_resid > 0 && error == 0) {
890 len = min(uio->uio_resid, m->m_len);
892 error = uiomove(mtod(m, void *), len, uio);
897 TUNDEBUG(ifp, "Dropping mbuf\n");
904 * the cdevsw write interface - an atomic write is a packet - or else!
907 tunwrite(struct cdev *dev, struct uio *uio, int flag)
909 struct tun_softc *tp = dev->si_drv1;
910 struct ifnet *ifp = TUN2IFP(tp);
912 uint32_t family, mru;
915 TUNDEBUG(ifp, "tunwrite\n");
917 if ((ifp->if_flags & IFF_UP) != IFF_UP)
918 /* ignore silently */
921 if (uio->uio_resid == 0)
925 if (tp->tun_flags & TUN_IFHEAD)
926 mru += sizeof(family);
927 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
928 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
932 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
933 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
937 m->m_pkthdr.rcvif = ifp;
939 mac_ifnet_create_mbuf(ifp, m);
942 /* Could be unlocked read? */
943 mtx_lock(&tp->tun_mtx);
944 if (tp->tun_flags & TUN_IFHEAD) {
945 mtx_unlock(&tp->tun_mtx);
946 if (m->m_len < sizeof(family) &&
947 (m = m_pullup(m, sizeof(family))) == NULL)
949 family = ntohl(*mtod(m, u_int32_t *));
950 m_adj(m, sizeof(family));
952 mtx_unlock(&tp->tun_mtx);
956 BPF_MTAP2(ifp, &family, sizeof(family), m);
971 return (EAFNOSUPPORT);
973 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
974 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
975 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
976 CURVNET_SET(ifp->if_vnet);
977 M_SETFIB(m, ifp->if_fib);
978 netisr_dispatch(isr, m);
984 * tunpoll - the poll interface, this is only useful on reads
985 * really. The write detect always returns true, write never blocks
986 * anyway, it either accepts the packet or drops it.
989 tunpoll(struct cdev *dev, int events, struct thread *td)
991 struct tun_softc *tp = dev->si_drv1;
992 struct ifnet *ifp = TUN2IFP(tp);
996 TUNDEBUG(ifp, "tunpoll\n");
998 if (events & (POLLIN | POLLRDNORM)) {
999 IFQ_LOCK(&ifp->if_snd);
1000 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
1002 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1003 revents |= events & (POLLIN | POLLRDNORM);
1005 TUNDEBUG(ifp, "tunpoll waiting\n");
1006 selrecord(td, &tp->tun_rsel);
1008 IFQ_UNLOCK(&ifp->if_snd);
1010 if (events & (POLLOUT | POLLWRNORM))
1011 revents |= events & (POLLOUT | POLLWRNORM);
1017 * tunkqfilter - support for the kevent() system call.
1020 tunkqfilter(struct cdev *dev, struct knote *kn)
1022 struct tun_softc *tp = dev->si_drv1;
1023 struct ifnet *ifp = TUN2IFP(tp);
1025 switch(kn->kn_filter) {
1027 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1028 ifp->if_xname, dev2unit(dev));
1029 kn->kn_fop = &tun_read_filterops;
1033 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1034 ifp->if_xname, dev2unit(dev));
1035 kn->kn_fop = &tun_write_filterops;
1039 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1040 ifp->if_xname, dev2unit(dev));
1045 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1051 * Return true of there is data in the interface queue.
1054 tunkqread(struct knote *kn, long hint)
1057 struct tun_softc *tp = kn->kn_hook;
1058 struct cdev *dev = tp->tun_dev;
1059 struct ifnet *ifp = TUN2IFP(tp);
1061 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1063 "%s have data in the queue. Len = %d, minor = %#x\n",
1064 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1068 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1077 * Always can write, always return MTU in kn->data.
1080 tunkqwrite(struct knote *kn, long hint)
1082 struct tun_softc *tp = kn->kn_hook;
1083 struct ifnet *ifp = TUN2IFP(tp);
1085 kn->kn_data = ifp->if_mtu;
1091 tunkqdetach(struct knote *kn)
1093 struct tun_softc *tp = kn->kn_hook;
1095 knlist_remove(&tp->tun_rsel.si_note, kn, 0);