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