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
22 #include <sys/param.h>
24 #include <sys/systm.h>
27 #include <sys/module.h>
28 #include <sys/socket.h>
29 #include <sys/filio.h>
30 #include <sys/sockio.h>
31 #include <sys/ttycom.h>
33 #include <sys/signalvar.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
39 #include <sys/vnode.h>
40 #include <sys/malloc.h>
41 #include <machine/bus.h> /* XXX Shouldn't really be required ! */
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <net/intrq.h>
49 #include <netinet/in.h>
52 #include <net/if_tunvar.h>
53 #include <net/if_tun.h>
55 #define TUNDEBUG if (tundebug) printf
57 #define TUN_MAXUNIT 0x7fff /* ifp->if_unit is only 15 bits */
59 static MALLOC_DEFINE(M_TUN, TUNNAME, "Tunnel Interface");
60 static int tundebug = 0;
61 static struct tun_softc *tunhead = NULL;
62 static struct rman tununits[1];
63 static udev_t tunbasedev = NOUDEV;
64 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
66 static void tunclone(void *arg, char *name, int namelen, dev_t *dev);
67 static void tuncreate(dev_t dev);
68 static int tunifioctl(struct ifnet *, u_long, caddr_t);
69 static int tuninit(struct ifnet *);
70 static int tunmodevent(module_t, int, void *);
71 static int tunoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
73 static void tunstart(struct ifnet *);
75 static d_open_t tunopen;
76 static d_close_t tunclose;
77 static d_read_t tunread;
78 static d_write_t tunwrite;
79 static d_ioctl_t tunioctl;
80 static d_poll_t tunpoll;
83 static struct cdevsw tun_cdevsw = {
91 /* strategy */ nostrategy,
100 tunclone(void *arg, char *name, int namelen, dev_t *dev)
109 if (strcmp(name, TUNNAME) == 0) {
110 r = rman_reserve_resource(tununits, 0, TUN_MAXUNIT, 1,
111 RF_ALLOCATED | RF_ACTIVE, NULL);
112 u = rman_get_start(r);
113 err = rman_release_resource(r);
114 KASSERT(err == 0, ("Unexpected failure releasing resource"));
115 *dev = makedev(CDEV_MAJOR, unit2minor(u));
116 if ((*dev)->si_flags & SI_NAMED)
117 return; /* Already make_dev()d */
118 } else if (dev_stdclone(name, NULL, TUNNAME, &u) != 1)
119 return; /* Don't recognise the name */
121 *dev = make_dev(&tun_cdevsw, unit2minor(u),
122 UID_ROOT, GID_WHEEL, 0600, "tun%d", u);
125 * All devices depend on tunbasedev so that we can simply
126 * destroy_dev() this device at module unload time to get
127 * rid of all our make_dev()d resources.
129 if (tunbasedev == NOUDEV)
130 tunbasedev = (*dev)->si_udev;
132 (*dev)->si_flags |= SI_CHEAPCLONE;
133 dev_depends(udev2dev(tunbasedev, 0), *dev);
138 tunmodevent(module_t mod, int type, void *data)
140 static eventhandler_tag tag;
141 struct tun_softc *tp;
147 tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
150 if (!devfs_present) {
151 err = cdevsw_add(&tun_cdevsw);
153 EVENTHANDLER_DEREGISTER(dev_clone, tag);
157 tununits->rm_type = RMAN_ARRAY;
158 tununits->rm_descr = "open if_tun units";
159 err = rman_init(tununits);
161 cdevsw_remove(&tun_cdevsw);
162 EVENTHANDLER_DEREGISTER(dev_clone, tag);
165 err = rman_manage_region(tununits, 0, TUN_MAXUNIT);
167 printf("%s: tununits: rman_manage_region: Failed %d\n",
170 cdevsw_remove(&tun_cdevsw);
171 EVENTHANDLER_DEREGISTER(dev_clone, tag);
176 err = rman_fini(tununits);
179 EVENTHANDLER_DEREGISTER(dev_clone, tag);
181 while (tunhead != NULL) {
182 KASSERT((tunhead->tun_flags & TUN_OPEN) == 0,
183 ("tununits is out of sync - unit %d",
184 tunhead->tun_if.if_unit));
186 dev = makedev(tun_cdevsw.d_maj,
187 unit2minor(tp->tun_if.if_unit));
188 KASSERT(dev->si_drv1 == tp, ("Bad makedev result"));
190 bpfdetach(&tp->tun_if);
191 if_detach(&tp->tun_if);
192 KASSERT(dev->si_flags & SI_NAMED, ("Missing make_dev"));
197 * Destroying tunbasedev results in all of our make_dev()s
198 * conveniently going away.
200 if (tunbasedev != NOUDEV)
201 destroy_dev(udev2dev(tunbasedev, 0));
204 cdevsw_remove(&tun_cdevsw);
210 static moduledata_t tun_mod = {
216 DECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
219 tunstart(struct ifnet *ifp)
221 struct tun_softc *tp = ifp->if_softc;
223 if (tp->tun_flags & TUN_RWAIT) {
224 tp->tun_flags &= ~TUN_RWAIT;
227 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
228 pgsigio(&tp->tun_sigio, SIGIO, 0);
229 selwakeup(&tp->tun_rsel);
235 struct tun_softc *sc;
238 if (!(dev->si_flags & SI_NAMED))
239 dev = make_dev(&tun_cdevsw, minor(dev),
240 UID_UUCP, GID_DIALER, 0600, "tun%d", dev2unit(dev));
242 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
243 sc->tun_flags = TUN_INITED;
248 ifp->if_unit = dev2unit(dev);
249 ifp->if_name = TUNNAME;
250 ifp->if_mtu = TUNMTU;
251 ifp->if_ioctl = tunifioctl;
252 ifp->if_output = tunoutput;
253 ifp->if_start = tunstart;
254 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
255 ifp->if_type = IFT_PPP;
256 ifp->if_snd.ifq_maxlen = ifqmaxlen;
259 bpfattach(ifp, DLT_NULL, sizeof(u_int));
264 tunopen(dev_t dev, int flag, int mode, struct thread *td)
268 struct tun_softc *tp;
271 unit = dev2unit(dev);
272 if (unit > TUN_MAXUNIT)
275 r = rman_reserve_resource(tununits, unit, unit, 1,
276 RF_ALLOCATED | RF_ACTIVE, NULL);
280 dev->si_flags &= ~SI_CHEAPCLONE;
287 KASSERT(!(tp->tun_flags & TUN_OPEN), ("Resource & flags out-of-sync"));
289 tp->tun_pid = td->td_proc->p_pid;
291 tp->tun_flags |= TUN_OPEN;
292 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
298 * tunclose - close the device - mark i/f down & delete
302 tunclose(dev_t dev, int foo, int bar, struct thread *td)
304 struct tun_softc *tp;
312 KASSERT(tp->r_unit, ("Unit %d not marked open", ifp->if_unit));
313 tp->tun_flags &= ~TUN_OPEN;
317 * junk all pending output
319 IF_DRAIN(&ifp->if_snd);
321 if (ifp->if_flags & IFF_UP) {
327 if (ifp->if_flags & IFF_RUNNING) {
328 register struct ifaddr *ifa;
331 /* find internet addresses and delete routes */
332 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
333 if (ifa->ifa_addr->sa_family == AF_INET)
334 rtinit(ifa, (int)RTM_DELETE,
335 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
336 ifp->if_flags &= ~IFF_RUNNING;
340 funsetown(&tp->tun_sigio);
341 selwakeup(&tp->tun_rsel);
343 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
344 err = rman_release_resource(tp->r_unit);
345 KASSERT(err == 0, ("Unit %d failed to release", ifp->if_unit));
351 tuninit(struct ifnet *ifp)
353 struct tun_softc *tp = ifp->if_softc;
354 register struct ifaddr *ifa;
357 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
359 ifp->if_flags |= IFF_UP | IFF_RUNNING;
360 getmicrotime(&ifp->if_lastchange);
362 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
363 ifa = TAILQ_NEXT(ifa, ifa_link)) {
364 if (ifa->ifa_addr == NULL)
366 /* XXX: Should maybe return straight off? */
369 if (ifa->ifa_addr->sa_family == AF_INET) {
370 struct sockaddr_in *si;
372 si = (struct sockaddr_in *)ifa->ifa_addr;
373 if (si->sin_addr.s_addr)
374 tp->tun_flags |= TUN_IASET;
376 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
377 if (si && si->sin_addr.s_addr)
378 tp->tun_flags |= TUN_DSTADDR;
387 * Process an ioctl request.
390 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
392 struct ifreq *ifr = (struct ifreq *)data;
393 struct tun_softc *tp = ifp->if_softc;
400 ifs = (struct ifstat *)data;
402 sprintf(ifs->ascii + strlen(ifs->ascii),
403 "\tOpened by PID %d\n", tp->tun_pid);
406 error = tuninit(ifp);
407 TUNDEBUG("%s%d: address set, error=%d\n",
408 ifp->if_name, ifp->if_unit, error);
411 error = tuninit(ifp);
412 TUNDEBUG("%s%d: destination address set, error=%d\n",
413 ifp->if_name, ifp->if_unit, error);
416 ifp->if_mtu = ifr->ifr_mtu;
417 TUNDEBUG("%s%d: mtu set\n", ifp->if_name, ifp->if_unit);
431 * tunoutput - queue packets from higher level ready to put out.
437 struct sockaddr *dst,
440 struct tun_softc *tp = ifp->if_softc;
445 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
448 error = mac_check_ifnet_transmit(ifp, m0);
455 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
456 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
457 ifp->if_unit, tp->tun_flags);
462 /* BPF write needs to be handled specially */
463 if (dst->sa_family == AF_UNSPEC) {
464 dst->sa_family = *(mtod(m0, int *));
465 m0->m_len -= sizeof(int);
466 m0->m_pkthdr.len -= sizeof(int);
467 m0->m_data += sizeof(int);
472 * We need to prepend the address family as
473 * a four byte field. Cons up a dummy header
474 * to pacify bpf. This is safe because bpf
475 * will only read from the mbuf (i.e., it won't
476 * try to free it or keep a pointer to it).
479 uint32_t af = dst->sa_family;
483 m.m_data = (char *)⁡
488 /* prepend sockaddr? this may abort if the mbuf allocation fails */
489 if (tp->tun_flags & TUN_LMODE) {
490 /* allocate space for sockaddr */
491 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
493 /* if allocation failed drop packet */
499 bcopy(dst, m0->m_data, dst->sa_len);
503 if (tp->tun_flags & TUN_IFHEAD) {
504 /* Prepend the address family */
505 M_PREPEND(m0, 4, M_DONTWAIT);
507 /* if allocation failed drop packet */
513 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
516 if (dst->sa_family != AF_INET)
520 return (EAFNOSUPPORT);
524 if (! IF_HANDOFF(&ifp->if_snd, m0, ifp)) {
525 ifp->if_collisions++;
533 * the cdevsw interface is now pretty minimal.
536 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
540 struct tun_softc *tp = dev->si_drv1;
541 struct tuninfo *tunp;
545 tunp = (struct tuninfo *)data;
546 if (tunp->mtu < IF_MINMTU)
548 if (tp->tun_if.if_mtu != tunp->mtu
549 && (error = suser(td)) != 0)
551 tp->tun_if.if_mtu = tunp->mtu;
552 tp->tun_if.if_type = tunp->type;
553 tp->tun_if.if_baudrate = tunp->baudrate;
556 tunp = (struct tuninfo *)data;
557 tunp->mtu = tp->tun_if.if_mtu;
558 tunp->type = tp->tun_if.if_type;
559 tunp->baudrate = tp->tun_if.if_baudrate;
562 tundebug = *(int *)data;
565 *(int *)data = tundebug;
569 tp->tun_flags |= TUN_LMODE;
570 tp->tun_flags &= ~TUN_IFHEAD;
572 tp->tun_flags &= ~TUN_LMODE;
576 tp->tun_flags |= TUN_IFHEAD;
577 tp->tun_flags &= ~TUN_LMODE;
579 tp->tun_flags &= ~TUN_IFHEAD;
582 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
585 /* deny this if UP */
586 if (tp->tun_if.if_flags & IFF_UP)
589 switch (*(int *)data & ~IFF_MULTICAST) {
590 case IFF_POINTOPOINT:
592 tp->tun_if.if_flags &=
593 ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
594 tp->tun_if.if_flags |= *(int *)data;
601 tp->tun_pid = curthread->td_proc->p_pid;
607 tp->tun_flags |= TUN_ASYNC;
609 tp->tun_flags &= ~TUN_ASYNC;
613 if (tp->tun_if.if_snd.ifq_head) {
614 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
615 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
616 *(int *)data += mb->m_len;
622 return (fsetown(*(int *)data, &tp->tun_sigio));
625 *(int *)data = fgetown(tp->tun_sigio);
628 /* This is deprecated, FIOSETOWN should be used instead. */
630 return (fsetown(-(*(int *)data), &tp->tun_sigio));
632 /* This is deprecated, FIOGETOWN should be used instead. */
634 *(int *)data = -fgetown(tp->tun_sigio);
644 * The cdevsw read interface - reads a packet at a time, or at
645 * least as much of a packet as can be read.
648 tunread(dev_t dev, struct uio *uio, int flag)
650 struct tun_softc *tp = dev->si_drv1;
651 struct ifnet *ifp = &tp->tun_if;
655 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
656 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
657 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
658 ifp->if_unit, tp->tun_flags);
662 tp->tun_flags &= ~TUN_RWAIT;
666 IF_DEQUEUE(&ifp->if_snd, m);
668 if (flag & IO_NDELAY) {
670 return (EWOULDBLOCK);
672 tp->tun_flags |= TUN_RWAIT;
673 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
674 "tunread", 0)) != 0) {
682 while (m && uio->uio_resid > 0 && error == 0) {
683 len = min(uio->uio_resid, m->m_len);
685 error = uiomove(mtod(m, caddr_t), len, uio);
690 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
697 * the cdevsw write interface - an atomic write is a packet - or else!
700 tunwrite(dev_t dev, struct uio *uio, int flag)
702 struct tun_softc *tp = dev->si_drv1;
703 struct ifnet *ifp = &tp->tun_if;
704 struct mbuf *top, **mp, *m;
705 int error=0, tlen, mlen;
708 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
710 if (uio->uio_resid == 0)
713 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
714 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
718 tlen = uio->uio_resid;
720 /* get a header mbuf */
721 MGETHDR(m, M_DONTWAIT, MT_DATA);
728 while (error == 0 && uio->uio_resid > 0) {
729 m->m_len = min(mlen, uio->uio_resid);
730 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
733 if (uio->uio_resid > 0) {
734 MGET (m, M_DONTWAIT, MT_DATA);
749 top->m_pkthdr.len = tlen;
750 top->m_pkthdr.rcvif = ifp;
752 mac_create_mbuf_from_ifnet(ifp, top);
756 if (tp->tun_flags & TUN_IFHEAD) {
758 * Conveniently, we already have a 4-byte address
759 * family prepended to our packet !
760 * Inconveniently, it's in the wrong byte order !
762 if ((top = m_pullup(top, sizeof(family))) == NULL)
764 *mtod(top, u_int32_t *) =
765 ntohl(*mtod(top, u_int32_t *));
767 *mtod(top, u_int32_t *) =
768 htonl(*mtod(top, u_int32_t *));
771 * We need to prepend the address family as
772 * a four byte field. Cons up a dummy header
773 * to pacify bpf. This is safe because bpf
774 * will only read from the mbuf (i.e., it won't
775 * try to free it or keep a pointer to it).
778 uint32_t af = AF_INET;
782 m.m_data = (char *)⁡
788 if (tp->tun_flags & TUN_IFHEAD) {
789 if (top->m_len < sizeof(family) &&
790 (top = m_pullup(top, sizeof(family))) == NULL)
792 family = ntohl(*mtod(top, u_int32_t *));
793 m_adj(top, sizeof(family));
797 ifp->if_ibytes += top->m_pkthdr.len;
800 return (family_enqueue(family, top));
804 * tunpoll - the poll interface, this is only useful on reads
805 * really. The write detect always returns true, write never blocks
806 * anyway, it either accepts the packet or drops it.
809 tunpoll(dev_t dev, int events, struct thread *td)
812 struct tun_softc *tp = dev->si_drv1;
813 struct ifnet *ifp = &tp->tun_if;
817 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
819 if (events & (POLLIN | POLLRDNORM)) {
820 if (ifp->if_snd.ifq_len > 0) {
821 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
822 ifp->if_unit, ifp->if_snd.ifq_len);
823 revents |= events & (POLLIN | POLLRDNORM);
825 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
827 selrecord(td, &tp->tun_rsel);
830 if (events & (POLLOUT | POLLWRNORM))
831 revents |= events & (POLLOUT | POLLWRNORM);