1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
4 * (C) 2011 Intra2net AG <http://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>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/nfnetlink.h>
22 #include <linux/netfilter/nfnetlink_acct.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
26 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
32 struct list_head head
;
34 char name
[NFACCT_NAME_MAX
];
35 struct rcu_head rcu_head
;
39 struct nfacct_filter
{
44 #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
45 #define NFACCT_OVERQUOTA_BIT 2 /* NFACCT_F_OVERQUOTA */
47 static int nfnl_acct_new(struct net
*net
, struct sock
*nfnl
,
48 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
49 const struct nlattr
* const tb
[],
50 struct netlink_ext_ack
*extack
)
52 struct nf_acct
*nfacct
, *matching
= NULL
;
54 unsigned int size
= 0;
60 acct_name
= nla_data(tb
[NFACCT_NAME
]);
61 if (strlen(acct_name
) == 0)
64 list_for_each_entry(nfacct
, &net
->nfnl_acct_list
, head
) {
65 if (strncmp(nfacct
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
68 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
76 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
77 /* reset counters if you request a replacement. */
78 atomic64_set(&matching
->pkts
, 0);
79 atomic64_set(&matching
->bytes
, 0);
80 smp_mb__before_atomic();
81 /* reset overquota flag if quota is enabled. */
82 if ((matching
->flags
& NFACCT_F_QUOTA
))
83 clear_bit(NFACCT_OVERQUOTA_BIT
,
90 if (tb
[NFACCT_FLAGS
]) {
91 flags
= ntohl(nla_get_be32(tb
[NFACCT_FLAGS
]));
92 if (flags
& ~NFACCT_F_QUOTA
)
94 if ((flags
& NFACCT_F_QUOTA
) == NFACCT_F_QUOTA
)
96 if (flags
& NFACCT_F_OVERQUOTA
)
98 if ((flags
& NFACCT_F_QUOTA
) && !tb
[NFACCT_QUOTA
])
104 nfacct
= kzalloc(sizeof(struct nf_acct
) + size
, GFP_KERNEL
);
108 if (flags
& NFACCT_F_QUOTA
) {
109 u64
*quota
= (u64
*)nfacct
->data
;
111 *quota
= be64_to_cpu(nla_get_be64(tb
[NFACCT_QUOTA
]));
112 nfacct
->flags
= flags
;
115 nla_strlcpy(nfacct
->name
, tb
[NFACCT_NAME
], NFACCT_NAME_MAX
);
117 if (tb
[NFACCT_BYTES
]) {
118 atomic64_set(&nfacct
->bytes
,
119 be64_to_cpu(nla_get_be64(tb
[NFACCT_BYTES
])));
121 if (tb
[NFACCT_PKTS
]) {
122 atomic64_set(&nfacct
->pkts
,
123 be64_to_cpu(nla_get_be64(tb
[NFACCT_PKTS
])));
125 refcount_set(&nfacct
->refcnt
, 1);
126 list_add_tail_rcu(&nfacct
->head
, &net
->nfnl_acct_list
);
131 nfnl_acct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
132 int event
, struct nf_acct
*acct
)
134 struct nlmsghdr
*nlh
;
135 struct nfgenmsg
*nfmsg
;
136 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
140 event
= nfnl_msg_type(NFNL_SUBSYS_ACCT
, event
);
141 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
145 nfmsg
= nlmsg_data(nlh
);
146 nfmsg
->nfgen_family
= AF_UNSPEC
;
147 nfmsg
->version
= NFNETLINK_V0
;
150 if (nla_put_string(skb
, NFACCT_NAME
, acct
->name
))
151 goto nla_put_failure
;
153 old_flags
= acct
->flags
;
154 if (type
== NFNL_MSG_ACCT_GET_CTRZERO
) {
155 pkts
= atomic64_xchg(&acct
->pkts
, 0);
156 bytes
= atomic64_xchg(&acct
->bytes
, 0);
157 smp_mb__before_atomic();
158 if (acct
->flags
& NFACCT_F_QUOTA
)
159 clear_bit(NFACCT_OVERQUOTA_BIT
, &acct
->flags
);
161 pkts
= atomic64_read(&acct
->pkts
);
162 bytes
= atomic64_read(&acct
->bytes
);
164 if (nla_put_be64(skb
, NFACCT_PKTS
, cpu_to_be64(pkts
),
166 nla_put_be64(skb
, NFACCT_BYTES
, cpu_to_be64(bytes
),
168 nla_put_be32(skb
, NFACCT_USE
, htonl(refcount_read(&acct
->refcnt
))))
169 goto nla_put_failure
;
170 if (acct
->flags
& NFACCT_F_QUOTA
) {
171 u64
*quota
= (u64
*)acct
->data
;
173 if (nla_put_be32(skb
, NFACCT_FLAGS
, htonl(old_flags
)) ||
174 nla_put_be64(skb
, NFACCT_QUOTA
, cpu_to_be64(*quota
),
176 goto nla_put_failure
;
183 nlmsg_cancel(skb
, nlh
);
188 nfnl_acct_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
190 struct net
*net
= sock_net(skb
->sk
);
191 struct nf_acct
*cur
, *last
;
192 const struct nfacct_filter
*filter
= cb
->data
;
197 last
= (struct nf_acct
*)cb
->args
[1];
202 list_for_each_entry_rcu(cur
, &net
->nfnl_acct_list
, head
) {
210 if (filter
&& (cur
->flags
& filter
->mask
) != filter
->value
)
213 if (nfnl_acct_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
215 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
216 NFNL_MSG_ACCT_NEW
, cur
) < 0) {
217 cb
->args
[1] = (unsigned long)cur
;
227 static int nfnl_acct_done(struct netlink_callback
*cb
)
233 static const struct nla_policy filter_policy
[NFACCT_FILTER_MAX
+ 1] = {
234 [NFACCT_FILTER_MASK
] = { .type
= NLA_U32
},
235 [NFACCT_FILTER_VALUE
] = { .type
= NLA_U32
},
238 static int nfnl_acct_start(struct netlink_callback
*cb
)
240 const struct nlattr
*const attr
= cb
->data
;
241 struct nlattr
*tb
[NFACCT_FILTER_MAX
+ 1];
242 struct nfacct_filter
*filter
;
248 err
= nla_parse_nested_deprecated(tb
, NFACCT_FILTER_MAX
, attr
,
249 filter_policy
, NULL
);
253 if (!tb
[NFACCT_FILTER_MASK
] || !tb
[NFACCT_FILTER_VALUE
])
256 filter
= kzalloc(sizeof(struct nfacct_filter
), GFP_KERNEL
);
260 filter
->mask
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_MASK
]));
261 filter
->value
= ntohl(nla_get_be32(tb
[NFACCT_FILTER_VALUE
]));
267 static int nfnl_acct_get(struct net
*net
, struct sock
*nfnl
,
268 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
269 const struct nlattr
* const tb
[],
270 struct netlink_ext_ack
*extack
)
276 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
277 struct netlink_dump_control c
= {
278 .dump
= nfnl_acct_dump
,
279 .start
= nfnl_acct_start
,
280 .done
= nfnl_acct_done
,
281 .data
= (void *)tb
[NFACCT_FILTER
],
284 return netlink_dump_start(nfnl
, skb
, nlh
, &c
);
287 if (!tb
[NFACCT_NAME
])
289 acct_name
= nla_data(tb
[NFACCT_NAME
]);
291 list_for_each_entry(cur
, &net
->nfnl_acct_list
, head
) {
292 struct sk_buff
*skb2
;
294 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
297 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
303 ret
= nfnl_acct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
305 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
306 NFNL_MSG_ACCT_NEW
, cur
);
311 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
316 /* this avoids a loop in nfnetlink. */
317 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
322 /* try to delete object, fail if it is still in use. */
323 static int nfnl_acct_try_del(struct nf_acct
*cur
)
327 /* We want to avoid races with nfnl_acct_put. So only when the current
328 * refcnt is 1, we decrease it to 0.
330 if (refcount_dec_if_one(&cur
->refcnt
)) {
331 /* We are protected by nfnl mutex. */
332 list_del_rcu(&cur
->head
);
333 kfree_rcu(cur
, rcu_head
);
340 static int nfnl_acct_del(struct net
*net
, struct sock
*nfnl
,
341 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
342 const struct nlattr
* const tb
[],
343 struct netlink_ext_ack
*extack
)
345 struct nf_acct
*cur
, *tmp
;
349 if (!tb
[NFACCT_NAME
]) {
350 list_for_each_entry_safe(cur
, tmp
, &net
->nfnl_acct_list
, head
)
351 nfnl_acct_try_del(cur
);
355 acct_name
= nla_data(tb
[NFACCT_NAME
]);
357 list_for_each_entry(cur
, &net
->nfnl_acct_list
, head
) {
358 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
361 ret
= nfnl_acct_try_del(cur
);
370 static const struct nla_policy nfnl_acct_policy
[NFACCT_MAX
+1] = {
371 [NFACCT_NAME
] = { .type
= NLA_NUL_STRING
, .len
= NFACCT_NAME_MAX
-1 },
372 [NFACCT_BYTES
] = { .type
= NLA_U64
},
373 [NFACCT_PKTS
] = { .type
= NLA_U64
},
374 [NFACCT_FLAGS
] = { .type
= NLA_U32
},
375 [NFACCT_QUOTA
] = { .type
= NLA_U64
},
376 [NFACCT_FILTER
] = {.type
= NLA_NESTED
},
379 static const struct nfnl_callback nfnl_acct_cb
[NFNL_MSG_ACCT_MAX
] = {
380 [NFNL_MSG_ACCT_NEW
] = { .call
= nfnl_acct_new
,
381 .attr_count
= NFACCT_MAX
,
382 .policy
= nfnl_acct_policy
},
383 [NFNL_MSG_ACCT_GET
] = { .call
= nfnl_acct_get
,
384 .attr_count
= NFACCT_MAX
,
385 .policy
= nfnl_acct_policy
},
386 [NFNL_MSG_ACCT_GET_CTRZERO
] = { .call
= nfnl_acct_get
,
387 .attr_count
= NFACCT_MAX
,
388 .policy
= nfnl_acct_policy
},
389 [NFNL_MSG_ACCT_DEL
] = { .call
= nfnl_acct_del
,
390 .attr_count
= NFACCT_MAX
,
391 .policy
= nfnl_acct_policy
},
394 static const struct nfnetlink_subsystem nfnl_acct_subsys
= {
396 .subsys_id
= NFNL_SUBSYS_ACCT
,
397 .cb_count
= NFNL_MSG_ACCT_MAX
,
401 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT
);
403 struct nf_acct
*nfnl_acct_find_get(struct net
*net
, const char *acct_name
)
405 struct nf_acct
*cur
, *acct
= NULL
;
408 list_for_each_entry_rcu(cur
, &net
->nfnl_acct_list
, head
) {
409 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
412 if (!try_module_get(THIS_MODULE
))
415 if (!refcount_inc_not_zero(&cur
->refcnt
)) {
416 module_put(THIS_MODULE
);
427 EXPORT_SYMBOL_GPL(nfnl_acct_find_get
);
429 void nfnl_acct_put(struct nf_acct
*acct
)
431 if (refcount_dec_and_test(&acct
->refcnt
))
432 kfree_rcu(acct
, rcu_head
);
434 module_put(THIS_MODULE
);
436 EXPORT_SYMBOL_GPL(nfnl_acct_put
);
438 void nfnl_acct_update(const struct sk_buff
*skb
, struct nf_acct
*nfacct
)
440 atomic64_inc(&nfacct
->pkts
);
441 atomic64_add(skb
->len
, &nfacct
->bytes
);
443 EXPORT_SYMBOL_GPL(nfnl_acct_update
);
445 static void nfnl_overquota_report(struct net
*net
, struct nf_acct
*nfacct
)
450 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
454 ret
= nfnl_acct_fill_info(skb
, 0, 0, NFNL_MSG_ACCT_OVERQUOTA
, 0,
460 netlink_broadcast(net
->nfnl
, skb
, 0, NFNLGRP_ACCT_QUOTA
,
464 int nfnl_acct_overquota(struct net
*net
, struct nf_acct
*nfacct
)
468 int ret
= NFACCT_UNDERQUOTA
;
470 /* no place here if we don't have a quota */
471 if (!(nfacct
->flags
& NFACCT_F_QUOTA
))
472 return NFACCT_NO_QUOTA
;
474 quota
= (u64
*)nfacct
->data
;
475 now
= (nfacct
->flags
& NFACCT_F_QUOTA_PKTS
) ?
476 atomic64_read(&nfacct
->pkts
) : atomic64_read(&nfacct
->bytes
);
481 !test_and_set_bit(NFACCT_OVERQUOTA_BIT
, &nfacct
->flags
)) {
482 nfnl_overquota_report(net
, nfacct
);
487 EXPORT_SYMBOL_GPL(nfnl_acct_overquota
);
489 static int __net_init
nfnl_acct_net_init(struct net
*net
)
491 INIT_LIST_HEAD(&net
->nfnl_acct_list
);
496 static void __net_exit
nfnl_acct_net_exit(struct net
*net
)
498 struct nf_acct
*cur
, *tmp
;
500 list_for_each_entry_safe(cur
, tmp
, &net
->nfnl_acct_list
, head
) {
501 list_del_rcu(&cur
->head
);
503 if (refcount_dec_and_test(&cur
->refcnt
))
504 kfree_rcu(cur
, rcu_head
);
508 static struct pernet_operations nfnl_acct_ops
= {
509 .init
= nfnl_acct_net_init
,
510 .exit
= nfnl_acct_net_exit
,
513 static int __init
nfnl_acct_init(void)
517 ret
= register_pernet_subsys(&nfnl_acct_ops
);
519 pr_err("nfnl_acct_init: failed to register pernet ops\n");
523 ret
= nfnetlink_subsys_register(&nfnl_acct_subsys
);
525 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
531 unregister_pernet_subsys(&nfnl_acct_ops
);
536 static void __exit
nfnl_acct_exit(void)
538 nfnetlink_subsys_unregister(&nfnl_acct_subsys
);
539 unregister_pernet_subsys(&nfnl_acct_ops
);
542 module_init(nfnl_acct_init
);
543 module_exit(nfnl_acct_exit
);