2 * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
3 * (C) 2011 Intra2net AG <http://www.intra2net.com>
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).
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/refcount.h>
15 #include <linux/netlink.h>
16 #include <linux/rculist.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <net/netlink.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/nfnetlink.h>
25 #include <linux/netfilter/nfnetlink_acct.h>
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
29 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
35 struct list_head head
;
37 char name
[NFACCT_NAME_MAX
];
38 struct rcu_head rcu_head
;
42 struct nfacct_filter
{
47 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
48 #define NFACCT_OVERQUOTA_BIT 2 /* NFACCT_F_OVERQUOTA */
50 static int nfnl_acct_new(struct net
*net
, struct sock
*nfnl
,
51 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
52 const struct nlattr
* const tb
[],
53 struct netlink_ext_ack
*extack
)
55 struct nf_acct
*nfacct
, *matching
= NULL
;
57 unsigned int size
= 0;
63 acct_name
= nla_data(tb
[NFACCT_NAME
]);
64 if (strlen(acct_name
) == 0)
67 list_for_each_entry(nfacct
, &net
->nfnl_acct_list
, head
) {
68 if (strncmp(nfacct
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
71 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
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
,
93 if (tb
[NFACCT_FLAGS
]) {
94 flags
= ntohl(nla_get_be32(tb
[NFACCT_FLAGS
]));
95 if (flags
& ~NFACCT_F_QUOTA
)
97 if ((flags
& NFACCT_F_QUOTA
) == NFACCT_F_QUOTA
)
99 if (flags
& NFACCT_F_OVERQUOTA
)
101 if ((flags
& NFACCT_F_QUOTA
) && !tb
[NFACCT_QUOTA
])
107 nfacct
= kzalloc(sizeof(struct nf_acct
) + size
, GFP_KERNEL
);
111 if (flags
& NFACCT_F_QUOTA
) {
112 u64
*quota
= (u64
*)nfacct
->data
;
114 *quota
= be64_to_cpu(nla_get_be64(tb
[NFACCT_QUOTA
]));
115 nfacct
->flags
= flags
;
118 strncpy(nfacct
->name
, nla_data(tb
[NFACCT_NAME
]), NFACCT_NAME_MAX
);
120 if (tb
[NFACCT_BYTES
]) {
121 atomic64_set(&nfacct
->bytes
,
122 be64_to_cpu(nla_get_be64(tb
[NFACCT_BYTES
])));
124 if (tb
[NFACCT_PKTS
]) {
125 atomic64_set(&nfacct
->pkts
,
126 be64_to_cpu(nla_get_be64(tb
[NFACCT_PKTS
])));
128 refcount_set(&nfacct
->refcnt
, 1);
129 list_add_tail_rcu(&nfacct
->head
, &net
->nfnl_acct_list
);
134 nfnl_acct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
135 int event
, struct nf_acct
*acct
)
137 struct nlmsghdr
*nlh
;
138 struct nfgenmsg
*nfmsg
;
139 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
143 event
= nfnl_msg_type(NFNL_SUBSYS_ACCT
, event
);
144 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
148 nfmsg
= nlmsg_data(nlh
);
149 nfmsg
->nfgen_family
= AF_UNSPEC
;
150 nfmsg
->version
= NFNETLINK_V0
;
153 if (nla_put_string(skb
, NFACCT_NAME
, acct
->name
))
154 goto nla_put_failure
;
156 old_flags
= acct
->flags
;
157 if (type
== NFNL_MSG_ACCT_GET_CTRZERO
) {
158 pkts
= atomic64_xchg(&acct
->pkts
, 0);
159 bytes
= atomic64_xchg(&acct
->bytes
, 0);
160 smp_mb__before_atomic();
161 if (acct
->flags
& NFACCT_F_QUOTA
)
162 clear_bit(NFACCT_OVERQUOTA_BIT
, &acct
->flags
);
164 pkts
= atomic64_read(&acct
->pkts
);
165 bytes
= atomic64_read(&acct
->bytes
);
167 if (nla_put_be64(skb
, NFACCT_PKTS
, cpu_to_be64(pkts
),
169 nla_put_be64(skb
, NFACCT_BYTES
, cpu_to_be64(bytes
),
171 nla_put_be32(skb
, NFACCT_USE
, htonl(refcount_read(&acct
->refcnt
))))
172 goto nla_put_failure
;
173 if (acct
->flags
& NFACCT_F_QUOTA
) {
174 u64
*quota
= (u64
*)acct
->data
;
176 if (nla_put_be32(skb
, NFACCT_FLAGS
, htonl(old_flags
)) ||
177 nla_put_be64(skb
, NFACCT_QUOTA
, cpu_to_be64(*quota
),
179 goto nla_put_failure
;
186 nlmsg_cancel(skb
, nlh
);
191 nfnl_acct_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
193 struct net
*net
= sock_net(skb
->sk
);
194 struct nf_acct
*cur
, *last
;
195 const struct nfacct_filter
*filter
= cb
->data
;
200 last
= (struct nf_acct
*)cb
->args
[1];
205 list_for_each_entry_rcu(cur
, &net
->nfnl_acct_list
, head
) {
213 if (filter
&& (cur
->flags
& filter
->mask
) != filter
->value
)
216 if (nfnl_acct_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
218 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
219 NFNL_MSG_ACCT_NEW
, cur
) < 0) {
220 cb
->args
[1] = (unsigned long)cur
;
230 static int nfnl_acct_done(struct netlink_callback
*cb
)
236 static const struct nla_policy filter_policy
[NFACCT_FILTER_MAX
+ 1] = {
237 [NFACCT_FILTER_MASK
] = { .type
= NLA_U32
},
238 [NFACCT_FILTER_VALUE
] = { .type
= NLA_U32
},
241 static struct nfacct_filter
*
242 nfacct_filter_alloc(const struct nlattr
* const attr
)
244 struct nfacct_filter
*filter
;
245 struct nlattr
*tb
[NFACCT_FILTER_MAX
+ 1];
248 err
= nla_parse_nested(tb
, NFACCT_FILTER_MAX
, attr
, filter_policy
,
253 if (!tb
[NFACCT_FILTER_MASK
] || !tb
[NFACCT_FILTER_VALUE
])
254 return ERR_PTR(-EINVAL
);
256 filter
= kzalloc(sizeof(struct nfacct_filter
), GFP_KERNEL
);
258 return ERR_PTR(-ENOMEM
);
260 filter
->mask
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_MASK
]));
261 filter
->value
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_VALUE
]));
266 static int nfnl_acct_get(struct net
*net
, struct sock
*nfnl
,
267 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
268 const struct nlattr
* const tb
[],
269 struct netlink_ext_ack
*extack
)
275 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
276 struct netlink_dump_control c
= {
277 .dump
= nfnl_acct_dump
,
278 .done
= nfnl_acct_done
,
281 if (tb
[NFACCT_FILTER
]) {
282 struct nfacct_filter
*filter
;
284 filter
= nfacct_filter_alloc(tb
[NFACCT_FILTER
]);
286 return PTR_ERR(filter
);
290 return netlink_dump_start(nfnl
, skb
, nlh
, &c
);
293 if (!tb
[NFACCT_NAME
])
295 acct_name
= nla_data(tb
[NFACCT_NAME
]);
297 list_for_each_entry(cur
, &net
->nfnl_acct_list
, head
) {
298 struct sk_buff
*skb2
;
300 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
303 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
309 ret
= nfnl_acct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
311 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
312 NFNL_MSG_ACCT_NEW
, cur
);
317 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
322 /* this avoids a loop in nfnetlink. */
323 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
328 /* try to delete object, fail if it is still in use. */
329 static int nfnl_acct_try_del(struct nf_acct
*cur
)
333 /* We want to avoid races with nfnl_acct_put. So only when the current
334 * refcnt is 1, we decrease it to 0.
336 if (refcount_dec_if_one(&cur
->refcnt
)) {
337 /* We are protected by nfnl mutex. */
338 list_del_rcu(&cur
->head
);
339 kfree_rcu(cur
, rcu_head
);
346 static int nfnl_acct_del(struct net
*net
, struct sock
*nfnl
,
347 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
348 const struct nlattr
* const tb
[],
349 struct netlink_ext_ack
*extack
)
351 struct nf_acct
*cur
, *tmp
;
355 if (!tb
[NFACCT_NAME
]) {
356 list_for_each_entry_safe(cur
, tmp
, &net
->nfnl_acct_list
, head
)
357 nfnl_acct_try_del(cur
);
361 acct_name
= nla_data(tb
[NFACCT_NAME
]);
363 list_for_each_entry(cur
, &net
->nfnl_acct_list
, head
) {
364 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
367 ret
= nfnl_acct_try_del(cur
);
376 static const struct nla_policy nfnl_acct_policy
[NFACCT_MAX
+1] = {
377 [NFACCT_NAME
] = { .type
= NLA_NUL_STRING
, .len
= NFACCT_NAME_MAX
-1 },
378 [NFACCT_BYTES
] = { .type
= NLA_U64
},
379 [NFACCT_PKTS
] = { .type
= NLA_U64
},
380 [NFACCT_FLAGS
] = { .type
= NLA_U32
},
381 [NFACCT_QUOTA
] = { .type
= NLA_U64
},
382 [NFACCT_FILTER
] = {.type
= NLA_NESTED
},
385 static const struct nfnl_callback nfnl_acct_cb
[NFNL_MSG_ACCT_MAX
] = {
386 [NFNL_MSG_ACCT_NEW
] = { .call
= nfnl_acct_new
,
387 .attr_count
= NFACCT_MAX
,
388 .policy
= nfnl_acct_policy
},
389 [NFNL_MSG_ACCT_GET
] = { .call
= nfnl_acct_get
,
390 .attr_count
= NFACCT_MAX
,
391 .policy
= nfnl_acct_policy
},
392 [NFNL_MSG_ACCT_GET_CTRZERO
] = { .call
= nfnl_acct_get
,
393 .attr_count
= NFACCT_MAX
,
394 .policy
= nfnl_acct_policy
},
395 [NFNL_MSG_ACCT_DEL
] = { .call
= nfnl_acct_del
,
396 .attr_count
= NFACCT_MAX
,
397 .policy
= nfnl_acct_policy
},
400 static const struct nfnetlink_subsystem nfnl_acct_subsys
= {
402 .subsys_id
= NFNL_SUBSYS_ACCT
,
403 .cb_count
= NFNL_MSG_ACCT_MAX
,
407 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT
);
409 struct nf_acct
*nfnl_acct_find_get(struct net
*net
, const char *acct_name
)
411 struct nf_acct
*cur
, *acct
= NULL
;
414 list_for_each_entry_rcu(cur
, &net
->nfnl_acct_list
, head
) {
415 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
418 if (!try_module_get(THIS_MODULE
))
421 if (!refcount_inc_not_zero(&cur
->refcnt
)) {
422 module_put(THIS_MODULE
);
433 EXPORT_SYMBOL_GPL(nfnl_acct_find_get
);
435 void nfnl_acct_put(struct nf_acct
*acct
)
437 if (refcount_dec_and_test(&acct
->refcnt
))
438 kfree_rcu(acct
, rcu_head
);
440 module_put(THIS_MODULE
);
442 EXPORT_SYMBOL_GPL(nfnl_acct_put
);
444 void nfnl_acct_update(const struct sk_buff
*skb
, struct nf_acct
*nfacct
)
446 atomic64_inc(&nfacct
->pkts
);
447 atomic64_add(skb
->len
, &nfacct
->bytes
);
449 EXPORT_SYMBOL_GPL(nfnl_acct_update
);
451 static void nfnl_overquota_report(struct net
*net
, struct nf_acct
*nfacct
)
456 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
460 ret
= nfnl_acct_fill_info(skb
, 0, 0, NFNL_MSG_ACCT_OVERQUOTA
, 0,
466 netlink_broadcast(net
->nfnl
, skb
, 0, NFNLGRP_ACCT_QUOTA
,
470 int nfnl_acct_overquota(struct net
*net
, const struct sk_buff
*skb
,
471 struct nf_acct
*nfacct
)
475 int ret
= NFACCT_UNDERQUOTA
;
477 /* no place here if we don't have a quota */
478 if (!(nfacct
->flags
& NFACCT_F_QUOTA
))
479 return NFACCT_NO_QUOTA
;
481 quota
= (u64
*)nfacct
->data
;
482 now
= (nfacct
->flags
& NFACCT_F_QUOTA_PKTS
) ?
483 atomic64_read(&nfacct
->pkts
) : atomic64_read(&nfacct
->bytes
);
488 !test_and_set_bit(NFACCT_OVERQUOTA_BIT
, &nfacct
->flags
)) {
489 nfnl_overquota_report(net
, nfacct
);
494 EXPORT_SYMBOL_GPL(nfnl_acct_overquota
);
496 static int __net_init
nfnl_acct_net_init(struct net
*net
)
498 INIT_LIST_HEAD(&net
->nfnl_acct_list
);
503 static void __net_exit
nfnl_acct_net_exit(struct net
*net
)
505 struct nf_acct
*cur
, *tmp
;
507 list_for_each_entry_safe(cur
, tmp
, &net
->nfnl_acct_list
, head
) {
508 list_del_rcu(&cur
->head
);
510 if (refcount_dec_and_test(&cur
->refcnt
))
511 kfree_rcu(cur
, rcu_head
);
515 static struct pernet_operations nfnl_acct_ops
= {
516 .init
= nfnl_acct_net_init
,
517 .exit
= nfnl_acct_net_exit
,
520 static int __init
nfnl_acct_init(void)
524 ret
= register_pernet_subsys(&nfnl_acct_ops
);
526 pr_err("nfnl_acct_init: failed to register pernet ops\n");
530 ret
= nfnetlink_subsys_register(&nfnl_acct_subsys
);
532 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
538 unregister_pernet_subsys(&nfnl_acct_ops
);
543 static void __exit
nfnl_acct_exit(void)
545 nfnetlink_subsys_unregister(&nfnl_acct_subsys
);
546 unregister_pernet_subsys(&nfnl_acct_ops
);
549 module_init(nfnl_acct_init
);
550 module_exit(nfnl_acct_exit
);