Skip to content

Fix macOS single-hop traces: per-probe send sockets for ICMP/UDP/TCP (#12) - #96

Merged
lance0 merged 4 commits into
masterfrom
fix/macos-ttl-per-probe-socket
Jun 27, 2026
Merged

lance0 merged 4 commits into
masterfrom
fix/macos-ttl-per-probe-socket

Conversation

@lance0

@lance0 lance0 commented Jun 26, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes #12 — on macOS, traces can collapse to a single hop (only the destination or gateway visible), intermittently and more often on fast Apple Silicon.

Root cause (confirmed by Apple DTS, thread 726398): sendto() is asynchronous. The kernel stamps each queued datagram with the socket's current IP_TTL when it drains from the send queue — not at the sendto() call. So a tight loop of setsockopt(IP_TTL=n); send on a shared socket can transmit every queued probe with the final TTL, collapsing the trace to one hop.

The previous mitigation was a fixed 500µs inter-probe delay. That only masks the race and is hardware-dependent. Reproduced on the wire on an M2 / macOS 26.5.1: outgoing ttl 13 repeated 13× then 14→30, with full correctness only returning at --rate 50 (~20ms spacing) — 40× the current default.

Fix

Each probe is now sent from a fresh socket on macOS (Apple's recommended approach over a timing delay). One socket carries exactly one datagram, so its TTL is always correct — deterministic regardless of CPU speed. The race is not ICMP-specific, so all three probe modes are covered:

  • ICMP (run_icmp): per-probe socket via build_send_socket. The shared socket is retained for the single PMTUD probe and Linux Echo Reply polling.
  • UDP (run_udp): per-probe socket via build_udp_send_socket, re-bound to the flow's source port so Paris/Dublin flow identification and NAT detection are preserved. SO_REUSEADDR (macOS-only) lets the same port re-bind rapidly.
  • TCP (run_tcp): per-probe raw socket via build_tcp_send_socket; the source port lives in the crafted SYN, so nothing flow-specific is lost.

Each path also does a macOS preflight that creates/binds the socket(s) once before the loop with ?, so a misconfiguration (bad --source-ip, interface, port) fails fast at startup instead of spinning silently — matching non-macOS behavior.

Other platforms are unchanged (single shared socket / per-flow Vec reused; no new socket options). The 500µs delay is kept as a margin and still covers the FreeBSD/NetBSD raw paths.

Correlation is unaffected: macOS already rewrites the ICMP identifier on DGRAM sockets, and the receiver matches on the probe sequence / payload-embedded identifier rather than the kernel identifier (correlate.rs payload fallback) — verified across both the Echo Reply and Time Exceeded match paths.

Verification

  • ✅ CI green on Linux, macOS, and FreeBSD: build, cargo fmt --check, clippy --all-targets -D warnings, and the test suite all pass — the macOS job compiles and lints the macOS-only send paths.
  • ⏳ Live on-device check pending. CI can't send real probes (no raw sockets/network), so the remaining step is a real trace on macOS showing a clean 1→N sweep at the default rate across -p icmp/udp/tcp (force the old behavior first by temporarily disabling the 500µs delay), ideally confirmed on the affected M2.

Known edge

If per-probe socket creation fails transiently mid-run (e.g. fd exhaustion) after the preflight passes, the loop logs and continues rather than aborting — the same degradation as a non-macOS mid-run send failure.

On macOS, sendto() is asynchronous: the kernel stamps each queued datagram
with the socket's current IP_TTL at drain time, not at the sendto() call. So
rapid per-probe setsockopt(IP_TTL) calls on a shared DGRAM socket can all be
transmitted with the final TTL, leaving only the destination/gateway visible.
Apple's DTS confirms this (developer.apple.com/forums/thread/726398) and
recommends a fresh socket per send over a timing delay.

The prior fix was a fixed 500us inter-probe delay, which only masks the race
and is insufficient on fast Apple Silicon — reproduced on the wire on an M2
(macOS 26.5.1): TTL 13 repeated 13x then 14..30, needing ~20ms spacing to
clear, far more than the 500us default.

run_icmp now builds a fresh send socket per probe on macOS via the new
build_send_socket() helper (which also re-applies interface and source-IP
binding). This is deterministic regardless of hardware speed. The shared
socket is retained for the single PMTUD probe and Linux Echo Reply polling.
Other platforms are unchanged.

Correlation is unaffected: macOS already rewrites the ICMP identifier on DGRAM
sockets, and the receiver matches on the probe sequence / payload-embedded
identifier rather than the kernel identifier (correlate.rs payload fallback).

The 500us delay is kept as a secondary safety margin and still covers the
FreeBSD/NetBSD raw-socket paths, which are not converted here.

Verification: macOS on-device confirmation still pending (the macOS send path
is cfg-gated off when building on Linux).
The async-sendto TTL race is not ICMP-specific: any shared socket that
mutates IP_TTL per probe is affected, since macOS applies the socket's
current TTL when each queued datagram drains. run_udp and run_tcp had the
same shared-socket + per-probe setsockopt(IP_TTL) pattern, so -p udp / -p tcp
traces could still collapse to a single hop on fast macOS hardware (and the
500us floor is bypassed entirely when --rate sets a shorter delay).

Both paths now send each probe from a fresh socket on macOS, via new
build_udp_send_socket() / build_tcp_send_socket() helpers (shared once on
other platforms, per-probe on macOS, so they stay type-checked everywhere):

- UDP: re-binds the same per-flow source port each probe. create_udp_dgram_
  socket_bound_full now sets SO_REUSEADDR before bind so rapid re-binding of
  that port doesn't fail with EADDRINUSE. Flow identification and NAT
  detection are preserved because the source port is unchanged.
- TCP: the source port lives in the crafted SYN, not a socket bind, so the
  per-probe raw socket just re-applies source-IP binding and DSCP.

Non-macOS behavior is unchanged (single shared socket / per-flow Vec reused).
The 500us delay remains as a margin and for the FreeBSD/NetBSD raw paths.

Verification: macOS on-device confirmation still pending (the macOS send
paths are cfg-gated off when building on Linux). Linux: fmt, clippy
-D warnings, 217 lib tests pass.
@lance0 lance0 changed the title Fix macOS single-hop traces: per-probe send socket (#12) Fix macOS single-hop traces: per-probe send sockets for ICMP/UDP/TCP (#12) Jun 26, 2026
@lance0
lance0 requested a review from Copilot June 26, 2026 23:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a macOS-specific traceroute correctness issue where rapid setsockopt(IP_TTL); sendto() loops can result in probes being transmitted with the wrong TTL (collapsing traces to a single hop). The fix makes macOS probe sending deterministic by using per-probe send sockets across ICMP/UDP/TCP, while keeping other platforms’ behavior unchanged.

Changes:

  • Add per-probe send socket creation on macOS for ICMP/UDP/TCP to eliminate TTL stamping races caused by async sendto().
  • Add macOS “preflight” socket creation/binding checks for UDP/TCP to fail fast on misconfiguration.
  • Update documentation/CHANGELOG to describe the macOS race and the per-probe socket mitigation.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/trace/engine.rs Introduces macOS per-probe sockets for ICMP/UDP/TCP send paths and adds macOS preflight validation.
src/probe/udp.rs Adds macOS-only SO_REUSEADDR to allow rapid re-binding of per-flow UDP source ports when using per-probe sockets.
CHANGELOG.md Documents the macOS single-hop trace fix and its rationale.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/trace/engine.rs Outdated
Comment on lines +82 to +85
/// macOS no longer depends on this delay for correctness: the ICMP path sends each
/// probe from a fresh socket (`build_send_socket`), which removes the race entirely.
/// The delay is retained as a small safety margin and still covers the FreeBSD/NetBSD
/// raw-socket send paths, which have not been converted to per-probe sockets.
Comment thread src/trace/engine.rs Outdated
Comment on lines +94 to +96
// Minimum inter-probe spacing so a queued datagram drains (and is stamped with
// its intended TTL) before the next send. This is a margin, not a guarantee;
// the deterministic fix on macOS is the per-probe socket in run_icmp.
Comment thread src/probe/udp.rs Resolved
On macOS, UDP and TCP create their send socket inside the probe loop (for the
per-probe TTL fix). A fatal misconfiguration — invalid --source-ip, address
family mismatch, port/interface bind failure — would then be logged and
`continue`d on every probe: an infinite silent spin with no sent counters in
headless/TUI runs, where non-macOS fails fast via `?`. (ICMP was already safe;
its shared socket is still created pre-loop.)

Add a macOS preflight that creates/binds the send socket(s) once before the
loop with `?` — per UDP flow and once for TCP — so configuration errors abort
at startup like other platforms. The loop still builds a fresh socket per
probe; its log+continue now only tolerates genuinely transient failures.

Also narrow the SO_REUSEADDR added for rapid per-probe re-binding to macOS
only; other platforms keep their previous UDP socket options unchanged.

Linux: fmt, clippy -D warnings, 217 lib tests pass.
@lance0
lance0 force-pushed the fix/macos-ttl-per-probe-socket branch from 60ccba7 to 91addd3 Compare June 27, 2026 01:43
- udp.rs: propagate set_reuse_address failure with `?` instead of ignoring
  it. macOS per-probe rebinding of the same flow source port depends on it,
  so a failure should fail fast at the preflight rather than surface as a
  loop-time EADDRINUSE. SO_REUSEADDR alone is sufficient (verified on
  macOS 26.5.1); SO_REUSEPORT is not needed since each socket is closed
  before the next bind.
- engine.rs: apply_rate_limit comments described the deterministic fix as
  ICMP-only; it now covers ICMP, UDP, and TCP.
- CHANGELOG: the on-wire macOS capture confirmed the root cause, not the
  fix — reworded to avoid overclaiming.
@lance0
lance0 merged commit 7e580a5 into master Jun 27, 2026
5 checks passed
@lance0
lance0 deleted the fix/macos-ttl-per-probe-socket branch June 27, 2026 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Only one hop is shown, and IP TTL is stuck at 1

2 participants