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 $
20 #include "opt_inet6.h"
22 #include <sys/param.h>
25 #include <sys/systm.h>
27 #include <sys/socket.h>
29 #include <sys/device.h>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/ttycom.h>
33 #include <sys/signalvar.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
38 #include <sys/vnode.h>
39 #include <sys/malloc.h>
40 #include <sys/mplock2.h>
41 #include <sys/devfs.h>
42 #include <sys/queue.h>
46 #include <net/if_types.h>
47 #include <net/if_clone.h>
48 #include <net/ifq_var.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
53 #include <netinet/in.h>
56 #include "if_tunvar.h"
60 #define TUNDEBUG if (tundebug) if_printf
63 static int tunmodevent(module_t, int, void *);
66 static struct tun_softc *tuncreate(cdev_t, int);
67 static void tundestroy(struct tun_softc *sc);
70 static int tun_clone_create(struct if_clone *, int, caddr_t);
71 static int tun_clone_destroy(struct ifnet *);
73 /* network interface */
74 static int tunifinit(struct ifnet *);
75 static void tunifstart(struct ifnet *, struct ifaltq_subque *);
76 static int tunifoutput(struct ifnet *, struct mbuf *,
77 struct sockaddr *, struct rtentry *rt);
78 static int tunifioctl(struct ifnet *, u_long,
79 caddr_t, struct ucred *);
81 /* character device */
82 static d_open_t tunopen;
83 static d_close_t tunclose;
84 static d_read_t tunread;
85 static d_write_t tunwrite;
86 static d_ioctl_t tunioctl;
87 static d_kqfilter_t tunkqfilter;
88 static d_clone_t tunclone;
90 static struct dev_ops tun_ops = {
97 .d_kqfilter = tunkqfilter
101 static void tun_filter_detach(struct knote *);
102 static int tun_filter_read(struct knote *, long);
103 static int tun_filter_write(struct knote *, long);
105 static struct filterops tun_read_filtops = {
111 static struct filterops tun_write_filtops = {
118 static int tundebug = 0; /* debug flag */
119 static int tunrefcnt = 0; /* module reference counter */
121 static MALLOC_DEFINE(M_TUN, TUN, "Tunnel Interface");
123 static DEVFS_DEFINE_CLONE_BITMAP(tun);
125 struct if_clone tun_cloner = IF_CLONE_INITIALIZER(
126 TUN, tun_clone_create, tun_clone_destroy, 0, IF_MAXUNIT);
128 static SLIST_HEAD(,tun_softc) tun_listhead =
129 SLIST_HEAD_INITIALIZER(&tun_listhead);
131 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
132 "Enable debug output");
133 SYSCTL_DECL(_net_link);
134 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
135 "IP tunnel software network interface");
136 SYSCTL_INT(_net_link_tun, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0,
137 "Enable debug output");
138 SYSCTL_INT(_net_link_tun, OID_AUTO, refcnt, CTLFLAG_RD, &tunrefcnt, 0,
139 "Number of opened devices");
141 DEV_MODULE(if_tun, tunmodevent, NULL);
144 * tunmodevent - module event handler
147 tunmodevent(module_t mod, int type, void *data)
149 static cdev_t dev = NULL;
150 struct tun_softc *sc, *sc_tmp;
154 dev = make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
155 tunclone, UID_UUCP, GID_DIALER,
158 SLIST_INIT(&tun_listhead);
159 if_clone_attach(&tun_cloner);
166 if_clone_detach(&tun_cloner);
168 SLIST_FOREACH_MUTABLE(sc, &tun_listhead, tun_link, sc_tmp)
171 dev_ops_remove_all(&tun_ops);
172 destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tun));
183 tunclone(struct dev_clone_args *ap)
187 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
188 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
189 0600, "%s%d", TUN, unit);
191 if (tuncreate(ap->a_dev, 0) == NULL)
197 static struct tun_softc *
198 tuncreate(cdev_t dev, int flags)
200 struct tun_softc *sc;
202 int unit = minor(dev);
204 sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
207 sc->tun_flags = TUN_INITED;
208 sc->tun_flags |= flags;
210 reference_dev(dev); /* device association */
212 ifp = sc->tun_ifp = if_alloc(IFT_PPP);
214 kprintf("%s: failed to if_alloc() interface for %s%d",
215 __func__, TUN, unit);
219 if_initname(ifp, TUN, unit);
220 ifp->if_mtu = TUNMTU;
221 ifp->if_ioctl = tunifioctl;
222 ifp->if_output = tunifoutput;
223 ifp->if_start = tunifstart;
224 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
225 ifp->if_type = IFT_PPP;
227 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
228 ifq_set_ready(&ifp->if_snd);
230 if_attach(ifp, NULL);
231 bpfattach(ifp, DLT_NULL, sizeof(u_int));
233 SLIST_INSERT_HEAD(&tun_listhead, sc, tun_link);
234 TUNDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n",
235 unit, sc->tun_flags);
240 tundestroy(struct tun_softc *sc)
242 cdev_t dev = sc->tun_dev;
243 struct ifnet *ifp = sc->tun_ifp;
244 int unit = minor(dev);
246 TUNDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n",
255 release_dev(dev); /* device disassociation */
257 /* Also destroy the cloned device */
259 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), unit);
261 SLIST_REMOVE(&tun_listhead, sc, tun_softc, tun_link);
266 * tunnel open - must be superuser & the device must be configured in
269 tunopen(struct dev_open_args *ap)
271 cdev_t dev = ap->a_head.a_dev;
273 struct tun_softc *sc;
276 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
280 if (sc == NULL && (sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL)
282 if (sc->tun_flags & TUN_OPEN)
286 if ((sc->tun_flags & TUN_CLONE) == 0) {
287 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
289 /* Announce the return of the interface. */
290 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
293 sc->tun_pid = curproc->p_pid;
294 sc->tun_flags |= TUN_OPEN;
297 TUNDEBUG(ifp, "opened, minor = %#x. Module refcnt = %d\n",
298 minor(dev), tunrefcnt);
303 * close the device - mark interface down & delete routing info
306 tunclose(struct dev_close_args *ap)
308 cdev_t dev = ap->a_head.a_dev;
309 struct tun_softc *sc = dev->si_drv1;
311 int unit = minor(dev);
314 ("try closing the already destroyed %s%d", TUN, unit));
317 sc->tun_flags &= ~TUN_OPEN;
320 /* Junk all pending output. */
321 ifq_purge_all(&ifp->if_snd);
323 if (ifp->if_flags & IFF_UP)
325 ifp->if_flags &= ~IFF_RUNNING;
327 if ((sc->tun_flags & TUN_CLONE) == 0) {
328 if_purgeaddrs_nolink(ifp);
330 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
332 /* Announce the departure of the interface. */
333 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
336 funsetown(&sc->tun_sigio);
337 KNOTE(&sc->tun_rkq.ki_note, 0);
342 if_printf(ifp, ". Module refcnt = %d is out of sync! "
343 "Force refcnt to be 0.\n", tunrefcnt);
346 TUNDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n",
349 /* Only auto-destroy if the interface was not manually created. */
350 if ((sc->tun_flags & TUN_MANUALMAKE) == 0) {
360 * Interface clone support
362 * Create and destroy tun device/interface via ifconfig(8).
365 static struct tun_softc *
368 struct tun_softc *sc;
370 SLIST_FOREACH(sc, &tun_listhead, tun_link) {
371 if (minor(sc->tun_dev) == unit)
378 tun_clone_create(struct if_clone *ifc __unused, int unit,
379 caddr_t param __unused)
381 struct tun_softc *sc;
386 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tun), unit)) {
387 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), unit);
388 dev = make_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
389 0600, "%s%d", TUN, unit);
391 dev = devfs_find_device_by_name("%s%d", TUN, unit);
396 if ((sc = tuncreate(dev, TUN_MANUALMAKE)) == NULL)
400 sc->tun_flags |= TUN_CLONE;
401 TUNDEBUG(sc->tun_ifp, "clone created, minor = %#x, flags = 0x%x\n",
402 minor(sc->tun_dev), sc->tun_flags);
408 tun_clone_destroy(struct ifnet * ifp)
410 struct tun_softc *sc = ifp->if_softc;
412 if (sc->tun_flags & TUN_OPEN)
414 if ((sc->tun_flags & TUN_CLONE) == 0)
417 TUNDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n",
418 minor(sc->tun_dev), sc->tun_flags);
426 * Network interface functions
430 tunifinit(struct ifnet *ifp)
433 struct tun_softc *sc = ifp->if_softc;
435 struct ifaddr_container *ifac;
438 TUNDEBUG(ifp, "initialize\n");
440 ifp->if_flags |= IFF_UP | IFF_RUNNING;
441 getmicrotime(&ifp->if_lastchange);
443 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
444 struct ifaddr *ifa = ifac->ifa;
446 if (ifa->ifa_addr == NULL) {
448 /* XXX: Should maybe return straight off? */
451 if (ifa->ifa_addr->sa_family == AF_INET) {
452 struct sockaddr_in *si;
454 si = (struct sockaddr_in *)ifa->ifa_addr;
455 if (si->sin_addr.s_addr)
456 sc->tun_flags |= TUN_IASET;
465 * Process an ioctl request.
470 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
472 struct ifreq *ifr = (struct ifreq *)data;
473 struct tun_softc *sc = ifp->if_softc;
479 ifs = (struct ifstat *)data;
481 ksprintf(ifs->ascii + strlen(ifs->ascii),
482 "\tOpened by PID %d\n", sc->tun_pid);
485 error = tunifinit(ifp);
486 TUNDEBUG(ifp, "address set, error=%d\n", error);
489 error = tunifinit(ifp);
490 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
493 ifp->if_mtu = ifr->ifr_mtu;
494 TUNDEBUG(ifp, "mtu set\n");
507 * Start packet transmission on the interface.
508 * when the interface queue is rate-limited by ALTQ,
509 * if_start is needed to drain packets from the queue in order
510 * to notify readers when outgoing packets become ready.
513 tunifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
515 struct tun_softc *sc = ifp->if_softc;
518 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
520 if (!ifq_is_enabled(&ifp->if_snd))
525 if (sc->tun_flags & TUN_RWAIT) {
526 sc->tun_flags &= ~TUN_RWAIT;
529 if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
530 pgsigio(sc->tun_sigio, SIGIO, 0);
531 ifsq_deserialize_hw(ifsq);
532 KNOTE(&sc->tun_rkq.ki_note, 0);
533 ifsq_serialize_hw(ifsq);
538 * tunifoutput - queue packets from higher level ready to put out.
543 tunifoutput_serialized(struct ifnet *ifp, struct mbuf *m0,
544 struct sockaddr *dst, struct rtentry *rt)
546 struct tun_softc *sc = ifp->if_softc;
548 struct altq_pktattr pktattr;
550 TUNDEBUG(ifp, "output\n");
552 if ((sc->tun_flags & TUN_READY) != TUN_READY) {
553 TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
559 * if the queueing discipline needs packet classification,
560 * do it before prepending link headers.
562 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
564 /* BPF write needs to be handled specially */
565 if (dst->sa_family == AF_UNSPEC) {
566 dst->sa_family = *(mtod(m0, int *));
567 m0->m_len -= sizeof(int);
568 m0->m_pkthdr.len -= sizeof(int);
569 m0->m_data += sizeof(int);
576 * We need to prepend the address family as
579 uint32_t af = dst->sa_family;
581 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
586 /* prepend sockaddr? this may abort if the mbuf allocation fails */
587 if (sc->tun_flags & TUN_LMODE) {
588 /* allocate space for sockaddr */
589 M_PREPEND(m0, dst->sa_len, M_NOWAIT);
591 /* if allocation failed drop packet */
593 IFNET_STAT_INC(ifp, oerrors, 1);
596 bcopy(dst, m0->m_data, dst->sa_len);
600 if (sc->tun_flags & TUN_IFHEAD) {
601 /* Prepend the address family */
602 M_PREPEND(m0, 4, M_NOWAIT);
604 /* if allocation failed drop packet */
606 IFNET_STAT_INC(ifp, oerrors, 1);
609 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
613 if (dst->sa_family != AF_INET)
617 return (EAFNOSUPPORT);
621 error = ifq_handoff(ifp, m0, &pktattr);
623 IFNET_STAT_INC(ifp, collisions, 1);
625 IFNET_STAT_INC(ifp, opackets, 1);
626 if (sc->tun_flags & TUN_RWAIT) {
627 sc->tun_flags &= ~TUN_RWAIT;
631 if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
632 pgsigio(sc->tun_sigio, SIGIO, 0);
634 ifnet_deserialize_all(ifp);
635 KNOTE(&sc->tun_rkq.ki_note, 0);
636 ifnet_serialize_all(ifp);
642 tunifoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
647 ifnet_serialize_all(ifp);
648 error = tunifoutput_serialized(ifp, m0, dst, rt);
649 ifnet_deserialize_all(ifp);
656 * the ops interface is now pretty minimal.
659 tunioctl(struct dev_ioctl_args *ap)
661 cdev_t dev = ap->a_head.a_dev;
662 caddr_t data = ap->a_data;
663 struct tun_softc *sc = dev->si_drv1;
664 struct ifnet *ifp = sc->tun_ifp;
666 struct tuninfo *tunp;
671 tunp = (struct tuninfo *)data;
672 if (ifp->if_type != tunp->type)
674 if (tunp->mtu < IF_MINMTU)
676 ifp->if_mtu = tunp->mtu;
677 ifp->if_baudrate = tunp->baudrate;
681 tunp = (struct tuninfo *)data;
682 tunp->mtu = ifp->if_mtu;
683 tunp->type = ifp->if_type;
684 tunp->baudrate = ifp->if_baudrate;
688 ifr = (struct ifreq *)data;
689 strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
693 tundebug = *(int *)data;
697 *(int *)data = tundebug;
702 sc->tun_flags |= TUN_LMODE;
703 sc->tun_flags &= ~TUN_IFHEAD;
705 sc->tun_flags &= ~TUN_LMODE;
711 sc->tun_flags |= TUN_IFHEAD;
712 sc->tun_flags &= ~TUN_LMODE;
714 sc->tun_flags &= ~TUN_IFHEAD;
719 *(int *)data = (sc->tun_flags & TUN_IFHEAD) ? 1 : 0;
723 /* deny this if UP */
724 if (ifp->if_flags & IFF_UP)
727 switch (*(int *)data & ~IFF_MULTICAST) {
728 case IFF_POINTOPOINT:
730 ifp->if_flags &= ~(IFF_BROADCAST | IFF_POINTOPOINT);
731 ifp->if_flags |= *(int *)data;
739 sc->tun_pid = curproc->p_pid;
744 sc->tun_flags |= TUN_ASYNC;
746 sc->tun_flags &= ~TUN_ASYNC;
750 *(int *)data = ifsq_poll_pktlen(
751 ifq_get_subq_default(&ifp->if_snd));
755 error = fsetown(*(int *)data, &sc->tun_sigio);
759 *(int *)data = fgetown(&sc->tun_sigio);
762 /* This is deprecated, FIOSETOWN should be used instead. */
764 error = fsetown(-(*(int *)data), &sc->tun_sigio);
767 /* This is deprecated, FIOGETOWN should be used instead. */
769 *(int *)data = -fgetown(&sc->tun_sigio);
781 * The ops read interface - reads a packet at a time, or at
782 * least as much of a packet as can be read.
785 tunread(struct dev_read_args *ap)
787 cdev_t dev = ap->a_head.a_dev;
788 struct uio *uio = ap->a_uio;
789 struct tun_softc *sc = dev->si_drv1;
790 struct ifnet *ifp = sc->tun_ifp;
791 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
795 TUNDEBUG(ifp, "read\n");
796 if ((sc->tun_flags & TUN_READY) != TUN_READY) {
797 TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
801 sc->tun_flags &= ~TUN_RWAIT;
803 ifnet_serialize_all(ifp);
805 while ((m0 = ifsq_dequeue(ifsq)) == NULL) {
806 if (ap->a_ioflag & IO_NDELAY) {
807 ifnet_deserialize_all(ifp);
808 return (EWOULDBLOCK);
810 sc->tun_flags |= TUN_RWAIT;
811 ifnet_deserialize_all(ifp);
812 if ((error = tsleep(sc, PCATCH, "tunread", 0)) != 0)
814 ifnet_serialize_all(ifp);
817 ifnet_deserialize_all(ifp);
819 while (m0 && uio->uio_resid > 0 && error == 0) {
820 len = (int)szmin(uio->uio_resid, m0->m_len);
822 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
827 TUNDEBUG(ifp, "dropping mbuf\n");
834 * the ops write interface - an atomic write is a packet - or else!
837 tunwrite(struct dev_write_args *ap)
839 cdev_t dev = ap->a_head.a_dev;
840 struct uio *uio = ap->a_uio;
841 struct tun_softc *sc = dev->si_drv1;
842 struct ifnet *ifp = sc->tun_ifp;
843 struct mbuf *top, **mp, *m;
845 uint32_t family, mru;
849 TUNDEBUG(ifp, "tunwrite\n");
851 if (uio->uio_resid == 0)
855 if (sc->tun_flags & TUN_IFHEAD)
856 mru += sizeof(family);
857 if (uio->uio_resid > mru) {
858 TUNDEBUG(ifp, "len = %zd!\n", uio->uio_resid);
862 /* get a header mbuf */
863 MGETHDR(m, M_WAITOK, MT_DATA);
867 tlen = uio->uio_resid;
870 while (error == 0 && uio->uio_resid > 0) {
871 m->m_len = (int)szmin(MHLEN, uio->uio_resid);
872 error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
875 if (uio->uio_resid > 0) {
876 MGET(m, M_WAITOK, MT_DATA);
886 IFNET_STAT_INC(ifp, ierrors, 1);
890 top->m_pkthdr.len = (int)tlen;
891 top->m_pkthdr.rcvif = ifp;
897 if (sc->tun_flags & TUN_IFHEAD) {
899 * Conveniently, we already have a 4-byte
900 * address family prepended to our packet !
901 * Inconveniently, it's in the wrong byte
904 if ((top = m_pullup(top, sizeof(family)))
909 *mtod(top, u_int32_t *) =
910 ntohl(*mtod(top, u_int32_t *));
911 bpf_mtap(ifp->if_bpf, top);
912 *mtod(top, u_int32_t *) =
913 htonl(*mtod(top, u_int32_t *));
916 * We need to prepend the address family as
919 static const uint32_t af = AF_INET;
921 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
928 if (sc->tun_flags & TUN_IFHEAD) {
929 if (top->m_len < sizeof(family) &&
930 (top = m_pullup(top, sizeof(family))) == NULL)
932 family = ntohl(*mtod(top, u_int32_t *));
933 m_adj(top, sizeof(family));
938 IFNET_STAT_INC(ifp, ibytes, top->m_pkthdr.len);
939 IFNET_STAT_INC(ifp, ipackets, 1);
954 return (EAFNOSUPPORT);
957 netisr_queue(isr, top);
963 * tunkqfilter - support for the kevent() system call.
966 tunkqfilter(struct dev_kqfilter_args *ap)
968 cdev_t dev = ap->a_head.a_dev;
969 struct tun_softc *sc = dev->si_drv1;
970 struct ifnet *ifp = sc->tun_ifp;
971 struct knote *kn = ap->a_kn;
975 ifnet_serialize_all(ifp);
977 switch (kn->kn_filter) {
979 kn->kn_fop = &tun_read_filtops;
980 kn->kn_hook = (caddr_t)sc;
983 kn->kn_fop = &tun_write_filtops;
984 kn->kn_hook = (caddr_t)sc;
987 ifnet_deserialize_all(ifp);
988 ap->a_result = EOPNOTSUPP;
992 klist = &sc->tun_rkq.ki_note;
993 knote_insert(klist, kn);
994 ifnet_deserialize_all(ifp);
1000 tun_filter_detach(struct knote *kn)
1002 struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1003 struct klist *klist = &sc->tun_rkq.ki_note;
1005 knote_remove(klist, kn);
1009 tun_filter_write(struct knote *kn, long hint)
1011 /* Always ready for a write */
1016 tun_filter_read(struct knote *kn, long hint)
1018 struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1019 struct ifnet *ifp = sc->tun_ifp;
1022 ifnet_serialize_all(ifp);
1023 if (!ifsq_is_empty(ifq_get_subq_default(&ifp->if_snd)))
1025 ifnet_deserialize_all(ifp);