概览
读者将学习如何使用命令行(CLI)修改 IPsec Site-to-Site VPN 的默认配置。
目录
网络拓扑图
网络拓扑如下所示,EdgeRouter 上使用了以下接口:
ER-R
- eth0 (WAN) - 203.0.113.1
- eth1 (LAN) - 192.168.1.1/24
ER-L
- eth0 (WAN) - 192.0.2.1
- eth1 (LAN) - 172.16.1.1/24
使用图形化界面创建 IPsec Site-to-Site VPN
鉴于本文的目的,假设路由和接口配置已经到位并且已经测试了连通性。
与 IPsec 相关的端口和协议是:
- UDP 500 (IKE)
- Protocol 50 (ESP)
- UDP 4500 (NAT-T)
1.定义 IPsec Peer 和散列函数/加密算法
VPN > IPsec Site-to-Site > +Add Peer
- 勾选上 Show advanced options
- 去掉 Automatically open firewall and exclude from NAT
Peer: 192.0.2.1
Description: ipsec
Local IP: 203.0.113.1
Encryption: AES-128
Hash: SHA1
DH Group: 14
Pre-shared Secret: <secret>
Local subnet: 192.168.1.0/24
Remote subnet: 172.16.1.0/24
2.变更应用
1.定义 IPsec Peer 和散列函数/加密算法
VPN > IPsec Site-to-Site > +Add Peer
- 勾选上 Show advanced options
- 去掉 Automatically open firewall and exclude from NAT
Peer: 203.0.113.1
Description: ipsec
Local IP: 192.0.2.1
Encryption: AES-128
Hash: SHA1
DH Group: 14
Pre-shared Secret: <secret>
Local subnet: 172.16.1.0/24
Remote subnet: 192.168.1.0/24
2.变更应用
‘Automatically open firewall and exclude from NAT’ 选项,会向 iptables
中添加以下条目
UBNT_VPN_IPSEC_FW_HOOK
在 local 方向,允许 UDP 端口 500(IKE),UDP 端口 4500(NAT-T)和 ESP 。UBNT_VPN_IPSEC_FW_IN_HOOK
允许 local 和入站方向中从远程子网到本地子网的 IPsec 流量。UBNT_VPN_IPSEC_SNAT_HOOK
排除 NAT 中从本地子网到远程子网的所有流量。
您可以通过在 CLI 中输入以下命令来验证 iptables
firewall 是否生效
sudo iptables -L -vn
Chain UBNT_VPN_IPSEC_FW_HOOK (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 500,4500
0 0 ACCEPT esp -- * * 0.0.0.0/0 0.0.0.0/0
Chain UBNT_VPN_IPSEC_FW_IN_HOOK (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 172.16.1.0/24 192.168.1.0/24
sudo iptables -t nat -L -vn
Chain UBNT_VPN_IPSEC_SNAT_HOOK (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * * 192.168.1.0/24 172.16.1.0/24
使用 CLI 修改 VPN 配置
在上一节中,我们未启用 ‘automatic firewall and NAT exclusion' 功能, 按照下面的步骤天添加您自己的 IPsec firewall/NAT 规则:
1.进入到配置模式
configure
- 在 local 方向添加允许 IKE,NAT-T,ESP 和 IPsec 的防火墙规则。
set firewall name WAN_LOCAL rule 30 action accept
set firewall name WAN_LOCAL rule 30 description ike
set firewall name WAN_LOCAL rule 30 destination port 500
set firewall name WAN_LOCAL rule 30 log disable
set firewall name WAN_LOCAL rule 30 protocol udp
set firewall name WAN_LOCAL rule 40 action accept
set firewall name WAN_LOCAL rule 40 description esp
set firewall name WAN_LOCAL rule 40 log disable
set firewall name WAN_LOCAL rule 40 protocol esp
set firewall name WAN_LOCAL rule 50 action accept
set firewall name WAN_LOCAL rule 50 description nat-t
set firewall name WAN_LOCAL rule 50 destination port 4500
set firewall name WAN_LOCAL rule 50 log disable
set firewall name WAN_LOCAL rule 50 protocol udp
set firewall name WAN_LOCAL rule 60 action accept
set firewall name WAN_LOCAL rule 60 description ipsec
set firewall name WAN_LOCAL rule 60 destination address 192.168.1.0/24
set firewall name WAN_LOCAL rule 60 source address 172.16.1.0/24
set firewall name WAN_LOCAL rule 60 log disable
set firewall name WAN_LOCAL rule 60 ipsec match-ipsec
- 在 local 方向添加允许 IKE,NAT-T,ESP 和 IPsec 的防火墙规则。
set firewall name WAN_IN rule 30 action accept
set firewall name WAN_IN rule 30 description ipsec
set firewall name WAN_IN rule 30 destination address 192.168.1.0/24
set firewall name WAN_IN rule 30 source address 172.16.1.0/24
set firewall name WAN_IN rule 30 log disable
set firewall name WAN_IN rule 30 ipsec match-ipsec
4.添加防火墙规则,允许在入站方向上远程和本地子网之间的 IPsec 流量。
set service nat rule 5000 description ipsec-exclude
set service nat rule 5000 destination address 172.16.1.0/24
set service nat rule 5000 exclude
set service nat rule 5000 outbound-interface eth0
set service nat rule 5000 protocol all
set service nat rule 5000 source address 192.168.1.0/24
set service nat rule 5000 type masquerade
IPsec VPN 由两个阶段组成,每个阶段都使用自己的一组称为安全关联(SA)的散列/函数加密算法。阶段 1(P1)用于验证 Peer 并建立 VPN,而实际数据(流量)在阶段 2(P2)中传递。 因此,我们可以定义 P2 SA 专注于性能,以及 P1 SA 专注于安全性。任何形式的加密/散列的基本原则是安全性的增加,通常会导致性能下降。
可用的加密选项:
- AES128
- AES256
- AES128GCM128
- AES256GCM128
- 3DES
可用的散列选项:
- MD5
- SHA1
- SHA2-256
- SHA2-384
- SHA2-512
请按照以下步骤修改默认 VPN 设置:
1.显示当前的 IPsec VPN SA 配置(仅显示相关输出)。
show vpn
ipsec {
auto-firewall-nat-exclude disable
esp-group FOO0 {
proposal 1 {
encryption aes128
hash sha1
}
}
ike-group FOO0 {
proposal 1 {
dh-group 14
encryption aes128
hash sha1
}
}
}
...
如上面的输出所示,P1(ike-group FOO0)和 P2(esp-group FOO0)都使用相同的 SA 集(AES128 和 SHA1)。
2.更改 P1 和 P2 安全关联。
set vpn ipsec esp-group FOO0 proposal 1 encryption aes128
set vpn ipsec esp-group FOO0 proposal 1 hash md5
set vpn ipsec ike-group FOO0 proposal 1 encryption aes256
set vpn ipsec ike-group FOO0 proposal 1 hash sha256
3.更改 ESP / IKE lifetime(以秒为单位),以便降低隧道重新协商的频率。
set vpn ipsec esp-group FOO0 lifetime 43200
set vpn ipsec ike-group FOO0 lifetime 86400
4.禁用完全向前保密(PFS)。
set vpn ipsec esp-group FOO0 pfs disable
5.将 IKE 密钥交换版本从 v1 更改为版本 v2。
set vpn ipsec ike-group FOO0 key-exchange ikev2
6.将 IKE 密钥交换模式更改为野蛮模式(不推荐使用,仅支持 IKEv2)。
set vpn ipsec ike-group FOO0 mode aggressive
7.更改 IPsec 连接类型。
set vpn ipsec site-to-site peer 192.0.2.1 connection-type respond
8.即使没有检测到 NAT,也强制使用 NAT-T(UDP 4500)封装。
set vpn ipsec site-to-site peer 192.0.2.1 force-encapsulation enable
9.更改本地 IPsec 接口地址。
仅配置以下陈述之一。 使用以下选项确定哪种命令最适合您的情况:
(A) 您的 WAN 口通过 DHCP 获取地址
delete vpn ipsec site-to-site peer 192.0.2.1 local-address
set vpn ipsec site-to-site peer 192.0.2.1 dhcp-interface eth0
(B) 您的 WAN 口通过 PPPoE 获取地址
set vpn ipsec site-to-site peer 192.0.2.1 local-address 0.0.0.0
10.如果远端 Peer 位于 NAT 后面(与 L2TP 不兼容),则将对等地址更改为 0.0.0.0。
delete vpn ipsec site-to-site peer 192.0.2.1
set vpn ipsec site-to-site peer 0.0.0.0 …
11.启用死对等检测(DPD)。
set vpn ipsec ike-group FOO0 dead-peer-detection action restart
set vpn ipsec ike-group FOO0 dead-peer-detection interval 30
set vpn ipsec ike-group FOO0 dead-peer-detection timeout 120
hold
VPN 状态被置于“on-hold”状态,但保留策略直到重新启动流量。clear
VPN 状态和策略被清除,隧道被拆除。restart
VPN 状态和策略重新启动,路由器尝试重新协商遂道。
DPD interval
是这些消息开始之后的秒数。 timeout
是尝试失败后重新发送消息的秒数。
12.启用 P2(ESP)IPsec 流量加速。
set system offload ipsec enable
13.提交更改并保存配置。
commit ; save
- set vpn ipsec ipsec-interfaces
- set vpn ipsec nat-traversal
- set vpn ipsec nat-networks
您可以使用 CLI 界面中的以下命令验证 VPN 设置:
show firewall name WAN_LOCAL statistics
show firewall name WAN_IN statistics
show nat statistics
show vpn ipsec sa
show vpn log