Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | |
| 16 | #include <net/netfilter/nf_conntrack.h> |
| 17 | #include <net/netfilter/nf_conntrack_extend.h> |
| 18 | #include <net/netfilter/nf_conntrack_acct.h> |
| 19 | |
| 20 | #ifdef CONFIG_NF_CT_ACCT |
| 21 | #define NF_CT_ACCT_DEFAULT 1 |
| 22 | #else |
| 23 | #define NF_CT_ACCT_DEFAULT 0 |
| 24 | #endif |
| 25 | |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 26 | static int nf_ct_acct __read_mostly = NF_CT_ACCT_DEFAULT; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 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 |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 32 | static struct ctl_table acct_sysctl_table[] = { |
| 33 | { |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 34 | .procname = "nf_conntrack_acct", |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 35 | .data = &init_net.ct.sysctl_acct, |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 36 | .maxlen = sizeof(unsigned int), |
| 37 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 38 | .proc_handler = proc_dointvec, |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 39 | }, |
| 40 | {} |
| 41 | }; |
| 42 | #endif /* CONFIG_SYSCTL */ |
| 43 | |
| 44 | unsigned int |
| 45 | seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir) |
| 46 | { |
| 47 | struct nf_conn_counter *acct; |
| 48 | |
| 49 | acct = nf_conn_acct_find(ct); |
| 50 | if (!acct) |
| 51 | return 0; |
| 52 | |
| 53 | return seq_printf(s, "packets=%llu bytes=%llu ", |
| 54 | (unsigned long long)acct[dir].packets, |
| 55 | (unsigned long long)acct[dir].bytes); |
| 56 | }; |
| 57 | EXPORT_SYMBOL_GPL(seq_print_acct); |
| 58 | |
| 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]), |
| 62 | .id = NF_CT_EXT_ACCT, |
| 63 | }; |
| 64 | |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 65 | #ifdef CONFIG_SYSCTL |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 66 | static int nf_conntrack_acct_init_sysctl(struct net *net) |
| 67 | { |
| 68 | struct ctl_table *table; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 69 | |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 70 | table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table), |
| 71 | GFP_KERNEL); |
| 72 | if (!table) |
| 73 | goto out; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 74 | |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 75 | table[0].data = &net->ct.sysctl_acct; |
| 76 | |
| 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) { |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 80 | printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n"); |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 81 | goto out_register; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 82 | } |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 83 | return 0; |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 84 | |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 85 | out_register: |
| 86 | kfree(table); |
| 87 | out: |
| 88 | return -ENOMEM; |
| 89 | } |
| 90 | |
| 91 | static void nf_conntrack_acct_fini_sysctl(struct net *net) |
| 92 | { |
| 93 | struct ctl_table *table; |
| 94 | |
| 95 | table = net->ct.acct_sysctl_header->ctl_table_arg; |
| 96 | unregister_net_sysctl_table(net->ct.acct_sysctl_header); |
| 97 | kfree(table); |
| 98 | } |
| 99 | #else |
| 100 | static int nf_conntrack_acct_init_sysctl(struct net *net) |
| 101 | { |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 105 | static void nf_conntrack_acct_fini_sysctl(struct net *net) |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 106 | { |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 107 | } |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 108 | #endif |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 109 | |
| 110 | int nf_conntrack_acct_init(struct net *net) |
| 111 | { |
| 112 | int ret; |
| 113 | |
| 114 | net->ct.sysctl_acct = nf_ct_acct; |
| 115 | |
| 116 | if (net_eq(net, &init_net)) { |
| 117 | #ifdef CONFIG_NF_CT_ACCT |
Danny ter Haar | 404d0ae | 2008-10-15 22:01:34 -0700 | [diff] [blame] | 118 | printk(KERN_WARNING "CONFIG_NF_CT_ACCT is deprecated and will be removed soon. Please use\n"); |
Martin Olsson | 6d60f9df | 2009-04-07 10:30:24 +0200 | [diff] [blame] | 119 | printk(KERN_WARNING "nf_conntrack.acct=1 kernel parameter, acct=1 nf_conntrack module option or\n"); |
Alexey Dobriyan | d716a4d | 2008-10-08 11:35:09 +0200 | [diff] [blame] | 120 | printk(KERN_WARNING "sysctl net.netfilter.nf_conntrack_acct=1 to enable it.\n"); |
| 121 | #endif |
| 122 | |
| 123 | ret = nf_ct_extend_register(&acct_extend); |
| 124 | if (ret < 0) { |
| 125 | printk(KERN_ERR "nf_conntrack_acct: Unable to register extension\n"); |
| 126 | goto out_extend_register; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | ret = nf_conntrack_acct_init_sysctl(net); |
| 131 | if (ret < 0) |
| 132 | goto out_sysctl; |
| 133 | |
| 134 | return 0; |
| 135 | |
| 136 | out_sysctl: |
| 137 | if (net_eq(net, &init_net)) |
| 138 | nf_ct_extend_unregister(&acct_extend); |
| 139 | out_extend_register: |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | void nf_conntrack_acct_fini(struct net *net) |
| 144 | { |
| 145 | nf_conntrack_acct_fini_sysctl(net); |
| 146 | if (net_eq(net, &init_net)) |
| 147 | nf_ct_extend_unregister(&acct_extend); |
Krzysztof Piotr Oledzki | 5840157 | 2008-07-21 10:01:34 -0700 | [diff] [blame] | 148 | } |