LAN NetworksNetwork design

DHCP: How a Device Gets Its IP Address

The DORA exchange, leases and renewal, pools and reservations, DHCP relay between VLANs, APIPA and DHCP snooping, with a practical example on OPNsense.

  • Category: LAN Networks · Network design · Diagnostics
  • Published:
  • Editorial team: Alba Technology Academy
  • Reading time: about 9 minutes

We plug a computer into the network or connect a smartphone to Wi-Fi and, a few moments later, we are online, without having typed an IP address, a subnet mask or the router's address: a DHCP server provided them. This article explains how client and server exchange messages, what leases and renewals mean, how pools and reservations are organised, how a DHCP relay serves more than one VLAN and how to deal with conflicts and rogue servers, with a practical example on OPNsense.

What DHCP is for

To communicate on an IPv4 network a device needs at least four pieces of information: its own IP address; the subnet mask, which tells it which addresses belong to the local network; the default gateway, the router that receives all traffic leaving the network; and at least one DNS server to translate names into addresses. Configuring them by hand on every device would be slow and error-prone.

DHCP, defined in RFC 2131, automates this work. A server keeps the list of available addresses and assigns them to the clients that ask, together with the other parameters, which the protocol calls options and which are listed in RFC 2132: subnet mask (option 1), router (3), DNS servers (6), domain name (15), lease time (51) and many more. In a home network the DHCP server is almost always the router; in a business it may be the firewall, a layer 3 switch or a dedicated server.

The DORA exchange: Discover, Offer, Request, Ack

A newly connected client has no address and does not know where the server is: that is why its first message is a broadcast, sent to every device on the local network. DHCP runs over UDP: the server listens on port 67, the client on port 68. The complete exchange is named after the initials of its four messages, DORA.

  1. DHCPDISCOVER: the client asks "is there a DHCP server?". As it has no address yet, it uses 0.0.0.0 as the source and the limited broadcast address 255.255.255.255 as the destination; at the Ethernet level the destination is the broadcast MAC address ff:ff:ff:ff:ff:ff. The switch forwards the message to every port in the same VLAN.
  2. DHCPOFFER: every server that receives the request may reply by offering a free address, together with the mask, gateway, DNS servers and lease time. Depending on what the client asked for, the offer travels as a broadcast or directly to the client's MAC address.
  3. DHCPREQUEST: the client chooses one offer and formally requests it, again as a broadcast. The message carries the identifier of the chosen server (option 54), so any other servers understand that their offer was declined and release the address.
  4. DHCPACK: the server confirms the assignment. From this moment the client configures the address and uses it for the whole lease. If the address were no longer valid in the meantime, the server would answer with a DHCPNAK and the client would start again.
The DORA exchange between a client and a DHCP server Timing diagram with two vertical lines, the client on the left and the DHCP server 192.168.10.1 on the right. The client sends DHCPDISCOVER as a broadcast from 0.0.0.0 port 68 to 255.255.255.255 port 67. The server replies with DHCPOFFER, offering the address 192.168.10.100. The client sends DHCPREQUEST as a broadcast, naming the chosen server. The server confirms with DHCPACK, which carries the address, mask, gateway, DNS servers and lease time. Finally the client uses 192.168.10.100 with a /24 mask. Client DHCP server 192.168.10.1 1 · DHCPDISCOVER (broadcast)0.0.0.0:68 → 255.255.255.255:67 2 · DHCPOFFER: offers 192.168.10.100192.168.10.1:67 → client:68 3 · DHCPREQUEST (broadcast): I accept the offer0.0.0.0:68 → 255.255.255.255:67 · chosen server: 192.168.10.1 4 · DHCPACK: assignment confirmedaddress, mask, gateway, DNS, lease time 192.168.10.100/24
Figure 1 — The DORA exchange. Example addresses; the offer and the acknowledgement may travel as broadcast or unicast, depending on what the client asked for.

The protocol also defines other messages, which are especially useful for troubleshooting.

MessageSent byPurpose
DHCPDISCOVERClientLooks for available DHCP servers (broadcast)
DHCPOFFERServerOffers an address and the network parameters
DHCPREQUESTClientAccepts an offer, renews or confirms an address already in use
DHCPACKServerConfirms the assignment and the lease time
DHCPNAKServerRefuses the request: the address is not valid on that network
DHCPDECLINEClientReports that the address received is already used by another device
DHCPRELEASEClientGives the address back before it expires
DHCPINFORMClientAsks only for the parameters, as it already has a manually configured address

