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

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