Files
tailscale/cmd
Brad Fitzpatrick 28ba1a1d19 wgengine, wgengine/magicsock, cmd/tailscaled: add experimental tswg data plane
With TS_DEBUG_TSWG=1 on Linux, if the tswg kernel module is loaded,
tailscaled now uses it as its WireGuard data plane instead of a TUN
device and wireguard-go, so that packets on direct paths are encrypted,
decrypted and forwarded entirely in the kernel. If the module isn't
loaded, tailscaled logs why and falls back to TUN as before.

tailscaled creates a tswg device named tailscale0 and hands the engine
and router a fake tun.Device that only reports that name, so the router
configures addresses and routes on it. The engine keeps wireguard-go
running with no peers, since its receive loops drive magicsock's socket
reads, and starts a kernel data plane (wgengine/kerneldp_linux.go) that:

  - shares magicsock's UDP sockets with the kernel, again after every
    rebind, so disco and WireGuard keep sharing one 4-tuple;
  - sets the device's fwmark to the Tailscale bypass mark;
  - reconciles the kernel's peers with magicsock's peers and the live
    per-peer config (allowed IPs, PSK), with roaming disabled;
  - points each peer at magicsock's best path when it's direct and
    trusted, and otherwise at a per-peer 127.0.0.0/8 address on a
    loopback bridge, which hands the kernel's packets to magicsock to
    send over DERP, a peer relay, or both DERP and a candidate path;
  - injects WireGuard packets magicsock receives over DERP or a peer
    relay into the kernel from the peer's bridge address;
  - feeds the kernel's per-peer traffic back to magicsock so it keeps
    heartbeating active direct paths, and reports kernel peer status.

magicsock gains the hooks for that: Options.KernelWGRecv,
SetKernelSocketHook, SendWireGuardFromKernel, KernelPeerPaths and
NoteKernelActivity.

In kernel mode the engine's packet filter, netstack and MagicDNS
interception don't apply, since packets don't pass through userspace;
the engine logs that. This is an experiment, not something to run for
real yet.

The code is behind a new tswg feature tag, so the minimal builds don't
include it.

TestTSWGTailscaledDirect and TestTSWGTailscaledDERP run tailscaled in
this mode on two gokrazy VMs, behind easy NATs (the kernel ends up
sending directly to the peer's NAT-traversed address) and behind hard
NATs (everything flows through the bridge and DERP).

Updates #426

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I66133ebf8a22582df4d396cb51859091d6f7c4ae
2026-11-09 15:47:19 +00:00
..
2026-07-10 14:26:11 -07:00
2026-07-10 14:26:11 -07:00
2026-07-10 14:26:11 -07:00