]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
7 years agotests: shell: add quotes when using <<<-style here document
Florian Westphal [Fri, 8 Jun 2018 12:41:08 +0000 (14:41 +0200)] 
tests: shell: add quotes when using <<<-style here document

bash 4.3.30 removes newlines in RULESET when "" are omitted, which
then causes nft -f to complain about invalid syntax.

As a result, all test cases that use this here-doc style fail.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agobuild: missing headers file in Makefile.am
Florian Westphal [Fri, 8 Jun 2018 11:16:07 +0000 (13:16 +0200)] 
build: missing headers file in Makefile.am

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agobuild: Bump version to v0.9.0
Florian Westphal [Fri, 8 Jun 2018 11:00:30 +0000 (13:00 +0200)] 
build: Bump version to v0.9.0

Update dependency on libnftnl.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agobuild: update release name
Pablo Neira Ayuso [Fri, 8 Jun 2018 10:56:03 +0000 (12:56 +0200)] 
build: update release name

Dedicated to Al Capp cartoonist:

https://en.wikipedia.org/wiki/Fearless_Fosdick

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: use 100ms for set timeout
Florian Westphal [Thu, 7 Jun 2018 16:31:59 +0000 (18:31 +0200)] 
tests: use 100ms for set timeout

Pablo reports set test fails with HZ=250, as it lists "324ms" instead
of "321".  This is because of rounding errors that occur when converting
from user-side millisecond scale to kernel-internal jiffies one.

use 100ms for now to avoid this error.
Alternatives would be to store use-provided value in kernel or to avoid
the conversions; this would require a change to make timeout independent from
jiffies on kernel side.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: netlink_delinarize: don't stop when encountering unsupported expression
Florian Westphal [Wed, 23 May 2018 21:30:24 +0000 (23:30 +0200)] 
src: netlink_delinarize: don't stop when encountering unsupported expression

The error message is still shown, but try to make sense of further
expressions (if any).

I tried to replace the expression by a textual representation.

Two variants I tested are:
1. append as comment:
   ip saddr 127.0.0.2 drop comment "unknown expression 'foo'"
   This allows nft -f, but it adds/alters a comment.

