[generic 952] 创建网络命名空间会截断 conntrack 链式事件,fullcone/SFE/ECM 收不到 DESTROY(附补丁)

Open
#14,304 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, linux

Research direction

Start by comparing the 6.18 patch in the issue with target/linux/generic/hack-*/952-add-net-conntrack-events-support-multiple-registrant.patch and the corresponding conntrack netlink source. The issue reports successful patch application and compilation on Linux 6.6, 6.12, and 6.18; verify the correct patch variants and build configuration for this tree. Done means the per-netns notifier fix applies cleanly and preserves delivery to later conntrack event handlers when a namespace is created or a netlink send fails.

Written by the indexing model from the issue text.

Description

问题

target/linux/generic/hack-*/952-add-net-conntrack-events-support-multiple-registrant.patch 打开 CONFIG_NF_CONNTRACK_CHAIN_EVENTS 后,conntrack 事件改成用 atomic_notifier 链分发,但 ctnetlink 仍然只有一个静态的 ctnl_notifier(struct notifier_block)。上游的 ctnetlink_net_init() 会在每个网络命名空间里执行 nf_conntrack_register_notifier(net, &ctnl_notifier)。

notifier 链靠 nb->next 串起来。新建 netns 时,它的链是空的,把同一个块注册进去会把 ctnl_notifier.next 写成 NULL。这个块同时挂在 init_net 的链上,于是 init_net 链上排在 ctnetlink 后面的所有注册者(nft_fullcone、shortcut-fe、qca-nss-ecm 等)全部掉链,从此收不到任何 conntrack 事件,包括 IPCT_DESTROY。删除这个 netns 只会从它自己的链上注销,init_net 的链不会恢复,只能重启或重新加载这些模块。

另外,链模式下 ctnetlink_conntrack_event() 在 netlink 发送失败时仍然 return -ENOBUFS(6.18 约第 876、886 行)。-ENOBUFS 带 NOTIFY_STOP_MASK 位,链的遍历会停在 ctnetlink,后面的注册者同样收不到这次事件。而链的调用方(nf_conntrack_eventmask_report / nf_ct_deliver_cached_events 的链式版本)本来就不看返回值。

影响范围
  • package/network/services/fullconenat-nft/Makefile 强制 CONFIG_NF_CONNTRACK_CHAIN_EVENTS=y,firewall4 依赖 kmod-nft-fullcone,所以所有带 fw4 的固件都走链式事件,shortcut-fe 也会打开它。
  • 触发条件只是“创建一个网络命名空间”:ip netns add、unshare -n、Docker/LXC 容器,或任何会建 netns 的服务。x86 上跑 Docker 的用户几乎必然触发。
  • 后果:ECM/SFE 已加速的流在 conntrack 删除后仍留在硬件/快速路径里转发(脱离 conntrack,无 ct 的 NAT 映射会占用外部端口,回包可能被转给错误的内部主机);conntrack -D/conntrack -F 对已加速的流不再生效;nft_fullcone 收不到销毁事件,只能在下次用到映射时才回收。
  • 5.4/5.10/5.15/6.1/7.3 的 952 补丁结构相同,应该也有同样的问题(没有逐一核对上游源码)。
实测(IPQ9574,QSDK 12.5 内核 5.4,QCA 自带的等价链式事件补丁,同样是静态块)
  • 用一个只读诊断模块遍历 init_net 的 conntrack 事件链:链上只剩 ctnetlink,next=NULL,ECM 和 nft_fullcone 都已掉链。该设备之前建过网络命名空间。
  • 对一条已被 PPE 加速的连接执行 conntrack -D:ct 被删除,ECM 连接原样保留,流量继续走硬件。IPv4(NAT)和 IPv6 都一样。
  • 换上每个 netns 独立 notifier_block 的 ctnetlink 后:ip netns add / ip netns del 前后 init_net 链都保持完整(ECM、ctnetlink、fullcone 都在),新 netns 拿到自己的块;conntrack -D 后 ECM 立即删除对应连接。
补丁

新增 target/linux/generic/hack-*/954-net-conntrack-events-per-netns-ctnetlink-notifier.patch(放在 952 之后):

  1. 每个 netns 通过 net_generic(pernet ops 加 .id/.size)分配自己的 notifier_block,ctnetlink_net_init / ctnetlink_net_pre_exit 用它注册和注销。
  2. 链模式下回调包一层,始终返回 NOTIFY_DONE。

已验证:在 linux 6.6.162、6.12.118、6.18.64 上,952 + 954 都能零模糊(patch -F0)应用;打开 NF_CONNTRACK_EVENTS、NF_CONNTRACK_CHAIN_EVENTS、NF_CT_NETLINK=m 后 nf_conntrack_netlink.o 编译通过,没有新增警告。6.x 上只做了编译验证,运行验证是在上面那台 5.4 设备上做的(同样的改法)。三个版本的补丁内容相同,只有行号不同;下面是 6.18 版。

954-net-conntrack-events-per-netns-ctnetlink-notifier.patch(6.18)
Subject: [PATCH] netfilter: ctnetlink: per-netns notifier block for chain events

