FLOWSPEC

MP-BGP FLOWSPEC

本文是 RTBH 的后续,传统的 RTBH 仅能提供有限的路由控制,优点是无需设备支持高级特性和功能,仅需要支持 BGP 即可,但随着网络设备性能大幅提高以及专用转发芯片的使用,性能已经不再是网络设备瓶颈;Flowspec 就是应对分布式路由/限速控制协议的典型,基于 MP-BGP,集中控制路由、限速等;控制粒度更细,也更灵活方便。

Flowspec

传统的路由协议仅能传递 “下一跳” ,无法传递像 drop 这种动作,更不用说限速、重定向等更高级操作;

Flowspec(Flow Specification,流规格)的开发就是用来实现对 BGP 网络中的非法流量进行过滤与巡查,以减轻DoS(Denial of Service,拒绝服务)攻击和 DDoS(Distributed Denial of Service,分布式拒绝服务)攻击对网络的影响。借助 BGP 路由更新,Flowspec 针对攻击流量的特点,可集中配置和管理匹配规则以及流量动作,并快速地将匹配规则和流量动作应用到其他 BGP 路由器中。

为支持 Flowspec,MP-BGP 定义了 Flowspec IPv4/6 地址族和 Flowspec VPNv4/6 地址族,其中 Flowspec IPv4/6 v4/v6 用于公网,VPNv4/6 用于 VPN实例/VRF 下的控制。
这两段描述引自这里

Flowspec 是 MP-BGP IPv4/v6 以及 VPNv4/6 AF 下可用的一个 sub-AF,主要 NLRI 类型如下:

BGP FlowSpec NLRI Types

  • Type 1 - Destination Prefix 目的
  • Type 2 - Source Prefix 源
  • Type 3 - IP Protocol IP协议
  • Type 4 - Port 端口
  • Type 5 - Destination port 目的端口
  • Type 6 - Source port 源端口
  • Type 7 - ICMP type ICMP 类型
  • Type 8 - ICMP code ICMP 代码
  • Type 9 - TCP flags TCP 标志位
  • Type 10 - Packet length 包长度
  • Type 11 - DSCP DSCP 值
  • Type 12 - Fragment 分片标记

FLowspec 动作(或者说行为)使用 BGP 的扩展 Communtity,具体如下:

BGP Extended Communities Type

  • 0x8006 - Flow spec traffic-rate 限速
  • 0x8007 - Flow spec traffic-action 采样
  • 0x8008 - Flow spec redirect 重定向下一跳,或重定向到其他 VRF
  • 0x8009 - Flow spec traffic-remarking 设置 DSCP 值

示例

示例拓扑:
topology
下面的示例配置设备为 H3C VSR

  • 192.168.2.1 禁止主动访问TCP 192.168.1.1:22
    if-match destination-ip 192.168.1.1 255.255.255.255
    if-match source-ip 192.168.2.1 255.255.255.255
    if-match protocol 6
    if-match destination-port 22
    if-match tcp-flags match 2
    apply deny
    
  • 192.168.2.1 访问 192.168.3.254 限速1kbps
    if-match destination-ip 192.168.3.254 255.255.255.255
    if-match source-ip 192.168.2.1 255.255.255.255
    apply traffic-rate 1
    
  • 192.168.1.1 禁止主动 ping 192.168.3.254
    if-match destination-ip 192.168.3.254 255.255.255.255
    if-match source-ip 192.168.1.1 255.255.255.255
    if-match protocol 1
    if-match icmp-type 8
    if-match icmp-code 0
    apply deny
    
  • 192.168.3.1 源黑洞
    if-match source-ip 192.168.3.1 255.255.255.255
    apply deny
    

测试

限制192.168.1.1或者192.168.2.1访问192.168.3.1

###R5###
flowspec
 address-family ipv4
  flow-route ttt

img

匹配顺序

流量可以匹配多个 FLowspec 规则的情况并不鲜见,上面提到的 NLRI type 在 flowspec rule 中叫做 "components",直译为“组件”,RFC5575 将 flowspec rule 的匹配顺序定义为如下:

  • flowspec rules 定义顺序无关紧要,不影响匹配优先级
  • 首先对比 type 序号,序号小的优先
  • 然后对比 IP 前缀,前缀小的优先
  • 前缀相同情况,掩码大的优先(更明细优先,类似单播路由表中的最长匹配)
  • 如果仍然一致,则对其他 components 进行字符串比较,算法同 C 语言中 memcmp() 函数,小值优先
  • 仍然相同,字符串长的优先
  • 如果所有的都一致,那就无所谓优先了
  • 使用上面方式对 flowspec rules 排序,流量依次匹配,匹配成功后跳出,执行 rule 中定义的行为;

下面使用最长匹配方式演示一下,拓扑还是上面:

flow-route aaa
 #24掩码
 if-match source-ip 192.168.1.0 255.255.255.0
 apply deny
#
flow-route bbb
 #25掩码
 if-match source-ip 192.168.1.0 255.255.255.128
 #限速,小流量不会产生影响
 apply traffic-rate 10
#
flowspec
 address-family ipv4
  flow-route aaa
  flow-route bbb

R1 flowspec 路由已经生效
img

执行的是掩码更长的策略,仍然可以 ping 通
img

说明

当前路由器等网络设备更多是作为 Flowspec 的执行者,某些网络设备甚至不能定义 flowspec rules,只能从控制器接收 rules 并执行;本文中使用 H3C VSR 演示 Flowspec 也只是粗略,无法基于路由器分组等分别控制;Flowspec 实际使用过程中一般都会有专门的控制器和路由器建立 flowspec 邻居,集中下发flowspec rules。

