Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 79 ยท The Network Layer: IP

DHCP: Getting an Address

See how DHCP gives hosts an IP address automatically: the Discover, Offer, Request, Acknowledge exchange, leases and renewal, relay agents, options and rogue servers.

When you join a Wi-Fi network, your phone has no IP address, does not know the subnet mask, and cannot know where the router or the DNS server is. Typing all that in by hand for every device would be impractical. The Dynamic Host Configuration Protocol (DHCP) solves this: a server on the network hands out an address and the other settings automatically, in under a second.

DHCP is a good example of how a protocol can run before the host has an address at all. Looking at it shows how broadcasts, UDP and the data link layer fit together, and it explains several everyday failures, such as the mysterious 169.254.x.x address.

What DHCP provides

A DHCP server answers with more than an address. A typical reply contains:

  • an IP address for the client, and a lease time that says how long it may keep it,
  • the subnet mask (or prefix length), so the host knows which addresses are on the local link,
  • the default gateway (router), for everything outside the subnet,
  • one or more DNS servers and a search domain, for name resolution,
  • sometimes an NTP server, a boot file name for network booting, or vendor-specific data.

Without these, a host could not even send its first packet off the local network. The server draws addresses from a pool (for example 192.168.1.100 to 192.168.1.199), and it can also hold reservations, which always give the same address to a given MAC address. Reservations are how printers and servers get stable addresses without being configured by hand.

The DORA exchange

DHCP uses UDP: the server listens on port 67 and the client uses port 68. The client has no address yet, so the first messages are broadcasts: source 0.0.0.0, destination 255.255.255.255, and at the link layer the broadcast MAC address ff:ff:ff:ff:ff:ff. The four-message conversation is called DORA:

  1. Discover. The client broadcasts "is there a DHCP server out there?". It may include an address it would like to have.
  2. Offer. Each server that has a free address replies with an offer: the proposed address, mask, lease time and options. The offer is addressed to the client's MAC address.
  3. Request. The client picks one offer and broadcasts a request that names the chosen server. Broadcasting it tells the other servers that their offers were declined, so they can free the addresses they held.
  4. Acknowledge. The chosen server confirms, and the lease starts. The client can now configure its interface. (A server that cannot honor the request sends a NAK instead.)
sequenceDiagram
    participant C as Client (no address yet)
    participant S as DHCP server
    C->>S: DISCOVER (broadcast)
    S->>C: OFFER 192.168.1.101, 8 h
    C->>S: REQUEST 192.168.1.101 (broadcast)
    S->>C: ACK (lease starts)

The client matches the four messages through a random transaction ID (xid) it puts in each one.

Because the client has no IP address, the server cannot rely on ARP to find it. Replies go to the client's MAC address, or, if the client asked for it with the broadcast flag, to the broadcast address.

Real packets are in the old BOOTP format: a fixed 236-byte header (operation, transaction ID, client MAC address, "your address", server address and more), then a magic cookie and a list of options. Each option is a type byte, a length byte and the data. Option 53 is the message type (1 = Discover, 2 = Offer, 3 = Request, 5 = ACK), option 1 the subnet mask, 3 the router, 6 the DNS servers, 51 the lease time, 54 the server identifier.

Python Build and read DHCP options

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.

Leases, renewal and release

An address is not given away forever; it is leased. This lets the server reclaim addresses from devices that left the network. The client does not wait for the lease to run out:

  • At T1, half the lease time, it unicasts a Request to its server to renew. If the server agrees, the clock restarts.
  • At T2, 87.5% of the lease, if the server has not answered, it broadcasts a Request to any server (rebinding).
  • If the lease expires with no answer, the client must stop using the address and start again from Discover.

A client that leaves cleanly can send a Release so the address returns to the pool at once. Short leases (minutes) suit busy Wi-Fi with many visitors. Long leases (days) suit stable wired networks. Most home routers use a lease of 12 to 24 hours.

Python A DHCP server and client in one script

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.

Relay agents: one server for many subnets

