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