From: Pablo Neira Ayuso Subject: Re: Problem with iptables-translate and tcp flags match Date: Mon, 28 Nov 2016 12:35:37 +0100 Message-ID: <20161128113537.GB1691@salvia> References: <20161125215017.GK6158@orbyte.nwl.cc> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii To: Phil Sutter , netfilter-devel@vger.kernel.org Return-path: Received: from mail.us.es ([193.147.175.20]:58098 "EHLO mail.us.es" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753751AbcK1Lfn (ORCPT ); Mon, 28 Nov 2016 06:35:43 -0500 Received: from antivirus1-rhel7.int (unknown [192.168.2.11]) by mail.us.es (Postfix) with ESMTP id 7339113A365 for ; Mon, 28 Nov 2016 12:35:40 +0100 (CET) Received: from antivirus1-rhel7.int (localhost [127.0.0.1]) by antivirus1-rhel7.int (Postfix) with ESMTP id 6217915D632 for ; Mon, 28 Nov 2016 12:35:40 +0100 (CET) Received: from antivirus1-rhel7.int (localhost [127.0.0.1]) by antivirus1-rhel7.int (Postfix) with ESMTP id 598E315D648 for ; Mon, 28 Nov 2016 12:35:38 +0100 (CET) Content-Disposition: inline In-Reply-To: <20161125215017.GK6158@orbyte.nwl.cc> Sender: netfilter-devel-owner@vger.kernel.org List-ID: Hi, On Fri, Nov 25, 2016 at 10:50:17PM +0100, Phil Sutter wrote: > Hi, > > I have a problem which exposes bugs in both iptables-translate and nft > and am a bit at a loss with it. But first things first: > > | $ iptables-translate -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -j DROP > | nft add rule ip filter INPUT tcp flags & fin|syn == fin|syn counter drop > > This only appears to be fine at first glance. When adding the nftables > rule, the outcome is this: > > | tcp flags & (fin | syn) | fin == fin | syn counter packets 0 bytes 0 > > Of course, this is obviously wrong. Looking at the output of > iptables-translate again, it becomes clear that nftables can only > interpret this the wrong way, categorically because binary AND takes > precedence over binary OR - so iptables-translate is broken in that it > should add parentheses like so: > > | nft add rule ip filter INPUT tcp flags & (fin|syn) == fin|syn counter drop > > And indeed this leads to expected results in nftables ruleset. On the > other hand what nftables interprets the wrong statement into looks a bit > fishy as well, but that's another topic - my issue with nftables is > exposed when I apply my quick hack to resolve this, which is to just > output the missing parentheses print_tcp_xlate(): iptables-translate > then outputs the following: > > | nft add rule ip filter INPUT tcp flags & (fin|syn) == (fin|syn) counter drop > > But nftables in return rejects it: > > | :1:45-45: Error: syntax error, unexpected '(' > | add rule ip filter INPUT tcp flags & (syn|fin) == (syn|fin) counter > | ^ > > The error message is emitted by the parser (which I am still at war > with) and in my opinion it should allow the parentheses on the right > side of that relational expression. > > So here's a number of question marks for you: > * Am I on the right track with adding parentheses to iptables-translate > output at all? > * If so, where would I adjust parser_bison.y to allow for them? Maybe > rhs_expr should be extended to allow for it, but it's so generic I'm > afraid of side-effects. Or is the better way to add another case to > relational_expr which explicitly mentions the parentheses around > rhs_expr? There used to be parens there indeed, but this got removed when rhs_expr was introduced. If you look at old nftables releases you should find it there (well, not exactly in rhs_expr since that was added recently). So my first suggestion is to restore this in the way this was before.