GRE详解

发布时间:2020-06-19 07:12:12

GRE(Generic Routing Encapsulation )

GRE是一种最传统的隧道协议,其根本功能就是要实现隧道功能,通过隧道连接的两个远程网络就如同直连, GRE在两个远程网络之间

模拟出直连链路,从而使网络间达到直连的效果,为此, GRES要完成多次封装,总共有3次,换句话说,就是在 GRE隧道中传输的

数据包都有3个爆头,因为只谈IP协议,所以GRE中的IP数据包是一层套一层,总共有 3IP地址,GRE在实现隧道时,需要创建 虚拟直连链路,GRES现的虚拟链路可以认为是隧道,隧道是模拟链路,所以隧道两端也有 IP地址,但隧道需要在公网中找到起点和

重点,所以隧道的源和终点分别都以公网 IP地址结尾,该链路是通过 GRE协议来完成的,隧道传递数据包的过程分为 3步:

1 接收原始 IP 数据包当作乘客协议,原始数据包包头的 IP 地址为私有 IP 地址。

2、 将原始IP数据包封装进GRE协议,GR助议成为封装协议(Encapsulation Protocol ),封装的包头IP地址为虚拟直连链路两端 的 IP 地址。

3、将整个GRE数据包当作数据,在外层封装公网的IP包头,也就是隧道的起源和终点,从而路由到隧道终点

GRE1道中传输的数据包格式如下:

注:

1、其中公网IP包头部分也成为传输协议(Tran sport Protocol )

2GRE会在原始IP数据包之外,额外多封装24个字节或28个字节,具体视GRE莫式而定

下图为GRE专输数据过程:

1- 1- E 2





GRE要在远程路由器之间创建虚拟直连链路,也就是隧道Tunnel),如果没有该隧道,GRE不能完成隧道功能,隧道是

GRE最基本的功能,也是GRE所有功能;上图环境中,当上海分公司 R2将数据包IP地址封装为192.168.1.4发往武汉时,GRE操作过 程如下:

1、假设R1R3GRE®拟直连链路(隧道)已经简历,隧道链路两端的地址分别为 别为 202.1.1.1 61.1.1.1.

2R1收到目标IP192.168.1.4的数据包后,将原始数据包当作乘客数据包封装进 GRE协议中,并且添加GRE包头,包头中源IP

为隧道本端地址1.1.1.1,包头中目标IP为隧道对端地址1.1.1.2,从而完成GRE数据包的分装。

3、在封装了 GRE隧道地址的数据包外面分装 GRE隧道起源IP地址,该IP地址为公网地址,即源IP100.1.1.1,目标IP为隧道终 点200.1.1.1,最后将数据包发出去。

数据包被发送到in ternet之后,所有路由器只根据数据包最外面的公网IP进行转发,也就是只根据公网IP地址61.1.1.1 来转发,直到数据包到达公网IP的真正目的地后,即到达 R3(IP : 61.1.1.1 )之后,公网IP包头才会被剥开,当R3剥开数据包的 公网IP包头后,发现GRE包头,发现目标IP地址为1.1.1.2,从而得知自己就是 GR魄道的终点,所以继续将 GRE包头剥开,最后 发现目标IP地址为192.168.1.4,然后将数据包发往192.168.1.4 (路由器R4)o 通过以上GRES程,上海分公司R2直接通过私有IP地址192.168.1.4,最终成功与武汉分公司 R4通信。

配置GRE

在远程路由器之间配置 GRE总共分为三步:

1 创建虚拟链路(隧道)接口,号码任意,两端不可相同。

2 配置虚拟链路(隧道)接口地址,该地址是在 GRE包头中被封装的地址。

3 定义虚拟链路(隧道)的源和目的,因为数据包最终要在公网中传递,所以该地址就是在公网中指导路由器转发数据包的可路由公 网IP,也是建立隧道两端路由器的真实公网 IP 0

注:

1 GRE Tunnel只支持路由器,不支持集中器和 PIX以及ASA

2 GRE支 持的协议有 IPDec netIPXAppletalk o

3GRE可分为 Point-to-Point GRE Multipoint GRE mGRE 两种。

4 Point-to-Point GRE 只能在两台路由器之间建立。

5Multipoint GRE mGRE也可以在两台以上的路由器之间建立。

