]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
4 years agosrc: queue: allow use of arbitrary queue expressions
Florian Westphal [Tue, 15 Jun 2021 12:57:08 +0000 (14:57 +0200)] 
src: queue: allow use of arbitrary queue expressions

back in 2016 Liping Zhang added support to kernel and libnftnl to
specify a source register containing the queue number to use.

This was never added to nft itself, so allow this.
On linearization side, check if attached expression is a range.
If its not, allocate a new register and set NFTNL_EXPR_QUEUE_SREG_QNUM
attribute after generating the lowlevel expressions for the kernel.

On delinarization we need to check for presence of
NFTNL_EXPR_QUEUE_SREG_QNUM and decode the expression(s) when present.

Also need to do postprocessing for STMT_QUEUE so that the protocol
context is set correctly, without this only raw payload expressions
will be shown (@nh,32,...) instead of 'ip ...'.

Next patch adds test cases.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: new queue flag input format
Florian Westphal [Wed, 16 Jun 2021 16:25:28 +0000 (18:25 +0200)] 
parser: new queue flag input format

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: add queue_stmt_compat
Florian Westphal [Wed, 16 Jun 2021 00:01:36 +0000 (02:01 +0200)] 
parser: add queue_stmt_compat

Rename existing rules to _compat to make sure old rules using 'queue'
statement will work.

Next patch adds distinct input format where flags are explicitly
provided:

 queue flags name,<nextflag> num 1

Without this, extension of queue expression to handle arbitrary
expression instead of queue number or range results in parser errors.

Example:
   queue num jhash ip saddr mod 4 and 1 bypass

will fail because scanner is still in 'ip' state, not 'queue', when
"bypass" is read.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add queue expr and flags to queue_stmt_alloc
Florian Westphal [Tue, 15 Jun 2021 23:45:29 +0000 (01:45 +0200)] 
src: add queue expr and flags to queue_stmt_alloc

Preparation patch to avoid too much $<stmt>$ references in the parser.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: restrict queue num expressiveness
Florian Westphal [Tue, 15 Jun 2021 22:43:46 +0000 (00:43 +0200)] 
parser: restrict queue num expressiveness

Else we run into trouble once we allow
queue num symhash mod 4 and 1

and so on.  Example problem:

queue num jhash ip saddr mod 4 and 1 bypass

This will fail to parse because the scanner is in the wrong state
(ip, not queue), so 'bypass' is parsed as a string.

Currently, while nft will eat the above just fine (minus 'bypass'),
nft rejects this from the evaluation phase with
   Error: queue number is not constant

So seems we are lucky and can restrict the supported expressions
to integer and range.

Furthermore, the line looks wrong because this statement:

   queue num jhash ip saddr mod 4 and 1 bypass

doesn't specifiy a number, "queue num 4" does, or "queue num 1-2" do.

For arbitrary expr support it seems sensible to enforce stricter
ordering to avoid any problems with the flags, for example:

queue bypass,futurekeyword to jhash ip saddr mod 42

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoevaluate: fix hash expression maxval
Florian Westphal [Wed, 16 Jun 2021 21:16:45 +0000 (23:16 +0200)] 
evaluate: fix hash expression maxval

It needs to account for the offset too.

Fixes: 9bee0c86f179 ("src: add offset attribute for hash expression")
Fixes: d4f9a8fb9e9a ("src: add offset attribute for numgen expression")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agorule: memleak of list of timeout policies
Pablo Neira Ayuso [Thu, 17 Jun 2021 15:29:11 +0000 (17:29 +0200)] 
rule: memleak of list of timeout policies

Release list of ct timeout policy when object is freed.

Direct leak of 160 byte(s) in 2 object(s) allocated from:
    #0 0x7fc0273ad330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7fc0231377c4 in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7fc023137983 in xzalloc /home/.../devel/nftables/src/utils.c:75
    #3 0x7fc0231f64d6 in nft_parse /home/.../devel/nftables/src/parser_bison.y:4448

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: memleak in osf flags
Pablo Neira Ayuso [Thu, 17 Jun 2021 15:24:59 +0000 (17:24 +0200)] 
parser_bison: memleak in osf flags

Release osf string flag after processing.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink_delinearize: memleak when listing ct event rule
Pablo Neira Ayuso [Thu, 17 Jun 2021 15:01:54 +0000 (17:01 +0200)] 
netlink_delinearize: memleak when listing ct event rule

listing a ruleset containing:

ct event set new,related,destroy,label

results in memleak:

 Direct leak of 3672 byte(s) in 27 object(s) allocated from:
    #0 0x7fa5465c0330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7fa54233772c in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7fa5423378eb in xzalloc /home/.../devel/nftables/src/utils.c:75
    #3 0x7fa5422488c6 in expr_alloc /home/.../devel/nftables/src/expression.c:45
    #4 0x7fa54224fb91 in binop_expr_alloc /home/.../devel/nftables/src/expression.c:698
    #5 0x7fa54224ddf8 in bitmask_expr_to_binops /home/.../devel/nftables/src/expression.c:512
    #6 0x7fa5423102ca in expr_postprocess /home/.../devel/nftables/src/netlink_delinearize.c:2448

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosegtree: memleak in error path of the set to segtree conversion
Pablo Neira Ayuso [Thu, 17 Jun 2021 10:31:11 +0000 (12:31 +0200)] 
segtree: memleak in error path of the set to segtree conversion

Release the array of intervals and the segtree in case of error,
otherwise these structures and objects are never released:

SUMMARY: AddressSanitizer: 2864 byte(s) leaked in 37 allocation(s).

Moreover, improve existing a test coverage of this error path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink_delinearize: memleak in string netlink postprocessing
Pablo Neira Ayuso [Thu, 17 Jun 2021 00:58:09 +0000 (02:58 +0200)] 
netlink_delinearize: memleak in string netlink postprocessing

Listing a matching wilcard string results in a memleak: ifname "dummy*"

