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.37 2008/06/05 18:06:32 swildner Exp $
20 #include "opt_atalk.h"
22 #include "opt_inet6.h"
25 #include <sys/param.h>
28 #include <sys/systm.h>
30 #include <sys/socket.h>
32 #include <sys/device.h>
33 #include <sys/filio.h>
34 #include <sys/sockio.h>
35 #include <sys/thread2.h>
36 #include <sys/ttycom.h>
38 #include <sys/signalvar.h>
39 #include <sys/filedesc.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
47 #include <net/if_types.h>
48 #include <net/ifq_var.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
53 #include <netinet/in.h>
58 #include "if_tunvar.h"
61 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
63 static void tunattach (void *);
64 PSEUDO_SET(tunattach, if_tun);
66 static void tuncreate (cdev_t dev);
68 #define TUNDEBUG if (tundebug) if_printf
69 static int tundebug = 0;
70 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
72 static int tunoutput (struct ifnet *, struct mbuf *, struct sockaddr *,
74 static int tunifioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
75 static int tuninit (struct ifnet *);
76 static void tunstart(struct ifnet *);
78 static d_open_t tunopen;
79 static d_close_t tunclose;
80 static d_read_t tunread;
81 static d_write_t tunwrite;
82 static d_ioctl_t tunioctl;
83 static d_poll_t tunpoll;
86 static struct dev_ops tun_ops = {
87 { "tun", CDEV_MAJOR, 0 },
97 tunattach(void *dummy)
99 dev_ops_add(&tun_ops, 0, 0);
103 tuncreate(cdev_t dev)
105 struct tun_softc *sc;
108 dev = make_dev(&tun_ops, minor(dev),
109 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
111 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
112 sc->tun_flags = TUN_INITED;
115 if_initname(ifp, "tun", lminor(dev));
116 ifp->if_mtu = TUNMTU;
117 ifp->if_ioctl = tunifioctl;
118 ifp->if_output = tunoutput;
119 ifp->if_start = tunstart;
120 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
121 ifp->if_type = IFT_PPP;
122 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
123 ifq_set_ready(&ifp->if_snd);
125 if_attach(ifp, NULL);
126 bpfattach(ifp, DLT_NULL, sizeof(u_int));
131 * tunnel open - must be superuser & the device must be
135 tunopen(struct dev_open_args *ap)
137 cdev_t dev = ap->a_head.a_dev;
139 struct tun_softc *tp;
142 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) != 0)
150 if (tp->tun_flags & TUN_OPEN)
152 tp->tun_pid = curproc->p_pid;
154 tp->tun_flags |= TUN_OPEN;
155 TUNDEBUG(ifp, "open\n");
160 * tunclose - close the device - mark i/f down & delete
164 tunclose(struct dev_close_args *ap)
166 cdev_t dev = ap->a_head.a_dev;
167 struct tun_softc *tp;
173 tp->tun_flags &= ~TUN_OPEN;
176 /* Junk all pending output. */
177 ifq_purge(&ifp->if_snd);
179 if (ifp->if_flags & IFF_UP)
181 ifp->if_flags &= ~IFF_RUNNING;
182 if_purgeaddrs_nolink(ifp);
184 funsetown(tp->tun_sigio);
185 selwakeup(&tp->tun_rsel);
187 TUNDEBUG(ifp, "closed\n");
192 tuninit(struct ifnet *ifp)
194 struct tun_softc *tp = ifp->if_softc;
195 struct ifaddr_container *ifac;
198 TUNDEBUG(ifp, "tuninit\n");
200 ifp->if_flags |= IFF_UP | IFF_RUNNING;
201 getmicrotime(&ifp->if_lastchange);
203 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
204 struct ifaddr *ifa = ifac->ifa;
206 if (ifa->ifa_addr == NULL) {
208 /* XXX: Should maybe return straight off? */
211 if (ifa->ifa_addr->sa_family == AF_INET) {
212 struct sockaddr_in *si;
214 si = (struct sockaddr_in *)ifa->ifa_addr;
215 if (si->sin_addr.s_addr)
216 tp->tun_flags |= TUN_IASET;
225 * Process an ioctl request.
230 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
232 struct ifreq *ifr = (struct ifreq *)data;
233 struct tun_softc *tp = ifp->if_softc;
239 ifs = (struct ifstat *)data;
241 ksprintf(ifs->ascii + strlen(ifs->ascii),
242 "\tOpened by PID %d\n", tp->tun_pid);
245 error = tuninit(ifp);
246 TUNDEBUG(ifp, "address set, error=%d\n", error);
249 error = tuninit(ifp);
250 TUNDEBUG(ifp, "destination address set, error=%d\n", error);
253 ifp->if_mtu = ifr->ifr_mtu;
254 TUNDEBUG(ifp, "mtu set\n");
267 * tunoutput - queue packets from higher level ready to put out.
272 tunoutput_serialized(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
275 struct tun_softc *tp = ifp->if_softc;
277 struct altq_pktattr pktattr;
279 TUNDEBUG(ifp, "tunoutput\n");
281 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
282 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
288 * if the queueing discipline needs packet classification,
289 * do it before prepending link headers.
291 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
293 /* BPF write needs to be handled specially */
294 if (dst->sa_family == AF_UNSPEC) {
295 dst->sa_family = *(mtod(m0, int *));
296 m0->m_len -= sizeof(int);
297 m0->m_pkthdr.len -= sizeof(int);
298 m0->m_data += sizeof(int);
303 * We need to prepend the address family as
306 uint32_t af = dst->sa_family;
308 bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
311 /* prepend sockaddr? this may abort if the mbuf allocation fails */
312 if (tp->tun_flags & TUN_LMODE) {
313 /* allocate space for sockaddr */
314 M_PREPEND(m0, dst->sa_len, MB_DONTWAIT);
316 /* if allocation failed drop packet */
318 IF_DROP(&ifp->if_snd);
322 bcopy(dst, m0->m_data, dst->sa_len);
326 if (tp->tun_flags & TUN_IFHEAD) {
327 /* Prepend the address family */
328 M_PREPEND(m0, 4, MB_DONTWAIT);
330 /* if allocation failed drop packet */
332 IF_DROP(&ifp->if_snd);
336 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
339 if (dst->sa_family != AF_INET)
347 error = ifq_handoff(ifp, m0, &pktattr);
349 ifp->if_collisions++;
352 if (tp->tun_flags & TUN_RWAIT) {
353 tp->tun_flags &= ~TUN_RWAIT;
357 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
358 pgsigio(tp->tun_sigio, SIGIO, 0);
359 selwakeup(&tp->tun_rsel);
366 tunoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
371 ifnet_serialize_all(ifp);
372 error = tunoutput_serialized(ifp, m0, dst, rt);
373 ifnet_deserialize_all(ifp);
379 * the ops interface is now pretty minimal.
382 tunioctl(struct dev_ioctl_args *ap)
384 cdev_t dev = ap->a_head.a_dev;
385 struct tun_softc *tp = dev->si_drv1;
386 struct tuninfo *tunp;
390 tunp = (struct tuninfo *)ap->a_data;
391 if (tunp->mtu < IF_MINMTU)
393 tp->tun_if.if_mtu = tunp->mtu;
394 tp->tun_if.if_type = tunp->type;
395 tp->tun_if.if_baudrate = tunp->baudrate;
398 tunp = (struct tuninfo *)ap->a_data;
399 tunp->mtu = tp->tun_if.if_mtu;
400 tunp->type = tp->tun_if.if_type;
401 tunp->baudrate = tp->tun_if.if_baudrate;
404 tundebug = *(int *)ap->a_data;
407 *(int *)ap->a_data = tundebug;
410 if (*(int *)ap->a_data) {
411 tp->tun_flags |= TUN_LMODE;
412 tp->tun_flags &= ~TUN_IFHEAD;
414 tp->tun_flags &= ~TUN_LMODE;
417 if (*(int *)ap->a_data) {
418 tp->tun_flags |= TUN_IFHEAD;
419 tp->tun_flags &= ~TUN_LMODE;
421 tp->tun_flags &= ~TUN_IFHEAD;
424 *(int *)ap->a_data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
427 /* deny this if UP */
428 if (tp->tun_if.if_flags & IFF_UP)
431 switch (*(int *)ap->a_data & ~IFF_MULTICAST) {
432 case IFF_POINTOPOINT:
434 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
435 tp->tun_if.if_flags |= *(int *)ap->a_data;
442 tp->tun_pid = curproc->p_pid;
445 if (*(int *)ap->a_data)
446 tp->tun_flags |= TUN_ASYNC;
448 tp->tun_flags &= ~TUN_ASYNC;
451 if (!ifq_is_empty(&tp->tun_if.if_snd)) {
454 mb = ifq_poll(&tp->tun_if.if_snd);
455 for( *(int *)ap->a_data = 0; mb != 0; mb = mb->m_next)
456 *(int *)ap->a_data += mb->m_len;
458 *(int *)ap->a_data = 0;
462 return (fsetown(*(int *)ap->a_data, &tp->tun_sigio));
465 *(int *)ap->a_data = fgetown(tp->tun_sigio);
468 /* This is deprecated, FIOSETOWN should be used instead. */
470 return (fsetown(-(*(int *)ap->a_data), &tp->tun_sigio));
472 /* This is deprecated, FIOGETOWN should be used instead. */
474 *(int *)ap->a_data = -fgetown(tp->tun_sigio);
484 * The ops read interface - reads a packet at a time, or at
485 * least as much of a packet as can be read.
488 tunread(struct dev_read_args *ap)
490 cdev_t dev = ap->a_head.a_dev;
491 struct uio *uio = ap->a_uio;
492 struct tun_softc *tp = dev->si_drv1;
493 struct ifnet *ifp = &tp->tun_if;
497 TUNDEBUG(ifp, "read\n");
498 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
499 TUNDEBUG(ifp, "not ready 0%o\n", tp->tun_flags);
503 tp->tun_flags &= ~TUN_RWAIT;
505 ifnet_serialize_all(ifp);
507 while ((m0 = ifq_dequeue(&ifp->if_snd, NULL)) == NULL) {
508 if (ap->a_ioflag & IO_NDELAY) {
509 ifnet_deserialize_all(ifp);
512 tp->tun_flags |= TUN_RWAIT;
513 ifnet_deserialize_all(ifp);
514 if ((error = tsleep(tp, PCATCH, "tunread", 0)) != 0)
516 ifnet_serialize_all(ifp);
519 ifnet_deserialize_all(ifp);
521 while (m0 && uio->uio_resid > 0 && error == 0) {
522 len = min(uio->uio_resid, m0->m_len);
524 error = uiomove(mtod(m0, caddr_t), len, uio);
529 TUNDEBUG(ifp, "Dropping mbuf\n");
536 * the ops write interface - an atomic write is a packet - or else!
539 tunwrite(struct dev_write_args *ap)
541 cdev_t dev = ap->a_head.a_dev;
542 struct uio *uio = ap->a_uio;
543 struct tun_softc *tp = dev->si_drv1;
544 struct ifnet *ifp = &tp->tun_if;
545 struct mbuf *top, **mp, *m;
546 int error=0, tlen, mlen;
550 TUNDEBUG(ifp, "tunwrite\n");
552 if (uio->uio_resid == 0)
555 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
556 TUNDEBUG(ifp, "len=%d!\n", uio->uio_resid);
559 tlen = uio->uio_resid;
561 /* get a header mbuf */
562 MGETHDR(m, MB_DONTWAIT, MT_DATA);
569 while (error == 0 && uio->uio_resid > 0) {
570 m->m_len = min(mlen, uio->uio_resid);
571 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
574 if (uio->uio_resid > 0) {
575 MGET (m, MB_DONTWAIT, MT_DATA);
590 top->m_pkthdr.len = tlen;
591 top->m_pkthdr.rcvif = ifp;
594 if (tp->tun_flags & TUN_IFHEAD) {
596 * Conveniently, we already have a 4-byte address
597 * family prepended to our packet !
598 * Inconveniently, it's in the wrong byte order !
600 if ((top = m_pullup(top, sizeof(family))) == NULL)
602 *mtod(top, u_int32_t *) =
603 ntohl(*mtod(top, u_int32_t *));
604 bpf_mtap(ifp->if_bpf, top);
605 *mtod(top, u_int32_t *) =
606 htonl(*mtod(top, u_int32_t *));
609 * We need to prepend the address family as
612 static const uint32_t af = AF_INET;
614 bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
618 if (tp->tun_flags & TUN_IFHEAD) {
619 if (top->m_len < sizeof(family) &&
620 (top = m_pullup(top, sizeof(family))) == NULL)
622 family = ntohl(*mtod(top, u_int32_t *));
623 m_adj(top, sizeof(family));
627 ifp->if_ibytes += top->m_pkthdr.len;
653 return (EAFNOSUPPORT);
656 netisr_dispatch(isr, top);
661 * tunpoll - the poll interface, this is only useful on reads
662 * really. The write detect always returns true, write never blocks
663 * anyway, it either accepts the packet or drops it.
666 tunpoll(struct dev_poll_args *ap)
668 cdev_t dev = ap->a_head.a_dev;
669 struct tun_softc *tp = dev->si_drv1;
670 struct ifnet *ifp = &tp->tun_if;
673 TUNDEBUG(ifp, "tunpoll\n");
675 ifnet_serialize_all(ifp);
677 if (ap->a_events & (POLLIN | POLLRDNORM)) {
678 if (!ifq_is_empty(&ifp->if_snd)) {
679 TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
680 revents |= ap->a_events & (POLLIN | POLLRDNORM);
682 TUNDEBUG(ifp, "tunpoll waiting\n");
683 selrecord(curthread, &tp->tun_rsel);
686 if (ap->a_events & (POLLOUT | POLLWRNORM))
687 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
689 ifnet_deserialize_all(ifp);
690 ap->a_events = revents;
695 * Start packet transmission on the interface.
696 * when the interface queue is rate-limited by ALTQ,
697 * if_start is needed to drain packets from the queue in order
698 * to notify readers when outgoing packets become ready.
701 tunstart(struct ifnet *ifp)
703 struct tun_softc *tp = ifp->if_softc;
706 if (!ifq_is_enabled(&ifp->if_snd))
709 m = ifq_poll(&ifp->if_snd);
711 if (tp->tun_flags & TUN_RWAIT) {
712 tp->tun_flags &= ~TUN_RWAIT;
715 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
716 pgsigio(tp->tun_sigio, SIGIO, 0);
717 selwakeup(&tp->tun_rsel);