Dynamic Host Configuration Protocol DHCP (DHCPv4)

1. Introduction to DHCP

Dynamic Host Configuration Protocol (DHCP) is a technology for centralized dynamic management and configuration of user IP addresses.

DHCP adopts client/server communication mode. The client (DHCP Client) makes a configuration application to the server (DHCP Server), and the server returns the configuration information (including IP address, default gateway, DNS Server, WINS Server, etc.) allocated to the client Parameters), which can realize the dynamic allocation of IP addresses and the centralized configuration management of other network parameters.

The development of DHCP:

BOOTP (Bootstrap Protocol) is a boot protocol, based on the IP/UDP protocol, also known as the bootstrapping protocol, and is the predecessor of the DHCP protocol. BOOTP is used in the local area network of diskless workstations, allowing diskless workstations to obtain an IP address from a central server. Through the BOOTP protocol, the diskless workstations in the LAN can be assigned dynamic IP addresses, so that there is no need for the administrator to set a static IP address for each user.

DHCP is developed on the basis of BOOTstrap (BOOTstrap Protocol), but BOOTP runs in a relatively static environment (each host has a fixed network connection). The administrator configures a special BOOTP parameter file for each host. Will remain unchanged for a long time. DHCP extends BOOTP from the following two aspects:

  • DHCP adds the functions of dynamic allocation of reused network addresses and additional configuration options, allowing the computer to obtain all the configuration information it needs with only one message.
  • DHCP allows computers to obtain IP addresses dynamically, rather than assigning addresses to each host statically.

DHCP technology realizes the dynamic allocation and centralized management of user addresses and configuration information, so that enterprises can dynamically allocate and manage addresses for enterprise users, avoid tedious manual configuration, and can quickly adapt to network changes.

2. DHCP message:

DHCP message type:

DHCP Discover The client finds available servers
DHCP offer The server is used to respond to the client's DHCP Discover message and specify the corresponding configuration parameters
DHCP Request Sent by the client to the server to request configuration parameters or request configuration confirmation or lease renewal
DHCP ACK From server to client, including configuration parameters including IP address.
DHCP Decline When the client finds that the address has been used, it is used to notify the server.
DHCP Inform When the client already has an IP address, use it to request other configuration parameters from the server
DHCP NAK The address request sent by the server to the client to register the client is incorrect or the lease has expired
DHCP Release Used to notify the server when the client wants to release the address

DHCP messages are high-level protocol messages carried on UDP, using two port numbers, 67 (DHCP server) and 68 (DHCP client).

DHCP message format

Insert picture description here

The meaning of each field in the DHCP message

