Repository navigation
Conversation
vxzman
force-pushed
the
pr/freebsd-support
branch
from
October 4, 2026 15:44
4db9e75 to
1fa5127
Compare
nekohasekai
force-pushed
the
dev
branch
2 times, most recently
from
October 9, 2026 03:10
7539c98 to
5c2edb1
Compare
Share the darwin route-based monitor and auto-route range building logic with FreeBSD, which uses the same AF_ROUTE and FetchRIB primitives.
Add /dev/tun based NativeTun for FreeBSD: - Clone device setup via TUNSIFHEAD/TUNSIFMODE/TUNSIFPID ioctls - Set TUNSTRANSIENT (FreeBSD 15+) for lifecycle cleanup on close - IPv4/IPv6 address assignment with NODAD and link-local scope handling - Auto route via AF_ROUTE route messages in the default FIB - Loop prevention: a dedicated FIB (iproute2_table_index) holds a copy of the real default gateway, so outbound sockets bound to that FIB never match the capture routes - gVisor link endpoint with self-address reflection
vxzman
force-pushed
the
pr/freebsd-support
branch
from
October 10, 2026 05:50
1fa5127 to
d12856c
Compare
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
This PR adds native FreeBSD TUN device and routing support to
sing-tun.Key Design & Implementation Details:
Device Management (
tun_freebsd.go):/dev/tunclone device, discovers the assigned interface name viaTUNGIFNAME, and sets flags withTUNSIFHEAD(4-byte packet information header),TUNSIFMODE(IFF_BROADCAST|IFF_MULTICAST), andTUNSIFPID.TUNSTRANSIENTso the device is automatically destroyed upon closing the descriptor, surviving evenSIGKILL. On FreeBSD 12–14, best-effortSIOCIFDESTROYcleanup is performed onClose().SIOCSIFNAME), safely skipping rename if the clone device already matches the requested name (avoidingEEXIST).SIOCAIFADDRandSIOCAIFADDR_IN6withIN6_IFF_NODADflag and sets ND6 flags to suppress automatic link-local generation. Link-local IPv6 default gateways are embedded with interface scope.Loop Prevention via FIB Isolation (
FIB 2022):SO_BINDTODEVICE/fwmarkand macOS's scoped routing (IP_BOUND_IF). Instead, FreeBSD natively provides per-socket routing table selection viaSO_SETFIB.auto_route) are installed into default FIB 0 via AF_ROUTE routing sockets.route.FetchRIBand replicated into a dedicated FIB (iproute2_table_index, default 2022). Outbound dialer sockets in sing-box are marked withSO_SETFIB=2022, ensuring outbound traffic never loops back into the tun capture routes.gVisor Link Endpoint (
tun_freebsd_gvisor.go):LinkEndpointfor FreeBSD with self-address reflection (packets directed to the tun interface's own IP are reflected back).Network Monitoring (
monitor_route_bsd.go):AF_ROUTEandFetchRIBroute change monitor with FreeBSD under build tagdarwin || freebsd.Test Plan
go build ./...across:GOOS=freebsd GOARCH=amd64GOOS=freebsd GOARCH=arm64GOOS=linux GOARCH=amd64GOOS=darwin GOARCH=arm64go vet ./...passes without warnings.net.fibs > 1,net.add_addr_allfibs=1): verified zero loopback, CPU idle, successful traffic capture and outbound dialing.