From 0da6abd72a4acff77fd4fa224db447c0c7599e1e Mon Sep 17 00:00:00 2001 From: Nemu-x Date: Thu, 29 Oct 2026 20:37:31 +0300 Subject: [PATCH 1/3] feat: derive the fake TTL from the server's hop limit FakeOptions.AutoTTL (delta/min/max, zapret's --dpi-desync-autottl) sets the fake's TTL from the hop count estimated from the server's SYN-ACK, with Fake.TTL as an optional fallback. Linux/Android IPv6 records the hop limit with IPV6_RECVHOPLIMIT (enabled before connect by ControlAutoTTL) and returns it through IPV6_2292PKTOPTIONS. Linux IPv4, Windows and macOS keep no received TTL for TCP sockets and are reported in PlatformCapabilities().AutoTTL and Unsupported with the reason. --- autottl.go | 245 ++++++++++++++++++++++++++++++++++ autottl_darwin.go | 8 ++ autottl_linux.go | 120 +++++++++++++++++ autottl_linux_test.go | 261 +++++++++++++++++++++++++++++++++++++ autottl_other.go | 20 +++ autottl_reason_other.go | 5 + autottl_test.go | 221 +++++++++++++++++++++++++++++++ autottl_windows.go | 8 ++ capability.go | 12 +- conn.go | 7 + fake.go | 11 +- fake_linux.go | 10 +- fake_windows.go | 6 +- key.go | 2 + sockopt_linux.go | 19 +++ sockopt_linux_386.go | 32 +++++ third_party/NOTICE | 10 ++ third_party/ZAPRET-LICENSE | 21 +++ 18 files changed, 1011 insertions(+), 7 deletions(-) create mode 100644 autottl.go create mode 100644 autottl_darwin.go create mode 100644 autottl_linux.go create mode 100644 autottl_linux_test.go create mode 100644 autottl_other.go create mode 100644 autottl_reason_other.go create mode 100644 autottl_test.go create mode 100644 autottl_windows.go create mode 100644 sockopt_linux.go create mode 100644 sockopt_linux_386.go create mode 100644 third_party/ZAPRET-LICENSE diff --git a/autottl.go b/autottl.go new file mode 100644 index 0000000..6817047 --- /dev/null +++ b/autottl.go @@ -0,0 +1,245 @@ +package desync + +import ( + "errors" + "fmt" + "net" + "strconv" + "strings" + "syscall" +) + +var ( + // ErrAutoTTLUnsupported reports that this platform, or the connection's + // address family, gives an unprivileged TCP socket no way to read the TTL + // or hop limit of the packets it received. PlatformCapabilities().AutoTTL + // and Unsupported["auto-ttl-ipv4"/"auto-ttl-ipv6"] say why. + ErrAutoTTLUnsupported = errors.New("desync: auto TTL cannot observe the server's TTL here") + // ErrAutoTTLUnobserved reports that the server's TTL was not observed or + // does not yield a usable hop count, so no TTL could be derived. + ErrAutoTTLUnobserved = errors.New("desync: auto TTL could not derive a TTL from the observed path") +) + +// Defaults of AutoTTL, those of zapret's --dpi-desync-autottl (-1:3-20). +const ( + defaultAutoTTLDelta = 1 + defaultAutoTTLMin = 3 + defaultAutoTTLMax = 20 + // maxGuessedHops bounds the path lengths EstimateHops accepts. + maxGuessedHops = 32 +) + +// AutoTTL derives a low TTL from the observed length of the path to the +// server: TTL = hops - Delta, clamped to Min..Max. A result that is not below +// the hop count would reach the server, so it is rejected instead. The hop +// count comes from the TTL of the server's packets (see EstimateHops), which +// assumes the return path is as long as the forward path. +// +// Zero fields select the defaults (Delta 1, Min 3, Max 20, zapret's +// --dpi-desync-autottl default), so &AutoTTL{} is a complete policy. +type AutoTTL struct { + // Delta is subtracted from the hop count, 1..254 (0 means 1). It is the + // margin between the fake's last hop and the server. + Delta int + // Min and Max bound the TTL, 1..255 (0 means 3 and 20). A path so short + // that hops-Delta is below Min still uses Min, unless Min reaches the + // server; then no TTL is derived. + Min, Max int +} + +func (a AutoTTL) normalized() AutoTTL { + if a.Delta == 0 { + a.Delta = defaultAutoTTLDelta + } + if a.Min == 0 { + a.Min = defaultAutoTTLMin + } + if a.Max == 0 { + a.Max = defaultAutoTTLMax + } + return a +} + +// Validate checks the policy after applying defaults. +func (a AutoTTL) Validate() error { + if a.Delta < 0 || a.Delta > 254 { + return fmt.Errorf("desync: auto TTL delta must be between 1 and 254 (or zero for default)") + } + if a.Min < 0 || a.Min > 255 || a.Max < 0 || a.Max > 255 { + return fmt.Errorf("desync: auto TTL bounds must be between 1 and 255 (or zero for default)") + } + if n := a.normalized(); n.Min > n.Max { + return fmt.Errorf("desync: auto TTL minimum %d exceeds maximum %d", n.Min, n.Max) + } + return nil +} + +// TTL returns the TTL for a path of hops routers, or false when no TTL in +// Min..Max stays below the hop count. The policy must be valid. +func (a AutoTTL) TTL(hops int) (int, bool) { + a = a.normalized() + ttl := hops - a.Delta + if ttl < a.Min { + ttl = a.Min + } + if ttl > a.Max { + ttl = a.Max + } + if ttl >= hops || ttl < 1 { + return 0, false + } + return ttl, true +} + +// EstimateHops guesses how many routers decremented a packet that arrived +// with the given TTL (or IPv6 hop limit). Senders start at one of the common +// initial values 64 (Linux, macOS, most network gear), 128 (Windows) or 255; +// paths are assumed shorter than 32 hops, so 33..64 is read as starting at +// 64, 97..128 at 128 and 224..255 at 255. Any other value is ambiguous and +// returns false. The guess is zapret's hop_count_guess (MIT, see +// third_party/NOTICE), except that 64 and 128 count as zero hops. +func EstimateHops(ttl int) (int, bool) { + for _, initial := range [...]int{64, 128, 255} { + if ttl <= initial && ttl > initial-maxGuessedHops { + return initial - ttl, true + } + } + return 0, false +} + +// HopObservation is what ObserveHops read from a connection. +type HopObservation struct { + // TTL is the TTL or hop limit of the last in-order segment received + // from the server, normally its SYN-ACK. + TTL int + // Hops is EstimateHops(TTL). + Hops int +} + +// ObserveHops reads the TTL of the packets the server sent on c (unwrapping +// *Conn and other NetConn wrappers) and estimates the path length. It works +// only where PlatformCapabilities().AutoTTL reports the connection's family, +// and only for a socket created with ControlAutoTTL; otherwise the error +// wraps ErrAutoTTLUnsupported or ErrAutoTTLUnobserved. It changes nothing; +// call it before the first Write of a *Conn with AutoTTL, which turns the +// observation off once it used it. +func ObserveHops(c net.Conn) (HopObservation, error) { + raw, err := socketOf(c) + if err != nil { + return HopObservation{}, err + } + return observeHops(raw, false) +} + +func observeHops(raw syscall.RawConn, stop bool) (HopObservation, error) { + var ttl int + var opErr error + err := raw.Control(func(fd uintptr) { ttl, opErr = observeTTL(fd, stop) }) + if err == nil { + err = opErr + } + if err != nil { + return HopObservation{}, err + } + hops, ok := EstimateHops(ttl) + if !ok { + return HopObservation{TTL: ttl}, fmt.Errorf("%w: received TTL %d does not follow an initial TTL of 64, 128 or 255 within %d hops", ErrAutoTTLUnobserved, ttl, maxGuessedHops) + } + return HopObservation{TTL: ttl, Hops: hops}, nil +} + +// ControlAutoTTL prepares a socket before it connects, so that ObserveHops +// and FakeOptions.AutoTTL can later read the TTL of the server's SYN-ACK. Use +// it as (or call it from) net.Dialer.Control. Where the platform or family +// cannot observe the TTL it does nothing and returns nil, so a dialer can use +// it everywhere; the failure is reported when the TTL is needed. +// +// On Linux it enables IPV6_RECVHOPLIMIT on IPv6 sockets. The kernel then +// clones every in-order segment to record its hop limit; the *Conn wrapper +// disables the option again once it read the value for the fake. +func ControlAutoTTL(network, address string, c syscall.RawConn) error { + var opErr error + err := c.Control(func(fd uintptr) { opErr = prepareTTLObservation(fd, network) }) + if err != nil { + return err + } + return opErr +} + +// AutoTTLCapability describes how the package observes the server's TTL. +type AutoTTLCapability struct { + // Mechanism names the kernel interface, empty when there is none. + Mechanism string + // IPv4 and IPv6 report the peer families that can be observed. + IPv4, IPv6 bool +} + +// validateTTL checks Fake.TTL and Fake.AutoTTL. +func (f *FakeOptions) validateTTL() error { + if f.AutoTTL == nil { + if f.TTL < 1 || f.TTL > 255 { + return fmt.Errorf("desync: fake TTL must be between 1 and 255") + } + return nil + } + if f.TTL < 0 || f.TTL > 255 { + return fmt.Errorf("desync: fake TTL must be between 1 and 255 (or zero with AutoTTL)") + } + if err := f.AutoTTL.Validate(); err != nil { + return err + } + // Without a fallback TTL the fake needs an observable family. A platform + // without the fake reports ErrFakeUnsupported instead. + if f.TTL == 0 && supportsFake && !platformAutoTTL.IPv4 && !platformAutoTTL.IPv6 { + return ErrAutoTTLUnsupported + } + return nil +} + +// fakeTTL selects the TTL of the fake prefix before anything is sent. With +// AutoTTL it observes the path; when that fails it uses Fake.TTL as the +// fallback, or fails without a fallback. +func (c *Conn) fakeTTL(raw syscall.RawConn) (int, error) { + f := c.opts.Fake + if f.AutoTTL == nil { + c.usedTTL.Store(int32(f.TTL)) + return f.TTL, nil + } + obs, err := observeHops(raw, true) + if err == nil { + if ttl, ok := f.AutoTTL.TTL(obs.Hops); ok { + c.usedTTL.Store(int32(ttl) | autoTTLFlag) + return ttl, nil + } + n := f.AutoTTL.normalized() + err = fmt.Errorf("%w: %d hops (received TTL %d) leave no TTL in %d..%d below the server with delta %d", ErrAutoTTLUnobserved, obs.Hops, obs.TTL, n.Min, n.Max, n.Delta) + } + if f.TTL != 0 { + c.usedTTL.Store(int32(f.TTL)) + return f.TTL, nil + } + return 0, err +} + +const autoTTLFlag = 1 << 8 + +// FakeTTL reports the TTL the fake prefix was sent with, and whether AutoTTL +// derived it (false: the fixed or fallback Fake.TTL). It is 0 until the fake +// was attempted. +func (c *Conn) FakeTTL() (ttl int, auto bool) { + v := c.usedTTL.Load() + return int(v &^ autoTTLFlag), v&autoTTLFlag != 0 +} + +func writeAutoTTL(b *strings.Builder, a *AutoTTL) { + if a == nil { + return + } + n := a.normalized() + b.WriteString(",auto:") + b.WriteString(strconv.Itoa(n.Delta)) + b.WriteByte(':') + b.WriteString(strconv.Itoa(n.Min)) + b.WriteByte('-') + b.WriteString(strconv.Itoa(n.Max)) +} diff --git a/autottl_darwin.go b/autottl_darwin.go new file mode 100644 index 0000000..c492c16 --- /dev/null +++ b/autottl_darwin.go @@ -0,0 +1,8 @@ +//go:build darwin + +package desync + +// xnu's tcp_input never saves received IP control data (no ip_savecontrol or +// ip6_savecontrol call), and ip6_ctloutput answers getsockopt +// IPV6_2292PKTOPTIONS with empty data. +const autoTTLReason = "xnu TCP keeps no received IP control data (tcp_input saves none, and getsockopt IPV6_2292PKTOPTIONS always returns empty data)" diff --git a/autottl_linux.go b/autottl_linux.go new file mode 100644 index 0000000..58a4305 --- /dev/null +++ b/autottl_linux.go @@ -0,0 +1,120 @@ +//go:build linux + +package desync + +import ( + "fmt" + "os" + "strings" + "syscall" + "unsafe" +) + +// Linux (and Android) record the hop limit of IPv6 TCP segments when +// IPV6_RECVHOPLIMIT is enabled: tcp_v6_do_rcv clones each segment and, at +// ipv6_pktoptions, stores the hop limit of the last in-order one (including +// the SYN-ACK that completes connect) in np->mcast_hops. getsockopt +// IPV6_2292PKTOPTIONS returns it as an IPV6_HOPLIMIT control message on a +// stream socket. The option must be enabled before connect to see the SYN-ACK: +// after it nothing arrives before the ClientHello is sent. +// +// IPv4 has no counterpart: tcp_v4_do_rcv records nothing, and IP_PKTOPTIONS +// reports inet->mc_ttl, which only tcp_v4_syn_recv_sock (an accepted, passive +// socket) sets from a received packet. IPv4-mapped peers of an IPv6 socket +// take the IPv4 path too. Datagram ancillary data (IP_RECVTTL) is not +// delivered for TCP. +var platformAutoTTL = AutoTTLCapability{Mechanism: "IPV6_RECVHOPLIMIT/IPV6_2292PKTOPTIONS", IPv6: true} + +const autoTTLIPv4Reason = "Linux records no received TTL for an outgoing IPv4 TCP connection: IP_RECVTTL control data is datagram-only, and IP_PKTOPTIONS reports the multicast TTL, which only an accepted socket sets from the handshake" +const autoTTLIPv6Reason = "" + +// in6.h values, identical on every Linux architecture. +const ( + ipv6RecvHopLimit = 0x33 // IPV6_RECVHOPLIMIT + ipv6HopLimit = 0x34 // IPV6_HOPLIMIT + ipv62292PktOptions = 0x6 // IPV6_2292PKTOPTIONS +) + +func prepareTTLObservation(fd uintptr, network string) error { + if !strings.HasPrefix(network, "tcp") { + return nil + } + sa, err := syscall.Getsockname(int(fd)) + if err != nil { + return os.NewSyscallError("getsockname", err) + } + if _, ok := sa.(*syscall.SockaddrInet6); !ok || network == "tcp4" { + return nil + } + // A dual-stack socket may still connect to an IPv4-mapped address, which + // this cannot observe; enabling it there is harmless. + return os.NewSyscallError("setsockopt IPV6_RECVHOPLIMIT", syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, ipv6RecvHopLimit, 1)) +} + +// observeTTL reads the recorded hop limit. stop disables the recording +// afterwards, so the kernel no longer clones every received segment. +func observeTTL(fd uintptr, stop bool) (int, error) { + peer, err := socketPeer(fd) + if err != nil { + return 0, err + } + if peer.To4() != nil { + return 0, fmt.Errorf("%w: %s", ErrAutoTTLUnsupported, autoTTLIPv4Reason) + } + on, err := syscall.GetsockoptInt(int(fd), syscall.IPPROTO_IPV6, ipv6RecvHopLimit) + if err != nil { + return 0, os.NewSyscallError("getsockopt IPV6_RECVHOPLIMIT", err) + } + if on == 0 { + return 0, fmt.Errorf("%w: IPV6_RECVHOPLIMIT was not enabled before connect (dial with ControlAutoTTL)", ErrAutoTTLUnobserved) + } + buf := make([]byte, 256) + n, err := getsockoptBytes(fd, syscall.IPPROTO_IPV6, ipv62292PktOptions, buf) + if stop { + _ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, ipv6RecvHopLimit, 0) + } + if err != nil { + return 0, os.NewSyscallError("getsockopt IPV6_2292PKTOPTIONS", err) + } + // A 32-bit process on a 64-bit kernel still receives 64-bit control + // message headers here: do_ipv6_getsockopt does not set MSG_CMSG_COMPAT + // for IPV6_2292PKTOPTIONS (do_ip_getsockopt does for IP_PKTOPTIONS). + for _, layout := range [...][2]int{{int(unsafe.Sizeof(syscall.Cmsghdr{})), int(unsafe.Sizeof(uintptr(0)))}, {16, 8}} { + if v, ok := cmsgInt(buf[:n], layout[0], layout[1], syscall.IPPROTO_IPV6, ipv6HopLimit); ok { + // mcast_hops starts at 1 (IPV6_DEFAULT_MCASTHOPS) and the + // caller may set -1: neither was received. + if v < 1 || v > 255 { + break + } + return v, nil + } + } + return 0, fmt.Errorf("%w: the kernel reported no received hop limit", ErrAutoTTLUnobserved) +} + +// cmsgInt finds the int-valued control message level/typ in b, laid out with +// headers of hdr bytes (length word, level, type) and the given alignment. +func cmsgInt(b []byte, hdr, align, level, typ int) (int, bool) { + for len(b) >= hdr { + var length int + if hdr == 16 { + length = int(*(*uint64)(unsafe.Pointer(&b[0]))) + } else { + length = int(*(*uint32)(unsafe.Pointer(&b[0]))) + } + if length < hdr || length > len(b) { + return 0, false + } + lv := int(*(*int32)(unsafe.Pointer(&b[hdr-8]))) + ty := int(*(*int32)(unsafe.Pointer(&b[hdr-4]))) + if lv == level && ty == typ && length >= hdr+4 { + return int(*(*int32)(unsafe.Pointer(&b[hdr]))), true + } + next := (length + align - 1) &^ (align - 1) + if next > len(b) { + return 0, false + } + b = b[next:] + } + return 0, false +} diff --git a/autottl_linux_test.go b/autottl_linux_test.go new file mode 100644 index 0000000..e3a69ee --- /dev/null +++ b/autottl_linux_test.go @@ -0,0 +1,261 @@ +//go:build linux + +package desync + +import ( + "bytes" + "context" + "errors" + "net" + "syscall" + "testing" + "time" + "unsafe" +) + +// hopLimitListener listens on IPv6 loopback with hop limit hl, which the +// SYN-ACK (sent by the listener) and the accepted socket inherit, so a +// loopback path looks like 64/128/255 - hl hops to the client. +func hopLimitListener(t *testing.T, hl int) net.Listener { + t.Helper() + lc := net.ListenConfig{Control: func(_, _ string, c syscall.RawConn) error { + var opErr error + err := c.Control(func(fd uintptr) { + opErr = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, hl) + }) + if err != nil { + return err + } + return opErr + }} + ln, err := lc.Listen(context.Background(), "tcp6", "[::1]:0") + if err != nil { + t.Skipf("IPv6 loopback unavailable: %v", err) + } + t.Cleanup(func() { ln.Close() }) + return ln +} + +func dialAutoTTL(t *testing.T, ln net.Listener, control bool) (*net.TCPConn, *net.TCPConn) { + t.Helper() + d := net.Dialer{Timeout: 5 * time.Second} + if control { + d.Control = ControlAutoTTL + } + client, err := d.Dial("tcp6", ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + server, err := ln.Accept() + if err != nil { + client.Close() + t.Fatal(err) + } + t.Cleanup(func() { client.Close(); server.Close() }) + return client.(*net.TCPConn), server.(*net.TCPConn) +} + +// The client reads the hop limit of the server's SYN-ACK right after +// connect, before it sent anything, without privileges. +func TestLinuxObserveHopsSynAck(t *testing.T) { + for _, tc := range []struct{ hl, hops int }{{120, 8}, {61, 3}, {250, 5}, {64, 0}} { + ln := hopLimitListener(t, tc.hl) + client, _ := dialAutoTTL(t, ln, true) + got, err := ObserveHops(WrapConn(client, Options{TLSRec: []Pos{{Offset: 1}}})) + if err != nil || got.TTL != tc.hl || got.Hops != tc.hops { + t.Fatalf("hop limit %d: %+v %v, want TTL %d hops %d", tc.hl, got, err, tc.hl, tc.hops) + } + } + // Ambiguous values are reported, not guessed. + ln := hopLimitListener(t, 80) + client, _ := dialAutoTTL(t, ln, true) + if got, err := ObserveHops(client); !errors.Is(err, ErrAutoTTLUnobserved) || got.TTL != 80 { + t.Fatalf("hop limit 80: %+v %v", got, err) + } +} + +// The recorded value follows the last in-order segment, here data sent with +// a different hop limit after the handshake. +func TestLinuxObserveHopsFollowsData(t *testing.T) { + ln := hopLimitListener(t, 120) + client, server := dialAutoTTL(t, ln, true) + raw, err := server.SyscallConn() + if err != nil { + t.Fatal(err) + } + var opErr error + if err := raw.Control(func(fd uintptr) { + opErr = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, 100) + }); err != nil || opErr != nil { + t.Fatal(err, opErr) + } + if _, err := server.Write([]byte("x")); err != nil { + t.Fatal(err) + } + _ = client.SetReadDeadline(time.Now().Add(5 * time.Second)) + if _, err := client.Read(make([]byte, 1)); err != nil { + t.Fatal(err) + } + if got, err := ObserveHops(client); err != nil || got.TTL != 100 || got.Hops != 28 { + t.Fatalf("%+v %v", got, err) + } +} + +func TestLinuxObserveHopsNeedsControl(t *testing.T) { + ln := hopLimitListener(t, 120) + client, _ := dialAutoTTL(t, ln, false) + if _, err := ObserveHops(client); !errors.Is(err, ErrAutoTTLUnobserved) { + t.Fatalf("without ControlAutoTTL: %v", err) + } + // IPv4 stays unobservable even when prepared. + ln4, err := net.Listen("tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln4.Close() + c4, err := (&net.Dialer{Control: ControlAutoTTL}).Dial("tcp4", ln4.Addr().String()) + if err != nil { + t.Fatal(err) + } + defer c4.Close() + if _, err := ObserveHops(c4); !errors.Is(err, ErrAutoTTLUnsupported) { + t.Fatalf("IPv4: %v", err) + } +} + +// End to end: the fake is sent with the TTL derived from the SYN-ACK (hop +// limit 120: 8 hops, TTL 7). The server drops segments below hop limit 8, +// like the router before it would, so the stream is intact only if the fake +// used the derived TTL and TCP retransmitted the original bytes. +func TestLinuxFakeAutoTTLRoundTrip(t *testing.T) { + ln := hopLimitListener(t, 120) + client, server := dialAutoTTL(t, ln, true) + dropLowHop(t, server, true) + hello := captureHello(t, "example.test") + tail := []byte("after the hello") + before := tcpHop(t, client) + w := WrapConn(client, Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{}, Payload: bytes.Repeat([]byte{'X'}, 600)}}).(*Conn) + _ = w.SetWriteDeadline(time.Now().Add(10 * time.Second)) + read := readUntil(server, tail) + input := append(append([]byte(nil), hello...), tail...) + if n, err := w.Write(input); err != nil || n != len(input) { + t.Fatalf("Write: %d %v", n, err) + } + if got := <-read; !bytes.Equal(got, input) { + t.Fatalf("server stream changed: got %d bytes %q", len(got), got) + } + if ttl, auto := w.FakeTTL(); ttl != 7 || !auto { + t.Fatalf("FakeTTL: %d %v, want 7 derived", ttl, auto) + } + if after := tcpHop(t, client); after != before { + t.Fatalf("hop limit not restored: %d -> %d", before, after) + } + if n, ok := tcpRetransmits(t, client); ok && n == 0 { + t.Fatal("the fake was not dropped and retransmitted") + } + // The wrapper stopped the per-segment recording after reading it. + raw, _ := client.SyscallConn() + var on int + _ = raw.Control(func(fd uintptr) { on, _ = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_IPV6, ipv6RecvHopLimit) }) + if on != 0 { + t.Fatal("IPV6_RECVHOPLIMIT still enabled") + } +} + +// The reason IPv4 is unsupported, observed: with IP_RECVTTL enabled before +// connect, IP_PKTOPTIONS on the client reports the multicast TTL (1), not the +// TTL 120 of the server's SYN-ACK and data. +func TestLinuxIPv4TCPRecordsNoTTL(t *testing.T) { + const ipPktOptions, ipRecvTTL, ipTTL = 9, 12, 2 // linux/in.h + lc := net.ListenConfig{Control: func(_, _ string, c syscall.RawConn) error { + return c.Control(func(fd uintptr) { _ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_TTL, 120) }) + }} + ln, err := lc.Listen(context.Background(), "tcp4", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + d := net.Dialer{Control: func(_, _ string, c syscall.RawConn) error { + return c.Control(func(fd uintptr) { _ = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, ipRecvTTL, 1) }) + }} + client, err := d.Dial("tcp4", ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + defer client.Close() + server, err := ln.Accept() + if err != nil { + t.Fatal(err) + } + defer server.Close() + if _, err := server.Write([]byte("x")); err != nil { + t.Fatal(err) + } + _ = client.SetReadDeadline(time.Now().Add(5 * time.Second)) + if _, err := client.Read(make([]byte, 1)); err != nil { + t.Fatal(err) + } + raw, _ := client.(*net.TCPConn).SyscallConn() + buf := make([]byte, 256) + var n int + var opErr error + _ = raw.Control(func(fd uintptr) { n, opErr = getsockoptBytes(fd, syscall.IPPROTO_IP, ipPktOptions, buf) }) + if opErr != nil { + t.Fatal(opErr) + } + msgs, err := syscall.ParseSocketControlMessage(buf[:n]) + if err != nil { + t.Fatal(err) + } + for _, m := range msgs { + if m.Header.Level == syscall.IPPROTO_IP && m.Header.Type == ipTTL && len(m.Data) >= 4 { + v := int(m.Data[0]) | int(m.Data[1])<<8 | int(m.Data[2])<<16 | int(m.Data[3])<<24 + if v == 120 { + t.Fatalf("the kernel now reports the received TTL on IPv4 TCP; auto TTL could support IPv4") + } + t.Logf("IP_PKTOPTIONS IP_TTL: %d (not the received 120)", v) + return + } + } + t.Logf("IP_PKTOPTIONS returned no IP_TTL message (%d bytes)", n) +} + +// Both header layouts are parsed: a 32-bit process on a 64-bit kernel gets +// 64-bit headers from IPV6_2292PKTOPTIONS. +func TestLinuxCmsgIntLayouts(t *testing.T) { + // 64-bit: IPV6_PKTINFO (len 36, padded to 40) then IPV6_HOPLIMIT 120. + b64 := make([]byte, 64) + put := func(b []byte, off int, v uint32) { + b[off], b[off+1], b[off+2], b[off+3] = byte(v), byte(v>>8), byte(v>>16), byte(v>>24) + } + little := func() bool { x := uint16(1); return *(*byte)(unsafe.Pointer(&x)) == 1 }() + if !little { + t.Skip("layout fixture is little-endian") + } + put(b64, 0, 36) + put(b64, 8, syscall.IPPROTO_IPV6) + put(b64, 12, 50) // IPV6_PKTINFO + put(b64, 40, 20) + put(b64, 48, syscall.IPPROTO_IPV6) + put(b64, 52, ipv6HopLimit) + put(b64, 56, 120) + if v, ok := cmsgInt(b64[:60], 16, 8, syscall.IPPROTO_IPV6, ipv6HopLimit); !ok || v != 120 { + t.Fatalf("64-bit: %d %v", v, ok) + } + if _, ok := cmsgInt(b64[:60], 12, 4, syscall.IPPROTO_IPV6, ipv6HopLimit); ok { + t.Fatal("64-bit headers misread as 32-bit") + } + b32 := make([]byte, 16) + put(b32, 0, 16) + put(b32, 4, syscall.IPPROTO_IPV6) + put(b32, 8, ipv6HopLimit) + put(b32, 12, 61) + if v, ok := cmsgInt(b32, 12, 4, syscall.IPPROTO_IPV6, ipv6HopLimit); !ok || v != 61 { + t.Fatalf("32-bit: %d %v", v, ok) + } + for _, b := range [][]byte{nil, make([]byte, 16), {200, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} { + if _, ok := cmsgInt(b, 16, 8, syscall.IPPROTO_IPV6, ipv6HopLimit); ok { + t.Fatalf("garbage %x parsed", b) + } + } +} diff --git a/autottl_other.go b/autottl_other.go new file mode 100644 index 0000000..9455d59 --- /dev/null +++ b/autottl_other.go @@ -0,0 +1,20 @@ +//go:build !linux + +package desync + +import "fmt" + +// No unprivileged interface reports the TTL of a TCP socket's received +// packets here; autoTTLReason (per OS) says why. +var platformAutoTTL = AutoTTLCapability{} + +const ( + autoTTLIPv4Reason = autoTTLReason + autoTTLIPv6Reason = autoTTLReason +) + +func prepareTTLObservation(uintptr, string) error { return nil } + +func observeTTL(uintptr, bool) (int, error) { + return 0, fmt.Errorf("%w: %s", ErrAutoTTLUnsupported, autoTTLReason) +} diff --git a/autottl_reason_other.go b/autottl_reason_other.go new file mode 100644 index 0000000..7b8af9c --- /dev/null +++ b/autottl_reason_other.go @@ -0,0 +1,5 @@ +//go:build !linux && !windows && !darwin + +package desync + +const autoTTLReason = "observing the server's TTL is not implemented on this platform" diff --git a/autottl_test.go b/autottl_test.go new file mode 100644 index 0000000..a27365a --- /dev/null +++ b/autottl_test.go @@ -0,0 +1,221 @@ +package desync + +import ( + "bytes" + "errors" + "runtime" + "strings" + "testing" + "time" +) + +func TestEstimateHops(t *testing.T) { + for _, tc := range []struct { + ttl, hops int + ok bool + }{ + {64, 0, true}, {63, 1, true}, {60, 4, true}, {50, 14, true}, {33, 31, true}, + {128, 0, true}, {127, 1, true}, {120, 8, true}, {116, 12, true}, {97, 31, true}, + {255, 0, true}, {254, 1, true}, {243, 12, true}, {224, 31, true}, + {32, 0, false}, {1, 0, false}, {0, 0, false}, {-1, 0, false}, + {65, 0, false}, {96, 0, false}, {129, 0, false}, {200, 0, false}, {223, 0, false}, {256, 0, false}, + } { + hops, ok := EstimateHops(tc.ttl) + if hops != tc.hops || ok != tc.ok { + t.Errorf("EstimateHops(%d) = %d, %v; want %d, %v", tc.ttl, hops, ok, tc.hops, tc.ok) + } + } +} + +func TestAutoTTLPolicy(t *testing.T) { + for _, tc := range []struct { + policy AutoTTL + hops, ttl int + ok bool + }{ + {AutoTTL{}, 10, 9, true}, + {AutoTTL{}, 4, 3, true}, + {AutoTTL{}, 3, 0, false}, // Min 3 would reach the server + {AutoTTL{}, 0, 0, false}, + {AutoTTL{}, 30, 20, true}, // clamped to Max + {AutoTTL{Delta: 1, Min: 3, Max: 20}, 12, 11, true}, + {AutoTTL{Delta: 2, Min: 1, Max: 255}, 5, 3, true}, + {AutoTTL{Delta: 2, Min: 1, Max: 255}, 2, 1, true}, // 0 raised to Min + {AutoTTL{Delta: 2, Min: 1, Max: 255}, 1, 0, false}, + {AutoTTL{Delta: 3}, 5, 3, true}, // 2 raised to Min 3, still short + {AutoTTL{Delta: 4, Min: 6, Max: 8}, 31, 8, true}, + {AutoTTL{Delta: 4, Min: 6, Max: 8}, 6, 0, false}, + } { + if err := tc.policy.Validate(); err != nil { + t.Fatalf("%+v: %v", tc.policy, err) + } + ttl, ok := tc.policy.TTL(tc.hops) + if ttl != tc.ttl || ok != tc.ok { + t.Errorf("%+v.TTL(%d) = %d, %v; want %d, %v", tc.policy, tc.hops, ttl, ok, tc.ttl, tc.ok) + } + } +} + +func TestAutoTTLValidate(t *testing.T) { + for _, bad := range []AutoTTL{ + {Delta: -1}, {Delta: 255}, {Min: -1}, {Min: 256}, {Max: -1}, {Max: 256}, + {Min: 30}, // above the default Max 20 + {Min: 9, Max: 8}, // inverted + {Max: 2}, // below the default Min 3 + {Delta: 1000, Min: 1, Max: 2}, + } { + err := bad.Validate() + if err == nil || !strings.HasPrefix(err.Error(), "desync: ") { + t.Errorf("%+v: %v", bad, err) + } + } + for _, good := range []AutoTTL{{}, {Delta: 254, Min: 255, Max: 255}, {Min: 30, Max: 40}, {Min: 1, Max: 2}} { + if err := good.Validate(); err != nil { + t.Errorf("%+v: %v", good, err) + } + } +} + +func TestFakeAutoTTLValidation(t *testing.T) { + for _, ttl := range []int{-1, 256} { + err := (Options{Fake: &FakeOptions{TTL: ttl, AutoTTL: &AutoTTL{}}}).Validate() + if err == nil || !strings.HasPrefix(err.Error(), "desync: ") { + t.Fatalf("TTL %d with AutoTTL: %v", ttl, err) + } + } + if err := (Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{Min: 50}}}).Validate(); err == nil || !strings.Contains(err.Error(), "auto TTL") { + t.Fatalf("invalid policy accepted: %v", err) + } + if err := (Options{Fake: &FakeOptions{}}).Validate(); err == nil || err.Error() != "desync: fake TTL must be between 1 and 255" { + t.Fatalf("TTL 0 without AutoTTL: %v", err) + } + caps := PlatformCapabilities() + auto := (Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{}}}).Validate() + fallback := (Options{Fake: &FakeOptions{TTL: 6, AutoTTL: &AutoTTL{}}}).Validate() + switch { + case !caps.FakeTCP: + if !errors.Is(auto, ErrFakeUnsupported) || !errors.Is(fallback, ErrFakeUnsupported) { + t.Fatalf("%s: %v / %v", runtime.GOOS, auto, fallback) + } + case caps.AutoTTL.IPv4 || caps.AutoTTL.IPv6: + if auto != nil || fallback != nil { + t.Fatalf("%s: %v / %v", runtime.GOOS, auto, fallback) + } + default: + if !errors.Is(auto, ErrAutoTTLUnsupported) || fallback != nil { + t.Fatalf("%s: %v / %v", runtime.GOOS, auto, fallback) + } + } +} + +func TestAutoTTLKey(t *testing.T) { + fixed := Options{Fake: &FakeOptions{TTL: 4}} + const want = "desync/v1;tlsrec=;split=;delay=0;hello=default;fake=ttl:4,prefix:0,timeout:0,payload:default;random=off" + if got := fixed.Key(); got != want { + t.Fatalf("fixed-TTL key changed:\n got %s\nwant %s", got, want) + } + auto := Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{}}} + const wantAuto = "desync/v1;tlsrec=;split=;delay=0;hello=default;fake=ttl:0,prefix:0,timeout:0,payload:default,auto:1:3-20;random=off" + if got := auto.Key(); got != wantAuto { + t.Fatalf("auto key:\n got %s\nwant %s", got, wantAuto) + } + explicit := Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{Delta: 1, Min: 3, Max: 20}}} + if explicit.Key() != auto.Key() { + t.Fatal("defaults and explicit defaults differ") + } + keys := map[string]bool{} + for _, o := range []Options{ + fixed, auto, + {Fake: &FakeOptions{AutoTTL: &AutoTTL{Delta: 2}}}, + {Fake: &FakeOptions{AutoTTL: &AutoTTL{Min: 4}}}, + {Fake: &FakeOptions{AutoTTL: &AutoTTL{Max: 12}}}, + {Fake: &FakeOptions{TTL: 4, AutoTTL: &AutoTTL{}}}, + } { + if keys[o.Key()] { + t.Fatalf("duplicate key %s", o.Key()) + } + keys[o.Key()] = true + } +} + +func TestAutoTTLCapabilityPerOS(t *testing.T) { + c := PlatformCapabilities() + var want AutoTTLCapability + if runtime.GOOS == "linux" || runtime.GOOS == "android" { + want = AutoTTLCapability{Mechanism: "IPV6_RECVHOPLIMIT/IPV6_2292PKTOPTIONS", IPv6: true} + } + if c.AutoTTL != want { + t.Fatalf("%s: auto TTL %+v, want %+v", runtime.GOOS, c.AutoTTL, want) + } + for name, missing := range map[string]bool{"auto-ttl-ipv4": !want.IPv4, "auto-ttl-ipv6": !want.IPv6} { + if reason, ok := c.Unsupported[name]; ok != missing || ok && reason == "" { + t.Fatalf("%s: reason %q (present %v), want present %v", name, reason, ok, missing) + } + } + t.Logf("%s auto TTL: %+v, unsupported: ipv4 %q ipv6 %q", runtime.GOOS, c.AutoTTL, c.Unsupported["auto-ttl-ipv4"], c.Unsupported["auto-ttl-ipv6"]) +} + +// A connection whose family cannot be observed reports why, on every OS. +func TestObserveHopsUnobservableLoopback(t *testing.T) { + caps := PlatformCapabilities().AutoTTL + for _, network := range []string{"tcp4", "tcp6"} { + t.Run(network, func(t *testing.T) { + client, _ := loopbackPair(t, network) + _, err := ObserveHops(WrapConn(client, Options{TLSRec: []Pos{{Offset: 1}}})) + supported := caps.IPv4 && network == "tcp4" || caps.IPv6 && network == "tcp6" + // A supported family was not prepared with ControlAutoTTL. + if supported && !errors.Is(err, ErrAutoTTLUnobserved) || !supported && !errors.Is(err, ErrAutoTTLUnsupported) { + t.Fatalf("ObserveHops: %v", err) + } + t.Logf("%v", err) + }) + } +} + +// AutoTTL falls back to Fake.TTL when the path cannot be observed, and fails +// before sending anything without a fallback. tcp4 loopback is unobservable +// on every OS. +func TestFakeAutoTTLFallback(t *testing.T) { + if !SupportsFake() { + t.Skip("no TCP fake on " + runtime.GOOS) + } + hello := captureHello(t, "example.test") + tail := []byte("after the hello") + t.Run("fallback", func(t *testing.T) { + client, server := loopbackPair(t, "tcp4") + // Loopback delivers the first transmission: fake bytes equal to the + // real ones keep the stream verifiable. + w := WrapConn(client, Options{Fake: &FakeOptions{TTL: 64, AutoTTL: &AutoTTL{}, Payload: hello}}).(*Conn) + if ttl, auto := w.FakeTTL(); ttl != 0 || auto { + t.Fatalf("FakeTTL before the write: %d %v", ttl, auto) + } + _ = w.SetWriteDeadline(time.Now().Add(10 * time.Second)) + read := readUntil(server, tail) + input := append(append([]byte(nil), hello...), tail...) + if n, err := w.Write(input); err != nil || n != len(input) { + t.Fatalf("Write: %d %v", n, err) + } + if got := <-read; !bytes.Equal(got, input) { + t.Fatalf("server stream changed: %d bytes", len(got)) + } + if ttl, auto := w.FakeTTL(); ttl != 64 || auto { + t.Fatalf("FakeTTL: %d %v, want the fallback 64", ttl, auto) + } + }) + t.Run("no-fallback", func(t *testing.T) { + opts := Options{Fake: &FakeOptions{AutoTTL: &AutoTTL{}}} + if err := opts.Validate(); err != nil { + t.Skipf("rejected by Validate: %v", err) + } + client, server := loopbackPair(t, "tcp4") + w := WrapConn(client, opts) + _ = w.SetWriteDeadline(time.Now().Add(10 * time.Second)) + if _, err := w.Write(hello); !errors.Is(err, ErrAutoTTLUnsupported) { + t.Fatalf("Write: %v", err) + } + _ = server.SetReadDeadline(time.Now().Add(200 * time.Millisecond)) + if n, _ := server.Read(make([]byte, 1)); n != 0 { + t.Fatal("bytes reached the server") + } + }) +} diff --git a/autottl_windows.go b/autottl_windows.go new file mode 100644 index 0000000..b817220 --- /dev/null +++ b/autottl_windows.go @@ -0,0 +1,8 @@ +//go:build windows + +package desync + +// Winsock returns received-TTL control data only to WSARecvMsg, which, like +// IP_RECVTTL and IPV6_RECVHOPLIMIT, is limited to datagram and raw sockets, +// and SIO_TCP_INFO (TCP_INFO_v0/v1) has no TTL field. +const autoTTLReason = "Winsock reports a received TTL only through WSARecvMsg control data, which datagram and raw sockets get (IP_RECVTTL/IPV6_RECVHOPLIMIT); SIO_TCP_INFO has no TTL field" diff --git a/capability.go b/capability.go index cff9816..2d19018 100644 --- a/capability.go +++ b/capability.go @@ -88,8 +88,11 @@ type Capabilities struct { OOB bool // Ack describes send-queue / acknowledgement observation for SplitWait. Ack AckCapability + // AutoTTL describes observing the server's TTL for FakeOptions.AutoTTL. + AutoTTL AutoTTLCapability // Unsupported explains, keyed by method ("fake-tcp", "fake-udp", - // "disorder", "oob"), why a method is not implemented on this OS. + // "disorder", "oob", "auto-ttl-ipv4", "auto-ttl-ipv6"), why a method is + // not implemented on this OS. Unsupported map[string]string } @@ -129,8 +132,15 @@ func PlatformCapabilities() Capabilities { Disorder: platformDisorder, OOB: supportsOOB, Ack: platformAck, + AutoTTL: platformAutoTTL, Unsupported: map[string]string{}, } + if !platformAutoTTL.IPv4 { + c.Unsupported["auto-ttl-ipv4"] = autoTTLIPv4Reason + } + if !platformAutoTTL.IPv6 { + c.Unsupported["auto-ttl-ipv6"] = autoTTLIPv6Reason + } if !supportsFake { c.Unsupported["fake-tcp"] = fakeUnsupportedReason } diff --git a/conn.go b/conn.go index 7bafabc..6f938e9 100644 --- a/conn.go +++ b/conn.go @@ -5,6 +5,7 @@ import ( "net" "os" "sync" + "sync/atomic" "time" ) @@ -73,6 +74,8 @@ type Conn struct { writeDeadline time.Time deadlineChanged chan struct{} watch *helloWatch // guarded by deadlineMu + + usedTTL atomic.Int32 // the fake's TTL, see FakeTTL } // WrapConn returns c with the options applied, or c itself when they are @@ -91,6 +94,10 @@ func WrapConn(c net.Conn, opts Options) net.Conn { if opts.Fake != nil { fake := *opts.Fake fake.Payload = append([]byte(nil), fake.Payload...) + if fake.AutoTTL != nil { + auto := *fake.AutoTTL + fake.AutoTTL = &auto + } opts.Fake = &fake } if opts.OOB != nil { diff --git a/fake.go b/fake.go index c9b40c8..b189c0e 100644 --- a/fake.go +++ b/fake.go @@ -19,7 +19,14 @@ var ( // bytes into the application's stream. Implemented on Windows and Linux // (including Android); see PlatformCapabilities for the macOS reason. type FakeOptions struct { + // TTL expires the fake before the server (1..255). With AutoTTL it is + // the fallback used when the path cannot be observed; 0 then fails the + // write instead. TTL int + // AutoTTL derives the TTL from the TTL of the server's packets on this + // connection (see AutoTTL, ControlAutoTTL and + // PlatformCapabilities().AutoTTL). + AutoTTL *AutoTTL // Prefix is measured after TLS record fragmentation. Negative offsets // count from the end; zero defaults to -1 (all but the last byte). Prefix int @@ -54,8 +61,8 @@ func (o Options) Validate() error { if o.Fake == nil { return nil } - if o.Fake.TTL < 1 || o.Fake.TTL > 255 { - return fmt.Errorf("desync: fake TTL must be between 1 and 255") + if err := o.Fake.validateTTL(); err != nil { + return err } if o.Fake.Timeout < 0 { return fmt.Errorf("desync: fake timeout must not be negative") diff --git a/fake_linux.go b/fake_linux.go index 3221974..2484ad7 100644 --- a/fake_linux.go +++ b/fake_linux.go @@ -141,6 +141,10 @@ func (c *Conn) sendFake(real, payload, tail []byte) error { if err != nil { return fmt.Errorf("desync: TCP fake: %w", err) } + ttl, err := c.fakeTTL(raw) + if err != nil { + return err + } select { case fakeSlots <- struct{}{}: default: @@ -151,7 +155,7 @@ func (c *Conn) sendFake(real, payload, tail []byte) error { if err != nil { return err } - if err := c.spliceFake(raw, obs, op, real, start); err != nil { + if err := c.spliceFake(raw, obs, op, real, ttl, start); err != nil { return fmt.Errorf("desync: TCP fake: %w", err) } // The original prefix is in place and the hop limit restored: the tail @@ -165,12 +169,12 @@ func (c *Conn) sendFake(real, payload, tail []byte) error { // spliceFake transmits the fake prefix with the low hop limit. Whatever // happens, the page is rewritten with the original bytes, then the hop limit // is restored, then the resources are released. -func (c *Conn) spliceFake(raw syscall.RawConn, obs ackObserver, op *spliceFake, real []byte, start time.Time) (err error) { +func (c *Conn) spliceFake(raw syscall.RawConn, obs ackObserver, op *spliceFake, real []byte, ttl int, start time.Time) (err error) { defer func() { op.restore(real) op.release() }() - saved, err := lowerHop(raw, c.opts.Fake.TTL) + saved, err := lowerHop(raw, ttl) if err != nil { return err } diff --git a/fake_windows.go b/fake_windows.go index 77f5f4a..13b5fa0 100644 --- a/fake_windows.go +++ b/fake_windows.go @@ -204,6 +204,10 @@ func (c *Conn) sendFake(real, payload, tail []byte) error { if err != nil { return err } + ttl, err := c.fakeTTL(raw) + if err != nil { + return err + } ticker := time.NewTicker(5 * time.Millisecond) defer ticker.Stop() acquire: @@ -239,7 +243,7 @@ acquire: return } nativeErr = func() (callErr error) { - saved, ttlErr := setFakeTTL(socket, c.opts.Fake.TTL) + saved, ttlErr := setFakeTTL(socket, ttl) if ttlErr != nil { return ttlErr } diff --git a/key.go b/key.go index 86a6c91..62383f6 100644 --- a/key.go +++ b/key.go @@ -55,6 +55,8 @@ func (o Options) Key() string { sum := sha256.Sum256(f.Payload) b.WriteString(hex.EncodeToString(sum[:])) } + // Only when set, so keys of fixed-TTL fakes are unchanged. + writeAutoTTL(&b, f.AutoTTL) } b.WriteString(";random=") if r := o.Random; r == nil { diff --git a/sockopt_linux.go b/sockopt_linux.go new file mode 100644 index 0000000..eb6973a --- /dev/null +++ b/sockopt_linux.go @@ -0,0 +1,19 @@ +//go:build linux && !386 + +package desync + +import ( + "syscall" + "unsafe" +) + +// getsockoptBytes reads a socket option into buf and returns its length. +func getsockoptBytes(fd uintptr, level, option int, buf []byte) (int, error) { + size := uint32(len(buf)) + _, _, errno := syscall.Syscall6(syscall.SYS_GETSOCKOPT, fd, uintptr(level), uintptr(option), + uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&size)), 0) + if errno != 0 { + return 0, errno + } + return int(size), nil +} diff --git a/sockopt_linux_386.go b/sockopt_linux_386.go new file mode 100644 index 0000000..837178b --- /dev/null +++ b/sockopt_linux_386.go @@ -0,0 +1,32 @@ +//go:build linux && 386 + +package desync + +import ( + "syscall" + "unsafe" +) + +const socketcallGetsockopt = 15 // linux/net.h SYS_GETSOCKOPT + +// getsockoptBytes reads a socket option into buf and returns its length. +// linux/386 reaches getsockopt through socketcall, whose arguments are passed +// in memory, out of reach of the rule that keeps syscall arguments in place. +// The value and its length therefore live in one buffer of non-constant +// size, which the compiler allocates on the (non-moving) heap. +func getsockoptBytes(fd uintptr, level, option int, buf []byte) (int, error) { + mem := make([]byte, 4+len(buf)) + size := (*uint32)(unsafe.Pointer(&mem[0])) + *size = uint32(len(buf)) + args := [5]uintptr{fd, uintptr(level), uintptr(option), uintptr(unsafe.Pointer(&mem[4])), uintptr(unsafe.Pointer(size))} + _, _, errno := syscall.Syscall(syscall.SYS_SOCKETCALL, socketcallGetsockopt, uintptr(unsafe.Pointer(&args)), 0) + if errno != 0 { + return 0, errno + } + n := int(*size) + if n > len(buf) { + n = len(buf) + } + copy(buf, mem[4:4+n]) + return n, nil +} diff --git a/third_party/NOTICE b/third_party/NOTICE index 6588f7d..6e8fef9 100644 --- a/third_party/NOTICE +++ b/third_party/NOTICE @@ -18,3 +18,13 @@ hufrea. The license text is in BYEDPI-LICENSE. Derived from it: before the next one), --oob and --disoob (the piece and one urgent byte sent with MSG_OOB), desync.c DESYNC_DISORDER, DESYNC_OOB, DESYNC_DISOOB and send_oob. Reimplemented in Go; the default urgent byte 'a' is ByeDPI's. + +zapret (https://github.com/bol-van/zapret), MIT License, Copyright (c) +2016-2024 bol-van. The license text is in ZAPRET-LICENSE. Derived from it: + +- autottl.go: the hop count guess from a received TTL and the standard + initial TTLs 64/128/255 within 32 hops (nfq/darkmagic.c hop_count_guess), + and the delta/min/max TTL policy with the defaults 1, 3 and 20 and the + rejection of a TTL not below the hop count (autottl_eval, + AUTOTTL_DEFAULT_DESYNC_*, --dpi-desync-autottl), v72.13. Reimplemented in + Go; 64 and 128 are read as zero hops. diff --git a/third_party/ZAPRET-LICENSE b/third_party/ZAPRET-LICENSE new file mode 100644 index 0000000..6f8f4cc --- /dev/null +++ b/third_party/ZAPRET-LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016-2024 bol-van + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 787a18c02a89ad61abb5684515bfa78a1dcc779c Mon Sep 17 00:00:00 2001 From: Nemu-x Date: Thu, 29 Oct 2026 20:37:31 +0300 Subject: [PATCH 2/3] feat: expand a TTL range into candidate options FakeTTLCandidates and UDPTTLCandidates return one option set per TTL with a stable Key, so discovery can probe the TTL where the path cannot be observed. UDPOptions gains Key. --- ttlrange.go | 88 +++++++++++++++++++++++++++++++++++++++++++++ ttlrange_test.go | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 ttlrange.go create mode 100644 ttlrange_test.go diff --git a/ttlrange.go b/ttlrange.go new file mode 100644 index 0000000..9b04a57 --- /dev/null +++ b/ttlrange.go @@ -0,0 +1,88 @@ +package desync + +import ( + "crypto/sha256" + "encoding/hex" + "fmt" + "strconv" + "strings" +) + +// maxTTLCandidates bounds a TTL range expansion; a discovery budget (for +// example controller.Config.MaxAttempts, default 8) is usually far smaller. +const maxTTLCandidates = 64 + +func validTTLRange(from, to int) error { + if from < 1 || to > 255 || from > to { + return fmt.Errorf("desync: TTL range must satisfy 1 <= from <= to <= 255") + } + if to-from+1 > maxTTLCandidates { + return fmt.Errorf("desync: TTL range must have at most %d values", maxTTLCandidates) + } + return nil +} + +// FakeTTLCandidates expands a TTL range into one Options per TTL, in +// ascending order: copies of base with Fake.TTL set to from..to and +// Fake.AutoTTL cleared. It is the deterministic fallback where the path +// cannot be observed (see PlatformCapabilities().AutoTTL): register each +// candidate under an ID derived from its Key, which is stable for a given +// base and TTL, and let automatic discovery probe them. base.Fake must be +// set; the candidates share base's slices, which WrapConn copies. +func FakeTTLCandidates(base Options, from, to int) ([]Options, error) { + if base.Fake == nil { + return nil, fmt.Errorf("desync: TTL candidates need Fake options") + } + if err := validTTLRange(from, to); err != nil { + return nil, err + } + out := make([]Options, 0, to-from+1) + for ttl := from; ttl <= to; ttl++ { + o := base + fake := *base.Fake + fake.TTL, fake.AutoTTL = ttl, nil + o.Fake = &fake + out = append(out, o) + } + return out, nil +} + +// UDPTTLCandidates is FakeTTLCandidates for the UDP burst. The burst precedes +// the peer's first reply, so there is no packet whose TTL could be observed +// in time; a TTL range probed by discovery, or AutoTTL.TTL applied to the +// hops ObserveHops measured on a TCP connection to the same server, selects +// it instead. +func UDPTTLCandidates(base UDPOptions, from, to int) ([]UDPOptions, error) { + if err := validTTLRange(from, to); err != nil { + return nil, err + } + out := make([]UDPOptions, 0, to-from+1) + for ttl := from; ttl <= to; ttl++ { + o := base + o.TTL = ttl + out = append(out, o) + } + return out, nil +} + +// Key returns a stable, canonical description of the fields that affect the +// UDP burst, like Options.Key (Counters is not part of it). The payload is +// represented by its SHA-256 digest. +func (o UDPOptions) Key() string { + var b strings.Builder + b.WriteString(keyVersion) + b.WriteString(";udp=ttl:") + b.WriteString(strconv.Itoa(o.TTL)) + b.WriteString(",repeats:") + b.WriteString(strconv.Itoa(o.Repeats)) + b.WriteString(",packets:") + b.WriteString(strconv.Itoa(o.Packets)) + b.WriteString(",payload:") + if len(o.Payload) == 0 { + b.WriteString("default") + } else { + sum := sha256.Sum256(o.Payload) + b.WriteString(hex.EncodeToString(sum[:])) + } + return b.String() +} diff --git a/ttlrange_test.go b/ttlrange_test.go new file mode 100644 index 0000000..a4857d0 --- /dev/null +++ b/ttlrange_test.go @@ -0,0 +1,92 @@ +package desync + +import ( + "strconv" + "strings" + "testing" +) + +func TestFakeTTLCandidates(t *testing.T) { + base := Options{TLSRec: []Pos{{Offset: 1, FromSNI: true}}, Fake: &FakeOptions{TTL: 9, Prefix: 20, AutoTTL: &AutoTTL{}}} + baseKey := base.Key() + got, err := FakeTTLCandidates(base, 2, 8) + if err != nil { + t.Fatal(err) + } + if len(got) != 7 { + t.Fatalf("%d candidates", len(got)) + } + again, _ := FakeTTLCandidates(base, 2, 8) + seen := map[string]bool{} + for i, o := range got { + ttl := 2 + i + if o.Fake == base.Fake || o.Fake.TTL != ttl || o.Fake.AutoTTL != nil || o.Fake.Prefix != 20 || len(o.TLSRec) != 1 { + t.Fatalf("candidate %d: %+v", i, o.Fake) + } + want := Options{TLSRec: base.TLSRec, Fake: &FakeOptions{TTL: ttl, Prefix: 20}} + if o.Key() != want.Key() || o.Key() != again[i].Key() { + t.Fatalf("candidate %d key %s, want %s", i, o.Key(), want.Key()) + } + if !strings.Contains(o.Key(), "fake=ttl:"+strconv.Itoa(ttl)+",") || strings.Contains(o.Key(), "auto:") { + t.Fatalf("candidate %d key %s", i, o.Key()) + } + if seen[o.Key()] { + t.Fatalf("duplicate key %s", o.Key()) + } + seen[o.Key()] = true + if SupportsFake() { + if err := o.Validate(); err != nil { + t.Fatalf("candidate %d: %v", i, err) + } + } + } + if base.Fake.TTL != 9 || base.Fake.AutoTTL == nil || base.Key() != baseKey { + t.Fatal("base was modified") + } + for _, r := range [][2]int{{0, 4}, {4, 256}, {5, 4}, {1, 65}, {-3, -1}} { + if _, err := FakeTTLCandidates(base, r[0], r[1]); err == nil || !strings.HasPrefix(err.Error(), "desync: ") { + t.Fatalf("range %v: %v", r, err) + } + } + if one, err := FakeTTLCandidates(base, 255, 255); err != nil || len(one) != 1 { + t.Fatalf("single TTL: %v %v", one, err) + } + if _, err := FakeTTLCandidates(Options{}, 2, 8); err == nil || !strings.HasPrefix(err.Error(), "desync: ") { + t.Fatalf("no fake: %v", err) + } +} + +func TestUDPTTLCandidatesAndKey(t *testing.T) { + const want = "desync/v1;udp=ttl:4,repeats:0,packets:0,payload:default" + if got := (UDPOptions{TTL: 4}).Key(); got != want { + t.Fatalf("key %s, want %s", got, want) + } + if (UDPOptions{TTL: 4, Counters: &UDPCounters{}}).Key() != want { + t.Fatal("counters change the key") + } + if (UDPOptions{TTL: 4, Payload: []byte{1}}).Key() == (UDPOptions{TTL: 4, Payload: []byte{2}}).Key() { + t.Fatal("payload not in the key") + } + base := UDPOptions{TTL: 9, Repeats: 2, Packets: 1, Payload: []byte("x")} + got, err := UDPTTLCandidates(base, 3, 6) + if err != nil || len(got) != 4 { + t.Fatalf("%d %v", len(got), err) + } + seen := map[string]bool{} + for i, o := range got { + w := base + w.TTL = 3 + i + if o.TTL != 3+i || o.Key() != w.Key() || seen[o.Key()] { + t.Fatalf("candidate %d: %+v %s", i, o, o.Key()) + } + seen[o.Key()] = true + if SupportsUDPFake() { + if err := o.Validate(); err != nil { + t.Fatal(err) + } + } + } + if _, err := UDPTTLCandidates(base, 9, 2); err == nil { + t.Fatal("inverted range accepted") + } +} From a6ab5ed6b3c6266acd6c1f14a6deea12ba6cbf5d Mon Sep 17 00:00:00 2001 From: Nemu-x Date: Thu, 29 Oct 2026 20:37:31 +0300 Subject: [PATCH 3/3] docs: document automatic TTL --- README.md | 34 ++++++++++++++++++++++++++++++++-- docs/connection-io.md | 17 +++++++++++++++++ docs/vnext.md | 10 ++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a7d46c9..577c75d 100644 --- a/README.md +++ b/README.md @@ -156,13 +156,43 @@ opts := desync.Options{ } ``` -- `TTL` must expire after the filter and before the server. It depends on the route; 4 is an example, not a default. +- `TTL` must expire after the filter and before the server. It depends on the route; 4 is an example, not a default. `AutoTTL` derives it per connection where that can be observed, `FakeTTLCandidates` covers the rest (see [automatic TTL](#automatic-ttl)). - `Prefix` is measured after record fragmentation. Negative values count from the end; `-1` leaves the last byte for a normal write. - `Payload` replaces the bytes of the first transmission; by default an embedded TLS template is used, padded or truncated to the prefix. - `Timeout` (default 3 s) bounds the native waits. Write deadlines on the wrapper bound normal writes; `Close` interrupts both. Windows sends the prefix with `TransmitFile` from a delete-on-close temporary file, then puts the original bytes in the file cache for the retransmission and restores the socket's hop limits; native resources are kept until the operation completes or is cancelled. Linux and Android `vmsplice` a shared page holding the fake bytes into a pipe and `splice` it to the socket, so the send queue references the page instead of a copy; once `SIOCOUTQNSD` reports the prefix transmitted, the page is overwritten with the original bytes, then the hop limit is restored. This needs a route whose device supports scatter-gather (loopback, Ethernet, Wi-Fi, tun and WireGuard do); otherwise the kernel copies the spliced bytes and the server receives the fake, which fails the handshake. An error after a transformed send is terminal for the connection. Fake can be combined with `TLSRec`, not with `Split`, `Disorder` or `OOB`. macOS reports it unsupported (`Capabilities.Unsupported["fake-tcp"]`). +### Automatic TTL + +```go +dialer := net.Dialer{Control: desync.ControlAutoTTL} // before connect; no-op where unobservable +raw, err := dialer.Dial("tcp", "[2001:db8::1]:443") +if err != nil { + return err +} +conn := desync.WrapConn(raw, desync.Options{ + Fake: &desync.FakeOptions{ + AutoTTL: &desync.AutoTTL{}, // Delta 1, Min 3, Max 20 + TTL: 4, // fallback where the path cannot be observed; 0: fail instead + }, +}) +// after the first Write: ttl, auto := conn.(*desync.Conn).FakeTTL() +``` + +- The client reads the TTL of the server's SYN-ACK, guesses its initial value (64, 128 or 255; `EstimateHops`) and so the number of routers, and sends the fake with `hops - Delta`, clamped to `Min`..`Max` (zapret's `--dpi-desync-autottl`). A result that does not stay below the hop count is rejected: the fallback `TTL` is used, or the write fails with `ErrAutoTTLUnobserved` before anything is sent. +- Only **Linux/Android over IPv6** can observe it without privileges (`IPV6_RECVHOPLIMIT`, enabled by `ControlAutoTTL` before connect). Linux IPv4, Windows and macOS keep no received TTL for a TCP socket; `PlatformCapabilities().AutoTTL` and `Unsupported["auto-ttl-ipv4"]`/`["auto-ttl-ipv6"]` say why. There `AutoTTL` without a fallback `TTL` fails `Validate` (Windows) or the write (`ErrAutoTTLUnsupported`, Linux IPv4). +- `ObserveHops(conn)` returns the observed TTL and hops without changing anything, for diagnostics or to set `UDPOptions.TTL` with `AutoTTL{}.TTL(hops)`. The UDP burst itself has no automatic TTL: it precedes the peer's first reply. +- Where nothing can be observed, probe a range instead. `FakeTTLCandidates(base, 2, 8)` (and `UDPTTLCandidates`) return one option set per TTL, each with a stable, distinct `Key()`; register them as controller candidates and let discovery keep the one that verifies: + +```go +candidates, err := desync.FakeTTLCandidates(desync.Options{Fake: &desync.FakeOptions{Prefix: -1}}, 2, 8) +// one strategy ID per candidate, e.g. "fake-ttl-" + strconv.Itoa(c.Fake.TTL); +// derive the controller's PolicyVersion from the candidates' Key() values +``` + +The estimate assumes that the return path is as long as the forward path and that the server uses a standard initial TTL; a CDN or middlebox answering with its own TTL, or an asymmetric route, makes it wrong. See [connection I/O](docs/connection-io.md#automatic-ttl). + ### Low-TTL UDP burst (Windows, Linux, macOS) ```go @@ -221,4 +251,4 @@ Fuzz targets, benchmarks and what has been verified are listed in [validation](d ## License -MIT, see [LICENSE](LICENSE). The default fake TLS template and the fake, disorder and out-of-band mechanisms come from ByeDPI (MIT); what was taken is listed in [third_party/NOTICE](third_party/NOTICE). +MIT, see [LICENSE](LICENSE). The default fake TLS template and the fake, disorder and out-of-band mechanisms come from ByeDPI (MIT), the automatic TTL estimate from zapret (MIT); what was taken is listed in [third_party/NOTICE](third_party/NOTICE). diff --git a/docs/connection-io.md b/docs/connection-io.md index e34092d..8654d4e 100644 --- a/docs/connection-io.md +++ b/docs/connection-io.md @@ -85,6 +85,23 @@ A loopback test verifies the property directly: a classic BPF socket filter on t Not implemented, and reported in `PlatformCapabilities().Unsupported["fake-tcp"]`. The fake needs a first transmission whose bytes differ from what the socket later retransmits. Linux gets that from splice, which queues page references; Windows from `TransmitFile`, which reads the file cache at transmission time. xnu has no `splice` or `vmsplice`, and its `sendfile` allocates mbufs (`alloc_sendpkt`) and reads the file into them (`fo_read`) before `pru_send` appends them to the socket buffer (`bsd/kern/uipc_syscalls.c`, `sendfile`): the socket buffer holds a copy, so changing the file afterwards never reaches a retransmission, and the server would receive the fake bytes. There is no `MSG_ZEROCOPY` either. Writing the fake and later the original through ordinary writes would add bytes to the stream instead of replacing them. +## Automatic TTL + +`FakeOptions.AutoTTL` needs the TTL (IPv6: hop limit) with which the server's packets reach the client, read from the client's own TCP socket before the ClientHello is sent, without raw sockets or privileges. The only packet received by then is the SYN-ACK. + +| OS | Received TTL on a TCP socket | Result | +| --- | --- | --- | +| Linux, Android, IPv6 | With `IPV6_RECVHOPLIMIT` set, `tcp_v6_do_rcv` clones each segment and at `ipv6_pktoptions` stores the hop limit of the last in-order one (the SYN-ACK, which completes `connect`) in `np->mcast_hops`; `getsockopt(IPV6_2292PKTOPTIONS)` returns it as an `IPV6_HOPLIMIT` control message (`net/ipv6/tcp_ipv6.c`, `net/ipv6/ipv6_sockglue.c`) | supported | +| Linux, Android, IPv4 (and IPv4-mapped) | `tcp_v4_do_rcv` records nothing. `IP_PKTOPTIONS` reports `inet->mc_ttl`, which only `tcp_v4_syn_recv_sock` (an accepted socket) sets from a received packet; `IP_RECVTTL` control data is delivered to datagram sockets only (`net/ipv4/ip_sockglue.c`, `net/ipv4/tcp_ipv4.c`). A test reads `1` where the server sent 120 | unsupported | +| macOS, iOS | `tcp_input` never calls `ip_savecontrol`/`ip6_savecontrol`, and `ip6_ctloutput` answers `IPV6_2292PKTOPTIONS` with empty data (xnu `bsd/netinet/tcp_input.c`, `bsd/netinet6/ip6_output.c`) | unsupported | +| Windows | Received-TTL control data exists only for `WSARecvMsg`, which, like `IP_RECVTTL`/`IPV6_RECVHOPLIMIT`, is limited to `SOCK_DGRAM`/`SOCK_RAW`; `TCP_INFO_v0/v1` (`SIO_TCP_INFO`) has no TTL field | unsupported | + +The option must be enabled before `connect` (`ControlAutoTTL` as `net.Dialer.Control`); otherwise the SYN-ACK is not recorded, nothing else arrives before the ClientHello, and `ObserveHops` reports `ErrAutoTTLUnobserved`. An unrecorded value reads as `IPV6_DEFAULT_MCASTHOPS` (1), which no estimate accepts. While the option is on, the kernel clones every received in-order segment, so the wrapper turns it off once it read the value. The value follows later in-order segments too: a test changes the server's hop limit after the handshake and reads the new one. A 32-bit process on a 64-bit kernel receives 64-bit control message headers here (`do_ipv6_getsockopt` does not set `MSG_CMSG_COMPAT`), so both layouts are parsed; linux/386 is tested on an amd64 kernel. + +`EstimateHops` follows zapret's `hop_count_guess`: a received TTL of 33..64 started at 64, 97..128 at 128 and 224..255 at 255 (paths shorter than 32 hops); anything else is not guessed. `AutoTTL.TTL` follows `autottl_eval` with a negative delta: `hops - Delta` clamped to `Min..Max`, rejected unless it stays below the hop count. A packet sent with TTL `t` is dropped by the `t`-th router, so `hops - 1` still expires before the server; the margin absorbs a return path one hop shorter than the forward path. The estimate is wrong when the path is asymmetric, when the server (or a load balancer or CDN edge in front of it) starts at a non-standard TTL, or when an on-path device answers the handshake itself. + +ByeDPI has no automatic TTL: it has `--ttl` (default 8) and `--auto` triggers that switch to another fixed group. zapret's `nfqws` reads the TTL of the first incoming packet through NFQUEUE, which needs root, and therefore also covers IPv4. Unprivileged alternatives that were not implemented: an ICMP echo (`IcmpSendEcho` on Windows; `SOCK_DGRAM`/`IPPROTO_ICMP` ping sockets on macOS, and on Linux where `net.ipv4.ping_group_range` allows them) reports the reply's TTL, but measures a different protocol that hosts and CDNs often filter or answer from elsewhere; a UDP probe needs a server that answers UDP; a traceroute through `IP_RECVERR` depends on ICMP time-exceeded replies from routers. Where nothing can be observed, `FakeTTLCandidates`/`UDPTTLCandidates` expand a TTL range into option sets with stable keys for discovery to probe. + ## UDP burst and ICMP errors The low-TTL datagrams can draw ICMP time-exceeded replies from the router where they expire. They must not fail the owned socket's `ReadFrom`: diff --git a/docs/vnext.md b/docs/vnext.md index 11224ff..d994a4a 100644 --- a/docs/vnext.md +++ b/docs/vnext.md @@ -35,6 +35,7 @@ application probe and real application tests; a successful TCP dial is insuffici | Platform ACK capabilities | `PlatformCapabilities`/`ProbeConn`; opt-in `Options.SplitWait` (sent/acked) read from the socket's send queue, bounded by Close, deadlines and a timeout; explicit errors where unsupported; Windows reported as counter-based, not reliable | | Bounded randomization | Opt-in `Options.Random` record/write cut ranges with count/minimum-piece bounds, optional reproducible seed (per-connection crypto/rand seed otherwise) and `Options.Key()` strategy identity; deterministic options unchanged | | Cross-platform methods | `Options.Disorder` and `Options.OOB` (ByeDPI disorder, oob, disoob) on Windows, Linux/Android and macOS; TCP fake on Linux/Android via vmsplice/splice; UDP low-TTL burst on Linux/Android and macOS; macOS TCP fake reported unsupported with the reason; `Capabilities.Disorder`, `OOB` and `Unsupported` | +| Automatic TTL | `FakeOptions.AutoTTL` (zapret-style delta/min/max) from the SYN-ACK hop limit on Linux/Android IPv6 (`ControlAutoTTL`, `ObserveHops`, `EstimateHops`), with an optional fixed fallback TTL; Linux IPv4, Windows and macOS reported unsupported with the reason; `FakeTTLCandidates`/`UDPTTLCandidates` and `UDPOptions.Key()` for range discovery elsewhere | | Examples and tests | Owned HTTPS application probe, fragmented/malformed TLS, concurrency, deadlines, IPv4/IPv6 and native Windows regressions | The controller is available as an API. It is **not yet connected to SlothClash or @@ -140,6 +141,15 @@ reported in `Capabilities.Unsupported`. Not yet done: macOS runtime evidence beyond CI, real-network effectiveness, and combining Fake with Split, Disorder or OOB. +Automatic TTL: implemented where an unprivileged TCP socket can see the +server's TTL, which is Linux/Android over IPv6 only (`IPV6_RECVHOPLIMIT` +recorded from the SYN-ACK; verified on loopback with a server hop limit and a +BPF drop filter, on amd64 and 386). Linux IPv4, Windows and macOS expose no +received TTL for TCP and are reported in `Capabilities.Unsupported`; there the +controller probes `FakeTTLCandidates` (for example 2..8). Not yet done: real +network evidence that the estimate places the fake between filter and server, +an unprivileged ICMP-echo estimate for IPv4, and desync-socks flags. + ### 3. First-class Mihomo outbound as a maintained patch - Add an optional `type: desync` adapter in the maintained Mihomo patch set,