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.5 2003/07/19 21:14:43 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_t dev, int flag, int mode, struct thread *td)
140 struct tun_softc *tp;
143 KKASSERT(td->td_proc);
144 if ((error = suser(td)) != NULL)
152 if (tp->tun_flags & TUN_OPEN)
154 tp->tun_pid = td->td_proc->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_t dev, int foo, int bar, struct thread *td)
169 struct tun_softc *tp;
176 tp->tun_flags &= ~TUN_OPEN;
180 * junk all pending output
184 IF_DEQUEUE(&ifp->if_snd, m);
190 if (ifp->if_flags & IFF_UP) {
196 if (ifp->if_flags & IFF_RUNNING) {
197 register struct ifaddr *ifa;
200 /* find internet addresses and delete routes */
201 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
202 if (ifa->ifa_addr->sa_family == AF_INET)
203 rtinit(ifa, (int)RTM_DELETE,
204 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
205 ifp->if_flags &= ~IFF_RUNNING;
209 funsetown(tp->tun_sigio);
210 selwakeup(&tp->tun_rsel);
212 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
220 struct tun_softc *tp = ifp->if_softc;
221 register struct ifaddr *ifa;
224 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
226 ifp->if_flags |= IFF_UP | IFF_RUNNING;
227 getmicrotime(&ifp->if_lastchange);
229 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
230 ifa = TAILQ_NEXT(ifa, ifa_link)) {
231 if (ifa->ifa_addr == NULL)
233 /* XXX: Should maybe return straight off? */
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->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;
254 * Process an ioctl request.
257 tunifioctl(ifp, cmd, data)
262 struct ifreq *ifr = (struct ifreq *)data;
263 struct tun_softc *tp = ifp->if_softc;
270 ifs = (struct ifstat *)data;
272 sprintf(ifs->ascii + strlen(ifs->ascii),
273 "\tOpened by PID %d\n", tp->tun_pid);
276 error = tuninit(ifp);
277 TUNDEBUG("%s%d: address set, error=%d\n",
278 ifp->if_name, ifp->if_unit, error);
281 error = tuninit(ifp);
282 TUNDEBUG("%s%d: destination address set, error=%d\n",
283 ifp->if_name, ifp->if_unit, error);
286 ifp->if_mtu = ifr->ifr_mtu;
287 TUNDEBUG("%s%d: mtu set\n",
288 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 uint32_t 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_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
419 struct tun_softc *tp = dev->si_drv1;
420 struct tuninfo *tunp;
424 tunp = (struct tuninfo *)data;
425 if (tunp->mtu < IF_MINMTU)
427 tp->tun_if.if_mtu = tunp->mtu;
428 tp->tun_if.if_type = tunp->type;
429 tp->tun_if.if_baudrate = tunp->baudrate;
432 tunp = (struct tuninfo *)data;
433 tunp->mtu = tp->tun_if.if_mtu;
434 tunp->type = tp->tun_if.if_type;
435 tunp->baudrate = tp->tun_if.if_baudrate;
438 tundebug = *(int *)data;
441 *(int *)data = tundebug;
445 tp->tun_flags |= TUN_LMODE;
446 tp->tun_flags &= ~TUN_IFHEAD;
448 tp->tun_flags &= ~TUN_LMODE;
452 tp->tun_flags |= TUN_IFHEAD;
453 tp->tun_flags &= ~TUN_LMODE;
455 tp->tun_flags &= ~TUN_IFHEAD;
458 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
461 /* deny this if UP */
462 if (tp->tun_if.if_flags & IFF_UP)
465 switch (*(int *)data & ~IFF_MULTICAST) {
466 case IFF_POINTOPOINT:
468 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
469 tp->tun_if.if_flags |= *(int *)data;
476 tp->tun_pid = curproc->p_pid;
482 tp->tun_flags |= TUN_ASYNC;
484 tp->tun_flags &= ~TUN_ASYNC;
488 if (tp->tun_if.if_snd.ifq_head) {
489 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
490 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
491 *(int *)data += mb->m_len;
497 return (fsetown(*(int *)data, &tp->tun_sigio));
500 *(int *)data = fgetown(tp->tun_sigio);
503 /* This is deprecated, FIOSETOWN should be used instead. */
505 return (fsetown(-(*(int *)data), &tp->tun_sigio));
507 /* This is deprecated, FIOGETOWN should be used instead. */
509 *(int *)data = -fgetown(tp->tun_sigio);
519 * The cdevsw read interface - reads a packet at a time, or at
520 * least as much of a packet as can be read.
523 tunread(dev, uio, flag)
528 struct tun_softc *tp = dev->si_drv1;
529 struct ifnet *ifp = &tp->tun_if;
533 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
534 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
535 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
536 ifp->if_unit, tp->tun_flags);
540 tp->tun_flags &= ~TUN_RWAIT;
544 IF_DEQUEUE(&ifp->if_snd, m0);
546 if (flag & IO_NDELAY) {
550 tp->tun_flags |= TUN_RWAIT;
551 if((error = tsleep((caddr_t)tp, PCATCH,
552 "tunread", 0)) != 0) {
560 while (m0 && uio->uio_resid > 0 && error == 0) {
561 len = min(uio->uio_resid, m0->m_len);
563 error = uiomove(mtod(m0, caddr_t), len, uio);
568 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
575 * the cdevsw write interface - an atomic write is a packet - or else!
578 tunwrite(dev, uio, flag)
583 struct tun_softc *tp = dev->si_drv1;
584 struct ifnet *ifp = &tp->tun_if;
585 struct mbuf *top, **mp, *m;
586 int error=0, tlen, mlen;
589 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
591 if (uio->uio_resid == 0)
594 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
595 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
599 tlen = uio->uio_resid;
601 /* get a header mbuf */
602 MGETHDR(m, M_DONTWAIT, MT_DATA);
609 while (error == 0 && uio->uio_resid > 0) {
610 m->m_len = min(mlen, uio->uio_resid);
611 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
614 if (uio->uio_resid > 0) {
615 MGET (m, M_DONTWAIT, MT_DATA);
630 top->m_pkthdr.len = tlen;
631 top->m_pkthdr.rcvif = ifp;
634 if (tp->tun_flags & TUN_IFHEAD) {
636 * Conveniently, we already have a 4-byte address
637 * family prepended to our packet !
638 * Inconveniently, it's in the wrong byte order !
640 if ((top = m_pullup(top, sizeof(family))) == NULL)
642 *mtod(top, u_int32_t *) =
643 ntohl(*mtod(top, u_int32_t *));
645 *mtod(top, u_int32_t *) =
646 htonl(*mtod(top, u_int32_t *));
649 * We need to prepend the address family as
650 * a four byte field. Cons up a dummy header
651 * to pacify bpf. This is safe because bpf
652 * will only read from the mbuf (i.e., it won't
653 * try to free it or keep a pointer to it).
656 uint32_t af = AF_INET;
660 m.m_data = (char *)⁡
666 if (tp->tun_flags & TUN_IFHEAD) {
667 if (top->m_len < sizeof(family) &&
668 (top = m_pullup(top, sizeof(family))) == NULL)
670 family = ntohl(*mtod(top, u_int32_t *));
671 m_adj(top, sizeof(family));
675 ifp->if_ibytes += top->m_pkthdr.len;
678 return family_enqueue(family, top);
682 * tunpoll - the poll interface, this is only useful on reads
683 * really. The write detect always returns true, write never blocks
684 * anyway, it either accepts the packet or drops it.
687 tunpoll(dev_t dev, int events, struct thread *td)
690 struct tun_softc *tp = dev->si_drv1;
691 struct ifnet *ifp = &tp->tun_if;
695 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
697 if (events & (POLLIN | POLLRDNORM)) {
698 if (ifp->if_snd.ifq_len > 0) {
699 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
700 ifp->if_unit, ifp->if_snd.ifq_len);
701 revents |= events & (POLLIN | POLLRDNORM);
703 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
705 selrecord(td, &tp->tun_rsel);
708 if (events & (POLLOUT | POLLWRNORM))
709 revents |= events & (POLLOUT | POLLWRNORM);