nattraversal.guide A guide by Firezone

NAT traversal without an open inbound port

NAT traversal lets two devices behind different routers send packets to each other. This guide explains the NAT types, STUN, TURN, ICE, and UDP hole punching in plain words. It also shows when a relay is the only path that works.

See how Firezone sets up a connectionBook a demo

How two peers behind NAT find a direct pathTwo peers are each behind a NAT device. Each peer asks a STUN and TURN server on the internet for its public address. The peers then send packets to each other at the same time, and a direct path opens between the two NAT devices. If no direct path opens, the server relays the encrypted packets.InternetNetwork ANetwork BSTUNSTUNdirect pathSTUN / TURN serverpublic addressNAT Ahome routerNAT Bcloud NATPeer Aprivate addressPeer Bprivate address
  1. Each peer learns its public address

    A peer sends a STUN request. The server answers with the address and port that the NAT put on the packet.

  2. The peers exchange candidates

    Each peer sends its list of addresses to the other peer through a signaling channel that both can already reach.

  3. Both sides send at the same time

    Each outbound packet opens a mapping in its own NAT. The packet from the other side then matches that mapping and gets in.

  4. A relay covers the hard cases

    If no direct pair answers, the peers send their encrypted packets through the TURN relay.

What NAT traversal is

Network address translation (NAT) lets many devices share one public IP address. The router rewrites the source address and port of each outbound packet. It keeps a table of these mappings, and it uses the table to send replies back to the correct device.

This design has a side effect. A device behind a NAT can start a conversation, but no one outside can start a conversation with it. A packet that matches no mapping has no destination, so the router drops it.

NAT traversal is the set of techniques that lets two devices connect when one or both are behind a NAT. The classic alternative is port forwarding, where an administrator adds a permanent inbound rule to the router. NAT traversal needs no such rule. The firewall continues to refuse all inbound traffic that the device did not ask for.

NAT types in plain words

Not all NAT devices behave the same way. Two properties decide if a direct connection is possible.

Endpoint-independent mapping

A NAT with endpoint-independent mapping gives a local socket one public port, and it uses that port for every destination. The public address that a STUN server sees is the same address that any other peer sees. This type is common in home routers, and it is the easy case for hole punching.

Symmetric NAT

A symmetric NAT selects a new public port for each destination. The address that a STUN server sees is correct only for packets from that server. A second peer cannot use it, because the NAT gives the connection to that peer a different port. Symmetric NAT is common in corporate firewalls and in some cloud NAT services.

Carrier-grade NAT

Carrier-grade NAT (CGNAT) is a NAT that an internet provider runs for many customers at once. Mobile networks and many home providers use it. The customer has no control over it, so port forwarding is not possible. A device behind CGNAT is frequently behind two layers of NAT, the home router and the NAT of the provider.

Filtering

A NAT also decides which inbound packets can use a mapping. Some NATs accept a packet from any source. Most accept a packet only from an address that the device already sent a packet to. Hole punching works with the second, stricter rule.

STUN, TURN, and ICE

Three standards do most of the work. Each one solves a different part of the problem.

  • STUN answers one question, which is the public address of the device. The device sends a request to a STUN server. The server replies with the source address and port that it saw. RFC 8489 defines the protocol.
  • TURN gives the device a socket on a public server. A TURN relay forwards each packet that arrives at that socket to the device. The relay always works, because the two peers only make outbound connections to it.
  • ICE is the algorithm that puts the two together. Each peer collects addresses, the peers exchange them, and they test each pair. RFC 8445 defines the algorithm.

ICE candidates

A candidate is an address and port where a peer can send and receive data. ICE candidates come in three types.

  • Host candidate. The address of a local network interface. It works when the two peers are on the same network.
  • Server-reflexive candidate. The public address that a STUN server saw. It is the outside of the NAT.
  • Relay candidate. A socket on a TURN relay. It is the path that works when the other two types fail.

The peers cannot talk to each other yet, so they exchange candidates through a signaling channel. Each peer then tests all pairs of local and remote candidates. ICE ranks the pairs that answer, and a direct pair ranks above a relayed pair.

UDP hole punching, step by step

UDP hole punching uses one fact about NAT devices. A NAT that saw an outbound packet to an address permits inbound packets from that address on the same port. If the two peers send at the same time, each NAT sees an outbound packet first.

Assume that peer A has the public address 35.10.10.10:52625, and peer B has 40.10.10.10:52625. Each peer learned its address from a STUN server and sent it to the other peer.

  1. Peer A sends a UDP packet to 40.10.10.10:52625.
  2. Peer B sends a UDP packet to 35.10.10.10:52625.
  3. NAT A rewrites the source of the packet from A, and it records a mapping for that destination.
  4. NAT B does the same for the packet from B.
  5. The packet from A arrives at NAT B. It matches the mapping from step 4, so NAT B lets it in.
  6. The packet from B arrives at NAT A. It matches the mapping from step 3, so NAT A lets it in.