Direct leak of 136 byte(s) in 1 object(s) allocated from:
    #0 0x7f27ba52e330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f27b9e1d434 in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7f27b9e1d5f3 in xzalloc /home/.../devel/nftables/src/utils.c:75
    #3 0x7f27b9d2e8c6 in expr_alloc /home/.../devel/nftables/src/expression.c:45
    #4 0x7f27b9d326e9 in constant_expr_alloc /home/.../devel/nftables/src/expression.c:419
    #5 0x7f27b9db9318 in netlink_alloc_value /home/.../devel/nftables/src/netlink.c:390
    #6 0x7f27b9de0433 in netlink_parse_cmp /home/.../devel/nftables/src/netlink_delinearize.c:321
    #7 0x7f27b9deb025 in netlink_parse_expr /home/.../devel/nftables/src/netlink_delinearize.c:1764
    #8 0x7f27b9deb0de in netlink_parse_rule_expr /home/.../devel/nftables/src/netlink_delinearize.c:1776
    #9 0x7f27b860af7b in nftnl_expr_foreach /home/.../devel/libnftnl/src/rule.c:690

Direct leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f27ba52e330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f27b9e1d434 in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7f27b96975c5 in __gmpz_init2 (/usr/lib/x86_64-linux-gnu/libgmp.so.10+0x1c5c5)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: memleak in binary operation transfer to RHS
Pablo Neira Ayuso [Wed, 16 Jun 2021 23:33:13 +0000 (01:33 +0200)] 
evaluate: memleak in binary operation transfer to RHS

Remove useless reference count grabbing on constant expression that
results in a memleak.

Direct leak of 136 byte(s) in 1 object(s) allocated from:
    #0 0x7f4cd54af330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f4cd4d9e489 in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7f4cd4d9e648 in xzalloc /home/.../devel/nftables/src/utils.c:75
    #3 0x7f4cd4caf8c6 in expr_alloc /home/.../devel/nftables/src/expression.c:45
    #4 0x7f4cd4cb36e9 in constant_expr_alloc /home/.../devel/nftables/src/expression.c:419
    #5 0x7f4cd4ca714c in integer_type_parse /home/.../devel/nftables/src/datatype.c:397
    #6 0x7f4cd4ca4bee in symbolic_constant_parse /home/.../devel/nftables/src/datatype.c:165
    #7 0x7f4cd4ca4572 in symbol_parse /home/.../devel/nftables/src/datatype.c:135
    #8 0x7f4cd4cc333f in expr_evaluate_symbol /home/.../devel/nftables/src/evaluate.c:251
[...]
Indirect leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x7f4cd54af330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x7f4cd4d9e489 in xmalloc /home/.../devel/nftables/src/utils.c:36
    #2 0x7f4cd46185c5 in __gmpz_init2 (/usr/lib/x86_64-linux-gnu/libgmp.so.10+0x1c5c5)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: unbreak verdict maps with implicit map with interval concatenations
Pablo Neira Ayuso [Wed, 16 Jun 2021 11:49:08 +0000 (13:49 +0200)] 
evaluate: unbreak verdict maps with implicit map with interval concatenations

Verdict maps in combination with interval concatenations are broken, e.g.

 # nft add rule x y tcp dport . ip saddr vmap { 1025-65535 . 192.168.10.2 : accept }

Retrieve the concatenation field length and count from the map->map
expressions that represents the key of the implicit map.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: do not skip mapping elements
Pablo Neira Ayuso [Wed, 16 Jun 2021 17:00:50 +0000 (19:00 +0200)] 
evaluate: do not skip mapping elements

Set element keys are of EXPR_SET_ELEM expression type, however, mappings
use the EXPR_MAPPING expression to wrap the EXPR_SET_ELEM key
(mapping->left) and the corresponding data (mapping->right).

This patch adds a wrapper function to fetch the EXPR_SET_ELEM expression
from the key in case of mappings and use it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: add a icmp-reply only and icmpv6 id test cases
Florian Westphal [Tue, 15 Jun 2021 16:01:51 +0000 (18:01 +0200)] 
tests: add a icmp-reply only and icmpv6 id test cases

Check that nft doesn't remove the dependency in these cases:
icmp type echo-reply icmp id 1
("icmp id" matches both echo request and reply).

Add icmpv6 test cases.  These fail without the previous patches:

add rule ip6 test-ip6 input icmpv6 id 1:
 'icmpv6 id 1' mismatches
 'icmpv6 type { echo-request, echo-reply} icmpv6 parameter-problem 65536/16'

add rule ip6 test-ip6 input icmpv6 type echo-reply icmpv6 id 65534':
  'icmpv6 type echo-reply icmpv6 id 65534' mismatches
  'icmpv6 type echo-reply @th,32,16 65534'

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agopayload: do not remove icmp echo dependency
Florian Westphal [Tue, 15 Jun 2021 16:01:50 +0000 (18:01 +0200)] 
payload: do not remove icmp echo dependency

"icmp type echo-request icmp id 2" and "icmp id 2" are not the same,
the latter gains an implicit dependency on both echo-request and
echo-reply.

Change payload dependency tracking to not store dependency in case
the value type is ICMP(6)_ECHO(REPLY).

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agonetlink_delinearize: add missing icmp id/sequence support
Florian Westphal [Tue, 15 Jun 2021 16:01:49 +0000 (18:01 +0200)] 
netlink_delinearize: add missing icmp id/sequence support

Pablo reports following input and output:
in: icmpv6 id 1
out: icmpv6 type { echo-request, echo-reply } icmpv6 parameter-problem 65536/16

Reason is that icmp fields overlap, decoding of the correct name requires
check of the icmpv6 type.  This only works for equality tests, for
instance

in: icmpv6 type echo-request icmpv6 id 1
will be listed as "icmpv6 id 1" (which is not correct either, since the
input only matches on echo-request).

with this patch, output of 'icmpv6 id 1' is
icmpv6 type { echo-request, echo-reply } icmpv6 id 1

The second problem, the removal of a single check (request OR reply),
is resolved in the followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: shell: cover split chain reference across tables
Pablo Neira Ayuso [Mon, 14 Jun 2021 18:51:11 +0000 (20:51 +0200)] 
tests: shell: cover split chain reference across tables

Add a test to cover table T1 containing the definition of chain C1, and
table T1' (actually the same definition as T1) that contains a (jump)
reference to chain C1.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: extend connlimit test
Pablo Neira Ayuso [Fri, 4 Jun 2021 01:10:06 +0000 (03:10 +0200)] 
tests: shell: extend connlimit test

