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
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 const char tunname[] = "tun";
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 static 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 void 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 *);
137 static struct if_clone *tun_cloner;
139 static d_open_t tunopen;
140 static d_close_t tunclose;
141 static d_read_t tunread;
142 static d_write_t tunwrite;
143 static d_ioctl_t tunioctl;
144 static d_poll_t tunpoll;
145 static d_kqfilter_t tunkqfilter;
147 static int tunkqread(struct knote *, long);
148 static int tunkqwrite(struct knote *, long);
149 static void tunkqdetach(struct knote *);
151 static struct filterops tun_read_filterops = {
154 .f_detach = tunkqdetach,
155 .f_event = tunkqread,
158 static struct filterops tun_write_filterops = {
161 .f_detach = tunkqdetach,
162 .f_event = tunkqwrite,
165 static struct cdevsw tun_cdevsw = {
166 .d_version = D_VERSION,
167 .d_flags = D_NEEDMINOR,
174 .d_kqfilter = tunkqfilter,
179 tun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
184 /* find any existing device, or allocate new unit number */
185 i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
187 /* No preexisting struct cdev *, create one */
188 dev = make_dev(&tun_cdevsw, unit,
189 UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
191 tuncreate(tunname, dev);
197 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
200 char devname[SPECNAMELEN + 1];
201 int u, i, append_unit;
207 * If tun cloning is enabled, only the superuser can create an
210 if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
213 if (strcmp(name, tunname) == 0) {
215 } else if (dev_stdclone(name, NULL, tunname, &u) != 1)
216 return; /* Don't recognise the name */
217 if (u != -1 && u > IF_MAXUNIT)
218 return; /* Unit number too high */
225 CURVNET_SET(CRED_TO_VNET(cred));
226 /* find any existing device, or allocate new unit number */
227 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
230 namelen = snprintf(devname, sizeof(devname), "%s%d",
234 /* No preexisting struct cdev *, create one */
235 *dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
236 UID_UUCP, GID_DIALER, 0600, "%s", name);
239 if_clone_create(name, namelen, NULL);
244 tun_destroy(struct tun_softc *tp)
248 mtx_lock(&tp->tun_mtx);
249 if ((tp->tun_flags & TUN_OPEN) != 0)
250 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
252 mtx_unlock(&tp->tun_mtx);
254 CURVNET_SET(TUN2IFP(tp)->if_vnet);
256 bpfdetach(TUN2IFP(tp));
257 if_detach(TUN2IFP(tp));
258 if_free(TUN2IFP(tp));
260 seldrain(&tp->tun_rsel);
261 knlist_destroy(&tp->tun_rsel.si_note);
262 mtx_destroy(&tp->tun_mtx);
263 cv_destroy(&tp->tun_cv);
269 tun_clone_destroy(struct ifnet *ifp)
271 struct tun_softc *tp = ifp->if_softc;
274 TAILQ_REMOVE(&tunhead, tp, tun_list);
280 tunmodevent(module_t mod, int type, void *data)
282 static eventhandler_tag tag;
283 struct tun_softc *tp;
287 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
288 clone_setup(&tunclones);
289 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
292 tun_cloner = if_clone_simple(tunname, tun_clone_create,
293 tun_clone_destroy, 0);
296 if_clone_detach(tun_cloner);
297 EVENTHANDLER_DEREGISTER(dev_clone, tag);
298 drain_dev_clone_events();
301 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
302 TAILQ_REMOVE(&tunhead, tp, tun_list);
308 clone_cleanup(&tunclones);
309 mtx_destroy(&tunmtx);
317 static moduledata_t tun_mod = {
323 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
326 tunstart(struct ifnet *ifp)
328 struct tun_softc *tp = ifp->if_softc;
331 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
332 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
333 IFQ_LOCK(&ifp->if_snd);
334 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
336 IFQ_UNLOCK(&ifp->if_snd);
339 IFQ_UNLOCK(&ifp->if_snd);
342 mtx_lock(&tp->tun_mtx);
343 if (tp->tun_flags & TUN_RWAIT) {
344 tp->tun_flags &= ~TUN_RWAIT;
347 selwakeuppri(&tp->tun_rsel, PZERO + 1);
348 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
349 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
350 mtx_unlock(&tp->tun_mtx);
351 pgsigio(&tp->tun_sigio, SIGIO, 0);
353 mtx_unlock(&tp->tun_mtx);
356 /* XXX: should return an error code so it can fail. */
358 tuncreate(const char *name, struct cdev *dev)
360 struct tun_softc *sc;
363 dev->si_flags &= ~SI_CHEAPCLONE;
365 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
366 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
367 cv_init(&sc->tun_cv, "tun_condvar");
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_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
389 ifp->if_capabilities |= IFCAP_LINKSTATE;
390 ifp->if_capenable |= IFCAP_LINKSTATE;
393 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
395 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
396 ifp->if_xname, dev2unit(dev));
400 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
403 struct tun_softc *tp;
406 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
411 tuncreate(tunname, dev);
416 * XXXRW: This use of tun_pid is subject to error due to the
417 * fact that a reference to the tunnel can live beyond the
418 * death of the process that created it. Can we replace this
419 * with a simple busy flag?
421 mtx_lock(&tp->tun_mtx);
422 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
423 mtx_unlock(&tp->tun_mtx);
426 tp->tun_pid = td->td_proc->p_pid;
428 tp->tun_flags |= TUN_OPEN;
430 if_link_state_change(ifp, LINK_STATE_UP);
431 TUNDEBUG(ifp, "open\n");
432 mtx_unlock(&tp->tun_mtx);
438 * tunclose - close the device - mark i/f down & delete
442 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
444 struct tun_softc *tp;
450 mtx_lock(&tp->tun_mtx);
451 tp->tun_flags &= ~TUN_OPEN;
455 * junk all pending output
457 CURVNET_SET(ifp->if_vnet);
458 IFQ_PURGE(&ifp->if_snd);
460 if (ifp->if_flags & IFF_UP) {
461 mtx_unlock(&tp->tun_mtx);
463 mtx_lock(&tp->tun_mtx);
466 /* Delete all addresses and routes which reference this interface. */
467 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
470 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
471 mtx_unlock(&tp->tun_mtx);
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 mtx_lock(&tp->tun_mtx);
484 if_link_state_change(ifp, LINK_STATE_DOWN);
487 funsetown(&tp->tun_sigio);
488 selwakeuppri(&tp->tun_rsel, PZERO + 1);
489 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
490 TUNDEBUG (ifp, "closed\n");
492 cv_broadcast(&tp->tun_cv);
493 mtx_unlock(&tp->tun_mtx);
498 tuninit(struct ifnet *ifp)
500 struct tun_softc *tp = ifp->if_softc;
505 TUNDEBUG(ifp, "tuninit\n");
507 mtx_lock(&tp->tun_mtx);
508 ifp->if_flags |= IFF_UP;
509 ifp->if_drv_flags |= IFF_DRV_RUNNING;
510 getmicrotime(&ifp->if_lastchange);
514 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
515 if (ifa->ifa_addr->sa_family == AF_INET) {
516 struct sockaddr_in *si;
518 si = (struct sockaddr_in *)ifa->ifa_addr;
519 if (si->sin_addr.s_addr)
520 tp->tun_flags |= TUN_IASET;
522 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
523 if (si && si->sin_addr.s_addr)
524 tp->tun_flags |= TUN_DSTADDR;
527 if_addr_runlock(ifp);
529 mtx_unlock(&tp->tun_mtx);
533 * Process an ioctl request.
536 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
538 struct ifreq *ifr = (struct ifreq *)data;
539 struct tun_softc *tp = ifp->if_softc;
545 ifs = (struct ifstat *)data;
546 mtx_lock(&tp->tun_mtx);
548 sprintf(ifs->ascii + strlen(ifs->ascii),
549 "\tOpened by PID %d\n", tp->tun_pid);
550 mtx_unlock(&tp->tun_mtx);
554 TUNDEBUG(ifp, "address set\n");
558 TUNDEBUG(ifp, "destination address set\n");
561 ifp->if_mtu = ifr->ifr_mtu;
562 TUNDEBUG(ifp, "mtu set\n");
575 * tunoutput - queue packets from higher level ready to put out.
578 tunoutput(struct ifnet *ifp, struct mbuf *m0, 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 error = (ifp->if_transmit)(ifp, m0);
666 * the cdevsw interface is now pretty minimal.
669 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
673 struct tun_softc *tp = dev->si_drv1;
674 struct tuninfo *tunp;
678 tunp = (struct tuninfo *)data;
679 if (tunp->mtu < IF_MINMTU)
681 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
682 error = priv_check(td, PRIV_NET_SETIFMTU);
686 mtx_lock(&tp->tun_mtx);
687 TUN2IFP(tp)->if_mtu = tunp->mtu;
688 TUN2IFP(tp)->if_type = tunp->type;
689 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
690 mtx_unlock(&tp->tun_mtx);
693 tunp = (struct tuninfo *)data;
694 mtx_lock(&tp->tun_mtx);
695 tunp->mtu = TUN2IFP(tp)->if_mtu;
696 tunp->type = TUN2IFP(tp)->if_type;
697 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
698 mtx_unlock(&tp->tun_mtx);
701 tundebug = *(int *)data;
704 *(int *)data = tundebug;
707 mtx_lock(&tp->tun_mtx);
709 tp->tun_flags |= TUN_LMODE;
710 tp->tun_flags &= ~TUN_IFHEAD;
712 tp->tun_flags &= ~TUN_LMODE;
713 mtx_unlock(&tp->tun_mtx);
716 mtx_lock(&tp->tun_mtx);
718 tp->tun_flags |= TUN_IFHEAD;
719 tp->tun_flags &= ~TUN_LMODE;
721 tp->tun_flags &= ~TUN_IFHEAD;
722 mtx_unlock(&tp->tun_mtx);
725 mtx_lock(&tp->tun_mtx);
726 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
727 mtx_unlock(&tp->tun_mtx);
730 /* deny this if UP */
731 if (TUN2IFP(tp)->if_flags & IFF_UP)
734 switch (*(int *)data & ~IFF_MULTICAST) {
735 case IFF_POINTOPOINT:
737 mtx_lock(&tp->tun_mtx);
738 TUN2IFP(tp)->if_flags &=
739 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
740 TUN2IFP(tp)->if_flags |= *(int *)data;
741 mtx_unlock(&tp->tun_mtx);
748 mtx_lock(&tp->tun_mtx);
749 tp->tun_pid = curthread->td_proc->p_pid;
750 mtx_unlock(&tp->tun_mtx);
755 mtx_lock(&tp->tun_mtx);
757 tp->tun_flags |= TUN_ASYNC;
759 tp->tun_flags &= ~TUN_ASYNC;
760 mtx_unlock(&tp->tun_mtx);
763 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
765 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
766 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
767 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
768 *(int *)data += mb->m_len;
769 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
774 return (fsetown(*(int *)data, &tp->tun_sigio));
777 *(int *)data = fgetown(&tp->tun_sigio);
780 /* This is deprecated, FIOSETOWN should be used instead. */
782 return (fsetown(-(*(int *)data), &tp->tun_sigio));
784 /* This is deprecated, FIOGETOWN should be used instead. */
786 *(int *)data = -fgetown(&tp->tun_sigio);
796 * The cdevsw read interface - reads a packet at a time, or at
797 * least as much of a packet as can be read.
800 tunread(struct cdev *dev, struct uio *uio, int flag)
802 struct tun_softc *tp = dev->si_drv1;
803 struct ifnet *ifp = TUN2IFP(tp);
807 TUNDEBUG (ifp, "read\n");
808 mtx_lock(&tp->tun_mtx);
809 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
810 mtx_unlock(&tp->tun_mtx);
811 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
815 tp->tun_flags &= ~TUN_RWAIT;
818 IFQ_DEQUEUE(&ifp->if_snd, m);
820 if (flag & O_NONBLOCK) {
821 mtx_unlock(&tp->tun_mtx);
822 return (EWOULDBLOCK);
824 tp->tun_flags |= TUN_RWAIT;
825 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
828 mtx_unlock(&tp->tun_mtx);
833 mtx_unlock(&tp->tun_mtx);
835 while (m && uio->uio_resid > 0 && error == 0) {
836 len = min(uio->uio_resid, m->m_len);
838 error = uiomove(mtod(m, void *), len, uio);
843 TUNDEBUG(ifp, "Dropping mbuf\n");
850 * the cdevsw write interface - an atomic write is a packet - or else!
853 tunwrite(struct cdev *dev, struct uio *uio, int flag)
855 struct tun_softc *tp = dev->si_drv1;
856 struct ifnet *ifp = TUN2IFP(tp);
861 TUNDEBUG(ifp, "tunwrite\n");
863 if ((ifp->if_flags & IFF_UP) != IFF_UP)
864 /* ignore silently */
867 if (uio->uio_resid == 0)
870 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
871 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
875 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0, M_PKTHDR)) == NULL) {
880 m->m_pkthdr.rcvif = ifp;
882 mac_ifnet_create_mbuf(ifp, m);
885 /* Could be unlocked read? */
886 mtx_lock(&tp->tun_mtx);
887 if (tp->tun_flags & TUN_IFHEAD) {
888 mtx_unlock(&tp->tun_mtx);
889 if (m->m_len < sizeof(family) &&
890 (m = m_pullup(m, sizeof(family))) == NULL)
892 family = ntohl(*mtod(m, u_int32_t *));
893 m_adj(m, sizeof(family));
895 mtx_unlock(&tp->tun_mtx);
899 BPF_MTAP2(ifp, &family, sizeof(family), m);
924 return (EAFNOSUPPORT);
926 /* First chunk of an mbuf contains good junk */
927 if (harvest.point_to_point)
928 random_harvest(m, 16, 3, 0, RANDOM_NET);
929 ifp->if_ibytes += m->m_pkthdr.len;
931 CURVNET_SET(ifp->if_vnet);
932 M_SETFIB(m, ifp->if_fib);
933 netisr_dispatch(isr, m);
939 * tunpoll - the poll interface, this is only useful on reads
940 * really. The write detect always returns true, write never blocks
941 * anyway, it either accepts the packet or drops it.
944 tunpoll(struct cdev *dev, int events, struct thread *td)
946 struct tun_softc *tp = dev->si_drv1;
947 struct ifnet *ifp = TUN2IFP(tp);
951 TUNDEBUG(ifp, "tunpoll\n");
953 if (events & (POLLIN | POLLRDNORM)) {
954 IFQ_LOCK(&ifp->if_snd);
955 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
957 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
958 revents |= events & (POLLIN | POLLRDNORM);
960 TUNDEBUG(ifp, "tunpoll waiting\n");
961 selrecord(td, &tp->tun_rsel);
963 IFQ_UNLOCK(&ifp->if_snd);
965 if (events & (POLLOUT | POLLWRNORM))
966 revents |= events & (POLLOUT | POLLWRNORM);
972 * tunkqfilter - support for the kevent() system call.
975 tunkqfilter(struct cdev *dev, struct knote *kn)
977 struct tun_softc *tp = dev->si_drv1;
978 struct ifnet *ifp = TUN2IFP(tp);
980 switch(kn->kn_filter) {
982 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
983 ifp->if_xname, dev2unit(dev));
984 kn->kn_fop = &tun_read_filterops;
988 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
989 ifp->if_xname, dev2unit(dev));
990 kn->kn_fop = &tun_write_filterops;
994 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
995 ifp->if_xname, dev2unit(dev));
1000 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1006 * Return true of there is data in the interface queue.
1009 tunkqread(struct knote *kn, long hint)
1012 struct tun_softc *tp = kn->kn_hook;
1013 struct cdev *dev = tp->tun_dev;
1014 struct ifnet *ifp = TUN2IFP(tp);
1016 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1018 "%s have data in the queue. Len = %d, minor = %#x\n",
1019 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1023 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1032 * Always can write, always return MTU in kn->data.
1035 tunkqwrite(struct knote *kn, long hint)
1037 struct tun_softc *tp = kn->kn_hook;
1038 struct ifnet *ifp = TUN2IFP(tp);
1040 kn->kn_data = ifp->if_mtu;
1046 tunkqdetach(struct knote *kn)
1048 struct tun_softc *tp = kn->kn_hook;
1050 knlist_remove(&tp->tun_rsel.si_note, kn, 0);