]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
3 years agocache: filter out rules by chain
Pablo Neira Ayuso [Wed, 10 Nov 2021 17:08:41 +0000 (18:08 +0100)] 
cache: filter out rules by chain

With an autogenerated ruleset with ~20k chains.

 # time nft list ruleset &> /dev/null

 real    0m1,712s
 user    0m1,258s
 sys     0m0,454s

Speed up listing of a specific chain:

 # time nft list chain nat MWDG-UGR-234PNG3YBUOTS5QD &> /dev/null

 real    0m0,542s
 user    0m0,251s
 sys     0m0,292s

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: missing family in cache filtering
Pablo Neira Ayuso [Tue, 9 Nov 2021 11:15:44 +0000 (12:15 +0100)] 
cache: missing family in cache filtering

Check family when filtering out listing of tables and sets.

Fixes: 3f1d3912c3a6 ("cache: filter out tables that are not requested")
Fixes: 635ee1cad8aa ("cache: filter out sets and maps that are not requested")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: do not populate cache if it is going to be flushed
Pablo Neira Ayuso [Tue, 9 Nov 2021 09:44:46 +0000 (10:44 +0100)] 
cache: do not populate cache if it is going to be flushed

Skip set element netlink dump if set is flushed, this speeds up
set flush + add element operation in a batch file for an existing set.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: move list filter under struct
Pablo Neira Ayuso [Tue, 9 Nov 2021 09:35:05 +0000 (10:35 +0100)] 
cache: move list filter under struct

Wrap the table and set fields for list filtering to prepare for the
introduction element filters.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: update ct timeout section with the state names
Florian Westphal [Thu, 28 Oct 2021 15:36:06 +0000 (17:36 +0200)] 
doc: update ct timeout section with the state names

docs are too terse and did not have the list of valid timeout states.
While at it, adjust default stream timeout of udp to 120, this is the
current kernel default.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: py: update rawpayload.t.json
Pablo Neira Ayuso [Fri, 5 Nov 2021 15:47:57 +0000 (16:47 +0100)] 
tests: py: update rawpayload.t.json

Missing update of json test.

Fixes: 6ad2058da66a ("datatype: add xinteger_type alias to print in hexadecimal")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: grab reference in set expression evaluation
Pablo Neira Ayuso [Fri, 5 Nov 2021 14:55:20 +0000 (15:55 +0100)] 
evaluate: grab reference in set expression evaluation

Do not clone expression when evaluation a set expression, grabbing the
reference counter to reuse the object is sufficient.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: clone variable expression if there is more than one reference
Pablo Neira Ayuso [Fri, 5 Nov 2021 13:43:17 +0000 (14:43 +0100)] 
evaluate: clone variable expression if there is more than one reference

Clone the expression that defines the variable value if there are
multiple references to it in the ruleset. This saves heap memory
consumption in case the variable defines a set with a huge number of
elements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomnl: do not build nftnl_set element list
Pablo Neira Ayuso [Thu, 4 Nov 2021 11:53:11 +0000 (12:53 +0100)] 
mnl: do not build nftnl_set element list

Do not call alloc_setelem_cache() to build the set element list in
nftnl_set. Instead, translate one single set element expression to
nftnl_set_elem object at a time and use this object to build the netlink
header.

Using a huge test set containing 1.1 million element blocklist, this
patch is reducing userspace memory consumption by 40%.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: py: remove verdict from closing end interval
Pablo Neira Ayuso [Thu, 4 Nov 2021 19:28:51 +0000 (20:28 +0100)] 
tests: py: remove verdict from closing end interval

Kernel does not allow for NFT_SET_ELEM_INTERVAL_END flag and
NFTA_SET_ELEM_DATA. The closing end interval represents a mismatch,
therefore, no verdict can be applied. The existing payload files show
the drop verdict when this is unset (because NF_DROP=0).

This update is required to fix payload warnings in tests/py after
libnftnl's ("set: use NFTNL_SET_ELEM_VERDICT to print verdict").

Fixes: 6671d9d137f6 ("mnl: Set NFTNL_SET_DATA_TYPE before dumping set elements")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: raw payload match and mangle on inner header / payload data
Pablo Neira Ayuso [Tue, 2 Nov 2021 13:01:58 +0000 (14:01 +0100)] 
src: raw payload match and mangle on inner header / payload data

This patch adds support to match on inner header / payload data:

 # nft add rule x y @ih,32,32 0x14000000 counter

you can also mangle payload data:

 # nft add rule x y @ih,32,32 set 0x14000000 counter

This update triggers a checksum update at the layer 4 header via
csum_flags, mangling odd bytes is also aligned to 16-bits.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: shell: $NFT needs to be invoked unquoted
Štěpán Němec [Fri, 5 Nov 2021 11:39:11 +0000 (12:39 +0100)] 
tests: shell: $NFT needs to be invoked unquoted

The variable has to undergo word splitting, otherwise the shell tries
to find the variable value as an executable, which breaks in cases that
7c8a44b25c22 ("tests: shell: Allow wrappers to be passed as nft command")
intends to support.

Mention this in the shell tests README.

Fixes: d8ccad2a2b73 ("tests: cover baecd1cf2685 ("segtree: Fix segfault when restoring a huge interval set")")
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: README: clarify test file name convention
Štěpán Němec [Fri, 5 Nov 2021 11:39:10 +0000 (12:39 +0100)] 
tests: shell: README: clarify test file name convention

Since commit 4d26b6dd3c4c, test file name suffix no longer reflects
expected exit code in all cases.

Move the sentence "Since they are located with `find', test files can
be put in any subdirectory." to a separate paragraph.

Fixes: 4d26b6dd3c4c ("tests: shell: change all test scripts to return 0")
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: README: $NFT does not have to be a path to a binary
Štěpán Němec [Fri, 5 Nov 2021 11:39:09 +0000 (12:39 +0100)] 
tests: shell: README: $NFT does not have to be a path to a binary

