Skip to contentSkip to conversation

Misc fixes (more or less)

PATCH In review Phil Sutter 1 participant 14 messages Nov 22, 2023 – Nov 23, 2023 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover Misc fixes (more or less)

This is early fallout from working on a merge of ebtables commandline parsers with the shared one. It is a mix of actual bug fixes, small improvements and an implementaion of --change-counters command for ebtables-nft.

Phil Sutter (12):
Makefile: Install arptables-translate link and man page
nft-bridge: nft_bridge_add() uses wrong flags
xshared: struct xt_cmd_parse::xlate is unused
xshared: All variants support -v, update OPTSTRING_COMMON
xshared: Drop needless assignment in --help case
xshared: Drop pointless CMD_REPLACE check
tests: xlate: Print failing command line
ebtables: Drop append_entry() wrapper
ebtables: Make ebt_load_match_extensions() static
ebtables: Align line number formatting with legacy
xshared: do_parse: Ignore '-j CONTINUE'
ebtables: Implement --change-counters command

 iptables/Makefile.am                          |  11 +-
 iptables/nft-bridge.c                         |   6 +-
 iptables/nft-bridge.h                         |   1 -
 iptables/nft-cmd.c                            |  20 ++++
 iptables/nft-cmd.h                            |  12 +++
 iptables/nft.c                                |  65 +++++++++++
 iptables/nft.h                                |   1 +
 .../testcases/ebtables/0010-change-counters_0 |  45 ++++++++
 iptables/xshared.c                            |  11 +-
 iptables/xshared.h                            |   9 +-
 iptables/xtables-eb.c                         | 102 ++++++++++--------
 iptables/xtables-translate.8                  |  12 ++-
 iptables/xtables-translate.c                  |   1 -
 xlate-test.py                                 |   5 +-
 14 files changed, 229 insertions(+), 72 deletions(-)
 create mode 100755 iptables/tests/shell/testcases/ebtables/0010-change-counters_0
Signature · 1 line
2.41.0
author reviewer
#1 lore raw

PATCH 1/12 Makefile: Install arptables-translate link and man page

While at it, introduce a variable to hold the various semantic links to xtables-translate.8 man page.

Fixes 5b7324e0675e3 ("nft-arp: add arptables-translate")
---
 iptables/Makefile.am         | 11 +++++++----
 iptables/xtables-translate.8 | 12 ++++++++----
 2 files changed, 15 insertions(+), 8 deletions(-)
iptables/Makefile.am +7 -4
index 8a7227024987f..0f8b430c20213 100644
@@ -69,10 +69,12 @@ man_MANS         = iptables.8 iptables-restore.8 iptables-save.8 \
 dist_sbin_SCRIPTS = iptables-apply
 dist_pkgdata_DATA = iptables.xslt
 
+xlate_man_links = iptables-translate.8 ip6tables-translate.8 \
+		  iptables-restore-translate.8 ip6tables-restore-translate.8 \
+		  ebtables-translate.8 arptables-translate.8
+
 if ENABLE_NFTABLES
-man_MANS	+= iptables-translate.8 ip6tables-translate.8 \
-		   iptables-restore-translate.8 ip6tables-restore-translate.8 \
-		   xtables-monitor.8 ebtables-translate.8
+man_MANS	+= ${xlate_man_links} xtables-monitor.8
 
 dist_man_MANS	 = xtables-nft.8 xtables-translate.8 xtables-legacy.8 \
 		   arptables-nft.8 arptables-nft-restore.8 arptables-nft-save.8 \
@@ -97,6 +99,7 @@ x_sbin_links  = iptables-nft iptables-nft-restore iptables-nft-save \
 		arptables-nft arptables \
 		arptables-nft-restore arptables-restore \
 		arptables-nft-save arptables-save \
+		arptables-translate \
 		ebtables-nft ebtables \
 		ebtables-nft-restore ebtables-restore \
 		ebtables-nft-save ebtables-save \