6 Point-to-Point GRE 支持IP单播,组播,以及IGP动态路由协议和非IP协议。

7Multipoint GRE mGRE只支持单播,组播以及动态IGP路由协议,不支持非IP协议。

GRE隧道接口没有OSI —层协议做检测,只要本地源地址有效,并且隧道终点地址有路由可达,那么 GRE隧道接口就会

UP,而无论隧道对端是否已经配置隧道接口,如果 GRE隧道的接口状态为down只要达到如下3中情况任意一个即可:

1 没有向往隧道终点地址的路由。

2 去往隧道终点地址的路由指向了隧道接口自己。

3 隧道起源地址的接口状态为 down,

以下面的图为例,配置GRE

1. 1. E 1





说明:图中In ternet 使用路由器R2来模拟!

配置R1:

Router>e n

Router#c onf t

Router(c on fig)# no ip do lo

Router(c on fig)#lin con 0

Router(config-line)#no exec-t

Router(config-line)#logg s

Router(c on fig-li ne)#exit

Router(c on fig)#ho CHENYI-R1

CHENYI-R1 (co nfig)#i nt f0/0

CHENYI-R1 (config-if)#ip add 10.1.1.1 255.255.255.0

CHENYI-R1 (co nfig-if)# no sh

CHENYI-R1 (co nfig-if)#i nt f1/0

CHENYI-R1 (config-if)#ip add 202.1.1.1 255.255.255.0

CHENYI-R1 (co nfig-if)# no sh

CHENYI-R1 (config-if)#ip route 0.0.0.0 0.0.0.0 202.1.1.10

说明:配置R1的接口地址,并写默认路由指向In ternet (路由器R2),地址为202.1.1.10

配置R3:

Router>e n

Router#c onf t

Router(c on fig)# no ip do lo

Router(c on fig)#lin con 0

Router(config-line)#no exec-t

Router(con fig-li ne)#logg s

Router(con fig-li ne)#exit

Router(c on fig)#ho CHENYI-R3

CHENYI-R3(con fig)#i nt f0/0

CHENYI-R3(config-if)#ip add 61.1.1.1 255.255.255.0

CHENYI-R3(co nfig-if)# no sh

CHENYI-R3(co nfig-if)#i nt f1/0

CHENYI-R3(config-if)#ip add 192.168.1.3 255.255.255.0

CHENYI-R3(co nfig-if)# no sh

CHENYI-R3(config-if)#ip route 0.0.0.0 0.0.0.0 61.1.1.10

配置R2

Router>e n

Router#c onf t

Router(c on fig)# no ip do lo

Router(c on fig)#lin con 0

Router(config-line)#no exec-t

Router(config-line)#logg s

Router(c on fig-li ne)#exit

Router(co nfig)#HO CHENYI-R2

CHENYI-R2(c on fig)#i nt f0/0

CHENYI-R2(config-if)#ip add 202.1.1.10 255.255.255.0

CHENYI-R2(co nfig-if)# no sh

CHENYI-R2(c on fig-if)#ip add 61.1.1.10 255.255.255.0

CHENYI-R2(co nfig-if)# no sh

配置GRE

CHENYI-R1 (con fig)#i nterface tunnel 1

CHENYI-R1 (config-if)#ip add 1.1.1.1 255.255.255.0

CHENYI-R1 (config-if)#tunnel source 202.1.1.1

CHENYI-R1 (config-if)#tunnel destination 61.1.1.1

查看R1GRE的状态

CHENYI-R1 (con fig)#do sh int tunnel 1

Tunn el1 is up, li ne protocol is up

Hardware is Tunnel

In ternet address is 1.1.1.1/24

MTU 1514 bytes, BW 9 Kbit, DLY 500000 usec, reliability 255/255, txload 1/255, rxload 1/255

En capsulati on TUNNEL, loopback not set Keepalive not set

Tu nnel source 202.1.1.1, dest in ation 61.1.1.1

Tunnel protocol/tra nsport GRE/IP

Key disabled, seque ncing disabled

Checksum ming of packets disabled

Tu nnel TTL 255

Fast tunn eli ng en abled

Tu nnel tran smit ban dwidth 8000 (kbps)

Tunnel receive bandwidth 8000 (kbps)

Last in put n ever, output n ever, output hang n ever

