]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
4 years agonetlink: don't crash when set elements are not evaluated as expected
Florian Westphal [Tue, 30 Mar 2021 23:26:19 +0000 (01:26 +0200)] 
netlink: don't crash when set elements are not evaluated as expected

define foo = 2001:db8:123::/48

table inet filter {
set foo {
typeof ip6 saddr
elements = $foo
}
}

gives crash.  This now exits with:

stdin:1:14-30: Error: Unexpected initial set type prefix
define foo = 2001:db8:123::/48
             ^^^^^^^^^^^^^^^^^

For literals, bison parser protects us, as it enforces
'elements = { 2001:... '.

For 'elements = $foo' we can't detect it at parsing stage as the '$foo'
symbol might as well evaluate to "{ 2001, ...}" (i.e. we can't do a
set element allocation).

So at least detect this from set instantiaton.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoparser_bison: simplify flowtable offload flag parser
Pablo Neira Ayuso [Wed, 31 Mar 2021 14:14:03 +0000 (16:14 +0200)] 
parser_bison: simplify flowtable offload flag parser

Remove ft_flags_spec rule.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agomnl: do not set flowtable flags twice
Pablo Neira Ayuso [Wed, 31 Mar 2021 14:07:13 +0000 (16:07 +0200)] 
mnl: do not set flowtable flags twice

Flags are already set on from mnl_nft_flowtable_add(), remove duplicated
code.

Fixes: e6cc9f37385 ("nftables: add flags offload to flowtable")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agorule: remove semicolon in flowtable offload
Pablo Neira Ayuso [Thu, 25 Mar 2021 12:06:02 +0000 (13:06 +0100)] 
rule: remove semicolon in flowtable offload

opts->stmt_separator already prints the semicolon when needed.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser: fix scope closure of COUNTER token
Florian Westphal [Thu, 25 Mar 2021 09:34:40 +0000 (10:34 +0100)] 
parser: fix scope closure of COUNTER token

It is closed after allocation, which is too early: this
stopped 'packets' and 'bytes' from getting parsed correctly.

Also add a test case for this.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agosrc: add datatype->describe()
Pablo Neira Ayuso [Wed, 24 Mar 2021 16:19:32 +0000 (17:19 +0100)] 
src: add datatype->describe()

As an alternative to print the datatype values when no symbol table is
available. Use it to print protocols available via getprotobynumber()
which actually refers to /etc/protocols.

Not very efficient, getprotobynumber() causes a series of open()/close()
calls on /etc/protocols, but this is called from a non-critical path.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1503
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agonftables: add flags offload to flowtable
Frank Wunderlich [Sun, 21 Mar 2021 16:49:16 +0000 (17:49 +0100)] 
nftables: add flags offload to flowtable

allow flags (currently only offload) in flowtables like it is stated
here: https://lwn.net/Articles/804384/

tested on mt7622/Bananapi-R64

table ip filter {
flowtable f {
hook ingress priority filter + 1
devices = { lan3, lan0, wan }
flags offload;
}

chain forward {
type filter hook forward priority filter; policy accept;
ip protocol { tcp, udp } flow add @f
}
}

table ip nat {
chain post {
type nat hook postrouting priority filter; policy accept;
oifname "wan" masquerade
}
}

Signed-off-by: Frank Wunderlich <frank-w@public-files.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: no need to define a set in ct state
Pablo Neira Ayuso [Wed, 24 Mar 2021 16:54:33 +0000 (17:54 +0100)] 
doc: no need to define a set in ct state

ct state are flags, no need to define a set for this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodoc: use symbolic names for chain priorities
Simon Ruderich [Tue, 9 Mar 2021 10:53:30 +0000 (11:53 +0100)] 
doc: use symbolic names for chain priorities

This replaces the numbers with the matching symbolic names with one
exception: The NAT example used "priority 0" for the prerouting
priority. This is replaced by "dstnat" which has priority -100 which is
the new recommended priority.

Also use spaces instead of tabs for consistency in lines which require
updates.

Signed-off-by: Simon Ruderich <simon@ruderich.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: fix 0025empty_dynset_0
Pablo Neira Ayuso [Wed, 24 Mar 2021 12:36:14 +0000 (13:36 +0100)] 
tests: shell: fix 0025empty_dynset_0

