基于FRR全面解析BGP协议(五):FRR的BGP路由策略
https://www.codenong.com/cs106763166/
route map类似一张表,其中的每一个表项可以看做是比较复杂的static route。如:
route map TEST permit 10
match A
match B
match C
set A
set B
set C
意思就是在名为TEST的route map中添加一个entry,序号为10,其类型为perimit.
match动作指定了匹配条件,在示例中有3个match动作,也就是有3个匹配条件。
set动作指定了如果匹配成功,应对执行的操作,在示例中有3个set动作,也就是有3个操作。
只有某条路由route,三个match动作(match A,B,C)都匹配成功后,才会进行下面的set动作(是所有操作,即set A,B,C)
match和set动作都是可选的,不是必须的,例如将上面的示例改为下面的形式依然有效:
route map TEST permit 10
match A
match B
match C
或
route map TEST permit 10
set A
set B
set C
还有route map中的entry还有另一种类型,即deny,如:
route map TEST delay 20
match A
如果某条路由route匹配到指定条件(match A),则该route会被deny掉,不会出现在FIB中。deny类型的entry可以用来过滤掉某些路由。
当某个route map有多个entry时,会根据entry的序号,从小到大进行匹配,匹配到其中任何一个entry就会结束。如:
route map TEST permit 10
…
route map TEST deny 20
…
这样就会先匹配entry 10,匹配不成功再继续匹配entry 20。
注意!每个route map的最后一个entry都是deny!它是自动隐含在route map中的,当route map中的所有entry都不匹配时,就会自动deny掉对应的路由。
为了防止不匹配的路由被deny掉,应该在每个route map的最后加上一个空的permit entry,如:
route map TEST deny 10
…
route map TEST permit 20
本文会向你展示如何在 Quagga 中使用前缀列表和路由映射。
拓扑和需求
本教程使用下面的拓扑结构。
服务供应商A和供应商B已经将对方设置成为 eBGP 对等体,实现互相通信。他们的自治系统号和前缀分别如下所示。
- 对等区段: 192.168.1.0/24
- 服务供应商A: 自治系统号 100, 前缀 10.10.0.0/16
- 服务供应商B: 自治系统号 200, 前缀 10.20.0.0/16
在这个场景中,供应商B只想从A接收 10.10.10.0/23, 10.10.10.0/24 和 10.10.11.0/24 三个前缀。
安装 Quagga 和设置 BGP 对等体
在之前的教程中,我们已经写了安装 Quagga 和设置 BGP 对等体的方法,所以这里就不再详细说明了,只简单介绍下 BGP 配置和前缀广播:
上图说明 BGP 对等体已经开启。Router-A 在向 router-B 广播多个前缀,而 Router-B 也在向 router-A 广播一个前缀 10.20.0.0/16。两个路由器都能正确无误地收发前缀。
创建前缀列表
路由器可以使用 ACL 或前缀列表来过滤一个前缀。前缀列表比 ACL 更常用,因为前者处理步骤少,而且易于创建和维护。
ip prefix-list DEMO-PRFX permit 192.168.0.0/23
上面的命令创建了名为“DEMO-FRFX”的前缀列表,只允许存在 192.168.0.0/23 这个前缀。
前缀列表的另一个强大功能是支持子网掩码区间,请看下面的例子:
ip prefix-list DEMO-PRFX permit 192.168.0.0/23 le 24
这个命令创建的前缀列表包含在 192.168.0.0/23 和 /24 之间的前缀,分别是 192.168.0.0/23, 192.168.0.0/24 和 192.168.1.0/24。运算符“le”表示小于等于,你也可以使用“ge”表示大于等于。
一个前缀列表语句可以有多个允许或拒绝操作。每个语句都自动或手动地分配有一个序列号。
如果存在多个前缀列表语句,则这些语句会按序列号顺序被依次执行。在配置前缀列表的时候,我们需要注意在所有前缀列表语句之后是隐性拒绝语句,就是说凡是不被明显允许的,都会被拒绝。
如果要设置成允许所有前缀,前缀列表语句设置如下:
ip prefix-list DEMO-PRFX permit 0.0.0.0/0 le 32
我们已经知道如何创建前缀列表语句了,现在我们要创建一个名为“PRFX-LST”的前缀列表,来满足我们实验场景的需求。
router-b# conf t
router-b(config)#ip prefix-list PRFX-LST permit 10.10.10.0/23 le 24
创建路由映射
除了前缀列表和 ACL,这里还有另一种机制,叫做路由映射,也可以在 BGP 路由器中控制前缀。事实上,路由映射针对前缀匹配的微调效果比前缀列表和 ACL 都强。
与前缀列表类似,路由映射语句也可以指定允许和拒绝操作,也需要分配一个序列号。每个路由匹配可以有多个允许或拒绝操作。例如:
route-map DEMO-RMAP permit 10
上面的语句创建了名为“DEMO-RMAP”的路由映射,添加序列号为10的允许操作。现在我们在这个序列号所对应的路由映射下使用 match 命令进行匹配。
router-a(config-route-map)# match (press ?in the keyboard)
as-path Match BGP AS path list
community Match BGP community list
extcommunity Match BGP/VPN extended community list
interface match first hop interface of route
ip IP information
ipv6 IPv6 information
metric Match metric of route
origin BGP origin code
peer Match peer address
probability Match portion of routes defined by percentage value
tag Match tag of route
如你所见,路由映射可以匹配很多属性,在本教程中匹配的是前缀。
route-map DEMO-RMAP permit 10
match ip address prefix-list DEMO-PRFX
这个 match 命令会匹配之前建好的前缀列表中允许的 IP 地址(也就是前缀 192.168.0.0/23, 192.168.0.0/24 和 192.168.1.0/24)。
接下来,我们可以使用 set 命令来修改这些属性。例子如下:
route-map DEMO-RMAP permit 10
match ip address prefix-list DEMO-PRFX
set(press ?in keyboard)
aggregator BGP aggregator attribute
as-path Transform BGP AS-path attribute
atomic-aggregate BGP atomic aggregate attribute
comm-listset BGP community list(for deletion)
community BGP community attribute
extcommunity BGP extended community attribute
forwarding-address ForwardingAddress
ip IP information
ipv6 IPv6 information
local-preference BGP local preference path attribute
metric Metric value for destination routing protocol
metric-type Type of metric
origin BGP origin code
originator-id BGP originator ID attribute
src src address forroute
tag Tag value for routing protocol
vpnv4 VPNv4 information
weight BGP weight for routing table
如你所见,set 命令也可以修改很多属性。为了作个示范,我们修改一下 BGP 的 local-preference 这个属性。
route-map DEMO-RMAP permit 10
match ip address prefix-list DEMO-PRFX
setlocal-preference 500
如同前缀列表,路由映射语句的末尾也有隐性拒绝操作。所以我们需要添加另外一个允许语句(使用序列号20)来允许所有前缀。
route-map DEMO-RMAP permit 10
match ip address prefix-list DEMO-PRFX
setlocal-preference 500
!
route-map DEMO-RMAP permit 20
序列号20未指定任何匹配命令,所以默认匹配所有前缀。在这个路由映射语句中,所有的前缀都被允许。
回想一下,我们的需求是只允许或只拒绝一些前缀,所以上面的 set 命令不应该存在于这个场景中。我们只需要一个允许语句,如下如示:
router-b# conf t
router-b(config)#route-map RMAP permit 10
router-b(config-route-map)# match ip address prefix-list PRFX-LST
这个路由映射才是我们需要的效果。
应用路由映射
注意,在被应用于一个接口或一个 BGP 邻居之前,ACL、前缀列表和路由映射都不会生效。与 ACL 和前缀列表一样,一条路由映射语句也能被多个接口或邻居使用。然而,一个接口或一个邻居只能有一条路由映射语句应用于输入端,以及一条路由映射语句应用于输出端。
下面我们将这条路由映射语句应用于 router-B 的 BGP 配置,为 router-B 的邻居 192.168.1.1 设置输入前缀广播。
router-b# conf terminal
router-b(config)# router bgp 200
router-b(config-router)# neighbor 192.168.1.1route-map RMAP in
现在检查下广播路由和收取路由。
显示广播路由的命令:
show ip bgp neighbor-IP advertised-routes
显示收取路由的命令:
show ip bgp neighbor-IP routes
可以看到,router-A 有4条路由前缀到达 router-B,而 router-B 只接收3条。查看一下范围,我们就能知道只有被路由映射允许的前缀才能在 router-B 上显示出来,其他的前缀一概丢弃。
小提示:如果接收前缀内容没有刷新,试试重置下 BGP 会话,使用这个命令:clear ip bgp neighbor-IP
。本教程中命令如下:
clearip bgp 192.168.1.1
我们能看到系统已经满足我们的要求了。接下来我们可以在 router-A 和 router-B 上创建相似的前缀列表和路由映射语句来更好地控制输入输出的前缀。
这里把配置过程总结一下,方便查看。
router bgp 200
network 10.20.0.0/16
neighbor 192.168.1.1 remote-as100
neighbor 192.168.1.1route-map RMAP in
!
ip prefix-list PRFX-LST seq 5 permit 10.10.10.0/23 le 24
!
route-map RMAP permit 10
match ip address prefix-list PRFX-LST
总结
在本教程中我们演示了如何在 Quagga 中设置前缀列表和路由映射来过滤 BGP 路由。我们也展示了如何将前缀列表结合进路由映射来进行输入前缀的微调功能。你可以参考这些方法来设置满足自己需求的前缀列表和路由映射。这些工具是保护网络免受路由毒化和来自 bogon 路由(LCTT 译注:指不该出现在internet路由表中的地址)的广播。
mpls1(config-router)# ip prefix-list DEMO-PRFX permit 192.168.0.0/23 mpls1(config)# quit mpls1# quit root@mpls1:~# vtysh Hello, this is FRRouting (version 7.3.1). Copyright 1996-2005 Kunihiro Ishiguro, et al. mpls1# sh run Building configuration... Current configuration: ! frr version 7.3.1 frr defaults traditional hostname mpls1 log file /var/log/frr/frr.log log syslog informational service integrated-vtysh-config ! interface enp7s0 ip address 172.16.0.1/30 ip ospf area 0.0.0.0 ! interface enp8s0 ip address 172.16.0.9/30 ip ospf area 0.0.0.0 ! interface lo ip address 192.168.0.1/32 ip ospf area 0.0.0.0 ! router bgp 100 ! router ospf ospf router-id 192.168.0.1 passive-interface lo capability opaque mpls-te on mpls-te router-address 192.168.0.1 segment-routing on segment-routing global-block 16000 19999 segment-routing node-msd 8 segment-routing prefix 192.168.0.1/32 index 1001 router-info area ! ip prefix-list DEMO-PRFX seq 5 permit 192.168.0.0/23 ! line vty ! end
mpls1(config)# no ip prefix-list DEMO-PRFX ]mpls1(config)# ]no router bgp 100 % Unknown command: ]no router bgp 100 mpls1(config)# no router bgp 100
前缀测试
节点1:
root@mpls1:/etc/frr# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet 192.168.0.1/32 brd 192.168.0.1 scope global lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:e5:30:19 brd ff:ff:ff:ff:ff:ff inet 192.168.122.2/24 scope global enp1s0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fee5:3019/64 scope link valid_lft forever preferred_lft forever 3: enp7s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:d9:52:82 brd ff:ff:ff:ff:ff:ff inet 172.16.0.1/30 brd 172.16.0.3 scope global enp7s0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fed9:5282/64 scope link valid_lft forever preferred_lft forever 4: enp8s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 52:54:00:ff:18:f4 brd ff:ff:ff:ff:ff:ff inet 172.16.0.9/30 brd 172.16.0.11 scope global enp8s0 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:feff:18f4/64 scope link valid_lft forever preferred_lft forever root@mpls1:/etc/frr#
root@mpls1:/etc/frr# vtysh Hello, this is FRRouting (version 7.3.1). Copyright 1996-2005 Kunihiro Ishiguro, et al. mpls1# sh run Building configuration... Current configuration: ! frr version 7.3.1 frr defaults traditional hostname mpls1 log file /var/log/frr/frr.log log syslog informational service integrated-vtysh-config ! interface enp7s0 ip address 172.16.0.1/30 ip ospf area 0.0.0.0 ! interface enp8s0 ip address 172.16.0.9/30 ip ospf area 0.0.0.0 ! interface lo ip address 192.168.0.1/32 ip ospf area 0.0.0.0 ! router bgp 65000 neighbor 192.168.122.205 remote-as 65000 ! address-family ipv4 unicast network 192.168.0.1/32 network 192.168.122.0/24 neighbor 192.168.122.205 prefix-list DEMO-PRFX out ------------------------不广播出去 exit-address-family ! ip prefix-list DEMO-PRFX seq 5 deny 192.168.0.1/32 ---------------------- ! line vty ! end mpls1#
节点二
root@gobgp:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 enp1s0 0.0.0.0 192.168.122.1 0.0.0.0 UG 100 0 0 enp1s0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0 192.168.122.1 0.0.0.0 255.255.255.255 UH 100 0 0 enp1s0 root@gobgp:~# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. ^C --- 192.168.0.1 ping statistics --- 2 packets transmitted, 0 received, 100% packet loss, time 1029ms root@gobgp:~#
节点不deny
mpls1# conf t mpls1(config)# router bgp 65000 mpls1(config-router)# no neighbor 192.168.122.205 prefix-list DEMO-PRFX out -----------取消策略 mpls1(config-router)# end mpls1# wr Note: this version of vtysh never writes vtysh.conf Building Configuration... Integrated configuration saved to /etc/frr/frr.conf [OK] mpls1# sh run Building configuration... Current configuration: ! frr version 7.3.1 frr defaults traditional hostname mpls1 log file /var/log/frr/frr.log log syslog informational service integrated-vtysh-config ! interface enp7s0 ip address 172.16.0.1/30 ip ospf area 0.0.0.0 ! interface enp8s0 ip address 172.16.0.9/30 ip ospf area 0.0.0.0 ! interface lo ip address 192.168.0.1/32 ip ospf area 0.0.0.0 ! router bgp 65000 neighbor 192.168.122.205 remote-as 65000 ! address-family ipv4 unicast network 192.168.0.1/32 network 192.168.122.0/24 exit-address-family ! ip prefix-list DEMO-PRFX seq 5 deny 192.168.0.1/32 ! line vty ! end mpls1#
root@gobgp:~# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. 64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.101 ms ^C --- 192.168.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.101/0.101/0.101/0.000 ms root@gobgp:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 enp1s0 0.0.0.0 192.168.122.1 0.0.0.0 UG 100 0 0 enp1s0 192.168.0.1 192.168.122.2 255.255.255.255 UGH 20 0 0 enp1s0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0 192.168.122.1 0.0.0.0 255.255.255.255 UH 100 0 0 enp1s0 root@gobgp:~#
route map 测试
mpls1# conf t mpls1(config)# route-map RMAP deny 10 mpls1(config-route-map)# match ip address prefix-list DEMO-PRFX mpls1(config-route-map)# router bgp 65000 mpls1(config-router)# neighbor 192.168.122.205 route-map RMAP out mpls1(config-router)# end mpls1# wr Note: this version of vtysh never writes vtysh.conf Building Configuration... Integrated configuration saved to /etc/frr/frr.conf [OK] mpls1#
mpls1# sh run Building configuration... Current configuration: ! frr version 7.3.1 frr defaults traditional hostname mpls1 log file /var/log/frr/frr.log log syslog informational service integrated-vtysh-config ! interface enp7s0 ip address 172.16.0.1/30 ip ospf area 0.0.0.0 ! interface enp8s0 ip address 172.16.0.9/30 ip ospf area 0.0.0.0 ! interface lo ip address 192.168.0.1/32 ip ospf area 0.0.0.0 ! router bgp 65000 neighbor 192.168.122.205 remote-as 65000 ! address-family ipv4 unicast network 192.168.0.1/32 network 192.168.122.0/24 neighbor 192.168.122.205 route-map RMAP out exit-address-family ! ip prefix-list DEMO-PRFX seq 5 deny 192.168.0.1/32 ! route-map RMAP deny 10 match ip address prefix-list DEMO-PRFX ! line vty ! end mpls1#
无法访问了
root@gobgp:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 enp1s0 0.0.0.0 192.168.122.1 0.0.0.0 UG 100 0 0 enp1s0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0 192.168.122.1 0.0.0.0 255.255.255.255 UH 100 0 0 enp1s0 root@gobgp:~# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. ^C --- 192.168.0.1 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms root@gobgp:~#
mpls1# conf t mpls1(config)# router bgp 65000 mpls1(config-router)# no neighbor 192.168.122.205 route-map RMAP out mpls1(config-router)# end mpls1# wr Note: this version of vtysh never writes vtysh.conf Building Configuration... Integrated configuration saved to /etc/frr/frr.conf [OK] mpls1#
又可以访问了
root@gobgp:~# ping 192.168.0.1 PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data. 64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=0.124 ms ^C --- 192.168.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.124/0.124/0.124/0.000 ms root@gobgp:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 enp1s0 0.0.0.0 192.168.122.1 0.0.0.0 UG 100 0 0 enp1s0 192.168.0.1 192.168.122.2 255.255.255.255 UGH 20 0 0 enp1s0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0 192.168.122.1 0.0.0.0 255.255.255.255 UH 100 0 0 enp1s0 root@gobgp:~#
flowspec
flowspec + vrf
bash-4.4# cat /etc/frr/daemons | grep yes
zebra=yes
bgpd=yes
pbrd=yes
Steps to Reproduce Configure BGP to accept flowspec router bgp 12345 bgp router-id 10.0.0.1 neighbor 10.15.10.253 remote-as 12345 ! address-family ipv4 flowspec neighbor 10.15.10.253 activate exit-address-family rfp full-table-download off ! Check received flowspec entry 94613e4eaa8c# show bgp ipv4 flowspec BGP table version is 1103, local router ID is 10.0.0.1, vrf id 0 Status codes: s suppressed, d damped, h history, * valid, > best, = multipath, i internal, r RIB-failure, S Stale, R Removed Nexthop codes: @NNN nexthop's vrf id, < announce-nh-self Origin codes: i - IGP, e - EGP, ? - incomplete Network Next Hop Metric LocPrf Weight Path *>i to 10.17.10.20/32 from 0.0.0.0/0 proto = 6 dstp = 443 10.15.10.253 100 0 i Displayed 1 routes and 1 total paths 94613e4eaa8c# show bgp ipv4 flowspec 10.17.10.20/32 BGP flowspec entry: (flags 0x418) Destination Address 10.17.10.20/32 Source Address 0.0.0.0/0 IP Protocol = 6 Destination Port = 443 FS:redirect IP 0x0 NH 10.15.10.253 received for 00:03:39 not installed in PBR bash-4.4# iptables -t mangle --list Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination
In order to configure an IPv4 Flowspec engine, use the following configuration. As of today, it is only possible to configure Flowspec on the default VRF. .. code-block:: frr router bgp <AS> neighbor <A.B.C.D> remote-as <remoteAS> address-family ipv4 flowspec neighbor <A.B.C.D> activate exit exit You can see Flowspec entries, by using one of the following show commands: .. index:: show bgp ipv4 flowspec [detail | A.B.C.D] .. clicmd:: show bgp ipv4 flowspec [detail | A.B.C.D]
router bgp <ASx> neighbor <A.B.C.D> remote-as <ASz> address-family ipv4 flowspec neighbor A.B.C.D activate exit exit router bgp <ASy> vrf vrf2 address-family ipv4 unicast rt redirect import <E.F.G.H:II> exit exit
iptables
root@mpls1:~# iptables -L FLOWSPEC iptables: No chain/target/match by that name. root@mpls1:~#
Distributed micro-segmentation with Flow-Spec