fix(policy): keep the Spark NZ CHILD_SA when its rekey is declined

- Add the keep_child_sa_on_rekey_decline preset key
- Carry it through the runtime carrier config to the SWu config
- Set it for Spark NZ
- Keep the CHILD_SA on NO_ADDITIONAL_SAS and ask again next interval where the key is set
This commit is contained in:
BFNOC committed 2026-10-08 13:05:18 +08:00
1 parent aa20775ab8
commit eae1fd9eb0
17 files changed
+99 -7

No files matched your search

+28 -1
View File
@@ -7,6 +7,7 @@ import (
"time"
"github.com/iniwex5/vowifi-go/engine/ikev2"
"go.uber.org/zap"
)
const (
@@ -25,6 +26,8 @@ type rekeyTimerSpec struct {
target **time.Timer
action func() error
immediateFail func(error) bool
// declined keeps the current SA in service and asks again next interval.
declined func(error) bool
retryInterval time.Duration
}
@@ -74,10 +77,26 @@ func (s *Session) startChildSARekeyTimer(interval time.Duration) {
s.startRekeyTimer(rekeyTimerSpec{
name: "CHILD_SA", interval: interval,
reset: reset, target: &s.childRekeyTimer, action: s.RekeyChildSA,
immediateFail: isChildSANotFoundError,
immediateFail: isChildSANotFoundError, declined: s.childRekeyDeclined(),
})
}
// childRekeyDeclined treats NO_ADDITIONAL_SAS as a decline only where the
// carrier preset opts in; elsewhere it stays a rekey failure.
func (s *Session) childRekeyDeclined() func(error) bool {
if s.cfg != nil && s.cfg.KeepChildSAOnRekeyDecline {
return isNoAdditionalSAsError
}
return nil
}
// isNoAdditionalSAsError reports a peer that refuses a UE-initiated CHILD_SA
// rekey. RFC 7296 2.8 keeps the old SA valid after a failed rekey.
func isNoAdditionalSAsError(err error) bool {
var rejection *createChildSARejectError
return errors.As(err, &rejection) && rejection.NotifyType == ikev2.NO_ADDITIONAL_SAS
}
func isChildSANotFoundError(err error) bool {
var rejection *createChildSARejectError
return errors.As(err, &rejection) && rejection.NotifyType == ikev2.CHILD_SA_NOT_FOUND
@@ -121,6 +140,14 @@ func (s *Session) runRekeyTimer(timer *time.Timer, spec rekeyTimerSpec) {
}
continue
}
if spec.declined != nil && spec.declined(err) {
s.Logger.Warn("peer declined "+spec.name+" rekey; keeping current SA", zap.Error(err))
failures = 0
if !s.resetRekeyTimer(timer, rekeyDelay(spec.interval)) {
return
}
continue
}
failures++
if failures >= rekeyMaxFailures || spec.immediateFail != nil && spec.immediateFail(err) {
s.failEstablishedControl(fmt.Errorf("swu: %s rekey failed: %w", spec.name, err))
+44
View File
@@ -2,6 +2,7 @@ package swu
import (
"errors"
"sync/atomic"
"testing"
"time"
@@ -54,3 +55,46 @@ func waitForRekeyTimerFailure(t *testing.T, session *Session) {
}
session.rekeyTimerWG.Wait()
}
func TestNoAdditionalSAsKeepsSessionAndAsksAgainNextInterval(t *testing.T) {
session := NewSession(&Config{KeepChildSAOnRekeyDecline: true})
var attempts atomic.Int32
session.startRekeyTimer(rekeyTimerSpec{
name: "CHILD_SA", interval: time.Millisecond, target: &session.childRekeyTimer,
retryInterval: time.Hour, declined: session.childRekeyDeclined(),
action: func() error {
attempts.Add(1)
return &createChildSARejectError{NotifyType: ikev2.NO_ADDITIONAL_SAS}
},
})
deadline := time.After(time.Second)
for attempts.Load() < rekeyMaxFailures+1 {
select {
case <-session.done:
t.Fatalf("declined rekey failed the session: %v", session.TerminalError())
case <-deadline:
t.Fatalf("attempts = %d, want the timer re-armed after each decline", attempts.Load())
case <-time.After(time.Millisecond):
}
}
session.cancel()
session.rekeyTimerWG.Wait()
if err := session.TerminalError(); err != nil {
t.Fatalf("terminal error = %v, want session kept", err)
}
}
func TestNoAdditionalSAsStillFailsWithoutPresetOptIn(t *testing.T) {
session := NewSession(&Config{})
if session.childRekeyDeclined() != nil {
t.Fatal("NO_ADDITIONAL_SAS is a decline without the preset opt-in")
}
session.startRekeyTimer(rekeyTimerSpec{
name: "CHILD_SA", interval: time.Millisecond, target: &session.childRekeyTimer,
retryInterval: time.Millisecond, declined: session.childRekeyDeclined(),
action: func() error {
return &createChildSARejectError{NotifyType: ikev2.NO_ADDITIONAL_SAS}
},
})
waitForRekeyTimerFailure(t, session)
}
+1
View File
@@ -103,6 +103,7 @@ type Config struct {
EnableDeviceIdentitySpoof bool
DeviceIdentityIMEI string
WithholdDeviceIdentity bool // keep DEVICE_IDENTITY out of the first IKE_AUTH
KeepChildSAOnRekeyDecline bool // keep the CHILD_SA when its rekey gets NO_ADDITIONAL_SAS
IKEIdentityMode string
AKAChallengeMode string
AKAIdentityMode string
@@ -105,6 +105,7 @@ func toInternalIKE(result *policy.EffectiveCarrierConfig, cfg carrier.EffectiveC
result.ReauthIntervalSeconds = cfg.ReauthIntervalSeconds
result.IKERekeyIntervalSeconds = cfg.IKERekeyIntervalSeconds
result.WithholdDeviceIdentity = cfg.WithholdDeviceIdentity
result.KeepChildSAOnRekeyDecline = cfg.KeepChildSAOnRekeyDecline
}
func toInternalIMS(result *policy.EffectiveCarrierConfig, cfg carrier.EffectiveCarrierConfig) {
@@ -158,6 +159,7 @@ func fromInternalIKE(result *carrier.EffectiveCarrierConfig, cfg policy.Effectiv
result.ReauthIntervalSeconds = cfg.ReauthIntervalSeconds
result.IKERekeyIntervalSeconds = cfg.IKERekeyIntervalSeconds
result.WithholdDeviceIdentity = cfg.WithholdDeviceIdentity
result.KeepChildSAOnRekeyDecline = cfg.KeepChildSAOnRekeyDecline
}
func fromInternalIMS(result *carrier.EffectiveCarrierConfig, cfg policy.EffectiveCarrierConfig) {
@@ -93,7 +93,7 @@ func populatedCarrierConfig() carrier.EffectiveCarrierConfig {
IMSIdentitySource: "isim", IMSLocalPort: 5060, IMSTCPKeepaliveSeconds: 40,
IMSOptionsPingIntervalSeconds: 50, DPDKeepaliveIntervalSeconds: 60,
ReauthIntervalSeconds: 70, IKERekeyIntervalSeconds: 80,
WithholdDeviceIdentity: true,
WithholdDeviceIdentity: true, KeepChildSAOnRekeyDecline: true,
IMSRegisterTemplate: populatedTemplate("flattened"),
IMSRegisterPolicySource: "preset", SMSRoutingMethod: "sip", SMSRoutingGW: "gw.example",
ForceSMSCAuth: true, IMS: populatedTemplate("compatibility"),
@@ -42,6 +42,7 @@ func CarrierPlanFromEffectiveConfig(config EffectiveCarrierConfig) CarrierPlan {
DPDKeepaliveIntervalSeconds: config.DPDKeepaliveIntervalSeconds,
ReauthIntervalSeconds: config.ReauthIntervalSeconds,
IKERekeyIntervalSeconds: config.IKERekeyIntervalSeconds,
KeepChildSAOnRekeyDecline: config.KeepChildSAOnRekeyDecline,
AKAPrimePreferred: config.AKAPrimePreferred,
},
IMS: IMSPlan{
@@ -87,6 +88,7 @@ func EffectiveCarrierConfigFromCarrierPlan(plan CarrierPlan) EffectiveCarrierCon
DPDKeepaliveIntervalSeconds: plan.IKE.DPDKeepaliveIntervalSeconds,
ReauthIntervalSeconds: plan.IKE.ReauthIntervalSeconds,
IKERekeyIntervalSeconds: plan.IKE.IKERekeyIntervalSeconds,
KeepChildSAOnRekeyDecline: plan.IKE.KeepChildSAOnRekeyDecline,
AKAPrimePreferred: plan.IKE.AKAPrimePreferred,
IMSDomain: plan.IMS.Domain, IMSRealm: plan.IMS.Realm, IMSRegistrar: plan.IMS.Registrar,
IMSPCSCF: plan.IMS.PCSCF, IMSUserAgent: plan.IMS.UserAgent, IMSTransport: plan.IMS.Transport,
+3
View File
@@ -117,6 +117,9 @@ func mergePresetIKE(config *EffectiveCarrierConfig, preset CarrierPreset) {
if preset.IKERekeyIntervalSeconds > 0 {
config.IKERekeyIntervalSeconds = preset.IKERekeyIntervalSeconds
}
if preset.KeepChildSAOnRekeyDecline {
config.KeepChildSAOnRekeyDecline = true
}
}
func mergePresetIMS(config *EffectiveCarrierConfig, preset CarrierPreset) {
@@ -79,6 +79,9 @@ func applyCarrierOverride(preset CarrierPreset, override CarrierOverride) Carrie
if override.IKERekeyIntervalSeconds > 0 {
preset.IKERekeyIntervalSeconds = override.IKERekeyIntervalSeconds
}
if override.KeepChildSAOnRekeyDecline {
preset.KeepChildSAOnRekeyDecline = true
}
if override.WithholdDeviceIdentity {
preset.WithholdDeviceIdentity = true
}
@@ -142,14 +142,15 @@ func TestResolveEmbeddedCarrierPresets(t *testing.T) {
t.Fatalf("vodafone uk extra PDN = %+v", extra)
}
if spark := ResolveEffectiveCarrierConfig("530", "05"); spark.PresetID != "spark_nz_53005" ||
!spark.WithholdDeviceIdentity || !CarrierPlanFromEffectiveConfig(spark).Device.Withhold {
!spark.WithholdDeviceIdentity || !CarrierPlanFromEffectiveConfig(spark).Device.Withhold ||
!spark.KeepChildSAOnRekeyDecline || !CarrierPlanFromEffectiveConfig(spark).IKE.KeepChildSAOnRekeyDecline {
t.Fatalf("spark nz = %+v", spark)
}
lebaraUK := ResolveEffectiveCarrierConfig("234", "87")
if lebaraUK.PresetID != "lebara_uk_23487" || lebaraUK.DeviceModel != "rmx3366" ||
lebaraUK.EPDGAddr != "epdg.epc.mnc087.mcc234.pub.3gppnetwork.org" ||
lebaraUK.EPDGAddrSource != "standard" || lebaraUK.IKERekeyIntervalSeconds != 9000 ||
lebaraUK.WithholdDeviceIdentity ||
lebaraUK.WithholdDeviceIdentity || lebaraUK.KeepChildSAOnRekeyDecline ||
lebaraUK.IMSRegisterTemplate.ID != "lebara_uk_23487" {
t.Fatalf("lebara uk = %+v", lebaraUK)
}
@@ -40,7 +40,7 @@ func TestOriginalCarrierPresetAssetsRemainExact(t *testing.T) {
"orange_fr_20801.yaml": "68d6d1cc830316d17cd607d65cf176e0f92b27393bd899735136a0fabc507328",
"smart_ph_51503.yaml": "fca9263e07be5707fa1365879179462757e554261512dbcc58cfd0fac390086e",
"softbank_44020.yaml": "6398fe7ccbae656d6a85589ba1cf903b4d07e4c22978d7fff92aad11ccfb1dd7",
"spark_nz_53005.yaml": "a8b250ce8efc3f3afac0b4a4a6c913e4b151e9c8e550024603c69c4fec9c7c40",
"spark_nz_53005.yaml": "866b6142a772bb8f31a98143732bc3bcb618e985b7b9f868b9ef2d0401e5c85e",
"sunrise_22802.yaml": "2160911e6d4fca664fbc9c474eee71a468d282703760f3c87e99df9043872e7b",
"telekom_de_26201.yaml": "aac0d40b575b4ab5625ac437b3ea7cd228633f87b1e2b5f3ddd12f7024bd4455",
"three_hk_454003.yaml": "a3a84a436646cddd80df30aa2493f4b4076f08664a478d0b954c4142ef196d61",
@@ -5,6 +5,7 @@ custom_epdg: epdg.epc.mnc005.mcc530.pub.3gppnetwork.spark.co.nz
device_model: iphone15,4
dpd_interval_seconds: 600
withhold_device_identity: true
keep_child_sa_on_rekey_decline: true
ike_proposals:
- aes256-sha256-prfsha256-modp2048
esp_proposals:
+4
View File
@@ -146,6 +146,7 @@ type CarrierOverride struct {
DPDKeepaliveIntervalSeconds int `yaml:"dpd_keepalive_interval_seconds"`
ReauthIntervalSeconds int `yaml:"reauth_interval_seconds"`
IKERekeyIntervalSeconds int `yaml:"ike_rekey_interval_seconds"`
KeepChildSAOnRekeyDecline bool `yaml:"keep_child_sa_on_rekey_decline"`
IMSRegisterTemplate IMSRegisterTemplateOverride `yaml:"ims_register_template"`
SMSRoutingMethod string `yaml:"sms_routing_method"`
SMSRoutingGW string `yaml:"sms_routing_gw"`
@@ -188,6 +189,7 @@ type CarrierPreset struct {
DPDKeepaliveIntervalSeconds int
ReauthIntervalSeconds int
IKERekeyIntervalSeconds int
KeepChildSAOnRekeyDecline bool
IMSRegisterTemplate IMSRegisterTemplate
IPStackType string `json:"ip_stack,omitempty" yaml:"ip_stack,omitempty"`
SMSRoutingMethod, SMSRoutingGW string
@@ -221,6 +223,7 @@ type EffectiveCarrierConfig struct {
IMSOptionsPingIntervalSeconds int
DPDKeepaliveIntervalSeconds, ReauthIntervalSeconds int
IKERekeyIntervalSeconds int
KeepChildSAOnRekeyDecline bool
IMSRegisterTemplate IMSRegisterTemplate
IMSRegisterPolicySource string
SMSRoutingMethod, SMSRoutingGW string
@@ -265,6 +268,7 @@ type IKEPlan struct {
AlgorithmPolicy string
DPDKeepaliveIntervalSeconds, ReauthIntervalSeconds int
IKERekeyIntervalSeconds int
KeepChildSAOnRekeyDecline bool
AKAPrimePreferred bool
}
type IMSPlan struct {
@@ -265,6 +265,7 @@ func carrierPlanFromCompatibility(value carrier.EffectiveCarrierConfig) policy.C
ReauthIntervalSeconds: value.ReauthIntervalSeconds,
IKERekeyIntervalSeconds: value.IKERekeyIntervalSeconds,
WithholdDeviceIdentity: value.WithholdDeviceIdentity,
KeepChildSAOnRekeyDecline: value.KeepChildSAOnRekeyDecline,
SMSRoutingMethod: value.SMSRoutingMethod, SMSRoutingGW: value.SMSRoutingGW,
ForceSMSCAuth: value.ForceSMSCAuth,
}
@@ -991,10 +991,10 @@ func TestCarrierPresetKeysReachSWUConfig(t *testing.T) {
plan := carrierPlanFromCompatibility(carrier.ResolveEffectiveCarrierConfig(mcc, mnc))
return BuildSWUConfig(SessionConfig{Prepared: profile.PreparedSession{CarrierPlan: plan}})
}
if spark := build("530", "05"); !spark.WithholdDeviceIdentity {
if spark := build("530", "05"); !spark.WithholdDeviceIdentity || !spark.KeepChildSAOnRekeyDecline {
t.Fatalf("Spark NZ SWu config = %+v", spark)
}
if lebara := build("234", "87"); lebara.WithholdDeviceIdentity {
if lebara := build("234", "87"); lebara.WithholdDeviceIdentity || lebara.KeepChildSAOnRekeyDecline {
t.Fatalf("Lebara UK SWu config = %+v", lebara)
}
}
@@ -39,6 +39,7 @@ func BuildSWUConfig(cfg SessionConfig) *swu.Config {
EnableDeviceIdentitySpoof: plan.Device.IdentityEnabled,
DeviceIdentityIMEI: firstNonEmpty(plan.Device.IdentityIMEI, prepared.Profile.IMEI),
WithholdDeviceIdentity: plan.Device.Withhold,
KeepChildSAOnRekeyDecline: plan.IKE.KeepChildSAOnRekeyDecline,
IKEIdentityMode: plan.IKE.IKEIdentityMode,
AKAChallengeMode: plan.IKE.AKAChallengeMode,
AKAIdentityMode: plan.IKE.AKAIdentityMode,
@@ -74,6 +74,7 @@ func copyCarrierIKEFromInternal(result *EffectiveCarrierConfig, value policy.Eff
result.ReauthIntervalSeconds = value.ReauthIntervalSeconds
result.IKERekeyIntervalSeconds = value.IKERekeyIntervalSeconds
result.WithholdDeviceIdentity = value.WithholdDeviceIdentity
result.KeepChildSAOnRekeyDecline = value.KeepChildSAOnRekeyDecline
}
func copyCarrierIMSFromInternal(result *EffectiveCarrierConfig, value policy.EffectiveCarrierConfig) {
+1
View File
@@ -146,6 +146,7 @@ type EffectiveCarrierConfig struct {
ReauthIntervalSeconds int
IKERekeyIntervalSeconds int
WithholdDeviceIdentity bool
KeepChildSAOnRekeyDecline bool
IMSRegisterTemplate IMSRegisterTemplate
IMSRegisterPolicySource string
SMSRoutingMethod string