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