xref: /netbsd-src/share/man/man4/tap.4 (revision a221396914e6a6ce826ec11c6e875c740273ca70)
1.\" $NetBSD: tap.4,v 1.16 2022/05/02 23:25:12 gutteridge 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.\"
15.\"  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16.\"  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17.\"  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18.\"  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19.\"  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20.\"  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21.\"  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22.\"  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23.\"  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24.\"  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25.\"  POSSIBILITY OF SUCH DAMAGE.
26.\"
27.Dd May 2, 2022
28.Dt TAP 4
29.Os
30.Sh NAME
31.Nm tap
32.Nd Ethernet tunnel software network interface
33.Sh SYNOPSIS
34.Cd pseudo-device tap
35.Sh DESCRIPTION
36The
37.Nm
38driver allows the creation and use of virtual Ethernet devices.
39Those interfaces appear just as any real Ethernet NIC to the kernel,
40but can also be accessed by userland through a character device node in order
41to read frames being sent by the system or to inject frames. In that respect
42it is very similar to what
43.Xr tun 4
44provides.
45.Ss INTERFACE CREATION
46Interfaces may be created in two different ways:
47using the
48.Xr ifconfig 8
49.Cm create
50command with a specified device number,
51or its
52.Xr ioctl 2
53equivalent,
54.Dv SIOCIFCREATE ,
55or using the special cloning device
56.Pa /dev/tap .
57.Pp
58The former works the same as any other cloning network interface:
59the administrator can create and destroy interfaces at any time,
60notably at boot time.
61This is the easiest way of combining
62.Nm
63and
64.Xr bridge 4 .
65Later, userland will actually access the interfaces through the specific
66device nodes
67.Pa /dev/tapN .
68.Pp
69The latter is aimed at applications that need a virtual Ethernet device for
70the duration of their execution.
71A new interface is created at the opening of
72.Pa /dev/tap ,
73and is later destroyed when the last process using the file descriptor closes
74it.
75.Ss CHARACTER DEVICES
76Whether the
77.Nm
78devices are accessed through the special cloning device
79.Pa /dev/tap
80or through the specific devices
81.Pa /dev/tapN ,
82the possible actions to control the matching interface are the same.
83.Pp
84When using
85.Pa /dev/tap
86though, as the interface is created on-the-fly, its name is not known
87immediately by the application.
88Therefore the
89.Dv TAPGIFNAME
90ioctl is provided.
91It should be the first action an application using the special cloning device
92will do.
93It takes a pointer to a
94.Ft struct ifreq
95as an argument.
96.Pp
97Ethernet frames sent out by the kernel on a
98.Nm
99interface can be obtained by the controlling application with
100.Xr read 2 .
101It can also inject frames in the kernel with
102.Xr write 2 .
103There is absolutely no validation of the content of the injected frame,
104it can be any data, of any length.
105.Pp
106One call of
107.Xr write 2
108will inject a single frame in the kernel, as one call of
109.Xr read 2
110will retrieve a single frame from the queue, to the extent of the provided
111buffer.
112If the buffer is not large enough, the frame will be truncated.
113.Pp
114.Nm
115character devices support the
116.Dv FIONREAD
117ioctl which returns the size of the next available frame,
118or 0 if there is no available frame in the queue.
119.Pp
120They also support non-blocking I/O through the
121.Dv FIONBIO
122ioctl.
123In that mode,
124.Er EWOULDBLOCK
125is returned by
126.Xr read 2
127when no data is available.
128.Pp
129Asynchronous I/O is supported through the
130.Dv FIOASYNC ,
131.Dv FIOSETOWN ,
132and
133.Dv FIOGETOWN
134ioctls.
135The first will enable
136.Dv SIGIO
137generation, while the two other configure the process group that
138will receive the signal when data is ready.
139.Pp
140Synchronisation may also be achieved through the use of
141.Xr select 2 ,
142.Xr poll 2 ,
143or
144.Xr kevent 2 .
145.Ss ETHERNET ADDRESS
146When a
147.Nm
148device is created, it is assigned an Ethernet address
149of the form f2:0b:a4:xx:xx:xx.
150This address can later be changed using
151.Xr ifconfig 8
152to add an active link layer address, or directly via the
153.Dv SIOCALIFADDR
154ioctl on a
155.Dv PF_LINK
156socket, as it is not available on
157the ioctl handler of the character device interface.
158.Ss LINK STATE
159When an application has opened the
160.Nm
161character device the link is considered up, otherwise down.
162As such, it is best to open the character device once connectivity has
163been established so that Duplicate Address Detection, if applicable,
164can be performed.
165If connectivity is lost, the character device should be closed.
166.Sh FILES
167.Bl -tag -compact -width /dev/tap[0-9]*
168.It Pa /dev/tap
169cloning device
170.It Pa /dev/tap[0-9]*
171individual character device nodes
172.El
173.Sh SEE ALSO
174.Xr bridge 4 ,
175.Xr l2tp 4 ,
176.Xr tun 4 ,
177.Xr vether 4 ,
178.Xr ifconfig 8
179.Sh HISTORY
180The
181.Nm
182driver first appeared in
183.Nx 3.0 .
184.Sh CAVEATS
185Starting from
186.Nx 10.0 ,
187the
188.Nm
189driver can no longer be used as a
190.Xr bridge 4
191endpoint because it supports a link state based on if it has been opened or not.
192Use the
193.Xr vether 4
194driver instead as it's been explicitly designed for this purpose.
195