TUN(4)

NAME
tun, tap - network tunnel pseudo-device

SYNOPSIS
pseudo-device tun

#include <sys/types.h>
#include <net/if_tun.h>

DESCRIPTION
The tun pseudo-device driver provides character special devices for communicating with the kernel network stack via the tun and tap network interfaces. Packets sent to these interfaces can be read from the device special file by a userland process and processed as desired. Packets written to the device special file by the userland process are injected back into the kernel networking subsystem.

tun and tap interfaces can be created at runtime using the ifconfig ifaceN create command, or by opening the character special devices /dev/tunN or /dev/tapN respectively.

The minor number of the device special files are associated with the unit number of the network interfaces.

Each device has an exclusive open property: it cannot be opened if it is already open and in use by another process. On the last close of the device all queued packets are discarded. If the device was created by opening a device special file it will be automatically destroyed. The last close of a device special file associated with an interface created via ifconfig(8) will be marked as not running and traffic sent out the will be dropped.

Each read returns at most one packet; if insufficient buffer space is provided, the packet is truncated. Each write supplies exactly one packet. Writes never block. If the protocol queue is full, the packet is dropped, a “collision” is counted, and ENOBUFS is returned.

The following ioctl(2) special commands defined in <net/if_tun.h> are supported:

TUNGIFINFO struct tuninfo *
TUNSIFINFO struct tuninfo *
    Get or set the interface characteristics.

        /* iface info */
        struct tuninfo {
                u_int   mtu;
                u_short type;
                u_short flags;
                u_int   baudrate;
        };

    flags and type are set by the kernel when an interface is created, and must be set to the same values that the kernel provided.

TUNSIFMODE int *
    is provided for backwards compatibility. The flags set must match what the kernel initialised them to.

The generic ioctls FIONREAD, FIONBIO, FIOASYNC, FIOSETOWN, FIOGETOWN are also supported.

  Point-to-Point Layer 3 tunnel interface (tun)
Each packet read from or written to a tun interface is prefixed with a tunnel header consisting of a 4-byte network byte order integer containing the address family of the packet. tun supports AF_INET, AF_INET6, and AF_MPLS packets.

  Ethernet tunnel interface (tap)
Each packet read from or written to a tap interface is an Ethernet packet. The Ethernet CRC at the end of the frame is not required.

The device special files for tap interfaces support the following additional ioctl(2) commands:

SIOCGIFADDR uint8_t[ETHER_ADDR_LEN]
SIOCSIFADDR uint8_t[ETHER_ADDR_LEN]
    Get or set the link layer address (MAC address) of the interface.

FILES
/dev/tun*
/dev/tap*

ERRORS
If open fails, errno(2) may be set to one of:

[ENXIO]
    Not that many devices configured.

[EBUSY]
    Device was already open.

If a write(2) call fails, errno(2) is set to one of:

[EMSGSIZE]
    The packet supplied was too small or too large. The maximum sized packet allowed is currently 16384 bytes.

[ENOMEM]
    There were no mbufs.

ioctl(2) commands may fail with:

[EINVAL]
    Invalid parameters were specified.

[ENOTTY]
    Unrecognized ioctl command.

A read(2) call may fail because of:

[EIO]
    The associated interface has been destroyed.

[EWOULDBLOCK]
    Non-blocking I/O was selected and no packets were available.

SEE ALSO
ioctl(2), inet(4), intro(4), netintro(4), hostname.if(5), ifconfig(8), netstart(8)

HISTORY
tun interfaces originally supported both Layer 3 and Ethernet packets by reconfiguring the type of interface with TUNSIFINFO. Ethernet packet support was split into the separate tap interface in OpenBSD 5.9.

AUTHORS
tun was written by Julian Onions <Julian.Onions@nexor.co.uk> at Nottingham University.

The tap interface functionality was written by Claudio Jeker <claudio@openbsd.org>.