1.DHCP概述
在IP网络环境中,设备间要通信或访问网络,必须获得IP地址。IP地址有2种设置方式:静态配置和动态获取。
-
静态配置,需要手工配置IP地址,子网掩码,默认网关,DNS等信息;
-
动态获取,则由DHCP服务器自动分配上诉信息。
动态获取使用的协议是DHCP。有两个优点:
-
对使用者来说,无需关心所在的网络细节,接入网线(Wifi)即可访问网络;
-
对网络管理者来说,可以简化网络配置和运维。
2. DHCP RFC
Abstract
The Dynamic Host Configuration Protocol (DHCP) provides a framework for passing configuration information to hosts on a TCPIP network. DHCP is based on the Bootstrap Protocol (BOOTP) [7], adding the capability of automatic allocation of reusable network addresses and additional configuration options [19]. DHCP captures the behavior of BOOTP relay agents [7, 21], and DHCP participants can interoperate with BOOTP participants [9].
Introduction
The Dynamic Host Configuration Protocol (DHCP) provides configuration parameters to Internet hosts. DHCP consists of two components: a protocol for delivering host-specific configuration parameters from a DHCP server to a host and a mechanism for allocation of network addresses to hosts.
DHCP is built on a client-server model, where designated DHCP server hosts allocate network addresses and deliver configuration parameters to dynamically configured hosts. Throughout the remainder of this document, the term "server" refers to a host providing initialization parameters through DHCP, and the term "client" refers to a host requesting initialization parameters from a DHCP server.
3. DHCP总体流程
image-20200711082907928.png1. DHCPDISCOVER
Client在首次启动时,需要向网络获取IP地址,会发送一个广播消息(目的IP地址为一个L3的广播地址:255.255.255.255, 目的MAC为一个L2的广播地址:FF-FF-FF-FF-FF-FF, 源IP地址为0.0.0.0, 源MAC地址为客户端实际的MAC地址)。
2. DHCPOFFER
DHCP服务器收到discover消息后,从服务器的IP地址池里挑选一个没有被分配的IP地址,通过offer消息发给客户端。offer消息在L3是一个广播消息(目的地址为255.255.255.255),但在L2是一个单播消息(目的MAC地址为client的MAC地址)。
3. DHCPREQUEST
客户端收到Offer消息后
-
如果接受该IP地址,就回复request消息;
-
如果发现该IP地址已被使用,就回复decline消息
客户端如何发现该IP地址已被使用?
- 客户端通过广播ARP请求来判断该IP地址是否被使用,如果没有设备回复ARP请求,则证明该IP地址可以使用,不会冲突。
什么场景会出现IP地址冲突?
-
其他设备手工配置的静态IP地址,和DHCP服务器分配的地址一样
-
DHCP服务器地址池不够用。(TBD,不够用的时候offer消息都不应该下发,待构造环境模拟验证)
4. DHCPACK
服务器收到客户端的request消息后,就正式下发ack消息,分配offer消息中的IP地址给客户端。严格来说,是租用IP地址给客户端,否则特别是在公共环境中,如果没有租期的概念,DHCP服务器的IP地址池很快会被耗尽。通过租期设置,可以让地址池中的IP循环轮流给不同的用户使用。
4. DHCP 包结构
20151022153738889.jpeg
FIELD BYTES NAME DESCRIPTION op 1 OpCode Identifies the packet as an request or reply: 1=BOOTREQUEST, 2=BOOTREPLY htype 1 Hardware Type Specifies the network hardware address type. hlen 1 Hardware Length Specifies the length hardware address length. hops 1 Hops The client sets the value to zero and the value increments if the request is forwarded across a router. xid 4 Transaction ID A random number that is chosen by the client. All DHCP messages exchanged for a given DHCP transaction use the ID (xid). secs 2 Seconds Specifies number of seconds since the DHCP process started. flags 2 Flags Indicates whether the message will be broadcast or unicast. ciaddr 4 Client IP address Only used when client knows its IP address as in the case of the Bound, Renew, or Rebinding states. yiaddr 4 Your IP address If the client IP address is 0.0.0.0, the DHCP server will place the offered client IP address in this field. siaddr 4 Server IP address If the client knows the IP address of the DHCP server, this field will be populated with the DHCP server address. Otherwise, it is used in DHCPOFFER and DHCPACK from DHCP server. giaddr 4 Router IP address (GI ADDR) The Gateway IP address, filled in by the DHCP/BootP Relay Agent. chaddr 16 Client MAC address The DHCP client MAC address. sname 64 Server name The optional server host name. file 128 Boot file name The boot file name. options variable Option parameters The optional parameters that can be provided by the DHCP server. RFC 2132 gives all possible options. 5. DHCP配置
5.1 Cisco配置(PacketTrace)
5.1.1 同一网段(同一VLAN)
image-20200711174919804.png
// 进入特权模式和全局配置模式
Router>en
Router#configure terminal
// 配置路由器接口IP地址,并启用对应接口
Router(config)#interface GigabitEthernet0/0
Router(config-if)#ip address 192.168.1.1 255.255.255.0
Router(config-if)#no shutdown
Router(config-if)#
// 配置DHCP地址池,网段,默认网关
Router(config)#ip dhcp pool zone1
Router(dhcp-config)#network 192.168.1.0 255.255.255.0
Router(dhcp-config)#default-router 192.168.1.1
Router(dhcp-config)#exit
Router(config)#
Router(config)#exit
Router#
// 保存当前配置
Router#copy running-config startup-config
PC上配置动态获取IP地址,即可获得IP:
image-20200711175039323.png
网友评论