2 * Copyright (C) 1999-2000 by Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 * -------------------------------------------------------------------------
29 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
30 * Nottingham University 1987.
35 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
40 #include <sys/param.h>
42 #include <sys/fcntl.h>
43 #include <sys/filio.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/module.h>
50 #include <sys/selinfo.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
56 #include <sys/ttycom.h>
58 #include <sys/queue.h>
61 #include <net/ethernet.h>
63 #include <net/if_dl.h>
64 #include <net/route.h>
65 #include <net/if_types.h>
67 #include <netinet/in.h>
69 #include <net/if_tapvar.h>
70 #include <net/if_tap.h>
73 #define CDEV_NAME "tap"
74 #define TAPDEBUG if (tapdebug) printf
78 #define TAPMAXUNIT 0x7fff
79 #define VMNET_DEV_MASK CLONE_FLAG0
82 static int tapmodevent(module_t, int, void *);
85 static void tapclone(void *, struct ucred *, char *, int,
87 static void tapcreate(struct cdev *);
89 /* network interface */
90 static void tapifstart(struct ifnet *);
91 static int tapifioctl(struct ifnet *, u_long, caddr_t);
92 static void tapifinit(void *);
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;
102 static struct cdevsw tap_cdevsw = {
103 .d_version = D_VERSION,
104 .d_flags = D_PSEUDO | D_NEEDGIANT,
115 * All global variables in if_tap.c are locked with tapmtx, with the
116 * exception of tapdebug, which is accessed unlocked; tapclones is
119 static struct mtx tapmtx;
120 static int tapdebug = 0; /* debug flag */
121 static int tapuopen = 0; /* allow user open() */
122 static SLIST_HEAD(, tap_softc) taphead; /* first device */
123 static struct clonedevs *tapclones;
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, "");
129 SYSCTL_DECL(_net_link);
130 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
131 "Ethernet tunnel software network interface");
132 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
133 "Allow user to open /dev/tap (based on node permissions)");
134 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
136 DEV_MODULE(if_tap, tapmodevent, NULL);
141 * module event handler
144 tapmodevent(mod, type, data)
149 static eventhandler_tag eh_tag = NULL;
150 struct tap_softc *tp = NULL;
151 struct ifnet *ifp = NULL;
157 /* intitialize device */
159 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
160 SLIST_INIT(&taphead);
162 clone_setup(&tapclones);
163 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
164 if (eh_tag == NULL) {
165 clone_cleanup(&tapclones);
166 mtx_destroy(&tapmtx);
173 * The EBUSY algorithm here can't quite atomically
174 * guarantee that this is race-free since we have to
175 * release the tap mtx to deregister the clone handler.
178 SLIST_FOREACH(tp, &taphead, tap_next) {
179 mtx_lock(&tp->tap_mtx);
180 if (tp->tap_flags & TAP_OPEN) {
181 mtx_unlock(&tp->tap_mtx);
185 mtx_unlock(&tp->tap_mtx);
189 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
192 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
193 SLIST_REMOVE_HEAD(&taphead, tap_next);
198 TAPDEBUG("detaching %s\n", ifp->if_xname);
201 KASSERT(!(tp->tap_flags & TAP_OPEN),
202 ("%s flags is out of sync", ifp->if_xname));
204 destroy_dev(tp->tap_dev);
207 if_free_type(ifp, IFT_ETHER);
210 mtx_destroy(&tp->tap_mtx);
215 clone_cleanup(&tapclones);
217 mtx_destroy(&tapmtx);
232 * We need to support two kind of devices - tap and vmnet
235 tapclone(arg, cred, name, namelen, dev)
244 char *device_name = name;
251 if (strcmp(name, TAP) == 0) {
253 } else if (strcmp(name, VMNET) == 0) {
255 extra = VMNET_DEV_MASK;
257 } else if (dev_stdclone(name, NULL, device_name, &unit) != 1) {
259 extra = VMNET_DEV_MASK;
260 if (dev_stdclone(name, NULL, device_name, &unit) != 1)
264 /* find any existing device, or allocate new unit number */
265 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
267 *dev = make_dev(&tap_cdevsw, unit2minor(unit | extra),
268 UID_ROOT, GID_WHEEL, 0600, "%s%d", device_name, unit);
271 (*dev)->si_flags |= SI_CHEAPCLONE;
280 * to create interface
286 struct ifnet *ifp = NULL;
287 struct tap_softc *tp = NULL;
288 unsigned short macaddr_hi;
293 dev->si_flags &= ~SI_CHEAPCLONE;
295 /* allocate driver storage and create device */
296 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
297 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
299 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
302 unit = dev2unit(dev);
304 /* select device: tap or vmnet */
305 if (unit & VMNET_DEV_MASK) {
307 tp->tap_flags |= TAP_VMNET;
313 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, minor(dev));
315 /* generate fake MAC address: 00 bd xx xx xx unit_no */
316 macaddr_hi = htons(0x00bd);
317 bcopy(&macaddr_hi, eaddr, sizeof(short));
318 bcopy(&ticks, &eaddr[2], sizeof(long));
319 eaddr[5] = (u_char)unit;
321 /* fill the rest and attach interface */
322 ifp = tp->tap_ifp = if_alloc(IFT_ETHER);
324 panic("%s%d: can not if_alloc()", name, unit);
326 if_initname(ifp, name, unit);
327 ifp->if_init = tapifinit;
328 ifp->if_start = tapifstart;
329 ifp->if_ioctl = tapifioctl;
330 ifp->if_mtu = ETHERMTU;
331 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
332 ifp->if_snd.ifq_maxlen = ifqmaxlen;
338 ether_ifattach(ifp, eaddr);
341 mtx_lock(&tp->tap_mtx);
342 tp->tap_flags |= TAP_INITED;
343 mtx_unlock(&tp->tap_mtx);
345 TAPDEBUG("interface %s is created. minor = %#x\n",
346 ifp->if_xname, minor(dev));
353 * to open tunnel. must be superuser
356 tapopen(dev, flag, mode, td)
362 struct tap_softc *tp = NULL;
363 struct ifnet *ifp = NULL;
366 if (tapuopen == 0 && suser(td) != 0)
369 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
373 * XXXRW: Non-atomic test-and-set of si_drv1. Currently protected
374 * by Giant, but the race actually exists under memory pressure as
375 * well even when running with Giant, as malloc() may sleep.
383 mtx_lock(&tp->tap_mtx);
384 if (tp->tap_flags & TAP_OPEN) {
385 mtx_unlock(&tp->tap_mtx);
389 bcopy(IF_LLADDR(tp->tap_ifp), tp->ether_addr, sizeof(tp->ether_addr));
390 tp->tap_pid = td->td_proc->p_pid;
391 tp->tap_flags |= TAP_OPEN;
393 mtx_unlock(&tp->tap_mtx);
396 ifp->if_drv_flags |= IFF_DRV_RUNNING;
397 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
400 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, minor(dev));
409 * close the device - mark i/f down & delete routing info
412 tapclose(dev, foo, bar, td)
419 struct tap_softc *tp = dev->si_drv1;
420 struct ifnet *ifp = tp->tap_ifp;
423 /* junk all pending output */
424 IF_DRAIN(&ifp->if_snd);
427 * do not bring the interface down, and do not anything with
428 * interface, if we are in VMnet mode. just close the device.
431 mtx_lock(&tp->tap_mtx);
432 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
433 mtx_unlock(&tp->tap_mtx);
436 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
437 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
438 rtinit(ifa, (int)RTM_DELETE, 0);
441 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
445 mtx_unlock(&tp->tap_mtx);
447 funsetown(&tp->tap_sigio);
448 selwakeuppri(&tp->tap_rsel, PZERO+1);
450 mtx_lock(&tp->tap_mtx);
451 tp->tap_flags &= ~TAP_OPEN;
453 mtx_unlock(&tp->tap_mtx);
455 TAPDEBUG("%s is closed. minor = %#x\n",
456 ifp->if_xname, minor(dev));
465 * network interface initialization function
471 struct tap_softc *tp = (struct tap_softc *)xtp;
472 struct ifnet *ifp = tp->tap_ifp;
474 TAPDEBUG("initializing %s\n", ifp->if_xname);
476 ifp->if_drv_flags |= IFF_DRV_RUNNING;
477 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
479 /* attempt to start output */
487 * Process an ioctl request on network interface
490 tapifioctl(ifp, cmd, data)
495 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
496 struct ifstat *ifs = NULL;
500 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
507 ifs = (struct ifstat *)data;
508 dummy = strlen(ifs->ascii);
509 mtx_lock(&tp->tap_mtx);
510 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
511 snprintf(ifs->ascii + dummy,
512 sizeof(ifs->ascii) - dummy,
513 "\tOpened by PID %d\n", tp->tap_pid);
514 mtx_unlock(&tp->tap_mtx);
520 dummy = ether_ioctl(ifp, cmd, data);
532 * queue packets from higher level ready to put out
538 struct tap_softc *tp = ifp->if_softc;
541 TAPDEBUG("%s starting\n", ifp->if_xname);
544 * do not junk pending output if we are in VMnet mode.
545 * XXX: can this do any harm because of queue overflow?
548 mtx_lock(&tp->tap_mtx);
549 if (((tp->tap_flags & TAP_VMNET) == 0) &&
550 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
551 struct mbuf *m = NULL;
553 mtx_unlock(&tp->tap_mtx);
556 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
561 IF_DEQUEUE(&ifp->if_snd, m);
570 mtx_unlock(&tp->tap_mtx);
573 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
575 if (ifp->if_snd.ifq_len != 0) {
576 mtx_lock(&tp->tap_mtx);
577 if (tp->tap_flags & TAP_RWAIT) {
578 tp->tap_flags &= ~TAP_RWAIT;
582 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
583 mtx_unlock(&tp->tap_mtx);
584 pgsigio(&tp->tap_sigio, SIGIO, 0);
586 mtx_unlock(&tp->tap_mtx);
588 selwakeuppri(&tp->tap_rsel, PZERO+1);
589 ifp->if_opackets ++; /* obytes are counted in ether_output */
592 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
600 * the cdevsw interface is now pretty minimal
603 tapioctl(dev, cmd, data, flag, td)
610 struct tap_softc *tp = dev->si_drv1;
611 struct ifnet *ifp = tp->tap_ifp;
612 struct tapinfo *tapp = NULL;
619 tapp = (struct tapinfo *)data;
620 ifp->if_mtu = tapp->mtu;
621 ifp->if_type = tapp->type;
622 ifp->if_baudrate = tapp->baudrate;
627 tapp = (struct tapinfo *)data;
628 tapp->mtu = ifp->if_mtu;
629 tapp->type = ifp->if_type;
630 tapp->baudrate = ifp->if_baudrate;
634 tapdebug = *(int *)data;
638 *(int *)data = tapdebug;
646 mtx_lock(&tp->tap_mtx);
648 tp->tap_flags |= TAP_ASYNC;
650 tp->tap_flags &= ~TAP_ASYNC;
651 mtx_unlock(&tp->tap_mtx);
657 if (ifp->if_snd.ifq_head) {
658 struct mbuf *mb = ifp->if_snd.ifq_head;
660 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
661 *(int *)data += mb->m_len;
668 return (fsetown(*(int *)data, &tp->tap_sigio));
671 *(int *)data = fgetown(&tp->tap_sigio);
674 /* this is deprecated, FIOSETOWN should be used instead */
676 return (fsetown(-(*(int *)data), &tp->tap_sigio));
678 /* this is deprecated, FIOGETOWN should be used instead */
680 *(int *)data = -fgetown(&tp->tap_sigio);
683 /* VMware/VMnet port ioctl's */
685 case SIOCGIFFLAGS: /* get ifnet flags */
686 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
689 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
692 f &= ~IFF_CANTCHANGE;
696 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
700 case OSIOCGIFADDR: /* get MAC address of the remote side */
702 mtx_lock(&tp->tap_mtx);
703 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
704 mtx_unlock(&tp->tap_mtx);
707 case SIOCSIFADDR: /* set MAC address of the remote side */
708 mtx_lock(&tp->tap_mtx);
709 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
710 mtx_unlock(&tp->tap_mtx);
723 * the cdevsw read interface - reads a packet at a time, or at
724 * least as much of a packet as can be read
727 tapread(dev, uio, flag)
732 struct tap_softc *tp = dev->si_drv1;
733 struct ifnet *ifp = tp->tap_ifp;
734 struct mbuf *m = NULL;
735 int error = 0, len, s;
737 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev));
739 mtx_lock(&tp->tap_mtx);
740 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
741 mtx_unlock(&tp->tap_mtx);
744 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
745 ifp->if_xname, minor(dev), tp->tap_flags);
750 tp->tap_flags &= ~TAP_RWAIT;
751 mtx_unlock(&tp->tap_mtx);
753 /* sleep until we get a packet */
756 IF_DEQUEUE(&ifp->if_snd, m);
760 if (flag & O_NONBLOCK)
761 return (EWOULDBLOCK);
763 mtx_lock(&tp->tap_mtx);
764 tp->tap_flags |= TAP_RWAIT;
765 mtx_unlock(&tp->tap_mtx);
766 error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0);
772 /* feed packet to bpf */
775 /* xfer packet to user space */
776 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
777 len = min(uio->uio_resid, m->m_len);
781 error = uiomove(mtod(m, void *), len, uio);
786 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
798 * the cdevsw write interface - an atomic write is a packet - or else!
801 tapwrite(dev, uio, flag)
806 struct tap_softc *tp = dev->si_drv1;
807 struct ifnet *ifp = tp->tap_ifp;
811 TAPDEBUG("%s writting, minor = %#x\n",
812 ifp->if_xname, minor(dev));
814 if (uio->uio_resid == 0)
817 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
818 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
819 ifp->if_xname, uio->uio_resid, minor(dev));
824 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN)) == NULL) {
829 m->m_pkthdr.rcvif = ifp;
831 /* Pass packet up to parent. */
832 (*ifp->if_input)(ifp, m);
833 ifp->if_ipackets ++; /* ibytes are counted in parent */
842 * the poll interface, this is only useful on reads
843 * really. the write detect always returns true, write never blocks
844 * anyway, it either accepts the packet or drops it
847 tappoll(dev, events, td)
852 struct tap_softc *tp = dev->si_drv1;
853 struct ifnet *ifp = tp->tap_ifp;
856 TAPDEBUG("%s polling, minor = %#x\n",
857 ifp->if_xname, minor(dev));
860 if (events & (POLLIN | POLLRDNORM)) {
861 if (ifp->if_snd.ifq_len > 0) {
862 TAPDEBUG("%s have data in queue. len = %d, " \
863 "minor = %#x\n", ifp->if_xname,
864 ifp->if_snd.ifq_len, minor(dev));
866 revents |= (events & (POLLIN | POLLRDNORM));
868 TAPDEBUG("%s waiting for data, minor = %#x\n",
869 ifp->if_xname, minor(dev));
871 selrecord(td, &tp->tap_rsel);
875 if (events & (POLLOUT | POLLWRNORM))
876 revents |= (events & (POLLOUT | POLLWRNORM));