Talk about DHCP protocol

table of Contents

01. Introduction to DHCP protocol

02, DHCP working principle

03, Wireshark packet capture analysis

04, DHCP shortcomings


01. Introduction to DHCP protocol

DHCP (DynamicHost ConfigurationProtocol), dynamic host configuration protocol, is an application layer protocol. For details, please refer to the article " TCP/IP four-layer model ". When we set the client host ip address to be dynamically obtained, the DHCP server will assign an IP to the client according to the DHCP protocol, so that the client can use this IP to access the Internet.

Several concepts of DHCP:

DHCPClient : DHCP client, a client that requests an IP address through the DHCP protocol. The DHCP client is an interface-level concept. If a host has multiple Ethernet interfaces, each interface on the host can be configured as a DHCP client. Each VLAN interface on the switch can also be configured as a DHCP client.

DHCPServer : The DHCP server is responsible for providing IP addresses for DHCP clients and for managing the assigned IP addresses.

DHCPRelay : DHCP relay, when a DHCP client applies for an IP address across network segments, it implements the function of forwarding DHCP messages.

DHCPSecurity : DHCP security features, realize the management function of the legal user IP address table.

DHCPSnooping : DHCP snooping, records user information that has applied for an IP address through a Layer 2 device.

02, DHCP working principle

DHCP uses the UDP protocol to work, using two port numbers 67 (DHCP server) and 68 (DHCP client) . Port 546 is used for DHCPv6Client, not for DHCPv4, and serves for DHCPfailover. The message sent by the DHCP client to the DHCP server is called a DHCP request message, and the message sent by the DHCP server to the DHCP client is called a DHCP response message.

image

The DHCP interaction process is divided into 4 steps

The first step : The client initiates a DHCP Discover packet in the LAN, the purpose is to find a DHCPServer that can provide it with an IP.

Step 2 : After the available DHCPServer receives the Discover packet, it sends a DHCPOffer packet to give the client a response, which is intended to tell the client that it can provide an IP address.

Step 3 : After the Client receives the Offer packet, it sends a DHCPRequest packet to request IP allocation. 

Step 4 : DHCPServer sends an ACK packet to confirm the information.

 

03, Wireshark packet capture analysis

To capture DHCP packets, you must first ensure that there is an available DHCP server, and then set the host IP address acquisition mode to be automatically acquired. Our test environment is that the notebook is set to DHCP mode, and the router enables the DHCP service. Since the laptop wants to route DHCP to obtain the IP when it is turned on, the wireshark software cannot be quickly opened to capture packets when it is turned on. We use the following command to disconnect the host's network connection first, and then connect to the network.

ipconfig /release
ipconfig /renew

In cmd, you can use ipconfig/? to view the meaning of each parameter

Detailed command:

(1)ipconfig /release

Disconnect the current network connection, the host IP becomes 0.0.0.0, the host is disconnected from the network, and cannot access the network. 

(2)ipconfig /renew 

Update the adapter information and request to connect to the network. After this command is over, the host will get an available IP and access the network again.

 

The captured data packets are as follows, pay attention to the use of dhcp filter conditions .

image

Wirehark capture file: click to download

You can clearly see the four steps of DHCP.

 

DHCP Discover packet

In the Discover stage, it can be seen that the client is sending broadcast replication. For MAC address broadcast related content, please see " 802.3 Ethernet Frame ". For IP address broadcast related content, please see " IP Protocol Detailed Explanation ". It can also be seen from the figure below that DHCP is based on the UDP protocol , using two port numbers 67 (DHCP server) and 68 (DHCP client) . This was mentioned above and confirmed in the packet capture file. The DHCP message format is based on the BOOTP message format. The specific message format of DHCP is not the focus of this article. This article will not explain the meaning of each byte in the DHCP message in detail.

image

 

DHCP Offer package 

When the DHCP server receives a DHCPDiscover packet, it responds with a DHCPOfferr packet to the client. In this datagram, the client has obtained the most important IP address information. In addition, the server also sent information such as the subnet mask, router, DNS, domain name, and IP address lease period.

The DHCP server still uses the broadcast address as the destination address , because at this time the client requesting the IP assignment does not have its own IP, and there may be multiple clients using the IP 0.0.0.0 as the source IP to send an IP assignment request to the DHCP server, and DHCP cannot Use the IP 0.0.0.0 as the destination IP address, so the broadcast method is still used to tell the requesting Clients that this is a DHCP server that can be used.

image

 

DHCP Request包 

When the Client receives the DHCPOffer packet (if there are multiple DHCP servers available, it may receive multiple DHCPOffer packets), confirm that there is a DHCP server that can interact with it, and then the Client sends a Request packet to request an IP allocation. 

At this time, the source IP and destination IP are still 0.0.0.0 and 255.255.255.255.

image

 

DHCP ACK packet 

The server responds to the DHCP request with a DHCPACK packet.

 image

04, DHCP shortcomings

DHCP can allocate IP addresses very well, but the DHCP protocol has some shortcomings, such as:

1. The DHCP protocol does not support address assignment across network segments

2. The DHCP protocol cannot obtain the IP address of a non-DHCP client. If the address pool is set unreasonably, it may cause an address conflict.

Based on the above-mentioned shortcomings, we need to set up the IP address pool reasonably and reserve some IP addresses to assign to servers that need a fixed IP.

 

Click to view the album where this article is located, STM32F207 network development

 

Pay attention to the official account, and receive article updates as soon as possible .

Guess you like

Origin blog.csdn.net/Firefly_cjd/article/details/112519354