ILD

DHCP协议学习
作者:Yuan Jianpeng 邮箱:yuanjp@hust.edu.cn
发布时间:2021-8-21 站点:Inside Linux Development

DHCP消息的格式如下:

   0                   1                   2                   3
   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     op (1)    |   htype (1)   |   hlen (1)    |   hops (1)    |
   +---------------+---------------+---------------+---------------+
   |                            xid (4)                            |
   +-------------------------------+-------------------------------+
   |           secs (2)            |           flags (2)           |
   +-------------------------------+-------------------------------+
   |                          ciaddr  (4)                          |
   +---------------------------------------------------------------+
   |                          yiaddr  (4)                          |
   +---------------------------------------------------------------+
   |                          siaddr  (4)                          |
   +---------------------------------------------------------------+
   |                          giaddr  (4)                          |
   +---------------------------------------------------------------+
   |                                                               |
   |                          chaddr  (16)                         |
   |                                                               |
   |                                                               |
   +---------------------------------------------------------------+
   |                                                               |
   |                          sname   (64)                         |
   +---------------------------------------------------------------+
   |                                                               |
   |                          file    (128)                        |
   +---------------------------------------------------------------+
   |                                                               |
   |                          options (variable)                   |
   +---------------------------------------------------------------+


op    1字节,消息类型。1 = BOOTREQUEST,2=BOOTREPLY。dhcp的类型在option里面

htype   硬件地址类型

hlen    硬件地址长度

hops    client设置成0,relay agents可以使用。

xid        随机数,transcation id

secs        client设置,开始请求到renew的秒数

ciaddr    client ip address

yiaddr    your(client)ip address

siaddr        ip address of next server to use in bootstrap

giaddr        relay agent ip address

chaddr    client hardware address

sname     optional server host name

file        boot file name


options的前4个字节为magic cookie,它们为99, 130, 83, 99;和RFC 1497定义的magic cookie相同。options剩余的field由tagged parameters列表组成,被称作选项。RC1497规定的vendor extensions全是DHCP选项,RFC1533给出DHCP使用的所有选项。


0和255是固定长度option,只有一个字节,0表示pad option,255表示end option,其他option的格式

1个字节的code + 1个字节的len + len个字节的data


9.6. DHCP Message Type

   This option is used to convey the type of the DHCP message.  The code
   for this option is 53, and its length is 1.  Legal values for this
   option are:

           Value   Message Type
           -----   ------------
             1     DHCPDISCOVER
             2     DHCPOFFER
             3     DHCPREQUEST
             4     DHCPDECLINE
             5     DHCPACK
             6     DHCPNAK
             7     DHCPRELEASE
             8     DHCPINFORM

    Code   Len  Type
   +-----+-----+-----+
   |  53 |  1  | 1-9 |


server identifier

这个表示是dhcp server的地址

    Code   Len            Address
   +-----+-----+-----+-----+-----+-----+
   |  54 |  4  |  a1 |  a2 |  a3 |  a4 |
   +-----+-----+-----+-----+-----+-----+


routers

表示子网内的路由器的地址(网关)

    Code   Len         Address 1               Address 2
   +-----+-----+-----+-----+-----+-----+-----+-----+--
   |  3  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   +-----+-----+-----+-----+-----+-----+-----+-----+--


subnet mask

    Code   Len        Subnet Mask
   +-----+-----+-----+-----+-----+-----+
   |  1  |  4  |  m1 |  m2 |  m3 |  m4 |
   +-----+-----+-----+-----+-----+-----+


domain name server

    Code   Len         Address 1               Address 2
   +-----+-----+-----+-----+-----+-----+-----+-----+--
   |  6  |  n  |  a1 |  a2 |  a3 |  a4 |  a1 |  a2 |  ...
   +-----+-----+-----+-----+-----+-----+-----+-----+--


V-I Vendor Class option

用来定义厂商

                        1 1 1 1 1 1
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  option-code  |  option-len   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |      enterprise-number1       |
   |                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   data-len1   |               |
   +-+-+-+-+-+-+-+-+               |
   /      vendor-class-data1       /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ----
   |      enterprise-number2       |   ^
   |                               |   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |
   |   data-len2   |               | optional
   +-+-+-+-+-+-+-+-+               |   |
   /      vendor-class-data2       /   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |
   ~            ...                ~   V
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ----