Broadcasts do not cross routers, so a DHCP server on one subnet would only serve that subnet. A company with fifty VLANs does not want fifty servers. Instead the router on each subnet runs a DHCP relay agent (the "IP helper" on many routers). It listens for broadcasts and forwards them as unicast packets to the central server, adding the router's own address on that subnet in the giaddr field ("gateway address"). The server looks at giaddr to decide which pool to use, and sends the reply back through the relay, which delivers it to the client.

flowchart LR
    C1["Client on 10.1.1.0/24"] -->|"broadcast"| R["Router with relay<br/>giaddr = 10.1.1.1"]
    C2["Client on 10.1.2.0/24"] -->|"broadcast"| R2["Router with relay<br/>giaddr = 10.1.2.1"]
    R -->|"unicast"| S["Central DHCP server"]
    R2 -->|"unicast"| S

The giaddr field tells the server which subnet's pool to pick an address from.

Failure cases and security

APIPA. If a client finds no DHCP server, many systems give themselves a link-local address from 169.254.0.0/16 (called APIPA on Windows, "IPv4 link-local" in the standards). Such an address works only between hosts on the same link and is never routed. So seeing 169.254.x.x on your computer means "DHCP did not answer": check the cable, the Wi-Fi connection, or whether the server's pool is empty.

Conflicts. Before using an address, a careful client sends an ARP probe to check that no one already has it, and declines the offer if someone does. A static address inside the DHCP pool is a classic cause of conflicts, which is why static ranges should be kept outside the pool.

Rogue DHCP servers. DHCP has no authentication. Anyone who plugs in a device that answers Discover faster than the real server (a home router connected the wrong way round is the usual accident) can give clients a wrong gateway or DNS server. An attacker can use the same trick to route victims' traffic through their machine. Defenses:

  • DHCP snooping on switches: only ports marked "trusted" (toward the real server) may send Offers and ACKs, and the switch builds a table of address bindings.
  • Starvation protection: an attacker can send thousands of Discover messages with fake MAC addresses to use up the pool; switches limit the number of MAC addresses per port.
  • Monitoring, to notice a second server answering.

IPv6 can also use DHCPv6, but many hosts there need no server because of SLAAC.

To see your own lease on Windows run ipconfig /all, on Linux ip addr and nmcli device show, on macOS ipconfig getpacket en0. The output lists the lease start, the lease end and the DHCP server's address. These commands are read-only; do them on your own machine.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which is the correct order of the DHCP exchange?

    DORA: the client discovers servers, a server offers an address, the client requests the one it picked, and the server acknowledges.
  2. 2. Why does the client send its first DHCP message as a broadcast?

    The client sends from 0.0.0.0 to 255.255.255.255 because it has no configuration at all. Later messages, such as a renewal, can be unicast.
  3. 3. Your computer shows the address 169.254.37.8. What most likely happened?

    169.254.0.0/16 is the link-local range used when automatic configuration fails. It only works on the local link.
  4. 4. How can one DHCP server serve many subnets?

    The relay agent converts the local broadcast into a unicast to the server. The giaddr field tells the server which pool to use.
  5. 5. What does DHCP snooping on a switch prevent?

    Only trusted ports may send server messages such as Offer and ACK. Offers arriving on a client port are dropped.

Summary

  • DHCP gives hosts an address, mask, gateway, DNS servers and other options automatically, using UDP ports 67 (server) and 68 (client).
  • The exchange is Discover, Offer, Request, Acknowledge (DORA); the first messages are broadcasts because the client has no address.
  • Addresses are leased: the client renews at 50% of the lease, rebinds at 87.5%, and starts over when the lease expires. Reservations give fixed addresses.
  • Relay agents forward broadcasts as unicast so one server can serve many subnets, using giaddr to choose the pool.
  • A 169.254.x.x address means DHCP failed. DHCP is unauthenticated, so rogue servers are a risk; switches defend with DHCP snooping.

Next, see how the network reports problems back to hosts: ICMP.