netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net 0/2] netfilter: updates for net
@ 2025-09-02 18:58 Florian Westphal
  2025-09-02 18:58 ` [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang Florian Westphal
  2025-09-02 18:58 ` [PATCH net 2/2] netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX Florian Westphal
  0 siblings, 2 replies; 6+ messages in thread
From: Florian Westphal @ 2025-09-02 18:58 UTC (permalink / raw)
  To: netdev
  Cc: Paolo Abeni, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netfilter-devel, pablo

Hi,

The following patchset contains Netfilter fixes for *net*:

1) Fix a silly bug in conntrack selftest, busyloop may get optimized to
   for (;;), reported by Yi Chen.

2) Introduce new NFTA_DEVICE_PREFIX attribute in nftables netlink api,
   re-using old NFTA_DEVICE_NAME led to confusion with different
   kernel/userspace versions.  This refines the wildcard interface
   support added in 6.16 release.  From Phil Sutter.

Please, pull these changes from:
The following changes since commit a6099f263e1f408bcc7913c9df24b0677164fc5d:

  net: ethernet: ti: am65-cpsw-nuss: Fix null pointer dereference for ndev (2025-09-02 14:51:45 +0200)

are available in the Git repository at:

  https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf.git tags/nf-25-09-02

for you to fetch changes up to 745d9ca5317a03b55016cdd810e4d2aac57f45df:

  netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX (2025-09-02 20:52:28 +0200)

----------------------------------------------------------------
netfilter pull request nf-25-09-02

----------------------------------------------------------------
Florian Westphal (1):
      selftests: netfilter: fix udpclash tool hang

