]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | struct nft_quota { | |
19 | u64 quota; | |
18965317 | 20 | unsigned long flags; |
795595f6 | 21 | atomic64_t consumed; |
3d2f30a1 PNA |
22 | }; |
23 | ||
22609b43 | 24 | static 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 | ||
18965317 PNA |
30 | static inline bool nft_quota_invert(struct nft_quota *priv) |
31 | { | |
32 | return priv->flags & NFT_QUOTA_F_INV; | |
33 | } | |
34 | ||
173705d9 PNA |
35 | static inline void nft_quota_do_eval(struct nft_quota *priv, |
36 | struct nft_regs *regs, | |
37 | const struct nft_pktinfo *pkt) | |
3d2f30a1 | 38 | { |
18965317 | 39 | if (nft_overquota(priv, pkt->skb) ^ nft_quota_invert(priv)) |
3d2f30a1 PNA |
40 | regs->verdict.code = NFT_BREAK; |
41 | } | |
42 | ||
43 | static const struct nla_policy nft_quota_policy[NFTA_QUOTA_MAX + 1] = { | |
44 | [NFTA_QUOTA_BYTES] = { .type = NLA_U64 }, | |
45 | [NFTA_QUOTA_FLAGS] = { .type = NLA_U32 }, | |
73c25fb1 | 46 | [NFTA_QUOTA_CONSUMED] = { .type = NLA_U64 }, |
3d2f30a1 PNA |
47 | }; |
48 | ||
18965317 PNA |
49 | #define NFT_QUOTA_DEPLETED_BIT 1 /* From NFT_QUOTA_F_DEPLETED. */ |
50 | ||
173705d9 PNA |
51 | static void nft_quota_obj_eval(struct nft_object *obj, |
52 | struct nft_regs *regs, | |
53 | const struct nft_pktinfo *pkt) | |
54 | { | |
55 | struct nft_quota *priv = nft_obj_data(obj); | |
18965317 | 56 | bool overquota; |
173705d9 | 57 | |
18965317 PNA |
58 | overquota = nft_overquota(priv, pkt->skb); |
59 | if (overquota ^ nft_quota_invert(priv)) | |
60 | regs->verdict.code = NFT_BREAK; | |
61 | ||
62 | if (overquota && | |
63 | !test_and_set_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags)) | |
64 | nft_obj_notify(nft_net(pkt), obj->table, obj, 0, 0, | |
65 | NFT_MSG_NEWOBJ, nft_pf(pkt), 0, GFP_ATOMIC); | |
173705d9 PNA |
66 | } |
67 | ||
68 | static int nft_quota_do_init(const struct nlattr * const tb[], | |
69 | struct nft_quota *priv) | |
3d2f30a1 | 70 | { |
18965317 | 71 | unsigned long flags = 0; |
73c25fb1 | 72 | u64 quota, consumed = 0; |
3d2f30a1 PNA |
73 | |
74 | if (!tb[NFTA_QUOTA_BYTES]) | |
75 | return -EINVAL; | |
76 | ||
77 | quota = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_BYTES])); | |
78 | if (quota > S64_MAX) | |
79 | return -EOVERFLOW; | |
80 | ||
73c25fb1 PNA |
81 | if (tb[NFTA_QUOTA_CONSUMED]) { |
82 | consumed = be64_to_cpu(nla_get_be64(tb[NFTA_QUOTA_CONSUMED])); | |
83 | if (consumed > quota) | |
84 | return -EINVAL; | |
85 | } | |
86 | ||
3d2f30a1 PNA |
87 | if (tb[NFTA_QUOTA_FLAGS]) { |
88 | flags = ntohl(nla_get_be32(tb[NFTA_QUOTA_FLAGS])); | |
89 | if (flags & ~NFT_QUOTA_F_INV) | |
90 | return -EINVAL; | |
18965317 PNA |
91 | if (flags & NFT_QUOTA_F_DEPLETED) |
92 | return -EOPNOTSUPP; | |
3d2f30a1 PNA |
93 | } |
94 | ||
95 | priv->quota = quota; | |
18965317 | 96 | priv->flags = flags; |
73c25fb1 | 97 | atomic64_set(&priv->consumed, consumed); |
3d2f30a1 PNA |
98 | |
99 | return 0; | |
100 | } | |
101 | ||
173705d9 PNA |
102 | static int nft_quota_obj_init(const struct nlattr * const tb[], |
103 | struct nft_object *obj) | |
104 | { | |
105 | struct nft_quota *priv = nft_obj_data(obj); | |
106 | ||
107 | return nft_quota_do_init(tb, priv); | |
108 | } | |
109 | ||
43da04a5 PNA |
110 | static int nft_quota_do_dump(struct sk_buff *skb, struct nft_quota *priv, |
111 | bool reset) | |
3d2f30a1 | 112 | { |
8010d7fe | 113 | u64 consumed, consumed_cap; |
18965317 | 114 | u32 flags = priv->flags; |
43da04a5 | 115 | |
795595f6 PNA |
116 | /* Since we inconditionally increment consumed quota for each packet |
117 | * that we see, don't go over the quota boundary in what we send to | |
118 | * userspace. | |
119 | */ | |
8010d7fe PNA |
120 | consumed = atomic64_read(&priv->consumed); |
121 | if (consumed >= priv->quota) { | |
122 | consumed_cap = priv->quota; | |
123 | flags |= NFT_QUOTA_F_DEPLETED; | |
124 | } else { | |
125 | consumed_cap = consumed; | |
126 | } | |
3d2f30a1 PNA |
127 | |
128 | if (nla_put_be64(skb, NFTA_QUOTA_BYTES, cpu_to_be64(priv->quota), | |
129 | NFTA_QUOTA_PAD) || | |
8010d7fe | 130 | nla_put_be64(skb, NFTA_QUOTA_CONSUMED, cpu_to_be64(consumed_cap), |
795595f6 | 131 | NFTA_QUOTA_PAD) || |
3d2f30a1 PNA |
132 | nla_put_be32(skb, NFTA_QUOTA_FLAGS, htonl(flags))) |
133 | goto nla_put_failure; | |
8010d7fe PNA |
134 | |
135 | if (reset) { | |
136 | atomic64_sub(consumed, &priv->consumed); | |
137 | clear_bit(NFT_QUOTA_DEPLETED_BIT, &priv->flags); | |
138 | } | |
3d2f30a1 PNA |
139 | return 0; |
140 | ||
141 | nla_put_failure: | |
142 | return -1; | |
143 | } | |
144 | ||
43da04a5 PNA |
145 | static int nft_quota_obj_dump(struct sk_buff *skb, struct nft_object *obj, |
146 | bool reset) | |
173705d9 PNA |
147 | { |
148 | struct nft_quota *priv = nft_obj_data(obj); | |
149 | ||
43da04a5 | 150 | return nft_quota_do_dump(skb, priv, reset); |
173705d9 PNA |
151 | } |
152 | ||
153 | static struct nft_object_type nft_quota_obj __read_mostly = { | |
154 | .type = NFT_OBJECT_QUOTA, | |
155 | .size = sizeof(struct nft_quota), | |
156 | .maxattr = NFTA_QUOTA_MAX, | |
157 | .policy = nft_quota_policy, | |
158 | .init = nft_quota_obj_init, | |
159 | .eval = nft_quota_obj_eval, | |
160 | .dump = nft_quota_obj_dump, | |
161 | .owner = THIS_MODULE, | |
162 | }; | |
163 | ||
164 | static void nft_quota_eval(const struct nft_expr *expr, | |
165 | struct nft_regs *regs, | |
166 | const struct nft_pktinfo *pkt) | |
167 | { | |
168 | struct nft_quota *priv = nft_expr_priv(expr); | |
169 | ||
170 | nft_quota_do_eval(priv, regs, pkt); | |
171 | } | |
172 | ||
173 | static int nft_quota_init(const struct nft_ctx *ctx, | |
174 | const struct nft_expr *expr, | |
175 | const struct nlattr * const tb[]) | |
176 | { | |
177 | struct nft_quota *priv = nft_expr_priv(expr); | |
178 | ||
179 | return nft_quota_do_init(tb, priv); | |
180 | } | |
181 | ||
182 | static int nft_quota_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
183 | { | |
43da04a5 | 184 | struct nft_quota *priv = nft_expr_priv(expr); |
173705d9 | 185 | |
43da04a5 | 186 | return nft_quota_do_dump(skb, priv, false); |
173705d9 PNA |
187 | } |
188 | ||
3d2f30a1 PNA |
189 | static struct nft_expr_type nft_quota_type; |
190 | static const struct nft_expr_ops nft_quota_ops = { | |
191 | .type = &nft_quota_type, | |
192 | .size = NFT_EXPR_SIZE(sizeof(struct nft_quota)), | |
193 | .eval = nft_quota_eval, | |
194 | .init = nft_quota_init, | |
195 | .dump = nft_quota_dump, | |
196 | }; | |
197 | ||
198 | static struct nft_expr_type nft_quota_type __read_mostly = { | |
199 | .name = "quota", | |
200 | .ops = &nft_quota_ops, | |
201 | .policy = nft_quota_policy, | |
202 | .maxattr = NFTA_QUOTA_MAX, | |
203 | .flags = NFT_EXPR_STATEFUL, | |
204 | .owner = THIS_MODULE, | |
205 | }; | |
206 | ||
207 | static int __init nft_quota_module_init(void) | |
208 | { | |
173705d9 PNA |
209 | int err; |
210 | ||
211 | err = nft_register_obj(&nft_quota_obj); | |
212 | if (err < 0) | |
213 | return err; | |
214 | ||
215 | err = nft_register_expr(&nft_quota_type); | |
216 | if (err < 0) | |
217 | goto err1; | |
218 | ||
219 | return 0; | |
220 | err1: | |
221 | nft_unregister_obj(&nft_quota_obj); | |
222 | return err; | |
3d2f30a1 PNA |
223 | } |
224 | ||
225 | static void __exit nft_quota_module_exit(void) | |
226 | { | |
173705d9 PNA |
227 | nft_unregister_expr(&nft_quota_type); |
228 | nft_unregister_obj(&nft_quota_obj); | |
3d2f30a1 PNA |
229 | } |
230 | ||
231 | module_init(nft_quota_module_init); | |
232 | module_exit(nft_quota_module_exit); | |
233 | ||
234 | MODULE_LICENSE("GPL"); | |
235 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
236 | MODULE_ALIAS_NFT_EXPR("quota"); | |
173705d9 | 237 | MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_QUOTA); |