Use of udp to traverse nat gateways
To create a simple text-only tunnel, use the commands
user@left $ nat-traverse 40000:natgw-of-right:40001 user@right $ nat-traverse 40001:natgw-of-left:40000
where 40000 is an unused \s-1UDP\s0 port on \*(C`left\*(C' and 40001 is an unused port on \*(C`right\*(C'. See \*(L"\s-1EXAMPLES\s0\*(R" for more.
This document describes nat-traverse v0.5.
nat-traverse establishes connections between nodes which are behind \s-1NAT\s0 gateways, i.e. hosts which do not have public \s-1IP\s0 addresses. Additionally, you can setup a small \s-1VPN\s0 by using pppd on top of nat-traverse (see \*(L"\s-1EXAMPLES\s0\*(R"). nat-traverse does not need an external server on the Internet, and it isn't necessary to reconfigure the involved \s-1NAT\s0 gateways, either. nat-traverse works out-of-the-box.
See \*(L"\s-1TECHNIQUE\s0\*(R" for how this is achieved.
Limitation: nat-traverse does not work with gateways which change the port numbers. This is a fundamental problem of nat-traverse's design, as the changed port numbers are (in general) not predictable.
Sets the local port to use and the remote address to connect to. Note that you have to give the \s-1IP\s0 address or hostname of the \s-1NAT\s0 gateway of the host you want to connect to, as the target host doesn't have a public \s-1IP\s0 address. Runs the specified command after establishing the connection. The command will be run with its \s-1STDIN\s0 and \s-1STDOUT\s0 bound to the socket, i.e. everything the command writes to \s-1STDOUT\s0 will be forwarded to the peer. If no command is specified, nat-traverse will relay input from \s-1STDIN\s0 to the peer and vice versa, i.e. nat-traverse degrades to netcat. Sets the number of initial garbage packets to send. The default, 10, should work with most firewalls. Sets the maximum number of seconds to wait for an acknowledgement by the peer. Quits nat-traverse after the tunnel has been established successfully. nat-traverse returns a non-0 statuscode to indicate that it wasn't able to establish the tunnel. \*(C`--quit-after-connect\*(C' is useful if you want another program to use the tunnel. For example, you could configure OpenVPN to use the the same ports as nat-traverse \*(-- thus OpenVPN would be able to cross \s-1NAT\s0 gateways.
nat-traverse establishes connections between hosts behind \s-1NAT\s0 gateways without need for reconfiguration of the involved \s-1NAT\s0 gateways.
Firstly, nat-traverse on host \*(C`left\*(C' sends garbage \s-1UDP\s0 packets to the \s-1NAT\s0 gateway of \*(C`right\*(C'. These packets are, of course, discarded by the firewall.
Then \*(C`right\*(C''s nat-traverse sends garbage \s-1UDP\s0 packets to the \s-1NAT\s0 gateway of \*(C`left\*(C'. These packets are not discarded, as \*(C`left\*(C''s \s-1NAT\s0 gateway thinks these packets are replies to the packets sent in step 1!
\*(C`left\*(C''s nat-traverse continues to send garbage packets to \*(C`right\*(C''s \s-1NAT\s0 gateway. These packets are now not dropped either, as the \s-1NAT\s0 gateway thinks the packets are replies to the packets sent in step 2.
Finally, both hosts send an acknowledgement packet to signal readiness. When these packets are received, the connection is established and nat-traverse can either relay \s-1STDIN/STDOUT\s0 to the socket or execute a program.
It's easy to setup a \s-1VPN\s0 (Virtual Private Network) by using the Point-to-Point Protocol Daemon, \*(C`pppd\*(C':
root@left # nat-traverse \ --cmd="pppd updetach noauth passive notty \ ipparam vpn 10.0.0.1:10.0.0.2" 40000:natgw-of-right:40001 root@right # nat-traverse \ --cmd="pppd nodetach notty noauth" 40001:natgw-of-left:40000
\*(C`pppd\*(C' creates a new interface, typically \*(C`ppp0\*(C'. Using this interface, you can ping 10.0.0.1 or 10.0.0.2. As you can see, \*(C`pppd\*(C' upgrades the data-only tunnel nat-traverse provides to a full \s-1IP\s0 tunnel. Thus you can establish reliable \s-1TCP\s0 connections over the tunnel, even though the tunnel uses \s-1UDP\s0! Furthermore, you could even add IPv6 addresses to \*(C`ppp0\*(C' by running \*(C`ip -6 addr add...\*(C'!
Note though that although this \s-1VPN\s0 is arguably a private network, it is not secured in any way. You may want to use \s-1SSH\s0 to encrypt the connection.
You can use \*(C`netcat\*(C' to forward one of your local \s-1UDP\s0 or \s-1TCP\s0 ports to an arbitrary \s-1UDP\s0 or \s-1TCP\s0 port of the remote host, similar to \*(C`ssh -L\*(C' or \*(C`ssh -R\*(C':
user@left $ nat-traverse 10001:natgw-of-right:10002 \ --cmd="nc -vl 20000" user@right $ nat-traverse 10002:natgw-of-left:10001 \ --cmd="nc -v localhost 22"
As soon as the tunnel is established (using \s-1UDP\s0 ports 10001 and 10002), \*(C`left\*(C''s \s-1TCP\s0 port 20000 is forwarded to \*(C`right\*(C''s \s-1SSH\s0 Daemon (\s-1TCP\s0 port 22):
user@some-other-host $ ssh -p 20000 user@left # Will connect to right's SSH daemon!
But do note that you lose the reliability of \s-1TCP\s0 in this example, as the actual data is transported via \s-1UDP\s0; so this is only a toy example. If you want reliable streams, use \s-1PPP\s0 on top of nat-traverse, as described above.
You can use <OpenVPN> over nat-traverse if you want to have a secure \s-1VPN\s0.
Using OpenVPN over nat-traverse requires only one change to OpenVPN's configuration file, presuming that you don't want to use OpenVPN's multi-client mode: You have to adjust the \*(C`code\*(C' and \*(C`lport\*(C' options accordingly, for example:
# Options to add to left's and right's OpenVPN config: port 60001 lport 60001
# Command to execute on left resp. right: root@left # until \ nat-traverse --quit-after-connect 60001:right:60001 \ do \ sleep 5 \ done; \ openvpn [...] root@right # until \ nat-traverse --quit-after-connect 60001:left:60001 \ do \ sleep 5 \ done; \ openvpn [...]<!--
The \*(C`until\*(C' loop ensures that OpenVPN will not be started before nat-traverse was able to establish the connection. Michael Kugele (\*(C`michael (at) kugele.net\*(C') also reported a way to still be able to use OpenVPN's multi-client mode with nat-traverse: As all instances of nat-traverse have to use unique ports (because a connection is identified by the source/destination port combination), you've to use redirection rules to redirect the ports used by nat-traverse to the port the OpenVPN daemon listens on:
iptables -t nat -A PREROUTING -p udp \ --dport $LPORT -j DNAT --to $HOST:$PORT iptables -t nat -A PREROUTING -p udp \ --dport $PORT -j REDIRECT --to-port $LPORT
$LPORT specifies the source port nat-traverse uses on the server side, and \*(C`$HOST:$PORT\*(C' is the address of the OpenVPN server.)
Only IPv4 is supported, nat-traverse won't work with IPv6 addresses. Drop me a note if you do need IPv6 support.
nat-traverse does not work with gateways which change the port numbers. This is a fundamental problem of nat-traverse's design, as the changed port numbers are (in general) not predictable.
The \s-1IP\s0 Network Address Translator (\s-1NAT\s0). K. Egevang, P. Francis. May 1994. (Obsoleted by \s-1RFC3022\s0) (Status: \s-1INFORMATIONAL\s0)
Traditional \s-1IP\s0 Network Address Translator (Traditional \s-1NAT\s0). P. Srisuresh, K. Egevang. January 2001. (Obsoletes \s-1RFC1631\s0) (Status: \s-1INFORMATIONAL\s0)
The Point-to-Point Protocol (\s-1PPP\s0). W. Simpson, Ed.. July 1994. (Obsoletes \s-1RFC1548\s0) (Updated by \s-1RFC2153\s0) (Also \s-1STD0051\s0) (Status: \s-1STANDARD\s0)
Website of Paul's \s-1PPP\s0 Package (open source implementation of the Point-to-Point Protocol (\s-1PPP\s0) on Linux and Solaris)
Dieser Vortrag zeigt, wie man einen Tunnel zwischen zwei Computern, die beide hinter NAT-Gateways sitzen, hinbekommt. Dazu wird ein neues Programm vorgestellt, welches sowohl einfache TastendrA\*~Xcke an die Gegenseite weiterleiten, als auch beliebige Programme mit Verbindungen zur Gegenseite starten kann. Damit ist ein einfaches \s-1VPN\s0 schnell aufgebaut.
Copyright (C) 2005, 2012 Ingo Blechschmidt, <[email protected]>.
You may want to visit nat-traverse's Freecode project page, <http://freecode.com/projects/nat-traverse/>.
This program is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the \s-1GNU\s0 General Public License for more details.
You should have received a copy of the \s-1GNU\s0 General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, \s-1MA\s0 02110-1301, \s-1USA\s0.