Field Length/byte meaning
ON (on code) 1 Indicates the type of message: 1. Client request message, 2. Server response message
htype (hardware type) 1 Indicates the type of hardware address. For Ethernet, the value of this type is "1".
Hops 1 Indicates the number of DHCP relays through which the current DHCP message passes. This field is set to 0 by the client or server, and 1 is added to this field every time a DHCP relay is passed. The function of this field is to limit the number of DHCP relays that DHCP messages pass through.
xid 4 Transaction ID, a random number selected by the client, is used by the server and client to communicate requests and responses between them, and the client uses it to match requests and responses. The ID is set by the client and returned by the server as a 32-bit integer.
secs (seconds) 2 Filled by the client, indicating the number of seconds since the client started to obtain the IP address or the IP address was renewed.
flags 2 This field is reserved unused in BOOTP and represents a flag field in DHCP. Only the highest bit of the flag field is meaningful, and the remaining bits are set to 0. The leftmost field is interpreted as the broadcast response flag bit, 0: the client requests the server to send the response message in unicast form 1: the client requests the server to send the response message in broadcast form
ciaddr (client ip address) 4 Indicates the IP address of the client. It can be an IP address assigned by the server to the client or an existing IP address of the client. The client does not have an IP address in the initialization state, and this field is 0.0.0.0. The IP address 0.0.0.0 only allows the host to use it for temporary communication when the system using DHCP is started, and is not a valid destination address.
yiaddr (your client ip address) 4 Indicates the IP address assigned by the server to the client. When the server responds with DHCP, it will fill in this field with the IP address assigned to the client.
siaddr (server ip address) 4 The DHCP client obtains the IP address of the server that starts the configuration information.
giaddr (gateway ip address) 4 This field indicates the IP address of the first DHCP relay (note: not the gateway defined in the address pool). When the client sends a DHCP request, if the server and the client are not in the same network, the first DHCP relay will fill in this field with its own IP address when forwarding the DHCP request message. The server will determine the network segment address based on this field, and then select the address pool for assigning addresses to the user. The server will also send a response message to the DHCP relay based on this address, and the DHCP relay will forward the message to the client. If more than one DHCP relay is passed before reaching the DHCP server, then the relay after the first DHCP relay will not change this field, but only increase the number of Hops by 1.
chaddr (client hardware address) 16 该字段表示客户端的MAC地址,此字段与前面的“Hardware Type”和“Hardware Length”保持一致。当客户端发出DHCP请求时,将自己的硬件地址填入此字段。对于以太网,当“Hardware Type”和“Hardware Length”分别为“1”和“6”时,此字段必须填入6字节的以太网MAC地址。
sname (server host name) 64 该字段表示客户端获取配置信息的服务器名字。此字段由DHCP Server填写,是可选的。如果填写,必须是一个以0结尾的字符串。
file (file name) 128 该字段表示客户端的启动配置文件名。此字段由DHCP Server填写,是可选的,如果填写,必须是一个以0结尾的字符串。
options 可变 该字段表示DHCP的选项字段,至少为312字节,格式为"代码+长度+数据"。DHCP通过此字段包含了服务器分配给终端的配置信息,如网关IP地址,DNS服务器的IP地址,客户端可以使用IP地址的有效租期等信息。

DHCP Opthion字段选项

DHCP报文中的Options字段可以用来存放普通协议中没有定义的控制信息和参数。如果用户在DHCP服务器端配置了Options字段,DHCP客户端在申请IP地址的时候,会通过服务器端回应的DHCP报文获得Options字段中的配置信息。

Options字段由Type、Length和Value三部分组成。这三部分的表示含义如下所示:

字段 长度 含义
Type 1字节 该字段表示信息类型
Length 1字节 该字段表示后面信息内容的长度
Value 其长度为Length字段所指定 该字段表示信息内容

常见Options字段
DHCP Options选项的取值范围为1~255。

Option id 描述
1 设置子网掩码选项
3 设置网关地址选项
6 设置DNS服务器地址选项
7 设置日志服务器地址选项
12 设置DHCP客户端的主机名选项
15 设置域名后缀选项
33 设置静态路由选项。该选项中包含一组有分类静态路由(即目的地址的掩码固定为自然掩码,不能划分子网),客户端收到该选项后,将在路由表中添加这些静态路由。如果存在Option121,则忽略该选项。
44 设置NetBios服务器选项
46 设置NetBios节点类型选项
50 设置请求IP地址选项
51 设置IP地址租约时间选项
52 设置Option附加选项
53 设置DHCP消息类型,长度为1字节,1-DHCP DISCOVER、2-DHCP OFFER、3-DHCP REQUEST、4-DHCP DECLINE、5-DHCP ACK、6-DHCP NAK、7-DHCP RELEASE、8-DHCP INFORM
54 4字节,用来设置服务器标识
55 设置请求参数列表选项。客户端利用该选项指明需要从服务器获取哪些网络配置参数。该选项内容为客户端请求的参数对应的选项值
58 设置续约T1时间,一般是租期时间的50%。
59 设置续约T2时间。一般是租期时间的87.5%。
60 设置厂商分类信息选项,用于标识DHCP客户端的类型和配置。华为自定义:可配置该终端设备在发起DHCP请求时,通过Option 60携带域信息。收到DHCP报文时,可根据Option 60中携带的域信息来分配IP地址。
61 设置客户端标识选项
66 设置TFTP服务器名选项,用来指定为客户端分配的TFTP服务器的域名
67 设置启动文件名选项,用来指定为客户端分配的启动文件名
82 华为自定义:作为DHCP Relay,在中继用户DHCP报文时,可在Option 82中填写用户的物理位置信息,通知DHCP服务器按物理位置信息对为用户分配IP地址。
121 设置无分类路由选项。该选项中包含一组无分类静态路由(即目的地址的掩码为任意值,可以通过掩码来划分子网),客户端收到该选项后,将在路由表中添加这些静态路由。

