netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [iptables PATCH 2/2] Improve error messages for unsupported extensions
Date: Fri, 11 Feb 2022 18:12:11 +0100	[thread overview]
Message-ID: <20220211171211.26484-3-phil@nwl.cc> (raw)
In-Reply-To: <20220211171211.26484-1-phil@nwl.cc>

If a given extension was not supported by the kernel, iptables would
print a rather confusing error message if extension parameters were
given:

| # rm /lib/modules/$(uname -r)/kernel/net/netfilter/xt_LOG.ko
| # iptables -A FORWARD -j LOG --log-prefix foo
| iptables v1.8.7 (legacy): unknown option "--log-prefix"

Avoid this by pretending extension revision 0 is always supported. It is
the same hack as used to successfully print extension help texts as
unprivileged user, extended to all error codes to serve privileged ones
as well.

In addition, print a warning if kernel rejected revision 0 and it's not
a permissions problem. This helps users find out which extension in a
rule the kernel didn't like.

Finally, the above commands result in these messages:

| Warning: Extension LOG revision 0 not supported, missing kernel module?
| iptables: No chain/target/match by that name.

Or, for iptables-nft:

| Warning: Extension LOG revision 0 not supported, missing kernel module?
| iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain FORWARD

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 iptables/nft.c       | 12 +++++++++---
 libxtables/xtables.c |  7 ++++++-
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/iptables/nft.c b/iptables/nft.c
index 041e1b8ccd3e5..d011d7c88da12 100644
--- a/iptables/nft.c
+++ b/iptables/nft.c
@@ -3514,10 +3514,16 @@ int nft_compatible_revision(const char *name, uint8_t rev, int opt)
 err:
 	mnl_socket_close(nl);
 
-	/* pretend revision 0 is valid if not permitted to check -
-	 * this is required for printing extension help texts as user */
-	if (ret < 0 && errno == EPERM && rev == 0)
+	/* pretend revision 0 is valid -
+	 * this is required for printing extension help texts as user, also
+	 * helps error messaging on unavailable kernel extension */
+	if (ret < 0 && rev == 0) {
+		if (errno != EPERM)
+			fprintf(stderr,
+				"Warning: Extension %s revision 0 not supported, missing kernel module?\n",
+				name);
 		return 1;
+	}
 
 	return ret < 0 ? 0 : 1;
 }
diff --git a/libxtables/xtables.c b/libxtables/xtables.c
index b34d62acf6015..87424d045466b 100644
--- a/libxtables/xtables.c
+++ b/libxtables/xtables.c
@@ -958,7 +958,12 @@ int xtables_compatible_revision(const char *name, uint8_t revision, int opt)
 		/* Definitely don't support this? */
 		if (errno == ENOENT || errno == EPROTONOSUPPORT) {
 			close(sockfd);
-			return 0;
+			/* Pretend revision 0 support for better error messaging */
+			if (revision == 0)
+				fprintf(stderr,
+					"Warning: Extension %s revision 0 not supported, missing kernel module?\n",
+					name);
+			return (revision == 0);
 		} else if (errno == ENOPROTOOPT) {
 			close(sockfd);
 			/* Assume only revision 0 support (old kernel) */
-- 
2.34.1


  parent reply	other threads:[~2022-02-11 17:12 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-11 17:12 [iptables PATCH 0/2] Improve error messages for unsupported extensions Phil Sutter
2022-02-11 17:12 ` [iptables PATCH 1/2] libxtables: Register only the highest revision extension Phil Sutter
2022-02-11 17:12 ` Phil Sutter [this message]
2022-03-02 14:52 ` [iptables PATCH 0/2] Improve error messages for unsupported extensions Phil Sutter
2022-03-02 15:06   ` Florian Westphal
2022-03-02 20:17     ` Phil Sutter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220211171211.26484-3-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).