Skip to content

Test that detach reaches the endpoint as a nil dispatcher - #89

Open
arpitjain099 wants to merge 67 commits into
SagerNet:devfrom
arpitjain099:fix/detach-forwards-nil
Open

arpitjain099 wants to merge 67 commits into
SagerNet:devfrom
arpitjain099:fix/detach-forwards-nil

Conversation

@arpitjain099

@arpitjain099 arpitjain099 commented Sep 3, 2026 •

Copy link
Copy Markdown

The fix this started from landed upstream in be4ce8d, where Attach now forwards a literal nil, so the code change is gone and only the tests are left. #85 is still open and the reporter asked for these to be kept, since the detach-must-arrive-as-nil contract is easy to break again in a later Attach rewrite, and #90 touches that same function.

Two tests with a recording endpoint: Attach(nil) has to arrive as nil, and a real dispatcher still has to arrive wrapped. Taking the nil branch back out fails the first and leaves the second green. go test -tags with_gvisor . and gofmt are clean.

nekohasekai and others added 23 commits November 26, 2026 10:45
Marks were read and written only within the mask of the auto-redirect
marks, so a hijacked packet kept the bits of other programs, and their
policy routing rules with a higher priority took it away from the tun.
A foreign mark that equals one of the marks under the mask was also
taken as a verdict: connections marked by Mullvad were reset. With
nftables, the input and reset marks are written as whole values and
matched exactly again, and only the output mark, which hands the routing
decision back, keeps the other bits. The DNS hijack rules now set the
input mark as well.

The iptables backend still reads and writes every mark within the mask.
The backend is now selected in the constructor, as the routing rule of
the input mark is installed before auto-redirect starts.
Restoring the input mark from conntrack was limited to the original
direction, so that replies of hijacked flows which other routing rules
carried away are not sent into the tun. Replies of flows originated
from the tun were no longer routed back into it as a result. These
flows now have their own conntrack mark, the whole mark mask, which has
no meaning as a packet mark.
The headroom check of packets written back to the tun was lost in the
go stack rewrite, so buffers without enough front headroom for the IP
and UDP headers panicked in ExtendHeader. They are copied again.
Fragments were reassembled before the forward dispatcher saw them, so
a fragmented IPv6 datagram of a forwarded flow was answered with Packet
Too Big again after the client had fragmented it to the path MTU.
Fragments now pass the return path, local endpoints and the forward
dispatcher before local reassembly: the first fragment carries the
5-tuple, later fragments follow its decision and are held briefly while
the flow is judged, and fragment entries are removed once all ranges
are seen. ICMP errors quoting a first fragment are matched, and no ICMP
error is built for non-first IPv4 fragments.
Read frames, iovecs, message headers, GSO segments, UDP session
lookups, the single frame scratch and the transmit segments of a failed
write kept pointers into read slots and arena slabs after use, which
outlive the mapping once it is released. They are cleared once the
batch is handled.
The engine slept in its event loop while backing off from recoverable
read errors, stalling every timer, socket and control message for up
to a second. The device read event is now disabled and restored by a
wheel timer instead.
Writes failing with ENOMEM aborted connections like fatal errors, while
the same allocation failure reported as ENOBUFS dropped the frame. Both
drop the frame now.
The marks now default to bits 26 to 28 on Android as well, which netd
leaves free, so the separate Android defaults are gone; the TPROXY mark
is the input and output marks together within that window.

With strict_route on a single stack, prerouting_udp_icmp no longer marks
UDP and ICMP of the disabled family for output, except from excluded
interfaces. Stale redirect route rules carrying a mark are deleted by
that mark.
The iptables backend wrote and matched the input and reset packet marks
under the mark mask, and restored the input mark from conntrack with
--restore-mark, which keeps the other bits. On Linux, outside of netd,
these marks are now whole values as with nftables, so policy routing
rules of other programs with a higher priority no longer take hijacked
packets away from the tun.

The transparent listener given by VpnService also carries the
protectedFromVpn bit, so netd no longer routes its replies back into
the tun, and output-tproxy leaves replies alone.
Same as on main for nftables. With iptables, link-local, multicast and
broadcast destinations are excluded as well, by address and, outside of
Android, by address type. The unreachable chains of the disabled family
now let through traffic carrying the output mark or, on Android, the
protectedFromVpn bit, established and related flows, and ICMPv6
neighbor discovery and multicast listener messages.
Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@arpitjain099
arpitjain099 force-pushed the fix/detach-forwards-nil branch from 58afdae to 3768c12 Compare November 27, 2026 09:17
@arpitjain099 arpitjain099 changed the title Forward a nil dispatcher so detach reaches the endpoint Test that detach reaches the endpoint as a nil dispatcher Nov 27, 2026
@nekohasekai
nekohasekai force-pushed the dev branch 3 times, most recently from 8b7d7d5 to 95b95fd Compare December 6, 2026 01:24
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.

3 participants