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/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>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/nfnetlink.h>
24 #include <linux/netfilter/nfnetlink_acct.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
28 MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
30 static LIST_HEAD(nfnl_acct_list
);
35 struct list_head head
;
37 char name
[NFACCT_NAME_MAX
];
38 struct rcu_head rcu_head
;
42 nfnl_acct_new(struct sock
*nfnl
, struct sk_buff
*skb
,
43 const struct nlmsghdr
*nlh
, const struct nlattr
* const tb
[])
45 struct nf_acct
*nfacct
, *matching
= NULL
;
51 acct_name
= nla_data(tb
[NFACCT_NAME
]);
53 list_for_each_entry(nfacct
, &nfnl_acct_list
, head
) {
54 if (strncmp(nfacct
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
57 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
65 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
) {
66 /* reset counters if you request a replacement. */
67 atomic64_set(&matching
->pkts
, 0);
68 atomic64_set(&matching
->bytes
, 0);
74 nfacct
= kzalloc(sizeof(struct nf_acct
), GFP_KERNEL
);
78 strncpy(nfacct
->name
, nla_data(tb
[NFACCT_NAME
]), NFACCT_NAME_MAX
);
80 if (tb
[NFACCT_BYTES
]) {
81 atomic64_set(&nfacct
->bytes
,
82 be64_to_cpu(nla_get_be64(tb
[NFACCT_BYTES
])));
84 if (tb
[NFACCT_PKTS
]) {
85 atomic64_set(&nfacct
->pkts
,
86 be64_to_cpu(nla_get_be64(tb
[NFACCT_PKTS
])));
88 atomic_set(&nfacct
->refcnt
, 1);
89 list_add_tail_rcu(&nfacct
->head
, &nfnl_acct_list
);
94 nfnl_acct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
95 int event
, struct nf_acct
*acct
)
98 struct nfgenmsg
*nfmsg
;
99 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
102 event
|= NFNL_SUBSYS_ACCT
<< 8;
103 nlh
= nlmsg_put(skb
, portid
, seq
, event
, sizeof(*nfmsg
), flags
);
107 nfmsg
= nlmsg_data(nlh
);
108 nfmsg
->nfgen_family
= AF_UNSPEC
;
109 nfmsg
->version
= NFNETLINK_V0
;
112 if (nla_put_string(skb
, NFACCT_NAME
, acct
->name
))
113 goto nla_put_failure
;
115 if (type
== NFNL_MSG_ACCT_GET_CTRZERO
) {
116 pkts
= atomic64_xchg(&acct
->pkts
, 0);
117 bytes
= atomic64_xchg(&acct
->bytes
, 0);
119 pkts
= atomic64_read(&acct
->pkts
);
120 bytes
= atomic64_read(&acct
->bytes
);
122 if (nla_put_be64(skb
, NFACCT_PKTS
, cpu_to_be64(pkts
)) ||
123 nla_put_be64(skb
, NFACCT_BYTES
, cpu_to_be64(bytes
)) ||
124 nla_put_be32(skb
, NFACCT_USE
, htonl(atomic_read(&acct
->refcnt
))))
125 goto nla_put_failure
;
132 nlmsg_cancel(skb
, nlh
);
137 nfnl_acct_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
139 struct nf_acct
*cur
, *last
;
144 last
= (struct nf_acct
*)cb
->args
[1];
149 list_for_each_entry_rcu(cur
, &nfnl_acct_list
, head
) {
150 if (last
&& cur
!= last
)
153 if (nfnl_acct_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
155 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
156 NFNL_MSG_ACCT_NEW
, cur
) < 0) {
157 cb
->args
[1] = (unsigned long)cur
;
168 nfnl_acct_get(struct sock
*nfnl
, struct sk_buff
*skb
,
169 const struct nlmsghdr
*nlh
, const struct nlattr
* const tb
[])
175 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
176 struct netlink_dump_control c
= {
177 .dump
= nfnl_acct_dump
,
179 return netlink_dump_start(nfnl
, skb
, nlh
, &c
);
182 if (!tb
[NFACCT_NAME
])
184 acct_name
= nla_data(tb
[NFACCT_NAME
]);
186 list_for_each_entry(cur
, &nfnl_acct_list
, head
) {
187 struct sk_buff
*skb2
;
189 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
192 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
198 ret
= nfnl_acct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
200 NFNL_MSG_TYPE(nlh
->nlmsg_type
),
201 NFNL_MSG_ACCT_NEW
, cur
);
206 ret
= netlink_unicast(nfnl
, skb2
, NETLINK_CB(skb
).portid
,
211 /* this avoids a loop in nfnetlink. */
212 return ret
== -EAGAIN
? -ENOBUFS
: ret
;
217 /* try to delete object, fail if it is still in use. */
218 static int nfnl_acct_try_del(struct nf_acct
*cur
)
222 /* we want to avoid races with nfnl_acct_find_get. */
223 if (atomic_dec_and_test(&cur
->refcnt
)) {
224 /* We are protected by nfnl mutex. */
225 list_del_rcu(&cur
->head
);
226 kfree_rcu(cur
, rcu_head
);
228 /* still in use, restore reference counter. */
229 atomic_inc(&cur
->refcnt
);
236 nfnl_acct_del(struct sock
*nfnl
, struct sk_buff
*skb
,
237 const struct nlmsghdr
*nlh
, const struct nlattr
* const tb
[])
243 if (!tb
[NFACCT_NAME
]) {
244 list_for_each_entry(cur
, &nfnl_acct_list
, head
)
245 nfnl_acct_try_del(cur
);
249 acct_name
= nla_data(tb
[NFACCT_NAME
]);
251 list_for_each_entry(cur
, &nfnl_acct_list
, head
) {
252 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
) != 0)
255 ret
= nfnl_acct_try_del(cur
);
264 static const struct nla_policy nfnl_acct_policy
[NFACCT_MAX
+1] = {
265 [NFACCT_NAME
] = { .type
= NLA_NUL_STRING
, .len
= NFACCT_NAME_MAX
-1 },
266 [NFACCT_BYTES
] = { .type
= NLA_U64
},
267 [NFACCT_PKTS
] = { .type
= NLA_U64
},
270 static const struct nfnl_callback nfnl_acct_cb
[NFNL_MSG_ACCT_MAX
] = {
271 [NFNL_MSG_ACCT_NEW
] = { .call
= nfnl_acct_new
,
272 .attr_count
= NFACCT_MAX
,
273 .policy
= nfnl_acct_policy
},
274 [NFNL_MSG_ACCT_GET
] = { .call
= nfnl_acct_get
,
275 .attr_count
= NFACCT_MAX
,
276 .policy
= nfnl_acct_policy
},
277 [NFNL_MSG_ACCT_GET_CTRZERO
] = { .call
= nfnl_acct_get
,
278 .attr_count
= NFACCT_MAX
,
279 .policy
= nfnl_acct_policy
},
280 [NFNL_MSG_ACCT_DEL
] = { .call
= nfnl_acct_del
,
281 .attr_count
= NFACCT_MAX
,
282 .policy
= nfnl_acct_policy
},
285 static const struct nfnetlink_subsystem nfnl_acct_subsys
= {
287 .subsys_id
= NFNL_SUBSYS_ACCT
,
288 .cb_count
= NFNL_MSG_ACCT_MAX
,
292 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT
);
294 struct nf_acct
*nfnl_acct_find_get(const char *acct_name
)
296 struct nf_acct
*cur
, *acct
= NULL
;
299 list_for_each_entry_rcu(cur
, &nfnl_acct_list
, head
) {
300 if (strncmp(cur
->name
, acct_name
, NFACCT_NAME_MAX
)!= 0)
303 if (!try_module_get(THIS_MODULE
))
306 if (!atomic_inc_not_zero(&cur
->refcnt
)) {
307 module_put(THIS_MODULE
);
318 EXPORT_SYMBOL_GPL(nfnl_acct_find_get
);
320 void nfnl_acct_put(struct nf_acct
*acct
)
322 atomic_dec(&acct
->refcnt
);
323 module_put(THIS_MODULE
);
325 EXPORT_SYMBOL_GPL(nfnl_acct_put
);
327 void nfnl_acct_update(const struct sk_buff
*skb
, struct nf_acct
*nfacct
)
329 atomic64_inc(&nfacct
->pkts
);
330 atomic64_add(skb
->len
, &nfacct
->bytes
);
332 EXPORT_SYMBOL_GPL(nfnl_acct_update
);
334 static int __init
nfnl_acct_init(void)
338 pr_info("nfnl_acct: registering with nfnetlink.\n");
339 ret
= nfnetlink_subsys_register(&nfnl_acct_subsys
);
341 pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
349 static void __exit
nfnl_acct_exit(void)
351 struct nf_acct
*cur
, *tmp
;
353 pr_info("nfnl_acct: unregistering from nfnetlink.\n");
354 nfnetlink_subsys_unregister(&nfnl_acct_subsys
);
356 list_for_each_entry_safe(cur
, tmp
, &nfnl_acct_list
, head
) {
357 list_del_rcu(&cur
->head
);
358 /* We are sure that our objects have no clients at this point,
359 * it's safe to release them all without checking refcnt. */
360 kfree_rcu(cur
, rcu_head
);
364 module_init(nfnl_acct_init
);
365 module_exit(nfnl_acct_exit
);