思科、華爲 GRE 隧道技術 的原理及配置詳解

概述

  • GRE是一種最傳統的隧道協議,其根本功能就是要實現隧道功能。
  • GRE (Generic Routing Encapsulation)是對某些網絡層協議(IPX 、IPX、AppleTalk等) 的數據進行封裝,使用這些被封裝的數據報告能夠在另一個網絡層協議中傳輸。

隧道傳遞數據包的過程分爲3步:

  • 接收原始IP數據包當作乘客協議,原始IP數據包包頭的IP地址爲私有IP地址。
  • 將原始IP數據包封裝進GRE協議,GRE協議稱爲封裝協議(Encapsulation Protocol),封裝的包頭IP地址爲虛擬直連鏈路兩端的IP地址。
  • 將整個GRE數據包當作數據,在外層封裝公網IP包頭,也就是隧道的起源和終點,從而路由到隧道終點。

在這裏插入圖片描述

GRE 隧道 配置思路

  • 基礎配置
  • 配置tunnel邏輯接口
  • 配置到對端網絡內網網段路由
  • 放開相應的域間規則

工作原理

數據包被髮到Internet之後,所有路由器只根據數據包最外面的公網IP進行轉發,也就是隻根據公網目標IP地址200.1.1.1來轉發,直到數據包到達公網IP的真正目的地後,即到達R3(IP:200.1.1.1)之後,公網IP包頭纔會被剝開,當R3剝開數據包的公網IP包頭後,發現GRE包頭,發現目標IP爲1.1.1.2,從而得知自己就是GRE隧道的終點,所以繼續將GRE包頭剝開,最後發現目標IP地址爲192.168.1.4,然後將數據包發往192.168.1.4(路由器R4)。

實驗

在這裏插入圖片描述

實驗要求:

北京公司R2 可以通過公網 ISP telent 到 R4

GRE 配置

R1:
Router>enable
Router#conf t
Router(config)#hostname R2
R2(config)#no ip domain-lookup #關閉域名解析

R2(config)#int e0/0
R2(config-if)#ip add 10.1.1.2 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#
R2(config)#ip route 0.0.0.0 0.0.0.0 10.1.1.1

R1(config)#int tunnel 1 #設置tunnel 1 口
R1(config-if)#ip add 1.1.1.1 255.255.255.0 #配置tunnel IP
R1(config-if)#tunnel source 100.1.1.1 #配置源地址,公網ip
R1(config-if)#tunnel destination 200.1.1.1 #設置目地址 公網ip
R1(config-if)#exit
R1(config)#

R2:
Router>enable
Router#conf t
Router(config)#hostname R2
R2(config)#no ip domain-lookup

R2(config)#int e0/0
R2(config-if)#ip add 10.1.1.2 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#exit
R2(config)#
R2(config)#ip route 0.0.0.0 0.0.0.0 10.1.1.1

** ISP**

Router>ena
Router#conf t
Router(config)#hostname ISP

ISP(config)#int e0/0
ISP(config-if)#ip add 100.1.1.5 255.255.255.0
ISP(config-if)#no sh

ISP(config-if)#int e0/1
ISP(config-if)#ip add 200.1.1.5 255.255.255.0
ISP(config-if)#no sh
ISP(config-if)#ex

R3:

Router(config)#host R3
R3(config)#int e0/1
R3(config-if)#ip add 200.1.1.1 255.255.255.0
R3(config-if)#no sh
R3(config-if)#int e0/0
R3(config-if)#ip add 192.168.1.1 255.255.255.0
R3(config-if)#no sh
R3(config-if)#exit
R3(config)#
R3(config)#ip route 0.0.0.0 0.0.0.0 200.1.1.5

R3(config)#int tunnel 3 #設置tunnel 3口
R3(config-if)#ip add 1.1.1.2 255.255.255.0 #配置tunnel IP
R3(config-if)#tunnel source 200.1.1.1 #配置源地址,公網ip
R3(config-if)#tunnel destination 100.1.1.1 #設置目地址 公網ip
R3(config)#

R4:

