@@ -27,8 +27,6 @@ MODULE_LICENSE("GPL");
2727MODULE_AUTHOR ("Pablo Neira Ayuso <pablo@netfilter.org>" );
2828MODULE_DESCRIPTION ("nfacct: Extended Netfilter accounting infrastructure" );
2929
30- static LIST_HEAD (nfnl_acct_list );
31-
3230struct 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,
185184static int
186185nfnl_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
257257nfnl_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
336337nfnl_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
395397MODULE_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
423425void 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}
428432EXPORT_SYMBOL_GPL (nfnl_acct_put );
@@ -478,34 +482,59 @@ int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
478482}
479483EXPORT_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+
481509static 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 );
492529err_out :
493530 return ret ;
494531}
495532
496533static 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
511540module_init (nfnl_acct_init );
0 commit comments