【转】DHCP snooping 详解

本文最后一次被修改是在:2016 年 12 月 4 日 20:22:39

一、机制概述

DHCP 都非常熟悉了,对于 DHCP 客户端而言,初始过程中都是通过发送广播的 DHCP discovery 消息寻找 DHCP 服务器,然而这时候如果内网中存在私设的 DHCP 服务器,那么就会对网络造成影响,例如客户端通过私设的 DHCP 服务器拿到一个非法的地址,最终导致 PC 无法上网。

在 DHCP snooping 环境中(部署在交换机上),我们将端口视为 trust 或 untrust 两种安全级别,也就是信任或非信任接口。在交换机上,将连接合法 DHCP 服务器的接口配置为 trust 。只有 trust 接口上收到的来自 DHCPserver 的报文(如 DHCPOFFER、DHCPACK、DHCPNAK 或者 DHCPLEASEQUERY)才会被放行,相反,在 untrust 接口上收到的来自 DHCPserver 的报文将被过滤掉,这样一来就可以防止非法的 DHCPserver 接入。同时在部署了 DHCP Snooping 的交换机本地,还能维护一张 DHCP snooping 的绑定数据库(binding database),用于保存侦听到的 DHCP 交互的表项,信息包括(针对 untrust 接口的):MAC 地址、IP 地址(DHCP 分配的)、租期、绑定类型、VLAN 号、接口编号(DHCP 客户端也就是连接客户端 PC 的 untrust 接口)。这个 DHCP snooping binding database 除了可以做一些基本的安全接入控制,还能够用于 DAI 等防 ARP 欺骗的解决方案。

一台支持 DHCP snooping 的交换机,如果在其 untrust 接口上,收到来自下游交换机发送的、且带有 option 82 的 DHCP 报文,则默认的动作是丢弃这些报文。如果该交换机开启了 DHCP snooping 并且带有 option 82 的 DHCP 报文是在 trusted 接口上收到的,则交换机接收这些报文,但是不会根据报文中包含的相关信息建立 DHCP binding database 表项。

如果交换机确实通过一个 untrust 接口连接了下游交换机,并且希望放行该接口收到的、下游交换机发送出来的带有 option 82 的 DHCP 报文,则可使用全局命令:ip dhcp snooping information option allow-untrusted,同时由于是通过 untrust 接口收到的 DHCP 报文,因此会根据侦听的结果创建 DHCP snooping binding database 表项。

二、Option 82

DHCP option 82 又称为 DHCP 中继代理信息选项(Relay Agent Information Option),是 DHCP 报文中的一个选项,其编号为 82 。

Cisco注释:The DHCP option-82 feature is supported only when DHCP snooping is globally enabled and on the VLANs to which subscriber devices using this feature are assigned.

基本的交互过程如下(下面描述的过程与上面的配图无关):

  1. DHCP client 广播 DHCP request;
  2. 交换机收到 DHCP request,它在报文中插入 option 82 信息(当然,要你先做了相应的配置后),在插入的信息中,remote-ID suboption 是该交换机的 MAC 地址;circuit-ID suboption 是收到 DHCP request 报文的接口 ID;
  3. 如果交换机上还部署了 DHCP relay,那么交换机将中继地址也添加进 DHCP 报文中;
  4. 交换机将携带了 option 82 信息的 DHCP 报文转发给 DHCP 服务器;
  5. DHCP 服务器接收到了 DHCP 报文,如果这台服务器是能够识别 option 82 的,那么它将根据 option 82 中携带的信息,来为客户端分配 IP 地址,以及选择相应的策略。然后服务器回应一个 DHCP 包,其中也包含 option 82 信息;
  6. 交换机收到该 DHCP 报文后,发现报文的 option 82 信息正是从本地始发,交换机将 option 82 信息移除后,将 DHCP 报文转发给 DHCP client 。

在上述事件的发生过程中,下面字段的内容不会发生改变:

  • Circuit-ID suboption fields

–Suboption type
–Length of the suboption type
–Circuit-ID type
–Length of the circuit-ID type

  • Remote-ID suboption fields

–Suboption type
–Length of the suboption type
–Remote-ID type
–Length of the remote-ID type

