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 $
21 #include <sys/param.h>
23 #include <sys/systm.h>
25 #include <sys/socket.h>
26 #include <sys/filio.h>
27 #include <sys/sockio.h>
28 #include <sys/ttycom.h>
30 #include <sys/signalvar.h>
31 #include <sys/filedesc.h>
32 #include <sys/kernel.h>
33 #include <sys/sysctl.h>
36 #include <sys/vnode.h>
37 #include <sys/malloc.h>
40 #include <net/if_types.h>
41 #include <net/route.h>
42 #include <net/intrq.h>
45 #include <netinet/in.h>
50 #include <net/if_tunvar.h>
51 #include <net/if_tun.h>
53 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
55 static void tunattach __P((void *));
56 PSEUDO_SET(tunattach, if_tun);
58 static void tuncreate __P((dev_t dev));
60 #define TUNDEBUG if (tundebug) printf
61 static int tundebug = 0;
62 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
64 static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
66 static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
67 static int tuninit __P((struct ifnet *));
69 static d_open_t tunopen;
70 static d_close_t tunclose;
71 static d_read_t tunread;
72 static d_write_t tunwrite;
73 static d_ioctl_t tunioctl;
74 static d_poll_t tunpoll;
77 static struct cdevsw tun_cdevsw = {
85 /* strategy */ nostrategy,
99 cdevsw_add(&tun_cdevsw);
106 struct tun_softc *sc;
109 dev = make_dev(&tun_cdevsw, minor(dev),
110 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
112 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
113 bzero(sc, sizeof *sc);
114 sc->tun_flags = TUN_INITED;
117 ifp->if_unit = lminor(dev);
118 ifp->if_name = "tun";
119 ifp->if_mtu = TUNMTU;
120 ifp->if_ioctl = tunifioctl;
121 ifp->if_output = tunoutput;
122 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
123 ifp->if_type = IFT_PPP;
124 ifp->if_snd.ifq_maxlen = ifqmaxlen;
127 bpfattach(ifp, DLT_NULL, sizeof(u_int));
132 * tunnel open - must be superuser & the device must be
136 tunopen(dev, flag, mode, p)
142 struct tun_softc *tp;
154 if (tp->tun_flags & TUN_OPEN)
156 tp->tun_pid = p->p_pid;
158 tp->tun_flags |= TUN_OPEN;
159 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
164 * tunclose - close the device - mark i/f down & delete
168 tunclose(dev, foo, bar, p)
175 struct tun_softc *tp;
182 tp->tun_flags &= ~TUN_OPEN;
186 * junk all pending output
190 IF_DEQUEUE(&ifp->if_snd, m);
196 if (ifp->if_flags & IFF_UP) {
202 if (ifp->if_flags & IFF_RUNNING) {
203 register struct ifaddr *ifa;
206 /* find internet addresses and delete routes */
207 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
208 if (ifa->ifa_addr->sa_family == AF_INET)
209 rtinit(ifa, (int)RTM_DELETE,
210 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
211 ifp->if_flags &= ~IFF_RUNNING;
215 funsetown(tp->tun_sigio);
216 selwakeup(&tp->tun_rsel);
218 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
226 struct tun_softc *tp = ifp->if_softc;
227 register struct ifaddr *ifa;
230 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
232 ifp->if_flags |= IFF_UP | IFF_RUNNING;
233 getmicrotime(&ifp->if_lastchange);
235 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
236 ifa = TAILQ_NEXT(ifa, ifa_link)) {
237 if (ifa->ifa_addr == NULL)
239 /* XXX: Should maybe return straight off? */
242 if (ifa->ifa_addr->sa_family == AF_INET) {
243 struct sockaddr_in *si;
245 si = (struct sockaddr_in *)ifa->ifa_addr;
246 if (si->sin_addr.s_addr)
247 tp->tun_flags |= TUN_IASET;
249 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
250 if (si && si->sin_addr.s_addr)
251 tp->tun_flags |= TUN_DSTADDR;
260 * Process an ioctl request.
263 tunifioctl(ifp, cmd, data)
268 struct ifreq *ifr = (struct ifreq *)data;
269 struct tun_softc *tp = ifp->if_softc;
276 ifs = (struct ifstat *)data;
278 sprintf(ifs->ascii + strlen(ifs->ascii),
279 "\tOpened by PID %d\n", tp->tun_pid);
282 error = tuninit(ifp);
283 TUNDEBUG("%s%d: address set, error=%d\n",
284 ifp->if_name, ifp->if_unit, error);
287 error = tuninit(ifp);
288 TUNDEBUG("%s%d: destination address set, error=%d\n",
289 ifp->if_name, ifp->if_unit, error);
292 ifp->if_mtu = ifr->ifr_mtu;
293 TUNDEBUG("%s%d: mtu set\n",
294 ifp->if_name, ifp->if_unit);
308 * tunoutput - queue packets from higher level ready to put out.
311 tunoutput(ifp, m0, dst, rt)
314 struct sockaddr *dst;
317 struct tun_softc *tp = ifp->if_softc;
320 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
322 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
323 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
324 ifp->if_unit, tp->tun_flags);
329 /* BPF write needs to be handled specially */
330 if (dst->sa_family == AF_UNSPEC) {
331 dst->sa_family = *(mtod(m0, int *));
332 m0->m_len -= sizeof(int);
333 m0->m_pkthdr.len -= sizeof(int);
334 m0->m_data += sizeof(int);
339 * We need to prepend the address family as
340 * a four byte field. Cons up a dummy header
341 * to pacify bpf. This is safe because bpf
342 * will only read from the mbuf (i.e., it won't
343 * try to free it or keep a pointer to it).
346 uint32_t af = dst->sa_family;
350 m.m_data = (char *)⁡
355 /* prepend sockaddr? this may abort if the mbuf allocation fails */
356 if (tp->tun_flags & TUN_LMODE) {
357 /* allocate space for sockaddr */
358 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
360 /* if allocation failed drop packet */
362 s = splimp(); /* spl on queue manipulation */
363 IF_DROP(&ifp->if_snd);
368 bcopy(dst, m0->m_data, dst->sa_len);
372 if (tp->tun_flags & TUN_IFHEAD) {
373 /* Prepend the address family */
374 M_PREPEND(m0, 4, M_DONTWAIT);
376 /* if allocation failed drop packet */
378 s = splimp(); /* spl on queue manipulation */
379 IF_DROP(&ifp->if_snd);
384 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
387 if (dst->sa_family != AF_INET)
396 if (IF_QFULL(&ifp->if_snd)) {
397 IF_DROP(&ifp->if_snd);
400 ifp->if_collisions++;
403 ifp->if_obytes += m0->m_pkthdr.len;
404 IF_ENQUEUE(&ifp->if_snd, m0);
408 if (tp->tun_flags & TUN_RWAIT) {
409 tp->tun_flags &= ~TUN_RWAIT;
412 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
413 pgsigio(tp->tun_sigio, SIGIO, 0);
414 selwakeup(&tp->tun_rsel);
419 * the cdevsw interface is now pretty minimal.
422 tunioctl(dev, cmd, data, flag, p)
430 struct tun_softc *tp = dev->si_drv1;
431 struct tuninfo *tunp;
435 tunp = (struct tuninfo *)data;
436 if (tunp->mtu < IF_MINMTU)
438 tp->tun_if.if_mtu = tunp->mtu;
439 tp->tun_if.if_type = tunp->type;
440 tp->tun_if.if_baudrate = tunp->baudrate;
443 tunp = (struct tuninfo *)data;
444 tunp->mtu = tp->tun_if.if_mtu;
445 tunp->type = tp->tun_if.if_type;
446 tunp->baudrate = tp->tun_if.if_baudrate;
449 tundebug = *(int *)data;
452 *(int *)data = tundebug;
456 tp->tun_flags |= TUN_LMODE;
457 tp->tun_flags &= ~TUN_IFHEAD;
459 tp->tun_flags &= ~TUN_LMODE;
463 tp->tun_flags |= TUN_IFHEAD;
464 tp->tun_flags &= ~TUN_LMODE;
466 tp->tun_flags &= ~TUN_IFHEAD;
469 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
472 /* deny this if UP */
473 if (tp->tun_if.if_flags & IFF_UP)
476 switch (*(int *)data & ~IFF_MULTICAST) {
477 case IFF_POINTOPOINT:
479 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
480 tp->tun_if.if_flags |= *(int *)data;
487 tp->tun_pid = curproc->p_pid;
493 tp->tun_flags |= TUN_ASYNC;
495 tp->tun_flags &= ~TUN_ASYNC;
499 if (tp->tun_if.if_snd.ifq_head) {
500 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
501 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
502 *(int *)data += mb->m_len;
508 return (fsetown(*(int *)data, &tp->tun_sigio));
511 *(int *)data = fgetown(tp->tun_sigio);
514 /* This is deprecated, FIOSETOWN should be used instead. */
516 return (fsetown(-(*(int *)data), &tp->tun_sigio));
518 /* This is deprecated, FIOGETOWN should be used instead. */
520 *(int *)data = -fgetown(tp->tun_sigio);
530 * The cdevsw read interface - reads a packet at a time, or at
531 * least as much of a packet as can be read.
534 tunread(dev, uio, flag)
539 struct tun_softc *tp = dev->si_drv1;
540 struct ifnet *ifp = &tp->tun_if;
544 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
545 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
546 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
547 ifp->if_unit, tp->tun_flags);
551 tp->tun_flags &= ~TUN_RWAIT;
555 IF_DEQUEUE(&ifp->if_snd, m0);
557 if (flag & IO_NDELAY) {
561 tp->tun_flags |= TUN_RWAIT;
562 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
563 "tunread", 0)) != 0) {
571 while (m0 && uio->uio_resid > 0 && error == 0) {
572 len = min(uio->uio_resid, m0->m_len);
574 error = uiomove(mtod(m0, caddr_t), len, uio);
579 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
586 * the cdevsw write interface - an atomic write is a packet - or else!
589 tunwrite(dev, uio, flag)
594 struct tun_softc *tp = dev->si_drv1;
595 struct ifnet *ifp = &tp->tun_if;
596 struct mbuf *top, **mp, *m;
597 int error=0, tlen, mlen;
600 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
602 if (uio->uio_resid == 0)
605 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
606 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
610 tlen = uio->uio_resid;
612 /* get a header mbuf */
613 MGETHDR(m, M_DONTWAIT, MT_DATA);
620 while (error == 0 && uio->uio_resid > 0) {
621 m->m_len = min(mlen, uio->uio_resid);
622 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
625 if (uio->uio_resid > 0) {
626 MGET (m, M_DONTWAIT, MT_DATA);
641 top->m_pkthdr.len = tlen;
642 top->m_pkthdr.rcvif = ifp;
645 if (tp->tun_flags & TUN_IFHEAD) {
647 * Conveniently, we already have a 4-byte address
648 * family prepended to our packet !
649 * Inconveniently, it's in the wrong byte order !
651 if ((top = m_pullup(top, sizeof(family))) == NULL)
653 *mtod(top, u_int32_t *) =
654 ntohl(*mtod(top, u_int32_t *));
656 *mtod(top, u_int32_t *) =
657 htonl(*mtod(top, u_int32_t *));
660 * We need to prepend the address family as
661 * a four byte field. Cons up a dummy header
662 * to pacify bpf. This is safe because bpf
663 * will only read from the mbuf (i.e., it won't
664 * try to free it or keep a pointer to it).
667 uint32_t af = AF_INET;
671 m.m_data = (char *)⁡
677 if (tp->tun_flags & TUN_IFHEAD) {
678 if (top->m_len < sizeof(family) &&
679 (top = m_pullup(top, sizeof(family))) == NULL)
681 family = ntohl(*mtod(top, u_int32_t *));
682 m_adj(top, sizeof(family));
686 ifp->if_ibytes += top->m_pkthdr.len;
689 return family_enqueue(family, top);
693 * tunpoll - the poll interface, this is only useful on reads
694 * really. The write detect always returns true, write never blocks
695 * anyway, it either accepts the packet or drops it.
698 tunpoll(dev, events, p)
704 struct tun_softc *tp = dev->si_drv1;
705 struct ifnet *ifp = &tp->tun_if;
709 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
711 if (events & (POLLIN | POLLRDNORM)) {
712 if (ifp->if_snd.ifq_len > 0) {
713 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
714 ifp->if_unit, ifp->if_snd.ifq_len);
715 revents |= events & (POLLIN | POLLRDNORM);
717 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
719 selrecord(p, &tp->tun_rsel);
722 if (events & (POLLOUT | POLLWRNORM))
723 revents |= events & (POLLOUT | POLLWRNORM);