1.\" $OpenBSD: tun.4,v 1.19 2001/06/22 12:15:47 mpech Exp $ 2.\" 3.Dd March 10, 1996 4.Dt TUN 4 5.Os 6.Sh NAME 7.Nm tun 8.Nd Tunnel Network Interface 9.Sh SYNOPSIS 10.Cd "pseudo-device tun 4" 11.Sh DESCRIPTION 12The 13.Nm 14interface is a software loopback mechanism that can be loosely 15described as the network interface analog of the 16.Xr pty 4 , 17that is, 18.Nm 19does for network interfaces what the 20.Nm pty 21driver does for terminals. 22.Pp 23The 24.Nm 25driver, like the 26.Nm pty 27driver, provides two interfaces: an interface like the usual facility 28it is simulating (a network interface in the case of 29.Nm tun , 30or a terminal for 31.Nm pty ) , 32and a character-special device 33.Dq control 34interface. 35.Pp 36The network interfaces are named 37.Sy tun Ns Ar 0 , 38.Sy tun Ns Ar 1 , 39etc, as many in all as the 40.Ar count 41figure given on the 42.Sy pseudo-device 43line. Each one supports the usual network-interface 44.Xr ioctl 2 Ns s , 45such as 46.Dv SIOCSIFADDR 47and 48.Dv SIOCSIFNETMASK , 49and thus can be used with 50.Xr ifconfig 8 51like any other interface. At boot time, they are 52.Dv POINTOPOINT 53interfaces, but this can be changed; see the description of the control 54device, below. When the system chooses to transmit a packet on the 55network interface, the packet can be read from the control device (it 56appears as 57.Dq input 58there); writing a packet to the control device generates an input 59packet on the network interface, as if the (nonexistent) hardware had 60just received it. 61.Pp 62There are two control interfaces. The 63.Em data 64interface, normally 65.Pa /dev/tun Ns Sy N , 66is exclusive-open (it cannot be opened if it is already open), is 67normally restricted to the superuser, and can 68.Dq transmit 69and 70.Dq receive 71packets. The 72.Em control 73interface, normally 74.Pa /dev/tunc Ns Sy N , 75cannot send and receive packets, but can be opened by many processes at 76once; it is intended for status queries and changes (many of which can 77also be implemented with 78.Fn ioctl 79calls on the data interface). There are a number of status bits that 80can be set or cleared via the control interfaces; they are mentioned 81below where applicable, and they are all summarized in the discussions 82of the control interfaces. 83.\" Why isn't .Ss documented in mdoc(7) and mdoc.samples(7)? 84.Ss The data interface 85The data interface supports 86.Xr read 2 , 87.Xr write 2 , 88and 89.Xr ioctl 2 90calls to, respectively, collect 91.Dq output 92packets, generate 93.Dq input 94packets, and perform control functions. As mentioned above, this 95interface is exclusive-open; if the 96.Dv SUONLY 97bit is set (which it is by default), it cannot be opened at all except 98by the superuser. By default, a 99.Fn read 100call will return an error 101.Pf ( Er EHOSTDOWN ) 102if the interface is not 103.Dq ready 104(which means that the control device is open and the interface's 105address has been set); if preferred, the 106.Dv RRWAIT 107bit can be set, in which case a 108.Fn read 109call will block (even if non-blocking I/O has been enabled) until the 110interface is ready. Once the interface is ready, 111.Fn read 112will return a packet if one is available; if not, it will either block 113until one is or return 114.Er EWOULDBLOCK , 115depending on whether non-blocking I/O has been enabled. If the packet 116is longer than is allowed for in the buffer passed to 117.Fn read , 118the extra data will be silently dropped. 119.Pp 120The first u_int32_t of data will always be the address family (eg, 121.Dv AF_INET ) 122of the packet in network byte order. By default, the packet data follows 123immediately, but if 124the 125.Dv PREPADDR 126bit is set, the address to which the packet is to be sent is placed 127after the address family u_int32_t and before the packet data. The size and 128layout of the address depends on the address family; for 129.Dv AF_INET , 130for example, it is a 131.Va struct in_addr . 132A 133.Xr write 2 134call passes a packet in to be 135.Dq received 136on the pseudo-interface. Each 137.Fn write 138call supplies exactly one packet; the packet length is taken from the 139amount of data provided to 140.Fn write . 141The first u_int32_t must be the address family of the packet in network 142byte order, much as in packets returned by 143.Fn read ; 144the packet data always follows immediately. 145A 146.Fn write 147with an invalid address family (e.g. not specified or in the wrong byte 148order) will return 149.Er ENOSUPPORT . 150A large number of 151.Xr ioctl 2 152calls are also supported. They are defined in 153.Aq Pa net/if_tun.h Ns . 154.Bl -tag -width TUN_PREPADDR 155.It Dv TUNSDEBUG 156The argument should be a pointer to an 157.Va int ; 158this sets the internal debugging variable to that value. What, if 159anything, this variable controls is not documented here; see the source 160code. 161.It Dv TUNGDEBUG 162The argument should be a pointer to an 163.Va int ; 164this stores the internal debugging variable's value into it. 165.It Dv TUNSIFMODE 166The argument should be a pointer to an 167.Va int ; 168its value must be 169.Dv IFF_POINTOPOINT 170or 171.Dv IFF_BROADCAST . 172The type of the corresponding 173.Em tun Ns Sy n 174interface is set to the supplied type. If the value is anything else, 175an 176.Er EINVAL 177error occurs. The interface must be down at the time; if it is up, an 178.Er EBUSY 179error occurs. 180.\" X .It Dv TUNSFLAG 181.\" X The interface's flag bits are set as specified in the 182.\" X .Va int 183.\" X argument. Only some of the bits can be modified; the rest are 184.\" X read-only. The bits are defined in 185.\" X .Aq Pa net/if_tun.h 186.\" X with a 187.\" X .Dv TUN_ 188.\" X prefix; for example, the bit called 189.\" X .Dv RRWAIT 190.\" X in this document would be referred to in source code as 191.\" X .Dv TUN_RRWAIT . 192.\" X The bits are: 193.\" X .\" Why isn't the way to create a table like this documented in mdoc(7) 194.\" X .\" or mdoc.samples(7)?! 195.\" X .Bl -column "TUN_PREPADDR" "RO/RW" -compact -indent-two 196.\" X .It Name Ta RO/RW Ta Meaning 197.\" X .It Dv TUN_OPEN Ta RO Ta "Data control device is open." 198.\" X .It Dv TUN_INITED Ta RO Ta "Initialized." 199.\" X .It Dv TUN_RCOLL Ta RO Ta "Select-for-read collision." 200.\" X .It Dv TUN_IASET Ta RO Ta "Address has been set." 201.\" X .It Dv TUN_DSTADDR Ta RO Ta "Destination address has been set." 202.\" X .It Dv TUN_RWAIT Ta RO Ta "A process is blocked in Fn read Ns ." 203.\" X .It Dv TUN_ASYNC Ta RO Ta "Generate Dv SIGIO No for readers." 204.\" X .It Dv TUN_NBIO Ta RO Ta "Non-blocking I/O for reads." 205.\" X .It Dv TUN_BRDADDR Ta RO Ta "Broadcast address has been set." 206.\" X .It Dv TUN_PREPADDR Ta RW Ta "Prepend sent-to address for reads." 207.\" X .It Dv TUN_STAYUP Ta RW Ta "Don't take interface down on close." 208.\" X .It Dv TUN_SUONLY Ta RW Ta "Data control device is superuser only." 209.\" X .It Dv TUN_RRWAIT Ta RW Ta "Wait for ready when reading." 210.\" X .El 211.\" X .It Dv TUNGFLAG 212.\" X The interface's flag bits are fetched into the argument 213.\" X .Va int . 214.\" X The flags and their meanings are as for 215.\" X .Dv TUNSFLAG . 216.\" X .It Dv FIONBIO 217.\" X Turn non-blocking I/O for reads off or on, according as the argument 218.\" X .Va int Ns 's 219.\" X value is or isn't zero. (Writes are always nonblocking.) 220.\" X .It Dv FIOASYNC 221.\" X Turn asynchronous I/O for reads (ie, generation of 222.\" X .Dv SIGIO 223.\" X when data is available to be read) off or on, according as the argument 224.\" X .Va int Ns 's 225.\" X value is or isn't zero. 226.\" X .It Dv FIONREAD 227.\" X If any packets are queued to be read, store the size of the first one 228.\" X into the argument 229.\" X .Va int ; 230.\" X otherwise, store zero. 231.\" X .It Dv TIOCSPGRP 232.\" X Set the process group to receive 233.\" X .Dv SIGIO 234.\" X signals, when asynchronous I/O is enabled, to the argument 235.\" X .Va int 236.\" X value. 237.\" X .It Dv TIOCGPGRP 238.\" X Retrieve the process group value for 239.\" X .Dv SIGIO 240.\" X signals into the argument 241.\" X .Va int 242.\" X value. 243.El 244The data control device also supports 245.Xr select 2 246for read; selecting for write is pointless, and always succeeds, since 247writes are always nonblocking (if the packet cannot be accepted for a 248transient reason (eg, no buffer space available), it is silently 249dropped; if the reason is not transient (eg, packet too large), an 250error is returned). 251.Pp 252On the last close of the data device, by default, the interface is 253brought down (as if with 254.Dq ifconfig tun Ns Sy n down ) ; 255if the 256.Dv STAYUP 257bit is set, this is not done. In either case, all queued packets are 258thrown away. (If the interface is up when the data device is not open, 259either because of 260.Dv STAYUP 261or because it was explicitly brought up, output packets are always 262thrown away rather than letting them pile up.) 263.Ss The control interface 264The alternative control interface is a text-based interface designed 265for shell-script or human use; it allows control of many of the things 266that can be done with 267.Fn ioctl 268calls on the data interface, and a few more as well. 269.Pp 270.Fn read Ns s 271on the control interface always return a single line of text (or just 272the beginning of the line, if the buffer passed to 273.Xr read 2 274was too small to take the whole line). The line contains items in the 275general format 276.Do 277.Li item=value 278.Dc , 279where 280.Li item 281is a keyword and 282.Li value 283is a value appropriate to the keyword. This line is intended for human 284use; programs should use the 285.Fn ioctl 286interface. Here is an actual example (broken because of width 287restrictions): 288.Bd -literal 289unit=0 flags=(open,inited,!rcoll,iaset,!dstaddr,!rwait,!async, 290!nbio,!brdaddr,prepaddr,stayup,suonly,rrwait) type=broadcast 291mtu=1500 coll=0 ipkts=0/0 opkts=0/0 pgrp=0 292.Ed 293.Pp 294Note that the current file offset is ignored for reads, so using a tool like 295.Xr cat 1 296will result in infinite output. Use something more like 297.Dq head\ \&-1 298for command-line use. It is possible to 299.Xr select 2 300for reading on this device, which will indicate that the device is 301readable whenever the state is changed. 302.Pp 303Writes to the control interface are interpreted as modifications to the 304state. Each 305.Fn write 306call is treated separately. The data written is broken at whitespace 307(blanks, tabs, newlines); each resulting fragment has its first 308character examined. If this character is a 309.Ql \&+ 310or 311.Ql \&\- , 312the rest of the fragment is taken as a flag name, and the flag is 313turned on (for 314.Ql \&+ ) 315or off (for 316.Ql \&\- ) . 317(Flag names are as generated on reads; they are the same as the 318.Dv TUN_ Ns Em xxx 319constants, with the leading 320.Dv TUN_ 321removed and the rest lowercased.) If the first character is 322.Ql t , 323the second character must be 324.Ql b 325or 326.Ql p , 327and the interface type is set to 328.Dv IFF_BROADCAST 329or 330.Dv IFF_POINTOPOINT , 331respectively. If the first character is 332.Ql g 333or 334.Ql m , 335the rest of the fragment is taken as a number in decimal (possibly with 336a leading \&\- sign) and the result is taken as a new process group, 337for 338.Ql g 339or MTU, for 340.Ql m . 341(The MTU must not be less than 1; attempts to set it so return 342.Er EIO . ) 343.Sh SEE ALSO 344.Xr inet 4 , 345.Xr inet6 4 , 346.Xr netintro 4 347.Sh BUGS 348The 349.Dv SUONLY 350bit is a botch, especially since the control interface, which is never 351restricted by the kernel, can change it. Access control really should 352be handled by the permission bits on the 353.Pa /dev 354entries for the data and control devices; this bit is a historical 355artifact. 356.Pp 357The process-group values for 358.Dv SIGIO 359signals should be checked; as it stands, the driver can be used (by 360anyone who can open the control or data device) to send any desired 361signal to an arbitrary process or process group. (Until this is fixed, 362you should be careful to set the permission bits to allow only root to 363open the control device, and either do the same for the data device or 364leave the 365.Dv SUONLY 366bit set.) 367.Sh NOTES 368Very old versions of the tunnel device did not include the address 369family at the start of the packet. More recent versions passed the 370address family as a single byte, but this caused problems with bpf, 371hence the current version passes a u_int32_t of address family. This was 372initially pass in host byte order, but the current version now uses 373network byte order. 374 375