Skip to contentSkip to conversation

Extract nftnl_rule parsing code

PATCH In review Phil Sutter 1 participant 5 messages Apr 21, 2023 – May 3, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Extract nftnl_rule parsing code

nft-shared.c was already oversized, upcoming enhancement of the nftnl_rule parser will add to that. So prepare any further work in that field by creating a common 'nft-ruleparse.c' source and one for each family to hold all the parsing code (basically the stack below nft_rule_to_iptables_command_state).

Collect the existing expression parsing callbacks in a new struct nft_ruleparse_ops and add a pointer to it into nft_family_ops. This way the callbacks may be static and the nft-ruleparse-<family>.c sources only export their ops object.

This series does things somewhat gradually:

* First pull everything from nft-shared.c into nft-ruleparse.c (likewise with header files)
* Then perform the *_ops struct changes which should not have a functional implication
* Finally weed parsers from nft-<family>.c files into nft-ruleparse-<family>.c ones.

Phil Sutter (3):
nft: Introduce nft-ruleparse.{c,h}
nft: Extract rule parsing callbacks from nft_family_ops
nft: ruleparse: Create family-specific source files

 iptables/Makefile.am            |    3 +
 iptables/nft-arp.c              |  139 +---
 iptables/nft-bridge.c           |  390 +---------
 iptables/nft-cache.h            |    2 +
 iptables/nft-ipv4.c             |  106 +--
 iptables/nft-ipv6.c             |   83 +--
 iptables/nft-ruleparse-arp.c    |  168 +++++
 iptables/nft-ruleparse-bridge.c |  422 +++++++++++
 iptables/nft-ruleparse-ipv4.c   |  135 ++++
 iptables/nft-ruleparse-ipv6.c   |  112 +++
 iptables/nft-ruleparse.c        | 1208 +++++++++++++++++++++++++++++++
 iptables/nft-ruleparse.h        |  138 ++++
 iptables/nft-shared.c           | 1190 ------------------------------
 iptables/nft-shared.h           |  115 +--
 14 files changed, 2194 insertions(+), 2017 deletions(-)
 create mode 100644 iptables/nft-ruleparse-arp.c
 create mode 100644 iptables/nft-ruleparse-bridge.c
 create mode 100644 iptables/nft-ruleparse-ipv4.c
 create mode 100644 iptables/nft-ruleparse-ipv6.c
 create mode 100644 iptables/nft-ruleparse.c
 create mode 100644 iptables/nft-ruleparse.h
Signature · 1 line
2.40.0
author reviewer
#1 lore raw

PATCH 1/3 nft: Introduce nft-ruleparse.{c,h}

Extract all code dealing with parsing from struct nftnl_rule into struct iptables_command_state from nft-shared.c into a separate source file.

Basically this is nft_rule_to_iptables_command_state() and the functions it calls, plus family-independent parsers called from family-specific callbacks.

---
 iptables/Makefile.am     |    1 +
 iptables/nft-ruleparse.c | 1208 ++++++++++++++++++++++++++++++++++++++
 iptables/nft-ruleparse.h |  117 ++++
 iptables/nft-shared.c    | 1190 -------------------------------------
 iptables/nft-shared.h    |  101 +---
 5 files changed, 1327 insertions(+), 1290 deletions(-)
 create mode 100644 iptables/nft-ruleparse.c
 create mode 100644 iptables/nft-ruleparse.h
iptables/Makefile.am +1 -0
index 1f37640f263c9..d5922da6a2d84 100644
@@ -46,6 +46,7 @@ xtables_nft_multi_SOURCES += nft.c nft.h \
 			     nft-cache.c nft-cache.h \
 			     nft-chain.c nft-chain.h \
 			     nft-cmd.c nft-cmd.h \
+			     nft-ruleparse.c nft-ruleparse.h \
 			     nft-shared.c nft-shared.h \
 			     xtables-monitor.c \
 			     xtables.c xtables-arp.c xtables-eb.c \
iptables/nft-ruleparse.c new file +1208 -0

1,208 lines not on this page · raw message

iptables/nft-ruleparse.h new file +117 -0

117 lines not on this page · raw message

iptables/nft-shared.c +0 -1190

1,220 lines not on this page · raw message

iptables/nft-shared.h +1 -100

133 lines not on this page · raw message

Signature · 1 line
2.40.0
author reviewer
#2 lore raw

PATCH 2/3 nft: Extract rule parsing callbacks from nft_family_ops

In reply to Phil Sutter

Introduce struct nft_ruleparse_ops holding the family-specific expression parsers and integrate it into nft_family_ops for now.

