netfilter: per network namespace nfacct
Subsystems (2)
PATCH 1/1 netfilter: per network namespace nfacct
- Move the nfnl_acct_list into the network namespace, initialize and destroy it per namespace
- Keep track of refcnt on nfacct objects, the old logic does not longer work with a per namespace list
- Adjust xt_nfacct to pass the namespace when registring objects
--- include/linux/netfilter/nfnetlink_acct.h | 3 +- include/net/net_namespace.h | 3 ++ net/netfilter/nfnetlink_acct.c | 71 ++++++++++++++++++++++---------- net/netfilter/xt_nfacct.c | 2 +- 4 files changed, 56 insertions(+), 23 deletions(-)
include/linux/netfilter/nfnetlink_acct.h +2 -1
@@ -2,6 +2,7 @@#define _NFNL_ACCT_H_#include <uapi/linux/netfilter/nfnetlink_acct.h> +#include <net/net_namespace.h> enum {NFACCT_NO_QUOTA =-1 ,
@@ -11,7 +12,7 @@ enum {struct nf_acct ; -struct nf_acct *nfnl_acct_find_get (const char *filter_name); +struct nf_acct *nfnl_acct_find_get (struct net *net,const char *filter_name);void nfnl_acct_put (struct nf_acct *acct);void nfnl_acct_update (const struct sk_buff *skb,struct nf_acct *nfacct); extern int nfnl_acct_overquota(const struct sk_buff *skb,
include/net/net_namespace.h +3 -0
@@ -118,6 +118,9 @@ struct net {#endif struct sock *nfnl;struct sock *nfnl_stash; +#if IS_ENABLED (CONFIG_NETFILTER_NETLINK_ACCT ) +struct list_head nfnl_acct_list; +#endif #endif #ifdef CONFIG_WEXT_CORE struct sk_buff_head wext_nlevents;
net/netfilter/nfnetlink_acct.c +50 -21
176 lines not on this page · raw message
net/netfilter/xt_nfacct.c +1 -1
@@ -37,7 +37,7 @@ nfacct_mt_checkentry(const struct xt_mtchk_param *par)struct xt_nfacct_match_info *info = par->matchinfo;struct nf_acct *nfacct; - nfacct =nfnl_acct_find_get (info->name); + nfacct =nfnl_acct_find_get (par->net, info->name);if (nfacct ==NULL ) {pr_info ("xt_nfacct: accounting object with name `%s' " "does not exists\n" , info->name);
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainernet/netfilter/nfnetlink_acct.c int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) · line 489
Quoted 9 lines · Andreas Schultz
net/netfilter/nfnetlink_acct.c +5
-0@@ -478,34 +482,59 @@ int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) }EXPORT_SYMBOL_GPL (nfnl_acct_overquota); +static int __net_init nfnl_acct_net_init (struct net *net) +{ +INIT_LIST_HEAD (&net->nfnl_acct_list); + +return 0 ;
^^^^^^^^
Minor comestic: Please, make sure we get indent as tabs of 8-chars long.
net/netfilter/nfnetlink_acct.c int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) · line 502
Quoted 13 lines
net/netfilter/nfnetlink_acct.c +13
-0+} + +static void __net_exit nfnl_acct_net_exit(struct net *net) +{ +struct nf_acct *cur, *tmp; + +list_for_each_entry_safe (cur, tmp, &net->nfnl_acct_list, head) { +list_del_rcu (&cur->head); + +if (atomic_dec_and_test (&cur->refcnt)) +kfree_rcu (cur, rcu_head); + } +}
You better use nfnl_acct_put() here, otherwise we leak a module refcount.
Other than that, this looks fine with me. Please send a v2.
Thanks.
Quoted 1 line · Pablo Neira Ayuso
On Wed, Aug 05, 2015 at 05:51:45PM +0200, Andreas Schultz wrote:
[..]
Quoted 6 lines
Quoted 6 lines
net/netfilter/nfnetlink_acct.c +6
-0+static void __net_exit nfnl_acct_net_exit(struct net *net) +{ +struct nf_acct *cur, *tmp; + +list_for_each_entry_safe (cur, tmp, &net->nfnl_acct_list, head) { +list_del_rcu (&cur->head);
Quoted 8 lines
net/netfilter/nfnetlink_acct.c int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) · line 502
Quoted 5 lines
net/netfilter/nfnetlink_acct.c +5
-0+ +if (atomic_dec_and_test (&cur->refcnt)) +kfree_rcu (cur, rcu_head); + } +}You better use
nfnl_acct_put()here, otherwise we leak a module refcount.
The module refcount is only taken in nfnl_acct_find_get. The initial insert into the list in nfnl_acct_new is not taking the module refcount.
Releasing the module refcount here would IMHO release one recount to many. Or do I miss something?
Quoted 4 lines
Other than that, this looks fine with me. Please send a v2.
Thanks.
Andreas
Pablo Neira Ayuso
maintainerQuoted 20 lines · Andreas Schultz
Quoted 1 line · Pablo Neira Ayuso
On Wed, Aug 05, 2015 at 05:51:45PM +0200, Andreas Schultz wrote:
[..]
Quoted 6 lines
Quoted 6 lines
net/netfilter/nfnetlink_acct.c +6
-0+static void __net_exit nfnl_acct_net_exit(struct net *net) +{ +struct nf_acct *cur, *tmp; + +list_for_each_entry_safe (cur, tmp, &net->nfnl_acct_list, head) { +list_del_rcu (&cur->head);Quoted 8 lines
net/netfilter/nfnetlink_acct.c int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) · line 502
Quoted 5 lines
net/netfilter/nfnetlink_acct.c +5
-0+ +if (atomic_dec_and_test (&cur->refcnt)) +kfree_rcu (cur, rcu_head); + } +}You better use
nfnl_acct_put()here, otherwise we leak a module refcount.
Quoted 6 lines
The module refcount is only taken in
nfnl_acct_find_get. The initial insert into the list innfnl_acct_newis not taking the module refcount.Releasing the module refcount here would IMHO release one recount to many. Or do I miss something?
With netns in place, we don't know in what order the __net_exit functions are called, ie. We may still have references to objects from xt_nfacct.
Quoted 25 lines · Pablo Neira Ayuso
Quoted 24 lines · Andreas Schultz
Quoted 1 line · Pablo Neira Ayuso
On Wed, Aug 05, 2015 at 05:51:45PM +0200, Andreas Schultz wrote:
[..]
Quoted 6 lines
Quoted 6 lines
net/netfilter/nfnetlink_acct.c +6
-0+static void __net_exit nfnl_acct_net_exit(struct net *net) +{ +struct nf_acct *cur, *tmp; + +list_for_each_entry_safe (cur, tmp, &net->nfnl_acct_list, head) { +list_del_rcu (&cur->head);Quoted 8 lines
net/netfilter/nfnetlink_acct.c int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct) · line 502
Quoted 5 lines
net/netfilter/nfnetlink_acct.c +5
-0+ +if (atomic_dec_and_test (&cur->refcnt)) +kfree_rcu (cur, rcu_head); + } +}You better use
nfnl_acct_put()here, otherwise we leak a module refcount.The module refcount is only taken in
nfnl_acct_find_get. The initial insert into the list innfnl_acct_newis not taking the module refcount.
Quoted 6 lines
Quoted 2 lines
Releasing the module refcount here would IMHO release one recount to many. Or do I miss something?
With netns in place, we don't know in what order the
__net_exitfunctions are called, ie. We may still have references to objects fromxt_nfacct.
Exactly my point, only the xt_nfacct object references also takes a module reference. We can not release this reference for them. So we remove the nfacct object from the list and only free the object if xt_nfacct is *NOT* holding a reference.
If xt_nfacct is holding a reference, it will put that reference together with the module reference later.
Pablo Neira Ayuso
maintainerQuoted 1 line · Andreas Schultz
On 08/06/2015 03:42 PM, Pablo Neira Ayuso wrote:
[...]
Quoted 11 lines
Quoted 3 lines
With netns in place, we don't know in what order the
__net_exitfunctions are called, ie. We may still have references to objects fromxt_nfacct.Exactly my point, only the
xt_nfacctobject references also takes a module reference. We can not release this reference for them. So we remove the nfacct object from the list and only free the object ifxt_nfacctis *NOT* holding a reference.If
xt_nfacctis holding a reference, it will put that reference together with the module reference later.
You're right. This patch is applied, thanks Andreas:
BTW, I have mangled the original subject to:
netfilter: nfacct: per network namespace support
Just for the next time, we prefer this format:
SUBSYS: COMPONENT: description