Skip to content

TUN inbound: Route each new UDP destination, keeping FullCone NAT within a route - #7346

Open
osypai wants to merge 1 commit into
XTLS:mainfrom
osypai:tun-udp-route-cone
Open

osypai wants to merge 1 commit into
XTLS:mainfrom
osypai:tun-udp-route-cone

Conversation

@osypai

@osypai osypai commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

The UDP FullCone table (#5509) is keyed by the source only, and only the first packet of a session is routed, so later destinations of that source reuse its outbound. As the TUN never sees a socket close, a port that the OS reuses before the session expires inherits the old route. For example, DNS on a port last used by a direct or blocked flow never reaches the DNS outbound, and a resolver that retries from the same port (mDNSResponder) keeps the stale session alive.

Now a new destination is routed once, with fake IPs judged by their domain and no DNS lookup made. It then joins its source's session for the same rule, or gets a session of its own. Destinations of one rule still share a session, so FullCone is kept within a rule, and a balancer picking another outbound doesn't split it. Routing runs off the goroutine that reads the tun. WireGuard and MASQUE servers are unchanged.

Checked on a real utun on macOS with the system resolver: DNS on a reused port is answered and destinations of one rule leave from one port, while on main the DNS goes nowhere and every destination leaves from the first one. A new destination costs about 2 µs more; established flows and throughput are unchanged.

AI disclosure: this PR was completed using Claude Opus 5.5.

…hin a route

The UDP FullCone table was keyed by the source only, and only the first
packet of a session was routed, so a port that the OS reused before the
session expired inherited the old route, e.g. DNS on a port last used by
a direct or blocked flow never reached the DNS outbound.

Sessions are now keyed by source and the matched routing rule. A new
destination is routed once, off the goroutine that reads the tun, and
joins its source's session for the same rule or gets its own.
@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

这个做法可以有,甚至可以推广到其它入站,就不需要大改路由模块做 UDP 逐包 dispatch 了 #7329 (comment) ,改路由也行

不过其它入站原先靠的是超时机制,TUN 这里原先没有超时机制的吗?

@osypai

osypai commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

这个做法可以有,甚至可以推广到其它入站,就不需要大改路由模块做 UDP 逐包 dispatch 了 #7329 (comment) ,改路由也行

不过其它入站原先靠的是超时机制,TUN 这里原先没有超时机制的吗?

Right, the TUN UDP table has no timeout of its own; a session only ends when its outbound goes idle.

A timeout has a limit here, though: it only removes a mapping that stops receiving packets. When the system resolver gets the port of a finished flow, its retries go to that stale mapping and keep it active. macOS mDNSResponder retries a stuck query from the same port at intervals shorter than the timeout, so the mapping never expires and the name stays unresolvable.

@RPRX

RPRX commented Nov 8, 2026

Copy link
Copy Markdown
Member

照你这么说的话 TPROXY 无人报告过问题(macOS 机器在局域网内),你去看下 inbound UDP worker 那里超时是几分钟

不过我还是赞同往这个方向改,不过如果路由被 API 改了,那么还是得改路由模块

@RPRX
RPRX requested review from LjhAUMEM and yiguodev November 8, 2026 08:14
@LjhAUMEM

LjhAUMEM commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

tun 看不了一点,不在计划里

@LjhAUMEM

LjhAUMEM commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

off-topic

这个极简 stack 是什么路边,setdealine 都不支持

// what is opened through the stack of Xray takes no deadline, it ends when it is closed

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

这个问题肯定得修,只是代码没人细看的话不太敢直接合

@Fangliding 有没有更好的写法建议

@Fangliding

Copy link
Copy Markdown
Member

我是不太喜欢这样侵入路由组件 目前比较流行的workaround似乎是对UDP dest 53单独执行非锥跟踪

@RPRX

RPRX commented Nov 11, 2026

Copy link
Copy Markdown
Member

我就是觉得这样做有点呃了,但是路由 UDP 逐包 dispatch 的话如果是进程路由就太酸爽了

@Fangliding

Copy link
Copy Markdown
Member

因为UDP DNS当年设置的ID位数太少导致系统解析器一个请求就开一个本地port 把port当DNS ID用来避免 导致容易导致本地端口用尽而重复撞车 这个情况几乎是唯一的
或者写一个非常micro的sniffer来检查是否是DNS数据包来决定要不要禁用全锥 但是DNS嗅探也是个魔丸 因为DNS格式太简单写的限制太小甚至有可能误判

@RPRXX
RPRXX force-pushed the main branch 2 times, most recently from 9ade89d to 5d17b87 Compare November 14, 2026 15:58
@RPRX

RPRX commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

通读了之前在 #7281 #7329 的讨论,想到了这个 PR,为了 #7329 (comment) 的目标,如果路由做 UDP 逐包 dispatch 的话

但是路由 UDP 逐包 dispatch 的话如果是进程路由就太酸爽了

process 需要有个缓存机制了,或者:

毕竟这个 PR 是侵入了路由组件且不通用,可以先给路由实现个轻量版逐包 dispatch,只有 UDP destination 不同时才重新路由

@Fangliding @Meo597

@Meo597

Meo597 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

#7281 给我干 emo 了得缓一缓
意思是要把它整体 revert 掉,挪到路由阶段呗?
那这样就很科学了

@RPRX

RPRX commented Nov 18, 2026

Copy link
Copy Markdown
Member

对,就是把这个 PR 的理念移到路由模块,这样的话所有入站就都有了,且之后兼容路由模块的 API 更新也好做

实现上需要给 Link 套一层 reader & writer:出站从入站 read 出不同目标时触发重新路由,出站往入站的 write 要加锁避免 race

@Meo597

Meo597 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

firewall 没办法简单地搬到路由阶段,除非改写 IP

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

firewall 没办法简单地搬到路由阶段,除非改写 IP

不是把 firewall 搬到路由阶段,就是目标不同时正常重新路由而已,我想了下 process 的缓存机制加不加都行

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

想了下,读的时候应该统一读然后 channel 传递,udpipe,写的时候加锁避免 concurrent write 就行

@RPRX

RPRX commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

@Meo597 也就是给 DispatchLink() 进来的套一层即可,你试试,再配合 #6110 (comment) 就所有入站都有了

@Meo597

Meo597 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

试了下,硬让 ai vibe 我 review 不了一点
因为这块我从没摸过

@RPRX

RPRX commented Nov 19, 2026

Copy link
Copy Markdown
Member

这东西加上结构体的 Interrupt() Close() 顶多一百多行代码,不需要 vibe

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.

5 participants