Linux与网络设备 GRE配置经验总结

系统 Linux
GRE(General Routing Encapsulation),即通用路由封装,是一种三层技术。它的最大作用是可以对某些网络层协议的报文进行封装,如对路由协议、语音、视频等组播报文或IPv6报文进行封装。同时,也能够与 IPSec 结合,解决 GRE 的安全问题。

[[353011]]

本文转载自微信公众号「新钛云服」,作者侯明明。转载本文请联系新钛云服公众号。   

GRE 介绍及应用场景

GRE(General Routing Encapsulation),即通用路由封装,是一种三层技术。它的最大作用是可以对某些网络层协议的报文进行封装,如对路由协议、语音、视频等组播报文或IPv6报文进行封装。同时,也能够与 IPSec 结合,解决 GRE 的安全问题。

本文主要介绍 Linux 与 网络设备(华为防火墙、华为路由器、Juniper SRX防火墙)对接 GRE 的配置方法。

GRE 报文

如下图所示,GRE 是按照 TCPIP 协议栈进行逐层封装,新的 IP 头会封装在原有的 IP 头中,然后运送出去,封装操作是通过 Tunnel 接口完成的,GRE 协议经过 Tunnel 口时,会将接口的封装协议设置为 GRE 协议。

GRE 的配置场景

CentOS 7.6 与 华为防火墙建立 GRE 隧道

拓扑图

实现目标

  • CentOS 与 华为防火墙建立 GRE 隧道;
  • 华为防火墙背后的内网网段 192.168.1.0/24 通过 GRE 隧道从 CentOS 到 Internet;
  • CentOS 配置端口映射,将 192.168.1.10 的 8080 端口映射到 CentOS 的公网地址 200.1.1.1 的 8080 端口。

配置

  • CentOS

配置接口与路由

[root@CentOS ~]# vim /etc/sysconfig/network-scripts/ifcfg-tun0 
DEVICE=tun0 
BOOTPROTO=none 
ONBOOT=yes 
DEVICETYPE=tunnel 
TYPE=GRE 
PEER_INNER_IPADDR=172.16.1.2 
PEER_OUTER_IPADDR=100.1.1.1 
MY_INNER_IPADDR=172.16.1.1 
MY_OUTER_IPADDR=200.1.1.1 
 
[root@CentOS ~]# vim /etc/sysconfig/network-scripts/route-tun0 
192.168.1.0/24 via 172.16.1.2 
 
[root@CentOS ~]# ifup tun0 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Iptables 配置

# 安装 iptables 管理服务 
[root@CentOS ~]# yum install iptables-services 
 
# 在 INPUT 方向要放行对端的公网地址 
[root@CentOS ~]# iptables -I INPUT -s 100.1.1.1/32 -j ACCEPT 
 
