Skip to contentSkip to conversation

netfilter: nf_tables: include conntrack state in trace messages

PATCH Superseded Florian Westphal 2 participants 6 messages May 8, 2025 – May 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: nf_tables: include conntrack state in trace messages

Add the minimal relevant info needed for userspace ("nftables monitor trace") to provide the conntrack view of the packet:

- state (new, related, established)
- direction (original, reply)
- status (e.g., if connection is subject to dnat)
- id (allows to query ctnetlink for remaining conntrack state info)

Example:
trace id a62 inet filter PRE_RAW packet: iif "enp0s3" ether [..]
[..]
trace id a62 inet filter PRE_MANGLE conntrack: ct direction original ct state new ct id 32 trace id a62 inet filter PRE_MANGLE packet: [..]
[..]
trace id a62 inet filter IN conntrack: ct direction original ct state new ct status dnat-done ct id 32
[..]

First patch is a needed prerequisite to avoid a module dependency. Second patch adds the needed info.

Patches for libnftnl and nftables will follow shortly.

author maintainer
#1 lore raw

PATCH 1/2 netfilter: conntrack: make nf_conntrack_id callable without a module dependency

While nf_conntrack_id() doesn't need any functionaliy from conntrack, it does reside in nf_conntrack_core.c -- callers add a module dependency on conntrack.

Followup patch will need to compute the conntrack id from nf_tables_trace.c to include it in nf_trace messages emitted to userspace via netlink.

I don't want to introduce a module dependency between nf_tables and conntrack for this.

Since trace is slowpath, the added indirection is ok.

One alternative is to move nf_conntrack_id to the netfilter/core.c, but I don't see a compelling reason so far.

---
 include/linux/netfilter.h         | 1 +
 net/netfilter/nf_conntrack_core.c | 6 ++++++
 2 files changed, 7 insertions(+)
include/linux/netfilter.h +1 -0
index 2b8aac2c70ad..10d95556befe 100644
@@ -470,6 +470,7 @@ struct nf_ct_hook {
 	void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb);
 	void (*set_closing)(struct nf_conntrack *nfct);
 	int (*confirm)(struct sk_buff *skb);
+	u32 (*get_id)(const struct nf_conntrack *nfct);
 };
 extern const struct nf_ct_hook __rcu *nf_ct_hook;
 
net/netfilter/nf_conntrack_core.c +6 -0
index de8d50af9b5b..201d3c4ec623 100644
@@ -505,6 +505,11 @@ u32 nf_ct_get_id(const struct nf_conn *ct)
 }
 EXPORT_SYMBOL_GPL(nf_ct_get_id);
 
