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