Since commit 7c8a44b25c22, $NFT can contain an arbitrary command,
e.g. 'valgrind nft'.

Fixes: 7c8a44b25c22 ("tests: shell: Allow wrappers to be passed as nft command")
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: README: copy edit
Štěpán Němec [Fri, 5 Nov 2021 11:39:08 +0000 (12:39 +0100)] 
tests: shell: README: copy edit

Grammar, wording, formatting fixes (no substantial change of meaning).

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agodatatype: add xinteger_type alias to print in hexadecimal
Pablo Neira Ayuso [Tue, 2 Nov 2021 13:07:04 +0000 (14:07 +0100)] 
datatype: add xinteger_type alias to print in hexadecimal

Add an alias of the integer type to print raw payload expressions in
hexadecimal.

Update tests/py.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: postpone transport protocol match check after nat expression evaluation
Pablo Neira Ayuso [Tue, 2 Nov 2021 10:31:40 +0000 (11:31 +0100)] 
evaluate: postpone transport protocol match check after nat expression evaluation

Fix bogus error report when using transport protocol as map key.

Fixes: 50780456a01a ("evaluate: check for missing transport protocol match in nat map with concatenations")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser: extend limit syntax
Jeremy Sowden [Fri, 29 Oct 2021 20:40:09 +0000 (21:40 +0100)] 
parser: extend limit syntax

The documentation describes the syntax of limit statements thus:

  limit rate [over] packet_number / TIME_UNIT [burst packet_number packets]
  limit rate [over] byte_number BYTE_UNIT / TIME_UNIT [burst byte_number BYTE_UNIT]

  TIME_UNIT := second | minute | hour | day
  BYTE_UNIT := bytes | kbytes | mbytes

From this one might infer that a limit may be specified by any of the
following:

  limit rate 1048576/second
  limit rate 1048576 mbytes/second

  limit rate 1048576 / second
  limit rate 1048576 mbytes / second

However, the last does not currently parse:

  $ sudo /usr/sbin/nft add filter input limit rate 1048576 mbytes / second
  Error: wrong rate format
  add filter input limit rate 1048576 mbytes / second
                   ^^^^^^^^^^^^^^^^^^^^^^^^^

Extend the `limit_rate_bytes` parser rule to support it, and add some
new Python test-cases.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser: add `limit_rate_pkts` and `limit_rate_bytes` rules
Jeremy Sowden [Fri, 29 Oct 2021 20:40:08 +0000 (21:40 +0100)] 
parser: add `limit_rate_pkts` and `limit_rate_bytes` rules

Factor the `N / time-unit` and `N byte-unit / time-unit` expressions
from limit expressions out into separate `limit_rate_pkts` and
`limit_rate_bytes` rules respectively.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser: add new `limit_bytes` rule
Jeremy Sowden [Fri, 29 Oct 2021 20:40:07 +0000 (21:40 +0100)] 
parser: add new `limit_bytes` rule

Refactor the `N byte-unit` expression out of the `limit_bytes_burst`
rule into a separate `limit_bytes` rule.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: run-tests.sh: ensure non-zero exit when $failed != 0
Štěpán Němec [Wed, 20 Oct 2021 12:44:09 +0000 (14:44 +0200)] 
tests: run-tests.sh: ensure non-zero exit when $failed != 0

