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 net
*net
, struct sock
*nfnl
,
60 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
61 const struct nlattr
* const tb
[],
62 struct netlink_ext_ack
*extack
)
64 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
65 struct nf_acct
*nfacct
, *matching
= NULL
;
67 unsigned int size
= 0;
73 acct_name
= nla_data(tb
[NFACCT_NAME
]);
74 if (strlen(acct_name
) == 0)
77 list_for_each_entry(nfacct
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
78 if (strncmp(nfacct
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
81 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
89 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
90 /* reset counters if you request a replacement. */
91 atomic64_set(&matching
->pkts
, 0);
92 atomic64_set(&matching
->bytes
, 0);
93 smp_mb__before_atomic();
94 /* reset overquota flag if quota is enabled. */
95 if ((matching
->flags
& NFACCT_F_QUOTA
))
96 clear_bit(NFACCT_OVERQUOTA_BIT
,
103 if (tb
[NFACCT_FLAGS
]) {
104 flags
= ntohl(nla_get_be32(tb
[NFACCT_FLAGS
]));
105 if (flags
& ~NFACCT_F_QUOTA
)
107 if ((flags
& NFACCT_F_QUOTA
) == NFACCT_F_QUOTA
)
109 if (flags
& NFACCT_F_OVERQUOTA
)
111 if ((flags
& NFACCT_F_QUOTA
) && !tb
[NFACCT_QUOTA
])
117 nfacct
= kzalloc(sizeof(struct nf_acct
) + size
, GFP_KERNEL
);
121 if (flags
& NFACCT_F_QUOTA
) {
122 u64
*quota
= (u64
*)nfacct
->data
;
124 *quota
= be64_to_cpu(nla_get_be64(tb
[NFACCT_QUOTA
]));
125 nfacct
->flags
= flags
;
128 nla_strscpy(nfacct
->name
, tb
[NFACCT_NAME
], NFACCT_NAME_MAX
);
130 if (tb
[NFACCT_BYTES
]) {
131 atomic64_set(&nfacct
->bytes
,
132 be64_to_cpu(nla_get_be64(tb
[NFACCT_BYTES
])));
134 if (tb
[NFACCT_PKTS
]) {
135 atomic64_set(&nfacct
->pkts
,
136 be64_to_cpu(nla_get_be64(tb
[NFACCT_PKTS
])));
138 refcount_set(&nfacct
->refcnt
, 1);
139 list_add_tail_rcu(&nfacct
->head
, &nfnl_acct_net
->nfnl_acct_list
);
144 nfnl_acct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
145 int event
, struct nf_acct
*acct
)
147 struct nlmsghdr
*nlh
;
148 struct nfgenmsg
*nfmsg
;
149 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
153 event
= nfnl_msg_type(NFNL_SUBSYS_ACCT
, event
);
154 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
158 nfmsg
= nlmsg_data(nlh
);
159 nfmsg
->nfgen_family
= AF_UNSPEC
;
160 nfmsg
->version
= NFNETLINK_V0
;
163 if (nla_put_string(skb
, NFACCT_NAME
, acct
->name
))
164 goto nla_put_failure
;
166 old_flags
= acct
->flags
;
167 if (type
== NFNL_MSG_ACCT_GET_CTRZERO
) {
168 pkts
= atomic64_xchg(&acct
->pkts
, 0);
169 bytes
= atomic64_xchg(&acct
->bytes
, 0);
170 smp_mb__before_atomic();
171 if (acct
->flags
& NFACCT_F_QUOTA
)
172 clear_bit(NFACCT_OVERQUOTA_BIT
, &acct
->flags
);
174 pkts
= atomic64_read(&acct
->pkts
);
175 bytes
= atomic64_read(&acct
->bytes
);
177 if (nla_put_be64(skb
, NFACCT_PKTS
, cpu_to_be64(pkts
),
179 nla_put_be64(skb
, NFACCT_BYTES
, cpu_to_be64(bytes
),
181 nla_put_be32(skb
, NFACCT_USE
, htonl(refcount_read(&acct
->refcnt
))))
182 goto nla_put_failure
;
183 if (acct
->flags
& NFACCT_F_QUOTA
) {
184 u64
*quota
= (u64
*)acct
->data
;
186 if (nla_put_be32(skb
, NFACCT_FLAGS
, htonl(old_flags
)) ||
187 nla_put_be64(skb
, NFACCT_QUOTA
, cpu_to_be64(*quota
),
189 goto nla_put_failure
;
196 nlmsg_cancel(skb
, nlh
);
201 nfnl_acct_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
203 struct net
*net
= sock_net(skb
->sk
);
204 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
205 struct nf_acct
*cur
, *last
;
206 const struct nfacct_filter
*filter
= cb
->data
;
211 last
= (struct nf_acct
*)cb
->args
[1];
216 list_for_each_entry_rcu(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
224 if (filter
&& (cur
->flags
& filter
->mask
) != filter
->value
)
227 if (nfnl_acct_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
229 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
230 NFNL_MSG_ACCT_NEW
, cur
) < 0) {
231 cb
->args
[1] = (unsigned long)cur
;
241 static int nfnl_acct_done(struct netlink_callback
*cb
)
247 static const struct nla_policy filter_policy
[NFACCT_FILTER_MAX
+ 1] = {
248 [NFACCT_FILTER_MASK
] = { .type
= NLA_U32
},
249 [NFACCT_FILTER_VALUE
] = { .type
= NLA_U32
},
252 static int nfnl_acct_start(struct netlink_callback
*cb
)
254 const struct nlattr
*const attr
= cb
->data
;
255 struct nlattr
*tb
[NFACCT_FILTER_MAX
+ 1];
256 struct nfacct_filter
*filter
;
262 err
= nla_parse_nested_deprecated(tb
, NFACCT_FILTER_MAX
, attr
,
263 filter_policy
, NULL
);
267 if (!tb
[NFACCT_FILTER_MASK
] || !tb
[NFACCT_FILTER_VALUE
])
270 filter
= kzalloc(sizeof(struct nfacct_filter
), GFP_KERNEL
);
274 filter
->mask
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_MASK
]));
275 filter
->value
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_VALUE
]));
281 static int nfnl_acct_get(struct net
*net
, struct sock
*nfnl
,
282 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
283 const struct nlattr
* const tb
[],
284 struct netlink_ext_ack
*extack
)
286 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
291 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
292 struct netlink_dump_control c
= {
293 .dump
= nfnl_acct_dump
,
294 .start
= nfnl_acct_start
,
295 .done
= nfnl_acct_done
,
296 .data
= (void *)tb
[NFACCT_FILTER
],
299 return netlink_dump_start(nfnl
, skb
, nlh
, &c
);
302 if (!tb
[NFACCT_NAME
])
304 acct_name
= nla_data(tb
[NFACCT_NAME
]);
306 list_for_each_entry(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
307 struct sk_buff
*skb2
;
309 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
312 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
318 ret
= nfnl_acct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
320 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
321 NFNL_MSG_ACCT_NEW
, cur
);
326 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
331 /* this avoids a loop in nfnetlink. */
332 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
337 /* try to delete object, fail if it is still in use. */
338 static int nfnl_acct_try_del(struct nf_acct
*cur
)
342 /* We want to avoid races with nfnl_acct_put. So only when the current
343 * refcnt is 1, we decrease it to 0.
345 if (refcount_dec_if_one(&cur
->refcnt
)) {
346 /* We are protected by nfnl mutex. */
347 list_del_rcu(&cur
->head
);
348 kfree_rcu(cur
, rcu_head
);
355 static int nfnl_acct_del(struct net
*net
, struct sock
*nfnl
,
356 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
357 const struct nlattr
* const tb
[],
358 struct netlink_ext_ack
*extack
)
360 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
361 struct nf_acct
*cur
, *tmp
;
365 if (!tb
[NFACCT_NAME
]) {
366 list_for_each_entry_safe(cur
, tmp
, &nfnl_acct_net
->nfnl_acct_list
, head
)
367 nfnl_acct_try_del(cur
);
371 acct_name
= nla_data(tb
[NFACCT_NAME
]);
373 list_for_each_entry(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
374 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
377 ret
= nfnl_acct_try_del(cur
);
386 static const struct nla_policy nfnl_acct_policy
[NFACCT_MAX
+1] = {
387 [NFACCT_NAME
] = { .type
= NLA_NUL_STRING
, .len
= NFACCT_NAME_MAX
-1 },
388 [NFACCT_BYTES
] = { .type
= NLA_U64
},
389 [NFACCT_PKTS
] = { .type
= NLA_U64
},
390 [NFACCT_FLAGS
] = { .type
= NLA_U32
},
391 [NFACCT_QUOTA
] = { .type
= NLA_U64
},
392 [NFACCT_FILTER
] = {.type
= NLA_NESTED
},
395 static const struct nfnl_callback nfnl_acct_cb
[NFNL_MSG_ACCT_MAX
] = {
396 [NFNL_MSG_ACCT_NEW
] = { .call
= nfnl_acct_new
,
397 .attr_count
= NFACCT_MAX
,
398 .policy
= nfnl_acct_policy
},
399 [NFNL_MSG_ACCT_GET
] = { .call
= nfnl_acct_get
,
400 .attr_count
= NFACCT_MAX
,
401 .policy
= nfnl_acct_policy
},
402 [NFNL_MSG_ACCT_GET_CTRZERO
] = { .call
= nfnl_acct_get
,
403 .attr_count
= NFACCT_MAX
,
404 .policy
= nfnl_acct_policy
},
405 [NFNL_MSG_ACCT_DEL
] = { .call
= nfnl_acct_del
,
406 .attr_count
= NFACCT_MAX
,
407 .policy
= nfnl_acct_policy
},
410 static const struct nfnetlink_subsystem nfnl_acct_subsys
= {
412 .subsys_id
= NFNL_SUBSYS_ACCT
,
413 .cb_count
= NFNL_MSG_ACCT_MAX
,
417 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT
);
419 struct nf_acct
*nfnl_acct_find_get(struct net
*net
, const char *acct_name
)
421 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
422 struct nf_acct
*cur
, *acct
= NULL
;
425 list_for_each_entry_rcu(cur
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
426 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
429 if (!try_module_get(THIS_MODULE
))
432 if (!refcount_inc_not_zero(&cur
->refcnt
)) {
433 module_put(THIS_MODULE
);
444 EXPORT_SYMBOL_GPL(nfnl_acct_find_get
);
446 void nfnl_acct_put(struct nf_acct
*acct
)
448 if (refcount_dec_and_test(&acct
->refcnt
))
449 kfree_rcu(acct
, rcu_head
);
451 module_put(THIS_MODULE
);
453 EXPORT_SYMBOL_GPL(nfnl_acct_put
);
455 void nfnl_acct_update(const struct sk_buff
*skb
, struct nf_acct
*nfacct
)
457 atomic64_inc(&nfacct
->pkts
);
458 atomic64_add(skb
->len
, &nfacct
->bytes
);
460 EXPORT_SYMBOL_GPL(nfnl_acct_update
);
462 static void nfnl_overquota_report(struct net
*net
, struct nf_acct
*nfacct
)
467 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
471 ret
= nfnl_acct_fill_info(skb
, 0, 0, NFNL_MSG_ACCT_OVERQUOTA
, 0,
477 netlink_broadcast(net
->nfnl
, skb
, 0, NFNLGRP_ACCT_QUOTA
,
481 int nfnl_acct_overquota(struct net
*net
, struct nf_acct
*nfacct
)
485 int ret
= NFACCT_UNDERQUOTA
;
487 /* no place here if we don't have a quota */
488 if (!(nfacct
->flags
& NFACCT_F_QUOTA
))
489 return NFACCT_NO_QUOTA
;
491 quota
= (u64
*)nfacct
->data
;
492 now
= (nfacct
->flags
& NFACCT_F_QUOTA_PKTS
) ?
493 atomic64_read(&nfacct
->pkts
) : atomic64_read(&nfacct
->bytes
);
498 !test_and_set_bit(NFACCT_OVERQUOTA_BIT
, &nfacct
->flags
)) {
499 nfnl_overquota_report(net
, nfacct
);
504 EXPORT_SYMBOL_GPL(nfnl_acct_overquota
);
506 static int __net_init
nfnl_acct_net_init(struct net
*net
)
508 INIT_LIST_HEAD(&nfnl_acct_pernet(net
)->nfnl_acct_list
);
513 static void __net_exit
nfnl_acct_net_exit(struct net
*net
)
515 struct nfnl_acct_net
*nfnl_acct_net
= nfnl_acct_pernet(net
);
516 struct nf_acct
*cur
, *tmp
;
518 list_for_each_entry_safe(cur
, tmp
, &nfnl_acct_net
->nfnl_acct_list
, head
) {
519 list_del_rcu(&cur
->head
);
521 if (refcount_dec_and_test(&cur
->refcnt
))
522 kfree_rcu(cur
, rcu_head
);
526 static struct pernet_operations nfnl_acct_ops
= {
527 .init
= nfnl_acct_net_init
,
528 .exit
= nfnl_acct_net_exit
,
529 .id
= &nfnl_acct_net_id
,
530 .size
= sizeof(struct nfnl_acct_net
),
533 static int __init
nfnl_acct_init(void)
537 ret
= register_pernet_subsys(&nfnl_acct_ops
);
539 pr_err("nfnl_acct_init: failed to register pernet ops\n");
543 ret
= nfnetlink_subsys_register(&nfnl_acct_subsys
);
545 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
551 unregister_pernet_subsys(&nfnl_acct_ops
);
556 static void __exit
nfnl_acct_exit(void)
558 nfnetlink_subsys_unregister(&nfnl_acct_subsys
);
559 unregister_pernet_subsys(&nfnl_acct_ops
);
562 module_init(nfnl_acct_init
);
563 module_exit(nfnl_acct_exit
);