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
19 #include "opt_atalk.h"
21 #include "opt_inet6.h"
24 #include <sys/param.h>
27 #include <sys/systm.h>
30 #include <sys/module.h>
31 #include <sys/socket.h>
32 #include <sys/fcntl.h>
33 #include <sys/filio.h>
34 #include <sys/sockio.h>
35 #include <sys/ttycom.h>
37 #include <sys/selinfo.h>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
44 #include <sys/malloc.h>
45 #include <sys/random.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
102 #define TUNNAME "tun"
105 * All mutable global variables in if_tun are locked using tunmtx, with
106 * the exception of tundebug, which is used unlocked, and tunclones,
107 * which is static after setup.
109 static struct mtx tunmtx;
110 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
111 static int tundebug = 0;
112 static int tundclone = 1;
113 static struct clonedevs *tunclones;
114 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
115 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
117 SYSCTL_DECL(_net_link);
118 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
119 "IP tunnel software network interface.");
120 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
121 "Enable legacy devfs interface creation.");
123 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
125 static void tunclone(void *arg, struct ucred *cred, char *name,
126 int namelen, struct cdev **dev);
127 static void tuncreate(const char *name, struct cdev *dev);
128 static int tunifioctl(struct ifnet *, u_long, caddr_t);
129 static int tuninit(struct ifnet *);
130 static int tunmodevent(module_t, int, void *);
131 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
133 static void tunstart(struct ifnet *);
135 static int tun_clone_create(struct if_clone *, int, caddr_t);
136 static void tun_clone_destroy(struct ifnet *);
138 IFC_SIMPLE_DECLARE(tun, 0);
140 static d_open_t tunopen;
141 static d_close_t tunclose;
142 static d_read_t tunread;
143 static d_write_t tunwrite;
144 static d_ioctl_t tunioctl;
145 static d_poll_t tunpoll;
146 static d_kqfilter_t tunkqfilter;
148 static int tunkqread(struct knote *, long);
149 static int tunkqwrite(struct knote *, long);
150 static void tunkqdetach(struct knote *);
152 static struct filterops tun_read_filterops = {
155 .f_detach = tunkqdetach,
156 .f_event = tunkqread,
159 static struct filterops tun_write_filterops = {
162 .f_detach = tunkqdetach,
163 .f_event = tunkqwrite,
166 static struct cdevsw tun_cdevsw = {
167 .d_version = D_VERSION,
168 .d_flags = D_PSEUDO | D_NEEDGIANT | D_NEEDMINOR,
175 .d_kqfilter = tunkqfilter,
180 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
185 /* find any existing device, or allocate new unit number */
186 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
188 /* No preexisting struct cdev *, create one */
189 dev = make_dev(&tun_cdevsw, unit,
190 UID_UUCP, GID_DIALER, 0600, "%s%d", ifc->ifc_name, unit);
193 dev->si_flags |= SI_CHEAPCLONE;
196 tuncreate(ifc->ifc_name, dev);
202 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
205 char devname[SPECNAMELEN + 1];
206 int u, i, append_unit;
212 * If tun cloning is enabled, only the superuser can create an
215 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
218 if (strcmp(name, TUNNAME) == 0) {
220 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
221 return; /* Don't recognise the name */
222 if (u != -1 && u > IF_MAXUNIT)
223 return; /* Unit number too high */
230 CURVNET_SET(CRED_TO_VNET(cred));
231 /* find any existing device, or allocate new unit number */
232 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
235 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
239 /* No preexisting struct cdev *, create one */
240 *dev = make_dev(&tun_cdevsw, u,
241 UID_UUCP, GID_DIALER, 0600, "%s", name);
244 (*dev)->si_flags |= SI_CHEAPCLONE;
248 if_clone_create(name, namelen, NULL);
253 tun_destroy(struct tun_softc *tp)
258 mtx_lock(&tp->tun_mtx);
259 if ((tp->tun_flags & TUN_OPEN) != 0)
260 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
262 mtx_unlock(&tp->tun_mtx);
264 CURVNET_SET(TUN2IFP(tp)->if_vnet);
266 bpfdetach(TUN2IFP(tp));
267 if_detach(TUN2IFP(tp));
268 if_free(TUN2IFP(tp));
270 knlist_destroy(&tp->tun_rsel.si_note);
271 mtx_destroy(&tp->tun_mtx);
272 cv_destroy(&tp->tun_cv);
278 tun_clone_destroy(struct ifnet *ifp)
280 struct tun_softc *tp = ifp->if_softc;
283 TAILQ_REMOVE(&tunhead, tp, tun_list);
289 tunmodevent(module_t mod, int type, void *data)
291 static eventhandler_tag tag;
292 struct tun_softc *tp;
296 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
297 clone_setup(&tunclones);
298 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
301 if_clone_attach(&tun_cloner);
304 if_clone_detach(&tun_cloner);
305 EVENTHANDLER_DEREGISTER(dev_clone, tag);
308 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
309 TAILQ_REMOVE(&tunhead, tp, tun_list);
315 clone_cleanup(&tunclones);
316 mtx_destroy(&tunmtx);
324 static moduledata_t tun_mod = {
330 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
333 tunstart(struct ifnet *ifp)
335 struct tun_softc *tp = ifp->if_softc;
338 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
339 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
340 IFQ_LOCK(&ifp->if_snd);
341 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
343 IFQ_UNLOCK(&ifp->if_snd);
346 IFQ_UNLOCK(&ifp->if_snd);
349 mtx_lock(&tp->tun_mtx);
350 if (tp->tun_flags & TUN_RWAIT) {
351 tp->tun_flags &= ~TUN_RWAIT;
354 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
355 mtx_unlock(&tp->tun_mtx);
356 pgsigio(&tp->tun_sigio, SIGIO, 0);
358 mtx_unlock(&tp->tun_mtx);
359 selwakeuppri(&tp->tun_rsel, PZERO + 1);
360 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
363 /* XXX: should return an error code so it can fail. */
365 tuncreate(const char *name, struct cdev *dev)
367 struct tun_softc *sc;
370 dev->si_flags &= ~SI_CHEAPCLONE;
372 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
373 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
374 cv_init(&sc->tun_cv, "tun_condvar");
375 sc->tun_flags = TUN_INITED;
378 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
381 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
383 panic("%s%d: failed to if_alloc() interface.\n",
384 name, dev2unit(dev));
385 if_initname(ifp, name, dev2unit(dev));
386 ifp->if_mtu = TUNMTU;
387 ifp->if_ioctl = tunifioctl;
388 ifp->if_output = tunoutput;
389 ifp->if_start = tunstart;
390 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
392 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
393 ifp->if_snd.ifq_drv_maxlen = 0;
394 IFQ_SET_READY(&ifp->if_snd);
395 knlist_init_mtx(&sc->tun_rsel.si_note, NULL);
398 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
400 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
401 ifp->if_xname, dev2unit(dev));
405 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
408 struct tun_softc *tp;
411 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
416 tuncreate(TUNNAME, dev);
421 * XXXRW: This use of tun_pid is subject to error due to the
422 * fact that a reference to the tunnel can live beyond the
423 * death of the process that created it. Can we replace this
424 * with a simple busy flag?
426 mtx_lock(&tp->tun_mtx);
427 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
428 mtx_unlock(&tp->tun_mtx);
431 tp->tun_pid = td->td_proc->p_pid;
433 tp->tun_flags |= TUN_OPEN;
434 mtx_unlock(&tp->tun_mtx);
436 if_link_state_change(ifp, LINK_STATE_UP);
437 TUNDEBUG(ifp, "open\n");
443 * tunclose - close the device - mark i/f down & delete
447 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
449 struct tun_softc *tp;
456 mtx_lock(&tp->tun_mtx);
457 tp->tun_flags &= ~TUN_OPEN;
459 mtx_unlock(&tp->tun_mtx);
462 * junk all pending output
464 CURVNET_SET(ifp->if_vnet);
466 IFQ_PURGE(&ifp->if_snd);
469 if (ifp->if_flags & IFF_UP) {
475 /* Delete all addresses and routes which reference this interface. */
476 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
480 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
481 /* deal w/IPv4 PtP destination; unlocked read */
482 if (ifa->ifa_addr->sa_family == AF_INET) {
483 rtinit(ifa, (int)RTM_DELETE,
484 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
486 rtinit(ifa, (int)RTM_DELETE, 0);
490 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
493 if_link_state_change(ifp, LINK_STATE_DOWN);
496 mtx_lock(&tp->tun_mtx);
497 funsetown(&tp->tun_sigio);
498 selwakeuppri(&tp->tun_rsel, PZERO + 1);
499 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
500 TUNDEBUG (ifp, "closed\n");
502 cv_broadcast(&tp->tun_cv);
503 mtx_unlock(&tp->tun_mtx);
508 tuninit(struct ifnet *ifp)
511 struct tun_softc *tp = ifp->if_softc;
516 TUNDEBUG(ifp, "tuninit\n");
518 ifp->if_flags |= IFF_UP;
519 ifp->if_drv_flags |= IFF_DRV_RUNNING;
520 getmicrotime(&ifp->if_lastchange);
524 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
525 if (ifa->ifa_addr->sa_family == AF_INET) {
526 struct sockaddr_in *si;
528 si = (struct sockaddr_in *)ifa->ifa_addr;
529 mtx_lock(&tp->tun_mtx);
530 if (si->sin_addr.s_addr)
531 tp->tun_flags |= TUN_IASET;
533 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
534 if (si && si->sin_addr.s_addr)
535 tp->tun_flags |= TUN_DSTADDR;
536 mtx_unlock(&tp->tun_mtx);
539 if_addr_runlock(ifp);
545 * Process an ioctl request.
548 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
550 struct ifreq *ifr = (struct ifreq *)data;
551 struct tun_softc *tp = ifp->if_softc;
558 ifs = (struct ifstat *)data;
559 mtx_lock(&tp->tun_mtx);
561 sprintf(ifs->ascii + strlen(ifs->ascii),
562 "\tOpened by PID %d\n", tp->tun_pid);
563 mtx_unlock(&tp->tun_mtx);
566 error = tuninit(ifp);
567 TUNDEBUG(ifp, "address set, error=%d\n", error);
570 error = tuninit(ifp);
571 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
574 ifp->if_mtu = ifr->ifr_mtu;
575 TUNDEBUG(ifp, "mtu set\n");
589 * tunoutput - queue packets from higher level ready to put out.
595 struct sockaddr *dst,
598 struct tun_softc *tp = ifp->if_softc;
599 u_short cached_tun_flags;
603 TUNDEBUG (ifp, "tunoutput\n");
606 error = mac_ifnet_check_transmit(ifp, m0);
613 /* Could be unlocked read? */
614 mtx_lock(&tp->tun_mtx);
615 cached_tun_flags = tp->tun_flags;
616 mtx_unlock(&tp->tun_mtx);
617 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
618 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
623 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
628 /* BPF writes need to be handled specially. */
629 if (dst->sa_family == AF_UNSPEC) {
630 bcopy(dst->sa_data, &af, sizeof(af));
634 if (bpf_peers_present(ifp->if_bpf)) {
636 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
639 /* prepend sockaddr? this may abort if the mbuf allocation fails */
640 if (cached_tun_flags & TUN_LMODE) {
641 /* allocate space for sockaddr */
642 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
644 /* if allocation failed drop packet */
650 bcopy(dst, m0->m_data, dst->sa_len);
654 if (cached_tun_flags & TUN_IFHEAD) {
655 /* Prepend the address family */
656 M_PREPEND(m0, 4, M_DONTWAIT);
658 /* if allocation failed drop packet */
664 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
667 if (dst->sa_family != AF_INET)
671 return (EAFNOSUPPORT);
675 error = (ifp->if_transmit)(ifp, m0);
677 ifp->if_collisions++;
685 * the cdevsw interface is now pretty minimal.
688 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
692 struct tun_softc *tp = dev->si_drv1;
693 struct tuninfo *tunp;
697 tunp = (struct tuninfo *)data;
698 if (tunp->mtu < IF_MINMTU)
700 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
701 error = priv_check(td, PRIV_NET_SETIFMTU);
705 TUN2IFP(tp)->if_mtu = tunp->mtu;
706 TUN2IFP(tp)->if_type = tunp->type;
707 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
710 tunp = (struct tuninfo *)data;
711 tunp->mtu = TUN2IFP(tp)->if_mtu;
712 tunp->type = TUN2IFP(tp)->if_type;
713 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
716 tundebug = *(int *)data;
719 *(int *)data = tundebug;
722 mtx_lock(&tp->tun_mtx);
724 tp->tun_flags |= TUN_LMODE;
725 tp->tun_flags &= ~TUN_IFHEAD;
727 tp->tun_flags &= ~TUN_LMODE;
728 mtx_unlock(&tp->tun_mtx);
731 mtx_lock(&tp->tun_mtx);
733 tp->tun_flags |= TUN_IFHEAD;
734 tp->tun_flags &= ~TUN_LMODE;
736 tp->tun_flags &= ~TUN_IFHEAD;
737 mtx_unlock(&tp->tun_mtx);
740 /* Could be unlocked read? */
741 mtx_lock(&tp->tun_mtx);
742 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
743 mtx_unlock(&tp->tun_mtx);
746 /* deny this if UP */
747 if (TUN2IFP(tp)->if_flags & IFF_UP)
750 switch (*(int *)data & ~IFF_MULTICAST) {
751 case IFF_POINTOPOINT:
753 TUN2IFP(tp)->if_flags &=
754 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
755 TUN2IFP(tp)->if_flags |= *(int *)data;
762 mtx_lock(&tp->tun_mtx);
763 tp->tun_pid = curthread->td_proc->p_pid;
764 mtx_unlock(&tp->tun_mtx);
769 mtx_lock(&tp->tun_mtx);
771 tp->tun_flags |= TUN_ASYNC;
773 tp->tun_flags &= ~TUN_ASYNC;
774 mtx_unlock(&tp->tun_mtx);
778 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
780 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
781 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
782 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
783 *(int *)data += mb->m_len;
784 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
790 return (fsetown(*(int *)data, &tp->tun_sigio));
793 *(int *)data = fgetown(&tp->tun_sigio);
796 /* This is deprecated, FIOSETOWN should be used instead. */
798 return (fsetown(-(*(int *)data), &tp->tun_sigio));
800 /* This is deprecated, FIOGETOWN should be used instead. */
802 *(int *)data = -fgetown(&tp->tun_sigio);
812 * The cdevsw read interface - reads a packet at a time, or at
813 * least as much of a packet as can be read.
816 tunread(struct cdev *dev, struct uio *uio, int flag)
818 struct tun_softc *tp = dev->si_drv1;
819 struct ifnet *ifp = TUN2IFP(tp);
823 TUNDEBUG (ifp, "read\n");
824 mtx_lock(&tp->tun_mtx);
825 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
826 mtx_unlock(&tp->tun_mtx);
827 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
831 tp->tun_flags &= ~TUN_RWAIT;
832 mtx_unlock(&tp->tun_mtx);
836 IFQ_DEQUEUE(&ifp->if_snd, m);
838 if (flag & O_NONBLOCK) {
840 return (EWOULDBLOCK);
842 mtx_lock(&tp->tun_mtx);
843 tp->tun_flags |= TUN_RWAIT;
844 mtx_unlock(&tp->tun_mtx);
845 if ((error = tsleep(tp, PCATCH | (PZERO + 1),
846 "tunread", 0)) != 0) {
854 while (m && uio->uio_resid > 0 && error == 0) {
855 len = min(uio->uio_resid, m->m_len);
857 error = uiomove(mtod(m, void *), len, uio);
862 TUNDEBUG(ifp, "Dropping mbuf\n");
869 * the cdevsw write interface - an atomic write is a packet - or else!
872 tunwrite(struct cdev *dev, struct uio *uio, int flag)
874 struct tun_softc *tp = dev->si_drv1;
875 struct ifnet *ifp = TUN2IFP(tp);
881 TUNDEBUG(ifp, "tunwrite\n");
883 if ((ifp->if_flags & IFF_UP) != IFF_UP)
884 /* ignore silently */
887 if (uio->uio_resid == 0)
890 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
891 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
895 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
900 m->m_pkthdr.rcvif = ifp;
902 mac_ifnet_create_mbuf(ifp, m);
905 /* Could be unlocked read? */
906 mtx_lock(&tp->tun_mtx);
907 if (tp->tun_flags & TUN_IFHEAD) {
908 mtx_unlock(&tp->tun_mtx);
909 if (m->m_len < sizeof(family) &&
910 (m = m_pullup(m, sizeof(family))) == NULL)
912 family = ntohl(*mtod(m, u_int32_t *));
913 m_adj(m, sizeof(family));
915 mtx_unlock(&tp->tun_mtx);
919 BPF_MTAP2(ifp, &family, sizeof(family), m);
944 return (EAFNOSUPPORT);
946 /* First chunk of an mbuf contains good junk */
947 if (harvest.point_to_point)
948 random_harvest(m, 16, 3, 0, RANDOM_NET);
949 ifp->if_ibytes += m->m_pkthdr.len;
951 CURVNET_SET(ifp->if_vnet);
952 netisr_dispatch(isr, m);
958 * tunpoll - the poll interface, this is only useful on reads
959 * really. The write detect always returns true, write never blocks
960 * anyway, it either accepts the packet or drops it.
963 tunpoll(struct cdev *dev, int events, struct thread *td)
966 struct tun_softc *tp = dev->si_drv1;
967 struct ifnet *ifp = TUN2IFP(tp);
972 TUNDEBUG(ifp, "tunpoll\n");
974 if (events & (POLLIN | POLLRDNORM)) {
975 IFQ_LOCK(&ifp->if_snd);
976 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
978 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
979 revents |= events & (POLLIN | POLLRDNORM);
981 TUNDEBUG(ifp, "tunpoll waiting\n");
982 selrecord(td, &tp->tun_rsel);
984 IFQ_UNLOCK(&ifp->if_snd);
986 if (events & (POLLOUT | POLLWRNORM))
987 revents |= events & (POLLOUT | POLLWRNORM);
994 * tunkqfilter - support for the kevent() system call.
997 tunkqfilter(struct cdev *dev, struct knote *kn)
1000 struct tun_softc *tp = dev->si_drv1;
1001 struct ifnet *ifp = TUN2IFP(tp);
1004 switch(kn->kn_filter) {
1006 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1007 ifp->if_xname, dev2unit(dev));
1008 kn->kn_fop = &tun_read_filterops;
1012 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1013 ifp->if_xname, dev2unit(dev));
1014 kn->kn_fop = &tun_write_filterops;
1018 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1019 ifp->if_xname, dev2unit(dev));
1025 kn->kn_hook = (caddr_t) dev;
1026 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1032 * Return true of there is data in the interface queue.
1035 tunkqread(struct knote *kn, long hint)
1038 struct cdev *dev = (struct cdev *)(kn->kn_hook);
1039 struct tun_softc *tp = dev->si_drv1;
1040 struct ifnet *ifp = TUN2IFP(tp);
1043 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1045 "%s have data in the queue. Len = %d, minor = %#x\n",
1046 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1050 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1060 * Always can write, always return MTU in kn->data.
1063 tunkqwrite(struct knote *kn, long hint)
1066 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1067 struct ifnet *ifp = TUN2IFP(tp);
1070 kn->kn_data = ifp->if_mtu;
1077 tunkqdetach(struct knote *kn)
1079 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1081 knlist_remove(&tp->tun_rsel.si_note, kn, 0);