## What is TCP hole punching?

From: andrew cooke <andrew@...>

Date: Tue, 8 Feb 2011 07:19:15 -0300

It is common (largely because of the restricted number of IPV4 addresses) for
several computers to share the same "external" address.  This is done by NAT
(Network Address Translation) at the point where the internal network meets
the external network.

By default, NAT does not support "incoming" connections.  This is because
there is no simple way for a computer connecting from "outside" to identify a
particular internal computer (since all share the same external address).

The lack of incoming connections means that an application on a computer on an
internal network must initiate all network exchanges and, furthermore, can
only connect to a computer that does have an external address.  This is a
problem for peer-to-peer (P2P) systems, since it requires a separate, public
server to act as the target for connections.

The problem is made worse by the fact that the TCP specification does not
allow for a conversation to be "redirected" to a different machine.  When two
P2P peers have both connected to a central server there is no simple mechanism
for them to address each other directly; all traffic must continue to be
routed through the public server to which the initial connections were made.

This last point is the problem solved by TCP hole punching.  It is a mechanism
that allows two peers to converse directly, even when NAT is in use.

Because a variety of implementations are used, the details that follow are
only a rough sketch.  See the references posted by others for more details [I
am writing this answer not because I am an expert on this, but because I found
neither of the other answers to be very clear].

In general, hole punching requires the following:

- An external, public server that is the target for initial connections,
and which helps coordinate the connection process.

- Two peers behind NAT, who will eventually be connected directly.

- NAT implementations that are lenient in the traffic that they accept,
and which are predictable in how they operate.

- A protocol (like TCP) which is lenient in the traffic it accepts and
which, in particular, uses a state machine and messages that are more
symmetric than the server / client roles that are assumed at a higher
level.

The "leniency" I mention above is driven by the need for these components to
function reliably on unreliable networks and to support a wide variety of
traffic (it is not, typically, a result of poor implementation).

The general process (bearing in mind that this is only a rough sketch) for
hole punching is:

- Peers connect to a central, public server and agree on which pairs will
connect.

- The central server identifies, for each NAT, how future outgoing connections
are created (when a peer makes a connection that passes through the NAT then
a port must be opened on on the NAT to receive the response; typically the
port numbers used are sequential).

- Peers then open new connections, each aware of the external (NAT) address
and likely port of the other peer.

By manipulating the TCP traffic (eg. by setting TTL values so that some
outgoing packets cannot reach the other peer) and/or by exploiting uncertainty
in timings and leniency in the NAT and TCP implementations, and by exploiting
symmetry in the underlying state machines and messages, it is then possible
for the two new connections, each of which was opened as client, to reach a
state as though they were opened in a normal client / server conversation.
Once the two peer connections are in the desired state no further use is made
of the public server; the peers can communicate directly with each other.

Andrew