通用路由封装协议GRE(Generic Routing Encapsulation)提供了将一种协议的报文封装在另一种协议报文中的机制,是一种隧道封装技术。GRE可以封装组播数据,并可以和IPSec结合使用,从而保证语音、视频等组播业务的安全  IPSec 用于在两个端点之间提供安全的IP通信,但只能加密并传播单播数据,无法加密和传输语音、视频、动态路由协议信息等组播数据流量  GRE属于网络层协议* IP协议号为47 GRE的优点总结:  GRE实现机制简单,对隧道两端的设备负担小  GRE隧道可以通过IPv4网络连通多种网络协议的本地网络,有效利用了原有的网络架构,降低成本  GRE隧道扩展了跳数受限网络协议的工作范围,支持企业灵活设计网络拓扑  GRE隧道可以封装组播数据,和IPSec结合使用时可以保证语音、视频等组播业务的安全  GRE隧道支持使能MPLS LDP,使用GRE隧道承载MPLS LDP报文,建立LDP LSP,实现MPLS骨干网的互通  GRE隧道将不连续的子网连接起来,用于组建***,实现企业总部和分支间安全的连接

GRE的应用场景

 GRE用来对某些网络层协议如IPX(Internet Packet Exchange)的报文进行封装,使这些被封装的报文能够在另一网络层协议(如IP)中传输。GRE可以解决异种网络的传输问题 使用GRE可以克服IGP协议的一些局限性。例如,RIP路由协议是一种距离矢量路由协议,最大跳数为15。如果网络直径超过15,设备将无法通信。这种情况下,可以使用GRE技术在两个网络节点之间搭建隧道,隐藏它们之间的跳数,扩大网络的工作范围 GRE和IPSEC技术的结合 GRE本身并不支持加密,因而通过GRE隧道传输的流量是不加密的。将IPSec技术与GRE相结合,可以先建立GRE隧道对报文进行GRE封装,然后再建立IPSec隧道对报文进行加密,以保证报文传输的完整性和私密性

隧道接口

 GRE隧道是通过隧道两端的Tunnel接口建立的,所以需要在隧道两端的设备上分别配置Tunnel接口。对于GRE的Tunnel接口,需要指定其协议类型为GRE、源地址或源接口、目的地址和Tunnel接口IP地址  隧道接口(tunnel接口)是为实现报文的封装而提供的一种点对点类型的虚拟接口 与loopback接口类似 都是一种逻辑接口  GRE隧道接口包含源地址目的地址隧道接口IP地址封装类型  Tunnel的源地址:配置报文传输协议中的源地址。  当配置地址类型时,直接作为源地址使用  当配置类型为源接口时,取该接口的IP地址作为源地址使用 ** Tunnel的目的地址**:配置报文传输协议中的目的地址  Tunnel接口IP地址:为了在Tunnel接口上启用动态路由协议,或使用静态路由协议发布Tunnel接口,需要为Tunnel接口分配IP地址。Tunnel接口的IP地址可以不是公网地址,甚至可以借用其他接口的IP地址以节约IP地址。但是当Tunnel接口借用IP地址后,该地址不能直接通过tunnel口互通,因此在借用IP地址情况下,必须配置静态路由或路由协议先实现借用地址的互通性,才能实现Tunnel的互通。 命令行配置如下:

[Huawei]interface Tunnel 0/0/1
[Huawei-Tunnel0/0/1]ip address 123.1.1.1 24
[Huawei-Tunnel0/0/1]tunnel-protocol gre 
[Huawei-Tunnel0/0/1]source 202.1.1.1 
[Huawei-Tunnel0/0/1]destination 101.1.1.1
[Huawei]ip route-static 192.168.2.0 24 Tunnel 0/0/1

GRE的报文

乘客协议(Passenger Protocol):封装前的报文称为净荷,封装前的报文协议称为乘客协议  封装协议(Encapsulation Protocol):GRE Header是由封装协议完成并填充的,封装协议也称为运载协议(Carrier Protocol)  传输协议(Transport Protocol或者Delivery Protocol):负责对封装后的报文进行转发的协议称为传输协议 GRE报文重要字段解释:

Protocol Type		标识乘客协议的协议类型。常见的乘客协议为IPv4协议,协议代码为0800
Key				关键字字段,隧道接收端用于对收到的报文进行验证 Tunnel两端的key一定要一样

比如: 乘客协议为IP或IPX协议 封装协议为GRE协议 传输协议为IP协议

GRE的工作原理

 需求:运行IP协议的两个子网网络1和网络2 通过在SZ_***和BJ_***之间使用三层隧道协议GRE实现互联

封装

 在PC1上面ping PC2 SZ_接收到PC1的ICMP报文后,首先交由转发模块处理  转发模块根据报文头中的目的地址在路由表或转发表中查找出接口,确定如何转发此报文。如果发现出接口是GRE Tunnel0/0/1接口,系统会读取tunnel0/0/1接口的配置 发现隧道封装协议为GRE协议 并且隧道的SIP和DIP都有 会交给GRE模块进行处理,则对报文进行GRE封装,即添加GRE头。GRE协议模块工作就做完了  然后回交给路由器的转发模块*处理 此时的SIP是202.1.1.1 DIP是101.1.1.1的数据报文 路由器根据DIP查找转发表 会匹配默认路由从G0/0/0发出去

