Merge branches 'pm-cpuidle' and 'pm-cpufreq'
[deliverable/linux.git] / net / netfilter / nf_conntrack_acct.c
CommitLineData
58401572
KPO
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>
5a0e3ad6 12#include <linux/slab.h>
58401572
KPO
13#include <linux/kernel.h>
14#include <linux/moduleparam.h>
bc3b2d7f 15#include <linux/export.h>
58401572
KPO
16
17#include <net/netfilter/nf_conntrack.h>
18#include <net/netfilter/nf_conntrack_extend.h>
19#include <net/netfilter/nf_conntrack_acct.h>
20
eb939922 21static bool nf_ct_acct __read_mostly;
58401572
KPO
22
23module_param_named(acct, nf_ct_acct, bool, 0644);
24MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
25
26#ifdef CONFIG_SYSCTL
58401572
KPO
27static struct ctl_table acct_sysctl_table[] = {
28 {
58401572 29 .procname = "nf_conntrack_acct",
d716a4df 30 .data = &init_net.ct.sysctl_acct,
58401572
KPO
31 .maxlen = sizeof(unsigned int),
32 .mode = 0644,
6d9f239a 33 .proc_handler = proc_dointvec,
58401572
KPO
34 },
35 {}
36};
37#endif /* CONFIG_SYSCTL */
38
39unsigned int
40seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
41{
f7b13e43
HE
42 struct nf_conn_acct *acct;
43 struct nf_conn_counter *counter;
58401572
KPO
44
45 acct = nf_conn_acct_find(ct);
46 if (!acct)
47 return 0;
48
f7b13e43 49 counter = acct->counter;
58401572 50 return seq_printf(s, "packets=%llu bytes=%llu ",
f7b13e43
HE
51 (unsigned long long)atomic64_read(&counter[dir].packets),
52 (unsigned long long)atomic64_read(&counter[dir].bytes));
58401572
KPO
53};
54EXPORT_SYMBOL_GPL(seq_print_acct);
55
56static struct nf_ct_ext_type acct_extend __read_mostly = {
f7b13e43
HE
57 .len = sizeof(struct nf_conn_acct),
58 .align = __alignof__(struct nf_conn_acct),
58401572
KPO
59 .id = NF_CT_EXT_ACCT,
60};
61
d716a4df
AD
62#ifdef CONFIG_SYSCTL
63static int nf_conntrack_acct_init_sysctl(struct net *net)
58401572 64{
d716a4df 65 struct ctl_table *table;
58401572 66
d716a4df
AD
67 table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
68 GFP_KERNEL);
69 if (!table)
70 goto out;
71
72 table[0].data = &net->ct.sysctl_acct;
58401572 73
464dc801
EB
74 /* Don't export sysctls to unprivileged users */
75 if (net->user_ns != &init_user_ns)
76 table[0].procname = NULL;
77
ec8f23ce
EB
78 net->ct.acct_sysctl_header = register_net_sysctl(net, "net/netfilter",
79 table);
d716a4df
AD
80 if (!net->ct.acct_sysctl_header) {
81 printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n");
82 goto out_register;
58401572 83 }
d716a4df 84 return 0;
58401572 85
d716a4df
AD
86out_register:
87 kfree(table);
88out:
89 return -ENOMEM;
90}
58401572 91
d716a4df
AD
92static void nf_conntrack_acct_fini_sysctl(struct net *net)
93{
94 struct ctl_table *table;
58401572 95
d716a4df
AD
96 table = net->ct.acct_sysctl_header->ctl_table_arg;
97 unregister_net_sysctl_table(net->ct.acct_sysctl_header);
98 kfree(table);
99}
100#else
101static int nf_conntrack_acct_init_sysctl(struct net *net)
102{
103 return 0;
104}
105
106static void nf_conntrack_acct_fini_sysctl(struct net *net)
107{
108}
109#endif
110
b7ff3a1f 111int nf_conntrack_acct_pernet_init(struct net *net)
d716a4df 112{
d716a4df 113 net->ct.sysctl_acct = nf_ct_acct;
b7ff3a1f
G
114 return nf_conntrack_acct_init_sysctl(net);
115}
d716a4df 116
b7ff3a1f
G
117void nf_conntrack_acct_pernet_fini(struct net *net)
118{
119 nf_conntrack_acct_fini_sysctl(net);
120}
d716a4df 121
b7ff3a1f
G
122int nf_conntrack_acct_init(void)
123{
124 int ret = nf_ct_extend_register(&acct_extend);
d716a4df 125 if (ret < 0)
b7ff3a1f 126 pr_err("nf_conntrack_acct: Unable to register extension\n");
d716a4df 127 return ret;
58401572
KPO
128}
129
b7ff3a1f 130void nf_conntrack_acct_fini(void)
58401572 131{
b7ff3a1f 132 nf_ct_extend_unregister(&acct_extend);
58401572 133}
This page took 0.355217 seconds and 5 git commands to generate.