1 /* $NetBSD: if_tap.c,v 1.61 2009/12/09 21:32:59 dsl Exp $ */
4 * Copyright (c) 2003, 2004, 2008, 2009 The NetBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * tap(4) is a virtual Ethernet interface. It appears as a real Ethernet
31 * device to the system, but can also be accessed by userland through a
32 * character device interface, which allows reading and injecting frames.
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.61 2009/12/09 21:32:59 dsl Exp $");
38 #if defined(_KERNEL_OPT)
40 #include "opt_modular.h"
41 #include "opt_compat_netbsd.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/device.h>
51 #include <sys/filedesc.h>
52 #include <sys/ksyms.h>
55 #include <sys/select.h>
56 #include <sys/sockio.h>
57 #if defined(COMPAT_40) || defined(MODULAR)
58 #include <sys/sysctl.h>
60 #include <sys/kauth.h>
61 #include <sys/mutex.h>
62 #include <sys/simplelock.h>
67 #include <net/if_dl.h>
68 #include <net/if_ether.h>
69 #include <net/if_media.h>
70 #include <net/if_tap.h>
75 #include <compat/sys/sockio.h>
77 #if defined(COMPAT_40) || defined(MODULAR)
79 * sysctl node management
81 * It's not really possible to use a SYSCTL_SETUP block with
82 * current module implementation, so it is easier to just define
85 * The handler function is a "helper" in Andrew Brown's sysctl
86 * framework terminology. It is used as a gateway for sysctl
87 * requests over the nodes.
89 * tap_log allows the module to log creations of nodes and
90 * destroy them all at once using sysctl_teardown.
93 static int tap_sysctl_handler(SYSCTLFN_PROTO
);
94 SYSCTL_SETUP_PROTO(sysctl_tap_setup
);
98 * Since we're an Ethernet device, we need the 3 following
99 * components: a leading struct device, a struct ethercom,
100 * and also a struct ifmedia since we don't attach a PHY to
101 * ourselves. We could emulate one, but there's no real
107 struct ifmedia sc_im
;
108 struct ethercom sc_ec
;
110 #define TAP_INUSE 0x00000001 /* tap device can only be opened once */
111 #define TAP_ASYNCIO 0x00000002 /* user is using async I/O (SIGIO) on the device */
112 #define TAP_NBIO 0x00000004 /* user wants calls to avoid blocking */
113 #define TAP_GOING 0x00000008 /* interface is being destroyed */
114 struct selinfo sc_rsel
;
115 pid_t sc_pgid
; /* For async. IO */
117 struct simplelock sc_kqlock
;
119 struct timespec sc_atime
;
120 struct timespec sc_mtime
;
121 struct timespec sc_btime
;
124 /* autoconf(9) glue */
128 static int tap_match(device_t
, cfdata_t
, void *);
129 static void tap_attach(device_t
, device_t
, void *);
130 static int tap_detach(device_t
, int);
132 CFATTACH_DECL_NEW(tap
, sizeof(struct tap_softc
),
133 tap_match
, tap_attach
, tap_detach
, NULL
);
134 extern struct cfdriver tap_cd
;
136 /* Real device access routines */
137 static int tap_dev_close(struct tap_softc
*);
138 static int tap_dev_read(int, struct uio
*, int);
139 static int tap_dev_write(int, struct uio
*, int);
140 static int tap_dev_ioctl(int, u_long
, void *, struct lwp
*);
141 static int tap_dev_poll(int, int, struct lwp
*);
142 static int tap_dev_kqfilter(int, struct knote
*);
144 /* Fileops access routines */
145 static int tap_fops_close(file_t
*);
146 static int tap_fops_read(file_t
*, off_t
*, struct uio
*,
148 static int tap_fops_write(file_t
*, off_t
*, struct uio
*,
150 static int tap_fops_ioctl(file_t
*, u_long
, void *);
151 static int tap_fops_poll(file_t
*, int);
152 static int tap_fops_stat(file_t
*, struct stat
*);
153 static int tap_fops_kqfilter(file_t
*, struct knote
*);
155 static const struct fileops tap_fileops
= {
156 .fo_read
= tap_fops_read
,
157 .fo_write
= tap_fops_write
,
158 .fo_ioctl
= tap_fops_ioctl
,
159 .fo_fcntl
= fnullop_fcntl
,
160 .fo_poll
= tap_fops_poll
,
161 .fo_stat
= tap_fops_stat
,
162 .fo_close
= tap_fops_close
,
163 .fo_kqfilter
= tap_fops_kqfilter
,
164 .fo_restart
= fnullop_restart
,
167 /* Helper for cloning open() */
168 static int tap_dev_cloner(struct lwp
*);
170 /* Character device routines */
171 static int tap_cdev_open(dev_t
, int, int, struct lwp
*);
172 static int tap_cdev_close(dev_t
, int, int, struct lwp
*);
173 static int tap_cdev_read(dev_t
, struct uio
*, int);
174 static int tap_cdev_write(dev_t
, struct uio
*, int);
175 static int tap_cdev_ioctl(dev_t
, u_long
, void *, int, struct lwp
*);
176 static int tap_cdev_poll(dev_t
, int, struct lwp
*);
177 static int tap_cdev_kqfilter(dev_t
, struct knote
*);
179 const struct cdevsw tap_cdevsw
= {
180 tap_cdev_open
, tap_cdev_close
,
181 tap_cdev_read
, tap_cdev_write
,
182 tap_cdev_ioctl
, nostop
, notty
,
183 tap_cdev_poll
, nommap
,
188 #define TAP_CLONER 0xfffff /* Maximal minor value */
190 /* kqueue-related routines */
191 static void tap_kqdetach(struct knote
*);
192 static int tap_kqread(struct knote
*, long);
195 * Those are needed by the if_media interface.
198 static int tap_mediachange(struct ifnet
*);
199 static void tap_mediastatus(struct ifnet
*, struct ifmediareq
*);
202 * Those are needed by the ifnet interface, and would typically be
203 * there for any network interface driver.
204 * Some other routines are optional: watchdog and drain.
207 static void tap_start(struct ifnet
*);
208 static void tap_stop(struct ifnet
*, int);
209 static int tap_init(struct ifnet
*);
210 static int tap_ioctl(struct ifnet
*, u_long
, void *);
212 /* Internal functions */
213 #if defined(COMPAT_40) || defined(MODULAR)
214 static int tap_lifaddr(struct ifnet
*, u_long
, struct ifaliasreq
*);
216 static void tap_softintr(void *);
219 * tap is a clonable interface, although it is highly unrealistic for
220 * an Ethernet device.
222 * Here are the bits needed for a clonable interface.
224 static int tap_clone_create(struct if_clone
*, int);
225 static int tap_clone_destroy(struct ifnet
*);
227 struct if_clone tap_cloners
= IF_CLONE_INITIALIZER("tap",
231 /* Helper functionis shared by the two cloning code paths */
232 static struct tap_softc
* tap_clone_creator(int);
233 int tap_clone_destroyer(device_t
);
240 error
= config_cfattach_attach(tap_cd
.cd_name
, &tap_ca
);
242 aprint_error("%s: unable to register cfattach\n",
244 (void)config_cfdriver_detach(&tap_cd
);
248 if_clone_attach(&tap_cloners
);
251 /* Pretty much useless for a pseudo-device */
253 tap_match(device_t parent
, cfdata_t cfdata
, void *arg
)
260 tap_attach(device_t parent
, device_t self
, void *aux
)
262 struct tap_softc
*sc
= device_private(self
);
264 #if defined(COMPAT_40) || defined(MODULAR)
265 const struct sysctlnode
*node
;
268 uint8_t enaddr
[ETHER_ADDR_LEN
] =
269 { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
270 char enaddrstr
[3 * ETHER_ADDR_LEN
];
275 sc
->sc_sih
= softint_establish(SOFTINT_CLOCK
, tap_softintr
, sc
);
276 getnanotime(&sc
->sc_btime
);
277 sc
->sc_atime
= sc
->sc_mtime
= sc
->sc_btime
;
279 if (!pmf_device_register(self
, NULL
, NULL
))
280 aprint_error_dev(self
, "couldn't establish power handler\n");
283 * In order to obtain unique initial Ethernet address on a host,
284 * do some randomisation using the current uptime. It's not meant
285 * for anything but avoiding hard-coding an address.
288 ui
= (tv
.tv_sec
^ tv
.tv_usec
) & 0xffffff;
289 memcpy(enaddr
+3, (uint8_t *)&ui
, 3);
291 aprint_verbose_dev(self
, "Ethernet address %s\n",
292 ether_snprintf(enaddrstr
, sizeof(enaddrstr
), enaddr
));
295 * Why 1000baseT? Why not? You can add more.
297 * Note that there are 3 steps: init, one or several additions to
298 * list of supported media, and in the end, the selection of one
301 ifmedia_init(&sc
->sc_im
, 0, tap_mediachange
, tap_mediastatus
);
302 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_1000_T
, 0, NULL
);
303 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_1000_T
|IFM_FDX
, 0, NULL
);
304 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_100_TX
, 0, NULL
);
305 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_100_TX
|IFM_FDX
, 0, NULL
);
306 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_10_T
, 0, NULL
);
307 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_10_T
|IFM_FDX
, 0, NULL
);
308 ifmedia_add(&sc
->sc_im
, IFM_ETHER
|IFM_AUTO
, 0, NULL
);
309 ifmedia_set(&sc
->sc_im
, IFM_ETHER
|IFM_AUTO
);
312 * One should note that an interface must do multicast in order
315 ifp
= &sc
->sc_ec
.ec_if
;
316 strcpy(ifp
->if_xname
, device_xname(self
));
318 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
319 ifp
->if_ioctl
= tap_ioctl
;
320 ifp
->if_start
= tap_start
;
321 ifp
->if_stop
= tap_stop
;
322 ifp
->if_init
= tap_init
;
323 IFQ_SET_READY(&ifp
->if_snd
);
325 sc
->sc_ec
.ec_capabilities
= ETHERCAP_VLAN_MTU
| ETHERCAP_JUMBO_MTU
;
327 /* Those steps are mandatory for an Ethernet driver, the fisrt call
328 * being common to all network interface drivers. */
330 ether_ifattach(ifp
, enaddr
);
334 #if defined(COMPAT_40) || defined(MODULAR)
336 * Add a sysctl node for that interface.
338 * The pointer transmitted is not a string, but instead a pointer to
339 * the softc structure, which we can use to build the string value on
340 * the fly in the helper function of the node. See the comments for
341 * tap_sysctl_handler for details.
343 * Usually sysctl_createv is called with CTL_CREATE as the before-last
344 * component. However, we can allocate a number ourselves, as we are
345 * the only consumer of the net.link.<iface> node. In this case, the
346 * unit number is conveniently used to number the node. CTL_CREATE
347 * would just work, too.
349 if ((error
= sysctl_createv(NULL
, 0, NULL
,
350 &node
, CTLFLAG_READWRITE
,
351 CTLTYPE_STRING
, device_xname(self
), NULL
,
352 tap_sysctl_handler
, 0, sc
, 18,
353 CTL_NET
, AF_LINK
, tap_node
, device_unit(sc
->sc_dev
),
355 aprint_error_dev(self
, "sysctl_createv returned %d, ignoring\n",
360 * Initialize the two locks for the device.
362 * We need a lock here because even though the tap device can be
363 * opened only once, the file descriptor might be passed to another
364 * process, say a fork(2)ed child.
366 * The Giant saves us from most of the hassle, but since the read
367 * operation can sleep, we don't want two processes to wake up at
368 * the same moment and both try and dequeue a single packet.
370 * The queue for event listeners (used by kqueue(9), see below) has
371 * to be protected, too, but we don't need the same level of
372 * complexity for that lock, so a simple spinning lock is fine.
374 mutex_init(&sc
->sc_rdlock
, MUTEX_DEFAULT
, IPL_NONE
);
375 simple_lock_init(&sc
->sc_kqlock
);
377 selinit(&sc
->sc_rsel
);
381 * When detaching, we do the inverse of what is done in the attach
382 * routine, in reversed order.
385 tap_detach(device_t self
, int flags
)
387 struct tap_softc
*sc
= device_private(self
);
388 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
389 #if defined(COMPAT_40) || defined(MODULAR)
394 sc
->sc_flags
|= TAP_GOING
;
400 softint_disestablish(sc
->sc_sih
);
402 #if defined(COMPAT_40) || defined(MODULAR)
404 * Destroying a single leaf is a very straightforward operation using
405 * sysctl_destroyv. One should be sure to always end the path with
408 if ((error
= sysctl_destroyv(NULL
, CTL_NET
, AF_LINK
, tap_node
,
409 device_unit(sc
->sc_dev
), CTL_EOL
)) != 0)
410 aprint_error_dev(self
,
411 "sysctl_destroyv returned %d, ignoring\n", error
);
415 ifmedia_delete_instance(&sc
->sc_im
, IFM_INST_ANY
);
416 seldestroy(&sc
->sc_rsel
);
417 mutex_destroy(&sc
->sc_rdlock
);
419 pmf_device_deregister(self
);
425 * This function is called by the ifmedia layer to notify the driver
426 * that the user requested a media change. A real driver would
427 * reconfigure the hardware.
430 tap_mediachange(struct ifnet
*ifp
)
436 * Here the user asks for the currently used media.
439 tap_mediastatus(struct ifnet
*ifp
, struct ifmediareq
*imr
)
441 struct tap_softc
*sc
= (struct tap_softc
*)ifp
->if_softc
;
442 imr
->ifm_active
= sc
->sc_im
.ifm_cur
->ifm_media
;
446 * This is the function where we SEND packets.
448 * There is no 'receive' equivalent. A typical driver will get
449 * interrupts from the hardware, and from there will inject new packets
450 * into the network stack.
452 * Once handled, a packet must be freed. A real driver might not be able
453 * to fit all the pending packets into the hardware, and is allowed to
454 * return before having sent all the packets. It should then use the
455 * if_flags flag IFF_OACTIVE to notify the upper layer.
457 * There are also other flags one should check, such as IFF_PAUSE.
459 * It is our duty to make packets available to BPF listeners.
461 * You should be aware that this function is called by the Ethernet layer
464 * When the device is opened, we have to pass the packet(s) to the
465 * userland. For that we stay in OACTIVE mode while the userland gets
466 * the packets, and we send a signal to the processes waiting to read.
468 * wakeup(sc) is the counterpart to the tsleep call in
469 * tap_dev_read, while selnotify() is used for kevent(2) and
470 * poll(2) (which includes select(2)) listeners.
473 tap_start(struct ifnet
*ifp
)
475 struct tap_softc
*sc
= (struct tap_softc
*)ifp
->if_softc
;
478 if ((sc
->sc_flags
& TAP_INUSE
) == 0) {
479 /* Simply drop packets */
481 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
488 bpf_mtap(ifp
->if_bpf
, m0
);
493 } else if (!IFQ_IS_EMPTY(&ifp
->if_snd
)) {
494 ifp
->if_flags
|= IFF_OACTIVE
;
496 selnotify(&sc
->sc_rsel
, 0, 1);
497 if (sc
->sc_flags
& TAP_ASYNCIO
)
498 softint_schedule(sc
->sc_sih
);
503 tap_softintr(void *cookie
)
505 struct tap_softc
*sc
;
511 if (sc
->sc_flags
& TAP_ASYNCIO
) {
512 ifp
= &sc
->sc_ec
.ec_if
;
513 if (ifp
->if_flags
& IFF_RUNNING
) {
515 b
= POLLIN
|POLLRDNORM
;
520 fownsignal(sc
->sc_pgid
, SIGIO
, a
, b
, NULL
);
525 * A typical driver will only contain the following handlers for
526 * ioctl calls, except SIOCSIFPHYADDR.
527 * The latter is a hack I used to set the Ethernet address of the
530 * Note that both ifmedia_ioctl() and ether_ioctl() have to be
531 * called under splnet().
534 tap_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
536 struct tap_softc
*sc
= (struct tap_softc
*)ifp
->if_softc
;
537 struct ifreq
*ifr
= (struct ifreq
*)data
;
548 error
= ifmedia_ioctl(ifp
, ifr
, &sc
->sc_im
, cmd
);
550 #if defined(COMPAT_40) || defined(MODULAR)
552 error
= tap_lifaddr(ifp
, cmd
, (struct ifaliasreq
*)data
);
556 error
= ether_ioctl(ifp
, cmd
, data
);
557 if (error
== ENETRESET
)
567 #if defined(COMPAT_40) || defined(MODULAR)
569 * Helper function to set Ethernet address. This has been replaced by
570 * the generic SIOCALIFADDR ioctl on a PF_LINK socket.
573 tap_lifaddr(struct ifnet
*ifp
, u_long cmd
, struct ifaliasreq
*ifra
)
575 const struct sockaddr
*sa
= &ifra
->ifra_addr
;
577 if (sa
->sa_family
!= AF_LINK
)
580 if_set_sadl(ifp
, sa
->sa_data
, ETHER_ADDR_LEN
, false);
587 * _init() would typically be called when an interface goes up,
588 * meaning it should configure itself into the state in which it
592 tap_init(struct ifnet
*ifp
)
594 ifp
->if_flags
|= IFF_RUNNING
;
602 * _stop() is called when an interface goes down. It is our
603 * responsability to validate that state by clearing the
606 * We have to wake up all the sleeping processes to have the pending
607 * read requests cancelled.
610 tap_stop(struct ifnet
*ifp
, int disable
)
612 struct tap_softc
*sc
= (struct tap_softc
*)ifp
->if_softc
;
614 ifp
->if_flags
&= ~IFF_RUNNING
;
616 selnotify(&sc
->sc_rsel
, 0, 1);
617 if (sc
->sc_flags
& TAP_ASYNCIO
)
618 softint_schedule(sc
->sc_sih
);
622 * The 'create' command of ifconfig can be used to create
623 * any numbered instance of a given device. Thus we have to
624 * make sure we have enough room in cd_devs to create the
625 * user-specified instance. config_attach_pseudo will do this
629 tap_clone_create(struct if_clone
*ifc
, int unit
)
631 if (tap_clone_creator(unit
) == NULL
) {
632 aprint_error("%s%d: unable to attach an instance\n",
633 tap_cd
.cd_name
, unit
);
641 * tap(4) can be cloned by two ways:
642 * using 'ifconfig tap0 create', which will use the network
643 * interface cloning API, and call tap_clone_create above.
644 * opening the cloning device node, whose minor number is TAP_CLONER.
645 * See below for an explanation on how this part work.
647 static struct tap_softc
*
648 tap_clone_creator(int unit
)
652 cf
= malloc(sizeof(*cf
), M_DEVBUF
, M_WAITOK
);
653 cf
->cf_name
= tap_cd
.cd_name
;
654 cf
->cf_atname
= tap_ca
.ca_name
;
656 /* let autoconf find the first free one */
658 cf
->cf_fstate
= FSTATE_STAR
;
661 cf
->cf_fstate
= FSTATE_NOTFOUND
;
664 return device_private(config_attach_pseudo(cf
));
668 * The clean design of if_clone and autoconf(9) makes that part
669 * really straightforward. The second argument of config_detach
670 * means neither QUIET nor FORCED.
673 tap_clone_destroy(struct ifnet
*ifp
)
675 struct tap_softc
*sc
= ifp
->if_softc
;
677 return tap_clone_destroyer(sc
->sc_dev
);
681 tap_clone_destroyer(device_t dev
)
683 cfdata_t cf
= device_cfdata(dev
);
686 if ((error
= config_detach(dev
, 0)) != 0)
687 aprint_error_dev(dev
, "unable to detach instance\n");
694 * tap(4) is a bit of an hybrid device. It can be used in two different
696 * 1. ifconfig tapN create, then use /dev/tapN to read/write off it.
697 * 2. open /dev/tap, get a new interface created and read/write off it.
698 * That interface is destroyed when the process that had it created exits.
700 * The first way is managed by the cdevsw structure, and you access interfaces
701 * through a (major, minor) mapping: tap4 is obtained by the minor number
702 * 4. The entry points for the cdevsw interface are prefixed by tap_cdev_.
704 * The second way is the so-called "cloning" device. It's a special minor
705 * number (chosen as the maximal number, to allow as much tap devices as
706 * possible). The user first opens the cloner (e.g., /dev/tap), and that
707 * call ends in tap_cdev_open. The actual place where it is handled is
710 * An tap device cannot be opened more than once at a time, so the cdevsw
711 * part of open() does nothing but noting that the interface is being used and
712 * hence ready to actually handle packets.
716 tap_cdev_open(dev_t dev
, int flags
, int fmt
, struct lwp
*l
)
718 struct tap_softc
*sc
;
720 if (minor(dev
) == TAP_CLONER
)
721 return tap_dev_cloner(l
);
723 sc
= device_lookup_private(&tap_cd
, minor(dev
));
727 /* The device can only be opened once */
728 if (sc
->sc_flags
& TAP_INUSE
)
730 sc
->sc_flags
|= TAP_INUSE
;
735 * There are several kinds of cloning devices, and the most simple is the one
736 * tap(4) uses. What it does is change the file descriptor with a new one,
737 * with its own fileops structure (which maps to the various read, write,
738 * ioctl functions). It starts allocating a new file descriptor with falloc,
739 * then actually creates the new tap devices.
741 * Once those two steps are successful, we can re-wire the existing file
742 * descriptor to its new self. This is done with fdclone(): it fills the fp
743 * structure as needed (notably f_data gets filled with the fifth parameter
744 * passed, the unit of the tap device which will allows us identifying the
745 * device later), and returns EMOVEFD.
747 * That magic value is interpreted by sys_open() which then replaces the
748 * current file descriptor by the new one (through a magic member of struct
751 * The tap device is flagged as being busy since it otherwise could be
752 * externally accessed through the corresponding device node with the cdevsw
757 tap_dev_cloner(struct lwp
*l
)
759 struct tap_softc
*sc
;
763 if ((error
= fd_allocfile(&fp
, &fd
)) != 0)
766 if ((sc
= tap_clone_creator(-1)) == NULL
) {
767 fd_abort(curproc
, fp
, fd
);
771 sc
->sc_flags
|= TAP_INUSE
;
773 return fd_clone(fp
, fd
, FREAD
|FWRITE
, &tap_fileops
,
774 (void *)(intptr_t)device_unit(sc
->sc_dev
));
778 * While all other operations (read, write, ioctl, poll and kqfilter) are
779 * really the same whether we are in cdevsw or fileops mode, the close()
780 * function is slightly different in the two cases.
782 * As for the other, the core of it is shared in tap_dev_close. What
783 * it does is sufficient for the cdevsw interface, but the cloning interface
784 * needs another thing: the interface is destroyed when the processes that
785 * created it closes it.
788 tap_cdev_close(dev_t dev
, int flags
, int fmt
,
791 struct tap_softc
*sc
=
792 device_lookup_private(&tap_cd
, minor(dev
));
797 return tap_dev_close(sc
);
801 * It might happen that the administrator used ifconfig to externally destroy
802 * the interface. In that case, tap_fops_close will be called while
803 * tap_detach is already happening. If we called it again from here, we
804 * would dead lock. TAP_GOING ensures that this situation doesn't happen.
807 tap_fops_close(file_t
*fp
)
809 int unit
= (intptr_t)fp
->f_data
;
810 struct tap_softc
*sc
;
813 sc
= device_lookup_private(&tap_cd
, unit
);
817 /* tap_dev_close currently always succeeds, but it might not
818 * always be the case. */
819 KERNEL_LOCK(1, NULL
);
820 if ((error
= tap_dev_close(sc
)) != 0) {
821 KERNEL_UNLOCK_ONE(NULL
);
825 /* Destroy the device now that it is no longer useful,
826 * unless it's already being destroyed. */
827 if ((sc
->sc_flags
& TAP_GOING
) != 0) {
828 KERNEL_UNLOCK_ONE(NULL
);
832 error
= tap_clone_destroyer(sc
->sc_dev
);
833 KERNEL_UNLOCK_ONE(NULL
);
838 tap_dev_close(struct tap_softc
*sc
)
844 /* Let tap_start handle packets again */
845 ifp
= &sc
->sc_ec
.ec_if
;
846 ifp
->if_flags
&= ~IFF_OACTIVE
;
848 /* Purge output queue */
849 if (!(IFQ_IS_EMPTY(&ifp
->if_snd
))) {
853 IFQ_DEQUEUE(&ifp
->if_snd
, m
);
860 bpf_mtap(ifp
->if_bpf
, m
);
867 sc
->sc_flags
&= ~(TAP_INUSE
| TAP_ASYNCIO
);
873 tap_cdev_read(dev_t dev
, struct uio
*uio
, int flags
)
875 return tap_dev_read(minor(dev
), uio
, flags
);
879 tap_fops_read(file_t
*fp
, off_t
*offp
, struct uio
*uio
,
880 kauth_cred_t cred
, int flags
)
884 KERNEL_LOCK(1, NULL
);
885 error
= tap_dev_read((intptr_t)fp
->f_data
, uio
, flags
);
886 KERNEL_UNLOCK_ONE(NULL
);
891 tap_dev_read(int unit
, struct uio
*uio
, int flags
)
893 struct tap_softc
*sc
=
894 device_lookup_private(&tap_cd
, unit
);
902 getnanotime(&sc
->sc_atime
);
904 ifp
= &sc
->sc_ec
.ec_if
;
905 if ((ifp
->if_flags
& IFF_UP
) == 0)
909 * In the TAP_NBIO case, we have to make sure we won't be sleeping
911 if ((sc
->sc_flags
& TAP_NBIO
) != 0) {
912 if (!mutex_tryenter(&sc
->sc_rdlock
))
913 return (EWOULDBLOCK
);
915 mutex_enter(&sc
->sc_rdlock
);
919 if (IFQ_IS_EMPTY(&ifp
->if_snd
)) {
920 ifp
->if_flags
&= ~IFF_OACTIVE
;
922 * We must release the lock before sleeping, and re-acquire it
925 mutex_exit(&sc
->sc_rdlock
);
926 if (sc
->sc_flags
& TAP_NBIO
)
929 error
= tsleep(sc
, PSOCK
|PCATCH
, "tap", 0);
934 /* The device might have been downed */
935 if ((ifp
->if_flags
& IFF_UP
) == 0)
937 if ((sc
->sc_flags
& TAP_NBIO
)) {
938 if (!mutex_tryenter(&sc
->sc_rdlock
))
939 return (EWOULDBLOCK
);
941 mutex_enter(&sc
->sc_rdlock
);
946 IFQ_DEQUEUE(&ifp
->if_snd
, m
);
947 ifp
->if_flags
&= ~IFF_OACTIVE
;
957 bpf_mtap(ifp
->if_bpf
, m
);
961 * One read is one packet.
964 error
= uiomove(mtod(m
, void *),
965 min(m
->m_len
, uio
->uio_resid
), uio
);
968 } while (m
!= NULL
&& uio
->uio_resid
> 0 && error
== 0);
974 mutex_exit(&sc
->sc_rdlock
);
979 tap_fops_stat(file_t
*fp
, struct stat
*st
)
982 struct tap_softc
*sc
;
983 int unit
= (uintptr_t)fp
->f_data
;
985 (void)memset(st
, 0, sizeof(*st
));
987 KERNEL_LOCK(1, NULL
);
988 sc
= device_lookup_private(&tap_cd
, unit
);
994 st
->st_dev
= makedev(cdevsw_lookup_major(&tap_cdevsw
), unit
);
995 st
->st_atimespec
= sc
->sc_atime
;
996 st
->st_mtimespec
= sc
->sc_mtime
;
997 st
->st_ctimespec
= st
->st_birthtimespec
= sc
->sc_btime
;
998 st
->st_uid
= kauth_cred_geteuid(fp
->f_cred
);
999 st
->st_gid
= kauth_cred_getegid(fp
->f_cred
);
1001 KERNEL_UNLOCK_ONE(NULL
);
1006 tap_cdev_write(dev_t dev
, struct uio
*uio
, int flags
)
1008 return tap_dev_write(minor(dev
), uio
, flags
);
1012 tap_fops_write(file_t
*fp
, off_t
*offp
, struct uio
*uio
,
1013 kauth_cred_t cred
, int flags
)
1017 KERNEL_LOCK(1, NULL
);
1018 error
= tap_dev_write((intptr_t)fp
->f_data
, uio
, flags
);
1019 KERNEL_UNLOCK_ONE(NULL
);
1024 tap_dev_write(int unit
, struct uio
*uio
, int flags
)
1026 struct tap_softc
*sc
=
1027 device_lookup_private(&tap_cd
, unit
);
1029 struct mbuf
*m
, **mp
;
1036 getnanotime(&sc
->sc_mtime
);
1037 ifp
= &sc
->sc_ec
.ec_if
;
1039 /* One write, one packet, that's the rule */
1040 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1045 m
->m_pkthdr
.len
= uio
->uio_resid
;
1048 while (error
== 0 && uio
->uio_resid
> 0) {
1050 MGET(*mp
, M_DONTWAIT
, MT_DATA
);
1056 (*mp
)->m_len
= min(MHLEN
, uio
->uio_resid
);
1057 error
= uiomove(mtod(*mp
, void *), (*mp
)->m_len
, uio
);
1058 mp
= &(*mp
)->m_next
;
1067 m
->m_pkthdr
.rcvif
= ifp
;
1071 bpf_mtap(ifp
->if_bpf
, m
);
1074 (*ifp
->if_input
)(ifp
, m
);
1081 tap_cdev_ioctl(dev_t dev
, u_long cmd
, void *data
, int flags
,
1084 return tap_dev_ioctl(minor(dev
), cmd
, data
, l
);
1088 tap_fops_ioctl(file_t
*fp
, u_long cmd
, void *data
)
1090 return tap_dev_ioctl((intptr_t)fp
->f_data
, cmd
, data
, curlwp
);
1094 tap_dev_ioctl(int unit
, u_long cmd
, void *data
, struct lwp
*l
)
1096 struct tap_softc
*sc
=
1097 device_lookup_private(&tap_cd
, unit
);
1106 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1111 IFQ_POLL(&ifp
->if_snd
, m
);
1116 *(int *)data
= m
->m_pkthdr
.len
;
1121 error
= fsetown(&sc
->sc_pgid
, cmd
, data
);
1125 error
= fgetown(sc
->sc_pgid
, cmd
, data
);
1129 sc
->sc_flags
|= TAP_ASYNCIO
;
1131 sc
->sc_flags
&= ~TAP_ASYNCIO
;
1135 sc
->sc_flags
|= TAP_NBIO
;
1137 sc
->sc_flags
&= ~TAP_NBIO
;
1144 struct ifreq
*ifr
= (struct ifreq
*)data
;
1145 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1147 strlcpy(ifr
->ifr_name
, ifp
->if_xname
, IFNAMSIZ
);
1158 tap_cdev_poll(dev_t dev
, int events
, struct lwp
*l
)
1160 return tap_dev_poll(minor(dev
), events
, l
);
1164 tap_fops_poll(file_t
*fp
, int events
)
1166 return tap_dev_poll((intptr_t)fp
->f_data
, events
, curlwp
);
1170 tap_dev_poll(int unit
, int events
, struct lwp
*l
)
1172 struct tap_softc
*sc
=
1173 device_lookup_private(&tap_cd
, unit
);
1179 if (events
& (POLLIN
|POLLRDNORM
)) {
1180 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1185 IFQ_POLL(&ifp
->if_snd
, m
);
1189 revents
|= events
& (POLLIN
|POLLRDNORM
);
1191 simple_lock(&sc
->sc_kqlock
);
1192 selrecord(l
, &sc
->sc_rsel
);
1193 simple_unlock(&sc
->sc_kqlock
);
1196 revents
|= events
& (POLLOUT
|POLLWRNORM
);
1201 static struct filterops tap_read_filterops
= { 1, NULL
, tap_kqdetach
,
1203 static struct filterops tap_seltrue_filterops
= { 1, NULL
, tap_kqdetach
,
1207 tap_cdev_kqfilter(dev_t dev
, struct knote
*kn
)
1209 return tap_dev_kqfilter(minor(dev
), kn
);
1213 tap_fops_kqfilter(file_t
*fp
, struct knote
*kn
)
1215 return tap_dev_kqfilter((intptr_t)fp
->f_data
, kn
);
1219 tap_dev_kqfilter(int unit
, struct knote
*kn
)
1221 struct tap_softc
*sc
=
1222 device_lookup_private(&tap_cd
, unit
);
1227 KERNEL_LOCK(1, NULL
);
1228 switch(kn
->kn_filter
) {
1230 kn
->kn_fop
= &tap_read_filterops
;
1233 kn
->kn_fop
= &tap_seltrue_filterops
;
1236 KERNEL_UNLOCK_ONE(NULL
);
1241 simple_lock(&sc
->sc_kqlock
);
1242 SLIST_INSERT_HEAD(&sc
->sc_rsel
.sel_klist
, kn
, kn_selnext
);
1243 simple_unlock(&sc
->sc_kqlock
);
1244 KERNEL_UNLOCK_ONE(NULL
);
1249 tap_kqdetach(struct knote
*kn
)
1251 struct tap_softc
*sc
= (struct tap_softc
*)kn
->kn_hook
;
1253 KERNEL_LOCK(1, NULL
);
1254 simple_lock(&sc
->sc_kqlock
);
1255 SLIST_REMOVE(&sc
->sc_rsel
.sel_klist
, kn
, knote
, kn_selnext
);
1256 simple_unlock(&sc
->sc_kqlock
);
1257 KERNEL_UNLOCK_ONE(NULL
);
1261 tap_kqread(struct knote
*kn
, long hint
)
1263 struct tap_softc
*sc
= (struct tap_softc
*)kn
->kn_hook
;
1264 struct ifnet
*ifp
= &sc
->sc_ec
.ec_if
;
1268 KERNEL_LOCK(1, NULL
);
1270 IFQ_POLL(&ifp
->if_snd
, m
);
1275 kn
->kn_data
= m
->m_pkthdr
.len
;
1277 rv
= (kn
->kn_data
!= 0 ? 1 : 0);
1278 KERNEL_UNLOCK_ONE(NULL
);
1282 #if defined(COMPAT_40) || defined(MODULAR)
1284 * sysctl management routines
1285 * You can set the address of an interface through:
1286 * net.link.tap.tap<number>
1288 * Note the consistent use of tap_log in order to use
1289 * sysctl_teardown at unload time.
1291 * In the kernel you will find a lot of SYSCTL_SETUP blocks. Those
1292 * blocks register a function in a special section of the kernel
1293 * (called a link set) which is used at init_sysctl() time to cycle
1294 * through all those functions to create the kernel's sysctl tree.
1296 * It is not possible to use link sets in a module, so the
1297 * easiest is to simply call our own setup routine at load time.
1299 * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
1300 * CTLFLAG_PERMANENT flag, meaning they cannot be removed. Once the
1301 * whole kernel sysctl tree is built, it is not possible to add any
1304 * It should be noted that we're not saving the sysctlnode pointer
1305 * we are returned when creating the "tap" node. That structure
1306 * cannot be trusted once out of the calling function, as it might
1307 * get reused. So we just save the MIB number, and always give the
1308 * full path starting from the root for later calls to sysctl_createv
1309 * and sysctl_destroyv.
1311 SYSCTL_SETUP(sysctl_tap_setup
, "sysctl net.link.tap subtree setup")
1313 const struct sysctlnode
*node
;
1316 if ((error
= sysctl_createv(clog
, 0, NULL
, NULL
,
1318 CTLTYPE_NODE
, "net", NULL
,
1320 CTL_NET
, CTL_EOL
)) != 0)
1323 if ((error
= sysctl_createv(clog
, 0, NULL
, NULL
,
1325 CTLTYPE_NODE
, "link", NULL
,
1327 CTL_NET
, AF_LINK
, CTL_EOL
)) != 0)
1331 * The first four parameters of sysctl_createv are for management.
1333 * The four that follows, here starting with a '0' for the flags,
1334 * describe the node.
1336 * The next series of four set its value, through various possible
1339 * Last but not least, the path to the node is described. That path
1340 * is relative to the given root (third argument). Here we're
1341 * starting from the root.
1343 if ((error
= sysctl_createv(clog
, 0, NULL
, &node
,
1345 CTLTYPE_NODE
, "tap", NULL
,
1347 CTL_NET
, AF_LINK
, CTL_CREATE
, CTL_EOL
)) != 0)
1349 tap_node
= node
->sysctl_num
;
1353 * The helper functions make Andrew Brown's interface really
1354 * shine. It makes possible to create value on the fly whether
1355 * the sysctl value is read or written.
1357 * As shown as an example in the man page, the first step is to
1358 * create a copy of the node to have sysctl_lookup work on it.
1360 * Here, we have more work to do than just a copy, since we have
1361 * to create the string. The first step is to collect the actual
1362 * value of the node, which is a convenient pointer to the softc
1363 * of the interface. From there we create the string and use it
1364 * as the value, but only for the *copy* of the node.
1366 * Then we let sysctl_lookup do the magic, which consists in
1367 * setting oldp and newp as required by the operation. When the
1368 * value is read, that means that the string will be copied to
1369 * the user, and when it is written, the new value will be copied
1370 * over in the addr array.
1372 * If newp is NULL, the user was reading the value, so we don't
1373 * have anything else to do. If a new value was written, we
1376 * If it is incorrect, we can return an error and leave 'node' as
1377 * it is: since it is a copy of the actual node, the change will
1380 * Upon a correct input, we commit the change to the ifnet
1381 * structure of our interface.
1384 tap_sysctl_handler(SYSCTLFN_ARGS
)
1386 struct sysctlnode node
;
1387 struct tap_softc
*sc
;
1391 char addr
[3 * ETHER_ADDR_LEN
];
1392 uint8_t enaddr
[ETHER_ADDR_LEN
];
1395 sc
= node
.sysctl_data
;
1396 ifp
= &sc
->sc_ec
.ec_if
;
1397 (void)ether_snprintf(addr
, sizeof(addr
), CLLADDR(ifp
->if_sadl
));
1398 node
.sysctl_data
= addr
;
1399 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
1400 if (error
|| newp
== NULL
)
1404 if (len
< 11 || len
> 17)
1408 if (ether_nonstatic_aton(enaddr
, addr
) != 0)
1410 if_set_sadl(ifp
, enaddr
, ETHER_ADDR_LEN
, false);