1、DHCP分配地址过程在与读者的交流中,发现许多读者对DHCP服务的工作原理不是很清楚,如为什么DHCP服务报文不能穿越网关,在网络中有多台DHCP服务器时客户端到底该如何选择?各阶段的DHCP报文格式有哪些主要变化?DHCP客户端与DHCP服务器又是如何识别并建立通信的等。在此本文给出了具体介绍。11.4.4 DHCP服务IP地址自动分配原理 DHCP在提供服务时,DHCP客户端是以UDP 68号端口进行数据传输的,而DHCP服务器是以UDP 67号端口进行数据传输的。DHCP服务不仅体现在为DHCP客户端提供IP地址自动分配过程中,还体现在后面的IP地址续约和释放过程中。本节仅介绍DHCP客
2、户端初次分配IP地址的过程。 在整个DHCP服务器为DHCP客户端初次提供IP地址自动分配过程中,一共经过了以下四个阶段,利用了表11-10中的前四个报文:发现阶段(DHCP客户端在网络中广播发送DHCP DISCOVER请求报文,发现DHCP服务器,请求IP地址租约)、提供阶段(DHCP服务器通过DHCP OFFER报文向DHCP客户端提供IP地址预分配)、选择阶段(DHCP客户端通过DHCP REQUEST报文确认选择第一个DHCP服务器为它提供IP地址自动分配服务)和确认阶段(被选择的DHCP服务器通过DHCP ACK报文把在DHCP OFFER报文中准备的IP地址租约给对应DHCP客户
3、端)。在DHCP客户端在获得了一个IP地址以后,就可以发送一个免费ARP请求探测网络中是否还有其它主机使用IP地址,来避免由于DHCP服务器地址池重叠而引发的IP冲突。以上四个阶段如图11-19所示,相当于DHCP客户端与DHCP服务器的四次握手过程。具体描述如下:图11-19 DHCP客户端从DHCP服务器获取IP地址的四个阶段 (1)发现阶段:即DHCP客户端获取网络中DHCP服务器信息的阶段。在客户端配置了DHCP客户端程序(如在Windows系统中进行了如图11-16所示的配置)并启动后,以广播方式发送DHCP DISCOVER报文寻找网络中的DHCP服务器。示例如下。 此广播报文采用
4、传输层的UDP 68号端口发送(封装的目的端口为UDP 68号端口),经过网络层IP协议封装后,源IP地址为0.0.0.0(因为此时还没有分配IP地址),目的IP地址为255.255.255.255(有限广播IP地址)。如下是一个DHCP DISCOVER报文封装的IP报头示例,可以看到Destination Address(目的地址)是255.255.255.255,而Source Address(源地址)是0.0.0.0。IP报头中的其它字段含义大家可以参见本书第7章7.3.4节相关内容。IP:ID = 0x0; Proto = UDP;Len: 328IP:Version = 4 (0x
5、4)IP:Header Length = 20 (0x14)IP:Service Type = 0 (0x0)IP:Precedence = RoutineIP:.0. = Normal DelayIP:.0. = NormalThroughputIP:.0. = NormalReliabilityIP:Total Length = 328(0x148)IP:Identification = 0 (0x0)IP:Flags Summary = 0 (0x0)IP:.0 = Last fragmentin datagramIP:.0. = May fragmentdatagram if nece
6、ssaryIP:Fragment Offset = 0 (0x0)bytesIP:Time to Live = 128 (0x80)IP:Protocol = UDP - User Datagram !-使用UDP传输层协议IP:Checksum = 0x39A6IP:Source Address = 0.0.0.0 !-源IP地址为0.0.0.0IP:Destination Address = 255.255.255.255 !-目的IP地址为255.255.255.255IP:Data:Number of data bytesremaining = 308 (0x0134)【经验之谈】在以
7、上DHCP DISCOVER报文中IP报头中的目的地址(Destination Address)是255.255.255.255这个有限广播地址。这个有限广播地址就是代表任意一个IPv4子网的广播地址,当然是发送报文的主机所在的子网和DHCP服务器所在子网的广播地址,但因为此时DHCP客户端并不知道DHCP服务器所在的是哪个子网。下面所有其它DHCP报文中的255.255.255.255地址的含义也是一样的。 至于IP报头中的源地址(Source Address),由于当前DHCP客户端主机并未分配具体的IP地址,所以只能用具有任意代表功能的0.0.0.0地址来表示了。下面所有其它DHCP报文
8、中指定的0.0.0.0地址的含义也是一样的。 因为此时,DHCP客户端没有分配到IP地址,也不知道DHCP服务器或DHCP中继的IP地址,所以在DHCP DISCOVER报文中Ciaddr(客户端IP地址)、Yiaddr(被分配的DHCP客户端IP地址)、Siaddr(下一个为DHCP客户端分配IP地址的DHCP服务器地址)、Giaddr(DHCP中继IP地址)这四个字段均为0.0.0.0,如下所示。另外,从中可以看到,在CHADDR字段和DHCP选项中Client Identifier字段中都标识了DHCP客户端网卡MAC地址。DHCP:Discover (xid=21274A1D)DHCP
9、:Op Code (op) = 1 (0x1)DHCP:Hardware Type (htype)= 1 (0x1) 10Mb EthernetDHCP:Hardware Address Length(hlen) = 6 (0x6)DHCP:Hops (hops) = 0 (0x0)DHCP:Transaction ID (xid)= 556223005 (0x21274A1D)DHCP:Seconds (secs) = 0 (0x0)DHCP:Flags(flags) = 1 (0x1)!-标志位置1,代表以广播方式发送DHCP:1. = BroadcastDHCP:Client IP Ad
10、dress (ciaddr) = 0.0.0.0DHCP:Your IP Address (yiaddr) =0.0.0.0DHCP:Server IP Address (siaddr) = 0.0.0.0DHCP:Relay IP Address (giaddr) =0.0.0.0DHCP:Client Ethernet Address (chaddr) = 08002B2ED85EDHCP:Server Host Name (sname)= DHCP:Boot File Name (file)= DHCP:Magic Cookie = OKDHCP:Option Field (option
11、s)DHCP:DHCP Message Type = DHCP Discover !-DHCP报文类型为DHCP DiscoverDHCP:Client-identifier =(Type:1) 08 00 2b 2e d8 5eDHCP:Host Name = JUMBO-WS !-DHCP服务器主机名DHCP:Parameter Request List= (Length:7) 01 0f 03 2c 2e 2f 06DHCP:End of this optionfield (2)提供阶段:即DHCP服务器向DHCP客户端提供预分配IP地址的阶段。网络中的所有DHCP服务器接收到客户端的D
12、HCP DISCOVER报文后,都会根据自己地址池中IP地址分配的优先次序选出一个IP地址,然后与其它参数一起通过传输层的UDP 67号端口,在DHCP OFFER报文中以广播方式发送给客户端(目的端口是DHCP客户端的UDP 68号端口)。客户端通过封装在帧中的目的MAC地址(也就在DHCP DISCOVER报文中的CHADDR字段值)的比对来确定是否接收该帧。但这样一来,理论上DHCP客户端可能会收到多个DHCP OFFER报文(当网络中存在多个DHCP服务器时),但DHCP客户端只接受第一个到来的DHCP OFFER报文。 DHCP OFFER报文经过IP协议封装后的源IP地址DHCP服
13、务器自己的IP地址,目的地址仍是255.255.255.255广播地址,使用的协议仍为UDP。下面是一个DHCP OFFER报文的IP报头示例。IP:ID = 0x3C30; Proto = UDP;Len: 328IP:Version = 4 (0x4)IP:Header Length = 20 (0x14)IP:Service Type = 0 (0x0)IP:Precedence = RoutineIP:.0. = Normal DelayIP:.0. = NormalThroughputIP:.0. = NormalReliabilityIP:Total Length = 328(0x
14、148)IP:Identification = 15408(0x3C30)IP:Flags Summary = 0 (0x0)IP:.0 = Last fragmentin datagramIP:.0. = May fragmentdatagram if necessaryIP:Fragment Offset = 0 (0x0)bytesIP:Time to Live = 128 (0x80)IP:Protocol = UDP - User DatagramIP:Checksum = 0x2FA8IP:Source Address = 157.54.48.151IP:Destination A
15、ddress = 255.255.255.255IP:Data:Number of data bytesremaining = 308 (0x0134) 在DHCP OFFER报文中,Ciaddr字段值仍为0.0.0.0,因为客户端仍没有分配到IP地址;Yiaddr字段已有值了,这是DHCP服务器为该客户端预分配的IP地址;因为此时仍没有得到客户端确认,所以Siaddr字段值仍为0.0.0.0;因为没有经过DHCP中继服务器,所以Giaddr字段值仍为0.0.0.0。另外,在DHCP可选项部分,可以看到由服务器随IP地址一起发送的各种选项。在这种情况下,服务器发送的是子网掩码、默认网关(路由器
16、)、租约时间、WINS服务器地址(NetBIOS名称服务)和NetBIOS节点类型。下面是一个DHCP OFFER报文示例。DHCP:Offer (xid=21274A1D)DHCP:Op Code (op) = 2 (0x2)DHCP:Hardware Type (htype)= 1 (0x1) 10Mb EthernetDHCP:Hardware Address Length(hlen) = 6 (0x6)DHCP:Hops (hops) = 0 (0x0)DHCP:Transaction ID (xid)= 556223005 (0x21274A1D)DHCP:Seconds (secs
17、) = 0 (0x0)DHCP:Flags (flags) = 1 (0x1)DHCP:1. = BroadcastDHCP:Client IP Address (ciaddr) = 0.0.0.0DHCP:Your IP Address (yiaddr) =157.54.50.5DHCP:Server IP Address (siaddr) = 0.0.0.0DHCP:Relay IP Address (giaddr) =0.0.0.0DHCP:Client Ethernet Address(chaddr) = 08002B2ED85EDHCP:Server Host Name (sname
18、)= DHCP:Boot File Name (file)= DHCP:Magic Cookie = OKDHCP:Option Field (options)DHCP:DHCP Message Type = DHCPOffer!-DHCP报文类型为DHCP OfferDHCP:Subnet Mask =255.255.240.0!-所分配IP地址的子网掩码为255.255.240.0DHCP:Renewal Time Value (T1) = 8 Days,0:00:00!-想要继续租约原来分配的IP地址,则提出续约申请的期限为8天DHCP:Rebinding Time Value (T2)
19、 = 14 Days, 0:00:00!-如果上次申请续约失败,再次申请绑定原来分配到的IP地址的期限为14天DHCP:IP Address Lease Time = 16Days, 0:00:00!-租约期限为16天,也就是DHCP客户端可使用此IP地址的最长时间为16天DHCP:Server Identifier =157.54.48.151!-DHCP服务器的IP地址为157.54.48.151DHCP:Router =157.54.48.1!-默认网关IP地址为157.54.48.1DHCP:NetBIOS Name Service =157.54.16.154!-DNS服务器IP地址
20、为157.54.16.154DHCP:NetBIOS Node Type =(Length: 1) 04DHCP:End of this optionfield (3)选择阶段:即DHCP客户端选择IP地址的阶段。如果有多台DHCP服务器向该客户端发来DHCP OFFER报文,客户端只接受第一个收到的DHCP OFFER报文,然后以广播方式发送DHCP REQUEST报文。在该报文的“Requested Address”选项中包含DHCP服务器在DHCP OFFER报文中预分配的IP地址,对应的DHCP服务器IP地址等。这样也就相当于同时告诉得其他DHCP服务器,它们可以释放已提供的地址,并将
21、这些地址返回到可用地址池中。 在DHCP OFFER报文封装的IP协议头部中,客户端的SourceAddress仍然是0.0.0.0,数据包的Destination仍然是255.255.255.255。但在DHCP OFFER报文中Ciaddr、Yiaddr、Siaddr、Giaddr字段的地址均0.0.0.0,大家自己分析一下为什么,很简单的。下面是一个DHCP OFEER报文头部和DHCP OFFER报文示例。IP:ID = 0x100; Proto = UDP;Len: 328IP:Version = 4 (0x4)IP:Header Length = 20 (0x14)IP:Servi
22、ce Type = 0 (0x0)IP:Precedence = RoutineIP:.0. = Normal DelayIP:.0. = NormalThroughputIP:.0. = NormalReliabilityIP:Total Length = 328(0x148)IP:Identification = 256(0x100)IP:Flags Summary = 0 (0x0)IP:.0 = Last fragmentin datagramIP:.0. = May fragmentdatagram if necessaryIP:Fragment Offset = 0 (0x0)by
23、tesIP:Time to Live = 128 (0x80)IP:Protocol = UDP - UserDatagramIP:Checksum = 0x38A6IP:Source Address = 0.0.0.0IP:Destination Address = 255.255.255.255IP:Data:Number of data bytesremaining = 308 (0x0134)DHCP:Request (xid=21274A1D)DHCP:Op Code (op) = 1 (0x1)DHCP:Hardware Type (htype)= 1 (0x1) 10Mb Eth
24、ernetDHCP:Hardware Address Length(hlen) = 6 (0x6)DHCP:Hops (hops) = 0 (0x0)DHCP:Transaction ID (xid)= 556223005 (0x21274A1D)DHCP:Seconds (secs) = 0 (0x0)DHCP:Flags (flags) = 1 (0x1)DHCP:1. = BroadcastDHCP:Client IP Address (ciaddr) = 0.0.0.0DHCP:Your IP Address (yiaddr) =0.0.0.0DHCP:Server IP Addres
25、s (siaddr) = 0.0.0.0DHCP:Relay IP Address (giaddr) =0.0.0.0DHCP:Client Ethernet Address(chaddr) = 08002B2ED85EDHCP:Server Host Name (sname)= DHCP:Boot File Name (file)= DHCP:Magic Cookie = OKDHCP:Option Field (options)DHCP:DHCP Message Type = DHCP RequestDHCP:Client-identifier = (Type:1) 08 00 2b 2e
26、 d8 5eDHCP:Requested Address =157.54.50.5DHCP:Server Identifier =157.54.48.151DHCP:Host Name =JUMBO-WSDHCP:Parameter Request List= (Length:7) 01 0f 03 2c 2e 2f 06DHCP:End of this optionfield(4)确认阶段:即DHCP服务器确认分配级DHCP客户端IP地址的阶段。某个DHCP服务器在收到DHCP客户端发来的DHCP REQUEST报文后,只有DHCP客户端选择的服务器会进行如下操作:如果确认将地址分配给该客户
27、端,则以广播方式返回DHCP ACK报文;否则返回DHCP NAK报文,表明地址不能分配给该客户端。 在DHCP服务器发送的DHCP ACK报文的IP协议头部,SourceAddress是DHCP服务器IP地址,DestinationAddress仍然是广播地址255.255.255.255。在DHCP ACK报文中的Yiaddr字段包含要分配给客户端的IP地址,而Chaddr和DHCP:Client Identifier字段是发出请求的客户端中网卡的MAC地址。同时在选项部分也会在DHCP OFFER报文中把所分配的IP地址的子网掩码、默认网关、DNS服务器、租约期、续约时间等信息加上。IP
28、:ID = 0x3D30; Proto = UDP;Len: 328IP:Version = 4 (0x4)IP:Header Length = 20 (0x14)IP:Service Type = 0 (0x0)IP:Precedence = RoutineIP:.0. = Normal DelayIP:.0. = NormalThroughputIP:.0. = NormalReliabilityIP:Total Length = 328(0x148)IP:Identification = 15664(0x3D30)IP:Flags Summary = 0 (0x0)IP:.0 = Las
29、t fragmentin datagramIP:.0. = May fragmentdatagram if necessaryIP:Fragment Offset = 0 (0x0)bytesIP:Time to Live = 128 (0x80)IP:Protocol = UDP - UserDatagramIP:Checksum = 0x2EA8IP:Source Address = 157.54.48.151IP:Destination Address = 255.255.255.255IP:Data:Number of data bytesremaining = 308 (0x0134)DHCP:ACK (xid=21274A1D)DHCP:Op Code (op) = 2 (0x2)DHCP:Hardware Type (htype)= 1 (0x1) 10Mb EthernetDHCP:Hardware Address Length(hlen) = 6 (0x6)DHCP:Hops (hops) = 0 (0x0)DHCP:Transaction I
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1