1
2
3
4
5
6
7
8
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
23static bool nf_ct_acct __read_mostly;
24
25module_param_named(acct, nf_ct_acct, bool, 0644);
26MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
27
28#ifdef CONFIG_SYSCTL
29static 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
40
41unsigned int
42seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
43{
44 struct nf_conn_acct *acct;
45 struct nf_conn_counter *counter;
46
47 acct = nf_conn_acct_find(ct);
48 if (!acct)
49 return 0;
50
51 counter = acct->counter;
52 seq_printf(s, "packets=%llu bytes=%llu ",
53 (unsigned long long)atomic64_read(&counter[dir].packets),
54 (unsigned long long)atomic64_read(&counter[dir].bytes));
55
56 return 0;
57};
58EXPORT_SYMBOL_GPL(seq_print_acct);
59
60static const struct nf_ct_ext_type acct_extend = {
61 .len = sizeof(struct nf_conn_acct),
62 .align = __alignof__(struct nf_conn_acct),
63 .id = NF_CT_EXT_ACCT,
64};
65
66#ifdef CONFIG_SYSCTL
67static int nf_conntrack_acct_init_sysctl(struct net *net)
68{
69 struct ctl_table *table;
70
71 table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
72 GFP_KERNEL);
73 if (!table)
74 goto out;
75
76 table[0].data = &net->ct.sysctl_acct;
77
78
79 if (net->user_ns != &init_user_ns)
80 table[0].procname = NULL;
81
82 net->ct.acct_sysctl_header = register_net_sysctl(net, "net/netfilter",
83 table);
84 if (!net->ct.acct_sysctl_header) {
85 pr_err("can't register to sysctl\n");
86 goto out_register;
87 }
88 return 0;
89
90out_register:
91 kfree(table);
92out:
93 return -ENOMEM;
94}
95
96static void nf_conntrack_acct_fini_sysctl(struct net *net)
97{
98 struct ctl_table *table;
99
100 table = net->ct.acct_sysctl_header->ctl_table_arg;
101 unregister_net_sysctl_table(net->ct.acct_sysctl_header);
102 kfree(table);
103}
104#else
105static int nf_conntrack_acct_init_sysctl(struct net *net)
106{
107 return 0;
108}
109
110static void nf_conntrack_acct_fini_sysctl(struct net *net)
111{
112}
113#endif
114
115int nf_conntrack_acct_pernet_init(struct net *net)
116{
117 net->ct.sysctl_acct = nf_ct_acct;
118 return nf_conntrack_acct_init_sysctl(net);
119}
120
121void nf_conntrack_acct_pernet_fini(struct net *net)
122{
123 nf_conntrack_acct_fini_sysctl(net);
124}
125
126int nf_conntrack_acct_init(void)
127{
128 int ret = nf_ct_extend_register(&acct_extend);
129 if (ret < 0)
130 pr_err("Unable to register extension\n");
131 return ret;
132}
133
134void nf_conntrack_acct_fini(void)
135{
136 nf_ct_extend_unregister(&acct_extend);
137}
138