Sub topic 1
Why NAT exists: private addresses and IPv4 exhaustion
Prerequisites
This chapter assumes you already know the following:
- Basic IPv4 addressing: dotted-decimal notation and CIDR prefixes such as
/24. - What a router and a default gateway do.
- That TCP and UDP use port numbers.
- How to run commands in a Linux shell.
You do not need to know anything about NAT itself. This chapter explains the problem NAT was built to solve. Later chapters cover how it works.
The size of the IPv4 address space
An IPv4 address is 32 bits long. That gives 2^32 = 4,294,967,296 possible addresses, about 4.3 billion.
That sounded like plenty in the early 1980s, when the internet connected a few hundred research networks. Two design choices made it worse than the raw number suggests.
- Classful allocation. Early IPv4 split the space into fixed classes. A Class A network (
/8) held about 16.7 million addresses. A Class B (/16) held 65,536. A Class C (/24) held 256. An organization with 300 hosts did not fit in a Class C, so it was often given a Class B. Most of that block went unused. - Reserved ranges. Not every address is usable on the public internet. Multicast (
224.0.0.0/4), the reserved block (240.0.0.0/4), loopback (127.0.0.0/8), and the private ranges described below are all excluded.
By the early 1990s, address demand was growing faster than supply. Classless Inter-Domain Routing (CIDR) in 1993 allowed blocks of any prefix length. This reduced waste and slowed the problem. It did not remove it.
Exhaustion in practice
Address allocation is handled in a hierarchy. The Internet Assigned Numbers Authority (IANA) gives large blocks to five Regional Internet Registries (RIRs). The RIRs give smaller blocks to ISPs and large organizations.
IANA handed out its last free blocks to the RIRs in February 2011. The RIRs then ran out one by one. APNIC was first in April 2011, and RIPE NCC followed in 2012. Others followed in later years. Today, new allocations come mostly from small reserved pools, waiting lists, and the transfer market, where organizations buy and sell address blocks.
The practical result is that a public IPv4 address is a scarce, priced resource. A household or small business cannot expect an ISP to give it dozens of public addresses.
Private address ranges
Many networks never need to be reachable from the whole internet. A printer, a laptop, and an office file server only need to talk to each other and to some outside services. [RFC 1918](https://www.rfc-editor.org/rfc/rfc1918) reserves three blocks for this use.
| Block | Range | Addresses | Typical use |
|---|---|---|---|
| `10.0.0.0/8` | 10.0.0.0 to 10.255.255.255 | about 16.8 million | Large enterprises, cloud virtual networks |
| `172.16.0.0/12` | 172.16.0.0 to 172.31.255.255 | about 1.05 million | Medium networks, container platforms |
| `192.168.0.0/16` | 192.168.0.0 to 192.168.255.255 | 65,536 | Home and small-office networks |
Three properties of these ranges matter for everything that follows.
- They are not globally unique. Any organization may use them without asking anyone. Thousands of networks use
192.168.1.0/24at the same time. - They are not routed on the public internet. ISPs and backbone routers are expected to drop packets whose source or destination is a private address. Your laptop at
192.168.1.20is therefore unreachable from outside by that address. - They are free. You can build a private network with millions of hosts without registering anything.
Other special-purpose ranges also appear in this tutorial. The block 169.254.0.0/16 is link-local, used when DHCP fails. The block 100.64.0.0/10 is shared address space reserved for carrier-grade NAT, covered in a later chapter.
The problem private addresses create
Private addressing solves the shortage but causes a new problem. Consider a host with a private address that wants to fetch a web page.
Suppose 192.168.1.20 sends a packet to a web server at 198.51.100.10. The request may travel across the internet, since the destination is public. The server then replies to 192.168.1.20. Routers on the return path will drop that packet or send it somewhere unrelated, because the address belongs to no single network.
The outside world needs to see a public source address. Something must stand between the private network and the internet and swap the address in each packet.
Network address translation as the answer
Network Address Translation (NAT) is that something. A NAT device sits at the border between a private network and a public one. It rewrites address information in packets as they pass through.
For outbound traffic, the device replaces the private source address with its own public address. When the reply comes back addressed to that public address, the device swaps the private address back in and forwards the packet to the right internal host.
From the server's view, every request comes from one public address. The server does not see the private hosts at all.
The 1994 specification [RFC 1631](https://www.rfc-editor.org/rfc/rfc1631) described this idea as a short-term fix for address exhaustion. Its successor, [RFC 3022](https://www.rfc-editor.org/rfc/rfc3022), describes the form most people use today. The mechanism turned out to be so useful that it became permanent.
Why many hosts can share one address
If NAT only swapped addresses, two internal hosts talking to the same server would be indistinguishable. The gateway would not know which host an incoming reply belonged to. Real NAT solves this by also rewriting port numbers, so a single public address can carry thousands of simultaneous connections. This is called port address translation (PAT), and the next chapters build up to it. For now, remember the key point: NAT converts a many-to-one address problem into a bookkeeping problem. The gateway must remember which internal connection each outside packet belongs to.
What NAT buys and what it costs
NAT became universal because it solved real problems.
- It conserves public addresses. A household with 30 devices uses one public address.
- It decouples internal and external addressing. You can renumber your ISP connection without touching internal hosts. You can also merge two companies' networks without changing the public side.
- It hides internal topology. Outsiders see one address, not your host layout.
It also has costs, and they explain much of the rest of this tutorial.
- Inbound connections fail by default. The gateway only has mappings for connections that began from inside. An outside host that tries to reach
192.168.1.20finds nothing. Port forwarding exists to fix this. - It breaks the end-to-end model. The address a host sees on its own interface is not the address the world sees. Protocols that embed IP addresses inside their payload, such as classic FTP and SIP, can break.
- The gateway holds state. Each connection uses memory in the translation table, and a table that fills up drops new connections.
- It complicates peer-to-peer traffic. Two hosts behind separate NAT devices cannot easily reach each other without extra techniques.
- Overlapping private ranges clash. If two networks both use
10.0.0.0/24and you connect them with a VPN, routing becomes ambiguous.
Seeing NAT from a host
You can observe NAT without configuring it. Compare the address your machine thinks it has with the address the internet sees.
# The address on your local interface (often a private address)
ip -4 addr show scope global
# The address the outside world sees for your traffic
curl -s https://ifconfig.meOn a home network the first command typically prints something like 192.168.1.20/24. The second prints a public address, such as one in your ISP's range. If the two differ, a NAT device is translating your traffic somewhere along the path.
Be careful with one case. If the second address is in 100.64.0.0/10, or if your router's WAN address differs from what curl reports, your ISP is probably doing NAT as well. That situation is carrier-grade NAT, and it affects whether port forwarding can work at all.
Where this leads
This chapter explained why NAT exists: IPv4 has only about 4.3 billion addresses, they ran out, and private ranges let organizations reuse the same addresses internally. NAT is the translation layer that lets those private networks reach the public internet.
The next chapters build the mechanism step by step. First you will see how ports and the 5-tuple identify a connection. Then you will see what NAT changes in a packet and how the gateway remembers each connection.
Exercise
A host at 10.4.7.9 sends a packet directly to a public web server, with no NAT device in the path. The server receives the packet and replies. Why does the reply never reach the host, and what does NAT change to fix this?
Show answer
The server addresses its reply to 10.4.7.9, which is an RFC 1918 private address. Private addresses are not routed on the public internet, so routers drop the reply or cannot deliver it to the right network. NAT fixes this by replacing the private source address with the gateway's public address on the way out. The server then replies to that public address, and the gateway translates the destination back to 10.4.7.9 before delivering the reply.
Exercise
Which of these addresses are private under RFC 1918, and which are not: 172.20.5.1, 172.32.0.1, 192.168.200.4, 100.64.3.3?
Show answer
172.20.5.1 and 192.168.200.4 are private. 172.20.5.1 falls inside 172.16.0.0/12, which covers 172.16.0.0 to 172.31.255.255. 172.32.0.1 is outside that range, so it is a public address. 100.64.3.3 is in 100.64.0.0/10, the shared address space for carrier-grade NAT. It is not an RFC 1918 range, though it is also not routed on the public internet.