Blame SOURCES/0019-xtables-Align-effect-of-4-6-options-with-legacy.patch

fc8f74
From aa221d3a7ffc8e3245d9031173b306431ddfaf9f Mon Sep 17 00:00:00 2001
fc8f74
From: Phil Sutter <phil@nwl.cc>
fc8f74
Date: Fri, 21 Feb 2020 13:18:32 +0100
fc8f74
Subject: [PATCH] xtables: Align effect of -4/-6 options with legacy
fc8f74
fc8f74
Legacy iptables doesn't accept -4 or -6 if they don't match the
fc8f74
symlink's native family. The only exception to that is iptables-restore
fc8f74
which simply ignores the lines introduced by non-matching options, which
fc8f74
is useful to create combined dump files for feeding into both
fc8f74
iptables-restore and ip6tables-restore.
fc8f74
fc8f74
Signed-off-by: Phil Sutter <phil@nwl.cc>
fc8f74
(cherry picked from commit 1639b8ba5105542c73e0e1c35e70f245dab89d81)
fc8f74
Signed-off-by: Phil Sutter <psutter@redhat.com>
fc8f74
---
fc8f74
 .../shell/testcases/iptables/0006-46-args_0   | 88 +++++++++++++++++++
fc8f74
 iptables/xtables.c                            | 21 ++---
fc8f74
 2 files changed, 96 insertions(+), 13 deletions(-)
fc8f74
 create mode 100755 iptables/tests/shell/testcases/iptables/0006-46-args_0
fc8f74
fc8f74
diff --git a/iptables/tests/shell/testcases/iptables/0006-46-args_0 b/iptables/tests/shell/testcases/iptables/0006-46-args_0
fc8f74
new file mode 100755
fc8f74
index 0000000000000..17a0a01829df5
fc8f74
--- /dev/null
fc8f74
+++ b/iptables/tests/shell/testcases/iptables/0006-46-args_0
fc8f74
@@ -0,0 +1,88 @@
fc8f74
+#!/bin/bash
fc8f74
+
fc8f74
+RC=0
fc8f74
+
fc8f74
+$XT_MULTI iptables -6 -A FORWARD -j ACCEPT
fc8f74
+rc=$?
fc8f74
+if [[ $rc -ne 2 ]]; then
fc8f74
+	echo "'iptables -6' returned $rc instead of 2"
fc8f74
+	RC=1
fc8f74
+fi
fc8f74
+
fc8f74
+$XT_MULTI ip6tables -4 -A FORWARD -j ACCEPT
fc8f74
+rc=$?
fc8f74
+if [[ $rc -ne 2 ]]; then
fc8f74
+	echo "'ip6tables -4' returned $rc instead of 2"
fc8f74
+	RC=1
fc8f74
+fi
fc8f74
+
fc8f74
+RULESET='*filter
fc8f74
+-4 -A FORWARD -d 10.0.0.1 -j ACCEPT
fc8f74
+-6 -A FORWARD -d fec0:10::1 -j ACCEPT
fc8f74
+COMMIT
fc8f74
+'
fc8f74
+EXPECT4='-P FORWARD ACCEPT
fc8f74
+-A FORWARD -d 10.0.0.1/32 -j ACCEPT'
fc8f74
+EXPECT6='-P FORWARD ACCEPT
fc8f74
+-A FORWARD -d fec0:10::1/128 -j ACCEPT'
fc8f74
+EXPECT_EMPTY='-P FORWARD ACCEPT'
fc8f74
+
fc8f74
+echo "$RULESET" | $XT_MULTI iptables-restore || {
fc8f74
+	echo "iptables-restore failed!"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT4") <($XT_MULTI iptables -S FORWARD) || {
fc8f74
+	echo "unexpected iptables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT_EMPTY") <($XT_MULTI ip6tables -S FORWARD) || {
fc8f74
+	echo "unexpected non-empty ip6tables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+
fc8f74
+$XT_MULTI iptables -F FORWARD
fc8f74
+
fc8f74
+echo "$RULESET" | $XT_MULTI ip6tables-restore || {
fc8f74
+	echo "ip6tables-restore failed!"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT6") <($XT_MULTI ip6tables -S FORWARD) || {
fc8f74
+	echo "unexpected ip6tables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT_EMPTY") <($XT_MULTI iptables -S FORWARD) || {
fc8f74
+	echo "unexpected non-empty iptables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+
fc8f74
+$XT_MULTI ip6tables -F FORWARD
fc8f74
+
fc8f74
+$XT_MULTI iptables -4 -A FORWARD -d 10.0.0.1 -j ACCEPT || {
fc8f74
+	echo "iptables failed!"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT4") <($XT_MULTI iptables -S FORWARD) || {
fc8f74
+	echo "unexpected iptables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT_EMPTY") <($XT_MULTI ip6tables -S FORWARD) || {
fc8f74
+	echo "unexpected non-empty ip6tables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+
fc8f74
+$XT_MULTI iptables -F FORWARD
fc8f74
+
fc8f74
+$XT_MULTI ip6tables -6 -A FORWARD -d fec0:10::1 -j ACCEPT || {
fc8f74
+	echo "ip6tables failed!"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT6") <($XT_MULTI ip6tables -S FORWARD) || {
fc8f74
+	echo "unexpected ip6tables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+diff -u -Z <(echo -e "$EXPECT_EMPTY") <($XT_MULTI iptables -S FORWARD) || {
fc8f74
+	echo "unexpected non-empty iptables ruleset"
fc8f74
+	RC=1
fc8f74
+}
fc8f74
+
fc8f74
+exit $RC
fc8f74
diff --git a/iptables/xtables.c b/iptables/xtables.c
fc8f74
index 4b24d15c46295..8c2d21d42b7d2 100644
fc8f74
--- a/iptables/xtables.c
fc8f74
+++ b/iptables/xtables.c
fc8f74
@@ -913,27 +913,22 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],
fc8f74
 			break;
fc8f74
 
fc8f74
 		case '4':
fc8f74
+			if (args->family == AF_INET)
fc8f74
+				break;
fc8f74
+
fc8f74
 			if (p->restore && args->family == AF_INET6)
fc8f74
 				return;
fc8f74
 
fc8f74
-			if (args->family != AF_INET)
fc8f74
-				exit_tryhelp(2);
fc8f74
-
fc8f74
-			h->ops = nft_family_ops_lookup(args->family);
fc8f74
-			break;
fc8f74
+			exit_tryhelp(2);
fc8f74
 
fc8f74
 		case '6':
fc8f74
+			if (args->family == AF_INET6)
fc8f74
+				break;
fc8f74
+
fc8f74
 			if (p->restore && args->family == AF_INET)
fc8f74
 				return;
fc8f74
 
fc8f74
-			args->family = AF_INET6;
fc8f74
-			xtables_set_nfproto(AF_INET6);
fc8f74
-
fc8f74
-			h->ops = nft_family_ops_lookup(args->family);
fc8f74
-			if (h->ops == NULL)
fc8f74
-				xtables_error(PARAMETER_PROBLEM,
fc8f74
-					      "Unknown family");
fc8f74
-			break;
fc8f74
+			exit_tryhelp(2);
fc8f74
 
fc8f74
 		case 1: /* non option */
fc8f74
 			if (optarg[0] == '!' && optarg[1] == '\0') {
fc8f74
-- 
fc8f74
2.26.2
fc8f74