f

fjcharles

V1

2023/01/11阅读:17主题:默认主题

大瞎话GRE隧道

大瞎话网络小知识

GRE隧道介绍

GRE(Generic Routing Encapsulation,通用路由封装)是一种可以在虚拟点对点链路中封装多种网络层协议的隧道协议。可以对某些网络层协议的数据报进行封装,使这些被封装的数据报能够在IPv4网络中传输。

就像超级马里奥中的管道,在服务器之间建立一个管道,从一端进去从另一端出来。

场景

任务

马里奥在A服务器,它要去B服务器营救公主,这两台服务器之间由公网ip可以互通,需要让A服务器内网可以ping通B服务器内网(即内网互通)

环境

  • centos7.6 2台(阿里云/华为云等云均可)

实现原理

在A和B服务器之间搭建一条GRE隧道,例如10.10.0.0/24网段,让A和B组成局域网,在局域网内的ip就能实现互通了。

操作步骤:

1. A与B服务器建立隧道

# 在A服务器终端下执行
# 创建一个GRE类型隧道设备gre1, 并设置对端IP为139.9.83.17。
# 隧道数据包将被从192.168.0.193也就是本地IP地址发起,其TTL字段被设置为255。
# 隧道设备分配的IP地址为10.10.10.1,掩码为255.255.255.0。

[root@a_host ~]# ip tunnel add gre1 mode gre remote 139.9.83.17 local 192.168.0.193 ttl 255 key 123456
[root@a_host ~]# ip link set gre1 up
[root@a_host ~]# ip addr add 10.10.10.1 peer 10.10.10.2 dev gre1
# 在B服务器终端下执行
[root@b_host ~]# ip tunnel add gre1 mode gre remote 43.254.3.249 local 172.16.0.108 ttl 255 key 123456
[root@b_host ~]# ip link set gre1 up
[root@b_host ~]# ip addr add 10.10.10.2 peer 10.10.10.1 dev gre1

一旦建立gre隧道,可以在A和B服务器会自动建立一条路由,可以通过route -n查看

# 在A服务器查看路由
[root@a_host ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0
#自动建立的路由--------------------------------------------------------------
10.10.10.2      0.0.0.0         255.255.255.255 UH    0      0        0 gre1
#自动建立的路由--------------------------------------------------------------
169.254.169.254 192.168.0.254   255.255.255.255 UGH   100    0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
[root@a_host ~]

# 在B服务器查看路由
[root@b_host ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.0.1      0.0.0.0         UG    100    0        0 eth0
#自动建立的路由--------------------------------------------------------------
10.10.10.1      0.0.0.0         255.255.255.255 UH    0      0        0 gre1
#自动建立的路由--------------------------------------------------------------
169.254.169.254 172.16.0.254    255.255.255.255 UGH   100    0        0 eth0
172.16.0.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0
1.1 测试隧道互通

在A服务器ping 10.10.10.2通过tcpdump抓包工具可以看出数据库包进入gre隧道,做了一层包装从eth0出口出去。

# 在A host执行抓包命令
[root@a_host ~]# tcpdump -i gre1 -nnn -s 1000

通过tcpdump抓eth0 数据包,可以看到数据包做了一层GRE包装。

2. 添加对端子网路由表-内网互通

# A host 增加静态路由,让172.16.0.108走gre1隧道
[root@a_host ~]# ip route add 172.16.0.108 dev gre1

# 查看路由表
[root@a_host ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    100    0        0 eth0
10.10.10.2      0.0.0.0         255.255.255.255 UH    0      0        0 gre1
169.254.169.254 192.168.0.254   255.255.255.255 UGH   100    0        0 eth0
#增加一条静态路由--------------------------------------------------------------
172.16.0.108    0.0.0.0         255.255.255.255 UH    0      0        0 gre1
#增加一条静态路由--------------------------------------------------------------
192.168.0.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0

# B host 增加静态路由,让192.168.0.193走gre1隧道
[root@b_host ~]# ip route add 192.168.0.193 dev gre1

# 查看路由表
[root@b_host ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.0.1      0.0.0.0         UG    100    0        0 eth0
10.10.10.1      0.0.0.0         255.255.255.255 UH    0      0        0 gre1
169.254.169.254 172.16.0.254    255.255.255.255 UGH   100    0        0 eth0
172.16.0.0      0.0.0.0         255.255.255.0   U     100    0        0 eth0
#增加一条静态路由--------------------------------------------------------------
192.168.0.193   0.0.0.0         255.255.255.255 UH    0      0        0 gre1
#增加一条静态路由--------------------------------------------------------------
2.1 测试内网ip互通

测试结果

从测试中可以看出,内网ip能够互通,这样马里奥就可以通过gre隧道营救公主了。😃

GRE隧道优缺点

优点:

  • 建立隧道,打通私网,将不连续的子网连接起来支持灵活设计网络拓扑。

缺点:

  • gre隧道本身并不支持加密,因而通过GRE隧道传输的流量是不加密的,可以通过+ipsec方式解决。
  • linux下gre隧道也没有keepalive机制,会掉线。

下回一起实验GRE隧道加密

分类:

后端

标签:

计算机网络

作者介绍

f
fjcharles
V1