POSIX [1] does not specify the behavior of `exit' with arguments
outside the 0-255 range, but what generally (bash, dash, zsh, OpenBSD
ksh, busybox) seems to happen is the shell exiting with status & 255
[2], which results in zero exit for certain non-zero arguments.

[1] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#exit
[2] https://git.savannah.gnu.org/cgit/bash.git/tree/builtins/common.c#n579

Fixes: 0c6592420586 ("tests: fix return codes")
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: Fix bogus testsuite failure with 250Hz
Phil Sutter [Tue, 2 Nov 2021 19:53:53 +0000 (20:53 +0100)] 
tests: shell: Fix bogus testsuite failure with 250Hz

Previous fix for HZ=100 was not sufficient, a kernel with HZ=250 rounds
the 10ms to 8ms it seems. Do as Lukas suggests and accept the occasional
input/output asymmetry instead of continuing the hide'n'seek game.

Fixes: c9c5b5f621c37 ("tests: shell: Fix bogus testsuite failure with 100Hz")
Suggested-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: Support netdev egress hook
Lukas Wunner [Wed, 11 Mar 2020 12:20:06 +0000 (13:20 +0100)] 
src: Support netdev egress hook

Add userspace support for the netdev egress hook which is queued up for
v5.16-rc1, complete with documentation and tests.  Usage is identical to
the ingress hook.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: py: Move netdev-specific tests to appropriate subdirectory
Lukas Wunner [Sun, 24 Oct 2021 07:37:35 +0000 (09:37 +0200)] 
tests: py: Move netdev-specific tests to appropriate subdirectory

The fwd and dup statements are specific to netdev hooks, so move their
tests to the appropriate subdirectory.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: shell: add testcase for --terse
Pablo Neira Ayuso [Wed, 27 Oct 2021 23:50:41 +0000 (01:50 +0200)] 
tests: shell: add testcase for --terse

Compare listing with and without --terse for:

 nft list ruleset
 nft list set x y

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: disable NFT_CACHE_SETELEM_BIT on --terse listing only
Pablo Neira Ayuso [Wed, 27 Oct 2021 23:14:30 +0000 (01:14 +0200)] 
cache: disable NFT_CACHE_SETELEM_BIT on --terse listing only

Instead of NFT_CACHE_SETELEM which also disables set dump.

Fixes: 6bcd0d576a60 ("cache: unset NFT_CACHE_SETELEM with --terse listing")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: ensure evaluate_cache_list flags are set correctly
Chris Arges [Tue, 26 Oct 2021 22:09:28 +0000 (00:09 +0200)] 
cache: ensure evaluate_cache_list flags are set correctly

This change ensures that when listing rulesets with the terse flag that the
terse flag is maintained.

Fixes: 6bcd0d576a60 ("cache: unset NFT_CACHE_SETELEM with --terse listing")
Signed-off-by: Chris Arges <carges@cloudflare.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: honor table in set filtering
Pablo Neira Ayuso [Mon, 25 Oct 2021 21:46:36 +0000 (23:46 +0200)] 
cache: honor table in set filtering

Check if table mismatch, in case the same set name is used in different
tables.

Fixes: 635ee1cad8aa ("cache: filter out sets and maps that are not requested")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: honor filter in set listing commands
Pablo Neira Ayuso [Mon, 25 Oct 2021 21:34:07 +0000 (23:34 +0200)] 
cache: honor filter in set listing commands

Fetch table, set and set elements only for set listing commands, e.g.
nft list set inet filter ipv4_bogons.

Fixes: 635ee1cad8aa ("cache: filter out sets and maps that are not requested")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: always set on NFT_CACHE_REFRESH for listing
Pablo Neira Ayuso [Mon, 25 Oct 2021 21:32:34 +0000 (23:32 +0200)] 
cache: always set on NFT_CACHE_REFRESH for listing

This flag forces a refresh of the cache on list commands, several
object types are missing this flag, this fixes nft --interactive
mode.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoconfigure: default to libedit for cli
Pablo Neira Ayuso [Mon, 25 Oct 2021 20:46:13 +0000 (22:46 +0200)] 
configure: default to libedit for cli

readline support only compiles for libreadline5, set libedit as default
library.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: cover baecd1cf2685 ("segtree: Fix segfault when restoring a huge interval...
Štěpán Němec [Wed, 20 Oct 2021 12:42:20 +0000 (14:42 +0200)] 
tests: cover baecd1cf2685 ("segtree: Fix segfault when restoring a huge interval set")

Test inspired by [1] with both the set and stack size reduced by the
same power of 2, to preserve the (pre-baecd1cf2685) segfault on one
hand, and make the test successfully complete (post-baecd1cf2685) in a
few seconds even on weaker hardware on the other.

(The reason I stopped at 128kB stack size is that with 64kB I was
getting segfaults even with baecd1cf2685 applied.)

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1908127

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Helped-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agomain: _exit() if setuid
Florian Westphal [Sat, 16 Oct 2021 22:56:23 +0000 (00:56 +0200)] 
main: _exit() if setuid

Apparently some people think its a good idea to make nft setuid so
unrivilged users can change settings.

"nft -f /etc/shadow" is just one example of why this is a bad idea.
Disable this.  Do not print anything, fd cannot be trusted.

This change intentionally doesn't affect libnftables, on the off-chance
that somebody creates an suid program and knows what they're doing.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: shell: auto-removal of chain hook on netns removal
Florian Westphal [Tue, 19 Oct 2021 12:07:25 +0000 (14:07 +0200)] 
tests: shell: auto-removal of chain hook on netns removal

This is the nft equivalent of the syzbot report that lead to
kernel commit 68a3765c659f8
("netfilter: nf_tables: skip netdev events generated on netns removal").

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agorule: replace three conditionals with one
Jeremy Sowden [Thu, 7 Oct 2021 20:12:22 +0000 (21:12 +0100)] 
rule: replace three conditionals with one

When outputting set definitions, merge three consecutive
`if (!list_empty(&set->stmt_list))` conditionals.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agorule: fix stateless output after listing sets containing counters
Jeremy Sowden [Thu, 7 Oct 2021 20:12:21 +0000 (21:12 +0100)] 
rule: fix stateless output after listing sets containing counters

Before outputting counters in set definitions the
`NFT_CTX_OUTPUT_STATELESS` flag was set to suppress output of the
counter state and unconditionally cleared afterwards, regardless of
whether it had been originally set.  Record the original set of flags
and restore it.

Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=994273
Fixes: 6d80e0f15492 ("src: support for counter in set definition")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agorule: remove fake stateless output of named counters
Jeremy Sowden [Thu, 7 Oct 2021 20:12:20 +0000 (21:12 +0100)] 
rule: remove fake stateless output of named counters

When `-s` is passed, no state is output for named quotas and counter and
quota rules, but fake zero state is output for named counters.  Remove
the output of named counters to match the remaining stateful objects.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: libnftables-json: make the example valid libnftables JSON input
Štěpán Němec [Mon, 11 Oct 2021 11:59:04 +0000 (13:59 +0200)] 
doc: libnftables-json: make the example valid libnftables JSON input

- Add missing comma between array elements.
- Fix chain 'name' property.
- Match 'op' property is mandatory.

Fixes: 2e56f533b36a ("doc: Improve example in libnftables-json(5)")
Fixes: 90d4ee087171 ("JSON: Make match op mandatory, introduce 'in' operator")
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agocache: unset NFT_CACHE_SETELEM with --terse listing
Pablo Neira Ayuso [Sat, 2 Oct 2021 11:49:53 +0000 (13:49 +0200)] 
cache: unset NFT_CACHE_SETELEM with --terse listing

Skip populating the set element cache in this case to speed up listing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: filter out sets and maps that are not requested
Pablo Neira Ayuso [Wed, 29 Sep 2021 16:01:47 +0000 (18:01 +0200)] 
cache: filter out sets and maps that are not requested

Do not fetch set content for list commands that specify a
set name.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: filter out tables that are not requested
Pablo Neira Ayuso [Wed, 29 Sep 2021 11:09:03 +0000 (13:09 +0200)] 
cache: filter out tables that are not requested

Do not fetch table content for list commands that specify a
table name, e.g.

 # nft list table filter

This speeds up listing of a given table by not populating the
cache with tables that are not needed.

 - Full ruleset (huge with ~100k lines).

 # sudo nft list ruleset &> /dev/null
 real    0m3,049s
 user    0m2,080s
 sys     0m0,968s

- Listing per table is now faster:

 # nft list table nat &> /dev/null
 real    0m1,969s
 user    0m1,412s
 sys     0m0,556s

 # nft list table filter &> /dev/null
 real    0m0,697s
 user    0m0,478s
 sys     0m0,220s

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1326
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: finer grain cache population for list commands
Pablo Neira Ayuso [Wed, 29 Sep 2021 09:57:41 +0000 (11:57 +0200)] 
cache: finer grain cache population for list commands

Skip full cache population for list commands to speed up listing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: set on cache flags for nested notation
Pablo Neira Ayuso [Wed, 29 Sep 2021 08:55:19 +0000 (10:55 +0200)] 
cache: set on cache flags for nested notation

Set on the cache flags for the nested notation too, this is fixing nft -f
with two files, one that contains the set declaration and another that
adds a rule that refers to such set.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1474
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: check for missing transport protocol match in nat map with concatenations
Pablo Neira Ayuso [Tue, 28 Sep 2021 20:34:10 +0000 (22:34 +0200)] 
evaluate: check for missing transport protocol match in nat map with concatenations

Restore this error with NAT maps:

 # nft add rule 'ip ipfoo c dnat to ip daddr map @y'
 Error: transport protocol mapping is only valid after transport protocol match
 add rule ip ipfoo c dnat to ip daddr map @y
                     ~~~~    ^^^^^^^^^^^^^^^

Allow for transport protocol match in the map too, which is implicitly
pulling in a transport protocol dependency.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: check for concatenation in set data datatype
Pablo Neira Ayuso [Tue, 28 Sep 2021 12:09:54 +0000 (14:09 +0200)] 
evaluate: check for concatenation in set data datatype

When adding this rule with an existing map:

  add rule nat x y meta l4proto { tcp, udp } dnat ip to ip daddr . th dport map @fwdtoip_th

reports a bogus:

Error: datatype mismatch: expected IPv4 address, expression has type
concatenation of (IPv4 address, internet network service)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomonitor: honor NLM_F_EXCL netlink flag
Pablo Neira Ayuso [Sun, 26 Sep 2021 10:27:45 +0000 (12:27 +0200)] 
monitor: honor NLM_F_EXCL netlink flag

This allow to report for the create command.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: monitor: update insert and replace commands
Pablo Neira Ayuso [Fri, 24 Sep 2021 23:34:36 +0000 (01:34 +0200)] 
tests: monitor: update insert and replace commands

Adjust test after these two kernel fixes:

("netfilter: nf_tables: reverse order in rule replacement expansion")
("netfilter: nf_tables: add position handle in event notification")

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomonitor: honor NLM_F_APPEND flag for rules
Pablo Neira Ayuso [Mon, 20 Sep 2021 21:39:17 +0000 (23:39 +0200)] 
monitor: honor NLM_F_APPEND flag for rules

Print 'add' or 'insert' according to this netlink flag.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomonitor: display rule position handle
Pablo Neira Ayuso [Mon, 20 Sep 2021 16:52:18 +0000 (18:52 +0200)] 
monitor: display rule position handle

This allow to locate the incremental update in the ruleset.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink: dynset: set compound expr dtype based on set key definition
Florian Westphal [Tue, 28 Sep 2021 19:34:30 +0000 (21:34 +0200)] 
netlink: dynset: set compound expr dtype based on set key definition

"nft add rule ... add @t { ip saddr . 22 ..." will be listed as
'ip saddr . 0x16  [ invalid type]".

This is a display bug, the compound expression created during netlink
deserialization lacks correct datatypes for the value expression.

Avoid this by setting the individual expressions' datatype.
The set key has the needed information, so walk over the types and set
them in the dynset statment.

Also add a test case.

Reported-by: Paulo Ricardo Bruck <paulobruck1@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agopayload: don't adjust offsets of autogenerated dependency expressions
Florian Westphal [Tue, 28 Sep 2021 12:16:48 +0000 (14:16 +0200)] 
payload: don't adjust offsets of autogenerated dependency expressions

Pablo says:
  user reports that this is broken:
  nft --debug=netlink add rule bridge filter forward vlan id 100 vlan id set 200
[..]
    [ payload load 2b @ link header + 14 => reg 1 ]
[..]
    [ payload load 2b @ link header + 28 => reg 1 ]
    [ bitwise reg 1 = ( reg 1 & 0x000000f0 ) ^ 0x0000c800 ]
    [ payload write reg 1 => 2b @ link header + 14 csum_type 0 csum_off 0 csum_flags 0x0 ]

    offset says 28, it is assuming q-in-q, in this case it is mangling the
    existing header.

The problem here is that 'vlan id set 200' needs a read-modify-write
cycle because 'vlan id set' has to preserve bits located in the same byte area
as the vlan id.

The first 'payload load' at offset 14 is generated via 'vlan id 100',
this part is ok.

The second 'payload load' at offset 28 is the bogus one.
Its added as a dependency, but then adjusted because nft evaluation
considers this identical to 'vlan id 1 vlan id '2, where nft assumes
q-in-q.

To fix this, skip offset adjustments for raw expressions and mark the
dependency-generated payload instruction as such.

This is fine because raw payload operations assume that user specifies
base/offset/length manually.

Also add a test case for this.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonetlink: reset temporary set element stmt list after list splice
Pablo Neira Ayuso [Thu, 16 Sep 2021 11:51:23 +0000 (13:51 +0200)] 
netlink: reset temporary set element stmt list after list splice

Reset temporary stmt list to deal with the key_end case which might
result in a jump backward to handle the rhs of the interval.

Reported-by: Martin Zatloukal <slezi2@pvfree.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: fix synopsis of named counter, quota and ct {helper,timeout,expect}
Pablo Neira Ayuso [Thu, 16 Sep 2021 09:59:13 +0000 (11:59 +0200)] 
doc: fix synopsis of named counter, quota and ct {helper,timeout,expect}

Synopsis is not complete. Add examples for counters and quotas.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: py: update ct expiration
Pablo Neira Ayuso [Wed, 15 Sep 2021 00:05:22 +0000 (02:05 +0200)] 
tests: py: update ct expiration

Since 309785674b25 ("datatype: time_print() ignores -T"), time_type
honors -T option. Given tests/py run in numeric format, this patch
fixes a warning since the ct expiration is now expressed in seconds.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: revert hashtable for expression handlers
Pablo Neira Ayuso [Tue, 14 Sep 2021 23:05:52 +0000 (01:05 +0200)] 
src: revert hashtable for expression handlers

Partially revert 913979f882d1 ("src: add expression handler hashtable")
which is causing a crash with two instances of the nftables handler.

$ sudo python
[sudo] password for echerkashin:
Python 3.9.7 (default, Sep  3 2021, 06:18:44)
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from nftables import Nftables
>>> n1=Nftables()
>>> n2=Nftables()
>>> <Ctrl-D>
double free or corruption (top)
Aborted

Reported-by: Eugene Crosser <crosser@average.org>
Suggested-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: nfnetlink_log allows one single process through unicast
Pablo Neira Ayuso [Mon, 6 Sep 2021 20:34:50 +0000 (22:34 +0200)] 
doc: nfnetlink_log allows one single process through unicast

nfnetlink_log uses netlink unicast to send logs to one single process in
userspace.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink: rework range_expr_to_prefix()
Pablo Neira Ayuso [Mon, 6 Sep 2021 20:04:51 +0000 (22:04 +0200)] 
netlink: rework range_expr_to_prefix()

Consolidate prefix calculation in range_expr_is_prefix().

Add tests/py for 9208fb30dc49 ("src: Check range bounds before converting to
prefix").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agometa: skip -T for hour and date format
Pablo Neira Ayuso [Mon, 6 Sep 2021 08:31:19 +0000 (10:31 +0200)] 
meta: skip -T for hour and date format

If -T is used:

- meta hour displays the hours in seconds based on your timezone.
- meta time displays the UNIX time since 1970 in nanoseconds.

Better, skip -T for these two datatypes and use the formatted output
instead, ie.

- meta hour "00:00:20"
- meta time "1970-01-01 01:00:01"

Fixes: f8f32deda31d ("meta: Introduce new conditions 'time', 'day' and 'hour'")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: Check range bounds before converting to prefix
Xiao Liang [Mon, 6 Sep 2021 03:06:41 +0000 (11:06 +0800)] 
src: Check range bounds before converting to prefix

The lower bound must be the first value of the prefix to be coverted.
For example, range "10.0.0.15-10.0.0.240" can not be converted to
"10.0.0.15/24". Validate it by checking if the lower bound value has
enough trailing zeros.

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: refer to ulogd manpage
Pablo Neira Ayuso [Mon, 6 Sep 2021 11:18:34 +0000 (13:18 +0200)] 
doc: refer to ulogd manpage

Refer to the ulogd daemon in the log statement section.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: libnflog handles `log group`, not libnfq
Duncan Roe [Mon, 6 Sep 2021 03:40:09 +0000 (13:40 +1000)] 
doc: libnflog handles `log group`, not libnfq

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodoc: Missing NFT_CTX_OUTPUT_NUMERIC_SYMBOL in libnftables documentation
Pablo Neira Ayuso [Mon, 6 Sep 2021 08:39:58 +0000 (10:39 +0200)] 
doc: Missing NFT_CTX_OUTPUT_NUMERIC_SYMBOL in libnftables documentation

Add NFT_CTX_OUTPUT_NUMERIC_SYMBOL to libnftables.adoc to keep it in sync
with the nftables/libnftables.h header.

Fixes: 685a06447ee4 ("doc: libnftables.adoc misc cleanups")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoinclude: add NFT_CTX_OUTPUT_NUMERIC_TIME to NFT_CTX_OUTPUT_NUMERIC_ALL
Pablo Neira Ayuso [Fri, 3 Sep 2021 08:29:16 +0000 (10:29 +0200)] 
include: add NFT_CTX_OUTPUT_NUMERIC_TIME to NFT_CTX_OUTPUT_NUMERIC_ALL

Therefore, -n honors numeric time in seconds.

Fixes: f8f32deda31d ("meta: Introduce new conditions 'time', 'day' and 'hour'")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agodatatype: time_print() ignores -T
Pablo Neira Ayuso [Fri, 3 Sep 2021 08:12:11 +0000 (10:12 +0200)] 
datatype: time_print() ignores -T

Honor NFT_CTX_OUTPUT_NUMERIC_TIME.

 # nft list ruleset
 table ip x {
        set y {
                type ipv4_addr
                flags timeout
                elements = { 1.1.1.1 timeout 5m expires 1m49s40ms }
        }
 }
 # sudo nft -T list ruleset
 table ip x {
        set y {
                type ipv4_addr
                flags timeout
                elements = { 1.1.1.1 timeout 300s expires 108s }
        }
 }

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1561
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agorule: remove redundant meta protocol from the evaluation step
Pablo Neira Ayuso [Mon, 30 Aug 2021 19:37:50 +0000 (21:37 +0200)] 
rule: remove redundant meta protocol from the evaluation step

567ea4774e13 ("netlink_delinearize: incorrect meta protocol dependency kill")
does not document two cases that are handled in this patch:

- 'meta protocol ip' is removed if used in the ip family.
- 'meta protocol ip6' is removed if used in the ip6 family.

This patch removes this redundancy earlier, from the evaluation step
before netlink bytecode generation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink_delinearize: incorrect meta protocol dependency kill again
Pablo Neira Ayuso [Mon, 30 Aug 2021 21:31:59 +0000 (23:31 +0200)] 
netlink_delinearize: incorrect meta protocol dependency kill again

This patch adds __meta_dependency_may_kill() to consolidate inspection
of the meta protocol, nfproto and ether type expression to validate
dependency removal on listings.

Phil reports that 567ea4774e13 includes an update on the ip and ip6
families that is not described in the patch, moreover, it flips the
default verdict from true to false.

Fixes: 567ea4774e13 ("netlink_delinearize: incorrect meta protocol dependency kill")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser_json: Fix error reporting for invalid syntax
Phil Sutter [Wed, 1 Sep 2021 14:41:44 +0000 (16:41 +0200)] 
parser_json: Fix error reporting for invalid syntax

Errors emitted by the JSON parser caused BUG() in erec_print() due to
input descriptor values being bogus.

Due to lack of 'include' support, JSON parser uses a single input
descriptor only and it lived inside the json_ctx object on stack of
nft_parse_json_*() functions.

By the time errors are printed though, that scope is not valid anymore.
Move the static input descriptor object to avoid this.

Fixes: 586ad210368b7 ("libnftables: Implement JSON parser")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonetlink_delinearize: incorrect meta protocol dependency kill
Pablo Neira Ayuso [Thu, 26 Aug 2021 10:24:37 +0000 (12:24 +0200)] 
netlink_delinearize: incorrect meta protocol dependency kill

meta protocol is meaningful in bridge, netdev and inet families, do
not remove this.

Fixes: 056aaa3e6dc6 ("netlink_delinearize: Refactor meta_may_dependency_kill()")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: provide a empty list for flowtables and objects when request fails
Pablo Neira Ayuso [Wed, 25 Aug 2021 13:46:20 +0000 (15:46 +0200)] 
cache: provide a empty list for flowtables and objects when request fails

Old kernels do not support for dumping the flowtable and object lists,
provide an empty list instead to unbreak the cache initialization.

Fixes: 560963c4d41e ("cache: add hashtable cache for flowtable")
Fixes: 45a84088ecbd ("cache: add hashtable cache for object")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: skip set element netlink dump for add/delete element command
Pablo Neira Ayuso [Tue, 24 Aug 2021 09:52:36 +0000 (11:52 +0200)] 
cache: skip set element netlink dump for add/delete element command

Add NFT_CACHE_SETELEM_MAYBE to dump the set elements conditionally,
only in case that the set interval flag is set on.

Reported-by: Cristian Constantin <const.crist@googlemail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: monitor: Continue on error
Phil Sutter [Wed, 11 Aug 2021 20:11:46 +0000 (22:11 +0200)] 
tests: monitor: Continue on error

Just make sure return code reflects the overall result.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: monitor: Print errors to stderr
Phil Sutter [Wed, 11 Aug 2021 18:08:14 +0000 (20:08 +0200)] 
tests: monitor: Print errors to stderr

While being at it, introduce die() to error and exit. But don't use it
everywhere to prepare for continuing on errors.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: json_echo: Print errors to stderr
Phil Sutter [Wed, 11 Aug 2021 16:14:06 +0000 (18:14 +0200)] 
tests: json_echo: Print errors to stderr

Apart from the obvious, this fixes exit_dump() which tried to dump the
wrong variable ('out' instead of 'obj') and missed that json.dumps()
doesn't print but just returns a string. Make it call exit_err() to
share some code, which changes the prefix from 'FAIL' to 'Error' as a
side-effect.

While being at it, fix for a syntax warning with newer Python in
unrelated code.

Fixes: bb32d8db9a125 ("JSON: Add support for echo option")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agosrc: Optimize prefix match only if is big-endian
Xiao Liang [Fri, 20 Aug 2021 16:12:37 +0000 (00:12 +0800)] 
src: Optimize prefix match only if is big-endian

A prefix of integer type is big-endian in nature. Prefix match can be
optimized to truncated 'cmp' only if it is big-endian.

[ Add one tests/py for this use-case --pablo ]

Fixes: 25338cdb6c77 ("src: Optimize prefix matches on byte-boundaries")
Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: shell: add nft-f/0022variables_0 dump file
Pablo Neira Ayuso [Fri, 20 Aug 2021 11:05:15 +0000 (13:05 +0200)] 
tests: shell: add nft-f/0022variables_0 dump file

Dump file was missing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: queue: consolidate queue statement syntax
Pablo Neira Ayuso [Fri, 20 Aug 2021 10:43:50 +0000 (12:43 +0200)] 
src: queue: consolidate queue statement syntax

Print queue statement using the 'queue ... to' syntax to consolidate the
syntax around Florian's proposal introduced in 6cf0f2c17bfb ("src:
queue: allow use of arbitrary queue expressions").

Retain backward compatibility, 'queue num' syntax is still allowed.

Update and add new tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser: permit symbolic define for 'queue num' again
Florian Westphal [Fri, 20 Aug 2021 09:52:35 +0000 (11:52 +0200)] 
parser: permit symbolic define for 'queue num' again

WHen I simplified the parser to restrict 'queue num' I forgot that
instead of range and immediate value its also allowed to pass in
a variable expression, e.g.

define myq = 0
add rule ... 'queue num $myq bypass'

Allow those as well and add a test case for this.

Fixes: 767f0af82a389 ("parser: restrict queue num expressiveness")
Reported-by: Amish <anon.amish@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agobuild: Bump version to v1.0.0 v1.0.0
Pablo Neira Ayuso [Tue, 17 Aug 2021 17:25:46 +0000 (19:25 +0200)] 
build: Bump version to v1.0.0

Update libversion since new API has been added in 9edaa6a51eab ("src:
add --define key=value").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink_delinearize: skip flags / mask notation for singleton bitmask again
Pablo Neira Ayuso [Sun, 15 Aug 2021 12:46:53 +0000 (14:46 +0200)] 
netlink_delinearize: skip flags / mask notation for singleton bitmask again

!= operation should also be covered too.

Fixes: 347a4aa16e64 ("netlink_delinearize: skip flags / mask notation for singleton bitmask")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: expand variable containing set into multiple mappings
Pablo Neira Ayuso [Wed, 11 Aug 2021 10:08:41 +0000 (12:08 +0200)] 
evaluate: expand variable containing set into multiple mappings

 # cat x.nft
 define interfaces = { eth0, eth1 }

 table ip x {
        chain y {
type filter hook input priority 0; policy accept;
                iifname vmap { lo : accept, $interfaces : drop }
        }
 }
 # nft -f x.nft
 # nft list ruleset
 table ip x {
        chain y {
type filter hook input priority 0; policy accept;
                iifname vmap { "lo" : accept, "eth0" : drop, "eth1" : drop }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests/py: Make netns spawning more robust
Phil Sutter [Tue, 10 Aug 2021 13:40:06 +0000 (15:40 +0200)] 
tests/py: Make netns spawning more robust

On systems without unshare Python module, try to call unshare binary
with oneself as parameters.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotcpopt: bogus assertion on undefined options
Pablo Neira Ayuso [Wed, 11 Aug 2021 03:24:48 +0000 (05:24 +0200)] 
tcpopt: bogus assertion on undefined options

 # nft add rule x y tcp option 6 exists
 # nft list ruleset
 nft: tcpopt.c:208: tcpopt_init_raw: Assertion `expr->exthdr.desc != NULL' failed.
 Aborted

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1557
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomnl: revisit hook listing
Pablo Neira Ayuso [Mon, 2 Aug 2021 10:32:52 +0000 (12:32 +0200)] 
mnl: revisit hook listing

