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.18 2005/05/08 18:17:05 joerg 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>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/ttycom.h>
34 #include <sys/signalvar.h>
35 #include <sys/filedesc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
44 #include <net/if_types.h>
45 #include <net/ifq_var.h>
46 #include <net/netisr.h>
47 #include <net/route.h>
50 #include <netinet/in.h>
55 #include "if_tunvar.h"
58 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
60 static void tunattach (void *);
61 PSEUDO_SET(tunattach, if_tun);
63 static void tuncreate (dev_t dev);
65 #define TUNDEBUG if (tundebug) printf
66 static int tundebug = 0;
67 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
69 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
71 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
72 static int tuninit (struct ifnet *);
73 static void tunstart(struct ifnet *);
75 static d_open_t tunopen;
76 static d_close_t tunclose;
77 static d_read_t tunread;
78 static d_write_t tunwrite;
79 static d_ioctl_t tunioctl;
80 static d_poll_t tunpoll;
83 static struct cdevsw tun_cdevsw = {
97 /* strategy */ nostrategy,
103 tunattach(void *dummy)
105 cdevsw_add(&tun_cdevsw, 0, 0);
112 struct tun_softc *sc;
115 dev = make_dev(&tun_cdevsw, minor(dev),
116 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
118 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
119 bzero(sc, sizeof *sc);
120 sc->tun_flags = TUN_INITED;
123 if_initname(ifp, "tun", lminor(dev));
124 ifp->if_mtu = TUNMTU;
125 ifp->if_ioctl = tunifioctl;
126 ifp->if_output = tunoutput;
127 ifp->if_start = tunstart;
128 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
129 ifp->if_type = IFT_PPP;
130 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
131 ifq_set_ready(&ifp->if_snd);
134 bpfattach(ifp, DLT_NULL, sizeof(u_int));
139 * tunnel open - must be superuser & the device must be
143 tunopen(dev_t dev, int flag, int mode, struct thread *td)
146 struct tun_softc *tp;
149 KKASSERT(td->td_proc);
150 if ((error = suser(td)) != NULL)
158 if (tp->tun_flags & TUN_OPEN)
160 tp->tun_pid = td->td_proc->p_pid;
162 tp->tun_flags |= TUN_OPEN;
163 TUNDEBUG("%s: open\n", ifp->if_xname);
168 * tunclose - close the device - mark i/f down & delete
172 tunclose(dev_t dev, int foo, int bar, struct thread *td)
175 struct tun_softc *tp;
181 tp->tun_flags &= ~TUN_OPEN;
184 /* Junk all pending output. */
186 ifq_purge(&ifp->if_snd);
189 if (ifp->if_flags & IFF_UP) {
195 if (ifp->if_flags & IFF_RUNNING) {
199 /* find internet addresses and delete routes */
200 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
201 if (ifa->ifa_addr->sa_family == AF_INET)
202 rtinit(ifa, (int)RTM_DELETE,
203 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
204 ifp->if_flags &= ~IFF_RUNNING;
208 funsetown(tp->tun_sigio);
209 selwakeup(&tp->tun_rsel);
211 TUNDEBUG ("%s: closed\n", ifp->if_xname);
219 struct tun_softc *tp = ifp->if_softc;
223 TUNDEBUG("%s: tuninit\n", ifp->if_xname);
225 ifp->if_flags |= IFF_UP | IFF_RUNNING;
226 getmicrotime(&ifp->if_lastchange);
228 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
229 ifa = TAILQ_NEXT(ifa, ifa_link)) {
230 if (ifa->ifa_addr == NULL)
232 /* XXX: Should maybe return straight off? */
235 if (ifa->ifa_addr->sa_family == AF_INET) {
236 struct sockaddr_in *si;
238 si = (struct sockaddr_in *)ifa->ifa_addr;
239 if (si->sin_addr.s_addr)
240 tp->tun_flags |= TUN_IASET;
242 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
243 if (si && si->sin_addr.s_addr)
244 tp->tun_flags |= TUN_DSTADDR;
253 * Process an ioctl request.
256 tunifioctl(ifp, cmd, data, cr)
262 struct ifreq *ifr = (struct ifreq *)data;
263 struct tun_softc *tp = ifp->if_softc;
270 ifs = (struct ifstat *)data;
272 sprintf(ifs->ascii + strlen(ifs->ascii),
273 "\tOpened by PID %d\n", tp->tun_pid);
276 error = tuninit(ifp);
277 TUNDEBUG("%s: address set, error=%d\n",
278 ifp->if_xname, error);
281 error = tuninit(ifp);
282 TUNDEBUG("%s destination address set, error=%d\n",
283 ifp->if_xname, error);
286 ifp->if_mtu = ifr->ifr_mtu;
287 TUNDEBUG("%s: mtu set\n",
302 * tunoutput - queue packets from higher level ready to put out.
305 tunoutput(ifp, m0, dst, rt)
308 struct sockaddr *dst;
311 struct tun_softc *tp = ifp->if_softc;
313 struct altq_pktattr pktattr;
315 TUNDEBUG ("%s: tunoutput\n", ifp->if_xname);
317 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
318 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
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 s = splimp(); /* spl on queue manipulation */
356 IF_DROP(&ifp->if_snd);
361 bcopy(dst, m0->m_data, dst->sa_len);
365 if (tp->tun_flags & TUN_IFHEAD) {
366 /* Prepend the address family */
367 M_PREPEND(m0, 4, MB_DONTWAIT);
369 /* if allocation failed drop packet */
371 s = splimp(); /* spl on queue manipulation */
372 IF_DROP(&ifp->if_snd);
377 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
380 if (dst->sa_family != AF_INET)
388 error = ifq_handoff(ifp, m0, &pktattr);
390 ifp->if_collisions++;
397 * the cdevsw interface is now pretty minimal.
400 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
403 struct tun_softc *tp = dev->si_drv1;
404 struct tuninfo *tunp;
408 tunp = (struct tuninfo *)data;
409 if (tunp->mtu < IF_MINMTU)
411 tp->tun_if.if_mtu = tunp->mtu;
412 tp->tun_if.if_type = tunp->type;
413 tp->tun_if.if_baudrate = tunp->baudrate;
416 tunp = (struct tuninfo *)data;
417 tunp->mtu = tp->tun_if.if_mtu;
418 tunp->type = tp->tun_if.if_type;
419 tunp->baudrate = tp->tun_if.if_baudrate;
422 tundebug = *(int *)data;
425 *(int *)data = tundebug;
429 tp->tun_flags |= TUN_LMODE;
430 tp->tun_flags &= ~TUN_IFHEAD;
432 tp->tun_flags &= ~TUN_LMODE;
436 tp->tun_flags |= TUN_IFHEAD;
437 tp->tun_flags &= ~TUN_LMODE;
439 tp->tun_flags &= ~TUN_IFHEAD;
442 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
445 /* deny this if UP */
446 if (tp->tun_if.if_flags & IFF_UP)
449 switch (*(int *)data & ~IFF_MULTICAST) {
450 case IFF_POINTOPOINT:
452 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
453 tp->tun_if.if_flags |= *(int *)data;
460 tp->tun_pid = curproc->p_pid;
466 tp->tun_flags |= TUN_ASYNC;
468 tp->tun_flags &= ~TUN_ASYNC;
472 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
475 mb = ifq_poll(&tp->tun_if.if_snd);
476 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
477 *(int *)data += mb->m_len;
483 return (fsetown(*(int *)data, &tp->tun_sigio));
486 *(int *)data = fgetown(tp->tun_sigio);
489 /* This is deprecated, FIOSETOWN should be used instead. */
491 return (fsetown(-(*(int *)data), &tp->tun_sigio));
493 /* This is deprecated, FIOGETOWN should be used instead. */
495 *(int *)data = -fgetown(tp->tun_sigio);
505 * The cdevsw read interface - reads a packet at a time, or at
506 * least as much of a packet as can be read.
509 tunread(dev, uio, flag)
514 struct tun_softc *tp = dev->si_drv1;
515 struct ifnet *ifp = &tp->tun_if;
519 TUNDEBUG ("%s: read\n", ifp->if_xname);
520 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
521 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
526 tp->tun_flags &= ~TUN_RWAIT;
530 m0 = ifq_dequeue(&ifp->if_snd);
532 if (flag & IO_NDELAY) {
536 tp->tun_flags |= TUN_RWAIT;
537 if((error = tsleep((caddr_t)tp, PCATCH,
538 "tunread", 0)) != 0) {
546 while (m0 && uio->uio_resid > 0 && error == 0) {
547 len = min(uio->uio_resid, m0->m_len);
549 error = uiomove(mtod(m0, caddr_t), len, uio);
554 TUNDEBUG("%s: Dropping mbuf\n", ifp->if_xname);
561 * the cdevsw write interface - an atomic write is a packet - or else!
564 tunwrite(dev, uio, flag)
569 struct tun_softc *tp = dev->si_drv1;
570 struct ifnet *ifp = &tp->tun_if;
571 struct mbuf *top, **mp, *m;
572 int error=0, tlen, mlen;
576 TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
578 if (uio->uio_resid == 0)
581 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
582 TUNDEBUG("%s: len=%d!\n", ifp->if_xname,
586 tlen = uio->uio_resid;
588 /* get a header mbuf */
589 MGETHDR(m, MB_DONTWAIT, MT_DATA);
596 while (error == 0 && uio->uio_resid > 0) {
597 m->m_len = min(mlen, uio->uio_resid);
598 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
601 if (uio->uio_resid > 0) {
602 MGET (m, MB_DONTWAIT, MT_DATA);
617 top->m_pkthdr.len = tlen;
618 top->m_pkthdr.rcvif = ifp;
621 if (tp->tun_flags & TUN_IFHEAD) {
623 * Conveniently, we already have a 4-byte address
624 * family prepended to our packet !
625 * Inconveniently, it's in the wrong byte order !
627 if ((top = m_pullup(top, sizeof(family))) == NULL)
629 *mtod(top, u_int32_t *) =
630 ntohl(*mtod(top, u_int32_t *));
631 bpf_mtap(ifp->if_bpf, top);
632 *mtod(top, u_int32_t *) =
633 htonl(*mtod(top, u_int32_t *));
636 * We need to prepend the address family as
639 static const uint32_t af = AF_INET;
641 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
645 if (tp->tun_flags & TUN_IFHEAD) {
646 if (top->m_len < sizeof(family) &&
647 (top = m_pullup(top, sizeof(family))) == NULL)
649 family = ntohl(*mtod(top, u_int32_t *));
650 m_adj(top, sizeof(family));
654 ifp->if_ibytes += top->m_pkthdr.len;
680 return (EAFNOSUPPORT);
683 netisr_dispatch(isr, top);
688 * tunpoll - the poll interface, this is only useful on reads
689 * really. The write detect always returns true, write never blocks
690 * anyway, it either accepts the packet or drops it.
693 tunpoll(dev_t dev, int events, struct thread *td)
696 struct tun_softc *tp = dev->si_drv1;
697 struct ifnet *ifp = &tp->tun_if;
701 TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
703 if (events & (POLLIN | POLLRDNORM)) {
704 if (!ifq_is_empty(&ifp->if_snd)) {
705 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
706 ifp->if_snd.ifq_len);
707 revents |= events & (POLLIN | POLLRDNORM);
709 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
710 selrecord(td, &tp->tun_rsel);
713 if (events & (POLLOUT | POLLWRNORM))
714 revents |= events & (POLLOUT | POLLWRNORM);
721 * Start packet transmission on the interface.
722 * when the interface queue is rate-limited by ALTQ,
723 * if_start is needed to drain packets from the queue in order
724 * to notify readers when outgoing packets become ready.
727 tunstart(struct ifnet *ifp)
729 struct tun_softc *tp = ifp->if_softc;
732 if (!ifq_is_enabled(&ifp->if_snd))
735 m = ifq_poll(&ifp->if_snd);
737 if (tp->tun_flags & TUN_RWAIT) {
738 tp->tun_flags &= ~TUN_RWAIT;
741 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
742 pgsigio(tp->tun_sigio, SIGIO, 0);
743 selwakeup(&tp->tun_rsel);