tap Special File

Purpose

Provides software network interface which is Ethernet tunnel.

Description

The tap interface is a software loopback mechanism. It is a network interface analog of the pseudoterminal (pty).

The tap driver, like the pty driver, provides two interfaces: a simulating interface (an Ethernet interface for tap or a terminal for pty) and a character-special device, the control interface.

The network interfaces are named as tap0, tap1, and so on, one for each open control device that has been opened. These Ethernet network interfaces persist until the if_tap.ko module is unloaded or until removed with the ifconfig destroy command.

The tap devices are created by using interface cloning with the ifconfig tapN create command. This is the preferred method for creating the tap devices. You can remove of interfaces by using the ifconfig tapN destroy command.

Each interface supports the usual Ethernet network interface ioctl, such as SIOCSIFADDR and SIOCSIFNETMASK, and thus can be used with the ifconfig command like any other Ethernet interface. When the system chooses to transmit an Ethernet frame on the network interface, the frame can be read from the control device. It appears as input there. The process of writing an Ethernet frame to the control device generates an input frame on the network interface, as if the (nonexistent) hardware had just received it.

The Ethernet tunnel device, normally /dev/tapN, is exclusive-open (it cannot be opened if it is already open), and is restricted to the super-user or users with the PV_NET_CNTL privilege. The tunnel device is not marked up when the control device is opened. A read() call returns an error (EHOSTDOWN) if the interface is not ready. After the interface is ready, the read() call returns an Ethernet frame, if any frame is available. In case the frame is not avilable, it either blocks until one frame is available or returns an EWOULDBLOCK error, depending on whether the nonblocking I/O has been enabled. If the frame is longer than is allowed in the buffer passed to the read() call, the extra data is dropped.

A write(2) call passes an Ethernet frame to be received on the pseudointerface. Each write() call supplies exactly one frame. The frame length is taken from the amount of data provided to the write() call. If the frame cannot be accepted for a transient reason (for example, no buffer space is available), the write() call is not block, but is dropped. However, if the reason is not transient (for example, the frame is too large), an error is returned.

The following ioctl calls are supported and are defined in the <net/if_tap.h> file:

Item Description
TAPSIFINFO Sets the network interface information (line speed, MTU, and type). The argument should be a pointer to the struct tapinfo parameter.
TAPGIFINFO Retrieves the network interface information, such as line speed, MTU, and type. The argument should be a pointer to a struct tapinfo parameter
TAPSDEBUG Sets the internal debugging variable to that value. The argument should be a pointer to an intvariable.
TAPGDEBUG Stores the internal debugging variable's value into it. The argument should be a pointer to an int variable.
TAPGIFNAME Retrieves the network interface name. The argument should be a pointer to the struct ifreq parameter. The interface name is returned in the ifr_name field.
FIONBIO Turns nonblocking I/O for read operation off or on, depending on whether the value of the int argument is zero. Write operation are always nonblocking.
FIOASYNC Turns asynchronous I/O for read operation (that is generation of the SIGIO signal when data is available to be read) off or on, depending on whether the value of the intargument is zero.
FIONREAD Stores the size of the first one into theint argument if any frames are queued to be read; otherwise, stores zero..
TIOCSPGRP Sets the process group to receive the SIGIO signals, when asynchronous I/O is enabled, to the int argument value.
TIOCGPGRP Retrieves the process group value for the SIGIO signals into the argument int value.

The control device also supports the select() call for a read operation. Although the write() call is supported, it is rarely chosen. The write() call always succeeds because write operations are nonblocking. On the last close operation of the data device, the interface is brought down (as if with theifconfig tapN down). All the queued frames are thrown away. If the interface is up when the data device is not open, output frames are thrown away to avoid piling up.