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.7 2003/07/26 20:19:33 rob 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 = {
92 /* strategy */ nostrategy,
102 cdevsw_add(&tun_cdevsw);
109 struct tun_softc *sc;
112 dev = make_dev(&tun_cdevsw, minor(dev),
113 UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
115 MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
116 bzero(sc, sizeof *sc);
117 sc->tun_flags = TUN_INITED;
120 ifp->if_unit = lminor(dev);
121 ifp->if_name = "tun";
122 ifp->if_mtu = TUNMTU;
123 ifp->if_ioctl = tunifioctl;
124 ifp->if_output = tunoutput;
125 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
126 ifp->if_type = IFT_PPP;
127 ifp->if_snd.ifq_maxlen = ifqmaxlen;
130 bpfattach(ifp, DLT_NULL, sizeof(u_int));
135 * tunnel open - must be superuser & the device must be
139 tunopen(dev_t dev, int flag, int mode, struct thread *td)
142 struct tun_softc *tp;
145 KKASSERT(td->td_proc);
146 if ((error = suser(td)) != NULL)
154 if (tp->tun_flags & TUN_OPEN)
156 tp->tun_pid = td->td_proc->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_t dev, int foo, int bar, struct thread *td)
171 struct tun_softc *tp;
178 tp->tun_flags &= ~TUN_OPEN;
182 * junk all pending output
186 IF_DEQUEUE(&ifp->if_snd, m);
192 if (ifp->if_flags & IFF_UP) {
198 if (ifp->if_flags & IFF_RUNNING) {
202 /* find internet addresses and delete routes */
203 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
204 if (ifa->ifa_addr->sa_family == AF_INET)
205 rtinit(ifa, (int)RTM_DELETE,
206 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
207 ifp->if_flags &= ~IFF_RUNNING;
211 funsetown(tp->tun_sigio);
212 selwakeup(&tp->tun_rsel);
214 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
222 struct tun_softc *tp = ifp->if_softc;
226 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
228 ifp->if_flags |= IFF_UP | IFF_RUNNING;
229 getmicrotime(&ifp->if_lastchange);
231 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
232 ifa = TAILQ_NEXT(ifa, ifa_link)) {
233 if (ifa->ifa_addr == NULL)
235 /* XXX: Should maybe return straight off? */
238 if (ifa->ifa_addr->sa_family == AF_INET) {
239 struct sockaddr_in *si;
241 si = (struct sockaddr_in *)ifa->ifa_addr;
242 if (si->sin_addr.s_addr)
243 tp->tun_flags |= TUN_IASET;
245 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
246 if (si && si->sin_addr.s_addr)
247 tp->tun_flags |= TUN_DSTADDR;
256 * Process an ioctl request.
259 tunifioctl(ifp, cmd, data)
264 struct ifreq *ifr = (struct ifreq *)data;
265 struct tun_softc *tp = ifp->if_softc;
272 ifs = (struct ifstat *)data;
274 sprintf(ifs->ascii + strlen(ifs->ascii),
275 "\tOpened by PID %d\n", tp->tun_pid);
278 error = tuninit(ifp);
279 TUNDEBUG("%s%d: address set, error=%d\n",
280 ifp->if_name, ifp->if_unit, error);
283 error = tuninit(ifp);
284 TUNDEBUG("%s%d: destination address set, error=%d\n",
285 ifp->if_name, ifp->if_unit, error);
288 ifp->if_mtu = ifr->ifr_mtu;
289 TUNDEBUG("%s%d: mtu set\n",
290 ifp->if_name, ifp->if_unit);
304 * tunoutput - queue packets from higher level ready to put out.
307 tunoutput(ifp, m0, dst, rt)
310 struct sockaddr *dst;
313 struct tun_softc *tp = ifp->if_softc;
316 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
318 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
319 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
320 ifp->if_unit, tp->tun_flags);
325 /* BPF write needs to be handled specially */
326 if (dst->sa_family == AF_UNSPEC) {
327 dst->sa_family = *(mtod(m0, int *));
328 m0->m_len -= sizeof(int);
329 m0->m_pkthdr.len -= sizeof(int);
330 m0->m_data += sizeof(int);
335 * We need to prepend the address family as
336 * a four byte field. Cons up a dummy header
337 * to pacify bpf. This is safe because bpf
338 * will only read from the mbuf (i.e., it won't
339 * try to free it or keep a pointer to it).
342 uint32_t af = dst->sa_family;
346 m.m_data = (char *)⁡
351 /* prepend sockaddr? this may abort if the mbuf allocation fails */
352 if (tp->tun_flags & TUN_LMODE) {
353 /* allocate space for sockaddr */
354 M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
356 /* if allocation failed drop packet */
358 s = splimp(); /* spl on queue manipulation */
359 IF_DROP(&ifp->if_snd);
364 bcopy(dst, m0->m_data, dst->sa_len);
368 if (tp->tun_flags & TUN_IFHEAD) {
369 /* Prepend the address family */
370 M_PREPEND(m0, 4, M_DONTWAIT);
372 /* if allocation failed drop packet */
374 s = splimp(); /* spl on queue manipulation */
375 IF_DROP(&ifp->if_snd);
380 *(u_int32_t *)m0->m_data = htonl(dst->sa_family);
383 if (dst->sa_family != AF_INET)
392 if (IF_QFULL(&ifp->if_snd)) {
393 IF_DROP(&ifp->if_snd);
396 ifp->if_collisions++;
399 ifp->if_obytes += m0->m_pkthdr.len;
400 IF_ENQUEUE(&ifp->if_snd, m0);
404 if (tp->tun_flags & TUN_RWAIT) {
405 tp->tun_flags &= ~TUN_RWAIT;
408 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
409 pgsigio(tp->tun_sigio, SIGIO, 0);
410 selwakeup(&tp->tun_rsel);
415 * the cdevsw interface is now pretty minimal.
418 tunioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
421 struct tun_softc *tp = dev->si_drv1;
422 struct tuninfo *tunp;
426 tunp = (struct tuninfo *)data;
427 if (tunp->mtu < IF_MINMTU)
429 tp->tun_if.if_mtu = tunp->mtu;
430 tp->tun_if.if_type = tunp->type;
431 tp->tun_if.if_baudrate = tunp->baudrate;
434 tunp = (struct tuninfo *)data;
435 tunp->mtu = tp->tun_if.if_mtu;
436 tunp->type = tp->tun_if.if_type;
437 tunp->baudrate = tp->tun_if.if_baudrate;
440 tundebug = *(int *)data;
443 *(int *)data = tundebug;
447 tp->tun_flags |= TUN_LMODE;
448 tp->tun_flags &= ~TUN_IFHEAD;
450 tp->tun_flags &= ~TUN_LMODE;
454 tp->tun_flags |= TUN_IFHEAD;
455 tp->tun_flags &= ~TUN_LMODE;
457 tp->tun_flags &= ~TUN_IFHEAD;
460 *(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
463 /* deny this if UP */
464 if (tp->tun_if.if_flags & IFF_UP)
467 switch (*(int *)data & ~IFF_MULTICAST) {
468 case IFF_POINTOPOINT:
470 tp->tun_if.if_flags &= ~(IFF_BROADCAST|IFF_POINTOPOINT);
471 tp->tun_if.if_flags |= *(int *)data;
478 tp->tun_pid = curproc->p_pid;
484 tp->tun_flags |= TUN_ASYNC;
486 tp->tun_flags &= ~TUN_ASYNC;
490 if (tp->tun_if.if_snd.ifq_head) {
491 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
492 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
493 *(int *)data += mb->m_len;
499 return (fsetown(*(int *)data, &tp->tun_sigio));
502 *(int *)data = fgetown(tp->tun_sigio);
505 /* This is deprecated, FIOSETOWN should be used instead. */
507 return (fsetown(-(*(int *)data), &tp->tun_sigio));
509 /* This is deprecated, FIOGETOWN should be used instead. */
511 *(int *)data = -fgetown(tp->tun_sigio);
521 * The cdevsw read interface - reads a packet at a time, or at
522 * least as much of a packet as can be read.
525 tunread(dev, uio, flag)
530 struct tun_softc *tp = dev->si_drv1;
531 struct ifnet *ifp = &tp->tun_if;
535 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
536 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
537 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
538 ifp->if_unit, tp->tun_flags);
542 tp->tun_flags &= ~TUN_RWAIT;
546 IF_DEQUEUE(&ifp->if_snd, m0);
548 if (flag & IO_NDELAY) {
552 tp->tun_flags |= TUN_RWAIT;
553 if((error = tsleep((caddr_t)tp, PCATCH,
554 "tunread", 0)) != 0) {
562 while (m0 && uio->uio_resid > 0 && error == 0) {
563 len = min(uio->uio_resid, m0->m_len);
565 error = uiomove(mtod(m0, caddr_t), len, uio);
570 TUNDEBUG("%s%d: Dropping mbuf\n", ifp->if_name, ifp->if_unit);
577 * the cdevsw write interface - an atomic write is a packet - or else!
580 tunwrite(dev, uio, flag)
585 struct tun_softc *tp = dev->si_drv1;
586 struct ifnet *ifp = &tp->tun_if;
587 struct mbuf *top, **mp, *m;
588 int error=0, tlen, mlen;
591 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
593 if (uio->uio_resid == 0)
596 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
597 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
601 tlen = uio->uio_resid;
603 /* get a header mbuf */
604 MGETHDR(m, M_DONTWAIT, MT_DATA);
611 while (error == 0 && uio->uio_resid > 0) {
612 m->m_len = min(mlen, uio->uio_resid);
613 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
616 if (uio->uio_resid > 0) {
617 MGET (m, M_DONTWAIT, MT_DATA);
632 top->m_pkthdr.len = tlen;
633 top->m_pkthdr.rcvif = ifp;
636 if (tp->tun_flags & TUN_IFHEAD) {
638 * Conveniently, we already have a 4-byte address
639 * family prepended to our packet !
640 * Inconveniently, it's in the wrong byte order !
642 if ((top = m_pullup(top, sizeof(family))) == NULL)
644 *mtod(top, u_int32_t *) =
645 ntohl(*mtod(top, u_int32_t *));
647 *mtod(top, u_int32_t *) =
648 htonl(*mtod(top, u_int32_t *));
651 * We need to prepend the address family as
652 * a four byte field. Cons up a dummy header
653 * to pacify bpf. This is safe because bpf
654 * will only read from the mbuf (i.e., it won't
655 * try to free it or keep a pointer to it).
658 uint32_t af = AF_INET;
662 m.m_data = (char *)⁡
668 if (tp->tun_flags & TUN_IFHEAD) {
669 if (top->m_len < sizeof(family) &&
670 (top = m_pullup(top, sizeof(family))) == NULL)
672 family = ntohl(*mtod(top, u_int32_t *));
673 m_adj(top, sizeof(family));
677 ifp->if_ibytes += top->m_pkthdr.len;
680 return family_enqueue(family, top);
684 * tunpoll - the poll interface, this is only useful on reads
685 * really. The write detect always returns true, write never blocks
686 * anyway, it either accepts the packet or drops it.
689 tunpoll(dev_t dev, int events, struct thread *td)
692 struct tun_softc *tp = dev->si_drv1;
693 struct ifnet *ifp = &tp->tun_if;
697 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
699 if (events & (POLLIN | POLLRDNORM)) {
700 if (ifp->if_snd.ifq_len > 0) {
701 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
702 ifp->if_unit, ifp->if_snd.ifq_len);
703 revents |= events & (POLLIN | POLLRDNORM);
705 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
707 selrecord(td, &tp->tun_rsel);
710 if (events & (POLLOUT | POLLWRNORM))
711 revents |= events & (POLLOUT | POLLWRNORM);