194139027SPablo Neira Ayuso /*
294139027SPablo Neira Ayuso  * (C) 2011 Pablo Neira Ayuso <pablo@netfilter.org>
394139027SPablo Neira Ayuso  * (C) 2011 Intra2net AG <http://www.intra2net.com>
494139027SPablo Neira Ayuso  *
594139027SPablo Neira Ayuso  * This program is free software; you can redistribute it and/or modify
694139027SPablo Neira Ayuso  * it under the terms of the GNU General Public License version 2 as
794139027SPablo Neira Ayuso  * published by the Free Software Foundation (or any later at your option).
894139027SPablo Neira Ayuso  */
994139027SPablo Neira Ayuso #include <linux/init.h>
1094139027SPablo Neira Ayuso #include <linux/module.h>
1194139027SPablo Neira Ayuso #include <linux/kernel.h>
1294139027SPablo Neira Ayuso #include <linux/skbuff.h>
136523cf9aSAndrew Morton #include <linux/atomic.h>
14b54ab92bSReshetova, Elena #include <linux/refcount.h>
1594139027SPablo Neira Ayuso #include <linux/netlink.h>
1694139027SPablo Neira Ayuso #include <linux/rculist.h>
1794139027SPablo Neira Ayuso #include <linux/slab.h>
1894139027SPablo Neira Ayuso #include <linux/types.h>
1994139027SPablo Neira Ayuso #include <linux/errno.h>
2094139027SPablo Neira Ayuso #include <net/netlink.h>
2194139027SPablo Neira Ayuso #include <net/sock.h>
2294139027SPablo Neira Ayuso 
2394139027SPablo Neira Ayuso #include <linux/netfilter.h>
2494139027SPablo Neira Ayuso #include <linux/netfilter/nfnetlink.h>
2594139027SPablo Neira Ayuso #include <linux/netfilter/nfnetlink_acct.h>
2694139027SPablo Neira Ayuso 
2794139027SPablo Neira Ayuso MODULE_LICENSE("GPL");
2894139027SPablo Neira Ayuso MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
2994139027SPablo Neira Ayuso MODULE_DESCRIPTION("nfacct: Extended Netfilter accounting infrastructure");
3094139027SPablo Neira Ayuso 
3194139027SPablo Neira Ayuso struct nf_acct {
3294139027SPablo Neira Ayuso 	atomic64_t		pkts;
3394139027SPablo Neira Ayuso 	atomic64_t		bytes;
34683399edSMathieu Poirier 	unsigned long		flags;
3594139027SPablo Neira Ayuso 	struct list_head	head;
36b54ab92bSReshetova, Elena 	refcount_t		refcnt;
3794139027SPablo Neira Ayuso 	char			name[NFACCT_NAME_MAX];
3894139027SPablo Neira Ayuso 	struct rcu_head		rcu_head;
39683399edSMathieu Poirier 	char			data[0];
4094139027SPablo Neira Ayuso };
4194139027SPablo Neira Ayuso 
42f111f780SAlexey Perevalov struct nfacct_filter {
43f111f780SAlexey Perevalov 	u32 value;
44f111f780SAlexey Perevalov 	u32 mask;
45f111f780SAlexey Perevalov };
46f111f780SAlexey Perevalov 
47683399edSMathieu Poirier #define NFACCT_F_QUOTA (NFACCT_F_QUOTA_PKTS | NFACCT_F_QUOTA_BYTES)
48b6d04688SAlexey Perevalov #define NFACCT_OVERQUOTA_BIT	2	/* NFACCT_F_OVERQUOTA */
49683399edSMathieu Poirier 
507b8002a1SPablo Neira Ayuso static int nfnl_acct_new(struct net *net, struct sock *nfnl,
517b8002a1SPablo Neira Ayuso 			 struct sk_buff *skb, const struct nlmsghdr *nlh,
5204ba724bSPablo Neira Ayuso 			 const struct nlattr * const tb[],
5304ba724bSPablo Neira Ayuso 			 struct netlink_ext_ack *extack)
5494139027SPablo Neira Ayuso {
5594139027SPablo Neira Ayuso 	struct nf_acct *nfacct, *matching = NULL;
5694139027SPablo Neira Ayuso 	char *acct_name;
57683399edSMathieu Poirier 	unsigned int size = 0;
58683399edSMathieu Poirier 	u32 flags = 0;
5994139027SPablo Neira Ayuso 
6094139027SPablo Neira Ayuso 	if (!tb[NFACCT_NAME])
6194139027SPablo Neira Ayuso 		return -EINVAL;
6294139027SPablo Neira Ayuso 
6394139027SPablo Neira Ayuso 	acct_name = nla_data(tb[NFACCT_NAME]);
64deadcfc3SPablo Neira Ayuso 	if (strlen(acct_name) == 0)
65deadcfc3SPablo Neira Ayuso 		return -EINVAL;
6694139027SPablo Neira Ayuso 
673499abb2SAndreas Schultz 	list_for_each_entry(nfacct, &net->nfnl_acct_list, head) {
6894139027SPablo Neira Ayuso 		if (strncmp(nfacct->name, acct_name, NFACCT_NAME_MAX) != 0)
6994139027SPablo Neira Ayuso 			continue;
7094139027SPablo Neira Ayuso 
7194139027SPablo Neira Ayuso                 if (nlh->nlmsg_flags & NLM_F_EXCL)
7294139027SPablo Neira Ayuso 			return -EEXIST;
7394139027SPablo Neira Ayuso 
7494139027SPablo Neira Ayuso 		matching = nfacct;
7594139027SPablo Neira Ayuso 		break;
7694139027SPablo Neira Ayuso         }
7794139027SPablo Neira Ayuso 
7894139027SPablo Neira Ayuso 	if (matching) {
7994139027SPablo Neira Ayuso 		if (nlh->nlmsg_flags & NLM_F_REPLACE) {
8094139027SPablo Neira Ayuso 			/* reset counters if you request a replacement. */
8194139027SPablo Neira Ayuso 			atomic64_set(&matching->pkts, 0);
8294139027SPablo Neira Ayuso 			atomic64_set(&matching->bytes, 0);
83f9da455bSLinus Torvalds 			smp_mb__before_atomic();
84683399edSMathieu Poirier 			/* reset overquota flag if quota is enabled. */
85683399edSMathieu Poirier 			if ((matching->flags & NFACCT_F_QUOTA))
86b6d04688SAlexey Perevalov 				clear_bit(NFACCT_OVERQUOTA_BIT,
87b6d04688SAlexey Perevalov 					  &matching->flags);
8894139027SPablo Neira Ayuso 			return 0;
8994139027SPablo Neira Ayuso 		}
9094139027SPablo Neira Ayuso 		return -EBUSY;
9194139027SPablo Neira Ayuso 	}
9294139027SPablo Neira Ayuso 
93683399edSMathieu Poirier 	if (tb[NFACCT_FLAGS]) {
94683399edSMathieu Poirier 		flags = ntohl(nla_get_be32(tb[NFACCT_FLAGS]));
95683399edSMathieu Poirier 		if (flags & ~NFACCT_F_QUOTA)
96683399edSMathieu Poirier 			return -EOPNOTSUPP;
97683399edSMathieu Poirier 		if ((flags & NFACCT_F_QUOTA) == NFACCT_F_QUOTA)
98683399edSMathieu Poirier 			return -EINVAL;
99683399edSMathieu Poirier 		if (flags & NFACCT_F_OVERQUOTA)
100683399edSMathieu Poirier 			return -EINVAL;
101eda3fc50SPhil Turnbull 		if ((flags & NFACCT_F_QUOTA) && !tb[NFACCT_QUOTA])
102eda3fc50SPhil Turnbull 			return -EINVAL;
103683399edSMathieu Poirier 
104683399edSMathieu Poirier 		size += sizeof(u64);
105683399edSMathieu Poirier 	}
106683399edSMathieu Poirier 
107683399edSMathieu Poirier 	nfacct = kzalloc(sizeof(struct nf_acct) + size, GFP_KERNEL);
10894139027SPablo Neira Ayuso 	if (nfacct == NULL)
10994139027SPablo Neira Ayuso 		return -ENOMEM;
11094139027SPablo Neira Ayuso 
111683399edSMathieu Poirier 	if (flags & NFACCT_F_QUOTA) {
112683399edSMathieu Poirier 		u64 *quota = (u64 *)nfacct->data;
113683399edSMathieu Poirier 
114683399edSMathieu Poirier 		*quota = be64_to_cpu(nla_get_be64(tb[NFACCT_QUOTA]));
115683399edSMathieu Poirier 		nfacct->flags = flags;
116683399edSMathieu Poirier 	}
117683399edSMathieu Poirier 
1184b83a904SEric Dumazet 	nla_strlcpy(nfacct->name, tb[NFACCT_NAME], NFACCT_NAME_MAX);
11994139027SPablo Neira Ayuso 
12094139027SPablo Neira Ayuso 	if (tb[NFACCT_BYTES]) {
12194139027SPablo Neira Ayuso 		atomic64_set(&nfacct->bytes,
122fe31d1a8SPatrick McHardy 			     be64_to_cpu(nla_get_be64(tb[NFACCT_BYTES])));
12394139027SPablo Neira Ayuso 	}
12494139027SPablo Neira Ayuso 	if (tb[NFACCT_PKTS]) {
12594139027SPablo Neira Ayuso 		atomic64_set(&nfacct->pkts,
126fe31d1a8SPatrick McHardy 			     be64_to_cpu(nla_get_be64(tb[NFACCT_PKTS])));
12794139027SPablo Neira Ayuso 	}
128b54ab92bSReshetova, Elena 	refcount_set(&nfacct->refcnt, 1);
1293499abb2SAndreas Schultz 	list_add_tail_rcu(&nfacct->head, &net->nfnl_acct_list);
13094139027SPablo Neira Ayuso 	return 0;
13194139027SPablo Neira Ayuso }
13294139027SPablo Neira Ayuso 
13394139027SPablo Neira Ayuso static int
13415e47304SEric W. Biederman nfnl_acct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
13594139027SPablo Neira Ayuso 		   int event, struct nf_acct *acct)
13694139027SPablo Neira Ayuso {
13794139027SPablo Neira Ayuso 	struct nlmsghdr *nlh;
13894139027SPablo Neira Ayuso 	struct nfgenmsg *nfmsg;
13915e47304SEric W. Biederman 	unsigned int flags = portid ? NLM_F_MULTI : 0;
14094139027SPablo Neira Ayuso 	u64 pkts, bytes;
141d24675cbSAlexey Perevalov 	u32 old_flags;
14294139027SPablo Neira Ayuso 
143dedb67c4SPablo Neira Ayuso 	event = nfnl_msg_type(NFNL_SUBSYS_ACCT, event);
14415e47304SEric W. Biederman 	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
14594139027SPablo Neira Ayuso 	if (nlh == NULL)
14694139027SPablo Neira Ayuso 		goto nlmsg_failure;
14794139027SPablo Neira Ayuso 
14894139027SPablo Neira Ayuso 	nfmsg = nlmsg_data(nlh);
14994139027SPablo Neira Ayuso 	nfmsg->nfgen_family = AF_UNSPEC;
15094139027SPablo Neira Ayuso 	nfmsg->version = NFNETLINK_V0;
15194139027SPablo Neira Ayuso 	nfmsg->res_id = 0;
15294139027SPablo Neira Ayuso 
1537c801189SDavid S. Miller 	if (nla_put_string(skb, NFACCT_NAME, acct->name))
1547c801189SDavid S. Miller 		goto nla_put_failure;
15594139027SPablo Neira Ayuso 
156d24675cbSAlexey Perevalov 	old_flags = acct->flags;
15794139027SPablo Neira Ayuso 	if (type == NFNL_MSG_ACCT_GET_CTRZERO) {
15894139027SPablo Neira Ayuso 		pkts = atomic64_xchg(&acct->pkts, 0);
15994139027SPablo Neira Ayuso 		bytes = atomic64_xchg(&acct->bytes, 0);
160f9da455bSLinus Torvalds 		smp_mb__before_atomic();
161683399edSMathieu Poirier 		if (acct->flags & NFACCT_F_QUOTA)
162b6d04688SAlexey Perevalov 			clear_bit(NFACCT_OVERQUOTA_BIT, &acct->flags);
16394139027SPablo Neira Ayuso 	} else {
16494139027SPablo Neira Ayuso 		pkts = atomic64_read(&acct->pkts);
16594139027SPablo Neira Ayuso 		bytes = atomic64_read(&acct->bytes);
16694139027SPablo Neira Ayuso 	}
167b46f6dedSNicolas Dichtel 	if (nla_put_be64(skb, NFACCT_PKTS, cpu_to_be64(pkts),
168b46f6dedSNicolas Dichtel 			 NFACCT_PAD) ||
169b46f6dedSNicolas Dichtel 	    nla_put_be64(skb, NFACCT_BYTES, cpu_to_be64(bytes),
170b46f6dedSNicolas Dichtel 			 NFACCT_PAD) ||
171b54ab92bSReshetova, Elena 	    nla_put_be32(skb, NFACCT_USE, htonl(refcount_read(&acct->refcnt))))
1727c801189SDavid S. Miller 		goto nla_put_failure;
173683399edSMathieu Poirier 	if (acct->flags & NFACCT_F_QUOTA) {
174683399edSMathieu Poirier 		u64 *quota = (u64 *)acct->data;
17594139027SPablo Neira Ayuso 
176d24675cbSAlexey Perevalov 		if (nla_put_be32(skb, NFACCT_FLAGS, htonl(old_flags)) ||
177b46f6dedSNicolas Dichtel 		    nla_put_be64(skb, NFACCT_QUOTA, cpu_to_be64(*quota),
178b46f6dedSNicolas Dichtel 				 NFACCT_PAD))
179683399edSMathieu Poirier 			goto nla_put_failure;
180683399edSMathieu Poirier 	}
18194139027SPablo Neira Ayuso 	nlmsg_end(skb, nlh);
18294139027SPablo Neira Ayuso 	return skb->len;
18394139027SPablo Neira Ayuso 
18494139027SPablo Neira Ayuso nlmsg_failure:
18594139027SPablo Neira Ayuso nla_put_failure:
18694139027SPablo Neira Ayuso 	nlmsg_cancel(skb, nlh);
18794139027SPablo Neira Ayuso 	return -1;
18894139027SPablo Neira Ayuso }
18994139027SPablo Neira Ayuso 
19094139027SPablo Neira Ayuso static int
19194139027SPablo Neira Ayuso nfnl_acct_dump(struct sk_buff *skb, struct netlink_callback *cb)
19294139027SPablo Neira Ayuso {
1933499abb2SAndreas Schultz 	struct net *net = sock_net(skb->sk);
19494139027SPablo Neira Ayuso 	struct nf_acct *cur, *last;
195f111f780SAlexey Perevalov 	const struct nfacct_filter *filter = cb->data;
19694139027SPablo Neira Ayuso 
19794139027SPablo Neira Ayuso 	if (cb->args[2])
19894139027SPablo Neira Ayuso 		return 0;
19994139027SPablo Neira Ayuso 
20094139027SPablo Neira Ayuso 	last = (struct nf_acct *)cb->args[1];
20194139027SPablo Neira Ayuso 	if (cb->args[1])
20294139027SPablo Neira Ayuso 		cb->args[1] = 0;
20394139027SPablo Neira Ayuso 
20494139027SPablo Neira Ayuso 	rcu_read_lock();
2053499abb2SAndreas Schultz 	list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
206991a6b73SPablo Neira Ayuso 		if (last) {
207991a6b73SPablo Neira Ayuso 			if (cur != last)
20894139027SPablo Neira Ayuso 				continue;
20994139027SPablo Neira Ayuso 
210991a6b73SPablo Neira Ayuso 			last = NULL;
211991a6b73SPablo Neira Ayuso 		}
212f111f780SAlexey Perevalov 
213f111f780SAlexey Perevalov 		if (filter && (cur->flags & filter->mask) != filter->value)
214f111f780SAlexey Perevalov 			continue;
215f111f780SAlexey Perevalov 
21615e47304SEric W. Biederman 		if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).portid,
21794139027SPablo Neira Ayuso 				       cb->nlh->nlmsg_seq,
21894139027SPablo Neira Ayuso 				       NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
21994139027SPablo Neira Ayuso 				       NFNL_MSG_ACCT_NEW, cur) < 0) {
22094139027SPablo Neira Ayuso 			cb->args[1] = (unsigned long)cur;
22194139027SPablo Neira Ayuso 			break;
22294139027SPablo Neira Ayuso 		}
22394139027SPablo Neira Ayuso 	}
22494139027SPablo Neira Ayuso 	if (!cb->args[1])
22594139027SPablo Neira Ayuso 		cb->args[2] = 1;
22694139027SPablo Neira Ayuso 	rcu_read_unlock();
22794139027SPablo Neira Ayuso 	return skb->len;
22894139027SPablo Neira Ayuso }
22994139027SPablo Neira Ayuso 
230f111f780SAlexey Perevalov static int nfnl_acct_done(struct netlink_callback *cb)
231f111f780SAlexey Perevalov {
232f111f780SAlexey Perevalov 	kfree(cb->data);
233f111f780SAlexey Perevalov 	return 0;
234f111f780SAlexey Perevalov }
235f111f780SAlexey Perevalov 
236f111f780SAlexey Perevalov static const struct nla_policy filter_policy[NFACCT_FILTER_MAX + 1] = {
237f111f780SAlexey Perevalov 	[NFACCT_FILTER_MASK]	= { .type = NLA_U32 },
238f111f780SAlexey Perevalov 	[NFACCT_FILTER_VALUE]	= { .type = NLA_U32 },
239f111f780SAlexey Perevalov };
240f111f780SAlexey Perevalov 
2413e673b23SFlorian Westphal static int nfnl_acct_start(struct netlink_callback *cb)
242f111f780SAlexey Perevalov {
2433e673b23SFlorian Westphal 	const struct nlattr *const attr = cb->data;
244f111f780SAlexey Perevalov 	struct nlattr *tb[NFACCT_FILTER_MAX + 1];
2453e673b23SFlorian Westphal 	struct nfacct_filter *filter;
246f111f780SAlexey Perevalov 	int err;
247f111f780SAlexey Perevalov 
2483e673b23SFlorian Westphal 	if (!attr)
2493e673b23SFlorian Westphal 		return 0;
2503e673b23SFlorian Westphal 
251fceb6435SJohannes Berg 	err = nla_parse_nested(tb, NFACCT_FILTER_MAX, attr, filter_policy,
252fceb6435SJohannes Berg 			       NULL);
253f111f780SAlexey Perevalov 	if (err < 0)
2543e673b23SFlorian Westphal 		return err;
255f111f780SAlexey Perevalov 
256017b1b6dSPhil Turnbull 	if (!tb[NFACCT_FILTER_MASK] || !tb[NFACCT_FILTER_VALUE])
2573e673b23SFlorian Westphal 		return -EINVAL;
258017b1b6dSPhil Turnbull 
259f111f780SAlexey Perevalov 	filter = kzalloc(sizeof(struct nfacct_filter), GFP_KERNEL);
260f111f780SAlexey Perevalov 	if (!filter)
2613e673b23SFlorian Westphal 		return -ENOMEM;
262f111f780SAlexey Perevalov 
263f111f780SAlexey Perevalov 	filter->mask = ntohl(nla_get_be32(tb[NFACCT_FILTER_MASK]));
264f111f780SAlexey Perevalov 	filter->value = ntohl(nla_get_be32(tb[NFACCT_FILTER_VALUE]));
2653e673b23SFlorian Westphal 	cb->data = filter;
266f111f780SAlexey Perevalov 
2673e673b23SFlorian Westphal 	return 0;
268f111f780SAlexey Perevalov }
269f111f780SAlexey Perevalov 
2707b8002a1SPablo Neira Ayuso static int nfnl_acct_get(struct net *net, struct sock *nfnl,
2717b8002a1SPablo Neira Ayuso 			 struct sk_buff *skb, const struct nlmsghdr *nlh,
27204ba724bSPablo Neira Ayuso 			 const struct nlattr * const tb[],
27304ba724bSPablo Neira Ayuso 			 struct netlink_ext_ack *extack)
27494139027SPablo Neira Ayuso {
2753ab0b245SPablo Neira Ayuso 	int ret = -ENOENT;
27694139027SPablo Neira Ayuso 	struct nf_acct *cur;
27794139027SPablo Neira Ayuso 	char *acct_name;
27894139027SPablo Neira Ayuso 
27994139027SPablo Neira Ayuso 	if (nlh->nlmsg_flags & NLM_F_DUMP) {
28080d326faSPablo Neira Ayuso 		struct netlink_dump_control c = {
28180d326faSPablo Neira Ayuso 			.dump = nfnl_acct_dump,
2823e673b23SFlorian Westphal 			.start = nfnl_acct_start,
283f111f780SAlexey Perevalov 			.done = nfnl_acct_done,
2843e673b23SFlorian Westphal 			.data = (void *)tb[NFACCT_FILTER],
28580d326faSPablo Neira Ayuso 		};
286f111f780SAlexey Perevalov 
28780d326faSPablo Neira Ayuso 		return netlink_dump_start(nfnl, skb, nlh, &c);
28894139027SPablo Neira Ayuso 	}
28994139027SPablo Neira Ayuso 
29094139027SPablo Neira Ayuso 	if (!tb[NFACCT_NAME])
29194139027SPablo Neira Ayuso 		return -EINVAL;
29294139027SPablo Neira Ayuso 	acct_name = nla_data(tb[NFACCT_NAME]);
29394139027SPablo Neira Ayuso 
2943499abb2SAndreas Schultz 	list_for_each_entry(cur, &net->nfnl_acct_list, head) {
29594139027SPablo Neira Ayuso 		struct sk_buff *skb2;
29694139027SPablo Neira Ayuso 
29794139027SPablo Neira Ayuso 		if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
29894139027SPablo Neira Ayuso 			continue;
29994139027SPablo Neira Ayuso 
30094139027SPablo Neira Ayuso 		skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3013ab0b245SPablo Neira Ayuso 		if (skb2 == NULL) {
3023ab0b245SPablo Neira Ayuso 			ret = -ENOMEM;
30394139027SPablo Neira Ayuso 			break;
3043ab0b245SPablo Neira Ayuso 		}
30594139027SPablo Neira Ayuso 
30615e47304SEric W. Biederman 		ret = nfnl_acct_fill_info(skb2, NETLINK_CB(skb).portid,
30794139027SPablo Neira Ayuso 					 nlh->nlmsg_seq,
30894139027SPablo Neira Ayuso 					 NFNL_MSG_TYPE(nlh->nlmsg_type),
30994139027SPablo Neira Ayuso 					 NFNL_MSG_ACCT_NEW, cur);
3103ab0b245SPablo Neira Ayuso 		if (ret <= 0) {
31194139027SPablo Neira Ayuso 			kfree_skb(skb2);
31294139027SPablo Neira Ayuso 			break;
31394139027SPablo Neira Ayuso 		}
31415e47304SEric W. Biederman 		ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
3153ab0b245SPablo Neira Ayuso 					MSG_DONTWAIT);
3163ab0b245SPablo Neira Ayuso 		if (ret > 0)
3173ab0b245SPablo Neira Ayuso 			ret = 0;
3183ab0b245SPablo Neira Ayuso 
3193ab0b245SPablo Neira Ayuso 		/* this avoids a loop in nfnetlink. */
3203ab0b245SPablo Neira Ayuso 		return ret == -EAGAIN ? -ENOBUFS : ret;
3213ab0b245SPablo Neira Ayuso 	}
32294139027SPablo Neira Ayuso 	return ret;
32394139027SPablo Neira Ayuso }
32494139027SPablo Neira Ayuso 
32594139027SPablo Neira Ayuso /* try to delete object, fail if it is still in use. */
32694139027SPablo Neira Ayuso static int nfnl_acct_try_del(struct nf_acct *cur)
32794139027SPablo Neira Ayuso {
32894139027SPablo Neira Ayuso 	int ret = 0;
32994139027SPablo Neira Ayuso 
33012be15ddSLiping Zhang 	/* We want to avoid races with nfnl_acct_put. So only when the current
33112be15ddSLiping Zhang 	 * refcnt is 1, we decrease it to 0.
33212be15ddSLiping Zhang 	 */
333b54ab92bSReshetova, Elena 	if (refcount_dec_if_one(&cur->refcnt)) {
33494139027SPablo Neira Ayuso 		/* We are protected by nfnl mutex. */
33594139027SPablo Neira Ayuso 		list_del_rcu(&cur->head);
33694139027SPablo Neira Ayuso 		kfree_rcu(cur, rcu_head);
33794139027SPablo Neira Ayuso 	} else {
33894139027SPablo Neira Ayuso 		ret = -EBUSY;
33994139027SPablo Neira Ayuso 	}
34094139027SPablo Neira Ayuso 	return ret;
34194139027SPablo Neira Ayuso }
34294139027SPablo Neira Ayuso 
3437b8002a1SPablo Neira Ayuso static int nfnl_acct_del(struct net *net, struct sock *nfnl,
3447b8002a1SPablo Neira Ayuso 			 struct sk_buff *skb, const struct nlmsghdr *nlh,
34504ba724bSPablo Neira Ayuso 			 const struct nlattr * const tb[],
34604ba724bSPablo Neira Ayuso 			 struct netlink_ext_ack *extack)
34794139027SPablo Neira Ayuso {
34893fac10bSLiping Zhang 	struct nf_acct *cur, *tmp;
34994139027SPablo Neira Ayuso 	int ret = -ENOENT;
35093fac10bSLiping Zhang 	char *acct_name;
35194139027SPablo Neira Ayuso 
35294139027SPablo Neira Ayuso 	if (!tb[NFACCT_NAME]) {
35393fac10bSLiping Zhang 		list_for_each_entry_safe(cur, tmp, &net->nfnl_acct_list, head)
35494139027SPablo Neira Ayuso 			nfnl_acct_try_del(cur);
35594139027SPablo Neira Ayuso 
35694139027SPablo Neira Ayuso 		return 0;
35794139027SPablo Neira Ayuso 	}
35894139027SPablo Neira Ayuso 	acct_name = nla_data(tb[NFACCT_NAME]);
35994139027SPablo Neira Ayuso 
3603499abb2SAndreas Schultz 	list_for_each_entry(cur, &net->nfnl_acct_list, head) {
36194139027SPablo Neira Ayuso 		if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX) != 0)
36294139027SPablo Neira Ayuso 			continue;
36394139027SPablo Neira Ayuso 
36494139027SPablo Neira Ayuso 		ret = nfnl_acct_try_del(cur);
36594139027SPablo Neira Ayuso 		if (ret < 0)
36694139027SPablo Neira Ayuso 			return ret;
36794139027SPablo Neira Ayuso 
36894139027SPablo Neira Ayuso 		break;
36994139027SPablo Neira Ayuso 	}
37094139027SPablo Neira Ayuso 	return ret;
37194139027SPablo Neira Ayuso }
37294139027SPablo Neira Ayuso 
37394139027SPablo Neira Ayuso static const struct nla_policy nfnl_acct_policy[NFACCT_MAX+1] = {
37494139027SPablo Neira Ayuso 	[NFACCT_NAME] = { .type = NLA_NUL_STRING, .len = NFACCT_NAME_MAX-1 },
37594139027SPablo Neira Ayuso 	[NFACCT_BYTES] = { .type = NLA_U64 },
37694139027SPablo Neira Ayuso 	[NFACCT_PKTS] = { .type = NLA_U64 },
377683399edSMathieu Poirier 	[NFACCT_FLAGS] = { .type = NLA_U32 },
378683399edSMathieu Poirier 	[NFACCT_QUOTA] = { .type = NLA_U64 },
379f111f780SAlexey Perevalov 	[NFACCT_FILTER] = {.type = NLA_NESTED },
38094139027SPablo Neira Ayuso };
38194139027SPablo Neira Ayuso 
38294139027SPablo Neira Ayuso static const struct nfnl_callback nfnl_acct_cb[NFNL_MSG_ACCT_MAX] = {
38394139027SPablo Neira Ayuso 	[NFNL_MSG_ACCT_NEW]		= { .call = nfnl_acct_new,
38494139027SPablo Neira Ayuso 					    .attr_count = NFACCT_MAX,
38594139027SPablo Neira Ayuso 					    .policy = nfnl_acct_policy },
38694139027SPablo Neira Ayuso 	[NFNL_MSG_ACCT_GET] 		= { .call = nfnl_acct_get,
38794139027SPablo Neira Ayuso 					    .attr_count = NFACCT_MAX,
38894139027SPablo Neira Ayuso 					    .policy = nfnl_acct_policy },
38994139027SPablo Neira Ayuso 	[NFNL_MSG_ACCT_GET_CTRZERO] 	= { .call = nfnl_acct_get,
39094139027SPablo Neira Ayuso 					    .attr_count = NFACCT_MAX,
39194139027SPablo Neira Ayuso 					    .policy = nfnl_acct_policy },
39294139027SPablo Neira Ayuso 	[NFNL_MSG_ACCT_DEL]		= { .call = nfnl_acct_del,
39394139027SPablo Neira Ayuso 					    .attr_count = NFACCT_MAX,
39494139027SPablo Neira Ayuso 					    .policy = nfnl_acct_policy },
39594139027SPablo Neira Ayuso };
39694139027SPablo Neira Ayuso 
39794139027SPablo Neira Ayuso static const struct nfnetlink_subsystem nfnl_acct_subsys = {
39894139027SPablo Neira Ayuso 	.name				= "acct",
39994139027SPablo Neira Ayuso 	.subsys_id			= NFNL_SUBSYS_ACCT,
40094139027SPablo Neira Ayuso 	.cb_count			= NFNL_MSG_ACCT_MAX,
40194139027SPablo Neira Ayuso 	.cb				= nfnl_acct_cb,
40294139027SPablo Neira Ayuso };
40394139027SPablo Neira Ayuso 
40494139027SPablo Neira Ayuso MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ACCT);
40594139027SPablo Neira Ayuso 
4063499abb2SAndreas Schultz struct nf_acct *nfnl_acct_find_get(struct net *net, const char *acct_name)
40794139027SPablo Neira Ayuso {
40894139027SPablo Neira Ayuso 	struct nf_acct *cur, *acct = NULL;
40994139027SPablo Neira Ayuso 
41094139027SPablo Neira Ayuso 	rcu_read_lock();
4113499abb2SAndreas Schultz 	list_for_each_entry_rcu(cur, &net->nfnl_acct_list, head) {
41294139027SPablo Neira Ayuso 		if (strncmp(cur->name, acct_name, NFACCT_NAME_MAX)!= 0)
41394139027SPablo Neira Ayuso 			continue;
41494139027SPablo Neira Ayuso 
41594139027SPablo Neira Ayuso 		if (!try_module_get(THIS_MODULE))
41694139027SPablo Neira Ayuso 			goto err;
41794139027SPablo Neira Ayuso 
418b54ab92bSReshetova, Elena 		if (!refcount_inc_not_zero(&cur->refcnt)) {
41994139027SPablo Neira Ayuso 			module_put(THIS_MODULE);
42094139027SPablo Neira Ayuso 			goto err;
42194139027SPablo Neira Ayuso 		}
42294139027SPablo Neira Ayuso 
42394139027SPablo Neira Ayuso 		acct = cur;
42494139027SPablo Neira Ayuso 		break;
42594139027SPablo Neira Ayuso 	}
42694139027SPablo Neira Ayuso err:
42794139027SPablo Neira Ayuso 	rcu_read_unlock();
42894139027SPablo Neira Ayuso 	return acct;
42994139027SPablo Neira Ayuso }
43094139027SPablo Neira Ayuso EXPORT_SYMBOL_GPL(nfnl_acct_find_get);
43194139027SPablo Neira Ayuso 
43294139027SPablo Neira Ayuso void nfnl_acct_put(struct nf_acct *acct)
43394139027SPablo Neira Ayuso {
434b54ab92bSReshetova, Elena 	if (refcount_dec_and_test(&acct->refcnt))
4353499abb2SAndreas Schultz 		kfree_rcu(acct, rcu_head);
4363499abb2SAndreas Schultz 
43794139027SPablo Neira Ayuso 	module_put(THIS_MODULE);
43894139027SPablo Neira Ayuso }
43994139027SPablo Neira Ayuso EXPORT_SYMBOL_GPL(nfnl_acct_put);
44094139027SPablo Neira Ayuso 
44194139027SPablo Neira Ayuso void nfnl_acct_update(const struct sk_buff *skb, struct nf_acct *nfacct)
44294139027SPablo Neira Ayuso {
44394139027SPablo Neira Ayuso 	atomic64_inc(&nfacct->pkts);
44494139027SPablo Neira Ayuso 	atomic64_add(skb->len, &nfacct->bytes);
44594139027SPablo Neira Ayuso }
44694139027SPablo Neira Ayuso EXPORT_SYMBOL_GPL(nfnl_acct_update);
44794139027SPablo Neira Ayuso 
448aca30018SLiping Zhang static void nfnl_overquota_report(struct net *net, struct nf_acct *nfacct)
449683399edSMathieu Poirier {
450683399edSMathieu Poirier 	int ret;
451683399edSMathieu Poirier 	struct sk_buff *skb;
452683399edSMathieu Poirier 
453683399edSMathieu Poirier 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
454683399edSMathieu Poirier 	if (skb == NULL)
455683399edSMathieu Poirier 		return;
456683399edSMathieu Poirier 
457683399edSMathieu Poirier 	ret = nfnl_acct_fill_info(skb, 0, 0, NFNL_MSG_ACCT_OVERQUOTA, 0,
458683399edSMathieu Poirier 				  nfacct);
459683399edSMathieu Poirier 	if (ret <= 0) {
460683399edSMathieu Poirier 		kfree_skb(skb);
461683399edSMathieu Poirier 		return;
462683399edSMathieu Poirier 	}
463aca30018SLiping Zhang 	netlink_broadcast(net->nfnl, skb, 0, NFNLGRP_ACCT_QUOTA,
464683399edSMathieu Poirier 			  GFP_ATOMIC);
465683399edSMathieu Poirier }
466683399edSMathieu Poirier 
467cceae76eSTaehee Yoo int nfnl_acct_overquota(struct net *net, struct nf_acct *nfacct)
468683399edSMathieu Poirier {
469683399edSMathieu Poirier 	u64 now;
470683399edSMathieu Poirier 	u64 *quota;
471683399edSMathieu Poirier 	int ret = NFACCT_UNDERQUOTA;
472683399edSMathieu Poirier 
473683399edSMathieu Poirier 	/* no place here if we don't have a quota */
474683399edSMathieu Poirier 	if (!(nfacct->flags & NFACCT_F_QUOTA))
475683399edSMathieu Poirier 		return NFACCT_NO_QUOTA;
476683399edSMathieu Poirier 
477683399edSMathieu Poirier 	quota = (u64 *)nfacct->data;
478683399edSMathieu Poirier 	now = (nfacct->flags & NFACCT_F_QUOTA_PKTS) ?
479683399edSMathieu Poirier 	       atomic64_read(&nfacct->pkts) : atomic64_read(&nfacct->bytes);
480683399edSMathieu Poirier 
481683399edSMathieu Poirier 	ret = now > *quota;
482683399edSMathieu Poirier 
483683399edSMathieu Poirier 	if (now >= *quota &&
484b6d04688SAlexey Perevalov 	    !test_and_set_bit(NFACCT_OVERQUOTA_BIT, &nfacct->flags)) {
485aca30018SLiping Zhang 		nfnl_overquota_report(net, nfacct);
486683399edSMathieu Poirier 	}
487683399edSMathieu Poirier 
488683399edSMathieu Poirier 	return ret;
489683399edSMathieu Poirier }
490683399edSMathieu Poirier EXPORT_SYMBOL_GPL(nfnl_acct_overquota);
491683399edSMathieu Poirier 
4923499abb2SAndreas Schultz static int __net_init nfnl_acct_net_init(struct net *net)
4933499abb2SAndreas Schultz {
4943499abb2SAndreas Schultz 	INIT_LIST_HEAD(&net->nfnl_acct_list);
4953499abb2SAndreas Schultz 
4963499abb2SAndreas Schultz 	return 0;
4973499abb2SAndreas Schultz }
4983499abb2SAndreas Schultz 
4993499abb2SAndreas Schultz static void __net_exit nfnl_acct_net_exit(struct net *net)
5003499abb2SAndreas Schultz {
5013499abb2SAndreas Schultz 	struct nf_acct *cur, *tmp;
5023499abb2SAndreas Schultz 
5033499abb2SAndreas Schultz 	list_for_each_entry_safe(cur, tmp, &net->nfnl_acct_list, head) {
5043499abb2SAndreas Schultz 		list_del_rcu(&cur->head);
5053499abb2SAndreas Schultz 
506b54ab92bSReshetova, Elena 		if (refcount_dec_and_test(&cur->refcnt))
5073499abb2SAndreas Schultz 			kfree_rcu(cur, rcu_head);
5083499abb2SAndreas Schultz 	}
5093499abb2SAndreas Schultz }
5103499abb2SAndreas Schultz 
5113499abb2SAndreas Schultz static struct pernet_operations nfnl_acct_ops = {
5123499abb2SAndreas Schultz         .init   = nfnl_acct_net_init,
5133499abb2SAndreas Schultz         .exit   = nfnl_acct_net_exit,
5143499abb2SAndreas Schultz };
5153499abb2SAndreas Schultz 
51694139027SPablo Neira Ayuso static int __init nfnl_acct_init(void)
51794139027SPablo Neira Ayuso {
51894139027SPablo Neira Ayuso 	int ret;
51994139027SPablo Neira Ayuso 
5203499abb2SAndreas Schultz 	ret = register_pernet_subsys(&nfnl_acct_ops);
5213499abb2SAndreas Schultz 	if (ret < 0) {
5223499abb2SAndreas Schultz 		pr_err("nfnl_acct_init: failed to register pernet ops\n");
5233499abb2SAndreas Schultz 		goto err_out;
5243499abb2SAndreas Schultz 	}
5253499abb2SAndreas Schultz 
52694139027SPablo Neira Ayuso 	ret = nfnetlink_subsys_register(&nfnl_acct_subsys);
52794139027SPablo Neira Ayuso 	if (ret < 0) {
52894139027SPablo Neira Ayuso 		pr_err("nfnl_acct_init: cannot register with nfnetlink.\n");
5293499abb2SAndreas Schultz 		goto cleanup_pernet;
53094139027SPablo Neira Ayuso 	}
53194139027SPablo Neira Ayuso 	return 0;
5323499abb2SAndreas Schultz 
5333499abb2SAndreas Schultz cleanup_pernet:
5343499abb2SAndreas Schultz 	unregister_pernet_subsys(&nfnl_acct_ops);
53594139027SPablo Neira Ayuso err_out:
53694139027SPablo Neira Ayuso 	return ret;
53794139027SPablo Neira Ayuso }
53894139027SPablo Neira Ayuso 
53994139027SPablo Neira Ayuso static void __exit nfnl_acct_exit(void)
54094139027SPablo Neira Ayuso {
54194139027SPablo Neira Ayuso 	nfnetlink_subsys_unregister(&nfnl_acct_subsys);
5423499abb2SAndreas Schultz 	unregister_pernet_subsys(&nfnl_acct_ops);
54394139027SPablo Neira Ayuso }
54494139027SPablo Neira Ayuso 
54594139027SPablo Neira Ayuso module_init(nfnl_acct_init);
54694139027SPablo Neira Ayuso module_exit(nfnl_acct_exit);
547