Skip to content

Commit 9413902

Browse files
committed
netfilter: add extended accounting infrastructure over nfnetlink
We currently have two ways to account traffic in netfilter: - iptables chain and rule counters: # iptables -L -n -v Chain INPUT (policy DROP 3 packets, 867 bytes) pkts bytes target prot opt in out source destination 8 1104 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 - use flow-based accounting provided by ctnetlink: # conntrack -L tcp 6 431999 ESTABLISHED src=192.168.1.130 dst=212.106.219.168 sport=58152 dport=80 packets=47 bytes=7654 src=212.106.219.168 dst=192.168.1.130 sport=80 dport=58152 packets=49 bytes=66340 [ASSURED] mark=0 use=1 While trying to display real-time accounting statistics, we require to pool the kernel periodically to obtain this information. This is OK if the number of flows is relatively low. However, in case that the number of flows is huge, we can spend a considerable amount of cycles to iterate over the list of flows that have been obtained. Moreover, if we want to obtain the sum of the flow accounting results that match some criteria, we have to iterate over the whole list of existing flows, look for matchings and update the counters. This patch adds the extended accounting infrastructure for nfnetlink which aims to allow displaying real-time traffic accounting without the need of complicated and resource-consuming implementation in user-space. Basically, this new infrastructure allows you to create accounting objects. One accounting object is composed of packet and byte counters. In order to manipulate create accounting objects, you require the new libnetfilter_acct library. It contains several examples of use: libnetfilter_acct/examples# ./nfacct-add http-traffic libnetfilter_acct/examples# ./nfacct-get http-traffic = { pkts = 000000000000, bytes = 000000000000 }; Then, you can use one of this accounting objects in several iptables rules using the new nfacct match (which comes in a follow-up patch): # iptables -I INPUT -p tcp --sport 80 -m nfacct --nfacct-name http-traffic # iptables -I OUTPUT -p tcp --dport 80 -m nfacct --nfacct-name http-traffic The idea is simple: if one packet matches the rule, the nfacct match updates the counters. Thanks to Patrick McHardy, Eric Dumazet, Changli Gao for reviewing and providing feedback for this contribution. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 80e60e6 commit 9413902

File tree

6 files changed

+400
-1
lines changed

6 files changed

+400
-1
lines changed

include/linux/netfilter/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ header-y += nf_conntrack_tcp.h
77
header-y += nf_conntrack_tuple_common.h
88
header-y += nf_nat.h
99
header-y += nfnetlink.h
10+
header-y += nfnetlink_acct.h
1011
header-y += nfnetlink_compat.h
1112
header-y += nfnetlink_conntrack.h
1213
header-y += nfnetlink_log.h

include/linux/netfilter/nfnetlink.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ struct nfgenmsg {
4848
#define NFNL_SUBSYS_ULOG 4
4949
#define NFNL_SUBSYS_OSF 5
5050
#define NFNL_SUBSYS_IPSET 6
51-
#define NFNL_SUBSYS_COUNT 7
51+
#define NFNL_SUBSYS_ACCT 7
52+
#define NFNL_SUBSYS_COUNT 8
5253

5354
#ifdef __KERNEL__
5455

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _NFNL_ACCT_H_
2+
#define _NFNL_ACCT_H_
3+
4+
#ifndef NFACCT_NAME_MAX
5+
#define NFACCT_NAME_MAX 32
6+
#endif
7+
8+
enum nfnl_acct_msg_types {
9+
NFNL_MSG_ACCT_NEW,
10+
NFNL_MSG_ACCT_GET,
11+
NFNL_MSG_ACCT_GET_CTRZERO,
12+
NFNL_MSG_ACCT_DEL,
13+
NFNL_MSG_ACCT_MAX
14+
};
15+
16+
enum nfnl_acct_type {
17+
NFACCT_UNSPEC,
18+
NFACCT_NAME,
19+
NFACCT_PKTS,
20+
NFACCT_BYTES,
21+
NFACCT_USE,
22+
__NFACCT_MAX
23+
};
24+
#define NFACCT_MAX (__NFACCT_MAX - 1)
25+
26+
#ifdef __KERNEL__
27+
28+
struct nf_acct;
29+
30+
extern struct nf_acct *nfnl_acct_find_get(const char *filter_name);
31+
extern void nfnl_acct_put(struct nf_acct *acct);
32+
extern void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct);
33+
34+
#endif /* __KERNEL__ */
35+
36+
#endif /* _NFNL_ACCT_H */

net/netfilter/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ menu "Core Netfilter Configuration"
44
config NETFILTER_NETLINK
55
tristate
66

7+
config NETFILTER_NETLINK_ACCT
8+
tristate "Netfilter NFACCT over NFNETLINK interface"
9+
depends on NETFILTER_ADVANCED
10+
select NETFILTER_NETLINK
11+
help
12+
If this option is enabled, the kernel will include support
13+
for extended accounting via NFNETLINK.
14+
715
config NETFILTER_NETLINK_QUEUE
816
tristate "Netfilter NFQUEUE over NFNETLINK interface"
917
depends on NETFILTER_ADVANCED

net/netfilter/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o
77
obj-$(CONFIG_NETFILTER) = netfilter.o
88

99
obj-$(CONFIG_NETFILTER_NETLINK) += nfnetlink.o
10+
obj-$(CONFIG_NETFILTER_NETLINK_ACCT) += nfnetlink_acct.o
1011
obj-$(CONFIG_NETFILTER_NETLINK_QUEUE) += nfnetlink_queue.o
1112
obj-$(CONFIG_NETFILTER_NETLINK_LOG) += nfnetlink_log.o
1213

0 commit comments

Comments
 (0)