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.35 2007/01/15 00:51:43 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;
102 static d_kqfilter_t tapkqfilter;
104 static struct dev_ops tap_ops = {
105 { CDEV_NAME, CDEV_MAJOR, 0 },
112 .d_kqfilter = tapkqfilter
115 static int taprefcnt = 0; /* module ref. counter */
116 static int taplastunit = -1; /* max. open unit number */
117 static int tapdebug = 0; /* debug flag */
119 MALLOC_DECLARE(M_TAP);
120 MALLOC_DEFINE(M_TAP, CDEV_NAME, "Ethernet tunnel interface");
121 SYSCTL_INT(_debug, OID_AUTO, if_tap_debug, CTLFLAG_RW, &tapdebug, 0, "");
122 DEV_MODULE(if_tap, tapmodevent, NULL);
127 * module event handler
130 tapmodevent(module_t mod, int type, void *data)
132 static int attached = 0;
133 struct ifnet *ifp = NULL;
141 dev_ops_add(&tap_ops, 0, 0);
149 dev_ops_remove(&tap_ops, 0, 0);
151 /* XXX: maintain tap ifs in a local list */
153 while (unit <= taplastunit) {
154 TAILQ_FOREACH(ifp, &ifnet, if_link) {
155 if ((strcmp(ifp->if_dname, TAP) == 0) ||
156 (strcmp(ifp->if_dname, VMNET) == 0)) {
157 if (ifp->if_dunit == unit)
163 struct tap_softc *tp = ifp->if_softc;
165 TAPDEBUG(ifp, "detached. minor = %#x, " \
166 "taplastunit = %d\n",
167 minor(tp->tap_dev), taplastunit);
170 destroy_dev(tp->tap_dev);
191 * to create interface
194 tapcreate(cdev_t dev)
196 struct ifnet *ifp = NULL;
197 struct tap_softc *tp = NULL;
198 uint8_t ether_addr[ETHER_ADDR_LEN];
202 /* allocate driver storage and create device */
203 MALLOC(tp, struct tap_softc *, sizeof(*tp), M_TAP, M_WAITOK);
204 bzero(tp, sizeof(*tp));
206 /* select device: tap or vmnet */
207 if (minor(dev) & VMNET_DEV_MASK) {
209 unit = lminor(dev) & 0xff;
210 tp->tap_flags |= TAP_VMNET;
217 tp->tap_dev = make_dev(&tap_ops, minor(dev), UID_ROOT, GID_WHEEL,
218 0600, "%s%d", name, unit);
219 tp->tap_dev->si_drv1 = dev->si_drv1 = tp;
220 reference_dev(tp->tap_dev); /* so we can destroy it later */
222 /* generate fake MAC address: 00 bd xx xx xx unit_no */
223 ether_addr[0] = 0x00;
224 ether_addr[1] = 0xbd;
225 bcopy(&ticks, ðer_addr[2], 3);
226 ether_addr[5] = (u_char)unit;
228 /* fill the rest and attach interface */
232 if_initname(ifp, name, unit);
233 if (unit > taplastunit)
236 ifp->if_init = tapifinit;
237 ifp->if_start = tapifstart;
238 ifp->if_ioctl = tapifioctl;
239 ifp->if_mtu = ETHERMTU;
240 ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
241 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
242 ifq_set_ready(&ifp->if_snd);
244 ether_ifattach(ifp, ether_addr, NULL);
246 tp->tap_flags |= TAP_INITED;
248 TAPDEBUG(ifp, "created. minor = %#x\n", minor(tp->tap_dev));
255 * to open tunnel. must be superuser
258 tapopen(struct dev_open_args *ap)
260 cdev_t dev = ap->a_head.a_dev;
261 struct tap_softc *tp = NULL;
262 struct ifnet *ifp = NULL;
265 if ((error = suser_cred(ap->a_cred, 0)) != 0)
272 ifp = &tp->arpcom.ac_if;
274 ifp = &tp->arpcom.ac_if;
276 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
278 /* Announce the return of the interface. */
279 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
282 if (tp->tap_flags & TAP_OPEN)
285 bcopy(tp->arpcom.ac_enaddr, tp->ether_addr, sizeof(tp->ether_addr));
287 tp->tap_td = curthread;
288 tp->tap_flags |= TAP_OPEN;
291 TAPDEBUG(ifp, "opened. minor = %#x, refcnt = %d, taplastunit = %d\n",
292 minor(tp->tap_dev), taprefcnt, taplastunit);
301 * close the device - mark i/f down & delete routing info
304 tapclose(struct dev_close_args *ap)
306 cdev_t dev = ap->a_head.a_dev;
307 struct tap_softc *tp = dev->si_drv1;
308 struct ifnet *ifp = &tp->tap_if;
310 /* junk all pending output */
312 lwkt_serialize_enter(ifp->if_serializer);
313 ifq_purge(&ifp->if_snd);
314 lwkt_serialize_exit(ifp->if_serializer);
317 * do not bring the interface down, and do not anything with
318 * interface, if we are in VMnet mode. just close the device.
321 if (((tp->tap_flags & TAP_VMNET) == 0) && (ifp->if_flags & IFF_UP)) {
322 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
324 /* Announce the departure of the interface. */
325 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
328 lwkt_serialize_enter(ifp->if_serializer);
329 if (ifp->if_flags & IFF_RUNNING) {
330 /* find internet addresses and delete routes */
331 struct ifaddr *ifa = NULL;
333 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
334 if (ifa->ifa_addr->sa_family == AF_INET) {
335 rtinit(ifa, (int)RTM_DELETE, 0);
337 /* remove address from interface */
339 sizeof(*(ifa->ifa_addr)));
340 bzero(ifa->ifa_dstaddr,
341 sizeof(*(ifa->ifa_dstaddr)));
342 bzero(ifa->ifa_netmask,
343 sizeof(*(ifa->ifa_netmask)));
347 ifp->if_flags &= ~IFF_RUNNING;
349 lwkt_serialize_exit(ifp->if_serializer);
352 funsetown(tp->tap_sigio);
353 selwakeup(&tp->tap_rsel);
355 tp->tap_flags &= ~TAP_OPEN;
361 if_printf(ifp, "minor = %#x, refcnt = %d is out of sync. "
362 "set refcnt to 0\n", minor(tp->tap_dev), taprefcnt);
365 TAPDEBUG(ifp, "closed. minor = %#x, refcnt = %d, taplastunit = %d\n",
366 minor(tp->tap_dev), taprefcnt, taplastunit);
375 * network interface initialization function
380 struct tap_softc *tp = (struct tap_softc *)xtp;
381 struct ifnet *ifp = &tp->tap_if;
383 TAPDEBUG(ifp, "initializing, minor = %#x\n", minor(tp->tap_dev));
385 ifp->if_flags |= IFF_RUNNING;
386 ifp->if_flags &= ~IFF_OACTIVE;
388 /* attempt to start output */
396 * Process an ioctl request on network interface
401 tapifioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
403 struct tap_softc *tp = (struct tap_softc *)(ifp->if_softc);
404 struct ifstat *ifs = NULL;
411 dummy = ether_ioctl(ifp, cmd, data);
415 if ((tp->tap_flags & TAP_VMNET) == 0) {
417 * Only for non-vmnet tap(4)
419 if (ifp->if_flags & IFF_UP) {
420 if ((ifp->if_flags & IFF_RUNNING) == 0)
425 case SIOCADDMULTI: /* XXX -- just like vmnet does */
430 ifs = (struct ifstat *)data;
431 dummy = strlen(ifs->ascii);
432 if (tp->tap_td != NULL && dummy < sizeof(ifs->ascii)) {
433 if (tp->tap_td->td_proc) {
434 ksnprintf(ifs->ascii + dummy,
435 sizeof(ifs->ascii) - dummy,
436 "\tOpened by pid %d\n",
437 (int)tp->tap_td->td_proc->p_pid);
439 ksnprintf(ifs->ascii + dummy,
440 sizeof(ifs->ascii) - dummy,
441 "\tOpened by td %p\n", tp->tap_td);
457 * queue packets from higher level ready to put out
460 tapifstart(struct ifnet *ifp)
462 struct tap_softc *tp = ifp->if_softc;
464 TAPDEBUG(ifp, "starting, minor = %#x\n", minor(tp->tap_dev));
467 * do not junk pending output if we are in VMnet mode.
468 * XXX: can this do any harm because of queue overflow?
471 if (((tp->tap_flags & TAP_VMNET) == 0) &&
472 ((tp->tap_flags & TAP_READY) != TAP_READY)) {
473 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
474 minor(tp->tap_dev), tp->tap_flags);
476 ifq_purge(&ifp->if_snd);
480 ifp->if_flags |= IFF_OACTIVE;
482 if (!ifq_is_empty(&ifp->if_snd)) {
483 if (tp->tap_flags & TAP_RWAIT) {
484 tp->tap_flags &= ~TAP_RWAIT;
487 KNOTE(&tp->tap_rsel.si_note, 0);
489 if ((tp->tap_flags & TAP_ASYNC) && (tp->tap_sigio != NULL))
490 pgsigio(tp->tap_sigio, SIGIO, 0);
493 * selwakeup is not MPSAFE. tapifstart is.
496 selwakeup(&tp->tap_rsel);
498 ifp->if_opackets ++; /* obytes are counted in ether_output */
501 ifp->if_flags &= ~IFF_OACTIVE;
508 * the ops interface is now pretty minimal
511 tapioctl(struct dev_ioctl_args *ap)
513 cdev_t dev = ap->a_head.a_dev;
514 caddr_t data = ap->a_data;
515 struct tap_softc *tp = dev->si_drv1;
516 struct ifnet *ifp = &tp->tap_if;
517 struct tapinfo *tapp = NULL;
522 lwkt_serialize_enter(ifp->if_serializer);
527 tapp = (struct tapinfo *)data;
528 ifp->if_mtu = tapp->mtu;
529 ifp->if_type = tapp->type;
530 ifp->if_baudrate = tapp->baudrate;
534 tapp = (struct tapinfo *)data;
535 tapp->mtu = ifp->if_mtu;
536 tapp->type = ifp->if_type;
537 tapp->baudrate = ifp->if_baudrate;
541 tapdebug = *(int *)data;
545 *(int *)data = tapdebug;
550 tp->tap_flags |= TAP_ASYNC;
552 tp->tap_flags &= ~TAP_ASYNC;
557 if ((mb = ifq_poll(&ifp->if_snd)) != NULL) {
558 for(; mb != NULL; mb = mb->m_next)
559 *(int *)data += mb->m_len;
564 error = fsetown(*(int *)data, &tp->tap_sigio);
568 *(int *)data = fgetown(tp->tap_sigio);
571 /* this is deprecated, FIOSETOWN should be used instead */
573 error = fsetown(-(*(int *)data), &tp->tap_sigio);
576 /* this is deprecated, FIOGETOWN should be used instead */
578 *(int *)data = -fgetown(tp->tap_sigio);
581 /* VMware/VMnet port ioctl's */
583 case SIOCGIFFLAGS: /* get ifnet flags */
584 bcopy(&ifp->if_flags, data, sizeof(ifp->if_flags));
587 case VMIO_SIOCSIFFLAGS: /* VMware/VMnet SIOCSIFFLAGS */
590 f &= ~IFF_CANTCHANGE;
592 ifp->if_flags = f | (ifp->if_flags & IFF_CANTCHANGE);
595 case OSIOCGIFADDR: /* get MAC address of the remote side */
597 bcopy(tp->ether_addr, data, sizeof(tp->ether_addr));
600 case SIOCSIFADDR: /* set MAC address of the remote side */
601 bcopy(data, tp->ether_addr, sizeof(tp->ether_addr));
608 lwkt_serialize_exit(ifp->if_serializer);
616 * the ops read interface - reads a packet at a time, or at
617 * least as much of a packet as can be read
620 tapread(struct dev_read_args *ap)
622 cdev_t dev = ap->a_head.a_dev;
623 struct uio *uio = ap->a_uio;
624 struct tap_softc *tp = dev->si_drv1;
625 struct ifnet *ifp = &tp->tap_if;
626 struct mbuf *m0 = NULL;
629 TAPDEBUG(ifp, "reading, minor = %#x\n", minor(tp->tap_dev));
631 if ((tp->tap_flags & TAP_READY) != TAP_READY) {
632 TAPDEBUG(ifp, "not ready. minor = %#x, tap_flags = 0x%x\n",
633 minor(tp->tap_dev), tp->tap_flags);
638 tp->tap_flags &= ~TAP_RWAIT;
640 /* sleep until we get a packet */
642 lwkt_serialize_enter(ifp->if_serializer);
643 m0 = ifq_dequeue(&ifp->if_snd, NULL);
645 if (ap->a_ioflag & IO_NDELAY) {
646 lwkt_serialize_exit(ifp->if_serializer);
647 return (EWOULDBLOCK);
649 tp->tap_flags |= TAP_RWAIT;
651 tsleep_interlock(tp);
652 lwkt_serialize_exit(ifp->if_serializer);
653 error = tsleep(tp, PCATCH, "taprd", 0);
658 lwkt_serialize_exit(ifp->if_serializer);
660 } while (m0 == NULL);
664 /* xfer packet to user space */
665 while ((m0 != NULL) && (uio->uio_resid > 0) && (error == 0)) {
666 len = min(uio->uio_resid, m0->m_len);
670 error = uiomove(mtod(m0, caddr_t), len, uio);
675 TAPDEBUG(ifp, "dropping mbuf, minor = %#x\n",
687 * the ops write interface - an atomic write is a packet - or else!
690 tapwrite(struct dev_write_args *ap)
692 cdev_t dev = ap->a_head.a_dev;
693 struct uio *uio = ap->a_uio;
694 struct tap_softc *tp = dev->si_drv1;
695 struct ifnet *ifp = &tp->tap_if;
696 struct mbuf *top = NULL, **mp = NULL, *m = NULL;
697 int error = 0, tlen, mlen;
699 TAPDEBUG(ifp, "writing, minor = %#x\n", minor(tp->tap_dev));
701 if (uio->uio_resid == 0)
704 if ((uio->uio_resid < 0) || (uio->uio_resid > TAPMRU)) {
705 TAPDEBUG(ifp, "invalid packet len = %d, minor = %#x\n",
706 uio->uio_resid, minor(tp->tap_dev));
710 tlen = uio->uio_resid;
712 /* get a header mbuf */
713 MGETHDR(m, MB_DONTWAIT, MT_DATA);
720 while ((error == 0) && (uio->uio_resid > 0)) {
721 m->m_len = min(mlen, uio->uio_resid);
722 error = uiomove(mtod(m, caddr_t), m->m_len, uio);
725 if (uio->uio_resid > 0) {
726 MGET(m, MB_DONTWAIT, MT_DATA);
741 top->m_pkthdr.len = tlen;
742 top->m_pkthdr.rcvif = ifp;
745 * Ethernet bridge and bpf are handled in ether_input
747 * adjust mbuf and give packet to the ether_input
749 lwkt_serialize_enter(ifp->if_serializer);
750 ifp->if_input(ifp, top);
751 ifp->if_ipackets ++; /* ibytes are counted in ether_input */
752 lwkt_serialize_exit(ifp->if_serializer);
761 * the poll interface, this is only useful on reads
762 * really. the write detect always returns true, write never blocks
763 * anyway, it either accepts the packet or drops it
766 tappoll(struct dev_poll_args *ap)
768 cdev_t dev = ap->a_head.a_dev;
769 struct tap_softc *tp = dev->si_drv1;
770 struct ifnet *ifp = &tp->tap_if;
773 TAPDEBUG(ifp, "polling, minor = %#x\n", minor(tp->tap_dev));
775 lwkt_serialize_enter(ifp->if_serializer);
776 if (ap->a_events & (POLLIN | POLLRDNORM)) {
777 if (!ifq_is_empty(&ifp->if_snd)) {
779 "has data in queue. minor = %#x\n",
782 revents |= (ap->a_events & (POLLIN | POLLRDNORM));
785 TAPDEBUG(ifp, "waiting for data, minor = %#x\n",
788 selrecord(curthread, &tp->tap_rsel);
791 lwkt_serialize_exit(ifp->if_serializer);
793 if (ap->a_events & (POLLOUT | POLLWRNORM))
794 revents |= (ap->a_events & (POLLOUT | POLLWRNORM));
795 ap->a_events = revents;
799 static int filt_tapread(struct knote *kn, long hint);
800 static void filt_tapdetach(struct knote *kn);
801 static struct filterops tapread_filtops =
802 { 1, NULL, filt_tapdetach, filt_tapread };
805 tapkqfilter(struct dev_kqfilter_args *ap)
807 cdev_t dev = ap->a_head.a_dev;
808 struct knote *kn = ap->a_kn;
809 struct tap_softc *tp;
818 switch(kn->kn_filter) {
820 list = &tp->tap_rsel.si_note;
821 kn->kn_fop = &tapread_filtops;
822 kn->kn_hook = (void *)tp;
832 SLIST_INSERT_HEAD(list, kn, kn_selnext);
839 filt_tapread(struct knote *kn, long hint)
841 struct tap_softc *tp = (void *)kn->kn_hook;
842 struct ifnet *ifp = &tp->tap_if;
844 if (ifq_is_empty(&ifp->if_snd) == 0) {
852 filt_tapdetach(struct knote *kn)
854 struct tap_softc *tp = (void *)kn->kn_hook;
856 SLIST_REMOVE(&tp->tap_rsel.si_note, kn, knote, kn_selnext);