From 393bc00da9ee97df25b910faa57be63520b8def5 Mon Sep 17 00:00:00 2001 From: yibaiba <946100224@qq.com> Date: Sat, 8 Aug 2026 14:16:11 +0800 Subject: [PATCH] [rewrite-vowifi-sms-followup-14] preserve initial IMS transport through security switch --- .../internal/vowifi/imscore/register.go | 1 + .../vowifi/imscore/register_transport.go | 12 +- .../internal/vowifi/imscore/sec_agree_test.go | 114 ++++++++++++++++++ .../internal/vowifi/imscore/service.go | 5 + .../internal/vowifi/imscore/types.go | 1 + 5 files changed, 132 insertions(+), 1 deletion(-) diff --git a/third_party/vowifi-go/internal/vowifi/imscore/register.go b/third_party/vowifi-go/internal/vowifi/imscore/register.go index 3645e5af..b46e8a02 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/register.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/register.go @@ -108,6 +108,7 @@ func (s *Service) runRegisterFlow(ctx context.Context) (time.Duration, error) { if session.security != nil && session.security.server == nil { return 0, errors.New("imscore: registration completed without 3GPP security agreement") } + s.finalizeRegistrationTransportSwitch() expires := registrationExpires(resp, s.cfg.Expires) session.expires = expires if publicID := firstSIPHeaderURI(resp.Header("P-Associated-URI")); publicID != "" { 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 9d7ddd60..6ac3dbe7 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/register_transport.go @@ -143,6 +143,9 @@ func (s *Service) activateProtectedRegistrationTCP(conn net.Conn) { s.mu.Lock() previous := s.registrationTCP s.registrationTCP = conn + if previous != nil && previous != conn { + s.registrationPreviousTCP = previous + } s.registrationTCPProtected = true s.registrationTransport = "tcp" s.mu.Unlock() @@ -151,7 +154,14 @@ func (s *Service) activateProtectedRegistrationTCP(conn net.Conn) { }) s.networkDone.Add(1) go s.readRegistrationStream(conn) - if previous != nil && previous != conn { +} + +func (s *Service) finalizeRegistrationTransportSwitch() { + s.mu.Lock() + previous := s.registrationPreviousTCP + s.registrationPreviousTCP = nil + s.mu.Unlock() + if previous != nil { _ = previous.Close() } } diff --git a/third_party/vowifi-go/internal/vowifi/imscore/sec_agree_test.go b/third_party/vowifi-go/internal/vowifi/imscore/sec_agree_test.go index 71ed1d8a..e442a2a0 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/sec_agree_test.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/sec_agree_test.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "io" "net" "strings" "testing" @@ -103,6 +104,119 @@ func TestRegisterSwitchesFromInitialUDPToProtectedTCP(t *testing.T) { } } +func TestRegisterKeepsInitialTCPUntilProtectedRegisterCompletes(t *testing.T) { + initial, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + defer initial.Close() + protected, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)}) + if err != nil { + t.Fatal(err) + } + defer protected.Close() + result := make(chan error, 1) + go serveMakeBeforeBreakRegistration(initial, protected, result) + + network := &captureIPSecNetwork{SystemIMSNetwork: NewSystemIMSNetwork(net.IPv4(127, 0, 0, 1))} + svc, err := New(&IMSConfig{ + DeviceID: "dev-sec-mbb", IMEI: "860349055895064", IMSI: "234102356143376", + IMPI: "234102356143376@ims.example", IMPU: []string{"sip:234102356143376@ims.example"}, + Domain: "ims.example", LocalIP: net.IPv4(127, 0, 0, 1), Transport: "tcp", + Registrar: initial.Addr().String(), IMSNetwork: network, + AKAProvider: stubAKAProvider{}, IPSec3GPPEnabled: true, + }) + if err != nil { + t.Fatal(err) + } + defer svc.Stop() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + if err := svc.Register(ctx); err != nil { + t.Fatalf("Register: %v", err) + } + if err := <-result; err != nil { + t.Fatal(err) + } +} + +func serveMakeBeforeBreakRegistration(initial, protected *net.TCPListener, result chan<- error) { + initialConn, err := initial.AcceptTCP() + if err != nil { + result <- err + return + } + defer initialConn.Close() + initialRequest, err := readSIPStreamMessage(bufio.NewReader(initialConn)) + if err != nil { + result <- err + return + } + client, err := parseSecurityMechanism(splitSecurityMechanisms(sipHeaderValue(initialRequest, "Security-Client"))[0]) + if err != nil { + result <- err + return + } + serverHeader := fmt.Sprintf("ipsec-3gpp;q=0.98;alg=hmac-sha-1-96;mod=trans;ealg=aes-cbc;spi-c=858993459;spi-s=1145324612;port-c=6059;port-s=%d", tcpPort(protected.Addr())) + challenge := strings.TrimPrefix(strings.TrimSpace(digestChallengeHeaderNoQOP()), "WWW-Authenticate: ") + headers := "WWW-Authenticate: " + challenge + "\r\nSecurity-Server: " + serverHeader + "\r\n" + if _, err = initialConn.Write([]byte(registerWireResponse(initialRequest, 401, headers))); err != nil { + result <- err + return + } + protectedConn, err := protected.AcceptTCP() + if err != nil { + result <- err + return + } + defer protectedConn.Close() + authenticated, err := readSIPStreamMessage(bufio.NewReader(protectedConn)) + if err != nil { + result <- err + return + } + if err := assertTCPConnectionOpen(initialConn); err != nil { + result <- err + return + } + if tcpPort(protectedConn.RemoteAddr()) != int(client.PortC) { + result <- fmt.Errorf("protected source port = %d, want %d", tcpPort(protectedConn.RemoteAddr()), client.PortC) + return + } + if _, err = protectedConn.Write([]byte(registerWireResponse(authenticated, 200, ""))); err != nil { + result <- err + return + } + result <- waitForTCPConnectionClose(initialConn) +} + +func assertTCPConnectionOpen(conn *net.TCPConn) error { + if err := conn.SetReadDeadline(time.Now().Add(50 * time.Millisecond)); err != nil { + return err + } + var probe [1]byte + _, err := conn.Read(probe[:]) + if netErr, ok := err.(net.Error); ok && netErr.Timeout() { + return conn.SetReadDeadline(time.Time{}) + } + if err == nil { + return errors.New("initial REGISTER connection received unexpected data") + } + return fmt.Errorf("initial REGISTER connection closed before protected response: %w", err) +} + +func waitForTCPConnectionClose(conn *net.TCPConn) error { + if err := conn.SetReadDeadline(time.Now().Add(time.Second)); err != nil { + return err + } + var probe [1]byte + _, err := conn.Read(probe[:]) + if errors.Is(err, io.EOF) { + return nil + } + return fmt.Errorf("initial REGISTER connection remained open: %w", err) +} + func serveUDPChallengeThenTCPSuccess(udpServer *net.UDPConn, tcpServer *net.TCPListener, result chan<- error) { buffer := make([]byte, 64*1024) n, remote, err := udpServer.ReadFromUDP(buffer) diff --git a/third_party/vowifi-go/internal/vowifi/imscore/service.go b/third_party/vowifi-go/internal/vowifi/imscore/service.go index 6b6f3127..32499ec3 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/service.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/service.go @@ -291,10 +291,12 @@ func (s *Service) Stop() { } registrationIO := s.registrationIO registrationTCP := s.registrationTCP + registrationPreviousTCP := s.registrationPreviousTCP securityServerIO := s.securityServerIO clientPortReserve := s.clientPortReserve s.registrationIO = nil s.registrationTCP = nil + s.registrationPreviousTCP = nil s.registrationTCPProtected = false s.registrationTransport = "" s.securityServerIO = nil @@ -309,6 +311,9 @@ func (s *Service) Stop() { if registrationTCP != nil { _ = registrationTCP.Close() } + if registrationPreviousTCP != nil && registrationPreviousTCP != registrationTCP { + _ = registrationPreviousTCP.Close() + } if securityServerIO != nil { _ = securityServerIO.Close() } diff --git a/third_party/vowifi-go/internal/vowifi/imscore/types.go b/third_party/vowifi-go/internal/vowifi/imscore/types.go index 22c3eac4..998508ed 100644 --- a/third_party/vowifi-go/internal/vowifi/imscore/types.go +++ b/third_party/vowifi-go/internal/vowifi/imscore/types.go @@ -194,6 +194,7 @@ type Service struct { transport *sipTransport registrationIO net.PacketConn registrationTCP net.Conn + registrationPreviousTCP net.Conn registrationTCPProtected bool registrationTransport string securityServerIO net.Listener