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.9 2004/01/06 03:17:27 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/ttycom.h>
57 #include <sys/vnode.h>
60 #include <net/ethernet.h>
62 #include <net/if_arp.h>
63 #include <net/route.h>
65 #include <netinet/in.h>
67 #include "if_tapvar.h"
71 #define CDEV_NAME "tap"
72 #define CDEV_MAJOR 149
73 #define TAPDEBUG if (tapdebug) printf
77 #define VMNET_DEV_MASK 0x00010000
80 static int tapmodevent (module_t, int, void *);
83 static void tapcreate (dev_t);
85 /* network interface */
86 static void tapifstart (struct ifnet *);
87 static int tapifioctl (struct ifnet *, u_long, caddr_t);
88 static void tapifinit (void *);
90 /* character device */
91 static d_open_t tapopen;
92 static d_close_t tapclose;
93 static d_read_t tapread;
94 static d_write_t tapwrite;
95 static d_ioctl_t tapioctl;
96 static d_poll_t tappoll;
98 static struct cdevsw tap_cdevsw = {
99 /* dev name */ CDEV_NAME,
100 /* dev major */ CDEV_MAJOR,
106 /* close */ tapclose,
108 /* write */ tapwrite,
109 /* ioctl */ tapioctl,
112 /* startegy */ nostrategy,
117 static int taprefcnt = 0; /* module ref. counter */
118 static int taplastunit = -1; /* max. open unit number */
119 static int tapdebug = 0; /* debug flag */
121 MALLOC_DECLARE(M_TAP);
122 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
123 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
124 DEV_MODULE(if_tap, tapmodevent, NULL);
129 * module event handler
132 tapmodevent(mod, type, data)
137 static int attached = 0;
138 struct ifnet *ifp = NULL;
146 cdevsw_add(&tap_cdevsw);
154 cdevsw_remove(&tap_cdevsw);
156 /* XXX: maintain tap ifs in a local list */
158 while (unit <= taplastunit) {
160 TAILQ_FOREACH(ifp, &ifnet, if_link)
161 if ((strcmp(ifp->if_dname, TAP) == 0) ||
162 (strcmp(ifp->if_dname, VMNET) == 0))
163 if (ifp->if_dunit == unit)
168 struct tap_softc *tp = ifp->if_softc;
170 TAPDEBUG("detaching %s. minor = %#x, " \
171 "taplastunit = %d\n",
172 ifp->if_xname, minor(tp->tap_dev),
176 ether_ifdetach(ifp, 1);
178 destroy_dev(tp->tap_dev);
199 * to create interface
205 struct ifnet *ifp = NULL;
206 struct tap_softc *tp = NULL;
207 unsigned short macaddr_hi;
211 /* allocate driver storage and create device */
212 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
213 bzero(tp, sizeof(*tp));
215 /* select device: tap or vmnet */
216 if (minor(dev) & VMNET_DEV_MASK) {
218 unit = lminor(dev) & 0xff;
219 tp->tap_flags |= TAP_VMNET;
226 tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
227 0600, "%s%d", name, unit);
228 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
230 /* generate fake MAC address: 00 bd xx xx xx unit_no */
231 macaddr_hi = htons(0x00bd);
232 bcopy(&macaddr_hi, &tp->arpcom.ac_enaddr[0], sizeof(short));
233 bcopy(&ticks, &tp->arpcom.ac_enaddr[2], sizeof(long));
234 tp->arpcom.ac_enaddr[5] = (u_char)unit;
236 /* fill the rest and attach interface */
240 if_initname(ifp, name, unit);
241 if (unit > taplastunit)
244 ifp->if_init = tapifinit;
245 ifp->if_output = ether_output;
246 ifp->if_start = tapifstart;
247 ifp->if_ioctl = tapifioctl;
248 ifp->if_mtu = ETHERMTU;
249 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
250 ifp->if_snd.ifq_maxlen = ifqmaxlen;
253 ether_ifattach(ifp, 1);
256 tp->tap_flags |= TAP_INITED;
258 TAPDEBUG("interface %s created. minor = %#x\n",
259 ifp->if_xname, minor(tp->tap_dev));
266 * to open tunnel. must be superuser
269 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
271 struct tap_softc *tp = NULL;
276 if ((error = suser(td)) != 0)
285 if (tp->tap_flags & TAP_OPEN)
288 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
291 tp->tap_flags |= TAP_OPEN;
294 TAPDEBUG("%s is open. minor = %#x, refcnt = %d, taplastunit = %d\n",
296 minor(tp->tap_dev), taprefcnt, taplastunit);
305 * close the device - mark i/f down & delete routing info
308 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
311 struct tap_softc *tp = dev->si_drv1;
312 struct ifnet *ifp = &tp->tap_if;
313 struct mbuf *m = NULL;
315 /* junk all pending output */
319 IF_DEQUEUE(&ifp->if_snd, m);
326 * do not bring the interface down, and do not anything with
327 * interface, if we are in VMnet mode. just close the device.
330 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
333 if (ifp->if_flags & IFF_RUNNING) {
334 /* find internet addresses and delete routes */
335 struct ifaddr *ifa = NULL;
337 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
338 if (ifa->ifa_addr->sa_family == AF_INET) {
339 rtinit(ifa, (int)RTM_DELETE, 0);
341 /* remove address from interface */
343 sizeof(*(ifa->ifa_addr)));
344 bzero(ifa->ifa_dstaddr,
345 sizeof(*(ifa->ifa_dstaddr)));
346 bzero(ifa->ifa_netmask,
347 sizeof(*(ifa->ifa_netmask)));
351 ifp->if_flags &= ~IFF_RUNNING;
356 funsetown(tp->tap_sigio);
357 selwakeup(&tp->tap_rsel);
359 tp->tap_flags &= ~TAP_OPEN;
365 printf("%s minor = %#x, refcnt = %d is out of sync. " \
366 "set refcnt to 0\n", ifp->if_xname,
367 minor(tp->tap_dev), taprefcnt);
370 TAPDEBUG("%s is closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
371 ifp->if_xname, minor(tp->tap_dev),
372 taprefcnt, taplastunit);
381 * network interface initialization function
387 struct tap_softc *tp = (struct tap_softc *)xtp;
388 struct ifnet *ifp = &tp->tap_if;
390 TAPDEBUG("initializing %s, minor = %#x\n",
391 ifp->if_xname, minor(tp->tap_dev));
393 ifp->if_flags |= IFF_RUNNING;
394 ifp->if_flags &= ~IFF_OACTIVE;
396 /* attempt to start output */
404 * Process an ioctl request on network interface
407 tapifioctl(ifp, cmd, data)
412 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
413 struct ifstat *ifs = NULL;
421 dummy = ether_ioctl(ifp, cmd, data);
425 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
432 ifs = (struct ifstat *)data;
433 dummy = strlen(ifs->ascii);
434 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
435 if (tp->tap_td->td_proc) {
436 snprintf(ifs->ascii + dummy,
437 sizeof(ifs->ascii) - dummy,
438 "\tOpened by pid %d\n",
439 (int)tp->tap_td->td_proc->p_pid);
441 snprintf(ifs->ascii + dummy,
442 sizeof(ifs->ascii) - dummy,
443 "\tOpened by td %p\n", tp->tap_td);
460 * queue packets from higher level ready to put out
466 struct tap_softc *tp = ifp->if_softc;
469 TAPDEBUG("%s starting, minor = %#x\n",
470 ifp->if_xname, minor(tp->tap_dev));
473 * do not junk pending output if we are in VMnet mode.
474 * XXX: can this do any harm because of queue overflow?
477 if (((tp->tap_flags & TAP_VMNET) == 0) &&
478 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
479 struct mbuf *m = NULL;
481 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
483 minor(tp->tap_dev), tp->tap_flags);
487 IF_DEQUEUE(&ifp->if_snd, m);
498 ifp->if_flags |= IFF_OACTIVE;
500 if (ifp->if_snd.ifq_len != 0) {
501 if (tp->tap_flags & TAP_RWAIT) {
502 tp->tap_flags &= ~TAP_RWAIT;
506 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
507 pgsigio(tp->tap_sigio, SIGIO, 0);
509 selwakeup(&tp->tap_rsel);
510 ifp->if_opackets ++; /* obytes are counted in ether_output */
513 ifp->if_flags &= ~IFF_OACTIVE;
521 * the cdevsw interface is now pretty minimal
524 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
526 struct tap_softc *tp = dev->si_drv1;
527 struct ifnet *ifp = &tp->tap_if;
528 struct tapinfo *tapp = NULL;
534 tapp = (struct tapinfo *)data;
535 ifp->if_mtu = tapp->mtu;
536 ifp->if_type = tapp->type;
537 ifp->if_baudrate = tapp->baudrate;
542 tapp = (struct tapinfo *)data;
543 tapp->mtu = ifp->if_mtu;
544 tapp->type = ifp->if_type;
545 tapp->baudrate = ifp->if_baudrate;
549 tapdebug = *(int *)data;
553 *(int *)data = tapdebug;
562 tp->tap_flags |= TAP_ASYNC;
564 tp->tap_flags &= ~TAP_ASYNC;
570 if (ifp->if_snd.ifq_head) {
571 struct mbuf *mb = ifp->if_snd.ifq_head;
573 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
574 *(int *)data += mb->m_len;
582 return (fsetown(*(int *)data, &tp->tap_sigio));
585 *(int *)data = fgetown(tp->tap_sigio);
588 /* this is deprecated, FIOSETOWN should be used instead */
590 return (fsetown(-(*(int *)data), &tp->tap_sigio));
592 /* this is deprecated, FIOGETOWN should be used instead */
594 *(int *)data = -fgetown(tp->tap_sigio);
597 /* VMware/VMnet port ioctl's */
599 case SIOCGIFFLAGS: /* get ifnet flags */
600 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
603 case VMIO_SIOCSIFFLAGS: { /* VMware/VMnet SIOCSIFFLAGS */
604 short f = *(short *)data;
607 f &= ~IFF_CANTCHANGE;
611 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
615 case OSIOCGIFADDR: /* get MAC address of the remote side */
617 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
620 case SIOCSIFADDR: /* set MAC address of the remote side */
621 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
634 * the cdevsw read interface - reads a packet at a time, or at
635 * least as much of a packet as can be read
638 tapread(dev, uio, flag)
643 struct tap_softc *tp = dev->si_drv1;
644 struct ifnet *ifp = &tp->tap_if;
645 struct mbuf *m0 = NULL;
646 int error = 0, len, s;
648 TAPDEBUG("%s reading, minor = %#x\n",
649 ifp->if_xname, minor(tp->tap_dev));
651 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
652 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
654 minor(tp->tap_dev), tp->tap_flags);
659 tp->tap_flags &= ~TAP_RWAIT;
661 /* sleep until we get a packet */
664 IF_DEQUEUE(&ifp->if_snd, m0);
668 if (flag & IO_NDELAY)
669 return (EWOULDBLOCK);
671 tp->tap_flags |= TAP_RWAIT;
672 error = tsleep((caddr_t)tp, PCATCH, "taprd", 0);
676 } while (m0 == NULL);
678 /* feed packet to bpf */
679 if (ifp->if_bpf != NULL)
682 /* xfer packet to user space */
683 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
684 len = min(uio->uio_resid, m0->m_len);
688 error = uiomove(mtod(m0, caddr_t), len, uio);
693 TAPDEBUG("%s dropping mbuf, minor = %#x\n",
694 ifp->if_xname, minor(tp->tap_dev));
705 * the cdevsw write interface - an atomic write is a packet - or else!
708 tapwrite(dev, uio, flag)
713 struct tap_softc *tp = dev->si_drv1;
714 struct ifnet *ifp = &tp->tap_if;
715 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
716 struct ether_header *eh = NULL;
717 int error = 0, tlen, mlen;
719 TAPDEBUG("%s writting, minor = %#x\n",
720 ifp->if_xname, minor(tp->tap_dev));
722 if (uio->uio_resid == 0)
725 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
726 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
728 uio->uio_resid, minor(tp->tap_dev));
732 tlen = uio->uio_resid;
734 /* get a header mbuf */
735 MGETHDR(m, M_DONTWAIT, MT_DATA);
742 while ((error == 0) && (uio->uio_resid > 0)) {
743 m->m_len = min(mlen, uio->uio_resid);
744 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
747 if (uio->uio_resid > 0) {
748 MGET(m, M_DONTWAIT, MT_DATA);
763 top->m_pkthdr.len = tlen;
764 top->m_pkthdr.rcvif = ifp;
767 * Ethernet bridge and bpf are handled in ether_input
769 * adjust mbuf and give packet to the ether_input
772 eh = mtod(top, struct ether_header *);
773 m_adj(top, sizeof(struct ether_header));
774 ether_input(ifp, eh, top);
775 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
784 * the poll interface, this is only useful on reads
785 * really. the write detect always returns true, write never blocks
786 * anyway, it either accepts the packet or drops it
789 tappoll(dev_t dev, int events, d_thread_t *td)
791 struct tap_softc *tp = dev->si_drv1;
792 struct ifnet *ifp = &tp->tap_if;
795 TAPDEBUG("%s polling, minor = %#x\n",
796 ifp->if_xname, minor(tp->tap_dev));
799 if (events & (POLLIN | POLLRDNORM)) {
800 if (ifp->if_snd.ifq_len > 0) {
801 TAPDEBUG("%s have data in queue. len = %d, " \
802 "minor = %#x\n", ifp->if_xname,
803 ifp->if_snd.ifq_len, minor(tp->tap_dev));
805 revents |= (events & (POLLIN | POLLRDNORM));
808 TAPDEBUG("%s waiting for data, minor = %#x\n",
809 ifp->if_xname, minor(tp->tap_dev));
811 selrecord(td, &tp->tap_rsel);
815 if (events & (POLLOUT | POLLWRNORM))
816 revents |= (events & (POLLOUT | POLLWRNORM));