概述
- 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
TunnelTTL 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类似
-