In the port field of the circuit-ID suboption, the port numbers start at 3. 也就是说这个字段中,端口号是从 3 开始的,第一个端口号就是 3,依次类推。例如,一个带了 SFP 插槽的 24 口交换机,port 3 是 Fast x/0/1,port 4 是 Fast x/0/2,以此类推,port 27 就是 SFQ 模块的 x/0/1 口。其中 x 为堆叠成员编号。

Option 报文结构:

DHCP option 82 又称为 DHCP 中继代理信息选项(Relay Agent Information Option),是 DHCP 报文中的一个选项,其编号为 82 。RFC3046 定义了 option 82,选项位置在 option 255 之前而在其它 option 之后。

CODE:82
LEN:Agent Information field 的字节数,不包含 code 及 len 字段的长度
Option 82 可包含多个 suboption,RFC3046 定义了如下两个:

Subopt:子选项编号,如果是 circuit ID 则值为 1,remote ID 则值为 2
Len:Sub-option Value 的字节个数,不包括 SubOpt 和 Len 字段的两个字节

option 82 子选项 1:即 Circuit ID,它表示接收到的 DHCP 请求报文来自的电路标识,这个标识只在中继代理节点内部有意义,在服务器端不可以解析其含义,只作为一个不具含义的标识使用。一般情况下,默认是接收到 DHCP 请求报文的接入交换机 VlanID 加接入二层端口名称,如 Vlan2 + Ethernet0/0/10,也可以由用户指定自己的代理电路 ID 。通常子选项 1 与子选项 2 要共同使用来标识 DHCP 客户端的信息。

option 82 子选项 2:即 Remote ID,一般情况下为插入该 option 82 信息的接入层交换机的 MAC 地址。

CISCO注释:If the DHCP relay agent is enabled but DHCP snooping is disabled, the DHCP option-82 data insertion feature is not supported.

三、DHCP Snooping Binding Database

在交换机上激活 DHCP snooping 后,交换机会使用在 untrust 接口上 “ 侦听到的 “ DHCP 交互内容形成一个表:dhcp snooping binding database 。如下:

SW1#show ip dhcp snooping binding
MacAddress         ipAddress      Lease(sec)  Type          VLAN  Interface
-----------------  ------------   ---------- -------------  ----  ---------------
00:B0:64:04:09:99  192.168.10.1     86025    dhcp-snooping    10  FastEthernet0/1
Total number of bindings: 1

默认情况下,这个数据库是动态的,也就是说当交换机重启,数据库内的表项全部丢失。这个事情的结果可能会造成网络的中断。因此我们可以将这个数据库以文件的形式存储起来,这个就是 DHCP snooping database agent 。

配置命令非常简单,全局命令:

SW2(config)#ip dhcp snooping database ?
  flash:       Database agent URL
  ftp:         Database agent URL
  http:        Database agent URL
  https:       Database agent URL
  rcp:         Database agent URL
  scp:         Database agent URL
  tftp:        Database agent URL
  timeout      Configure abort timeout interval
  write-delay  Configure delay timer for writes to URL

由于 flash 空间非常有限,因此建议将文件存储在 TFTP 服务器上。当部署在 TFTP 服务器上时,注意先创建一个空的文件以对应配置命令中的 URL,这个文件用于 dhcp snooping binding database 的写入(视具体设备而定)。

形成的文件的格式如下:

TYPE DHCP-SNOOPING
VERSION 1
BEGIN
 
 
...
...
 
END

下面是一个例子:

2bb4c2a1
TYPE DHCP-SNOOPING
VERSION 1
BEGIN
192.1.168.1 3 0003.47d8.c91f 2BB6488E interface-id 21ae5fbb
192.1.168.3 3 0003.44d6.c52f 2BB648EB interface-id 1bdb223f
192.1.168.2 3 0003.47d9.c8f1 2BB648AB interface-id 584a38f0
END

四、配置示例

实验 1:基础实验

  • PC 属于 VLAN10,网关在 SW2 上;DHCPserver 属于 VLAN100,网关在 SW2 上;
  • SW1 为接入层交换机,部署 DHCP snooping,将 Fa 0/24 定义为 trust 接口;
  • SW2 为核心层交换机,部署 DHCP relay(也就是 DHCP 中继,即 SW2 配置下的 VLAN 10 下的 ip helper-address 192.168.100.1 这条命令)。

