Repository navigation
Unify IPv4 send path on IP_HDRINCL (TTL in the IP header) (#12) - #97
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the IPv4 probe send path with a single raw
IP_HDRINCLsocket that writes the TTL directly into a hand-built IP header, instead ofsetsockopt(IP_TTL)before an asynchronoussendto. 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 theIP_HDRINCLraw socket. The kernel does not compute transport checksums for HDRINCL packets, so UDP/TCP/ICMP checksums are built in.The
ip_len/ip_offbyte-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 newrun_{icmp,udp,tcp}_hdrinclfunctions + an IPv4 PMTUD helper. The existingrun_*functions handle IPv6 unchanged (there is no IPv4-styleIP_HDRINCLfor 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 missingIP_DONTFRAG). Response correlation is unchanged (the receiver matches on quoted-packet fields, independent of how probes are sent).Validation
#[ignore]d test crafts and sends IPv4 ICMP/UDP/TCP/DF packets through anIP_HDRINCLsocket and asserts the kernel accepts them — which only succeeds if that kernel'sip_len/ip_offbyte order is correct (BSD rejects a wrong order withEINVALat 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 underCAP_NET_RAW.Scope / follow-ups (tracked in ROADMAP)
IPV6_HOPLIMITancillary data) is the remaining item.apply_rate_limitfloor is now redundant for IPv4; it is retained until the IPv6 item lands, then it can be dropped.