Commit | Line | Data |
---|---|---|
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> | |
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 | int nf_ct_acct __read_mostly = NF_CT_ACCT_DEFAULT; | |
26 | EXPORT_SYMBOL_GPL(nf_ct_acct); | |
27 | ||
28 | module_param_named(acct, nf_ct_acct, bool, 0644); | |
29 | MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting."); | |
30 | ||
31 | #ifdef CONFIG_SYSCTL | |
32 | static struct ctl_table_header *acct_sysctl_header; | |
33 | static struct ctl_table acct_sysctl_table[] = { | |
34 | { | |
35 | .ctl_name = CTL_UNNUMBERED, | |
36 | .procname = "nf_conntrack_acct", | |
37 | .data = &nf_ct_acct, | |
38 | .maxlen = sizeof(unsigned int), | |
39 | .mode = 0644, | |
40 | .proc_handler = &proc_dointvec, | |
41 | }, | |
42 | {} | |
43 | }; | |
44 | #endif /* CONFIG_SYSCTL */ | |
45 | ||
46 | unsigned int | |
47 | seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir) | |
48 | { | |
49 | struct nf_conn_counter *acct; | |
50 | ||
51 | acct = nf_conn_acct_find(ct); | |
52 | if (!acct) | |
53 | return 0; | |
54 | ||
55 | return seq_printf(s, "packets=%llu bytes=%llu ", | |
56 | (unsigned long long)acct[dir].packets, | |
57 | (unsigned long long)acct[dir].bytes); | |
58 | }; | |
59 | EXPORT_SYMBOL_GPL(seq_print_acct); | |
60 | ||
61 | static struct nf_ct_ext_type acct_extend __read_mostly = { | |
62 | .len = sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]), | |
63 | .align = __alignof__(struct nf_conn_counter[IP_CT_DIR_MAX]), | |
64 | .id = NF_CT_EXT_ACCT, | |
65 | }; | |
66 | ||
67 | int nf_conntrack_acct_init(void) | |
68 | { | |
69 | int ret; | |
70 | ||
71 | #ifdef CONFIG_NF_CT_ACCT | |
72 | printk(KERN_WARNING "CONFIG_NF_CT_ACCT is deprecated and will be removed soon. Plase use\n"); | |
73 | printk(KERN_WARNING "nf_conntrack.acct=1 kernel paramater, acct=1 nf_conntrack module option or\n"); | |
74 | printk(KERN_WARNING "sysctl net.netfilter.nf_conntrack_acct=1 to enable it.\n"); | |
75 | #endif | |
76 | ||
77 | ret = nf_ct_extend_register(&acct_extend); | |
78 | if (ret < 0) { | |
79 | printk(KERN_ERR "nf_conntrack_acct: Unable to register extension\n"); | |
80 | return ret; | |
81 | } | |
82 | ||
83 | #ifdef CONFIG_SYSCTL | |
84 | acct_sysctl_header = register_sysctl_paths(nf_net_netfilter_sysctl_path, | |
85 | acct_sysctl_table); | |
86 | ||
87 | if (!acct_sysctl_header) { | |
88 | nf_ct_extend_unregister(&acct_extend); | |
89 | ||
90 | printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n"); | |
91 | return -ENOMEM; | |
92 | } | |
93 | #endif | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | void nf_conntrack_acct_fini(void) | |
99 | { | |
100 | #ifdef CONFIG_SYSCTL | |
101 | unregister_sysctl_table(acct_sysctl_header); | |
102 | #endif | |
103 | nf_ct_extend_unregister(&acct_extend); | |
104 | } |