Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 79 ยท The Network Layer: IP

NAT and Private Networks

Learn how NAT and NAPT work: translation tables, port forwarding, CGNAT, why peer-to-peer breaks behind NAT, and how hole punching gets around it.

Almost every home and office network uses private addresses such as 192.168.1.20. Those addresses are not routed on the public Internet, yet the machines that use them browse the web every day. The device that makes this possible is NAT, Network Address Translation: a router rewrites the addresses in packets as they pass through, so many private hosts can share one public address.

NAT was meant as a short-term fix for running out of IPv4 addresses. It worked so well that it became part of nearly every network, and it quietly changed how the Internet behaves. This lesson shows how the translation table works, how to let outside traffic in, why NAT hurts peer-to-peer applications, and how those applications work around it.

Private ranges and the basic idea

Three IPv4 blocks are reserved for private use (RFC 1918): 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Any organization may use them, and so thousands of networks reuse the same addresses. Routers on the Internet drop packets with private source or destination addresses, so a private host cannot talk to the outside world directly.

The NAT router sits on the border. Its inside interface has a private address and its outside interface has one public address. When an inside host sends a packet out, the router:

  1. replaces the source address with its own public address,
  2. remembers the translation in a table,
  3. recomputes the IP and transport checksums, because the addresses changed.

When the reply comes back to the public address, the router looks it up in the table and rewrites the destination back to the private host.

NAPT: translating ports too

If the router only swapped addresses, one public address could serve one inside host at a time. Real NAT uses NAPT (Network Address and Port Translation, also called PAT or "NAT overload"). The router also rewrites the source port, choosing a free one for each connection. The pair (public address, public port) is what identifies the connection.

flowchart LR
    A["Laptop<br/>192.168.1.20:51000"] --> R["NAT router<br/>203.0.113.5"]
    B["Phone<br/>192.168.1.21:51000"] --> R
    R -->|"203.0.113.5:40001"| S["Web server<br/>198.51.100.10:443"]
    R -->|"203.0.113.5:40002"| S

Two inside hosts use the same source port, so the router gives them different public ports.

A translation entry normally holds the protocol, the inside address and port, the outside address and port, and the public port that was assigned. Entries are created by the first outgoing packet and expire after a timeout, which is short for UDP (often 30 to 60 seconds) and long for established TCP connections (hours). For TCP, the router can also remove an entry when it sees the connection close.

Python A NAPT translation table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last two lines show a side effect that people often call "NAT security". Because the router has no entry for unsolicited packets, it drops them, so outsiders cannot start connections to inside hosts. That is a useful accident, but it is not a firewall: the protection comes from the missing table entry, and a real stateful firewall makes the same decision on purpose and with rules.

Port forwarding

Sometimes you want outside traffic to reach an inside server, for example a game server or a camera. Since no table entry exists, you create a permanent one by hand: port forwarding (also called a virtual server or static NAT for one port). The rule says "anything arriving at 203.0.113.5:8080 goes to 192.168.1.50:80".

Rules like this are usually written as three things: the protocol, the public port, and the inside address and port. The router then translates the destination of inbound packets (destination NAT) and the source of the replies (the usual source NAT). Other variants are the DMZ host, which forwards every unmatched port to one inside machine, and UPnP or NAT-PMP, which let an application on the inside ask the router to open a port itself. UPnP is convenient but widely disabled, because any malware on the inside can open ports too.

Python Port forwarding rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Carrier-grade NAT

The shortage got worse. Many ISPs no longer have a public IPv4 address for each customer. They put customers behind CGNAT (carrier-grade NAT, or large-scale NAT), a second layer of NAPT run by the provider. A home router then has an outside address from the shared range 100.64.0.0/10, which is reserved for exactly this purpose, and its traffic is translated again at the ISP.

CGNAT works for browsing, but it has costs:

  • You cannot forward ports, because you do not control the outer NAT.
  • Many customers share one public address, so one abuser can get the address blocked for all of them, and CAPTCHAs appear more often.
  • Logging "who used this address at 14:03" now needs the port as well as the address.
  • Games, voice calls and peer-to-peer apps are harder to connect.

If your router shows a WAN address that starts with 100.64 to 100.127, you are behind CGNAT.

What NAT breaks

