NAT & PAT
SNAT vs DNAT, connection tracking, port forwarding, NAT traversal techniques, and why NAT breaks peer-to-peer connectivity.
Overview
Overview Network Address Translation (NAT) is the mechanism by which IP addresses are rewritten in packet headers as they traverse a router or firewall. NAT was originally designed as a stopgap to address IPv4 exhaustion — by hiding many private RFC 1918 addresses behind a single public IP, NAT allowed billions of devices to share a relatively small pool of public addresses. Today NAT is ubiquitous: home routers, cloud NAT gateways, container runtimes, and Kubernetes kube-proxy all rely on it.
Source NAT (SNAT) rewrites the source IP address of outgoing packets. This is the standard form of NAT used in home routers and cloud NAT gateways: a device with a private IP (10.0.0.5) sends a packet; the router rewrites the source to its public IP (203.0.113.1) and records the mapping in a connection tracking table. When the response arrives, the router looks up the connection and rewrites the destination back to 10.0.0.5.
PAT (Port Address Translation), also called NAPT or IP masquerade, is the most common form of SNAT. It maps multiple private (IP, port) pairs to a single public IP using different ports. This allows thousands of connections to share one public IP. The connection tracking table maps (private_ip, private_port, protocol) to (public_ip, public_port, protocol).
Destination NAT (DNAT) rewrites the destination IP (and optionally port) of incoming packets. DNAT is used for port forwarding: an external request to public_ip:80 is rewritten to private_ip:8080 and forwarded to an internal server. This is how Docker port publishing, Kubernetes NodePort and LoadBalancer services, and AWS Network Load Balancers work.
NAT fundamentally breaks the internet's end-to-end connectivity model. Two peers behind separate NATs cannot establish a direct connection by simply dialing each other's IP, because neither has a stable, reachable public endpoint. NAT traversal techniques — STUN, TURN, ICE, hole punching — are required to establish peer-to-peer connections in the presence of NAT. This complexity is a major driver of VPN adoption, WebRTC infrastructure costs, and the push toward IPv6.
Key concepts
- Connection Tracking (conntrack)
- SNAT (Source NAT)
- DNAT (Destination NAT)
- NAT Traversal and Hole Punching
- Hairpin NAT (NAT Reflection)
Interview questions
- Explain how Docker port publishing works under the hood using DNAT and iptables.
- Why does NAT break peer-to-peer connectivity, and how do STUN and TURN solve this for WebRTC?
Sign up free to read the full topic
More Networking topics
Start free on Cariara · Coding problems · System design studio