sing-box icon indicating copy to clipboard operation
sing-box copied to clipboard

Linux auto_redirect: local tailscaled UDP enters TUN despite process_name bypass (1.14.0)

Open kinist opened this issue 2 months ago • 1 comments

Summary

On Debian 13, standalone tailscaled runs alongside sing-box 1.14.0. Tailscale's locally generated UDP traffic enters the sing-box TUN despite a process_name rule with action: bypass.

A temporary nftables rule restoring Tailscale's routing mark after sing-box's output processing restored direct connectivity.

Could you help determine whether this is a configuration issue, a process pre-match limitation, a conntrack interaction, or a bug?

This report contains configuration excerpts and observed packet traces. It is not yet a standalone minimal reproducer, and the exact root cause has not been established. IP addresses below are anonymized example addresses and cannot be used directly for reproduction; ports, lengths, interfaces and observed results are preserved.

Environment

  • Debian 13, Linux amd64.
  • sing-box and standalone tailscaled run as systemd services on the same host.
  • sing-box: 1.14.0
  • sing-box revision: 0b8995879f29a9b98ee027bc17b75e101445b238
  • Build environment: go1.26.7 linux/amd64, CGO enabled.
  • Tailscale: 1.102.3-t9329c3677-ga522f65e9
  • Tailscale provides subnet routing and an exit node; no upstream exit node is selected.
  • Physical interface: ens18; sing-box TUN: tun0.
  • Main default route points to a separate LAN gateway.
  • ss -uapne confirms tailscaled listens on UDP 41641 with socket fwmark:0x80000.

Relevant configuration

The following is an excerpt, not a complete runnable configuration:

{
  "inbounds": [
    {
      "type": "tun",
      "tag": "tun-in",
      "address": "172.19.0.1/30",
      "auto_route": true,
      "auto_redirect": true,
      "strict_route": true,
      "route_exclude_address": [
        "192.168.6.0/24",
        "100.64.0.0/10"
      ]
    }
  ],
  "route": {
    "rules": [
      {
        "process_name": [
          "tailscale",
          "tailscaled"
        ],
        "action": "bypass",
        "outbound": "direct-out"
      }
    ]
  }
}

In the actual configuration, the bypass rule is at index 3. The preceding three rules only match socks-in. direct-out is a direct outbound.

The full configuration passes:

sing-box check -C /etc/sing-box

Observed behavior

While running tailscale ping, packet captures show public peer discovery traffic entering tun0:

tun0 Out 10.0.0.3:41641 -> 198.51.100.20:32506 UDP length 124

STUN traffic shows the following path:

tun0  Out 10.0.0.3:41641 -> 203.0.113.30:3478 UDP length 40
ens18 Out 10.0.0.3:43460 -> 203.0.113.30:3478 UDP length 40
ens18 In  203.0.113.30:3478 -> 10.0.0.3:43460 UDP length 32
tun0  In  203.0.113.30:3478 -> 10.0.0.3:41641 UDP length 32

Conntrack entries for the original UDP 41641 flows have mark=8227 (0x2023). The re-originated UDP 43460 flow has mark=8228 (0x2024).

Restarting tailscaled alone did not resolve the issue. The new process's UDP probes still entered TUN. Conntrack was not flushed, so this does not rule out existing connection-state involvement.

Connectivity remained relayed through DERP, typically around 327 ms or higher, with some tests timing out.

Relevant routing and nftables behavior

Existing Tailscale policy rules include:

5210: fwmark 0x80000/0xff0000 lookup main
5230: fwmark 0x80000/0xff0000 lookup default
5250: fwmark 0x80000/0xff0000 unreachable
5270: lookup 52

Sing-box policy rules include:

9000: fwmark 0x2024 goto 9002
9001: fwmark 0x2023 lookup 2022
9002: nop

The generated sing-box chains show:

  • output_prematch at priority -149, with early returns for connection marks 0x2023 and 0x2024, followed by UDP queuing to NFQUEUE 100.
  • output_udp_icmp at priority -148, with bypass checks and a final action setting packet and connection marks to 0x2023.

An existing iptables-nft OUTPUT chain at priority -150 also saves the packet's nonzero 0xff0000 bits into the connection mark for new connections. Its PREROUTING counterpart restores those bits for related/established connections.

The exact packet verdict sequence and the ownership of these additional mangle rules have not yet been traced.

Tested workaround

The following independent nftables rule restores the Tailscale mark after sing-box's output UDP processing. 10.0.0.3 is a substituted example address.

table inet tailscale_bypass {
    chain output {
        type route hook output priority -147; policy accept;
        ip saddr 10.0.0.3 meta skuid 0 udp sport 41641 counter meta mark set 0x80000 ct mark set 0x80000
    }
}

Results:

  • Peer discovery and STUN packets leave ens18 with source port 41641.
  • Five consecutive pings establish direct connectivity at 5, 5, 5, 6, 5 ms.
  • After persistently loading the rule and restarting tailscaled, five further pings are direct at 4, 6, 5, 5, 20 ms.
  • A 10-second capture on tun0, filtered to the local address and UDP source port 41641, captures zero packets.
  • No upstream gateway/NAT configuration changes were made during these tests.

This demonstrates an effective workaround in this environment; it does not establish why the process-based bypass failed. The workaround only covers locally generated IPv4 UDP. Full VM reboot and phone exit-node application behavior have not yet been tested.

