IPv4 addresses are scarce, so most homes and organisations use private addresses inside and share a few public addresses for the internet. NAT makes this possible.

The Idea of NAT

Network Address Translation (NAT), running on a router or firewall at the edge of a network, rewrites the IP addresses (and often port numbers) of packets as they cross between the inside private network and the outside public internet. Private addresses (10/8, 172.16/12, 192.168/16) are not routed on the internet, so outgoing packets get a public source address; replies coming back to that public address are translated back to the private host. The router keeps a NAT translation table of these mappings.

Definition: Network Address Translation. A technique in which a router replaces the private IP addresses (and port numbers) in packets with public ones as they leave a network, and reverses the change for replies, using a translation table.

Types of NAT

Static NAT maps one private address to one public address permanently; it is used for servers inside the network that must be reachable from outside. Dynamic NAT maps private addresses to public ones taken from a pool, as needed; when the pool runs out, new hosts must wait. PAT (Port Address Translation), also called NAT overload or NAPT, maps many private addresses to one public address by also translating port numbers: each inside connection gets a unique outside port, so the router can tell replies apart. PAT is what every home router does. Port forwarding is a static rule that sends traffic arriving at a public port to a chosen inside host and port, such as a web server.

Advantages and Limitations

NAT conserves IPv4 addresses, since thousands of hosts share one public address; gives some privacy and security, since inside addresses are hidden and unsolicited incoming connections are dropped; and lets an organisation change ISPs without renumbering inside hosts. But it breaks the end-to-end principle: outside hosts cannot start connections to inside hosts without port forwarding. It causes trouble for peer-to-peer, VoIP and some VPN protocols that carry addresses inside their data (needing NAT traversal techniques such as STUN), adds processing and state at the router, and complicates troubleshooting and logging. Carrier-grade NAT (CGNAT) puts a second layer of NAT at the ISP. IPv6, with abundant addresses, removes the need for NAT.

Table 3.3: Types of NAT

Type Mapping Typical use
Static NAT One private to one public Inside servers
Dynamic NAT Private to public from a pool Organisations with a pool
PAT (overload) Many private to one public, by port Home and office routers
Port forwarding Public port to inside host Hosting a service

Example 3.3: A PAT translation table

Problem: Hosts 192.168.1.10 (port 50001) and 192.168.1.11 (port 50001) both open web connections through a router with public address 203.0.113.5. Show the translation table and how a reply is handled.

Solution:

  1. The first connection is mapped to 203.0.113.5:40001, the second to 203.0.113.5:40002.
  2. Both packets leave with source 203.0.113.5, but different ports.
  3. A reply arrives for 203.0.113.5:40002. The router looks it up and finds 192.168.1.11:50001.
  4. It rewrites the destination and forwards the packet inside.

Result: One public address serves both hosts; the port tells the replies apart.

public, next_port, table = "203.0.113.5", 40001, {}
def outbound(src_ip, src_port):
    global next_port
    key = (src_ip, src_port)
    if key not in table:
        table[key] = next_port
        next_port += 1
    return public, table[key]
def inbound(port):
    for inside, p in table.items():
        if p == port:
            return inside
    return None
for h in [("192.168.1.10", 50001), ("192.168.1.11", 50001)]:
    print(h, "->", outbound(*h))
print("reply to port 40002 goes to", inbound(40002))
print("unsolicited packet to port 45000:", inbound(45000))

Output

('192.168.1.10', 50001) -> ('203.0.113.5', 40001)
('192.168.1.11', 50001) -> ('203.0.113.5', 40002)
reply to port 40002 goes to ('192.168.1.11', 50001)
unsolicited packet to port 45000: None

Common mistakes

  • Thinking NAT is a full firewall. It hides addresses but does not inspect traffic.
  • Forgetting port forwarding when hosting a server behind PAT.
  • Using dynamic NAT when there are more hosts than public addresses; PAT is needed.

Key takeaways

  • NAT rewrites private addresses to public ones at the network edge.
  • Static NAT is one-to-one; dynamic NAT uses a pool; PAT shares one address by ports.
  • NAT conserves IPv4 addresses and hides inside hosts.
  • It breaks end-to-end connectivity and complicates P2P, VoIP and VPNs.

Practice questions

  1. What is PAT?
  2. Why can an outside host not reach a host behind PAT?
  3. Which protocol removes the need for NAT?

Answers to the practice questions

  1. Port Address Translation, which maps many private addresses to one public address using different port numbers.
  2. The router has no translation entry for unsolicited incoming traffic, unless port forwarding is set up.
  3. IPv6.

Sources

  • Official BCA syllabus Computer Networks, course code 26BCA404DS02, Unit III