Sub topic 10
NAT: sharing one public address
This chapter assumes you know what an IPv4 address is and what the private ranges are (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). It also assumes you know that DHCP hands out addresses and that a SOHO router sits between the LAN and the internet. By the end you should be able to explain how many devices share one public address, and what breaks when they can't.
The problem NAT solves
IPv4 has about 4.3 billion addresses, which is too few for every device. Private addresses are not routable on the internet, so ISPs and routers drop them. Network Address Translation (NAT) lets devices with private addresses reach public servers anyway. The router rewrites the source address of outgoing packets to its own public address, and it reverses the rewrite on replies.
A SOHO router does this for every packet. Your laptop at 192.168.1.20 and your phone at 192.168.1.31 both appear to the internet as the router's single public address.
Types of NAT
| Type | Mapping | Typical use |
|---|---|---|
| Static NAT | One private address to one fixed public address | Hosting a server that needs its own public address |
| Dynamic NAT | Private addresses to a pool of public addresses, first come first served | Larger sites with several public addresses |
| PAT (NAT overload) | Many private addresses to one public address, told apart by port number | Nearly all home and small office routers |
Port Address Translation (PAT) is what people usually mean by "NAT" on a SOHO router. The router tracks each conversation by its port numbers, not just its addresses.
How PAT works
The router keeps a translation table. When a LAN device opens a connection, the router picks an unused public-side port and records the mapping. When the reply arrives addressed to that port, the router looks up the entry and forwards the packet to the right device.
Each active connection gets its own table entry. A second device connecting to the same server gets a different public-side port, so the replies never mix. Real routers often keep the original source port when it is free and only change it when two devices collide. The sketch below always assigns a new port to keep the logic easy to follow.
A working model in code
This Python script simulates a PAT table. It also shows why unsolicited inbound packets are dropped.
import itertools
PUBLIC_IP = "203.0.113.10"
next_port = itertools.count(40000)
outbound_map = {} # (src_ip, src_port, dst_ip, dst_port) -> public_port
inbound_map = {} # (public_port, remote_ip, remote_port) -> (src_ip, src_port)
def outbound(src_ip, src_port, dst_ip, dst_port):
key = (src_ip, src_port, dst_ip, dst_port)
if key not in outbound_map:
public_port = next(next_port)
outbound_map[key] = public_port
inbound_map[(public_port, dst_ip, dst_port)] = (src_ip, src_port)
return PUBLIC_IP, outbound_map[key]
def inbound(remote_ip, remote_port, public_port):
return inbound_map.get((public_port, remote_ip, remote_port), "dropped")
print(outbound("192.168.1.20", 51000, "93.184.216.34", 443))
print(outbound("192.168.1.31", 51000, "93.184.216.34", 443))
print(inbound("93.184.216.34", 443, 40000))
print(inbound("198.51.100.7", 12345, 40000))The two LAN devices use the same source port, 51000, but receive public ports 40000 and 40001. The reply from the real server maps back to 192.168.1.20. The packet from 198.51.100.7 matches no entry, so it is dropped.
Inbound traffic and port forwarding
A packet that matches no table entry has nowhere to go, so the router discards it. This makes NAT feel like a firewall. It is a side effect, not a security feature, and the chapter on firewalls covers real filtering.
To host something on the LAN, such as a game server or a camera, you add a static entry. This is port forwarding. For example, you could forward public TCP port 8080 to 192.168.1.50 port 80. That entry stays in place even when no one has connected out first. A DMZ host goes further and sends all unmatched inbound traffic to one LAN device. Use it sparingly.
Forwarding to a device that gets its address by DHCP breaks when the address changes. Give the target a DHCP reservation or a static address first.
Edge cases and limits
- Table timeouts. Idle entries expire, typically after seconds for UDP and much longer for TCP. Long-idle connections can die silently, and applications work around this with keepalive packets.
- Table size. A cheap router has a limited number of entries. Heavy peer-to-peer traffic can fill the table and make new connections fail.
- Protocols that embed addresses. Active-mode FTP and some VoIP protocols put IP addresses inside the data. Routers use an application-layer gateway (ALG) feature to rewrite those. When an ALG is buggy, you may need to turn it off.
- Breaking end-to-end addressing. An outside host cannot start a connection to a NAT'd device without a forwarding rule. Peer-to-peer apps use techniques such as STUN and relay servers to work around this.
- Double NAT. Two NAT devices in a row, such as an ISP modem-router with your own router behind it, translate twice. Browsing works, but port forwarding fails unless you forward on both devices or put the first one in bridge mode.
- Carrier-grade NAT (CGNAT). Some ISPs, especially cellular and satellite providers, share one public address among many customers. The WAN address on your router is then in 100.64.0.0/10 or another private range, and inbound connections are not possible.
Checking NAT from the client
On Windows, ipconfig shows the private address that NAT will translate. The default gateway is the router's LAN address.
ipconfig
(Invoke-RestMethod https://api.ipify.org)The first command prints something like 192.168.1.20. The second asks an outside service which address it sees, and it prints the router's public address. Every device on the LAN gets the same answer from the second command.
Exam points
- PAT, also called NAT overload, lets many private hosts share one public address by using port numbers.
- NAT hides private addressing from the internet, but it is not a replacement for a firewall.
- Port forwarding creates a static inbound mapping. Pair it with a DHCP reservation.
- Symptoms of double NAT are working browsing and failing port forwarding or online-game connectivity.
Exercise
A user forwards TCP port 3389 on their home router to a PC for remote access. It works for a week, then stops. The router's port-forwarding rule is unchanged. The user can still browse the web from the PC. Give two likely causes and how you would check each.
Show answer
First, the PC's DHCP lease changed its address, so the rule now points to an old address. Run ipconfig on the PC, compare the address with the rule's target, and fix it with a DHCP reservation. Second, the ISP changed the arrangement, for example by placing the connection behind CGNAT or by changing the public address that the user connects to. Compare the router's WAN address with the public address seen by an external site. If the WAN address is private or in 100.64.0.0/10, inbound connections cannot reach the router, and the user needs a public address from the ISP or a different remote-access method.
Exercise
Two laptops on the same LAN both open connections from source port 50000 to the same web server on port 443. How does the router keep the replies separate?
Show answer
The router's PAT table maps each connection to a different public-side port, for example 40000 and 40001. Replies arrive addressed to those distinct ports, and the router looks up each one and rewrites the destination to the correct laptop's private address and original source port.