The tun interface is a software loopback mechanism that can be loosely described as the network interface analog of the pty(4), that is, tun does for network interfaces what the pty(4) driver does for terminals.
The tun driver, like the pty(4) driver, provides two interfaces: an interface like the usual facility it is simulating (a network interface in the case of tun, or a terminal for pty(4)), and a character-special device "control" interface.
The network interfaces are named "tun0", "tun1", etc., one for each control device that has been opened. These network interfaces persist until the if_tun.ko module is unloaded (if tun is built into your kernel, the network interfaces cannot be removed).
The tun interface permits opens on the special control device /dev/tun. When this device is opened, tun will return a handle for the lowest unused tun device (use devname(3) to determine which). Control devices (once successfully opened) persist until if_tun.ko is unloaded in the same way that network interfaces persist (see above).
Each interface supports the usual network-interface ioctl 2 s, such as SIOCSIFADDR and SIOCSIFNETMASK, and thus can be used with ifconfig(8) like any other interface. At boot time, they are POINTOPOINT interfaces, but this can be changed; see the description of the control device, below. When the system chooses to transmit a packet on the network interface, the packet can be read from the control device (it appears as "input" there); writing a packet to the control device generates an input packet on the network interface, as if the (non-existent) hardware had just received it.
The tunnel device (/dev/tun N) is exclusive-open (it cannot be opened if it is already open). A read(2) call will return an error (EHOSTDOWN) if the interface is not "ready" (which means that the control device is open and the interfaces address has been set).
Once the interface is ready, read(2) will return a packet if one is available; if not, it will either block until one is or return EWOULDBLOCK, depending on whether non-blocking I/O has been enabled. If the packet is longer than is allowed for in the buffer passed to read(2), the extra data will be silently dropped.
If the TUNSLMODE ioctl has been set, packets read from the control device will be prepended with the destination address as presented to the network interface output routine, tunoutput. The destination address is in
.Vt struct sockaddr format. The actual length of the prepended address is in the member sa_len. If the TUNSIFHEAD ioctl has been set, packets will be prepended with a four byte address family in network byte order. TUNSLMODE and TUNSIFHEAD are mutually exclusive. In any case, the packet data follows immediately.
A write(2) call passes a packet in to be "received" on the pseudo-interface. If the TUNSIFHEAD ioctl has been set, the address family must be prepended, otherwise the packet is assumed to be of type AF_INET. Each write(2) call supplies exactly one packet; the packet length is taken from the amount of data provided to write(2) (minus any supplied address family). Writes will not block; if the packet cannot be accepted for a transient reason (e.g., no buffer space available), it is silently dropped; if the reason is not transient (e.g., packet too large), an error is returned.
The following ioctl(2) calls are supported (defined in
.In net/if_tun.h ) :