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_atalk.h"
22 #include "opt_inet6.h"
25 #include <sys/param.h>
28 #include <sys/systm.h>
30 #include <sys/socket.h>
32 #include <sys/device.h>
33 #include <sys/filio.h>
34 #include <sys/sockio.h>
35 #include <sys/thread2.h>
36 #include <sys/ttycom.h>
37 #include <sys/signalvar.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
45 #include <sys/mplock2.h>
48 #include <net/if_types.h>
49 #include <net/ifq_var.h>
50 #include <net/netisr.h>
51 #include <net/route.h>
52 #include <sys/devfs.h>
55 #include <netinet/in.h>
60 #include "if_tunvar.h"
63 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
65 static void tunattach (void *);
66 PSEUDO_SET(tunattach, if_tun);
68 static void tuncreate (cdev_t dev);
70 #define TUNDEBUG if (tundebug) if_printf
71 static int tundebug = 0;
72 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
73 "Enable debug output");
75 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
77 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
78 static int tuninit (struct ifnet *);
79 static void tunstart(struct ifnet *);
80 static void tun_filter_detach(struct knote *);
81 static int tun_filter_read(struct knote *, long);
82 static int tun_filter_write(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_kqfilter_t tunkqfilter;
91 static d_clone_t tunclone;
92 DEVFS_DECLARE_CLONE_BITMAP(tun);
95 #define TUN_PREALLOCATED_UNITS 4
97 #define TUN_PREALLOCATED_UNITS NTUN
100 static struct dev_ops tun_ops = {
107 .d_kqfilter = tunkqfilter
111 tunattach(void *dummy)
114 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
115 tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
116 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
117 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
118 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
120 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
124 tunclone(struct dev_clone_args *ap)
128 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
129 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
136 tuncreate(cdev_t dev)
138 struct tun_softc *sc;
142 dev = make_dev(&tun_ops, minor(dev),
143 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
146 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
147 sc->tun_flags = TUN_INITED;
150 if_initname(ifp, "tun", lminor(dev));
151 ifp->if_mtu = TUNMTU;
152 ifp->if_ioctl = tunifioctl;
153 ifp->if_output = tunoutput;
154 ifp->if_start = tunstart;
155 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
156 ifp->if_type = IFT_PPP;
157 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
158 ifq_set_ready(&ifp->if_snd);
160 if_attach(ifp, NULL);
161 bpfattach(ifp, DLT_NULL, sizeof(u_int));
166 * tunnel open - must be superuser & the device must be
170 tunopen(struct dev_open_args *ap)
172 cdev_t dev = ap->a_head.a_dev;
174 struct tun_softc *tp;
177 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
185 if (tp->tun_flags & TUN_OPEN)
187 tp->tun_pid = curproc->p_pid;
189 tp->tun_flags |= TUN_OPEN;
190 TUNDEBUG(ifp, "open\n");
195 * tunclose - close the device - mark i/f down & delete
199 tunclose(struct dev_close_args *ap)
201 cdev_t dev = ap->a_head.a_dev;
202 struct tun_softc *tp;
208 tp->tun_flags &= ~TUN_OPEN;
211 /* Junk all pending output. */
212 ifq_purge(&ifp->if_snd);
214 if (ifp->if_flags & IFF_UP)
216 ifp->if_flags &= ~IFF_RUNNING;
217 if_purgeaddrs_nolink(ifp);
219 funsetown(tp->tun_sigio);
220 KNOTE(&tp->tun_rkq.ki_note, 0);
222 TUNDEBUG(ifp, "closed\n");
224 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
225 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
232 tuninit(struct ifnet *ifp)
234 struct tun_softc *tp = ifp->if_softc;
235 struct ifaddr_container *ifac;
238 TUNDEBUG(ifp, "tuninit\n");
240 ifp->if_flags |= IFF_UP | IFF_RUNNING;
241 getmicrotime(&ifp->if_lastchange);
243 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
244 struct ifaddr *ifa = ifac->ifa;
246 if (ifa->ifa_addr == NULL) {
248 /* XXX: Should maybe return straight off? */
251 if (ifa->ifa_addr->sa_family == AF_INET) {
252 struct sockaddr_in *si;
254 si = (struct sockaddr_in *)ifa->ifa_addr;
255 if (si->sin_addr.s_addr)
256 tp->tun_flags |= TUN_IASET;
265 * Process an ioctl request.
270 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
272 struct ifreq *ifr = (struct ifreq *)data;
273 struct tun_softc *tp = ifp->if_softc;
279 ifs = (struct ifstat *)data;
281 ksprintf(ifs->ascii + strlen(ifs->ascii),
282 "\tOpened by PID %d\n", tp->tun_pid);
285 error = tuninit(ifp);
286 TUNDEBUG(ifp, "address set, error=%d\n", error);
289 error = tuninit(ifp);
290 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
293 ifp->if_mtu = ifr->ifr_mtu;
294 TUNDEBUG(ifp, "mtu set\n");
307 * tunoutput - queue packets from higher level ready to put out.
312 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
315 struct tun_softc *tp = ifp->if_softc;
317 struct altq_pktattr pktattr;
319 TUNDEBUG(ifp, "tunoutput\n");
321 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
322 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
328 * if the queueing discipline needs packet classification,
329 * do it before prepending link headers.
331 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
333 /* BPF write needs to be handled specially */
334 if (dst->sa_family == AF_UNSPEC) {
335 dst->sa_family = *(mtod(m0, int *));
336 m0->m_len -= sizeof(int);
337 m0->m_pkthdr.len -= sizeof(int);
338 m0->m_data += sizeof(int);
343 * We need to prepend the address family as
346 uint32_t af = dst->sa_family;
348 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
351 /* prepend sockaddr? this may abort if the mbuf allocation fails */
352 if (tp->tun_flags & TUN_LMODE) {
353 /* allocate space for sockaddr */
354 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
356 /* if allocation failed drop packet */
358 IF_DROP(&ifp->if_snd);
362 bcopy(dst, m0->m_data, dst->sa_len);
366 if (tp->tun_flags & TUN_IFHEAD) {
367 /* Prepend the address family */
368 M_PREPEND(m0, 4, MB_DONTWAIT);
370 /* if allocation failed drop packet */
372 IF_DROP(&ifp->if_snd);
376 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
379 if (dst->sa_family != AF_INET)
387 error = ifq_handoff(ifp, m0, &pktattr);
389 ifp->if_collisions++;
392 if (tp->tun_flags & TUN_RWAIT) {
393 tp->tun_flags &= ~TUN_RWAIT;
397 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
398 pgsigio(tp->tun_sigio, SIGIO, 0);
400 ifnet_deserialize_all(ifp);
401 KNOTE(&tp->tun_rkq.ki_note, 0);
402 ifnet_serialize_all(ifp);
408 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
413 ifnet_serialize_all(ifp);
414 error = tunoutput_serialized(ifp, m0, dst, rt);
415 ifnet_deserialize_all(ifp);
421 * the ops interface is now pretty minimal.
424 tunioctl(struct dev_ioctl_args *ap)
426 cdev_t dev = ap->a_head.a_dev;
427 struct tun_softc *tp = dev->si_drv1;
428 struct tuninfo *tunp;
432 tunp = (struct tuninfo *)ap->a_data;
433 if (tunp->mtu < IF_MINMTU)
435 tp->tun_if.if_mtu = tunp->mtu;
436 tp->tun_if.if_type = tunp->type;
437 tp->tun_if.if_baudrate = tunp->baudrate;
440 tunp = (struct tuninfo *)ap->a_data;
441 tunp->mtu = tp->tun_if.if_mtu;
442 tunp->type = tp->tun_if.if_type;
443 tunp->baudrate = tp->tun_if.if_baudrate;
446 tundebug = *(int *)ap->a_data;
449 *(int *)ap->a_data = tundebug;
452 if (*(int *)ap->a_data) {
453 tp->tun_flags |= TUN_LMODE;
454 tp->tun_flags &= ~TUN_IFHEAD;
456 tp->tun_flags &= ~TUN_LMODE;
459 if (*(int *)ap->a_data) {
460 tp->tun_flags |= TUN_IFHEAD;
461 tp->tun_flags &= ~TUN_LMODE;
463 tp->tun_flags &= ~TUN_IFHEAD;
466 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
469 /* deny this if UP */
470 if (tp->tun_if.if_flags & IFF_UP)
473 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
474 case IFF_POINTOPOINT:
476 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
477 tp->tun_if.if_flags |= *(int *)ap->a_data;
484 tp->tun_pid = curproc->p_pid;
487 if (*(int *)ap->a_data)
488 tp->tun_flags |= TUN_ASYNC;
490 tp->tun_flags &= ~TUN_ASYNC;
493 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
496 mb = ifq_poll(&tp->tun_if.if_snd);
497 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
498 *(int *)ap->a_data += mb->m_len;
500 *(int *)ap->a_data = 0;
504 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
507 *(int *)ap->a_data = fgetown(tp->tun_sigio);
510 /* This is deprecated, FIOSETOWN should be used instead. */
512 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
514 /* This is deprecated, FIOGETOWN should be used instead. */
516 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
526 * The ops read interface - reads a packet at a time, or at
527 * least as much of a packet as can be read.
530 tunread(struct dev_read_args *ap)
532 cdev_t dev = ap->a_head.a_dev;
533 struct uio *uio = ap->a_uio;
534 struct tun_softc *tp = dev->si_drv1;
535 struct ifnet *ifp = &tp->tun_if;
539 TUNDEBUG(ifp, "read\n");
540 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
541 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
545 tp->tun_flags &= ~TUN_RWAIT;
547 ifnet_serialize_all(ifp);
549 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
550 if (ap->a_ioflag & IO_NDELAY) {
551 ifnet_deserialize_all(ifp);
554 tp->tun_flags |= TUN_RWAIT;
555 ifnet_deserialize_all(ifp);
556 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
558 ifnet_serialize_all(ifp);
561 ifnet_deserialize_all(ifp);
563 while (m0 && uio->uio_resid > 0 && error == 0) {
564 len = (int)szmin(uio->uio_resid, m0->m_len);
566 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
571 TUNDEBUG(ifp, "Dropping mbuf\n");
578 * the ops write interface - an atomic write is a packet - or else!
581 tunwrite(struct dev_write_args *ap)
583 cdev_t dev = ap->a_head.a_dev;
584 struct uio *uio = ap->a_uio;
585 struct tun_softc *tp = dev->si_drv1;
586 struct ifnet *ifp = &tp->tun_if;
587 struct mbuf *top, **mp, *m;
593 TUNDEBUG(ifp, "tunwrite\n");
595 if (uio->uio_resid == 0)
598 if (uio->uio_resid > TUNMRU) {
599 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
602 tlen = uio->uio_resid;
604 /* get a header mbuf */
605 MGETHDR(m, MB_DONTWAIT, MT_DATA);
612 while (error == 0 && uio->uio_resid > 0) {
613 m->m_len = (int)szmin(mlen, uio->uio_resid);
614 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
617 if (uio->uio_resid > 0) {
618 MGET (m, MB_DONTWAIT, MT_DATA);
633 top->m_pkthdr.len = (int)tlen;
634 top->m_pkthdr.rcvif = ifp;
637 if (tp->tun_flags & TUN_IFHEAD) {
639 * Conveniently, we already have a 4-byte address
640 * family prepended to our packet !
641 * Inconveniently, it's in the wrong byte order !
643 if ((top = m_pullup(top, sizeof(family))) == NULL)
645 *mtod(top, u_int32_t *) =
646 ntohl(*mtod(top, u_int32_t *));
647 bpf_mtap(ifp->if_bpf, top);
648 *mtod(top, u_int32_t *) =
649 htonl(*mtod(top, u_int32_t *));
652 * We need to prepend the address family as
655 static const uint32_t af = AF_INET;
657 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
661 if (tp->tun_flags & TUN_IFHEAD) {
662 if (top->m_len < sizeof(family) &&
663 (top = m_pullup(top, sizeof(family))) == NULL)
665 family = ntohl(*mtod(top, u_int32_t *));
666 m_adj(top, sizeof(family));
670 ifp->if_ibytes += top->m_pkthdr.len;
696 return (EAFNOSUPPORT);
699 netisr_queue(isr, top);
703 static struct filterops tun_read_filtops =
704 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_read };
705 static struct filterops tun_write_filtops =
706 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_write };
709 tunkqfilter(struct dev_kqfilter_args *ap)
711 cdev_t dev = ap->a_head.a_dev;
712 struct tun_softc *tp = dev->si_drv1;
713 struct knote *kn = ap->a_kn;
717 ifnet_serialize_all(&tp->tun_if);
719 switch (kn->kn_filter) {
721 kn->kn_fop = &tun_read_filtops;
722 kn->kn_hook = (caddr_t)tp;
725 kn->kn_fop = &tun_write_filtops;
726 kn->kn_hook = (caddr_t)tp;
729 ifnet_deserialize_all(&tp->tun_if);
730 ap->a_result = EOPNOTSUPP;
734 klist = &tp->tun_rkq.ki_note;
735 knote_insert(klist, kn);
736 ifnet_deserialize_all(&tp->tun_if);
742 tun_filter_detach(struct knote *kn)
744 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
745 struct klist *klist = &tp->tun_rkq.ki_note;
747 knote_remove(klist, kn);
751 tun_filter_write(struct knote *kn, long hint)
753 /* Always ready for a write */
758 tun_filter_read(struct knote *kn, long hint)
760 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
763 ifnet_serialize_all(&tp->tun_if);
764 if (!ifq_is_empty(&tp->tun_if.if_snd))
766 ifnet_deserialize_all(&tp->tun_if);
772 * Start packet transmission on the interface.
773 * when the interface queue is rate-limited by ALTQ,
774 * if_start is needed to drain packets from the queue in order
775 * to notify readers when outgoing packets become ready.
778 tunstart(struct ifnet *ifp)
780 struct tun_softc *tp = ifp->if_softc;
783 if (!ifq_is_enabled(&ifp->if_snd))
786 m = ifq_poll(&ifp->if_snd);
788 if (tp->tun_flags & TUN_RWAIT) {
789 tp->tun_flags &= ~TUN_RWAIT;
792 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
793 pgsigio(tp->tun_sigio, SIGIO, 0);
794 ifnet_deserialize_tx(ifp);
795 KNOTE(&tp->tun_rkq.ki_note, 0);
796 ifnet_serialize_tx(ifp);