G27
Self-checkedTier 1 6 min readcn.nat

Network address translation

Why NAT exists (a shortage of public IPv4 addresses and private address blocks), how a NAT router with one public address translates both the address and the port number of outgoing packets, keeps a translation table and maps replies back, what happens to packets that arrive for an unknown port, how many simultaneous connections one public address can support, and the objections to NAT (breaking end-to-end addressing, servers behind NAT, protocols that carry addresses); the translation logic is simulated with a real table and checked for invertibility and exhaustion.

Why it exists

IPv4 has only 2322^{32} addresses, fewer than the number of devices. Network address translation lets a whole home or company network, using private addresses that are valid only inside, share one public address when it talks to the Internet. The NAT router rewrites the addresses (and port numbers) of packets that pass through it. GATE asks how the translation table works and what its limits are.

Prerequisites

You need the private address ranges and the structure of an IPv4 address (IPv4 addressing). Ports are the transport-layer identifiers of processes (TCP segments and UDP datagrams carry a source and destination port of 1616 bits, TCP).

Precise definitions

Private addresses (RFC 1918, quoted): 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. They can be used by anybody inside their own network but are never routed on the public Internet.

A NAT router sits between the private network and the Internet and has one (or a few) public addresses. In the common form, NAPT (network address and port translation, also called PAT or "masquerading"):

  • For a packet leaving the private network, it replaces the source address and source port by its own public address and a new, unused public port, and stores the pair in the NAT translation table: public port ↔ (private address, private port).
  • For a packet arriving from outside with a destination port that is in the table, it replaces the destination address and port by the private ones and forwards it inside. If the port is not in the table, the router has no way to decide which host is meant and drops the packet.
  • The router recomputes the IP header checksum and the TCP/UDP checksum (which covers the addresses and ports) for every rewritten packet.

Entries are created by the first outgoing packet of a connection and removed when the connection ends or after a timeout of silence.

How it works

A NAT with one public address 128.119.40.1: start

Computed by a script, not typed
eventpacket before translationpacket after translationtranslation table entry
1. outbound
2. inbound
3. outbound
4. inbound
5. inbound
6. outbound

Step 0

Private hosts 10.0.0.1 and 10.0.0.2 behind one public address. Two hosts using the same private port number (3345) are separated by different public ports.

0 / 6
computed by script: fragments are cut and reassembled, routing tables are computed round by round, flooding is simulated. python3 scripts/gen-cn3-traces.py

The first host 10.0.0.1 opens a connection from its private port 33453345 to a web server 128.119.40.186:80. The NAT router, whose public address is 128.119.40.1, assigns the public port 10241024 and writes 1024 ↔ 10.0.0.1:3345. The server sees a connection from 128.119.40.1:1024 and answers to that endpoint; the router looks up port 10241024 and rewrites the destination to 10.0.0.1:3345. A second host 10.0.0.2 also uses the private port 33453345: this is possible because the router gives it a different public port 10251025. The ports are the only thing that tells the replies apart, which is why plain address translation (without ports) could serve only one inside host at a time per public address. A packet addressed to the public port 10301030 finds no entry and is dropped.

Capacity. A port number has 1616 bits. In the model of this site the translated ports are 1024…655351024 \dots 65535, so 64 51264\,512 simultaneous translations are possible with one public address; the number of hosts that can share it follows:

How many hosts can share one public address: start

Computed by a script, not typed
loadhosts
1 connection per host
2 connections per host
5 connections per host
10 connections per host
50 connections per host
100 connections per host

Step 0

The 16-bit port field limits the number of simultaneous translations. The range 1024 … 65535 is the usual choice for translated ports (quoted convention).

0 / 6
computed by script: fragments are cut and reassembled, routing tables are computed round by round, flooding is simulated. python3 scripts/gen-cn3-traces.py

(The exact count depends on the implementation; some NATs can reuse a public port for connections to different remote endpoints, which raises the limit. The usual port range is quoted from common practice.)

Where NAT is configured. Servers inside a private network are reachable from outside only if the administrator creates a static mapping (port forwarding: public port 8080 is forwarded to a private web server).

Worked examples

EX 1Reading a NAT table

Easy

NAT table: 5001 ↔ 192.168.1.10:4000, 5002 ↔ 192.168.1.11:4000. A packet from the Internet arrives at 203.0.113.5:5002. The router sends it to 192.168.1.11:4000. A packet to port 50035003 is dropped.

