Netfilter-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [iptables PATCH] xtables-translate: Fix for interface name corner-cases
@ 2020-02-06 14:46 Phil Sutter
  2020-02-10 11:24 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Phil Sutter @ 2020-02-06 14:46 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

There are two special situations xlate_ifname() didn't cover for:

* Interface name being '*': This went unchanged, creating a command nft
  wouldn't accept. Instead translate into '\*' which doesn't change
  semantics.

* Interface name being '+': Can't translate into nft wildcard character
  as nft doesn't accept asterisk-only interface names. Instead decide
  what to do based on 'invert' value: Skip match creation if false,
  match against an invalid interface name if true.

Also add a test to make sure future changes to this behaviour are
noticed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 extensions/generic.txlate    | 12 ++++++++++++
 iptables/xtables-translate.c | 27 +++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/extensions/generic.txlate b/extensions/generic.txlate
index b38fbd1fe113b..aabe13b169718 100644
--- a/extensions/generic.txlate
+++ b/extensions/generic.txlate
@@ -18,3 +18,15 @@ nft add rule bridge filter FORWARD iifname != "iname" meta ibrname "ilogname" oi
 
 ebtables-translate -I INPUT -p ip -d 1:2:3:4:5:6/ff:ff:ff:ff:00:00
 nft insert rule bridge filter INPUT ether type 0x800 ether daddr 01:02:03:04:00:00 and ff:ff:ff:ff:00:00 == 01:02:03:04:00:00 counter
+
+# asterisk is not special in iptables and it is even a valid interface name
+iptables-translate -A FORWARD -i '*'
+nft add rule ip filter FORWARD iifname "\*" counter
+
+# skip for always matching interface names
+iptables-translate -A FORWARD -i '+'
+nft add rule ip filter FORWARD counter
+
+# match against invalid interface name to simulate never matching rule
+iptables-translate -A FORWARD ! -i '+'
+nft add rule ip filter FORWARD iifname "INVAL/D" counter
diff --git a/iptables/xtables-translate.c b/iptables/xtables-translate.c
index 77a186b905d73..d2a62f4d53ee8 100644
--- a/iptables/xtables-translate.c
+++ b/iptables/xtables-translate.c
@@ -33,15 +33,34 @@ void xlate_ifname(struct xt_xlate *xl, const char *nftmeta, const char *ifname,
 		  bool invert)
 {
 	int ifaclen = strlen(ifname);
-	char iface[IFNAMSIZ];
+	char iface[IFNAMSIZ + 1];
 
 	if (ifaclen < 1 || ifaclen >= IFNAMSIZ)
 		return;
 
 	strcpy(iface, ifname);
-	if (iface[ifaclen - 1] == '+')
-		iface[ifaclen - 1] = '*';
-
+	switch (iface[ifaclen - 1]) {
+	case '+':
+		if (ifaclen > 1) {
+			iface[ifaclen - 1] = '*';
+			break;
+		}
+		/* Nftables does not support wildcard only string. Workaround
+		 * is easy, given that this will match always or never
+		 * depending on 'invert' value. To match always, simply don't
+		 * generate an expression. To match never, use an invalid
+		 * interface name (kernel doesn't accept '/' in names) to match
+		 * against. */
+		if (!invert)
+			return;
+		strcpy(iface, "INVAL/D");
+		invert = false;
+		break;
+	case '*':
+		iface[ifaclen - 1] = '\\';
+		strcat(iface, "*");
+		break;
+	}
 	xt_xlate_add(xl, "%s %s\"%s\" ", nftmeta, invert ? "!= " : "", iface);
 }
 
-- 
2.24.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [iptables PATCH] xtables-translate: Fix for interface name corner-cases
  2020-02-06 14:46 [iptables PATCH] xtables-translate: Fix for interface name corner-cases Phil Sutter
@ 2020-02-10 11:24 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2020-02-10 11:24 UTC (permalink / raw)
  To: Phil Sutter; +Cc: netfilter-devel

On Thu, Feb 06, 2020 at 03:46:25PM +0100, Phil Sutter wrote:
> There are two special situations xlate_ifname() didn't cover for:
> 
> * Interface name being '*': This went unchanged, creating a command nft
>   wouldn't accept. Instead translate into '\*' which doesn't change
>   semantics.
> 
> * Interface name being '+': Can't translate into nft wildcard character
>   as nft doesn't accept asterisk-only interface names. Instead decide
>   what to do based on 'invert' value: Skip match creation if false,
>   match against an invalid interface name if true.
> 
> Also add a test to make sure future changes to this behaviour are
> noticed.

This also looks good to me. Thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-06 14:46 [iptables PATCH] xtables-translate: Fix for interface name corner-cases Phil Sutter
2020-02-10 11:24 ` Pablo Neira Ayuso

Netfilter-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netfilter-devel/0 netfilter-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netfilter-devel netfilter-devel/ https://lore.kernel.org/netfilter-devel \
		netfilter-devel@vger.kernel.org
	public-inbox-index netfilter-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netfilter-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git