Netfilter updates for net-next
Subsystems (7)
Pablo Neira Ayuso
maintainerv2 cover Netfilter updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next, mostly including flowtable fixes and improvements:
1) Do not offload flows to hardware from the packet path if they are already being torn down.
2) Fetch hardware stats when a flow is deleted, so the last stats sync with the conntrack entry is no longer missed. Update the mtk_ppe and airoha drivers to use this. From Lorenzo Bianconi and me.
3) Fix xt_time monthday computation for dates from 2040 on by using time64_to_tm() instead of the days_since_epoch[] table. This also shrinks the xt_time object by about 23%. From Fernando F. Mancera.
4) Do not offload multicast flows from nft_flow_offload, from Koral Ilgun.
5) Make the nf_hooks_lwtunnel sysctl read-only in non-init netns so unprivileged users cannot toggle the global static branch. From Pablo Neira Ayuso.
6) Purge the remaining flowtable entries directly when the flowtable is destroyed instead of going through the hardware offload workqueue, which may fail to allocate work.
7) Remove a no-op loop initializer in the nf_queue selftest to silence a clang -Wunused-value warning. From Sasha Levin.
8) Do not scrub the skb as crossing netns in the IPIP flowtable tx path, so the skb mark is preserved.
9) Do not let the DEV_PATH_TUN entry set h_source from the tunnel device's dev_addr. Before this patch, fast-path frames went out with part of the tunnel's local IP address as the Ethernet source MAC. This broke offloaded flows on DS-Lite over PPPoE. From Marek Majkowski.
10) Clear IPS_OFFLOAD_BIT only once, as part of the one-time teardown transition. Before this patch, a repeated teardown of an expired hardware flow could clear the bit owned by a replacement flow for the same conntrack entry. From João Silva.
11) Call skb_ensure_writable() in nfnetlink_queue before writing NFQA_L2HDR over the mac header. Before this patch, the write could corrupt cloned frames that bridge flooding sends to other ports. From Tamaki Yanagawa.
12) Add a UDP flowtable offload selftest for IPv4 and IPv6, with and without masquerade. From Jiri Peska.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-26-11-16
Thanks.
----------------------------------------------------------------
The following changes since commit 573af801552999954caa8347e4490b04cf4763f0:
Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net (2026-11-12 14:54:47 -0800)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-26-11-16
for you to fetch changes up to 03b7948d634a19df8b6a31c75055cf03194aaba2:
selftests: netfilter: add UDP flowtable offload test (2026-11-16 01:05:20 +0100)
----------------------------------------------------------------
netfilter pull request 26-11-16
----------------------------------------------------------------
Fernando Fernandez Mancera (1):
netfilter: xt_time: fix monthday computation for dates past 2039
Jiri Peska (1):
selftests: netfilter: add UDP flowtable offload test
João Silva (1):
netfilter: flowtable: do not clear the offload bit twice
Koral Ilgun (1):
netfilter: nft_flow_offload: do not offload multicast
Lorenzo Bianconi (1):
net: airoha: update stats on flow deletion
Marek Majkowski (1):
netfilter: flowtable: tunnel device poisons h_source of fast-path frames
Pablo Neira Ayuso (5):
netfilter: flowtable: do not hw offload flows in teardown state
netfilter: flowtable: retrieve stats on flow deletion
net: ethernet: mtk_ppe: update stats on flow deletion
netfilter: flowtable: purge entries immediately when destroying the flowtable
netfilter: flowtable: do not scrub as crossing netns
Sasha Levin (1):
selftests: netfilter: nf_queue: drop the no-op loop initializer
Tamaki Yanagawa (1):
netfilter: nfnetlink_queue: make skb writable before writing NFQA_L2HDR
drivers/net/ethernet/airoha/airoha_ppe.c | 31 +-- drivers/net/ethernet/mediatek/mtk_ppe_offload.c | 32 ++- include/net/netfilter/nf_flow_table.h | 3 +- net/netfilter/nf_flow_table_core.c | 35 ++- net/netfilter/nf_flow_table_ip.c | 4 +- net/netfilter/nf_flow_table_offload.c | 83 ++++--- net/netfilter/nf_flow_table_path.c | 7 +- net/netfilter/nfnetlink_queue.c | 7 +- net/netfilter/nft_flow_offload.c | 5 + net/netfilter/xt_time.c | 82 +------ tools/testing/selftests/net/netfilter/Makefile | 1 + tools/testing/selftests/net/netfilter/nf_queue.c | 2 +- .../selftests/net/netfilter/nft_flowtable_udp.sh | 238 +++++++++++++++++++++ 13 files changed, 390 insertions(+), 140 deletions(-) create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh
Pablo Neira Ayuso
maintainerPATCH v2 1/13 netfilter: flowtable: do not hw offload flows in teardown state
Sashiko reports that refreshing hw offload from packet path is racy since the flow might be already torn down.
Since commit 7c549fb7eecd ("netfilter: flowtable: generalize pending status bit"), GC skips flow entries that are still pending to be offloaded. Let's just check for the teardown status bit after the pending bit is set on to address this race.
--- net/netfilter/nf_flow_table_offload.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/nf_flow_table_offload.c +6 -0
@@ -1120,6 +1120,12 @@ void nf_flow_offload_refresh(struct nf_flowtable *flowtable,if (!offload)return ; +if (test_bit (NF_FLOW_TEARDOWN , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +kfree (offload); +return ; + } +flow_offload_queue_work (offload); }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Pablo Neira Ayuso
maintainerRe: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
Quoted 13 lines · On Mon, Nov 16, 2026 at 12:23:27AM +0000, [email protected] wrote:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Quoted 7 lines
- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
This is a v2 pull request for nf-next.
I am keeping back two patches that need more work from the previous v1.
Quoted 5 lines
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
[email protected]
AI reviewRe: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
In reply to Pablo Neira AyusoThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete.
- [Low] The commit message for "netfilter: flowtable: do not hw offload flows in teardown state" fixes a real race in…
--
netfilter: flowtable: do not hw offload flows in teardown state
This commit changes nf_flow_offload_refresh() to check NF_FLOW_TEARDOWN again after NF_FLOW_PENDING has been taken. If the flow is already being torn down, the FLOW_CLS_REPLACE work is dropped instead of queued. The fix depends on GC skipping flows that have NF_FLOW_PENDING set.
Quoted 7 lines
Sashiko reports that refreshing hw offload from packet path is racy since the flow might be already torn down.
Since commit
7c549fb7eecd("netfilter: flowtable: generalize pending status bit"), GC skips flow entries that are still pending to be offloaded. Let's just check for the teardown status bit after the pending bit is set on to address this race.
[Severity: Low]
Should this have a Fixes: tag? The Sashiko report is credited in the text, but there is no Reported-by: or Link: tag.
The fix only works because of this early return in nf_flow_offload_gc_step():
if (test_bit(NF_FLOW_PENDING, &flow->flags)) return;
That return was added by 7c549fb7eecd. A stable backport without that commit would leave the race open. Could the dependency be noted for backporters?
The message also doesn't say what goes wrong. A hardware rule gets reinstalled after GC's DESTROY, and the flow is then freed while that rule is still in hardware.
Other fixes in this series do have Fixes: tags, for example "netfilter: flowtable: purge entries immediately when destroying the flowtable" and "netfilter: flowtable: do not scrub as crossing netns".
Quoted 6 lines
net/netfilter/nf_flow_table_offload.c +0
-0@@ -1120,6 +1120,12 @@ void nf_flow_offload_refresh(struct nf_flowtable *flowtable,if (!offload)
net/netfilter/nf_flow_table_offload.c void nf_flow_offload_refresh(struct nf_flowtable *flowtable, · line 1130
Quoted 10 lines
net/netfilter/nf_flow_table_offload.c +6
-0return ; +if (test_bit (NF_FLOW_TEARDOWN , &flow->flags)) { +clear_bit (NF_FLOW_PENDING , &flow->flags); +kfree (offload); +return ; + } +flow_offload_queue_work (offload); }
[Severity: High]
Is NF_FLOW_TEARDOWN the only bit that needs checking here? NF_FLOW_CLOSING also leads to the hardware rule being removed. That bit is still checked only in the caller, before NF_FLOW_PENDING is taken:
net/netfilter/nf_flow_table_core.c:flow_offload_refresh() {
...
if (likely(!nf_flowtable_hw_offload(flow_table)) ||
test_bit(NF_FLOW_CLOSING, &flow->flags))
return;
if (test_bit(NF_FLOW_HW, &flow->flags))
nf_flow_offload_refresh(flow_table, flow);
}This is the same check-before-lock pattern the patch fixes for TEARDOWN. Can the following happen?
CPU A (packet path)flow_offload_refresh()
sees CLOSING == 0, HW == 1
stalls before test_and_set_bit(NF_FLOW_PENDING) in nf_flow_offload_work_alloc()
CPU B (packet path)nf_flow_state_check()
FIN/RST sets NF_FLOW_CLOSING
GCnf_flow_offload_gc_step()
PENDING clear, teardown false, CLOSING && HW && !HW_DYING nf_flow_offload_del()
takes PENDING, sets NF_FLOW_HW_DYING, queues FLOW_CLS_DESTROYflow_offload_work_handler()
flow_offload_work_del() removes the rule, sets NF_FLOW_HW_DEAD clears PENDING
CPU A resumesnf_flow_offload_work_alloc()
test_and_set_bit(NF_FLOW_PENDING) succeedsnf_flow_offload_refresh()
test_bit(NF_FLOW_TEARDOWN) is 0, queues FLOW_CLS_REPLACEflow_offload_work_add()
puts the rule back in hardware, with no HW_DYING or HW_DEAD check
The flow now has CLOSING|HW|HW_DYING|HW_DEAD set and a live hardware rule. GC never sends another DESTROY, because the CLOSING branch requires !HW_DYING. When the flow later expires, nf_flow_offload_gc_step() takes this path:
if (teardown) {
if (test_bit(NF_FLOW_HW, &flow->flags)) {
if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
nf_flow_offload_del(flow_table, flow);
else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
flow_offload_del(flow_table, flow);Does this leak the hardware rule? The flow and conntrack get freed, but the rule stays installed with a cookie that points at freed memory.
Destroying the flowtable doesn't clean it up either. nf_flow_table_do_purge(), added by the later "purge entries immediately when destroying the flowtable" commit, skips the hardware purge when NF_FLOW_HW_DEAD is set. No later patch in the series touches flow_offload_refresh() or nf_flow_offload_refresh().
A wider interleaving could also give a use-after-free. Suppose a later GC step reads PENDING == 0 just before CPU A's test_and_set_bit(). In that same step the flow becomes a teardown (expired, ct dying, dst check failure, or custom gc).
GC then sees the HW_DYING|HW_DEAD left over from the earlier DESTROY and calls flow_offload_del(). The flow and ct are freed through call_rcu() while the REPLACE work is still queued. flow_offload_work_handler() then dereferences offload->flow, offload->flow->ct and offload->flow->flags outside any RCU read section.
Would it work to also check NF_FLOW_HW_DYING (or NF_FLOW_CLOSING) here, next to TEARDOWN? HW_DYING is always set while PENDING is held, before the DESTROY work clears PENDING, so one check would cover both teardown paths.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Paolo Abeni
maintainerRe: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
Quoted 5 lines · On 11/17/26 01:22, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
Quoted 7 lines
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete.
I understand from discussion on v1 that the above is understood and will be addressed as follow-up. The same for the mid issues in patch 2/13 and 3/13. @Pablo can you please confirm the above?
Thanks!
Paolo
Pablo Neira Ayuso
maintainerRe: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
Quoted 12 lines · Paolo Abeni
Quoted 11 lines · On 11/17/26 01:22, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 0 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
Quoted 4 lines
Quoted 1 line
- [High] The fix is incomplete.
I understand from discussion on v1 that the above is understood and will be addressed as follow-up. The same for the mid issues in patch 2/13 and 3/13. @Pablo can you please confirm the above?
Confirmed. We'd prefer to address one thing at a time,
- The CLOSING state logic needs a review, 1/13 is focusing on the TEARDOWN state specifically.
- Regarding 2/13, more existing drivers need updates. Currently this update to retrieve stats when the flow entry is deleted is a no-op for nfp and mlx5 because these drivers are ignoring the stats object in the delete path. No harm, just those need to be updated. There is also a remark on different lastused type (u32 jiffies vs. u64 in drivers).
- The issue described in 3/13 in also a pre-existing issue, ie. conntrack -D or -F is also missing pending (non-accumulated) stats in hw.
I consider these are a pre-existing issues.
Re: [PATCH net-next 01/13] netfilter: flowtable: do not hw offload flows in teardown state
In reply to Pablo Neira AyusoThis series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Quoted 9 lines · On Mon, 16 Nov 2026 01:16:00 +0100 you wrote:
Sashiko reports that refreshing hw offload from packet path is racy since the flow might be already torn down.
Since commit
7c549fb7eecd("netfilter: flowtable: generalize pending status bit"), GC skips flow entries that are still pending to be offloaded. Let's just check for the teardown status bit after the pending bit is set on to address this race.[...]
Here is the summary with links:
- [net-next,01/13] netfilter: flowtable: do not hw offload flows in teardown state
https://git.kernel.org/netdev/net-next/c/0abb54363974 - [net-next,02/13] netfilter: flowtable: retrieve stats on flow deletion https://git.kernel.org/netdev/net-next/c/2785eca574b0 - [net-next,03/13] net: ethernet: mtk_ppe: update stats on flow deletion https://git.kernel.org/netdev/net-next/c/272435e43f1c - [net-next,04/13] net: airoha: update stats on flow deletion https://git.kernel.org/netdev/net-next/c/0602fe6ca35c - [net-next,05/13] netfilter: xt_time: fix monthday computation for dates past 2039 https://git.kernel.org/netdev/net-next/c/71d4a4199524 - [net-next,06/13] netfilter: nft_flow_offload: do not offload multicast https://git.kernel.org/netdev/net-next/c/b0ca1ef87626 - [net-next,07/13] netfilter: flowtable: purge entries immediately when destroying the flowtable https://git.kernel.org/netdev/net-next/c/1ba460c8e20b - [net-next,08/13] selftests: netfilter: nf_queue: drop the no-op loop initializer https://git.kernel.org/netdev/net-next/c/f1eaa9155e69 - [net-next,09/13] netfilter: flowtable: do not scrub as crossing netns https://git.kernel.org/netdev/net-next/c/4733af7f19e7 - [net-next,10/13] netfilter: flowtable: tunnel device poisons h_source of fast-path frames https://git.kernel.org/netdev/net-next/c/ada5902116b4 - [net-next,11/13] netfilter: flowtable: do not clear the offload bit twice https://git.kernel.org/netdev/net-next/c/8147f6e61e16 - [net-next,12/13] netfilter: nfnetlink_queue: make skb writable before writing NFQA_L2HDR https://git.kernel.org/netdev/net-next/c/3abad1970ab4 - [net-next,13/13] selftests: netfilter: add UDP flowtable offload test https://git.kernel.org/netdev/net-next/c/03b7948d634a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Pablo Neira Ayuso
maintainerPATCH v2 2/13 netfilter: flowtable: retrieve stats on flow deletion
In reply to Pablo Neira AyusoRetrieve stats too when deleting a flow from hardware. Apparently, the existing approach is missing last stats sync with the conntrack entry. Pass the stats object to the driver so it has a chance to populate it at deletion time. Drivers need to be updated to use this new approach.
--- net/netfilter/nf_flow_table_offload.c | 54 ++++++++++++++++----------- 1 file changed, 33 insertions(+), 21 deletions(-)
net/netfilter/nf_flow_table_offload.c +33 -21
@@ -928,12 +928,33 @@ static int nf_flow_offload_tuple(struct nf_flowtable *flowtable, }up_read (&flowtable->flow_block_lock); -if (cmd ==FLOW_CLS_STATS ) +if (stats)memcpy (stats, &cls_flow.stats,sizeof (*stats));return i; } +static void flow_offload_update_stats (struct flow_offload_work *offload, +struct flow_stats *stats) +{ +u64 lastused; + + lastused =max_t (u64, stats[0 ].lastused, stats[1 ].lastused); + offload->flow->timeout =max_t (u64, offload->flow->timeout, + lastused +flow_offload_get_timeout (offload->flow)); + +if (offload->flowtable->flags &NF_FLOWTABLE_COUNTER ) { +if (stats[0 ].pkts) +nf_ct_acct_add (offload->flow->ct, +FLOW_OFFLOAD_DIR_ORIGINAL , + stats[0 ].pkts, stats[0 ].bytes); +if (stats[1 ].pkts) +nf_ct_acct_add (offload->flow->ct, +FLOW_OFFLOAD_DIR_REPLY , + stats[1 ].pkts, stats[1 ].bytes); + } +} +static int flow_offload_tuple_add (struct flow_offload_work *offload,struct nf_flow_rule *flow_rule,enum flow_offload_tuple_dir dir)
@@ -946,11 +967,12 @@ static int flow_offload_tuple_add(struct flow_offload_work *offload, }static void flow_offload_tuple_del (struct flow_offload_work *offload, -enum flow_offload_tuple_dir dir) +enum flow_offload_tuple_dir dir, +struct flow_stats *stats) {nf_flow_offload_tuple (offload->flowtable, offload->flow,NULL , dir, offload->flowtable->priority, -FLOW_CLS_DESTROY ,NULL , +FLOW_CLS_DESTROY , stats, &offload->flowtable->flow_block.cb_list); }
@@ -991,10 +1013,15 @@ static void flow_offload_work_add(struct flow_offload_work *offload)static void flow_offload_work_del (struct flow_offload_work *offload) { +struct flow_stats stats[FLOW_OFFLOAD_DIR_MAX ] = {}; +clear_bit (IPS_HW_OFFLOAD_BIT , &offload->flow->ct->status); -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL ); +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL , &stats[0 ]);if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY ); +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY , &stats[1 ]); + +flow_offload_update_stats (offload, stats); +set_bit (NF_FLOW_HW_DEAD , &offload->flow->flags); }
@@ -1011,27 +1038,12 @@ static void flow_offload_tuple_stats(struct flow_offload_work *offload,static void flow_offload_work_stats (struct flow_offload_work *offload) {struct flow_stats stats[FLOW_OFFLOAD_DIR_MAX ] = {}; -u64 lastused;flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_ORIGINAL , &stats[0 ]);if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags))flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_REPLY , &stats[1 ]); - - lastused =max_t (u64, stats[0 ].lastused, stats[1 ].lastused); - offload->flow->timeout =max_t (u64, offload->flow->timeout, - lastused +flow_offload_get_timeout (offload->flow)); - -if (offload->flowtable->flags &NF_FLOWTABLE_COUNTER ) { -if (stats[0 ].pkts) -nf_ct_acct_add (offload->flow->ct, -FLOW_OFFLOAD_DIR_ORIGINAL , - stats[0 ].pkts, stats[0 ].bytes); -if (stats[1 ].pkts) -nf_ct_acct_add (offload->flow->ct, -FLOW_OFFLOAD_DIR_REPLY , - stats[1 ].pkts, stats[1 ].bytes); - } +flow_offload_update_stats (offload, stats); }static void flow_offload_work_handler (struct work_struct *work)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 02/13] netfilter: flowtable: retrieve stats on flow deletion
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] Destroy-time stats only work for drivers that fill f->stats on FLOW_CLS_DESTROY, and the series converts only two.
- [Medium] netfilter: flowtable: broken timeout update in flow_offload_update_stats()
--
netfilter: flowtable: retrieve stats on flow deletion
The flowtable offload code now passes a stats object to drivers with FLOW_CLS_DESTROY. The result goes through a new flow_offload_update_stats() helper, which the FLOW_CLS_STATS path also uses, so the last hardware counters can be synced to conntrack when a flow is deleted. Drivers have to be updated to fill in the stats at destroy time.
Quoted 4 lines
net/netfilter/nf_flow_table_offload.c +0
-0
[ ... ]
Quoted 3 lines
net/netfilter/nf_flow_table_offload.c +3
-0+static void flow_offload_update_stats (struct flow_offload_work *offload, +struct flow_stats *stats) +{
[ ... ]
Quoted 11 lines
net/netfilter/nf_flow_table_offload.c +11
-0+if (offload->flowtable->flags &NF_FLOWTABLE_COUNTER ) { +if (stats[0 ].pkts) +nf_ct_acct_add (offload->flow->ct, +FLOW_OFFLOAD_DIR_ORIGINAL , + stats[0 ].pkts, stats[0 ].bytes); +if (stats[1 ].pkts) +nf_ct_acct_add (offload->flow->ct, +FLOW_OFFLOAD_DIR_REPLY , + stats[1 ].pkts, stats[1 ].bytes); + } +}
[ ... ]
Quoted 5 lines
net/netfilter/nf_flow_table_offload.c +1
-0@@ -991,10 +1013,15 @@ static void flow_offload_work_add(struct flow_offload_work *offload)static void flow_offload_work_del (struct flow_offload_work *offload) { +struct flow_stats stats[FLOW_OFFLOAD_DIR_MAX ] = {};
net/netfilter/nf_flow_table_offload.c static void flow_offload_work_add(struct flow_offload_work *offload) · line 1024
Quoted 10 lines
net/netfilter/nf_flow_table_offload.c +6
-2+clear_bit (IPS_HW_OFFLOAD_BIT , &offload->flow->ct->status); -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL ); +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL , &stats[0 ]);if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY ); +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY , &stats[1 ]); + +flow_offload_update_stats (offload, stats); +
[Severity: Medium]
Moving the timeout update into a shared helper means it now also runs on FLOW_CLS_DESTROY. Is that safe?
flow->timeout is a u32 stamp that is compared against nf_flowtable_time_stamp, which is (u32)jiffies. flow_stats.lastused is a u64 that drivers fill from the full jiffies value. Once a 64-bit kernel has been up for about five minutes, jiffies is above 2^32. At that point lastused + flow_offload_get_timeout() always has upper bits set, so max_t(u64, ...) always picks it over the zero-extended flow->timeout. The result is then truncated back to u32. The hardware value always wins, even when it is older than what software has already recorded.
That u32/u64 mixing already exists in flow_offload_work_stats(), but on deletion the flow is not necessarily going away. nf_flow_offload_gc_step() calls nf_flow_offload_del() for NF_FLOW_CLOSING flows and keeps them in the software flowtable until flow->timeout expires. If software saw the FIN/RST and refreshed flow->timeout, the stale hardware lastused can now move flow->timeout backwards. The closing flow then expires early.
The opposite happens with drivers that don't fill cls_flow.stats on destroy, which today is every driver until the later patches land. stats[] stays zeroed and lastused is 0, so the helper computes max(flow->timeout, offload_timeout). Whenever the u32 flow->timeout has wrapped into [0, offload_timeout), it gets bumped to offload_timeout. That silently extends the software lifetime of the flow.
For NF_FLOW_TEARDOWN flows, flow_offload_teardown() has already run flow_offload_fixup_ct() before nf_flow_offload_del() is queued. So updating flow->timeout at deletion time doesn't reach conntrack anyway.
Does flow_offload_work_del() need to touch flow->timeout at all, or would syncing the packet and byte counters be enough there? If the timeout update has to stay, could it be skipped when lastused is 0? Could the comparison also be done on the truncated u32 values with a wrap-safe check, similar to how nf_flow_has_expired() compares flow->timeout, rather than max_t(u64, ...)?
[Severity: Medium]
This depends on each driver's FLOW_CLS_DESTROY handler filling in cls_flow.stats. If a driver doesn't touch it, stats[] stays zeroed. Because of the stats[i].pkts checks, flow_offload_update_stats() then skips nf_ct_acct_add().
Later patches in this series convert mtk_ppe and airoha. Are there other in-tree backends that still lose the final counter delta on deletion?
For mlx5 conntrack offload, mlx5_tc_ct_block_flow_offload() in drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c sends DESTROY straight to the del handler:
case FLOW_CLS_DESTROY: return mlx5_tc_ct_block_flow_offload_del(ft, f); case FLOW_CLS_STATS: return mlx5_tc_ct_block_flow_offload_stats(ft, f);
mlx5_tc_ct_block_flow_offload_del() removes the entry and drops the counter reference. It never does a final mlx5_fc_query_cached() / flow_stats_update(). The representor nft flowtable path mlx5e_rep_setup_ft_cb()->mlx5e_delete_flower() seems to behave the same way.
For nfp, nfp_fl_ct_offload_nft_flow() does:
case FLOW_CLS_DESTROY: ct_map_ent = rhashtable_lookup_fast(&zt->priv->ct_map_table, &flow->cookie, nfp_ct_map_params); return nfp_fl_ct_del_flow(ct_map_ent);
This frees the entry without the delta accounting that nfp_fl_ct_stats() does for FLOW_CLS_STATS.
act_ct always sets NF_FLOWTABLE_COUNTER in tcf_ct_flow_table_get(). On these drivers, could packets and bytes counted in hardware between the last FLOW_CLS_STATS poll and deletion still never reach the conntrack counters? That looks like the same missing last stats sync that the
Closes: tag refers to.
sfc doesn't look affected, since efx_tc_ct_stats() only reports lastused.
Could the new driver contract also be documented? The DESTROY-time values need to be deltas since the last FLOW_CLS_STATS, the same as the STATS paths report, not cumulative totals. Otherwise nf_ct_acct_add() counts some traffic twice. They also need to be read before the hardware counter or MIB slot is released.
Quoted 2 lines
net/netfilter/nf_flow_table_offload.c +0
-0set_bit (NF_FLOW_HW_DEAD , &offload->flow->flags); }
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 3/13 net: ethernet: mtk_ppe: update stats on flow deletion
In reply to Pablo Neira AyusoUpdate this driver to use the new approach to fetch stats on flow deletion, introduced by ("netfilter: flowtable: retrieve stats on flow deletion").
--- .../net/ethernet/mediatek/mtk_ppe_offload.c | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-)
drivers/net/ethernet/mediatek/mtk_ppe_offload.c +21 -11
@@ -526,6 +526,24 @@ mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f,return err; } +static void +mtk_flow_offload_update_stats (struct mtk_eth *eth,struct flow_cls_offload *f, +struct mtk_flow_entry *entry) +{ +struct mtk_foe_accounting diff; +u32 idle; + + idle =mtk_foe_entry_idle_time (eth->ppe[entry->ppe_index], entry); + f->stats.lastused = jiffies - idle *HZ ; + +if (entry->hash !=0xFFFF && +mtk_foe_entry_get_mib (eth->ppe[entry->ppe_index], entry->hash, + &diff)) { + f->stats.pkts += diff.packets; + f->stats.bytes += diff.bytes; + } +} +static int mtk_flow_offload_destroy (struct mtk_eth *eth,struct flow_cls_offload *f) {
@@ -536,6 +554,8 @@ mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)if (!entry)return -ENOENT ; +mtk_flow_offload_update_stats (eth, f, entry); +mtk_foe_entry_clear (eth->ppe[entry->ppe_index], entry);rhashtable_remove_fast (ð->flow_table, &entry->node, mtk_flow_ht_params);
@@ -550,23 +570,13 @@ static int mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f) { struct mtk_flow_entry *entry; -struct mtk_foe_accounting diff; -u32 idle; entry =rhashtable_lookup (ð->flow_table, &f->cookie, mtk_flow_ht_params);if (!entry)return -ENOENT ; - idle =mtk_foe_entry_idle_time (eth->ppe[entry->ppe_index], entry); - f->stats.lastused = jiffies - idle *HZ ; - -if (entry->hash !=0xFFFF && -mtk_foe_entry_get_mib (eth->ppe[entry->ppe_index], entry->hash, - &diff)) { - f->stats.pkts += diff.packets; - f->stats.bytes += diff.bytes; - } +mtk_flow_offload_update_stats (eth, f, entry);return 0 ; }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 03/13] net: ethernet: mtk_ppe: update stats on flow deletion
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] mtk_flow_offload_update_stats() (drivers/net/ethernet/mediatek/mtk_ppe_offload.c:539) uses…
- [Medium] mtk_flow_offload_destroy() (drivers/net/ethernet/mediatek/mtk_ppe_offload.c:557-559) takes the…
- [Medium] net: mtk_ppe: mtk_flow_offload_update_stats() reads MIB 0 on L2 flows
--
net: ethernet: mtk_ppe: update stats on flow deletion
This commit moves the stats collection out of mtk_flow_offload_stats() and into a new helper, mtk_flow_offload_update_stats(). The helper is also called from mtk_flow_offload_destroy(), so final counters are reported when a flow is deleted, following "netfilter: flowtable:
retrieve stats on flow deletion".
Quoted 11 lines
drivers/net/ethernet/mediatek/mtk_ppe_offload.c +3
-0@@ -526,6 +526,24 @@ mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f,return err; } +static void +mtk_flow_offload_update_stats (struct mtk_eth *eth,struct flow_cls_offload *f, +struct mtk_flow_entry *entry)
drivers/net/ethernet/mediatek/mtk_ppe_offload.c mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f, · line 541
Quoted 10 lines
drivers/net/ethernet/mediatek/mtk_ppe_offload.c +10
-0+{ +struct mtk_foe_accounting diff; +u32 idle; + + idle =mtk_foe_entry_idle_time (eth->ppe[entry->ppe_index], entry); + f->stats.lastused = jiffies - idle *HZ ; + +if (entry->hash !=0xFFFF && +mtk_foe_entry_get_mib (eth->ppe[entry->ppe_index], entry->hash, + &diff)) {
[Severity: Medium]
Does this hash check catch bridge flows?
mtk_flow_offload_replace() allocates the entry with kzalloc_obj(), so entry->hash starts at 0. For MTK_PPE_PKT_TYPE_BRIDGE, mtk_foe_entry_commit() returns via mtk_foe_entry_commit_l2() before entry->hash is set to 0xffff:
drivers/net/ethernet/mediatek/mtk_ppe.c:mtk_foe_entry_commit() {
...
if (type == MTK_PPE_PKT_TYPE_BRIDGE)
return mtk_foe_entry_commit_l2(ppe, entry);
hash = mtk_ppe_hash_entry(ppe->eth, &entry->data);
entry->hash = 0xffff;
...
}mtk_foe_entry_commit_l2() sets entry->type to MTK_FLOW_TYPE_L2 and adds the template to ppe->l2_flows, but leaves entry->hash alone. Hardware FOE slots only go to the MTK_FLOW_TYPE_L2_SUBFLOW entries on entry->l2_flows. mtk_flow_entry_update() and __mtk_foe_entry_clear() both check for MTK_FLOW_TYPE_L2 before using entry->hash, but this helper only checks entry->hash != 0xFFFF.
So for a bridge flow it looks like mtk_foe_entry_get_mib() runs with hash 0. Because mtk_ppe_start() enables MTK_PPE_MIB_CFG_RD_CLR, that read also clears hardware MIB counter 0. The bridge rule is credited with another flow's packets and bytes, and whatever L3/L4 flow is bound at FOE index 0 loses them from its next FLOW_CLS_STATS delta.
This already happens on FLOW_CLS_STATS today, but calling the helper from mtk_flow_offload_destroy() adds the same destructive read on every FLOW_CLS_DESTROY of a bridge flow, and those bogus counts end up in the final stats handed back to the caller.
Should the helper skip entry->type == MTK_FLOW_TYPE_L2, or should mtk_foe_entry_commit_l2() initialize entry->hash to 0xffff? If bridge flows are expected to report stats at all, would the counters of the subflows on entry->l2_flows need to be summed instead?
[Severity: Medium]
Does this hash check work for bridge (MTK_FLOW_TYPE_L2) base entries?
mtk_flow_offload_replace() allocates the entry with kzalloc_obj(), so hash starts at 0. For MTK_PPE_PKT_TYPE_BRIDGE, mtk_foe_entry_commit() returns before hash is set to 0xffff:
drivers/net/ethernet/mediatek/mtk_ppe.c:mtk_foe_entry_commit() {
...
if (type == MTK_PPE_PKT_TYPE_BRIDGE)
return mtk_foe_entry_commit_l2(ppe, entry);
hash = mtk_ppe_hash_entry(ppe->eth, &entry->data);
entry->hash = 0xffff;
...
}mtk_foe_entry_commit_l2() only sets entry->type. mtk_flow_entry_update() takes the goto out path for L2 entries. L2 masters are never placed on foe_flow[]. So the master's hash appears to stay at 0.
If so, mtk_foe_entry_get_mib(ppe, 0, &diff) runs for bridge flows. mtk_ppe_start() enables MTK_PPE_MIB_CFG_RD_CLR, so this read also clears hardware MIB slot 0.
With the new call in mtk_flow_offload_destroy(), every FLOW_CLS_DESTROY of a bridge flow now read-clears slot 0. Would an unrelated L3/L4 flow bound at FOE index 0 then lose those packets and bytes from its next FLOW_CLS_STATS delta, and undercount its conntrack counters?
The bridge flow's own subflow counters (the cur->hash entries on entry->l2_flows) also don't seem to be read before __mtk_foe_entry_clear() frees them.
L2 masters keeping hash == 0 predates this patch and already affects mtk_flow_offload_stats(). The destructive read on the destroy path is new, though. Could the helper skip entries with entry->type == MTK_FLOW_TYPE_L2? Alternatively, could mtk_foe_entry_commit_l2() set hash to 0xffff?
Quoted 9 lines
drivers/net/ethernet/mediatek/mtk_ppe_offload.c +5
-0+ f->stats.pkts += diff.packets; + f->stats.bytes += diff.bytes; + } +} +static int mtk_flow_offload_destroy (struct mtk_eth *eth,struct flow_cls_offload *f) {
drivers/net/ethernet/mediatek/mtk_ppe_offload.c mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f) · line 559
Quoted 6 lines
drivers/net/ethernet/mediatek/mtk_ppe_offload.c +2
-0@@ -536,6 +554,8 @@ mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)if (!entry)return -ENOENT ; +mtk_flow_offload_update_stats (eth, f, entry); +mtk_foe_entry_clear (eth->ppe[entry->ppe_index], entry);
[Severity: Medium]
Can traffic forwarded between the stats snapshot and the invalidation go missing from the final counters?
The read-clear happens in this chain:
mtk_flow_offload_update_stats()
-> mtk_foe_entry_get_mib()
-> mtk_mib_entry_read()
It runs while hwe->ib1 is still MTK_FOE_STATE_BIND, and it holds only mtk_flow_offload_mutex, not ppe_lock. The entry is invalidated later, in a separate critical section:
drivers/net/ethernet/mediatek/mtk_ppe.c:__mtk_foe_entry_clear() {
...
if (entry->hash != 0xffff) {
struct mtk_foe_entry *hwe = mtk_foe_get_entry(ppe, entry->hash);
hwe->ib1 &= ~MTK_FOE_IB1_STATE;
hwe->ib1 |= FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_INVALID);
dma_wmb();
mtk_ppe_cache_clear(ppe);
...
acct->packets = 0;
acct->bytes = 0;
}
entry->hash = 0xffff;
...
}The PPE counts packets it forwards in that window into the MIB slot, but they never reach f->stats. The flowtable core now passes f->stats to nf_ct_acct_add() as the final sync. This can happen whenever traffic is still flowing at deletion time (flowtable flush, conntrack -D, device teardown).
__mtk_foe_entry_clear() zeroes only the software acct_table copy and then drops the index, so the leftover hardware MIB counts are never drained. If the hardware doesn't reset the slot on rebind, could those counts be charged to the next flow bound at that index? That part predates this patch, and the patch makes it smaller.
Just moving the helper call after mtk_foe_entry_clear() won't work, because entry->hash is reset there. Could the code instead save the hash, invalidate and flush the entry, and then do the final read-clear on the saved index?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 4/13 net: airoha: update stats on flow deletion
In reply to Pablo Neira AyusoFrom: Lorenzo Bianconi <[email protected]>
Update this driver to use the new approach to fetch stats on flow deletion, introduced by ("netfilter: flowtable: retrieve stats on flow deletion").
--- drivers/net/ethernet/airoha/airoha_ppe.c | 31 +++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-)
drivers/net/ethernet/airoha/airoha_ppe.c +19 -12
@@ -1285,6 +1285,23 @@ static int airoha_ppe_flow_offload_replace(struct airoha_eth *eth,return err; } +static void airoha_ppe_flow_update_stats (struct airoha_eth *eth, +struct flow_cls_offload *f, +struct airoha_flow_table_entry *e) +{ +u32 idle =airoha_ppe_entry_idle_time (eth->ppe, e); + + f->stats.lastused = jiffies - idle *HZ ; +if (e->hash !=0xffff ) { +struct airoha_foe_stats64 stats = {}; + +airoha_ppe_foe_entry_get_stats (eth->ppe, e->hash, &stats); + f->stats.pkts += (stats.packets - e->stats.packets); + f->stats.bytes += (stats.bytes - e->stats.bytes); + e->stats = stats; + } +} +static int airoha_ppe_flow_offload_destroy (struct airoha_eth *eth,struct flow_cls_offload *f) {
@@ -1295,6 +1312,7 @@ static int airoha_ppe_flow_offload_destroy(struct airoha_eth *eth,if (!e)return -ENOENT ; +airoha_ppe_flow_update_stats (eth, f, e);airoha_ppe_foe_flow_remove_entry (eth->ppe, e);rhashtable_remove_fast (ð->flow_table, &e->node, airoha_flow_table_params);
@@ -1342,24 +1360,13 @@ static int airoha_ppe_flow_offload_stats(struct airoha_eth *eth, struct flow_cls_offload *f) {struct airoha_flow_table_entry *e; -u32 idle; e =rhashtable_lookup (ð->flow_table, &f->cookie, airoha_flow_table_params);if (!e)return -ENOENT ; - idle =airoha_ppe_entry_idle_time (eth->ppe, e); - f->stats.lastused = jiffies - idle *HZ ; - -if (e->hash !=0xffff ) { -struct airoha_foe_stats64 stats = {}; - -airoha_ppe_foe_entry_get_stats (eth->ppe, e->hash, &stats); - f->stats.pkts += (stats.packets - e->stats.packets); - f->stats.bytes += (stats.bytes - e->stats.bytes); - e->stats = stats; - } +airoha_ppe_flow_update_stats (eth, f, e);return 0 ; }
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 04/13] net: airoha: update stats on flow deletion
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] airoha_ppe_flow_offload_destroy() (drivers/net/ethernet/airoha/airoha_ppe.c:1315-1316) takes the 'final'…
--
net: airoha: update stats on flow deletion
This moves the per-flow stats collection into a new helper, airoha_ppe_flow_update_stats(). The helper is now also called from airoha_ppe_flow_offload_destroy(), so the flowtable gets the final counters when a flow is deleted, following ("netfilter: flowtable:
retrieve stats on flow deletion").
Quoted 4 lines
drivers/net/ethernet/airoha/airoha_ppe.c +0
-0
[ ... ]
drivers/net/ethernet/airoha/airoha_ppe.c static int airoha_ppe_flow_offload_destroy(struct airoha_eth *eth, · line 1316
Quoted 6 lines
drivers/net/ethernet/airoha/airoha_ppe.c +1
-0@@ -1295,6 +1312,7 @@ static int airoha_ppe_flow_offload_destroy(struct airoha_eth *eth,if (!e)return -ENOENT ; +airoha_ppe_flow_update_stats (eth, f, e);airoha_ppe_foe_flow_remove_entry (eth->ppe, e);
[Severity: Low]
Can packets counted by the hardware after this snapshot be lost from the final stats?
airoha_ppe_flow_update_stats() reads the counters through airoha_ppe_foe_entry_get_stats() (memcpy_fromio() of npu->stats[index] plus ppe->foe_stats[index]). It does this without ppe_lock, while the FOE entry is still in BIND state.
After that, airoha_ppe_foe_flow_remove_entry() takes ppe_lock and calls:
airoha_ppe_foe_remove_flow()
e->data.ib1 |= FIELD_PREP(AIROHA_FOE_IB1_BIND_STATE,
AIROHA_FOE_STATE_INVALID);
airoha_ppe_foe_commit_entry(ppe, &e->data, e->hash, false);Since rx_wlan is false, the commit goes through airoha_ppe_foe_flow_stats_update(). That function zeroes the counters for this hash:
airoha_ppe_foe_flow_stat_entry_reset(ppe, npu, index);
Between the snapshot and the invalidating commit, the PPE keeps forwarding and counting packets for this flow. That window includes:
- waiting for ppe_lock, which the NAPI path also takes
(airoha_qdma_rx_process()->airoha_ppe_check_skb()->
airoha_ppe_foe_insert_entry()) - the SRAM MMIO writes
- the ack poll
Packets counted in that window are then reset and never reach flow_offload_update_stats() / nf_ct_acct_add(). Flows torn down for idle timeout are not affected. Flows deleted while still carrying traffic (conntrack flush, route or device change, one-sided TCP teardown) would under-report.
Would it work to do this under ppe_lock instead? Save the hash, invalidate the entry without resetting the counters, read the stats, then reset them.
The mtk peer patch "net: ethernet: mtk_ppe: update stats on flow deletion" appears to use the same snapshot-then-clear order.
Quoted 2 lines
drivers/net/ethernet/airoha/airoha_ppe.c +0
-0rhashtable_remove_fast (ð->flow_table, &e->node, airoha_flow_table_params);
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 5/13 netfilter: xt_time: fix monthday computation for dates past 2039
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
iocaltime_3() derives the year from days_since_epoch[], which only has entries up to 2039. For any date from 2040-01-01 on, the lookup stays on the 2039 entry, so the day-of-year offset is 365 or more. That value is used to compute r->monthday, which ends up larger than 31 (32 on 2040-01-01, and it wraps in the u8 later in the year).
Use time64_to_tm() instead of the table walk. It is always built in, handles any time64_t, and always returns tm_mday in 1-31, so the table and its 2039 limit go away. The result for 1970-2039 is unchanged.
Only the real-time clock reaches localtime_3(), so this is not triggerable before the clock passes 2040-01-01 UTC.
This significantly reduces the size of xt_time object.
add/remove: 7/10 grow/shrink: 0/1 up/down: 143/-490 (-347)
Total: Before=1493, After=1146, chg -23.24%
--- net/netfilter/xt_time.c | 82 +++-------------------------------------- 1 file changed, 6 insertions(+), 76 deletions(-)
net/netfilter/xt_time.c +6 -76
@@ -13,8 +13,8 @@#include <linux/ktime.h> #include <linux/module.h> -#include <linux/rtc.h> #include <linux/skbuff.h> +#include <linux/time.h> #include <linux/types.h> #include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_time.h>
@@ -31,38 +31,7 @@ struct xtm {extern struct timezone sys_tz;/* ouch */ -static const u_int16_t days_since_year[] = { -0 ,31 ,59 ,90 ,120 ,151 ,181 ,212 ,243 ,273 ,304 ,334 , -}; - -static const u_int16_t days_since_leapyear[] = { -0 ,31 ,60 ,91 ,121 ,152 ,182 ,213 ,244 ,274 ,305 ,335 , -}; - -/* -* Since time progresses forward, it is best to organize this array in reverse, -* to minimize lookup time. -*/ -enum { -DSE_FIRST =2039 , -SECONDS_PER_DAY =86400 , -}; -static const u_int16_t days_since_epoch[] = { -/* 2039 - 2030 */ -25202 ,24837 ,24472 ,24106 ,23741 ,23376 ,23011 ,22645 ,22280 ,21915 , -/* 2029 - 2020 */ -21550 ,21184 ,20819 ,20454 ,20089 ,19723 ,19358 ,18993 ,18628 ,18262 , -/* 2019 - 2010 */ -17897 ,17532 ,17167 ,16801 ,16436 ,16071 ,15706 ,15340 ,14975 ,14610 , -/* 2009 - 2000 */ -14245 ,13879 ,13514 ,13149 ,12784 ,12418 ,12053 ,11688 ,11323 ,10957 , -/* 1999 - 1990 */ -10592 ,10227 ,9862 ,9496 ,9131 ,8766 ,8401 ,8035 ,7670 ,7305 , -/* 1989 - 1980 */ -6940 ,6574 ,6209 ,5844 ,5479 ,5113 ,4748 ,4383 ,4018 ,3652 , -/* 1979 - 1970 */ -3287 ,2922 ,2557 ,2191 ,1826 ,1461 ,1096 ,730 ,365 ,0 , -}; +#define SECONDS_PER_DAY 86400/* * Each network packet has a (nano)seconds-since-the-epoch (SSTE) timestamp.
@@ -102,51 +71,12 @@ static inline void localtime_2(struct xtm *r, time64_t time)static void localtime_3 (struct xtm *r,time64_t time) { -unsigned int year, i, w = r->dse; - -/* -* In each year, a certain number of days-since-the-epoch have passed. -* Find the year that is closest to said days. -* -* Consider, for example, w=21612 (2029-03-04). Loop will abort on -* dse[i] <= w, which happens when dse[i] == 21550. This implies -* year == 2009. w will then be 62. -*/ -for (i =0 , year =DSE_FIRST ; days_since_epoch[i] > w; - ++i, --year) -/* just loop */ ; - - w -= days_since_epoch[i]; +struct tm tm; -/* -* By now we have the current year, and the day of the year. -* r->yearday = w; -* -* On to finding the month (like above). In each month, a certain -* number of days-since-New Year have passed, and find the closest -* one. -* -* Consider w=62 (in a non-leap year). Loop will abort on -* dsy[i] < w, which happens when dsy[i] == 31+28 (i == 2). -* Concludes i == 2, i.e. 3rd month => March. -* -* (A different approach to use would be to subtract a monthlength -* from w repeatedly while counting.) -*/ -if (is_leap_year (year)) { -/* use days_since_leapyear[] in a leap year */ -for (i =ARRAY_SIZE (days_since_leapyear) -1 ; - i >0 && days_since_leapyear[i] > w; --i) -/* just loop */ ; - r->monthday = w - days_since_leapyear[i] +1 ; - }else { -for (i =ARRAY_SIZE (days_since_year) -1 ; - i >0 && days_since_year[i] > w; --i) -/* just loop */ ; - r->monthday = w - days_since_year[i] +1 ; - } +time64_to_tm (time,0 , &tm); - r->month = i +1 ; + r->monthday = tm.tm_mday;/* 1-31 */ + r->month = tm.tm_mon +1 ;/* 1-12 */ } static bool
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 05/13] netfilter: xt_time: fix monthday computation for dates past 2039
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The patch fixes a real, user-visible correctness bug: -m time --monthdays rules give wrong results once the clock reaches…
- [Low] The commit message has three inaccuracies, and it understates the impact.
- [Low] After the patch, some comments and state in net/netfilter/xt_time.c still describe the removed table-based date…
--
netfilter: xt_time: fix monthday computation for dates past 2039
This patch replaces the days_since_epoch[] table walk in localtime_3() with time64_to_tm(). This keeps r->monthday and r->month in range for dates from 2040-01-01 on. The lookup tables and the 2039 limit are removed, which also shrinks the xt_time object.
Quoted 5 lines
iocaltime_3()derives the year from days_since_epoch[], which only has entries up to 2039. For any date from 2040-01-01 on, the lookup stays on the 2039 entry, so the day-of-year offset is 365 or more. That value is used to computer->monthday, which ends up larger than 31 (32 on 2040-01-01, and it wraps in the u8 later in the year).
[ ... ]
Quoted 2 lines
Only the real-time clock reaches
localtime_3(), so this is not triggerable before the clock passes 2040-01-01 UTC.
[Severity: Low]
This isn't a bug, but there's a typo: iocaltime_3() should be localtime_3().
Is the "2040-01-01 UTC" part accurate when XT_TIME_LOCAL_TZ is set? time_mt() adjusts the stamp before it calls localtime_3():
if (info->flags & XT_TIME_LOCAL_TZ)
/* Adjust for local timezone */
stamp -= 60 * sys_tz.tz_minuteswest;For timezones east of UTC, tz_minuteswest is negative, so stamp moves forward. do_sys_settimeofday64() accepts tz_minuteswest down to -15*60. That means the old code could produce a bad monthday up to 15 hours before 2040-01-01 UTC.
Could the message also say what the out-of-range monthday leads to? time_mt() uses it as a shift count:
if (!(info->monthdays_match & (1U << current_time.monthday)))
With the old code, a monthday of 32 or more makes this shift undefined behaviour, and the rule silently fails to match. Once the u8 wraps back into 1-31 later in 2040, rules match on the wrong days.
Quoted 2 lines
Signed-off-by Fernando Fernandez Mancera <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>
[Severity: Low]
Should this have a Fixes: tag? The days_since_epoch[] table and DSE_FIRST = 2039 came in with the original match:
Without the tag, stable backporters could read this as only the size cleanup suggested by the bloat-o-meter numbers.
Quoted 4 lines
net/netfilter/xt_time.c +0
-0
[ ... ]
Quoted 6 lines
net/netfilter/xt_time.c +0
-2@@ -102,51 +71,12 @@ static inline void localtime_2(struct xtm *r, time64_t time)static void localtime_3 (struct xtm *r,time64_t time) { -unsigned int year, i, w = r->dse; -
[ ... ]
net/netfilter/xt_time.c static inline void localtime_2(struct xtm *r, time64_t time) · line 80
Quoted 6 lines
net/netfilter/xt_time.c +3
-1+time64_to_tm (time,0 , &tm); - r->month = i +1 ; + r->monthday = tm.tm_mday;/* 1-31 */ + r->month = tm.tm_mon +1 ;/* 1-12 */ }
[Severity: Low]
This isn't a bug, but localtime_3() now works only from time and no longer reads r->dse. Are some comments and struct xtm now out of date?
The file header still credits dietlibc:
* It is using some modified code from dietlibc (localtime() function)
* that you can find at https://www.fefe.de/dietlibc/
The table walk removed here was the last of that code.
The comment in localtime_2() still says it computes monthday:
/* * Here comes the rest (weekday, monthday). First, divide the SSTE * by seconds-per-day to get the number of _days_ since the epoch. */
It now computes only dse and weekday.
struct xtm also keeps this field:
unsigned int dse;
It used to carry days-since-epoch from localtime_2() to localtime_3(). Now it is only read inside localtime_2(). Could this lead readers to think localtime_3() depends on localtime_2() running first?
There is also a small side effect. Weekday comes from the unsigned div_u64() in localtime_2(). Monthday comes from the signed, floor-corrected division in time64_to_tm(). The two only differ for a negative stamp. That needs a time within the first day of 1970, plus either the XT_TIME_CONTIGUOUS one-day subtraction or a timezone west of UTC.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 6/13 netfilter: nft_flow_offload: do not offload multicast
In reply to Pablo Neira AyusoFrom: Koral Ilgun <[email protected]>
A flow holds one output per direction, which it resolves when it is added. The outputs of a multicast route change when listeners join or leave. So a multicast flow in the flowtable continues to send to the output that it resolved, also after the last listener left, and the counters of the multicast routing stop.
A ruleset can keep multicast out with a match before the flow offload statement. Without one, IPv4 multicast stays out of the flowtable only because the reverse route lookup in nft_flow_route() refuses a multicast source address. The IPv6 route lookup has no such check, so IPv6 multicast that ip6mr forwards goes into the flowtable.
Skip a multicast destination for both families in the flow offload expression itself, so that the result no longer depends on the ruleset or on the route lookup.
--- net/netfilter/nft_flow_offload.c | 5 +++++ 1 file changed, 5 insertions(+)
net/netfilter/nft_flow_offload.c +5 -0
@@ -10,6 +10,7 @@#include <linux/netfilter/nf_conntrack_common.h> #include <linux/netfilter/nf_tables.h> #include <net/ip.h> +#include <net/ipv6.h> #include <net/flow.h> #include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_core.h>
@@ -33,6 +34,10 @@ static bool nft_flow_offload_skip(struct sk_buff *skb, int family)if (unlikely (opt->optlen))return true; + +return ipv4_is_multicast (ip_hdr (skb)->daddr); + }else if (family ==NFPROTO_IPV6 ) { +return ipv6_addr_is_multicast (&ipv6_hdr (skb)->daddr); }return false;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 06/13] netfilter: nft_flow_offload: do not offload multicast
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] Commit "netfilter: nft_flow_offload: do not offload multicast" fixes a long-standing bug that users can see, but it has no Fixes: tag.
--
netfilter: nft_flow_offload: do not offload multicast
This commit makes nft_flow_offload_skip() skip packets with a multicast destination address for both IPv4 and IPv6. Before this, IPv4 multicast stayed out of the flowtable only because the reverse route lookup failed. IPv6 multicast forwarded by ip6mr could be offloaded and then keep sending to a stale output.
Quoted 4 lines
net/netfilter/nft_flow_offload.c +0
-0
[ ... ]
net/netfilter/nft_flow_offload.c static bool nft_flow_offload_skip(struct sk_buff *skb, int family) · line 40
Quoted 8 lines
net/netfilter/nft_flow_offload.c +4
-0@@ -33,6 +34,10 @@ static bool nft_flow_offload_skip(struct sk_buff *skb, int family)if (unlikely (opt->optlen))return true; + +return ipv4_is_multicast (ip_hdr (skb)->daddr); + }else if (family ==NFPROTO_IPV6 ) { +return ipv6_addr_is_multicast (&ipv6_hdr (skb)->daddr);
[Severity: Low]
This isn't a bug, but should this patch carry a Fixes: tag?
Before this change, nft_flow_offload_skip() returned false for IPv6. Nothing else in nft_flow_offload_eval() rejected a multicast destination, so IPv6 multicast forwarded by ip6mr reached the flow offload expression through the FORWARD hook:
net/ipv6/ip6mr.c:ip6mr_forward2() {
...
return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
net, NULL, skb, indev, skb->dev,
ip6mr_forward2_finish);
...
}A flowtable entry could then be created for it. That entry keeps sending to the output it resolved when it was added, even after the last listener leaves, and the mroute counters stop.
The IPv6 path seems to have been offloadable since the flow offload expression was added, so the likely target would be:
Most of the other fixes in the same pull request carry Fixes: tags, for example "netfilter: flowtable: purge entries immediately when destroying the flowtable" and "netfilter: flowtable: do not clear the offload bit twice". Without a tag here, stable maintainers have no way to tell which trees need this fix.
Quoted 3 lines
net/netfilter/nft_flow_offload.c +0
-0}return false;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 7/13 netfilter: flowtable: purge entries immediately when destroying the flowtable
In reply to Pablo Neira AyusoDo not use hardware offload workqueue to purge the remaining flow entries when the flowtable is being destroyed. Instead, iterate over the flowtable to purge the remaining flow offload entries. Work memory allocations can fail, leaving us with either waiting indefinitely until work can be allocated or leaving entries in the hardware.
Given it is possible to encounter either the original or reply direction when walking down the rhashtable in the destroy path, use the NF_FLOW_PENDING bit to indicate that the first tuplehash has been already visited, so this flow can be safely released.
--- include/net/netfilter/nf_flow_table.h | 3 ++- net/netfilter/nf_flow_table_core.c | 29 +++++++++++++++++++++++---- net/netfilter/nf_flow_table_offload.c | 23 +++++++++++++-------- 3 files changed, 42 insertions(+), 13 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -1
@@ -375,7 +375,8 @@ void nf_flow_offload_stats(struct nf_flowtable *flowtable, struct flow_offload *flow);void nf_flow_table_offload_flush (struct nf_flowtable *flowtable); -void nf_flow_table_offload_flush_cleanup (struct nf_flowtable *flowtable); +void nf_flow_table_offload_purge (struct nf_flowtable *flowtable, +struct flow_offload *flow); int nf_flow_table_offload_setup(struct nf_flowtable *flowtable, struct net_device *dev,
net/netfilter/nf_flow_table_core.c +25 -4
@@ -770,6 +770,29 @@ void nf_flow_table_cleanup(struct net_device *dev) }EXPORT_SYMBOL_GPL (nf_flow_table_cleanup); +static void nf_flow_table_do_purge (void *ptr,void *arg) +{ +struct flow_offload_tuple_rhash *tuplehash = ptr; +struct nf_flowtable *flow_table = arg; +struct flow_offload *flow; +int dir; + + dir = tuplehash->tuple.dir; + flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); + +/* Let the second tuplehash that is seen release the flow. */ +if (!test_and_set_bit (NF_FLOW_PENDING , &flow->flags)) +return ; + +flow_offload_teardown (flow); + +if (nf_flowtable_hw_offload (flow_table) && + !test_bit (NF_FLOW_HW_DEAD , &flow->flags)) +nf_flow_table_offload_purge (flow_table, flow); + +flow_offload_free (flow); +} +void nf_flow_table_free (struct nf_flowtable *flow_table) {mutex_lock (&flowtable_lock);
@@ -779,10 +802,8 @@ void nf_flow_table_free(struct nf_flowtable *flow_table)cancel_delayed_work_sync (&flow_table->gc_work);nf_flow_table_offload_flush (flow_table);/* ... no more pending work after this stage ... */ -nf_flow_table_iterate (flow_table, nf_flow_table_do_cleanup,NULL ); -nf_flow_table_gc_run (flow_table); -nf_flow_table_offload_flush_cleanup (flow_table); -rhashtable_destroy (&flow_table->rhashtable); +rhashtable_free_and_destroy (&flow_table->rhashtable, + nf_flow_table_do_purge, flow_table); }EXPORT_SYMBOL_GPL (nf_flow_table_free);
net/netfilter/nf_flow_table_offload.c +15 -8
@@ -1181,14 +1181,6 @@ void nf_flow_offload_stats(struct nf_flowtable *flowtable,flow_offload_queue_work (offload); } -void nf_flow_table_offload_flush_cleanup (struct nf_flowtable *flowtable) -{ -if (nf_flowtable_hw_offload (flowtable)) { -flush_workqueue (nf_flow_offload_del_wq); -nf_flow_table_gc_run (flowtable); - } -} -void nf_flow_table_offload_flush (struct nf_flowtable *flowtable) {if (nf_flowtable_hw_offload (flowtable)) {
@@ -1198,6 +1190,21 @@ void nf_flow_table_offload_flush(struct nf_flowtable *flowtable) } } +void nf_flow_table_offload_purge (struct nf_flowtable *flowtable, +struct flow_offload *flow) +{ +struct flow_offload_work offload = { + .cmd =FLOW_CLS_DESTROY , + .flowtable = flowtable, + .flow = flow, + }; + +if (nf_flow_offload_unsupported (flow)) +return ; + +flow_offload_work_del (&offload); +} +static int nf_flow_table_block_setup (struct nf_flowtable *flowtable,struct flow_block_offload *bo,enum flow_block_command cmd)
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 07/13] netfilter: flowtable: purge entries immediately when destroying the flowtable
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message for "netfilter: flowtable: purge entries immediately when destroying the flowtable" understates the bug it…
--
netfilter: flowtable: purge entries immediately when destroying the flowtable
This commit stops using the hardware offload workqueue to purge the remaining flow entries when a flowtable is destroyed. nf_flow_table_free() now calls rhashtable_free_and_destroy() with nf_flow_table_do_purge(), which tears down each flow, removes it from hardware synchronously and frees it. NF_FLOW_PENDING marks that the first tuplehash of a flow has already been visited.
Quoted 3 lines
Work memory
allocations can fail, leaving us with either waiting indefinitely until work can be allocated or leaving entries in the hardware.
[Severity: Low]
Does this fully describe what could happen in the old destroy path?
In the old nf_flow_table_free(), a torn-down flow with NF_FLOW_HW set and NF_FLOW_HW_DYING clear goes through:
nf_flow_table_gc_run()
nf_flow_offload_gc_step()
nf_flow_offload_del()
nf_flow_offload_work_alloc() /* GFP_ATOMIC */
If that allocation fails, nf_flow_offload_del() returns before it sets NF_FLOW_HW_DYING:
offload = nf_flow_offload_work_alloc(flowtable, flow, FLOW_CLS_DESTROY);
if (!offload)
return;
set_bit(NF_FLOW_HW_DYING, &flow->flags);
flow_offload_queue_work(offload);The flow therefore stays in the table for the second pass in nf_flow_table_offload_flush_cleanup():
flush_workqueue(nf_flow_offload_del_wq);
nf_flow_table_gc_run(flowtable);If the allocation fails again, rhashtable_destroy() frees only the bucket tables. Wouldn't the flow_offload leak, along with its nf_conn reference (nf_ct_put() in flow_offload_free_rcu() never runs) and its route dst references?
If the allocation succeeds in the second pass, the FLOW_CLS_DESTROY work is queued after the last flush_workqueue(). Nothing flushes it before nf_flow_table_free() returns. The flow still leaks, because flow_offload_del() is never reached for it.
The caller then frees the flowtable. act_ct frees ct_ft after tcf_ct_flow_table_cleanup_work(), and nf_tables kfree()s the nft_flowtable that embeds it.
Can flow_offload_work_handler() and flow_offload_work_del() then read offload->flowtable->net, ->priority and ->flow_block.cb_list from freed memory?
The code change looks correct and fixes both cases. Could the changelog also mention the flow, nf_conn and dst leak, and the possible use-after-free of the flowtable?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 8/13 selftests: netfilter: nf_queue: drop the no-op loop initializer
In reply to Pablo Neira AyusoFrom: Sasha Levin <[email protected]>
The out-of-order verdict loop starts with "for (ooo_count; ...", and clang warns about the unused expression:
nf_queue.c:354:10: warning: expression result unused [-Wunused-value]
Leave the initializer empty.
--- tools/testing/selftests/net/netfilter/nf_queue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/net/netfilter/nf_queue.c +1 -1
@@ -351,7 +351,7 @@ static int mainloop(void)if (opts.out_of_order) { ooo_ids[ooo_count] = id;if (ooo_count >=15 ) { -for (ooo_count; ooo_count >=0 ; ooo_count--) { +for (; ooo_count >=0 ; ooo_count--) { nlh =nfq_build_verdict (buf, ooo_ids[ooo_count], opts.queue_num, opts.verdict);if (mnl_socket_sendto (nl, nlh, nlh->nlmsg_len) <0 ) {
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 9/13 netfilter: flowtable: do not scrub as crossing netns
In reply to Pablo Neira AyusoIPIP tunneling support does not result in crossing a netns, therefore, adopt the more conservative variant of skb_scrub_packet() by specifying !xnet, keep the existing skb mark in place.
--- net/netfilter/nf_flow_table_ip.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_ip.c +2 -2
@@ -630,7 +630,7 @@ static int nf_flow_tunnel_ip_push(struct net *net, struct sk_buff *skb,if (err)return err; -skb_scrub_packet (skb, true); +skb_scrub_packet (skb, false);skb_clear_hash_if_not_l4 (skb);/* Push down and install the IP header. */
@@ -699,7 +699,7 @@ static int nf_flow_tunnel_ip6_push(struct net *net, struct sk_buff *skb,if (err)return err; -skb_scrub_packet (skb, true); +skb_scrub_packet (skb, false); mtu =dst_mtu (dst) -sizeof (*ip6h); mtu =max (mtu,IPV6_MIN_MTU );skb_dst_update_pmtu_no_confirm (skb, mtu);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 09/13] netfilter: flowtable: do not scrub as crossing netns
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message and its Fixes: tag cover only half of what the patch changes.
--
netfilter: flowtable: do not scrub as crossing netns
This commit changes the flowtable tunnel tx push helpers to call skb_scrub_packet() with xnet set to false, so the existing skb mark is kept. The commit message gives the reason that IPIP tunneling does not cross a netns.
Quoted 4 lines
net/netfilter/nf_flow_table_ip.c +0
-0
[ ... ]
net/netfilter/nf_flow_table_ip.c static int nf_flow_tunnel_ip6_push(struct net *net, struct sk_buff *skb, · line 702
Quoted 6 lines
net/netfilter/nf_flow_table_ip.c +1
-1@@ -699,7 +699,7 @@ static int nf_flow_tunnel_ip6_push(struct net *net, struct sk_buff *skb,if (err)return err; -skb_scrub_packet (skb, true); +skb_scrub_packet (skb, false);
[Severity: Low]
This isn't a bug, but should the commit message and Fixes: tags also cover this hunk?
The message only mentions "IPIP tunneling support", and the only tag is:
That tag matches the change in nf_flow_tunnel_ip_push(). The call changed here in nf_flow_tunnel_ip6_push() came from a different commit:
93cf357fa797 ("netfilter: flowtable: Add IP6IP6 tx sw acceleration")
The IPv6 outer path changes behavior as well. With xnet set to false, skb_scrub_packet() now returns before it reaches this code:
skb->mark = 0; skb_clear_tstamp(skb);
Stable selection still works, because 93cf357fa797 came after d30301ba4b07. Every tree that has the IPv6 code also has the commit that is cited.
Could a second Fixes: tag for 93cf357fa797 be added, along with a sentence about the IP6IP6 / IPv6 outer path?
Quoted 3 lines
net/netfilter/nf_flow_table_ip.c +0
-0mtu =dst_mtu (dst) -sizeof (*ip6h); mtu =max (mtu,IPV6_MIN_MTU );skb_dst_update_pmtu_no_confirm (skb, mtu);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org
Pablo Neira Ayuso
maintainerPATCH v2 10/13 netfilter: flowtable: tunnel device poisons h_source of fast-path frames
In reply to Pablo Neira AyusoFrom: Marek Majkowski <[email protected]>
When a flow enters an IP tunnel, the forward path discovered by dev_fill_forward_path() starts with a DEV_PATH_TUN entry for the tunnel device, followed by whatever carries the outer packet. nft_dev_path_info() handles DEV_PATH_TUN in the same switch case as the Ethernet-like path types and copies path->dev->dev_addr into info->h_source if it is still unset. For a tunnel device dev_addr is not an Ethernet address: ip6_tunnel stores the 16-byte local IPv6 there and ip_tunnel the 4-byte local IPv4. The tunnel entry is the first one on the path, so its "address" wins and poisons h_source: every fast-path frame is transmitted with the first bytes of the tunnel local IP address as the Ethernet source MAC.
Seen on a DS-Lite setup, LAN -> ip6tnl (IPv4 inside IPv6) -> PPPoE -> 802.1Q VLAN -> Ethernet, with conntrack flows offloaded through an nftables flowtable ('flow add @ft'). The first packets of a flow, still on the slow path, and the fast-path packets of the same flow differ only in the Ethernet source address:
slow path: d8:58:d7:00:2e:bd > 80:ac:ac:39:ef:c2, 802.1Q vlan 35,
PPPoE [ses 0x454] IP6 2a01:110f:4328:c400::1 >
2a01:1000:0:1::2216 next-header IPIP, 192.0.0.2.40010 >
1.1.1.1.53fast path: 2a:01:11:0f:43:28 > 80:ac:ac:39:ef:c2, same frame otherwise
As you can see 2a:01:11:0f:43:28 is the start of 2a01:110f:4328:c400::1, the tunnel local address. The PPPoE endpoint binds the session to the subscriber MAC and silently drops the frames, the effect is that every TCP/UDP flow dies the moment it is offloaded, while ICMP, which is never offloaded, keeps working. Received traffic is unaffected.
Do not let DEV_PATH_TUN poison h_source: skip the assignment for it. The real link-layer device appears later on the same path and provides the correct address; for a path with no Ethernet device the address stays zero as before.
--- net/netfilter/nf_flow_table_path.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nf_flow_table_path.c +6 -1
@@ -121,7 +121,12 @@ static int nft_dev_path_info(struct net_device_path_stack *stack,case DEV_PATH_PPPOE :case DEV_PATH_TUN : info->dev = path->dev; -if (is_zero_ether_addr (info->h_source)) +/* A tunnel device has no Ethernet address: its dev_addr +* is the tunnel local IP address. Take h_source from the +* real link-layer device further down the path. +*/ +if (path->type !=DEV_PATH_TUN && +is_zero_ether_addr (info->h_source))memcpy (info->h_source, path->dev->dev_addr,ETH_ALEN ); if (path->type == DEV_PATH_ETHERNET ||
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 11/13 netfilter: flowtable: do not clear the offload bit twice
In reply to Pablo Neira AyusoFrom: João Silva <[email protected]>
An expired hardware flow remains in the flowtable until its asynchronous hardware deletion completes. Teardown clears IPS_OFFLOAD_BIT, so a packet can create a replacement flow referencing the same conntrack entry while the old flow is still waiting for deletion.
The next GC pass calls flow_offload_teardown() on the expired old flow again, clearing the replacement's offload bit. More packets can then create additional flows for the same conntrack entry. A pending hardware work item can also delay deletion and expose the same repeated teardown.
Commit d2d31ea8cd80 ("netfilter: conntrack: fix erronous removal of offload bit") closed this window for software flows by unlinking them in the same GC pass and made the conntrack state fixup one-shot, but hardware flows stay linked until NF_FLOW_HW_DEAD is set.
Make clearing IPS_OFFLOAD_BIT part of the existing one-time teardown transition, alongside the conntrack state fixup. Subsequent teardown calls must not alter the offload state now owned by a replacement flow.
--- net/netfilter/nf_flow_table_core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
net/netfilter/nf_flow_table_core.c +4 -2
@@ -398,9 +398,11 @@ static void flow_offload_del(struct nf_flowtable *flow_table,void flow_offload_teardown (struct flow_offload *flow) { +if (test_and_set_bit (NF_FLOW_TEARDOWN , &flow->flags)) +return ; +clear_bit (IPS_OFFLOAD_BIT , &flow->ct->status); -if (!test_and_set_bit (NF_FLOW_TEARDOWN , &flow->flags)) -flow_offload_fixup_ct (flow); +flow_offload_fixup_ct (flow); }EXPORT_SYMBOL_GPL (flow_offload_teardown);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 12/13 netfilter: nfnetlink_queue: make skb writable before writing NFQA_L2HDR
In reply to Pablo Neira AyusoFrom: Tamaki Yanagawa <[email protected]>
nfqa_parse_bridge() copies the NFQA_L2HDR attribute over the mac header of the queued skb in place. Bridge flooding queues clones that share their data with the copies sent to the other ports, so the rewritten header also shows up on frames that never went through the queue, and is changed while those may already be in transmission. Call skb_ensure_writable() before the copy. At the bridge hooks skb->data points to the network header, so the mac header sits in the headroom, which skb_clone_writable() already takes into account: a write length of zero is enough, and a clone that owns its header area (nohdr) is left alone instead of being copied.
--- net/netfilter/nfnetlink_queue.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
net/netfilter/nfnetlink_queue.c +6 -1
@@ -1830,10 +1830,15 @@ static int nfqa_parse_bridge(struct nf_queue_entry *entry,if (mac_header_len !=nla_len (nfqa[NFQA_L2HDR ]))return -EINVAL ; -else if (mac_header_len >0 ) + +if (mac_header_len >0 ) { +if (skb_ensure_writable (entry->skb,0 )) +return -ENOMEM ; +memcpy (skb_mac_header (entry->skb),nla_data (nfqa[NFQA_L2HDR ]), mac_header_len); + } }return 0 ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v2 13/13 selftests: netfilter: add UDP flowtable offload test
In reply to Pablo Neira AyusoFrom: Jiri Peska <[email protected]>
nft_flowtable.sh offloads only TCP, so the UDP flowtable paths never
run: the UDP tuple lookup, the UDP checksum update, IPv6 source NAT, and nf_ct_acct_update behind the flowtable counter flag.
Add an echo test for IPv4 and IPv6, with and without masquerade. The payload must come back intact, the forward chain must see only the handshake, and conntrack must mark the flow OFFLOAD and account every datagram.
--- .../testing/selftests/net/netfilter/Makefile | 1 + .../net/netfilter/nft_flowtable_udp.sh | 238 ++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100755 tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh
tools/testing/selftests/net/netfilter/Makefile +1 -0
@@ -30,6 +30,7 @@ TEST_PROGS := \ nft_fib.sh \ nft_fib_nexthop.sh \ nft_flowtable.sh \ + nft_flowtable_udp.sh \ nft_interface_stress.sh \ nft_meta.sh \ nft_nat.sh \
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh new file +238 -0
238 lines not on this page · raw message
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH net-next 13/13] selftests: netfilter: add UDP flowtable offload test
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 2
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] listener_ready() in nft_flowtable_udp.sh (line 111) runs ss -N "$ns2" -uln -H "sport = :$port" | grep -q "$port".
- [Low] The test cannot detect a broken UDP checksum rewrite in the flowtable NAT path, although the commit message and file header…
- [Low] The header comment of nft_flowtable_udp.sh lists nf_flow_dnat_ipv6 and flow_offload_fixup_ct among the paths this UDP…
--
selftests: netfilter: add UDP flowtable offload test
This adds nft_flowtable_udp.sh, which sends a burst of UDP echoes through an ingress flowtable for IPv4 and IPv6, with and without masquerade. It checks that the payload comes back, that the forward chain sees only the first packets of the flow, and that conntrack marks the entry OFFLOAD and accounts every datagram.
Quoted 6 lines
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh new file +0
-0
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh line 10
Quoted 10 lines
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh new file +10
-0@@ -0,0 +1,238 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# nft_flowtable.sh offloads TCP only. These UDP paths therefore stay at +# zero coverage in that run: +# +# nf_flow_tuple_ip / nf_flow_tuple_ipv6 IPPROTO_UDP +# nf_flow_nat_ip_udp / nf_flow_nat_port_udp IPv4 NAT checksum +# nf_flow_snat_ipv6 / nf_flow_dnat_ipv6 IPv6 NAT, including UDP +# flow_offload_get_timeout / flow_offload_fixup_ct for UDP
[Severity: Low]
Does this test actually exercise nf_flow_dnat_ipv6() and flow_offload_fixup_ct()?
The only NAT rule the script adds is a source NAT:
nft add rule inet nat postrouting oifname "veth1" masquerade
flow_offload_alloc() sets NF_FLOW_DNAT only from IPS_DST_NAT:
net/netfilter/nf_flow_table_core.c:flow_offload_alloc() {
...
if (ct->status & IPS_DST_NAT)
__set_bit(NF_FLOW_DNAT, &flow->flags);
...
}nf_flow_nat_ipv6() calls nf_flow_dnat_ipv6() only when that bit is set:
if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);So it looks like the DNAT path never runs.
flow_offload_fixup_ct() has one caller, flow_offload_teardown(). Teardown happens through gc expiry, ct dying, dst/MTU failures, or flowtable/netdev cleanup. None of these happen during the four run_case() checks. Teardown only happens when the EXIT trap deletes the namespaces, which is after every check has finished. A regression in the UDP fixup_ct path would therefore not fail this test.
Also, tools/testing/selftests/bpf/prog_tests/xdp_flowtable.c already sends UDP through a flowtable. The "zero coverage" wording only holds because of the "in that run" qualifier.
Could these entries be removed from the header? The other option is to add a DNAT case (for example a prerouting dnat rule) and an explicit teardown/fixup check.
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh line 47
Quoted 7 lines
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh new file +7
-0+ip link add veth0 netns "$nsr" type veth peer name eth0 netns "$ns1" +ip link add veth1 netns "$nsr" type veth peer name eth0 netns "$ns2" + +ip -net "$nsr" link set veth0 up +ip -net "$nsr" link set veth1 up +ip -net "$ns1" link set eth0 up +ip -net "$ns2" link set eth0 up
[Severity: Low]
Can this test detect a broken UDP checksum rewrite in the masquerade cases?
The veth pairs keep their default features. VETH_FEATURES includes NETIF_F_HW_CSUM, so the datagrams socat sends from ns1 and ns2 are CHECKSUM_PARTIAL.
For those skbs, nf_flow_nat_ip_udp() and the IPv6 equivalent, nf_flow_nat_ipv6_l4proto(), only patch the pseudo-header partial sum:
if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
inet_proto_csum_replace4(&udph->check, skb, addr,
new_addr, true);The skb is still CHECKSUM_PARTIAL when it reaches the peer namespace. skb_csum_unnecessary() returns true for it, so the receiving UDP stack never verifies the checksum.
The only correctness check is the payload echo in udp_exchange():
if ! IFS= read -r -n 4 -t 2 got <&"${SO[0]}"; thenThat check passes whether the rewrite is right or wrong. The full-checksum (non-PARTIAL) branch of the rewrite never runs either.
Would it make sense to run "ethtool -K eth0 tx off" in ns1 and ns2, so the datagrams carry full checksums that the receiver verifies? If not, the header and commit message could say that checksum correctness is not verified.
[ ... ]
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh line 122
Quoted 13 lines
tools/testing/selftests/net/netfilter/nft_flowtable_udp.sh new file +13
-0+listener_ready() { + ss -N "$ns2" -uln -H "sport = :$port" | grep -q "$port" +} + +ip netns exec "$ns2" socat -T 60 \ + UDP4-LISTEN:"$port",reuseaddr,fork EXEC:cat >/dev/null 2>&1 & +ip netns exec "$ns2" socat -T 60 \ + UDP6-LISTEN:"$port",reuseaddr,ipv6only=1,fork EXEC:cat >/dev/null 2>&1 & + +if ! busywait 3000 listener_ready; then + echo "FAIL: UDP listener did not start" >&2 + exit 1 +fi
[Severity: Medium]
Can listener_ready() succeed when only one of the two listeners has bound?
The ss query covers both address families. The grep matches as soon as either the UDP4-LISTEN or the UDP6-LISTEN socat has bound port 12345. busywait() returns on the first success via loopy_wait() in lib.sh:
if out=$("$@"); then
echo -n "$out"The two socat processes start in the background at the same time. Nothing orders their binds.
If the IPv6 listener binds first, the script goes straight to run_case "IPv4 UDP". udp_exchange() can then send datagram 0 to 10.0.2.99:12345 before the UDP4 socat is listening. ns2 drops it with an ICMP port unreachable, and the read times out. The run then reports "FAIL: packet 0 timeout" and "IPv4 UDP: payload was not forwarded" for a reason that has nothing to do with the flowtable.
Could the check wait for both sockets? For example, it could query ss -4 and ss -6 separately, or require two matching lines.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261116001612.2806263-2-pablo%40netfilter.org