The lease: an address on loan

The server does not give the address away: it lends it, as a lease, for a set time. This way the addresses of devices that leave the network, such as a guest's smartphone, become available again without any manual work. Typical lease times range from a few hours, for guest networks, to a few days.

The client does not wait for the lease to expire before renewing it. RFC 2131 defines two timers, which the server may send with options 58 and 59; if it does not, the default values are:

  • T1 = 50 % of the lease time: the client enters the RENEWING state and sends a DHCPREQUEST as unicast directly to the server that assigned the address. If the server answers with a DHCPACK, the lease starts again.
  • T2 = 87.5 % of the lease time: if the server still has not answered, the client moves to the REBINDING state and sends the request as a broadcast, to any DHCP server able to confirm the address.
  • Expiry: if nobody answers, the client must stop using the address and start again from DHCPDISCOVER.
Lease, renewal and rebinding over time Timeline of a 24-hour lease. From 0 to 12 hours the client uses the address. At 12 hours, that is 50 per cent, timer T1 starts the renewal as unicast to the server that assigned the address. At 21 hours, that is 87.5 per cent, timer T2 starts rebinding as a broadcast to any server. At 24 hours the lease expires and the client starts again from DHCPDISCOVER. 24-hour lease (example): T1 and T2 with their default values address in useRENEWING (unicast)REBINDING 0 h12 h21 h24 h T1 = 50 %T2 = 87.5 % expiry: back to DHCPDISCOVER
Figure 2 — Simplified view of the renewal. During rebinding the broadcast request can be confirmed by any DHCP server on the network.

Scopes, pools, exclusions and reservations

On each network the server manages a scope: the subnet it serves, for example 192.168.10.0/24, with its parameters. Inside the scope you define the pool, the range of addresses handed out dynamically, for example from 192.168.10.100 to 192.168.10.199. The addresses outside the pool remain free for devices configured by hand: the gateway, managed switches, access points and servers.

Some servers also let you declare exclusions: addresses or ranges that fall within the scope but must never be assigned. This is a basic rule: an address set by hand on a device must never be part of the dynamic pool, otherwise sooner or later the server will hand it to another device as well.

For devices that must always have the same address, such as a network printer or a NAS, the tidiest solution is a reservation (also called a static mapping): the server binds the device's MAC address, or its DHCP client identifier, to a fixed address, for example 00:00:5E:00:53:2A → 192.168.10.50. The device stays configured for DHCP but always receives the same address, with gateway and DNS settings always up to date. The reserved address is usually chosen outside the dynamic pool.

Several VLANs: the DHCP relay

A broadcast does not cross a router: it stays within its broadcast domain, that is, its own network or VLAN. In a network segmented into several VLANs, a client in the guest VLAN therefore cannot reach, with its DHCPDISCOVER, a server located on another network. You need either a server with an interface in every VLAN, such as a firewall that routes between the VLANs, or a DHCP relay agent on the device that does the routing.

The relay, often configured on a layer 3 switch with the ip helper-address command or an equivalent, listens for DHCP broadcasts in the VLAN, writes its own address on that network into the giaddr field and forwards the request to the server as unicast. The server reads giaddr, chooses the pool of the matching network and replies to the relay, which delivers the answer to the client. With option 82 (RFC 3046) the relay can add information about the switch and the port the request came from.

DHCP relay between two VLANs A client in VLAN 60, network 192.168.60.0/24, sends DHCPDISCOVER as a broadcast. The layer 3 switch, with the address 192.168.60.1 in VLAN 60 and 192.168.10.2 in VLAN 10, acts as a relay: it forwards the request as unicast to the DHCP server 192.168.10.1 in VLAN 10, with giaddr set to 192.168.60.1. The server chooses the pool of network 192.168.60.0/24 and replies to the relay, which delivers the offer to the client. VLAN 60 · 192.168.60.0/24 Client0.0.0.0 L3 switch · relayVLAN 60: 192.168.60.1VLAN 10: 192.168.10.2 VLAN 10 · 192.168.10.0/24 DHCP server192.168.10.1 ① broadcast ② unicast ③ offer ④ offer giaddr = 192.168.60.1: the server picks the 192.168.60.0/24 pool
Figure 3 — The DHCP relay turns the client's broadcast into a unicast request to the central server. Example addresses.

When something goes wrong: conflicts and APIPA

