]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
23 months agobuild: Bump version to 1.0.3 v1.0.3
Pablo Neira Ayuso [Tue, 31 May 2022 08:21:44 +0000 (10:21 +0200)] 
build: Bump version to 1.0.3

Still requires libnftnl 1.2.1

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agonft: simplify chain lookup in do_list_chain
Chander Govindarajan [Wed, 25 May 2022 09:55:43 +0000 (15:25 +0530)] 
nft: simplify chain lookup in do_list_chain

use the chain_cache_find function for faster lookup of chain instead of
iterating over all chains in table

Signed-off-by: ChanderG <mail@chandergovind.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agointervals: fix compilation --with-mini-gmp
Pablo Neira Ayuso [Mon, 30 May 2022 17:00:05 +0000 (19:00 +0200)] 
intervals: fix compilation --with-mini-gmp

Use pr_gmp_debug() instead to compile with minigmp.

intervals.c: In function ‘set_delete’:
intervals.c:489:25: warning: implicit declaration of function ‘gmp_printf’; did you mean ‘gmp_vfprintf’? [-Wimplicit-function-declaration]
  489 |                         gmp_printf("remove: [%Zx-%Zx]\n",
      |                         ^~~~~~~~~~
      |                         gmp_vfprintf

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agojson: update json output ordering to place rules after chains
Chander Govindarajan [Mon, 23 May 2022 10:07:11 +0000 (15:37 +0530)] 
json: update json output ordering to place rules after chains

Currently the json output of `nft -j list ruleset` interleaves rules
with chains.

As reported in this bug:

 https://bugzilla.netfilter.org/show_bug.cgi?id=1580

the json cannot be fed into `nft -j -f <file>` since rules may
reference chains that are created later

Instead create rules after all chains are output.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1580
Signed-off-by: ChanderG <mail@chandergovind.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agonetlink_delinearize: release last register on exit
Pablo Neira Ayuso [Fri, 13 May 2022 14:46:31 +0000 (16:46 +0200)] 
netlink_delinearize: release last register on exit

netlink_release_registers() does not release the expression in the last
32-bit register.

struct netlink_parse_ctx {
...
        struct expr             *registers[MAX_REGS + 1];

This array is MAX_REGS + 1 (verdict register + 16 32-bit registers).

Fixes: 371c3a0bc3c2 ("netlink_delinearize: release expressions in context registers")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agosets_with_ifnames: add test case for concatenated range
Florian Westphal [Fri, 29 Apr 2022 18:32:39 +0000 (20:32 +0200)] 
sets_with_ifnames: add test case for concatenated range

Refactor existing test case for simple interace name ranges
(without concatenations) to also cover "addr . ifname".

Signed-off-by: Florian Westphal <fw@strlen.de>
23 months agosegtree: add pretty-print support for wildcard strings in concatenated sets
Florian Westphal [Fri, 29 Apr 2022 18:32:38 +0000 (20:32 +0200)] 
segtree: add pretty-print support for wildcard strings in concatenated sets

For concat ranges, something like 'ppp*' is translated as a range
from 'ppp\0\0\0...' to 'ppp\ff\ff\ff...'.

In order to display this properly, check for presence of string base
type and convert to symbolic expression, with appended '*' character.

Signed-off-by: Florian Westphal <fw@strlen.de>
23 months agonetlink: swap byteorder for host-endian concat data
Florian Westphal [Fri, 29 Apr 2022 18:32:37 +0000 (20:32 +0200)] 
netlink: swap byteorder for host-endian concat data

All data must be passed in network byte order, else matching
won't work respectively kernel will reject the interval because
it thinks that start is after end

This is needed to allow use of 'ppp*' in interval sets with
concatenations.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agointervals: deletion should adjust range not yet in the kernel
Pablo Neira Ayuso [Fri, 6 May 2022 21:46:59 +0000 (23:46 +0200)] 
intervals: deletion should adjust range not yet in the kernel

Do not remove the range if it does not exists yet in the kernel, adjust it
instead.  Uncovered by use-after-free error.

==276702==ERROR: AddressSanitizer: heap-use-after-free on address 0x60d00190663c at pc 0x7ff310ab526f bp 0x7fffeb76f750 sp 0x7fffeb76f748 READ of size 4 at 0x60d00190663c thread T0
    #0 0x7ff310ab526e in __adjust_elem_right .../nftables/src/intervals.c:300
    #1 0x7ff310ab59a7 in adjust_elem_right .../nftables/src/intervals.c:311
    #2 0x7ff310ab6daf in setelem_adjust .../nftables/src/intervals.c:354
    #3 0x7ff310ab783a in setelem_delete .../nftables/src/intervals.c:411
    #4 0x7ff310ab80e6 in __set_delete .../nftables/src/intervals.c:451

Fixes: 3e8d934e4f72 ("intervals: support to partial deletion with automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agooptimize: memleak in statement matrix
Pablo Neira Ayuso [Wed, 4 May 2022 10:02:43 +0000 (12:02 +0200)] 
optimize: memleak in statement matrix

Release clone object in case this statement is not supported.

Fixes: 743b0e81371f ("optimize: do not clone unsupported statement")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agooptimize: merge nat rules with same selectors into map
Pablo Neira Ayuso [Tue, 3 May 2022 15:51:36 +0000 (17:51 +0200)] 
optimize: merge nat rules with same selectors into map

Verdict and nat are mutually exclusive, no need to support for this
combination.

 # cat ruleset.nft
 table ip x {
        chain y {
type nat hook postrouting priority srcnat; policy drop;
                ip saddr 1.1.1.1 tcp dport 8000 snat to 4.4.4.4:80
                ip saddr 2.2.2.2 tcp dport 8001 snat to 5.5.5.5:90
        }
 }

 # nft -o -c -f ruleset.nft
 Merging:
 ruleset.nft:4:3-52:                ip saddr 1.1.1.1 tcp dport 8000 snat to 4.4.4.4:80
 ruleset.nft:5:3-52:                ip saddr 2.2.2.2 tcp dport 8001 snat to 5.5.5.5:90
 into:
        snat to ip saddr . tcp dport map { 1.1.1.1 . 8000 : 4.4.4.4 . 80, 2.2.2.2 . 8001 : 5.5.5.5 . 90 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agooptimize: do not clone unsupported statement
Pablo Neira Ayuso [Tue, 3 May 2022 15:49:56 +0000 (17:49 +0200)] 
optimize: do not clone unsupported statement

Skip unsupported statements when building the statement matrix,
otherwise clone remains uninitialized.

Fixes: fb298877ece2 ("src: add ruleset optimization infrastructure")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agooptimize: incorrect logic in verdict comparison
Pablo Neira Ayuso [Tue, 3 May 2022 09:30:57 +0000 (11:30 +0200)] 
optimize: incorrect logic in verdict comparison

Keep inspecting rule verdicts before assuming they are equal. Update
existing test to catch this bug.

Fixes: 1542082e259b ("optimize: merge same selector with different verdict into verdict map")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agosrc: fix always-true assertions
Florian Westphal [Tue, 26 Apr 2022 09:47:37 +0000 (11:47 +0200)] 
src: fix always-true assertions

assert(1) is a no-op, this should be assert(0). Use BUG() instead.
Add missing CATCHALL to avoid BUG().

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agointervals: set on EXPR_F_KERNEL flag for new elements in set cache
Pablo Neira Ayuso [Mon, 18 Apr 2022 13:17:59 +0000 (15:17 +0200)] 
intervals: set on EXPR_F_KERNEL flag for new elements in set cache

So follow up command in this batch that update the set assumes this
element is already in the kernel.

Fixes: 3da9643fb9ff ("intervals: add support to automerge with kernel elements")
Fixes: 3ed9fadaab95 ("intervals: build list of elements to be added from cache")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agotests: add concat test case with integer base type subkey
Florian Westphal [Sun, 17 Apr 2022 22:00:18 +0000 (00:00 +0200)] 
tests: add concat test case with integer base type subkey

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agosrc: allow use of base integer types as set keys in concatenations
Florian Westphal [Sun, 17 Apr 2022 20:27:41 +0000 (22:27 +0200)] 
src: allow use of base integer types as set keys in concatenations

"typeof ip saddr . ipsec in reqid" won't work because reqid uses
integer type, i.e. dtype->size is 0.

With "typeof", the size can be derived from the expression length,
via set->key.

This computes the concat length based either on dtype->size or
expression length.

It also updates concat evaluation to permit a zero datatype size
if the subkey expression has nonzero length (i.e., typeof was used).

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agointervals: build list of elements to be added from cache
Pablo Neira Ayuso [Fri, 15 Apr 2022 09:40:09 +0000 (11:40 +0200)] 
intervals: build list of elements to be added from cache

Loop over the set cache and add elements that have no EXPR_F_KERNEL,
meaning that these are new elements in the set that have resulted
from adjusting/split existing ranges.

This fixes several partial deletions of the same interval in one
command.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agointervals: fix deletion of multiple ranges with automerge
Pablo Neira Ayuso [Thu, 14 Apr 2022 15:47:30 +0000 (17:47 +0200)] 
intervals: fix deletion of multiple ranges with automerge

Iterate over the list of elements to be deleted, then splice one
EXPR_F_REMOVE element at a time to update the list of existing sets
incrementally.

Fixes: 3e8d934e4f722 ("intervals: support to partial deletion with automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agointervals: add elements with EXPR_F_KERNEL to purge list only
Pablo Neira Ayuso [Thu, 14 Apr 2022 16:16:31 +0000 (18:16 +0200)] 
intervals: add elements with EXPR_F_KERNEL to purge list only

Do not add elements to purge list which are not in the kernel,
otherwise, bogus ENOENT is reported.

Fixes: 3e8d934e4f722 ("intervals: support to partial deletion with automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agonetlink: remove unused argument from helper function
Florian Westphal [Sun, 17 Apr 2022 20:58:19 +0000 (22:58 +0200)] 
netlink: remove unused argument from helper function

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agointervals: Simplify element sanity checks
Phil Sutter [Thu, 14 Apr 2022 11:39:24 +0000 (13:39 +0200)] 
intervals: Simplify element sanity checks

Since setelem_delete() assigns to 'prev' pointer only if it doesn't have
EXPR_F_REMOVE flag set, there is no need to check that flag in called
functions.

Fixes: 3e8d934e4f722 ("intervals: support to partial deletion with automerge")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agointervals: unset EXPR_F_KERNEL for adjusted elements
Pablo Neira Ayuso [Wed, 13 Apr 2022 13:37:19 +0000 (15:37 +0200)] 
intervals: unset EXPR_F_KERNEL for adjusted elements

This element is adjusted, reset the EXPR_F_KERNEL flag, this is a new
element and the old is purged from the kernel.

The existing list of elements in the kernel is spliced to the elements
to be removed, then merge-sorted. The EXPR_F_REMOVE flag specifies that
this element represents a deletion.

The EXPR_F_REMOVE and EXPR_F_KERNEL allows to track objects: whether
element is in the kernel (EXPR_F_KERNEL), element is new (no flag) or
element represents a removal (EXPR_F_REMOVE).

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agosrc: restore interval sets work with string datatypes
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:30 +0000 (04:01 +0200)] 
src: restore interval sets work with string datatypes

Switch byteorder of string datatypes to host byteorder.

Partial revert of ("src: make interval sets work with string datatypes")
otherwise new interval code complains with conflicting intervals.

testcases/sets/sets_with_ifnames passes fine again.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agointervals: support to partial deletion with automerge
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:22 +0000 (04:01 +0200)] 
intervals: support to partial deletion with automerge

Splice the existing set element cache with the elements to be deleted
and merge sort it.  The elements to be deleted are identified by the
EXPR_F_REMOVE flag.

The set elements to be deleted is automerged in first place if the
automerge flag is set on.

There are four possible deletion scenarios:

- Exact match, eg. delete [a-b] and there is a [a-b] range in the kernel set.
- Adjust left side of range, eg. delete [a-b] from range [a-x] where x > b.
- Adjust right side of range, eg. delete [a-b] from range [x-b] where x < a.
- Split range, eg. delete [a-b] from range [x-y] where x < a and b < y.

Update nft_evaluate() to use the safe list variant since new commands
are dynamically registered to the list to update ranges.

This patch also restores the set element existence check for Linux
kernels <= 5.7.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agoevaluate: allow for zero length ranges
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:19 +0000 (04:01 +0200)] 
evaluate: allow for zero length ranges

Allow for ranges such as, eg. 30-30.

This is required by the new intervals.c code, which normalize constant,
prefix set elements to all ranges.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agointervals: add support to automerge with kernel elements
Pablo Neira Ayuso [Wed, 13 Apr 2022 02:01:17 +0000 (04:01 +0200)] 
intervals: add support to automerge with kernel elements

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

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

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

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

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

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

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

Not used by anyone anymore, remove it.

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

This is a rewrite of the segtree interval codebase.

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

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

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

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

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

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

More relevant updates:

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

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

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

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

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

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

Also add test cases for this.

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

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

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

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

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

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

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

key ifname
flags interval
elements = { eth* }

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

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

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

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

Allows to interface names in interval sets:

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

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

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

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

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

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

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

No functional change intended.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Add meta time tests using < and > operands.

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

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

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

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

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

Fix this by using uint64_t instead.

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

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

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

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

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

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

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

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

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

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

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

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

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

A few examples:

- With anonymous sets:

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

- With named sets:

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

Incremental updates are also supported, eg.

 nft add element x y { 3.3.3.3 . 0x28 }

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

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

Otherwise listing breaks showing [invalid type] notice.

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

Update tests to cover this usecase.

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

without this test fails with:

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

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

Phil Sutter says:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

The following example ruleset crashes:

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

because handling for EXPR_SET is missing.

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

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

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

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

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

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

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

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

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

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

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

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

Isolate 'performance' and 'memory' keywords.

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

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

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

Two more keywords isolated.

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

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

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

Isolate two more keywords shared with list command.

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

Some keywords are shared with list command.

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

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

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

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

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

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

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

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

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

It shares two keywords with PARSER_SC_IP.

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

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

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

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

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

Isolates only 'cpi' keyword for now.

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

Quite a few keywords are shared with PARSER_SC_TCP.

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

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

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

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

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

Unify the two, header fields are almost identical.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Still requires libnftnl 1.2.1

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

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

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

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

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

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

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

This wasn't covered at all.

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

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

 testcases/sets/0034get_element_0

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

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

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

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

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

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

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

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

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

Signed-off-by: Florian Westphal <fw@strlen.de>