]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
5 years agosrc: NAT support for intervals in maps
Pablo Neira Ayuso [Fri, 24 Apr 2020 19:56:46 +0000 (21:56 +0200)] 
src: NAT support for intervals in maps

This patch allows you to specify an interval of IP address in maps.

 table ip x {
        chain y {
                type nat hook postrouting priority srcnat; policy accept;
                snat ip interval to ip saddr map { 10.141.11.4 : 192.168.2.2-192.168.2.4 }
        }
 }

The example above performs SNAT to packets that comes from 10.141.11.4
to an interval of IP addresses from 192.168.2.2 to 192.168.2.4 (both
included).

You can also combine this with dynamic maps:

 table ip x {
        map y {
                type ipv4_addr : interval ipv4_addr
                flags interval
                elements = { 10.141.10.0/24 : 192.168.2.2-192.168.2.4 }
        }

        chain y {
                type nat hook postrouting priority srcnat; policy accept;
                snat ip interval to ip saddr map @y
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: Allow building from tarballs without yacc/lex
Matt Turner [Tue, 7 Apr 2020 20:23:37 +0000 (13:23 -0700)] 
build: Allow building from tarballs without yacc/lex

The generated files are included in the tarballs already, but
configure.ac was coded to fail if yacc/lex were not found regardless.

Signed-off-by: Matt Turner <mattst88@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: Include generated man pages in dist tarball
Matt Turner [Tue, 7 Apr 2020 19:05:08 +0000 (12:05 -0700)] 
build: Include generated man pages in dist tarball

Most projects ship pre-generated man pages in the distribution tarball
so that builders don't need the documentation tools installed, similar
to how bison-generated sources are included.

To do this, we conditionalize the presence check of a2x on whether nft.8
already exists in the source directory, as it would exist if included in
the distribution tarball.

Secondly, we move the 'if BUILD_MAN' conditional to around the man page
generation rules. This ensures that the man pages are unconditionally
installed. Also only add the man pages to CLEANFILES if their generation
is enabled.

Signed-off-by: Matt Turner <mattst88@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: proper ct timeout list initialization
Pablo Neira Ayuso [Wed, 15 Apr 2020 21:58:57 +0000 (23:58 +0200)] 
parser_bison: proper ct timeout list initialization

Initialize list of timeout policies from ct_timeout_block.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1403
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: Set NFT_SET_CONCAT flag for sets with concatenated ranges
Stefano Brivio [Mon, 13 Apr 2020 19:48:03 +0000 (21:48 +0200)] 
src: Set NFT_SET_CONCAT flag for sets with concatenated ranges

Pablo reports that nft, after commit 8ac2f3b2fca3 ("src: Add support
for concatenated set ranges"), crashes with older kernels (< 5.6)
without support for concatenated set ranges: those sets will be sent
to the kernel, which adds them without notion of the fact that
different concatenated fields are actually included, and nft crashes
while trying to list this kind of malformed concatenation.

Use the NFT_SET_CONCAT flag introduced by kernel commit ef516e8625dd
("netfilter: nf_tables: reintroduce the NFT_SET_CONCAT flag") when
sets including concatenated ranges are sent to the kernel, so that
older kernels (with no knowledge of this flag itself) will refuse set
creation.

Note that, in expr_evaluate_set(), we have to check for the presence
of the flag, also on empty sets that might carry it in context data,
and actually set it in the actual set flags.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoinclude: Resync nf_tables.h cache copy
Stefano Brivio [Mon, 13 Apr 2020 19:48:02 +0000 (21:48 +0200)] 
include: Resync nf_tables.h cache copy

Get this header in sync with nf.git as of commit ef516e8625dd.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: broken error reporting with mappings
Pablo Neira Ayuso [Sat, 11 Apr 2020 12:19:36 +0000 (14:19 +0200)] 
segtree: broken error reporting with mappings

Segfault on error reporting when intervals overlap.

ip saddr vmap {
    10.0.1.0-10.0.1.255 : accept,
    10.0.1.1-10.0.2.255 : drop
}

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1415
Fixes: 4d6ad0f310d6 ("segtree: check for overlapping elements at insertion")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodoc: add hashing expressions description
Laura Garcia Liebana [Wed, 1 Apr 2020 15:48:13 +0000 (17:48 +0200)] 
doc: add hashing expressions description

The hashing expressions jhash and symhash are missing in the
nft manual.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoconcat: provide proper dtype when parsing typeof udata
Florian Westphal [Wed, 1 Apr 2020 14:02:16 +0000 (16:02 +0200)] 
concat: provide proper dtype when parsing typeof udata

Pablo reports following list bug:
table ip foo {
        map whitelist {
                typeof ip saddr . ip daddr : meta mark
                elements = { 0x0 [invalid type] . 0x0 [invalid type] : 0x00000001,
                             0x0 [invalid type] . 0x0 [invalid type] : 0x00000002 }
        }
}

Problem is that concat provided 'invalid' dtype.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agobuild: Bump version to v0.9.4 v0.9.4
Pablo Neira Ayuso [Tue, 31 Mar 2020 21:10:41 +0000 (23:10 +0200)] 
build: Bump version to v0.9.4

Update release name based on Jazz series,
Jo Jones Trio's "Jive at Five":

https://www.youtube.com/watch?v=phFyIKf2h4s&list=PL_i-72Hx6rt7eQ6D_lxoKEUx5Gk7SRfX3&index=13&t=0s

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink: Show the handles of unknown rules in "nft monitor trace"
Luis Ressel [Thu, 26 Mar 2020 15:22:29 +0000 (15:22 +0000)] 
netlink: Show the handles of unknown rules in "nft monitor trace"

When "nft monitor trace" doesn't know a rule (because it was only added
to the ruleset after nft was invoked), that rule is silently omitted in
the trace output, which can come as a surprise when debugging issues.

Instead, we can at least show the information we got via netlink, i.e.
the family, table and chain name, rule handle and verdict.

Signed-off-by: Luis Ressel <aranea@aixah.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: swap json and gmp fields in nft -V
Pablo Neira Ayuso [Tue, 31 Mar 2020 23:20:53 +0000 (01:20 +0200)] 
main: swap json and gmp fields in nft -V

 # ./configure --with-xtable --with-json
 ...
 # make
 ...
 # make install
 ...
 # nft -V
 nftables v0.9.3 (Topsy)
   cli:          readline
   json:         no
   minigmp:      yes
   libxtables:   yes

json: and minigmp: are accidentally swapped.

I introduced this bug while mangling Jeremy's original patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: simplify error in chain type and hook
Pablo Neira Ayuso [Tue, 31 Mar 2020 18:27:52 +0000 (20:27 +0200)] 
parser_bison: simplify error in chain type and hook

Remove extra string after error, location is sufficient.

 # nft -f x
 /tmp/x:3:8-11: Error: unknown chain type
                type nput hook input device eth0 priority 0
                     ^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: check for device in non-netdev chains
Pablo Neira Ayuso [Tue, 31 Mar 2020 17:25:57 +0000 (19:25 +0200)] 
evaluate: check for device in non-netdev chains

 # nft -f /tmp/x
 /tmp/x:3:26-36: Error: This chain type cannot be bound to device
                 type filter hook input device eth0 priority 0
                                        ^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: improve error reporting in netdev ingress chain
Pablo Neira Ayuso [Tue, 31 Mar 2020 11:30:31 +0000 (13:30 +0200)] 
evaluate: improve error reporting in netdev ingress chain

 # nft -f /tmp/x.nft
 /tmp/x.nft:3:20-24: Error: The netdev family does not support this hook
                 type filter hook input device eth0 priority 0
                                  ^^^^^

 # nft -f /tmp/x.nft
 /tmp/x.nft:3:3-49: Error: Missing `device' in this chain definition
                 type filter hook ingress device eth0 priority 0
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: store location of basechain definition
Pablo Neira Ayuso [Tue, 31 Mar 2020 11:53:39 +0000 (13:53 +0200)] 
parser_bison: store location of basechain definition

Wrap basechain definition field around structure, add field later.
This is useful for error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agorule: add hook_spec
Pablo Neira Ayuso [Tue, 31 Mar 2020 11:36:16 +0000 (13:36 +0200)] 
rule: add hook_spec

Store location of chain hook definition.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: display error if set statement is missing
Pablo Neira Ayuso [Fri, 27 Mar 2020 18:26:49 +0000 (19:26 +0100)] 
evaluate: display error if set statement is missing

 # cat /tmp/x
 table x {
        set y {
                type ipv4_addr
                elements = {
                        1.1.1.1 counter packets 1 bytes 67,
                }
        }
 }
 # nft -f /tmp/x
 /tmp/x:5:12-18: Error: missing counter statement in set definition
                        1.1.1.1 counter packets 1 bytes 67,
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^

Instead, this should be:

 table x {
        set y {
                type ipv4_addr
counter               <-------
                elements = {
                        1.1.1.1 counter packets 1 bytes 67,
                }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add support for flowtable counter
Pablo Neira Ayuso [Tue, 24 Mar 2020 17:38:51 +0000 (18:38 +0100)] 
src: add support for flowtable counter

Allow users to enable flow counters via control plane toggle, e.g.

 table ip x {
flowtable y {
hook ingress priority 0;
counter;
}

chain z {
type filter hook ingress priority filter;
flow add @z
}
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoinclude: resync nf_tables.h cache copy
Pablo Neira Ayuso [Tue, 24 Mar 2020 17:38:01 +0000 (18:38 +0100)] 
include: resync nf_tables.h cache copy

Fetch recent updates to the kernel header.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: Introduce test for insertion of overlapping and non-overlapping ranges
Stefano Brivio [Thu, 5 Mar 2020 20:34:11 +0000 (21:34 +0100)] 
tests: shell: Introduce test for insertion of overlapping and non-overlapping ranges

Insertion of overlapping ranges should return success only if the new
elements are identical to existing ones, or, for concatenated ranges,
if the new element is less specific (in all its fields) than any
existing one.

Note that, in case the range is identical to an existing one, insertion
won't actually be performed, but no error will be returned either on
'add element'.

This was inspired by a failing case reported by Phil Sutter (where
concatenated overlapping ranges would fail insertion silently) and is
fixed by kernel series with subject:
nftables: Consistently report partial and entire set overlaps

With that series, these tests now pass also if the call to set_overlap()
on insertion is skipped. Partial or entire overlapping was already
detected by the kernel for concatenated ranges (nft_set_pipapo) from
the beginning, and that series makes the nft_set_rbtree implementation
consistent in terms of detection and reporting. Without that, overlap
checks are performed by nft but not guaranteed by the kernel.

However, we can't just drop set_overlap() now, as we need to preserve
compatibility with older kernels.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: update nat expressions payload to include proto flags
Pablo Neira Ayuso [Fri, 20 Mar 2020 12:47:40 +0000 (13:47 +0100)] 
tests: py: update nat expressions payload to include proto flags

Update tests according to 6c84577b0d23 ("evaluate: add range specified
flag setting (missing NF_NAT_RANGE_PROTO_SPECIFIED)")

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: support for counter in set definition
Pablo Neira Ayuso [Tue, 17 Mar 2020 13:50:38 +0000 (14:50 +0100)] 
src: support for counter in set definition

This patch allows you to turn on counter for each element in the set.

 table ip x {
set y {
typeof ip saddr
counter
elements = { 192.168.10.35, 192.168.10.101, 192.168.10.135 }
}

chain z {
type filter hook output priority filter; policy accept;
ip daddr @y
}
 }

This example shows how to turn on counters globally in the set 'y'.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: add range specified flag setting (missing NF_NAT_RANGE_PROTO_SPECIFIED)
Pablo Neira Ayuso [Wed, 18 Mar 2020 18:51:20 +0000 (19:51 +0100)] 
evaluate: add range specified flag setting (missing NF_NAT_RANGE_PROTO_SPECIFIED)

Sergey reports:

With nf_tables it is not possible to use port range for masquerading.
Masquerade statement has option "to [:port-port]" which give no effect
to translation behavior. But it must change source port of packet to
one from ":port-port" range.

My network:

        +-----------------------------+
        |   ROUTER                    |
        |                             |
        |                   Masquerade|
        | 10.0.0.1            1.1.1.1 |
        | +------+           +------+ |
        | | eth1 |           | eth2 | |
        +-+--^---+-----------+---^--+-+
             |                   |
             |                   |
        +----v------+     +------v----+
        |           |     |           |
        | 10.0.0.2  |     |  1.1.1.2  |
        |           |     |           |
        |PC1        |     |PC2        |
        +-----------+     +-----------+

For testing i used rule like this:

        rule ip nat POSTROUTING oifname eth2 masquerade to :666

Run netcat for 1.1.1.2 667(UDP) and get dump from PC2:

        15:22:25.591567 a8:f9:4b:aa:08:44 > a8:f9:4b:ac:e7:8f, ethertype IPv4 (0x0800), length 60: 1.1.1.1.34466 > 1.1.1.2.667: UDP, length 1

Address translation works fine, but source port are not belongs to
specified range.

I see in similar source code (i.e. nft_redir.c, nft_nat.c) that
there is setting NF_NAT_RANGE_PROTO_SPECIFIED flag. After adding this,
repeat test for kernel with this patch, and get dump:

        16:16:22.324710 a8:f9:4b:aa:08:44 > a8:f9:4b:ac:e7:8f, ethertype IPv4 (0x0800), length 60: 1.1.1.1.666 > 1.1.1.2.667: UDP, length 1

Now it is works fine.

Reported-by: Sergey Marinkevich <s@marinkevich.ru>
Tested-by: Sergey Marinkevich <s@marinkevich.ru>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: support for restoring element counters
Pablo Neira Ayuso [Wed, 11 Mar 2020 12:00:01 +0000 (13:00 +0100)] 
src: support for restoring element counters

This patch allows you to restore counters in dynamic sets:

 table ip test {
        set test {
                type ipv4_addr
                size 65535
                flags dynamic,timeout
                timeout 30d
                gc-interval 1d
                elements = { 192.168.10.13 expires 19d23h52m27s576ms counter packets 51 bytes 17265 }
        }
        chain output {
                type filter hook output priority 0;
                update @test { ip saddr }
        }
 }

You can also add counters to elements from the control place, ie.

 table ip test {
        set test {
                type ipv4_addr
                size 65535
                elements = { 192.168.2.1 counter packets 75 bytes 19043 }
        }

        chain output {
                type filter hook output priority filter; policy accept;
                ip daddr @test
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink: remove unused parameter from netlink_gen_stmt_stateful()
Pablo Neira Ayuso [Wed, 11 Mar 2020 12:02:26 +0000 (13:02 +0100)] 
netlink: remove unused parameter from netlink_gen_stmt_stateful()

Remove context from netlink_gen_stmt_stateful().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests/py: Move tcpopt.t to any/ directory
Phil Sutter [Tue, 10 Mar 2020 11:56:18 +0000 (12:56 +0100)] 
tests/py: Move tcpopt.t to any/ directory

Merge tcpopt.t files in ip, ip6 and inet into a common one, they were
just marignally different.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests/py: Add tests involving concatenated ranges
Phil Sutter [Sat, 7 Mar 2020 02:00:10 +0000 (03:00 +0100)] 
tests/py: Add tests involving concatenated ranges

Very basic testing, just a set definition, a rule which references it
and another one with an anonymous set.

Sadly this is already enough to expose some pending issues:

* Payload dependency killing ignores the concatenated IP header
  expressions on LHS, so rule output is asymmetric.

* Anonymous sets don't accept concatenated ranges yet, so the second
  rule is manually disabled for now.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agoparser_json: Support ranges in concat expressions
Phil Sutter [Fri, 6 Mar 2020 15:15:48 +0000 (16:15 +0100)] 
parser_json: Support ranges in concat expressions

Duplicate commit 8ac2f3b2fca38's changes to bison parser into JSON
parser by introducing a new context flag signalling we're parsing
concatenated expressions.

Fixes: 8ac2f3b2fca38 ("src: Add support for concatenated set ranges")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Eric Garver <eric@garver.life>
5 years agotests/py: Fix JSON output for changed timezone
Phil Sutter [Sat, 7 Mar 2020 01:40:56 +0000 (02:40 +0100)] 
tests/py: Fix JSON output for changed timezone

When setting a fixed timezone, JSON expected output for one (known)
asymmetric rule was left out by accident.

Fixes: 7e326d697ecf4 ("tests/py: Set a fixed timezone in nft-test.py")
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agomain: use one data-structure to initialize getopt_long(3) arguments and help.
Jeremy Sowden [Thu, 5 Mar 2020 14:48:05 +0000 (14:48 +0000)] 
main: use one data-structure to initialize getopt_long(3) arguments and help.

By generating the getopt_long(3) optstring and options, and the help
from one source, we reduce the chance that they may get out of sync.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: interpolate default include path into help format-string.
Jeremy Sowden [Thu, 5 Mar 2020 14:48:04 +0000 (14:48 +0000)] 
main: interpolate default include path into help format-string.

The default include path is a string literal defined as a preprocessor
macro by autoconf.  We can just interpolate it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: include '--reversedns' in help.
Jeremy Sowden [Thu, 5 Mar 2020 14:48:03 +0000 (14:48 +0000)] 
main: include '--reversedns' in help.

The long option for '-N' was omitted from the help.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: include '-d' in help.
Jeremy Sowden [Thu, 5 Mar 2020 14:48:02 +0000 (14:48 +0000)] 
main: include '-d' in help.

The short option for '--debug' was omitted from the help.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: fix rshift statement expression.
Jeremy Sowden [Mon, 9 Mar 2020 11:07:47 +0000 (11:07 +0000)] 
parser_bison: fix rshift statement expression.

The RHS of RSHIFT statement expressions should be primary_stmt_expr, not
primary_rhs_expr.

Fixes: dccab4f646b4 ("parser_bison: consolidate stmt_expr rule")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: remove duplicates from option string.
Jeremy Sowden [Wed, 4 Mar 2020 08:57:35 +0000 (08:57 +0000)] 
main: remove duplicates from option string.

The string of options passed to getopt_long(3) contains duplicates.
Update it to match the opt_vals enum which immediately precedes it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: add more information to `nft -V`.
Jeremy Sowden [Tue, 3 Mar 2020 23:15:30 +0000 (00:15 +0100)] 
main: add more information to `nft -V`.

In addition to the package-version and release-name, output the CLI
implementation (if any) and whether mini-gmp was used, e.g.:

    $ ./src/nft -V
    nftables v0.9.3 (Topsy)
      cli:          linenoise
      json:         yes
      minigmp:      no
      libxtables:   yes

[pablo@netfilter.org: add json and libxtables, use -V ]

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: fix leaks.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:33 +0000 (09:48 +0000)] 
src: fix leaks.

Some bitmask variables are not cleared.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agonetlink_delinearize: set shift RHS byte-order.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:32 +0000 (09:48 +0000)] 
netlink_delinearize: set shift RHS byte-order.

The RHS operand for bitwise shift is in HBO.  Set this explicitly.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: no need to swap byte-order for values of fewer than 16 bits.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:31 +0000 (09:48 +0000)] 
evaluate: no need to swap byte-order for values of fewer than 16 bits.

Endianness is not meaningful for objects smaller than 2 bytes and the
byte-order conversions are no-ops in the kernel, so just update the
expression as if it were constant.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: convert the byte-order of payload statement arguments.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:30 +0000 (09:48 +0000)] 
evaluate: convert the byte-order of payload statement arguments.

Since shift operations require host byte-order, we need to be able to
convert the result of the shift back to network byte-order, in a rule
like:

  nft add rule ip t c tcp dport set tcp dport lshift 1

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: don't evaluate payloads twice.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:29 +0000 (09:48 +0000)] 
evaluate: don't evaluate payloads twice.

Payload munging means that evaluation of payload expressions may not be
idempotent.  Add a flag to prevent them from being evaluated more than
once.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: simplify calculation of payload size.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:28 +0000 (09:48 +0000)] 
evaluate: simplify calculation of payload size.

Use div_round_up and one statement.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: add separate variables for lshift and xor binops.
Jeremy Sowden [Tue, 3 Mar 2020 09:48:27 +0000 (09:48 +0000)] 
evaluate: add separate variables for lshift and xor binops.

stmt_evaluate_payload has distinct variables for some, but not all, the
binop expressions it creates.  Add variables for the rest.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agosrc: support for offload chain flag
Pablo Neira Ayuso [Tue, 3 Mar 2020 12:14:59 +0000 (13:14 +0100)] 
src: support for offload chain flag

This patch extends the basechain definition to allow users to specify
the offload flag. This flag enables hardware offload if your drivers
supports it.

 # cat file.nft
 table netdev x {
    chain y {
       type filter hook ingress device eth0 priority 10; flags offload;
    }
 }
 # nft -f file.nft

Note: You have to enable offload via ethtool:

 # ethtool -K eth0 hw-tc-offload on

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: update nat_addr_port with typeof+concat maps
Florian Westphal [Wed, 26 Feb 2020 12:15:54 +0000 (13:15 +0100)] 
tests: update nat_addr_port with typeof+concat maps

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoexpressions: concat: add typeof support
Florian Westphal [Sat, 22 Feb 2020 21:02:49 +0000 (22:02 +0100)] 
expressions: concat: add typeof support

Previous patches allow to pass concatenations as the mapped-to
data type.

This doesn't work with typeof() because the concat expression has
no support to store the typeof data in the kernel, leading to:

map t2 {
    typeof numgen inc mod 2 : ip daddr . tcp dport

being shown as
     type 0 : ipv4_addr . inet_service

... which can't be parsed back by nft.

This allows the concat expression to store the sub-expressions
in set of nested attributes.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: stmt_evaluate_nat_map() only if stmt->nat.ipportmap == true
Pablo Neira Ayuso [Tue, 25 Feb 2020 09:28:13 +0000 (10:28 +0100)] 
evaluate: stmt_evaluate_nat_map() only if stmt->nat.ipportmap == true

stmt_evaluate_nat_map() is only called when the parser sets on
stmt->nat.ipportmap.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: adjust tests to new nat concatenation syntax
Pablo Neira Ayuso [Mon, 24 Feb 2020 12:52:50 +0000 (13:52 +0100)] 
tests: shell: adjust tests to new nat concatenation syntax

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: nat concatenation support with anonymous maps
Pablo Neira Ayuso [Mon, 24 Feb 2020 12:28:34 +0000 (13:28 +0100)] 
src: nat concatenation support with anonymous maps

This patch extends the parser to define the mapping datatypes, eg.

  ... dnat ip addr . port to ip saddr map { 1.1.1.1 : 2.2.2.2 . 30 }
  ... dnat ip addr . port to ip saddr map @y

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: nat: add and use maps with both address and service
Florian Westphal [Mon, 24 Feb 2020 00:03:24 +0000 (01:03 +0100)] 
tests: nat: add and use maps with both address and service

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: allow nat maps containing both ip(6) address and port
Florian Westphal [Mon, 24 Feb 2020 00:03:23 +0000 (01:03 +0100)] 
src: allow nat maps containing both ip(6) address and port

nft will now be able to handle
map destinations {
type ipv4_addr . inet_service : ipv4_addr . inet_service
}

chain f {
dnat to ip daddr . tcp dport map @destinations
}

Something like this won't work though:
 meta l4proto tcp dnat ip6 to numgen inc mod 4 map { 0 : dead::f001 . 8080, ..

as we lack the type info to properly dissect "dead::f001" as an ipv6
address.

For the named map case, this info is available in the map
definition, but for the anon case we'd need to resort to guesswork.

Support is added by peeking into the map definition when evaluating
a nat statement with a map.
Right now, when a map is provided as address, we will only check that
the mapped-to data type matches the expected size (of an ipv4 or ipv6
address).

After this patch, if the mapped-to type is a concatenation, it will
take a peek at the individual concat expressions.  If its a combination
of address and service, nft will translate this so that the kernel nat
expression looks at the returned register that would store the
inet_service part of the octet soup returned from the lookup expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: add two new helpers
Florian Westphal [Mon, 24 Feb 2020 00:03:22 +0000 (01:03 +0100)] 
evaluate: add two new helpers

In order to support 'dnat to ip saddr map @foo', where @foo returns
both an address and a inet_service, we will need to peek into the map
and process the concatenations sub-expressions.

Add two helpers for this, will be used in followup patches.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink: handle concatenations on set elements mappings
Florian Westphal [Mon, 24 Feb 2020 00:03:21 +0000 (01:03 +0100)] 
netlink: handle concatenations on set elements mappings

We can already handle concatenated keys, this extends concat
coverage to the data type as well, i.e. this can be dissected:

type ipv4_addr : ipv4_addr . inet_service

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: process concat expressions when used as mapped-to expr
Florian Westphal [Mon, 24 Feb 2020 00:03:20 +0000 (01:03 +0100)] 
evaluate: process concat expressions when used as mapped-to expr

Needed to avoid triggering the 'dtype->size == 0' tests.
Evaluation will build a new concatenated type that holds the
size of the aggregate.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: add initial nat map test
Florian Westphal [Mon, 24 Feb 2020 00:03:19 +0000 (01:03 +0100)] 
tests: add initial nat map test

Will be extended to cover upcoming
'dnat to ip saddr . tcp dport map { \
  1.2.3.4 . 80 : 5.6.7.8 : 8080,
  2.2.3.4 . 80 : 7.6.7.8 : 1234,
   ...

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpression: use common code for expr_ops/expr_ops_by_type
Florian Westphal [Sat, 22 Feb 2020 21:12:02 +0000 (22:12 +0100)] 
expression: use common code for expr_ops/expr_ops_by_type

Useless duplication.  Also, this avoids bloating expr_ops_by_type()
when it needs to cope with more expressions.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agotests: shell: avoid spurious failure when running in host namespace
Florian Westphal [Sat, 22 Feb 2020 08:46:40 +0000 (09:46 +0100)] 
tests: shell: avoid spurious failure when running in host namespace

Dump validation may fail:
- tcp dport { 22, 23 } counter packets 0 bytes 0
+ tcp dport { 22, 23 } counter packets 9 bytes 3400

... which is normal on host namespace.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agotests: 0034get_element_0: do not discard stderr
Florian Westphal [Fri, 21 Feb 2020 23:02:25 +0000 (00:02 +0100)] 
tests: 0034get_element_0: do not discard stderr

run_tests.sh alreadty discards stderr by default, but will show it in
case the test script is run directly (passed as argument).

Discarding stderr also in the script prevents one from seeing
BUG() assertions and the like.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: print correct statement name on family mismatch
Florian Westphal [Thu, 20 Feb 2020 11:58:40 +0000 (12:58 +0100)] 
evaluate: print correct statement name on family mismatch

nft add rule inet filter c ip daddr 1.2.3.4 dnat ip6 to f00::1
Error: conflicting protocols specified: ip vs. unknown. You must specify ip or ip6 family in tproxy statement

Should be: ... "in nat statement".

Fixes: fbe27464dee4588d90 ("src: add nat support for the inet family")
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agomnl: do not use expr->identifier to fetch device name
Pablo Neira Ayuso [Wed, 19 Feb 2020 20:05:26 +0000 (21:05 +0100)] 
mnl: do not use expr->identifier to fetch device name

This string might not be nul-terminated, resulting in spurious errors
when adding netdev chains.

Fixes: 3fdc7541fba0 ("src: add multidevice support for netdev chain")
Fixes: 92911b362e90 ("src: add support to add flowtables")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: memleak in device parser
Pablo Neira Ayuso [Wed, 19 Feb 2020 19:56:42 +0000 (20:56 +0100)] 
parser_bison: memleak in device parser

==1135425== 9 bytes in 1 blocks are definitely lost in loss record 1 of 1
==1135425==    at 0x483577F: malloc (vg_replace_malloc.c:309)
==1135425==    by 0x4BE846A: strdup (strdup.c:42)
==1135425==    by 0x48A5EDD: xstrdup (utils.c:75)
==1135425==    by 0x48C9A20: nft_lex (scanner.l:640)
==1135425==    by 0x48BC1A4: nft_parse (parser_bison.c:5682)
==1135425==    by 0x48AC336: nft_parse_bison_buffer (libnftables.c:375)
==1135425==    by 0x48AC336: nft_run_cmd_from_buffer (libnftables.c:443)
==1135425==    by 0x10A707: main (main.c:384)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: Introduce test for set with concatenated ranges
Stefano Brivio [Fri, 14 Feb 2020 15:27:25 +0000 (16:27 +0100)] 
tests: Introduce test for set with concatenated ranges

This test checks that set elements can be added, deleted, that
addition and deletion are refused when appropriate, that entries
time out properly, and that they can be fetched by matching values
in the given ranges.

v5:
 - speed this up by performing the timeout test for one single
   permutation (Phil Sutter), by decreasing the number of
   permutations from 96 to 12 if this is invoked by run-tests.sh
   (Pablo Neira Ayuso) and by combining some commands into single
   nft calls where possible: with dash 0.5.8 on AMD Epyc 7351 the
   test now takes 1.8s instead of 82.5s
 - renumber test to 0043, 0042 was added meanwhile
v4: No changes
v3:
 - renumber test to 0042, 0041 was added meanwhile
v2:
 - actually check an IPv6 prefix, instead of specifying everything
   as explicit ranges in ELEMS_ipv6_addr
 - renumber test to 0041, 0038 already exists

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: improve error reporting when remove rules
Pablo Neira Ayuso [Wed, 19 Feb 2020 14:35:10 +0000 (15:35 +0100)] 
src: improve error reporting when remove rules

 # nft delete rule ip y z handle 7
 Error: Could not process rule: No such file or directory
 delete rule ip y z handle 7
                ^

 # nft delete rule ip x z handle 7
 Error: Could not process rule: No such file or directory
 delete rule ip x z handle 7
                  ^

 # nft delete rule ip x x handle 7
 Error: Could not process rule: No such file or directory
 delete rule ip x x handle 7
                           ^

 # nft replace rule x y handle 10 ip saddr 1.1.1.2 counter
 Error: Could not process rule: No such file or directory
 replace rule x y handle 10 ip saddr 1.1.1.2 counter
                         ^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: improve error reporting when setting policy on non-base chain
Pablo Neira Ayuso [Wed, 19 Feb 2020 13:57:26 +0000 (14:57 +0100)] 
src: improve error reporting when setting policy on non-base chain

When trying to set a policy to non-base chain:

 # nft add chain x y { policy accept\; }
 Error: Could not process rule: Operation not supported
 add chain x y { policy accept; }
                 ^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: extended error support for create command
Pablo Neira Ayuso [Wed, 19 Feb 2020 13:35:21 +0000 (14:35 +0100)] 
mnl: extended error support for create command

 # nft create table x
 Error: Could not process rule: File exists
 create table x
              ^

 # nft create chain x y
 Error: Could not process rule: File exists
 create chain x y
                ^

 # nft create set x y { typeof ip saddr\; }
 Error: Could not process rule: File exists
 create set x y { typeof ip saddr; }
              ^

 # nft create counter x y
 Error: Could not process rule: File exists
 create counter x y
                  ^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: combine extended netlink error reporting with mispelling support
Pablo Neira Ayuso [Tue, 18 Feb 2020 11:59:24 +0000 (12:59 +0100)] 
src: combine extended netlink error reporting with mispelling support

Preliminary support: only for the deletion command, e.g.

 # nft delete table twst
 Error: No such file or directory; did you mean table ‘test’ in family ip?
 delete table twst
              ^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: initial extended netlink error reporting
Pablo Neira Ayuso [Mon, 17 Feb 2020 21:38:13 +0000 (22:38 +0100)] 
src: initial extended netlink error reporting

This patch correlates the in-kernel extended netlink error offset and
the location information.

Assuming 'foo' table does not exist, then error reporting shows:

 # nft delete table foo
 Error: Could not process rule: No such file or directory
 delete table foo
              ^^^

Similarly, if table uniquely identified by handle '1234' does not exist,
then error reporting shows:

 # nft delete table handle 1234
 Error: Could not process rule: No such file or directory
 delete table handle 1234
                     ^^^^

 Assuming 'bar' chain does not exists in the kernel, while 'foo' does:

 # nft delete chain foo bar
 Error: Could not process rule: No such file or directory
 delete chain foo bar
                  ^^^

 This also gives us a hint when adding rules:

 # nft add rule ip foo bar counter
 Error: Could not process rule: No such file or directory
 add rule ip foo bar counter
             ^^^

This is based on ("src: basic support for extended netlink errors") from
Florian Westphal, posted in 2018, with no netlink offset correlation
support.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser: incorrect handle location
Pablo Neira Ayuso [Mon, 17 Feb 2020 21:37:29 +0000 (22:37 +0100)] 
parser: incorrect handle location

Handle location is not correct, this leads to misleading error
reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: use list_is_first() from scanner_pop_indesc()
Pablo Neira Ayuso [Thu, 13 Feb 2020 12:27:18 +0000 (13:27 +0100)] 
scanner: use list_is_first() from scanner_pop_indesc()

!list_empty() always stands true since the list is never empty
when calling scanner_pop_indesc().

Check for list_is_first() which actually tells us this is the
initial input file, hence, state->indesc is set to NULL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: maps: update data expression dtype based on set
Florian Westphal [Thu, 13 Feb 2020 11:45:55 +0000 (12:45 +0100)] 
src: maps: update data expression dtype based on set

What we want:
-               update @sticky-set-svc-M53CN2XYVUHRQ7UB { ip saddr : 0x00000002 }
what we got:
+               update @sticky-set-svc-M53CN2XYVUHRQ7UB { ip saddr : 0x2000000 [invalid type] }

Reported-by: Serguei Bezverkhi <sbezverk@gmail.com>
Close: https://bugzilla.netfilter.org/show_bug.cgi?id=1405
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agotests: shell: validate error reporting with include and glob
Pablo Neira Ayuso [Thu, 13 Feb 2020 11:13:11 +0000 (12:13 +0100)] 
tests: shell: validate error reporting with include and glob

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: add test for glob includes
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:20 +0000 (10:17 +0000)] 
tests: shell: add test for glob includes

Including more than MAX_INCLUDE_DEPTH file in one statement should succeed.

This reproduces bug #1243.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: remove parser_state->indesc_idx
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:28 +0000 (10:17 +0000)] 
scanner: remove parser_state->indesc_idx

Now that we have a proper stack implementation, we don't need an
additional counter for the number of buffer state pushed.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: fix indesc_list stack to be in the correct order
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:27 +0000 (10:17 +0000)] 
scanner: fix indesc_list stack to be in the correct order

This fixes the location displayed in error messages.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoInclusion depth was computed incorrectly for glob includes.
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:35 +0000 (05:17 -0500)] 
Inclusion depth was computed incorrectly for glob includes.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: remove parser_state->indescs static array
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:24 +0000 (10:17 +0000)] 
scanner: remove parser_state->indescs static array

This static array is redundant with the indesc_list structure, but
is less flexible.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: move indesc list append in scanner_push_indesc
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:22 +0000 (10:17 +0000)] 
scanner: move indesc list append in scanner_push_indesc

Having a single point makes refactoring easier.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: move the file descriptor to be in the input_descriptor structure
Laurent Fasnacht [Mon, 10 Feb 2020 10:17:21 +0000 (10:17 +0000)] 
scanner: move the file descriptor to be in the input_descriptor structure

This prevents a static allocation of file descriptors array, thus allows
more flexibility.

Signed-off-by: Laurent Fasnacht <fasnacht@protonmail.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoscanner: Extend asteriskstring definition
Phil Sutter [Thu, 6 Feb 2020 11:31:56 +0000 (12:31 +0100)] 
scanner: Extend asteriskstring definition

Accept escaped asterisks also mid-string and as only character.
Especially the latter will help when translating from iptables where
asterisk has no special meaning.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agodoc: nft.8: Mention wildcard interface matching
Phil Sutter [Thu, 6 Feb 2020 11:24:51 +0000 (12:24 +0100)] 
doc: nft.8: Mention wildcard interface matching

Special meaning of asterisk in interface names wasn't described
anywhere.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agodoc: nft.8: Describe element commands in their own section
Phil Sutter [Thu, 6 Feb 2020 16:01:23 +0000 (17:01 +0100)] 
doc: nft.8: Describe element commands in their own section

This unifies the redundant information in sets and maps sections and
also covers 'get' command.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosrc: compute mnemonic port name much easier
Jan Engelhardt [Fri, 7 Feb 2020 11:43:21 +0000 (12:43 +0100)] 
src: compute mnemonic port name much easier

Signed-off-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: add bit-shift tests.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:23 +0000 (11:20 +0000)] 
tests: py: add bit-shift tests.

Add a couple of Python test-cases for setting the CT mark to a bitwise
expression derived from the packet mark and vice versa.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: add missing JSON output.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:22 +0000 (11:20 +0000)] 
tests: py: add missing JSON output.

The JSON output was missing for some existing tests.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: add bit-shift tests.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:21 +0000 (11:20 +0000)] 
tests: shell: add bit-shift tests.

Add a couple of shell test-cases for setting the CT mark to a bitwise
expression derived from the packet mark and vice versa.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: change shift byte-order to host-endian.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:20 +0000 (11:20 +0000)] 
evaluate: change shift byte-order to host-endian.

The byte-order of the righthand operands of the right-shifts generated
for payload and exthdr expressions is big-endian.  However, all right
operands should be host-endian.  Since evaluation of the shift binop
will insert a byte-order conversion to enforce this, change the
endianness in order to avoid the extra operation.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: correct variable name.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:19 +0000 (11:20 +0000)] 
evaluate: correct variable name.

Rename the `lshift` variable used to store an right-shift expression to
`rshift`.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser: add parenthesized statement expressions.
Jeremy Sowden [Mon, 3 Feb 2020 11:20:18 +0000 (11:20 +0000)] 
parser: add parenthesized statement expressions.

Primary and primary RHS expressions support parenthesized basic and
basic RHS expressions.  However, primary statement expressions do not
support parenthesized basic statement expressions.  Add them.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: Add support for concatenated set ranges
Stefano Brivio [Thu, 30 Jan 2020 00:16:57 +0000 (01:16 +0100)] 
src: Add support for concatenated set ranges

After exporting field lengths via NFTNL_SET_DESC_CONCAT attributes,
we now need to adjust parsing of user input and generation of
netlink key data to complete support for concatenation of set
ranges.

Instead of using separate elements for start and end of a range,
denoting the end element by the NFT_SET_ELEM_INTERVAL_END flag,
as it's currently done for ranges without concatenation, we'll use
the new attribute NFTNL_SET_ELEM_KEY_END as suggested by Pablo. It
behaves in the same way as NFTNL_SET_ELEM_KEY, but it indicates
that the included key represents the upper bound of a range.

For example, "packets with an IPv4 address between 192.0.2.0 and
192.0.2.42, with destination port between 22 and 25", needs to be
expressed as a single element with two keys:

  NFTA_SET_ELEM_KEY: 192.0.2.0 . 22
  NFTA_SET_ELEM_KEY_END: 192.0.2.42 . 25

To achieve this, we need to:

- adjust the lexer rules to allow multiton expressions as elements
  of a concatenation. As wildcards are not allowed (semantics would
  be ambiguous), exclude wildcards expressions from the set of
  possible multiton expressions, and allow them directly where
  needed. Concatenations now admit prefixes and ranges

- generate, for each element in a range concatenation, a second key
  attribute, that includes the upper bound for the range

- also expand prefixes and non-ranged values in the concatenation
  to ranges: given a set with interval and concatenation support,
  the kernel has no way to tell which elements are ranged, so they
  all need to be. For example, 192.0.2.0 . 192.0.2.9 : 1024 is
  sent as:

  NFTA_SET_ELEM_KEY: 192.0.2.0 . 1024
  NFTA_SET_ELEM_KEY_END: 192.0.2.9 . 1024

- aggregate ranges when elements received by the kernel represent
  concatenated ranges, see concat_range_aggregate()

- perform a few minor adjustments where interval expressions
  are already handled: we have intervals in these sets, but
  the set specification isn't just an interval, so we can't
  just aggregate and deaggregate interval ranges linearly

v4: No changes
v3:
 - rework to use a separate key for closing element of range instead of
   a separate element with EXPR_F_INTERVAL_END set (Pablo Neira Ayuso)
v2:
 - reworked netlink_gen_concat_data(), moved loop body to a new function,
   netlink_gen_concat_data_expr() (Phil Sutter)
 - dropped repeated pattern in bison file, replaced by a new helper,
   compound_expr_alloc_or_add() (Phil Sutter)
 - added set_is_nonconcat_range() helper (Phil Sutter)
 - in expr_evaluate_set(), we need to set NFT_SET_SUBKEY also on empty
   sets where the set in the context already has the flag
 - dropped additional 'end' parameter from netlink_gen_data(),
   temporarily set EXPR_F_INTERVAL_END on expressions and use that from
   netlink_gen_concat_data() to figure out we need to add the 'end'
   element (Phil Sutter)
 - replace range_mask_len() by a simplified version, as we don't need
   to actually store the composing masks of a range (Phil Sutter)

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: Add support for NFTNL_SET_DESC_CONCAT
Stefano Brivio [Thu, 30 Jan 2020 00:16:56 +0000 (01:16 +0100)] 
src: Add support for NFTNL_SET_DESC_CONCAT

To support arbitrary range concatenations, the kernel needs to know
how long each field in the concatenation is. The new libnftnl
NFTNL_SET_DESC_CONCAT set attribute describes this as an array of
lengths, in bytes, of concatenated fields.

While evaluating concatenated expressions, export the datatype size
into the new field_len array, and hand the data over via libnftnl.

Similarly, when data is passed back from libnftnl, parse it into
the set description.

When set data is cloned, we now need to copy the additional fields
in set_clone(), too.

This change depends on the libnftnl patch with title:
  set: Add support for NFTA_SET_DESC_CONCAT attributes

v4: No changes
v3: Rework to use set description data instead of a stand-alone
    attribute
v2: No changes

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoinclude: resync nf_tables.h cache copy
Stefano Brivio [Thu, 30 Jan 2020 00:16:55 +0000 (01:16 +0100)] 
include: resync nf_tables.h cache copy

Get this header in sync with nf-next as of merge commit
b3a608222336 (5.6-rc1-ish).

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: Support testing host binaries
Phil Sutter [Thu, 6 Feb 2020 00:36:01 +0000 (01:36 +0100)] 
tests: py: Support testing host binaries

Support -H/--host option to use host's libnftables.so.1. Alternatively
users may specify a custom library path via -l/--library option.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: monitor: Support testing host's nft binary
Phil Sutter [Wed, 5 Feb 2020 18:48:53 +0000 (19:48 +0100)] 
tests: monitor: Support testing host's nft binary

Add support for -H/--host flag to use 'nft' tool from $PATH instead of
the local one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: json_echo: Support testing host binaries
Phil Sutter [Fri, 10 Jan 2020 10:19:42 +0000 (11:19 +0100)] 
tests: json_echo: Support testing host binaries

Support -H/--host option to use host's libnftables.so.1. Alternatively
users may specify a custom library path via -l/--library option.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: json_echo: Fix for Python3
Phil Sutter [Thu, 6 Feb 2020 00:21:30 +0000 (01:21 +0100)] 
tests: json_echo: Fix for Python3

The keys() method returns an object which does not support indexing, so
convert it to a list prior to doing so.

Fixes: a35e3a0cdc63a ("tests: json_echo: convert to py3")
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agonetlink: add support for handling shift expressions.
Jeremy Sowden [Sun, 19 Jan 2020 22:57:09 +0000 (22:57 +0000)] 
netlink: add support for handling shift expressions.

The kernel supports bitwise shift operations, so add support to the
netlink linearization and delinearization code.  The number of bits (the
righthand operand) is expected to be a 32-bit value in host endianness.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoinclude: update nf_tables.h.
Jeremy Sowden [Sun, 19 Jan 2020 22:57:08 +0000 (22:57 +0000)] 
include: update nf_tables.h.

The kernel UAPI header includes a couple of new bitwise netlink
attributes and an enum.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink_delinearize: remove commented out pr_debug statement.
Jeremy Sowden [Sun, 19 Jan 2020 22:57:05 +0000 (22:57 +0000)] 
netlink_delinearize: remove commented out pr_debug statement.

The statement doesn't compile, so remove it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink_delinearize: fix typo.
Jeremy Sowden [Sun, 19 Jan 2020 22:57:04 +0000 (22:57 +0000)] 
netlink_delinearize: fix typo.

s/Of/If/ in comment describing function.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>