blob: 4c2b4c0c4d5fa4ac209ab85020e97e14ed716ab9 [file] [log] [blame]
Pablo Neira Ayuso94139022011-12-23 14:19:50 +01001/*
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>
Andrew Morton6523cf92012-03-28 12:10:57 +000013#include <linux/atomic.h>
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010014#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>
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010021
22#include <linux/netfilter.h>
23#include <linux/netfilter/nfnetlink.h>
24#include <linux/netfilter/nfnetlink_acct.h>
25
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
28MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
29
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010030struct nf_acct {
31 atomic64_t pkts;
32 atomic64_t bytes;
Mathieu Poirier683399e2014-04-20 18:57:36 -060033 unsigned long flags;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010034 struct list_head head;
35 atomic_t refcnt;
36 char name[NFACCT_NAME_MAX];
37 struct rcu_head rcu_head;
Mathieu Poirier683399e2014-04-20 18:57:36 -060038 char data[0];
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010039};
40
Alexey Perevalovf111f782014-08-20 22:03:18 +040041struct nfacct_filter {
42 u32 value;
43 u32 mask;
44};
45
Mathieu Poirier683399e2014-04-20 18:57:36 -060046#define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
Alexey Perevalovb6d04682014-07-31 17:14:05 +040047#define NFACCT_OVERQUOTA_BIT 2 /* NFACCT_F_OVERQUOTA */
Mathieu Poirier683399e2014-04-20 18:57:36 -060048
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +010049static 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[])
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010052{
53 struct nf_acct *nfacct, *matching = NULL;
54 char *acct_name;
Mathieu Poirier683399e2014-04-20 18:57:36 -060055 unsigned int size = 0;
56 u32 flags = 0;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010057
58 if (!tb[NFACCT_NAME])
59 return -EINVAL;
60
61 acct_name = nla_data(tb[NFACCT_NAME]);
Pablo Neira Ayusodeadcfc2013-03-23 16:57:59 +010062 if (strlen(acct_name) == 0)
63 return -EINVAL;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010064
Andreas Schultz3499abb2015-08-05 17:51:45 +020065 list_for_each_entry(nfacct, &net->nfnl_acct_list, head) {
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010066 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);
Linus Torvaldsf9da4552014-06-12 14:27:40 -070081 smp_mb__before_atomic();
Mathieu Poirier683399e2014-04-20 18:57:36 -060082 /* reset overquota flag if quota is enabled. */
83 if ((matching->flags & NFACCT_F_QUOTA))
Alexey Perevalovb6d04682014-07-31 17:14:05 +040084 clear_bit(NFACCT_OVERQUOTA_BIT,
85 &matching->flags);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +010086 return 0;
87 }
88 return -EBUSY;
89 }
90
Mathieu Poirier683399e2014-04-20 18:57:36 -060091 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
100 size += sizeof(u64);
101 }
102
103 nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100104 if (nfacct == NULL)
105 return -ENOMEM;
106
Mathieu Poirier683399e2014-04-20 18:57:36 -0600107 if (flags & NFACCT_F_QUOTA) {
108 u64 *quota = (u64 *)nfacct->data;
109
110 *quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
111 nfacct->flags = flags;
112 }
113
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100114 strncpy(nfacct->name, nla_data(tb[NFACCT_NAME]), NFACCT_NAME_MAX);
115
116 if (tb[NFACCT_BYTES]) {
117 atomic64_set(&nfacct->bytes,
Patrick McHardyfe31d1a2012-08-19 10:16:09 +0000118 be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES])));
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100119 }
120 if (tb[NFACCT_PKTS]) {
121 atomic64_set(&nfacct->pkts,
Patrick McHardyfe31d1a2012-08-19 10:16:09 +0000122 be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100123 }
124 atomic_set(&nfacct->refcnt, 1);
Andreas Schultz3499abb2015-08-05 17:51:45 +0200125 list_add_tail_rcu(&nfacct->head, &net->nfnl_acct_list);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100126 return 0;
127}
128
129static int
Eric W. Biederman15e47302012-09-07 20:12:54 +0000130nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100131 int event, struct nf_acct *acct)
132{
133 struct nlmsghdr *nlh;
134 struct nfgenmsg *nfmsg;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000135 unsigned int flags = portid ? NLM_F_MULTI : 0;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100136 u64 pkts, bytes;
Alexey Perevalovd24675c2014-07-30 19:17:55 +0400137 u32 old_flags;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100138
139 event |= NFNL_SUBSYS_ACCT << 8;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000140 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100141 if (nlh == NULL)
142 goto nlmsg_failure;
143
144 nfmsg = nlmsg_data(nlh);
145 nfmsg->nfgen_family = AF_UNSPEC;
146 nfmsg->version = NFNETLINK_V0;
147 nfmsg->res_id = 0;
148
David S. Miller7c801182012-04-01 18:46:29 -0400149 if (nla_put_string(skb, NFACCT_NAME, acct->name))
150 goto nla_put_failure;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100151
Alexey Perevalovd24675c2014-07-30 19:17:55 +0400152 old_flags = acct->flags;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100153 if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
154 pkts = atomic64_xchg(&acct->pkts, 0);
155 bytes = atomic64_xchg(&acct->bytes, 0);
Linus Torvaldsf9da4552014-06-12 14:27:40 -0700156 smp_mb__before_atomic();
Mathieu Poirier683399e2014-04-20 18:57:36 -0600157 if (acct->flags & NFACCT_F_QUOTA)
Alexey Perevalovb6d04682014-07-31 17:14:05 +0400158 clear_bit(NFACCT_OVERQUOTA_BIT, &acct->flags);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100159 } else {
160 pkts = atomic64_read(&acct->pkts);
161 bytes = atomic64_read(&acct->bytes);
162 }
David S. Miller7c801182012-04-01 18:46:29 -0400163 if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts)) ||
164 nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes)) ||
165 nla_put_be32(skb, NFACCT_USE, htonl(atomic_read(&acct->refcnt))))
166 goto nla_put_failure;
Mathieu Poirier683399e2014-04-20 18:57:36 -0600167 if (acct->flags & NFACCT_F_QUOTA) {
168 u64 *quota = (u64 *)acct->data;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100169
Alexey Perevalovd24675c2014-07-30 19:17:55 +0400170 if (nla_put_be32(skb, NFACCT_FLAGS, htonl(old_flags)) ||
Mathieu Poirier683399e2014-04-20 18:57:36 -0600171 nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota)))
172 goto nla_put_failure;
173 }
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100174 nlmsg_end(skb, nlh);
175 return skb->len;
176
177nlmsg_failure:
178nla_put_failure:
179 nlmsg_cancel(skb, nlh);
180 return -1;
181}
182
183static int
184nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
185{
Andreas Schultz3499abb2015-08-05 17:51:45 +0200186 struct net *net = sock_net(skb->sk);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100187 struct nf_acct *cur, *last;
Alexey Perevalovf111f782014-08-20 22:03:18 +0400188 const struct nfacct_filter *filter = cb->data;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100189
190 if (cb->args[2])
191 return 0;
192
193 last = (struct nf_acct *)cb->args[1];
194 if (cb->args[1])
195 cb->args[1] = 0;
196
197 rcu_read_lock();
Andreas Schultz3499abb2015-08-05 17:51:45 +0200198 list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
Pablo Neira Ayuso991a6b72013-06-01 15:31:40 +0200199 if (last) {
200 if (cur != last)
201 continue;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100202
Pablo Neira Ayuso991a6b72013-06-01 15:31:40 +0200203 last = NULL;
204 }
Alexey Perevalovf111f782014-08-20 22:03:18 +0400205
206 if (filter && (cur->flags & filter->mask) != filter->value)
207 continue;
208
Eric W. Biederman15e47302012-09-07 20:12:54 +0000209 if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100210 cb->nlh->nlmsg_seq,
211 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
212 NFNL_MSG_ACCT_NEW, cur) < 0) {
213 cb->args[1] = (unsigned long)cur;
214 break;
215 }
216 }
217 if (!cb->args[1])
218 cb->args[2] = 1;
219 rcu_read_unlock();
220 return skb->len;
221}
222
Alexey Perevalovf111f782014-08-20 22:03:18 +0400223static int nfnl_acct_done(struct netlink_callback *cb)
224{
225 kfree(cb->data);
226 return 0;
227}
228
229static const struct nla_policy filter_policy[NFACCT_FILTER_MAX + 1] = {
230 [NFACCT_FILTER_MASK] = { .type = NLA_U32 },
231 [NFACCT_FILTER_VALUE] = { .type = NLA_U32 },
232};
233
234static struct nfacct_filter *
235nfacct_filter_alloc(const struct nlattr * const attr)
236{
237 struct nfacct_filter *filter;
238 struct nlattr *tb[NFACCT_FILTER_MAX + 1];
239 int err;
240
241 err = nla_parse_nested(tb, NFACCT_FILTER_MAX, attr, filter_policy);
242 if (err < 0)
243 return ERR_PTR(err);
244
Phil Turnbull017b1b62016-02-24 15:34:43 -0500245 if (!tb[NFACCT_FILTER_MASK] || !tb[NFACCT_FILTER_VALUE])
246 return ERR_PTR(-EINVAL);
247
Alexey Perevalovf111f782014-08-20 22:03:18 +0400248 filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL);
249 if (!filter)
250 return ERR_PTR(-ENOMEM);
251
252 filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK]));
253 filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE]));
254
255 return filter;
256}
257
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +0100258static int nfnl_acct_get(struct net *net, struct sock *nfnl,
259 struct sk_buff *skb, const struct nlmsghdr *nlh,
260 const struct nlattr * const tb[])
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100261{
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100262 int ret = -ENOENT;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100263 struct nf_acct *cur;
264 char *acct_name;
265
266 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000267 struct netlink_dump_control c = {
268 .dump = nfnl_acct_dump,
Alexey Perevalovf111f782014-08-20 22:03:18 +0400269 .done = nfnl_acct_done,
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000270 };
Alexey Perevalovf111f782014-08-20 22:03:18 +0400271
272 if (tb[NFACCT_FILTER]) {
273 struct nfacct_filter *filter;
274
275 filter = nfacct_filter_alloc(tb[NFACCT_FILTER]);
276 if (IS_ERR(filter))
277 return PTR_ERR(filter);
278
279 c.data = filter;
280 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000281 return netlink_dump_start(nfnl, skb, nlh, &c);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100282 }
283
284 if (!tb[NFACCT_NAME])
285 return -EINVAL;
286 acct_name = nla_data(tb[NFACCT_NAME]);
287
Andreas Schultz3499abb2015-08-05 17:51:45 +0200288 list_for_each_entry(cur, &net->nfnl_acct_list, head) {
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100289 struct sk_buff *skb2;
290
291 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
292 continue;
293
294 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100295 if (skb2 == NULL) {
296 ret = -ENOMEM;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100297 break;
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100298 }
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100299
Eric W. Biederman15e47302012-09-07 20:12:54 +0000300 ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100301 nlh->nlmsg_seq,
302 NFNL_MSG_TYPE(nlh->nlmsg_type),
303 NFNL_MSG_ACCT_NEW, cur);
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100304 if (ret <= 0) {
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100305 kfree_skb(skb2);
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100306 break;
307 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000308 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100309 MSG_DONTWAIT);
310 if (ret > 0)
311 ret = 0;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100312
Pablo Neira Ayuso3ab0b242011-12-30 20:32:26 +0100313 /* this avoids a loop in nfnetlink. */
314 return ret == -EAGAIN ? -ENOBUFS : ret;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100315 }
316 return ret;
317}
318
319/* try to delete object, fail if it is still in use. */
320static int nfnl_acct_try_del(struct nf_acct *cur)
321{
322 int ret = 0;
323
324 /* we want to avoid races with nfnl_acct_find_get. */
325 if (atomic_dec_and_test(&cur->refcnt)) {
326 /* We are protected by nfnl mutex. */
327 list_del_rcu(&cur->head);
328 kfree_rcu(cur, rcu_head);
329 } else {
330 /* still in use, restore reference counter. */
331 atomic_inc(&cur->refcnt);
332 ret = -EBUSY;
333 }
334 return ret;
335}
336
Pablo Neira Ayuso7b8002a2015-12-15 18:41:56 +0100337static int nfnl_acct_del(struct net *net, struct sock *nfnl,
338 struct sk_buff *skb, const struct nlmsghdr *nlh,
339 const struct nlattr * const tb[])
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100340{
341 char *acct_name;
342 struct nf_acct *cur;
343 int ret = -ENOENT;
344
345 if (!tb[NFACCT_NAME]) {
Andreas Schultz3499abb2015-08-05 17:51:45 +0200346 list_for_each_entry(cur, &net->nfnl_acct_list, head)
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100347 nfnl_acct_try_del(cur);
348
349 return 0;
350 }
351 acct_name = nla_data(tb[NFACCT_NAME]);
352
Andreas Schultz3499abb2015-08-05 17:51:45 +0200353 list_for_each_entry(cur, &net->nfnl_acct_list, head) {
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100354 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
355 continue;
356
357 ret = nfnl_acct_try_del(cur);
358 if (ret < 0)
359 return ret;
360
361 break;
362 }
363 return ret;
364}
365
366static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
367 [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
368 [NFACCT_BYTES] = { .type = NLA_U64 },
369 [NFACCT_PKTS] = { .type = NLA_U64 },
Mathieu Poirier683399e2014-04-20 18:57:36 -0600370 [NFACCT_FLAGS] = { .type = NLA_U32 },
371 [NFACCT_QUOTA] = { .type = NLA_U64 },
Alexey Perevalovf111f782014-08-20 22:03:18 +0400372 [NFACCT_FILTER] = {.type = NLA_NESTED },
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100373};
374
375static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
376 [NFNL_MSG_ACCT_NEW] = { .call = nfnl_acct_new,
377 .attr_count = NFACCT_MAX,
378 .policy = nfnl_acct_policy },
379 [NFNL_MSG_ACCT_GET] = { .call = nfnl_acct_get,
380 .attr_count = NFACCT_MAX,
381 .policy = nfnl_acct_policy },
382 [NFNL_MSG_ACCT_GET_CTRZERO] = { .call = nfnl_acct_get,
383 .attr_count = NFACCT_MAX,
384 .policy = nfnl_acct_policy },
385 [NFNL_MSG_ACCT_DEL] = { .call = nfnl_acct_del,
386 .attr_count = NFACCT_MAX,
387 .policy = nfnl_acct_policy },
388};
389
390static const struct nfnetlink_subsystem nfnl_acct_subsys = {
391 .name = "acct",
392 .subsys_id = NFNL_SUBSYS_ACCT,
393 .cb_count = NFNL_MSG_ACCT_MAX,
394 .cb = nfnl_acct_cb,
395};
396
397MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
398
Andreas Schultz3499abb2015-08-05 17:51:45 +0200399struct nf_acct *nfnl_acct_find_get(struct net *net, const char *acct_name)
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100400{
401 struct nf_acct *cur, *acct = NULL;
402
403 rcu_read_lock();
Andreas Schultz3499abb2015-08-05 17:51:45 +0200404 list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100405 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
406 continue;
407
408 if (!try_module_get(THIS_MODULE))
409 goto err;
410
411 if (!atomic_inc_not_zero(&cur->refcnt)) {
412 module_put(THIS_MODULE);
413 goto err;
414 }
415
416 acct = cur;
417 break;
418 }
419err:
420 rcu_read_unlock();
421 return acct;
422}
423EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
424
425void nfnl_acct_put(struct nf_acct *acct)
426{
Andreas Schultz3499abb2015-08-05 17:51:45 +0200427 if (atomic_dec_and_test(&acct->refcnt))
428 kfree_rcu(acct, rcu_head);
429
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100430 module_put(THIS_MODULE);
431}
432EXPORT_SYMBOL_GPL(nfnl_acct_put);
433
434void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
435{
436 atomic64_inc(&nfacct->pkts);
437 atomic64_add(skb->len, &nfacct->bytes);
438}
439EXPORT_SYMBOL_GPL(nfnl_acct_update);
440
Mathieu Poirier683399e2014-04-20 18:57:36 -0600441static void nfnl_overquota_report(struct nf_acct *nfacct)
442{
443 int ret;
444 struct sk_buff *skb;
445
446 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
447 if (skb == NULL)
448 return;
449
450 ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
451 nfacct);
452 if (ret <= 0) {
453 kfree_skb(skb);
454 return;
455 }
456 netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
457 GFP_ATOMIC);
458}
459
460int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
461{
462 u64 now;
463 u64 *quota;
464 int ret = NFACCT_UNDERQUOTA;
465
466 /* no place here if we don't have a quota */
467 if (!(nfacct->flags & NFACCT_F_QUOTA))
468 return NFACCT_NO_QUOTA;
469
470 quota = (u64 *)nfacct->data;
471 now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
472 atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
473
474 ret = now > *quota;
475
476 if (now >= *quota &&
Alexey Perevalovb6d04682014-07-31 17:14:05 +0400477 !test_and_set_bit(NFACCT_OVERQUOTA_BIT, &nfacct->flags)) {
Mathieu Poirier683399e2014-04-20 18:57:36 -0600478 nfnl_overquota_report(nfacct);
479 }
480
481 return ret;
482}
483EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
484
Andreas Schultz3499abb2015-08-05 17:51:45 +0200485static int __net_init nfnl_acct_net_init(struct net *net)
486{
487 INIT_LIST_HEAD(&net->nfnl_acct_list);
488
489 return 0;
490}
491
492static void __net_exit nfnl_acct_net_exit(struct net *net)
493{
494 struct nf_acct *cur, *tmp;
495
496 list_for_each_entry_safe(cur, tmp, &net->nfnl_acct_list, head) {
497 list_del_rcu(&cur->head);
498
499 if (atomic_dec_and_test(&cur->refcnt))
500 kfree_rcu(cur, rcu_head);
501 }
502}
503
504static struct pernet_operations nfnl_acct_ops = {
505 .init = nfnl_acct_net_init,
506 .exit = nfnl_acct_net_exit,
507};
508
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100509static int __init nfnl_acct_init(void)
510{
511 int ret;
512
Andreas Schultz3499abb2015-08-05 17:51:45 +0200513 ret = register_pernet_subsys(&nfnl_acct_ops);
514 if (ret < 0) {
515 pr_err("nfnl_acct_init: failed to register pernet ops\n");
516 goto err_out;
517 }
518
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100519 pr_info("nfnl_acct: registering with nfnetlink.\n");
520 ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
521 if (ret < 0) {
522 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
Andreas Schultz3499abb2015-08-05 17:51:45 +0200523 goto cleanup_pernet;
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100524 }
525 return 0;
Andreas Schultz3499abb2015-08-05 17:51:45 +0200526
527cleanup_pernet:
528 unregister_pernet_subsys(&nfnl_acct_ops);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100529err_out:
530 return ret;
531}
532
533static void __exit nfnl_acct_exit(void)
534{
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100535 pr_info("nfnl_acct: unregistering from nfnetlink.\n");
536 nfnetlink_subsys_unregister(&nfnl_acct_subsys);
Andreas Schultz3499abb2015-08-05 17:51:45 +0200537 unregister_pernet_subsys(&nfnl_acct_ops);
Pablo Neira Ayuso94139022011-12-23 14:19:50 +0100538}
539
540module_init(nfnl_acct_init);
541module_exit(nfnl_acct_exit);