netfilter: conntrack related cleanups
Subsystems (5)
Florian Westphal
maintainercover netfilter: conntrack related cleanups
This patch series contains cleanups to conntrack and related users such as ovs and act_ct.
First patch converts conntrack reference counting to refcount_t api. Second patch gets rid of ip_ct_attach hook, we can use existing nf_ct_hook for this.
Third patch constifies a couple of structures that don't need to be writeable.
Last two patches splits nf_ct_put and nf_conntrack_put. These functions still do the same thing, but now only nf_conntrack_put uses the nf_ct_hook indirection, nf_ct_put uses a direct call. Virtually all places should use nf_ct_put -- only core kernel code needs to use the indirection.
Before this change, nf_ct_put was merely an alias for nf_conntrack_put so even conntrack itself did additional indirection.
Florian Westphal (5):
netfilter: conntrack: convert to refcount_t api
netfilter: core: move ip_ct_attach indirection to struct nf_ct_hook
netfilter: make function op structures const
netfilter: conntrack: avoid useless indirection during conntrack
destruction
net: prefer nf_ct_put instead of nf_conntrack_put
include/linux/netfilter.h | 10 ++-- include/linux/netfilter/nf_conntrack_common.h | 10 ++-- include/net/netfilter/nf_conntrack.h | 8 ++- net/netfilter/core.c | 29 +++++------ net/netfilter/nf_conntrack_core.c | 50 +++++++++---------- net/netfilter/nf_conntrack_expect.c | 4 +- net/netfilter/nf_conntrack_netlink.c | 10 ++-- net/netfilter/nf_conntrack_standalone.c | 4 +- net/netfilter/nf_flow_table_core.c | 2 +- net/netfilter/nf_nat_core.c | 2 +- net/netfilter/nf_synproxy_core.c | 1 - net/netfilter/nfnetlink_queue.c | 8 +-- net/netfilter/nft_ct.c | 4 +- net/netfilter/xt_CT.c | 3 +- net/openvswitch/conntrack.c | 15 ++++-- net/sched/act_ct.c | 7 ++- 16 files changed, 84 insertions(+), 83 deletions(-)
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 1/5 netfilter: conntrack: convert to refcount_t api
Convert nf_conn reference counting from atomic_t to refcount_t based api. refcount_t api provides more runtime sanity checks and will warn on certain constructs, e.g. refcount_inc() on a zero reference count, which usually indicates use-after-free.
For this reason template allocation is changed to init the refcount to 1, the subsequenct add operations are removed.
Likewise, init_conntrack() is changed to set the initial refcount to 1 instead refcount_inc().
This is safe because the new entry is not (yet) visible to other cpus.
--- include/linux/netfilter/nf_conntrack_common.h | 8 +++--- net/netfilter/nf_conntrack_core.c | 26 +++++++++---------- net/netfilter/nf_conntrack_expect.c | 4 +-- net/netfilter/nf_conntrack_netlink.c | 6 ++--- net/netfilter/nf_conntrack_standalone.c | 4 +-- net/netfilter/nf_flow_table_core.c | 2 +- net/netfilter/nf_synproxy_core.c | 1 - net/netfilter/nft_ct.c | 4 +-- net/netfilter/xt_CT.c | 3 +-- net/openvswitch/conntrack.c | 1 - net/sched/act_ct.c | 1 - 11 files changed, 27 insertions(+), 33 deletions(-)
include/linux/netfilter/nf_conntrack_common.h +4 -4
@@ -2,7 +2,7 @@#ifndef _NF_CONNTRACK_COMMON_H#define _NF_CONNTRACK_COMMON_H -#include <linux/atomic.h> +#include <linux/refcount.h> #include <uapi/linux/netfilter/nf_conntrack_common.h> struct ip_conntrack_stat {
@@ -25,19 +25,19 @@ struct ip_conntrack_stat {#define NFCT_PTRMASK ~(NFCT_INFOMASK)struct nf_conntrack { -atomic_t use; +refcount_t use; };void nf_conntrack_destroy (struct nf_conntrack *nfct);static inline void nf_conntrack_put (struct nf_conntrack *nfct) { -if (nfct &&atomic_dec_and_test (&nfct->use)) +if (nfct &&refcount_dec_and_test (&nfct->use))nf_conntrack_destroy (nfct); }static inline void nf_conntrack_get (struct nf_conntrack *nfct) {if (nfct) -atomic_inc (&nfct->use); +refcount_inc (&nfct->use); }#endif /* _NF_CONNTRACK_COMMON_H */
net/netfilter/nf_conntrack_core.c +13 -13
@@ -585,7 +585,7 @@ struct nf_conn *nf_ct_tmpl_alloc(struct net *net, tmpl->status =IPS_TEMPLATE ;write_pnet (&tmpl->ct_net, net);nf_ct_zone_add (tmpl, zone); -atomic_set (&tmpl->ct_general.use,0 ); +refcount_set (&tmpl->ct_general.use,1 );return tmpl; }
@@ -618,7 +618,7 @@ destroy_conntrack(struct nf_conntrack *nfct)struct nf_conn *ct = (struct nf_conn *)nfct;pr_debug ("destroy_conntrack(%p)\n" , ct); -WARN_ON (atomic_read (&nfct->use) !=0 ); +WARN_ON (refcount_read (&nfct->use) !=0 );if (unlikely (nf_ct_is_template (ct))) {nf_ct_tmpl_free (ct);
@@ -742,7 +742,7 @@ nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2)/* caller must hold rcu readlock and none of the nf_conntrack_locks */ static void nf_ct_gc_expired (struct nf_conn *ct) { -if (!atomic_inc_not_zero (&ct->ct_general.use)) +if (!refcount_inc_not_zero (&ct->ct_general.use))return ;if (nf_ct_should_gc (ct))
@@ -810,7 +810,7 @@ __nf_conntrack_find_get(struct net *net, const struct nf_conntrack_zone *zone,* in, try to obtain a reference and re-check tuple */ ct =nf_ct_tuplehash_to_ctrack (h); -if (likely (atomic_inc_not_zero (&ct->ct_general.use))) { +if (likely (refcount_inc_not_zero (&ct->ct_general.use))) {if (likely (nf_ct_key_equal (h, tuple, zone, net))) goto found;
@@ -907,7 +907,7 @@ nf_conntrack_hash_check_insert(struct nf_conn *ct)smp_wmb ();/* The caller holds a reference to this object */ -atomic_set (&ct->ct_general.use,2 ); +refcount_set (&ct->ct_general.use,2 );__nf_conntrack_hash_insert (ct, hash, reply_hash);nf_conntrack_double_unlock (hash, reply_hash);NF_CT_STAT_INC (net, insert);
@@ -958,7 +958,7 @@ static void __nf_conntrack_insert_prepare(struct nf_conn *ct) {struct nf_conn_tstamp *tstamp; -atomic_inc (&ct->ct_general.use); +refcount_inc (&ct->ct_general.use); ct->status |=IPS_CONFIRMED ;/* set conntrack timestamp, if enabled. */
@@ -1351,7 +1351,7 @@ static unsigned int early_drop_list(struct net *net, nf_ct_is_dying(tmp))continue ; -if (!atomic_inc_not_zero (&tmp->ct_general.use)) +if (!refcount_inc_not_zero (&tmp->ct_general.use))continue ;/* kill only if still in same netns -- might have moved due to
@@ -1469,7 +1469,7 @@ static void gc_worker(struct work_struct *work)continue ;/* need to take reference to avoid possible races */ -if (!atomic_inc_not_zero (&tmp->ct_general.use)) +if (!refcount_inc_not_zero (&tmp->ct_general.use))continue ;if (gc_worker_skip_ct (tmp)) {
@@ -1569,7 +1569,7 @@ __nf_conntrack_alloc(struct net *net,/* Because we use RCU lookups, we set ct_general.use to zero before * this is inserted in any list. */ -atomic_set (&ct->ct_general.use,0 ); +refcount_set (&ct->ct_general.use,0 );return ct; out:atomic_dec (&cnet->count);
@@ -1594,7 +1594,7 @@ void nf_conntrack_free(struct nf_conn *ct)/* A freed object has refcnt == 0, that's * the golden rule for SLAB_TYPESAFE_BY_RCU */ -WARN_ON (atomic_read (&ct->ct_general.use) !=0 ); +WARN_ON (refcount_read (&ct->ct_general.use) !=0 );nf_ct_ext_destroy (ct);kmem_cache_free (nf_conntrack_cachep, ct);
@@ -1686,8 +1686,8 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,if (!exp)__nf_ct_try_assign_helper (ct, tmpl,GFP_ATOMIC ); -/* Now it is inserted into the unconfirmed list, bump refcount */ -nf_conntrack_get (&ct->ct_general); +/* Now it is inserted into the unconfirmed list, set refcount to 1. */ +refcount_set (&ct->ct_general.use,1 );nf_ct_add_to_unconfirmed_list (ct);local_bh_enable ();
@@ -2300,7 +2300,7 @@ get_next_corpse(int (*iter)(struct nf_conn *i, void *data),return NULL ; found: -atomic_inc (&ct->ct_general.use); +refcount_inc (&ct->ct_general.use);spin_unlock (lockp);local_bh_enable ();return ct;
net/netfilter/nf_conntrack_expect.c +2 -2
@@ -203,12 +203,12 @@ nf_ct_find_expectation(struct net *net,* about to invoke ->destroy(), or nf_ct_delete() via timeout * or early_drop(). * -* The atomic_inc_not_zero() check tells: If that fails, we +* The refcount_inc_not_zero() check tells: If that fails, we * know that the ct is being destroyed. If it succeeds, we * can be sure the ct cannot disappear underneath. */ if (unlikely (nf_ct_is_dying (exp->master) || - !atomic_inc_not_zero (&exp->master->ct_general.use))) + !refcount_inc_not_zero (&exp->master->ct_general.use)))return NULL ;if (exp->flags &NF_CT_EXPECT_PERMANENT ) {
net/netfilter/nf_conntrack_netlink.c +3 -3
@@ -508,7 +508,7 @@ static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)static int ctnetlink_dump_use (struct sk_buff *skb,const struct nf_conn *ct) { -if (nla_put_be32 (skb,CTA_USE ,htonl (atomic_read (&ct->ct_general.use)))) +if (nla_put_be32 (skb,CTA_USE ,htonl (refcount_read (&ct->ct_general.use)))) goto nla_put_failure;return 0 ;
@@ -1200,7 +1200,7 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) ct =nf_ct_tuplehash_to_ctrack (h);if (nf_ct_is_expired (ct)) {if (i <ARRAY_SIZE (nf_ct_evict) && -atomic_inc_not_zero (&ct->ct_general.use)) +refcount_inc_not_zero (&ct->ct_general.use)) nf_ct_evict[i++] = ct;continue ; }
@@ -1748,7 +1748,7 @@ ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dyingNFNL_MSG_TYPE (cb->nlh->nlmsg_type), ct, dying, 0);if (res <0 ) { -if (!atomic_inc_not_zero (&ct->ct_general.use)) +if (!refcount_inc_not_zero (&ct->ct_general.use))continue ; cb->args[0 ] = cpu; cb->args[1 ] = (unsigned long )ct;
net/netfilter/nf_conntrack_standalone.c +2 -2
@@ -303,7 +303,7 @@ static int ct_seq_show(struct seq_file *s, void *v)int ret =0 ;WARN_ON (!ct); -if (unlikely (!atomic_inc_not_zero (&ct->ct_general.use))) +if (unlikely (!refcount_inc_not_zero (&ct->ct_general.use)))return 0 ;if (nf_ct_should_gc (ct)) {
@@ -370,7 +370,7 @@ static int ct_seq_show(struct seq_file *s, void *v)ct_show_zone (s, ct,NF_CT_DEFAULT_ZONE_DIR );ct_show_delta_time (s, ct); -seq_printf (s,"use=%u\n" ,atomic_read (&ct->ct_general.use)); +seq_printf (s,"use=%u\n" ,refcount_read (&ct->ct_general.use));if (seq_has_overflowed (s)) goto release;
net/netfilter/nf_flow_table_core.c +1 -1
@@ -48,7 +48,7 @@ struct flow_offload *flow_offload_alloc(struct nf_conn *ct)struct flow_offload *flow;if (unlikely (nf_ct_is_dying (ct) || - !atomic_inc_not_zero (&ct->ct_general.use))) + !refcount_inc_not_zero (&ct->ct_general.use)))return NULL ; flow =kzalloc (sizeof (*flow),GFP_ATOMIC );
net/netfilter/nf_synproxy_core.c +0 -1
@@ -349,7 +349,6 @@ static int __net_init synproxy_net_init(struct net *net) goto err2;__set_bit (IPS_CONFIRMED_BIT , &ct->status); -nf_conntrack_get (&ct->ct_general); snet->tmpl = ct; snet->stats = alloc_percpu(struct synproxy_stats);
net/netfilter/nft_ct.c +1 -3
@@ -259,7 +259,7 @@ static void nft_ct_set_zone_eval(const struct nft_expr *expr, ct =this_cpu_read (nft_ct_pcpu_template); -if (likely (atomic_read (&ct->ct_general.use) ==1 )) { +if (likely (refcount_read (&ct->ct_general.use) ==1 )) {nf_ct_zone_add (ct, &zone); }else {/* previous skb got queued to userspace */
@@ -270,7 +270,6 @@ static void nft_ct_set_zone_eval(const struct nft_expr *expr, } } -atomic_inc (&ct->ct_general.use);nf_ct_set (skb, ct,IP_CT_NEW ); }#endif
@@ -375,7 +374,6 @@ static bool nft_ct_tmpl_alloc_pcpu(void)return false; } -atomic_set (&tmp->ct_general.use,1 );per_cpu (nft_ct_pcpu_template, cpu) = tmp; }
net/netfilter/xt_CT.c +1 -2
@@ -24,7 +24,7 @@ static inline int xt_ct_target(struct sk_buff *skb, struct nf_conn *ct)return XT_CONTINUE ;if (ct) { -atomic_inc (&ct->ct_general.use); +refcount_inc (&ct->ct_general.use);nf_ct_set (skb, ct,IP_CT_NEW ); }else {nf_ct_set (skb, ct,IP_CT_UNTRACKED );
@@ -201,7 +201,6 @@ static int xt_ct_tg_check(const struct xt_tgchk_param *par, goto err4; }__set_bit (IPS_CONFIRMED_BIT , &ct->status); -nf_conntrack_get (&ct->ct_general); out: info->ct = ct;return 0 ;
net/openvswitch/conntrack.c +0 -1
@@ -1716,7 +1716,6 @@ int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, goto err_free_ct;__set_bit (IPS_CONFIRMED_BIT , &ct_info.ct->status); -nf_conntrack_get (&ct_info.ct->ct_general);return 0 ; err_free_ct:__ovs_ct_free_action (&ct_info);
net/sched/act_ct.c +0 -1
@@ -1228,7 +1228,6 @@ static int tcf_ct_fill_params(struct net *net,return -ENOMEM ; }__set_bit (IPS_CONFIRMED_BIT , &tmpl->status); -nf_conntrack_get (&tmpl->ct_general); p->tmpl = tmpl;return 0 ;
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 2/5 netfilter: core: move ip_ct_attach indirection to struct nf_ct_hook
In reply to Florian Westphalip_ct_attach predates struct nf_ct_hook, we can place it there and remove the exported symbol.
--- include/linux/netfilter.h | 2 +- net/netfilter/core.c | 19 ++++++++----------- net/netfilter/nf_conntrack_core.c | 4 +--- 3 files changed, 10 insertions(+), 15 deletions(-)
include/linux/netfilter.h +1 -1
@@ -440,7 +440,6 @@ nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)#if IS_ENABLED (CONFIG_NF_CONNTRACK )#include <linux/netfilter/nf_conntrack_zones_common.h> -extern void (*ip_ct_attach)(struct sk_buff *,const struct sk_buff *)__rcu ;void nf_ct_attach (struct sk_buff *,const struct sk_buff *);struct nf_conntrack_tuple ; bool nf_ct_get_tuple_skb(struct nf_conntrack_tuple *dst_tuple,
@@ -463,6 +462,7 @@ struct nf_ct_hook {void (*destroy)(struct nf_conntrack *);bool (*get_tuple_skb)(struct nf_conntrack_tuple *,const struct sk_buff *); +void (*attach)(struct sk_buff *nskb,const struct sk_buff *skb); };extern struct nf_ct_hook __rcu *nf_ct_hook;
net/netfilter/core.c +8 -11
@@ -673,25 +673,22 @@ struct nf_ct_hook __rcu *nf_ct_hook __read_mostly;EXPORT_SYMBOL_GPL (nf_ct_hook);#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -/* This does not belong here, but locally generated errors need it if connection -tracking in use: without this, connection may not be in hash table, and hence -manufactured ICMP or RST packets will not be associated with it. */ -void (*ip_ct_attach)(struct sk_buff *,const struct sk_buff *) -__rcu __read_mostly ; -EXPORT_SYMBOL (ip_ct_attach); -struct nf_nat_hook __rcu *nf_nat_hook__read_mostly ;EXPORT_SYMBOL_GPL (nf_nat_hook); +/* This does not belong here, but locally generated errors need it if connection +* tracking in use: without this, connection may not be in hash table, and hence +* manufactured ICMP or RST packets will not be associated with it. +*/ void nf_ct_attach (struct sk_buff *new,const struct sk_buff *skb) { -void (*attach)(struct sk_buff *,const struct sk_buff *); +const struct nf_ct_hook *ct_hook;if (skb->_nfct) {rcu_read_lock (); - attach =rcu_dereference (ip_ct_attach); -if (attach) -attach (new, skb); + ct_hook =rcu_dereference (nf_ct_hook); +if (ct_hook) + ct_hook->attach (new, skb);rcu_read_unlock (); } }
net/netfilter/nf_conntrack_core.c +1 -3
@@ -2455,7 +2455,6 @@ static int kill_all(struct nf_conn *i, void *data)void nf_conntrack_cleanup_start (void ) { conntrack_gc_work.exiting = true; -RCU_INIT_POINTER (ip_ct_attach,NULL ); }void nf_conntrack_cleanup_end (void )
@@ -2774,12 +2773,11 @@ static struct nf_ct_hook nf_conntrack_hook = { .update = nf_conntrack_update, .destroy = destroy_conntrack, .get_tuple_skb = nf_conntrack_get_tuple_skb, + .attach = nf_conntrack_attach, };void nf_conntrack_init_end (void ) { -/* For use by REJECT target */ -RCU_INIT_POINTER (ip_ct_attach, nf_conntrack_attach);RCU_INIT_POINTER (nf_ct_hook, &nf_conntrack_hook); }
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 3/5 netfilter: make function op structures const
In reply to Florian WestphalNo functional changes, these structures should be const.
--- include/linux/netfilter.h | 8 ++++---- net/netfilter/core.c | 10 +++++----- net/netfilter/nf_conntrack_core.c | 4 ++-- net/netfilter/nf_conntrack_netlink.c | 4 ++-- net/netfilter/nf_nat_core.c | 2 +- net/netfilter/nfnetlink_queue.c | 8 ++++---- 6 files changed, 18 insertions(+), 18 deletions(-)
include/linux/netfilter.h +4 -4
@@ -381,13 +381,13 @@ struct nf_nat_hook { enum ip_conntrack_dir dir); }; -extern struct nf_nat_hook __rcu *nf_nat_hook; +extern const struct nf_nat_hook __rcu *nf_nat_hook;static inline void nf_nat_decode_session (struct sk_buff *skb,struct flowi *fl,u_int8_t family) {#if IS_ENABLED (CONFIG_NF_NAT ) -struct nf_nat_hook *nat_hook; +const struct nf_nat_hook *nat_hook;rcu_read_lock (); nat_hook =rcu_dereference (nf_nat_hook);
@@ -464,7 +464,7 @@ struct nf_ct_hook { const struct sk_buff *); void (*attach)(struct sk_buff *nskb, const struct sk_buff *skb); }; -extern struct nf_ct_hook __rcu *nf_ct_hook; +extern const struct nf_ct_hook __rcu *nf_ct_hook;struct nlattr ;
@@ -479,7 +479,7 @@ struct nfnl_ct_hook {void (*seq_adjust)(struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo,s32 off); }; -extern struct nfnl_ct_hook __rcu *nfnl_ct_hook; +extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;/** * nf_skb_duplicated - TEE target has sent a packet
net/netfilter/core.c +5 -5
@@ -666,14 +666,14 @@ EXPORT_SYMBOL(nf_hook_slow_list);/* This needs to be compiled in any case to avoid dependencies between the * nfnetlink_queue code and nf_conntrack. */ -struct nfnl_ct_hook __rcu *nfnl_ct_hook__read_mostly ; +const struct nfnl_ct_hook __rcu *nfnl_ct_hook__read_mostly ;EXPORT_SYMBOL_GPL (nfnl_ct_hook); -struct nf_ct_hook __rcu *nf_ct_hook__read_mostly ; +const struct nf_ct_hook __rcu *nf_ct_hook__read_mostly ;EXPORT_SYMBOL_GPL (nf_ct_hook);#if IS_ENABLED (CONFIG_NF_CONNTRACK ) -struct nf_nat_hook __rcu *nf_nat_hook__read_mostly ; +const struct nf_nat_hook __rcu *nf_nat_hook__read_mostly ;EXPORT_SYMBOL_GPL (nf_nat_hook);/* This does not belong here, but locally generated errors need it if connection
@@ -696,7 +696,7 @@ EXPORT_SYMBOL(nf_ct_attach);void nf_conntrack_destroy (struct nf_conntrack *nfct) { -struct nf_ct_hook *ct_hook; +const struct nf_ct_hook *ct_hook;rcu_read_lock (); ct_hook =rcu_dereference (nf_ct_hook);
@@ -709,7 +709,7 @@ EXPORT_SYMBOL(nf_conntrack_destroy);bool nf_ct_get_tuple_skb (struct nf_conntrack_tuple *dst_tuple,const struct sk_buff *skb) { -struct nf_ct_hook *ct_hook; +const struct nf_ct_hook *ct_hook;bool ret = false;rcu_read_lock ();
net/netfilter/nf_conntrack_core.c +2 -2
@@ -2085,9 +2085,9 @@ static int __nf_conntrack_update(struct net *net, struct sk_buff *skb,struct nf_conn *ct, enum ip_conntrack_info ctinfo) { + const struct nf_nat_hook *nat_hook;struct nf_conntrack_tuple_hash *h;struct nf_conntrack_tuple tuple; -struct nf_nat_hook *nat_hook;unsigned int status;int dataoff;u16 l3num;
@@ -2769,7 +2769,7 @@ int nf_conntrack_init_start(void)return ret; } -static struct nf_ct_hook nf_conntrack_hook = { +static const struct nf_ct_hook nf_conntrack_hook = { .update = nf_conntrack_update, .destroy = destroy_conntrack, .get_tuple_skb = nf_conntrack_get_tuple_skb,
net/netfilter/nf_conntrack_netlink.c +2 -2
@@ -1819,7 +1819,7 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct, const struct nlattr *attr)__must_hold (RCU ) { -struct nf_nat_hook *nat_hook; +const struct nf_nat_hook *nat_hook;int err; nat_hook =rcu_dereference (nf_nat_hook);
@@ -2921,7 +2921,7 @@ static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,nf_ct_tcp_seqadj_set (skb, ct, ctinfo, diff); } -static struct nfnl_ct_hook ctnetlink_glue_hook = { +static const struct nfnl_ct_hook ctnetlink_glue_hook = { .build_size = ctnetlink_glue_build_size, .build = ctnetlink_glue_build, .parse = ctnetlink_glue_parse,
net/netfilter/nf_nat_core.c +1 -1
@@ -1167,7 +1167,7 @@ static struct pernet_operations nat_net_ops = { .size = sizeof(struct nat_net), }; -static struct nf_nat_hook nat_hook = { +static const struct nf_nat_hook nat_hook = { .parse_nat_setup = nfnetlink_parse_nat_setup, #ifdef CONFIG_XFRM .decode_session = __nf_nat_decode_session,
net/netfilter/nfnetlink_queue.c +4 -4
@@ -225,7 +225,7 @@ find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)static void nfqnl_reinject (struct nf_queue_entry *entry,unsigned int verdict) { -struct nf_ct_hook *ct_hook; +const struct nf_ct_hook *ct_hook;int err; if (verdict == NF_ACCEPT ||
@@ -388,7 +388,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,struct net_device *outdev;struct nf_conn *ct =NULL ;enum ip_conntrack_info ctinfo =0 ; -struct nfnl_ct_hook *nfnl_ct; +const struct nfnl_ct_hook *nfnl_ct;bool csum_verify;char *secdata =NULL ;u32 seclen =0 ;
@@ -1103,7 +1103,7 @@ static int nfqnl_recv_verdict_batch(struct sk_buff *skb,return 0 ; } -static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct, +static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct, const struct nlmsghdr *nlh, const struct nlattr * const nfqa[], struct nf_queue_entry *entry,
@@ -1170,11 +1170,11 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, {struct nfnl_queue_net *q =nfnl_queue_pernet (info->net);u_int16_t queue_num =ntohs (info->nfmsg->res_id); +const struct nfnl_ct_hook *nfnl_ct;struct nfqnl_msg_verdict_hdr *vhdr;enum ip_conntrack_info ctinfo;struct nfqnl_instance *queue;struct nf_queue_entry *entry; -struct nfnl_ct_hook *nfnl_ct;struct nf_conn *ct =NULL ;unsigned int verdict;int err;
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 4/5 netfilter: conntrack: avoid useless indirection during conntrack destruction
In reply to Florian Westphalnf_ct_put() results in a usesless indirection:
nf_ct_put -> nf_conntrack_put -> nf_conntrack_destroy -> rcu readlock + indirect call of ct_hooks->destroy().
There are two _put helpers:nf_ct_put and nf_conntrack_put. The latter is what should be used in code that MUST NOT cause a linker dependency on the conntrack module (e.g. calls from core network stack).
Everyone else should call nf_ct_put() instead.
A followup patch will convert a few nf_conntrack_put() calls to nf_ct_put(), in particular from modules that already have a conntrack dependency such as act_ct or even nf_conntrack itself.
--- include/linux/netfilter/nf_conntrack_common.h | 2 ++ include/net/netfilter/nf_conntrack.h | 8 ++++++-- net/netfilter/nf_conntrack_core.c | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-)
include/linux/netfilter/nf_conntrack_common.h +2 -0
@@ -29,6 +29,8 @@ struct nf_conntrack { };void nf_conntrack_destroy (struct nf_conntrack *nfct); + +/* like nf_ct_put, but without module dependency on nf_conntrack */ static inline void nf_conntrack_put (struct nf_conntrack *nfct) {if (nfct &&refcount_dec_and_test (&nfct->use))
include/net/netfilter/nf_conntrack.h +6 -2
@@ -76,6 +76,8 @@ struct nf_conn {* Hint, SKB address this struct and refcnt via skb->_nfct and * helpers nf_conntrack_get() and nf_conntrack_put(). * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt, +* except that the latter uses internal indirection and does not +* result in a conntrack module dependency. * beware nf_ct_get() is different and don't inc refcnt. */ struct nf_conntrack ct_general;
@@ -170,11 +172,13 @@ nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)return (struct nf_conn *)(nfct &NFCT_PTRMASK ); } +void nf_ct_destroy (struct nf_conntrack *nfct); +/* decrement reference count on a conntrack */ static inline void nf_ct_put (struct nf_conn *ct) { -WARN_ON (!ct); -nf_conntrack_put (&ct->ct_general); +if (ct &&refcount_dec_and_test (&ct->ct_general.use)) +nf_ct_destroy (&ct->ct_general); }/* Protocol module loading */
net/netfilter/nf_conntrack_core.c +6 -6
@@ -558,7 +558,7 @@ static void nf_ct_del_from_dying_or_unconfirmed_list(struct nf_conn *ct)#define NFCT_ALIGN (len) (((len) + NFCT_INFOMASK) & ~NFCT_INFOMASK) -/* Released via destroy_conntrack() */ +/* Released via nf_ct_destroy() */ struct nf_conn *nf_ct_tmpl_alloc (struct net *net,const struct nf_conntrack_zone *zone,gfp_t flags)
@@ -612,12 +612,11 @@ static void destroy_gre_conntrack(struct nf_conn *ct)#endif } -static void -destroy_conntrack (struct nf_conntrack *nfct) +void nf_ct_destroy (struct nf_conntrack *nfct) {struct nf_conn *ct = (struct nf_conn *)nfct; -pr_debug ("destroy_conntrack(%p)\n" , ct); +pr_debug ("%s(%p)\n" , __func__, ct);WARN_ON (refcount_read (&nfct->use) !=0 );if (unlikely (nf_ct_is_template (ct))) {
@@ -643,9 +642,10 @@ destroy_conntrack(struct nf_conntrack *nfct)if (ct->master)nf_ct_put (ct->master); -pr_debug ("destroy_conntrack: returning ct=%p to slab\n" , ct); +pr_debug ("%s: returning ct=%p to slab\n" , __func__, ct);nf_conntrack_free (ct); } +EXPORT_SYMBOL (nf_ct_destroy);static void nf_ct_delete_from_lists (struct nf_conn *ct) {
@@ -2771,7 +2771,7 @@ int nf_conntrack_init_start(void)static const struct nf_ct_hook nf_conntrack_hook = { .update = nf_conntrack_update, - .destroy = destroy_conntrack, + .destroy = nf_ct_destroy, .get_tuple_skb = nf_conntrack_get_tuple_skb, .attach = nf_conntrack_attach, };
Signature · 1 line
2.34.1
Florian Westphal
maintainerPATCH 5/5 net: prefer nf_ct_put instead of nf_conntrack_put
In reply to Florian WestphalIts the same as nf_conntrack_put(), but without the need for an indirect call. The downside is a module dependency on nf_conntrack, but all of these already depend on conntrack anyway.
--- net/netfilter/nf_conntrack_core.c | 4 ++-- net/openvswitch/conntrack.c | 14 ++++++++++---- net/sched/act_ct.c | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-)
net/netfilter/nf_conntrack_core.c +2 -2
@@ -989,7 +989,7 @@ static int __nf_ct_resolve_clash(struct sk_buff *skb,nf_ct_acct_merge (ct, ctinfo, loser_ct);nf_ct_add_to_dying_list (loser_ct); -nf_conntrack_put (&loser_ct->ct_general); +nf_ct_put (loser_ct);nf_ct_set (skb, ct, ctinfo);NF_CT_STAT_INC (net, clash_resolve);
@@ -1921,7 +1921,7 @@ nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state)/* Invalid: inverse of the return code tells * the netfilter core what to do */ pr_debug ("nf_conntrack_in: Can't track with proto module\n" ); -nf_conntrack_put (&ct->ct_general); +nf_ct_put (ct); skb->_nfct =0 ;NF_CT_STAT_INC_ATOMIC (state->net, invalid);if (ret == -NF_DROP )
net/openvswitch/conntrack.c +10 -4
@@ -574,7 +574,7 @@ ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,struct nf_conn *ct =nf_ct_tuplehash_to_ctrack (h);nf_ct_delete (ct,0 ,0 ); -nf_conntrack_put (&ct->ct_general); +nf_ct_put (ct); } }
@@ -723,7 +723,7 @@ static bool skb_nfct_cached(struct net *net,if (nf_ct_is_confirmed (ct))nf_ct_delete (ct,0 ,0 ); -nf_conntrack_put (&ct->ct_general); +nf_ct_put (ct);nf_ct_set (skb,NULL ,0 );return false; }
@@ -967,7 +967,8 @@ static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,/* Associate skb with specified zone. */ if (tmpl) { -nf_conntrack_put (skb_nfct (skb)); + ct =nf_ct_get (skb, &ctinfo); +nf_ct_put (ct);nf_conntrack_get (&tmpl->ct_general);nf_ct_set (skb, tmpl,IP_CT_NEW ); }
@@ -1328,7 +1329,12 @@ int ovs_ct_execute(struct net *net, struct sk_buff *skb,int ovs_ct_clear (struct sk_buff *skb,struct sw_flow_key *key) { -nf_conntrack_put (skb_nfct (skb)); +enum ip_conntrack_info ctinfo; +struct nf_conn *ct; + + ct =nf_ct_get (skb, &ctinfo); + +nf_ct_put (ct);nf_ct_set (skb,NULL ,IP_CT_UNTRACKED );ovs_ct_fill_key (skb, key, false);
net/sched/act_ct.c +3 -3
@@ -598,7 +598,7 @@ static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,if (nf_ct_is_confirmed (ct))nf_ct_kill (ct); -nf_conntrack_put (&ct->ct_general); +nf_ct_put (ct);nf_ct_set (skb,NULL ,IP_CT_UNTRACKED );return false;
@@ -763,7 +763,7 @@ static void tcf_ct_params_free(struct rcu_head *head)tcf_ct_flow_table_put (params);if (params->tmpl) -nf_conntrack_put (¶ms->tmpl->ct_general); +nf_ct_put (params->tmpl);kfree (params); }
@@ -967,7 +967,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,qdisc_skb_cb (skb)->post_ct = false; ct =nf_ct_get (skb, &ctinfo);if (ct) { -nf_conntrack_put (&ct->ct_general); +nf_ct_put (ct);nf_ct_set (skb,NULL ,IP_CT_UNTRACKED ); }
Signature · 1 line
2.34.1
Pablo Neira Ayuso
maintainerQuoted 10 lines · Florian Westphal
This patch series contains cleanups to conntrack and related users such as ovs and
act_ct.First patch converts conntrack reference counting to
refcount_tapi. Second patch gets rid ofip_ct_attachhook, we can use existingnf_ct_hookfor this.Third patch constifies a couple of structures that don't need to be writeable.
Quoted 8 lines
Last two patches splits
nf_ct_putandnf_conntrack_put. These functions still do the same thing, but now onlynf_conntrack_putuses thenf_ct_hookindirection,nf_ct_putuses a direct call. Virtually all places should usenf_ct_put-- only core kernel code needs to use the indirection.Before this change,
nf_ct_putwas merely an alias fornf_conntrack_putso even conntrack itself did additional indirection.
Series applied, thanks