]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
5 years agosrc: support for implicit chain bindings
Pablo Neira Ayuso [Sat, 4 Jul 2020 00:43:44 +0000 (02:43 +0200)] 
src: support for implicit chain bindings

This patch allows you to group rules in a subchain, e.g.

 table inet x {
        chain y {
                type filter hook input priority 0;
                tcp dport 22 jump {
                        ip saddr { 127.0.0.0/8, 172.23.0.0/16, 192.168.13.0/24 } accept
                        ip6 saddr ::1/128 accept;
                }
        }
 }

This also supports for the `goto' chain verdict.

This patch adds a new chain binding list to avoid a chain list lookup from the
delinearize path for the usual chains. This can be simplified later on with a
single hashtable per table for all chains.

From the shell, you have to use the explicit separator ';', in bash you
have to escape this:

 # nft add rule inet x y tcp dport 80 jump { ip saddr 127.0.0.1 accept\; ip6 saddr ::1 accept \; }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodatatype: convert chain name from gmp value to string
Pablo Neira Ayuso [Sat, 4 Jul 2020 00:43:40 +0000 (02:43 +0200)] 
datatype: convert chain name from gmp value to string

Add expr_chain_export() helper function to convert the chain name that
is stored in a gmp value variable to string.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: allow for variables in the log prefix string
Pablo Neira Ayuso [Tue, 7 Jul 2020 15:42:37 +0000 (17:42 +0200)] 
src: allow for variables in the log prefix string

For example:

 define test = "state"
 define foo = "match"

 table x {
        chain y {
                ct state invalid log prefix "invalid $test $foo:"
        }
 }

This patch scans for variables in the log prefix string. The log prefix
expression is a list of constant and variable expression that are
converted into a constant expression from the evaluation phase.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: use expression to store the log prefix
Pablo Neira Ayuso [Tue, 7 Jul 2020 12:31:33 +0000 (14:31 +0200)] 
src: use expression to store the log prefix

Intsead of using an array of char.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: zap element statement when decomposing interval
Pablo Neira Ayuso [Mon, 6 Jul 2020 08:48:16 +0000 (10:48 +0200)] 
segtree: zap element statement when decomposing interval

Otherwise, interval sets do not display element statement such as
counters.

Fixes: 6d80e0f15492 ("src: support for counter in set definition")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: Allow for empty set variable definition
Pablo Neira Ayuso [Fri, 3 Jul 2020 11:24:59 +0000 (13:24 +0200)] 
src: Allow for empty set variable definition

Allow for empty set definition in variables if they are merged to
non-empty set definition:

 define BASE_ALLOWED_INCOMING_TCP_PORTS = {22, 80, 443}
 define EXTRA_ALLOWED_INCOMING_TCP_PORTS = {}

 table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport {$BASE_ALLOWED_INCOMING_TCP_PORTS, $EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
    }
 }

However, disallow this:

 define EXTRA_ALLOWED_INCOMING_TCP_PORTS = {}

 table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport {$EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
    }
 }

 # nft -f x.nft
 /tmp/x.nft:6:18-52: Error: Set is empty
        tcp dport {$EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodoc: revisit meta/rt primary expressions and ct statement
Florian Westphal [Mon, 22 Jun 2020 08:24:57 +0000 (10:24 +0200)] 
doc: revisit meta/rt primary expressions and ct statement

Clarify meta/rt ipsec examples and document that 'ct helper set'
needs to be used *after* conntrack lookup.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agodoc: Document notrack statement
Phil Sutter [Mon, 22 Jun 2020 13:07:40 +0000 (15:07 +0200)] 
doc: Document notrack statement

Merely a stub, but better to mention it explicitly instead of having it
appear in synproxy examples and letting users guess as to what it does.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Florian Westphal <fw@strlen.de>
5 years agotests: shell: Allow wrappers to be passed as nft command
Stefano Brivio [Sun, 14 Jun 2020 21:41:37 +0000 (23:41 +0200)] 
tests: shell: Allow wrappers to be passed as nft command

The current check on $NFT only allows to directly pass an executable,
so I've been commenting it out locally for a while to run tests with
valgrind.

Instead of using the -x test, run nft without arguments and check the
exit status. POSIX.1-2017, Shell and Utilities volume, par. 2.8.2
("Exit Status for Commands") states:

  If a command is not found, the exit status shall be 127. If the
  command name is found, but it is not an executable utility, the
  exit status shall be 126. Applications that invoke utilities
  without using the shell should use these exit status values to
  report similar errors.

While this script isn't POSIX-compliant, it requires bash, and any
modern version of bash complies with those exit status requirements.
Also valgrind complies with this.

We need to quote the NFT variable passed to execute the commands in
the main loop and adjust error and informational messages, too.

This way, for example, export NFT="valgrind nft" can be issued to
run tests with valgrind.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: Run in separate network namespace, don't break connectivity
Stefano Brivio [Sun, 14 Jun 2020 21:41:57 +0000 (23:41 +0200)] 
tests: Run in separate network namespace, don't break connectivity

It might be convenient to run tests from a development branch that
resides on another host, and if we break connectivity on the test
host as tests are executed, we can't run them this way.

If kernel implementation (CONFIG_NET_NS), unshare(1), or Python
bindings for unshare() are not available, warn and continue.

Suggested-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: Bump version to v0.9.6 v0.9.6
Pablo Neira Ayuso [Mon, 15 Jun 2020 19:13:39 +0000 (21:13 +0200)] 
build: Bump version to v0.9.6

v0.9.5 broke 'vmap' support:

https://bugzilla.kernel.org/show_bug.cgi?id=208093

Release new version to fix this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: rename testcases/map/dump/0009vmap_0dump.nft
Pablo Neira Ayuso [Sun, 14 Jun 2020 15:43:01 +0000 (17:43 +0200)] 
tests: shell: rename testcases/map/dump/0009vmap_0dump.nft

Missing .nft extension in dump file.

Fixes: 54eb1e16cc47 ("evaluate: missing datatype definition in implicit_set_declaration()")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: fix build with gcc <= 4.8
Fabrice Fontaine [Sun, 7 Jun 2020 21:36:47 +0000 (23:36 +0200)] 
main: fix build with gcc <= 4.8

Since commit 719e44277f8e89323a87219b4d4bc7abac05b051, build with
gcc <= 4.8 fails on:

main.c:186:2: error: 'for' loop initial declarations are only allowed in C99 mode
  for (size_t i = IDX_INTERACTIVE + 1; i < NR_NFT_OPTIONS; ++i)
  ^

Fixes:
 - http://autobuild.buildroot.org/results/cf2359b8311fe91f9335c91f2bb4a730c9f4c9dc

Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agocmd: add misspelling suggestions for rule commands
Pablo Neira Ayuso [Sun, 7 Jun 2020 17:46:50 +0000 (19:46 +0200)] 
cmd: add misspelling suggestions for rule commands

 # nft add rule foo ber counter
 Error: No such file or directory; did you mean chain ‘bar’ in table ip ‘foo’?
 add rule foo ber counter
              ^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agometa: fix asan runtime error in tc handle
Pablo Neira Ayuso [Sun, 7 Jun 2020 17:56:40 +0000 (19:56 +0200)] 
meta: fix asan runtime error in tc handle

ASAN reports:

 meta.c:92:17: runtime error: left shift of 34661 by 16 places cannot be represented in type 'int'

use 32-bit integer as tmp variable.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: fix asan runtime error
Pablo Neira Ayuso [Sun, 7 Jun 2020 17:52:03 +0000 (19:52 +0200)] 
segtree: fix asan runtime error

ASAN reports:

 segtree.c:387:30: runtime error: variable length array bound evaluates to non-positive value 0

Update array definition to be the set size plus 1.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink: release dummy rule object from netlink_parse_set_expr()
Pablo Neira Ayuso [Sun, 7 Jun 2020 15:51:42 +0000 (17:51 +0200)] 
netlink: release dummy rule object from netlink_parse_set_expr()

netlink_parse_set_expr() creates a dummy rule object to reuse the
existing netlink parser. Release the rule object to fix a memleak.
Zap the statement list to avoid a use-after-free since the statement
needs to remain in place after releasing the rule.

==21601==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 2016 byte(s) in 4 object(s) allocated from:
    #0 0x7f7824b26330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f78245fcebd in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36
    #2 0x7f78245fd016 in xzalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:65
    #3 0x7f782456f0b5 in rule_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/rule.c:623

Add a test to check for set counters.

SUMMARY: AddressSanitizer: 2016 byte(s) leaked in 4 allocation(s).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: remove superfluous check in set_evaluate()
Pablo Neira Ayuso [Sun, 7 Jun 2020 15:32:25 +0000 (17:32 +0200)] 
evaluate: remove superfluous check in set_evaluate()

If set_is_objmap() is true, then set->data is always NULL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: missing datatype definition in implicit_set_declaration()
Pablo Neira Ayuso [Sun, 7 Jun 2020 13:23:21 +0000 (15:23 +0200)] 
evaluate: missing datatype definition in implicit_set_declaration()

set->data from implicit_set_declaration(), otherwise, set_evaluation()
bails out with:

 # nft -f /etc/nftables/inet-filter.nft
 /etc/nftables/inet-filter.nft:8:32-54: Error: map definition does not specify
 mapping data type
                tcp dport vmap { 22 : jump ssh_input }
                               ^^^^^^^^^^^^^^^^^^^^^^^
 /etc/nftables/inet-filter.nft:13:26-52: Error: map definition does not specify
 mapping data type
                 iif vmap { "eth0" : jump wan_input }
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Add a test to cover this case.

Fixes: 7aa08d45031e ("evaluate: Perform set evaluation on implicitly declared (anonymous) sets")
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=208093
Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: Bump version to v0.9.5 v0.9.5
Pablo Neira Ayuso [Sat, 6 Jun 2020 09:57:15 +0000 (11:57 +0200)] 
build: Bump version to v0.9.5

Update release name based on Jazz series, Gene Krupa's "Capital Idea".

Bump dependencies on libmnl and libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: allow flowtable definitions with no devices
Pablo Neira Ayuso [Wed, 20 May 2020 18:23:37 +0000 (20:23 +0200)] 
src: allow flowtable definitions with no devices

The listing shows no devices:

 # nft list ruleset
 table ip x {
        flowtable y {
                hook ingress priority filter
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: delete devices to an existing flowtable
Pablo Neira Ayuso [Wed, 20 May 2020 18:23:36 +0000 (20:23 +0200)] 
src: delete devices to an existing flowtable

This patch allows you to remove a device to an existing flowtable:

 # nft delete flowtable x y { devices = { eth0 } \;  }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add devices to an existing flowtable
Pablo Neira Ayuso [Wed, 20 May 2020 18:23:35 +0000 (20:23 +0200)] 
src: add devices to an existing flowtable

This patch allows you to add new devices to an existing flowtables.

 # nft add flowtable x y { devices = { eth0 } \; }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: add function to convert flowtable device list to array
Pablo Neira Ayuso [Wed, 20 May 2020 18:23:34 +0000 (20:23 +0200)] 
mnl: add function to convert flowtable device list to array

This patch adds nft_flowtable_dev_array() to convert the list of devices
into an array. This array is released through
nft_flowtable_dev_array_free().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: Enable anonymous set rule with concatenated ranges in inet/sets.t
Stefano Brivio [Wed, 27 May 2020 20:51:22 +0000 (22:51 +0200)] 
tests: py: Enable anonymous set rule with concatenated ranges in inet/sets.t

Commit 64b9aa3803dd ("tests/py: Add tests involving concatenated
ranges") introduced a rule, commented out, adding an anonymous set
including concatenated ranges. Now that they are properly handled,
we can enable it.

Note that this introduces a new warning. In the output below, '\'
marks newlines I introduced to keep lines short:

inet/sets.t: WARNING: line 24: \
'add rule inet test-inet input ip daddr . tcp dport \
{ 10.0.0.0/8 . 10-23, 192.168.1.1-192.168.3.8 . 80-443 } accept': \
'ip daddr . tcp dport \
{ 10.0.0.0/8 . 10-23, 192.168.1.1-192.168.3.8 . 80-443 } accept' \
mismatches 'meta nfproto ipv4 ip daddr . tcp dport \
{ 10.0.0.0/8 . 10-23, 192.168.1.1-192.168.3.8 . 80-443} accept'

which is similar to the existing warning, also introduced by
commit 64b9aa3803dd:

inet/sets.t: WARNING: line 23: \
'add rule inet test-inet input \
ip saddr . ip daddr . tcp dport @set3 accept': \
'ip saddr . ip daddr . tcp dport @set3 accept' mismatches \
'meta nfproto ipv4 ip saddr . ip daddr . tcp dport @set3 accept'

This is mentioned in the commit message for 64b9aa3803dd itself:

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

which means that for family inet, 'meta nfproto ipv4' is added to
the output of the rule, on top of what was passed as input, but not
for families bridge and netdev.

For this reason, it's not possible in this case to specify a single
expected output, differing from the input, and, also,
'meta nfproto ipv4' can only be passed as input for family inet as
it's not relevant for the other families.

As an alternative, we could split the rules from this test into
tests for the corresponding families, as this test case itself
is under the 'inet' directory, but I consider this beyond the scope
of this patchset.

v2: Enable rule in py/inet/sets.t instead of adding a new test in
    shell/sets (Phil Sutter)

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
5 years agoevaluate: Perform set evaluation on implicitly declared (anonymous) sets
Stefano Brivio [Wed, 27 May 2020 20:51:21 +0000 (22:51 +0200)] 
evaluate: Perform set evaluation on implicitly declared (anonymous) sets

If a set is implicitly declared, set_evaluate() is not called as a
result of cmd_evaluate_add(), because we're adding in fact something
else (e.g. a rule). Expression-wise, evaluation still happens as the
implicit set expression is eventually found in the tree and handled
by expr_evaluate_set(), but context-wise evaluation (set_evaluate())
is skipped, and this might be relevant instead.

This is visible in the reported case of an anonymous set including
concatenated ranges:

  # nft add rule t c ip saddr . tcp dport { 192.0.2.1 . 20-30 } accept
  BUG: invalid range expression type concat
  nft: expression.c:1160: range_expr_value_low: Assertion `0' failed.
  Aborted

