Chapters

DHCP in depth

Chapter 3 introduced DHCP: four messages, and the laptop gets its IP, mask, gateway and DNS server. This chapter looks at the real packets, what happens over a lease's lifetime, how one DHCP server can serve both of Kadé's office networks, and how DHCP can be attacked.

Application protocols · chapter 27 of 34 · 9 min read

A device that knows nothing

A brand-new device joining a network has no IP, no gateway and no DNS server. Configuring every phone, laptop and printer by hand would be slow and error-prone. DHCP (Dynamic Host Configuration Protocol) automates it. In one exchange it provides everything the earlier chapters needed:

SettingHome valueUsed in
IP address192.168.1.23Chapter 2
Subnet mask255.255.255.0Telling local from remote (chapter 4)
Default gateway192.168.1.1Reaching other networks (chapter 5)
DNS server192.168.1.1Resolving names (chapter 22)
Lease time24 hoursSection 4
DHCP is an application-layer protocol running over UDP (chapter 11's quiz): the server listens on port 67, the client on port 68.

DORA, packet by packet

Chapter 3 showed the four messages: Discover, Offer, Request, Ack. Here they are as real packets. Click each to see its addresses and contents:

frame (MAC)
a4:83:e7:2b:91:0c → ff:ff:ff:ff:ff:ff (broadcast)
IP
0.0.0.0 → 255.255.255.255
UDP ports
68 → 67
sent by
Laptop
DHCP fieldValue
op1 (request)
xid0x3903f326 (random; ties the four messages together)
ciaddr / yiaddr0.0.0.0 / 0.0.0.0
chaddra4:83:e7:2b:91:0c (the laptop's MAC)
OptionValue
53 message typeDISCOVER
50 requested IP192.168.1.23 (what it had last time)
12 host namekade-laptop
55 parameter list1, 3, 6, 15, 51, 119 (what it wants to know)

The laptop has no address and does not know where the server is, so it shouts to everyone.

  • Why the Request is also a broadcast: a network can have more than one DHCP server, and several may send offers. Broadcasting the Request, with the chosen server's ID inside, tells the chosen one "yes" and all the others "no thanks" in one message.
  • Option numbers: everything beyond the basic addresses travels as numbered options: 1 mask, 3 router, 6 DNS, 51 lease time, 53 message type, 54 server ID. Options like 42 (NTP servers) and 119 (domain search list) exist too.
  • A final safety check: after the Ack, a careful client sends an ARP probe for its new address (chapter 9). If someone answers, the address is already in use; the client sends DHCPDECLINE and starts again.

More than four messages

#MessageFromWhen
1DISCOVER, OFFER, REQUEST, ACKbothGetting an address (DORA)
2NAKserver"You cannot have that address." Example: your laptop wakes up at the office still asking for its home address 192.168.1.23. The office server refuses, and the laptop starts a fresh Discover.
3DECLINEclientThe ARP check found the offered address already in use
4RELEASEclient"I am done with this address" (a clean shutdown or ipconfig /release)
5INFORMclientA device with a static IP asking only for the other settings (DNS, etc.)
A returning device often skips Discover and Offer: it simply broadcasts a Request for the address it had last time ("init-reboot"). If the lease is still valid, the server Acks at once. That is why reconnecting to known WiFi is fast.

Leases: renew, rebind, expire

A DHCP address is a lease: it is yours only for a set time, say 24 hours. Two timers decide what happens before it runs out:

#TimeStateWhat the client does
10BoundUses the address normally
2T1 = 50% (12 h)RenewingSends a Request straight to its own server (unicast). An Ack resets the lease to a fresh 24 hours.
3T2 = 87.5% (21 h)RebindingIts server has not answered, so it broadcasts the Request: any DHCP server may extend the lease.
4100% (24 h)ExpiredMust stop using the address, and starts over with Discover

If a device leaves and never renews, its address goes back to the pool for reuse. That is exactly why your laptop's IP can change (chapter 7). Try it: see what the office PC does when the DHCP server goes down:

Situation
✓✓✓0 h3 h6 h9 h12 h15 h18 h21 h24 h27 h30 h
  • bound
  • renewing
  • rebinding
  • no lease: 169.254.x.x
  1. 0 hDORA: lease 172.16.1.61 for 24 h
  2. 12 hRenew at T1 (unicast to its server) → Ack: lease reset to 24 h
  3. 24 hRenew at T1 (unicast to its server) → Ack: lease reset to 24 h

The office PC, with a 24 h lease: T1 at 12 h, T2 at 21 h. Retries happen at half the remaining time each (a common pattern). Its address keeps working through every failed renewal, until the lease actually expires.

Pools, reservations and static addresses

The server hands out addresses from a pool (also called a scope or range) for each network. Kadé's office staff network:

.0network.1router.2 - .49static + reserved.50 - .199 · dynamic poollaptops, phones, visitors.200 - .254spare.255broadcast
Chapter 6 gave the front desk PC .10 and the office printer .20. Those come from the fixed range below the pool, so the pool never hands them to someone else.
#KindWho decides the addressGood for
1DynamicThe server, any free address from the poolLaptops, phones, visitors
2ReservationThe server, always the same address for a given MAC (chapter 7)Printers, the packing PC, anything others need to find. Still managed centrally, and gets DNS and gateway changes automatically.
3StaticTyped into the device itself; no DHCPThe router itself, and the DHCP server
Remember chapter 7's private MACs: a phone with a random MAC per network breaks a reservation. Printers and PCs use their real MAC, so reservations work well for them.

One server, two networks: DHCP relay

DHCP relies on broadcasts, and broadcasts stop at routers (chapter 9). Kadé's office has two networks behind one router (chapter 6). Rather than run two DHCP servers, Kadé runs one on a small office server at 172.16.1.5 and lets the router relay:

warehouse · 172.16.2.0/24Stock scanner (new)no address yetOffice routereth2 172.16.2.1: DHCP relayeth1 172.16.1.1staff · 172.16.1.0/24DHCP server172.16.1.5 · pools for both1 · broadcast Discover2 · unicast to 172.16.1.5with giaddr = 172.16.2.13 · Offer from the warehouse pool4 · relayed to the scanner

The relay writes its own interface address into the giaddr field. The server reads it, sees 172.16.2.1, and picks an address from the warehouse pool (172.16.2.0/24), not the staff pool. On Cisco routers this setting is called ip helper-address.

Wide by design - scroll it sideways.

Attacks on DHCP

Like ARP, DHCP trusts whoever answers first. Anyone plugged into the office network can abuse that:

#AttackWhat happensDefence
1Rogue DHCP serverAn attacker's laptop (or a home router plugged in by mistake) answers Discovers faster than the real server and hands out itself as gateway and DNS. Every new device now sends its traffic, and its DNS lookups, through the attacker.DHCP snooping on managed switches: only the port facing the real server may send Offers and Acks
2StarvationThe attacker requests every address in the pool using fake MACs. Real devices get nothing and fall back to 169.254.Port security (limit MACs per switch port); DHCP snooping rate limits
A mistakenly plugged-in home router is the most common "rogue server" in small offices. Symptom: some staff suddenly get 192.168.0.x addresses and lose access to office printers.

DHCP in GCP, and IPv6

GCP VMsGCP runs DHCP for you: the metadata server (169.254.169.254) answers kade-api's DHCP requests and hands out its internal IP (10.10.1.10), the gateway and DNS settings. You never manage a pool. "Reserving" an internal IP in GCP is the same idea as a reservation.
IPv6Devices can build their own address from the network's prefix without any server (SLAAC), learning the router from router advertisements. DHCPv6 exists for networks that want central control. Covered in the IPv6 chapter later.

Time sync: NTP

DHCP can also tell a device where to get the time (option 42). That sounds minor, but a surprising amount of networking breaks when clocks are wrong:

#Depends on correct timeWhat breaks when the clock is off
1TLS certificates (chapter 25)"Not yet valid" or "expired" errors for perfectly good certificates
22-step codes (TOTP)Codes change every 30 s: a clock 1 minute off rejects correct codes
3Tokens (JWT exp, nbf)kade-api rejects fresh tokens, or accepts expired ones. Libraries allow a small "clock skew" margin for this.
4Logs across machineskade-api and kade-db logs seem to happen in the wrong order, making incidents impossible to follow
5Signed things with expiryDNSSEC signatures, signed URLs, cloud API request signatures fail

NTP (Network Time Protocol) keeps clocks within milliseconds of real time, over UDP port 123 (chapter 14). Servers form levels called strata: stratum 0 is a reference clock (atomic or GPS), stratum 1 servers are wired to one, stratum 2 servers ask stratum 1, and so on. A client asks several servers and trusts the best ones.

The clever part is correcting for network delay. Each exchange records four timestamps:

t1client sends the request (client's clock)
t2server receives it (server's clock)
t3server sends the reply (server's clock)
t4client receives the reply (client's clock)

delay = (t4 − t1) − (t3 − t2) · offset = ((t2 − t1) + (t3 − t4)) ÷ 2

Round-trip delay
(42 − 0) − (521 − 520) = 41 ms
Clock offset
((520 − 0) + (521 − 42)) ÷ 2 = 499.5 ms

The client’s clock is 499.5 ms behind the server’s. It will speed up slightly until it catches up. The formula assumes the trip out and back take equal time; any difference between them becomes error, which is why NTP prefers nearby servers with short, steady delays.

  • Gentle correction: NTP clients (chrony, systemd-timesyncd) normally slew the clock, running it slightly fast or slow until it is right, instead of jumping. Jumps back in time confuse programs that measure durations.
  • In GCP, every VM gets time from the metadata server (metadata.google.internal, 169.254.169.254). Google also "smears" leap seconds over a day, so clocks never show a strange extra second.
  • Security: plain NTP is unauthenticated, and old public NTP servers were abused for amplification attacks (chapter 14). NTS (Network Time Security) adds authentication.

Summary

#IdeaIn one lineNew terms
1DHCPGives IP, mask, gateway, DNS and a lease; UDP 67/68DHCP
2DORADiscover and Request are broadcasts from 0.0.0.0; options carry the settings; ARP check afterDHCP options, server ID
3Other messagesNAK, DECLINE, RELEASE, INFORM; returning clients skip to RequestDHCPNAK, DHCPDECLINE
4LeasesRenew at 50% (unicast), rebind at 87.5% (broadcast), expire at 100%lease, T1, T2
5PoolsDynamic from a pool; reservations by MAC; static outside the poolpool, scope, reservation
6RelayRouters forward broadcasts as unicast; giaddr picks the poolDHCP relay, giaddr
7AttacksRogue servers and starvation; DHCP snoopingDHCP snooping
8ElsewhereGCP runs DHCP for VMs; IPv6 uses SLAAC or DHCPv6SLAAC, DHCPv6
9TimeNTP over UDP 123; strata; 4 timestamps give offset and delay; GCP metadata serverNTP, stratum, clock skew, NTS

Try it yourself

The last DHCP Ack your laptop got

  • What to look for: yiaddr, server_identifier, lease_time, router, domain_name_server.
  • Runs on: macOS · Linux: cat /var/lib/dhcp/dhclient.leases or nmcli device show
  • From section: 2. DORA, packet by packet
ipconfig getpacket en0

DORA live

  • Steps: sudo tcpdump -n -v -i en0 'udp port 67 or udp port 68', then reconnect to WiFi
  • What to look for: 0.0.0.0.68 > 255.255.255.255.67, then the options decoded.
  • From section: 2. DORA, packet by packet
sudo tcpdump -n -v -i en0 'udp port 67 or udp port 68'

Lease times

ipconfig /all

RELEASE, then a fresh DORA

  • Steps: ipconfig /release then ipconfig /renew
  • What to look for: Usually the same address comes back.
  • Runs on: Windows · macOS: sudo ipconfig set en0 DHCP
  • From section: 4. Leases: renew, rebind, expire
ipconfig /releaseipconfig /renew

Every DHCP server that answers on your network

  • What to look for: Exactly one. Two means a rogue server.
  • From section: 7. Attacks on DHCP
sudo nmap --script broadcast-dhcp-discover

Whether the clock is synced, and how far off

  • Steps: timedatectl and chronyc tracking
  • What to look for: System clock synchronized: yes; chrony's System time offset, often well under 1 ms.
  • Runs on: Linux · macOS: sntp time.google.com · Windows: w32tm /query /status
  • From section: 9. Time sync: NTP
timedatectlchronyc tracking

Spotted a mistake, or something unclear? Suggest an edit on GitHub