netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v5 1/7 netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
Add a refcount for struct nf_ct_timeout which is used by ct extension to set the custom ct timeout policy, this tells us that the ct timeout is being used by a conntrack entry. When the last conntrack entry drops the refcount on the ct timeout, the ct timeout is released.
Remove the refcount for control plane which controls if the ruleset refers to the timeout policy. After this update, it is possible to remove the ct timeout policy from nfnetlink_cttimeout immediately. This is for simplicity not to handle two refcounts on a single object.
Remove nf_queue_nf_hook_drop(): a packet sitting in nfqueue will just hold a reference to the nf_ct_timeout object until packet is reinjected, since this is part of the ct extension, this will be released by the time the conntrack is freed.
nf_ct_untimeout() is still called to clean up in a best effort basis:
the ct timeout on existing entries gets removed when the ct timeout goes away, but as long as the iptables ruleset still refers to the ct timeout through a template, new conntracks may keep attaching it and extend its lifetime until the rule is removed.
nf_ct_untimeout() is not called anymore from module removal path, this is unlikely to find timeouts give module refcount is bumped, and the new refcount already tracks the ct timeout policy use so it is released when unused.
---
v5: address possible null-ptr-deref in xt_CT reported by AI reviewer.
include/net/netfilter/nf_conntrack_timeout.h | 27 ++++- net/netfilter/nf_conntrack_core.c | 6 +- net/netfilter/nf_conntrack_timeout.c | 27 ++++- net/netfilter/nfnetlink_cttimeout.c | 112 +++++++++---------- net/netfilter/nft_ct.c | 7 +- net/netfilter/xt_CT.c | 2 +- 6 files changed, 107 insertions(+), 74 deletions(-)
include/net/netfilter/nf_conntrack_timeout.h +25 -2
@@ -12,6 +12,7 @@#define CTNL_TIMEOUT_NAME_MAX 32struct nf_ct_timeout { +refcount_t refcnt;__u16 l3num;const struct nf_conntrack_l4proto *l4proto;struct rcu_head rcu;
@@ -22,6 +23,22 @@ struct nf_conn_timeout {struct nf_ct_timeout __rcu *timeout; }; +static inline void nf_ct_timeout_put (const struct nf_conn *ct) +{ +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT +struct nf_conn_timeout *timeout_ext; +struct nf_ct_timeout *timeout; + + timeout_ext =nf_ct_ext_find (ct,NF_CT_EXT_TIMEOUT ); +if (!timeout_ext) +return ; + + timeout =rcu_dereference (timeout_ext->timeout); +if (timeout &&refcount_dec_and_test (&timeout->refcnt)) +kfree_rcu (timeout, rcu); +#endif +} +static inline unsigned int *nf_ct_timeout_data (const struct nf_conn_timeout *t) {
@@ -56,8 +73,14 @@ struct nf_conn_timeout *nf_ct_timeout_ext_add(struct nf_conn *ct,#ifdef CONFIG_NF_CONNTRACK_TIMEOUT struct nf_conn_timeout *timeout_ext; +if (!timeout) +return NULL ; + timeout_ext =nf_ct_ext_add (ct,NF_CT_EXT_TIMEOUT , gfp); -if (timeout_ext ==NULL ) +if (!timeout_ext || timeout_ext->timeout) +return NULL ; + +if (!refcount_inc_not_zero (&timeout->refcnt))return NULL ;rcu_assign_pointer (timeout_ext->timeout, timeout);
@@ -75,7 +98,7 @@ static inline unsigned int *nf_ct_timeout_lookup(const struct nf_conn *ct)struct nf_conn_timeout *timeout_ext; timeout_ext =nf_ct_timeout_find (ct); -if (timeout_ext) +if (timeout_ext &&rcu_access_pointer (timeout_ext->timeout)) timeouts =nf_ct_timeout_data (timeout_ext);#endif return timeouts;
net/netfilter/nf_conntrack_core.c +4 -2
@@ -1737,16 +1737,18 @@ void nf_conntrack_free(struct nf_conn *ct)*/ WARN_ON (refcount_read (&ct->ct_general.use) !=0 ); +rcu_read_lock ();if (ct->status &IPS_SRC_NAT_DONE ) {const struct nf_nat_hook *nat_hook; -rcu_read_lock (); nat_hook =rcu_dereference (nf_nat_hook);if (nat_hook) nat_hook->remove_nat_bysrc (ct); -rcu_read_unlock (); } +nf_ct_timeout_put (ct); +rcu_read_unlock (); +kfree (ct->ext);kmem_cache_free (nf_conntrack_cachep, ct); cnet =nf_ct_pernet (net);
net/netfilter/nf_conntrack_timeout.c +24 -3
@@ -25,17 +25,32 @@const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook__read_mostly ;EXPORT_SYMBOL_GPL (nf_ct_timeout_hook); +/* nf_ct_iterate_cleanup() holds the conntrack lock. */ static int untimeout (struct nf_conn *ct,void *timeout) {struct nf_conn_timeout *timeout_ext =nf_ct_timeout_find (ct);if (timeout_ext) { -const struct nf_ct_timeout *t; +struct nf_ct_timeout *t; - t =rcu_access_pointer (timeout_ext->timeout); +rcu_read_lock (); + t =rcu_dereference (timeout_ext->timeout); +if (!t) { +rcu_read_unlock (); +return 0 ; + } -if (!timeout || t == timeout) +if (!timeout || t == timeout) {RCU_INIT_POINTER (timeout_ext->timeout,NULL ); + +/* No race with nf_conntrack_free() which is called +* only after the conntrack has been removed from +* the hashes. +*/ +if (refcount_dec_and_test (&t->refcnt)) +kfree_rcu (t, rcu); + } +rcu_read_unlock (); }/* We are not intended to delete this conntrack. */
@@ -70,6 +85,8 @@ int nf_ct_set_timeout(struct net *net, struct nf_conn *ct,const char *errmsg =NULL ;int ret =0 ; +WARN_ON_ONCE (!nf_ct_is_template (ct)); +rcu_read_lock (); h =rcu_dereference (nf_ct_timeout_hook);if (!h) {
@@ -127,6 +144,8 @@ void nf_ct_destroy_timeout(struct nf_conn *ct)struct nf_conn_timeout *timeout_ext;const struct nf_ct_timeout_hooks *h; +WARN_ON_ONCE (!nf_ct_is_template (ct)); +rcu_read_lock (); h =rcu_dereference (nf_ct_timeout_hook);
@@ -139,6 +158,8 @@ void nf_ct_destroy_timeout(struct nf_conn *ct)if (t) h->timeout_put (t);RCU_INIT_POINTER (timeout_ext->timeout,NULL ); +if (t &&refcount_dec_and_test (&t->refcnt)) +kfree_rcu (t, rcu); } }rcu_read_unlock ();
net/netfilter/nfnetlink_cttimeout.c +50 -62
219 lines not on this page · raw message
net/netfilter/nft_ct.c +3 -4
@@ -897,8 +897,6 @@ static void nft_ct_timeout_obj_eval(struct nft_object *obj, } } -rcu_assign_pointer (timeout->timeout, priv->timeout); -/* adjust the timeout as per 'new' state. ct is unconfirmed, * so the current timestamp must not be added. */
@@ -949,6 +947,7 @@ static int nft_ct_timeout_obj_init(const struct nft_ctx *ctx, timeout->l3num = l3num; timeout->l4proto = l4proto; +refcount_set (&timeout->refcnt,1 ); ret =nf_ct_netns_get (ctx->net, ctx->family);if (ret <0 )
@@ -969,10 +968,10 @@ static void nft_ct_timeout_obj_destroy(const struct nft_ctx *ctx,struct nft_ct_timeout_obj *priv =nft_obj_data (obj);struct nf_ct_timeout *timeout = priv->timeout; -nf_queue_nf_hook_drop (ctx->net);nf_ct_untimeout (ctx->net, timeout);nf_ct_netns_put (ctx->net, ctx->family); -kfree_rcu (priv->timeout, rcu); +if (refcount_dec_and_test (&timeout->refcnt)) +kfree_rcu (priv->timeout, rcu); } static int nft_ct_timeout_obj_dump(struct sk_buff *skb,
net/netfilter/xt_CT.c +1 -1
@@ -284,7 +284,7 @@ static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,struct nf_conn_help *help;if (ct) { -if (info->helper[0 ] || info->timeout[0 ]) +if (info->helper[0 ])nf_queue_nf_hook_drop (par->net); help =nfct_help (ct);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 2/7 netfilter: nf_conntrack_helper: dynamically allocate struct nf_conntrack_helper
Adapt all existing helpers to use a modified version of nf_ct_helper_init(), to dynamically allocate struct nf_conntrack_helper.
Allocate expect_policy[] built-in into the helper to ensure this area is reachable after helper removal since a follow up patch adds refcount to track use of the nf_conntrack_helper structure from packet path so it remains around until last reference from ct helper extension is dropped.
Export __nf_conntrack_helper_register() which allows to register nfnetlink_cthelper dynamically allocated helper. Adapt nfnetlink_cthelper to use the built-in expect_policy[].
This is a preparation patch to add packet path refcounting to helpers.
---
v5: no changes
include/net/netfilter/nf_conntrack_helper.h | 16 +-- net/ipv4/netfilter/nf_nat_snmp_basic_main.c | 27 +++-- net/netfilter/nf_conntrack_amanda.c | 39 +++---- net/netfilter/nf_conntrack_ftp.c | 5 +- net/netfilter/nf_conntrack_h323_main.c | 107 ++++++++------------ net/netfilter/nf_conntrack_helper.c | 74 +++++++++++--- net/netfilter/nf_conntrack_irc.c | 5 +- net/netfilter/nf_conntrack_netbios_ns.c | 20 ++-- net/netfilter/nf_conntrack_pptp.c | 22 ++-- net/netfilter/nf_conntrack_sane.c | 5 +- net/netfilter/nf_conntrack_sip.c | 5 +- net/netfilter/nf_conntrack_snmp.c | 21 ++-- net/netfilter/nf_conntrack_tftp.c | 5 +- net/netfilter/nfnetlink_cthelper.c | 47 ++++----- 14 files changed, 209 insertions(+), 189 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +9 -7
@@ -29,13 +29,16 @@ enum nf_ct_helper_flags {#define NF_CT_HELPER_NAME_LEN 16 +/* Must be kept in sync with the classes defined by helpers */ +#define NF_CT_MAX_EXPECT_CLASSES 4 +struct nf_conntrack_helper {struct hlist_node hnode;/* Internal use. */ char name[NF_CT_HELPER_NAME_LEN ];/* name of the module */ refcount_t refcnt;struct module *me;/* pointer to self */ -const struct nf_conntrack_expect_policy *expect_policy; +struct nf_conntrack_expect_policy expect_policy[NF_CT_MAX_EXPECT_CLASSES ];/* Tuple of things we will help (compared against server response) */ struct nf_conntrack_tuple tuple;
@@ -63,9 +66,6 @@ struct nf_conntrack_helper {char nat_mod_name[NF_CT_HELPER_NAME_LEN ]; }; -/* Must be kept in sync with the classes defined by helpers */ -#define NF_CT_MAX_EXPECT_CLASSES 4 -/* nf_conn feature for connections that have a helper */ struct nf_conn_help {/* Helper. if any */
@@ -103,11 +103,13 @@ void nf_ct_helper_init(struct nf_conntrack_helper *helper, struct nf_conn *ct), struct module *module); -int nf_conntrack_helper_register (struct nf_conntrack_helper *); +int nf_conntrack_helper_register (struct nf_conntrack_helper *,struct nf_conntrack_helper **); +int __nf_conntrack_helper_register (struct nf_conntrack_helper *);void nf_conntrack_helper_unregister (struct nf_conntrack_helper *); -int nf_conntrack_helpers_register (struct nf_conntrack_helper *,unsigned int ); -void nf_conntrack_helpers_unregister (struct nf_conntrack_helper *, +int nf_conntrack_helpers_register (struct nf_conntrack_helper *,unsigned int , +struct nf_conntrack_helper **); +void nf_conntrack_helpers_unregister (struct nf_conntrack_helper **,unsigned int );struct nf_conn_help *nf_ct_helper_ext_add (struct nf_conn *ct,gfp_t gfp);
net/ipv4/netfilter/nf_nat_snmp_basic_main.c +16 -11
@@ -202,29 +202,34 @@ static const struct nf_conntrack_expect_policy snmp_exp_policy = { .timeout = 180, }; -static struct nf_conntrack_helper snmp_trap_helper__read_mostly = { - .me =THIS_MODULE , - .help = help, - .expect_policy = &snmp_exp_policy, - .name ="snmp_trap" , - .tuple.src.l3num =AF_INET , - .tuple.src.u.udp.port =cpu_to_be16 (SNMP_TRAP_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , -}; +static struct nf_conntrack_helper snmp_trap_helper__read_mostly ; +static struct nf_conntrack_helper *snmp_trap_helper_ptr__read_mostly ;static int __init nf_nat_snmp_basic_init (void ) { +int err; +BUG_ON (nf_nat_snmp_hook !=NULL );RCU_INIT_POINTER (nf_nat_snmp_hook, help); -return nf_conntrack_helper_register (&snmp_trap_helper); +nf_ct_helper_init (&snmp_trap_helper,AF_INET ,IPPROTO_UDP , +"snmp_trap" ,SNMP_TRAP_PORT ,SNMP_TRAP_PORT ,SNMP_TRAP_PORT , + &snmp_exp_policy,0 , help,NULL ,THIS_MODULE ); + + err =nf_conntrack_helper_register (&snmp_trap_helper, &snmp_trap_helper_ptr); +if (err <0 ) { +RCU_INIT_POINTER (nf_nat_snmp_hook,NULL ); +return err; + } + +return 0 ; } static void __exit nf_nat_snmp_basic_fini(void) {RCU_INIT_POINTER (nf_nat_snmp_hook,NULL );synchronize_rcu (); -nf_conntrack_helper_unregister (&snmp_trap_helper); +nf_conntrack_helper_unregister (snmp_trap_helper_ptr); }module_init (nf_nat_snmp_basic_init);
net/netfilter/nf_conntrack_amanda.c +14 -25
@@ -169,35 +169,15 @@ static const struct nf_conntrack_expect_policy amanda_exp_policy = { .timeout = 180, }; -static struct nf_conntrack_helper amanda_helper[2 ]__read_mostly = { - { - .name =HELPER_NAME , - .me =THIS_MODULE , - .help = amanda_help, - .tuple.src.l3num =AF_INET , - .tuple.src.u.udp.port =cpu_to_be16 (10080 ), - .tuple.dst.protonum =IPPROTO_UDP , - .expect_policy = &amanda_exp_policy, - .nat_mod_name =NF_NAT_HELPER_NAME (HELPER_NAME ), - }, - { - .name ="amanda" , - .me =THIS_MODULE , - .help = amanda_help, - .tuple.src.l3num =AF_INET6 , - .tuple.src.u.udp.port =cpu_to_be16 (10080 ), - .tuple.dst.protonum =IPPROTO_UDP , - .expect_policy = &amanda_exp_policy, - .nat_mod_name =NF_NAT_HELPER_NAME (HELPER_NAME ), - }, -}; +static struct nf_conntrack_helper amanda_helper[2 ]__read_mostly ; +static struct nf_conntrack_helper *amanda_helper_ptr[2 ]__read_mostly ; static void __exit nf_conntrack_amanda_fini(void) {int i; -nf_conntrack_helpers_unregister (amanda_helper, -ARRAY_SIZE (amanda_helper)); +nf_conntrack_helpers_unregister (amanda_helper_ptr, +ARRAY_SIZE (amanda_helper_ptr));for (i =0 ; i <ARRAY_SIZE (search); i++)textsearch_destroy (search[i].ts); }
@@ -217,8 +197,17 @@ static int __init nf_conntrack_amanda_init(void) goto err1; } } + +nf_ct_helper_init (&amanda_helper[0 ],AF_INET ,IPPROTO_UDP , +HELPER_NAME ,10080 ,10080 ,10080 , + &amanda_exp_policy,0 , amanda_help,NULL ,THIS_MODULE ); +nf_ct_helper_init (&amanda_helper[1 ],AF_INET6 ,IPPROTO_UDP , +HELPER_NAME ,10080 ,10080 ,10080 , + &amanda_exp_policy,0 , amanda_help,NULL ,THIS_MODULE ); + ret =nf_conntrack_helpers_register (amanda_helper, -ARRAY_SIZE (amanda_helper)); +ARRAY_SIZE (amanda_helper), + amanda_helper_ptr);if (ret <0 ) goto err1;return 0 ;
net/netfilter/nf_conntrack_ftp.c +3 -2
@@ -552,6 +552,7 @@ static int nf_ct_ftp_from_nlattr(struct nlattr *attr, struct nf_conn *ct) }static struct nf_conntrack_helper ftp[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *ftp_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy ftp_exp_policy = { .max_expected =1 ,
@@ -560,7 +561,7 @@ static const struct nf_conntrack_expect_policy ftp_exp_policy = { static void __exit nf_conntrack_ftp_fini(void) { -nf_conntrack_helpers_unregister (ftp, ports_c *2 ); +nf_conntrack_helpers_unregister (ftp_ptr, ports_c *2 ); }static int __init nf_conntrack_ftp_init (void )
@@ -585,7 +586,7 @@ static int __init nf_conntrack_ftp_init(void) nf_ct_ftp_from_nlattr, THIS_MODULE); } - ret =nf_conntrack_helpers_register (ftp, ports_c *2 ); + ret =nf_conntrack_helpers_register (ftp, ports_c *2 , ftp_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_h323_main.c +42 -65
196 lines not on this page · raw message
net/netfilter/nf_conntrack_helper.c +62 -12
133 lines not on this page · raw message
net/netfilter/nf_conntrack_irc.c +3 -2
@@ -255,6 +255,7 @@ static int help(struct sk_buff *skb, unsigned int protoff, }static struct nf_conntrack_helper irc[MAX_PORTS ]__read_mostly ; +static struct nf_conntrack_helper *irc_ptr[MAX_PORTS ]__read_mostly ;static struct nf_conntrack_expect_policy irc_exp_policy;static int __init nf_conntrack_irc_init (void )
@@ -289,7 +290,7 @@ static int __init nf_conntrack_irc_init(void) 0, help, NULL, THIS_MODULE); } - ret =nf_conntrack_helpers_register (&irc[0 ], ports_c); + ret =nf_conntrack_helpers_register (&irc[0 ], ports_c, irc_ptr);if (ret) {pr_err ("failed to register helpers\n" );kfree (irc_buffer);
@@ -301,7 +302,7 @@ static int __init nf_conntrack_irc_init(void) static void __exit nf_conntrack_irc_fini(void) { -nf_conntrack_helpers_unregister (irc, ports_c); +nf_conntrack_helpers_unregister (irc_ptr, ports_c);kfree (irc_buffer); }
net/netfilter/nf_conntrack_netbios_ns.c +9 -11
@@ -44,27 +44,25 @@ static int netbios_ns_help(struct sk_buff *skb, unsigned int protoff,return nf_conntrack_broadcast_help (skb, ct, ctinfo, timeout); } -static struct nf_conntrack_helper helper__read_mostly = { - .name =HELPER_NAME , - .tuple.src.l3num =NFPROTO_IPV4 , - .tuple.src.u.udp.port =cpu_to_be16 (NMBD_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , - .me =THIS_MODULE , - .help = netbios_ns_help, - .expect_policy = &exp_policy, -}; +static struct nf_conntrack_helper helper__read_mostly ; +static struct nf_conntrack_helper *helper_ptr__read_mostly ;static int __init nf_conntrack_netbios_ns_init (void ) {NF_CT_HELPER_BUILD_BUG_ON (0 ); exp_policy.timeout = timeout; -return nf_conntrack_helper_register (&helper); + +nf_ct_helper_init (&helper,AF_INET ,IPPROTO_UDP ,HELPER_NAME , +NMBD_PORT ,NMBD_PORT ,NMBD_PORT , + &exp_policy,0 , netbios_ns_help,NULL ,THIS_MODULE ); + +return nf_conntrack_helper_register (&helper, &helper_ptr); } static void __exit nf_conntrack_netbios_ns_fini(void) { -nf_conntrack_helper_unregister (&helper); +nf_conntrack_helper_unregister (helper_ptr); }module_init (nf_conntrack_netbios_ns_init);
net/netfilter/nf_conntrack_pptp.c +10 -12
@@ -582,27 +582,25 @@ static const struct nf_conntrack_expect_policy pptp_exp_policy = { };/* control protocol helper */ -static struct nf_conntrack_helper pptp__read_mostly = { - .name ="pptp" , - .me =THIS_MODULE , - .tuple.src.l3num =AF_INET , - .tuple.src.u.tcp.port =cpu_to_be16 (PPTP_CONTROL_PORT ), - .tuple.dst.protonum =IPPROTO_TCP , - .help = conntrack_pptp_help, - .destroy = pptp_destroy_siblings, - .expect_policy = &pptp_exp_policy, -}; +static struct nf_conntrack_helper pptp__read_mostly ; +static struct nf_conntrack_helper *pptp_ptr__read_mostly ;static int __init nf_conntrack_pptp_init (void ) {NF_CT_HELPER_BUILD_BUG_ON (sizeof (struct nf_ct_pptp_master )); -return nf_conntrack_helper_register (&pptp); +nf_ct_helper_init (&pptp,AF_INET ,IPPROTO_TCP , +"pptp" ,PPTP_CONTROL_PORT ,PPTP_CONTROL_PORT ,PPTP_CONTROL_PORT , + &pptp_exp_policy,0 , conntrack_pptp_help,NULL ,THIS_MODULE ); + + pptp.destroy = pptp_destroy_siblings; + +return nf_conntrack_helper_register (&pptp, &pptp_ptr); } static void __exit nf_conntrack_pptp_fini(void) { -nf_conntrack_helper_unregister (&pptp); +nf_conntrack_helper_unregister (pptp_ptr); }module_init (nf_conntrack_pptp_init);
net/netfilter/nf_conntrack_sane.c +3 -2
@@ -167,6 +167,7 @@ static int help(struct sk_buff *skb, }static struct nf_conntrack_helper sane[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *sane_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy sane_exp_policy = { .max_expected =1 ,
@@ -175,7 +176,7 @@ static const struct nf_conntrack_expect_policy sane_exp_policy = { static void __exit nf_conntrack_sane_fini(void) { -nf_conntrack_helpers_unregister (sane, ports_c *2 ); +nf_conntrack_helpers_unregister (sane_ptr, ports_c *2 ); }static int __init nf_conntrack_sane_init (void )
@@ -200,7 +201,7 @@ static int __init nf_conntrack_sane_init(void) THIS_MODULE); } - ret =nf_conntrack_helpers_register (sane, ports_c *2 ); + ret =nf_conntrack_helpers_register (sane, ports_c *2 , sane_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_sip.c +3 -2
@@ -1731,6 +1731,7 @@ static int sip_help_udp(struct sk_buff *skb, unsigned int protoff, }static struct nf_conntrack_helper sip[MAX_PORTS *4 ]__read_mostly ; +static struct nf_conntrack_helper *sip_ptr[MAX_PORTS *4 ]__read_mostly ;static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX +1 ] = { [SIP_EXPECT_SIGNALLING ] = {
@@ -1757,7 +1758,7 @@ static const struct nf_conntrack_expect_policy sip_exp_policy[SIP_EXPECT_MAX + 1 static void __exit nf_conntrack_sip_fini(void) { -nf_conntrack_helpers_unregister (sip, ports_c *4 ); +nf_conntrack_helpers_unregister (sip_ptr, ports_c *4 ); }static int __init nf_conntrack_sip_init (void )
@@ -1788,7 +1789,7 @@ static int __init nf_conntrack_sip_init(void) NULL, THIS_MODULE); } - ret =nf_conntrack_helpers_register (sip, ports_c *4 ); + ret =nf_conntrack_helpers_register (sip, ports_c *4 , sip_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nf_conntrack_snmp.c +10 -11
@@ -47,25 +47,24 @@ static struct nf_conntrack_expect_policy exp_policy = { .max_expected = 1, }; -static struct nf_conntrack_helper helper__read_mostly = { - .name ="snmp" , - .tuple.src.l3num =NFPROTO_IPV4 , - .tuple.src.u.udp.port =cpu_to_be16 (SNMP_PORT ), - .tuple.dst.protonum =IPPROTO_UDP , - .me =THIS_MODULE , - .help = snmp_conntrack_help, - .expect_policy = &exp_policy, -}; +static struct nf_conntrack_helper helper__read_mostly ; +static struct nf_conntrack_helper *helper_ptr__read_mostly ;static int __init nf_conntrack_snmp_init (void ) { exp_policy.timeout = timeout; -return nf_conntrack_helper_register (&helper); + +nf_ct_helper_init (&helper,AF_INET ,IPPROTO_UDP , +"snmp" ,SNMP_PORT ,SNMP_PORT ,SNMP_PORT , + &exp_policy,0 , snmp_conntrack_help,NULL , +THIS_MODULE ); + +return nf_conntrack_helper_register (&helper, &helper_ptr); } static void __exit nf_conntrack_snmp_fini(void) { -nf_conntrack_helper_unregister (&helper); +nf_conntrack_helper_unregister (helper_ptr); }module_init (nf_conntrack_snmp_init);
net/netfilter/nf_conntrack_tftp.c +3 -2
@@ -96,6 +96,7 @@ static int tftp_help(struct sk_buff *skb, }static struct nf_conntrack_helper tftp[MAX_PORTS *2 ]__read_mostly ; +static struct nf_conntrack_helper *tftp_ptr[MAX_PORTS *2 ]__read_mostly ;static const struct nf_conntrack_expect_policy tftp_exp_policy = { .max_expected =1 ,
@@ -104,7 +105,7 @@ static const struct nf_conntrack_expect_policy tftp_exp_policy = { static void __exit nf_conntrack_tftp_fini(void) { -nf_conntrack_helpers_unregister (tftp, ports_c *2 ); +nf_conntrack_helpers_unregister (tftp_ptr, ports_c *2 ); }static int __init nf_conntrack_tftp_init (void )
@@ -127,7 +128,7 @@ static int __init nf_conntrack_tftp_init(void) THIS_MODULE); } - ret =nf_conntrack_helpers_register (tftp, ports_c *2 ); + ret =nf_conntrack_helpers_register (tftp, ports_c *2 , tftp_ptr);if (ret <0 ) {pr_err ("failed to register helpers\n" );return ret;
net/netfilter/nfnetlink_cthelper.c +22 -25
136 lines not on this page · raw message
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 3/7 netfilter: nf_conntrack_pptp: move GRE specific cleanup to GRE tracker
In reply to Pablo Neira AyusoMove the GRE specific cleanup to nf_conntrack_proto_gre.c to ensure that the .destroy callback for the pptp helper is still reachable by existing conntrack entries while pptp module is being removed.
This is a preparation patch, no functional changes are intended.
---
v5: no changes
.../net/netfilter/ipv4/nf_conntrack_ipv4.h | 4 ++ net/netfilter/nf_conntrack_pptp.c | 63 +------------------ net/netfilter/nf_conntrack_proto_gre.c | 61 ++++++++++++++++++ 3 files changed, 67 insertions(+), 61 deletions(-)
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +4 -0
@@ -20,4 +20,8 @@ extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp;extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_gre;#endif +#if IS_ENABLED (CONFIG_NF_CONNTRACK_PPTP ) +void gre_pptp_destroy_siblings (struct nf_conn *ct); +#endif +#endif /*_NF_CONNTRACK_IPV4_H*/
net/netfilter/nf_conntrack_pptp.c +2 -61
81 lines not on this page · raw message
net/netfilter/nf_conntrack_proto_gre.c +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 4/7 netfilter: nf_conntrack_helper: add refcounting from datapath
In reply to Pablo Neira AyusoThis patch adds a new ->ct_refcnt field to struct nf_conntrack_helper which is bumped when the helper is used by the ct helper extension. Drop this reference count when the conntrack entry is released. This is a packet path refcount which ensures that struct nf_conntrack_helper remains in place for tricky scenarios where a packet sits in nfqueue, or elsewhere, with a conntrack that refers to this helper.
For simplicity, this leaves a single refcount for helper objects in place, remove the existing refcount for control plane that ensures that the helper does not go away if it is used by ruleset.
On helper removal, the help callback is set to NULL to disable it from packet path and, after rcu grace period, existing expectations are removed. Update ctnetlink to disable access to .to_nlattr and .from_nlattr if the helper is going away.
Remove nf_queue_nf_hook_drop() since it has proven not to be effective because packets with unconfirmed conntracks which are still flying to sit in nfqueue.
---
v5: no changes
include/net/netfilter/nf_conntrack_helper.h | 26 +++++++++-- net/netfilter/nf_conntrack_core.c | 3 +- net/netfilter/nf_conntrack_helper.c | 52 ++++++++++----------- net/netfilter/nf_conntrack_netlink.c | 28 +++++++---- net/netfilter/nf_conntrack_ovs.c | 9 +++- net/netfilter/nf_conntrack_proto.c | 15 ++++-- net/netfilter/nfnetlink_cthelper.c | 14 ++---- net/netfilter/nft_ct.c | 3 +- net/netfilter/xt_CT.c | 3 -- 9 files changed, 89 insertions(+), 64 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +21 -5
@@ -35,20 +35,22 @@ enum nf_ct_helper_flags {struct nf_conntrack_helper {struct hlist_node hnode;/* Internal use. */ +struct rcu_head rcu; +char name[NF_CT_HELPER_NAME_LEN ];/* name of the module */ -refcount_t refcnt;struct module *me;/* pointer to self */ struct nf_conntrack_expect_policy expect_policy[NF_CT_MAX_EXPECT_CLASSES ]; +refcount_t ct_refcnt; +/* Tuple of things we will help (compared against server response) */ struct nf_conntrack_tuple tuple;/* Function to call when data passes; return verdict, or -1 to invalidate. */ -int (*help)(struct sk_buff *skb, -unsigned int protoff, -struct nf_conn *ct, -enum ip_conntrack_info conntrackinfo); +int __rcu (*help)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);void (*destroy)(struct nf_conn *ct);
@@ -138,6 +140,20 @@ static inline void *nfct_help_data(const struct nf_conn *ct)return (void *)help->data; } +static inline void nf_ct_help_put (const struct nf_conn *ct) +{ +struct nf_conntrack_helper *helper; +struct nf_conn_help *help; + + help =nfct_help (ct); +if (!help) +return ; + + helper =rcu_dereference (help->helper); +if (helper &&refcount_dec_and_test (&helper->ct_refcnt)) +kfree_rcu (helper, rcu); +} +int nf_conntrack_helper_init (void );void nf_conntrack_helper_fini (void );
net/netfilter/nf_conntrack_core.c +2 -1
@@ -1746,6 +1746,7 @@ void nf_conntrack_free(struct nf_conn *ct) nat_hook->remove_nat_bysrc (ct); } +nf_ct_help_put (ct);nf_ct_timeout_put (ct);rcu_read_unlock ();
@@ -1829,7 +1830,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl, assign_helper =rcu_dereference (exp->assign_helper);if (assign_helper) { help =nf_ct_helper_ext_add (ct,GFP_ATOMIC ); -if (help) +if (help &&refcount_inc_not_zero (&assign_helper->ct_refcnt))rcu_assign_pointer (help->helper, assign_helper); }
net/netfilter/nf_conntrack_helper.c +24 -28
112 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +18 -10
@@ -240,7 +240,8 @@ static int ctnetlink_dump_helpinfo(struct sk_buff *skb,if (nla_put_string (skb,CTA_HELP_NAME , helper->name)) goto nla_put_failure; -if (helper->to_nlattr) +if (rcu_access_pointer (helper->help) && + helper->to_nlattr) helper->to_nlattr (skb, ct);nla_nest_end (skb, nest_helper);
@@ -1935,6 +1936,7 @@ static int ctnetlink_change_helper(struct nf_conn *ct,if (err <0 )return err; +rcu_read_lock ();/* don't change helper of sibling connections */ if (ct->master) {/* If we try to change the helper to the same thing twice,
@@ -1943,27 +1945,27 @@ static int ctnetlink_change_helper(struct nf_conn *ct,*/ err = -EBUSY ;if (help) { -rcu_read_lock (); helper =rcu_dereference (help->helper);if (helper && !strcmp (helper->name, helpname)) err =0 ; -rcu_read_unlock (); } - +rcu_read_unlock ();return err; } -if (!strcmp (helpname,"" )) { -if (help && help->helper) { +if (!strcmp (helpname,"" ) && help) { + helper =rcu_dereference (help->helper); +if (helper) {/* we had a helper before ... */ nf_ct_remove_expectations (ct);RCU_INIT_POINTER (help->helper,NULL ); +if (refcount_dec_and_test (&helper->ct_refcnt)) +kfree_rcu (helper, rcu); } - +rcu_read_unlock ();return 0 ; } -rcu_read_lock (); helper =__nf_conntrack_helper_find (helpname,nf_ct_l3num (ct),nf_ct_protonum (ct));if (helper ==NULL ) {
@@ -1974,7 +1976,8 @@ static int ctnetlink_change_helper(struct nf_conn *ct,if (help) {if (rcu_access_pointer (help->helper) == helper) {/* update private helper data if allowed. */ -if (helper->from_nlattr) +if (rcu_access_pointer (helper->help) && + helper->from_nlattr) helper->from_nlattr (helpinfo, ct); err =0 ; }else
@@ -2289,11 +2292,16 @@ ctnetlink_create_conntrack(struct net *net, goto err2; }/* set private helper data if allowed. */ -if (helper->from_nlattr) +if (rcu_access_pointer (helper->help) && + helper->from_nlattr) helper->from_nlattr (helpinfo, ct);/* disable helper auto-assignment for this entry */ ct->status |=IPS_HELPER ; +if (!refcount_inc_not_zero (&helper->ct_refcnt)) { + err = -ENOENT ; + goto err2; + }RCU_INIT_POINTER (help->helper, helper); } }
net/netfilter/nf_conntrack_ovs.c +8 -1
@@ -12,6 +12,9 @@int nf_ct_helper (struct sk_buff *skb,struct nf_conn *ct,enum ip_conntrack_info ctinfo,u16 proto) { +int (*helper_cb)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);const struct nf_conntrack_helper *helper;const struct nf_conn_help *help;unsigned int protoff;
@@ -60,7 +63,11 @@ int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct,if (helper->tuple.dst.protonum != proto)return NF_ACCEPT ; - err = helper->help (skb, protoff, ct, ctinfo); + helper_cb =rcu_dereference (helper->help); +if (!helper_cb) +return NF_ACCEPT ; + + err =helper_cb (skb, protoff, ct, ctinfo);if (err !=NF_ACCEPT )return err;
net/netfilter/nf_conntrack_proto.c +10 -5
@@ -129,6 +129,9 @@ unsigned int nf_confirm(void *priv, struct sk_buff *skb, const struct nf_hook_state *state) { +int (*helper_cb)(struct sk_buff *skb,unsigned int protoff, +struct nf_conn *ct, +enum ip_conntrack_info conntrackinfo);const struct nf_conn_help *help;enum ip_conntrack_info ctinfo;unsigned int protoff;
@@ -175,11 +178,13 @@ unsigned int nf_confirm(void *priv,/* rcu_read_lock()ed by nf_hook */ helper =rcu_dereference (help->helper);if (helper) { - ret = helper->help (skb, - protoff, - ct, ctinfo); -if (ret !=NF_ACCEPT ) -return ret; + helper_cb =rcu_dereference (helper->help); +if (helper_cb) { + ret =helper_cb (skb, protoff, + ct, ctinfo); +if (ret !=NF_ACCEPT ) +return ret; + } } }
net/netfilter/nfnetlink_cthelper.c +5 -9
@@ -713,15 +713,11 @@ static int nfnl_cthelper_del(struct sk_buff *skb, const struct nfnl_info *info, tuple.dst.protonum != cur->tuple.dst.protonum)) continue; -if (refcount_dec_if_one (&cur->refcnt)) { - found = true; -nf_conntrack_helper_unregister (cur); - -list_del (&nlcth->list); -kfree (nlcth); - }else { - ret = -EBUSY ; - } + found = true; +nf_conntrack_helper_unregister (cur); + +list_del (&nlcth->list); +kfree (nlcth); }/* Make sure we return success if we flush and there is no helpers */
net/netfilter/nft_ct.c +1 -2
@@ -1101,7 +1101,6 @@ static void nft_ct_helper_obj_destroy(const struct nft_ctx *ctx, {struct nft_ct_helper_obj *priv =nft_obj_data (obj); -nf_queue_nf_hook_drop (ctx->net);if (priv->helper4)nf_conntrack_helper_put (priv->helper4);if (priv->helper6)
@@ -1144,7 +1143,7 @@ static void nft_ct_helper_obj_eval(struct nft_object *obj,return ; help =nf_ct_helper_ext_add (ct,GFP_ATOMIC ); -if (help) { +if (help &&refcount_inc_not_zero (&to_assign->ct_refcnt)) {rcu_assign_pointer (help->helper, to_assign);set_bit (IPS_HELPER_BIT , &ct->status);
net/netfilter/xt_CT.c +0 -3
@@ -284,9 +284,6 @@ static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,struct nf_conn_help *help;if (ct) { -if (info->helper[0 ]) -nf_queue_nf_hook_drop (par->net); - help =nfct_help (ct);xt_ct_put_helper (help);
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 5/7 netfilter: conntrack: revert ct extension genid infrastructure
In reply to Pablo Neira AyusoThis infrastructure is not used anymore after moving ct timeout and helper to use datapath refcount to track object use.
Revert commit c56716c69ce1 ("netfilter: extensions: introduce extension genid count") this patch disables all ct extensions (leading to NULL) for unconfirmed conntracks, when this is only targeted at ct helper and ct timeout. There is also codebase that dereferences the ct extension without checking for NULL which could lead to crash.
---
v5: no changes
include/net/netfilter/nf_conntrack_extend.h | 12 ---- net/netfilter/nf_conntrack_core.c | 61 +-------------------- net/netfilter/nf_conntrack_extend.c | 32 +---------- 3 files changed, 2 insertions(+), 103 deletions(-)
include/net/netfilter/nf_conntrack_extend.h +0 -12
@@ -38,7 +38,6 @@ enum nf_ct_ext_id {struct nf_ct_ext {u8 offset[NF_CT_EXT_NUM ];u8 len; -unsigned int gen_id;char data[]__aligned(8) ; };
@@ -52,8 +51,6 @@ static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)return (ct->ext &&__nf_ct_ext_exist (ct->ext, id)); } -void *__nf_ct_ext_find (const struct nf_ct_ext *ext,u8 id); -static inline void *nf_ct_ext_find (const struct nf_conn *ct,u8 id) {struct nf_ct_ext *ext = ct->ext;
@@ -61,19 +58,10 @@ static inline void *nf_ct_ext_find(const struct nf_conn *ct, u8 id)if (!ext || !__nf_ct_ext_exist (ext, id))return NULL ; -if (unlikely (ext->gen_id)) -return __nf_ct_ext_find (ext, id); - return (void *)ct->ext + ct->ext->offset[id]; }/* Add this type, returns pointer to data or NULL. */ void *nf_ct_ext_add (struct nf_conn *ct,enum nf_ct_ext_id id,gfp_t gfp); -/* ext genid. if ext->id != ext_genid, extensions cannot be used -* anymore unless conntrack has CONFIRMED bit set. -*/ -extern atomic_t nf_conntrack_ext_genid; -void nf_ct_ext_bump_genid (void ); -#endif /* _NF_CONNTRACK_EXTEND_H */
net/netfilter/nf_conntrack_core.c +1 -60
101 lines not on this page · raw message
net/netfilter/nf_conntrack_extend.c +1 -31
@@ -27,8 +27,6 @@#define NF_CT_EXT_PREALLOC 128u/* conntrack events are on by default */ -atomic_t nf_conntrack_ext_genid__read_mostly =ATOMIC_INIT (1 ); -static const u8 nf_ct_ext_type_len[NF_CT_EXT_NUM ] = { [NF_CT_EXT_HELPER ] =sizeof (struct nf_conn_help ), #if IS_ENABLED(CONFIG_NF_NAT)
@@ -118,10 +116,8 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)if (!new)return NULL ; -if (!ct->ext) { +if (!ct->ext)memset (new->offset,0 ,sizeof (new->offset)); - new->gen_id =atomic_read (&nf_conntrack_ext_genid); - } new->offset[id] = newoff; new->len = newlen;
@@ -131,29 +127,3 @@ void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)return (void *)new + newoff; }EXPORT_SYMBOL (nf_ct_ext_add); - -/* Use nf_ct_ext_find wrapper. This is only useful for unconfirmed entries. */ -void *__nf_ct_ext_find (const struct nf_ct_ext *ext,u8 id) -{ -unsigned int gen_id =atomic_read (&nf_conntrack_ext_genid); -unsigned int this_id =READ_ONCE (ext->gen_id); - -if (!__nf_ct_ext_exist (ext, id)) -return NULL ; - -if (this_id ==0 || ext->gen_id == gen_id) -return (void *)ext + ext->offset[id]; - -return NULL ; -} -EXPORT_SYMBOL (__nf_ct_ext_find); - -void nf_ct_ext_bump_genid (void ) -{ -unsigned int value =atomic_inc_return (&nf_conntrack_ext_genid); - -if (value ==UINT_MAX ) -atomic_set (&nf_conntrack_ext_genid,1 ); - -msleep (HZ ); -}
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 6/7 netfilter: conntrack: call nf_ct_gre_keymap_destroy() if master helper is pptp
In reply to Pablo Neira AyusoFor GRE flows, validate that the ct master helper (if any) is pptp before calling nf_ct_gre_keymap_destroy(), so the helper data area can be accessed safely. Note that only the pptp helper provides a .destroy callback.
---
v5: no changes
net/netfilter/nf_conntrack_core.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_core.c +16 -2
@@ -562,9 +562,23 @@ static void destroy_gre_conntrack(struct nf_conn *ct) {#ifdef CONFIG_NF_CT_PROTO_GRE struct nf_conn *master = ct->master; +struct nf_conn_help *help; + +if (!master) +return ; + + help =nfct_help (master); +if (help) { +struct nf_conntrack_helper *helper; -if (master) -nf_ct_gre_keymap_destroy (master); +rcu_read_lock (); + helper =rcu_dereference (help->helper); +/* Only pptp helper has a destroy callback. */ +if (helper && helper->destroy) +nf_ct_gre_keymap_destroy (master); + +rcu_read_unlock (); + }#endif }
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 7/7 netfilter: ctnetlink: call helper->destroy() when unhelping conntrack
In reply to Pablo Neira AyusoThe pptp helper maintains GRE mappings that need to be released in case that the helper is unset. Wait for RCU grace period after unhelping the conntrack to ensure that packet do not race to add new mapping.
---
v5: new in this series, after AI reviewer report.
net/netfilter/nf_conntrack_netlink.c | 4 ++++ 1 file changed, 4 insertions(+)
net/netfilter/nf_conntrack_netlink.c +4 -0
@@ -1959,6 +1959,10 @@ static int ctnetlink_change_helper(struct nf_conn *ct,/* we had a helper before ... */ nf_ct_remove_expectations (ct);RCU_INIT_POINTER (help->helper,NULL ); +if (helper->destroy) { +synchronize_rcu (); + helper->destroy (ct); + }if (refcount_dec_and_test (&helper->ct_refcnt))kfree_rcu (helper, rcu); }
Signature · 1 line
2.47.3