]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
3 years agointervals: add support to automerge with kernel elements
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:17 +0000 (04:01 +0200)] 
intervals: add support to automerge with kernel elements

Extend the interval codebase to support for merging elements in the
kernel with userspace element updates.

Add a list of elements to be purged to cmd and set objects. These
elements representing outdated intervals are deleted before adding the
updated ranges.

This routine splices the list of userspace and kernel elements, then it
mergesorts to identify overlapping and contiguous ranges. This splice
operation is undone so the set userspace cache remains consistent.

Incrementally update the elements in the cache, this allows to remove
dd44081d91ce ("segtree: Fix add and delete of element in same batch").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agomnl: update mnl_nft_setelem_del() to allow for more reuse
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:16 +0000 (04:01 +0200)] 
mnl: update mnl_nft_setelem_del() to allow for more reuse

Pass handle and element list as parameters to allow for code reuse.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: remove rbtree datastructure
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:14 +0000 (04:01 +0200)] 
src: remove rbtree datastructure

Not used by anyone anymore, remove it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: replace interval segment tree overlap and automerge
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:13 +0000 (04:01 +0200)] 
src: replace interval segment tree overlap and automerge

This is a rewrite of the segtree interval codebase.

This patch now splits the original set_to_interval() function in three
routines:

- add set_automerge() to merge overlapping and contiguous ranges.
  The elements, expressed either as single value, prefix and ranges are
  all first normalized to ranges. This elements expressed as ranges are
  mergesorted. Then, there is a linear list inspection to check for
  merge candidates. This code only merges elements in the same batch,
  ie. it does not merge elements in the kernela and the userspace batch.

- add set_overlap() to check for overlapping set elements. Linux
  kernel >= 5.7 already checks for overlaps, older kernels still needs
  this code. This code checks for two conflict types:

  1) between elements in this batch.
  2) between elements in this batch and kernelspace.

  The elements in the kernel are temporarily merged into the list of
  elements in the batch to check for this overlaps. The EXPR_F_KERNEL
  flag allows us to restore the set cache after the overlap check has
  been performed.

- set_to_interval() now only transforms set elements, expressed as range
  e.g. [a,b], to individual set elements using the EXPR_F_INTERVAL_END
  flag notation to represent e.g. [a,b+1), where b+1 has the
  EXPR_F_INTERVAL_END flag set on.

More relevant updates:

- The overlap and automerge routines are now performed in the evaluation
  phase.

- The userspace set object representation now stores a reference to the
  existing kernel set object (in case there is already a set with this
  same name in the kernel). This is required by the new overlap and
  automerge approach.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: add EXPR_F_KERNEL to identify expression in the kernel
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:09 +0000 (04:01 +0200)] 
src: add EXPR_F_KERNEL to identify expression in the kernel

This allows to identify the set elements that reside in the kernel.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosegtree: add support for get element with sets that contain ifnames
Florian Westphal [Sat, 9 Apr 2022 13:58:32 +0000 (15:58 +0200)] 
segtree: add support for get element with sets that contain ifnames

