~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nfnetlink_acct.c

Version: ~ [ linux-5.9 ] ~ [ linux-5.8.14 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.70 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.150 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.200 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.238 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.238 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 struct nfacct_filter {
 44         u32 value;
 45         u32 mask;
 46 };
 47 
 48 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
 49 #define NFACCT_OVERQUOTA_BIT    2       /* NFACCT_F_OVERQUOTA */
 50 
 51 static int
 52 nfnl_acct_new(struct sock *nfnl, struct sk_buff *skb,
 53              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
 54 {
 55         struct nf_acct *nfacct, *matching = NULL;
 56         char *acct_name;
 57         unsigned int size = 0;
 58         u32 flags = 0;
 59 
 60         if (!tb[NFACCT_NAME])
 61                 return -EINVAL;
 62 
 63         acct_name = nla_data(tb[NFACCT_NAME]);
 64         if (strlen(acct_name) == 0)
 65                 return -EINVAL;
 66 
 67         list_for_each_entry(nfacct, &nfnl_acct_list, head) {
 68                 if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0)
 69                         continue;
 70 
 71                 if (nlh->nlmsg_flags & NLM_F_EXCL)
 72                         return -EEXIST;
 73 
 74                 matching = nfacct;
 75                 break;
 76         }
 77 
 78         if (matching) {
 79                 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
 80                         /* reset counters if you request a replacement. */
 81                         atomic64_set(&matching->pkts, 0);
 82                         atomic64_set(&matching->bytes, 0);
 83                         smp_mb__before_atomic();
 84                         /* reset overquota flag if quota is enabled. */
 85                         if ((matching->flags & NFACCT_F_QUOTA))
 86                                 clear_bit(NFACCT_OVERQUOTA_BIT,
 87                                           &matching->flags);
 88                         return 0;
 89                 }
 90                 return -EBUSY;
 91         }
 92 
 93         if (tb[NFACCT_FLAGS]) {
 94                 flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
 95                 if (flags & ~NFACCT_F_QUOTA)
 96                         return -EOPNOTSUPP;
 97                 if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
 98                         return -EINVAL;
 99                 if (flags & NFACCT_F_OVERQUOTA)
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, &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 nf_acct *cur, *last;
189         const struct nfacct_filter *filter = cb->data;
190 
191         if (cb->args[2])
192                 return 0;
193 
194         last = (struct nf_acct *)cb->args[1];
195         if (cb->args[1])
196                 cb->args[1] = 0;
197 
198         rcu_read_lock();
199         list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
200                 if (last) {
201                         if (cur != last)
202                                 continue;
203 
204                         last = NULL;
205                 }
206 
207                 if (filter && (cur->flags & filter->mask) != filter->value)
208                         continue;
209 
210                 if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
211                                        cb->nlh->nlmsg_seq,
212                                        NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
213                                        NFNL_MSG_ACCT_NEW, cur) < 0) {
214                         cb->args[1] = (unsigned long)cur;
215                         break;
216                 }
217         }
218         if (!cb->args[1])
219                 cb->args[2] = 1;
220         rcu_read_unlock();
221         return skb->len;
222 }
223 
224 static int nfnl_acct_done(struct netlink_callback *cb)
225 {
226         kfree(cb->data);
227         return 0;
228 }
229 
230 static const struct nla_policy filter_policy[NFACCT_FILTER_MAX + 1] = {
231         [NFACCT_FILTER_MASK]    = { .type = NLA_U32 },
232         [NFACCT_FILTER_VALUE]   = { .type = NLA_U32 },
233 };
234 
235 static struct nfacct_filter *
236 nfacct_filter_alloc(const struct nlattr * const attr)
237 {
238         struct nfacct_filter *filter;
239         struct nlattr *tb[NFACCT_FILTER_MAX + 1];
240         int err;
241 
242         err = nla_parse_nested(tb, NFACCT_FILTER_MAX, attr, filter_policy);
243         if (err < 0)
244                 return ERR_PTR(err);
245 
246         filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL);
247         if (!filter)
248                 return ERR_PTR(-ENOMEM);
249 
250         filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK]));
251         filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE]));
252 
253         return filter;
254 }
255 
256 static int
257 nfnl_acct_get(struct sock *nfnl, struct sk_buff *skb,
258              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
259 {
260         int ret = -ENOENT;
261         struct nf_acct *cur;
262         char *acct_name;
263 
264         if (nlh->nlmsg_flags & NLM_F_DUMP) {
265                 struct netlink_dump_control c = {
266                         .dump = nfnl_acct_dump,
267                         .done = nfnl_acct_done,
268                 };
269 
270                 if (tb[NFACCT_FILTER]) {
271                         struct nfacct_filter *filter;
272 
273                         filter = nfacct_filter_alloc(tb[NFACCT_FILTER]);
274                         if (IS_ERR(filter))
275                                 return PTR_ERR(filter);
276 
277                         c.data = filter;
278                 }
279                 return netlink_dump_start(nfnl, skb, nlh, &c);
280         }
281 
282         if (!tb[NFACCT_NAME])
283                 return -EINVAL;
284         acct_name = nla_data(tb[NFACCT_NAME]);
285 
286         list_for_each_entry(cur, &nfnl_acct_list, head) {
287                 struct sk_buff *skb2;
288 
289                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
290                         continue;
291 
292                 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
293                 if (skb2 == NULL) {
294                         ret = -ENOMEM;
295                         break;
296                 }
297 
298                 ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
299                                          nlh->nlmsg_seq,
300                                          NFNL_MSG_TYPE(nlh->nlmsg_type),
301                                          NFNL_MSG_ACCT_NEW, cur);
302                 if (ret <= 0) {
303                         kfree_skb(skb2);
304                         break;
305                 }
306                 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
307                                         MSG_DONTWAIT);
308                 if (ret > 0)
309                         ret = 0;
310 
311                 /* this avoids a loop in nfnetlink. */
312                 return ret == -EAGAIN ? -ENOBUFS : ret;
313         }
314         return ret;
315 }
316 
317 /* try to delete object, fail if it is still in use. */
318 static int nfnl_acct_try_del(struct nf_acct *cur)
319 {
320         int ret = 0;
321 
322         /* we want to avoid races with nfnl_acct_find_get. */
323         if (atomic_dec_and_test(&cur->refcnt)) {
324                 /* We are protected by nfnl mutex. */
325                 list_del_rcu(&cur->head);
326                 kfree_rcu(cur, rcu_head);
327         } else {
328                 /* still in use, restore reference counter. */
329                 atomic_inc(&cur->refcnt);
330                 ret = -EBUSY;
331         }
332         return ret;
333 }
334 
335 static int
336 nfnl_acct_del(struct sock *nfnl, struct sk_buff *skb,
337              const struct nlmsghdr *nlh, const struct nlattr * const tb[])
338 {
339         char *acct_name;
340         struct nf_acct *cur;
341         int ret = -ENOENT;
342 
343         if (!tb[NFACCT_NAME]) {
344                 list_for_each_entry(cur, &nfnl_acct_list, head)
345                         nfnl_acct_try_del(cur);
346 
347                 return 0;
348         }
349         acct_name = nla_data(tb[NFACCT_NAME]);
350 
351         list_for_each_entry(cur, &nfnl_acct_list, head) {
352                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
353                         continue;
354 
355                 ret = nfnl_acct_try_del(cur);
356                 if (ret < 0)
357                         return ret;
358 
359                 break;
360         }
361         return ret;
362 }
363 
364 static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
365         [NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
366         [NFACCT_BYTES] = { .type = NLA_U64 },
367         [NFACCT_PKTS] = { .type = NLA_U64 },
368         [NFACCT_FLAGS] = { .type = NLA_U32 },
369         [NFACCT_QUOTA] = { .type = NLA_U64 },
370         [NFACCT_FILTER] = {.type = NLA_NESTED },
371 };
372 
373 static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
374         [NFNL_MSG_ACCT_NEW]             = { .call = nfnl_acct_new,
375                                             .attr_count = NFACCT_MAX,
376                                             .policy = nfnl_acct_policy },
377         [NFNL_MSG_ACCT_GET]             = { .call = nfnl_acct_get,
378                                             .attr_count = NFACCT_MAX,
379                                             .policy = nfnl_acct_policy },
380         [NFNL_MSG_ACCT_GET_CTRZERO]     = { .call = nfnl_acct_get,
381                                             .attr_count = NFACCT_MAX,
382                                             .policy = nfnl_acct_policy },
383         [NFNL_MSG_ACCT_DEL]             = { .call = nfnl_acct_del,
384                                             .attr_count = NFACCT_MAX,
385                                             .policy = nfnl_acct_policy },
386 };
387 
388 static const struct nfnetlink_subsystem nfnl_acct_subsys = {
389         .name                           = "acct",
390         .subsys_id                      = NFNL_SUBSYS_ACCT,
391         .cb_count                       = NFNL_MSG_ACCT_MAX,
392         .cb                             = nfnl_acct_cb,
393 };
394 
395 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
396 
397 struct nf_acct *nfnl_acct_find_get(const char *acct_name)
398 {
399         struct nf_acct *cur, *acct = NULL;
400 
401         rcu_read_lock();
402         list_for_each_entry_rcu(cur, &nfnl_acct_list, head) {
403                 if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
404                         continue;
405 
406                 if (!try_module_get(THIS_MODULE))
407                         goto err;
408 
409                 if (!atomic_inc_not_zero(&cur->refcnt)) {
410                         module_put(THIS_MODULE);
411                         goto err;
412                 }
413 
414                 acct = cur;
415                 break;
416         }
417 err:
418         rcu_read_unlock();
419         return acct;
420 }
421 EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
422 
423 void nfnl_acct_put(struct nf_acct *acct)
424 {
425         atomic_dec(&acct->refcnt);
426         module_put(THIS_MODULE);
427 }
428 EXPORT_SYMBOL_GPL(nfnl_acct_put);
429 
430 void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
431 {
432         atomic64_inc(&nfacct->pkts);
433         atomic64_add(skb->len, &nfacct->bytes);
434 }
435 EXPORT_SYMBOL_GPL(nfnl_acct_update);
436 
437 static void nfnl_overquota_report(struct nf_acct *nfacct)
438 {
439         int ret;
440         struct sk_buff *skb;
441 
442         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
443         if (skb == NULL)
444                 return;
445 
446         ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
447                                   nfacct);
448         if (ret <= 0) {
449                 kfree_skb(skb);
450                 return;
451         }
452         netlink_broadcast(init_net.nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
453                           GFP_ATOMIC);
454 }
455 
456 int nfnl_acct_overquota(const struct sk_buff *skb, struct nf_acct *nfacct)
457 {
458         u64 now;
459         u64 *quota;
460         int ret = NFACCT_UNDERQUOTA;
461 
462         /* no place here if we don't have a quota */
463         if (!(nfacct->flags & NFACCT_F_QUOTA))
464                 return NFACCT_NO_QUOTA;
465 
466         quota = (u64 *)nfacct->data;
467         now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
468                atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
469 
470         ret = now > *quota;
471 
472         if (now >= *quota &&
473             !test_and_set_bit(NFACCT_OVERQUOTA_BIT, &nfacct->flags)) {
474                 nfnl_overquota_report(nfacct);
475         }
476 
477         return ret;
478 }
479 EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
480 
481 static int __init nfnl_acct_init(void)
482 {
483         int ret;
484 
485         pr_info("nfnl_acct: registering with nfnetlink.\n");
486         ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
487         if (ret < 0) {
488                 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
489                 goto err_out;
490         }
491         return 0;
492 err_out:
493         return ret;
494 }
495 
496 static void __exit nfnl_acct_exit(void)
497 {
498         struct nf_acct *cur, *tmp;
499 
500         pr_info("nfnl_acct: unregistering from nfnetlink.\n");
501         nfnetlink_subsys_unregister(&nfnl_acct_subsys);
502 
503         list_for_each_entry_safe(cur, tmp, &nfnl_acct_list, head) {
504                 list_del_rcu(&cur->head);
505                 /* We are sure that our objects have no clients at this point,
506                  * it's safe to release them all without checking refcnt. */
507                 kfree_rcu(cur, rcu_head);
508         }
509 }
510 
511 module_init(nfnl_acct_init);
512 module_exit(nfnl_acct_exit);
513 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp