Blame SOURCES/0037-arptables-nft-save-Fix-position-of-j-option.patch

8cce6c
From 59e25afcdc0a415c8b1bb6fff5fc14985d79e06b Mon Sep 17 00:00:00 2001
8cce6c
From: Phil Sutter <phil@nwl.cc>
8cce6c
Date: Thu, 31 Jan 2019 16:12:54 +0100
8cce6c
Subject: [PATCH] arptables-nft-save: Fix position of -j option
8cce6c
8cce6c
Legacy arptables-save (just like arptables itself) prints verdict as
8cce6c
first option, then matches and finally any target options.
8cce6c
8cce6c
To achieve this without introducing double/trailing spaces everywhere,
8cce6c
integrate target ('-j') option printing into
8cce6c
nft_arp_print_rule_details() and make it print separating whitespace
8cce6c
before each option.
8cce6c
8cce6c
In nft_arp_save_rule(), replace the call to save_matches_and_target() by
8cce6c
by a direct call to cs->target->save() since the former prints '-j'
8cce6c
option itself. Since there are no match extensions in arptables, any
8cce6c
other code from that function is not needed.
8cce6c
8cce6c
Signed-off-by: Phil Sutter <phil@nwl.cc>
8cce6c
Signed-off-by: Florian Westphal <fw@strlen.de>
8cce6c
(cherry picked from commit 2c3f7a2cd6fd8325b3a84e280cce945c6c20b87f)
8cce6c
Signed-off-by: Phil Sutter <psutter@redhat.com>
8cce6c
---
8cce6c
 iptables/nft-arp.c                            | 65 +++++++++++--------
8cce6c
 .../arptables/0001-arptables-save-restore_0   | 32 ++++-----
8cce6c
 .../0002-arptables-restore-defaults_0         |  6 +-
8cce6c
 3 files changed, 58 insertions(+), 45 deletions(-)
8cce6c
8cce6c
diff --git a/iptables/nft-arp.c b/iptables/nft-arp.c
8cce6c
index f357fc4a43c4c..2cbdf23214049 100644
8cce6c
--- a/iptables/nft-arp.c
8cce6c
+++ b/iptables/nft-arp.c
8cce6c
@@ -434,14 +434,21 @@ static void nft_arp_print_header(unsigned int format, const char *chain,
8cce6c
 	}
8cce6c
 }
8cce6c
 
