Skip to content

Unify IPv4 send path on IP_HDRINCL (TTL in the IP header) (#12) - #97

Merged
lance0 merged 2 commits into
masterfrom
fix/ip-hdrincl-unified-ttl
Jun 27, 2026
Merged

lance0 merged 2 commits into
masterfrom
fix/ip-hdrincl-unified-ttl

Conversation

@lance0

@lance0 lance0 commented Jun 27, 2026

Copy link
Copy Markdown
Owner

Summary

Replaces the IPv4 probe send path with a single raw IP_HDRINCL socket that writes the TTL directly into a hand-built IP header, instead of setsockopt(IP_TTL) before an asynchronous sendto. This removes the stale-TTL race behind #12 (traces collapsing to one hop on fast macOS/BSD) by construction, uniformly across platforms — no per-probe sockets, no inter-probe delay for IPv4.

This is the clean end state for the IPv4 side of the #12 follow-ups: the TTL physically rides in the packet, so there is no socket-state window to lose regardless of CPU speed or OS.

What changed

New src/probe/rawip.rs:

  • internet_checksum (RFC 1071), build_ipv4_header, build_ipv4_packet, build_udp_datagram + udp_checksum_v4, and the IP_HDRINCL raw socket. The kernel does not compute transport checksums for HDRINCL packets, so UDP/TCP/ICMP checksums are built in.
  • One socket emits ICMP/UDP/TCP by setting the IP protocol byte.

The ip_len/ip_off byte-order trap (the part that needed care): every IPv4 field is network order except these two, which are host order on macOS/NetBSD and network order on Linux/FreeBSD ≥11 (FreeBSD flipped to network order in 11.0; pre-11 is EOL). The decision is a single documented const, and the encoding is a pure, unit-tested function so both branches are covered on one host.

src/trace/engine.rs: IPv4 dispatches to new run_{icmp,udp,tcp}_hdrincl functions + an IPv4 PMTUD helper. The existing run_* functions handle IPv6 unchanged (there is no IPv4-style IP_HDRINCL for IPv6; it keeps per-probe sockets). Existing transport builders are reused; DSCP maps to the IP ToS byte; PMTUD sets DF in the header — which also makes IPv4 PMTUD work on NetBSD (no more missing IP_DONTFRAG). Response correlation is unchanged (the receiver matches on quoted-packet fields, independent of how probes are sent).

Validation

  • Unit tests (10): Internet checksum against a known vector + self-verify, IPv4 header field layout, both byte-order branches, UDP datagram + checksum end-to-end, full-packet assembly, and an independent IPv4 parser round-trip.
  • Real-kernel runtime test: an #[ignore]d test crafts and sends IPv4 ICMP/UDP/TCP/DF packets through an IP_HDRINCL socket and asserts the kernel accepts them — which only succeeds if that kernel's ip_len/ip_off byte order is correct (BSD rejects a wrong order with EINVAL at send). It is wired into the Linux, macOS, and FreeBSD CI jobs under root, so each real kernel validates its own byte order. macOS exercises the same host-order branch NetBSD uses (transitive coverage). Verified locally on Linux under CAP_NET_RAW.

Scope / follow-ups (tracked in ROADMAP)

  • IPv6 still uses per-probe sockets on macOS and the shared-socket + delay path on FreeBSD/NetBSD. Making IPv6 hop-limit deterministic on FreeBSD/NetBSD (per-probe sockets or IPV6_HOPLIMIT ancillary data) is the remaining item.
  • The 500µs apply_rate_limit floor is now redundant for IPv4; it is retained until the IPv6 item lands, then it can be dropped.
  • IPv6 paths were intentionally left untouched here since they can't be runtime-tested in this environment.

lance0 added 2 commits June 26, 2026 22:42
Send every IPv4 probe through a single raw IP_HDRINCL socket with the TTL
written directly into a hand-built IP header, instead of setsockopt(IP_TTL)
before an asynchronous sendto. This removes the stale-TTL race that collapsed
traces to a single hop on macOS/BSD (issue #12) by construction, uniformly
across platforms, with no per-probe sockets and no inter-probe delay for IPv4.

New `probe::rawip` module:
- internet_checksum (RFC 1071), build_ipv4_header, build_ipv4_packet,
  build_udp_datagram + udp_checksum_v4 (the kernel does not compute transport
  checksums for HDRINCL packets), and the IP_HDRINCL raw socket.
- The byte-order-sensitive ip_len/ip_off fields are encoded via a pure,
  unit-tested function; the per-OS choice (host order on macOS/NetBSD, network
  order on Linux/FreeBSD >= 11) is a single const, documented with sources.
- One socket emits ICMP/UDP/TCP by varying the IP protocol byte.

engine.rs dispatches IPv4 to new run_{icmp,udp,tcp}_hdrincl functions and a
PMTUD helper; the existing functions handle IPv6 unchanged (no IPv4-style
IP_HDRINCL exists for IPv6, which keeps the per-probe-socket path). The
existing transport builders (ICMP message, TCP segment) are reused as the
payload; UDP now builds its own header/checksum. DSCP maps to the IP ToS byte;
PMTUD sets the DF bit in the header (this also makes IPv4 PMTUD work on NetBSD,
which lacks IP_DONTFRAG). Response correlation is unchanged — the receiver
matches on quoted-packet fields, independent of how probes are sent.

Validation:
- Unit tests for checksums, header layout, and both byte-order branches.
- A privileged runtime test crafts and sends ICMP/UDP/TCP/DF packets via
  IP_HDRINCL; it passes only if the kernel accepts the header byte order (BSD
  rejects a wrong order with EINVAL). Verified locally on Linux under
  CAP_NET_RAW, and wired into the Linux, macOS, and FreeBSD CI jobs under root
  so each real kernel validates its own byte order (macOS also covers NetBSD's
  host-order branch).
… test

- --source-ip now fails fast on the IPv4 HDRINCL path: the raw socket is bound
  to the configured source so a non-local/invalid address errors at bind
  instead of silently emitting spoofed-source packets with mismatched
  transport checksums. ipv4_src() returns Result and rejects an IPv6
  --source-ip against an IPv4 target.

- The IPv4 HDRINCL loops no longer incur the macOS/BSD 500us floor: TTL rides
  in the header, so there is no setsockopt(IP_TTL) race to mask. They use a
  new apply_rate_limit_explicit() that honors only --rate. The floor (in
  apply_rate_limit) now applies solely to the IPv6 shared-socket path on
  FreeBSD/NetBSD; comment/CHANGELOG/ROADMAP updated to match.

- Docs: IPv4 PMTUD now sets DF in the IP header (IP_HDRINCL), so the NetBSD
  "no IP_DONTFRAG" limitation no longer applies (README, docs/FEATURES).

- Added a Linux on-wire test: send an IPv4 ICMP echo via IP_HDRINCL to
  loopback, receive it on a raw ICMP socket, and verify the emitted TTL, ToS,
  protocol, source/dest, and both checksums on the wire (not just that the
  kernel accepted the byte order). Verified under CAP_NET_RAW in docker.
@lance0
lance0 merged commit c02fc0f into master Jun 27, 2026
5 checks passed
@lance0
lance0 deleted the fix/ip-hdrincl-unified-ttl branch June 27, 2026 03:13
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.

1 participant