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.22 2005/11/28 17:13:46 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/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>
59 #include <sys/serialize.h>
62 #include <net/ethernet.h>
64 #include <net/ifq_var.h>
65 #include <net/if_arp.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
70 #include "if_tapvar.h"
74 #define CDEV_NAME "tap"
75 #define CDEV_MAJOR 149
76 #define TAPDEBUG if (tapdebug) if_printf
80 #define VMNET_DEV_MASK 0x00010000
83 static int tapmodevent (module_t, int, void *);
86 static void tapcreate (dev_t);
88 /* network interface */
89 static void tapifstart (struct ifnet *);
90 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 /* dev name */ CDEV_NAME,
104 /* dev major */ CDEV_MAJOR,
110 /* close */ tapclose,
112 /* write */ tapwrite,
113 /* ioctl */ tapioctl,
116 /* startegy */ nostrategy,
121 static int taprefcnt = 0; /* module ref. counter */
122 static int taplastunit = -1; /* max. open unit number */
123 static int tapdebug = 0; /* debug flag */
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);
133 * module event handler
136 tapmodevent(mod, type, data)
141 static int attached = 0;
142 struct ifnet *ifp = NULL;
150 cdevsw_add(&tap_cdevsw, 0, 0);
158 cdevsw_remove(&tap_cdevsw, 0, 0);
160 /* XXX: maintain tap ifs in a local list */
162 while (unit <= taplastunit) {
163 TAILQ_FOREACH(ifp, &ifnet, if_link) {
164 if ((strcmp(ifp->if_dname, TAP) == 0) ||
165 (strcmp(ifp->if_dname, VMNET) == 0)) {
166 if (ifp->if_dunit == unit)
172 struct tap_softc *tp = ifp->if_softc;
174 TAPDEBUG(ifp, "detached. minor = %#x, " \
175 "taplastunit = %d\n",
176 minor(tp->tap_dev), taplastunit);
178 lwkt_serialize_enter(ifp->if_serializer);
180 lwkt_serialize_exit(ifp->if_serializer);
181 destroy_dev(tp->tap_dev);
202 * to create interface
208 struct ifnet *ifp = NULL;
209 struct tap_softc *tp = NULL;
210 uint8_t ether_addr[ETHER_ADDR_LEN];
214 /* allocate driver storage and create device */
215 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
216 bzero(tp, sizeof(*tp));
218 /* select device: tap or vmnet */
219 if (minor(dev) & VMNET_DEV_MASK) {
221 unit = lminor(dev) & 0xff;
222 tp->tap_flags |= TAP_VMNET;
229 tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
230 0600, "%s%d", name, unit);
231 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
232 reference_dev(tp->tap_dev); /* so we can destroy it later */
234 /* generate fake MAC address: 00 bd xx xx xx unit_no */
235 ether_addr[0] = 0x00;
236 ether_addr[1] = 0xbd;
237 bcopy(&ticks, ether_addr, 4);
238 ether_addr[5] = (u_char)unit;
240 /* fill the rest and attach interface */
244 if_initname(ifp, name, unit);
245 if (unit > taplastunit)
248 ifp->if_init = tapifinit;
249 ifp->if_start = tapifstart;
250 ifp->if_ioctl = tapifioctl;
251 ifp->if_mtu = ETHERMTU;
252 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
253 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
254 ifq_set_ready(&ifp->if_snd);
256 ether_ifattach(ifp, ether_addr, NULL);
258 tp->tap_flags |= TAP_INITED;
260 TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
267 * to open tunnel. must be superuser
270 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
272 struct tap_softc *tp = NULL;
275 if ((error = suser(td)) != 0)
284 if (tp->tap_flags & TAP_OPEN)
287 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
290 tp->tap_flags |= TAP_OPEN;
293 TAPDEBUG(&tp->arpcom.ac_if,
294 "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
295 minor(tp->tap_dev), taprefcnt, taplastunit);
304 * close the device - mark i/f down & delete routing info
307 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
309 struct tap_softc *tp = dev->si_drv1;
310 struct ifnet *ifp = &tp->tap_if;
312 /* junk all pending output */
314 lwkt_serialize_enter(ifp->if_serializer);
315 ifq_purge(&ifp->if_snd);
316 lwkt_serialize_exit(ifp->if_serializer);
319 * do not bring the interface down, and do not anything with
320 * interface, if we are in VMnet mode. just close the device.
323 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
325 lwkt_serialize_enter(ifp->if_serializer);
326 if (ifp->if_flags & IFF_RUNNING) {
327 /* find internet addresses and delete routes */
328 struct ifaddr *ifa = NULL;
330 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
331 if (ifa->ifa_addr->sa_family == AF_INET) {
332 rtinit(ifa, (int)RTM_DELETE, 0);
334 /* remove address from interface */
336 sizeof(*(ifa->ifa_addr)));
337 bzero(ifa->ifa_dstaddr,
338 sizeof(*(ifa->ifa_dstaddr)));
339 bzero(ifa->ifa_netmask,
340 sizeof(*(ifa->ifa_netmask)));
344 ifp->if_flags &= ~IFF_RUNNING;
346 lwkt_serialize_exit(ifp->if_serializer);
349 funsetown(tp->tap_sigio);
350 selwakeup(&tp->tap_rsel);
352 tp->tap_flags &= ~TAP_OPEN;
358 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
359 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
362 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
363 minor(tp->tap_dev), taprefcnt, taplastunit);
372 * network interface initialization function
378 struct tap_softc *tp = (struct tap_softc *)xtp;
379 struct ifnet *ifp = &tp->tap_if;
381 TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
383 ifp->if_flags |= IFF_RUNNING;
384 ifp->if_flags &= ~IFF_OACTIVE;
386 /* attempt to start output */
394 * 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;
413 dummy = ether_ioctl(ifp, cmd, data);
416 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
422 ifs = (struct ifstat *)data;
423 dummy = strlen(ifs->ascii);
424 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
425 if (tp->tap_td->td_proc) {
426 snprintf(ifs->ascii + dummy,
427 sizeof(ifs->ascii) - dummy,
428 "\tOpened by pid %d\n",
429 (int)tp->tap_td->td_proc->p_pid);
431 snprintf(ifs->ascii + dummy,
432 sizeof(ifs->ascii) - dummy,
433 "\tOpened by td %p\n", tp->tap_td);
449 * queue packets from higher level ready to put out
455 struct tap_softc *tp = ifp->if_softc;
457 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
460 * do not junk pending output if we are in VMnet mode.
461 * XXX: can this do any harm because of queue overflow?
464 if (((tp->tap_flags & TAP_VMNET) == 0) &&
465 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
466 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
467 minor(tp->tap_dev), tp->tap_flags);
469 ifq_purge(&ifp->if_snd);
473 ifp->if_flags |= IFF_OACTIVE;
475 if (!ifq_is_empty(&ifp->if_snd)) {
476 if (tp->tap_flags & TAP_RWAIT) {
477 tp->tap_flags &= ~TAP_RWAIT;
481 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
482 pgsigio(tp->tap_sigio, SIGIO, 0);
485 * selwakeup is not MPSAFE. tapifstart is.
488 selwakeup(&tp->tap_rsel);
490 ifp->if_opackets ++; /* obytes are counted in ether_output */
493 ifp->if_flags &= ~IFF_OACTIVE;
500 * the cdevsw interface is now pretty minimal
503 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
505 struct tap_softc *tp = dev->si_drv1;
506 struct ifnet *ifp = &tp->tap_if;
507 struct tapinfo *tapp = NULL;
512 lwkt_serialize_enter(ifp->if_serializer);
517 tapp = (struct tapinfo *)data;
518 ifp->if_mtu = tapp->mtu;
519 ifp->if_type = tapp->type;
520 ifp->if_baudrate = tapp->baudrate;
524 tapp = (struct tapinfo *)data;
525 tapp->mtu = ifp->if_mtu;
526 tapp->type = ifp->if_type;
527 tapp->baudrate = ifp->if_baudrate;
531 tapdebug = *(int *)data;
535 *(int *)data = tapdebug;
543 tp->tap_flags |= TAP_ASYNC;
545 tp->tap_flags &= ~TAP_ASYNC;
550 if ((mb = ifq_poll(&ifp->if_snd)) != NULL) {
551 for(; mb != NULL; mb = mb->m_next)
552 *(int *)data += mb->m_len;
557 error = fsetown(*(int *)data, &tp->tap_sigio);
561 *(int *)data = fgetown(tp->tap_sigio);
564 /* this is deprecated, FIOSETOWN should be used instead */
566 error = fsetown(-(*(int *)data), &tp->tap_sigio);
569 /* this is deprecated, FIOGETOWN should be used instead */
571 *(int *)data = -fgetown(tp->tap_sigio);
574 /* VMware/VMnet port ioctl's */
576 case SIOCGIFFLAGS: /* get ifnet flags */
577 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
580 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
583 f &= ~IFF_CANTCHANGE;
585 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
588 case OSIOCGIFADDR: /* get MAC address of the remote side */
590 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
593 case SIOCSIFADDR: /* set MAC address of the remote side */
594 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
601 lwkt_serialize_exit(ifp->if_serializer);
609 * the cdevsw read interface - reads a packet at a time, or at
610 * least as much of a packet as can be read
613 tapread(dev, uio, flag)
618 struct tap_softc *tp = dev->si_drv1;
619 struct ifnet *ifp = &tp->tap_if;
620 struct mbuf *m0 = NULL;
623 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
625 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
626 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
627 minor(tp->tap_dev), tp->tap_flags);
632 tp->tap_flags &= ~TAP_RWAIT;
634 /* sleep until we get a packet */
636 lwkt_serialize_enter(ifp->if_serializer);
637 m0 = ifq_dequeue(&ifp->if_snd, NULL);
639 tp->tap_flags |= TAP_RWAIT;
640 tsleep_interlock(tp);
641 lwkt_serialize_exit(ifp->if_serializer);
642 if (flag & IO_NDELAY)
643 return (EWOULDBLOCK);
644 error = tsleep(tp, PCATCH, "taprd", 0);
648 lwkt_serialize_exit(ifp->if_serializer);
650 } while (m0 == NULL);
654 /* xfer packet to user space */
655 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
656 len = min(uio->uio_resid, m0->m_len);
660 error = uiomove(mtod(m0, caddr_t), len, uio);
665 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
677 * the cdevsw write interface - an atomic write is a packet - or else!
680 tapwrite(dev, uio, flag)
685 struct tap_softc *tp = dev->si_drv1;
686 struct ifnet *ifp = &tp->tap_if;
687 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
688 int error = 0, tlen, mlen;
690 TAPDEBUG(ifp, "writting, minor = %#x\n", minor(tp->tap_dev));
692 if (uio->uio_resid == 0)
695 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
696 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
697 uio->uio_resid, minor(tp->tap_dev));
701 tlen = uio->uio_resid;
703 /* get a header mbuf */
704 MGETHDR(m, MB_DONTWAIT, MT_DATA);
711 while ((error == 0) && (uio->uio_resid > 0)) {
712 m->m_len = min(mlen, uio->uio_resid);
713 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
716 if (uio->uio_resid > 0) {
717 MGET(m, MB_DONTWAIT, MT_DATA);
732 top->m_pkthdr.len = tlen;
733 top->m_pkthdr.rcvif = ifp;
736 * Ethernet bridge and bpf are handled in ether_input
738 * adjust mbuf and give packet to the ether_input
740 lwkt_serialize_enter(ifp->if_serializer);
741 ifp->if_input(ifp, top);
742 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
743 lwkt_serialize_exit(ifp->if_serializer);
752 * the poll interface, this is only useful on reads
753 * really. the write detect always returns true, write never blocks
754 * anyway, it either accepts the packet or drops it
757 tappoll(dev_t dev, int events, d_thread_t *td)
759 struct tap_softc *tp = dev->si_drv1;
760 struct ifnet *ifp = &tp->tap_if;
763 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
765 lwkt_serialize_enter(ifp->if_serializer);
766 if (events & (POLLIN | POLLRDNORM)) {
767 if (!ifq_is_empty(&ifp->if_snd)) {
769 "has data in queue. minor = %#x\n",
772 revents |= (events & (POLLIN | POLLRDNORM));
775 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
778 selrecord(td, &tp->tap_rsel);
781 lwkt_serialize_exit(ifp->if_serializer);
783 if (events & (POLLOUT | POLLWRNORM))
784 revents |= (events & (POLLOUT | POLLWRNORM));