Pablo Neira Ayuso | ceb98d0 | 2011-12-23 14:28:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org> |
| 3 | * (C) 2011 Intra2net AG <http://www.intra2net.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 (or any |
| 7 | * later at your option) as published by the Free Software Foundation. |
| 8 | */ |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/skbuff.h> |
| 11 | |
| 12 | #include <linux/netfilter/x_tables.h> |
| 13 | #include <linux/netfilter/nfnetlink_acct.h> |
| 14 | #include <linux/netfilter/xt_nfacct.h> |
| 15 | |
| 16 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); |
| 17 | MODULE_DESCRIPTION("Xtables: match for the extended accounting infrastructure"); |
| 18 | MODULE_LICENSE("GPL"); |
| 19 | MODULE_ALIAS("ipt_nfacct"); |
| 20 | MODULE_ALIAS("ip6t_nfacct"); |
| 21 | |
| 22 | static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par) |
| 23 | { |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 24 | int overquota; |
Pablo Neira Ayuso | ceb98d0 | 2011-12-23 14:28:59 +0100 | [diff] [blame] | 25 | const struct xt_nfacct_match_info *info = par->targinfo; |
| 26 | |
| 27 | nfnl_acct_update(skb, info->nfacct); |
| 28 | |
Mathieu Poirier | 683399e | 2014-04-20 18:57:36 -0600 | [diff] [blame] | 29 | overquota = nfnl_acct_overquota(skb, info->nfacct); |
| 30 | |
| 31 | return overquota == NFACCT_UNDERQUOTA ? false : true; |
Pablo Neira Ayuso | ceb98d0 | 2011-12-23 14:28:59 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static int |
| 35 | nfacct_mt_checkentry(const struct xt_mtchk_param *par) |
| 36 | { |
| 37 | struct xt_nfacct_match_info *info = par->matchinfo; |
| 38 | struct nf_acct *nfacct; |
| 39 | |
Andreas Schultz | 3499abb | 2015-08-05 17:51:45 +0200 | [diff] [blame] | 40 | nfacct = nfnl_acct_find_get(par->net, info->name); |
Pablo Neira Ayuso | ceb98d0 | 2011-12-23 14:28:59 +0100 | [diff] [blame] | 41 | if (nfacct == NULL) { |
| 42 | pr_info("xt_nfacct: accounting object with name `%s' " |
| 43 | "does not exists\n", info->name); |
| 44 | return -ENOENT; |
| 45 | } |
| 46 | info->nfacct = nfacct; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | nfacct_mt_destroy(const struct xt_mtdtor_param *par) |
| 52 | { |
| 53 | const struct xt_nfacct_match_info *info = par->matchinfo; |
| 54 | |
| 55 | nfnl_acct_put(info->nfacct); |
| 56 | } |
| 57 | |
| 58 | static struct xt_match nfacct_mt_reg __read_mostly = { |
| 59 | .name = "nfacct", |
| 60 | .family = NFPROTO_UNSPEC, |
| 61 | .checkentry = nfacct_mt_checkentry, |
| 62 | .match = nfacct_mt, |
| 63 | .destroy = nfacct_mt_destroy, |
| 64 | .matchsize = sizeof(struct xt_nfacct_match_info), |
| 65 | .me = THIS_MODULE, |
| 66 | }; |
| 67 | |
| 68 | static int __init nfacct_mt_init(void) |
| 69 | { |
| 70 | return xt_register_match(&nfacct_mt_reg); |
| 71 | } |
| 72 | |
| 73 | static void __exit nfacct_mt_exit(void) |
| 74 | { |
| 75 | xt_unregister_match(&nfacct_mt_reg); |
| 76 | } |
| 77 | |
| 78 | module_init(nfacct_mt_init); |
| 79 | module_exit(nfacct_mt_exit); |