GRE:通用路由封装,全称 Generic Routing Encapsulation,也算是×××技术的一种。它能快速、简单地跨越Internet(公网)实现私网之间连通,特别是在跨越公网组建大的网络。但GRE致命的缺点就是,他是明文传输数据。所以在部署GRE的时候,往往结合各种安全措施来保护数据,如利用Ipsec对其数加密。本文介绍GRE的一些简单的实验。
GRE的原理并不复杂,它只是在私网IP包头前再封装一个公网的IP包头,而在公网上传输,公网的路由器看的只是公网的IP包头的IP,不会看到私网的IP包头,直到到达目标路由器,目标路由器接受到数据后,去除公网IP包头发现是GRE数据,那么它就将原封不动这个私网IP数据传送到内网中(只是IP包头以上的数据不动,改变的是二层报头)。
GRE隧道特点:
- 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.0router 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.0router 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 0ip 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.0interface Ethernet0/1
ip address 172.16.1.1 255.255.255.0router 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