if_tun.c revision 21818
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 SIOCSIFMTU:
272		ifp->if_mtu = ifr->ifr_mtu;
273		TUNDEBUG("%s%d: mtu set\n",
274			 ifp->if_name, ifp->if_unit);
275		break;
276	case SIOCADDMULTI:
277	case SIOCDELMULTI:
278		break;
279
280
281	default:
282		error = EINVAL;
283	}
284	splx(s);
285	return (error);
286}
287
288/*
289 * tunoutput - queue packets from higher level ready to put out.
290 */
291int
292tunoutput(ifp, m0, dst, rt)
293	struct ifnet   *ifp;
294	struct mbuf    *m0;
295	struct sockaddr *dst;
296	struct rtentry *rt;
297{
298	struct tun_softc *tp = &tunctl[ifp->if_unit];
299	struct proc	*p;
300	int		s;
301
302	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
303
304	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
305		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
306			  ifp->if_unit, tp->tun_flags);
307		m_freem (m0);
308		return EHOSTDOWN;
309	}
310
311#if NBPFILTER > 0
312	/* BPF write needs to be handled specially */
313	if (dst->sa_family == AF_UNSPEC) {
314		dst->sa_family = *(mtod(m0, int *));
315		m0->m_len -= sizeof(int);
316		m0->m_pkthdr.len -= sizeof(int);
317		m0->m_data += sizeof(int);
318	}
319
320	if (ifp->if_bpf) {
321		/*
322		 * We need to prepend the address family as
323		 * a four byte field.  Cons up a dummy header
324		 * to pacify bpf.  This is safe because bpf
325		 * will only read from the mbuf (i.e., it won't
326		 * try to free it or keep a pointer to it).
327		 */
328		struct mbuf m;
329		u_int af = dst->sa_family;
330
331		m.m_next = m0;
332		m.m_len = 4;
333		m.m_data = (char *)&af;
334
335		bpf_mtap(ifp, &m);
336	}
337#endif
338
339	switch(dst->sa_family) {
340#ifdef INET
341	case AF_INET:
342		s = splimp();
343		if (IF_QFULL(&ifp->if_snd)) {
344			IF_DROP(&ifp->if_snd);
345			m_freem(m0);
346			splx(s);
347			ifp->if_collisions++;
348			return (ENOBUFS);
349		}
350		ifp->if_obytes += m0->m_pkthdr.len;
351		IF_ENQUEUE(&ifp->if_snd, m0);
352		splx(s);
353		ifp->if_opackets++;
354		break;
355#endif
356	default:
357		m_freem(m0);
358		return EAFNOSUPPORT;
359	}
360
361	if (tp->tun_flags & TUN_RWAIT) {
362		tp->tun_flags &= ~TUN_RWAIT;
363		wakeup((caddr_t)tp);
364	}
365	if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
366		if (tp->tun_pgrp > 0)
367			gsignal(tp->tun_pgrp, SIGIO);
368		else if ((p = pfind(-tp->tun_pgrp)) != 0)
369			psignal(p, SIGIO);
370	}
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	int		cmd;
382	caddr_t		data;
383	int		flag;
384	struct proc	*p;
385{
386	int		unit = 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		if (*(int *)data)
411			tp->tun_flags |= TUN_NBIO;
412		else
413			tp->tun_flags &= ~TUN_NBIO;
414		break;
415	case FIOASYNC:
416		if (*(int *)data)
417			tp->tun_flags |= TUN_ASYNC;
418		else
419			tp->tun_flags &= ~TUN_ASYNC;
420		break;
421	case FIONREAD:
422		s = splimp();
423		if (tp->tun_if.if_snd.ifq_head) {
424			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
425			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
426				*(int *)data += mb->m_len;
427		} else
428			*(int *)data = 0;
429		splx(s);
430		break;
431	case TIOCSPGRP:
432		tp->tun_pgrp = *(int *)data;
433		break;
434	case TIOCGPGRP:
435		*(int *)data = tp->tun_pgrp;
436		break;
437	default:
438		return (ENOTTY);
439	}
440	return (0);
441}
442
443/*
444 * The cdevsw read interface - reads a packet at a time, or at
445 * least as much of a packet as can be read.
446 */
447static	int
448tunread(dev_t dev, struct uio *uio, int flag)
449{
450	int		unit = minor(dev);
451	struct tun_softc *tp = &tunctl[unit];
452	struct ifnet	*ifp = &tp->tun_if;
453	struct mbuf	*m, *m0;
454	int		error=0, len, s;
455
456	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
457	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
458		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
459			  ifp->if_unit, tp->tun_flags);
460		return EHOSTDOWN;
461	}
462
463	tp->tun_flags &= ~TUN_RWAIT;
464
465	s = splimp();
466	do {
467		IF_DEQUEUE(&ifp->if_snd, m0);
468		if (m0 == 0) {
469			if (tp->tun_flags & TUN_NBIO) {
470				splx(s);
471				return EWOULDBLOCK;
472			}
473			tp->tun_flags |= TUN_RWAIT;
474			if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
475					"tunread", 0)) {
476				splx(s);
477				return error;
478			}
479		}
480	} while (m0 == 0);
481	splx(s);
482
483	while (m0 && uio->uio_resid > 0 && error == 0) {
484		len = min(uio->uio_resid, m0->m_len);
485		if (len == 0)
486			break;
487		error = uiomove(mtod(m0, caddr_t), len, uio);
488		MFREE(m0, m);
489		m0 = m;
490	}
491
492	if (m0) {
493		TUNDEBUG("Dropping mbuf\n");
494		m_freem(m0);
495	}
496	return error;
497}
498
499/*
500 * the cdevsw write interface - an atomic write is a packet - or else!
501 */
502static	int
503tunwrite(dev_t dev, struct uio *uio, int flag)
504{
505	int		unit = minor (dev);
506	struct ifnet	*ifp = &tunctl[unit].tun_if;
507	struct mbuf	*top, **mp, *m;
508	int		error=0, s, tlen, mlen;
509
510	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
511
512	if (uio->uio_resid < 0 || uio->uio_resid > ifp->if_mtu) {
513		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
514		    uio->uio_resid);
515		return EIO;
516	}
517	tlen = uio->uio_resid;
518
519	/* get a header mbuf */
520	MGETHDR(m, M_DONTWAIT, MT_DATA);
521	if (m == NULL)
522		return ENOBUFS;
523	mlen = MHLEN;
524
525	top = 0;
526	mp = &top;
527	while (error == 0 && uio->uio_resid > 0) {
528		m->m_len = min(mlen, uio->uio_resid);
529		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
530		*mp = m;
531		mp = &m->m_next;
532		if (uio->uio_resid > 0) {
533			MGET (m, M_DONTWAIT, MT_DATA);
534			if (m == 0) {
535				error = ENOBUFS;
536				break;
537			}
538			mlen = MLEN;
539		}
540	}
541	if (error) {
542		if (top)
543			m_freem (top);
544		return error;
545	}
546
547	top->m_pkthdr.len = tlen;
548	top->m_pkthdr.rcvif = ifp;
549
550#if NBPFILTER > 0
551	if (ifp->if_bpf) {
552		/*
553		 * We need to prepend the address family as
554		 * a four byte field.  Cons up a dummy header
555		 * to pacify bpf.  This is safe because bpf
556		 * will only read from the mbuf (i.e., it won't
557		 * try to free it or keep a pointer to it).
558		 */
559		struct mbuf m;
560		u_int af = AF_INET;
561
562		m.m_next = top;
563		m.m_len = 4;
564		m.m_data = (char *)&af;
565
566		bpf_mtap(ifp, &m);
567	}
568#endif
569
570	s = splimp();
571	if (IF_QFULL (&ipintrq)) {
572		IF_DROP(&ipintrq);
573		splx(s);
574		ifp->if_collisions++;
575		m_freem(top);
576		return ENOBUFS;
577	}
578	IF_ENQUEUE(&ipintrq, top);
579	splx(s);
580	ifp->if_ibytes += tlen;
581	ifp->if_ipackets++;
582	schednetisr(NETISR_IP);
583	return error;
584}
585
586/*
587 * tunselect - the select interface, this is only useful on reads
588 * really. The write detect always returns true, write never blocks
589 * anyway, it either accepts the packet or drops it.
590 */
591static	int
592tunselect(dev_t dev, int rw, struct proc *p)
593{
594	int		unit = minor(dev), s;
595	struct tun_softc *tp = &tunctl[unit];
596	struct ifnet	*ifp = &tp->tun_if;
597
598	s = splimp();
599	TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
600
601	switch (rw) {
602	case FREAD:
603		if (ifp->if_snd.ifq_len > 0) {
604			splx(s);
605			TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
606			    ifp->if_unit, ifp->if_snd.ifq_len);
607			return 1;
608		}
609		selrecord(p, &tp->tun_rsel);
610		break;
611	case FWRITE:
612		splx(s);
613		return 1;
614	}
615	splx(s);
616	TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
617	return 0;
618}
619
620
621#endif  /* NTUN */
622