if_tap.c revision 257696
1139823Simp/*-
263670Snsayer * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
363670Snsayer * All rights reserved.
463670Snsayer *
563670Snsayer * Redistribution and use in source and binary forms, with or without
663670Snsayer * modification, are permitted provided that the following conditions
763670Snsayer * are met:
863670Snsayer * 1. Redistributions of source code must retain the above copyright
963670Snsayer *    notice, this list of conditions and the following disclaimer.
1063670Snsayer * 2. Redistributions in binary form must reproduce the above copyright
1163670Snsayer *    notice, this list of conditions and the following disclaimer in the
1263670Snsayer *    documentation and/or other materials provided with the distribution.
1363670Snsayer *
1463670Snsayer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1563670Snsayer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1663670Snsayer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1763670Snsayer * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1863670Snsayer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1963670Snsayer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2063670Snsayer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2163670Snsayer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2263670Snsayer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2363670Snsayer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2463670Snsayer * SUCH DAMAGE.
2563670Snsayer *
2663670Snsayer * BASED ON:
2763670Snsayer * -------------------------------------------------------------------------
2863670Snsayer *
2963670Snsayer * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
3063670Snsayer * Nottingham University 1987.
3163670Snsayer */
3263670Snsayer
3363670Snsayer/*
3463670Snsayer * $FreeBSD: head/sys/net/if_tap.c 257696 2013-11-05 10:29:47Z glebius $
3563803Snsayer * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
3663670Snsayer */
3763670Snsayer
38162711Sru#include "opt_compat.h"
3963670Snsayer#include "opt_inet.h"
4063670Snsayer
4163670Snsayer#include <sys/param.h>
4263670Snsayer#include <sys/conf.h>
43139207Sphk#include <sys/fcntl.h>
4463670Snsayer#include <sys/filio.h>
45236724Strociny#include <sys/jail.h>
4663670Snsayer#include <sys/kernel.h>
4763670Snsayer#include <sys/malloc.h>
4863670Snsayer#include <sys/mbuf.h>
49129880Sphk#include <sys/module.h>
5063670Snsayer#include <sys/poll.h>
51164033Srwatson#include <sys/priv.h>
5263670Snsayer#include <sys/proc.h>
53139207Sphk#include <sys/selinfo.h>
5463670Snsayer#include <sys/signalvar.h>
5563670Snsayer#include <sys/socket.h>
5663670Snsayer#include <sys/sockio.h>
5763670Snsayer#include <sys/sysctl.h>
5863670Snsayer#include <sys/systm.h>
5963670Snsayer#include <sys/ttycom.h>
6063670Snsayer#include <sys/uio.h>
6183043Sbrooks#include <sys/queue.h>
6263670Snsayer
6363670Snsayer#include <net/bpf.h>
6463670Snsayer#include <net/ethernet.h>
6563670Snsayer#include <net/if.h>
66257176Sglebius#include <net/if_var.h>
67166497Sbms#include <net/if_clone.h>
68152315Sru#include <net/if_dl.h>
69238183Semaste#include <net/if_media.h>
70236725Strociny#include <net/if_types.h>
7163670Snsayer#include <net/route.h>
72236724Strociny#include <net/vnet.h>
7363670Snsayer
7463670Snsayer#include <netinet/in.h>
7563670Snsayer
7663670Snsayer#include <net/if_tapvar.h>
7763670Snsayer#include <net/if_tap.h>
7863670Snsayer
7963670Snsayer
8063670Snsayer#define CDEV_NAME	"tap"
8163670Snsayer#define TAPDEBUG	if (tapdebug) printf
8263670Snsayer
83241610Sglebiusstatic const char tapname[] = "tap";
84241610Sglebiusstatic const char vmnetname[] = "vmnet";
8583043Sbrooks#define TAPMAXUNIT	0x7fff
86126077Sphk#define VMNET_DEV_MASK	CLONE_FLAG0
8763670Snsayer
8863670Snsayer/* module */
89111742Sdesstatic int		tapmodevent(module_t, int, void *);
9063670Snsayer
9163670Snsayer/* device */
92148868Srwatsonstatic void		tapclone(void *, struct ucred *, char *, int,
93148868Srwatson			    struct cdev **);
94130585Sphkstatic void		tapcreate(struct cdev *);
9563670Snsayer
9663670Snsayer/* network interface */
9793084Sbdestatic void		tapifstart(struct ifnet *);
9893084Sbdestatic int		tapifioctl(struct ifnet *, u_long, caddr_t);
9993084Sbdestatic void		tapifinit(void *);
10063670Snsayer
101166497Sbmsstatic int		tap_clone_create(struct if_clone *, int, caddr_t);
102166497Sbmsstatic void		tap_clone_destroy(struct ifnet *);
103241610Sglebiusstatic struct if_clone *tap_cloner;
104166497Sbmsstatic int		vmnet_clone_create(struct if_clone *, int, caddr_t);
105166497Sbmsstatic void		vmnet_clone_destroy(struct ifnet *);
106241610Sglebiusstatic struct if_clone *vmnet_cloner;
107166497Sbms
10863670Snsayer/* character device */
10963670Snsayerstatic d_open_t		tapopen;
11063670Snsayerstatic d_close_t	tapclose;
11163670Snsayerstatic d_read_t		tapread;
11263670Snsayerstatic d_write_t	tapwrite;
11363670Snsayerstatic d_ioctl_t	tapioctl;
11463670Snsayerstatic d_poll_t		tappoll;
115156783Semaxstatic d_kqfilter_t	tapkqfilter;
11663670Snsayer
117156783Semax/* kqueue(2) */
118156783Semaxstatic int		tapkqread(struct knote *, long);
119156783Semaxstatic int		tapkqwrite(struct knote *, long);
120156783Semaxstatic void		tapkqdetach(struct knote *);
121156783Semax
122156783Semaxstatic struct filterops	tap_read_filterops = {
123156783Semax	.f_isfd =	1,
124156783Semax	.f_attach =	NULL,
125156783Semax	.f_detach =	tapkqdetach,
126156783Semax	.f_event =	tapkqread,
127156783Semax};
128156783Semax
129156783Semaxstatic struct filterops	tap_write_filterops = {
130156783Semax	.f_isfd =	1,
131156783Semax	.f_attach =	NULL,
132156783Semax	.f_detach =	tapkqdetach,
133156783Semax	.f_event =	tapkqwrite,
134156783Semax};
135156783Semax
13663670Snsayerstatic struct cdevsw	tap_cdevsw = {
137126080Sphk	.d_version =	D_VERSION,
138226500Sed	.d_flags =	D_NEEDMINOR,
139111815Sphk	.d_open =	tapopen,
140111815Sphk	.d_close =	tapclose,
141111815Sphk	.d_read =	tapread,
142111815Sphk	.d_write =	tapwrite,
143111815Sphk	.d_ioctl =	tapioctl,
144111815Sphk	.d_poll =	tappoll,
145111815Sphk	.d_name =	CDEV_NAME,
146156783Semax	.d_kqfilter =	tapkqfilter,
14763670Snsayer};
14863670Snsayer
149127003Srwatson/*
150127003Srwatson * All global variables in if_tap.c are locked with tapmtx, with the
151127003Srwatson * exception of tapdebug, which is accessed unlocked; tapclones is
152127003Srwatson * static at runtime.
153127003Srwatson */
154127003Srwatsonstatic struct mtx		tapmtx;
15583043Sbrooksstatic int			tapdebug = 0;        /* debug flag   */
156167713Sbmsstatic int			tapuopen = 0;        /* allow user open() */
157167713Sbmsstatic int			tapuponopen = 0;    /* IFF_UP on open() */
158166497Sbmsstatic int			tapdclone = 1;	/* enable devfs cloning */
15983043Sbrooksstatic SLIST_HEAD(, tap_softc)	taphead;             /* first device */
160126077Sphkstatic struct clonedevs 	*tapclones;
16163670Snsayer
16263670SnsayerMALLOC_DECLARE(M_TAP);
16363670SnsayerMALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
16463670SnsayerSYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
165144979Smdodd
166144979SmdoddSYSCTL_DECL(_net_link);
167227309Sedstatic SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
168144979Smdodd    "Ethernet tunnel software network interface");
169144979SmdoddSYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
170144979Smdodd	"Allow user to open /dev/tap (based on node permissions)");
171167713SbmsSYSCTL_INT(_net_link_tap, OID_AUTO, up_on_open, CTLFLAG_RW, &tapuponopen, 0,
172167713Sbms	"Bring interface up when /dev/tap is opened");
173166497SbmsSYSCTL_INT(_net_link_tap, OID_AUTO, devfs_cloning, CTLFLAG_RW, &tapdclone, 0,
174166497Sbms	"Enably legacy devfs interface creation");
175144979SmdoddSYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
176144979Smdodd
177166497SbmsTUNABLE_INT("net.link.tap.devfs_cloning", &tapdclone);
178166497Sbms
17963670SnsayerDEV_MODULE(if_tap, tapmodevent, NULL);
18063670Snsayer
181166497Sbmsstatic int
182166497Sbmstap_clone_create(struct if_clone *ifc, int unit, caddr_t params)
183166497Sbms{
184166497Sbms	struct cdev *dev;
185166497Sbms	int i;
186166497Sbms
187241610Sglebius	/* Find any existing device, or allocate new unit number. */
188241610Sglebius	i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, 0);
189166497Sbms	if (i) {
190243615Sdavidxu		dev = make_dev(&tap_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600,
191241610Sglebius		    "%s%d", tapname, unit);
192166497Sbms	}
193166497Sbms
194166497Sbms	tapcreate(dev);
195166497Sbms	return (0);
196166497Sbms}
197166497Sbms
198166497Sbms/* vmnet devices are tap devices in disguise */
199166497Sbmsstatic int
200166497Sbmsvmnet_clone_create(struct if_clone *ifc, int unit, caddr_t params)
201166497Sbms{
202241610Sglebius	struct cdev *dev;
203241610Sglebius	int i;
204241610Sglebius
205241610Sglebius	/* Find any existing device, or allocate new unit number. */
206241610Sglebius	i = clone_create(&tapclones, &tap_cdevsw, &unit, &dev, VMNET_DEV_MASK);
207241610Sglebius	if (i) {
208241610Sglebius		dev = make_dev(&tap_cdevsw, unit | VMNET_DEV_MASK, UID_ROOT,
209257078Sgrehan		    GID_WHEEL, 0600, "%s%d", vmnetname, unit);
210241610Sglebius	}
211241610Sglebius
212241610Sglebius	tapcreate(dev);
213241610Sglebius	return (0);
214166497Sbms}
215166497Sbms
216166497Sbmsstatic void
217166497Sbmstap_destroy(struct tap_softc *tp)
218166497Sbms{
219166497Sbms	struct ifnet *ifp = tp->tap_ifp;
220166497Sbms
221236724Strociny	CURVNET_SET(ifp->if_vnet);
222240938Semaste	destroy_dev(tp->tap_dev);
223225177Sattilio	seldrain(&tp->tap_rsel);
224256008Sglebius	knlist_clear(&tp->tap_rsel.si_note, 0);
225166497Sbms	knlist_destroy(&tp->tap_rsel.si_note);
226166497Sbms	ether_ifdetach(ifp);
227227459Sbrooks	if_free(ifp);
228166497Sbms
229166497Sbms	mtx_destroy(&tp->tap_mtx);
230166497Sbms	free(tp, M_TAP);
231236724Strociny	CURVNET_RESTORE();
232166497Sbms}
233166497Sbms
234166497Sbmsstatic void
235166497Sbmstap_clone_destroy(struct ifnet *ifp)
236166497Sbms{
237166497Sbms	struct tap_softc *tp = ifp->if_softc;
238166497Sbms
239166497Sbms	mtx_lock(&tapmtx);
240166497Sbms	SLIST_REMOVE(&taphead, tp, tap_softc, tap_next);
241166497Sbms	mtx_unlock(&tapmtx);
242166497Sbms	tap_destroy(tp);
243166497Sbms}
244166497Sbms
245166497Sbms/* vmnet devices are tap devices in disguise */
246166497Sbmsstatic void
247166497Sbmsvmnet_clone_destroy(struct ifnet *ifp)
248166497Sbms{
249166497Sbms	tap_clone_destroy(ifp);
250166497Sbms}
251166497Sbms
25263670Snsayer/*
25363670Snsayer * tapmodevent
25463670Snsayer *
25563670Snsayer * module event handler
25663670Snsayer */
25763670Snsayerstatic int
258156783Semaxtapmodevent(module_t mod, int type, void *data)
25963670Snsayer{
26083043Sbrooks	static eventhandler_tag	 eh_tag = NULL;
26183043Sbrooks	struct tap_softc	*tp = NULL;
26283043Sbrooks	struct ifnet		*ifp = NULL;
26363670Snsayer
26463670Snsayer	switch (type) {
26563670Snsayer	case MOD_LOAD:
26663670Snsayer
26783043Sbrooks		/* intitialize device */
26883043Sbrooks
269127003Srwatson		mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
27083043Sbrooks		SLIST_INIT(&taphead);
27183043Sbrooks
272126845Sphk		clone_setup(&tapclones);
27371602Sphk		eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
274127003Srwatson		if (eh_tag == NULL) {
275127170Srwatson			clone_cleanup(&tapclones);
276127003Srwatson			mtx_destroy(&tapmtx);
277126077Sphk			return (ENOMEM);
278127003Srwatson		}
279241610Sglebius		tap_cloner = if_clone_simple(tapname, tap_clone_create,
280241610Sglebius		    tap_clone_destroy, 0);
281241610Sglebius		vmnet_cloner = if_clone_simple(vmnetname, vmnet_clone_create,
282241610Sglebius		    vmnet_clone_destroy, 0);
28383043Sbrooks		return (0);
28463670Snsayer
28583043Sbrooks	case MOD_UNLOAD:
286127003Srwatson		/*
287127003Srwatson		 * The EBUSY algorithm here can't quite atomically
288127003Srwatson		 * guarantee that this is race-free since we have to
289127003Srwatson		 * release the tap mtx to deregister the clone handler.
290127003Srwatson		 */
291127003Srwatson		mtx_lock(&tapmtx);
292127003Srwatson		SLIST_FOREACH(tp, &taphead, tap_next) {
293127098Srwatson			mtx_lock(&tp->tap_mtx);
294127003Srwatson			if (tp->tap_flags & TAP_OPEN) {
295127098Srwatson				mtx_unlock(&tp->tap_mtx);
296127003Srwatson				mtx_unlock(&tapmtx);
29783043Sbrooks				return (EBUSY);
298127003Srwatson			}
299127098Srwatson			mtx_unlock(&tp->tap_mtx);
300127003Srwatson		}
301127003Srwatson		mtx_unlock(&tapmtx);
30283043Sbrooks
30371602Sphk		EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
304241610Sglebius		if_clone_detach(tap_cloner);
305241610Sglebius		if_clone_detach(vmnet_cloner);
306204464Skib		drain_dev_clone_events();
30763670Snsayer
308127003Srwatson		mtx_lock(&tapmtx);
30983043Sbrooks		while ((tp = SLIST_FIRST(&taphead)) != NULL) {
31083043Sbrooks			SLIST_REMOVE_HEAD(&taphead, tap_next);
311127003Srwatson			mtx_unlock(&tapmtx);
31283043Sbrooks
313147256Sbrooks			ifp = tp->tap_ifp;
31483043Sbrooks
315121816Sbrooks			TAPDEBUG("detaching %s\n", ifp->if_xname);
31683043Sbrooks
317166497Sbms			tap_destroy(tp);
318127003Srwatson			mtx_lock(&tapmtx);
31983043Sbrooks		}
320127003Srwatson		mtx_unlock(&tapmtx);
321126077Sphk		clone_cleanup(&tapclones);
32263670Snsayer
323135354Srwatson		mtx_destroy(&tapmtx);
324135354Srwatson
32583043Sbrooks		break;
32663670Snsayer
32763670Snsayer	default:
32863670Snsayer		return (EOPNOTSUPP);
32963670Snsayer	}
33063670Snsayer
33163670Snsayer	return (0);
33263670Snsayer} /* tapmodevent */
33363670Snsayer
33463670Snsayer
33563670Snsayer/*
33671602Sphk * DEVFS handler
33771602Sphk *
33871602Sphk * We need to support two kind of devices - tap and vmnet
33971602Sphk */
34071602Sphkstatic void
341156783Semaxtapclone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
34271602Sphk{
343166497Sbms	char		devname[SPECNAMELEN + 1];
344166497Sbms	int		i, unit, append_unit;
345166438Sbms	int		extra;
34671602Sphk
347130640Sphk	if (*dev != NULL)
34871602Sphk		return;
34971602Sphk
350166514Sbms	if (!tapdclone ||
351166514Sbms	    (!tapuopen && priv_check_cred(cred, PRIV_NET_IFCREATE, 0) != 0))
352166497Sbms		return;
353166497Sbms
354166497Sbms	unit = 0;
355166497Sbms	append_unit = 0;
356126077Sphk	extra = 0;
357166497Sbms
358166497Sbms	/* We're interested in only tap/vmnet devices. */
359241610Sglebius	if (strcmp(name, tapname) == 0) {
360126077Sphk		unit = -1;
361241610Sglebius	} else if (strcmp(name, vmnetname) == 0) {
362126077Sphk		unit = -1;
363126077Sphk		extra = VMNET_DEV_MASK;
364241610Sglebius	} else if (dev_stdclone(name, NULL, tapname, &unit) != 1) {
365241610Sglebius		if (dev_stdclone(name, NULL, vmnetname, &unit) != 1) {
366126077Sphk			return;
367166497Sbms		} else {
368166497Sbms			extra = VMNET_DEV_MASK;
369166497Sbms		}
37083043Sbrooks	}
37171602Sphk
372166497Sbms	if (unit == -1)
373166497Sbms		append_unit = 1;
374166497Sbms
375236724Strociny	CURVNET_SET(CRED_TO_VNET(cred));
376126077Sphk	/* find any existing device, or allocate new unit number */
377126077Sphk	i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
378126077Sphk	if (i) {
379166497Sbms		if (append_unit) {
380166497Sbms			/*
381166497Sbms			 * We were passed 'tun' or 'tap', with no unit specified
382166497Sbms			 * so we'll need to append it now.
383166497Sbms			 */
384166497Sbms			namelen = snprintf(devname, sizeof(devname), "%s%d", name,
385166497Sbms			    unit);
386166497Sbms			name = devname;
387166497Sbms		}
388166497Sbms
389204464Skib		*dev = make_dev_credf(MAKEDEV_REF, &tap_cdevsw, unit | extra,
390204464Skib		     cred, UID_ROOT, GID_WHEEL, 0600, "%s", name);
39171602Sphk	}
392166497Sbms
393166497Sbms	if_clone_create(name, namelen, NULL);
394236724Strociny	CURVNET_RESTORE();
39571602Sphk} /* tapclone */
39671602Sphk
39771602Sphk
39871602Sphk/*
39963670Snsayer * tapcreate
40063670Snsayer *
40163670Snsayer * to create interface
40263670Snsayer */
40363670Snsayerstatic void
404156783Semaxtapcreate(struct cdev *dev)
40563670Snsayer{
40663670Snsayer	struct ifnet		*ifp = NULL;
40763670Snsayer	struct tap_softc	*tp = NULL;
40863670Snsayer	unsigned short		 macaddr_hi;
409178221Semax	uint32_t		 macaddr_mid;
410213028Sjhb	int			 unit;
411241610Sglebius	const char		*name = NULL;
412147256Sbrooks	u_char			eaddr[6];
41363670Snsayer
41463670Snsayer	/* allocate driver storage and create device */
415184205Sdes	tp = malloc(sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
416127098Srwatson	mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
417127003Srwatson	mtx_lock(&tapmtx);
41883043Sbrooks	SLIST_INSERT_HEAD(&taphead, tp, tap_next);
419127003Srwatson	mtx_unlock(&tapmtx);
42063670Snsayer
421126796Sphk	unit = dev2unit(dev);
42283043Sbrooks
42363670Snsayer	/* select device: tap or vmnet */
424126796Sphk	if (unit & VMNET_DEV_MASK) {
425241610Sglebius		name = vmnetname;
42663803Snsayer		tp->tap_flags |= TAP_VMNET;
42783043Sbrooks	} else
428241610Sglebius		name = tapname;
42963670Snsayer
430126796Sphk	unit &= TAPMAXUNIT;
431126796Sphk
432183397Sed	TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, dev2unit(dev));
43383043Sbrooks
43463670Snsayer	/* generate fake MAC address: 00 bd xx xx xx unit_no */
43563670Snsayer	macaddr_hi = htons(0x00bd);
436178221Semax	macaddr_mid = (uint32_t) ticks;
437147256Sbrooks	bcopy(&macaddr_hi, eaddr, sizeof(short));
438178221Semax	bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
439147256Sbrooks	eaddr[5] = (u_char)unit;
44063670Snsayer
441111742Sdes	/* fill the rest and attach interface */
442147256Sbrooks	ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
443147256Sbrooks	if (ifp == NULL)
444147256Sbrooks		panic("%s%d: can not if_alloc()", name, unit);
44563670Snsayer	ifp->if_softc = tp;
446121816Sbrooks	if_initname(ifp, name, unit);
44763670Snsayer	ifp->if_init = tapifinit;
44863670Snsayer	ifp->if_start = tapifstart;
44963670Snsayer	ifp->if_ioctl = tapifioctl;
45063670Snsayer	ifp->if_mtu = ETHERMTU;
45163670Snsayer	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
452213028Sjhb	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
453205222Sqingli	ifp->if_capabilities |= IFCAP_LINKSTATE;
454205222Sqingli	ifp->if_capenable |= IFCAP_LINKSTATE;
45563670Snsayer
45683043Sbrooks	dev->si_drv1 = tp;
457126077Sphk	tp->tap_dev = dev;
45883043Sbrooks
459147256Sbrooks	ether_ifattach(ifp, eaddr);
46063670Snsayer
461127098Srwatson	mtx_lock(&tp->tap_mtx);
46263803Snsayer	tp->tap_flags |= TAP_INITED;
463127098Srwatson	mtx_unlock(&tp->tap_mtx);
46463803Snsayer
465213028Sjhb	knlist_init_mtx(&tp->tap_rsel.si_note, &tp->tap_mtx);
466158697Semax
467121816Sbrooks	TAPDEBUG("interface %s is created. minor = %#x\n",
468183397Sed		ifp->if_xname, dev2unit(dev));
46963670Snsayer} /* tapcreate */
47063670Snsayer
47163670Snsayer
47263670Snsayer/*
473111742Sdes * tapopen
47463670Snsayer *
47563670Snsayer * to open tunnel. must be superuser
47663670Snsayer */
47763670Snsayerstatic int
478156783Semaxtapopen(struct cdev *dev, int flag, int mode, struct thread *td)
47963670Snsayer{
48063670Snsayer	struct tap_softc	*tp = NULL;
481133460Semax	struct ifnet		*ifp = NULL;
482213028Sjhb	int			 error;
48363670Snsayer
484164033Srwatson	if (tapuopen == 0) {
485164033Srwatson		error = priv_check(td, PRIV_NET_TAP);
486164033Srwatson		if (error)
487164033Srwatson			return (error);
488164033Srwatson	}
48963670Snsayer
490126796Sphk	if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
491126796Sphk		return (ENXIO);
49283043Sbrooks
49363670Snsayer	tp = dev->si_drv1;
49463670Snsayer
495127165Srwatson	mtx_lock(&tp->tap_mtx);
496127165Srwatson	if (tp->tap_flags & TAP_OPEN) {
497127165Srwatson		mtx_unlock(&tp->tap_mtx);
498127165Srwatson		return (EBUSY);
499127165Srwatson	}
50063670Snsayer
501152315Sru	bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
50283366Sjulian	tp->tap_pid = td->td_proc->p_pid;
50363670Snsayer	tp->tap_flags |= TAP_OPEN;
504147256Sbrooks	ifp = tp->tap_ifp;
50563670Snsayer
506148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
507148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
508167713Sbms	if (tapuponopen)
509167713Sbms		ifp->if_flags |= IFF_UP;
510205024Sqingli	if_link_state_change(ifp, LINK_STATE_UP);
511213028Sjhb	mtx_unlock(&tp->tap_mtx);
51263670Snsayer
513183397Sed	TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, dev2unit(dev));
514133460Semax
51563670Snsayer	return (0);
51663670Snsayer} /* tapopen */
51763670Snsayer
51863670Snsayer
51963670Snsayer/*
52063670Snsayer * tapclose
52163670Snsayer *
52263670Snsayer * close the device - mark i/f down & delete routing info
52363670Snsayer */
52463670Snsayerstatic int
525156783Semaxtapclose(struct cdev *dev, int foo, int bar, struct thread *td)
52663670Snsayer{
527156783Semax	struct ifaddr		*ifa;
52863670Snsayer	struct tap_softc	*tp = dev->si_drv1;
529147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
53063670Snsayer
53163670Snsayer	/* junk all pending output */
532213028Sjhb	mtx_lock(&tp->tap_mtx);
533236724Strociny	CURVNET_SET(ifp->if_vnet);
53483043Sbrooks	IF_DRAIN(&ifp->if_snd);
53563670Snsayer
53663803Snsayer	/*
53763803Snsayer	 * do not bring the interface down, and do not anything with
53863803Snsayer	 * interface, if we are in VMnet mode. just close the device.
53963803Snsayer	 */
54063803Snsayer
54163803Snsayer	if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
542127098Srwatson		mtx_unlock(&tp->tap_mtx);
54363670Snsayer		if_down(ifp);
544213028Sjhb		mtx_lock(&tp->tap_mtx);
545148887Srwatson		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
546213028Sjhb			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
547213028Sjhb			mtx_unlock(&tp->tap_mtx);
54863803Snsayer			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
549146620Speadar				rtinit(ifa, (int)RTM_DELETE, 0);
55063670Snsayer			}
551146620Speadar			if_purgeaddrs(ifp);
552213028Sjhb			mtx_lock(&tp->tap_mtx);
55363670Snsayer		}
554213028Sjhb	}
55563670Snsayer
556205024Sqingli	if_link_state_change(ifp, LINK_STATE_DOWN);
557236724Strociny	CURVNET_RESTORE();
558236724Strociny
55996122Salfred	funsetown(&tp->tap_sigio);
560122352Stanimura	selwakeuppri(&tp->tap_rsel, PZERO+1);
561213028Sjhb	KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
56263670Snsayer
56363670Snsayer	tp->tap_flags &= ~TAP_OPEN;
56463670Snsayer	tp->tap_pid = 0;
565127098Srwatson	mtx_unlock(&tp->tap_mtx);
56663670Snsayer
567121816Sbrooks	TAPDEBUG("%s is closed. minor = %#x\n",
568183397Sed		ifp->if_xname, dev2unit(dev));
56963670Snsayer
57063670Snsayer	return (0);
57163670Snsayer} /* tapclose */
57263670Snsayer
57363670Snsayer
57463670Snsayer/*
57563670Snsayer * tapifinit
57663670Snsayer *
57763670Snsayer * network interface initialization function
57863670Snsayer */
57963670Snsayerstatic void
580156783Semaxtapifinit(void *xtp)
58163670Snsayer{
58263670Snsayer	struct tap_softc	*tp = (struct tap_softc *)xtp;
583147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
58463670Snsayer
585121816Sbrooks	TAPDEBUG("initializing %s\n", ifp->if_xname);
58663670Snsayer
587213028Sjhb	mtx_lock(&tp->tap_mtx);
588148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
589148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
590213028Sjhb	mtx_unlock(&tp->tap_mtx);
59163670Snsayer
59263670Snsayer	/* attempt to start output */
59363670Snsayer	tapifstart(ifp);
59463670Snsayer} /* tapifinit */
59563670Snsayer
59663670Snsayer
59763670Snsayer/*
59863670Snsayer * tapifioctl
59963670Snsayer *
60063670Snsayer * Process an ioctl request on network interface
60163670Snsayer */
602105228Sphkstatic int
603156783Semaxtapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
60463670Snsayer{
605160376Sbrooks	struct tap_softc	*tp = ifp->if_softc;
606189866Sscf	struct ifreq		*ifr = (struct ifreq *)data;
60763670Snsayer	struct ifstat		*ifs = NULL;
608238183Semaste	struct ifmediareq	*ifmr = NULL;
609238183Semaste	int			 dummy, error = 0;
61063670Snsayer
61163670Snsayer	switch (cmd) {
61263670Snsayer		case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
61363670Snsayer		case SIOCADDMULTI:
61463670Snsayer		case SIOCDELMULTI:
61583043Sbrooks			break;
61663670Snsayer
617238183Semaste		case SIOCGIFMEDIA:
618238183Semaste			ifmr = (struct ifmediareq *)data;
619238183Semaste			dummy = ifmr->ifm_count;
620238183Semaste			ifmr->ifm_count = 1;
621238183Semaste			ifmr->ifm_status = IFM_AVALID;
622238183Semaste			ifmr->ifm_active = IFM_ETHER;
623238183Semaste			if (tp->tap_flags & TAP_OPEN)
624238183Semaste				ifmr->ifm_status |= IFM_ACTIVE;
625238183Semaste			ifmr->ifm_current = ifmr->ifm_active;
626238183Semaste			if (dummy >= 1) {
627238183Semaste				int media = IFM_ETHER;
628238183Semaste				error = copyout(&media, ifmr->ifm_ulist,
629238183Semaste				    sizeof(int));
630238183Semaste			}
631238183Semaste			break;
632238183Semaste
633189866Sscf		case SIOCSIFMTU:
634189866Sscf			ifp->if_mtu = ifr->ifr_mtu;
635189866Sscf			break;
636189866Sscf
63763670Snsayer		case SIOCGIFSTATUS:
63863670Snsayer			ifs = (struct ifstat *)data;
63963670Snsayer			dummy = strlen(ifs->ascii);
640127098Srwatson			mtx_lock(&tp->tap_mtx);
64163670Snsayer			if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
64263670Snsayer				snprintf(ifs->ascii + dummy,
64363670Snsayer					sizeof(ifs->ascii) - dummy,
64463670Snsayer					"\tOpened by PID %d\n", tp->tap_pid);
645127098Srwatson			mtx_unlock(&tp->tap_mtx);
64683043Sbrooks			break;
64763670Snsayer
64863670Snsayer		default:
649238183Semaste			error = ether_ioctl(ifp, cmd, data);
650238183Semaste			break;
65163670Snsayer	}
65263670Snsayer
653238183Semaste	return (error);
65463670Snsayer} /* tapifioctl */
65563670Snsayer
65663670Snsayer
65763670Snsayer/*
658111742Sdes * tapifstart
659111742Sdes *
66063670Snsayer * queue packets from higher level ready to put out
66163670Snsayer */
66263670Snsayerstatic void
663156783Semaxtapifstart(struct ifnet *ifp)
66463670Snsayer{
66563670Snsayer	struct tap_softc	*tp = ifp->if_softc;
66663670Snsayer
667121816Sbrooks	TAPDEBUG("%s starting\n", ifp->if_xname);
66863670Snsayer
66963803Snsayer	/*
67063803Snsayer	 * do not junk pending output if we are in VMnet mode.
67163803Snsayer	 * XXX: can this do any harm because of queue overflow?
67263803Snsayer	 */
67363803Snsayer
674127098Srwatson	mtx_lock(&tp->tap_mtx);
675111742Sdes	if (((tp->tap_flags & TAP_VMNET) == 0) &&
67663803Snsayer	    ((tp->tap_flags & TAP_READY) != TAP_READY)) {
677213028Sjhb		struct mbuf *m;
67863670Snsayer
679127098Srwatson		/* Unlocked read. */
680121816Sbrooks		TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
681121816Sbrooks		    tp->tap_flags);
68263670Snsayer
683213028Sjhb		for (;;) {
68463670Snsayer			IF_DEQUEUE(&ifp->if_snd, m);
685213028Sjhb			if (m != NULL) {
68663670Snsayer				m_freem(m);
687213028Sjhb				ifp->if_oerrors++;
688213028Sjhb			} else
689213028Sjhb				break;
690213028Sjhb		}
691213028Sjhb		mtx_unlock(&tp->tap_mtx);
69263670Snsayer
69363670Snsayer		return;
69463670Snsayer	}
69563670Snsayer
696148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
69763670Snsayer
698213028Sjhb	if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
69963670Snsayer		if (tp->tap_flags & TAP_RWAIT) {
70063670Snsayer			tp->tap_flags &= ~TAP_RWAIT;
701111748Sdes			wakeup(tp);
70263670Snsayer		}
70363670Snsayer
704127098Srwatson		if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
705127098Srwatson			mtx_unlock(&tp->tap_mtx);
70695883Salfred			pgsigio(&tp->tap_sigio, SIGIO, 0);
707213028Sjhb			mtx_lock(&tp->tap_mtx);
708213028Sjhb		}
70963670Snsayer
710122352Stanimura		selwakeuppri(&tp->tap_rsel, PZERO+1);
711213028Sjhb		KNOTE_LOCKED(&tp->tap_rsel.si_note, 0);
71263670Snsayer		ifp->if_opackets ++; /* obytes are counted in ether_output */
71363670Snsayer	}
71463670Snsayer
715148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
716213028Sjhb	mtx_unlock(&tp->tap_mtx);
71763670Snsayer} /* tapifstart */
71863670Snsayer
71963670Snsayer
72063670Snsayer/*
72163670Snsayer * tapioctl
72263670Snsayer *
72363670Snsayer * the cdevsw interface is now pretty minimal
72463670Snsayer */
72563670Snsayerstatic int
726156783Semaxtapioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
72763670Snsayer{
72863670Snsayer	struct tap_softc	*tp = dev->si_drv1;
729147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
730111742Sdes	struct tapinfo		*tapp = NULL;
731102052Ssobomax	int			 f;
732162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
733162711Sru    defined(COMPAT_FREEBSD4)
734162711Sru	int			 ival;
735162711Sru#endif
73663670Snsayer
73763670Snsayer	switch (cmd) {
738111742Sdes		case TAPSIFINFO:
739111742Sdes			tapp = (struct tapinfo *)data;
740213028Sjhb			mtx_lock(&tp->tap_mtx);
741111742Sdes			ifp->if_mtu = tapp->mtu;
742111742Sdes			ifp->if_type = tapp->type;
743111742Sdes			ifp->if_baudrate = tapp->baudrate;
744213028Sjhb			mtx_unlock(&tp->tap_mtx);
745111742Sdes			break;
74663670Snsayer
747111742Sdes		case TAPGIFINFO:
748111742Sdes			tapp = (struct tapinfo *)data;
749213028Sjhb			mtx_lock(&tp->tap_mtx);
750111742Sdes			tapp->mtu = ifp->if_mtu;
751111742Sdes			tapp->type = ifp->if_type;
752111742Sdes			tapp->baudrate = ifp->if_baudrate;
753213028Sjhb			mtx_unlock(&tp->tap_mtx);
754111742Sdes			break;
75563670Snsayer
75663670Snsayer		case TAPSDEBUG:
757159079Smarius			tapdebug = *(int *)data;
75883043Sbrooks			break;
75963670Snsayer
76063670Snsayer		case TAPGDEBUG:
761159079Smarius			*(int *)data = tapdebug;
76283043Sbrooks			break;
76363670Snsayer
764182880Semax		case TAPGIFNAME: {
765182880Semax			struct ifreq	*ifr = (struct ifreq *) data;
766182880Semax
767182880Semax			strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
768182880Semax			} break;
769182880Semax
77063670Snsayer		case FIONBIO:
77183043Sbrooks			break;
77263670Snsayer
77363670Snsayer		case FIOASYNC:
774127098Srwatson			mtx_lock(&tp->tap_mtx);
775159079Smarius			if (*(int *)data)
77663670Snsayer				tp->tap_flags |= TAP_ASYNC;
77763670Snsayer			else
77863670Snsayer				tp->tap_flags &= ~TAP_ASYNC;
779127098Srwatson			mtx_unlock(&tp->tap_mtx);
78083043Sbrooks			break;
78163670Snsayer
78263670Snsayer		case FIONREAD:
783213028Sjhb			if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
784213028Sjhb				struct mbuf *mb;
78563670Snsayer
786213028Sjhb				IFQ_LOCK(&ifp->if_snd);
787213028Sjhb				IFQ_POLL_NOLOCK(&ifp->if_snd, mb);
788213028Sjhb				for (*(int *)data = 0; mb != NULL;
789213028Sjhb				     mb = mb->m_next)
790159079Smarius					*(int *)data += mb->m_len;
791213028Sjhb				IFQ_UNLOCK(&ifp->if_snd);
79283043Sbrooks			} else
793159079Smarius				*(int *)data = 0;
79483043Sbrooks			break;
79563670Snsayer
79663670Snsayer		case FIOSETOWN:
797159079Smarius			return (fsetown(*(int *)data, &tp->tap_sigio));
79863670Snsayer
79963670Snsayer		case FIOGETOWN:
800159079Smarius			*(int *)data = fgetown(&tp->tap_sigio);
80163670Snsayer			return (0);
80263670Snsayer
80363670Snsayer		/* this is deprecated, FIOSETOWN should be used instead */
80463670Snsayer		case TIOCSPGRP:
805159079Smarius			return (fsetown(-(*(int *)data), &tp->tap_sigio));
80663670Snsayer
80763670Snsayer		/* this is deprecated, FIOGETOWN should be used instead */
80863670Snsayer		case TIOCGPGRP:
809159079Smarius			*(int *)data = -fgetown(&tp->tap_sigio);
81063670Snsayer			return (0);
81163670Snsayer
81263670Snsayer		/* VMware/VMnet port ioctl's */
81363670Snsayer
814162711Sru#if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
815162711Sru    defined(COMPAT_FREEBSD4)
816162711Sru		case _IO('V', 0):
817162711Sru			ival = IOCPARM_IVAL(data);
818162711Sru			data = (caddr_t)&ival;
819162711Sru			/* FALLTHROUGH */
820162711Sru#endif
82183043Sbrooks		case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
822162711Sru			f = *(int *)data;
82363670Snsayer			f &= 0x0fff;
82463670Snsayer			f &= ~IFF_CANTCHANGE;
82563670Snsayer			f |= IFF_UP;
82663670Snsayer
827213028Sjhb			mtx_lock(&tp->tap_mtx);
82863670Snsayer			ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
829213028Sjhb			mtx_unlock(&tp->tap_mtx);
83083043Sbrooks			break;
83163670Snsayer
832257696Sglebius		case SIOCGIFADDR:	/* get MAC address of the remote side */
833127165Srwatson			mtx_lock(&tp->tap_mtx);
83463861Snsayer			bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
835127165Srwatson			mtx_unlock(&tp->tap_mtx);
83683043Sbrooks			break;
83763670Snsayer
83863861Snsayer		case SIOCSIFADDR:	/* set MAC address of the remote side */
839127165Srwatson			mtx_lock(&tp->tap_mtx);
84063861Snsayer			bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
841127165Srwatson			mtx_unlock(&tp->tap_mtx);
84283043Sbrooks			break;
84363670Snsayer
84463670Snsayer		default:
84563670Snsayer			return (ENOTTY);
84663670Snsayer	}
84763670Snsayer	return (0);
84863670Snsayer} /* tapioctl */
84963670Snsayer
85063670Snsayer
85163670Snsayer/*
85263670Snsayer * tapread
85363670Snsayer *
85463670Snsayer * the cdevsw read interface - reads a packet at a time, or at
85563670Snsayer * least as much of a packet as can be read
85663670Snsayer */
85763670Snsayerstatic int
858156783Semaxtapread(struct cdev *dev, struct uio *uio, int flag)
85963670Snsayer{
86063670Snsayer	struct tap_softc	*tp = dev->si_drv1;
861147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
86290227Sdillon	struct mbuf		*m = NULL;
863213028Sjhb	int			 error = 0, len;
86463670Snsayer
865183397Sed	TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, dev2unit(dev));
86663670Snsayer
867127098Srwatson	mtx_lock(&tp->tap_mtx);
86863670Snsayer	if ((tp->tap_flags & TAP_READY) != TAP_READY) {
869127098Srwatson		mtx_unlock(&tp->tap_mtx);
870127098Srwatson
871127098Srwatson		/* Unlocked read. */
872121816Sbrooks		TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
873183397Sed			ifp->if_xname, dev2unit(dev), tp->tap_flags);
87463803Snsayer
87563670Snsayer		return (EHOSTDOWN);
87663670Snsayer	}
87763670Snsayer
87863670Snsayer	tp->tap_flags &= ~TAP_RWAIT;
87963670Snsayer
88063670Snsayer	/* sleep until we get a packet */
88163670Snsayer	do {
88290227Sdillon		IF_DEQUEUE(&ifp->if_snd, m);
88363670Snsayer
88490227Sdillon		if (m == NULL) {
885213028Sjhb			if (flag & O_NONBLOCK) {
886213028Sjhb				mtx_unlock(&tp->tap_mtx);
88763670Snsayer				return (EWOULDBLOCK);
888213028Sjhb			}
889111742Sdes
89063670Snsayer			tp->tap_flags |= TAP_RWAIT;
891213028Sjhb			error = mtx_sleep(tp, &tp->tap_mtx, PCATCH | (PZERO + 1),
892213028Sjhb			    "taprd", 0);
893213028Sjhb			if (error) {
894213028Sjhb				mtx_unlock(&tp->tap_mtx);
89563670Snsayer				return (error);
896213028Sjhb			}
89763670Snsayer		}
89890227Sdillon	} while (m == NULL);
899213028Sjhb	mtx_unlock(&tp->tap_mtx);
90063670Snsayer
90163670Snsayer	/* feed packet to bpf */
902106939Ssam	BPF_MTAP(ifp, m);
90363670Snsayer
90463670Snsayer	/* xfer packet to user space */
90590227Sdillon	while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
90690227Sdillon		len = min(uio->uio_resid, m->m_len);
90763670Snsayer		if (len == 0)
90863670Snsayer			break;
90963670Snsayer
910111741Sdes		error = uiomove(mtod(m, void *), len, uio);
91190227Sdillon		m = m_free(m);
91263670Snsayer	}
91363670Snsayer
91490227Sdillon	if (m != NULL) {
915121816Sbrooks		TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
916183397Sed			dev2unit(dev));
91790227Sdillon		m_freem(m);
91863670Snsayer	}
91963670Snsayer
92063670Snsayer	return (error);
92163670Snsayer} /* tapread */
92263670Snsayer
92363670Snsayer
92463670Snsayer/*
92563670Snsayer * tapwrite
92663670Snsayer *
92763670Snsayer * the cdevsw write interface - an atomic write is a packet - or else!
92863670Snsayer */
92963670Snsayerstatic int
930156783Semaxtapwrite(struct cdev *dev, struct uio *uio, int flag)
93163670Snsayer{
932166443Sbms	struct ether_header	*eh;
93363670Snsayer	struct tap_softc	*tp = dev->si_drv1;
934147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
935137101Sglebius	struct mbuf		*m;
93663670Snsayer
937240945Semaste	TAPDEBUG("%s writing, minor = %#x\n",
938183397Sed		ifp->if_xname, dev2unit(dev));
93963670Snsayer
94063670Snsayer	if (uio->uio_resid == 0)
94163670Snsayer		return (0);
94263670Snsayer
94363670Snsayer	if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
944194990Skib		TAPDEBUG("%s invalid packet len = %zd, minor = %#x\n",
945183397Sed			ifp->if_xname, uio->uio_resid, dev2unit(dev));
94663803Snsayer
94763670Snsayer		return (EIO);
94863670Snsayer	}
94963670Snsayer
950243882Sglebius	if ((m = m_uiotombuf(uio, M_NOWAIT, 0, ETHER_ALIGN,
951163915Sandre	    M_PKTHDR)) == NULL) {
95263670Snsayer		ifp->if_ierrors ++;
953163986Scsjp		return (ENOBUFS);
95463670Snsayer	}
95563670Snsayer
956137101Sglebius	m->m_pkthdr.rcvif = ifp;
957111742Sdes
958166443Sbms	/*
959166443Sbms	 * Only pass a unicast frame to ether_input(), if it would actually
960166443Sbms	 * have been received by non-virtual hardware.
961166443Sbms	 */
962166443Sbms	if (m->m_len < sizeof(struct ether_header)) {
963166443Sbms		m_freem(m);
964166443Sbms		return (0);
965166443Sbms	}
966166443Sbms	eh = mtod(m, struct ether_header *);
967166443Sbms
968166443Sbms	if (eh && (ifp->if_flags & IFF_PROMISC) == 0 &&
969166443Sbms	    !ETHER_IS_MULTICAST(eh->ether_dhost) &&
970166443Sbms	    bcmp(eh->ether_dhost, IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0) {
971166443Sbms		m_freem(m);
972166443Sbms		return (0);
973166443Sbms	}
974166443Sbms
975106939Ssam	/* Pass packet up to parent. */
976236724Strociny	CURVNET_SET(ifp->if_vnet);
977137101Sglebius	(*ifp->if_input)(ifp, m);
978236724Strociny	CURVNET_RESTORE();
979106939Ssam	ifp->if_ipackets ++; /* ibytes are counted in parent */
98063670Snsayer
98163670Snsayer	return (0);
98263670Snsayer} /* tapwrite */
98363670Snsayer
98463670Snsayer
98563670Snsayer/*
98663670Snsayer * tappoll
98763670Snsayer *
98863670Snsayer * the poll interface, this is only useful on reads
98963670Snsayer * really. the write detect always returns true, write never blocks
99063670Snsayer * anyway, it either accepts the packet or drops it
99163670Snsayer */
99263670Snsayerstatic int
993156783Semaxtappoll(struct cdev *dev, int events, struct thread *td)
99463670Snsayer{
99563670Snsayer	struct tap_softc	*tp = dev->si_drv1;
996147256Sbrooks	struct ifnet		*ifp = tp->tap_ifp;
997213028Sjhb	int			 revents = 0;
99863670Snsayer
999121816Sbrooks	TAPDEBUG("%s polling, minor = %#x\n",
1000183397Sed		ifp->if_xname, dev2unit(dev));
100163670Snsayer
100263670Snsayer	if (events & (POLLIN | POLLRDNORM)) {
1003213028Sjhb		IFQ_LOCK(&ifp->if_snd);
1004213028Sjhb		if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1005121816Sbrooks			TAPDEBUG("%s have data in queue. len = %d, " \
1006121816Sbrooks				"minor = %#x\n", ifp->if_xname,
1007183397Sed				ifp->if_snd.ifq_len, dev2unit(dev));
100863803Snsayer
100963670Snsayer			revents |= (events & (POLLIN | POLLRDNORM));
101083043Sbrooks		} else {
1011121816Sbrooks			TAPDEBUG("%s waiting for data, minor = %#x\n",
1012183397Sed				ifp->if_xname, dev2unit(dev));
101363803Snsayer
101483805Sjhb			selrecord(td, &tp->tap_rsel);
101563670Snsayer		}
1016213028Sjhb		IFQ_UNLOCK(&ifp->if_snd);
101763670Snsayer	}
101863670Snsayer
101963670Snsayer	if (events & (POLLOUT | POLLWRNORM))
102063670Snsayer		revents |= (events & (POLLOUT | POLLWRNORM));
102163670Snsayer
102263670Snsayer	return (revents);
102363670Snsayer} /* tappoll */
1024156783Semax
1025156783Semax
1026156783Semax/*
1027156783Semax * tap_kqfilter
1028156783Semax *
1029156783Semax * support for kevent() system call
1030156783Semax */
1031156783Semaxstatic int
1032156783Semaxtapkqfilter(struct cdev *dev, struct knote *kn)
1033156783Semax{
1034156783Semax	struct tap_softc	*tp = dev->si_drv1;
1035156783Semax	struct ifnet		*ifp = tp->tap_ifp;
1036156783Semax
1037156783Semax	switch (kn->kn_filter) {
1038156783Semax	case EVFILT_READ:
1039156783Semax		TAPDEBUG("%s kqfilter: EVFILT_READ, minor = %#x\n",
1040183397Sed			ifp->if_xname, dev2unit(dev));
1041156783Semax		kn->kn_fop = &tap_read_filterops;
1042156783Semax		break;
1043156783Semax
1044156783Semax	case EVFILT_WRITE:
1045156783Semax		TAPDEBUG("%s kqfilter: EVFILT_WRITE, minor = %#x\n",
1046183397Sed			ifp->if_xname, dev2unit(dev));
1047156783Semax		kn->kn_fop = &tap_write_filterops;
1048156783Semax		break;
1049156783Semax
1050156783Semax	default:
1051156783Semax		TAPDEBUG("%s kqfilter: invalid filter, minor = %#x\n",
1052183397Sed			ifp->if_xname, dev2unit(dev));
1053156783Semax		return (EINVAL);
1054156783Semax		/* NOT REACHED */
1055156783Semax	}
1056156783Semax
1057213028Sjhb	kn->kn_hook = tp;
1058156783Semax	knlist_add(&tp->tap_rsel.si_note, kn, 0);
1059156783Semax
1060156783Semax	return (0);
1061156783Semax} /* tapkqfilter */
1062156783Semax
1063156783Semax
1064156783Semax/*
1065156783Semax * tap_kqread
1066156783Semax *
1067156783Semax * Return true if there is data in the interface queue
1068156783Semax */
1069156783Semaxstatic int
1070156783Semaxtapkqread(struct knote *kn, long hint)
1071156783Semax{
1072213028Sjhb	int			 ret;
1073213028Sjhb	struct tap_softc	*tp = kn->kn_hook;
1074213028Sjhb	struct cdev		*dev = tp->tap_dev;
1075156783Semax	struct ifnet		*ifp = tp->tap_ifp;
1076156783Semax
1077156783Semax	if ((kn->kn_data = ifp->if_snd.ifq_len) > 0) {
1078156783Semax		TAPDEBUG("%s have data in queue. len = %d, minor = %#x\n",
1079183397Sed			ifp->if_xname, ifp->if_snd.ifq_len, dev2unit(dev));
1080156783Semax		ret = 1;
1081156783Semax	} else {
1082156783Semax		TAPDEBUG("%s waiting for data, minor = %#x\n",
1083183397Sed			ifp->if_xname, dev2unit(dev));
1084156783Semax		ret = 0;
1085156783Semax	}
1086156783Semax
1087156783Semax	return (ret);
1088156783Semax} /* tapkqread */
1089156783Semax
1090156783Semax
1091156783Semax/*
1092156783Semax * tap_kqwrite
1093156783Semax *
1094156783Semax * Always can write. Return the MTU in kn->data
1095156783Semax */
1096156783Semaxstatic int
1097156783Semaxtapkqwrite(struct knote *kn, long hint)
1098156783Semax{
1099213028Sjhb	struct tap_softc	*tp = kn->kn_hook;
1100156783Semax	struct ifnet		*ifp = tp->tap_ifp;
1101156783Semax
1102156783Semax	kn->kn_data = ifp->if_mtu;
1103156783Semax
1104156783Semax	return (1);
1105156783Semax} /* tapkqwrite */
1106156783Semax
1107156783Semax
1108156783Semaxstatic void
1109156783Semaxtapkqdetach(struct knote *kn)
1110156783Semax{
1111213028Sjhb	struct tap_softc	*tp = kn->kn_hook;
1112156783Semax
1113156783Semax	knlist_remove(&tp->tap_rsel.si_note, kn, 0);
1114156783Semax} /* tapkqdetach */
1115156783Semax
1116