Introduction
The Dynamic Host Configuration Protocol (DHCP) is a crucial network protocol used in local area networks. This protocol utilizes the UDP protocol and serves two primary purposes: it automatically assigns IP addresses to users within an internal network or through an Internet service provider, and it acts as a central management tool for internal network administrators. This article provides a comprehensive overview of DHCP explained.
More information
How DHCP works:
DHCP provides IP addresses from a pool of IP addresses that is defined in the DHCP server database, called a scope. If the client accepts the address, it can use it for a predefined period of time, called a lease. If the client cannot obtain an IP address from the DHCP server, it cannot initialize TCP/IP properly.
When DHCP configures TCP/IP parameters for a client, both the DHCP server and the client go through a four-step process. Note that much of the communication is done via broadcasts. Broadcast communication can cause problems if the router is unable to forward these DHCP messages.
The IP lease process must be used when the client is in one of the following four states:
- Configure the client using DHCP to initialize TCP/IP for the first time;
- Occurs when DHCP drops the lease when a client requests a specific IP address but the server refuses it.
- The client previously leased an IP address, but later released it and is now applying for a new lease. This happens when the user enters the ipconfig /release and ipconfig /renew commands.
Client requests an IP address (DHCPDISCOVER):
When an IPv4 client detects the need for an IP address when it starts, it will initialize a limited version of TCP/IP and then broadcast a message requesting the address of a DHCP server. The broadcast message informs the listening server that the client needs IP address information. The message sent by the DHCP client at this stage includes a lease request, the client source address, 0.0.0.0, and the destination address, which is the broadcast address 255.255.255.255. The message also includes the client hardware MAC address and machine name, which also indicates the device that initiated the request to the DHCP server.
The real message that the client sends to the DHCP server to request an IP address is called a DHCPDISCOVER message. Every host on the network that has TCP/IP installed will receive this broadcast information, but only the DHCP server will respond.
Server provides IP address (DHCPOFFER):
All DHCP servers with valid IP addresses will provide IP address information to the DHCP client. It responds with an unassigned IP address in the address pool for the requesting host to use. To be able to respond to the DHCPDISCOVER message, the DHCP server must have valid IP configuration information for the client. The DHCPOFFER message that the DHCP server replies contains the following information:
- The client’s hardware address
- Provided IP address
- Appropriate subnet mask
- Lease validity period
- Server ID, which is the IP address of the DHCP server
The client selects an IP address (DHCPREQUEST):
The DHCP client selects the IP address provided by the first DHCPOFFER message it receives. It then broadcasts this information to the network. In this message, the client requests the IP address provided by the server. This is because the client may receive offers from more than one DHCP server. By broadcasting this request, the client informs other DHCP servers that it will not accept other offers. To further ensure that there is no doubt about the server offer that the client accepts, the DHCPREQUEST message also contains the following information:
- Provide the IP address of the server that accepted the offer
- Client hardware address
- IP addresses accepted by the client
Server confirms IP lease (DHCPACK):
The DHCP server responds to the client and assigns the IP address to the client. After that, it sends a DHCPACK confirmation message to the client. This message contains a valid lease for the IP address and other configuration information.
Sometimes, after the client receives the lease offered by the server, the DHCP lease request may still be unsuccessful. There may be several situations:
- The IP address is invalid because the client has moved to a different subnet.
- The client attempts to lease its previous IP address but that IP address is no longer available.
In the above situation, the server will send an unsuccessful message DHCPNACK. The client that receives DHCPNACK must restart the entire DHCP initialization process. In other words, it must send another DHCPDISCOVER message to find a new IP address.
PS: I saw a good book on gitbook, but it is too slow to access without a VPN. In order to facilitate my own reading and knowledge sharing, I reposted the article to Zhihu. Since I have not contacted the author (said to be a goddess), I took the liberty to post it here. If there is any offense, please contact me and I will delete it at any time. Thank you.