路由策略

路由重分佈

在使用多種網絡協議的網絡中,路由重分佈允許將一個或多個路由器從其他路由進程或協議中學到的路由(靜態,動態),將路由通告到自己的進程中。

應用場景:

遷移網絡(割接):比如在原來在的網絡中運行 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。