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.2 2003/06/17 04:28:48 dillon Exp $
22 #include <sys/param.h>
24 #include <sys/systm.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>
41 #include <net/if_types.h>
42 #include <net/route.h>
43 #include <net/intrq.h>
46 #include <netinet/in.h>
51 #include <net/if_tunvar.h>
52 #include <net/if_tun.h>
54 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
56 static void tunattach __P((void *));
57 PSEUDO_SET(tunattach, if_tun);
59 static void tuncreate __P((dev_t dev));
61 #define TUNDEBUG if (tundebug) printf
62 static int tundebug = 0;
63 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
65 static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
67 static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
68 static int tuninit __P((struct ifnet *));
70 static d_open_t tunopen;
71 static d_close_t tunclose;
72 static d_read_t tunread;
73 static d_write_t tunwrite;
74 static d_ioctl_t tunioctl;
75 static d_poll_t tunpoll;
78 static struct cdevsw tun_cdevsw = {
86 /* strategy */ nostrategy,
100 cdevsw_add(&tun_cdevsw);
107 struct tun_softc *sc;
110 dev = make_dev(&tun_cdevsw, minor(dev),
111 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
113 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
114 bzero(sc, sizeof *sc);
115 sc->tun_flags = TUN_INITED;
118 ifp->if_unit = lminor(dev);
119 ifp->if_name = "tun";
120 ifp->if_mtu = TUNMTU;
121 ifp->if_ioctl = tunifioctl;
122 ifp->if_output = tunoutput;
123 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
124 ifp->if_type = IFT_PPP;
125 ifp->if_snd.ifq_maxlen = ifqmaxlen;
128 bpfattach(ifp, DLT_NULL, sizeof(u_int));
133 * tunnel open - must be superuser & the device must be
137 tunopen(dev, flag, mode, p)
143 struct tun_softc *tp;
155 if (tp->tun_flags & TUN_OPEN)
157 tp->tun_pid = p->p_pid;
159 tp->tun_flags |= TUN_OPEN;
160 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
165 * tunclose - close the device - mark i/f down & delete
169 tunclose(dev, foo, bar, p)
176 struct tun_softc *tp;
183 tp->tun_flags &= ~TUN_OPEN;
187 * junk all pending output
191 IF_DEQUEUE(&ifp->if_snd, m);
197 if (ifp->if_flags & IFF_UP) {
203 if (ifp->if_flags & IFF_RUNNING) {
204 register struct ifaddr *ifa;
207 /* find internet addresses and delete routes */
208 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
209 if (ifa->ifa_addr->sa_family == AF_INET)
210 rtinit(ifa, (int)RTM_DELETE,
211 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
212 ifp->if_flags &= ~IFF_RUNNING;
216 funsetown(tp->tun_sigio);
217 selwakeup(&tp->tun_rsel);
219 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
227 struct tun_softc *tp = ifp->if_softc;
228 register struct ifaddr *ifa;
231 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
233 ifp->if_flags |= IFF_UP | IFF_RUNNING;
234 getmicrotime(&ifp->if_lastchange);
236 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
237 ifa = TAILQ_NEXT(ifa, ifa_link)) {
238 if (ifa->ifa_addr == NULL)
240 /* XXX: Should maybe return straight off? */
243 if (ifa->ifa_addr->sa_family == AF_INET) {
244 struct sockaddr_in *si;
246 si = (struct sockaddr_in *)ifa->ifa_addr;
247 if (si->sin_addr.s_addr)
248 tp->tun_flags |= TUN_IASET;
250 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
251 if (si && si->sin_addr.s_addr)
252 tp->tun_flags |= TUN_DSTADDR;
261 * Process an ioctl request.
264 tunifioctl(ifp, cmd, data)
269 struct ifreq *ifr = (struct ifreq *)data;
270 struct tun_softc *tp = ifp->if_softc;
277 ifs = (struct ifstat *)data;
279 sprintf(ifs->ascii + strlen(ifs->ascii),
280 "\tOpened by PID %d\n", tp->tun_pid);
283 error = tuninit(ifp);
284 TUNDEBUG("%s%d: address set, error=%d\n",
285 ifp->if_name, ifp->if_unit, error);
288 error = tuninit(ifp);
289 TUNDEBUG("%s%d: destination address set, error=%d\n",
290 ifp->if_name, ifp->if_unit, error);
293 ifp->if_mtu = ifr->ifr_mtu;
294 TUNDEBUG("%s%d: mtu set\n",
295 ifp->if_name, ifp->if_unit);
309 * tunoutput - queue packets from higher level ready to put out.
312 tunoutput(ifp, m0, dst, rt)
315 struct sockaddr *dst;
318 struct tun_softc *tp = ifp->if_softc;
321 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
323 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
324 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
325 ifp->if_unit, tp->tun_flags);
330 /* BPF write needs to be handled specially */
331 if (dst->sa_family == AF_UNSPEC) {
332 dst->sa_family = *(mtod(m0, int *));
333 m0->m_len -= sizeof(int);
334 m0->m_pkthdr.len -= sizeof(int);
335 m0->m_data += sizeof(int);
340 * We need to prepend the address family as
341 * a four byte field. Cons up a dummy header
342 * to pacify bpf. This is safe because bpf
343 * will only read from the mbuf (i.e., it won't
344 * try to free it or keep a pointer to it).
347 uint32_t af = dst->sa_family;
351 m.m_data = (char *)⁡
356 /* prepend sockaddr? this may abort if the mbuf allocation fails */
357 if (tp->tun_flags & TUN_LMODE) {
358 /* allocate space for sockaddr */
359 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
361 /* if allocation failed drop packet */
363 s = splimp(); /* spl on queue manipulation */
364 IF_DROP(&ifp->if_snd);
369 bcopy(dst, m0->m_data, dst->sa_len);
373 if (tp->tun_flags & TUN_IFHEAD) {
374 /* Prepend the address family */
375 M_PREPEND(m0, 4, M_DONTWAIT);
377 /* if allocation failed drop packet */
379 s = splimp(); /* spl on queue manipulation */
380 IF_DROP(&ifp->if_snd);
385 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
388 if (dst->sa_family != AF_INET)
397 if (IF_QFULL(&ifp->if_snd)) {
398 IF_DROP(&ifp->if_snd);
401 ifp->if_collisions++;
404 ifp->if_obytes += m0->m_pkthdr.len;
405 IF_ENQUEUE(&ifp->if_snd, m0);
409 if (tp->tun_flags & TUN_RWAIT) {
410 tp->tun_flags &= ~TUN_RWAIT;
413 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
414 pgsigio(tp->tun_sigio, SIGIO, 0);
415 selwakeup(&tp->tun_rsel);
420 * the cdevsw interface is now pretty minimal.
423 tunioctl(dev, cmd, data, flag, p)
431 struct tun_softc *tp = dev->si_drv1;
432 struct tuninfo *tunp;
436 tunp = (struct tuninfo *)data;
437 if (tunp->mtu < IF_MINMTU)
439 tp->tun_if.if_mtu = tunp->mtu;
440 tp->tun_if.if_type = tunp->type;
441 tp->tun_if.if_baudrate = tunp->baudrate;
444 tunp = (struct tuninfo *)data;
445 tunp->mtu = tp->tun_if.if_mtu;
446 tunp->type = tp->tun_if.if_type;
447 tunp->baudrate = tp->tun_if.if_baudrate;
450 tundebug = *(int *)data;
453 *(int *)data = tundebug;
457 tp->tun_flags |= TUN_LMODE;
458 tp->tun_flags &= ~TUN_IFHEAD;
460 tp->tun_flags &= ~TUN_LMODE;
464 tp->tun_flags |= TUN_IFHEAD;
465 tp->tun_flags &= ~TUN_LMODE;
467 tp->tun_flags &= ~TUN_IFHEAD;
470 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
473 /* deny this if UP */
474 if (tp->tun_if.if_flags & IFF_UP)
477 switch (*(int *)data & ~IFF_MULTICAST) {
478 case IFF_POINTOPOINT:
480 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
481 tp->tun_if.if_flags |= *(int *)data;
488 tp->tun_pid = curproc->p_pid;
494 tp->tun_flags |= TUN_ASYNC;
496 tp->tun_flags &= ~TUN_ASYNC;
500 if (tp->tun_if.if_snd.ifq_head) {
501 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
502 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
503 *(int *)data += mb->m_len;
509 return (fsetown(*(int *)data, &tp->tun_sigio));
512 *(int *)data = fgetown(tp->tun_sigio);
515 /* This is deprecated, FIOSETOWN should be used instead. */
517 return (fsetown(-(*(int *)data), &tp->tun_sigio));
519 /* This is deprecated, FIOGETOWN should be used instead. */
521 *(int *)data = -fgetown(tp->tun_sigio);
531 * The cdevsw read interface - reads a packet at a time, or at
532 * least as much of a packet as can be read.
535 tunread(dev, uio, flag)
540 struct tun_softc *tp = dev->si_drv1;
541 struct ifnet *ifp = &tp->tun_if;
545 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
546 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
547 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
548 ifp->if_unit, tp->tun_flags);
552 tp->tun_flags &= ~TUN_RWAIT;
556 IF_DEQUEUE(&ifp->if_snd, m0);
558 if (flag & IO_NDELAY) {
562 tp->tun_flags |= TUN_RWAIT;
563 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
564 "tunread", 0)) != 0) {
572 while (m0 && uio->uio_resid > 0 && error == 0) {
573 len = min(uio->uio_resid, m0->m_len);
575 error = uiomove(mtod(m0, caddr_t), len, uio);
580 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
587 * the cdevsw write interface - an atomic write is a packet - or else!
590 tunwrite(dev, uio, flag)
595 struct tun_softc *tp = dev->si_drv1;
596 struct ifnet *ifp = &tp->tun_if;
597 struct mbuf *top, **mp, *m;
598 int error=0, tlen, mlen;
601 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
603 if (uio->uio_resid == 0)
606 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
607 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
611 tlen = uio->uio_resid;
613 /* get a header mbuf */
614 MGETHDR(m, M_DONTWAIT, MT_DATA);
621 while (error == 0 && uio->uio_resid > 0) {
622 m->m_len = min(mlen, uio->uio_resid);
623 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
626 if (uio->uio_resid > 0) {
627 MGET (m, M_DONTWAIT, MT_DATA);
642 top->m_pkthdr.len = tlen;
643 top->m_pkthdr.rcvif = ifp;
646 if (tp->tun_flags & TUN_IFHEAD) {
648 * Conveniently, we already have a 4-byte address
649 * family prepended to our packet !
650 * Inconveniently, it's in the wrong byte order !
652 if ((top = m_pullup(top, sizeof(family))) == NULL)
654 *mtod(top, u_int32_t *) =
655 ntohl(*mtod(top, u_int32_t *));
657 *mtod(top, u_int32_t *) =
658 htonl(*mtod(top, u_int32_t *));
661 * We need to prepend the address family as
662 * a four byte field. Cons up a dummy header
663 * to pacify bpf. This is safe because bpf
664 * will only read from the mbuf (i.e., it won't
665 * try to free it or keep a pointer to it).
668 uint32_t af = AF_INET;
672 m.m_data = (char *)⁡
678 if (tp->tun_flags & TUN_IFHEAD) {
679 if (top->m_len < sizeof(family) &&
680 (top = m_pullup(top, sizeof(family))) == NULL)
682 family = ntohl(*mtod(top, u_int32_t *));
683 m_adj(top, sizeof(family));
687 ifp->if_ibytes += top->m_pkthdr.len;
690 return family_enqueue(family, top);
694 * tunpoll - the poll interface, this is only useful on reads
695 * really. The write detect always returns true, write never blocks
696 * anyway, it either accepts the packet or drops it.
699 tunpoll(dev, events, p)
705 struct tun_softc *tp = dev->si_drv1;
706 struct ifnet *ifp = &tp->tun_if;
710 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
712 if (events & (POLLIN | POLLRDNORM)) {
713 if (ifp->if_snd.ifq_len > 0) {
714 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
715 ifp->if_unit, ifp->if_snd.ifq_len);
716 revents |= events & (POLLIN | POLLRDNORM);
718 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
720 selrecord(p, &tp->tun_rsel);
723 if (events & (POLLOUT | POLLWRNORM))
724 revents |= events & (POLLOUT | POLLWRNORM);