Blame SOURCES/0063-nft-Simplify-immediate-parsing.patch

d8275f
From c65bd8b3c23f0fe5f824274467740a2d350dcb9c Mon Sep 17 00:00:00 2001
d8275f
From: Phil Sutter <phil@nwl.cc>
d8275f
Date: Tue, 1 Mar 2022 18:59:31 +0100
d8275f
Subject: [PATCH] nft: Simplify immediate parsing
d8275f
d8275f
Implementations of parse_immediate callback are mostly trivial, the only
d8275f
relevant part is access to family-specific parts of struct
d8275f
iptables_command_state when setting goto flag for iptables and
d8275f
ip6tables. Refactor them into simple set_goto_flag callbacks.
d8275f
d8275f
Signed-off-by: Phil Sutter <phil@nwl.cc>
d8275f
Acked-by: Florian Westphal <fw@strlen.de>
d8275f
(cherry picked from commit b5f2faea325a315bfb932ebc634f3298d4824cae)
d8275f
---
d8275f
 iptables/nft-arp.c    |  9 ---------
d8275f
 iptables/nft-bridge.c |  9 ---------
d8275f
 iptables/nft-ipv4.c   | 12 +++---------
d8275f
 iptables/nft-ipv6.c   | 12 +++---------
d8275f
 iptables/nft-shared.c | 17 +++++++----------
d8275f
 iptables/nft-shared.h |  2 +-
d8275f
 6 files changed, 14 insertions(+), 47 deletions(-)
d8275f
d8275f
diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
d8275f
index 7c61c31a13c40..0c37a762cd418 100644
d8275f
--- a/iptables/nft-arp.c
d8275f
+++ b/iptables/nft-arp.c
d8275f
@@ -182,14 +182,6 @@ static void nft_arp_parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e,
d8275f
 	fw->arp.invflags |= flags;
d8275f
 }
d8275f
 
d8275f
-static void nft_arp_parse_immediate(const char *jumpto, bool nft_goto,
d8275f
-				    void *data)
d8275f
-{
d8275f
-	struct iptables_command_state *cs = data;
d8275f
-
d8275f
-	cs->jumpto = jumpto;
d8275f
-}
d8275f
-
d8275f
 static void parse_mask_ipv4(struct nft_xt_ctx *ctx, struct in_addr *mask)