DHCPserver 的配置如下:

 no ip routing
 ip default-gateway 192.168.100.254
 interface fast0/0
 ip address 192.168.100.1 255.255.255.0
 no shutdown
 exit
 
 service dhcp
 ip dhcp pool vlan10
 network 192.168.10.0 /24
 default-router 192.168.10.254

SW1 的配置如下:

 vlan 10
 !
 ip dhcp snooping // 全局开启 DHCP snooping
  ip dhcp snooping vlan 10 // 激活 vlan 10 的 DHCP snooping
  no ip dhcp snooping information option // 不写入 option 82
 !
 interface fast0/1
 switchport access vlan 10
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 ip dhcp snooping trust

SW2 的配置如下:

 vlan 10
 vlan 100
 name Server
 !
 interface fast0/1
 switchport access vlan 100
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 interface vlan 10
 ip address 192.168.10.254 255.255.255.0
 ip helper-address 192.168.100.1
 interface vlan 100
 ip address 192.168.100.254 255.255.255.0

实验结果:

PC 获取到了地址,在 SW1 上查看 dhcp snooping binding database:

SW1#show ip dhcp snooping binding
MacAddress         ipAddress      Lease(sec)  Type          VLAN  Interface
-----------------  -------------  ----------  ----------    ----  ---------------
00:B0:64:04:09:99  192.168.10.1     86025     dhcp-snooping   10  FastEthernet0/1
Total number of bindings: 1

实验 2:接入交换机插入 option 82,核心交换机做中继

  • 在 SW1 上开启 DHCP snooping,同时不去 no ip dhcp snooping information option,也就是插入 option 82 。

其他设备配置与上一个实验相同,发现 PC 机无法获取地址,通过在网关设备上,也即是 SW2 上 debug 会发现报错:relay information option exists, but giaddr is zero 。giaddr 字段意思为该报文经过的第一个 DHCP 中继代理的 IP 地址。

在 SW2 上增加配置:

(全局配置模式)ip dhcp relay information trust-all

或:

interface vlan 10
ip dhcp relay information trusted

上述命令,将使交换机接受携带 option 82 信息,同时 giaddr 为 0 的 DHCP 报文,然后做中继。如此一来即可成功获取地址。

SW1 的配置如下:

 vlan 10
 !
 ip dhcp snooping
  ip dhcp snooping vlan 10
 interface fast0/1
 switchport access vlan 10
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 ip dhcp snooping trust

SW2 的配置如下:

 vlan 10
 vlan 100
 name Server
 !
 ip dhcp relay information trust-all
 interface fast0/1
 switchport access vlan 100
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 interface vlan 10
 ip address 192.168.10.254 255.255.255.0
 ip helper-address 192.168.100.1
 interface vlan 100
 ip address 192.168.100.254 255.255.255.0

实验 3:接入层交换机插入 option 82,上游核心交换机也开启 dhcp snooping

  • 接入层交换机 SW1 开启 dhcp snooping,插入 option 82;
  • 核心层交换机 SW2 也开启 dhcp snooping 。

核心层交换机 SW2 也开启 dhcp snooping,默认所有端口都是 untrust 的,这时如果 SW2 的 fast 0/24 口收到携带了 option 82 的 DHCP 报文,则丢弃。解决办法之一,是将 SW2 的 fast 0/24 口配置为 trust 接口,但是这么一来通过该 trust 接口收到的 DHCP 报文,是不会创建 dhcp snooping binding 表项的。另一个方法是,使用命令:ip dhcp snooping information option allow-untrusted 。这条命令放行携带了 option 82 的 DHCP 报文,同时会在本地创建 dhcp snooping binding 表项。

SW1 的配置如下:

 vlan 10
 !
 ip dhcp snooping
 ip dhcp snooping vlan 10
 interface fast0/1
 switchport access vlan 10
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 ip dhcp snooping trust

