]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
5 years agocache: Reduce caching for get command
Phil Sutter [Thu, 14 Nov 2019 14:45:07 +0000 (15:45 +0100)] 
cache: Reduce caching for get command

Introduce a function to distinguish which command object was given and
request only the necessary bits to have sets and their elements
available for 'get element' command.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: Fix get element for little endian ranges
Phil Sutter [Thu, 14 Nov 2019 13:41:50 +0000 (14:41 +0100)] 
segtree: Fix get element for little endian ranges

This fixes get element command for interval sets with host byte order
data type, like e.g. mark. During serializing of the range (or element)
to query, data was exported in wrong byteorder and consequently not
found in kernel.

The mystery part is that code seemed correct: When calling
constant_expr_alloc() from set_elem_add(), the set key's byteorder was
passed with correct value of BYTEORDER_HOST_ENDIAN.

Comparison with delete/add element code paths though turned out that in
those use-cases, constant_expr_alloc() is called with BYTEORDER_INVALID:

- seg_tree_init() takes byteorder field value of first element in
  init->expressions (i.e., the elements requested on command line) and
  assigns that to tree->byteorder
- tree->byteorder is passed to constant_expr_alloc() in
  set_insert_interval()
- the elements' byteorder happens to be the default value

This patch may not fix the right side, but at least it aligns get with
add/delete element codes.

Fixes: a43cc8d53096d ("src: support for get element command")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: Check ranges when deleting elements
Phil Sutter [Tue, 12 Nov 2019 19:00:15 +0000 (20:00 +0100)] 
segtree: Check ranges when deleting elements

Make sure any intervals to delete actually exist, otherwise reject the
command. Without this, it is possible to mess up rbtree contents:

| # nft list ruleset
| table ip t {
|  set s {
|  type ipv4_addr
|  flags interval
|  auto-merge
|  elements = { 192.168.1.0-192.168.1.254, 192.168.1.255 }
|  }
| }
| # nft delete element t s '{ 192.168.1.0/24 }'
| # nft list ruleset
| table ip t {
|  set s {
|  type ipv4_addr
|  flags interval
|  auto-merge
|  elements = { 192.168.1.255-255.255.255.255 }
|  }
| }

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agometa: Rewrite hour_type_print()
Phil Sutter [Tue, 12 Nov 2019 18:28:37 +0000 (19:28 +0100)] 
meta: Rewrite hour_type_print()

There was no point in this recursively called __hour_type_print_r() at
all, it takes only four lines of code to split the number of seconds
into hours, minutes and seconds.

While being at it, inverse the conditional to reduce indenting for the
largest part of the function's body. Also introduce SECONDS_PER_DAY
macro to avoid magic numbers.

Fixes: f8f32deda31df ("meta: Introduce new conditions 'time', 'day' and 'hour'")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agolibnftables: Store top_scope in struct nft_ctx
Phil Sutter [Wed, 30 Oct 2019 20:45:39 +0000 (21:45 +0100)] 
libnftables: Store top_scope in struct nft_ctx

Allow for interactive sessions to make use of defines. Since parser is
initialized for each line, top scope defines didn't persist although
they are actually useful for stuff like:

| # nft -i
| define goodports = { 22, 23, 80, 443 }
| add rule inet t c tcp dport $goodports accept
| add rule inet t c tcp sport $goodports accept

While being at it, introduce scope_alloc() and scope_free().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodoc: Drop incorrect requirement for nft configs
Phil Sutter [Tue, 5 Nov 2019 13:13:27 +0000 (14:13 +0100)] 
doc: Drop incorrect requirement for nft configs

The shebang is not needed in files to be used with --file parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosrc: add and use `set_is_meter` helper
Jeremy Sowden [Tue, 5 Nov 2019 21:40:26 +0000 (21:40 +0000)] 
src: add and use `set_is_meter` helper

The sets constructed for meters are flagged as anonymous and dynamic.
However, in some places there are only checks that they are dynamic,
which can lead to normal sets being classified as meters.

For example:

  # nft add table t
  # nft add set t s { type ipv4_addr; size 256; flags dynamic,timeout; }
  # nft add chain t c
  # nft add rule t c tcp dport 80 meter m size 128 { ip saddr limit rate 10/second }
  # nft list meters
  table ip t {
          set s {
                  type ipv4_addr
                  size 256
                  flags dynamic,timeout
          }
          meter m {
                  type ipv4_addr
                  size 128
                  flags dynamic
          }
  }
  # nft list meter t m
  table ip t {
          meter m {
                  type ipv4_addr
                  size 128
                  flags dynamic
          }
  }
  # nft list meter t s
  Error: No such file or directory
  list meter t s
               ^

Add a new helper `set_is_meter` and use it wherever there are checks for
meters.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agosrc: flowtable: add support for delete command by handle
Eric Jallot [Mon, 4 Nov 2019 20:23:59 +0000 (21:23 +0100)] 
src: flowtable: add support for delete command by handle

Also, display handle when listing with '-a'.

Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodoc: fix missing family in plural forms list command.
Eric Jallot [Wed, 30 Oct 2019 17:19:17 +0000 (18:19 +0100)] 
doc: fix missing family in plural forms list command.

Fixes: 067ac215e93f ("doc: update nft list plural form parameters")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: flowtable: add support for named flowtable listing
Eric Jallot [Wed, 30 Oct 2019 17:06:19 +0000 (18:06 +0100)] 
src: flowtable: add support for named flowtable listing

This patch allows you to dump a named flowtable.

 # nft list flowtable inet t f
 table inet t {
         flowtable f {
                 hook ingress priority filter + 10
                 devices = { eth0, eth1 }
         }
 }

Also:
libnftables-json.adoc: fix missing quotes.

Fixes: db0697ce7f60 ("src: support for flowtable listing")
Fixes: 872f373dc50f ("doc: Add JSON schema documentation")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: Replace use of untyped nftnl data setters
Phil Sutter [Mon, 28 Oct 2019 17:08:55 +0000 (18:08 +0100)] 
mnl: Replace use of untyped nftnl data setters

Setting strings won't make a difference, but passing data length to
*_set_data() functions allows for catching accidental changes on either
side.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: remove artifical cap on 8 devices per flowtable
Sven Auhagen [Sat, 26 Oct 2019 11:17:21 +0000 (13:17 +0200)] 
mnl: remove artifical cap on 8 devices per flowtable

Currently assuming a maximum of 8 devices, remove this artificial cap.

Signed-off-by: Sven Auhagen <sven.auhagen@voleatech.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add multidevice support for netdev chain
Pablo Neira Ayuso [Sat, 26 Oct 2019 11:15:10 +0000 (13:15 +0200)] 
src: add multidevice support for netdev chain

This patch allows you to specify multiple netdevices to be bound to the
netdev basechain, eg.

 # nft add chain netdev x y { \
type filter hook ingress devices = { eth0, eth1 } priority 0\; }

json codebase has been updated to support for one single device with the
existing representation, no support for multidevice is included in this
patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests/py: Fix test script for Python3 tempfile
Phil Sutter [Tue, 29 Oct 2019 11:20:18 +0000 (12:20 +0100)] 
tests/py: Fix test script for Python3 tempfile