解封装

 BJ_从G0/0/0接口收到该报文,交给转发模块处理 分析DIP头发现报文的目的地址为本设备,并且protocol数值为47 则BJ_去掉IP头后交给GRE模块处理。GRE首先会进行核对 发过来的GRE报文中的SIP和DIP是否和我自身tunnel接口配置一样 如果一样的话 GRE模块会接收 如果不一样的话 直接进行丢弃。  GRE协议剥掉GRE报头,获取ICMP协议报文,再交由ICMP协议对此数据报文进行后续的转发模块处理。查找路由表 从接口G0/0/1接口发出去

GRE的配置

第一步:基本配置

**SZ_*路由器的配置

#
 sysname SZ_***
#
interface GigabitEthernet0/0/0
 ip address 202.1.1.1 255.255.255.0 
#
interface GigabitEthernet0/0/1
 ip address 192.168.1.254 255.255.255.0 
#
ip route-static 0.0.0.0 0.0.0.0 202.1.1.254
#

**BJ_*路由器的配置

#
 sysname BJ_***
#
interface GigabitEthernet0/0/0
 ip address 101.1.1.1 255.255.255.0 
#
interface GigabitEthernet0/0/1
 ip address 192.168.2.254 255.255.255.0 
#
ip route-static 0.0.0.0 0.0.0.0 101.1.1.254
#

internet的配置

#
interface GigabitEthernet0/0/0
 ip address 202.1.1.254 255.255.255.0 
#
interface GigabitEthernet0/0/1
 ip address 101.1.1.254 255.255.255.0 
#

检查如下: **检查SZ_*和PC1的通信

<SZ_***>ping 192.168.1.1
  PING 192.168.1.1: 56  data bytes, press CTRL_C to break
    Reply from 192.168.1.1: bytes=56 Sequence=1 ttl=128 time=40 ms
    Reply from 192.168.1.1: bytes=56 Sequence=2 ttl=128 time=60 ms
    Reply from 192.168.1.1: bytes=56 Sequence=3 ttl=128 time=40 ms
    Reply from 192.168.1.1: bytes=56 Sequence=4 ttl=128 time=60 ms
    Reply from 192.168.1.1: bytes=56 Sequence=5 ttl=128 time=50 ms

  --- 192.168.1.1 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 40/50/60 ms

**检查BJ_*和PC2的通信

<SZ_***>ping 192.168.1.1
  PING 192.168.1.1: 56  data bytes, press CTRL_C to break
    Reply from 192.168.1.1: bytes=56 Sequence=1 ttl=128 time=40 ms
    Reply from 192.168.1.1: bytes=56 Sequence=2 ttl=128 time=60 ms
    Reply from 192.168.1.1: bytes=56 Sequence=3 ttl=128 time=40 ms
    Reply from 192.168.1.1: bytes=56 Sequence=4 ttl=128 time=60 ms
    Reply from 192.168.1.1: bytes=56 Sequence=5 ttl=128 time=50 ms

  --- 192.168.1.1 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 40/50/60 ms

**检查SZ_***和BJ_*的通信

<SZ_***>ping 101.1.1.1
  PING 101.1.1.1: 56  data bytes, press CTRL_C to break
    Reply from 101.1.1.1: bytes=56 Sequence=1 ttl=254 time=30 ms
    Reply from 101.1.1.1: bytes=56 Sequence=2 ttl=254 time=20 ms
    Reply from 101.1.1.1: bytes=56 Sequence=3 ttl=254 time=40 ms
    Reply from 101.1.1.1: bytes=56 Sequence=4 ttl=254 time=20 ms
    Reply from 101.1.1.1: bytes=56 Sequence=5 ttl=254 time=30 ms

  --- 101.1.1.1 ping statistics ---
    5 packet(s) transmitted
    5 packet(s) received
    0.00% packet loss
    round-trip min/avg/max = 20/28/40 ms

检查PC1和PC2的通信

PC>ping  192.168.2.2

Ping 192.168.2.2: 32 data bytes, Press Ctrl_C to break
Request timeout!
Request timeout!
Request timeout!
Request timeout!
Request timeout!

--- 192.168.2.2 ping statistics ---
  5 packet(s) transmitted
  0 packet(s) received
  100.00% packet loss

第二步:分别在SZ_***和BJ_***启动TUNNEL接口

**SZ_*的配置

#
interface Tunnel0/0/1
 ip address 123.1.1.1 255.255.255.0 
 tunnel-protocol gre
 source 202.1.1.1
 destination 101.1.1.1
#

**BJ_*的配置

#
interface Tunnel0/0/1
 ip address 123.1.1.2 255.255.255.0 
 tunnel-protocol gre
 source 101.1.1.1
 destination 202.1.1.1
#

第三步:配置引流路由

**SZ_*的配置

