RouterOS connect Linode use Gre over IPsec

  Site to Site 建立的VPN 标准情况下需要预先协商哪些网段经过VPN,所以每次有任何一侧需要增加网段而改动配置都会导致VPN 中断,这个在生产环境里会很难接受。而对于个人用户来说Site to Site VPN 又很难利用另一侧的网络出口访问Internet。本文的思路是在两端使用非本地网段的回环接口地址建立IPSEC VPN,然后使用这两个地址来建立GRE 通道,随后相关路由指向GRE 通道地址即可。


RouterOS:-------
                                                           |IPSEC|
Linode:  -------

Linux 部分,系统是Debian Jessie

安装所需软件

$ sudo apt-get install strongswan

建立回环地址,建立GRE 通道

/etc/network/interfaces
auto lo lo:1
iface lo inet loopback

iface lo:1 inet static ##本地添加一个2.2.2.2/32 的回环地址,用于建立VPN的感兴趣流
        address 2.2.2.2
        netmask 255.255.255.255

auto tun0 ##GRE 通道设置##
iface tun0 inet tunnel
        address 5.5.5.2 ##本地通道地址##
        netmask 255.255.255.252
        mode gre
        endpoint 1.1.1.1 ##对端用于建立通道的地址,即对端VPN的感兴趣流地址##
        dstaddr 5.5.5.1 ##对端通道地址##
        local 2.2.2.2 ##本地用于建立通道的地址,即本地VPN的感兴趣流地址##
        ttl 64
        mtu 1476
        up ifconfig tun0 multicast ##通道生效后启用多播,用于路由协议##

打开本机的ipv4 转发:

echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
sysctl -p /etc/sysctl.conf

允许Aggressive 模式,因为Aggressive 模式并不安全,默认是关闭的


/etc/strongswan.d/charon.conf
i_dont_care_about_security_and_use_aggressive_mode_psk = yes

IPSEC 配置


/etc/ipsec.conf
config setup ##基本配置##
        interfaces="ipsec0=eth0"
        klipsdebug=none
        plutodebug=all
        uniqueids=yes

conn %default ##所有VPN的通用配置##
        keyingtries=0
        authby=rsasig

conn ros ##与RouterOS建立VPN 的配置##
        aggressive=yes ##使用暴力模式##
        left= ##本机公网地址##
        leftsubnet=2.2.2.2/32 ##本机用于建立感兴趣流的回环地址##
        right=%any ##对端来源地址,由于对端是动态地址,所以此处设置any##
        rightid=@ ##对端的fqdn 验证##
        rightsubnet=1.1.1.1/32 ##对端用于建立感兴趣流的回环地址##
        keyexchange=ike ##VPN协议##
        authby=secret ##ike阶段验证方式,使用presharekey##
        auth=esp ##使用的协议##
        ike=3des-sha1-modp1024 ##ike阶段参数,即p1##
        esp=3des-md5-modp1024 ##esp阶段参数,即p2##
        pfs=no ##不使用pfs##
        type=tunnel ##类型##
        auto=start ##start表示strongswan开启后即连接此VPN##
/etc/ipsec.secrets
: PSK  ##presharekey 设置##

如果想让RouterOS 通过GRE 通道上网,需要增加iptables 规则

/sbin/iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -t nat -A POSTROUTING -s 192.168.88.0/24 -o eth0 -j MASQUERADE ##此处地址为RouterOS 侧的内网地址,按需配置##

RouterOS 部分

GRE 通道

/interface gre
add !keepalive local-address=1.1.1.1 mtu=1476 name=gre-linode remote-address=2.2.2.2

增加回环口,GRE通道地址

/ip address
add address=1.1.1.1 interface=loopback network=1.1.1.0
add address=5.5.5.1/30 interface=gre-linode network=5.5.5.0

针对回环口与GRE 通道口的src-nat设置

以下条目记得移动到masquerade 条目之上

/ip firewall nat
add chain=srcnat dst-address=2.2.2.2 src-address=1.1.1.1
add chain=srcnat dst-address=5.5.5.2 src-address=5.5.5.1

IPSEC VPN 设置

/ip ipsec peer
add address=/32 enc-algorithm=3des exchange-mode=aggressive generate-policy=port-override local-address=0.0.0.0 my-id=fqdn: nat-traversal=no secret=

/ip ipsec proposal
add auth-algorithms=md5 enc-algorithms=3des lifetime=1h name=linode pfs-group=none

/ip ipsec policy
add dst-address=2.2.2.2/32 proposal=linode sa-dst-address= sa-src-address=0.0.0.0 src-address=1.1.1.1/32 tunnel=yes

使用GRE 通道访问Internet,需要修改MSS 值

/ip firewall mangle
add action=change-mss chain=postrouting new-mss=1300 out-interface=gre-linode protocol=tcp tcp-flags=syn

有关使用GRE 通道上网

  最简单就是加一条默认路由,把所有流量扔给Linode 侧的GRE 通道地址就可以了。如果想拿来翻墙区分国内国外流量,本文不做讨论,请用“freedomroutes.domestic”“routeros” 等关键词搜索即可。