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.29 2006/09/10 01:26:40 dillon 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/device.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.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/thread2.h>
57 #include <sys/ttycom.h>
59 #include <sys/vnode.h>
60 #include <sys/serialize.h>
63 #include <net/ethernet.h>
65 #include <net/ifq_var.h>
66 #include <net/if_arp.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
71 #include "if_tapvar.h"
75 #define CDEV_NAME "tap"
76 #define CDEV_MAJOR 149
77 #define TAPDEBUG if (tapdebug) if_printf
81 #define VMNET_DEV_MASK 0x00010000
84 static int tapmodevent (module_t, int, void *);
87 static void tapcreate (cdev_t);
89 /* network interface */
90 static void tapifstart (struct ifnet *);
91 static int tapifioctl (struct ifnet *, u_long, caddr_t,
93 static void tapifinit (void *);
95 /* character device */
96 static d_open_t tapopen;
97 static d_close_t tapclose;
98 static d_read_t tapread;
99 static d_write_t tapwrite;
100 static d_ioctl_t tapioctl;
101 static d_poll_t tappoll;
103 static struct dev_ops tap_ops = {
104 { CDEV_NAME, CDEV_MAJOR, 0 },
113 static int taprefcnt = 0; /* module ref. counter */
114 static int taplastunit = -1; /* max. open unit number */
115 static int tapdebug = 0; /* debug flag */
117 MALLOC_DECLARE(M_TAP);
118 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
119 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
120 DEV_MODULE(if_tap, tapmodevent, NULL);
125 * module event handler
128 tapmodevent(module_t mod, int type, void *data)
130 static int attached = 0;
131 struct ifnet *ifp = NULL;
139 dev_ops_add(&tap_ops, 0, 0);
147 dev_ops_remove(&tap_ops, 0, 0);
149 /* XXX: maintain tap ifs in a local list */
151 while (unit <= taplastunit) {
152 TAILQ_FOREACH(ifp, &ifnet, if_link) {
153 if ((strcmp(ifp->if_dname, TAP) == 0) ||
154 (strcmp(ifp->if_dname, VMNET) == 0)) {
155 if (ifp->if_dunit == unit)
161 struct tap_softc *tp = ifp->if_softc;
163 TAPDEBUG(ifp, "detached. minor = %#x, " \
164 "taplastunit = %d\n",
165 minor(tp->tap_dev), taplastunit);
168 destroy_dev(tp->tap_dev);
189 * to create interface
192 tapcreate(cdev_t dev)
194 struct ifnet *ifp = NULL;
195 struct tap_softc *tp = NULL;
196 uint8_t ether_addr[ETHER_ADDR_LEN];
200 /* allocate driver storage and create device */
201 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
202 bzero(tp, sizeof(*tp));
204 /* select device: tap or vmnet */
205 if (minor(dev) & VMNET_DEV_MASK) {
207 unit = lminor(dev) & 0xff;
208 tp->tap_flags |= TAP_VMNET;
215 tp->tap_dev = make_dev(&tap_ops, minor(dev), UID_ROOT, GID_WHEEL,
216 0600, "%s%d", name, unit);
217 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
218 reference_dev(tp->tap_dev); /* so we can destroy it later */
220 /* generate fake MAC address: 00 bd xx xx xx unit_no */
221 ether_addr[0] = 0x00;
222 ether_addr[1] = 0xbd;
223 bcopy(&ticks, ether_addr, 4);
224 ether_addr[5] = (u_char)unit;
226 /* fill the rest and attach interface */
230 if_initname(ifp, name, unit);
231 if (unit > taplastunit)
234 ifp->if_init = tapifinit;
235 ifp->if_start = tapifstart;
236 ifp->if_ioctl = tapifioctl;
237 ifp->if_mtu = ETHERMTU;
238 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
239 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
240 ifq_set_ready(&ifp->if_snd);
242 ether_ifattach(ifp, ether_addr, NULL);
244 tp->tap_flags |= TAP_INITED;
246 TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
253 * to open tunnel. must be superuser
256 tapopen(struct dev_open_args *ap)
258 cdev_t dev = ap->a_head.a_dev;
259 struct tap_softc *tp = NULL;
262 if ((error = suser_cred(ap->a_cred, 0)) != 0)
271 if (tp->tap_flags & TAP_OPEN)
274 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
276 tp->tap_td = curthread;
277 tp->tap_flags |= TAP_OPEN;
280 TAPDEBUG(&tp->arpcom.ac_if,
281 "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
282 minor(tp->tap_dev), taprefcnt, taplastunit);
291 * close the device - mark i/f down & delete routing info
294 tapclose(struct dev_close_args *ap)
296 cdev_t dev = ap->a_head.a_dev;
297 struct tap_softc *tp = dev->si_drv1;
298 struct ifnet *ifp = &tp->tap_if;
300 /* junk all pending output */
302 lwkt_serialize_enter(ifp->if_serializer);
303 ifq_purge(&ifp->if_snd);
304 lwkt_serialize_exit(ifp->if_serializer);
307 * do not bring the interface down, and do not anything with
308 * interface, if we are in VMnet mode. just close the device.
311 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
313 lwkt_serialize_enter(ifp->if_serializer);
314 if (ifp->if_flags & IFF_RUNNING) {
315 /* find internet addresses and delete routes */
316 struct ifaddr *ifa = NULL;
318 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
319 if (ifa->ifa_addr->sa_family == AF_INET) {
320 rtinit(ifa, (int)RTM_DELETE, 0);
322 /* remove address from interface */
324 sizeof(*(ifa->ifa_addr)));
325 bzero(ifa->ifa_dstaddr,
326 sizeof(*(ifa->ifa_dstaddr)));
327 bzero(ifa->ifa_netmask,
328 sizeof(*(ifa->ifa_netmask)));
332 ifp->if_flags &= ~IFF_RUNNING;
334 lwkt_serialize_exit(ifp->if_serializer);
337 funsetown(tp->tap_sigio);
338 selwakeup(&tp->tap_rsel);
340 tp->tap_flags &= ~TAP_OPEN;
346 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
347 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
350 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
351 minor(tp->tap_dev), taprefcnt, taplastunit);
360 * network interface initialization function
365 struct tap_softc *tp = (struct tap_softc *)xtp;
366 struct ifnet *ifp = &tp->tap_if;
368 TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
370 ifp->if_flags |= IFF_RUNNING;
371 ifp->if_flags &= ~IFF_OACTIVE;
373 /* attempt to start output */
381 * Process an ioctl request on network interface
386 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
388 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
389 struct ifstat *ifs = NULL;
396 dummy = ether_ioctl(ifp, cmd, data);
399 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
405 ifs = (struct ifstat *)data;
406 dummy = strlen(ifs->ascii);
407 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
408 if (tp->tap_td->td_proc) {
409 snprintf(ifs->ascii + dummy,
410 sizeof(ifs->ascii) - dummy,
411 "\tOpened by pid %d\n",
412 (int)tp->tap_td->td_proc->p_pid);
414 snprintf(ifs->ascii + dummy,
415 sizeof(ifs->ascii) - dummy,
416 "\tOpened by td %p\n", tp->tap_td);
432 * queue packets from higher level ready to put out
435 tapifstart(struct ifnet *ifp)
437 struct tap_softc *tp = ifp->if_softc;
439 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
442 * do not junk pending output if we are in VMnet mode.
443 * XXX: can this do any harm because of queue overflow?
446 if (((tp->tap_flags & TAP_VMNET) == 0) &&
447 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
448 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
449 minor(tp->tap_dev), tp->tap_flags);
451 ifq_purge(&ifp->if_snd);
455 ifp->if_flags |= IFF_OACTIVE;
457 if (!ifq_is_empty(&ifp->if_snd)) {
458 if (tp->tap_flags & TAP_RWAIT) {
459 tp->tap_flags &= ~TAP_RWAIT;
463 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
464 pgsigio(tp->tap_sigio, SIGIO, 0);
467 * selwakeup is not MPSAFE. tapifstart is.
470 selwakeup(&tp->tap_rsel);
472 ifp->if_opackets ++; /* obytes are counted in ether_output */
475 ifp->if_flags &= ~IFF_OACTIVE;
482 * the ops interface is now pretty minimal
485 tapioctl(struct dev_ioctl_args *ap)
487 cdev_t dev = ap->a_head.a_dev;
488 caddr_t data = ap->a_data;
489 struct tap_softc *tp = dev->si_drv1;
490 struct ifnet *ifp = &tp->tap_if;
491 struct tapinfo *tapp = NULL;
496 lwkt_serialize_enter(ifp->if_serializer);
501 tapp = (struct tapinfo *)data;
502 ifp->if_mtu = tapp->mtu;
503 ifp->if_type = tapp->type;
504 ifp->if_baudrate = tapp->baudrate;
508 tapp = (struct tapinfo *)data;
509 tapp->mtu = ifp->if_mtu;
510 tapp->type = ifp->if_type;
511 tapp->baudrate = ifp->if_baudrate;
515 tapdebug = *(int *)data;
519 *(int *)data = tapdebug;
524 tp->tap_flags |= TAP_ASYNC;
526 tp->tap_flags &= ~TAP_ASYNC;
531 if ((mb = ifq_poll(&ifp->if_snd)) != NULL) {
532 for(; mb != NULL; mb = mb->m_next)
533 *(int *)data += mb->m_len;
538 error = fsetown(*(int *)data, &tp->tap_sigio);
542 *(int *)data = fgetown(tp->tap_sigio);
545 /* this is deprecated, FIOSETOWN should be used instead */
547 error = fsetown(-(*(int *)data), &tp->tap_sigio);
550 /* this is deprecated, FIOGETOWN should be used instead */
552 *(int *)data = -fgetown(tp->tap_sigio);
555 /* VMware/VMnet port ioctl's */
557 case SIOCGIFFLAGS: /* get ifnet flags */
558 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
561 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
564 f &= ~IFF_CANTCHANGE;
566 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
569 case OSIOCGIFADDR: /* get MAC address of the remote side */
571 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
574 case SIOCSIFADDR: /* set MAC address of the remote side */
575 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
582 lwkt_serialize_exit(ifp->if_serializer);
590 * the ops read interface - reads a packet at a time, or at
591 * least as much of a packet as can be read
594 tapread(struct dev_read_args *ap)
596 cdev_t dev = ap->a_head.a_dev;
597 struct uio *uio = ap->a_uio;
598 struct tap_softc *tp = dev->si_drv1;
599 struct ifnet *ifp = &tp->tap_if;
600 struct mbuf *m0 = NULL;
603 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
605 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
606 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
607 minor(tp->tap_dev), tp->tap_flags);
612 tp->tap_flags &= ~TAP_RWAIT;
614 /* sleep until we get a packet */
616 lwkt_serialize_enter(ifp->if_serializer);
617 m0 = ifq_dequeue(&ifp->if_snd, NULL);
619 if (ap->a_ioflag & IO_NDELAY) {
620 lwkt_serialize_exit(ifp->if_serializer);
621 return (EWOULDBLOCK);
623 tp->tap_flags |= TAP_RWAIT;
625 tsleep_interlock(tp);
626 lwkt_serialize_exit(ifp->if_serializer);
627 error = tsleep(tp, PCATCH, "taprd", 0);
632 lwkt_serialize_exit(ifp->if_serializer);
634 } while (m0 == NULL);
638 /* xfer packet to user space */
639 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
640 len = min(uio->uio_resid, m0->m_len);
644 error = uiomove(mtod(m0, caddr_t), len, uio);
649 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
661 * the ops write interface - an atomic write is a packet - or else!
664 tapwrite(struct dev_write_args *ap)
666 cdev_t dev = ap->a_head.a_dev;
667 struct uio *uio = ap->a_uio;
668 struct tap_softc *tp = dev->si_drv1;
669 struct ifnet *ifp = &tp->tap_if;
670 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
671 int error = 0, tlen, mlen;
673 TAPDEBUG(ifp, "writting, minor = %#x\n", minor(tp->tap_dev));
675 if (uio->uio_resid == 0)
678 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
679 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
680 uio->uio_resid, minor(tp->tap_dev));
684 tlen = uio->uio_resid;
686 /* get a header mbuf */
687 MGETHDR(m, MB_DONTWAIT, MT_DATA);
694 while ((error == 0) && (uio->uio_resid > 0)) {
695 m->m_len = min(mlen, uio->uio_resid);
696 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
699 if (uio->uio_resid > 0) {
700 MGET(m, MB_DONTWAIT, MT_DATA);
715 top->m_pkthdr.len = tlen;
716 top->m_pkthdr.rcvif = ifp;
719 * Ethernet bridge and bpf are handled in ether_input
721 * adjust mbuf and give packet to the ether_input
723 lwkt_serialize_enter(ifp->if_serializer);
724 ifp->if_input(ifp, top);
725 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
726 lwkt_serialize_exit(ifp->if_serializer);
735 * the poll interface, this is only useful on reads
736 * really. the write detect always returns true, write never blocks
737 * anyway, it either accepts the packet or drops it
740 tappoll(struct dev_poll_args *ap)
742 cdev_t dev = ap->a_head.a_dev;
743 struct tap_softc *tp = dev->si_drv1;
744 struct ifnet *ifp = &tp->tap_if;
747 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
749 lwkt_serialize_enter(ifp->if_serializer);
750 if (ap->a_events & (POLLIN | POLLRDNORM)) {
751 if (!ifq_is_empty(&ifp->if_snd)) {
753 "has data in queue. minor = %#x\n",
756 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
759 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
762 selrecord(curthread, &tp->tap_rsel);
765 lwkt_serialize_exit(ifp->if_serializer);
767 if (ap->a_events & (POLLOUT | POLLWRNORM))
768 revents |= (ap->a_events & (POLLOUT | POLLWRNORM));
769 ap->a_events = revents;