# 配置源地址转换 
[root@CentOS ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 200.1.1.1 
 
# 端口映射 
[root@CentOS ~]# iptables -t nat -A PREROUTING -d 200.1.1.1 -p tcp --dport 8080 -j DNAT --to-dest 192.168.1.10:8080 
 
# 保存 iptables 
[root@CentOS ~]# service iptables save 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

开启 ipv4 转发

[root@CentOS ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf 
 
[root@CentOS ~]# sysctl -p 
  • 1.
  • 2.
  • 3.
  • 华为防火墙

本次以华为 USG6300E 系列防火墙为例:

配置接口,并添加到安全区域

interface Tunnel0 
ip address 172.16.1.2 255.255.255.0 
tunnel-protocol gre 
source 100.1.1.1 
destination 200.1.1.1 
 
# 将接口添加到安全区域内 
[USG6300E] firewall zone tunnel 
firewall zone name tunnel 
set priority 75 
add interface Tunnel0 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

配置安全策略

在实际的实施中,可以将策略收紧一些,根据需求限制源和目的地址。

如果条件允许的话,可以先将默认安全策略设置为 permit,待调通之后,再修改安全策略:

security-policy 
rule name tunnel_out 
source-zone trust 
destination-zone tunnel 
action permit 
  
rule name tunnel_in 
source-zone tunnel 
destination-zone trust 
action permit 
 
# 放行 tunnel 到 untrust 的流量 
rule name tunnel_untrust 
source-zone tunnel 
destination-zone untrust 
action permit 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

配置策略路由

[USG6300E]policy-based-route 

policy-based-route 
rule name PBR 
source-zone trust 
source-address 192.168.1.0 mask 255.255.255.0 
action pbr egress-interface Tunnel0 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

配置 No-NAT

设置去往隧道的流量不使用源地址转换:

[USG6300E-policy-nat]dis th 
nat-policy 
rule name SNAT 
source-zone tunnel 
destination-zone untrust 
source-address 192.168.1.0 mask 255.255.255.0 
action no-nat 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

验证

主要有如下几个测试方法:

  1. 在 CentOS 或 防火墙 ping 对端的隧道地址;
  2. 使用 192.168.1.0/24 网段内的设备 traceroute 公网地址,查看经过的路径以确认是否经过隧道转发。

Ubuntu 18 与 华为路由器建立 GRE 隧道

拓扑图

实现目标

  • Ubuntu 18 与华为路由器建立 GRE 隧道;
  • 华为防火墙背后的内网网段 192.168.1.0/24 通过 GRE 隧道从 CentOS 到 Internet;
  • Ubuntu 配置端口映射,将 192.168.1.10 的 8080 端口映射到 CentOS 的公网地址 200.1.1.1 的 8080 端口。

配置

  • Ubuntu

netplan 配置

root@ubunt18demo:~# vim /etc/netplan/00-installer-config.yaml 
network: 
  ethernets: 
    ens3: 
      addresses: 
        - 200.1.1.1/24 
      gateway4: 200.1.1.254 
      nameservers: 
        addresses: 
            - 114.114.114.114 
  tunnels: 
      tun0: 
        mode: gre 
        local: 200.1.1.1 
        remote: 100.1.1.1 
        addresses: [ 172.16.1.1/24 ] 
        routes: 
        - to: 192.168.1.0/24 
          via: 172.16.1.2 
 
# 可以先执行 netplan try 验证一下,如果没有断掉的话可以按 ENTER 确认配置 
# 如果和主机 SSH 中断,可以等待 120S 会自动恢复 
root@ubunt18demo:~# netplay try 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

iptables 设置

Ufw 是 Ubuntu 的防火墙配置工具,底层还是调用 iptables 处理的:

# 启用 ufw 
ufw enable 
 
# 放行 SSH 
ufw allow ssh 
 
# 放行 GRE 对端进入的流量 
ufw allow from 100.1.1.1/32 
 
# 配置 nat 映射 
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source 200.1.1.1 
iptables -t nat -A PREROUTING -d 200.1.1.1 -p tcp --dport 8080 -j DNAT --to-dest 192.168.1.10:8080 
 
# 将 ufw 设置为开机自启动 
systemctl enable ufw 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

开启 ipv4 转发:

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf 
 
sysctl -p 
  • 1.
  • 2.
  • 3.

华为路由器

以 AR1200 系列路由器为例:

  • 配置接口
interface Tunnel0/0/1 
ip address 172.16.1.2 255.255.255.0 
tunnel-protocol gre 
source 100.1.1.1 
destination 200.1.1.1 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 配置策略路由
# 配置 ACL 
[AR1200] acl number 3000 
[AR1200-acl-adv-3000] rule 10 permit ip destination 192.168.1.0 0.0.0.255 
 
# 配置流分类 
[AR1200] traffic classifier togretunnel 
[AR1200-classifier-togretunnel] if-match acl 3000 
 
# 配置流行为 
[AR1200] traffic behavior togretunnel 
[AR1200-behavior-togretunnel] redirect ip-nexthop 172.16.1.1 
 
# 配置流策略 
[AR1200] traffic policy togretunnel 
[AR1200-trafficpolicy-vlan10] classifier togretunnel behavior togretunnel 
 
# 在内网口调用流策略 
[AR1200] interface gigabitethernet 1/0/1 
[AR1200-GigabitEthernet3/0/0] traffic-policy togretunnel inbound 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

验证

验证方法同 CentOS 与 华为防火墙建立 GRE 隧道一致。

Juniper SRX 防火墙的 GRE 配置

SRX 防火墙的出接口如果使用了 route-instances,那么配置 tunnel 口时,一定要注意增加 route-instance destination,如下所示:

set interfaces gr-0/0/0 unit 0 tunnel source 100.1.1.1 
set interfaces gr-0/0/0 unit 0 tunnel destination 200.1.1.1 
set interfaces gr-0/0/0 unit 0 tunnel routing-instance destination EXAMPLE-INSTANCE 
set interfaces gr-0/0/0 unit 0 family inet address 172.16.1.2/24 
  • 1.
  • 2.
  • 3.
  • 4.

另外策略路由在 SRX 中称为 FBF,还有 No-NAT的配置示例如下:

# 配置 firewall filter,匹配需要进入隧道的流量 
set firewall filter to-GreTunnel term 1 from source-address 192.168.1.0/24 
set firewall filter to-GreTunnel term 1 then routing-instance EXAMPLE-INSTANCE 
set firewall filter to-GreTunnel term 3 then accept 
 
set routing-options rib-groups global import-rib EXAMPLE-INSTANCE.inet.0 
 
# 配置去往 Gre Tunnel 的路由 
set routing-instances EXAMPLE-INSTANCE instance-type forwarding 
set routing-instances EXAMPLE-INSTANCE routing-options interface-routes rib-group inet global 
set routing-instances EXAMPLE-INSTANCE routing-options static route 0.0.0.0/0 next-hop 172.16.1.1 
 
# 在内网口调用 firewall filter 
set interfaces reth2 unit 0 family inet filter input to-GreTunnel 
 
# 去往隧道口的流量不做 SNAT 
set security nat source rule-set Gre-snat from zone Trust 
set security nat source rule-set Gre-snat to zone EXAMPLE-INSTANCE 
set security nat source rule-set Gre-snat rule to-cn2-no-nat match source-address 192.168.1.0/24 
set security nat source rule-set Gre-snat rule to-cn2-no-nat match destination-address 0.0.0.0/0 
set security nat source rule-set Gre-snat rule to-cn2-no-nat then source-nat off 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

CentOS 的策略路由

如果有使用 Linux 作为中转的场景,也就是说华为防火墙和 Linux 建立 GRE 隧道,Linux 又和其他设备建立,由 Linux 做中转流量,这种场景下,可以在 Linux 配置策略路由,如下所示:

# 临时配置,重启后会消失,可以作为调试使用 
ip rule add from 192.168.1.0/24 table 100 pref 10 
ip route add 0.0.0.0/0 via 200.1.1.254 table 100 
 
# 将配置持久化 
vim /etc/sysconfig/network-scripts/rule-eth0 
from 192.168.1.0/24 table 100 pref 10 
 
vim /etc/sysconfig/network-scripts/route-eth0 
default via 172.16.1.1 dev tun0 
 
# 验证命令 
ip rule show 
ip route show table 100 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

总结

GRE虽然配置还比较简单, 在实际项目中会经常用到。 Linux 通常需要系统工程师配置,而网络设备通常由网络工程师配置,如果对接有问题,则需要沟通协调,就比较浪费时间。如果一个工程师能够完成两种设备的配置,并进行排错,那么效率将会提高很多。

 

责任编辑:武晓燕 来源: 新钛云服
相关推荐

2013-12-18 15:54:21

2009-12-16 14:32:14

Linux Mint系

2009-10-15 09:27:00

2009-12-18 18:29:43

Linux Fedor

2009-10-28 17:04:20

linux快速启动

2010-08-20 11:51:52

ADSL设备

2009-09-16 17:13:54

学习Linq

2020-04-20 21:22:50

网络设备网络协议计算机网络

2010-07-13 16:07:18

Perl

2009-08-19 09:24:43

AJAX引擎经验总结

2009-09-29 16:32:11

OJB Hiberna

2010-05-19 16:57:49

SVN与CVS的区别

2010-05-18 18:19:07

Subversion服

2009-10-21 14:43:42

linux网络设备

2010-05-18 17:12:11

Subversion安

2010-05-18 17:39:28

Subversion配

2021-09-09 14:54:10

Linuxbridge网络设备

2009-09-16 17:44:54

LINQ to SQL

2009-09-08 10:57:55

LINQ查询操作

2011-07-08 13:15:52

JSP
点赞
收藏

51CTO技术栈公众号