DanielLavrushin:b4 双栈 Telegram 路由应依据出口接口判断 IPv6
DanielLavrushin 记录 b4 的问题:双栈接口路由使用整台主机的全局 IPv6 探测,可能漏用只有 ULA 的 NAT66 隧道,也可能把 IPv6 请求送入仅支持 IPv4 的隧道。作者提出由出口接口地址、网关或出口 IP 判断能力并短期缓存;该问题单是待实现方案,不是修复发布。
作者原文@DanielLavrushinProblem
Behind an interface-routed Telegram set (
system.mtproto.upstream_set), b4's own Telegram dials pick their IP family indialFamilies(src/mtproto/dialfamily.go):
- a set that routes IPv4 only or IPv6 only keeps to that family;
- a set that routes both asks the host probe
config.HostCanReachIPv6: a global IPv6 address outside fc00::/7 on any interface, plus an IPv6 default route.The route's families are the queue switches (
buildRouteStateinsrc/tables/routing.go:ipv4: cfg.Queue.IPv4Enabled,ipv6: cfg.Queue.IPv6Enabled).routeEnsurePolicyRoutinginstalls the set's IPv6 default whenever queue IPv6 is on, whether or not the egress interface has an IPv6 address. Neither signal tells whether the set's tunnel carries IPv6, so the dual-stack case goes wrong both ways:
- A NAT66 tunnel on a router without a global IPv6 address. The tunnel has only a ULA, as with a VPN provider that hands out fc00::/7 addresses and NATs them. The host probe rejects ULA, so b4 dials Telegram over IPv4 only although the route carries IPv6. Telegram keeps working over IPv4; IPv6 goes unused.
- An IPv4-only tunnel on a router with a global IPv6 address on its WAN. The host probe is true, so b4's own data centre dials try the DC's IPv6 address first (
ResolveDCAllputs it first) and send it into the tunnel, where it cannot leave. Measured in a network namespace with b4's rule and route forms:
- WireGuard without an IPv6 peer:
EHOSTUNREACHafter about 1 s;- a tunnel that drops IPv6 silently: the full 3 s dial timeout.
Only a timeout starts the address cooldown (
recordDialFailure), so theEHOSTUNREACHcase repeats on every dial, and the IPv4 attempt waits the 500 ms race stagger behind it.Direction
For a dual-stack interface route, decide IPv6 from the egress interface itself: an IPv6 address on it (
routeGetIfaceAddr(iface, true), which accepts ULA), or an IPv6 egress gateway or egress IP.
- Publish the interface, or that capability, through
tables.SetRoute.- Carry it on
mtproto.TelegramRouteandtelegramPath.- Check it at dial time behind a short cache, because the link watcher does not see address changes.
Keep the single-family branches as they are: they keep a dial inside the set's route and kill switch.
Tests:
- a unit test in
src/mtprotowith the interface check hooked: a dual-stack route, the host probe true and false, an interface with and without an IPv6 address;- a
src/tablestest that the interface is published.Context
Raised in the Copilot review of #619 (https://github.com/DanielLavrushin/b4/pull/619#discussion_r4630991588). The suggestion there, returning the route's family flags, would extend case 2 to routers without a global address, so #619 does not take it. The one place case 1 made something unreachable is fixed in #619: the bridge's relay of a LAN client's own IPv6 destination now keeps only the route's family limits.