1 .\" $OpenBSD: tun.4,v 1.2 1996/12/16 16:08:52 deraadt Exp $
7 .Nd Tunnel Network Interface
9 .Cd "pseudo-device tun 4"
13 interface is a software loopback mechanism that can be loosely
14 described as the network interface analog of the
18 does for network interfaces what the
20 driver does for terminals.
26 driver, provides two interfaces: an interface like the usual facility
27 it is simulating (a network interface in the case of
31 and a character-special device
35 The network interfaces are named
38 etc, as many in all as the
42 line. Each one supports the usual network-interface
48 and thus can be used with
50 like any other interface. At boot time, they are
52 interfaces, but this can be changed; see the description of the control
53 device, below. When the system chooses to transmit a packet on the
54 network interface, the packet can be read from the control device (it
57 there); writing a packet to the control device generates an input
58 packet on the network interface, as if the (nonexistent) hardware had
61 There are two control interfaces. The
64 .Pa /dev/tun Ns Sy N ,
65 is exclusive-open (it cannot be opened if it is already open), is
66 normally restricted to the super-user, and can
73 .Pa /dev/tunc Ns Sy N ,
74 cannot send and receive packets, but can be opened by many processes at
75 once; it is intended for status queries and changes (many of which can
76 also be implemented with
78 calls on the data interface). There are a number of status bits that
79 can be set or cleared via the control interfaces; they are mentioned
80 below where applicable, and they are all summarized in the discussions
81 of the control interfaces.
82 .\" Why isn't .Ss documented in mdoc(7) and mdoc.samples(7)?
83 .Ss The data interface
84 The data interface supports
89 calls to, respectively, collect
93 packets, and perform control functions. As mentioned above, this
94 interface is exclusive-open; if the
96 bit is set (which it is by default), it cannot be opened at all except
97 by the super-user. By default, a
99 call will return an error
101 if the interface is not
103 (which means that the control device is open and the interface's
104 address has been set); if preferred, the
106 bit can be set, in which case a
108 call will block (even if non-blocking I/O has been enabled) until the
109 interface is ready. Once the interface is ready,
111 will return a packet if one is available; if not, it will either block
112 until one is or return
114 depending on whether non-blocking I/O has been enabled. If the packet
115 is longer than is allowed for in the buffer passed to
117 the extra data will be silently dropped.
119 The first byte of data will always be the address family (eg,
121 of the packet. By default, the packet data follows immediately, but if
124 bit is set, the address to which the packet is to be sent is placed
125 after the address family byte and before the packet data. The size and
126 layout of the address depends on the address family; for
132 call passes a packet in to be
134 on the pseudo-interface. Each
136 call supplies exactly one packet; the packet length is taken from the
137 amount of data provided to
139 The first byte must be the address family of the packet, much as in
142 the packet data always follows immediately.
145 calls are also supported. They are defined in
146 .Aq Pa net/if_tun.h Ns .
147 .Bl -tag -width TUN_PREPADDR
149 The argument should be a pointer to an
151 this sets the internal debugging variable to that value. What, if
152 anything, this variable controls is not documented here; see the source
155 The argument should be a pointer to an
157 this stores the internal debugging variable's value into it.
159 The argument should be a pointer to an
165 The type of the corresponding
167 interface is set to the supplied type. If the value is anything else,
170 error occurs. The interface must be down at the time; if it is up, an
173 .\" X .It Dv TUNSFLAG
174 .\" X The interface's flag bits are set as specified in the
176 .\" X argument. Only some of the bits can be modified; the rest are
177 .\" X read-only. The bits are defined in
178 .\" X .Aq Pa net/if_tun.h
181 .\" X prefix; for example, the bit called
183 .\" X in this document would be referred to in source code as
184 .\" X .Dv TUN_RRWAIT .
186 .\" X .\" Why isn't the way to create a table like this documented in mdoc(7)
187 .\" X .\" or mdoc.samples(7)?!
188 .\" X .Bl -column "TUN_PREPADDR" "RO/RW" -compact -indent-two
189 .\" X .It Name Ta RO/RW Ta Meaning
190 .\" X .It Dv TUN_OPEN Ta RO Ta "Data control device is open."
191 .\" X .It Dv TUN_INITED Ta RO Ta "Initialized."
192 .\" X .It Dv TUN_RCOLL Ta RO Ta "Select-for-read collision."
193 .\" X .It Dv TUN_IASET Ta RO Ta "Address has been set."
194 .\" X .It Dv TUN_DSTADDR Ta RO Ta "Destination address has been set."
195 .\" X .It Dv TUN_RWAIT Ta RO Ta "A process is blocked in Fn read Ns ."
196 .\" X .It Dv TUN_ASYNC Ta RO Ta "Generate Dv SIGIO No for readers."
197 .\" X .It Dv TUN_NBIO Ta RO Ta "Non-blocking I/O for reads."
198 .\" X .It Dv TUN_BRDADDR Ta RO Ta "Broadcast address has been set."
199 .\" X .It Dv TUN_PREPADDR Ta RW Ta "Prepend sent-to address for reads."
200 .\" X .It Dv TUN_STAYUP Ta RW Ta "Don't take interface down on close."
201 .\" X .It Dv TUN_SUONLY Ta RW Ta "Data control device is super-user only."
202 .\" X .It Dv TUN_RRWAIT Ta RW Ta "Wait for ready when reading."
204 .\" X .It Dv TUNGFLAG
205 .\" X The interface's flag bits are fetched into the argument
207 .\" X The flags and their meanings are as for
210 .\" X Turn non-blocking I/O for reads off or on, according as the argument
212 .\" X value is or isn't zero. (Writes are always nonblocking.)
213 .\" X .It Dv FIOASYNC
214 .\" X Turn asynchronous I/O for reads (ie, generation of
216 .\" X when data is available to be read) off or on, according as the argument
218 .\" X value is or isn't zero.
219 .\" X .It Dv FIONREAD
220 .\" X If any packets are queued to be read, store the size of the first one
221 .\" X into the argument
223 .\" X otherwise, store zero.
224 .\" X .It Dv TIOCSPGRP
225 .\" X Set the process group to receive
227 .\" X signals, when asynchronous I/O is enabled, to the argument
230 .\" X .It Dv TIOCGPGRP
231 .\" X Retrieve the process group value for
233 .\" X signals into the argument
237 The data control device also supports
239 for read; selecting for write is pointless, and always succeeds, since
240 writes are always nonblocking (if the packet cannot be accepted for a
241 transient reason (eg, no buffer space available), it is silently
242 dropped; if the reason is not transient (eg, packet too large), an
245 On the last close of the data device, by default, the interface is
246 brought down (as if with
247 .Dq ifconfig tun Ns Sy n down ) ;
250 bit is set, this is not done. In either case, all queued packets are
251 thrown away. (If the interface is up when the data device is not open,
254 or because it was explicitly brought up, output packets are always
255 thrown away rather than letting them pile up.)
256 .Ss The control interface
257 The alternative control interface is a text-based interface designed
258 for shell-script or human use; it allows control of many of the things
259 that can be done with
261 calls on the data interface, and a few more as well.
264 on the control interface always return a single line of text (or just
265 the beginning of the line, if the buffer passed to
267 was too small to take the whole line). The line contains items in the
276 is a value appropriate to the keyword. This line is intended for human
277 use; programs should use the
279 interface. Here is an actual example (broken because of width
282 unit=0 flags=(open,inited,!rcoll,iaset,!dstaddr,!rwait,!async,
283 !nbio,!brdaddr,prepaddr,stayup,suonly,rrwait) type=broadcast
284 mtu=1500 coll=0 ipkts=0/0 opkts=0/0 pgrp=0
287 Note that the current file offset is ignored for reads, so using a tool like
289 will result in infinite output. Use something more like
291 for command-line use. It is possible to
293 for reading on this device, which will indicate that the device is
294 readable whenever the state is changed.
296 Writes to the control interface are interpreted as modifications to the
299 call is treated separately. The data written is broken at whitespace
300 (blanks, tabs, newlines); each resulting fragment has its first
301 character examined. If this character is a
305 the rest of the fragment is taken as a flag name, and the flag is
310 (Flag names are as generated on reads; they are the same as the
312 constants, with the leading
314 removed and the rest lowercased.) If the first character is
316 the second character must be
320 and the interface type is set to
323 .Dv IFF_POINTOPOINT ,
324 respectively. If the first character is
328 the rest of the fragment is taken as a number in decimal (possibly with
329 a leading \&\- sign) and the result is taken as a new process group,
334 (The MTU must not be less than 1; attempts to set it so return
337 This interface is useful for command-line reconfiguration, such as
338 setting the interface type at boot time, with
345 bit is a botch, especially since the control interface, which is never
346 restricted by the kernel, can change it. Access control really should
347 be handled by the permission bits on the
349 entries for the data and control devices; this bit is a historical
352 The process-group values for
354 signals should be checked; as it stands, the driver can be used (by
355 anyone who can open the control or data device) to send any desired
356 signal to an arbitrary process or process group. (Until this is fixed,
357 you should be careful to set the permisison bits to allow only root to
358 open the control device, and either do the same for the data device or