]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
7 years agodoc: update doc/ispell_nft to track recent nft.8 updates
Duncan Roe [Wed, 2 May 2018 23:02:20 +0000 (09:02 +1000)] 
doc: update doc/ispell_nft to track recent nft.8 updates

Track changes in commits 3baa28f24b3d70a7ee17d584c113a2c4e057a565 and
4787edad132c30ae0f6bb00135ae5d970b0ccb74 (rename ibriport and obriport:
s/iport/name).

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agometer: enforce presence of a max size
Florian Westphal [Wed, 2 May 2018 12:50:12 +0000 (14:50 +0200)] 
meter: enforce presence of a max size

meters are updated dynamically, so we don't know in advance
how large this structure can be.

Add a 'size' keyword to specifiy an upper limit and update
the old syntax to assume a default max value of 65535.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agolibnftables: fix header export
Arturo Borrero Gonzalez [Tue, 1 May 2018 17:33:45 +0000 (19:33 +0200)] 
libnftables: fix header export

Instruct Make to actually install the header to the system, otherwise
users won't see the header in their system after running 'make install'.

Also, export main libnftables header with a proper name, since we have another
private header called 'nftables.h' (i.e, let's be concrete with the naming).

Signed-off-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agobuild: Bump version to v0.8.4 v0.8.4
Florian Westphal [Tue, 1 May 2018 09:14:58 +0000 (11:14 +0200)] 
build: Bump version to v0.8.4

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agorule: do not hardcode ingress when printing flowtable
Pablo Neira Ayuso [Wed, 25 Apr 2018 16:50:26 +0000 (18:50 +0200)] 
rule: do not hardcode ingress when printing flowtable

Call hook number to string function instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: missing flowtable evaluation from nested notation
Pablo Neira Ayuso [Wed, 25 Apr 2018 16:51:08 +0000 (18:51 +0200)] 
evaluate: missing flowtable evaluation from nested notation

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: fix --debug mnl not producing output
Duncan Roe [Wed, 25 Apr 2018 23:23:49 +0000 (09:23 +1000)] 
src: fix --debug mnl not producing output

cache_update() needs to accept the full debug mask instead of a boolean of
NFT_DEBUG_NETLINK, because called functions may wish to check other bits
(NFT_DEBUG_MNL in particular).

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: reword insert position, this expects rule handle to insert, not a relative postition
Florian Westphal [Tue, 24 Apr 2018 14:54:52 +0000 (16:54 +0200)] 
doc: reword insert position, this expects rule handle to insert, not a relative postition

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agostatement: Fix get_rate() for zero byte_rate
Phil Sutter [Tue, 24 Apr 2018 09:46:01 +0000 (11:46 +0200)] 
statement: Fix get_rate() for zero byte_rate

The algorithm didn't detect whether given byte_rate was zero,
pointlessly iterating through data units. Make it exit early in this
case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: Free flowtable in handle_free()
Phil Sutter [Tue, 24 Apr 2018 09:44:19 +0000 (11:44 +0200)] 
rule: Free flowtable in handle_free()

Fixes: db0697ce7f602 ("src: support for flowtable listing")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: no EINTR handling from netlink_get_setelem()
Pablo Neira Ayuso [Tue, 24 Apr 2018 09:53:54 +0000 (11:53 +0200)] 
netlink: no EINTR handling from netlink_get_setelem()

This cannot happen, this call does not set the NLM_F_DUMP flag.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: centralize netlink error reporting
Pablo Neira Ayuso [Tue, 24 Apr 2018 09:33:34 +0000 (11:33 +0200)] 
src: centralize netlink error reporting

Consolidate error reporting from do_command() call.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: simplify netlink_get_setelems() and rename it to netlink_list_setelems()
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:21:32 +0000 (01:21 +0200)] 
src: simplify netlink_get_setelems() and rename it to netlink_list_setelems()

This is called from cache population path, remove netlink_io_error()
call since this is not needed. Rename it for consistency with similar
netlink_list_*() NLM_F_DUMP functions. Get rid of location parameter.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: remove unused function declarations
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:13 +0000 (01:05 +0200)] 
netlink: remove unused function declarations

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: don't pass location to netlink_list_*() function
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:12 +0000 (01:05 +0200)] 
netlink: don't pass location to netlink_list_*() function