d8275f
 {
d8275f
 	mask->s_addr = ctx->bitwise.mask[0];
d8275f
@@ -575,7 +567,6 @@ struct nft_family_ops nft_family_ops_arp = {
d8275f
 	.print_payload		= NULL,
d8275f
 	.parse_meta		= nft_arp_parse_meta,
d8275f
 	.parse_payload		= nft_arp_parse_payload,
d8275f
-	.parse_immediate	= nft_arp_parse_immediate,
d8275f
 	.print_header		= nft_arp_print_header,
d8275f
 	.print_rule		= nft_arp_print_rule,
d8275f
 	.save_rule		= nft_arp_save_rule,
d8275f
diff --git a/iptables/nft-bridge.c b/iptables/nft-bridge.c
d8275f
index 2aa15e2d1e69d..e00a19e843d93 100644
d8275f
--- a/iptables/nft-bridge.c
d8275f
+++ b/iptables/nft-bridge.c
d8275f
@@ -284,14 +284,6 @@ static void nft_bridge_parse_payload(struct nft_xt_ctx *ctx,
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
-static void nft_bridge_parse_immediate(const char *jumpto, bool nft_goto,
d8275f
-				       void *data)
d8275f
-{
d8275f
-	struct iptables_command_state *cs = data;
d8275f
-
d8275f
-	cs->jumpto = jumpto;
d8275f
-}
d8275f
-
d8275f
 /* return 0 if saddr, 1 if daddr, -1 on error */
d8275f
 static int
d8275f
 lookup_check_ether_payload(uint32_t base, uint32_t offset, uint32_t len)
d8275f
@@ -948,7 +940,6 @@ struct nft_family_ops nft_family_ops_bridge = {
d8275f
 	.print_payload		= NULL,
d8275f
 	.parse_meta		= nft_bridge_parse_meta,
d8275f
 	.parse_payload		= nft_bridge_parse_payload,
d8275f
-	.parse_immediate	= nft_bridge_parse_immediate,
d8275f
 	.parse_lookup		= nft_bridge_parse_lookup,
d8275f
 	.parse_match		= nft_bridge_parse_match,
d8275f
 	.parse_target		= nft_bridge_parse_target,
d8275f
diff --git a/iptables/nft-ipv4.c b/iptables/nft-ipv4.c
d8275f
index d8c48ce8817b6..c826ac153139f 100644
d8275f
--- a/iptables/nft-ipv4.c
d8275f
+++ b/iptables/nft-ipv4.c
d8275f
@@ -241,15 +241,9 @@ static void nft_ipv4_parse_payload(struct nft_xt_ctx *ctx,
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
-static void nft_ipv4_parse_immediate(const char *jumpto, bool nft_goto,
d8275f
-				     void *data)
d8275f
+static void nft_ipv4_set_goto_flag(struct iptables_command_state *cs)
d8275f
 {
d8275f
-	struct iptables_command_state *cs = data;
d8275f
-
d8275f
-	cs->jumpto = jumpto;
d8275f
-
d8275f
-	if (nft_goto)
d8275f
-		cs->fw.ip.flags |= IPT_F_GOTO;
d8275f
+	cs->fw.ip.flags |= IPT_F_GOTO;
d8275f
 }
d8275f
 
d8275f
 static void print_fragment(unsigned int flags, unsigned int invflags,
d8275f
@@ -473,7 +467,7 @@ struct nft_family_ops nft_family_ops_ipv4 = {
d8275f
 	.is_same		= nft_ipv4_is_same,
d8275f
 	.parse_meta		= nft_ipv4_parse_meta,
d8275f
 	.parse_payload		= nft_ipv4_parse_payload,
d8275f
-	.parse_immediate	= nft_ipv4_parse_immediate,
d8275f
+	.set_goto_flag		= nft_ipv4_set_goto_flag,
d8275f
 	.print_header		= print_header,
d8275f
 	.print_rule		= nft_ipv4_print_rule,
d8275f
 	.save_rule		= nft_ipv4_save_rule,
d8275f
diff --git a/iptables/nft-ipv6.c b/iptables/nft-ipv6.c
d8275f
index a5481b3f77ac5..127bc96379968 100644
d8275f
--- a/iptables/nft-ipv6.c
d8275f
+++ b/iptables/nft-ipv6.c
d8275f
@@ -180,15 +180,9 @@ static void nft_ipv6_parse_payload(struct nft_xt_ctx *ctx,
d8275f
 	}
d8275f
 }
d8275f
 
d8275f
-static void nft_ipv6_parse_immediate(const char *jumpto, bool nft_goto,
d8275f
-				     void *data)
d8275f
+static void nft_ipv6_set_goto_flag(struct iptables_command_state *cs)
d8275f
 {
d8275f
-	struct iptables_command_state *cs = data;
d8275f
-
d8275f
-	cs->jumpto = jumpto;
d8275f
-
d8275f
-	if (nft_goto)
d8275f
-		cs->fw6.ipv6.flags |= IP6T_F_GOTO;
d8275f
+	cs->fw6.ipv6.flags |= IP6T_F_GOTO;
d8275f
 }
d8275f
 
d8275f
 static void nft_ipv6_print_rule(struct nft_handle *h, struct nftnl_rule *r,
d8275f
@@ -415,7 +409,7 @@ struct nft_family_ops nft_family_ops_ipv6 = {
d8275f
 	.is_same		= nft_ipv6_is_same,
d8275f
 	.parse_meta		= nft_ipv6_parse_meta,
d8275f
 	.parse_payload		= nft_ipv6_parse_payload,
d8275f
-	.parse_immediate	= nft_ipv6_parse_immediate,
d8275f
+	.set_goto_flag		= nft_ipv6_set_goto_flag,
d8275f
 	.print_header		= print_header,
d8275f
 	.print_rule		= nft_ipv6_print_rule,
d8275f
 	.save_rule		= nft_ipv6_save_rule,
d8275f
diff --git a/iptables/nft-shared.c b/iptables/nft-shared.c
d8275f
index 7f757d38ecaec..172cf2054a33c 100644
d8275f
--- a/iptables/nft-shared.c
d8275f
+++ b/iptables/nft-shared.c
d8275f
@@ -510,9 +510,7 @@ static void nft_parse_counter(struct nftnl_expr *e, struct xt_counters *counters
d8275f
 static void nft_parse_immediate(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
d8275f
 {
d8275f
 	const char *chain = nftnl_expr_get_str(e, NFTNL_EXPR_IMM_CHAIN);
d8275f
-	const char *jumpto = NULL;
d8275f
-	bool nft_goto = false;
d8275f
-	void *data = ctx->cs;
d8275f
+	struct iptables_command_state *cs = ctx->cs;
d8275f
 	int verdict;
d8275f
 
d8275f
 	if (nftnl_expr_is_set(e, NFTNL_EXPR_IMM_DATA)) {
d8275f
@@ -535,23 +533,22 @@ static void nft_parse_immediate(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
d8275f
 	/* Standard target? */
d8275f
 	switch(verdict) {
d8275f
 	case NF_ACCEPT:
d8275f
-		jumpto = "ACCEPT";
d8275f
+		cs->jumpto = "ACCEPT";
d8275f
 		break;
d8275f
 	case NF_DROP:
d8275f
-		jumpto = "DROP";
d8275f
+		cs->jumpto = "DROP";
d8275f
 		break;
d8275f
 	case NFT_RETURN:
d8275f
-		jumpto = "RETURN";
d8275f
+		cs->jumpto = "RETURN";
d8275f
 		break;;
d8275f
 	case NFT_GOTO:
d8275f
-		nft_goto = true;
d8275f
+		if (ctx->h->ops->set_goto_flag)
d8275f
+			ctx->h->ops->set_goto_flag(cs);
d8275f
 		/* fall through */
d8275f
 	case NFT_JUMP:
d8275f
-		jumpto = chain;
d8275f
+		cs->jumpto = chain;
d8275f
 		break;
d8275f
 	}
d8275f
-
d8275f
-	ctx->h->ops->parse_immediate(jumpto, nft_goto, data);
d8275f
 }
d8275f
 
d8275f
 static void nft_parse_limit(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
d8275f
diff --git a/iptables/nft-shared.h b/iptables/nft-shared.h
d8275f
index 520a296fb530c..29f7056714235 100644
d8275f
--- a/iptables/nft-shared.h
d8275f
+++ b/iptables/nft-shared.h
d8275f
@@ -89,7 +89,7 @@ struct nft_family_ops {
d8275f
 			  void *data);
d8275f
 	void (*parse_lookup)(struct nft_xt_ctx *ctx, struct nftnl_expr *e,
d8275f
 			     void *data);
d8275f
-	void (*parse_immediate)(const char *jumpto, bool nft_goto, void *data);
d8275f
+	void (*set_goto_flag)(struct iptables_command_state *cs);
d8275f
 
d8275f
 	void (*print_table_header)(const char *tablename);
d8275f
 	void (*print_header)(unsigned int format, const char *chain,
d8275f
-- 
d8275f
2.34.1
d8275f