SW2 的配置如下:

 vlan 10
 vlan 100
 name Server
 !
 ip dhcp snooping information option allow-untrusted // 注意区分这两条命令
 ip dhcp relay information trust-all // 注意区分这两条命令(这条命令使交换机接受携带
                                        option 82 信息,同时 giaddr 为 0 的 DHCP
                                        报文,然后做中继。)
 interface fast0/1
 switchport access vlan 100
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 interface vlan 10
 ip address 192.168.10.254 255.255.255.0
 ip helper-address 192.168.100.1
 Interface vlan 100
 ip address 192.168.100.254 255.255.255.0

实验 4:无 DHCP 中继环境中,option 82 及相关问题


这个环境比较简单,PC 与 DHCP 服务器同处一个 VLAN,我主要想证实一下普通交换机对携带了 option 82 的 DHCP 包的处理情况。

  • SW1 仍然开启 dhcp snooping,同时插入 option 82;
  • SW2 将服务器划入与 PC 相同的 VLAN,相当于纯做透传。

SW1 的配置如下:

 vlan 10
 !
 ip dhcp snooping
 ip dhcp snooping vlan 10
 interface fast0/1
 switchport access vlan 10
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 ip dhcp snooping trust

SW2 的配置如下:

 vlan 10
 !
 interface fast0/1
 switchport access vlan 10
 interface fast0/24
 switchport trunk encapsulation dot1q
 switchport mode trunk
 interface vlan 10
 ip address 192.168.10.254 255.255.255.0

DHCP server 的配置就是常规配置,这里不再赘述。

实验的结果是,PC 无法获取地址。我们分析一下,SW1 将 PC 发出来的 DHCP 请求消息插入 option 82,随后发给 SW2 。通过 SW2 上的 debug 信息我们发现:

*Mar 16 17:09:21.858: DHCPD: inconsistent relay information.
*Mar 16 17:09:21.858: DHCPD: relay information option exists, but giaddr is zero.

SW2 意识到,DHCP 报文中有中继信息选项,也就是 option 82,但是 giaddr 是全 0 。SW2 虽然意识到了这个问题,但还是乖乖将这个数据交给了 DHCPserver,通过在 DHCPserver 上的 debug 信息:

*Mar 4 07:02:23.473: DHCPD: inconsistent relay information.
*Mar 4 07:02:23.473: DHCPD: relay information option exists, but giaddr is zero.

显然,DHCP 服务器也意识到了上面的问题,它采取的动作是,忽略这个 DHCP 请求。因此 PC 无法获取地址。

解决方法有两个,一个是让 SW1 不插入 opiton 82,另一个是在 DHCPserver 上使用如下配置:

(全局配置模式)ip dhcp relay information trust-all

或:

interface fast0/0
ip dhcp relay information trusted

思路和实验 2 一样。这样一来 SW2 会接受这些 DHCP 报文并进行处理,PC 就能够获取到地址了。

五、其他配置

ip dhcp snooping limit rate rate
注意,在交换机开启了 DHCP snooping 后,由于对 DHCP 报文都要进行窥探,因此当网络中存在 DHCP 攻击行为,将会严重消耗交换机的性能,因此,可以通过该命令限制接口上允许收到的 DHCP 报文数量。Configure the number of DHCP packets per second that an interface can receive. The range is 1 to 2048. By default, no rate limit is configured.

ip dhcp snooping verify mac-address
Configure the switch to verify that the source MAC address in a DHCP packet received on untrusted ports matches the client hardware address in the packet. The default is to verify that the source MAC address matches the client hardware address in the packet.

ip dhcp snooping binding mac-address vlan vlan-id ip-address interface interface-id expiry seconds
静态配置一条 dhcp snooping binding database 表项,该命令是在特权模式下配置。

 

红茶三杯
网络工程 | 项目管理 | IT服务管理 | CCIE培训
学习 沉淀 成长 分享
微博:http://weibo.com/vinsoney
博客:http://blog.sina.com.cn/vinsoney
站点:http://ccietea.com

这篇文章对你有帮助吗?

相关文章

1条评论

  1. 今天(2016年12月4日20:22:39)对本文进行了修改,主要修改的是一些格式的问题,还有一些错别字、英文单词拼写错误和标点符号的使用错误。

Ricky 发表评论 取消回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据