if_tun.c revision 49469
129088Smarkm/*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
229088Smarkm
329088Smarkm/*
429088Smarkm * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
529088Smarkm * Nottingham University 1987.
629088Smarkm *
729088Smarkm * This source may be freely distributed, however I would be interested
829088Smarkm * in any changes that are made.
929088Smarkm *
1029088Smarkm * This driver takes packets off the IP i/f and hands them up to a
1129088Smarkm * user process to have its wicked way with. This driver has it's
1229088Smarkm * roots in a similar driver written by Phil Cockcroft (formerly) at
1329088Smarkm * UCL. This driver is based much more on read/write/poll mode of
1429088Smarkm * operation though.
1529088Smarkm */
1629088Smarkm
1729088Smarkm#include "tun.h"
1829088Smarkm#if NTUN > 0
1929088Smarkm
2029088Smarkm#include "opt_devfs.h"
2129088Smarkm#include "opt_inet.h"
2229088Smarkm
2329088Smarkm#include <sys/param.h>
2429088Smarkm#include <sys/proc.h>
2529088Smarkm#include <sys/systm.h>
2629088Smarkm#include <sys/mbuf.h>
2729088Smarkm#include <sys/socket.h>
2829088Smarkm#include <sys/filio.h>
2929088Smarkm#include <sys/sockio.h>
3029088Smarkm#include <sys/ttycom.h>
3129088Smarkm#include <sys/poll.h>
3229088Smarkm#include <sys/signalvar.h>
3329088Smarkm#include <sys/filedesc.h>
3481965Smarkm#include <sys/kernel.h>
3529088Smarkm#include <sys/sysctl.h>
3629088Smarkm#ifdef DEVFS
3729088Smarkm#include <sys/devfsext.h>
3829088Smarkm#endif /*DEVFS*/
3929088Smarkm#include <sys/conf.h>
4029088Smarkm#include <sys/uio.h>
4129088Smarkm#include <sys/vnode.h>
4229088Smarkm
4329088Smarkm#include <net/if.h>
4429088Smarkm#include <net/netisr.h>
4529088Smarkm#include <net/route.h>
4629088Smarkm
4729088Smarkm#ifdef INET
4829088Smarkm#include <netinet/in.h>
4929088Smarkm#include <netinet/in_var.h>
5029088Smarkm#endif
5129088Smarkm
5229088Smarkm#ifdef NS
5329088Smarkm#include <netns/ns.h>
5429088Smarkm#include <netns/ns_if.h>
5529088Smarkm#endif
5629088Smarkm
5729088Smarkm#include "bpf.h"
5829088Smarkm#if NBPF > 0
5929088Smarkm#include <net/bpf.h>
6087139Smarkm#endif
6187139Smarkm
6287139Smarkm#include <net/if_tunvar.h>
6329088Smarkm#include <net/if_tun.h>
6487139Smarkm
6587139Smarkmstatic void tunattach __P((void *));
6687139SmarkmPSEUDO_SET(tunattach, if_tun);
6787139Smarkm
6887139Smarkm#define TUNDEBUG	if (tundebug) printf
6987139Smarkmstatic int tundebug = 0;
7087139SmarkmSYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
7129088Smarkm
7229088Smarkmstatic struct tun_softc tunctl[NTUN];
7329088Smarkm
7487139Smarkmstatic int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
7587139Smarkm	    struct rtentry *rt));
7687139Smarkmstatic int tunifioctl __P((struct ifnet *, u_long, caddr_t));
7787139Smarkmstatic int tuninit __P((int));
7887139Smarkm
7987139Smarkmstatic	d_open_t	tunopen;
8029088Smarkmstatic	d_close_t	tunclose;
81static	d_read_t	tunread;
82static	d_write_t	tunwrite;
83static	d_ioctl_t	tunioctl;
84static	d_poll_t	tunpoll;
85
86#define CDEV_MAJOR 52
87static struct cdevsw tun_cdevsw = {
88	/* open */	tunopen,
89	/* close */	tunclose,
90	/* read */	tunread,
91	/* write */	tunwrite,
92	/* ioctl */	tunioctl,
93	/* stop */	nostop,
94	/* reset */	noreset,
95	/* devtotty */	nodevtotty,
96	/* poll */	tunpoll,
97	/* mmap */	nommap,
98	/* strategy */	nostrategy,
99	/* name */	"tun",
100	/* parms */	noparms,
101	/* maj */	CDEV_MAJOR,
102	/* dump */	nodump,
103	/* psize */	nopsize,
104	/* flags */	0,
105	/* maxio */	0,
106	/* bmaj */	-1
107};
108
109
110static	int	tun_devsw_installed;
111#ifdef	DEVFS
112static	void	*tun_devfs_token[NTUN];
113#endif
114
115#define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
116#define dev_val(n) (((n) >> 8) | ((n) & 0xff))
117
118static void
119tunattach(dummy)
120	void *dummy;
121{
122	register int i;
123	struct ifnet *ifp;
124
125	if ( tun_devsw_installed )
126		return;
127	cdevsw_add(&tun_cdevsw);
128	tun_devsw_installed = 1;
129	for ( i = 0; i < NTUN; i++ ) {
130#ifdef DEVFS
131		tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
132						      DV_CHR, UID_UUCP,
133						      GID_DIALER, 0600,
134						      "tun%d", i);
135#endif
136		tunctl[i].tun_flags = TUN_INITED;
137
138		ifp = &tunctl[i].tun_if;
139		ifp->if_unit = i;
140		ifp->if_name = "tun";
141		ifp->if_mtu = TUNMTU;
142		ifp->if_ioctl = tunifioctl;
143		ifp->if_output = tunoutput;
144		ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
145		ifp->if_snd.ifq_maxlen = ifqmaxlen;
146		if_attach(ifp);
147#if NBPF > 0
148		bpfattach(ifp, DLT_NULL, sizeof(u_int));
149#endif
150	}
151}
152
153/*
154 * tunnel open - must be superuser & the device must be
155 * configured in
156 */
157static	int
158tunopen(dev, flag, mode, p)
159	dev_t	dev;
160	int	flag, mode;
161	struct proc *p;
162{
163	struct ifnet	*ifp;
164	struct tun_softc *tp;
165	register int	unit, error;
166
167	error = suser(p);
168	if (error)
169		return (error);
170
171	if ((unit = dev_val(minor(dev))) >= NTUN)
172		return (ENXIO);
173	tp = &tunctl[unit];
174	if (tp->tun_flags & TUN_OPEN)
175		return EBUSY;
176	tp->tun_pid = p->p_pid;
177	ifp = &tp->tun_if;
178	tp->tun_flags |= TUN_OPEN;
179	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
180	return (0);
181}
182
183/*
184 * tunclose - close the device - mark i/f down & delete
185 * routing info
186 */
187static	int
188tunclose(dev, foo, bar, p)
189	dev_t dev;
190	int foo;
191	int bar;
192	struct proc *p;
193{
194	register int	unit = dev_val(minor(dev)), s;
195	struct tun_softc *tp = &tunctl[unit];
196	struct ifnet	*ifp = &tp->tun_if;
197	struct mbuf	*m;
198
199	tp->tun_flags &= ~TUN_OPEN;
200	tp->tun_pid = 0;
201
202	/*
203	 * junk all pending output
204	 */
205	do {
206		s = splimp();
207		IF_DEQUEUE(&ifp->if_snd, m);
208		splx(s);
209		if (m)
210			m_freem(m);
211	} while (m);
212
213	if (ifp->if_flags & IFF_UP) {
214		s = splimp();
215		if_down(ifp);
216		splx(s);
217	}
218
219	if (ifp->if_flags & IFF_RUNNING) {
220		register struct ifaddr *ifa;
221
222		s = splimp();
223		/* find internet addresses and delete routes */
224		for (ifa = ifp->if_addrhead.tqh_first; ifa;
225		    ifa = ifa->ifa_link.tqe_next)
226			if (ifa->ifa_addr->sa_family == AF_INET)
227				rtinit(ifa, (int)RTM_DELETE,
228				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
229		ifp->if_flags &= ~IFF_RUNNING;
230		splx(s);
231	}
232
233	funsetown(tp->tun_sigio);
234	selwakeup(&tp->tun_rsel);
235
236	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
237	return (0);
238}
239
240static int
241tuninit(unit)
242	int	unit;
243{
244	struct tun_softc *tp = &tunctl[unit];
245	struct ifnet	*ifp = &tp->tun_if;
246	register struct ifaddr *ifa;
247
248	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
249
250	ifp->if_flags |= IFF_UP | IFF_RUNNING;
251	getmicrotime(&ifp->if_lastchange);
252
253	for (ifa = ifp->if_addrhead.tqh_first; ifa;
254	     ifa = ifa->ifa_link.tqe_next) {
255#ifdef INET
256		if (ifa->ifa_addr->sa_family == AF_INET) {
257		    struct sockaddr_in *si;
258
259		    si = (struct sockaddr_in *)ifa->ifa_addr;
260		    if (si && si->sin_addr.s_addr)
261			    tp->tun_flags |= TUN_IASET;
262
263		    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
264		    if (si && si->sin_addr.s_addr)
265			    tp->tun_flags |= TUN_DSTADDR;
266		}
267#endif
268	}
269	return 0;
270}
271
272/*
273 * Process an ioctl request.
274 */
275int
276tunifioctl(ifp, cmd, data)
277	struct ifnet *ifp;
278	u_long	cmd;
279	caddr_t	data;
280{
281	struct ifreq *ifr = (struct ifreq *)data;
282	struct tun_softc *tp = &tunctl[ifp->if_unit];
283	struct ifstat *ifs;
284	int		error = 0, s;
285
286	s = splimp();
287	switch(cmd) {
288	case SIOCGIFSTATUS:
289		ifs = (struct ifstat *)data;
290		if (tp->tun_pid)
291			sprintf(ifs->ascii + strlen(ifs->ascii),
292			    "\tOpened by PID %d\n", tp->tun_pid);
293		return(0);
294	case SIOCSIFADDR:
295		tuninit(ifp->if_unit);
296		TUNDEBUG("%s%d: address set\n",
297			 ifp->if_name, ifp->if_unit);
298		break;
299	case SIOCSIFDSTADDR:
300		tuninit(ifp->if_unit);
301		TUNDEBUG("%s%d: destination address set\n",
302			 ifp->if_name, ifp->if_unit);
303		break;
304	case SIOCSIFMTU:
305		ifp->if_mtu = ifr->ifr_mtu;
306		TUNDEBUG("%s%d: mtu set\n",
307			 ifp->if_name, ifp->if_unit);
308		break;
309	case SIOCADDMULTI:
310	case SIOCDELMULTI:
311		break;
312
313
314	default:
315		error = EINVAL;
316	}
317	splx(s);
318	return (error);
319}
320
321/*
322 * tunoutput - queue packets from higher level ready to put out.
323 */
324int
325tunoutput(ifp, m0, dst, rt)
326	struct ifnet   *ifp;
327	struct mbuf    *m0;
328	struct sockaddr *dst;
329	struct rtentry *rt;
330{
331	struct tun_softc *tp = &tunctl[ifp->if_unit];
332	int		s;
333
334	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
335
336	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
337		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
338			  ifp->if_unit, tp->tun_flags);
339		m_freem (m0);
340		return EHOSTDOWN;
341	}
342
343#if NBPF > 0
344	/* BPF write needs to be handled specially */
345	if (dst->sa_family == AF_UNSPEC) {
346		dst->sa_family = *(mtod(m0, int *));
347		m0->m_len -= sizeof(int);
348		m0->m_pkthdr.len -= sizeof(int);
349		m0->m_data += sizeof(int);
350	}
351
352	if (ifp->if_bpf) {
353		/*
354		 * We need to prepend the address family as
355		 * a four byte field.  Cons up a dummy header
356		 * to pacify bpf.  This is safe because bpf
357		 * will only read from the mbuf (i.e., it won't
358		 * try to free it or keep a pointer to it).
359		 */
360		struct mbuf m;
361		u_int af = dst->sa_family;
362
363		m.m_next = m0;
364		m.m_len = 4;
365		m.m_data = (char *)&af;
366
367		bpf_mtap(ifp, &m);
368	}
369#endif /* NBPF > 0 */
370
371	/* prepend sockaddr? this may abort if the mbuf allocation fails */
372	if (tp->tun_flags & TUN_LMODE) {
373		/* allocate space for sockaddr */
374		M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
375
376		/* if allocation failed drop packet */
377		if (m0 == NULL){
378			s = splimp();	/* spl on queue manipulation */
379			IF_DROP(&ifp->if_snd);
380			splx(s);
381			ifp->if_oerrors++;
382			return (ENOBUFS);
383		} else {
384			bcopy(dst, m0->m_data, dst->sa_len);
385		}
386	}
387
388	switch(dst->sa_family) {
389#ifdef INET
390	case AF_INET:
391		s = splimp();
392		if (IF_QFULL(&ifp->if_snd)) {
393			IF_DROP(&ifp->if_snd);
394			m_freem(m0);
395			splx(s);
396			ifp->if_collisions++;
397			return (ENOBUFS);
398		}
399		ifp->if_obytes += m0->m_pkthdr.len;
400		IF_ENQUEUE(&ifp->if_snd, m0);
401		splx(s);
402		ifp->if_opackets++;
403		break;
404#endif
405	default:
406		m_freem(m0);
407		return EAFNOSUPPORT;
408	}
409
410	if (tp->tun_flags & TUN_RWAIT) {
411		tp->tun_flags &= ~TUN_RWAIT;
412		wakeup((caddr_t)tp);
413	}
414	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
415		pgsigio(tp->tun_sigio, SIGIO, 0);
416	selwakeup(&tp->tun_rsel);
417	return 0;
418}
419
420/*
421 * the cdevsw interface is now pretty minimal.
422 */
423static	int
424tunioctl(dev, cmd, data, flag, p)
425	dev_t		dev;
426	u_long		cmd;
427	caddr_t		data;
428	int		flag;
429	struct proc	*p;
430{
431	int		unit = dev_val(minor(dev)), s;
432	struct tun_softc *tp = &tunctl[unit];
433 	struct tuninfo *tunp;
434
435	switch (cmd) {
436 	case TUNSIFINFO:
437 	        tunp = (struct tuninfo *)data;
438		if (tunp->mtu < IF_MINMTU)
439			return (EINVAL);
440 		tp->tun_if.if_mtu = tunp->mtu;
441 		tp->tun_if.if_type = tunp->type;
442 		tp->tun_if.if_baudrate = tunp->baudrate;
443 		break;
444 	case TUNGIFINFO:
445 		tunp = (struct tuninfo *)data;
446 		tunp->mtu = tp->tun_if.if_mtu;
447 		tunp->type = tp->tun_if.if_type;
448 		tunp->baudrate = tp->tun_if.if_baudrate;
449 		break;
450	case TUNSDEBUG:
451		tundebug = *(int *)data;
452		break;
453	case TUNGDEBUG:
454		*(int *)data = tundebug;
455		break;
456	case TUNSLMODE:
457		if (*(int *)data)
458			tp->tun_flags |= TUN_LMODE;
459		else
460			tp->tun_flags &= ~TUN_LMODE;
461		break;
462	case TUNSIFMODE:
463		/* deny this if UP */
464		if (tp->tun_if.if_flags & IFF_UP)
465			return(EBUSY);
466
467		switch (*(int *)data) {
468		case IFF_POINTOPOINT:
469			tp->tun_if.if_flags |= IFF_POINTOPOINT;
470			tp->tun_if.if_flags &= ~IFF_BROADCAST;
471			break;
472		case IFF_BROADCAST:
473			tp->tun_if.if_flags &= ~IFF_POINTOPOINT;
474			tp->tun_if.if_flags |= IFF_BROADCAST;
475			break;
476		default:
477			return(EINVAL);
478		}
479		break;
480	case FIONBIO:
481		break;
482	case FIOASYNC:
483		if (*(int *)data)
484			tp->tun_flags |= TUN_ASYNC;
485		else
486			tp->tun_flags &= ~TUN_ASYNC;
487		break;
488	case FIONREAD:
489		s = splimp();
490		if (tp->tun_if.if_snd.ifq_head) {
491			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
492			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
493				*(int *)data += mb->m_len;
494		} else
495			*(int *)data = 0;
496		splx(s);
497		break;
498	case FIOSETOWN:
499		return (fsetown(*(int *)data, &tp->tun_sigio));
500
501	case FIOGETOWN:
502		*(int *)data = fgetown(tp->tun_sigio);
503		return (0);
504
505	/* This is deprecated, FIOSETOWN should be used instead. */
506	case TIOCSPGRP:
507		return (fsetown(-(*(int *)data), &tp->tun_sigio));
508
509	/* This is deprecated, FIOGETOWN should be used instead. */
510	case TIOCGPGRP:
511		*(int *)data = -fgetown(tp->tun_sigio);
512		return (0);
513
514	default:
515		return (ENOTTY);
516	}
517	return (0);
518}
519
520/*
521 * The cdevsw read interface - reads a packet at a time, or at
522 * least as much of a packet as can be read.
523 */
524static	int
525tunread(dev, uio, flag)
526	dev_t dev;
527	struct uio *uio;
528	int flag;
529{
530	int		unit = dev_val(minor(dev));
531	struct tun_softc *tp = &tunctl[unit];
532	struct ifnet	*ifp = &tp->tun_if;
533	struct mbuf	*m, *m0;
534	int		error=0, len, s;
535
536	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
537	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
538		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
539			  ifp->if_unit, tp->tun_flags);
540		return EHOSTDOWN;
541	}
542
543	tp->tun_flags &= ~TUN_RWAIT;
544
545	s = splimp();
546	do {
547		IF_DEQUEUE(&ifp->if_snd, m0);
548		if (m0 == 0) {
549			if (flag & IO_NDELAY) {
550				splx(s);
551				return EWOULDBLOCK;
552			}
553			tp->tun_flags |= TUN_RWAIT;
554			if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
555					"tunread", 0)) != 0) {
556				splx(s);
557				return error;
558			}
559		}
560	} while (m0 == 0);
561	splx(s);
562
563	while (m0 && uio->uio_resid > 0 && error == 0) {
564		len = min(uio->uio_resid, m0->m_len);
565		if (len == 0)
566			break;
567		error = uiomove(mtod(m0, caddr_t), len, uio);
568		MFREE(m0, m);
569		m0 = m;
570	}
571
572	if (m0) {
573		TUNDEBUG("Dropping mbuf\n");
574		m_freem(m0);
575	}
576	return error;
577}
578
579/*
580 * the cdevsw write interface - an atomic write is a packet - or else!
581 */
582static	int
583tunwrite(dev, uio, flag)
584	dev_t dev;
585	struct uio *uio;
586	int flag;
587{
588	int		unit = dev_val(minor(dev));
589	struct ifnet	*ifp = &tunctl[unit].tun_if;
590	struct mbuf	*top, **mp, *m;
591	int		error=0, s, tlen, mlen;
592
593	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
594
595	if (uio->uio_resid == 0)
596		return 0;
597
598	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
599		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
600		    uio->uio_resid);
601		return EIO;
602	}
603	tlen = uio->uio_resid;
604
605	/* get a header mbuf */
606	MGETHDR(m, M_DONTWAIT, MT_DATA);
607	if (m == NULL)
608		return ENOBUFS;
609	mlen = MHLEN;
610
611	top = 0;
612	mp = &top;
613	while (error == 0 && uio->uio_resid > 0) {
614		m->m_len = min(mlen, uio->uio_resid);
615		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
616		*mp = m;
617		mp = &m->m_next;
618		if (uio->uio_resid > 0) {
619			MGET (m, M_DONTWAIT, MT_DATA);
620			if (m == 0) {
621				error = ENOBUFS;
622				break;
623			}
624			mlen = MLEN;
625		}
626	}
627	if (error) {
628		if (top)
629			m_freem (top);
630		return error;
631	}
632
633	top->m_pkthdr.len = tlen;
634	top->m_pkthdr.rcvif = ifp;
635
636#if NBPF > 0
637	if (ifp->if_bpf) {
638		/*
639		 * We need to prepend the address family as
640		 * a four byte field.  Cons up a dummy header
641		 * to pacify bpf.  This is safe because bpf
642		 * will only read from the mbuf (i.e., it won't
643		 * try to free it or keep a pointer to it).
644		 */
645		struct mbuf m;
646		u_int af = AF_INET;
647
648		m.m_next = top;
649		m.m_len = 4;
650		m.m_data = (char *)&af;
651
652		bpf_mtap(ifp, &m);
653	}
654#endif
655
656#ifdef INET
657	s = splimp();
658	if (IF_QFULL (&ipintrq)) {
659		IF_DROP(&ipintrq);
660		splx(s);
661		ifp->if_collisions++;
662		m_freem(top);
663		return ENOBUFS;
664	}
665	IF_ENQUEUE(&ipintrq, top);
666	splx(s);
667	ifp->if_ibytes += tlen;
668	ifp->if_ipackets++;
669	schednetisr(NETISR_IP);
670#endif
671	return error;
672}
673
674/*
675 * tunpoll - the poll interface, this is only useful on reads
676 * really. The write detect always returns true, write never blocks
677 * anyway, it either accepts the packet or drops it.
678 */
679static	int
680tunpoll(dev, events, p)
681	dev_t dev;
682	int events;
683	struct proc *p;
684{
685	int		unit = dev_val(minor(dev)), s;
686	struct tun_softc *tp = &tunctl[unit];
687	struct ifnet	*ifp = &tp->tun_if;
688	int		revents = 0;
689
690	s = splimp();
691	TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
692
693	if (events & (POLLIN | POLLRDNORM)) {
694		if (ifp->if_snd.ifq_len > 0) {
695			TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
696			    ifp->if_unit, ifp->if_snd.ifq_len);
697			revents |= events & (POLLIN | POLLRDNORM);
698		} else {
699			TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
700			    ifp->if_unit);
701			selrecord(p, &tp->tun_rsel);
702		}
703	}
704	if (events & (POLLOUT | POLLWRNORM))
705		revents |= events & (POLLOUT | POLLWRNORM);
706
707	splx(s);
708	return (revents);
709}
710
711
712#endif  /* NTUN */
713