Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3bcc5fdf authored by Florian Westphal's avatar Florian Westphal Committed by Pablo Neira Ayuso
Browse files

netfilter: connlimit: move insertion of new element out of count function



Allows easier code-reuse in followup patches.

Reviewed-by: default avatarJesper Dangaard Brouer <brouer@redhat.com>
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent d9ec4f1e
Loading
Loading
Loading
Loading
+23 −15
Original line number Original line Diff line number Diff line
@@ -97,13 +97,12 @@ static int count_hlist(struct net *net,
		       const struct nf_conntrack_tuple *tuple,
		       const struct nf_conntrack_tuple *tuple,
		       const union nf_inet_addr *addr,
		       const union nf_inet_addr *addr,
		       const union nf_inet_addr *mask,
		       const union nf_inet_addr *mask,
		       u_int8_t family)
		       u_int8_t family, bool *addit)
{
{
	const struct nf_conntrack_tuple_hash *found;
	const struct nf_conntrack_tuple_hash *found;
	struct xt_connlimit_conn *conn;
	struct xt_connlimit_conn *conn;
	struct hlist_node *n;
	struct hlist_node *n;
	struct nf_conn *found_ct;
	struct nf_conn *found_ct;
	bool addit = true;
	int matches = 0;
	int matches = 0;


	rcu_read_lock();
	rcu_read_lock();
@@ -126,7 +125,7 @@ static int count_hlist(struct net *net,
			 * We should not see tuples twice unless someone hooks
			 * We should not see tuples twice unless someone hooks
			 * this into a table without "-p tcp --syn".
			 * this into a table without "-p tcp --syn".
			 */
			 */
			addit = false;
			*addit = false;
		} else if (already_closed(found_ct)) {
		} else if (already_closed(found_ct)) {
			/*
			/*
			 * we do not care about connections which are
			 * we do not care about connections which are
@@ -146,18 +145,20 @@ static int count_hlist(struct net *net,


	rcu_read_unlock();
	rcu_read_unlock();


	if (addit) {
	return matches;
		/* save the new connection in our list */
}
		conn = kmalloc(sizeof(*conn), GFP_ATOMIC);

static bool add_hlist(struct hlist_head *head,
		      const struct nf_conntrack_tuple *tuple,
		      const union nf_inet_addr *addr)
{
	struct xt_connlimit_conn *conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
	if (conn == NULL)
	if (conn == NULL)
			return -ENOMEM;
		return false;
	conn->tuple = *tuple;
	conn->tuple = *tuple;
	conn->addr = *addr;
	conn->addr = *addr;
	hlist_add_head(&conn->node, head);
	hlist_add_head(&conn->node, head);
		++matches;
	return true;
	}

	return matches;
}
}


static int count_them(struct net *net,
static int count_them(struct net *net,
@@ -170,6 +171,7 @@ static int count_them(struct net *net,
	struct hlist_head *hhead;
	struct hlist_head *hhead;
	int count;
	int count;
	u32 hash;
	u32 hash;
	bool addit = true;


	if (family == NFPROTO_IPV6)
	if (family == NFPROTO_IPV6)
		hash = connlimit_iphash6(addr, mask);
		hash = connlimit_iphash6(addr, mask);
@@ -179,7 +181,13 @@ static int count_them(struct net *net,
	hhead = &data->iphash[hash];
	hhead = &data->iphash[hash];


	spin_lock_bh(&data->lock);
	spin_lock_bh(&data->lock);
	count = count_hlist(net, hhead, tuple, addr, mask, family);
	count = count_hlist(net, hhead, tuple, addr, mask, family, &addit);
	if (addit) {
		if (add_hlist(hhead, tuple, addr))
			count++;
		else
			count = -ENOMEM;
	}
	spin_unlock_bh(&data->lock);
	spin_unlock_bh(&data->lock);


	return count;
	return count;