Skip to contentSkip to conversation

iptables: fix the wrong appending of jump verdict after the comment.

PATCH In review Shyam Saini 3 participants 4 messages Jan 26, 2017 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iptables: fix the wrong appending of jump verdict after the comment.

Fix wrong appending of jump verdict after the comment

For example:

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s  192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT

Note that even without comment with double-quotes (i.e. --comment "foobar"), it will add quotes:

$ iptables-translate -A FORWARD -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j DROP -m comment --comment singlecomment
nft add rule ip filter FORWARD ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"singlecomment\"drop

Attempting to apply the translated/generated rule will result to:

$ nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr  192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT
<cmdline>:1:111-114: Error: syntax error, unexpected jump, expecting endof file or newline or semicolon
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment "foobar"jump LONGNACCEPT

After this patch

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment \"foobar\"
which is correct translation
---
 iptables/nft-ipv4.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/nft-ipv4.c +2 -3
index 52b1bed..e5947a7 100644
@@ -489,12 +489,11 @@ static int nft_ipv4_xlate(const void *data, struct xt_xlate *xl)
 
 	/* Always add counters per rule, as in iptables */
 	xt_xlate_add(xl, "counter ");
+	ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
 
 	comment = xt_xlate_get_comment(xl);
 	if (comment)
-		xt_xlate_add(xl, "comment %s", comment);
-
-	ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
+		xt_xlate_add(xl, " comment %s", comment);
 
 	return ret;
 }
Signature · 1 line
2.7.4
#1 lore raw

Hi!

Quoted 14 lines · Shyam Saini

Fix wrong appending of jump verdict after the comment

For example:

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s  192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT

Note that even without comment with double-quotes (i.e. --comment "foobar"), it will add quotes:

$ iptables-translate -A FORWARD -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j DROP -m comment --comment singlecomment
nft add rule ip filter FORWARD ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"singlecomment\"drop

Attempting to apply the translated/generated rule will result to:

Quoted 9 lines
$ nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr  192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT
<cmdline>:1:111-114: Error: syntax error, unexpected jump, expecting endof file or newline or semicolon
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment "foobar"jump LONGNACCEPT

After this patch

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment \"foobar\"
which is correct translation

I get the following:

sudo nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr
192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment
\"foobar\"
<cmdline>:1:1-127: Error: Could not process rule: Operation not supported
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr
192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment "foobar" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Shivani

Quoted 31 lines
---
 iptables/nft-ipv4.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/nft-ipv4.c +2 -4
index 52b1bed..e5947a7 100644
@@ -489,12 +489,11 @@ static int nft_ipv4_xlate(const void *data, struct xt_xlate *xl)
 
        /* Always add counters per rule, as in iptables */
        xt_xlate_add(xl, "counter ");
+       ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
 
        comment = xt_xlate_get_comment(xl);
        if (comment)
-               xt_xlate_add(xl, "comment %s", comment);
-
-       ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
+               xt_xlate_add(xl, " comment %s", comment);
 
        return ret;
 }
--

2.7.4

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Reviewed-by on v1 1/1
#2 lore raw
In reply to Shyam Saini
Quoted 18 lines · Shyam Saini

Fix wrong appending of jump verdict after the comment

For example:

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s  192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT

Note that even without comment with double-quotes (i.e. --comment "foobar"), it will add quotes:

$ iptables-translate -A FORWARD -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j DROP -m comment --comment singlecomment
nft add rule ip filter FORWARD ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"singlecomment\"drop

Attempting to apply the translated/generated rule will result to:

$ nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr  192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT
<cmdline>:1:111-114: Error: syntax error, unexpected jump, expecting endof file or newline or semicolon
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment "foobar"jump LONGNACCEPT
Quoted 6 lines

After this patch

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment \"foobar\"
which is correct translation

It does get accepted by nft. Sorry about the last mail. You could probably send out similar patch for ip6 too.

Shivani

Quoted 30 lines
---
 iptables/nft-ipv4.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/nft-ipv4.c +2 -4
index 52b1bed..e5947a7 100644
@@ -489,12 +489,11 @@ static int nft_ipv4_xlate(const void *data, struct xt_xlate *xl)
 
        /* Always add counters per rule, as in iptables */
        xt_xlate_add(xl, "counter ");
+       ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
 
        comment = xt_xlate_get_comment(xl);
        if (comment)
-               xt_xlate_add(xl, "comment %s", comment);
-
-       ret = xlate_action(cs, !!(cs->fw.ip.flags & IPT_F_GOTO), xl);
+               xt_xlate_add(xl, " comment %s", comment);
 
        return ret;
 }
--

2.7.4

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 24 lines · Shivani Bhardwaj
Quoted 23 lines · Shyam Saini

Fix wrong appending of jump verdict after the comment

For example:

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s  192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT

Note that even without comment with double-quotes (i.e. --comment "foobar"), it will add quotes:

$ iptables-translate -A FORWARD -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j DROP -m comment --comment singlecomment
nft add rule ip filter FORWARD ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment \"singlecomment\"drop

Attempting to apply the translated/generated rule will result to:

$ nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr  192.168.0.0/16 tcp sport 80 counter comment \"foobar\"jump LONGNACCEPT
<cmdline>:1:111-114: Error: syntax error, unexpected jump, expecting endof file or newline or semicolon
add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter comment "foobar"jump LONGNACCEPT

After this patch

$ iptables-translate -A INPUT -p tcp -m tcp --sport http -s 192.168.0.0/16 -d 192.168.0.0/16 -j LONGNACCEPT -m comment --comment "foobar"
nft add rule ip filter INPUT ip saddr 192.168.0.0/16 ip daddr 192.168.0.0/16 tcp sport 80 counter jump LONGNACCEPT comment \"foobar\"
which is correct translation
Quoted 6 lines
Quoted 1 line

It does get accepted by nft. Sorry about the last mail. You could probably send out similar patch for ip6 too.

I have mangled your patch to include the missing IPv6 chunk.

So this patch has been applied, thanks!