perf probe: Add --bootconfig to output definition in bootconfig format
[linux-2.6-microblaze.git] / net / netfilter / nfnetlink_acct.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
4  * (C) 2011 Intra2net AG <https://www.intra2net.com>
5  */
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/skbuff.h>
10 #include <linux/atomic.h>
11 #include <linux/refcount.h>
12 #include <linux/netlink.h>
13 #include <linux/rculist.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <net/netlink.h>
18 #include <net/sock.h>
19 #include <net/netns/generic.h>
20
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/nfnetlink.h>
23 #include <linux/netfilter/nfnetlink_acct.h>
24
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
27 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
28
29 struct nf_acct {
30         atomic64_t              pkts;
31         atomic64_t              bytes;
32         unsigned long           flags;
33         struct list_head        head;
34         refcount_t              refcnt;
35         char                    name[NFACCT_NAME_MAX];
36         struct rcu_head         rcu_head;
37         char                    data[];
38 };
39
40 struct nfacct_filter {
41         u32 value;
42         u32 mask;
43 };
44
45 struct nfnl_acct_net {
46         struct list_head        nfnl_acct_list;
47 };
48
49 static unsigned int nfnl_acct_net_id __read_mostly;
50
51 static inline struct nfnl_acct_net *nfnl_acct_pernet(struct net *net)
52 {
53         return net_generic(net, nfnl_acct_net_id);
54 }
55
56 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
57 #define NFACCT_OVERQUOTA_BIT    2       /* NFACCT_F_OVERQUOTA */
58
59 static int nfnl_acct_new(struct sk_buff *skb, const struct nfnl_info *info,
60                          const struct nlattr * const tb[])
61 {
62         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(info->net);
63         struct nf_acct *nfacct, *matching = NULL;
64         unsigned int size = 0;
65         char *acct_name;
66         u32 flags = 0;
67
68         if (!tb[NFACCT_NAME])
69                 return -EINVAL;
70
71         acct_name = nla_data(tb[NFACCT_NAME]);
72         if (strlen(acct_name) == 0)
73                 return -EINVAL;
74
75         list_for_each_entry(nfacct, &nfnl_acct_net->nfnl_acct_list, head) {
76                 if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0)
77                         continue;
78
79                 if (info->nlh->nlmsg_flags & NLM_F_EXCL)
80                         return -EEXIST;
81
82                 matching = nfacct;
83                 break;
84         }
85
86         if (matching) {
87                 if (info->nlh->nlmsg_flags & NLM_F_REPLACE) {
88                         /* reset counters if you request a replacement. */
89                         atomic64_set(&matching->pkts, 0);
90                         atomic64_set(&matching->bytes, 0);
91                         smp_mb__before_atomic();
92                         /* reset overquota flag if quota is enabled. */
93                         if ((matching->flags & NFACCT_F_QUOTA))
94                                 clear_bit(NFACCT_OVERQUOTA_BIT,
95                                           &matching->flags);
96                         return 0;
97                 }
98                 return -EBUSY;
99         }
100
101         if (tb[NFACCT_FLAGS]) {
102                 flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
103                 if (flags & ~NFACCT_F_QUOTA)
104                         return -EOPNOTSUPP;
105                 if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
106                         return -EINVAL;
107                 if (flags & NFACCT_F_OVERQUOTA)
108                         return -EINVAL;
109                 if ((flags & NFACCT_F_QUOTA) && !tb[NFACCT_QUOTA])
110                         return -EINVAL;
111
112                 size += sizeof(u64);
113         }
114
115         nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
116         if (nfacct == NULL)
117                 return -ENOMEM;
118
119         if (flags & NFACCT_F_QUOTA) {
120                 u64 *quota = (u64 *)nfacct->data;
121
122                 *quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
123                 nfacct->flags = flags;
124         }
125
126         nla_strscpy(nfacct->name, tb[NFACCT_NAME], NFACCT_NAME_MAX);
127
128         if (tb[NFACCT_BYTES]) {
129                 atomic64_set(&nfacct->bytes,
130                              be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES])));
131         }
132         if (tb[NFACCT_PKTS]) {
133                 atomic64_set(&nfacct->pkts,
134                              be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
135         }
136         refcount_set(&nfacct->refcnt, 1);
137         list_add_tail_rcu(&nfacct->head, &nfnl_acct_net->nfnl_acct_list);
138         return 0;
139 }
140
141 static int
142 nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
143                    int event, struct nf_acct *acct)
144 {
145         struct nlmsghdr *nlh;
146         unsigned int flags = portid ? NLM_F_MULTI : 0;
147         u64 pkts, bytes;
148         u32 old_flags;
149
150         event = nfnl_msg_type(NFNL_SUBSYS_ACCT, event);
151         nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
152                            NFNETLINK_V0, 0);
153         if (!nlh)
154                 goto nlmsg_failure;
155
156         if (nla_put_string(skb, NFACCT_NAME, acct->name))
157                 goto nla_put_failure;
158
159         old_flags = acct->flags;
160         if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
161                 pkts = atomic64_xchg(&acct->pkts, 0);
162                 bytes = atomic64_xchg(&acct->bytes, 0);
163                 smp_mb__before_atomic();
164                 if (acct->flags & NFACCT_F_QUOTA)
165                         clear_bit(NFACCT_OVERQUOTA_BIT, &acct->flags);
166         } else {
167                 pkts = atomic64_read(&acct->pkts);
168                 bytes = atomic64_read(&acct->bytes);
169         }
170         if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts),
171                          NFACCT_PAD) ||
172             nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes),
173                          NFACCT_PAD) ||
174             nla_put_be32(skb, NFACCT_USE, htonl(refcount_read(&acct->refcnt))))
175                 goto nla_put_failure;
176         if (acct->flags & NFACCT_F_QUOTA) {
177                 u64 *quota = (u64 *)acct->data;
178
179                 if (nla_put_be32(skb, NFACCT_FLAGS, htonl(old_flags)) ||
180                     nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota),
181                                  NFACCT_PAD))
182                         goto nla_put_failure;
183         }
184         nlmsg_end(skb, nlh);
185         return skb->len;
186
187 nlmsg_failure:
188 nla_put_failure:
189         nlmsg_cancel(skb, nlh);
190         return -1;
191 }
192
193 static int
194 nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
195 {
196         struct net *net = sock_net(skb->sk);
197         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(net);
198         struct nf_acct *cur, *last;
199         const struct nfacct_filter *filter = cb->data;
200
201         if (cb->args[2])
202                 return 0;
203
204         last = (struct nf_acct *)cb->args[1];
205         if (cb->args[1])
206                 cb->args[1] = 0;
207
208         rcu_read_lock();
209         list_for_each_entry_rcu(cur, &nfnl_acct_net->nfnl_acct_list, head) {
210                 if (last) {
211                         if (cur != last)
212                                 continue;
213
214                         last = NULL;
215                 }
216
217                 if (filter && (cur->flags & filter->mask) != filter->value)
218                         continue;
219
220                 if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
221                                        cb->nlh->nlmsg_seq,
222                                        NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
223                                        NFNL_MSG_ACCT_NEW, cur) < 0) {
224                         cb->args[1] = (unsigned long)cur;
225                         break;
226                 }
227         }
228         if (!cb->args[1])
229                 cb->args[2] = 1;
230         rcu_read_unlock();
231         return skb->len;
232 }
233
234 static int nfnl_acct_done(struct netlink_callback *cb)
235 {
236         kfree(cb->data);
237         return 0;
238 }
239
240 static const struct nla_policy filter_policy[NFACCT_FILTER_MAX + 1] = {
241         [NFACCT_FILTER_MASK]    = { .type = NLA_U32 },
242         [NFACCT_FILTER_VALUE]   = { .type = NLA_U32 },
243 };
244
245 static int nfnl_acct_start(struct netlink_callback *cb)
246 {
247         const struct nlattr *const attr = cb->data;
248         struct nlattr *tb[NFACCT_FILTER_MAX + 1];
249         struct nfacct_filter *filter;
250         int err;
251
252         if (!attr)
253                 return 0;
254
255         err = nla_parse_nested_deprecated(tb, NFACCT_FILTER_MAX, attr,
256                                           filter_policy, NULL);
257         if (err < 0)
258                 return err;
259
260         if (!tb[NFACCT_FILTER_MASK] || !tb[NFACCT_FILTER_VALUE])
261                 return -EINVAL;
262
263         filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL);
264         if (!filter)
265                 return -ENOMEM;
266
267         filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK]));
268         filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE]));
269         cb->data = filter;
270
271         return 0;
272 }
273
274 static int nfnl_acct_get(struct sk_buff *skb, const struct nfnl_info *info,
275                          const struct nlattr * const tb[])
276 {
277         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(info->net);
278         int ret = -ENOENT;
279         struct nf_acct *cur;
280         char *acct_name;
281
282         if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
283                 struct netlink_dump_control c = {
284                         .dump = nfnl_acct_dump,
285                         .start = nfnl_acct_start,
286                         .done = nfnl_acct_done,
287                         .data = (void *)tb[NFACCT_FILTER],
288                 };
289
290                 return netlink_dump_start(info->sk, skb, info->nlh, &c);
291         }
292
293         if (!tb[NFACCT_NAME])
294                 return -EINVAL;
295         acct_name = nla_data(tb[NFACCT_NAME]);
296
297         list_for_each_entry(cur, &nfnl_acct_net->nfnl_acct_list, head) {
298                 struct sk_buff *skb2;
299
300                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
301                         continue;
302
303                 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
304                 if (skb2 == NULL) {
305                         ret = -ENOMEM;
306                         break;
307                 }
308
309                 ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
310                                           info->nlh->nlmsg_seq,
311                                           NFNL_MSG_TYPE(info->nlh->nlmsg_type),
312                                           NFNL_MSG_ACCT_NEW, cur);
313                 if (ret <= 0) {
314                         kfree_skb(skb2);
315                         break;
316                 }
317                 ret = netlink_unicast(info->sk, skb2, NETLINK_CB(skb).portid,
318                                       MSG_DONTWAIT);
319                 if (ret > 0)
320                         ret = 0;
321
322                 /* this avoids a loop in nfnetlink. */
323                 return ret == -EAGAIN ? -ENOBUFS : ret;
324         }
325         return ret;
326 }
327
328 /* try to delete object, fail if it is still in use. */
329 static int nfnl_acct_try_del(struct nf_acct *cur)
330 {
331         int ret = 0;
332
333         /* We want to avoid races with nfnl_acct_put. So only when the current
334          * refcnt is 1, we decrease it to 0.
335          */
336         if (refcount_dec_if_one(&cur->refcnt)) {
337                 /* We are protected by nfnl mutex. */
338                 list_del_rcu(&cur->head);
339                 kfree_rcu(cur, rcu_head);
340         } else {
341                 ret = -EBUSY;
342         }
343         return ret;
344 }
345
346 static int nfnl_acct_del(struct sk_buff *skb, const struct nfnl_info *info,
347                          const struct nlattr * const tb[])
348 {
349         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(info->net);
350         struct nf_acct *cur, *tmp;
351         int ret = -ENOENT;
352         char *acct_name;
353
354         if (!tb[NFACCT_NAME]) {
355                 list_for_each_entry_safe(cur, tmp, &nfnl_acct_net->nfnl_acct_list, head)
356                         nfnl_acct_try_del(cur);
357
358                 return 0;
359         }
360         acct_name = nla_data(tb[NFACCT_NAME]);
361
362         list_for_each_entry(cur, &nfnl_acct_net->nfnl_acct_list, head) {
363                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
364                         continue;
365
366                 ret = nfnl_acct_try_del(cur);
367                 if (ret < 0)
368                         return ret;
369
370                 break;
371         }
372         return ret;
373 }
374
375 static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
376         [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
377         [NFACCT_BYTES] = { .type = NLA_U64 },
378         [NFACCT_PKTS] = { .type = NLA_U64 },
379         [NFACCT_FLAGS] = { .type = NLA_U32 },
380         [NFACCT_QUOTA] = { .type = NLA_U64 },
381         [NFACCT_FILTER] = {.type = NLA_NESTED },
382 };
383
384 static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
385         [NFNL_MSG_ACCT_NEW] = {
386                 .call           = nfnl_acct_new,
387                 .type           = NFNL_CB_MUTEX,
388                 .attr_count     = NFACCT_MAX,
389                 .policy         = nfnl_acct_policy
390         },
391         [NFNL_MSG_ACCT_GET] = {
392                 .call           = nfnl_acct_get,
393                 .type           = NFNL_CB_MUTEX,
394                 .attr_count     = NFACCT_MAX,
395                 .policy         = nfnl_acct_policy
396         },
397         [NFNL_MSG_ACCT_GET_CTRZERO] = {
398                 .call           = nfnl_acct_get,
399                 .type           = NFNL_CB_MUTEX,
400                 .attr_count     = NFACCT_MAX,
401                 .policy         = nfnl_acct_policy
402         },
403         [NFNL_MSG_ACCT_DEL] = {
404                 .call           = nfnl_acct_del,
405                 .type           = NFNL_CB_MUTEX,
406                 .attr_count     = NFACCT_MAX,
407                 .policy         = nfnl_acct_policy
408         },
409 };
410
411 static const struct nfnetlink_subsystem nfnl_acct_subsys = {
412         .name                           = "acct",
413         .subsys_id                      = NFNL_SUBSYS_ACCT,
414         .cb_count                       = NFNL_MSG_ACCT_MAX,
415         .cb                             = nfnl_acct_cb,
416 };
417
418 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
419
420 struct nf_acct *nfnl_acct_find_get(struct net *net, const char *acct_name)
421 {
422         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(net);
423         struct nf_acct *cur, *acct = NULL;
424
425         rcu_read_lock();
426         list_for_each_entry_rcu(cur, &nfnl_acct_net->nfnl_acct_list, head) {
427                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
428                         continue;
429
430                 if (!try_module_get(THIS_MODULE))
431                         goto err;
432
433                 if (!refcount_inc_not_zero(&cur->refcnt)) {
434                         module_put(THIS_MODULE);
435                         goto err;
436                 }
437
438                 acct = cur;
439                 break;
440         }
441 err:
442         rcu_read_unlock();
443         return acct;
444 }
445 EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
446
447 void nfnl_acct_put(struct nf_acct *acct)
448 {
449         if (refcount_dec_and_test(&acct->refcnt))
450                 kfree_rcu(acct, rcu_head);
451
452         module_put(THIS_MODULE);
453 }
454 EXPORT_SYMBOL_GPL(nfnl_acct_put);
455
456 void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
457 {
458         atomic64_inc(&nfacct->pkts);
459         atomic64_add(skb->len, &nfacct->bytes);
460 }
461 EXPORT_SYMBOL_GPL(nfnl_acct_update);
462
463 static void nfnl_overquota_report(struct net *net, struct nf_acct *nfacct)
464 {
465         int ret;
466         struct sk_buff *skb;
467
468         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
469         if (skb == NULL)
470                 return;
471
472         ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
473                                   nfacct);
474         if (ret <= 0) {
475                 kfree_skb(skb);
476                 return;
477         }
478         nfnetlink_broadcast(net, skb, 0, NFNLGRP_ACCT_QUOTA, GFP_ATOMIC);
479 }
480
481 int nfnl_acct_overquota(struct net *net, struct nf_acct *nfacct)
482 {
483         u64 now;
484         u64 *quota;
485         int ret = NFACCT_UNDERQUOTA;
486
487         /* no place here if we don't have a quota */
488         if (!(nfacct->flags & NFACCT_F_QUOTA))
489                 return NFACCT_NO_QUOTA;
490
491         quota = (u64 *)nfacct->data;
492         now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
493                atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
494
495         ret = now > *quota;
496
497         if (now >= *quota &&
498             !test_and_set_bit(NFACCT_OVERQUOTA_BIT, &nfacct->flags)) {
499                 nfnl_overquota_report(net, nfacct);
500         }
501
502         return ret;
503 }
504 EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
505
506 static int __net_init nfnl_acct_net_init(struct net *net)
507 {
508         INIT_LIST_HEAD(&nfnl_acct_pernet(net)->nfnl_acct_list);
509
510         return 0;
511 }
512
513 static void __net_exit nfnl_acct_net_exit(struct net *net)
514 {
515         struct nfnl_acct_net *nfnl_acct_net = nfnl_acct_pernet(net);
516         struct nf_acct *cur, *tmp;
517
518         list_for_each_entry_safe(cur, tmp, &nfnl_acct_net->nfnl_acct_list, head) {
519                 list_del_rcu(&cur->head);
520
521                 if (refcount_dec_and_test(&cur->refcnt))
522                         kfree_rcu(cur, rcu_head);
523         }
524 }
525
526 static struct pernet_operations nfnl_acct_ops = {
527         .init   = nfnl_acct_net_init,
528         .exit   = nfnl_acct_net_exit,
529         .id     = &nfnl_acct_net_id,
530         .size   = sizeof(struct nfnl_acct_net),
531 };
532
533 static int __init nfnl_acct_init(void)
534 {
535         int ret;
536
537         ret = register_pernet_subsys(&nfnl_acct_ops);
538         if (ret < 0) {
539                 pr_err("nfnl_acct_init: failed to register pernet ops\n");
540                 goto err_out;
541         }
542
543         ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
544         if (ret < 0) {
545                 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
546                 goto cleanup_pernet;
547         }
548         return 0;
549
550 cleanup_pernet:
551         unregister_pernet_subsys(&nfnl_acct_ops);
552 err_out:
553         return ret;
554 }
555
556 static void __exit nfnl_acct_exit(void)
557 {
558         nfnetlink_subsys_unregister(&nfnl_acct_subsys);
559         unregister_pernet_subsys(&nfnl_acct_ops);
560 }
561
562 module_init(nfnl_acct_init);
563 module_exit(nfnl_acct_exit);