DanielLavrushin 报告 b4 手动指定路由表可能覆盖其他服务的默认路由

DanielLavrushin 报告,b4 set 同时手动指定 routing.fwmark 与 routing.table 时,会跳过自动分配使用的外部占用检查;若该表已被其他服务使用,安装同 metric 的默认路由可能覆盖原路由。他称 Linux 3.16、4.9、7.0 的 IPv4 均发生替换;IPv6 在 3.16 上并列添加,在 4.9、7.0 上替换。#739 处理的是退出清理,不能解决此前安装时的覆盖,且该 PR 仍未合并。他列出检查占用并改分配、仅警告、使用独立 metric 三种待决方向;独立 metric 也可能让 b4 路由无法被选中,尚无已发布修复。

作者原文

Problem

When a set's routing.fwmark and routing.table are pinned to a table another service already uses, enabling the set overwrites that service's default route in the table.

  • routeResolveIDs returns pinned values before any check of the table (routing.go#L3370-L3371). A table b4 picks itself goes through routeTableTakenByOthers (routing.go#L3417, #L3435), which recognises an rt_tables name, another rule's lookup and another service's routes.
  • routeReplaceDefaultRoute adds b4's default with ip [-6] route replace default ... table T at the kernel's default metric, 0 for IPv4 and 1024 for IPv6 (routing.go#L2805). replace overwrites the default at that metric whatever its device.

#739 keeps the cleanup from deleting another service's default, but the install has already replaced it by then.

Measured

Table 137 holds another service's default via 10.8.0.1 dev d1 and default via fd00:8::1 dev d1. After b4's replace default dev d0 proto 155 in both families:

| Kernel | IPv4 | IPv6 |
| --- | --- | --- |
| 3.16 | replaced by b4's | b4's added next to it |
| 4.9 | replaced by b4's | replaced by b4's |
| 7.0 | replaced by b4's | replaced by b4's |

Decision needed

A pinned table is the user's explicit choice, so this needs a call on what b4 does with one that another service uses:

  1. Run the same routeTableTakenByOthers check on pinned tables, warn, and assign a table, as routeResolveIDs already does for a pinned mark it cannot use.
  2. Keep the pinned table and only warn.
  3. Install b4's default at a metric of its own, as the kill switch does with 4096, so it never replaces another default. Lookup order then depends on the other service's metric, so b4's route may never be used.

Found while reviewing #739.