1.\" $OpenBSD: tun.4,v 1.43 2015/10/23 16:45:51 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: October 23 2015 $ 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 . 55.Pp 56Each device has an exclusive open property: it cannot be opened 57if it is already open and in use by another process. 58Each read returns at most one packet; if insufficient 59buffer space is provided, the packet is truncated. 60Each write supplies exactly one packet. 61Each packet read or written is prefixed with a tunnel header consisting of 62a 4-byte network byte order integer containing the address family. 63On the last close of the device, all queued packets are discarded. 64If the device was created by opening 65.Pa /dev/tunN , 66it will be automatically destroyed. 67Devices created via 68.Xr ifconfig 8 69are only marked as not running and traffic will be dropped returning 70.Er EHOSTDOWN . 71.Pp 72Writes never block. 73If the protocol queue is full, the packet is dropped, a 74.Dq collision 75is counted, and 76.Er ENOBUFS 77is returned. 78.Pp 79In addition to the usual network interface 80ioctl commands described in 81.Xr netintro 4 , 82the following special commands defined in 83.In net/if_tun.h 84are supported: 85.Pp 86.Bl -tag -width indent -compact 87.It Dv TUNGIFINFO Fa "struct tuninfo *" 88.It Dv TUNSIFINFO Fa "struct tuninfo *" 89Get or set the interface characteristics. 90.Bd -literal 91/* iface info */ 92struct tuninfo { 93 u_int mtu; 94 u_short type; 95 u_short flags; 96 u_int baudrate; 97}; 98.Ed 99.Pp 100.Va flags 101sets the interface flags, and 102can include one or more of 103.Dv IFF_UP , 104.Dv IFF_POINTOPOINT , 105.Dv IFF_MULTICAST , 106.Dv IFF_BROADCAST . 107Flags given will be set; flags omitted will be cleared; flags not in this list 108will not be changed even when given. 109Flags default to 110.Dv IFF_POINTOPOINT . 111It is an error to set both 112.Dv IFF_POINTOPOINT 113and 114.Dv IFF_BROADCAST . 115.\" should say what type affects... 116.Va type 117defaults to 118.Dv IFT_TUNNEL . 119This sets the interface media address header type. 120.Pp 121.It Dv TUNSIFMODE Fa int *flags 122Set just the interface flags. 123The same restrictions as for 124.Dv TUNSIFINFO 125apply. 126.Pp 127.It Dv FIONBIO Fa int *flag 128Set non-blocking I/O. 129.Pp 130.It Dv FIOASYNC Fa int *flag 131Cause signal 132.Dv SIGIO 133to be sent when a packet can be read. 134.Pp 135.It Dv TIOCSPGRP Fa int *pgrp 136.It Dv TIOCGPGRP Fa int *pgrp 137Get or set the process group to which signals might be sent 138via 139.Dv FIOASYNC . 140.Pp 141.It Dv FIONREAD Fa int *count 142Get the byte count of the next packet available to be read. 143.El 144.Sh FILES 145.Bl -tag -width /dev/tun* -compact 146.It Pa /dev/tun* 147.El 148.Sh ERRORS 149If open fails, 150.Xr errno 2 151may be set to one of: 152.Bl -tag -width Er 153.It Bq Er ENXIO 154Not that many devices configured. 155.It Bq Er EBUSY 156Device was already open. 157.El 158.Pp 159If a 160.Xr write 2 161call fails, 162.Xr errno 2 163is set to one of: 164.Bl -tag -width Er 165.It Bq Er EMSGSIZE 166The packet supplied was too small or too large. 167The maximum sized packet allowed is currently 16384 bytes. 168.It Bq Er ENOBUFS 169There were no mbufs, or 170the queue for the outgoing protocol is full. 171.It Bq Er EAFNOSUPPORT 172The address family specified in the tunnel header was not 173recognized. 174.El 175.Pp 176Ioctl commands may fail with: 177.Bl -tag -width Er 178.It Bq Er EINVAL 179Attempt to set both 180.Dv IFF_POINTOPOINT 181and 182.Dv IFF_BROADCAST 183with 184.Dv TUNSIFMODE 185or using 186.Dv SIOCGIFADDR 187or 188.Dv SIOCSIFADDR . 189.It Bq Er ENOTTY 190Unrecognized ioctl command. 191.El 192.Pp 193A 194.Xr read 2 195call may fail because of: 196.Bl -tag -width Er 197.It Bq Er EHOSTDOWN 198The device is not ready. 199The device must have an 200.Xr inet 4 201interface address assigned to it, such as via 202.Dv SIOCSIFADDR . 203.It Bq Er EWOULDBLOCK 204Non-blocking I/O was selected and no packets were available. 205.El 206.Pp 207An attempt to send a packet out via the interface may fail with: 208.Bl -tag -width Er 209.It Bq Er EHOSTDOWN 210The device is not ready. 211The device must have an 212.Xr inet 4 213interface address assigned to it, such as via 214.Dv SIOCSIFADDR . 215.El 216.Sh SEE ALSO 217.Xr inet 4 , 218.Xr intro 4 , 219.Xr netintro 4 , 220.Xr hostname.if 5 , 221.Xr ifconfig 8 , 222.Xr netstart 8 223