bash
# https://www.strongswan.org/testing/testresults/ikev2/net2net-psk/sun.swanctl.conf
connections {
gw-gw {
local_addrs = 192.168.0.2
remote_addrs = 192.168.0.1
local {
auth = psk
id = sun.strongswan.org
}
remote {
auth = psk
id = moon.strongswan.org
}
children {
net-net {
local_ts = 10.2.0.0/16
remote_ts = 10.1.0.0/16
updown = /usr/local/libexec/ipsec/_updown iptables
rekey_time = 5400
rekey_bytes = 500000000
rekey_packets = 1000000
esp_proposals = aes128gcm128-x25519
}
}
version = 2
mobike = no
reauth_time = 10800
proposals = aes128-sha256-x25519
}
}
secrets {
ike-1 {
id-moon = moon.strongswan.org
id-sun =sun.strongswan.org
secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
}
}
# https://www.strongswan.org/testing/testresults/ikev2/net2net-psk/moon.swanctl.conf
connections {
gw-gw {
local_addrs = 192.168.0.1
remote_addrs = 192.168.0.2
local {
auth = psk
id = moon.strongswan.org
}
remote {
auth = psk
id = sun.strongswan.org
}
children {
net-net {
local_ts = 10.1.0.0/16
remote_ts = 10.2.0.0/16
updown = /usr/local/libexec/ipsec/_updown iptables
rekey_time = 5400
rekey_bytes = 500000000
rekey_packets = 1000000
esp_proposals = aes128gcm128-x25519
}
}
version = 2
mobike = no
reauth_time = 10800
proposals = aes128-sha256-x25519
}
}
secrets {
ike-1 {
id-1 = moon.strongswan.org
secret = 0x45a30759df97dc26a15b88ff
}
ike-2 {
id-2 = sun.strongswan.org
secret = "This is a strong password"
}
ike-3 {
id-3a = moon.strongswan.org
id-3b =sun.strongswan.org
secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
}
ike-4 {
secret = 'My "home" is my "castle"!'
}
ike-5 {
id-5 = 192.168.0.1
secret = "Andi's home"
}
}
好的,我来解释一下这对 strongSwan IPsec 配置文件的含义。 这两个配置文件分别用于 VPN 连接的两端,我们称之为 "sun" 和 "moon"。 它们定义了一个网关到网关 (gateway-to-gateway) 的 IPsec VPN 连接,使用预共享密钥 (PSK) 进行身份验证。
总体结构:
两个配置文件都包含两个主要部分:
connections
: 定义了 VPN 连接的参数,例如本地/远程地址、身份验证方式、流量选择器等。secrets
: 存储了用于身份验证的预共享密钥。
详细解释 (以 sun.swanctl.conf 为例,moon.swanctl.conf 类似,但参数相反):
sun.swanctl.conf:
ini
connections {
gw-gw {
local_addrs = 192.168.0.2
remote_addrs = 192.168.0.1
local {
auth = psk
id = sun.strongswan.org
}
remote {
auth = psk
id = moon.strongswan.org
}
children {
net-net {
local_ts = 10.2.0.0/16
remote_ts = 10.1.0.0/16
updown = /usr/local/libexec/ipsec/_updown iptables
rekey_time = 5400
rekey_bytes = 500000000
rekey_packets = 1000000
esp_proposals = aes128gcm128-x25519
}
}
version = 2
mobike = no
reauth_time = 10800
proposals = aes128-sha256-x25519
}
}
secrets {
ike-1 {
id-moon = moon.strongswan.org
id-sun = sun.strongswan.org
secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL
}
}
- connections: 定义了一个名为
gw-gw
(gateway-to-gateway) 的连接。local_addrs = 192.168.0.2
: 本地 VPN 网关的 IP 地址是 192.168.0.2 (sun)。remote_addrs = 192.168.0.1
: 远程 VPN 网关的 IP 地址是 192.168.0.1 (moon)。local { auth = psk; id = sun.strongswan.org }
: 本地身份验证配置: 使用预共享密钥 (psk
) 认证,本地身份标识是sun.strongswan.org
。remote { auth = psk; id = moon.strongswan.org }
: 远程身份验证配置: 使用预共享密钥 (psk
) 认证,远程身份标识是moon.strongswan.org
。children { net-net { ... } }
: 定义了 SA (Security Association,安全联盟) 的子配置,用于保护实际的数据流量。local_ts = 10.2.0.0/16
: 本地流量选择器 (Traffic Selector),指定了本地网络是 10.2.0.0/16 网段。 这意味着从 10.2.0.0/16 发出的流量会被加密并通过 VPN 隧道传输。remote_ts = 10.1.0.0/16
: 远程流量选择器,指定了远程网络是 10.1.0.0/16 网段。 这意味着去往 10.1.0.0/16 的流量会被加密并通过 VPN 隧道传输。updown = /usr/local/libexec/ipsec/_updown iptables
: 指定了在 SA 建立或断开时执行的脚本。 这个脚本通常用于配置防火墙规则,允许或阻止通过 VPN 隧道的流量。rekey_time = 5400
: SA 重新密钥的时间间隔 (秒)。rekey_bytes = 500000000
: SA 重新密钥的数据量 (字节)。rekey_packets = 1000000
: SA 重新密钥的数据包数量。esp_proposals = aes128gcm128-x25519
: 指定了用于 ESP (Encapsulating Security Payload) 的加密算法。 这里使用的是 AES-128-GCM 搭配 X25519 密钥交换算法。
version = 2
: 指定使用 IKEv2 协议。mobike = no
: 禁用 MOBIKE (Mobility and Multihoming Protocol)。reauth_time = 10800
: 重新身份验证的时间间隔 (秒)。proposals = aes128-sha256-x25519
: 指定了用于 IKE (Internet Key Exchange) 协商的加密算法。 这里使用的是 AES-128 加密,SHA-256 哈希算法,搭配 X25519 密钥交换算法。
- secrets: 定义了预共享密钥。
ike-1 { id-moon = moon.strongswan.org; id-sun = sun.strongswan.org; secret = 0sv+NkxY9LLZvwj4qCC2o/gGrWDF2d21jL }
: 指定了用于moon.strongswan.org
和sun.strongswan.org
之间身份验证的预共享密钥。 注意,这个密钥必须在两个配置文件中完全相同。
moon.swanctl.conf:
与 sun.swanctl.conf
类似,但是 local_addrs
和 remote_addrs
以及 local_ts
和 remote_ts
的值是相反的。 重要的是,secrets
部分中 ike-1
的 secret
值必须与 sun.swanctl.conf
中相同。
总结:
这个配置定义了一个简单的 IKEv2 IPsec VPN,使用预共享密钥进行身份验证,并保护 10.2.0.0/16 和 10.1.0.0/16 两个网段之间的流量。
关键点:
- IP 地址: 确保
local_addrs
和remote_addrs
设置正确,并且两个 VPN 网关可以互相访问。 - 身份标识: 确保
id
(例如sun.strongswan.org
和moon.strongswan.org
) 在两个配置文件中匹配。 - 预共享密钥: 确保预共享密钥在两个配置文件中完全相同。
- 流量选择器: 确保
local_ts
和remote_ts
正确定义了需要保护的流量。 - 防火墙规则: 需要配置防火墙规则,允许 UDP 500 和 4500 端口的流量通过,以及 ESP 协议的流量通过。
- NAT 穿透: 如果 VPN 网关位于 NAT 设备之后,可能需要配置 NAT 穿透。