Connecting the Unreachable: NAT Traversal with Netmaker

published
April 4, 2025
TABLE OF CONTENTS
Experience Seamless Network Management
Sign up for a 2-week free trial and experience seamless remote access for easy setup and full control with Netmaker.

You've deployed devices across different networks – some in the cloud, some in an office, maybe even a few at home. You need them to communicate securely and directly. That's the core promise of Netmaker: creating seamless virtual overlay networks using WireGuard. But there's a common obstacle: Network Address Translation, or NAT. Most devices aren't directly on the public internet; they sit behind routers performing NAT. This translation process complicates direct peer-to-peer connections. So, how does Netmaker ensure your nodes can find and connect to each other, even when they're hidden behind NAT? It uses a combination of clever techniques known as NAT traversal. Understanding how this works is key to appreciating the reliability of your Netmaker network and troubleshooting any potential connectivity bumps.

The NAT Barrier

First, let's quickly understand the problem. NAT routers were primarily invented to conserve the limited pool of IPv4 addresses. Your laptop or server typically has a private IP address (like 192.168.1.10 or 10.0.0.5) that only makes sense within its local network. When it sends traffic to the internet, your router translates this private source IP to its own public IP address and assigns a unique outgoing port. It keeps a table mapping these translations so return traffic finds its way back. This works great for client-to-server communication initiated from inside the private network. However, it makes it very difficult for an external device (like another Netmaker node) to initiate a connection to a device behind NAT, because the external device only knows the router's public IP, not the specific private IP and port of the target machine inside. This is a fundamental challenge for peer-to-peer technologies like WireGuard, which Netmaker manages under the hood.

Netmaker's Strategy: Coordination and Discovery

At its core, Netmaker is a management server that orchestrates connections. The Netclient agent, running on each node, reports its local network information (like its IP address and WireGuard listening port) to the Netmaker Server. The server then distributes this information to other peers in the same network via a Message Broker, allowing them to configure their WireGuard connections.

The problem is that the IP address and port reported by the Netclient might be its private ones. Netmaker needs to discover the public IP address and port that the NAT router assigns for outgoing connections from that specific WireGuard port. This publicly reachable combination is often called the "public endpoint" or "reflexive address."

Technique 1: Finding the Public Endpoint with STUN

To discover this public endpoint, Netmaker utilizes STUN (Session Traversal Utilities for NAT). A STUN server is a simple service sitting on the public internet. The Netclient sends a packet to a STUN server, essentially asking, "What IP address and port did you see this packet come from?" The STUN server replies with the public IP and port it observed. The Netclient then reports this discovered public endpoint information back to the Netmaker server. Netmaker can be configured with a list of STUN servers to use for this process. For this to work reliably, firewalls need to permit outgoing UDP traffic to the STUN servers, as outlined in the Netclient firewall rules.

Technique 2: Punching Through with UDP Hole Punching

Once two nodes know each other's potential public endpoints (discovered via STUN and shared by the Netmaker server), they can attempt a direct connection using a technique called UDP Hole Punching. Here’s the idea:

  1. Node A sends a UDP packet from its WireGuard port to Node B's discovered public endpoint (IP:Port).
  2. Simultaneously, Node B sends a UDP packet from its WireGuard port to Node A's discovered public endpoint.

When Node A sends its packet out, its NAT router creates a temporary entry in its state table allowing incoming packets from Node B's public endpoint back to Node A's WireGuard port (because it's expecting a reply). Node B's NAT router does the same. If timed correctly, these outgoing packets effectively "punch holes" through their respective NATs, allowing the incoming packets from the peer to pass through. If successful, a direct peer-to-peer connection is established. This is the ideal scenario for Netmaker, enabling a fast and efficient mesh network. The server itself often plays a role using its embedded netclient capabilities to help coordinate this timing.

Technique 3: Relaying When Direct Connections Fail

Sometimes, UDP hole punching doesn't work. Certain types of NAT (like Symmetric NAT) or restrictive corporate firewalls can prevent direct connections. In these situations, Netmaker needs a fallback: relaying.

The general concept is standardized as TURN (Traversal Using Relays around NAT). While Netmaker doesn't implement a standard TURN server, it offers similar functionality through its own Relay mechanism. A designated node within your Netmaker network, one with a stable, publicly reachable IP address, acts as the relay. When Node A cannot reach Node B directly, it instead sends its traffic to the Relay Server. The Relay Server then forwards this traffic to Node B. Return traffic follows the same path in reverse.

Netmaker provides this functionality through Relay Servers (Pro), which are explicitly configured for specific nodes, often those known to be behind challenging NATs or firewalls. This is now part of the unified Gateways feature set. You can configure a Relay Node to ensure connectivity for hard-to-reach devices. Additionally, FailOver Servers (Pro) provide an automatic fallback relaying mechanism if peer-to-peer connectivity fails unexpectedly, enhancing network resilience.

Orchestration: Putting It All Together

Netmaker intelligently orchestrates these techniques. It prioritizes direct peer-to-peer connections via STUN and UDP hole punching for optimal performance and low latency. When direct connections aren't possible, or if explicitly configured, it utilizes Relays or Failover nodes. This dynamic approach is central to Netmaker's flexibility. The Netclient agent continuously monitors its status and communicates changes to the server, ensuring the network adapts to changing conditions.

Conclusion

Network Address Translation is a reality of modern networking, but it doesn't have to be a barrier to building powerful, distributed systems. Netmaker employs a sophisticated suite of NAT traversal techniques—STUN for discovery, UDP hole punching for direct connections, and Relaying as a reliable fallback—to ensure your devices can communicate securely and efficiently, wherever they are. This underlying magic allows you to focus on building your secure networks, whether for Remote Access, Site-to-Site connectivity, integrating non-native devices, or managing access via the Netmaker Desktop client, confident that Netmaker is working behind the scenes to bridge the gaps.

Explore the Netmaker features and check the Getting Started guide to build your own resilient network today.

Experience Seamless Network Management
Sign up for a 2-week free trial and experience seamless remote access for easy setup and full control with Netmaker.
More posts

GET STARTED

A WireGuard® VPN that connects machines securely, wherever they are.
Star us on GitHub
Can we use Cookies?  (see  Privacy Policy).