| 1 |
|
| 2 |
<html><head> |
| 3 |
<title>Building TUN based virtual networks with socat</title> |
| 4 |
<link rel="stylesheet" type="text/css" href="dest-unreach.css"> |
| 5 |
</head> |
| 6 |
|
| 7 |
<body> |
| 8 |
|
| 9 |
<h1>Building TUN based virtual networks with socat</h1> |
| 10 |
|
| 11 |
<h2>Introduction</h2> |
| 12 |
<p> |
| 13 |
Some operating systems allow the generation of virtual network interfaces that |
| 14 |
do not connect to a wire but to a process that simulates the network. Often |
| 15 |
these devices are called TUN or TAP. |
| 16 |
</p> |
| 17 |
<p> |
| 18 |
socat provides an address type that creates a TUN device on Linux; the other |
| 19 |
socat address can be any type; it transfers the "wire" data as desired. |
| 20 |
</p> |
| 21 |
<p> |
| 22 |
This document shows how a simple virtual network can be created between |
| 23 |
two hosts that may be far (many network hops) apart. On both hosts a socat |
| 24 |
instance is started that connects to the other host using TCP and creates a TUN |
| 25 |
device. See <a href="socat-openssltunnel.html">socat-openssltunnel.html</a> for |
| 26 |
a guide on securing the connection using SSL. |
| 27 |
</p> |
| 28 |
<p> |
| 29 |
The following IP addresses are used in the example; replace them in the |
| 30 |
following commands with the requirements of your situation:</p> |
| 31 |
<table border="1"> |
| 32 |
<tr><th>host</th><th>address</th><th>mask</th></tr> |
| 33 |
<tr><td>physical "server" address</td><td>1.2.3.4</td><td>n/a</td></tr> |
| 34 |
<tr><td>physical "client" address</td><td>n/a</td><td>n/a</td></tr> |
| 35 |
<tr><td>TUN on "server"</td><td>192.168.255.1</td><td>255.255.255.0</td></tr> |
| 36 |
<tr><td>TUN on "client"</td><td>192.168.255.2</td><td>255.255.255.0</td></tr> |
| 37 |
</table> |
| 38 |
<p>The TCP connection uses port 11443.</p> |
| 39 |
|
| 40 |
<p>On "default" Linux installations, creating TUN/TAP devices might require |
| 41 |
root privilege.</p> |
| 42 |
|
| 43 |
|
| 44 |
<h2>Generate TUN devices with socat</h2> |
| 45 |
<p>In this section two instances of socat are used to generate TUN devices on |
| 46 |
different hosts and connect the "wire" sides, providing a simple virtual |
| 47 |
network. |
| 48 |
</p> |
| 49 |
<p> |
| 50 |
We distinguish server and client only with respect to the connection between |
| 51 |
the two socat instances; the TUN interfaces both have the same quality. |
| 52 |
</p> |
| 53 |
|
| 54 |
<h3>TUN Server</h3> |
| 55 |
|
| 56 |
<span class="shell">socat -d -d \ |
| 57 |
UDP-LISTEN:11443 \ |
| 58 |
TUN:192.168.255.1/24,up |
| 59 |
</span> |
| 60 |
<p>After starting this command, socat will wait for a connection and then |
| 61 |
create a TUN pseudo network device with address 192.168.255.1; the bit number |
| 62 |
specifies the mask of the network that is pretended to be connected on this |
| 63 |
interface.</p> |
| 64 |
|
| 65 |
<h3>TUN Client</h3> |
| 66 |
<span class="shell">socat \ |
| 67 |
UDP:1.2.3.4:11443 \ |
| 68 |
TUN:192.168.255.2/24,up |
| 69 |
</span> |
| 70 |
<p>This command should establish a connection to the server and create the TUN |
| 71 |
device on the client.</p> |
| 72 |
|
| 73 |
<h3>Seeing it work</h3> |
| 74 |
|
| 75 |
<p> |
| 76 |
After successful connection both TUN interfaces should be active and transfer |
| 77 |
data between each other using the TCP connection. Try this by pinging |
| 78 |
192.168.255.1 from the client and 192.168.255.2 from the server. |
| 79 |
</p> |
| 80 |
|
| 81 |
<h3>TCP/IP version 6</h3> |
| 82 |
|
| 83 |
<p>IPv6 as transport should work just like any TCP/IPv6 connection.</p> |
| 84 |
|
| 85 |
<p>Creation of an IPv6 virtual interface is not directly possible, but you can |
| 86 |
generate an IPv4 interface as described above, and add IPv6 addresses using |
| 87 |
the <tt>ifconfig</tt> command. |
| 88 |
|
| 89 |
<h2>Troubleshooting</h2> |
| 90 |
|
| 91 |
<h3>Test TUN integration</h3> |
| 92 |
<p> |
| 93 |
If you get error messages like this:</p> |
| 94 |
<span class="error">... E unknown device/address "tun"</span> |
| 95 |
<p>your socat executable probably does not provide TUN/TAP support. Potential |
| 96 |
reasons: you are not on Linux or are using an older version of socat. |
| 97 |
</p> |
| 98 |
|
| 99 |
<h3>Missing kernel support</h3> |
| 100 |
<p>An error message like:</p> |
| 101 |
<span class="error">... E open("/dev/net/tun", 02, 0666): No such file or directory</span> |
| 102 |
<p>indicates that your kernel either needs to load the tun module or does not |
| 103 |
have TUN/TAP support compiled in. Try to load the module:</p> |
| 104 |
<span class="shell">modprobe tun</span> |
| 105 |
<p>and check |
| 106 |
for /dev/net/tun. If that does not succeed you need to |
| 107 |
rebuild your kernel with the appropriate configuration (probably under |
| 108 |
<b>Device driver / Network device support / Network device / Universal TUN/TAP</b>). |
| 109 |
</p> |
| 110 |
|
| 111 |
<h3>TUN cloning device permissions</h3> |
| 112 |
<p>An error message like:</p> |
| 113 |
<span class="error">... E open("/dev/net/tun", 02, 0666): Permission denied</span> |
| 114 |
<p>indicates that you do not have permission to read or write the TUN cloning |
| 115 |
device. Check its permission and ownership.</p> |
| 116 |
|
| 117 |
<h3>Interface down</h3> |
| 118 |
<p>If no error occurs but the pings do not work check if the network devices |
| 119 |
have been created:</p> |
| 120 |
<span class="shell">ifconfig tun0</span> |
| 121 |
<p>The output should look like:</p> |
| 122 |
<pre> |
| 123 |
tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 |
| 124 |
inet addr:192.168.255.1 P-t-P:192.168.255.1 Mask:255.255.255.0 |
| 125 |
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 |
| 126 |
RX packets:0 errors:0 dropped:0 overruns:0 frame:0 |
| 127 |
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 |
| 128 |
collisions:0 txqueuelen:500 |
| 129 |
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) |
| 130 |
</pre> |
| 131 |
<p>Check the "UP" keyword; you forget the "up" option in the socat command if |
| 132 |
it is missing.<p> |
| 133 |
<p>Check if the correct IP address and network mask are displayed.</p> |
| 134 |
|
| 135 |
<h3>Routing</h3> |
| 136 |
<p></p> |
| 137 |
<span class="shell">netstat -an |fgrep 192.168.255</span> |
| 138 |
<p>The output should look like:</p> |
| 139 |
<pre> |
| 140 |
192.168.255.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0 |
| 141 |
</pre> |
| 142 |
|
| 143 |
<h3>Other problems</h3> |
| 144 |
<p>Another reason for failure might be iptables.</p> |
| 145 |
<p>Run socat with options <tt>-d -d -d</tt>, this will show every data transfer |
| 146 |
between the two processes. Each ping probe should cause a forth and a back |
| 147 |
transfer.<p> |
| 148 |
|
| 149 |
<h2>History</h2> |
| 150 |
<p> |
| 151 |
Linux TUN/TAP support was added to socat in version 1.6.0.</p> |
| 152 |
|
| 153 |
<p>This document was last modified in February 2010.</p> |
| 154 |
|
| 155 |
<h2>More info about socat TUN/TAP support</h2> |
| 156 |
|
| 157 |
<h3>Links regarding this tutorial</h3> |
| 158 |
<a href="socat.html#ADDRESS_TUN">socat address tun</a><br> |
| 159 |
|
| 160 |
<h3>socat options for TUN/TAP addresses</h3> |
| 161 |
<a href="socat.html#GROUP_TUN">TUN/TAP options</a><br> |
| 162 |
|
| 163 |
<h2>References</h2> |
| 164 |
<a href="http://www.dest-unreach.org/socat">socat home page</a><br> |
| 165 |
<a href="socat.html">socat man page</a><br> |
| 166 |
<a href="http://openvpn.net/">OpenVPN home page</a><br> |
| 167 |
<a href="http://en.wikipedia.org/wiki/TUN/TAP">TUN/TAP on Wikipedia</a><br> |
| 168 |
|
| 169 |
<p> |
| 170 |
<small>Copyright: Gerhard Rieger 2007-2010</small><br> |
| 171 |
<small>License: <a href="http://www.fsf.org/licensing/licenses/fdl.html">GNU Free Documentation License (FDL)</a></small> |
| 172 |
</p> |
| 173 |
|
| 174 |
</body> |
| 175 |
</html> |