nft get element inet filter s { bla, prefixfoo }
table inet filter {
        set s {
                type ifname
                flags interval
                elements = { "prefixfoo*",
                             "bla" }
        }

Also add test cases for this.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosegtree: use correct byte order for 'element get'
Florian Westphal [Sat, 9 Apr 2022 13:58:31 +0000 (15:58 +0200)] 
segtree: use correct byte order for 'element get'

Fails when the argument / set contains strings: we need to use
host byte order if element has string base type.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: add testcases for interface names in sets
Florian Westphal [Sat, 9 Apr 2022 13:58:30 +0000 (15:58 +0200)] 
tests: add testcases for interface names in sets

Add initial test case, sets with names and interfaces,
anonymous and named ones.

Check match+no-match.
netns with ppp1 and ppq veth, send packets via both interfaces.
Rule counters should have incremented on the three rules.
(that match on set that have "abcdef1" or "abcdef*" strings in them).

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosegtree: add string "range" reversal support
Florian Westphal [Sat, 9 Apr 2022 13:58:29 +0000 (15:58 +0200)] 
segtree: add string "range" reversal support

Previous commits allows to use set key as a range, i.e.

key ifname
flags interval
elements = { eth* }

and then have it match on any interface starting with 'eth'.

Listing is broken however, we need to reverse-translate the (128bit)
number back to a string.

'eth*' is stored as interval
00687465 0000000 ..  00697465 0000000, i.e. "eth-eti",
this adds the needed endianess fixups.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: make interval sets work with string datatypes
Florian Westphal [Sat, 9 Apr 2022 13:58:28 +0000 (15:58 +0200)] 
src: make interval sets work with string datatypes

Allows to interface names in interval sets:

table inet filter {
        set s {
                type ifname
                flags interval
                elements = { eth*, foo }
        }

Concatenations are not yet supported, also, listing is broken,
those strings will not be printed back because the values will remain
in big-endian order.  Followup patch will extend segtree to translate
this back to host byte order.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: string prefix expression must retain original length
Florian Westphal [Sat, 9 Apr 2022 13:58:27 +0000 (15:58 +0200)] 
evaluate: string prefix expression must retain original length

To make something like "eth*" work for interval sets (match
eth0, eth1, and so on...) we must treat the string as a 128 bit
integer.

Without this, segtree will do the wrong thing when applying the prefix,
because we generate the prefix based on 'eth*' as input, with a length of 3.

The correct import needs to be done on "eth\0\0\0\0\0\0\0...", i.e., if
the input buffer were an ipv6 address, it should look like "eth\0::",
not "::eth".

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosegtree: split prefix and range creation to a helper function
Florian Westphal [Sat, 9 Apr 2022 13:58:26 +0000 (15:58 +0200)] 
segtree: split prefix and range creation to a helper function

No functional change intended.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: keep prefix expression length
Florian Westphal [Sat, 9 Apr 2022 13:58:25 +0000 (15:58 +0200)] 
evaluate: keep prefix expression length

Else, range_expr_value_high() will see a 0 length when doing:

mpz_init_bitmask(tmp, expr->len - expr->prefix_len);

This wasn't a problem so far because prefix expressions generated
from "string*" were never passed down to the prefix->range conversion
functions.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: make byteorder conversion on string base type a no-op
Florian Westphal [Sat, 9 Apr 2022 13:58:24 +0000 (15:58 +0200)] 
evaluate: make byteorder conversion on string base type a no-op

Prerequisite for support of interface names in interval sets:
 table inet filter {
set s {
type ifname
flags interval
elements = { "foo" }
}
chain input {
type filter hook input priority filter; policy accept;
iifname @s counter
}
 }

Will yield: "Byteorder mismatch: meta expected big endian, got host endian".
This is because of:

 /* Data for range lookups needs to be in big endian order */
 if (right->set->flags & NFT_SET_INTERVAL &&
   byteorder_conversion(ctx, &rel->left, BYTEORDER_BIG_ENDIAN) < 0)

It doesn't make sense to me to add checks to all callers of
byteorder_conversion(), so treat this similar to EXPR_CONCAT and turn
TYPE_STRING byteorder change into a no-op.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: py: Add meta time tests without 'meta' keyword
Martin Gignac [Sat, 9 Apr 2022 12:57:02 +0000 (08:57 -0400)] 
tests: py: Add meta time tests without 'meta' keyword

v1.0.2 of 'nft' fails on 'time < "2022-07-01 11:00:00"' but succeeds
when 'meta' is specified ('meta time < "2022-07-01 11:00:00"'). This
extends coverage by testing 'time' without 'meta'.

Signed-off-by: Martin Gignac <martin.gignac@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: py: Don't colorize output if stderr is redirected
Phil Sutter [Thu, 7 Apr 2022 11:53:05 +0000 (13:53 +0200)] 
tests: py: Don't colorize output if stderr is redirected

Cover for calls with '2>/tmp/log' and avoid printing escape sequences to
that file. One could still keep colored output on stdout, but that
required a printing routine for non-errors.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: monitor: Hide temporary file names from error output
Phil Sutter [Wed, 6 Apr 2022 13:41:03 +0000 (15:41 +0200)] 
tests: monitor: Hide temporary file names from error output

Make error output deterministic by passing input to nft via stdin. This
way error messages will contain "/dev/stdin" instead of the temporary
file name.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: py: extend meta time coverage
Pablo Neira Ayuso [Fri, 8 Apr 2022 08:26:12 +0000 (10:26 +0200)] 
tests: py: extend meta time coverage

Add meta time tests using < and > operands.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agometa: fix compiler warning in date_type_parse()
Lukas Straub [Tue, 5 Apr 2022 16:43:30 +0000 (16:43 +0000)] 
meta: fix compiler warning in date_type_parse()

After commit 0210097879 ("meta: time: use uint64_t instead of time_t")
there is a compiler warning due to comparison of the return value from
parse_iso_date with -1, which is now implicitly cast to uint64_t.

Fix this by making parse_iso_date take a pointer to the tstamp and
return bool instead.

Fixes: 0210097879 ("meta: time: use uint64_t instead of time_t")
Signed-off-by: Lukas Straub <lukasstraub2@web.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agometa: time: use uint64_t instead of time_t
Lukas Straub [Tue, 5 Apr 2022 10:41:14 +0000 (10:41 +0000)] 
meta: time: use uint64_t instead of time_t

time_t may be 32 bit on some platforms and thus can't fit a timestamp
with nanoseconds resolution. This causes overflows and ultimatively
breaks meta time expressions on such platforms.

Fix this by using uint64_t instead.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1567
Fixes: f8f32deda31df ("meta: Introduce new conditions 'time', 'day' and 'hour'")
Signed-off-by: Lukas Straub <lukasstraub2@web.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoinclude: add missing `#include`
Jeremy Sowden [Mon, 4 Apr 2022 12:13:40 +0000 (13:13 +0100)] 
include: add missing `#include`

datatype.h uses bool and so should include <stdbool.h>.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoexamples: add .gitignore file
Jeremy Sowden [Mon, 4 Apr 2022 12:13:39 +0000 (13:13 +0100)] 
examples: add .gitignore file

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: py: add inet/vmap tests
Pablo Neira Ayuso [Tue, 29 Mar 2022 10:07:17 +0000 (12:07 +0200)] 
tests: py: add inet/vmap tests

Add a few tests with concatenations including raw and integer type
expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: Restore optimization for raw payload expressions
Pablo Neira Ayuso [Sun, 27 Mar 2022 21:14:43 +0000 (23:14 +0200)] 
optimize: Restore optimization for raw payload expressions

This patch reverts d0f14b5337e7 ("optimize: do not merge raw payload
expressions") after adding support for concatenation with variable
length TYPE_INTEGER.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: allow to use integer type header fields via typeof set declaration
Pablo Neira Ayuso [Mon, 28 Mar 2022 15:36:40 +0000 (17:36 +0200)] 
src: allow to use integer type header fields via typeof set declaration

Header fields such as udp length cannot be used in concatenations because
it is using the generic integer_type:

 test.nft:3:10-19: Error: can not use variable sized data types (integer) in concat expressions
                typeof udp length . @th,32,32
                       ^^^^^^^^^^~~~~~~~~~~~~

This patch slightly extends ("src: allow to use typeof of raw expressions in
set declaration") to set on NFTNL_UDATA_SET_KEY_PAYLOAD_LEN in userdata if
TYPE_INTEGER is used.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: allow to use typeof of raw expressions in set declaration
Pablo Neira Ayuso [Mon, 28 Mar 2022 15:53:39 +0000 (17:53 +0200)] 
src: allow to use typeof of raw expressions in set declaration

Use the dynamic datatype to allocate an instance of TYPE_INTEGER and set
length and byteorder. Add missing information to the set userdata area
for raw payload expressions which allows to rebuild the set typeof from
the listing path.

A few examples:

- With anonymous sets:

  nft add rule x y ip saddr . @ih,32,32 { 1.1.1.1 . 0x14, 2.2.2.2 . 0x1e }

- With named sets:

 table x {
        set y {
                typeof ip saddr . @ih,32,32
                elements = { 1.1.1.1 . 0x14 }
        }
 }

Incremental updates are also supported, eg.

 nft add element x y { 3.3.3.3 . 0x28 }

expr_evaluate_concat() is used to evaluate both set key definitions
and set key values, using two different function might help to simplify
this code in the future.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexpression: typeof verdict needs verdict datatype
Pablo Neira Ayuso [Mon, 28 Mar 2022 11:25:42 +0000 (13:25 +0200)] 
expression: typeof verdict needs verdict datatype

Otherwise listing breaks showing [invalid type] notice.

 # nft list ruleset
 table inet x {
        map y {
                typeof ip saddr : verdict
                elements = { 1.1.1.1 : 0x1010101 [invalid type] }
        }
 }

Update tests to cover this usecase.

Fixes: 4ab1e5e60779 ("src: allow use of 'verdict' in typeof definitions")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: copy field_count for anonymous object maps as well
Florian Westphal [Mon, 21 Mar 2022 16:03:41 +0000 (17:03 +0100)] 
src: copy field_count for anonymous object maps as well

without this test fails with:

W: [FAILED]     tests/shell/testcases/maps/anon_objmap_concat: got 134
BUG: invalid range expression type concat
nft: expression.c:1452: range_expr_value_low: Assertion `0' failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agorule: Avoid segfault with anonymous chains
Pablo Neira Ayuso [Thu, 17 Mar 2022 20:53:25 +0000 (21:53 +0100)] 
rule: Avoid segfault with anonymous chains

Phil Sutter says:

"When trying to add a rule which contains an anonymous chain to a
non-existent chain, string_misspell_update() is called with a NULL
string because the anonymous chain has no name. Avoid this by making the
function NULL-pointer tolerant."

Fixes: c330152b7f777 ("src: support for implicit chain bindings")
Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoevaluate: init cmd pointer for new on-stack context
Florian Westphal [Fri, 4 Mar 2022 10:30:55 +0000 (11:30 +0100)] 
evaluate: init cmd pointer for new on-stack context

else, this will segfault when trying to print the
"table 'x' doesn't exist" error message.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agooptimize: do not assume log prefix
Pablo Neira Ayuso [Fri, 4 Mar 2022 09:37:48 +0000 (10:37 +0100)] 
optimize: do not assume log prefix

... log prefix might not be present in log statements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: do not merge unsupported statement expressions
Pablo Neira Ayuso [Thu, 3 Mar 2022 13:56:58 +0000 (14:56 +0100)] 
optimize: do not merge unsupported statement expressions

Only value, range, prefix, set and list are supported at this stage.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: incorrect assert() for unexpected expression type
Pablo Neira Ayuso [Thu, 3 Mar 2022 12:11:29 +0000 (13:11 +0100)] 
optimize: incorrect assert() for unexpected expression type

assert(1) is noop, this should be assert(0) instead.

Fixes: 561aa3cfa8da ("optimize: merge verdict maps with same lookup key")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: more robust statement merge with vmap
Pablo Neira Ayuso [Thu, 3 Mar 2022 12:06:59 +0000 (13:06 +0100)] 
optimize: more robust statement merge with vmap

Check expressions that are expected on the rhs rather than using a
catch-all default case.

Actually, lists and sets need to be their own routine, because this
needs the set element key expression to be merged.

This is a follow up to 99eb46969f3d ("optimize: fix vmap with anonymous
sets").

Fixes: 1542082e259b ("optimize: merge same selector with different verdict into verdict map")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: fix vmap with anonymous sets
Pablo Neira Ayuso [Thu, 3 Mar 2022 11:20:29 +0000 (12:20 +0100)] 
optimize: fix vmap with anonymous sets

The following example ruleset crashes:

 table inet a {
        chain b {
                tcp dport { 1 } accept
                tcp dport 2-3 drop
        }
 }

because handling for EXPR_SET is missing.

Fixes: 1542082e259b ("optimize: merge same selector with different verdict into verdict map")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoscanner: Fix for ipportmap nat statements
Phil Sutter [Wed, 2 Mar 2022 13:45:49 +0000 (14:45 +0100)] 
scanner: Fix for ipportmap nat statements

Due to lookahead, "addr" keyword is still found in IP/IP6 scope, not
STMT_NAT one.

Fixes: a67fce7ffe7e4 ("scanner: nat: Move to own scope")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: dup, fwd, tproxy: Move to own scopes
Phil Sutter [Fri, 30 Jul 2021 16:11:51 +0000 (18:11 +0200)] 
scanner: dup, fwd, tproxy: Move to own scopes

With these three scopes in place, keyword 'to' may be isolated.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: meta: Move to own scope
Phil Sutter [Fri, 30 Jul 2021 16:05:19 +0000 (18:05 +0200)] 
scanner: meta: Move to own scope

This allows to isolate 'length' and 'protocol' keywords shared by other
scopes as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: at: Move to own scope
Phil Sutter [Fri, 30 Jul 2021 15:43:26 +0000 (17:43 +0200)] 
scanner: at: Move to own scope

Modification of raw TCP option rule is a bit more complicated to avoid
pushing tcp_hdr_option_type into the introduced scope by accident.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: nat: Move to own scope
Phil Sutter [Fri, 30 Jul 2021 15:26:31 +0000 (17:26 +0200)] 
scanner: nat: Move to own scope

Unify nat, masquerade and redirect statements, they widely share their
syntax.

Note the workaround of adding "prefix" to SCANSTATE_IP. This is required
to fix for 'snat ip prefix ...' style expressions.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: policy: move to own scope
Phil Sutter [Fri, 30 Jul 2021 15:20:27 +0000 (17:20 +0200)] 
scanner: policy: move to own scope

Isolate 'performance' and 'memory' keywords.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: flags: move to own scope
Phil Sutter [Fri, 30 Jul 2021 15:13:39 +0000 (17:13 +0200)] 
scanner: flags: move to own scope

This isolates at least 'constant', 'dynamic' and 'all' keywords.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: reject: Move to own scope
Phil Sutter [Tue, 27 Jul 2021 14:38:59 +0000 (16:38 +0200)] 
scanner: reject: Move to own scope

Two more keywords isolated.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: import, export: Move to own scopes
Phil Sutter [Tue, 27 Jul 2021 14:35:14 +0000 (16:35 +0200)] 
scanner: import, export: Move to own scopes

In theory, one could use a common scope for both import and export
commands, their parameters are identical.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: reset: move to own Scope
Phil Sutter [Tue, 27 Jul 2021 16:55:39 +0000 (18:55 +0200)] 
scanner: reset: move to own Scope

Isolate two more keywords shared with list command.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: monitor: Move to own Scope
Phil Sutter [Fri, 23 Jul 2021 16:27:11 +0000 (18:27 +0200)] 
scanner: monitor: Move to own Scope

Some keywords are shared with list command.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: rt: Extend scope over rt0, rt2 and srh
Phil Sutter [Fri, 23 Jul 2021 16:15:42 +0000 (18:15 +0200)] 
scanner: rt: Extend scope over rt0, rt2 and srh

These are technically all just routing headers with different types, so
unify them under the same scope.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: type: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 16:08:47 +0000 (18:08 +0200)] 
scanner: type: Move to own scope

As a side-effect, this fixes for use of 'classid' as set data type.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: dst, frag, hbh, mh: Move to own scopes
Phil Sutter [Fri, 23 Jul 2021 15:39:55 +0000 (17:39 +0200)] 
scanner: dst, frag, hbh, mh: Move to own scopes

These are the remaining IPv6 extension header expressions, only rt
expression was scoped already.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: ah, esp: Move to own scopes
Phil Sutter [Fri, 23 Jul 2021 15:20:00 +0000 (17:20 +0200)] 
scanner: ah, esp: Move to own scopes

They share 'sequence' keyword with icmp and tcp expressions.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: osf: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 15:11:47 +0000 (17:11 +0200)] 
scanner: osf: Move to own scope

It shares two keywords with PARSER_SC_IP.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: dccp, th: Move to own scopes
Phil Sutter [Fri, 23 Jul 2021 15:05:27 +0000 (17:05 +0200)] 
scanner: dccp, th: Move to own scopes

With them in place, heavily shared keywords 'sport' and 'dport' may be
isolated.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: udp{,lite}: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 13:05:41 +0000 (15:05 +0200)] 
scanner: udp{,lite}: Move to own scope

All used keywords are shared with others, so no separation for now apart
from 'csumcov' which was actually missing from scanner.l.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: comp: Move to own scope.
Phil Sutter [Fri, 23 Jul 2021 12:56:02 +0000 (14:56 +0200)] 
scanner: comp: Move to own scope.

Isolates only 'cpi' keyword for now.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: synproxy: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 12:34:34 +0000 (14:34 +0200)] 
scanner: synproxy: Move to own scope

Quite a few keywords are shared with PARSER_SC_TCP.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: tcp: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 12:03:19 +0000 (14:03 +0200)] 
scanner: tcp: Move to own scope

Apart from header fields, this isolates TCP option types and
fields, too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: igmp: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 11:09:57 +0000 (13:09 +0200)] 
scanner: igmp: Move to own scope

At least isolates 'mrt' and 'group' keywords, the latter is shared with
log statement.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: icmp{,v6}: Move to own scope
Phil Sutter [Fri, 23 Jul 2021 11:00:50 +0000 (13:00 +0200)] 
scanner: icmp{,v6}: Move to own scope

Unify the two, header fields are almost identical.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agosrc: add tcp option reset support
Florian Westphal [Mon, 20 Dec 2021 11:30:18 +0000 (12:30 +0100)] 
src: add tcp option reset support

This allows to replace a tcp option with nops, similar
to the TCPOPTSTRIP feature of iptables.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agobuild: explicitly pass --version-script to linker
Sam James [Thu, 24 Feb 2022 19:45:43 +0000 (19:45 +0000)] 
build: explicitly pass --version-script to linker

--version-script is a linker option, so let's use -Wl, so that
libtool handles it properly. It seems like the previous method gets silently
ignored with GNU libtool in some cases(?) and downstream in Gentoo,
we had to apply this change to make the build work with slibtool anyway.

But it's indeed correct in any case, so let's swap.

Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agolibnftables.map: export new nft_ctx_{get,set}_optimize API
Sam James [Thu, 24 Feb 2022 19:45:42 +0000 (19:45 +0000)] 
libnftables.map: export new nft_ctx_{get,set}_optimize API

[ Remove incorrect symbol names were exported via .map file ]

Without this, we're not explicitly saying this is part of the
public API.

This new API was added in 1.0.2 and is used by e.g. the main
nft binary. Noticed when fixing the version-script option
(separate patch) which picked up this problem when .map
was missing symbols (related to when symbol visibility
options get set).

Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: add test case for flowtable with owner flag
Florian Westphal [Tue, 22 Feb 2022 12:51:09 +0000 (13:51 +0100)] 
tests: add test case for flowtable with owner flag

BUG: KASAN: use-after-free in nf_hook_entries_grow+0x675/0x980
Read of size 4 at ... nft/19662
 nf_hook_entries_grow+0x675/0x980

This is fixed by kernel commit 6069da443bf
("netfilter: nf_tables: unregister flowtable hooks on netns exit").

The test case here uses owner flag, netlink event handler doesn't
release the flowtable, next attempt to add one then causes uaf because
of dangling ingress hook reference.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoexamples: compile with `make check' and add AM_CPPFLAGS
Pablo Neira Ayuso [Mon, 21 Feb 2022 23:56:36 +0000 (00:56 +0100)] 
examples: compile with `make check' and add AM_CPPFLAGS

Compile examples via `make check' like libnftnl does. Use AM_CPPFLAGS to
specify local headers via -I.

Unfortunately, `make distcheck' did not catch this compile time error in
my system, since it was using the nftables/libnftables.h file of the
previous nftables release.

Fixes: 5b364657a35f ("build: missing SUBIRS update")
Fixes: caf2a6ad2d22 ("examples: add libnftables example program")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agobuild: missing SUBIRS update v1.0.2
Pablo Neira Ayuso [Mon, 21 Feb 2022 11:52:28 +0000 (12:52 +0100)] 
build: missing SUBIRS update

Add new examples folder, other `make distcheck' fails.

Fixes: caf2a6ad2d22 ("examples: add libnftables example program")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agobuild: Bump version to 1.0.2
Pablo Neira Ayuso [Mon, 21 Feb 2022 11:38:10 +0000 (12:38 +0100)] 
build: Bump version to 1.0.2

Still requires libnftnl 1.2.1

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoscanner: rt: Move seg-left keyword into scope
Phil Sutter [Fri, 23 Jul 2021 16:18:41 +0000 (18:18 +0200)] 
scanner: rt: Move seg-left keyword into scope

It's not used outside of rt_hdr_expr, so move it out of INIT scope.

Fixes: 8861db1b771a6 ("scanner: rt: move to own scope")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: Some time units are only used in limit scope
Phil Sutter [Fri, 30 Jul 2021 15:36:58 +0000 (17:36 +0200)] 
scanner: Some time units are only used in limit scope

'hour' and 'day' are allowed as unqualified meta expressions, so leave
them alone.

Fixes: eae2525685252 ("scanner: limit: move to own scope")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoscanner: Move 'maps' keyword into list cmd scope
Phil Sutter [Thu, 29 Jul 2021 13:00:20 +0000 (15:00 +0200)] 
scanner: Move 'maps' keyword into list cmd scope

This was missed when introducing SCANSTATE_CMD_LIST, no other command
operates on "maps".

Fixes: 6a24ffb04642e ("scanner: add list cmd parser scope")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: py: Test connlimit statement
Phil Sutter [Thu, 17 Feb 2022 11:52:20 +0000 (12:52 +0100)] 
tests: py: Test connlimit statement

This wasn't covered at all.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agosegtree: memleak get element command
Pablo Neira Ayuso [Tue, 15 Feb 2022 14:12:20 +0000 (15:12 +0100)] 
segtree: memleak get element command

Release removed interval expressions before get_set_interval_find()
fails. The memleak can be triggered through:

 testcases/sets/0034get_element_0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink: check key is EXPR_CONCAT before accessing field
Pablo Neira Ayuso [Thu, 17 Feb 2022 17:08:02 +0000 (18:08 +0100)] 
netlink: check key is EXPR_CONCAT before accessing field

alloc_nftnl_setelem() needs to check for EXPR_CONCAT before accessing
field_count.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexamples: load ruleset from JSON
Pablo Neira Ayuso [Mon, 14 Feb 2022 12:02:21 +0000 (13:02 +0100)] 
examples: load ruleset from JSON

Add an example to load a ruleset file expressed in JSON.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexamples: add libnftables example program
Pablo Neira Ayuso [Mon, 14 Feb 2022 11:31:48 +0000 (12:31 +0100)] 
examples: add libnftables example program

Create an example folder to add example source code files to show how to
use libnftables. Add first example program using the buffer API.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser_json: permit empty device list
Florian Westphal [Mon, 7 Feb 2022 13:09:28 +0000 (14:09 +0100)] 
parser_json: permit empty device list

Normal input parser allows flowtables without 'devices' token, which
makes the json export part elide 'dev' entirely, this then breaks on
re-import:

$ nft -j -f json.dump
/tmp/json_1:1:14-14: Error: Object item not found: dev

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoparser_json: fix flowtable device datatype
Florian Westphal [Mon, 7 Feb 2022 12:03:20 +0000 (13:03 +0100)] 
parser_json: fix flowtable device datatype

Failed with: BUG: invalid expresion type symbol

Fixes: 78bbe7f7a55be489 ("mnl: do not use expr->identifier to fetch device name")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agojson: add flow statement json export + parser
Florian Westphal [Mon, 7 Feb 2022 12:03:20 +0000 (13:03 +0100)] 
json: add flow statement json export + parser

flow statement has no export, its shown as:
".. }, "flow add @ft" ] } }"

With this patch:

".. }, {"flow": {"op": "add", "flowtable": "@ft"}}]}}"

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agodoc: add undefine and redefine keywords
Pierre Ducroquet [Mon, 7 Feb 2022 09:48:11 +0000 (10:48 +0100)] 
doc: add undefine and redefine keywords

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoiface: handle EINTR case when creating the cache
Pablo Neira Ayuso [Thu, 27 Jan 2022 18:05:55 +0000 (19:05 +0100)] 
iface: handle EINTR case when creating the cache

If interface netlink dump is interrupted, then retry.

Before this patch, the netlink socket is reopened to drop stale dump
messages, instead empty the netlink queue and retry.

Reviewed-by: Eugene Crosser <crosser@average.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: do not merge raw payload expressions
Pablo Neira Ayuso [Wed, 26 Jan 2022 22:03:45 +0000 (23:03 +0100)] 
optimize: do not merge raw payload expressions

Merging raw expressions results in a valid concatenation which throws:

 Error: can not use variable sized data types (integer) in concat expressions

Disable merging raw expressions until this is supported by skipping raw
expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: check for payload base and offset when searching for mergers
Pablo Neira Ayuso [Wed, 26 Jan 2022 22:10:51 +0000 (23:10 +0100)] 
optimize: check for payload base and offset when searching for mergers

Extend the existing checks to cover the payload base and offset.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: merge verdict maps with same lookup key
Pablo Neira Ayuso [Wed, 26 Jan 2022 21:49:35 +0000 (22:49 +0100)] 
optimize: merge verdict maps with same lookup key

Merge two consecutive verdict maps with the same lookup key.

For instance, merge the following:

 table inet x {
        chain filter_in_tcp {
                tcp dport vmap {
                           80 : accept,
                           81 : accept,
                          443 : accept,
                          931 : accept,
                         5001 : accept,
                         5201 : accept,
                }
                tcp dport vmap {
                         6800-6999  : accept,
                        33434-33499 : accept,
                }
        }
 }

into:

 table inet x {
        chain filter_in_tcp {
                tcp dport vmap {
                           80 : accept,
                           81 : accept,
                          443 : accept,
                          931 : accept,
                         5001 : accept,
                         5201 : accept,
                         6800-6999  : accept,
                        33434-33499 : accept,
                }
}
 }

This patch updates statement comparison routine to inspect the verdict
expression type to detect possible merger.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: add __expr_cmp()
Pablo Neira Ayuso [Wed, 26 Jan 2022 21:49:27 +0000 (22:49 +0100)] 
optimize: add __expr_cmp()

Add helper function to compare expression to allow for reuse.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonetlink: Use abort() in case of netlink_abi_error
Eugene Crosser [Thu, 9 Dec 2021 18:26:06 +0000 (19:26 +0100)] 
netlink: Use abort() in case of netlink_abi_error

Library functions should not use exit(), application that uses the
library may contain error handling path, that cannot be executed if
library functions calls exit(). For truly fatal errors, using abort() is
more acceptable than exit().

Signed-off-by: Eugene Crosser <crosser@average.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoparser_bison: missing synproxy support in map declarations
Pablo Neira Ayuso [Wed, 19 Jan 2022 21:39:56 +0000 (22:39 +0100)] 
parser_bison: missing synproxy support in map declarations

Update parser to allow for maps with synproxy.

Fixes: f44ab88b1088 ("src: add synproxy stateful object support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agotests: shell: remove redundant payload expressions
Jeremy Sowden [Sat, 15 Jan 2022 18:27:09 +0000 (18:27 +0000)] 
tests: shell: remove redundant payload expressions

Now that we keep track of more payload dependencies, more redundant
payloads are eliminated.  Remove these from the shell test-cases.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: py: remove redundant payload expressions
Jeremy Sowden [Sat, 15 Jan 2022 18:27:08 +0000 (18:27 +0000)] 
tests: py: remove redundant payload expressions

Now that we keep track of more payload dependencies, more redundant
payloads are eliminated.  Remove these from the Python test-cases.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agosrc: store more than one payload dependency
Jeremy Sowden [Sat, 15 Jan 2022 18:27:07 +0000 (18:27 +0000)] 
src: store more than one payload dependency

Change the payload-dependency context to store a dependency for every
protocol layer.  This allows us to eliminate more redundant protocol
expressions.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agosrc: add a helper that returns a payload dependency for a particular base
Jeremy Sowden [Sat, 15 Jan 2022 18:27:06 +0000 (18:27 +0000)] 
src: add a helper that returns a payload dependency for a particular base

Currently, with only one base and dependency stored this is superfluous,
but it will become more useful when the next commit adds support for
storing a payload for every base.

Remove redundant `ctx->pbase` check.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: py: fix inet/ip.t bridge payload
Jeremy Sowden [Sat, 15 Jan 2022 18:27:05 +0000 (18:27 +0000)] 
tests: py: fix inet/ip.t bridge payload

Correct the statement used to load the protocol in the bridge payload
of one of the ip tests.

A previous commit was supposed, in part, to do this, but the update got
lost.

Fixes: 4b8e51ea5fc8 ("tests: py: fix inet/ip.t payloads")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agosrc: silence compiler warnings
Florian Westphal [Sat, 15 Jan 2022 19:00:49 +0000 (20:00 +0100)] 
src: silence compiler warnings

cache.c:504:22: warning: ‘chain’ may be used uninitialized in this function [-Wmaybe-uninitialized]
cache.c:504:22: warning: ‘table’ may be used uninitialized in this function [-Wmaybe-uninitialized]
erec.c:128:16: warning: ‘line’ may be used uninitialized in this function [-Wmaybe-uninitialized]
optimize.c:524:9: warning: ‘line’ may be used uninitialized in this function [-Wmaybe-uninitialized]

Fixes: 8ad4056e9182 ("erec: expose print_location() and line_location()")
Fixes: afbd102211dc ("src: do not use the nft_cache_filter object from mnl.c")
Fixes: fb298877ece2 ("src: add ruleset optimization infrastructure")
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agolibnftables: use xrealloc()
Pablo Neira Ayuso [Sat, 15 Jan 2022 17:50:21 +0000 (18:50 +0100)] 
libnftables: use xrealloc()

Instead of realloc(), so process stops execution in case memory
allocation fails.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: 'nft list chain' prints anonymous chains correctly
Pablo Neira Ayuso [Wed, 12 Jan 2022 00:34:01 +0000 (01:34 +0100)] 
src: 'nft list chain' prints anonymous chains correctly

If the user is requesting a chain listing, e.g. nft list chain x y
and a rule refers to an anonymous chain that cannot be found in the cache,
then fetch such anonymous chain and its ruleset.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1577
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: add helper function to fill up the rule cache
Pablo Neira Ayuso [Wed, 12 Jan 2022 00:34:00 +0000 (01:34 +0100)] 
cache: add helper function to fill up the rule cache

Add a helper function to dump the rules and add them to the
corresponding chain.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agocache: do not set error code twice
Pablo Neira Ayuso [Wed, 12 Jan 2022 00:33:59 +0000 (01:33 +0100)] 
cache: do not set error code twice

The 'ret' variable is already set to a negative value to report an
error, do not set it again to a negative value.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: do not use the nft_cache_filter object from mnl.c
Pablo Neira Ayuso [Wed, 12 Jan 2022 00:33:58 +0000 (01:33 +0100)] 
src: do not use the nft_cache_filter object from mnl.c

Pass the table and chain strings to mnl_nft_rule_dump() instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: merge several selectors with different verdict into verdict map
Pablo Neira Ayuso [Sun, 2 Jan 2022 20:54:01 +0000 (21:54 +0100)] 
optimize: merge several selectors with different verdict into verdict map

Transform:

  ip saddr 1.1.1.1 ip daddr 2.2.2.2 accept
  ip saddr 2.2.2.2 ip daddr 3.3.3.3 drop

into:

  ip saddr . ip daddr vmap { 1.1.1.1 . 2.2.2.2 : accept, 2.2.2.2 . 3.3.3.3 : drop }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: merge same selector with different verdict into verdict map
Pablo Neira Ayuso [Sun, 2 Jan 2022 20:53:41 +0000 (21:53 +0100)] 
optimize: merge same selector with different verdict into verdict map

Transform:

  ct state invalid drop
  ct state established,related accept

into:

  ct state vmap { established : accept, related : accept, invalid : drop }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agooptimize: merge rules with same selectors into a concatenation
Pablo Neira Ayuso [Sun, 2 Jan 2022 20:53:26 +0000 (21:53 +0100)] 
optimize: merge rules with same selectors into a concatenation

This patch extends the ruleset optimization infrastructure to collapse
several rules with the same selectors into a concatenation.

Transform:

  meta iifname eth1 ip saddr 1.1.1.1 ip daddr 2.2.2.3 accept
  meta iifname eth1 ip saddr 1.1.1.2 ip daddr 2.2.2.5 accept
  meta iifname eth2 ip saddr 1.1.1.3 ip daddr 2.2.2.6 accept

into:

  meta iifname . ip saddr . ip daddr { eth1 . 1.1.1.1 . 2.2.2.6, eth1 . 1.1.1.2 . 2.2.2.5 , eth1 . 1.1.1.3 . 2.2.2.6 } accept

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: add ruleset optimization infrastructure
Pablo Neira Ayuso [Sun, 2 Jan 2022 20:46:21 +0000 (21:46 +0100)] 
src: add ruleset optimization infrastructure

This patch adds a new -o/--optimize option to enable ruleset
optimization.

You can combine this option with the dry run mode (--check) to review
the proposed ruleset updates without actually loading the ruleset, e.g.

 # nft -c -o -f ruleset.test
 Merging:
 ruleset.nft:16:3-37:           ip daddr 192.168.0.1 counter accept
 ruleset.nft:17:3-37:           ip daddr 192.168.0.2 counter accept
 ruleset.nft:18:3-37:           ip daddr 192.168.0.3 counter accept
 into:
        ip daddr { 192.168.0.1, 192.168.0.2, 192.168.0.3 } counter packets 0 bytes 0 accept

This infrastructure collects the common statements that are used in
rules, then it builds a matrix of rules vs. statements. Then, it looks
for common statements in consecutive rules which allows to merge rules.

This ruleset optimization always performs an implicit dry run to
validate that the original ruleset is correct. Then, on a second pass,
it performs the ruleset optimization and add the rules into the kernel
(unless --check has been specified by the user).

From libnftables perspective, there is a new API to enable
this feature:

  uint32_t nft_ctx_get_optimize(struct nft_ctx *ctx);
  void nft_ctx_set_optimize(struct nft_ctx *ctx, uint32_t flags);

This patch adds support for the first optimization: Collapse a linear
list of rules matching on a single selector into a set as exposed in the
example above.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agosrc: remove '$' in symbol_expr_print
Pablo Neira Ayuso [Sun, 2 Jan 2022 20:46:10 +0000 (21:46 +0100)] 
src: remove '$' in symbol_expr_print

This is used in --debug=eval mode to annotate symbols that have not yet
been evaluated, remove it.

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