V-I Vendor-specific Information option
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  option-code  |  option-len   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |      enterprise-number1       |
   |                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |   data-len1   |               |
   +-+-+-+-+-+-+-+-+ option-data1  |
   /                               /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ----
   |      enterprise-number2       |   ^
   |                               |   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |
   |   data-len2   |               | optional
   +-+-+-+-+-+-+-+-+ option-data2  |   |
   /                               /   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+   |
   ~            ...                ~   V
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ----

   option-code         OPTION_V-I_VENDOR_OPTS (125)

   option-len          total length of all following option data in
                       octets

   enterprise-numberN  The vendor's registered 32-bit Enterprise Number
                       as registered with IANA [3]

   data-lenN           Length of option-data field

   option-dataN        Vendor-specific options, described below


每个DHCP消息都必须有一个特定的选项 - DHCP message type


客户端服务器交互 - 分配一个网络地址

  1. 客户端广播一个DHCPDISCOVER消息到本地子网,客户端可以包含建议的网络地址和过期时间值。

  2. 每个服务器响应一个DHCPOFFER消息,yiaddr包含一个可用的网络地址

  3. 客户端可能收到多个DHCPOFFER消息,客户端可以选择等待多个响应。客户端选择一个服务器,广播一个DHCPREQUEST消息,server identifier选项必须为选择的服务器的标识。也可以包含其他想要的配置选项,但是requested IP address必须为DHCPOFFER中的yiaddr提供的值。DHCPREQUEST中的secs域必须使用DHCPDISCOVER中相同的值。当客户端超时没有收到DHCPOFFER,客户端重传DHCPDISCOVER消息。

  4. 服务器接收DHCPREQUEST广播消息,这些没被选中的server,接收到该消息后,被告知客户端婉拒了该服务器的OFFER。被选中的服务器收到消息后,响应一个DHCPACK消息,该消息包含分配给客户端的配置参数。client identifier和chaddr的组合标识了一个客户端。如果选中的客户端不能满足DHCPREQUEST,它将返回一个DHCPNAK消息。

  5. 客户端收到DHCPACK消息后,执行一些检查操作,完成网络配置。如果客户端收到DHCPNAK消息,客户端重启配置过程。客户端重传DHCPREQUEST,如果超时没有收到DHCPACK或DHCPNAK。

  6. 客户端可以选择放弃租约,通过发送一个DHCPRELEASE消息。


客户端服务器交互 - 重用之前分配的网络地址

如果客户端记住并且希望重用之前分配的网络地址,客户端可以选择去掉一些步骤:

  1. 客户端广播DHCPREQUEST消息,并将网络地址包含在requested IP address选项中。

  2. 服务端返回DHCPACK消息,如果客户端的请求是非法的,服务端返回一个DHCPNAK消息。如果服务器不能保证信息是正确的,则服务器不要返回DHCPNAK消息。


后续消息类似。


续约

时间单位为秒,0xffffffff表示无限时间。


从外部获取网络地址

如果客户端通过其它方式获得网络地址,如手动配置,可以通过DHCPINFORM请求消息来获得其它配置参数。


Client parameters in DHCP

客户端可以通知服务端它感兴趣的配置参数,通过parameter request list选项。此外,客户端也可以建议网络地址和租约期限。


When clients should use DHCP

当网络参数变化时,应当使用DHCP获取或验证IP地址,如重启,断线。



相关标准

RFC 951   BOOTSTRAP PROTOCOL (BOOTP)

RFC 1542 Clarifications and Extensions for the Bootstrap Protocol

RFC 2131 Dynamic Host Configuration Protocol

https://datatracker.ietf.org/doc/html/rfc2131


DHCP Options and BOOTP Vendor Extensions

https://datatracker.ietf.org/doc/html/rfc2132


Vendor-Identifying Vendor Options for
        Dynamic Host Configuration Protocol version 4 (DHCPv4)

https://www.rfc-editor.org/rfc/rfc3925.html


 

Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.