]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
8 years agoparser: compact list of rhs keyword expressions
Florian Westphal [Wed, 27 Jul 2016 13:16:32 +0000 (15:16 +0200)] 
parser: compact list of rhs keyword expressions

Condenses the copy/pastry via a define to avoid repeating this
when the list gets extended.

Based on earlier patch from Pablo.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoexhtdr: pacify compiler warning
Florian Westphal [Thu, 13 Jul 2017 14:02:31 +0000 (16:02 +0200)] 
exhtdr: pacify compiler warning

gcc 7.1.1 complains:

exthdr.c:41:31: warning: ā€˜%d’ directive output may be truncated writing between 1 and 8 bytes into a region of size 3 [-Wformat-truncation=]
    snprintf(buf, sizeof buf, "%d", offset);
                               ^~
This warning is incorrect, as offset is limited by tcp option ranges,
but gcc doesn't know this. Increase buffer to avoid the warning.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotcpopt: make tcptopt structs static
Florian Westphal [Thu, 13 Jul 2017 13:50:30 +0000 (15:50 +0200)] 
tcpopt: make tcptopt structs static

not used outside of tcpopt.c, so unexport from header file and
make them static.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoinclude: Remove datatype_register().
Varsha Rao [Fri, 30 Jun 2017 09:26:19 +0000 (14:56 +0530)] 
include: Remove datatype_register().

Remove datatype_register() function and its calling __init functions.
Add arguments of datatype_register() function to datatype array.

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoman: add include statement documentation.
Ismo Puustinen [Wed, 28 Jun 2017 08:16:57 +0000 (11:16 +0300)] 
man: add include statement documentation.

Add to man page information about using the wildcard include statements.

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: update include directory tests to support wildcard syntax.
Ismo Puustinen [Tue, 27 Jun 2017 12:14:59 +0000 (15:14 +0300)] 
tests: update include directory tests to support wildcard syntax.

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: support for wildcards in include statements.
Ismo Puustinen [Tue, 27 Jun 2017 12:14:58 +0000 (15:14 +0300)] 
scanner: support for wildcards in include statements.

Use glob() to find paths in include statements. The rules are these:

  1. If no files can be found in the pattern with wildcards, do not
     return an error.
  2. Do not match any files beginning with '.'.
  3. Do not handle include directories anymore. For example, the
     statement:
       include "foo/"
     would now need to be rewritten:
       include "foo/*"

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: Fail test forcefully when bug is not fixed
Shyam Saini [Wed, 21 Jun 2017 08:47:45 +0000 (14:17 +0530)] 
tests: py: Fail test forcefully when bug is not fixed

When we have "fail" in the test cases then py test doesn't complain
anything, but the test should complain if the fix is not applied.

