netfilter: nfnetlink_acct: Adding quota support to accounting framework
[firefly-linux-kernel-4.4.55.git] / net / netfilter / nfnetlink_acct.c
1 /*
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 as
7  * published by the Free Software Foundation (or any later at your option).
8  */
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/skbuff.h>
13 #include <linux/atomic.h>
14 #include <linux/netlink.h>
15 #include <linux/rculist.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <net/netlink.h>
20 #include <net/sock.h>
21
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/nfnetlink.h>
24 #include <linux/netfilter/nfnetlink_acct.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
28 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
29
30 static LIST_HEAD(nfnl_acct_list);
31
32 struct nf_acct {
33         atomic64_t              pkts;
34         atomic64_t              bytes;
35         unsigned long           flags;
36         struct list_head        head;
37         atomic_t                refcnt;
38         char                    name[NFACCT_NAME_MAX];
39         struct rcu_head         rcu_head;
40         char                    data[0];
41 };
42
43 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
44
45 static int
46 nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
47              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
48 {
49         struct nf_acct *nfacct, *matching = NULL;
50         char *acct_name;
51         unsigned int size = 0;
52         u32 flags = 0;
53
54         if (!tb[NFACCT_NAME])
55                 return -EINVAL;
56
57         acct_name = nla_data(tb[NFACCT_NAME]);
58         if (strlen(acct_name) == 0)
59                 return -EINVAL;
60
61         list_for_each_entry(nfacct, &nfnl_acct_list, head) {
62                 if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0)
63                         continue;
64
65                 if (nlh->nlmsg_flags & NLM_F_EXCL)
66                         return -EEXIST;
67
68                 matching = nfacct;
69                 break;
70         }
71
72         if (matching) {
73                 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
74                         /* reset counters if you request a replacement. */
75                         atomic64_set(&matching->pkts, 0);
76                         atomic64_set(&matching->bytes, 0);
77                         smp_mb__before_clear_bit();
78                         /* reset overquota flag if quota is enabled. */
79                         if ((matching->flags & NFACCT_F_QUOTA))
80                                 clear_bit(NFACCT_F_OVERQUOTA, &matching->flags);
81                         return 0;
82                 }
83                 return -EBUSY;
84         }
85
86         nfacct = kzalloc(sizeof(struct nf_acct), GFP_KERNEL);
87         if (tb[NFACCT_FLAGS]) {
88                 flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
89                 if (flags & ~NFACCT_F_QUOTA)
90                         return -EOPNOTSUPP;
91                 if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
92                         return -EINVAL;
93                 if (flags & NFACCT_F_OVERQUOTA)
94                         return -EINVAL;
95
96                 size += sizeof(u64);
97         }
98
99         nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
100         if (nfacct == NULL)
101                 return -ENOMEM;
102
103         if (flags & NFACCT_F_QUOTA) {
104                 u64 *quota = (u64 *)nfacct->data;
105
106                 *quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
107                 nfacct->flags = flags;
108         }
109
110         strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX);
111
112         if (tb[NFACCT_BYTES]) {
113                 atomic64_set(&nfacct->bytes,
114                              be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES])));
115         }
116         if (tb[NFACCT_PKTS]) {
117                 atomic64_set(&nfacct->pkts,
118                              be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
119         }
120         atomic_set(&nfacct->refcnt, 1);
121         list_add_tail_rcu(&nfacct->head, &nfnl_acct_list);
122         return 0;
123 }
124
125 static int
126 nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
127                    int event, struct nf_acct *acct)
128 {
129         struct nlmsghdr *nlh;
130         struct nfgenmsg *nfmsg;
131         unsigned int flags = portid ? NLM_F_MULTI : 0;
132         u64 pkts, bytes;
133
134         event |= NFNL_SUBSYS_ACCT << 8;
135         nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
136         if (nlh == NULL)
137                 goto nlmsg_failure;
138
139         nfmsg = nlmsg_data(nlh);
140         nfmsg->nfgen_family = AF_UNSPEC;
141         nfmsg->version = NFNETLINK_V0;
142         nfmsg->res_id = 0;
143
144         if (nla_put_string(skb, NFACCT_NAME, acct->name))
145                 goto nla_put_failure;
146
147         if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
148                 pkts = atomic64_xchg(&acct->pkts, 0);
149                 bytes = atomic64_xchg(&acct->bytes, 0);
150                 smp_mb__before_clear_bit();
151                 if (acct->flags & NFACCT_F_QUOTA)
152                         clear_bit(NFACCT_F_OVERQUOTA, &acct->flags);
153         } else {
154                 pkts = atomic64_read(&acct->pkts);
155                 bytes = atomic64_read(&acct->bytes);
156         }
157         if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts)) ||
158             nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) ||
159             nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt))))
160                 goto nla_put_failure;
161         if (acct->flags & NFACCT_F_QUOTA) {
162                 u64 *quota = (u64 *)acct->data;
163
164                 if (nla_put_be32(skb, NFACCT_FLAGS, htonl(acct->flags)) ||
165                     nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota)))
166                         goto nla_put_failure;
167         }
168         nlmsg_end(skb, nlh);
169         return skb->len;
170
171 nlmsg_failure:
172 nla_put_failure:
173         nlmsg_cancel(skb, nlh);
174         return -1;
175 }
176
177 static int
178 nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
179 {
180         struct nf_acct *cur, *last;
181
182         if (cb->args[2])
183                 return 0;
184
185         last = (struct nf_acct *)cb->args[1];
186         if (cb->args[1])
187                 cb->args[1] = 0;
188
189         rcu_read_lock();
190         list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
191                 if (last) {
192                         if (cur != last)
193                                 continue;
194
195                         last = NULL;
196                 }
197                 if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
198                                        cb->nlh->nlmsg_seq,
199                                        NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
200                                        NFNL_MSG_ACCT_NEW, cur) < 0) {
201                         cb->args[1] = (unsigned long)cur;
202                         break;
203                 }
204         }
205         if (!cb->args[1])
206                 cb->args[2] = 1;
207         rcu_read_unlock();
208         return skb->len;
209 }
210
211 static int
212 nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
213              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
214 {
215         int ret = -ENOENT;
216         struct nf_acct *cur;
217         char *acct_name;
218
219         if (nlh->nlmsg_flags & NLM_F_DUMP) {
220                 struct netlink_dump_control c = {
221                         .dump = nfnl_acct_dump,
222                 };
223                 return netlink_dump_start(nfnl, skb, nlh, &c);
224         }
225
226         if (!tb[NFACCT_NAME])
227                 return -EINVAL;
228         acct_name = nla_data(tb[NFACCT_NAME]);
229
230         list_for_each_entry(cur, &nfnl_acct_list, head) {
231                 struct sk_buff *skb2;
232
233                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
234                         continue;
235
236                 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
237                 if (skb2 == NULL) {
238                         ret = -ENOMEM;
239                         break;
240                 }
241
242                 ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
243                                          nlh->nlmsg_seq,
244                                          NFNL_MSG_TYPE(nlh->nlmsg_type),
245                                          NFNL_MSG_ACCT_NEW, cur);
246                 if (ret <= 0) {
247                         kfree_skb(skb2);
248                         break;
249                 }
250                 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
251                                         MSG_DONTWAIT);
252                 if (ret > 0)
253                         ret = 0;
254
255                 /* this avoids a loop in nfnetlink. */
256                 return ret == -EAGAIN ? -ENOBUFS : ret;
257         }
258         return ret;
259 }
260
261 /* try to delete object, fail if it is still in use. */
262 static int nfnl_acct_try_del(struct nf_acct *cur)
263 {
264         int ret = 0;
265
266         /* we want to avoid races with nfnl_acct_find_get. */
267         if (atomic_dec_and_test(&cur->refcnt)) {
268                 /* We are protected by nfnl mutex. */
269                 list_del_rcu(&cur->head);
270                 kfree_rcu(cur, rcu_head);
271         } else {
272                 /* still in use, restore reference counter. */
273                 atomic_inc(&cur->refcnt);
274                 ret = -EBUSY;
275         }
276         return ret;
277 }
278
279 static int
280 nfnl_acct_del(struct sock *nfnl, struct sk_buff *skb,
281              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
282 {
283         char *acct_name;
284         struct nf_acct *cur;
285         int ret = -ENOENT;
286
287         if (!tb[NFACCT_NAME]) {
288                 list_for_each_entry(cur, &nfnl_acct_list, head)
289                         nfnl_acct_try_del(cur);
290
291                 return 0;
292         }
293         acct_name = nla_data(tb[NFACCT_NAME]);
294
295         list_for_each_entry(cur, &nfnl_acct_list, head) {
296                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
297                         continue;
298
299                 ret = nfnl_acct_try_del(cur);
300                 if (ret < 0)
301                         return ret;
302
303                 break;
304         }
305         return ret;
306 }
307
308 static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
309         [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
310         [NFACCT_BYTES] = { .type = NLA_U64 },
311         [NFACCT_PKTS] = { .type = NLA_U64 },
312         [NFACCT_FLAGS] = { .type = NLA_U32 },
313         [NFACCT_QUOTA] = { .type = NLA_U64 },
314 };
315
316 static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
317         [NFNL_MSG_ACCT_NEW]             = { .call = nfnl_acct_new,
318                                             .attr_count = NFACCT_MAX,
319                                             .policy = nfnl_acct_policy },
320         [NFNL_MSG_ACCT_GET]             = { .call = nfnl_acct_get,
321                                             .attr_count = NFACCT_MAX,
322                                             .policy = nfnl_acct_policy },
323         [NFNL_MSG_ACCT_GET_CTRZERO]     = { .call = nfnl_acct_get,
324                                             .attr_count = NFACCT_MAX,
325                                             .policy = nfnl_acct_policy },
326         [NFNL_MSG_ACCT_DEL]             = { .call = nfnl_acct_del,
327                                             .attr_count = NFACCT_MAX,
328                                             .policy = nfnl_acct_policy },
329 };
330
331 static const struct nfnetlink_subsystem nfnl_acct_subsys = {
332         .name                           = "acct",
333         .subsys_id                      = NFNL_SUBSYS_ACCT,
334         .cb_count                       = NFNL_MSG_ACCT_MAX,
335         .cb                             = nfnl_acct_cb,
336 };
337
338 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
339
340 struct nf_acct *nfnl_acct_find_get(const char *acct_name)
341 {
342         struct nf_acct *cur, *acct = NULL;
343
344         rcu_read_lock();
345         list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
346                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
347                         continue;
348
349                 if (!try_module_get(THIS_MODULE))
350                         goto err;
351
352                 if (!atomic_inc_not_zero(&cur->refcnt)) {
353                         module_put(THIS_MODULE);
354                         goto err;
355                 }
356
357                 acct = cur;
358                 break;
359         }
360 err:
361         rcu_read_unlock();
362         return acct;
363 }
364 EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
365
366 void nfnl_acct_put(struct nf_acct *acct)
367 {
368         atomic_dec(&acct->refcnt);
369         module_put(THIS_MODULE);
370 }
371 EXPORT_SYMBOL_GPL(nfnl_acct_put);
372
373 void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
374 {
375         atomic64_inc(&nfacct->pkts);
376         atomic64_add(skb->len, &nfacct->bytes);
377 }
378 EXPORT_SYMBOL_GPL(nfnl_acct_update);
379
380 static void nfnl_overquota_report(struct nf_acct *nfacct)
381 {
382         int ret;
383         struct sk_buff *skb;
384
385         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
386         if (skb == NULL)
387                 return;
388
389         ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
390                                   nfacct);
391         if (ret <= 0) {
392                 kfree_skb(skb);
393                 return;
394         }
395         netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
396                           GFP_ATOMIC);
397 }
398
399 int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
400 {
401         u64 now;
402         u64 *quota;
403         int ret = NFACCT_UNDERQUOTA;
404
405         /* no place here if we don't have a quota */
406         if (!(nfacct->flags & NFACCT_F_QUOTA))
407                 return NFACCT_NO_QUOTA;
408
409         quota = (u64 *)nfacct->data;
410         now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
411                atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
412
413         ret = now > *quota;
414
415         if (now >= *quota &&
416             !test_and_set_bit(NFACCT_F_OVERQUOTA, &nfacct->flags)) {
417                 nfnl_overquota_report(nfacct);
418         }
419
420         return ret;
421 }
422 EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
423
424 static int __init nfnl_acct_init(void)
425 {
426         int ret;
427
428         pr_info("nfnl_acct: registering with nfnetlink.\n");
429         ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
430         if (ret < 0) {
431                 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
432                 goto err_out;
433         }
434         return 0;
435 err_out:
436         return ret;
437 }
438
439 static void __exit nfnl_acct_exit(void)
440 {
441         struct nf_acct *cur, *tmp;
442
443         pr_info("nfnl_acct: unregistering from nfnetlink.\n");
444         nfnetlink_subsys_unregister(&nfnl_acct_subsys);
445
446         list_for_each_entry_safe(cur, tmp, &nfnl_acct_list, head) {
447                 list_del_rcu(&cur->head);
448                 /* We are sure that our objects have no clients at this point,
449                  * it's safe to release them all without checking refcnt. */
450                 kfree_rcu(cur, rcu_head);
451         }
452 }
453
454 module_init(nfnl_acct_init);
455 module_exit(nfnl_acct_exit);