Sub topic 13
How NAT rewrites addresses
This chapter assumes you know IPv4 dotted-decimal addresses, the private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16), the IPv4 header fields including the header checksum, and the difference between a host and a router. It covers address-only translation. Port translation (PAT) is the next chapter.
What NAT does
Network Address Translation (NAT) is a function on a border router or firewall. It rewrites IP addresses in packets that cross between a private network and the public Internet. Private addresses are not routable on the Internet, so a packet leaving a private host would otherwise get no reply.
The NAT device sits at the edge and has two kinds of interface:
- The inside interface faces the private network.
- The outside interface faces the Internet and holds one or more public addresses.
Terms used below:
- Inside local: the private address of a host, as seen inside.
- Inside global: the public address that represents that host on the Internet.
Translating in both directions
NAT changes the source address of outbound packets and the destination address of inbound packets. The NAT device keeps a table that maps inside local to inside global. It builds the table in one direction and uses it in both.
The server only ever sees 203.0.113.10. The host never sees anything unusual.
Static and dynamic NAT
| Type | Mapping | Created | Typical use |
|---|---|---|---|
| Static NAT | One inside local to one fixed inside global | By the administrator | A server that must be reachable from outside |
| Dynamic NAT | Inside local to an address taken from a pool | On the first outbound packet | Many hosts sharing a small pool |
Both types map one private address to one public address. With dynamic NAT, the number of hosts that can talk to the Internet at the same time is limited by the pool size. Once the pool is empty, new hosts get no translation and their packets are dropped.
A small model
The class below models dynamic NAT. Outbound traffic creates mappings, and inbound traffic needs an existing mapping.
class DynamicNat:
def __init__(self, pool):
self.free = list(pool)
self.out = {} # inside local -> inside global
self.back = {} # inside global -> inside local
def outbound(self, src):
if src not in self.out:
if not self.free:
raise RuntimeError("pool exhausted")
pub = self.free.pop(0)
self.out[src] = pub
self.back[pub] = src
return self.out[src]
def inbound(self, dst):
return self.back.get(dst) # None means drop
nat = DynamicNat(["203.0.113.10", "203.0.113.11"])
print(nat.outbound("10.0.0.5"))
print(nat.outbound("10.0.0.6"))
print(nat.outbound("10.0.0.5"))
print(nat.inbound("203.0.113.11"))
print(nat.inbound("203.0.113.99"))
try:
nat.outbound("10.0.0.7")
except RuntimeError as e:
print(e)It prints 203.0.113.10, 203.0.113.11, 203.0.113.10, 10.0.0.6, None and pool exhausted. The third line shows that a host keeps its mapping. The fifth line shows that an unsolicited inbound packet has no entry and is dropped.
What else must change
Rewriting the address field is not enough. A correct NAT also updates the following:
- IP header checksum. The checksum covers the whole IP header, so any address change invalidates it. The router recomputes it. This happens anyway because the TTL is decremented at every hop.
- TCP and UDP checksums. These include a pseudo-header that contains the source and destination IP addresses. If NAT changes an address, it must adjust the transport checksum too. Otherwise the receiver discards the segment.
- ICMP error messages. An ICMP error carries a copy of the original packet's header. NAT must translate the addresses inside that copy as well.
- Addresses inside the payload. Some protocols, such as active-mode FTP, put IP addresses in the data. NAT does not rewrite them unless an application-layer gateway is installed.
Common errors
- Assuming a mapping exists before an inside host has sent anything. With dynamic NAT, no table entry exists until the first outbound packet, so connections started from outside fail. Static NAT avoids this.
- Translating the wrong field. Outbound packets change the source address. Inbound packets change the destination address. The other field stays as it was.
- Forgetting that dynamic NAT without ports limits concurrency to the pool size. N public addresses serve at most N active inside hosts.
- Believing that NAT is a security feature by design. Dropping unsolicited inbound packets is a side effect of the missing table entry. It is not an access-control policy.
- Forgetting that routers between the NAT device and the Internet must never see private addresses. NAT must translate before the packet leaves the outside interface.
Exercise
A host 192.168.1.20 sends a packet to 198.51.100.7 through a NAT router whose static mapping for this host is 203.0.113.10. Give the source and destination of (a) the packet on the Internet side and (b) the server's reply on the Internet side. Which checksums must the router update on the outbound packet?
Show answer
(a) Source 203.0.113.10, destination 198.51.100.7. (b) Source 198.51.100.7, destination 203.0.113.10. On the outbound packet the router updates the IP header checksum and the TCP or UDP checksum, because both cover the changed source address. The reply is translated back to destination 192.168.1.20 on the inside.
Exercise
A company uses dynamic NAT with a pool of 8 public addresses and no port translation. At most how many inside hosts can have active Internet sessions at once, and what happens to a ninth host's first packet?
Show answer
At most 8, because each active host holds one public address. The ninth host gets no translation, so its packet is dropped (the pool is exhausted) until a mapping is released.