Skip to contentSkip to conversation

nft-ruleparse: Introduce nft_create_target()

PATCH In review Phil Sutter 1 participant 3 messages Aug 4, 2023 – Aug 10, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/2 nft-ruleparse: Introduce nft_create_target()

Like nft_create_match(), this is a small wrapper around the typical target extension lookup and (standard) init code.

To use it from nft_parse_target() and nft_parse_log(), introduce an inner variant which accepts the target payload size as parameter.

The call to rule_parse_ops::target callback was problematic with standard target, because the callbacks initialized iptables_command_state::jumpto with the target name, "standard" in that case. Perform its tasks in nft_create_target(), keep it only for bridge family's special handling of watcher "targets".

---
 iptables/nft-ruleparse-arp.c    |   1 -
 iptables/nft-ruleparse-bridge.c |   5 +-
 iptables/nft-ruleparse-ipv4.c   |   1 -
 iptables/nft-ruleparse-ipv6.c   |   1 -
 iptables/nft-ruleparse.c        | 126 ++++++++++++--------------------
 iptables/nft-ruleparse.h        |   5 +-
 6 files changed, 52 insertions(+), 87 deletions(-)
iptables/nft-ruleparse-arp.c +0 -1
index b68fb06d8e0f9..d80ca922955cf 100644
@@ -164,5 +164,4 @@ static void nft_arp_parse_payload(struct nft_xt_ctx *ctx,
 struct nft_ruleparse_ops nft_ruleparse_ops_arp = {
 	.meta		= nft_arp_parse_meta,
 	.payload	= nft_arp_parse_payload,
-	.target		= nft_ipv46_parse_target,
 };
iptables/nft-ruleparse-bridge.c +2 -3
index 50fb92833046a..c6cc9af5ea198 100644
@@ -406,11 +406,10 @@ static void nft_bridge_parse_target(struct xtables_target *t,
 	if (strcmp(t->name, "log") == 0 ||
 	    strcmp(t->name, "nflog") == 0) {
 		parse_watcher(t, &cs->match_list, false);
+		cs->jumpto = NULL;
+		cs->target = NULL;
 		return;
 	}
-
-	cs->target = t;
-	cs->jumpto = t->name;
 }
 
 struct nft_ruleparse_ops nft_ruleparse_ops_bridge = {
iptables/nft-ruleparse-ipv4.c +0 -1
index c87e159cc5fec..491cbf42c7754 100644
@@ -131,5 +131,4 @@ static void nft_ipv4_parse_payload(struct nft_xt_ctx *ctx,
 struct nft_ruleparse_ops nft_ruleparse_ops_ipv4 = {
 	.meta		= nft_ipv4_parse_meta,
 	.payload	= nft_ipv4_parse_payload,
-	.target		= nft_ipv46_parse_target,
 };
iptables/nft-ruleparse-ipv6.c +0 -1
index af55420b73766..7581b8636e601 100644
@@ -108,5 +108,4 @@ static void nft_ipv6_parse_payload(struct nft_xt_ctx *ctx,
 struct nft_ruleparse_ops nft_ruleparse_ops_ipv6 = {
 	.meta		= nft_ipv6_parse_meta,
 	.payload	= nft_ipv6_parse_payload,
-	.target		= nft_ipv46_parse_target,
 };
iptables/nft-ruleparse.c +48 -78

193 lines not on this page · raw message

iptables/nft-ruleparse.h +2 -3
index fd083c08ff343..25ce05d2e8644 100644
@@ -117,6 +117,8 @@ extern struct nft_ruleparse_ops nft_ruleparse_ops_ipv6;
 void *nft_create_match(struct nft_xt_ctx *ctx,
 		       struct iptables_command_state *cs,
 		       const char *name, bool reuse);
+void *nft_create_target(struct nft_xt_ctx *ctx, const char *name);
+
 
 bool nft_rule_to_iptables_command_state(struct nft_handle *h,
 					const struct nftnl_rule *r,
@@ -129,9 +131,6 @@ int parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e, uint8_t key,
 	       char *iniface, unsigned char *iniface_mask, char *outiface,
 	       unsigned char *outiface_mask, uint8_t *invflags);
 
-void nft_ipv46_parse_target(struct xtables_target *t,
-			    struct iptables_command_state *cs);
-
 int nft_parse_hl(struct nft_xt_ctx *ctx, struct nftnl_expr *e,
 		 struct iptables_command_state *cs);
 
Signature · 1 line
2.40.0
author reviewer
#1 lore raw

PATCH v2 2/2 nft-ruleparse: parse meta mark set as MARK target

From: Florian Westphal <[email protected]>

Mixing nftables and iptables-nft in the same table doesn't work, but some people do this.

v1.8.8 ignored rules it could not represent in iptables syntax, v1.8.9 bails in this case.

Add parsing of meta mark expressions so iptables-nft can render them as -j MARK rules.

This is flawed, nft has features that have no corresponding syntax in iptables, but we can't undo this.

---
 iptables/nft-ruleparse.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)
iptables/nft-ruleparse.c +28 -12
index a5eb6d098084a..c8322f936acd9 100644
@@ -146,11 +146,6 @@ static bool nft_parse_meta_set_common(struct nft_xt_ctx* ctx,
 		return false;
 	}
 
-	if (sreg->immediate.data[0] == 0) {
-		ctx->errmsg = "meta sreg immediate is 0";
-		return false;
-	}
-
 	return true;
 }
 
@@ -159,7 +154,6 @@ static void nft_parse_meta_set(struct nft_xt_ctx *ctx,
 {
 	struct nft_xt_ctx_reg *sreg;
 	enum nft_registers sregnum;
-	const char *targname;
 
 	sregnum = nftnl_expr_get_u32(e, NFTNL_EXPR_META_SREG);
 	sreg = nft_xt_ctx_get_sreg(ctx, sregnum);
@@ -171,21 +165,43 @@ static void nft_parse_meta_set(struct nft_xt_ctx *ctx,
 		if (!nft_parse_meta_set_common(ctx, sreg))
 			return;
 
-		targname = "TRACE";
+		if (sreg->immediate.data[0] == 0) {
+			ctx->errmsg = "meta sreg immediate is 0";
+			return;
+		}
+
+		if (!nft_create_target(ctx, "TRACE"))
+			ctx->errmsg = "target TRACE not found";
 		break;
 	case NFT_META_BRI_BROUTE:
 		if (!nft_parse_meta_set_common(ctx, sreg))
 			return;
 
 		ctx->cs->jumpto = "DROP";
-		return;
+		break;
+	case NFT_META_MARK: {
+		struct xt_mark_tginfo2 *mt;
+
+		if (!nft_parse_meta_set_common(ctx, sreg))
+			return;
+
+		mt = nft_create_target(ctx, "MARK");
+		if (!mt) {
+			ctx->errmsg = "target MARK not found";
+			return;
+		}
+
+		mt->mark = sreg->immediate.data[0];
+		if (sreg->bitwise.set)
+			mt->mask = sreg->bitwise.mask[0];
+		else
+			mt->mask = ~0u;
+		break;
+	}
 	default:
 		ctx->errmsg = "meta sreg key not supported";
-		return;
+		break;
 	}
-
-	if (!nft_create_target(ctx, targname))
-		ctx->errmsg = "target TRACE not found";
 }
 
 static void nft_parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
Signature · 1 line
2.40.0
author reviewer
#2 lore raw
In reply to Phil Sutter
Quoted 6 lines · Phil Sutter

Like nft_create_match(), this is a small wrapper around the typical target extension lookup and (standard) init code.

To use it from nft_parse_target() and nft_parse_log(), introduce an inner variant which accepts the target payload size as parameter.

Quoted 7 lines

The call to rule_parse_ops::target callback was problematic with standard target, because the callbacks initialized iptables_command_state::jumpto with the target name, "standard" in that case. Perform its tasks in nft_create_target(), keep it only for bridge family's special handling of watcher "targets".

Series applied.