]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
8 years agotests: py: fix some typos in README
Timothy Redaelli [Thu, 2 Mar 2017 15:15:18 +0000 (16:15 +0100)] 
tests: py: fix some typos in README

Python script name ends by .py instead of .sh

Signed-off-by: Timothy Redaelli <tredaelli@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: revisit tcp options support
Pablo Neira Ayuso [Tue, 28 Feb 2017 12:31:54 +0000 (13:31 +0100)] 
src: revisit tcp options support

Rework syntax, add tokens so we can extend the grammar more easily.
This has triggered several syntax changes with regards to the original
patch, specifically:

tcp option sack0 left 1

There is no space between sack and the block number anymore, no more
offset field, now they are a single field. Just like we do with rt, rt0
and rt2. This simplifies our grammar and that is good since it makes our
life easier when extending it later on to accomodate new features.

I have also renamed sack_permitted to sack-permitted. I couldn't find
any option using underscore so far, so let's keep it consistent with
what we have.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: add test entries for conntrack zones
Florian Westphal [Mon, 27 Feb 2017 23:59:17 +0000 (00:59 +0100)] 
tests: add test entries for conntrack zones

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: support zone set statement with optional direction
Florian Westphal [Mon, 27 Feb 2017 23:59:07 +0000 (00:59 +0100)] 
src: support zone set statement with optional direction

nft automatically understands 'ct zone set 1' but when a direction is
specified too we get a parser error since they are currently only
allowed for plain ct expressions.

This permits the existing syntax ('ct original zone') for all tokens with
an optional direction also for set statements.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: refactor print function so it can be re-used for ct statement
Florian Westphal [Mon, 27 Feb 2017 23:59:05 +0000 (00:59 +0100)] 
ct: refactor print function so it can be re-used for ct statement

Once directional zone support is added we also need to print the
direction of the statement, so factor the common code to re-use
this helper from the statement print function.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add conntrack zone support
Florian Westphal [Mon, 27 Feb 2017 23:59:02 +0000 (00:59 +0100)] 
src: add conntrack zone support

This enables zone get/set support.

As the zone can be optionally tied to a direction as well we need a new
token for this (unless we turn reply/original into tokens in which case
we could handle zone via STRING).

There was some discussion on how zone set support should be handled,
especially 'zone set 1'.

There are several issues to consider:

1. its not possible to change a zone 'later on', any given
conntrack flow has exactly one zone for its entire lifetime.

2. to create conntracks in a given zone, the zone therefore has to be
assigned *before* the packet gets picked up by conntrack (so that lookup
finds the correct existing flow or the flow is created with the desired
zone id).  In iptables, this is enforced because zones are assigned with
CT target and this is restricted to the 'raw' table in iptables, which
runs after defragmentation but before connection tracking.

3. Thus, in nftables the 'ct zone set' rule needs to hook before
conntrack too, e.g. via

 table raw {
  chain pre {
   type filter hook prerouting priority -300;
   iif eth3 ct zone set 23
  }
  chain out {
   type filter hook output priority -300;
   oif eth3 ct zone set 23
  }
 }

... but this is not enforced.

There were two alternatives to better document this.
One was to use an explicit 'template' keyword:
  nft ... template zone set 23

... but 'connection tracking templates' are a kernel detail
that users should not and need not know about.

The other one was to use the meta keyword instead since
we're (from a practical point of view) assigning the zone to
the packet, not the conntrack:

 nft ... meta zone set 23

However, next patch also supports 'directional' zones, and

 nft ... meta original zone 23

makes no sense because 'direction' refers to a direction as understood
by the connection tracker.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: store byteorder for set data
Pablo Neira Ayuso [Mon, 27 Feb 2017 22:42:03 +0000 (23:42 +0100)] 
src: store byteorder for set data

