linux/net/netfilter/nf_conntrack_acct.c
<<
>>
Prefs
   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/slab.h>
  13#include <linux/kernel.h>
  14#include <linux/moduleparam.h>
  15#include <linux/export.h>
  16
  17#include <net/netfilter/nf_conntrack.h>
  18#include <net/netfilter/nf_conntrack_extend.h>
  19#include <net/netfilter/nf_conntrack_acct.h>
  20
  21static bool nf_ct_acct __read_mostly;
  22
  23module_param_named(acct, nf_ct_acct, bool, 0644);
  24MODULE_PARM_DESC(acct, "Enable connection tracking flow accounting.");
  25
  26#ifdef CONFIG_SYSCTL
  27static struct ctl_table acct_sysctl_table[] = {
  28        {
  29                .procname       = "nf_conntrack_acct",
  30                .data           = &init_net.ct.sysctl_acct,
  31                .maxlen         = sizeof(unsigned int),
  32                .mode           = 0644,
  33                .proc_handler   = proc_dointvec,
  34        },
  35        {}
  36};
  37#endif /* CONFIG_SYSCTL */
  38
  39unsigned int
  40seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
  41{
  42        struct nf_conn_counter *acct;
  43
  44        acct = nf_conn_acct_find(ct);
  45        if (!acct)
  46                return 0;
  47
  48        return seq_printf(s, "packets=%llu bytes=%llu ",
  49                          (unsigned long long)atomic64_read(&acct[dir].packets),
  50                          (unsigned long long)atomic64_read(&acct[dir].bytes));
  51};
  52EXPORT_SYMBOL_GPL(seq_print_acct);
  53
  54static struct nf_ct_ext_type acct_extend __read_mostly = {
  55        .len    = sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]),
  56        .align  = __alignof__(struct nf_conn_counter[IP_CT_DIR_MAX]),
  57        .id     = NF_CT_EXT_ACCT,
  58};
  59
  60#ifdef CONFIG_SYSCTL
  61static int nf_conntrack_acct_init_sysctl(struct net *net)
  62{
  63        struct ctl_table *table;
  64
  65        table = kmemdup(acct_sysctl_table, sizeof(acct_sysctl_table),
  66                        GFP_KERNEL);
  67        if (!table)
  68                goto out;
  69
  70        table[0].data = &net->ct.sysctl_acct;
  71
  72        /* Don't export sysctls to unprivileged users */
  73        if (net->user_ns != &init_user_ns)
  74                table[0].procname = NULL;
  75
  76        net->ct.acct_sysctl_header = register_net_sysctl(net, "net/netfilter",
  77                                                         table);
  78        if (!net->ct.acct_sysctl_header) {
  79                printk(KERN_ERR "nf_conntrack_acct: can't register to sysctl.\n");
  80                goto out_register;
  81        }
  82        return 0;
  83
  84out_register:
  85        kfree(table);
  86out:
  87        return -ENOMEM;
  88}
  89
  90static void nf_conntrack_acct_fini_sysctl(struct net *net)
  91{
  92        struct ctl_table *table;
  93
  94        table = net->ct.acct_sysctl_header->ctl_table_arg;
  95        unregister_net_sysctl_table(net->ct.acct_sysctl_header);
  96        kfree(table);
  97}
  98#else
  99static int nf_conntrack_acct_init_sysctl(struct net *net)
 100{
 101        return 0;
 102}
 103
 104static void nf_conntrack_acct_fini_sysctl(struct net *net)
 105{
 106}
 107#endif
 108
 109int nf_conntrack_acct_pernet_init(struct net *net)
 110{
 111        net->ct.sysctl_acct = nf_ct_acct;
 112        return nf_conntrack_acct_init_sysctl(net);
 113}
 114
 115void nf_conntrack_acct_pernet_fini(struct net *net)
 116{
 117        nf_conntrack_acct_fini_sysctl(net);
 118}
 119
 120int nf_conntrack_acct_init(void)
 121{
 122        int ret = nf_ct_extend_register(&acct_extend);
 123        if (ret < 0)
 124                pr_err("nf_conntrack_acct: Unable to register extension\n");
 125        return ret;
 126}
 127
 128void nf_conntrack_acct_fini(void)
 129{
 130        nf_ct_extend_unregister(&acct_extend);
 131}
 132
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.