Netfilter/IPVS updates for net-next
Subsystems (6)
Pablo Neira Ayuso
maintainercover Netfilter/IPVS updates for net-next
Hi,
The following patchset contains Netfilter/IPVS updates for net-next, this contains updates to address sashiko reports in IPVS and Netfilter on possible pre-existing issues. This also includes a series to add refcount for ct helper and timeout to deal with a corner case scenario with unconfirmed conntracks flying to nfqueue.
1) Add a conn_max sysctl to IPVS to limit the maximum number of connections, from Julian Anastasov.
2) Use get_unaligned_be16() to access TCP MSS in nfnetlink_osf, from Fernando Fernandez Mancera.
3) Use {READ,WRITE}_ONCE to access helper flags from nfnetlink_helper.
Several patches for the synproxy infrastructure, from Fernando Fernandez Mancera:
4) Drop packet if TCP timestamp adjustment fails.
5) Continue parsing of TCP timestamp to deal with possible duplicates.
6) Use {get,put}_unaligned_be32() to acess the TCP timestamp.
7) Hold ct->lock to initialize nf_ct_seqadj_init().
Updates for the ct timeout infrastructure, to deal with a corner case for unconfirmed conntracks flying to nfqueue:
8) Add a refcount to track ct timeout policy use by ct extension, release the timeout until the last ct extension drops the refcnt on it.
Similar update for the ct helper infrastructure:
9) Dynamic allocation of ct helpers, as a preparation for adding refcount to track ct extension use.
10) Move destroy_sibling_or_exp() to nf_conntrack_proto_gre, so pptp conntrack helper module removal does not make this code unreachable via the helper->destroy callback. This is another dependency for the new refcount coming in this series.
11) Add a refcount to track use of it from the ct extension, then ct helper and timeout is reachable to the connection until it goes away.
12) Remove the genid infrastructure in ct extensions. The primary goal was to detect that a ct extension such as ct timeout and ct helper went stale for unconfirmed conntrack, either because object or module was removed. This deactivates all ct extensions though for this unconfirmed conntrack.
13) Call nf_ct_gre_keymap_destroy() if this is a master conntrack with a pptp helper only.
sashiko.dev reports one more relevant issue when unsetting the helper via ctnetlink that I will address in a follow up patch.
Then, two more assorted updates:
14) Avoid a unlikely underflow in bridge VLAN untag, only possible if buggy bridge VLAN filtering is buggy, remove WARN_ON_ONCE while at it. From David Carlier.
15) Use get_unaligned_be32() in nf_conntrack_tcp to access sack extension, from Rosen Penev.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-26-06-07
Thanks.
----------------------------------------------------------------
The following changes since commit bfa3d89cc15c09f7d1581c834a5ed725189ec19f:
Merge tag 'batadv-next-pullrequest-20260603' of https://git.open-mesh.org/batadv (2026-06-04 19:14:35 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git tags/nf-next-26-06-07
for you to fetch changes up to d3bf9eae486490832bd08fd62ab0ac601f346bd4:
netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack() (2026-06-07 11:13:47 +0200)
----------------------------------------------------------------
netfilter pull request 26-06-07
----------------------------------------------------------------
David Carlier (1):
netfilter: flowtable: avoid num_encaps underflow on bridge VLAN untag
Fernando Fernandez Mancera (5):
netfilter: nfnetlink_osf: fix mss parsing on big-endian architectures
netfilter: synproxy: drop packets if timestamp adjustment fails
netfilter: synproxy: adjust duplicate timestamp options
netfilter: synproxy: fix unaligned memory access in timestamp adjustment
netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
Julian Anastasov (1):
ipvs: add conn_max sysctl to limit connections
Pablo Neira Ayuso (7):
netfilter: nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags
netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
netfilter: nf_conntrack_helper: dynamically allocate struct nf_conntrack_helper
netfilter: nf_conntrack_pptp: move GRE specific cleanup to GRE tracker
netfilter: nf_conntrack_helper: add refcounting from datapath
netfilter: conntrack: revert ct extension genid infrastructure
netfilter: conntrack: call nf_ct_gre_keymap_destroy() if master helper is pptp
Rosen Penev (1):
netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack()
Documentation/networking/ipvs-sysctl.rst | 35 +++++++ include/net/ip_vs.h | 22 +++++ include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 4 + include/net/netfilter/nf_conntrack_extend.h | 12 --- include/net/netfilter/nf_conntrack_helper.h | 42 ++++++--- include/net/netfilter/nf_conntrack_timeout.h | 27 +++++- net/ipv4/netfilter/nf_nat_snmp_basic_main.c | 27 +++--- net/netfilter/ipvs/ip_vs_conn.c | 10 +- net/netfilter/ipvs/ip_vs_ctl.c | 53 +++++++++++ net/netfilter/nf_conntrack_amanda.c | 39 +++----- net/netfilter/nf_conntrack_core.c | 92 +++++------------- net/netfilter/nf_conntrack_extend.c | 32 +------ net/netfilter/nf_conntrack_ftp.c | 5 +- net/netfilter/nf_conntrack_h323_main.c | 107 +++++++++------------ net/netfilter/nf_conntrack_helper.c | 125 +++++++++++++++++-------- net/netfilter/nf_conntrack_irc.c | 5 +- net/netfilter/nf_conntrack_netbios_ns.c | 20 ++-- net/netfilter/nf_conntrack_netlink.c | 28 ++++-- net/netfilter/nf_conntrack_ovs.c | 9 +- net/netfilter/nf_conntrack_pptp.c | 83 +++------------- net/netfilter/nf_conntrack_proto.c | 15 ++- net/netfilter/nf_conntrack_proto_gre.c | 61 ++++++++++++ net/netfilter/nf_conntrack_proto_tcp.c | 10 +- net/netfilter/nf_conntrack_sane.c | 5 +- net/netfilter/nf_conntrack_seqadj.c | 2 + net/netfilter/nf_conntrack_sip.c | 5 +- net/netfilter/nf_conntrack_snmp.c | 21 ++--- net/netfilter/nf_conntrack_tftp.c | 5 +- net/netfilter/nf_conntrack_timeout.c | 27 +++++- net/netfilter/nf_flow_table_path.c | 3 +- net/netfilter/nf_synproxy_core.c | 40 ++++---- net/netfilter/nfnetlink_cthelper.c | 79 ++++++++-------- net/netfilter/nfnetlink_cttimeout.c | 112 ++++++++++------------ net/netfilter/nfnetlink_osf.c | 6 +- net/netfilter/nft_ct.c | 10 +- net/netfilter/xt_CT.c | 3 - 36 files changed, 653 insertions(+), 528 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/15 ipvs: add conn_max sysctl to limit connections
From: Julian Anastasov <[email protected]>
Currently, we are using atomic_t to track the number of connections. On 64-bit setups with large memory there is a risk this counter to overflow. Also, setups with many containers may need to tune the limit for connections.
Add sysctl control to limit the number of connections to 1,073,741,824 (64-bit) and 16,777,216 (32-bit). Depending on the admin's privilege, the value is used to change a soft or hard limit allowing unprivileged admins to change the soft limit in range determined by privileged admins.
--- Documentation/networking/ipvs-sysctl.rst | 35 ++++++++++++++++ include/net/ip_vs.h | 22 ++++++++++ net/netfilter/ipvs/ip_vs_conn.c | 10 ++++- net/netfilter/ipvs/ip_vs_ctl.c | 53 ++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-)
Documentation/networking/ipvs-sysctl.rst +35 -0
@@ -56,6 +56,41 @@ conn_lfactor - INTEGER -4: grow if load goes above 6% (buckets = nodes * 16) 2: grow if load goes above 400% (buckets = nodes / 4) +conn_max - INTEGER + Limit for number of connections, per netns. + + Controls the soft and hard limit for number of connections. + Initially, the platform specific limit is assigned for init_net. + The value can be changed and later the soft limit propagated + to other networking namespaces. + + Privileged admin can change both limits up to the value of the + platform limit while the unprivileged admin can change only the + soft limit up to the value of the hard limit. + + For setups using conntrack=1 (CONFIG_IP_VS_NFCT for + Netfilter connection tracking) the connections can be + limited also by nf_conntrack_max. + + soft limit hard limit + ===================================================== + init_net: + create netns platform platform + priv admin 0 .. platform 0 .. platform + ===================================================== + new netns: + create netns init_net:soft init_net:soft + priv admin 0 .. platform 0 .. platform + unpriv admin 0 .. hard N/A + + Limits per platform: + 1,073,741,824 (2^30 for 64-bit) + 16,777,216 (2^24 for 32-bit) + + Possible values: 0 .. platform limit + + Default: platform limit + conn_reuse_mode - INTEGER 1 - default
include/net/ip_vs.h +22 -0
@@ -44,6 +44,14 @@#define IP_VS_CONN_TAB_MAX_BITS 20#endif +/* conn_max limits */ +#if BITS_PER_LONG >32 +/* Limit of atomic_t but restricted by roundup_pow_of_two() in ip_vs_core.c */ +#define IP_VS_CONN_MAX (1 << 30) +#else +#define IP_VS_CONN_MAX (1 << 24) +#endif +/* svc_table limits */ #define IP_VS_SVC_TAB_MIN_BITS 4#define IP_VS_SVC_TAB_MAX_BITS 20
@@ -1220,6 +1228,10 @@ struct netns_ipvs {/* sysctl variables */ int sysctl_amemthresh;int sysctl_am_droprate; +#ifdef CONFIG_SYSCTL +int sysctl_conn_max;/* soft limit for conns */ +int conn_max_limit;/* hard limit for conn_max */ +#endif int sysctl_drop_entry;int sysctl_drop_packet;int sysctl_secure_tcp;
@@ -1317,6 +1329,11 @@ struct netns_ipvs {#ifdef CONFIG_SYSCTL +static inline int sysctl_conn_max (struct netns_ipvs *ipvs) +{ +return READ_ONCE (ipvs->sysctl_conn_max); +} +static inline int sysctl_sync_threshold (struct netns_ipvs *ipvs) {return ipvs->sysctl_sync_threshold[0 ];
@@ -1436,6 +1453,11 @@ static inline int sysctl_est_nice(struct netns_ipvs *ipvs)#else +static inline int sysctl_conn_max (struct netns_ipvs *ipvs) +{ +return IP_VS_CONN_MAX ; +} +static inline int sysctl_sync_threshold (struct netns_ipvs *ipvs) {return DEFAULT_SYNC_THRESHOLD ;
net/netfilter/ipvs/ip_vs_conn.c +9 -1
@@ -1358,9 +1358,18 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af,struct netns_ipvs *ipvs = p->ipvs;struct ip_vs_proto_data *pd =ip_vs_proto_data_get (p->ipvs, p->protocol); +/* Increment conn_count up to conn_max */ +int count =atomic_read (&ipvs->conn_count); +int max =sysctl_conn_max (ipvs); + +do { +if (count >= max) +return NULL ; + }while (!atomic_try_cmpxchg (&ipvs->conn_count, &count, count +1 )); cp =kmem_cache_alloc (ip_vs_conn_cachep,GFP_ATOMIC );if (cp ==NULL ) { +atomic_dec (&ipvs->conn_count);IP_VS_ERR_RL ("%s(): no memory\n" , __func__);return NULL ; }
@@ -1414,7 +1423,6 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af, cp->in_seq.delta =0 ; cp->out_seq.delta =0 ; -atomic_inc (&ipvs->conn_count);if (unlikely (flags &IP_VS_CONN_F_NO_CPORT )) {int af_id =ip_vs_af_index (cp->af);
net/netfilter/ipvs/ip_vs_ctl.c +53 -0
71 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH net-next 01/15] ipvs: add conn_max sysctl to limit connections
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 8 lines · On Sun, 7 Jun 2026 11:49:40 +0200 you wrote:
From: Julian Anastasov <[email protected]>
Currently, we are using
atomic_tto track the number of connections. On 64-bit setups with large memory there is a risk this counter to overflow. Also, setups with many containers may need to tune the limit for connections.[...]
Here is the summary with links:
- [net-next,01/15] ipvs: add conn_max sysctl to limit connections
https://git.kernel.org/netdev/net-next/c/4a15044a2b06 - [net-next,02/15] netfilter: nfnetlink_osf: fix mss parsing on big-endian architectures https://git.kernel.org/netdev/net-next/c/a625c94144c9 - [net-next,03/15] netfilter: nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags https://git.kernel.org/netdev/net-next/c/f8bf5edf7157 - [net-next,04/15] netfilter: synproxy: drop packets if timestamp adjustment fails https://git.kernel.org/netdev/net-next/c/63d29ee95c4a - [net-next,05/15] netfilter: synproxy: adjust duplicate timestamp options https://git.kernel.org/netdev/net-next/c/22bb132cfb9b - [net-next,06/15] netfilter: synproxy: fix unaligned memory access in timestamp adjustment https://git.kernel.org/netdev/net-next/c/992c20bc8a4a - [net-next,07/15] netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock https://git.kernel.org/netdev/net-next/c/9e37388b8070 - [net-next,08/15] netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount https://git.kernel.org/netdev/net-next/c/7d6a9cdb8d3a - [net-next,09/15] netfilter: nf_conntrack_helper: dynamically allocate struct nf_conntrack_helper https://git.kernel.org/netdev/net-next/c/6031487d4e27 - [net-next,10/15] netfilter: nf_conntrack_pptp: move GRE specific cleanup to GRE tracker https://git.kernel.org/netdev/net-next/c/fe97fd540a03 - [net-next,11/15] netfilter: nf_conntrack_helper: add refcounting from datapath https://git.kernel.org/netdev/net-next/c/ac46f3f35b6e - [net-next,12/15] netfilter: conntrack: revert ct extension genid infrastructure https://git.kernel.org/netdev/net-next/c/35e21a4dccc5 - [net-next,13/15] netfilter: conntrack: call nf_ct_gre_keymap_destroy() if master helper is pptp https://git.kernel.org/netdev/net-next/c/b0f02608fbcd - [net-next,14/15] netfilter: flowtable: avoid num_encaps underflow on bridge VLAN untag https://git.kernel.org/netdev/net-next/c/e052f920773b - [net-next,15/15] netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack() https://git.kernel.org/netdev/net-next/c/d3bf9eae4864
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/15 netfilter: nfnetlink_osf: fix mss parsing on big-endian architectures
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
The MSS calculation in nf_osf_match_one() manually shifts bytes to construct a 16-bit value before passing it to ntohs().
This works on little-endian hosts but it does not work on big-endian as the bytes are being always shifted and set in the same way for all architectures.
Use get_unaligned_be16() to fix this on big-endian systems. It also simplifies the code.
--- net/netfilter/nfnetlink_osf.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
net/netfilter/nfnetlink_osf.c +1 -5
@@ -95,11 +95,7 @@ static bool nf_osf_match_one(const struct sk_buff *skb,switch (*optp) {case OSFOPT_MSS : - mss = optp[3 ]; - mss <<=8 ; - mss |= optp[2 ]; - - mss =ntohs ((__force __be16 )mss); + mss =get_unaligned_be16 (&optp[2 ]);break ;case OSFOPT_TS :break ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 3/15 netfilter: nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags
In reply to Pablo Neira AyusoConntrack helper flags are accessed from packet and netlink dump path. Concurrent update of userspace helper flags is not possible, because the nfnl_mutex in held on updates. These flags are only used by userspace helpers. Use {READ,WRITE}_ONCE() to access this flags from lockless paths.
--- net/netfilter/nf_conntrack_core.c | 4 +++- net/netfilter/nfnetlink_cthelper.c | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-)
net/netfilter/nf_conntrack_core.c +3 -1
@@ -2213,6 +2213,7 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct, {const struct nf_conntrack_helper *helper;const struct nf_conn_help *help; +unsigned int helper_flags;int protoff; help =nfct_help (ct);
@@ -2223,7 +2224,8 @@ static int nf_confirm_cthelper(struct sk_buff *skb, struct nf_conn *ct,if (!helper)return NF_ACCEPT ; -if (!(helper->flags &NF_CT_HELPER_F_USERSPACE )) + helper_flags =READ_ONCE (helper->flags); +if (!(helper_flags &NF_CT_HELPER_F_USERSPACE ))return NF_ACCEPT ;switch (nf_ct_l3num (ct)) {
net/netfilter/nfnetlink_cthelper.c +13 -7
@@ -41,8 +41,9 @@ static int nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff, struct nf_conn *ct, enum ip_conntrack_info ctinfo) { - const struct nf_conn_help *help; struct nf_conntrack_helper *helper; +const struct nf_conn_help *help; +unsigned int helper_flags; help =nfct_help (ct);if (help ==NULL )
@@ -53,8 +54,10 @@ nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff,if (helper ==NULL )return NF_DROP ; + helper_flags =READ_ONCE (helper->flags); +/* This is a user-space helper not yet configured, skip. */ -if ((helper->flags & +if ((helper_flags & (NF_CT_HELPER_F_USERSPACE |NF_CT_HELPER_F_CONFIGURED )) ==NF_CT_HELPER_F_USERSPACE )return NF_ACCEPT ;
@@ -404,10 +407,10 @@ nfnl_cthelper_update(const struct nlattr * const tb[],switch (status) {case NFCT_HELPER_STATUS_ENABLED : - helper->flags |=NF_CT_HELPER_F_CONFIGURED ; +WRITE_ONCE (helper->flags, helper->flags |NF_CT_HELPER_F_CONFIGURED );break ;case NFCT_HELPER_STATUS_DISABLED : - helper->flags &= ~NF_CT_HELPER_F_CONFIGURED ; +WRITE_ONCE (helper->flags, helper->flags & ~NF_CT_HELPER_F_CONFIGURED );break ; } }
@@ -529,8 +532,8 @@ static int nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, int event, struct nf_conntrack_helper *helper) { - struct nlmsghdr *nlh; unsigned int flags = portid ? NLM_F_MULTI : 0; +struct nlmsghdr *nlh;int status; event =nfnl_msg_type (NFNL_SUBSYS_CTHELPER , event);
@@ -554,7 +557,7 @@ nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,if (nla_put_be32 (skb,NFCTH_PRIV_DATA_LEN ,htonl (helper->data_len))) goto nla_put_failure; -if (helper->flags &NF_CT_HELPER_F_CONFIGURED ) +if (READ_ONCE (helper->flags) &NF_CT_HELPER_F_CONFIGURED ) status =NFCT_HELPER_STATUS_ENABLED ;else status =NFCT_HELPER_STATUS_DISABLED ;
@@ -575,6 +578,7 @@ static int nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb) { struct nf_conntrack_helper *cur, *last; +unsigned int helper_flags;rcu_read_lock (); last = (struct nf_conntrack_helper *)cb->args[1 ];
@@ -583,8 +587,10 @@ nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb)hlist_for_each_entry_rcu (cur, &nf_ct_helper_hash[cb->args[0 ]], hnode) { + helper_flags =READ_ONCE (cur->flags); +/* skip non-userspace conntrack helpers. */ -if (!(cur->flags &NF_CT_HELPER_F_USERSPACE )) +if (!(helper_flags &NF_CT_HELPER_F_USERSPACE ))continue ;if (cb->args[1 ]) {
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 4/15 netfilter: synproxy: drop packets if timestamp adjustment fails
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
If a packet was malformed or if skb_ensure_writable() failed, the synproxy_tstamp_adjust() function returned 0 indicating an error but it was ignored on the callers.
Make the function return a boolean instead to clarify the result and drop the packet if synproxy_tstamp_adjust() failed due to ENOMEM from skb_ensure_writable(). In addition, if there are malformed options, skip the tstamp update but do not drop the packet as that should be done by the policy directly.
--- net/netfilter/nf_synproxy_core.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
net/netfilter/nf_synproxy_core.c +13 -9
@@ -184,7 +184,7 @@ synproxy_check_timestamp_cookie(struct synproxy_options *opts) opts->options |= opts->tsecr & (1 <<5 ) ?NF_SYNPROXY_OPT_ECN :0 ; } -static unsigned int +static bool synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, struct tcphdr *th, struct nf_conn *ct, enum ip_conntrack_info ctinfo,
@@ -194,13 +194,13 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,__be32 *ptr, old;if (synproxy->tsoff ==0 ) -return 1 ; +return true; optoff = protoff +sizeof (struct tcphdr ); optend = protoff + th->doff *4 ;if (skb_ensure_writable (skb, optend)) -return 0 ; +return false; th = (struct tcphdr *)(skb->data + protoff);
@@ -209,7 +209,7 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,switch (op[0 ]) {case TCPOPT_EOL : -return 1 ; +return true;case TCPOPT_NOP : optoff++;continue ;
@@ -217,7 +217,7 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,if (optoff +1 == optend || optoff + op[1 ] > optend || op[1 ] <2 ) -return 0 ; +return true;if (op[0 ] ==TCPOPT_TIMESTAMP && op[1 ] ==TCPOLEN_TIMESTAMP ) {if (CTINFO2DIR (ctinfo) ==IP_CT_DIR_REPLY ) {
@@ -233,12 +233,12 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, }inet_proto_csum_replace4 (&th->check, skb, old, *ptr, false); -return 1 ; +return true; } optoff += op[1 ]; } } -return 1 ; +return true; }#ifdef CONFIG_PROC_FS
@@ -749,7 +749,9 @@ ipv4_synproxy_hook(void *priv, struct sk_buff *skb,break ; } -synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy); +if (!synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy)) +return NF_DROP_REASON (skb,SKB_DROP_REASON_NETFILTER_DROP ,ENOMEM ); +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (ipv4_synproxy_hook);
@@ -1177,7 +1179,9 @@ ipv6_synproxy_hook(void *priv, struct sk_buff *skb,break ; } -synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy); +if (!synproxy_tstamp_adjust (skb, thoff, th, ct, ctinfo, synproxy)) +return NF_DROP_REASON (skb,SKB_DROP_REASON_NETFILTER_DROP ,ENOMEM ); +return NF_ACCEPT ; }EXPORT_SYMBOL_GPL (ipv6_synproxy_hook);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 5/15 netfilter: synproxy: adjust duplicate timestamp options
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
RFC 9293 does not mention anything about duplicated options and each networking stack handles it in their own way. Currently, Linux kernel is processing options sequentially and in case of duplicated timestamp options, the value from the latest one overrides the others.
As SYNPROXY is modifying only the first timestamp option found, a packet can reach the backend server and it might parse the wrong timestamp value. Let's just continue parsing the following options and in case a duplicated timestamp is found, adjust it too.
--- net/netfilter/nf_synproxy_core.c | 1 - 1 file changed, 1 deletion(-)
net/netfilter/nf_synproxy_core.c +0 -1
@@ -233,7 +233,6 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, }inet_proto_csum_replace4 (&th->check, skb, old, *ptr, false); -return true; } optoff += op[1 ]; }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 6/15 netfilter: synproxy: fix unaligned memory access in timestamp adjustment
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
Use get_unaligned_be32() and put_unaligned_be32() to safely read and write the timestamp fields. This prevents performance degradation due to unaligned memory access or even a crash on strict alignment architectures.
This follows the implementation of timestamp parsing in the networking stack at tcp_parse_options() and synproxy_parse_options().
--- net/netfilter/nf_synproxy_core.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
net/netfilter/nf_synproxy_core.c +9 -10
@@ -191,7 +191,7 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff, const struct nf_conn_synproxy *synproxy) {unsigned int optoff, optend; -__be32 *ptr, old; +u32 new, old;if (synproxy->tsoff ==0 )return true;
@@ -221,18 +221,17 @@ synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,if (op[0 ] ==TCPOPT_TIMESTAMP && op[1 ] ==TCPOLEN_TIMESTAMP ) {if (CTINFO2DIR (ctinfo) ==IP_CT_DIR_REPLY ) { - ptr = (__be32 *)&op[2 ]; - old = *ptr; - *ptr =htonl (ntohl (*ptr) - - synproxy->tsoff); + old =get_unaligned_be32 (&op[2 ]); + new = old - synproxy->tsoff; +put_unaligned_be32 (new, &op[2 ]); }else { - ptr = (__be32 *)&op[6 ]; - old = *ptr; - *ptr =htonl (ntohl (*ptr) + - synproxy->tsoff); + old =get_unaligned_be32 (&op[6 ]); + new = old + synproxy->tsoff; +put_unaligned_be32 (new, &op[6 ]); }inet_proto_csum_replace4 (&th->check, skb, - old, *ptr, false); +cpu_to_be32 (old), +cpu_to_be32 (new), false); } optoff += op[1 ]; }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 7/15 netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
In reply to Pablo Neira AyusoFrom: Fernando Fernandez Mancera <[email protected]>
nf_ct_seqadj_init() is called without holding the ct lock. This can race with nf_ct_seq_adjust() when a connection is in CLOSE state due to an RST or connection reopening. In addition for SYN_RECV state, concurrent processing of packets can trigger nf_ct_seq_adjust() too. These situations create a read/write data race.
As synproxy is the only user of nf_ct_seqadj_init() at the moment, fix this by holding ct->lock inside nf_ct_seqadj_init() until all is done.
--- net/netfilter/nf_conntrack_seqadj.c | 2 ++ 1 file changed, 2 insertions(+)
net/netfilter/nf_conntrack_seqadj.c +2 -0
@@ -17,12 +17,14 @@ int nf_ct_seqadj_init(struct nf_conn *ct, enum ip_conntrack_info ctinfo,if (off ==0 )return 0 ; +spin_lock_bh (&ct->lock);set_bit (IPS_SEQ_ADJUST_BIT , &ct->status); seqadj =nfct_seqadj (ct); this_way = &seqadj->seq[dir]; this_way->offset_before = off; this_way->offset_after = off; +spin_unlock_bh (&ct->lock);return 0 ; }EXPORT_SYMBOL_GPL (nf_ct_seqadj_init);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 8/15 netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
In reply to Pablo Neira AyusoAdd a refcount for struct nf_ct_timeout which is used by ct extension to set the custom ct timeout policy, this tells us that the ct timeout is being used by a conntrack entry. When the last conntrack entry drops the refcount on the ct timeout, the ct timeout is released.
Remove the refcount for control plane which controls if the ruleset refers to the timeout policy. After this update, it is possible to remove the ct timeout policy from nfnetlink_cttimeout immediately. This is for simplicity not to handle two refcounts on a single object.
Remove nf_queue_nf_hook_drop(): a packet sitting in nfqueue will just hold a reference to the nf_ct_timeout object until packet is reinjected, since this is part of the ct extension, this will be released by the time the conntrack is freed.
nf_ct_untimeout() is still called to clean up in a best effort basis:
the ct timeout on existing entries gets removed when the ct timeout goes away, but as long as the iptables ruleset still refers to the ct timeout through a template, new conntracks may keep attaching it and extend its lifetime until the rule is removed.
nf_ct_untimeout() is not called anymore from module removal path, this is unlikely to find timeouts give module refcount is bumped, and the new refcount already tracks the ct timeout policy use so it is released when unused.
--- include/net/netfilter/nf_conntrack_timeout.h | 27 ++++- net/netfilter/nf_conntrack_core.c | 6 +- net/netfilter/nf_conntrack_timeout.c | 27 ++++- net/netfilter/nfnetlink_cttimeout.c | 112 +++++++++---------- net/netfilter/nft_ct.c | 7 +- net/netfilter/xt_CT.c | 2 +- 6 files changed, 107 insertions(+), 74 deletions(-)
include/net/netfilter/nf_conntrack_timeout.h +25 -2
@@ -12,6 +12,7 @@#define CTNL_TIMEOUT_NAME_MAX 32struct nf_ct_timeout { +refcount_t refcnt;__u16 l3num;const struct nf_conntrack_l4proto *l4proto;struct rcu_head rcu;
@@ -22,6 +23,22 @@ struct nf_conn_timeout {struct nf_ct_timeout __rcu *timeout; }; +static inline void nf_ct_timeout_put (const struct nf_conn *ct) +{ +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT +struct nf_conn_timeout *timeout_ext; +struct nf_ct_timeout *timeout; + + timeout_ext =nf_ct_ext_find (ct,NF_CT_EXT_TIMEOUT ); +if (!timeout_ext) +return ; + + timeout =rcu_dereference (timeout_ext->timeout); +if (timeout &&refcount_dec_and_test (&timeout->refcnt)) +kfree_rcu (timeout, rcu); +#endif +} +static inline unsigned int *nf_ct_timeout_data (const struct nf_conn_timeout *t) {
@@ -56,8 +73,14 @@ struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,#ifdef CONFIG_NF_CONNTRACK_TIMEOUT struct nf_conn_timeout *timeout_ext; +if (!timeout) +return NULL ; + timeout_ext =nf_ct_ext_add (ct,NF_CT_EXT_TIMEOUT , gfp); -if (timeout_ext ==NULL ) +if (!timeout_ext || timeout_ext->timeout) +return NULL ; + +if (!refcount_inc_not_zero (&timeout->refcnt))return NULL ;rcu_assign_pointer (timeout_ext->timeout, timeout);
@@ -75,7 +98,7 @@ static inline unsigned int *nf_ct_timeout_lookup(const struct nf_conn *ct)struct nf_conn_timeout *timeout_ext; timeout_ext =nf_ct_timeout_find (ct); -if (timeout_ext) +if (timeout_ext &&rcu_access_pointer (timeout_ext->timeout)) timeouts =nf_ct_timeout_data (timeout_ext);#endif return timeouts;
net/netfilter/nf_conntrack_core.c +4 -2
@@ -1737,16 +1737,18 @@ void nf_conntrack_free(struct nf_conn *ct)*/ WARN_ON (refcount_read (&ct->ct_general.use) !=0 ); +rcu_read_lock ();if (ct->status &IPS_SRC_NAT_DONE ) {const struct nf_nat_hook *nat_hook; -rcu_read_lock (); nat_hook =rcu_dereference (nf_nat_hook);if (nat_hook) nat_hook->remove_nat_bysrc (ct); -rcu_read_unlock (); } +nf_ct_timeout_put (ct); +rcu_read_unlock (); +kfree (ct->ext);kmem_cache_free (nf_conntrack_cachep, ct); cnet =nf_ct_pernet (net);
net/netfilter/nf_conntrack_timeout.c +24 -3
@@ -25,17 +25,32 @@const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook__read_mostly ;EXPORT_SYMBOL_GPL (nf_ct_timeout_hook); +/* nf_ct_iterate_cleanup() holds the conntrack lock. */ static int untimeout (struct nf_conn *ct,void *timeout) {struct nf_conn_timeout *timeout_ext =nf_ct_timeout_find (ct);if (timeout_ext) { -const struct nf_ct_timeout *t; +struct nf_ct_timeout *t; - t =rcu_access_pointer (timeout_ext->timeout); +rcu_read_lock (); + t =rcu_dereference (timeout_ext->timeout); +if (!t) { +rcu_read_unlock (); +return 0 ; + } -if (!timeout || t == timeout) +if (!timeout || t == timeout) {RCU_INIT_POINTER (timeout_ext->timeout,NULL ); + +/* No race with nf_conntrack_free() which is called +* only after the conntrack has been removed from +* the hashes. +*/ +if (refcount_dec_and_test (&t->refcnt)) +kfree_rcu (t, rcu); + } +rcu_read_unlock (); }/* We are not intended to delete this conntrack. */
@@ -70,6 +85,8 @@ int nf_ct_set_timeout(struct net *net, struct nf_conn *ct,const char *errmsg =NULL ;int ret =0 ; +WARN_ON_ONCE (!nf_ct_is_template (ct)); +rcu_read_lock (); h =rcu_dereference (nf_ct_timeout_hook);if (!h) {
@@ -127,6 +144,8 @@ void nf_ct_destroy_timeout(struct nf_conn *ct)struct nf_conn_timeout *timeout_ext;const struct nf_ct_timeout_hooks *h; +WARN_ON_ONCE (!nf_ct_is_template (ct)); +rcu_read_lock (); h =rcu_dereference (nf_ct_timeout_hook);
@@ -139,6 +158,8 @@ void nf_ct_destroy_timeout(struct nf_conn *ct)if (t) h->timeout_put (t);RCU_INIT_POINTER (timeout_ext->timeout,NULL ); +if (t &&refcount_dec_and_test (&t->refcnt)) +kfree_rcu (t, rcu); } }rcu_read_unlock ();
net/netfilter/nfnetlink_cttimeout.c +50 -62
219 lines not on this page · raw message
net/netfilter/nft_ct.c +3 -4
@@ -897,8 +897,6 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj, } } -rcu_assign_pointer (timeout->timeout, priv->timeout); -/* adjust the timeout as per 'new' state. ct is unconfirmed, * so the current timestamp must not be added. */
@@ -949,6 +947,7 @@ static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx, timeout->l3num = l3num; timeout->l4proto = l4proto; +refcount_set (&timeout->refcnt,1 ); ret =nf_ct_netns_get (ctx->net, ctx->family);if (ret <0 )
@@ -969,10 +968,10 @@ static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,struct nft_ct_timeout_obj *priv =nft_obj_data (obj);struct nf_ct_timeout *timeout = priv->timeout; -nf_queue_nf_hook_drop (ctx->net);nf_ct_untimeout (ctx->net, timeout);nf_ct_netns_put (ctx->net, ctx->family); -kfree_rcu (priv->timeout, rcu); +if (refcount_dec_and_test (&timeout->refcnt)) +kfree_rcu (priv->timeout, rcu); } static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
net/netfilter/xt_CT.c +1 -1
@@ -284,7 +284,7 @@ static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,struct nf_conn_help *help;if (ct) { -if (info->helper[0 ] || info->timeout[0 ]) +if (info->helper[0 ])nf_queue_nf_hook_drop (par->net); help =nfct_help (ct);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerRe: [PATCH net-next 08/15] netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Hi,
include/net/netfilter/nf_conntrack_timeout.h struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct, · line 80
Quoted 10 lines · Pablo Neira Ayuso
include/net/netfilter/nf_conntrack_timeout.h +4
-1@@ -56,8 +73,14 @@ struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,#ifdef CONFIG_NF_CONNTRACK_TIMEOUT struct nf_conn_timeout *timeout_ext; +if (!timeout) +return NULL ; + timeout_ext =nf_ct_ext_add (ct,NF_CT_EXT_TIMEOUT , gfp); -if (timeout_ext ==NULL ) +if (!timeout_ext || timeout_ext->timeout)
^^^^^^^^^^^^^^^^^^^^^^^
This check is useless, it is always going to be null.
There is also Julian Anastasov issue with documentation.
Quoted 1 line
Quoted 1 line
Documentation/networking/ipvs-sysctl.rst:76: WARNING: Block quote ends without a blank line; unexpected unindent. [docutils]
Documentation/networking/ipvs-sysctl.rst:76: ERROR: Unexpected section title or transition.
And regarding:
netfilter: synproxy: fix unaligned memory access in timestamp adjustment
it is fixing the unaligned access, but inet_proto_csum_replace4() wants 16-bit words starting from an even offset, sashiko reports that this still would not work in uneven. I don't think this is worth, but at least it is possible to add a note in the patch description.
I can post a v2 for this PR.
Jakub Kicinski
maintainerRe: [PATCH net-next 08/15] netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Quoted 22 lines · On Tue, 9 Jun 2026 00:30:26 +0200 Pablo Neira Ayuso wrote:
include/net/netfilter/nf_conntrack_timeout.h struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct, · line 80
Quoted 10 lines · Pablo Neira Ayuso
include/net/netfilter/nf_conntrack_timeout.h +4
-1@@ -56,8 +73,14 @@ struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,#ifdef CONFIG_NF_CONNTRACK_TIMEOUT struct nf_conn_timeout *timeout_ext; +if (!timeout) +return NULL ; + timeout_ext =nf_ct_ext_add (ct,NF_CT_EXT_TIMEOUT , gfp); -if (timeout_ext ==NULL ) +if (!timeout_ext || timeout_ext->timeout)^^^^^^^^^^^^^^^^^^^^^^^This check is useless, it is always going to be null.
There is also Julian Anastasov issue with documentation.
Quoted 1 line
Quoted 1 line
Documentation/networking/ipvs-sysctl.rst:76: WARNING: Block quote ends without a blank line; unexpected unindent. [docutils]
Documentation/networking/ipvs-sysctl.rst:76: ERROR: Unexpected section title or transition.And regarding:
Quoted 8 lines
netfilter: synproxy: fix unaligned memory access in timestamp adjustment
it is fixing the unaligned access, but
inet_proto_csum_replace4()wants 16-bit words starting from an even offset, sashiko reports that this still would not work in uneven. I don't think this is worth, but at least it is possible to add a note in the patch description.I can post a v2 for this PR.
Bad timing :( Please send follow ups
Pablo Neira Ayuso
maintainerRe: [PATCH net-next 08/15] netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Quoted 1 line · Jakub Kicinski
On Tue, 9 Jun 2026 00:30:26 +0200 Pablo Neira Ayuso wrote:
[...]
Quoted 3 lines
Quoted 1 line
I can post a v2 for this PR.
Bad timing :( Please send follow ups
No problem, we'll follow up, thanks.
Pablo Neira Ayuso
maintainerPATCH 9/15 netfilter: nf_conntrack_helper: dynamically allocate struct nf_conntrack_helper
In reply to Pablo Neira AyusoAdapt all existing helpers to use a modified version of nf_ct_helper_init(), to dynamically allocate struct nf_conntrack_helper.
Allocate expect_policy[] built-in into the helper to ensure this area is reachable after helper removal since a follow up patch adds refcount to track use of the nf_conntrack_helper structure from packet path so it remains around until last reference from ct helper extension is dropped.
Export __nf_conntrack_helper_register() which allows to register nfnetlink_cthelper dynamically allocated helper. Adapt nfnetlink_cthelper to use the built-in expect_policy[].
This is a preparation patch to add packet path refcounting to helpers.
--- include/net/netfilter/nf_conntrack_helper.h | 16 +-- net/ipv4/netfilter/nf_nat_snmp_basic_main.c | 27 +++-- net/netfilter/nf_conntrack_amanda.c | 39 +++---- net/netfilter/nf_conntrack_ftp.c | 5 +- net/netfilter/nf_conntrack_h323_main.c | 107 ++++++++------------ net/netfilter/nf_conntrack_helper.c | 75 +++++++++++--- net/netfilter/nf_conntrack_irc.c | 5 +- net/netfilter/nf_conntrack_netbios_ns.c | 20 ++-- net/netfilter/nf_conntrack_pptp.c | 22 ++-- net/netfilter/nf_conntrack_sane.c | 5 +- net/netfilter/nf_conntrack_sip.c | 5 +- net/netfilter/nf_conntrack_snmp.c | 21 ++-- net/netfilter/nf_conntrack_tftp.c | 5 +- net/netfilter/nfnetlink_cthelper.c | 47 ++++----- 14 files changed, 210 insertions(+), 189 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +9 -7
@@ -29,13 +29,16 @@ enum nf_ct_helper_flags {#define NF_CT_HELPER_NAME_LEN 16 +/* Must be kept in sync with the classes defined by helpers */ +#define NF_CT_MAX_EXPECT_CLASSES 4 +struct nf_conntrack_helper {struct hlist_node hnode;/* Internal use. */ char name[NF_CT_HELPER_NAME_LEN ];/* name of the module */ refcount_t refcnt;struct module *me;/* pointer to self */ -const struct nf_conntrack_expect_policy *expect_policy; +struct nf_conntrack_expect_policy expect_policy[NF_CT_MAX_EXPECT_CLASSES ];/* Tuple of things we will help (compared against server response) */ struct nf_conntrack_tuple tuple;
@@ -63,9 +66,6 @@ struct nf_conntrack_helper {char nat_mod_name[NF_CT_HELPER_NAME_LEN ]; }; -/* Must be kept in sync with the classes defined by helpers */ -#define NF_CT_MAX_EXPECT_CLASSES 4 -/* nf_conn feature for connections that have a helper */ struct nf_conn_help {/* Helper. if any */
@@ -103,11 +103,13 @@ void nf_ct_helper_init(struct nf_conntrack_helper *helper, struct nf_conn *ct), struct module *module); -int nf_conntrack_helper_register (struct nf_conntrack_helper *); +int nf_conntrack_helper_register (struct nf_conntrack_helper *,struct nf_conntrack_helper **); +int __nf_conntrack_helper_register (struct nf_conntrack_helper *);void nf_conntrack_helper_unregister (struct nf_conntrack_helper *); -int nf_conntrack_helpers_register (struct nf_conntrack_helper *,unsigned int ); -void nf_conntrack_helpers_unregister (struct nf_conntrack_helper *, +int nf_conntrack_helpers_register (struct nf_conntrack_helper *,unsigned int , +struct nf_conntrack_helper **); +void nf_conntrack_helpers_unregister (struct nf_conntrack_helper **,unsigned int );struct nf_conn_help *nf_ct_helper_ext_add (struct nf_conn *ct,gfp_t gfp);
net/ipv4/netfilter/nf_nat_snmp_basic_main.c +16 -11
@@ -202,29 +202,34 @@ static const struct nf_conntrack_expect_policy snmp_exp_policy = { .timeout = 180, }; -static struct nf_conntrack_helper snmp_trap_helper__read_mostly = { - .me =THIS_MODULE , - .help = help, - .expect_policy = &snmp_exp_policy, - .name ="snmp_trap" , - .tuple.src.l3num =AF_INET , - .tuple.src.u.udp.port =cpu_to_be16 (SNMP_TRAP_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , -}; +static struct nf_conntrack_helper snmp_trap_helper__read_mostly ; +static struct nf_conntrack_helper *snmp_trap_helper_ptr__read_mostly ;static int __init nf_nat_snmp_basic_init (void ) { +int err; +BUG_ON (nf_nat_snmp_hook !=NULL );RCU_INIT_POINTER (nf_nat_snmp_hook, help); -return nf_conntrack_helper_register (&snmp_trap_helper); +nf_ct_helper_init (&snmp_trap_helper,AF_INET ,IPPROTO_UDP , +"snmp_trap" ,SNMP_TRAP_PORT ,SNMP_TRAP_PORT ,SNMP_TRAP_PORT , + &snmp_exp_policy,0 , help,NULL ,THIS_MODULE ); + + err =nf_conntrack_helper_register (&snmp_trap_helper, &snmp_trap_helper_ptr); +if (err <0 ) { +RCU_INIT_POINTER (nf_nat_snmp_hook,NULL ); +return err; + } + +return 0 ; } static void __exit nf_nat_snmp_basic_fini(void) {RCU_INIT_POINTER (nf_nat_snmp_hook,NULL );synchronize_rcu (); -nf_conntrack_helper_unregister (&snmp_trap_helper); +nf_conntrack_helper_unregister (snmp_trap_helper_ptr); }module_init (nf_nat_snmp_basic_init);
net/netfilter/nf_conntrack_amanda.c +14 -25
@@ -169,35 +169,15 @@ static const struct nf_conntrack_expect_policy amanda_exp_policy = { .timeout = 180, }; -static struct nf_conntrack_helper amanda_helper[2 ]__read_mostly = { - { - .name =HELPER_NAME , - .me =THIS_MODULE , - .help = amanda_help, - .tuple.src.l3num =AF_INET , - .tuple.src.u.udp.port =cpu_to_be16 (10080 ), - .tuple.dst.protonum =IPPROTO_UDP , - .expect_policy = &amanda_exp_policy, - .nat_mod_name =NF_NAT_HELPER_NAME (HELPER_NAME ), - }, - { - .name ="amanda" , - .me =THIS_MODULE , - .help = amanda_help, - .tuple.src.l3num =AF_INET6 , - .tuple.src.u.udp.port =cpu_to_be16 (10080 ), - .tuple.dst.protonum =IPPROTO_UDP , - .expect_policy = &amanda_exp_policy, - .nat_mod_name =NF_NAT_HELPER_NAME (HELPER_NAME ), - }, -}; +static struct nf_conntrack_helper amanda_helper[2 ]__read_mostly ; +static struct nf_conntrack_helper *amanda_helper_ptr[2 ]__read_mostly ; static void __exit nf_conntrack_amanda_fini(void) {int i; -nf_conntrack_helpers_unregister (amanda_helper, -ARRAY_SIZE (amanda_helper)); +nf_conntrack_helpers_unregister (amanda_helper_ptr, +ARRAY_SIZE (amanda_helper_ptr));for (i =0 ; i <ARRAY_SIZE (search); i++)textsearch_destroy (search[i].ts); }
@@ -217,8 +197,17 @@ static int __init nf_conntrack_amanda_init(void) goto err1; } } + +nf_ct_helper_init (&amanda_helper[0 ],AF_INET ,IPPROTO_UDP , +HELPER_NAME ,10080 ,10080 ,10080 , + &amanda_exp_policy,0 , amanda_help,NULL ,THIS_MODULE ); +nf_ct_helper_init (&amanda_helper[1 ],AF_INET6 ,IPPROTO_UDP , +HELPER_NAME ,10080 ,10080 ,10080 , + &amanda_exp_policy,0 , amanda_help,NULL ,THIS_MODULE ); + ret =nf_conntrack_helpers_register (amanda_helper, -ARRAY_SIZE (amanda_helper)); +ARRAY_SIZE (amanda_helper), + amanda_helper_ptr);if (ret <0 ) goto err1;return 0 ;
net/netfilter/nf_conntrack_ftp.c +3 -2
@@ -555,6 +555,7 @@ static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct) }static struct nf_conntrack_helper ftp[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *ftp_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy ftp_exp_policy = { .max_expected =1 ,
@@ -563,7 +564,7 @@ static const struct nf_conntrack_expect_policy ftp_exp_policy = { static void __exit nf_conntrack_ftp_fini(void) { -nf_conntrack_helpers_unregister (ftp, ports_c *2 ); +nf_conntrack_helpers_unregister (ftp_ptr, ports_c *2 ); }static int __init nf_conntrack_ftp_init (void )
@@ -588,7 +589,7 @@ static int __init nf_conntrack_ftp_init(void) nf_ct_ftp_from_nlattr, THIS_MODULE); } - ret =nf_conntrack_helpers_register (ftp, ports_c *2 ); + ret =nf_conntrack_helpers_register (ftp, ports_c *2 , ftp_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_h323_main.c +42 -65
196 lines not on this page · raw message
net/netfilter/nf_conntrack_helper.c +63 -12
134 lines not on this page · raw message
net/netfilter/nf_conntrack_irc.c +3 -2
@@ -255,6 +255,7 @@ static int help(struct sk_buff *skb, unsigned int protoff, }static struct nf_conntrack_helper irc[MAX_PORTS ]__read_mostly ; +static struct nf_conntrack_helper *irc_ptr[MAX_PORTS ]__read_mostly ;static struct nf_conntrack_expect_policy irc_exp_policy;static int __init nf_conntrack_irc_init (void )
@@ -289,7 +290,7 @@ static int __init nf_conntrack_irc_init(void) 0, help, NULL, THIS_MODULE); } - ret =nf_conntrack_helpers_register (&irc[0 ], ports_c); + ret =nf_conntrack_helpers_register (&irc[0 ], ports_c, irc_ptr);if (ret) {pr_err ("failed to register helpers\n" );kfree (irc_buffer);
@@ -301,7 +302,7 @@ static int __init nf_conntrack_irc_init(void) static void __exit nf_conntrack_irc_fini(void) { -nf_conntrack_helpers_unregister (irc, ports_c); +nf_conntrack_helpers_unregister (irc_ptr, ports_c);kfree (irc_buffer); }
net/netfilter/nf_conntrack_netbios_ns.c +9 -11
@@ -44,27 +44,25 @@ static int netbios_ns_help(struct sk_buff *skb, unsigned int protoff,return nf_conntrack_broadcast_help (skb, ct, ctinfo, timeout); } -static struct nf_conntrack_helper helper__read_mostly = { - .name =HELPER_NAME , - .tuple.src.l3num =NFPROTO_IPV4 , - .tuple.src.u.udp.port =cpu_to_be16 (NMBD_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , - .me =THIS_MODULE , - .help = netbios_ns_help, - .expect_policy = &exp_policy, -}; +static struct nf_conntrack_helper helper__read_mostly ; +static struct nf_conntrack_helper *helper_ptr__read_mostly ;static int __init nf_conntrack_netbios_ns_init (void ) {NF_CT_HELPER_BUILD_BUG_ON (0 ); exp_policy.timeout = timeout; -return nf_conntrack_helper_register (&helper); + +nf_ct_helper_init (&helper,AF_INET ,IPPROTO_UDP ,HELPER_NAME , +NMBD_PORT ,NMBD_PORT ,NMBD_PORT , + &exp_policy,0 , netbios_ns_help,NULL ,THIS_MODULE ); + +return nf_conntrack_helper_register (&helper, &helper_ptr); } static void __exit nf_conntrack_netbios_ns_fini(void) { -nf_conntrack_helper_unregister (&helper); +nf_conntrack_helper_unregister (helper_ptr); }module_init (nf_conntrack_netbios_ns_init);
net/netfilter/nf_conntrack_pptp.c +10 -12
@@ -582,27 +582,25 @@ static const struct nf_conntrack_expect_policy pptp_exp_policy = { };/* control protocol helper */ -static struct nf_conntrack_helper pptp__read_mostly = { - .name ="pptp" , - .me =THIS_MODULE , - .tuple.src.l3num =AF_INET , - .tuple.src.u.tcp.port =cpu_to_be16 (PPTP_CONTROL_PORT ), - .tuple.dst.protonum =IPPROTO_TCP , - .help = conntrack_pptp_help, - .destroy = pptp_destroy_siblings, - .expect_policy = &pptp_exp_policy, -}; +static struct nf_conntrack_helper pptp__read_mostly ; +static struct nf_conntrack_helper *pptp_ptr__read_mostly ;static int __init nf_conntrack_pptp_init (void ) {NF_CT_HELPER_BUILD_BUG_ON (sizeof (struct nf_ct_pptp_master )); -return nf_conntrack_helper_register (&pptp); +nf_ct_helper_init (&pptp,AF_INET ,IPPROTO_TCP , +"pptp" ,PPTP_CONTROL_PORT ,PPTP_CONTROL_PORT ,PPTP_CONTROL_PORT , + &pptp_exp_policy,0 , conntrack_pptp_help,NULL ,THIS_MODULE ); + + pptp.destroy = pptp_destroy_siblings; + +return nf_conntrack_helper_register (&pptp, &pptp_ptr); } static void __exit nf_conntrack_pptp_fini(void) { -nf_conntrack_helper_unregister (&pptp); +nf_conntrack_helper_unregister (pptp_ptr); }module_init (nf_conntrack_pptp_init);
net/netfilter/nf_conntrack_sane.c +3 -2
@@ -167,6 +167,7 @@ static int help(struct sk_buff *skb, }static struct nf_conntrack_helper sane[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *sane_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy sane_exp_policy = { .max_expected =1 ,
@@ -175,7 +176,7 @@ static const struct nf_conntrack_expect_policy sane_exp_policy = { static void __exit nf_conntrack_sane_fini(void) { -nf_conntrack_helpers_unregister (sane, ports_c *2 ); +nf_conntrack_helpers_unregister (sane_ptr, ports_c *2 ); }static int __init nf_conntrack_sane_init (void )
@@ -200,7 +201,7 @@ static int __init nf_conntrack_sane_init(void) THIS_MODULE); } - ret =nf_conntrack_helpers_register (sane, ports_c *2 ); + ret =nf_conntrack_helpers_register (sane, ports_c *2 , sane_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_sip.c +3 -2
@@ -1731,6 +1731,7 @@ static int sip_help_udp(struct sk_buff *skb, unsigned int protoff, }static struct nf_conntrack_helper sip[MAX_PORTS *4 ]__read_mostly ; +static struct nf_conntrack_helper *sip_ptr[MAX_PORTS *4 ]__read_mostly ;static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX +1 ] = { [SIP_EXPECT_SIGNALLING ] = {
@@ -1757,7 +1758,7 @@ static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1 static void __exit nf_conntrack_sip_fini(void) { -nf_conntrack_helpers_unregister (sip, ports_c *4 ); +nf_conntrack_helpers_unregister (sip_ptr, ports_c *4 ); }static int __init nf_conntrack_sip_init (void )
@@ -1788,7 +1789,7 @@ static int __init nf_conntrack_sip_init(void) NULL, THIS_MODULE); } - ret =nf_conntrack_helpers_register (sip, ports_c *4 ); + ret =nf_conntrack_helpers_register (sip, ports_c *4 , sip_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_snmp.c +10 -11
@@ -47,25 +47,24 @@ static struct nf_conntrack_expect_policy exp_policy = { .max_expected = 1, }; -static struct nf_conntrack_helper helper__read_mostly = { - .name ="snmp" , - .tuple.src.l3num =NFPROTO_IPV4 , - .tuple.src.u.udp.port =cpu_to_be16 (SNMP_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , - .me =THIS_MODULE , - .help = snmp_conntrack_help, - .expect_policy = &exp_policy, -}; +static struct nf_conntrack_helper helper__read_mostly ; +static struct nf_conntrack_helper *helper_ptr__read_mostly ;static int __init nf_conntrack_snmp_init (void ) { exp_policy.timeout = timeout; -return nf_conntrack_helper_register (&helper); + +nf_ct_helper_init (&helper,AF_INET ,IPPROTO_UDP , +"snmp" ,SNMP_PORT ,SNMP_PORT ,SNMP_PORT , + &exp_policy,0 , snmp_conntrack_help,NULL , +THIS_MODULE ); + +return nf_conntrack_helper_register (&helper, &helper_ptr); } static void __exit nf_conntrack_snmp_fini(void) { -nf_conntrack_helper_unregister (&helper); +nf_conntrack_helper_unregister (helper_ptr); }module_init (nf_conntrack_snmp_init);
net/netfilter/nf_conntrack_tftp.c +3 -2
@@ -96,6 +96,7 @@ static int tftp_help(struct sk_buff *skb, }static struct nf_conntrack_helper tftp[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *tftp_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy tftp_exp_policy = { .max_expected =1 ,
@@ -104,7 +105,7 @@ static const struct nf_conntrack_expect_policy tftp_exp_policy = { static void __exit nf_conntrack_tftp_fini(void) { -nf_conntrack_helpers_unregister (tftp, ports_c *2 ); +nf_conntrack_helpers_unregister (tftp_ptr, ports_c *2 ); }static int __init nf_conntrack_tftp_init (void )
@@ -127,7 +128,7 @@ static int __init nf_conntrack_tftp_init(void) THIS_MODULE); } - ret =nf_conntrack_helpers_register (tftp, ports_c *2 ); + ret =nf_conntrack_helpers_register (tftp, ports_c *2 , tftp_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nfnetlink_cthelper.c +22 -25
136 lines not on this page · raw message
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 10/15 netfilter: nf_conntrack_pptp: move GRE specific cleanup to GRE tracker
In reply to Pablo Neira AyusoMove the GRE specific cleanup to nf_conntrack_proto_gre.c to ensure that the .destroy callback for the pptp helper is still reachable by existing conntrack entries while pptp module is being removed.
This is a preparation patch, no functional changes are intended.
--- .../net/netfilter/ipv4/nf_conntrack_ipv4.h | 4 ++ net/netfilter/nf_conntrack_pptp.c | 63 +------------------ net/netfilter/nf_conntrack_proto_gre.c | 61 ++++++++++++++++++ 3 files changed, 67 insertions(+), 61 deletions(-)
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +4 -0
@@ -20,4 +20,8 @@ extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp;extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre;#endif +#if IS_ENABLED (CONFIG_NF_CONNTRACK_PPTP ) +void gre_pptp_destroy_siblings (struct nf_conn *ct); +#endif +#endif /*_NF_CONNTRACK_IPV4_H*/
net/netfilter/nf_conntrack_pptp.c +2 -61
81 lines not on this page · raw message
net/netfilter/nf_conntrack_proto_gre.c +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 11/15 netfilter: nf_conntrack_helper: add refcounting from datapath
In reply to Pablo Neira AyusoThis patch adds a new ->ct_refcnt field to struct nf_conntrack_helper which is bumped when the helper is used by the ct helper extension. Drop this reference count when the conntrack entry is released. This is a packet path refcount which ensures that struct nf_conntrack_helper remains in place for tricky scenarios where a packet sits in nfqueue, or elsewhere, with a conntrack that refers to this helper.
For simplicity, this leaves a single refcount for helper objects in place, remove the existing refcount for control plane that ensures that the helper does not go away if it is used by ruleset.
On helper removal, the help callback is set to NULL to disable it from packet path and, after rcu grace period, existing expectations are removed. Update ctnetlink to disable access to .to_nlattr and .from_nlattr if the helper is going away.
Remove nf_queue_nf_hook_drop() since it has proven not to be effective because packets with unconfirmed conntracks which are still flying to sit in nfqueue.
--- include/net/netfilter/nf_conntrack_helper.h | 26 +++++++++-- net/netfilter/nf_conntrack_core.c | 3 +- net/netfilter/nf_conntrack_helper.c | 52 ++++++++++----------- net/netfilter/nf_conntrack_netlink.c | 28 +++++++---- net/netfilter/nf_conntrack_ovs.c | 9 +++- net/netfilter/nf_conntrack_proto.c | 15 ++++-- net/netfilter/nfnetlink_cthelper.c | 14 ++---- net/netfilter/nft_ct.c | 3 +- net/netfilter/xt_CT.c | 3 -- 9 files changed, 89 insertions(+), 64 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +21 -5
@@ -35,20 +35,22 @@ enum nf_ct_helper_flags {struct nf_conntrack_helper {struct hlist_node hnode;/* Internal use. */ +struct rcu_head rcu; +char name[NF_CT_HELPER_NAME_LEN ];/* name of the module */ -refcount_t refcnt;struct module *me;/* pointer to self */ struct nf_conntrack_expect_policy expect_policy[NF_CT_MAX_EXPECT_CLASSES ]; +refcount_t ct_refcnt; +/* Tuple of things we will help (compared against server response) */ struct nf_conntrack_tuple tuple;/* Function to call when data passes; return verdict, or -1 to invalidate. */ -int (*help)(struct sk_buff *skb, -unsigned int protoff, -struct nf_conn *ct, -enum ip_conntrack_info conntrackinfo); +int __rcu (*help)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);void (*destroy)(struct nf_conn *ct);
@@ -138,6 +140,20 @@ static inline void *nfct_help_data(const struct nf_conn *ct)return (void *)help->data; } +static inline void nf_ct_help_put (const struct nf_conn *ct) +{ +struct nf_conntrack_helper *helper; +struct nf_conn_help *help; + + help =nfct_help (ct); +if (!help) +return ; + + helper =rcu_dereference (help->helper); +if (helper &&refcount_dec_and_test (&helper->ct_refcnt)) +kfree_rcu (helper, rcu); +} +int nf_conntrack_helper_init (void );void nf_conntrack_helper_fini (void );
net/netfilter/nf_conntrack_core.c +2 -1
@@ -1746,6 +1746,7 @@ void nf_conntrack_free(struct nf_conn *ct) nat_hook->remove_nat_bysrc (ct); } +nf_ct_help_put (ct);nf_ct_timeout_put (ct);rcu_read_unlock ();
@@ -1829,7 +1830,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl, assign_helper =rcu_dereference (exp->assign_helper);if (assign_helper) { help =nf_ct_helper_ext_add (ct,GFP_ATOMIC ); -if (help) +if (help &&refcount_inc_not_zero (&assign_helper->ct_refcnt))rcu_assign_pointer (help->helper, assign_helper); }
net/netfilter/nf_conntrack_helper.c +24 -28
112 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +18 -10
@@ -240,7 +240,8 @@ static int ctnetlink_dump_helpinfo(struct sk_buff *skb,if (nla_put_string (skb,CTA_HELP_NAME , helper->name)) goto nla_put_failure; -if (helper->to_nlattr) +if (rcu_access_pointer (helper->help) && + helper->to_nlattr) helper->to_nlattr (skb, ct);nla_nest_end (skb, nest_helper);
@@ -1935,6 +1936,7 @@ static int ctnetlink_change_helper(struct nf_conn *ct,if (err <0 )return err; +rcu_read_lock ();/* don't change helper of sibling connections */ if (ct->master) {/* If we try to change the helper to the same thing twice,
@@ -1943,27 +1945,27 @@ static int ctnetlink_change_helper(struct nf_conn *ct,*/ err = -EBUSY ;if (help) { -rcu_read_lock (); helper =rcu_dereference (help->helper);if (helper && !strcmp (helper->name, helpname)) err =0 ; -rcu_read_unlock (); } - +rcu_read_unlock ();return err; } -if (!strcmp (helpname,"" )) { -if (help && help->helper) { +if (!strcmp (helpname,"" ) && help) { + helper =rcu_dereference (help->helper); +if (helper) {/* we had a helper before ... */ nf_ct_remove_expectations (ct);RCU_INIT_POINTER (help->helper,NULL ); +if (refcount_dec_and_test (&helper->ct_refcnt)) +kfree_rcu (helper, rcu); } - +rcu_read_unlock ();return 0 ; } -rcu_read_lock (); helper =__nf_conntrack_helper_find (helpname,nf_ct_l3num (ct),nf_ct_protonum (ct));if (helper ==NULL ) {
@@ -1974,7 +1976,8 @@ static int ctnetlink_change_helper(struct nf_conn *ct,if (help) {if (rcu_access_pointer (help->helper) == helper) {/* update private helper data if allowed. */ -if (helper->from_nlattr) +if (rcu_access_pointer (helper->help) && + helper->from_nlattr) helper->from_nlattr (helpinfo, ct); err =0 ; }else
@@ -2289,11 +2292,16 @@ ctnetlink_create_conntrack(struct net *net, goto err2; }/* set private helper data if allowed. */ -if (helper->from_nlattr) +if (rcu_access_pointer (helper->help) && + helper->from_nlattr) helper->from_nlattr (helpinfo, ct);/* disable helper auto-assignment for this entry */ ct->status |=IPS_HELPER ; +if (!refcount_inc_not_zero (&helper->ct_refcnt)) { + err = -ENOENT ; + goto err2; + }RCU_INIT_POINTER (help->helper, helper); } }
net/netfilter/nf_conntrack_ovs.c +8 -1
@@ -12,6 +12,9 @@int nf_ct_helper (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo,u16 proto) { +int (*helper_cb)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);const struct nf_conntrack_helper *helper;const struct nf_conn_help *help;unsigned int protoff;
@@ -60,7 +63,11 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct,if (helper->tuple.dst.protonum != proto)return NF_ACCEPT ; - err = helper->help (skb, protoff, ct, ctinfo); + helper_cb =rcu_dereference (helper->help); +if (!helper_cb) +return NF_ACCEPT ; + + err =helper_cb (skb, protoff, ct, ctinfo);if (err !=NF_ACCEPT )return err;
net/netfilter/nf_conntrack_proto.c +10 -5
@@ -129,6 +129,9 @@ unsigned int nf_confirm(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { +int (*helper_cb)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);const struct nf_conn_help *help;enum ip_conntrack_info ctinfo;unsigned int protoff;
@@ -175,11 +178,13 @@ unsigned int nf_confirm(void *priv,/* rcu_read_lock()ed by nf_hook */ helper =rcu_dereference (help->helper);if (helper) { - ret = helper->help (skb, - protoff, - ct, ctinfo); -if (ret !=NF_ACCEPT ) -return ret; + helper_cb =rcu_dereference (helper->help); +if (helper_cb) { + ret =helper_cb (skb, protoff, + ct, ctinfo); +if (ret !=NF_ACCEPT ) +return ret; + } } }
net/netfilter/nfnetlink_cthelper.c +5 -9
@@ -719,15 +719,11 @@ static int nfnl_cthelper_del(struct sk_buff *skb, const struct nfnl_info *info, tuple.dst.protonum != cur->tuple.dst.protonum)) continue; -if (refcount_dec_if_one (&cur->refcnt)) { - found = true; -nf_conntrack_helper_unregister (cur); - -list_del (&nlcth->list); -kfree (nlcth); - }else { - ret = -EBUSY ; - } + found = true; +nf_conntrack_helper_unregister (cur); + +list_del (&nlcth->list); +kfree (nlcth); }/* Make sure we return success if we flush and there is no helpers */
net/netfilter/nft_ct.c +1 -2
@@ -1101,7 +1101,6 @@ static void nft_ct_helper_obj_destroy(const struct nft_ctx *ctx, {struct nft_ct_helper_obj *priv =nft_obj_data (obj); -nf_queue_nf_hook_drop (ctx->net);if (priv->helper4)nf_conntrack_helper_put (priv->helper4);if (priv->helper6)
@@ -1144,7 +1143,7 @@ static void nft_ct_helper_obj_eval(struct nft_object *obj,return ; help =nf_ct_helper_ext_add (ct,GFP_ATOMIC ); -if (help) { +if (help &&refcount_inc_not_zero (&to_assign->ct_refcnt)) {rcu_assign_pointer (help->helper, to_assign);set_bit (IPS_HELPER_BIT , &ct->status);
net/netfilter/xt_CT.c +0 -3
@@ -284,9 +284,6 @@ static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,struct nf_conn_help *help;if (ct) { -if (info->helper[0 ]) -nf_queue_nf_hook_drop (par->net); - help =nfct_help (ct);xt_ct_put_helper (help);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 12/15 netfilter: conntrack: revert ct extension genid infrastructure
In reply to Pablo Neira AyusoThis infrastructure is not used anymore after moving ct timeout and helper to use datapath refcount to track object use.
Revert commit c56716c69ce1 ("netfilter: extensions: introduce extension genid count") this patch disables all ct extensions (leading to NULL) for unconfirmed conntracks, when this is only targeted at ct helper and ct timeout. There is also codebase that dereferences the ct extension without checking for NULL which could lead to crash.
--- include/net/netfilter/nf_conntrack_extend.h | 12 ---- net/netfilter/nf_conntrack_core.c | 61 +-------------------- net/netfilter/nf_conntrack_extend.c | 32 +---------- 3 files changed, 2 insertions(+), 103 deletions(-)
include/net/netfilter/nf_conntrack_extend.h +0 -12
@@ -38,7 +38,6 @@ enum nf_ct_ext_id {struct nf_ct_ext {u8 offset[NF_CT_EXT_NUM ];u8 len; -unsigned int gen_id;char data[]__aligned(8) ; };
@@ -52,8 +51,6 @@ static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)return (ct->ext &&__nf_ct_ext_exist (ct->ext, id)); } -void *__nf_ct_ext_find (const struct nf_ct_ext *ext,u8 id); -static inline void *nf_ct_ext_find (const struct nf_conn *ct,u8 id) {struct nf_ct_ext *ext = ct->ext;
@@ -61,19 +58,10 @@ static inline void *nf_ct_ext_find(const struct nf_conn *ct, u8 id)if (!ext || !__nf_ct_ext_exist (ext, id))return NULL ; -if (unlikely (ext->gen_id)) -return __nf_ct_ext_find (ext, id); - return (void *)ct->ext + ct->ext->offset[id]; }/* Add this type, returns pointer to data or NULL. */ void *nf_ct_ext_add (struct nf_conn *ct,enum nf_ct_ext_id id,gfp_t gfp); -/* ext genid. if ext->id != ext_genid, extensions cannot be used -* anymore unless conntrack has CONFIRMED bit set. -*/ -extern atomic_t nf_conntrack_ext_genid; -void nf_ct_ext_bump_genid (void ); -#endif /* _NF_CONNTRACK_EXTEND_H */
net/netfilter/nf_conntrack_core.c +1 -60
101 lines not on this page · raw message
net/netfilter/nf_conntrack_extend.c +1 -31
@@ -27,8 +27,6 @@#define NF_CT_EXT_PREALLOC 128u/* conntrack events are on by default */ -atomic_t nf_conntrack_ext_genid__read_mostly =ATOMIC_INIT (1 ); -static const u8 nf_ct_ext_type_len[NF_CT_EXT_NUM ] = { [NF_CT_EXT_HELPER ] =sizeof (struct nf_conn_help ), #if IS_ENABLED(CONFIG_NF_NAT)
@@ -118,10 +116,8 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)if (!new)return NULL ; -if (!ct->ext) { +if (!ct->ext)memset (new->offset,0 ,sizeof (new->offset)); - new->gen_id =atomic_read (&nf_conntrack_ext_genid); - } new->offset[id] = newoff; new->len = newlen;
@@ -131,29 +127,3 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)return (void *)new + newoff; }EXPORT_SYMBOL (nf_ct_ext_add); - -/* Use nf_ct_ext_find wrapper. This is only useful for unconfirmed entries. */ -void *__nf_ct_ext_find (const struct nf_ct_ext *ext,u8 id) -{ -unsigned int gen_id =atomic_read (&nf_conntrack_ext_genid); -unsigned int this_id =READ_ONCE (ext->gen_id); - -if (!__nf_ct_ext_exist (ext, id)) -return NULL ; - -if (this_id ==0 || ext->gen_id == gen_id) -return (void *)ext + ext->offset[id]; - -return NULL ; -} -EXPORT_SYMBOL (__nf_ct_ext_find); - -void nf_ct_ext_bump_genid (void ) -{ -unsigned int value =atomic_inc_return (&nf_conntrack_ext_genid); - -if (value ==UINT_MAX ) -atomic_set (&nf_conntrack_ext_genid,1 ); - -msleep (HZ ); -}
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 13/15 netfilter: conntrack: call nf_ct_gre_keymap_destroy() if master helper is pptp
In reply to Pablo Neira AyusoFor GRE flows, validate that the ct master helper (if any) is pptp before calling nf_ct_gre_keymap_destroy(), so the helper data area can be accessed safely. Note that only the pptp helper provides a .destroy callback.
--- net/netfilter/nf_conntrack_core.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_core.c +16 -2
@@ -562,9 +562,23 @@ static void destroy_gre_conntrack(struct nf_conn *ct) {#ifdef CONFIG_NF_CT_PROTO_GRE struct nf_conn *master = ct->master; +struct nf_conn_help *help; + +if (!master) +return ; + + help =nfct_help (master); +if (help) { +struct nf_conntrack_helper *helper; -if (master) -nf_ct_gre_keymap_destroy (master); +rcu_read_lock (); + helper =rcu_dereference (help->helper); +/* Only pptp helper has a destroy callback. */ +if (helper && helper->destroy) +nf_ct_gre_keymap_destroy (master); + +rcu_read_unlock (); + }#endif }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 14/15 netfilter: flowtable: avoid num_encaps underflow on bridge VLAN untag
In reply to Pablo Neira AyusoFrom: David Carlier <[email protected]>
The DEV_PATH_BR_VLAN_UNTAG case post-decrements info->num_encaps inside WARN_ON_ONCE(). num_encaps is u8, so if it's already 0 the decrement still happens and wraps it to 255. The break only leaves the inner switch -- a later path entry can set info->indev back to a real device, and we end up returning with num_encaps == 255.
nft_dev_forward_path() then walks info.encap[] (size 2) up to num_encaps, which means an OOB stack read and a bogus count copied into the route descriptor.
Should only happen on a malformed bridge path stack, hence the WARN, but worth handling sanely. Move the decrement out of the WARN.
[ While at this, remove the WARN_ON_ONCE since this can only happen
with a buggy bridge path stack --pablo ].
--- net/netfilter/nf_flow_table_path.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_flow_table_path.c +2 -1
@@ -163,10 +163,11 @@ static void nft_dev_path_info(const struct net_device_path_stack *stack, info->num_encaps++;break ;case DEV_PATH_BR_VLAN_UNTAG : -if (WARN_ON_ONCE (info->num_encaps-- ==0 )) { +if (info->num_encaps ==0 ) { info->indev =NULL ;break ; } + info->num_encaps--;break ;case DEV_PATH_BR_VLAN_KEEP :break ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH 15/15 netfilter: nf_conntrack: use get_unaligned_be32() in tcp_sack()
In reply to Pablo Neira AyusoFrom: Rosen Penev <[email protected]>
The timestamp-only fast path dereferences the option stream as *(__be32 *)ptr, which assumes 4-byte alignment that the TCP option stream does not guarantee. Use get_unaligned_be32() instead, which reads the value safely and already returns host byte order, so the htonl() on the comparison constant can be dropped.
This matches the existing get_unaligned_be32() use later in the same function.
--- net/netfilter/nf_conntrack_proto_tcp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +5 -5
@@ -405,11 +405,11 @@ static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff,return ;/* Fast path for timestamp-only option */ -if (length ==TCPOLEN_TSTAMP_ALIGNED - && *(__be32 *)ptr ==htonl ((TCPOPT_NOP <<24 ) - | (TCPOPT_NOP <<16 ) - | (TCPOPT_TIMESTAMP <<8 ) - |TCPOLEN_TIMESTAMP )) +if (length ==TCPOLEN_TSTAMP_ALIGNED && +get_unaligned_be32 (ptr) == ((TCPOPT_NOP <<24 ) | + (TCPOPT_NOP <<16 ) | + (TCPOPT_TIMESTAMP <<8 ) | +TCPOLEN_TIMESTAMP ))return ;while (length >0 ) {
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerHi,
I'm replying to Sashiko.dev comments here:
* [PATCH net-next 06/15] netfilter: synproxy: fix unaligned memory access in timestamp adjustment
Refers to pre-existing issue. I think this comment is not correct?
* [PATCH net-next 07/15] netfilter: synproxy: protect nf_ct_seqadj_init() with conntrack lock
Refers to pre-existing issue. But I think it is not correct, reopened connections in TIME_WAIT are killed by TCP protocol tracker, so they start in a clean state.
It also suggests check for NULL in seqadj = nfct_seqadj(ct); this is related to a bug in the ct extension generation ID which is addressed by this batch. Anyway, Florian and me agreed that adding this NULL check for safety is good to go, and I have a patch fot this.
* [PATCH net-next 08/15] netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Wrong comment by AI.
Quoted 2 lines
Does removing this assignment cause nftables ct timeout rules to silently fail if a timeout extension is already present?
The ct timeout is only applied to a new conntrack that unconfirmed, this override semantics does not make sense to me.
* [PATCH net-next 11/15] netfilter: nf_conntrack_helper: add refcounting from datapath
Refer to pre-existing issue. Yes, ->destroy() is missing here and in ctnetlink, I will post a patch to address this. This also refer to ctnetlink_change_helper().
* [PATCH net-next 13/15] netfilter: conntrack: call nf_ct_gre_keymap_destroy() if master helper is pptp
No other helper support the .destroy callback. AI assumes other helpers support this callback.
* [PATCH net-next 14/15] netfilter: flowtable: avoid num_encaps underflow on bridge VLAN untag
Refers to pre-existing issue. Yes, this function nft_dev_path_info() can be made more robust when failing to build a fast path. I have a follow up patch for this.
There is another issue with the IPVS patch that adds the conn_max documentation (incorrect format), Julian Anastasov is ready to send a follow up patch address it.
Let me know, thanks.
Quoted 156 lines · Pablo Neira Ayuso
Hi,
The following patchset contains Netfilter/IPVS updates for net-next, this contains updates to address sashiko reports in IPVS and Netfilter on possible pre-existing issues. This also includes a series to add refcount for ct helper and timeout to deal with a corner case scenario with unconfirmed conntracks flying to nfqueue.
1) Add a
conn_maxsysctl to IPVS to limit the maximum number of connections, from Julian Anastasov.2) Use
get_unaligned_be16()to access TCP MSS innfnetlink_osf, from Fernando Fernandez Mancera.3) Use {READ,WRITE}_ONCE to access helper flags from
nfnetlink_helper.Several patches for the synproxy infrastructure, from Fernando Fernandez Mancera:
4) Drop packet if TCP timestamp adjustment fails.
5) Continue parsing of TCP timestamp to deal with possible duplicates.
6) Use {get,put}_unaligned_be32() to acess the TCP timestamp.
7) Hold
ct->lockto initializenf_ct_seqadj_init().Updates for the ct timeout infrastructure, to deal with a corner case for unconfirmed conntracks flying to nfqueue:
8) Add a refcount to track ct timeout policy use by ct extension, release the timeout until the last ct extension drops the refcnt on it.
Similar update for the ct helper infrastructure:
9) Dynamic allocation of ct helpers, as a preparation for adding refcount to track ct extension use.
10) Move
destroy_sibling_or_exp()tonf_conntrack_proto_gre, so pptp conntrack helper module removal does not make this code unreachable via thehelper->destroycallback. This is another dependency for the new refcount coming in this series.11) Add a refcount to track use of it from the ct extension, then ct helper and timeout is reachable to the connection until it goes away.
12) Remove the genid infrastructure in ct extensions. The primary goal was to detect that a ct extension such as ct timeout and ct helper went stale for unconfirmed conntrack, either because object or module was removed. This deactivates all ct extensions though for this unconfirmed conntrack.
13) Call
nf_ct_gre_keymap_destroy()if this is a master conntrack with a pptp helper only.sashiko.dev reports one more relevant issue when unsetting the helper via ctnetlink that I will address in a follow up patch.
Then, two more assorted updates:
14) Avoid a unlikely underflow in bridge VLAN untag, only possible if buggy bridge VLAN filtering is buggy, remove
WARN_ON_ONCEwhile at it. From David Carlier.15) Use
get_unaligned_be32()innf_conntrack_tcpto access sack extension, from Rosen Penev.Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.gitnf-next-26-06-07Thanks.
----------------------------------------------------------------
The following changes since commit
bfa3d89cc15c09f7d1581c834a5ed725189ec19f:Merge tag 'batadv-next-pullrequest-20260603' of https://git.open-mesh.org/batadv (2026-06-04 19:14:35 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.gittags/nf-next-26-06-07for you to fetch changes up to
d3bf9eae486490832bd08fd62ab0ac601f346bd4:netfilter:
nf_conntrack: useget_unaligned_be32()intcp_sack()(2026-06-07 11:13:47 +0200)----------------------------------------------------------------
netfilter pull request 26-06-07----------------------------------------------------------------
David Carlier (1):
netfilter: flowtable: avoidnum_encapsunderflow on bridge VLAN untagFernando Fernandez Mancera (5):
netfilter:nfnetlink_osf: fix mss parsing on big-endian architectures
netfilter: synproxy: drop packets if timestamp adjustment fails
netfilter: synproxy: adjust duplicate timestamp options
netfilter: synproxy: fix unaligned memory access in timestamp adjustment
netfilter: synproxy: protectnf_ct_seqadj_init()with conntrack lockJulian Anastasov (1):
ipvs: addconn_maxsysctl to limit connectionsPablo Neira Ayuso (7):
netfilter:nfnetlink_cthelper: use {READ,WRITE}_ONCE for accessing helper flags
netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
netfilter:nf_conntrack_helper: dynamically allocate structnf_conntrack_helper
netfilter:nf_conntrack_pptp: move GRE specific cleanup to GRE tracker
netfilter:nf_conntrack_helper: add refcounting from datapath
netfilter: conntrack: revert ct extension genid infrastructure
netfilter: conntrack: callnf_ct_gre_keymap_destroy()if master helper is pptpRosen Penev (1):
netfilter:nf_conntrack: useget_unaligned_be32()intcp_sack()Documentation/networking/ipvs-sysctl.rst | 35 +++++++ include/net/ip_vs.h | 22 +++++ include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 4 + include/net/netfilter/nf_conntrack_extend.h | 12 --- include/net/netfilter/nf_conntrack_helper.h | 42 ++++++--- include/net/netfilter/nf_conntrack_timeout.h | 27 +++++- net/ipv4/netfilter/nf_nat_snmp_basic_main.c | 27 +++--- net/netfilter/ipvs/ip_vs_conn.c | 10 +- net/netfilter/ipvs/ip_vs_ctl.c | 53 +++++++++++ net/netfilter/nf_conntrack_amanda.c | 39 +++----- net/netfilter/nf_conntrack_core.c | 92 +++++------------- net/netfilter/nf_conntrack_extend.c | 32 +------ net/netfilter/nf_conntrack_ftp.c | 5 +- net/netfilter/nf_conntrack_h323_main.c | 107 +++++++++------------ net/netfilter/nf_conntrack_helper.c | 125 +++++++++++++++++-------- net/netfilter/nf_conntrack_irc.c | 5 +- net/netfilter/nf_conntrack_netbios_ns.c | 20 ++-- net/netfilter/nf_conntrack_netlink.c | 28 ++++-- net/netfilter/nf_conntrack_ovs.c | 9 +- net/netfilter/nf_conntrack_pptp.c | 83 +++------------- net/netfilter/nf_conntrack_proto.c | 15 ++- net/netfilter/nf_conntrack_proto_gre.c | 61 ++++++++++++ net/netfilter/nf_conntrack_proto_tcp.c | 10 +- net/netfilter/nf_conntrack_sane.c | 5 +- net/netfilter/nf_conntrack_seqadj.c | 2 + net/netfilter/nf_conntrack_sip.c | 5 +- net/netfilter/nf_conntrack_snmp.c | 21 ++--- net/netfilter/nf_conntrack_tftp.c | 5 +- net/netfilter/nf_conntrack_timeout.c | 27 +++++- net/netfilter/nf_flow_table_path.c | 3 +- net/netfilter/nf_synproxy_core.c | 40 ++++---- net/netfilter/nfnetlink_cthelper.c | 79 ++++++++-------- net/netfilter/nfnetlink_cttimeout.c | 112 ++++++++++------------ net/netfilter/nfnetlink_osf.c | 6 +- net/netfilter/nft_ct.c | 10 +- net/netfilter/xt_CT.c | 3 - 36 files changed, 653 insertions(+), 528 deletions(-)
Pablo Neira Ayuso
maintainerHi,
Quoted 7 lines · Pablo Neira Ayuso
Hi,
I'm replying to Sashiko.dev comments here:
* [PATCH net-next 06/15] netfilter: synproxy: fix unaligned memory access in timestamp adjustment
Refers to pre-existing issue. I think this comment is not correct?
Yes, it is, inet_proto_csum_replace4() needs an even offset to the 16-bit word. Checksum will be corrupted, but at least the alignment issue is addressed, which leaves things better than before. Maybe it is worth to add a note to this patch.
I can post a v2 of this PR to refine the few things that make sense at this stage.