netfilter: nf_tables: notify internal updates of stateful objects
[linux-2.6-block.git] / net / netfilter / nft_quota.c
CommitLineData
3d2f30a1
PNA
1/*
2 * Copyright (c) 2016 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/atomic.h>
13#include <linux/netlink.h>
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
16#include <net/netfilter/nf_tables.h>
17
18struct nft_quota {
19 u64 quota;
20 bool invert;
795595f6 21 atomic64_t consumed;
3d2f30a1
PNA
22};
23
22609b43 24static inline bool nft_overquota(struct nft_quota *priv,
795595f6 25 const struct sk_buff *skb)
3d2f30a1 26{
795595f6 27 return atomic64_add_return(skb->len, &priv->consumed) >= priv->quota;
3d2f30a1
PNA
28}
29
173705d9
PNA
30static inline void nft_quota_do_eval(struct nft_quota *priv,
31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt)
3d2f30a1 33{
795595f6 34 if (nft_overquota(priv, pkt->skb) ^ priv->invert)
3d2f30a1
PNA
35 regs->verdict.code = NFT_BREAK;
36}
37
38static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = {
39 [NFTA_QUOTA_BYTES] = { .type = NLA_U64 },
40 [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 },
41};
42
173705d9
PNA
43static void nft_quota_obj_eval(struct nft_object *obj,
44 struct nft_regs *regs,
45 const struct nft_pktinfo *pkt)
46{
47 struct nft_quota *priv = nft_obj_data(obj);
48
49 nft_quota_do_eval(priv, regs, pkt);
50}
51
52static int nft_quota_do_init(const struct nlattr * const tb[],
53 struct nft_quota *priv)
3d2f30a1 54{
3d2f30a1
PNA
55 u32 flags = 0;
56 u64 quota;
57
58 if (!tb[NFTA_QUOTA_BYTES])
59 return -EINVAL;
60
61 quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES]));
62 if (quota > S64_MAX)
63 return -EOVERFLOW;
64
65 if (tb[NFTA_QUOTA_FLAGS]) {
66 flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS]));
67 if (flags & ~NFT_QUOTA_F_INV)
68 return -EINVAL;
69 }
70
71 priv->quota = quota;
72 priv->invert = (flags & NFT_QUOTA_F_INV) ? true : false;
795595f6 73 atomic64_set(&priv->consumed, 0);
3d2f30a1
PNA
74
75 return 0;
76}
77
173705d9
PNA
78static int nft_quota_obj_init(const struct nlattr * const tb[],
79 struct nft_object *obj)
80{
81 struct nft_quota *priv = nft_obj_data(obj);
82
83 return nft_quota_do_init(tb, priv);
84}
85
43da04a5
PNA
86static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv,
87 bool reset)
3d2f30a1 88{
3d2f30a1 89 u32 flags = priv->invert ? NFT_QUOTA_F_INV : 0;
795595f6
PNA
90 u64 consumed;
91
43da04a5
PNA
92 if (reset)
93 consumed = atomic64_xchg(&priv->consumed, 0);
94 else
95 consumed = atomic64_read(&priv->consumed);
96
795595f6
PNA
97 /* Since we inconditionally increment consumed quota for each packet
98 * that we see, don't go over the quota boundary in what we send to
99 * userspace.
100 */
101 if (consumed > priv->quota)
102 consumed = priv->quota;
3d2f30a1
PNA
103
104 if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota),
105 NFTA_QUOTA_PAD) ||
795595f6
PNA
106 nla_put_be64(skb, NFTA_QUOTA_CONSUMED, cpu_to_be64(consumed),
107 NFTA_QUOTA_PAD) ||
3d2f30a1
PNA
108 nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags)))
109 goto nla_put_failure;
110 return 0;
111
112nla_put_failure:
113 return -1;
114}
115
43da04a5
PNA
116static int nft_quota_obj_dump(struct sk_buff *skb, struct nft_object *obj,
117 bool reset)
173705d9
PNA
118{
119 struct nft_quota *priv = nft_obj_data(obj);
120
43da04a5 121 return nft_quota_do_dump(skb, priv, reset);
173705d9
PNA
122}
123
124static struct nft_object_type nft_quota_obj __read_mostly = {
125 .type = NFT_OBJECT_QUOTA,
126 .size = sizeof(struct nft_quota),
127 .maxattr = NFTA_QUOTA_MAX,
128 .policy = nft_quota_policy,
129 .init = nft_quota_obj_init,
130 .eval = nft_quota_obj_eval,
131 .dump = nft_quota_obj_dump,
132 .owner = THIS_MODULE,
133};
134
135static void nft_quota_eval(const struct nft_expr *expr,
136 struct nft_regs *regs,
137 const struct nft_pktinfo *pkt)
138{
139 struct nft_quota *priv = nft_expr_priv(expr);
140
141 nft_quota_do_eval(priv, regs, pkt);
142}
143
144static int nft_quota_init(const struct nft_ctx *ctx,
145 const struct nft_expr *expr,
146 const struct nlattr * const tb[])
147{
148 struct nft_quota *priv = nft_expr_priv(expr);
149
150 return nft_quota_do_init(tb, priv);
151}
152
153static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr)
154{
43da04a5 155 struct nft_quota *priv = nft_expr_priv(expr);
173705d9 156
43da04a5 157 return nft_quota_do_dump(skb, priv, false);
173705d9
PNA
158}
159
3d2f30a1
PNA
160static struct nft_expr_type nft_quota_type;
161static const struct nft_expr_ops nft_quota_ops = {
162 .type = &nft_quota_type,
163 .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)),
164 .eval = nft_quota_eval,
165 .init = nft_quota_init,
166 .dump = nft_quota_dump,
167};
168
169static struct nft_expr_type nft_quota_type __read_mostly = {
170 .name = "quota",
171 .ops = &nft_quota_ops,
172 .policy = nft_quota_policy,
173 .maxattr = NFTA_QUOTA_MAX,
174 .flags = NFT_EXPR_STATEFUL,
175 .owner = THIS_MODULE,
176};
177
178static int __init nft_quota_module_init(void)
179{
173705d9
PNA
180 int err;
181
182 err = nft_register_obj(&nft_quota_obj);
183 if (err < 0)
184 return err;
185
186 err = nft_register_expr(&nft_quota_type);
187 if (err < 0)
188 goto err1;
189
190 return 0;
191err1:
192 nft_unregister_obj(&nft_quota_obj);
193 return err;
3d2f30a1
PNA
194}
195
196static void __exit nft_quota_module_exit(void)
197{
173705d9
PNA
198 nft_unregister_expr(&nft_quota_type);
199 nft_unregister_obj(&nft_quota_obj);
3d2f30a1
PNA
200}
201
202module_init(nft_quota_module_init);
203module_exit(nft_quota_module_exit);
204
205MODULE_LICENSE("GPL");
206MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
207MODULE_ALIAS_NFT_EXPR("quota");
173705d9 208MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA);