1.\" $OpenBSD: tun.4,v 1.45 2020/01/09 09:25:16 claudio Exp $ 2.\" 3.\" Copyright (c) 2003 Marcus D. Watts All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, and the entire permission notice in its entirety, 10.\" including the disclaimer of warranties. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote 15.\" products derived from this software without specific prior 16.\" written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21.\" MARCUS D. WATTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24.\" OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25.\" ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 26.\" TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 27.\" USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28.\" 29.Dd $Mdocdate: January 9 2020 $ 30.Dt TUN 4 31.Os 32.Sh NAME 33.Nm tun 34.Nd network tunnel pseudo-device 35.Sh SYNOPSIS 36.Cd "pseudo-device tun" 37.Pp 38.In sys/types.h 39.In net/if_tun.h 40.Sh DESCRIPTION 41The 42.Nm 43driver provides a network interface pseudo-device. 44Packets sent to this interface can be read by a userland process 45and processed as desired. 46Packets written by the userland process are injected back into 47the kernel networking subsystem. 48.Pp 49A 50.Nm 51interface can be created at runtime using the 52.Ic ifconfig tun Ns Ar N Ic create 53command or by opening the character special device 54.Pa /dev/tunN . 55By default 56.Nm 57operates as a point-to-point interface. 58.Pp 59Each device has an exclusive open property: it cannot be opened 60if it is already open and in use by another process. 61Each read returns at most one packet; if insufficient 62buffer space is provided, the packet is truncated. 63Each write supplies exactly one packet. 64Each packet read or written is prefixed with a tunnel header consisting of 65a 4-byte network byte order integer containing the address family. 66On the last close of the device, all queued packets are discarded. 67If the device was created by opening 68.Pa /dev/tunN , 69it will be automatically destroyed. 70Devices created via 71.Xr ifconfig 8 72are only marked as not running and traffic will be dropped returning 73.Er EHOSTDOWN . 74.Pp 75Writes never block. 76If the protocol queue is full, the packet is dropped, a 77.Dq collision 78is counted, and 79.Er ENOBUFS 80is returned. 81.Pp 82In addition to the usual network interface ioctl commands described in 83.Xr netintro 4 , 84the following special commands defined in 85.In net/if_tun.h 86are supported: 87.Pp 88.Bl -tag -width indent -compact 89.It Dv TUNGIFINFO Fa "struct tuninfo *" 90.It Dv TUNSIFINFO Fa "struct tuninfo *" 91Get or set the interface characteristics. 92.Bd -literal 93/* iface info */ 94struct tuninfo { 95 u_int mtu; 96 u_short type; 97 u_short flags; 98 u_int baudrate; 99}; 100.Ed 101.Pp 102.Va flags 103sets the interface flags, and 104can include one or more of 105.Dv IFF_UP , 106.Dv IFF_POINTOPOINT , 107.Dv IFF_MULTICAST , 108.Dv IFF_BROADCAST . 109Flags given will be set; flags omitted will be cleared; flags not in this list 110will not be changed even when given. 111Flags default to 112.Dv IFF_POINTOPOINT . 113It is an error to set both 114.Dv IFF_POINTOPOINT 115and 116.Dv IFF_BROADCAST . 117.\" should say what type affects... 118.Va type 119defaults to 120.Dv IFT_TUNNEL . 121This sets the interface media address header type. 122.Pp 123.It Dv TUNSIFMODE Fa int * 124Set just the interface flags. 125The same restrictions as for 126.Dv TUNSIFINFO 127apply. 128.El 129.Pp 130The generic ioctls 131.Dv FIONREAD , 132.Dv FIONBIO , 133.Dv FIOASYNC , 134.Dv FIOSETOWN , 135.Dv FIOGETOWN 136are supported by 137.Nm . 138.Sh FILES 139.Bl -tag -width /dev/tun* -compact 140.It Pa /dev/tun* 141.El 142.Sh ERRORS 143If open fails, 144.Xr errno 2 145may be set to one of: 146.Bl -tag -width Er 147.It Bq Er ENXIO 148Not that many devices configured. 149.It Bq Er EBUSY 150Device was already open. 151.El 152.Pp 153If a 154.Xr write 2 155call fails, 156.Xr errno 2 157is set to one of: 158.Bl -tag -width Er 159.It Bq Er EMSGSIZE 160The packet supplied was too small or too large. 161The maximum sized packet allowed is currently 16384 bytes. 162.It Bq Er ENOBUFS 163There were no mbufs, or 164the queue for the outgoing protocol is full. 165.It Bq Er EAFNOSUPPORT 166The address family specified in the tunnel header was not 167recognized. 168.El 169.Pp 170Ioctl commands may fail with: 171.Bl -tag -width Er 172.It Bq Er EINVAL 173Attempt to set both 174.Dv IFF_POINTOPOINT 175and 176.Dv IFF_BROADCAST 177with 178.Dv TUNSIFMODE 179or using 180.Dv SIOCGIFADDR 181or 182.Dv SIOCSIFADDR . 183.It Bq Er ENOTTY 184Unrecognized ioctl command. 185.El 186.Pp 187A 188.Xr read 2 189call may fail because of: 190.Bl -tag -width Er 191.It Bq Er EHOSTDOWN 192The device is not ready. 193The device must have an 194.Xr inet 4 195interface address assigned to it, such as via 196.Dv SIOCSIFADDR . 197.It Bq Er EWOULDBLOCK 198Non-blocking I/O was selected and no packets were available. 199.El 200.Pp 201An attempt to send a packet out via the interface may fail with: 202.Bl -tag -width Er 203.It Bq Er EHOSTDOWN 204The device is not ready. 205The device must have an 206.Xr inet 4 207interface address assigned to it, such as via 208.Dv SIOCSIFADDR . 209.El 210.Sh SEE ALSO 211.Xr ioctl 2 , 212.Xr inet 4 , 213.Xr intro 4 , 214.Xr netintro 4 , 215.Xr hostname.if 5 , 216.Xr ifconfig 8 , 217.Xr netstart 8 218