笔者个人认为当前版本 flowspec 还是有些遗憾,上述基本都是三层策略,对二层基本没有支持,这也暗示 flowspec 协议开销比较大,只适用于较大的网络环境,不太适用于像园区网这种拓扑相对简单场景;我个人任然认为如果能够增加 mac 地址匹配等二层功能该协议会更有发展前景。

完整配置

###PC1###
IP:192.168.1.1/24 GW:192.168.1.254

###PC2###
IP:192.168.2.1/24 GW:192.168.2.254

###PC3###
IP:192.168.3.1/24 GW:192.168.3.254

注意下面配置使用 H3C VSR Version 7.1.064, Release 1362P12 不同厂商/不同型号配置会有区别

###R1###
isis 1
 is-level level-1
 cost-style wide
 network-entity 65.0000.0010.0100.1001.00
#
interface LoopBack0
 ip address 1.1.1.1 255.255.255.255
 isis enable 1
#
interface GigabitEthernet1/0
 port link-mode route
 ip address 10.0.14.1 255.255.255.0
 isis enable 1
#
interface GigabitEthernet2/0
 port link-mode route
 ip address 192.168.1.254 255.255.255.0
 isis enable 1
 isis silent
#
bgp 65500
 router-id 1.1.1.1
 peer 5.5.5.5 as-number 65500
 peer 5.5.5.5 connect-interface LoopBack0
 #
 address-family ipv4 flowspec
  peer 5.5.5.5 enable
  peer 5.5.5.5 validation-disable
#
###R2###
isis 1
 is-level level-1
 cost-style wide
 network-entity 65.0000.0020.0200.2002.00
#
interface LoopBack0
 ip address 2.2.2.2 255.255.255.255
 isis enable 1
#
interface GigabitEthernet1/0
 port link-mode route
 ip address 10.0.24.2 255.255.255.0
 isis enable 1
#
interface GigabitEthernet2/0
 port link-mode route
 ip address 192.168.2.254 255.255.255.0
 isis enable 1
 isis silent
#
bgp 65500
 router-id 2.2.2.2
 peer 5.5.5.5 as-number 65500
 peer 5.5.5.5 connect-interface LoopBack0
 #
 address-family ipv4 flowspec
  peer 5.5.5.5 enable
  peer 5.5.5.5 validation-disable
#
###R3###
isis 1
 is-level level-1
 cost-style wide
 network-entity 65.0000.0030.0300.3003.00
#
interface LoopBack0
 ip address 3.3.3.3 255.255.255.255
 isis enable 1
#
interface GigabitEthernet1/0
 port link-mode route
 ip address 10.0.34.3 255.255.255.0
 isis enable 1
#
interface GigabitEthernet2/0
 port link-mode route
 ip address 192.168.3.254 255.255.255.0
 isis enable 1
 isis silent
#
bgp 65500
 router-id 3.3.3.3
 peer 5.5.5.5 as-number 65500
 peer 5.5.5.5 connect-interface LoopBack0
 #
 address-family ipv4 flowspec
  peer 5.5.5.5 enable
  peer 5.5.5.5 validation-disable
#
###R4###
isis 1
 is-level level-1
 cost-style wide
 network-entity 65.0000.0040.0400.4004.00
#
interface LoopBack0
 ip address 4.4.4.4 255.255.255.255
 isis enable 1
#
interface GigabitEthernet1/0
 port link-mode route
 ip address 10.0.14.4 255.255.255.0
 isis enable 1
#
interface GigabitEthernet2/0
 port link-mode route
 ip address 10.0.24.4 255.255.255.0
 isis enable 1
#
interface GigabitEthernet3/0
 port link-mode route
 ip address 10.0.34.4 255.255.255.0
 isis enable 1
#
interface GigabitEthernet4/0
 port link-mode route
 ip address 10.0.45.4 255.255.255.0
 isis enable 1
#
###R5###
isis 1
 is-level level-1
 cost-style wide
 network-entity 65.0000.0050.0500.5005.00
#
flow-route aaa

#
flow-route bbb

#
flowspec
 address-family ipv4
  flow-route aaa
  flow-route bbb
#
interface LoopBack0
 ip address 5.5.5.5 255.255.255.255
 isis enable 1
#
interface GigabitEthernet1/0
 port link-mode route
 ip address 10.0.45.5 255.255.255.0
 isis enable 1
#
bgp 65500
 router-id 5.5.5.5
 group all internal
 peer 1.1.1.1 group all
 peer 2.2.2.2 group all
 peer 3.3.3.3 group all
 #
 address-family ipv4 flowspec
  peer 1.1.1.1 enable
  peer 1.1.1.1 reflect-client
  peer 1.1.1.1 validation-disable
  peer 2.2.2.2 enable
  peer 2.2.2.2 reflect-client
  peer 2.2.2.2 validation-disable
  peer 3.3.3.3 enable
  peer 3.3.3.3 reflect-client
  peer 3.3.3.3 validation-disable
#

参考

https://www.h3c.com/cn/d_201905/1182433_30005_0.htm
https://datatracker.ietf.org/doc/html/rfc5575
https://www.juniper.net/documentation/en_US/day-one-books/DO_BGP_FLowspec.pdf
https://www.iana.org/assignments/flow-spec/flow-spec.xhtml

posted @ 2024-04-24 11:30  bfhyqy  阅读(37)  评论(0编辑  收藏  举报