NAT & PAT
Explain how NAT and port address translation let private hosts share a public address.
By Invicly Tech, , in Computer Networking. About 13 minutes to read.
In short
Network address translation rewrites IP addresses as packets cross a router, letting many private hosts share one public IPv4 address. Port address translation tracks each connection by source port so replies return to the right host. NAT conserves addresses but breaks end-to-end reachability.
Explained simply
There are not enough IPv4 addresses for every device in the world. NAT (network address translation) helps by letting many devices with private addresses, like 192.168.1.20, share one public address. The router changes the addresses in each packet as it passes through.
When your phone opens a website, the router swaps the phone's private address for the router's public one, and remembers that swap in a table. When the reply comes back, it looks in the table and swaps the address back, so the reply reaches your phone.
If two devices at home talk to the same website at the same time, how does the router tell the replies apart? It also changes the port number, a second small label in each packet. This is called PAT (port address translation) or NAT overload, and it is what nearly every home router does.
NAT has a side effect: devices outside cannot start a conversation with a device inside, because the router has no table entry for it. That is why hosting a game server or a camera at home needs port forwarding, and why some video calls need extra tricks to connect.
Press Play on a diagram to walk through it one step at a time.
- 1Host sends
10.0.0.5:51000 to 93.184.215.14:443
- 2Router rewrites
Source becomes 203.0.113.7:40001
- 3Table entry
40001 maps to 10.0.0.5:51000
- 4Reply arrives
To 203.0.113.7:40001
- 5Translate back
Delivered to 10.0.0.5:51000
- 1Internet server
Sees the ISP's public address.
- 2ISP CGNAT
100.64.x.x to a shared public IP.
- 3Home router NAT
192.168.1.x to 100.64.x.x.
- 4Laptop
Private address 192.168.1.20.
In depth
Static NAT, dynamic NAT and PAT
Static NAT maps one private address to one public address permanently, in both directions. It suits a server that must be reachable from outside, but it uses one public address per host.
Dynamic NAT gives inside hosts a public address from a pool, first come, first served, while they are active. If the pool has 10 addresses and an 11th host wants out, it fails. It is rare today.
PAT, also called NAT overload or masquerading on Linux, maps many private addresses to one public address and tells connections apart by rewriting the source port as well. A port is a 16-bit number, so one public address has about 64,000 ports per protocol (TCP and UDP separately), and many NATs can reuse the same public port for connections to different destinations. That is enough for a home or small office; large sites use several public addresses.
- 1 private to 1 public, fixed
- Works inbound and outbound
- Used for servers
- One public IP per host
- Private to a pool of publics
- Mapping while active
- Fails when the pool is empty
- Rare today
- Many private to 1 public
- Ports tell flows apart
- Outbound only by default
- Home and office routers
Reading the translation table
Cisco names four addresses. Inside local is the host's real private address. Inside global is the public address that represents it. Outside global is the remote server's real address, and outside local is how the server appears from inside, which is usually the same. show ip nat translations prints one row per flow, such as tcp 203.0.113.7:40001 10.0.0.5:51000 93.184.215.14:443 93.184.215.14:443.
Linux keeps the same information in connection tracking (conntrack). conntrack -L shows each flow with two tuples: the original direction, as the inside host sent it, and the expected reply, as it will arrive from outside. When the reply tuple's destination differs from the original source, NAT has been applied.
Entries expire. On Linux an established TCP flow stays for 5 days of idle time by default (nf_conntrack_tcp_timeout_established = 432000 seconds), while UDP entries expire far sooner. That is why apps behind NAT send keepalives on long, quiet UDP sessions.
- Inside local: real private address
- Inside global: public address used for it
- conntrack -L: original and reply tuples
- conntrack -D can delete a stuck entry
Port forwarding and hairpin NAT
Port forwarding is destination NAT: packets that arrive at the public address on a chosen port are rewritten to an inside host and port. On a Linux router with nftables, a prerouting rule such as iifname eth0 tcp dport 8443 dnat to 10.0.0.20:443 does it, and the forward chain of your firewall must allow the traffic. On Cisco, ip nat inside source static tcp 10.0.0.20 443 203.0.113.7 8443 creates the same mapping.
A common surprise: it works from outside, but not from inside when you use the public address. The packet reaches the router from the LAN, gets DNATed to 10.0.0.20, and the server replies directly to the client on the same LAN, which does not recognise the reply. Hairpin NAT (NAT loopback) fixes this by also rewriting the source, so the reply goes back through the router. Split DNS, which gives inside clients the private address, is often cleaner.
What NAT breaks, and how apps get through
NAT breaks protocols that write IP addresses inside their payload, such as active FTP and SIP. Routers use helpers called ALGs (application layer gateways) to rewrite those too, though they often cause their own bugs. IPsec ESP has no ports at all, so VPNs use NAT traversal: ESP is wrapped in UDP port 4500.
Peer-to-peer apps (video calls, games) have it hardest, because both sides may be behind NAT. RFC 4787 describes NAT behaviour. With endpoint-independent mapping, a host's inside address and port always map to the same public port, whatever the destination, which makes hole punching possible. With address-dependent or port-dependent mapping (often called symmetric NAT), each new destination gets a new public port, and hole punching usually fails.
The toolkit: STUN (RFC 8489) tells a host its public address and port; ICE (RFC 8445) tries every candidate pair; TURN (RFC 8656) relays the traffic through a server when nothing else works.
- Peer A to STUN and signalling server: STUN: what is my public address?1STUN: what is my public address?
- STUN and signalling server to Peer A: You are 203.0.113.7:400012You are 203.0.113.7:40001
- Peer B to STUN and signalling server: STUN: what is my public address?3STUN: what is my public address?
- STUN and signalling server to Peer B: You are 198.51.100.9:520004You are 198.51.100.9:52000
- STUN and signalling server to Peer A: Signalling: B is at 198.51.100.9:520005Signalling: B is at 198.51.100.9:52000
- Peer A to Peer B: UDP to B: opens a mapping in A's NAT6UDP to B: opens a mapping in A's NAT
- Peer B to Peer A: UDP to A: passes A's NAT, direct path works7UDP to A: passes A's NAT, direct path works
CGNAT, IPv6 and NAT as security
Many ISPs now put customers behind carrier-grade NAT (CGNAT): your home router gets an address from 100.64.0.0/10, the shared address space reserved by RFC 6598, and the ISP translates again to a public address shared by many homes. Port forwarding on your router then cannot work, because the ISP's NAT has no mapping for you. Shared addresses also mean one abusive user can get an address blocked for everyone, and ISPs must log port ranges to trace abuse.
IPv6 has enough addresses for every device, so NAT is not needed. For IPv6-only networks that must reach IPv4 servers, NAT64 with DNS64 translates, often using the well-known prefix 64:ff9b::/96.
NAT is not a firewall. PAT drops unsolicited inbound packets only as a side effect of having no mapping. A stateful firewall that allows established traffic and blocks new inbound connections gives the same protection, on IPv4 and IPv6, without rewriting addresses.
- CGNAT range: 100.64.0.0/10
- NAT64 well-known prefix: 64:ff9b::/96
- A stateful firewall, not NAT, is the security control
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Compare static NAT, dynamic NAT and PAT~40m
- 2Read a NAT translation table~40m
- 3Configure port forwarding~40m
- 4Explain NAT traversal problems~40m
Key points
- PAT (NAT overload) is what almost every home and office uses.
- Inbound connections need port forwarding (static NAT for a port).
- NAT is not a firewall, though stateful tracking blocks unsolicited inbound traffic.
- Peer-to-peer apps use STUN and TURN to traverse NAT.
Key terms
Tap a card to flip it. 0/4 known
Examples
# Linux router: masquerade LAN traffic out of eth0 (nftables)
sudo nft add table ip nat
sudo nft add chain ip nat post '{ type nat hook postrouting priority 100; }'
sudo nft add rule ip nat post oifname eth0 masquerade
sudo conntrack -L | head # view translation entriesReading Linux conntrack entries on a NAT router
The router's public address is 203.0.113.7, and a laptop at 10.0.0.5 is browsing. Step 1: in the first entry, the original tuple shows 10.0.0.5:51000 to 93.184.215.14:443. Step 2: the reply tuple expects packets from 93.184.215.14:443 to 203.0.113.7:40001, so the router rewrote the source to its own address and port 40001. Step 3: 431999 is the seconds left before the idle entry expires. Step 4: the UDP DNS entry has only seconds left, which shows how short UDP mappings are.
$ sudo conntrack -L
tcp 6 431999 ESTABLISHED src=10.0.0.5 dst=93.184.215.14 sport=51000 dport=443
src=93.184.215.14 dst=203.0.113.7 sport=443 dport=40001 [ASSURED] mark=0 use=1
udp 17 25 src=10.0.0.5 dst=1.1.1.1 sport=48211 dport=53
src=1.1.1.1 dst=203.0.113.7 sport=53 dport=48211 mark=0 use=1
$ sudo conntrack -L -p tcp --dport 443 | wc -l # count HTTPS flowsPort-forwarding a web server on a Linux router
The router has eth0 on the internet (203.0.113.7) and the server is 10.0.0.20:443 on the LAN. Step 1: enable forwarding. Step 2: create a nat table with a prerouting chain for DNAT and a postrouting chain for masquerading. Step 3: rewrite TCP port 8443 arriving on eth0 to 10.0.0.20:443. Step 4: masquerade LAN traffic leaving eth0. Step 5: test from outside the network, not from the LAN, because hairpin traffic needs an extra rule. If you also run a filter forward chain with a drop policy, allow this traffic there.
sudo sysctl -w net.ipv4.ip_forward=1
sudo nft add table ip nat
sudo nft add chain ip nat pre '{ type nat hook prerouting priority -100; }'
sudo nft add chain ip nat post '{ type nat hook postrouting priority 100; }'
sudo nft add rule ip nat pre iifname eth0 tcp dport 8443 dnat to 10.0.0.20:443
sudo nft add rule ip nat post oifname eth0 masquerade
sudo nft list table ip nat
# from outside: curl -k https://203.0.113.7:8443/Quick reference
- PAT (overload, masquerade) = many private hosts behind one public IP, told apart by port
- Cisco PAT: ip nat inside source list 1 interface Gi0/1 overload
- Cisco terms: inside local = private address, inside global = its public address
- Linux: conntrack -L lists translations; nft ... masquerade for SNAT, dnat to for forwarding
- CGNAT uses 100.64.0.0/10; port forwarding on your own router cannot fix it
- IPsec through NAT: NAT-T wraps ESP in UDP 4500
- STUN finds your public mapping, ICE tries candidate paths, TURN relays as a last resort
- NAT is not a firewall: use stateful filtering for security, on IPv4 and IPv6
Common mistakes
- Treating NAT as a security control instead of a firewall.
- Forgetting to enable IP forwarding on a Linux router.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Hosts 10.0.0.5 and 10.0.0.6 both open connections from source port 50000 to 93.184.215.14:443 through a PAT router with public address 203.0.113.7. Write the two translation rows the router might create.
- Exercise 2medium
Configure PAT on a Cisco router: LAN 10.0.0.0/24 on Gi0/0, internet on Gi0/1. Then add a port forward of public TCP 8443 to 10.0.0.20:443.
- Exercise 3hard
Two friends try a peer-to-peer video call. Both are behind NATs that assign a new public port for every new destination. Explain why hole punching fails, and what the app does instead.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is NAT a security feature?
- Not by design. It blocks unsolicited inbound traffic only because there is no mapping for it. Real protection comes from a stateful firewall, which works the same on IPv4 and IPv6.
- Why can't I port forward even though I configured my router correctly?
- Your ISP may use CGNAT: check whether your router's WAN address is in 100.64.0.0/10 or differs from what a what-is-my-IP site shows. If so, ask the ISP for a public IPv4 address, use IPv6, or use a tunnel or relay service.
- Does IPv6 use NAT?
- Normally no. IPv6 has enough addresses for every device to have a global one, and a stateful firewall provides the inbound protection. NAT64 exists only to let IPv6-only networks reach IPv4 servers.
- What is the difference between SNAT and DNAT?
- SNAT rewrites the source address, as PAT does for outgoing traffic. DNAT rewrites the destination address, as port forwarding does for incoming traffic.