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.28 2006/06/13 08:12:03 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);
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);
133 if_attach(ifp, NULL);
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(ifp, "open\n");
168 * tunclose - close the device - mark i/f down & delete
172 tunclose(dev_t dev, int foo, int bar, struct thread *td)
174 struct tun_softc *tp;
180 tp->tun_flags &= ~TUN_OPEN;
183 /* Junk all pending output. */
184 lwkt_serialize_enter(ifp->if_serializer);
185 ifq_purge(&ifp->if_snd);
186 lwkt_serialize_exit(ifp->if_serializer);
188 if (ifp->if_flags & IFF_UP) {
189 lwkt_serialize_enter(ifp->if_serializer);
191 lwkt_serialize_exit(ifp->if_serializer);
194 if (ifp->if_flags & IFF_RUNNING) {
197 lwkt_serialize_enter(ifp->if_serializer);
198 /* find internet addresses and delete routes */
199 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
200 if (ifa->ifa_addr->sa_family == AF_INET) {
201 rtinit(ifa, (int)RTM_DELETE,
202 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
205 ifp->if_flags &= ~IFF_RUNNING;
206 lwkt_serialize_exit(ifp->if_serializer);
209 funsetown(tp->tun_sigio);
210 selwakeup(&tp->tun_rsel);
212 TUNDEBUG(ifp, "closed\n");
217 tuninit(struct ifnet *ifp)
219 struct tun_softc *tp = ifp->if_softc;
223 TUNDEBUG(ifp, "tuninit\n");
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.
258 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
260 struct ifreq *ifr = (struct ifreq *)data;
261 struct tun_softc *tp = ifp->if_softc;
267 ifs = (struct ifstat *)data;
269 sprintf(ifs->ascii + strlen(ifs->ascii),
270 "\tOpened by PID %d\n", tp->tun_pid);
273 error = tuninit(ifp);
274 TUNDEBUG(ifp, "address set, error=%d\n", error);
277 error = tuninit(ifp);
278 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
281 ifp->if_mtu = ifr->ifr_mtu;
282 TUNDEBUG(ifp, "mtu set\n");
295 * tunoutput - queue packets from higher level ready to put out.
300 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
303 struct tun_softc *tp = ifp->if_softc;
305 struct altq_pktattr pktattr;
307 TUNDEBUG(ifp, "tunoutput\n");
309 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
310 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
316 * if the queueing discipline needs packet classification,
317 * do it before prepending link headers.
319 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
321 /* BPF write needs to be handled specially */
322 if (dst->sa_family == AF_UNSPEC) {
323 dst->sa_family = *(mtod(m0, int *));
324 m0->m_len -= sizeof(int);
325 m0->m_pkthdr.len -= sizeof(int);
326 m0->m_data += sizeof(int);
331 * We need to prepend the address family as
334 uint32_t af = dst->sa_family;
336 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
339 /* prepend sockaddr? this may abort if the mbuf allocation fails */
340 if (tp->tun_flags & TUN_LMODE) {
341 /* allocate space for sockaddr */
342 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
344 /* if allocation failed drop packet */
346 IF_DROP(&ifp->if_snd);
350 bcopy(dst, m0->m_data, dst->sa_len);
354 if (tp->tun_flags & TUN_IFHEAD) {
355 /* Prepend the address family */
356 M_PREPEND(m0, 4, MB_DONTWAIT);
358 /* if allocation failed drop packet */
360 IF_DROP(&ifp->if_snd);
364 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
367 if (dst->sa_family != AF_INET)
375 error = ifq_handoff(ifp, m0, &pktattr);
377 ifp->if_collisions++;
380 if (tp->tun_flags & TUN_RWAIT) {
381 tp->tun_flags &= ~TUN_RWAIT;
385 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
386 pgsigio(tp->tun_sigio, SIGIO, 0);
387 selwakeup(&tp->tun_rsel);
394 * the cdevsw interface is now pretty minimal.
397 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
399 struct tun_softc *tp = dev->si_drv1;
400 struct tuninfo *tunp;
404 tunp = (struct tuninfo *)data;
405 if (tunp->mtu < IF_MINMTU)
407 tp->tun_if.if_mtu = tunp->mtu;
408 tp->tun_if.if_type = tunp->type;
409 tp->tun_if.if_baudrate = tunp->baudrate;
412 tunp = (struct tuninfo *)data;
413 tunp->mtu = tp->tun_if.if_mtu;
414 tunp->type = tp->tun_if.if_type;
415 tunp->baudrate = tp->tun_if.if_baudrate;
418 tundebug = *(int *)data;
421 *(int *)data = tundebug;
425 tp->tun_flags |= TUN_LMODE;
426 tp->tun_flags &= ~TUN_IFHEAD;
428 tp->tun_flags &= ~TUN_LMODE;
432 tp->tun_flags |= TUN_IFHEAD;
433 tp->tun_flags &= ~TUN_LMODE;
435 tp->tun_flags &= ~TUN_IFHEAD;
438 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
441 /* deny this if UP */
442 if (tp->tun_if.if_flags & IFF_UP)
445 switch (*(int *)data & ~IFF_MULTICAST) {
446 case IFF_POINTOPOINT:
448 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
449 tp->tun_if.if_flags |= *(int *)data;
456 tp->tun_pid = curproc->p_pid;
460 tp->tun_flags |= TUN_ASYNC;
462 tp->tun_flags &= ~TUN_ASYNC;
465 lwkt_serialize_enter(tp->tun_if.if_serializer);
466 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
469 mb = ifq_poll(&tp->tun_if.if_snd);
470 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
471 *(int *)data += mb->m_len;
475 lwkt_serialize_exit(tp->tun_if.if_serializer);
478 return (fsetown(*(int *)data, &tp->tun_sigio));
481 *(int *)data = fgetown(tp->tun_sigio);
484 /* This is deprecated, FIOSETOWN should be used instead. */
486 return (fsetown(-(*(int *)data), &tp->tun_sigio));
488 /* This is deprecated, FIOGETOWN should be used instead. */
490 *(int *)data = -fgetown(tp->tun_sigio);
500 * The cdevsw read interface - reads a packet at a time, or at
501 * least as much of a packet as can be read.
504 tunread(dev_t dev, struct uio *uio, int flag)
506 struct tun_softc *tp = dev->si_drv1;
507 struct ifnet *ifp = &tp->tun_if;
511 TUNDEBUG(ifp, "read\n");
512 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
513 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
517 tp->tun_flags &= ~TUN_RWAIT;
519 lwkt_serialize_enter(ifp->if_serializer);
521 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
522 if (flag & IO_NDELAY) {
523 lwkt_serialize_exit(ifp->if_serializer);
526 tp->tun_flags |= TUN_RWAIT;
527 lwkt_serialize_exit(ifp->if_serializer);
528 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
530 lwkt_serialize_enter(ifp->if_serializer);
533 lwkt_serialize_exit(ifp->if_serializer);
535 while (m0 && uio->uio_resid > 0 && error == 0) {
536 len = min(uio->uio_resid, m0->m_len);
538 error = uiomove(mtod(m0, caddr_t), len, uio);
543 TUNDEBUG(ifp, "Dropping mbuf\n");
550 * the cdevsw write interface - an atomic write is a packet - or else!
553 tunwrite(dev_t dev, struct uio *uio, int flag)
555 struct tun_softc *tp = dev->si_drv1;
556 struct ifnet *ifp = &tp->tun_if;
557 struct mbuf *top, **mp, *m;
558 int error=0, tlen, mlen;
562 TUNDEBUG(ifp, "tunwrite\n");
564 if (uio->uio_resid == 0)
567 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
568 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
571 tlen = uio->uio_resid;
573 /* get a header mbuf */
574 MGETHDR(m, MB_DONTWAIT, MT_DATA);
581 while (error == 0 && uio->uio_resid > 0) {
582 m->m_len = min(mlen, uio->uio_resid);
583 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
586 if (uio->uio_resid > 0) {
587 MGET (m, MB_DONTWAIT, MT_DATA);
602 top->m_pkthdr.len = tlen;
603 top->m_pkthdr.rcvif = ifp;
606 if (tp->tun_flags & TUN_IFHEAD) {
608 * Conveniently, we already have a 4-byte address
609 * family prepended to our packet !
610 * Inconveniently, it's in the wrong byte order !
612 if ((top = m_pullup(top, sizeof(family))) == NULL)
614 *mtod(top, u_int32_t *) =
615 ntohl(*mtod(top, u_int32_t *));
616 bpf_mtap(ifp->if_bpf, top);
617 *mtod(top, u_int32_t *) =
618 htonl(*mtod(top, u_int32_t *));
621 * We need to prepend the address family as
624 static const uint32_t af = AF_INET;
626 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
630 if (tp->tun_flags & TUN_IFHEAD) {
631 if (top->m_len < sizeof(family) &&
632 (top = m_pullup(top, sizeof(family))) == NULL)
634 family = ntohl(*mtod(top, u_int32_t *));
635 m_adj(top, sizeof(family));
639 ifp->if_ibytes += top->m_pkthdr.len;
665 return (EAFNOSUPPORT);
668 netisr_dispatch(isr, top);
673 * tunpoll - the poll interface, this is only useful on reads
674 * really. The write detect always returns true, write never blocks
675 * anyway, it either accepts the packet or drops it.
678 tunpoll(dev_t dev, int events, struct thread *td)
680 struct tun_softc *tp = dev->si_drv1;
681 struct ifnet *ifp = &tp->tun_if;
684 TUNDEBUG(ifp, "tunpoll\n");
686 lwkt_serialize_enter(ifp->if_serializer);
688 if (events & (POLLIN | POLLRDNORM)) {
689 if (!ifq_is_empty(&ifp->if_snd)) {
690 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
691 revents |= events & (POLLIN | POLLRDNORM);
693 TUNDEBUG(ifp, "tunpoll waiting\n");
694 selrecord(td, &tp->tun_rsel);
697 if (events & (POLLOUT | POLLWRNORM))
698 revents |= events & (POLLOUT | POLLWRNORM);
700 lwkt_serialize_exit(ifp->if_serializer);
706 * Start packet transmission on the interface.
707 * when the interface queue is rate-limited by ALTQ,
708 * if_start is needed to drain packets from the queue in order
709 * to notify readers when outgoing packets become ready.
712 tunstart(struct ifnet *ifp)
714 struct tun_softc *tp = ifp->if_softc;
717 if (!ifq_is_enabled(&ifp->if_snd))
720 m = ifq_poll(&ifp->if_snd);
722 if (tp->tun_flags & TUN_RWAIT) {
723 tp->tun_flags &= ~TUN_RWAIT;
726 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
727 pgsigio(tp->tun_sigio, SIGIO, 0);
728 selwakeup(&tp->tun_rsel);