The timing does not need to be exact. UDP has no connection state. If the first packet arrives too early and the NAT drops it, the next packet gets through.

Neither firewall has an inbound rule at any point. Each mapping exists only because a device inside sent a packet out, and it expires when the traffic stops. For that reason, the peers send small keepalive packets to keep the mapping open.

When a relay is the only option

Hole punching does not always succeed. These are the usual causes.

  • Symmetric NAT on the two sides. Each NAT selects a port that the other peer cannot know. No pair of server-reflexive candidates matches.
  • A firewall that blocks unknown UDP destinations. Some networks permit UDP only to a short list of ports or hosts.
  • A stateless firewall. A stateless filter does not remember outbound packets, so it drops the UDP reply unless a rule permits it.
  • Several layers of NAT. Carrier-grade NAT in front of a home router can work, but each layer adds one more device that must cooperate.

With one symmetric NAT, the result depends on the filter of the other NAT. Some combinations connect, and some do not.

In these cases, a relay carries the traffic. A relayed path has a cost. Each packet makes a detour through a third server, which adds latency and puts a limit on throughput. A good design uses the relay only when it must, and it moves to a direct path when one appears.

A relay does not need to read your data. If the two peers encrypt end to end, the relay forwards packets that it cannot decrypt.

How Firezone does NAT traversal

Firezone is zero trust access built on WireGuard. Clients and Gateways make outbound connections only. You do not open or forward an inbound port for a Client or for a Gateway.

The connection setup sequence in the documentation has three phases.

  1. Candidates. Each Client and Gateway collects host, server-reflexive, and relay candidates. Each one allocates a socket on the two nearest Firezone relays at sign-in, before it needs a connection.
  2. Tests. The peers exchange candidates through the Firezone control plane, and then they test the pairs. A direct pair ranks above a relayed pair.
  3. WireGuard handshake. The peers also get the public key of each other from the control plane. They do a WireGuard handshake on the selected path. The session keys rotate every 2 minutes.

A Client gets keys and candidates only after a Policy permits the connection. A device with no Policy cannot exchange a packet with the Gateway.

Direct first, relay when necessary

If a direct path works, traffic goes straight from the Client to the Gateway. If no direct path works, traffic goes through a Firezone relay. The relay forwards WireGuard packets and cannot decrypt them.

The article Nothing on the wire but WireGuard describes the newer design of this process. The WireGuard session starts through the relays, which always connect. Firezone then sends probes inside the encrypted tunnel to find a direct path, and it moves the traffic to the best path. A person who watches the network sees only WireGuard packets.

Port forwarding, relay only, and hole punching with a relay

There are three common ways to connect two devices that are behind NAT. They differ in the firewall work, the speed, and the cases that they cover.

Port forwardingRelay onlyHole punching + relay (Firezone)
No inbound firewall ruleNoOne rule for each serviceYesYes
Direct path between the peersYesNoAll traffic takes a detourYesWhen the NATs permit it
Works when the two sides have symmetric NATPartialYou must control one NATYesYesThrough the relay
Works behind carrier-grade NATNoThe provider owns the NATYesYes
Service stays hidden from the internetNoThe port answers any senderYesYes
Setup work on each routerManual, and again after each changeNoneNone

The table describes the usual design of each approach. A specific product or network can be different. Test on your own networks before you decide.

Common questions

What is the difference between STUN and TURN?

A STUN server tells a device its public address, and then it is out of the path. A TURN relay stays in the path and forwards each packet. STUN is cheap and helps you connect directly. TURN costs bandwidth, but it works on networks where a direct path does not.

Does UDP hole punching work with symmetric NAT?

Not reliably. A symmetric NAT uses a different public port for each destination, so the other peer does not know where to send. If the two sides have symmetric NAT, a direct path usually fails and a relay carries the traffic.

Is NAT traversal a security risk?

NAT traversal does not open a permanent hole. A mapping exists only after a device inside sends a packet out, and it accepts replies only from that destination. The risk is in who can start the process. Make sure that a peer gets connection details only after it passes an access check.

Why is my connection relayed and not direct?

The most common causes are symmetric NAT on the two sides, a firewall that limits outbound UDP, and a stateless firewall that drops UDP replies. For Firezone, the ports reference lists what to allow, and it includes the rule for stateless firewalls.

Do I need to open firewall ports for Firezone?

You need no inbound ports in most cases. Clients and Gateways need outbound HTTPS on port 443, UDP port 3478 for STUN, UDP ports 49152-65535 for the relays, and UDP for WireGuard traffic.

Does a relay see my traffic?

A relay sees the source, the destination, and the size of each packet. With end-to-end encryption, it does not see the contents. Firezone encrypts traffic with WireGuard between the Client and the Gateway, and the relays do not have the keys.

Keep every inbound port closed

Firezone connects Clients to Gateways with hole punching, and it uses a relay when a direct path is not possible. You run the Gateways, and Firezone runs the control plane and the relays. The Starter plan is free.

See plans and pricingBook a demo