From: Fernando Fernandez Mancera <fmancera@suse.de>
To: netfilter-devel@vger.kernel.org
Cc: coreteam@netfilter.org, pablo@netfilter.org, fw@strlen.de,
phil@nwl.cc, aconole@redhat.com, echaudro@redhat.com,
i.maximets@ovn.org, Fernando Fernandez Mancera <fmancera@suse.de>
Subject: [PATCH 1/4 nf-next v2] netfilter: conntrack: add nf_ct_get_or_find() helper
Date: Mon, 10 Nov 2025 16:42:46 +0100 [thread overview]
Message-ID: <20251110154249.3586-2-fmancera@suse.de> (raw)
In-Reply-To: <20251110154249.3586-1-fmancera@suse.de>
Introduce a helper function which tries to get a ct, if it is a
template, then the tuple pointer from the skb and perform a lookup to
find the actual ct.
A refcounted bool parameter must be passed in order to know if the
caller need to call nf_ct_put() after it is done using the ct.
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
---
v2: added this commit to the series
---
include/net/netfilter/nf_conntrack.h | 3 +++
net/netfilter/nf_conntrack_core.c | 35 ++++++++++++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/include/net/netfilter/nf_conntrack.h b/include/net/netfilter/nf_conntrack.h
index aa0a7c82199e..5a0dd5715122 100644
--- a/include/net/netfilter/nf_conntrack.h
+++ b/include/net/netfilter/nf_conntrack.h
@@ -201,6 +201,9 @@ bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
u_int16_t l3num, struct net *net,
struct nf_conntrack_tuple *tuple);
+struct nf_conn *nf_ct_get_or_find(struct net *net, const struct sk_buff *skb,
+ u16 l3num, bool *refcounted);
+
void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
u32 extra_jiffies, unsigned int bytes);
diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c
index 0b95f226f211..3ef152a378cc 100644
--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -428,6 +428,41 @@ bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
}
EXPORT_SYMBOL_GPL(nf_ct_get_tuplepr);
+struct nf_conn *
+nf_ct_get_or_find(struct net *net, const struct sk_buff *skb, u16 l3num,
+ bool *refcounted)
+{
+ const struct nf_conntrack_tuple_hash *h;
+ const struct nf_conntrack_zone *zone;
+ struct nf_conntrack_tuple tuple;
+ enum ip_conntrack_info ctinfo;
+ struct nf_conn *ct;
+
+ ct = nf_ct_get(skb, &ctinfo);
+ if (ct && !nf_ct_is_template(ct))
+ return ct;
+
+ if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num, net, &tuple))
+ return NULL;
+
+ if (ct)
+ zone = nf_ct_zone(ct);
+ else
+ zone = &nf_ct_zone_dflt;
+
+ h = nf_conntrack_find_get(net, zone, &tuple);
+ if (!h)
+ return NULL;
+
+ ct = nf_ct_tuplehash_to_ctrack(h);
+
+ /* refcount increased, nf_ct_put() is needed after done with it */
+ *refcounted = true;
+
+ return ct;
+}
+EXPORT_SYMBOL_GPL(nf_ct_get_or_find);
+
bool
nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
const struct nf_conntrack_tuple *orig)
--
2.51.0
next prev parent reply other threads:[~2025-11-10 15:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-10 15:42 [PATCH 0/4 nf-next v2] netfilter: rework conncount API to receive ct directly Fernando Fernandez Mancera
2025-11-10 15:42 ` Fernando Fernandez Mancera [this message]
2025-11-10 15:42 ` [PATCH 2/4 nf-next v2] netfilter: nf_conncount: only track connection if it is not confirmed Fernando Fernandez Mancera
2025-11-10 20:44 ` Florian Westphal
2025-11-10 21:40 ` Fernando Fernandez Mancera
2025-11-10 15:42 ` [PATCH 3/4 nf-next v2] netfilter: nf_conncount: make nf_conncount_gc_list() to disable BH Fernando Fernandez Mancera
2025-11-10 15:42 ` [PATCH 4/4 nf-next v2] netfilter: nft_connlimit: update connection list if add was skipped Fernando Fernandez Mancera
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251110154249.3586-2-fmancera@suse.de \
--to=fmancera@suse.de \
--cc=aconole@redhat.com \
--cc=coreteam@netfilter.org \
--cc=echaudro@redhat.com \
--cc=fw@strlen.de \
--cc=i.maximets@ovn.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
--cc=phil@nwl.cc \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.