]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nft_counter.c
Merge tag 'powerpc-5.11-6' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nft_counter.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
96518518 2/*
ef1f7df9 3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518 4 *
96518518
PM
5 * Development of this code funded by Astaro AG (http://www.astaro.com/)
6 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/seqlock.h>
12#include <linux/netlink.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15#include <net/netfilter/nf_tables.h>
16
17struct nft_counter {
d84701ec
PN
18 s64 bytes;
19 s64 packets;
0c45e769
PNA
20};
21
22struct nft_counter_percpu_priv {
d84701ec 23 struct nft_counter __percpu *counter;
0c45e769
PNA
24};
25
d84701ec
PN
26static DEFINE_PER_CPU(seqcount_t, nft_counter_seq);
27
b1ce0ced
PNA
28static inline void nft_counter_do_eval(struct nft_counter_percpu_priv *priv,
29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt)
96518518 31{
d84701ec
PN
32 struct nft_counter *this_cpu;
33 seqcount_t *myseq;
0c45e769
PNA
34
35 local_bh_disable();
36 this_cpu = this_cpu_ptr(priv->counter);
d84701ec
PN
37 myseq = this_cpu_ptr(&nft_counter_seq);
38
39 write_seqcount_begin(myseq);
40
41 this_cpu->bytes += pkt->skb->len;
42 this_cpu->packets++;
43
44 write_seqcount_end(myseq);
0c45e769 45 local_bh_enable();
96518518
PM
46}
47
b1ce0ced
PNA
48static inline void nft_counter_obj_eval(struct nft_object *obj,
49 struct nft_regs *regs,
50 const struct nft_pktinfo *pkt)
51{
52 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
53
54 nft_counter_do_eval(priv, regs, pkt);
55}
56
57static int nft_counter_do_init(const struct nlattr * const tb[],
58 struct nft_counter_percpu_priv *priv)
59{
d84701ec
PN
60 struct nft_counter __percpu *cpu_stats;
61 struct nft_counter *this_cpu;
b1ce0ced 62
d84701ec 63 cpu_stats = alloc_percpu(struct nft_counter);
b1ce0ced
PNA
64 if (cpu_stats == NULL)
65 return -ENOMEM;
66
67 preempt_disable();
68 this_cpu = this_cpu_ptr(cpu_stats);
69 if (tb[NFTA_COUNTER_PACKETS]) {
d84701ec 70 this_cpu->packets =
b1ce0ced
PNA
71 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
72 }
73 if (tb[NFTA_COUNTER_BYTES]) {
d84701ec 74 this_cpu->bytes =
b1ce0ced
PNA
75 be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
76 }
77 preempt_enable();
78 priv->counter = cpu_stats;
79 return 0;
80}
81
84fba055
FW
82static int nft_counter_obj_init(const struct nft_ctx *ctx,
83 const struct nlattr * const tb[],
b1ce0ced
PNA
84 struct nft_object *obj)
85{
86 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
87
88 return nft_counter_do_init(tb, priv);
89}
90
91static void nft_counter_do_destroy(struct nft_counter_percpu_priv *priv)
92{
93 free_percpu(priv->counter);
94}
95
00bfb320
PNA
96static void nft_counter_obj_destroy(const struct nft_ctx *ctx,
97 struct nft_object *obj)
b1ce0ced
PNA
98{
99 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
100
101 nft_counter_do_destroy(priv);
102}
103
dd03b1ad 104static void nft_counter_reset(struct nft_counter_percpu_priv *priv,
086f3321 105 struct nft_counter *total)
96518518 106{
d84701ec 107 struct nft_counter *this_cpu;
0c45e769 108
d84701ec
PN
109 local_bh_disable();
110 this_cpu = this_cpu_ptr(priv->counter);
111 this_cpu->packets -= total->packets;
112 this_cpu->bytes -= total->bytes;
113 local_bh_enable();
43da04a5
PNA
114}
115
d84701ec 116static void nft_counter_fetch(struct nft_counter_percpu_priv *priv,
43da04a5
PNA
117 struct nft_counter *total)
118{
d84701ec
PN
119 struct nft_counter *this_cpu;
120 const seqcount_t *myseq;
43da04a5
PNA
121 u64 bytes, packets;
122 unsigned int seq;
123 int cpu;
124
125 memset(total, 0, sizeof(*total));
126 for_each_possible_cpu(cpu) {
d84701ec
PN
127 myseq = per_cpu_ptr(&nft_counter_seq, cpu);
128 this_cpu = per_cpu_ptr(priv->counter, cpu);
43da04a5 129 do {
d84701ec
PN
130 seq = read_seqcount_begin(myseq);
131 bytes = this_cpu->bytes;
132 packets = this_cpu->packets;
133 } while (read_seqcount_retry(myseq, seq));
43da04a5 134
d84701ec
PN
135 total->bytes += bytes;
136 total->packets += packets;
43da04a5
PNA
137 }
138}
139
b1ce0ced 140static int nft_counter_do_dump(struct sk_buff *skb,
d84701ec 141 struct nft_counter_percpu_priv *priv,
43da04a5 142 bool reset)
086f3321 143{
086f3321
PNA
144 struct nft_counter total;
145
d84701ec 146 nft_counter_fetch(priv, &total);
0c45e769 147
b46f6ded
ND
148 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes),
149 NFTA_COUNTER_PAD) ||
150 nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.packets),
151 NFTA_COUNTER_PAD))
96518518 152 goto nla_put_failure;
d84701ec
PN
153
154 if (reset)
155 nft_counter_reset(priv, &total);
156
96518518
PM
157 return 0;
158
159nla_put_failure:
160 return -1;
161}
162
b1ce0ced 163static int nft_counter_obj_dump(struct sk_buff *skb,
43da04a5 164 struct nft_object *obj, bool reset)
b1ce0ced 165{
43da04a5 166 struct nft_counter_percpu_priv *priv = nft_obj_data(obj);
b1ce0ced 167
43da04a5 168 return nft_counter_do_dump(skb, priv, reset);
b1ce0ced
PNA
169}
170
96518518
PM
171static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
172 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
173 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
174};
175
dfc46034
PBG
176static struct nft_object_type nft_counter_obj_type;
177static const struct nft_object_ops nft_counter_obj_ops = {
178 .type = &nft_counter_obj_type,
b1ce0ced 179 .size = sizeof(struct nft_counter_percpu_priv),
b1ce0ced
PNA
180 .eval = nft_counter_obj_eval,
181 .init = nft_counter_obj_init,
182 .destroy = nft_counter_obj_destroy,
183 .dump = nft_counter_obj_dump,
dfc46034
PBG
184};
185
186static struct nft_object_type nft_counter_obj_type __read_mostly = {
187 .type = NFT_OBJECT_COUNTER,
188 .ops = &nft_counter_obj_ops,
189 .maxattr = NFTA_COUNTER_MAX,
190 .policy = nft_counter_policy,
b1ce0ced
PNA
191 .owner = THIS_MODULE,
192};
193
194static void nft_counter_eval(const struct nft_expr *expr,
195 struct nft_regs *regs,
196 const struct nft_pktinfo *pkt)
197{
198 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
199
200 nft_counter_do_eval(priv, regs, pkt);
201}
202
203static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
204{
d84701ec 205 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
b1ce0ced 206
43da04a5 207 return nft_counter_do_dump(skb, priv, false);
b1ce0ced
PNA
208}
209
96518518
PM
210static int nft_counter_init(const struct nft_ctx *ctx,
211 const struct nft_expr *expr,
212 const struct nlattr * const tb[])
213{
0c45e769 214 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
0c45e769 215
b1ce0ced 216 return nft_counter_do_init(tb, priv);
0c45e769 217}
96518518 218
0c45e769
PNA
219static void nft_counter_destroy(const struct nft_ctx *ctx,
220 const struct nft_expr *expr)
221{
222 struct nft_counter_percpu_priv *priv = nft_expr_priv(expr);
96518518 223
b1ce0ced 224 nft_counter_do_destroy(priv);
96518518
PM
225}
226
086f3321
PNA
227static int nft_counter_clone(struct nft_expr *dst, const struct nft_expr *src)
228{
229 struct nft_counter_percpu_priv *priv = nft_expr_priv(src);
230 struct nft_counter_percpu_priv *priv_clone = nft_expr_priv(dst);
d84701ec
PN
231 struct nft_counter __percpu *cpu_stats;
232 struct nft_counter *this_cpu;
086f3321
PNA
233 struct nft_counter total;
234
d84701ec 235 nft_counter_fetch(priv, &total);
086f3321 236
d84701ec 237 cpu_stats = alloc_percpu_gfp(struct nft_counter, GFP_ATOMIC);
086f3321 238 if (cpu_stats == NULL)
5cc6ce9f 239 return -ENOMEM;
086f3321
PNA
240
241 preempt_disable();
242 this_cpu = this_cpu_ptr(cpu_stats);
d84701ec
PN
243 this_cpu->packets = total.packets;
244 this_cpu->bytes = total.bytes;
086f3321
PNA
245 preempt_enable();
246
247 priv_clone->counter = cpu_stats;
248 return 0;
249}
250
ef1f7df9
PM
251static struct nft_expr_type nft_counter_type;
252static const struct nft_expr_ops nft_counter_ops = {
253 .type = &nft_counter_type,
0c45e769 254 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter_percpu_priv)),
96518518
PM
255 .eval = nft_counter_eval,
256 .init = nft_counter_init,
0c45e769 257 .destroy = nft_counter_destroy,
371ebcbb 258 .destroy_clone = nft_counter_destroy,
96518518 259 .dump = nft_counter_dump,
086f3321 260 .clone = nft_counter_clone,
96518518
PM
261};
262
ef1f7df9
PM
263static struct nft_expr_type nft_counter_type __read_mostly = {
264 .name = "counter",
265 .ops = &nft_counter_ops,
266 .policy = nft_counter_policy,
267 .maxattr = NFTA_COUNTER_MAX,
151d799a 268 .flags = NFT_EXPR_STATEFUL,
ef1f7df9
PM
269 .owner = THIS_MODULE,
270};
271
96518518
PM
272static int __init nft_counter_module_init(void)
273{
d84701ec
PN
274 int cpu, err;
275
276 for_each_possible_cpu(cpu)
277 seqcount_init(per_cpu_ptr(&nft_counter_seq, cpu));
b1ce0ced 278
dfc46034 279 err = nft_register_obj(&nft_counter_obj_type);
b1ce0ced
PNA
280 if (err < 0)
281 return err;
282
283 err = nft_register_expr(&nft_counter_type);
284 if (err < 0)
285 goto err1;
286
287 return 0;
288err1:
dfc46034 289 nft_unregister_obj(&nft_counter_obj_type);
b1ce0ced 290 return err;
96518518
PM
291}
292
293static void __exit nft_counter_module_exit(void)
294{
ef1f7df9 295 nft_unregister_expr(&nft_counter_type);
dfc46034 296 nft_unregister_obj(&nft_counter_obj_type);
96518518
PM
297}
298
299module_init(nft_counter_module_init);
300module_exit(nft_counter_module_exit);
301
302MODULE_LICENSE("GPL");
303MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
304MODULE_ALIAS_NFT_EXPR("counter");
b1ce0ced 305MODULE_ALIAS_NFT_OBJ(NFT_OBJECT_COUNTER);
4cacc395 306MODULE_DESCRIPTION("nftables counter rule support");