@@ -108,7 +111,7 @@ iptables-extensions.8: iptables-extensions.8.tmpl ../extensions/matches.man ../e
 		-e '/@MATCH@/ r ../extensions/matches.man' \
 		-e '/@TARGET@/ r ../extensions/targets.man' $< >$@;
 
-iptables-translate.8 ip6tables-translate.8 iptables-restore-translate.8 ip6tables-restore-translate.8 ebtables-translate.8:
+${xlate_man_links}:
 	${AM_VERBOSE_GEN} echo '.so man8/xtables-translate.8' >$@
 
 ip6tables.8 ip6tables-apply.8 ip6tables-restore.8 ip6tables-save.8:
iptables/xtables-translate.8 +8 -4
index ba16c5257c4a5..fe1278874b4da 100644
@@ -30,10 +30,12 @@ iptables-translate \(em translation tool to migrate from iptables to nftables
 ip6tables-translate \(em translation tool to migrate from ip6tables to nftables
 .P
 ebtables-translate \(em translation tool to migrate from ebtables to nftables
+.P
+arptables-translate \(em translation tool to migrate from arptables to nftables
 .SH DESCRIPTION
 There is a set of tools to help the system administrator translate a given
-ruleset from \fBiptables(8)\fP, \fBip6tables(8)\fP and \fBebtables(8)\fP to
-\fBnftables(8)\fP.
+ruleset from \fBiptables(8)\fP, \fBip6tables(8)\fP, \fBebtables(8)\fP and
+\fBarptables(8)\fP to \fBnftables(8)\fP.
 
 The available commands are:
 
@@ -47,11 +49,13 @@ ip6tables\-translate
 ip6tables\-restore\-translate
 .IP \[bu] 2
 ebtables\-translate
+.IP \[bu] 2
+arptables\-translate
 
 .SH USAGE
 They take as input the original
-\fBiptables(8)\fP/\fBip6tables(8)\fP/\fBebtables(8)\fP syntax and
-output the native \fBnftables(8)\fP syntax.
+\fBiptables(8)\fP/\fBip6tables(8)\fP/\fBebtables(8)\fP/\fBarptables(8)\fP
+syntax and output the native \fBnftables(8)\fP syntax.
 
 The \fBiptables-restore-translate\fP tool reads a ruleset in the syntax
 produced by \fBiptables-save(8)\fP. Likewise, the
Signature · 1 line
2.41.0
author reviewer
#2 lore raw

PATCH 2/12 nft-bridge: nft_bridge_add() uses wrong flags

In reply to Phil Sutter

When checking whether -s or -d was given, invflags were used by accident. This change has no functional effect since the values remain the same, but this way it's clear where the previously assigned flags are used.

Fixes 07f4ca9681688 ("xtables-compat: ebtables: allow checking for zero-mac")
---
 iptables/nft-bridge.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
iptables/nft-bridge.c +2 -2
index d9a8ad2b0f373..772525e1b45a9 100644
@@ -134,14 +134,14 @@ static int nft_bridge_add(struct nft_handle *h, struct nft_rule_ctx *ctx,
 	struct ebt_entry *fw = &cs->eb;
 	uint32_t op;
 
-	if (fw->bitmask & EBT_ISOURCE) {
+	if (fw->bitmask & EBT_SOURCEMAC) {
 		op = nft_invflags2cmp(fw->invflags, EBT_ISOURCE);
 		add_addr(h, r, NFT_PAYLOAD_LL_HEADER,
 			 offsetof(struct ethhdr, h_source),
 			 fw->sourcemac, fw->sourcemsk, ETH_ALEN, op);
 	}
 
-	if (fw->bitmask & EBT_IDEST) {
+	if (fw->bitmask & EBT_DESTMAC) {
 		op = nft_invflags2cmp(fw->invflags, EBT_IDEST);
 		add_addr(h, r, NFT_PAYLOAD_LL_HEADER,
 			 offsetof(struct ethhdr, h_dest),
Signature · 1 line
2.41.0
author reviewer
#3 lore raw

PATCH 3/12 xshared: struct xt_cmd_parse::xlate is unused

In reply to Phil Sutter

Drop the boolean, it was meant to disable some existence checks in do_parse() prior to the caching rework. Now that do_parse() runs before any caching is done, the checks in question don't exist anymore so drop this relict.

Fixes a7f1e208cdf9c ("nft: split parsing from netlink commands")
---
 iptables/xshared.h           | 1 -
 iptables/xtables-translate.c | 1 -
 2 files changed, 2 deletions(-)
iptables/xshared.h +0 -1
index 5586385456a4d..c77556a1987dc 100644
@@ -284,7 +284,6 @@ struct xt_cmd_parse {
 	bool				restore;
 	int				line;
 	int				verbose;
-	bool				xlate;
 	struct xt_cmd_parse_ops		*ops;
 };
 
iptables/xtables-translate.c +0 -1
index ea9dce204dfc9..ad44311230323 100644
@@ -259,7 +259,6 @@ static int do_command_xlate(struct nft_handle *h, int argc, char *argv[],
 		.table		= *table,
 		.restore	= restore,
 		.line		= line,
-		.xlate		= true,
 		.ops		= &h->ops->cmd_parse,
 	};
 	struct iptables_command_state cs = {
Signature · 1 line
2.41.0
author reviewer
#4 lore raw

PATCH 4/12 xshared: All variants support -v

In reply to Phil Sutter

Update OPTSTRING_COMMON and remove the flag from *_OPTSTRING defines.

Fixes 51d9d9e081344 ("ebtables: Support verbose mode")
---
 iptables/xshared.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
iptables/xshared.h +4 -4
index c77556a1987dc..815b9d3e98726 100644
@@ -75,10 +75,10 @@ struct xtables_globals;
 struct xtables_rule_match;
 struct xtables_target;
 
-#define OPTSTRING_COMMON "-:A:C:D:E:F::I:L::M:N:P:VX::Z::" "c:d:i:j:o:p:s:t:"
-#define IPT_OPTSTRING	OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nvw::x"
-#define ARPT_OPTSTRING	OPTSTRING_COMMON "R:S::" "h::l:nvx" /* "m:" */
-#define EBT_OPTSTRING	OPTSTRING_COMMON "hv"
+#define OPTSTRING_COMMON "-:A:C:D:E:F::I:L::M:N:P:VX::Z::" "c:d:i:j:o:p:s:t:v"
+#define IPT_OPTSTRING	OPTSTRING_COMMON "R:S::W::" "46bfg:h::m:nw::x"
+#define ARPT_OPTSTRING	OPTSTRING_COMMON "R:S::" "h::l:nx" /* "m:" */
+#define EBT_OPTSTRING	OPTSTRING_COMMON "h"
 
 /* define invflags which won't collide with IPT ones.
  * arptables-nft does NOT use the legacy ARPT_INV_* defines.
Signature · 1 line
2.41.0
author reviewer
#5 lore raw

PATCH 5/12 xshared: Drop needless assignment in --help case

In reply to Phil Sutter

Help text printing code does not refer to optarg, so there is no need to assign to it if unset.

---
 iptables/xshared.c | 3 ---
 1 file changed, 3 deletions(-)
iptables/xshared.c +0 -3
index 5f75a0a57a023..53e72b7abb1e8 100644
@@ -1527,9 +1527,6 @@ void do_parse(int argc, char *argv[],
 			break;
 
 		case 'h':
-			if (!optarg)
-				optarg = argv[optind];
-
 			/* iptables -p icmp -h */
 			if (!cs->matches && cs->protocol)
 				xtables_find_match(cs->protocol,
Signature · 1 line
2.41.0
author reviewer
#6 lore raw

PATCH 6/12 xshared: Drop pointless CMD_REPLACE check

In reply to Phil Sutter

All current users set default source and destination addresses in their post_parse callbacks, so legacy variants are safe and nft variants don't have this restriction anyway.

---
 iptables/xshared.c | 5 -----
 1 file changed, 5 deletions(-)
iptables/xshared.c +0 -5
index 53e72b7abb1e8..c4d7a266fed5e 100644
@@ -1836,11 +1836,6 @@ void do_parse(int argc, char *argv[],
 	if (p->ops->post_parse)
 		p->ops->post_parse(p->command, cs, args);
 
-	if (p->command == CMD_REPLACE &&
-	    (args->s.naddrs != 1 || args->d.naddrs != 1))
-		xtables_error(PARAMETER_PROBLEM, "Replacement rule does not "
-			   "specify a unique address");
-
 	generic_opt_check(p->command, cs->options);
 
 	if (p->chain != NULL && strlen(p->chain) >= XT_EXTENSION_MAXNAMELEN)
Signature · 1 line
2.41.0
author reviewer
#7 lore raw

PATCH 7/12 tests: xlate: Print failing command line

In reply to Phil Sutter

If the command segfaults, 'error' variable is empty and the resulting error message is even misleading as the called program may not have been iptables-translate.

---
 xlate-test.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xlate-test.py +3 -2
index ddd68b91d3a7e..b6a78bb250e27 100755
@@ -41,9 +41,10 @@ xtables_nft_multi = 'xtables-nft-multi'
 
 
 def test_one_xlate(name, sourceline, expected, result):
-    rc, output, error = run_proc([xtables_nft_multi] + shlex.split(sourceline))
+    cmd = [xtables_nft_multi] + shlex.split(sourceline)
+    rc, output, error = run_proc(cmd)
     if rc != 0:
-        result.append(name + ": " + red("Error: ") + "iptables-translate failure")
+        result.append(name + ": " + red("Error: ") + "Call failed: " + " ".join(cmd))
         result.append(error)
         return False
 
Signature · 1 line
2.41.0
author reviewer
#8 lore raw

PATCH 8/12 ebtables: Drop append_entry() wrapper

In reply to Phil Sutter

There is no point in having it when there is no code to share.

---
 iptables/xtables-eb.c | 26 ++++----------------------
 1 file changed, 4 insertions(+), 22 deletions(-)
iptables/xtables-eb.c +4 -22
index a8ad57c735cc5..3fa5c179ba4b1 100644
@@ -112,24 +112,6 @@ static int parse_rule_number(const char *rule)
 	return rule_nr;
 }
 
-static int
-append_entry(struct nft_handle *h,
-	     const char *chain,
-	     const char *table,
-	     struct iptables_command_state *cs,
-	     int rule_nr,
-	     bool verbose, bool append)
-{
-	int ret = 1;
-
-	if (append)
-		ret = nft_cmd_rule_append(h, chain, table, cs, verbose);
-	else
-		ret = nft_cmd_rule_insert(h, chain, table, cs, rule_nr, verbose);
-
-	return ret;
-}
-
 static int
 delete_entry(struct nft_handle *h,
 	     const char *chain,
@@ -1178,11 +1160,11 @@ int do_commandeb(struct nft_handle *h, int argc, char *argv[], char **table,
 	} else if (command == 'F') {
 		ret = nft_cmd_rule_flush(h, chain, *table, flags & OPT_VERBOSE);
 	} else if (command == 'A') {
-		ret = append_entry(h, chain, *table, &cs, 0,
-				   flags & OPT_VERBOSE, true);
+		ret = nft_cmd_rule_append(h, chain, *table, &cs,
+					  flags & OPT_VERBOSE);
 	} else if (command == 'I') {
-		ret = append_entry(h, chain, *table, &cs, rule_nr - 1,
-				   flags & OPT_VERBOSE, false);
+		ret = nft_cmd_rule_insert(h, chain, *table, &cs,
+					  rule_nr - 1, flags & OPT_VERBOSE);
 	} else if (command == 'D') {
 		ret = delete_entry(h, chain, *table, &cs, rule_nr - 1,
 				   rule_nr_end, flags & OPT_VERBOSE);
Signature · 1 line
2.41.0
author reviewer
#9 lore raw

PATCH 9/12 ebtables: Make ebt_load_match_extensions() static

In reply to Phil Sutter

The function is not used outside of xtables-eb.c.

---
 iptables/nft-bridge.h | 1 -
 iptables/xtables-eb.c | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)
iptables/nft-bridge.h +0 -1
index eb1b3928b6543..0e6a29650acca 100644
@@ -115,7 +115,6 @@ static inline const char *ebt_target_name(unsigned int verdict)
 })								\
 
 void ebt_cs_clean(struct iptables_command_state *cs);
-void ebt_load_match_extensions(void);
 void ebt_add_match(struct xtables_match *m,
 			  struct iptables_command_state *cs);
 void ebt_add_watcher(struct xtables_target *watcher,
iptables/xtables-eb.c +1 -1
index 3fa5c179ba4b1..cd45e0495ebcb 100644
@@ -504,7 +504,7 @@ static void ebt_load_watcher(const char *name)
 		xtables_error(OTHER_PROBLEM, "Can't alloc memory");
 }
 
-void ebt_load_match_extensions(void)
+static void ebt_load_match_extensions(void)
 {
 	opts = ebt_original_options;
 	ebt_load_match("802_3");
Signature · 1 line
2.41.0
author reviewer
#10 lore raw

PATCH 10/12 ebtables: Align line number formatting with legacy

In reply to Phil Sutter

Legacy ebtables appends a dot to the number printed in first column if --Ln flag was given.

Fixes da871de2a6efb ("nft: bootstrap ebtables-compat")
---
 iptables/nft-bridge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/nft-bridge.c +1 -1
index 772525e1b45a9..1fcdeaf2cad68 100644
@@ -354,7 +354,7 @@ static void nft_bridge_print_rule(struct nft_handle *h, struct nftnl_rule *r,
 	struct iptables_command_state cs = {};
 
 	if (format & FMT_LINENUMBERS)
-		printf("%d ", num);
+		printf("%d. ", num);
 
 	nft_rule_to_ebtables_command_state(h, r, &cs);
 	__nft_bridge_save_rule(&cs, format);
Signature · 1 line
2.41.0
author reviewer
#11 lore raw

PATCH 11/12 xshared: do_parse: Ignore '-j CONTINUE'

In reply to Phil Sutter

While iptables does not support his NOP, arptables man page claims it does (although legacy arptables rejects it) and ebtables prefers to print it instead of omitting the '-j' option.

Accept and ignore the target when parsing to at least fix for arptables-nft and prepare for ebtables-nft using do_parse() as well.

---
 iptables/xshared.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
iptables/xshared.c +2 -1
index c4d7a266fed5e..1b02f35a9de3a 100644
@@ -1634,7 +1634,8 @@ void do_parse(int argc, char *argv[],
 		case 'j':
 			set_option(&cs->options, OPT_JUMP, &args->invflags,
 				   invert);
-			command_jump(cs, optarg);
+			if (strcmp(optarg, "CONTINUE"))
+				command_jump(cs, optarg);
 			break;
 
 		case 'i':
Signature · 1 line
2.41.0
author reviewer
#12 lore raw

PATCH 12/12 ebtables: Implement --change-counters command

In reply to Phil Sutter

Treat it like --replace against the same rule with changed counters. The operation is obviously not atomic, so rule counters may change in kernel while the rule is fetched, modified and replaced.

---
 iptables/nft-cmd.c                            | 20 +++++
 iptables/nft-cmd.h                            | 12 +++
 iptables/nft.c                                | 65 ++++++++++++++++
 iptables/nft.h                                |  1 +
 .../testcases/ebtables/0010-change-counters_0 | 45 +++++++++++
 iptables/xtables-eb.c                         | 74 ++++++++++++++-----
 6 files changed, 197 insertions(+), 20 deletions(-)
 create mode 100755 iptables/tests/shell/testcases/ebtables/0010-change-counters_0
iptables/nft-cmd.c +20 -0
index 8a824586ad8c3..8372d171b00c4 100644
@@ -400,3 +400,23 @@ int ebt_cmd_user_chain_policy(struct nft_handle *h, const char *table,
 
 	return 1;
 }
+
+int nft_cmd_rule_change_counters(struct nft_handle *h,
+				 const char *chain, const char *table,
+				 struct iptables_command_state *cs,
+				 int rule_nr, uint8_t counter_op, bool verbose)
+{
+	struct nft_cmd *cmd;
+
+	cmd = nft_cmd_new(h, NFT_COMPAT_RULE_CHANGE_COUNTERS, table, chain,
+			  rule_nr == -1 ? cs : NULL, rule_nr, verbose);
+	if (!cmd)
+		return 0;
+
+	cmd->counter_op = counter_op;
+	cmd->counters = cs->counters;
+
+	nft_cache_level_set(h, NFT_CL_RULES, cmd);
+
+	return 1;
+}
iptables/nft-cmd.h +12 -0
index ae5908d8d596b..8163b82c3511f 100644
@@ -7,6 +7,13 @@
 
 struct nftnl_rule;
 
+enum {
+	CTR_OP_INC_PKTS = 1 << 0,
+	CTR_OP_DEC_PKTS = 1 << 1,
+	CTR_OP_INC_BYTES = 1 << 2,
+	CTR_OP_DEC_BYTES = 1 << 3,
+};
+
 struct nft_cmd {
 	struct list_head		head;
 	int				command;
@@ -22,6 +29,7 @@ struct nft_cmd {
 	} obj;
 	const char			*policy;
 	struct xt_counters		counters;
+	uint8_t				counter_op;
 	const char			*rename;
 	int				counters_save;
 	struct {
@@ -77,6 +85,10 @@ int nft_cmd_rule_list_save(struct nft_handle *h, const char *chain,
 			   const char *table, int rulenum, int counters);
 int ebt_cmd_user_chain_policy(struct nft_handle *h, const char *table,
 			      const char *chain, const char *policy);
+int nft_cmd_rule_change_counters(struct nft_handle *h,
+				 const char *chain, const char *table,
+				 struct iptables_command_state *cs,
+				 int rule_nr, uint8_t counter_op, bool verbose);
 void nft_cmd_table_new(struct nft_handle *h, const char *table);
 
 #endif /* _NFT_CMD_H_ */
iptables/nft.c +65 -0

101 lines not on this page · raw message

iptables/nft.h +1 -0
index 5acbbf82e2c29..79f1e037cd6d3 100644
@@ -72,6 +72,7 @@ enum obj_update_type {
 	NFT_COMPAT_RULE_SAVE,
 	NFT_COMPAT_RULE_ZERO,
 	NFT_COMPAT_BRIDGE_USER_CHAIN_UPDATE,
+	NFT_COMPAT_RULE_CHANGE_COUNTERS,
 };
 
 struct cache_chain {
iptables/tests/shell/testcases/ebtables/0010-change-counters_0 new file +45 -0

45 lines not on this page · raw message

iptables/xtables-eb.c +54 -20

129 lines not on this page · raw message

Signature · 1 line
2.41.0
author reviewer
#13 lore raw
In reply to Phil Sutter
Quoted 5 lines · Phil Sutter

This is early fallout from working on a merge of ebtables commandline parsers with the shared one. It is a mix of actual bug fixes, small improvements and an implementaion of --change-counters command for ebtables-nft.

Quoted 13 lines

Phil Sutter (12):
Makefile: Install arptables-translate link and man page
nft-bridge: nft_bridge_add() uses wrong flags
xshared: struct xt_cmd_parse::xlate is unused
xshared: All variants support -v, update OPTSTRING_COMMON
xshared: Drop needless assignment in --help case
xshared: Drop pointless CMD_REPLACE check
tests: xlate: Print failing command line
ebtables: Drop append_entry() wrapper
ebtables: Make ebt_load_match_extensions() static
ebtables: Align line number formatting with legacy
xshared: do_parse: Ignore '-j CONTINUE'
ebtables: Implement --change-counters command

Series applied.