DHCP protocol detailed

 DHCP: Dynamic Host Configuration Protocol 
  TCP / IP protocol you want to run normal, then network hosts and routers inevitably need to configure some information (such as IP address of the interface, etc.). With this configuration host / router to provide / use a specific network service. 
  Elements are necessary host information: IP address, subnet mask, DNS server IP address of the 
  TCP / IP host configuration protocol information There are three main methods: 
1. Manually configured 
2. dynamically acquired 
3. According to a particular computing algorithm. 
  In the network, we host divided into two categories, server host and client host, 
+ Server Host: generally use manual configuration 
+ client host: general use of dynamic access 
  servers generally use manual configuration, and the client is generally obtained dynamically. Based on the following reasons: 
1. the client host mobile service more frequently than the host 
2. Host service needs to provide more reliable service, its configuration information should be to reduce dependence on other systems / hosts 
3. Customer host to little more than the number of the service host many. 
4. Knowledge Network Configuration client host user is lower than the user of the service host

  Bootp DHCP protocol has evolved. Bootp protocol provides limited information on the configuration of the host, and the host information has been configured it can not be modified. DHCP provides almost all of the configuration information of the host, and introduced the concept of lease, etc., so that the host can dynamically change the information. DHCP is backward compatible Bootp. 
  DHCP is based on UDP / IP transport. DHCP server uses port number 67, DHCP client uses the port number 68.

DHCP agreement

DHCP is divided into two parts: the configuration information and management transfer address 
+ address management: management processing address dynamically assigned IP 'address is provided to the client address lease 
+ transfer configuration information: DHCP message format comprising the state machine

DHCP address management

Address pool and address lease

  In the dynamic assignment of IP addresses, DHCP clients want DHCP server sends an IP address request. The DHCP server maintains a pool of IP addresses, DHCP taken from a response to the DHCP client IP address from the pool. When address assignment, DHCP server will specify the response of the lease to the DHCP client's IP address, the address is available only period in the lease, but the DHCP client requested an extension of the lease term lease (lease renewal period).

DHCP packets

Write pictures described here 
Op: packet type, divided into two categories: Request (1) and the Reply (2) 
HW the Type: type of hardware, general Ethernet:. 1 
HW Len: Hardware address length in bytes. The corresponding Ethernet: 6 (mac address length is 6 bytes 48bit) 
Transaction ID: Transaction ID, a random number generated with a client, the server Reply, Request will be copied to the Transaction Reply message. 
Secs: from the first launch Renew IP requests or requests in the past number of seconds 
Flags: flag, there are currently only use the first bit set to 1 indicating broadcast 
Client IP Address: IP address of the current client, if the client does not have current IP address is set to 0 
Your IP the address: the client wants the server to provide an IP address, the IP address will be entered in this field 
(Next) server IP address: the client IP address required boot another server 
Gateway (Relay) IP address: gateway (relay) IP address of a DHCP relay forwards DHCP packets to fill in when 
Server name: Server name, there is 64bytes, generally do not use, filled with 0 
the Boot File name: path of the boot file, 128bytes generally not used, filled with 0 
option-: option indefinite length. DHCP message more important field, there will be more later in detail.

DHCP Option

  DHCP expand from Bootp come, DHCP messages have also evolved Bootp packets. But on top Bootp DHCP adds many features, its messages also need to have some expansion. If Bootp message content can not be met, there is in the DHCP packet in the form of Option. 
  DHCP Option agreement is actually carried by many of the Bootp 
  DHCP There are many types of Option, vary in length (but are integers bytes). Option follow the format 
+ Option If no value, only the contents of the flag bit or the like, places a byte 
+ Opiton if there is a value, i.e. Opiton the following name-value pairs, then Opiton require multiple bytes, where the first byte indicates the name option, the second byte indicates the length value, and the third byte value represented.

  Supports a large number of DHCP Option (Bootp also supported portion thereof), the following are some common

Option name description
0 Pad Padding bits
1 Subnet Mask Subnet Mask
3 Router Address Router address
6 DNS DNS server
15 DN domain name
50 Requested IP Address IP address request
51 Address Lease Time Address Lease Time
53 DHCP Message Type DHCP message type, such as Discover, Request, Offer, ACK, etc.
54 Server Identifier Server identity
55 Parameter Request List Parameter request list
56 DHCP Error Message DHCP Error Messages
58 Lease Renewal Time Lease renewal time
59 Lease Rebinding Time Lease re-set time
61 Client Identifier Customer Identification
119 Domain Search List Find a list of domain names
255 End End

上面是常用的Option,DHCP报文与Option 255标识报文的结束。 
在DHCP Option中,我们着重看下DHCP Message Type(53),DHCP Message Type标识DHCP消息类型,主要有一下类型

DHCP 消息类型 对应的Option值

DHCP 协议 操作流程

1. 新的client加入网络时,会使用0.0.0.0作为源地址,发送discover广播报文,查询网络上有哪些DHCP server,以及这些DHCP server 能Offer哪些IP地址 
2. DHCP服务器接收到DHCP Discover报文后,回应Offer报文,提供IP地址(可能包含DNS等其他信息)给client 
3. client 根据收到的Offer报文,选择一个DHCP server,并选择它提供的IP地址。然后广播Request报文,想DHCP Server请求该IP地址,同时想本地网络(尤其是其他DHCP Server)公告自己已经选择了某个DHCP Server的某个IP地址。 
4. DHCP Server 回应ACK报文,将IP地址分配给Client端 (特殊情况:DHCP Server在发送Offer报文和接收到Request的短暂时间内把IP分配给了其他主机) 
5. DHCP Client 收到ACK报文后,会针对获得的IP地址发送ARP Request,进行IP地址冲突检测。 
6. 如果IP地址已经被其他主机使用,则Client放弃该IP地址,想Server发送DHCP DECLINE报文告诉Server该地址不能使用。然后一段时间后(一般10s)再此尝试获取该IP地址 
7. 如果Client仍然无法使用该IP地址,则发送DHCP RELEASE报文,放弃该地址。

Host has an IP address, just update the lease 
1. At this point you can skip DHCP Discover message and DHCP Offer packet 
2. Client sends the IP address of the current carrying Request packet 
3. If Server Client agree to renew, then sends a DHCP ACK packet. If the refusal to renew, then send DHCPNAK message.

Guess you like

Origin www.cnblogs.com/hjw459/p/11937612.html