An address conflict occurs when two devices use the same IP. The most common cause is an address set by hand inside the pool. To reduce the risk, many clients use an ARP request to check that the address they received is really free and, if it is taken, decline it with a DHCPDECLINE; some servers also check that nobody answers before offering an address.

If no server answers at all, many operating systems assign themselves a link-local address in the 169.254.0.0/16 network, as described in RFC 3927; on Windows this mechanism is known as APIPA. The device can only talk to other devices on the same local network that have an address of the same kind, and it has neither gateway nor DNS: no Internet. Meanwhile it keeps looking for a DHCP server. An address starting with 169.254 is therefore a precise symptom: the client got no answer. Typical causes are the link itself, the VLAN of the port, a stopped DHCP service, an exhausted pool or a missing relay. On Windows the ipconfig /all command shows the DHCP server, the address obtained and when the lease expires.

Rogue DHCP servers and DHCP snooping

The client accepts the first valid offer it receives, without checking who sent it. A rogue DHCP server can therefore hand out wrong addresses and bring the network to a halt. The most frequent case is accidental: a home router plugged into an office socket through its LAN port. The worst case is an attack: a server that sets a computer controlled by the attacker as the gateway or DNS server.

Managed switches offer a specific defence, DHCP snooping. Ports are divided into trusted ports, towards the authorised DHCP server or the relay, and untrusted ports, towards user devices. On an untrusted port the switch drops the messages that only a server may send, such as DHCPOFFER and DHCPACK. The switch also builds a binding table of MAC address, IP address, VLAN and port, which other protection features, such as dynamic ARP inspection, use to block spoofing.

An example with OPNsense

On an OPNsense firewall the DHCP server is configured per interface. In the versions based on ISC DHCP the path is Services › DHCPv4 › [interface], for example LAN: you enable the service, enter the start and end of the pool in the Range field and, if needed, specify DNS servers, gateway and lease time. If gateway and DNS are left empty, the firewall usually hands out its own address on that interface. On the same page, in the DHCP Static Mappings section, you add reservations by entering a MAC address and an IP address. With one VLAN per network, you repeat the configuration on each interface, with a different pool for each.

ISC, the organisation that developed ISC DHCP, has ended its maintenance, and recent OPNsense versions offer other services: Kea DHCP (Services › Kea DHCP) and Dnsmasq (Services › Dnsmasq DNS & DHCP). The concepts stay the same, namely scopes, pools, options and reservations, but menu names, the default service and the available options depend on the installed version: before configuring anything, check the official documentation for your version.

Conclusions

DHCP is easy to enable, but worth understanding. The DORA exchange, the renewal timers and the relay explain how to design tidy networks, with pools kept apart from static addresses and reservations for fixed devices. A 169.254 address or an unexpected gateway become readable symptoms, and DHCP snooping shuts the door on unauthorised servers.

The video

DHCP: How a Device Gets Its IP Address — Animation without voice-over, with on-screen text in English and sound effects. Also on YouTube

Technical sources

  • RFC 2131 — Dynamic Host Configuration Protocol — The DHCP protocol: DISCOVER, OFFER, REQUEST, ACK, NAK, DECLINE, RELEASE and INFORM messages, UDP ports 67 and 68, leases, T1 (50 %) and T2 (87.5 %) timers, RENEWING and REBINDING states, the giaddr field for relays.
  • RFC 2132 — DHCP Options and BOOTP Vendor Extensions — Options mentioned: subnet mask (1), router (3), DNS servers (6), domain name (15), lease time (51), message type (53), server identifier (54), T1 (58) and T2 (59).
  • RFC 3046 — DHCP Relay Agent Information Option — Option 82, with which the relay adds information about the switch and port the request came from.
  • RFC 3927 — Dynamic Configuration of IPv4 Link-Local Addresses — IPv4 link-local addresses in the 169.254.0.0/16 network, used when no configuration is obtained (APIPA on Windows).
  • RFC 1918 e RFC 7042 — Private addresses used in the examples (192.168.10.0/24, 192.168.60.0/24) and the MAC address block reserved for documentation (00-00-5E-00-53-00 … 00-00-5E-00-53-FF).
  • Documentazione ufficiale di OPNsense — servizi DHCP — Per-interface DHCPv4 configuration (ISC DHCP), static mappings and the alternative Kea DHCP and Dnsmasq services; menus and the default service depend on the version.

Standards and official documentation available from the respective organisations.

OPNsense® is a registered trademark. Alba Technology does not represent the OPNsense project.