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