Sub topic 1
Why NAT exists: private addresses and IPv4 exhaustion
Prerequisites
This chapter assumes you know the following:
- Basic IPv4 addressing and subnet masks, including CIDR notation such as
/24. - What a router and a default gateway do.
- That TCP and UDP use port numbers.
You do not need to run any commands yet. Later chapters add the hands-on Linux work.
The address shortage
An IPv4 address is 32 bits long, so there are 2^32, or about 4.3 billion, possible addresses. That sounded like plenty in the early 1980s. It stopped being enough as homes, phones, servers, and sensors all needed to be online.
Addresses are handed out by a hierarchy. IANA gives large blocks to the five regional internet registries (RIRs). The RIRs give smaller blocks to ISPs and large organizations. The pool of unallocated blocks ran out at IANA in 2011. Several RIRs have since run out of their own pools too.
Two things followed:
- Organizations could no longer expect a public address for every device.
- Addresses became something to conserve, bought, or sold.
The long-term fix is IPv6, which has 128-bit addresses. Moving every network to IPv6 takes years. NAT was the short-term fix that spread everywhere, and it is still in wide use today.
Private address ranges
RFC 1918 reserves three IPv4 ranges for use inside private networks. Routers on the public internet do not forward traffic to or from these ranges.
| Range | CIDR block | Addresses | Typical use |
|---|---|---|---|
| 10.0.0.0 to 10.255.255.255 | 10.0.0.0/8 | about 16.7 million | Large enterprises, cloud networks |
| 172.16.0.0 to 172.31.255.255 | 172.16.0.0/12 | about 1 million | Medium networks |
| 192.168.0.0 to 192.168.255.255 | 192.168.0.0/16 | 65,536 | Home and small office networks |
Because these addresses are never routed on the public internet, many unrelated organizations can use the same ones at once. Your home router and your neighbor's router can both use 192.168.1.0/24 without conflict.
That reuse creates the problem NAT solves. A host with 192.168.1.20 can send a packet to a public server. The server cannot reply to 192.168.1.20, because that address does not identify one specific machine on the internet.
What NAT adds
Network address translation (NAT) is a function on a router or firewall that rewrites the IP addresses, and often the port numbers, in packets as they cross between two networks. At the edge of a private network, the NAT device swaps the private source address for one public address on the way out. It reverses the swap for replies on the way back in.
This lets a whole private network share a small number of public addresses, often just one. The private hosts need no changes. They keep using their private addresses and default gateway.
The following sections build on this picture. Later chapters cover how the router tracks connections, how many hosts can share one address, and how outside clients can reach a private service.
Side effects of NAT
NAT was designed to save addresses, but it changes how a network behaves:
- Hosts are hidden by default. An outside host cannot start a connection to a private address, because the NAT device has no rule for where to send it. This resembles a firewall, but it is a side effect and not a security feature.
- End-to-end addressing is lost. The address a host sees on its own interface differs from the address the rest of the internet sees. Some protocols break because of this.
- The NAT device holds state. It must remember each connection to translate replies correctly.
Where you meet NAT
NAT appears in many places:
- Home routers, which share one ISP-assigned address among all your devices.
- Cloud virtual networks, where instances use private addresses and a NAT gateway provides outbound internet access.
- Container hosts, which translate container addresses to the host address.
- ISPs, which sometimes put many customers behind one address. This is carrier-grade NAT, covered later.
Exercise
Your office uses 10.0.0.0/8 and a partner's office also uses 10.0.0.0/8. Why can both organizations reach the public internet without address conflicts, and what does each need at its network edge?
Show answer
Private ranges are not routed on the public internet, so each organization can reuse them internally. Each needs a NAT device at its edge that rewrites the private source address to a public address on outbound traffic and reverses the translation for replies. A conflict would only appear if the two networks were connected directly, for example by a VPN, without further address translation.