]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
4 years agoscanner: quota: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:11 +0000 (14:23 +0100)] 
scanner: quota: move to own scope

... and move "used" keyword to it.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: limit: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:10 +0000 (14:23 +0100)] 
scanner: limit: move to own scope

Moves rate and burst out of INITIAL.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: vlan: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:09 +0000 (14:23 +0100)] 
scanner: vlan: move to own scope

ID needs to remain exposed as its used by ct, icmp, icmp6 and so on.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: remove saddr/daddr from initial state
Florian Westphal [Thu, 11 Mar 2021 13:23:08 +0000 (14:23 +0100)] 
scanner: remove saddr/daddr from initial state

This can now be reduced to expressions that can expect saddr/daddr tokens.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: arp: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:07 +0000 (14:23 +0100)] 
scanner: arp: move to own scope

allows to move the arp specific tokens out of the INITIAL scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: add ether scope
Florian Westphal [Thu, 11 Mar 2021 13:23:06 +0000 (14:23 +0100)] 
scanner: add ether scope

just like previous change: useless as-is, but prepares
for removal of saddr/daddr from INITIAL scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: add fib scope
Florian Westphal [Thu, 11 Mar 2021 13:23:05 +0000 (14:23 +0100)] 
scanner: add fib scope

makes no sense as-is because all keywords need to stay
in the INITIAL scope.

This can be changed after all saddr/daddr users have been scoped.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: ip6: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:04 +0000 (14:23 +0100)] 
scanner: ip6: move to own scope

move flowlabel and hoplimit.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: ip: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:03 +0000 (14:23 +0100)] 
scanner: ip: move to own scope

Move the ip option names (rr, lsrr, ...) out of INITIAL scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: ct: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:02 +0000 (14:23 +0100)] 
scanner: ct: move to own scope

This allows moving multiple ct specific keywords out of INITIAL scope.
Next few patches follow same pattern:
 1. add a scope_close_XXX rule
 2. add a SCANSTATE_XXX & make flex switch to it when
    encountering XXX keyword
 3. make bison leave SCANSTATE_XXXX when it has seen the complete
    expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: move remaining cache functions in rule.c to cache.c
Pablo Neira Ayuso [Thu, 11 Mar 2021 12:34:10 +0000 (13:34 +0100)] 
src: move remaining cache functions in rule.c to cache.c

Move all the cache logic to src/cache.c

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoscanner: socket: move to own scope
Florian Westphal [Mon, 8 Mar 2021 17:18:37 +0000 (18:18 +0100)] 
scanner: socket: move to own scope

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: rt: move to own scope
Florian Westphal [Mon, 8 Mar 2021 17:18:36 +0000 (18:18 +0100)] 
scanner: rt: move to own scope

classid and nexthop can be moved out of INIT scope.
Rest are still needed because tehy are used by other expressions as
well.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: ipsec: move to own scope
Florian Westphal [Mon, 8 Mar 2021 17:18:35 +0000 (18:18 +0100)] 
scanner: ipsec: move to own scope

... and hide the ipsec specific tokens from the INITITAL scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: queue: move to own scope
Florian Westphal [Mon, 8 Mar 2021 17:18:34 +0000 (18:18 +0100)] 
scanner: queue: move to own scope

allows to remove 3 queue specific keywords from INITIAL scope.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: introduce start condition stack
Florian Westphal [Mon, 8 Mar 2021 17:18:33 +0000 (18:18 +0100)] 
scanner: introduce start condition stack

Add a small initial chunk of flex start conditionals.

This starts with two low-hanging fruits, numgen and j/symhash.

NUMGEN and HASH start conditions are entered from flex when
the corresponding expression token is encountered.

Flex returns to the INIT condition when the bison parser
has seen a complete numgen/hash statement.

This intentionally uses a stack rather than BEGIN()
to eventually support nested states.

The scanner_pop_start_cond() function argument is not used yet, but
will need to be used later to deal with nesting.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: remove unused tokens
Florian Westphal [Mon, 8 Mar 2021 17:18:32 +0000 (18:18 +0100)] 
scanner: remove unused tokens

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agonftables: xt: fix misprint in nft_xt_compatible_revision
Pavel Tikhomirov [Tue, 9 Mar 2021 15:09:15 +0000 (18:09 +0300)] 
nftables: xt: fix misprint in nft_xt_compatible_revision

The rev variable is used here instead of opt obviously by mistake.
Please see iptables:nft_compatible_revision() for an example how it
should be.

