1 /* Accouting handling for netfilter. */
4 * (C) 2008 Krzysztof Piotr Oledzki <ole@ans.pl>
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_extend.h>
21 #include <net/netfilter/nf_conntrack_acct.h>
23 static bool nf_ct_acct __read_mostly;
25 module_param_named(acct, nf_ct_acct, bool, 0644);
26 MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
29 static struct ctl_table acct_sysctl_table[] = {
31 .procname = "nf_conntrack_acct",
32 .data = &init_net.ct.sysctl_acct,
33 .maxlen = sizeof(unsigned int),
35 .proc_handler = proc_dointvec,
39 #endif /* CONFIG_SYSCTL */
42 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
44 struct nf_conn_acct *acct;
45 struct nf_conn_counter *counter;
47 acct = nf_conn_acct_find(ct);
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));
58 EXPORT_SYMBOL_GPL(seq_print_acct);
60 static const struct nf_ct_ext_type acct_extend = {
61 .len = sizeof(struct nf_conn_acct),
62 .align = __alignof__(struct nf_conn_acct),
67 static int nf_conntrack_acct_init_sysctl(struct net *net)
69 struct ctl_table *table;
71 table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
76 table[0].data = &net->ct.sysctl_acct;
78 /* Don't export sysctls to unprivileged users */
79 if (net->user_ns != &init_user_ns)
80 table[0].procname = NULL;
82 net->ct.acct_sysctl_header = register_net_sysctl(net, "net/netfilter",
84 if (!net->ct.acct_sysctl_header) {
85 pr_err("can't register to sysctl\n");
96 static void nf_conntrack_acct_fini_sysctl(struct net *net)
98 struct ctl_table *table;
100 table = net->ct.acct_sysctl_header->ctl_table_arg;
101 unregister_net_sysctl_table(net->ct.acct_sysctl_header);
105 static int nf_conntrack_acct_init_sysctl(struct net *net)
110 static void nf_conntrack_acct_fini_sysctl(struct net *net)
115 int nf_conntrack_acct_pernet_init(struct net *net)
117 net->ct.sysctl_acct = nf_ct_acct;
118 return nf_conntrack_acct_init_sysctl(net);
121 void nf_conntrack_acct_pernet_fini(struct net *net)
123 nf_conntrack_acct_fini_sysctl(net);
126 int nf_conntrack_acct_init(void)
128 int ret = nf_ct_extend_register(&acct_extend);
130 pr_err("Unable to register extension\n");
134 void nf_conntrack_acct_fini(void)
136 nf_ct_extend_unregister(&acct_extend);