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.
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.19 2005/06/14 18:30:55 joerg Exp $
36 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
41 #include <sys/param.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.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>
58 #include <sys/vnode.h>
61 #include <net/ethernet.h>
63 #include <net/if_arp.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
68 #include "if_tapvar.h"
72 #define CDEV_NAME "tap"
73 #define CDEV_MAJOR 149
74 #define TAPDEBUG if (tapdebug) if_printf
78 #define VMNET_DEV_MASK 0x00010000
81 static int tapmodevent (module_t, int, void *);
84 static void tapcreate (dev_t);
86 /* network interface */
87 static void tapifstart (struct ifnet *);
88 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 /* dev name */ CDEV_NAME,
102 /* dev major */ CDEV_MAJOR,
108 /* close */ tapclose,
110 /* write */ tapwrite,
111 /* ioctl */ tapioctl,
114 /* startegy */ nostrategy,
119 static int taprefcnt = 0; /* module ref. counter */
120 static int taplastunit = -1; /* max. open unit number */
121 static int tapdebug = 0; /* debug flag */
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, "");
126 DEV_MODULE(if_tap, tapmodevent, NULL);
131 * module event handler
134 tapmodevent(mod, type, data)
139 static int attached = 0;
140 struct ifnet *ifp = NULL;
148 cdevsw_add(&tap_cdevsw, 0, 0);
156 cdevsw_remove(&tap_cdevsw, 0, 0);
158 /* XXX: maintain tap ifs in a local list */
160 while (unit <= taplastunit) {
162 TAILQ_FOREACH(ifp, &ifnet, if_link)
163 if ((strcmp(ifp->if_dname, TAP) == 0) ||
164 (strcmp(ifp->if_dname, VMNET) == 0))
165 if (ifp->if_dunit == unit)
170 struct tap_softc *tp = ifp->if_softc;
172 TAPDEBUG(ifp, "detached. minor = %#x, " \
173 "taplastunit = %d\n",
174 minor(tp->tap_dev), taplastunit);
179 destroy_dev(tp->tap_dev);
200 * to create interface
206 struct ifnet *ifp = NULL;
207 struct tap_softc *tp = NULL;
208 uint8_t ether_addr[ETHER_ADDR_LEN];
212 /* allocate driver storage and create device */
213 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
214 bzero(tp, sizeof(*tp));
216 /* select device: tap or vmnet */
217 if (minor(dev) & VMNET_DEV_MASK) {
219 unit = lminor(dev) & 0xff;
220 tp->tap_flags |= TAP_VMNET;
227 tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
228 0600, "%s%d", name, unit);
229 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
230 reference_dev(tp->tap_dev); /* so we can destroy it later */
232 /* generate fake MAC address: 00 bd xx xx xx unit_no */
233 ether_addr[0] = 0x00;
234 ether_addr[1] = 0xbd;
235 bcopy(&ticks, ether_addr, 4);
236 ether_addr[5] = (u_char)unit;
238 /* fill the rest and attach interface */
242 if_initname(ifp, name, unit);
243 if (unit > taplastunit)
246 ifp->if_init = tapifinit;
247 ifp->if_start = tapifstart;
248 ifp->if_ioctl = tapifioctl;
249 ifp->if_mtu = ETHERMTU;
250 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
251 ifp->if_snd.ifq_maxlen = ifqmaxlen;
253 ether_ifattach(ifp, ether_addr);
255 tp->tap_flags |= TAP_INITED;
257 TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
264 * to open tunnel. must be superuser
267 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
269 struct tap_softc *tp = NULL;
272 if ((error = suser(td)) != 0)
281 if (tp->tap_flags & TAP_OPEN)
284 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
287 tp->tap_flags |= TAP_OPEN;
290 TAPDEBUG(&tp->arpcom.ac_if,
291 "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
292 minor(tp->tap_dev), taprefcnt, taplastunit);
301 * close the device - mark i/f down & delete routing info
304 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
306 struct tap_softc *tp = dev->si_drv1;
307 struct ifnet *ifp = &tp->tap_if;
308 struct mbuf *m = NULL;
310 /* junk all pending output */
314 IF_DEQUEUE(&ifp->if_snd, m);
321 * do not bring the interface down, and do not anything with
322 * interface, if we are in VMnet mode. just close the device.
325 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
328 if (ifp->if_flags & IFF_RUNNING) {
329 /* find internet addresses and delete routes */
330 struct ifaddr *ifa = NULL;
332 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
333 if (ifa->ifa_addr->sa_family == AF_INET) {
334 rtinit(ifa, (int)RTM_DELETE, 0);
336 /* remove address from interface */
338 sizeof(*(ifa->ifa_addr)));
339 bzero(ifa->ifa_dstaddr,
340 sizeof(*(ifa->ifa_dstaddr)));
341 bzero(ifa->ifa_netmask,
342 sizeof(*(ifa->ifa_netmask)));
346 ifp->if_flags &= ~IFF_RUNNING;
351 funsetown(tp->tap_sigio);
352 selwakeup(&tp->tap_rsel);
354 tp->tap_flags &= ~TAP_OPEN;
360 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
361 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
364 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
365 minor(tp->tap_dev), taprefcnt, taplastunit);
374 * network interface initialization function
380 struct tap_softc *tp = (struct tap_softc *)xtp;
381 struct ifnet *ifp = &tp->tap_if;
383 TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
385 ifp->if_flags |= IFF_RUNNING;
386 ifp->if_flags &= ~IFF_OACTIVE;
388 /* attempt to start output */
396 * Process an ioctl request on network interface
399 tapifioctl(ifp, cmd, data, cr)
405 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
406 struct ifstat *ifs = NULL;
414 dummy = ether_ioctl(ifp, cmd, data);
418 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
425 ifs = (struct ifstat *)data;
426 dummy = strlen(ifs->ascii);
427 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
428 if (tp->tap_td->td_proc) {
429 snprintf(ifs->ascii + dummy,
430 sizeof(ifs->ascii) - dummy,
431 "\tOpened by pid %d\n",
432 (int)tp->tap_td->td_proc->p_pid);
434 snprintf(ifs->ascii + dummy,
435 sizeof(ifs->ascii) - dummy,
436 "\tOpened by td %p\n", tp->tap_td);
453 * queue packets from higher level ready to put out
459 struct tap_softc *tp = ifp->if_softc;
461 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
464 * do not junk pending output if we are in VMnet mode.
465 * XXX: can this do any harm because of queue overflow?
468 if (((tp->tap_flags & TAP_VMNET) == 0) &&
469 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
470 struct mbuf *m = NULL;
472 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
473 minor(tp->tap_dev), tp->tap_flags);
477 IF_DEQUEUE(&ifp->if_snd, m);
489 ifp->if_flags |= IFF_OACTIVE;
491 if (ifp->if_snd.ifq_len != 0) {
492 if (tp->tap_flags & TAP_RWAIT) {
493 tp->tap_flags &= ~TAP_RWAIT;
497 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
498 pgsigio(tp->tap_sigio, SIGIO, 0);
500 selwakeup(&tp->tap_rsel);
501 ifp->if_opackets ++; /* obytes are counted in ether_output */
504 ifp->if_flags &= ~IFF_OACTIVE;
513 * the cdevsw interface is now pretty minimal
516 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
518 struct tap_softc *tp = dev->si_drv1;
519 struct ifnet *ifp = &tp->tap_if;
520 struct tapinfo *tapp = NULL;
525 tapp = (struct tapinfo *)data;
526 ifp->if_mtu = tapp->mtu;
527 ifp->if_type = tapp->type;
528 ifp->if_baudrate = tapp->baudrate;
533 tapp = (struct tapinfo *)data;
534 tapp->mtu = ifp->if_mtu;
535 tapp->type = ifp->if_type;
536 tapp->baudrate = ifp->if_baudrate;
540 tapdebug = *(int *)data;
544 *(int *)data = tapdebug;
553 tp->tap_flags |= TAP_ASYNC;
555 tp->tap_flags &= ~TAP_ASYNC;
561 if (ifp->if_snd.ifq_head) {
562 struct mbuf *mb = ifp->if_snd.ifq_head;
564 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
565 *(int *)data += mb->m_len;
573 return (fsetown(*(int *)data, &tp->tap_sigio));
576 *(int *)data = fgetown(tp->tap_sigio);
579 /* this is deprecated, FIOSETOWN should be used instead */
581 return (fsetown(-(*(int *)data), &tp->tap_sigio));
583 /* this is deprecated, FIOGETOWN should be used instead */
585 *(int *)data = -fgetown(tp->tap_sigio);
588 /* VMware/VMnet port ioctl's */
590 case SIOCGIFFLAGS: /* get ifnet flags */
591 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
594 case VMIO_SIOCSIFFLAGS: { /* VMware/VMnet SIOCSIFFLAGS */
595 short f = *(short *)data;
598 f &= ~IFF_CANTCHANGE;
602 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
606 case OSIOCGIFADDR: /* get MAC address of the remote side */
608 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
611 case SIOCSIFADDR: /* set MAC address of the remote side */
612 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
625 * the cdevsw read interface - reads a packet at a time, or at
626 * least as much of a packet as can be read
629 tapread(dev, uio, flag)
634 struct tap_softc *tp = dev->si_drv1;
635 struct ifnet *ifp = &tp->tap_if;
636 struct mbuf *m0 = NULL;
639 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
641 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
642 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
643 minor(tp->tap_dev), tp->tap_flags);
648 tp->tap_flags &= ~TAP_RWAIT;
650 /* sleep until we get a packet */
653 IF_DEQUEUE(&ifp->if_snd, m0);
657 if (flag & IO_NDELAY)
658 return (EWOULDBLOCK);
660 tp->tap_flags |= TAP_RWAIT;
661 error = tsleep((caddr_t)tp, PCATCH, "taprd", 0);
665 } while (m0 == NULL);
669 /* xfer packet to user space */
670 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
671 len = min(uio->uio_resid, m0->m_len);
675 error = uiomove(mtod(m0, caddr_t), len, uio);
680 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
692 * the cdevsw write interface - an atomic write is a packet - or else!
695 tapwrite(dev, uio, flag)
700 struct tap_softc *tp = dev->si_drv1;
701 struct ifnet *ifp = &tp->tap_if;
702 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
703 int error = 0, tlen, mlen;
705 TAPDEBUG(ifp, "writting, minor = %#x\n", minor(tp->tap_dev));
707 if (uio->uio_resid == 0)
710 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
711 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
712 uio->uio_resid, minor(tp->tap_dev));
716 tlen = uio->uio_resid;
718 /* get a header mbuf */
719 MGETHDR(m, MB_DONTWAIT, MT_DATA);
726 while ((error == 0) && (uio->uio_resid > 0)) {
727 m->m_len = min(mlen, uio->uio_resid);
728 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
731 if (uio->uio_resid > 0) {
732 MGET(m, MB_DONTWAIT, MT_DATA);
747 top->m_pkthdr.len = tlen;
748 top->m_pkthdr.rcvif = ifp;
751 * Ethernet bridge and bpf are handled in ether_input
753 * adjust mbuf and give packet to the ether_input
756 (*ifp->if_input)(ifp, top);
757 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
766 * the poll interface, this is only useful on reads
767 * really. the write detect always returns true, write never blocks
768 * anyway, it either accepts the packet or drops it
771 tappoll(dev_t dev, int events, d_thread_t *td)
773 struct tap_softc *tp = dev->si_drv1;
774 struct ifnet *ifp = &tp->tap_if;
777 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
781 if (events & (POLLIN | POLLRDNORM)) {
782 if (ifp->if_snd.ifq_len > 0) {
784 "has data in queue. len = %d, minor = %#x\n",
785 ifp->if_snd.ifq_len, minor(tp->tap_dev));
787 revents |= (events & (POLLIN | POLLRDNORM));
790 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
793 selrecord(td, &tp->tap_rsel);
797 if (events & (POLLOUT | POLLWRNORM))
798 revents |= (events & (POLLOUT | POLLWRNORM));