Update this command to display the hook datapath for a packet depending
on its family.

This patch also includes:

- Group of existing hooks based on the hook location.
- Order hooks by priority, from INT_MIN to INT_MAX.
- Do not add sign to priority zero.
- Refresh include/linux/netfilter/nfnetlink_hook.h cache copy.
- Use NFNLA_CHAIN_* attributes to print the chain family, table and name.
  If NFNLA_CHAIN_* attributes are not available, display the hookfn name.
- Update syntax: remove optional hook parameter, promote the 'device'
  argument.

The following example shows the hook datapath for IPv4 packets coming in
from netdevice 'eth0':

 # nft list hooks ip device eth0
 family ip {
        hook ingress {
                +0000000010 chain netdev x y [nf_tables]
                +0000000300 chain inet m w  [nf_tables]
        }
        hook input {
                -0000000100 chain ip a b [nf_tables]
                +0000000300 chain inet m z [nf_tables]
        }
        hook forward {
                -0000000225 selinux_ipv4_forward
                 0000000000 chain ip a c [nf_tables]
        }
        hook output {
                -0000000225 selinux_ipv4_output
        }
        hook postrouting {
                +0000000225 selinux_ipv4_postroute
        }
 }

Note that the listing above includes the existing netdev and inet
hooks/chains which *might* interfer in the travel of an incoming IPv4
packet. This allows users to debug the pipeline, basically, to
understand in what order the hooks/chains are evaluated for the IPv4
packets.

