(一)、原理
1. 前言
VRRP(Virtual Router Redundancy Protocol)協議是用於實現路由器冗餘的協議,最新協議在RFC3768中定義,原來的定義RFC2338被廢除,新協議相對還簡化了一些功能。
2. 協議說明
2.1 協議
VRRP協議是爲消除在靜態缺省路由環境下的缺省路由器單點故障引起的網絡失效而設計的主備模式的協議,使得在發生故障而進行設備功能切換時可以不影響內外數據通信,不需要再修改內部網絡的網絡參數。VRRP協議需要具有IP地址備份,優先路由選擇,減少不必要的路由器間通信等功能。
VRRP協議將兩臺或多臺路由器設備虛擬成一個設備,對外提供虛擬路由器IP(一個或多個),而在路由器組內部,如果實際擁有這個對外IP的路由器如果工作正常的話就是MASTER,或者是通過算法選舉產生,MASTER實現針對虛擬路由器IP的各種網絡功能,如ARP請求,ICMP,以及數據的轉發等;其他設備不擁有該IP,狀態是BACKUP,除了接收MASTER的VRRP狀態通告信息外,不執行對外的網絡功能。當主機失效時,BACKUP將接管原先MASTER的網絡功能。
配置VRRP協議時需要配置每個路由器的虛擬路由器ID(VRID)和優先權值,使用VRID將路由器進行分組,具有相同VRID值的路由器爲同一個組,VRID是一個0~255的正整數;同一組中的路由器通過使用優先權值來選舉MASTER,優先權大者爲MASTER,優先權也是一個0~255的正整數。
VRRP協議使用多播數據來傳輸VRRP數據,VRRP數據使用特殊的虛擬源MAC地址發送數據而不是自身網卡的MAC地址,VRRP運行時只有MASTER路由器定時發送VRRP通告信息,表示MASTER工作正常以及虛擬路由器IP(組),BACKUP只接收VRRP數據,不發送數據,如果一定時間內沒有接收到MASTER的通告信息,各BACKUP將宣告自己成爲MASTER,發送通告信息,重新進行MASTER選舉狀態。
2.2 MASTER選舉
如果對外的虛擬路由器IP就是路由器本身配置的IP地址的話,該路由器始終都是MASTER;否則如果不具備虛擬IP的話,將進行MASTER選舉,各路由器都宣告自己是MASTER,發送VRRP通告信息,如果收到其他機器的發來的通告信息的優先級比自己高,將轉回BACKUP狀態,如果相等的話,將比較路由器的實際IP,IP值較大的優先權高。不過如果對外的虛擬路由器IP就是路由器本身的IP的話,該路由器始終將是MASTER,這時的優先級值爲255。
2.3 協議狀態機
VRRP協議狀態比較簡單,就三種狀態,初始化,主機,備份機。
+---------------+
+--------->| || |
| Master | | Backup |
| |
初始化:
路由器啓動時,如果路由器的優先級是255(最高優先級,路由器擁有路由器地址),要發送VRRP通告信息,併發送廣播ARP信息通告路由器IP地址對應的MAC地址爲路由虛擬MAC,設置通告信息定時器準備定時發送VRRP通告信息,轉爲MASTER狀態;否則進入BACKUP狀態,設置定時器檢查定時檢查是否收到MASTER的通告信息。
主機:
主機狀態下的路由器要完成如下功能:
設置定時通告定時器;
用VRRP虛擬MAC地址響應路由器IP地址的ARP請求;
轉發目的MAC是VRRP虛擬MAC的數據包;
如果是虛擬路由器IP的擁有者,將接受目的地址是虛擬路由器IP的數據包,否則丟棄;
當收到shutdown的事件時刪除定時通告定時器,發送優先權級爲0的通告包,轉初始化狀態;
如果定時通告定時器超時時,發送VRRP通告信息;
收到VRRP通告信息時,如果優先權爲0,發送VRRP通告信息;否則判斷數據的優先級是否高於本機,或相等而且實際IP地址大於本地實際IP,設置定時通告定時器,復位主機超時定時器,轉BACKUP狀態;否則的話,丟棄該通告包;
備機:
備機狀態下的路由器要實現以下功能:
設置主機超時定時器;
不能響應針對虛擬路由器IP的ARP請求信息;
丟棄所有目的MAC地址是虛擬路由器MAC地址的數據包;
不接受目的是虛擬路由器IP的所有數據包;
當收到shutdown的事件時刪除主機超時定時器,轉初始化狀態;
主機超時定時器超時的時候,發送VRRP通告信息,廣播ARP地址信息,轉MASTER狀態;
收到VRRP通告信息時,如果優先權爲0,表示進入MASTER選舉;否則判斷數據的優先級是否高於本機,如果高的話承認MASTER有效,復位主機超時定時器;否則的話,丟棄該通告包;
2.4 ARP查詢處理
當內部主機通過ARP查詢虛擬路由器IP地址對應的MAC地址時,MASTER路由器回覆的MAC地址爲虛擬的VRRP的MAC地址,而不是實際網卡的MAC地址,這樣在路由器切換時讓內網機器覺察不到;而在路由器重新啓動時,不能主動發送本機網卡的實際MAC地址。如果虛擬路由器開啓的ARP代理(proxy_arp)功能,代理的ARP迴應也迴應VRRP虛擬MAC地址;
2.5 VRRP應用舉例
+-----------+ +-----------+
| Rtr1 | | Rtr2 |
|(MR VRID=1)| |(BR VRID=1)|
|(BR VRID=2)| |(MR VRID=2)|
VRID=1 +-----------+ +-----------+ VRID=2
IP A ---------->* *
這是通常VRRP使用拓撲,兩臺路由器運行VRRP互爲備份,路由器1作爲VRID組1的MASTER,IP地址A,VRID組2的BACKUP,路由器2作爲VRID組2的MASTER,IP地址B,VRID組1的BACKUP,內部網絡中一部分機器的缺省網關地址是IP地址A,一部分是IP地址B,正常情況下以A爲網關的數據將走路由器1,以B爲網關的數據將走路由器2,如果一臺路由器發生故障,所有數據將走另一臺路由器。
3. 協議定義
3.1 以太頭
源MAC地址必須爲虛擬MAC地址:00-00-5E-00-01-{VRID},VRID爲虛擬路由器ID值,16進制格式,所以同一網段中最多有255個VRRP路由器;目的MAC爲多播類型的MAC。
3.2 IP頭參數
VRRP包的源地址是本機地址,目的地址必須爲224.0.0.18,爲一多播地址;IP協議號爲112;IP包的TTL值必須爲255。
3.3 VRRP協議數據格式
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| Type | Virtual Rtr ID| Priority | Count IP Addrs|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Auth Type | Adver Int | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| IP Address (1) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| . |
| . |
| . |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| IP Address (n) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication Data (1) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Authentication Data (2) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
其中:
version:版本,4位,在RFC3768中定義爲2;
Type:類型,4位,目前只定義一種類類型:通告數據,取值爲1;
Virtual Rtr ID:虛擬路由器ID,8位
Priority:優先級,8位,具備冗餘IP地址的設備的優先級爲255;
Count IP Addrs:VRRP包中的IP地址數量,8位;
Auth Type:認證類型,8位,RFC3768中認證功能已經取消,此字段值定義0(不認證),爲1,2只作爲對老版本的兼容;
Adver Int:通告包的發送間隔時間,8位,單位是秒,缺省是1秒;
Checksum:校驗和,16位,校驗數據範圍只是VRRP數據,即從VRRP的版本字段開始的數據,不包括IP頭;
IP Address(es):和虛擬路由器相關的IP地址,數量由Count IP Addrs決定
Authentication Data:RFC3768中定義該字段只是爲了和老版本兼容,必須置0。
3.4 接收數據時的必須檢查
收到VRRP數據包時要進行以下驗證,不滿足的數據包將被丟棄:
- TTL必須爲255;
- VRRP版本號必須爲2;
- 一個包中數據字段必須完整;
- 校驗和必須正確;
- 必須驗證在接收的網卡上配置了VRID值,而且本地路由器不是路由IP地址的擁有者
- 必須驗證VVRP認證類型和配置的一致;
4. 結論
VRRP實現了對路由器IP地址的冗餘功能,防止了單點故障造成的網絡失效,VRRP本身是熱備形式的,但可以通過互相熱備實現路由器的均衡處理,新版的VRRP較老版簡化了認證處理,實際不再進行數據的認證,這是因爲在實際應用中經常出現認證成爲造成多個MASTER同時使用的異常情況
(二)、案例
1、案例一 思科
1)拓撲
2) 配置:
R1:
interface FastEthernet0/0.10
encapsulation dot1Q 10
ip address 192.168.10.1 255.255.255.0
ip nat inside
vrrp 1 ip 192.168.10.1
interface FastEthernet0/0.20
encapsulation dot1Q 20
ip address 192.168.20.1 255.255.255.0
ip nat inside
vrrp 2 ip 192.168.20.254
interface FastEthernet2/0
ip address 10.10.10.1 255.255.255.252
ip nat outside
ip route 0.0.0.0 0.0.0.0 10.10.10.2
access-list 20 permit 192.168.0.0 0.0.255.255
ip nat inside source list 20 interface FastEthernet2/0 overload
R2:
interface FastEthernet0/0.10
encapsulation dot1Q 10
ip address 192.168.10.254 255.255.255.0
ip nat inside
ip virtual-reassembly
vrrp 1 ip 192.168.10.1
track 1 interface fastEthernet 2/0 line-protocol 建立跟蹤組
interface FastEthernet0/0.20
encapsulation dot1Q 20
ip address 192.168.20.254 255.255.255.0
ip nat inside
ip virtual-reassembly
vrrp 2 ip 192.168.20.254
vrrp 2 track 1 de 160 應用跟蹤組並作出動作
interface FastEthernet2/0
ip address 20.20.20.1 255.255.255.252
ip nat outside
ip route 0.0.0.0 0.0.0.0 20.20.20.2
access-list 20 permit 192.168.0.0 0.0.255.255
ip nat inside source list 20 interface FastEthernet2/0 overload
R3:
interface Loopback1
ip address 100.100.100.100 255.255.255.0
interface FastEthernet0/0
ip address 20.20.20.2 255.255.255.252
interface FastEthernet2/0
ip address 10.10.10.2 255.255.255.252
SW1:
interface FastEthernet1/1 - 2
switchport mode trunk
channel-group 1 mode on
interface FastEthernet1/3 - 5
switchport access vlan 20
interface range FastEthernet1/6 - 8
switchport access vlan 10
interface FastEthernet1/11
switchport mode trunk
interface Vlan1
no ip address
interface Vlan10
ip address 192.168.10.100 255.255.255.0
ip route 0.0.0.0 0.0.0.0 192.168.10.1
SW2:
interface range FastEthernet1/1 -2
switchport mode trunk
channel-group 1 mode on
interface range FastEthernet1/3 - 5
switchport access vlan 10
interface range FastEthernet1/6 - 8
switchport access vlan 20
interface FastEthernet1/12
switchport mode trunk
2、案例二
1)拓撲:
2) 實驗要求:
1、 R14和R5上運行vrrp使得vlan10從R14訪問外網,使得vlan20從R5訪問外網
2、 R14和R5上運行nat實現內網用戶訪問外網
3、 在R14和R5上配置單臂路由實現vlan間通信
4、 SW4和SW6上有vlan10和vlan20,f0/1和f0/2是鏈路聚合
3) 配置:
R5:
interface Ethernet0
ip address 192.168.101.5 255.255.255.0
interface Ethernet1.10
vlan-type dot1q vid 10
ip address 192.168.10.254 255.255.255.0
vrrp vrid 10 virtual-ip 192.168.10.1
interface Ethernet1.20
vlan-type dot1q vid 20
ip address 192.168.20.254 255.255.255.0
vrrp vrid 20 virtual-ip 192.168.20.254
vrrp vrid 20 track Serial1 reduced 160
acl 2000 match-order auto
rule normal permit source any
interface Serial1
ip address 20.20.20.1 255.255.255.0
nat outbound 2000 interface
R14
interface Ethernet1.10
vlan-type dot1q vid 10
ip address 192.168.10.1 255.255.255.0
vrrp vrid 10 virtual-ip 192.168.10.1
vrrp vrid 10 track Serial0 reduced 200
interface Ethernet1.20
vlan-type dot1q vid 20
ip address 192.168.20.1 255.255.255.0
vrrp vrid 20 virtual-ip 192.168.20.254
[r14-Ethernet1.20]vrrp vrid 20 virtual-ip 192.168.20.254
檢測,如果網絡端口,自己
[r14-Ethernet1.10]vrrp vrid 10 track s0 reduced 200
interface Serial0
ip address 10.10.10.1 255.255.255.0
[r14-Serial0]nat outbound 2000 interface
[r14]ip route-static 0.0.0.0 0.0.0.0 10.10.10.2
ISP路由的配置
interface Serial0
ip address 10.10.10.2 255.255.255.0
interface Serial1
ip address 20.20.20.2 255.255.255.0
interface LoopBack1
ip address 192.168.100.100 255.255.255.0
s4:
vlan 10
vlan 20
interface Vlan-interface1
ip address 192.168.101.24 255.255.255.0
link-aggregation group 1 mode manual
interface Ethernet1/0/1
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
interface Ethernet1/0/2
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
interface Ethernet1/0/3
port link-type trunk
port trunk permit vlan all
interface Ethernet1/0/4
port access vlan 10
interface Ethernet1/0/5
port access vlan 10
interface Ethernet1/0/6
port access vlan 10
interface Ethernet1/0/7
port access vlan 10
interface Ethernet1/0/8
port access vlan 10
interface Ethernet1/0/9
port access vlan 10
interface Ethernet1/0/10
port access vlan 10
interface Ethernet1/0/11
port access vlan 20
interface Ethernet1/0/12
port access vlan 20
interface Ethernet1/0/13
port access vlan 20
interface Ethernet1/0/14
port access vlan 20
interface Ethernet1/0/15
port access vlan 20
S6:
link-aggregation group 1 mode manual
vlan 10
vlan 20
interface Vlan-interface1
ip address 192.168.101.26 255.255.255.0
interface Aux1/0/0
interface Ethernet1/0/1
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
interface Ethernet1/0/2
port link-type trunk
port trunk permit vlan all
port link-aggregation group 1
interface Ethernet1/0/3
port link-type trunk
port trunk permit vlan all
interface Ethernet1/0/4
port access vlan 10
interface Ethernet1/0/5
port access vlan 10
interface Ethernet1/0/6
port access vlan 10
interface Ethernet1/0/7
port access vlan 10
interface Ethernet1/0/8
port access vlan 10
interface Ethernet1/0/9
port access vlan 10
interface Ethernet1/0/10
port access vlan 10
interface Ethernet1/0/11
port access vlan 20
interface Ethernet1/0/12
port access vlan 20
interface Ethernet1/0/13
port access vlan 20
interface Ethernet1/0/14
port access vlan 20
interface Ethernet1/0/15
port access vlan 20