if_tun.c revision 47625
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	dev_t dev;
125
126	if ( tun_devsw_installed )
127		return;
128	dev = makedev(CDEV_MAJOR, 0);
129	cdevsw_add(&dev, &tun_cdevsw, NULL);
130	tun_devsw_installed = 1;
131	for ( i = 0; i < NTUN; i++ ) {
132#ifdef DEVFS
133		tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
134						      DV_CHR, UID_UUCP,
135						      GID_DIALER, 0600,
136						      "tun%d", i);
137#endif
138		tunctl[i].tun_flags = TUN_INITED;
139
140		ifp = &tunctl[i].tun_if;
141		ifp->if_unit = i;
142		ifp->if_name = "tun";
143		ifp->if_mtu = TUNMTU;
144		ifp->if_ioctl = tunifioctl;
145		ifp->if_output = tunoutput;
146		ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
147		ifp->if_snd.ifq_maxlen = ifqmaxlen;
148		if_attach(ifp);
149#if NBPFILTER > 0
150		bpfattach(ifp, DLT_NULL, sizeof(u_int));
151#endif
152	}
153}
154
155/*
156 * tunnel open - must be superuser & the device must be
157 * configured in
158 */
159static	int
160tunopen(dev, flag, mode, p)
161	dev_t	dev;
162	int	flag, mode;
163	struct proc *p;
164{
165	struct ifnet	*ifp;
166	struct tun_softc *tp;
167	register int	unit, error;
168
169	error = suser(p);
170	if (error)
171		return (error);
172
173	if ((unit = dev_val(minor(dev))) >= NTUN)
174		return (ENXIO);
175	tp = &tunctl[unit];
176	if (tp->tun_flags & TUN_OPEN)
177		return EBUSY;
178	ifp = &tp->tun_if;
179	tp->tun_flags |= TUN_OPEN;
180	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
181	return (0);
182}
183
184/*
185 * tunclose - close the device - mark i/f down & delete
186 * routing info
187 */
188static	int
189tunclose(dev, foo, bar, p)
190	dev_t dev;
191	int foo;
192	int bar;
193	struct proc *p;
194{
195	register int	unit = dev_val(minor(dev)), s;
196	struct tun_softc *tp = &tunctl[unit];
197	struct ifnet	*ifp = &tp->tun_if;
198	struct mbuf	*m;
199
200	tp->tun_flags &= ~TUN_OPEN;
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	register struct ifreq *ifr = (struct ifreq *)data;
282	int		error = 0, s;
283
284	s = splimp();
285	switch(cmd) {
286	case SIOCSIFADDR:
287		tuninit(ifp->if_unit);
288		TUNDEBUG("%s%d: address set\n",
289			 ifp->if_name, ifp->if_unit);
290		break;
291	case SIOCSIFDSTADDR:
292		tuninit(ifp->if_unit);
293		TUNDEBUG("%s%d: destination address set\n",
294			 ifp->if_name, ifp->if_unit);
295		break;
296	case SIOCSIFMTU:
297		ifp->if_mtu = ifr->ifr_mtu;
298		TUNDEBUG("%s%d: mtu set\n",
299			 ifp->if_name, ifp->if_unit);
300		break;
301	case SIOCADDMULTI:
302	case SIOCDELMULTI:
303		break;
304
305
306	default:
307		error = EINVAL;
308	}
309	splx(s);
310	return (error);
311}
312
313/*
314 * tunoutput - queue packets from higher level ready to put out.
315 */
316int
317tunoutput(ifp, m0, dst, rt)
318	struct ifnet   *ifp;
319	struct mbuf    *m0;
320	struct sockaddr *dst;
321	struct rtentry *rt;
322{
323	struct tun_softc *tp = &tunctl[ifp->if_unit];
324	int		s;
325
326	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
327
328	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
329		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
330			  ifp->if_unit, tp->tun_flags);
331		m_freem (m0);
332		return EHOSTDOWN;
333	}
334
335#if NBPFILTER > 0
336	/* BPF write needs to be handled specially */
337	if (dst->sa_family == AF_UNSPEC) {
338		dst->sa_family = *(mtod(m0, int *));
339		m0->m_len -= sizeof(int);
340		m0->m_pkthdr.len -= sizeof(int);
341		m0->m_data += sizeof(int);
342	}
343
344	if (ifp->if_bpf) {
345		/*
346		 * We need to prepend the address family as
347		 * a four byte field.  Cons up a dummy header
348		 * to pacify bpf.  This is safe because bpf
349		 * will only read from the mbuf (i.e., it won't
350		 * try to free it or keep a pointer to it).
351		 */
352		struct mbuf m;
353		u_int af = dst->sa_family;
354
355		m.m_next = m0;
356		m.m_len = 4;
357		m.m_data = (char *)&af;
358
359		bpf_mtap(ifp, &m);
360	}
361#endif /* NBPFILTER > 0 */
362
363	/* prepend sockaddr? this may abort if the mbuf allocation fails */
364	if (tp->tun_flags & TUN_LMODE) {
365		/* allocate space for sockaddr */
366		M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
367
368		/* if allocation failed drop packet */
369		if (m0 == NULL){
370			s = splimp();	/* spl on queue manipulation */
371			IF_DROP(&ifp->if_snd);
372			splx(s);
373			ifp->if_oerrors++;
374			return (ENOBUFS);
375		} else {
376			bcopy(dst, m0->m_data, dst->sa_len);
377		}
378	}
379
380	switch(dst->sa_family) {
381#ifdef INET
382	case AF_INET:
383		s = splimp();
384		if (IF_QFULL(&ifp->if_snd)) {
385			IF_DROP(&ifp->if_snd);
386			m_freem(m0);
387			splx(s);
388			ifp->if_collisions++;
389			return (ENOBUFS);
390		}
391		ifp->if_obytes += m0->m_pkthdr.len;
392		IF_ENQUEUE(&ifp->if_snd, m0);
393		splx(s);
394		ifp->if_opackets++;
395		break;
396#endif
397	default:
398		m_freem(m0);
399		return EAFNOSUPPORT;
400	}
401
402	if (tp->tun_flags & TUN_RWAIT) {
403		tp->tun_flags &= ~TUN_RWAIT;
404		wakeup((caddr_t)tp);
405	}
406	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
407		pgsigio(tp->tun_sigio, SIGIO, 0);
408	selwakeup(&tp->tun_rsel);
409	return 0;
410}
411
412/*
413 * the cdevsw interface is now pretty minimal.
414 */
415static	int
416tunioctl(dev, cmd, data, flag, p)
417	dev_t		dev;
418	u_long		cmd;
419	caddr_t		data;
420	int		flag;
421	struct proc	*p;
422{
423	int		unit = dev_val(minor(dev)), s;
424	struct tun_softc *tp = &tunctl[unit];
425 	struct tuninfo *tunp;
426
427	switch (cmd) {
428 	case TUNSIFINFO:
429 	        tunp = (struct tuninfo *)data;
430 		tp->tun_if.if_mtu = tunp->mtu;
431 		tp->tun_if.if_type = tunp->type;
432 		tp->tun_if.if_baudrate = tunp->baudrate;
433 		break;
434 	case TUNGIFINFO:
435 		tunp = (struct tuninfo *)data;
436 		tunp->mtu = tp->tun_if.if_mtu;
437 		tunp->type = tp->tun_if.if_type;
438 		tunp->baudrate = tp->tun_if.if_baudrate;
439 		break;
440	case TUNSDEBUG:
441		tundebug = *(int *)data;
442		break;
443	case TUNGDEBUG:
444		*(int *)data = tundebug;
445		break;
446	case TUNSLMODE:
447		if (*(int *)data)
448			tp->tun_flags |= TUN_LMODE;
449		else
450			tp->tun_flags &= ~TUN_LMODE;
451		break;
452	case TUNSIFMODE:
453		/* deny this if UP */
454		if (tp->tun_if.if_flags & IFF_UP)
455			return(EBUSY);
456
457		switch (*(int *)data) {
458		case IFF_POINTOPOINT:
459			tp->tun_if.if_flags |= IFF_POINTOPOINT;
460			tp->tun_if.if_flags &= ~IFF_BROADCAST;
461			break;
462		case IFF_BROADCAST:
463			tp->tun_if.if_flags &= ~IFF_POINTOPOINT;
464			tp->tun_if.if_flags |= IFF_BROADCAST;
465			break;
466		default:
467			return(EINVAL);
468		}
469		break;
470	case FIONBIO:
471		break;
472	case FIOASYNC:
473		if (*(int *)data)
474			tp->tun_flags |= TUN_ASYNC;
475		else
476			tp->tun_flags &= ~TUN_ASYNC;
477		break;
478	case FIONREAD:
479		s = splimp();
480		if (tp->tun_if.if_snd.ifq_head) {
481			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
482			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
483				*(int *)data += mb->m_len;
484		} else
485			*(int *)data = 0;
486		splx(s);
487		break;
488	case FIOSETOWN:
489		return (fsetown(*(int *)data, &tp->tun_sigio));
490
491	case FIOGETOWN:
492		*(int *)data = fgetown(tp->tun_sigio);
493		return (0);
494
495	/* This is deprecated, FIOSETOWN should be used instead. */
496	case TIOCSPGRP:
497		return (fsetown(-(*(int *)data), &tp->tun_sigio));
498
499	/* This is deprecated, FIOGETOWN should be used instead. */
500	case TIOCGPGRP:
501		*(int *)data = -fgetown(tp->tun_sigio);
502		return (0);
503
504	default:
505		return (ENOTTY);
506	}
507	return (0);
508}
509
510/*
511 * The cdevsw read interface - reads a packet at a time, or at
512 * least as much of a packet as can be read.
513 */
514static	int
515tunread(dev, uio, flag)
516	dev_t dev;
517	struct uio *uio;
518	int flag;
519{
520	int		unit = dev_val(minor(dev));
521	struct tun_softc *tp = &tunctl[unit];
522	struct ifnet	*ifp = &tp->tun_if;
523	struct mbuf	*m, *m0;
524	int		error=0, len, s;
525
526	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
527	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
528		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
529			  ifp->if_unit, tp->tun_flags);
530		return EHOSTDOWN;
531	}
532
533	tp->tun_flags &= ~TUN_RWAIT;
534
535	s = splimp();
536	do {
537		IF_DEQUEUE(&ifp->if_snd, m0);
538		if (m0 == 0) {
539			if (flag & IO_NDELAY) {
540				splx(s);
541				return EWOULDBLOCK;
542			}
543			tp->tun_flags |= TUN_RWAIT;
544			if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
545					"tunread", 0)) != 0) {
546				splx(s);
547				return error;
548			}
549		}
550	} while (m0 == 0);
551	splx(s);
552
553	while (m0 && uio->uio_resid > 0 && error == 0) {
554		len = min(uio->uio_resid, m0->m_len);
555		if (len == 0)
556			break;
557		error = uiomove(mtod(m0, caddr_t), len, uio);
558		MFREE(m0, m);
559		m0 = m;
560	}
561
562	if (m0) {
563		TUNDEBUG("Dropping mbuf\n");
564		m_freem(m0);
565	}
566	return error;
567}
568
569/*
570 * the cdevsw write interface - an atomic write is a packet - or else!
571 */
572static	int
573tunwrite(dev, uio, flag)
574	dev_t dev;
575	struct uio *uio;
576	int flag;
577{
578	int		unit = dev_val(minor(dev));
579	struct ifnet	*ifp = &tunctl[unit].tun_if;
580	struct mbuf	*top, **mp, *m;
581	int		error=0, s, tlen, mlen;
582
583	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
584
585	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
586		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
587		    uio->uio_resid);
588		return EIO;
589	}
590	tlen = uio->uio_resid;
591
592	/* get a header mbuf */
593	MGETHDR(m, M_DONTWAIT, MT_DATA);
594	if (m == NULL)
595		return ENOBUFS;
596	mlen = MHLEN;
597
598	top = 0;
599	mp = &top;
600	while (error == 0 && uio->uio_resid > 0) {
601		m->m_len = min(mlen, uio->uio_resid);
602		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
603		*mp = m;
604		mp = &m->m_next;
605		if (uio->uio_resid > 0) {
606			MGET (m, M_DONTWAIT, MT_DATA);
607			if (m == 0) {
608				error = ENOBUFS;
609				break;
610			}
611			mlen = MLEN;
612		}
613	}
614	if (error) {
615		if (top)
616			m_freem (top);
617		return error;
618	}
619
620	top->m_pkthdr.len = tlen;
621	top->m_pkthdr.rcvif = ifp;
622
623#if NBPFILTER > 0
624	if (ifp->if_bpf) {
625		/*
626		 * We need to prepend the address family as
627		 * a four byte field.  Cons up a dummy header
628		 * to pacify bpf.  This is safe because bpf
629		 * will only read from the mbuf (i.e., it won't
630		 * try to free it or keep a pointer to it).
631		 */
632		struct mbuf m;
633		u_int af = AF_INET;
634
635		m.m_next = top;
636		m.m_len = 4;
637		m.m_data = (char *)&af;
638
639		bpf_mtap(ifp, &m);
640	}
641#endif
642
643#ifdef INET
644	s = splimp();
645	if (IF_QFULL (&ipintrq)) {
646		IF_DROP(&ipintrq);
647		splx(s);
648		ifp->if_collisions++;
649		m_freem(top);
650		return ENOBUFS;
651	}
652	IF_ENQUEUE(&ipintrq, top);
653	splx(s);
654	ifp->if_ibytes += tlen;
655	ifp->if_ipackets++;
656	schednetisr(NETISR_IP);
657#endif
658	return error;
659}
660
661/*
662 * tunpoll - the poll interface, this is only useful on reads
663 * really. The write detect always returns true, write never blocks
664 * anyway, it either accepts the packet or drops it.
665 */
666static	int
667tunpoll(dev, events, p)
668	dev_t dev;
669	int events;
670	struct proc *p;
671{
672	int		unit = dev_val(minor(dev)), s;
673	struct tun_softc *tp = &tunctl[unit];
674	struct ifnet	*ifp = &tp->tun_if;
675	int		revents = 0;
676
677	s = splimp();
678	TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
679
680	if (events & (POLLIN | POLLRDNORM)) {
681		if (ifp->if_snd.ifq_len > 0) {
682			TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
683			    ifp->if_unit, ifp->if_snd.ifq_len);
684			revents |= events & (POLLIN | POLLRDNORM);
685		} else {
686			TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
687			    ifp->if_unit);
688			selrecord(p, &tp->tun_rsel);
689		}
690	}
691	if (events & (POLLOUT | POLLWRNORM))
692		revents |= events & (POLLOUT | POLLWRNORM);
693
694	splx(s);
695	return (revents);
696}
697
698
699#endif  /* NTUN */
700