]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man4/tun.4
This commit was generated by cvs2svn to compensate for changes in r163799,
[FreeBSD/FreeBSD.git] / share / man / man4 / tun.4
1 .\" $NetBSD: tun.4,v 1.1 1996/06/25 22:17:37 pk Exp $
2 .\" $FreeBSD$
3 .\" Based on PR#2411
4 .\"
5 .Dd October 9, 2006
6 .Dt TUN 4
7 .Os
8 .Sh NAME
9 .Nm tun
10 .Nd tunnel software network interface
11 .Sh SYNOPSIS
12 .Cd device tun
13 .Sh DESCRIPTION
14 The
15 .Nm
16 interface is a software loopback mechanism that can be loosely
17 described as the network interface analog of the
18 .Xr pty 4 ,
19 that is,
20 .Nm
21 does for network interfaces what the
22 .Xr pty 4
23 driver does for terminals.
24 .Pp
25 The
26 .Nm
27 driver, like the
28 .Xr pty 4
29 driver, provides two interfaces: an interface like the usual facility
30 it is simulating
31 (a network interface in the case of
32 .Nm ,
33 or a terminal for
34 .Xr pty 4 ) ,
35 and a character-special device
36 .Dq control
37 interface.
38 .Pp
39 The network interfaces are named
40 .Dq Li tun0 ,
41 .Dq Li tun1 ,
42 etc., one for each control device that has been opened.
43 These network interfaces persist until the
44 .Pa if_tun.ko
45 module is unloaded (if
46 .Nm
47 is built into your kernel, the network interfaces cannot be removed).
48 .Pp
49 The
50 .Nm
51 interface
52 permits opens on the special control device
53 .Pa /dev/tun .
54 When this device is opened,
55 .Nm
56 will return a handle for the lowest unused
57 .Nm
58 device (use
59 .Xr devname 3
60 to determine which).
61 Control devices (once successfully opened) persist until
62 .Pa if_tun.ko
63 is unloaded in the same way that network interfaces persist (see above).
64 .Pp
65 Each interface supports the usual network-interface
66 .Xr ioctl 2 Ns s ,
67 such as
68 .Dv SIOCAIFADDR
69 and thus can be used with
70 .Xr ifconfig 8
71 like any other interface.
72 At boot time, they are
73 .Dv POINTOPOINT
74 interfaces, but this can be changed; see the description of the control
75 device, below.
76 When the system chooses to transmit a packet on the
77 network interface, the packet can be read from the control device
78 (it appears as
79 .Dq input
80 there);
81 writing a packet to the control device generates an input
82 packet on the network interface, as if the (non-existent)
83 hardware had just received it.
84 .Pp
85 The tunnel device
86 .Pq Pa /dev/tun Ns Ar N
87 is exclusive-open
88 (it cannot be opened if it is already open).
89 A
90 .Xr read 2
91 call will return an error
92 .Pq Er EHOSTDOWN
93 if the interface is not
94 .Dq ready
95 (which means that the control device is open and the interface's
96 address has been set).
97 .Pp
98 Once the interface is ready,
99 .Xr read 2
100 will return a packet if one is available; if not, it will either block
101 until one is or return
102 .Er EWOULDBLOCK ,
103 depending on whether non-blocking I/O has been enabled.
104 If the packet is longer than is allowed for in the buffer passed to
105 .Xr read 2 ,
106 the extra data will be silently dropped.
107 .Pp
108 If the
109 .Dv TUNSLMODE
110 ioctl has been set, packets read from the control device will be prepended
111 with the destination address as presented to the network interface output
112 routine,
113 .Fn tunoutput .
114 The destination address is in
115 .Vt struct sockaddr
116 format.
117 The actual length of the prepended address is in the member
118 .Va sa_len .
119 If the
120 .Dv TUNSIFHEAD
121 ioctl has been set, packets will be prepended with a four byte address
122 family in network byte order.
123 .Dv TUNSLMODE
124 and
125 .Dv TUNSIFHEAD
126 are mutually exclusive.
127 In any case, the packet data follows immediately.
128 .Pp
129 A
130 .Xr write 2
131 call passes a packet in to be
132 .Dq received
133 on the pseudo-interface.
134 If the
135 .Dv TUNSIFHEAD
136 ioctl has been set, the address family must be prepended, otherwise the
137 packet is assumed to be of type
138 .Dv AF_INET .
139 Each
140 .Xr write 2
141 call supplies exactly one packet; the packet length is taken from the
142 amount of data provided to
143 .Xr write 2
144 (minus any supplied address family).
145 Writes will not block; if the packet cannot be accepted for a
146 transient reason
147 (e.g., no buffer space available),
148 it is silently dropped; if the reason is not transient
149 (e.g., packet too large),
150 an error is returned.
151 .Pp
152 The following
153 .Xr ioctl 2
154 calls are supported
155 (defined in
156 .In net/if_tun.h ) :
157 .Bl -tag -width ".Dv TUNSIFMODE"
158 .It Dv TUNSDEBUG
159 The argument should be a pointer to an
160 .Vt int ;
161 this sets the internal debugging variable to that value.
162 What, if anything, this variable controls is not documented here; see
163 the source code.
164 .It Dv TUNGDEBUG
165 The argument should be a pointer to an
166 .Vt int ;
167 this stores the internal debugging variable's value into it.
168 .It Dv TUNSIFINFO
169 The argument should be a pointer to an
170 .Vt struct tuninfo
171 and allows setting the MTU, the type, and the baudrate of the tunnel
172 device.
173 The
174 .Vt struct tuninfo
175 is declared in
176 .In net/if_tun.h .
177 .Pp
178 The use of this ioctl is restricted to the super-user.
179 .It Dv TUNGIFINFO
180 The argument should be a pointer to an
181 .Vt struct tuninfo ,
182 where the current MTU, type, and baudrate will be stored.
183 .It Dv TUNSIFMODE
184 The argument should be a pointer to an
185 .Vt int ;
186 its value must be either
187 .Dv IFF_POINTOPOINT
188 or
189 .Dv IFF_BROADCAST
190 and should have
191 .Dv IFF_MULTICAST
192 OR'd into the value if multicast support is required.
193 The type of the corresponding
194 .Dq Li tun Ns Ar N
195 interface is set to the supplied type.
196 If the value is outside the above range, an
197 .Er EINVAL
198 error is returned.
199 The interface must be down at the time; if it is up, an
200 .Er EBUSY
201 error is returned.
202 .It Dv TUNSLMODE
203 The argument should be a pointer to an
204 .Vt int ;
205 a non-zero value turns off
206 .Dq multi-af
207 mode and turns on
208 .Dq link-layer
209 mode, causing packets read from the tunnel device to be prepended with
210 the network destination address (see above).
211 .It Dv TUNSIFPID
212 Will set the pid owning the tunnel device to the current process's pid.
213 .It Dv TUNSIFHEAD
214 The argument should be a pointer to an
215 .Vt int ;
216 a non-zero value turns off
217 .Dq link-layer
218 mode, and enables
219 .Dq multi-af
220 mode, where every packet is preceded with a four byte address family.
221 .It Dv TUNGIFHEAD
222 The argument should be a pointer to an
223 .Vt int ;
224 the ioctl sets the value to one if the device is in
225 .Dq multi-af
226 mode, and zero otherwise.
227 .It Dv FIONBIO
228 Turn non-blocking I/O for reads off or on, according as the argument
229 .Vt int Ns 's
230 value is or is not zero.
231 (Writes are always non-blocking.)
232 .It Dv FIOASYNC
233 Turn asynchronous I/O for reads
234 (i.e., generation of
235 .Dv SIGIO
236 when data is available to be read)
237 off or on, according as the argument
238 .Vt int Ns 's
239 value is or is not zero.
240 .It Dv FIONREAD
241 If any packets are queued to be read, store the size of the first one
242 into the argument
243 .Vt int ;
244 otherwise, store zero.
245 .It Dv TIOCSPGRP
246 Set the process group to receive
247 .Dv SIGIO
248 signals, when asynchronous I/O is enabled, to the argument
249 .Vt int
250 value.
251 .It Dv TIOCGPGRP
252 Retrieve the process group value for
253 .Dv SIGIO
254 signals into the argument
255 .Vt int
256 value.
257 .El
258 .Pp
259 The control device also supports
260 .Xr select 2
261 for read; selecting for write is pointless, and always succeeds, since
262 writes are always non-blocking.
263 .Pp
264 On the last close of the data device, by default, the interface is
265 brought down
266 (as if with
267 .Nm ifconfig Ar tunN Cm down ) .
268 All queued packets are thrown away.
269 If the interface is up when the data device is not open
270 output packets are always thrown away rather than letting
271 them pile up.
272 .Sh SEE ALSO
273 .Xr ioctl 2 ,
274 .Xr read 2 ,
275 .Xr select 2 ,
276 .Xr write 2 ,
277 .Xr devname 3 ,
278 .Xr inet 4 ,
279 .Xr intro 4 ,
280 .Xr pty 4 ,
281 .Xr ifconfig 8
282 .Sh AUTHORS
283 This manual page was originally obtained from
284 .Nx .