Blame SOURCES/0042-xtables-Fix-position-of-replaced-rules-in-cache.patch

8cce6c
From 53422c35d925973702e043ac69119f87e08399e0 Mon Sep 17 00:00:00 2001
8cce6c
From: Phil Sutter <phil@nwl.cc>
8cce6c
Date: Tue, 15 Jan 2019 23:23:04 +0100
8cce6c
Subject: [PATCH] xtables: Fix position of replaced rules in cache
8cce6c
8cce6c
When replacing a rule, the replacement was simply appended to the
8cce6c
chain's rule list. Instead, insert it where the rule it replaces was.
8cce6c
8cce6c
This also fixes for zero counters command to remove the old rule from
8cce6c
cache.
8cce6c
8cce6c
Signed-off-by: Phil Sutter <phil@nwl.cc>
8cce6c
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8cce6c
(cherry picked from commit 5ca9acf51adf9dcc8e0d82cd8f5b9b2514f900ee)
8cce6c
Signed-off-by: Phil Sutter <psutter@redhat.com>
8cce6c
---
8cce6c
 iptables/nft.c         | 34 +++++++++++++++++-----------------
8cce6c
 iptables/nft.h         |  2 +-
8cce6c
 iptables/xtables-arp.c |  2 +-
8cce6c
 iptables/xtables-eb.c  |  2 +-
8cce6c
 iptables/xtables.c     |  4 ++--
8cce6c
 5 files changed, 22 insertions(+), 22 deletions(-)
8cce6c
8cce6c
diff --git a/iptables/nft.c b/iptables/nft.c
8cce6c
index c2af1a6fd0985..76764fde4e9fb 100644
8cce6c
--- a/iptables/nft.c
8cce6c
+++ b/iptables/nft.c
8cce6c
@@ -1186,7 +1186,7 @@ nft_chain_find(struct nft_handle *h, const char *table, const char *chain);
8cce6c
 
8cce6c
 int
