1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * (C) 2008 Krzysztof Piotr Oledzki <ole@ans.pl>
4  */
5 
6 #ifndef _NF_CONNTRACK_ACCT_H
7 #define _NF_CONNTRACK_ACCT_H
8 #include <net/net_namespace.h>
9 #include <linux/netfilter/nf_conntrack_common.h>
10 #include <linux/netfilter/nf_conntrack_tuple_common.h>
11 #include <net/netfilter/nf_conntrack.h>
12 #include <net/netfilter/nf_conntrack_extend.h>
13 
14 struct nf_conn_counter {
15 	atomic64_t packets;
16 	atomic64_t bytes;
17 };
18 
19 struct nf_conn_acct {
20 	struct nf_conn_counter counter[IP_CT_DIR_MAX];
21 };
22 
23 static inline
nf_conn_acct_find(const struct nf_conn * ct)24 struct nf_conn_acct *nf_conn_acct_find(const struct nf_conn *ct)
25 {
26 	return nf_ct_ext_find(ct, NF_CT_EXT_ACCT);
27 }
28 
29 static inline
nf_ct_acct_ext_add(struct nf_conn * ct,gfp_t gfp)30 struct nf_conn_acct *nf_ct_acct_ext_add(struct nf_conn *ct, gfp_t gfp)
31 {
32 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
33 	struct net *net = nf_ct_net(ct);
34 	struct nf_conn_acct *acct;
35 
36 	if (!net->ct.sysctl_acct)
37 		return NULL;
38 
39 	acct = nf_ct_ext_add(ct, NF_CT_EXT_ACCT, gfp);
40 	if (!acct)
41 		pr_debug("failed to add accounting extension area");
42 
43 
44 	return acct;
45 #else
46 	return NULL;
47 #endif
48 }
49 
50 /* Check if connection tracking accounting is enabled */
nf_ct_acct_enabled(struct net * net)51 static inline bool nf_ct_acct_enabled(struct net *net)
52 {
53 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
54 	return net->ct.sysctl_acct != 0;
55 #else
56 	return false;
57 #endif
58 }
59 
60 /* Enable/disable connection tracking accounting */
nf_ct_set_acct(struct net * net,bool enable)61 static inline void nf_ct_set_acct(struct net *net, bool enable)
62 {
63 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
64 	net->ct.sysctl_acct = enable;
65 #endif
66 }
67 
68 void nf_ct_acct_add(struct nf_conn *ct, u32 dir, unsigned int packets,
69 		    unsigned int bytes);
70 
nf_ct_acct_update(struct nf_conn * ct,u32 dir,unsigned int bytes)71 static inline void nf_ct_acct_update(struct nf_conn *ct, u32 dir,
72 				     unsigned int bytes)
73 {
74 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
75 	nf_ct_acct_add(ct, dir, 1, bytes);
76 #endif
77 }
78 
79 void nf_conntrack_acct_pernet_init(struct net *net);
80 
81 void nf_conntrack_acct_fini(void);
82 
83 #endif /* _NF_CONNTRACK_ACCT_H */
84