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.
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.
- 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.
- 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.
- 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.
- 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 protocol also defines other messages, which are especially useful for troubleshooting.
| Message | Sent by | Purpose |
|---|---|---|
| DHCPDISCOVER | Client | Looks for available DHCP servers (broadcast) |
| DHCPOFFER | Server | Offers an address and the network parameters |
| DHCPREQUEST | Client | Accepts an offer, renews or confirms an address already in use |
| DHCPACK | Server | Confirms the assignment and the lease time |
| DHCPNAK | Server | Refuses the request: the address is not valid on that network |
| DHCPDECLINE | Client | Reports that the address received is already used by another device |
| DHCPRELEASE | Client | Gives the address back before it expires |
| DHCPINFORM | Client | Asks 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.
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.
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
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 2: IP Addressing and Network Services (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 3: Switching, VLANs, and Business Networks (English edition)
- Book: OPNsense® in Practice — Volume 1: The Foundations of a Professional Journey (English edition)
- Video course: Reti LAN da Zero: Switch, IP, Router, Wi-Fi e Internet (video course in Italian)
- Video course: VLAN con OPNsense®: progettazione e configurazione (video course in Italian)
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Related article: How a Firewall Rule Works
- Specialization: LAN Networks
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.