NAT violates the end-to-end idea that a packet leaves the sender unchanged. The problems follow from that.

  • Inbound connections. Nobody outside can open a connection to a host with no table entry. This is fine for clients and a problem for servers and peers.
  • Addresses inside payloads. Some protocols carry IP addresses or ports in their data, such as FTP's PORT command and SIP. The NAT must rewrite them too (an application-level gateway, ALG) or the protocol breaks.
  • Encrypted or authenticated headers. IPsec's authentication covers the address fields, so NAT breaks it unless the packets are wrapped in UDP ("NAT traversal").
  • Timeouts. An idle connection loses its entry. Long-lived connections send keepalives to keep it alive, which is one reason chat apps wake up your phone regularly.
  • Complexity. Debugging needs both views of an address, and logs on the server show the router, not the user.

IPv6 removes the need for NAT, because every device can have a global address. A firewall (not NAT) then decides what is allowed in.

Peer-to-peer and hole punching

Suppose Alice and Bob are both behind NAT and want to talk directly, for a video call. Neither can connect to the other: each router drops the other's first packet. Applications solve this with hole punching, which uses the fact that NAT creates an entry for outgoing packets, and that entry lets matching replies in.

  1. Both clients contact a rendezvous server on the public Internet. The server sees each client's public address and port (as translated by their NATs) and tells each one the other's.
  2. Both clients send a packet to the other's public address at about the same time.
  3. Alice's packet creates an entry in Alice's NAT for "Bob". It is dropped by Bob's NAT, but when Bob's own packet toward Alice created an entry in his NAT, Alice's next packet matches it and gets in. Now both NATs have an entry and traffic flows directly.
sequenceDiagram
    participant A as Alice (behind NAT A)
    participant S as Rendezvous server
    participant B as Bob (behind NAT B)
    A->>S: hello (NAT A maps to 203.0.113.5:40001)
    B->>S: hello (NAT B maps to 198.51.100.8:50002)
    S->>A: Bob is at 198.51.100.8:50002
    S->>B: Alice is at 203.0.113.5:40001
    A->>B: packet (opens a hole in NAT A)
    B->>A: packet (opens a hole in NAT B)
    A-->>B: direct traffic

Each side's outgoing packet opens a hole that lets the other side's packets in.

Hole punching works only if the NAT keeps the same public port for the same inside port, no matter who the remote host is (an "endpoint-independent mapping"). Some NATs, often called symmetric, choose a new public port for every destination, so the port the server saw is useless to Bob. Then, and always when both sides are behind CGNAT with strict rules, applications fall back to a relay server that forwards the traffic. The standard toolkit is STUN (discover your public address), TURN (relay) and ICE (try all the options and pick the best); WebRTC uses all three.

Python Which NATs allow hole punching?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real NATs are more varied than "cone" and "symmetric". Some also filter by the remote address or port before letting a reply in, and some are unpredictable. That is why ICE tries many candidate paths instead of assuming one design.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the router change in a packet that leaves a NAPT network?

    The source address becomes the public address and the source port becomes a free public port. The entry lets replies be translated back. The checksums must be recomputed too.
  2. 2. Why does an unsolicited packet from the Internet fail to reach an inside host?

    Entries are created by outgoing packets. Without one (or a port-forwarding rule) the router has no inside host to deliver the packet to.
  3. 3. A router's WAN address is 100.72.4.9. What does that most likely mean?

    100.64.0.0/10 is the shared address space reserved for CGNAT. Port forwarding on your own router will not make you reachable from outside.
  4. 4. What is the role of the rendezvous server in hole punching?

    The server only exchanges addresses. After both peers send packets, each NAT has an entry and the data flows directly. If that fails, a relay (TURN) forwards the data.

Summary

  • NAT lets many private hosts share a public address. NAPT also rewrites ports and keeps a translation table of outgoing connections.
  • Unsolicited inbound packets have no entry and are dropped; port forwarding creates fixed entries for servers.
  • CGNAT adds a second NAT at the ISP, using 100.64.0.0/10, so customers cannot forward ports.
  • NAT breaks end-to-end addressing: protocols that embed addresses, IPsec, idle connections and peer-to-peer apps all suffer.
  • Hole punching with a rendezvous server connects two NATed peers when mappings are predictable; otherwise a relay is needed. IPv6 removes the reason for NAT.

Next, see how a host gets its address in the first place: DHCP.