#
ip route-static 192.168.2.0 255.255.255.0 Tunnel0/0/1
#

**BJ_*的配置

#
ip route-static 192.168.1.0 255.255.255.0 Tunnel0/0/1
#

第四步:检查GRE隧道是否成功

**SZ_*的检查

<SZ_***>display interface Tunnel 0/0/1
**Tunnel0/0/1 current state : UP
Line protocol current state : UP**
Last line protocol up time : 2020-02-25 17:50:10 UTC-08:00
Description:HUAWEI, AR Series, Tunnel0/0/1 Interface
Route Port,The Maximum Transmit Unit is 1500
Internet Address is 123.1.1.1/24
Encapsulation is TUNNEL, loopback not set
**Tunnel source 202.1.1.1 (GigabitEthernet0/0/0), destination 101.1.1.1
Tunnel protocol/transport GRE/IP, key disabled
keepalive disabled**
Checksumming of packets disabled
Current system time: 2020-02-25 20:19:16-08:00
    300 seconds input rate 0 bits/sec, 0 packets/sec
    300 seconds output rate 0 bits/sec, 0 packets/sec
    0 seconds input rate 0 bits/sec, 0 packets/sec
    0 seconds output rate 0 bits/sec, 0 packets/sec
    0 packets input,  0 bytes
    0 input error
    0 packets output,  0 bytes
    0 output error
    Input bandwidth utilization  : --
    Output bandwidth utilization : --

<SZ_***>

**BJ_*的检查

<BJ_***>display interface Tunnel 0/0/1
**Tunnel0/0/1 current state : UP
Line protocol current state : UP**
Last line protocol up time : 2020-02-25 17:50:53 UTC-08:00
Description:HUAWEI, AR Series, Tunnel0/0/1 Interface
Route Port,The Maximum Transmit Unit is 1500
Internet Address is 123.1.1.2/24
Encapsulation is TUNNEL, loopback not set
**Tunnel source 101.1.1.1 (GigabitEthernet0/0/0), destination 202.1.1.1
Tunnel protocol/transport GRE/IP, key disabled
keepalive disabled**
Checksumming of packets disabled
Current system time: 2020-02-25 20:21:08-08:00
    300 seconds input rate 0 bits/sec, 0 packets/sec
    300 seconds output rate 0 bits/sec, 0 packets/sec
    0 seconds input rate 0 bits/sec, 0 packets/sec
    0 seconds output rate 0 bits/sec, 0 packets/sec
    0 packets input,  0 bytes
    0 input error
    0 packets output,  0 bytes
    0 output error
    Input bandwidth utilization  : --
    Output bandwidth utilization : --

<BJ_***>

GRE的Keepalive检测功能

 Keepalive检测功能用于在任意时刻检测隧道链路是否处于Keepalive状态,即检测隧道对端是否可达。如果对端不可达,隧道连接就会及时关闭,避免形成数据空洞。使能Keepalive检测功能后,GRE隧道本端会定期向对端发送Keepalive探测报文。若对端可达,则本端会收到对端的回应报文;若对端不可达,则收不到对端的回应报文。  如果在隧道一端配置了Keepalive功能,无论对端是否配置Keepalive,配置的Keepalive功能在该端都生效。隧道对端收到Keepalive探测报文,无论是否配置Keepalive,都会给源端发送一个回应报文。  使能Keepalive检测功能后,GRE隧道的源端会创建一个计数器,并周期性地发送Keepalive探测报文,同时进行不可达计数。每发送一个探测报文,不可达计数加1 如果源端在计数器值达到预先设置的值之前收到回应报文,则表明对端可达。如果计数器值达到预先设置的重试次数,源端还是没有收到回应报文,则认为对端不可达。此时,源端将关闭隧道连接。

#
interface Tunnel0/0/1
 ip address 123.1.1.1 255.255.255.0 
 tunnel-protocol gre
 keepalive period 3 	---默认值为5秒
 source 202.1.1.1
 destination 101.1.1.1
#
[SZ_***]display interface Tunnel 0/0/1
Tunnel0/0/1 current state : UP
Line protocol current state : UP
Last line protocol up time : 2020-02-25 17:50:10 UTC-08:00
Description:HUAWEI, AR Series, Tunnel0/0/1 Interface
Route Port,The Maximum Transmit Unit is 1500
Internet Address is 123.1.1.1/24
Encapsulation is TUNNEL, loopback not set
Tunnel source 202.1.1.1 (GigabitEthernet0/0/0), destination 101.1.1.1
Tunnel protocol/transport GRE/IP, key disabled
keepalive enable period 3 retry-times 3
Checksumming of packets disabled
Current system time: 2020-02-25 20:36:57-08:00
    300 seconds input rate 0 bits/sec, 0 packets/sec
    300 seconds output rate 0 bits/sec, 0 packets/sec
    0 seconds input rate 0 bits/sec, 0 packets/sec
    0 seconds output rate 0 bits/sec, 0 packets/sec
    0 packets input,  0 bytes
    0 input error
    4 packets output,  192 bytes
    0 output error
    Input bandwidth utilization  : --
    Output bandwidth utilization : --