清理失效IPsecSA¶
清理失效sa¶
一、ROS-A 新增 SA 专用 Netwatch¶
ROS-A 当前参数:
执行:
/tool netwatch
add name="SA-A-WAN1" host=223.5.5.5 src-address=10.240.0.48 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-A-WAN2" host=1.2.4.8 src-address=10.240.0.48 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-A-WAN3" host=119.29.29.29 src-address=10.240.0.48 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-A-GRE" host=198.19.13.1 src-address=198.19.13.2 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
检查新增条目:
二、ROS-A SA watchdog 脚本¶
如果确认还没有 sa-watchdog-a,执行:
/system script
add name="sa-watchdog-a" policy=read,write,policy,test,sensitive source={
:global saAWdBad
:global saAWdCooldown
:if ([:typeof $saAWdBad] = "nothing") do={
:set saAWdBad 0
}
:if ([:typeof $saAWdCooldown] = "nothing") do={
:set saAWdCooldown 0
}
:if ($saAWdCooldown > 0) do={
:set saAWdCooldown ($saAWdCooldown - 1)
}
:local wan1 [/tool netwatch get [find where name="SA-A-WAN1"] status]
:local wan2 [/tool netwatch get [find where name="SA-A-WAN2"] status]
:local wan3 [/tool netwatch get [find where name="SA-A-WAN3"] status]
:local internetOk false
:if (($wan1 = "up") || ($wan2 = "up") || ($wan3 = "up")) do={
:set internetOk true
}
# All independent WAN probes are down.
# Treat it as an Internet failure and do not reset IPsec.
:if ($internetOk = false) do={
:set saAWdBad 0
:return
}
:local greStatus [/tool netwatch get [find where name="SA-A-GRE"] status]
:local activeCount [:len [/ip ipsec active-peers find]]
:local establishedCount [:len [/ip ipsec active-peers find where state="established"]]
:local matureCount [:len [/ip ipsec installed-sa find where state="mature"]]
:local saHealthy false
:if (($establishedCount > 0) && ($matureCount >= 2)) do={
:set saHealthy true
}
# IPsec SA is healthy but GRE endpoint is down.
# This is more likely a remote GRE problem, so do not reset SA.
:if (($greStatus != "up") && ($saHealthy = true)) do={
:set saAWdBad 0
:return
}
:local tunnelHealthy false
:if (($greStatus = "up") && ($saHealthy = true)) do={
:set tunnelHealthy true
}
:if ($tunnelHealthy = true) do={
:set saAWdBad 0
:return
}
:set saAWdBad ($saAWdBad + 1)
:log warning ("SA-A abnormal: count=" . $saAWdBad . " gre=" . $greStatus . " established=" . $establishedCount . " mature-sa=" . $matureCount)
# Three consecutive abnormal checks.
# Scheduler interval is 30 seconds, so approximately 90 seconds.
:if (($saAWdBad >= 3) && ($saAWdCooldown = 0)) do={
:local peerCfg [/ip ipsec peer find where name="peer-ct"]
:local policyCfg [/ip ipsec policy find where peer="peer-ct" and protocol=gre]
:if ([:len $peerCfg] = 0) do={
:log error "SA-A peer-ct configuration not found"
:set saAWdBad 0
:return
}
:if ([:len $policyCfg] = 0) do={
:log error "SA-A GRE IPsec policy not found"
:set saAWdBad 0
:return
}
:log error "SA-A resetting peer-ct"
/ip ipsec policy set $policyCfg disabled=yes
/ip ipsec peer set $peerCfg disabled=yes
:delay 3s
# Current ROS-A has only one active IPsec peer.
/ip ipsec active-peers kill-connections
:delay 2s
/ip ipsec peer set $peerCfg disabled=no
/ip ipsec policy set $policyCfg disabled=no
:set saAWdBad 0
:set saAWdCooldown 10
:log warning "SA-A peer-ct reset completed"
}
}
添加调度器:
/system scheduler
add name="sa-watchdog-a-scheduler" start-time=startup interval=30s on-event="/system script run sa-watchdog-a" policy=read,write,policy,test,sensitive
三、ROS-B 新增 SA 专用 Netwatch¶
ROS-B 当前参数:
执行:
/tool netwatch
add name="SA-B-WAN1" host=223.5.5.5 src-address=10.240.0.46 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-B-WAN2" host=1.2.4.8 src-address=10.240.0.46 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-B-WAN3" host=119.29.29.29 src-address=10.240.0.46 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
add name="SA-B-GRE" host=198.19.13.9 src-address=198.19.13.10 type=icmp interval=10s timeout=2s packet-count=5 packet-interval=200ms thr-loss-percent=50%
检查新增条目:
四、ROS-B SA watchdog 脚本¶
/system script
add name="sa-watchdog-b" policy=read,write,policy,test,sensitive source={
:global saBWdBad
:global saBWdCooldown
:if ([:typeof $saBWdBad] = "nothing") do={
:set saBWdBad 0
}
:if ([:typeof $saBWdCooldown] = "nothing") do={
:set saBWdCooldown 0
}
:if ($saBWdCooldown > 0) do={
:set saBWdCooldown ($saBWdCooldown - 1)
}
:local wan1 [/tool netwatch get [find where name="SA-B-WAN1"] status]
:local wan2 [/tool netwatch get [find where name="SA-B-WAN2"] status]
:local wan3 [/tool netwatch get [find where name="SA-B-WAN3"] status]
:local internetOk false
:if (($wan1 = "up") || ($wan2 = "up") || ($wan3 = "up")) do={
:set internetOk true
}
:if ($internetOk = false) do={
:set saBWdBad 0
:return
}
:local greStatus [/tool netwatch get [find where name="SA-B-GRE"] status]
:local activeCount [:len [/ip ipsec active-peers find]]
:local establishedCount [:len [/ip ipsec active-peers find where state="established"]]
:local matureCount [:len [/ip ipsec installed-sa find where state="mature"]]
:local saHealthy false
:if (($establishedCount > 0) && ($matureCount >= 2)) do={
:set saHealthy true
}
:if (($greStatus != "up") && ($saHealthy = true)) do={
:set saBWdBad 0
:return
}
:local tunnelHealthy false
:if (($greStatus = "up") && ($saHealthy = true)) do={
:set tunnelHealthy true
}
:if ($tunnelHealthy = true) do={
:set saBWdBad 0
:return
}
:set saBWdBad ($saBWdBad + 1)
:log warning ("SA-B abnormal: count=" . $saBWdBad . " gre=" . $greStatus . " established=" . $establishedCount . " mature-sa=" . $matureCount)
:if (($saBWdBad >= 3) && ($saBWdCooldown = 0)) do={
:local peerCfg [/ip ipsec peer find where name="peer-cm"]
:local policyCfg [/ip ipsec policy find where peer="peer-cm" and protocol=gre]
:if ([:len $peerCfg] = 0) do={
:log error "SA-B peer-cm configuration not found"
:set saBWdBad 0
:return
}
:if ([:len $policyCfg] = 0) do={
:log error "SA-B GRE IPsec policy not found"
:set saBWdBad 0
:return
}
:log error "SA-B resetting peer-cm"
/ip ipsec policy set $policyCfg disabled=yes
/ip ipsec peer set $peerCfg disabled=yes
:delay 3s
# Current ROS-B has only one active IPsec peer.
/ip ipsec active-peers kill-connections
:delay 2s
/ip ipsec peer set $peerCfg disabled=no
/ip ipsec policy set $policyCfg disabled=no
:set saBWdBad 0
:set saBWdCooldown 10
:log warning "SA-B peer-cm reset completed"
}
}
添加调度器:
/system scheduler
add name="sa-watchdog-b-scheduler" start-time=startup interval=30s on-event="/system script run sa-watchdog-b" policy=read,write,policy,test,sensitive
五、判断逻辑¶
SA-A/B-WAN1、WAN2、WAN3 全部 down:
认为外网整体故障
不清理 SA
至少一个 WAN up,GRE up,active peer established,mature SA >= 2:
隧道正常
不处理
GRE down,但 peer established 且 mature SA >= 2:
更可能是远端 GRE 问题
不清理 SA
GRE down,且 peer 不 established 或 mature SA < 2:
连续 3 次异常后重建
时间参数:
六、验证命令¶
先只检查新增 Netwatch:
手动执行 watchdog。当前隧道正常时不会重置:
A:
B:
查看日志:
查看调度器:
查看实际 SA:
正常状态应为: