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