if_tun.c revision 42769
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	tunopen,	tunclose,	tunread,	tunwrite,
89	tunioctl,	nullstop,	noreset,	nodevtotty,
90	tunpoll,	nommap,		nostrategy,	"tun",	NULL,	-1
91};
92
93
94static	int	tun_devsw_installed;
95#ifdef	DEVFS
96static	void	*tun_devfs_token[NTUN];
97#endif
98
99#define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
100#define dev_val(n) (((n) >> 8) | ((n) & 0xff))
101
102static void
103tunattach(dummy)
104	void *dummy;
105{
106	register int i;
107	struct ifnet *ifp;
108	dev_t dev;
109
110	if ( tun_devsw_installed )
111		return;
112	dev = makedev(CDEV_MAJOR, 0);
113	cdevsw_add(&dev, &tun_cdevsw, NULL);
114	tun_devsw_installed = 1;
115	for ( i = 0; i < NTUN; i++ ) {
116#ifdef DEVFS
117		tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
118						      DV_CHR, UID_UUCP,
119						      GID_DIALER, 0600,
120						      "tun%d", i);
121#endif
122		tunctl[i].tun_flags = TUN_INITED;
123
124		ifp = &tunctl[i].tun_if;
125		ifp->if_unit = i;
126		ifp->if_name = "tun";
127		ifp->if_mtu = TUNMTU;
128		ifp->if_ioctl = tunifioctl;
129		ifp->if_output = tunoutput;
130		ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
131		ifp->if_snd.ifq_maxlen = ifqmaxlen;
132		if_attach(ifp);
133#if NBPFILTER > 0
134		bpfattach(ifp, DLT_NULL, sizeof(u_int));
135#endif
136	}
137}
138
139/*
140 * tunnel open - must be superuser & the device must be
141 * configured in
142 */
143static	int
144tunopen(dev, flag, mode, p)
145	dev_t	dev;
146	int	flag, mode;
147	struct proc *p;
148{
149	struct ifnet	*ifp;
150	struct tun_softc *tp;
151	register int	unit, error;
152
153	error = suser(p->p_ucred, &p->p_acflag);
154	if (error)
155		return (error);
156
157	if ((unit = dev_val(minor(dev))) >= NTUN)
158		return (ENXIO);
159	tp = &tunctl[unit];
160	if (tp->tun_flags & TUN_OPEN)
161		return EBUSY;
162	ifp = &tp->tun_if;
163	tp->tun_flags |= TUN_OPEN;
164	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
165	return (0);
166}
167
168/*
169 * tunclose - close the device - mark i/f down & delete
170 * routing info
171 */
172static	int
173tunclose(dev, foo, bar, p)
174	dev_t dev;
175	int foo;
176	int bar;
177	struct proc *p;
178{
179	register int	unit = dev_val(minor(dev)), s;
180	struct tun_softc *tp = &tunctl[unit];
181	struct ifnet	*ifp = &tp->tun_if;
182	struct mbuf	*m;
183
184	tp->tun_flags &= ~TUN_OPEN;
185
186	/*
187	 * junk all pending output
188	 */
189	do {
190		s = splimp();
191		IF_DEQUEUE(&ifp->if_snd, m);
192		splx(s);
193		if (m)
194			m_freem(m);
195	} while (m);
196
197	if (ifp->if_flags & IFF_UP) {
198		s = splimp();
199		if_down(ifp);
200		if (ifp->if_flags & IFF_RUNNING) {
201		    /* find internet addresses and delete routes */
202		    register struct ifaddr *ifa;
203		    for (ifa = ifp->if_addrhead.tqh_first; ifa;
204			 ifa = ifa->ifa_link.tqe_next) {
205			if (ifa->ifa_addr->sa_family == AF_INET) {
206			    rtinit(ifa, (int)RTM_DELETE,
207				   tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
208			}
209		    }
210		}
211		splx(s);
212	}
213	funsetown(tp->tun_sigio);
214	selwakeup(&tp->tun_rsel);
215
216	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
217	return (0);
218}
219
220static int
221tuninit(unit)
222	int	unit;
223{
224	struct tun_softc *tp = &tunctl[unit];
225	struct ifnet	*ifp = &tp->tun_if;
226	register struct ifaddr *ifa;
227
228	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
229
230	ifp->if_flags |= IFF_UP | IFF_RUNNING;
231	getmicrotime(&ifp->if_lastchange);
232
233	for (ifa = ifp->if_addrhead.tqh_first; ifa;
234	     ifa = ifa->ifa_link.tqe_next) {
235#ifdef INET
236		if (ifa->ifa_addr->sa_family == AF_INET) {
237		    struct sockaddr_in *si;
238
239		    si = (struct sockaddr_in *)ifa->ifa_addr;
240		    if (si && si->sin_addr.s_addr)
241			    tp->tun_flags |= TUN_IASET;
242
243		    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
244		    if (si && si->sin_addr.s_addr)
245			    tp->tun_flags |= TUN_DSTADDR;
246		}
247#endif
248	}
249	return 0;
250}
251
252/*
253 * Process an ioctl request.
254 */
255int
256tunifioctl(ifp, cmd, data)
257	struct ifnet *ifp;
258	u_long	cmd;
259	caddr_t	data;
260{
261	register struct ifreq *ifr = (struct ifreq *)data;
262	int		error = 0, s;
263
264	s = splimp();
265	switch(cmd) {
266	case SIOCSIFADDR:
267		tuninit(ifp->if_unit);
268		TUNDEBUG("%s%d: address set\n",
269			 ifp->if_name, ifp->if_unit);
270		break;
271	case SIOCSIFDSTADDR:
272		tuninit(ifp->if_unit);
273		TUNDEBUG("%s%d: destination address set\n",
274			 ifp->if_name, ifp->if_unit);
275		break;
276	case SIOCSIFMTU:
277		ifp->if_mtu = ifr->ifr_mtu;
278		TUNDEBUG("%s%d: mtu set\n",
279			 ifp->if_name, ifp->if_unit);
280		break;
281	case SIOCADDMULTI:
282	case SIOCDELMULTI:
283		break;
284
285
286	default:
287		error = EINVAL;
288	}
289	splx(s);
290	return (error);
291}
292
293/*
294 * tunoutput - queue packets from higher level ready to put out.
295 */
296int
297tunoutput(ifp, m0, dst, rt)
298	struct ifnet   *ifp;
299	struct mbuf    *m0;
300	struct sockaddr *dst;
301	struct rtentry *rt;
302{
303	struct tun_softc *tp = &tunctl[ifp->if_unit];
304	int		s;
305
306	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
307
308	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
309		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
310			  ifp->if_unit, tp->tun_flags);
311		m_freem (m0);
312		return EHOSTDOWN;
313	}
314
315#if NBPFILTER > 0
316	/* BPF write needs to be handled specially */
317	if (dst->sa_family == AF_UNSPEC) {
318		dst->sa_family = *(mtod(m0, int *));
319		m0->m_len -= sizeof(int);
320		m0->m_pkthdr.len -= sizeof(int);
321		m0->m_data += sizeof(int);
322	}
323
324	if (ifp->if_bpf) {
325		/*
326		 * We need to prepend the address family as
327		 * a four byte field.  Cons up a dummy header
328		 * to pacify bpf.  This is safe because bpf
329		 * will only read from the mbuf (i.e., it won't
330		 * try to free it or keep a pointer to it).
331		 */
332		struct mbuf m;
333		u_int af = dst->sa_family;
334
335		m.m_next = m0;
336		m.m_len = 4;
337		m.m_data = (char *)&af;
338
339		bpf_mtap(ifp, &m);
340	}
341#endif
342
343	switch(dst->sa_family) {
344#ifdef INET
345	case AF_INET:
346		s = splimp();
347		if (IF_QFULL(&ifp->if_snd)) {
348			IF_DROP(&ifp->if_snd);
349			m_freem(m0);
350			splx(s);
351			ifp->if_collisions++;
352			return (ENOBUFS);
353		}
354		ifp->if_obytes += m0->m_pkthdr.len;
355		IF_ENQUEUE(&ifp->if_snd, m0);
356		splx(s);
357		ifp->if_opackets++;
358		break;
359#endif
360	default:
361		m_freem(m0);
362		return EAFNOSUPPORT;
363	}
364
365	if (tp->tun_flags & TUN_RWAIT) {
366		tp->tun_flags &= ~TUN_RWAIT;
367		wakeup((caddr_t)tp);
368	}
369	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
370		pgsigio(tp->tun_sigio, SIGIO, 0);
371	selwakeup(&tp->tun_rsel);
372	return 0;
373}
374
375/*
376 * the cdevsw interface is now pretty minimal.
377 */
378static	int
379tunioctl(dev, cmd, data, flag, p)
380	dev_t		dev;
381	u_long		cmd;
382	caddr_t		data;
383	int		flag;
384	struct proc	*p;
385{
386	int		unit = dev_val(minor(dev)), s;
387	struct tun_softc *tp = &tunctl[unit];
388 	struct tuninfo *tunp;
389
390	switch (cmd) {
391 	case TUNSIFINFO:
392 	        tunp = (struct tuninfo *)data;
393 		tp->tun_if.if_mtu = tunp->mtu;
394 		tp->tun_if.if_type = tunp->type;
395 		tp->tun_if.if_baudrate = tunp->baudrate;
396 		break;
397 	case TUNGIFINFO:
398 		tunp = (struct tuninfo *)data;
399 		tunp->mtu = tp->tun_if.if_mtu;
400 		tunp->type = tp->tun_if.if_type;
401 		tunp->baudrate = tp->tun_if.if_baudrate;
402 		break;
403	case TUNSDEBUG:
404		tundebug = *(int *)data;
405		break;
406	case TUNGDEBUG:
407		*(int *)data = tundebug;
408		break;
409	case FIONBIO:
410		break;
411	case FIOASYNC:
412		if (*(int *)data)
413			tp->tun_flags |= TUN_ASYNC;
414		else
415			tp->tun_flags &= ~TUN_ASYNC;
416		break;
417	case FIONREAD:
418		s = splimp();
419		if (tp->tun_if.if_snd.ifq_head) {
420			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
421			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
422				*(int *)data += mb->m_len;
423		} else
424			*(int *)data = 0;
425		splx(s);
426		break;
427	case FIOSETOWN:
428		return (fsetown(*(int *)data, &tp->tun_sigio));
429
430	case FIOGETOWN:
431		*(int *)data = fgetown(tp->tun_sigio);
432		return (0);
433
434	/* This is deprecated, FIOSETOWN should be used instead. */
435	case TIOCSPGRP:
436		return (fsetown(-(*(int *)data), &tp->tun_sigio));
437
438	/* This is deprecated, FIOGETOWN should be used instead. */
439	case TIOCGPGRP:
440		*(int *)data = -fgetown(tp->tun_sigio);
441		return (0);
442
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