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"
23 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <sys/socket.h>
30 #include <sys/device.h>
31 #include <sys/filio.h>
32 #include <sys/sockio.h>
33 #include <sys/thread2.h>
34 #include <sys/ttycom.h>
35 #include <sys/signalvar.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
43 #include <sys/mplock2.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
50 #include <sys/devfs.h>
53 #include <netinet/in.h>
58 #include "if_tunvar.h"
61 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
63 static void tunattach (void *);
64 PSEUDO_SET(tunattach, if_tun);
66 static void tuncreate (cdev_t dev);
68 #define TUNDEBUG if (tundebug) if_printf
69 static int tundebug = 0;
70 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
71 "Enable debug output");
73 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
75 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
76 static int tuninit (struct ifnet *);
77 static void tunstart(struct ifnet *, struct ifaltq_subque *);
78 static void tun_filter_detach(struct knote *);
79 static int tun_filter_read(struct knote *, long);
80 static int tun_filter_write(struct knote *, long);
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;
89 static d_clone_t tunclone;
90 DEVFS_DECLARE_CLONE_BITMAP(tun);
93 #define TUN_PREALLOCATED_UNITS 4
95 #define TUN_PREALLOCATED_UNITS NTUN
98 static struct dev_ops tun_ops = {
105 .d_kqfilter = tunkqfilter
109 tunattach(void *dummy)
112 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
113 tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
114 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
115 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
116 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
118 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
122 tunclone(struct dev_clone_args *ap)
126 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
127 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
134 tuncreate(cdev_t dev)
136 struct tun_softc *sc;
140 dev = make_dev(&tun_ops, minor(dev),
141 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
144 sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
145 sc->tun_flags = TUN_INITED;
148 if_initname(ifp, "tun", lminor(dev));
149 ifp->if_mtu = TUNMTU;
150 ifp->if_ioctl = tunifioctl;
151 ifp->if_output = tunoutput;
152 ifp->if_start = tunstart;
153 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
154 ifp->if_type = IFT_PPP;
155 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
156 ifq_set_ready(&ifp->if_snd);
158 if_attach(ifp, NULL);
159 bpfattach(ifp, DLT_NULL, sizeof(u_int));
164 * tunnel open - must be superuser & the device must be
168 tunopen(struct dev_open_args *ap)
170 cdev_t dev = ap->a_head.a_dev;
172 struct tun_softc *tp;
175 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
183 if (tp->tun_flags & TUN_OPEN)
185 tp->tun_pid = curproc->p_pid;
187 tp->tun_flags |= TUN_OPEN;
188 TUNDEBUG(ifp, "open\n");
193 * tunclose - close the device - mark i/f down & delete
197 tunclose(struct dev_close_args *ap)
199 cdev_t dev = ap->a_head.a_dev;
200 struct tun_softc *tp;
206 tp->tun_flags &= ~TUN_OPEN;
209 /* Junk all pending output. */
210 ifq_purge_all(&ifp->if_snd);
212 if (ifp->if_flags & IFF_UP)
214 ifp->if_flags &= ~IFF_RUNNING;
215 if_purgeaddrs_nolink(ifp);
217 funsetown(&tp->tun_sigio);
218 KNOTE(&tp->tun_rkq.ki_note, 0);
220 TUNDEBUG(ifp, "closed\n");
222 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
223 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
230 tuninit(struct ifnet *ifp)
233 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);
345 * We need to prepend the address family as
348 uint32_t af = dst->sa_family;
350 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
355 /* prepend sockaddr? this may abort if the mbuf allocation fails */
356 if (tp->tun_flags & TUN_LMODE) {
357 /* allocate space for sockaddr */
358 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
360 /* if allocation failed drop packet */
362 IFNET_STAT_INC(ifp, oerrors, 1);
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 IFNET_STAT_INC(ifp, oerrors, 1);
378 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
381 if (dst->sa_family != AF_INET)
389 error = ifq_handoff(ifp, m0, &pktattr);
391 IFNET_STAT_INC(ifp, collisions, 1);
393 IFNET_STAT_INC(ifp, opackets, 1);
394 if (tp->tun_flags & TUN_RWAIT) {
395 tp->tun_flags &= ~TUN_RWAIT;
399 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
400 pgsigio(tp->tun_sigio, SIGIO, 0);
402 ifnet_deserialize_all(ifp);
403 KNOTE(&tp->tun_rkq.ki_note, 0);
404 ifnet_serialize_all(ifp);
410 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
415 ifnet_serialize_all(ifp);
416 error = tunoutput_serialized(ifp, m0, dst, rt);
417 ifnet_deserialize_all(ifp);
423 * the ops interface is now pretty minimal.
426 tunioctl(struct dev_ioctl_args *ap)
428 cdev_t dev = ap->a_head.a_dev;
429 struct tun_softc *tp = dev->si_drv1;
430 struct tuninfo *tunp;
434 tunp = (struct tuninfo *)ap->a_data;
435 if (tunp->mtu < IF_MINMTU)
437 tp->tun_if.if_mtu = tunp->mtu;
438 tp->tun_if.if_type = tunp->type;
439 tp->tun_if.if_baudrate = tunp->baudrate;
442 tunp = (struct tuninfo *)ap->a_data;
443 tunp->mtu = tp->tun_if.if_mtu;
444 tunp->type = tp->tun_if.if_type;
445 tunp->baudrate = tp->tun_if.if_baudrate;
448 tundebug = *(int *)ap->a_data;
451 *(int *)ap->a_data = tundebug;
454 if (*(int *)ap->a_data) {
455 tp->tun_flags |= TUN_LMODE;
456 tp->tun_flags &= ~TUN_IFHEAD;
458 tp->tun_flags &= ~TUN_LMODE;
461 if (*(int *)ap->a_data) {
462 tp->tun_flags |= TUN_IFHEAD;
463 tp->tun_flags &= ~TUN_LMODE;
465 tp->tun_flags &= ~TUN_IFHEAD;
468 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
471 /* deny this if UP */
472 if (tp->tun_if.if_flags & IFF_UP)
475 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
476 case IFF_POINTOPOINT:
478 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
479 tp->tun_if.if_flags |= *(int *)ap->a_data;
486 tp->tun_pid = curproc->p_pid;
489 if (*(int *)ap->a_data)
490 tp->tun_flags |= TUN_ASYNC;
492 tp->tun_flags &= ~TUN_ASYNC;
495 *(int *)ap->a_data = ifsq_poll_pktlen(
496 ifq_get_subq_default(&tp->tun_if.if_snd));
499 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
502 *(int *)ap->a_data = fgetown(&tp->tun_sigio);
505 /* This is deprecated, FIOSETOWN should be used instead. */
507 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
509 /* This is deprecated, FIOGETOWN should be used instead. */
511 *(int *)ap->a_data = -fgetown(&tp->tun_sigio);
521 * The ops read interface - reads a packet at a time, or at
522 * least as much of a packet as can be read.
525 tunread(struct dev_read_args *ap)
527 cdev_t dev = ap->a_head.a_dev;
528 struct uio *uio = ap->a_uio;
529 struct tun_softc *tp = dev->si_drv1;
530 struct ifnet *ifp = &tp->tun_if;
531 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
535 TUNDEBUG(ifp, "read\n");
536 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
537 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
541 tp->tun_flags &= ~TUN_RWAIT;
543 ifnet_serialize_all(ifp);
545 while ((m0 = ifsq_dequeue(ifsq)) == NULL) {
546 if (ap->a_ioflag & IO_NDELAY) {
547 ifnet_deserialize_all(ifp);
550 tp->tun_flags |= TUN_RWAIT;
551 ifnet_deserialize_all(ifp);
552 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
554 ifnet_serialize_all(ifp);
557 ifnet_deserialize_all(ifp);
559 while (m0 && uio->uio_resid > 0 && error == 0) {
560 len = (int)szmin(uio->uio_resid, m0->m_len);
562 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
567 TUNDEBUG(ifp, "Dropping mbuf\n");
574 * the ops write interface - an atomic write is a packet - or else!
577 tunwrite(struct dev_write_args *ap)
579 cdev_t dev = ap->a_head.a_dev;
580 struct uio *uio = ap->a_uio;
581 struct tun_softc *tp = dev->si_drv1;
582 struct ifnet *ifp = &tp->tun_if;
583 struct mbuf *top, **mp, *m;
589 TUNDEBUG(ifp, "tunwrite\n");
591 if (uio->uio_resid == 0)
594 if (uio->uio_resid > TUNMRU) {
595 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
598 tlen = uio->uio_resid;
600 /* get a header mbuf */
601 MGETHDR(m, MB_WAIT, MT_DATA);
608 while (error == 0 && uio->uio_resid > 0) {
609 m->m_len = (int)szmin(mlen, uio->uio_resid);
610 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
613 if (uio->uio_resid > 0) {
614 MGET (m, MB_WAIT, MT_DATA);
625 IFNET_STAT_INC(ifp, ierrors, 1);
629 top->m_pkthdr.len = (int)tlen;
630 top->m_pkthdr.rcvif = ifp;
636 if (tp->tun_flags & TUN_IFHEAD) {
638 * Conveniently, we already have a 4-byte
639 * address family prepended to our packet !
640 * Inconveniently, it's in the wrong byte
643 if ((top = m_pullup(top, sizeof(family)))
648 *mtod(top, u_int32_t *) =
649 ntohl(*mtod(top, u_int32_t *));
650 bpf_mtap(ifp->if_bpf, top);
651 *mtod(top, u_int32_t *) =
652 htonl(*mtod(top, u_int32_t *));
655 * We need to prepend the address family as
658 static const uint32_t af = AF_INET;
660 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
667 if (tp->tun_flags & TUN_IFHEAD) {
668 if (top->m_len < sizeof(family) &&
669 (top = m_pullup(top, sizeof(family))) == NULL)
671 family = ntohl(*mtod(top, u_int32_t *));
672 m_adj(top, sizeof(family));
676 IFNET_STAT_INC(ifp, ibytes, top->m_pkthdr.len);
677 IFNET_STAT_INC(ifp, ipackets, 1);
692 return (EAFNOSUPPORT);
695 netisr_queue(isr, top);
699 static struct filterops tun_read_filtops =
700 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_read };
701 static struct filterops tun_write_filtops =
702 { FILTEROP_ISFD, NULL, tun_filter_detach, tun_filter_write };
705 tunkqfilter(struct dev_kqfilter_args *ap)
707 cdev_t dev = ap->a_head.a_dev;
708 struct tun_softc *tp = dev->si_drv1;
709 struct knote *kn = ap->a_kn;
713 ifnet_serialize_all(&tp->tun_if);
715 switch (kn->kn_filter) {
717 kn->kn_fop = &tun_read_filtops;
718 kn->kn_hook = (caddr_t)tp;
721 kn->kn_fop = &tun_write_filtops;
722 kn->kn_hook = (caddr_t)tp;
725 ifnet_deserialize_all(&tp->tun_if);
726 ap->a_result = EOPNOTSUPP;
730 klist = &tp->tun_rkq.ki_note;
731 knote_insert(klist, kn);
732 ifnet_deserialize_all(&tp->tun_if);
738 tun_filter_detach(struct knote *kn)
740 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
741 struct klist *klist = &tp->tun_rkq.ki_note;
743 knote_remove(klist, kn);
747 tun_filter_write(struct knote *kn, long hint)
749 /* Always ready for a write */
754 tun_filter_read(struct knote *kn, long hint)
756 struct tun_softc *tp = (struct tun_softc *)kn->kn_hook;
759 ifnet_serialize_all(&tp->tun_if);
760 if (!ifsq_is_empty(ifq_get_subq_default(&tp->tun_if.if_snd)))
762 ifnet_deserialize_all(&tp->tun_if);
768 * Start packet transmission on the interface.
769 * when the interface queue is rate-limited by ALTQ,
770 * if_start is needed to drain packets from the queue in order
771 * to notify readers when outgoing packets become ready.
774 tunstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
776 struct tun_softc *tp = ifp->if_softc;
779 ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
781 if (!ifq_is_enabled(&ifp->if_snd))
786 if (tp->tun_flags & TUN_RWAIT) {
787 tp->tun_flags &= ~TUN_RWAIT;
790 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
791 pgsigio(tp->tun_sigio, SIGIO, 0);
792 ifsq_deserialize_hw(ifsq);
793 KNOTE(&tp->tun_rkq.ki_note, 0);
794 ifsq_serialize_hw(ifsq);