Use bash, otherwise it reports here:

testcases/nft-f/0025empty_dynset_0: 22: Syntax error: redirection unexpected

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: flowtable add after delete in batch
Pablo Neira Ayuso [Wed, 17 Mar 2021 19:50:12 +0000 (20:50 +0100)] 
tests: shell: flowtable add after delete in batch

Check for bogus EEXIST and EBUSY errors.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosegtree: release single element already contained in an interval
Pablo Neira Ayuso [Tue, 16 Mar 2021 23:44:09 +0000 (00:44 +0100)] 
segtree: release single element already contained in an interval

Before this patch:

 table ip x {
        chain y {
                ip saddr { 1.1.1.1-1.1.1.2, 1.1.1.1 }
        }
 }

results in:

 table ip x {
        chain y {
                ip saddr { 1.1.1.1 }
        }
 }

due to incorrect interval merge logic.

If the element 1.1.1.1 is already contained in an existing interval
1.1.1.1-1.1.1.2, release it.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1512
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoparser: add missing scope_close annotation for RT keyword
Florian Westphal [Wed, 24 Mar 2021 11:07:05 +0000 (12:07 +0100)] 
parser: add missing scope_close annotation for RT keyword

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: log: move to own scope
Florian Westphal [Tue, 16 Mar 2021 23:40:36 +0000 (00:40 +0100)] 
scanner: log: move to own scope

GROUP and PREFIX are used by igmp and nat, so they can't be moved out of
INITIAL scope yet.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: counter: move to own scope
Florian Westphal [Tue, 16 Mar 2021 23:40:35 +0000 (00:40 +0100)] 
scanner: counter: move to own scope

move bytes/packets away from initial state.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: add support for scope nesting
Florian Westphal [Tue, 16 Mar 2021 23:40:34 +0000 (00:40 +0100)] 
scanner: add support for scope nesting

Adding a COUNTER scope introduces parsing errors.  Example:

add rule  ... counter ip saddr 1.2.3.4

This is supposed to be

    COUNTER IP SADDR SYMBOL

but it will be parsed as

    COUNTER IP STRING SYMBOL

... and rule fails with unknown saddr.
This is because IP state change gets popped right after it was pushed.

bison parser invokes scanner_pop_start_cond() helper via
'close_scope_counter' rule after it has processed the entire 'counter' rule.
But that happens *after* flex has executed the 'IP' rule.

IOW, the sequence of events is not the exepcted
"COUNTER close_scope_counter IP SADDR SYMBOL close_scope_ip", it is
"COUNTER IP close_scope_counter".

close_scope_counter pops the just-pushed SCANSTATE_IP and returns the
scanner to SCANSTATE_COUNTER, so next input token (saddr) gets parsed
as a string, which gets then rejected from bison.

To resolve this, defer the pop operation until the current state is done.
scanner_pop_start_cond() already gets the scope that it has been
completed as an argument, so we can compare it to the active state.

If those are not the same, just defer the pop operation until the
bison reports its done with the active flex scope.

This leads to following sequence of events:
  1. flex switches to SCANSTATE_COUNTER
  2. flex switches to SCANSTATE_IP
  3. bison calls scanner_pop_start_cond(SCANSTATE_COUNTER)
  4. flex remains in SCANSTATE_IP, bison continues
  5. bison calls scanner_pop_start_cond(SCANSTATE_IP) once the entire
     ip rule has completed: this pops both IP and COUNTER.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: avoid -fasan heap overflow warnings
Florian Westphal [Thu, 18 Mar 2021 16:31:30 +0000 (17:31 +0100)] 
scanner: avoid -fasan heap overflow warnings

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: secmark: move to own scope
Florian Westphal [Thu, 11 Mar 2021 13:23:13 +0000 (14:23 +0100)] 
scanner: secmark: move to own scope

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoscanner: move until,over,used keywords away from init state
Florian Westphal [Thu, 11 Mar 2021 13:23:12 +0000 (14:23 +0100)] 
scanner: move until,over,used keywords away from init state

Only applicable for limit and quota. "ct count" also needs 'over'.

Signed-off-by: Florian Westphal <fw@strlen.de>
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>