if_tun.c revision 48021
1/*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
2
3/*
4 * Copyright (c) 1988, Julian Onions <jpo@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 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/poll mode of
14 * operation though.
15 */
16
17#include "tun.h"
18#if NTUN > 0
19
20#include "opt_devfs.h"
21#include "opt_inet.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/socket.h>
28#include <sys/filio.h>
29#include <sys/sockio.h>
30#include <sys/ttycom.h>
31#include <sys/poll.h>
32#include <sys/signalvar.h>
33#include <sys/filedesc.h>
34#include <sys/kernel.h>
35#include <sys/sysctl.h>
36#ifdef DEVFS
37#include <sys/devfsext.h>
38#endif /*DEVFS*/
39#include <sys/conf.h>
40#include <sys/uio.h>
41#include <sys/vnode.h>
42
43#include <net/if.h>
44#include <net/netisr.h>
45#include <net/route.h>
46
47#ifdef INET
48#include <netinet/in.h>
49#include <netinet/in_var.h>
50#endif
51
52#ifdef NS
53#include <netns/ns.h>
54#include <netns/ns_if.h>
55#endif
56
57#include "bpfilter.h"
58#if NBPFILTER > 0
59#include <net/bpf.h>
60#endif
61
62#include <net/if_tunvar.h>
63#include <net/if_tun.h>
64
65static void tunattach __P((void *));
66PSEUDO_SET(tunattach, if_tun);
67
68#define TUNDEBUG	if (tundebug) printf
69static int tundebug = 0;
70SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
71
72static struct tun_softc tunctl[NTUN];
73
74static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
75	    struct rtentry *rt));
76static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
77static int tuninit __P((int));
78
79static	d_open_t	tunopen;
80static	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 NBPFILTER > 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 NBPFILTER > 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 /* NBPFILTER > 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 		tp->tun_if.if_mtu = tunp->mtu;
439 		tp->tun_if.if_type = tunp->type;
440 		tp->tun_if.if_baudrate = tunp->baudrate;
441 		break;
442 	case TUNGIFINFO:
443 		tunp = (struct tuninfo *)data;
444 		tunp->mtu = tp->tun_if.if_mtu;
445 		tunp->type = tp->tun_if.if_type;
446 		tunp->baudrate = tp->tun_if.if_baudrate;
447 		break;
448	case TUNSDEBUG:
449		tundebug = *(int *)data;
450		break;
451	case TUNGDEBUG:
452		*(int *)data = tundebug;
453		break;
454	case TUNSLMODE:
455		if (*(int *)data)
456			tp->tun_flags |= TUN_LMODE;
457		else
458			tp->tun_flags &= ~TUN_LMODE;
459		break;
460	case TUNSIFMODE:
461		/* deny this if UP */
462		if (tp->tun_if.if_flags & IFF_UP)
463			return(EBUSY);
464
465		switch (*(int *)data) {
466		case IFF_POINTOPOINT:
467			tp->tun_if.if_flags |= IFF_POINTOPOINT;
468			tp->tun_if.if_flags &= ~IFF_BROADCAST;
469			break;
470		case IFF_BROADCAST:
471			tp->tun_if.if_flags &= ~IFF_POINTOPOINT;
472			tp->tun_if.if_flags |= IFF_BROADCAST;
473			break;
474		default:
475			return(EINVAL);
476		}
477		break;
478	case FIONBIO:
479		break;
480	case FIOASYNC:
481		if (*(int *)data)
482			tp->tun_flags |= TUN_ASYNC;
483		else
484			tp->tun_flags &= ~TUN_ASYNC;
485		break;
486	case FIONREAD:
487		s = splimp();
488		if (tp->tun_if.if_snd.ifq_head) {
489			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
490			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
491				*(int *)data += mb->m_len;
492		} else
493			*(int *)data = 0;
494		splx(s);
495		break;
496	case FIOSETOWN:
497		return (fsetown(*(int *)data, &tp->tun_sigio));
498
499	case FIOGETOWN:
500		*(int *)data = fgetown(tp->tun_sigio);
501		return (0);
502
503	/* This is deprecated, FIOSETOWN should be used instead. */
504	case TIOCSPGRP:
505		return (fsetown(-(*(int *)data), &tp->tun_sigio));
506
507	/* This is deprecated, FIOGETOWN should be used instead. */
508	case TIOCGPGRP:
509		*(int *)data = -fgetown(tp->tun_sigio);
510		return (0);
511
512	default:
513		return (ENOTTY);
514	}
515	return (0);
516}
517
518/*
519 * The cdevsw read interface - reads a packet at a time, or at
520 * least as much of a packet as can be read.
521 */
522static	int
523tunread(dev, uio, flag)
524	dev_t dev;
525	struct uio *uio;
526	int flag;
527{
528	int		unit = dev_val(minor(dev));
529	struct tun_softc *tp = &tunctl[unit];
530	struct ifnet	*ifp = &tp->tun_if;
531	struct mbuf	*m, *m0;
532	int		error=0, len, s;
533
534	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
535	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
536		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
537			  ifp->if_unit, tp->tun_flags);
538		return EHOSTDOWN;
539	}
540
541	tp->tun_flags &= ~TUN_RWAIT;
542
543	s = splimp();
544	do {
545		IF_DEQUEUE(&ifp->if_snd, m0);
546		if (m0 == 0) {
547			if (flag & IO_NDELAY) {
548				splx(s);
549				return EWOULDBLOCK;
550			}
551			tp->tun_flags |= TUN_RWAIT;
552			if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
553					"tunread", 0)) != 0) {
554				splx(s);
555				return error;
556			}
557		}
558	} while (m0 == 0);
559	splx(s);
560
561	while (m0 && uio->uio_resid > 0 && error == 0) {
562		len = min(uio->uio_resid, m0->m_len);
563		if (len == 0)
564			break;
565		error = uiomove(mtod(m0, caddr_t), len, uio);
566		MFREE(m0, m);
567		m0 = m;
568	}
569
570	if (m0) {
571		TUNDEBUG("Dropping mbuf\n");
572		m_freem(m0);
573	}
574	return error;
575}
576
577/*
578 * the cdevsw write interface - an atomic write is a packet - or else!
579 */
580static	int
581tunwrite(dev, uio, flag)
582	dev_t dev;
583	struct uio *uio;
584	int flag;
585{
586	int		unit = dev_val(minor(dev));
587	struct ifnet	*ifp = &tunctl[unit].tun_if;
588	struct mbuf	*top, **mp, *m;
589	int		error=0, s, tlen, mlen;
590
591	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
592
593	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
594		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
595		    uio->uio_resid);
596		return EIO;
597	}
598	tlen = uio->uio_resid;
599
600	/* get a header mbuf */
601	MGETHDR(m, M_DONTWAIT, MT_DATA);
602	if (m == NULL)
603		return ENOBUFS;
604	mlen = MHLEN;
605
606	top = 0;
607	mp = &top;
608	while (error == 0 && uio->uio_resid > 0) {
609		m->m_len = min(mlen, uio->uio_resid);
610		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
611		*mp = m;
612		mp = &m->m_next;
613		if (uio->uio_resid > 0) {
614			MGET (m, M_DONTWAIT, MT_DATA);
615			if (m == 0) {
616				error = ENOBUFS;
617				break;
618			}
619			mlen = MLEN;
620		}
621	}
622	if (error) {
623		if (top)
624			m_freem (top);
625		return error;
626	}
627
628	top->m_pkthdr.len = tlen;
629	top->m_pkthdr.rcvif = ifp;
630
631#if NBPFILTER > 0
632	if (ifp->if_bpf) {
633		/*
634		 * We need to prepend the address family as
635		 * a four byte field.  Cons up a dummy header
636		 * to pacify bpf.  This is safe because bpf
637		 * will only read from the mbuf (i.e., it won't
638		 * try to free it or keep a pointer to it).
639		 */
640		struct mbuf m;
641		u_int af = AF_INET;
642
643		m.m_next = top;
644		m.m_len = 4;
645		m.m_data = (char *)&af;
646
647		bpf_mtap(ifp, &m);
648	}
649#endif
650
651#ifdef INET
652	s = splimp();
653	if (IF_QFULL (&ipintrq)) {
654		IF_DROP(&ipintrq);
655		splx(s);
656		ifp->if_collisions++;
657		m_freem(top);
658		return ENOBUFS;
659	}
660	IF_ENQUEUE(&ipintrq, top);
661	splx(s);
662	ifp->if_ibytes += tlen;
663	ifp->if_ipackets++;
664	schednetisr(NETISR_IP);
665#endif
666	return error;
667}
668
669/*
670 * tunpoll - the poll interface, this is only useful on reads
671 * really. The write detect always returns true, write never blocks
672 * anyway, it either accepts the packet or drops it.
673 */
674static	int
675tunpoll(dev, events, p)
676	dev_t dev;
677	int events;
678	struct proc *p;
679{
680	int		unit = dev_val(minor(dev)), s;
681	struct tun_softc *tp = &tunctl[unit];
682	struct ifnet	*ifp = &tp->tun_if;
683	int		revents = 0;
684
685	s = splimp();
686	TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
687
688	if (events & (POLLIN | POLLRDNORM)) {
689		if (ifp->if_snd.ifq_len > 0) {
690			TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
691			    ifp->if_unit, ifp->if_snd.ifq_len);
692			revents |= events & (POLLIN | POLLRDNORM);
693		} else {
694			TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
695			    ifp->if_unit);
696			selrecord(p, &tp->tun_rsel);
697		}
698	}
699	if (events & (POLLOUT | POLLWRNORM))
700		revents |= events & (POLLOUT | POLLWRNORM);
701
702	splx(s);
703	return (revents);
704}
705
706
707#endif  /* NTUN */
708