Sub topic 15
NAT limitations and trade-offs
This chapter assumes you know how NAT and PAT rewrite addresses and ports, and what the translation table holds. It covers what NAT costs, what it breaks, and why it is still widely used.
Why NAT exists
NAT was introduced to slow the exhaustion of IPv4 addresses. A whole private network can share one public address, so the number of public addresses you need falls sharply. Two side benefits follow:
- An organisation can change ISPs without renumbering its internal hosts.
- Internal addresses are not visible from outside.
The second benefit is incidental, because NAT is not a firewall. A NAT drops unsolicited inbound packets only because it has no table entry for them. A rule like that can be written in a real firewall without any translation.
Core limitations
Broken end-to-end connectivity
IP was designed so that any host can reach any other host. Behind a NAT, only inside hosts can start connections. The table entry is created by the first outbound packet, so an outside host cannot initiate contact with an inside server.
The diagram shows the drop.
Two workarounds exist:
- Static port forwarding. An administrator permanently maps a public port to an inside address and port.
- Hole punching and relays. Peer-to-peer applications use a third server to coordinate or relay traffic.
Port exhaustion
PAT identifies each flow by a public port number. A port is 16 bits, so there are at most 65,536 values. If ports below 1024 are reserved, that leaves 64,512 for translation. Once they are all in use, new connections fail until old entries expire.
The same limit applies to carrier-grade NAT (CGNAT), where an ISP puts many customers behind one public address. Each customer receives only a small slice of the ports.
State and timeouts
The NAT table is per-flow state that the router must hold and look up on every packet. This has several consequences:
- Idle entries expire. UDP entries typically expire within seconds to minutes, and idle TCP entries after much longer. Long-lived idle connections therefore break unless the application sends keepalives.
- Failover is hard. If the NAT router reboots, every active connection is lost. A plain router has no such state, so its traffic can be rerouted freely.
- Symmetry is required. Both directions of a flow must pass through the same NAT, which makes multi-homed designs harder.
Protocols that carry addresses in the payload
NAT rewrites only the IP and transport headers. Any protocol that puts an IP address or port inside its data breaks unless the NAT understands that protocol:
- Active-mode FTP. The client sends its own private address and port in the control channel, and the server then tries to connect back to that address.
- SIP and H.323. Signalling messages carry the addresses used for media streams.
- IPsec. The Authentication Header (AH) covers the IP addresses, so rewriting them invalidates it. ESP in tunnel mode usually needs NAT traversal, which wraps it in UDP.
A NAT with an application-level gateway (ALG) can rewrite these payloads. That adds complexity, and it fails for encrypted payloads.
Checksums and fragments
Changing an address or port forces the NAT to recompute the IP header checksum. It must also recompute the TCP or UDP checksum, which covers a pseudo-header containing the IP addresses. That is extra work on every packet.
Fragmentation causes a further problem. Only the first fragment carries the transport header with the port numbers. Later fragments have only the IP header, so the NAT cannot map them by port. It must either reassemble the datagram first or track the IP identification field.
ICMP error messages also need care. They quote the header of the original packet, so the NAT has to translate the embedded copy as well.
Operational costs
- Traceability. Many users share one address. Identifying who did what needs translation logs with timestamps and ports.
- Debugging. The address seen in a packet capture depends on which side of the NAT you are on.
- Layering. A network-layer device that reads and modifies transport-layer fields violates strict layering.
A small PAT simulation
The model below keys the table by inside address and port only. A real NAT also includes the destination in the key. A small port range makes exhaustion easy to see.
class Pat:
def __init__(self, public_ip, first_port, last_port):
self.public_ip = public_ip
self.free = list(range(first_port, last_port + 1))
self.out = {}
self.back = {}
def outbound(self, ip, port):
key = (ip, port)
if key not in self.out:
if not self.free:
raise RuntimeError("no free public ports")
public_port = self.free.pop(0)
self.out[key] = public_port
self.back[public_port] = key
return self.public_ip, self.out[key]
def inbound(self, public_port):
return self.back.get(public_port)
nat = Pat("203.0.113.1", 65530, 65535)
for i in range(1, 7):
print(nat.outbound("10.0.0." + str(i), 5000))
print(nat.inbound(65530))
print(nat.inbound(1234))
try:
nat.outbound("10.0.0.7", 5000)
except RuntimeError as err:
print("failed:", err)The six hosts get ports 65530 to 65535. An inbound packet for 65530 maps back to ('10.0.0.1', 5000). An unknown port returns None, which the NAT treats as a drop. The seventh host fails because no ports remain.
Trade-offs at a glance
| Aspect | Benefit | Cost |
|---|---|---|
| Address use | Many hosts share one public address | Port exhaustion under heavy load |
| Renumbering | Inside addresses stay fixed | Public address changes still affect port forwards |
| Reachability | Unsolicited inbound traffic is dropped | Servers and peer-to-peer apps need workarounds |
| Router design | Simple to deploy at the edge | Per-flow state, checksum rewriting, failover difficulty |
| Protocols | Works for most client-server traffic | Breaks or needs ALGs for FTP, SIP and IPsec AH |
IPv6 removes the shortage that motivated NAT. Each host can have a globally unique address, which restores end-to-end connectivity. Access control then belongs to a firewall.
Exercise
A PAT router has one public IP and uses ports 1024 to 65535 for translation, one port per flow. Each inside host keeps 4 simultaneous connections open. What is the maximum number of hosts it can support at once, and what happens to the next connection attempt after that?
Show answer
The router has 65535 - 1024 + 1 = 64,512 usable ports. Each host uses 4, so it supports 64,512 / 4 = 16,128 hosts. After that, no free port is left, so the NAT cannot create a new entry and the new connection fails until an existing entry expires.
Exercise
Why does active-mode FTP fail through a basic NAT, even though the client opened the control connection?
Show answer
The client puts its private IP address and a port number inside the FTP control payload. A basic NAT rewrites only the IP and TCP headers, so the server tries to connect back to the unreachable private address. An ALG that rewrites the payload fixes this, and so does passive mode.