Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.31
      1 /*	$NetBSD: if_tun.c,v 1.31 1997/09/24 19:45:11 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
      5  * Nottingham University 1987.
      6  *
      7  * This source may be freely distributed, however I would be interested
      8  * in any changes that are made.
      9  *
     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 its
     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
     14  * operation though.
     15  */
     16 
     17 #include "tun.h"
     18 #if NTUN > 0
     19 
     20 #include <sys/param.h>
     21 #include <sys/proc.h>
     22 #include <sys/systm.h>
     23 #include <sys/mbuf.h>
     24 #include <sys/buf.h>
     25 #include <sys/protosw.h>
     26 #include <sys/socket.h>
     27 #include <sys/ioctl.h>
     28 #include <sys/errno.h>
     29 #include <sys/syslog.h>
     30 #include <sys/select.h>
     31 #include <sys/poll.h>
     32 #include <sys/file.h>
     33 #include <sys/signalvar.h>
     34 #include <sys/conf.h>
     35 
     36 #include <machine/cpu.h>
     37 
     38 #include <net/if.h>
     39 #include <net/if_ether.h>
     40 #include <net/netisr.h>
     41 #include <net/route.h>
     42 
     43 
     44 #ifdef INET
     45 #include <netinet/in.h>
     46 #include <netinet/in_systm.h>
     47 #include <netinet/in_var.h>
     48 #include <netinet/ip.h>
     49 #include <netinet/if_inarp.h>
     50 #endif
     51 
     52 #ifdef NS
     53 #include <netns/ns.h>
     54 #include <netns/ns_if.h>
     55 #endif
     56 
     57 #include "bpfilter.h"
     58 #if NBPFILTER > 0
     59 #include <sys/time.h>
     60 #include <net/bpf.h>
     61 #endif
     62 
     63 #include <net/if_tun.h>
     64 
     65 #define TUNDEBUG	if (tundebug) printf
     66 int	tundebug = 0;
     67 
     68 struct tun_softc tunctl[NTUN];
     69 extern int ifqmaxlen;
     70 void	tunattach __P((int));
     71 
     72 int	tun_ioctl __P((struct ifnet *, u_long, caddr_t));
     73 int	tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
     74 		       struct rtentry *rt));
     75 
     76 static void tuninit __P((struct tun_softc *));
     77 
     78 void
     79 tunattach(unused)
     80 	int unused;
     81 {
     82 	register int i;
     83 	struct ifnet *ifp;
     84 
     85 	for (i = 0; i < NTUN; i++) {
     86 		tunctl[i].tun_flags = TUN_INITED;
     87 
     88 		ifp = &tunctl[i].tun_if;
     89 		sprintf(ifp->if_xname, "tun%d", i);
     90 		ifp->if_softc = &tunctl[i];
     91 		ifp->if_mtu = TUNMTU;
     92 		ifp->if_ioctl = tun_ioctl;
     93 		ifp->if_output = tun_output;
     94 		ifp->if_flags = IFF_POINTOPOINT;
     95 		ifp->if_snd.ifq_maxlen = ifqmaxlen;
     96 		ifp->if_collisions = 0;
     97 		ifp->if_ierrors = 0;
     98 		ifp->if_oerrors = 0;
     99 		ifp->if_ipackets = 0;
    100 		ifp->if_opackets = 0;
    101 		if_attach(ifp);
    102 #if NBPFILTER > 0
    103 		bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int32_t));
    104 #endif
    105 	}
    106 }
    107 
    108 /*
    109  * tunnel open - must be superuser & the device must be
    110  * configured in
    111  */
    112 int
    113 tunopen(dev, flag, mode, p)
    114 	dev_t	dev;
    115 	int	flag, mode;
    116 	struct proc *p;
    117 {
    118 	struct ifnet	*ifp;
    119 	struct tun_softc *tp;
    120 	register int	unit, error;
    121 
    122 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    123 		return (error);
    124 
    125 	if ((unit = minor(dev)) >= NTUN)
    126 		return (ENXIO);
    127 	tp = &tunctl[unit];
    128 	if (tp->tun_flags & TUN_OPEN)
    129 		return ENXIO;
    130 	ifp = &tp->tun_if;
    131 	tp->tun_flags |= TUN_OPEN;
    132 	TUNDEBUG("%s: open\n", ifp->if_xname);
    133 	return (0);
    134 }
    135 
    136 /*
    137  * tunclose - close the device - mark i/f down & delete
    138  * routing info
    139  */
    140 int
    141 tunclose(dev, flag, mode, p)
    142 	dev_t	dev;
    143 	int	flag;
    144 	int	mode;
    145 	struct proc *p;
    146 {
    147 	register int	unit = minor(dev), s;
    148 	struct tun_softc *tp = &tunctl[unit];
    149 	struct ifnet	*ifp = &tp->tun_if;
    150 	struct mbuf	*m;
    151 
    152 	tp->tun_flags &= ~TUN_OPEN;
    153 
    154 	/*
    155 	 * junk all pending output
    156 	 */
    157 	do {
    158 		s = splimp();
    159 		IF_DEQUEUE(&ifp->if_snd, m);
    160 		splx(s);
    161 		if (m)
    162 			m_freem(m);
    163 	} while (m);
    164 
    165 	if (ifp->if_flags & IFF_UP) {
    166 		s = splimp();
    167 		if_down(ifp);
    168 		if (ifp->if_flags & IFF_RUNNING) {
    169 			/* find internet addresses and delete routes */
    170 			register struct ifaddr *ifa;
    171 			for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    172 			    ifa = ifa->ifa_list.tqe_next) {
    173 				if (ifa->ifa_addr->sa_family == AF_INET) {
    174 					rtinit(ifa, (int)RTM_DELETE,
    175 					       tp->tun_flags & TUN_DSTADDR
    176 							? RTF_HOST
    177 							: 0);
    178 				}
    179 			}
    180 		}
    181 		splx(s);
    182 	}
    183 	tp->tun_pgrp = 0;
    184 	selwakeup(&tp->tun_rsel);
    185 
    186 	TUNDEBUG ("%s: closed\n", ifp->if_xname);
    187 	return (0);
    188 }
    189 
    190 static void
    191 tuninit(tp)
    192 	struct tun_softc *tp;
    193 {
    194 	struct ifnet	*ifp = &tp->tun_if;
    195 	register struct ifaddr *ifa;
    196 
    197 	TUNDEBUG("%s: tuninit\n", ifp->if_xname);
    198 
    199 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    200 
    201 	tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
    202 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    203 	     ifa = ifa->ifa_list.tqe_next) {
    204 		if (ifa->ifa_addr->sa_family == AF_INET) {
    205 			struct sockaddr_in *sin;
    206 
    207 			sin = satosin(ifa->ifa_addr);
    208 			if (sin && sin->sin_addr.s_addr)
    209 				tp->tun_flags |= TUN_IASET;
    210 
    211 			if (ifp->if_flags & IFF_POINTOPOINT) {
    212 				sin = satosin(ifa->ifa_dstaddr);
    213 				if (sin && sin->sin_addr.s_addr)
    214 					tp->tun_flags |= TUN_DSTADDR;
    215 			}
    216 		}
    217 	}
    218 
    219 	return;
    220 }
    221 
    222 /*
    223  * Process an ioctl request.
    224  */
    225 int
    226 tun_ioctl(ifp, cmd, data)
    227 	struct ifnet *ifp;
    228 	u_long cmd;
    229 	caddr_t	data;
    230 {
    231 	int		error = 0, s;
    232 
    233 	s = splimp();
    234 	switch(cmd) {
    235 	case SIOCSIFADDR:
    236 		tuninit((struct tun_softc *)(ifp->if_softc));
    237 		TUNDEBUG("%s: address set\n", ifp->if_xname);
    238 		break;
    239 	case SIOCSIFDSTADDR:
    240 		tuninit((struct tun_softc *)(ifp->if_softc));
    241 		TUNDEBUG("%s: destination address set\n", ifp->if_xname);
    242 		break;
    243 	case SIOCSIFBRDADDR:
    244 		TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
    245 		break;
    246 	case SIOCSIFMTU: {
    247 		struct ifreq *ifr = (struct ifreq *) data;
    248 		if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
    249 		    error = EINVAL;
    250 		    break;
    251 		}
    252 		TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
    253 		ifp->if_mtu = ifr->ifr_mtu;
    254 		break;
    255 	}
    256 	default:
    257 		error = EINVAL;
    258 	}
    259 	splx(s);
    260 	return (error);
    261 }
    262 
    263 /*
    264  * tun_output - queue packets from higher level ready to put out.
    265  */
    266 int
    267 tun_output(ifp, m0, dst, rt)
    268 	struct ifnet   *ifp;
    269 	struct mbuf    *m0;
    270 	struct sockaddr *dst;
    271 	struct rtentry *rt;
    272 {
    273 	struct tun_softc *tp = ifp->if_softc;
    274 	struct proc	*p;
    275 	int		s;
    276 
    277 	TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
    278 
    279 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    280 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
    281 			  tp->tun_flags);
    282 		m_freem (m0);
    283 		return (EHOSTDOWN);
    284 	}
    285 
    286 #if NBPFILTER > 0
    287 	if (tp->tun_bpf) {
    288 		/*
    289 		 * We need to prepend the address family as
    290 		 * a four byte field.  Cons up a dummy header
    291 		 * to pacify bpf.  This is safe because bpf
    292 		 * will only read from the mbuf (i.e., it won't
    293 		 * try to free it or keep a pointer to it).
    294 		 */
    295 		struct mbuf m;
    296 		u_int32_t af = dst->sa_family;
    297 
    298 		m.m_next = m0;
    299 		m.m_len = sizeof(af);
    300 		m.m_data = (char *)&af;
    301 
    302 		bpf_mtap(tp->tun_bpf, &m);
    303 	}
    304 #endif
    305 
    306 	switch(dst->sa_family) {
    307 #ifdef INET
    308 	case AF_INET:
    309 		if (tp->tun_flags & TUN_PREPADDR) {
    310 			/* Simple link-layer header */
    311 			M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
    312 			if (m0 == NULL) {
    313 				IF_DROP(&ifp->if_snd);
    314 				return (ENOBUFS);
    315 			}
    316 			bcopy(dst, mtod(m0, char *), dst->sa_len);
    317 		}
    318 
    319 		s = splimp();
    320 		if (IF_QFULL(&ifp->if_snd)) {
    321 			IF_DROP(&ifp->if_snd);
    322 			m_freem(m0);
    323 			splx(s);
    324 			ifp->if_collisions++;
    325 			return (ENOBUFS);
    326 		}
    327 		IF_ENQUEUE(&ifp->if_snd, m0);
    328 		splx(s);
    329 		ifp->if_opackets++;
    330 		break;
    331 #endif
    332 	default:
    333 		m_freem(m0);
    334 		return (EAFNOSUPPORT);
    335 	}
    336 
    337 	if (tp->tun_flags & TUN_RWAIT) {
    338 		tp->tun_flags &= ~TUN_RWAIT;
    339 		wakeup((caddr_t)tp);
    340 	}
    341 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    342 		if (tp->tun_pgrp > 0)
    343 			gsignal(tp->tun_pgrp, SIGIO);
    344 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    345 			psignal(p, SIGIO);
    346 	}
    347 	selwakeup(&tp->tun_rsel);
    348 	return (0);
    349 }
    350 
    351 /*
    352  * the cdevsw interface is now pretty minimal.
    353  */
    354 int
    355 tunioctl(dev, cmd, data, flag, p)
    356 	dev_t		dev;
    357 	u_long		cmd;
    358 	caddr_t		data;
    359 	int		flag;
    360 	struct proc	*p;
    361 {
    362 	int		unit = minor(dev), s;
    363 	struct tun_softc *tp = &tunctl[unit];
    364 
    365 	switch (cmd) {
    366 	case TUNSDEBUG:
    367 		tundebug = *(int *)data;
    368 		break;
    369 
    370 	case TUNGDEBUG:
    371 		*(int *)data = tundebug;
    372 		break;
    373 
    374 	case TUNSIFMODE:
    375 		switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
    376 		case IFF_POINTOPOINT:
    377 		case IFF_BROADCAST:
    378 			s = splimp();
    379 			if (tp->tun_if.if_flags & IFF_UP) {
    380 				splx(s);
    381 				return (EBUSY);
    382 			}
    383 			tp->tun_if.if_flags &=
    384 				~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
    385 			tp->tun_if.if_flags |= *(int *)data;
    386 			splx(s);
    387 			break;
    388 		default:
    389 			return (EINVAL);
    390 			break;
    391 		}
    392 		break;
    393 
    394 	case TUNSLMODE:
    395 		if (*(int *)data)
    396 			tp->tun_flags |= TUN_PREPADDR;
    397 		else
    398 			tp->tun_flags &= ~TUN_PREPADDR;
    399 		break;
    400 
    401 	case FIONBIO:
    402 		if (*(int *)data)
    403 			tp->tun_flags |= TUN_NBIO;
    404 		else
    405 			tp->tun_flags &= ~TUN_NBIO;
    406 		break;
    407 
    408 	case FIOASYNC:
    409 		if (*(int *)data)
    410 			tp->tun_flags |= TUN_ASYNC;
    411 		else
    412 			tp->tun_flags &= ~TUN_ASYNC;
    413 		break;
    414 
    415 	case FIONREAD:
    416 		s = splimp();
    417 		if (tp->tun_if.if_snd.ifq_head)
    418 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    419 		else
    420 			*(int *)data = 0;
    421 		splx(s);
    422 		break;
    423 
    424 	case TIOCSPGRP:
    425 		tp->tun_pgrp = *(int *)data;
    426 		break;
    427 
    428 	case TIOCGPGRP:
    429 		*(int *)data = tp->tun_pgrp;
    430 		break;
    431 
    432 	default:
    433 		return (ENOTTY);
    434 	}
    435 	return (0);
    436 }
    437 
    438 /*
    439  * The cdevsw read interface - reads a packet at a time, or at
    440  * least as much of a packet as can be read.
    441  */
    442 int
    443 tunread(dev, uio, ioflag)
    444 	dev_t		dev;
    445 	struct uio	*uio;
    446 	int		ioflag;
    447 {
    448 	int		unit = minor(dev);
    449 	struct tun_softc *tp = &tunctl[unit];
    450 	struct ifnet	*ifp = &tp->tun_if;
    451 	struct mbuf	*m, *m0;
    452 	int		error=0, len, s;
    453 
    454 	TUNDEBUG ("%s: read\n", ifp->if_xname);
    455 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    456 		TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
    457 		return EHOSTDOWN;
    458 	}
    459 
    460 	tp->tun_flags &= ~TUN_RWAIT;
    461 
    462 	s = splimp();
    463 	do {
    464 		IF_DEQUEUE(&ifp->if_snd, m0);
    465 		if (m0 == 0) {
    466 			if (tp->tun_flags & TUN_NBIO) {
    467 				splx(s);
    468 				return (EWOULDBLOCK);
    469 			}
    470 			tp->tun_flags |= TUN_RWAIT;
    471 			if (tsleep((caddr_t)tp, PZERO|PCATCH, "tunread", 0)) {
    472 				splx(s);
    473 				return (EINTR);
    474 			}
    475 		}
    476 	} while (m0 == 0);
    477 	splx(s);
    478 
    479 	while (m0 && uio->uio_resid > 0 && error == 0) {
    480 		len = min(uio->uio_resid, m0->m_len);
    481 		if (len == 0)
    482 			break;
    483 		error = uiomove(mtod(m0, caddr_t), len, uio);
    484 		MFREE(m0, m);
    485 		m0 = m;
    486 	}
    487 
    488 	if (m0) {
    489 		TUNDEBUG("Dropping mbuf\n");
    490 		m_freem(m0);
    491 	}
    492 	if (error)
    493 		ifp->if_ierrors++;
    494 	return (error);
    495 }
    496 
    497 /*
    498  * the cdevsw write interface - an atomic write is a packet - or else!
    499  */
    500 int
    501 tunwrite(dev, uio, ioflag)
    502 	dev_t		dev;
    503 	struct uio	*uio;
    504 	int		ioflag;
    505 {
    506 	int		unit = minor (dev);
    507 	struct tun_softc *tp = &tunctl[unit];
    508 	struct ifnet	*ifp = &tp->tun_if;
    509 	struct mbuf	*top, **mp, *m;
    510 	struct ifqueue	*ifq;
    511 	struct sockaddr	dst;
    512 	int		isr, error=0, s, tlen, mlen;
    513 
    514 	TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
    515 
    516 	if (tp->tun_flags & TUN_PREPADDR) {
    517 		if (uio->uio_resid < sizeof(dst))
    518 			return (EIO);
    519 		error = uiomove((caddr_t)&dst, sizeof(dst), uio);
    520 		if (dst.sa_len > sizeof(dst)) {
    521 			/* Duh.. */
    522 			char discard;
    523 			int n = dst.sa_len - sizeof(dst);
    524 			while (n--)
    525 				if ((error = uiomove(&discard, 1, uio)) != 0)
    526 					return (error);
    527 		}
    528 	} else {
    529 #ifdef INET
    530 		dst.sa_family = AF_INET;
    531 #endif
    532 	}
    533 
    534 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    535 		TUNDEBUG("%s: len=%d!\n", ifp->if_xname, uio->uio_resid);
    536 		return (EIO);
    537 	}
    538 
    539 	switch (dst.sa_family) {
    540 #ifdef INET
    541 	case AF_INET:
    542 		ifq = &ipintrq;
    543 		isr = NETISR_IP;
    544 		break;
    545 #endif
    546 	default:
    547 		return (EAFNOSUPPORT);
    548 	}
    549 
    550 	tlen = uio->uio_resid;
    551 
    552 	/* get a header mbuf */
    553 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    554 	if (m == NULL)
    555 		return (ENOBUFS);
    556 	mlen = MHLEN;
    557 
    558 	top = 0;
    559 	mp = &top;
    560 	while (error == 0 && uio->uio_resid > 0) {
    561 		m->m_len = min(mlen, uio->uio_resid);
    562 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    563 		*mp = m;
    564 		mp = &m->m_next;
    565 		if (uio->uio_resid > 0) {
    566 			MGET (m, M_DONTWAIT, MT_DATA);
    567 			if (m == 0) {
    568 				error = ENOBUFS;
    569 				break;
    570 			}
    571 			mlen = MLEN;
    572 		}
    573 	}
    574 	if (error) {
    575 		if (top)
    576 			m_freem (top);
    577 		ifp->if_ierrors++;
    578 		return (error);
    579 	}
    580 
    581 	top->m_pkthdr.len = tlen;
    582 	top->m_pkthdr.rcvif = ifp;
    583 
    584 #if NBPFILTER > 0
    585 	if (tp->tun_bpf) {
    586 		/*
    587 		 * We need to prepend the address family as
    588 		 * a four byte field.  Cons up a dummy header
    589 		 * to pacify bpf.  This is safe because bpf
    590 		 * will only read from the mbuf (i.e., it won't
    591 		 * try to free it or keep a pointer to it).
    592 		 */
    593 		struct mbuf m;
    594 		u_int32_t af = AF_INET;
    595 
    596 		m.m_next = top;
    597 		m.m_len = sizeof(af);
    598 		m.m_data = (char *)&af;
    599 
    600 		bpf_mtap(tp->tun_bpf, &m);
    601 	}
    602 #endif
    603 
    604 	s = splimp();
    605 	if (IF_QFULL(ifq)) {
    606 		IF_DROP(ifq);
    607 		splx(s);
    608 		ifp->if_collisions++;
    609 		m_freem(top);
    610 		return (ENOBUFS);
    611 	}
    612 	IF_ENQUEUE(ifq, top);
    613 	splx(s);
    614 	ifp->if_ipackets++;
    615 	schednetisr(isr);
    616 	return (error);
    617 }
    618 
    619 /*
    620  * tunpoll - the poll interface, this is only useful on reads
    621  * really. The write detect always returns true, write never blocks
    622  * anyway, it either accepts the packet or drops it.
    623  */
    624 int
    625 tunpoll(dev, events, p)
    626 	dev_t		dev;
    627 	int		events;
    628 	struct proc	*p;
    629 {
    630 	int		unit = minor(dev), s;
    631 	struct tun_softc *tp = &tunctl[unit];
    632 	struct ifnet	*ifp = &tp->tun_if;
    633 	int		revents = 0;
    634 
    635 	s = splimp();
    636 	TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
    637 
    638 	if (events & (POLLIN | POLLRDNORM))
    639 		if (ifp->if_snd.ifq_len > 0) {
    640 			TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
    641 			    ifp->if_snd.ifq_len);
    642 			revents |= events & (POLLIN | POLLRDNORM);
    643 		} else {
    644 			TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
    645 			selrecord(p, &tp->tun_rsel);
    646 		}
    647 
    648 	if (events & (POLLOUT | POLLWRNORM))
    649 		revents |= events & (POLLOUT | POLLWRNORM);
    650 
    651 	splx(s);
    652 	return (revents);
    653 }
    654 
    655 #endif  /* NTUN */
    656