netfilter: nft_ct: move custom expectation support to helper
cover netfilter: nft_ct: move custom expectation support to helper
Also in this series:
- 6fb421bd07f1 ("netfilter: nft_ct: expectation timeouts are passed in milliseconds") from v7.2-rc1 is included because it fixes the timeout unit bug this branch has, and the fix's moved code uses its jiffies timeout.
PATCH 1/2 netfilter: nft_ct: expectation timeouts are passed in milliseconds
From: Florian Westphal <[email protected]>
[ Upstream commit 6fb421bd07f156cdf0cdede062d31f1c21def326 ]
Userspace passes '5000' in case user asks for 5 seconds.
Allowing for sub-second expectation lifetimes makes sense to me. so fix up the kernel side instead of munging nft to send a value rounded up to next second.
Also note that this violates nft convention of passing integers in network byte order, but we can't change this anymore.
Stable-dep-of: 3679da4ad8be ("netfilter: nft_ct: move custom expectation support to helper")
--- net/netfilter/nft_ct.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
net/netfilter/nft_ct.c +18 -3
@@ -1198,11 +1198,23 @@ struct nft_ct_expect_obj {u32 timeout; }; +static int nft_ct_expect_timeout_get (const struct nlattr *attr,u32 *val) +{ +unsigned long jiffies_val =msecs_to_jiffies (nla_get_u32 (attr)); + +if (jiffies_val >UINT_MAX ) +return -ERANGE ; + + *val = jiffies_val; +return 0 ; +} +static int nft_ct_expect_obj_init (const struct nft_ctx *ctx,const struct nlattr *const tb[],struct nft_object *obj) {struct nft_ct_expect_obj *priv =nft_obj_data (obj); +int err; if (!tb[NFTA_CT_EXPECT_L4PROTO] || !tb[NFTA_CT_EXPECT_DPORT] ||
@@ -1238,8 +1250,11 @@ static int nft_ct_expect_obj_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } + err =nft_ct_expect_timeout_get (tb[NFTA_CT_EXPECT_TIMEOUT ], &priv->timeout); +if (err) +return err; + priv->dport =nla_get_be16 (tb[NFTA_CT_EXPECT_DPORT ]); - priv->timeout =nla_get_u32 (tb[NFTA_CT_EXPECT_TIMEOUT ]); priv->size =nla_get_u8 (tb[NFTA_CT_EXPECT_SIZE ]);return nf_ct_netns_get (ctx->net, ctx->family);
@@ -1259,7 +1274,7 @@ static int nft_ct_expect_obj_dump(struct sk_buff *skb,if (nla_put_be16 (skb,NFTA_CT_EXPECT_L3PROTO ,htons (priv->l3num)) ||nla_put_u8 (skb,NFTA_CT_EXPECT_L4PROTO , priv->l4proto) ||nla_put_be16 (skb,NFTA_CT_EXPECT_DPORT , priv->dport) || -nla_put_u32 (skb,NFTA_CT_EXPECT_TIMEOUT , priv->timeout) || +nla_put_u32 (skb,NFTA_CT_EXPECT_TIMEOUT ,jiffies_to_msecs (priv->timeout)) ||nla_put_u8 (skb,NFTA_CT_EXPECT_SIZE , priv->size))return -1 ;
@@ -1309,7 +1324,7 @@ static void nft_ct_expect_obj_eval(struct nft_object *obj, &ct->tuplehash[!dir].tuple.src.u3, &ct->tuplehash[!dir].tuple.dst.u3, priv->l4proto, NULL, &priv->dport); - exp->timeout += priv->timeout *HZ ; + exp->timeout += priv->timeout;if (nf_ct_expect_related (exp,0 ) !=0 ) regs->verdict.code =NF_DROP ;
base-commit: b869a5313cbfe7102873d3de318ddb68675146d0
Signature · 1 line
2.50.1
PATCH 2/2 netfilter: nft_ct: move custom expectation support to helper
In reply to Mark BundschuhFrom: Pablo Neira Ayuso <[email protected]>
[ Upstream commit 3679da4ad8be84cddaf40bc307fef1fe13e051ff ]
Originally, the ct expectation support called nf_ct_helper_ext_add() for confirmed conntracks, which is invalid, triggering a splat. This was fixed by commit 1710eb913bdc ("netfilter: nft_ct: skip expectations for confirmed conntrack") which restricted it to unconfirmed conntracks.
However, early insertion of expectations into the expectations list when the conntrack is unconfirmed leads to stale entries pointing to the wrong hlist_head through .pprev due to ct extension reallocation.
Commit 7c9664351980 ("netfilter: move nat hlist_head to nf_conn") moved the nat hlist_head to nf_conn for this reason:
1. ...
2. When reallocation of extension area occurs we need to fixup the bysource hash head via hlist_replace_rcu.
I'd rather not increase the size of the struct nf_conn for this feature has very limited scope: only one expectation can be created at a time given expect_clash() will make nf_ct_expect_related() reports EBUSY. For this reason, relax nf_ct_expect_related() not to drop packets in case expectation creation fails, therefore, expectation creation becomes best effort.
To address this issue, add an internal ct helper and attach it to the conntrack entry to streamline the custom ct expectation support with existing ct helpers.
Expose a new nf_conntrack_helper_release() function to release the internal helper that is allocated and attached to the conntrack entry to create the custom expectations. The nft_ct module removal always waits for rcu grace period, then the NULL helper callback is observed after this.
This patch also restricts the creation of expectations to different helpers other than this custom helper that is created for this type of expectations.
[ - ac46f3f35b6e ("netfilter: nf_conntrack_helper: add refcounting from
datapath") is not backported because it reworks helper lifetime around a datapath ct_refcnt in 9 files, so one static nft_ct_expect helper is unhelped at nft_ct module exit.
- d4beefc90a66 ("netfilter: nft_ct: support expectation creation for natted flows") is not backported because it adds expectation support for NATed flows, so the moved expectation code has no NAT follow-master part.
- Context differs due to commit: 6031487d4e27 ]
--- include/net/netfilter/nf_conntrack_helper.h | 1 + net/netfilter/nf_conntrack_helper.c | 20 ++-- net/netfilter/nft_ct.c | 100 +++++++++++++++----- 3 files changed, 89 insertions(+), 32 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +1 -0
@@ -105,6 +105,7 @@ void nf_ct_helper_init(struct nf_conntrack_helper *helper,int nf_conntrack_helper_register (struct nf_conntrack_helper *);void nf_conntrack_helper_unregister (struct nf_conntrack_helper *); +void nf_conntrack_helper_release (struct nf_conntrack_helper *);int nf_conntrack_helpers_register (struct nf_conntrack_helper *,unsigned int ); void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *,
net/netfilter/nf_conntrack_helper.c +13 -7
@@ -425,6 +425,18 @@ static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)return this == me; } +void nf_conntrack_helper_release (struct nf_conntrack_helper *me) +{ +nf_ct_expect_iterate_destroy (expect_iter_me, me); +nf_ct_iterate_destroy (unhelp, me); + +/* nf_ct_iterate_destroy() does an unconditional synchronize_rcu() as +* last step, this ensures rcu readers of exp->helper are done. +* No need for another synchronize_rcu() here. +*/ +} +EXPORT_SYMBOL_GPL (nf_conntrack_helper_release); +void nf_conntrack_helper_unregister (struct nf_conntrack_helper *me) {mutex_lock (&nf_ct_helper_mutex);
@@ -437,13 +449,7 @@ void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)*/ synchronize_rcu (); -nf_ct_expect_iterate_destroy (expect_iter_me, me); -nf_ct_iterate_destroy (unhelp, me); - -/* nf_ct_iterate_destroy() does an unconditional synchronize_rcu() as -* last step, this ensures rcu readers of exp->helper are done. -* No need for another synchronize_rcu() here. -*/ +nf_conntrack_helper_release (me); }EXPORT_SYMBOL_GPL (nf_conntrack_helper_unregister);
net/netfilter/nft_ct.c +75 -25
149 lines not on this page · raw message
Signature · 1 line
2.50.1
Greg KH
maintainerQuoted 6 lines · Mark Bundschuh
Also in this series:
-6fb421bd07f1("netfilter:nft_ct: expectation timeouts are passed in milliseconds") from v7.2-rc1 is included because it fixes the timeout unit bug this branch has, and the fix's moved code uses its jiffies timeout.
Did yo uforget to include a 6.18.y series?
thanks,
greg k-h
Quoted 1 line · Greg KH
Did yo uforget to include a 6.18.y series?
I sent it a bit earlier as a separate thread (it's the same as 6.12):
https://lore.kernel.org/all/[email protected]
Thanks,
Mark