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_arp.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
68 #include <net/if_tapvar.h>
69 #include <net/if_tap.h>
72 #define CDEV_NAME "tap"
73 #define TAPDEBUG if (tapdebug) printf
77 #define TAPMAXUNIT 0x7fff
78 #define VMNET_DEV_MASK CLONE_FLAG0
81 static int tapmodevent(module_t, int, void *);
84 static void tapclone(void *, char *, int, struct cdev **);
85 static void tapcreate(struct cdev *);
87 /* network interface */
88 static void tapifstart(struct ifnet *);
89 static int tapifioctl(struct ifnet *, u_long, caddr_t);
90 static void tapifinit(void *);
92 /* character device */
93 static d_open_t tapopen;
94 static d_close_t tapclose;
95 static d_read_t tapread;
96 static d_write_t tapwrite;
97 static d_ioctl_t tapioctl;
98 static d_poll_t tappoll;
100 static struct cdevsw tap_cdevsw = {
101 .d_version = D_VERSION,
102 .d_flags = D_PSEUDO | D_NEEDGIANT,
113 * All global variables in if_tap.c are locked with tapmtx, with the
114 * exception of tapdebug, which is accessed unlocked; tapclones is
117 static struct mtx tapmtx;
118 static int tapdebug = 0; /* debug flag */
119 static int tapuopen = 0; /* allow user open() */
120 static SLIST_HEAD(, tap_softc) taphead; /* first device */
121 static struct clonedevs *tapclones;
123 MALLOC_DECLARE(M_TAP);
124 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
125 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
127 SYSCTL_DECL(_net_link);
128 SYSCTL_NODE(_net_link, OID_AUTO, tap, CTLFLAG_RW, 0,
129 "Ethernet tunnel software network interface");
130 SYSCTL_INT(_net_link_tap, OID_AUTO, user_open, CTLFLAG_RW, &tapuopen, 0,
131 "Allow user to open /dev/tap (based on node permissions)");
132 SYSCTL_INT(_net_link_tap, OID_AUTO, debug, CTLFLAG_RW, &tapdebug, 0, "");
134 DEV_MODULE(if_tap, tapmodevent, NULL);
139 * module event handler
142 tapmodevent(mod, type, data)
147 static eventhandler_tag eh_tag = NULL;
148 struct tap_softc *tp = NULL;
149 struct ifnet *ifp = NULL;
155 /* intitialize device */
157 mtx_init(&tapmtx, "tapmtx", NULL, MTX_DEF);
158 SLIST_INIT(&taphead);
160 clone_setup(&tapclones);
161 eh_tag = EVENTHANDLER_REGISTER(dev_clone, tapclone, 0, 1000);
162 if (eh_tag == NULL) {
163 clone_cleanup(&tapclones);
164 mtx_destroy(&tapmtx);
171 * The EBUSY algorithm here can't quite atomically
172 * guarantee that this is race-free since we have to
173 * release the tap mtx to deregister the clone handler.
176 SLIST_FOREACH(tp, &taphead, tap_next) {
177 mtx_lock(&tp->tap_mtx);
178 if (tp->tap_flags & TAP_OPEN) {
179 mtx_unlock(&tp->tap_mtx);
183 mtx_unlock(&tp->tap_mtx);
187 EVENTHANDLER_DEREGISTER(dev_clone, eh_tag);
190 while ((tp = SLIST_FIRST(&taphead)) != NULL) {
191 SLIST_REMOVE_HEAD(&taphead, tap_next);
196 TAPDEBUG("detaching %s\n", ifp->if_xname);
199 KASSERT(!(tp->tap_flags & TAP_OPEN),
200 ("%s flags is out of sync", ifp->if_xname));
202 destroy_dev(tp->tap_dev);
207 mtx_destroy(&tp->tap_mtx);
212 clone_cleanup(&tapclones);
214 mtx_destroy(&tapmtx);
229 * We need to support two kind of devices - tap and vmnet
232 tapclone(arg, name, namelen, dev)
240 char *device_name = name;
247 if (strcmp(name, TAP) == 0) {
249 } else if (strcmp(name, VMNET) == 0) {
251 extra = VMNET_DEV_MASK;
253 } else if (dev_stdclone(name, NULL, device_name, &unit) != 1) {
255 extra = VMNET_DEV_MASK;
256 if (dev_stdclone(name, NULL, device_name, &unit) != 1)
260 /* find any existing device, or allocate new unit number */
261 i = clone_create(&tapclones, &tap_cdevsw, &unit, dev, extra);
263 *dev = make_dev(&tap_cdevsw, unit2minor(unit | extra),
264 UID_ROOT, GID_WHEEL, 0600, "%s%d", device_name, unit);
267 (*dev)->si_flags |= SI_CHEAPCLONE;
276 * to create interface
282 struct ifnet *ifp = NULL;
283 struct tap_softc *tp = NULL;
284 unsigned short macaddr_hi;
288 dev->si_flags &= ~SI_CHEAPCLONE;
290 /* allocate driver storage and create device */
291 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK | M_ZERO);
292 mtx_init(&tp->tap_mtx, "tap_mtx", NULL, MTX_DEF);
294 SLIST_INSERT_HEAD(&taphead, tp, tap_next);
297 unit = dev2unit(dev);
299 /* select device: tap or vmnet */
300 if (unit & VMNET_DEV_MASK) {
302 tp->tap_flags |= TAP_VMNET;
308 TAPDEBUG("tapcreate(%s%d). minor = %#x\n", name, unit, minor(dev));
310 /* generate fake MAC address: 00 bd xx xx xx unit_no */
311 macaddr_hi = htons(0x00bd);
312 bcopy(&macaddr_hi, &tp->arpcom.ac_enaddr[0], sizeof(short));
313 bcopy(&ticks, &tp->arpcom.ac_enaddr[2], sizeof(long));
314 tp->arpcom.ac_enaddr[5] = (u_char)unit;
316 /* fill the rest and attach interface */
319 if_initname(ifp, name, unit);
320 ifp->if_init = tapifinit;
321 ifp->if_start = tapifstart;
322 ifp->if_ioctl = tapifioctl;
323 ifp->if_mtu = ETHERMTU;
324 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
325 ifp->if_snd.ifq_maxlen = ifqmaxlen;
331 ether_ifattach(ifp, tp->arpcom.ac_enaddr);
334 mtx_lock(&tp->tap_mtx);
335 tp->tap_flags |= TAP_INITED;
336 mtx_unlock(&tp->tap_mtx);
338 TAPDEBUG("interface %s is created. minor = %#x\n",
339 ifp->if_xname, minor(dev));
346 * to open tunnel. must be superuser
349 tapopen(dev, flag, mode, td)
355 struct tap_softc *tp = NULL;
356 struct ifnet *ifp = NULL;
359 if (tapuopen == 0 && suser(td) != 0)
362 if ((dev2unit(dev) & CLONE_UNITMASK) > TAPMAXUNIT)
366 * XXXRW: Non-atomic test-and-set of si_drv1. Currently protected
367 * by Giant, but the race actually exists under memory pressure as
368 * well even when running with Giant, as malloc() may sleep.
376 mtx_lock(&tp->tap_mtx);
377 if (tp->tap_flags & TAP_OPEN) {
378 mtx_unlock(&tp->tap_mtx);
382 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
383 tp->tap_pid = td->td_proc->p_pid;
384 tp->tap_flags |= TAP_OPEN;
386 mtx_unlock(&tp->tap_mtx);
389 ifp->if_flags |= IFF_RUNNING;
390 ifp->if_flags &= ~IFF_OACTIVE;
393 TAPDEBUG("%s is open. minor = %#x\n", ifp->if_xname, minor(dev));
402 * close the device - mark i/f down & delete routing info
405 tapclose(dev, foo, bar, td)
412 struct tap_softc *tp = dev->si_drv1;
413 struct ifnet *ifp = &tp->tap_if;
416 /* junk all pending output */
417 IF_DRAIN(&ifp->if_snd);
420 * do not bring the interface down, and do not anything with
421 * interface, if we are in VMnet mode. just close the device.
424 mtx_lock(&tp->tap_mtx);
425 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
426 mtx_unlock(&tp->tap_mtx);
429 if (ifp->if_flags & IFF_RUNNING) {
430 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
431 rtinit(ifa, (int)RTM_DELETE, 0);
434 ifp->if_flags &= ~IFF_RUNNING;
438 mtx_unlock(&tp->tap_mtx);
440 funsetown(&tp->tap_sigio);
441 selwakeuppri(&tp->tap_rsel, PZERO+1);
443 mtx_lock(&tp->tap_mtx);
444 tp->tap_flags &= ~TAP_OPEN;
446 mtx_unlock(&tp->tap_mtx);
448 TAPDEBUG("%s is closed. minor = %#x\n",
449 ifp->if_xname, minor(dev));
458 * network interface initialization function
464 struct tap_softc *tp = (struct tap_softc *)xtp;
465 struct ifnet *ifp = &tp->tap_if;
467 TAPDEBUG("initializing %s\n", ifp->if_xname);
469 ifp->if_flags |= IFF_RUNNING;
470 ifp->if_flags &= ~IFF_OACTIVE;
472 /* attempt to start output */
480 * Process an ioctl request on network interface
483 tapifioctl(ifp, cmd, data)
488 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
489 struct ifstat *ifs = NULL;
493 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
500 ifs = (struct ifstat *)data;
501 dummy = strlen(ifs->ascii);
502 mtx_lock(&tp->tap_mtx);
503 if (tp->tap_pid != 0 && dummy < sizeof(ifs->ascii))
504 snprintf(ifs->ascii + dummy,
505 sizeof(ifs->ascii) - dummy,
506 "\tOpened by PID %d\n", tp->tap_pid);
507 mtx_unlock(&tp->tap_mtx);
513 dummy = ether_ioctl(ifp, cmd, data);
525 * queue packets from higher level ready to put out
531 struct tap_softc *tp = ifp->if_softc;
534 TAPDEBUG("%s starting\n", ifp->if_xname);
537 * do not junk pending output if we are in VMnet mode.
538 * XXX: can this do any harm because of queue overflow?
541 mtx_lock(&tp->tap_mtx);
542 if (((tp->tap_flags & TAP_VMNET) == 0) &&
543 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
544 struct mbuf *m = NULL;
546 mtx_unlock(&tp->tap_mtx);
549 TAPDEBUG("%s not ready, tap_flags = 0x%x\n", ifp->if_xname,
554 IF_DEQUEUE(&ifp->if_snd, m);
563 mtx_unlock(&tp->tap_mtx);
566 ifp->if_flags |= IFF_OACTIVE;
568 if (ifp->if_snd.ifq_len != 0) {
569 mtx_lock(&tp->tap_mtx);
570 if (tp->tap_flags & TAP_RWAIT) {
571 tp->tap_flags &= ~TAP_RWAIT;
575 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL)) {
576 mtx_unlock(&tp->tap_mtx);
577 pgsigio(&tp->tap_sigio, SIGIO, 0);
579 mtx_unlock(&tp->tap_mtx);
581 selwakeuppri(&tp->tap_rsel, PZERO+1);
582 ifp->if_opackets ++; /* obytes are counted in ether_output */
585 ifp->if_flags &= ~IFF_OACTIVE;
593 * the cdevsw interface is now pretty minimal
596 tapioctl(dev, cmd, data, flag, td)
603 struct tap_softc *tp = dev->si_drv1;
604 struct ifnet *ifp = &tp->tap_if;
605 struct tapinfo *tapp = NULL;
612 tapp = (struct tapinfo *)data;
613 ifp->if_mtu = tapp->mtu;
614 ifp->if_type = tapp->type;
615 ifp->if_baudrate = tapp->baudrate;
620 tapp = (struct tapinfo *)data;
621 tapp->mtu = ifp->if_mtu;
622 tapp->type = ifp->if_type;
623 tapp->baudrate = ifp->if_baudrate;
627 tapdebug = *(int *)data;
631 *(int *)data = tapdebug;
639 mtx_lock(&tp->tap_mtx);
641 tp->tap_flags |= TAP_ASYNC;
643 tp->tap_flags &= ~TAP_ASYNC;
644 mtx_unlock(&tp->tap_mtx);
650 if (ifp->if_snd.ifq_head) {
651 struct mbuf *mb = ifp->if_snd.ifq_head;
653 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
654 *(int *)data += mb->m_len;
661 return (fsetown(*(int *)data, &tp->tap_sigio));
664 *(int *)data = fgetown(&tp->tap_sigio);
667 /* this is deprecated, FIOSETOWN should be used instead */
669 return (fsetown(-(*(int *)data), &tp->tap_sigio));
671 /* this is deprecated, FIOGETOWN should be used instead */
673 *(int *)data = -fgetown(&tp->tap_sigio);
676 /* VMware/VMnet port ioctl's */
678 case SIOCGIFFLAGS: /* get ifnet flags */
679 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
682 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
685 f &= ~IFF_CANTCHANGE;
689 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
693 case OSIOCGIFADDR: /* get MAC address of the remote side */
695 mtx_lock(&tp->tap_mtx);
696 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
697 mtx_unlock(&tp->tap_mtx);
700 case SIOCSIFADDR: /* set MAC address of the remote side */
701 mtx_lock(&tp->tap_mtx);
702 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
703 mtx_unlock(&tp->tap_mtx);
716 * the cdevsw read interface - reads a packet at a time, or at
717 * least as much of a packet as can be read
720 tapread(dev, uio, flag)
725 struct tap_softc *tp = dev->si_drv1;
726 struct ifnet *ifp = &tp->tap_if;
727 struct mbuf *m = NULL;
728 int error = 0, len, s;
730 TAPDEBUG("%s reading, minor = %#x\n", ifp->if_xname, minor(dev));
732 mtx_lock(&tp->tap_mtx);
733 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
734 mtx_unlock(&tp->tap_mtx);
737 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
738 ifp->if_xname, minor(dev), tp->tap_flags);
743 tp->tap_flags &= ~TAP_RWAIT;
744 mtx_unlock(&tp->tap_mtx);
746 /* sleep until we get a packet */
749 IF_DEQUEUE(&ifp->if_snd, m);
753 if (flag & O_NONBLOCK)
754 return (EWOULDBLOCK);
756 mtx_lock(&tp->tap_mtx);
757 tp->tap_flags |= TAP_RWAIT;
758 mtx_unlock(&tp->tap_mtx);
759 error = tsleep(tp,PCATCH|(PZERO+1),"taprd",0);
765 /* feed packet to bpf */
768 /* xfer packet to user space */
769 while ((m != NULL) && (uio->uio_resid > 0) && (error == 0)) {
770 len = min(uio->uio_resid, m->m_len);
774 error = uiomove(mtod(m, void *), len, uio);
779 TAPDEBUG("%s dropping mbuf, minor = %#x\n", ifp->if_xname,
791 * the cdevsw write interface - an atomic write is a packet - or else!
794 tapwrite(dev, uio, flag)
799 struct tap_softc *tp = dev->si_drv1;
800 struct ifnet *ifp = &tp->tap_if;
804 TAPDEBUG("%s writting, minor = %#x\n",
805 ifp->if_xname, minor(dev));
807 if (uio->uio_resid == 0)
810 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
811 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
812 ifp->if_xname, uio->uio_resid, minor(dev));
817 if ((m = m_uiotombuf(uio, M_DONTWAIT, 0, ETHER_ALIGN)) == NULL) {
822 m->m_pkthdr.rcvif = ifp;
824 /* Pass packet up to parent. */
825 (*ifp->if_input)(ifp, m);
826 ifp->if_ipackets ++; /* ibytes are counted in parent */
835 * the poll interface, this is only useful on reads
836 * really. the write detect always returns true, write never blocks
837 * anyway, it either accepts the packet or drops it
840 tappoll(dev, events, td)
845 struct tap_softc *tp = dev->si_drv1;
846 struct ifnet *ifp = &tp->tap_if;
849 TAPDEBUG("%s polling, minor = %#x\n",
850 ifp->if_xname, minor(dev));
853 if (events & (POLLIN | POLLRDNORM)) {
854 if (ifp->if_snd.ifq_len > 0) {
855 TAPDEBUG("%s have data in queue. len = %d, " \
856 "minor = %#x\n", ifp->if_xname,
857 ifp->if_snd.ifq_len, minor(dev));
859 revents |= (events & (POLLIN | POLLRDNORM));
861 TAPDEBUG("%s waiting for data, minor = %#x\n",
862 ifp->if_xname, minor(dev));
864 selrecord(td, &tp->tap_rsel);
868 if (events & (POLLOUT | POLLWRNORM))
869 revents |= (events & (POLLOUT | POLLWRNORM));