DHCP在工作过程中涉及到的报文种类及其作用如下:
1、DHCP DISCOVER:客户端开始DHCP过程的第一个报文,是请求IP地址和其它配置参数的广播报文。
2、DHCP OFFER:服务器对DHCP DISCOVER报文的响应,是包含有效IP地址及配置的单播(或广播)报文。
3、DHCP REQUEST:客户端对DHCP OFFER报文的响应,表示接受相关配置。客户端续延IP地址租期时也会发出该报文。
4、DHCP ACK :服务器对客户端的DHCP REQUEST报文的确认响应报文。客户端收到此报文后,才真正获得了IP地址和相关的配置信息。
通过实验抓包加深印象:(dhcp 中继实验)
配置DHCP server
interface GigabitEthernet1/0/1
port link-mode route
combo enable fiber
ip address 10.10.10.1 255.255.255.252
dhcp server ip-pool 123
gateway-list 180.16.10.254
network 180.16.10.0 mask 255.255.255.0
dhcp enable
ip route-static 180.16.10.0 24 10.10.10.2
交换机配置
dhcp enable
interface Vlan-interface10
ip address 180.16.10.254 255.255.255.0
dhcp select relay
dhcp relay server-address 10.10.10.1
interface GigabitEthernet1/0/1
port link-mode route
combo enable fiber
ip address 10.10.10.2 255.255.255.252
interface GigabitEthernet1/0/3
port link-mode bridge
port access vlan 10
combo enable fiber
stp edged-port
配置完成后PC通过dhcp服务器获取到ip地址:
PC mac地址信息:
通过在交换机上抓取dhcp报文信息,得到如下结果:
// 发现阶段======================以广播的形式向局域网内请求dhcp服务器,此时客户端使用udp的68号端口发起请求
*Aug 3 10:29:03:454 2023 SW DHCPR/7/PACKET:
From 0.0.0.0 port 68, interface Vlan-interface10
Message type: REQUEST (1)
Hardware type: 1, Hardware address length: 6
Hops: 0, Transaction ID: 340012688
Seconds: 0, Broadcast flag: 1
Client IP address: 0.0.0.0 Your IP address: 0.0.0.0
Server IP address: 0.0.0.0 Relay agent IP address: 0.0.0.0
Client hardware address: 124e-cdfc-0406
Server host name: not configured
Boot file name: not configured
DHCP message type: DHCPDISCOVER (1)
//提供阶段======================局域网内的dhcp服务器响应客户端的请求(网络内如有多台dhcp服务的话均会发起响应),此时服务器通过UDP67号端口回应客户端
*Aug 3 10:29:04:043 2023 SW DHCPR/7/PACKET:
From 10.10.10.1 port 67, interface Vlan-interface10
Message type: REPLY (2)
Hardware type: 1, Hardware address length: 6
Hops: 1, Transaction ID: 340012688
Seconds: 0, Broadcast flag: 1
Client IP address: 0.0.0.0 Your IP address: 180.16.10.1
Server IP address: 0.0.0.0 Relay agent IP address: 180.16.10.254
Client hardware address: 124e-cdfc-0406
Server host name: not configured
Boot file name: not configured
DHCP message type: DHCPOFFER (2)
//选择阶段======================客户端收到dhcp服务器的回应报文后(多台服务器响应只选择最先收到的),向服务器确认分配的地址可以使用
*Aug 3 10:29:04:051 2023 SW DHCPR/7/PACKET:
From 0.0.0.0 port 68, interface Vlan-interface10
Message type: REQUEST (1)
Hardware type: 1, Hardware address length: 6
Hops: 0, Transaction ID: 340012688
Seconds: 0, Broadcast flag: 1
Client IP address: 0.0.0.0 Your IP address: 0.0.0.0
Server IP address: 0.0.0.0 Relay agent IP address: 0.0.0.0
Client hardware address: 124e-cdfc-0406
Server host name: not configured
Boot file name: not configured
DHCP message type: DHCPREQUEST (3)
//确认阶段======================服务器收到客户端的ip地址确认报文后,服务器先会查询此地址的可用性,如可用则回复ack确认报文,完成dhcp流程
*Aug 3 10:29:04:058 2023 SW DHCPR/7/PACKET:
From 10.10.10.1 port 67, interface Vlan-interface10
Message type: REPLY (2)
Hardware type: 1, Hardware address length: 6
Hops: 1, Transaction ID: 340012688
Seconds: 0, Broadcast flag: 1
Client IP address: 0.0.0.0 Your IP address: 180.16.10.1
Server IP address: 0.0.0.0 Relay agent IP address: 180.16.10.254
Client hardware address: 124e-cdfc-0406
Server host name: not configured
Boot file name: not configured
DHCP message type: DHCPACK (5)
//发现阶段======================携带的内容有:(向局域网内请求dhcp服务器)
1、请求的源目端口号
2、发起请求的方式(广播)
3、请求客户端的mac地址
//提供阶段======================携带的内容有:(dhcp服务器响应请求,并提供IP地址信息)
1、服务器响应分配的IP地址
2、如是中继场景则会携带dhcp中继ip
3、客户端的mac地址
4、真实dhcp服务器地址
5、分配给客户端的租期时间
5、ip地址网关
6、分配的dns信息
//选择阶段======================携带的内容有:(收到服务器的回应后,使用服务器分配的地址向服务器确认可用性)
1、客户端mac地址
2、真实服务器地址
//确认阶段======================携带的内容有:(收到客户端的请求确认报文,服务器会去确认地址可用性,如可用回复ACK即完成dhcp请求过程)
1、服务器响应分配的IP地址
2、如是中继场景则会携带dhcp中继ip
3、客户端的mac地址
4、真实dhcp服务器地址
5、分配给客户端的租期时间
5、ip地址网关
6、分配的dns信息
PS补充:
当PC端触发dhcp续约租期时是通过 dhcp request报文进行向服务器再次申请租期,触发向服务器申请租期的情况为:
1、在租期时间的50%会触发
2、在租期时间的87.5%会触发
3、当租期时间100%时会要求放弃此地址,重新申请