8cce6c
 nft_rule_append(struct nft_handle *h, const char *chain, const char *table,
8cce6c
-		void *data, uint64_t handle, bool verbose)
8cce6c
+		void *data, struct nftnl_rule *ref, bool verbose)
8cce6c
 {
8cce6c
 	struct nftnl_chain *c;
8cce6c
 	struct nftnl_rule *r;
8cce6c
@@ -1202,8 +1202,9 @@ nft_rule_append(struct nft_handle *h, const char *chain, const char *table,
8cce6c
 	if (r == NULL)
8cce6c
 		return 0;
8cce6c
 
8cce6c
-	if (handle > 0) {
8cce6c
-		nftnl_rule_set(r, NFTNL_RULE_HANDLE, &handle);
8cce6c
+	if (ref) {
8cce6c
+		nftnl_rule_set_u64(r, NFTNL_RULE_HANDLE,
8cce6c
+				   nftnl_rule_get_u64(ref, NFTNL_RULE_HANDLE));
8cce6c
 		type = NFT_COMPAT_RULE_REPLACE;
8cce6c
 	} else
8cce6c
 		type = NFT_COMPAT_RULE_APPEND;
8cce6c
@@ -1216,12 +1217,17 @@ nft_rule_append(struct nft_handle *h, const char *chain, const char *table,
8cce6c
 	if (verbose)
8cce6c
 		h->ops->print_rule(r, 0, FMT_PRINT_RULE);
8cce6c
 
8cce6c
-	c = nft_chain_find(h, table, chain);
8cce6c
-	if (!c) {
8cce6c
-		errno = ENOENT;
8cce6c
-		return 0;
8cce6c
+	if (ref) {
8cce6c
+		nftnl_chain_rule_insert_at(r, ref);
8cce6c
+		nftnl_chain_rule_del(r);
8cce6c
+	} else {
8cce6c
+		c = nft_chain_find(h, table, chain);
8cce6c
+		if (!c) {
8cce6c
+			errno = ENOENT;
8cce6c
+			return 0;
8cce6c
+		}
8cce6c
+		nftnl_chain_rule_add_tail(r, c);
8cce6c
 	}
8cce6c
-	nftnl_chain_rule_add_tail(r, c);
8cce6c
 
8cce6c
 	return 1;
8cce6c
 }
8cce6c
@@ -2109,7 +2115,7 @@ int nft_rule_insert(struct nft_handle *h, const char *chain,
8cce6c
 			r = nft_rule_find(h, c, data, rulenum - 1);
8cce6c
 			if (r != NULL)
8cce6c
 				return nft_rule_append(h, chain, table, data,
8cce6c
-						       0, verbose);
8cce6c
+						       NULL, verbose);
8cce6c
 
8cce6c
 			errno = ENOENT;
8cce6c
 			goto err;
8cce6c
@@ -2181,11 +2187,7 @@ int nft_rule_replace(struct nft_handle *h, const char *chain,
8cce6c
 			(unsigned long long)
8cce6c
 			nftnl_rule_get_u64(r, NFTNL_RULE_HANDLE));
8cce6c
 
8cce6c
-		nftnl_rule_list_del(r);
8cce6c
-
8cce6c
-		ret = nft_rule_append(h, chain, table, data,
8cce6c
-				      nftnl_rule_get_u64(r, NFTNL_RULE_HANDLE),
8cce6c
-				      verbose);
8cce6c
+		ret = nft_rule_append(h, chain, table, data, r, verbose);
8cce6c
 	} else
8cce6c
 		errno = ENOENT;
8cce6c
 
8cce6c
@@ -2461,9 +2463,7 @@ int nft_rule_zero_counters(struct nft_handle *h, const char *chain,
8cce6c
 
8cce6c
 	cs.counters.pcnt = cs.counters.bcnt = 0;
8cce6c
 
8cce6c
-	ret =  nft_rule_append(h, chain, table, &cs,
8cce6c
-			       nftnl_rule_get_u64(r, NFTNL_RULE_HANDLE),
8cce6c
-			       false);
8cce6c
+	ret =  nft_rule_append(h, chain, table, &cs, r, false);
8cce6c
 
8cce6c
 error:
8cce6c
 	return ret;
8cce6c
diff --git a/iptables/nft.h b/iptables/nft.h
8cce6c
index dfdffd69342db..97d73c8b534be 100644
8cce6c
--- a/iptables/nft.h
8cce6c
+++ b/iptables/nft.h
8cce6c
@@ -98,7 +98,7 @@ bool nft_chain_exists(struct nft_handle *h, const char *table, const char *chain
8cce6c
  */
8cce6c
 struct nftnl_rule;
8cce6c
 
8cce6c
-int nft_rule_append(struct nft_handle *h, const char *chain, const char *table, void *data, uint64_t handle, bool verbose);
8cce6c
+int nft_rule_append(struct nft_handle *h, const char *chain, const char *table, void *data, struct nftnl_rule *ref, bool verbose);
8cce6c
 int nft_rule_insert(struct nft_handle *h, const char *chain, const char *table, void *data, int rulenum, bool verbose);
8cce6c
 int nft_rule_check(struct nft_handle *h, const char *chain, const char *table, void *data, bool verbose);
8cce6c
 int nft_rule_delete(struct nft_handle *h, const char *chain, const char *table, void *data, bool verbose);
8cce6c
diff --git a/iptables/xtables-arp.c b/iptables/xtables-arp.c
8cce6c
index 2dce1a52f16fd..18cdced627c55 100644
8cce6c
--- a/iptables/xtables-arp.c
8cce6c
+++ b/iptables/xtables-arp.c
8cce6c
@@ -825,7 +825,7 @@ append_entry(struct nft_handle *h,
8cce6c
 		for (j = 0; j < ndaddrs; j++) {
8cce6c
 			cs->arp.arp.tgt.s_addr = daddrs[j].s_addr;
8cce6c
 			if (append) {
8cce6c
-				ret = nft_rule_append(h, chain, table, cs, 0,
8cce6c
+				ret = nft_rule_append(h, chain, table, cs, NULL,
8cce6c
 						      verbose);
8cce6c
 			} else {
8cce6c
 				ret = nft_rule_insert(h, chain, table, cs,
8cce6c
diff --git a/iptables/xtables-eb.c b/iptables/xtables-eb.c
8cce6c
index 871891442e431..4c52c29aa4817 100644
8cce6c
--- a/iptables/xtables-eb.c
8cce6c
+++ b/iptables/xtables-eb.c
8cce6c
@@ -171,7 +171,7 @@ append_entry(struct nft_handle *h,
8cce6c
 	int ret = 1;
8cce6c
 
8cce6c
 	if (append)
8cce6c
-		ret = nft_rule_append(h, chain, table, cs, 0, verbose);
8cce6c
+		ret = nft_rule_append(h, chain, table, cs, NULL, verbose);
8cce6c
 	else
8cce6c
 		ret = nft_rule_insert(h, chain, table, cs, rule_nr, verbose);
8cce6c
 
8cce6c
diff --git a/iptables/xtables.c b/iptables/xtables.c
8cce6c
index da11e8cc159a0..d0167e6396975 100644
8cce6c
--- a/iptables/xtables.c
8cce6c
+++ b/iptables/xtables.c
8cce6c
@@ -406,7 +406,7 @@ add_entry(const char *chain,
8cce6c
 
8cce6c
 				if (append) {
8cce6c
 					ret = nft_rule_append(h, chain, table,
8cce6c
-							      cs, 0,
8cce6c
+							      cs, NULL,
8cce6c
 							      verbose);
8cce6c
 				} else {
8cce6c
 					ret = nft_rule_insert(h, chain, table,
8cce6c
@@ -426,7 +426,7 @@ add_entry(const char *chain,
8cce6c
 				       &d.mask.v6[j], sizeof(struct in6_addr));
8cce6c
 				if (append) {
8cce6c
 					ret = nft_rule_append(h, chain, table,
8cce6c
-							      cs, 0,
8cce6c
+							      cs, NULL,
8cce6c
 							      verbose);
8cce6c
 				} else {
8cce6c
 					ret = nft_rule_insert(h, chain, table,
8cce6c
-- 
8cce6c
2.20.1
8cce6c