Skip to content

Commit 3499abb

Browse files
Andreas Schultzummakynes
authored andcommitted
netfilter: nfacct: per network namespace support
- 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 Signed-off-by: Andreas Schultz <aschultz@tpip.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent d2168e8 commit 3499abb

4 files changed

Lines changed: 56 additions & 23 deletions

File tree

‎include/linux/netfilter/nfnetlink_acct.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _NFNL_ACCT_H_
33

44
#include <uapi/linux/netfilter/nfnetlink_acct.h>
5+
#include <net/net_namespace.h>
56

67
enum {
78
NFACCT_NO_QUOTA = -1,
@@ -11,7 +12,7 @@ enum {
1112

1213
struct nf_acct;
1314

14-
struct nf_acct *nfnl_acct_find_get(const char *filter_name);
15+
struct nf_acct *nfnl_acct_find_get(struct net *net, const char *filter_name);
1516
void nfnl_acct_put(struct nf_acct *acct);
1617
void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct);
1718
extern int nfnl_acct_overquota(const struct sk_buff *skb,

‎include/net/net_namespace.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ struct net {
118118
#endif
119119
struct sock *nfnl;
120120
struct sock *nfnl_stash;
121+
#if IS_ENABLED(CONFIG_NETFILTER_NETLINK_ACCT)
122+
struct list_head nfnl_acct_list;
123+
#endif
121124
#endif
122125
#ifdef CONFIG_WEXT_CORE
123126
struct sk_buff_head wext_nlevents;

‎net/netfilter/nfnetlink_acct.c‎

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ MODULE_LICENSE("GPL");
2727
MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
2828
MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
2929

30-
static LIST_HEAD(nfnl_acct_list);
31-
3230
struct nf_acct {
3331
atomic64_t pkts;
3432
atomic64_t bytes;
@@ -53,6 +51,7 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
5351
const struct nlmsghdr *nlh, const struct nlattr * const tb[])
5452
{
5553
struct nf_acct *nfacct, *matching = NULL;
54+
struct net *net = sock_net(nfnl);
5655
char *acct_name;
5756
unsigned int size = 0;
5857
u32 flags = 0;
@@ -64,7 +63,7 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
6463
if (strlen(acct_name) == 0)
6564
return -EINVAL;
6665

67-
list_for_each_entry(nfacct, &nfnl_acct_list, head) {
66+
list_for_each_entry(nfacct, &net->nfnl_acct_list, head) {
6867
if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0)
6968
continue;
7069

@@ -124,7 +123,7 @@ nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
124123
be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
125124
}
126125
atomic_set(&nfacct->refcnt, 1);
127-
list_add_tail_rcu(&nfacct->head, &nfnl_acct_list);
126+
list_add_tail_rcu(&nfacct->head, &net->nfnl_acct_list);
128127
return 0;
129128
}
130129

@@ -185,6 +184,7 @@ nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
185184
static int
186185
nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
187186
{
187+
struct net *net = sock_net(skb->sk);
188188
struct nf_acct *cur, *last;
189189
const struct nfacct_filter *filter = cb->data;
190190

@@ -196,7 +196,7 @@ nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
196196
cb->args[1] = 0;
197197

198198
rcu_read_lock();
199-
list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
199+
list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
200200
if (last) {
201201
if (cur != last)
202202
continue;
@@ -257,6 +257,7 @@ static int
257257
nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
258258
const struct nlmsghdr *nlh, const struct nlattr * const tb[])
259259
{
260+
struct net *net = sock_net(nfnl);
260261
int ret = -ENOENT;
261262
struct nf_acct *cur;
262263
char *acct_name;
@@ -283,7 +284,7 @@ nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
283284
return -EINVAL;
284285
acct_name = nla_data(tb[NFACCT_NAME]);
285286

286-
list_for_each_entry(cur, &nfnl_acct_list, head) {
287+
list_for_each_entry(cur, &net->nfnl_acct_list, head) {
287288
struct sk_buff *skb2;
288289

289290
if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
@@ -336,19 +337,20 @@ static int
336337
nfnl_acct_del(struct sock *nfnl, struct sk_buff *skb,
337338
const struct nlmsghdr *nlh, const struct nlattr * const tb[])
338339
{
340+
struct net *net = sock_net(nfnl);
339341
char *acct_name;
340342
struct nf_acct *cur;
341343
int ret = -ENOENT;
342344

343345
if (!tb[NFACCT_NAME]) {
344-
list_for_each_entry(cur, &nfnl_acct_list, head)
346+
list_for_each_entry(cur, &net->nfnl_acct_list, head)
345347
nfnl_acct_try_del(cur);
346348

347349
return 0;
348350
}
349351
acct_name = nla_data(tb[NFACCT_NAME]);
350352

351-
list_for_each_entry(cur, &nfnl_acct_list, head) {
353+
list_for_each_entry(cur, &net->nfnl_acct_list, head) {
352354
if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
353355
continue;
354356

@@ -394,12 +396,12 @@ static const struct nfnetlink_subsystem nfnl_acct_subsys = {
394396

395397
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
396398

397-
struct nf_acct *nfnl_acct_find_get(const char *acct_name)
399+
struct nf_acct *nfnl_acct_find_get(struct net *net, const char *acct_name)
398400
{
399401
struct nf_acct *cur, *acct = NULL;
400402

401403
rcu_read_lock();
402-
list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
404+
list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
403405
if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
404406
continue;
405407

@@ -422,7 +424,9 @@ EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
422424

423425
void nfnl_acct_put(struct nf_acct *acct)
424426
{
425-
atomic_dec(&acct->refcnt);
427+
if (atomic_dec_and_test(&acct->refcnt))
428+
kfree_rcu(acct, rcu_head);
429+
426430
module_put(THIS_MODULE);
427431
}
428432
EXPORT_SYMBOL_GPL(nfnl_acct_put);
@@ -478,34 +482,59 @@ int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
478482
}
479483
EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
480484

485+
static int __net_init nfnl_acct_net_init(struct net *net)
486+
{
487+
INIT_LIST_HEAD(&net->nfnl_acct_list);
488+
489+
return 0;
490+
}
491+
492+
static void __net_exit nfnl_acct_net_exit(struct net *net)
493+
{
494+
struct nf_acct *cur, *tmp;
495+
496+
list_for_each_entry_safe(cur, tmp, &net->nfnl_acct_list, head) {
497+
list_del_rcu(&cur->head);
498+
499+
if (atomic_dec_and_test(&cur->refcnt))
500+
kfree_rcu(cur, rcu_head);
501+
}
502+
}
503+
504+
static struct pernet_operations nfnl_acct_ops = {
505+
.init = nfnl_acct_net_init,
506+
.exit = nfnl_acct_net_exit,
507+
};
508+
481509
static int __init nfnl_acct_init(void)
482510
{
483511
int ret;
484512

513+
ret = register_pernet_subsys(&nfnl_acct_ops);
514+
if (ret < 0) {
515+
pr_err("nfnl_acct_init: failed to register pernet ops\n");
516+
goto err_out;
517+
}
518+
485519
pr_info("nfnl_acct: registering with nfnetlink.\n");
486520
ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
487521
if (ret < 0) {
488522
pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
489-
goto err_out;
523+
goto cleanup_pernet;
490524
}
491525
return 0;
526+
527+
cleanup_pernet:
528+
unregister_pernet_subsys(&nfnl_acct_ops);
492529
err_out:
493530
return ret;
494531
}
495532

496533
static void __exit nfnl_acct_exit(void)
497534
{
498-
struct nf_acct *cur, *tmp;
499-
500535
pr_info("nfnl_acct: unregistering from nfnetlink.\n");
501536
nfnetlink_subsys_unregister(&nfnl_acct_subsys);
502-
503-
list_for_each_entry_safe(cur, tmp, &nfnl_acct_list, head) {
504-
list_del_rcu(&cur->head);
505-
/* We are sure that our objects have no clients at this point,
506-
* it's safe to release them all without checking refcnt. */
507-
kfree_rcu(cur, rcu_head);
508-
}
537+
unregister_pernet_subsys(&nfnl_acct_ops);
509538
}
510539

511540
module_init(nfnl_acct_init);

‎net/netfilter/xt_nfacct.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ nfacct_mt_checkentry(const struct xt_mtchk_param *par)
3737
struct xt_nfacct_match_info *info = par->matchinfo;
3838
struct nf_acct *nfacct;
3939

40-
nfacct = nfnl_acct_find_get(info->name);
40+
nfacct = nfnl_acct_find_get(par->net, info->name);
4141
if (nfacct == NULL) {
4242
pr_info("xt_nfacct: accounting object with name `%s' "
4343
"does not exists\n", info->name);

0 commit comments

Comments
 (0)