EX 2Two hosts, same private port

Medium

Hosts 10.0.0.1 and 10.0.0.2 both open connections from port 33453345 to the same server. Without port translation the server would see two connections from the same public address and port, and the replies could not be told apart. With NAPT they leave as 128.119.40.1:1024 and 128.119.40.1:1025.

EX 3How many hosts

Medium

One public address, 64 51264\,512 usable ports, each host keeping 55 connections: ⌊64 512/5⌋=12 902\lfloor 64\,512/5 \rfloor = 12\,902 hosts. A browser that keeps 5050 connections open limits the number to 12901290.

EX 4Typical questions

GATE level

(a) "What does a NAT router replace in an outgoing packet?" The source IP address and the source port (and the checksums are recomputed). (b) "What information does the NAT translation table store?" Public port, private IP address and private port (and often the protocol and remote endpoint). (c) "Can an outside host start a connection to a host behind NAT?" Not without a static mapping or a hole punched by outgoing traffic. (d) "How many private hosts can one public address serve with NAT using ports?" About 2162^{16} simultaneous connections, not hosts. (e) "What problem does NAT solve?" Shortage of public IPv4 addresses.

Cheat box

Cheat box

  • Private blocks 10/8, 172.16/12, 192.168/16; NAT router has a public address.
  • Outgoing: replace source IP and port by public IP and a new port, record in the table. Incoming: look up the destination port, replace the destination.
  • Unknown port: packet dropped.
  • Limit: about 65 00065\,000 simultaneous translations per public address (the 16-bit port).
  • Breaks end-to-end addressing; servers behind NAT need port forwarding.

Traps and GATE patterns

Connections

The private blocks are written in CIDR notation (CIDR). The ports belong to TCP and UDP (TCP, UDP and ports). NAT slows the need for a larger address space, whose permanent answer is IPv6 (outside the syllabus line).

PYQs

1 previous year question from your book. I re-solved each one myself (0 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2024 · MSQQ21 · page 550 of the book
Which of the following fields is/are modified in the
IP header of a packet going out of a network address
translation (NAT) device from an internal network to
an external network?
  • (a)Total Length
  • (b)Source IP
  • (c)Destination IP
  • (d)Header Checksum

Self-check

Question 1

Why does the NAT router need the port number in its table?

Show answer

Several inside hosts share the one public address; the public port number is what tells the replies of their connections apart.

Question 2

A packet from outside arrives at the public port 40004000 and the table has no entry for it. What happens?

Show answer

It is dropped; the router does not know which host should receive it.

Question 3

Which header fields must be recomputed after the rewrite?

Show answer

The IP header checksum, and the TCP or UDP checksum because it covers a pseudo-header with the addresses and the ports.

Revision card

One-screen revision

  • NAPT: (private IP, port) ↔\leftrightarrow (public IP, new port) in a table.
  • Outgoing creates entries; incoming without entry is dropped.
  • Limit about 64 00064\,000 connections per public address; servers need port forwarding.

References

  • ·Kurose, Ross, Computer Networking: A Top-Down Approach (8th edition), Chapter 4, Section 4.3.4 (Network address translation)
  • ·Tanenbaum, Wetherall, Computer Networks (5th edition), Chapter 5, Section 5.6.2 (NAT)
  • ·Forouzan, Data Communications and Networking (5th edition), Chapter 19 (NAT)

Verification

scripts/enum-checks/cn_route_check.py: the translation model of scripts/cn_route.py (Nat) was exercised by 300 random sessions: every outbound packet leaves with the public address and a public port; the reply to that public port is translated back to exactly the original private endpoint; one private endpoint keeps one public port for all its remote peers and different private endpoints get different public ports; a packet for an unmapped public port is dropped; allocating ports 1024 to 65535 to distinct private endpoints runs out after exactly 64512 mappings. MODEL: translation by private endpoint (address and port) with sequential port allocation starting at 1024; real devices differ (full-cone, restricted-cone, port-restricted and symmetric NAT behaviours, timeouts that remove idle entries, different port ranges); the table of capacity assumes one public port per private endpoint. QUOTED, not verifiable here: the private address blocks (RFC 1918), the usual port range for translated ports, the statement that NAT breaks end-to-end connectivity, the treatment of ICMP and of protocols that embed addresses (FTP), NAT traversal techniques and the relation to IPv6. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (CRC remainders, binomial tail, checksums); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never