xref: /dragonfly/sys/net/tun/if_tun.c (revision 7d84b73d)
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: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
17  */
18 
19 #include "opt_inet.h"
20 #include "opt_inet6.h"
21 
22 #include <sys/param.h>
23 #include <sys/proc.h>
24 #include <sys/caps.h>
25 #include <sys/systm.h>
26 #include <sys/mbuf.h>
27 #include <sys/socket.h>
28 #include <sys/conf.h>
29 #include <sys/device.h>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/ttycom.h>
33 #include <sys/signalvar.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/uio.h>
38 #include <sys/vnode.h>
39 #include <sys/malloc.h>
40 #include <sys/mplock2.h>
41 #include <sys/devfs.h>
42 #include <sys/queue.h>
43 
44 #include <net/bpf.h>
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/if_clone.h>
48 #include <net/ifq_var.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #endif
55 
56 #include "if_tunvar.h"
57 #include "if_tun.h"
58 
59 #define TUN		"tun"
60 #define TUNDEBUG	if (tundebug) if_printf
61 
62 /* module */
63 static int		tunmodevent(module_t, int, void *);
64 
65 /* device */
66 static struct tun_softc *tuncreate(cdev_t, int);
67 static void		tundestroy(struct tun_softc *sc);
68 
69 /* clone */
70 static int		tun_clone_create(struct if_clone *, int,
71 					 caddr_t, caddr_t);
72 static int		tun_clone_destroy(struct ifnet *);
73 
74 /* network interface */
75 static int		tunifinit(struct ifnet *);
76 static void		tunifstart(struct ifnet *, struct ifaltq_subque *);
77 static int		tunifoutput(struct ifnet *, struct mbuf *,
78 				    struct sockaddr *, struct rtentry *rt);
79 static int		tunifioctl(struct ifnet *, u_long,
80 				   caddr_t, struct ucred *);
81 
82 /* character device */
83 static d_open_t		tunopen;
84 static d_close_t	tunclose;
85 static d_read_t		tunread;
86 static d_write_t	tunwrite;
87 static d_ioctl_t	tunioctl;
88 static d_kqfilter_t	tunkqfilter;
89 static d_clone_t	tunclone;
90 
91 static struct dev_ops	tun_ops = {
92 	{ TUN, 0, 0 },
93 	.d_open =	tunopen,
94 	.d_close =	tunclose,
95 	.d_read =	tunread,
96 	.d_write =	tunwrite,
97 	.d_ioctl =	tunioctl,
98 	.d_kqfilter =	tunkqfilter
99 };
100 
101 /* kqueue support */
102 static void		tun_filter_detach(struct knote *);
103 static int		tun_filter_read(struct knote *, long);
104 static int		tun_filter_write(struct knote *, long);
105 
106 static struct filterops tun_read_filtops = {
107 	FILTEROP_ISFD,
108 	NULL,
109 	tun_filter_detach,
110 	tun_filter_read
111 };
112 static struct filterops tun_write_filtops = {
113 	FILTEROP_ISFD,
114 	NULL,
115 	tun_filter_detach,
116 	tun_filter_write
117 };
118 
119 static int		tundebug = 0;	/* debug flag */
120 static int		tunrefcnt = 0;	/* module reference counter */
121 
122 static MALLOC_DEFINE(M_TUN, TUN, "Tunnel Interface");
123 
124 static DEVFS_DEFINE_CLONE_BITMAP(tun);
125 
126 struct if_clone tun_cloner = IF_CLONE_INITIALIZER(
127 	TUN, tun_clone_create, tun_clone_destroy, 0, IF_MAXUNIT);
128 
129 static SLIST_HEAD(,tun_softc) tun_listhead =
130 	SLIST_HEAD_INITIALIZER(&tun_listhead);
131 
132 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0,
133 	   "Enable debug output");
134 SYSCTL_DECL(_net_link);
135 SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
136 	    "IP tunnel software network interface");
137 SYSCTL_INT(_net_link_tun, OID_AUTO, debug, CTLFLAG_RW, &tundebug, 0,
138 	   "Enable debug output");
139 SYSCTL_INT(_net_link_tun, OID_AUTO, refcnt, CTLFLAG_RD, &tunrefcnt, 0,
140 	   "Number of opened devices");
141 
142 DEV_MODULE(if_tun, tunmodevent, NULL);
143 
144 /*
145  * tunmodevent - module event handler
146  */
147 static int
148 tunmodevent(module_t mod, int type, void *data)
149 {
150 	static cdev_t dev = NULL;
151 	struct tun_softc *sc, *sc_tmp;
152 
153 	switch (type) {
154 	case MOD_LOAD:
155 		dev = make_autoclone_dev(&tun_ops, &DEVFS_CLONE_BITMAP(tun),
156 					 tunclone, UID_UUCP, GID_DIALER,
157 					 0600, TUN);
158 
159 		SLIST_INIT(&tun_listhead);
160 		if_clone_attach(&tun_cloner);
161 		break;
162 
163 	case MOD_UNLOAD:
164 		if (tunrefcnt > 0)
165 			return (EBUSY);
166 
167 		if_clone_detach(&tun_cloner);
168 
169 		SLIST_FOREACH_MUTABLE(sc, &tun_listhead, tun_link, sc_tmp)
170 			tundestroy(sc);
171 
172 		dev_ops_remove_all(&tun_ops);
173 		destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(tun));
174 		break;
175 
176 	default:
177 		return (EOPNOTSUPP);
178 	}
179 
180 	return (0);
181 }
182 
183 static int
184 tunclone(struct dev_clone_args *ap)
185 {
186 	char ifname[IFNAMSIZ];
187 	int unit;
188 
189 	unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun), 0);
190 	ksnprintf(ifname, IFNAMSIZ, "%s%d", TUN, unit);
191 	ap->a_dev = make_only_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
192 				  0600, "%s", ifname);
193 
194 	/*
195 	 * Use the if_clone framework to create cloned device/interface,
196 	 * so the two clone methods (autoclone device /dev/tun; ifconfig
197 	 * clone) are consistent and can be mix used.
198 	 *
199 	 * Need to pass the cdev_t because the device created by
200 	 * 'make_only_dev()' doesn't appear in '/dev' yet so that it can't
201 	 * be found by 'devfs_find_device_by_name()' in 'tun_clone_create()'.
202 	 */
203 	return (if_clone_create(ifname, IFNAMSIZ, NULL, (caddr_t)ap->a_dev));
204 }
205 
206 static struct tun_softc *
207 tuncreate(cdev_t dev, int flags)
208 {
209 	struct tun_softc *sc;
210 	struct ifnet *ifp;
211 	int unit = minor(dev);
212 
213 	sc = kmalloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
214 	dev->si_drv1 = sc;
215 	sc->tun_dev = dev;
216 	sc->tun_flags = TUN_INITED;
217 	sc->tun_flags |= flags;
218 
219 	reference_dev(dev);  /* device association */
220 
221 	ifp = sc->tun_ifp = if_alloc(IFT_PPP);
222 	if (ifp == NULL) {
223 		kprintf("%s: failed to if_alloc() interface for %s%d",
224 			__func__, TUN, unit);
225 		return (NULL);
226 	}
227 
228 	if_initname(ifp, TUN, unit);
229 	ifp->if_mtu = TUNMTU;
230 	ifp->if_ioctl = tunifioctl;
231 	ifp->if_output = tunifoutput;
232 	ifp->if_start = tunifstart;
233 	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
234 	ifp->if_softc = sc;
235 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
236 	ifq_set_ready(&ifp->if_snd);
237 
238 	if_attach(ifp, NULL);
239 	bpfattach(ifp, DLT_NULL, sizeof(uint32_t));
240 
241 	SLIST_INSERT_HEAD(&tun_listhead, sc, tun_link);
242 	TUNDEBUG(ifp, "created, minor = %#x, flags = 0x%x\n",
243 		 unit, sc->tun_flags);
244 	return (sc);
245 }
246 
247 static void
248 tundestroy(struct tun_softc *sc)
249 {
250 	cdev_t dev = sc->tun_dev;
251 	struct ifnet *ifp = sc->tun_ifp;
252 	int unit = minor(dev);
253 
254 	TUNDEBUG(ifp, "destroyed, minor = %#x. Module refcnt = %d\n",
255 		 unit, tunrefcnt);
256 
257 	bpfdetach(ifp);
258 	if_detach(ifp);
259 	if_free(ifp);
260 
261 	sc->tun_dev = NULL;
262 	dev->si_drv1 = NULL;
263 	release_dev(dev);  /* device disassociation */
264 
265 	/* Also destroy the cloned device */
266 	destroy_dev(dev);
267 	devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun), unit);
268 
269 	SLIST_REMOVE(&tun_listhead, sc, tun_softc, tun_link);
270 	kfree(sc, M_TUN);
271 }
272 
273 /*
274  * tunnel open - must be superuser & the device must be configured in
275  */
276 static int
277 tunopen(struct dev_open_args *ap)
278 {
279 	cdev_t dev = ap->a_head.a_dev;
280 	struct tun_softc *sc;
281 	int error;
282 
283 	if ((error = caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT)) != 0)
284 		return (error);
285 
286 	sc = dev->si_drv1;
287 	if (sc == NULL && (sc = tuncreate(dev, 0)) == NULL)
288 		return (ENOMEM);
289 	if (sc->tun_flags & TUN_OPEN)
290 		return (EBUSY);
291 
292 	sc->tun_pid = curproc->p_pid;
293 	sc->tun_flags |= TUN_OPEN;
294 	tunrefcnt++;
295 
296 	TUNDEBUG(sc->tun_ifp, "opened, minor = %#x. Module refcnt = %d\n",
297 		 minor(dev), tunrefcnt);
298 	return (0);
299 }
300 
301 /*
302  * close the device - mark interface down & delete routing info
303  */
304 static int
305 tunclose(struct dev_close_args *ap)
306 {
307 	cdev_t dev = ap->a_head.a_dev;
308 	struct tun_softc *sc = dev->si_drv1;
309 	struct ifnet *ifp;
310 	int unit = minor(dev);
311 	char ifname[IFNAMSIZ];
312 
313 	KASSERT(sc != NULL,
314 		("try closing the already destroyed %s%d", TUN, unit));
315 	ifp = sc->tun_ifp;
316 
317 	sc->tun_flags &= ~TUN_OPEN;
318 	sc->tun_pid = 0;
319 
320 	/* Junk all pending output. */
321 	ifq_purge_all(&ifp->if_snd);
322 
323 	if (ifp->if_flags & IFF_UP)
324 		if_down(ifp);
325 	ifp->if_flags &= ~IFF_RUNNING;
326 
327 	if ((sc->tun_flags & TUN_MANUALMAKE) == 0) {
328 		if_purgeaddrs_nolink(ifp);
329 
330 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
331 
332 		/* Announce the departure of the interface. */
333 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
334 	}
335 
336 	funsetown(&sc->tun_sigio);
337 	KNOTE(&sc->tun_rkq.ki_note, 0);
338 
339 	tunrefcnt--;
340 	if (tunrefcnt < 0) {
341 		tunrefcnt = 0;
342 		if_printf(ifp, ". Module refcnt = %d is out of sync! "
343 			  "Force refcnt to be 0.\n", tunrefcnt);
344 	}
345 
346 	TUNDEBUG(ifp, "closed, minor = %#x. Module refcnt = %d\n",
347 		 unit, tunrefcnt);
348 
349 	/* Only auto-destroy if the interface was not manually created. */
350 	if ((sc->tun_flags & TUN_MANUALMAKE) == 0) {
351 		ksnprintf(ifname, IFNAMSIZ, "%s%d", TUN, unit);
352 		if_clone_destroy(ifname);
353 	}
354 
355 	return (0);
356 }
357 
358 
359 /*
360  * Interface clone support
361  *
362  * Create and destroy tun device/interface via ifconfig(8).
363  */
364 
365 static struct tun_softc *
366 tunfind(int unit)
367 {
368 	struct tun_softc *sc;
369 
370 	SLIST_FOREACH(sc, &tun_listhead, tun_link) {
371 		if (minor(sc->tun_dev) == unit)
372 			return (sc);
373 	}
374 	return (NULL);
375 }
376 
377 static int
378 tun_clone_create(struct if_clone *ifc __unused, int unit,
379 		 caddr_t params __unused, caddr_t data)
380 {
381 	struct tun_softc *sc;
382 	cdev_t dev = (cdev_t)data;
383 	int flags;
384 
385 	if (tunfind(unit) != NULL)
386 		return (EEXIST);
387 
388 	if (dev == NULL) {
389 		if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tun), unit)) {
390 			devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun), unit);
391 			dev = make_dev(&tun_ops, unit, UID_UUCP, GID_DIALER,
392 				       0600, "%s%d", TUN, unit);
393 		} else {
394 			dev = devfs_find_device_by_name("%s%d", TUN, unit);
395 			if (dev == NULL)
396 				return (ENOENT);
397 		}
398 		flags = TUN_MANUALMAKE;
399 	} else {
400 		flags = 0;
401 	}
402 
403 	if ((sc = tuncreate(dev, flags)) == NULL)
404 		return (ENOMEM);
405 
406 	sc->tun_flags |= TUN_CLONE;
407 
408 	TUNDEBUG(sc->tun_ifp, "clone created, minor = %#x, flags = 0x%x\n",
409 		 minor(sc->tun_dev), sc->tun_flags);
410 
411 	return (0);
412 }
413 
414 static int
415 tun_clone_destroy(struct ifnet *ifp)
416 {
417 	struct tun_softc *sc = ifp->if_softc;
418 
419 	if (sc->tun_flags & TUN_OPEN)
420 		return (EBUSY);
421 	if ((sc->tun_flags & TUN_CLONE) == 0)
422 		return (EINVAL);
423 
424 	TUNDEBUG(ifp, "clone destroyed, minor = %#x, flags = 0x%x\n",
425 		 minor(sc->tun_dev), sc->tun_flags);
426 	tundestroy(sc);
427 
428 	return (0);
429 }
430 
431 
432 /*
433  * Network interface functions
434  */
435 
436 static int
437 tunifinit(struct ifnet *ifp)
438 {
439 #ifdef INET
440 	struct tun_softc *sc = ifp->if_softc;
441 #endif
442 	struct ifaddr_container *ifac;
443 	int error = 0;
444 
445 	TUNDEBUG(ifp, "initialize\n");
446 
447 	ifp->if_flags |= IFF_UP | IFF_RUNNING;
448 	getmicrotime(&ifp->if_lastchange);
449 
450 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
451 		struct ifaddr *ifa = ifac->ifa;
452 
453 		if (ifa->ifa_addr == NULL) {
454 			error = EFAULT;
455 			/* XXX: Should maybe return straight off? */
456 		} else {
457 #ifdef INET
458 			if (ifa->ifa_addr->sa_family == AF_INET) {
459 				struct sockaddr_in *si;
460 
461 				si = (struct sockaddr_in *)ifa->ifa_addr;
462 				if (si->sin_addr.s_addr)
463 					sc->tun_flags |= TUN_IASET;
464 			}
465 #endif
466 		}
467 	}
468 	return (error);
469 }
470 
471 /*
472  * Process an ioctl request.
473  *
474  * MPSAFE
475  */
476 static int
477 tunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
478 {
479 	struct ifreq *ifr = (struct ifreq *)data;
480 	struct tun_softc *sc = ifp->if_softc;
481 	struct ifstat *ifs;
482 	int error = 0;
483 
484 	switch (cmd) {
485 	case SIOCGIFSTATUS:
486 		ifs = (struct ifstat *)data;
487 		if (sc->tun_pid)
488 			ksprintf(ifs->ascii + strlen(ifs->ascii),
489 			    "\tOpened by PID %d\n", sc->tun_pid);
490 		break;
491 	case SIOCSIFADDR:
492 		error = tunifinit(ifp);
493 		TUNDEBUG(ifp, "address set, error=%d\n", error);
494 		break;
495 	case SIOCSIFDSTADDR:
496 		error = tunifinit(ifp);
497 		TUNDEBUG(ifp, "destination address set, error=%d\n", error);
498 		break;
499 	case SIOCSIFMTU:
500 		ifp->if_mtu = ifr->ifr_mtu;
501 		TUNDEBUG(ifp, "mtu set\n");
502 		break;
503 	case SIOCSIFFLAGS:
504 	case SIOCADDMULTI:
505 	case SIOCDELMULTI:
506 		break;
507 	default:
508 		error = EINVAL;
509 	}
510 	return (error);
511 }
512 
513 /*
514  * Start packet transmission on the interface.
515  * when the interface queue is rate-limited by ALTQ,
516  * if_start is needed to drain packets from the queue in order
517  * to notify readers when outgoing packets become ready.
518  */
519 static void
520 tunifstart(struct ifnet *ifp, struct ifaltq_subque *ifsq)
521 {
522 	struct tun_softc *sc = ifp->if_softc;
523 	struct mbuf *m;
524 
525 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
526 
527 	if (!ifq_is_enabled(&ifp->if_snd))
528 		return;
529 
530 	m = ifsq_poll(ifsq);
531 	if (m != NULL) {
532 		if (sc->tun_flags & TUN_RWAIT) {
533 			sc->tun_flags &= ~TUN_RWAIT;
534 			wakeup((caddr_t)sc);
535 		}
536 		if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
537 			pgsigio(sc->tun_sigio, SIGIO, 0);
538 		ifsq_deserialize_hw(ifsq);
539 		KNOTE(&sc->tun_rkq.ki_note, 0);
540 		ifsq_serialize_hw(ifsq);
541 	}
542 }
543 
544 /*
545  * tunifoutput - queue packets from higher level ready to put out.
546  *
547  * MPSAFE
548  */
549 static int
550 tunifoutput_serialized(struct ifnet *ifp, struct mbuf *m0,
551 		       struct sockaddr *dst, struct rtentry *rt)
552 {
553 	struct tun_softc *sc = ifp->if_softc;
554 	int error;
555 	struct altq_pktattr pktattr;
556 
557 	TUNDEBUG(ifp, "output\n");
558 
559 	if ((sc->tun_flags & TUN_READY) != TUN_READY) {
560 		TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
561 		m_freem (m0);
562 		return (EHOSTDOWN);
563 	}
564 
565 	/*
566 	 * if the queueing discipline needs packet classification,
567 	 * do it before prepending link headers.
568 	 */
569 	ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
570 
571 	/* BPF write needs to be handled specially */
572 	if (dst->sa_family == AF_UNSPEC) {
573 		dst->sa_family = *(mtod(m0, int *));
574 		m0->m_len -= sizeof(int);
575 		m0->m_pkthdr.len -= sizeof(int);
576 		m0->m_data += sizeof(int);
577 	}
578 
579 	if (ifp->if_bpf) {
580 		bpf_gettoken();
581 		if (ifp->if_bpf) {
582 			/*
583 			 * We need to prepend the address family as
584 			 * a four byte field.
585 			 */
586 			uint32_t af = dst->sa_family;
587 
588 			bpf_ptap(ifp->if_bpf, m0, &af, sizeof(af));
589 		}
590 		bpf_reltoken();
591 	}
592 
593 	/* prepend sockaddr? this may abort if the mbuf allocation fails */
594 	if (sc->tun_flags & TUN_LMODE) {
595 		/* allocate space for sockaddr */
596 		M_PREPEND(m0, dst->sa_len, M_NOWAIT);
597 
598 		/* if allocation failed drop packet */
599 		if (m0 == NULL){
600 			IFNET_STAT_INC(ifp, oerrors, 1);
601 			return (ENOBUFS);
602 		} else {
603 			bcopy(dst, m0->m_data, dst->sa_len);
604 		}
605 	}
606 
607 	if (sc->tun_flags & TUN_IFHEAD) {
608 		/* Prepend the address family */
609 		M_PREPEND(m0, 4, M_NOWAIT);
610 
611 		/* if allocation failed drop packet */
612 		if (m0 == NULL){
613 			IFNET_STAT_INC(ifp, oerrors, 1);
614 			return (ENOBUFS);
615 		} else {
616 			*(u_int32_t *)m0->m_data = htonl(dst->sa_family);
617 		}
618 	} else {
619 #ifdef INET
620 		if (dst->sa_family != AF_INET)
621 #endif
622 		{
623 			m_freem(m0);
624 			return (EAFNOSUPPORT);
625 		}
626 	}
627 
628 	error = ifq_handoff(ifp, m0, &pktattr);
629 	if (error) {
630 		IFNET_STAT_INC(ifp, collisions, 1);
631 	} else {
632 		IFNET_STAT_INC(ifp, opackets, 1);
633 		if (sc->tun_flags & TUN_RWAIT) {
634 			sc->tun_flags &= ~TUN_RWAIT;
635 			wakeup((caddr_t)sc);
636 		}
637 		get_mplock();
638 		if (sc->tun_flags & TUN_ASYNC && sc->tun_sigio)
639 			pgsigio(sc->tun_sigio, SIGIO, 0);
640 		rel_mplock();
641 		ifnet_deserialize_all(ifp);
642 		KNOTE(&sc->tun_rkq.ki_note, 0);
643 		ifnet_serialize_all(ifp);
644 	}
645 	return (error);
646 }
647 
648 static int
649 tunifoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
650 	    struct rtentry *rt)
651 {
652 	int error;
653 
654 	ifnet_serialize_all(ifp);
655 	error = tunifoutput_serialized(ifp, m0, dst, rt);
656 	ifnet_deserialize_all(ifp);
657 
658 	return (error);
659 }
660 
661 
662 /*
663  * the ops interface is now pretty minimal.
664  */
665 static int
666 tunioctl(struct dev_ioctl_args *ap)
667 {
668 	cdev_t dev = ap->a_head.a_dev;
669 	caddr_t data = ap->a_data;
670 	struct tun_softc *sc = dev->si_drv1;
671 	struct ifnet *ifp = sc->tun_ifp;
672 	struct ifreq *ifr;
673 	struct tuninfo *tunp;
674 	int error = 0;
675 
676 	switch (ap->a_cmd) {
677 	case TUNSIFINFO:
678 		tunp = (struct tuninfo *)data;
679 		if (ifp->if_type != tunp->type)
680 			return (EPROTOTYPE);
681 		if (tunp->mtu < IF_MINMTU)
682 			return (EINVAL);
683 		ifp->if_mtu = tunp->mtu;
684 		ifp->if_baudrate = tunp->baudrate;
685 		break;
686 
687 	case TUNGIFINFO:
688 		tunp = (struct tuninfo *)data;
689 		tunp->mtu = ifp->if_mtu;
690 		tunp->type = ifp->if_type;
691 		tunp->baudrate = ifp->if_baudrate;
692 		break;
693 
694 	case TUNGIFNAME:
695 		ifr = (struct ifreq *)data;
696 		strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
697 		break;
698 
699 	case TUNSDEBUG:
700 		tundebug = *(int *)data;
701 		break;
702 
703 	case TUNGDEBUG:
704 		*(int *)data = tundebug;
705 		break;
706 
707 	case TUNSLMODE:
708 		if (*(int *)data) {
709 			sc->tun_flags |= TUN_LMODE;
710 			sc->tun_flags &= ~TUN_IFHEAD;
711 		} else {
712 			sc->tun_flags &= ~TUN_LMODE;
713 		}
714 		break;
715 
716 	case TUNSIFHEAD:
717 		if (*(int *)data) {
718 			sc->tun_flags |= TUN_IFHEAD;
719 			sc->tun_flags &= ~TUN_LMODE;
720 		} else {
721 			sc->tun_flags &= ~TUN_IFHEAD;
722 		}
723 		break;
724 
725 	case TUNGIFHEAD:
726 		*(int *)data = (sc->tun_flags & TUN_IFHEAD) ? 1 : 0;
727 		break;
728 
729 	case TUNSIFMODE:
730 		/* deny this if UP */
731 		if (ifp->if_flags & IFF_UP)
732 			return (EBUSY);
733 
734 		switch (*(int *)data & ~IFF_MULTICAST) {
735 		case IFF_POINTOPOINT:
736 		case IFF_BROADCAST:
737 			ifp->if_flags &= ~(IFF_BROADCAST | IFF_POINTOPOINT);
738 			ifp->if_flags |= *(int *)data;
739 			break;
740 		default:
741 			return (EINVAL);
742 		}
743 		break;
744 
745 	case TUNSIFPID:
746 		sc->tun_pid = curproc->p_pid;
747 		break;
748 
749 	case FIOASYNC:
750 		if (*(int *)data)
751 			sc->tun_flags |= TUN_ASYNC;
752 		else
753 			sc->tun_flags &= ~TUN_ASYNC;
754 		break;
755 
756 	case FIONREAD:
757 		*(int *)data = ifsq_poll_pktlen(
758 		    ifq_get_subq_default(&ifp->if_snd));
759 		break;
760 
761 	case FIOSETOWN:
762 		error = fsetown(*(int *)data, &sc->tun_sigio);
763 		break;
764 
765 	case FIOGETOWN:
766 		*(int *)data = fgetown(&sc->tun_sigio);
767 		break;
768 
769 	/* This is deprecated, FIOSETOWN should be used instead. */
770 	case TIOCSPGRP:
771 		error = fsetown(-(*(int *)data), &sc->tun_sigio);
772 		break;
773 
774 	/* This is deprecated, FIOGETOWN should be used instead. */
775 	case TIOCGPGRP:
776 		*(int *)data = -fgetown(&sc->tun_sigio);
777 		break;
778 
779 	default:
780 		error = ENOTTY;
781 		break;
782 	}
783 
784 	return (error);
785 }
786 
787 /*
788  * The ops read interface - reads a packet at a time, or at
789  * least as much of a packet as can be read.
790  */
791 static	int
792 tunread(struct dev_read_args *ap)
793 {
794 	cdev_t dev = ap->a_head.a_dev;
795 	struct uio *uio = ap->a_uio;
796 	struct tun_softc *sc = dev->si_drv1;
797 	struct ifnet *ifp = sc->tun_ifp;
798 	struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
799 	struct mbuf *m0;
800 	int error=0, len;
801 
802 	TUNDEBUG(ifp, "read\n");
803 	if ((sc->tun_flags & TUN_READY) != TUN_READY) {
804 		TUNDEBUG(ifp, "not ready, flags = 0x%x\n", sc->tun_flags);
805 		return (EHOSTDOWN);
806 	}
807 
808 	sc->tun_flags &= ~TUN_RWAIT;
809 
810 	ifnet_serialize_all(ifp);
811 
812 	while ((m0 = ifsq_dequeue(ifsq)) == NULL) {
813 		if (ap->a_ioflag & IO_NDELAY) {
814 			ifnet_deserialize_all(ifp);
815 			return (EWOULDBLOCK);
816 		}
817 		sc->tun_flags |= TUN_RWAIT;
818 		ifnet_deserialize_all(ifp);
819 		if ((error = tsleep(sc, PCATCH, "tunread", 0)) != 0)
820 			return (error);
821 		ifnet_serialize_all(ifp);
822 	}
823 
824 	ifnet_deserialize_all(ifp);
825 
826 	while (m0 && uio->uio_resid > 0 && error == 0) {
827 		len = (int)szmin(uio->uio_resid, m0->m_len);
828 		if (len != 0)
829 			error = uiomove(mtod(m0, caddr_t), (size_t)len, uio);
830 		m0 = m_free(m0);
831 	}
832 
833 	if (m0) {
834 		TUNDEBUG(ifp, "dropping mbuf\n");
835 		m_freem(m0);
836 	}
837 	return (error);
838 }
839 
840 /*
841  * the ops write interface - an atomic write is a packet - or else!
842  */
843 static	int
844 tunwrite(struct dev_write_args *ap)
845 {
846 	cdev_t dev = ap->a_head.a_dev;
847 	struct uio *uio = ap->a_uio;
848 	struct tun_softc *sc = dev->si_drv1;
849 	struct ifnet *ifp = sc->tun_ifp;
850 	struct mbuf *top, **mp, *m;
851 	size_t tlen;
852 	uint32_t family, mru;
853 	int error = 0;
854 	int isr;
855 
856 	TUNDEBUG(ifp, "tunwrite\n");
857 
858 	if (uio->uio_resid == 0)
859 		return (0);
860 
861 	mru = TUNMRU;
862 	if (sc->tun_flags & TUN_IFHEAD)
863 		mru += sizeof(family);
864 	if (uio->uio_resid > mru) {
865 		TUNDEBUG(ifp, "len = %zd!\n", uio->uio_resid);
866 		return (EIO);
867 	}
868 
869 	/* get a header mbuf */
870 	MGETHDR(m, M_WAITOK, MT_DATA);
871 
872 	tlen = uio->uio_resid;
873 	top = NULL;
874 	mp = &top;
875 	while (error == 0 && uio->uio_resid > 0) {
876 		m->m_len = (int)szmin(MHLEN, uio->uio_resid);
877 		error = uiomove(mtod(m, caddr_t), (size_t)m->m_len, uio);
878 		*mp = m;
879 		mp = &m->m_next;
880 		if (uio->uio_resid > 0)
881 			MGET(m, M_WAITOK, MT_DATA);
882 	}
883 	if (error) {
884 		if (top)
885 			m_freem(top);
886 		IFNET_STAT_INC(ifp, ierrors, 1);
887 		return (error);
888 	}
889 
890 	top->m_pkthdr.len = (int)tlen;
891 	top->m_pkthdr.rcvif = ifp;
892 
893 	if (ifp->if_bpf) {
894 		bpf_gettoken();
895 
896 		if (ifp->if_bpf) {
897 			if (sc->tun_flags & TUN_IFHEAD) {
898 				/*
899 				 * Conveniently, we already have a 4-byte
900 				 * address family prepended to our packet !
901 				 * Inconveniently, it's in the wrong byte
902 				 * order !
903 				 */
904 				if ((top = m_pullup(top, sizeof(family)))
905 				    == NULL) {
906 					bpf_reltoken();
907 					return (ENOBUFS);
908 				}
909 				*mtod(top, u_int32_t *) =
910 				    ntohl(*mtod(top, u_int32_t *));
911 				bpf_mtap(ifp->if_bpf, top);
912 				*mtod(top, u_int32_t *) =
913 				    htonl(*mtod(top, u_int32_t *));
914 			} else {
915 				/*
916 				 * We need to prepend the address family as
917 				 * a four byte field.
918 				 */
919 				static const uint32_t af = AF_INET;
920 
921 				bpf_ptap(ifp->if_bpf, top, &af, sizeof(af));
922 			}
923 		}
924 
925 		bpf_reltoken();
926 	}
927 
928 	if (sc->tun_flags & TUN_IFHEAD) {
929 		if (top->m_len < sizeof(family) &&
930 		    (top = m_pullup(top, sizeof(family))) == NULL)
931 				return (ENOBUFS);
932 		family = ntohl(*mtod(top, u_int32_t *));
933 		m_adj(top, sizeof(family));
934 	} else {
935 		family = AF_INET;
936 	}
937 
938 	IFNET_STAT_INC(ifp, ibytes, top->m_pkthdr.len);
939 	IFNET_STAT_INC(ifp, ipackets, 1);
940 
941 	switch (family) {
942 #ifdef INET
943 	case AF_INET:
944 		isr = NETISR_IP;
945 		break;
946 #endif
947 #ifdef INET6
948 	case AF_INET6:
949 		isr = NETISR_IPV6;
950 		break;
951 #endif
952 	default:
953 		m_freem(m);
954 		return (EAFNOSUPPORT);
955 	}
956 
957 	netisr_queue(isr, top);
958 	return (0);
959 }
960 
961 
962 /*
963  * tunkqfilter - support for the kevent() system call.
964  */
965 static int
966 tunkqfilter(struct dev_kqfilter_args *ap)
967 {
968 	cdev_t dev = ap->a_head.a_dev;
969 	struct tun_softc *sc = dev->si_drv1;
970 	struct ifnet *ifp = sc->tun_ifp;
971 	struct knote *kn = ap->a_kn;
972 	struct klist *klist;
973 
974 	ap->a_result = 0;
975 	ifnet_serialize_all(ifp);
976 
977 	switch (kn->kn_filter) {
978 	case EVFILT_READ:
979 		kn->kn_fop = &tun_read_filtops;
980 		kn->kn_hook = (caddr_t)sc;
981 		break;
982 	case EVFILT_WRITE:
983 		kn->kn_fop = &tun_write_filtops;
984 		kn->kn_hook = (caddr_t)sc;
985 		break;
986 	default:
987 		ifnet_deserialize_all(ifp);
988 		ap->a_result = EOPNOTSUPP;
989 		return (0);
990 	}
991 
992 	klist = &sc->tun_rkq.ki_note;
993 	knote_insert(klist, kn);
994 	ifnet_deserialize_all(ifp);
995 
996 	return (0);
997 }
998 
999 static void
1000 tun_filter_detach(struct knote *kn)
1001 {
1002 	struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1003 	struct klist *klist = &sc->tun_rkq.ki_note;
1004 
1005 	knote_remove(klist, kn);
1006 }
1007 
1008 static int
1009 tun_filter_write(struct knote *kn, long hint)
1010 {
1011 	/* Always ready for a write */
1012 	return (1);
1013 }
1014 
1015 static int
1016 tun_filter_read(struct knote *kn, long hint)
1017 {
1018 	struct tun_softc *sc = (struct tun_softc *)kn->kn_hook;
1019 	struct ifnet *ifp = sc->tun_ifp;
1020 	int ready = 0;
1021 
1022 	ifnet_serialize_all(ifp);
1023 	if (!ifsq_is_empty(ifq_get_subq_default(&ifp->if_snd)))
1024 		ready = 1;
1025 	ifnet_deserialize_all(ifp);
1026 
1027 	return (ready);
1028 }
1029