Phil Sutter (1):
      netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX

 include/uapi/linux/netfilter/nf_tables.h           |  2 ++
 net/netfilter/nf_tables_api.c                      | 42 ++++++++++++++++------
 .../selftests/net/netfilter/conntrack_clash.sh     |  2 +-
 .../selftests/net/netfilter/conntrack_resize.sh    |  5 +--
 tools/testing/selftests/net/netfilter/udpclash.c   |  3 +-
 5 files changed, 39 insertions(+), 15 deletions(-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang
  2025-09-02 18:58 [PATCH net 0/2] netfilter: updates for net Florian Westphal
@ 2025-09-02 18:58 ` Florian Westphal
  2025-09-03 22:15   ` Jakub Kicinski
  2025-09-02 18:58 ` [PATCH net 2/2] netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX Florian Westphal
  1 sibling, 1 reply; 6+ messages in thread
From: Florian Westphal @ 2025-09-02 18:58 UTC (permalink / raw)
  To: netdev
  Cc: Paolo Abeni, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netfilter-devel, pablo

Yi Chen reports that 'udpclash' loops forever depending on compiler
(and optimization level used); while (x == 1) gets optimized into
for (;;).  Switch to stdatomic to prevent this.

While at it, also run it under timeout(1) and fix the resize script
to not ignore the timeout passed as second parameter to insert_flood.

Reported-by: Yi Chen <yiche@redhat.com>
Suggested-by: Yi Chen <yiche@redhat.com>
Fixes: 78a588363587 ("selftests: netfilter: add conntrack clash resolution test case")
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 tools/testing/selftests/net/netfilter/conntrack_clash.sh  | 2 +-
 tools/testing/selftests/net/netfilter/conntrack_resize.sh | 5 +++--
 tools/testing/selftests/net/netfilter/udpclash.c          | 3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/net/netfilter/conntrack_clash.sh b/tools/testing/selftests/net/netfilter/conntrack_clash.sh
index 606a43a60f73..7fc6c5dbd551 100755
--- a/tools/testing/selftests/net/netfilter/conntrack_clash.sh
+++ b/tools/testing/selftests/net/netfilter/conntrack_clash.sh
@@ -99,7 +99,7 @@ run_one_clash_test()
 	local entries
 	local cre
 
-	if ! ip netns exec "$ns" ./udpclash $daddr $dport;then
+	if ! ip netns exec "$ns" timeout 30 ./udpclash $daddr $dport;then
 		echo "INFO: did not receive expected number of replies for $daddr:$dport"
 		ip netns exec "$ctns" conntrack -S
 		# don't fail: check if clash resolution triggered after all.
diff --git a/tools/testing/selftests/net/netfilter/conntrack_resize.sh b/tools/testing/selftests/net/netfilter/conntrack_resize.sh
index 788cd56ea4a0..615fe3c6f405 100755
--- a/tools/testing/selftests/net/netfilter/conntrack_resize.sh
+++ b/tools/testing/selftests/net/netfilter/conntrack_resize.sh
@@ -187,7 +187,7 @@ ct_udpclash()
 	[ -x udpclash ] || return
 
         while [ $now -lt $end ]; do
-		ip netns exec "$ns" ./udpclash 127.0.0.1 $((RANDOM%65536)) > /dev/null 2>&1
+		ip netns exec "$ns" timeout 30 ./udpclash 127.0.0.1 $((RANDOM%65536)) > /dev/null 2>&1
 
 		now=$(date +%s)
 	done
@@ -277,6 +277,7 @@ check_taint()
 insert_flood()
 {
 	local n="$1"
+	local timeout="$2"
 	local r=0
 
 	r=$((RANDOM%$insert_count))
@@ -302,7 +303,7 @@ test_floodresize_all()
 	read tainted_then < /proc/sys/kernel/tainted
 
 	for n in "$nsclient1" "$nsclient2";do
-		insert_flood "$n" &
+		insert_flood "$n" "$timeout" &
 	done
 
 	# resize table constantly while flood/insert/dump/flushs
diff --git a/tools/testing/selftests/net/netfilter/udpclash.c b/tools/testing/selftests/net/netfilter/udpclash.c
index 85c7b906ad08..0a3b8890b530 100644
--- a/tools/testing/selftests/net/netfilter/udpclash.c
+++ b/tools/testing/selftests/net/netfilter/udpclash.c
@@ -14,6 +14,7 @@
  *  ed07d9a021df ("netfilter: nf_conntrack: resolve clash for matching conntracks")
  *  6a757c07e51f ("netfilter: conntrack: allow insertion of clashing entries")
  */
+#include <stdatomic.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
@@ -29,7 +30,7 @@ struct thread_args {
 	int sockfd;
 };
 
-static int wait = 1;
+static atomic_int wait = ATOMIC_VAR_INIT(1);
 
 static void *thread_main(void *varg)
 {
-- 
2.49.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH net 2/2] netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX
  2025-09-02 18:58 [PATCH net 0/2] netfilter: updates for net Florian Westphal
  2025-09-02 18:58 ` [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang Florian Westphal
@ 2025-09-02 18:58 ` Florian Westphal
  1 sibling, 0 replies; 6+ messages in thread
From: Florian Westphal @ 2025-09-02 18:58 UTC (permalink / raw)
  To: netdev
  Cc: Paolo Abeni, David S. Miller, Eric Dumazet, Jakub Kicinski,
	netfilter-devel, pablo

From: Phil Sutter <phil@nwl.cc>

This new attribute is supposed to be used instead of NFTA_DEVICE_NAME
for simple wildcard interface specs. It holds a NUL-terminated string
representing an interface name prefix to match on.

While kernel code to distinguish full names from prefixes in
NFTA_DEVICE_NAME is simpler than this solution, reusing the existing
attribute with different semantics leads to confusion between different
versions of kernel and user space though:

* With old kernels, wildcards submitted by user space are accepted yet
  silently treated as regular names.
* With old user space, wildcards submitted by kernel may cause crashes
  since libnftnl expects NUL-termination when there is none.

Using a distinct attribute type sanitizes these situations as the
receiving part detects and rejects the unexpected attribute nested in
*_HOOK_DEVS attributes.

Fixes: 6d07a289504a ("netfilter: nf_tables: Support wildcard netdev hook specs")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 include/uapi/linux/netfilter/nf_tables.h |  2 ++
 net/netfilter/nf_tables_api.c            | 42 +++++++++++++++++-------
 2 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
index 2beb30be2c5f..8e0eb832bc01 100644
--- a/include/uapi/linux/netfilter/nf_tables.h
+++ b/include/uapi/linux/netfilter/nf_tables.h
@@ -1784,10 +1784,12 @@ enum nft_synproxy_attributes {
  * enum nft_device_attributes - nf_tables device netlink attributes
  *
  * @NFTA_DEVICE_NAME: name of this device (NLA_STRING)
+ * @NFTA_DEVICE_PREFIX: device name prefix, a simple wildcard (NLA_STRING)
  */
 enum nft_devices_attributes {
 	NFTA_DEVICE_UNSPEC,
 	NFTA_DEVICE_NAME,
+	NFTA_DEVICE_PREFIX,
 	__NFTA_DEVICE_MAX
 };
 #define NFTA_DEVICE_MAX		(__NFTA_DEVICE_MAX - 1)
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 58c5425d61c2..c1082de09656 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -1959,6 +1959,18 @@ static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
 	return -ENOSPC;
 }
 
+static bool hook_is_prefix(struct nft_hook *hook)
+{
+	return strlen(hook->ifname) >= hook->ifnamelen;
+}
+
+static int nft_nla_put_hook_dev(struct sk_buff *skb, struct nft_hook *hook)
+{
+	int attr = hook_is_prefix(hook) ? NFTA_DEVICE_PREFIX : NFTA_DEVICE_NAME;
+
+	return nla_put_string(skb, attr, hook->ifname);
+}
+
 static int nft_dump_basechain_hook(struct sk_buff *skb,
 				   const struct net *net, int family,
 				   const struct nft_base_chain *basechain,
@@ -1990,16 +2002,15 @@ static int nft_dump_basechain_hook(struct sk_buff *skb,
 			if (!first)
 				first = hook;
 
-			if (nla_put(skb, NFTA_DEVICE_NAME,
-				    hook->ifnamelen, hook->ifname))
+			if (nft_nla_put_hook_dev(skb, hook))
 				goto nla_put_failure;
 			n++;
 		}
 		nla_nest_end(skb, nest_devs);
 
 		if (n == 1 &&
-		    nla_put(skb, NFTA_HOOK_DEV,
-			    first->ifnamelen, first->ifname))
+		    !hook_is_prefix(first) &&
+		    nla_put_string(skb, NFTA_HOOK_DEV, first->ifname))
 			goto nla_put_failure;
 	}
 	nla_nest_end(skb, nest);
@@ -2310,7 +2321,8 @@ void nf_tables_chain_destroy(struct nft_chain *chain)
 }
 
 static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
-					      const struct nlattr *attr)
+					      const struct nlattr *attr,
+					      bool prefix)
 {
 	struct nf_hook_ops *ops;
 	struct net_device *dev;
@@ -2327,7 +2339,8 @@ static struct nft_hook *nft_netdev_hook_alloc(struct net *net,
 	if (err < 0)
 		goto err_hook_free;
 
-	hook->ifnamelen = nla_len(attr);
+	/* include the terminating NUL-char when comparing non-prefixes */
+	hook->ifnamelen = strlen(hook->ifname) + !prefix;
 
 	/* nf_tables_netdev_event() is called under rtnl_mutex, this is
 	 * indirectly serializing all the other holders of the commit_mutex with
@@ -2374,14 +2387,22 @@ static int nf_tables_parse_netdev_hooks(struct net *net,
 	struct nft_hook *hook, *next;
 	const struct nlattr *tmp;
 	int rem, n = 0, err;
+	bool prefix;
 
 	nla_for_each_nested(tmp, attr, rem) {
-		if (nla_type(tmp) != NFTA_DEVICE_NAME) {
+		switch (nla_type(tmp)) {
+		case NFTA_DEVICE_NAME:
+			prefix = false;
+			break;
+		case NFTA_DEVICE_PREFIX:
+			prefix = true;
+			break;
+		default:
 			err = -EINVAL;
 			goto err_hook;
 		}
 
-		hook = nft_netdev_hook_alloc(net, tmp);
+		hook = nft_netdev_hook_alloc(net, tmp, prefix);
 		if (IS_ERR(hook)) {
 			NL_SET_BAD_ATTR(extack, tmp);
 			err = PTR_ERR(hook);
@@ -2427,7 +2448,7 @@ static int nft_chain_parse_netdev(struct net *net, struct nlattr *tb[],
 	int err;
 
 	if (tb[NFTA_HOOK_DEV]) {
-		hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]);
+		hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV], false);
 		if (IS_ERR(hook)) {
 			NL_SET_BAD_ATTR(extack, tb[NFTA_HOOK_DEV]);
 			return PTR_ERR(hook);
@@ -9458,8 +9479,7 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,
 
 	list_for_each_entry_rcu(hook, hook_list, list,
 				lockdep_commit_lock_is_held(net)) {
-		if (nla_put(skb, NFTA_DEVICE_NAME,
-			    hook->ifnamelen, hook->ifname))
+		if (nft_nla_put_hook_dev(skb, hook))
 			goto nla_put_failure;
 	}
 	nla_nest_end(skb, nest_devs);
-- 
2.49.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang
  2025-09-02 18:58 ` [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang Florian Westphal
@ 2025-09-03 22:15   ` Jakub Kicinski
  2025-09-03 22:52     ` Florian Westphal
  0 siblings, 1 reply; 6+ messages in thread
From: Jakub Kicinski @ 2025-09-03 22:15 UTC (permalink / raw)
  To: Florian Westphal
  Cc: netdev, Paolo Abeni, David S. Miller, Eric Dumazet,
	netfilter-devel, pablo

On Tue,  2 Sep 2025 20:58:54 +0200 Florian Westphal wrote:
> Yi Chen reports that 'udpclash' loops forever depending on compiler
> (and optimization level used); while (x == 1) gets optimized into
> for (;;).  Switch to stdatomic to prevent this.

gcc version 15.1.1 (F42) w/ whatever flags kselftests use appear to be
unaware of this macro:

udpclash.c:33:26: error: implicit declaration of function ‘ATOMIC_VAR_INIT’; did you mean ‘ATOMIC_FLAG_INIT’? [-Wimplicit-function-declaration]
   33 | static atomic_int wait = ATOMIC_VAR_INIT(1);
      |                          ^~~~~~~~~~~~~~~
      |                          ATOMIC_FLAG_INIT
udpclash.c:33:26: error: initializer element is not constant

Could you perhaps use volatile instead?

> +#include <stdatomic.h>
>  #include <stdio.h>
>  #include <string.h>
>  #include <stdlib.h>
> @@ -29,7 +30,7 @@ struct thread_args {
>  	int sockfd;
>  };
>  
> -static int wait = 1;
> +static atomic_int wait = ATOMIC_VAR_INIT(1);
>  
>  static void *thread_main(void *varg)
>  {


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang
  2025-09-03 22:15   ` Jakub Kicinski
@ 2025-09-03 22:52     ` Florian Westphal
  2025-09-03 23:09       ` Jakub Kicinski
  0 siblings, 1 reply; 6+ messages in thread
From: Florian Westphal @ 2025-09-03 22:52 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: netdev, Paolo Abeni, David S. Miller, Eric Dumazet,
	netfilter-devel, pablo

Jakub Kicinski <kuba@kernel.org> wrote:
> On Tue,  2 Sep 2025 20:58:54 +0200 Florian Westphal wrote:
> > Yi Chen reports that 'udpclash' loops forever depending on compiler
> > (and optimization level used); while (x == 1) gets optimized into
> > for (;;).  Switch to stdatomic to prevent this.
> 
> gcc version 15.1.1 (F42) w/ whatever flags kselftests use appear to be
> unaware of this macro:
> 
> udpclash.c:33:26: error: implicit declaration of function ‘ATOMIC_VAR_INIT’; did you mean ‘ATOMIC_FLAG_INIT’? [-Wimplicit-function-declaration]
>    33 | static atomic_int wait = ATOMIC_VAR_INIT(1);
>       |                          ^~~~~~~~~~~~~~~
>       |                          ATOMIC_FLAG_INIT
> udpclash.c:33:26: error: initializer element is not constant
> Could you perhaps use volatile instead?

That works too.  I'll send a new PR tomorrow, its late here.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang
  2025-09-03 22:52     ` Florian Westphal
@ 2025-09-03 23:09       ` Jakub Kicinski
  0 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2025-09-03 23:09 UTC (permalink / raw)
  To: Florian Westphal
  Cc: netdev, Paolo Abeni, David S. Miller, Eric Dumazet,
	netfilter-devel, pablo

On Thu, 4 Sep 2025 00:52:20 +0200 Florian Westphal wrote:
> Jakub Kicinski <kuba@kernel.org> wrote:
> > On Tue,  2 Sep 2025 20:58:54 +0200 Florian Westphal wrote:  
> > > Yi Chen reports that 'udpclash' loops forever depending on compiler
> > > (and optimization level used); while (x == 1) gets optimized into
> > > for (;;).  Switch to stdatomic to prevent this.  
> > 
> > gcc version 15.1.1 (F42) w/ whatever flags kselftests use appear to be
> > unaware of this macro:
> > 
> > udpclash.c:33:26: error: implicit declaration of function ‘ATOMIC_VAR_INIT’; did you mean ‘ATOMIC_FLAG_INIT’? [-Wimplicit-function-declaration]
> >    33 | static atomic_int wait = ATOMIC_VAR_INIT(1);
> >       |                          ^~~~~~~~~~~~~~~
> >       |                          ATOMIC_FLAG_INIT
> > udpclash.c:33:26: error: initializer element is not constant
> > Could you perhaps use volatile instead?  
> 
> That works too.  I'll send a new PR tomorrow, its late here.

👍️ FWIW I'll be sending the PR tomorrow so plenty time..

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-03 23:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-02 18:58 [PATCH net 0/2] netfilter: updates for net Florian Westphal
2025-09-02 18:58 ` [PATCH net 1/2] selftests: netfilter: fix udpclash tool hang Florian Westphal
2025-09-03 22:15   ` Jakub Kicinski
2025-09-03 22:52     ` Florian Westphal
2025-09-03 23:09       ` Jakub Kicinski
2025-09-02 18:58 ` [PATCH net 2/2] netfilter: nf_tables: Introduce NFTA_DEVICE_PREFIX Florian Westphal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).