雏鹰部落

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3976|回复: 3

[讨论/求助] DHCP snooping详解

[复制链接]
发表于 2013-6-15 17:41:44 | 显示全部楼层 |阅读模式
一、机制概述

    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了交换机本地,还能维护一张DHCPsnooping的绑定数据库(binding database),用于保存侦听到的DHCP交互的表项,信息包括(针对untrust接口的):MAC地址、IP地址(DHCP分配的)、租期、绑定类型、VLAN号、接口编号(DHCP客户端也就是连接客户端PC的untrust接口)。这个DHCP snooping banding databse除了可以做一些基本的安全接入控制,还能够用于DAI等防ARP欺骗的解决方案。
    一台支持DHCP snooping的交换机,如果在其untrust接口上,收到来自下游交换机发送的、且带有option82的DHCP报文,则默认的动作是丢弃这些报文。如果该交换机开启了DHCP snooping并且带有option82的DHCP报文是在trusted接口上收到的,则交换机接收这些报文,但是不会根据报文中包含的相关信息建立DHCP bingding databse表项。





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

二、Option82


    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.




基本的交互过程如下(下面描述的过程与上面的配图无关):
  • DHCP client广播DHCP request
  • 交换机收到DHCP request,它在报文中插入option82信息(当然,要你先做了相应的配置后),在插入的信息中,remote-ID suboption是该交换机的MAC;circuit-ID suboption是收到DHCP request报文的接口ID。
  • 如果交换机上还部署了DHCP relay,那么交换机将中继地址也添加进DHCP报文中
  • 交换机将携带了option82信息的DHCP报文转发给DHCP服务器
  • DHCP服务器接收到了DHCP报文,如果这台服务器是能够识别option82的,那么它将根据option82中携带的信息,来为客户端分配IP地址,以及选择相应的策略。然后服务器回应一个DHCP包,其中也包含option82信息。
  • 交换机收到该DHCP报文后,发现报文的option82信息正是从本地始发,交换机将option82信息移除后,将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口交换机,port3是fast x/0/1,port4是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字段的长度
Option82可包含多个suboption,rfc3046定义了如下两个:




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

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

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

CISCO注释:If the DHCP relay agent is enabled but DHCP snooping is disabled, the DHCP option-82 data **ion 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 bingding 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.实验1:基础实验
PC属于VLAN10,网关在SW2上。DHCPserver属于VLAN100,网关在SW2上。
SW1为接入层交换机,部署DHCP snooping,将Fa0/24定义为trust接口。
SW2为核心层交换机,部署DHCP relay





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                  !! vlan10激活DHCP snooping
no ip dhcp snooping information option    !! 不写入option82
!
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 bingding databsae:
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.实验2:接入交换机插入option82,核心交换机做中继
在SW1上开启DHCP snooping,同时不去no ip dhcp snooping information option,也就是插入option82。
其他设备配置与上一个实验相同,发现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
上述命令,将使交换机接受携带option82信息,同时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.实验3:接入层交换机插入option82,上游核心交换机也开启dhcp snooping
接入层交换机SW1开启dhcp snooping,插入option82;
核心层交换机SW2也开启dhcp snooping,默认所有端口都是untrust的,这时如果SW2的fast0/24口收到携带了option82的dhcp报文,则丢弃。解决办法之一,是将SW2的fast0/24口配置为trust接口,但是这么一来通过该trust接口收到的dhcp报文,是不会创建dhcp snooping binding表项的。另一个方法是,使用命令:ip dhcp snooping information option allow-untrusted。这条命令放行携带了option82的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
                                         !!注意区分上述两条命令
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中继环境中,option82及相关问题





这个环境比较简单,PC与DHCP服务器同处一个VLAN,我主要想证实一下普通交换机对携带了option82的DHCP包的处理情况。
SW1仍然开启dhcp snooping,同时插入option82
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 请求消息插入option82,随后发给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报文中有中继信息选项,也就是option82,但是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不插入opiton82,另一个方法,是在DHCPserver上,使用配置:
(全局配置模式)ip dhcp relay information trust-all
或:
Interface fast0/0
  ip dhcp relay information trusted
思路和实验2一样。这样一来SW2会接受这些DHCP报文并进行处理,PC就能够获取到地址了。


5.其他配置
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 bingding dabase表项,该命令是在特权模式下配置。







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




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
发表于 2013-6-15 17:59:29 | 显示全部楼层
沙发秒了~
DHCP是网络中非常用到的技术。
嗅探功能的分享很到位!
感谢耿叔的付出!
发表于 2013-6-15 22:44:46 | 显示全部楼层
好牛奔呀~~ 好东西 马上学习收藏
发表于 2013-6-17 16:45:38 | 显示全部楼层
如此有营养的帖子一定使劲顶!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|熊猫同学技术论坛|小黑屋| 网络工程师论坛 ( 沪ICP备09076391 )

GMT+8, 2024-4-26 16:38 , Processed in 0.080085 second(s), 19 queries , Gzip On.

快速回复 返回顶部 返回列表