If the netdevice is not specified, then the ingress hooks are not
shown.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: check more flag match transformations to compact syntax
Pablo Neira Ayuso [Wed, 28 Jul 2021 09:04:50 +0000 (11:04 +0200)] 
tests: py: check more flag match transformations to compact syntax

Add a few more tests to extend coverage.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: tcp flags & (fin | syn | rst | ack) == syn
Pablo Neira Ayuso [Tue, 27 Jul 2021 21:18:16 +0000 (23:18 +0200)] 
tests: py: tcp flags & (fin | syn | rst | ack) == syn

Add a test case to cover translation to tcp flags syn / fin,syn,rst,ack.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink_delinearize: skip flags / mask notation for singleton bitmask
Pablo Neira Ayuso [Tue, 27 Jul 2021 20:26:52 +0000 (22:26 +0200)] 
netlink_delinearize: skip flags / mask notation for singleton bitmask

Do not transform 'tcp flags & flag == flag' to 'flag / flag'.
The parser does not accept this notation yet.

Fixes: c3d57114f119 ("parser_bison: add shortcut syntax for matching flags without binary operations")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: idempotent tcp flags & syn != 0 to tcp flag syn
Pablo Neira Ayuso [Tue, 27 Jul 2021 20:21:48 +0000 (22:21 +0200)] 
tests: py: idempotent tcp flags & syn != 0 to tcp flag syn

