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