netfilter: nf_conntrack_expect: use conntrack GC to reap expectations
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v6 1/1 netfilter: nf_conntrack_expect: use conntrack GC to reap expectations
This patch replaces the timer API by GC worker approach for expectations, as it already happened in many other subsystems.
Use the existing conntrack GC worker to iterate over the local list of expectations in the master conntrack to reap expired expectations. Check IPS_HELPER_BIT to run GC for expectations, set it on for nft_ct expectation which nevers sets it. Hold the expectation spinlock while iterating over the master conntrack expectation list to synchronize with nf_ct_remove_expectations(). This also performs runtime packet path garbage collection through the expectation insertion and lookup functions while walking over one of the chains of the global expectation hashtables. Unconfirmed conntrack entries are skipped since ct->ext can be reallocated and dying are skipped since those will be gone soon. Set on IPS_HELPER_BIT if the helper ct extension is added, then the new GC worker does not need to bump the ct refcount to check if the ct->ext helper is available.
This removes the extra bump on the refcount for expectation timers, this allows to remove several nf_ct_expect_put() calls after the unlink, after this update only refcount remains at 1 while on the expectation hashes.
This patch implicitly addresses a race with the existing timer API allowing an expectation to access a stale exp->master pointer which has been already released when expectation removal loses races with an expiring timer, ie. timer_del() reporting false.
Add a new NF_CT_EXPECT_DEAD flag to reap this expectation via GC. This is needed by nf_conntrack_unexpect_related() which is called in error paths to invalidate newly created expectations that has been added into the hashes. These expectactions cannot be inmediately released as GC or nf_ct_remove_expectations() could race to make it. On expectation insert, the runtime GC reaps stale expectations before checking the expectation limit set by policy.
Set current timestamp in nf_ct_expect_alloc(), then add the expectation policy timeout (or custom timeout specified added on top of this) to specify the expectation lifetime.
---
v6: - remove check for IPS_HELPER_BIT in nft_ct expect.
- from GC, check that conntrack is confirmed and !dying before walking the expectation list to deal with possible typesafe_rcu scenario with recycled conntrack.
- ignore AI reviewer comment on possible double unlink in del_expect() in ctnetlink, spinlock is held when calling expect_find_get().
include/net/netfilter/nf_conntrack_expect.h | 16 +- .../linux/netfilter/nf_conntrack_common.h | 1 + net/netfilter/nf_conntrack_core.c | 33 +++- net/netfilter/nf_conntrack_expect.c | 145 +++++++++--------- net/netfilter/nf_conntrack_h323_main.c | 4 +- net/netfilter/nf_conntrack_helper.c | 10 +- net/netfilter/nf_conntrack_netlink.c | 22 ++- net/netfilter/nf_conntrack_sip.c | 13 +- net/netfilter/nft_ct.c | 3 +- 9 files changed, 139 insertions(+), 108 deletions(-)
include/net/netfilter/nf_conntrack_expect.h +13 -3
@@ -54,8 +54,8 @@ struct nf_conntrack_expect {/* The conntrack of the master connection */ struct nf_conn *master; -/* Timer function; deletes the expectation. */ -struct timer_list timeout; +/* jiffies32 when this expectation expires */ +u32 timeout;#if IS_ENABLED (CONFIG_NF_NAT )union nf_inet_addr saved_addr;
@@ -69,6 +69,14 @@ struct nf_conntrack_expect {struct rcu_head rcu; }; +static inline bool nf_ct_exp_is_expired (const struct nf_conntrack_expect *exp) +{ +if (READ_ONCE (exp->flags) &NF_CT_EXPECT_DEAD ) +return true; + +return (__s32)(READ_ONCE (exp->timeout) - nfct_time_stamp) <=0 ; +} +static inline struct net *nf_ct_exp_net (struct nf_conntrack_expect *exp) {return read_pnet (&exp->net);
@@ -130,7 +138,6 @@ static inline void nf_ct_unlink_expect(struct nf_conntrack_expect *exp)void nf_ct_remove_expectations (struct nf_conn *ct);void nf_ct_unexpect_related (struct nf_conntrack_expect *exp); -bool nf_ct_remove_expect (struct nf_conntrack_expect *exp);void nf_ct_expect_iterate_destroy (bool (*iter)(struct nf_conntrack_expect *e,void *data),void *data); void nf_ct_expect_iterate_net(struct net *net,
@@ -153,5 +160,8 @@ static inline int nf_ct_expect_related(struct nf_conntrack_expect *expect, return nf_ct_expect_related_report(expect, 0, 0, flags); } +struct nf_conn_help ; +void nf_ct_expectation_gc (struct nf_conn_help *master_help); +#endif /*_NF_CONNTRACK_EXPECT_H*/
include/uapi/linux/netfilter/nf_conntrack_common.h +1 -0
@@ -160,6 +160,7 @@ enum ip_conntrack_expect_events {#define NF_CT_EXPECT_USERSPACE 0x4#ifdef __KERNEL__ +#define NF_CT_EXPECT_DEAD 0x8#define NF_CT_EXPECT_MASK (NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE | \ NF_CT_EXPECT_USERSPACE)#endif
net/netfilter/nf_conntrack_core.c +32 -1
@@ -1471,6 +1471,31 @@ static bool gc_worker_can_early_drop(const struct nf_conn *ct)return false; } +static void nf_ct_help_gc (struct nf_conn *ct) +{ +struct nf_conn_help *help; + +if (!refcount_inc_not_zero (&ct->ct_general.use)) +return ; + +/* load ->status after refcount increase */ +smp_acquire__after_ctrl_dep (); + +if (!nf_ct_is_confirmed (ct) ||nf_ct_is_dying (ct)) { +nf_ct_put (ct); +return ; + } + +/* re-check helper due to SLAB_TYPESAFE_BY_RCU */ +if (test_bit (IPS_HELPER_BIT , &ct->status)) { + help =nfct_help (ct); +if (help) +nf_ct_expectation_gc (help); + } + +nf_ct_put (ct); +} +static void gc_worker (struct work_struct *work) {unsigned int i, hashsz, nf_conntrack_max95 =0 ;
@@ -1543,7 +1568,13 @@ static void gc_worker(struct work_struct *work) expires = (expires - (long )next_run) / ++count; next_run += expires; -if (nf_conntrack_max95 ==0 ||gc_worker_skip_ct (tmp)) +if (gc_worker_skip_ct (tmp)) +continue ; + +if (test_bit (IPS_HELPER_BIT , &tmp->status)) +nf_ct_help_gc (tmp); + +if (nf_conntrack_max95 ==0 )continue ; net =nf_ct_net (tmp);
net/netfilter/nf_conntrack_expect.c +72 -73
293 lines not on this page · raw message
net/netfilter/nf_conntrack_h323_main.c +2 -2
@@ -1388,8 +1388,8 @@ static int process_rcf(struct sk_buff *skb, struct nf_conn *ct,"timeout to %u seconds for" , info->timeout);nf_ct_dump_tuple (&exp->tuple); -mod_timer_pending (&exp->timeout, - jiffies + info->timeout *HZ ); +WRITE_ONCE (exp->timeout, + nfct_time_stamp + (info->timeout *HZ )); }spin_unlock_bh (&nf_conntrack_expect_lock); }
net/netfilter/nf_conntrack_helper.c +4 -6
@@ -181,10 +181,10 @@ nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)struct nf_conn_help *help; help =nf_ct_ext_add (ct,NF_CT_EXT_HELPER , gfp); -if (help) +if (help) { +__set_bit (IPS_HELPER_BIT , &ct->status);INIT_HLIST_HEAD (&help->expectations); -else -pr_debug ("failed to add helper extension area" ); + }return help; }EXPORT_SYMBOL_GPL (nf_ct_helper_ext_add);
@@ -203,10 +203,8 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,return 0 ; help =nfct_help (tmpl); -if (help !=NULL ) { +if (help) helper =rcu_dereference (help->helper); -set_bit (IPS_HELPER_BIT , &ct->status); - } help =nfct_help (ct);
net/netfilter/nf_conntrack_netlink.c +9 -13
@@ -3014,8 +3014,8 @@ static int ctnetlink_exp_dump_expect(struct sk_buff *skb, const struct nf_conntrack_expect *exp) { + __s32 timeout = (__s32)(READ_ONCE(exp->timeout) - nfct_time_stamp) / HZ;struct nf_conn *master = exp->master; -long timeout = ((long )exp->timeout.expires - (long )jiffies) /HZ ;struct nf_conntrack_helper *helper;#if IS_ENABLED (CONFIG_NF_NAT )struct nlattr *nest_parms;
@@ -3178,6 +3178,9 @@ ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) restart:hlist_for_each_entry_rcu (exp, &nf_ct_expect_hash[cb->args[0 ]], hnode) { +if (nf_ct_exp_is_expired (exp)) +continue ; +if (l3proto && exp->tuple.src.l3num != l3proto)continue ;
@@ -3456,11 +3459,8 @@ static int ctnetlink_del_expect(struct sk_buff *skb, }/* after list removal, usage count == 1 */ -if (timer_delete (&exp->timeout)) { -nf_ct_unlink_expect_report (exp,NETLINK_CB (skb).portid, -nlmsg_report (info->nlh)); -nf_ct_expect_put (exp); - } +nf_ct_unlink_expect_report (exp,NETLINK_CB (skb).portid, +nlmsg_report (info->nlh));spin_unlock_bh (&nf_conntrack_expect_lock);/* have to put what we 'get' above. * after this line usage count == 0 */
@@ -3484,14 +3484,10 @@ static int ctnetlink_change_expect(struct nf_conntrack_expect *x, const struct nlattr * const cda[]) { - if (cda[CTA_EXPECT_TIMEOUT]) { - if (!timer_delete(&x->timeout)) - return -ETIME; + if (cda[CTA_EXPECT_TIMEOUT]) + WRITE_ONCE(x->timeout, nfct_time_stamp + + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ); - x->timeout.expires = jiffies + -ntohl (nla_get_be32 (cda[CTA_EXPECT_TIMEOUT ])) *HZ ; -add_timer (&x->timeout); - }return 0 ; }
net/netfilter/nf_conntrack_sip.c +5 -8
@@ -897,11 +897,10 @@ static int refresh_signalling_expectation(struct nf_conn *ct, exp->tuple.dst.protonum != proto || exp->tuple.dst.u.udp.port != port) continue; -if (mod_timer_pending (&exp->timeout, jiffies + expires *HZ )) { - exp->flags &= ~NF_CT_EXPECT_INACTIVE ; - found =1 ; -break ; - } +WRITE_ONCE (exp->timeout, nfct_time_stamp + (expires *HZ )); +WRITE_ONCE (exp->flags, exp->flags & ~NF_CT_EXPECT_INACTIVE ); + found =1 ; +break ; }spin_unlock_bh (&nf_conntrack_expect_lock);return found;
@@ -920,8 +919,7 @@ static void flush_expectations(struct nf_conn *ct, bool media)hlist_for_each_entry_safe (exp, next, &help->expectations, lnode) {if ((exp->class !=SIP_EXPECT_SIGNALLING ) ^ media)continue ; -if (!nf_ct_remove_expect (exp)) -continue ; +nf_ct_unlink_expect (exp);if (!media)break ; }
@@ -1413,7 +1411,6 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,nf_ct_expect_init (exp,SIP_EXPECT_SIGNALLING ,nf_ct_l3num (ct), saddr, &daddr, proto,NULL , &port); - exp->timeout.expires = sip_timeout *HZ ;rcu_assign_pointer (exp->assign_helper, helper); exp->flags =NF_CT_EXPECT_PERMANENT |NF_CT_EXPECT_INACTIVE ;
net/netfilter/nft_ct.c +1 -2
@@ -1145,7 +1145,6 @@ static void nft_ct_helper_obj_eval(struct nft_object *obj, help =nf_ct_helper_ext_add (ct,GFP_ATOMIC );if (help &&refcount_inc_not_zero (&to_assign->ct_refcnt)) {rcu_assign_pointer (help->helper, to_assign); -set_bit (IPS_HELPER_BIT , &ct->status);if ((ct->status &IPS_NAT_MASK ) && !nfct_seqadj (ct))if (!nfct_seqadj_ext_add (ct))
@@ -1326,7 +1325,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.expires = jiffies + priv->timeout *HZ ; + exp->timeout += priv->timeout *HZ ;if (nf_ct_expect_related (exp,0 ) !=0 ) regs->verdict.code =NF_DROP ;
Signature · 1 line
2.47.3