Detailed Explanation of DHCP Execution Process

Most of this article refers to:

"Internet Network Protocol"

Detailed Explanation of DHCP Protocol

Introduction to DHCP

Dynamic Host Configuration Protocol (Dynamic Host Configuration Protocol) , referred to as DHCP . The predecessor is the BOOTP protocol, which is a LAN network protocol. It uses the UDP protocol to work and uses two ports allocated by IANA uniformly: 67 (server side) and 68 (client side) . DHCP is usually used in a LAN environment . Its main function is to centralize management and assign IP addresses, so that the client can dynamically obtain information such as IP addresses, Gateway addresses, and DNS server addresses , and can improve the utilization rate of addresses.

To put it simply, its main function is: a protocol for dynamically assigning IP addresses and other information to newly added intranet machines when a new host joins in the intranet.

In addition, DHCP also provides the function of helping clients to pre-boot the execution environment PXE ( Pre-boot Execution Environment ).

Here is just a simple record of the process of DHCP assigning IP, PXE is just a simple pass.


DHCP as a function of PXE :

The network administrator, that is, DHCP , not only has the function of assigning IP , but also has a more important function, which is to help the client automatically install the operating system (PXE)

In some empty computers without OS installed, it may be necessary to initialize the OS through PXE when the system starts.

Now almost all network cards of general computers support PXE booting. The PXE client is in the ROM (read-only memory) of the network card. When the computer boots, the BIOS transfers the PXE client into the memory for execution.
After the installation is complete, you will be prompted to reboot your computer. At this time, it is enough to modify the BIOS back to boot from the hard disk during the reboot process. In this way, it is realized that only one operating system needs to be installed, and this operating system will be used when the computer is started later.

On the BIOS setting page, there is an item PXE Boot to LAN. If it is set to Enabled, it means that the computer starts from the network, and the configuration file and operating system kernel are downloaded from the PXE server to start; if it is set to Disabled, it means that it is booted locally, and the BIOS is started. Finally, it will look for the boot sector. If the operating system is not installed, the boot sector will not be found, and it will not start at this time.

The role of DHCP in PXE is mainly to provide the PXE client with the location of the PXE server, and then the PXE client can interact with the server to obtain information such as the files it wants, and complete the function of initializing the operating system.. I won’t go into details here, the following is a rough flow chart:

img

The following begins to formally introduce the general process of DHCP allocation of IP! ! !

In the DHCP protocol, the DHCP Server is similar to a network administrator . It only needs to configure a shared IP address. After that, each newly connected machine will apply for this shared IP address through the DHCP protocol, and it can work normally after it successfully applies for the IP. After waiting for the added host to be used up (equivalent to expiration, etc.), it can be returned to the DHCP Server for use by other machines.

Before introducing the workflow, let's introduce the IP address allocation method and the lease table.

There are three ways to assign addresses :

  • Manual configuration: the administrator specifies an address for each specific computer, or sets it through related commands (ifconfig, ip addr)
  • Automatic configuration: The server assigns a permanent address to the computer connecting to the network for the first time. After the DHCP client is successfully assigned an IP address from the DHCP server for the first time, it will use this address forever.
  • Dynamic configuration: The address is leased to the computer within a certain period of time. After the client is assigned an IP address from the DHCP server for the first time, it does not use the address permanently. After each use, the DHCP client must release the IP address , and the customer must renew the lease or deactivate the address after the lease period ends. For routers, the frequently used address allocation method is dynamic configuration.

Lease form :

  • Static lease table: corresponds to a static lease storage file, and the server reads the static lease table from the file when it is running. (Store assigned static IP)
  • Dynamic lease table: Corresponding to a periodic storage file, the server periodically saves the lease table into the file, and will read the last stored lease table at the beginning of the program. (The lease table records all currently assigned leases, including statically linked ones). (Store the assigned dynamic IP)
  • The DHCP server is always in the state of passively accepting requests. When there is a request from a client, the server will read the current state of the client and the information of the client, and search in the static lease table and dynamic lease table to find the corresponding Table entry, and then perform different replies according to the status of the client.
  • When receiving the first request from the client, the DHCP server first searches the static lease table; if there is a requested table entry, returns the static IP address of the client; otherwise, selects an available IP from the IP address pool to assign to the client, and adds information into the dynamic database. In addition, the server will periodically refresh the lease table and write it to the file archive. During this process, it will check the lease period of the dynamic lease table by the way.

