]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
4 years agosrc: allow use of 'verdict' in typeof definitions
Florian Westphal [Sat, 30 Jan 2021 18:58:42 +0000 (19:58 +0100)] 
src: allow use of 'verdict' in typeof definitions

'verdict' cannot be used as part of a map typeof-based key definition,
its a datatype and not an expression, e.g.:

  typeof iifname . ip protocol . th dport : verdic

... will fail.

Make the parser convert a 'verdict' symbol to a verdict expression
and allow to store its presence as part of the typeof key definition.

Reported-by: Frank Myhr <fmyhr@fhmtech.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agodoc: nft: fix some typos and formatting issues
Štěpán Němec [Mon, 22 Feb 2021 12:03:20 +0000 (13:03 +0100)] 
doc: nft: fix some typos and formatting issues

Trying to escape asciidoc (9.1.0) * with \ preserves the backslash in
the formatted man page. Bare * works as expected.

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomain: fix nft --help output fallout from 719e4427
Štěpán Němec [Mon, 22 Feb 2021 12:03:19 +0000 (13:03 +0100)] 
main: fix nft --help output fallout from 719e4427

Long options were missing the double dash.

Fixes: 719e44277f8e ("main: use one data-structure to initialize getopt_long(3) arguments and help.")
Cc: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: init parser state for every new buffer/file
Eric Garver [Fri, 19 Feb 2021 15:11:26 +0000 (10:11 -0500)] 
json: init parser state for every new buffer/file

Otherwise invalid error states cause subsequent json parsing to fail
when it should not.

Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agomonitor: Don't print newgen message with JSON output
Phil Sutter [Wed, 17 Feb 2021 11:38:42 +0000 (12:38 +0100)] 
monitor: Don't print newgen message with JSON output

Iff this should be printed, it must adhere to output format settings. In
its current form it breaks JSON syntax, so skip it for non-default
output formats.

Fixes: cb7e02f44d6a6 ("src: enable json echo output when reading native syntax")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoevaluate: set evaluation context for set elements
Florian Westphal [Wed, 3 Feb 2021 16:57:07 +0000 (17:57 +0100)] 
evaluate: set evaluation context for set elements

This resolves same issue as previous patch when such
expression is used as a set key:

        set z {
                typeof ct zone
-               elements = { 1, 512, 768, 1024, 1280, 1536 }
+               elements = { 1, 2, 3, 4, 5, 6 }
        }

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoevaluate: pick data element byte order, not dtype one
Florian Westphal [Wed, 3 Feb 2021 16:57:06 +0000 (17:57 +0100)] 
evaluate: pick data element byte order, not dtype one

Some expressions have integer base type, not a specific one, e.g. 'ct zone'.
In that case nft used the wrong byte order.

Without this, nft adds
elements = { "eth0" : 256, "eth1" : 512, "veth4" : 256 }
instead of 1, 2, 3.

This is not a 'display bug', the added elements have wrong byte order.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: extend dtype test case to cover expression with integer type
Florian Westphal [Wed, 3 Feb 2021 16:57:05 +0000 (17:57 +0100)] 
tests: extend dtype test case to cover expression with integer type

... nft doesn't handle this correctly at the moment: they are added
as network byte order (invalid byte order).

ct zone has integer_type, the byte order has to be taken from the expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoevaluate: incorrect usage of stmt_binary_error() in reject
Pablo Neira Ayuso [Tue, 9 Feb 2021 13:22:12 +0000 (14:22 +0100)] 
evaluate: incorrect usage of stmt_binary_error() in reject

Don't pass ctx->pctx.protocol[PROTO_BASE_LL_HDR] to stmt_binary_error(),
it's not useful for the error reporting as location is not available.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoerec: Sanitize erec location indesc
Phil Sutter [Tue, 26 Jan 2021 17:52:15 +0000 (18:52 +0100)] 
erec: Sanitize erec location indesc

erec_print() unconditionally dereferences erec->locations->indesc, so
make sure it is valid when either creating an erec or adding a location.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: shell: extend 0025empty_dynset_0 to cover multi-statement support
Pablo Neira Ayuso [Tue, 9 Feb 2021 11:57:14 +0000 (12:57 +0100)] 
tests: shell: extend 0025empty_dynset_0 to cover multi-statement support

Add a test to cover multi-statement support.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotrace: do not remove icmp type from packet dump
Florian Westphal [Mon, 8 Feb 2021 14:54:44 +0000 (15:54 +0100)] 
trace: do not remove icmp type from packet dump

As of 0.9.8 the icmp type is marked as a protocol field, so its
elided in 'nft monitor trace' output:

   icmp code 0 icmp id 44380 ..

Restore it.  Unlike tcp, where 'tcp sport' et. al in the dump
will make the 'ip protocol tcp' redundant this case isn't obvious
in the icmp case:

  icmp type 8 code 0 id ...

Reported-by: Martin Gignac <martin.gignac@gmail.com>
Fixes: 98b871512c4677 ("src: add auto-dependencies for ipv4 icmp")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add negation match on singleton bitmask value
Pablo Neira Ayuso [Mon, 1 Feb 2021 21:21:41 +0000 (22:21 +0100)] 
src: add negation match on singleton bitmask value

This patch provides a shortcut for:

ct status and dnat == 0

which allows to check for the packet whose dnat bit is unset:

  # nft add rule x y ct status ! dnat counter

