if_tun.c revision 51709
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 * $FreeBSD: head/sys/net/if_tun.c 51709 1999-09-27 00:55:29Z peter $
17 */
18
19#include "opt_inet.h"
20
21#include <sys/param.h>
22#include <sys/proc.h>
23#include <sys/systm.h>
24#include <sys/mbuf.h>
25#include <sys/socket.h>
26#include <sys/filio.h>
27#include <sys/sockio.h>
28#include <sys/ttycom.h>
29#include <sys/poll.h>
30#include <sys/signalvar.h>
31#include <sys/filedesc.h>
32#include <sys/kernel.h>
33#include <sys/sysctl.h>
34#include <sys/conf.h>
35#include <sys/uio.h>
36#include <sys/vnode.h>
37#include <sys/malloc.h>
38
39#include <net/if.h>
40#include <net/netisr.h>
41#include <net/route.h>
42
43#ifdef INET
44#include <netinet/in.h>
45#include <netinet/in_var.h>
46#endif
47
48#ifdef NS
49#include <netns/ns.h>
50#include <netns/ns_if.h>
51#endif
52
53#include <net/bpf.h>
54
55#include <net/if_tunvar.h>
56#include <net/if_tun.h>
57
58static MALLOC_DEFINE(M_TUN, "tun", "Tunnel Interface");
59
60static void tunattach __P((void *));
61PSEUDO_SET(tunattach, if_tun);
62
63static void tuncreate __P((dev_t dev));
64
65#define TUNDEBUG	if (tundebug) printf
66static int tundebug = 0;
67SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
68
69static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
70	    struct rtentry *rt));
71static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
72static int tuninit __P((struct ifnet *));
73
74static	d_open_t	tunopen;
75static	d_close_t	tunclose;
76static	d_read_t	tunread;
77static	d_write_t	tunwrite;
78static	d_ioctl_t	tunioctl;
79static	d_poll_t	tunpoll;
80
81#define CDEV_MAJOR 52
82static struct cdevsw tun_cdevsw = {
83	/* open */	tunopen,
84	/* close */	tunclose,
85	/* read */	tunread,
86	/* write */	tunwrite,
87	/* ioctl */	tunioctl,
88	/* poll */	tunpoll,
89	/* mmap */	nommap,
90	/* strategy */	nostrategy,
91	/* name */	"tun",
92	/* maj */	CDEV_MAJOR,
93	/* dump */	nodump,
94	/* psize */	nopsize,
95	/* flags */	0,
96	/* bmaj */	-1
97};
98
99static void
100tunattach(dummy)
101	void *dummy;
102{
103
104	cdevsw_add(&tun_cdevsw);
105}
106
107static void
108tuncreate(dev)
109	dev_t dev;
110{
111	struct tun_softc *sc;
112	struct ifnet *ifp;
113
114	dev = make_dev(&tun_cdevsw, minor(dev),
115	    UID_UUCP, GID_DIALER, 0600, "tun%d", lminor(dev));
116
117	MALLOC(sc, struct tun_softc *, sizeof(*sc), M_TUN, M_WAITOK);
118	bzero(sc, sizeof *sc);
119	sc->tun_flags = TUN_INITED;
120
121	ifp = &sc->tun_if;
122	ifp->if_unit = lminor(dev);
123	ifp->if_name = "tun";
124	ifp->if_mtu = TUNMTU;
125	ifp->if_ioctl = tunifioctl;
126	ifp->if_output = tunoutput;
127	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
128	ifp->if_snd.ifq_maxlen = ifqmaxlen;
129	ifp->if_softc = sc;
130	if_attach(ifp);
131	bpfattach(ifp, DLT_NULL, sizeof(u_int));
132	dev->si_drv1 = sc;
133}
134
135/*
136 * tunnel open - must be superuser & the device must be
137 * configured in
138 */
139static	int
140tunopen(dev, flag, mode, p)
141	dev_t	dev;
142	int	flag, mode;
143	struct proc *p;
144{
145	struct ifnet	*ifp;
146	struct tun_softc *tp;
147	register int	error;
148
149	error = suser(p);
150	if (error)
151		return (error);
152
153	tp = dev->si_drv1;
154	if (!tp) {
155		tuncreate(dev);
156		tp = dev->si_drv1;
157	}
158	if (tp->tun_flags & TUN_OPEN)
159		return EBUSY;
160	tp->tun_pid = p->p_pid;
161	ifp = &tp->tun_if;
162	tp->tun_flags |= TUN_OPEN;
163	TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
164	return (0);
165}
166
167/*
168 * tunclose - close the device - mark i/f down & delete
169 * routing info
170 */
171static	int
172tunclose(dev, foo, bar, p)
173	dev_t dev;
174	int foo;
175	int bar;
176	struct proc *p;
177{
178	register int	s;
179	struct tun_softc *tp;
180	struct ifnet	*ifp;
181	struct mbuf	*m;
182
183	tp = dev->si_drv1;
184	ifp = &tp->tun_if;
185
186	tp->tun_flags &= ~TUN_OPEN;
187	tp->tun_pid = 0;
188
189	/*
190	 * junk all pending output
191	 */
192	do {
193		s = splimp();
194		IF_DEQUEUE(&ifp->if_snd, m);
195		splx(s);
196		if (m)
197			m_freem(m);
198	} while (m);
199
200	if (ifp->if_flags & IFF_UP) {
201		s = splimp();
202		if_down(ifp);
203		splx(s);
204	}
205
206	if (ifp->if_flags & IFF_RUNNING) {
207		register struct ifaddr *ifa;
208
209		s = splimp();
210		/* find internet addresses and delete routes */
211		for (ifa = ifp->if_addrhead.tqh_first; ifa;
212		    ifa = ifa->ifa_link.tqe_next)
213			if (ifa->ifa_addr->sa_family == AF_INET)
214				rtinit(ifa, (int)RTM_DELETE,
215				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
216		ifp->if_flags &= ~IFF_RUNNING;
217		splx(s);
218	}
219
220	funsetown(tp->tun_sigio);
221	selwakeup(&tp->tun_rsel);
222
223	TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
224	return (0);
225}
226
227static int
228tuninit(ifp)
229	struct ifnet *ifp;
230{
231	struct tun_softc *tp = ifp->if_softc;
232	register struct ifaddr *ifa;
233
234	TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
235
236	ifp->if_flags |= IFF_UP | IFF_RUNNING;
237	getmicrotime(&ifp->if_lastchange);
238
239	for (ifa = ifp->if_addrhead.tqh_first; ifa;
240	     ifa = ifa->ifa_link.tqe_next) {
241#ifdef INET
242		if (ifa->ifa_addr->sa_family == AF_INET) {
243		    struct sockaddr_in *si;
244
245		    si = (struct sockaddr_in *)ifa->ifa_addr;
246		    if (si && si->sin_addr.s_addr)
247			    tp->tun_flags |= TUN_IASET;
248
249		    si = (struct sockaddr_in *)ifa->ifa_dstaddr;
250		    if (si && si->sin_addr.s_addr)
251			    tp->tun_flags |= TUN_DSTADDR;
252		}
253#endif
254	}
255	return 0;
256}
257
258/*
259 * Process an ioctl request.
260 */
261int
262tunifioctl(ifp, cmd, data)
263	struct ifnet *ifp;
264	u_long	cmd;
265	caddr_t	data;
266{
267	struct ifreq *ifr = (struct ifreq *)data;
268	struct tun_softc *tp = ifp->if_softc;
269	struct ifstat *ifs;
270	int		error = 0, s;
271
272	s = splimp();
273	switch(cmd) {
274	case SIOCGIFSTATUS:
275		ifs = (struct ifstat *)data;
276		if (tp->tun_pid)
277			sprintf(ifs->ascii + strlen(ifs->ascii),
278			    "\tOpened by PID %d\n", tp->tun_pid);
279		return(0);
280	case SIOCSIFADDR:
281		tuninit(ifp);
282		TUNDEBUG("%s%d: address set\n",
283			 ifp->if_name, ifp->if_unit);
284		break;
285	case SIOCSIFDSTADDR:
286		tuninit(ifp);
287		TUNDEBUG("%s%d: destination address set\n",
288			 ifp->if_name, ifp->if_unit);
289		break;
290	case SIOCSIFMTU:
291		ifp->if_mtu = ifr->ifr_mtu;
292		TUNDEBUG("%s%d: mtu set\n",
293			 ifp->if_name, ifp->if_unit);
294		break;
295	case SIOCADDMULTI:
296	case SIOCDELMULTI:
297		break;
298
299
300	default:
301		error = EINVAL;
302	}
303	splx(s);
304	return (error);
305}
306
307/*
308 * tunoutput - queue packets from higher level ready to put out.
309 */
310int
311tunoutput(ifp, m0, dst, rt)
312	struct ifnet   *ifp;
313	struct mbuf    *m0;
314	struct sockaddr *dst;
315	struct rtentry *rt;
316{
317	struct tun_softc *tp = ifp->if_softc;
318	int		s;
319
320	TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
321
322	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
323		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
324			  ifp->if_unit, tp->tun_flags);
325		m_freem (m0);
326		return EHOSTDOWN;
327	}
328
329	/* BPF write needs to be handled specially */
330	if (dst->sa_family == AF_UNSPEC) {
331		dst->sa_family = *(mtod(m0, int *));
332		m0->m_len -= sizeof(int);
333		m0->m_pkthdr.len -= sizeof(int);
334		m0->m_data += sizeof(int);
335	}
336
337	if (ifp->if_bpf) {
338		/*
339		 * We need to prepend the address family as
340		 * a four byte field.  Cons up a dummy header
341		 * to pacify bpf.  This is safe because bpf
342		 * will only read from the mbuf (i.e., it won't
343		 * try to free it or keep a pointer to it).
344		 */
345		struct mbuf m;
346		u_int af = dst->sa_family;
347
348		m.m_next = m0;
349		m.m_len = 4;
350		m.m_data = (char *)&af;
351
352		bpf_mtap(ifp, &m);
353	}
354
355	/* prepend sockaddr? this may abort if the mbuf allocation fails */
356	if (tp->tun_flags & TUN_LMODE) {
357		/* allocate space for sockaddr */
358		M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
359
360		/* if allocation failed drop packet */
361		if (m0 == NULL){
362			s = splimp();	/* spl on queue manipulation */
363			IF_DROP(&ifp->if_snd);
364			splx(s);
365			ifp->if_oerrors++;
366			return (ENOBUFS);
367		} else {
368			bcopy(dst, m0->m_data, dst->sa_len);
369		}
370	}
371
372	switch(dst->sa_family) {
373#ifdef INET
374	case AF_INET:
375		s = splimp();
376		if (IF_QFULL(&ifp->if_snd)) {
377			IF_DROP(&ifp->if_snd);
378			m_freem(m0);
379			splx(s);
380			ifp->if_collisions++;
381			return (ENOBUFS);
382		}
383		ifp->if_obytes += m0->m_pkthdr.len;
384		IF_ENQUEUE(&ifp->if_snd, m0);
385		splx(s);
386		ifp->if_opackets++;
387		break;
388#endif
389	default:
390		m_freem(m0);
391		return EAFNOSUPPORT;
392	}
393
394	if (tp->tun_flags & TUN_RWAIT) {
395		tp->tun_flags &= ~TUN_RWAIT;
396		wakeup((caddr_t)tp);
397	}
398	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
399		pgsigio(tp->tun_sigio, SIGIO, 0);
400	selwakeup(&tp->tun_rsel);
401	return 0;
402}
403
404/*
405 * the cdevsw interface is now pretty minimal.
406 */
407static	int
408tunioctl(dev, cmd, data, flag, p)
409	dev_t		dev;
410	u_long		cmd;
411	caddr_t		data;
412	int		flag;
413	struct proc	*p;
414{
415	int		s;
416	struct tun_softc *tp = dev->si_drv1;
417 	struct tuninfo *tunp;
418
419	switch (cmd) {
420 	case TUNSIFINFO:
421 	        tunp = (struct tuninfo *)data;
422		if (tunp->mtu < IF_MINMTU)
423			return (EINVAL);
424 		tp->tun_if.if_mtu = tunp->mtu;
425 		tp->tun_if.if_type = tunp->type;
426 		tp->tun_if.if_baudrate = tunp->baudrate;
427 		break;
428 	case TUNGIFINFO:
429 		tunp = (struct tuninfo *)data;
430 		tunp->mtu = tp->tun_if.if_mtu;
431 		tunp->type = tp->tun_if.if_type;
432 		tunp->baudrate = tp->tun_if.if_baudrate;
433 		break;
434	case TUNSDEBUG:
435		tundebug = *(int *)data;
436		break;
437	case TUNGDEBUG:
438		*(int *)data = tundebug;
439		break;
440	case TUNSLMODE:
441		if (*(int *)data)
442			tp->tun_flags |= TUN_LMODE;
443		else
444			tp->tun_flags &= ~TUN_LMODE;
445		break;
446	case TUNSIFMODE:
447		/* deny this if UP */
448		if (tp->tun_if.if_flags & IFF_UP)
449			return(EBUSY);
450
451		switch (*(int *)data) {
452		case IFF_POINTOPOINT:
453			tp->tun_if.if_flags |= IFF_POINTOPOINT;
454			tp->tun_if.if_flags &= ~IFF_BROADCAST;
455			break;
456		case IFF_BROADCAST:
457			tp->tun_if.if_flags &= ~IFF_POINTOPOINT;
458			tp->tun_if.if_flags |= IFF_BROADCAST;
459			break;
460		default:
461			return(EINVAL);
462		}
463		break;
464	case FIONBIO:
465		break;
466	case FIOASYNC:
467		if (*(int *)data)
468			tp->tun_flags |= TUN_ASYNC;
469		else
470			tp->tun_flags &= ~TUN_ASYNC;
471		break;
472	case FIONREAD:
473		s = splimp();
474		if (tp->tun_if.if_snd.ifq_head) {
475			struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
476			for( *(int *)data = 0; mb != 0; mb = mb->m_next)
477				*(int *)data += mb->m_len;
478		} else
479			*(int *)data = 0;
480		splx(s);
481		break;
482	case FIOSETOWN:
483		return (fsetown(*(int *)data, &tp->tun_sigio));
484
485	case FIOGETOWN:
486		*(int *)data = fgetown(tp->tun_sigio);
487		return (0);
488
489	/* This is deprecated, FIOSETOWN should be used instead. */
490	case TIOCSPGRP:
491		return (fsetown(-(*(int *)data), &tp->tun_sigio));
492
493	/* This is deprecated, FIOGETOWN should be used instead. */
494	case TIOCGPGRP:
495		*(int *)data = -fgetown(tp->tun_sigio);
496		return (0);
497
498	default:
499		return (ENOTTY);
500	}
501	return (0);
502}
503
504/*
505 * The cdevsw read interface - reads a packet at a time, or at
506 * least as much of a packet as can be read.
507 */
508static	int
509tunread(dev, uio, flag)
510	dev_t dev;
511	struct uio *uio;
512	int flag;
513{
514	struct tun_softc *tp = dev->si_drv1;
515	struct ifnet	*ifp = &tp->tun_if;
516	struct mbuf	*m, *m0;
517	int		error=0, len, s;
518
519	TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
520	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
521		TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
522			  ifp->if_unit, tp->tun_flags);
523		return EHOSTDOWN;
524	}
525
526	tp->tun_flags &= ~TUN_RWAIT;
527
528	s = splimp();
529	do {
530		IF_DEQUEUE(&ifp->if_snd, m0);
531		if (m0 == 0) {
532			if (flag & IO_NDELAY) {
533				splx(s);
534				return EWOULDBLOCK;
535			}
536			tp->tun_flags |= TUN_RWAIT;
537			if((error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
538					"tunread", 0)) != 0) {
539				splx(s);
540				return error;
541			}
542		}
543	} while (m0 == 0);
544	splx(s);
545
546	while (m0 && uio->uio_resid > 0 && error == 0) {
547		len = min(uio->uio_resid, m0->m_len);
548		if (len == 0)
549			break;
550		error = uiomove(mtod(m0, caddr_t), len, uio);
551		MFREE(m0, m);
552		m0 = m;
553	}
554
555	if (m0) {
556		TUNDEBUG("Dropping mbuf\n");
557		m_freem(m0);
558	}
559	return error;
560}
561
562/*
563 * the cdevsw write interface - an atomic write is a packet - or else!
564 */
565static	int
566tunwrite(dev, uio, flag)
567	dev_t dev;
568	struct uio *uio;
569	int flag;
570{
571	struct tun_softc *tp = dev->si_drv1;
572	struct ifnet	*ifp = &tp->tun_if;
573	struct mbuf	*top, **mp, *m;
574	int		error=0, s, tlen, mlen;
575
576	TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
577
578	if (uio->uio_resid == 0)
579		return 0;
580
581	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
582		TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
583		    uio->uio_resid);
584		return EIO;
585	}
586	tlen = uio->uio_resid;
587
588	/* get a header mbuf */
589	MGETHDR(m, M_DONTWAIT, MT_DATA);
590	if (m == NULL)
591		return ENOBUFS;
592	mlen = MHLEN;
593
594	top = 0;
595	mp = &top;
596	while (error == 0 && uio->uio_resid > 0) {
597		m->m_len = min(mlen, uio->uio_resid);
598		error = uiomove(mtod (m, caddr_t), m->m_len, uio);
599		*mp = m;
600		mp = &m->m_next;
601		if (uio->uio_resid > 0) {
602			MGET (m, M_DONTWAIT, MT_DATA);
603			if (m == 0) {
604				error = ENOBUFS;
605				break;
606			}
607			mlen = MLEN;
608		}
609	}
610	if (error) {
611		if (top)
612			m_freem (top);
613		return error;
614	}
615
616	top->m_pkthdr.len = tlen;
617	top->m_pkthdr.rcvif = ifp;
618
619	if (ifp->if_bpf) {
620		/*
621		 * We need to prepend the address family as
622		 * a four byte field.  Cons up a dummy header
623		 * to pacify bpf.  This is safe because bpf
624		 * will only read from the mbuf (i.e., it won't
625		 * try to free it or keep a pointer to it).
626		 */
627		struct mbuf m;
628		u_int af = AF_INET;
629
630		m.m_next = top;
631		m.m_len = 4;
632		m.m_data = (char *)&af;
633
634		bpf_mtap(ifp, &m);
635	}
636
637#ifdef INET
638	s = splimp();
639	if (IF_QFULL (&ipintrq)) {
640		IF_DROP(&ipintrq);
641		splx(s);
642		ifp->if_collisions++;
643		m_freem(top);
644		return ENOBUFS;
645	}
646	IF_ENQUEUE(&ipintrq, top);
647	splx(s);
648	ifp->if_ibytes += tlen;
649	ifp->if_ipackets++;
650	schednetisr(NETISR_IP);
651#endif
652	return error;
653}
654
655/*
656 * tunpoll - the poll interface, this is only useful on reads
657 * really. The write detect always returns true, write never blocks
658 * anyway, it either accepts the packet or drops it.
659 */
660static	int
661tunpoll(dev, events, p)
662	dev_t dev;
663	int events;
664	struct proc *p;
665{
666	int		s;
667	struct tun_softc *tp = dev->si_drv1;
668	struct ifnet	*ifp = &tp->tun_if;
669	int		revents = 0;
670
671	s = splimp();
672	TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
673
674	if (events & (POLLIN | POLLRDNORM)) {
675		if (ifp->if_snd.ifq_len > 0) {
676			TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
677			    ifp->if_unit, ifp->if_snd.ifq_len);
678			revents |= events & (POLLIN | POLLRDNORM);
679		} else {
680			TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
681			    ifp->if_unit);
682			selrecord(p, &tp->tun_rsel);
683		}
684	}
685	if (events & (POLLOUT | POLLWRNORM))
686		revents |= events & (POLLOUT | POLLWRNORM);
687
688	splx(s);
689	return (revents);
690}
691