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
21 #include <sys/param.h>
23 #include <sys/systm.h>
25 #include <sys/module.h>
26 #include <sys/socket.h>
27 #include <sys/filio.h>
28 #include <sys/sockio.h>
29 #include <sys/ttycom.h>
31 #include <sys/signalvar.h>
32 #include <sys/filedesc.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
37 #include <sys/vnode.h>
38 #include <sys/malloc.h>
39 #include <machine/bus.h> /* XXX Shouldn't really be required ! */
43 #include <net/if_types.h>
44 #include <net/route.h>
45 #include <net/intrq.h>
47 #include <netinet/in.h>
50 #include <net/if_tunvar.h>
51 #include <net/if_tun.h>
53 #define TUNDEBUG if (tundebug) printf
55 #define TUN_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
57 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
58 static int tundebug = 0;
59 static struct tun_softc *tunhead = NULL;
60 static struct rman tununits[1];
61 static udev_t tunbasedev = NOUDEV;
62 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
64 static void tunclone(void *arg, char *name, int namelen, dev_t *dev);
65 static void tuncreate(dev_t dev);
66 static int tunifioctl(struct ifnet *, u_long, caddr_t);
67 static int tuninit(struct ifnet *);
68 static int tunmodevent(module_t, int, void *);
69 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
71 static void tunstart(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 = {
89 /* strategy */ nostrategy,
98 tunclone(void *arg, char *name, int namelen, dev_t *dev)
107 if (strcmp(name, TUNNAME) == 0) {
108 r = rman_reserve_resource(tununits, 0, TUN_MAXUNIT, 1,
109 RF_ALLOCATED | RF_ACTIVE, NULL);
110 u = rman_get_start(r);
111 err = rman_release_resource(r);
112 KASSERT(err == 0, ("Unexpected failure releasing resource"));
113 *dev = makedev(CDEV_MAJOR, unit2minor(u));
114 if ((*dev)->si_flags & SI_NAMED)
115 return; /* Already make_dev()d */
116 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
117 return; /* Don't recognise the name */
119 *dev = make_dev(&tun_cdevsw, unit2minor(u),
120 UID_ROOT, GID_WHEEL, 0600, "tun%d", u);
123 * All devices depend on tunbasedev so that we can simply
124 * destroy_dev() this device at module unload time to get
125 * rid of all our make_dev()d resources.
127 if (tunbasedev == NOUDEV)
128 tunbasedev = (*dev)->si_udev;
130 (*dev)->si_flags |= SI_CHEAPCLONE;
131 dev_depends(udev2dev(tunbasedev, 0), *dev);
136 tunmodevent(module_t mod, int type, void *data)
138 static eventhandler_tag tag;
139 struct tun_softc *tp;
145 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
148 if (!devfs_present) {
149 err = cdevsw_add(&tun_cdevsw);
151 EVENTHANDLER_DEREGISTER(dev_clone, tag);
155 tununits->rm_type = RMAN_ARRAY;
156 tununits->rm_descr = "open if_tun units";
157 err = rman_init(tununits);
159 cdevsw_remove(&tun_cdevsw);
160 EVENTHANDLER_DEREGISTER(dev_clone, tag);
163 err = rman_manage_region(tununits, 0, TUN_MAXUNIT);
165 printf("%s: tununits: rman_manage_region: Failed %d\n",
168 cdevsw_remove(&tun_cdevsw);
169 EVENTHANDLER_DEREGISTER(dev_clone, tag);
174 err = rman_fini(tununits);
177 EVENTHANDLER_DEREGISTER(dev_clone, tag);
179 while (tunhead != NULL) {
180 KASSERT((tunhead->tun_flags & TUN_OPEN) == 0,
181 ("tununits is out of sync - unit %d",
182 tunhead->tun_if.if_unit));
184 dev = makedev(tun_cdevsw.d_maj,
185 unit2minor(tp->tun_if.if_unit));
186 KASSERT(dev->si_drv1 == tp, ("Bad makedev result"));
188 bpfdetach(&tp->tun_if);
189 if_detach(&tp->tun_if);
190 KASSERT(dev->si_flags & SI_NAMED, ("Missing make_dev"));
195 * Destroying tunbasedev results in all of our make_dev()s
196 * conveniently going away.
198 if (tunbasedev != NOUDEV)
199 destroy_dev(udev2dev(tunbasedev, 0));
202 cdevsw_remove(&tun_cdevsw);
208 static moduledata_t tun_mod = {
214 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
217 tunstart(struct ifnet *ifp)
219 struct tun_softc *tp = ifp->if_softc;
221 if (tp->tun_flags & TUN_RWAIT) {
222 tp->tun_flags &= ~TUN_RWAIT;
225 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
226 pgsigio(&tp->tun_sigio, SIGIO, 0);
227 selwakeup(&tp->tun_rsel);
233 struct tun_softc *sc;
236 if (!(dev->si_flags & SI_NAMED))
237 dev = make_dev(&tun_cdevsw, minor(dev),
238 UID_UUCP, GID_DIALER, 0600, "tun%d", dev2unit(dev));
240 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
241 sc->tun_flags = TUN_INITED;
246 ifp->if_unit = dev2unit(dev);
247 ifp->if_name = TUNNAME;
248 ifp->if_mtu = TUNMTU;
249 ifp->if_ioctl = tunifioctl;
250 ifp->if_output = tunoutput;
251 ifp->if_start = tunstart;
252 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
253 ifp->if_type = IFT_PPP;
254 ifp->if_snd.ifq_maxlen = ifqmaxlen;
257 bpfattach(ifp, DLT_NULL, sizeof(u_int));
262 tunopen(dev_t dev, int flag, int mode, struct thread *td)
266 struct tun_softc *tp;
269 unit = dev2unit(dev);
270 if (unit > TUN_MAXUNIT)
273 r = rman_reserve_resource(tununits, unit, unit, 1,
274 RF_ALLOCATED | RF_ACTIVE, NULL);
278 dev->si_flags &= ~SI_CHEAPCLONE;
285 KASSERT(!(tp->tun_flags & TUN_OPEN), ("Resource & flags out-of-sync"));
287 tp->tun_pid = td->td_proc->p_pid;
289 tp->tun_flags |= TUN_OPEN;
290 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
296 * tunclose - close the device - mark i/f down & delete
300 tunclose(dev_t dev, int foo, int bar, struct thread *td)
302 struct tun_softc *tp;
310 KASSERT(tp->r_unit, ("Unit %d not marked open", ifp->if_unit));
311 tp->tun_flags &= ~TUN_OPEN;
315 * junk all pending output
317 IF_DRAIN(&ifp->if_snd);
319 if (ifp->if_flags & IFF_UP) {
325 if (ifp->if_flags & IFF_RUNNING) {
326 register struct ifaddr *ifa;
329 /* find internet addresses and delete routes */
330 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
331 if (ifa->ifa_addr->sa_family == AF_INET)
332 rtinit(ifa, (int)RTM_DELETE,
333 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
334 ifp->if_flags &= ~IFF_RUNNING;
338 funsetown(&tp->tun_sigio);
339 selwakeup(&tp->tun_rsel);
341 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
342 err = rman_release_resource(tp->r_unit);
343 KASSERT(err == 0, ("Unit %d failed to release", ifp->if_unit));
349 tuninit(struct ifnet *ifp)
351 struct tun_softc *tp = ifp->if_softc;
352 register struct ifaddr *ifa;
355 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
357 ifp->if_flags |= IFF_UP | IFF_RUNNING;
358 getmicrotime(&ifp->if_lastchange);
360 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
361 ifa = TAILQ_NEXT(ifa, ifa_link)) {
362 if (ifa->ifa_addr == NULL)
364 /* XXX: Should maybe return straight off? */
367 if (ifa->ifa_addr->sa_family == AF_INET) {
368 struct sockaddr_in *si;
370 si = (struct sockaddr_in *)ifa->ifa_addr;
371 if (si->sin_addr.s_addr)
372 tp->tun_flags |= TUN_IASET;
374 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
375 if (si && si->sin_addr.s_addr)
376 tp->tun_flags |= TUN_DSTADDR;
385 * Process an ioctl request.
388 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
390 struct ifreq *ifr = (struct ifreq *)data;
391 struct tun_softc *tp = ifp->if_softc;
398 ifs = (struct ifstat *)data;
400 sprintf(ifs->ascii + strlen(ifs->ascii),
401 "\tOpened by PID %d\n", tp->tun_pid);
404 error = tuninit(ifp);
405 TUNDEBUG("%s%d: address set, error=%d\n",
406 ifp->if_name, ifp->if_unit, error);
409 error = tuninit(ifp);
410 TUNDEBUG("%s%d: destination address set, error=%d\n",
411 ifp->if_name, ifp->if_unit, error);
414 ifp->if_mtu = ifr->ifr_mtu;
415 TUNDEBUG("%s%d: mtu set\n", ifp->if_name, ifp->if_unit);
429 * tunoutput - queue packets from higher level ready to put out.
435 struct sockaddr *dst,
438 struct tun_softc *tp = ifp->if_softc;
440 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
442 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
443 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
444 ifp->if_unit, tp->tun_flags);
449 /* BPF write needs to be handled specially */
450 if (dst->sa_family == AF_UNSPEC) {
451 dst->sa_family = *(mtod(m0, int *));
452 m0->m_len -= sizeof(int);
453 m0->m_pkthdr.len -= sizeof(int);
454 m0->m_data += sizeof(int);
459 * We need to prepend the address family as
460 * a four byte field. Cons up a dummy header
461 * to pacify bpf. This is safe because bpf
462 * will only read from the mbuf (i.e., it won't
463 * try to free it or keep a pointer to it).
466 uint32_t af = dst->sa_family;
470 m.m_data = (char *)⁡
475 /* prepend sockaddr? this may abort if the mbuf allocation fails */
476 if (tp->tun_flags & TUN_LMODE) {
477 /* allocate space for sockaddr */
478 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
480 /* if allocation failed drop packet */
486 bcopy(dst, m0->m_data, dst->sa_len);
490 if (tp->tun_flags & TUN_IFHEAD) {
491 /* Prepend the address family */
492 M_PREPEND(m0, 4, M_DONTWAIT);
494 /* if allocation failed drop packet */
500 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
503 if (dst->sa_family != AF_INET)
507 return (EAFNOSUPPORT);
511 if (! IF_HANDOFF(&ifp->if_snd, m0, ifp)) {
512 ifp->if_collisions++;
520 * the cdevsw interface is now pretty minimal.
523 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
527 struct tun_softc *tp = dev->si_drv1;
528 struct tuninfo *tunp;
532 tunp = (struct tuninfo *)data;
533 if (tunp->mtu < IF_MINMTU)
535 if (tp->tun_if.if_mtu != tunp->mtu
536 && (error = suser(td)) != 0)
538 tp->tun_if.if_mtu = tunp->mtu;
539 tp->tun_if.if_type = tunp->type;
540 tp->tun_if.if_baudrate = tunp->baudrate;
543 tunp = (struct tuninfo *)data;
544 tunp->mtu = tp->tun_if.if_mtu;
545 tunp->type = tp->tun_if.if_type;
546 tunp->baudrate = tp->tun_if.if_baudrate;
549 tundebug = *(int *)data;
552 *(int *)data = tundebug;
556 tp->tun_flags |= TUN_LMODE;
557 tp->tun_flags &= ~TUN_IFHEAD;
559 tp->tun_flags &= ~TUN_LMODE;
563 tp->tun_flags |= TUN_IFHEAD;
564 tp->tun_flags &= ~TUN_LMODE;
566 tp->tun_flags &= ~TUN_IFHEAD;
569 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
572 /* deny this if UP */
573 if (tp->tun_if.if_flags & IFF_UP)
576 switch (*(int *)data & ~IFF_MULTICAST) {
577 case IFF_POINTOPOINT:
579 tp->tun_if.if_flags &=
580 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
581 tp->tun_if.if_flags |= *(int *)data;
588 tp->tun_pid = curthread->td_proc->p_pid;
594 tp->tun_flags |= TUN_ASYNC;
596 tp->tun_flags &= ~TUN_ASYNC;
600 if (tp->tun_if.if_snd.ifq_head) {
601 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
602 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
603 *(int *)data += mb->m_len;
609 return (fsetown(*(int *)data, &tp->tun_sigio));
612 *(int *)data = fgetown(tp->tun_sigio);
615 /* This is deprecated, FIOSETOWN should be used instead. */
617 return (fsetown(-(*(int *)data), &tp->tun_sigio));
619 /* This is deprecated, FIOGETOWN should be used instead. */
621 *(int *)data = -fgetown(tp->tun_sigio);
631 * The cdevsw read interface - reads a packet at a time, or at
632 * least as much of a packet as can be read.
635 tunread(dev_t dev, struct uio *uio, int flag)
637 struct tun_softc *tp = dev->si_drv1;
638 struct ifnet *ifp = &tp->tun_if;
642 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
643 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
644 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
645 ifp->if_unit, tp->tun_flags);
649 tp->tun_flags &= ~TUN_RWAIT;
653 IF_DEQUEUE(&ifp->if_snd, m);
655 if (flag & IO_NDELAY) {
657 return (EWOULDBLOCK);
659 tp->tun_flags |= TUN_RWAIT;
660 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
661 "tunread", 0)) != 0) {
669 while (m && uio->uio_resid > 0 && error == 0) {
670 len = min(uio->uio_resid, m->m_len);
672 error = uiomove(mtod(m, caddr_t), len, uio);
677 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
684 * the cdevsw write interface - an atomic write is a packet - or else!
687 tunwrite(dev_t dev, struct uio *uio, int flag)
689 struct tun_softc *tp = dev->si_drv1;
690 struct ifnet *ifp = &tp->tun_if;
691 struct mbuf *top, **mp, *m;
692 int error=0, tlen, mlen;
695 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
697 if (uio->uio_resid == 0)
700 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
701 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
705 tlen = uio->uio_resid;
707 /* get a header mbuf */
708 MGETHDR(m, M_DONTWAIT, MT_DATA);
715 while (error == 0 && uio->uio_resid > 0) {
716 m->m_len = min(mlen, uio->uio_resid);
717 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
720 if (uio->uio_resid > 0) {
721 MGET (m, M_DONTWAIT, MT_DATA);
736 top->m_pkthdr.len = tlen;
737 top->m_pkthdr.rcvif = ifp;
740 if (tp->tun_flags & TUN_IFHEAD) {
742 * Conveniently, we already have a 4-byte address
743 * family prepended to our packet !
744 * Inconveniently, it's in the wrong byte order !
746 if ((top = m_pullup(top, sizeof(family))) == NULL)
748 *mtod(top, u_int32_t *) =
749 ntohl(*mtod(top, u_int32_t *));
751 *mtod(top, u_int32_t *) =
752 htonl(*mtod(top, u_int32_t *));
755 * We need to prepend the address family as
756 * a four byte field. Cons up a dummy header
757 * to pacify bpf. This is safe because bpf
758 * will only read from the mbuf (i.e., it won't
759 * try to free it or keep a pointer to it).
762 uint32_t af = AF_INET;
766 m.m_data = (char *)⁡
772 if (tp->tun_flags & TUN_IFHEAD) {
773 if (top->m_len < sizeof(family) &&
774 (top = m_pullup(top, sizeof(family))) == NULL)
776 family = ntohl(*mtod(top, u_int32_t *));
777 m_adj(top, sizeof(family));
781 ifp->if_ibytes += top->m_pkthdr.len;
784 return (family_enqueue(family, top));
788 * tunpoll - the poll interface, this is only useful on reads
789 * really. The write detect always returns true, write never blocks
790 * anyway, it either accepts the packet or drops it.
793 tunpoll(dev_t dev, int events, struct thread *td)
796 struct tun_softc *tp = dev->si_drv1;
797 struct ifnet *ifp = &tp->tun_if;
801 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
803 if (events & (POLLIN | POLLRDNORM)) {
804 if (ifp->if_snd.ifq_len > 0) {
805 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
806 ifp->if_unit, ifp->if_snd.ifq_len);
807 revents |= events & (POLLIN | POLLRDNORM);
809 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
811 selrecord(td, &tp->tun_rsel);
814 if (events & (POLLOUT | POLLWRNORM))
815 revents |= events & (POLLOUT | POLLWRNORM);