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
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/route.h>
41 #include <net/intrq.h>
44 #include <netinet/in.h>
49 #include <net/if_tunvar.h>
50 #include <net/if_tun.h>
52 static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
54 static void tunattach __P((void *));
55 PSEUDO_SET(tunattach, if_tun);
57 static void tuncreate __P((dev_t dev));
59 #define TUNDEBUG if (tundebug) printf
60 static int tundebug = 0;
61 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
63 static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
65 static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
66 static int tuninit __P((struct ifnet *));
68 static d_open_t tunopen;
69 static d_close_t tunclose;
70 static d_read_t tunread;
71 static d_write_t tunwrite;
72 static d_ioctl_t tunioctl;
73 static d_poll_t tunpoll;
76 static struct cdevsw tun_cdevsw = {
84 /* strategy */ nostrategy,
98 cdevsw_add(&tun_cdevsw);
105 struct tun_softc *sc;
108 dev = make_dev(&tun_cdevsw, minor(dev),
109 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
111 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
112 bzero(sc, sizeof *sc);
113 sc->tun_flags = TUN_INITED;
116 ifp->if_unit = lminor(dev);
117 ifp->if_name = "tun";
118 ifp->if_mtu = TUNMTU;
119 ifp->if_ioctl = tunifioctl;
120 ifp->if_output = tunoutput;
121 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
122 ifp->if_snd.ifq_maxlen = ifqmaxlen;
125 bpfattach(ifp, DLT_NULL, sizeof(u_int));
130 * tunnel open - must be superuser & the device must be
134 tunopen(dev, flag, mode, p)
140 struct tun_softc *tp;
152 if (tp->tun_flags & TUN_OPEN)
154 tp->tun_pid = p->p_pid;
156 tp->tun_flags |= TUN_OPEN;
157 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
162 * tunclose - close the device - mark i/f down & delete
166 tunclose(dev, foo, bar, p)
173 struct tun_softc *tp;
180 tp->tun_flags &= ~TUN_OPEN;
184 * junk all pending output
188 IF_DEQUEUE(&ifp->if_snd, m);
194 if (ifp->if_flags & IFF_UP) {
200 if (ifp->if_flags & IFF_RUNNING) {
201 register struct ifaddr *ifa;
204 /* find internet addresses and delete routes */
205 for (ifa = ifp->if_addrhead.tqh_first; ifa;
206 ifa = ifa->ifa_link.tqe_next)
207 if (ifa->ifa_addr->sa_family == AF_INET)
208 rtinit(ifa, (int)RTM_DELETE,
209 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
210 ifp->if_flags &= ~IFF_RUNNING;
214 funsetown(tp->tun_sigio);
215 selwakeup(&tp->tun_rsel);
217 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
225 struct tun_softc *tp = ifp->if_softc;
226 register struct ifaddr *ifa;
228 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
230 ifp->if_flags |= IFF_UP | IFF_RUNNING;
231 getmicrotime(&ifp->if_lastchange);
233 for (ifa = ifp->if_addrhead.tqh_first; ifa;
234 ifa = ifa->ifa_link.tqe_next) {
236 if (ifa->ifa_addr->sa_family == AF_INET) {
237 struct sockaddr_in *si;
239 si = (struct sockaddr_in *)ifa->ifa_addr;
240 if (si && si->sin_addr.s_addr)
241 tp->tun_flags |= TUN_IASET;
243 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
244 if (si && si->sin_addr.s_addr)
245 tp->tun_flags |= TUN_DSTADDR;
253 * Process an ioctl request.
256 tunifioctl(ifp, cmd, data)
261 struct ifreq *ifr = (struct ifreq *)data;
262 struct tun_softc *tp = ifp->if_softc;
269 ifs = (struct ifstat *)data;
271 sprintf(ifs->ascii + strlen(ifs->ascii),
272 "\tOpened by PID %d\n", tp->tun_pid);
276 TUNDEBUG("%s%d: address set\n",
277 ifp->if_name, ifp->if_unit);
281 TUNDEBUG("%s%d: destination address set\n",
282 ifp->if_name, ifp->if_unit);
285 ifp->if_mtu = ifr->ifr_mtu;
286 TUNDEBUG("%s%d: mtu set\n",
287 ifp->if_name, ifp->if_unit);
302 * tunoutput - queue packets from higher level ready to put out.
305 tunoutput(ifp, m0, dst, rt)
308 struct sockaddr *dst;
311 struct tun_softc *tp = ifp->if_softc;
314 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
316 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
317 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
318 ifp->if_unit, tp->tun_flags);
323 /* BPF write needs to be handled specially */
324 if (dst->sa_family == AF_UNSPEC) {
325 dst->sa_family = *(mtod(m0, int *));
326 m0->m_len -= sizeof(int);
327 m0->m_pkthdr.len -= sizeof(int);
328 m0->m_data += sizeof(int);
333 * We need to prepend the address family as
334 * a four byte field. Cons up a dummy header
335 * to pacify bpf. This is safe because bpf
336 * will only read from the mbuf (i.e., it won't
337 * try to free it or keep a pointer to it).
340 u_int af = dst->sa_family;
344 m.m_data = (char *)⁡
349 /* prepend sockaddr? this may abort if the mbuf allocation fails */
350 if (tp->tun_flags & TUN_LMODE) {
351 /* allocate space for sockaddr */
352 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
354 /* if allocation failed drop packet */
356 s = splimp(); /* spl on queue manipulation */
357 IF_DROP(&ifp->if_snd);
362 bcopy(dst, m0->m_data, dst->sa_len);
366 if (tp->tun_flags & TUN_IFHEAD) {
367 /* Prepend the address family */
368 M_PREPEND(m0, 4, M_DONTWAIT);
370 /* if allocation failed drop packet */
372 s = splimp(); /* spl on queue manipulation */
373 IF_DROP(&ifp->if_snd);
378 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
381 if (dst->sa_family != AF_INET)
390 if (IF_QFULL(&ifp->if_snd)) {
391 IF_DROP(&ifp->if_snd);
394 ifp->if_collisions++;
397 ifp->if_obytes += m0->m_pkthdr.len;
398 IF_ENQUEUE(&ifp->if_snd, m0);
402 if (tp->tun_flags & TUN_RWAIT) {
403 tp->tun_flags &= ~TUN_RWAIT;
406 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
407 pgsigio(tp->tun_sigio, SIGIO, 0);
408 selwakeup(&tp->tun_rsel);
413 * the cdevsw interface is now pretty minimal.
416 tunioctl(dev, cmd, data, flag, p)
424 struct tun_softc *tp = dev->si_drv1;
425 struct tuninfo *tunp;
429 tunp = (struct tuninfo *)data;
430 if (tunp->mtu < IF_MINMTU)
432 tp->tun_if.if_mtu = tunp->mtu;
433 tp->tun_if.if_type = tunp->type;
434 tp->tun_if.if_baudrate = tunp->baudrate;
437 tunp = (struct tuninfo *)data;
438 tunp->mtu = tp->tun_if.if_mtu;
439 tunp->type = tp->tun_if.if_type;
440 tunp->baudrate = tp->tun_if.if_baudrate;
443 tundebug = *(int *)data;
446 *(int *)data = tundebug;
450 tp->tun_flags |= TUN_LMODE;
451 tp->tun_flags &= ~TUN_IFHEAD;
453 tp->tun_flags &= ~TUN_LMODE;
457 tp->tun_flags |= TUN_IFHEAD;
458 tp->tun_flags &= ~TUN_LMODE;
460 tp->tun_flags &= ~TUN_IFHEAD;
463 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
466 /* deny this if UP */
467 if (tp->tun_if.if_flags & IFF_UP)
470 switch (*(int *)data) {
471 case IFF_POINTOPOINT:
472 tp->tun_if.if_flags |= IFF_POINTOPOINT;
473 tp->tun_if.if_flags &= ~IFF_BROADCAST;
476 tp->tun_if.if_flags &= ~IFF_POINTOPOINT;
477 tp->tun_if.if_flags |= IFF_BROADCAST;
484 tp->tun_pid = curproc->p_pid;
490 tp->tun_flags |= TUN_ASYNC;
492 tp->tun_flags &= ~TUN_ASYNC;
496 if (tp->tun_if.if_snd.ifq_head) {
497 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
498 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
499 *(int *)data += mb->m_len;
505 return (fsetown(*(int *)data, &tp->tun_sigio));
508 *(int *)data = fgetown(tp->tun_sigio);
511 /* This is deprecated, FIOSETOWN should be used instead. */
513 return (fsetown(-(*(int *)data), &tp->tun_sigio));
515 /* This is deprecated, FIOGETOWN should be used instead. */
517 *(int *)data = -fgetown(tp->tun_sigio);
527 * The cdevsw read interface - reads a packet at a time, or at
528 * least as much of a packet as can be read.
531 tunread(dev, uio, flag)
536 struct tun_softc *tp = dev->si_drv1;
537 struct ifnet *ifp = &tp->tun_if;
541 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
542 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
543 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
544 ifp->if_unit, tp->tun_flags);
548 tp->tun_flags &= ~TUN_RWAIT;
552 IF_DEQUEUE(&ifp->if_snd, m0);
554 if (flag & IO_NDELAY) {
558 tp->tun_flags |= TUN_RWAIT;
559 if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
560 "tunread", 0)) != 0) {
568 while (m0 && uio->uio_resid > 0 && error == 0) {
569 len = min(uio->uio_resid, m0->m_len);
572 error = uiomove(mtod(m0, caddr_t), len, uio);
578 TUNDEBUG("Dropping mbuf\n");
585 * the cdevsw write interface - an atomic write is a packet - or else!
588 tunwrite(dev, uio, flag)
593 struct tun_softc *tp = dev->si_drv1;
594 struct ifnet *ifp = &tp->tun_if;
595 struct mbuf *top, **mp, *m;
596 int error=0, tlen, mlen;
599 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
601 if (uio->uio_resid == 0)
604 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
605 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
609 tlen = uio->uio_resid;
611 /* get a header mbuf */
612 MGETHDR(m, M_DONTWAIT, MT_DATA);
619 while (error == 0 && uio->uio_resid > 0) {
620 m->m_len = min(mlen, uio->uio_resid);
621 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
624 if (uio->uio_resid > 0) {
625 MGET (m, M_DONTWAIT, MT_DATA);
640 top->m_pkthdr.len = tlen;
641 top->m_pkthdr.rcvif = ifp;
644 if (tp->tun_flags & TUN_IFHEAD)
646 * Conveniently, we already have a 4-byte address
647 * family prepended to our packet !
652 * We need to prepend the address family as
653 * a four byte field. Cons up a dummy header
654 * to pacify bpf. This is safe because bpf
655 * will only read from the mbuf (i.e., it won't
656 * try to free it or keep a pointer to it).
663 m.m_data = (char *)⁡
669 if (tp->tun_flags & TUN_IFHEAD) {
670 if (top->m_len < sizeof(family) &&
671 (top = m_pullup(top, sizeof(family))) == NULL)
673 family = ntohl(*mtod(top, u_int32_t *));
674 m_adj(top, sizeof(family));
678 ifp->if_ibytes += top->m_pkthdr.len;
681 return family_enqueue(family, top);
685 * tunpoll - the poll interface, this is only useful on reads
686 * really. The write detect always returns true, write never blocks
687 * anyway, it either accepts the packet or drops it.
690 tunpoll(dev, events, p)
696 struct tun_softc *tp = dev->si_drv1;
697 struct ifnet *ifp = &tp->tun_if;
701 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
703 if (events & (POLLIN | POLLRDNORM)) {
704 if (ifp->if_snd.ifq_len > 0) {
705 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
706 ifp->if_unit, ifp->if_snd.ifq_len);
707 revents |= events & (POLLIN | POLLRDNORM);
709 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
711 selrecord(p, &tp->tun_rsel);
714 if (events & (POLLOUT | POLLWRNORM))
715 revents |= events & (POLLOUT | POLLWRNORM);