Featured image of post OpenWrt 搭建传统 IKEv1 (Cisco IPSec XAuth) VPN 服务端

OpenWrt 搭建传统 IKEv1 (Cisco IPSec XAuth) VPN 服务端

适用场景:适用于老旧客户端、不支持 IKEv2 的设备,采用预共享密钥 (PSK) + 用户名密码 (XAuth) 方式拨入。
底层软件:strongSwan (starter 模式) + firewall4 (nftables)


一、环境说明与避坑须知

  1. 软件冲突排查:
    • OpenWrt 软件源中存在古老的 openswan,容易与 strongswan 产生命名冲突。
    • 如果安装了 openswan,必须先卸载,否则 /usr/sbin/ipsec 会指向错误的二进制解析器导致语法解析崩溃。
  2. Web 插件说明:
    • 社区版插件(如 luci-app-ipsec-vpnd)模板陈旧,生成的语法(如 charondebug、rightdns 等)会导致 strongSwan 5.9+ 解析报错。建议采用纯配置文件管理。
  3. 防火墙版本:
    • OpenWrt 22.03+ / 24.10+ 使用的是 firewall4 (nftables),传统的 /etc/firewall.user 默认失效,需使用 .nft 自定义链。

二、安装必要软件包

登录 OpenWrt 终端,执行:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 1. 检查并移除冲突包
opkg remove openswan --force-depends 2>/dev/null

# 2. 安装 strongSwan 及 starter 基础组件
opkg update
opkg install strongswan \
             strongswan-ipsec \
             strongswan-mod-stroke \
             strongswan-mod-xauth-generic \
             strongswan-mod-kernel-libipsec \
             strongswan-mod-aes \
             strongswan-mod-gmp \
             strongswan-mod-hmac \
             strongswan-mod-openssl \
             strongswan-mod-socket-default \
             strongswan-mod-kernel-netlink \
             strongswan-mod-random \
             strongswan-mod-updown \
             strongswan-mod-sha1 \
             strongswan-mod-sha2

三、配置文件编写

1. 主配置文件 /etc/ipsec.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
cat << 'EOF' > /etc/ipsec.conf
# /etc/ipsec.conf - strongSwan IPsec configuration file

config setup
	uniqueids=no

conn %default
	dpdaction=clear
	dpddelay=300s
	rekey=no
	left=%defaultroute
	leftfirewall=yes
	right=%any
	ikelifetime=60m
	keylife=20m
	rekeymargin=3m
	keyingtries=1
	auto=add

# Cisco IPSec (IKEv1 with PSK + XAuth)
conn IKEv1-PSK-XAuth
	keyexchange=ikev1
	leftsubnet=0.0.0.0/0
	rightsubnet=10.1.20.16/28
	rightsourceip=10.1.20.16/28
	ike=aes128-sha1-modp2048,aes128-sha1-modp1024,3des-sha1-modp1024
	esp=aes128-sha1,3des-sha1
	leftauth=psk
	rightauth=psk
	rightauth2=xauth
EOF

注意:配置段落内必须使用 制表符 (Tab) 或空格缩进,避免顶格引发语法报错。

2. 凭证文件 /etc/ipsec.secrets

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
cat << 'EOF' > /etc/ipsec.secrets
# 预共享密钥 (PSK)
: PSK "YourPreSharedKey123"

# XAuth 客户端账号密码 (格式: 用户名 : XAUTH '密码')
user1 : XAUTH 'UserPass123'
user2 : XAUTH 'UserPass456'
EOF

chmod 600 /etc/ipsec.secrets

四、配置 Firewall4 (nftables) 转发与 NAT

在 OpenWrt 24.10 下,添加自定义 nftables 规则,解决内网互通、出网 MASQUERADE 及 TCP MSS 钳制:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 1. 编写自定义 nft 规则
cat << 'EOF' > /etc/nftables.d/ipsec-vpn.nft
# TCP MSS 钳制 (解决打不开网页、TCP 连接卡死问题)
chain mangle_ipsec {
	type filter hook forward priority mangle - 1; policy accept;
	tcp flags syn tcp option maxseg size set 1360
}

# 放行双向转发
chain forward_ipsec {
	type filter hook forward priority filter - 1; policy accept;
	ip saddr 10.1.20.16/28 accept
	ip daddr 10.1.20.16/28 accept
}

# 出网 NAT 伪装
chain srcnat_ipsec {
	type nat hook postrouting priority srcnat - 1; policy accept;
	ip saddr 10.1.20.16/28 masquerade
}
EOF

# 2. 注入 firewall4 配置
uci add firewall include
uci set firewall.@include[-1].path='/etc/nftables.d/ipsec-vpn.nft'
uci set firewall.@include[-1].type='nftables'
uci commit firewall

# 3. 确保开启系统内核路由转发
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/99-ipforward.conf

# 4. 重载防火墙
/etc/init.d/firewall reload

五、服务启停与运维命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 开机自启与启动服务
/etc/init.d/ipsec enable
/etc/init.d/ipsec restart

# 查看服务状态
/etc/init.d/ipsec status

# 修改密码后热重载(无需断开其他用户)
ipsec stroke rereadsecrets