根据Options选项功能的不同,此字段的作用对象也不同。

主要记忆50~60Option id的作用。

三、DHCP原理描述

DHCP角色:

  • DHCP客户端:通过DHCP协议请求获取IP地址等网络参数的设备。例如,IP电话、PC、手机、无盘工作站等。
  • DHCP服务器:负责为DHCP客户端分配网络参数的设备。
  • (可选)DHCP中继:负责转发DHCP服务器和DHCP客户端之间的DHCP报文,协助DHCP服务器向DHCP客户端动态分配网络参数的设备。

DHCP客户端广播发送请求报文(即目的IP地址为255.255.255.255),位于同一网段内的DHCP服务器能够接收请求报文。如果DHCP客户端和DHCP服务器不在同一个网段,DHCP服务器无法接收来自客户端的请求报文,此时,需要通过DHCP中继来转发DHCP报文。不同于传统的IP报文转发,DHCP中继接收到DHCP请求或应答报文后,会重新修改报文格式并生成一个新的DHCP报文再进行转发。

在企业网络中,如果需要规划较多网段,且网段中的终端都需要通过DHCP自动获取IP地址等网络参数时,可以部署DHCP中继。这样,不同网段的终端可以共用一个DHCP服务器,节省了服务器资源,方便统一管理。

DHCP报文是基于UDP协议传输的。
DHCP客户端向DHCP服务器发送报文时采用67端口号,DHCP服务器向DHCP客户端发送报文时采用68端口号。

DHCP服务器给首次接入网络的客户端分配网络参数的工作原理:

无中继场景时DHCP客户端首次接入网络的工作原理:

