Computer Networking › Section D · The network layer: data plane › Topic D.4
DHCP and network address translation
Open a laptop in a café and within seconds it has an address, a gateway and a DNS server, and it shares one public address with everyone else in the room. Two protocols make that happen: DHCP hands out addresses, and NAT lets many devices hide behind one.
Getting an address
An organisation first obtains a block of addresses, usually a slice of its ISP's larger block. Each interface then needs one address from that block. A network administrator can type it in by hand, which is still normal for routers and servers, but doing so for every phone and laptop would be slow and error-prone. Instead most hosts use the Dynamic Host Configuration Protocol (DHCP), which assigns addresses automatically. DHCP is often called plug-and-play: a device joins a subnet and is configured without any human action, and its address can be reused by another device once it leaves.
What DHCP provides
A DHCP server, which in a home is built into the broadband router, gives a client four essential settings: an IP address, the subnet mask (or prefix length), the default gateway, which is the first-hop router for traffic leaving the subnet, and the addresses of DNS servers. The address comes on a lease, valid for a set time such as 8 hours. A server can be configured to give a particular device the same address every time, or to hand out any free address from a pool. DHCP runs over UDP, with servers listening on port 67 and clients on port 68.
The four-message exchange
A new client has no address and does not know where the server is, so DHCP starts with broadcasts. 1. Discover: the client sends a UDP message from 0.0.0.0, port 68, to the limited broadcast 255.255.255.255, port 67, carrying a random transaction ID. 2. Offer: each server that hears it replies with a proposed address, mask, gateway, DNS servers and lease time. 3. Request: the client picks one offer and broadcasts a request naming it, so other servers know theirs was declined. 4. Acknowledge (ACK): the chosen server confirms. The four steps are often remembered as DORA.
Leases, renewal and relays
A lease does not end abruptly. By default a client tries to renew with its own server when half the lease has passed, a unicast request that usually just extends the lease. If that server is unreachable, at seven-eighths of the lease the client broadcasts to any server. Only if the lease expires must it stop using the address. Broadcasts do not cross routers, so a large organisation that keeps one central DHCP server places a DHCP relay agent, usually a function of each subnet's router, to forward clients' messages to the server and pass the replies back.
Worked example: a laptop joins Wi-Fi
A laptop joins a library network whose router at 192.168.20.1 runs DHCP with 12-hour leases. 1. The laptop broadcasts Discover from 0.0.0.0:68 to 255.255.255.255:67. 2. The router offers 192.168.20.57, prefix /24, gateway 192.168.20.1 and DNS server 192.168.20.1, for 12 hours. 3. The laptop broadcasts a Request for 192.168.20.57. 4. The router sends ACK, and the laptop configures its interface. 5. After 6 hours the laptop unicasts a renewal; if the router does not answer, it broadcasts again after 10.5 hours (seven-eighths of 12). Check: until step 4 the laptop has no usable address, which is why steps 1 and 3 must be broadcasts.
Private address ranges
Three IPv4 blocks are reserved for private networks: 10.0.0.0/8 (about 16.8 million addresses), 172.16.0.0/12 (172.16.0.0 to 172.31.255.255, about 1 million) and 192.168.0.0/16 (65,536). Any home or company may use them internally, so millions of networks reuse the same numbers, and routers on the public internet never forward datagrams addressed to them. That is why addresses such as 192.168.1.10 are so common: they only have meaning inside one local network, sometimes called a realm. To reach the internet, traffic from a private realm needs translating.
How NAT works
A NAT router sits between a private realm and the internet and owns one public address. When a host inside sends a datagram out, the router replaces the private source address and port with its public address and a new source port, and records the pair in a NAT translation table. When a reply arrives at the public address and that port, the router looks up the entry, rewrites the destination back to the private address and port, and forwards it inside. It must also recompute the IP and TCP or UDP checksums. The 16-bit port field allows over 60,000 simultaneous mappings per public address and protocol.
Worked example: two laptops, one address
A home NAT router has public address 203.0.113.9. 1. Laptop A, 192.168.1.20, opens a connection from port 51000 to a web server at 198.51.100.80, port 443. 2. The router rewrites the source to 203.0.113.9:6001 and records 6001 to 192.168.1.20:51000. 3. Laptop B, 192.168.1.21, also uses source port 51000 for its own connection; the router maps it to 203.0.113.9:6002. 4. The server replies to 203.0.113.9:6002; the table says 192.168.1.21:51000, so the router forwards the reply to laptop B. Check: the server sees two connections from one address, distinguished only by port.
The case against NAT, and carrier-grade NAT
NAT has critics. It breaks the end-to-end principle: a datagram's addresses are no longer those of the true endpoints, and a network-layer device rewrites transport-layer port numbers. Worst, an outside host cannot start a connection to an inside one, since no table entry exists until the inside host sends first. Servers behind NAT need manual port forwarding, and peer-to-peer and calling apps use traversal techniques such as STUN. As IPv4 ran out, many ISPs, especially mobile operators, added carrier-grade NAT, sharing one public address among many customers, often using the shared block 100.64.0.0/10.
Common mistakes to avoid
First, writing the DHCP steps out of order; it is discover, offer, request, acknowledge. Second, thinking a DHCP lease ends without warning; the client renews at half the lease. Third, treating 172.32.x.x as private; the private block ends at 172.31.255.255. Fourth, believing NAT is a firewall by design; blocking unsolicited inbound traffic is a side effect, not a security policy. Fifth, assuming NAT changes only addresses; it rewrites ports and fixes checksums too. Sixth, expecting DHCP broadcasts to cross routers without a relay agent.
Key takeaways
DHCP gives a joining host an address, mask, default gateway and DNS servers on a lease, over UDP ports 67 and 68, in four steps: discover, offer, request, acknowledge. Clients renew at half the lease and rebroadcast at seven-eighths; relay agents carry DHCP across routers. Private blocks 10/8, 172.16/12 and 192.168/16 are reused freely and never routed publicly. NAT shares one public address by rewriting source addresses and ports and keeping a translation table, at the cost of end-to-end reachability. Carrier-grade NAT does the same at ISP scale.
Flashcards
Flashcard
Dynamic Host Configuration Protocol: automatic address assignment
Test your understanding
Question 1 of 5
In what order are the four DHCP messages exchanged when a new client joins a network?
Question 2 of 5
Which of these addresses lies in one of the three private IPv4 blocks?
Question 3 of 5
A NAT table maps 203.0.113.9:6002 to 192.168.1.21:51000. A reply arrives for 203.0.113.9 port 6002. What does the router do?
Question 4 of 5
A client receives a 12-hour DHCP lease. When does it first try to renew with its server?
Question 5 of 5
Why can an outside computer usually not start a connection to a laptop behind a home NAT router?
