From eae1fd9eb026c03ea8dbeb8f3f84558a6fbd3ad2 Mon Sep 17 00:00:00 2001 From: BFNOC <15773134+BFNOC@users.noreply.github.com> Date: Thu, 8 Oct 2026 11:01:26 +0800 Subject: [PATCH] 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 --- .../vowifi-go/engine/swu/rekey_timer.go | 29 +++++++++++- .../vowifi-go/engine/swu/rekey_timer_test.go | 44 +++++++++++++++++++ third_party/vowifi-go/engine/swu/session.go | 1 + .../internal/runtimehostcarrier/convert.go | 2 + .../runtimehostcarrier/convert_test.go | 2 +- .../internal/vowifi/policy/carrier_plan.go | 2 + .../vowifi-go/internal/vowifi/policy/merge.go | 3 ++ .../internal/vowifi/policy/override_merge.go | 3 ++ .../internal/vowifi/policy/policy_test.go | 5 ++- .../vowifi/policy/preset_original_test.go | 2 +- .../vowifi/policy/presets/spark_nz_53005.yaml | 1 + .../vowifi-go/internal/vowifi/policy/types.go | 4 ++ .../internal/vowifi/runtimecore/compat.go | 1 + .../vowifi/runtimecore/runtimecore_test.go | 4 +- .../internal/vowifi/runtimecore/swu_config.go | 1 + .../runtimehost/carrier/conversion.go | 1 + .../vowifi-go/runtimehost/carrier/types.go | 1 + 17 files changed, 99 insertions(+), 7 deletions(-) diff --git a/third_party/vowifi-go/engine/swu/rekey_timer.go b/third_party/vowifi-go/engine/swu/rekey_timer.go index a42062e2..f41fa6aa 100644 --- a/third_party/vowifi-go/engine/swu/rekey_timer.go +++ b/third_party/vowifi-go/engine/swu/rekey_timer.go @@ -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)) diff --git a/third_party/vowifi-go/engine/swu/rekey_timer_test.go b/third_party/vowifi-go/engine/swu/rekey_timer_test.go index 70b3fbad..b5cd8943 100644 --- a/third_party/vowifi-go/engine/swu/rekey_timer_test.go +++ b/third_party/vowifi-go/engine/swu/rekey_timer_test.go @@ -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) +} diff --git a/third_party/vowifi-go/engine/swu/session.go b/third_party/vowifi-go/engine/swu/session.go index 5e77bdc4..9099a510 100644 --- a/third_party/vowifi-go/engine/swu/session.go +++ b/third_party/vowifi-go/engine/swu/session.go @@ -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 diff --git a/third_party/vowifi-go/internal/runtimehostcarrier/convert.go b/third_party/vowifi-go/internal/runtimehostcarrier/convert.go index 81ea0eb0..db75b761 100644 --- a/third_party/vowifi-go/internal/runtimehostcarrier/convert.go +++ b/third_party/vowifi-go/internal/runtimehostcarrier/convert.go @@ -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) { diff --git a/third_party/vowifi-go/internal/runtimehostcarrier/convert_test.go b/third_party/vowifi-go/internal/runtimehostcarrier/convert_test.go index 41e6e1f6..ddcad5ad 100644 --- a/third_party/vowifi-go/internal/runtimehostcarrier/convert_test.go +++ b/third_party/vowifi-go/internal/runtimehostcarrier/convert_test.go @@ -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"), diff --git a/third_party/vowifi-go/internal/vowifi/policy/carrier_plan.go b/third_party/vowifi-go/internal/vowifi/policy/carrier_plan.go index fee28bda..feae7587 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/carrier_plan.go +++ b/third_party/vowifi-go/internal/vowifi/policy/carrier_plan.go @@ -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, diff --git a/third_party/vowifi-go/internal/vowifi/policy/merge.go b/third_party/vowifi-go/internal/vowifi/policy/merge.go index 2d1d901e..6af16862 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/merge.go +++ b/third_party/vowifi-go/internal/vowifi/policy/merge.go @@ -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) { diff --git a/third_party/vowifi-go/internal/vowifi/policy/override_merge.go b/third_party/vowifi-go/internal/vowifi/policy/override_merge.go index c989166c..1d3ac8da 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/override_merge.go +++ b/third_party/vowifi-go/internal/vowifi/policy/override_merge.go @@ -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 } diff --git a/third_party/vowifi-go/internal/vowifi/policy/policy_test.go b/third_party/vowifi-go/internal/vowifi/policy/policy_test.go index 80fee560..4c9596dc 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/policy_test.go +++ b/third_party/vowifi-go/internal/vowifi/policy/policy_test.go @@ -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) } diff --git a/third_party/vowifi-go/internal/vowifi/policy/preset_original_test.go b/third_party/vowifi-go/internal/vowifi/policy/preset_original_test.go index 21ec8f77..48220475 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/preset_original_test.go +++ b/third_party/vowifi-go/internal/vowifi/policy/preset_original_test.go @@ -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", diff --git a/third_party/vowifi-go/internal/vowifi/policy/presets/spark_nz_53005.yaml b/third_party/vowifi-go/internal/vowifi/policy/presets/spark_nz_53005.yaml index 75c86496..650d7e3f 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/presets/spark_nz_53005.yaml +++ b/third_party/vowifi-go/internal/vowifi/policy/presets/spark_nz_53005.yaml @@ -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: diff --git a/third_party/vowifi-go/internal/vowifi/policy/types.go b/third_party/vowifi-go/internal/vowifi/policy/types.go index 3ef9fc69..dcc64f0e 100644 --- a/third_party/vowifi-go/internal/vowifi/policy/types.go +++ b/third_party/vowifi-go/internal/vowifi/policy/types.go @@ -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 { diff --git a/third_party/vowifi-go/internal/vowifi/runtimecore/compat.go b/third_party/vowifi-go/internal/vowifi/runtimecore/compat.go index 18c9bff2..7757a5ff 100644 --- a/third_party/vowifi-go/internal/vowifi/runtimecore/compat.go +++ b/third_party/vowifi-go/internal/vowifi/runtimecore/compat.go @@ -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, } diff --git a/third_party/vowifi-go/internal/vowifi/runtimecore/runtimecore_test.go b/third_party/vowifi-go/internal/vowifi/runtimecore/runtimecore_test.go index a6299a5a..84f254b5 100644 --- a/third_party/vowifi-go/internal/vowifi/runtimecore/runtimecore_test.go +++ b/third_party/vowifi-go/internal/vowifi/runtimecore/runtimecore_test.go @@ -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) } } diff --git a/third_party/vowifi-go/internal/vowifi/runtimecore/swu_config.go b/third_party/vowifi-go/internal/vowifi/runtimecore/swu_config.go index 8b0a2bf0..c45f4882 100644 --- a/third_party/vowifi-go/internal/vowifi/runtimecore/swu_config.go +++ b/third_party/vowifi-go/internal/vowifi/runtimecore/swu_config.go @@ -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, diff --git a/third_party/vowifi-go/runtimehost/carrier/conversion.go b/third_party/vowifi-go/runtimehost/carrier/conversion.go index a010cb45..5ba42ec8 100644 --- a/third_party/vowifi-go/runtimehost/carrier/conversion.go +++ b/third_party/vowifi-go/runtimehost/carrier/conversion.go @@ -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) { diff --git a/third_party/vowifi-go/runtimehost/carrier/types.go b/third_party/vowifi-go/runtimehost/carrier/types.go index a02c3f88..349112b2 100644 --- a/third_party/vowifi-go/runtimehost/carrier/types.go +++ b/third_party/vowifi-go/runtimehost/carrier/types.go @@ -146,6 +146,7 @@ type EffectiveCarrierConfig struct { ReauthIntervalSeconds int IKERekeyIntervalSeconds int WithholdDeviceIdentity bool + KeepChildSAOnRekeyDecline bool IMSRegisterTemplate IMSRegisterTemplate IMSRegisterPolicySource string SMSRoutingMethod string