Add a test to cover this case.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: disallow negation with binary operation
Pablo Neira Ayuso [Tue, 27 Jul 2021 15:23:34 +0000 (17:23 +0200)] 
evaluate: disallow negation with binary operation

The negation was introduced to provide a simple shortcut. Extend
e6c32b2fa0b8 ("src: add negation match on singleton bitmask value") to
disallow negation with binary operations too.

 # nft add rule meh tcp_flags 'tcp flags & (fin | syn | rst | ack) ! syn'
 Error: cannot combine negation with binary expression
 add rule meh tcp_flags tcp flags & (fin | syn | rst | ack) ! syn
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ~~~

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink_linearize: incorrect netlink bytecode with binary operation and flags
Pablo Neira Ayuso [Tue, 27 Jul 2021 15:23:30 +0000 (17:23 +0200)] 
netlink_linearize: incorrect netlink bytecode with binary operation and flags

nft generates incorrect bytecode when combining flag datatype and binary
operations:

  # nft --debug=netlink add rule meh tcp_flags 'tcp flags & (fin | syn | rst | ack) syn'
ip
  [ meta load l4proto => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ payload load 1b @ transport header + 13 => reg 1 ]
  [ bitwise reg 1 = ( reg 1 & 0x00000017 ) ^ 0x00000000 ]
  [ bitwise reg 1 = ( reg 1 & 0x00000002 ) ^ 0x00000000 ]
  [ cmp neq reg 1 0x00000000 ]

