xref: /freebsd/share/man/man4/tun.4 (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 March 10, 1996
6.Dt TUN 4
7.Os
8.Sh NAME
9.Nm tun
10.Nd tunnel software network interface
11.Sh SYNOPSIS
12.Cd pseudo-device tun
13.Sh DESCRIPTION
14The
15.Nm
16interface is a software loopback mechanism that can be loosely
17described as the network interface analog of the
18.Xr pty 4 ,
19that is,
20.Nm
21does for network interfaces what the
22.Nm pty
23driver does for terminals.
24.Pp
25The
26.Nm
27driver, like the
28.Nm pty
29driver, provides two interfaces: an interface like the usual facility
30it is simulating
31.Po
32a network interface in the case of
33.Nm ,
34or a terminal for
35.Nm pty
36.Pc ,
37and a character-special device
38.Dq control
39interface.
40.Pp
41The network interfaces are named
42.Sy tun Ns Ar 0 ,
43.Sy tun Ns Ar 1 ,
44etc, as many as were made by
45.Xr MAKEDEV 8 .
46Each one supports the usual network-interface
47.Xr ioctl 2 Ns s ,
48such as
49.Dv SIOCSIFADDR
50and
51.Dv SIOCSIFNETMASK ,
52and thus can be used with
53.Xr ifconfig 8
54like any other interface.  At boot time, they are
55.Dv POINTOPOINT
56interfaces, but this can be changed; see the description of the control
57device, below.  When the system chooses to transmit a packet on the
58network interface, the packet can be read from the control device
59.Po
60it appears as
61.Dq input
62there
63.Pc ;
64writing a packet to the control device generates an input
65packet on the network interface, as if the
66.Pq non-existent
67hardware had just received it.
68.Pp
69The tunnel device, normally
70.Pa /dev/tun Ns Sy N ,
71is exclusive-open
72.Po
73it cannot be opened if it is already open
74.Pc
75and is restricted to the super-user.
76A
77.Fn read
78call will return an error
79.Pq Er EHOSTDOWN
80if the interface is not
81.Dq ready
82.Po
83which means that the control device is open and the interface's
84address has been set
85.Pc .
86Once the interface is ready,
87.Fn read
88will return a packet if one is available; if not, it will either block
89until one is or return
90.Er EWOULDBLOCK ,
91depending on whether non-blocking I/O has been enabled.  If the packet
92is longer than is allowed for in the buffer passed to
93.Fn read ,
94the extra data will be silently dropped.
95.Pp
96Packets can be optionally prepended with the destination address as presented
97to the network interface output routine
98.Pq Sq Li tunoutput .
99The destination address is in
100.Sq Li struct sockaddr
101format.
102The actual length of the prepended address is in the member
103.Sq Li sa_len .
104The packet data follows immediately.
105A
106.Xr write 2
107call passes a packet in to be
108.Dq received
109on the pseudo-interface.  Each
110.Fn write
111call supplies exactly one packet; the packet length is taken from the
112amount of data provided to
113.Fn write .
114Writes will not block; if the packet cannot be accepted for a
115transient reason
116.Pq e.g., no buffer space available ,
117it is silently dropped; if the reason is not transient
118.Pq e.g., packet too large ,
119an error is returned.
120If
121.Dq link-layer mode
122is on
123.Pq see Dv TUNSLMODE No below ,
124the actual packet data must be preceded by a
125.Sq Li struct sockaddr .
126The driver currently only inspects the
127.Sq Li sa_family
128field.
129The following
130.Xr ioctl 2
131calls are supported
132.Pq defined in Aq Pa net/if_tun.h Ns :
133.Bl -tag -width TUNSIFMODE
134.It Dv TUNSDEBUG
135The argument should be a pointer to an
136.Va int ;
137this sets the internal debugging variable to that value.  What, if
138anything, this variable controls is not documented here; see the source
139code.
140.It Dv TUNGDEBUG
141The argument should be a pointer to an
142.Va int ;
143this stores the internal debugging variable's value into it.
144.It Dv TUNSIFMODE
145The argument should be a pointer to an
146.Va int ;
147its value must be either
148.Dv IFF_POINTOPOINT
149or
150.Dv IFF_BROADCAST .
151The type of the corresponding
152.Em tun Ns Sy n
153interface is set to the supplied type.  If the value is anything else,
154an
155.Er EINVAL
156error occurs.  The interface must be down at the time; if it is up, an
157.Er EBUSY
158error occurs.
159.It Dv TUNSLMODE
160The argument should be a pointer to an
161.Va int ;
162a non-zero value turns on
163.Dq link-layer
164mode, causing packets read from the tunnel device to be prepended with
165network destination address.
166.It Dv FIONBIO
167Turn non-blocking I/O for reads off or on, according as the argument
168.Va int Ns 's
169value is or isn't zero
170.Pq Writes are always nonblocking .
171.It Dv FIOASYNC
172Turn asynchronous I/O for reads
173.Po
174i.e., generation of
175.Dv SIGIO
176when data is available to be read
177.Pc off or on, according as the argument
178.Va int Ns 's
179value is or isn't zero.
180.It Dv FIONREAD
181If any packets are queued to be read, store the size of the first one
182into the argument
183.Va int ;
184otherwise, store zero.
185.It Dv TIOCSPGRP
186Set the process group to receive
187.Dv SIGIO
188signals, when asynchronous I/O is enabled, to the argument
189.Va int
190value.
191.It Dv TIOCGPGRP
192Retrieve the process group value for
193.Dv SIGIO
194signals into the argument
195.Va int
196value.
197.El
198.Pp
199The control device also supports
200.Xr select 2
201for read; selecting for write is pointless, and always succeeds, since
202writes are always non-blocking.
203.Pp
204On the last close of the data device, by default, the interface is
205brought down
206.Po as if with
207.Dq ifconfig tun Ns Sy n No down
208.Pc .
209All queued packets are thrown away.
210If the interface is up when the data device is not open
211output packets are always thrown away rather than letting
212them pile up.
213.Sh SEE ALSO
214.Xr inet 4 ,
215.Xr intro 4
216.Sh BUGS
217Currently is IP-only.
218.Sh AUTHORS
219This man page has been obtained from
220.Bx Net .
221