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.33 2008/01/06 01:51:55 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>
31 #include <sys/device.h>
32 #include <sys/filio.h>
33 #include <sys/sockio.h>
34 #include <sys/thread2.h>
35 #include <sys/ttycom.h>
37 #include <sys/signalvar.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
57 #include "if_tunvar.h"
60 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
62 static void tunattach (void *);
63 PSEUDO_SET(tunattach, if_tun);
65 static void tuncreate (cdev_t dev);
67 #define TUNDEBUG if (tundebug) if_printf
68 static int tundebug = 0;
69 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
71 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
73 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
74 static int tuninit (struct ifnet *);
75 static void tunstart(struct ifnet *);
77 static d_open_t tunopen;
78 static d_close_t tunclose;
79 static d_read_t tunread;
80 static d_write_t tunwrite;
81 static d_ioctl_t tunioctl;
82 static d_poll_t tunpoll;
85 static struct dev_ops tun_ops = {
86 { "tun", CDEV_MAJOR, 0 },
96 tunattach(void *dummy)
98 dev_ops_add(&tun_ops, 0, 0);
102 tuncreate(cdev_t dev)
104 struct tun_softc *sc;
107 dev = make_dev(&tun_ops, minor(dev),
108 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
110 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
111 sc->tun_flags = TUN_INITED;
114 if_initname(ifp, "tun", lminor(dev));
115 ifp->if_mtu = TUNMTU;
116 ifp->if_ioctl = tunifioctl;
117 ifp->if_output = tunoutput;
118 ifp->if_start = tunstart;
119 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
120 ifp->if_type = IFT_PPP;
121 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
122 ifq_set_ready(&ifp->if_snd);
124 if_attach(ifp, NULL);
125 bpfattach(ifp, DLT_NULL, sizeof(u_int));
130 * tunnel open - must be superuser & the device must be
134 tunopen(struct dev_open_args *ap)
136 cdev_t dev = ap->a_head.a_dev;
138 struct tun_softc *tp;
141 if ((error = suser_cred(ap->a_cred, 0)) != NULL)
149 if (tp->tun_flags & TUN_OPEN)
151 tp->tun_pid = curproc->p_pid;
153 tp->tun_flags |= TUN_OPEN;
154 TUNDEBUG(ifp, "open\n");
159 * tunclose - close the device - mark i/f down & delete
163 tunclose(struct dev_close_args *ap)
165 cdev_t dev = ap->a_head.a_dev;
166 struct tun_softc *tp;
172 tp->tun_flags &= ~TUN_OPEN;
175 /* Junk all pending output. */
176 lwkt_serialize_enter(ifp->if_serializer);
177 ifq_purge(&ifp->if_snd);
178 lwkt_serialize_exit(ifp->if_serializer);
180 if (ifp->if_flags & IFF_UP) {
181 lwkt_serialize_enter(ifp->if_serializer);
183 lwkt_serialize_exit(ifp->if_serializer);
185 ifp->if_flags &= ~IFF_RUNNING;
186 if_purgeaddrs_nolink(ifp);
188 funsetown(tp->tun_sigio);
189 selwakeup(&tp->tun_rsel);
191 TUNDEBUG(ifp, "closed\n");
196 tuninit(struct ifnet *ifp)
198 struct tun_softc *tp = ifp->if_softc;
202 TUNDEBUG(ifp, "tuninit\n");
204 ifp->if_flags |= IFF_UP | IFF_RUNNING;
205 getmicrotime(&ifp->if_lastchange);
207 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
208 ifa = TAILQ_NEXT(ifa, ifa_link)) {
209 if (ifa->ifa_addr == NULL)
211 /* XXX: Should maybe return straight off? */
214 if (ifa->ifa_addr->sa_family == AF_INET) {
215 struct sockaddr_in *si;
217 si = (struct sockaddr_in *)ifa->ifa_addr;
218 if (si->sin_addr.s_addr)
219 tp->tun_flags |= TUN_IASET;
228 * Process an ioctl request.
233 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
235 struct ifreq *ifr = (struct ifreq *)data;
236 struct tun_softc *tp = ifp->if_softc;
242 ifs = (struct ifstat *)data;
244 ksprintf(ifs->ascii + strlen(ifs->ascii),
245 "\tOpened by PID %d\n", tp->tun_pid);
248 error = tuninit(ifp);
249 TUNDEBUG(ifp, "address set, error=%d\n", error);
252 error = tuninit(ifp);
253 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
256 ifp->if_mtu = ifr->ifr_mtu;
257 TUNDEBUG(ifp, "mtu set\n");
270 * tunoutput - queue packets from higher level ready to put out.
275 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
278 struct tun_softc *tp = ifp->if_softc;
280 struct altq_pktattr pktattr;
282 TUNDEBUG(ifp, "tunoutput\n");
284 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
285 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
291 * if the queueing discipline needs packet classification,
292 * do it before prepending link headers.
294 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
296 /* BPF write needs to be handled specially */
297 if (dst->sa_family == AF_UNSPEC) {
298 dst->sa_family = *(mtod(m0, int *));
299 m0->m_len -= sizeof(int);
300 m0->m_pkthdr.len -= sizeof(int);
301 m0->m_data += sizeof(int);
306 * We need to prepend the address family as
309 uint32_t af = dst->sa_family;
311 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
314 /* prepend sockaddr? this may abort if the mbuf allocation fails */
315 if (tp->tun_flags & TUN_LMODE) {
316 /* allocate space for sockaddr */
317 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
319 /* if allocation failed drop packet */
321 IF_DROP(&ifp->if_snd);
325 bcopy(dst, m0->m_data, dst->sa_len);
329 if (tp->tun_flags & TUN_IFHEAD) {
330 /* Prepend the address family */
331 M_PREPEND(m0, 4, MB_DONTWAIT);
333 /* if allocation failed drop packet */
335 IF_DROP(&ifp->if_snd);
339 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
342 if (dst->sa_family != AF_INET)
350 error = ifq_handoff(ifp, m0, &pktattr);
352 ifp->if_collisions++;
355 if (tp->tun_flags & TUN_RWAIT) {
356 tp->tun_flags &= ~TUN_RWAIT;
360 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
361 pgsigio(tp->tun_sigio, SIGIO, 0);
362 selwakeup(&tp->tun_rsel);
369 * the ops interface is now pretty minimal.
372 tunioctl(struct dev_ioctl_args *ap)
374 cdev_t dev = ap->a_head.a_dev;
375 struct tun_softc *tp = dev->si_drv1;
376 struct tuninfo *tunp;
380 tunp = (struct tuninfo *)ap->a_data;
381 if (tunp->mtu < IF_MINMTU)
383 tp->tun_if.if_mtu = tunp->mtu;
384 tp->tun_if.if_type = tunp->type;
385 tp->tun_if.if_baudrate = tunp->baudrate;
388 tunp = (struct tuninfo *)ap->a_data;
389 tunp->mtu = tp->tun_if.if_mtu;
390 tunp->type = tp->tun_if.if_type;
391 tunp->baudrate = tp->tun_if.if_baudrate;
394 tundebug = *(int *)ap->a_data;
397 *(int *)ap->a_data = tundebug;
400 if (*(int *)ap->a_data) {
401 tp->tun_flags |= TUN_LMODE;
402 tp->tun_flags &= ~TUN_IFHEAD;
404 tp->tun_flags &= ~TUN_LMODE;
407 if (*(int *)ap->a_data) {
408 tp->tun_flags |= TUN_IFHEAD;
409 tp->tun_flags &= ~TUN_LMODE;
411 tp->tun_flags &= ~TUN_IFHEAD;
414 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
417 /* deny this if UP */
418 if (tp->tun_if.if_flags & IFF_UP)
421 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
422 case IFF_POINTOPOINT:
424 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
425 tp->tun_if.if_flags |= *(int *)ap->a_data;
432 tp->tun_pid = curproc->p_pid;
435 if (*(int *)ap->a_data)
436 tp->tun_flags |= TUN_ASYNC;
438 tp->tun_flags &= ~TUN_ASYNC;
441 lwkt_serialize_enter(tp->tun_if.if_serializer);
442 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
445 mb = ifq_poll(&tp->tun_if.if_snd);
446 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
447 *(int *)ap->a_data += mb->m_len;
449 *(int *)ap->a_data = 0;
451 lwkt_serialize_exit(tp->tun_if.if_serializer);
454 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
457 *(int *)ap->a_data = fgetown(tp->tun_sigio);
460 /* This is deprecated, FIOSETOWN should be used instead. */
462 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
464 /* This is deprecated, FIOGETOWN should be used instead. */
466 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
476 * The ops read interface - reads a packet at a time, or at
477 * least as much of a packet as can be read.
480 tunread(struct dev_read_args *ap)
482 cdev_t dev = ap->a_head.a_dev;
483 struct uio *uio = ap->a_uio;
484 struct tun_softc *tp = dev->si_drv1;
485 struct ifnet *ifp = &tp->tun_if;
489 TUNDEBUG(ifp, "read\n");
490 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
491 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
495 tp->tun_flags &= ~TUN_RWAIT;
497 lwkt_serialize_enter(ifp->if_serializer);
499 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
500 if (ap->a_ioflag & IO_NDELAY) {
501 lwkt_serialize_exit(ifp->if_serializer);
504 tp->tun_flags |= TUN_RWAIT;
505 lwkt_serialize_exit(ifp->if_serializer);
506 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
508 lwkt_serialize_enter(ifp->if_serializer);
511 lwkt_serialize_exit(ifp->if_serializer);
513 while (m0 && uio->uio_resid > 0 && error == 0) {
514 len = min(uio->uio_resid, m0->m_len);
516 error = uiomove(mtod(m0, caddr_t), len, uio);
521 TUNDEBUG(ifp, "Dropping mbuf\n");
528 * the ops write interface - an atomic write is a packet - or else!
531 tunwrite(struct dev_write_args *ap)
533 cdev_t dev = ap->a_head.a_dev;
534 struct uio *uio = ap->a_uio;
535 struct tun_softc *tp = dev->si_drv1;
536 struct ifnet *ifp = &tp->tun_if;
537 struct mbuf *top, **mp, *m;
538 int error=0, tlen, mlen;
542 TUNDEBUG(ifp, "tunwrite\n");
544 if (uio->uio_resid == 0)
547 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
548 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
551 tlen = uio->uio_resid;
553 /* get a header mbuf */
554 MGETHDR(m, MB_DONTWAIT, MT_DATA);
561 while (error == 0 && uio->uio_resid > 0) {
562 m->m_len = min(mlen, uio->uio_resid);
563 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
566 if (uio->uio_resid > 0) {
567 MGET (m, MB_DONTWAIT, MT_DATA);
582 top->m_pkthdr.len = tlen;
583 top->m_pkthdr.rcvif = ifp;
586 if (tp->tun_flags & TUN_IFHEAD) {
588 * Conveniently, we already have a 4-byte address
589 * family prepended to our packet !
590 * Inconveniently, it's in the wrong byte order !
592 if ((top = m_pullup(top, sizeof(family))) == NULL)
594 *mtod(top, u_int32_t *) =
595 ntohl(*mtod(top, u_int32_t *));
596 bpf_mtap(ifp->if_bpf, top);
597 *mtod(top, u_int32_t *) =
598 htonl(*mtod(top, u_int32_t *));
601 * We need to prepend the address family as
604 static const uint32_t af = AF_INET;
606 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
610 if (tp->tun_flags & TUN_IFHEAD) {
611 if (top->m_len < sizeof(family) &&
612 (top = m_pullup(top, sizeof(family))) == NULL)
614 family = ntohl(*mtod(top, u_int32_t *));
615 m_adj(top, sizeof(family));
619 ifp->if_ibytes += top->m_pkthdr.len;
645 return (EAFNOSUPPORT);
648 netisr_dispatch(isr, top);
653 * tunpoll - the poll interface, this is only useful on reads
654 * really. The write detect always returns true, write never blocks
655 * anyway, it either accepts the packet or drops it.
658 tunpoll(struct dev_poll_args *ap)
660 cdev_t dev = ap->a_head.a_dev;
661 struct tun_softc *tp = dev->si_drv1;
662 struct ifnet *ifp = &tp->tun_if;
665 TUNDEBUG(ifp, "tunpoll\n");
667 lwkt_serialize_enter(ifp->if_serializer);
669 if (ap->a_events & (POLLIN | POLLRDNORM)) {
670 if (!ifq_is_empty(&ifp->if_snd)) {
671 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
672 revents |= ap->a_events & (POLLIN | POLLRDNORM);
674 TUNDEBUG(ifp, "tunpoll waiting\n");
675 selrecord(curthread, &tp->tun_rsel);
678 if (ap->a_events & (POLLOUT | POLLWRNORM))
679 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
681 lwkt_serialize_exit(ifp->if_serializer);
682 ap->a_events = revents;
687 * Start packet transmission on the interface.
688 * when the interface queue is rate-limited by ALTQ,
689 * if_start is needed to drain packets from the queue in order
690 * to notify readers when outgoing packets become ready.
693 tunstart(struct ifnet *ifp)
695 struct tun_softc *tp = ifp->if_softc;
698 if (!ifq_is_enabled(&ifp->if_snd))
701 m = ifq_poll(&ifp->if_snd);
703 if (tp->tun_flags & TUN_RWAIT) {
704 tp->tun_flags &= ~TUN_RWAIT;
707 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
708 pgsigio(tp->tun_sigio, SIGIO, 0);
709 selwakeup(&tp->tun_rsel);