Skip to content

TUN inbound: Support autoSystemRoutingTable and autoOutboundsInterface on FreeBSD as well - #6691

Merged
RPRX merged 1 commit into
XTLS:mainfrom
brookwko:freebsd-auto-route
Aug 27, 2026
Merged

RPRX merged 1 commit into
XTLS:mainfrom
brookwko:freebsd-auto-route

Conversation

@brookwko

Copy link
Copy Markdown
Contributor

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. A 0.0.0.0/0 / ::/0 entry 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.
  • setinterface marks each outbound socket with setsockopt(SO_SETFIB, 1) — the same per-socket table selection route(8) -fib uses.

This needs the boot tunable net.fibs>=2 (a routing table is a boot-time resource on FreeBSD). If it's requested without that, NewTun fails early with a message telling the user to add net.fibs=2 to /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 requiring net.fibs>=2 mirrors established FreeBSD tunnel practice (cf. wg-quick's tunnelfib).

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"], autoOutboundsInterface unset → "auto"):

  • The TUN comes up with the configured address; the eight split routes land in FIB 0 pointing at the TUN; the real default route is untouched.
  • A fetch to 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).
  • FIB 1 shows the mirrored connected + default routes.
  • A route-table change triggers [tun] update interface and re-syncs FIB 1.
  • On shutdown every route and the interface are removed and a restart succeeds (the interface is explicitly destroyed — the wireguard tun library leaves it behind on FreeBSD otherwise).
  • Package unit tests pass on FreeBSD.

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.
  • The BSD route-socket helpers are duplicated with tun_darwin.go per the existing per-platform file style; a shared darwin || freebsd file could dedup them later.
  • The escape FIB number is fixed at 1; a config knob could make it selectable.

Refs #5594, #6366, #5887.

…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.
@RPRX
RPRX requested a review from Fangliding August 27, 2026 17:37
@RPRX

RPRX commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

首先 #6624 (comment) ,虽然不知道这代码是啥写的但即使是 AI 写的也认了,反正特定系统

其次我大概了解一些 FreeBSD 和 OpenBSD 了,前者应用范围更广,后者主要应用于网关,怪不得前者有人 PR TUN、后者有人 PR TPROXY,我觉得小众系统相对于 Linux 的优势主要是挖洞的人更少吧,不过 AI 出来了也难说,但至少后者用在网关上感觉比 Linux 更放心

最后 #6624 (comment) ,先合并了,存在 bug 的话真正用它的人自会反馈

@RPRX RPRX changed the title TUN inbound: Support autoSystemRoutingTable and autoOutboundsInterface on FreeBSD TUN inbound: Support autoSystemRoutingTable and autoOutboundsInterface on FreeBSD as well Aug 27, 2026
@RPRX
RPRX merged commit c1958db into XTLS:main Aug 27, 2026
40 checks passed
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.
@brookwko
brookwko deleted the freebsd-auto-route branch August 28, 2026 04:58
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.

2 participants