Sub topic 14
PAT and the NAT translation table
This chapter assumes you know private address ranges, the basic idea of NAT, and that TCP and UDP headers carry 16-bit source and destination port numbers. It covers how Port Address Translation (PAT) lets many private hosts share one public address, and how to work with the translation table in exam problems.
Why plain NAT is not enough
Basic dynamic NAT maps each private address to one public address from a pool. Each active host uses up one public address, so a network with 200 active hosts needs 200 public addresses. Public IPv4 addresses are scarce, so this does not scale.
PAT, also called NAT overload or NAPT, removes the limit. The router translates the port number as well as the address. Many private hosts share one public IP address, and the router tells their flows apart by the public port it assigns to each.
| Type | Mapping | Public addresses needed |
|---|---|---|
| Static NAT | One private address to one fixed public address | One per host |
| Dynamic NAT | Private address to a public address from a pool | One per active host |
| PAT | (private IP, private port) to (public IP, public port) | One for the whole network |
The translation table
The NAT router keeps a table that records each active flow. A typical entry has four fields:
| Inside local (private) | Inside global (public) | Protocol | Remote endpoint |
|---|---|---|---|
| 192.168.1.10:3000 | 203.0.113.5:5000 | TCP | 198.51.100.7:80 |
| 192.168.1.11:3000 | 203.0.113.5:5001 | TCP | 198.51.100.7:80 |
Both hosts use source port 3000, which is legal because the ports belong to different machines. The router gives them different public ports, 5000 and 5001. That choice is what keeps the two flows apart.
Packet walk-through
For an outbound packet, the router does the following:
- It looks up the (source IP, source port) pair in the table.
- If there is no entry, it picks an unused public port and creates one.
- It rewrites the source IP and source port.
- It recomputes the IP header checksum and the TCP or UDP checksum, because both cover the changed fields.
For an inbound packet, the router matches the destination port against the table. It rewrites the destination IP and port back to the private values and forwards the packet inside. If no entry matches, the router drops the packet. This is why unsolicited inbound connections fail.
A small PAT simulator
This sketch keeps two dictionaries, one for each direction. It keys outbound lookups on (IP, port) only. A real router often also includes the remote endpoint and the protocol in the key.
class Pat:
def __init__(self, public_ip, first_port=5000):
self.public_ip = public_ip
self.next_port = first_port
self.out = {} # (private_ip, private_port) -> public_port
self.back = {} # public_port -> (private_ip, private_port)
def outbound(self, src_ip, src_port):
key = (src_ip, src_port)
if key not in self.out:
self.out[key] = self.next_port
self.back[self.next_port] = key
self.next_port += 1
return self.public_ip, self.out[key]
def inbound(self, dst_port):
return self.back.get(dst_port) # None means the packet is dropped
pat = Pat("203.0.113.5")
print(pat.outbound("192.168.1.10", 3000))
print(pat.outbound("192.168.1.11", 3000))
print(pat.outbound("192.168.1.10", 3000))
print(pat.inbound(5001))
print(pat.inbound(6000))The output is:
('203.0.113.5', 5000)
('203.0.113.5', 5001)
('203.0.113.5', 5000)
('192.168.1.11', 3000)
NoneThe third call reuses the existing entry, so a flow keeps the same public port for its whole lifetime.
Capacity and entry lifetime
The port field is 16 bits, so there are 2^16 = 65,536 port values. Ports 0 to 1023 are well-known ports, which leaves about 64,512 for translation. With one public IP address, a PAT router can therefore hold roughly 64,000 simultaneous flows. If you do not need to avoid well-known ports, GATE questions usually use the full 2^16 = 65,536.
Entries are not permanent. The router removes a TCP entry after a FIN or RST exchange, or after an idle timeout. UDP has no connection teardown, so UDP entries rely on timeouts alone.
Common errors
- Forgetting the checksums. The TCP and UDP checksums include a pseudo-header with the IP addresses. Changing the address therefore changes the transport checksum as well as the IP header checksum.
- Matching inbound packets on the source. The lookup uses the destination port of the incoming packet, which is the public port the router assigned.
- Assuming the private port is kept. The router may change the port even when there is no conflict. Many implementations try to preserve it, but the table only has to be consistent.
- Treating NAT as a firewall. Dropping unmatched inbound packets is a side effect of the table lookup, not a security design.
Exercise
A PAT router has public address 203.0.113.5 and assigns public ports starting at 6000, in order of first use. Host 10.0.0.2 opens a connection from port 4000 to 198.51.100.7:80. Then host 10.0.0.3 opens a connection from port 4000 to the same server. Then host 10.0.0.2 sends another packet on its first connection. Give the source address and port seen by the server for each of the three packets.
Show answer
The first packet creates the entry 10.0.0.2:4000 to 6000, so the server sees 203.0.113.5:6000. The second packet comes from a different private host, so it gets a new entry 10.0.0.3:4000 to 6001, and the server sees 203.0.113.5:6001. The third packet matches the first entry and reuses it, so the server sees 203.0.113.5:6000 again.
Exercise
A company uses one public IP address with PAT and reserves ports 0 to 1023. What is the maximum number of simultaneous flows, assuming a flow needs one unique public port?
Show answer
The usable ports run from 1024 to 65535, which is 65,536 - 1,024 = 64,512 flows.