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>
26 #include <sys/systm.h>
29 #include <sys/module.h>
30 #include <sys/socket.h>
31 #include <sys/fcntl.h>
32 #include <sys/filio.h>
33 #include <sys/sockio.h>
35 #include <sys/syslog.h>
36 #include <sys/ttycom.h>
38 #include <sys/selinfo.h>
39 #include <sys/signalvar.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/random.h>
47 #include <sys/ctype.h>
50 #include <net/if_var.h>
51 #include <net/if_clone.h>
52 #include <net/if_types.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
57 #include <netinet/in.h>
60 #include <net/if_tun.h>
62 #include <sys/queue.h>
63 #include <sys/condvar.h>
65 #include <security/mac/mac_framework.h>
68 * tun_list is protected by global tunmtx. Other mutable fields are
69 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
70 * static for the duration of a tunnel interface.
73 TAILQ_ENTRY(tun_softc) tun_list;
75 u_short tun_flags; /* misc flags */
76 #define TUN_OPEN 0x0001
77 #define TUN_INITED 0x0002
78 #define TUN_RCOLL 0x0004
79 #define TUN_IASET 0x0008
80 #define TUN_DSTADDR 0x0010
81 #define TUN_LMODE 0x0020
82 #define TUN_RWAIT 0x0040
83 #define TUN_ASYNC 0x0080
84 #define TUN_IFHEAD 0x0100
85 #define TUN_DYING 0x0200
87 #define TUN_READY (TUN_OPEN | TUN_INITED)
89 pid_t tun_pid; /* owning pid */
90 struct ifnet *tun_ifp; /* the interface */
91 struct sigio *tun_sigio; /* information for async I/O */
92 struct selinfo tun_rsel; /* read select */
93 struct mtx tun_mtx; /* protect mutable softc fields */
94 struct cv tun_cv; /* protect against ref'd dev destroy */
96 #define TUN2IFP(sc) ((sc)->tun_ifp)
98 #define TUNDEBUG if (tundebug) if_printf
101 * All mutable global variables in if_tun are locked using tunmtx, with
102 * the exception of tundebug, which is used unlocked, and tunclones,
103 * which is static after setup.
105 static struct mtx tunmtx;
106 static eventhandler_tag tag;
107 static const char tunname[] = "tun";
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 static struct sx tun_ioctl_sx;
116 SX_SYSINIT(tun_ioctl_sx, &tun_ioctl_sx, "tun_ioctl");
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) != 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 tp->tun_flags |= TUN_DYING;
276 if ((tp->tun_flags & TUN_OPEN) != 0)
277 cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
279 mtx_unlock(&tp->tun_mtx);
281 CURVNET_SET(TUN2IFP(tp)->if_vnet);
284 bpfdetach(TUN2IFP(tp));
285 if_detach(TUN2IFP(tp));
287 sx_xlock(&tun_ioctl_sx);
288 TUN2IFP(tp)->if_softc = NULL;
289 sx_xunlock(&tun_ioctl_sx);
291 free_unr(tun_unrhdr, TUN2IFP(tp)->if_dunit);
292 if_free(TUN2IFP(tp));
294 seldrain(&tp->tun_rsel);
295 knlist_clear(&tp->tun_rsel.si_note, 0);
296 knlist_destroy(&tp->tun_rsel.si_note);
297 mtx_destroy(&tp->tun_mtx);
298 cv_destroy(&tp->tun_cv);
304 tun_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
306 struct tun_softc *tp = ifp->if_softc;
309 TAILQ_REMOVE(&tunhead, tp, tun_list);
317 vnet_tun_init(const void *unused __unused)
319 V_tun_cloner = if_clone_advanced(tunname, 0, tun_clone_match,
320 tun_clone_create, tun_clone_destroy);
322 VNET_SYSINIT(vnet_tun_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
323 vnet_tun_init, NULL);
326 vnet_tun_uninit(const void *unused __unused)
328 if_clone_detach(V_tun_cloner);
330 VNET_SYSUNINIT(vnet_tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
331 vnet_tun_uninit, NULL);
334 tun_uninit(const void *unused __unused)
336 struct tun_softc *tp;
338 EVENTHANDLER_DEREGISTER(dev_clone, tag);
339 drain_dev_clone_events();
342 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
343 TAILQ_REMOVE(&tunhead, tp, tun_list);
349 delete_unrhdr(tun_unrhdr);
350 clone_cleanup(&tunclones);
351 mtx_destroy(&tunmtx);
353 SYSUNINIT(tun_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY, tun_uninit, NULL);
356 tunmodevent(module_t mod, int type, void *data)
361 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
362 clone_setup(&tunclones);
363 tun_unrhdr = new_unrhdr(0, IF_MAXUNIT, &tunmtx);
364 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
369 /* See tun_uninit, so it's done after the vnet_sysuninit() */
377 static moduledata_t tun_mod = {
383 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
384 MODULE_VERSION(if_tun, 1);
387 tunstart(struct ifnet *ifp)
389 struct tun_softc *tp = ifp->if_softc;
392 TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
393 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
394 IFQ_LOCK(&ifp->if_snd);
395 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
397 IFQ_UNLOCK(&ifp->if_snd);
400 IFQ_UNLOCK(&ifp->if_snd);
403 mtx_lock(&tp->tun_mtx);
404 if (tp->tun_flags & TUN_RWAIT) {
405 tp->tun_flags &= ~TUN_RWAIT;
408 selwakeuppri(&tp->tun_rsel, PZERO + 1);
409 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
410 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
411 mtx_unlock(&tp->tun_mtx);
412 pgsigio(&tp->tun_sigio, SIGIO, 0);
414 mtx_unlock(&tp->tun_mtx);
417 /* XXX: should return an error code so it can fail. */
419 tuncreate(const char *name, struct cdev *dev)
421 struct tun_softc *sc;
424 sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
425 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
426 cv_init(&sc->tun_cv, "tun_condvar");
427 sc->tun_flags = TUN_INITED;
430 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
433 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
435 panic("%s%d: failed to if_alloc() interface.\n",
436 name, dev2unit(dev));
437 if_initname(ifp, name, dev2unit(dev));
438 ifp->if_mtu = TUNMTU;
439 ifp->if_ioctl = tunifioctl;
440 ifp->if_output = tunoutput;
441 ifp->if_start = tunstart;
442 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
444 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
445 ifp->if_snd.ifq_drv_maxlen = 0;
446 IFQ_SET_READY(&ifp->if_snd);
447 knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
448 ifp->if_capabilities |= IFCAP_LINKSTATE;
449 ifp->if_capenable |= IFCAP_LINKSTATE;
452 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
454 TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
455 ifp->if_xname, dev2unit(dev));
459 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
462 struct tun_softc *tp;
465 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
470 tuncreate(tunname, dev);
474 mtx_lock(&tp->tun_mtx);
475 if ((tp->tun_flags & (TUN_OPEN | TUN_DYING)) != 0) {
476 mtx_unlock(&tp->tun_mtx);
480 tp->tun_pid = td->td_proc->p_pid;
481 tp->tun_flags |= TUN_OPEN;
483 if_link_state_change(ifp, LINK_STATE_UP);
484 TUNDEBUG(ifp, "open\n");
485 mtx_unlock(&tp->tun_mtx);
491 * tunclose - close the device - mark i/f down & delete
495 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
498 struct tun_softc *tp;
505 mtx_lock(&tp->tun_mtx);
508 * Realistically, we can't be obstinate here. This only means that the
509 * tuntap device was closed out of order, and the last closer wasn't the
510 * controller. These are still good to know about, though, as software
511 * should avoid multiple processes with a tuntap device open and
512 * ill-defined transfer of control (e.g., handoff, TUNSIFPID, close in
515 if (p->p_pid != tp->tun_pid) {
517 "pid %d (%s), %s: tun/tap protocol violation, non-controlling process closed last.\n",
518 p->p_pid, p->p_comm, dev->si_name);
522 * junk all pending output
524 CURVNET_SET(ifp->if_vnet);
525 IFQ_PURGE(&ifp->if_snd);
527 if (ifp->if_flags & IFF_UP) {
528 mtx_unlock(&tp->tun_mtx);
530 mtx_lock(&tp->tun_mtx);
533 /* Delete all addresses and routes which reference this interface. */
534 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
537 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
538 mtx_unlock(&tp->tun_mtx);
539 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
540 /* deal w/IPv4 PtP destination; unlocked read */
541 if (ifa->ifa_addr->sa_family == AF_INET) {
542 rtinit(ifa, (int)RTM_DELETE,
543 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
545 rtinit(ifa, (int)RTM_DELETE, 0);
549 mtx_lock(&tp->tun_mtx);
551 if_link_state_change(ifp, LINK_STATE_DOWN);
554 funsetown(&tp->tun_sigio);
555 selwakeuppri(&tp->tun_rsel, PZERO + 1);
556 KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
557 TUNDEBUG (ifp, "closed\n");
558 tp->tun_flags &= ~TUN_OPEN;
561 cv_broadcast(&tp->tun_cv);
562 mtx_unlock(&tp->tun_mtx);
567 tuninit(struct ifnet *ifp)
569 struct tun_softc *tp = ifp->if_softc;
574 TUNDEBUG(ifp, "tuninit\n");
576 mtx_lock(&tp->tun_mtx);
577 ifp->if_flags |= IFF_UP;
578 ifp->if_drv_flags |= IFF_DRV_RUNNING;
579 getmicrotime(&ifp->if_lastchange);
583 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
584 if (ifa->ifa_addr->sa_family == AF_INET) {
585 struct sockaddr_in *si;
587 si = (struct sockaddr_in *)ifa->ifa_addr;
588 if (si->sin_addr.s_addr)
589 tp->tun_flags |= TUN_IASET;
591 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
592 if (si && si->sin_addr.s_addr)
593 tp->tun_flags |= TUN_DSTADDR;
596 if_addr_runlock(ifp);
598 mtx_unlock(&tp->tun_mtx);
602 * Process an ioctl request.
605 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
607 struct ifreq *ifr = (struct ifreq *)data;
608 struct tun_softc *tp;
612 sx_xlock(&tun_ioctl_sx);
620 ifs = (struct ifstat *)data;
621 mtx_lock(&tp->tun_mtx);
623 snprintf(ifs->ascii, sizeof(ifs->ascii),
624 "\tOpened by PID %d\n", tp->tun_pid);
626 ifs->ascii[0] = '\0';
627 mtx_unlock(&tp->tun_mtx);
631 TUNDEBUG(ifp, "address set\n");
634 ifp->if_mtu = ifr->ifr_mtu;
635 TUNDEBUG(ifp, "mtu set\n");
645 sx_xunlock(&tun_ioctl_sx);
650 * tunoutput - queue packets from higher level ready to put out.
653 tunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
656 struct tun_softc *tp = ifp->if_softc;
657 u_short cached_tun_flags;
661 TUNDEBUG (ifp, "tunoutput\n");
664 error = mac_ifnet_check_transmit(ifp, m0);
671 /* Could be unlocked read? */
672 mtx_lock(&tp->tun_mtx);
673 cached_tun_flags = tp->tun_flags;
674 mtx_unlock(&tp->tun_mtx);
675 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
676 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
681 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
686 /* BPF writes need to be handled specially. */
687 if (dst->sa_family == AF_UNSPEC)
688 bcopy(dst->sa_data, &af, sizeof(af));
692 if (bpf_peers_present(ifp->if_bpf))
693 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
695 /* prepend sockaddr? this may abort if the mbuf allocation fails */
696 if (cached_tun_flags & TUN_LMODE) {
697 /* allocate space for sockaddr */
698 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
700 /* if allocation failed drop packet */
702 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
703 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
706 bcopy(dst, m0->m_data, dst->sa_len);
710 if (cached_tun_flags & TUN_IFHEAD) {
711 /* Prepend the address family */
712 M_PREPEND(m0, 4, M_NOWAIT);
714 /* if allocation failed drop packet */
716 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
717 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
720 *(u_int32_t *)m0->m_data = htonl(af);
727 return (EAFNOSUPPORT);
731 error = (ifp->if_transmit)(ifp, m0);
734 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
739 * the cdevsw interface is now pretty minimal.
742 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
745 struct ifreq ifr, *ifrp;
746 struct tun_softc *tp = dev->si_drv1;
747 struct tuninfo *tunp;
752 ifrp = (struct ifreq *)data;
753 strlcpy(ifrp->ifr_name, TUN2IFP(tp)->if_xname, IFNAMSIZ);
756 tunp = (struct tuninfo *)data;
757 if (TUN2IFP(tp)->if_type != tunp->type)
759 mtx_lock(&tp->tun_mtx);
760 if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
761 strlcpy(ifr.ifr_name, if_name(TUN2IFP(tp)), IFNAMSIZ);
762 ifr.ifr_mtu = tunp->mtu;
763 CURVNET_SET(TUN2IFP(tp)->if_vnet);
764 error = ifhwioctl(SIOCSIFMTU, TUN2IFP(tp),
768 mtx_unlock(&tp->tun_mtx);
772 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
773 mtx_unlock(&tp->tun_mtx);
776 tunp = (struct tuninfo *)data;
777 mtx_lock(&tp->tun_mtx);
778 tunp->mtu = TUN2IFP(tp)->if_mtu;
779 tunp->type = TUN2IFP(tp)->if_type;
780 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
781 mtx_unlock(&tp->tun_mtx);
784 tundebug = *(int *)data;
787 *(int *)data = tundebug;
790 mtx_lock(&tp->tun_mtx);
792 tp->tun_flags |= TUN_LMODE;
793 tp->tun_flags &= ~TUN_IFHEAD;
795 tp->tun_flags &= ~TUN_LMODE;
796 mtx_unlock(&tp->tun_mtx);
799 mtx_lock(&tp->tun_mtx);
801 tp->tun_flags |= TUN_IFHEAD;
802 tp->tun_flags &= ~TUN_LMODE;
804 tp->tun_flags &= ~TUN_IFHEAD;
805 mtx_unlock(&tp->tun_mtx);
808 mtx_lock(&tp->tun_mtx);
809 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
810 mtx_unlock(&tp->tun_mtx);
813 /* deny this if UP */
814 if (TUN2IFP(tp)->if_flags & IFF_UP)
817 switch (*(int *)data & ~IFF_MULTICAST) {
818 case IFF_POINTOPOINT:
820 mtx_lock(&tp->tun_mtx);
821 TUN2IFP(tp)->if_flags &=
822 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
823 TUN2IFP(tp)->if_flags |= *(int *)data;
824 mtx_unlock(&tp->tun_mtx);
831 mtx_lock(&tp->tun_mtx);
832 tp->tun_pid = curthread->td_proc->p_pid;
833 mtx_unlock(&tp->tun_mtx);
838 mtx_lock(&tp->tun_mtx);
840 tp->tun_flags |= TUN_ASYNC;
842 tp->tun_flags &= ~TUN_ASYNC;
843 mtx_unlock(&tp->tun_mtx);
846 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
848 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
849 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
850 for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
851 *(int *)data += mb->m_len;
852 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
857 return (fsetown(*(int *)data, &tp->tun_sigio));
860 *(int *)data = fgetown(&tp->tun_sigio);
863 /* This is deprecated, FIOSETOWN should be used instead. */
865 return (fsetown(-(*(int *)data), &tp->tun_sigio));
867 /* This is deprecated, FIOGETOWN should be used instead. */
869 *(int *)data = -fgetown(&tp->tun_sigio);
879 * The cdevsw read interface - reads a packet at a time, or at
880 * least as much of a packet as can be read.
883 tunread(struct cdev *dev, struct uio *uio, int flag)
885 struct tun_softc *tp = dev->si_drv1;
886 struct ifnet *ifp = TUN2IFP(tp);
890 TUNDEBUG (ifp, "read\n");
891 mtx_lock(&tp->tun_mtx);
892 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
893 mtx_unlock(&tp->tun_mtx);
894 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
898 tp->tun_flags &= ~TUN_RWAIT;
901 IFQ_DEQUEUE(&ifp->if_snd, m);
903 if (flag & O_NONBLOCK) {
904 mtx_unlock(&tp->tun_mtx);
905 return (EWOULDBLOCK);
907 tp->tun_flags |= TUN_RWAIT;
908 error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
911 mtx_unlock(&tp->tun_mtx);
916 mtx_unlock(&tp->tun_mtx);
918 while (m && uio->uio_resid > 0 && error == 0) {
919 len = min(uio->uio_resid, m->m_len);
921 error = uiomove(mtod(m, void *), len, uio);
926 TUNDEBUG(ifp, "Dropping mbuf\n");
933 * the cdevsw write interface - an atomic write is a packet - or else!
936 tunwrite(struct cdev *dev, struct uio *uio, int flag)
938 struct tun_softc *tp = dev->si_drv1;
939 struct ifnet *ifp = TUN2IFP(tp);
941 uint32_t family, mru;
944 TUNDEBUG(ifp, "tunwrite\n");
946 if ((ifp->if_flags & IFF_UP) != IFF_UP)
947 /* ignore silently */
950 if (uio->uio_resid == 0)
954 if (tp->tun_flags & TUN_IFHEAD)
955 mru += sizeof(family);
956 if (uio->uio_resid < 0 || uio->uio_resid > mru) {
957 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
961 if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
962 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
966 m->m_pkthdr.rcvif = ifp;
968 mac_ifnet_create_mbuf(ifp, m);
971 /* Could be unlocked read? */
972 mtx_lock(&tp->tun_mtx);
973 if (tp->tun_flags & TUN_IFHEAD) {
974 mtx_unlock(&tp->tun_mtx);
975 if (m->m_len < sizeof(family) &&
976 (m = m_pullup(m, sizeof(family))) == NULL)
978 family = ntohl(*mtod(m, u_int32_t *));
979 m_adj(m, sizeof(family));
981 mtx_unlock(&tp->tun_mtx);
985 BPF_MTAP2(ifp, &family, sizeof(family), m);
1000 return (EAFNOSUPPORT);
1002 random_harvest_queue(m, sizeof(*m), RANDOM_NET_TUN);
1003 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
1004 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1005 CURVNET_SET(ifp->if_vnet);
1006 M_SETFIB(m, ifp->if_fib);
1007 netisr_dispatch(isr, m);
1013 * tunpoll - the poll interface, this is only useful on reads
1014 * really. The write detect always returns true, write never blocks
1015 * anyway, it either accepts the packet or drops it.
1018 tunpoll(struct cdev *dev, int events, struct thread *td)
1020 struct tun_softc *tp = dev->si_drv1;
1021 struct ifnet *ifp = TUN2IFP(tp);
1025 TUNDEBUG(ifp, "tunpoll\n");
1027 if (events & (POLLIN | POLLRDNORM)) {
1028 IFQ_LOCK(&ifp->if_snd);
1029 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
1031 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
1032 revents |= events & (POLLIN | POLLRDNORM);
1034 TUNDEBUG(ifp, "tunpoll waiting\n");
1035 selrecord(td, &tp->tun_rsel);
1037 IFQ_UNLOCK(&ifp->if_snd);
1039 if (events & (POLLOUT | POLLWRNORM))
1040 revents |= events & (POLLOUT | POLLWRNORM);
1046 * tunkqfilter - support for the kevent() system call.
1049 tunkqfilter(struct cdev *dev, struct knote *kn)
1051 struct tun_softc *tp = dev->si_drv1;
1052 struct ifnet *ifp = TUN2IFP(tp);
1054 switch(kn->kn_filter) {
1056 TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
1057 ifp->if_xname, dev2unit(dev));
1058 kn->kn_fop = &tun_read_filterops;
1062 TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1063 ifp->if_xname, dev2unit(dev));
1064 kn->kn_fop = &tun_write_filterops;
1068 TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
1069 ifp->if_xname, dev2unit(dev));
1074 knlist_add(&tp->tun_rsel.si_note, kn, 0);
1080 * Return true of there is data in the interface queue.
1083 tunkqread(struct knote *kn, long hint)
1086 struct tun_softc *tp = kn->kn_hook;
1087 struct cdev *dev = tp->tun_dev;
1088 struct ifnet *ifp = TUN2IFP(tp);
1090 if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1092 "%s have data in the queue. Len = %d, minor = %#x\n",
1093 ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1097 "%s waiting for data, minor = %#x\n", ifp->if_xname,
1106 * Always can write, always return MTU in kn->data.
1109 tunkqwrite(struct knote *kn, long hint)
1111 struct tun_softc *tp = kn->kn_hook;
1112 struct ifnet *ifp = TUN2IFP(tp);
1114 kn->kn_data = ifp->if_mtu;
1120 tunkqdetach(struct knote *kn)
1122 struct tun_softc *tp = kn->kn_hook;
1124 knlist_remove(&tp->tun_rsel.si_note, kn, 0);