diff --git a/third_party/vowifi-go/internal/vowifi/imscore/keepalive_test.go b/third_party/vowifi-go/internal/vowifi/imscore/keepalive_test.go index 598baa3e..6f8342a5 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/keepalive_test.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/keepalive_test.go @@ -16,8 +16,8 @@ func TestProtectedSIPStreamClosureInvalidatesRegistration(t *testing.T) { client, server := net.Pipe() defer server.Close() service.activateProtectedRegistrationTCP(client) - if readiness := service.SMSReadiness(); !readiness.Ready { - t.Fatalf("initial readiness = %+v", readiness) + if readiness := service.SMSReadiness(); !readiness.TransportReady { + t.Fatalf("initial transport readiness = %+v", readiness) } if err := server.Close(); err != nil { t.Fatal(err) diff --git a/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go b/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go index f1f8652e..3052c51e 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go @@ -367,8 +367,12 @@ func (s *Service) trackProtectedConnection(conn net.Conn) bool { return false default: s.protectedConns[conn] = struct{}{} + changed := !s.portSPushReady.Swap(true) s.protectedConnMu.Unlock() s.cancelPortSReconnectWatch() + if changed { + s.notifySMSReadiness() + } return true } } @@ -376,7 +380,12 @@ func (s *Service) trackProtectedConnection(conn net.Conn) bool { func (s *Service) untrackProtectedConnection(conn net.Conn) { s.protectedConnMu.Lock() delete(s.protectedConns, conn) + empty := len(s.protectedConns) == 0 + changed := empty && s.portSPushReady.Swap(false) s.protectedConnMu.Unlock() + if changed { + s.notifySMSReadiness() + } } func (s *Service) readRegistrationStream(conn net.Conn) { diff --git a/third_party/vowifi-go/internal/vowifi/imscore/service_notify.go b/third_party/vowifi-go/internal/vowifi/imscore/service_notify.go index 4edbebc1..18f55db6 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/service_notify.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/service_notify.go @@ -16,7 +16,9 @@ func (s *Service) maybeNotifySMSReady(reason string) { return } s.mu.Lock() + pushReady := !s.protectedSMSPushRequiredLocked() || s.portSPushReady.Load() ready := !s.smsReadyNotified && s.smsReceiverReady && + pushReady && strings.TrimSpace(s.cfg.SMSC) != "" && s.regStatus.Load() == registrationRegistered if !ready { diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_24_341_test.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_24_341_test.go index 1fe049b2..931cb77a 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_24_341_test.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_24_341_test.go @@ -144,6 +144,28 @@ func TestNotifySMSMemoryAvailableRetriesRejectedFinalResponses(t *testing.T) { } } +func TestRejectedRPReportDoesNotSendMemoryAvailable(t *testing.T) { + service, _, _ := newInboundSMSTestService(t) + var kinds []smscodec.RPDUKind + service.transport.SetSendFn(func(request string) error { + body, err := rawSIPBody(request) + if err != nil { + t.Fatal(err) + } + kinds = append(kinds, smscodec.ClassifyRPDU(body).Kind) + service.transport.DeliverResponse(registerResponseForRequest(request, 488, nil)) + return nil + }) + raw := inboundSMSRequest(t, imsSMSContentType, + inboundRPData(t, 0x41, "+447700900123", "rejected report")) + service.sendRPReportWithRetry(rpReportRequest{ + Inbound: raw, Body: smscodec.BuildRPAck(0x41), RPMR: 0x41, + }) + if len(kinds) != 1 || kinds[0] != smscodec.RPDUKindAck { + t.Fatalf("outbound RPDU kinds = %v, want only RP-ACK", kinds) + } +} + func TestNotifySMSMemoryAvailableAbortOnStop(t *testing.T) { service, _, _ := newInboundSMSTestService(t) service.rememberSMSMemoryDenied( diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go index 2477dbeb..b9d0e22b 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_ack.go @@ -44,9 +44,6 @@ const ( rpReportInitialDelay = 0 rpReportRetryDelay = time.Second rpReportMaxAttempts = 4 - // One prompt covers every message the SMSC releases in the same flush, - // so rate limit it rather than sending one per rejected report. - smmaPromptMinInterval = time.Minute ) type mtAckAudit struct { @@ -154,45 +151,9 @@ func (s *Service) sendRPReportWithRetry(report rpReportRequest) { "IMS RP report delivery failed", "device", deviceID, "attempts", attempts, "rp_mr", int(report.RPMR), "error", err) - s.promptSMSCRedeliveryAfterRejectedReport(err) } } -// A report the gateway rejects leaves the SMSC believing the short message is -// still outstanding. Measured on 2026-09-03: one message was redelivered three -// times over 24 minutes on a rejection every time, on two different P-CSCF -// instances and with a report byte-identical in structure to ones the same pool -// accepts, and nothing else was delivered while it stayed at the head. RP-SMMA -// is the only receiver-initiated way to ask for the queue (TS 24.011 clears -// MCEF and alerts the service centre), so send one instead of waiting out a -// retransmission timer that had already backed off past 18 minutes. -func (s *Service) promptSMSCRedeliveryAfterRejectedReport(reportErr error) { - if rpReportRejectStatus(reportErr) == 0 { - return - } - if s == nil || s.stopped() || !s.reserveSMMAPrompt(time.Now()) { - return - } - s.smsSendMu.Lock() - err := s.sendRPSMMAWithRetryPolicy(rpReportInitialDelay, rpReportRetryDelay) - s.smsSendMu.Unlock() - if err != nil { - logging.WarnRate("smsip-smma-prompt-"+s.DeviceID(), 30*time.Second, - "IMS RP-SMMA redelivery prompt failed", - "device", s.DeviceID(), "error", err) - return - } - logging.Info("IMS RP-SMMA sent to release the SMSC queue", "device", s.DeviceID()) -} - -func (s *Service) reserveSMMAPrompt(now time.Time) bool { - last := s.smmaPromptLastAt.Load() - if last != 0 && now.Sub(time.Unix(0, last)) < smmaPromptMinInterval { - return false - } - return s.smmaPromptLastAt.CompareAndSwap(last, now.UnixNano()) -} - func (s *Service) sendRPReportWithRetryPolicy( report rpReportRequest, initialDelay time.Duration, diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness.go index 2f82252a..b539306c 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness.go @@ -23,6 +23,9 @@ func (s *Service) SMSReadiness() SMSReadiness { strings.TrimSpace(s.regSession.contactUser) != "" transportReady := registered && s.registeredSIPTransportReadyLocked() receiverReady := s.smsReceiverReady + if s.protectedSMSPushRequiredLocked() { + receiverReady = receiverReady && s.portSPushReady.Load() + } smsc := "" if s.cfg != nil { smsc = s.cfg.SMSC @@ -31,6 +34,13 @@ func (s *Service) SMSReadiness() SMSReadiness { return evaluateSMSReadiness(registered, profileReady, transportReady, receiverReady, smsc) } +func (s *Service) protectedSMSPushRequiredLocked() bool { + return !s.externalTransport && !s.portSRecoveryRejected.Load() && + s.regSession != nil && + s.regSession.security != nil && + strings.TrimSpace(s.regSession.security.verifyHeader) != "" +} + // SetOnSMSReadinessChanged installs the readiness observer and immediately // publishes a snapshot so callers cannot miss startup transitions. func (s *Service) SetOnSMSReadinessChanged(fn func(SMSReadiness)) { diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness_test.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness_test.go index caf0efc1..96a426ff 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness_test.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_readiness_test.go @@ -2,11 +2,71 @@ package imscore import ( "errors" + "net" "testing" "github.com/iniwex5/vowifi-go/internal/vowifi/smsdelivery" ) +func TestProtectedSMSReadinessRequiresPortSFlow(t *testing.T) { + service := newProtectedKeepaliveTestService(t) + registration, registrationPeer := net.Pipe() + push, pushPeer := net.Pipe() + t.Cleanup(func() { + _ = registrationPeer.Close() + _ = pushPeer.Close() + }) + service.mu.Lock() + service.registrationTCP = registration + service.registrationTCPProtected = true + service.mu.Unlock() + + if got := service.SMSReadiness(); got.ReceiverReady || got.Ready { + t.Fatalf("readiness without port-s = %+v", got) + } + if !service.trackProtectedConnection(push) { + t.Fatal("track port-s") + } + if got := service.SMSReadiness(); !got.ReceiverReady || !got.Ready { + t.Fatalf("readiness with port-s = %+v", got) + } + service.untrackProtectedConnection(push) + if got := service.SMSReadiness(); got.ReceiverReady || got.Ready { + t.Fatalf("readiness after port-s closed = %+v", got) + } + service.portSRecoveryRejected.Store(true) + if got := service.SMSReadiness(); !got.ReceiverReady || !got.Ready { + t.Fatalf("on-demand port-s listener readiness = %+v", got) + } +} + +func TestProtectedSMSReadyCallbackWaitsForPortSFlow(t *testing.T) { + service := newProtectedKeepaliveTestService(t) + service.regStatus.Store(registrationRegistered) + registration, registrationPeer := net.Pipe() + push, pushPeer := net.Pipe() + t.Cleanup(func() { + _ = registrationPeer.Close() + _ = pushPeer.Close() + }) + service.mu.Lock() + service.registrationTCP = registration + service.registrationTCPProtected = true + service.mu.Unlock() + + called := 0 + service.SetOnSMSReady(func() { called++ }) + if called != 0 { + t.Fatalf("callback before port-s = %d", called) + } + if !service.trackProtectedConnection(push) { + t.Fatal("track port-s") + } + if called != 1 { + t.Fatalf("callback after port-s = %d, want 1", called) + } +} + func TestEvaluateSMSReadinessRequiresEveryPrerequisite(t *testing.T) { tests := []struct { name string diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go b/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go index baf89dc0..d811fd39 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sms_runtime.go @@ -110,7 +110,6 @@ type messagingRuntime struct { smmaSent map[string]time.Time moSelfHealLastAt atomic.Int64 - smmaPromptLastAt atomic.Int64 lastMTAckMu sync.Mutex } diff --git a/third_party/vowifi-go/internal/vowifi/imscore/types.go b/third_party/vowifi-go/internal/vowifi/imscore/types.go index fe723742..db9b7ecc 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/types.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/types.go @@ -184,6 +184,7 @@ type Service struct { protectedConns map[net.Conn]struct{} portSReconnectGrace time.Duration portSLastReadAt atomic.Int64 + portSPushReady atomic.Bool portSRecoveryPending atomic.Bool portSRecoveryRejected atomic.Bool portSWatchMu sync.Mutex diff --git a/third_party/vowifi-go/internal/vowifi/netstack/gvisor_network_test.go b/third_party/vowifi-go/internal/vowifi/netstack/gvisor_network_test.go index c846519a..526189db 100644 --- a/third_party/vowifi-go/internal/vowifi/netstack/gvisor_network_test.go +++ b/third_party/vowifi-go/internal/vowifi/netstack/gvisor_network_test.go @@ -236,6 +236,14 @@ func TestConfigureIMSTCPKeepalive(t *testing.T) { } } +func TestIMSKeepaliveDeadFlowDetectionFitsSMSRecoveryWindow(t *testing.T) { + detectionWindow := imsTCPKeepaliveIdle + + imsTCPKeepaliveInterval*time.Duration(imsTCPKeepaliveProbes) + if detectionWindow > time.Minute { + t.Fatalf("dead-flow detection window = %s, want at most 1m", detectionWindow) + } +} + func assertIPv4UDPPacket(t *testing.T, packet []byte, destination net.IP, payload []byte) { t.Helper() if len(packet) < 28 || packet[0]>>4 != 4 { diff --git a/third_party/vowifi-go/internal/vowifi/netstack/gvisor_tcp.go b/third_party/vowifi-go/internal/vowifi/netstack/gvisor_tcp.go index ddb9dbc4..8fa47bbe 100644 --- a/third_party/vowifi-go/internal/vowifi/netstack/gvisor_tcp.go +++ b/third_party/vowifi-go/internal/vowifi/netstack/gvisor_tcp.go @@ -22,9 +22,10 @@ const ( imsIPv4LinkMTU = imsTCPMSS + header.IPv4MinimumSize + header.TCPMinimumSize imsIPv6LinkMTU = header.IPv6MinimumMTU tcpListenBacklog = 4096 - // Port-s has no SIP CRLF; keep both IMS TCP 5-tuples at 30s idle. + // Port-s has no SIP CRLF. Probe after 30s of silence, then retry quickly + // enough to expose a dead reverse flow before the SMSC attempts delivery. imsTCPKeepaliveIdle = 30 * time.Second - imsTCPKeepaliveInterval = 30 * time.Second + imsTCPKeepaliveInterval = 5 * time.Second imsTCPKeepaliveProbes = 3 )