zoukankan      html  css  js  c++  java
  • 路由策略

    路由重分布

    在使用多种网络协议的网络中,路由重分布允许将一个或多个路由器从其他路由进程或协议中学到的路由(静态,动态),将路由通告到自己的进程中。

    应用场景:

    迁移网络(割接):比如在原来在的网络中运行 RIP 协议,采用分段割接的方式,先将一部分网络由 RIP 切换成 OSPF,但由于当前阶段只完成一部分网络切换,其他部分仍然在运行 RIP 协议。此时要将 OSPF 学习的路由重分布到 RIP 协议中,用于保证正常网络运行。

    路由重分布的类型:

    1. 直连路由
    2. 静态路由
    3. 动态路由协议(其他路由协议学习到的路由)

    但只有路由表中的路由才可以进行重分布

    重分布对 Metric 的影响:

    • 距离矢量路由协议:引入的外部路由的 Metric 值默认为无限大,需要手动配置
    • 链路状态路由协议:
      • OSPF:引入的外部路由的 cost 为 20,如果引入的是 BGP cost 为 1.

    重分布类型

    单点单向重分布:将协议 B 引入协议 A

    单点双向重分布:将协议 B 引入协议 A,同时也将协议 A 引入协议 B,下面的配置例子

    多点单向重分布:将多台设备上的 B 引入 A,主要是做冗余备份

    多点多向重分布:要注意环路的出现。

    如何引入

    这里 R1 和 R2 之间运行 RIP 协议,R2 和 R3 之间运行 EIGRP 协议,R3 和 R4 之间运行 OSPF 协议。想实现在 R1 使用 Lo 0 接口访问 R4 的 Lo 0 接口,以及在 R4 使用 Lo 0 接口访问 R1 的 Lo 0 接口。

    实现将 1.1.1.1/24 路由引入到 R4 中:

    1. 在 R2 上 ERGRP 配置引入 RIP 路由,进行重分布,进而 R3 能收到 1.1.1.1 网络的路由。
    2. 在 R3 上 OSPF 引入 EIGRP 路由,进而 R4 能收到 1.1.1.1 网络的路由。
    # R2 - 引入 RIP
    Router(config)#router eigrp 100
    # 由于引入的是距离矢量,所以需要手动配置 metric 所需参数
    Router(config-router)#redistribute rip metric 10000 1000 255 1 1500
    
    # R3 查看引入的路由
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    D EX  1.0.0.0/8 [170/537600] via 23.1.1.1, 00:00:12, Ethernet0/0
          4.0.0.0/32 is subnetted, 1 subnets
    O        4.4.4.4 [110/11] via 34.1.1.4, 00:12:26, Ethernet0/1
          12.0.0.0/24 is subnetted, 1 subnets
    D EX     12.1.1.0 [170/537600] via 23.1.1.1, 00:00:12, Ethernet0/0
          23.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        23.1.1.0/24 is directly connected, Ethernet0/0
    L        23.1.1.3/32 is directly connected, Ethernet0/0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.1/32 is directly connected, Ethernet0/1
    
    # R3 - 引入 EIGRP
    Router(config)#router ospf 100
    # OSPF 引入外部路由,有默认值 20,不用手动配置
    Router(config-router)#redistribute eigrp 100
    
    # R4 - 在 R4 查看
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    O E2  1.0.0.0/8 [110/20] via 34.1.1.1, 00:00:22, Ethernet0/0
          4.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        4.4.4.0/24 is directly connected, Loopback0
    L        4.4.4.4/32 is directly connected, Loopback0
          12.0.0.0/24 is subnetted, 1 subnets
    O E2     12.1.1.0 [110/20] via 34.1.1.1, 00:00:22, Ethernet0/0
          23.0.0.0/24 is subnetted, 1 subnets
    O E2     23.1.1.0 [110/20] via 34.1.1.1, 00:00:22, Ethernet0/0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/0
    L        34.1.1.4/32 is directly connected, Ethernet0/0
    

    实现将 4.4.4.4/24 路由引入 R1 中:

    1. 在 R3 EIGRP 中引入 OSPF 路由,实现 R2 收到 4.4.4.4 的网络路由。
    2. 在 R2 RIP 中引入 EIGRP 路由,实现 R1 收到 4.4.4.4 的网络路由。
    # R3 - 引入 OSPF
    Router(config)#router eigrp 100
    # EIGRP 是距离矢量,所以引入 OSPF 要设置 Metric
    Router(config-router)#redistribute ospf 100 metric 1000 1000 255 1 1500
    
    # 在 R2 查看,多出 4.0 的路由
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    R     1.0.0.0/8 [120/1] via 12.1.1.1, 00:00:12, Ethernet0/0
          4.0.0.0/32 is subnetted, 1 subnets
    D EX     4.4.4.4 [170/2841600] via 23.1.1.3, 00:01:25, Ethernet0/1
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          23.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        23.1.1.0/24 is directly connected, Ethernet0/1
    L        23.1.1.1/32 is directly connected, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    D EX     34.1.1.0 [170/2841600] via 23.1.1.3, 00:01:25, Ethernet0/1
    
    # 在 R2 RIP 引入 EIGRP 路由
    Router(config)#router rip
    # RIP 是用跳数计算 Metric
    Router(config-router)#redistribute eigrp 100 metric 5
    
    # 在 R1 上查看,4.0 的路由
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
          1.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        1.1.1.0/24 is directly connected, Loopback0
    L        1.1.1.1/32 is directly connected, Loopback0
    R     4.0.0.0/8 [120/5] via 12.1.1.2, 00:00:05, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
    R     23.0.0.0/8 [120/5] via 12.1.1.2, 00:00:05, Ethernet0/0
    R     34.0.0.0/8 [120/5] via 12.1.1.2, 00:00:05, Ethernet0/0
    

    路由过滤

    通过一些工具如 分发列表,前缀列表,road-map 等控制收到或者发出的路由信息。

    分发列表

    将出站和入站的路由进行控制,并且控制时有方向设置。通过 ACL 实现.

    • 出站时:影响其他设备
    • 入站时:影响本设备

    通过分发列表控制路由过滤

    比如在重分发的拓扑中,R4 中除了引入 1.1.1.1/24 网络的路由,还将 12 和 23 网络的路由也引入到 OSPF 中:

    这里如果我们想实现,仅仅引入 1.0 网络的路由,就可以通过分发列表来实现,通过过滤出站方向实现:

    1. 之前在 R2 上将 RIP 的路由引入到了 EIGRP 中,这时在引入后,加上一个分发列表的工具,仅需要 1.0 的网络在 EIGRP 中传递,R3 仅能收到 1.0 的网络去除 12 网络.
    # 声明 ACL 允许的范围
    Router2(config)#access-list 1 permit 1.1.1.1 0.255.255.255
    Router2(config)#router eigrp 100
    # 在 EIGRP 分发 RIP 的路由时,仅允许分发 access-list 为 1 的路由
    Router2(config-router)#distribute-list 1 out rip
    
    # 在 R4 上查看,仅能看到 1.0网络
    Route4r#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    O E2  1.0.0.0/8 [110/20] via 34.1.1.1, 00:00:25, Ethernet0/0
          4.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        4.4.4.0/24 is directly connected, Loopback0
    L        4.4.4.4/32 is directly connected, Loopback0
          23.0.0.0/24 is subnetted, 1 subnets
    O E2     23.1.1.0 [110/20] via 34.1.1.1, 00:14:03, Ethernet0/0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/0
    L        34.1.1.4/32 is directly connected, Ethernet0/0
    
    1. 在 R3 上 OSPF 中引入 EIGRP 路由时,加上分发列表工具,仅允许 1.0 网络在 OSPF 中传递,去除 23 网络.
    Router3(config)#access-list 1 permit 1.1.1.1 0.255.255.255
    Router3(config)#router ospf 100
    # ospf 在向外分发路由时,对于 eigrp 100 下学习到的路由来说,仅允许传递 access-list 为 1 的内容。
    Router3(config-router)#distribute-list 1 out eigrp 100
    
    # 在 R4 查看,23 网络的路由也没有:
    Router4>show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    O E2  1.0.0.0/8 [110/20] via 34.1.1.1, 00:14:52, Ethernet0/0
          4.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        4.4.4.0/24 is directly connected, Loopback0
    L        4.4.4.4/32 is directly connected, Loopback0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/0
    L        34.1.1.4/32 is directly connected, Ethernet0/0
    

    接着对于 R1 来说,除了收到 4.0 的网络,还收到了 23 和 34 的网络。这里在入站过滤,仅允许 4.0 的网络。

    1. 在 R2 上 EIGRP 引入路由入站时,加上分发列表,允许 4.0 网络,过滤 23,34 网络。
    Router2(config)#access-list 2 permit 4.4.4.0 0.0.0.255
    Router2(config)#router eigrp 100
    # 在 eigrp 中,仅引入 access-list 为 2 的路由
    Router2(config-router)#distribute-list 2 in
    
    # R2 查看,发现已经没有 34 网络
    Router2#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
    R     1.0.0.0/8 [120/1] via 12.1.1.1, 00:00:02, Ethernet0/0
          4.0.0.0/32 is subnetted, 1 subnets
    D EX     4.4.4.4 [170/2841600] via 23.1.1.3, 00:40:56, Ethernet0/1
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          23.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        23.1.1.0/24 is directly connected, Ethernet0/1
    L        23.1.1.1/32 is directly connected, Ethernet0/1
    
    1. 在 R1 上 RIP 引入路由入站时,加上分发列表,允许 4.0 网络,去除 23 网络。
    Router1(config)#access-list 2 permit 4.4.4.4 0.0.0.0
    Router1(config)#router rip
    # 在 ri[ 中,仅引入 access-list 为 2 的路由
    Router1(config-router)#distribute-list 2 in
    
    # 由于 rip 死亡时间较慢,可以先手动清空路由
    Router1#clear ip route *
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
          1.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        1.1.1.0/24 is directly connected, Loopback0
    L        1.1.1.1/32 is directly connected, Loopback0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
    

    前缀列表

    由于 ACL 只能匹配前缀信息,而不能匹配掩码。在遇到一些情况时无法做出正确的匹配。一般情况下使用前缀列表进行筛选路由。

    比如收到 10.1.1.0/2410.1.1.0/25 的两条路由。我们想要匹配 10.1.1.0/25 .

    通过 ACL,可以这样写 access-list 1 permit 10.1.1.0 0.0.0.0 但此时,两条路由都会匹配到。匹配的原因就在于无法对掩码进行区分。

    前缀列表写法:ip prefix-list [name] [seq] [permit | deny] [prefix/len] ge [min_length] le [max_length]

    • name:前缀列表的名字,为任意的名字或数字
    • seq:前缀列表条⽬的序号, 默认步张为 5
    • prefix:所匹配路由的前缀(⽹络部分)
    • len:所匹配路由前缀的bit⻓度
    • ge:⼤于等于
    • le:⼩于等于
    • min_length:路由掩码部分,最⼩的⼦⽹掩码的值
    • max_length:路由掩码部分,最⼤的⼦⽹掩码的值

    允许访问所有:ip prefix-list permit-all.pre permit 0.0.0.0/0 le 32

    这时可以使用前缀列表来进行匹配。匹配规则:

    1. 前缀列表由 IP 地址和掩码组成,IP 地址是网段或主机,掩码为正常掩码32位。
    2. 一个前缀列表可以有多条匹配规则,并且每天匹配规则都有一个序号,匹配时从序号小的开始匹配,匹配到就退出。
    3. 如果没有配置前缀列表的序号,会自动添加一个序号,默认是 5. 新的序列以步长为 5 增加。
    4. 如果前缀列表中的所有规则都没匹配成功,默认拒绝。因为默认情况下,会含有一条 deny 所有的原则。
    5. 如果应用不存在的前缀列表,默认就含有 permit 所有。

    # 由于传过来的 192.168.0.0 是 32 网段的,所以后面掩码的匹配要是 32
    Router1(config)#ip prefix-list pre01.pre seq 5 permit 192.168.0.0/24 ge 32 le 32
    Router1(config)#ip prefix-list pre01.pre seq 10  permit 192.168.1.0/24 ge 32 le 32
    Router1(config)#router eigrp 100
    Router1(config-router)#distribute-list prefix  pre01.pre in
    
    
    # 在 R1 查看路由:
    Router1#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
          10.0.0.0/8 is variably subnetted, 8 subnets, 2 masks
    C        10.1.0.0/24 is directly connected, Loopback0
    L        10.1.0.1/32 is directly connected, Loopback0
    C        10.1.1.0/24 is directly connected, Loopback1
    L        10.1.1.1/32 is directly connected, Loopback1
    C        10.1.2.0/24 is directly connected, Loopback2
    L        10.1.2.1/32 is directly connected, Loopback2
    C        10.1.3.0/24 is directly connected, Loopback3
    L        10.1.3.1/32 is directly connected, Loopback3
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
          192.168.0.0/32 is subnetted, 1 subnets
    D EX     192.168.0.1 [170/537600] via 12.1.1.2, 00:04:42, Ethernet0/0
          192.168.1.0/32 is subnetted, 1 subnets
    D EX     192.168.1.1 [170/537600] via 12.1.1.2, 00:02:15, Ethernet0/0
    
    # 在 R3 配置, 这里没有配置 ge le 是因为默认就是 24 网段
    Router3(config)#ip  prefix-list pre01.pre seq 5 permit 10.1.2.0/24
    Router3(config)#ip  prefix-list pre01.pre seq 10 permit 10.1.3.0/24
    Router3(config)#router ospf 100
    Router3(config-router)#distribute-list prefix  pre01.pre in
    
    # 在 R3 查询
    Router#show ip route
    Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
           D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
           N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
           E1 - OSPF external type 1, E2 - OSPF external type 2
           i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
           ia - IS-IS inter area, * - candidate default, U - per-user static route
           o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
           a - application route
           + - replicated route, % - next hop override, p - overrides from PfR
    
    Gateway of last resort is not set
    
          10.0.0.0/24 is subnetted, 2 subnets
    O E2     10.1.2.0 [110/20] via 23.1.1.2, 00:01:22, Ethernet0/0
    O E2     10.1.3.0 [110/20] via 23.1.1.2, 00:01:22, Ethernet0/0
          23.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        23.1.1.0/24 is directly connected, Ethernet0/0
    L        23.1.1.3/32 is directly connected, Ethernet0/0
          192.168.0.0/24 is variably subnetted, 2 subnets, 2 masks
    C        192.168.0.0/24 is directly connected, Loopback0
    L        192.168.0.1/32 is directly connected, Loopback0
          192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
    C        192.168.1.0/24 is directly connected, Loopback1
    L        192.168.1.1/32 is directly connected, Loopback1
          192.168.2.0/24 is variably subnetted, 2 subnets, 2 masks
    C        192.168.2.0/24 is directly connected, Loopback2
    L        192.168.2.1/32 is directly connected, Loopback2
          192.168.3.0/24 is variably subnetted, 2 subnets, 2 masks
    C        192.168.3.0/24 is directly connected, Loopback3
    L        192.168.3.1/32 is directly connected, Loopback3
    

    或者通过出站方向控制,在 R2 上:

    1. 将 OSPF 引入 EIGRP 时,控制 EIGRP 输出的路由。
    2. 将 EIGRP 引入 OSPF 时,控制 OSPF 输出的路由。
    Router(config)#ip prefix-list o2e.pre seq 5 permit 192.168.0.0/24 ge 32 le 32
    Router(config)#ip prefix-list o2e.pre seq 10 permit 192.168.1.0/24 ge 32 le 32
    Router(config)#ip prefix-list e2o.pre seq 5 permit 10.1.2.0/24
    Router(config)#ip prefix-list e2o.pre seq 10 permit 10.1.3.0/24
    
    Router2(config)#router ospf 100
    Router2(config-router)#distribute-list prefix o2e.pre out
    
    Router2(config)#router ospf 100
    Router2(config-router)#distribute-list prefix e2o.pre out
    

    Route-map

    route-map 的功能是当路由器发布,接受或者通过重分布引入路由时,可根据实际组网的需求实施一些策略,以便对路由信息进行过滤或者改变路由信息的一些属性。

    Route-map 特点:

    1. 动作有:permit ,deny
    2. 匹配时自上而下匹配,匹配到就退出。如果匹配不上,隐含一条拒绝所有。
    3. 使用命令方式,而不是编号。
    4. 每条 route-map 都有序号类似于前缀列表。
    5. 可以为 route-map 设置属性条件。

    R3 OSPF 中引入手动配置的静态路由,并更改在传递时的 Metric 值:

    # R3 去往 R4 的静态路由
    ip route 4.4.4.4 255.255.255.255 34.1.1.4
    ip route 10.1.0.0 255.255.255.0 34.1.1.4
    ip route 10.1.1.0 255.255.255.0 34.1.1.4
    ip route 10.1.2.0 255.255.255.0 34.1.1.4
    ip route 10.1.3.0 255.255.255.0 34.1.1.4
    
    # 声明 ACL 列表,用于匹配网段
    access-list 1 permit 4.4.4.4
    access-list 1 permit 10.1.2.0
    access-list 1 permit 10.1.1.0
    access-list 2 permit 10.1.3.0
    
    # 声明 route-map 列表,序号为 10 匹配满足 ACL 为 1 的流量,并设置其 Metric 为 100
    route-map s2o.map permit 10
     match ip address 1
     set metric 100
     
    # 声明 route-map 列表,序号为 20 匹配满足 ACL 为 2 的流量,并设置其 Metric 为 200
    route-map s2o.map permit 20
     match ip address 2
     set metric 200
     
    # 允许其他所有流量,序号为 30
    route-map s2o.map permit 30
    
    # 使用重分布引入路由静态路由,并使用 route-map 匹配
    router ospf 100
     router-id 3.3.3.3
     redistribute static subnets route-map s2o.map
     network 23.1.1.3 0.0.0.0 area 0
    

    R2 EIGRP 中引入 OSPF 的流量,并更改其 Metric

    # 声明 ACL,抓取流量
    access-list 1 permit 10.1.0.0 0.0.255.255
    
    # 编写 route-map 改变 metric
    route-map o2e.map permit 10
     match ip address 1
     match metric 100
     set metric 10000 1000 255 1 1500
     set tag 66666
    
    # 重分布 ospf 时,引用 route-map
    router eigrp 100
     network 12.1.1.2 0.0.0.0
     redistribute ospf 100 route-map o2e.map
    

    多点单向重分布引发次优的问题

    有时为了保证网络可靠性,我们会设置一条冗余的路由起到 backup,或者作为负载均衡使用。看下面这个场景,左面区域运行 OSPF,右面区域运行 EIGRP.

    这里想在 OSPF 网络中,访问 EIGRP 区域中的 4.4.4.4 网络。我们一般会采用路由重分布的方式将 EIGRP 路由,引入到 OSPF 网络中。

    在 R3 上查看下路由,由于 R3 上跑有 EIGRP 和 OSPF 两个协议,所以路由表会有两个协议的路由:

    R3#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    D EX     4.4.4.0 [170/409600] via 34.1.1.4, 00:00:05, Ethernet0/1
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [110/20] via 13.1.1.1, 00:15:34, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 00:18:36, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0/1
    
    

    可以发现,现在 R3 到 4.0 网络,所在路由为 D EX 4.4.4.0 [170/409600] via 34.1.1.4, 00:00:05, Ethernet0/1 作为 EIGRP 的外部路由引入。

    在 R2 上将 EIGRP 引入 OSPF:

    R2(config)#router ospf 100
    R2(config)#redistribute eigrp 100
    
    # 在 R1 上查看路由,发现 EIGRP 的路由作为 OSPF 的外部引入了
    Router#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    O E2     4.4.4.0 [110/20] via 12.1.1.2, 00:00:12, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/1
    L        13.1.1.1/32 is directly connected, Ethernet0/1
          24.0.0.0/24 is subnetted, 1 subnets
    O E2     24.1.1.0 [110/20] via 12.1.1.2, 00:00:12, Ethernet0/0
          34.0.0.0/24 is subnetted, 1 subnets
    O E2     34.1.1.0 [110/20] via 12.1.1.2, 00:00:12, Ethernet0/0
    

    在 R3 上查看路由:

    # 在 R3 查看路由,可以发现也引入到 R3 中
    R3#show ip route
    
          4.0.0.0/24 is subnetted, 1 subnets
    O E2     4.4.4.0 [110/20] via 13.1.1.1, 00:00:42, Ethernet0/0
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [110/20] via 13.1.1.1, 00:02:55, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 00:05:57, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0/1
    

    但可以发现出现了一个问题,原来 R3 到 4.0 网络是直接走 R4 的,但现在下一跳成为了 13.1.1.1 也就是 R1. 也就是说,现在 R3 到达 4.0 网络,会先走 R1 再走 R3 再走 R4.

    这个问题就是次优路由的问题,明明 R3 有更好的选择,却偏偏选择了较远的路由。

    产生这个问题的原因在于:R3 原来直接由 EIGRP 引入的外部路由的 AD 值为 170. 后来 R2 将 EIGRP 的路由引入到 OSPF 时,同时也把去往 4.0 网络的路由引入,但是作为 OSPF 的外部路由引入,而 OSPF 的外部路由 AD 为 110.

    此时 R3 就用两个选择,一个是 EIGRP 去往 4.0 外部路由,AD 为 170,另一个是 OSPF 去往 4.0 的外部路由,AD 为 110. 很明显,R3 会选择 AD 小的,所以就产生了次优的路径。

    这时如果在 R3 上,同样将 EIGRP 引入 OSPF:

    # R3
    R3(config)#router ospf 100
    R3(config-router)#redistribute eigrp 100
    
    # 在 R2 上查看路由:
    R2#show ip route
    
          4.0.0.0/24 is subnetted, 1 subnets
    D EX     4.4.4.0 [170/409600] via 24.1.1.4, 00:01:02, Ethernet0/1
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          13.0.0.0/24 is subnetted, 1 subnets
    O        13.1.1.0 [110/20] via 12.1.1.1, 03:18:56, Ethernet0/0
          24.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        24.1.1.0/24 is directly connected, Ethernet0/1
    L        24.1.1.2/32 is directly connected, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    D        34.1.1.0 [90/307200] via 24.1.1.4, 00:04:09, Ethernet0/1
    
    

    会发现 R2 到 4.0 路由,依然是 EIGRP AD=170 的外部路由。原因在于 R3 再做 EIGRP 到 OSPF 重分布时,只能重分布路由表中存在的路由,而此时 R3 路由表中存在的路由为:

          4.0.0.0/24 is subnetted, 1 subnets
    O E2     4.4.4.0 [110/20] via 13.1.1.1, 00:00:42, Ethernet0/0
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [110/20] via 13.1.1.1, 00:02:55, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 00:05:57, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0/1
    

    注意去往 4.0 网络的路由是 O E2,属于 OSPF,并不是 EIGRP,所以重分布并没有包含 4.0 的路由。所以 R2 到 4.0 还是之前 EIGRP 的路由。

    此时如果我们将 R2 的 E1/0 接口 shutdown. 此时 R2 运行的 EIGRP 协议就会失效,进而引入 OSPF 的 EIGRP 路由也会失效。所以 R3 会恢复 EIGRP 去往 4.0 的路由。

    R3#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    D EX     4.4.4.0 [170/409600] via 34.1.1.4, 00:00:06, Ethernet0/1
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [110/20] via 13.1.1.1, 04:23:17, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 04:26:19, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0/1
    
    

    而此时,由于 R3 已经做了重分布,所以 R2 会收到 AD=110 去往 4.0 的 OSPF 外部路由,所以 R2依然可以访问 4.0

    R2#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    O E2     4.4.4.0 [110/20] via 12.1.1.1, 00:02:02, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          13.0.0.0/24 is subnetted, 1 subnets
    O        13.1.1.0 [110/20] via 12.1.1.1, 03:27:36, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    O E2     24.1.1.0 [110/20] via 12.1.1.1, 00:02:02, Ethernet0/0
          34.0.0.0/24 is subnetted, 1 subnets
    

    接着将 R2 的 E1/0 接口打开,会发现去往 4.0 的路由不会发生变化:

    R2#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    O E2     4.4.4.0 [110/20] via 12.1.1.1, 00:04:18, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          13.0.0.0/24 is subnetted, 1 subnets
    O        13.1.1.0 [110/20] via 12.1.1.1, 03:29:52, Ethernet0/0
          24.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        24.1.1.0/24 is directly connected, Ethernet0/1
    L        24.1.1.2/32 is directly connected, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    O E2     34.1.1.0 [110/20] via 12.1.1.1, 00:04:18, Ethernet0/0
    

    这是因为,虽然 EIGRP 恢复了 4.0 的路由,但是恢复的路由 AD 为 170,比现在的 AD=110 的路由大,所以不会引入。

    而且由于 R2 上也配置了重分布,但和之前一样,只能重分布路由表中存在的路由,现在 R2 路由表去往 4.0 的路由是通过 OSPF 引入的,所以不会重新引入 OSPF.

    进而 R3 路由不会发生变化。

    解决方法1:配置路由过滤

    这里我们以开始时,R3 还没重分布前举例。此时出现次优的原因就是因为,R3 上明明有去往 4.0 的 EIGRP 路由,但是却收到了 OSPF 引入的 4.0 的路由。

    我们第一想法就是,在 R3 配置,拒绝掉 OSPF 去往 4.0 的路由就可以了。

    R3(config)#access-list 1 deny 4.4.4.0 0.0.0.255
    R3(config)#access-list 1 permit any
    R3(config)#router ospf 100
    R3(config-router)#distribute-list 1 in
    
    # 重新查看路由表,可以看到去往 4.0 网络的路由已经恢复了。
    R3#show ip route
          4.0.0.0/24 is subnetted, 1 subnets
    D EX     4.4.4.0 [170/409600] via 34.1.1.4, 00:00:53, Ethernet0/1
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 04:41:02, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0/1
    

    但此时有另外一个问题,如果由于网络问题,此时 R3 上 E0/1 的接口 down 了,此时 EIGRP 协议会失效。虽然 R2 也可以去往 4.0 网络,但由于分发列表已经拒绝了 4.0 的网络,所以此时 R3 就无法在访问 4.0 网络了。

    # 根本没有去往 4.0 网络的路由
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [110/20] via 13.1.1.1, 00:04:58, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    O E2     24.1.1.0 [110/20] via 13.1.1.1, 00:04:21, Ethernet0/0
          34.0.0.0/24 is subnetted, 1 subnets
    O E2     34.1.1.0 [110/20] via 13.1.1.1, 00:04:21, Ethernet0/0
    

    所以该方法虽然解决了次优的问题,但网络的可靠性无法保证。

    解决方法2:调整协议的 AD

    另一想法是,此现次优的原因就是因为,引入的 OSPF 的外部路由的 AD 小于 EIGRP 的 AD 值,所以将 OSPF 的 AD 调大就可以了。

    R3(config)#router ospf 100
    R3(config)#no distribute-list 1 in
    R3(config-router)#distance ospf exter 180
    
    # 查看 R3 路由表,选择 EIGRP 的外部路由
          4.0.0.0/24 is subnetted, 1 subnets
    D EX     4.4.4.0 [170/409600] via 34.1.1.4, 00:00:03, Ethernet0/1
          12.0.0.0/24 is subnetted, 1 subnets
    O        12.1.1.0 [180/20] via 13.1.1.1, 00:00:03, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/0
    L        13.1.1.3/32 is directly connected, Ethernet0/0
          24.0.0.0/24 is subnetted, 1 subnets
    D        24.1.1.0 [90/307200] via 34.1.1.4, 00:01:10, Ethernet0/1
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.3/32 is directly connected, Ethernet0
    

    最后在 R3 上开启重分布,然后在 R2 同样修改 OSPF 的 AD 值就可以了。

    多点双向重分布引发问题

    先构造出环路:

    在 R5 重分布引入 5.0 的网络

    !
    interface Loopback0
     ip address 5.5.5.5 255.255.255.255
    !
    router rip
     version 2
     redistribute connected metric 2
     network 15.0.0.0
     no auto-summary
    !
    

    先看红色逆时针方向,在 R3 将 RIP 引入 OSPF 中,让 OSPF 能访问 RIP 的网络:

    R3(config)#router ospf 100
    R3(config-router)#redistribute rip
    
    # R4 查看,多了 RIP 的路由
    R4#show ip route
          4.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        4.4.4.0/24 is directly connected, Loopback0
    L        4.4.4.4/32 is directly connected, Loopback0
    O E2  5.0.0.0/8 [110/20] via 34.1.1.3, 00:00:01, Ethernet0/1
    O E2  12.0.0.0/8 [110/20] via 34.1.1.3, 00:00:01, Ethernet0/1
          13.0.0.0/24 is subnetted, 1 subnets
    O E2     13.1.1.0 [110/20] via 34.1.1.3, 00:00:01, Ethernet0/1
    O E2  15.0.0.0/8 [110/20] via 34.1.1.3, 00:00:01, Ethernet0/1
          24.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        24.1.1.0/24 is directly connected, Ethernet0/0
    L        24.1.1.4/32 is directly connected, Ethernet0/0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.4/32 is directly connected, Ethernet0/1
    

    在 R2 上将 OSPF 引入 RIP,让 RIP 能访问 OSPF 的网络, 并设置 Metric 为 1:

    R2(config)#router rip
    R2(config-router)#redistribute ospf 100 metric 1
    

    现在在 R1 上查看路由, 发现去往 5.0 的网络的路由下一跳变为 12.1.1.2:

    R1#show ip route
    
    R     5.0.0.0/8 [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/1
    L        13.1.1.1/32 is directly connected, Ethernet0/1
          15.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        15.1.1.0/24 is directly connected, Ethernet0/2
    L        15.1.1.1/32 is directly connected, Ethernet0/2
    R     24.0.0.0/8 [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
    R     34.0.0.0/8 [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
    
    

    现在就构造出了环路,原因在于 R1 有两条去往 5.0 网络的路由,一条是 RIP 通过重分布直连得到 AD 为 120 ,Metric 为 2. 一条是 OSPF 引入 RIP 得到, AD 为 120,Metric 为1. 此时 R1 会选 Metric 为1的路由。

    R1 此时想访问 5.0 网络,会将数据包发给 R2,由于 5.0 网络是 R2 通过 OSPF 从 R3 学习到的,所以数据包会转发给 R4 到 R3. 在 R3 上 5.0 的网络是通过 RIP 引入 OSPF 得到的,而此时会将数据包再发给 R1. 至此出现环路。永远到不了 R5.

    同理看蓝色顺时针方向:

    在 R2 上将Rip 引入 OSPF :

    R2(config)#router ospf 100
    R2(config-router)#redistribute rip
    
    在 R4 上查看,发现又会多了几条到达相同网段的路由,第一次是通过 R3 引入的,这次是通过 R2引入的
    R4#show ip route
    
          4.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        4.4.4.0/24 is directly connected, Loopback0
    L        4.4.4.4/32 is directly connected, Loopback0
    O E2  5.0.0.0/8 [110/20] via 34.1.1.3, 00:02:00, Ethernet0/1
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    O E2     12.0.0.0/8 [110/20] via 34.1.1.3, 00:02:00, Ethernet0/1
    O E2     12.1.1.0/24 [110/20] via 24.1.1.2, 00:00:01, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    O E2     13.0.0.0/8 [110/20] via 24.1.1.2, 00:00:01, Ethernet0/0
    O E2     13.1.1.0/24 [110/20] via 34.1.1.3, 00:02:00, Ethernet0/1
    O E2  15.0.0.0/8 [110/20] via 34.1.1.3, 00:02:00, Ethernet0/1
          24.0.0.0/8 is variably subnetted, 3 subnets, 3 masks
    O E2     24.0.0.0/8 [110/20] via 34.1.1.3, 00:00:53, Ethernet0/1
    C        24.1.1.0/24 is directly connected, Ethernet0/0
    L        24.1.1.4/32 is directly connected, Ethernet0/0
          34.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        34.1.1.0/24 is directly connected, Ethernet0/1
    L        34.1.1.4/32 is directly connected, Ethernet0/1
    
    

    在 R3 上将 OSPF 引入 RIP:

    R3(config)#router rip
    R3(config-router)#redistribute ospf 100 metric 1
    
    在 R1 查看:发现多了去往 24,34 网段的路由,之前是由 R2 引入的,这次是由 R3 引入的。 去往 5.0 的路由没有变化,是因为之前 R3 是通过 RIP 引入 5.0 到 ospf,再从 ospf 引回到 RIP 是没有 5.5.5.5 路由的,因为是 5.0 就是 RIP.
    R1#show ip route
    
    R     5.0.0.0/8 [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/1
    L        13.1.1.1/32 is directly connected, Ethernet0/1
          15.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        15.1.1.0/24 is directly connected, Ethernet0/2
    L        15.1.1.1/32 is directly connected, Ethernet0/2
    R     24.0.0.0/8 [120/1] via 13.1.1.3, 00:00:06, Ethernet0/1
                     [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
    R     34.0.0.0/8 [120/1] via 13.1.1.3, 00:00:06, Ethernet0/1
                     [120/1] via 12.1.1.2, 00:00:12, Ethernet0/0
    

    此时在 R1 ping 5.5.5.5

    R1>traceroute 5.5.5.5
    Type escape sequence to abort.
    Tracing the route to 5.5.5.5
    VRF info: (vrf in name/id, vrf out name/id)
      1 12.1.1.2 0 msec 0 msec 0 msec
      2 24.1.1.4 1 msec 0 msec 0 msec
      3 34.1.1.3 0 msec 1 msec 0 msec
      4 13.1.1.1 1 msec 2 msec 4 msec
      5 12.1.1.2 4 msec 8 msec 2 msec
      6 24.1.1.4 2 msec 1 msec 1 msec
      7 34.1.1.3 0 msec 1 msec 1 msec
      8 13.1.1.1 1 msec 1 msec 0 msec
      9 12.1.1.2 0 msec 1 msec 1 msec
     10 24.1.1.4 1 msec 3 msec 1 msec
     11 34.1.1.3 0 msec 1 msec 2 msec
     12 13.1.1.1 1 msec 1 msec 1 msec
     # ctrl+shift+6 停止
    

    解决环路方法1:设置路由过滤,分发列表

    首先看红色逆时针方向,产生环路的原因是因为在 R2 上将 RIP 路由引入到 OSPF 上,但是在 R3 上又将 OSPF 引回到 RIP,而且引入的 metric 值更好,所以产生了环路。

    所以一个解决思路是,在 R3 上 RIP 重分布 OSPF 路由时,仅仅重分布 OSPF 自己的路由,不包含从 RIP 学习到的路由。同样 R2 上,OSPF 引入 RIP 路由,仅仅引入 RIP 自己的路由。

    在 R3 上声明前缀列表:

    # RIP 本身的路由
    R3(config)#ip prefix-list r2o.pre permit 5.5.5.5/24
    R3(config)#ip prefix-list r2o.pre permit 15.1.1.0/24
    R3(config)#ip prefix-list r2o.pre permit 12.1.1.0/24
    R3(config)#ip prefix-list r2o.pre permit 13.1.1.0/24
    
    # OSPF 本身的路由
    R3(config)# ip prefix-list o2r.pre permit 24.1.1.0/24
    R3(config)# ip prefix-list o2r.pre permit 34.1.1.0/24
    
    # RIP 重分布时,仅重分布 ospf 的路由
    R3(config)#router rip
    R3(config-router)#distribute-list prefix o2r.pre out ospf 100
    R3(config-router)#exit
    
    # ospf 重分布时,仅重分布 RIP 的路由
    R3(config)#router ospf 100
    R3(config-router)#distribute-list prefix r2o.pre out rip
    
    

    同理由于是双向,在蓝色顺时针方向,R3 上 OSPF 引入 RIP 路由时,仅引入 RIP 路由。R2 上 RIP 引入 OSPF 路由时,仅引入 OSPF 路由。

    在 R2 上声明前缀列表

    # RIP 本身的路由
    R2(config)#ip prefix-list r2o.pre permit 5.5.5.5/24
    R2(config)#ip prefix-list r2o.pre permit 15.1.1.0/24
    R2(config)#ip prefix-list r2o.pre permit 12.1.1.0/24
    R2(config)#ip prefix-list r2o.pre permit 13.1.1.0/24
    
    # OSPF 本身的路由
    R2(config)# ip prefix-list o2r.pre permit 24.1.1.0/24
    R2(config)# ip prefix-list o2r.pre permit 34.1.1.0/24
    
    # RIP 重分布时,仅重分布 ospf 的路由
    R2(config)#router rip
    R2(config-router)#distribute-list prefix o2r.pre out ospf 100
    R2(config-router)#exit
    
    # ospf 重分布时,仅重分布 RIP 的路由
    R2(config)#router ospf 100
    R2(config-router)#distribute-list prefix r2o.pre out rip
    

    在 R1 查看,路由已经恢复:

          5.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    R        5.0.0.0/8 is possibly down,
              routing via 15.1.1.5, Ethernet0/2
    R        5.5.5.0/24 [120/2] via 15.1.1.5, 00:00:24, Ethernet0/2
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.1/32 is directly connected, Ethernet0/0
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/1
    L        13.1.1.1/32 is directly connected, Ethernet0/1
          15.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        15.1.1.0/24 is directly connected, Ethernet0/2
    L        15.1.1.1/32 is directly connected, Ethernet0/2
          24.0.0.0/24 is subnetted, 1 subnets
    R        24.1.1.0 [120/1] via 13.1.1.3, 00:00:18, Ethernet0/1
                      [120/1] via 12.1.1.2, 00:00:16, Ethernet0/0
          34.0.0.0/24 is subnetted, 1 subnets
    R        34.1.1.0 [120/1] via 13.1.1.3, 00:00:18, Ethernet0/1
                      [120/1] via 12.1.1.2, 00:00:16, Ethernet0/0
    

    在 R2 查看路由:发现还有一个问题,就是 R2 走 5.5.5.5 时,选择了 Ethernet 0/1 口,而不是选 RIP 去往 5.0 的路由。

    R2>show ip route
    
          5.0.0.0/24 is subnetted, 1 subnets
    O E2     5.5.5.0 [110/60] via 24.1.1.4, 00:05:19, Ethernet0/1
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          13.0.0.0/24 is subnetted, 1 subnets
    O E2     13.1.1.0 [110/60] via 24.1.1.4, 00:05:19, Ethernet0/1
          15.0.0.0/24 is subnetted, 1 subnets
    O E2     15.1.1.0 [110/60] via 24.1.1.4, 00:05:19, Ethernet0/1
          24.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        24.1.1.0/24 is directly connected, Ethernet0/1
    L        24.1.1.2/32 is directly connected, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    O        34.1.1.0 [110/20] via 24.1.1.4, 00:05:19, Ethernet0/1
    
    

    原因在于 R3 先将 RIP 引入 OSPF,然后在 R3 有两个去往 5.0 网络的路由,一个是 RIP AD=120,另一个是 OSPF 学到的外部路由 AD=110. 所以 R3 会选择 110的. 产生次优路径,同样如果 R2 先将 RIP 引入 OSPF,则 R3 也会产生次优。

    解决方法就是调整 OSPF 外部路由的 AD 值:

    R2(config)#router ospf 100
    R2(config)#distance ospf external 130
    
    # 在 R2 查看:选择 RIP 去往 5.0 
    R2#show ip route
          5.0.0.0/24 is subnetted, 1 subnets
    R        5.5.5.0 [120/3] via 12.1.1.1, 00:00:02, Ethernet0/0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/0
    L        12.1.1.2/32 is directly connected, Ethernet0/0
          13.0.0.0/24 is subnetted, 1 subnets
    R        13.1.1.0 [120/1] via 12.1.1.1, 00:00:02, Ethernet0/0
          15.0.0.0/24 is subnetted, 1 subnets
    R        15.1.1.0 [120/1] via 12.1.1.1, 00:00:02, Ethernet0/0
          24.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        24.1.1.0/24 is directly connected, Ethernet0/1
    L        24.1.1.2/32 is directly connected, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    O        34.1.1.0 [130/20] via 24.1.1.4, 00:00:02, Ethernet0/1
    
    

    同样调整 R3 的 AD:

    R2(config)#router ospf 100
    R2(config)#distance ospf external 130
    

    但如果路由表存在的路由较多,写分发列表较为困难。

    解决解决环路方法2:使用 road-map 打 tag

    先看红色顺时针方向:

    在 R3 上,OSPF 引入 RIP 的路由时,我们将引入的 RIP 路由设置一个标记,然后在 R2 上 RIP 引入 OSPF 时,发现如果是由这个标记的就说明是 RIP 本身的路由然后给拒绝掉。

    同样在 RIP 引入 OSPF 时,给 OSPF 打上另外一个标记,在 OSPF 引入 RIP 时,发现这个标记认为是 OSPF 自己的,所以拒绝掉。

    同理蓝色逆时针方向:

    在 R3 上 RIP 引入 OSPF 时,给 OSPF 打上标记。在 R2 OSPF 引入 RIP时,拒绝掉该标记。

    在 R2 OSPF 引入 RIP 时,给 RIP 打上标记,然后 R3 OSPF 引入RIP 时,拒绝掉该标记。

    # R3
    !
    route-map r2o.map deny 10
     match tag 102
    !
    route-map r2o.map permit 20
     set tag 101
    !
    route-map o2r.map deny 10
     match tag 202
    !
    route-map o2r.map permit 20
     set tag 201
    !
    router ospf 100
     router-id 3.3.3.3
     redistribute rip subnets route-map r2o.map
     network 34.1.1.3 0.0.0.0 area 0
     distance ospf external 130
    !
    router rip
     version 2
     redistribute ospf 100 metric 1 route-map o2r.map
     network 13.0.0.0
     no auto-summary
    !
    ip forward-protocol nd
    !
    
    # R2
    !
    route-map o2r.map deny 10
     match tag 101
    !
    route-map o2r.map permit 20
     set tag 102
    !
    route-map r2o.map deny 10
     match tag 201
    !
    route-map r2o.map permit 20
     set tag 202
    !
    !
    router ospf 100
     router-id 2.2.2.2
     redistribute rip subnets route-map r2o.map
     network 24.1.1.2 0.0.0.0 area 0
     distance ospf external 130
    !
    router rip
     version 2
     redistribute ospf 100 metric 1 route-map o2r.map
     network 12.0.0.0
     no auto-summary
    !
    

    总结:

    1. 注意 redistribute Protocol时,只有路由表中存在的该 Protocol 路由才能被重分布。
    2. 路由表中,存在的路由和在该路由器上,多个协议重分布的顺序有关。

    策略路由

    注意上面的重分布,分发列表,前缀列表,road-map 等都是路由策略,是对路由本身进行操作。修改 metric,打 tag ,过滤路由等。核心是通过调整路由表的路由,而影响选路。

    策略路由(Policy base route),策略优于路由,转发时,先看策略,然后根据策略转发。如果没有策略,再根据路由表转发。

    应用场景:在某些情况下,不想通过路由器的路由表进行转发,而是通过人为的策略进行转发,比如如下的场景。

    对于这四台路由器来说,由于运行 OPSF 协议,同时 OSPF 是基于链路状态的协议,会根据带宽等计算 Metric, 由于 R1 R2 R4 上行链路的带宽明显大的多,所以在去往 R4 后的路由,在 R1 的路由表中会优先选择上面的链路。

    但这样下行链路就没法被使用,希望将一些流量小的业务迁移至下行链路。

    具体来说,希望实现:

    1. PC 7 访问 PC8 走,R1 - R2 - R4. 模拟大流量。
    2. PC 6 访问 PC8 走,R1 - R3 - R4.模拟小流量。
    3. R1 lo 0 口访问 PC8 走,R1 - R2 - R4。
    4. R1 lo 1 口访问 PC8 走,R1 - R3 - R4。

    预配:

    PC6
    PC6>ip 192.168.1.1 255.255.255.0 gateway 192.168.1.254
    
    PC7
    PC7>ip 192.168.1.2 255.255.255.0 gateway 192.168.1.254
    
    PC8
    PC8>ip 192.168.1.1 255.255.255.0 gateway 192.168.2.254
    
    在 R1 通告 5 个网段(192,12,13,10,20,),R4 通告 3 个网段,其余通告 2 个网段。
    

    在 R1 上查看 PC8 的路由, 由于 metric 相同,所以会有两条路由:

    R1#show ip route
          10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        10.1.1.0/24 is directly connected, Loopback0
    L        10.1.1.1/32 is directly connected, Loopback0
          12.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        12.1.1.0/24 is directly connected, Ethernet0/1
    L        12.1.1.1/32 is directly connected, Ethernet0/1
          13.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        13.1.1.0/24 is directly connected, Ethernet0/2
    L        13.1.1.1/32 is directly connected, Ethernet0/2
          20.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
    C        20.1.1.0/24 is directly connected, Loopback1
    L        20.1.1.1/32 is directly connected, Loopback1
          24.0.0.0/24 is subnetted, 1 subnets
    O        24.1.1.0 [110/20] via 12.1.1.2, 00:19:52, Ethernet0/1
          34.0.0.0/24 is subnetted, 1 subnets
    O        34.1.1.0 [110/20] via 13.1.1.3, 00:16:37, Ethernet0/2
          192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks
    C        192.168.1.0/24 is directly connected, Ethernet0/0
    L        192.168.1.254/32 is directly connected, Ethernet0/0
    O     192.168.2.0/24 [110/30] via 13.1.1.3, 00:10:32, Ethernet0/2
                         [110/30] via 12.1.1.2, 00:10:32, Ethernet0/1
    
    

    现在将上行链路的带宽调大到 100 M,默认是 10 M:

    R1(config)#int e 0/1
    R1(config-if)#ip ospf cost 1
    
    R2(config)#int e 0/1
    R2(config-if)#ip ospf cost 1
    
    # 在 R1 上查看新的路由,去往 2.0 的网络的下一跳为 R2:
    R1#show ip route
    O     192.168.2.0/24 [110/12] via 12.1.1.2, 00:00:04, Ethernet0/1
    
    # 在 PC6 上 trace,也可以发现走上行链路
    PC6> trace 192.168.2.1
    trace to 192.168.2.1, 8 hops max, press Ctrl+C to stop
     1   192.168.1.254   1.206 ms  0.689 ms  1.005 ms
     2   12.1.1.2   1.425 ms  1.097 ms  2.378 ms
     3   24.1.1.4   1.282 ms  1.137 ms  1.096 ms
     4   *192.168.2.1   1.116 ms (ICMP type:3, code:3, Destination port unreachable)
    

    进行 PBR 配置

    1. 通过 ACL 抓取路由
    2. 通过 road-map 修改下一跳
    3. 通过在接口或者全局应用 PBR
    # R1 配置 ACL 抓取
    R1(config)#ip access-list extended PC1
    R1(config-ext-nacl)#permit ip 192.168.1.1 0.0.0.0 192.168.2.1 0.0.0.0
    R1(config-ext-nacl)#exit
    
    R1(config)#ip access-list extended PC2
    R1(config-ext-nacl)#permit ip 192.168.1.2 0.0.0.0 192.168.2.1 0.0.0.0
    R1(config-ext-nacl)#end
    
    # 配置 road-map
    
    R1(config)#route-map PBR.map permit 10
    R1(config-route-map)#match ip address PC1
    R1(config-route-map)#set ip next-hop 12.1.1.2
    R1(config-route-map)#exit
    
    R1(config)#route-map PBR.map permit 20
    R1(config-route-map)#match ip address PC2
    R1(config-route-map)#set ip next-hop 13.1.1.3
    
    # 在接口上调用
    R1(config)#int e 0/0
    R1(config-if)#ip policy route-map PBR.map
    
    
    # PC6 上查看,走上行
    PC6> trace 192.168.2.1
    trace to 192.168.2.1, 8 hops max, press Ctrl+C to stop
     1   192.168.1.254   0.372 ms  0.330 ms  0.268 ms
     2   12.1.1.2   0.983 ms  0.346 ms  0.754 ms
     3   24.1.1.4   2.510 ms  1.235 ms  2.367 ms
     4   *192.168.2.1   1.945 ms (ICMP type:3, code:3, Destination port unreachable)
    
    # PC 7 查看,走下行
    PC7> trace 192.168.2.1
    trace to 192.168.2.1, 8 hops max, press Ctrl+C to stop
     1   192.168.1.254   0.678 ms  0.582 ms  0.902 ms
     2   13.1.1.3   1.059 ms  1.399 ms  2.538 ms
     3   34.1.1.4   1.001 ms  0.765 ms  0.702 ms
     4   *192.168.2.1   0.849 ms (ICMP type:3, code:3, Destination port unreachable)
    

    但这时,如果我们将 R1 0/2 的接口断开,按照现在 R1 策略的写法,PC 7 的数据包应该到不了 PC8,但是结果是可以到的。

    原因在于:

    根据 PBR 转发前,会检查 next-hop 是否可达,如果可达,则根据 PBR 转发,如果不可达,则根据路由表转发。

    假如想在 R1 上的回环口按照目前的策略访问 PC8 进行测试,但由于这是 R1 是始发者,无法进入 e0/1 口。这时就需要配置全局应用策略:

    # R1 配置 ACL 抓取
    R1(config)#ip access-list extended Lo0
    R1(config-ext-nacl)#permit ip 10.1.1.1 0.0.0.0 192.168.2.1 0.0.0.0
    R1(config-ext-nacl)#exit
    
    R1(config)#ip access-list extended Lo1
    R1(config-ext-nacl)#permit ip 20.1.1.1 0.0.0.0 192.168.2.1 0.0.0.0
    R1(config-ext-nacl)#end
    
    # 配置 road-map
    
    R1(config)#route-map LoPBR.map permit 10
    R1(config-route-map)#match ip address Lo0
    R1(config-route-map)#set ip next-hop 12.1.1.2
    R1(config-route-map)#exit
    
    R1(config)#route-map LoPBR.map permit 20
    R1(config-route-map)#match ip address Lo1
    R1(config-route-map)#set ip next-hop 13.1.1.3
    
    # 全局上调用
    R1(config-if)#ip local policy route-map PBR.map
    
    # 使用 Lo0 访问
    R1#traceroute 192.168.2.1 source lo0
    Type escape sequence to abort.
    Tracing the route to 192.168.2.1
    VRF info: (vrf in name/id, vrf out name/id)
      1 12.1.1.2 1 msec 1 msec 1 msec
      2 24.1.1.4 2 msec 1 msec 0 msec
      3 192.168.2.1 2 msec 1 msec 0 msec
    
    # 使用 Lo1 访问 
    R1#traceroute 192.168.2.1 source lo1
    Type escape sequence to abort.
    Tracing the route to 192.168.2.1
    VRF info: (vrf in name/id, vrf out name/id)
      1 12.1.1.2 2 msec 1 msec 1 msec
      2 24.1.1.4 1 msec 1 msec 2 msec
      3 192.168.2.1 1 msec 2 msec 2 msec
    

    如果想要改变经过自己的数据包的走向,在接口下配置 PBR。

    如果想要改变自己始发数据包的走向,在全局配置 PBR。

  • 相关阅读:
    OpenCV 使用FLANN进行特征点匹配
    OpenCV 特征描述
    OpenCV 特征点检测
    OpenCV 亚像素级的角点检测
    OpenCV Shi-Tomasi角点检测子
    OpenCV Harris 角点检测子
    OpenCV 模板匹配
    OpenCV 直方图计算
    OpenCV 直方图均衡化
    OpenCV 仿射变换
  • 原文地址:https://www.cnblogs.com/michael9/p/13708781.html
Copyright © 2011-2022 走看看