HCNP-LAB:基本路由策略配置
什么是路由策略?
路由策略可以在路由协议发布、接收和引入路由时配置使用,也可用于过滤路由和改变路由属性。路由策略的工具只要有Route-policy和filter-policy,作用在控制平面对路由进行控制.
路由策略工具:Filter-policy
filter-policy:过滤策略,在路由协议进程下应用,可以使用ACL(访问控制列表)、ip-prefix(前缀列表)、route-policy(路由策略)去匹配路由,用于控制路由的接收和发布(只有通过该过滤策略才能接收或发送,未通过该策略则被过滤丢弃)
filter-policy有import和export这两个方向,import是对接收的路由进行过滤(只针对自己的路由协议学习到的路由进行过滤),export是对发送的路由进行过滤(过滤发送给对方的路由条目)
但我们需要注意的是不同类型的路由协议,过滤的结果也不一样,因为filter-policy过滤的是路由信息。
路由协议的类型主要分为:距离矢量路由协议、链路状态路由协议
距离矢量传递的是路由信息,而链路状态传递的是LSA(链路状态描述)再根据SPF算法计算路由
我们做几个小实验熟悉一下filter-policy:
距离矢量和链路状态在Filter-policy下的体现(区域内):
实验拓扑:
实验要求如下:
1.所有路由器处于Area0(骨干区域)中
2.Router-id一致配置为10.1.0.X(X为设备编号)
3.R1宣告172.16.1~5.0路由到OSPF中
4.R2使用ACL搭配filter-policy过滤奇数段路由(172.16.1.0、172.16.3.0、172.16.5.0)
配置基本IP地址:
R1:
interface g0/0/0 ip add 10.1.12.1 24 undo shutdown interface loopback1 ip add 172.16.1.1 24 interface loopback1 ip add 172.16.2.1 24 interface loopback1 ip add 172.16.3.1 24 interface loopback1 ip add 172.16.4.1 24 interface loopback1 ip add 172.16.5.1 24
R2:
interface g0/0/0 ip add 10.1.12.2 24 undo shutdown interface g0/0/1 ip add 10.1.23.2 24 undo shutdown
R3:
interface g0/0/0 ip add 10.1.23.3 24 undo shutdown
OSPF配置:
R1:
#要设置环回口的网络类型为broadcast 因为在ospf中定义环回口默认的子网掩码为/32位 将其修改为broadcast还原网络掩码
ospf 1 router-id 10.1.0.1 area 0.0.0.0 network 10.1.12.0 0.0.0.255 network 172.16.0.0 0.0.255.255 interface LoopBack1 ospf network broadcast interface LoopBack2 ospf network broadcast interface LoopBack3 ospf network broadcast interface LoopBack4 ospf network broadcast interface LoopBack5 ospf network broadcast
R2:
ospf 1 router-id 10.1.0.2 area 0.0.0.0 network 10.1.12.0 0.0.0.255 network 10.1.23.0 0.0.0.255
R3:
ospf 1 router-id 10.1.0.3 area 0.0.0.0 network 10.1.23.0 0.0.0.255
Filter-policy配置(R2配置):
R2:
acl number 2000 rule 5 deny source 172.16.1.0 0.0.6.0 #匹配奇数段 0代表严格匹配 1代表任意匹配 rule 10 permit
172.16.0000 0001.0
111.11.0000 0110.0
--------------------------
匹配第一个:
1)172.16.1(0000 0001).0/21
2)172.16.3(0000 0011).0/21
3)172.16.5(0000 0101).0/21
4)172.16.7(0000 0111).0/21
在OSPF进程下应用:
ospf 1 router-id 10.1.0.2
filter-policy 2000 import
然后我们再来对比一下做Filter-policy之前R2的路由表:
做Filter-policy后的路由表:
我们可以看到路由表中确实是没有了对应的路由条目,但在LSDB中依旧存在...(同一个区域内的LSDB是一致的--所以R2会发送自己的DD报文给R3 R3会将其与自己的LSDB进行对比再获取)
再查看R3的OSPF路由表信息:
总结:
在使用filter-policy来过滤路由条目时,如果路由协议为链路状态路由协议(isis、ospf(LSA1/2))则只能阻止路由表的生成,而不能过滤LSA,也不能阻止路由的传递(只能阻止自身路由表对应条目的生成)。
距离矢量和链路状态在Filter-policy下的体现(区域间):
实验拓扑:
实验要求如下:
1.R1和R2处于OSPF Area 0 | R2与R3处于OSPF Area 1
2.Router-id一致配置为10.1.0.X(X为设备编号)
3.R1宣告172.16.1~5.0路由到OSPF中
4.R2使用ACL搭配filter-policy过滤偶数段路由(172.16.2.0、172.16.4.0)
OSPF配置:
R1:
#要设置环回口的网络类型为broadcast 因为在ospf中定义环回口默认的子网掩码为/32位 将其修改为broadcast还原网络掩码
ospf 1 router-id 10.1.0.1 area 0.0.0.0 network 10.1.12.0 0.0.0.255 network 172.16.0.0 0.0.255.255 interface LoopBack1 ospf network broadcast interface LoopBack2 ospf network broadcast interface LoopBack3 ospf network broadcast interface LoopBack4 ospf network broadcast interface LoopBack5 ospf network broadcast
R2:
ospf 1 router-id 10.1.0.2
area 0.0.0.0
network 10.1.12.0 0.0.0.255
area 0.0.0.1
network 10.1.23.0 0.0.0.255
R3:
ospf 1 router-id 10.1.0.3
area 0.0.0.1
network 10.1.23.0 0.0.0.255
Filter-policy配置(R2配置):
R2:
acl number 2000
rule 5 permit source 172.16.0.0 0.0.0.255 #允许172.16.0.0/24网段,因为我们只需要过滤偶数网段
rule 10 deny source 172.16.0.0 0.0.6.0
rule 15 permit
172.16.0000 0000.0
111.11.0000 0110.0
--------------------------
匹配第一个:
1)172.16.0(0000 0001).0/21
2)172.16.2(0000 0011).0/21
3)172.16.4(0000 0101).0/21
4)172.16.6(0000 0111).0/21
在OSPF进程下应用:
ospf 1 router-id 10.1.0.2
filter-policy 2000 import
R2查看OSPF路由表:
我们再来查看一下R2的LSDB数据库:
Router-LSA:
我们可以看到Router-LSA还是存在的,filter-policy无法过滤一类、二类LSA
然后我们再来查看一下三类LSA(Summary-LSA) 用于区域间传递--传递的是路由信息而不是链路状态
LSA3(Summary-LSA)通告原则如下(针对当前情况来说):
·边界路由器ABR为区域内的每条OSPF路由各产生一份LSA3并向其他区域通告。
·OSPF区域间传递的是路由,具有矢量性,只有出现在ABR路由表中的路由才会被通告
然后我们再来查看R3的路由表及LSDB数据库:
LSDB数据库如下:
总结:Filter-policy无法过滤LSA,只能过滤路由信息。OSPF不算是纯粹的链路状态路由协议,区域间以及外部引入是传递路由信息,而不是链路状态
我们Filter-policy import之所以可以阻断,不让R3其收到路由,是因为ABR传递Summary-LSA的原则是:在其OSPF路由表中存在,且处于不同区域(R2路由表中不存在这些网段,所以不会进行传递)
如果说我们只是单纯不想R3接收到呢?
filter-policy export只能针对外部路由进行过滤,所以不能满足我们的条件。
1)我们可以在R2 G0/0/1口上进行过滤LSA 或者 R3 OSPF进程下进行过滤
R2:
acl number 2000 rule 5 permit source 172.16.0.0 0.0.0.255 rule 10 deny source 172.16.0.0 0.0.6.0 rule 15 permit
然后我们在其g0/0/1下进行过滤LSA(3类)
interface GigabitEthernet0/0/1
ospf filter-lsa-out summary acl 2000
R3:
acl number 2000 rule 5 permit source 172.16.0.0 0.0.0.255 rule 10 deny source 172.16.0.0 0.0.6.0 rule 15 permit ospf 1 router-id 10.1.0.3
area 1 filter-policy 2000 import
距离矢量和链路状态在Filter-policy下的体现(外部引入):
实验拓扑:
实验要求如下:
1.R1和R2建立RIPv2邻居,将业务网段1~5宣告到RIP进程中
2.R2和R3建立OSPFv2邻居,R2作为ASBR将RIP路由引入到OSPF中
3.R2配置filter-policy将奇数网段过滤掉
RIP配置如下:
R1:
rip 1 undo summary version 2 network 172.16.0.0 network 10.0.0.0
R2:
rip 1 undo summary version 2 network 10.0.0.0
OSPF配置如下:
R2:
1 2 3 | ospf 1 router-id 10.1.0.2 <br> import-route rip 1 area 0.0.0.0 network 10.1.23.0 0.0.0.255 |
R3:
ospf 1 router-id 10.1.0.3 area 0.0.0.0 network 10.1.23.0 0.0.0.255
Filter-policy Export(R2)配置如下:
R2:
acl number 2000 step 10 rule 10 deny source 172.16.1.0 0.0.6.0 rule 20 permit
ospf 1 router-id 10.1.0.2
filter-policy 2000 export rip 1
R2查看LSDB(奇数网段的也不存在了--被过滤掉了):
R3查看OSPF路由表:
如果我们只想R3收不到,后续其他区域收的到我们就可以在接口上过滤五类LSA:
R2:
interface GigabitEthernet0/0/1 ip address 10.1.23.2 255.255.255.0 ospf filter-lsa-out ase acl 2000
查看R2 LSDB:
配置完如果不生效可以使用reset ospf process试试
R3查看路由表:
距离矢量RIP Filter-policy过滤路由条目:
实验拓扑如下:
实验要求:
1.配置RIP路由协议使得R1\R2\R3全网互通
2.R2配置Filter-policy(禁止奇数段路由进入路由表)
配置如下:
R1:
rip 1 undo summary version 2 network 172.16.0.0 network 10.0.0.0
R2:
rip 1 undo summary version 2 network 10.0.0.0
R3:
rip 1 undo summary version 2 network 10.0.0.0
R3查看没做过滤策略时是否接收的到路由信息:
R2配置filter-policy配置过滤策略:
acl number 2000 #配置访问控制列表匹配 step 10 rule 10 deny source 172.16.1.0 0.0.6.0 rule 20 permit
rip 1
filter-policy 2000 import GigabitEthernet0/0/0 #针对g0/0/0收到的RIP路由进行过滤
R2查看路由是否存在:
Filter-policy总结:
Filter-policy是一种非常灵活的路由控制工具.
·主要针对距离矢量路由协议进行路由过滤(可针对import\export方向以及接口)
import方向针对自身的路由条目进行过滤
export方向针对通告的路由进行过滤
路由策略工具:Route-policy
Route-policy是一种比较复杂的过滤器,用于过滤路由信息以及为通过过滤的路由信息设置路由属性(tag/cost等..)。一个route-policy由多个节点构成,一个节点包括多个if-match(匹配条件)和apply(应用属性)子句。if-match用于定义该节点的匹配条件,apply子句用于定义通过过滤的路由行为。如果if-match子句的过滤规则为"与",则该节点下的所有if-match子句都必须匹配,如果if-match子句的过滤条件是"或",则该节点下的所有if-match子句只用匹配一个条件即可。如果没有通过任何一个节点的过滤,路由信息将无法通过该route-policy。它不仅可以匹配给定路由信息的某些属性,还可以在条件满足时改变路由信息的属性。route-policy可以使用前几种过滤器定义自己的匹配规则。
Route-policy的应用场景:
·控制路由的引入
在对路由进行引入的时候,为了防止次优路径或者环路,可以应用策略工具进行控制或修改路由的属性
·控制路由的发布和接收
根据业务需求,可以通过策略根据来控制路由的发布和接收
·设置路由的属性
可以通过该工具来对网络进行优化和调整
我们来做个小实验:
实验拓扑如下:
实验要求:
1.配置RIP路由协议(R1和R2之间配置RIP路由协议)
2.配置OSPF路由协议(R2和R3之间配置OSPF路由协议)
3.配置路由引入(将RIP路由信息引入到OSPF中,奇数路由修改Cost为100,偶数路由修改Tag为200)
路由配置如下:
R1:
rip 1 undo summary version 2 network 172.16.0.0 network 10.0.0.0
R2:
rip 1 undo summary version 2 network 10.0.0.0 ospf 1 router-id 10.1.0.2 area 0.0.0.0 network 10.1.23.0 0.0.0.255
R3:
ospf 1 router-id 10.1.0.3 area 0.0.0.0 network 10.1.23.0 0.0.0.255
路由策略配置:
·奇数修改Cost为100 偶数Tag修改为200
R2:
route-policy R2O permit node 10 if-match acl 2000 apply cost 100 route-policy R2O permit node 20 if-match acl 2001 apply tag 200 route-policy R2O permit node 30
R3查看路由(奇数是否被修改Cost为100):
R3查看LSDB Tag是否被修改:
修改成功!
Route-policy中的permit和deny以及acl中的deny和permit有什么区别?
route-policy工作流程如下:
1)从上到下进入Node(节点)
·匹配if-match
匹配成功---》
节点匹配模式(是permit还是deny)
Permit(允许)
Apply赋予路由属性---通过策略
deny(拒绝)
拒绝通过
匹配不成功---》
匹配下一个节点---回到1)继续匹配 直到匹配成功 如果匹配一直不成功则拒绝通过
ACL中的permit代表匹配这条路由 deny代表不匹配这条路由
eNSP综合实验:
拓扑图如下:
实验要求如下:
1.OSPF Router-id统一设置为10.1.0.X 设备编号
2.OSPF 2 Area 0 DR设置为R2
3.OSPF 1 Area 0中过滤192.168.8.0/24
4.RIP 1中过滤172.16.0.0/24 192.168.0.0/24
5.OSPF 2 Area 0中过滤奇数路由
172.16.1.0/24
172.16.3.0/24
172.16.5.0/24
172.16.7.0/24
6,R3过滤192.168.0.0/24
7.OSPF 1 Area 0
192.168.1/3/5.0走R4
192.168.2/4/6.0走R5
192.168.7.0负载分担
6.RIP 1
172.16.1/3/5走R4
172.16.2/4/6走R5
172.16.7.0负载分担
1)基础IP配置:
R1:
interface LoopBack1 ip address 192.168.0.1 255.255.255.0 interface LoopBack2 ip address 192.168.1.1 255.255.255.0 interface LoopBack3 ip address 192.168.2.1 255.255.255.0 interface LoopBack4 ip address 192.168.3.1 255.255.255.0 interface LoopBack5 ip address 192.168.4.1 255.255.255.0 interface LoopBack6 ip address 192.168.5.1 255.255.255.0 interface LoopBack7 ip address 192.168.6.1 255.255.255.0 interface LoopBack8 ip address 192.168.7.1 255.255.255.0 interface LoopBack9 ip address 192.168.8.1 255.255.255.0 interface GigabitEthernet0/0/0 ip address 10.1.123.1 255.255.255.0
R2:
interface GigabitEthernet0/0/0 ip address 10.1.123.2 255.255.255.0 interface GigabitEthernet0/0/1 ip address 10.1.24.2 255.255.255.0 interface GigabitEthernet0/0/2 ip address 10.1.25.2 255.255.255.0
R3:
interface GigabitEthernet0/0/0 ip address 10.1.123.3 255.255.255.0
R4:
interface GigabitEthernet0/0/0 ip address 10.1.24.4 255.255.255.0 interface Serial1/0/0 link-protocol ppp ip address 10.1.46.4 255.255.255.0
R5:
interface GigabitEthernet0/0/0 ip address 10.1.25.5 255.255.255.0 interface Serial1/0/0 link-protocol ppp ip address 10.1.56.5 255.255.255.0
R6:
interface Serial1/0/0 link-protocol ppp ip address 10.1.46.6 255.255.255.0 interface Serial1/0/1 link-protocol ppp ip address 10.1.56.6 255.255.255.0 interface LoopBack1 ip address 172.16.0.1 255.255.255.0 interface LoopBack2 ip address 172.16.1.1 255.255.255.0 interface LoopBack3 ip address 172.16.2.1 255.255.255.0 interface LoopBack4 ip address 172.16.3.1 255.255.255.0 interface LoopBack5 ip address 172.16.4.1 255.255.255.0 interface LoopBack6 ip address 172.16.5.1 255.255.255.0 interface LoopBack7 ip address 172.16.6.1 255.255.255.0 interface LoopBack8 ip address 172.16.7.1 255.255.255.0
2)路由协议配置:
OSPF 2:
R1:
ospf 2 router-id 10.1.0.1 silent-interface LoopBack1 silent-interface LoopBack2 silent-interface LoopBack3 silent-interface LoopBack4 silent-interface LoopBack5 silent-interface LoopBack6 silent-interface LoopBack7 silent-interface LoopBack8 silent-interface LoopBack9 area 0.0.0.0 network 10.1.123.0 0.0.0.255 network 192.168.0.0 0.0.255.255 interface LoopBack1 ospf network-type broadcast interface LoopBack2 ospf network-type broadcast interface LoopBack3 ospf network-type broadcast interface LoopBack4 ospf network-type broadcast interface LoopBack5 ospf network-type broadcast interface LoopBack6 ospf network-type broadcast interface LoopBack7 ospf network-type broadcast interface LoopBack8 ospf network-type broadcast interface LoopBack9 ospf network-type broadcast
R2:
ospf 2 router-id 10.1.0.2
area 0.0.0.0
network 10.1.123.0 0.0.0.255
R3:
ospf 2 router-id 10.1.0.3
area 0.0.0.0
network 10.1.123.0 0.0.0.255
RIP 1:
R2/R4/R5: rip 1 undo summary version 2 network 10.0.0.0
OSPF 1:
R4: ospf 1 router-id 10.1.0.4 area 0.0.0.0 network 10.1.46.0 0.0.0.255 network 10.1.56.0 0.0.0.255 R5: ospf 1 router-id 10.1.0.5 area 0.0.0.0 ] network 10.1.56.0 0.0.0.255 R6: ospf 1 router-id 10.1.0.6 silent-interface LoopBack1 silent-interface LoopBack2 silent-interface LoopBack3 silent-interface LoopBack4 silent-interface LoopBack5 silent-interface LoopBack6 silent-interface LoopBack7 silent-interface LoopBack8 area 0.0.0.0 network 10.1.46.0 0.0.0.255 network 10.1.56.0 0.0.0.255 network 172.16.0.0 0.0.255.255
要求点解答:
1.OSPF Router-id统一设置为10.1.0.X 设备编号
解决,配置OSPF进程ID
ospf [进程] [进程号1-65535] [router-id]
2.OSPF 2 Area 0 DR设置为R2
配置DR优先级,默认优先级为1 越大越优(非抢占性) 每一个MA网络要选举一个DR设备
3.RIP 1中过滤172.16.0.0/24 192.168.0.0/24
route-policy:
R4/R5:
acl number 2000 rule 5 permit source 172.16.0.0 0.0.0.255
route-policy O-TO-R deny node 10 if-match acl 2000 route-policy O-TO-R permit node 15
rip 1
import-route ospf 1 route-policy O-TO-R
filter-policy:
R2:
ip ip-prefix O-R index 10 deny 192.168.0.0 24
ip ip-prefix O-R index 20 deny 10.1.123.0 24
ip ip-prefix O-R index 30 permit 0.0.0.0 0 less-equal 32
rip 1
filter-policy ip-prefix O-R export
import-route ospf 2
R2查看是否过滤172.16.0.0/24:
R4/R5查看是否过滤192.168.0.0/24:
4.OSPF 1 Area 0中过滤192.168.8.0/24
R4/R5:
ip ip-prefix hcdp index 10 permit 10.1.24.0 24
ip ip-prefix hcdp index 20 permit 10.1.25.0 24
acl number 2001 rule 5 permit source 192.168.8.0 0.0.0.255
route-policy R-O deny node 10
if-match acl 2001
route-policy R-O deny node 20
if-match ip-prefix hcdp
route-policy R-O permit node 30
ospf 1
import-route rip 1 route-policy R-O
R6查看OSPF路由表:
5.OSPF 2 Area 0中过滤奇数路由
172.16.1.0/24
172.16.3.0/24
172.16.5.0/24
172.16.7.0/24
R2: ip ip-prefix hcdp index 10 permit 10.1.24.0 24 ip ip-prefix hcdp index 20 permit 10.1.25.0 24 ip ip-prefix hcdp index 30 permit 10.1.46.0 24 ip ip-prefix hcdp index 40 permit 10.1.56.0 24 acl number 2000 rule 5 permit source 172.16.1.0 0.0.6.0 #拒绝奇数段路由 route-policy R-O deny node 10 if-match acl 2002 #过滤对方直连网段 route-policy R-O deny node 15 if-match ip-prefix hcdp
#允许其他路由 route-policy R-O permit node 20
ospf 2 router-id 10.1.0.2
import-route rip 1 route-policy R-O
R1查看路由表:
6.R3过滤192.168.0.0/24
ip ip-prefix 192.168.0 index 10 deny 192.168.0.0 24 ip ip-prefix 192.168.0 index 20 permit 0.0.0.0 0 less-equal 32 ospf 2 router-id 10.1.0.3 filter-policy ip-prefix 192.168.0 import
R3查看路由表:
7.OSPF 1 Area 0
192.168.1/3/5.0走R4
192.168.2/4/6.0走R5
192.168.7.0负载分担
R4: acl number 2002 rule 10 deny source 192.168.7.0 0.0.0.255 rule 20 permit source 192.168.1.0 0.0.6.0 acl number 2003 rule 5 permit source 192.168.0.0 0.0.6.0 route-policy R-O permit node 20 if-match acl 2002 apply cost 10 apply cost-type type-1 route-policy R-O permit node 30 if-match acl 2003 apply cost 20 apply cost-type type-1 route-policy R-O permit node 40 R5: acl number 2002 rule 10 deny source 192.168.7.0 0.0.0.255 rule 20 permit source 192.168.1.0 0.0.6.0 acl number 2003 rule 5 permit source 192.168.0.0 0.0.6.0 route-policy R-O permit node 20 if-match acl 2002 apply cost 20 apply cost-type type-1 route-policy R-O permit node 30 if-match acl 2003 apply cost 10 apply cost-type type-1 route-policy R-O permit node 40
R6查看数据流向:
192.168.1/3/5.0---->R4(10.1.46.4)
192.168.2/4/6.0--- >R5(10.1.56.4)
192.168.7.0--->负载分担--两条路都走
6.RIP 1
172.16.1/3/5走R4
172.16.2/4/6走R5
172.16.7.0负载分担
R5: acl number 2004 rule 10 deny source 172.16.7.0 0.0.0.255 rule 20 permit source 172.16.1.0 0.0.6.0 acl number 2005 rule 5 permit source 172.16.0.0 0.0.6.0 route-policy O-TO-R permit node 15 if-match acl 2004 apply cost 5 route-policy O-TO-R permit node 25 if-match acl 2005 apply cost 3 route-policy O-TO-R permit node 35 R6:
acl number 2004
rule 10 deny source 172.16.7.0 0.0.0.255
rule 20 permit source 172.16.1.0 0.0.6.0
acl number 2005
rule 5 permit source 172.16.0.0 0.0.6.0
route-policy O-TO-R permit node 15
if-match acl 2005
apply cost 5
route-policy O-TO-R permit node 25
if-match acl 2004
apply cost 3
route-policy O-TO-R permit node 35
R2查看路由表看数据流向是否正确:
172.16.1/3/5.0走10.1.24.4(R4)
172.16.2/4/6.0走10.1.25.5(R5)
172.16.7.0/24负载分担
测试如下:
我们只能测试偶数,因为奇数被过滤(如果想测试奇数则删除过滤即可)
R1 tracert R6:
奇数网段测试:
R2:
undo route-policy R-O node 10
反过来也是如此 R6去看路径是否正确:
大概就这样 如果有问题可以在评论区留言~ 先溜了!! 欢迎大家来互相学习~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix