1.\" $NetBSD: tap.4,v 1.7 2007/09/10 10:35:52 cube Exp $ 2.\" 3.\" Copyright (c) 2004, 2005 The NetBSD Foundation. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of The NetBSD Foundation nor the names of its 15.\" contributors may be used to endorse or promote products derived 16.\" from this software without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd December 18, 2006 31.Dt TAP 4 32.Os 33.Sh NAME 34.Nm tap 35.Nd virtual Ethernet device 36.Sh SYNOPSIS 37.Cd pseudo-device tap 38.Sh DESCRIPTION 39The 40.Nm 41driver allows the creation and use of virtual Ethernet devices. 42Those interfaces appear just as any real Ethernet NIC to the kernel, 43but can also be accessed by userland through a character device node in order 44to read frames being sent by the system or to inject frames. 45.Pp 46In that respect it is very similar to what 47.Xr tun 4 48provides, but the added Ethernet layer allows easy integration with machine 49emulators or virtual Ethernet networks through the use of 50.Xr bridge 4 51with tunneling. 52.Ss INTERFACE CREATION 53Interfaces may be created in two different ways: 54using the 55.Xr ifconfig 8 56.Cm create 57command with a specified device number, 58or its 59.Xr ioctl 2 60equivalent, 61.Dv SIOCIFCREATE , 62or using the special cloning device 63.Pa /dev/tap . 64.Pp 65The former works the same as any other cloning network interface: 66the administrator can create and destroy interfaces at any time, 67notably at boot time. 68This is the easiest way of combining 69.Nm 70and 71.Xr bridge 4 . 72Later, userland will actually access the interfaces through the specific 73device nodes 74.Pa /dev/tapN . 75.Pp 76The latter is aimed at applications that need a virtual Ethernet device for 77the duration of their execution. 78A new interface is created at the opening of 79.Pa /dev/tap , 80and is later destroyed when the last process using the file descriptor closes 81it. 82.Ss CHARACTER DEVICES 83Whether the 84.Nm 85devices are accessed through the special cloning device 86.Pa /dev/tap 87or through the specific devices 88.Pa /dev/tapN , 89the possible actions to control the matching interface are the same. 90.Pp 91When using 92.Pa /dev/tap 93though, as the interface is created on-the-fly, its name is not known 94immediately by the application. 95Therefore the 96.Dv TAPGIFNAME 97ioctl is provided. 98It should be the first action an application using the special cloning device 99will do. 100It takes a pointer to a 101.Ft struct ifreq 102as an argument. 103.Pp 104Ethernet frames sent out by the kernel on a 105.Nm 106interface can be obtained by the controlling application with 107.Xr read 2 . 108It can also inject frames in the kernel with 109.Xr write 2 . 110There is absolutely no validation of the content of the injected frame, 111it can be any data, of any length. 112.Pp 113One call of 114.Xr write 2 115will inject a single frame in the kernel, as one call of 116.Xr read 2 117will retrieve a single frame from the queue, to the extent of the provided 118buffer. 119If the buffer is not large enough, the frame will be truncated. 120.Pp 121.Nm 122character devices support the 123.Dv FIONREAD 124ioctl which returns the size of the next available frame, 125or 0 if there is no available frame in the queue. 126.Pp 127They also support non-blocking I/O through the 128.Dv FIONBIO 129ioctl. 130In that mode, 131.Er EWOULDBLOCK 132is returned by 133.Xr read 2 134when no data is available. 135.Pp 136Asynchronous I/O is supported through the 137.Dv FIOASYNC , 138.Dv FIOSETOWN , 139and 140.Dv FIOGETOWN 141ioctls. 142The first will enable 143.Dv SIGIO 144generation, while the two other configure the process group that 145will receive the signal when data is ready. 146.Pp 147Synchronisation may also be achieved through the use of 148.Xr select 2 , 149.Xr poll 2 , 150or 151.Xr kevent 2 . 152.Ss ETHERNET ADDRESS 153When a 154.Nm 155device is created, it is assigned an Ethernet address 156of the form f2:0b:a4:xx:xx:xx. 157This address can later be changed in two ways: 158through a sysctl node, or an ioctl call. 159.Pp 160The sysctl node is net.link.tap.\*[Lt]iface\*[Gt]. 161Any string of six colon-separated hexadecimal numbers will be accepted. 162Reading that node will provide a string representation of the current 163Ethernet address. 164.Pp 165The address can also be changed with the 166.Dv SIOCSIFPHYADDR 167ioctl, which is used the same way as with 168.Xr gif 4 . 169The difference is in the family of the address which is passed inside the 170.Ft struct ifreqalias 171argument, which should be set to 172.Dv AF_LINK . 173This ioctl call should be made on a socket, as it is not available on 174the ioctl handler of the character device interface. 175.Sh FILES 176.Bl -tag -compact -width /dev/tap[0-9]* 177.It Pa /dev/tap 178cloning device 179.It Pa /dev/tap[0-9]* 180individual character device nodes 181.El 182.Sh SEE ALSO 183.Xr bridge 4 , 184.Xr etherip 4 , 185.Xr gif 4 , 186.Xr tun 4 , 187.Xr ifconfig 8 188.Sh HISTORY 189The 190.Nm 191driver first appeared in 192.Nx 3.0 . 193