Extend existing test to add a ct count expression in the set definition.

This test cover the upstream kernel fix ad9f151e560b ("netfilter:
nf_tables: initialize set before expression setup").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: replace opencoded NFT_SET_ANONYMOUS set flag check by set_is_anonymous()
Pablo Neira Ayuso [Mon, 14 Jun 2021 12:57:25 +0000 (14:57 +0200)] 
src: replace opencoded NFT_SET_ANONYMOUS set flag check by set_is_anonymous()

Use set_is_anonymous() to check for the NFT_SET_ANONYMOUS set flag
instead.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: add xzalloc_array() and use it to allocate the expression hashtable
Pablo Neira Ayuso [Mon, 14 Jun 2021 12:47:47 +0000 (14:47 +0200)] 
src: add xzalloc_array() and use it to allocate the expression hashtable

Otherwise, assertion to ensure that no colission occur is hit due to
uninitialized hashtable memory area:

nft: netlink_delinearize.c:1741: expr_handler_init: Assertion `expr_handle_ht[hash] == NULL' failed.

Fixes: c4058f96c6a5 ("netlink_delinearize: Fix suspicious calloc() call")
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: add set to cache once
Pablo Neira Ayuso [Mon, 14 Jun 2021 12:05:09 +0000 (14:05 +0200)] 
evaluate: add set to cache once

67d3969a7244 ("evaluate: add set to the cache") re-adds the set into the
cache again.

This bug was hidden behind 5ec5c706d993 ("cache: add hashtable cache for
table") which broke set_evaluate() for anonymous sets.

Phil reported a gcc compilation warning which uncovered this problem.

Reported-by: Phil Sutter <phil@nwl.cc>
Fixes: 67d3969a7244 ("evaluate: add set to the cache")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: tests: add missing concat test case
Florian Westphal [Mon, 14 Jun 2021 13:01:41 +0000 (15:01 +0200)] 
json: tests: add missing concat test case

Fix
ERROR: did not find JSON equivalent for rule 'ip saddr . ip daddr { 192.0.2.1 . 10.0.0.1-10.0.0.2 }'

when running nft-test.py -j

Fixes: bbcc5eda7e58 ("evaluate: restore interval + concatenation in anonymous set")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: tests: fix vlan.t cfi test case
Florian Westphal [Mon, 14 Jun 2021 12:57:57 +0000 (14:57 +0200)] 
json: tests: fix vlan.t cfi test case

The test case exists twice, once with new dei, once with old 'cfi',
so we need to retain the cfi one in the json file too.

Else we get:
 WARNING: line 2: Wrote JSON equivalent for rule vlan id 4094 vlan dei 1

Fixes: d2fba515ff94 ("src: add vlan dei")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agorule: Fix for potential off-by-one in cmd_add_loc()
Phil Sutter [Fri, 11 Jun 2021 15:08:34 +0000 (17:08 +0200)] 
rule: Fix for potential off-by-one in cmd_add_loc()

Using num_attrs as index means it must be at max one less than the
array's size at function start.

Fixes: 27362a5bfa433 ("rule: larger number of error locations")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonetlink_delinearize: Fix suspicious calloc() call
Phil Sutter [Fri, 11 Jun 2021 15:02:01 +0000 (17:02 +0200)] 
netlink_delinearize: Fix suspicious calloc() call

Parameter passed to sizeof() was wrong. While being at it, replace the
whole call with xmalloc_array() which takes care of error checking.

Fixes: 913979f882d13 ("src: add expression handler hashtable")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonetlink: Avoid memleak in error path of netlink_delinearize_obj()
Phil Sutter [Fri, 11 Jun 2021 14:46:48 +0000 (16:46 +0200)] 
netlink: Avoid memleak in error path of netlink_delinearize_obj()

If parsing udata fails, 'obj' has to be freed before returning to
caller.

Fixes: 293c9b114faef ("src: add comment support for objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonetlink: Avoid memleak in error path of netlink_delinearize_table()
Phil Sutter [Fri, 11 Jun 2021 16:33:44 +0000 (18:33 +0200)] 
netlink: Avoid memleak in error path of netlink_delinearize_table()

If parsing udata fails, 'table' has to be freed before returning to
caller.

Fixes: c156232a530b3 ("src: add comment support when adding tables")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonetlink: Avoid memleak in error path of netlink_delinearize_chain()
Phil Sutter [Fri, 11 Jun 2021 16:32:13 +0000 (18:32 +0200)] 
netlink: Avoid memleak in error path of netlink_delinearize_chain()

If parsing udata fails, 'chain' has to be freed before returning to
caller.

Fixes: 702ac2b72c0e8 ("src: add comment support for chains")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonetlink: Avoid memleak in error path of netlink_delinearize_set()
Phil Sutter [Fri, 11 Jun 2021 14:43:21 +0000 (16:43 +0200)] 
netlink: Avoid memleak in error path of netlink_delinearize_set()

Duplicate string 'comment' later when the function does not fail
anymore.

Fixes: 0864c2d49ee8a ("src: add comment support for set declarations")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agojson: Drop pointless assignment in exthdr_expr_json()
Phil Sutter [Fri, 11 Jun 2021 14:23:22 +0000 (16:23 +0200)] 
json: Drop pointless assignment in exthdr_expr_json()

The updated value of 'is_exists' is no longer read at this point.

Fixes: cb21869649208 ("json: tcp: add raw tcp option match support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoevaluate: Mark fall through case in str2hooknum()
Phil Sutter [Fri, 11 Jun 2021 14:19:18 +0000 (16:19 +0200)] 
evaluate: Mark fall through case in str2hooknum()

It is certainly intentional, so just mark it as such.

Fixes: b4775dec9f80b ("src: ingress inet support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoparser_json: Fix for memleak in tcp option error path
Phil Sutter [Fri, 11 Jun 2021 14:07:02 +0000 (16:07 +0200)] 
parser_json: Fix for memleak in tcp option error path

If 'kind' value is invalid, the function returned without freeing 'expr'
first. Fix this by performing the check before allocation.

Fixes: cb21869649208 ("json: tcp: add raw tcp option match support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoparser_bison: Fix for implicit declaration of isalnum
Phil Sutter [Fri, 11 Jun 2021 14:03:32 +0000 (16:03 +0200)] 
parser_bison: Fix for implicit declaration of isalnum

Have to include ctype.h to make it known.

Fixes: e76bb37940181 ("src: allow for variables in the log prefix string")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoevaluate: restore interval + concatenation in anonymous set
Pablo Neira Ayuso [Fri, 11 Jun 2021 16:51:08 +0000 (18:51 +0200)] 
evaluate: restore interval + concatenation in anonymous set

Perform the table and set lookup only for non-anonymous sets, where the
incremental cache update is required.

The problem fixed by 7aa08d45031e ("evaluate: Perform set evaluation on
implicitly declared (anonymous) sets") resurrected after the cache
rework.

 # nft add rule x y tcp sport . tcp dport vmap { ssh . 0-65535 : accept, 0-65535 . ssh : accept }
 BUG: invalid range expression type concat
 nft: expression.c:1422: range_expr_value_low: Assertion `0' failed.
 Abort

Add a test case to make sure this does not happen again.

Fixes: 5ec5c706d993 ("cache: add hashtable cache for table")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: add vlan dei
Pablo Neira Ayuso [Fri, 11 Jun 2021 15:29:14 +0000 (17:29 +0200)] 
src: add vlan dei

the CFI bit has been repurposed as DEI "Drop Eligible Indicator"
since 802.1Q-2011.

The vlan cfi field is still retained for compatibility.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1516
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: add LISTING section
Florian Westphal [Wed, 12 May 2021 12:00:54 +0000 (14:00 +0200)] 
doc: add LISTING section

mention various 'nft list' options, such as secmarks, flow tables, and
so on.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add support for base hook dumping
Florian Westphal [Wed, 27 Jan 2021 13:29:50 +0000 (14:29 +0100)] 
src: add support for base hook dumping

Example output:
$ nft list hook ip input
family ip hook input {
        +0000000000 nft_do_chain_inet [nf_tables]       # nft table ip filter chain input
        +0000000010 nft_do_chain_inet [nf_tables]       # nft table ip firewalld chain filter_INPUT
        +0000000100 nf_nat_ipv4_local_in [nf_nat]
        +2147483647 ipv4_confirm [nf_conntrack]
}

$ nft list hooks netdev type ingress device lo
family netdev hook ingress device lo {
        +0000000000 nft_do_chain_netdev [nf_tables]
}

$ nft list hooks inet
family ip hook prerouting {
        -0000000400 ipv4_conntrack_defrag [nf_defrag_ipv4]
        -0000000300 iptable_raw_hook [iptable_raw]
        -0000000290 nft_do_chain_inet [nf_tables]       # nft table ip firewalld chain raw_PREROUTING
        -0000000200 ipv4_conntrack_in [nf_conntrack]
        -0000000140 nft_do_chain_inet [nf_tables]       # nft table ip firewalld chain mangle_PREROUTING
        -0000000100 nf_nat_ipv4_pre_routing [nf_nat]
}
...

'nft list hooks' will display everyting except the netdev family
via successive dump request for all family:hook combinations.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: add list cmd parser scope
Florian Westphal [Mon, 10 May 2021 15:59:06 +0000 (17:59 +0200)] 
scanner: add list cmd parser scope

Followup patch will add new 'hooks' keyword for
  nft list hooks

Add a scope for list to avoid exposure of the new keyword in nft
rulesets.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosegtree: Fix segfault when restoring a huge interval set
Phil Sutter [Wed, 9 Jun 2021 13:49:52 +0000 (15:49 +0200)] 
segtree: Fix segfault when restoring a huge interval set

Restoring a set of IPv4 prefixes with about 1.1M elements crashes nft as
set_to_segtree() exhausts the stack. Prevent this by allocating the
pointer array on heap and make sure it is freed before returning to
caller.

With this patch in place, restoring said set succeeds with allocation of
about 3GB of memory, according to valgrind.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agojson: Print warnings to stderr rather than stdout
Kerin Millar [Tue, 9 Mar 2021 15:28:45 +0000 (15:28 +0000)] 
json: Print warnings to stderr rather than stdout

Unsurprisingly, printing warnings to stdout results in malformed JSON.

Signed-off-by: Kerin Millar <kfm@plushkava.net>
Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1511
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink: quick sort array of devices
Pablo Neira Ayuso [Tue, 8 Jun 2021 12:48:20 +0000 (14:48 +0200)] 
netlink: quick sort array of devices

Provide an ordered list of devices for (netdev) chain and flowtable.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1525
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocmd: check for table mismatch first in error reporting
Pablo Neira Ayuso [Tue, 8 Jun 2021 11:53:43 +0000 (13:53 +0200)] 
cmd: check for table mismatch first in error reporting

If the fuzzy lookup provides a table, check if it is an inexact
matching, in that case, report that the table does not exist and provide
a mispelling suggestion for the non-existing table.

Initialize table to NULL since the fuzzy lookup might return no table
at all.

This patch fixes misleading error reporting:

 # nft delete chain xxx yyy
 Error: No such file or directory; did you mean chain ‘B’ in table ip ‘A’?
 delete chain xxx yyy
              ^^^

This refers to table 'xxx' but the suggestion refers to the chain instead.

Therefore, if the fuzzy lookup provides an exact matching table, then do
the fuzzy lookup for the next non-existing object (either chain, set,
...).

Fixes: 3a0e07106f66 ("src: combine extended netlink error reporting with mispelling support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agorule: rework CMD_OBJ_SETELEMS logic
Pablo Neira Ayuso [Sat, 5 Jun 2021 09:32:46 +0000 (11:32 +0200)] 
rule: rework CMD_OBJ_SETELEMS logic

Do not clone the set and zap the elements during the set and map
expansion to the CMD_OBJ_SETELEMS command.

Instead, update the CMD_OBJ_SET command to add the set to the kernel
(without elements) and let CMD_OBJ_SETELEMS add the elements. The
CMD_OBJ_SET command calls set_to_intervals() to update set->init->size
(NFTNL_SET_DESC_SIZE) before adding the set to the kernel. Updating the
set size from do_add_setelems() comes too late, it might result in
spurious ENFILE errors for interval sets.

Moreover, skip CMD_OBJ_SETELEMS if the set definition specifies no
elements.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1500
Fixes: c9eae091983a ("src: add CMD_OBJ_SETELEMS")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: add test case for removal of anon sets with only a single element
Florian Westphal [Wed, 26 May 2021 16:58:06 +0000 (18:58 +0200)] 
tests: add test case for removal of anon sets with only a single element

Also add a few examples that should not be changed:
- anon set with 2 elements
- anon map with 1 element
- anon set with a concatenation

The latter could be done with cmp but this currently triggers
'Error: Use concatenations with sets and maps, not singleton values'
after removing the anon set.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoevaluate: remove anon sets with exactly one element
Florian Westphal [Fri, 25 Jan 2019 15:50:23 +0000 (16:50 +0100)] 
evaluate: remove anon sets with exactly one element

Auto-replace lookups in single-element anon sets with a standard compare.

'add rule foo bar meta iif { "lo" }' gets replaced with
'add rule foo bar meta iif "lo"'.

The former is a set lookup, the latter is a comparision.
Comparisions are faster for the one-element case.

Only prefixes, ranges and values are handled at this time.

Anonymous maps are left alone, same for concatenations.

Concatenations could be handled, but it would require more work:
the concatenation would have to be replaced with a singleton value.
Evaluation step rejects concat RHS on a relational expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: remove redundant test cases
Florian Westphal [Thu, 3 Jun 2021 20:45:13 +0000 (22:45 +0200)] 
tests: remove redundant test cases

Check for
... 23-42 ...
... { 23-42 } ...

and remove the latter.  Followup patch will translate the former to the
latter during evaluation step to avoid the unneded anon set.

A separate test case will be added that checks for such rewrites.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests: ct: prefer normal cmp
Florian Westphal [Thu, 3 Jun 2021 21:37:29 +0000 (23:37 +0200)] 
tests: ct: prefer normal cmp

Followup patch will replace the { 1.2.3.4 } with single
cmp, so this will cause an error when the netlink dump gets
compared.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: missing catchall expression stub with ./configure --without-json
Pablo Neira Ayuso [Fri, 4 Jun 2021 14:55:04 +0000 (16:55 +0200)] 
json: missing catchall expression stub with ./configure --without-json

set_elem_catchall_expr_json undeclared here (not in a function); did you mean 'set_elem_catchall_expr_ops'?
 1344 |         .json           = set_elem_catchall_expr_json,
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                           set_elem_catchall_expr_ops

https://bugzilla.netfilter.org/show_bug.cgi?id=1542
Fixes: 5c2c6b092860 json: catchall element support
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: py: update netdev reject test file
Florian Westphal [Wed, 2 Jun 2021 18:47:34 +0000 (20:47 +0200)] 
tests: py: update netdev reject test file

netdev/reject.t throws a couple of WARNINGs. For some reason this file
wasn't updated after the reject statement json output was changed to
keep the icmp type/protocol.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agodoc: nft: ct id does not allow for original|reply
Pablo Neira Ayuso [Thu, 3 Jun 2021 23:42:22 +0000 (01:42 +0200)] 
doc: nft: ct id does not allow for original|reply

This is a singleton ct key, move it to where it belongs to.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agostatement: connlimit: remove extra whitespace in print function
Pablo Neira Ayuso [Wed, 2 Jun 2021 23:25:32 +0000 (01:25 +0200)] 
statement: connlimit: remove extra whitespace in print function

Instead of:

 ct count 2  accept
           ^^

simply print:

 ct count 2 accept

While at it, add incomplete reference 'ct count' to manpage.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agojson: catchall element support
Florian Westphal [Wed, 2 Jun 2021 18:38:46 +0000 (20:38 +0200)] 
json: catchall element support

Treat '*' as catchall element, not as a symbol.
Also add missing json test cases for wildcard set support.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agotests/py: fix error message
Florian Westphal [Wed, 2 Jun 2021 13:28:50 +0000 (15:28 +0200)] 
tests/py: fix error message

This should say "should have failed" only if the set add operation
was supposed to fail, not when its supposed to work.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: fix parse of flagcmp expression
Florian Westphal [Wed, 2 Jun 2021 11:39:39 +0000 (13:39 +0200)] 
json: fix parse of flagcmp expression

The json test case for the flagcmp notation ('tcp flags syn,fin / syn,fin') fails with:
command: {"nftables": [{"add": {"rule": {"family": "ip", "table": "test-ip4", "chain": "input", "expr": [{"match": {"left": {"&": [{"payload": {"field": "flags", "protocol": "tcp"}}, ["fin", "syn"]]}, "op": "==", "right": ["fin", "syn"]}}]}}}]}
internal:0:0-0: Error: List expression only allowed on RHS or in statement expression.
internal:0:0-0: Error: Failed to parse RHS of binop expression.
internal:0:0-0: Error: Invalid LHS of relational.
internal:0:0-0: Error: Parsing expr array at index 0 failed.
internal:0:0-0: Error: Parsing command array at index 0 failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agojson: fix base chain output
Florian Westphal [Wed, 2 Jun 2021 10:47:24 +0000 (12:47 +0200)] 
json: fix base chain output

nft-test.py -j fails with
python: json.c:243: chain_print_json: Assertion `__out' failed.

The member was changed from char * to a struct, pass the name again.

Fixes: 5008798157e2114f ("libnftables: location-based error reporting for chain type")
Signed-off-by: Florian Westphal <fw@strlen.de>
(cherry picked from commit cabe8992b3ee4eb0001a07075b317d966df6bcbd)

4 years agobuild: Bump version to v0.9.9 v0.9.9
Pablo Neira Ayuso [Tue, 25 May 2021 16:35:56 +0000 (18:35 +0200)] 
build: Bump version to v0.9.9

Update release name based on the Fearless Fosdick series: Prudence Pimpleton.

Bump dependencies on libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: tiny spelling fix in stateful object section s/an/a
Florian Westphal [Tue, 25 May 2021 15:12:20 +0000 (17:12 +0200)] 
doc: tiny spelling fix in stateful object section s/an/a

Quoting Duncan Roe:
  Tiny nit: suggest "by a unique" instead of "by an unique".
  "a" reads better to this native en-GB speaker at least.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoinclude: missing sctp_chunk.h in Makefile.am
Pablo Neira Ayuso [Tue, 25 May 2021 12:04:36 +0000 (14:04 +0200)] 
include: missing sctp_chunk.h in Makefile.am

Fix make distcheck.

Fixes: 0e3871cfd9a1 ("exthdr: Implement SCTP Chunk matching")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpression: display an error on unknown datatype
Pablo Neira Ayuso [Mon, 24 May 2021 20:05:02 +0000 (22:05 +0200)] 
expression: display an error on unknown datatype

 # nft describe foo
 datatype foo is invalid

Fixes: 21cbab5b6ffe ("expression: extend 'nft describe' to allow listing data types")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: allow == and != in the new shortcut syntax to match for flags
Pablo Neira Ayuso [Mon, 24 May 2021 19:54:36 +0000 (21:54 +0200)] 
evaluate: allow == and != in the new shortcut syntax to match for flags

The flags / mask syntax only allows for ==, !=  and the implicit
operation (which is == in this case).

 # nft add rule x y tcp flags ! syn / syn,ack
 Error: either == or != is allowed
 add rule x y tcp flags ! syn / syn,ack
              ^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agofiles: improve secmark.nft example
Dominick Grift [Mon, 24 May 2021 09:47:51 +0000 (11:47 +0200)] 
files: improve secmark.nft example

use proper priorities to ensure that ct works properly

Signed-off-by: Dominick Grift <dominick.grift@defensec.nl>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpr_postprocess: Avoid an unintended fall through
Phil Sutter [Thu, 20 May 2021 13:11:37 +0000 (15:11 +0200)] 
expr_postprocess: Avoid an unintended fall through

Parsing a range expression, the switch case fell through to prefix
expression case, thereby recursing once more for expr->left. This seems
not to have caused harm, but is certainly not intended.

Fixes: ee4391d0ac1e7 ("nat: transform range to prefix expression when possible")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agorule: skip exact matches on fuzzy lookup
Pablo Neira Ayuso [Wed, 19 May 2021 20:57:13 +0000 (22:57 +0200)] 
rule: skip exact matches on fuzzy lookup

The fuzzy lookup is exercised from the error path, when no object is
found. Remove branch that checks for exact matching since that should
not ever happen.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocmd: typo in chain fuzzy lookup
Pablo Neira Ayuso [Wed, 19 May 2021 20:27:42 +0000 (22:27 +0200)] 
cmd: typo in chain fuzzy lookup

Refer to chain, not table.

Error: No such file or directory; did you mean table ‘z’ in family ip?
add chain x y { type filter nat prerouting priority dstnat; }
            ^

It should say instead:

Error: No such file or directory; did you mean chain ‘z’ in table ip ‘x’?

[ Florian added args check for fmt to the netlink_io_error() prototype. ]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agolibnftables: location-based error reporting for chain type
Pablo Neira Ayuso [Wed, 19 May 2021 19:57:41 +0000 (21:57 +0200)] 
libnftables: location-based error reporting for chain type

Store the location of the chain type for better error reporting.

Several users that compile custom kernels reported that error
reporting is misleading when accidentally selecting
CONFIG_NFT_NAT=n.

After this patch, a better hint is provided:

 # nft 'add chain x y { type nat hook prerouting priority dstnat; }'
 Error: Could not process rule: No such file or directory
 add chain x y { type nat hook prerouting priority dstnat; }
                      ^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: nft.8: Extend monitor description by trace
Phil Sutter [Wed, 19 May 2021 11:12:48 +0000 (13:12 +0200)] 
doc: nft.8: Extend monitor description by trace

Briefly describe 'nft monitor trace' command functionality.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agodoc: document cgroupv2
Pablo Neira Ayuso [Wed, 19 May 2021 09:18:28 +0000 (11:18 +0200)] 
doc: document cgroupv2

This patch adds documentation for cgroupsv2 support.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexthdr: Implement SCTP Chunk matching
Phil Sutter [Tue, 4 May 2021 11:41:38 +0000 (13:41 +0200)] 
exthdr: Implement SCTP Chunk matching

Extend exthdr expression to support scanning through SCTP packet chunks
and matching on fixed fields' values.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
4 years agojson: Simplify non-tcpopt exthdr printing a bit
Phil Sutter [Tue, 4 May 2021 11:18:11 +0000 (13:18 +0200)] 
json: Simplify non-tcpopt exthdr printing a bit

This was just duplicate code apart from the object's name.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoscanner: sctp: Move to own scope
Phil Sutter [Tue, 4 May 2021 11:06:32 +0000 (13:06 +0200)] 
scanner: sctp: Move to own scope

This isolates only "vtag" token for now.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Florian Westphal <fw@strlen.de>
4 years agodatatype: skip cgroupv2 rootfs in listing
Pablo Neira Ayuso [Tue, 18 May 2021 12:31:58 +0000 (14:31 +0200)] 
datatype: skip cgroupv2 rootfs in listing

cgroupv2 path is expressed from the /sys/fs/cgroup folder, update
listing to skip it.

 # nft add rule x y socket cgroupv2 level 1 "user.slice" counter
 # nft list ruleset
 table ip x {
        chain y {
                type filter hook input priority filter; policy accept;
                socket cgroupv2 level 1 "user.slice" counter
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: use PRIu64 format
Pablo Neira Ayuso [Tue, 18 May 2021 11:45:05 +0000 (13:45 +0200)] 
src: use PRIu64 format

Fix the following compilation warnings on x86_32.

datatype.c: In function ‘cgroupv2_type_print’:
datatype.c:1387:22: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘uint64_t’ {aka ‘long long unsigned int’} [-Wformat=]
   nft_print(octx, "%lu", id);
                    ~~^   ~~
                    %llu

meta.c: In function ‘date_type_print’:
meta.c:411:21: warning: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘uint64_t’ {aka ‘long long unsigned int’} [-Wformat=]
  nft_print(octx, "%lu", tstamp);
                   ~~^   ~~~~~~
                   %llu

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: add shortcut syntax for matching flags without binary operations
Pablo Neira Ayuso [Wed, 12 May 2021 23:34:01 +0000 (01:34 +0200)] 
parser_bison: add shortcut syntax for matching flags without binary operations

This patch adds the following shortcut syntax:

expression flags / flags

instead of:

expression and flags == flags

For example:

tcp flags syn,ack / syn,ack,fin,rst
                  ^^^^^^^   ^^^^^^^^^^^^^^^
                   value         mask

instead of:

tcp flags and (syn|ack|fin|rst) == syn|ack

The second list of comma-separated flags represents the mask which are
examined and the first list of comma-separated flags must be set.

You can also use the != operator with this syntax:

tcp flags != fin,rst / syn,ack,fin,rst

This shortcut is based on the prefix notation, but it is also similar to
the iptables tcp matching syntax.

This patch introduces the flagcmp expression to print the tcp flags in
this new notation. The delinearize path transforms the binary expression
to this new flagcmp expression whenever possible.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: check errno before invoking cache_release()
Marco Oliverio [Thu, 13 May 2021 14:10:32 +0000 (16:10 +0200)] 
cache: check errno before invoking cache_release()

if genid changes during cache_init(), check_genid() sets errno to EINTR to force
a re-init of the cache.

cache_release() may inadvertly change errno by calling free().  Indeed free()
may invoke madvise() that changes errno to ENOSYS on system where kernel is
configured without support for this syscall.

Signed-off-by: Marco Oliverio <marco.oliverio@tanaza.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonetlink_delinearize: fix binary operation postprocessing with sets
Pablo Neira Ayuso [Wed, 12 May 2021 23:42:13 +0000 (01:42 +0200)] 
netlink_delinearize: fix binary operation postprocessing with sets

If the right-hand side expression of the binary expression is a set,
then, skip the postprocessing step otherwise the tests/py report the
following warning:

 # ./nft-test.py inet/tcp.t
 inet/tcp.t: WARNING: line 80: 'add rule ip test-ip4 input tcp flags & (syn|fin) == (syn|fin)': 'tcp flags & (fin | syn) == fin | syn' mismatches 'tcp flags ! fin,syn'
 inet/tcp.t: WARNING: line 83: 'add rule ip test-ip4 input tcp flags & (fin | syn | rst | psh | ack | urg) == { fin, ack, psh | ack, fin | psh | ack }': 'tcp flags & (fin | syn | rst | psh | ack | urg) == { fin, ack, psh | ack, fin | psh | ack }' mismatches 'tcp flags ! fin,syn,rst,psh,ack,urg'

This listing is not correct.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: don't assume fixed handle value in cache/0008_delete_by_handle_0
Pablo Neira Ayuso [Wed, 12 May 2021 14:19:56 +0000 (16:19 +0200)] 
tests: shell: don't assume fixed handle value in cache/0008_delete_by_handle_0

This test is occasionally reporting warning in one of my test boxes.
Update this test to extract the handle from ruleset listing, use
rudimentary invocation of the cut command to work around this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: don't crash on set definition with incorrect datatype
Pablo Neira Ayuso [Mon, 10 May 2021 16:52:47 +0000 (18:52 +0200)] 
evaluate: don't crash on set definition with incorrect datatype

Cache updates have resurrected the bug described in 5afa5a164ff1
("evaluate: check for NULL datatype in rhs in lookup expr").

This is triggered by testcases/cache/0008_delete_by_handle_0.

Fixes: df48e56e987f ("cache: add hashtable cache for sets")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: add set element catch-all support
Pablo Neira Ayuso [Mon, 10 May 2021 16:52:45 +0000 (18:52 +0200)] 
src: add set element catch-all support

Add a catchall expression (EXPR_SET_ELEM_CATCHALL).

Use the asterisk (*) to represent the catch-all set element, e.g.

 table x {
     set y {
type ipv4_addr
counter
elements = { 1.2.3.4 counter packets 0 bytes 0, * counter packets 0 bytes 0 }
     }
 }

Special handling for segtree: zap the catch-all element from the set
element list and re-add it after processing.

Remove wildcard_expr deadcode in src/parser_bison.y

This patch also adds several tests for the tests/py and tests/shell
infrastructures.

Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser_bison: add set_elem_key_expr rule
Pablo Neira Ayuso [Mon, 10 May 2021 16:52:40 +0000 (18:52 +0200)] 
parser_bison: add set_elem_key_expr rule

Add a rule to specify the set key expression in preparation for the
catch-all element support.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: Reduce size of NAT statement synopsis
Phil Sutter [Thu, 6 May 2021 08:12:45 +0000 (10:12 +0200)] 
doc: Reduce size of NAT statement synopsis

Introduce non-terminals representing address and port which may
represent ranges as well. Combined with dropping the distinction between
PR_FLAGS and PRF_FLAGS, all the lines for each nat statement type can be
combined.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: Introduce 0043_concatenated_ranges_1 for subnets of different sizes
Stefano Brivio [Wed, 5 May 2021 22:23:14 +0000 (00:23 +0200)] 
tests: Introduce 0043_concatenated_ranges_1 for subnets of different sizes

The report from https://bugzilla.netfilter.org/show_bug.cgi?id=1520
showed a display issue with particular IPv6 mask lengths in elements
of sets with concatenations. Make sure we cover insertion and listing
of different mask lengths in concatenated set elements for IPv4 and
IPv6.

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosegtree: Fix range_mask_len() for subnet ranges exceeding unsigned int
Stefano Brivio [Wed, 5 May 2021 22:23:13 +0000 (00:23 +0200)] 
segtree: Fix range_mask_len() for subnet ranges exceeding unsigned int

As concatenated ranges are fetched from kernel sets and displayed to
the user, range_mask_len() evaluates whether the range is suitable for
display as netmask, and in that case it calculates the mask length by
right-shifting the endpoints until no set bits are left, but in the
existing version the temporary copies of the endpoints are derived by
copying their unsigned int representation, which doesn't suffice for
IPv6 netmask lengths, in general.

PetrB reports that, after inserting a /56 subnet in a concatenated set
element, it's listed as a /64 range. In fact, this happens for any
IPv6 mask shorter than 64 bits.

Fix this issue by simply sourcing the range endpoints provided by the
caller and setting the temporary copies with mpz_init_set(), instead
of fetching the unsigned int representation. The issue only affects
displaying of the masks, setting elements already works as expected.

Reported-by: PetrB <petr.boltik@gmail.com>
Bugzilla: https://bugzilla.netfilter.org/show_bug.cgi?id=1520
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add cgroupsv2 support
Pablo Neira Ayuso [Tue, 20 Apr 2021 23:26:46 +0000 (01:26 +0200)] 
src: add cgroupsv2 support

Add support for matching on the cgroups version 2.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: remove object from cache on delete object command
Pablo Neira Ayuso [Thu, 29 Apr 2021 16:15:08 +0000 (18:15 +0200)] 
evaluate: remove object from cache on delete object command

Update the cache to remove this object from the evaluation phase.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: remove flowtable from cache on delete flowtable command
Pablo Neira Ayuso [Thu, 29 Apr 2021 16:07:34 +0000 (18:07 +0200)] 
evaluate: remove flowtable from cache on delete flowtable command

Update the cache to remove this flowtable from the evaluation phase.
Add flowtable_cache_del() function for this purpose.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: remove set from cache on delete set command
Pablo Neira Ayuso [Thu, 29 Apr 2021 15:49:25 +0000 (17:49 +0200)] 
evaluate: remove set from cache on delete set command

Update the cache to remove this set from the evaluation phase.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: remove chain from cache on delete chain command
Pablo Neira Ayuso [Thu, 29 Apr 2021 15:41:25 +0000 (17:41 +0200)] 
evaluate: remove chain from cache on delete chain command

Update the cache to remove this chain from the evaluation phase. Add
chain_cache_del() function for this purpose.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: add hashtable cache for table
Pablo Neira Ayuso [Thu, 29 Apr 2021 20:23:05 +0000 (22:23 +0200)] 
cache: add hashtable cache for table

Add a hashtable for fast table lookups.

Tables that reside in the cache use the table->cache_hlist and
table->cache_list heads.

Table that are created from command line / ruleset are also added
to the cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: add object to the cache
Pablo Neira Ayuso [Thu, 15 Apr 2021 12:00:26 +0000 (14:00 +0200)] 
evaluate: add object to the cache

If the cache does not contain this object that is defined in this batch,
add it to the cache. This allows for references to this new object in
the same batch.

This patch also adds missing handle_merge() to set the object name,
otherwise object name is NULL and obj_cache_find() crashes.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: missing table cache for several policy objects
Pablo Neira Ayuso [Thu, 15 Apr 2021 12:00:22 +0000 (14:00 +0200)] 
cache: missing table cache for several policy objects

Populate the cache with tables for several policy objects types.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: add flowtable to the cache
Pablo Neira Ayuso [Thu, 15 Apr 2021 12:00:20 +0000 (14:00 +0200)] 
evaluate: add flowtable to the cache

If the cache does not contain this flowtable that is defined in this
batch, then add it to the cache. This allows for references to this new
flowtable in the same batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoevaluate: add set to the cache
Pablo Neira Ayuso [Thu, 15 Apr 2021 12:00:16 +0000 (14:00 +0200)] 
evaluate: add set to the cache

If the cache does not contain the set that is defined in this batch, add
it to the cache. This allows for references to this new set in the same
batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: add set_cache_del() and use it
Pablo Neira Ayuso [Thu, 15 Apr 2021 13:06:07 +0000 (15:06 +0200)] 
cache: add set_cache_del() and use it

Update set_cache_del() from the monitor path to remove sets
in the cache.

Fixes: df48e56e987f ("cache: add hashtable cache for sets")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: add hashtable cache for flowtable
Pablo Neira Ayuso [Thu, 29 Apr 2021 20:19:07 +0000 (22:19 +0200)] 
cache: add hashtable cache for flowtable

Add flowtable hashtable cache.

Actually I am not expecting that many flowtables to benefit from the
hashtable to be created by streamline this code with tables, chains,
sets and policy objects.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: add hashtable cache for object
Pablo Neira Ayuso [Thu, 29 Apr 2021 20:09:15 +0000 (22:09 +0200)] 
cache: add hashtable cache for object

This patch adds a hashtable for object lookups.

This patch also splits table->objs in two:

- Sets that reside in the cache are stored in the new
  tables->cache_obj and tables->cache_obj_ht.

- Set that defined via command line / ruleset file reside in
  tables->obj.

Sets in the cache (already in the kernel) are not placed in the
table->objs list.

By keeping separated lists, objs defined via command line / ruleset file
can be added to cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: consolidate object cache infrastructure
Pablo Neira Ayuso [Thu, 29 Apr 2021 19:55:34 +0000 (21:55 +0200)] 
src: consolidate object cache infrastructure

This patch consolidates the object cache infrastructure. Update set and
chains to use it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: consolidate nft_cache infrastructure
Pablo Neira Ayuso [Thu, 29 Apr 2021 18:29:09 +0000 (20:29 +0200)] 
src: consolidate nft_cache infrastructure

- prepend nft_ prefix to nft_cache API and internal functions
- move declarations to cache.h (and remove redundant declarations)
- move struct nft_cache definition to cache.h

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: pass chain name to chain_cache_find()
Pablo Neira Ayuso [Thu, 29 Apr 2021 18:04:55 +0000 (20:04 +0200)] 
src: pass chain name to chain_cache_find()

You can identify chains through the unique handle in deletions, update
this interface to take a string instead of the handle to prepare for
the introduction of 64-bit handle chain lookups.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agorule: skip fuzzy lookup for unexisting 64-bit handle
Pablo Neira Ayuso [Thu, 29 Apr 2021 23:01:17 +0000 (01:01 +0200)] 
rule: skip fuzzy lookup for unexisting 64-bit handle

Deletion by handle, if incorrect, should not exercise the misspell
lookup functions.

Fixes: 3a0e07106f66 ("src: combine extended netlink error reporting with mispelling support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>