because we reach do_add_set() without properly evaluated flags and
set description, and eventually end up in expr_to_intervals(), which
can't handle that expression.

Explicitly call set_evaluate() as we add anonymous sets into the
context, and instruct the same function to:
- skip expression-wise set evaluation if the set is anonymous, as
  that happens later anyway as part of the general tree evaluation
- skip the insertion in the set cache, as it makes no sense to have
  sets that shouldn't be referenced there

For object maps, the allocation of the expression for set->data is
already handled by set_evaluate(), so we can now drop that from
stmt_evaluate_objref_map().

v2:
 - skip insertion of set in cache (Pablo Neira Ayuso)
 - drop double allocation of expression (and leak of the first
   one) for object maps (Pablo Neira Ayuso)

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: enable reject with 802.1q
Michael Braun [Wed, 6 May 2020 09:46:24 +0000 (11:46 +0200)] 
evaluate: enable reject with 802.1q

This enables the use nft bridge reject with bridge vlan filtering.

It depends on a kernel patch to make the kernel preserve the
vlan id in nft bridge reject generation.

[ pablo: update tests/py ]

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodatatype: add frag-needed (ipv4) to reject options
Michael Braun [Wed, 6 May 2020 09:46:23 +0000 (11:46 +0200)] 
datatype: add frag-needed (ipv4) to reject options

