blob: 3241fee9f2a19585fb180f552a104299e0918dc6 [file] [log] [blame]
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +01001/*
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 */
Florian Westphalb2606642018-02-09 15:52:07 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010011#include <linux/module.h>
12#include <linux/skbuff.h>
13
14#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter/nfnetlink_acct.h>
16#include <linux/netfilter/xt_nfacct.h>
17
18MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
19MODULE_DESCRIPTION("Xtables: match for the extended accounting infrastructure");
20MODULE_LICENSE("GPL");
21MODULE_ALIAS("ipt_nfacct");
22MODULE_ALIAS("ip6t_nfacct");
23
24static bool nfacct_mt(const struct sk_buff *skb, struct xt_action_param *par)
25{
Mathieu Poirier683399e2014-04-20 18:57:36 -060026 int overquota;
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010027 const struct xt_nfacct_match_info *info = par->targinfo;
28
29 nfnl_acct_update(skb, info->nfacct);
30
Taehee Yoocceae762018-02-11 19:17:20 +090031 overquota = nfnl_acct_overquota(xt_net(par), info->nfacct);
Mathieu Poirier683399e2014-04-20 18:57:36 -060032
33 return overquota == NFACCT_UNDERQUOTA ? false : true;
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010034}
35
36static int
37nfacct_mt_checkentry(const struct xt_mtchk_param *par)
38{
39 struct xt_nfacct_match_info *info = par->matchinfo;
40 struct nf_acct *nfacct;
41
Andreas Schultz3499abb2015-08-05 17:51:45 +020042 nfacct = nfnl_acct_find_get(par->net, info->name);
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010043 if (nfacct == NULL) {
Florian Westphalb2606642018-02-09 15:52:07 +010044 pr_info_ratelimited("accounting object `%s' does not exists\n",
45 info->name);
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010046 return -ENOENT;
47 }
48 info->nfacct = nfacct;
49 return 0;
50}
51
52static void
53nfacct_mt_destroy(const struct xt_mtdtor_param *par)
54{
55 const struct xt_nfacct_match_info *info = par->matchinfo;
56
57 nfnl_acct_put(info->nfacct);
58}
59
Juliana Rodrigueiro4e5fbcb2019-08-16 17:02:22 +020060static struct xt_match nfacct_mt_reg[] __read_mostly = {
61 {
62 .name = "nfacct",
63 .revision = 0,
64 .family = NFPROTO_UNSPEC,
65 .checkentry = nfacct_mt_checkentry,
66 .match = nfacct_mt,
67 .destroy = nfacct_mt_destroy,
68 .matchsize = sizeof(struct xt_nfacct_match_info),
69 .usersize = offsetof(struct xt_nfacct_match_info, nfacct),
70 .me = THIS_MODULE,
71 },
72 {
73 .name = "nfacct",
74 .revision = 1,
75 .family = NFPROTO_UNSPEC,
76 .checkentry = nfacct_mt_checkentry,
77 .match = nfacct_mt,
78 .destroy = nfacct_mt_destroy,
79 .matchsize = sizeof(struct xt_nfacct_match_info_v1),
80 .usersize = offsetof(struct xt_nfacct_match_info_v1, nfacct),
81 .me = THIS_MODULE,
82 },
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010083};
84
85static int __init nfacct_mt_init(void)
86{
Juliana Rodrigueiro4e5fbcb2019-08-16 17:02:22 +020087 return xt_register_matches(nfacct_mt_reg, ARRAY_SIZE(nfacct_mt_reg));
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010088}
89
90static void __exit nfacct_mt_exit(void)
91{
Juliana Rodrigueiro4e5fbcb2019-08-16 17:02:22 +020092 xt_unregister_matches(nfacct_mt_reg, ARRAY_SIZE(nfacct_mt_reg));
Pablo Neira Ayusoceb98d02011-12-23 14:28:59 +010093}
94
95module_init(nfacct_mt_init);
96module_exit(nfacct_mt_exit);