Repository navigation
TUN inbound: Support autoSystemRoutingTable and autoOutboundsInterface on FreeBSD as well - #6691
Merged
Merged
Conversation
…face` on FreeBSD Implements the TUN inbound's two auto-route options on FreeBSD, the last platform where they were stubs (Windows in XTLS#5887, macOS/Linux in XTLS#6366), closing the gap left in XTLS#5594. - autoSystemRoutingTable installs the configured CIDRs into the TUN, splitting a default route into eight more-specific prefixes so the real default route survives for outbound-interface discovery (darwin scheme). - autoOutboundsInterface steers outbound sockets back onto the physical egress. FreeBSD has no per-socket interface bind, so Start() mirrors the physical default route into a dedicated FIB and each outbound socket is marked with setsockopt(SO_SETFIB), kept in sync by a routing-socket monitor. This needs the boot tunable net.fibs>=2; NewTun fails early with guidance otherwise, before the dialer controller is registered. - gateway assigns the point-to-point address pair (v4) plus a link-local v6 address to the TUN. System routes are interface routes, not gateway routes: tun(4) is a broadcast interface whose peer address doubles as the subnet broadcast, which the kernel refuses to route to (EACCES). Verified end-to-end on FreeBSD 14.4, dual stack: full-tunnel routing, the SO_SETFIB outbound escape (the proxy dial does not loop back into the TUN), route-change resync, and clean teardown + restart.
Member
|
首先 #6624 (comment) , 其次我大概了解一些 FreeBSD 和 OpenBSD 了,前者应用范围更广,后者主要应用于网关, 最后 #6624 (comment) ,先合并了,存在 bug 的话真正用它的人自会反馈 |
autoSystemRoutingTable and autoOutboundsInterface on FreeBSD as well
olivezane
added a commit
to olivezane/Xray-core-ai
that referenced
this pull request
Aug 28, 2026
Upstream XTLS#6691 shipped tun_freebsd.go against upstream's package-global updater var; this fork hands the InterfaceUpdater to each device via Tun.SetUpdater (as tun_linux/darwin/windows/android do). Add the updater field + SetUpdater and route the two bare references through t.updater.
RPRX
pushed a commit
that referenced
this pull request
Oct 12, 2026
RPRX
pushed a commit
that referenced
this pull request
Oct 12, 2026
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.
What
Implements the two auto-route options of the TUN inbound on FreeBSD, the last platform where they were still stubs. This closes the FreeBSD gap left open in #5594 after Windows (#5887) and macOS/Linux (#6366) were done.
autoSystemRoutingTable: installs the configured CIDRs as routes into the TUN. A0.0.0.0/0/::/0entry is split into eight more-specific prefixes (1.0.0.0/8 … 128.0.0.0/1, v6 mirrored) so the system's real default route survives for outbound-interface discovery — same scheme as the darwin implementation.autoOutboundsInterface("auto"or a fixed name): steers outbound sockets back onto the physical egress so they don't loop through the TUN.gateway: assigns the point-to-point address pair to the TUN (previously ignored on FreeBSD).The one design point worth reviewing: outbound escape via a second FIB
darwin/linux bind each outbound socket to the egress interface (
IP_BOUND_IF/SO_BINDTODEVICE). FreeBSD has no per-socket interface bind, so instead it uses a dedicated routing table (FIB):Start()mirrors the physical default route (and the connected prefix its gateway resolves through) into FIB 1, keeping it in sync via a routing-socket monitor.setinterfacemarks each outbound socket withsetsockopt(SO_SETFIB, 1)— the same per-socket table selectionroute(8) -fibuses.This needs the boot tunable
net.fibs>=2(a routing table is a boot-time resource on FreeBSD). If it's requested without that,NewTunfails early with a message telling the user to addnet.fibs=2to/boot/loader.conf— before any dialer controller is registered, so a failed start can't leave outbound sockets pointed at a table that was never set up. Reserving FIB 1 and requiringnet.fibs>=2mirrors established FreeBSD tunnel practice (cf. wg-quick'stunnelfib).System routes use interface routes rather than gateway routes: the tun(4) device is a broadcast interface whose point-to-point peer address doubles as the subnet broadcast, so routing to that gateway is refused with EACCES — interface routes sidestep it (what wg-quick does on FreeBSD).
Testing
Verified end-to-end on a FreeBSD 14.4-RELEASE VM (
net.fibs=2), full-tunnel config (autoSystemRoutingTable: ["0.0.0.0/0"],autoOutboundsInterfaceunset →"auto"):fetchto an arbitrary public IP from inside the guest travels through the TUN, out via the SO_SETFIB-escaped outbound, to the upstream server and back (proves the escape prevents the outbound from looping into the TUN).[tun] update interfaceand re-syncs FIB 1.GOOS=freebsd go build ./...,go vet, and the gofumpt check are all clean; the host test suite is unaffected.Not in scope (possible follow-ups)
sockopt.interface(transport-level) is still unimplemented on FreeBSD.darwin || freebsdfile could dedup them later.Refs #5594, #6366, #5887.