When a DHCP client broadcasts DHCP Discover messages, only a DHCP server on the same network segment as the DHCP client can receive the messages. If the DHCP client is on a different network segment from the DHCP server, a DHCP relay agent must be deployed to forward DHCP messages between the DHCP client and server.
Depending on whether a DHCP relay agent is used, the way in which network parameters are allocated to a new DHCP client differs.
Figure 1 shows the message exchange process between a DHCP server and a new DHCP client when no DHCP relay agent is deployed. This process is called four-message exchange.
Stage 1: The Discovery Stage
When a DHCP client accesses a network for the first time, it does not know the IP address of the DHCP server. To learn this information, the client broadcasts a DHCP Discover message in which the destination IP address is 255.255.255.255 to all devices (including the DHCP server or relay agent) on the network segment. The DHCP Discover message includes the client's MAC address (chaddr field), parameter request list (Option 55), and broadcast flag (flags field).
Stage 2: The Offer Stage
The DHCP server on the same network segment as the DHCP client receives the DHCP Discover message, selects an available IP address from the address pool that is on the same network segment as the IP address of the interface that receives the DHCP Discover message, and then sends a DHCP Offer message carrying the selected IP address to the DHCP client.
In most cases, an address pool specifies the lease of the IP addresses it contains. If the DHCP Discover message contains an expected lease, the server compares the expected lease with the specified lease and allocates an IP address with the shorter of the two leases to the client.
The IP address allocation order cannot be modified.
If no available IP address is found, the address pool automatically reclaims the IP addresses in Expired and Conflict status. If an IP address is available after the reclaim, the server allocates this IP address. Otherwise, the DHCP client sends a DHCP Discover message again to request an IP address after the timeout interval for the client to wait for a response from the server expires.
You can specify certain IP addresses to exclude on the DHCP server. For example, if you have statically allocated 192.168.1.100/24 to your DNS server, you can exclude this IP address from the address pool on network segment 192.168.1.0/24 so that it is not allocated through DHCP. This helps prevent IP address conflicts.
To prevent a newly allocated IP address conflicting with existing IP addresses, the DHCP server sends an ICMP Echo Request packet before sending a DHCP Offer message. This ICMP packet contains the IP address to be allocated in both the source and destination IP address fields. The server can allocate the IP address if it receives no ICMP Echo Reply packet within the detection period (no client is using this IP address). If the server receives an ICMP Echo Reply packet within the detection period, the DHCP server lists this IP address as a conflicting IP address (as it is in use by another client), and then waits for the next DHCP Discover message to start the IP address selection process again.
The IP address allocated during the offer stage may not be the final IP address used by the client. This is because the IP address may be allocated to another client if the DHCP server receives no response 16 seconds after the DHCP Offer message is sent. The IP address for the client can be determined only after the request and acknowledgment stages.
If multiple DHCP servers reply with a DHCP Offer message to the client, the client accepts only the first DHCP Offer message it receives. The client then broadcasts a DHCP Request message carrying the selected DHCP server identifier (Option 54) and IP address (Option 50, with the IP address specified in the yiaddr field of the accepted DHCP Offer message).
The DHCP Request message notifies all the DHCP servers of the IP address that the DHCP client has selected. The unselected IP addresses offered by other DHCP servers are then free to be allocated to other clients.
When a switch functions as a DHCP client and multiple DHCP servers exist on the network, the DHCP client polls DHCP servers according to the sequence of receiving DHCP Offer messages. If the previous DHCP server fails to allocate IP addresses, the DHCP client selects the next DHCP server.
Stage 4: The Acknowledgment Stage
After receiving a DHCP Request message, the DHCP server sends to the client a DHCP ACK message that contains the IP address specified in the Option 50 field of the DHCP Request message.
To determine whether any other device is using this IP address, the DHCP client broadcasts gratuitous ARP packets after receiving the DHCP ACK message. The client can use this IP address if no response is received within the specified time. However, if the DHCP client receives a response within the specified time, this IP address is already in use. The client then sends a DHCP Decline message to the DHCP server and applies for a new IP address. The server lists this IP address as a conflicting IP address.
Occasionally, the DHCP server may not allocate the IP address specified in the Option 50 field because, for example, an error occurs during negotiation or it does not receive the DHCP Request message quickly enough. In this case, the server replies with a DHCP NAK message to notify the client that the requested IP address cannot be allocated. The client then sends a DHCP Discover message to apply for a new IP address.
The message exchange process between a DHCP server and a new DHCP client when a DHCP relay agent is deployed is similar to that described in Network Parameter Allocation without a DHCP Relay Agent. The main difference is that the DHCP relay agent acts as an intermediary to forward DHCP messages between a DHCP server and client that would otherwise be unable to communicate with each other. The following describes how the DHCP relay agent functions in the message exchange process.
Figure 2 shows the message exchange process between a DHCP server and a new DHCP client when a DHCP relay agent is deployed.
Stage 1: The Discovery Stage
If there are multiple DHCP relay agents between the DHCP client and server, each the DHCP relay agent processes the DHCP Discover message using the same method.
Stage 2: The Offer Stage
After receiving a DHCP Discover message, the DHCP server selects an address pool on the same network segment as that specified in the giaddr field and allocates an IP address and other network parameters from the address pool. The DHCP server then unicasts a DHCP Offer message to the DHCP relay agent specified in the giaddr field.
Stage 3: The Request Stage
The DHCP relay agent processes the DHCP Request message from the client in the same manner as that described in Stage 3: The Request Stage.
Stage 4: The Acknowledgment Stage
The DHCP relay agent processes the DHCP ACK message from the server in the same manner as that described in Stage 4: The Acknowledgment Stage.