a7ad0e19e0de269cb67ff058f955ebd7986f16f9
[linux-2.6-block.git] / net / netfilter / nf_conntrack_acct.c
1 /* Accouting handling for netfilter. */
2
3 /*
4  * (C) 2008 Krzysztof Piotr Oledzki <ole@ans.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/netfilter.h>
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/moduleparam.h>
17 #include <linux/export.h>
18
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_extend.h>
21 #include <net/netfilter/nf_conntrack_acct.h>
22
23 static bool nf_ct_acct __read_mostly;
24
25 module_param_named(acct, nf_ct_acct, bool, 0644);
26 MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
27
28 #ifdef CONFIG_SYSCTL
29 static struct ctl_table acct_sysctl_table[] = {
30         {
31                 .procname       = "nf_conntrack_acct",
32                 .data           = &init_net.ct.sysctl_acct,
33                 .maxlen         = sizeof(unsigned int),
34                 .mode           = 0644,
35                 .proc_handler   = proc_dointvec,
36         },
37         {}
38 };
39 #endif /* CONFIG_SYSCTL */
40
41 static const struct nf_ct_ext_type acct_extend = {
42         .len    = sizeof(struct nf_conn_acct),
43         .align  = __alignof__(struct nf_conn_acct),
44         .id     = NF_CT_EXT_ACCT,
45 };
46
47 #ifdef CONFIG_SYSCTL
48 static int nf_conntrack_acct_init_sysctl(struct net *net)
49 {
50         struct ctl_table *table;
51
52         table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
53                         GFP_KERNEL);
54         if (!table)
55                 goto out;
56
57         table[0].data = &net->ct.sysctl_acct;
58
59         /* Don't export sysctls to unprivileged users */
60         if (net->user_ns != &init_user_ns)
61                 table[0].procname = NULL;
62
63         net->ct.acct_sysctl_header = register_net_sysctl(net, "net/netfilter",
64                                                          table);
65         if (!net->ct.acct_sysctl_header) {
66                 pr_err("can't register to sysctl\n");
67                 goto out_register;
68         }
69         return 0;
70
71 out_register:
72         kfree(table);
73 out:
74         return -ENOMEM;
75 }
76
77 static void nf_conntrack_acct_fini_sysctl(struct net *net)
78 {
79         struct ctl_table *table;
80
81         table = net->ct.acct_sysctl_header->ctl_table_arg;
82         unregister_net_sysctl_table(net->ct.acct_sysctl_header);
83         kfree(table);
84 }
85 #else
86 static int nf_conntrack_acct_init_sysctl(struct net *net)
87 {
88         return 0;
89 }
90
91 static void nf_conntrack_acct_fini_sysctl(struct net *net)
92 {
93 }
94 #endif
95
96 int nf_conntrack_acct_pernet_init(struct net *net)
97 {
98         net->ct.sysctl_acct = nf_ct_acct;
99         return nf_conntrack_acct_init_sysctl(net);
100 }
101
102 void nf_conntrack_acct_pernet_fini(struct net *net)
103 {
104         nf_conntrack_acct_fini_sysctl(net);
105 }
106
107 int nf_conntrack_acct_init(void)
108 {
109         int ret = nf_ct_extend_register(&acct_extend);
110         if (ret < 0)
111                 pr_err("Unable to register extension\n");
112         return ret;
113 }
114
115 void nf_conntrack_acct_fini(void)
116 {
117         nf_ct_extend_unregister(&acct_extend);
118 }