]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nfnetlink_acct.c
11d863c8b11f6b0d66fa2a61f735a599021d3dd0
[mirror_ubuntu-artful-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 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_atomic();
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 if (tb[NFACCT_FLAGS]) {
87 flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
88 if (flags & ~NFACCT_F_QUOTA)
89 return -EOPNOTSUPP;
90 if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
91 return -EINVAL;
92 if (flags & NFACCT_F_OVERQUOTA)
93 return -EINVAL;
94
95 size += sizeof(u64);
96 }
97
98 nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
99 if (nfacct == NULL)
100 return -ENOMEM;
101
102 if (flags & NFACCT_F_QUOTA) {
103 u64 *quota = (u64 *)nfacct->data;
104
105 *quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
106 nfacct->flags = flags;
107 }
108
109 strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX);
110
111 if (tb[NFACCT_BYTES]) {
112 atomic64_set(&nfacct->bytes,
113 be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES])));
114 }
115 if (tb[NFACCT_PKTS]) {
116 atomic64_set(&nfacct->pkts,
117 be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
118 }
119 atomic_set(&nfacct->refcnt, 1);
120 list_add_tail_rcu(&nfacct->head, &nfnl_acct_list);
121 return 0;
122 }
123
124 static int
125 nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
126 int event, struct nf_acct *acct)
127 {
128 struct nlmsghdr *nlh;
129 struct nfgenmsg *nfmsg;
130 unsigned int flags = portid ? NLM_F_MULTI : 0;
131 u64 pkts, bytes;
132 u32 old_flags;
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 old_flags = acct->flags;
148 if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
149 pkts = atomic64_xchg(&acct->pkts, 0);
150 bytes = atomic64_xchg(&acct->bytes, 0);
151 smp_mb__before_atomic();
152 if (acct->flags & NFACCT_F_QUOTA)
153 clear_bit(NFACCT_F_OVERQUOTA, &acct->flags);
154 } else {
155 pkts = atomic64_read(&acct->pkts);
156 bytes = atomic64_read(&acct->bytes);
157 }
158 if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts)) ||
159 nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) ||
160 nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt))))
161 goto nla_put_failure;
162 if (acct->flags & NFACCT_F_QUOTA) {
163 u64 *quota = (u64 *)acct->data;
164
165 if (nla_put_be32(skb, NFACCT_FLAGS, htonl(old_flags)) ||
166 nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota)))
167 goto nla_put_failure;
168 }
169 nlmsg_end(skb, nlh);
170 return skb->len;
171
172 nlmsg_failure:
173 nla_put_failure:
174 nlmsg_cancel(skb, nlh);
175 return -1;
176 }
177
178 static int
179 nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
180 {
181 struct nf_acct *cur, *last;
182
183 if (cb->args[2])
184 return 0;
185
186 last = (struct nf_acct *)cb->args[1];
187 if (cb->args[1])
188 cb->args[1] = 0;
189
190 rcu_read_lock();
191 list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
192 if (last) {
193 if (cur != last)
194 continue;
195
196 last = NULL;
197 }
198 if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
199 cb->nlh->nlmsg_seq,
200 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
201 NFNL_MSG_ACCT_NEW, cur) < 0) {
202 cb->args[1] = (unsigned long)cur;
203 break;
204 }
205 }
206 if (!cb->args[1])
207 cb->args[2] = 1;
208 rcu_read_unlock();
209 return skb->len;
210 }
211
212 static int
213 nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
214 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
215 {
216 int ret = -ENOENT;
217 struct nf_acct *cur;
218 char *acct_name;
219
220 if (nlh->nlmsg_flags & NLM_F_DUMP) {
221 struct netlink_dump_control c = {
222 .dump = nfnl_acct_dump,
223 };
224 return netlink_dump_start(nfnl, skb, nlh, &c);
225 }
226
227 if (!tb[NFACCT_NAME])
228 return -EINVAL;
229 acct_name = nla_data(tb[NFACCT_NAME]);
230
231 list_for_each_entry(cur, &nfnl_acct_list, head) {
232 struct sk_buff *skb2;
233
234 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
235 continue;
236
237 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
238 if (skb2 == NULL) {
239 ret = -ENOMEM;
240 break;
241 }
242
243 ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
244 nlh->nlmsg_seq,
245 NFNL_MSG_TYPE(nlh->nlmsg_type),
246 NFNL_MSG_ACCT_NEW, cur);
247 if (ret <= 0) {
248 kfree_skb(skb2);
249 break;
250 }
251 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
252 MSG_DONTWAIT);
253 if (ret > 0)
254 ret = 0;
255
256 /* this avoids a loop in nfnetlink. */
257 return ret == -EAGAIN ? -ENOBUFS : ret;
258 }
259 return ret;
260 }
261
262 /* try to delete object, fail if it is still in use. */
263 static int nfnl_acct_try_del(struct nf_acct *cur)
264 {
265 int ret = 0;
266
267 /* we want to avoid races with nfnl_acct_find_get. */
268 if (atomic_dec_and_test(&cur->refcnt)) {
269 /* We are protected by nfnl mutex. */
270 list_del_rcu(&cur->head);
271 kfree_rcu(cur, rcu_head);
272 } else {
273 /* still in use, restore reference counter. */
274 atomic_inc(&cur->refcnt);
275 ret = -EBUSY;
276 }
277 return ret;
278 }
279
280 static int
281 nfnl_acct_del(struct sock *nfnl, struct sk_buff *skb,
282 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
283 {
284 char *acct_name;
285 struct nf_acct *cur;
286 int ret = -ENOENT;
287
288 if (!tb[NFACCT_NAME]) {
289 list_for_each_entry(cur, &nfnl_acct_list, head)
290 nfnl_acct_try_del(cur);
291
292 return 0;
293 }
294 acct_name = nla_data(tb[NFACCT_NAME]);
295
296 list_for_each_entry(cur, &nfnl_acct_list, head) {
297 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
298 continue;
299
300 ret = nfnl_acct_try_del(cur);
301 if (ret < 0)
302 return ret;
303
304 break;
305 }
306 return ret;
307 }
308
309 static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
310 [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
311 [NFACCT_BYTES] = { .type = NLA_U64 },
312 [NFACCT_PKTS] = { .type = NLA_U64 },
313 [NFACCT_FLAGS] = { .type = NLA_U32 },
314 [NFACCT_QUOTA] = { .type = NLA_U64 },
315 };
316
317 static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
318 [NFNL_MSG_ACCT_NEW] = { .call = nfnl_acct_new,
319 .attr_count = NFACCT_MAX,
320 .policy = nfnl_acct_policy },
321 [NFNL_MSG_ACCT_GET] = { .call = nfnl_acct_get,
322 .attr_count = NFACCT_MAX,
323 .policy = nfnl_acct_policy },
324 [NFNL_MSG_ACCT_GET_CTRZERO] = { .call = nfnl_acct_get,
325 .attr_count = NFACCT_MAX,
326 .policy = nfnl_acct_policy },
327 [NFNL_MSG_ACCT_DEL] = { .call = nfnl_acct_del,
328 .attr_count = NFACCT_MAX,
329 .policy = nfnl_acct_policy },
330 };
331
332 static const struct nfnetlink_subsystem nfnl_acct_subsys = {
333 .name = "acct",
334 .subsys_id = NFNL_SUBSYS_ACCT,
335 .cb_count = NFNL_MSG_ACCT_MAX,
336 .cb = nfnl_acct_cb,
337 };
338
339 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
340
341 struct nf_acct *nfnl_acct_find_get(const char *acct_name)
342 {
343 struct nf_acct *cur, *acct = NULL;
344
345 rcu_read_lock();
346 list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
347 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
348 continue;
349
350 if (!try_module_get(THIS_MODULE))
351 goto err;
352
353 if (!atomic_inc_not_zero(&cur->refcnt)) {
354 module_put(THIS_MODULE);
355 goto err;
356 }
357
358 acct = cur;
359 break;
360 }
361 err:
362 rcu_read_unlock();
363 return acct;
364 }
365 EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
366
367 void nfnl_acct_put(struct nf_acct *acct)
368 {
369 atomic_dec(&acct->refcnt);
370 module_put(THIS_MODULE);
371 }
372 EXPORT_SYMBOL_GPL(nfnl_acct_put);
373
374 void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
375 {
376 atomic64_inc(&nfacct->pkts);
377 atomic64_add(skb->len, &nfacct->bytes);
378 }
379 EXPORT_SYMBOL_GPL(nfnl_acct_update);
380
381 static void nfnl_overquota_report(struct nf_acct *nfacct)
382 {
383 int ret;
384 struct sk_buff *skb;
385
386 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
387 if (skb == NULL)
388 return;
389
390 ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
391 nfacct);
392 if (ret <= 0) {
393 kfree_skb(skb);
394 return;
395 }
396 netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
397 GFP_ATOMIC);
398 }
399
400 int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
401 {
402 u64 now;
403 u64 *quota;
404 int ret = NFACCT_UNDERQUOTA;
405
406 /* no place here if we don't have a quota */
407 if (!(nfacct->flags & NFACCT_F_QUOTA))
408 return NFACCT_NO_QUOTA;
409
410 quota = (u64 *)nfacct->data;
411 now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
412 atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
413
414 ret = now > *quota;
415
416 if (now >= *quota &&
417 !test_and_set_bit(NFACCT_F_OVERQUOTA, &nfacct->flags)) {
418 nfnl_overquota_report(nfacct);
419 }
420
421 return ret;
422 }
423 EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
424
425 static int __init nfnl_acct_init(void)
426 {
427 int ret;
428
429 pr_info("nfnl_acct: registering with nfnetlink.\n");
430 ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
431 if (ret < 0) {
432 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
433 goto err_out;
434 }
435 return 0;
436 err_out:
437 return ret;
438 }
439
440 static void __exit nfnl_acct_exit(void)
441 {
442 struct nf_acct *cur, *tmp;
443
444 pr_info("nfnl_acct: unregistering from nfnetlink.\n");
445 nfnetlink_subsys_unregister(&nfnl_acct_subsys);
446
447 list_for_each_entry_safe(cur, tmp, &nfnl_acct_list, head) {
448 list_del_rcu(&cur->head);
449 /* We are sure that our objects have no clients at this point,
450 * it's safe to release them all without checking refcnt. */
451 kfree_rcu(cur, rcu_head);
452 }
453 }
454
455 module_init(nfnl_acct_init);
456 module_exit(nfnl_acct_exit);