This operation is only available for expression with a bitmask basetype, eg.

  # nft describe ct status
  ct expression, datatype ct_status (conntrack status) (basetype bitmask, integer), 32 bits

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: do not crash if dynamic set has no statements
Florian Westphal [Wed, 3 Feb 2021 18:42:27 +0000 (19:42 +0100)] 
evaluate: do not crash if dynamic set has no statements

list_first_entry() returns garbage when the list is empty.
There is no need to run the following loop if we have no statements,
so just return 0.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: add empty dynamic set
Florian Westphal [Wed, 3 Feb 2021 18:42:26 +0000 (19:42 +0100)] 
tests: add empty dynamic set

nft crashes on restore.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotestcases: move two dump files to correct location
Florian Westphal [Wed, 3 Feb 2021 18:42:25 +0000 (19:42 +0100)] 
testcases: move two dump files to correct location

The test cases were moved but the dumps remained in the old location.

Fixes: eb14363d44cea5 ("tests: shell: move chain priority and policy to chain folder")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests/py: Add a test sanitizer and fix its findings
Phil Sutter [Tue, 29 Dec 2020 18:33:44 +0000 (19:33 +0100)] 
tests/py: Add a test sanitizer and fix its findings

This is just basic housekeeping:

- Remove duplicate tests in any of the *.t files
- Remove explicit output if equal to command itself in *.t files
- Remove duplicate payload records in any of the *.t.payload* files
- Remove stale payload records (for which no commands exist in the
  respective *.t file
- Remove duplicate/stale entries in any of the *.t.json files

In some cases, tests were added instead of removing a stale payload
record if it fit nicely into the sequence of tests.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests/py: Write dissenting payload into the right file
Phil Sutter [Tue, 15 Dec 2020 12:52:47 +0000 (13:52 +0100)] 
tests/py: Write dissenting payload into the right file

The testsuite supports diverging payloads depending on table family.
This is necessary since for some families, dependency matches are
created.
If a payload mismatch happens, record it into a "got"-file which matches
the family-specific payload file, not the common one. This eases use of
diff-tools a lot as the extra other families' payloads confuse the
tools.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agojson: Do not abbreviate reject statement object
Phil Sutter [Tue, 2 Feb 2021 18:47:46 +0000 (19:47 +0100)] 
json: Do not abbreviate reject statement object

No need to reduce output size, also this way output is more predictable.

While being at it, drop some pointless chunks from
tests/py/bridge/reject.t.json.output.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agopayload: check icmp dependency before removing previous icmp expression
Florian Westphal [Mon, 1 Feb 2021 21:08:54 +0000 (22:08 +0100)] 
payload: check icmp dependency before removing previous icmp expression

nft is too greedy when removing icmp dependencies.
'icmp code 1 type 2' did remove the type when printing.

Be more careful and check that the icmp type dependency of the
candidate expression (earlier icmp payload expression) has the same
type dependency as the new expression.

Reported-by: Eric Garver <eric@garver.life>
Reported-by: Michael Biebl <biebl@debian.org>
Tested-by: Eric Garver <eric@garver.life>
Fixes: d0f3b9eaab8d77e ("payload: auto-remove simple icmp/icmpv6 dependency expressions")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: add icmp/6 test where dependency should be left alone
Florian Westphal [Mon, 1 Feb 2021 21:44:25 +0000 (22:44 +0100)] 
tests: add icmp/6 test where dependency should be left alone

These tests fail: nft should leave the type as-is.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: limit: Always include burst value
Phil Sutter [Mon, 25 Jan 2021 13:16:35 +0000 (14:16 +0100)] 
json: limit: Always include burst value

The default burst value is non-zero, so JSON output should include it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoreject: Unify inet, netdev and bridge delinearization
Phil Sutter [Tue, 26 Jan 2021 17:37:12 +0000 (18:37 +0100)] 
reject: Unify inet, netdev and bridge delinearization

Postprocessing for inet family did not attempt to kill any existing
payload dependency, although it is perfectly fine to do so. The mere
culprit is to not abbreviate default code rejects as that would drop
needed protocol info as a side-effect. Since postprocessing is then
almost identical to that of bridge and netdev families, merge them.

While being at it, extend tests/py/netdev/reject.t by a few more tests
taken from inet/reject.t so this covers icmpx rejects as well.

Cc: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoreject: Fix for missing dependencies in netdev family
Phil Sutter [Tue, 26 Jan 2021 16:06:33 +0000 (17:06 +0100)] 
reject: Fix for missing dependencies in netdev family

Like with bridge family, rejecting with either icmp or icmpv6 must
create a dependency match on meta protocol. Upon delinearization, treat
netdev reject identical to bridge as well so no family info is lost.

This makes reject statement in netdev family fully symmetric so fix
the tests in tests/py/netdev/reject.t, adjust the related payload dumps
and add JSON equivalents which were missing altogether.

Fixes: 0c42a1f2a0cc5 ("evaluate: add netdev support for reject default")
Fixes: a51a0bec1f698 ("tests: py: add netdev folder and reject.t icmp cases")
Cc: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: monitor: use correct $nft value in EXIT trap
Štěpán Němec [Wed, 27 Jan 2021 14:02:03 +0000 (15:02 +0100)] 
tests: monitor: use correct $nft value in EXIT trap

With double quotes, $nft was being expanded to the default value even
in presence of the -H option.

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Helped-by: Tomáš Doležal <todoleza@redhat.com>
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agosrc: evaluate: reset context maxlen value before prio evaluation
Florian Westphal [Tue, 26 Jan 2021 15:42:33 +0000 (16:42 +0100)] 
src: evaluate: reset context maxlen value before prio evaluation

unshare -n tests/shell/run-tests.sh tests/shell/testcases/nft-f/0024priority_0
W: [FAILED]     tests/shell/testcases/nft-f/0024priority_0: got 1
/dev/stdin:8:47-49: Error: Value 100 exceeds valid range 0-15
        type filter hook postrouting priority 100

Reported-by: Andreas Schultz <andreas.schultz@travelping.com
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoexthdr: remove tcp dependency for tcp option matching
Florian Westphal [Thu, 21 Jan 2021 15:02:47 +0000 (16:02 +0100)] 
exthdr: remove tcp dependency for tcp option matching

Kernel won't search for tcp options in non-tcp packets.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: ct: add missing test input
Florian Westphal [Tue, 26 Jan 2021 14:45:47 +0000 (15:45 +0100)] 
json: ct: add missing test input

ERROR: did not find JSON equivalent for rule 'meta mark set ct original ip saddr . meta mark map { 1.1.1.1 . 0x00000014 : 0x0000001e }'
ERROR: did not find JSON equivalent for rule 'ct original ip saddr . meta mark { 1.1.1.1 . 0x00000014 }'

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: icmp: move expected parts to json.output
Florian Westphal [Thu, 21 Jan 2021 15:46:27 +0000 (16:46 +0100)] 
json: icmp: move expected parts to json.output

Phil Sutter says:
In general, *.t.json files should contain JSON equivalents for rules as
they are *input* into nft. So we want them to be as close to the
introductory standard syntax comment as possible.

Undo earlier change and place the expected dependency added by
nft internals to json.output rather than icmp.t.json.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoevaluate: disallow ct original {s,d}ddr from concatenations
Pablo Neira Ayuso [Thu, 21 Jan 2021 15:41:35 +0000 (16:41 +0100)] 
evaluate: disallow ct original {s,d}ddr from concatenations

Extend 8b043938e77b ("evaluate: disallow ct original {s,d}ddr from
maps") to cover concatenations too.

Error: specify either ip or ip6 for address matching
add rule x y meta mark set ct original saddr . meta mark map { 1.1.1.1 . 20 : 30 }
                           ^^^^^^^^^^^^^^^^^

The old syntax for ct original saddr without either ip or ip6 results
in unknown key size, which breaks the listing. The old syntax is only
allowed in simple rules for backward compatibility.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1489
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: icmp: refresh json output
Florian Westphal [Thu, 21 Jan 2021 13:51:27 +0000 (14:51 +0100)] 
json: icmp: refresh json output

nft inserts dependencies for icmp header types, but I forgot to
update the json test files to reflect this change.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: ct: add missing rule
Florian Westphal [Thu, 21 Jan 2021 13:51:05 +0000 (14:51 +0100)] 
json: ct: add missing rule

ERROR: did not find JSON equivalent for rule 'meta mark set ct original ip daddr map { 1.1.1.1 : 0x00000011 }'

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: limit: set default burst to 5
Florian Westphal [Thu, 21 Jan 2021 13:43:52 +0000 (14:43 +0100)] 
json: limit: set default burst to 5

The tests fail because json printing omits a burst of 5 and
the parser treats that as 'burst 0'.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: fix icmpv6.t test cases
Florian Westphal [Thu, 21 Jan 2021 12:55:27 +0000 (13:55 +0100)] 
json: fix icmpv6.t test cases

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agobuild: Bump version to v0.9.8 v0.9.8
Pablo Neira Ayuso [Wed, 13 Jan 2021 15:35:32 +0000 (16:35 +0100)] 
build: Bump version to v0.9.8

Update release name based on the Fearless Fosdick series: E.D.S.

E.D.S. is the robotic "Electronic Detective Substitute" appearing in the
"Hole Story".

Bump dependencies on libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: disallow ct original {s,d}ddr from maps
Pablo Neira Ayuso [Fri, 15 Jan 2021 17:40:11 +0000 (18:40 +0100)] 
evaluate: disallow ct original {s,d}ddr from maps

test.nft:6:55-71: Error: specify either ip or ip6 for address matching
add rule ip mangle manout ct direction reply mark set ct original daddr map { $ext1_ip : 0x11, $ext2_ip : 0x12 }
                                                      ^^^^^^^^^^^^^^^^^

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1489
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosegtree: honor set element expiration
Pablo Neira Ayuso [Wed, 6 Jan 2021 13:58:36 +0000 (14:58 +0100)] 
segtree: honor set element expiration

Extend c1f0476fd590 ("segtree: copy expr data to closing element") to
use interval_expr_copy() from the linearization path.

Reported-by: Mike Dillinger <miked@softtalker.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoinclude: resync nf_tables.h cache copy
Pablo Neira Ayuso [Wed, 6 Jan 2021 13:22:12 +0000 (14:22 +0100)] 
include: resync nf_tables.h cache copy

Get this header in sync with nf-next as of 5.11-rc.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomain: fix typo in cli definition
Pablo Neira Ayuso [Wed, 6 Jan 2021 11:43:11 +0000 (12:43 +0100)] 
main: fix typo in cli definition

9420423900a2 ("cli: add libedit support") updated HAVE_LIBREADLINE to
HAVE_READLINE by mistake.

Fixes: 9420423900a2 ("cli: add libedit support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocli: use plain readline() interface with libedit
Pablo Neira Ayuso [Wed, 6 Jan 2021 11:28:01 +0000 (12:28 +0100)] 
cli: use plain readline() interface with libedit

Instead of the alternate interface [1].

I spent a bit of time debugging an issue with libedit support
9420423900a2 ("cli: add libedit support") that broke tests/shell.

This is the reproducer:

 # nft -i << EOF
 list ruleset
 EOF

which makes rl_callback_read_char() loop forever on read() as shown by
strace. The rl_line_buffer variable does not accumulate the typed
characters as it should when redirecting the standard input for some
reason.

Given our interactive interface is fairly simple at this stage, switch
to use the readline() interface instead of rl_callback_read_char().

[1] https://docs.freebsd.org/info/readline/readline.info.Alternate_Interface.html

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocli: add libedit support
Pablo Neira Ayuso [Mon, 4 Jan 2021 20:24:51 +0000 (21:24 +0100)] 
cli: add libedit support

Extend cli to support for libedit readline shim code:

./configure --with-cli=editline

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: set on flags to request multi-statement support
Pablo Neira Ayuso [Mon, 4 Jan 2021 20:24:47 +0000 (21:24 +0100)] 
src: set on flags to request multi-statement support

Old kernel reject requests for element with multiple statements because
userspace sets on the flags for multi-statements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: set element multi-statement support
Pablo Neira Ayuso [Fri, 18 Dec 2020 10:13:57 +0000 (11:13 +0100)] 
tests: shell: set element multi-statement support

This patch adds two tests to add multistatement support:

- Dynamic set updates from packet path.
- Set that is updated from the control plane.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: disallow burst 0 in ratelimits
Pablo Neira Ayuso [Thu, 17 Dec 2020 11:36:38 +0000 (12:36 +0100)] 
src: disallow burst 0 in ratelimits

The ratelimiter in nftables is similar to the one in iptables, and
iptables disallows a zero burst.

Update the byte rate limiter not to print burst 5 (default value).

Update tests/py payloads to print burst 5 instead of zero when the
burst is unspecified.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: add set element multi-statement support
Pablo Neira Ayuso [Wed, 16 Dec 2020 15:39:09 +0000 (16:39 +0100)] 
src: add set element multi-statement support

Extend the set element infrastructure to support for several statements.

This patch places the statements right after the key when printing it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: add support for multi-statement in dynamic sets and maps
Pablo Neira Ayuso [Wed, 9 Dec 2020 15:46:26 +0000 (16:46 +0100)] 
src: add support for multi-statement in dynamic sets and maps

This patch allows for two statements for dynamic set updates, e.g.

 nft rule x y add @y { ip daddr limit rate 1/second counter }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agobuild: search for python3
Pablo Neira Ayuso [Tue, 15 Dec 2020 19:09:27 +0000 (20:09 +0100)] 
build: search for python3

Eric Garver says: "It would probably be better to use the automake macro
AM_PATH_PYTHON. [...] The above is fine for now."

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: don't leave dangling pointers on hlist
Florian Westphal [Mon, 14 Dec 2020 15:53:29 +0000 (16:53 +0100)] 
json: don't leave dangling pointers on hlist

unshare -n tests/json_echo/run-test.py
[..]
Adding chain c
free(): double free detected in tcache 2
Aborted (core dumped)

The element must be deleted from the hlist prior to freeing it.

Fixes: 389a0e1edc89a ("json: echo: Speedup seqnum_to_json()")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: py: Fix for changed concatenated ranges output
Phil Sutter [Mon, 14 Dec 2020 17:50:36 +0000 (18:50 +0100)] 
tests: py: Fix for changed concatenated ranges output

Payload didn't change but libnftnl was fixed to print the key_end data
reg of concat-range elements, too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: trace: print packet unconditionally
Florian Westphal [Sat, 12 Dec 2020 18:33:09 +0000 (19:33 +0100)] 
nft: trace: print packet unconditionally

The kernel includes the packet dump once for each base hook.
This means that in case a table contained no matching rule(s),
the packet dump will be included in the base policy dump.

Simply move the packet dump request out of the switch statement
so the debug output shows current packet even with no matched rule.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: icmp, icmpv6: check we don't add second dependency
Florian Westphal [Wed, 9 Dec 2020 17:43:16 +0000 (18:43 +0100)] 
tests: icmp, icmpv6: check we don't add second dependency

If dependency is already fulfilled, do not add another one.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: ip: add one test case to cover both id and sequence
Florian Westphal [Wed, 9 Dec 2020 17:31:41 +0000 (18:31 +0100)] 
tests: ip: add one test case to cover both id and sequence

These are two 2-byte matches, so nft will merge the accesses to
a single 4-byte load+compare.

Check this is properly demangled.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: icmp, icmpv6: avoid remaining warnings
Florian Westphal [Wed, 9 Dec 2020 17:27:55 +0000 (18:27 +0100)] 
tests: icmp, icmpv6: avoid remaining warnings

In case of id/sequence, both 'reply' and 'request' are valid types.

nft currently does not remove dependencies that don't have
a fixed rhs constant.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agopayload: auto-remove simple icmp/icmpv6 dependency expressions
Florian Westphal [Tue, 8 Dec 2020 22:51:59 +0000 (23:51 +0100)] 
payload: auto-remove simple icmp/icmpv6 dependency expressions

Instead of:
icmpv6 type packet-too-big icmpv6 mtu 1280
display just
icmpv6 mtu 1280

The dependency added for id/sequence is still kept, its handled
by a anon set instead to cover both the echo 'request' and 'reply' cases.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: fix exepcted payload of icmpv6 expressions
Florian Westphal [Tue, 8 Dec 2020 21:55:52 +0000 (22:55 +0100)] 
tests: fix exepcted payload of icmpv6 expressions

nft will now auto-insert a icmpv6 type match.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add auto-dependencies for ipv6 icmp6
Florian Westphal [Tue, 8 Dec 2020 16:36:23 +0000 (17:36 +0100)] 
src: add auto-dependencies for ipv6 icmp6

Extend the earlier commit to also cover icmpv6.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: fix exepcted payload of icmp expressions
Florian Westphal [Tue, 8 Dec 2020 15:05:00 +0000 (16:05 +0100)] 
tests: fix exepcted payload of icmp expressions

after previous change nft will insert explicit icmp type match.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add auto-dependencies for ipv4 icmp
Florian Westphal [Tue, 8 Dec 2020 14:49:42 +0000 (15:49 +0100)] 
src: add auto-dependencies for ipv4 icmp

The ICMP header has field values that are only exist
for certain types.

Mark the icmp proto 'type' field as a nextheader field
and add a new th description to store the icmp type
dependency.  This can later be re-used for other protocol
dependend definitions such as mptcp options -- which are all share the
same tcp option number and have a special 4 bit marker inside the
mptcp option space that tells how the remaining option looks like.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoproto: reduce size of proto_desc structure
Florian Westphal [Mon, 7 Dec 2020 11:36:53 +0000 (12:36 +0100)] 
proto: reduce size of proto_desc structure

This will need an additional field. We can compress state
here to avoid further size increase.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoexthdr: remove unused proto_key member from struct
Florian Westphal [Sun, 22 Nov 2020 23:30:17 +0000 (00:30 +0100)] 
exthdr: remove unused proto_key member from struct

also, no need for this struct to be in the parser.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: shell: timeouts later than 23 days
Pablo Neira Ayuso [Tue, 8 Dec 2020 19:29:23 +0000 (20:29 +0100)] 
tests: shell: timeouts later than 23 days

Test timeout later than 23 days in set definitions and dynamic set
insertions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: double close_scope() call for implicit chains
Pablo Neira Ayuso [Tue, 8 Dec 2020 17:06:03 +0000 (18:06 +0100)] 
parser_bison: double close_scope() call for implicit chains

Call close_scope() from chain_block_alloc only.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1485
Fixes: c330152b7f77 ("src: support for implicit chain bindings")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: Document 'dccp type' match
Phil Sutter [Wed, 2 Dec 2020 17:58:01 +0000 (18:58 +0100)] 
doc: Document 'dccp type' match

Add a description of dccp_pkttype and extend DCCP header expression
synopsis by the 'type' argument.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agomonitor: fix formatting of if statements
Jose M. Guisado Gomez [Sun, 6 Dec 2020 10:12:34 +0000 (11:12 +0100)] 
monitor: fix formatting of if statements

Replace some "if(" introduced in cb7e02f4 by "if ("

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomonitor: add assignment check for json_echo
Jose M. Guisado Gomez [Sun, 6 Dec 2020 10:12:33 +0000 (11:12 +0100)] 
monitor: add assignment check for json_echo

When --echo and --json is specified and native syntax is read, only the
last instruction is printed. This happens because the reference to the
json_echo is reassigned each time netlink_echo_callback is executed for
an instruction to be echoed.

Add an assignment check for json_echo to avoid reassigning it.

Fixes: cb7e02f44d6a (src: enable json echo output when reading native syntax)
Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: report EPERM for non-root users
Pablo Neira Ayuso [Thu, 3 Dec 2020 12:33:46 +0000 (13:33 +0100)] 
src: report EPERM for non-root users

$ /usr/sbin/nft list ruleset
Operation not permitted (you must be root)

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1372
Acked-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomnl: reply netlink error message might be larger than MNL_SOCKET_BUFFER_SIZE
Pablo Neira Ayuso [Wed, 2 Dec 2020 22:20:40 +0000 (23:20 +0100)] 
mnl: reply netlink error message might be larger than MNL_SOCKET_BUFFER_SIZE

Netlink attribute maximum size is 65536 bytes (given nla_len is
16-bits). NFTA_SET_ELEM_LIST_ELEMENTS stores as many set elements as
possible that can fit into this netlink attribute.

Netlink messages with NLMSG_ERROR type originating from the kernel
contain the original netlink message as payload, they might be larger
than 65536 bytes.

Add NFT_MNL_ACK_MAXSIZE which estimates the maximum Netlink header
coming as (error) reply from the kernel. This estimate is based on the
maximum netlink message size that nft sends from userspace.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1464
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: allow to restore limit from dynamic set
Pablo Neira Ayuso [Wed, 2 Dec 2020 17:31:00 +0000 (18:31 +0100)] 
parser_bison: allow to restore limit from dynamic set

Update parser to allow to restore limit per set element in dynamic set.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1477
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: Fix seqnum_to_json() functionality
Phil Sutter [Wed, 2 Dec 2020 22:07:11 +0000 (23:07 +0100)] 
json: Fix seqnum_to_json() functionality

Introduction of json_cmd_assoc_hash missed that by the time the hash
table insert happens, the struct cmd object's 'seqnum' field which is
used as key is not initialized yet. This doesn't happen until
nft_netlink() prepares the batch object which records the lowest seqnum.
Therefore push all json_cmd_assoc objects into a temporary list until
the first lookup happens. At this time, all referenced cmd objects have
their seqnum set and the list entries can be moved into the hash table
for fast lookups.

To expose such problems in the future, make json_events_cb() emit an
error message if the passed message has a handle but no assoc entry is
found for its seqnum.

Fixes: 389a0e1edc89a ("json: echo: Speedup seqnum_to_json()")
Cc: Derek Dai <daiderek@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agosrc: enable json echo output when reading native syntax
Jose M. Guisado Gomez [Tue, 4 Aug 2020 10:38:46 +0000 (12:38 +0200)] 
src: enable json echo output when reading native syntax

This patch fixes a bug in which nft did not print any output when
specifying --echo and --json and reading nft native syntax.

This patch respects behavior when input is json, in which the output
would be the identical input plus the handles.

Adds a json_echo member inside struct nft_ctx to build and store the json object
containing the json command objects, the object is built using a mock
monitor to reuse monitor json code. This json object is only used when
we are sure we have not read json from input.

[ added json_alloc_echo() to compile without json support --pablo ]

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1446
Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Tested-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: echo: Speedup seqnum_to_json()
Phil Sutter [Fri, 20 Nov 2020 19:01:59 +0000 (20:01 +0100)] 
json: echo: Speedup seqnum_to_json()

Derek Dai reports:
"If there are a lot of command in JSON node, seqnum_to_json() will slow
down application (eg: firewalld) dramatically since it iterate whole
command list every time."

He sent a patch implementing a lookup table, but we can do better: Speed
this up by introducing a hash table to store the struct json_cmd_assoc
objects in, taking their netlink sequence number as key.

Quickly tested restoring a ruleset containing about 19k rules:

| # time ./before/nft -jeaf large_ruleset.json >/dev/null
| 4.85user 0.47system 0:05.48elapsed 97%CPU (0avgtext+0avgdata 69732maxresident)k
| 0inputs+0outputs (15major+16937minor)pagefaults 0swaps

| # time ./after/nft -jeaf large_ruleset.json >/dev/null
| 0.18user 0.44system 0:00.70elapsed 89%CPU (0avgtext+0avgdata 68484maxresident)k
| 0inputs+0outputs (15major+16645minor)pagefaults 0swaps

Bugzilla: https://bugzilla.netfilter.org/show_bug.cgi?id=1479
Reported-by: Derek Dai <daiderek@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: py: update format of registers in bitwise payloads.
Jeremy Sowden [Sun, 15 Nov 2020 15:11:47 +0000 (15:11 +0000)] 
tests: py: update format of registers in bitwise payloads.

libnftnl has been changed to bring the format of registers in bitwise
dumps in line with those in other types of expression.  Update the
expected output of Python test-cases.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoproto: Fix ARP header field ordering
Phil Sutter [Tue, 10 Nov 2020 12:07:49 +0000 (13:07 +0100)] 
proto: Fix ARP header field ordering

In ARP header, destination ether address sits between source IP and
destination IP addresses. Enum arp_hdr_fields had this wrong, which
in turn caused wrong ordering of entries in proto_arp->templates. When
expanding a combined payload expression, code assumes that template
entries are ordered by header offset, therefore the destination ether
address match was printed as raw if an earlier field was matched as
well:

| arp saddr ip 192.168.1.1 arp daddr ether 3e:d1:3f:d6:12:0b

was printed as:

| arp saddr ip 192.168.1.1 @nh,144,48 69068440080907

Note: Although strictly not necessary, reorder fields in
proto_arp->templates as well to match their actual ordering, just to
avoid confusion.

Fixes: 4b0f2a712b579 ("src: support for arp sender and target ethernet and IPv4 addresses")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: py: remove duplicate payloads.
Jeremy Sowden [Mon, 9 Nov 2020 18:07:10 +0000 (18:07 +0000)] 
tests: py: remove duplicate payloads.

nft-test.py only needs one payload per rule, but a number of rules have
duplicates, typically one per address family, so just keep the last
payload for rules listed more than once.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: tcp: add raw tcp option match support
Florian Westphal [Tue, 3 Nov 2020 11:04:20 +0000 (12:04 +0100)] 
json: tcp: add raw tcp option match support

To similar change as in previous one, this time for the
jason (de)serialization.

Re-uses the raw payload match syntax, i.e. base,offset,length.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotcp: add raw tcp option match support
Florian Westphal [Mon, 2 Nov 2020 19:10:25 +0000 (20:10 +0100)] 
tcp: add raw tcp option match support

tcp option @42,16,4 (@kind,offset,length).

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotcpopt: allow to check for presence of any tcp option
Florian Westphal [Wed, 21 Oct 2020 21:54:17 +0000 (23:54 +0200)] 
tcpopt: allow to check for presence of any tcp option

nft currently doesn't allow to check for presence of arbitrary tcp options.
Only known options where nft provides a template can be tested for.

This allows to test for presence of raw protocol values as well.

Example:

tcp option 42 exists

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotcpopt: split tcpopt_hdr_fields into per-option enum
Florian Westphal [Mon, 2 Nov 2020 14:22:40 +0000 (15:22 +0100)] 
tcpopt: split tcpopt_hdr_fields into per-option enum

Currently we're limited to ten template fields in exthdr_desc struct.
Using a single enum for all tpc option fields thus won't work
indefinitely (TCPOPTHDR_FIELD_TSECR is 9) when new option templates get
added.

Fortunately we can just use one enum per tcp option to avoid this.
As a side effect this also allows to simplify the sack offset
calculations.  Rather than computing that on-the-fly, just add extra
fields to the SACK template.

expr->exthdr.offset now holds the 'raw' value, filled in from the option
template. This would ease implementation of 'raw option matching'
using offset and length to load from the option.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotcpopt: rename noop to nop
Florian Westphal [Mon, 2 Nov 2020 13:58:41 +0000 (14:58 +0100)] 
tcpopt: rename noop to nop

'nop' is the tcp padding "option". "noop" is retained for compatibility
on parser side.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotcpopts: clean up parser -> tcpopt.c plumbing
Florian Westphal [Mon, 2 Nov 2020 13:53:26 +0000 (14:53 +0100)] 
tcpopts: clean up parser -> tcpopt.c plumbing

tcpopt template mapping is asymmetric:
one mapping is to match dumped netlink exthdr expression to the original
tcp option template.

This struct is indexed by the raw, on-write kind/type number.

The other mapping maps parsed options to the tcp option template.
Remove the latter.  The parser is changed to translate the textual
option name, e.g. "maxseg" to the on-wire number.

This avoids the second mapping, it will also allow to more easily
support raw option matching in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: merge sack-perm/sack-permitted and maxseg/mss
Florian Westphal [Sun, 1 Nov 2020 23:27:04 +0000 (00:27 +0100)] 
parser: merge sack-perm/sack-permitted and maxseg/mss

One was added by the tcp option parsing ocde, the other by synproxy.

So we have:
synproxy ... sack-perm
synproxy ... mss

and

tcp option maxseg
tcp option sack-permitted

This kills the extra tokens on the scanner/parser side,
so sack-perm and sack-permitted can both be used.

Likewise, 'synproxy maxseg' and 'tcp option mss size 42' will work too.
On the output side, the shorter form is now preferred, i.e. sack-perm
and mss.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: fix ip6 dnat test case after range to prefix transformation change
Florian Westphal [Sat, 7 Nov 2020 13:26:34 +0000 (14:26 +0100)] 
json: fix ip6 dnat test case after range to prefix transformation change

Tests currently fail with
ip6/dnat.t: WARNING: line 8: ... because test still expects a range expression.

Fixes: ee4391d0ac1e7 ("nat: transform range to prefix expression when possible")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: add missing nat_type flag and netmap nat flag
Florian Westphal [Tue, 3 Nov 2020 13:32:12 +0000 (14:32 +0100)] 
json: add missing nat_type flag and netmap nat flag

JSON in/output doesn't know about nat_type and thus cannot save/restore
nat mappings involving prefixes or concatenations because the snat
statement lacks the prefix/concat/interval type flags.

Furthermore, bison parser was extended to support netmap.
This is done with an internal 'netmap' flag that is passed to the
kernel.  We need to dump/restore that as well.

Also make sure ip/snat.t passes in json mode.

Fixes: 35a6b10c1bc4 ("src: add netmap support")
Fixes: 9599d9d25a6b ("src: NAT support for intervals in maps")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: avoid warning and add missing json test cases
Florian Westphal [Tue, 3 Nov 2020 12:48:20 +0000 (13:48 +0100)] 
tests: avoid warning and add missing json test cases

make dnat.t pass in json mode.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: json: add missing test case output
Florian Westphal [Tue, 3 Nov 2020 10:12:50 +0000 (11:12 +0100)] 
tests: json: add missing test case output

Fix warnings and errors when running nf-test.py -j due to missing json test case updates.
This also makes bridge/reject.t pass in json mode.

No code changes.

Fixes: 8615ed93f6e4c4 ("evaluate: enable reject with 802.1q")
Fixes: fae0a0972d7a71 ("tests: py: Enable anonymous set rule with concatenated ranges in inet/sets.t")
Fixes: 2a20b5bdbde8a1 ("datatype: add frag-needed (ipv4) to reject options")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: Optimize prefix matches on byte-boundaries
Phil Sutter [Tue, 27 Oct 2020 16:33:15 +0000 (17:33 +0100)] 
src: Optimize prefix matches on byte-boundaries

If a prefix expression's length is on a byte-boundary, it is sufficient
to just reduce the length passed to "cmp" expression. No need for
explicit bitwise modification of data on LHS. The relevant code is
already there, used for string prefix matches. There is one exception
though, namely zero-length prefixes: Kernel doesn't accept zero-length
"cmp" expressions, so keep them in the old code-path for now.

This patch depends upon the previous one to correctly parse odd-sized
payload matches but has to extend support for non-payload LHS as well.
In practice, this is needed for "ct" expressions as they allow matching
against IP address prefixes, too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agosrc: Support odd-sized payload matches
Phil Sutter [Tue, 27 Oct 2020 16:05:25 +0000 (17:05 +0100)] 
src: Support odd-sized payload matches

When expanding a payload match, don't disregard oversized templates at
the right offset. A more flexible user may extract less bytes from the
packet if only parts of a field are interesting, e.g. only the prefix of
source/destination address. Support that by using the template, but fix
the length. Later when creating a relational expression for it, detect
the unusually small payload expression length and turn the RHS value
into a prefix expression.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: py: add netdev folder and reject.t icmp cases
Jose M. Guisado Gomez [Thu, 22 Oct 2020 19:43:55 +0000 (21:43 +0200)] 
tests: py: add netdev folder and reject.t icmp cases

Add unit tests for the use of reject with icmp inside netdev family.

reject.t from inet family couldn't be reused because it was using
meta nfproto which is not supported inside netdev.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: add netdev support for reject default
Jose M. Guisado Gomez [Thu, 22 Oct 2020 19:43:54 +0000 (21:43 +0200)] 
evaluate: add netdev support for reject default

Enables not specifying any icmp type and code when using reject inside
netdev.

This patch completely enables using reject for the netdev family.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: correct chain name in example of adding a rule
Jeremy Sowden [Sun, 1 Nov 2020 19:33:13 +0000 (19:33 +0000)] 
doc: correct chain name in example of adding a rule

The example adds a rule to the `output` chain, not the `input` chain.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests/shell: Improve fix in sets/0036add_set_element_expiration_0
Phil Sutter [Thu, 29 Oct 2020 12:35:55 +0000 (13:35 +0100)] 
tests/shell: Improve fix in sets/0036add_set_element_expiration_0

Explicitly eliminate the newgen message from output instead of just the
last line to make sure no other output is dropped by accident. This also
allows the test to pass in unpatched kernels which do not emit the
newgen message despite NLM_F_ECHO if no netlink listeners are present.

Fixes: 46b54fdcf266d ("Revert "monitor: do not print generation ID with --echo"")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: shell: exercise validation with nft -c
Pablo Neira Ayuso [Fri, 30 Oct 2020 19:36:22 +0000 (20:36 +0100)] 
tests: shell: exercise validation with nft -c

Using oif in fib from prerouting is not support, make sure -c reports an
error.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agobuild: Bump version to v0.9.7 v0.9.7
Pablo Neira Ayuso [Mon, 26 Oct 2020 13:40:40 +0000 (14:40 +0100)] 
build: Bump version to v0.9.7

Update release name based on the Fearless Fosdick series: Anyface.

Bump dependencies on libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoRevert "monitor: do not print generation ID with --echo"
Pablo Neira Ayuso [Thu, 22 Oct 2020 20:34:17 +0000 (22:34 +0200)] 
Revert "monitor: do not print generation ID with --echo"

Revert 0e258556f7f3 ("monitor: do not print generation ID with --echo").

There is actually a kernel bug which is preventing from displaying
this generation ID message.

Update the tests/shell to remove the last line of the --echo output
which displays the generation ID once the "netfilter: nftables: fix netlink
report logic in flowtable and genid" kernel fix is applied.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomonitor: do not print generation ID with --echo
Pablo Neira Ayuso [Tue, 20 Oct 2020 19:48:16 +0000 (21:48 +0200)] 
monitor: do not print generation ID with --echo

This fixes testcases/sets/0036add_set_element_expiration_0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: Fix memleak in set_dtype_json()
Phil Sutter [Thu, 8 Oct 2020 17:10:13 +0000 (19:10 +0200)] 
json: Fix memleak in set_dtype_json()

Turns out json_string() already dups the input, so the temporary dup
passed to it is lost.

Fixes: e70354f53e9f6 ("libnftables: Implement JSON output support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosegtree: UAF in interval_map_decompose()
Pablo Neira Ayuso [Tue, 20 Oct 2020 19:24:36 +0000 (21:24 +0200)] 
segtree: UAF in interval_map_decompose()

reported by tests/monitor# bash run-tests.sh
...
SUMMARY: AddressSanitizer: heap-use-after-free /home/pablo/devel/scm/git-netfilter/nftables/src/expression.c:1385 in expr_ops

Due to incorrect structure layout when calling interval_expr_copy().

Fixes: c1f0476fd590 ("segtree: copy expr data to closing element")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: improve rule error reporting
Pablo Neira Ayuso [Mon, 19 Oct 2020 12:46:22 +0000 (14:46 +0200)] 
src: improve rule error reporting

Kernel provides information regarding expression since
83d9dcba06c5 ("netfilter: nf_tables: extended netlink error reporting for
expressions").

A common mistake is to refer a chain which does not exist, e.g.

 # nft add rule x y jump test
 Error: Could not process rule: No such file or directory
 add rule x y jump test
                   ^^^^

Use the existing netlink extended error reporting infrastructure to
provide better error reporting as in the example above.

Requires Linux kernel patch 83d9dcba06c5 ("netfilter: nf_tables:
extended netlink error reporting for expressions").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: constify location parameter in cmd_add_loc()
Pablo Neira Ayuso [Mon, 19 Oct 2020 21:51:16 +0000 (23:51 +0200)] 
src: constify location parameter in cmd_add_loc()

Constify pointer to location object to compile check for unintentional
updates.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agorule: larger number of error locations
Pablo Neira Ayuso [Mon, 19 Oct 2020 12:45:48 +0000 (14:45 +0200)] 
rule: larger number of error locations

Statically store up to 32 locations per command, if the number of
locations is larger than 32, then skip rather than hit assertion.

Revisit this later to dynamically store location per command using a
hashtable.

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