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"
25 #include <sys/param.h>
28 #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>
46 #include <sys/vimage.h>
49 #include <net/if_clone.h>
50 #include <net/if_types.h>
51 #include <net/netisr.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
57 #include <net/if_tun.h>
59 #include <sys/queue.h>
61 #include <security/mac/mac_framework.h>
64 * tun_list is protected by global tunmtx. Other mutable fields are
65 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
66 * static for the duration of a tunnel interface.
69 TAILQ_ENTRY(tun_softc) tun_list;
71 u_short tun_flags; /* misc flags */
72 #define TUN_OPEN 0x0001
73 #define TUN_INITED 0x0002
74 #define TUN_RCOLL 0x0004
75 #define TUN_IASET 0x0008
76 #define TUN_DSTADDR 0x0010
77 #define TUN_LMODE 0x0020
78 #define TUN_RWAIT 0x0040
79 #define TUN_ASYNC 0x0080
80 #define TUN_IFHEAD 0x0100
82 #define TUN_READY (TUN_OPEN | TUN_INITED)
85 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
86 * actually needed? Can we just return EBUSY if already open?
87 * Problem is that this involved inherent races when a tun device
88 * is handed off from one process to another, as opposed to just
89 * being slightly stale informationally.
91 pid_t tun_pid; /* owning pid */
92 struct ifnet *tun_ifp; /* the interface */
93 struct sigio *tun_sigio; /* information for async I/O */
94 struct selinfo tun_rsel; /* read select */
95 struct mtx tun_mtx; /* protect mutable softc fields */
97 #define TUN2IFP(sc) ((sc)->tun_ifp)
99 #define TUNDEBUG if (tundebug) if_printf
100 #define TUNNAME "tun"
103 * All mutable global variables in if_tun are locked using tunmtx, with
104 * the exception of tundebug, which is used unlocked, and tunclones,
105 * which is static after setup.
107 static struct mtx tunmtx;
108 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
109 static int tundebug = 0;
110 static int tundclone = 1;
111 static struct clonedevs *tunclones;
112 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
113 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
115 SYSCTL_DECL(_net_link);
116 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
117 "IP tunnel software network interface.");
118 SYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
119 "Enable legacy devfs interface creation.");
121 TUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
123 static void tunclone(void *arg, struct ucred *cred, char *name,
124 int namelen, struct cdev **dev);
125 static void tuncreate(const char *name, struct cdev *dev);
126 static int tunifioctl(struct ifnet *, u_long, caddr_t);
127 static int tuninit(struct ifnet *);
128 static int tunmodevent(module_t, int, void *);
129 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
131 static void tunstart(struct ifnet *);
133 static int tun_clone_create(struct if_clone *, int, caddr_t);
134 static void tun_clone_destroy(struct ifnet *);
136 IFC_SIMPLE_DECLARE(tun, 0);
138 static d_open_t tunopen;
139 static d_close_t tunclose;
140 static d_read_t tunread;
141 static d_write_t tunwrite;
142 static d_ioctl_t tunioctl;
143 static d_poll_t tunpoll;
144 static d_kqfilter_t tunkqfilter;
146 static int tunkqread(struct knote *, long);
147 static int tunkqwrite(struct knote *, long);
148 static void tunkqdetach(struct knote *);
150 static struct filterops tun_read_filterops = {
153 .f_detach = tunkqdetach,
154 .f_event = tunkqread,
157 static struct filterops tun_write_filterops = {
160 .f_detach = tunkqdetach,
161 .f_event = tunkqwrite,
164 static struct cdevsw tun_cdevsw = {
165 .d_version = D_VERSION,
166 .d_flags = D_PSEUDO | D_NEEDGIANT | D_NEEDMINOR,
173 .d_kqfilter = tunkqfilter,
178 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
183 /* find any existing device, or allocate new unit number */
184 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
186 /* No preexisting struct cdev *, create one */
187 dev = make_dev(&tun_cdevsw, unit,
188 UID_UUCP, GID_DIALER, 0600, "%s%d", ifc->ifc_name, unit);
191 dev->si_flags |= SI_CHEAPCLONE;
194 tuncreate(ifc->ifc_name, dev);
200 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
203 char devname[SPECNAMELEN + 1];
204 int u, i, append_unit;
210 * If tun cloning is enabled, only the superuser can create an
213 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
216 if (strcmp(name, TUNNAME) == 0) {
218 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
219 return; /* Don't recognise the name */
220 if (u != -1 && u > IF_MAXUNIT)
221 return; /* Unit number too high */
228 CURVNET_SET(TD_TO_VNET(curthread));
229 /* find any existing device, or allocate new unit number */
230 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
233 namelen = snprintf(devname, sizeof(devname), "%s%d", name,
237 /* No preexisting struct cdev *, create one */
238 *dev = make_dev(&tun_cdevsw, u,
239 UID_UUCP, GID_DIALER, 0600, "%s", name);
242 (*dev)->si_flags |= SI_CHEAPCLONE;
246 if_clone_create(name, namelen, NULL);
251 tun_destroy(struct tun_softc *tp)
256 KASSERT((tp->tun_flags & TUN_OPEN) == 0,
257 ("tununits is out of sync - unit %d", TUN2IFP(tp)->if_dunit));
259 CURVNET_SET(TUN2IFP(tp)->if_vnet);
261 bpfdetach(TUN2IFP(tp));
262 if_detach(TUN2IFP(tp));
263 if_free(TUN2IFP(tp));
265 knlist_destroy(&tp->tun_rsel.si_note);
266 mtx_destroy(&tp->tun_mtx);
272 tun_clone_destroy(struct ifnet *ifp)
274 struct tun_softc *tp = ifp->if_softc;
277 TAILQ_REMOVE(&tunhead, tp, tun_list);
283 tunmodevent(module_t mod, int type, void *data)
285 static eventhandler_tag tag;
286 struct tun_softc *tp;
290 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
291 clone_setup(&tunclones);
292 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
295 if_clone_attach(&tun_cloner);
298 if_clone_detach(&tun_cloner);
299 EVENTHANDLER_DEREGISTER(dev_clone, tag);
302 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
303 TAILQ_REMOVE(&tunhead, tp, tun_list);
309 clone_cleanup(&tunclones);
310 mtx_destroy(&tunmtx);
318 static moduledata_t tun_mod = {
324 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
327 tunstart(struct ifnet *ifp)
329 struct tun_softc *tp = ifp->if_softc;
332 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
333 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
334 IFQ_LOCK(&ifp->if_snd);
335 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
337 IFQ_UNLOCK(&ifp->if_snd);
340 IFQ_UNLOCK(&ifp->if_snd);
343 mtx_lock(&tp->tun_mtx);
344 if (tp->tun_flags & TUN_RWAIT) {
345 tp->tun_flags &= ~TUN_RWAIT;
348 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
349 mtx_unlock(&tp->tun_mtx);
350 pgsigio(&tp->tun_sigio, SIGIO, 0);
352 mtx_unlock(&tp->tun_mtx);
353 selwakeuppri(&tp->tun_rsel, PZERO + 1);
354 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
357 /* XXX: should return an error code so it can fail. */
359 tuncreate(const char *name, struct cdev *dev)
361 struct tun_softc *sc;
364 dev->si_flags &= ~SI_CHEAPCLONE;
366 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
367 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
368 sc->tun_flags = TUN_INITED;
371 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
374 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
376 panic("%s%d: failed to if_alloc() interface.\n",
377 name, dev2unit(dev));
378 if_initname(ifp, name, dev2unit(dev));
379 ifp->if_mtu = TUNMTU;
380 ifp->if_ioctl = tunifioctl;
381 ifp->if_output = tunoutput;
382 ifp->if_start = tunstart;
383 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
385 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
386 ifp->if_snd.ifq_drv_maxlen = 0;
387 IFQ_SET_READY(&ifp->if_snd);
388 knlist_init(&sc->tun_rsel.si_note, NULL, NULL, NULL, NULL);
391 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
393 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
394 ifp->if_xname, dev2unit(dev));
398 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
401 struct tun_softc *tp;
404 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
409 tuncreate(TUNNAME, dev);
414 * XXXRW: This use of tun_pid is subject to error due to the
415 * fact that a reference to the tunnel can live beyond the
416 * death of the process that created it. Can we replace this
417 * with a simple busy flag?
419 mtx_lock(&tp->tun_mtx);
420 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
421 mtx_unlock(&tp->tun_mtx);
424 tp->tun_pid = td->td_proc->p_pid;
426 tp->tun_flags |= TUN_OPEN;
427 mtx_unlock(&tp->tun_mtx);
429 TUNDEBUG(ifp, "open\n");
435 * tunclose - close the device - mark i/f down & delete
439 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
441 struct tun_softc *tp;
448 mtx_lock(&tp->tun_mtx);
449 tp->tun_flags &= ~TUN_OPEN;
453 * junk all pending output
455 CURVNET_SET(ifp->if_vnet);
457 IFQ_PURGE(&ifp->if_snd);
459 mtx_unlock(&tp->tun_mtx);
461 if (ifp->if_flags & IFF_UP) {
467 /* Delete all addresses and routes which reference this interface. */
468 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
472 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
473 /* deal w/IPv4 PtP destination; unlocked read */
474 if (ifa->ifa_addr->sa_family == AF_INET) {
475 rtinit(ifa, (int)RTM_DELETE,
476 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
478 rtinit(ifa, (int)RTM_DELETE, 0);
482 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
487 funsetown(&tp->tun_sigio);
488 selwakeuppri(&tp->tun_rsel, PZERO + 1);
489 KNOTE_UNLOCKED(&tp->tun_rsel.si_note, 0);
490 TUNDEBUG (ifp, "closed\n");
495 tuninit(struct ifnet *ifp)
497 struct tun_softc *tp = ifp->if_softc;
501 TUNDEBUG(ifp, "tuninit\n");
503 ifp->if_flags |= IFF_UP;
504 ifp->if_drv_flags |= IFF_DRV_RUNNING;
505 getmicrotime(&ifp->if_lastchange);
508 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
509 if (ifa->ifa_addr->sa_family == AF_INET) {
510 struct sockaddr_in *si;
512 si = (struct sockaddr_in *)ifa->ifa_addr;
513 mtx_lock(&tp->tun_mtx);
514 if (si->sin_addr.s_addr)
515 tp->tun_flags |= TUN_IASET;
517 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
518 if (si && si->sin_addr.s_addr)
519 tp->tun_flags |= TUN_DSTADDR;
520 mtx_unlock(&tp->tun_mtx);
528 * Process an ioctl request.
531 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
533 struct ifreq *ifr = (struct ifreq *)data;
534 struct tun_softc *tp = ifp->if_softc;
541 ifs = (struct ifstat *)data;
542 mtx_lock(&tp->tun_mtx);
544 sprintf(ifs->ascii + strlen(ifs->ascii),
545 "\tOpened by PID %d\n", tp->tun_pid);
546 mtx_unlock(&tp->tun_mtx);
549 error = tuninit(ifp);
550 TUNDEBUG(ifp, "address set, error=%d\n", error);
553 error = tuninit(ifp);
554 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
557 ifp->if_mtu = ifr->ifr_mtu;
558 TUNDEBUG(ifp, "mtu set\n");
572 * tunoutput - queue packets from higher level ready to put out.
578 struct sockaddr *dst,
581 struct tun_softc *tp = ifp->if_softc;
582 u_short cached_tun_flags;
586 TUNDEBUG (ifp, "tunoutput\n");
589 error = mac_ifnet_check_transmit(ifp, m0);
596 /* Could be unlocked read? */
597 mtx_lock(&tp->tun_mtx);
598 cached_tun_flags = tp->tun_flags;
599 mtx_unlock(&tp->tun_mtx);
600 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
601 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
606 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
611 /* BPF writes need to be handled specially. */
612 if (dst->sa_family == AF_UNSPEC) {
613 bcopy(dst->sa_data, &af, sizeof(af));
617 if (bpf_peers_present(ifp->if_bpf)) {
619 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
622 /* prepend sockaddr? this may abort if the mbuf allocation fails */
623 if (cached_tun_flags & TUN_LMODE) {
624 /* allocate space for sockaddr */
625 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
627 /* if allocation failed drop packet */
633 bcopy(dst, m0->m_data, dst->sa_len);
637 if (cached_tun_flags & TUN_IFHEAD) {
638 /* Prepend the address family */
639 M_PREPEND(m0, 4, M_DONTWAIT);
641 /* if allocation failed drop packet */
647 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
650 if (dst->sa_family != AF_INET)
654 return (EAFNOSUPPORT);
658 IFQ_HANDOFF(ifp, m0, error);
660 ifp->if_collisions++;
668 * the cdevsw interface is now pretty minimal.
671 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
675 struct tun_softc *tp = dev->si_drv1;
676 struct tuninfo *tunp;
680 tunp = (struct tuninfo *)data;
681 if (tunp->mtu < IF_MINMTU)
683 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
684 error = priv_check(td, PRIV_NET_SETIFMTU);
688 TUN2IFP(tp)->if_mtu = tunp->mtu;
689 TUN2IFP(tp)->if_type = tunp->type;
690 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
693 tunp = (struct tuninfo *)data;
694 tunp->mtu = TUN2IFP(tp)->if_mtu;
695 tunp->type = TUN2IFP(tp)->if_type;
696 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
699 tundebug = *(int *)data;
702 *(int *)data = tundebug;
705 mtx_lock(&tp->tun_mtx);
707 tp->tun_flags |= TUN_LMODE;
708 tp->tun_flags &= ~TUN_IFHEAD;
710 tp->tun_flags &= ~TUN_LMODE;
711 mtx_unlock(&tp->tun_mtx);
714 mtx_lock(&tp->tun_mtx);
716 tp->tun_flags |= TUN_IFHEAD;
717 tp->tun_flags &= ~TUN_LMODE;
719 tp->tun_flags &= ~TUN_IFHEAD;
720 mtx_unlock(&tp->tun_mtx);
723 /* Could be unlocked read? */
724 mtx_lock(&tp->tun_mtx);
725 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
726 mtx_unlock(&tp->tun_mtx);
729 /* deny this if UP */
730 if (TUN2IFP(tp)->if_flags & IFF_UP)
733 switch (*(int *)data & ~IFF_MULTICAST) {
734 case IFF_POINTOPOINT:
736 TUN2IFP(tp)->if_flags &=
737 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
738 TUN2IFP(tp)->if_flags |= *(int *)data;
745 mtx_lock(&tp->tun_mtx);
746 tp->tun_pid = curthread->td_proc->p_pid;
747 mtx_unlock(&tp->tun_mtx);
752 mtx_lock(&tp->tun_mtx);
754 tp->tun_flags |= TUN_ASYNC;
756 tp->tun_flags &= ~TUN_ASYNC;
757 mtx_unlock(&tp->tun_mtx);
761 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
763 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
764 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
765 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
766 *(int *)data += mb->m_len;
767 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
773 return (fsetown(*(int *)data, &tp->tun_sigio));
776 *(int *)data = fgetown(&tp->tun_sigio);
779 /* This is deprecated, FIOSETOWN should be used instead. */
781 return (fsetown(-(*(int *)data), &tp->tun_sigio));
783 /* This is deprecated, FIOGETOWN should be used instead. */
785 *(int *)data = -fgetown(&tp->tun_sigio);
795 * The cdevsw read interface - reads a packet at a time, or at
796 * least as much of a packet as can be read.
799 tunread(struct cdev *dev, struct uio *uio, int flag)
801 struct tun_softc *tp = dev->si_drv1;
802 struct ifnet *ifp = TUN2IFP(tp);
806 TUNDEBUG (ifp, "read\n");
807 mtx_lock(&tp->tun_mtx);
808 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
809 mtx_unlock(&tp->tun_mtx);
810 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
814 tp->tun_flags &= ~TUN_RWAIT;
815 mtx_unlock(&tp->tun_mtx);
819 IFQ_DEQUEUE(&ifp->if_snd, m);
821 if (flag & O_NONBLOCK) {
823 return (EWOULDBLOCK);
825 mtx_lock(&tp->tun_mtx);
826 tp->tun_flags |= TUN_RWAIT;
827 mtx_unlock(&tp->tun_mtx);
828 if ((error = tsleep(tp, PCATCH | (PZERO + 1),
829 "tunread", 0)) != 0) {
837 while (m && uio->uio_resid > 0 && error == 0) {
838 len = min(uio->uio_resid, m->m_len);
840 error = uiomove(mtod(m, void *), len, uio);
845 TUNDEBUG(ifp, "Dropping mbuf\n");
852 * the cdevsw write interface - an atomic write is a packet - or else!
855 tunwrite(struct cdev *dev, struct uio *uio, int flag)
857 struct tun_softc *tp = dev->si_drv1;
858 struct ifnet *ifp = TUN2IFP(tp);
864 TUNDEBUG(ifp, "tunwrite\n");
866 if ((ifp->if_flags & IFF_UP) != IFF_UP)
867 /* ignore silently */
870 if (uio->uio_resid == 0)
873 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
874 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
878 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
883 m->m_pkthdr.rcvif = ifp;
885 mac_ifnet_create_mbuf(ifp, m);
888 /* Could be unlocked read? */
889 mtx_lock(&tp->tun_mtx);
890 if (tp->tun_flags & TUN_IFHEAD) {
891 mtx_unlock(&tp->tun_mtx);
892 if (m->m_len < sizeof(family) &&
893 (m = m_pullup(m, sizeof(family))) == NULL)
895 family = ntohl(*mtod(m, u_int32_t *));
896 m_adj(m, sizeof(family));
898 mtx_unlock(&tp->tun_mtx);
902 BPF_MTAP2(ifp, &family, sizeof(family), m);
927 return (EAFNOSUPPORT);
929 /* First chunk of an mbuf contains good junk */
930 if (harvest.point_to_point)
931 random_harvest(m, 16, 3, 0, RANDOM_NET);
932 ifp->if_ibytes += m->m_pkthdr.len;
934 CURVNET_SET(ifp->if_vnet);
935 netisr_dispatch(isr, m);
941 * tunpoll - the poll interface, this is only useful on reads
942 * really. The write detect always returns true, write never blocks
943 * anyway, it either accepts the packet or drops it.
946 tunpoll(struct cdev *dev, int events, struct thread *td)
949 struct tun_softc *tp = dev->si_drv1;
950 struct ifnet *ifp = TUN2IFP(tp);
955 TUNDEBUG(ifp, "tunpoll\n");
957 if (events & (POLLIN | POLLRDNORM)) {
958 IFQ_LOCK(&ifp->if_snd);
959 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
961 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
962 revents |= events & (POLLIN | POLLRDNORM);
964 TUNDEBUG(ifp, "tunpoll waiting\n");
965 selrecord(td, &tp->tun_rsel);
967 IFQ_UNLOCK(&ifp->if_snd);
969 if (events & (POLLOUT | POLLWRNORM))
970 revents |= events & (POLLOUT | POLLWRNORM);
977 * tunkqfilter - support for the kevent() system call.
980 tunkqfilter(struct cdev *dev, struct knote *kn)
983 struct tun_softc *tp = dev->si_drv1;
984 struct ifnet *ifp = TUN2IFP(tp);
987 switch(kn->kn_filter) {
989 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
990 ifp->if_xname, dev2unit(dev));
991 kn->kn_fop = &tun_read_filterops;
995 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
996 ifp->if_xname, dev2unit(dev));
997 kn->kn_fop = &tun_write_filterops;
1001 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1002 ifp->if_xname, dev2unit(dev));
1008 kn->kn_hook = (caddr_t) dev;
1009 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1015 * Return true of there is data in the interface queue.
1018 tunkqread(struct knote *kn, long hint)
1021 struct cdev *dev = (struct cdev *)(kn->kn_hook);
1022 struct tun_softc *tp = dev->si_drv1;
1023 struct ifnet *ifp = TUN2IFP(tp);
1026 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1028 "%s have data in the queue. Len = %d, minor = %#x\n",
1029 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1033 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1043 * Always can write, always return MTU in kn->data.
1046 tunkqwrite(struct knote *kn, long hint)
1049 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1050 struct ifnet *ifp = TUN2IFP(tp);
1053 kn->kn_data = ifp->if_mtu;
1060 tunkqdetach(struct knote *kn)
1062 struct tun_softc *tp = ((struct cdev *)kn->kn_hook)->si_drv1;
1064 knlist_remove(&tp->tun_rsel.si_note, kn, 0);