Add new UDATA_SET_DATABYTEORDER attribute for NFTA_SET_UDATA to store
the datatype byteorder. This is required if integer_type is used on the
rhs of the mapping given that this datatype comes with no specific
byteorder.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: rework NFTNL_SET_USERDATA to accomodate new attributes
Pablo Neira Ayuso [Mon, 27 Feb 2017 22:47:19 +0000 (23:47 +0100)] 
netlink: rework NFTNL_SET_USERDATA to accomodate new attributes

Rework the NFTNL_SET_USERDATA in netlink_delinearize_set() to accomodate
rhs datatype byteorder in mappings.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: rename set_keytype_alloc() to set_datatype_alloc()
Pablo Neira Ayuso [Mon, 27 Feb 2017 22:16:09 +0000 (23:16 +0100)] 
src: rename set_keytype_alloc() to set_datatype_alloc()

This function can be used either side of the map, so rename it to
something generic.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: set byteorder as lhs expression context in stmt_evaluate_arg()
Pablo Neira Ayuso [Mon, 27 Feb 2017 12:54:45 +0000 (13:54 +0100)] 
evaluate: set byteorder as lhs expression context in stmt_evaluate_arg()

stmt_evaluate_arg() needs to take the lhs map expression byteorder in
order to evaluate the lhs of mappings accordingly.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: add DTYPE_F_CLONE flag
Pablo Neira Ayuso [Sat, 25 Feb 2017 12:15:14 +0000 (13:15 +0100)] 
datatype: add DTYPE_F_CLONE flag

This flag allows us to identify datatypes that are instances from
original datatypes.

This fixes a possible double free when attaching a concatenation
datatype to set->keytype while being also referenced from concatenation
expressions.

