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 <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, "");
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 make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
106 tunclone, UID_UUCP, GID_DIALER, 0600, "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);
111 /* Doesn't need uninit because unloading is not possible, see PSEUDO_SET */
115 tunclone(struct dev_clone_args *ap)
119 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
120 ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER, 0600,
127 tuncreate(cdev_t dev)
129 struct tun_softc *sc;
133 dev = make_dev(&tun_ops, minor(dev),
134 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
137 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
138 sc->tun_flags = TUN_INITED;
141 if_initname(ifp, "tun", lminor(dev));
142 ifp->if_mtu = TUNMTU;
143 ifp->if_ioctl = tunifioctl;
144 ifp->if_output = tunoutput;
145 ifp->if_start = tunstart;
146 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
147 ifp->if_type = IFT_PPP;
148 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
149 ifq_set_ready(&ifp->if_snd);
151 if_attach(ifp, NULL);
152 bpfattach(ifp, DLT_NULL, sizeof(u_int));
157 * tunnel open - must be superuser & the device must be
161 tunopen(struct dev_open_args *ap)
163 cdev_t dev = ap->a_head.a_dev;
165 struct tun_softc *tp;
168 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
176 if (tp->tun_flags & TUN_OPEN)
178 tp->tun_pid = curproc->p_pid;
180 tp->tun_flags |= TUN_OPEN;
181 TUNDEBUG(ifp, "open\n");
186 * tunclose - close the device - mark i/f down & delete
190 tunclose(struct dev_close_args *ap)
192 cdev_t dev = ap->a_head.a_dev;
193 struct tun_softc *tp;
199 tp->tun_flags &= ~TUN_OPEN;
202 /* Junk all pending output. */
203 ifq_purge(&ifp->if_snd);
205 if (ifp->if_flags & IFF_UP)
207 ifp->if_flags &= ~IFF_RUNNING;
208 if_purgeaddrs_nolink(ifp);
210 funsetown(tp->tun_sigio);
211 selwakeup(&tp->tun_rsel);
213 TUNDEBUG(ifp, "closed\n");
215 if (dev->si_uminor >= TUN_PREALLOCATED_UNITS) {
216 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), dev->si_uminor);
223 tuninit(struct ifnet *ifp)
225 struct tun_softc *tp = ifp->if_softc;
226 struct ifaddr_container *ifac;
229 TUNDEBUG(ifp, "tuninit\n");
231 ifp->if_flags |= IFF_UP | IFF_RUNNING;
232 getmicrotime(&ifp->if_lastchange);
234 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
235 struct ifaddr *ifa = ifac->ifa;
237 if (ifa->ifa_addr == NULL) {
239 /* XXX: Should maybe return straight off? */
242 if (ifa->ifa_addr->sa_family == AF_INET) {
243 struct sockaddr_in *si;
245 si = (struct sockaddr_in *)ifa->ifa_addr;
246 if (si->sin_addr.s_addr)
247 tp->tun_flags |= TUN_IASET;
256 * Process an ioctl request.
261 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
263 struct ifreq *ifr = (struct ifreq *)data;
264 struct tun_softc *tp = ifp->if_softc;
270 ifs = (struct ifstat *)data;
272 ksprintf(ifs->ascii + strlen(ifs->ascii),
273 "\tOpened by PID %d\n", tp->tun_pid);
276 error = tuninit(ifp);
277 TUNDEBUG(ifp, "address set, error=%d\n", error);
280 error = tuninit(ifp);
281 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
284 ifp->if_mtu = ifr->ifr_mtu;
285 TUNDEBUG(ifp, "mtu set\n");
298 * tunoutput - queue packets from higher level ready to put out.
303 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
306 struct tun_softc *tp = ifp->if_softc;
308 struct altq_pktattr pktattr;
310 TUNDEBUG(ifp, "tunoutput\n");
312 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
313 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
319 * if the queueing discipline needs packet classification,
320 * do it before prepending link headers.
322 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
324 /* BPF write needs to be handled specially */
325 if (dst->sa_family == AF_UNSPEC) {
326 dst->sa_family = *(mtod(m0, int *));
327 m0->m_len -= sizeof(int);
328 m0->m_pkthdr.len -= sizeof(int);
329 m0->m_data += sizeof(int);
334 * We need to prepend the address family as
337 uint32_t af = dst->sa_family;
339 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
342 /* prepend sockaddr? this may abort if the mbuf allocation fails */
343 if (tp->tun_flags & TUN_LMODE) {
344 /* allocate space for sockaddr */
345 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
347 /* if allocation failed drop packet */
349 IF_DROP(&ifp->if_snd);
353 bcopy(dst, m0->m_data, dst->sa_len);
357 if (tp->tun_flags & TUN_IFHEAD) {
358 /* Prepend the address family */
359 M_PREPEND(m0, 4, MB_DONTWAIT);
361 /* if allocation failed drop packet */
363 IF_DROP(&ifp->if_snd);
367 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
370 if (dst->sa_family != AF_INET)
378 error = ifq_handoff(ifp, m0, &pktattr);
380 ifp->if_collisions++;
383 if (tp->tun_flags & TUN_RWAIT) {
384 tp->tun_flags &= ~TUN_RWAIT;
388 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
389 pgsigio(tp->tun_sigio, SIGIO, 0);
390 selwakeup(&tp->tun_rsel);
397 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
402 ifnet_serialize_all(ifp);
403 error = tunoutput_serialized(ifp, m0, dst, rt);
404 ifnet_deserialize_all(ifp);
410 * the ops interface is now pretty minimal.
413 tunioctl(struct dev_ioctl_args *ap)
415 cdev_t dev = ap->a_head.a_dev;
416 struct tun_softc *tp = dev->si_drv1;
417 struct tuninfo *tunp;
421 tunp = (struct tuninfo *)ap->a_data;
422 if (tunp->mtu < IF_MINMTU)
424 tp->tun_if.if_mtu = tunp->mtu;
425 tp->tun_if.if_type = tunp->type;
426 tp->tun_if.if_baudrate = tunp->baudrate;
429 tunp = (struct tuninfo *)ap->a_data;
430 tunp->mtu = tp->tun_if.if_mtu;
431 tunp->type = tp->tun_if.if_type;
432 tunp->baudrate = tp->tun_if.if_baudrate;
435 tundebug = *(int *)ap->a_data;
438 *(int *)ap->a_data = tundebug;
441 if (*(int *)ap->a_data) {
442 tp->tun_flags |= TUN_LMODE;
443 tp->tun_flags &= ~TUN_IFHEAD;
445 tp->tun_flags &= ~TUN_LMODE;
448 if (*(int *)ap->a_data) {
449 tp->tun_flags |= TUN_IFHEAD;
450 tp->tun_flags &= ~TUN_LMODE;
452 tp->tun_flags &= ~TUN_IFHEAD;
455 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
458 /* deny this if UP */
459 if (tp->tun_if.if_flags & IFF_UP)
462 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
463 case IFF_POINTOPOINT:
465 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
466 tp->tun_if.if_flags |= *(int *)ap->a_data;
473 tp->tun_pid = curproc->p_pid;
476 if (*(int *)ap->a_data)
477 tp->tun_flags |= TUN_ASYNC;
479 tp->tun_flags &= ~TUN_ASYNC;
482 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
485 mb = ifq_poll(&tp->tun_if.if_snd);
486 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
487 *(int *)ap->a_data += mb->m_len;
489 *(int *)ap->a_data = 0;
493 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
496 *(int *)ap->a_data = fgetown(tp->tun_sigio);
499 /* This is deprecated, FIOSETOWN should be used instead. */
501 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
503 /* This is deprecated, FIOGETOWN should be used instead. */
505 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
515 * The ops read interface - reads a packet at a time, or at
516 * least as much of a packet as can be read.
519 tunread(struct dev_read_args *ap)
521 cdev_t dev = ap->a_head.a_dev;
522 struct uio *uio = ap->a_uio;
523 struct tun_softc *tp = dev->si_drv1;
524 struct ifnet *ifp = &tp->tun_if;
528 TUNDEBUG(ifp, "read\n");
529 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
530 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
534 tp->tun_flags &= ~TUN_RWAIT;
536 ifnet_serialize_all(ifp);
538 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
539 if (ap->a_ioflag & IO_NDELAY) {
540 ifnet_deserialize_all(ifp);
543 tp->tun_flags |= TUN_RWAIT;
544 ifnet_deserialize_all(ifp);
545 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
547 ifnet_serialize_all(ifp);
550 ifnet_deserialize_all(ifp);
552 while (m0 && uio->uio_resid > 0 && error == 0) {
553 len = (int)szmin(uio->uio_resid, m0->m_len);
555 error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
560 TUNDEBUG(ifp, "Dropping mbuf\n");
567 * the ops write interface - an atomic write is a packet - or else!
570 tunwrite(struct dev_write_args *ap)
572 cdev_t dev = ap->a_head.a_dev;
573 struct uio *uio = ap->a_uio;
574 struct tun_softc *tp = dev->si_drv1;
575 struct ifnet *ifp = &tp->tun_if;
576 struct mbuf *top, **mp, *m;
582 TUNDEBUG(ifp, "tunwrite\n");
584 if (uio->uio_resid == 0)
587 if (uio->uio_resid > TUNMRU) {
588 TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
591 tlen = uio->uio_resid;
593 /* get a header mbuf */
594 MGETHDR(m, MB_DONTWAIT, MT_DATA);
601 while (error == 0 && uio->uio_resid > 0) {
602 m->m_len = (int)szmin(mlen, uio->uio_resid);
603 error = uiomove(mtod (m, caddr_t), (size_t)m->m_len, uio);
606 if (uio->uio_resid > 0) {
607 MGET (m, MB_DONTWAIT, MT_DATA);
622 top->m_pkthdr.len = (int)tlen;
623 top->m_pkthdr.rcvif = ifp;
626 if (tp->tun_flags & TUN_IFHEAD) {
628 * Conveniently, we already have a 4-byte address
629 * family prepended to our packet !
630 * Inconveniently, it's in the wrong byte order !
632 if ((top = m_pullup(top, sizeof(family))) == NULL)
634 *mtod(top, u_int32_t *) =
635 ntohl(*mtod(top, u_int32_t *));
636 bpf_mtap(ifp->if_bpf, top);
637 *mtod(top, u_int32_t *) =
638 htonl(*mtod(top, u_int32_t *));
641 * We need to prepend the address family as
644 static const uint32_t af = AF_INET;
646 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
650 if (tp->tun_flags & TUN_IFHEAD) {
651 if (top->m_len < sizeof(family) &&
652 (top = m_pullup(top, sizeof(family))) == NULL)
654 family = ntohl(*mtod(top, u_int32_t *));
655 m_adj(top, sizeof(family));
659 ifp->if_ibytes += top->m_pkthdr.len;
685 return (EAFNOSUPPORT);
688 netisr_dispatch(isr, top);
693 * tunpoll - the poll interface, this is only useful on reads
694 * really. The write detect always returns true, write never blocks
695 * anyway, it either accepts the packet or drops it.
698 tunpoll(struct dev_poll_args *ap)
700 cdev_t dev = ap->a_head.a_dev;
701 struct tun_softc *tp = dev->si_drv1;
702 struct ifnet *ifp = &tp->tun_if;
705 TUNDEBUG(ifp, "tunpoll\n");
707 ifnet_serialize_all(ifp);
709 if (ap->a_events & (POLLIN | POLLRDNORM)) {
710 if (!ifq_is_empty(&ifp->if_snd)) {
711 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
712 revents |= ap->a_events & (POLLIN | POLLRDNORM);
714 TUNDEBUG(ifp, "tunpoll waiting\n");
715 selrecord(curthread, &tp->tun_rsel);
718 if (ap->a_events & (POLLOUT | POLLWRNORM))
719 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
721 ifnet_deserialize_all(ifp);
722 ap->a_events = revents;
727 * Start packet transmission on the interface.
728 * when the interface queue is rate-limited by ALTQ,
729 * if_start is needed to drain packets from the queue in order
730 * to notify readers when outgoing packets become ready.
733 tunstart(struct ifnet *ifp)
735 struct tun_softc *tp = ifp->if_softc;
738 if (!ifq_is_enabled(&ifp->if_snd))
741 m = ifq_poll(&ifp->if_snd);
743 if (tp->tun_flags & TUN_RWAIT) {
744 tp->tun_flags &= ~TUN_RWAIT;
747 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
748 pgsigio(tp->tun_sigio, SIGIO, 0);
749 selwakeup(&tp->tun_rsel);