Home | History | Annotate | Line # | Download | only in net
if_tun.c revision 1.22
      1 /*	$NetBSD: if_tun.c,v 1.22 1996/02/13 22:00:26 christos 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 
     34 #include <machine/cpu.h>
     35 
     36 #include <net/if.h>
     37 #include <net/netisr.h>
     38 #include <net/route.h>
     39 
     40 #ifdef INET
     41 #include <netinet/in.h>
     42 #include <netinet/in_systm.h>
     43 #include <netinet/in_var.h>
     44 #include <netinet/ip.h>
     45 #include <netinet/if_ether.h>
     46 #endif
     47 
     48 #ifdef NS
     49 #include <netns/ns.h>
     50 #include <netns/ns_if.h>
     51 #endif
     52 
     53 #include "bpfilter.h"
     54 #if NBPFILTER > 0
     55 #include <sys/time.h>
     56 #include <net/bpf.h>
     57 #endif
     58 
     59 #include <net/if_tun.h>
     60 #include <net/net_conf.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 int tuninit __P((int));
     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 		ifp->if_unit = i;
     87 		ifp->if_name = "tun";
     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%d: open\n", ifp->if_name, ifp->if_unit);
    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 ? RTF_HOST : 0);
    173 				}
    174 			}
    175 		}
    176 		splx(s);
    177 	}
    178 	tp->tun_pgrp = 0;
    179 	selwakeup(&tp->tun_rsel);
    180 
    181 	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
    182 	return (0);
    183 }
    184 
    185 static int
    186 tuninit(unit)
    187 	int	unit;
    188 {
    189 	struct tun_softc *tp = &tunctl[unit];
    190 	struct ifnet	*ifp = &tp->tun_if;
    191 	register struct ifaddr *ifa;
    192 
    193 	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
    194 
    195 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
    196 
    197 	for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
    198 	    ifa = ifa->ifa_list.tqe_next) {
    199 		if (ifa->ifa_addr->sa_family == AF_INET) {
    200 			struct sockaddr_in *sin;
    201 
    202 			sin = satosin(ifa->ifa_addr);
    203 			if (sin && sin->sin_addr.s_addr)
    204 				tp->tun_flags |= TUN_IASET;
    205 
    206 			sin = satosin(ifa->ifa_dstaddr);
    207 			if (sin && sin->sin_addr.s_addr)
    208 				tp->tun_flags |= TUN_DSTADDR;
    209 		}
    210 	}
    211 
    212 	return 0;
    213 }
    214 
    215 /*
    216  * Process an ioctl request.
    217  */
    218 int
    219 tun_ioctl(ifp, cmd, data)
    220 	struct ifnet *ifp;
    221 	u_long	cmd;
    222 	caddr_t	data;
    223 {
    224 	int		error = 0, s;
    225 
    226 	s = splimp();
    227 	switch(cmd) {
    228 	case SIOCSIFADDR:
    229 		tuninit(ifp->if_unit);
    230 		TUNDEBUG("%s%d: address set\n",
    231 			 ifp->if_name, ifp->if_unit);
    232 		break;
    233 	case SIOCSIFDSTADDR:
    234 		tuninit(ifp->if_unit);
    235 		TUNDEBUG("%s%d: destination address set\n",
    236 			 ifp->if_name, ifp->if_unit);
    237 		break;
    238 	default:
    239 		error = EINVAL;
    240 	}
    241 	splx(s);
    242 	return (error);
    243 }
    244 
    245 /*
    246  * tun_output - queue packets from higher level ready to put out.
    247  */
    248 int
    249 tun_output(ifp, m0, dst, rt)
    250 	struct ifnet   *ifp;
    251 	struct mbuf    *m0;
    252 	struct sockaddr *dst;
    253 	struct rtentry *rt;
    254 {
    255 	struct tun_softc *tp = &tunctl[ifp->if_unit];
    256 	struct proc	*p;
    257 	int		s;
    258 
    259 	TUNDEBUG ("%s%d: tun_output\n", ifp->if_name, ifp->if_unit);
    260 
    261 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    262 		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
    263 			  ifp->if_unit, tp->tun_flags);
    264 		m_freem (m0);
    265 		return EHOSTDOWN;
    266 	}
    267 
    268 #if NBPFILTER > 0
    269 	if (tp->tun_bpf) {
    270 		/*
    271 		 * We need to prepend the address family as
    272 		 * a four byte field.  Cons up a dummy header
    273 		 * to pacify bpf.  This is safe because bpf
    274 		 * will only read from the mbuf (i.e., it won't
    275 		 * try to free it or keep a pointer to it).
    276 		 */
    277 		struct mbuf m;
    278 		u_int32_t af = dst->sa_family;
    279 
    280 		m.m_next = m0;
    281 		m.m_len = sizeof(af);
    282 		m.m_data = (char *)&af;
    283 
    284 		bpf_mtap(tp->tun_bpf, &m);
    285 	}
    286 #endif
    287 
    288 	switch(dst->sa_family) {
    289 #ifdef INET
    290 	case AF_INET:
    291 		s = splimp();
    292 		if (IF_QFULL(&ifp->if_snd)) {
    293 			IF_DROP(&ifp->if_snd);
    294 			m_freem(m0);
    295 			splx(s);
    296 			ifp->if_collisions++;
    297 			return (ENOBUFS);
    298 		}
    299 		IF_ENQUEUE(&ifp->if_snd, m0);
    300 		splx(s);
    301 		ifp->if_opackets++;
    302 		break;
    303 #endif
    304 	default:
    305 		m_freem(m0);
    306 		return EAFNOSUPPORT;
    307 	}
    308 
    309 	if (tp->tun_flags & TUN_RWAIT) {
    310 		tp->tun_flags &= ~TUN_RWAIT;
    311 		wakeup((caddr_t)tp);
    312 	}
    313 	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
    314 		if (tp->tun_pgrp > 0)
    315 			gsignal(tp->tun_pgrp, SIGIO);
    316 		else if ((p = pfind(-tp->tun_pgrp)) != NULL)
    317 			psignal(p, SIGIO);
    318 	}
    319 	selwakeup(&tp->tun_rsel);
    320 	return 0;
    321 }
    322 
    323 /*
    324  * the cdevsw interface is now pretty minimal.
    325  */
    326 int
    327 tunioctl(dev, cmd, data, flag, p)
    328 	dev_t		dev;
    329 	u_long		cmd;
    330 	caddr_t		data;
    331 	int		flag;
    332 	struct proc	*p;
    333 {
    334 	int		unit = minor(dev), s;
    335 	struct tun_softc *tp = &tunctl[unit];
    336 
    337 	switch (cmd) {
    338 	case TUNSDEBUG:
    339 		tundebug = *(int *)data;
    340 		break;
    341 	case TUNGDEBUG:
    342 		*(int *)data = tundebug;
    343 		break;
    344 	case FIONBIO:
    345 		if (*(int *)data)
    346 			tp->tun_flags |= TUN_NBIO;
    347 		else
    348 			tp->tun_flags &= ~TUN_NBIO;
    349 		break;
    350 	case FIOASYNC:
    351 		if (*(int *)data)
    352 			tp->tun_flags |= TUN_ASYNC;
    353 		else
    354 			tp->tun_flags &= ~TUN_ASYNC;
    355 		break;
    356 	case FIONREAD:
    357 		s = splimp();
    358 		if (tp->tun_if.if_snd.ifq_head)
    359 			*(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
    360 		else
    361 			*(int *)data = 0;
    362 		splx(s);
    363 		break;
    364 	case TIOCSPGRP:
    365 		tp->tun_pgrp = *(int *)data;
    366 		break;
    367 	case TIOCGPGRP:
    368 		*(int *)data = tp->tun_pgrp;
    369 		break;
    370 	default:
    371 		return (ENOTTY);
    372 	}
    373 	return (0);
    374 }
    375 
    376 /*
    377  * The cdevsw read interface - reads a packet at a time, or at
    378  * least as much of a packet as can be read.
    379  */
    380 int
    381 tunread(dev, uio, ioflag)
    382 	dev_t		dev;
    383 	struct uio	*uio;
    384 	int		ioflag;
    385 {
    386 	int		unit = minor(dev);
    387 	struct tun_softc *tp = &tunctl[unit];
    388 	struct ifnet	*ifp = &tp->tun_if;
    389 	struct mbuf	*m, *m0;
    390 	int		error=0, len, s;
    391 
    392 	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
    393 	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
    394 		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
    395 			  ifp->if_unit, tp->tun_flags);
    396 		return EHOSTDOWN;
    397 	}
    398 
    399 	tp->tun_flags &= ~TUN_RWAIT;
    400 
    401 	s = splimp();
    402 	do {
    403 		IF_DEQUEUE(&ifp->if_snd, m0);
    404 		if (m0 == 0) {
    405 			if (tp->tun_flags & TUN_NBIO) {
    406 				splx(s);
    407 				return EWOULDBLOCK;
    408 			}
    409 			tp->tun_flags |= TUN_RWAIT;
    410 			tsleep((caddr_t)tp, PZERO + 1, "tunread", 0);
    411 		}
    412 	} while (m0 == 0);
    413 	splx(s);
    414 
    415 	while (m0 && uio->uio_resid > 0 && error == 0) {
    416 		len = min(uio->uio_resid, m0->m_len);
    417 		if (len == 0)
    418 			break;
    419 		error = uiomove(mtod(m0, caddr_t), len, uio);
    420 		MFREE(m0, m);
    421 		m0 = m;
    422 	}
    423 
    424 	if (m0) {
    425 		TUNDEBUG("Dropping mbuf\n");
    426 		m_freem(m0);
    427 	}
    428 	if (error)
    429 		ifp->if_ierrors++;
    430 	return error;
    431 }
    432 
    433 /*
    434  * the cdevsw write interface - an atomic write is a packet - or else!
    435  */
    436 int
    437 tunwrite(dev, uio, ioflag)
    438 	dev_t		dev;
    439 	struct uio	*uio;
    440 	int		ioflag;
    441 {
    442 	int		unit = minor (dev);
    443 	struct ifnet	*ifp = &tunctl[unit].tun_if;
    444 	struct mbuf	*top, **mp, *m;
    445 	int		error=0, s, tlen, mlen;
    446 
    447 	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
    448 
    449 	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
    450 		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
    451 		    uio->uio_resid);
    452 		return EIO;
    453 	}
    454 	tlen = uio->uio_resid;
    455 
    456 	/* get a header mbuf */
    457 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    458 	if (m == NULL)
    459 		return ENOBUFS;
    460 	mlen = MHLEN;
    461 
    462 	top = 0;
    463 	mp = &top;
    464 	while (error == 0 && uio->uio_resid > 0) {
    465 		m->m_len = min(mlen, uio->uio_resid);
    466 		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
    467 		*mp = m;
    468 		mp = &m->m_next;
    469 		if (uio->uio_resid > 0) {
    470 			MGET (m, M_DONTWAIT, MT_DATA);
    471 			if (m == 0) {
    472 				error = ENOBUFS;
    473 				break;
    474 			}
    475 			mlen = MLEN;
    476 		}
    477 	}
    478 	if (error) {
    479 		if (top)
    480 			m_freem (top);
    481 		ifp->if_ierrors++;
    482 		return error;
    483 	}
    484 
    485 	top->m_pkthdr.len = tlen;
    486 	top->m_pkthdr.rcvif = ifp;
    487 
    488 #if NBPFILTER > 0
    489 	if (tunctl[unit].tun_bpf) {
    490 		/*
    491 		 * We need to prepend the address family as
    492 		 * a four byte field.  Cons up a dummy header
    493 		 * to pacify bpf.  This is safe because bpf
    494 		 * will only read from the mbuf (i.e., it won't
    495 		 * try to free it or keep a pointer to it).
    496 		 */
    497 		struct mbuf m;
    498 		u_int32_t af = AF_INET;
    499 
    500 		m.m_next = top;
    501 		m.m_len = sizeof(af);
    502 		m.m_data = (char *)&af;
    503 
    504 		bpf_mtap(tunctl[unit].tun_bpf, &m);
    505 	}
    506 #endif
    507 
    508 	s = splimp();
    509 	if (IF_QFULL (&ipintrq)) {
    510 		IF_DROP(&ipintrq);
    511 		splx(s);
    512 		ifp->if_collisions++;
    513 		m_freem(top);
    514 		return ENOBUFS;
    515 	}
    516 	IF_ENQUEUE(&ipintrq, top);
    517 	splx(s);
    518 	ifp->if_ipackets++;
    519 	schednetisr(NETISR_IP);
    520 	return error;
    521 }
    522 
    523 /*
    524  * tunselect - the select interface, this is only useful on reads
    525  * really. The write detect always returns true, write never blocks
    526  * anyway, it either accepts the packet or drops it.
    527  */
    528 int
    529 tunselect(dev, rw, p)
    530 	dev_t		dev;
    531 	int		rw;
    532 	struct proc	*p;
    533 {
    534 	int		unit = minor(dev), s;
    535 	struct tun_softc *tp = &tunctl[unit];
    536 	struct ifnet	*ifp = &tp->tun_if;
    537 
    538 	s = splimp();
    539 	TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
    540 
    541 	switch (rw) {
    542 	case FREAD:
    543 		if (ifp->if_snd.ifq_len > 0) {
    544 			splx(s);
    545 			TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
    546 			    ifp->if_unit, ifp->if_snd.ifq_len);
    547 			return 1;
    548 		}
    549 		selrecord(curproc, &tp->tun_rsel);
    550 		break;
    551 	case FWRITE:
    552 		splx(s);
    553 		return 1;
    554 	}
    555 	splx(s);
    556 	TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
    557 	return 0;
    558 }
    559 
    560 #endif  /* NTUN */
    561