1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
4 * (C) 2011 Intra2net AG <https://www.intra2net.com>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/kernel.h>
9 #include <linux/skbuff.h>
10 #include <linux/atomic.h>
11 #include <linux/refcount.h>
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>
19 #include <net/netns/generic.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/nfnetlink.h>
23 #include <linux/netfilter/nfnetlink_acct.h>
25 MODULE_LICENSE("GPL");
26 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
27 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
33 struct list_head head
;
35 char name
[NFACCT_NAME_MAX
];
36 struct rcu_head rcu_head
;
40 struct nfacct_filter
{
45 struct nfnl_acct_net
{
46 struct list_head nfnl_acct_list
;
49 static unsigned int nfnl_acct_net_id __read_mostly
;
51 static inline struct nfnl_acct_net
*nfnl_acct_pernet(struct net
*net
)
53 return net_generic(net
, nfnl_acct_net_id
);
56 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
57 #define NFACCT_OVERQUOTA_BIT 2 /* NFACCT_F_OVERQUOTA */
59 static int nfnl_acct_new(struct sk_buff
*skb
, const struct nfnl_info
*info
,
60 const struct nlattr
* const tb
[])
62 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(info
->net
);
63 struct nf_acct
*nfacct
, *matching
= NULL
;
64 unsigned int size
= 0;
71 acct_name
= nla_data(tb
[NFACCT_NAME
]);
72 if (strlen(acct_name
) == 0)
75 list_for_each_entry(nfacct
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
76 if (strncmp(nfacct
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
79 if (info
->nlh
->nlmsg_flags
& NLM_F_EXCL
)
87 if (info
->nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
88 /* reset counters if you request a replacement. */
89 atomic64_set(&matching
->pkts
, 0);
90 atomic64_set(&matching
->bytes
, 0);
91 smp_mb__before_atomic();
92 /* reset overquota flag if quota is enabled. */
93 if ((matching
->flags
& NFACCT_F_QUOTA
))
94 clear_bit(NFACCT_OVERQUOTA_BIT
,
101 if (tb
[NFACCT_FLAGS
]) {
102 flags
= ntohl(nla_get_be32(tb
[NFACCT_FLAGS
]));
103 if (flags
& ~NFACCT_F_QUOTA
)
105 if ((flags
& NFACCT_F_QUOTA
) == NFACCT_F_QUOTA
)
107 if (flags
& NFACCT_F_OVERQUOTA
)
109 if ((flags
& NFACCT_F_QUOTA
) && !tb
[NFACCT_QUOTA
])
115 nfacct
= kzalloc(sizeof(struct nf_acct
) + size
, GFP_KERNEL
);
119 if (flags
& NFACCT_F_QUOTA
) {
120 u64
*quota
= (u64
*)nfacct
->data
;
122 *quota
= be64_to_cpu(nla_get_be64(tb
[NFACCT_QUOTA
]));
123 nfacct
->flags
= flags
;
126 nla_strscpy(nfacct
->name
, tb
[NFACCT_NAME
], NFACCT_NAME_MAX
);
128 if (tb
[NFACCT_BYTES
]) {
129 atomic64_set(&nfacct
->bytes
,
130 be64_to_cpu(nla_get_be64(tb
[NFACCT_BYTES
])));
132 if (tb
[NFACCT_PKTS
]) {
133 atomic64_set(&nfacct
->pkts
,
134 be64_to_cpu(nla_get_be64(tb
[NFACCT_PKTS
])));
136 refcount_set(&nfacct
->refcnt
, 1);
137 list_add_tail_rcu(&nfacct
->head
, &nfnl_acct_net
->nfnl_acct_list
);
142 nfnl_acct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
143 int event
, struct nf_acct
*acct
)
145 struct nlmsghdr
*nlh
;
146 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
150 event
= nfnl_msg_type(NFNL_SUBSYS_ACCT
, event
);
151 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
, AF_UNSPEC
,
156 if (nla_put_string(skb
, NFACCT_NAME
, acct
->name
))
157 goto nla_put_failure
;
159 old_flags
= acct
->flags
;
160 if (type
== NFNL_MSG_ACCT_GET_CTRZERO
) {
161 pkts
= atomic64_xchg(&acct
->pkts
, 0);
162 bytes
= atomic64_xchg(&acct
->bytes
, 0);
163 smp_mb__before_atomic();
164 if (acct
->flags
& NFACCT_F_QUOTA
)
165 clear_bit(NFACCT_OVERQUOTA_BIT
, &acct
->flags
);
167 pkts
= atomic64_read(&acct
->pkts
);
168 bytes
= atomic64_read(&acct
->bytes
);
170 if (nla_put_be64(skb
, NFACCT_PKTS
, cpu_to_be64(pkts
),
172 nla_put_be64(skb
, NFACCT_BYTES
, cpu_to_be64(bytes
),
174 nla_put_be32(skb
, NFACCT_USE
, htonl(refcount_read(&acct
->refcnt
))))
175 goto nla_put_failure
;
176 if (acct
->flags
& NFACCT_F_QUOTA
) {
177 u64
*quota
= (u64
*)acct
->data
;
179 if (nla_put_be32(skb
, NFACCT_FLAGS
, htonl(old_flags
)) ||
180 nla_put_be64(skb
, NFACCT_QUOTA
, cpu_to_be64(*quota
),
182 goto nla_put_failure
;
189 nlmsg_cancel(skb
, nlh
);
194 nfnl_acct_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
196 struct net
*net
= sock_net(skb
->sk
);
197 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
198 struct nf_acct
*cur
, *last
;
199 const struct nfacct_filter
*filter
= cb
->data
;
204 last
= (struct nf_acct
*)cb
->args
[1];
209 list_for_each_entry_rcu(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
217 if (filter
&& (cur
->flags
& filter
->mask
) != filter
->value
)
220 if (nfnl_acct_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
222 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
223 NFNL_MSG_ACCT_NEW
, cur
) < 0) {
224 cb
->args
[1] = (unsigned long)cur
;
234 static int nfnl_acct_done(struct netlink_callback
*cb
)
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
},
245 static int nfnl_acct_start(struct netlink_callback
*cb
)
247 const struct nlattr
*const attr
= cb
->data
;
248 struct nlattr
*tb
[NFACCT_FILTER_MAX
+ 1];
249 struct nfacct_filter
*filter
;
255 err
= nla_parse_nested_deprecated(tb
, NFACCT_FILTER_MAX
, attr
,
256 filter_policy
, NULL
);
260 if (!tb
[NFACCT_FILTER_MASK
] || !tb
[NFACCT_FILTER_VALUE
])
263 filter
= kzalloc(sizeof(struct nfacct_filter
), GFP_KERNEL
);
267 filter
->mask
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_MASK
]));
268 filter
->value
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_VALUE
]));
274 static int nfnl_acct_get(struct sk_buff
*skb
, const struct nfnl_info
*info
,
275 const struct nlattr
* const tb
[])
277 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(info
->net
);
282 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
283 struct netlink_dump_control c
= {
284 .dump
= nfnl_acct_dump
,
285 .start
= nfnl_acct_start
,
286 .done
= nfnl_acct_done
,
287 .data
= (void *)tb
[NFACCT_FILTER
],
290 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
293 if (!tb
[NFACCT_NAME
])
295 acct_name
= nla_data(tb
[NFACCT_NAME
]);
297 list_for_each_entry(cur
, &nfnl_acct_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
,
310 info
->nlh
->nlmsg_seq
,
311 NFNL_MSG_TYPE(info
->nlh
->nlmsg_type
),
312 NFNL_MSG_ACCT_NEW
, cur
);
318 ret
= nfnetlink_unicast(skb2
, info
->net
, NETLINK_CB(skb
).portid
);
325 /* try to delete object, fail if it is still in use. */
326 static int nfnl_acct_try_del(struct nf_acct
*cur
)
330 /* We want to avoid races with nfnl_acct_put. So only when the current
331 * refcnt is 1, we decrease it to 0.
333 if (refcount_dec_if_one(&cur
->refcnt
)) {
334 /* We are protected by nfnl mutex. */
335 list_del_rcu(&cur
->head
);
336 kfree_rcu(cur
, rcu_head
);
343 static int nfnl_acct_del(struct sk_buff
*skb
, const struct nfnl_info
*info
,
344 const struct nlattr
* const tb
[])
346 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(info
->net
);
347 struct nf_acct
*cur
, *tmp
;
351 if (!tb
[NFACCT_NAME
]) {
352 list_for_each_entry_safe(cur
, tmp
, &nfnl_acct_net
->nfnl_acct_list
, head
)
353 nfnl_acct_try_del(cur
);
357 acct_name
= nla_data(tb
[NFACCT_NAME
]);
359 list_for_each_entry(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
360 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
363 ret
= nfnl_acct_try_del(cur
);
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
},
376 [NFACCT_FLAGS
] = { .type
= NLA_U32
},
377 [NFACCT_QUOTA
] = { .type
= NLA_U64
},
378 [NFACCT_FILTER
] = {.type
= NLA_NESTED
},
381 static const struct nfnl_callback nfnl_acct_cb
[NFNL_MSG_ACCT_MAX
] = {
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
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
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
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
408 static const struct nfnetlink_subsystem nfnl_acct_subsys
= {
410 .subsys_id
= NFNL_SUBSYS_ACCT
,
411 .cb_count
= NFNL_MSG_ACCT_MAX
,
415 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT
);
417 struct nf_acct
*nfnl_acct_find_get(struct net
*net
, const char *acct_name
)
419 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
420 struct nf_acct
*cur
, *acct
= NULL
;
423 list_for_each_entry_rcu(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
424 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
427 if (!try_module_get(THIS_MODULE
))
430 if (!refcount_inc_not_zero(&cur
->refcnt
)) {
431 module_put(THIS_MODULE
);
442 EXPORT_SYMBOL_GPL(nfnl_acct_find_get
);
444 void nfnl_acct_put(struct nf_acct
*acct
)
446 if (refcount_dec_and_test(&acct
->refcnt
))
447 kfree_rcu(acct
, rcu_head
);
449 module_put(THIS_MODULE
);
451 EXPORT_SYMBOL_GPL(nfnl_acct_put
);
453 void nfnl_acct_update(const struct sk_buff
*skb
, struct nf_acct
*nfacct
)
455 atomic64_inc(&nfacct
->pkts
);
456 atomic64_add(skb
->len
, &nfacct
->bytes
);
458 EXPORT_SYMBOL_GPL(nfnl_acct_update
);
460 static void nfnl_overquota_report(struct net
*net
, struct nf_acct
*nfacct
)
465 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
469 ret
= nfnl_acct_fill_info(skb
, 0, 0, NFNL_MSG_ACCT_OVERQUOTA
, 0,
475 nfnetlink_broadcast(net
, skb
, 0, NFNLGRP_ACCT_QUOTA
, GFP_ATOMIC
);
478 int nfnl_acct_overquota(struct net
*net
, struct nf_acct
*nfacct
)
482 int ret
= NFACCT_UNDERQUOTA
;
484 /* no place here if we don't have a quota */
485 if (!(nfacct
->flags
& NFACCT_F_QUOTA
))
486 return NFACCT_NO_QUOTA
;
488 quota
= (u64
*)nfacct
->data
;
489 now
= (nfacct
->flags
& NFACCT_F_QUOTA_PKTS
) ?
490 atomic64_read(&nfacct
->pkts
) : atomic64_read(&nfacct
->bytes
);
495 !test_and_set_bit(NFACCT_OVERQUOTA_BIT
, &nfacct
->flags
)) {
496 nfnl_overquota_report(net
, nfacct
);
501 EXPORT_SYMBOL_GPL(nfnl_acct_overquota
);
503 static int __net_init
nfnl_acct_net_init(struct net
*net
)
505 INIT_LIST_HEAD(&nfnl_acct_pernet(net
)->nfnl_acct_list
);
510 static void __net_exit
nfnl_acct_net_exit(struct net
*net
)
512 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
513 struct nf_acct
*cur
, *tmp
;
515 list_for_each_entry_safe(cur
, tmp
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
516 list_del_rcu(&cur
->head
);
518 if (refcount_dec_and_test(&cur
->refcnt
))
519 kfree_rcu(cur
, rcu_head
);
523 static struct pernet_operations nfnl_acct_ops
= {
524 .init
= nfnl_acct_net_init
,
525 .exit
= nfnl_acct_net_exit
,
526 .id
= &nfnl_acct_net_id
,
527 .size
= sizeof(struct nfnl_acct_net
),
530 static int __init
nfnl_acct_init(void)
534 ret
= register_pernet_subsys(&nfnl_acct_ops
);
536 pr_err("nfnl_acct_init: failed to register pernet ops\n");
540 ret
= nfnetlink_subsys_register(&nfnl_acct_subsys
);
542 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
548 unregister_pernet_subsys(&nfnl_acct_ops
);
553 static void __exit
nfnl_acct_exit(void)
555 nfnetlink_subsys_unregister(&nfnl_acct_subsys
);
556 unregister_pernet_subsys(&nfnl_acct_ops
);
559 module_init(nfnl_acct_init
);
560 module_exit(nfnl_acct_exit
);