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
16 * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
17 * $DragonFly: src/sys/net/tun/if_tun.c,v 1.37 2008/06/05 18:06:32 swildner Exp $
21 #include "opt_atalk.h"
23 #include "opt_inet6.h"
26 #include <sys/param.h>
29 #include <sys/systm.h>
31 #include <sys/socket.h>
33 #include <sys/device.h>
34 #include <sys/filio.h>
35 #include <sys/sockio.h>
36 #include <sys/thread2.h>
37 #include <sys/ttycom.h>
39 #include <sys/signalvar.h>
40 #include <sys/filedesc.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
47 #include <sys/mplock2.h>
50 #include <net/if_types.h>
51 #include <net/ifq_var.h>
52 #include <net/netisr.h>
53 #include <net/route.h>
54 #include <sys/devfs.h>
57 #include <netinet/in.h>
62 #include "if_tunvar.h"
65 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
67 static void tunattach (void *);
68 PSEUDO_SET(tunattach, if_tun);
70 static void tuncreate (cdev_t dev);
72 #define TUNDEBUG if (tundebug) if_printf
73 static int tundebug = 0;
74 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
76 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
78 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
79 static int tuninit (struct ifnet *);
80 static void tunstart(struct ifnet *);
81 static void tun_filter_detach(struct knote *);
82 static int tun_filter_read(struct knote *, long);
84 static d_open_t tunopen;
85 static d_close_t tunclose;
86 static d_read_t tunread;
87 static d_write_t tunwrite;
88 static d_ioctl_t tunioctl;
89 static d_poll_t tunpoll;
90 static d_kqfilter_t tunkqfilter;
92 static d_clone_t tunclone;
93 DEVFS_DECLARE_CLONE_BITMAP(tun);
96 #define TUN_PREALLOCATED_UNITS 4
98 #define TUN_PREALLOCATED_UNITS NTUN
101 #define CDEV_MAJOR 52
102 static struct dev_ops tun_ops = {
103 { "tun", CDEV_MAJOR, D_KQFILTER },
110 .d_kqfilter = tunkqfilter
114 tunattach(void *dummy)
117 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
118 tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
119 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
120 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
121 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
123 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
127 tunclone(struct dev_clone_args *ap)
131 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
132 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
139 tuncreate(cdev_t dev)
141 struct tun_softc *sc;
145 dev = make_dev(&tun_ops, minor(dev),
146 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
149 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
150 sc->tun_flags = TUN_INITED;
153 if_initname(ifp, "tun", lminor(dev));
154 ifp->if_mtu = TUNMTU;
155 ifp->if_ioctl = tunifioctl;
156 ifp->if_output = tunoutput;
157 ifp->if_start = tunstart;
158 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
159 ifp->if_type = IFT_PPP;
160 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
161 ifq_set_ready(&ifp->if_snd);
163 if_attach(ifp, NULL);
164 bpfattach(ifp, DLT_NULL, sizeof(u_int));
169 * tunnel open - must be superuser & the device must be
173 tunopen(struct dev_open_args *ap)
175 cdev_t dev = ap->a_head.a_dev;
177 struct tun_softc *tp;
180 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
188 if (tp->tun_flags & TUN_OPEN)
190 tp->tun_pid = curproc->p_pid;
192 tp->tun_flags |= TUN_OPEN;
193 TUNDEBUG(ifp, "open\n");
198 * tunclose - close the device - mark i/f down & delete
202 tunclose(struct dev_close_args *ap)
204 cdev_t dev = ap->a_head.a_dev;
205 struct tun_softc *tp;
211 tp->tun_flags &= ~TUN_OPEN;
214 /* Junk all pending output. */
215 ifq_purge(&ifp->if_snd);
217 if (ifp->if_flags & IFF_UP)
219 ifp->if_flags &= ~IFF_RUNNING;
220 if_purgeaddrs_nolink(ifp);
222 funsetown(tp->tun_sigio);
223 selwakeup(&tp->tun_rsel);
225 TUNDEBUG(ifp, "closed\n");
227 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
228 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
235 tuninit(struct ifnet *ifp)
237 struct tun_softc *tp = ifp->if_softc;
238 struct ifaddr_container *ifac;
241 TUNDEBUG(ifp, "tuninit\n");
243 ifp->if_flags |= IFF_UP | IFF_RUNNING;
244 getmicrotime(&ifp->if_lastchange);
246 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
247 struct ifaddr *ifa = ifac->ifa;
249 if (ifa->ifa_addr == NULL) {
251 /* XXX: Should maybe return straight off? */
254 if (ifa->ifa_addr->sa_family == AF_INET) {
255 struct sockaddr_in *si;
257 si = (struct sockaddr_in *)ifa->ifa_addr;
258 if (si->sin_addr.s_addr)
259 tp->tun_flags |= TUN_IASET;
268 * Process an ioctl request.
273 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
275 struct ifreq *ifr = (struct ifreq *)data;
276 struct tun_softc *tp = ifp->if_softc;
282 ifs = (struct ifstat *)data;
284 ksprintf(ifs->ascii + strlen(ifs->ascii),
285 "\tOpened by PID %d\n", tp->tun_pid);
288 error = tuninit(ifp);
289 TUNDEBUG(ifp, "address set, error=%d\n", error);
292 error = tuninit(ifp);
293 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
296 ifp->if_mtu = ifr->ifr_mtu;
297 TUNDEBUG(ifp, "mtu set\n");
310 * tunoutput - queue packets from higher level ready to put out.
315 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
318 struct tun_softc *tp = ifp->if_softc;
320 struct altq_pktattr pktattr;
322 TUNDEBUG(ifp, "tunoutput\n");
324 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
325 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
331 * if the queueing discipline needs packet classification,
332 * do it before prepending link headers.
334 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
336 /* BPF write needs to be handled specially */
337 if (dst->sa_family == AF_UNSPEC) {
338 dst->sa_family = *(mtod(m0, int *));
339 m0->m_len -= sizeof(int);
340 m0->m_pkthdr.len -= sizeof(int);
341 m0->m_data += sizeof(int);
346 * We need to prepend the address family as
349 uint32_t af = dst->sa_family;
351 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
354 /* prepend sockaddr? this may abort if the mbuf allocation fails */
355 if (tp->tun_flags & TUN_LMODE) {
356 /* allocate space for sockaddr */
357 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
359 /* if allocation failed drop packet */
361 IF_DROP(&ifp->if_snd);
365 bcopy(dst, m0->m_data, dst->sa_len);
369 if (tp->tun_flags & TUN_IFHEAD) {
370 /* Prepend the address family */
371 M_PREPEND(m0, 4, MB_DONTWAIT);
373 /* if allocation failed drop packet */
375 IF_DROP(&ifp->if_snd);
379 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
382 if (dst->sa_family != AF_INET)
390 error = ifq_handoff(ifp, m0, &pktattr);
392 ifp->if_collisions++;
395 if (tp->tun_flags & TUN_RWAIT) {
396 tp->tun_flags &= ~TUN_RWAIT;
400 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
401 pgsigio(tp->tun_sigio, SIGIO, 0);
402 selwakeup(&tp->tun_rsel);
409 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
414 ifnet_serialize_all(ifp);
415 error = tunoutput_serialized(ifp, m0, dst, rt);
416 ifnet_deserialize_all(ifp);
422 * the ops interface is now pretty minimal.
425 tunioctl(struct dev_ioctl_args *ap)
427 cdev_t dev = ap->a_head.a_dev;
428 struct tun_softc *tp = dev->si_drv1;
429 struct tuninfo *tunp;
433 tunp = (struct tuninfo *)ap->a_data;
434 if (tunp->mtu < IF_MINMTU)
436 tp->tun_if.if_mtu = tunp->mtu;
437 tp->tun_if.if_type = tunp->type;
438 tp->tun_if.if_baudrate = tunp->baudrate;
441 tunp = (struct tuninfo *)ap->a_data;
442 tunp->mtu = tp->tun_if.if_mtu;
443 tunp->type = tp->tun_if.if_type;
444 tunp->baudrate = tp->tun_if.if_baudrate;
447 tundebug = *(int *)ap->a_data;
450 *(int *)ap->a_data = tundebug;
453 if (*(int *)ap->a_data) {
454 tp->tun_flags |= TUN_LMODE;
455 tp->tun_flags &= ~TUN_IFHEAD;
457 tp->tun_flags &= ~TUN_LMODE;
460 if (*(int *)ap->a_data) {
461 tp->tun_flags |= TUN_IFHEAD;
462 tp->tun_flags &= ~TUN_LMODE;
464 tp->tun_flags &= ~TUN_IFHEAD;
467 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
470 /* deny this if UP */
471 if (tp->tun_if.if_flags & IFF_UP)
474 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
475 case IFF_POINTOPOINT:
477 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
478 tp->tun_if.if_flags |= *(int *)ap->a_data;
485 tp->tun_pid = curproc->p_pid;
488 if (*(int *)ap->a_data)
489 tp->tun_flags |= TUN_ASYNC;
491 tp->tun_flags &= ~TUN_ASYNC;
494 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
497 mb = ifq_poll(&tp->tun_if.if_snd);
498 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
499 *(int *)ap->a_data += mb->m_len;
501 *(int *)ap->a_data = 0;
505 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
508 *(int *)ap->a_data = fgetown(tp->tun_sigio);
511 /* This is deprecated, FIOSETOWN should be used instead. */
513 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
515 /* This is deprecated, FIOGETOWN should be used instead. */
517 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
527 * The ops read interface - reads a packet at a time, or at
528 * least as much of a packet as can be read.
531 tunread(struct dev_read_args *ap)
533 cdev_t dev = ap->a_head.a_dev;
534 struct uio *uio = ap->a_uio;
535 struct tun_softc *tp = dev->si_drv1;
536 struct ifnet *ifp = &tp->tun_if;
540 TUNDEBUG(ifp, "read\n");
541 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
542 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
546 tp->tun_flags &= ~TUN_RWAIT;
548 ifnet_serialize_all(ifp);
550 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
551 if (ap->a_ioflag & IO_NDELAY) {
552 ifnet_deserialize_all(ifp);
555 tp->tun_flags |= TUN_RWAIT;
556 ifnet_deserialize_all(ifp);
557 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
559 ifnet_serialize_all(ifp);
562 ifnet_deserialize_all(ifp);
564 while (m0 && uio->uio_resid > 0 && error == 0) {
565 len = (int)szmin(uio->uio_resid, m0->m_len);
567 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
572 TUNDEBUG(ifp, "Dropping mbuf\n");
579 * the ops write interface - an atomic write is a packet - or else!
582 tunwrite(struct dev_write_args *ap)
584 cdev_t dev = ap->a_head.a_dev;
585 struct uio *uio = ap->a_uio;
586 struct tun_softc *tp = dev->si_drv1;
587 struct ifnet *ifp = &tp->tun_if;
588 struct mbuf *top, **mp, *m;
594 TUNDEBUG(ifp, "tunwrite\n");
596 if (uio->uio_resid == 0)
599 if (uio->uio_resid > TUNMRU) {
600 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
603 tlen = uio->uio_resid;
605 /* get a header mbuf */
606 MGETHDR(m, MB_DONTWAIT, MT_DATA);
613 while (error == 0 && uio->uio_resid > 0) {
614 m->m_len = (int)szmin(mlen, uio->uio_resid);
615 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
618 if (uio->uio_resid > 0) {
619 MGET (m, MB_DONTWAIT, MT_DATA);
634 top->m_pkthdr.len = (int)tlen;
635 top->m_pkthdr.rcvif = ifp;
638 if (tp->tun_flags & TUN_IFHEAD) {
640 * Conveniently, we already have a 4-byte address
641 * family prepended to our packet !
642 * Inconveniently, it's in the wrong byte order !
644 if ((top = m_pullup(top, sizeof(family))) == NULL)
646 *mtod(top, u_int32_t *) =
647 ntohl(*mtod(top, u_int32_t *));
648 bpf_mtap(ifp->if_bpf, top);
649 *mtod(top, u_int32_t *) =
650 htonl(*mtod(top, u_int32_t *));
653 * We need to prepend the address family as
656 static const uint32_t af = AF_INET;
658 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
662 if (tp->tun_flags & TUN_IFHEAD) {
663 if (top->m_len < sizeof(family) &&
664 (top = m_pullup(top, sizeof(family))) == NULL)
666 family = ntohl(*mtod(top, u_int32_t *));
667 m_adj(top, sizeof(family));
671 ifp->if_ibytes += top->m_pkthdr.len;
697 return (EAFNOSUPPORT);
700 netisr_dispatch(isr, top);
705 * tunpoll - the poll interface, this is only useful on reads
706 * really. The write detect always returns true, write never blocks
707 * anyway, it either accepts the packet or drops it.
710 tunpoll(struct dev_poll_args *ap)
712 cdev_t dev = ap->a_head.a_dev;
713 struct tun_softc *tp = dev->si_drv1;
714 struct ifnet *ifp = &tp->tun_if;
717 TUNDEBUG(ifp, "tunpoll\n");
719 ifnet_serialize_all(ifp);
721 if (ap->a_events & (POLLIN | POLLRDNORM)) {
722 if (!ifq_is_empty(&ifp->if_snd)) {
723 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
724 revents |= ap->a_events & (POLLIN | POLLRDNORM);
726 TUNDEBUG(ifp, "tunpoll waiting\n");
727 selrecord(curthread, &tp->tun_rsel);
730 if (ap->a_events & (POLLOUT | POLLWRNORM))
731 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
733 ifnet_deserialize_all(ifp);
734 ap->a_events = revents;
738 static struct filterops tun_read_filtops =
739 { 1, NULL, tun_filter_detach, tun_filter_read };
742 tunkqfilter(struct dev_kqfilter_args *ap)
744 cdev_t dev = ap->a_head.a_dev;
745 struct tun_softc *tp = dev->si_drv1;
746 struct knote *kn = ap->a_kn;
750 ifnet_serialize_all(&tp->tun_if);
752 switch (kn->kn_filter) {
754 kn->kn_fop = &tun_read_filtops;
755 kn->kn_hook = (caddr_t)tp;
759 ifnet_deserialize_all(&tp->tun_if);
763 klist = &tp->tun_rsel.si_note;
765 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
768 ifnet_deserialize_all(&tp->tun_if);
774 tun_filter_detach(struct knote *kn)
776 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
779 klist = &tp->tun_rsel.si_note;
781 SLIST_REMOVE(klist, kn, knote, kn_selnext);
786 tun_filter_read(struct knote *kn, long hint)
788 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
791 ifnet_serialize_all(&tp->tun_if);
792 if (!ifq_is_empty(&tp->tun_if.if_snd))
794 ifnet_deserialize_all(&tp->tun_if);
800 * Start packet transmission on the interface.
801 * when the interface queue is rate-limited by ALTQ,
802 * if_start is needed to drain packets from the queue in order
803 * to notify readers when outgoing packets become ready.
806 tunstart(struct ifnet *ifp)
808 struct tun_softc *tp = ifp->if_softc;
811 if (!ifq_is_enabled(&ifp->if_snd))
814 m = ifq_poll(&ifp->if_snd);
816 if (tp->tun_flags & TUN_RWAIT) {
817 tp->tun_flags &= ~TUN_RWAIT;
820 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
821 pgsigio(tp->tun_sigio, SIGIO, 0);
822 selwakeup(&tp->tun_rsel);