server: send granted traffic from the address the session actually used
fmr binds two IPv4 addresses. connFor picked whichever socket of the right family came first in the bind list, so a downtrain for a session established on .150 went out from .151 — and every packet was dropped by the client's NAT, which has no mapping for that pair. tcpdump on the server showed all 50 leaving; the client saw none. Read as "100% downstream loss", which is the worst kind of wrong: a confident measurement of something that never happened. Sessions now record which of our own bound addresses received their traffic, and granted sends (and delayed echo) go back out through that socket. The fallback to a family match is kept for the case where nothing has been received yet, and the test pins both paths — a single-homed lab can never reproduce this. Also: the client-side halves of the same work — anonymizer (core-privacy), local run archive with retention (core-archive), upload client, and the app's settings and history screens. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
7a94c9a3d7
commit
ce1aaa332a
@@ -0,0 +1,71 @@
|
||||
// SPDX-FileCopyrightText: 2026 Echolot contributors
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package dataplane
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A multi-homed server must answer from the address the client has been talking to. Sending from
|
||||
// a sibling address is silently dropped by the client's NAT or stateful firewall, and the client
|
||||
// then reports downstream loss that never happened — a wrong measurement, which is worse than a
|
||||
// failed one. This was a real bug: fmr binds two IPv4 addresses, the granted train went out from
|
||||
// the one the session had never used, and every packet vanished in transit.
|
||||
func TestConnForPrefersTheAddressTheSessionUsed(t *testing.T) {
|
||||
// Two loopback-bound sockets stand in for the two service addresses.
|
||||
a := mustListen(t, "127.0.0.1:0")
|
||||
b := mustListen(t, "127.0.0.1:0")
|
||||
defer a.Close()
|
||||
defer b.Close()
|
||||
|
||||
srv := &Server{}
|
||||
srv.conns = []*net.UDPConn{a, b}
|
||||
|
||||
client := netip.MustParseAddrPort("198.51.100.7:41000")
|
||||
bLocal := b.LocalAddr().(*net.UDPAddr).AddrPort()
|
||||
|
||||
if got := srv.connFor(client, bLocal); got != b {
|
||||
t.Fatalf("connFor picked the wrong socket: want the one the session used (%v)", bLocal)
|
||||
}
|
||||
|
||||
// With no recorded local address (nothing received yet) any socket of the right family is
|
||||
// the best available answer — but it must still be one, not nil.
|
||||
if got := srv.connFor(client, netip.AddrPort{}); got == nil {
|
||||
t.Fatal("connFor returned nil when a family match exists")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnForFallsBackByFamily(t *testing.T) {
|
||||
v4 := mustListen(t, "127.0.0.1:0")
|
||||
defer v4.Close()
|
||||
srv := &Server{}
|
||||
srv.conns = []*net.UDPConn{v4}
|
||||
|
||||
// A recorded local address that no longer matches any bound socket (a reload changed the
|
||||
// binds) must not strand the session: fall back rather than return nil.
|
||||
stale := netip.MustParseAddrPort("203.0.113.1:8442")
|
||||
if got := srv.connFor(netip.MustParseAddrPort("198.51.100.7:41000"), stale); got != v4 {
|
||||
t.Fatal("connFor should fall back to a family match when the recorded socket is gone")
|
||||
}
|
||||
|
||||
// No IPv6 socket is bound, so an IPv6 target has no answer — nil, not an IPv4 socket.
|
||||
if got := srv.connFor(netip.MustParseAddrPort("[2001:db8::1]:41000"), netip.AddrPort{}); got != nil {
|
||||
t.Fatal("connFor returned an IPv4 socket for an IPv6 target")
|
||||
}
|
||||
}
|
||||
|
||||
func mustListen(t *testing.T, addr string) *net.UDPConn {
|
||||
t.Helper()
|
||||
ua, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := net.ListenUDP("udp", ua)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
@@ -26,7 +26,7 @@ func (s *Server) DownTrain(sess *session.Session, g *session.Grant, count, sizeB
|
||||
if !target.IsValid() {
|
||||
return 0, fmt.Errorf("no observed data-plane source")
|
||||
}
|
||||
conn := s.connFor(target)
|
||||
conn := s.connFor(target, sess.DataLocal())
|
||||
if conn == nil {
|
||||
return 0, fmt.Errorf("no data-plane socket matches target family")
|
||||
}
|
||||
@@ -75,7 +75,7 @@ func (s *Server) BigSend(sess *session.Session, g *session.Grant, sizes []int, d
|
||||
if !target.IsValid() {
|
||||
return nil, fmt.Errorf("no observed data-plane source")
|
||||
}
|
||||
conn := s.connFor(target)
|
||||
conn := s.connFor(target, sess.DataLocal())
|
||||
if conn == nil {
|
||||
return nil, fmt.Errorf("no data-plane socket matches target family")
|
||||
}
|
||||
|
||||
@@ -73,9 +73,23 @@ func (s *Server) Serve(conn *net.UDPConn) error {
|
||||
}
|
||||
|
||||
// connFor picks a retained socket whose family matches the target.
|
||||
func (s *Server) connFor(target netip.AddrPort) *net.UDPConn {
|
||||
// connFor picks the socket to send to target from.
|
||||
//
|
||||
// When the session recorded which local address it has been talking to (local), that socket wins
|
||||
// outright. Falling back to "any socket of the right family" is only correct for a single-homed
|
||||
// server: on a multi-homed one it sends from a sibling address the client's NAT has no mapping
|
||||
// for, the packets are dropped in transit, and the client reports downstream loss that does not
|
||||
// exist. That bug is invisible in a lab with one address, which is exactly why this is explicit.
|
||||
func (s *Server) connFor(target, local netip.AddrPort) *net.UDPConn {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if local.IsValid() {
|
||||
for _, c := range s.conns {
|
||||
if c.LocalAddr().(*net.UDPAddr).AddrPort() == local {
|
||||
return c
|
||||
}
|
||||
}
|
||||
}
|
||||
want4 := target.Addr().Unmap().Is4()
|
||||
for _, c := range s.conns {
|
||||
la := c.LocalAddr().(*net.UDPAddr).AddrPort()
|
||||
@@ -94,7 +108,7 @@ func (s *Server) SendDelayedEcho(sess *session.Session, actionID string) error {
|
||||
if !target.IsValid() {
|
||||
return fmt.Errorf("session has no observed data-plane source yet")
|
||||
}
|
||||
conn := s.connFor(target)
|
||||
conn := s.connFor(target, sess.DataLocal())
|
||||
if conn == nil {
|
||||
return fmt.Errorf("no data-plane socket matches target family")
|
||||
}
|
||||
@@ -131,6 +145,9 @@ func (s *Server) handle(conn *net.UDPConn, raddr netip.AddrPort, pkt []byte, tRx
|
||||
return
|
||||
}
|
||||
sess.NoteDataSource(raddr)
|
||||
if la, ok := conn.LocalAddr().(*net.UDPAddr); ok {
|
||||
sess.NoteDataLocal(la.AddrPort())
|
||||
}
|
||||
sess.RecordUDP(session.UDPObservation{
|
||||
Seq: seq, TRxNs: tRxNs, TTxNs: time.Since(s.start).Nanoseconds(),
|
||||
Src: raddr.String(), Size: len(pkt), Type: typ,
|
||||
|
||||
Reference in New Issue
Block a user