4.3.10 · D1Computer Networks

Foundations — NAT — why, how, types (SNAT, DNAT, PAT)

1,975 words9 min readBack to topic

Before you can understand why NAT exists or how SNAT/DNAT/PAT differ, you must own a handful of building blocks. This page defines every one of them from zero — no prior networking assumed. We build them in the exact order the parent note secretly relies on.


1. What is a "packet"? (the thing everything happens to)

The internet never sends your whole video or webpage in one piece. It chops the data into many envelopes, each labelled with where it's from and where it's going.

Figure — NAT — why, how, types (SNAT, DNAT, PAT)

Why the topic needs this: NAT is defined as "rewriting header fields." You cannot understand rewriting until you know a packet has a header separate from a payload.


2. What is an IP address? (the street address)

Why four numbers each ? Because each is stored in 8 bits. A bit is a single -or- switch; bits make one byte, and a byte can count from to . Four bytes = bits total.

Why this exponent matters: is the entire supply of addresses — and there are far more phones, laptops, and gadgets than that. That shortage is the WHOLE reason NAT was invented.


3. The slash notation /8, /16 (how big is a block of addresses?)

Since an IPv4 address has bits total, fixing of them leaves host bits. Each host bit doubles the choices, so:

Figure — NAT — why, how, types (SNAT, DNAT, PAT)

Why the topic needs this: the three private ranges the parent lists (, , ) are described entirely in this notation. Without /n you can't see why one is bigger than another. See IPv4 Addressing & Subnetting for the deeper prefix math.


4. Public vs private addresses (why homes can reuse the same numbers)

Why the topic needs this: "many private devices share few public addresses" is the entire job of NAT. This distinction is the hinge. See RFC 1918 Private Addresses.


5. What is a port? (the apartment / door number)

Why to ? A port is stored in 16 bits, and values, counted through .

Figure — NAT — why, how, types (SNAT, DNAT, PAT)

Why the topic needs this: PAT ("Port Address Translation") works by rewriting ports so that many hosts sharing one public IP stay distinguishable. No ports, no PAT. See Ports & TCP-UDP Headers.


6. The four fields NAT can touch (the address label as a 4-tuple)

Every connection is identified by four values written together. The parent note writes them as:

Why the topic needs this: SNAT, DNAT, and PAT are defined by which entries of this tuple they rewrite. It is the coordinate system of the whole topic.


7. The translation table (the doorman's notebook)

Figure — NAT — why, how, types (SNAT, DNAT, PAT)

Why the topic needs this: "keeps a translation table so it can reverse the rewrite" is the mechanical heart of how NAT is reversible. Everything else is bookkeeping around this table.


8. Checksum (the tamper-check number)

Why the topic needs this: the parent says NAT "recomputes the affected checksums." This is why: rewriting a field breaks the old checksum.


Putting it together — prerequisite map

Bits and bytes 8 bits = one number 0-255

IP address 32 bits

Port 16 bits number 0-65535

Exponents 2^32 total addresses

Address shortage motivates NAT

Slash notation /n fixes first n bits

Private ranges RFC 1918

Public vs private reuse

Packet = header + payload

Header holds the fields

The 4-tuple src IP src port dst IP dst port

Translation table reverses rewrites

Checksum must be recomputed

NAT and its types SNAT DNAT PAT


Equipment checklist

Test yourself — each should be an instant, confident answer before you read the main NAT note.

What is the difference between a packet's header and its payload?
The header is the outside label routers read/rewrite; the payload is the sealed data inside, normally untouched.
How many bits is an IPv4 address, and how many total addresses does that give?
32 bits, giving billion addresses.
Why can each of the four numbers in an IP only go up to 255?
Each is stored in 8 bits, and .
What does the /n in mean?
The first bits are fixed; the remaining are free host bits.
How many addresses are in a /16 block?
.
Why can millions of homes all use without conflict?
Private IPs are never routed on the public internet; the router swaps them for a public IP before packets leave.
What is a port and how many are there?
A 16-bit number () identifying which conversation/app a packet belongs to; of them.
Name the four fields in the connection tuple.
src IP, src port, dst IP, dst port.
What does the translation table let the router do?
Reverse each outbound rewrite so replies reach the correct internal device.
Why must NAT recompute the checksum after rewriting a field?
The checksum is computed from the header contents; changing a field makes the old value wrong, so the packet would be discarded as corrupted.

Connections

  • NAT — Hinglish parent note
  • IPv4 Addressing & Subnetting — the /n prefix and host-bit math
  • RFC 1918 Private Addresses — the three private ranges
  • Ports & TCP-UDP Headers — where ports live in the header
  • Firewalls vs NAT — why NAT is not security
  • STUN, TURN & NAT Traversal — when the payload hides addresses
  • IPv6 — the bigger address space that eases the shortage
  • Port Forwarding & DMZ — DNAT in practice