Note the double bitwise expression. The last two expressions are not
correct either since it should match on the syn flag, ie. 0x2.

After this patch, netlink bytecode generation looks correct:

 # nft --debug=netlink add rule meh tcp_flags 'tcp flags & (fin | syn | rst | ack) syn'
ip
  [ meta load l4proto => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ payload load 1b @ transport header + 13 => reg 1 ]
  [ bitwise reg 1 = ( reg 1 & 0x00000017 ) ^ 0x00000000 ]
  [ cmp eq reg 1 0x00000002 ]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpression: missing != in flagcmp expression print function
Pablo Neira Ayuso [Tue, 27 Jul 2021 15:23:27 +0000 (17:23 +0200)] 
expression: missing != in flagcmp expression print function

Missing != when printing the expression.

Fixes: c3d57114f119 ("parser_bison: add shortcut syntax for matching flags without binary operations")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: missing json update for numeric reject with icmp numeric
Pablo Neira Ayuso [Mon, 26 Jul 2021 22:32:04 +0000 (00:32 +0200)] 
tests: py: missing json update for numeric reject with icmp numeric

Add missing tests to cover json support for reject with icmp numeric.

Fixes: 1ab1fcbc19a8 ("parser_bison: parse number as reject icmp code")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: update new reject with icmp code syntax leftover
Pablo Neira Ayuso [Mon, 26 Jul 2021 22:26:43 +0000 (00:26 +0200)] 
tests: py: update new reject with icmp code syntax leftover

