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>
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_types.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
55 #include <net/if_tun.h>
57 #include <sys/queue.h>
60 * tun_list is protected by global tunmtx. Other mutable fields are
61 * protected by tun->tun_mtx, or by their owning subsystem. tun_dev is
62 * static for the duration of a tunnel interface.
65 TAILQ_ENTRY(tun_softc) tun_list;
67 u_short tun_flags; /* misc flags */
68 #define TUN_OPEN 0x0001
69 #define TUN_INITED 0x0002
70 #define TUN_RCOLL 0x0004
71 #define TUN_IASET 0x0008
72 #define TUN_DSTADDR 0x0010
73 #define TUN_LMODE 0x0020
74 #define TUN_RWAIT 0x0040
75 #define TUN_ASYNC 0x0080
76 #define TUN_IFHEAD 0x0100
78 #define TUN_READY (TUN_OPEN | TUN_INITED)
81 * XXXRW: tun_pid is used to exclusively lock /dev/tun. Is this
82 * actually needed? Can we just return EBUSY if already open?
83 * Problem is that this involved inherent races when a tun device
84 * is handed off from one process to another, as opposed to just
85 * being slightly stale informationally.
87 pid_t tun_pid; /* owning pid */
88 struct ifnet *tun_ifp; /* the interface */
89 struct sigio *tun_sigio; /* information for async I/O */
90 struct selinfo tun_rsel; /* read select */
91 struct mtx tun_mtx; /* protect mutable softc fields */
93 #define TUN2IFP(sc) ((sc)->tun_ifp)
95 #define TUNDEBUG if (tundebug) if_printf
99 * All mutable global variables in if_tun are locked using tunmtx, with
100 * the exception of tundebug, which is used unlocked, and tunclones,
101 * which is static after setup.
103 static struct mtx tunmtx;
104 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
105 static int tundebug = 0;
106 static struct clonedevs *tunclones;
107 static TAILQ_HEAD(,tun_softc) tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
108 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
110 static void tunclone(void *arg, struct ucred *cred, char *name,
111 int namelen, struct cdev **dev);
112 static void tuncreate(struct cdev *dev);
113 static int tunifioctl(struct ifnet *, u_long, caddr_t);
114 static int tuninit(struct ifnet *);
115 static int tunmodevent(module_t, int, void *);
116 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
118 static void tunstart(struct ifnet *);
120 static d_open_t tunopen;
121 static d_close_t tunclose;
122 static d_read_t tunread;
123 static d_write_t tunwrite;
124 static d_ioctl_t tunioctl;
125 static d_poll_t tunpoll;
127 static struct cdevsw tun_cdevsw = {
128 .d_version = D_VERSION,
129 .d_flags = D_PSEUDO | D_NEEDGIANT,
140 tunclone(void *arg, struct ucred *cred, char *name, int namelen,
148 if (strcmp(name, TUNNAME) == 0) {
150 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
151 return; /* Don't recognise the name */
152 if (u != -1 && u > IF_MAXUNIT)
153 return; /* Unit number too high */
155 /* find any existing device, or allocate new unit number */
156 i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
158 /* No preexisting struct cdev *, create one */
159 *dev = make_dev(&tun_cdevsw, unit2minor(u),
160 UID_UUCP, GID_DIALER, 0600, "tun%d", u);
163 (*dev)->si_flags |= SI_CHEAPCLONE;
169 tun_destroy(struct tun_softc *tp)
174 KASSERT((tp->tun_flags & TUN_OPEN) == 0,
175 ("tununits is out of sync - unit %d", TUN2IFP(tp)->if_dunit));
178 bpfdetach(TUN2IFP(tp));
179 if_detach(TUN2IFP(tp));
180 if_free(TUN2IFP(tp));
182 mtx_destroy(&tp->tun_mtx);
187 tunmodevent(module_t mod, int type, void *data)
189 static eventhandler_tag tag;
190 struct tun_softc *tp;
194 mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
195 clone_setup(&tunclones);
196 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
201 EVENTHANDLER_DEREGISTER(dev_clone, tag);
204 while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
205 TAILQ_REMOVE(&tunhead, tp, tun_list);
211 clone_cleanup(&tunclones);
212 mtx_destroy(&tunmtx);
220 static moduledata_t tun_mod = {
226 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
229 tunstart(struct ifnet *ifp)
231 struct tun_softc *tp = ifp->if_softc;
234 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
235 IFQ_LOCK(&ifp->if_snd);
236 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
238 IFQ_UNLOCK(&ifp->if_snd);
241 IFQ_UNLOCK(&ifp->if_snd);
244 mtx_lock(&tp->tun_mtx);
245 if (tp->tun_flags & TUN_RWAIT) {
246 tp->tun_flags &= ~TUN_RWAIT;
249 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
250 mtx_unlock(&tp->tun_mtx);
251 pgsigio(&tp->tun_sigio, SIGIO, 0);
253 mtx_unlock(&tp->tun_mtx);
254 selwakeuppri(&tp->tun_rsel, PZERO + 1);
257 /* XXX: should return an error code so it can fail. */
259 tuncreate(struct cdev *dev)
261 struct tun_softc *sc;
264 dev->si_flags &= ~SI_CHEAPCLONE;
266 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
267 mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
268 sc->tun_flags = TUN_INITED;
271 TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
274 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
276 panic("%s%d: failed to if_alloc() interface.\n",
277 TUNNAME, dev2unit(dev));
278 if_initname(ifp, TUNNAME, dev2unit(dev));
279 ifp->if_mtu = TUNMTU;
280 ifp->if_ioctl = tunifioctl;
281 ifp->if_output = tunoutput;
282 ifp->if_start = tunstart;
283 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
285 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
286 ifp->if_snd.ifq_drv_maxlen = 0;
287 IFQ_SET_READY(&ifp->if_snd);
290 bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
295 tunopen(struct cdev *dev, int flag, int mode, struct thread *td)
298 struct tun_softc *tp;
301 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
311 * XXXRW: This use of tun_pid is subject to error due to the
312 * fact that a reference to the tunnel can live beyond the
313 * death of the process that created it. Can we replace this
314 * with a simple busy flag?
316 mtx_lock(&tp->tun_mtx);
317 if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
318 mtx_unlock(&tp->tun_mtx);
321 tp->tun_pid = td->td_proc->p_pid;
323 tp->tun_flags |= TUN_OPEN;
324 mtx_unlock(&tp->tun_mtx);
326 TUNDEBUG(ifp, "open\n");
332 * tunclose - close the device - mark i/f down & delete
336 tunclose(struct cdev *dev, int foo, int bar, struct thread *td)
338 struct tun_softc *tp;
345 mtx_lock(&tp->tun_mtx);
346 tp->tun_flags &= ~TUN_OPEN;
350 * junk all pending output
353 IFQ_PURGE(&ifp->if_snd);
355 mtx_unlock(&tp->tun_mtx);
357 if (ifp->if_flags & IFF_UP) {
363 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
367 /* find internet addresses and delete routes */
368 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
369 if (ifa->ifa_addr->sa_family == AF_INET)
371 rtinit(ifa, (int)RTM_DELETE,
372 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
373 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
377 funsetown(&tp->tun_sigio);
378 selwakeuppri(&tp->tun_rsel, PZERO + 1);
379 TUNDEBUG (ifp, "closed\n");
384 tuninit(struct ifnet *ifp)
386 struct tun_softc *tp = ifp->if_softc;
390 TUNDEBUG(ifp, "tuninit\n");
392 ifp->if_flags |= IFF_UP;
393 ifp->if_drv_flags |= IFF_DRV_RUNNING;
394 getmicrotime(&ifp->if_lastchange);
396 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
397 ifa = TAILQ_NEXT(ifa, ifa_link)) {
398 if (ifa->ifa_addr == NULL)
400 /* XXX: Should maybe return straight off? */
403 if (ifa->ifa_addr->sa_family == AF_INET) {
404 struct sockaddr_in *si;
406 si = (struct sockaddr_in *)ifa->ifa_addr;
407 mtx_lock(&tp->tun_mtx);
408 if (si->sin_addr.s_addr)
409 tp->tun_flags |= TUN_IASET;
411 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
412 if (si && si->sin_addr.s_addr)
413 tp->tun_flags |= TUN_DSTADDR;
414 mtx_unlock(&tp->tun_mtx);
423 * Process an ioctl request.
426 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
428 struct ifreq *ifr = (struct ifreq *)data;
429 struct tun_softc *tp = ifp->if_softc;
436 ifs = (struct ifstat *)data;
437 mtx_lock(&tp->tun_mtx);
439 sprintf(ifs->ascii + strlen(ifs->ascii),
440 "\tOpened by PID %d\n", tp->tun_pid);
441 mtx_unlock(&tp->tun_mtx);
444 error = tuninit(ifp);
445 TUNDEBUG(ifp, "address set, error=%d\n", error);
448 error = tuninit(ifp);
449 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
452 ifp->if_mtu = ifr->ifr_mtu;
453 TUNDEBUG(ifp, "mtu set\n");
467 * tunoutput - queue packets from higher level ready to put out.
473 struct sockaddr *dst,
476 struct tun_softc *tp = ifp->if_softc;
477 u_short cached_tun_flags;
481 TUNDEBUG (ifp, "tunoutput\n");
484 error = mac_check_ifnet_transmit(ifp, m0);
491 /* Could be unlocked read? */
492 mtx_lock(&tp->tun_mtx);
493 cached_tun_flags = tp->tun_flags;
494 mtx_unlock(&tp->tun_mtx);
495 if ((cached_tun_flags & TUN_READY) != TUN_READY) {
496 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
501 if ((ifp->if_flags & IFF_UP) != IFF_UP) {
506 /* BPF writes need to be handled specially. */
507 if (dst->sa_family == AF_UNSPEC) {
508 bcopy(dst->sa_data, &af, sizeof(af));
514 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
517 /* prepend sockaddr? this may abort if the mbuf allocation fails */
518 if (cached_tun_flags & TUN_LMODE) {
519 /* allocate space for sockaddr */
520 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
522 /* if allocation failed drop packet */
528 bcopy(dst, m0->m_data, dst->sa_len);
532 if (cached_tun_flags & TUN_IFHEAD) {
533 /* Prepend the address family */
534 M_PREPEND(m0, 4, M_DONTWAIT);
536 /* if allocation failed drop packet */
542 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
545 if (dst->sa_family != AF_INET)
549 return (EAFNOSUPPORT);
553 IFQ_HANDOFF(ifp, m0, error);
555 ifp->if_collisions++;
563 * the cdevsw interface is now pretty minimal.
566 tunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
570 struct tun_softc *tp = dev->si_drv1;
571 struct tuninfo *tunp;
575 tunp = (struct tuninfo *)data;
576 if (tunp->mtu < IF_MINMTU)
578 if (TUN2IFP(tp)->if_mtu != tunp->mtu
579 && (error = suser(td)) != 0)
581 TUN2IFP(tp)->if_mtu = tunp->mtu;
582 TUN2IFP(tp)->if_type = tunp->type;
583 TUN2IFP(tp)->if_baudrate = tunp->baudrate;
586 tunp = (struct tuninfo *)data;
587 tunp->mtu = TUN2IFP(tp)->if_mtu;
588 tunp->type = TUN2IFP(tp)->if_type;
589 tunp->baudrate = TUN2IFP(tp)->if_baudrate;
592 tundebug = *(int *)data;
595 *(int *)data = tundebug;
598 mtx_lock(&tp->tun_mtx);
600 tp->tun_flags |= TUN_LMODE;
601 tp->tun_flags &= ~TUN_IFHEAD;
603 tp->tun_flags &= ~TUN_LMODE;
604 mtx_unlock(&tp->tun_mtx);
607 mtx_lock(&tp->tun_mtx);
609 tp->tun_flags |= TUN_IFHEAD;
610 tp->tun_flags &= ~TUN_LMODE;
612 tp->tun_flags &= ~TUN_IFHEAD;
613 mtx_unlock(&tp->tun_mtx);
616 /* Could be unlocked read? */
617 mtx_lock(&tp->tun_mtx);
618 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
619 mtx_unlock(&tp->tun_mtx);
622 /* deny this if UP */
623 if (TUN2IFP(tp)->if_flags & IFF_UP)
626 switch (*(int *)data & ~IFF_MULTICAST) {
627 case IFF_POINTOPOINT:
629 TUN2IFP(tp)->if_flags &=
630 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
631 TUN2IFP(tp)->if_flags |= *(int *)data;
638 mtx_lock(&tp->tun_mtx);
639 tp->tun_pid = curthread->td_proc->p_pid;
640 mtx_unlock(&tp->tun_mtx);
645 mtx_lock(&tp->tun_mtx);
647 tp->tun_flags |= TUN_ASYNC;
649 tp->tun_flags &= ~TUN_ASYNC;
650 mtx_unlock(&tp->tun_mtx);
654 if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
656 IFQ_LOCK(&TUN2IFP(tp)->if_snd);
657 IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
658 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
659 *(int *)data += mb->m_len;
660 IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
666 return (fsetown(*(int *)data, &tp->tun_sigio));
669 *(int *)data = fgetown(&tp->tun_sigio);
672 /* This is deprecated, FIOSETOWN should be used instead. */
674 return (fsetown(-(*(int *)data), &tp->tun_sigio));
676 /* This is deprecated, FIOGETOWN should be used instead. */
678 *(int *)data = -fgetown(&tp->tun_sigio);
688 * The cdevsw read interface - reads a packet at a time, or at
689 * least as much of a packet as can be read.
692 tunread(struct cdev *dev, struct uio *uio, int flag)
694 struct tun_softc *tp = dev->si_drv1;
695 struct ifnet *ifp = TUN2IFP(tp);
699 TUNDEBUG (ifp, "read\n");
700 mtx_lock(&tp->tun_mtx);
701 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
702 mtx_unlock(&tp->tun_mtx);
703 TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
707 tp->tun_flags &= ~TUN_RWAIT;
708 mtx_unlock(&tp->tun_mtx);
712 IFQ_DEQUEUE(&ifp->if_snd, m);
714 if (flag & O_NONBLOCK) {
716 return (EWOULDBLOCK);
718 mtx_lock(&tp->tun_mtx);
719 tp->tun_flags |= TUN_RWAIT;
720 mtx_unlock(&tp->tun_mtx);
721 if((error = tsleep(tp, PCATCH | (PZERO + 1),
722 "tunread", 0)) != 0) {
730 while (m && uio->uio_resid > 0 && error == 0) {
731 len = min(uio->uio_resid, m->m_len);
733 error = uiomove(mtod(m, void *), len, uio);
738 TUNDEBUG(ifp, "Dropping mbuf\n");
745 * the cdevsw write interface - an atomic write is a packet - or else!
748 tunwrite(struct cdev *dev, struct uio *uio, int flag)
750 struct tun_softc *tp = dev->si_drv1;
751 struct ifnet *ifp = TUN2IFP(tp);
757 TUNDEBUG(ifp, "tunwrite\n");
759 if ((ifp->if_flags & IFF_UP) != IFF_UP)
760 /* ignore silently */
763 if (uio->uio_resid == 0)
766 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
767 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
771 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, 0)) == NULL) {
776 m->m_pkthdr.rcvif = ifp;
778 mac_create_mbuf_from_ifnet(ifp, m);
781 /* Could be unlocked read? */
782 mtx_lock(&tp->tun_mtx);
783 if (tp->tun_flags & TUN_IFHEAD) {
784 mtx_unlock(&tp->tun_mtx);
785 if (m->m_len < sizeof(family) &&
786 (m = m_pullup(m, sizeof(family))) == NULL)
788 family = ntohl(*mtod(m, u_int32_t *));
789 m_adj(m, sizeof(family));
791 mtx_unlock(&tp->tun_mtx);
795 BPF_MTAP2(ifp, &family, sizeof(family), m);
820 return (EAFNOSUPPORT);
822 /* First chunk of an mbuf contains good junk */
823 if (harvest.point_to_point)
824 random_harvest(m, 16, 3, 0, RANDOM_NET);
825 ifp->if_ibytes += m->m_pkthdr.len;
827 netisr_dispatch(isr, m);
832 * tunpoll - the poll interface, this is only useful on reads
833 * really. The write detect always returns true, write never blocks
834 * anyway, it either accepts the packet or drops it.
837 tunpoll(struct cdev *dev, int events, struct thread *td)
840 struct tun_softc *tp = dev->si_drv1;
841 struct ifnet *ifp = TUN2IFP(tp);
846 TUNDEBUG(ifp, "tunpoll\n");
848 if (events & (POLLIN | POLLRDNORM)) {
849 IFQ_LOCK(&ifp->if_snd);
850 IFQ_POLL_NOLOCK(&ifp->if_snd, m);
852 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
853 revents |= events & (POLLIN | POLLRDNORM);
855 TUNDEBUG(ifp, "tunpoll waiting\n");
856 selrecord(td, &tp->tun_rsel);
858 IFQ_UNLOCK(&ifp->if_snd);
860 if (events & (POLLOUT | POLLWRNORM))
861 revents |= events & (POLLOUT | POLLWRNORM);