if_tun.c revision 256008
112115Sdyson/*	$NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $	*/
212115Sdyson
312115Sdyson/*-
412115Sdyson * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
512115Sdyson * Nottingham University 1987.
612115Sdyson *
712115Sdyson * This source may be freely distributed, however I would be interested
812115Sdyson * in any changes that are made.
912115Sdyson *
1012115Sdyson * This driver takes packets off the IP i/f and hands them up to a
1112115Sdyson * user process to have its wicked way with. This driver has it's
1212115Sdyson * roots in a similar driver written by Phil Cockcroft (formerly) at
1312115Sdyson * UCL. This driver is based much more on read/write/poll mode of
1412115Sdyson * operation though.
1512115Sdyson *
1612115Sdyson * $FreeBSD: head/sys/net/if_tun.c 256008 2013-10-02 20:44:36Z glebius $
1712115Sdyson */
1812115Sdyson
1912115Sdyson#include "opt_atalk.h"
2012115Sdyson#include "opt_inet.h"
2112115Sdyson#include "opt_inet6.h"
2212115Sdyson#include "opt_ipx.h"
2312115Sdyson
2412115Sdyson#include <sys/param.h>
2512115Sdyson#include <sys/priv.h>
2612115Sdyson#include <sys/proc.h>
2712115Sdyson#include <sys/systm.h>
2812115Sdyson#include <sys/jail.h>
2912115Sdyson#include <sys/mbuf.h>
3012115Sdyson#include <sys/module.h>
3112115Sdyson#include <sys/socket.h>
3212115Sdyson#include <sys/fcntl.h>
3312115Sdyson#include <sys/filio.h>
3412115Sdyson#include <sys/sockio.h>
3512115Sdyson#include <sys/ttycom.h>
3612115Sdyson#include <sys/poll.h>
3712115Sdyson#include <sys/selinfo.h>
3812115Sdyson#include <sys/signalvar.h>
3912115Sdyson#include <sys/filedesc.h>
4012115Sdyson#include <sys/kernel.h>
4112115Sdyson#include <sys/sysctl.h>
4213260Swollman#include <sys/conf.h>
4312115Sdyson#include <sys/uio.h>
4412115Sdyson#include <sys/malloc.h>
4512115Sdyson#include <sys/random.h>
4612115Sdyson
4712115Sdyson#include <net/if.h>
4812115Sdyson#include <net/if_clone.h>
4912115Sdyson#include <net/if_types.h>
5012115Sdyson#include <net/netisr.h>
5112115Sdyson#include <net/route.h>
5229906Skato#include <net/vnet.h>
5324131Sbde#ifdef INET
5412115Sdyson#include <netinet/in.h>
5512115Sdyson#endif
5612115Sdyson#include <net/bpf.h>
5712115Sdyson#include <net/if_tun.h>
5812115Sdyson
5912115Sdyson#include <sys/queue.h>
6012115Sdyson#include <sys/condvar.h>
6112115Sdyson
6212115Sdyson#include <security/mac/mac_framework.h>
6312115Sdyson
6412115Sdyson/*
6512115Sdyson * tun_list is protected by global tunmtx.  Other mutable fields are
6612115Sdyson * protected by tun->tun_mtx, or by their owning subsystem.  tun_dev is
6712115Sdyson * static for the duration of a tunnel interface.
6812115Sdyson */
6912115Sdysonstruct tun_softc {
7028270Swollman	TAILQ_ENTRY(tun_softc)	tun_list;
7112911Sphk	struct cdev *tun_dev;
7212911Sphk	u_short	tun_flags;		/* misc flags */
7312911Sphk#define	TUN_OPEN	0x0001
7412911Sphk#define	TUN_INITED	0x0002
7512911Sphk#define	TUN_RCOLL	0x0004
7612911Sphk#define	TUN_IASET	0x0008
7712911Sphk#define	TUN_DSTADDR	0x0010
7812911Sphk#define	TUN_LMODE	0x0020
7912911Sphk#define	TUN_RWAIT	0x0040
8012911Sphk#define	TUN_ASYNC	0x0080
8112911Sphk#define	TUN_IFHEAD	0x0100
8212911Sphk
8312911Sphk#define TUN_READY       (TUN_OPEN | TUN_INITED)
8412115Sdyson
8531315Sbde	/*
8630280Sphk	 * XXXRW: tun_pid is used to exclusively lock /dev/tun.  Is this
8712911Sphk	 * actually needed?  Can we just return EBUSY if already open?
8812115Sdyson	 * Problem is that this involved inherent races when a tun device
8912115Sdyson	 * is handed off from one process to another, as opposed to just
9012115Sdyson	 * being slightly stale informationally.
9112115Sdyson	 */
9212115Sdyson	pid_t	tun_pid;		/* owning pid */
9312115Sdyson	struct	ifnet *tun_ifp;		/* the interface */
9412115Sdyson	struct  sigio *tun_sigio;	/* information for async I/O */
9512115Sdyson	struct	selinfo	tun_rsel;	/* read select */
9612115Sdyson	struct mtx	tun_mtx;	/* protect mutable softc fields */
9712115Sdyson	struct cv	tun_cv;		/* protect against ref'd dev destroy */
9812115Sdyson};
9912115Sdyson#define TUN2IFP(sc)	((sc)->tun_ifp)
10012115Sdyson
10138909Sbde#define TUNDEBUG	if (tundebug) if_printf
10212115Sdyson
10312115Sdyson/*
10412115Sdyson * All mutable global variables in if_tun are locked using tunmtx, with
10512911Sphk * the exception of tundebug, which is used unlocked, and tunclones,
10612115Sdyson * which is static after setup.
10716322Sgpalmer */
10816322Sgpalmerstatic struct mtx tunmtx;
10916322Sgpalmerstatic const char tunname[] = "tun";
11016322Sgpalmerstatic MALLOC_DEFINE(M_TUN, tunname, "Tunnel Interface");
11116322Sgpalmerstatic int tundebug = 0;
11216322Sgpalmerstatic int tundclone = 1;
11316322Sgpalmerstatic struct clonedevs *tunclones;
11412115Sdysonstatic TAILQ_HEAD(,tun_softc)	tunhead = TAILQ_HEAD_INITIALIZER(tunhead);
11516322SgpalmerSYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
11612115Sdyson
11712115SdysonSYSCTL_DECL(_net_link);
11812115Sdysonstatic SYSCTL_NODE(_net_link, OID_AUTO, tun, CTLFLAG_RW, 0,
11912115Sdyson    "IP tunnel software network interface.");
12012115SdysonSYSCTL_INT(_net_link_tun, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tundclone, 0,
12112911Sphk    "Enable legacy devfs interface creation.");
12212115Sdyson
12312115SdysonTUNABLE_INT("net.link.tun.devfs_cloning", &tundclone);
12412115Sdyson
12512115Sdysonstatic void	tunclone(void *arg, struct ucred *cred, char *name,
12612115Sdyson		    int namelen, struct cdev **dev);
12712115Sdysonstatic void	tuncreate(const char *name, struct cdev *dev);
12812115Sdysonstatic int	tunifioctl(struct ifnet *, u_long, caddr_t);
12912115Sdysonstatic void	tuninit(struct ifnet *);
13012115Sdysonstatic int	tunmodevent(module_t, int, void *);
13129208Sbdestatic int	tunoutput(struct ifnet *, struct mbuf *,
13229208Sbde		    const struct sockaddr *, struct route *ro);
13329208Sbdestatic void	tunstart(struct ifnet *);
13429208Sbde
13512115Sdysonstatic int	tun_clone_create(struct if_clone *, int, caddr_t);
13612115Sdysonstatic void	tun_clone_destroy(struct ifnet *);
13712115Sdysonstatic struct if_clone *tun_cloner;
13812115Sdyson
13929888Skatostatic d_open_t		tunopen;
14029888Skatostatic d_close_t	tunclose;
14129888Skatostatic d_read_t		tunread;
14229888Skatostatic d_write_t	tunwrite;
14312115Sdysonstatic d_ioctl_t	tunioctl;
14412115Sdysonstatic d_poll_t		tunpoll;
14512115Sdysonstatic d_kqfilter_t	tunkqfilter;
14612115Sdyson
14712115Sdysonstatic int		tunkqread(struct knote *, long);
14812115Sdysonstatic int		tunkqwrite(struct knote *, long);
14912115Sdysonstatic void		tunkqdetach(struct knote *);
15012115Sdyson
15112115Sdysonstatic struct filterops tun_read_filterops = {
15230469Sjulian	.f_isfd =	1,
15312115Sdyson	.f_attach =	NULL,
15412115Sdyson	.f_detach =	tunkqdetach,
15512115Sdyson	.f_event =	tunkqread,
15612115Sdyson};
15712115Sdyson
15812115Sdysonstatic struct filterops tun_write_filterops = {
15912115Sdyson	.f_isfd =	1,
16012115Sdyson	.f_attach =	NULL,
16112115Sdyson	.f_detach =	tunkqdetach,
16212115Sdyson	.f_event =	tunkqwrite,
16312115Sdyson};
16412115Sdyson
16512115Sdysonstatic struct cdevsw tun_cdevsw = {
16612115Sdyson	.d_version =	D_VERSION,
16712115Sdyson	.d_flags =	D_NEEDMINOR,
16812115Sdyson	.d_open =	tunopen,
16916322Sgpalmer	.d_close =	tunclose,
17012115Sdyson	.d_read =	tunread,
17112115Sdyson	.d_write =	tunwrite,
17212115Sdyson	.d_ioctl =	tunioctl,
17312115Sdyson	.d_poll =	tunpoll,
17412115Sdyson	.d_kqfilter =	tunkqfilter,
17512115Sdyson	.d_name =	tunname,
17612911Sphk};
17712115Sdyson
17812115Sdysonstatic int
17912115Sdysontun_clone_create(struct if_clone *ifc, int unit, caddr_t params)
18012115Sdyson{
18112115Sdyson	struct cdev *dev;
18212115Sdyson	int i;
18312115Sdyson
18412115Sdyson	/* find any existing device, or allocate new unit number */
18512115Sdyson	i = clone_create(&tunclones, &tun_cdevsw, &unit, &dev, 0);
18612115Sdyson	if (i) {
18712115Sdyson		/* No preexisting struct cdev *, create one */
18812115Sdyson		dev = make_dev(&tun_cdevsw, unit,
18912115Sdyson		    UID_UUCP, GID_DIALER, 0600, "%s%d", tunname, unit);
19039028Sbde	}
19112115Sdyson	tuncreate(tunname, dev);
19243301Sdillon
19312115Sdyson	return (0);
19412115Sdyson}
19512115Sdyson
19612115Sdysonstatic void
19729888Skatotunclone(void *arg, struct ucred *cred, char *name, int namelen,
19829888Skato    struct cdev **dev)
19912115Sdyson{
20012115Sdyson	char devname[SPECNAMELEN + 1];
20112115Sdyson	int u, i, append_unit;
20212115Sdyson
20312115Sdyson	if (*dev != NULL)
20429888Skato		return;
20529888Skato
20629888Skato	/*
20729888Skato	 * If tun cloning is enabled, only the superuser can create an
20812115Sdyson	 * interface.
20912115Sdyson	 */
21012115Sdyson	if (!tundclone || priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0)
21112115Sdyson		return;
21222521Sdyson
21312115Sdyson	if (strcmp(name, tunname) == 0) {
21412115Sdyson		u = -1;
21522521Sdyson	} else if (dev_stdclone(name, NULL, tunname, &u) != 1)
21639670Sbde		return;	/* Don't recognise the name */
21739670Sbde	if (u != -1 && u > IF_MAXUNIT)
21839670Sbde		return;	/* Unit number too high */
21939670Sbde
22039670Sbde	if (u == -1)
22112115Sdyson		append_unit = 1;
22212115Sdyson	else
22312115Sdyson		append_unit = 0;
22412115Sdyson
22512115Sdyson	CURVNET_SET(CRED_TO_VNET(cred));
22639028Sbde	/* find any existing device, or allocate new unit number */
22739028Sbde	i = clone_create(&tunclones, &tun_cdevsw, &u, dev, 0);
22839028Sbde	if (i) {
22939028Sbde		if (append_unit) {
23039028Sbde			namelen = snprintf(devname, sizeof(devname), "%s%d",
23139028Sbde			    name, u);
23239028Sbde			name = devname;
23339028Sbde		}
23443301Sdillon		/* No preexisting struct cdev *, create one */
23543301Sdillon		*dev = make_dev_credf(MAKEDEV_REF, &tun_cdevsw, u, cred,
23639028Sbde		    UID_UUCP, GID_DIALER, 0600, "%s", name);
23739028Sbde	}
23839028Sbde
23939028Sbde	if_clone_create(name, namelen, NULL);
24039028Sbde	CURVNET_RESTORE();
24139028Sbde}
24239670Sbde
24339670Sbdestatic void
24439670Sbdetun_destroy(struct tun_softc *tp)
24539670Sbde{
24639670Sbde	struct cdev *dev;
24739670Sbde
24839670Sbde	mtx_lock(&tp->tun_mtx);
24939670Sbde	if ((tp->tun_flags & TUN_OPEN) != 0)
25039670Sbde		cv_wait_unlock(&tp->tun_cv, &tp->tun_mtx);
25139670Sbde	else
25239670Sbde		mtx_unlock(&tp->tun_mtx);
25339670Sbde
25439670Sbde	CURVNET_SET(TUN2IFP(tp)->if_vnet);
25512115Sdyson	dev = tp->tun_dev;
25612115Sdyson	bpfdetach(TUN2IFP(tp));
25739670Sbde	if_detach(TUN2IFP(tp));
25812115Sdyson	if_free(TUN2IFP(tp));
25912115Sdyson	destroy_dev(dev);
26012115Sdyson	seldrain(&tp->tun_rsel);
26112115Sdyson	knlist_clear(&tp->tun_rsel.si_note, 0);
26212115Sdyson	knlist_destroy(&tp->tun_rsel.si_note);
26312115Sdyson	mtx_destroy(&tp->tun_mtx);
26412115Sdyson	cv_destroy(&tp->tun_cv);
26512115Sdyson	free(tp, M_TUN);
26612115Sdyson	CURVNET_RESTORE();
26712115Sdyson}
26812115Sdyson
26912115Sdysonstatic void
27012115Sdysontun_clone_destroy(struct ifnet *ifp)
27143301Sdillon{
27212115Sdyson	struct tun_softc *tp = ifp->if_softc;
27312115Sdyson
27412115Sdyson	mtx_lock(&tunmtx);
27512115Sdyson	TAILQ_REMOVE(&tunhead, tp, tun_list);
27612115Sdyson	mtx_unlock(&tunmtx);
27712115Sdyson	tun_destroy(tp);
27812115Sdyson}
27940651Sbde
28040651Sbdestatic int
28112115Sdysontunmodevent(module_t mod, int type, void *data)
28212115Sdyson{
28312115Sdyson	static eventhandler_tag tag;
28439028Sbde	struct tun_softc *tp;
28539028Sbde
28639028Sbde	switch (type) {
28739028Sbde	case MOD_LOAD:
28839028Sbde		mtx_init(&tunmtx, "tunmtx", NULL, MTX_DEF);
28939028Sbde		clone_setup(&tunclones);
29039028Sbde		tag = EVENTHANDLER_REGISTER(dev_clone, tunclone, 0, 1000);
29139028Sbde		if (tag == NULL)
29239028Sbde			return (ENOMEM);
29339028Sbde		tun_cloner = if_clone_simple(tunname, tun_clone_create,
29443301Sdillon		    tun_clone_destroy, 0);
29539028Sbde		break;
29639028Sbde	case MOD_UNLOAD:
29739028Sbde		if_clone_detach(tun_cloner);
29839028Sbde		EVENTHANDLER_DEREGISTER(dev_clone, tag);
29939028Sbde		drain_dev_clone_events();
30039028Sbde
30129888Skato		mtx_lock(&tunmtx);
30229888Skato		while ((tp = TAILQ_FIRST(&tunhead)) != NULL) {
30329888Skato			TAILQ_REMOVE(&tunhead, tp, tun_list);
30429888Skato			mtx_unlock(&tunmtx);
30529888Skato			tun_destroy(tp);
30612115Sdyson			mtx_lock(&tunmtx);
30729888Skato		}
30812115Sdyson		mtx_unlock(&tunmtx);
30912115Sdyson		clone_cleanup(&tunclones);
31012115Sdyson		mtx_destroy(&tunmtx);
31112115Sdyson		break;
31212115Sdyson	default:
31312115Sdyson		return EOPNOTSUPP;
31412115Sdyson	}
31512115Sdyson	return 0;
31612115Sdyson}
31712115Sdyson
31812115Sdysonstatic moduledata_t tun_mod = {
31912115Sdyson	"if_tun",
32012115Sdyson	tunmodevent,
32112115Sdyson	0
32212115Sdyson};
32312115Sdyson
32412115SdysonDECLARE_MODULE(if_tun, tun_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
32512115SdysonMODULE_VERSION(if_tun, 1);
32612115Sdyson
32712115Sdysonstatic void
32812115Sdysontunstart(struct ifnet *ifp)
32912115Sdyson{
33012115Sdyson	struct tun_softc *tp = ifp->if_softc;
33112115Sdyson	struct mbuf *m;
33212115Sdyson
33312115Sdyson	TUNDEBUG(ifp,"%s starting\n", ifp->if_xname);
33412115Sdyson	if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
33512115Sdyson		IFQ_LOCK(&ifp->if_snd);
33612115Sdyson		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
33712115Sdyson		if (m == NULL) {
33812115Sdyson			IFQ_UNLOCK(&ifp->if_snd);
33912115Sdyson			return;
34012115Sdyson		}
34112115Sdyson		IFQ_UNLOCK(&ifp->if_snd);
34212115Sdyson	}
34312115Sdyson
34412115Sdyson	mtx_lock(&tp->tun_mtx);
34512115Sdyson	if (tp->tun_flags & TUN_RWAIT) {
34612115Sdyson		tp->tun_flags &= ~TUN_RWAIT;
34712115Sdyson		wakeup(tp);
34812115Sdyson	}
34912115Sdyson	selwakeuppri(&tp->tun_rsel, PZERO + 1);
35012115Sdyson	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
35112115Sdyson	if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio) {
35212115Sdyson		mtx_unlock(&tp->tun_mtx);
35312115Sdyson		pgsigio(&tp->tun_sigio, SIGIO, 0);
35439671Sbde	} else
35512115Sdyson		mtx_unlock(&tp->tun_mtx);
35612115Sdyson}
35712115Sdyson
35812115Sdyson/* XXX: should return an error code so it can fail. */
35912115Sdysonstatic void
36012115Sdysontuncreate(const char *name, struct cdev *dev)
36112115Sdyson{
36212115Sdyson	struct tun_softc *sc;
36339671Sbde	struct ifnet *ifp;
36412115Sdyson
36512115Sdyson	sc = malloc(sizeof(*sc), M_TUN, M_WAITOK | M_ZERO);
36612115Sdyson	mtx_init(&sc->tun_mtx, "tun_mtx", NULL, MTX_DEF);
36712115Sdyson	cv_init(&sc->tun_cv, "tun_condvar");
36812115Sdyson	sc->tun_flags = TUN_INITED;
36912115Sdyson	sc->tun_dev = dev;
37012115Sdyson	mtx_lock(&tunmtx);
37112115Sdyson	TAILQ_INSERT_TAIL(&tunhead, sc, tun_list);
37212115Sdyson	mtx_unlock(&tunmtx);
37339671Sbde
37412115Sdyson	ifp = sc->tun_ifp = if_alloc(IFT_PPP);
37512115Sdyson	if (ifp == NULL)
37612115Sdyson		panic("%s%d: failed to if_alloc() interface.\n",
37712115Sdyson		    name, dev2unit(dev));
37812115Sdyson	if_initname(ifp, name, dev2unit(dev));
37912115Sdyson	ifp->if_mtu = TUNMTU;
38012115Sdyson	ifp->if_ioctl = tunifioctl;
38112115Sdyson	ifp->if_output = tunoutput;
38212115Sdyson	ifp->if_start = tunstart;
38312115Sdyson	ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
38412115Sdyson	ifp->if_softc = sc;
38512115Sdyson	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
38612115Sdyson	ifp->if_snd.ifq_drv_maxlen = 0;
38712115Sdyson	IFQ_SET_READY(&ifp->if_snd);
38812115Sdyson	knlist_init_mtx(&sc->tun_rsel.si_note, &sc->tun_mtx);
38912115Sdyson	ifp->if_capabilities |= IFCAP_LINKSTATE;
39012115Sdyson	ifp->if_capenable |= IFCAP_LINKSTATE;
39112115Sdyson
39212115Sdyson	if_attach(ifp);
39312115Sdyson	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
39412115Sdyson	dev->si_drv1 = sc;
39512115Sdyson	TUNDEBUG(ifp, "interface %s is created, minor = %#x\n",
39612115Sdyson	    ifp->if_xname, dev2unit(dev));
39712115Sdyson}
39812115Sdyson
39912115Sdysonstatic int
40012115Sdysontunopen(struct cdev *dev, int flag, int mode, struct thread *td)
40112115Sdyson{
40212115Sdyson	struct ifnet	*ifp;
40312115Sdyson	struct tun_softc *tp;
40412115Sdyson
40512115Sdyson	/*
40612115Sdyson	 * XXXRW: Non-atomic test and set of dev->si_drv1 requires
40712115Sdyson	 * synchronization.
40812115Sdyson	 */
40912115Sdyson	tp = dev->si_drv1;
41012115Sdyson	if (!tp) {
41112115Sdyson		tuncreate(tunname, dev);
41212115Sdyson		tp = dev->si_drv1;
41312115Sdyson	}
41412115Sdyson
41512115Sdyson	/*
41612115Sdyson	 * XXXRW: This use of tun_pid is subject to error due to the
41712115Sdyson	 * fact that a reference to the tunnel can live beyond the
41812115Sdyson	 * death of the process that created it.  Can we replace this
41912115Sdyson	 * with a simple busy flag?
42012115Sdyson	 */
42112115Sdyson	mtx_lock(&tp->tun_mtx);
42212115Sdyson	if (tp->tun_pid != 0 && tp->tun_pid != td->td_proc->p_pid) {
42312115Sdyson		mtx_unlock(&tp->tun_mtx);
42412115Sdyson		return (EBUSY);
42512115Sdyson	}
42612115Sdyson	tp->tun_pid = td->td_proc->p_pid;
42712115Sdyson
42812115Sdyson	tp->tun_flags |= TUN_OPEN;
42912115Sdyson	ifp = TUN2IFP(tp);
43012115Sdyson	if_link_state_change(ifp, LINK_STATE_UP);
43112115Sdyson	TUNDEBUG(ifp, "open\n");
43212115Sdyson	mtx_unlock(&tp->tun_mtx);
43312115Sdyson
43412115Sdyson	return (0);
43512115Sdyson}
43612115Sdyson
43712115Sdyson/*
43812115Sdyson * tunclose - close the device - mark i/f down & delete
43912115Sdyson * routing info
44012115Sdyson */
44112115Sdysonstatic	int
44212115Sdysontunclose(struct cdev *dev, int foo, int bar, struct thread *td)
44312115Sdyson{
44412115Sdyson	struct tun_softc *tp;
44512115Sdyson	struct ifnet *ifp;
44612115Sdyson
44712115Sdyson	tp = dev->si_drv1;
44812115Sdyson	ifp = TUN2IFP(tp);
44912115Sdyson
45012115Sdyson	mtx_lock(&tp->tun_mtx);
45112115Sdyson	tp->tun_flags &= ~TUN_OPEN;
45212115Sdyson	tp->tun_pid = 0;
45312115Sdyson
45412115Sdyson	/*
45512115Sdyson	 * junk all pending output
45612115Sdyson	 */
45712115Sdyson	CURVNET_SET(ifp->if_vnet);
45812115Sdyson	IFQ_PURGE(&ifp->if_snd);
45912115Sdyson
46027881Sdyson	if (ifp->if_flags & IFF_UP) {
46127881Sdyson		mtx_unlock(&tp->tun_mtx);
46212115Sdyson		if_down(ifp);
46312115Sdyson		mtx_lock(&tp->tun_mtx);
46412115Sdyson	}
46527881Sdyson
46612115Sdyson	/* Delete all addresses and routes which reference this interface. */
46712115Sdyson	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
46812115Sdyson		struct ifaddr *ifa;
46912115Sdyson
47012115Sdyson		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
47112115Sdyson		mtx_unlock(&tp->tun_mtx);
47212115Sdyson		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
47312115Sdyson			/* deal w/IPv4 PtP destination; unlocked read */
47412115Sdyson			if (ifa->ifa_addr->sa_family == AF_INET) {
47512115Sdyson				rtinit(ifa, (int)RTM_DELETE,
47612115Sdyson				    tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
47712115Sdyson			} else {
47812115Sdyson				rtinit(ifa, (int)RTM_DELETE, 0);
47912115Sdyson			}
48012115Sdyson		}
48112115Sdyson		if_purgeaddrs(ifp);
48212115Sdyson		mtx_lock(&tp->tun_mtx);
48312115Sdyson	}
48412115Sdyson	if_link_state_change(ifp, LINK_STATE_DOWN);
48512115Sdyson	CURVNET_RESTORE();
48612115Sdyson
48712115Sdyson	funsetown(&tp->tun_sigio);
48812115Sdyson	selwakeuppri(&tp->tun_rsel, PZERO + 1);
48912115Sdyson	KNOTE_LOCKED(&tp->tun_rsel.si_note, 0);
49012115Sdyson	TUNDEBUG (ifp, "closed\n");
49112115Sdyson
49212115Sdyson	cv_broadcast(&tp->tun_cv);
49312115Sdyson	mtx_unlock(&tp->tun_mtx);
49412115Sdyson	return (0);
49512115Sdyson}
49612911Sphk
49712115Sdysonstatic void
49812115Sdysontuninit(struct ifnet *ifp)
49912115Sdyson{
50012115Sdyson	struct tun_softc *tp = ifp->if_softc;
50112115Sdyson#ifdef INET
50212115Sdyson	struct ifaddr *ifa;
50312115Sdyson#endif
50412115Sdyson
50512115Sdyson	TUNDEBUG(ifp, "tuninit\n");
50612115Sdyson
50712147Sdyson	mtx_lock(&tp->tun_mtx);
50812115Sdyson	ifp->if_flags |= IFF_UP;
50912115Sdyson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
51012115Sdyson	getmicrotime(&ifp->if_lastchange);
51112115Sdyson
51212115Sdyson#ifdef INET
51312115Sdyson	if_addr_rlock(ifp);
51412115Sdyson	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
51512115Sdyson		if (ifa->ifa_addr->sa_family == AF_INET) {
51612115Sdyson			struct sockaddr_in *si;
51712115Sdyson
51812115Sdyson			si = (struct sockaddr_in *)ifa->ifa_addr;
51912115Sdyson			if (si->sin_addr.s_addr)
52012115Sdyson				tp->tun_flags |= TUN_IASET;
52143301Sdillon
52212115Sdyson			si = (struct sockaddr_in *)ifa->ifa_dstaddr;
52312115Sdyson			if (si && si->sin_addr.s_addr)
52412115Sdyson				tp->tun_flags |= TUN_DSTADDR;
52512115Sdyson		}
52612115Sdyson	}
52712115Sdyson	if_addr_runlock(ifp);
52812115Sdyson#endif
52912115Sdyson	mtx_unlock(&tp->tun_mtx);
53012115Sdyson}
53112115Sdyson
53212115Sdyson/*
53312115Sdyson * Process an ioctl request.
53412115Sdyson */
53512115Sdysonstatic int
53612115Sdysontunifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
53712115Sdyson{
53843301Sdillon	struct ifreq *ifr = (struct ifreq *)data;
53912115Sdyson	struct tun_softc *tp = ifp->if_softc;
54012115Sdyson	struct ifstat *ifs;
54112115Sdyson	int		error = 0;
54212115Sdyson
54312115Sdyson	switch(cmd) {
54412115Sdyson	case SIOCGIFSTATUS:
54512115Sdyson		ifs = (struct ifstat *)data;
54612115Sdyson		mtx_lock(&tp->tun_mtx);
54712115Sdyson		if (tp->tun_pid)
54812115Sdyson			sprintf(ifs->ascii + strlen(ifs->ascii),
54939678Sbde			    "\tOpened by PID %d\n", tp->tun_pid);
55012115Sdyson		mtx_unlock(&tp->tun_mtx);
55139678Sbde		break;
55239678Sbde	case SIOCSIFADDR:
55339678Sbde		tuninit(ifp);
55439678Sbde		TUNDEBUG(ifp, "address set\n");
55512115Sdyson		break;
55612115Sdyson	case SIOCSIFMTU:
55712115Sdyson		ifp->if_mtu = ifr->ifr_mtu;
55812115Sdyson		TUNDEBUG(ifp, "mtu set\n");
55939678Sbde		break;
56039678Sbde	case SIOCSIFFLAGS:
56112115Sdyson	case SIOCADDMULTI:
56212115Sdyson	case SIOCDELMULTI:
56312115Sdyson		break;
56439678Sbde	default:
56539678Sbde		error = EINVAL;
56639678Sbde	}
56712115Sdyson	return (error);
56839678Sbde}
56912115Sdyson
57012115Sdyson/*
57112115Sdyson * tunoutput - queue packets from higher level ready to put out.
57212115Sdyson */
57312115Sdysonstatic int
57412115Sdysontunoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
57539678Sbde    struct route *ro)
57612115Sdyson{
57739678Sbde	struct tun_softc *tp = ifp->if_softc;
57839678Sbde	u_short cached_tun_flags;
57912115Sdyson	int error;
58012115Sdyson	u_int32_t af;
58112115Sdyson
58212115Sdyson	TUNDEBUG (ifp, "tunoutput\n");
58339678Sbde
58439678Sbde#ifdef MAC
58512115Sdyson	error = mac_ifnet_check_transmit(ifp, m0);
58612115Sdyson	if (error) {
58739678Sbde		m_freem(m0);
58812115Sdyson		return (error);
58939678Sbde	}
59012115Sdyson#endif
59112115Sdyson
59212115Sdyson	/* Could be unlocked read? */
59312115Sdyson	mtx_lock(&tp->tun_mtx);
59412115Sdyson	cached_tun_flags = tp->tun_flags;
59512115Sdyson	mtx_unlock(&tp->tun_mtx);
59612911Sphk	if ((cached_tun_flags & TUN_READY) != TUN_READY) {
59712115Sdyson		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
59812115Sdyson		m_freem (m0);
59912115Sdyson		return (EHOSTDOWN);
60012115Sdyson	}
60112115Sdyson
60212115Sdyson	if ((ifp->if_flags & IFF_UP) != IFF_UP) {
60312115Sdyson		m_freem (m0);
60412115Sdyson		return (EHOSTDOWN);
60512115Sdyson	}
60612115Sdyson
60712115Sdyson	/* BPF writes need to be handled specially. */
60812115Sdyson	if (dst->sa_family == AF_UNSPEC)
60912115Sdyson		bcopy(dst->sa_data, &af, sizeof(af));
61012115Sdyson	else
61112115Sdyson		af = dst->sa_family;
61212115Sdyson
61312115Sdyson	if (bpf_peers_present(ifp->if_bpf))
61412115Sdyson		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m0);
61512115Sdyson
61612115Sdyson	/* prepend sockaddr? this may abort if the mbuf allocation fails */
61712115Sdyson	if (cached_tun_flags & TUN_LMODE) {
61843301Sdillon		/* allocate space for sockaddr */
61912115Sdyson		M_PREPEND(m0, dst->sa_len, M_NOWAIT);
62012115Sdyson
62112115Sdyson		/* if allocation failed drop packet */
62243301Sdillon		if (m0 == NULL) {
62312115Sdyson			ifp->if_iqdrops++;
62412115Sdyson			ifp->if_oerrors++;
62512115Sdyson			return (ENOBUFS);
62612115Sdyson		} else {
62712115Sdyson			bcopy(dst, m0->m_data, dst->sa_len);
62812115Sdyson		}
62912115Sdyson	}
63043301Sdillon
63112115Sdyson	if (cached_tun_flags & TUN_IFHEAD) {
63212115Sdyson		/* Prepend the address family */
63312115Sdyson		M_PREPEND(m0, 4, M_NOWAIT);
63412115Sdyson
63512115Sdyson		/* if allocation failed drop packet */
63612115Sdyson		if (m0 == NULL) {
63712115Sdyson			ifp->if_iqdrops++;
63812115Sdyson			ifp->if_oerrors++;
63912115Sdyson			return (ENOBUFS);
64012115Sdyson		} else
64143301Sdillon			*(u_int32_t *)m0->m_data = htonl(af);
64212115Sdyson	} else {
64312115Sdyson#ifdef INET
64412115Sdyson		if (af != AF_INET)
64512115Sdyson#endif
64612115Sdyson		{
64712115Sdyson			m_freem(m0);
64812115Sdyson			return (EAFNOSUPPORT);
64912115Sdyson		}
65012115Sdyson	}
65112115Sdyson
65212115Sdyson	error = (ifp->if_transmit)(ifp, m0);
65312115Sdyson	if (error)
65412115Sdyson		return (ENOBUFS);
65539670Sbde	ifp->if_opackets++;
65639670Sbde	return (0);
65739670Sbde}
65839670Sbde
65939670Sbde/*
66039670Sbde * the cdevsw interface is now pretty minimal.
66139670Sbde */
66239670Sbdestatic	int
66339670Sbdetunioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag,
66439670Sbde    struct thread *td)
66539670Sbde{
66639670Sbde	int		error;
66712115Sdyson	struct tun_softc *tp = dev->si_drv1;
66812115Sdyson	struct tuninfo *tunp;
66930280Sphk
67030474Sphk	switch (cmd) {
67130474Sphk	case TUNSIFINFO:
67230492Sphk		tunp = (struct tuninfo *)data;
67330474Sphk		if (tunp->mtu < IF_MINMTU)
67430474Sphk			return (EINVAL);
67512115Sdyson		if (TUN2IFP(tp)->if_mtu != tunp->mtu) {
67612115Sdyson			error = priv_check(td, PRIV_NET_SETIFMTU);
67712115Sdyson			if (error)
67812115Sdyson				return (error);
67912115Sdyson		}
68012115Sdyson		mtx_lock(&tp->tun_mtx);
68112115Sdyson		TUN2IFP(tp)->if_mtu = tunp->mtu;
68212115Sdyson		TUN2IFP(tp)->if_type = tunp->type;
68312115Sdyson		TUN2IFP(tp)->if_baudrate = tunp->baudrate;
68439671Sbde		mtx_unlock(&tp->tun_mtx);
68539671Sbde		break;
68639671Sbde	case TUNGIFINFO:
68739671Sbde		tunp = (struct tuninfo *)data;
68839671Sbde		mtx_lock(&tp->tun_mtx);
68939671Sbde		tunp->mtu = TUN2IFP(tp)->if_mtu;
69012115Sdyson		tunp->type = TUN2IFP(tp)->if_type;
69112115Sdyson		tunp->baudrate = TUN2IFP(tp)->if_baudrate;
69212115Sdyson		mtx_unlock(&tp->tun_mtx);
69312115Sdyson		break;
69412115Sdyson	case TUNSDEBUG:
69512115Sdyson		tundebug = *(int *)data;
69612115Sdyson		break;
69739670Sbde	case TUNGDEBUG:
69812115Sdyson		*(int *)data = tundebug;
69912115Sdyson		break;
70012115Sdyson	case TUNSLMODE:
70112115Sdyson		mtx_lock(&tp->tun_mtx);
70212115Sdyson		if (*(int *)data) {
70312115Sdyson			tp->tun_flags |= TUN_LMODE;
70438909Sbde			tp->tun_flags &= ~TUN_IFHEAD;
70512115Sdyson		} else
70612115Sdyson			tp->tun_flags &= ~TUN_LMODE;
70712115Sdyson		mtx_unlock(&tp->tun_mtx);
70812115Sdyson		break;
70912115Sdyson	case TUNSIFHEAD:
71012115Sdyson		mtx_lock(&tp->tun_mtx);
71112115Sdyson		if (*(int *)data) {
71212115Sdyson			tp->tun_flags |= TUN_IFHEAD;
71312115Sdyson			tp->tun_flags &= ~TUN_LMODE;
71412115Sdyson		} else
71512115Sdyson			tp->tun_flags &= ~TUN_IFHEAD;
71612115Sdyson		mtx_unlock(&tp->tun_mtx);
71712115Sdyson		break;
71834430Seivind	case TUNGIFHEAD:
71934430Seivind		mtx_lock(&tp->tun_mtx);
72034430Seivind		*(int *)data = (tp->tun_flags & TUN_IFHEAD) ? 1 : 0;
72112115Sdyson		mtx_unlock(&tp->tun_mtx);
72212115Sdyson		break;
72312115Sdyson	case TUNSIFMODE:
72412115Sdyson		/* deny this if UP */
72512115Sdyson		if (TUN2IFP(tp)->if_flags & IFF_UP)
72612115Sdyson			return(EBUSY);
72739671Sbde
72839671Sbde		switch (*(int *)data & ~IFF_MULTICAST) {
72912115Sdyson		case IFF_POINTOPOINT:
73012115Sdyson		case IFF_BROADCAST:
73112115Sdyson			mtx_lock(&tp->tun_mtx);
73212115Sdyson			TUN2IFP(tp)->if_flags &=
73312115Sdyson			    ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
73412115Sdyson			TUN2IFP(tp)->if_flags |= *(int *)data;
73512115Sdyson			mtx_unlock(&tp->tun_mtx);
73612115Sdyson			break;
73712115Sdyson		default:
73812911Sphk			return(EINVAL);
73912115Sdyson		}
74012115Sdyson		break;
74112115Sdyson	case TUNSIFPID:
74212115Sdyson		mtx_lock(&tp->tun_mtx);
74312115Sdyson		tp->tun_pid = curthread->td_proc->p_pid;
74412115Sdyson		mtx_unlock(&tp->tun_mtx);
74512115Sdyson		break;
74612115Sdyson	case FIONBIO:
74712115Sdyson		break;
74812115Sdyson	case FIOASYNC:
74912115Sdyson		mtx_lock(&tp->tun_mtx);
75012115Sdyson		if (*(int *)data)
75112115Sdyson			tp->tun_flags |= TUN_ASYNC;
75212115Sdyson		else
75312115Sdyson			tp->tun_flags &= ~TUN_ASYNC;
75443301Sdillon		mtx_unlock(&tp->tun_mtx);
75512115Sdyson		break;
75612115Sdyson	case FIONREAD:
75712115Sdyson		if (!IFQ_IS_EMPTY(&TUN2IFP(tp)->if_snd)) {
75812115Sdyson			struct mbuf *mb;
75939670Sbde			IFQ_LOCK(&TUN2IFP(tp)->if_snd);
76039670Sbde			IFQ_POLL_NOLOCK(&TUN2IFP(tp)->if_snd, mb);
76139670Sbde			for (*(int *)data = 0; mb != NULL; mb = mb->m_next)
76212115Sdyson				*(int *)data += mb->m_len;
76312115Sdyson			IFQ_UNLOCK(&TUN2IFP(tp)->if_snd);
76427881Sdyson		} else
76512115Sdyson			*(int *)data = 0;
76612115Sdyson		break;
76727881Sdyson	case FIOSETOWN:
76839671Sbde		return (fsetown(*(int *)data, &tp->tun_sigio));
76927881Sdyson
77012115Sdyson	case FIOGETOWN:
77112115Sdyson		*(int *)data = fgetown(&tp->tun_sigio);
77212115Sdyson		return (0);
77327881Sdyson
77427881Sdyson	/* This is deprecated, FIOSETOWN should be used instead. */
77512115Sdyson	case TIOCSPGRP:
77612115Sdyson		return (fsetown(-(*(int *)data), &tp->tun_sigio));
77727881Sdyson
77812115Sdyson	/* This is deprecated, FIOGETOWN should be used instead. */
77934430Seivind	case TIOCGPGRP:
78012115Sdyson		*(int *)data = -fgetown(&tp->tun_sigio);
78112115Sdyson		return (0);
78212115Sdyson
78312115Sdyson	default:
78412115Sdyson		return (ENOTTY);
78512115Sdyson	}
78612115Sdyson	return (0);
78712115Sdyson}
78812115Sdyson
78912115Sdyson/*
79012115Sdyson * The cdevsw read interface - reads a packet at a time, or at
79112115Sdyson * least as much of a packet as can be read.
79212115Sdyson */
79312115Sdysonstatic	int
79412911Sphktunread(struct cdev *dev, struct uio *uio, int flag)
79512115Sdyson{
79612115Sdyson	struct tun_softc *tp = dev->si_drv1;
79712115Sdyson	struct ifnet	*ifp = TUN2IFP(tp);
79812115Sdyson	struct mbuf	*m;
79912115Sdyson	int		error=0, len;
80012115Sdyson
80112147Sdyson	TUNDEBUG (ifp, "read\n");
80212746Sbde	mtx_lock(&tp->tun_mtx);
80312746Sbde	if ((tp->tun_flags & TUN_READY) != TUN_READY) {
80412746Sbde		mtx_unlock(&tp->tun_mtx);
80512115Sdyson		TUNDEBUG (ifp, "not ready 0%o\n", tp->tun_flags);
80612115Sdyson		return (EHOSTDOWN);
80712115Sdyson	}
80812115Sdyson
80943301Sdillon	tp->tun_flags &= ~TUN_RWAIT;
81012115Sdyson
81112115Sdyson	do {
81212115Sdyson		IFQ_DEQUEUE(&ifp->if_snd, m);
81312115Sdyson		if (m == NULL) {
81412115Sdyson			if (flag & O_NONBLOCK) {
81512115Sdyson				mtx_unlock(&tp->tun_mtx);
81612115Sdyson				return (EWOULDBLOCK);
81712115Sdyson			}
81812115Sdyson			tp->tun_flags |= TUN_RWAIT;
81912115Sdyson			error = mtx_sleep(tp, &tp->tun_mtx, PCATCH | (PZERO + 1),
82012115Sdyson			    "tunread", 0);
82112115Sdyson			if (error != 0) {
82212115Sdyson				mtx_unlock(&tp->tun_mtx);
82312115Sdyson				return (error);
82412115Sdyson			}
82512115Sdyson		}
82612115Sdyson	} while (m == NULL);
82712115Sdyson	mtx_unlock(&tp->tun_mtx);
82812115Sdyson
82912115Sdyson	while (m && uio->uio_resid > 0 && error == 0) {
83012911Sphk		len = min(uio->uio_resid, m->m_len);
83112115Sdyson		if (len != 0)
83212115Sdyson			error = uiomove(mtod(m, void *), len, uio);
83312115Sdyson		m = m_free(m);
83412115Sdyson	}
83512115Sdyson
83612115Sdyson	if (m) {
83712115Sdyson		TUNDEBUG(ifp, "Dropping mbuf\n");
83812115Sdyson		m_freem(m);
83912115Sdyson	}
84012115Sdyson	return (error);
84112115Sdyson}
84212115Sdyson
84312115Sdyson/*
84412115Sdyson * the cdevsw write interface - an atomic write is a packet - or else!
84512115Sdyson */
84612115Sdysonstatic	int
84712115Sdysontunwrite(struct cdev *dev, struct uio *uio, int flag)
84812115Sdyson{
84912115Sdyson	struct tun_softc *tp = dev->si_drv1;
85012115Sdyson	struct ifnet	*ifp = TUN2IFP(tp);
85112115Sdyson	struct mbuf	*m;
85212115Sdyson	uint32_t	family;
85312115Sdyson	int 		isr;
85412115Sdyson
85512115Sdyson	TUNDEBUG(ifp, "tunwrite\n");
85612115Sdyson
85712115Sdyson	if ((ifp->if_flags & IFF_UP) != IFF_UP)
85812115Sdyson		/* ignore silently */
85912115Sdyson		return (0);
86012115Sdyson
86112115Sdyson	if (uio->uio_resid == 0)
86212115Sdyson		return (0);
86312115Sdyson
86412115Sdyson	if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
86512115Sdyson		TUNDEBUG(ifp, "len=%zd!\n", uio->uio_resid);
86612115Sdyson		return (EIO);
86712115Sdyson	}
86812115Sdyson
86938909Sbde	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, 0, M_PKTHDR)) == NULL) {
87012115Sdyson		ifp->if_ierrors++;
87112115Sdyson		return (ENOBUFS);
87212115Sdyson	}
87312115Sdyson
87412115Sdyson	m->m_pkthdr.rcvif = ifp;
87512115Sdyson#ifdef MAC
87612115Sdyson	mac_ifnet_create_mbuf(ifp, m);
87712115Sdyson#endif
87812115Sdyson
87912115Sdyson	/* Could be unlocked read? */
88012115Sdyson	mtx_lock(&tp->tun_mtx);
88112115Sdyson	if (tp->tun_flags & TUN_IFHEAD) {
88212115Sdyson		mtx_unlock(&tp->tun_mtx);
88312115Sdyson		if (m->m_len < sizeof(family) &&
88412115Sdyson		    (m = m_pullup(m, sizeof(family))) == NULL)
88512911Sphk			return (ENOBUFS);
88612115Sdyson		family = ntohl(*mtod(m, u_int32_t *));
88712115Sdyson		m_adj(m, sizeof(family));
88812115Sdyson	} else {
88912115Sdyson		mtx_unlock(&tp->tun_mtx);
89012115Sdyson		family = AF_INET;
89112115Sdyson	}
89239678Sbde
89339678Sbde	BPF_MTAP2(ifp, &family, sizeof(family), m);
89439678Sbde
89539678Sbde	switch (family) {
89612115Sdyson#ifdef INET
89712115Sdyson	case AF_INET:
89812115Sdyson		isr = NETISR_IP;
89939678Sbde		break;
90039678Sbde#endif
90139678Sbde#ifdef INET6
90212115Sdyson	case AF_INET6:
90312115Sdyson		isr = NETISR_IPV6;
90412115Sdyson		break;
90512115Sdyson#endif
90639678Sbde#ifdef IPX
90712115Sdyson	case AF_IPX:
90839678Sbde		isr = NETISR_IPX;
90912115Sdyson		break;
91012115Sdyson#endif
91112115Sdyson#ifdef NETATALK
91212115Sdyson	case AF_APPLETALK:
91312115Sdyson		isr = NETISR_ATALK2;
91412115Sdyson		break;
91539678Sbde#endif
91639678Sbde	default:
91712115Sdyson		m_freem(m);
91839678Sbde		return (EAFNOSUPPORT);
91943309Sdillon	}
92012115Sdyson	if (harvest.point_to_point)
92143395Sbde		random_harvest(&(m->m_data), 12, 3, 0, RANDOM_NET_TUN);
92239678Sbde	ifp->if_ibytes += m->m_pkthdr.len;
92312115Sdyson	ifp->if_ipackets++;
92439678Sbde	CURVNET_SET(ifp->if_vnet);
92539678Sbde	M_SETFIB(m, ifp->if_fib);
92639678Sbde	netisr_dispatch(isr, m);
92739678Sbde	CURVNET_RESTORE();
92839678Sbde	return (0);
92939678Sbde}
93039678Sbde
93139678Sbde/*
93239678Sbde * tunpoll - the poll interface, this is only useful on reads
93343301Sdillon * really. The write detect always returns true, write never blocks
93412115Sdyson * anyway, it either accepts the packet or drops it.
93539678Sbde */
93639678Sbdestatic	int
93739678Sbdetunpoll(struct cdev *dev, int events, struct thread *td)
93812115Sdyson{
93939678Sbde	struct tun_softc *tp = dev->si_drv1;
94012115Sdyson	struct ifnet	*ifp = TUN2IFP(tp);
94112115Sdyson	int		revents = 0;
94212115Sdyson	struct mbuf	*m;
94339678Sbde
94439678Sbde	TUNDEBUG(ifp, "tunpoll\n");
94539678Sbde
94639678Sbde	if (events & (POLLIN | POLLRDNORM)) {
94739678Sbde		IFQ_LOCK(&ifp->if_snd);
94839678Sbde		IFQ_POLL_NOLOCK(&ifp->if_snd, m);
94912115Sdyson		if (m != NULL) {
95012115Sdyson			TUNDEBUG(ifp, "tunpoll q=%d\n", ifp->if_snd.ifq_len);
95112115Sdyson			revents |= events & (POLLIN | POLLRDNORM);
95239678Sbde		} else {
95339678Sbde			TUNDEBUG(ifp, "tunpoll waiting\n");
95439678Sbde			selrecord(td, &tp->tun_rsel);
95539678Sbde		}
95639678Sbde		IFQ_UNLOCK(&ifp->if_snd);
95739678Sbde	}
95839678Sbde	if (events & (POLLOUT | POLLWRNORM))
95939678Sbde		revents |= events & (POLLOUT | POLLWRNORM);
96039678Sbde
96112115Sdyson	return (revents);
96212115Sdyson}
96312115Sdyson
96412115Sdyson/*
96512115Sdyson * tunkqfilter - support for the kevent() system call.
96612115Sdyson */
96712115Sdysonstatic int
96812115Sdysontunkqfilter(struct cdev *dev, struct knote *kn)
96912115Sdyson{
97012911Sphk	struct tun_softc	*tp = dev->si_drv1;
97112115Sdyson	struct ifnet	*ifp = TUN2IFP(tp);
97212115Sdyson
97312115Sdyson	switch(kn->kn_filter) {
97412115Sdyson	case EVFILT_READ:
97512115Sdyson		TUNDEBUG(ifp, "%s kqfilter: EVFILT_READ, minor = %#x\n",
97612115Sdyson		    ifp->if_xname, dev2unit(dev));
97712115Sdyson		kn->kn_fop = &tun_read_filterops;
97812115Sdyson		break;
97912115Sdyson
98012115Sdyson	case EVFILT_WRITE:
98112115Sdyson		TUNDEBUG(ifp, "%s kqfilter: EVFILT_WRITE, minor = %#x\n",
98230280Sphk		    ifp->if_xname, dev2unit(dev));
98312115Sdyson		kn->kn_fop = &tun_write_filterops;
98412115Sdyson		break;
98512115Sdyson
98612115Sdyson	default:
98712406Sdyson		TUNDEBUG(ifp, "%s kqfilter: invalid filter, minor = %#x\n",
98812115Sdyson		    ifp->if_xname, dev2unit(dev));
98912115Sdyson		return(EINVAL);
99012115Sdyson	}
99112406Sdyson
99212406Sdyson	kn->kn_hook = tp;
99312406Sdyson	knlist_add(&tp->tun_rsel.si_note, kn, 0);
99412406Sdyson
99512406Sdyson	return (0);
99612406Sdyson}
99712406Sdyson
99812406Sdyson/*
99936102Sbde * Return true of there is data in the interface queue.
100012406Sdyson */
100112406Sdysonstatic int
100212406Sdysontunkqread(struct knote *kn, long hint)
100312406Sdyson{
100412406Sdyson	int			ret;
100536102Sbde	struct tun_softc	*tp = kn->kn_hook;
100636102Sbde	struct cdev		*dev = tp->tun_dev;
100736102Sbde	struct ifnet	*ifp = TUN2IFP(tp);
100836102Sbde
100936102Sbde	if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
101036102Sbde		TUNDEBUG(ifp,
101136102Sbde		    "%s have data in the queue.  Len = %d, minor = %#x\n",
101236102Sbde		    ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
101336102Sbde		ret = 1;
101412115Sdyson	} else {
101543301Sdillon		TUNDEBUG(ifp,
101636102Sbde		    "%s waiting for data, minor = %#x\n", ifp->if_xname,
101736102Sbde		    dev2unit(dev));
101836102Sbde		ret = 0;
101912115Sdyson	}
102036102Sbde
102112115Sdyson	return (ret);
102212115Sdyson}
102312115Sdyson
102438998Sbde/*
102512115Sdyson * Always can write, always return MTU in kn->data.
102612115Sdyson */
102712115Sdysonstatic int
102812115Sdysontunkqwrite(struct knote *kn, long hint)
102912115Sdyson{
103012115Sdyson	struct tun_softc	*tp = kn->kn_hook;
103112115Sdyson	struct ifnet	*ifp = TUN2IFP(tp);
103212115Sdyson
103312115Sdyson	kn->kn_data = ifp->if_mtu;
103412115Sdyson
103512115Sdyson	return (1);
103612115Sdyson}
103712115Sdyson
103812115Sdysonstatic void
103912115Sdysontunkqdetach(struct knote *kn)
104012115Sdyson{
104112115Sdyson	struct tun_softc	*tp = kn->kn_hook;
104212406Sdyson
104312406Sdyson	knlist_remove(&tp->tun_rsel.si_note, kn, 0);
104412406Sdyson}
104512406Sdyson