通用路由封装协议 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 : --
来源: http://blog.51cto.com/13817711/2473706