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 #include <linux/netfilter.h>
12 #include <linux/kernel.h>
13 #include <linux/moduleparam.h>
15 #include <net/netfilter/nf_conntrack.h>
16 #include <net/netfilter/nf_conntrack_extend.h>
17 #include <net/netfilter/nf_conntrack_acct.h>
19 #ifdef CONFIG_NF_CT_ACCT
20 #define NF_CT_ACCT_DEFAULT 1
22 #define NF_CT_ACCT_DEFAULT 0
25 static int nf_ct_acct __read_mostly = NF_CT_ACCT_DEFAULT;
27 module_param_named(acct, nf_ct_acct, bool, 0644);
28 MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
31 static struct ctl_table acct_sysctl_table[] = {
33 .ctl_name = CTL_UNNUMBERED,
34 .procname = "nf_conntrack_acct",
35 .data = &init_net.ct.sysctl_acct,
36 .maxlen = sizeof(unsigned int),
38 .proc_handler = proc_dointvec,
42 #endif /* CONFIG_SYSCTL */
45 seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
47 struct nf_conn_counter *acct;
49 acct = nf_conn_acct_find(ct);
53 return seq_printf(s, "packets=%llu bytes=%llu ",
54 (unsigned long long)acct[dir].packets,
55 (unsigned long long)acct[dir].bytes);
57 EXPORT_SYMBOL_GPL(seq_print_acct);
59 static struct nf_ct_ext_type acct_extend __read_mostly = {
60 .len = sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]),
61 .align = __alignof__(struct nf_conn_counter[IP_CT_DIR_MAX]),
66 static int nf_conntrack_acct_init_sysctl(struct net *net)
68 struct ctl_table *table;
70 table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
75 table[0].data = &net->ct.sysctl_acct;
77 net->ct.acct_sysctl_header = register_net_sysctl_table(net,
78 nf_net_netfilter_sysctl_path, table);
79 if (!net->ct.acct_sysctl_header) {
80 printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n");
91 static void nf_conntrack_acct_fini_sysctl(struct net *net)
93 struct ctl_table *table;
95 table = net->ct.acct_sysctl_header->ctl_table_arg;
96 unregister_net_sysctl_table(net->ct.acct_sysctl_header);
100 static int nf_conntrack_acct_init_sysctl(struct net *net)
105 static void nf_conntrack_acct_fini_sysctl(struct net *net)
110 int nf_conntrack_acct_init(struct net *net)
114 net->ct.sysctl_acct = nf_ct_acct;
116 if (net_eq(net, &init_net)) {
117 #ifdef CONFIG_NF_CT_ACCT
118 printk(KERN_WARNING "CONFIG_NF_CT_ACCT is deprecated and will be removed soon. Please use\n");
119 printk(KERN_WARNING "nf_conntrack.acct=1 kernel paramater, acct=1 nf_conntrack module option or\n");
120 printk(KERN_WARNING "sysctl net.netfilter.nf_conntrack_acct=1 to enable it.\n");
123 ret = nf_ct_extend_register(&acct_extend);
125 printk(KERN_ERR "nf_conntrack_acct: Unable to register extension\n");
126 goto out_extend_register;
130 ret = nf_conntrack_acct_init_sysctl(net);
137 if (net_eq(net, &init_net))
138 nf_ct_extend_unregister(&acct_extend);
143 void nf_conntrack_acct_fini(struct net *net)
145 nf_conntrack_acct_fini_sysctl(net);
146 if (net_eq(net, &init_net))
147 nf_ct_extend_unregister(&acct_extend);