When instantiating a temporary file using tempfile's TemporaryFile()
constructor, the resulting object's 'name' attribute is of type int.
This in turn makes print_msg() puke while trying to concatenate string
and int using '+' operator.

Fix this by using format strings consequently, thereby cleaning up code
a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agopy: add missing output flags.
Jeremy Sowden [Tue, 22 Oct 2019 20:58:53 +0000 (21:58 +0100)] 
py: add missing output flags.

`terse` and `numeric_time` are missing from the `output_flags` dict.
Add them and getters and setters for them.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agomain: remove duplicate output flag assignment.
Jeremy Sowden [Tue, 22 Oct 2019 20:58:55 +0000 (21:58 +0100)] 
main: remove duplicate output flag assignment.

`NFT_CTX_OUTPUT_NUMERIC_TIME` is implicit in
`NFT_CTX_OUTPUT_NUMERIC_ALL`: there are is no need explicitly to OR it
into output_flags when `--numeric` is passed.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: add missing `OPT_NUMERIC_PROTO` long option.
Jeremy Sowden [Tue, 22 Oct 2019 20:58:54 +0000 (21:58 +0100)] 
main: add missing `OPT_NUMERIC_PROTO` long option.

The `options` array is missing an entry for `OPT_NUMERIC_PROTO`.  Add a
new option, `--numeric-protocol`, consistent with the documentation.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodoc: add missing output flag documentation.
Jeremy Sowden [Tue, 22 Oct 2019 20:58:52 +0000 (21:58 +0100)] 
doc: add missing output flag documentation.

The documentation for NFT_CTX_OUTPUT_FLAG_NUMERIC_TIME and
NFT_CTX_OUTPUT_FLAG_NUMERIC_ALL is incomplete.  Add the missing bits.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoRevert "main: Fix for misleading error with negative chain priority"
Phil Sutter [Wed, 23 Oct 2019 11:27:25 +0000 (13:27 +0200)] 
Revert "main: Fix for misleading error with negative chain priority"

This reverts commit 9fc71bc6b602c8706d1214e0100bcd7638c257e3.

Given that this change breaks typical commands like
'nft list ruleset -a' while on the other hand escaping of semicolons and
(depending on shell) curly braces is still required, decision was made
to not go with this solution.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agosrc: add --terse to suppress output of set elements.
Jeremy Sowden [Mon, 21 Oct 2019 21:49:22 +0000 (22:49 +0100)] 
src: add --terse to suppress output of set elements.

Listing an entire ruleset or a table with `nft list` prints the elements
of all set definitions within the ruleset or table.  Seeing the full set
contents is not often necessary especially when requesting to see
someone's ruleset for help and support purposes.  Add a new option '-t,
--terse' options to suppress the output of set contents.

Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1374
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: use `-T` as the short option for `--numeric-time`.
Jeremy Sowden [Mon, 21 Oct 2019 21:49:21 +0000 (22:49 +0100)] 
src: use `-T` as the short option for `--numeric-time`.

A new `--terse` option will be introduced in a later patch.  Change the
short option used for `--numeric-time` from `-t` to `-T` in order to
leave `-t` free.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomain: Fix for misleading error with negative chain priority
Phil Sutter [Mon, 21 Oct 2019 16:51:14 +0000 (18:51 +0200)] 
main: Fix for misleading error with negative chain priority

getopt_long() would try to parse the negative priority as an option and
return -1 as it is not known:

| # nft add chain x y { type filter hook input priority -30\; }
| nft: invalid option -- '3'

Fix this by prefixing optstring with a plus character. This instructs
getopt_long() to not collate arguments but just stop after the first
non-option, leaving the rest for manual handling. In fact, this is just
what nft desires: mixing options with nft syntax leads to confusive
command lines anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotproxy: Add missing error checking when parsing from netlink
Phil Sutter [Mon, 21 Oct 2019 14:29:03 +0000 (16:29 +0200)] 
tproxy: Add missing error checking when parsing from netlink

netlink_get_register() may return NULL and every other caller checks
that. Assuming this situation is not expected, just jump to 'err' label
without queueing an explicit error message.

