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