简介
Policy-based VPN有很大的局限性,尤其是无法支持路由。而常用的基于路由的IPSec VPN所用到的VTi接口,要求两端WAN口都是固定IP,需要专线,这对大多数中小企业来说网络使用成本太高。
我们这里介绍如何配置GRE over IPSec, 通过GRE隧道,就可以使用各种路由协议灵活配置你的网络。
下图是本文用到示范网络。左端是EdgeRouter,右端是VyOS。
预先准备
路由设备
实现本文介绍的功能,你需要一台EdgeRouter和安装好VyOS的软路由。在参考本文开始配置以前,我们假定大家已经按照介绍VyOS一文完成了VyOS的初始化配置。使用EdgeOS的用户,可以使用系统自带的wizard完成初始配置。
设置动态域名
考虑到大多数客户的宽带接入都是动态IP地址, 因此需要通过域名来获取当前使用的IP地址。我们假定左边EdgeOS的域名是edgeos.ddns.com,右边的VyOS的域名是vyos.ddns.com
如何配置动态域名,请参见本站另一篇帖子EdgeRouter/VyOS配置DDNS动态域名。
基于规则的IPSec VPN
GRE隧道两端各用一个固定IP作为端口外围地址。
你可以使用路由器上任何接口IP地址,但是有一个问题就是,如果那个接口离线,相应的GRE接口也会进入离线状态。因此使用永远在线的loopback接口就有优势了。
配置loopback地址
下面我们为EdgeOS端loopback端口配置一个IP地址,10.255.0.254/32。
configure set interfaces loopback lo address 10.255.0.254/32
同样,再到VyOS配置相应的loopback地址,10.255.1.254/32。
configure set interfaces loopback lo address 10.255.1.254/32
配置IPSec隧道
我们首先要配置一条承载GRE的IPSec隧道,具体配置方法我们已经在如何设置分支机构互连(一)IPSec VPN一文中有了详细解释,这里就不再赘述。请注意替换原文中local prefix和remote prefix分别为两端的loopback地址。
完成后,可以从EdgeOS端使用以下命令测试隧道是否通畅。
sudo ping 10.255.1.254 -I 10.255.0.254
如果隧道还没建立起来,IPSec进程在监控到隧道流量后,会自动启动连接,连接过程中丢失几个包是正常现象。
GRE接口
GRE隧道配置有5个必配要素:
- 隧道名称,以tun起头,后面接从0到255的数字。
- 隧道本地外部地址 local-ip
- 隧道对端外部地址 remote-ip
- 隧道内部地址 address
- 最大传输单元 mtu,这个是包括IP包头的,和MSS不一样。
由于GRE是点到点隧道。因此隧道内两端各只有一个IP地址,因此CIDR必须是30。下面是EdgeOS侧GRE隧道配置的参考命令:
set interfaces tunnel tun0 address 10.254.0.1/30 set interfaces tunnel tun0 description 'EdgeOS ... (ipsec) ... VyOS' set interfaces tunnel tun0 encapsulation gre set interfaces tunnel tun0 local-ip 10.255.0.254 set interfaces tunnel tun0 mtu 1390 set interfaces tunnel tun0 multicast disable set interfaces tunnel tun0 remote-ip 10.255.1.254
同样,我们在VyOS也配置相应的GRE隧道:
set interfaces tunnel tun0 address 10.254.0.2/30 set interfaces tunnel tun0 description 'VyOS ... (ipsec) ... EdgeOS' set interfaces tunnel tun0 encapsulation gre set interfaces tunnel tun0 local-ip 10.255.1.254 set interfaces tunnel tun0 mtu 1390 set interfaces tunnel tun0 multicast disable set interfaces tunnel tun0 remote-ip 10.255.0.254
防火墙策略
由于我们相当与在WAN口对接了GRE隧道(IPSec流量来自于WAN口),因此还需要对应地配置一条防火墙策略,允许WAN口接入GRE隧道。
EdgeOS的auto-firewall-nat-exclude并不包括这一条规则,需要参考如下命令添加:
set firewall name WAN_LOCAL rule 210 action accept set firewall name WAN_LOCAL rule 210 description 'ipsec' set firewall name WAN_LOCAL rule 210 ipsec match-ipsec set firewall name WAN_LOCAL rule 210 log disable set firewall name WAN_LOCAL rule 210 protocol all
VyOS端,我们之前已经做过这个配置,不必重复了。
路由
要让流量通过GRE隧道转发,还需要配置相应的路由。下面我们就用静态路由作为参考。
EdgeOS端
set protocols static route 10.1.0.0/24 next-hop 10.254.0.2 description 'right side via GRE over IPSec' set protocols static route 10.1.0.0/24 next-hop 10.254.0.2 distance 100 commit;save;exit
VyOS端
set protocols static route 10.0.0.0/24 next-hop 10.254.0.1 description 'right side via GRE over IPSec' set protocols static route 10.0.0.0/24 next-hop 10.254.0.1 distance 100 commit;save;exit