Sobes.tech
Junior

How does NAT (Network Address Translation) work?

sobes.tech AI

Answer from AI

NAT (Network Address Translation) allows multiple devices in a private (local) network to use a single public IP address to access the Internet. Address translation occurs as packets pass through a router or firewall.

Main types of NAT:

  • Static NAT: One private IP address is permanently mapped to one public IP address. Used rarely, mainly for servers.
  • Dynamic NAT: Private IP addresses are mapped to an available pool of public IP addresses.
  • PAT (Port Address Translation) or NAT Overload: Multiple private IP addresses are mapped to a single public IP address, using different source port numbers. This is the most common type of NAT.

How PAT works:

  1. A device in the local network sends a packet to the Internet.
  2. The NAT router intercepts the packet.
  3. It replaces the private source IP address and port number with its public IP address and a new unique port number.
  4. The router records this translation in the NAT state table.
  5. The packet with the new address and port is sent to the Internet.
  6. When the response packet arrives at the NAT router, it uses the state table to determine which device in the local network it was intended for.
  7. The router restores the original private IP address and port number and forwards the packet to the appropriate device in the local network.

Example of a NAT state table entry (simplified):

Private IP:Port Public IP:Port
192.168.1.10:1234 203.0.113.5:5678
192.168.1.11:9876 203.0.113.5:9879
// Packet from local network to Internet:
// Original: src=192.168.1.10:1234, dst=8.8.8.8:53
// After NAT: src=203.0.113.5:5678, dst=8.8.8.8:53

// Response packet from Internet to local network:
// Original: src=8.8.8.8:53, dst=203.0.113.5:5678
// After NAT: src=8.8.8.8:53, dst=192.168.1.10:1234

NAT helps conserve public IP addresses and enhances security by hiding private IP addresses of devices in the local network.