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 * $Id: if_tap.c,v 0.21 2000/07/23 21:46:02 max Exp $
40 #include <sys/param.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
54 #include <sys/ttycom.h>
56 #include <sys/vnode.h>
59 #include <net/ethernet.h>
61 #include <net/if_arp.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
66 #include <net/if_tapvar.h>
67 #include <net/if_tap.h>
70 #define CDEV_NAME "tap"
71 #define CDEV_MAJOR 149
72 #define TAPDEBUG if (tapdebug) printf
76 #define VMNET_DEV_MASK 0x00010000
79 static int tapmodevent __P((module_t, int, void *));
82 static void tapcreate __P((dev_t));
84 /* network interface */
85 static void tapifstart __P((struct ifnet *));
86 static int tapifioctl __P((struct ifnet *, u_long, caddr_t));
87 static void tapifinit __P((void *));
89 /* character device */
90 static d_open_t tapopen;
91 static d_close_t tapclose;
92 static d_read_t tapread;
93 static d_write_t tapwrite;
94 static d_ioctl_t tapioctl;
95 static d_poll_t tappoll;
97 static struct cdevsw tap_cdevsw = {
101 /* write */ tapwrite,
102 /* ioctl */ tapioctl,
105 /* startegy */ nostrategy,
106 /* dev name */ CDEV_NAME,
107 /* dev major */ CDEV_MAJOR,
114 static int taprefcnt = 0; /* module ref. counter */
115 static int taplastunit = -1; /* max. open unit number */
116 static int tapdebug = 0; /* debug flag */
118 MALLOC_DECLARE(M_TAP);
119 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
120 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
121 DEV_MODULE(if_tap, tapmodevent, NULL);
126 * module event handler
129 tapmodevent(mod, type, data)
134 static int attached = 0;
135 struct ifnet *ifp = NULL;
143 cdevsw_add(&tap_cdevsw);
151 cdevsw_remove(&tap_cdevsw);
154 while (unit <= taplastunit) {
156 TAILQ_FOREACH(ifp, &ifnet, if_link)
157 if ((strcmp(ifp->if_name, TAP) == 0) ||
158 (strcmp(ifp->if_name, VMNET) == 0))
159 if (ifp->if_unit == unit)
164 struct tap_softc *tp = ifp->if_softc;
166 TAPDEBUG("detaching %s%d. minor = %#x, " \
167 "taplastunit = %d\n",
168 ifp->if_name, unit, minor(tp->tap_dev),
172 ether_ifdetach(ifp, 1);
174 destroy_dev(tp->tap_dev);
195 * to create interface
201 struct ifnet *ifp = NULL;
202 struct tap_softc *tp = NULL;
203 unsigned short macaddr_hi;
207 /* allocate driver storage and create device */
208 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
209 bzero(tp, sizeof(*tp));
211 /* select device: tap or vmnet */
212 if (minor(dev) & VMNET_DEV_MASK) {
214 unit = lminor(dev) & 0xff;
215 tp->tap_flags |= TAP_VMNET;
222 tp->tap_dev = make_dev(&tap_cdevsw, minor(dev), UID_ROOT, GID_WHEEL,
223 0600, "%s%d", name, unit);
224 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
226 /* generate fake MAC address: 00 bd xx xx xx unit_no */
227 macaddr_hi = htons(0x00bd);
228 bcopy(&macaddr_hi, &tp->arpcom.ac_enaddr[0], sizeof(short));
229 bcopy(&ticks, &tp->arpcom.ac_enaddr[2], sizeof(long));
230 tp->arpcom.ac_enaddr[5] = (u_char)unit;
232 /* fill the rest and attach interface */
237 if (unit > taplastunit)
241 ifp->if_init = tapifinit;
242 ifp->if_output = ether_output;
243 ifp->if_start = tapifstart;
244 ifp->if_ioctl = tapifioctl;
245 ifp->if_mtu = ETHERMTU;
246 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
247 ifp->if_snd.ifq_maxlen = ifqmaxlen;
250 ether_ifattach(ifp, 1);
253 tp->tap_flags |= TAP_INITED;
255 TAPDEBUG("interface %s%d created. minor = %#x\n",
256 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
263 * to open tunnel. must be superuser
266 tapopen(dev, flag, mode, p)
272 struct tap_softc *tp = NULL;
275 if ((error = suser(p)) != 0)
284 if (tp->tap_flags & TAP_OPEN)
287 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
289 tp->tap_pid = p->p_pid;
290 tp->tap_flags |= TAP_OPEN;
293 TAPDEBUG("%s%d is open. minor = %#x, refcnt = %d, taplastunit = %d\n",
294 tp->tap_if.if_name, tp->tap_if.if_unit,
295 minor(tp->tap_dev), taprefcnt, taplastunit);
304 * close the device - mark i/f down & delete routing info
307 tapclose(dev, foo, bar, p)
314 struct tap_softc *tp = dev->si_drv1;
315 struct ifnet *ifp = &tp->tap_if;
316 struct mbuf *m = NULL;
318 /* junk all pending output */
322 IF_DEQUEUE(&ifp->if_snd, m);
329 * do not bring the interface down, and do not anything with
330 * interface, if we are in VMnet mode. just close the device.
333 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
336 if (ifp->if_flags & IFF_RUNNING) {
337 /* find internet addresses and delete routes */
338 struct ifaddr *ifa = NULL;
340 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
341 if (ifa->ifa_addr->sa_family == AF_INET) {
342 rtinit(ifa, (int)RTM_DELETE, 0);
344 /* remove address from interface */
346 sizeof(*(ifa->ifa_addr)));
347 bzero(ifa->ifa_dstaddr,
348 sizeof(*(ifa->ifa_dstaddr)));
349 bzero(ifa->ifa_netmask,
350 sizeof(*(ifa->ifa_netmask)));
354 ifp->if_flags &= ~IFF_RUNNING;
359 funsetown(tp->tap_sigio);
360 selwakeup(&tp->tap_rsel);
362 tp->tap_flags &= ~TAP_OPEN;
368 printf("%s%d minor = %#x, refcnt = %d is out of sync. " \
369 "set refcnt to 0\n", ifp->if_name, ifp->if_unit,
370 minor(tp->tap_dev), taprefcnt);
373 TAPDEBUG("%s%d is closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
374 ifp->if_name, ifp->if_unit, minor(tp->tap_dev),
375 taprefcnt, taplastunit);
384 * network interface initialization function
390 struct tap_softc *tp = (struct tap_softc *)xtp;
391 struct ifnet *ifp = &tp->tap_if;
393 TAPDEBUG("initializing %s%d, minor = %#x\n",
394 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
396 ifp->if_flags |= IFF_RUNNING;
397 ifp->if_flags &= ~IFF_OACTIVE;
399 /* attempt to start output */
407 * Process an ioctl request on network interface
410 tapifioctl(ifp, cmd, data)
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_pid != 0 && dummy < sizeof(ifs->ascii))
438 snprintf(ifs->ascii + dummy,
439 sizeof(ifs->ascii) - dummy,
440 "\tOpened by PID %d\n", tp->tap_pid);
455 * queue packets from higher level ready to put out
461 struct tap_softc *tp = ifp->if_softc;
464 TAPDEBUG("%s%d starting, minor = %#x\n",
465 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
468 * do not junk pending output if we are in VMnet mode.
469 * XXX: can this do any harm because of queue overflow?
472 if (((tp->tap_flags & TAP_VMNET) == 0) &&
473 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
474 struct mbuf *m = NULL;
476 TAPDEBUG("%s%d not ready. minor = %#x, tap_flags = 0x%x\n",
477 ifp->if_name, ifp->if_unit,
478 minor(tp->tap_dev), tp->tap_flags);
482 IF_DEQUEUE(&ifp->if_snd, m);
493 ifp->if_flags |= IFF_OACTIVE;
495 if (ifp->if_snd.ifq_len != 0) {
496 if (tp->tap_flags & TAP_RWAIT) {
497 tp->tap_flags &= ~TAP_RWAIT;
501 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
502 pgsigio(tp->tap_sigio, SIGIO, 0);
504 selwakeup(&tp->tap_rsel);
505 ifp->if_opackets ++; /* obytes are counted in ether_output */
508 ifp->if_flags &= ~IFF_OACTIVE;
516 * the cdevsw interface is now pretty minimal
519 tapioctl(dev, cmd, data, flag, p)
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%d reading, minor = %#x\n",
649 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
651 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
652 TAPDEBUG("%s%d not ready. minor = %#x, tap_flags = 0x%x\n",
653 ifp->if_name, ifp->if_unit,
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|(PZERO+1),"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%d dropping mbuf, minor = %#x\n",
694 ifp->if_name, ifp->if_unit, 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%d writting, minor = %#x\n",
720 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
722 if (uio->uio_resid == 0)
725 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
726 TAPDEBUG("%s%d invalid packet len = %d, minor = %#x\n",
727 ifp->if_name, ifp->if_unit,
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, events, p)
794 struct tap_softc *tp = dev->si_drv1;
795 struct ifnet *ifp = &tp->tap_if;
798 TAPDEBUG("%s%d polling, minor = %#x\n",
799 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
802 if (events & (POLLIN | POLLRDNORM)) {
803 if (ifp->if_snd.ifq_len > 0) {
804 TAPDEBUG("%s%d have data in queue. len = %d, " \
805 "minor = %#x\n", ifp->if_name, ifp->if_unit,
806 ifp->if_snd.ifq_len, minor(tp->tap_dev));
808 revents |= (events & (POLLIN | POLLRDNORM));
811 TAPDEBUG("%s%d waiting for data, minor = %#x\n",
812 ifp->if_name, ifp->if_unit, minor(tp->tap_dev));
814 selrecord(p, &tp->tap_rsel);
818 if (events & (POLLOUT | POLLWRNORM))
819 revents |= (events & (POLLOUT | POLLWRNORM));