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 $
21 #include "opt_inet6.h"
24 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/socket.h>
31 #include <sys/device.h>
32 #include <sys/filio.h>
33 #include <sys/sockio.h>
34 #include <sys/thread2.h>
35 #include <sys/ttycom.h>
36 #include <sys/signalvar.h>
37 #include <sys/filedesc.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
44 #include <sys/mplock2.h>
47 #include <net/if_types.h>
48 #include <net/ifq_var.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 #include <sys/devfs.h>
54 #include <netinet/in.h>
59 #include "if_tunvar.h"
62 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
64 static void tunattach (void *);
65 PSEUDO_SET(tunattach, if_tun);
67 static void tuncreate (cdev_t dev);
69 #define TUNDEBUG if (tundebug) if_printf
70 static int tundebug = 0;
71 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
72 "Enable debug output");
74 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
76 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
77 static int tuninit (struct ifnet *);
78 static void tunstart(struct ifnet *);
79 static void tun_filter_detach(struct knote *);
80 static int tun_filter_read(struct knote *, long);
81 static int tun_filter_write(struct knote *, long);
83 static d_open_t tunopen;
84 static d_close_t tunclose;
85 static d_read_t tunread;
86 static d_write_t tunwrite;
87 static d_ioctl_t tunioctl;
88 static d_kqfilter_t tunkqfilter;
90 static d_clone_t tunclone;
91 DEVFS_DECLARE_CLONE_BITMAP(tun);
94 #define TUN_PREALLOCATED_UNITS 4
96 #define TUN_PREALLOCATED_UNITS NTUN
99 static struct dev_ops tun_ops = {
106 .d_kqfilter = tunkqfilter
110 tunattach(void *dummy)
113 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
114 tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
115 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
116 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
117 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
119 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
123 tunclone(struct dev_clone_args *ap)
127 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
128 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
135 tuncreate(cdev_t dev)
137 struct tun_softc *sc;
141 dev = make_dev(&tun_ops, minor(dev),
142 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
145 sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
146 sc->tun_flags = TUN_INITED;
149 if_initname(ifp, "tun", lminor(dev));
150 ifp->if_mtu = TUNMTU;
151 ifp->if_ioctl = tunifioctl;
152 ifp->if_output = tunoutput;
153 ifp->if_start = tunstart;
154 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
155 ifp->if_type = IFT_PPP;
156 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
157 ifq_set_ready(&ifp->if_snd);
159 if_attach(ifp, NULL);
160 bpfattach(ifp, DLT_NULL, sizeof(u_int));
165 * tunnel open - must be superuser & the device must be
169 tunopen(struct dev_open_args *ap)
171 cdev_t dev = ap->a_head.a_dev;
173 struct tun_softc *tp;
176 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
184 if (tp->tun_flags & TUN_OPEN)
186 tp->tun_pid = curproc->p_pid;
188 tp->tun_flags |= TUN_OPEN;
189 TUNDEBUG(ifp, "open\n");
194 * tunclose - close the device - mark i/f down & delete
198 tunclose(struct dev_close_args *ap)
200 cdev_t dev = ap->a_head.a_dev;
201 struct tun_softc *tp;
207 tp->tun_flags &= ~TUN_OPEN;
210 /* Junk all pending output. */
211 ifq_purge(&ifp->if_snd);
213 if (ifp->if_flags & IFF_UP)
215 ifp->if_flags &= ~IFF_RUNNING;
216 if_purgeaddrs_nolink(ifp);
218 funsetown(&tp->tun_sigio);
219 KNOTE(&tp->tun_rkq.ki_note, 0);
221 TUNDEBUG(ifp, "closed\n");
223 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
224 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
231 tuninit(struct ifnet *ifp)
233 struct tun_softc *tp = ifp->if_softc;
234 struct ifaddr_container *ifac;
237 TUNDEBUG(ifp, "tuninit\n");
239 ifp->if_flags |= IFF_UP | IFF_RUNNING;
240 getmicrotime(&ifp->if_lastchange);
242 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
243 struct ifaddr *ifa = ifac->ifa;
245 if (ifa->ifa_addr == NULL) {
247 /* XXX: Should maybe return straight off? */
250 if (ifa->ifa_addr->sa_family == AF_INET) {
251 struct sockaddr_in *si;
253 si = (struct sockaddr_in *)ifa->ifa_addr;
254 if (si->sin_addr.s_addr)
255 tp->tun_flags |= TUN_IASET;
264 * Process an ioctl request.
269 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
271 struct ifreq *ifr = (struct ifreq *)data;
272 struct tun_softc *tp = ifp->if_softc;
278 ifs = (struct ifstat *)data;
280 ksprintf(ifs->ascii + strlen(ifs->ascii),
281 "\tOpened by PID %d\n", tp->tun_pid);
284 error = tuninit(ifp);
285 TUNDEBUG(ifp, "address set, error=%d\n", error);
288 error = tuninit(ifp);
289 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
292 ifp->if_mtu = ifr->ifr_mtu;
293 TUNDEBUG(ifp, "mtu set\n");
306 * tunoutput - queue packets from higher level ready to put out.
311 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
314 struct tun_softc *tp = ifp->if_softc;
316 struct altq_pktattr pktattr;
318 TUNDEBUG(ifp, "tunoutput\n");
320 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
321 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
327 * if the queueing discipline needs packet classification,
328 * do it before prepending link headers.
330 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
332 /* BPF write needs to be handled specially */
333 if (dst->sa_family == AF_UNSPEC) {
334 dst->sa_family = *(mtod(m0, int *));
335 m0->m_len -= sizeof(int);
336 m0->m_pkthdr.len -= sizeof(int);
337 m0->m_data += sizeof(int);
342 * We need to prepend the address family as
345 uint32_t af = dst->sa_family;
347 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
350 /* prepend sockaddr? this may abort if the mbuf allocation fails */
351 if (tp->tun_flags & TUN_LMODE) {
352 /* allocate space for sockaddr */
353 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
355 /* if allocation failed drop packet */
357 IF_DROP(&ifp->if_snd);
361 bcopy(dst, m0->m_data, dst->sa_len);
365 if (tp->tun_flags & TUN_IFHEAD) {
366 /* Prepend the address family */
367 M_PREPEND(m0, 4, MB_DONTWAIT);
369 /* if allocation failed drop packet */
371 IF_DROP(&ifp->if_snd);
375 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
378 if (dst->sa_family != AF_INET)
386 error = ifq_handoff(ifp, m0, &pktattr);
388 ifp->if_collisions++;
391 if (tp->tun_flags & TUN_RWAIT) {
392 tp->tun_flags &= ~TUN_RWAIT;
396 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
397 pgsigio(tp->tun_sigio, SIGIO, 0);
399 ifnet_deserialize_all(ifp);
400 KNOTE(&tp->tun_rkq.ki_note, 0);
401 ifnet_serialize_all(ifp);
407 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
412 ifnet_serialize_all(ifp);
413 error = tunoutput_serialized(ifp, m0, dst, rt);
414 ifnet_deserialize_all(ifp);
420 * the ops interface is now pretty minimal.
423 tunioctl(struct dev_ioctl_args *ap)
425 cdev_t dev = ap->a_head.a_dev;
426 struct tun_softc *tp = dev->si_drv1;
427 struct tuninfo *tunp;
431 tunp = (struct tuninfo *)ap->a_data;
432 if (tunp->mtu < IF_MINMTU)
434 tp->tun_if.if_mtu = tunp->mtu;
435 tp->tun_if.if_type = tunp->type;
436 tp->tun_if.if_baudrate = tunp->baudrate;
439 tunp = (struct tuninfo *)ap->a_data;
440 tunp->mtu = tp->tun_if.if_mtu;
441 tunp->type = tp->tun_if.if_type;
442 tunp->baudrate = tp->tun_if.if_baudrate;
445 tundebug = *(int *)ap->a_data;
448 *(int *)ap->a_data = tundebug;
451 if (*(int *)ap->a_data) {
452 tp->tun_flags |= TUN_LMODE;
453 tp->tun_flags &= ~TUN_IFHEAD;
455 tp->tun_flags &= ~TUN_LMODE;
458 if (*(int *)ap->a_data) {
459 tp->tun_flags |= TUN_IFHEAD;
460 tp->tun_flags &= ~TUN_LMODE;
462 tp->tun_flags &= ~TUN_IFHEAD;
465 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
468 /* deny this if UP */
469 if (tp->tun_if.if_flags & IFF_UP)
472 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
473 case IFF_POINTOPOINT:
475 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
476 tp->tun_if.if_flags |= *(int *)ap->a_data;
483 tp->tun_pid = curproc->p_pid;
486 if (*(int *)ap->a_data)
487 tp->tun_flags |= TUN_ASYNC;
489 tp->tun_flags &= ~TUN_ASYNC;
492 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
495 mb = ifq_poll(&tp->tun_if.if_snd);
496 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
497 *(int *)ap->a_data += mb->m_len;
499 *(int *)ap->a_data = 0;
503 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
506 *(int *)ap->a_data = fgetown(&tp->tun_sigio);
509 /* This is deprecated, FIOSETOWN should be used instead. */
511 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
513 /* This is deprecated, FIOGETOWN should be used instead. */
515 *(int *)ap->a_data = -fgetown(&tp->tun_sigio);
525 * The ops read interface - reads a packet at a time, or at
526 * least as much of a packet as can be read.
529 tunread(struct dev_read_args *ap)
531 cdev_t dev = ap->a_head.a_dev;
532 struct uio *uio = ap->a_uio;
533 struct tun_softc *tp = dev->si_drv1;
534 struct ifnet *ifp = &tp->tun_if;
538 TUNDEBUG(ifp, "read\n");
539 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
540 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
544 tp->tun_flags &= ~TUN_RWAIT;
546 ifnet_serialize_all(ifp);
548 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
549 if (ap->a_ioflag & IO_NDELAY) {
550 ifnet_deserialize_all(ifp);
553 tp->tun_flags |= TUN_RWAIT;
554 ifnet_deserialize_all(ifp);
555 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
557 ifnet_serialize_all(ifp);
560 ifnet_deserialize_all(ifp);
562 while (m0 && uio->uio_resid > 0 && error == 0) {
563 len = (int)szmin(uio->uio_resid, m0->m_len);
565 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
570 TUNDEBUG(ifp, "Dropping mbuf\n");
577 * the ops write interface - an atomic write is a packet - or else!
580 tunwrite(struct dev_write_args *ap)
582 cdev_t dev = ap->a_head.a_dev;
583 struct uio *uio = ap->a_uio;
584 struct tun_softc *tp = dev->si_drv1;
585 struct ifnet *ifp = &tp->tun_if;
586 struct mbuf *top, **mp, *m;
592 TUNDEBUG(ifp, "tunwrite\n");
594 if (uio->uio_resid == 0)
597 if (uio->uio_resid > TUNMRU) {
598 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
601 tlen = uio->uio_resid;
603 /* get a header mbuf */
604 MGETHDR(m, MB_WAIT, MT_DATA);
611 while (error == 0 && uio->uio_resid > 0) {
612 m->m_len = (int)szmin(mlen, uio->uio_resid);
613 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
616 if (uio->uio_resid > 0) {
617 MGET (m, MB_WAIT, MT_DATA);
632 top->m_pkthdr.len = (int)tlen;
633 top->m_pkthdr.rcvif = ifp;
636 if (tp->tun_flags & TUN_IFHEAD) {
638 * Conveniently, we already have a 4-byte address
639 * family prepended to our packet !
640 * Inconveniently, it's in the wrong byte order !
642 if ((top = m_pullup(top, sizeof(family))) == NULL)
644 *mtod(top, u_int32_t *) =
645 ntohl(*mtod(top, u_int32_t *));
646 bpf_mtap(ifp->if_bpf, top);
647 *mtod(top, u_int32_t *) =
648 htonl(*mtod(top, u_int32_t *));
651 * We need to prepend the address family as
654 static const uint32_t af = AF_INET;
656 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
660 if (tp->tun_flags & TUN_IFHEAD) {
661 if (top->m_len < sizeof(family) &&
662 (top = m_pullup(top, sizeof(family))) == NULL)
664 family = ntohl(*mtod(top, u_int32_t *));
665 m_adj(top, sizeof(family));
669 ifp->if_ibytes += top->m_pkthdr.len;
690 return (EAFNOSUPPORT);
693 netisr_queue(isr, top);
697 static struct filterops tun_read_filtops =
698 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_read };
699 static struct filterops tun_write_filtops =
700 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_write };
703 tunkqfilter(struct dev_kqfilter_args *ap)
705 cdev_t dev = ap->a_head.a_dev;
706 struct tun_softc *tp = dev->si_drv1;
707 struct knote *kn = ap->a_kn;
711 ifnet_serialize_all(&tp->tun_if);
713 switch (kn->kn_filter) {
715 kn->kn_fop = &tun_read_filtops;
716 kn->kn_hook = (caddr_t)tp;
719 kn->kn_fop = &tun_write_filtops;
720 kn->kn_hook = (caddr_t)tp;
723 ifnet_deserialize_all(&tp->tun_if);
724 ap->a_result = EOPNOTSUPP;
728 klist = &tp->tun_rkq.ki_note;
729 knote_insert(klist, kn);
730 ifnet_deserialize_all(&tp->tun_if);
736 tun_filter_detach(struct knote *kn)
738 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
739 struct klist *klist = &tp->tun_rkq.ki_note;
741 knote_remove(klist, kn);
745 tun_filter_write(struct knote *kn, long hint)
747 /* Always ready for a write */
752 tun_filter_read(struct knote *kn, long hint)
754 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
757 ifnet_serialize_all(&tp->tun_if);
758 if (!ifq_is_empty(&tp->tun_if.if_snd))
760 ifnet_deserialize_all(&tp->tun_if);
766 * Start packet transmission on the interface.
767 * when the interface queue is rate-limited by ALTQ,
768 * if_start is needed to drain packets from the queue in order
769 * to notify readers when outgoing packets become ready.
772 tunstart(struct ifnet *ifp)
774 struct tun_softc *tp = ifp->if_softc;
777 if (!ifq_is_enabled(&ifp->if_snd))
780 m = ifq_poll(&ifp->if_snd);
782 if (tp->tun_flags & TUN_RWAIT) {
783 tp->tun_flags &= ~TUN_RWAIT;
786 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
787 pgsigio(tp->tun_sigio, SIGIO, 0);
788 ifnet_deserialize_tx(ifp);
789 KNOTE(&tp->tun_rkq.ki_note, 0);
790 ifnet_serialize_tx(ifp);