Insert picture description here

  1. 发现阶段——DHCP客户端发现DHCP服务器的阶段。

    DHCP客户端发送DHCP DISCOVER报文来发现DHCP服务器。由于DHCP客户端不知道DHCP服务器的IP地址,所以DHCP客户端以广播方式发送DHCP DISCOVER报文(目的IP地址为255.255.255.255,临时源IP为0.0.0.0),同一网段内所有DHCP服务器或中继都能收到此报文。DHCP DISCOVER报文中携带了客户端的MAC地址(DHCP DISCOVER报文中的chaddr/clint hardware address字段)、需要请求的参数列表选项(Option55中填充的内容,标识了客户端需要从服务器获取的网络配置参数)、广播标志位(DHCP DISCOVER报文中的flags字段,表示客户端请求服务器以单播或广播形式发送响应报文)等信息。
    DHCP DISCOVER报文中的Option字段定义了网络参数信息,不同Option值代表了不同的参数。例如,Option3表示客户端的网关地址选项(当客户端发送的DHCP DISCOVER报文的Option55中填充了选项值3,就表示客户端希望从服务器获取网关地址);Option53表示DHCP报文类型(例如,DHCP DISCOVER报文)。Option选项分为知名选项和自定义选项,关于知名选项的含义请参见RFC2132。除了RFC2132里面定义的知名选项,不同厂商可以根据需求自己定义自定义选项,例如,Option43为厂商特定信息选项。
    RFC2131中定义了DHCP报文的广播标志字段(flags),当标志字段的最高位为0时,表示客户端希望服务器以单播方式发送DHCP OFFER/DHCP ACK报文;当标志字段的最高位为1时,表示客户端希望服务器以广播方式发送DHCP OFFER/DHCP ACK报文。

  2. 提供阶段——DHCP服务器提供网络配置信息的阶段。

    位于同一网段的DHCP服务器都会接收到DHCP DISCOVER报文,每个DHCP服务器上可能会部署多个地址池,服务器通过地址池来管理可供分配的IP地址等网络参数。服务器接收到DHCP DISCOVER报文后,选择跟接收DHCP DISCOVER报文接口的IP地址处于同一网段的地址池,并且从中选择一个可用的IP地址,然后通过DHCP OFFER报文发送给DHCP客户端。DHCP OFFER报文里面携带了希望分配给指定MAC地址客户端的IP地址(DHCP报文中的yiaddr/your client ip address字段)及其租期等配置参数

    通常,DHCP服务器的地址池中会指定IP地址的租期,如果DHCP客户端发送的DHCP DISCOVER报文中携带了期望租期,服务器会将客户端请求的期望租期与其指定的租期进行比较,选择其中时间较短的租期分配给客户端

    选择IP地址的优先顺序:

    • DHCP服务器上已配置的与客户端MAC地址静态绑定的IP地址。
    • 客户端发送的DHCP DISCOVER报文中Option50字段(请求IP地址选项)指定的地址。
    • DHCP服务器上记录的曾经分配给客户端的IP地址。
    • 按照IP地址从大到小的顺序查询,选择最先找到的可供分配的IP地址。
    • 如果未找到可供分配的IP地址,则依次查询超过租期、处于冲突状态的IP地址,如果找到可用的IP地址,则进行分配;否则,发送DHCP NAK报文作为应答,通知DHCP客户端无法分配IP地址。DHCP客户端需要重新发送DHCP DISCOVER报文来申请IP地址。

    为了防止分配出去的IP地址跟网络中其他客户端的IP地址冲突,DHCP服务器在发送DHCP OFFER报文前可以通过发送源地址和目的地址都为预分配出去IP地址的ICMP ECHO REQUEST报文对分配的IP地址进行地址冲突探测。如果在指定的时间内没有收到应答报文,表示网络中没有客户端使用这个IP地址,可以分配给客户端;如果指定时间内收到应答报文,表示网络中已经存在使用此IP地址的客户端,则把此地址列为冲突地址,然后等待重新接收到DHCP DISCOVER报文后按照前面介绍的选择IP地址的优先顺序重新选择可用的IP地址。

    此阶段DHCP服务器分配给客户端的IP地址不一定是最终确定使用的IP地址,因为DHCP OFFER报文发送给客户端等待16秒后如果没有收到客户端的响应,此地址就可以继续分配给其他客户端。通过下面的选择阶段和确认阶段后才能最终确定客户端可以使用的IP地址。

  3. 请求阶段——DHCP客户端选择IP地址的阶段。

    因为DHCP DISCOVER报文是广播发送的,所以如果同一网段内存在多个DHCP服务器,接收到DHCP DISCOVER报文的服务器都会回应DHCP OFFER报文。如果有多个DHCP服务器向DHCP客户端回应DHCP OFFER报文,则DHCP客户端一般只接收第一个收到的DHCP OFFER报文,然后以广播方式发送DHCP REQUEST报文,该报文中包含客户端想选择的DHCP服务器标识符(即Option54)和分配给客户端IP地址(即Option50,填充了接收的DHCP OFFER报文中yiaddr字段的IP地址)。

    以广播方式发送DHCP REQUEST报文,是为了通知所有的DHCP服务器,它将选择某个DHCP服务器提供的IP地址,其他DHCP服务器可以重新将曾经分配给客户端的IP地址分配给其他客户端。

  4. 确认阶段——DHCP服务器确认所分配IP地址的阶段。

    当DHCP服务器收到DHCP客户端发送的DHCP REQUEST报文后,DHCP服务器回应DHCP ACK报文,表示DHCP REQUEST报文中请求的IP地址(Opton50填充的)分配给客户端使用。

    DHCP客户端收到DHCP ACK报文,会广播发送免费ARP(FARP)报文,探测本网段是否有其他终端使用服务器分配的IP地址,如果在指定时间内没有收到回应,表示客户端可以使用此地址。如果收到了回应,说明有其他终端使用了此地址,客户端会向服务器发送DHCP DECLINE报文,并重新向服务器请求IP地址,同时,服务器会将此地址列为冲突地址。当服务器没有空闲地址可分配时,再选择冲突地址进行分配,尽量减少分配出去的地址冲突。

    当DHCP服务器收到DHCP客户端发送的DHCP REQUEST报文后,如果DHCP服务器由于某些原因(例如协商出错或者由于发送REQUEST过慢导致服务器已经把此地址分配给其他客户端)无法分配DHCP REQUEST报文中Opton50填充的IP地址,则发送DHCP NAK报文作为应答,通知DHCP客户端无法分配此IP地址。DHCP客户端需要重新发送DHCP DISCOVER报文来申请新的IP地址。