---
 iptables/nft-arp.c       |  9 ++++++---
 iptables/nft-bridge.c    | 18 +++++++++++-------
 iptables/nft-ipv4.c      | 10 +++++++---
 iptables/nft-ipv6.c      | 10 +++++++---
 iptables/nft-ruleparse.c | 24 ++++++++++++------------
 iptables/nft-ruleparse.h | 16 ++++++++++++++++
 iptables/nft-shared.h    | 14 +-------------
 7 files changed, 60 insertions(+), 41 deletions(-)
iptables/nft-arp.c +6 -3
index 8963573a72e9e..7c7122374bb63 100644
@@ -779,23 +779,26 @@ nft_arp_replace_entry(struct nft_handle *h,
 	return nft_cmd_rule_replace(h, chain, table, cs, rulenum, verbose);
 }
 
+static struct nft_ruleparse_ops nft_ruleparse_ops_arp = {
+	.meta		= nft_arp_parse_meta,
+	.payload	= nft_arp_parse_payload,
+	.target		= nft_ipv46_parse_target,
+};
 struct nft_family_ops nft_family_ops_arp = {
 	.add			= nft_arp_add,
 	.is_same		= nft_arp_is_same,
 	.print_payload		= NULL,
-	.parse_meta		= nft_arp_parse_meta,
-	.parse_payload		= nft_arp_parse_payload,
 	.print_header		= nft_arp_print_header,
 	.print_rule		= nft_arp_print_rule,
 	.save_rule		= nft_arp_save_rule,
 	.save_chain		= nft_arp_save_chain,
+	.rule_parse		= &nft_ruleparse_ops_arp,
 	.cmd_parse		= {
 		.post_parse	= nft_arp_post_parse,
 	},
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
 	.init_cs		= nft_arp_init_cs,
 	.clear_cs		= xtables_clear_iptables_command_state,
-	.parse_target		= nft_ipv46_parse_target,
 	.add_entry		= nft_arp_add_entry,
 	.delete_entry		= nft_arp_delete_entry,
 	.check_entry		= nft_arp_check_entry,
iptables/nft-bridge.c +11 -7
index 22860d6b91a6f..0c9e1238f4c21 100644
@@ -560,8 +560,8 @@ static void nft_bridge_parse_lookup(struct nft_xt_ctx *ctx,
 		match->m->u.user.revision = match->revision;
 		xs_init_match(match);
 
-		if (ctx->h->ops->parse_match != NULL)
-			ctx->h->ops->parse_match(match, ctx->cs);
+		if (ctx->h->ops->rule_parse->match != NULL)
+			ctx->h->ops->rule_parse->match(match, ctx->cs);
 	}
 	if (!match)
 		return;
@@ -984,15 +984,19 @@ static int nft_bridge_xlate(const struct iptables_command_state *cs,
 	return ret;
 }
 
+static struct nft_ruleparse_ops nft_ruleparse_ops_bridge = {
+	.meta		= nft_bridge_parse_meta,
+	.payload	= nft_bridge_parse_payload,
+	.lookup		= nft_bridge_parse_lookup,
+	.match		= nft_bridge_parse_match,
+	.target		= nft_bridge_parse_target,
+};
+
 struct nft_family_ops nft_family_ops_bridge = {
 	.add			= nft_bridge_add,
 	.is_same		= nft_bridge_is_same,
 	.print_payload		= NULL,
-	.parse_meta		= nft_bridge_parse_meta,
-	.parse_payload		= nft_bridge_parse_payload,
-	.parse_lookup		= nft_bridge_parse_lookup,
-	.parse_match		= nft_bridge_parse_match,
-	.parse_target		= nft_bridge_parse_target,
+	.rule_parse		= &nft_ruleparse_ops_bridge,
 	.print_table_header	= nft_bridge_print_table_header,
 	.print_header		= nft_bridge_print_header,
 	.print_rule		= nft_bridge_print_rule,
iptables/nft-ipv4.c +7 -3
index fadadd2eb9ed6..3f769e88663ac 100644
@@ -440,21 +440,25 @@ nft_ipv4_replace_entry(struct nft_handle *h,
 	return nft_cmd_rule_replace(h, chain, table, cs, rulenum, verbose);
 }
 
+static struct nft_ruleparse_ops nft_ruleparse_ops_ipv4 = {
+	.meta		= nft_ipv4_parse_meta,
+	.payload	= nft_ipv4_parse_payload,
+	.target		= nft_ipv46_parse_target,
+};
+
 struct nft_family_ops nft_family_ops_ipv4 = {
 	.add			= nft_ipv4_add,
 	.is_same		= nft_ipv4_is_same,
-	.parse_meta		= nft_ipv4_parse_meta,
-	.parse_payload		= nft_ipv4_parse_payload,
 	.set_goto_flag		= nft_ipv4_set_goto_flag,
 	.print_header		= print_header,
 	.print_rule		= nft_ipv4_print_rule,
 	.save_rule		= nft_ipv4_save_rule,
 	.save_chain		= nft_ipv46_save_chain,
+	.rule_parse		= &nft_ruleparse_ops_ipv4,
 	.cmd_parse		= {
 		.proto_parse	= ipv4_proto_parse,
 		.post_parse	= ipv4_post_parse,
 	},
-	.parse_target		= nft_ipv46_parse_target,
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
 	.clear_cs		= xtables_clear_iptables_command_state,
 	.xlate			= nft_ipv4_xlate,
iptables/nft-ipv6.c +7 -3
index 85bb683f4862b..962aaf0d13831 100644
@@ -409,21 +409,25 @@ nft_ipv6_replace_entry(struct nft_handle *h,
 	return nft_cmd_rule_replace(h, chain, table, cs, rulenum, verbose);
 }
 
+static struct nft_ruleparse_ops nft_ruleparse_ops_ipv6 = {
+	.meta		= nft_ipv6_parse_meta,
+	.payload	= nft_ipv6_parse_payload,
+	.target		= nft_ipv46_parse_target,
+};
+
 struct nft_family_ops nft_family_ops_ipv6 = {
 	.add			= nft_ipv6_add,
 	.is_same		= nft_ipv6_is_same,
-	.parse_meta		= nft_ipv6_parse_meta,
-	.parse_payload		= nft_ipv6_parse_payload,
 	.set_goto_flag		= nft_ipv6_set_goto_flag,
 	.print_header		= print_header,
 	.print_rule		= nft_ipv6_print_rule,
 	.save_rule		= nft_ipv6_save_rule,
 	.save_chain		= nft_ipv46_save_chain,
+	.rule_parse		= &nft_ruleparse_ops_ipv6,
 	.cmd_parse		= {
 		.proto_parse	= ipv6_proto_parse,
 		.post_parse	= ipv6_post_parse,
 	},
-	.parse_target		= nft_ipv46_parse_target,
 	.rule_to_cs		= nft_rule_to_iptables_command_state,
 	.clear_cs		= xtables_clear_iptables_command_state,
 	.xlate			= nft_ipv6_xlate,
iptables/nft-ruleparse.c +12 -12
index 2d84241a16819..edbbfa40e9c43 100644
@@ -78,8 +78,8 @@ nft_create_match(struct nft_xt_ctx *ctx,
 
 	xs_init_match(match);
 
-	if (ctx->h->ops->parse_match)
-		ctx->h->ops->parse_match(match, cs);
+	if (ctx->h->ops->rule_parse->match)
+		ctx->h->ops->rule_parse->match(match, cs);
 
 	return match->m->data;
 }
@@ -168,7 +168,7 @@ static void nft_parse_meta_set(struct nft_xt_ctx *ctx,
 
 	target->t = t;
 
-	ctx->h->ops->parse_target(target, ctx->cs);
+	ctx->h->ops->rule_parse->target(target, ctx->cs);
 }
 
 static void nft_parse_meta(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
@@ -488,16 +488,16 @@ static void nft_parse_cmp(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 		ctx->errmsg = "cmp sreg undef";
 		break;
 	case NFT_XT_REG_META_DREG:
-		ctx->h->ops->parse_meta(ctx, sreg, e, ctx->cs);
+		ctx->h->ops->rule_parse->meta(ctx, sreg, e, ctx->cs);
 		break;
 	case NFT_XT_REG_PAYLOAD:
 		switch (sreg->payload.base) {
 		case NFT_PAYLOAD_LL_HEADER:
 			if (ctx->h->family == NFPROTO_BRIDGE)
-				ctx->h->ops->parse_payload(ctx, sreg, e, ctx->cs);
+				ctx->h->ops->rule_parse->payload(ctx, sreg, e, ctx->cs);
 			break;
 		case NFT_PAYLOAD_NETWORK_HEADER:
-			ctx->h->ops->parse_payload(ctx, sreg, e, ctx->cs);
+			ctx->h->ops->rule_parse->payload(ctx, sreg, e, ctx->cs);
 			break;
 		case NFT_PAYLOAD_TRANSPORT_HEADER:
 			nft_parse_transport(ctx, e, ctx->cs);
@@ -615,8 +615,8 @@ static void nft_parse_match(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 
 	match->m = m;
 
-	if (ctx->h->ops->parse_match != NULL)
-		ctx->h->ops->parse_match(match, ctx->cs);
+	if (ctx->h->ops->rule_parse->match != NULL)
+		ctx->h->ops->rule_parse->match(match, ctx->cs);
 }
 
 static void nft_parse_target(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
@@ -644,7 +644,7 @@ static void nft_parse_target(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 
 	target->t = t;
 
-	ctx->h->ops->parse_target(target, ctx->cs);
+	ctx->h->ops->rule_parse->target(target, ctx->cs);
 }
 
 static void nft_parse_limit(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
@@ -678,8 +678,8 @@ static void nft_parse_limit(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 static void nft_parse_lookup(struct nft_xt_ctx *ctx, struct nft_handle *h,
 			     struct nftnl_expr *e)
 {
-	if (ctx->h->ops->parse_lookup)
-		ctx->h->ops->parse_lookup(ctx, e);
+	if (ctx->h->ops->rule_parse->lookup)
+		ctx->h->ops->rule_parse->lookup(ctx, e);
 }
 
 static void nft_parse_log(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
@@ -729,7 +729,7 @@ static void nft_parse_log(struct nft_xt_ctx *ctx, struct nftnl_expr *e)
 
 	memcpy(&target->t->data, &info, sizeof(info));
 
-	ctx->h->ops->parse_target(target, ctx->cs);
+	ctx->h->ops->rule_parse->target(target, ctx->cs);
 }
 
 static void nft_parse_udp_range(struct nft_xt_ctx *ctx,
iptables/nft-ruleparse.h +16 -0
index 7fac6c7969645..69e98817bb6e1 100644
@@ -93,6 +93,22 @@ static inline struct nft_xt_ctx_reg *nft_xt_ctx_get_dreg(struct nft_xt_ctx *ctx,
 	return r;
 }
 
+struct nft_ruleparse_ops {
+	void (*meta)(struct nft_xt_ctx *ctx,
+		     const struct nft_xt_ctx_reg *sreg,
+		     struct nftnl_expr *e,
+		     struct iptables_command_state *cs);
+	void (*payload)(struct nft_xt_ctx *ctx,
+			const struct nft_xt_ctx_reg *sreg,
+			struct nftnl_expr *e,
+			struct iptables_command_state *cs);
+	void (*lookup)(struct nft_xt_ctx *ctx, struct nftnl_expr *e);
+	void (*match)(struct xtables_match *m,
+		      struct iptables_command_state *cs);
+	void (*target)(struct xtables_target *t,
+		       struct iptables_command_state *cs);
+};
+
 void *nft_create_match(struct nft_xt_ctx *ctx,
 		       struct iptables_command_state *cs,
 		       const char *name, bool reuse);
iptables/nft-shared.h +1 -13
index 2edee64920e8b..a06b263d77c1d 100644
@@ -46,15 +46,6 @@ struct nft_family_ops {
 			const struct iptables_command_state *cs_b);
 	void (*print_payload)(struct nftnl_expr *e,
 			      struct nftnl_expr_iter *iter);
-	void (*parse_meta)(struct nft_xt_ctx *ctx,
-			   const struct nft_xt_ctx_reg *sreg,
-			   struct nftnl_expr *e,
-			   struct iptables_command_state *cs);
-	void (*parse_payload)(struct nft_xt_ctx *ctx,
-			      const struct nft_xt_ctx_reg *sreg,
-			      struct nftnl_expr *e,
-			      struct iptables_command_state *cs);
-	void (*parse_lookup)(struct nft_xt_ctx *ctx, struct nftnl_expr *e);
 	void (*set_goto_flag)(struct iptables_command_state *cs);
 
 	void (*print_table_header)(const char *tablename);
@@ -67,11 +58,8 @@ struct nft_family_ops {
 	void (*save_rule)(const struct iptables_command_state *cs,
 			  unsigned int format);
 	void (*save_chain)(const struct nftnl_chain *c, const char *policy);
+	struct nft_ruleparse_ops *rule_parse;
 	struct xt_cmd_parse_ops cmd_parse;
-	void (*parse_match)(struct xtables_match *m,
-			    struct iptables_command_state *cs);
-	void (*parse_target)(struct xtables_target *t,
-			     struct iptables_command_state *cs);
 	void (*init_cs)(struct iptables_command_state *cs);
 	bool (*rule_to_cs)(struct nft_handle *h, const struct nftnl_rule *r,
 			   struct iptables_command_state *cs);
Signature · 1 line
2.40.0
author reviewer
#3 lore raw

PATCH 3/3 nft: ruleparse: Create family-specific source files

In reply to Phil Sutter

Extract the remaining nftnl rule parsing code from nft-<family>.c sources into dedicated ones to complete the separation.

---
 iptables/Makefile.am            |   2 +
 iptables/nft-arp.c              | 140 -----------
 iptables/nft-bridge.c           | 392 -----------------------------
 iptables/nft-cache.h            |   2 +
 iptables/nft-ipv4.c             | 108 --------
 iptables/nft-ipv6.c             |  85 -------
 iptables/nft-ruleparse-arp.c    | 168 +++++++++++++
 iptables/nft-ruleparse-bridge.c | 422 ++++++++++++++++++++++++++++++++
 iptables/nft-ruleparse-ipv4.c   | 135 ++++++++++
 iptables/nft-ruleparse-ipv6.c   | 112 +++++++++
 iptables/nft-ruleparse.h        |   5 +
 11 files changed, 846 insertions(+), 725 deletions(-)
 create mode 100644 iptables/nft-ruleparse-arp.c
 create mode 100644 iptables/nft-ruleparse-bridge.c
 create mode 100644 iptables/nft-ruleparse-ipv4.c
 create mode 100644 iptables/nft-ruleparse-ipv6.c
iptables/Makefile.am +2 -0
index d5922da6a2d84..8a7227024987f 100644
@@ -47,6 +47,8 @@ xtables_nft_multi_SOURCES += nft.c nft.h \
 			     nft-chain.c nft-chain.h \
 			     nft-cmd.c nft-cmd.h \
 			     nft-ruleparse.c nft-ruleparse.h \
+			     nft-ruleparse-arp.c nft-ruleparse-bridge.c \
+			     nft-ruleparse-ipv4.c nft-ruleparse-ipv6.c \
 			     nft-shared.c nft-shared.h \
 			     xtables-monitor.c \
 			     xtables.c xtables-arp.c xtables-eb.c \
iptables/nft-arp.c +0 -140

152 lines not on this page · raw message

iptables/nft-bridge.c +0 -392

404 lines not on this page · raw message

iptables/nft-cache.h +2 -0
index 58a015265056c..29ec6b5c3232b 100644
@@ -1,6 +1,8 @@
 #ifndef _NFT_CACHE_H_
 #define _NFT_CACHE_H_
 
+#include <libnftnl/chain.h>
+
 struct nft_handle;
 struct nft_chain;
 struct nft_cmd;
iptables/nft-ipv4.c +0 -108

120 lines not on this page · raw message

iptables/nft-ipv6.c +0 -85

97 lines not on this page · raw message

iptables/nft-ruleparse-arp.c new file +168 -0

168 lines not on this page · raw message

iptables/nft-ruleparse-bridge.c new file +422 -0

422 lines not on this page · raw message

iptables/nft-ruleparse-ipv4.c new file +135 -0

135 lines not on this page · raw message

iptables/nft-ruleparse-ipv6.c new file +112 -0

112 lines not on this page · raw message

iptables/nft-ruleparse.h +5 -0
index 69e98817bb6e1..fd083c08ff343 100644
@@ -109,6 +109,11 @@ struct nft_ruleparse_ops {
 		       struct iptables_command_state *cs);
 };
 
+extern struct nft_ruleparse_ops nft_ruleparse_ops_arp;
+extern struct nft_ruleparse_ops nft_ruleparse_ops_bridge;
+extern struct nft_ruleparse_ops nft_ruleparse_ops_ipv4;
+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);
Signature · 1 line
2.40.0
author reviewer
#4 lore raw
In reply to Phil Sutter
Quoted 11 lines · Phil Sutter

nft-shared.c was already oversized, upcoming enhancement of the nftnl_rule parser will add to that. So prepare any further work in that field by creating a common 'nft-ruleparse.c' source and one for each family to hold all the parsing code (basically the stack below nft_rule_to_iptables_command_state).

Collect the existing expression parsing callbacks in a new struct nft_ruleparse_ops and add a pointer to it into nft_family_ops. This way the callbacks may be static and the nft-ruleparse-<family>.c sources only export their ops object.

Quoted 8 lines

This series does things somewhat gradually:

* First pull everything from nft-shared.c into nft-ruleparse.c (likewise with header files)
* Then perform the *_ops struct changes which should not have a functional implication
* Finally weed parsers from nft-<family>.c files into nft-ruleparse-<family>.c ones.

Series applied after rebasing it onto current HEAD - the previous arptables-related fixes caused a minor conflict.