xref: /dragonfly/sys/net/tap/if_tap.c (revision 1847e88f)
1 /*
2  * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * BASED ON:
27  * -------------------------------------------------------------------------
28  *
29  * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30  * Nottingham University 1987.
31  */
32 
33 /*
34  * $FreeBSD: src/sys/net/if_tap.c,v 1.3.2.3 2002/04/14 21:41:48 luigi Exp $
35  * $DragonFly: src/sys/net/tap/if_tap.c,v 1.24 2005/12/30 18:37:09 dillon Exp $
36  * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
37  */
38 
39 #include "opt_inet.h"
40 
41 #include <sys/param.h>
42 #include <sys/conf.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 #include <sys/thread2.h>
56 #include <sys/ttycom.h>
57 #include <sys/uio.h>
58 #include <sys/vnode.h>
59 #include <sys/serialize.h>
60 
61 #include <net/bpf.h>
62 #include <net/ethernet.h>
63 #include <net/if.h>
64 #include <net/ifq_var.h>
65 #include <net/if_arp.h>
66 #include <net/route.h>
67 
68 #include <netinet/in.h>
69 
70 #include "if_tapvar.h"
71 #include "if_tap.h"
72 
73 
74 #define CDEV_NAME	"tap"
75 #define CDEV_MAJOR	149
76 #define TAPDEBUG	if (tapdebug) if_printf
77 
78 #define TAP		"tap"
79 #define VMNET		"vmnet"
80 #define VMNET_DEV_MASK	0x00010000
81 
82 /* module */
83 static int 		tapmodevent	(module_t, int, void *);
84 
85 /* device */
86 static void		tapcreate	(dev_t);
87 
88 /* network interface */
89 static void		tapifstart	(struct ifnet *);
90 static int		tapifioctl	(struct ifnet *, u_long, caddr_t,
91 					 struct ucred *);
92 static void		tapifinit	(void *);
93 
94 /* character device */
95 static d_open_t		tapopen;
96 static d_close_t	tapclose;
97 static d_read_t		tapread;
98 static d_write_t	tapwrite;
99 static d_ioctl_t	tapioctl;
100 static d_poll_t		tappoll;
101 
102 static struct cdevsw	tap_cdevsw = {
103 	/* dev name */	CDEV_NAME,
104 	/* dev major */	CDEV_MAJOR,
105 	/* flags */	0,
106 	/* port */	NULL,
107 	/* clone */	NULL,
108 
109 	/* open */	tapopen,
110 	/* close */	tapclose,
111 	/* read */	tapread,
112 	/* write */	tapwrite,
113 	/* ioctl */	tapioctl,
114 	/* poll */	tappoll,
115 	/* mmap */	nommap,
116 	/* startegy */	nostrategy,
117 	/* dump */	nodump,
118 	/* psize */	nopsize
119 };
120 
121 static int		taprefcnt = 0;		/* module ref. counter   */
122 static int		taplastunit = -1;	/* max. open unit number */
123 static int		tapdebug = 0;		/* debug flag            */
124 
125 MALLOC_DECLARE(M_TAP);
126 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
127 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
128 DEV_MODULE(if_tap, tapmodevent, NULL);
129 
130 /*
131  * tapmodevent
132  *
133  * module event handler
134  */
135 static int
136 tapmodevent(module_t mod, int type, void *data)
137 {
138 	static int		 attached = 0;
139 	struct ifnet		*ifp = NULL;
140 	int			 unit;
141 
142 	switch (type) {
143 	case MOD_LOAD:
144 		if (attached)
145 			return (EEXIST);
146 
147 		cdevsw_add(&tap_cdevsw, 0, 0);
148 		attached = 1;
149 	break;
150 
151 	case MOD_UNLOAD:
152 		if (taprefcnt > 0)
153 			return (EBUSY);
154 
155 		cdevsw_remove(&tap_cdevsw, 0, 0);
156 
157 		/* XXX: maintain tap ifs in a local list */
158 		unit = 0;
159 		while (unit <= taplastunit) {
160 			TAILQ_FOREACH(ifp, &ifnet, if_link) {
161 				if ((strcmp(ifp->if_dname, TAP) == 0) ||
162 				    (strcmp(ifp->if_dname, VMNET) == 0)) {
163 					if (ifp->if_dunit == unit)
164 						break;
165 				}
166 			}
167 
168 			if (ifp != NULL) {
169 				struct tap_softc	*tp = ifp->if_softc;
170 
171 				TAPDEBUG(ifp, "detached. minor = %#x, " \
172 					"taplastunit = %d\n",
173 					minor(tp->tap_dev), taplastunit);
174 
175 				ether_ifdetach(ifp);
176 				destroy_dev(tp->tap_dev);
177 				free(tp, M_TAP);
178 			}
179 			else
180 				unit ++;
181 		}
182 
183 		attached = 0;
184 	break;
185 
186 	default:
187 		return (EOPNOTSUPP);
188 	}
189 
190 	return (0);
191 } /* tapmodevent */
192 
193 
194 /*
195  * tapcreate
196  *
197  * to create interface
198  */
199 static void
200 tapcreate(dev_t dev)
201 {
202 	struct ifnet		*ifp = NULL;
203 	struct tap_softc	*tp = NULL;
204 	uint8_t			ether_addr[ETHER_ADDR_LEN];
205 	int			 unit;
206 	char			*name = NULL;
207 
208 	/* allocate driver storage and create device */
209 	MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
210 	bzero(tp, sizeof(*tp));
211 
212 	/* select device: tap or vmnet */
213 	if (minor(dev) & VMNET_DEV_MASK) {
214 		name = VMNET;
215 		unit = lminor(dev) & 0xff;
216 		tp->tap_flags |= TAP_VMNET;
217 	}
218 	else {
219 		name = TAP;
220 		unit = lminor(dev);
221 	}
222 
223 	tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
224 						0600, "%s%d", name, unit);
225 	tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
226 	reference_dev(tp->tap_dev);	/* so we can destroy it later */
227 
228 	/* generate fake MAC address: 00 bd xx xx xx unit_no */
229 	ether_addr[0] = 0x00;
230 	ether_addr[1] = 0xbd;
231 	bcopy(&ticks, ether_addr, 4);
232 	ether_addr[5] = (u_char)unit;
233 
234 	/* fill the rest and attach interface */
235 	ifp = &tp->tap_if;
236 	ifp->if_softc = tp;
237 
238 	if_initname(ifp, name, unit);
239 	if (unit > taplastunit)
240 		taplastunit = unit;
241 
242 	ifp->if_init = tapifinit;
243 	ifp->if_start = tapifstart;
244 	ifp->if_ioctl = tapifioctl;
245 	ifp->if_mtu = ETHERMTU;
246 	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
247 	ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
248 	ifq_set_ready(&ifp->if_snd);
249 
250 	ether_ifattach(ifp, ether_addr, NULL);
251 
252 	tp->tap_flags |= TAP_INITED;
253 
254 	TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
255 } /* tapcreate */
256 
257 
258 /*
259  * tapopen
260  *
261  * to open tunnel. must be superuser
262  */
263 static int
264 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
265 {
266 	struct tap_softc	*tp = NULL;
267 	int			 error;
268 
269 	if ((error = suser(td)) != 0)
270 		return (error);
271 
272 	tp = dev->si_drv1;
273 	if (tp == NULL) {
274 		tapcreate(dev);
275 		tp = dev->si_drv1;
276 	}
277 
278 	if (tp->tap_flags & TAP_OPEN)
279 		return (EBUSY);
280 
281 	bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
282 
283 	tp->tap_td = td;
284 	tp->tap_flags |= TAP_OPEN;
285 	taprefcnt ++;
286 
287 	TAPDEBUG(&tp->arpcom.ac_if,
288 		 "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
289 		 minor(tp->tap_dev), taprefcnt, taplastunit);
290 
291 	return (0);
292 } /* tapopen */
293 
294 
295 /*
296  * tapclose
297  *
298  * close the device - mark i/f down & delete routing info
299  */
300 static int
301 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
302 {
303 	struct tap_softc	*tp = dev->si_drv1;
304 	struct ifnet		*ifp = &tp->tap_if;
305 
306 	/* junk all pending output */
307 
308 	lwkt_serialize_enter(ifp->if_serializer);
309 	ifq_purge(&ifp->if_snd);
310 	lwkt_serialize_exit(ifp->if_serializer);
311 
312 	/*
313 	 * do not bring the interface down, and do not anything with
314 	 * interface, if we are in VMnet mode. just close the device.
315 	 */
316 
317 	if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
318 		if_down(ifp);
319 		lwkt_serialize_enter(ifp->if_serializer);
320 		if (ifp->if_flags & IFF_RUNNING) {
321 			/* find internet addresses and delete routes */
322 			struct ifaddr	*ifa = NULL;
323 
324 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
325 				if (ifa->ifa_addr->sa_family == AF_INET) {
326 					rtinit(ifa, (int)RTM_DELETE, 0);
327 
328 					/* remove address from interface */
329 					bzero(ifa->ifa_addr,
330 						   sizeof(*(ifa->ifa_addr)));
331 					bzero(ifa->ifa_dstaddr,
332 						   sizeof(*(ifa->ifa_dstaddr)));
333 					bzero(ifa->ifa_netmask,
334 						   sizeof(*(ifa->ifa_netmask)));
335 				}
336 			}
337 
338 			ifp->if_flags &= ~IFF_RUNNING;
339 		}
340 		lwkt_serialize_exit(ifp->if_serializer);
341 	}
342 
343 	funsetown(tp->tap_sigio);
344 	selwakeup(&tp->tap_rsel);
345 
346 	tp->tap_flags &= ~TAP_OPEN;
347 	tp->tap_td = NULL;
348 
349 	taprefcnt --;
350 	if (taprefcnt < 0) {
351 		taprefcnt = 0;
352 		if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
353 			"set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
354 	}
355 
356 	TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
357 		 minor(tp->tap_dev), taprefcnt, taplastunit);
358 
359 	return (0);
360 } /* tapclose */
361 
362 
363 /*
364  * tapifinit
365  *
366  * network interface initialization function
367  */
368 static void
369 tapifinit(void *xtp)
370 {
371 	struct tap_softc	*tp = (struct tap_softc *)xtp;
372 	struct ifnet		*ifp = &tp->tap_if;
373 
374 	TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
375 
376 	ifp->if_flags |= IFF_RUNNING;
377 	ifp->if_flags &= ~IFF_OACTIVE;
378 
379 	/* attempt to start output */
380 	tapifstart(ifp);
381 } /* tapifinit */
382 
383 
384 /*
385  * tapifioctl
386  *
387  * Process an ioctl request on network interface
388  *
389  * MPSAFE
390  */
391 int
392 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
393 {
394 	struct tap_softc 	*tp = (struct tap_softc *)(ifp->if_softc);
395 	struct ifstat		*ifs = NULL;
396 	int			 dummy;
397 
398 	switch (cmd) {
399 		case SIOCSIFADDR:
400 		case SIOCGIFADDR:
401 		case SIOCSIFMTU:
402 			dummy = ether_ioctl(ifp, cmd, data);
403 			return (dummy);
404 
405 		case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
406 		case SIOCADDMULTI:
407 		case SIOCDELMULTI:
408 			break;
409 
410 		case SIOCGIFSTATUS:
411 			ifs = (struct ifstat *)data;
412 			dummy = strlen(ifs->ascii);
413 			if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
414 				if (tp->tap_td->td_proc) {
415 				    snprintf(ifs->ascii + dummy,
416 					sizeof(ifs->ascii) - dummy,
417 					"\tOpened by pid %d\n",
418 					(int)tp->tap_td->td_proc->p_pid);
419 				} else {
420 				    snprintf(ifs->ascii + dummy,
421 					sizeof(ifs->ascii) - dummy,
422 					"\tOpened by td %p\n", tp->tap_td);
423 				}
424 			}
425 			break;
426 
427 		default:
428 			return (EINVAL);
429 	}
430 
431 	return (0);
432 } /* tapifioctl */
433 
434 
435 /*
436  * tapifstart
437  *
438  * queue packets from higher level ready to put out
439  */
440 static void
441 tapifstart(struct ifnet *ifp)
442 {
443 	struct tap_softc	*tp = ifp->if_softc;
444 
445 	TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
446 
447 	/*
448 	 * do not junk pending output if we are in VMnet mode.
449 	 * XXX: can this do any harm because of queue overflow?
450 	 */
451 
452 	if (((tp->tap_flags & TAP_VMNET) == 0) &&
453 	    ((tp->tap_flags & TAP_READY) != TAP_READY)) {
454 		TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
455 			 minor(tp->tap_dev), tp->tap_flags);
456 
457 		ifq_purge(&ifp->if_snd);
458 		return;
459 	}
460 
461 	ifp->if_flags |= IFF_OACTIVE;
462 
463 	if (!ifq_is_empty(&ifp->if_snd)) {
464 		if (tp->tap_flags & TAP_RWAIT) {
465 			tp->tap_flags &= ~TAP_RWAIT;
466 			wakeup((caddr_t)tp);
467 		}
468 
469 		if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
470 			pgsigio(tp->tap_sigio, SIGIO, 0);
471 
472 		/*
473 		 * selwakeup is not MPSAFE.  tapifstart is.
474 		 */
475 		get_mplock();
476 		selwakeup(&tp->tap_rsel);
477 		rel_mplock();
478 		ifp->if_opackets ++; /* obytes are counted in ether_output */
479 	}
480 
481 	ifp->if_flags &= ~IFF_OACTIVE;
482 } /* tapifstart */
483 
484 
485 /*
486  * tapioctl
487  *
488  * the cdevsw interface is now pretty minimal
489  */
490 static int
491 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
492 {
493 	struct tap_softc	*tp = dev->si_drv1;
494 	struct ifnet		*ifp = &tp->tap_if;
495  	struct tapinfo		*tapp = NULL;
496 	struct mbuf *mb;
497 	short f;
498 	int error;
499 
500 	lwkt_serialize_enter(ifp->if_serializer);
501 	error = 0;
502 
503 	switch (cmd) {
504 	case TAPSIFINFO:
505 		tapp = (struct tapinfo *)data;
506 		ifp->if_mtu = tapp->mtu;
507 		ifp->if_type = tapp->type;
508 		ifp->if_baudrate = tapp->baudrate;
509 		break;
510 
511 	case TAPGIFINFO:
512 		tapp = (struct tapinfo *)data;
513 		tapp->mtu = ifp->if_mtu;
514 		tapp->type = ifp->if_type;
515 		tapp->baudrate = ifp->if_baudrate;
516 		break;
517 
518 	case TAPSDEBUG:
519 		tapdebug = *(int *)data;
520 		break;
521 
522 	case TAPGDEBUG:
523 		*(int *)data = tapdebug;
524 		break;
525 
526 	case FIONBIO:
527 		break;
528 
529 	case FIOASYNC:
530 		if (*(int *)data)
531 			tp->tap_flags |= TAP_ASYNC;
532 		else
533 			tp->tap_flags &= ~TAP_ASYNC;
534 		break;
535 
536 	case FIONREAD:
537 		*(int *)data = 0;
538 		if ((mb = ifq_poll(&ifp->if_snd)) != NULL) {
539 			for(; mb != NULL; mb = mb->m_next)
540 				*(int *)data += mb->m_len;
541 		}
542 		break;
543 
544 	case FIOSETOWN:
545 		error = fsetown(*(int *)data, &tp->tap_sigio);
546 		break;
547 
548 	case FIOGETOWN:
549 		*(int *)data = fgetown(tp->tap_sigio);
550 		break;
551 
552 	/* this is deprecated, FIOSETOWN should be used instead */
553 	case TIOCSPGRP:
554 		error = fsetown(-(*(int *)data), &tp->tap_sigio);
555 		break;
556 
557 	/* this is deprecated, FIOGETOWN should be used instead */
558 	case TIOCGPGRP:
559 		*(int *)data = -fgetown(tp->tap_sigio);
560 		break;
561 
562 	/* VMware/VMnet port ioctl's */
563 
564 	case SIOCGIFFLAGS:	/* get ifnet flags */
565 		bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
566 		break;
567 
568 	case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
569 		f = *(short *)data;
570 		f &= 0x0fff;
571 		f &= ~IFF_CANTCHANGE;
572 		f |= IFF_UP;
573 		ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
574 		break;
575 
576 	case OSIOCGIFADDR:	/* get MAC address of the remote side */
577 	case SIOCGIFADDR:
578 		bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
579 		break;
580 
581 	case SIOCSIFADDR:	/* set MAC address of the remote side */
582 		bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
583 		break;
584 
585 	default:
586 		error = ENOTTY;
587 		break;
588 	}
589 	lwkt_serialize_exit(ifp->if_serializer);
590 	return (error);
591 } /* tapioctl */
592 
593 
594 /*
595  * tapread
596  *
597  * the cdevsw read interface - reads a packet at a time, or at
598  * least as much of a packet as can be read
599  */
600 static int
601 tapread(dev_t dev, struct uio *uio, int flag)
602 {
603 	struct tap_softc	*tp = dev->si_drv1;
604 	struct ifnet		*ifp = &tp->tap_if;
605 	struct mbuf		*m0 = NULL;
606 	int			 error = 0, len;
607 
608 	TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
609 
610 	if ((tp->tap_flags & TAP_READY) != TAP_READY) {
611 		TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
612 			 minor(tp->tap_dev), tp->tap_flags);
613 
614 		return (EHOSTDOWN);
615 	}
616 
617 	tp->tap_flags &= ~TAP_RWAIT;
618 
619 	/* sleep until we get a packet */
620 	do {
621 		lwkt_serialize_enter(ifp->if_serializer);
622 		m0 = ifq_dequeue(&ifp->if_snd, NULL);
623 		if (m0 == NULL) {
624 			tp->tap_flags |= TAP_RWAIT;
625 			tsleep_interlock(tp);
626 			lwkt_serialize_exit(ifp->if_serializer);
627 			if (flag & IO_NDELAY)
628 				return (EWOULDBLOCK);
629 			error = tsleep(tp, PCATCH, "taprd", 0);
630 			if (error)
631 				return (error);
632 		} else {
633 			lwkt_serialize_exit(ifp->if_serializer);
634 		}
635 	} while (m0 == NULL);
636 
637 	BPF_MTAP(ifp, m0);
638 
639 	/* xfer packet to user space */
640 	while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
641 		len = min(uio->uio_resid, m0->m_len);
642 		if (len == 0)
643 			break;
644 
645 		error = uiomove(mtod(m0, caddr_t), len, uio);
646 		m0 = m_free(m0);
647 	}
648 
649 	if (m0 != NULL) {
650 		TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
651 			 minor(tp->tap_dev));
652 		m_freem(m0);
653 	}
654 
655 	return (error);
656 } /* tapread */
657 
658 
659 /*
660  * tapwrite
661  *
662  * the cdevsw write interface - an atomic write is a packet - or else!
663  */
664 static int
665 tapwrite(dev_t dev, struct uio *uio, int flag)
666 {
667 	struct tap_softc	*tp = dev->si_drv1;
668 	struct ifnet		*ifp = &tp->tap_if;
669 	struct mbuf		*top = NULL, **mp = NULL, *m = NULL;
670 	int		 	 error = 0, tlen, mlen;
671 
672 	TAPDEBUG(ifp, "writting, minor = %#x\n", minor(tp->tap_dev));
673 
674 	if (uio->uio_resid == 0)
675 		return (0);
676 
677 	if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
678 		TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
679 			 uio->uio_resid, minor(tp->tap_dev));
680 
681 		return (EIO);
682 	}
683 	tlen = uio->uio_resid;
684 
685 	/* get a header mbuf */
686 	MGETHDR(m, MB_DONTWAIT, MT_DATA);
687 	if (m == NULL)
688 		return (ENOBUFS);
689 	mlen = MHLEN;
690 
691 	top = 0;
692 	mp = &top;
693 	while ((error == 0) && (uio->uio_resid > 0)) {
694 		m->m_len = min(mlen, uio->uio_resid);
695 		error = uiomove(mtod(m, caddr_t), m->m_len, uio);
696 		*mp = m;
697 		mp = &m->m_next;
698 		if (uio->uio_resid > 0) {
699 			MGET(m, MB_DONTWAIT, MT_DATA);
700 			if (m == NULL) {
701 				error = ENOBUFS;
702 				break;
703 			}
704 			mlen = MLEN;
705 		}
706 	}
707 	if (error) {
708 		ifp->if_ierrors ++;
709 		if (top)
710 			m_freem(top);
711 		return (error);
712 	}
713 
714 	top->m_pkthdr.len = tlen;
715 	top->m_pkthdr.rcvif = ifp;
716 
717 	/*
718 	 * Ethernet bridge and bpf are handled in ether_input
719 	 *
720 	 * adjust mbuf and give packet to the ether_input
721 	 */
722 	lwkt_serialize_enter(ifp->if_serializer);
723 	ifp->if_input(ifp, top);
724 	ifp->if_ipackets ++; /* ibytes are counted in ether_input */
725 	lwkt_serialize_exit(ifp->if_serializer);
726 
727 	return (0);
728 } /* tapwrite */
729 
730 
731 /*
732  * tappoll
733  *
734  * the poll interface, this is only useful on reads
735  * really. the write detect always returns true, write never blocks
736  * anyway, it either accepts the packet or drops it
737  */
738 static int
739 tappoll(dev_t dev, int events, d_thread_t *td)
740 {
741 	struct tap_softc	*tp = dev->si_drv1;
742 	struct ifnet		*ifp = &tp->tap_if;
743 	int		 	 revents = 0;
744 
745 	TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
746 
747 	lwkt_serialize_enter(ifp->if_serializer);
748 	if (events & (POLLIN | POLLRDNORM)) {
749 		if (!ifq_is_empty(&ifp->if_snd)) {
750 			TAPDEBUG(ifp,
751 				 "has data in queue. minor = %#x\n",
752 				 minor(tp->tap_dev));
753 
754 			revents |= (events & (POLLIN | POLLRDNORM));
755 		}
756 		else {
757 			TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
758 				 minor(tp->tap_dev));
759 
760 			selrecord(td, &tp->tap_rsel);
761 		}
762 	}
763 	lwkt_serialize_exit(ifp->if_serializer);
764 
765 	if (events & (POLLOUT | POLLWRNORM))
766 		revents |= (events & (POLLOUT | POLLWRNORM));
767 	return (revents);
768 } /* tappoll */
769