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