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.30 2006/09/10 01:26:40 dillon Exp $
20 #include "opt_atalk.h"
22 #include "opt_inet6.h"
25 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/socket.h>
31 #include <sys/device.h>
32 #include <sys/filio.h>
33 #include <sys/sockio.h>
34 #include <sys/thread2.h>
35 #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>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
57 #include "if_tunvar.h"
60 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
62 static void tunattach (void *);
63 PSEUDO_SET(tunattach, if_tun);
65 static void tuncreate (cdev_t dev);
67 #define TUNDEBUG if (tundebug) if_printf
68 static int tundebug = 0;
69 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
71 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
73 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
74 static int tuninit (struct ifnet *);
75 static void tunstart(struct ifnet *);
77 static d_open_t tunopen;
78 static d_close_t tunclose;
79 static d_read_t tunread;
80 static d_write_t tunwrite;
81 static d_ioctl_t tunioctl;
82 static d_poll_t tunpoll;
85 static struct dev_ops tun_ops = {
86 { "tun", CDEV_MAJOR, 0 },
96 tunattach(void *dummy)
98 dev_ops_add(&tun_ops, 0, 0);
102 tuncreate(cdev_t dev)
104 struct tun_softc *sc;
107 dev = make_dev(&tun_ops, minor(dev),
108 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
110 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
111 bzero(sc, sizeof *sc);
112 sc->tun_flags = TUN_INITED;
115 if_initname(ifp, "tun", lminor(dev));
116 ifp->if_mtu = TUNMTU;
117 ifp->if_ioctl = tunifioctl;
118 ifp->if_output = tunoutput;
119 ifp->if_start = tunstart;
120 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
121 ifp->if_type = IFT_PPP;
122 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
123 ifq_set_ready(&ifp->if_snd);
125 if_attach(ifp, NULL);
126 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 * tunnel open - must be superuser & the device must be
135 tunopen(struct dev_open_args *ap)
137 cdev_t dev = ap->a_head.a_dev;
139 struct tun_softc *tp;
142 if ((error = suser_cred(ap->a_cred, 0)) != NULL)
150 if (tp->tun_flags & TUN_OPEN)
152 tp->tun_pid = curproc->p_pid;
154 tp->tun_flags |= TUN_OPEN;
155 TUNDEBUG(ifp, "open\n");
160 * tunclose - close the device - mark i/f down & delete
164 tunclose(struct dev_close_args *ap)
166 cdev_t dev = ap->a_head.a_dev;
167 struct tun_softc *tp;
173 tp->tun_flags &= ~TUN_OPEN;
176 /* Junk all pending output. */
177 lwkt_serialize_enter(ifp->if_serializer);
178 ifq_purge(&ifp->if_snd);
179 lwkt_serialize_exit(ifp->if_serializer);
181 if (ifp->if_flags & IFF_UP) {
182 lwkt_serialize_enter(ifp->if_serializer);
184 lwkt_serialize_exit(ifp->if_serializer);
187 if (ifp->if_flags & IFF_RUNNING) {
190 lwkt_serialize_enter(ifp->if_serializer);
191 /* find internet addresses and delete routes */
192 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
193 if (ifa->ifa_addr->sa_family == AF_INET) {
194 rtinit(ifa, (int)RTM_DELETE,
195 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
198 ifp->if_flags &= ~IFF_RUNNING;
199 lwkt_serialize_exit(ifp->if_serializer);
202 funsetown(tp->tun_sigio);
203 selwakeup(&tp->tun_rsel);
205 TUNDEBUG(ifp, "closed\n");
210 tuninit(struct ifnet *ifp)
212 struct tun_softc *tp = ifp->if_softc;
216 TUNDEBUG(ifp, "tuninit\n");
218 ifp->if_flags |= IFF_UP | IFF_RUNNING;
219 getmicrotime(&ifp->if_lastchange);
221 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
222 ifa = TAILQ_NEXT(ifa, ifa_link)) {
223 if (ifa->ifa_addr == NULL)
225 /* XXX: Should maybe return straight off? */
228 if (ifa->ifa_addr->sa_family == AF_INET) {
229 struct sockaddr_in *si;
231 si = (struct sockaddr_in *)ifa->ifa_addr;
232 if (si->sin_addr.s_addr)
233 tp->tun_flags |= TUN_IASET;
235 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
236 if (si && si->sin_addr.s_addr)
237 tp->tun_flags |= TUN_DSTADDR;
246 * Process an ioctl request.
251 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
253 struct ifreq *ifr = (struct ifreq *)data;
254 struct tun_softc *tp = ifp->if_softc;
260 ifs = (struct ifstat *)data;
262 sprintf(ifs->ascii + strlen(ifs->ascii),
263 "\tOpened by PID %d\n", tp->tun_pid);
266 error = tuninit(ifp);
267 TUNDEBUG(ifp, "address set, error=%d\n", error);
270 error = tuninit(ifp);
271 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
274 ifp->if_mtu = ifr->ifr_mtu;
275 TUNDEBUG(ifp, "mtu set\n");
288 * tunoutput - queue packets from higher level ready to put out.
293 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
296 struct tun_softc *tp = ifp->if_softc;
298 struct altq_pktattr pktattr;
300 TUNDEBUG(ifp, "tunoutput\n");
302 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
303 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
309 * if the queueing discipline needs packet classification,
310 * do it before prepending link headers.
312 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
314 /* BPF write needs to be handled specially */
315 if (dst->sa_family == AF_UNSPEC) {
316 dst->sa_family = *(mtod(m0, int *));
317 m0->m_len -= sizeof(int);
318 m0->m_pkthdr.len -= sizeof(int);
319 m0->m_data += sizeof(int);
324 * We need to prepend the address family as
327 uint32_t af = dst->sa_family;
329 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
332 /* prepend sockaddr? this may abort if the mbuf allocation fails */
333 if (tp->tun_flags & TUN_LMODE) {
334 /* allocate space for sockaddr */
335 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
337 /* if allocation failed drop packet */
339 IF_DROP(&ifp->if_snd);
343 bcopy(dst, m0->m_data, dst->sa_len);
347 if (tp->tun_flags & TUN_IFHEAD) {
348 /* Prepend the address family */
349 M_PREPEND(m0, 4, MB_DONTWAIT);
351 /* if allocation failed drop packet */
353 IF_DROP(&ifp->if_snd);
357 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
360 if (dst->sa_family != AF_INET)
368 error = ifq_handoff(ifp, m0, &pktattr);
370 ifp->if_collisions++;
373 if (tp->tun_flags & TUN_RWAIT) {
374 tp->tun_flags &= ~TUN_RWAIT;
378 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
379 pgsigio(tp->tun_sigio, SIGIO, 0);
380 selwakeup(&tp->tun_rsel);
387 * the ops interface is now pretty minimal.
390 tunioctl(struct dev_ioctl_args *ap)
392 cdev_t dev = ap->a_head.a_dev;
393 struct tun_softc *tp = dev->si_drv1;
394 struct tuninfo *tunp;
398 tunp = (struct tuninfo *)ap->a_data;
399 if (tunp->mtu < IF_MINMTU)
401 tp->tun_if.if_mtu = tunp->mtu;
402 tp->tun_if.if_type = tunp->type;
403 tp->tun_if.if_baudrate = tunp->baudrate;
406 tunp = (struct tuninfo *)ap->a_data;
407 tunp->mtu = tp->tun_if.if_mtu;
408 tunp->type = tp->tun_if.if_type;
409 tunp->baudrate = tp->tun_if.if_baudrate;
412 tundebug = *(int *)ap->a_data;
415 *(int *)ap->a_data = tundebug;
418 if (*(int *)ap->a_data) {
419 tp->tun_flags |= TUN_LMODE;
420 tp->tun_flags &= ~TUN_IFHEAD;
422 tp->tun_flags &= ~TUN_LMODE;
425 if (*(int *)ap->a_data) {
426 tp->tun_flags |= TUN_IFHEAD;
427 tp->tun_flags &= ~TUN_LMODE;
429 tp->tun_flags &= ~TUN_IFHEAD;
432 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
435 /* deny this if UP */
436 if (tp->tun_if.if_flags & IFF_UP)
439 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
440 case IFF_POINTOPOINT:
442 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
443 tp->tun_if.if_flags |= *(int *)ap->a_data;
450 tp->tun_pid = curproc->p_pid;
453 if (*(int *)ap->a_data)
454 tp->tun_flags |= TUN_ASYNC;
456 tp->tun_flags &= ~TUN_ASYNC;
459 lwkt_serialize_enter(tp->tun_if.if_serializer);
460 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
463 mb = ifq_poll(&tp->tun_if.if_snd);
464 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
465 *(int *)ap->a_data += mb->m_len;
467 *(int *)ap->a_data = 0;
469 lwkt_serialize_exit(tp->tun_if.if_serializer);
472 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
475 *(int *)ap->a_data = fgetown(tp->tun_sigio);
478 /* This is deprecated, FIOSETOWN should be used instead. */
480 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
482 /* This is deprecated, FIOGETOWN should be used instead. */
484 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
494 * The ops read interface - reads a packet at a time, or at
495 * least as much of a packet as can be read.
498 tunread(struct dev_read_args *ap)
500 cdev_t dev = ap->a_head.a_dev;
501 struct uio *uio = ap->a_uio;
502 struct tun_softc *tp = dev->si_drv1;
503 struct ifnet *ifp = &tp->tun_if;
507 TUNDEBUG(ifp, "read\n");
508 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
509 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
513 tp->tun_flags &= ~TUN_RWAIT;
515 lwkt_serialize_enter(ifp->if_serializer);
517 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
518 if (ap->a_ioflag & IO_NDELAY) {
519 lwkt_serialize_exit(ifp->if_serializer);
522 tp->tun_flags |= TUN_RWAIT;
523 lwkt_serialize_exit(ifp->if_serializer);
524 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
526 lwkt_serialize_enter(ifp->if_serializer);
529 lwkt_serialize_exit(ifp->if_serializer);
531 while (m0 && uio->uio_resid > 0 && error == 0) {
532 len = min(uio->uio_resid, m0->m_len);
534 error = uiomove(mtod(m0, caddr_t), len, uio);
539 TUNDEBUG(ifp, "Dropping mbuf\n");
546 * the ops write interface - an atomic write is a packet - or else!
549 tunwrite(struct dev_write_args *ap)
551 cdev_t dev = ap->a_head.a_dev;
552 struct uio *uio = ap->a_uio;
553 struct tun_softc *tp = dev->si_drv1;
554 struct ifnet *ifp = &tp->tun_if;
555 struct mbuf *top, **mp, *m;
556 int error=0, tlen, mlen;
560 TUNDEBUG(ifp, "tunwrite\n");
562 if (uio->uio_resid == 0)
565 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
566 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
569 tlen = uio->uio_resid;
571 /* get a header mbuf */
572 MGETHDR(m, MB_DONTWAIT, MT_DATA);
579 while (error == 0 && uio->uio_resid > 0) {
580 m->m_len = min(mlen, uio->uio_resid);
581 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
584 if (uio->uio_resid > 0) {
585 MGET (m, MB_DONTWAIT, MT_DATA);
600 top->m_pkthdr.len = tlen;
601 top->m_pkthdr.rcvif = ifp;
604 if (tp->tun_flags & TUN_IFHEAD) {
606 * Conveniently, we already have a 4-byte address
607 * family prepended to our packet !
608 * Inconveniently, it's in the wrong byte order !
610 if ((top = m_pullup(top, sizeof(family))) == NULL)
612 *mtod(top, u_int32_t *) =
613 ntohl(*mtod(top, u_int32_t *));
614 bpf_mtap(ifp->if_bpf, top);
615 *mtod(top, u_int32_t *) =
616 htonl(*mtod(top, u_int32_t *));
619 * We need to prepend the address family as
622 static const uint32_t af = AF_INET;
624 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
628 if (tp->tun_flags & TUN_IFHEAD) {
629 if (top->m_len < sizeof(family) &&
630 (top = m_pullup(top, sizeof(family))) == NULL)
632 family = ntohl(*mtod(top, u_int32_t *));
633 m_adj(top, sizeof(family));
637 ifp->if_ibytes += top->m_pkthdr.len;
663 return (EAFNOSUPPORT);
666 netisr_dispatch(isr, top);
671 * tunpoll - the poll interface, this is only useful on reads
672 * really. The write detect always returns true, write never blocks
673 * anyway, it either accepts the packet or drops it.
676 tunpoll(struct dev_poll_args *ap)
678 cdev_t dev = ap->a_head.a_dev;
679 struct tun_softc *tp = dev->si_drv1;
680 struct ifnet *ifp = &tp->tun_if;
683 TUNDEBUG(ifp, "tunpoll\n");
685 lwkt_serialize_enter(ifp->if_serializer);
687 if (ap->a_events & (POLLIN | POLLRDNORM)) {
688 if (!ifq_is_empty(&ifp->if_snd)) {
689 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
690 revents |= ap->a_events & (POLLIN | POLLRDNORM);
692 TUNDEBUG(ifp, "tunpoll waiting\n");
693 selrecord(curthread, &tp->tun_rsel);
696 if (ap->a_events & (POLLOUT | POLLWRNORM))
697 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
699 lwkt_serialize_exit(ifp->if_serializer);
700 ap->a_events = revents;
705 * Start packet transmission on the interface.
706 * when the interface queue is rate-limited by ALTQ,
707 * if_start is needed to drain packets from the queue in order
708 * to notify readers when outgoing packets become ready.
711 tunstart(struct ifnet *ifp)
713 struct tun_softc *tp = ifp->if_softc;
716 if (!ifq_is_enabled(&ifp->if_snd))
719 m = ifq_poll(&ifp->if_snd);
721 if (tp->tun_flags & TUN_RWAIT) {
722 tp->tun_flags &= ~TUN_RWAIT;
725 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
726 pgsigio(tp->tun_sigio, SIGIO, 0);
727 selwakeup(&tp->tun_rsel);