This breaks revision compatibility checks completely when reading
compat-target rules from nft utility. That's why nftables can't work on
"old" kernels which don't support new revisons. That's a problem for
containers.

E.g.: 0 and 1 is supported but not 2:
https://git.sw.ru/projects/VZS/repos/vzkernel/browse/net/netfilter/xt_nat.c#111

Reproduce of the problem on Virtuozzo 7 kernel
3.10.0-1160.11.1.vz7.172.18 in centos 8 container:

  iptables-nft -t nat -N TEST
  iptables-nft -t nat -A TEST -j DNAT --to-destination 172.19.0.2
  nft list ruleset > nft.ruleset
  nft -f - < nft.ruleset
  #/dev/stdin:19:67-81: Error: Range has zero or negative size
  # meta l4proto tcp tcp dport 81 counter packets 0 bytes 0 dnat to 3.0.0.0-0.0.0.0
  #                                                                 ^^^^^^^^^^^^^^^

  nft -v
  #nftables v0.9.3 (Topsy)
  iptables-nft -v
  #iptables v1.8.7 (nf_tables)

Kernel returns ip range in rev 0 format:

  crash> p *((struct nf_nat_ipv4_multi_range_compat *) 0xffff8ca2fabb3068)
  $5 = {
    rangesize = 1,
    range = {{
        flags = 3,
        min_ip = 33559468,
        max_ip = 33559468,

But nft reads this as rev 2 format (nf_nat_range2) which does not have
rangesize, and thus flugs 3 is treated as ip 3.0.0.0, which is wrong and
can't be restored later.

(Should probably be the same on Centos 7 kernel 3.10.0-1160.11.1)

Fixes: fbc0768cb696 ("nftables: xt: don't use hard-coded AF_INET")
Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests/py: Fix for missing JSON equivalent in any/ct.t.json
Phil Sutter [Mon, 8 Mar 2021 14:43:23 +0000 (15:43 +0100)] 
tests/py: Fix for missing JSON equivalent in any/ct.t.json

JSON equivalent for recently added test of the '!' shortcut was missing.

Fixes: e6c32b2fa0b82 ("src: add negation match on singleton bitmask value")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agomnl: Set NFTNL_SET_DATA_TYPE before dumping set elements
Phil Sutter [Thu, 4 Feb 2021 01:20:23 +0000 (02:20 +0100)] 
mnl: Set NFTNL_SET_DATA_TYPE before dumping set elements

In combination with libnftnl's commit "set_elem: Fix printing of verdict
map elements", This adds the vmap target to netlink dumps. Adjust dumps
in tests/py accordingly.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests/py: Adjust payloads for fixed nat statement dumps
Phil Sutter [Tue, 29 Dec 2020 17:39:30 +0000 (18:39 +0100)] 
tests/py: Adjust payloads for fixed nat statement dumps

Libnftnl no longer dumps unused regs, so drop those.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agodoc: move drop rule on a separate line in blackhole example
Simon Ruderich [Sun, 7 Mar 2021 09:51:36 +0000 (10:51 +0100)] 
doc: move drop rule on a separate line in blackhole example

At first I overlooked the "drop". Putting it on a separate line makes it
more visible and also details the separate steps of this rule.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: remove duplicate tables in synproxy example
Simon Ruderich [Sun, 7 Mar 2021 09:51:35 +0000 (10:51 +0100)] 
doc: remove duplicate tables in synproxy example

The "outcome ruleset" is the same as the two tables in the example.
Don't duplicate this information which just wastes space in the
documentation and can confuse the reader (it took me a while to realize
the tables are the same).

In addition, use the same table name for both tables to make it clear
that they can be the same. They will be merged in the resulting ruleset.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: add * to include example to actually include files
Simon Ruderich [Sun, 7 Mar 2021 09:51:34 +0000 (10:51 +0100)] 
doc: add * to include example to actually include files

"/etc/firewall/rules/" causes no error but also doesn't include any
files contained in the directory.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser: compact ct obj list types
Florian Westphal [Thu, 4 Mar 2021 01:07:35 +0000 (02:07 +0100)] 
parser: compact ct obj list types

Add new ct_cmd_type and avoid copypaste of the ct cmd_list rules.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: compact map RHS type
Florian Westphal [Thu, 4 Mar 2021 01:07:34 +0000 (02:07 +0100)] 
parser: compact map RHS type

Similar to previous patch, we can avoid duplication.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser: squash duplicated spec/specid rules
Florian Westphal [Thu, 4 Mar 2021 01:07:33 +0000 (02:07 +0100)] 
parser: squash duplicated spec/specid rules

No need to have duplicate CMD rules for spec and specid: add and use
a common rule for those cases.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoexpression: memleak in verdict_expr_parse_udata()
Pablo Neira Ayuso [Fri, 5 Mar 2021 19:36:31 +0000 (20:36 +0100)] 
expression: memleak in verdict_expr_parse_udata()

Remove unnecessary verdict_expr_alloc() invocation.

Fixes: 4ab1e5e60779 ("src: allow use of 'verdict' in typeof definitions")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agocache: memleak list of chain
Pablo Neira Ayuso [Tue, 2 Mar 2021 11:40:27 +0000 (12:40 +0100)] 
cache: memleak list of chain

Release chain list from the error path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomnl: remove nft_mnl_socket_reopen()
Pablo Neira Ayuso [Tue, 2 Mar 2021 11:35:20 +0000 (12:35 +0100)] 
mnl: remove nft_mnl_socket_reopen()

nft_mnl_socket_reopen() was introduced to deal with the EINTR case.
By reopening the netlink socket, pending netlink messages that are part of
a stale netlink dump are implicitly drop. This patch replaces the
nft_mnl_socket_reopen() strategy by pulling out all of the remaining
netlink message to restart in a clean state.

This is implicitly fixing up a bug in the table ownership support, which
assumes that the netlink socket remains open until nft_ctx_free() is
invoked.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotable: support for the table owner flag
Pablo Neira Ayuso [Sat, 20 Feb 2021 15:18:03 +0000 (16:18 +0100)] 
table: support for the table owner flag

Add new flag to allow userspace process to own tables: Tables that have
an owner can only be updated/destroyed by the owner. The table is
destroyed either if the owner process calls nft_ctx_free() or owner
process is terminated (implicit table release).

The ruleset listing includes the program name that owns the table:

 nft> list ruleset
 table ip x { # progname nft
        flags owner

        chain y {
                type filter hook input priority filter; policy accept;
                counter packets 1 bytes 309
        }
 }

Original code to pretty print the netlink portID to program name has
been extracted from the conntrack userspace utility.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotable: rework flags printing
Pablo Neira Ayuso [Mon, 22 Feb 2021 14:44:35 +0000 (15:44 +0100)] 
table: rework flags printing

Simplify routine to print the table flags. Add table_flag_name() and use
it from json too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser: re-enable support for concatentation on map RHS
Florian Westphal [Tue, 23 Feb 2021 11:12:40 +0000 (12:12 +0100)] 
parser: re-enable support for concatentation on map RHS

"typeof .... : ip saddr . tcp dport" is legal.

This makes 'testcases/maps/nat_addr_port' pass again.

Fixes: 4ab1e5e6077918 ("src: allow use of 'verdict' in typeof definitions")
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: allow use of 'verdict' in typeof definitions
Florian Westphal [Sat, 30 Jan 2021 18:58:42 +0000 (19:58 +0100)] 
src: allow use of 'verdict' in typeof definitions

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

  typeof iifname . ip protocol . th dport : verdic

... will fail.

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

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

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

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

Long options were missing the double dash.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Add a test to cover multi-statement support.

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

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

   icmp code 0 icmp id 44380 ..

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

  icmp type 8 code 0 id ...

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

This patch provides a shortcut for:

ct status and dnat == 0

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

  # nft add rule x y ct status ! dnat counter

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

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

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

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

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

nft crashes on restore.

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

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

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

This is just basic housekeeping:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Bump dependencies on libnftnl.

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

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

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

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

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

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

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

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

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

Instead of the alternate interface [1].

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

This is the reproducer:

 # nft -i << EOF
 list ruleset
 EOF

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

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

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

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

Extend cli to support for libedit readline shim code:

./configure --with-cli=editline

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

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

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

This patch adds two tests to add multistatement support:

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

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

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

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

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

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

Extend the set element infrastructure to support for several statements.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Check this is properly demangled.

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

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

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

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

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

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

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

nft will now auto-insert a icmpv6 type match.

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

Extend the earlier commit to also cover icmpv6.

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

after previous change nft will insert explicit icmp type match.

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

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

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

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

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

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

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

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

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

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

Call close_scope() from chain_block_alloc only.

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

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

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

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

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

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

Add an assignment check for json_echo to avoid reassigning it.

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

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

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

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

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

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

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1464
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>