if_tun.c revision 36735
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/kernel.h>
34#include <sys/sysctl.h>
35#ifdef DEVFS
36#include <sys/devfsext.h>
37#endif /*DEVFS*/
38#include <sys/conf.h>
39#include <sys/uio.h>
40/*
41 * XXX stop <sys/vnode.h> from including <vnode_if.h>.  <vnode_if.h> doesn't
42 * exist if we are an LKM.
43 */
44#undef KERNEL
45#include <sys/vnode.h>
46#define KERNEL
47
48#include <net/if.h>
49#include <net/netisr.h>
50#include <net/route.h>
51
52#ifdef INET
53#include <netinet/in.h>
54#include <netinet/in_var.h>
55#endif
56
57#ifdef NS
58#include <netns/ns.h>
59#include <netns/ns_if.h>
60#endif
61
62#include "bpfilter.h"
63#if NBPFILTER > 0
64#include <net/bpf.h>
65#endif
66
67#include <net/if_tunvar.h>
68#include <net/if_tun.h>
69
70static void tunattach __P((void *));
71PSEUDO_SET(tunattach, if_tun);
72
73#define TUNDEBUG	if (tundebug) printf
74static int tundebug = 0;
75SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
76
77static struct tun_softc tunctl[NTUN];
78
79static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
80	    struct rtentry *rt));
81static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
82static int tuninit __P((int));
83
84static	d_open_t	tunopen;
85static	d_close_t	tunclose;
86static	d_read_t	tunread;
87static	d_write_t	tunwrite;
88static	d_ioctl_t	tunioctl;
89static	d_poll_t	tunpoll;
90
91#define CDEV_MAJOR 52
92static struct cdevsw tun_cdevsw = {
93	tunopen,	tunclose,	tunread,	tunwrite,
94	tunioctl,	nullstop,	noreset,	nodevtotty,
95	tunpoll,	nommap,		nostrategy,	"tun",	NULL,	-1
96};
97
98
99static	int	tun_devsw_installed;
100#ifdef	DEVFS
101static	void	*tun_devfs_token[NTUN];
102#endif
103
104#define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
105#define dev_val(n) (((n) >> 8) | ((n) & 0xff))
106
107static void
108tunattach(dummy)
109	void *dummy;
110{
111	register int i;
112	struct ifnet *ifp;
113	dev_t dev;
114
115	if ( tun_devsw_installed )
116		return;
117	dev = makedev(CDEV_MAJOR, 0);
118	cdevsw_add(&dev, &tun_cdevsw, NULL);
119	tun_devsw_installed = 1;
120	for ( i = 0; i < NTUN; i++ ) {
121#ifdef DEVFS
122		tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
123						      DV_CHR, UID_UUCP,
124						      GID_DIALER, 0600,
125						      "tun%d", i);
126#endif
127		tunctl[i].tun_flags = TUN_INITED;
128
129		ifp = &tunctl[i].tun_if;
130		ifp->if_unit = i;
131		ifp->if_name = "tun";
132		ifp->if_mtu = TUNMTU;
133		ifp->if_ioctl = tunifioctl;
134		ifp->if_output = tunoutput;
135		ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
136		ifp->if_snd.ifq_maxlen = ifqmaxlen;
137		if_attach(ifp);
138#if NBPFILTER > 0
139		bpfattach(ifp, DLT_NULL, sizeof(u_int));
140#endif
141	}
142}
143
144/*
145 * tunnel open - must be superuser & the device must be
146 * configured in
147 */
148static	int
149tunopen(dev, flag, mode, p)
150	dev_t	dev;
151	int	flag, mode;
152	struct proc *p;
153{
154	struct ifnet	*ifp;
155	struct tun_softc *tp;
156	register int	unit, error;
157
158	error = suser(p->p_ucred, &p->p_acflag);
159	if (error)
160		return (error);
161
162	if ((unit = dev_val(minor(dev))) >= NTUN)
163		return (ENXIO);
164	tp = &tunctl[unit];
165	if (tp->tun_flags & TUN_OPEN)
166		return EBUSY;
167	ifp = &tp->tun_if;
168	tp->tun_flags |= TUN_OPEN;
169	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
170	return (0);
171}
172
173/*
174 * tunclose - close the device - mark i/f down & delete
175 * routing info
176 */
177static	int
178tunclose(dev, foo, bar, p)
179	dev_t dev;
180	int foo;
181	int bar;
182	struct proc *p;
183{
184	register int	unit = dev_val(minor(dev)), s;
185	struct tun_softc *tp = &tunctl[unit];
186	struct ifnet	*ifp = &tp->tun_if;
187	struct mbuf	*m;
188
189	tp->tun_flags &= ~TUN_OPEN;
190
191	/*
192	 * junk all pending output
193	 */
194	do {
195		s = splimp();
196		IF_DEQUEUE(&ifp->if_snd, m);
197		splx(s);
198		if (m)
199			m_freem(m);
200	} while (m);
201
202	if (ifp->if_flags & IFF_UP) {
203		s = splimp();
204		if_down(ifp);
205		if (ifp->if_flags & IFF_RUNNING) {
206		    /* find internet addresses and delete routes */
207		    register struct ifaddr *ifa;
208		    for (ifa = ifp->if_addrhead.tqh_first; ifa;
209			 ifa = ifa->ifa_link.tqe_next) {
210			if (ifa->ifa_addr->sa_family == AF_INET) {
211			    rtinit(ifa, (int)RTM_DELETE,
212				   tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
213			}
214		    }
215		}
216		splx(s);
217	}
218	tp->tun_pgrp = 0;
219	selwakeup(&tp->tun_rsel);
220
221	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
222	return (0);
223}
224
225static int
226tuninit(unit)
227	int	unit;
228{
229	struct tun_softc *tp = &tunctl[unit];
230	struct ifnet	*ifp = &tp->tun_if;
231	register struct ifaddr *ifa;
232
233	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
234
235	ifp->if_flags |= IFF_UP | IFF_RUNNING;
236	getmicrotime(&ifp->if_lastchange);
237
238	for (ifa = ifp->if_addrhead.tqh_first; ifa;
239	     ifa = ifa->ifa_link.tqe_next) {
240#ifdef INET
241		if (ifa->ifa_addr->sa_family == AF_INET) {
242		    struct sockaddr_in *si;
243
244		    si = (struct sockaddr_in *)ifa->ifa_addr;
245		    if (si && si->sin_addr.s_addr)
246			    tp->tun_flags |= TUN_IASET;
247
248		    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
249		    if (si && si->sin_addr.s_addr)
250			    tp->tun_flags |= TUN_DSTADDR;
251		}
252#endif
253	}
254	return 0;
255}
256
257/*
258 * Process an ioctl request.
259 */
260int
261tunifioctl(ifp, cmd, data)
262	struct ifnet *ifp;
263	u_long	cmd;
264	caddr_t	data;
265{
266	register struct ifreq *ifr = (struct ifreq *)data;
267	int		error = 0, s;
268
269	s = splimp();
270	switch(cmd) {
271	case SIOCSIFADDR:
272		tuninit(ifp->if_unit);
273		TUNDEBUG("%s%d: address set\n",
274			 ifp->if_name, ifp->if_unit);
275		break;
276	case SIOCSIFDSTADDR:
277		tuninit(ifp->if_unit);
278		TUNDEBUG("%s%d: destination address set\n",
279			 ifp->if_name, ifp->if_unit);
280		break;
281	case SIOCSIFMTU:
282		ifp->if_mtu = ifr->ifr_mtu;
283		TUNDEBUG("%s%d: mtu set\n",
284			 ifp->if_name, ifp->if_unit);
285		break;
286	case SIOCADDMULTI:
287	case SIOCDELMULTI:
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	u_long		cmd;
392	caddr_t		data;
393	int		flag;
394	struct proc	*p;
395{
396	int		unit = dev_val(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		break;
421	case FIOASYNC:
422		if (*(int *)data)
423			tp->tun_flags |= TUN_ASYNC;
424		else
425			tp->tun_flags &= ~TUN_ASYNC;
426		break;
427	case FIONREAD:
428		s = splimp();
429		if (tp->tun_if.if_snd.ifq_head) {
430			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
431			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
432				*(int *)data += mb->m_len;
433		} else
434			*(int *)data = 0;
435		splx(s);
436		break;
437	case TIOCSPGRP:
438		tp->tun_pgrp = *(int *)data;
439		break;
440	case TIOCGPGRP:
441		*(int *)data = tp->tun_pgrp;
442		break;
443	default:
444		return (ENOTTY);
445	}
446	return (0);
447}
448
449/*
450 * The cdevsw read interface - reads a packet at a time, or at
451 * least as much of a packet as can be read.
452 */
453static	int
454tunread(dev, uio, flag)
455	dev_t dev;
456	struct uio *uio;
457	int flag;
458{
459	int		unit = dev_val(minor(dev));
460	struct tun_softc *tp = &tunctl[unit];
461	struct ifnet	*ifp = &tp->tun_if;
462	struct mbuf	*m, *m0;
463	int		error=0, len, s;
464
465	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
466	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
467		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
468			  ifp->if_unit, tp->tun_flags);
469		return EHOSTDOWN;
470	}
471
472	tp->tun_flags &= ~TUN_RWAIT;
473
474	s = splimp();
475	do {
476		IF_DEQUEUE(&ifp->if_snd, m0);
477		if (m0 == 0) {
478			if (flag & IO_NDELAY) {
479				splx(s);
480				return EWOULDBLOCK;
481			}
482			tp->tun_flags |= TUN_RWAIT;
483			if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
484					"tunread", 0)) {
485				splx(s);
486				return error;
487			}
488		}
489	} while (m0 == 0);
490	splx(s);
491
492	while (m0 && uio->uio_resid > 0 && error == 0) {
493		len = min(uio->uio_resid, m0->m_len);
494		if (len == 0)
495			break;
496		error = uiomove(mtod(m0, caddr_t), len, uio);
497		MFREE(m0, m);
498		m0 = m;
499	}
500
501	if (m0) {
502		TUNDEBUG("Dropping mbuf\n");
503		m_freem(m0);
504	}
505	return error;
506}
507
508/*
509 * the cdevsw write interface - an atomic write is a packet - or else!
510 */
511static	int
512tunwrite(dev, uio, flag)
513	dev_t dev;
514	struct uio *uio;
515	int flag;
516{
517	int		unit = dev_val(minor(dev));
518	struct ifnet	*ifp = &tunctl[unit].tun_if;
519	struct mbuf	*top, **mp, *m;
520	int		error=0, s, tlen, mlen;
521
522	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
523
524	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
525		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
526		    uio->uio_resid);
527		return EIO;
528	}
529	tlen = uio->uio_resid;
530
531	/* get a header mbuf */
532	MGETHDR(m, M_DONTWAIT, MT_DATA);
533	if (m == NULL)
534		return ENOBUFS;
535	mlen = MHLEN;
536
537	top = 0;
538	mp = &top;
539	while (error == 0 && uio->uio_resid > 0) {
540		m->m_len = min(mlen, uio->uio_resid);
541		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
542		*mp = m;
543		mp = &m->m_next;
544		if (uio->uio_resid > 0) {
545			MGET (m, M_DONTWAIT, MT_DATA);
546			if (m == 0) {
547				error = ENOBUFS;
548				break;
549			}
550			mlen = MLEN;
551		}
552	}
553	if (error) {
554		if (top)
555			m_freem (top);
556		return error;
557	}
558
559	top->m_pkthdr.len = tlen;
560	top->m_pkthdr.rcvif = ifp;
561
562#if NBPFILTER > 0
563	if (ifp->if_bpf) {
564		/*
565		 * We need to prepend the address family as
566		 * a four byte field.  Cons up a dummy header
567		 * to pacify bpf.  This is safe because bpf
568		 * will only read from the mbuf (i.e., it won't
569		 * try to free it or keep a pointer to it).
570		 */
571		struct mbuf m;
572		u_int af = AF_INET;
573
574		m.m_next = top;
575		m.m_len = 4;
576		m.m_data = (char *)&af;
577
578		bpf_mtap(ifp, &m);
579	}
580#endif
581
582#ifdef INET
583	s = splimp();
584	if (IF_QFULL (&ipintrq)) {
585		IF_DROP(&ipintrq);
586		splx(s);
587		ifp->if_collisions++;
588		m_freem(top);
589		return ENOBUFS;
590	}
591	IF_ENQUEUE(&ipintrq, top);
592	splx(s);
593	ifp->if_ibytes += tlen;
594	ifp->if_ipackets++;
595	schednetisr(NETISR_IP);
596#endif
597	return error;
598}
599
600/*
601 * tunpoll - the poll interface, this is only useful on reads
602 * really. The write detect always returns true, write never blocks
603 * anyway, it either accepts the packet or drops it.
604 */
605static	int
606tunpoll(dev, events, p)
607	dev_t dev;
608	int events;
609	struct proc *p;
610{
611	int		unit = dev_val(minor(dev)), s;
612	struct tun_softc *tp = &tunctl[unit];
613	struct ifnet	*ifp = &tp->tun_if;
614	int		revents = 0;
615
616	s = splimp();
617	TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
618
619	if (events & (POLLIN | POLLRDNORM))
620		if (ifp->if_snd.ifq_len > 0) {
621			TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
622			    ifp->if_unit, ifp->if_snd.ifq_len);
623			revents |= events & (POLLIN | POLLRDNORM);
624		} else {
625			TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
626			    ifp->if_unit);
627			selrecord(p, &tp->tun_rsel);
628		}
629
630	if (events & (POLLOUT | POLLWRNORM))
631		revents |= events & (POLLOUT | POLLWRNORM);
632
633	splx(s);
634	return (revents);
635}
636
637
638#endif  /* NTUN */
639