8cce6c
-static void nft_arp_print_rule_details(const struct arpt_entry *fw,
8cce6c
+static void nft_arp_print_rule_details(const struct iptables_command_state *cs,
8cce6c
 				       unsigned int format)
8cce6c
 {
8cce6c
+	const struct arpt_entry *fw = &cs->arp;
8cce6c
 	char buf[BUFSIZ];
8cce6c
 	char iface[IFNAMSIZ+2];
8cce6c
+	const char *sep = "";
8cce6c
 	int print_iface = 0;
8cce6c
 	int i;
8cce6c
 
8cce6c
+	if (strlen(cs->jumpto)) {
8cce6c
+		printf("%s-j %s", sep, cs->jumpto);
8cce6c
+		sep = " ";
8cce6c
+	}
8cce6c
+
8cce6c
 	iface[0] = '\0';
8cce6c
 
8cce6c
 	if (fw->arp.iniface[0] != '\0') {
8cce6c
@@ -453,9 +460,11 @@ static void nft_arp_print_rule_details(const struct arpt_entry *fw,
8cce6c
 		if (format & FMT_NUMERIC) strcat(iface, "*");
8cce6c
 		else strcat(iface, "any");
8cce6c
 	}
8cce6c
-	if (print_iface)
8cce6c
-		printf("%s-i %s ", fw->arp.invflags & ARPT_INV_VIA_IN ?
8cce6c
+	if (print_iface) {
8cce6c
+		printf("%s%s-i %s", sep, fw->arp.invflags & ARPT_INV_VIA_IN ?
8cce6c
 				   "! " : "", iface);
8cce6c
+		sep = " ";
8cce6c
+	}
8cce6c
 
8cce6c
 	print_iface = 0;
8cce6c
 	iface[0] = '\0';
8cce6c
@@ -469,12 +478,14 @@ static void nft_arp_print_rule_details(const struct arpt_entry *fw,
8cce6c
 		if (format & FMT_NUMERIC) strcat(iface, "*");
8cce6c
 		else strcat(iface, "any");
8cce6c
 	}
8cce6c
-	if (print_iface)
8cce6c
-		printf("%s-o %s ", fw->arp.invflags & ARPT_INV_VIA_OUT ?
8cce6c
+	if (print_iface) {
8cce6c
+		printf("%s%s-o %s", sep, fw->arp.invflags & ARPT_INV_VIA_OUT ?
8cce6c
 				   "! " : "", iface);
8cce6c
+		sep = " ";
8cce6c
+	}
8cce6c
 
8cce6c
 	if (fw->arp.smsk.s_addr != 0L) {
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_SRCIP
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_SRCIP
8cce6c
 			? "! " : "");
8cce6c
 		if (format & FMT_NUMERIC)
8cce6c
 			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.src)));
8cce6c
@@ -482,7 +493,8 @@ static void nft_arp_print_rule_details(const struct arpt_entry *fw,
8cce6c
 			sprintf(buf, "%s", addr_to_anyname(&(fw->arp.src)));
8cce6c
 		strncat(buf, mask_to_dotted(&(fw->arp.smsk)),
8cce6c
 			sizeof(buf) - strlen(buf) - 1);
8cce6c
-		printf("-s %s ", buf);
8cce6c
+		printf("-s %s", buf);
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 
8cce6c
 	for (i = 0; i < ARPT_DEV_ADDR_LEN_MAX; i++)
8cce6c
@@ -490,16 +502,16 @@ static void nft_arp_print_rule_details(const struct arpt_entry *fw,
8cce6c
 			break;
8cce6c
 	if (i == ARPT_DEV_ADDR_LEN_MAX)
8cce6c
 		goto after_devsrc;
8cce6c
-	printf("%s", fw->arp.invflags & ARPT_INV_SRCDEVADDR
8cce6c
+	printf("%s%s", sep, fw->arp.invflags & ARPT_INV_SRCDEVADDR
8cce6c
 		? "! " : "");
8cce6c
 	printf("--src-mac ");
8cce6c
 	print_mac_and_mask((unsigned char *)fw->arp.src_devaddr.addr,
8cce6c
 		(unsigned char *)fw->arp.src_devaddr.mask, ETH_ALEN);
8cce6c
-	printf(" ");
8cce6c
+	sep = " ";
8cce6c
 after_devsrc:
8cce6c
 
8cce6c
 	if (fw->arp.tmsk.s_addr != 0L) {
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_TGTIP
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_TGTIP
8cce6c
 			? "! " : "");
8cce6c
 		if (format & FMT_NUMERIC)
8cce6c
 			sprintf(buf, "%s", addr_to_dotted(&(fw->arp.tgt)));
8cce6c
@@ -507,7 +519,8 @@ after_devsrc:
8cce6c
 			sprintf(buf, "%s", addr_to_anyname(&(fw->arp.tgt)));
8cce6c
 		strncat(buf, mask_to_dotted(&(fw->arp.tmsk)),
8cce6c
 			sizeof(buf) - strlen(buf) - 1);
8cce6c
-		printf("-d %s ", buf);
8cce6c
+		printf("-d %s", buf);
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 
8cce6c
 	for (i = 0; i 
8cce6c
@@ -515,28 +528,28 @@ after_devsrc:
8cce6c
 			break;
8cce6c
 	if (i == ARPT_DEV_ADDR_LEN_MAX)
8cce6c
 		goto after_devdst;
8cce6c
-	printf("%s", fw->arp.invflags & ARPT_INV_TGTDEVADDR
8cce6c
+	printf("%s%s", sep, fw->arp.invflags & ARPT_INV_TGTDEVADDR
8cce6c
 		? "! " : "");
8cce6c
 	printf("--dst-mac ");
8cce6c
 	print_mac_and_mask((unsigned char *)fw->arp.tgt_devaddr.addr,
8cce6c
 		(unsigned char *)fw->arp.tgt_devaddr.mask, ETH_ALEN);
8cce6c
-	printf(" ");
8cce6c
+	sep = " ";
8cce6c
 
8cce6c
 after_devdst:
8cce6c
 
8cce6c
 	if (fw->arp.arhln_mask != 0) {
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_ARPHLN
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPHLN
8cce6c
 			? "! " : "");
8cce6c
 		printf("--h-length %d", fw->arp.arhln);
8cce6c
 		if (fw->arp.arhln_mask != 255)
8cce6c
 			printf("/%d", fw->arp.arhln_mask);
8cce6c
-		printf(" ");
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 
8cce6c
 	if (fw->arp.arpop_mask != 0) {
8cce6c
 		int tmp = ntohs(fw->arp.arpop);
8cce6c
 
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_ARPOP
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPOP
8cce6c
 			? "! " : "");
8cce6c
 		if (tmp <= NUMOPCODES && !(format & FMT_NUMERIC))
8cce6c
 			printf("--opcode %s", opcodes[tmp-1]);
8cce6c
@@ -545,13 +558,13 @@ after_devdst:
8cce6c
 
8cce6c
 		if (fw->arp.arpop_mask != 65535)
8cce6c
 			printf("/%d", ntohs(fw->arp.arpop_mask));
8cce6c
-		printf(" ");
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 
8cce6c
 	if (fw->arp.arhrd_mask != 0) {
8cce6c
 		uint16_t tmp = ntohs(fw->arp.arhrd);
8cce6c
 
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_ARPHRD
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPHRD
8cce6c
 			? "! " : "");
8cce6c
 		if (tmp == 1 && !(format & FMT_NUMERIC))
8cce6c
 			printf("--h-type %s", "Ethernet");
8cce6c
@@ -559,13 +572,13 @@ after_devdst:
8cce6c
 			printf("--h-type %u", tmp);
8cce6c
 		if (fw->arp.arhrd_mask != 65535)
8cce6c
 			printf("/%d", ntohs(fw->arp.arhrd_mask));
8cce6c
-		printf(" ");
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 
8cce6c
 	if (fw->arp.arpro_mask != 0) {
8cce6c
 		int tmp = ntohs(fw->arp.arpro);
8cce6c
 
8cce6c
-		printf("%s", fw->arp.invflags & ARPT_INV_ARPPRO
8cce6c
+		printf("%s%s", sep, fw->arp.invflags & ARPT_INV_ARPPRO
8cce6c
 			? "! " : "");
8cce6c
 		if (tmp == 0x0800 && !(format & FMT_NUMERIC))
8cce6c
 			printf("--proto-type %s", "IPv4");
8cce6c
@@ -573,7 +586,7 @@ after_devdst:
8cce6c
 			printf("--proto-type 0x%x", tmp);
8cce6c
 		if (fw->arp.arpro_mask != 65535)
8cce6c
 			printf("/%x", ntohs(fw->arp.arpro_mask));
8cce6c
-		printf(" ");
8cce6c
+		sep = " ";
8cce6c
 	}
8cce6c
 }
8cce6c
 
8cce6c
@@ -584,8 +597,10 @@ nft_arp_save_rule(const void *data, unsigned int format)
8cce6c
 
8cce6c
 	format |= FMT_NUMERIC;
8cce6c
 
8cce6c
-	nft_arp_print_rule_details(&cs->arp, format);
8cce6c
-	save_matches_and_target(cs, false, &cs->arp, format);
8cce6c
+	nft_arp_print_rule_details(cs, format);
8cce6c
+	if (cs->target && cs->target->save)
8cce6c
+		cs->target->save(&cs->fw, cs->target->t);
8cce6c
+	printf("\n");
8cce6c
 }
8cce6c
 
8cce6c
 static void
8cce6c
@@ -598,9 +613,7 @@ nft_arp_print_rule(struct nftnl_rule *r, unsigned int num, unsigned int format)
8cce6c
 
8cce6c
 	nft_rule_to_iptables_command_state(r, &cs);
8cce6c
 
8cce6c
-	if (strlen(cs.jumpto))
8cce6c
-		printf("-j %s ", cs.jumpto);
8cce6c
-	nft_arp_print_rule_details(&cs.arp, format);
8cce6c
+	nft_arp_print_rule_details(&cs, format);
8cce6c
 	print_matches_and_target(&cs, format);
8cce6c
 
8cce6c
 	if (!(format & FMT_NOCOUNTS)) {
8cce6c
diff --git a/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0 b/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
8cce6c
index f8629551b0ba9..0664e3b38d5e8 100755
8cce6c
--- a/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
8cce6c
+++ b/iptables/tests/shell/testcases/arptables/0001-arptables-save-restore_0
8cce6c
@@ -35,22 +35,22 @@ DUMP='*filter
8cce6c
 :INPUT ACCEPT
8cce6c
 :OUTPUT DROP
8cce6c
 :foo -
8cce6c
--A INPUT -s 10.0.0.0/8 --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A INPUT -d 192.168.123.1 --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A INPUT --src-mac fe:ed:ba:be:00:01 --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A INPUT --dst-mac fe:ed:ba:be:00:01 --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A INPUT --h-length 6 --h-type 1 -j foo
8cce6c
--A INPUT --h-length 6 --h-type 1 
8cce6c
--A OUTPUT -o lo --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A OUTPUT -o eth134 --h-length 6 --h-type 1 -j mangle --mangle-ip-s 10.0.0.1
8cce6c
--A OUTPUT -o eth432 --h-length 6 --h-type 1 -j CLASSIFY --set-class feed:babe
8cce6c
--A OUTPUT -o eth432 --h-length 6 --opcode 1 --h-type 1 -j CLASSIFY --set-class feed:babe
8cce6c
--A foo -i lo --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A foo --h-length 6 --h-type 1 -j ACCEPT
8cce6c
--A foo --h-length 6 --h-type 1 -j MARK --set-mark 12345
8cce6c
--A foo --h-length 6 --opcode 1 --h-type 1 -j ACCEPT
8cce6c
--A foo --h-length 6 --h-type 1 --proto-type 0x800 -j ACCEPT
8cce6c
--A foo -i lo --h-length 6 --opcode 1 --h-type 1 --proto-type 0x800 -j ACCEPT
8cce6c
+-A INPUT -j ACCEPT -s 10.0.0.0/8 --h-length 6 --h-type 1
8cce6c
+-A INPUT -j ACCEPT -d 192.168.123.1 --h-length 6 --h-type 1
8cce6c
+-A INPUT -j ACCEPT --src-mac fe:ed:ba:be:00:01 --h-length 6 --h-type 1
8cce6c
+-A INPUT -j ACCEPT --dst-mac fe:ed:ba:be:00:01 --h-length 6 --h-type 1
8cce6c
+-A INPUT -j foo --h-length 6 --h-type 1
8cce6c
+-A INPUT  --h-length 6 --h-type 1
8cce6c
+-A OUTPUT -j ACCEPT -o lo --h-length 6 --h-type 1
8cce6c
+-A OUTPUT -j mangle -o eth134 --h-length 6 --h-type 1 --mangle-ip-s 10.0.0.1
8cce6c
+-A OUTPUT -j CLASSIFY -o eth432 --h-length 6 --h-type 1 --set-class feed:babe
8cce6c
+-A OUTPUT -j CLASSIFY -o eth432 --h-length 6 --opcode 1 --h-type 1 --set-class feed:babe
8cce6c
+-A foo -j ACCEPT -i lo --h-length 6 --h-type 1
8cce6c
+-A foo -j ACCEPT --h-length 6 --h-type 1
8cce6c
+-A foo -j MARK --h-length 6 --h-type 1 --set-mark 12345
8cce6c
+-A foo -j ACCEPT --h-length 6 --opcode 1 --h-type 1
8cce6c
+-A foo -j ACCEPT --h-length 6 --h-type 1 --proto-type 0x800
8cce6c
+-A foo -j ACCEPT -i lo --h-length 6 --opcode 1 --h-type 1 --proto-type 0x800
8cce6c
 '
8cce6c
 
8cce6c
 diff -u <(echo -e "$DUMP") <($XT_MULTI arptables-save)
8cce6c
diff --git a/iptables/tests/shell/testcases/arptables/0002-arptables-restore-defaults_0 b/iptables/tests/shell/testcases/arptables/0002-arptables-restore-defaults_0
8cce6c
index ee17da0023b82..d742c3d506305 100755
8cce6c
--- a/iptables/tests/shell/testcases/arptables/0002-arptables-restore-defaults_0
8cce6c
+++ b/iptables/tests/shell/testcases/arptables/0002-arptables-restore-defaults_0
8cce6c
@@ -11,7 +11,7 @@ set -e
8cce6c
 DUMP='*filter
8cce6c
 :OUTPUT ACCEPT
8cce6c
 -A OUTPUT -j mangle --mangle-ip-s 10.0.0.1
8cce6c
--A OUTPUT --h-length 6 --h-type 1 -j mangle --mangle-ip-d 10.0.0.2
8cce6c
+-A OUTPUT -j mangle --h-length 6 --h-type 1 --mangle-ip-d 10.0.0.2
8cce6c
 '
8cce6c
 
8cce6c
 # note how mangle-ip-s is unset in second rule
8cce6c
@@ -19,8 +19,8 @@ DUMP='*filter
8cce6c
 EXPECT='*filter
8cce6c
 :INPUT ACCEPT
8cce6c
 :OUTPUT ACCEPT
8cce6c
--A OUTPUT --h-length 6 --h-type 1 -j mangle --mangle-ip-s 10.0.0.1
8cce6c
--A OUTPUT --h-length 6 --h-type 1 -j mangle --mangle-ip-d 10.0.0.2
8cce6c
+-A OUTPUT -j mangle --h-length 6 --h-type 1 --mangle-ip-s 10.0.0.1
8cce6c
+-A OUTPUT -j mangle --h-length 6 --h-type 1 --mangle-ip-d 10.0.0.2
8cce6c
 '
8cce6c
 
8cce6c
 $XT_MULTI arptables -F
8cce6c
-- 
8cce6c
2.20.1
8cce6c