Questions

  1. Should process_name with action: bypass handle standalone tailscaled UDP sockets carrying SO_MARK=0x80000 in this setup?
  2. Is there a supported sing-box configuration that avoids the extra nftables rule?
  3. Which targeted debug logs or nftables traces would best distinguish process lookup failure from pre-match/conntrack behavior?

Related issue

I checked #4463, which concerns forwarded traffic and reports that locally originated traffic works there. This report concerns locally originated tailscaled UDP, so I have not assumed the same underlying cause.

kinist avatar Sep 07 '26 03:09 kinist

I traced this through the current testing sing-box dependency and I think the process_name lookup may actually be working correctly.

The deterministic problem appears to be what the nftables backend does after pre-match returns ActionBypass.

Current testing uses:

github.com/sagernet/sing-tun
v0.9.2-0.20260909144315-8e1fb85d5b15

and that revision still has the following behavior.

1. nftables NFQUEUE does not preserve foreign fwmark bits

startNFQueue() does:

var markMask uint32
if !r.useNFTables {
    markMask = r.effectiveMarkMask()
}

So on the nftables backend:

markMask = 0

Then nfqueueHandler.handlePacket() handles a successful bypass decision as:

case ActionBypass:
    h.setVerdict(attr, nfqueue.NfRepeat, h.outputMark)

with the default:

AutoRedirectOutputMark = 0x2024

Inside setVerdict() the original mark is only preserved when markMask != 0:

if mark != 0 {
    if h.markMask != 0 {
        var originalMark uint32
        if attr.Mark != nil {
            originalMark = *attr.Mark
        }

        mark = originalMark&^h.markMask | mark
    }

    err = h.nfq.SetVerdictWithOption(
        packetID,
        verdict,
        nfqueue.WithMark(mark),
    )
}

Therefore on nftables, assuming the packet enters NFQUEUE with the Tailscale socket mark:

0x80000

and JudgeFlow() correctly returns:

ActionBypass

the implementation changes it to:

0x2024

rather than preserving the Tailscale-owned bits.

So the route rule can correctly identify tailscaled, but the act of encoding the bypass verdict destroys the routing intent carried by Tailscale's SO_MARK.

That also explains why restoring:

meta mark = 0x80000
ct mark   = 0x80000

after the sing-box chains immediately makes the reported setup work.

2. The iptables backend already preserves foreign marks

This is the strongest indication that this is a backend-parity bug rather than unsupported coexistence.

The iptables implementation uses:

mask := r.effectiveMarkMask()

and matches marks as:

value/mask

It also updates them using masked operations:

MARK --set-xmark value/mask

and saves conntrack state with:

CONNMARK --save-mark
    --nfmask <mask>
    --ctmask <mask>

With the default sing-box marks:

0x2023
0x2024
0x2025
0x2026

effectiveMarkMask() is:

0x2027

Tailscale's:

0x80000

does not overlap that mask.

So the iptables model is effectively:

third-party bits | sing-box-owned state

while the nftables model currently treats the whole 32-bit mark as owned by sing-box.

3. Fixing only nfqueueHandler.markMask is not sufficient

If the nftables backend simply started using the same mask, the bypass result would become something like:

0x80000 | 0x2024
= 0x82024

which is desirable from an ownership point of view.

But current nftables rules contain exact comparisons such as:

meta mark == 0x2024
ct mark   == 0x2024

so 0x82024 would no longer be recognized as the sing-box output/bypass state.

nftablesCreateMark() also currently replaces the complete packet/conntrack mark with the input state rather than updating only sing-box-owned bits.

So I think the invariant needs to be fixed across the complete nftables path.

Conceptually, writes should be:

new_mark =
    (old_mark & ~sing_box_mark_mask)
    | sing_box_state

and matches should be:

(mark & sing_box_mark_mask)
    == sing_box_state

for both:

meta mark
ct mark

This should cover at least the NFQUEUE verdict path, output/prerouting pre-match checks, exclude checks, mark creation, and any mark-based reroute state.

The existing effectiveMarkMask() behavior used by the iptables backend seems like the natural ownership definition unless the mark layout itself is going to be redesigned.

Regression cases

  1. Start with a local UDP socket carrying SO_MARK=0x80000, return ActionBypass, and verify the Tailscale-owned bits remain 0x80000 after NFQUEUE/repeat while the sing-box-owned bits encode bypass.
  2. Verify the same preservation for conntrack mark.
  3. Verify Tailscale's fwmark 0x80000/0xff0000 policy rule still matches and the packet does not enter the TUN.
  4. Verify an initially unmarked UDP flow behaves exactly as current auto_redirect does.
  5. Verify nftables recognizes sing-box states when unrelated mark bits are also present.
  6. Verify iptables and nftables backends have equivalent foreign-fwmark preservation semantics.

For debugging, logging the following around NFQUEUE pre-match would also make cases like this much easier to diagnose:

protocol
source/destination
original skb mark
pre-match action
resulting skb mark

So I would currently classify this as:

nftables auto_redirect clobbers non-sing-box fwmark bits while transporting an ActionBypass result.

The process-name bypass itself does not appear sufficient to preserve a socket's pre-existing routing mark.

I do not see a clean config-only workaround for the reported case when tailscaled runs as root: exclude_uid would exclude every root-owned process, and changing the sing-box auto_redirect mark values does not solve mark ownership.

The reporter's post-chain nftables rule restoring 0x80000 is therefore a reasonable targeted workaround until the nftables backend preserves non-owned mark bits itself.

HeisLuka avatar Sep 09 '26 20:09 HeisLuka