有中继场景时DHCP客户端首次接入网络的工作原理:

Insert picture description here

  1. 发现阶段

    1. 由于DHCP客户端不知道DHCP服务器的IP地址,所以DHCP客户端以广播方式发送DHCP DISCOVER报文(目的IP地址为255.255.255.255,临时源IP为0.0.0.0),同一网段内所有DHCP服务器或中继都能收到此报文。

    2. 检查DHCP报文中的hops字段,如果大于16,则丢弃DHCP报文;否则,将hops字段加1(表明经过一次DHCP中继),并继续下面的操作。

    3. DHCP报文中的hops字段表示DHCP报文经过的DHCP中继的数目,该字段由客户端或服务器设置为0,每经过一个DHCP中继时,该字段加1。hops字段的作用是限制DHCP报文所经过的DHCP中继的数目。目前,设备最多支持DHCP客户端与服务器之间有存在16个中继。

    4. 检查DHCP报文中的giaddr(gateway ip address)字段。如果是0,将giaddr字段设置为接收DHCP DISCOVER报文的接口IP地址。如果不是0,则不修改该字段,继续下面的操作。

    5. DHCP报文中的giaddr字段标识客户端网关的IP地址。如果服务器和客户端不在同一个网段且中间存在多个DHCP中继,当客户端发出DHCP请求时,第一个DHCP中继会把自己的IP地址填入此字段,后面的DHCP中继不修改此字段内容,DHCP服务器会根据此字段来判断出客户端所在的网段地址,从而为客户端分配该网段的IP地址

    6. 将DHCP报文的目的IP地址改为DHCP服务器或下一跳中继的IP地址,源地址改为中继连接客户端的接口地址,通过路由转发将DHCP报文单播发送到DHCP服务器或下一跳中继

    如果DHCP客户端与DHCP服务器之间存在多个DHCP中继,后面的中继接收到DHCP DISCOVER报文的处理流程同前面所述。

  2. 提供阶段

    1. DHCP服务器接收到DHCP DISCOVER报文后,选择与报文中giaddr字段为同一网段的地址池,并为客户端分配IP地址等参数然后向giaddr字段标识的DHCP中继单播发送DHCP OFFER报文。

    2. 检查报文中的giaddr字段,如果不是接口的地址,则丢弃该报文;否则,继续下面的操作。

    3. DHCP中继检查报文的广播标志位。如果广播标志位为1,则将DHCP OFFER报文广播发送给DHCP客户端;否则将DHCP OFFER报文单播发送给DHCP客户端。

  3. 选择阶段

  4. 确认阶段

选择阶段和确认阶段的过程同无中继场景。

DHCP客户端重用曾经使用的地址的工作原理:

DHCP客户端非首次接入网络时,可以重用曾经使用过的地址。
Insert picture description here

  1. 客户端广播发送包含前一次分配的IP地址的DHCP REQUEST报文,报文中的Option50(请求的IP地址选项)字段填入曾经使用过的IP地址。
  2. DHCP服务器收到DHCP REQUEST报文后,根据DHCP REQUEST报文中携带的MAC地址来查找有没有相应的租约记录,如果有则返回DHCP ACK报文,通知DHCP客户端可以继续使用这个IP地址。否则,保持沉默,等待客户端重新发送DHCP DISCOVER报文请求新的IP地址。

