netfilter: don't use INIT_RCU_HEAD()
[linux-2.6.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 #include <linux/netfilter.h>
12 #include <linux/kernel.h>
13 #include <linux/moduleparam.h>
14
15 #include <net/netfilter/nf_conntrack.h>
16 #include <net/netfilter/nf_conntrack_extend.h>
17 #include <net/netfilter/nf_conntrack_acct.h>
18
19 #ifdef CONFIG_NF_CT_ACCT
20 #define NF_CT_ACCT_DEFAULT 1
21 #else
22 #define NF_CT_ACCT_DEFAULT 0
23 #endif
24
25 static int nf_ct_acct __read_mostly = NF_CT_ACCT_DEFAULT;
26
27 module_param_named(acct, nf_ct_acct, bool, 0644);
28 MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
29
30 #ifdef CONFIG_SYSCTL
31 static struct ctl_table acct_sysctl_table[] = {
32         {
33                 .procname       = "nf_conntrack_acct",
34                 .data           = &init_net.ct.sysctl_acct,
35                 .maxlen         = sizeof(unsigned int),
36                 .mode           = 0644,
37                 .proc_handler   = proc_dointvec,
38         },
39         {}
40 };
41 #endif /* CONFIG_SYSCTL */
42
43 unsigned int
44 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
45 {
46         struct nf_conn_counter *acct;
47
48         acct = nf_conn_acct_find(ct);
49         if (!acct)
50                 return 0;
51
52         return seq_printf(s, "packets=%llu bytes=%llu ",
53                           (unsigned long long)acct[dir].packets,
54                           (unsigned long long)acct[dir].bytes);
55 };
56 EXPORT_SYMBOL_GPL(seq_print_acct);
57
58 static struct nf_ct_ext_type acct_extend __read_mostly = {
59         .len    = sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]),
60         .align  = __alignof__(struct nf_conn_counter[IP_CT_DIR_MAX]),
61         .id     = NF_CT_EXT_ACCT,
62 };
63
64 #ifdef CONFIG_SYSCTL
65 static int nf_conntrack_acct_init_sysctl(struct net *net)
66 {
67         struct ctl_table *table;
68
69         table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
70                         GFP_KERNEL);
71         if (!table)
72                 goto out;
73
74         table[0].data = &net->ct.sysctl_acct;
75
76         net->ct.acct_sysctl_header = register_net_sysctl_table(net,
77                         nf_net_netfilter_sysctl_path, table);
78         if (!net->ct.acct_sysctl_header) {
79                 printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n");
80                 goto out_register;
81         }
82         return 0;
83
84 out_register:
85         kfree(table);
86 out:
87         return -ENOMEM;
88 }
89
90 static void nf_conntrack_acct_fini_sysctl(struct net *net)
91 {
92         struct ctl_table *table;
93
94         table = net->ct.acct_sysctl_header->ctl_table_arg;
95         unregister_net_sysctl_table(net->ct.acct_sysctl_header);
96         kfree(table);
97 }
98 #else
99 static int nf_conntrack_acct_init_sysctl(struct net *net)
100 {
101         return 0;
102 }
103
104 static void nf_conntrack_acct_fini_sysctl(struct net *net)
105 {
106 }
107 #endif
108
109 int nf_conntrack_acct_init(struct net *net)
110 {
111         int ret;
112
113         net->ct.sysctl_acct = nf_ct_acct;
114
115         if (net_eq(net, &init_net)) {
116 #ifdef CONFIG_NF_CT_ACCT
117         printk(KERN_WARNING "CONFIG_NF_CT_ACCT is deprecated and will be removed soon. Please use\n");
118                 printk(KERN_WARNING "nf_conntrack.acct=1 kernel parameter, acct=1 nf_conntrack module option or\n");
119                 printk(KERN_WARNING "sysctl net.netfilter.nf_conntrack_acct=1 to enable it.\n");
120 #endif
121
122                 ret = nf_ct_extend_register(&acct_extend);
123                 if (ret < 0) {
124                         printk(KERN_ERR "nf_conntrack_acct: Unable to register extension\n");
125                         goto out_extend_register;
126                 }
127         }
128
129         ret = nf_conntrack_acct_init_sysctl(net);
130         if (ret < 0)
131                 goto out_sysctl;
132
133         return 0;
134
135 out_sysctl:
136         if (net_eq(net, &init_net))
137                 nf_ct_extend_unregister(&acct_extend);
138 out_extend_register:
139         return ret;
140 }
141
142 void nf_conntrack_acct_fini(struct net *net)
143 {
144         nf_conntrack_acct_fini_sysctl(net);
145         if (net_eq(net, &init_net))
146                 nf_ct_extend_unregister(&acct_extend);
147 }