DHCP general execution process

The DHCP workflow is roughly divided into the following four steps:

  • The new host applies for an IP address from the DHCP Server (DHCP DIscover)
  • DHCP Server sends the assigned IP to the new host (DHCP Offer)
  • The new host sends a DHCP Request broadcast packet, telling all DHCP servers which server to accept the IP and other information (DHCP Request)
  • The target DHCP Server broadcasts a DHCP ACK message confirmation packet to notify all hosts in the network that the applied IP address can be used normally (DHCP ACK)

The new host applies for an IP address from the DHCP Server

When a host newly joins a network, it only knows its own MAC address. It needs to tell other hosts in the network that it is coming, and apply for the corresponding IP and other information. This step is often called DHCP Discover .

The new host will use the IP address 0.0.0.0 to send a broadcast packet , provide its own MAC address, and tell all hosts that it is new, and now it needs an IP address, and requests the DHCP Server to assign an IP!

This broadcast packet encapsulates UDP, and UDP encapsulates BOOTP. In fact, DHCP is an enhanced version of BOOTP, but if you go to capture packets, the name you may see is still the BOOTP protocol. The format is as follows:

img

DHCP Server sends the assigned IP to the new host

The DHCP Server is equivalent to the IP administrator. After the new host sends a request, it can immediately receive and assign an IP (the first vacant IP will be selected from those addresses that have not been rented out ) to it. This process is called DHCP Offer . At the same time, the DHCP Server reserves the IP address provided for this client, so that it will not assign this IP address to other DHCP clients.

How does the DHCP Server determine the new host?

The DHCP Server uses the uniqueness of the MAC address to determine that the host sending the request is a new host.

The format of the DHCP Offer is like this, which contains the address assigned to the newcomer.

img

As can be seen from the figure above, the DHCP Server also sends broadcast packets. This is because although the MAC of the new host is confirmed, the corresponding IP address cannot be confirmed, so the new host can only accept it through the broadcast packet.

In addition to assigning IP, DHCP Server also assigns information such as subnet mask, gateway, and IPD address lease period.

The new host sends a DHCP Request broadcast packet to inform other DHCP Servers

Because the first step is to broadcast data packets, when there are multiple DHCP Servers in this network, it may apply for many IP addresses. At this time, it must be selected according to certain rules and notified to other DHCP Servers.

The new host will generally choose the first DHCP Offer, that is, the one that arrives first. Then a DHCP Request broadcast packet will be sent to the network, which contains == the MAC address of the client, the IP address in the accepted lease, the address of the DHCP server that provided this lease ==, etc., and tells all DHCP Servers which one it will accept At this time, other DHCP Servers will revoke the IP addresses provided by them so as to provide them to the next IP lease requester.

img

At this time, since the final confirmation from the DHCP Server has not been obtained, the client still broadcasts using 0.0.0.0 as the source IP address and 255.255.255.255 as the destination address. In BOOTP, accept the IP assigned by a certain DHCP Server.

Target DHCP Server broadcasts DHCP ACK message confirmation packet

When the DHCP server receives the client's DHCP request, it will broadcast and return a DHCP ACK message packet to the client, indicating that the client's choice has been accepted, and put the legal lease information and other configuration information of this IP address into the The broadcast packet is sent to the client.

img

  • After the client receives the DHCP ack broadcast, it will send three ARP resolution requests for this IP address to the network to perform conflict detection, and check whether other machines on the network use this IP address; if it is found that this IP address has been used, The client sends a DHCP decline packet to the DHCP server, rejects the IP address lease, and resends the DHCP discover message. At this point, in the DHCP server management console, the IP address will be displayed as BAD_ADDRESS.
  • If no other host on the network uses this IP address, the client's TCP/IP uses the IP address provided in the lease to complete initialization, so that it can communicate with hosts in other networks.

