Sub topic 12
How NAT lets private hosts reach the internet
This chapter assumes you know the three RFC 1918 private ranges and the difference between a public and a private address. Routers on the internet do not forward packets to or from private addresses. Network address translation (NAT) is the mechanism that lets hosts with private addresses still use the internet.
The problem NAT solves
A laptop at 192.168.1.10 sends a packet to a web server at 198.51.100.20. The server cannot reply to 192.168.1.10, because thousands of networks use that same address. The packet needs a source address that is unique on the internet.
A NAT device, usually your router, sits on the border between the private network and the public one. It rewrites the source address of outgoing packets to its own public address. It reverses the rewrite on the replies.
Port address translation
Home and office networks use a form of NAT called port address translation (PAT), also written NAPT or "NAT overload". Many private hosts share one public address, and the router tells their connections apart by port number.
For each outgoing connection, the router does three things:
- It picks an unused public port.
- It records the mapping in a translation table.
- It rewrites the source address and port of the packet.
A small simulation
This Python sketch models the translation table. Real routers also track the protocol and the remote address, so this is a simplification.
import itertools
PUBLIC_IP = "203.0.113.5"
ports = itertools.count(40000)
table = {} # (private_ip, private_port) -> public_port
reverse = {} # public_port -> (private_ip, private_port)
def outbound(src_ip, src_port):
key = (src_ip, src_port)
if key not in table:
public_port = next(ports)
table[key] = public_port
reverse[public_port] = key
return PUBLIC_IP, table[key]
def inbound(dst_port):
return reverse.get(dst_port) # None means no entry, so the packet is dropped
print(outbound("192.168.1.10", 51000)) # ('203.0.113.5', 40000)
print(outbound("192.168.1.11", 51000)) # ('203.0.113.5', 40001)
print(outbound("192.168.1.10", 51000)) # ('203.0.113.5', 40000), reuses the entry
print(inbound(40001)) # ('192.168.1.11', 51000)
print(inbound(45555)) # NoneBoth laptops used source port 51000, yet they got different public ports. That is how one public address serves many hosts.
Common errors and misunderstandings
- Inbound connections fail. An outside host cannot start a connection to a private host, because no table entry exists yet. To host a server behind NAT, you configure port forwarding, which is a fixed table entry such as "public port 8080 goes to
192.168.1.50:80". - NAT is not a firewall. Dropping unsolicited inbound packets is a side effect of the missing table entry. It is not a security policy. Use a real firewall rule set for protection.
- Entries expire. The router removes idle entries after a timeout. A long-lived idle connection can break silently unless the application sends keepalive packets.
- Embedded addresses break. Some protocols put IP addresses inside the payload, for example older FTP and some VoIP signaling. NAT rewrites only the headers, so these protocols need special helpers or workarounds.
- Overlapping networks. NAT does not fix two private networks that use the same range when you connect them directly. Plan non-overlapping ranges first.
Exercise
Two laptops on a home network, 192.168.1.10 and 192.168.1.11, both open connections from source port 51000 to the same web server. How does the router keep the replies separate, and why can an outside host not start a new connection to 192.168.1.10?
Show answer
The router maps each private address and port pair to a different public port, for example 40000 and 40001, and records both in its translation table. A reply arriving on public port 40001 is rewritten and delivered to 192.168.1.11:51000. An outside host cannot initiate a connection to 192.168.1.10 because no table entry exists for it until that host sends traffic out, unless port forwarding is configured.