Fixes: 2be1d52644cf7 ("src: Add tproxy support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_json: Fix checking of parse_policy() return code
Phil Sutter [Mon, 21 Oct 2019 14:20:10 +0000 (16:20 +0200)] 
parser_json: Fix checking of parse_policy() return code

The function was changed to return an expression or NULL but error
checking wasn't adjusted while doing so.

Fixes: dba4a9b4b5fe2 ("src: allow variable in chain policy")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: restore --echo with anonymous sets
Pablo Neira Ayuso [Thu, 17 Oct 2019 13:24:30 +0000 (15:24 +0200)] 
src: restore --echo with anonymous sets

If --echo is passed, then the cache already contains the commands that
have been sent to the kernel. However, anonymous sets are an exception
since the cache needs to be updated in this case.

Remove the old cache logic from the monitor code that has been replaced
by 01e5c6f0ed03 ("src: add cache level flags").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Phil Sutter <phil@nwl.cc>
5 years agoflowtable: fix memleak in exit path
Eric Jallot [Thu, 17 Oct 2019 11:08:36 +0000 (13:08 +0200)] 
flowtable: fix memleak in exit path

Add missing loop in table_free().
Free all objects in flowtable_free() and add conditions in case of error recovery
in the parser (See commit 4be0a3f922a29).

Also, fix memleak in the parser.

This fixes the following memleak:

 # valgrind --leak-check=full nft add flowtable inet raw f '{ hook ingress priority filter; devices = { eth0 }; }'
 ==15414== Memcheck, a memory error detector
 ==15414== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
 ==15414== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
 ==15414== Command: nft add flowtable inet raw f {\ hook\ ingress\ priority\ filter;\ devices\ =\ {\ eth0\ };\ }
 ==15414==
 ==15414==
 ==15414== HEAP SUMMARY:
 ==15414==     in use at exit: 266 bytes in 4 blocks
 ==15414==   total heap usage: 55 allocs, 51 frees, 208,105 bytes allocated
 ==15414==
 ==15414== 5 bytes in 1 blocks are definitely lost in loss record 2 of 4
 ==15414==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
 ==15414==    by 0x5C64AA9: strdup (strdup.c:42)
 ==15414==    by 0x4E705ED: xstrdup (utils.c:75)
 ==15414==    by 0x4E93F01: nft_lex (scanner.l:648)
 ==15414==    by 0x4E85C1C: nft_parse (parser_bison.c:5577)
 ==15414==    by 0x4E75A07: nft_parse_bison_buffer (libnftables.c:375)
 ==15414==    by 0x4E75A07: nft_run_cmd_from_buffer (libnftables.c:443)
 ==15414==    by 0x40170F: main (main.c:326)
 ==15414==
 ==15414== 261 (128 direct, 133 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
 ==15414==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
 ==15414==    by 0x4E705AD: xmalloc (utils.c:36)
 ==15414==    by 0x4E705AD: xzalloc (utils.c:65)
 ==15414==    by 0x4E560B6: expr_alloc (expression.c:45)
 ==15414==    by 0x4E56288: symbol_expr_alloc (expression.c:286)
 ==15414==    by 0x4E8A601: nft_parse (parser_bison.y:1842)
 ==15414==    by 0x4E75A07: nft_parse_bison_buffer (libnftables.c:375)
 ==15414==    by 0x4E75A07: nft_run_cmd_from_buffer (libnftables.c:443)
 ==15414==    by 0x40170F: main (main.c:326)

Fixes: 92911b362e906 ("src: add support to add flowtables")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: define flowtable device compound as a list
Pablo Neira Ayuso [Thu, 17 Oct 2019 11:00:31 +0000 (13:00 +0200)] 
src: define flowtable device compound as a list

This fixes a memleak when releasing the compound expression via
expr_free().

Fixes: 92911b362e90 ("src: add support to add flowtables")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agorule: Fix for single line ct timeout printing
Phil Sutter [Wed, 16 Oct 2019 22:20:59 +0000 (00:20 +0200)] 
rule: Fix for single line ct timeout printing

Commit 43ae7a48ae3de ("rule: do not print semicolon in ct timeout")
removed an extra semicolon at end of line, but thereby broke single line
output. The correct fix is to use opts->stmt_separator which holds
either newline or semicolon chars depending on output mode.

Fixes: 43ae7a48ae3de ("rule: do not print semicolon in ct timeout")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
5 years agotests/monitor: Fix for changed ct timeout format
Phil Sutter [Wed, 16 Oct 2019 21:46:10 +0000 (23:46 +0200)] 
tests/monitor: Fix for changed ct timeout format

Commit a9b0c385a1d5e ("rule: print space between policy and timeout")
changed spacing in ct timeout objects but missed to adjust related test
case.

Fixes: a9b0c385a1d5e ("rule: print space between policy and timeout")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
5 years agomonitor: Add missing newline to error message
Phil Sutter [Wed, 16 Oct 2019 18:27:20 +0000 (20:27 +0200)] 
monitor: Add missing newline to error message

These shouldn't happen in practice and printing to stderr is not the
right thing either, but fix this anyway.

Fixes: f9563c0feb24d ("src: add events reporting")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
5 years agomnl: Don't use nftnl_set_set()
Phil Sutter [Tue, 15 Oct 2019 13:58:13 +0000 (15:58 +0200)] 
mnl: Don't use nftnl_set_set()

The function is unsafe to use as it effectively bypasses data length
checks. Instead use nftnl_set_set_str() which at least asserts a const
char pointer is passed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agocli: add linenoise CLI implementation.
Jeremy Sowden [Tue, 24 Sep 2019 07:40:54 +0000 (08:40 +0100)] 
cli: add linenoise CLI implementation.

By default, continue to use libreadline, but if `--with-cli=linenoise`
is passed to configure, build the linenoise implementation instead.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpression: extend 'nft describe' to allow listing data types
Florian Westphal [Sat, 12 Oct 2019 22:17:52 +0000 (00:17 +0200)] 
expression: extend 'nft describe' to allow listing data types

nft describe ct_status
before:
symbol expression, datatype invalid (invalid), 0 bits

after:
datatype ct_status (conntrack status) (basetype bitmask, integer), 32 bits

pre-defined symbolic constants (in hexadecimal):
        expected                        0x00000001
        seen-reply                      0x00000002
[..]

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agodatatype: display description for header field < 8 bits
Pablo Neira Ayuso [Wed, 9 Oct 2019 20:09:52 +0000 (22:09 +0200)] 
datatype: display description for header field < 8 bits

 # nft describe ip dscp
 payload expression, datatype dscp (Differentiated Services Code Point) (basetype integer), 6 bits

 pre-defined symbolic constants (in hexadecimal):
 nft: datatype.c:209: switch_byteorder: Assertion `len > 0' failed.
 Aborted

Fixes: c89a0801d077 ("datatype: Display pre-defined inet_service values in host byte order")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosegtree: always close interval in non-anonymous sets
Pablo Neira Ayuso [Wed, 9 Oct 2019 09:54:32 +0000 (11:54 +0200)] 
segtree: always close interval in non-anonymous sets

Skip this optimization for non-anonymous sets, otherwise, element
deletion breaks.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoobj: fix memleak in parser_bison.y
Eric Jallot [Tue, 8 Oct 2019 13:47:24 +0000 (15:47 +0200)] 
obj: fix memleak in parser_bison.y

Each object (secmark, synproxy, quota, limit, counter) is dynamically allocated
by the parser and not freed at exit.
However, there is no need to use dynamic allocation here because struct obj
already provides the required storage. Update the grammar to ensure that
obj_alloc() is called before config occurs.

This fixes the following memleak (secmark as example):

 # valgrind --leak-check=full nft add secmark inet raw ssh \"system_u:object_r:ssh_server_packet_t:s0\"
 ==14643== Memcheck, a memory error detector
 ==14643== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
 ==14643== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
 ==14643== Command: nft add secmark inet raw ssh "system_u:object_r:ssh_server_packet_t:s0"
 ==14643==
 ==14643==
 ==14643== HEAP SUMMARY:
 ==14643==     in use at exit: 256 bytes in 1 blocks
 ==14643==   total heap usage: 41 allocs, 40 frees, 207,809 bytes allocated
 ==14643==
 ==14643== 256 bytes in 1 blocks are definitely lost in loss record 1 of 1
 ==14643==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
 ==14643==    by 0x4E72074: xmalloc (utils.c:36)
 ==14643==    by 0x4E72074: xzalloc (utils.c:65)
 ==14643==    by 0x4E89A31: nft_parse (parser_bison.y:3706)
 ==14643==    by 0x4E778E7: nft_parse_bison_buffer (libnftables.c:375)
 ==14643==    by 0x4E778E7: nft_run_cmd_from_buffer (libnftables.c:443)
 ==14643==    by 0x40170F: main (main.c:326)

Fixes: f44ab88b1088e ("src: add synproxy stateful object support")
Fixes: 3bc84e5c1fdd1 ("src: add support for setting secmark")
Fixes: c0697eabe832d ("src: add stateful object support for limit")
Fixes: 4d38878b39be4 ("src: add/create/delete stateful objects")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: fix failed tests due to missing quotes
Eric Jallot [Tue, 8 Oct 2019 18:06:32 +0000 (20:06 +0200)] 
tests: shell: fix failed tests due to missing quotes

Add double quotes to protect newlines when using <<< redirection.

See also commit b878cb7d83855.

Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: check we can use "dynamic" set for lookups
Florian Westphal [Mon, 7 Oct 2019 14:34:32 +0000 (16:34 +0200)] 
tests: check we can use "dynamic" set for lookups

Requires kernel commit acab713177377
("netfilter: nf_tables: allow lookups in dynamic sets"), else the
rule add will fail.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agosrc: obj: fix memleak in handle_free()
Eric Jallot [Mon, 30 Sep 2019 08:38:23 +0000 (10:38 +0200)] 
src: obj: fix memleak in handle_free()

Using limit object as example:

 # valgrind --leak-check=full nft list ruleset
 ==9937== Memcheck, a memory error detector
 ==9937== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
 ==9937== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
 ==9937== Command: nft list ruleset
 ==9937==
 table inet raw {
         limit lim1 {
                 rate 1/second
         }
 }
 ==9937==
 ==9937== HEAP SUMMARY:
 ==9937==     in use at exit: 5 bytes in 1 blocks
 ==9937==   total heap usage: 50 allocs, 49 frees, 212,065 bytes allocated
 ==9937==
 ==9937== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
 ==9937==    at 0x4C29EA3: malloc (vg_replace_malloc.c:309)
 ==9937==    by 0x5C65AA9: strdup (strdup.c:42)
 ==9937==    by 0x4E720A3: xstrdup (utils.c:75)
 ==9937==    by 0x4E660FF: netlink_delinearize_obj (netlink.c:972)
 ==9937==    by 0x4E6641C: list_obj_cb (netlink.c:1064)
 ==9937==    by 0x50E8993: nftnl_obj_list_foreach (object.c:494)
 ==9937==    by 0x4E664EA: netlink_list_objs (netlink.c:1085)
 ==9937==    by 0x4E4FE82: cache_init_objects (rule.c:188)
 ==9937==    by 0x4E4FE82: cache_init (rule.c:221)
 ==9937==    by 0x4E4FE82: cache_update (rule.c:271)
 ==9937==    by 0x4E7716E: nft_evaluate (libnftables.c:406)
 ==9937==    by 0x4E778F7: nft_run_cmd_from_buffer (libnftables.c:447)
 ==9937==    by 0x40170F: main (main.c:326)

Fixes: 4756d92e517ae ("src: listing of stateful objects")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agolibnftables: memleak when list of commands is empty
Pablo Neira Ayuso [Fri, 27 Sep 2019 06:37:12 +0000 (08:37 +0200)] 
libnftables: memleak when list of commands is empty

==9946== 200,807 (40 direct, 200,767 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==9946==    at 0x4837B65: calloc (vg_replace_malloc.c:762)
==9946==    by 0x4F28216: nftnl_batch_alloc (batch.c:66)
==9946==    by 0x48A33E8: mnl_batch_init (mnl.c:164)
==9946==    by 0x48A736F: nft_netlink.isra.0 (libnftables.c:29)
==9946==    by 0x48A7D03: nft_run_cmd_from_filename (libnftables.c:508)
==9946==    by 0x10A621: main (main.c:328)

Fixes: fc6d0f8b0cb1 ("libnftables: get rid of repeated initialization of netlink_ctx")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotests: shell: delete flowtable after flush chain
Pablo Neira Ayuso [Wed, 25 Sep 2019 08:24:45 +0000 (10:24 +0200)] 
tests: shell: delete flowtable after flush chain

Returns EBUSY on buggy kernels.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: do not cache sender buffer size
Pablo Neira Ayuso [Fri, 20 Sep 2019 15:01:47 +0000 (17:01 +0200)] 
mnl: do not cache sender buffer size

SO_SNDBUF never fails, this socket option just provides a hint to the
kernel.  SO_SNDBUFFORCE sets the buffer size to zero if the value goes
over INT_MAX. Userspace is caching the buffer hint that sends to the
kernel, so it might leave userspace out of sync if the kernel ignores
the hint. Do not make assumptions, fetch the sender buffer size from the
kernel via getsockopt().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agocli: remove unused declaration.
Jeremy Sowden [Mon, 16 Sep 2019 12:42:01 +0000 (13:42 +0100)] 
cli: remove unused declaration.

cli.h includes a forward declaration of struct parser_state which is not
needed.  Remove it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoconfigure: remove unused AC_SUBST macros.
Jeremy Sowden [Mon, 16 Sep 2019 12:42:00 +0000 (13:42 +0100)] 
configure: remove unused AC_SUBST macros.

configure.ac contains a couple of AC_SUBST macros which serve no
purpose.  Remove them.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: meter: avoid double-space in list ruleset output
Florian Westphal [Thu, 19 Sep 2019 09:23:46 +0000 (11:23 +0200)] 
src: meter: avoid double-space in list ruleset output

changes
meter f size 1024 { ip saddr limit rate 10/second}  accept
to
meter f size 1024 { ip saddr limit rate 10/second } accept

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: parser_json: fix crash while restoring secmark object
Eric Jallot [Mon, 16 Sep 2019 10:24:44 +0000 (12:24 +0200)] 
src: parser_json: fix crash while restoring secmark object

Before patch:
 # nft -j list ruleset | tee rules.json | jq '.'
 {
   "nftables": [
     {
       "metainfo": {
         "version": "0.9.2",
         "release_name": "Scram",
         "json_schema_version": 1
       }
     },
     {
       "table": {
         "family": "inet",
         "name": "t",
         "handle": 11
       }
     },
     {
       "secmark": {
         "family": "inet",
         "name": "s",
         "table": "t",
         "handle": 1,
         "context": "system_u:object_r:ssh_server_packet_t:s0"
       }
     }
   ]
 }

 # nft flush ruleset
 # nft -j -f rules.json
 Segmentation fault

Use "&tmp" instead of "tmp" in json_unpack() while translating "context" keyword.

After patch:
 # nft -j -f rules.json
 # nft list ruleset
 table inet t {
         secmark s {
                 "system_u:object_r:ssh_server_packet_t:s0"
         }
 }

Fixes: 3bc84e5c1fdd1 ("src: add support for setting secmark")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agonftables: don't crash in 'list ruleset' if policy is not set
Sergei Trofimovich [Mon, 16 Sep 2019 07:33:20 +0000 (08:33 +0100)] 
nftables: don't crash in 'list ruleset' if policy is not set

Minimal reproducer:

```
  $ cat nft.ruleset
    # filters
    table inet filter {
        chain prerouting {
            type filter hook prerouting priority -50
        }
    }

    # dump new state
    list ruleset

  $ nft -c -f ./nft.ruleset
    table inet filter {
    chain prerouting {
    Segmentation fault (core dumped)
```

The crash happens in `chain_print_declaration()`:

```
    if (chain->flags & CHAIN_F_BASECHAIN) {
        mpz_export_data(&policy, chain->policy->value,
                        BYTEORDER_HOST_ENDIAN, sizeof(int));
```

Here `chain->policy` is `NULL` (as textual rule does not mention it).

The change is not to print the policy if it's not set
(similar to `chain_evaluate()` handling).

CC: Florian Westphal <fw@strlen.de>
CC: Pablo Neira Ayuso <pablo@netfilter.org>
CC: netfilter-devel@vger.kernel.org
Bug: https://bugzilla.netfilter.org/show_bug.cgi?id=1365
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
Acked-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agojson: tests: fix typo in ct expectation json test
Fernando Fernandez Mancera [Sun, 15 Sep 2019 12:39:55 +0000 (14:39 +0200)] 
json: tests: fix typo in ct expectation json test

The correct form is "ct expectation" not "ct expect". That was causing the
tests/py/ip/object.t json test to fail.

Fixes: 1dd08fcfa07a ("src: add ct expectations support")
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoparser_bison: Fix 'exists' keyword on Big Endian
Phil Sutter [Fri, 13 Sep 2019 15:20:04 +0000 (17:20 +0200)] 
parser_bison: Fix 'exists' keyword on Big Endian

Size value passed to constant_expr_alloc() must correspond with actual
data size, otherwise wrong portion of data will be taken later when
serializing into netlink message.

Booleans require really just a bit, but make type of boolean_keys be
uint8_t (introducing new 'val8' name for it) and pass the data length
using sizeof() to avoid any magic numbers.

While being at it, fix len value in parser_json.c as well although it
worked before due to the value being rounded up to the next multiple of
8.

Fixes: 9fd9baba43c8e ("Introduce boolean datatype and boolean expression")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
5 years agojson: fix type mismatch on "ct expect" json exporting
Fernando Fernandez Mancera [Fri, 13 Sep 2019 11:56:59 +0000 (13:56 +0200)] 
json: fix type mismatch on "ct expect" json exporting

The size field in ct_expect struct should be parsed as json integer and not as
a string. Also, l3proto field is parsed as string and not as an integer. That
was causing a segmentation fault when exporting "ct expect" objects as json.

Fixes: 1dd08fcfa07a ("src: add ct expectations support")
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add synproxy stateful object support
Fernando Fernandez Mancera [Thu, 12 Sep 2019 23:07:05 +0000 (01:07 +0200)] 
src: add synproxy stateful object support

Add support for "synproxy" stateful object. For example (for TCP port 80 and
using maps with saddr):

table ip foo {
synproxy https-synproxy {
mss 1460
wscale 7
timestamp sack-perm
}

synproxy other-synproxy {
mss 1460
wscale 5
}

chain bar {
tcp dport 80 synproxy name "https-synproxy"
synproxy name ip saddr map { 192.168.1.0/24 : "https-synproxy", 192.168.2.0/24 : "other-synproxy" }
}
}

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agolibnftables: use-after-free in exit path
Pablo Neira Ayuso [Wed, 11 Sep 2019 11:52:43 +0000 (13:52 +0200)] 
libnftables: use-after-free in exit path

==29699== Invalid read of size 8
==29699==    at 0x507E140: ct_label_table_exit (ct.c:239)
==29699==    by 0x5091877: nft_exit (libnftables.c:97)
==29699==    by 0x5091877: nft_ctx_free (libnftables.c:297)
[...]
==29699==  Address 0xb251008 is 136 bytes inside a block of size 352 free'd
==29699==    at 0x4C2CDDB: free (vg_replace_malloc.c:530)
==29699==    by 0x509186F: nft_ctx_free (libnftables.c:296)
[...]
==29699==  Block was alloc'd at
==29699==    at 0x4C2DBC5: calloc (vg_replace_malloc.c:711)
==29699==    by 0x508C51D: xmalloc (utils.c:36)
==29699==    by 0x508C51D: xzalloc (utils.c:65)
==29699==    by 0x50916BE: nft_ctx_new (libnftables.c:151)
[...]

Release symbol tables before context object.

Fixes: 45cb29a2ada4 ("src: remove global symbol_table")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agonetlink_delinearize: fix wrong conversion to "list" in ct mark
Fernando Fernandez Mancera [Mon, 9 Sep 2019 19:52:47 +0000 (21:52 +0200)] 
netlink_delinearize: fix wrong conversion to "list" in ct mark

We only prefer "list" representation in "ct event". For any other type of "ct"
use the "or" representation so nft prints "ct mark set ct mark | 0x00000001"
instead of "ct mark set ct mark,0x00000001".

Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1364
Fixes: cb8f81ac3079 ("netlink_delinearize: prefer ct event set foo,bar over 'set foo|bar'")
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agomnl: fix --echo buffer size again
Pablo Neira Ayuso [Tue, 10 Sep 2019 08:23:16 +0000 (10:23 +0200)] 
mnl: fix --echo buffer size again

If restart is triggered with --echo, it causes rules to be duplicated
which is not correct. Remove restart logic.

1. If user passes --echo, use a default 4mb buffer.
2. assume each element in the batch will result in a 1k
   notification.

This passes tests both in x86_64 and s390.

Joint work with Florian Westphal.

Fixes: 877baf9538f6 ("src: mnl: retry when we hit -ENOBUFS")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoparser_json: fix crash on insert rule to bad references
Eric Garver [Tue, 10 Sep 2019 13:46:15 +0000 (09:46 -0400)] 
parser_json: fix crash on insert rule to bad references

Pass the location via the handle so the error leg in
erec_print_list() can reference it. Applies to invalid references
to tables, chains, and indexes.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: shell: add huge transaction from firewalld
Eric Garver [Tue, 10 Sep 2019 13:43:28 +0000 (09:43 -0400)] 
tests: shell: add huge transaction from firewalld

This is borrowed from one of firewalld's test cases.

Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: shell: add huge JSON transaction
Eric Garver [Tue, 10 Sep 2019 13:43:27 +0000 (09:43 -0400)] 
tests: shell: add huge JSON transaction

Expand the test case to also check for returned rule handles in the JSON
output.

Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: shell: verify huge transaction returns expected number of rules
Eric Garver [Tue, 10 Sep 2019 13:43:26 +0000 (09:43 -0400)] 
tests: shell: verify huge transaction returns expected number of rules

Verify that we get the expected number of rules with --echo (i.e. the
reply wasn't truncated).

Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agotests: shell: use-after-free from abort path
Pablo Neira Ayuso [Fri, 9 Aug 2019 12:49:14 +0000 (14:49 +0200)] 
tests: shell: use-after-free from abort path

Rule that fails to be added while holding a bound set triggers
user-after-free from the abort path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoevaluate: flag fwd and queue statements as terminal
Florian Westphal [Fri, 6 Sep 2019 14:43:37 +0000 (16:43 +0200)] 
evaluate: flag fwd and queue statements as terminal

Both queue and fwd statement end evaluation of a rule:

in
... fwd to "eth0" accept
... queue accept

"accept" is redundant and never evaluated in the kernel.
Add the missing "TERMINAL" flag so the evaluation step will catch
any trailing expressions:

nft add rule filter input queue counter
Error: Statement after terminal statement has no effect

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: evaluate: catch invalid 'meta day' values in eval step
Florian Westphal [Thu, 29 Aug 2019 14:09:04 +0000 (16:09 +0200)] 
src: evaluate: catch invalid 'meta day' values in eval step

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agotests: add meta time test cases
Ander Juaristi [Thu, 29 Aug 2019 14:09:03 +0000 (16:09 +0200)] 
tests: add meta time test cases

Signed-off-by: Ander Juaristi <a@juaristi.eus>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agometa: Introduce new conditions 'time', 'day' and 'hour'
Ander Juaristi [Thu, 29 Aug 2019 14:09:02 +0000 (16:09 +0200)] 
meta: Introduce new conditions 'time', 'day' and 'hour'

These keywords introduce new checks for a timestamp, an absolute date (which is converted to a timestamp),
an hour in the day (which is converted to the number of seconds since midnight) and a day of week.

When converting an ISO date (eg. 2019-06-06 17:00) to a timestamp,
we need to substract it the GMT difference in seconds, that is, the value
of the 'tm_gmtoff' field in the tm structure. This is because the kernel
doesn't know about time zones. And hence the kernel manages different timestamps
than those that are advertised in userspace when running, for instance, date +%s.

The same conversion needs to be done when converting hours (e.g 17:00) to seconds since midnight
as well.

The result needs to be computed modulo 86400 in case GMT offset (difference in seconds from UTC)
is negative.

We also introduce a new command line option (-t, --seconds) to show the actual
timestamps when printing the values, rather than the ISO dates, or the hour.

Some usage examples:

time < "2019-06-06 17:00" drop;
time < "2019-06-06 17:20:20" drop;
time < 12341234 drop;
day "Saturday" drop;
day 6 drop;
hour >= 17:00 drop;
hour >= "17:00:01" drop;
hour >= 63000 drop;

We need to convert an ISO date to a timestamp
without taking into account the time zone offset, since comparison will
be done in kernel space and there is no time zone information there.

Overwriting TZ is portable, but will cause problems when parsing a
ruleset that has 'time' and 'hour' rules. Parsing an 'hour' type must
not do time zone conversion, but that will be automatically done if TZ has
been overwritten to UTC.

Hence, we use timegm() to parse the 'time' type, even though it's not portable.
Overwriting TZ seems to be a much worse solution.

Finally, be aware that timestamps are converted to nanoseconds when
transferring to the kernel (as comparison is done with nanosecond
precision), and back to seconds when retrieving them for printing.

We swap left and right values in a range to properly handle
cross-day hour ranges (e.g. 23:15-03:22).

Signed-off-by: Ander Juaristi <a@juaristi.eus>
Reviewed-by: Florian Westphal <fw@strlen.de>
5 years agoevaluate: New internal helper __expr_evaluate_range
Ander Juaristi [Thu, 29 Aug 2019 14:09:01 +0000 (16:09 +0200)] 
evaluate: New internal helper __expr_evaluate_range

This is used by the followup patch to evaluate a range without emitting
an error when the left value is larger than the right one.

This is done to handle time-matching such as
23:00-01:00 -- expr_evaluate_range() will reject this, but
we want to be able to evaluate and then handle this as a request
to match from 23:00 to 1am.

Signed-off-by: Ander Juaristi <a@juaristi.eus>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agotests: shell: check that rule add with index works with echo
Eric Garver [Fri, 6 Sep 2019 00:33:02 +0000 (20:33 -0400)] 
tests: shell: check that rule add with index works with echo

Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agocache: fix --echo with index/position
Eric Garver [Fri, 6 Sep 2019 00:33:01 +0000 (20:33 -0400)] 
cache: fix --echo with index/position

Always call evaluate_cache_add() so it can set special flags - in this
case NFT_CACHE_UPDATE.

Fixes: 01e5c6f0ed03 ("src: add cache level flags")
Signed-off-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agometa: add ibrpvid and ibrvproto support
wenxu [Fri, 30 Aug 2019 04:01:33 +0000 (12:01 +0800)] 
meta: add ibrpvid and ibrvproto support

This allows you to match the bridge pvid and vlan protocol, for
instance:

nft add rule bridge firewall zones meta ibrvproto vlan
nft add rule bridge firewall zones meta ibrpvid 100

Signed-off-by: wenxu <wenxu@ucloud.cn>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: json: add support for element deletion
Florian Westphal [Thu, 29 Aug 2019 09:36:20 +0000 (11:36 +0200)] 
src: json: add support for element deletion

also add a test case.

Fixes: a87f2a2227be2 ("netfilter: support for element deletion")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
5 years agonetfilter: support for element deletion
Ander Juaristi [Tue, 9 Jul 2019 18:03:52 +0000 (20:03 +0200)] 
netfilter: support for element deletion

This patch implements element deletion from ruleset.

Example:

table ip set-test {
set testset {
type ipv4_addr;
flags timeout;
}

chain outputchain {
policy accept;
type filter hook output priority filter;

delete @testset { ip saddr }
}
}

Signed-off-by: Ander Juaristi <a@juaristi.eus>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agosrc: secmark: fix brace indentation and missing quotes in selctx output
Eric Jallot [Tue, 20 Aug 2019 15:50:05 +0000 (17:50 +0200)] 
src: secmark: fix brace indentation and missing quotes in selctx output

Before patch:
 # nft list secmarks | tee rules.nft
 table inet t {
        secmark s {
                system_u:object_r:ssh_server_packet_t:s0        }
 }
 # nft flush ruleset
 # nft -f rules.nft
 rules.nft:3:11-11: Error: syntax error, unexpected colon
                system_u:object_r:ssh_server_packet_t:s0

Colon is not allowed in strings and breaks nft -f.
So move to quoted string in selctx output.

After patch:
 # nft list secmarks | tee rules.nft
 table inet t {
         secmark s {
                 "system_u:object_r:ssh_server_packet_t:s0"
         }
 }
 # nft flush ruleset
 # nft -f rules.nft

Fixes: 3bc84e5c ("src: add support for setting secmark")
Signed-off-by: Eric Jallot <ejallot@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agobuild: Bump version to v0.9.2 v0.9.2
Pablo Neira Ayuso [Mon, 19 Aug 2019 10:06:32 +0000 (12:06 +0200)] 
build: Bump version to v0.9.2

Update dependency on libnftnl. Missing nf_synproxy.h in Makefile.am too.

Update release name based Jazz series, Fats Waller performing "Scram":
https://www.youtube.com/watch?v=c9-noJc9ifI

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: libnftnl: run single-initcalls only once
Florian Westphal [Fri, 16 Aug 2019 14:42:34 +0000 (16:42 +0200)] 
src: libnftnl: run single-initcalls only once

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agotests: make sure i is defined
Florian Westphal [Fri, 16 Aug 2019 10:32:19 +0000 (12:32 +0200)] 
tests: make sure i is defined

The test script can die in case there are severe problems,
such as rlen being 0 -- in that case i is undefined and script
evaluation is aborted.

Found during nft development, no existing test case shows this
problem.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: don't check asciidoc output with xmllint
Arturo Borrero Gonzalez [Thu, 15 Aug 2019 12:24:28 +0000 (14:24 +0200)] 
doc: don't check asciidoc output with xmllint

We don't need to check asciidoc output with xmllint because the generated XML
is generated by a tool, not by a human. Moreover, xmllint can cause
problems because it will try to download the DTD and that is problematic in
build systems with no network access.

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: json: fix constant parsing on bigendian
Florian Westphal [Wed, 14 Aug 2019 11:45:19 +0000 (13:45 +0200)] 
src: json: fix constant parsing on bigendian

json restore is broken on big-endian because we errounously
passed uint8_t with 64 bit size indicator.

On bigendian, this causes all values to get shifted by 56 bit,
this will then cause the eval step to bail because all values
are outside of the 8bit 0-255 protocol range.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: json: support json restore for "th" pseudoheader
Florian Westphal [Wed, 14 Aug 2019 11:34:52 +0000 (13:34 +0200)] 
src: json: support json restore for "th" pseudoheader

Json output could not be restored back by nft because it did
not recognize the new "th" pseudoheader.

Fixes: a43a696443a150f44 ("proto: add pseudo th protocol to match d/sport in generic way")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agogmputil: assert length is non-zero
Pablo Neira Ayuso [Wed, 14 Aug 2019 08:07:22 +0000 (10:07 +0200)] 
gmputil: assert length is non-zero

Importing, exporting and byteswapping zero length data should not
happen.

Use macro definition so we know from where the assertion is triggered in
the code for easier diagnosing in the future.

When importing datatype.h from gmputil.h, it seems gcc complains on
missing declarations in json.h.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: mnl: retry when we hit -ENOBUFS
Florian Westphal [Tue, 13 Aug 2019 20:12:46 +0000 (22:12 +0200)] 
src: mnl: retry when we hit -ENOBUFS

tests/shell/testcases/transactions/0049huge_0

still fails with ENOBUFS error after endian fix done in
previous patch.  Its enough to increase the scale factor (4)
on s390x, but rather than continue with these "guess the proper
size" game, just increase the buffer size and retry up to 3 times.

This makes above test work on s390x.

So, implement what Pablo suggested in the earlier commit:
    We could also explore increasing the buffer and retry if
    mnl_nft_socket_sendmsg() hits ENOBUFS if we ever hit this problem again.

v2: call setsockopt unconditionally, then increase on error.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: parser: fix parsing of chain priority and policy on bigendian
Florian Westphal [Tue, 13 Aug 2019 20:12:45 +0000 (22:12 +0200)] 
src: parser: fix parsing of chain priority and policy on bigendian

tests/shell/testcases/flowtable/0001flowtable_0
tests/shell/testcases/nft-f/0008split_tables_0
fail the 'dump compare' on s390x.
The priority (10) turns to 0, and accept turned to drop.

Problem is that '$1' is a 64bit value -- then we pass the address
and import 'int' -- we then get the upper all zero bits.

Add a 32bit interger type and use that.

v2: add uint32_t type to union, v1 used temporary value instead.

Fixes: 627c451b2351 ("src: allow variables in the chain priority specification")
Fixes: dba4a9b4b5fe ("src: allow variable in chain policy")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: fix jumps on bigendian arches
Florian Westphal [Tue, 13 Aug 2019 20:12:44 +0000 (22:12 +0200)] 
src: fix jumps on bigendian arches

table bla {
  chain foo { }
  chain bar { jump foo }
 }
}

Fails to restore on big-endian platforms:
jump.nft:5:2-9: Error: Could not process rule: No such file or directory
 jump foo

nft passes a 0-length name to the kernel.

This is because when we export the value (the string), we provide
the size of the destination buffer.

In earlier versions, the parser allocated the name with the same
fixed size and all was fine.

After the fix, the export places the name in the wrong location
in the destination buffer.

This makes tests/shell/testcases/chains/0001jumps_0 work on s390x.

v2: convert one error check to a BUG(), it should not happen unless
    kernel abi is broken.

Fixes: 142350f154c78 ("src: invalid read when importing chain name")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: mnl: fix setting rcvbuffer size
Florian Westphal [Tue, 13 Aug 2019 18:44:08 +0000 (20:44 +0200)] 
src: mnl: fix setting rcvbuffer size

Kernel expects socklen_t (int).
Using size_t causes kernel to read upper 0-bits.

This caused tests/shell/testcases/transactions/0049huge_0
to fail on s390x -- it uses 'echo' mode and will quickly
overrun the tiny buffer size set due to this bug.

Fixes: 89c82c261bb5 ("mnl: estimate receiver buffer size")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: add json test for vlan rule fix
M. Braun [Sun, 11 Aug 2019 10:16:03 +0000 (12:16 +0200)] 
tests: add json test for vlan rule fix

This fixes

ERROR: did not find JSON equivalent for rule 'ether type vlan ip
protocol 1 accept'

when running

./nft-test.py -j bridge/vlan.t

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoinclude: refresh nf_tables.h cached copy
Pablo Neira Ayuso [Fri, 9 Aug 2019 11:42:08 +0000 (13:42 +0200)] 
include: refresh nf_tables.h cached copy

Refresh it to fetch what we have in 5.3-rc1.

Remove NFT_OSF_F_VERSION definition, this is already available in
include/linux/netfilter/nf_tables.h

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: shell: move chain priority and policy to chain folder
Pablo Neira Ayuso [Fri, 9 Aug 2019 09:16:00 +0000 (11:16 +0200)] 
tests: shell: move chain priority and policy to chain folder

Move new chain tests for variable priority and policy to chain folder.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: allow variable in chain policy
Fernando Fernandez Mancera [Fri, 2 Aug 2019 10:12:10 +0000 (12:12 +0200)] 
src: allow variable in chain policy

This patch allows you to use variables in chain policy definition, e.g.

define default_policy = "accept"

add table ip foo
add chain ip foo bar {type filter hook input priority filter; policy $default_policy}

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: allow variables in the chain priority specification
Fernando Fernandez Mancera [Fri, 2 Aug 2019 10:12:08 +0000 (12:12 +0200)] 
src: allow variables in the chain priority specification

This patch allows you to use variables in chain priority definitions,
e.g.

define prio = filter
define prionum = 10
define prioffset = "filter - 150"

add table ip foo
add chain ip foo bar { type filter hook input priority $prio; }
add chain ip foo ber { type filter hook input priority $prionum; }
add chain ip foo bor { type filter hook input priority $prioffset; }

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: remove global symbol_table
Pablo Neira Ayuso [Wed, 7 Aug 2019 22:30:28 +0000 (00:30 +0200)] 
src: remove global symbol_table

Store symbol tables in context object instead. Use the nft_ctx object to
store the dynamic symbol table. Pass it on to the parse_ctx object so
this can be accessed from the parse routines. This dynamic symbol table
is also accesible from the output_ctx object for print routines.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: add parse_ctx object
Pablo Neira Ayuso [Wed, 7 Aug 2019 21:51:18 +0000 (23:51 +0200)] 
src: add parse_ctx object

This object stores the dynamic symbol tables that are loaded from files.
Pass this object to datatype parse functions, although this new
parameter is not used yet, this is just a preparation patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agocache: incorrect flush flag for table/chain
Laura Garcia Liebana [Wed, 31 Jul 2019 11:27:46 +0000 (13:27 +0200)] 
cache: incorrect flush flag for table/chain

After the new cache system, nft raises a table error flushing a chain in
a transaction.

 # nft "flush chain ip nftlb filter-newfarm ; \
    add rule ip nftlb filter-newfarm update \
    @persist-newfarm {  ip saddr : ct mark } ; \
    flush chain ip nftlb nat-newfarm"
 Error: No such file or directory
 flush chain ip nftlb filter-newfarm ; add rule ip nftlb (...)
                                                   ^^^^^

This patch sets the cache flag properly to save this case.

Fixes: 01e5c6f0ed031 ("src: add cache level flags")
Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: Fix dumping vlan rules
M. Braun [Mon, 15 Jul 2019 16:59:01 +0000 (18:59 +0200)] 
src: Fix dumping vlan rules

Given the following bridge rules:
1. ip protocol icmp accept
2. ether type vlan vlan type ip ip protocol icmp accept

The are currently both dumped by "nft list ruleset" as
1. ip protocol icmp accept
2. ip protocol icmp accept

Though, the netlink code actually is different

bridge filter FORWARD 4
  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000008 ]
  [ payload load 1b @ network header + 9 => reg 1 ]
  [ cmp eq reg 1 0x00000001 ]
  [ immediate reg 0 accept ]

bridge filter FORWARD 5 4
  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000081 ]
  [ payload load 2b @ link header + 16 => reg 1 ]
  [ cmp eq reg 1 0x00000008 ]
  [ payload load 1b @ network header + 9 => reg 1 ]
  [ cmp eq reg 1 0x00000001 ]
  [ immediate reg 0 accept ]

What happens here is that:

1. vlan type ip kills ether type vlan
2. ip protocol icmp kills vlan type ip

Fix this by avoiding the removal of all vlan statements
in the given example.

Signed-off-by: Michael Braun <michael-dev@fami-braun.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: Call bison with -Wno-yacc to silence warnings
Phil Sutter [Tue, 23 Jul 2019 12:30:39 +0000 (14:30 +0200)] 
src: Call bison with -Wno-yacc to silence warnings

Bison-3.3 significantly increased warnings for POSIX incompatibilities,
it now complains about missing support for %name-prefix, %define,
%destructor and string literals. The latter applies to parameter of
%name-prefix and all relevant %token statements.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoparser_bison: Fix for deprecated statements
Phil Sutter [Tue, 23 Jul 2019 09:53:39 +0000 (11:53 +0200)] 
parser_bison: Fix for deprecated statements

Bison-3.3 started to warn about:

/home/n0-1/git/nftables/src/parser_bison.y:117.1-19: warning: deprecated directive, use ‘%define api.prefix {nft_}’ [-Wdeprecated]
    117 | %name-prefix "nft_"
        | ^~~~~~~~~~~~~~~~~~~
/home/n0-1/git/nftables/src/parser_bison.y:119.1-12: warning: deprecated directive, use ‘%define api.pure’ [-Wdeprecated]
  119 | %pure-parser
      | ^~~~~~~~~~~~
/home/n0-1/git/nftables/src/parser_bison.y:124.1-14: warning: deprecated directive, use ‘%define parse.error verbose’ [-Wdeprecated]
  124 | %error-verbose
      | ^~~~~~~~~~~~~~

Replace the last two as suggested but leave the first one in place as
that causes compilation errors in scanner.l - flex seems not to pick up
the changed internal symbol names.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoscanner: don't rely on fseek for input stream repositioning
Florian Westphal [Sun, 21 Jul 2019 10:18:31 +0000 (12:18 +0200)] 
scanner: don't rely on fseek for input stream repositioning

It doesn't work when reading from a pipe, leading to parser
errors in case of 'cat foo | nft -f -', whereas 'nft -f < foo'
works fine.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1354
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agodoc: fib: explain example in more detail
Florian Westphal [Sun, 21 Jul 2019 10:43:05 +0000 (12:43 +0200)] 
doc: fib: explain example in more detail

As noted by Felix Dreissig, fib documentation is quite terse, so explain
the 'saddr . iif' example with a few more words.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1220
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: Sync comments with current expr definition
Brett Mastbergen [Tue, 23 Jul 2019 17:36:49 +0000 (13:36 -0400)] 
src: Sync comments with current expr definition

ops has been removed, and etype has been added

Signed-off-by: Brett Mastbergen <bmastbergen@untangle.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: shell: check for table re-definition usecase
Florian Westphal [Sun, 21 Jul 2019 00:14:07 +0000 (02:14 +0200)] 
tests: shell: check for table re-definition usecase

Make sure nft behaves like 0.9.0 -- the ruleset

flush ruleset
table inet filter {
}
table inet filter {
      chain test {
        counter
    }
}

loads again without crashing/generating an error message.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1351
Fixes: e5382c0d08e3c ("src: Support intra-transaction rule references")
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agocache: add NFT_CACHE_UPDATE and NFT_CACHE_FLUSHED flags
Pablo Neira Ayuso [Tue, 23 Jul 2019 13:03:23 +0000 (15:03 +0200)] 
cache: add NFT_CACHE_UPDATE and NFT_CACHE_FLUSHED flags

NFT_CACHE_FLUSHED tells cache_update() to skip the netlink dump to
populate the cache, since the existing ruleset is going to flushed by
this batch.

NFT_CACHE_UPDATE tells rule_evaluate() to perform incremental updates to
the cache based on the existing batch, this is required by the rule
commands that use the index and the position selectors.

This patch removes cache_flush() which is not required anymore. This
cache removal is coming too late, in the evaluation phase, after the
initial cache_update() invocation.

Be careful with NFT_CACHE_UPDATE, this flag needs to be left in place if
NFT_CACHE_FLUSHED is set on.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agorule: removed duplicate member initializer.
Jeremy Sowden [Fri, 19 Jul 2019 11:10:10 +0000 (12:10 +0100)] 
rule: removed duplicate member initializer.

Initialization of a netlink_ctx included two initializers for .nft.
Removed one of them.

Fixes: 2dc07bcd7eaa ("src: pass struct nft_ctx through struct netlink_ctx")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agolibnftables: get rid of repeated initialization of netlink_ctx
Jeremy Sowden [Fri, 19 Jul 2019 11:10:09 +0000 (12:10 +0100)] 
libnftables: get rid of repeated initialization of netlink_ctx

Most members in the context don't change, so there is no need to memset
it and reassign them on every iteration.  Moved that code out of the
loop.

Fixes: a72315d2bad4 ("src: add rule batching support")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agosrc: osf: fix snprintf -Wformat-truncation warning
Fernando Fernandez Mancera [Mon, 22 Jul 2019 16:34:08 +0000 (18:34 +0200)] 
src: osf: fix snprintf -Wformat-truncation warning

Fedora 30 uses very recent gcc (version 9.1.1 20190503 (Red Hat 9.1.1-1)),
osf produces following warnings:

-Wformat-truncation warning have been introduced in the version 7.1 of gcc.
Also, remove a unneeded address check of "tmp + 1" in nf_osf_strchr().

nfnl_osf.c: In function ‘nfnl_osf_load_fingerprints’:
nfnl_osf.c:292:39: warning: ‘%s’ directive output may be truncated writing
up to 1023 bytes into a region of size 128 [-Wformat-truncation=]
  292 |   cnt = snprintf(obuf, sizeof(obuf), "%s,", pbeg);
      |                                       ^~
nfnl_osf.c:292:9: note: ‘snprintf’ output between 2 and 1025 bytes into a
destination of size 128
  292 |   cnt = snprintf(obuf, sizeof(obuf), "%s,", pbeg);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nfnl_osf.c:302:46: warning: ‘%s’ directive output may be truncated writing
up to 1023 bytes into a region of size 32 [-Wformat-truncation=]
  302 |    cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
      |                                              ^~
nfnl_osf.c:302:10: note: ‘snprintf’ output between 1 and 1024 bytes into a
destination of size 32
  302 |    cnt = snprintf(f.genre, sizeof(f.genre), "%s", pbeg);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nfnl_osf.c:309:49: warning: ‘%s’ directive output may be truncated writing
up to 1023 bytes into a region of size 32 [-Wformat-truncation=]
  309 |   cnt = snprintf(f.version, sizeof(f.version), "%s", pbeg);
      |                                                 ^~
nfnl_osf.c:309:9: note: ‘snprintf’ output between 1 and 1024 bytes into a
destination of size 32
  309 |   cnt = snprintf(f.version, sizeof(f.version), "%s", pbeg);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nfnl_osf.c:317:47: warning: ‘%s’ directive output may be truncated writing
up to 1023 bytes into a region of size 32 [-Wformat-truncation=]
  317 |       snprintf(f.subtype, sizeof(f.subtype), "%s", pbeg);
      |                                               ^~
nfnl_osf.c:317:7: note: ‘snprintf’ output between 1 and 1024 bytes into a
destination of size 32
  317 |       snprintf(f.subtype, sizeof(f.subtype), "%s", pbeg);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>