Blame SOURCES/0073-Improve-error-messages-for-unsupported-extensions.patch

d8275f
From 4243bd97f3c703a75e795fdc6dd2273a7c74e85c Mon Sep 17 00:00:00 2001
d8275f
From: Phil Sutter <phil@nwl.cc>
d8275f
Date: Fri, 11 Feb 2022 17:47:22 +0100
d8275f
Subject: [PATCH] Improve error messages for unsupported extensions
d8275f
d8275f
If a given extension was not supported by the kernel, iptables would
d8275f
print a rather confusing error message if extension parameters were
d8275f
given:
d8275f
d8275f
| # rm /lib/modules/$(uname -r)/kernel/net/netfilter/xt_LOG.ko
d8275f
| # iptables -A FORWARD -j LOG --log-prefix foo
d8275f
| iptables v1.8.7 (legacy): unknown option "--log-prefix"
d8275f
d8275f
Avoid this by pretending extension revision 0 is always supported. It is
d8275f
the same hack as used to successfully print extension help texts as
d8275f
unprivileged user, extended to all error codes to serve privileged ones
d8275f
as well.
d8275f
d8275f
In addition, print a warning if kernel rejected revision 0 and it's not
d8275f
a permissions problem. This helps users find out which extension in a
d8275f
rule the kernel didn't like.
d8275f
d8275f
Finally, the above commands result in these messages:
d8275f
d8275f
| Warning: Extension LOG revision 0 not supported, missing kernel module?
d8275f
| iptables: No chain/target/match by that name.
d8275f
d8275f
Or, for iptables-nft:
d8275f
d8275f
| Warning: Extension LOG revision 0 not supported, missing kernel module?
d8275f
| iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain FORWARD
d8275f
d8275f
Signed-off-by: Phil Sutter <phil@nwl.cc>
d8275f
(cherry picked from commit 17534cb18ed0a5052dc45c117401251359dba6aa)
d8275f
---
d8275f
 iptables/nft.c       | 12 +++++++++---
d8275f
 libxtables/xtables.c |  7 ++++++-
d8275f
 2 files changed, 15 insertions(+), 4 deletions(-)
d8275f
d8275f
diff --git a/iptables/nft.c b/iptables/nft.c
d8275f
index c5cc6f83bf573..9643abf2d0085 100644
d8275f
--- a/iptables/nft.c
d8275f
+++ b/iptables/nft.c
d8275f
@@ -3110,10 +3110,16 @@ int nft_compatible_revision(const char *name, uint8_t rev, int opt)
d8275f
 err:
d8275f
 	mnl_socket_close(nl);
d8275f
 
d8275f
-	/* pretend revision 0 is valid if not permitted to check -
d8275f
-	 * this is required for printing extension help texts as user */
d8275f
-	if (ret < 0 && errno == EPERM && rev == 0)
d8275f
+	/* pretend revision 0 is valid -
d8275f
+	 * this is required for printing extension help texts as user, also
d8275f
+	 * helps error messaging on unavailable kernel extension */
d8275f
+	if (ret < 0 && rev == 0) {
d8275f
+		if (errno != EPERM)
d8275f
+			fprintf(stderr,
d8275f
+				"Warning: Extension %s revision 0 not supported, missing kernel module?\n",
d8275f
+				name);
d8275f
 		return 1;
d8275f
+	}
d8275f
 
d8275f
 	return ret < 0 ? 0 : 1;
d8275f
 }
d8275f
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
d8275f
index 57ad0330a454c..a5c8d7e2c17ef 100644
d8275f
--- a/libxtables/xtables.c
d8275f
+++ b/libxtables/xtables.c
d8275f
@@ -968,7 +968,12 @@ int xtables_compatible_revision(const char *name, uint8_t revision, int opt)
d8275f
 		/* Definitely don't support this? */
d8275f
 		if (errno == ENOENT || errno == EPROTONOSUPPORT) {
d8275f
 			close(sockfd);
d8275f
-			return 0;
d8275f
+			/* Pretend revision 0 support for better error messaging */
d8275f
+			if (revision == 0)
d8275f
+				fprintf(stderr,
d8275f
+					"Warning: Extension %s revision 0 not supported, missing kernel module?\n",
d8275f
+					name);
d8275f
+			return (revision == 0);
d8275f
 		} else if (errno == ENOPROTOOPT) {
d8275f
 			close(sockfd);
d8275f
 			/* Assume only revision 0 support (old kernel) */
d8275f
-- 
d8275f
2.34.1
d8275f