I forgot to update a few more spots in the json files.

Fixes: 08d2f0493671 ("src: promote 'reject with icmp CODE' syntax")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: error reporting for missing statements in set/map declaration
Pablo Neira Ayuso [Mon, 26 Jul 2021 16:00:09 +0000 (18:00 +0200)] 
evaluate: error reporting for missing statements in set/map declaration

Assuming this map:

        map y {
                type ipv4_addr : verdict
        }

This patch slightly improves error reporting to refer to the missing
'counter' statement in the map declaration.

 # nft 'add element x y { 1.2.3.4 counter packets 1 bytes 1 : accept, * counter : drop }'
 Error: missing statement in map declaration
 add element x y { 1.2.3.4 counter packets 10 bytes 640 : accept, * counter : drop }
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: promote 'reject with icmp CODE' syntax
Pablo Neira Ayuso [Mon, 26 Jul 2021 15:22:32 +0000 (17:22 +0200)] 
src: promote 'reject with icmp CODE' syntax

The kernel already assumes that that ICMP type to reject a packet is
destination-unreachable, hence the user specifies the *ICMP code*.

Simplify the syntax to:

... reject with icmp port-unreachable

this removes the 'type' keyword before the ICMP code to reject the
packet with.

IIRC, the original intention is to leave room for future extensions that
allow to specify both the ICMP type and the ICMP code, this is however
not possible with the current inconsistent syntax.

Update manpages which also refer to ICMP type.

Adjust tests/py to the new syntax.

Fixes: 5fdd0b6a0600 ("nft: complete reject support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: parse number as reject icmp code
Pablo Neira Ayuso [Mon, 26 Jul 2021 14:29:58 +0000 (16:29 +0200)] 
parser_bison: parse number as reject icmp code

Extend parser to accept a numeric icmp code, instead of bailing out:

 # nft add rule inet filter input reject with icmpx type 3
 Error: syntax error, unexpected number, expecting string
 add rule inet filter input reject with icmpx type 3
                                                   ^

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1555
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: stateful statement support in map
Pablo Neira Ayuso [Mon, 26 Jul 2021 10:00:07 +0000 (12:00 +0200)] 
parser_bison: stateful statement support in map

Missing parser extension to support for stateful statements in map.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: Fix bogus testsuite failure with 100Hz
Phil Sutter [Mon, 26 Jul 2021 13:27:32 +0000 (15:27 +0200)] 
tests: shell: Fix bogus testsuite failure with 100Hz

On kernels with CONFIG_HZ=100, clock granularity does not allow tracking
timeouts in single digit ms range. Change sets/0031set_timeout_size_0 to
not expose this detail.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
4 years agosrc: expose nft_ctx_clear_vars as API
Pablo Neira Ayuso [Sat, 24 Jul 2021 10:07:27 +0000 (12:07 +0200)] 
src: expose nft_ctx_clear_vars as API

This function might be useful to recycle the existing nft_ctx to use it
with different external variable definitions.

Moreover, reset ctx->num_vars to zero.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: fix nft_ctx_clear_include_paths in libnftables.map
Pablo Neira Ayuso [Sat, 24 Jul 2021 10:04:57 +0000 (12:04 +0200)] 
src: fix nft_ctx_clear_include_paths in libnftables.map

There a typo that prevents exposing the function as API.

Fixes: 16543a0136c0 ("libnftables: export public symbols only")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_json: inconditionally initialize ct timeout list
Pablo Neira Ayuso [Thu, 22 Jul 2021 16:28:14 +0000 (18:28 +0200)] 
parser_json: inconditionally initialize ct timeout list

The policy is optional, make sure this timeout list is initialized.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>