Skip to content

Loopback: 127/8, ::1 and own addresses stay inside the stack - #21

Closed
KoganTheDev wants to merge 13 commits into
feat/recoveryfrom
feat/loopback
Closed

KoganTheDev wants to merge 13 commits into
feat/recoveryfrom
feat/loopback

Conversation

@KoganTheDev

@KoganTheDev KoganTheDev commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

Item E, the last of the follow-up plan. A packet to 127.0.0.0/8 or to the stack's own address used to go out a real interface and ARP; a local application could not reach the stack through itself. Stacked on #20.

  • An IP-layer loopback path (no pseudo-interface, so interface iteration, DHCP, ARP and IPv6-on-the-first-interface are untouched): datagrams to 127/8, ::1 or an own v4/v6 address go into a queue bounded at 1024 datagrams and 4 MiB, drained by poll() before the channels (capped at half the frame budget; poll() returns false and ms_until_next_timer() is 0 while it holds anything). Delivery is deferred, never re-entrant.
  • Loopback MTU 65535 (MSS 65495 v4, 65475 v6); source address = the destination.
  • Martians (RFC 1122 3.2.1.3, Linux fib_validate_source): on a real interface, 127/8, ::1 and own-address sources are dropped and counted - this also closes the LAND-style self-loop an automated security review found.
  • ustack bind/connect/sendto accept loopback.
  • Two port bugs fixed: an ephemeral port could equal a local listener's port (found by the kernel differential: a self-connection), and a local connect could use its destination port as its source.
  • Per-packet cost: local-ness decided once per send; the receive check reads header octets; IPv6 own-address checks no longer allocate.

Tests

Every loopback test stack's real channel fails the test on any write, so a pass cannot come from traffic leaking out. v4 and v6 TCP, UDP, ping, queue bounds (count and bytes), martian/LAND drops; ustack sockets over 127.0.0.1, an own address and ::1; loopback cases in the ustack-vs-kernel differential (v4 and v6); fuzz seeds and loopback sends in the stateful harness.

make test (1318), asan, tsan, strict, SIM_SEEDS=2000 make sim, FUZZ_SECONDS=30 make fuzz, clang-format, clang-tidy, cppcheck. Bench: SimulatedBulkTransfer back within noise of the base (5.82 -> 5.84 ms), EchoBurst slightly faster.

🤖 Generated with Claude Code

KoganTheDev and others added 13 commits November 19, 2026 20:12
…e wire

A datagram to 127.0.0.0/8 or to one of the stack's own addresses is built
as for the wire and queued (bounded, 1024; overflow counts loopback_drops)
instead of routed, ARPed and transmitted. poll() delivers the queue before
the channels, capped at half the frame budget and only what was queued at
the start, so replies wait for the next call and no send re-enters a
receive path. ms_until_next_timer() is 0 while the queue is non-empty and
poll() returns false until it is empty.

Source address is the destination (Linux's rule), so a reply matches its
connection. MSS on a local route comes from LOOPBACK_MTU (65535). A 127/8
source or destination arriving on a real interface is dropped and counted
(RFC 1122 3.2.1.3), which also keeps forwarding away from loopback. The
IPv6 half is stubbed here and lands next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
_send_ipv6_packet queues a local destination instead of resolving a
neighbour; the source for a local destination is the destination itself
(so ::1 needs no configured address); a connection to a local peer needs
no route. _handle_ipv6 is split so the loopback queue reaches the same
address checks and transport demux as a frame from the wire, and ::1 as
source or destination on a real interface is dropped and counted
(RFC 4291 2.5.3). MSS on a local v6 route is LOOPBACK_MTU less 60.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A packet arriving on a real interface whose source is one of this stack's
own addresses (Linux's fib_validate_source rule) is dropped and counted,
v4 and v6. Accepted, its replies would be addressed to a local address
and land in the loopback queue, so a spoofed SYN with source == destination
and equal ports (LAND) made the stack talk to itself. The unspecified v6
source used by DAD and router solicitation is not an own address and still
passes. Also makes is_local_address() public for the socket layer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eal driver

bind() accepts any 127/8 address and ::1 (previously only the interface's
own address), and a UDP datagram to a local address is sized by the loopback
MTU rather than the interface's. Tests drive socket/bind/listen/connect/
accept/send/recv over 127.0.0.1, an own address and ::1 through the sim
driver on a channel that fails on any write, and a connect over the real
driver, which only completes because ms_until_next_timer() is 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found by the loopback differential: a ustack socket layer binds port 0 from
its own counter and connect() from the stack's, both starting at 49152, so a
connect to a local listener came from the listener's own port - source ==
destination, equal ports - and was a connection to itself. Harmless between
two hosts, fatal on loopback. The allocator now steps over listening ports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The same scenarios run with ustack talking to itself over 127.0.0.1 / ::1
on a channel that fails on any write, compared line by line with the
kernel's own loopback. Both stacks agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ADME section

The stateful harness now sends datagrams and one connection to 127.0.0.1
and ::1 whenever its application runs, and seven new seeds put loopback and
own-address sources and destinations on the wire (the martian paths, incl.
LAND). README gains a Loopback section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…receive

_send_tcp_segment, _send_udp_datagram and _send_icmp_message ask once and
hand the answer to _source_address_for and _send_ip_packet. The per-packet
receive check read IPv4Address temporaries, each a heap Bytes; it now works
on the header's octets. SimulatedBulkTransfer/0: 6.02 -> ~5.9 ms (base 5.82).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KoganTheDev

Copy link
Copy Markdown
Owner Author

Closed automatically when its base branch was deleted on merging #20. Continued unchanged in #22.

@KoganTheDev
KoganTheDev deleted the feat/loopback branch November 19, 2026 22:23
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