+static u32 nf_conntrack_get_id(const struct nf_conntrack *nfct)
+{
+	return nf_ct_get_id(nf_ct_to_nf_conn(nfct));
+}
+
 static void
 clean_from_lists(struct nf_conn *ct)
 {
@@ -2710,6 +2715,7 @@ static const struct nf_ct_hook nf_conntrack_hook = {
 	.attach		= nf_conntrack_attach,
 	.set_closing	= nf_conntrack_set_closing,
 	.confirm	= __nf_conntrack_confirm,
+	.get_id		= nf_conntrack_get_id,
 };
 
 void nf_conntrack_init_end(void)
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH 2/2 netfilter: nf_tables: add packets conntrack state to debug trace info

In reply to Florian Westphal

Add the minimal relevant info needed for userspace ("nftables monitor trace") to provide the conntrack view of the packet:

- state (new, related, established)
- direction (original, reply)
- status (e.g., if connection is subject to dnat)
- id (allows to query ctnetlink for remaining conntrack state info)

Example:
trace id a62 inet filter PRE_RAW packet: iif "enp0s3" ether [..]
[..]
trace id a62 inet filter PRE_MANGLE conntrack: ct direction original ct state new ct id 32 trace id a62 inet filter PRE_MANGLE packet: [..]
[..]
trace id a62 inet filter IN conntrack: ct direction original ct state new ct status dnat-done ct id 32
[..]

In this case one can see that while NAT is active, the new connection isn't subject to a translation.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nf_tables_trace.c          | 65 +++++++++++++++++++++++-
 2 files changed, 66 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 7d6bc19a0153..19cddbd1a315 100644
@@ -1841,6 +1841,7 @@ enum nft_xfrm_keys {
  * @NFTA_TRACE_MARK: nfmark (NLA_U32)
  * @NFTA_TRACE_NFPROTO: nf protocol processed (NLA_U32)
  * @NFTA_TRACE_POLICY: policy that decided fate of packet (NLA_U32)
+ * @NFTA_TRACE_CT: connection tracking information (NLA_NESTED: nft_ct_keys)
  */
 enum nft_trace_attributes {
 	NFTA_TRACE_UNSPEC,
@@ -1861,6 +1862,7 @@ enum nft_trace_attributes {
 	NFTA_TRACE_NFPROTO,
 	NFTA_TRACE_POLICY,
 	NFTA_TRACE_PAD,
+	NFTA_TRACE_CT,
 	__NFTA_TRACE_MAX
 };
 #define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)
net/netfilter/nf_tables_trace.c +64 -1

89 lines not on this page · raw message

Signature · 1 line
2.49.0
#3 lore raw

Re: [PATCH nf-next 2/2] netfilter: nf_tables: add packets conntrack state to debug trace info

Hi Florian,

Quoted 25 lines · Florian Westphal

Add the minimal relevant info needed for userspace ("nftables monitor trace") to provide the conntrack view of the packet:

- state (new, related, established)
- direction (original, reply)
- status (e.g., if connection is subject to dnat)
- id (allows to query ctnetlink for remaining conntrack state info)

Example:
trace id a62 inet filter PRE_RAW packet: iif "enp0s3" ether [..]
[..]
trace id a62 inet filter PRE_MANGLE conntrack: ct direction original ct state new ct id 32 trace id a62 inet filter PRE_MANGLE packet: [..]
[..]
trace id a62 inet filter IN conntrack: ct direction original ct state new ct status dnat-done ct id 32
[..]

In this case one can see that while NAT is active, the new connection isn't subject to a translation.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nf_tables_trace.c          | 65 +++++++++++++++++++++++-
 2 files changed, 66 insertions(+), 1 deletion(-)

[...]

Quoted 4 lines
net/netfilter/nf_tables_trace.c +0 -0
index 580c55268f65..ba8b0a8c00e6 100644

[...]

Quoted 6 lines
net/netfilter/nf_tables_trace.c +6 -0
+	if (nla_put_be32(nlskb, NFT_CT_STATE, htonl(state)))
+		goto nla_put_failure;
+
+	if (ct) {
+		u32 id = ct_hook->get_id(&ct->ct_general);
+		u32 status = READ_ONCE(ct->status);

net/netfilter/nf_tables_trace.c static int nf_trace_fill_dev_info(struct sk_buff *nlskb, · line 136

Quoted 10 lines
net/netfilter/nf_tables_trace.c +10 -0
+		u8 dir = CTINFO2DIR(ctinfo);
+
+		if (nla_put_u8(nlskb, NFT_CT_DIRECTION, dir))
+			goto nla_put_failure;
+
+		if (nla_put_be32(nlskb, NFT_CT_ID, (__force __be32)id))
+			goto nla_put_failure;
+
+		if (status && nla_put_be32(nlskb, NFT_CT_STATUS, htonl(status)))
+			goto nla_put_failure;

NFT_CT_* is enum nft_ct_keys which is not intended to be used as netlink attribute.

NFT_CT_STATE is 0 which is usually reserved for _UNSPEC in netlink attribute definitions.

My suggestion is that you define new attributes for this, it is boilerplate code to be added to uapi.

Thanks.

author maintainer
#4 lore raw

Re: [PATCH nf-next 2/2] netfilter: nf_tables: add packets conntrack state to debug trace info

Quoted 9 lines · Pablo Neira Ayuso

net/netfilter/nf_tables_trace.c static int nf_trace_fill_dev_info(struct sk_buff *nlskb, · line 136

Quoted 5 lines
net/netfilter/nf_tables_trace.c +5 -0
+		if (nla_put_be32(nlskb, NFT_CT_ID, (__force __be32)id))
+			goto nla_put_failure;
+
+		if (status && nla_put_be32(nlskb, NFT_CT_STATUS, htonl(status)))
+			goto nla_put_failure;

NFT_CT_* is enum nft_ct_keys which is not intended to be used as netlink attribute.

Quoted 5 lines

NFT_CT_STATE is 0 which is usually reserved for _UNSPEC in netlink attribute definitions.

My suggestion is that you define new attributes for this, it is boilerplate code to be added to uapi.

In that case I would prefer not to use NESTED attribute for this, i.e.:

* @NFTA_TRACE_CT_ID: connection tracking information (NLA_U32)
* @NFTA_TRACE_CT_STATUS: connection tracking information (NLA_U32)
* @NFTA_TRACE_CT_STATE: connection tracking information (NLA_U32)

... and so on. I see no potential for attribute re-use.

The only argument for NESTED is that userspace can check for presence of NFTA_TRACE_CT/NESTED instead of checking each ct trace attr in sequence.

Whats you preference?

#5 lore raw

Re: [PATCH nf-next 2/2] netfilter: nf_tables: add packets conntrack state to debug trace info

Quoted 22 lines · Florian Westphal
Quoted 9 lines · Pablo Neira Ayuso

net/netfilter/nf_tables_trace.c static int nf_trace_fill_dev_info(struct sk_buff *nlskb, · line 136

Quoted 5 lines
net/netfilter/nf_tables_trace.c +5 -0
+		if (nla_put_be32(nlskb, NFT_CT_ID, (__force __be32)id))
+			goto nla_put_failure;
+
+		if (status && nla_put_be32(nlskb, NFT_CT_STATUS, htonl(status)))
+			goto nla_put_failure;

NFT_CT_* is enum nft_ct_keys which is not intended to be used as netlink attribute.

Quoted 5 lines

NFT_CT_STATE is 0 which is usually reserved for _UNSPEC in netlink attribute definitions.

My suggestion is that you define new attributes for this, it is boilerplate code to be added to uapi.

In that case I would prefer not to use NESTED attribute for this, i.e.:

* @NFTA_TRACE_CT_ID: connection tracking information (NLA_U32)
* @NFTA_TRACE_CT_STATUS: connection tracking information (NLA_U32)
* @NFTA_TRACE_CT_STATE: connection tracking information (NLA_U32)

Quoted 7 lines

... and so on. I see no potential for attribute re-use.

The only argument for NESTED is that userspace can check for presence of NFTA_TRACE_CT/NESTED instead of checking each ct trace attr in sequence.

Whats you preference?

Flat representation (no nesting) is fine with me in this case.

Thanks.