]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
7 years agoscanner: add helpers token
Florian Westphal [Fri, 13 Apr 2018 09:40:50 +0000 (11:40 +0200)] 
scanner: add helpers token

without it, you get:
 nft list ct helpers table filter
 Error: syntax error, unexpected string, expecting helper or helpers

Fixes: 14fd3ad720f6e ("src: prepare for future ct timeout policy support")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser_bison: Pass struct nft_ctx to parser_init()
Phil Sutter [Fri, 13 Apr 2018 14:52:35 +0000 (16:52 +0200)] 
parser_bison: Pass struct nft_ctx to parser_init()

Signature of parser_init() got quite huge, so simply pass the whole
context pointer to it - most of the parameters are just taken from there
anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Keep cmds list outside of parser_state
Phil Sutter [Fri, 13 Apr 2018 14:52:34 +0000 (16:52 +0200)] 
libnftables: Keep cmds list outside of parser_state

Parser basically turns input into a list of commands and error messages.
Having the commands list being part of struct parser_state does not make
sense from this point of view, also it will have to go away with
upcoming JSON support anyway.

While being at it, change nft_netlink() to take just the list of
commands instead of the whole parser state as parameter, also take care
of command freeing in nft_run_cmd_from_* functions (where the list
resides as auto-variable) instead of from inside nft_run().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoct: Remove duplicate include of gmputil.h
Phil Sutter [Fri, 13 Apr 2018 14:52:33 +0000 (16:52 +0200)] 
ct: Remove duplicate include of gmputil.h

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoReview .gitignore files contents
Phil Sutter [Fri, 13 Apr 2018 14:52:32 +0000 (16:52 +0200)] 
Review .gitignore files contents

* Move entries belonging to src/ into src/.gitignore.
* Drop lines for files inside build-aux/ since that is ignored already.
* No need to ignore src/Makefile.in, the entry 'Makefile.in' catches
  that already.
* Remove entry for '.*.d', (recent?) autotools doesn't create any
  matching files.
* Drop entries for non-existent parser.c and parser.h files.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Fix differences printing for rules with sets
Phil Sutter [Fri, 13 Apr 2018 14:52:31 +0000 (16:52 +0200)] 
tests/py: Fix differences printing for rules with sets

If a rule contained a set, it would always print the tested rule as
differing from the output instead of any given output (if defined).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: rt.t: Drop needless rule output
Phil Sutter [Fri, 13 Apr 2018 14:52:30 +0000 (16:52 +0200)] 
tests/py: rt.t: Drop needless rule output

It is identical to the tested rule, so no point in keeping it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: Fix for last elem at interval end
Phil Sutter [Fri, 13 Apr 2018 14:52:29 +0000 (16:52 +0200)] 
segtree: Fix for last elem at interval end

Unclosed interval check at end of interval_map_decompose() missed to
check whether interval start is the last possible element in given set
before creating a range expression. This led to the last element
incorrectly printed as range from itself to itself. Fix this by
comparing the upper boundary against the lower one.

In order to keep indenting level low, invert the entry check and jump to
the end if it matches.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoReview raw payload allocation points
Phil Sutter [Fri, 13 Apr 2018 14:52:28 +0000 (16:52 +0200)] 
Review raw payload allocation points

In parser_bison.y, call payload_init_raw() instead of assigning all
fields manually. Also drop manual initialization of flags field: it is
not touched in allocation path, so no need for that.

In stmt_evaluate_payload(), setting dtype field is redundant since
payload_init_raw() does that already.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosegtree: Fix memory leaks
Phil Sutter [Fri, 13 Apr 2018 14:52:27 +0000 (16:52 +0200)] 
segtree: Fix memory leaks

This fixes memory leaks in three places:

* set_overlap():

The allocated intervals have to be freed again before returning to
caller. While being at it, reduce indenting level in said function to
stay below 80 columns boundary.

* range_is_prefix():
* interval_map_decompose():

GMP documentation suggests to call mpz_clear() for all mpz_t type
variables once they are not used anymore to free the space they occupy.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: Free table->objs in table_free()
Phil Sutter [Fri, 13 Apr 2018 14:52:26 +0000 (16:52 +0200)] 
rule: Free table->objs in table_free()

This fixes a memory leak identified by valgrind.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Fix forgotten bit after introducing error_fp
Phil Sutter [Fri, 13 Apr 2018 14:52:25 +0000 (16:52 +0200)] 
libnftables: Fix forgotten bit after introducing error_fp

Like nft_run_cmd_from_buffer, nft_run_cmd_from_filename doesn't need to
change output_fp temporarily when printing error records anymore.

Fixes: 4176e24e14f07 ("libnftables: Introduce nft_ctx_set_error()")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoerec: Review erec_print()
Phil Sutter [Fri, 13 Apr 2018 14:52:24 +0000 (16:52 +0200)] 
erec: Review erec_print()

A new requirement to erec for the upcoming JSON support is printing
records with file input descriptors without open stream. The approach is
to treat 'name' field as file name, open it, extract the offending line
and close it again.

Further changes to libnftables input parsing routines though have shown
that the whole concept of file pointer reuse in erec is tedious and not
worth keeping:

* Closed files are to be supported as well, so there needs to be
  fallback code for opening the file anyway.

* When input descriptor is duplicated from parser state into an error
  record, the file pointer is copied as well. Therefore care has to be
  taken to not free the parser state before any error records have been
  printed. This is the only point where old and duplicated input
  descriptors are connected.

Therefore drop struct input_descriptor's 'fp' field and just always open
the file by name. This way also the old stream offset doesn't have to be
restored after reading.

While being at it, this patch fixes two other (potential) problems:

* If the offending line from input contains tabs, add them at the right
  position in the marker buffer as well to avoid misalignment.

* The input file may not be seekable (/dev/stdin for instance), so skip
  printing of offending line and markers if it couldn't be read
  properly.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agocli: Drop String termination workaround
Phil Sutter [Wed, 11 Apr 2018 08:21:35 +0000 (10:21 +0200)] 
cli: Drop String termination workaround

This spot was missed by commit 2b3f18e0cf7a7 ("libnftables: Fix for
input without trailing newline") - since line termination is now added
in nft_run_cmd_from_buffer(), cli is relieved from doing so.

Fixes: 2b3f18e0cf7a7 ("libnftables: Fix for input without trailing newline")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: py: remove object ID from payload file
Pablo Neira Ayuso [Wed, 11 Apr 2018 07:56:25 +0000 (09:56 +0200)] 
tests: py: remove object ID from payload file

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Allow passing multiple files to nft-test.py
Phil Sutter [Tue, 10 Apr 2018 17:00:26 +0000 (19:00 +0200)] 
tests/py: Allow passing multiple files to nft-test.py

This allows to run the testsuite over a subset of test cases, e.g. like
so:

| ./nft-test.py bridge/*.t

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Review print statements in nft-test.py
Phil Sutter [Tue, 10 Apr 2018 17:00:25 +0000 (19:00 +0200)] 
tests/py: Review print statements in nft-test.py

Make use of format strings as they are easier to read than manual string
concatenation.

Also use class Table's __str__ method instead of printing the 'name'
attribute. This changes the output in that table names are prepended by
their family, but the extra information may come in handy when analyzing
issues.

Since class Chain's __str__ method returns just the 'name' attribute
content, it may be used as synonym.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/py: Use libnftables instead of calling nft binary
Phil Sutter [Tue, 10 Apr 2018 17:00:24 +0000 (19:00 +0200)] 
tests/py: Use libnftables instead of calling nft binary

This adds a simple nftables Python class in py/nftables.py which gives
access to libnftables API via ctypes module.

nft-test.py is extended to make use of the above class instead of
calling nft binary. Since command line formatting had to be touched
anyway, this patch also streamlines things a bit by introducing
__str__ methods to classes Table and Chain and making extensive use of
format strings instead of onerously adding all string parts together.

Since the called commands don't see a shell anymore, all shell meta
character escaping done in testcases is removed.

The visible effects of this change are:

* Four new warnings in ip/flowtable.t due to changing objref IDs (will
  be addressed later in a patch to libnftnl).

* Reported command line in warning and error messages changed slightly
  for obvious reasons.

* Reduction of a full test run's runtime by a factor of four. Status
  diff after running with 'time':

  < 83 test files, 77 files passed, 1724 unit tests, 0 error, 33 warning
  < 87.23user 696.13system 15:11.82elapsed 85%CPU (0avgtext+0avgdata 9604maxresident)k
  < 8inputs+36800outputs (0major+35171235minor)pagefaults 0swaps

  > 83 test files, 77 files passed, 1724 unit tests, 4 error, 33 warning
  > 6.80user 30.18system 3:45.86elapsed 16%CPU (0avgtext+0avgdata 14064maxresident)k
  > 0inputs+35808outputs (0major+2874minor)pagefaults 0swaps

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Simplify cookie integration
Phil Sutter [Tue, 10 Apr 2018 17:00:23 +0000 (19:00 +0200)] 
libnftables: Simplify cookie integration

This increases the size of struct output_ctx quite a bit, but allows to
simplify internal functions dealing with the cookies mainly because
output_fp becomes accessible from struct cookie.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Support buffering output and error
Phil Sutter [Tue, 10 Apr 2018 17:00:22 +0000 (19:00 +0200)] 
libnftables: Support buffering output and error

When integrating libnftables into Python code using ctypes module,
having to use a FILE pointer for output becomes a show-stopper.
Therefore make Python hackers' lives (a little) less painful by
providing convenience functions to setup buffering output and error
streams using fopencookie() and retrieving the buffers.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Introduce nft_ctx_set_error()
Phil Sutter [Tue, 10 Apr 2018 17:00:21 +0000 (19:00 +0200)] 
libnftables: Introduce nft_ctx_set_error()

Analogous to nft_ctx_set_output(), this allows to set a custom file
pointer for writing error messages to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agolibnftables: Fix for input without trailing newline
Phil Sutter [Tue, 10 Apr 2018 17:00:20 +0000 (19:00 +0200)] 
libnftables: Fix for input without trailing newline

Input parser implementation requires a newline at end of input,
otherwise the last pattern may not be recognized correctly.

If input comes from a file, the culprit was YY_INPUT macro not expecting
the last line not ending with a newline, so the last word wasn't
accepted. This is easily fixed by checking for feof(yyin) in there. A
simple test case for that is:

| echo -en "table ip t {\nchain c {\n}\n}" >/tmp/foo
| nft -f /tmp/foo

Input from a string buffer is a bit more tricky: The culprit here is
that detection of classid pattern is done by checking the character
following it which makes it impossible to sit right at end of input and
I haven't found an alternative to that. After dropping the manual
newline appending when combining argv into a single buffer in main(),
a rule like this won't be recognized anymore:

| nft add rule ip t c meta priority feed:babe

Since a direct call to run_cmd_from_buffer() via libnftables bypasses
the sanitizing done in main() entirely, it has to happen in libnftables
instead which means creating a newline-terminated duplicate of the input
buffer.

Note that main() created a buffer one byte longer than needed since it
accounts for whitespace at end of each argv but doesn't add it to the
buffer for the last one, so buffer length is reduced by two bytes
instead of just one although only one less character is printed into it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoct: Fix output_fp bypass in ct_print()
Phil Sutter [Tue, 10 Apr 2018 17:00:19 +0000 (19:00 +0200)] 
ct: Fix output_fp bypass in ct_print()

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure: don't enable xtables when --without-xtables is passed
Florian Westphal [Wed, 4 Apr 2018 11:50:08 +0000 (13:50 +0200)] 
configure: don't enable xtables when --without-xtables is passed

AC_ARG_WITH runs this when EITHER --with-foo or --without-foo is given,
so use 'withval'.

After this patch:
./configure -> xtables off
./configure --with-xtables -> xtables on
./configure --without-xtables -> xtables off (was on).

Reported-by: Alexander Dahl <ada@thorsis.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: use recursive call for SET_REF handling
Florian Westphal [Mon, 2 Apr 2018 19:26:07 +0000 (21:26 +0200)] 
evaluate: use recursive call for SET_REF handling

We can now call the helper again, with set->init as new RHS expression.

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