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

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