This enables to send icmp frag-needed messages using reject target.

I have a bridge with connects an gretap tunnel with some ethernet lan.
On the gretap device I use ignore-df to avoid packets being lost without
icmp reject to the sender of the bridged packet.

Still I want to avoid packet fragmentation with the gretap packets.
So I though about adding an nftables rule like this:

nft insert rule bridge filter FORWARD \
  ip protocol tcp \
  ip length > 1400 \
  ip frag-off & 0x4000 != 0 \
  reject with icmp type frag-needed

This would reject all tcp packets with ip dont-fragment bit set that are
bigger than some threshold (here 1400 bytes). The sender would then receive
ICMP unreachable - fragmentation needed and reduce its packet size (as
defined with PMTU).

[ pablo: update tests/py ]

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: remove empty file
Pablo Neira Ayuso [Tue, 26 May 2020 21:49:37 +0000 (23:49 +0200)] 
src: remove empty file

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1429
Fixes: f9465cf517cc ("src: add STMT_NAT_F_CONCAT flag and use it")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: Actually use all available hooks in bridge/chains.t
Stefano Brivio [Sun, 24 May 2020 13:00:07 +0000 (15:00 +0200)] 
tests: py: Actually use all available hooks in bridge/chains.t

Despite being explicitly mentioned as available, prerouting and
postrouting hooks are not used, filter-pre and filter-post chains
are both built to hook on input.

Fixes: 25851df85e85 ("tests: regression: revisit chain tests")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: Fix doc build, restore A2X assignment for doc/Makefile
Stefano Brivio [Sun, 24 May 2020 12:59:36 +0000 (14:59 +0200)] 
build: Fix doc build, restore A2X assignment for doc/Makefile

Commit 4f2813a313ae ("build: Include generated man pages in dist
tarball") skips AC_CHECK_PROG for A2X altogether if doc/nft.8 is
already present.

Now, starting from a clean situation, we can have this sequence:
  ./configure # doc/nft.8 not there, A2X set in doc/Makefile
  make # builds doc/nft.8
  ./configure # doc/nft.8 is there, A2X left empty in doc/Makefile
  make clean # removes doc/nft.8
  make

resulting in:

  [...]
    GEN      nft.8
  /bin/sh: -L: command not found
  make[2]: *** [Makefile:639: nft.8] Error 127

and the only way to get out of this is to issue ./configure again
after make clean, which is rather unexpected.

Instead of skipping AC_CHECK_PROG when doc/nft.8 is present, keep
it and simply avoid returning failure if a2x(1) is not available but
doc/nft.8 was built, so that A2X is properly set in doc/Makefile
whenever needed.

Fixes: 4f2813a313ae ("build: Include generated man pages in dist tarball")
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 agobuild: fix tentative generation of nft.8 after disabled doc
Laura Garcia Liebana [Fri, 15 May 2020 16:31:51 +0000 (18:31 +0200)] 
build: fix tentative generation of nft.8 after disabled doc

Despite doc generation is disabled, the makefile is trying to build it.

$ ./configure --disable-man-doc
$ make
Making all in doc
make[2]: Entering directory '/workdir/build-pkg/workdir/doc'
make[2]: *** No rule to make target 'nft.8', needed by 'all-am'.  Stop.
make[2]: Leaving directory '/workdir/build-pkg/workdir/doc'
make[1]: *** [Makefile:479: all-recursive] Error 1
make[1]: Leaving directory '/workdir/build-pkg/workdir'
make: *** [Makefile:388: all] Error 2

Fixes: 4f2813a313ae0 ("build: Include generated man pages in dist tarball")
Reported-by: Adan Marin Jacquot <adan.marin@zevenet.com>
Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add CMD_OBJ_SETELEMS
Pablo Neira Ayuso [Fri, 8 May 2020 12:44:03 +0000 (14:44 +0200)] 
src: add CMD_OBJ_SETELEMS

This new command type results from expanding the set definition in two
commands: One to add the set and another to add the elements. This
results in 1:1 mapping between the command object to the netlink API.
The command is then translated into a netlink message which gets a
unique sequence number. This sequence number allows to correlate the
netlink extended error reporting with the corresponding command.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agolibnftables: call nft_cmd_expand() only with CMD_ADD
Pablo Neira Ayuso [Fri, 8 May 2020 12:44:02 +0000 (14:44 +0200)] 
libnftables: call nft_cmd_expand() only with CMD_ADD

Restrict the expansion logic to the CMD_ADD command which is where this
is only required.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: rename CMD_OBJ_SETELEM to CMD_OBJ_ELEMENTS
Pablo Neira Ayuso [Fri, 8 May 2020 12:44:01 +0000 (14:44 +0200)] 
src: rename CMD_OBJ_SETELEM to CMD_OBJ_ELEMENTS

The CMD_OBJ_ELEMENTS provides an expression that contains the list of
set elements. This leaves room to introduce CMD_OBJ_SETELEMS in a follow
up patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: fix error rule reporting with missing table/chain and anonymous sets
Pablo Neira Ayuso [Sat, 9 May 2020 09:36:01 +0000 (11:36 +0200)] 
mnl: fix error rule reporting with missing table/chain and anonymous sets

handle_merge() skips handle location initialization because set name != NULL.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f64f1e in erec_print (octx=0x55555555d2c0, erec=0x55555555fcf0, debug_mask=0) at erec.c:95
95              switch (indesc->type) {
(gdb) bt
    buf=0x55555555db20 "add rule inet traffic-filter input tcp dport { 22, 80, 443 } accept") at libnftables.c:459
(gdb) p indesc
$1 = (const struct input_descriptor *) 0x0

Closes: http://bugzilla.opensuse.org/show_bug.cgi?id=1171321
Fixes: 086ec6f30c96 ("mnl: extended error support for create command")
Reported-by: Jan Engelhardt <jengelh@inai.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoJSON: Improve performance of json_events_cb()
Phil Sutter [Wed, 13 May 2020 14:29:51 +0000 (16:29 +0200)] 
JSON: Improve performance of json_events_cb()

The function tries to insert handles into JSON input for echo option.
Yet there may be nothing to do if the given netlink message doesn't
contain a handle, e.g. if it is an 'add element' command. Calling
seqnum_to_json() is pointless overhead in that case, and if input is
large this overhead is significant. Better wait with that call until
after checking if the message is relevant at all.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Eric Garver <eric@garver.life>
5 years agoevaluate: fix memleak in stmt_evaluate_reject_icmp()
Pablo Neira Ayuso [Wed, 6 May 2020 20:48:04 +0000 (22:48 +0200)] 
evaluate: fix memleak in stmt_evaluate_reject_icmp()

==26297==ERROR: LeakSanitizer: detected memory leaks
                                                                                               c
Direct leak of 512 byte(s) in 4 object(s) allocated from:
    #0 0x7f46f8167330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f46f7b3cf1c in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36
    #2 0x7f46f7b3d075 in xzalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:65
    #3 0x7f46f7a85760 in expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:45
    #4 0x7f46f7a8915d in constant_expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:388
    #5 0x7f46f7a7bad4 in symbolic_constant_parse /home/pablo/devel/scm/git-netfilter/nftables/src/datatype.c:173
    #6 0x7f46f7a7af5f in symbol_parse /home/pablo/devel/scm/git-netfilter/nftables/src/datatype.c:132
    #7 0x7f46f7abf2bd in stmt_evaluate_reject_icmp /home/pablo/devel/scm/git-netfilter/nftables./src/evaluate.c:2739
    [...]
SUMMARY: AddressSanitizer: 544 byte(s) leaked in 8 allocation(s).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: fix netlink_get_setelem() memleaks
Pablo Neira Ayuso [Wed, 6 May 2020 20:34:36 +0000 (22:34 +0200)] 
src: fix netlink_get_setelem() memleaks

==26693==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 256 byte(s) in 2 object(s) allocated from:
    #0 0x7f6ce2189330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f6ce1b1767a in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36
    #2 0x7f6ce1b177d3 in xzalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:65
    #3 0x7f6ce1a41760 in expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:45
    #4 0x7f6ce1a4dea7 in set_elem_expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:1278
    #5 0x7f6ce1ac2215 in netlink_delinearize_setelem /home/pablo/devel/scm/git-netfilter/nftables/src/netlink.c:1094
    #6 0x7f6ce1ac3c16 in list_setelem_cb /home/pablo/devel/scm/git-netfilter/nftables/src/netlink.c:1172
    #7 0x7f6ce0198808 in nftnl_set_elem_foreach /home/pablo/devel/scm/git-netfilter/libnftnl/src/set_elem.c:725

Indirect leak of 256 byte(s) in 2 object(s) allocated from:
    #0 0x7f6ce2189330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f6ce1b1767a in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36
    #2 0x7f6ce1b177d3 in xzalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:65
    #3 0x7f6ce1a41760 in expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:45
    #4 0x7f6ce1a4515d in constant_expr_alloc /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:388
    #5 0x7f6ce1abaf12 in netlink_alloc_value /home/pablo/devel/scm/git-netfilter/nftables/src/netlink.c:354
    #6 0x7f6ce1ac17f5 in netlink_delinearize_setelem /home/pablo/devel/scm/git-netfilter/nftables/src/netlink.c:1080
    #7 0x7f6ce1ac3c16 in list_setelem_cb /home/pablo/devel/scm/git-netfilter/nftables/src/netlink.c:1172
    #8 0x7f6ce0198808 in nftnl_set_elem_foreach /home/pablo/devel/scm/git-netfilter/libnftnl/src/set_elem.c:725

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7f6ce2189720 in __interceptor_realloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9720)
    #1 0x7f6ce1b1778d in xrealloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:55
    #2 0x7f6ce1b1756d in gmp_xrealloc /home/pablo/devel/scm/git-netfilter/nftables/src/gmputil.c:202
    #3 0x7f6ce1417059 in __gmpz_realloc (/usr/lib/x86_64-linux-gnu/libgmp.so.10+0x23059)

Indirect leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f6ce2189330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f6ce1b1767a in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36
    #2 0x7f6ce14105c5 in __gmpz_init2 (/usr/lib/x86_64-linux-gnu/libgmp.so.10+0x1c5c5)

SUMMARY: AddressSanitizer: 536 byte(s) leaked in 6 allocation(s).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: ct_timeout: release policy string and state list
Pablo Neira Ayuso [Tue, 5 May 2020 19:02:16 +0000 (21:02 +0200)] 
src: ct_timeout: release policy string and state list

=================================================================
==19037==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 18 byte(s) in 2 object(s) allocated from:
    #0 0x7ff6ee6f9810 in strdup (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x3a810)
    #1 0x7ff6ee22666d in xstrdup /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:75
    #2 0x7ff6ee28cce9 in nft_parse /home/pablo/devel/scm/git-netfilter/nftables/src/parser_bison.c:5792
    #3 0x4b903f302c8010a  (<unknown module>)

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7ff6ee7a8330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7ff6ee226578 in xmalloc /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:36

SUMMARY: AddressSanitizer: 34 byte(s) leaked in 3 allocation(s).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: release helper type string after parsing
Pablo Neira Ayuso [Tue, 5 May 2020 18:45:50 +0000 (20:45 +0200)] 
parser_bison: release helper type string after parsing

==4060==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7f637b64a810 in strdup (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x3a810)
    #1 0x7f637b17766d in xstrdup /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:75
    #2 0x7f637b1ddce9 in nft_parse /home/pablo/devel/scm/git-netfilter/nftables/src/parser_bison.c:5792

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_bison: release extended priority string after parsing
Pablo Neira Ayuso [Tue, 5 May 2020 18:37:47 +0000 (20:37 +0200)] 
parser_bison: release extended priority string after parsing

==29581==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1034 byte(s) in 152 object(s) allocated from:
    #0 0x7f7b55f1b810 in strdup (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x3a810)
    #1 0x7f7b559597e0 in xstrdup /home/pablo/devel/scm/git-netfilter/nftables/src/utils.c:75
    #2 0x7f7b55a494a0 in nft_lex /home/pablo/devel/scm/git-netfilter/nftables/src/scanner.l:641
    #3 0x7f7b559cec25 in nft_parse /home/pablo/devel/scm/git-netfilter/nftables/src/parser_bison.c:5792
    #4 0x7f7b5597e318 in nft_parse_bison_filename /home/pablo/devel/scm/git-netfilter/nftables/src/libnftables.c:392
    #5 0x7f7b5597f864 in nft_run_cmd_from_filename /home/pablo/devel/scm/git-netfilter/nftables/src/libnftables.c:495
    #6 0x562a25bbce71 in main /home/pablo/devel/scm/git-netfilter/nftables/src/main.c:457
    #7 0x7f7b5457509a in __libc_start_main ../csu/libc-start.c:308

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add rule_stmt_append() and use it
Pablo Neira Ayuso [Tue, 5 May 2020 18:12:46 +0000 (20:12 +0200)] 
src: add rule_stmt_append() and use it

This helper function adds a statement at the end of the rule statement
list and it updates the rule statement counter.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add rule_stmt_insert_at() and use it
Pablo Neira Ayuso [Tue, 5 May 2020 17:21:56 +0000 (19:21 +0200)] 
src: add rule_stmt_insert_at() and use it

This helper function adds a statement at a given position and it updates
the rule statement counter.

This patch fixes this:

flush table bridge test-bridge
add rule bridge test-bridge input vlan id 1 ip saddr 10.0.0.1
rule.c:2870:5: runtime error: index 2 out of bounds for type 'stmt *[*]'
=================================================================
==1043==ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address 0x7ffdd69c1350 at pc 0x7f1036f53330 bp 0x7ffdd69c1300 sp 0x7ffdd69c12f8
WRITE of size 8 at 0x7ffdd69c1350 thread T0
    #0 0x7f1036f5332f in payload_try_merge /home/mbr/nftables/src/rule.c:2870
    #1 0x7f1036f534b7 in rule_postprocess /home/mbr/nftables/src/rule.c:2885
    #2 0x7f1036fb2785 in rule_evaluate /home/mbr/nftables/src/evaluate.c:3744
    #3 0x7f1036fb627b in cmd_evaluate_add /home/mbr/nftables/src/evaluate.c:3982
    #4 0x7f1036fbb9e9 in cmd_evaluate /home/mbr/nftables/src/evaluate.c:4462
    #5 0x7f10370652d2 in nft_evaluate /home/mbr/nftables/src/libnftables.c:414
    #6 0x7f1037065ba1 in nft_run_cmd_from_buffer /home/mbr/nftables/src/libnftables.c:447

Reported-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: Fix get element command with prefixes
Phil Sutter [Thu, 30 Apr 2020 12:02:44 +0000 (14:02 +0200)] 
segtree: Fix get element command with prefixes

Code wasn't aware of prefix elements in interval sets. With previous
changes in place, they merely need to be accepted in
get_set_interval_find() - value comparison and expression duplication is
identical to ranges.

Extend sets/0034get_element_0 test to cover prefixes as well. While
being at it, also cover concatenated ranges.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosegtree: Merge get_set_interval_find() and get_set_interval_end()
Phil Sutter [Thu, 30 Apr 2020 11:57:35 +0000 (13:57 +0200)] 
segtree: Merge get_set_interval_find() and get_set_interval_end()

Both functions were very similar already. Under the assumption that they
will always either see a range (or start of) that matches exactly or not
at all, reduce complexity and make get_set_interval_find() accept NULL
(left or) right values. This way it becomes a full replacement for
get_set_interval_end().

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosegtree: Use expr_clone in get_set_interval_*()
Phil Sutter [Thu, 30 Apr 2020 11:45:40 +0000 (13:45 +0200)] 
segtree: Use expr_clone in get_set_interval_*()

Both functions perform interval set lookups with either start and end or
only start values as input. Interestingly, in practice they either see
values which are not contained or which match an existing range exactly.

Make use of the above and just return a clone of the matching entry
instead of creating a new one based on input data.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosegtree: Fix missing expires value in prefixes
Phil Sutter [Tue, 28 Apr 2020 18:54:03 +0000 (20:54 +0200)] 
segtree: Fix missing expires value in prefixes

This probable copy'n'paste bug prevented 'expiration' field from being
populated when turning a range into a prefix in
interval_map_decompose(). Consequently, interval sets with timeout did
print expiry value for ranges (such as 10.0.0.1-10.0.0.5) but not
prefixes (10.0.0.0/8, for instance).

Fixes: bb0e6d8a2851b ("segtree: incorrect handling of comments and timeouts with mapping")
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agomain: fix get_optstring truncating output
Michael Braun [Sat, 2 May 2020 10:11:43 +0000 (12:11 +0200)] 
main: fix get_optstring truncating output

Without this patch, get_optstring returns optstring = +hvVcf:insNSI:d:aejuy.
After this patch, get_optstring returns optstring = +hvVcf:insNSI:d:aejuypTt

This is due to optstring containing up to two chars per option, thus it was too
short.

Fixes: 906facf31d1d ("main: fix ASAN -fsanitize=address error in get_optstring()")
Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: dump generated use new nft tool
Michael Braun [Fri, 1 May 2020 21:09:49 +0000 (23:09 +0200)] 
tests: dump generated use new nft tool

Instead of using an (possibly outdated) system nft to generate dumps,
use the newly build tool.

This fixes the dump output being corrupted if the system tool does
not support parsing new features.

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodatatype: fix double-free resulting in use-after-free in datatype_free
Michael Braun [Fri, 1 May 2020 15:48:18 +0000 (17:48 +0200)] 
datatype: fix double-free resulting in use-after-free in datatype_free

nft list table bridge t
table bridge t {
        set s4 {
                typeof ip saddr . ip daddr
                elements = { 1.0.0.1 . 2.0.0.2 }
        }
}
=================================================================
==24334==ERROR: AddressSanitizer: heap-use-after-free on address 0x6080000000a8 at pc 0x7fe0e67df0ad bp 0x7ffff83e88c0 sp 0x7ffff83e88b8
READ of size 4 at 0x6080000000a8 thread T0
    #0 0x7fe0e67df0ac in datatype_free nftables/src/datatype.c:1110
    #1 0x7fe0e67e2092 in expr_free nftables/src/expression.c:89
    #2 0x7fe0e67a855e in set_free nftables/src/rule.c:359
    #3 0x7fe0e67b2f3e in table_free nftables/src/rule.c:1263
    #4 0x7fe0e67a70ce in __cache_flush nftables/src/rule.c:299
    #5 0x7fe0e67a71c7 in cache_release nftables/src/rule.c:305
    #6 0x7fe0e68dbfa9 in nft_ctx_free nftables/src/libnftables.c:292
    #7 0x55f00fbe0051 in main nftables/src/main.c:469
    #8 0x7fe0e553309a in __libc_start_main ../csu/libc-start.c:308
    #9 0x55f00fbdd429 in _start (nftables/src/.libs/nft+0x9429)

0x6080000000a8 is located 8 bytes inside of 96-byte region [0x6080000000a0,0x608000000100)
freed by thread T0 here:
    #0 0x7fe0e6e70fb0 in __interceptor_free (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe8fb0)
    #1 0x7fe0e68b8122 in xfree nftables/src/utils.c:29
    #2 0x7fe0e67df2e5 in datatype_free nftables/src/datatype.c:1117
    #3 0x7fe0e67e2092 in expr_free nftables/src/expression.c:89
    #4 0x7fe0e67a83fe in set_free nftables/src/rule.c:356
    #5 0x7fe0e67b2f3e in table_free nftables/src/rule.c:1263
    #6 0x7fe0e67a70ce in __cache_flush nftables/src/rule.c:299
    #7 0x7fe0e67a71c7 in cache_release nftables/src/rule.c:305
    #8 0x7fe0e68dbfa9 in nft_ctx_free nftables/src/libnftables.c:292
    #9 0x55f00fbe0051 in main nftables/src/main.c:469
    #10 0x7fe0e553309a in __libc_start_main ../csu/libc-start.c:308

previously allocated by thread T0 here:
    #0 0x7fe0e6e71330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7fe0e68b813d in xmalloc nftables/src/utils.c:36
    #2 0x7fe0e68b8296 in xzalloc nftables/src/utils.c:65
    #3 0x7fe0e67de7d5 in dtype_alloc nftables/src/datatype.c:1065
    #4 0x7fe0e67df862 in concat_type_alloc nftables/src/datatype.c:1146
    #5 0x7fe0e67ea852 in concat_expr_parse_udata nftables/src/expression.c:954
    #6 0x7fe0e685dc94 in set_make_key nftables/src/netlink.c:718
    #7 0x7fe0e685e177 in netlink_delinearize_set nftables/src/netlink.c:770
    #8 0x7fe0e685f667 in list_set_cb nftables/src/netlink.c:895
    #9 0x7fe0e4f95a03 in nftnl_set_list_foreach src/set.c:904

SUMMARY: AddressSanitizer: heap-use-after-free nftables/src/datatype.c:1110 in datatype_free
Shadow bytes around the buggy address:
  0x0c107fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c107fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c107fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c107fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c107fff8000: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fd fd
=>0x0c107fff8010: fa fa fa fa fd[fd]fd fd fd fd fd fd fd fd fd fd
  0x0c107fff8020: fa fa fa fa fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c107fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c107fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c107fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c107fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==24334==ABORTING

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoutils: fix UBSAN warning in fls
Michael Braun [Fri, 1 May 2020 15:48:17 +0000 (17:48 +0200)] 
utils: fix UBSAN warning in fls

../include/utils.h:120:5: runtime error: left shift of 1103101952 by 1 places cannot be represented in type 'int'

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: fix ASAN -fsanitize=address error in get_optstring()
Michael Braun [Fri, 1 May 2020 15:48:16 +0000 (17:48 +0200)] 
main: fix ASAN -fsanitize=address error in get_optstring()

nft list table bridge t
=================================================================
==28552==ERROR: AddressSanitizer: global-buffer-overflow on address 0x5579c662e816 at pc 0x7fc2803246aa bp 0x7fff495c86f0 sp 0x7fff495c7ea0
WRITE of size 2 at 0x5579c662e816 thread T0
    #0 0x7fc2803246a9 in vsprintf (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x546a9)
    #1 0x7fc2803249f6 in __interceptor_sprintf (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x549f6)
    #2 0x5579c661e7d2 in get_optstring nftables/src/main.c:128
    #3 0x5579c66202af in main nftables/src/main.c:315
    #4 0x7fc27ea7b09a in __libc_start_main ../csu/libc-start.c:308
    #5 0x5579c661e439 in _start (nftables/src/.libs/nft+0x9439)

0x5579c662e816 is located 0 bytes to the right of global variable 'optstring' defined in 'main.c:121:14' (0x5579c662e800) of size 22
0x5579c662e816 is located 42 bytes to the left of global variable 'options' defined in 'main.c:137:23' (0x5579c662e840) of size 672
SUMMARY: AddressSanitizer: global-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x546a9) in vsprintf
Shadow bytes around the buggy address:
  0x0aafb8cbdcb0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0aafb8cbdcc0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0aafb8cbdcd0: f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9 f9
  0x0aafb8cbdce0: f9 f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00
  0x0aafb8cbdcf0: 00 00 00 00 00 00 00 00 00 f9 f9 f9 f9 f9 f9 f9
=>0x0aafb8cbdd00: 00 00[06]f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00
  0x0aafb8cbdd10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0aafb8cbdd20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0aafb8cbdd30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0aafb8cbdd40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0aafb8cbdd50: 00 00 00 00 00 00 00 00 00 00 00 00 f9 f9 f9 f9
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==28552==ABORTING

Fixes: 719e44277f8e ("main: use one data-structure to initialize getopt_long(3) arguments and help.")
Signed-of-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoct: Add support for the 'id' key
Brett Mastbergen [Fri, 1 May 2020 17:55:35 +0000 (13:55 -0400)] 
ct: Add support for the 'id' key

The 'id' key allows for matching on the id of the conntrack entry.

v2: Remove ct_id_type

Signed-off-by: Brett Mastbergen <brett.mastbergen@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agorule: fix element cache update in __do_add_setelems()
Pablo Neira Ayuso [Thu, 30 Apr 2020 14:30:15 +0000 (16:30 +0200)] 
rule: fix element cache update in __do_add_setelems()

The set->init and expr arguments might actually refer to the same list
of elements. Skip set element cache update introduced by dd44081d91ce
("segtree: Fix add and delete of element in same batch") otherwise
list_splice_tail_init() actually operates with the same list as
arguments. Valgrind reports this problem as a memleak since the result
of this operation was an empty set element list.

Fixes: dd44081d91ce ("segtree: Fix add and delete of element in same batch")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agorule: memleak in __do_add_setelems()
Pablo Neira Ayuso [Thu, 30 Apr 2020 12:18:45 +0000 (14:18 +0200)] 
rule: memleak in __do_add_setelems()

This patch invokes interval_map_decompose() with named sets:

==3402== 2,352 (128 direct, 2,224 indirect) bytes in 1 blocks are definitely lost in loss record 9 of 9
==3402==    at 0x483577F: malloc (vg_replace_malloc.c:299)
==3402==    by 0x48996A8: xmalloc (utils.c:36)
==3402==    by 0x4899778: xzalloc (utils.c:65)
==3402==    by 0x487CB46: expr_alloc (expression.c:45)
==3402==    by 0x487E2A0: mapping_expr_alloc (expression.c:1140)
==3402==    by 0x4898AA8: interval_map_decompose (segtree.c:1095)
==3402==    by 0x4872BDF: __do_add_setelems (rule.c:1569)
==3402==    by 0x4872BDF: __do_add_setelems (rule.c:1559)
==3402==    by 0x4877936: do_command (rule.c:2710)
==3402==    by 0x489F1CB: nft_netlink.isra.5 (libnftables.c:42)
==3402==    by 0x489FB07: nft_run_cmd_from_filename (libnftables.c:508)
==3402==    by 0x10A9AA: main (main.c:455)

Fixes: dd44081d91ce ("segtree: Fix add and delete of element in same batch")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonat: transform range to prefix expression when possible
Pablo Neira Ayuso [Wed, 29 Apr 2020 12:11:13 +0000 (14:11 +0200)] 
nat: transform range to prefix expression when possible

This patch transform a range of IP addresses to prefix when listing the
ruleset.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: incorrect byteorder with typeof and integer_datatype
Pablo Neira Ayuso [Wed, 29 Apr 2020 10:10:07 +0000 (12:10 +0200)] 
evaluate: incorrect byteorder with typeof and integer_datatype

 table bridge t {
         set s3 {
                 typeof meta ibrpvid
                 elements = { 2, 3, 103 }
         }
 }

 # nft --debug=netlink -f test.nft
 s3 t 0
 s3 t 0
        element 00000100  : 0 [end]     element 00000200  : 0 [end]     element 00000300  : 0 [end]
                ^^^^^^^^

The integer_type uses BYTEORDER_INVALID byteorder (which is implicitly
handled as BYTEORDER_BIG_ENDIAN).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: add NAT mappings tests
Pablo Neira Ayuso [Mon, 27 Apr 2020 20:12:21 +0000 (22:12 +0200)] 
tests: shell: add NAT mappings tests

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: remove range test with service names
Pablo Neira Ayuso [Mon, 27 Apr 2020 18:56:36 +0000 (20:56 +0200)] 
tests: py: remove range test with service names

Service names printing are not default these days, using service names
with ranges is misleading.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: restore --debug=netlink output with sets
Pablo Neira Ayuso [Mon, 27 Apr 2020 18:40:12 +0000 (20:40 +0200)] 
mnl: restore --debug=netlink output with sets

(null) (null) b size 1

The debugging output displays table and set names as (null). This patch
sets the table and name before displaying the netlink debugging, then
unset them to not break the extended error support.

Fixes: 086ec6f30c96 ("mnl: extended error support for create command")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: py: concatenation, netmap and nat mappings
Pablo Neira Ayuso [Mon, 27 Apr 2020 18:10:34 +0000 (20:10 +0200)] 
tests: py: concatenation, netmap and nat mappings

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: fix crash when handling concatenation without map
Pablo Neira Ayuso [Mon, 27 Apr 2020 16:38:01 +0000 (18:38 +0200)] 
evaluate: fix crash when handling concatenation without map

Fix a crash when map is not specified, e.g.

 nft add rule x y snat ip addr . port to 1.1.1.1 . 22

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add STMT_NAT_F_CONCAT flag and use it
Pablo Neira Ayuso [Mon, 27 Apr 2020 10:21:34 +0000 (12:21 +0200)] 
src: add STMT_NAT_F_CONCAT flag and use it

Replace ipportmap boolean field by flags.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add netmap support
Pablo Neira Ayuso [Fri, 24 Apr 2020 19:56:51 +0000 (21:56 +0200)] 
src: add netmap support

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 prefix to ip saddr map { 10.141.11.0/24 : 192.168.2.0/24 }
        }
 }

The example above performs SNAT to packets that comes from
10.141.11.0/24 using the prefix 192.168.2.0/24, e.g. 10.141.11.4 is
mangled to 192.168.2.4.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoinclude: resync nf_nat.h kernel header
Pablo Neira Ayuso [Fri, 24 Apr 2020 19:56:49 +0000 (21:56 +0200)] 
include: resync nf_nat.h kernel header

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
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>