This article describes how DHCP allocates IPV4 addresses from the following aspects: assignment mode, working process, lease, packet format, and relay agent.


Review of previous article:
GO interface implementation analysis


The profile

Dynamic Host Configuration Protocol (DHCP) is a Dynamic Host Configuration Protocol that uses UDP to dynamically allocate IP addresses to LAN clients. DHCP has three commonly used ports 67,68, and 546. Ports 67 and 68 are used by the DHCP Server and DHCP Client. Port 546 is used for DHCP Failover and needs to be independently enabled for dual-system hot backup.

DHCP Assignment Mode

There are three DHCP assignment modes: static, automatic, and dynamic.

Static assignment: The DHCP server sends the assigned IP address to the DHCP client. In this mode, the DHCP server maps the MAC address of the client to the assigned IP address.

The DHCP server configuration is as follows:

Automatic allocation: The server assigns an IP address from the IP address pool to the client. On the Firewall, if the lease is set to Unlimited, the IP address is permanently assigned. In this assignment mode, the client can use the IP address permanently after obtaining the IP address from the DHCP server for the first time. This address cannot be assigned to other clients.

Dynamic allocation: After receiving an IP address from a client, the DHCP selects an IP address from the IP pool for allocation. The IP address is valid only during the lease period. If the lease expires, the IP address will be reclaimed.

The DHCP server configuration is as follows:

DHCP Process

1. DHCP Discover: The client sends a DHCP Discover message in broadcast mode. All DHCP servers on the LAN can receive the DHCP Discover message.

2. DHCP Offer: After receiving the DHCP Discover message from the client, the DHCP server selects an IP address from the configured address pool and preallocates the IP address to the client. The IP address is encapsulated in the DHCP Offer message and broadcast to the client.

3. DHCP Request: The client selects an IP address from the received DHCP Offer message, which is usually the first RECEIVED DHCP Offer message. However, the client cannot use this IP address.

DHCP Ack: The DHCP server checks whether the IP address is assigned by the client. If not, the DHCP server returns the IP address to the address pool and waits for the next request from the client. If you select a self-assigned IP address, the DHCP SERVER broadcasts a DHCP ACK packet to the client and binds the MAC address and IP address of the client on the server.

5. After receiving the packet, the client uses ARP to detect whether the IP address is used by other clients. If no response is received within the timeout period, the client uses the IP address. Apply for an IP address from the DHCP server.

After being restarted, the DHCP client does not need to send a DHCP Discover message to apply for an address. The DHCP client performs operations similar to renewal.

Packets captured using tcpdump are as follows:

The DHCP lease

Automatic renewal

A dynamically allocated IP address has a lease period. If the client still wants to use the IP address after the lease ends, the client needs to renew the IP address. The application process is as follows:

In addition, the lease of dynamically allocated IP addresses is limited. When 50% of the lease remains, the client sends a DHCP REQUEST message to the DHCP server to apply for lease renewal. If the server returns a DHCP ACK message, the client successfully renewed the lease. In this case, the CLIENT sends a DHCP NAK packet to all DHCP servers on the LAN in broadcast mode. In this packet, the REQUEST IP address is the IP address in use. If no DHCP ACK packet is received, the client continues to use the current IP address. The preceding procedure is repeated at 87.5% of the lease period. If the lease is not renewed, the client releases the IP address and applies for an IP address again.

Manually release IP addresses and apply for new ONES

The commands for DHCP to manually release IP addresses or apply for IP addresses are as follows (in Windows and Linux respectively) :

ipconfig /release| dhclient -r   Release the current IP address and other configuration informationCopy the code
ipconfig /renew | dhclient   eth0     Apply for an IP address from the DHCP serverCopy the code

DHCP Packet Format

The DHCP packet format is as follows:

The meanings of the parameters are as follows:

Op: indicates the packet type. The value can be 1 or 2

1: indicates the client request packet

2: indicates that the server responds to the packet

Htype: indicates the hardware type. 1 indicates Ethernet

Hlen: The length of the hardware address is 6 for the Ethernet

Hops: indicates the number of trunks that the current client has passed. The default value is 0 and the value must be smaller than or equal to 16. If the value is greater than 16, the client is marked as unreachable.

Xid: a random number selected by the client and used to correspond to the packet sent by the server

Secs: indicates the time after the client obtains an address or rents an address, in seconds

Flags: indicates that the first 15 bits are 0. The 16th bit indicates whether the packet is broadcast or multicast

0: The client sends packets to the server in unicast mode

1: The client sends packets to the server in broadcast mode

Ciaddr (Client IP address) : indicates the IP address of the client. It can be an IP address assigned by the server or an existing IP address on the client. In the client initialization state, the value is 0.0.0.0.

Yiaddr (Your Client IP Address) : indicates the IP address to be allocated by the server to the client. This field is populated when the server responds.

Siaddr (Server IP addr) : indicates the IP address from which the client obtains the configuration information.

Gateway IP addr (Giaddr) : indicates the IP address of the first DHCP relay. When the client and the DHCP server are on different network segments, when the client sends a DHCP request to the first DHCP Relay server, the DHCP server fills this field with its OWN IP address, and the server sends response packets based on this IP address.

Chaddr (Client Hardware Address) : indicates the MAC address of the client.

Sname: Indicates the name of the server from which the client obtains configuration information. This parameter is optional.

File: Indicates the name of the configuration file to be obtained by the client. This field is optional and is filled in by the server.

Options: indicates the DHCP option field.

DHCP can only communicate with clients on the same network segment and assign IP addresses. To solve this problem, you can use DHCP proxy to transfer packets between the client and server. To clients, a DHCP proxy server is a DHCP server, and a DHCP proxy server is a client that applies for IP address allocation. The following describes how a proxy server forwards packets and how to configure the proxy server.

DHCP relay agent

DHCP relay is used to allocate IP addresses when the DHCP server and client reside on different network segments. After receiving a DHCP Discover packet from a client, the DHCP Relay agent modifies the corresponding fields in the packet, changes the packet from broadcast to unicast, and sends the packet to the specified DHCP Server. The DHCP Relay agent also forwards other packets. The communication between the DHCP Relay Server and DHCP clients is transparent, and the communication between the DHCP Relay Server and clients is transparent.

The architecture diagram is as follows:

Configuration is as follows

Configure the DHCP relay server

1. Modify /etc/sysctl.conf to enable the Linux route-forwarding function

# Controls IP packet forwarding
net.ipv4.ip_ forward = 1Copy the code

2. Make the configuration take effect

#sysctl -pCopy the code

3. Configure DHCprelay

#vim /etc/sysconfig/dhcrelayCopy the code

4. Start the trunk service

#service dhcrelay startCopy the code

DHCP Server Configuration

DHCP Protocol Defects

DHCP can allocate IP addresses well, but it has some disadvantages, such as:

1. DHCP does not support IP address allocation across network segments

2. DHCP cannot obtain IP addresses from non-DHCP clients. If the address pool is improperly set, address conflicts may occur.

Based on the above defects, we need to properly set the IP address pool, reserve some IP addresses, and allocate them to the servers that need fixed IP addresses.

The above is the DHCP protocol introduction, I hope to help you have a simple understanding of DHCP protocol.


This article was first published on the public account “Mi Operation and Maintenance”. Click to view the original article.