Skip to contentSkip to conversation

netfilter: nf_tables: attach replacement rule after stale one

PATCH New Pablo Neira Ayuso 1 participant 2 messages Sep 17, 2013

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 netfilter: nf_tables: attach replacement rule after stale one

Insert the new rule after the existing stale one that will be removed in the commit phase.

---
 net/netfilter/nf_tables_api.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +2 -1
index 755d596..0e55e1b 100644
@@ -1659,7 +1659,8 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
 		rupd = nf_tables_trans_add(old_rule, &ctx);
 		if (rupd == NULL)
 			goto err2;
-		list_add_tail_rcu(&rule->list, &chain->rules);
+
+		list_add_tail(&rule->list, &old_rule->list);
 	} else if (nlh->nlmsg_flags & NLM_F_APPEND)
 		if (old_rule)
 			list_add_rcu(&rule->list, &old_rule->list);
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH 2/2 netfilter: nf_tables: do not allow deletion/replacement of stale rules

Stale rules are those that will be purge out in the commit step. Do not allow to delete/replace a rule that is stale.

---
 net/netfilter/nf_tables_api.c |   27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +18 -9
index 0e55e1b..0627023 100644
@@ -1655,12 +1655,17 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
 	}
 
 	if (nlh->nlmsg_flags & NLM_F_REPLACE) {
-		nft_rule_disactivate_next(net, old_rule);
-		rupd = nf_tables_trans_add(old_rule, &ctx);
-		if (rupd == NULL)
-			goto err2;
+		if (nft_rule_is_active_next(net, old_rule)) {
+			nft_rule_disactivate_next(net, old_rule);
+			rupd = nf_tables_trans_add(old_rule, &ctx);
+			if (rupd == NULL)
+				goto err2;
 
-		list_add_tail(&rule->list, &old_rule->list);
+			list_add_tail(&rule->list, &old_rule->list);
+		} else {
+			nf_tables_rule_destroy(rule);
+			return -ENOENT;
+		}
 	} else if (nlh->nlmsg_flags & NLM_F_APPEND)
 		if (old_rule)
 			list_add_rcu(&rule->list, &old_rule->list);
@@ -1698,11 +1703,15 @@ err1:
 static int
 nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
 {
-	nft_rule_disactivate_next(ctx->net, rule);
-	if (nf_tables_trans_add(rule, ctx) == NULL)
-		return -ENOMEM;
+	/* You cannot delete the same rule twice */
+	if (nft_rule_is_active_next(ctx->net, rule)) {
+		nft_rule_disactivate_next(ctx->net, rule);
+		if (nf_tables_trans_add(rule, ctx) == NULL)
+			return -ENOMEM;
 
-	return 0;
+		return 0;
+	}
+	return -ENOENT;
 }
 
 static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
Signature · 1 line
1.7.10.4