ip6/flowtable.t: ERROR: line 5: src/nft add rule --debug=netlink ip6 test-ip6 input flow table acct_out { meta iif . ip6 saddr timeout 600s counter }: This rule should not have failed.
*** Error in `src/nft': double free or corruption (fasttop): 0x000000000117ce70 ***

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: fix incorrect bytecode in numgen and hash mappings
Pablo Neira Ayuso [Sat, 25 Feb 2017 11:36:10 +0000 (12:36 +0100)] 
tests: py: fix incorrect bytecode in numgen and hash mappings

Byteorder is not correct as it is expressed in network byteorder. This
uncovered when storing set byteorder in NFTA_USER_DATA.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink_delinearize: remove integer_type_postprocess()
Pablo Neira Ayuso [Sat, 25 Feb 2017 11:01:42 +0000 (12:01 +0100)] 
netlink_delinearize: remove integer_type_postprocess()

Not required anymore since the set definition now comes with the right
byteorder for integer types via NFTA_SET_USERDATA area. So we don't need
to look at the lhs anymore. Note that this was a workaround that does
not work with named sets, where we cannot assume we have a lhs, since
it is valid to have a named set that is not referenced from any rule.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: store set byteorder in NFTA_SET_USERDATA
Pablo Neira Ayuso [Sat, 25 Feb 2017 00:02:08 +0000 (01:02 +0100)] 
netlink: store set byteorder in NFTA_SET_USERDATA

The integer datatype has neither specific byteorder nor length. This
results in the following broken output:

 # nft list ruleset
 table ip x {
        chain y {
                mark set cpu map { 0 : 0x00000001, 16777216 : 0x00000002}
        }
 }

Currently, with BYTEORDER_INVALID, nft defaults on network byteorder,
hence the output above.

This patch stores the key byteorder in the userdata using a TLV
structure in the NFTA_SET_USERDATA area, so nft can interpret key
accordingly when dumping the set back to userspace.

Thus, after this patch the listing is correct:

 # nft list ruleset
 table ip x {
        chain y {
                mark set cpu map { 0 : 0x00000001, 1 : 0x00000002}
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: store byteorder for set keys
Pablo Neira Ayuso [Fri, 24 Feb 2017 19:47:49 +0000 (20:47 +0100)] 
evaluate: store byteorder for set keys

Selectors that rely on the integer type and expect host endian
byteorder don't work properly.

We need to keep the byteorder around based on the left hand size
expression that provides the context, so store the byteorder when
evaluating the map.

Before this patch.

 # nft --debug=netlink add rule x y meta mark set meta cpu map { 0 : 1, 1 : 2 }
 __map%d x b
 __map%d x 0
        element 00000000  : 00000001 0 [end]    element 01000000  : 00000002 0 [end]
                                                        ^^^^^^^^

This is expressed in network byteorder, because the invalid byteorder
defaults on this.

After this patch:

 # nft --debug=netlink add rule x y meta mark set meta cpu map { 0 : 1, 1 : 2 }
 __map%d x b
 __map%d x 0
        element 00000000  : 00000001 0 [end]    element 00000001  : 00000002 0 [end]
                                                        ^^^^^^^^

This is in host byteorder, as the key selector in the map mandates.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: add tcpopt.h to Makefile.am
Pablo Neira Ayuso [Wed, 22 Feb 2017 12:46:55 +0000 (13:46 +0100)] 
include: add tcpopt.h to Makefile.am

Add this new header filer otherwise make distcheck breaks.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoerec: Fix input descriptors for included files
Anatole Denis [Tue, 21 Feb 2017 14:48:05 +0000 (15:48 +0100)] 
erec: Fix input descriptors for included files

Currently, when creating an error record (erec), the current location in the
file is duplicated, but not the input_descriptor inside it. Input descriptors
are added and removed by the parser when including files, and memory references
in the error record thus become incorrect when a subsequent file is included.

This patch copies the input descriptors recursively to ensure each erec has the
correct chain of input descriptors at the time of printing.

For example:
badinclude.nft:
```
include "error.nft"
include "empty.nft"
```
a.nft:
```
add rule t c obvious syntax error
```
b.nft: (empty file)

Results in the last included file being referenced and quoted for all errors
$ nft -f badinclude.nft
In file included from badinclude.nft:2:1-20:
./empty.nft:1:34-34: Error: syntax error, unexpected newline

                                 ^

Expected behavior:
$ nft -f badinclude.nft -I.
In file included from badinclude.nft:1:1-20:
./error.nft:1:34-34: Error: syntax error, unexpected newline
add rule t c obvious syntax error
                                 ^

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Document maps
Elise Lennion [Mon, 13 Feb 2017 16:47:11 +0000 (14:47 -0200)] 
doc: Document maps

This patch adds the missing documentation for maps. Also, updates sets
policy to match maps.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: remove nft_set_bitmap on each run
Pablo Neira Ayuso [Thu, 16 Feb 2017 13:30:39 +0000 (14:30 +0100)] 
tests: shell: remove nft_set_bitmap on each run

Update shell/run-tests.sh to remove this new module too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: refresh linux/netfilter/nf_tables.h
Pablo Neira Ayuso [Mon, 13 Feb 2017 19:02:19 +0000 (20:02 +0100)] 
include: refresh linux/netfilter/nf_tables.h

Get us in sync with kernel tree header file.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: Add basic tests for ip, ip6 and inet
Manuel Messner [Tue, 7 Feb 2017 02:14:15 +0000 (03:14 +0100)] 
tests: py: Add basic tests for ip, ip6 and inet

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agopayload: automatically kill dependencies for exthdr and tcpopt
Manuel Messner [Tue, 7 Feb 2017 02:14:14 +0000 (03:14 +0100)] 
payload: automatically kill dependencies for exthdr and tcpopt

This patch automatically removes the dependencies for exthdr and tcpopt.

 # nft add rule filter input tcp option maxseg kind 3 counter.
 # nft list table filter input

Before:
 # ip protocol 6 tcp option maxseg kind 3 counter

After:
 # tcp option maxseg kind 3 counter

Thus allowing to write tests as follows:
 # tcp option maxseg kind 3;ok

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agopayload: insert implicit meta tcp dependency when matching tcp options
Florian Westphal [Tue, 7 Feb 2017 02:14:13 +0000 (03:14 +0100)] 
payload: insert implicit meta tcp dependency when matching tcp options

nft add rule inet filter input tcp option sack 4 left 1
<cmdline>:1:28-49: Error: Cannot generate dependency: no network layer protocol specified

Users can avoid this via 'meta l4proto tcp', this enables implicit
dependency injection for the inet/bridge/netdev families.

Reviewed-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: add TCP option matching
Manuel Messner [Tue, 7 Feb 2017 02:14:12 +0000 (03:14 +0100)] 
src: add TCP option matching

This patch enables nft to match against TCP options.

Currently these TCP options are supported:
* End of Option List (eol)
* No-Operation (noop)
* Maximum Segment Size (maxseg)
* Window Scale (window)
* SACK Permitted (sack_permitted)
* SACK (sack)
* Timestamps (timestamp)

Syntax: tcp options $option_name [$offset] $field_name
Example:

 # count all incoming packets with a specific maximum segment size `x`
 # nft add rule filter input tcp option maxseg size x counter

 # count all incoming packets with a SACK TCP option where the third
 # (counted from zero) left field is greater `x`.
 # nft add rule filter input tcp option sack 2 left \> x counter

If the offset (the `2` in the example above) is zero, it can optionally
be omitted.
For all non-SACK TCP options it is always zero, thus can be left out.

Option names and field names are parsed from templates, similar to meta
and ct options rather than via keywords to prevent adding more keywords
than necessary.

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoexthdr: prepare exthdr_gen_dependency for tcp support
Manuel Messner [Tue, 7 Feb 2017 02:14:11 +0000 (03:14 +0100)] 
exthdr: prepare exthdr_gen_dependency for tcp support

currently exthdr always needs ipv6 dependency (i.e. link layer), but
with upcomming TCP option matching we also need to include TCP at the
network layer.

This patch prepares this change by adding two parameters to
exthdr_gen_dependency.

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoexthdr: prepare for tcp support
Manuel Messner [Tue, 7 Feb 2017 02:14:10 +0000 (03:14 +0100)] 
exthdr: prepare for tcp support

right now exthdr only deals with ipv6 extension headers, followup
patch will enable tcp option matching.

This adds the 'op' arg to exthdr_init.

Signed-off-by: Manuel Messner <mm@skelett.io>
Reviewed-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoinclude: linux: netfilter: nf_tables: copy file from nf-next
Manuel Messner [Tue, 7 Feb 2017 02:14:09 +0000 (03:14 +0100)] 
include: linux: netfilter: nf_tables: copy file from nf-next

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agostatement: Avoid rounding bytes in get_rate()
Elise Lennion [Sun, 12 Feb 2017 12:45:34 +0000 (10:45 -0200)] 
statement: Avoid rounding bytes in get_rate()

get_rate() is used to print quotas and limits and currently rounds the
number of bytes:

$ nft add quota filter https-quota 4000 kbytes
$ nft list ruleset
table ip filter {
quota https-quota {
3 mbytes
}
}

This may be a problem when loading your configuration after saving it
with 'list ruleset'. With this patch the values are represented in a
greater unit only when there is no rest in the conversion:

$ nft add quota filter https-quota2 2048 kbytes
$ nft list ruleset
table ip filter {
quota https-quota {
4000 kbytes
}

quota https-quota2 {
2 mbytes
}
}

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Document sets
Elise Lennion [Thu, 9 Feb 2017 17:35:25 +0000 (15:35 -0200)] 
doc: Document sets

This patch adds the missing documentation for sets.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Document stateful objects
Elise Lennion [Thu, 9 Feb 2017 13:48:16 +0000 (11:48 -0200)] 
doc: Document stateful objects

This patch adds documentation for stateful objects and updates
tables description to mention them.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: Replace getaddrinfo() by internal lookup table
Elise Lennion [Mon, 6 Feb 2017 15:53:40 +0000 (13:53 -0200)] 
datatype: Replace getaddrinfo() by internal lookup table

Nftables uses a internal service table to print service names. This
table should be used when parsing new rules, to avoid conflicts between
nft service table and the local /etc/services, when loading an exported
ruleset.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1118
Fixes: ccc5da4 ("datatype: Replace getnameinfo() by internal lookup table")
Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agostatement: fix print of ip dnat address
Florian Westphal [Fri, 3 Feb 2017 14:22:28 +0000 (15:22 +0100)] 
statement: fix print of ip dnat address

the change causes non-ipv6 addresses to not be printed at all in case
a nfproto was given.

Also add a test case to catch this.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1117
Fixes: 5ab0e10fc6e2c22363a ("src: support for RFC2732 IPv6 address format with brackets")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoconfigure: Require newer version of libxtables
Elise Lennion [Thu, 2 Feb 2017 11:22:55 +0000 (09:22 -0200)] 
configure: Require newer version of libxtables

Currently, the configure script requires xtables v1.6.0 when the option
--with-xtables is given. However, nftables-0.7 build fails with this
version, xtables v1.6.1 is the minimum required to have libxtables
support.

Fixes(Bug 1110 - Build failure if --with-xtables).

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Always print range expressions numerically
Elise Lennion [Thu, 2 Feb 2017 12:31:56 +0000 (10:31 -0200)] 
src: Always print range expressions numerically

Because the rules are more legible this way. Also, the parser doesn't
accept strings on ranges, so, printing ranges numerically better match
the rules definition.

Fixes(Bug 1046 - mobility header with range gives illegible rule).

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agomain: Validate the number of numeric options
Elise Lennion [Thu, 2 Feb 2017 12:25:53 +0000 (10:25 -0200)] 
main: Validate the number of numeric options

The number of numeric options influences the behavior and the user
should be warned if a invalid number is used.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: fix typo
Manuel Messner [Sun, 25 Dec 2016 19:27:59 +0000 (20:27 +0100)] 
evaluate: fix typo

Signed-off-by: Manuel Messner <mm@skelett.io>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: py: Add tests for stateful objects
Elise Lennion [Thu, 26 Jan 2017 17:21:45 +0000 (15:21 -0200)] 
tests: py: Add tests for stateful objects

These test for adding counters and quotas, to tables of different
families, and for referencing the objects in rules.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: Add suport for stateful objects in python tests
Elise Lennion [Thu, 26 Jan 2017 17:18:25 +0000 (15:18 -0200)] 
tests: py: Add suport for stateful objects in python tests

This allows to write pytests using the new stateful objects.

To add an object use the symbol '%', followed by the name, type and
specifications (currently used in quota):

%cnt1 type counter;ok # Adds the counter cnt1 to all tables

%qt1 type quota over 25 mbytes;ok # Adds the quota qt1 to all tables

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Allow list stateful objects in a table
Elise Lennion [Thu, 26 Jan 2017 17:15:44 +0000 (15:15 -0200)] 
src: Allow list stateful objects in a table

Currently, stateful objects can be listed by: listing all objects in
all tables; listing a single object in a table. Now it's allowed to
list all objects in a table.

$ nft list counters table filter
table ip filter {
counter https-traffic {
packets 14825 bytes 950063
}
counter http-traffic {
packets 117 bytes 9340
}
}

$ nft list quotas table filter
table ip filter {
quota https-quota {
25 mbytes used 2 mbytes
}
quota http-quota {
25 mbytes used 10 kbytes
}
}

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Evaluate table name before reset stateful objects in a table
Elise Lennion [Thu, 26 Jan 2017 17:14:30 +0000 (15:14 -0200)] 
evaluate: Evaluate table name before reset stateful objects in a table

Reseting stateful objects in a single table is already implemented and
cmd_evaluate_reset() now tests for the table name.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Allow list single stateful object
Elise Lennion [Thu, 26 Jan 2017 17:12:54 +0000 (15:12 -0200)] 
src: Allow list single stateful object

Currently the stateful objects can only be listed in groups. With this
patch listing a single object is allowed:

$ nft list counter filter https-traffic
table ip filter {
counter https-traffic {
packets 4014 bytes 228948
}
}

$ nft list quota filter https-quota
table ip filter {
quota https-quota {
25 mbytes used 278 kbytes
}
}

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Allow reset single stateful object
Elise Lennion [Thu, 26 Jan 2017 17:09:44 +0000 (15:09 -0200)] 
src: Allow reset single stateful object

Currently the stateful objects can only be reseted in groups. With this
patch reseting a single object is allowed:

$ nft reset counter filter https-traffic
table ip filter {
counter https-traffic {
packets 8774 bytes 542668
}
}

$ nft list counter filter https-traffic
table ip filter {
counter https-traffic {
packets 0 bytes 0
}
}

Heavily based on work from Pablo Neira Ayuso <pablo@netfilter.org>.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agopayload: explicit network ctx assignment for icmp/icmp6 in special families
Arturo Borrero Gonzalez [Wed, 25 Jan 2017 11:51:08 +0000 (12:51 +0100)] 
payload: explicit network ctx assignment for icmp/icmp6 in special families

In the inet, bridge and netdev families, we can add rules like these:

% nft add rule inet t c ip protocol icmp icmp type echo-request
% nft add rule inet t c ip6 nexthdr icmpv6 icmpv6 type echo-request

However, when we print the ruleset:

% nft list ruleset
table inet t {
chain c {
icmpv6 type echo-request
icmp type echo-request
}
}

These rules we obtain can't be added again:

% nft add rule inet t c icmp type echo-request
<cmdline>:1:19-27: Error: conflicting protocols specified: inet-service vs. icmp
add rule inet t c icmp type echo-request
                  ^^^^^^^^^

% nft add rule inet t c icmpv6 type echo-request
<cmdline>:1:19-29: Error: conflicting protocols specified: inet-service vs. icmpv6
add rule inet t c icmpv6 type echo-request
                  ^^^^^^^^^^^

Since I wouldn't expect an IP packet carrying ICMPv6, or IPv6 packet
carrying ICMP, if the link layer is inet, the network layer protocol context
can be safely update to 'ip' or 'ip6'.

Moreover, nft currently generates a 'meta nfproto ipvX' depedency when
using icmp or icmp6 in the inet family, and similar in netdev and bridge
families.

While at it, a bit of code factorization is introduced.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1073
Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: validate set size
Pablo Neira Ayuso [Tue, 24 Jan 2017 18:33:07 +0000 (19:33 +0100)] 
tests: shell: validate set size

Add two tests to make sure that set size checks work fine:

1) Check if set size is indeed working, this is a simple one.
2) Check if set size is correct after ENFILE error, there is bug that
   adds a new spare slot everytime we hit this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: Add test for flush bug on sets with size
Elise Lennion [Mon, 23 Jan 2017 22:42:00 +0000 (20:42 -0200)] 
tests: shell: Add test for flush bug on sets with size

This tests for a bug where elements can't be added after flushing a
full set with the flag NFTNL_SET_DESC_SIZE set.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agorule: check for errors from cache_init_objects() for stateful objects
Pablo Neira Ayuso [Mon, 23 Jan 2017 13:35:42 +0000 (14:35 +0100)] 
rule: check for errors from cache_init_objects() for stateful objects

Catch -1 case, so we have a chance to handle EINTR.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: don't bail out on dump errors
Pablo Neira Ayuso [Thu, 19 Jan 2017 16:43:56 +0000 (17:43 +0100)] 
netlink: don't bail out on dump errors

This patch removes the existing error messages on netlink dump errors.
These functions used to be called from list commands. These days they
are called from the cache cache population path.

Note that nft breaks with older kernels at netlink_list_objs() since we
have no stateful objects support there.

Silence errors at this stage and return an empty list, thus, nft bails
out on explicit user commands if no nf_tables support is available.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Honor obligatory stateless printing of flow tables
Elise Lennion [Wed, 18 Jan 2017 23:06:47 +0000 (21:06 -0200)] 
src: Honor obligatory stateless printing of flow tables

Always print flow statement as stateless expressions, given that this
just defines the flow table statement instance that is allocated per
entry.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
8 years agotests: shell: Add test for set element leak bug
Elise Lennion [Thu, 19 Jan 2017 16:29:47 +0000 (14:29 -0200)] 
tests: shell: Add test for set element leak bug

This tests for a bug where a repeated element is added and the set
elements counter is incorrectly increased.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
8 years agotests: py: Use stateless option on tests
Elise Lennion [Tue, 17 Jan 2017 15:44:54 +0000 (13:44 -0200)] 
tests: py: Use stateless option on tests

To don't trigger false errors because of unrelated traffic on the
tested machine.

Tests, which have rules with counter and 'ok' result, are updated to
avoid new Warnings.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: Include stateless option
Elise Lennion [Tue, 17 Jan 2017 15:39:34 +0000 (13:39 -0200)] 
doc: Include stateless option

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: fix export length and data corruption
Florian Westphal [Mon, 16 Jan 2017 13:24:31 +0000 (14:24 +0100)] 
evaluate: fix export length and data corruption

Pablo reported that ipv6 tests would fail on some systems:
WARNING: 'add rule --debug=netlink ip6 test-ip6 input ip6 flowlabel set 0':
'[ bitwise reg 1 = (reg=1 & 0x000000f0 ) ^ 0x00000000 ]' mismatches
'[ bitwise reg 1 = (reg=1 & 0x00000000 ) ^ 0x00000000 ]'
                                    ^ should be 'f'

Problem is that mpz_export_data expects the size of the output
buffer in bytes, but this gave bit-based size.

Then, when mpz_export_data clears the output buffer it will
also clear 8 extra bytes on stack; depending on compiler version (stack
layout) this will then clear the bitmask value that we want to export.

Fixes: 78936d50f306c ("evaluate: add support to set IPv6 non-byte header fields")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Tested-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Allow to list ruleset without stateful information
Elise Lennion [Mon, 16 Jan 2017 20:40:40 +0000 (18:40 -0200)] 
src: Allow to list ruleset without stateful information

Currently only counter and quota have stateful information.

For named counters, packets and bytes are displayed as 0.

Standard list ruleset:

table ip filter {
counter https {
packets 161942 bytes 10253353
}

chain output {
type filter hook output priority 0; policy accept;
counter name tcp dport map { https : "https"}
tcp dport https counter packets 171211 bytes 10869045
tcp dport https quota 25 mbytes used 10 mbytes
}
}

With stateless option, -s:

table ip filter {
counter https {
packets 0 bytes 0
}

chain output {
type filter hook output priority 0; policy accept;
counter name tcp dport map { https : "https"}
tcp dport https counter
tcp dport https quota 25 mbytes
}
}

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: add average bytes per packet counter support
Liping Zhang [Sun, 25 Dec 2016 12:12:55 +0000 (20:12 +0800)] 
ct: add average bytes per packet counter support

Similar to connbytes extension in iptables, now you can use it to match
average bytes per packet a connection has transferred so far.

For example, match avgpkt in "BOTH" diretion:
  # nft add rule x y ct avgpkt \> 100

Match avgpkt in reply direction:
  # nft add rule x y ct reply avgpkt \< 900

Or match avgpkt in original direction:
  # nft add rule x y ct original avgpkt \> 200

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agobuild: add missing backslash to list of CFLAGS
Tobias Klauser [Fri, 13 Jan 2017 14:24:09 +0000 (15:24 +0100)] 
build: add missing backslash to list of CFLAGS

Due to a missing backslash in the AM_CFLAGS list some warning flags do
not get added to the generated default CLFAGS. Add the missing backslash
to include them as well.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add maps tests
Elise Lennion [Fri, 6 Jan 2017 21:44:57 +0000 (19:44 -0200)] 
tests: shell: add maps tests

Test adding elements to simple and interval maps.

Based on original work from Richard Mörbitz and Pablo Neira.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: sort set elements in netlink_get_setelems()
Elise Lennion [Fri, 6 Jan 2017 21:43:32 +0000 (19:43 -0200)] 
src: sort set elements in netlink_get_setelems()

So users can better track their ruleset via git.

Without sorting, the elements can be listed in a different order
every time the set is created, generating unnecessary git changes.

Mergesort is used. Doesn't sort sets with 'flags interval' set on.

Pablo appends to this changelog description:

Currently these interval set elements are dumped in order. We'll likely
get new representations soon that may not guarantee this anymore, so
let's revisit this later in case we need it.

Without this patch, nft list ruleset with a set containing 40000
elements takes on my laptop:

real    0m2.742s
user    0m0.112s
sys     0m0.280s

With this patch:

real    0m2.846s
user    0m0.180s
sys     0m0.284s

Difference is small, so don't get nft more complicated with yet another
getopt() option, enable this by default.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: cover cache flush on flush ruleset
Pablo Neira Ayuso [Wed, 4 Jan 2017 13:28:02 +0000 (14:28 +0100)] 
tests: shell: cover cache flush on flush ruleset

This tests covers 530a82a72d15 ("evaluate: Update cache on flush
ruleset"). Make sure loading twice including an upfront ruleset flush
leaves us with an empty cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agomnl: use nftnl_nlmsg_build_hdr()
Pablo Neira Ayuso [Wed, 4 Jan 2017 13:55:58 +0000 (14:55 +0100)] 
mnl: use nftnl_nlmsg_build_hdr()

Instead of nftnl_.*_nlmsg_build_hdr() since they rely on this generic
function. This also helps us clean up source code indentation around
this function call.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: remove obsolete tokens
Florian Westphal [Sun, 8 Jan 2017 22:08:39 +0000 (23:08 +0100)] 
parser_bison: remove obsolete tokens

removed in c992153402c78d91e8be ("ct: allow resolving ct keys at run time").

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agodatatype: printf format warnings on 32-bit build
Duncan Roe [Tue, 3 Jan 2017 11:53:02 +0000 (12:53 +0100)] 
datatype: printf format warnings on 32-bit build

This is %lu with uint64_t again.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1109
Signed-off-by: Duncan Roe <duncan_roe@acslink.net.au>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: fix search_in_include_path test
Anatole Denis [Mon, 2 Jan 2017 15:30:01 +0000 (16:30 +0100)] 
scanner: fix search_in_include_path test

clang emits a warning in this function as we're using a boolean as the third
argument to strncmp. Indeed, this function only checks the first byte of the
path as is, so files beginning with . will be incorrectly included from the
current working directory instead of the include directory.

Fixes: f92a1a5c4a87 ("scanner: honor absolute and relative paths via include file")
Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: support for stateful object monitoring
Pablo Neira Ayuso [Thu, 1 Dec 2016 19:12:22 +0000 (20:12 +0100)] 
src: support for stateful object monitoring

This patch extends the event monitoring infrastructure to catch events
of addition and removal of stateful objects.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add support for stateful object maps
Pablo Neira Ayuso [Sun, 27 Nov 2016 23:03:50 +0000 (00:03 +0100)] 
src: add support for stateful object maps

You can create these maps using explicit map declarations:

 # nft add table filter
 # nft add chain filter input { type filter hook input priority 0\; }
 # nft add map filter badguys { type ipv4_addr : counter \; }
 # nft add rule filter input counter name ip saddr map @badguys
 # nft add counter filter badguy1
 # nft add counter filter badguy2
 # nft add element filter badguys { 192.168.2.3 : "badguy1" }
 # nft add element filter badguys { 192.168.2.4 : "badguy2" }

Or through implicit map definitions:

 table ip filter {
        counter http-traffic {
                packets 8 bytes 672
        }

        chain input {
                type filter hook input priority 0; policy accept;
                counter name tcp dport map { 80 : "http-traffic", 443 : "http-traffic"}
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add stateful object reference expression
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:34:57 +0000 (23:34 +0100)] 
src: add stateful object reference expression

This patch adds a new objref statement to refer to existing stateful
objects from rules, eg.

 # nft add rule filter input counter name test counter

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>