netfilter: ctnetlink: add timeout and protoinfo to destroy events
Subsystems (2)
Florian Westphal
maintainerPATCH v2 1/1 netfilter: ctnetlink: add timeout and protoinfo to destroy events
DESTROY events do not include the remaining timeout.
Unconditionally add the timeout so one can see if the entry timed out or was removed explicitly.
The latter case can happen when a conntrack gets deleted prematurely, e.g. due to a tcp reset, module removal, netdev notifier (nat/masquerade device went down), ctnetlink and so on.
Pablo suggested to also add the tcp (or other l4 tracker) information to help with debugging.
--- net/netfilter/nf_conntrack_netlink.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_netlink.c +7 -3
@@ -779,14 +779,18 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)if (ctnetlink_dump_status (skb, ct) <0 ) goto nla_put_failure; +if (ctnetlink_dump_timeout (skb, ct) <0 ) + goto nla_put_failure; +if (events & (1 <<IPCT_DESTROY )) { +/* always add protoinfo here */ +if (ctnetlink_dump_protoinfo (skb, ct) <0 ) + goto nla_put_failure; +if (ctnetlink_dump_acct (skb, ct, type) <0 ||ctnetlink_dump_timestamp (skb, ct) <0 ) goto nla_put_failure; }else { -if (ctnetlink_dump_timeout (skb, ct) <0 ) - goto nla_put_failure; -if (events & (1 <<IPCT_PROTOINFO ) &&ctnetlink_dump_protoinfo (skb, ct) <0 ) goto nla_put_failure;
Signature · 1 line
2.26.2
Pablo Neira Ayuso
maintainerHi Florian,
Quoted 11 lines · Florian Westphal
DESTROY events do not include the remaining timeout.
Unconditionally add the timeout so one can see if the entry timed out or was removed explicitly.
The latter case can happen when a conntrack gets deleted prematurely, e.g. due to a tcp reset, module removal, netdev notifier (nat/masquerade device went down), ctnetlink and so on.
Pablo suggested to also add the tcp (or other l4 tracker) information to help with debugging.
Probably a bit more fine grain, only the _STATE attribute.
See attached patch, it's an extension of yours.
From fa0b0008d2681f6ca088e1dbb09dfb9898463951 Mon Sep 17 00:00:00 2001
From: Florian Westphal <[email protected]>
Date: Thu, 10 Dec 2020 14:43:23 +0100
Subject: [PATCH] netfilter: ctnetlink: add timeout and protoinfo to destroy
events
DESTROY events do not include the remaining timeout.
Unconditionally add the timeout so one can see if the entry timed out or was removed explicitly.
The latter case can happen when a conntrack gets deleted prematurely, e.g. due to a tcp reset, module removal, netdev notifier (nat/masquerade device went down), ctnetlink and so on.
Pablo suggested to also add the tcp (or other l4 tracker) information to help with debugging.
--- include/net/netfilter/nf_conntrack_l4proto.h | 2 +- net/netfilter/nf_conntrack_netlink.c | 20 +++++++++++--------- net/netfilter/nf_conntrack_proto_dccp.c | 13 ++++++++++--- net/netfilter/nf_conntrack_proto_sctp.c | 13 +++++++++---- net/netfilter/nf_conntrack_proto_tcp.c | 10 ++++++++-- 5 files changed, 39 insertions(+), 19 deletions(-)
include/net/netfilter/nf_conntrack_l4proto.h +1 -1
@@ -32,7 +32,7 @@ struct nf_conntrack_l4proto {/* convert protoinfo to nfnetink attributes */ int (*to_nlattr)(struct sk_buff *skb,struct nlattr *nla, -struct nf_conn *ct); +struct nf_conn *ct,bool destroy);/* convert nfnetlink attributes to protoinfo */ int (*from_nlattr)(struct nlattr *tb[],struct nf_conn *ct);
net/netfilter/nf_conntrack_netlink.c +11 -9
@@ -179,7 +179,8 @@ static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)return -1 ; } -static int ctnetlink_dump_protoinfo (struct sk_buff *skb,struct nf_conn *ct) +static int ctnetlink_dump_protoinfo (struct sk_buff *skb,struct nf_conn *ct, +bool destroy) {const struct nf_conntrack_l4proto *l4proto;struct nlattr *nest_proto;
@@ -193,7 +194,7 @@ static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)if (!nest_proto) goto nla_put_failure; - ret = l4proto->to_nlattr (skb, nest_proto, ct); + ret = l4proto->to_nlattr (skb, nest_proto, ct, destroy);nla_nest_end (skb, nest_proto);
@@ -538,7 +539,7 @@ static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct)if (!test_bit (IPS_OFFLOAD_BIT , &ct->status) && (ctnetlink_dump_timeout (skb, ct) <0 || -ctnetlink_dump_protoinfo (skb, ct) <0 )) +ctnetlink_dump_protoinfo (skb, ct, false) <0 ))return -1 ;return 0 ;
@@ -779,16 +780,17 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)if (ctnetlink_dump_status (skb, ct) <0 ) goto nla_put_failure; +if (ctnetlink_dump_timeout (skb, ct) <0 ) + goto nla_put_failure; +if (events & (1 <<IPCT_DESTROY )) {if (ctnetlink_dump_acct (skb, ct, type) <0 || -ctnetlink_dump_timestamp (skb, ct) <0 ) +ctnetlink_dump_timestamp (skb, ct) <0 || +ctnetlink_dump_protoinfo (skb, ct, true) <0 ) goto nla_put_failure; }else { -if (ctnetlink_dump_timeout (skb, ct) <0 ) - goto nla_put_failure; -if (events & (1 <<IPCT_PROTOINFO ) - &&ctnetlink_dump_protoinfo (skb, ct) <0 ) + &&ctnetlink_dump_protoinfo (skb, ct, false) <0 ) goto nla_put_failure; if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
@@ -2723,7 +2725,7 @@ static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)if (ctnetlink_dump_timeout (skb, ct) <0 ) goto nla_put_failure; -if (ctnetlink_dump_protoinfo (skb, ct) <0 ) +if (ctnetlink_dump_protoinfo (skb, ct, false) <0 ) goto nla_put_failure;if (ctnetlink_dump_helpinfo (skb, ct) <0 )
net/netfilter/nf_conntrack_proto_dccp.c +10 -3
@@ -589,7 +589,7 @@ static void dccp_print_conntrack(struct seq_file *s, struct nf_conn *ct)#if IS_ENABLED (CONFIG_NF_CT_NETLINK )static int dccp_to_nlattr (struct sk_buff *skb,struct nlattr *nla, -struct nf_conn *ct) +struct nf_conn *ct,bool destroy) {struct nlattr *nest_parms;
@@ -597,15 +597,22 @@ static int dccp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, nest_parms =nla_nest_start (skb,CTA_PROTOINFO_DCCP );if (!nest_parms) goto nla_put_failure; -if (nla_put_u8 (skb,CTA_PROTOINFO_DCCP_STATE , ct->proto.dccp.state) || -nla_put_u8 (skb,CTA_PROTOINFO_DCCP_ROLE , +if (nla_put_u8 (skb,CTA_PROTOINFO_DCCP_STATE , ct->proto.dccp.state)) + goto nla_put_failure; + +if (destroy) + goto skip_state; + +if (nla_put_u8 (skb,CTA_PROTOINFO_DCCP_ROLE , ct->proto.dccp.role[IP_CT_DIR_ORIGINAL ]) ||nla_put_be64 (skb,CTA_PROTOINFO_DCCP_HANDSHAKE_SEQ ,cpu_to_be64 (ct->proto.dccp.handshake_seq),CTA_PROTOINFO_DCCP_PAD )) goto nla_put_failure; +skip_state:nla_nest_end (skb, nest_parms);spin_unlock_bh (&ct->lock); +return 0 ; nla_put_failure:
net/netfilter/nf_conntrack_proto_sctp.c +9 -4
@@ -543,7 +543,7 @@ static bool sctp_can_early_drop(const struct nf_conn *ct)#include <linux/netfilter/nfnetlink_conntrack.h> static int sctp_to_nlattr (struct sk_buff *skb,struct nlattr *nla, -struct nf_conn *ct) +struct nf_conn *ct,bool destroy) {struct nlattr *nest_parms;
@@ -552,15 +552,20 @@ static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,if (!nest_parms) goto nla_put_failure; -if (nla_put_u8 (skb,CTA_PROTOINFO_SCTP_STATE , ct->proto.sctp.state) || -nla_put_be32 (skb,CTA_PROTOINFO_SCTP_VTAG_ORIGINAL , +if (nla_put_u8 (skb,CTA_PROTOINFO_SCTP_STATE , ct->proto.sctp.state)) + goto nla_put_failure; + +if (destroy) + goto skip_state; + +if (nla_put_be32 (skb,CTA_PROTOINFO_SCTP_VTAG_ORIGINAL , ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL ]) ||nla_put_be32 (skb,CTA_PROTOINFO_SCTP_VTAG_REPLY , ct->proto.sctp.vtag[IP_CT_DIR_REPLY ])) goto nla_put_failure; +skip_state:spin_unlock_bh (&ct->lock); -nla_nest_end (skb, nest_parms);return 0 ;
net/netfilter/nf_conntrack_proto_tcp.c +8 -2
@@ -1186,7 +1186,7 @@ static bool tcp_can_early_drop(const struct nf_conn *ct)#include <linux/netfilter/nfnetlink_conntrack.h> static int tcp_to_nlattr (struct sk_buff *skb,struct nlattr *nla, -struct nf_conn *ct) +struct nf_conn *ct,bool destroy) {struct nlattr *nest_parms;struct nf_ct_tcp_flags tmp = {};
@@ -1196,6 +1196,12 @@ static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,if (!nest_parms) goto nla_put_failure; +if (nla_put_u8 (skb,CTA_PROTOINFO_TCP_STATE , ct->proto.tcp.state)) + goto nla_put_failure; + +if (destroy) + goto skip_state; + if (nla_put_u8(skb, CTA_PROTOINFO_TCP_STATE, ct->proto.tcp.state) || nla_put_u8(skb, CTA_PROTOINFO_TCP_WSCALE_ORIGINAL, ct->proto.tcp.seen[0].td_scale) ||
@@ -1212,8 +1218,8 @@ static int tcp_to_nlattr(struct sk_buff *skb, struct nlattr *nla,if (nla_put (skb,CTA_PROTOINFO_TCP_FLAGS_REPLY ,sizeof (struct nf_ct_tcp_flags ), &tmp)) goto nla_put_failure; +skip_state:spin_unlock_bh (&ct->lock); -nla_nest_end (skb, nest_parms);return 0 ;
Signature · 1 line
2.20.1