What Is DHCP?
Dynamic Host Configuration Protocol (DHCP) is a network management protocol for dynamic and centralized IP address allocation and management.
DHCP was first defined in October 1993 based on BOOTP. As defined in RFC 2131, DHCP operates based on the client–server model. A DHCP client applies to a DHCP server for network parameters, such as the IP address, subnet mask, default gateway address, and DNS server address. The DHCP server then replies with the configuration parameters to the client so the client can communicate with other IP networks.
Why Do We Need DHCP?
On an IP network, each device connected to the Internet must be assigned a unique IP address. DHCP helps network administrators to monitor and assign IP addresses in a centralized manner. It can automatically assign a new IP address to a computer when it is moved to another location. DHCP automates the process of allocating IP addresses, which reduces the time required for device configuration and deployment, as well as the possibility of configuration errors. In addition, a DHCP server can manage the configurations of multiple network segments. When the configuration of a network segment changes, an administrator only needs to update the corresponding configuration on the DHCP server.
DHCP offers the following advantages:
- Reliable IP address configuration: The IP address configuration parameters must be exact. It is easy to make a mistake when dealing with inputs such as 192.168.XXX.XXX. Typographical errors are typically very difficult to troubleshoot and the use of a DHCP server minimizes such errors.
- Reduced IP address conflicts: Each connected device must have an IP address. However, each IP address can be used only once. If an IP address conflict occurs between two devices, one or both of the devices cannot be connected. This can happen when IP addresses are allocated manually, particularly when there are a large number of endpoints that only connect periodically, such as mobile devices. The use of DHCP ensures that each IP address is used only once.
- Automatic IP address management: In any network without DHCP, network administrators need to manually assign and withdraw IP addresses. Keeping track of which device has what IP address can be an exercise in futility as it is nearly impossible to understand when devices require access to the network and when they leave. DHCP allows this to be automated and centralized so network professionals can manage all locations from a single location.
- Efficient change management: The use of DHCP makes it very simple to change addresses, scopes, or endpoints. For example, an organization may want to change its IP addressing scheme from one range to another. The DHCP server is configured with the new information and the information will be propagated to the new endpoints. Similarly, if a network device is upgraded and replaced, no network configuration is required.
How Does DHCP Work?
DHCP uses User Datagram Protocol (UDP), as its transport protocol. A DHCP client sends messages to a DHCP server through UDP port 68, and a DHCP server sends messages to a DHCP client through UDP port 67.
Only the DHCP server on the same network segment as a DHCP client can receive the DHCPDISCOVER message broadcast by the client. If a DHCP client and server are on different network segments, a DHCP relay agent must be deployed to forward DHCP messages between them. For DHCP clients, the DHCP relay agent is the DHCP server; for the DHCP server, the DHCP relay agent is a DHCP client.
DHCP Client Accessing the Network for the First Time Without a DHCP Relay Agent
As shown in the following figure, when no DHCP relay agent is deployed, the newly connected DHCP client and server exchange DHCP messages through four stages.
Message exchange between the newly connected DHCP client and server
Stage 1: Discovery stage
The newly connected DHCP client does not know the IP address of the DHCP server. To learn the IP address of the DHCP server, the DHCP client broadcasts a DHCPDISCOVER message with the destination IP address of 255.255.255.255 to all devices on the same network segment. Information carried in a DHCPDISCOVER message includes the client's MAC address (chaddr field), parameter request list (Option 55), and broadcast flag (flags field).
Stage 2: Offer stage
All the DHCP servers on the same network segment as the DHCP client receive the DHCPDISCOVER message. Each DHCP server selects an idle IP address from an address pool on the same network segment as the IP address of the interface receiving the DHCPDISCOVER message, and then responds with a DHCPOFFER message that includes this available IP address.
In most cases, the leases of IP addresses are specified in an address pool. If the DHCPDISCOVER message carries an expected lease, the DHCP server compares the expected lease with the specified lease and allocates the IP address with a smaller lease to the DHCP client.
- IP address statically bound to the client's MAC address on the DHCP server.
- IP address specified by the Option 50 field (requested IP address) in the DHCPDISCOVER message sent by the client.
- IP address in Expired state in the address pool, that is, the IP address that has been assigned to the client and whose lease has expired.
- Random IP address in Idle state in the address pool.
- If no IP address is available for allocation, the DHCP server automatically reclaims the expired and conflicting IP addresses in sequence. If an available IP address is found after the reclaim, the DHCP server allocates the IP address. If no IP address is available, the DHCP client resends a DHCPDISCOVER message to apply for an IP address after the response time interval expires.
To prevent the allocated IP address from conflicting with the IP addresses of other clients on the network, before sending a DHCPOFFER message, the DHCP server sends an ICMP Echo request message with the source address being the IP address of the DHCP server and the destination address being the to-be-allocated IP address to detect whether the IP address to be offered is already in use on the network. If the DHCP server receives no ICMP Echo reply message within the detection period, no client is using this IP address, and the DHCP server can allocate the IP address. If the DHCP server receives an ICMP Echo reply message within the detection period, this IP address is being used by another client, and the DHCP server marks this IP address as a conflicting one. The DHCP server then waits for the next DHCPDISCOVER message to start the IP address selection process again.
The IP address allocated in this 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 DHCPOFFER message is sent. The IP address for the client can be determined only after the request and acknowledgment stages.
Stage 3: Request stage
If multiple DHCP servers reply with a DHCPOFFER message to the DHCP client, the client accepts only the first received DHCPOFFER message. The client then broadcasts a DHCPREQUEST message that includes the server identifier (Option 54) to indicate the server it has selected and the requested IP address (Option 50, with the IP address specified in the yiaddr field of the accepted DHCPOFFER message).
After receiving the DHCPREQUEST message broadcast from the client, those servers not selected by the client know that the client has declined their offers, and can then allocate the offered IP addresses to other clients.
Stage 4: Acknowledgement stage
After receiving the DHCPREQUEST message, the DHCP server selected by the client sends a DHCPACK message to the client, carrying the IP address specified in Option 50 of the DHCPREQUEST message.
After receiving the DHCPACK message, the DHCP client broadcasts gratuitous ARP packets to check whether any other client is using the same IP address allocated by the DHCP server. If no response is received within the specified time, the DHCP client uses the IP address. If the DHCP client receives a response within the specified time, this IP address is being used by another client. The client then sends a DHCPDECLINE message to the DHCP server and applies for a new IP address. The DHCP server marks this IP address as a conflicting one. The DHCP server allocates conflicting IP addresses only when there is no idle IP address in the address pool, minimizing IP address conflicts.
Occasionally, the DHCP server may fail to allocate the IP address specified in Option 50 because, for example, an error occurs during negotiation or it takes a long time for the DHCP server to receive the DHCPREQUEST message. In this case, the DHCP server replies with a DHCPNAK message to notify the DHCP client that the requested IP address cannot be allocated. In this case, the DHCP client has to send another DHCPDISCOVER message to request another new IP address.
DHCP Client Accessing the Network for the First Time When a DHCP Relay Agent Exists
The working principles for a DHCP client to access the network for the first time when a DHCP relay agent exists are the same as those when no DHCP relay agent exists. The difference is that the DHCP relay agent forwards DHCP messages between the DHCP server and client. The following describes the working principles of the DHCP relay agent.
The following figure shows the message exchange between a DHCP server and a DHCP client that accesses the network for the first time when a DHCP relay agent is deployed.
Message exchange between a DHCP server and a DHCP client that accesses the network for the first time when a DHCP relay agent is deployed
Stage 1: Discovery stage
- Checks whether the value of the hops field in the message is greater than 16. If so, the DHCP relay agent discards the message. If not, the DHCP relay agent increases the value by 1 and proceeds to the following operations.
- Checks whether the value of the giaddr field in the message is 0. If so, the DHCP relay agent sets the giaddr field to the IP address of the interface that receives the DHCPDISCOVER message. If not, the DHCP relay agent does not change the field and proceeds to the following operations.
- Changes the destination IP address of the DHCPDISCOVER message to the IP address of the DHCP server or next-hop relay agent, changes the source IP address of the DHCPDISCOVER message to the IP address of the interface connecting the DHCP relay agent to the client, and unicasts the message to the DHCP server or next-hop relay agent.
If multiple DHCP relay agents exist between the DHCP client and server, each DHCP relay agent processes the DHCPDISCOVER message using the same method.
Stage 2: Offer stage
After receiving the DHCPDISCOVER message, the DHCP server selects an address pool on the same network segment as the address specified by the giaddr field in the message, allocates parameters such as an IP address to the client, and unicasts a DHCPOFFER message to the DHCP relay agent identified by the giaddr field.
- Checks whether the value of the giaddr field in the message is the IP address of the interface that receives the message. If so, the DHCP relay agent proceeds to the following operations. If not, the DHCP relay agent discards the message.
- Checks whether the value of the flags field is 1. If so, the DHCP relay agent broadcasts the DHCPOFFER message to the DHCP client. If not, the DHCP relay agent unicasts the DHCPOFFER message to the DHCP client.
Stage 3: Request stage
The message exchange between the DHCP relay agent and client in this stage is the same as that between the DHCP server and client when no DHCP relay agent exists.
Stage 4: Acknowledgement stage
The DHCP relay agent receives the DHCPACK message from the DHCP server. After that, the message exchange between the DHCP relay agent and client is the same as that between the DHCP server and client when no DHCP relay agent exists.
DHCP Client Reusing an IP Address
A non-newly connected DHCP client can reuse an IP address that has been allocated to it. As shown in the following figure, the DHCP client exchanges DHCP messages with the DHCP server to re-obtain network parameters such as the previously used IP address. This process is performed through two stages.
Message exchange for IP address reuse between a DHCP client and server
Stage 1: Request stage
The DHCP client broadcasts a DHCPREQUEST message that includes the IP address that the client has used. The requested IP address is added in Option 50.
Stage 2: Acknowledgement stage
After receiving the DHCPREQUEST message, the DHCP server checks whether there is a lease record matching the client MAC address. If there is such a lease record, the DHCP server replies with a DHCPACK message to notify the DHCP client that the requested IP address can be used. Otherwise, the DHCP server performs no operation and waits for a new DHCPDISCOVER message from the client.
DHCP Client Renewing Its IP Address Lease
A DHCP server defines a validity period for each IP address dynamically assigned to a DHCP client. The validity period is called a lease. A DHCPDISCOVER message from a DHCP client can carry an expected lease. When offering an IP address, the DHCP server compares the expected lease with the specified lease in the address pool and allocates the IP address with a shorter lease to the DHCP client. After the lease expires or the client goes offline, the server reclaims the IP address and can allocate this address to another client. This IP address reclaiming mechanism improves IP address utilization. If a DHCP client wishes to use the offered IP address continuously, it must request to extend the lease.
The following figure shows how a DHCP client extends its IP address lease when no DHCP relay agent exists.
DHCP client extending the address lease when no DHCP relay agent exists
- When the lease reaches 50% (T1) of its validity period, the DHCP client unicasts a DHCPREQUEST message to the DHCP server to request lease renewal. If the DHCP client receives a DHCPACK message, the IP address lease is successfully renewed (counted from 0). If the DHCP client receives a DHCPNAK message, the DHCP client must send a DHCPDISCOVER message to apply for a new IP address.
- If no response is received from the DHCP server when the lease reaches 87.5% (T2) of its validity period, the DHCP client broadcasts a DHCPREQUEST message to request lease renewal. If the DHCP client receives a DHCPACK message, the IP address lease is successfully renewed (counted from 0). If the DHCP client receives a DHCPNAK message, the DHCP client must send a DHCPDISCOVER message to apply for a new IP address.
- If no response is received when the lease expires, the DHCP client stops using the current IP address and sends a DHCPDISCOVER message to apply for a new one.
If a DHCP client no longer requires the use of its allocated IP address before the lease expires, the DHCP client sends a DHCPRELEASE message to the DHCP server to release the IP address. The DHCP server saves the configuration of this DHCP client and records the IP address in the allocated IP address list. The IP address can then be allocated to this DHCP client or other clients. A DHCP client can send a DHCPINFORM message to the DHCP server to request configuration update.
The following figure shows how a DHCP client renews its IP address lease through a DHCP relay agent. The process is similar to that when no DHCP relay agent exists.
Client renewing the address lease through a DHCP relay agent
DHCP Application Scenarios
DHCP supports dynamic and static IP address allocation. Network administrators can use either mechanism to allocate IP addresses to hosts based on their network requirements.
- Dynamic allocation: DHCP assigns an IP address to a client for a limited period of time, which is called a lease.
The lease can vary depending on how long a user is likely to need an Internet connection at a particular location. This mechanism applies to scenarios where hosts temporarily access the network and the number of idle IP addresses is less than the total number of hosts.
- Static allocation: DHCP allocates fixed IP addresses to clients as configured.
Compared with manual IP address configuration, DHCP static allocation prevents manual configuration errors and facilitates unified maintenance and management.
- Author: Tang Dandan
- Updated on: 2024-09-12
- Views: 32800
- Average rating: