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>
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, "");
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 *);
80 static d_open_t tunopen;
81 static d_close_t tunclose;
82 static d_read_t tunread;
83 static d_write_t tunwrite;
84 static d_ioctl_t tunioctl;
85 static d_poll_t tunpoll;
87 static d_clone_t tunclone;
88 DEVFS_DECLARE_CLONE_BITMAP(tun);
91 #define TUN_PREALLOCATED_UNITS 4
93 #define TUN_PREALLOCATED_UNITS NTUN
97 static struct dev_ops tun_ops = {
98 { "tun", CDEV_MAJOR, 0 },
108 tunattach(void *dummy)
111 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
112 tunclone, UID_UUCP, GID_DIALER, 0600, "tun");
113 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
114 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
115 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
117 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
121 tunclone(struct dev_clone_args *ap)
125 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
126 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
133 tuncreate(cdev_t dev)
135 struct tun_softc *sc;
139 dev = make_dev(&tun_ops, minor(dev),
140 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
143 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
144 sc->tun_flags = TUN_INITED;
147 if_initname(ifp, "tun", lminor(dev));
148 ifp->if_mtu = TUNMTU;
149 ifp->if_ioctl = tunifioctl;
150 ifp->if_output = tunoutput;
151 ifp->if_start = tunstart;
152 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
153 ifp->if_type = IFT_PPP;
154 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
155 ifq_set_ready(&ifp->if_snd);
157 if_attach(ifp, NULL);
158 bpfattach(ifp, DLT_NULL, sizeof(u_int));
163 * tunnel open - must be superuser & the device must be
167 tunopen(struct dev_open_args *ap)
169 cdev_t dev = ap->a_head.a_dev;
171 struct tun_softc *tp;
174 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
182 if (tp->tun_flags & TUN_OPEN)
184 tp->tun_pid = curproc->p_pid;
186 tp->tun_flags |= TUN_OPEN;
187 TUNDEBUG(ifp, "open\n");
192 * tunclose - close the device - mark i/f down & delete
196 tunclose(struct dev_close_args *ap)
198 cdev_t dev = ap->a_head.a_dev;
199 struct tun_softc *tp;
205 tp->tun_flags &= ~TUN_OPEN;
208 /* Junk all pending output. */
209 ifq_purge(&ifp->if_snd);
211 if (ifp->if_flags & IFF_UP)
213 ifp->if_flags &= ~IFF_RUNNING;
214 if_purgeaddrs_nolink(ifp);
216 funsetown(tp->tun_sigio);
217 selwakeup(&tp->tun_rsel);
219 TUNDEBUG(ifp, "closed\n");
221 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
222 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
229 tuninit(struct ifnet *ifp)
231 struct tun_softc *tp = ifp->if_softc;
232 struct ifaddr_container *ifac;
235 TUNDEBUG(ifp, "tuninit\n");
237 ifp->if_flags |= IFF_UP | IFF_RUNNING;
238 getmicrotime(&ifp->if_lastchange);
240 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
241 struct ifaddr *ifa = ifac->ifa;
243 if (ifa->ifa_addr == NULL) {
245 /* XXX: Should maybe return straight off? */
248 if (ifa->ifa_addr->sa_family == AF_INET) {
249 struct sockaddr_in *si;
251 si = (struct sockaddr_in *)ifa->ifa_addr;
252 if (si->sin_addr.s_addr)
253 tp->tun_flags |= TUN_IASET;
262 * Process an ioctl request.
267 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
269 struct ifreq *ifr = (struct ifreq *)data;
270 struct tun_softc *tp = ifp->if_softc;
276 ifs = (struct ifstat *)data;
278 ksprintf(ifs->ascii + strlen(ifs->ascii),
279 "\tOpened by PID %d\n", tp->tun_pid);
282 error = tuninit(ifp);
283 TUNDEBUG(ifp, "address set, error=%d\n", error);
286 error = tuninit(ifp);
287 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
290 ifp->if_mtu = ifr->ifr_mtu;
291 TUNDEBUG(ifp, "mtu set\n");
304 * tunoutput - queue packets from higher level ready to put out.
309 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
312 struct tun_softc *tp = ifp->if_softc;
314 struct altq_pktattr pktattr;
316 TUNDEBUG(ifp, "tunoutput\n");
318 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
319 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
325 * if the queueing discipline needs packet classification,
326 * do it before prepending link headers.
328 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
330 /* BPF write needs to be handled specially */
331 if (dst->sa_family == AF_UNSPEC) {
332 dst->sa_family = *(mtod(m0, int *));
333 m0->m_len -= sizeof(int);
334 m0->m_pkthdr.len -= sizeof(int);
335 m0->m_data += sizeof(int);
340 * We need to prepend the address family as
343 uint32_t af = dst->sa_family;
345 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
348 /* prepend sockaddr? this may abort if the mbuf allocation fails */
349 if (tp->tun_flags & TUN_LMODE) {
350 /* allocate space for sockaddr */
351 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
353 /* if allocation failed drop packet */
355 IF_DROP(&ifp->if_snd);
359 bcopy(dst, m0->m_data, dst->sa_len);
363 if (tp->tun_flags & TUN_IFHEAD) {
364 /* Prepend the address family */
365 M_PREPEND(m0, 4, MB_DONTWAIT);
367 /* if allocation failed drop packet */
369 IF_DROP(&ifp->if_snd);
373 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
376 if (dst->sa_family != AF_INET)
384 error = ifq_handoff(ifp, m0, &pktattr);
386 ifp->if_collisions++;
389 if (tp->tun_flags & TUN_RWAIT) {
390 tp->tun_flags &= ~TUN_RWAIT;
394 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
395 pgsigio(tp->tun_sigio, SIGIO, 0);
396 selwakeup(&tp->tun_rsel);
403 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
408 ifnet_serialize_all(ifp);
409 error = tunoutput_serialized(ifp, m0, dst, rt);
410 ifnet_deserialize_all(ifp);
416 * the ops interface is now pretty minimal.
419 tunioctl(struct dev_ioctl_args *ap)
421 cdev_t dev = ap->a_head.a_dev;
422 struct tun_softc *tp = dev->si_drv1;
423 struct tuninfo *tunp;
427 tunp = (struct tuninfo *)ap->a_data;
428 if (tunp->mtu < IF_MINMTU)
430 tp->tun_if.if_mtu = tunp->mtu;
431 tp->tun_if.if_type = tunp->type;
432 tp->tun_if.if_baudrate = tunp->baudrate;
435 tunp = (struct tuninfo *)ap->a_data;
436 tunp->mtu = tp->tun_if.if_mtu;
437 tunp->type = tp->tun_if.if_type;
438 tunp->baudrate = tp->tun_if.if_baudrate;
441 tundebug = *(int *)ap->a_data;
444 *(int *)ap->a_data = tundebug;
447 if (*(int *)ap->a_data) {
448 tp->tun_flags |= TUN_LMODE;
449 tp->tun_flags &= ~TUN_IFHEAD;
451 tp->tun_flags &= ~TUN_LMODE;
454 if (*(int *)ap->a_data) {
455 tp->tun_flags |= TUN_IFHEAD;
456 tp->tun_flags &= ~TUN_LMODE;
458 tp->tun_flags &= ~TUN_IFHEAD;
461 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
464 /* deny this if UP */
465 if (tp->tun_if.if_flags & IFF_UP)
468 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
469 case IFF_POINTOPOINT:
471 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
472 tp->tun_if.if_flags |= *(int *)ap->a_data;
479 tp->tun_pid = curproc->p_pid;
482 if (*(int *)ap->a_data)
483 tp->tun_flags |= TUN_ASYNC;
485 tp->tun_flags &= ~TUN_ASYNC;
488 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
491 mb = ifq_poll(&tp->tun_if.if_snd);
492 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
493 *(int *)ap->a_data += mb->m_len;
495 *(int *)ap->a_data = 0;
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;
534 TUNDEBUG(ifp, "read\n");
535 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
536 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
540 tp->tun_flags &= ~TUN_RWAIT;
542 ifnet_serialize_all(ifp);
544 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
545 if (ap->a_ioflag & IO_NDELAY) {
546 ifnet_deserialize_all(ifp);
549 tp->tun_flags |= TUN_RWAIT;
550 ifnet_deserialize_all(ifp);
551 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
553 ifnet_serialize_all(ifp);
556 ifnet_deserialize_all(ifp);
558 while (m0 && uio->uio_resid > 0 && error == 0) {
559 len = (int)szmin(uio->uio_resid, m0->m_len);
561 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
566 TUNDEBUG(ifp, "Dropping mbuf\n");
573 * the ops write interface - an atomic write is a packet - or else!
576 tunwrite(struct dev_write_args *ap)
578 cdev_t dev = ap->a_head.a_dev;
579 struct uio *uio = ap->a_uio;
580 struct tun_softc *tp = dev->si_drv1;
581 struct ifnet *ifp = &tp->tun_if;
582 struct mbuf *top, **mp, *m;
588 TUNDEBUG(ifp, "tunwrite\n");
590 if (uio->uio_resid == 0)
593 if (uio->uio_resid > TUNMRU) {
594 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
597 tlen = uio->uio_resid;
599 /* get a header mbuf */
600 MGETHDR(m, MB_DONTWAIT, MT_DATA);
607 while (error == 0 && uio->uio_resid > 0) {
608 m->m_len = (int)szmin(mlen, uio->uio_resid);
609 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
612 if (uio->uio_resid > 0) {
613 MGET (m, MB_DONTWAIT, MT_DATA);
628 top->m_pkthdr.len = (int)tlen;
629 top->m_pkthdr.rcvif = ifp;
632 if (tp->tun_flags & TUN_IFHEAD) {
634 * Conveniently, we already have a 4-byte address
635 * family prepended to our packet !
636 * Inconveniently, it's in the wrong byte order !
638 if ((top = m_pullup(top, sizeof(family))) == NULL)
640 *mtod(top, u_int32_t *) =
641 ntohl(*mtod(top, u_int32_t *));
642 bpf_mtap(ifp->if_bpf, top);
643 *mtod(top, u_int32_t *) =
644 htonl(*mtod(top, u_int32_t *));
647 * We need to prepend the address family as
650 static const uint32_t af = AF_INET;
652 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
656 if (tp->tun_flags & TUN_IFHEAD) {
657 if (top->m_len < sizeof(family) &&
658 (top = m_pullup(top, sizeof(family))) == NULL)
660 family = ntohl(*mtod(top, u_int32_t *));
661 m_adj(top, sizeof(family));
665 ifp->if_ibytes += top->m_pkthdr.len;
691 return (EAFNOSUPPORT);
694 netisr_dispatch(isr, top);
699 * tunpoll - the poll interface, this is only useful on reads
700 * really. The write detect always returns true, write never blocks
701 * anyway, it either accepts the packet or drops it.
704 tunpoll(struct dev_poll_args *ap)
706 cdev_t dev = ap->a_head.a_dev;
707 struct tun_softc *tp = dev->si_drv1;
708 struct ifnet *ifp = &tp->tun_if;
711 TUNDEBUG(ifp, "tunpoll\n");
713 ifnet_serialize_all(ifp);
715 if (ap->a_events & (POLLIN | POLLRDNORM)) {
716 if (!ifq_is_empty(&ifp->if_snd)) {
717 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
718 revents |= ap->a_events & (POLLIN | POLLRDNORM);
720 TUNDEBUG(ifp, "tunpoll waiting\n");
721 selrecord(curthread, &tp->tun_rsel);
724 if (ap->a_events & (POLLOUT | POLLWRNORM))
725 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
727 ifnet_deserialize_all(ifp);
728 ap->a_events = revents;
733 * Start packet transmission on the interface.
734 * when the interface queue is rate-limited by ALTQ,
735 * if_start is needed to drain packets from the queue in order
736 * to notify readers when outgoing packets become ready.
739 tunstart(struct ifnet *ifp)
741 struct tun_softc *tp = ifp->if_softc;
744 if (!ifq_is_enabled(&ifp->if_snd))
747 m = ifq_poll(&ifp->if_snd);
749 if (tp->tun_flags & TUN_RWAIT) {
750 tp->tun_flags &= ~TUN_RWAIT;
753 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
754 pgsigio(tp->tun_sigio, SIGIO, 0);
755 selwakeup(&tp->tun_rsel);