基於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