if_tun.c revision 20407
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 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#include <sys/signalvar.h>
33#include <sys/kernel.h>
34#include <sys/sysctl.h>
35#ifdef DEVFS
36#include <sys/devfsext.h>
37#endif /*DEVFS*/
38#include <sys/conf.h>
39
40#include <net/if.h>
41#include <net/netisr.h>
42#include <net/route.h>
43
44#ifdef INET
45#include <netinet/in.h>
46#include <netinet/in_systm.h>
47#include <netinet/in_var.h>
48#include <netinet/ip.h>
49#include <netinet/if_ether.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 <sys/time.h>
60#include <net/bpf.h>
61#endif
62
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 *, int, 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_select_t	tunselect;
85
86#define CDEV_MAJOR 52
87static struct cdevsw tun_cdevsw = {
88	tunopen,	tunclose,	tunread,	tunwrite,
89	tunioctl,	nullstop,	noreset,	nodevtotty,
90	tunselect,	nommap,		nostrategy,	"tun",	NULL,	-1
91};
92
93
94static tun_devsw_installed = 0;
95#ifdef	DEVFS
96static	void	*tun_devfs_token[NTUN];
97#endif
98
99static void
100tunattach(dummy)
101	void *dummy;
102{
103	register int i;
104	struct ifnet *ifp;
105	dev_t dev;
106
107	if ( tun_devsw_installed )
108		return;
109	dev = makedev(CDEV_MAJOR, 0);
110	cdevsw_add(&dev, &tun_cdevsw, NULL);
111	tun_devsw_installed = 1;
112	for ( i = 0; i < NTUN; i++ ) {
113#ifdef DEVFS
114		tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, i, DV_CHR,
115						      UID_UUCP, GID_DIALER,
116						      0600, "tun%d", i);
117#endif
118		tunctl[i].tun_flags = TUN_INITED;
119
120		ifp = &tunctl[i].tun_if;
121		ifp->if_unit = i;
122		ifp->if_name = "tun";
123		ifp->if_mtu = TUNMTU;
124		ifp->if_ioctl = tunifioctl;
125		ifp->if_output = tunoutput;
126		ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
127		ifp->if_snd.ifq_maxlen = ifqmaxlen;
128		ifp->if_collisions = 0;
129		ifp->if_ierrors = 0;
130		ifp->if_oerrors = 0;
131		ifp->if_ipackets = 0;
132		ifp->if_opackets = 0;
133		if_attach(ifp);
134#if NBPFILTER > 0
135		bpfattach(ifp, DLT_NULL, sizeof(u_int));
136#endif
137	}
138}
139
140/*
141 * tunnel open - must be superuser & the device must be
142 * configured in
143 */
144static	int
145tunopen(dev, flag, mode, p)
146	dev_t	dev;
147	int	flag, mode;
148	struct proc *p;
149{
150	struct ifnet	*ifp;
151	struct tun_softc *tp;
152	register int	unit, error;
153
154	error = suser(p->p_ucred, &p->p_acflag);
155	if (error)
156		return (error);
157
158	if ((unit = minor(dev)) >= NTUN)
159		return (ENXIO);
160	tp = &tunctl[unit];
161	if (tp->tun_flags & TUN_OPEN)
162		return EBUSY;
163	ifp = &tp->tun_if;
164	tp->tun_flags |= TUN_OPEN;
165	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
166	return (0);
167}
168
169/*
170 * tunclose - close the device - mark i/f down & delete
171 * routing info
172 */
173static	int
174tunclose(dev_t dev, int foo, int bar, struct proc *p)
175{
176	register int	unit = minor(dev), s;
177	struct tun_softc *tp = &tunctl[unit];
178	struct ifnet	*ifp = &tp->tun_if;
179	struct mbuf	*m;
180
181	tp->tun_flags &= ~TUN_OPEN;
182
183	/*
184	 * junk all pending output
185	 */
186	do {
187		s = splimp();
188		IF_DEQUEUE(&ifp->if_snd, m);
189		splx(s);
190		if (m)
191			m_freem(m);
192	} while (m);
193
194	if (ifp->if_flags & IFF_UP) {
195		s = splimp();
196		if_down(ifp);
197		if (ifp->if_flags & IFF_RUNNING) {
198		    /* find internet addresses and delete routes */
199		    register struct ifaddr *ifa;
200		    for (ifa = ifp->if_addrhead.tqh_first; ifa;
201			 ifa = ifa->ifa_link.tqe_next) {
202			if (ifa->ifa_addr->sa_family == AF_INET) {
203			    rtinit(ifa, (int)RTM_DELETE,
204				   tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
205			}
206		    }
207		}
208		splx(s);
209	}
210	tp->tun_pgrp = 0;
211	selwakeup(&tp->tun_rsel);
212
213	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
214	return (0);
215}
216
217static int
218tuninit(unit)
219	int	unit;
220{
221	struct tun_softc *tp = &tunctl[unit];
222	struct ifnet	*ifp = &tp->tun_if;
223	register struct ifaddr *ifa;
224
225	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
226
227	ifp->if_flags |= IFF_UP | IFF_RUNNING;
228	microtime(&ifp->if_lastchange);
229
230	for (ifa = ifp->if_addrhead.tqh_first; ifa;
231	     ifa = ifa->ifa_link.tqe_next)
232		if (ifa->ifa_addr->sa_family == AF_INET) {
233		    struct sockaddr_in *si;
234
235		    si = (struct sockaddr_in *)ifa->ifa_addr;
236		    if (si && si->sin_addr.s_addr)
237			    tp->tun_flags |= TUN_IASET;
238
239		    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
240		    if (si && si->sin_addr.s_addr)
241			    tp->tun_flags |= TUN_DSTADDR;
242		}
243
244	return 0;
245}
246
247/*
248 * Process an ioctl request.
249 */
250int
251tunifioctl(ifp, cmd, data)
252	struct ifnet *ifp;
253	int	cmd;
254	caddr_t	data;
255{
256	register struct ifreq *ifr = (struct ifreq *)data;
257	int		error = 0, s;
258
259	s = splimp();
260	switch(cmd) {
261	case SIOCSIFADDR:
262		tuninit(ifp->if_unit);
263		TUNDEBUG("%s%d: address set\n",
264			 ifp->if_name, ifp->if_unit);
265		break;
266	case SIOCSIFDSTADDR:
267		tuninit(ifp->if_unit);
268		TUNDEBUG("%s%d: destination address set\n",
269			 ifp->if_name, ifp->if_unit);
270		break;
271	case SIOCADDMULTI:
272	case SIOCDELMULTI:
273		if (ifr == 0) {
274			error = EAFNOSUPPORT;		/* XXX */
275			break;
276		}
277		switch (ifr->ifr_addr.sa_family) {
278
279#ifdef INET
280		case AF_INET:
281			break;
282#endif
283
284		default:
285			error = EAFNOSUPPORT;
286			break;
287		}
288		break;
289
290
291	default:
292		error = EINVAL;
293	}
294	splx(s);
295	return (error);
296}
297
298/*
299 * tunoutput - queue packets from higher level ready to put out.
300 */
301int
302tunoutput(ifp, m0, dst, rt)
303	struct ifnet   *ifp;
304	struct mbuf    *m0;
305	struct sockaddr *dst;
306	struct rtentry *rt;
307{
308	struct tun_softc *tp = &tunctl[ifp->if_unit];
309	struct proc	*p;
310	int		s;
311
312	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
313
314	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
315		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
316			  ifp->if_unit, tp->tun_flags);
317		m_freem (m0);
318		return EHOSTDOWN;
319	}
320
321#if NBPFILTER > 0
322	/* BPF write needs to be handled specially */
323	if (dst->sa_family == AF_UNSPEC) {
324		dst->sa_family = *(mtod(m0, int *));
325		m0->m_len -= sizeof(int);
326		m0->m_pkthdr.len -= sizeof(int);
327		m0->m_data += sizeof(int);
328	}
329
330	if (ifp->if_bpf) {
331		/*
332		 * We need to prepend the address family as
333		 * a four byte field.  Cons up a dummy header
334		 * to pacify bpf.  This is safe because bpf
335		 * will only read from the mbuf (i.e., it won't
336		 * try to free it or keep a pointer to it).
337		 */
338		struct mbuf m;
339		u_int af = dst->sa_family;
340
341		m.m_next = m0;
342		m.m_len = 4;
343		m.m_data = (char *)&af;
344
345		bpf_mtap(ifp, &m);
346	}
347#endif
348
349	switch(dst->sa_family) {
350#ifdef INET
351	case AF_INET:
352		s = splimp();
353		if (IF_QFULL(&ifp->if_snd)) {
354			IF_DROP(&ifp->if_snd);
355			m_freem(m0);
356			splx(s);
357			ifp->if_collisions++;
358			return (ENOBUFS);
359		}
360		ifp->if_obytes += m0->m_pkthdr.len;
361		IF_ENQUEUE(&ifp->if_snd, m0);
362		splx(s);
363		ifp->if_opackets++;
364		break;
365#endif
366	default:
367		m_freem(m0);
368		return EAFNOSUPPORT;
369	}
370
371	if (tp->tun_flags & TUN_RWAIT) {
372		tp->tun_flags &= ~TUN_RWAIT;
373		wakeup((caddr_t)tp);
374	}
375	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
376		if (tp->tun_pgrp > 0)
377			gsignal(tp->tun_pgrp, SIGIO);
378		else if ((p = pfind(-tp->tun_pgrp)) != 0)
379			psignal(p, SIGIO);
380	}
381	selwakeup(&tp->tun_rsel);
382	return 0;
383}
384
385/*
386 * the cdevsw interface is now pretty minimal.
387 */
388static	int
389tunioctl(dev, cmd, data, flag, p)
390	dev_t		dev;
391	int		cmd;
392	caddr_t		data;
393	int		flag;
394	struct proc	*p;
395{
396	int		unit = minor(dev), s;
397	struct tun_softc *tp = &tunctl[unit];
398 	struct tuninfo *tunp;
399
400	switch (cmd) {
401 	case TUNSIFINFO:
402 	        tunp = (struct tuninfo *)data;
403 		tp->tun_if.if_mtu = tunp->mtu;
404 		tp->tun_if.if_type = tunp->type;
405 		tp->tun_if.if_baudrate = tunp->baudrate;
406 		break;
407 	case TUNGIFINFO:
408 		tunp = (struct tuninfo *)data;
409 		tunp->mtu = tp->tun_if.if_mtu;
410 		tunp->type = tp->tun_if.if_type;
411 		tunp->baudrate = tp->tun_if.if_baudrate;
412 		break;
413	case TUNSDEBUG:
414		tundebug = *(int *)data;
415		break;
416	case TUNGDEBUG:
417		*(int *)data = tundebug;
418		break;
419	case FIONBIO:
420		if (*(int *)data)
421			tp->tun_flags |= TUN_NBIO;
422		else
423			tp->tun_flags &= ~TUN_NBIO;
424		break;
425	case FIOASYNC:
426		if (*(int *)data)
427			tp->tun_flags |= TUN_ASYNC;
428		else
429			tp->tun_flags &= ~TUN_ASYNC;
430		break;
431	case FIONREAD:
432		s = splimp();
433		if (tp->tun_if.if_snd.ifq_head) {
434			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
435			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
436				*(int *)data += mb->m_len;
437		} else
438			*(int *)data = 0;
439		splx(s);
440		break;
441	case TIOCSPGRP:
442		tp->tun_pgrp = *(int *)data;
443		break;
444	case TIOCGPGRP:
445		*(int *)data = tp->tun_pgrp;
446		break;
447	default:
448		return (ENOTTY);
449	}
450	return (0);
451}
452
453/*
454 * The cdevsw read interface - reads a packet at a time, or at
455 * least as much of a packet as can be read.
456 */
457static	int
458tunread(dev_t dev, struct uio *uio, int flag)
459{
460	int		unit = minor(dev);
461	struct tun_softc *tp = &tunctl[unit];
462	struct ifnet	*ifp = &tp->tun_if;
463	struct mbuf	*m, *m0;
464	int		error=0, len, s;
465
466	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
467	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
468		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
469			  ifp->if_unit, tp->tun_flags);
470		return EHOSTDOWN;
471	}
472
473	tp->tun_flags &= ~TUN_RWAIT;
474
475	s = splimp();
476	do {
477		IF_DEQUEUE(&ifp->if_snd, m0);
478		if (m0 == 0) {
479			if (tp->tun_flags & TUN_NBIO) {
480				splx(s);
481				return EWOULDBLOCK;
482			}
483			tp->tun_flags |= TUN_RWAIT;
484			if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
485					"tunread", 0)) {
486				splx(s);
487				return error;
488			}
489		}
490	} while (m0 == 0);
491	splx(s);
492
493	while (m0 && uio->uio_resid > 0 && error == 0) {
494		len = min(uio->uio_resid, m0->m_len);
495		if (len == 0)
496			break;
497		error = uiomove(mtod(m0, caddr_t), len, uio);
498		MFREE(m0, m);
499		m0 = m;
500	}
501
502	if (m0) {
503		TUNDEBUG("Dropping mbuf\n");
504		m_freem(m0);
505	}
506	return error;
507}
508
509/*
510 * the cdevsw write interface - an atomic write is a packet - or else!
511 */
512static	int
513tunwrite(dev_t dev, struct uio *uio, int flag)
514{
515	int		unit = minor (dev);
516	struct ifnet	*ifp = &tunctl[unit].tun_if;
517	struct mbuf	*top, **mp, *m;
518	int		error=0, s, tlen, mlen;
519
520	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
521
522	if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
523		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
524		    uio->uio_resid);
525		return EIO;
526	}
527	tlen = uio->uio_resid;
528
529	/* get a header mbuf */
530	MGETHDR(m, M_DONTWAIT, MT_DATA);
531	if (m == NULL)
532		return ENOBUFS;
533	mlen = MHLEN;
534
535	top = 0;
536	mp = &top;
537	while (error == 0 && uio->uio_resid > 0) {
538		m->m_len = min(mlen, uio->uio_resid);
539		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
540		*mp = m;
541		mp = &m->m_next;
542		if (uio->uio_resid > 0) {
543			MGET (m, M_DONTWAIT, MT_DATA);
544			if (m == 0) {
545				error = ENOBUFS;
546				break;
547			}
548			mlen = MLEN;
549		}
550	}
551	if (error) {
552		if (top)
553			m_freem (top);
554		return error;
555	}
556
557	top->m_pkthdr.len = tlen;
558	top->m_pkthdr.rcvif = ifp;
559
560#if NBPFILTER > 0
561	if (ifp->if_bpf) {
562		/*
563		 * We need to prepend the address family as
564		 * a four byte field.  Cons up a dummy header
565		 * to pacify bpf.  This is safe because bpf
566		 * will only read from the mbuf (i.e., it won't
567		 * try to free it or keep a pointer to it).
568		 */
569		struct mbuf m;
570		u_int af = AF_INET;
571
572		m.m_next = top;
573		m.m_len = 4;
574		m.m_data = (char *)&af;
575
576		bpf_mtap(ifp, &m);
577	}
578#endif
579
580	s = splimp();
581	if (IF_QFULL (&ipintrq)) {
582		IF_DROP(&ipintrq);
583		splx(s);
584		ifp->if_collisions++;
585		m_freem(top);
586		return ENOBUFS;
587	}
588	IF_ENQUEUE(&ipintrq, top);
589	splx(s);
590	ifp->if_ibytes += tlen;
591	ifp->if_ipackets++;
592	schednetisr(NETISR_IP);
593	return error;
594}
595
596/*
597 * tunselect - the select interface, this is only useful on reads
598 * really. The write detect always returns true, write never blocks
599 * anyway, it either accepts the packet or drops it.
600 */
601static	int
602tunselect(dev_t dev, int rw, struct proc *p)
603{
604	int		unit = minor(dev), s;
605	struct tun_softc *tp = &tunctl[unit];
606	struct ifnet	*ifp = &tp->tun_if;
607
608	s = splimp();
609	TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
610
611	switch (rw) {
612	case FREAD:
613		if (ifp->if_snd.ifq_len > 0) {
614			splx(s);
615			TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
616			    ifp->if_unit, ifp->if_snd.ifq_len);
617			return 1;
618		}
619		selrecord(p, &tp->tun_rsel);
620		break;
621	case FWRITE:
622		splx(s);
623		return 1;
624	}
625	splx(s);
626	TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
627	return 0;
628}
629
630
631#endif  /* NTUN */
632