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 $
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>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.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 <vfs/devfs/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, "");
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 *);
79 static d_open_t tunopen;
80 static d_close_t tunclose;
81 static d_read_t tunread;
82 static d_write_t tunwrite;
83 static d_ioctl_t tunioctl;
84 static d_poll_t tunpoll;
86 static d_clone_t tunclone;
87 DEVFS_DECLARE_CLONE_BITMAP(tun);
88 #define TUN_PREALLOCATED_UNITS 4
91 static struct dev_ops tun_ops = {
92 { "tun", CDEV_MAJOR, 0 },
102 tunattach(void *dummy)
105 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(tun));
107 for (i = 0; i < TUN_PREALLOCATED_UNITS; i++) {
108 make_dev(&tun_ops, i, UID_UUCP, GID_DIALER, 0600, "tun%d", i);
109 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), i);
112 make_dev(&tun_ops, 0, UID_UUCP, GID_DIALER, 0600, "tun");
113 devfs_clone_handler_add("tun", tunclone);
114 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
118 tunclone(struct dev_clone_args *ap)
122 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
123 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
130 tuncreate(cdev_t dev)
132 struct tun_softc *sc;
136 dev = make_dev(&tun_ops, minor(dev),
137 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
140 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
141 sc->tun_flags = TUN_INITED;
144 if_initname(ifp, "tun", lminor(dev));
145 ifp->if_mtu = TUNMTU;
146 ifp->if_ioctl = tunifioctl;
147 ifp->if_output = tunoutput;
148 ifp->if_start = tunstart;
149 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
150 ifp->if_type = IFT_PPP;
151 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
152 ifq_set_ready(&ifp->if_snd);
154 if_attach(ifp, NULL);
155 bpfattach(ifp, DLT_NULL, sizeof(u_int));
160 * tunnel open - must be superuser & the device must be
164 tunopen(struct dev_open_args *ap)
166 cdev_t dev = ap->a_head.a_dev;
168 struct tun_softc *tp;
171 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
179 if (tp->tun_flags & TUN_OPEN)
181 tp->tun_pid = curproc->p_pid;
183 tp->tun_flags |= TUN_OPEN;
184 TUNDEBUG(ifp, "open\n");
189 * tunclose - close the device - mark i/f down & delete
193 tunclose(struct dev_close_args *ap)
195 cdev_t dev = ap->a_head.a_dev;
196 struct tun_softc *tp;
202 tp->tun_flags &= ~TUN_OPEN;
205 /* Junk all pending output. */
206 ifq_purge(&ifp->if_snd);
208 if (ifp->if_flags & IFF_UP)
210 ifp->if_flags &= ~IFF_RUNNING;
211 if_purgeaddrs_nolink(ifp);
213 funsetown(tp->tun_sigio);
214 selwakeup(&tp->tun_rsel);
216 TUNDEBUG(ifp, "closed\n");
218 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
219 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
226 tuninit(struct ifnet *ifp)
228 struct tun_softc *tp = ifp->if_softc;
229 struct ifaddr_container *ifac;
232 TUNDEBUG(ifp, "tuninit\n");
234 ifp->if_flags |= IFF_UP | IFF_RUNNING;
235 getmicrotime(&ifp->if_lastchange);
237 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
238 struct ifaddr *ifa = ifac->ifa;
240 if (ifa->ifa_addr == NULL) {
242 /* XXX: Should maybe return straight off? */
245 if (ifa->ifa_addr->sa_family == AF_INET) {
246 struct sockaddr_in *si;
248 si = (struct sockaddr_in *)ifa->ifa_addr;
249 if (si->sin_addr.s_addr)
250 tp->tun_flags |= TUN_IASET;
259 * Process an ioctl request.
264 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
266 struct ifreq *ifr = (struct ifreq *)data;
267 struct tun_softc *tp = ifp->if_softc;
273 ifs = (struct ifstat *)data;
275 ksprintf(ifs->ascii + strlen(ifs->ascii),
276 "\tOpened by PID %d\n", tp->tun_pid);
279 error = tuninit(ifp);
280 TUNDEBUG(ifp, "address set, error=%d\n", error);
283 error = tuninit(ifp);
284 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
287 ifp->if_mtu = ifr->ifr_mtu;
288 TUNDEBUG(ifp, "mtu set\n");
301 * tunoutput - queue packets from higher level ready to put out.
306 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
309 struct tun_softc *tp = ifp->if_softc;
311 struct altq_pktattr pktattr;
313 TUNDEBUG(ifp, "tunoutput\n");
315 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
316 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
322 * if the queueing discipline needs packet classification,
323 * do it before prepending link headers.
325 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
327 /* BPF write needs to be handled specially */
328 if (dst->sa_family == AF_UNSPEC) {
329 dst->sa_family = *(mtod(m0, int *));
330 m0->m_len -= sizeof(int);
331 m0->m_pkthdr.len -= sizeof(int);
332 m0->m_data += sizeof(int);
337 * We need to prepend the address family as
340 uint32_t af = dst->sa_family;
342 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
345 /* prepend sockaddr? this may abort if the mbuf allocation fails */
346 if (tp->tun_flags & TUN_LMODE) {
347 /* allocate space for sockaddr */
348 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
350 /* if allocation failed drop packet */
352 IF_DROP(&ifp->if_snd);
356 bcopy(dst, m0->m_data, dst->sa_len);
360 if (tp->tun_flags & TUN_IFHEAD) {
361 /* Prepend the address family */
362 M_PREPEND(m0, 4, MB_DONTWAIT);
364 /* if allocation failed drop packet */
366 IF_DROP(&ifp->if_snd);
370 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
373 if (dst->sa_family != AF_INET)
381 error = ifq_handoff(ifp, m0, &pktattr);
383 ifp->if_collisions++;
386 if (tp->tun_flags & TUN_RWAIT) {
387 tp->tun_flags &= ~TUN_RWAIT;
391 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
392 pgsigio(tp->tun_sigio, SIGIO, 0);
393 selwakeup(&tp->tun_rsel);
400 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
405 ifnet_serialize_all(ifp);
406 error = tunoutput_serialized(ifp, m0, dst, rt);
407 ifnet_deserialize_all(ifp);
413 * the ops interface is now pretty minimal.
416 tunioctl(struct dev_ioctl_args *ap)
418 cdev_t dev = ap->a_head.a_dev;
419 struct tun_softc *tp = dev->si_drv1;
420 struct tuninfo *tunp;
424 tunp = (struct tuninfo *)ap->a_data;
425 if (tunp->mtu < IF_MINMTU)
427 tp->tun_if.if_mtu = tunp->mtu;
428 tp->tun_if.if_type = tunp->type;
429 tp->tun_if.if_baudrate = tunp->baudrate;
432 tunp = (struct tuninfo *)ap->a_data;
433 tunp->mtu = tp->tun_if.if_mtu;
434 tunp->type = tp->tun_if.if_type;
435 tunp->baudrate = tp->tun_if.if_baudrate;
438 tundebug = *(int *)ap->a_data;
441 *(int *)ap->a_data = tundebug;
444 if (*(int *)ap->a_data) {
445 tp->tun_flags |= TUN_LMODE;
446 tp->tun_flags &= ~TUN_IFHEAD;
448 tp->tun_flags &= ~TUN_LMODE;
451 if (*(int *)ap->a_data) {
452 tp->tun_flags |= TUN_IFHEAD;
453 tp->tun_flags &= ~TUN_LMODE;
455 tp->tun_flags &= ~TUN_IFHEAD;
458 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
461 /* deny this if UP */
462 if (tp->tun_if.if_flags & IFF_UP)
465 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
466 case IFF_POINTOPOINT:
468 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
469 tp->tun_if.if_flags |= *(int *)ap->a_data;
476 tp->tun_pid = curproc->p_pid;
479 if (*(int *)ap->a_data)
480 tp->tun_flags |= TUN_ASYNC;
482 tp->tun_flags &= ~TUN_ASYNC;
485 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
488 mb = ifq_poll(&tp->tun_if.if_snd);
489 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
490 *(int *)ap->a_data += mb->m_len;
492 *(int *)ap->a_data = 0;
496 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
499 *(int *)ap->a_data = fgetown(tp->tun_sigio);
502 /* This is deprecated, FIOSETOWN should be used instead. */
504 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
506 /* This is deprecated, FIOGETOWN should be used instead. */
508 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
518 * The ops read interface - reads a packet at a time, or at
519 * least as much of a packet as can be read.
522 tunread(struct dev_read_args *ap)
524 cdev_t dev = ap->a_head.a_dev;
525 struct uio *uio = ap->a_uio;
526 struct tun_softc *tp = dev->si_drv1;
527 struct ifnet *ifp = &tp->tun_if;
531 TUNDEBUG(ifp, "read\n");
532 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
533 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
537 tp->tun_flags &= ~TUN_RWAIT;
539 ifnet_serialize_all(ifp);
541 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
542 if (ap->a_ioflag & IO_NDELAY) {
543 ifnet_deserialize_all(ifp);
546 tp->tun_flags |= TUN_RWAIT;
547 ifnet_deserialize_all(ifp);
548 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
550 ifnet_serialize_all(ifp);
553 ifnet_deserialize_all(ifp);
555 while (m0 && uio->uio_resid > 0 && error == 0) {
556 len = min(uio->uio_resid, m0->m_len);
558 error = uiomove(mtod(m0, caddr_t), len, uio);
563 TUNDEBUG(ifp, "Dropping mbuf\n");
570 * the ops write interface - an atomic write is a packet - or else!
573 tunwrite(struct dev_write_args *ap)
575 cdev_t dev = ap->a_head.a_dev;
576 struct uio *uio = ap->a_uio;
577 struct tun_softc *tp = dev->si_drv1;
578 struct ifnet *ifp = &tp->tun_if;
579 struct mbuf *top, **mp, *m;
580 int error=0, tlen, mlen;
584 TUNDEBUG(ifp, "tunwrite\n");
586 if (uio->uio_resid == 0)
589 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
590 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
593 tlen = uio->uio_resid;
595 /* get a header mbuf */
596 MGETHDR(m, MB_DONTWAIT, MT_DATA);
603 while (error == 0 && uio->uio_resid > 0) {
604 m->m_len = min(mlen, uio->uio_resid);
605 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
608 if (uio->uio_resid > 0) {
609 MGET (m, MB_DONTWAIT, MT_DATA);
624 top->m_pkthdr.len = tlen;
625 top->m_pkthdr.rcvif = ifp;
628 if (tp->tun_flags & TUN_IFHEAD) {
630 * Conveniently, we already have a 4-byte address
631 * family prepended to our packet !
632 * Inconveniently, it's in the wrong byte order !
634 if ((top = m_pullup(top, sizeof(family))) == NULL)
636 *mtod(top, u_int32_t *) =
637 ntohl(*mtod(top, u_int32_t *));
638 bpf_mtap(ifp->if_bpf, top);
639 *mtod(top, u_int32_t *) =
640 htonl(*mtod(top, u_int32_t *));
643 * We need to prepend the address family as
646 static const uint32_t af = AF_INET;
648 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
652 if (tp->tun_flags & TUN_IFHEAD) {
653 if (top->m_len < sizeof(family) &&
654 (top = m_pullup(top, sizeof(family))) == NULL)
656 family = ntohl(*mtod(top, u_int32_t *));
657 m_adj(top, sizeof(family));
661 ifp->if_ibytes += top->m_pkthdr.len;
687 return (EAFNOSUPPORT);
690 netisr_dispatch(isr, top);
695 * tunpoll - the poll interface, this is only useful on reads
696 * really. The write detect always returns true, write never blocks
697 * anyway, it either accepts the packet or drops it.
700 tunpoll(struct dev_poll_args *ap)
702 cdev_t dev = ap->a_head.a_dev;
703 struct tun_softc *tp = dev->si_drv1;
704 struct ifnet *ifp = &tp->tun_if;
707 TUNDEBUG(ifp, "tunpoll\n");
709 ifnet_serialize_all(ifp);
711 if (ap->a_events & (POLLIN | POLLRDNORM)) {
712 if (!ifq_is_empty(&ifp->if_snd)) {
713 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
714 revents |= ap->a_events & (POLLIN | POLLRDNORM);
716 TUNDEBUG(ifp, "tunpoll waiting\n");
717 selrecord(curthread, &tp->tun_rsel);
720 if (ap->a_events & (POLLOUT | POLLWRNORM))
721 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
723 ifnet_deserialize_all(ifp);
724 ap->a_events = revents;
729 * Start packet transmission on the interface.
730 * when the interface queue is rate-limited by ALTQ,
731 * if_start is needed to drain packets from the queue in order
732 * to notify readers when outgoing packets become ready.
735 tunstart(struct ifnet *ifp)
737 struct tun_softc *tp = ifp->if_softc;
740 if (!ifq_is_enabled(&ifp->if_snd))
743 m = ifq_poll(&ifp->if_snd);
745 if (tp->tun_flags & TUN_RWAIT) {
746 tp->tun_flags &= ~TUN_RWAIT;
749 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
750 pgsigio(tp->tun_sigio, SIGIO, 0);
751 selwakeup(&tp->tun_rsel);