Not needed anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: netlink_list_chains() callers always wants all existing chains
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:11 +0000 (01:05 +0200)] 
netlink: netlink_list_chains() callers always wants all existing chains

Remove dead code, callers always need this to dump all of the existing
chains.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: pass cmd object to netlink function calls
Pablo Neira Ayuso [Mon, 23 Apr 2018 23:05:10 +0000 (01:05 +0200)] 
netlink: pass cmd object to netlink function calls

Simplify function footprint.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: remove dead netlink_io_error() calls
Pablo Neira Ayuso [Fri, 20 Apr 2018 10:30:01 +0000 (12:30 +0200)] 
netlink: remove dead netlink_io_error() calls

This error path is never entered since mnl_nft_*_batch_{add,del,replace}
calls never fail, and if they ever do fail it will be because we are
hitting OOM, in such case we can display a more generic non-netlink
error.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agomnl: remove non-batch netlink code
Pablo Neira Ayuso [Fri, 20 Apr 2018 10:06:26 +0000 (12:06 +0200)] 
mnl: remove non-batch netlink code

This functions have no clients anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: clear expression context before cmd evaluation
Florian Westphal [Thu, 19 Apr 2018 16:18:57 +0000 (18:18 +0200)] 
evaluate: clear expression context before cmd evaluation

We also need to clear expr ctx before we eval a command.
This is a followup fix to 'evaluate: reset eval context when evaluating
set definitions'.

The first patch only fixed set evaluation when dealing with
a complete table representation rather than individual commands.

Reported-by: David Fabian <david.fabian@bosson.cz>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: use ibrname and obrname
Pablo Neira Ayuso [Thu, 19 Apr 2018 12:32:25 +0000 (14:32 +0200)] 
src: use ibrname and obrname

Legacy tool name is 'brctl' and so the 'br' prefix is already known. If
we use ibrname and obrname it looks consistent with iifname and oifname.
So let's this instead of ibridgename and obridgename since Florian likes
this too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: missing rules for IBRIDGENAME and OBRIDGENAME
Pablo Neira Ayuso [Thu, 19 Apr 2018 11:25:38 +0000 (13:25 +0200)] 
parser_bison: missing rules for IBRIDGENAME and OBRIDGENAME

Fixes: 3baa28f24b3d ("src: rename ibrportname, obrportname")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoexpression: fix constant expression allocation on big endian with partial bytes
Sabrina Dubroca [Thu, 19 Apr 2018 08:45:23 +0000 (10:45 +0200)] 
expression: fix constant expression allocation on big endian with partial bytes

Commit 5259feeb7cda ("expression: fix constant expression allocation on
big endian") improved constant handling on big endian, but didn't handle
the case of partial bytes correctly.

Currently, constant_data_ptr(val, 6) points to the item after val,
instead of the last byte of val.

Thanks to Stefano for providing the correct expression.

Fixes: 5259feeb7cda ("expression: fix constant expression allocation on big endian")
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: reset eval context when evaluating set definitions
Florian Westphal [Wed, 18 Apr 2018 12:07:09 +0000 (14:07 +0200)] 
evaluate: reset eval context when evaluating set definitions

David reported nft chokes on this:
nft -f /tmp/A
/tmp/A:9:22-45: Error: datatype mismatch, expected concatenation of (IPv4 address, internet network service, IPv4 address), expression has type concatenation of (IPv4 address, internet network service)
cat /tmp/A
flush ruleset;
table ip filter {
set setA {
type ipv4_addr . inet_service . ipv4_addr
flags timeout
}
set setB {
type ipv4_addr . inet_service
flags timeout
}
}

Problem is we leak set definition details of setA to setB via eval
context, so reset this.

Also add test case for this.

Reported-by: David Fabian <david.fabian@bosson.cz>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: rename ibrportname, obrportname
Florian Westphal [Fri, 13 Apr 2018 12:36:32 +0000 (14:36 +0200)] 
src: rename ibrportname, obrportname

For bridge, iifname is the port name, whereas 'ibrport' is the
logical name of the bridge ("br0") the port ("iifname") is enslaved to.

So, 'ibrport' is a misnomer.

libnftl calls these 'bri_iifname' and 'bri_oifname', which is good
but using 'briiifname' in nft is rather ugly, so use 'ibridgename'
and 'obridgename' instead.

Old names are still recognized, listing shows the new names.

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