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.15 2004/06/02 14:42:59 eirikn 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, struct ucred *);
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,
101 tunattach(void *dummy)
103 cdevsw_add(&tun_cdevsw, 0, 0);
110 struct tun_softc *sc;
113 dev = make_dev(&tun_cdevsw, minor(dev),
114 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
116 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
117 bzero(sc, sizeof *sc);
118 sc->tun_flags = TUN_INITED;
121 if_initname(ifp, "tun", lminor(dev));
122 ifp->if_mtu = TUNMTU;
123 ifp->if_ioctl = tunifioctl;
124 ifp->if_output = tunoutput;
125 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
126 ifp->if_type = IFT_PPP;
127 ifp->if_snd.ifq_maxlen = ifqmaxlen;
130 bpfattach(ifp, DLT_NULL, sizeof(u_int));
135 * tunnel open - must be superuser & the device must be
139 tunopen(dev_t dev, int flag, int mode, struct thread *td)
142 struct tun_softc *tp;
145 KKASSERT(td->td_proc);
146 if ((error = suser(td)) != NULL)
154 if (tp->tun_flags & TUN_OPEN)
156 tp->tun_pid = td->td_proc->p_pid;
158 tp->tun_flags |= TUN_OPEN;
159 TUNDEBUG("%s: open\n", ifp->if_xname);
164 * tunclose - close the device - mark i/f down & delete
168 tunclose(dev_t dev, int foo, int bar, struct thread *td)
171 struct tun_softc *tp;
178 tp->tun_flags &= ~TUN_OPEN;
182 * junk all pending output
186 IF_DEQUEUE(&ifp->if_snd, m);
192 if (ifp->if_flags & IFF_UP) {
198 if (ifp->if_flags & IFF_RUNNING) {
202 /* find internet addresses and delete routes */
203 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
204 if (ifa->ifa_addr->sa_family == AF_INET)
205 rtinit(ifa, (int)RTM_DELETE,
206 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
207 ifp->if_flags &= ~IFF_RUNNING;
211 funsetown(tp->tun_sigio);
212 selwakeup(&tp->tun_rsel);
214 TUNDEBUG ("%s: closed\n", ifp->if_xname);
222 struct tun_softc *tp = ifp->if_softc;
226 TUNDEBUG("%s: tuninit\n", ifp->if_xname);
228 ifp->if_flags |= IFF_UP | IFF_RUNNING;
229 getmicrotime(&ifp->if_lastchange);
231 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
232 ifa = TAILQ_NEXT(ifa, ifa_link)) {
233 if (ifa->ifa_addr == NULL)
235 /* XXX: Should maybe return straight off? */
238 if (ifa->ifa_addr->sa_family == AF_INET) {
239 struct sockaddr_in *si;
241 si = (struct sockaddr_in *)ifa->ifa_addr;
242 if (si->sin_addr.s_addr)
243 tp->tun_flags |= TUN_IASET;
245 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
246 if (si && si->sin_addr.s_addr)
247 tp->tun_flags |= TUN_DSTADDR;
256 * Process an ioctl request.
259 tunifioctl(ifp, cmd, data, cr)
265 struct ifreq *ifr = (struct ifreq *)data;
266 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("%s: address set, error=%d\n",
281 ifp->if_xname, error);
284 error = tuninit(ifp);
285 TUNDEBUG("%s destination address set, error=%d\n",
286 ifp->if_xname, error);
289 ifp->if_mtu = ifr->ifr_mtu;
290 TUNDEBUG("%s: mtu set\n",
305 * tunoutput - queue packets from higher level ready to put out.
308 tunoutput(ifp, m0, dst, rt)
311 struct sockaddr *dst;
314 struct tun_softc *tp = ifp->if_softc;
317 TUNDEBUG ("%s: tunoutput\n", ifp->if_xname);
319 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
320 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
326 /* BPF write needs to be handled specially */
327 if (dst->sa_family == AF_UNSPEC) {
328 dst->sa_family = *(mtod(m0, int *));
329 m0->m_len -= sizeof(int);
330 m0->m_pkthdr.len -= sizeof(int);
331 m0->m_data += sizeof(int);
336 * We need to prepend the address family as
337 * a four byte field. Cons up a dummy header
338 * to pacify bpf. This is safe because bpf
339 * will only read from the mbuf (i.e., it won't
340 * try to free it or keep a pointer to it).
343 uint32_t af = dst->sa_family;
347 m.m_data = (char *)⁡
352 /* prepend sockaddr? this may abort if the mbuf allocation fails */
353 if (tp->tun_flags & TUN_LMODE) {
354 /* allocate space for sockaddr */
355 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
357 /* if allocation failed drop packet */
359 s = splimp(); /* spl on queue manipulation */
360 IF_DROP(&ifp->if_snd);
365 bcopy(dst, m0->m_data, dst->sa_len);
369 if (tp->tun_flags & TUN_IFHEAD) {
370 /* Prepend the address family */
371 M_PREPEND(m0, 4, MB_DONTWAIT);
373 /* if allocation failed drop packet */
375 s = splimp(); /* spl on queue manipulation */
376 IF_DROP(&ifp->if_snd);
381 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
384 if (dst->sa_family != AF_INET)
393 if (IF_QFULL(&ifp->if_snd)) {
394 IF_DROP(&ifp->if_snd);
397 ifp->if_collisions++;
400 ifp->if_obytes += m0->m_pkthdr.len;
401 IF_ENQUEUE(&ifp->if_snd, m0);
405 if (tp->tun_flags & TUN_RWAIT) {
406 tp->tun_flags &= ~TUN_RWAIT;
409 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
410 pgsigio(tp->tun_sigio, SIGIO, 0);
411 selwakeup(&tp->tun_rsel);
416 * the cdevsw interface is now pretty minimal.
419 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
422 struct tun_softc *tp = dev->si_drv1;
423 struct tuninfo *tunp;
427 tunp = (struct tuninfo *)data;
428 if (tunp->mtu < IF_MINMTU)
430 tp->tun_if.if_mtu = tunp->mtu;
431 tp->tun_if.if_type = tunp->type;
432 tp->tun_if.if_baudrate = tunp->baudrate;
435 tunp = (struct tuninfo *)data;
436 tunp->mtu = tp->tun_if.if_mtu;
437 tunp->type = tp->tun_if.if_type;
438 tunp->baudrate = tp->tun_if.if_baudrate;
441 tundebug = *(int *)data;
444 *(int *)data = tundebug;
448 tp->tun_flags |= TUN_LMODE;
449 tp->tun_flags &= ~TUN_IFHEAD;
451 tp->tun_flags &= ~TUN_LMODE;
455 tp->tun_flags |= TUN_IFHEAD;
456 tp->tun_flags &= ~TUN_LMODE;
458 tp->tun_flags &= ~TUN_IFHEAD;
461 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
464 /* deny this if UP */
465 if (tp->tun_if.if_flags & IFF_UP)
468 switch (*(int *)data & ~IFF_MULTICAST) {
469 case IFF_POINTOPOINT:
471 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
472 tp->tun_if.if_flags |= *(int *)data;
479 tp->tun_pid = curproc->p_pid;
485 tp->tun_flags |= TUN_ASYNC;
487 tp->tun_flags &= ~TUN_ASYNC;
491 if (tp->tun_if.if_snd.ifq_head) {
492 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
493 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
494 *(int *)data += mb->m_len;
500 return (fsetown(*(int *)data, &tp->tun_sigio));
503 *(int *)data = fgetown(tp->tun_sigio);
506 /* This is deprecated, FIOSETOWN should be used instead. */
508 return (fsetown(-(*(int *)data), &tp->tun_sigio));
510 /* This is deprecated, FIOGETOWN should be used instead. */
512 *(int *)data = -fgetown(tp->tun_sigio);
522 * The cdevsw read interface - reads a packet at a time, or at
523 * least as much of a packet as can be read.
526 tunread(dev, uio, flag)
531 struct tun_softc *tp = dev->si_drv1;
532 struct ifnet *ifp = &tp->tun_if;
536 TUNDEBUG ("%s: read\n", ifp->if_xname);
537 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
538 TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
543 tp->tun_flags &= ~TUN_RWAIT;
547 IF_DEQUEUE(&ifp->if_snd, m0);
549 if (flag & IO_NDELAY) {
553 tp->tun_flags |= TUN_RWAIT;
554 if((error = tsleep((caddr_t)tp, PCATCH,
555 "tunread", 0)) != 0) {
563 while (m0 && uio->uio_resid > 0 && error == 0) {
564 len = min(uio->uio_resid, m0->m_len);
566 error = uiomove(mtod(m0, caddr_t), len, uio);
571 TUNDEBUG("%s: Dropping mbuf\n", ifp->if_xname);
578 * the cdevsw write interface - an atomic write is a packet - or else!
581 tunwrite(dev, uio, flag)
586 struct tun_softc *tp = dev->si_drv1;
587 struct ifnet *ifp = &tp->tun_if;
588 struct mbuf *top, **mp, *m;
589 int error=0, tlen, mlen;
593 TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
595 if (uio->uio_resid == 0)
598 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
599 TUNDEBUG("%s: len=%d!\n", ifp->if_xname,
603 tlen = uio->uio_resid;
605 /* get a header mbuf */
606 MGETHDR(m, MB_DONTWAIT, MT_DATA);
613 while (error == 0 && uio->uio_resid > 0) {
614 m->m_len = min(mlen, uio->uio_resid);
615 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
618 if (uio->uio_resid > 0) {
619 MGET (m, MB_DONTWAIT, MT_DATA);
634 top->m_pkthdr.len = tlen;
635 top->m_pkthdr.rcvif = ifp;
638 if (tp->tun_flags & TUN_IFHEAD) {
640 * Conveniently, we already have a 4-byte address
641 * family prepended to our packet !
642 * Inconveniently, it's in the wrong byte order !
644 if ((top = m_pullup(top, sizeof(family))) == NULL)
646 *mtod(top, u_int32_t *) =
647 ntohl(*mtod(top, u_int32_t *));
649 *mtod(top, u_int32_t *) =
650 htonl(*mtod(top, u_int32_t *));
653 * We need to prepend the address family as
654 * a four byte field. Cons up a dummy header
655 * to pacify bpf. This is safe because bpf
656 * will only read from the mbuf (i.e., it won't
657 * try to free it or keep a pointer to it).
660 uint32_t af = AF_INET;
664 m.m_data = (char *)⁡
670 if (tp->tun_flags & TUN_IFHEAD) {
671 if (top->m_len < sizeof(family) &&
672 (top = m_pullup(top, sizeof(family))) == NULL)
674 family = ntohl(*mtod(top, u_int32_t *));
675 m_adj(top, sizeof(family));
679 ifp->if_ibytes += top->m_pkthdr.len;
705 return (EAFNOSUPPORT);
708 netisr_dispatch(isr, top);
713 * tunpoll - the poll interface, this is only useful on reads
714 * really. The write detect always returns true, write never blocks
715 * anyway, it either accepts the packet or drops it.
718 tunpoll(dev_t dev, int events, struct thread *td)
721 struct tun_softc *tp = dev->si_drv1;
722 struct ifnet *ifp = &tp->tun_if;
726 TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
728 if (events & (POLLIN | POLLRDNORM)) {
729 if (ifp->if_snd.ifq_len > 0) {
730 TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
731 ifp->if_snd.ifq_len);
732 revents |= events & (POLLIN | POLLRDNORM);
734 TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
735 selrecord(td, &tp->tun_rsel);
738 if (events & (POLLOUT | POLLWRNORM))
739 revents |= events & (POLLOUT | POLLWRNORM);