2. substitute in-place (i.e., add a constant expression
   with the error message instead of 'unkown' message:

   unknown expression "foo" ip saddr 127.0.0.2 drop

This won't allow 'nft -f' however.
I'm not sure this is a problem, in any case we lose information
if we can't find an expression (e.g. added by newer version).

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: shell: add crash reproducer
Florian Westphal [Tue, 29 May 2018 14:16:44 +0000 (16:16 +0200)] 
tests: shell: add crash reproducer

Two reports point to a crash in nft when 'flush' is provided
on existing ruleset.  In that case, nft will crash with a null-ptr
dereference.

"evaluate: do not inconditionally update cache from flush command"
causes the commit to fail due to a cache inconsistency, we then trip
over NULL location->indesc.  Cause of 2nd bug not known yet, not sure
how to fix cache issue either, so only adding reproducer so this can be
fixed later.

Without erec bug, the (errnoeous) error message would be
Could not process rule: File exists

Reported-by: Oleksandr Natalenko <oleksandr@natalenko.name>
Reported-by: Timothy Redaelli <tredaelli@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: do not reset generation ID on ruleset flush
Pablo Neira Ayuso [Thu, 7 Jun 2018 11:35:52 +0000 (13:35 +0200)] 
src: do not reset generation ID on ruleset flush

If 'flush ruleset' command is done, release the cache but still keep the
generation ID around. Hence, follow up calls to cache_update() will
assume that cache is updated and will not perform a netlink dump.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotest: py: Add test cases for socket matching
Máté Eckl [Mon, 4 Jun 2018 07:36:32 +0000 (09:36 +0200)] 
test: py: Add test cases for socket matching

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoinclude: update linux/netfilter_ipv4.h
Máté Eckl [Mon, 4 Jun 2018 09:58:17 +0000 (11:58 +0200)] 
include: update linux/netfilter_ipv4.h

Update file from <kerneldir>/usr/include/linux/netfilter_ipv4.h.

Reasons:
 - New values have been introduced in nf_ip_hook_priorities.
 - include limits.h was missing

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: Introduce socket matching
Máté Eckl [Thu, 31 May 2018 18:06:16 +0000 (20:06 +0200)] 
src: Introduce socket matching

For now it can only match sockets with IP(V6)_TRANSPARENT socket option
set. Example:

 table inet sockin {
chain sockchain {
type filter hook prerouting priority -150; policy accept;
socket transparent 1 mark set 0x00000001 nftrace set 1 counter packets 9 bytes 504 accept

}
 }

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoexpr: extend fwd statement to support address and family
Pablo Neira Ayuso [Thu, 9 Nov 2017 02:42:55 +0000 (03:42 +0100)] 
expr: extend fwd statement to support address and family

Allow to forward packets through to explicit destination and interface.

  nft add rule netdev x y fwd ip to 192.168.2.200 device eth0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: connlimit support
Pablo Neira Ayuso [Thu, 31 May 2018 16:08:06 +0000 (18:08 +0200)] 
src: connlimit support

This patch adds support for the new connlimit stateful expression, that
provides a mapping with the connlimit iptables extension through meters.
eg.

  nft add rule filter input tcp dport 22 \
meter test { ip saddr ct count over 2 } counter reject

This limits the maximum amount incoming of SSH connections per source
address up to 2 simultaneous connections.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonat: Eliminate misuse of AF_*
Máté Eckl [Wed, 6 Jun 2018 09:13:35 +0000 (11:13 +0200)] 
nat: Eliminate misuse of AF_*

Although the value of AF_INET and NFPROTO_IPV4 is the same, the use of
AF_INET was misleading when checking the proto family.
Same with AF_INET6.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: explicitly deny concatenated types in interval sets
Phil Sutter [Wed, 6 Jun 2018 11:21:49 +0000 (13:21 +0200)] 
evaluate: explicitly deny concatenated types in interval sets

Previously, this triggered a program abort:

| # nft add table ip t
| # nft add set ip t my_set '{ type ipv4_addr . inet_service ; flags interval ; }'
| # nft add element ip t my_set '{10.0.0.1 . tcp }'
| BUG: invalid range expression type concat
| nft: expression.c:1085: range_expr_value_low: Assertion `0' failed.

With this patch in place, the 'add set' command above gives an error
message:

| # nft add set ip t my_set3 '{ type ipv4_addr . inet_service ; flags interval ; }'
| Error: concatenated types not supported in interval sets
| add set ip t my_set3 { type ipv4_addr . inet_service ; flags interval ; }
|                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonft.8: Fix reject statement documentation
Phil Sutter [Wed, 6 Jun 2018 08:56:26 +0000 (10:56 +0200)] 
nft.8: Fix reject statement documentation

First of all, 'with icmp6' is invalid, expected is 'with icmpv6'. In
addition to that, parameter 'type' expects an icmp*_code type, not
icmp*_type. The respective table column was already correct, but in
synopsis it was wrong.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure.ac: fix typo in docbook2x error message
Fernando Fernandez Mancera [Tue, 5 Jun 2018 13:15:45 +0000 (15:15 +0200)] 
configure.ac: fix typo in docbook2x error message

The correct name is "docbook2x-man" not "docbookx2-man".

Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agolog: Add support for audit logging
Phil Sutter [Fri, 1 Jun 2018 15:15:07 +0000 (17:15 +0200)] 
log: Add support for audit logging

This is implemented via a pseudo log level. The kernel ignores any other
parameter, so reject those at evaluation stage. Audit logging is
therefore simply a matter of:

| log level audit

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: ip6/flowtable.t: Add missing JSON expected output
Phil Sutter [Fri, 1 Jun 2018 15:32:12 +0000 (17:32 +0200)] 
tests/py: ip6/flowtable.t: Add missing JSON expected output

The output differs from input in added size property.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Review large number parsing/printing
Phil Sutter [Fri, 1 Jun 2018 15:32:11 +0000 (17:32 +0200)] 
JSON: Review large number parsing/printing

When parsing large (uint64_t) values, capital 'I' has to be used in
format string. While being at it, make sure JSON output code handles
those variables correctly, too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Fix replace command parser
Phil Sutter [Fri, 1 Jun 2018 15:32:10 +0000 (17:32 +0200)] 
JSON: Fix replace command parser

This was completely broken. Yet another indicator the JSON API part of
testsuite is insufficient.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Fix add rule with index
Phil Sutter [Fri, 1 Jun 2018 15:32:09 +0000 (17:32 +0200)] 
JSON: Fix add rule with index

This was missed by commit fb557b5546084 ("JSON: Sort out rule position
and handles in general"): When adding a rule, the parser would still
expect "pos" property instead of "index".

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Simplify verdict statement parsing
Phil Sutter [Fri, 1 Jun 2018 15:32:08 +0000 (17:32 +0200)] 
JSON: Simplify verdict statement parsing

Reuse verdict expression parsing routine. Since the statement simply
wraps an expression, this change is an obvious one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Simplify wildcard expression
Phil Sutter [Fri, 1 Jun 2018 15:32:07 +0000 (17:32 +0200)] 
JSON: Simplify wildcard expression

Instead of dedicating an object for it ('{ "*": null }'), just use a
string consisting of an asterisk as sole character.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: Merge wildcard_rhs_expr and wildcard_stmt_expr
Phil Sutter [Fri, 1 Jun 2018 15:32:06 +0000 (17:32 +0200)] 
parser_bison: Merge wildcard_rhs_expr and wildcard_stmt_expr

The two are absolutely identical, just referenced at different places.
Since there is no need for the distinction, just merge them into one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Add missing JSON equivalent for rule in inet/tcp.t
Phil Sutter [Mon, 28 May 2018 16:51:09 +0000 (18:51 +0200)] 
tests/py: Add missing JSON equivalent for rule in inet/tcp.t

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Add missing JSON equivalent for inet/sets.t
Phil Sutter [Mon, 28 May 2018 16:51:08 +0000 (18:51 +0200)] 
tests/py: Add missing JSON equivalent for inet/sets.t

This adds the missing JSON variant for the two rules which are supposed
to work.

Fixes: 2efbdf7b8fcf7 ("tests: py: allow to specify sets with a timeout")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Adjust JSON for changes in any/ct.t
Phil Sutter [Mon, 28 May 2018 16:51:07 +0000 (18:51 +0200)] 
tests/py: Adjust JSON for changes in any/ct.t

Commit 71624f25f22b1 ("tests: py: add expires tests with different time
bases") removed two testcases and added five other ones, adjust JSON
equivalent and recorded output to those changes.

Fixes: 71624f25f22b1 ("tests: py: add expires tests with different time bases")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Simplify immediate value parsing
Phil Sutter [Mon, 28 May 2018 16:51:06 +0000 (18:51 +0200)] 
JSON: Simplify immediate value parsing

Since an explicit "immediate" expression doesn't exist in JSON (the
values are represented as plain JSON string/integer/boolean types),
there is no need for json_parse_immediate_expr() to comply to the common
expression parser parameter format.

Apart from that:

* Drop CTX_F_RHS checks - caller assures sane context already.
* Improve error message a bit for denied JSON types.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Fix parsing of meter statement key
Phil Sutter [Mon, 28 May 2018 16:51:05 +0000 (18:51 +0200)] 
JSON: Fix parsing of meter statement key

The key must be a set elem expression, but if a "regular" expression was
provided (which should be commonly accepted in case no set elem specific
properties are required), the resulting object tree crashed libnftables.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Improve prefix expression parsing error message a bit
Phil Sutter [Mon, 28 May 2018 16:51:04 +0000 (18:51 +0200)] 
JSON: Improve prefix expression parsing error message a bit

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Simplify tcp option expression parsing a bit
Phil Sutter [Mon, 28 May 2018 16:51:03 +0000 (18:51 +0200)] 
JSON: Simplify tcp option expression parsing a bit

When parsing the optional "field" property, use return code of
json_unpack() directly to check if it was present or not.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Fix parsing and printing of limit objects
Phil Sutter [Mon, 28 May 2018 16:51:02 +0000 (18:51 +0200)] 
JSON: Fix parsing and printing of limit objects

Fix parsing and printing of named limit objects by aligning the code
with parser/printer of anonymous ones.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Review set elem expressions
Phil Sutter [Mon, 28 May 2018 16:51:01 +0000 (18:51 +0200)] 
JSON: Review set elem expressions

* There is no need to prefix element-specific properties with 'elem_',
  they can't conflict.
* In json_parse_set_stmt(), searching for above properties is pointless
  since that's already done by called function.
* Fix potential NULL-pointer deref in json_parse_set_elem_expr_stmt():
  json_parse_flagged_expr() may return NULL.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Review meter statement support
Phil Sutter [Mon, 28 May 2018 16:51:00 +0000 (18:51 +0200)] 
JSON: Review meter statement support

Meter name being optional seems to come from old flow statement, so
don't support this. Also add size support as was recently added to
standard syntax.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Sort out rule position and handles in general
Phil Sutter [Mon, 28 May 2018 16:50:59 +0000 (18:50 +0200)] 
JSON: Sort out rule position and handles in general

First of all, don't print the position property when listing rules. This
was there only because libnftnl JSON output has it too, but since the
preferred way to *add* a rule at some location is via 'handle' keyword,
keeping "position" in output would be non-intuitive. Changing "position"
property name to "handle" instead is also a no-go since that would clash
with the real rule handle.

Secondly, turn all handle output on regardless of octx->handle setting.
For a programmatic API like JSON, this should be fine.

Thirdly, fix rule locations when parsing JSON: Respect "handle" property
for CMD_INSERT and CMD_ADD and ignore "pos" at all (actually even a
typo, should have read "position"). Also support "index" property
recently added to standard syntax.

Finally, adjust nft-test.py for the above changes: There is no
"position" property to drop from rule output, and "handle" property will
always be present.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Disallow non-array concat expression values
Phil Sutter [Mon, 28 May 2018 16:50:58 +0000 (18:50 +0200)] 
JSON: Disallow non-array concat expression values

Concat expressions with just a single element don't make sense, so
there's no point in supporting a shorter syntax for this.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoJSON: Use "type" for CT helper object
Phil Sutter [Mon, 28 May 2018 16:50:57 +0000 (18:50 +0200)] 
JSON: Use "type" for CT helper object

Property name "helper" was a bit unclear, "type" is better.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoobjref: Use "ct helper" for NFT_OBJECT_CT_HELPER
Phil Sutter [Mon, 28 May 2018 16:50:56 +0000 (18:50 +0200)] 
objref: Use "ct helper" for NFT_OBJECT_CT_HELPER

Change name of NFT_OBJECT_CT_HELPER in objref_type table to "ct helper"
for consistency. Note that this is not used in regular nft output since
objref_stmt_print() treats this object type separately.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agostatement: old kernels are allowing burst zero, don't print it
Pablo Neira Ayuso [Wed, 30 May 2018 10:40:32 +0000 (12:40 +0200)] 
statement: old kernels are allowing burst zero, don't print it

Don't print limit burst zero which was the default value in old kernels,
this is not allowed in more recent kernels that now operate like
iptables xt_limit which is what users are expecting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: remove nft_objref module on cleanup
Pablo Neira Ayuso [Wed, 30 May 2018 10:34:46 +0000 (12:34 +0200)] 
tests: shell: remove nft_objref module on cleanup

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agostatement: don't print burst if equals 5
Pablo Neira Ayuso [Tue, 29 May 2018 11:52:08 +0000 (13:52 +0200)] 
statement: don't print burst if equals 5

This is the default value we use if not specified, don't print it just
like we do in iptables.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agometers: do not set a defaut meter size from userspace
Florian Westphal [Tue, 29 May 2018 11:47:11 +0000 (13:47 +0200)] 
meters: do not set a defaut meter size from userspace

doing this breaks with older kernels as it will pick a set without
and update callback.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoinclude: update nf_tables.h
Máté Eckl [Mon, 28 May 2018 23:17:42 +0000 (01:17 +0200)] 
include: update nf_tables.h

It seems tracing ABI got broken because the header file has been
manually updated.

Joint work with Pablo.

Fixes: 0f8302635ad3 ("src: print 'handle' attribute in tables")
Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: py: fix race in deleting element
Pablo Neira Ayuso [Fri, 25 May 2018 08:43:00 +0000 (10:43 +0200)] 
tests: py: fix race in deleting element

Sometimes we may hit this because script is too slow to remove the
element with timeout from the set.

inet/sets.t: ERROR: line 18: I cannot delete element  dead::beef timeout 1s from the set set2

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: incorrect handling of comments and timeouts with mapping
Pablo Neira Ayuso [Fri, 25 May 2018 10:30:57 +0000 (12:30 +0200)] 
segtree: incorrect handling of comments and timeouts with mapping

Check if expression is a mapping to do the right handling.

Fixes: 35fedcf540bf ("segtree: missing comments in range and prefix expressions in sets")
Fixes: be90e03dd1fa ("segtree: add timeout for range and prefix expressions in sets")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agofix printing of "tcp flags syn" and "tcp flags == syn" expressions
Sabrina Dubroca [Fri, 25 May 2018 13:23:16 +0000 (15:23 +0200)] 
fix printing of "tcp flags syn" and "tcp flags == syn" expressions

Commit 6979625686ec ("relational: Eliminate meta OPs") introduced some
bugs when printing bitmask types.

First, during the post-processing phase of delinearization, the
expression for "tcp flags syn" (PAYLOAD & flag != 0) gets converted to
PAYLOAD == flag, which is not equivalent. This should be
PAYLOAD (IMPL) flag.

Then, during output, the "==" sign from "tcp flags == syn" is dropped,
because the bitmask condition in must_print_eq_op() was removed. Let's
restore it, so that "tcp flags == syn" doesn't get printed as
"tcp flags syn". An extra check for value types is added, so that we
don't start printing "==" for sets such as "tcp flags {syn,ack}"

Finally, add a regression test for this particular case.

Fixes: 6979625686ec ("relational: Eliminate meta OPs")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosegtree: add timeout for range and prefix expressions in sets
Pablo Neira Ayuso [Wed, 16 May 2018 21:03:51 +0000 (23:03 +0200)] 
segtree: add timeout for range and prefix expressions in sets

# nft add table x
 # nft add set x y { type ipv4_addr\; flags timeout,interval\; }
 # nft add element x y { 7.4.4.5-8.8.8.8 comment "good guy" timeout 30s}
 # nft list ruleset
 table ip x {
       set y {
                type ipv4_addr
                flags interval,timeout
                elements = { 7.4.4.5-8.8.8.8 timeout 30s expires 27s956ms comment "good guy" }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: chain dependency validation with maps
Pablo Neira Ayuso [Wed, 23 May 2018 10:46:05 +0000 (12:46 +0200)] 
tests: shell: chain dependency validation with maps

Just like 4b6fb07de07a ("tests: shell: more chain dependency
validation") but test chain dependency in jumps from maps.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: non-base chain loops
Pablo Neira Ayuso [Wed, 23 May 2018 10:45:06 +0000 (12:45 +0200)] 
tests: shell: non-base chain loops

Detect more non-base chain loops.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: more chain dependency validation
Pablo Neira Ayuso [Wed, 23 May 2018 10:08:02 +0000 (12:08 +0200)] 
tests: shell: more chain dependency validation

More exercising for the chain dependency validation.

Reported-by: Taehee Yoo <ap420073@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonft.8: Document limitation of reject statement in bridge family
Phil Sutter [Tue, 15 May 2018 15:34:30 +0000 (17:34 +0200)] 
nft.8: Document limitation of reject statement in bridge family

Bridge family allows reject statement in prerouting and input chains
only. Users can't know without looking at kernel code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: Make diff functions use print_* functions
Máté Eckl [Thu, 17 May 2018 07:37:02 +0000 (09:37 +0200)] 
tests: py: Make diff functions use print_* functions

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: Added paylad file description to README
Máté Eckl [Thu, 17 May 2018 07:37:01 +0000 (09:37 +0200)] 
tests: py: Added paylad file description to README

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: print path of the logfile
Máté Eckl [Thu, 17 May 2018 07:37:00 +0000 (09:37 +0200)] 
tests: py: print path of the logfile

It is good to know that a log is generated even without browsing the
nft-test.py source code.

Also print_info function is introduced.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: print_msg refactor
Máté Eckl [Thu, 17 May 2018 07:36:59 +0000 (09:36 +0200)] 
tests: py: print_msg refactor

The errstr attribute was hard-coded to "ERROR:"

errstr has been moved in the parameter list. As print_msg is only
used from the other print_* this is not an issue, and as there is a
print_error function, I don't think that strerr should default to
"ERROR:".

Also this kind of messages now get written to stderr. This can be
beneficial if someone wants to redirect output to a file.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: updated test file structure descripion in README
Máté Eckl [Thu, 17 May 2018 07:36:58 +0000 (09:36 +0200)] 
tests: py: updated test file structure descripion in README

The order of the table and chain definitions have changed in test files.
Now the name of the chain has to be specified in the definition of the
table, so their order is reverted.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: small typo fixes in the python tests README
Máté Eckl [Thu, 17 May 2018 07:36:57 +0000 (09:36 +0200)] 
tests: py: small typo fixes in the python tests README

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: specify python version in nft-test.py
Máté Eckl [Thu, 17 May 2018 07:36:55 +0000 (09:36 +0200)] 
tests: py: specify python version in nft-test.py

/usr/bin/python is linked to different main version of python in
different distributions (eg. 2 on debian, 3 on arch linux).

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosegtree: missing comments in range and prefix expressions in sets
Pablo Neira Ayuso [Wed, 16 May 2018 19:59:12 +0000 (21:59 +0200)] 
segtree: missing comments in range and prefix expressions in sets

 table inet filter {
        set spamhaus {
                type ipv4_addr
                flags interval
                elements = { 1.2.3.8/31 comment "evil people", 3.3.3.16-3.3.3.20 comment "more than evil" }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Move scanner object into struct nft_ctx
Phil Sutter [Tue, 15 May 2018 09:37:56 +0000 (11:37 +0200)] 
libnftables: Move scanner object into struct nft_ctx

The initial approach of keeping as much of lex/yacc-specific data
local to the relevant parsing routines was flawed in that input
descriptors which parsed commands' location information points at were
freed after parsing (in scanner_destroy()) although they were required
later for error reporting in case a command was rejected by the kernel.

To overcome this, keep the scanner pointer in struct nft_ctx so that it
can be kept in place until kernel communication has finished.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonftables: xt: don't use hard-coded AF_INET
Florian Westphal [Fri, 11 May 2018 21:17:16 +0000 (23:17 +0200)] 
nftables: xt: don't use hard-coded AF_INET

We need to check which revision type is requested (match, target)
and wheter its ipv4 or ipv6, then set family based on that.

This allows nft ipv6 family to display compat entries if a translation
is available.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonft.8: Drop misleading adjective 'absolute'
Phil Sutter [Fri, 11 May 2018 12:20:35 +0000 (14:20 +0200)] 
nft.8: Drop misleading adjective 'absolute'

Discussion showed that rule index may be interpreted as being absolute
or relative, so just drop this adjective without replacement.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: Return ENOENT if rule index is too large
Phil Sutter [Fri, 11 May 2018 10:33:46 +0000 (12:33 +0200)] 
evaluate: Return ENOENT if rule index is too large

Since EINVAL usually indicates errors from kernel, avoid using it here
too. Instead return ENOENT to indicate there's no entry to append or
prepend the rule to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Support testing JSON input and output as well
Phil Sutter [Tue, 8 May 2018 11:08:45 +0000 (13:08 +0200)] 
tests/py: Support testing JSON input and output as well

This extends nft-test.py by optional JSON testing capabilities,
activated via '-j'/'--enable-json' parameter).

JSON testing happens for all rules which are supposed to work: After a
rule has been added and the existing tests (payload, ruleset listing
output) have been performed, basically the same test is done again using
a recorded JSON equivalent and (if necessary) a recorded listing output.

The code tries to ease new test case creation overhead by
auto-generating JSON equivalent input via listing the (non-JSON) rule in
JSON format. Also, differing netlink debug and listing output are stored
in *.got files to assist in analyzing/fixing failing test cases.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Highlight offending parts in differences warnings
Phil Sutter [Tue, 8 May 2018 11:08:44 +0000 (13:08 +0200)] 
tests/py: Highlight offending parts in differences warnings

Print the non-equal parts of the two rules in yellow when printing the
differences warning.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Don't read expected payload for each table
Phil Sutter [Tue, 8 May 2018 11:08:43 +0000 (13:08 +0200)] 
tests/py: Don't read expected payload for each table

When testing rule adding to different table families, expected payload
was read for each tested family again. Instead, read it just once and
just try to read a family-specific payload for each tested family.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Reduce indenting level in nft-test.py
Phil Sutter [Tue, 8 May 2018 11:08:41 +0000 (13:08 +0200)] 
tests/py: Reduce indenting level in nft-test.py

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agopy: Add JSON support to nftables Class
Phil Sutter [Tue, 8 May 2018 11:08:40 +0000 (13:08 +0200)] 
py: Add JSON support to nftables Class

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agopy: Add getter/setter for echo output option
Phil Sutter [Tue, 8 May 2018 11:08:39 +0000 (13:08 +0200)] 
py: Add getter/setter for echo output option

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Implement JSON parser
Phil Sutter [Tue, 8 May 2018 11:08:38 +0000 (13:08 +0200)] 
libnftables: Implement JSON parser

If JSON output setting is active in current context, try parsing any
input as JSON. If the initial loading of the buffer or filename by
libjansson fails, fall back to regular syntax parser.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Implement JSON output support
Phil Sutter [Tue, 8 May 2018 11:08:37 +0000 (13:08 +0200)] 
libnftables: Implement JSON output support

Although technically there already is support for JSON output via 'nft
export json' command, it is hardly useable since it exports all the gory
details of nftables VM. Also, libnftables has no control over what is
exported since the content comes directly from libnftnl.

Instead, implement JSON format support for regular 'nft list' commands.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Introduce a few helper functions
Phil Sutter [Tue, 8 May 2018 11:08:36 +0000 (13:08 +0200)] 
libnftables: Introduce a few helper functions

This adds a bunch of functions for conversion of different values into
string (and vice-versa).

* log_level_parse(): A simple helper to turn log level string
                     representation into log level value.
* nat_etype2str(): Translate nat statement type into string
                   representation.
* ct_dir2str(): Convert IP_CT_DIR_* values into string representation.
* ct_label2str(): Convert ct_label values into string representation.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Make some functions globally accessible
Phil Sutter [Tue, 8 May 2018 11:08:35 +0000 (13:08 +0200)] 
libnftables: Make some functions globally accessible

This removes static flag and adds header prototype for the following
functions:

* must_print_eq_op() from src/expression.c
* fib_result_str() from src/fib.c
* set_policy2str() and chain_policy2str from src/rule.c

In fib.h, include linux/netfilter/nf_tables.h to make sure enum
nft_fib_result is known when including this file.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Make some arrays globally accessible
Phil Sutter [Tue, 8 May 2018 11:08:34 +0000 (13:08 +0200)] 
libnftables: Make some arrays globally accessible

This removes static flag and adds declarations in headers for the
following arrays:

* ct_templates from src/ct.c
* mark_tbl from src/datatype.c
* meta_templates and devgroup_tbl from src/meta.c
* table_flags_name from src/rule.c
* set_stmt_op_names from src/statement.c
* tcpopthdr_protocols from src/tcpopt.c

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Put bison parsing into dedicated functions
Phil Sutter [Tue, 8 May 2018 11:08:33 +0000 (13:08 +0200)] 
libnftables: Put bison parsing into dedicated functions

Preparing for an alternative JSON parser, put bison specific details
into separate functions.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoinclude/linux: Add required NFT_CT_MAX macro
Phil Sutter [Tue, 8 May 2018 11:08:32 +0000 (13:08 +0200)] 
include/linux: Add required NFT_CT_MAX macro

This should be dropped for a real UAPI header update.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agobuild: Bump version to v0.8.5 v0.8.5
Florian Westphal [Thu, 10 May 2018 11:59:52 +0000 (13:59 +0200)] 
build: Bump version to v0.8.5

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests/shell: Extend rule_management/0001addposition_0
Phil Sutter [Wed, 9 May 2018 14:03:43 +0000 (16:03 +0200)] 
tests/shell: Extend rule_management/0001addposition_0

Combine it with 0002insertposition_0 due to the many similarities,
extend it to test 'handle' and 'index' parameters as well and rename the
testcase accordingly.

Also add a new 0002addinsertlocation_1 which tests that wrong argument
to all of the location parameters fails.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoSupport 'add/insert rule index <IDX>'
Phil Sutter [Wed, 9 May 2018 14:03:42 +0000 (16:03 +0200)] 
Support 'add/insert rule index <IDX>'

Allow to specify an absolute rule position in add/insert commands like
with iptables. The translation to rule handle takes place in userspace,
so no kernel support for this is needed. Possible undesired effects are
pointed out in man page to make users aware that this way of specifying
a rule location might not be ideal.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: Copy locations in handle_merge()
Phil Sutter [Wed, 9 May 2018 14:03:41 +0000 (16:03 +0200)] 
rule: Copy locations in handle_merge()

This allows to make error messages point to the right part of the
command after handles were merged.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoDeprecate add/insert rule 'position' argument
Phil Sutter [Wed, 9 May 2018 14:03:40 +0000 (16:03 +0200)] 
Deprecate add/insert rule 'position' argument

Instead, use 'handle' keyword for the same effect since that is more
consistent with respect to replace/delete commands. The old keyword is
still supported for backwards compatibility and also listed in man page
along with a hint that it shouldn't be used anymore.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: py: allow to specify sets with a timeout
Florian Westphal [Wed, 9 May 2018 10:18:20 +0000 (12:18 +0200)] 
tests: py: allow to specify sets with a timeout

Not usable yet, as the set timeout netlink output isn't captured so far,
but it adds groundwork to add this as a follow-up.

Set definition syntax changes a little, if you want to
add multiple elements they now have to be separated by "," just
like in nftables.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: py: add expires tests with different time bases
Florian Westphal [Wed, 9 May 2018 09:59:39 +0000 (11:59 +0200)] 
tests: py: add expires tests with different time bases

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: support timeouts in milliseconds
Florian Westphal [Tue, 8 May 2018 22:52:30 +0000 (00:52 +0200)] 
src: support timeouts in milliseconds

currently the frontend uses seconds everywhere and
multiplies/divides by 1000.

Pass milliseconds around instead and extend the scanner to accept 'ms'
in timestrings.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: add size keyword to meter example
Florian Westphal [Tue, 8 May 2018 22:23:25 +0000 (00:23 +0200)] 
doc: add size keyword to meter example

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoxt: don't BUG if we can't find an extensions
Florian Westphal [Tue, 8 May 2018 14:22:21 +0000 (16:22 +0200)] 
xt: don't BUG if we can't find an extensions

it seems a bit harsh to just exit.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser: added missing semicolon
Máté Eckl [Mon, 7 May 2018 20:57:22 +0000 (22:57 +0200)] 
parser: added missing semicolon

It did not make any harm, but it was certainly missing.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: shell: add size to meters
Pablo Neira Ayuso [Tue, 8 May 2018 11:50:10 +0000 (13:50 +0200)] 
tests: shell: add size to meters

Otherwise, 65535 is used and testsuite reports dump mismatch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: delete chain and rule with jump to chain in same transaction
Pablo Neira Ayuso [Tue, 8 May 2018 11:23:25 +0000 (13:23 +0200)] 
tests: shell: delete chain and rule with jump to chain in same transaction

We should not hit EBUSY in this case.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agofiles: restore base table skeletons
Florian Westphal [Sun, 6 May 2018 19:44:56 +0000 (21:44 +0200)] 
files: restore base table skeletons

nftables releases until 0.8.2 included base skeleton hooks
that were installed into /etc/nftables (sysconfdir).

With 0.8.3 and newer these files were moved to the documentation
area but apparently some users expect them to be there.

Resurrect them.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoscanner: Support rfc4291 IPv4-compatible addresses
Phil Sutter [Sat, 5 May 2018 12:44:53 +0000 (14:44 +0200)] 
scanner: Support rfc4291 IPv4-compatible addresses

These are defined in section 2.5.5.1. Although it is stated that they
are deprecated and new implementations are not required to support them,
they occur in ruleset output if an address in the form '::feed:babe' was
given in input. In order to support reinsertion of that rule, we have to
support those deprecated addresses as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Fix expected output in {bridge,inet}/icmpX.t
Phil Sutter [Sat, 5 May 2018 12:44:37 +0000 (14:44 +0200)] 
tests/py: Fix expected output in {bridge,inet}/icmpX.t

The first expression in that rule is not eliminated in evaluation phase,
so there is no reason why it should be while delinearizing.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoproto: Fix wrong token in proto_icmp6
Phil Sutter [Sat, 5 May 2018 12:44:26 +0000 (14:44 +0200)] 
proto: Fix wrong token in proto_icmp6

'token' value of ICMP6HDR_MTU field must be 'mtu', not 'packet-too-big'.
This went unnoticed because rule delinearization for icmp/icmpv6 payload
expressions is problematic anyway in that different fields point to the
same offset and therefore are indistinguishable. In this case, an
expression like e.g. 'icmpv6 mtu 1500' will be printed later as 'icmpv6
parameter-problem 1500'.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: use location to display error messages
Pablo Neira Ayuso [Thu, 3 May 2018 10:46:08 +0000 (12:46 +0200)] 
src: use location to display error messages

 # nft add chain foo bar
 Error: Could not process rule: No such file or directory
 add chain foo bar
           ^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add obj_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:49:39 +0000 (12:49 +0200)] 
src: add obj_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add set_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:31:48 +0000 (12:31 +0200)] 
src: add set_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add chain_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:11:32 +0000 (12:11 +0200)] 
src: add chain_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add table_spec
Pablo Neira Ayuso [Thu, 3 May 2018 10:06:27 +0000 (12:06 +0200)] 
src: add table_spec

Store location object in handle to improve error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser: Removed LOOKUP token
Máté Eckl [Fri, 4 May 2018 21:50:47 +0000 (23:50 +0200)] 
parser: Removed LOOKUP token

It is never used.

Signed-off-by: Máté Eckl <ecklm94@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>