GRE:通用路由封裝,全稱 Generic Routing Encapsulation,也算是×××技術的一種。它能快速、簡單地跨越Internet(公網)實現私網之間連通,特別是在跨越公網組建大的網絡。但GRE致命的缺點就是,他是明文傳輸數據。所以在部署GRE的時候,往往結合各種安全措施來保護數據,如利用Ipsec對其數加密。本文介紹GRE的一些簡單的實驗。
GRE的原理並不復雜,它只是在私網IP包頭前再封裝一個公網的IP包頭,而在公網上傳輸,公網的路由器看的只是公網的IP包頭的IP,不會看到私網的IP包頭,直到到達目標路由器,目標路由器接受到數據后,去除公網IP包頭發現是GRE數據,那么它就將原封不動這個私網IP數據傳送到內網中(只是IP包頭以上的數據不動,改變的是二層報頭)。
GRE隧道特點:
傳輸(IP)報頭 | GRE報頭 | 乘客協議(IP)分組 |
---|
- GRE報頭包含一個協議類型字段,因此可通過隧道傳輸任意第3層協議的數據。
- GRE是無狀態的,也沒有流量控制機制。
- GRE不提供任何安全機制。
- GRE會帶來額外開銷,每個分組至少24字節。
隧道傳遞數據包的過程分為3步:
1.接受原始數據包當作乘客協議,原始數據包包頭的IP地址為私有IP地址
2.將原始IP數據包封裝進GRE協議,GRE協議稱為封裝協議,封裝的包頭IP地址為虛擬直連鏈路兩端的IP地址
3.將整個GRE數據包當作數據,在外層封裝公網IP包頭,也就是隧道的起源和終點,從而路由到隧道終點
實驗拓撲:
R1:模擬北京總部客戶端
R2:模擬北京總部
R3:模擬ISP
R4:模擬上海分部
R5:模擬上海分部客戶端
RI和R5設備上啟用環回口代表接入設備
R1和R2,R4和R5內部建立ospf傳遞路由
R3代表ISP采用直連路由模擬
R2和R4中默認路由路由接入ISP
R2和R4建立GRE隧道
最終目的使R1和R5上的環回口地址可以通信
配置如下
R1
interface Loopback0 #模擬接入用戶
ip address 1.1.1.1 255.255.255.0
!
interface Ethernet0/0
ip address 192.168.1.1 255.255.255.0
router ospf 100 #ospf建立內部路由
router-id 1.1.1.1
network 1.1.1.0 0.0.0.255 area 0
network 192.168.1.0 0.0.0.255 area 0
network 192.168.10.0 0.0.0.255 area 0
R2
interface Tunnel0 #建立GRE隧道
ip address 192.168.10.1 255.255.255.0 #隧道IP地址
tunnel source Ethernet0/1 #配置隧道的本地源端口
tunnel destination 200.200.200.1 #配置隧道的目標出口
!
interface Ethernet0/0
ip address 192.168.1.254 255.255.255.0
!
interface Ethernet0/1
ip address 100.100.100.1 255.255.255.0
router ospf 100
router-id 2.2.2.2
network 1.1.1.0 0.0.0.255 area 0
network 192.168.1.0 0.0.0.255 area 0
network 192.168.10.0 0.0.0.255 area 0
ip route 0.0.0.0 0.0.0.0 100.100.100.254 #保證去往ISP的路由
R3
interface Ethernet0/0
ip address 200.200.200.254 255.255.255.0
!
interface Ethernet0/1
ip address 100.100.100.254 255.255.255.0
R4
interface Tunnel0
ip address 192.168.10.2 255.255.255.0
tunnel source Ethernet0/0
tunnel destination 100.100.100.1
!
interface Ethernet0/0
ip address 200.200.200.1 255.255.255.0
!
interface Ethernet0/1
ip address 172.16.1.254 255.255.255.0
router ospf 100
router-id 4.4.4.4
network 2.2.2.0 0.0.0.255 area 0
network 172.16.1.0 0.0.0.255 area 0
network 192.168.10.0 0.0.0.255 area 0
ip route 0.0.0.0 0.0.0.0 200.200.200.254
R5
interface Loopback0
ip address 2.2.2.2 255.255.255.0
interface Ethernet0/1
ip address 172.16.1.1 255.255.255.0
router ospf 100
router-id 5.5.5.5
network 2.2.2.0 0.0.0.255 area 0
network 172.16.1.0 0.0.0.255 area 0
network 192.168.10.0 0.0.0.255 area 0
配置完成后查看R2路由表和ospf鄰接
S* 0.0.0.0/0 [1/0] via 100.100.100.254
1.0.0.0/32 is subnetted, 1 subnets
O 1.1.1.1 [110/11] via 192.168.1.1, 00:28:07, Ethernet0/0
2.0.0.0/32 is subnetted, 1 subnets
O 2.2.2.2 [110/1011] via 192.168.10.2, 00:16:49, Tunnel0
100.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C 100.100.100.0/24 is directly connected, Ethernet0/1
L 100.100.100.1/32 is directly connected, Ethernet0/1
172.16.0.0/24 is subnetted, 1 subnets
O 172.16.1.0 [110/1010] via 192.168.10.2, 00:16:49, Tunnel0
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
192.168.10.0/24 is variably subnetted, 2 subnets, 2 masks
C 192.168.10.0/24 is directly connected, Tunnel0
L 192.168.10.1/32 is directly connected, Tunnel0
#show ip ospf neighbor
Neighbor ID Pri State Dead Time Address Interface
4.4.4.4 0 FULL/ - 00:00:33 192.168.10.2 Tunnel0
1.1.1.1 1 FULL/BDR 00:00:31 192.168.1.1 Ethernet0/0
發現可以通過隧道學到對端的路由,以及ospf鄰接
在R1進行ping測試,完成實驗
R1#ping 2.2.2.2 source l0 repeat 10
Type escape sequence to abort.
Sending 10, 100-byte ICMP Echos to 2.2.2.2, timeout is 2 seconds:
Packet sent with a source address of 1.1.1.1
!!!!!!!!!!
Success rate is 100 percent (10/10), round-trip min/avg/max = 1/1/1 ms
GRE隧道的接口狀態是down的情況
1.沒有去往隧道終點地址的路由
2.去往隧道終點地址的路由指向了隧道接口自己
3.隧道起源地址的接口狀態為down