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.14 2004/06/02 14:42:59 eirikn 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,
89 static void tapifinit (void *);
91 /* character device */
92 static d_open_t tapopen;
93 static d_close_t tapclose;
94 static d_read_t tapread;
95 static d_write_t tapwrite;
96 static d_ioctl_t tapioctl;
97 static d_poll_t tappoll;
99 static struct cdevsw tap_cdevsw = {
100 /* dev name */ CDEV_NAME,
101 /* dev major */ CDEV_MAJOR,
107 /* close */ tapclose,
109 /* write */ tapwrite,
110 /* ioctl */ tapioctl,
113 /* startegy */ nostrategy,
118 static int taprefcnt = 0; /* module ref. counter */
119 static int taplastunit = -1; /* max. open unit number */
120 static int tapdebug = 0; /* debug flag */
122 MALLOC_DECLARE(M_TAP);
123 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
124 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
125 DEV_MODULE(if_tap, tapmodevent, NULL);
130 * module event handler
133 tapmodevent(mod, type, data)
138 static int attached = 0;
139 struct ifnet *ifp = NULL;
147 cdevsw_add(&tap_cdevsw, 0, 0);
155 cdevsw_remove(&tap_cdevsw, 0, 0);
157 /* XXX: maintain tap ifs in a local list */
159 while (unit <= taplastunit) {
161 TAILQ_FOREACH(ifp, &ifnet, if_link)
162 if ((strcmp(ifp->if_dname, TAP) == 0) ||
163 (strcmp(ifp->if_dname, VMNET) == 0))
164 if (ifp->if_dunit == unit)
169 struct tap_softc *tp = ifp->if_softc;
171 TAPDEBUG("detaching %s. minor = %#x, " \
172 "taplastunit = %d\n",
173 ifp->if_xname, minor(tp->tap_dev),
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_output = ether_output;
248 ifp->if_start = tapifstart;
249 ifp->if_ioctl = tapifioctl;
250 ifp->if_mtu = ETHERMTU;
251 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
252 ifp->if_snd.ifq_maxlen = ifqmaxlen;
255 ether_ifattach(ifp, ether_addr);
258 tp->tap_flags |= TAP_INITED;
260 TAPDEBUG("interface %s created. minor = %#x\n",
261 ifp->if_xname, minor(tp->tap_dev));
268 * to open tunnel. must be superuser
271 tapopen(dev_t dev, int flag, int mode, d_thread_t *td)
273 struct tap_softc *tp = NULL;
278 if ((error = suser(td)) != 0)
287 if (tp->tap_flags & TAP_OPEN)
290 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
293 tp->tap_flags |= TAP_OPEN;
296 TAPDEBUG("%s is open. minor = %#x, refcnt = %d, taplastunit = %d\n",
298 minor(tp->tap_dev), taprefcnt, taplastunit);
307 * close the device - mark i/f down & delete routing info
310 tapclose(dev_t dev, int foo, int bar, d_thread_t *td)
313 struct tap_softc *tp = dev->si_drv1;
314 struct ifnet *ifp = &tp->tap_if;
315 struct mbuf *m = NULL;
317 /* junk all pending output */
321 IF_DEQUEUE(&ifp->if_snd, m);
328 * do not bring the interface down, and do not anything with
329 * interface, if we are in VMnet mode. just close the device.
332 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
335 if (ifp->if_flags & IFF_RUNNING) {
336 /* find internet addresses and delete routes */
337 struct ifaddr *ifa = NULL;
339 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
340 if (ifa->ifa_addr->sa_family == AF_INET) {
341 rtinit(ifa, (int)RTM_DELETE, 0);
343 /* remove address from interface */
345 sizeof(*(ifa->ifa_addr)));
346 bzero(ifa->ifa_dstaddr,
347 sizeof(*(ifa->ifa_dstaddr)));
348 bzero(ifa->ifa_netmask,
349 sizeof(*(ifa->ifa_netmask)));
353 ifp->if_flags &= ~IFF_RUNNING;
358 funsetown(tp->tap_sigio);
359 selwakeup(&tp->tap_rsel);
361 tp->tap_flags &= ~TAP_OPEN;
367 printf("%s minor = %#x, refcnt = %d is out of sync. " \
368 "set refcnt to 0\n", ifp->if_xname,
369 minor(tp->tap_dev), taprefcnt);
372 TAPDEBUG("%s is closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
373 ifp->if_xname, minor(tp->tap_dev),
374 taprefcnt, taplastunit);
383 * network interface initialization function
389 struct tap_softc *tp = (struct tap_softc *)xtp;
390 struct ifnet *ifp = &tp->tap_if;
392 TAPDEBUG("initializing %s, minor = %#x\n",
393 ifp->if_xname, minor(tp->tap_dev));
395 ifp->if_flags |= IFF_RUNNING;
396 ifp->if_flags &= ~IFF_OACTIVE;
398 /* attempt to start output */
406 * Process an ioctl request on network interface
409 tapifioctl(ifp, cmd, data, cr)
415 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
416 struct ifstat *ifs = NULL;
424 dummy = ether_ioctl(ifp, cmd, data);
428 case SIOCSIFFLAGS: /* XXX -- just like vmnet does */
435 ifs = (struct ifstat *)data;
436 dummy = strlen(ifs->ascii);
437 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
438 if (tp->tap_td->td_proc) {
439 snprintf(ifs->ascii + dummy,
440 sizeof(ifs->ascii) - dummy,
441 "\tOpened by pid %d\n",
442 (int)tp->tap_td->td_proc->p_pid);
444 snprintf(ifs->ascii + dummy,
445 sizeof(ifs->ascii) - dummy,
446 "\tOpened by td %p\n", tp->tap_td);
463 * queue packets from higher level ready to put out
469 struct tap_softc *tp = ifp->if_softc;
472 TAPDEBUG("%s starting, minor = %#x\n",
473 ifp->if_xname, minor(tp->tap_dev));
476 * do not junk pending output if we are in VMnet mode.
477 * XXX: can this do any harm because of queue overflow?
480 if (((tp->tap_flags & TAP_VMNET) == 0) &&
481 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
482 struct mbuf *m = NULL;
484 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
486 minor(tp->tap_dev), tp->tap_flags);
490 IF_DEQUEUE(&ifp->if_snd, m);
501 ifp->if_flags |= IFF_OACTIVE;
503 if (ifp->if_snd.ifq_len != 0) {
504 if (tp->tap_flags & TAP_RWAIT) {
505 tp->tap_flags &= ~TAP_RWAIT;
509 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
510 pgsigio(tp->tap_sigio, SIGIO, 0);
512 selwakeup(&tp->tap_rsel);
513 ifp->if_opackets ++; /* obytes are counted in ether_output */
516 ifp->if_flags &= ~IFF_OACTIVE;
524 * the cdevsw interface is now pretty minimal
527 tapioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
529 struct tap_softc *tp = dev->si_drv1;
530 struct ifnet *ifp = &tp->tap_if;
531 struct tapinfo *tapp = NULL;
537 tapp = (struct tapinfo *)data;
538 ifp->if_mtu = tapp->mtu;
539 ifp->if_type = tapp->type;
540 ifp->if_baudrate = tapp->baudrate;
545 tapp = (struct tapinfo *)data;
546 tapp->mtu = ifp->if_mtu;
547 tapp->type = ifp->if_type;
548 tapp->baudrate = ifp->if_baudrate;
552 tapdebug = *(int *)data;
556 *(int *)data = tapdebug;
565 tp->tap_flags |= TAP_ASYNC;
567 tp->tap_flags &= ~TAP_ASYNC;
573 if (ifp->if_snd.ifq_head) {
574 struct mbuf *mb = ifp->if_snd.ifq_head;
576 for(*(int *)data = 0;mb != NULL;mb = mb->m_next)
577 *(int *)data += mb->m_len;
585 return (fsetown(*(int *)data, &tp->tap_sigio));
588 *(int *)data = fgetown(tp->tap_sigio);
591 /* this is deprecated, FIOSETOWN should be used instead */
593 return (fsetown(-(*(int *)data), &tp->tap_sigio));
595 /* this is deprecated, FIOGETOWN should be used instead */
597 *(int *)data = -fgetown(tp->tap_sigio);
600 /* VMware/VMnet port ioctl's */
602 case SIOCGIFFLAGS: /* get ifnet flags */
603 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
606 case VMIO_SIOCSIFFLAGS: { /* VMware/VMnet SIOCSIFFLAGS */
607 short f = *(short *)data;
610 f &= ~IFF_CANTCHANGE;
614 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
618 case OSIOCGIFADDR: /* get MAC address of the remote side */
620 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
623 case SIOCSIFADDR: /* set MAC address of the remote side */
624 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
637 * the cdevsw read interface - reads a packet at a time, or at
638 * least as much of a packet as can be read
641 tapread(dev, uio, flag)
646 struct tap_softc *tp = dev->si_drv1;
647 struct ifnet *ifp = &tp->tap_if;
648 struct mbuf *m0 = NULL;
649 int error = 0, len, s;
651 TAPDEBUG("%s reading, minor = %#x\n",
652 ifp->if_xname, minor(tp->tap_dev));
654 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
655 TAPDEBUG("%s not ready. minor = %#x, tap_flags = 0x%x\n",
657 minor(tp->tap_dev), tp->tap_flags);
662 tp->tap_flags &= ~TAP_RWAIT;
664 /* sleep until we get a packet */
667 IF_DEQUEUE(&ifp->if_snd, m0);
671 if (flag & IO_NDELAY)
672 return (EWOULDBLOCK);
674 tp->tap_flags |= TAP_RWAIT;
675 error = tsleep((caddr_t)tp, PCATCH, "taprd", 0);
679 } while (m0 == NULL);
681 /* feed packet to bpf */
682 if (ifp->if_bpf != NULL)
685 /* xfer packet to user space */
686 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
687 len = min(uio->uio_resid, m0->m_len);
691 error = uiomove(mtod(m0, caddr_t), len, uio);
696 TAPDEBUG("%s dropping mbuf, minor = %#x\n",
697 ifp->if_xname, minor(tp->tap_dev));
708 * the cdevsw write interface - an atomic write is a packet - or else!
711 tapwrite(dev, uio, flag)
716 struct tap_softc *tp = dev->si_drv1;
717 struct ifnet *ifp = &tp->tap_if;
718 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
719 struct ether_header *eh = NULL;
720 int error = 0, tlen, mlen;
722 TAPDEBUG("%s writting, minor = %#x\n",
723 ifp->if_xname, minor(tp->tap_dev));
725 if (uio->uio_resid == 0)
728 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
729 TAPDEBUG("%s invalid packet len = %d, minor = %#x\n",
731 uio->uio_resid, minor(tp->tap_dev));
735 tlen = uio->uio_resid;
737 /* get a header mbuf */
738 MGETHDR(m, MB_DONTWAIT, MT_DATA);
745 while ((error == 0) && (uio->uio_resid > 0)) {
746 m->m_len = min(mlen, uio->uio_resid);
747 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
750 if (uio->uio_resid > 0) {
751 MGET(m, MB_DONTWAIT, MT_DATA);
766 top->m_pkthdr.len = tlen;
767 top->m_pkthdr.rcvif = ifp;
770 * Ethernet bridge and bpf are handled in ether_input
772 * adjust mbuf and give packet to the ether_input
775 eh = mtod(top, struct ether_header *);
776 m_adj(top, sizeof(struct ether_header));
777 ether_input(ifp, eh, top);
778 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
787 * the poll interface, this is only useful on reads
788 * really. the write detect always returns true, write never blocks
789 * anyway, it either accepts the packet or drops it
792 tappoll(dev_t dev, int events, d_thread_t *td)
794 struct tap_softc *tp = dev->si_drv1;
795 struct ifnet *ifp = &tp->tap_if;
798 TAPDEBUG("%s polling, minor = %#x\n",
799 ifp->if_xname, minor(tp->tap_dev));
802 if (events & (POLLIN | POLLRDNORM)) {
803 if (ifp->if_snd.ifq_len > 0) {
804 TAPDEBUG("%s have data in queue. len = %d, " \
805 "minor = %#x\n", ifp->if_xname,
806 ifp->if_snd.ifq_len, minor(tp->tap_dev));
808 revents |= (events & (POLLIN | POLLRDNORM));
811 TAPDEBUG("%s waiting for data, minor = %#x\n",
812 ifp->if_xname, minor(tp->tap_dev));
814 selrecord(td, &tp->tap_rsel);
818 if (events & (POLLOUT | POLLWRNORM))
819 revents |= (events & (POLLOUT | POLLWRNORM));