AQA 7517, section 4.9.4.8
The basic concept
Network Address Translation (NAT) is a technique where a router changes the IP addresses in packets as they pass between a private network and the Internet.
Devices inside a network use private, non-routable addresses (4.9.4.6), which can't be used on the Internet. The router, which has one public address, translates between the two, so that many devices can share a single public IP address.
How it works
Going out to the Internet:
- A device (private address
192.168.1.10) sends a packet to a website. - The router replaces the private source IP address with its own public address, and replaces the source port number with one it generates.
- The router records the translation in a NAT table.
- The packet goes out onto the Internet, and the website sees only the router's public address.
Coming back:
- The website's reply is sent to the public address.
- The router looks in its NAT table to find which internal device the reply is for, replaces the destination with that device's private address and port, and forwards it.
Example NAT table
The router's public address is 86.12.34.56.
| Private address and port | Public address and port | Destination |
|---|---|---|
192.168.1.10:50001 |
86.12.34.56:60001 |
A web server |
192.168.1.11:50001 |
86.12.34.56:60002 |
A web server |
192.168.1.12:50777 |
86.12.34.56:60003 |
A game server |
Two devices might use the same port number internally (50001, as in the first two rows). The router gives each connection a different public port (60001 and 60002), and uses that to tell the replies apart. This is why many devices can share one address.
Why is NAT used?
- Conserves public IPv4 addresses. There aren't enough public IPv4 addresses for every device. With NAT, a whole home or company with hundreds of devices needs only one public address. This is the main reason it was introduced.
- Adds some security. Devices on the private network aren't directly visible or reachable from the Internet. The router drops unsolicited incoming packets that don't match a connection that someone inside started, because there's no entry in the NAT table. (It isn't a replacement for a firewall.)
- Easy network changes. The internal addressing can be changed freely, and an ISP change only affects the router's public address.
Limitations
- It breaks the end-to-end idea of every device having its own address, which complicates some applications (such as some games, video calls and servers).
- Servers inside the network can't be reached from the Internet without extra setup, such as port forwarding (4.9.4.9).
- The router has extra work to do, and must keep the NAT table.
- IPv6 has so many addresses that NAT isn't needed.
Worked example
Task: a home has a router (public address 86.12.34.56) and three devices. Explain how a reply to a laptop's web request finds its way back to the laptop and not the phone.
- The laptop (
192.168.1.10:50001) sends a request. The router rewrites the source as86.12.34.56:60001and stores that mapping in its NAT table. - The phone's separate request is rewritten as
86.12.34.56:60002. - The reply to the laptop's request arrives addressed to
86.12.34.56:60001. The router looks up port 60001 in the table, sees that it belongs to192.168.1.10:50001, and forwards the reply there. - A reply addressed to port 60002 goes to the phone. The public port number is what tells them apart.
Exam tip: say "the router replaces…", not "NAT replaces…". Going out, the router replaces the private source IP address with its public one and the source port with one it generates, and records the mapping in its NAT table. A reply is matched by its destination port. Why: many devices share one public address, which conserves IPv4 addresses.
Check yourself
What does NAT do?
A router translates between the private IP addresses used inside a network and its public IP address on the Internet, so many devices can share one public address.
Give two reasons for using NAT
It conserves the limited supply of public IPv4 addresses, and it adds some security by hiding internal devices and dropping unsolicited incoming traffic.
How does the router know which device a reply is for?
It uses its NAT table, which maps each public address and port number back to the private address and port of the device that started the connection.
Why is NAT not needed with IPv6?
IPv6 has so many addresses that every device can have its own public address.