tun - IP
network tunnel driver
packets are written to
they are received by the kernel
layer on the network interface du
When the kernel's IP layer sends packets to the IP interface du
, they are available for reading on
Instead of having hardware and an associated kernel interface that support network functions, the tun driver allows a network interface to be implemented as a user-space process. While talking to the same set of tunnel drivers on the same system, different network interface processes can implement different IP encapsulation methods, such as RFC 877 for use over CCITT X.25-based public data networks, or RFC 1055 SLIP or RFC 1661/1332 PPP for use over dedicated line s and dialup modems.
The tun driver provides support for a pair of devices collectively known as an IP tunnel The two devices comprising a tunnel are known as the inbound and outbound sides, similar to the pairing between /dev/tty n (the inbound terminal) and /dev/cu n (the outbound `auto -call unit available on many systems). The outbound side's minor device number is that of the inbound side plus M , which is 128, though they together appear to IP as one interface. If both the inbound and outbound sides of a tunnel device are open, packets received from IP are delivered to only the inbound side.
If a TCP packet received from IP is part of a telnet, rlogin, or FTP command stream, it will be put in a fast queue . All packets in the fast queue are delivered to the user before any packets in the normal queue.
A few special ioctls are provided for use on the /dev/tun* devices to supply the functionality needed by applications programs to emulate real hardware interfaces. The complete list of supported ioctls is:
Set or clear the IFF_POINTOPOINT in the associated network interface.
Set or clear `address mode', in which packets read are prefaced with four octets containing the destination IP address in network byte order. The third argument is a pointer to an integer containing either a zero or a one, indicating whether `address mode' should be cleared or set, respectively. If both `address mode' and `packed buffer mode' are set, each packet's length will come first, followed by the packet's destination address, followed by the packet itself
Get the current status of `address mode'.
Set or clear `packed buffer mode' where multiple packets are encoded in single read/write buffers. The third argument is a pointer to an integer containing either a zero or a one, indicating whether `packed buffer mode' should be cleared or set, respectively. If set (1), each packet is preceded by four octets representing the next packet's length in octets. The following packet will then be aligned to the next multiple of four octets. If cleared (0), packets will be delivered one per read(3) from the tunnel device. If both `address mode' and `packed buffer mode' are set, each packet's length will come first, followed by the packet's address, followed by the packet itself.
Get the current status of `packed buffer mode'.
- Set the max number of IP frames to send back in a packet buffer read.
Get the PKMAX value.
Set the number of long word zeroes to put on the front of each packet read in packed buffer mode.
Get the number of pad words.
Set the interface name (may only be invoked by the superuser).
Get the interface name.
Set or clear non-blocking mode for I/O operations
int tun_fd = -1, len;
tun_fd = open("/dev/tun0", O_RDWR);
ioctl(tun_fd, TUIOSNAME, "du");
len = read(tun_fd, packet, size);
write(tun_fd, packet, len);
If a packet is delivered to the interface for an address family other than AF_INET, EAFNOSUPPORT will be returned.
/dev/tun0 through /dev/tun M-1 `inbound tunnel devices
/dev/tun M through /dev/tun 2*M-1 ` outbound tunnel devices
, RFC 1332, RFC 1144
, RFC 1055
, RFC 877
Copyright 1991, 1992, 1993, 1994, 1995, 1996 Morning Star Technologies Inc.; all rights reserved.