Last cleari ng of "show in terface" coun ters n ever

In put queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0

Queue ing strategy: fifo

Output queue: 0/0 (size/max)

5 mi nute in put rate 0 bits/sec, 0 packets/sec

5 minute output rate 0 bits/sec, 0 packets/sec

0 packets in put, 0 bytes, 0 no buffer

Received 0 broadcasts, 0 runts, 0 giants, 0 throttles

0 in put errors, 0 CRC, 0 frame, 0 overr un, 0 igno red, 0 abort

0 packets output, 0 bytes, 0 underruns

0 output errors, 0 collisi ons, 0 in terface resets

0 output buffer failures, 0 output buffers swapped out

说明:可以看出,在R1创建GRE隧道之后,隧道接口状态便已经 up,这是因为默认情况下,GRE隧道接口没有OSI —层协议做检测, 只要本端源地址有效,并且隧道终点地址有路由可达,那么 GRE隧道接口就会up,而无论隧道对端是否已经配置隧道接口。

在武汉公司的路由器R3上配置连接到上海分公司路由器 R1GRE1道:

CHENYI-R3(config)#int tunnel 3

CHENYI-R3(config-if)#ip add 1.1.1.2 255.255.255.0

CHENYI-R3(co nfig-if)#tu nnel source 61.1.1.1

CHENYI-R3(config-if)#tun dest 202.1.1.1

CHENYI-R3(co nfig-if)#exit

说明:在R3上创建GRE虚拟链路(隧道)接口,号码为3,两端号码可不相同,隧道接口地址为1.1.1.2/24 ,隧道的起源为61.1.1.1.

隧道的终点为202.1.1.1

CHENYI-R3(co nfig-if)#do sh int tun 3

Tunn el3 is up, li ne protocol is up

Hardware is Tunnel

In ternet address is 1.1.1.2/24

MTU 1514 bytes, BW 9 Kbit, DLY 500000 usec,

reliability 255/255, txload 1/255, rxload 1/255

En capsulati on TUNNEL, loopback not set

Keepalive not set

Tu nnel source 61.1.1.1, dest in ation 202.1.1.1

Tunnel protocol/tra nsport GRE/IP

Key disabled, seque ncing disabled

Checksum ming of packets disabled

Tu nnel TTL 255

Fast tunn eli ng en abled

Tu nnel tran smit ban dwidth 8000 (kbps)

Tunnel receive bandwidth 8000 (kbps)

Last in put n ever, output n ever, output hang n ever

Last cleari ng of "show in terface" coun ters n ever

In put queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0

Queue ing strategy: fifo

Output queue: 0/0 (size/max)

5 mi nute in put rate 0 bits/sec, 0 packets/sec

5 minute output rate 0 bits/sec, 0 packets/sec

0 packets in put, 0 bytes, 0 no buffer

Received 0 broadcasts, 0 runts, 0 giants, 0 throttles

0 in put errors, 0 CRC, 0 frame, 0 overr un, 0 igno red, 0 abort

0 packets output, 0 bytes, 0 underruns

0 output errors, 0 collisi ons, 0 in terface resets

0 output buffer failures, 0 output buffers swapped out

说明:R3上的GRE隧道状态也已经up

此时可以测试GRE隧道了。使用R1 pi ng R3那么是没有问题的

但是ping上海分公司R1直接使用私有地址192.168.1.4到上海分公司R3的连通性是不通的。

因为北京分公司的路由器 R1在收到去往192.168.1.4后,因为默认路由从真实接口 F1/0出去,结果数据包被发到 In ternet中的路由器R2,由于In ternet路由器R2只有公网路由,没有用户的私有网段,所以武汉分公司 R1直接使用私有地址 192.168.1.4 上海分公司 R4无法通信。

要解决此问题,必须让流量从 GRE隧道中传输。

在创建GRE隧道的路由器双方将去往对方私有网段的数据包引入 GREW道中传输。

R1

CHENYI-R1(c on fig)# ip route 192.168.1.0 255.255.255.0 tunnel 1

R3:

CHENYI-R3(config-if)#ip route 10.1.1.0 255.255.255.0 tunnel 3

此时测试武汉分公司PC直接使用私有地址192.168.1.4到上海分公司PC的连通性:

本文出自“IT辰逸”博客,请务必保留此出处

GRE详解

相关推荐