Networking / NAT (network address translation)

NAT (network address translation)

How a home router lets every device share one public IP address: it rewrites each packet on the way out and keeps a table to steer the replies back in.

Beginner7 steps~6 min
Laptop — private address 192.168.1.10
Phone — private address 192.168.1.11
Router — holds the public address and the table
Server — a machine out on the internet
// THE SHORT VERSION

In 30 seconds

There are not enough IPv4 addresses for every device, so a home or office usually gets one public address. Devices inside use private addresses that only make sense locally.

The router rewrites the source of every outgoing packet to its own address and a port it chooses, and notes the pairing in a table. When a reply arrives on that port, the table says which device it belongs to.

A connection started from outside has no table entry, so it is dropped unless you add a port forwarding rule.

NAT (network address translation), step by step

STEPS01 / 07

This is the usual port-translating form of NAT (also called NAPT or PAT). Real tables also record the remote address and protocol, and expire idle rows. IPv6 has enough addresses to do without NAT.

STEP 01 / 07 Too few addresses
THE MATH
2^32 = 4 294 967 296 IPv4 addresses in total 1 public address per home
  1. Step 1: Too few addresses. IPv4 has about 4.3 billion addresses, fewer than the devices that want one. So a home gets a single public address, held by the router. The devices behind it use private addresses such as 192.168.1.10, which are reused in millions of homes and mean nothing on the internet.
  2. Step 2: A packet leaves the laptop. The laptop opens a connection to a web server. Its packet carries a source address and port (where replies should go) and a destination. The source is private, so a reply addressed to it could never be routed back across the internet.
  3. Step 3: The router rewrites the source. Before forwarding, the router swaps the source for its own public address and a free port it picks, 40001. It writes the pairing down in its translation table. To the server, the request simply comes from 203.0.113.7.
  4. Step 4: The reply finds its way back. The server answers to 203.0.113.7 port 40001. The router looks up 40001 in the table, finds the laptop, rewrites the destination back to 192.168.1.10:51000 and passes the packet in. Neither the laptop nor the server notices the swap.
  5. Step 5: Two devices, one address. The phone connects to the same server and happens to use the same source port, 51000. The router gives it a different public port, 40002. The port number is what tells the two conversations apart, so thousands of connections can share one public address.
  6. Step 6: Uninvited packets are dropped. An outside host tries to start a connection to the public address on port 8080. Nothing inside asked for it, so the table has no row for 8080 and the router has no idea which device it is for. The packet is discarded. This blocks unrequested traffic as a side effect, though it is not a full firewall.
  7. Step 7: Port forwarding lets one in. To host a game or web server at home, you add a permanent row by hand: anything arriving on public port 8080 goes to the laptop on port 8080. Now the same uninvited packet has a match and is delivered. This is port forwarding.
VALUES SO FAR
IPv4~4.3 billionlimit
laptop192.168.1.10private
phone192.168.1.11private
router203.0.113.7public
server····public
40001····learned
40002····learned
8080····forwarded

Spotted a mistake on this page?