With CONFIG_NF_CONNTRACK_CHAIN_EVENTS (952 patch), ctnetlink registers one
static notifier_block on the conntrack event chain of every network
namespace. The chain links blocks through nb->next, so registering the same
block on a new (empty) netns chain sets its ->next to NULL, and every
notifier registered after ctnetlink on the init_net chain (nft_fullcone,
shortcut-fe, ECM, ...) silently stops receiving events, including
IPCT_DESTROY. Creating any netns (ip netns add, unshare -n, containers)
triggers it, and deleting the netns does not repair the init_net chain.

Give each netns its own notifier_block through net_generic.

Also make the chain callback always return NOTIFY_DONE: the chain caller
ignores the return value, and -ENOBUFS (returned when the netlink send
fails) has NOTIFY_STOP_MASK set, which stops the chain walk at ctnetlink
and hides the event from later notifiers.

---
 net/netfilter/nf_conntrack_netlink.c | 46 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 42 insertions(+), 4 deletions(-)

--- a/net/netfilter/nf_conntrack_netlink.c
+++ b/net/netfilter/nf_conntrack_netlink.c
@@ -742,7 +742,7 @@
 
 static int
 #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
-ctnetlink_conntrack_event(struct notifier_block *this, unsigned long events, void *ptr)
+__ctnetlink_conntrack_event(struct notifier_block *this, unsigned long events, void *ptr)
 #else
 ctnetlink_conntrack_event(unsigned int events, const struct nf_ct_event *item)
 #endif
@@ -3765,9 +3765,38 @@
 
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
 #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
-static struct notifier_block ctnl_notifier = {
-	.notifier_call = ctnetlink_conntrack_event
+/*
+ * The event chain links its notifier_blocks through nb->next, so one block
+ * must not be registered on the chains of several network namespaces:
+ * registering a shared static block on a new netns chain rewrites ->next
+ * and cuts every notifier after it (nft_fullcone, ECM, ...) off the
+ * init_net chain. Give each netns its own block.
+ *
+ * The chain caller ignores return values, and -ENOBUFS carries
+ * NOTIFY_STOP_MASK, which would stop the walk before later notifiers.
+ * Always return NOTIFY_DONE.
+ */
+static int ctnetlink_conntrack_event(struct notifier_block *this,
+				     unsigned long events, void *ptr)
+{
+	__ctnetlink_conntrack_event(this, events, ptr);
+	return NOTIFY_DONE;
+}
+
+#include <net/netns/generic.h>
+
+struct ctnetlink_net {
+	struct notifier_block ct_notifier;
 };
+
+static unsigned int ctnetlink_net_id __read_mostly;
+
+static struct notifier_block *ctnl_notifier_of(struct net *net)
+{
+	struct ctnetlink_net *cn = net_generic(net, ctnetlink_net_id);
+
+	return &cn->ct_notifier;
+}
 #else
 static struct nf_ct_event_notifier ctnl_notifier = {
 	.ct_event = ctnetlink_conntrack_event,
@@ -3865,8 +3894,13 @@
 static int __net_init ctnetlink_net_init(struct net *net)
 {
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
+#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
+	ctnl_notifier_of(net)->notifier_call = ctnetlink_conntrack_event;
+	nf_conntrack_register_notifier(net, ctnl_notifier_of(net));
+#else
 	nf_conntrack_register_notifier(net, &ctnl_notifier);
 #endif
+#endif
 	return 0;
 }
 
@@ -3874,7 +3908,7 @@
 {
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
 #ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
-	nf_conntrack_unregister_notifier(net,&ctnl_notifier);
+	nf_conntrack_unregister_notifier(net, ctnl_notifier_of(net));
 #else
 	nf_conntrack_unregister_notifier(net);
 #endif
@@ -3884,6 +3918,10 @@
 static struct pernet_operations ctnetlink_net_ops = {
 	.init		= ctnetlink_net_init,
 	.pre_exit	= ctnetlink_net_pre_exit,
+#if defined(CONFIG_NF_CONNTRACK_EVENTS) && defined(CONFIG_NF_CONNTRACK_CHAIN_EVENTS)
+	.id		= &ctnetlink_net_id,
+	.size		= sizeof(struct ctnetlink_net),
+#endif
 };
 
 static int __init ctnetlink_init(void)
顺带发现(未处理)
  • 6.x 的 952 把 ctnl_notifier 换成链式块后,原来的 .exp_event = ctnetlink_expect_event 没有了,编译时会有 ctnetlink_expect_event defined but not used 警告,也就是打开链式事件后 ctnetlink 不再发送 expectation 事件(conntrack -E expect 收不到)。
  • qca-nss-ecm 的 IPv4 路由事件处理会在任何 IPv4 路由增删时让全部 IPv4 连接 defunct(fib_table_insert 不区分路由表,策略路由表变化也会触发),这是 QCA 的原设计,只是提醒一下代价。
Dominant language
C
Stars
31.6k
Forks
19k
Avg merge
22h 16m
Merged PRs (30d)
8

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from coolsnowwolf/lede

All issues in coolsnowwolf/lede

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.