Router>
Router>ena
Router#conf t
Router(config)#host R4
R4(config)#int e0/0
R4(config-if)#ip add 192.168.1.2 255.255.255.0
R4(config-if)#no sh
R4(config-if)#exit
R4(config)#
R4(config)#ip route 0.0.0.0 0.0.0.0 192.168.1.1

  • 驗證
    R1#show interfaces tunnel 1
    Tunnel1 is up, line protocol isup # tunnel 接口已近up
    Hardware isTunnel
    Internetaddress is 1.1.1.1/24 # 隧道接口
    MTU 1514 bytes, BW 9 Kbit, DLY 500000 usec,
    reliability 255/255, txload 1/255, rxload 1/255
    Encapsulation TUNNEL, loopback not set
    Keepalive not set
    Tunnel source 100.1.1.1, destination 200.1.1.1 #隧道源地址 目的地址
    Tunnel protocol/transport GRE/IP
    Key disabled, sequencing disabled
    Checksumming of packets disabled
    Tunnel TTL 255
    Fast tunneling enabled
    Tunnel transmit bandwidth 8000 (kbps)
    Tunnel receive bandwidth 8000 (kbps)
    Last input 00:33:17, output 00:33:17, output hang never
    Last clearing of “show interface” counters never
    Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0
    Queueing strategy: fifo
    Output queue: 0/0 (size/max)
    5 minute input rate 0 bits/sec, 0 packets/sec
    5 minute output rate 0 bits/sec, 0 packets/sec
    376 packets input, 26253 bytes, 0 no buffer
    Received 0 broadcasts, 0 runts, 0 giants, 0 throttles
    0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort
    469 packets output, 31271 bytes, 0 underruns
    0 output errors, 0 collisions, 0 interface resets
    0 output buffer failures, 0 output buffers swapped out
    R1#

隧道接口地址爲1.1.1.1/24,隧道的起源爲100.1.1.1,隧道的終點爲200.1.1.1,因爲源地址100.1.1.1所在的接口爲正常狀態,並且也有去往隧道終點200.1.1.1的路由,所以隧道狀態爲up。

  • 檢查連通性
R2>ping 192.168.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:
.....
Success rate is 0 percent (0/5)
R2>

此時,發現無法ping 不通,雖然我們的tunnel 口鏈路已近起來了,但是依然無法ping 通,說明依然走的是公網,並沒有走tunnel 口

  • 解決方法:
  • 在創建GRE隧道的路由器雙方將去往對方私有網段的數據包引入GRE隧道中傳輸:

R1(config)#ip route 192.168.1.0 255.255.255.0 tunnel 1
R3(config)#ip route 10.1.1.0 255.255.255.0 tunnel 3

  • 配置telent
    R4(config)#line vty 0 4 #進入虛擬終端
    R4(config-line)#login local #本地生效
    R4(config-line)#exit
    R4(config)#username wyh password 123456 #設置本地用戶名:wyh 密碼:123456

  • R1#telnet 192.168.1.2
    Trying 192.168.1.2 … Open

User Access Verification

Username: wyh
Password: 123456
R4>enable
Password: #輸入enable密碼
% Password: timeout expired!
Password:
R4#

總結

RE隧道接口沒有OSI一層協議做檢測,只要本端源地址有效,並且隧道終點地址有路由可達,那麼GRE隧道接口就會up,而無論隧道對端是否已經配置隧道接口;如果GRE隧道的接口狀態要down,只要達到如下3個情況中任意一個即可:
1.沒有去往隧道終點地址的路由。
2.去往隧道終點地址的路由指向了隧道接口自己。
3.隧道起源地址的接口狀態爲down。

  • GRE隧道也可以採用在隧道雙方交換hello包的機制來使雙方接口狀態保持一致,這種機制稱爲GRE keepalive,

  • 隧道之間定期向對端發送keepalive,在超過指定的時間沒有收到對端的迴應,便認爲對端已失效,從而將本端的line protocol狀態變爲down。

  • 默認配置時,GRE keepalive默認爲10秒發一個,連續3個包沒有迴應,即30秒之後,便認爲對端已失效,從而將本端的line protocol狀態變爲down。

  • 在配置GRE keepalive時,即使隧道對端不支持GRE keepalive,照樣可以收到迴應,並且即使雙方的發送間隔不一致,也能正常工作。

R1(config)#int tunnel 1
R1(config-if)#keepalive 5 3 #間隔爲5秒,連續3個包

說明:配置了keepalive的發送間隔爲5秒,連續3個包,即15秒沒有收到迴應但認爲對端失效,默認配置參數爲10秒,連續3個包,即30秒沒有收到迴應但認爲對端失效。

華爲GRE 配置

  • 配置防火牆
    • 基本配置(略)

    • 配置

      [USG-A]int Tunnel 1
      [USG-A-Tunnel1]ip add 1.1.1.1 24 # tunnel 接口ip地址
      [USG-A-Tunnel1]tunnel-protocol gre #選擇封裝協議
      [USG-A-Tunnel1]source 100.1.1.1 # 防火牆的公網ip
      [USG-A-Tunnel1]destination 200.1.1.1 # 防火牆的公網ip
      [USG-A-Tunnel1]quit
      [USG-A]ip route-static 192.168.1.0 255.255.255.0 Tunnel 1 #到對方的私網路由
      [USG-A]

    • 防火牆B類似

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章