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.11 2004/01/06 01:40:51 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/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/netisr.h>
46 #include <net/route.h>
49 #include <netinet/in.h>
54 #include "if_tunvar.h"
57 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
59 static void tunattach (void *);
60 PSEUDO_SET(tunattach, if_tun);
62 static void tuncreate (dev_t dev);
64 #define TUNDEBUG if (tundebug) printf
65 static int tundebug = 0;
66 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
68 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
70 static int tunifioctl (struct ifnet *, u_long, caddr_t);
71 static int tuninit (struct ifnet *);
73 static d_open_t tunopen;
74 static d_close_t tunclose;
75 static d_read_t tunread;
76 static d_write_t tunwrite;
77 static d_ioctl_t tunioctl;
78 static d_poll_t tunpoll;
81 static struct cdevsw tun_cdevsw = {
95 /* strategy */ nostrategy,
105 cdevsw_add(&tun_cdevsw);
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_flags = IFF_POINTOPOINT | IFF_MULTICAST;
128 ifp->if_type = IFT_PPP;
129 ifp->if_snd.ifq_maxlen = ifqmaxlen;
132 bpfattach(ifp, DLT_NULL, sizeof(u_int));
137 * tunnel open - must be superuser & the device must be
141 tunopen(dev_t dev, int flag, int mode, struct thread *td)
144 struct tun_softc *tp;
147 KKASSERT(td->td_proc);
148 if ((error = suser(td)) != NULL)
156 if (tp->tun_flags & TUN_OPEN)
158 tp->tun_pid = td->td_proc->p_pid;
160 tp->tun_flags |= TUN_OPEN;
161 TUNDEBUG("%s: open\n", ifp->if_xname);
166 * tunclose - close the device - mark i/f down & delete
170 tunclose(dev_t dev, int foo, int bar, struct thread *td)
173 struct tun_softc *tp;
180 tp->tun_flags &= ~TUN_OPEN;
184 * junk all pending output
188 IF_DEQUEUE(&ifp->if_snd, m);
194 if (ifp->if_flags & IFF_UP) {
200 if (ifp->if_flags & IFF_RUNNING) {
204 /* find internet addresses and delete routes */
205 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
206 if (ifa->ifa_addr->sa_family == AF_INET)
207 rtinit(ifa, (int)RTM_DELETE,
208 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
209 ifp->if_flags &= ~IFF_RUNNING;
213 funsetown(tp->tun_sigio);
214 selwakeup(&tp->tun_rsel);
216 TUNDEBUG ("%s: closed\n", ifp->if_xname);
224 struct tun_softc *tp = ifp->if_softc;
228 TUNDEBUG("%s: tuninit\n", ifp->if_xname);
230 ifp->if_flags |= IFF_UP | IFF_RUNNING;
231 getmicrotime(&ifp->if_lastchange);
233 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
234 ifa = TAILQ_NEXT(ifa, ifa_link)) {
235 if (ifa->ifa_addr == NULL)
237 /* XXX: Should maybe return straight off? */
240 if (ifa->ifa_addr->sa_family == AF_INET) {
241 struct sockaddr_in *si;
243 si = (struct sockaddr_in *)ifa->ifa_addr;
244 if (si->sin_addr.s_addr)
245 tp->tun_flags |= TUN_IASET;
247 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
248 if (si && si->sin_addr.s_addr)
249 tp->tun_flags |= TUN_DSTADDR;
258 * Process an ioctl request.
261 tunifioctl(ifp, cmd, data)
266 struct ifreq *ifr = (struct ifreq *)data;
267 struct tun_softc *tp = ifp->if_softc;
274 ifs = (struct ifstat *)data;
276 sprintf(ifs->ascii + strlen(ifs->ascii),
277 "\tOpened by PID %d\n", tp->tun_pid);
280 error = tuninit(ifp);
281 TUNDEBUG("%s: address set, error=%d\n",
282 ifp->if_xname, error);
285 error = tuninit(ifp);
286 TUNDEBUG("%s destination address set, error=%d\n",
287 ifp->if_xname, error);
290 ifp->if_mtu = ifr->ifr_mtu;
291 TUNDEBUG("%s: mtu set\n",
306 * tunoutput - queue packets from higher level ready to put out.
309 tunoutput(ifp, m0, dst, rt)
312 struct sockaddr *dst;
315 struct tun_softc *tp = ifp->if_softc;
318 TUNDEBUG ("%s: tunoutput\n", ifp->if_xname);
320 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
321 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
327 /* BPF write needs to be handled specially */
328 if (dst->sa_family == AF_UNSPEC) {
329 dst->sa_family = *(mtod(m0, int *));
330 m0->m_len -= sizeof(int);
331 m0->m_pkthdr.len -= sizeof(int);
332 m0->m_data += sizeof(int);
337 * We need to prepend the address family as
338 * a four byte field. Cons up a dummy header
339 * to pacify bpf. This is safe because bpf
340 * will only read from the mbuf (i.e., it won't
341 * try to free it or keep a pointer to it).
344 uint32_t af = dst->sa_family;
348 m.m_data = (char *)⁡
353 /* prepend sockaddr? this may abort if the mbuf allocation fails */
354 if (tp->tun_flags & TUN_LMODE) {
355 /* allocate space for sockaddr */
356 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
358 /* if allocation failed drop packet */
360 s = splimp(); /* spl on queue manipulation */
361 IF_DROP(&ifp->if_snd);
366 bcopy(dst, m0->m_data, dst->sa_len);
370 if (tp->tun_flags & TUN_IFHEAD) {
371 /* Prepend the address family */
372 M_PREPEND(m0, 4, M_DONTWAIT);
374 /* if allocation failed drop packet */
376 s = splimp(); /* spl on queue manipulation */
377 IF_DROP(&ifp->if_snd);
382 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
385 if (dst->sa_family != AF_INET)
394 if (IF_QFULL(&ifp->if_snd)) {
395 IF_DROP(&ifp->if_snd);
398 ifp->if_collisions++;
401 ifp->if_obytes += m0->m_pkthdr.len;
402 IF_ENQUEUE(&ifp->if_snd, m0);
406 if (tp->tun_flags & TUN_RWAIT) {
407 tp->tun_flags &= ~TUN_RWAIT;
410 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
411 pgsigio(tp->tun_sigio, SIGIO, 0);
412 selwakeup(&tp->tun_rsel);
417 * the cdevsw interface is now pretty minimal.
420 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
423 struct tun_softc *tp = dev->si_drv1;
424 struct tuninfo *tunp;
428 tunp = (struct tuninfo *)data;
429 if (tunp->mtu < IF_MINMTU)
431 tp->tun_if.if_mtu = tunp->mtu;
432 tp->tun_if.if_type = tunp->type;
433 tp->tun_if.if_baudrate = tunp->baudrate;
436 tunp = (struct tuninfo *)data;
437 tunp->mtu = tp->tun_if.if_mtu;
438 tunp->type = tp->tun_if.if_type;
439 tunp->baudrate = tp->tun_if.if_baudrate;
442 tundebug = *(int *)data;
445 *(int *)data = tundebug;
449 tp->tun_flags |= TUN_LMODE;
450 tp->tun_flags &= ~TUN_IFHEAD;
452 tp->tun_flags &= ~TUN_LMODE;
456 tp->tun_flags |= TUN_IFHEAD;
457 tp->tun_flags &= ~TUN_LMODE;
459 tp->tun_flags &= ~TUN_IFHEAD;
462 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
465 /* deny this if UP */
466 if (tp->tun_if.if_flags & IFF_UP)
469 switch (*(int *)data & ~IFF_MULTICAST) {
470 case IFF_POINTOPOINT:
472 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
473 tp->tun_if.if_flags |= *(int *)data;
480 tp->tun_pid = curproc->p_pid;
486 tp->tun_flags |= TUN_ASYNC;
488 tp->tun_flags &= ~TUN_ASYNC;
492 if (tp->tun_if.if_snd.ifq_head) {
493 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
494 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
495 *(int *)data += mb->m_len;
501 return (fsetown(*(int *)data, &tp->tun_sigio));
504 *(int *)data = fgetown(tp->tun_sigio);
507 /* This is deprecated, FIOSETOWN should be used instead. */
509 return (fsetown(-(*(int *)data), &tp->tun_sigio));
511 /* This is deprecated, FIOGETOWN should be used instead. */
513 *(int *)data = -fgetown(tp->tun_sigio);
523 * The cdevsw read interface - reads a packet at a time, or at
524 * least as much of a packet as can be read.
527 tunread(dev, uio, flag)
532 struct tun_softc *tp = dev->si_drv1;
533 struct ifnet *ifp = &tp->tun_if;
537 TUNDEBUG ("%s: read\n", ifp->if_xname);
538 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
539 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
544 tp->tun_flags &= ~TUN_RWAIT;
548 IF_DEQUEUE(&ifp->if_snd, m0);
550 if (flag & IO_NDELAY) {
554 tp->tun_flags |= TUN_RWAIT;
555 if((error = tsleep((caddr_t)tp, PCATCH,
556 "tunread", 0)) != 0) {
564 while (m0 && uio->uio_resid > 0 && error == 0) {
565 len = min(uio->uio_resid, m0->m_len);
567 error = uiomove(mtod(m0, caddr_t), len, uio);
572 TUNDEBUG("%s: Dropping mbuf\n", ifp->if_xname);
579 * the cdevsw write interface - an atomic write is a packet - or else!
582 tunwrite(dev, uio, flag)
587 struct tun_softc *tp = dev->si_drv1;
588 struct ifnet *ifp = &tp->tun_if;
589 struct mbuf *top, **mp, *m;
590 int error=0, tlen, mlen;
594 TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
596 if (uio->uio_resid == 0)
599 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
600 TUNDEBUG("%s: len=%d!\n", ifp->if_xname,
604 tlen = uio->uio_resid;
606 /* get a header mbuf */
607 MGETHDR(m, M_DONTWAIT, MT_DATA);
614 while (error == 0 && uio->uio_resid > 0) {
615 m->m_len = min(mlen, uio->uio_resid);
616 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
619 if (uio->uio_resid > 0) {
620 MGET (m, M_DONTWAIT, MT_DATA);
635 top->m_pkthdr.len = tlen;
636 top->m_pkthdr.rcvif = ifp;
639 if (tp->tun_flags & TUN_IFHEAD) {
641 * Conveniently, we already have a 4-byte address
642 * family prepended to our packet !
643 * Inconveniently, it's in the wrong byte order !
645 if ((top = m_pullup(top, sizeof(family))) == NULL)
647 *mtod(top, u_int32_t *) =
648 ntohl(*mtod(top, u_int32_t *));
650 *mtod(top, u_int32_t *) =
651 htonl(*mtod(top, u_int32_t *));
654 * We need to prepend the address family as
655 * a four byte field. Cons up a dummy header
656 * to pacify bpf. This is safe because bpf
657 * will only read from the mbuf (i.e., it won't
658 * try to free it or keep a pointer to it).
661 uint32_t af = AF_INET;
665 m.m_data = (char *)⁡
671 if (tp->tun_flags & TUN_IFHEAD) {
672 if (top->m_len < sizeof(family) &&
673 (top = m_pullup(top, sizeof(family))) == NULL)
675 family = ntohl(*mtod(top, u_int32_t *));
676 m_adj(top, sizeof(family));
680 ifp->if_ibytes += top->m_pkthdr.len;
706 return (EAFNOSUPPORT);
709 netisr_dispatch(isr, top);
714 * tunpoll - the poll interface, this is only useful on reads
715 * really. The write detect always returns true, write never blocks
716 * anyway, it either accepts the packet or drops it.
719 tunpoll(dev_t dev, int events, struct thread *td)
722 struct tun_softc *tp = dev->si_drv1;
723 struct ifnet *ifp = &tp->tun_if;
727 TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
729 if (events & (POLLIN | POLLRDNORM)) {
730 if (ifp->if_snd.ifq_len > 0) {
731 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
732 ifp->if_snd.ifq_len);
733 revents |= events & (POLLIN | POLLRDNORM);
735 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
736 selrecord(td, &tp->tun_rsel);
739 if (events & (POLLOUT | POLLWRNORM))
740 revents |= events & (POLLOUT | POLLWRNORM);