Sub topic 10
Private Address Ranges and How NAT Rewrites Addresses
Assume you know IPv4 notation, subnet masks, packets, routers, and TCP or UDP port numbers. This chapter focuses on addresses that are not globally routable and on the address translation performed by a network address translator (NAT).
Private address ranges
Private IPv4 addresses are reserved for internal networks. Routers on the public Internet do not forward packets whose source or destination belongs to these ranges. The ranges are defined by RFC 1918:
| CIDR range | Address interval | Common use |
|---|---|---|
| `10.0.0.0/8` | `10.0.0.0` to `10.255.255.255` | Large private networks |
| `172.16.0.0/12` | `172.16.0.0` to `172.31.255.255` | Medium-sized private networks |
| `192.168.0.0/16` | `192.168.0.0` to `192.168.255.255` | Home and small office networks |
The prefix length determines the size of each range. For example, /8 leaves 24 host bits, so 10.0.0.0/8 contains 2^24 addresses.
Private addresses may be reused by unrelated organizations. Two companies can both use 192.168.1.0/24 internally because those addresses are meaningful only inside their respective networks. This reuse creates a problem when an internal host needs to communicate with the public Internet.
Some other non-public addresses have different purposes. For example, 127.0.0.0/8 is loopback, 169.254.0.0/16 is commonly used for link-local addressing, and 0.0.0.0/0 means the default route rather than a private network.
import ipaddress
addresses = ["10.4.2.1", "172.20.5.9", "192.168.1.10", "8.8.8.8"]
for text in addresses:
address = ipaddress.ip_address(text)
print(text, address.is_private)How NAT rewrites addresses
Network Address Translation changes packet address fields as a packet crosses a router. A host might send a packet with source address 192.168.1.20, but the NAT router replaces that source with its public address, such as 203.0.113.7, before forwarding the packet.
The router stores a translation entry so that the reply can be reversed:
| Internal flow | Public flow |
|---|---|
| `192.168.1.20:51514 → 93.184.216.34:443` | `203.0.113.7:40001 → 93.184.216.34:443` |
For an outgoing packet, NAT usually performs these steps:
- Replace the private source address with the public address.
- Often replace the source port with an available public port.
- Recalculate checksums because the IP and possibly port fields changed.
- Save the mapping in a translation table.
- Forward the modified packet.
When the response arrives at 203.0.113.7:40001, the router looks up the table, restores destination 192.168.1.20:51514, recalculates checksums, and sends the packet internally.
NAT conserves public addresses and hides internal addressing, but it can interfere with protocols that embed IP addresses in their payloads. It also makes unsolicited inbound connections difficult unless a static mapping, port forwarding rule, or another traversal mechanism is configured.
Exercise
A private host 192.168.1.8:53000 connects through a NAT router using public address 198.51.100.4 and public port 62000. What destination does the router use for a reply sent to 198.51.100.4:62000?
Show answer
The router translates the reply's destination to 192.168.1.8:53000 using its NAT table.