Sub topic 1
Network Address Translation and Port Forwarding Explained
Prerequisites
Assume you know basic IPv4 addressing, subnet masks, default gateways, and TCP/UDP port numbers. You should also know that a router forwards packets between networks.
Why NAT exists: private addresses and IPv4 exhaustion
IPv4 has about 4.3 billion addresses, but the internet needs more addresses than that. Network Address Translation (NAT) reduces this pressure by allowing many devices to share one public IPv4 address.
Private IPv4 addresses are reserved for internal networks. They are not routed across the public internet.
| Range | Common use |
|---|---|
| 10.0.0.0/8 | Large private networks |
| 172.16.0.0/12 | Medium private networks |
| 192.168.0.0/16 | Home and small-office networks |
A home router might give a laptop 192.168.1.20 while using one public address for the whole home. The router translates traffic between these address spaces.
Ports, sockets, and identifying a connection
An IP address identifies a host. A port identifies an application endpoint on that host. Together, an address and port form a socket, such as 192.168.1.20:51514.
A flow is commonly identified by a five-tuple:
- Source IP address
- Source port
- Destination IP address
- Destination port
- Transport protocol, such as TCP or UDP
For example, a laptop might send TCP traffic from 192.168.1.20:51514 to 93.184.216.34:443.
What NAT changes in a packet
For outbound traffic, the router performs source NAT (SNAT). It replaces the private source address with its public address. It often also changes the source port. This combined technique is called Port Address Translation (PAT) or masquerading.
The web server replies to 203.0.113.5:40001, not directly to the private client. The router reverses the translation before delivering the reply internally.
How the NAT device remembers connections
The router stores a translation table. An entry links the internal flow to its public representation, including addresses, ports, and protocol. When a reply arrives, the router finds the matching entry and restores the internal destination.
NAT therefore depends on connection tracking. If the entry expires, or if an unsolicited packet has no matching entry, the router usually drops it. This is why outbound connections normally work automatically, while inbound connections require destination NAT or port forwarding.
Exercise
A client at 192.168.1.20:51514 connects to a web server on TCP port 443. What must the NAT router remember?
Show answer
It must remember the internal and public source ports, both IP addresses, and that the flow uses TCP, so it can translate the reply back to 192.168.1.20:51514.