Before applying 986dea8a4a9d ("evaluate: avoid reference to multiple src
data in statements which set values"), nft throws following error
message and exits with error code 134.

$ nft add rule x y tcp dport set { 0 , 1 }
  BUG: unknown expression type set reference
  nft: netlink_linearize.c:696: netlink_gen_expr: Assertion `0' failed.
  Aborted

This commit enforces nft-test.py to throw error message when the fix
is not applied.

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add --check option flag
Pablo M. Bermudo Garay [Fri, 23 Jun 2017 16:38:25 +0000 (18:38 +0200)] 
src: add --check option flag

Sometimes it can be useful to test if a command is valid without
applying any change to the rule-set. This commit adds a new option
flag (-c | --check) that performs a dry run execution of the commands.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add new generic context structure nft_ctx
Pablo M. Bermudo Garay [Fri, 23 Jun 2017 16:38:24 +0000 (18:38 +0200)] 
src: add new generic context structure nft_ctx

The new structure nft_ctx is meant to be used as a generic container of
context information.

This is a preparatory patch. So at the moment the struct just carry
output_ctx on his path through main.c and cli.c.

Based on original idea from Eric Leblond.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: Test input descriptors for included files
Shyam Saini [Fri, 23 Jun 2017 12:05:56 +0000 (17:35 +0530)] 
tests: shell: Test input descriptors for included files

Before b14572f72aac ("erec: Fix input descriptors for included files"),
nft error message was pointing to wrong file. But after this commit it
points to right file.

This patch adds a new test for this commit.

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: Add test for incomplete set add set command
Shyam Saini [Fri, 23 Jun 2017 12:05:55 +0000 (17:35 +0530)] 
tests: shell: Add test for incomplete set add set command

Before c6cd7c22548a ("src: fix crash when inputting an incomplete set
add command") commit, if we run nft with incomplete "add set" command it
caused segmentation fault and exit with error code 139 and further it
didn't throw any error message.

  For example:
    $ sudo nft add set t s

But after the aforementioned commit it throws syntax error message and
exits with return value 1.

  For example:
    $ sudo nft add set t s
    <cmdline>:1:12-12: Error: syntax error, unexpected newline, expecting '{'
    add set t s
               ^
This commit tests changes made in such commit.

Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Better error reporting for bad set references
Pablo Neira Ayuso [Mon, 19 Jun 2017 11:59:33 +0000 (13:59 +0200)] 
evaluate: Better error reporting for bad set references

In case you refer to an unexisting set, bail out with:

 # nft add table x
 # nft add chain x y
 # nft add rule x y ip protocol vmap @reject_to_rule2;
 <cmdline>:1:31-46: Error: Set 'reject_to_rule2' does not exist
 add rule x y ip protocol vmap @reject_to_rule2
                               ^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: merge nested set flags
Pablo Neira Ayuso [Sun, 18 Jun 2017 11:08:36 +0000 (13:08 +0200)] 
evaluate: merge nested set flags

A set may contain a nested set element definition, merge the nested set
flags so we don't hit:

 BUG: invalid data expression type range
 nft: netlink.c:400: netlink_gen_data: Assertion `0' failed.
 Aborted

With the following example ruleset:

 define dnat_ports      = { 1234-1567 }
 define port_allow      = {
        53,             # dns
        $dnat_ports,    # dnat
 }

 add rule x y tcp dport $port_allow accept

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1145
Fixes: a6b75b837f5e ("evaluate: set: Allow for set elems to be sets")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: reject meta nfproto outside of inet family
Florian Westphal [Fri, 16 Jun 2017 19:18:45 +0000 (21:18 +0200)] 
evaluate: reject meta nfproto outside of inet family

meta nfproto loads the hook family type of the current rule context
in the kernel, i.e. it will be NFPROTO_IPV6 for ip6 family,
NFPROTO_BRIDGE for bridge and so on.

The only case where this is useful is the inet pseudo family,
where this is useful to determine the real hook family
(NFPROTO_IPV4 or NFPROTO_IPV6).

In all other families 'meta nfproto' is either always true or false.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: restrict meta nfproto test cases to inet family
Florian Westphal [Fri, 16 Jun 2017 19:26:41 +0000 (21:26 +0200)] 
tests: restrict meta nfproto test cases to inet family

Followup patch will reject meta nfproto for non-inet families.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: remove two non-sensical rules
Florian Westphal [Fri, 16 Jun 2017 20:17:05 +0000 (22:17 +0200)] 
tests: remove two non-sensical rules

meta nfproto returns the hook family that the current packet
is being evaluted in, e.g.
NFPROTO_NETDEV in case we're called from the netdev context.

This makes no sense, if we add a rule to netdev, bridge, ip, ...
table then thats where it will be evaluated, no runtime test needed.

Only exception: inet family, in this case, nfproto will be either
ipv4 or ipv6.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: restrict ct saddr test to inet family
Florian Westphal [Fri, 16 Jun 2017 19:52:32 +0000 (21:52 +0200)] 
tests: restrict ct saddr test to inet family

any/ct.t: ERROR: line 94: src/nft add rule --debug=netlink ip6
test-ip6 output meta nfproto ipv4 ct original saddr 1.2.3.4: This rule should not have failed.

Actually, this failure is "ok; we can't find upper layer protocol
in this case, but even if we'd "fix" this it is still non-sensical,
meta nfproto ipv4, but family is ipv6 --> rule would never match.

First move this to an inet-specific test.
A followup patch will reject meta nfproto for all families except inet.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: Add test for ambiguity while setting the value
Shyam Saini [Fri, 16 Jun 2017 19:35:42 +0000 (01:05 +0530)] 
tests: py: Add test for ambiguity while setting the value

This test checks bug identified and fixed in the commit mentioned below
In a statement if there are  multiple src data then it  would be
totally ambiguous to decide which value to set.

Before the commit was made it returned 134(BUG), but now it returns 1
i.e, an error message.

Following rules tests ambiguity while setting the value:

 $ sudo nft add rule ip test-ip4 output ct mark set {0x11333, 0x11}
 <cmdline>:1:41-55: Error: you cannot use a set here, unknown value to use
 add rule ip test-ip4 output ct mark set {0x11333, 0x11}
                             ~~~~~~~~~~~~^^^^^^^^^^^^^^^

Test: 986dea8 ("evaluate: avoid reference to multiple src data in
statements which set values")
Signed-off-by: Shyam Saini <mayhs11saini@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Pass stateless, numeric, ip2name and handle variables as structure members.
Varsha Rao [Fri, 16 Jun 2017 09:24:06 +0000 (14:54 +0530)] 
src: Pass stateless, numeric, ip2name and handle variables as structure members.

libnftables library will be created soon. So declare numeric_output,
stateless_output, ip2name_output and handle_output as members of
structure output_ctx, instead of global variables. Rename these
variables as following,

numeric_output -> numeric
stateless_output -> stateless
ip2name_output -> ip2name
handle_output -> handle

Also add struct output_ctx *octx as member of struct netlink_ctx.

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: error reporting for nested ruleset representation
Pablo Neira Ayuso [Thu, 15 Jun 2017 12:35:33 +0000 (14:35 +0200)] 
src: error reporting for nested ruleset representation

If you load a file using the nested ruleset representation, ie. the one
you get via `nft list ruleset', error reporting doesn't help you much to
find the problem.

For example, the following ruleset points to an unexisting chain 'x':

 table test {
        chain test {
                type filter hook ingress priority 0; policy drop;
                ip saddr { 1.1.1.1, 2.2.2.2, 3.3.3.3, 4.4.4.4 } jump x
        }
 }

Error reporting is very sparse as it says:

 # nft -f /home/test/x
 /home/test/x:1:1-2: Error: Could not process rule: No such file or directory
 table netdev test{
 ^^

So it's hard to know what is exactly missing.

This patch enhances the existing logic, so nft points to the rule
causing the problem, ie.

 # nft -f /home/test/x
 /home/test/x:4:17-70: Error: Could not process rule: No such file or directory
                ip saddr { 1.1.1.1, 2.2.2.2, 3.3.3.3, 4.4.4.4 } jump x
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The idea behind this patch is to expand the single table command into a
list of individual commands, one per nested object inside the table.
This expanded list is spliced into the existing list of commands. Thus,
each command gets a sequence number that helps us correlate the error
with the command that triggers it.

This patch also includes reference counting for rules and objects. This
was already in place for table, chain and sets. We need this since now
we hold references to them from both the command and the table object
itself. So the last reference releases the object from memory. Note that
table objects still keep the list of chain, sets, etc. since the
existing cache logic needs this to work.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: rename ct eventmask to event
Florian Westphal [Wed, 7 Jun 2017 10:20:50 +0000 (12:20 +0200)] 
src: rename ct eventmask to event

ct status isn't named 'statusmask' either.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink_delinearize: prefer ct event set foo,bar over 'set foo|bar'
Florian Westphal [Wed, 7 Jun 2017 15:13:24 +0000 (17:13 +0200)] 
netlink_delinearize: prefer ct event set foo,bar over 'set foo|bar'

Translates binop representation to a list-based one, so nft prints
"ct event destroy,new" instead of 'ct event destroy|new'.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: display default directory for file inclusion in -h/--help
Pablo Neira Ayuso [Wed, 7 Jun 2017 10:24:52 +0000 (12:24 +0200)] 
src: display default directory for file inclusion in -h/--help

If no explicit relative or absolute path is enforced by the user, nft
relies on either -I/--includepath or the default include directory that
is set at compile time.

Given most of our users will rely on packaged versions of nft, provide a
way to display the location of this default includepath directory.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: nft: describe -I override behaviour when path starts by ./ and /
Pablo Neira Ayuso [Wed, 7 Jun 2017 10:13:14 +0000 (12:13 +0200)] 
doc: nft: describe -I override behaviour when path starts by ./ and /

You can override -I by starting your path either using ./ for relative
paths and / for absolute paths.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: added tests for ordering files in include dirs.
Ismo Puustinen [Wed, 7 Jun 2017 08:35:59 +0000 (11:35 +0300)] 
tests: added tests for ordering files in include dirs.

Test that the files are ordered properly by introducing included files
which have internal dependencies.

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoman: add include directory documentation.
Ismo Puustinen [Wed, 7 Jun 2017 08:35:58 +0000 (11:35 +0300)] 
man: add include directory documentation.

Short include directory introduction is added to the man page.

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: add files in include dirs in alphabetical order.
Ismo Puustinen [Wed, 7 Jun 2017 08:35:57 +0000 (11:35 +0300)] 
scanner: add files in include dirs in alphabetical order.

This means that if you have a directory structure like this

    /foo
    /foo/02_rules.nft
    /foo/01_rules.nft

where *.nft files in directory /foo are nft scripts, then an include
statement in another nft script like this

    include "/foo/"

guarantees that "01_rules.nft" is loaded before "02_rules.nft".

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser: allow ct eventmask set new,related
Florian Westphal [Wed, 7 Jun 2017 10:11:36 +0000 (12:11 +0200)] 
parser: allow ct eventmask set new,related

In case of bitmask types (tcp flags, ct eventmask) nft
allows to use comma operator to test multiple values, i.e.
tcp flags syn,ack   ct event new,destroy etc.

But currently nft fails to use this when used in a statement, i.e.
   ... ct eventmask set new,destroy
gives:
syntax error, unexpected comma

This allows makes this work, it is the same as
   ct eventmask set new|destroy

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agometa: permit meta nfproto ip in ip family
Florian Westphal [Mon, 29 May 2017 17:25:37 +0000 (19:25 +0200)] 
meta: permit meta nfproto ip in ip family

works:
add rule ip filter input ct original saddr 1.2.3.4
(family ctx init initialises network base to proto_ip).

fails to parse 1.2.3.4 address:
add rule ip filter input meta nfproto ipv4 ct original saddr 1.2.3.4

... because meta_expr_pctx_update() won't find a dependency
from "ip" to "ip" and then overwrites the correct base with proto_unknown.

"meta nfproto ip" is useless in the ip family, as it will always match,
i.e.  a better (but more compliated) fix would be to remove the statement
during evaluation.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: fix inet/bridge/netdev family handling for saddr/daddr
Florian Westphal [Mon, 29 May 2017 17:25:38 +0000 (19:25 +0200)] 
ct: fix inet/bridge/netdev family handling for saddr/daddr

"ct orignal saddr" has an invalid data type, as the address can be either ipv4 or ipv6.
For some cases we could infer it from the rhs, but there are cases where we don't have any
information, e.g. when passing ct original saddr to jhash expression.

So do the same thing that we do for "rt nexthop" -- error out and hint to user
they need to specifiy the desired address type with "meta nfproto".

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Remove expire information from list stateless ruleset.
Varsha Rao [Tue, 6 Jun 2017 06:25:40 +0000 (11:55 +0530)] 
src: Remove expire information from list stateless ruleset.

As expires is stateful information. This patch removes expire
information from list stateless ruleset. With nft -s option, the
ruleset will be as following.

table ip firewall {
        set host {
                type ipv4_addr
                flags timeout
                elements = { 10.0.0.2 timeout 10m }
        }
}

Signed-off-by: Varsha Rao <rvarsha016@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: test include directories
Ismo Puustinen [Tue, 6 Jun 2017 11:50:10 +0000 (14:50 +0300)] 
tests: test include directories

Add tests for:
  * including an empty directory
  * including directory with one or two files in it
  * testing for required trailing slash in directory name
  * testing for detecting non-existent directory
  * testing for a broken file in included directory

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: add support for include directories
Ismo Puustinen [Tue, 6 Jun 2017 11:50:09 +0000 (14:50 +0300)] 
scanner: add support for include directories

If a string after "include" keyword points to a directory instead of a
file, consider the directory to contain only nft rule files and try to
load them all. This helps with a use case where services drop their own
firewall configuration files into a directory and nft needs to include
those without knowing the exact file names.

File loading order from the include directory is not specified, so the
files inside an include directory should not depend on each other.

Fixes(Bug 1154 - Allow include statement to operate on directories and/or wildcards).

Signed-off-by: Ismo Puustinen <ismo.puustinen@intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: remove global nftnl_batch structure in mnl layer
Pablo Neira Ayuso [Mon, 29 May 2017 17:04:26 +0000 (19:04 +0200)] 
src: remove global nftnl_batch structure in mnl layer

The underlying mnl layer uses a global nftnl_batch structure. Instead,
pass pointer as parameter to the functions that need this. The netlink
layer stores a reference to this structure in struct netlink_ctx.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexpression: don't trim off unary expression on delinearization
Pablo Neira Ayuso [Fri, 26 May 2017 10:49:21 +0000 (11:49 +0100)] 
expression: don't trim off unary expression on delinearization

This transformation introduces an unnecessary asymmetry between the
linearization and delinearization steps that prevent rule deletion by
name to work fine.

Moreover, do not print htonl and ntonl from unary expression, this
syntax is not allowed by the parser.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: flush stdout on each test run
Pablo Neira Ayuso [Fri, 26 May 2017 10:49:26 +0000 (11:49 +0100)] 
tests: py: flush stdout on each test run

Useful when redirecting output to file and tail -f it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: avoid reference to multiple src data in statements which set values
Arturo Borrero Gonzalez [Fri, 26 May 2017 11:00:21 +0000 (13:00 +0200)] 
evaluate: avoid reference to multiple src data in statements which set values

Prevent this assert:

% nft [..] tcp dport set { 0 , 1 }
BUG: unknown expression type set reference
nft: netlink_linearize.c:696: netlink_gen_expr: Assertion `0' failed.
Aborted

We can't use a set here because we will not known which value to use.

With this patch, a proper error message is reported to users:

% nft add rule t c tcp dport set {1, 2, 3, 4, 5}
<cmdline>:1:28-42: Error: you cannot use a set here, unknown value to use
add rule t c tcp dport set {1, 2, 3, 4, 5}
             ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^

% nft add rule t c tcp dport set @s
<cmdline>:1:28-29: Error: you cannot reference a set here, unknown value to use
add rule t c tcp dport set @s
             ~~~~~~~~~~~~~~^^

This error is reported to all statements which set values.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Fix typo in manpage
Bertrand Bonnefoy-Claudet [Thu, 25 May 2017 10:26:26 +0000 (12:26 +0200)] 
doc: Fix typo in manpage

s/Simlar/Similar

Signed-off-by: Bertrand Bonnefoy-Claudet <bertrandbc@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: add size description for constant sets
Pablo Neira Ayuso [Fri, 26 May 2017 10:49:19 +0000 (11:49 +0100)] 
netlink: add size description for constant sets

The kernel side can make better decisions with this information when
selecting the right backend, so add this information to the set netlink
message.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosegtree: reset element size counter before adding intervals to set
Pablo Neira Ayuso [Fri, 26 May 2017 10:49:16 +0000 (11:49 +0100)] 
segtree: reset element size counter before adding intervals to set

Otherwise we get double the real size in terms of set elements during
the interval expansion to individual elements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agorule: adjust set expression size accordingly with intervals
Pablo Neira Ayuso [Fri, 26 May 2017 10:49:13 +0000 (11:49 +0100)] 
rule: adjust set expression size accordingly with intervals

For implicit sets, we have to call set_to_intervals() before we add the
set so we have the net size in elements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoMerge branch 'meta_l4_dependency'
Florian Westphal [Thu, 25 May 2017 07:14:58 +0000 (09:14 +0200)] 
Merge branch 'meta_l4_dependency'

Currently nft inserts different types of dependencies for l4 protocols,
depending on the family.

For inet, nft inserts 'meta l4proto' to e.g. check for tcp, for
ip, nft uses 'ip protocol'. Both are fine.  The ip6 family however
uses 'ip6 nexthdr', and thats a problem because e.g. tcp dport 22 will
not match packets that use ipv6 extension headers.

The series switches both ipv6 and ipv4 to use meta l4 instead
so ipv6 will always check the last transport header value.

We could ignore ip as only ipv6 uses extension headers.
However, switching ipv4 as well makes things a bit simpler because nft
then creates the same l4 dependency for all families.

Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: fix up meta l4proto change for ip family
Florian Westphal [Sun, 7 May 2017 02:04:03 +0000 (04:04 +0200)] 
tests: fix up meta l4proto change for ip family

We can delete some of the payload files now as ip/ip6/inet produce
same implicit meta l4proto dep.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: ip: switch implicit dependencies to meta l4proto too
Florian Westphal [Sun, 7 May 2017 01:53:30 +0000 (03:53 +0200)] 
src: ip: switch implicit dependencies to meta l4proto too

after ip6 nexthdr also switch ip to meta l4proto instead of ip protocol.

While its needed for ipv6 (due to extension headers) this isn't needed
for ip but it has the advantage that

tcp dport 22

produces same expressions for ip/ip6/inet families.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: fix up meta l4proto change for ip6 family
Florian Westphal [Wed, 22 Mar 2017 15:38:30 +0000 (16:38 +0100)] 
tests: fix up meta l4proto change for ip6 family

After previous commit nft generates meta l4proto for ipv6 dependencies
instead of checking the (first) nexthdr value.

This fixes up all tests cases accordingly except one which fails with

ip6/reject.t: ... 12: 'ip6 nexthdr 6 reject with tcp reset' mismatches 'meta l4proto 6 reject with tcp reset'
This will be fixed by removing the implicit dependency in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agopayload: enforce ip/ip6 protocol depending on icmp or icmpv6
Florian Westphal [Thu, 18 May 2017 11:30:54 +0000 (13:30 +0200)] 
payload: enforce ip/ip6 protocol depending on icmp or icmpv6

After some discussion with Pablo we agreed to treat icmp/icmpv6 specially.

in the case of a rule like 'tcp dport 22' the inet, bridge and netdev
families only care about the lower layer protocol.

In the icmpv6 case however we'd like to also enforce an ipv6 protocol check
(and ipv4 check in icmp case).

This extends payload_gen_special_dependency() to consider this.
With this patch:

add rule $pf filter input meta l4proto icmpv6
add rule $pf filter input meta l4proto icmpv6 icmpv6 type echo-request
add rule $pf filter input icmpv6 type echo-request

will work in all tables and all families.
For inet/bridge/netdev, an ipv6 protocol dependency is added; this will
not match ipv4 packets with ip->protocol == icmpv6, EXCEPT in the case
of the ip family.

Its still possible to match icmpv6-in-ipv4 in inet/bridge/netdev with an
explicit dependency:

add rule inet f i ip protocol ipv6-icmp meta l4proto ipv6-icmp icmpv6 type ...

Implicit dependencies won't get removed at the moment, so
  bridge ... icmp type echo-request
will be shown as
  ether type ip meta l4proto 1 icmp type echo-request

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: ipv6: switch implicit dependencies to meta l4proto
Florian Westphal [Wed, 22 Mar 2017 14:08:48 +0000 (15:08 +0100)] 
src: ipv6: switch implicit dependencies to meta l4proto

when using rule like

ip6 filter input tcp dport 22
nft generates:
  [ payload load 1b @ network header + 6 => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ payload load 2b @ transport header + 2 => reg 1 ]
  [ cmp eq reg 1 0x00001600 ]

which is: ip6 filter input ip6 nexthdr tcp dport 22
IOW, such a rule won't match if e.g. a fragment header is in place.

This changes ip6_proto to use 'meta l4proto' which is the protocol header
found by exthdr walk.

A side effect is that for bridge we get a shorter dependency chain as it
no longer needs to prepend 'ether proto ipv6' for old 'ip6 nexthdr' dep.

Only problem:

ip6 nexthdr tcp tcp dport 22
will now inject a (useless) meta l4 dependency as ip6 nexthdr is no
longer flagged as EXPR_F_PROTOCOL, to avoid this add a small helper
that skips the unneded meta dependency in that case.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: allow update of net base w. meta l4proto icmpv6
Florian Westphal [Tue, 21 Mar 2017 18:47:22 +0000 (19:47 +0100)] 
src: allow update of net base w. meta l4proto icmpv6

nft add rule ip6 f i meta l4proto ipv6-icmp icmpv6 type nd-router-advert
<cmdline>:1:50-60: Error: conflicting protocols specified: unknown vs. icmpv6

add icmpv6 to nexthdr list so base gets updated correctly.

Reported-by: Thomas Woerner <twoerner@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agopayload: split ll proto dependency into helper
Florian Westphal [Thu, 18 May 2017 11:30:54 +0000 (13:30 +0200)] 
payload: split ll proto dependency into helper

will be re-used in folloup patch for icmp/icmpv6 depenency
handling.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: add ip reject with tcp and check for mark too
Florian Westphal [Sat, 6 May 2017 23:47:48 +0000 (01:47 +0200)] 
tests: add ip reject with tcp and check for mark too

... to avoid resetting e.g. the ssh session to the vm that runs
nft-test.py.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agonetlink_delinearize: reject: remove dependency for tcp-resets
Florian Westphal [Sat, 6 May 2017 23:09:19 +0000 (01:09 +0200)] 
netlink_delinearize: reject: remove dependency for tcp-resets

We can remove a l4 dependency in ip/ipv6 families.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: add a comment wrt. reject dependency insertion
Florian Westphal [Sun, 7 May 2017 00:40:46 +0000 (02:40 +0200)] 
src: add a comment wrt. reject dependency insertion

at first I thought this was a bug but this in fact seems the right
thing, add a comment/example why adding dependency as first statement makes
sense.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: delete the old cache when dumping is interrupted
Liping Zhang [Sun, 14 May 2017 07:56:12 +0000 (15:56 +0800)] 
src: delete the old cache when dumping is interrupted

When the dumping operation is interrupted, we will restart the
cache_init(), but unfortunatly, we forget to delete the old cache.
So in extreme case, we will leak a huge amount of memory.

Running the following commands can simulate the extreme case:
  # nft add table t
  # nft add set t s {type inet_service \;}
  # for i in $(seq 65000); do
  nft add element t s {$i}
  done &
  # while : ; do
  time nft list ruleset -nn
  done

After a while, oom killer will be triggered:
  [ 2808.243537] Out of memory: Kill process 16975 (nft) score 649 or
                 sacrifice child
  [ 2808.255372] Killed process 16975 (nft) total-vm:1955348kB,
                 anon-rss:1952120kB, file-rss:0kB, shmem-rss:0kB
  [ 2858.353729] nft invoked oom-killer: gfp_mask=0x14201ca(GFP_HIGHUSER_
                 MOVABLE|__GFP_COLD), nodemask=(null),  order=0,
                 oom_score_adj=0
  [ 2858.374521] nft cpuset=/ mems_allowed=0
  ...

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexpression: print sets and maps in pretty format
Arturo Borrero Gonzalez [Tue, 2 May 2017 09:47:02 +0000 (11:47 +0200)] 
expression: print sets and maps in pretty format

Print elements per line instead of all in a single line.
The elements which can be 'short' are printed 5 per line,
and others, like IPv4 addresses are printed 2 per line.

Example:

% nft list ruleset -nnn
table ip t {
set s {
type inet_service
elements = { 1, 2, 3, 4, 10,
     432, 433, 434, 435, 436,
     437, 438, 439, 440, 441,
     442, 443, 444, 445, 446,
     447, 448, 449, 450, 12345 }
}

map m {
type inet_service . iface_index : verdict
elements = { 123 . "lo" : accept,
     1234 . "lo" : accept,
     12345 . "lo" : accept,
     12346 . "lo" : accept,
     12347 . "lo" : accept }
}

set s3 {
type ipv4_addr
elements = { 1.1.1.1, 2.2.2.2,
     3.3.3.3 }
}
}

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink_delink_delinearize: don't store dependency unless relop checks is eq check
Florian Westphal [Sun, 7 May 2017 02:04:10 +0000 (04:04 +0200)] 
netlink_delink_delinearize: don't store dependency unless relop checks is eq check

'ip protocol ne 6' is not a dependency for nexthdr protocol, and must
not be stored as such.

Fixes: 0b858391781ba308 ("src: annotate follow up dependency just after killing another")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: don't kill dependencies accross statements
Florian Westphal [Wed, 22 Mar 2017 23:50:45 +0000 (00:50 +0100)] 
tests: don't kill dependencies accross statements

add a test case for previous commit.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agonetlink_delinearize: don't kill dependencies accross statements
Florian Westphal [Sat, 6 May 2017 20:36:28 +0000 (22:36 +0200)] 
netlink_delinearize: don't kill dependencies accross statements

nft currently translates
ip protocol tcp meta mark set 1 tcp dport 22
to
mark set 0x00000001 tcp dport 22

This is wrong, the latter form is same as

mark set 0x00000001 ip protocol tcp tcp dport 22

and thats not correct (original rule sets mark for tcp packets only).

We need to clear the dependency stack whenever we see a statement other
than stmt_expr, as these will have side effects (counter, payload
mangling, logging and the like).

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser: allow listing sets in one table
Florian Westphal [Thu, 4 May 2017 15:51:54 +0000 (17:51 +0200)] 
parser: allow listing sets in one table

currently nft can lists sets:
nft list sets

but unlike e.g. 'quotas' or 'counters' we didn't support restricting it to
a table.  Now its possible to restrict set definition listing to one table:

nft list sets table inet filter

Reported-by: Thomas Woerner <twoerner@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonft.8: Enhance NAT documentation
Phil Sutter [Tue, 2 May 2017 17:51:27 +0000 (19:51 +0200)] 
nft.8: Enhance NAT documentation

This adds documentation about masquerade and redirect statements, points
out that for any NAT statement both prerouting and postrouting chains
are required and adds a bunch of examples to the section's end.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: delete element and chain in transaction
Pablo Neira Ayuso [Wed, 26 Apr 2017 09:59:39 +0000 (11:59 +0200)] 
tests: shell: delete element and chain in transaction

This patch adds a test to test refcounting from element to chain and
objects.

Reported-by: Andreas Schultz <aschultz@tpip.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: add conntrack event mask support
Florian Westphal [Fri, 24 Feb 2017 12:46:47 +0000 (13:46 +0100)] 
ct: add conntrack event mask support

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agohash: generate a random seed if seed option is empty
Liping Zhang [Sat, 15 Apr 2017 09:22:17 +0000 (17:22 +0800)] 
hash: generate a random seed if seed option is empty

Typing the "nft add rule x y ct mark set jhash ip saddr mod 2" will
not generate a random seed, instead, the seed will always be zero.

So if seed option is empty, we shoulde not set the NFTA_HASH_SEED
attribute, then a random seed will be generated in the kernel.

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: fix build warning on i686
Florian Westphal [Tue, 4 Apr 2017 18:46:01 +0000 (20:46 +0200)] 
src: fix build warning on i686

datatype.c:182:13: warning: format ā€˜%lu’ expects argument of type ā€˜long unsigned int’, but argument 2 has type ā€˜uint64_t {aka long long unsigned int}’ [-Wformat=] printf("%lu", val);

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexthdr: avoid crash with older kernels
Florian Westphal [Tue, 4 Apr 2017 18:46:46 +0000 (20:46 +0200)] 
exthdr: avoid crash with older kernels

if kernel is older it won't understand the EXTHDR_OP attribute, i.e.
the rule gets accepted as a check for ipv6 exthdr.

On dump nft is then presented with a invalid ipv6 exthdr.
So we need to get rid of the assert and output an "invalid" message on
list.  Longterm we need a proper vm description or kernel-side check
to reject such messages in first place.

After patch, test suite yields erros of type
ip6/tcpopt.t: WARNING: 'src/nft add rule --debug=netlink ip6 test-ip6 \
   input tcp option sack right 1': 'tcp option sack right 1' mismatches
'ip6 nexthdr 6 unknown-exthdr unknown 0x1 [invalid type]'

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: revisit fib examples
Pablo Neira Ayuso [Fri, 7 Apr 2017 21:36:30 +0000 (23:36 +0200)] 
doc: revisit fib examples

There are several typos there that may confuse users, fix them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: hash: fix seed attribute not listed
Laura Garcia Liebana [Fri, 24 Mar 2017 20:03:25 +0000 (21:03 +0100)] 
src: hash: fix seed attribute not listed

The tests warned about a problem with the seed listing.

/tests/py# ./nft-test.py ip/hash.t
ip/hash.t: WARNING: line: 4: 'src/nft add rule --debug=netlink \
 ip test-ip4 pre ct mark set jhash ip saddr . ip daddr mod 2 \
 seed 0xdeadbeef': 'ct mark set jhash ip saddr . ip daddr mod 2 \
 seed 0xdeadbeef' mismatches 'ct mark set jhash ip saddr . ip \
 daddr mod 2'
ip/hash.t: WARNING: line: 6: 'src/nft add rule --debug=netlink \
 ip test-ip4 pre ct mark set jhash ip saddr . ip daddr mod 2 seed \
 0xdeadbeef offset 100': 'ct mark set jhash ip saddr . ip daddr \
 mod 2 seed 0xdeadbeef offset 100' mismatches 'ct mark set jhash \
 ip saddr . ip daddr mod 2 offset 100'
ip/hash.t: 6 unit tests, 0 error, 2 warning

The expression type is now treated as an unsigned int in the
hash_expr_print() function.

Fixes 3a86406 ("src: hash: support of symmetric hash")
Signed-off-by: Laura Garcia Liebana <laura.garcia@zevenet.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: Add test for flush command selectiveness on sets
Elise Lennion [Fri, 24 Mar 2017 15:31:31 +0000 (12:31 -0300)] 
tests: Add test for flush command selectiveness on sets

This tests the selectiveness of flush command on structures that use the
generic set infrastructure (sets, maps and flow tables).

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Make flush command selective of the set structure type
Elise Lennion [Fri, 24 Mar 2017 15:30:41 +0000 (12:30 -0300)] 
src: Make flush command selective of the set structure type

The internal set infrastructure is used for sets, maps and flow tables.
The flush command requires the set type but currently it works for all
of them. E.g. if there is a set named 's' in a table 't' the following
command shouldn't be valid but still executes:

$ nft flush flow table t s

This patch makes the flush command selective so 'flush flow table' only
works in flow tables and so on.

Fixes: 6d37dae ("parser_bison: Allow flushing maps")
Fixes: 2daa0ee ("parser_bison: Allow flushing flow tables")
Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosets: Fix for missing space after last element
Phil Sutter [Wed, 22 Mar 2017 00:26:36 +0000 (01:26 +0100)] 
sets: Fix for missing space after last element

Not having a space between the last element in a set and the closing
curly brace looks ugly, so add it here.

This also adjusts all shell testcases as they match whitespace in nft
output and therefore fail otherwise.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: netns/0003many_0: Fix cleanup after error
Phil Sutter [Wed, 22 Mar 2017 00:26:35 +0000 (01:26 +0100)] 
tests: shell: netns/0003many_0: Fix cleanup after error

If rule set applying failed, this would leave a stray netns in place.

Interestingly, this situation led to other, seemingly unrelated
testcases to fail with spurious errors, e.g. sets/0015rulesetflush_0:

| $ ./run-tests.sh testcases/sets/0015rulesetflush_0
| I: using nft binary ../../src/nft
|
| W: [FAILED] testcases/sets/0015rulesetflush_0: expected 0 but got 1
| /tmp/tmp.BY7cuUYL8f:5:1-2: Error: Could not process rule: Operation not supported
| table inet filter {
| ^^
| /tmp/tmp.BY7cuUYL8f:9:1-2: Error: Could not process rule: No such file or directory
| add element inet filter blacklist_v4 {
| ^^
| /tmp/tmp.BY7cuUYL8f:5:1-2: Error: Could not process rule: Operation not supported
| table inet filter {
| ^^
| /tmp/tmp.BY7cuUYL8f:9:1-2: Error: Could not process rule: No such file or directory
| add element inet filter blacklist_v4 {
| ^^
|
| I: results: [OK] 0 [FAILED] 1 [TOTAL] 1
|
| $ ip netns list
| 1_0003many_0
| $ ip netns del 1_0003many_0
|
| $ ./run-tests.sh testcases/sets/0015rulesetflush_0
| I: using nft binary ../../src/nft
|
| I: [OK] testcases/sets/0015rulesetflush_0
|
| I: results: [OK] 1 [FAILED] 0 [TOTAL] 1

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: Add test cases for nested anonymous sets
Phil Sutter [Wed, 22 Mar 2017 00:26:34 +0000 (01:26 +0100)] 
tests: Add test cases for nested anonymous sets

This makes sure nesting of anonymous sets works regardless of whether
defines are used or not. As a side-effect, it also checks that overlap
checking when IP address prefixes are used, works.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: set: Fix nested set merge size adjustment
Phil Sutter [Mon, 20 Mar 2017 16:38:56 +0000 (17:38 +0100)] 
evaluate: set: Fix nested set merge size adjustment

When merging a nested set into the parent one, we are actually replacing
one item with the items of the nested set. Therefore we have to remove
the replaced item from size.

The respective bug isn't as easy to trigger, since the size field seems
to be relevant only when set elements are ranges which are checked for
overlaps. Here's an example of how to trigger it:

| add rule ip saddr { { 1.1.1.0/24, 3.3.3.0/24 }, 2.2.2.0/24 }

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: set: Allow for set elems to be sets
Phil Sutter [Mon, 20 Mar 2017 16:38:55 +0000 (17:38 +0100)] 
evaluate: set: Allow for set elems to be sets

Recursive use of sets is handled in parts by parser_bison.y, which
has a rule for inline unnamed sets in set_list_member_expr, e.g. like
this:

| add rule ip saddr { { 1.1.1.0, 2.2.2.0 }, 3.3.3.0 }

Yet there is another way to have an unnamed set inline, which is via
define:

| define myset = {
|  1.1.1.0,
|  2.2.2.0,
| }
| add rule ip saddr { $myset, 3.3.3.0 }

This didn't work because the inline set comes in as EXPR_SET_ELEM with
EXPR_SET as key. This patch handles that case by replacing the former by
a copy of the latter, so the following set list merging can take place.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Document add / delete element operations of sets and maps
Elise Lennion [Fri, 17 Mar 2017 15:04:48 +0000 (12:04 -0300)] 
doc: Document add / delete element operations of sets and maps

The add / delete operations weren't documented yet. They fit better
in the sets and maps blocks since these operations are used to directly
modify their content.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: Allow flushing maps
Elise Lennion [Fri, 17 Mar 2017 15:04:06 +0000 (12:04 -0300)] 
parser_bison: Allow flushing maps

This patch enables the command flush on maps, which removes all
entries in it:

$ nft flush map filter map1

Command above flushes map 'map1' in table 'filter'.

The documentation was updated accordingly.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: Allow flushing flow tables
Elise Lennion [Fri, 17 Mar 2017 15:03:17 +0000 (12:03 -0300)] 
parser_bison: Allow flushing flow tables

This patch enables the command flush on flow tables, which removes all
entries in it:

$ nft flush flow table filter ft-https

Command above flushes flow table 'ft-https' in table 'filter'.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Describe ICMP(v6) expression and types
Phil Sutter [Thu, 16 Mar 2017 12:43:21 +0000 (13:43 +0100)] 
doc: Describe ICMP(v6) expression and types

This adds a description of the icmp and icmpv6 expressions (to match
various ICMP header fields) as well as the icmp and icmpv6 type types
(yay) which are used for ICMP(v6) type field.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoproto: Add some exotic ICMPv6 types
Phil Sutter [Thu, 16 Mar 2017 12:43:20 +0000 (13:43 +0100)] 
proto: Add some exotic ICMPv6 types

This adds support for matching on inverse ND messages as defined by
RFC3122 (not implemented in Linux) and MLDv2 as defined by RFC3810.

Note that ICMPV6_MLD2_REPORT macro is defined in linux/icmpv6.h but
including that header leads to conflicts with symbols defined in
netinet/icmp6.h.

In addition to the above, "mld-listener-done" is introduced as an alias
for "mld-listener-reduction".

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: ct helper objects and helper set support
Florian Westphal [Tue, 14 Mar 2017 16:22:03 +0000 (17:22 +0100)] 
doc: ct helper objects and helper set support

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: add insert-failure test
Florian Westphal [Wed, 15 Mar 2017 13:21:56 +0000 (14:21 +0100)] 
tests: add insert-failure test

It should not be possible to add a ip6 restricted helper to ip family.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: add ct helper tests
Florian Westphal [Tue, 14 Mar 2017 15:55:17 +0000 (16:55 +0100)] 
tests: py: add ct helper tests

needs minor tweak to nft-test.py so we don't zap the ';' withhin the {}.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: implement add/create/delete for ct helper objects
Florian Westphal [Mon, 20 Feb 2017 17:06:32 +0000 (18:06 +0100)] 
src: implement add/create/delete for ct helper objects

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: allow listing all ct helpers
Florian Westphal [Mon, 20 Feb 2017 17:03:18 +0000 (18:03 +0100)] 
src: allow listing all ct helpers

this implements
nft list ct helpers table filter
table ip filter {
    ct helper ftp-standard {
..

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: refactor CMD_OBJ_QUOTA/COUNTER handling
Florian Westphal [Mon, 27 Feb 2017 14:11:42 +0000 (15:11 +0100)] 
evaluate: refactor CMD_OBJ_QUOTA/COUNTER handling

... to make adding CMD_OBJ_CT_HELPER support easier.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add initial ct helper support
Florian Westphal [Tue, 21 Feb 2017 17:11:31 +0000 (18:11 +0100)] 
src: add initial ct helper support

This adds initial support for defining conntrack helper objects
which can then be assigned to connections using the objref infrastructure:

table ip filter {
  ct helper ftp-standard {
    type "ftp" protocol tcp
  }
  chain y {
 tcp dport 21 ct helper set "ftp-standard"
  }
}

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: fix ct mark set example
Florian Westphal [Wed, 15 Mar 2017 15:02:25 +0000 (16:02 +0100)] 
doc: fix ct mark set example

Fixes: 0d7fdb10b10c7a6c ("doc: payload and conntrack statement")
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agofiles: provide 'raw' table equivalent
Florian Westphal [Tue, 14 Mar 2017 19:12:30 +0000 (20:12 +0100)] 
files: provide 'raw' table equivalent

useful for the 'ct zone set' statement, it has to be done before
the conntrack lookup but preferrably after the defragmention hook.

In iptables, the functionality resides in the CT target which is
restricted to the raw table.  This provides the skeleton for nft.

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agodoc: ct zone set support
Florian Westphal [Tue, 14 Mar 2017 19:12:17 +0000 (20:12 +0100)] 
doc: ct zone set support

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: shell: test comments with small sets
Pablo Neira Ayuso [Mon, 13 Mar 2017 12:22:03 +0000 (13:22 +0100)] 
tests: shell: test comments with small sets

Just a test to cover the bitmap backend implementation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Document boolean type and applications
Phil Sutter [Sat, 11 Mar 2017 13:31:41 +0000 (14:31 +0100)] 
doc: Document boolean type and applications

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: Adjust for changed exthdr debug output
Phil Sutter [Sat, 11 Mar 2017 13:31:40 +0000 (14:31 +0100)] 
tests: Adjust for changed exthdr debug output

Debug output from libnftnl has changed to include 'present' keyword if
NFT_EXTHDR_F_PRESENT flag is set in expression.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agofib: Support existence check
Phil Sutter [Sat, 11 Mar 2017 13:31:39 +0000 (14:31 +0100)] 
fib: Support existence check

This allows to check whether a FIB entry exists for a given packet by
comparing the expression with a boolean keyword like so:

| fib daddr oif exists

The implementation requires introduction of a generic expression flag
EXPR_F_BOOLEAN which allows relational expression to signal it's LHS
that a boolean comparison is being done (indicated by boolean type on
RHS). In contrast to exthdr existence checks, fib expression can't know
this in beforehand because the LHS syntax is absolutely identical to a
non-boolean comparison.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: fix crash when inputting an incomplete set add command
Liping Zhang [Sat, 11 Mar 2017 04:20:11 +0000 (12:20 +0800)] 
src: fix crash when inputting an incomplete set add command

After inputting the following nft command, set->keytype is not initialized
but we try to destroy it, so NULL pointer dereference will happen:
  # nft add set t s
  Segmentation fault (core dumped)
  #0  dtype_free (dtype=0x0) at datatype.c:1049
  #1  set_datatype_destroy (dtype=0x0) at datatype.c:1051
  #2  0x0000000000407f1a in set_free (set=0x838790) at rule.c:213
  #3  0x000000000042ff70 in nft_parse (scanner=scanner@entry=0x8386a0,
    state=state@entry=0x7ffc313ea670) at parser_bison.c:9355
  #4  0x000000000040727d in nft_run (scanner=scanner@entry=0x8386a0,
    state=state@entry=0x7ffc313ea670, msgs=msgs@entry=0x7ffc313ea660)
    at main.c:237
  #5  0x0000000000406e4a in main (argc=<optimized out>, argv=<optimized
    out>) at main.c:376

Fixes: b9b6092304ae ("evaluate: store byteorder for set keys")
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexthdr: Implement existence check
Phil Sutter [Fri, 10 Mar 2017 17:13:51 +0000 (18:13 +0100)] 
exthdr: Implement existence check

This allows to check for existence of an IPv6 extension or TCP
option header by using the following syntax:

| exthdr frag exists
| tcpopt window exists

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexthdr: Add support for exthdr specific flags
Phil Sutter [Fri, 10 Mar 2017 17:13:50 +0000 (18:13 +0100)] 
exthdr: Add support for exthdr specific flags

This allows to have custom flags in exthdr expression, which is
necessary for upcoming existence checks (of both IPv6 extension headers
as well as TCP options).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoIntroduce boolean datatype and boolean expression
Phil Sutter [Fri, 10 Mar 2017 17:13:49 +0000 (18:13 +0100)] 
Introduce boolean datatype and boolean expression

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: BUG when object type is unknown
Florian Westphal [Sun, 19 Feb 2017 22:15:55 +0000 (23:15 +0100)] 
netlink: BUG when object type is unknown

This will only trigger during development when adding new object types to
the parser.

The BUG() gives a clear hint where the serialization code needs to go.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: use nftnl_udata_put_u32()/nftnl_udata_get_u32()
Pablo Neira Ayuso [Mon, 6 Mar 2017 14:55:33 +0000 (15:55 +0100)] 
netlink: use nftnl_udata_put_u32()/nftnl_udata_get_u32()

Use these new type-specific helper functions instead available in
libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: hash: support of symmetric hash
Laura Garcia Liebana [Tue, 28 Feb 2017 17:42:50 +0000 (18:42 +0100)] 
src: hash: support of symmetric hash

This patch provides symmetric hash support according to source
ip address and port, and destination ip address and port.

The new attribute NFTA_HASH_TYPE has been included to support
different types of hashing functions. Currently supported
NFT_HASH_JENKINS through jhash and NFT_HASH_SYM through symhash.

The main difference between both types are:
 - jhash requires an expression with sreg, symhash doesn't.
 - symhash supports modulus and offset, but not seed.

Examples:

 nft add rule ip nat prerouting ct mark set jhash ip saddr mod 2
 nft add rule ip nat prerouting ct mark set symhash mod 2

Signed-off-by: Laura Garcia Liebana <laura.garcia@zevenet.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>