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.27 2005/12/11 13:00:17 swildner 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;
462 tp->tun_flags |= TUN_ASYNC;
464 tp->tun_flags &= ~TUN_ASYNC;
467 lwkt_serialize_enter(tp->tun_if.if_serializer);
468 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
471 mb = ifq_poll(&tp->tun_if.if_snd);
472 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
473 *(int *)data += mb->m_len;
477 lwkt_serialize_exit(tp->tun_if.if_serializer);
480 return (fsetown(*(int *)data, &tp->tun_sigio));
483 *(int *)data = fgetown(tp->tun_sigio);
486 /* This is deprecated, FIOSETOWN should be used instead. */
488 return (fsetown(-(*(int *)data), &tp->tun_sigio));
490 /* This is deprecated, FIOGETOWN should be used instead. */
492 *(int *)data = -fgetown(tp->tun_sigio);
502 * The cdevsw read interface - reads a packet at a time, or at
503 * least as much of a packet as can be read.
506 tunread(dev_t dev, struct uio *uio, int flag)
508 struct tun_softc *tp = dev->si_drv1;
509 struct ifnet *ifp = &tp->tun_if;
513 TUNDEBUG(ifp, "read\n");
514 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
515 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
519 tp->tun_flags &= ~TUN_RWAIT;
521 lwkt_serialize_enter(ifp->if_serializer);
523 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
524 if (flag & IO_NDELAY) {
525 lwkt_serialize_exit(ifp->if_serializer);
528 tp->tun_flags |= TUN_RWAIT;
529 lwkt_serialize_exit(ifp->if_serializer);
530 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
532 lwkt_serialize_enter(ifp->if_serializer);
535 lwkt_serialize_exit(ifp->if_serializer);
537 while (m0 && uio->uio_resid > 0 && error == 0) {
538 len = min(uio->uio_resid, m0->m_len);
540 error = uiomove(mtod(m0, caddr_t), len, uio);
545 TUNDEBUG(ifp, "Dropping mbuf\n");
552 * the cdevsw write interface - an atomic write is a packet - or else!
555 tunwrite(dev_t dev, struct uio *uio, int flag)
557 struct tun_softc *tp = dev->si_drv1;
558 struct ifnet *ifp = &tp->tun_if;
559 struct mbuf *top, **mp, *m;
560 int error=0, tlen, mlen;
564 TUNDEBUG(ifp, "tunwrite\n");
566 if (uio->uio_resid == 0)
569 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
570 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
573 tlen = uio->uio_resid;
575 /* get a header mbuf */
576 MGETHDR(m, MB_DONTWAIT, MT_DATA);
583 while (error == 0 && uio->uio_resid > 0) {
584 m->m_len = min(mlen, uio->uio_resid);
585 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
588 if (uio->uio_resid > 0) {
589 MGET (m, MB_DONTWAIT, MT_DATA);
604 top->m_pkthdr.len = tlen;
605 top->m_pkthdr.rcvif = ifp;
608 if (tp->tun_flags & TUN_IFHEAD) {
610 * Conveniently, we already have a 4-byte address
611 * family prepended to our packet !
612 * Inconveniently, it's in the wrong byte order !
614 if ((top = m_pullup(top, sizeof(family))) == NULL)
616 *mtod(top, u_int32_t *) =
617 ntohl(*mtod(top, u_int32_t *));
618 bpf_mtap(ifp->if_bpf, top);
619 *mtod(top, u_int32_t *) =
620 htonl(*mtod(top, u_int32_t *));
623 * We need to prepend the address family as
626 static const uint32_t af = AF_INET;
628 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
632 if (tp->tun_flags & TUN_IFHEAD) {
633 if (top->m_len < sizeof(family) &&
634 (top = m_pullup(top, sizeof(family))) == NULL)
636 family = ntohl(*mtod(top, u_int32_t *));
637 m_adj(top, sizeof(family));
641 ifp->if_ibytes += top->m_pkthdr.len;
667 return (EAFNOSUPPORT);
670 netisr_dispatch(isr, top);
675 * tunpoll - the poll interface, this is only useful on reads
676 * really. The write detect always returns true, write never blocks
677 * anyway, it either accepts the packet or drops it.
680 tunpoll(dev_t dev, int events, struct thread *td)
682 struct tun_softc *tp = dev->si_drv1;
683 struct ifnet *ifp = &tp->tun_if;
686 TUNDEBUG(ifp, "tunpoll\n");
688 lwkt_serialize_enter(ifp->if_serializer);
690 if (events & (POLLIN | POLLRDNORM)) {
691 if (!ifq_is_empty(&ifp->if_snd)) {
692 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
693 revents |= events & (POLLIN | POLLRDNORM);
695 TUNDEBUG(ifp, "tunpoll waiting\n");
696 selrecord(td, &tp->tun_rsel);
699 if (events & (POLLOUT | POLLWRNORM))
700 revents |= events & (POLLOUT | POLLWRNORM);
702 lwkt_serialize_exit(ifp->if_serializer);
708 * Start packet transmission on the interface.
709 * when the interface queue is rate-limited by ALTQ,
710 * if_start is needed to drain packets from the queue in order
711 * to notify readers when outgoing packets become ready.
714 tunstart(struct ifnet *ifp)
716 struct tun_softc *tp = ifp->if_softc;
719 if (!ifq_is_enabled(&ifp->if_snd))
722 m = ifq_poll(&ifp->if_snd);
724 if (tp->tun_flags & TUN_RWAIT) {
725 tp->tun_flags &= ~TUN_RWAIT;
728 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
729 pgsigio(tp->tun_sigio, SIGIO, 0);
730 selwakeup(&tp->tun_rsel);