# 查看当前在线用户与 SA 状态
ipsec stroke statusall
## 例如如下输出
Status of IKE charon daemon (strongSwan 6.0.7, Linux 6.12.112, x86_64):
  uptime: 15 minutes, since Oct 20 20:59:14 2026
  worker threads: 7 of 16 idle, 5/0/4/0 working, job queue: 0/0/0/0, scheduled: 4
  loaded plugins: charon aes sha2 sha1 random nonce openssl gmp hmac kernel-libipsec kernel-netlink socket-default stroke updown xauth-generic
Virtual IP pools (size/online/offline):
  10.1.20.16/28: 14/1/0
Listening IP addresses:
  192.168.1.11
  240e:b8f:6d10:b100:60e:3cff:fe1c:75cb
  172.17.0.1
Connections:
IKEv1-PSK-XAuth:  %any...%any  IKEv1, dpddelay=300s
IKEv1-PSK-XAuth:   local:  uses pre-shared key authentication
IKEv1-PSK-XAuth:   remote: uses pre-shared key authentication
IKEv1-PSK-XAuth:   remote: uses XAuth authentication: any
IKEv1-PSK-XAuth:   child:  0.0.0.0/0 === 10.1.20.16/28 TUNNEL, dpdaction=none
Security Associations (1 up, 0 connecting):
IKEv1-PSK-XAuth[2]: ESTABLISHED 7 seconds ago, 192.168.1.11[192.168.1.11]...101.90.31.56[192.168.8.105]
IKEv1-PSK-XAuth[2]: Remote XAuth identity: hello
IKEv1-PSK-XAuth[2]

六、客户端连接指引 (以 macOS 为例)

  1. 设置 -> 网络 -> 添加 VPN -> 选择 Cisco IPSec;

  2. 服务器地址:填路由器公网 IP 或 DDNS 域名;

  3. 账户名称 / 密码:user1 / UserPass123;

  4. 机器认证 (预共享密钥):选择共享密钥,输入 YourPreSharedKey123;

  5. 点击连接即可成功分配 10.1.20.x 虚拟 IP,实现访问局域网和外网。


七、最后,为 IPSec VPN 追加防火墙接口和区域

前面的配置完成后,客户端已经可以正常拨入连接了。

但是,还有个问题卡在这里:

  1. 客户端 访问不了 路由器的web 管理页面
  2. 客户端 也不能经过 vpn server 访问 公网 (例如 www.baidu.com)
  3. 客户端 也访问不了 内网的其他设备 (例如 192.168.1.177)

这几个问题不解决,明显是用不了的。该如何处理呢?

其实,需要为 IPSec VPN server 关联一个内部接口。

在一开始 安装 strongswan-mod-kernel-libipsec 包的时候,系统中就多了一个 ipsec0 的接口。

只需为这个VPN 接口关联一个 内部ip 和 防火墙规则 即可。

具体步骤如下

  1. “网络” -> “接口” 中点击 “添加新接口",名字随便,关键是要关联 ipsec0 这个VPN接口

  2. 点击"创建接口"后,进入接口编辑页面。这里改动的地方不多,追加一个 vpn 接口地址就行—— 例如我这里使用的 10.0.20.1

完成后,点击保存。并应用新的接口配置。

  1. “网络” -> “防火墙” 中点击 “添加” ,新建一个叫 VPN 的防火墙区域,并关联之前创建好的 接口ipsec_vpn

注意,这里 入站数据 出站数据 和 区域内转发 规则都改为 接受。

​ 最后,转发区域这里 把"允许转发到目标区域” 和 “允许来自源区域的转发"中按照如下配置,追加内网区域 ( lan ) 和公网区域 ( wan )——

保存后,同样需要点击 “保存并应用“让配置生效。

​

  1. 测试验证

    最后,客户端拨通 VPN 再测试验证下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    # 访问百度
    traceroute: Warning: www.baidu.com has multiple addresses; using 180.101.49.44
    traceroute to www.a.shifen.com (180.101.49.44), 64 hops max, 52 byte packets
     1  10.1.20.1 (10.1.20.1)  36.657 ms  30.415 ms  29.914 ms
     2  192.168.1.1 (192.168.1.1)  29.175 ms  61.533 ms  44.057 ms
     3  * * *
     4  * 124.75.246.113 (124.75.246.113)  60.011 ms
        124.75.246.73 (124.75.246.73)  63.740 ms
     5  222.72.236.58 (222.72.236.58)  39.785 ms
    
     # ping 内网主机
     % ping 192.168.100.177
    PING 192.168.100.177 (192.168.100.177): 56 data bytes
    64 bytes from 192.168.100.177: icmp_seq=0 ttl=63 time=28.923 ms
    64 bytes from 192.168.100.177: icmp_seq=1 ttl=63 time=41.691 ms
    ^C
    
    # 访问 路由器 管理页面
    % curl -x "" -I http://192.168.100.1/cgi-bin/luci/admin
    HTTP/1.1 403 Forbidden
    Connection: Keep-Alive
    Keep-Alive: timeout=20
    x-luci-login-required: yes
    content-type: text/html
    cache-control: no-cache
    expires: 0
    x-frame-options: SAMEORIGIN
    x-xss-protection: 1; mode=block
    x-content-type-options: nosniff