DHCP客户端更新租期的工作原理:

DHCP服务器采用动态分配机制给客户端分配IP地址时,分配出去的IP地址有租期限制。DHCP客户端向服务器申请地址时可以携带期望租期,服务器在分配租期时把客户端期望租期和地址池中租期配置比较,分配其中一个较短的租期给客户端。租期时间到后服务器会收回该IP地址,收回的IP地址可以继续分配给其他客户端使用。这种机制可以提高IP地址的利用率,避免客户端下线后IP地址继续被占用。如果DHCP客户端希望继续使用该地址,需要更新IP地址的租期(如延长IP地址租期)。

  • 当租期达到50%(T1)时,DHCP客户端会自动以单播的方式向DHCP服务器发送DHCP REQUEST报文,请求更新IP地址租期。如果收到DHCP服务器回应的DHCP ACK报文,则租期更新成功(即租期从0开始计算);如果收到DHCP NAK报文,则重新发送DHCP DISCOVER报文请求新的IP地址。
  • 当租期达到87.5%(T2)时,如果仍未收到DHCP服务器的应答,DHCP客户端会自动以广播的方式向DHCP服务器发送DHCP REQUEST报文,请求更新IP地址租期。如果收到DHCP服务器回应的DHCP ACK报文,则租期更新成功(即租期从0开始计算);如果收到DHCP NAK报文,则重新发送DHCP DISCOVER报文请求新的IP地址。
  • 如果租期时间到时都没有收到服务器的回应,客户端停止使用此IP地址,重新发送DHCP DISCOVER报文请求新的IP地址。

客户端在租期时间到之前,如果用户不想使用分配的IP地址(例如客户端网络位置需要变更),会触发DHCP客户端向DHCP服务器发送DHCP RELEASE报文,通知DHCP服务器释放IP地址的租期。DHCP服务器会保留这个DHCP客户端的配置信息,将IP地址列为曾经分配过的IP地址中,以便后续重新分配给该客户端或其他客户端

客户端可以通过发送DHCP INFORM报文向服务器请求更新配置信息。

四、DHCP的优缺点

优点

  • 提供安全而可靠的配置。DHCP避免了由于需要手动在每个计算机上键入值而引起的配置错误。DHCP还有助于防止由于在网络上配置新的计算机时重用以前指派的IP地址而引起的地址冲突。
  • 可以减少配置管理。使用DHCP服务器可以大大降低用于配置和重新配置网上计算机的时间。可以配置服务器以便在指派地址租约时提供其他配置值的全部范围。这些值是使用DHCP选项指派的。
  • DHCP租约续订过程还有助于确保客户端计算机配置需要经常更新的情况(如使用移动或便携式计算机频繁更改位置的用户),通过客户端计算机直接与DHCP服务器通讯可以高效、自动地进行这些更改。
  • IP地址采用租用方式,需要时向DHCP服务器申请IP,用完后释放,使IP地址可以再利用。
  • DHCP服务器数据库是一个动态数据库,向客户端提供租约或释放租约时会自动更新,降低了管理IP地址的难度,所有DHCP客户的设置和变更都由客户端和服务器自动完成,不需人工干涉。

缺点:

  • DHCP不能发现网络上非DHCP客户端已经在使用的IP地址。
  • DHCP服务器对于用户的接入没有限制,任何一台电脑只要连接到网络上,就能够通过DHCP服务器获得正确的网络配置,从而访问网络。这样使得非法的用户很容易进入内部网络,带来安全隐患。
  • When there are multiple DHCP servers on the network, especially when there are private fake DHCP servers, one DHCP server cannot find out the IP addresses that have been leased by other servers, which will cause confusion to the network.
  • The DHCP server cannot communicate with clients across routers unless the router allows BOOTP forwarding.

Guess you like

Origin blog.csdn.net/qq_40741808/article/details/106176412