And at this time, other hosts in the network can also know the relevant information of the new host through this data packet.

After re-login , the newly added host does not need to re-execute, but uses the previously applied IP to send the DHCP Request directly. After the DHCP Server receives it, it will judge whether the current IP is still available (not assigned to other hosts), and if it is available, it will return an ACK message. If it is not available, then return the DHCP NAK message segment to inform the newly added host that DHCP Discover and subsequent processes need to be re-executed.

Types of DHCP packets

In fact, the above four steps correspond to the four message types.

It can also be found that the above are actually the processes under normal work, so there must be abnormal processes. For example, assigned IP addresses conflict, and so on. Then there must be corresponding messages for processing.

DHCP has eight message types in total:

message type illustrate
Discover(0x01) The DHCP client does not know the location of the DHCP server when requesting an IP address, so the DHCP client broadcasts a Discover request message in the local network to discover the DHCP server on the network. All DHCP servers that receive the Discover message will send a response message, and the DHCP client can know the location of the DHCP server existing in the network based on this message.
Offer(0x02) After the DHCP server receives the Discover message, it will search for a suitable IP address in the configured address pool, add the corresponding lease period and other configuration information (such as gateway, DNS server, etc.), and construct an Offer message. Sent to the DHCP client to inform the user that the server can provide an IP address for it. But this message only tells the DHCP client that it can provide an IP address, and finally the client needs to use ARP to detect whether the IP address is duplicated.
Request(0x03) The DHCP client may receive many Offer request messages, so it must choose one of these responses. Usually, the server of the first Offer response message is selected as its own target server, and a broadcast Request message is sent to the server to notify the selected server that it hopes to obtain the assigned IP address. In addition, after successfully obtaining an IP address, the DHCP client will send a unicast Request message to the DHCP server to request to renew the lease when the lease period of the address reaches 50%. 87.5% of the time, the broadcast Request request message will be sent again to request to renew the lease
ACK(0x05) After the DHCP server receives the Request request message, it searches whether there is a corresponding lease record according to the user MAC carried in the Request message, and if so, sends an ACK response message to notify the user that the allocated IP address can be used.
WANT(0x06) If the DHCP server does not find a corresponding lease record after receiving the Request message or cannot normally allocate an IP address due to some reasons, it will send a NAK response message to the DHCP client to notify the user that a suitable IP address cannot be allocated.
Release(0x07) When the DHCP client no longer needs to use the assigned IP address (usually when the client is shut down or offline, etc.), it will actively send a RELEASE request message to the DHCP server to inform the server that the user no longer needs to assign an IP address and request the DHCP server Release the corresponding IP address.
Decline(0x04) After the DHCP client receives the ACK response message from the DHCP server, if it finds that the address allocated by the server conflicts or cannot be used due to other reasons through address conflict detection, it will send a Decline request message to the DHCP server to notify the server that the allocated IP address is unavailable. to obtain a new IP address.
nform(0x08) If the DHCP client needs to obtain more detailed configuration information from the DHCP server, it will send an Inform request message to the DHCP server; after receiving the message, the DHCP server will search for the corresponding configuration information according to the lease, and send The DHCP client sends an ACK response message. Basically useless now.

Revocation and renewal of IP address

Recycling of IP addresses: The addresses applied by DHCP generally have a time limit, that is, the allocated IP addresses will be recycled after the time limit expires.

Renewal of IP address: In the DHCP protocol, when the lease period reaches 50%, the client will directly send a DHCP request message packet to the DHCP Server that provided the IP address for it. After the client computer receives the DHCP ACK message packet responded by the server, it will update its configuration according to the new lease period and other updated TCP/IP parameters provided in the packet. In this way, the IP lease update is completed.

If there is no renewal when 50% of the lease has passed, the client will contact the DHCP that provided it with the IP address again when 87.5% of the lease has passed. If it is still unsuccessful, the client must give up the IP address and apply again when the lease is 100%. If there is no DHCP available at this time, the client will randomly obtain a related address according to the previously obtained subnet information, and try again every 5 minutes.

Guess you like

Origin blog.csdn.net/qq_53578500/article/details/126666506