Netfilter updates for net-next
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi,
The following batch contains Netfilter updates for net-next:
1) Use kvmalloc in xt_hashlimit, from Denis Kirjanov.
2) Tighten nf_conntrack sysctl accepted values for nf_conntrack_max and nf_ct_expect_max, from Nicolas Bouchinet.
3) Avoid lookup in nft_fib if socket is available, from Florian Westphal.
4) Initialize struct lsm_context in nfnetlink_queue to avoid hypothetical ENOMEM errors, Chenyuan Yang.
5) Use strscpy() instead of _pad when initializing xtables table name, kzalloc is already used to initialized the table memory area. From Thorsten Blum.
6) Missing socket lookup by conntrack information for IPv6 traffic in nft_socket, there is a similar chunk in IPv4, this was never added when IPv6 NAT was introduced. From Maxim Mikityanskiy.
7) Fix clang issues with nf_tables CONFIG_MITIGATION_RETPOLINE, from WangYuli.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-25-03-23
Thanks.
----------------------------------------------------------------
The following changes since commit 71ca3561c268a07888ba9ce089ab8c3f54710cd4:
Merge branch 'mptcp-pm-code-reorganisation' (2025-03-10 13:36:18 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git tags/nf-next-25-03-23
for you to fetch changes up to e3a4182edd1ae60e7e3539ff3b3784af9830d223:
netfilter: nf_tables: Only use nf_skip_indirect_calls() when MITIGATION_RETPOLINE (2025-03-23 10:53:47 +0100)
----------------------------------------------------------------
netfilter pull request 25-03-23
----------------------------------------------------------------
Chenyuan Yang (1):
netfilter: nfnetlink_queue: Initialize ctx to avoid memory allocation error
Denis Kirjanov (1):
netfilter: xt_hashlimit: replace vmalloc calls with kvmalloc
Florian Westphal (1):
netfilter: fib: avoid lookup if socket is available
Maxim Mikityanskiy (1):
netfilter: socket: Lookup orig tuple for IPv6 SNAT
Nicolas Bouchinet (1):
netfilter: conntrack: Bound nf_conntrack sysctl writes
Thorsten Blum (1):
netfilter: xtables: Use strscpy() instead of strscpy_pad()
WangYuli (1):
netfilter: nf_tables: Only use nf_skip_indirect_calls() when MITIGATION_RETPOLINE
include/net/netfilter/nft_fib.h | 21 +++++++++++++++++++++ net/ipv4/netfilter/nft_fib_ipv4.c | 11 +++++------ net/ipv6/netfilter/nf_socket_ipv6.c | 23 +++++++++++++++++++++++ net/ipv6/netfilter/nft_fib_ipv6.c | 19 ++++++++++--------- net/netfilter/nf_conntrack_standalone.c | 12 +++++++++--- net/netfilter/nf_tables_core.c | 11 ++++------- net/netfilter/nfnetlink_queue.c | 2 +- net/netfilter/xt_hashlimit.c | 12 +++++------- net/netfilter/xt_repldata.h | 2 +- 9 files changed, 79 insertions(+), 34 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/7 netfilter: xt_hashlimit: replace vmalloc calls with kvmalloc
From: Denis Kirjanov <[email protected]>
Replace vmalloc allocations with kvmalloc since kvmalloc is more flexible in memory allocation
--- net/netfilter/xt_hashlimit.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
net/netfilter/xt_hashlimit.c +5 -7
@@ -15,7 +15,6 @@#include <linux/random.h> #include <linux/jhash.h> #include <linux/slab.h> -#include <linux/vmalloc.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <linux/list.h>
@@ -294,8 +293,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,if (size <16 ) size =16 ; } -/* FIXME: don't use vmalloc() here or anywhere else -HW */ - hinfo =vmalloc (struct_size (hinfo, hash, size)); + hinfo =kvmalloc (struct_size (hinfo, hash, size),GFP_KERNEL );if (hinfo ==NULL )return -ENOMEM ; *out_hinfo = hinfo;
@@ -303,7 +301,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg,/* copy match config into hashtable config */ ret =cfg_copy (&hinfo->cfg, (void *)cfg,3 );if (ret) { -vfree (hinfo); +kvfree (hinfo);return ret; }
@@ -322,7 +320,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg, hinfo->rnd_initialized = false; hinfo->name =kstrdup (name,GFP_KERNEL );if (!hinfo->name) { -vfree (hinfo); +kvfree (hinfo);return -ENOMEM ; }spin_lock_init (&hinfo->lock);
@@ -344,7 +342,7 @@ static int htable_create(struct net *net, struct hashlimit_cfg3 *cfg, ops, hinfo);if (hinfo->pde ==NULL ) {kfree (hinfo->name); -vfree (hinfo); +kvfree (hinfo);return -ENOMEM ; } hinfo->net = net;
@@ -433,7 +431,7 @@ static void htable_put(struct xt_hashlimit_htable *hinfo)cancel_delayed_work_sync (&hinfo->gc_work);htable_selective_cleanup (hinfo, true);kfree (hinfo->name); -vfree (hinfo); +kvfree (hinfo); } }
Signature · 1 line
2.30.2
Re: [PATCH net-next 1/7] netfilter: xt_hashlimit: replace vmalloc calls with kvmalloc
This 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 10 lines · On Sun, 23 Mar 2025 11:09:16 +0100 you wrote:
From: Denis Kirjanov <[email protected]>
Replace vmalloc allocations with kvmalloc since kvmalloc is more flexible in memory allocation
Signed-off-by Denis Kirjanov <[email protected]>Reviewed-by Florian Westphal <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>[...]
Here is the summary with links:
- [net-next,1/7] netfilter: xt_hashlimit: replace vmalloc calls with kvmalloc
https://git.kernel.org/netdev/net-next/c/ddf8dec6db31 - [net-next,2/7] netfilter: conntrack: Bound nf_conntrack sysctl writes https://git.kernel.org/netdev/net-next/c/8b6861390ffe - [net-next,3/7] netfilter: fib: avoid lookup if socket is available https://git.kernel.org/netdev/net-next/c/eaaff9b6702e - [net-next,4/7] netfilter: nfnetlink_queue: Initialize ctx to avoid memory allocation error https://git.kernel.org/netdev/net-next/c/778b09d91baa - [net-next,5/7] netfilter: xtables: Use strscpy() instead of strscpy_pad() https://git.kernel.org/netdev/net-next/c/3b4aff61ca5d - [net-next,6/7] netfilter: socket: Lookup orig tuple for IPv6 SNAT https://git.kernel.org/netdev/net-next/c/932b32ffd760 - [net-next,7/7] netfilter: nf_tables: Only use nf_skip_indirect_calls() when MITIGATION_RETPOLINE https://git.kernel.org/netdev/net-next/c/e3a4182edd1a
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 2/7 netfilter: conntrack: Bound nf_conntrack sysctl writes
In reply to Pablo Neira AyusoFrom: Nicolas Bouchinet <[email protected]>
nf_conntrack_max and nf_conntrack_expect_max sysctls were authorized to be written any negative value, which would then be stored in the unsigned int variables nf_conntrack_max and nf_ct_expect_max variables.
While the do_proc_dointvec_conv function is supposed to limit writing handled by proc_dointvec proc_handler to INT_MAX. Such a negative value being written in an unsigned int leads to a very high value, exceeding this limit.
Moreover, the nf_conntrack_expect_max sysctl documentation specifies the minimum value is 1.
The proc_handlers have thus been updated to proc_dointvec_minmax in order to specify the following write bounds :
* Bound nf_conntrack_max sysctl writings between SYSCTL_ZERO and SYSCTL_INT_MAX.
* Bound nf_conntrack_expect_max sysctl writings between SYSCTL_ONE and SYSCTL_INT_MAX as defined in the sysctl documentation.
With this patch applied, sysctl writes outside the defined in the bound will thus lead to a write error :
``` sysctl -w net.netfilter.nf_conntrack_expect_max=-1 sysctl: setting key "net.netfilter.nf_conntrack_expect_max": Invalid argument ```
--- net/netfilter/nf_conntrack_standalone.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_standalone.c +9 -3
@@ -618,7 +618,9 @@ static struct ctl_table nf_ct_sysctl_table[] = { .data = &nf_conntrack_max, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, }, [NF_SYSCTL_CT_COUNT] = { .procname = "nf_conntrack_count",
@@ -654,7 +656,9 @@ static struct ctl_table nf_ct_sysctl_table[] = { .data = &nf_ct_expect_max, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ONE, + .extra2 = SYSCTL_INT_MAX, }, [NF_SYSCTL_CT_ACCT] = { .procname = "nf_conntrack_acct",
@@ -947,7 +951,9 @@ static struct ctl_table nf_ct_netfilter_table[] = { .data = &nf_conntrack_max, .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_INT_MAX, }, };
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/7 netfilter: fib: avoid lookup if socket is available
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
In case the fib match is used from the input hook we can avoid the fib lookup if early demux assigned a socket for us: check that the input interface matches sk-cached one.
Rework the existing 'lo bypass' logic to first check sk, then for loopback interface type to elide the fib lookup.
This speeds up fib matching a little, before:
93.08 GBit/s (no rules at all)
75.1 GBit/s ("fib saddr . iif oif missing drop" in prerouting) 75.62 GBit/s ("fib saddr . iif oif missing drop" in input)
After:
92.48 GBit/s (no rules at all)
75.62 GBit/s (fib rule in prerouting)
90.37 GBit/s (fib rule in input).
Numbers for the 'no rules' and 'prerouting' are expected to closely match in-between runs, the 3rd/input test case exercises the the 'avoid lookup if cached ifindex in sk matches' case.
Test used iperf3 via veth interface, lo can't be used due to existing loopback test.
--- include/net/netfilter/nft_fib.h | 21 +++++++++++++++++++++ net/ipv4/netfilter/nft_fib_ipv4.c | 11 +++++------ net/ipv6/netfilter/nft_fib_ipv6.c | 19 ++++++++++--------- 3 files changed, 36 insertions(+), 15 deletions(-)
include/net/netfilter/nft_fib.h +21 -0
@@ -18,6 +18,27 @@ nft_fib_is_loopback(const struct sk_buff *skb, const struct net_device *in)return skb->pkt_type ==PACKET_LOOPBACK || in->flags &IFF_LOOPBACK ; } +static inline bool nft_fib_can_skip (const struct nft_pktinfo *pkt) +{ +const struct net_device *indev =nft_in (pkt); +const struct sock *sk; + +switch (nft_hook (pkt)) { +case NF_INET_PRE_ROUTING : +case NF_INET_INGRESS : +case NF_INET_LOCAL_IN : +break ; +default : +return false; + } + + sk = pkt->skb->sk; +if (sk &&sk_fullsock (sk)) +return sk->sk_rx_dst_ifindex == indev->ifindex; + +return nft_fib_is_loopback (pkt->skb, indev); +} +int nft_fib_dump (struct sk_buff *skb,const struct nft_expr *expr,bool reset);int nft_fib_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[]);
net/ipv4/netfilter/nft_fib_ipv4.c +5 -6
@@ -71,6 +71,11 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,const struct net_device *oif;const struct net_device *found; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + } +/* * Do not set flowi4_oif, it restricts results (for example, asking * for oif 3 will get RTN_UNICAST result even if the daddr exits
@@ -85,12 +90,6 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,else oif =NULL ; -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING && -nft_fib_is_loopback (pkt->skb,nft_in (pkt))) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } - iph =skb_header_pointer (pkt->skb, noff,sizeof (_iph), &_iph);if (!iph) { regs->verdict.code =NFT_BREAK ;
net/ipv6/netfilter/nft_fib_ipv6.c +10 -9
@@ -170,6 +170,11 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,struct rt6_info *rt;int lookup_flags; +if (nft_fib_can_skip (pkt)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; + } +if (priv->flags &NFTA_FIB_F_IIF ) oif =nft_in (pkt);else if (priv->flags &NFTA_FIB_F_OIF )
@@ -181,17 +186,13 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,return ; } - lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); - -if (nft_hook (pkt) ==NF_INET_PRE_ROUTING || -nft_hook (pkt) ==NF_INET_INGRESS ) { -if (nft_fib_is_loopback (pkt->skb,nft_in (pkt)) || -nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { -nft_fib_store_result (dest, priv,nft_in (pkt)); -return ; - } +if (nft_fib_v6_skip_icmpv6 (pkt->skb, pkt->tprot, iph)) { +nft_fib_store_result (dest, priv,nft_in (pkt)); +return ; } + lookup_flags =nft_fib6_flowi_init (&fl6, priv, pkt, oif, iph); + *dest =0 ; rt = (void *)ip6_route_lookup (nft_net (pkt), &fl6, pkt->skb, lookup_flags);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/7 netfilter: nfnetlink_queue: Initialize ctx to avoid memory allocation error
In reply to Pablo Neira AyusoFrom: Chenyuan Yang <[email protected]>
It is possible that ctx in nfqnl_build_packet_message() could be used before it is properly initialize, which is only initialized by nfqnl_get_sk_secctx().
This patch corrects this problem by initializing the lsmctx to a safe value when it is declared.
This is similar to the commit 35fcac7a7c25
("audit: Initialize lsmctx to avoid memory allocation error").
--- net/netfilter/nfnetlink_queue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nfnetlink_queue.c +1 -1
@@ -567,7 +567,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,enum ip_conntrack_info ctinfo =0 ;const struct nfnl_ct_hook *nfnl_ct;bool csum_verify; -struct lsm_context ctx; +struct lsm_context ctx = {NULL ,0 ,0 };int seclen =0 ;ktime_t tstamp;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/7 netfilter: xtables: Use strscpy() instead of strscpy_pad()
In reply to Pablo Neira AyusoFrom: Thorsten Blum <[email protected]>
kzalloc() already zero-initializes the destination buffer, making strscpy() sufficient for safely copying the name. The additional NUL- padding performed by strscpy_pad() is unnecessary.
The size parameter is optional, and strscpy() automatically determines the size of the destination buffer using sizeof() if the argument is omitted. This makes the explicit sizeof() call unnecessary; remove it.
No functional changes intended.
--- net/netfilter/xt_repldata.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/xt_repldata.h +1 -1
@@ -29,7 +29,7 @@if (tbl ==NULL ) \return NULL ; \ term = (struct type##_error *)&(((char *)tbl)[term_offset]); \ -strscpy_pad (tbl->repl.name, info->name,sizeof (tbl->repl.name)); \ +strscpy (tbl->repl.name, info->name); \ *term = (struct type##_error)typ2##_ERROR_INIT; \ tbl->repl.valid_hooks = hook_mask; \ tbl->repl.num_entries = nhooks +1 ; \
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/7 netfilter: socket: Lookup orig tuple for IPv6 SNAT
In reply to Pablo Neira AyusoFrom: Maxim Mikityanskiy <[email protected]>
nf_sk_lookup_slow_v4 does the conntrack lookup for IPv4 packets to restore the original 5-tuple in case of SNAT, to be able to find the right socket (if any). Then socket_match() can correctly check whether the socket was transparent.
However, the IPv6 counterpart (nf_sk_lookup_slow_v6) lacks this conntrack lookup, making xt_socket fail to match on the socket when the packet was SNATed. Add the same logic to nf_sk_lookup_slow_v6.
IPv6 SNAT is used in Kubernetes clusters for pod-to-world packets, as pods' addresses are in the fd00::/8 ULA subnet and need to be replaced with the node's external address. Cilium leverages Envoy to enforce L7 policies, and Envoy uses transparent sockets. Cilium inserts an iptables prerouting rule that matches on -m socket --transparent and redirects the packets to localhost, but it fails to match SNATed IPv6 packets due to that missing conntrack lookup.
--- net/ipv6/netfilter/nf_socket_ipv6.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
net/ipv6/netfilter/nf_socket_ipv6.c +23 -0
@@ -103,6 +103,10 @@ struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,struct sk_buff *data_skb =NULL ;int doff =0 ;int thoff =0 , tproto; +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +enum ip_conntrack_info ctinfo; +struct nf_conn const *ct; +#endif tproto =ipv6_find_hdr (skb, &thoff,-1 ,NULL ,NULL );if (tproto <0 ) {
@@ -136,6 +140,25 @@ struct sock *nf_sk_lookup_slow_v6(struct net *net, const struct sk_buff *skb,return NULL ; } +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +/* Do the lookup with the original socket address in +* case this is a reply packet of an established +* SNAT-ted connection. +*/ + ct =nf_ct_get (skb, &ctinfo); +if (ct && + ((tproto !=IPPROTO_ICMPV6 && + ctinfo ==IP_CT_ESTABLISHED_REPLY ) || + (tproto ==IPPROTO_ICMPV6 && + ctinfo ==IP_CT_RELATED_REPLY )) && + (ct->status &IPS_SRC_NAT_DONE )) { + daddr = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u3.in6; + dport = (tproto ==IPPROTO_TCP ) ? + ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u.tcp.port : + ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple.src.u.udp.port; + } +#endif +return nf_socket_get_sock_v6 (net, data_skb, doff, tproto, saddr, daddr, sport, dport, indev); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/7 netfilter: nf_tables: Only use nf_skip_indirect_calls() when MITIGATION_RETPOLINE
In reply to Pablo Neira AyusoFrom: WangYuli <[email protected]>
1. MITIGATION_RETPOLINE is x86-only (defined in arch/x86/Kconfig), so no need to AND with CONFIG_X86 when checking if enabled.
2. Remove unused declaration of nf_skip_indirect_calls() when MITIGATION_RETPOLINE is disabled to avoid warnings.
3. Declare nf_skip_indirect_calls() and nf_skip_indirect_calls_enable() as inline when MITIGATION_RETPOLINE is enabled, as they are called only once and have simple logic.
Fix follow error with clang-21 when W=1e:
net/netfilter/nf_tables_core.c:39:20: error: unused function 'nf_skip_indirect_calls' [-Werror,-Wunused-function]
39 | static inline bool nf_skip_indirect_calls(void) { return false; }
| ^~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
make[4]: *** [scripts/Makefile.build:207: net/netfilter/nf_tables_core.o] Error 1 make[3]: *** [scripts/Makefile.build:465: net/netfilter] Error 2 make[3]: *** Waiting for unfinished jobs....
--- net/netfilter/nf_tables_core.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_core.c +4 -7
@@ -21,25 +21,22 @@#include <net/netfilter/nf_log.h> #include <net/netfilter/nft_meta.h> -#if defined(CONFIG_MITIGATION_RETPOLINE ) && defined(CONFIG_X86 ) - +#ifdef CONFIG_MITIGATION_RETPOLINE static struct static_key_false nf_tables_skip_direct_calls; -static bool nf_skip_indirect_calls (void ) +static inline bool nf_skip_indirect_calls (void ) {return static_branch_likely (&nf_tables_skip_direct_calls); } -static void __init nf_skip_indirect_calls_enable (void ) +static inline void __init nf_skip_indirect_calls_enable (void ) {if (!cpu_feature_enabled (X86_FEATURE_RETPOLINE ))static_branch_enable (&nf_tables_skip_direct_calls); }#else -static inline bool nf_skip_indirect_calls (void ) {return false; } -static inline void nf_skip_indirect_calls_enable (void ) { } -#endif +#endif /* CONFIG_MITIGATION_RETPOLINE */ static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt, const struct nft_verdict *verdict,
Signature · 1 line
2.30.2