Skip to content

Commit 7e5c6aa

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nf_tables: add packets conntrack state to debug trace info
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. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 90869f4 commit 7e5c6aa

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,10 @@ enum nft_xfrm_keys {
18411841
* @NFTA_TRACE_MARK: nfmark (NLA_U32)
18421842
* @NFTA_TRACE_NFPROTO: nf protocol processed (NLA_U32)
18431843
* @NFTA_TRACE_POLICY: policy that decided fate of packet (NLA_U32)
1844+
* @NFTA_TRACE_CT_ID: conntrack id (NLA_U32)
1845+
* @NFTA_TRACE_CT_DIRECTION: packets direction (NLA_U8)
1846+
* @NFTA_TRACE_CT_STATUS: conntrack status (NLA_U32)
1847+
* @NFTA_TRACE_CT_STATE: packet state (new, established, ...) (NLA_U32)
18441848
*/
18451849
enum nft_trace_attributes {
18461850
NFTA_TRACE_UNSPEC,
@@ -1861,6 +1865,10 @@ enum nft_trace_attributes {
18611865
NFTA_TRACE_NFPROTO,
18621866
NFTA_TRACE_POLICY,
18631867
NFTA_TRACE_PAD,
1868+
NFTA_TRACE_CT_ID,
1869+
NFTA_TRACE_CT_DIRECTION,
1870+
NFTA_TRACE_CT_STATUS,
1871+
NFTA_TRACE_CT_STATE,
18641872
__NFTA_TRACE_MAX
18651873
};
18661874
#define NFTA_TRACE_MAX (__NFTA_TRACE_MAX - 1)

net/netfilter/nf_tables_trace.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/netfilter.h>
1616
#include <linux/netfilter/nfnetlink.h>
1717
#include <linux/netfilter/nf_tables.h>
18+
#include <net/netfilter/nf_conntrack.h>
1819
#include <net/netfilter/nf_tables_core.h>
1920
#include <net/netfilter/nf_tables.h>
2021

@@ -90,6 +91,49 @@ static int nf_trace_fill_dev_info(struct sk_buff *nlskb,
9091
return 0;
9192
}
9293

94+
static int nf_trace_fill_ct_info(struct sk_buff *nlskb,
95+
const struct sk_buff *skb)
96+
{
97+
const struct nf_ct_hook *ct_hook;
98+
enum ip_conntrack_info ctinfo;
99+
const struct nf_conn *ct;
100+
u32 state;
101+
102+
ct_hook = rcu_dereference(nf_ct_hook);
103+
if (!ct_hook)
104+
return 0;
105+
106+
ct = nf_ct_get(skb, &ctinfo);
107+
if (!ct) {
108+
if (ctinfo != IP_CT_UNTRACKED) /* not seen by conntrack or invalid */
109+
return 0;
110+
111+
state = NF_CT_STATE_UNTRACKED_BIT;
112+
} else {
113+
state = NF_CT_STATE_BIT(ctinfo);
114+
}
115+
116+
if (nla_put_be32(nlskb, NFTA_TRACE_CT_STATE, htonl(state)))
117+
return -1;
118+
119+
if (ct) {
120+
u32 id = ct_hook->get_id(&ct->ct_general);
121+
u32 status = READ_ONCE(ct->status);
122+
u8 dir = CTINFO2DIR(ctinfo);
123+
124+
if (nla_put_u8(nlskb, NFTA_TRACE_CT_DIRECTION, dir))
125+
return -1;
126+
127+
if (nla_put_be32(nlskb, NFTA_TRACE_CT_ID, (__force __be32)id))
128+
return -1;
129+
130+
if (status && nla_put_be32(nlskb, NFTA_TRACE_CT_STATUS, htonl(status)))
131+
return -1;
132+
}
133+
134+
return 0;
135+
}
136+
93137
static int nf_trace_fill_pkt_info(struct sk_buff *nlskb,
94138
const struct nft_pktinfo *pkt)
95139
{
@@ -210,7 +254,11 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,
210254
nla_total_size(sizeof(__be32)) + /* trace type */
211255
nla_total_size(0) + /* VERDICT, nested */
212256
nla_total_size(sizeof(u32)) + /* verdict code */
213-
nla_total_size(sizeof(u32)) + /* id */
257+
nla_total_size(sizeof(u32)) + /* ct id */
258+
nla_total_size(sizeof(u8)) + /* ct direction */
259+
nla_total_size(sizeof(u32)) + /* ct state */
260+
nla_total_size(sizeof(u32)) + /* ct status */
261+
nla_total_size(sizeof(u32)) + /* trace id */
214262
nla_total_size(NFT_TRACETYPE_LL_HSIZE) +
215263
nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE) +
216264
nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE) +
@@ -291,6 +339,10 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,
291339

292340
if (nf_trace_fill_pkt_info(skb, pkt))
293341
goto nla_put_failure;
342+
343+
if (nf_trace_fill_ct_info(skb, pkt->skb))
344+
goto nla_put_failure;
345+
294346
info->packet_dumped = true;
295347
}
296348

0 commit comments

Comments
 (0)