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.26 2005/12/01 21:15:54 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>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/thread2.h>
33 #include <sys/ttycom.h>
35 #include <sys/signalvar.h>
36 #include <sys/filedesc.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
45 #include <net/if_types.h>
46 #include <net/ifq_var.h>
47 #include <net/netisr.h>
48 #include <net/route.h>
51 #include <netinet/in.h>
56 #include "if_tunvar.h"
59 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
61 static void tunattach (void *);
62 PSEUDO_SET(tunattach, if_tun);
64 static void tuncreate (dev_t dev);
66 #define TUNDEBUG if (tundebug) if_printf
67 static int tundebug = 0;
68 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
70 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
72 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
73 static int tuninit (struct ifnet *);
74 static void tunstart(struct ifnet *);
76 static d_open_t tunopen;
77 static d_close_t tunclose;
78 static d_read_t tunread;
79 static d_write_t tunwrite;
80 static d_ioctl_t tunioctl;
81 static d_poll_t tunpoll;
84 static struct cdevsw tun_cdevsw = {
98 /* strategy */ nostrategy,
104 tunattach(void *dummy)
106 cdevsw_add(&tun_cdevsw, 0, 0);
113 struct tun_softc *sc;
116 dev = make_dev(&tun_cdevsw, minor(dev),
117 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
119 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
120 bzero(sc, sizeof *sc);
121 sc->tun_flags = TUN_INITED;
124 if_initname(ifp, "tun", lminor(dev));
125 ifp->if_mtu = TUNMTU;
126 ifp->if_ioctl = tunifioctl;
127 ifp->if_output = tunoutput;
128 ifp->if_start = tunstart;
129 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
130 ifp->if_type = IFT_PPP;
131 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
132 ifq_set_ready(&ifp->if_snd);
134 if_attach(ifp, NULL);
135 bpfattach(ifp, DLT_NULL, sizeof(u_int));
140 * tunnel open - must be superuser & the device must be
144 tunopen(dev_t dev, int flag, int mode, struct thread *td)
147 struct tun_softc *tp;
150 KKASSERT(td->td_proc);
151 if ((error = suser(td)) != NULL)
159 if (tp->tun_flags & TUN_OPEN)
161 tp->tun_pid = td->td_proc->p_pid;
163 tp->tun_flags |= TUN_OPEN;
164 TUNDEBUG(ifp, "open\n");
169 * tunclose - close the device - mark i/f down & delete
173 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. */
185 lwkt_serialize_enter(ifp->if_serializer);
186 ifq_purge(&ifp->if_snd);
187 lwkt_serialize_exit(ifp->if_serializer);
189 if (ifp->if_flags & IFF_UP) {
190 lwkt_serialize_enter(ifp->if_serializer);
192 lwkt_serialize_exit(ifp->if_serializer);
195 if (ifp->if_flags & IFF_RUNNING) {
198 lwkt_serialize_enter(ifp->if_serializer);
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);
206 ifp->if_flags &= ~IFF_RUNNING;
207 lwkt_serialize_exit(ifp->if_serializer);
210 funsetown(tp->tun_sigio);
211 selwakeup(&tp->tun_rsel);
213 TUNDEBUG(ifp, "closed\n");
221 struct tun_softc *tp = ifp->if_softc;
225 TUNDEBUG(ifp, "tuninit\n");
227 ifp->if_flags |= IFF_UP | IFF_RUNNING;
228 getmicrotime(&ifp->if_lastchange);
230 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
231 ifa = TAILQ_NEXT(ifa, ifa_link)) {
232 if (ifa->ifa_addr == NULL)
234 /* XXX: Should maybe return straight off? */
237 if (ifa->ifa_addr->sa_family == AF_INET) {
238 struct sockaddr_in *si;
240 si = (struct sockaddr_in *)ifa->ifa_addr;
241 if (si->sin_addr.s_addr)
242 tp->tun_flags |= TUN_IASET;
244 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
245 if (si && si->sin_addr.s_addr)
246 tp->tun_flags |= TUN_DSTADDR;
255 * Process an ioctl request.
260 tunifioctl(ifp, cmd, data, cr)
266 struct ifreq *ifr = (struct ifreq *)data;
267 struct tun_softc *tp = ifp->if_softc;
273 ifs = (struct ifstat *)data;
275 sprintf(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(ifp, m0, dst, rt)
309 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 * the cdevsw interface is now pretty minimal.
406 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
408 struct tun_softc *tp = dev->si_drv1;
409 struct tuninfo *tunp;
413 tunp = (struct tuninfo *)data;
414 if (tunp->mtu < IF_MINMTU)
416 tp->tun_if.if_mtu = tunp->mtu;
417 tp->tun_if.if_type = tunp->type;
418 tp->tun_if.if_baudrate = tunp->baudrate;
421 tunp = (struct tuninfo *)data;
422 tunp->mtu = tp->tun_if.if_mtu;
423 tunp->type = tp->tun_if.if_type;
424 tunp->baudrate = tp->tun_if.if_baudrate;
427 tundebug = *(int *)data;
430 *(int *)data = tundebug;
434 tp->tun_flags |= TUN_LMODE;
435 tp->tun_flags &= ~TUN_IFHEAD;
437 tp->tun_flags &= ~TUN_LMODE;
441 tp->tun_flags |= TUN_IFHEAD;
442 tp->tun_flags &= ~TUN_LMODE;
444 tp->tun_flags &= ~TUN_IFHEAD;
447 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
450 /* deny this if UP */
451 if (tp->tun_if.if_flags & IFF_UP)
454 switch (*(int *)data & ~IFF_MULTICAST) {
455 case IFF_POINTOPOINT:
457 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
458 tp->tun_if.if_flags |= *(int *)data;
465 tp->tun_pid = curproc->p_pid;
471 tp->tun_flags |= TUN_ASYNC;
473 tp->tun_flags &= ~TUN_ASYNC;
476 lwkt_serialize_enter(tp->tun_if.if_serializer);
477 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
480 mb = ifq_poll(&tp->tun_if.if_snd);
481 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
482 *(int *)data += mb->m_len;
486 lwkt_serialize_exit(tp->tun_if.if_serializer);
489 return (fsetown(*(int *)data, &tp->tun_sigio));
492 *(int *)data = fgetown(tp->tun_sigio);
495 /* This is deprecated, FIOSETOWN should be used instead. */
497 return (fsetown(-(*(int *)data), &tp->tun_sigio));
499 /* This is deprecated, FIOGETOWN should be used instead. */
501 *(int *)data = -fgetown(tp->tun_sigio);
511 * The cdevsw read interface - reads a packet at a time, or at
512 * least as much of a packet as can be read.
515 tunread(dev, uio, flag)
520 struct tun_softc *tp = dev->si_drv1;
521 struct ifnet *ifp = &tp->tun_if;
525 TUNDEBUG(ifp, "read\n");
526 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
527 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
531 tp->tun_flags &= ~TUN_RWAIT;
533 lwkt_serialize_enter(ifp->if_serializer);
535 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
536 if (flag & IO_NDELAY) {
537 lwkt_serialize_exit(ifp->if_serializer);
540 tp->tun_flags |= TUN_RWAIT;
541 lwkt_serialize_exit(ifp->if_serializer);
542 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
544 lwkt_serialize_enter(ifp->if_serializer);
547 lwkt_serialize_exit(ifp->if_serializer);
549 while (m0 && uio->uio_resid > 0 && error == 0) {
550 len = min(uio->uio_resid, m0->m_len);
552 error = uiomove(mtod(m0, caddr_t), len, uio);
557 TUNDEBUG(ifp, "Dropping mbuf\n");
564 * the cdevsw write interface - an atomic write is a packet - or else!
567 tunwrite(dev, uio, flag)
572 struct tun_softc *tp = dev->si_drv1;
573 struct ifnet *ifp = &tp->tun_if;
574 struct mbuf *top, **mp, *m;
575 int error=0, tlen, mlen;
579 TUNDEBUG(ifp, "tunwrite\n");
581 if (uio->uio_resid == 0)
584 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
585 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
588 tlen = uio->uio_resid;
590 /* get a header mbuf */
591 MGETHDR(m, MB_DONTWAIT, MT_DATA);
598 while (error == 0 && uio->uio_resid > 0) {
599 m->m_len = min(mlen, uio->uio_resid);
600 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
603 if (uio->uio_resid > 0) {
604 MGET (m, MB_DONTWAIT, MT_DATA);
619 top->m_pkthdr.len = tlen;
620 top->m_pkthdr.rcvif = ifp;
623 if (tp->tun_flags & TUN_IFHEAD) {
625 * Conveniently, we already have a 4-byte address
626 * family prepended to our packet !
627 * Inconveniently, it's in the wrong byte order !
629 if ((top = m_pullup(top, sizeof(family))) == NULL)
631 *mtod(top, u_int32_t *) =
632 ntohl(*mtod(top, u_int32_t *));
633 bpf_mtap(ifp->if_bpf, top);
634 *mtod(top, u_int32_t *) =
635 htonl(*mtod(top, u_int32_t *));
638 * We need to prepend the address family as
641 static const uint32_t af = AF_INET;
643 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
647 if (tp->tun_flags & TUN_IFHEAD) {
648 if (top->m_len < sizeof(family) &&
649 (top = m_pullup(top, sizeof(family))) == NULL)
651 family = ntohl(*mtod(top, u_int32_t *));
652 m_adj(top, sizeof(family));
656 ifp->if_ibytes += top->m_pkthdr.len;
682 return (EAFNOSUPPORT);
685 netisr_dispatch(isr, top);
690 * tunpoll - the poll interface, this is only useful on reads
691 * really. The write detect always returns true, write never blocks
692 * anyway, it either accepts the packet or drops it.
695 tunpoll(dev_t dev, int events, struct thread *td)
697 struct tun_softc *tp = dev->si_drv1;
698 struct ifnet *ifp = &tp->tun_if;
701 TUNDEBUG(ifp, "tunpoll\n");
703 lwkt_serialize_enter(ifp->if_serializer);
705 if (events & (POLLIN | POLLRDNORM)) {
706 if (!ifq_is_empty(&ifp->if_snd)) {
707 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
708 revents |= events & (POLLIN | POLLRDNORM);
710 TUNDEBUG(ifp, "tunpoll waiting\n");
711 selrecord(td, &tp->tun_rsel);
714 if (events & (POLLOUT | POLLWRNORM))
715 revents |= events & (POLLOUT | POLLWRNORM);
717 lwkt_serialize_exit(ifp->if_serializer);
723 * Start packet transmission on the interface.
724 * when the interface queue is rate-limited by ALTQ,
725 * if_start is needed to drain packets from the queue in order
726 * to notify readers when outgoing packets become ready.
729 tunstart(struct ifnet *ifp)
731 struct tun_softc *tp = ifp->if_softc;
734 if (!ifq_is_enabled(&ifp->if_snd))
737 m = ifq_poll(&ifp->if_snd);
739 if (tp->tun_flags & TUN_RWAIT) {
740 tp->tun_flags &= ~TUN_RWAIT;
743 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
744 pgsigio(tp->tun_sigio, SIGIO, 0);
745 selwakeup(&tp->tun_rsel);