]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
8 years agoparser_bison: allow RESET token from rhs
Pablo Neira Ayuso [Fri, 2 Dec 2016 10:23:29 +0000 (11:23 +0100)] 
parser_bison: allow RESET token from rhs

Stateful objects have introduced a RESET token in the parser. This breaks the
use of RESET from the rhs, to fix this:

1) I added a new rule to catch this case, thus, dccp doesn't break.
2) Update reject_opts so reject with tcp reset still works.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: reset internal stateful objects
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:42:54 +0000 (23:42 +0100)] 
src: reset internal stateful objects

This patch allows you to atomically dump and reset stateful objects, eg.

 # nft list counters
 table ip filter {
        counter test {
              packets 1024 bytes 100000
        }
 }
 # nft reset quotas table filter
 counter test {
        packets 1024 bytes 100000
 }
 # nft reset quotas table filter
 counter test {
        packets 0 bytes 0
 }

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

This patch allows you to add and to delete objects, eg.

 # nft add quota filter test 1234567 bytes
 # nft list quotas
 table ip filter {
        quota test {
                 1234567 bytes
        }

 }
 # nft delete quota filter test

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: listing of stateful objects
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:35:25 +0000 (23:35 +0100)] 
src: listing of stateful objects

This patch allows you to dump existing stateful objects, eg.

 # nft list ruleset
 table ip filter {
        counter test {
                packets 64 bytes 1268
        }

        quota test {
                over 1 mbytes used 1268 bytes
        }

        chain input {
                type filter hook input priority 0; policy accept;
                quota name test drop
                counter name test
        }
 }

 # nft list quotas
 table ip filter {
        quota test {
                over 1 mbytes used 1268 bytes
        }
 }
 # nft list counters
 table ip filter {
        counter test {
                packets 64 bytes 1268
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add used quota support
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:24:21 +0000 (23:24 +0100)] 
src: add used quota support

table ip x {
        chain y {
                type filter hook forward priority 0; policy accept;
                quota over 200 mbytes used 1143 kbytes drop
        }
}

This patch allows us to list and to restore used quota.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: remove SET_F_* flag definitions
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:35:22 +0000 (23:35 +0100)] 
src: remove SET_F_* flag definitions

They map exactly one to one to we have in the kernel headers, so use
kernel definitions instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: fetch nf_tables.h updates
Pablo Neira Ayuso [Sun, 27 Nov 2016 22:35:14 +0000 (23:35 +0100)] 
include: fetch nf_tables.h updates

Get header in sync with recent updates.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosegtree: allocate memory for arrays on heap
Oleksandr Natalenko [Mon, 2 Jan 2017 19:54:09 +0000 (20:54 +0100)] 
segtree: allocate memory for arrays on heap

Huge sets may cause stack to be exhausted.
So, put allocate memory for arrays in
interval_map_decompose() function on heap.

Signed-off-by: Oleksandr Natalenko <oleksandr@natalenko.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoutils: provide array allocation wrapper
Oleksandr Natalenko [Mon, 2 Jan 2017 19:54:08 +0000 (20:54 +0100)] 
utils: provide array allocation wrapper

This will be used for allocating memory for arrays
in heap instead of keeping them on stack.

Signed-off-by: Oleksandr Natalenko <oleksandr@natalenko.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoinclude: Missing noinst_HEADERS updates v0.7
Pablo Neira Ayuso [Tue, 20 Dec 2016 19:43:21 +0000 (20:43 +0100)] 
include: Missing noinst_HEADERS updates

From recent updates, otherwise make distcheck breaks.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoconfigure: Bump version to v0.7
Pablo Neira Ayuso [Mon, 19 Dec 2016 18:37:29 +0000 (19:37 +0100)] 
configure: Bump version to v0.7

Update libnftnl dependency up to latest (1.0.7). Update my copyright
statements. Dedicate this release to Scroodge McDuck [1].

[1] https://en.wikipedia.org/wiki/Scrooge_McDuck#/media/File:ScroogeFirst.jpg

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxt: use NFTNL_* definitions
Pablo Neira Ayuso [Tue, 20 Dec 2016 12:40:36 +0000 (13:40 +0100)] 
xt: use NFTNL_* definitions

Do not use obsolete definitions in libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agomnl: add mnl_nft_setelem_batch_flush() and use it from netlink_flush_setelems()
Pablo Neira Ayuso [Tue, 20 Dec 2016 00:14:10 +0000 (01:14 +0100)] 
mnl: add mnl_nft_setelem_batch_flush() and use it from netlink_flush_setelems()

Commit 8bd99f2fca7e ("mnl: don't send empty set elements netlink message
to kernel") broke set flush because we still need to send the netlink
message with no elements to flush sets.

To avoid more whack-a-mole games, add a new explicit function
mnl_nft_setelem_batch_flush() that is used to request a set flush,
instead of reusing the one that allows us to explicitly delete given set
elements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoexpression: Show the base which pre-defined constants are displayed
Elise Lennion [Mon, 19 Dec 2016 22:11:04 +0000 (19:11 -0300)] 
expression: Show the base which pre-defined constants are displayed

so the user know how we express it.

The base was added to all symbol tables, which are associated with
datatype->sym_tbl, so they are displayed in the right base.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink_linearize: fix IPv6 layer 4 checksum mangling
Pablo Neira Ayuso [Wed, 14 Dec 2016 00:12:43 +0000 (01:12 +0100)] 
netlink_linearize: fix IPv6 layer 4 checksum mangling

In IPv6 there is no checksum field. We always have to trigger layer 4
checksum mangling if any of the layer 3 pseudoheader fields are updated.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: update quota and payload
Pablo Neira Ayuso [Tue, 13 Dec 2016 23:26:16 +0000 (00:26 +0100)] 
tests: py: update quota and payload

Include new consumed field for quota, and check for csum_flags in payload.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agomnl: don't send empty set elements netlink message to kernel
Pablo Neira Ayuso [Tue, 13 Dec 2016 22:51:33 +0000 (23:51 +0100)] 
mnl: don't send empty set elements netlink message to kernel

The following command:

 # nft --debug=mnl add rule x y flow table xyz { ip saddr timeout 30s counter }

breaks with EINVAL. The following netlink message is causing the
problem:

...
----------------        ------------------
|  0000000044  |        | message length |
| 02572 | R--- |        |  type | flags  |
|  0000000004  |        | sequence number|
|  0000000000  |        |     port ID    |
----------------        ------------------
| 02 00 00 00  |        |  extra header  |
|00008|--|00002|        |len |flags| type|
| 78 79 7a 00  |        |      data      |       x y z
|00008|--|00004|        |len |flags| type|
| 00 00 00 01  |        |      data      |
|00006|--|00001|        |len |flags| type|
| 78 00 00 00  |        |      data      |       x
----------------        ------------------
...

This is incorrect since this describes no elements at all, so it is
useless.  Add upfront check before iterating over the list of set
elements so the netlink message is not placed in the batch.

This patch also adds a set so flow tables are minimally covered.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosegtree: don't trigger error on exact overlaps
Pablo Neira Ayuso [Tue, 13 Dec 2016 00:34:15 +0000 (01:34 +0100)] 
segtree: don't trigger error on exact overlaps

So adding the same element doesn't trigger any error:

 # nft add element filter bogons { 3.3.3.123/24 }
 # nft add element filter bogons { 3.3.3.123/24 }

Still kernel reports an error if we use create instead:

 # nft create element filter bogons { 3.3.3.123/24 }
 <cmdline>:1:1-46: Error: Could not process rule: File exists
 create element filter bogons { 3.3.3.123/24 }
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosegtree: wrong prefix expression length on interval_map_decompose()
Pablo Neira Ayuso [Tue, 13 Dec 2016 00:17:52 +0000 (01:17 +0100)] 
segtree: wrong prefix expression length on interval_map_decompose()

interval_map_decompose() sets expr->len to zero. This causes problems
from expr_to_intervals() that calls range_expr_value_high() and
calculates:

 expr->len - expr->prefix_len

this operation underflows, then mpz_init_bitmask() allocates a huge
bitmask.

Use expr_value(i)->len given that we already use this to calculate the
prefix length.

Reported-by: Richard Mörbitz <richard.moerbitz@tu-dresden.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: Display pre-defined inet_service values in decimal base
Elise Lennion [Sat, 10 Dec 2016 23:59:36 +0000 (21:59 -0200)] 
datatype: Display pre-defined inet_service values in decimal base

because the convention is to represent ports in base 10.

gcc-workaround is no longer needed and was removed.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: Display pre-defined inet_service values in host byte order
Elise Lennion [Sat, 10 Dec 2016 00:35:13 +0000 (22:35 -0200)] 
datatype: Display pre-defined inet_service values in host byte order

nft describe displays, to the user, which values are available for a selector,
then the values should be in host byte order.

Variable size was replaced by len to better match the common pattern.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Fixes: ccc5da470e76 ("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 agosrc: add support to flush sets
Pablo Neira Ayuso [Mon, 5 Dec 2016 21:45:22 +0000 (22:45 +0100)] 
src: add support to flush sets

You can use this new command to remove all existing elements in a set:

 # nft flush set filter xyz

After this command, the set 'xyz' in table 'filter' becomes empty.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: trigger layer 4 checksum when pseudoheader fields are modified
Pablo Neira [Thu, 24 Nov 2016 11:12:33 +0000 (12:12 +0100)] 
src: trigger layer 4 checksum when pseudoheader fields are modified

This patch sets the NFT_PAYLOAD_L4CSUM_PSEUDOHDR when any of the
pseudoheader fields are modified. This implicitly enables stateless NAT,
that can be useful under some circuntances.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add test case for inserting element into verdict map
Liping Zhang [Tue, 8 Nov 2016 15:06:37 +0000 (23:06 +0800)] 
tests: shell: add test case for inserting element into verdict map

"dalegaard@gmail.com" reports that when inserting an element into a
verdict map, kernel crash will happen. Now add this test case so we
can avoid future regressions fail.

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Acked-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: return ctx->table from table_lookup_global()
Pablo Neira Ayuso [Thu, 1 Dec 2016 10:32:58 +0000 (11:32 +0100)] 
evaluate: return ctx->table from table_lookup_global()

Instead of returning ctx->cmd->table. Note that ctx->cmd->table and
ctx->table points to the same object when all commands are embedded into
the table definition. But this is not true if we mix table definitions
with linear list commands in one file that we load via nft -f.

Reported-by: Martin Bednar <martin@serafean.cz>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Update cache on flush ruleset
Anatole Denis [Thu, 1 Dec 2016 10:50:17 +0000 (11:50 +0100)] 
evaluate: Update cache on flush ruleset

After a flush, the cache should be empty, otherwise the cache and the expected
state are desynced, causing unwarranted errors. See
tests/shell/testcases/cache/0002_interval_0.

`flush table` and `flush chain` don't empty sets or destroy chains, so the cache
does not need an update in those cases, since only chain names and set contents
are held in cache for commands other than "list"

Reported-by: Leon Merten Lohse <leon@green-side.de>
Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agorule: Introduce helper function cache_flush
Anatole Denis [Thu, 1 Dec 2016 10:50:16 +0000 (11:50 +0100)] 
rule: Introduce helper function cache_flush

cache_release empties the cache, and marks it as uninitialized. Add cache_flush,
which does the same, except it keeps the cache initialized, eg. after a "nft
flush ruleset" when empty is the correct state of the cache.

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: honor -nn option from inet_service_type_print()
Pablo Neira Ayuso [Wed, 30 Nov 2016 22:03:46 +0000 (23:03 +0100)] 
datatype: honor -nn option from inet_service_type_print()

If -nn is passed, we have to display ports in numbers, not as a symbol.

Fixes: ccc5da470e76 ("datatype: Replace getnameinfo() by internal lookup table")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: Test TCP flags match with parentheses
Phil Sutter [Wed, 30 Nov 2016 21:07:14 +0000 (22:07 +0100)] 
tests: py: Test TCP flags match with parentheses

This should test the fix in commit 7222680eb328b ("parser_bison: Allow
parens on RHS of relational_expr").

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agobison: remove old log level tokens
Florian Westphal [Wed, 30 Nov 2016 19:33:14 +0000 (20:33 +0100)] 
bison: remove old log level tokens

not used anymore since 0423caa91ad26e7 ("src: don't need keyword for log level")

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodatatype: Replace getnameinfo() by internal lookup table
Elise Lennion [Wed, 30 Nov 2016 01:12:37 +0000 (23:12 -0200)] 
datatype: Replace getnameinfo() by internal lookup table

To avoid exceeding the inputs number limit of the flex scanner used,
when calling getnameinfo() in inet_service_type_print().

The new symbol_table was associated with inet_service_type, to enable
listing all pre-defined services using nft command line tool.

The listed services are all well-known and registered ports of my
local /etc/services file, from Ubuntu 16.04. Service numbers are
converted to respect network byte order.

Signed-off-by: Elise Lennion <elise.lennion@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: Allow parens on RHS of relational_expr
Phil Sutter [Mon, 28 Nov 2016 17:51:43 +0000 (18:51 +0100)] 
parser_bison: Allow parens on RHS of relational_expr

This is useful to allow a construct such as:

| tcp flags & (syn|fin) == (syn|fin)

Before, only the parentheses on the left side were allowed, but via a
quite funny path through the parser:

* expr might be a concat_expr
* concat_expr might be a basic_expr
* basic_expr is an inclusive_or_expr
* inclusive_or_expr might be an exclusive_or_expr
* exclusive_or_expr might be an and_expr
* and_expr might be 'and_expr AMPERSAND shift_expr'
  -> here we eliminate 'flags &' in above statement
* shift_expr might be a primary_expr
* primary_expr might be '( basic_expr )'

Commit a3e60492a684b ("parser: restrict relational rhs expression
recursion") introduced rhs_expr to disallow recursion on RHS, so just
reverting that change for relational_expr is a no go. Allowing rhs_expr
to be '( rhs_expr )' though seems way too intrusive to me since it's
being used in all kinds of places, so this patch is the safest way to
allow the above I could come up with.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add testcase for different defines usage
Arturo Borrero Gonzalez [Mon, 28 Nov 2016 12:59:28 +0000 (13:59 +0100)] 
tests: shell: add testcase for different defines usage

This testcase add some defines in a nft -f run and then uses
them in different spots (which are not covered in previous testcases).

 * defines used to define another one
 * different datatypes (numbers, strings, bits, ranges)
 * usage in sets, maps, contatenatios
 * single rules with single statements, multiple statements
 * reuse define in same rule

Perhaps this isn't testing many different code path, but I find this
interesting to have given it will probably be one of the most common
use cases of nftables.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add a testcase for many defines
Arturo Borrero Gonzalez [Mon, 28 Nov 2016 12:59:22 +0000 (13:59 +0100)] 
tests: shell: add a testcase for many defines

Use many defines in a single nft -f run.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests/py: Unmask negative set lookup
Anatole Denis [Thu, 24 Nov 2016 14:16:21 +0000 (15:16 +0100)] 
tests/py: Unmask negative set lookup

Many testcases were masked because of bug #888. This series of patches unmasks
them

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Interpret OP_NEQ against a set as OP_LOOKUP
Anatole Denis [Thu, 24 Nov 2016 14:16:20 +0000 (15:16 +0100)] 
src: Interpret OP_NEQ against a set as OP_LOOKUP

Now that the support for inverted matching is in the kernel and in libnftnl, add
it to nftables too.

This fixes bug #888

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoRevert "evaluate: check for NULL datatype in rhs in lookup expr"
Anatole Denis [Mon, 28 Nov 2016 16:43:10 +0000 (17:43 +0100)] 
Revert "evaluate: check for NULL datatype in rhs in lookup expr"

This reverts commit 5afa5a164ff1c066af1ec56d875b91562882bd50.
This commit is obsoleted by removing the possibility for a NULL right->dtype in
the first place, at set declaration.

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: Add regression test for malformed sets
Anatole Denis [Mon, 28 Nov 2016 16:43:09 +0000 (17:43 +0100)] 
tests: Add regression test for malformed sets

see: 5afa5a164ff1c066af1ec56d875b91562882bd50
When a malformed set is added, it was added before erroring out, causing a
segfault further down when used. This tests for this case, ensuring that
nftables doesn't segfault but errors correctly

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Add set to cache only when well-formed
Anatole Denis [Mon, 28 Nov 2016 16:43:08 +0000 (17:43 +0100)] 
evaluate: Add set to cache only when well-formed

When creating a set (in set_evaluate), it is added to the table cache before
being checked for correctness. When the set is ill-formed, the function returns
without removing the (non-existent, since the function returned) set. Further
references to this set will not result in an error (since the set is in the
lookup table), but the malformed set will probably cause a segfault.

The symptom (the segfault) was fixed by checking for NULL when evaluating a
reference to the set (commit 5afa5a164ff1c066af1ec56d875b91562882bd50), this
should fix the root cause.

Signed-off-by: Anatole Denis <anatole@rezel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add log flags syntax support
Liping Zhang [Sat, 19 Nov 2016 11:31:15 +0000 (19:31 +0800)] 
src: add log flags syntax support

Now NF_LOG_XXX is exposed to the userspace, we can set it explicitly.
Like iptables LOG target, we can log TCP sequence numbers, TCP options,
IP options, UID owning local socket and decode MAC header. Note the
log flags are mutually exclusive with group.

Some examples are listed below:
 # nft add rule t c log flags tcp sequence,options
 # nft add rule t c log flags ip options
 # nft add rule t c log flags skuid
 # nft add rule t c log flags ether
 # nft add rule t c log flags all
 # nft add rule t c log flags all group 1
 <cmdline>:1:14-16: Error: flags and group are mutually exclusive
 add rule t c log flags all group 1
              ^^^

Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: another testcase for deleting many set elements
Arturo Borrero Gonzalez [Wed, 16 Nov 2016 12:53:26 +0000 (13:53 +0100)] 
tests: shell: another testcase for deleting many set elements

This testcase adds and deletes many elements in a set.

We add and delete 65.536 elements in two different nft -f runs.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: testcase for deleting many set elements
Arturo Borrero Gonzalez [Wed, 16 Nov 2016 12:53:21 +0000 (13:53 +0100)] 
tests: shell: testcase for deleting many set elements

This testcase adds and deletes many elements in a set.

We add and delete 65.536 elements in a same batch of netlink messages,
(single nft -f run).

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: testcase for adding many set elements
Arturo Borrero Gonzalez [Wed, 16 Nov 2016 12:53:15 +0000 (13:53 +0100)] 
tests: shell: testcase for adding many set elements

This testcase adds many elements in a set.
We add 65.356 elements.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: allow to execute a single testcase
Arturo Borrero Gonzalez [Wed, 16 Nov 2016 12:51:52 +0000 (13:51 +0100)] 
tests: shell: allow to execute a single testcase

Using, for example:
 % sudo ./run-tests.sh testcase/mytest_0

Will result in an execution of this single testcase rather than
the complete suite.

This is useful while working with a concrete testcase.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: refresh nf_tables.h header
Pablo Neira Ayuso [Thu, 24 Nov 2016 11:09:23 +0000 (12:09 +0100)] 
include: refresh nf_tables.h header

Fetch what we have at 4.9-rc6.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoRevert "tests: py: nft-tests.py: Add function for loading and removing kernel modules"
Florian Westphal [Thu, 17 Nov 2016 16:43:11 +0000 (17:43 +0100)] 
Revert "tests: py: nft-tests.py: Add function for loading and removing kernel modules"

Its not needed at the moment, all tests that need a network interface
(meta iif/oif) use the lo interface.

For iifname/oifname the network device doesn't have to exist.

We can revisit this in case we need it in the future.

(seems commit causes issues on kernels built without dummy module).

Reported-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Manuel Messner <mm@skelett.io>
8 years agomnl: use nftnl_set_elems_nlmsg_build_payload_iter() when deleting elements
Pablo Neira Ayuso [Mon, 14 Nov 2016 21:41:26 +0000 (22:41 +0100)] 
mnl: use nftnl_set_elems_nlmsg_build_payload_iter() when deleting elements

Otherwise, nft crashes when deleting a very large number of elements.

*** stack smashing detected ***: nft terminated
Segmentation fault

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add notrack support
Pablo Neira Ayuso [Mon, 14 Nov 2016 21:19:07 +0000 (22:19 +0100)] 
src: add notrack support

This patch adds the notrack statement, to skip connection tracking for
certain packets.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: missing range conversion in icmpv6
Pablo Neira Ayuso [Mon, 14 Nov 2016 20:50:33 +0000 (21:50 +0100)] 
tests: py: missing range conversion in icmpv6

Use range expression to negate ranges.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: add some testcases for log flags
Pablo Neira Ayuso [Mon, 14 Nov 2016 20:44:33 +0000 (21:44 +0100)] 
tests: py: add some testcases for log flags

Remove null and zero flags from tests, to reduce the noise when running
tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add testcases for comments in set elements
Arturo Borrero Gonzalez [Wed, 9 Nov 2016 09:38:26 +0000 (10:38 +0100)] 
tests: shell: add testcases for comments in set elements

This patch adds a several testcases for comments in set elements.
This includes the netfilter bug #1090 about comments in set interval elements.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosegtree: keep element comments in set intervals
Pablo Neira Ayuso [Tue, 8 Nov 2016 23:19:45 +0000 (00:19 +0100)] 
segtree: keep element comments in set intervals

The conversion from the set element range representation to element
intervals doesn't keep the comment information around.

Fixes: https://bugzilla.netfilter.org/show_bug.cgi?id=1090
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Tested-by: Arturo Borrero Gonzalez <arturo@debian.org>
8 years agosrc: add offset attribute for hash expression
Laura Garcia Liebana [Fri, 4 Nov 2016 13:01:12 +0000 (14:01 +0100)] 
src: add offset attribute for hash expression

Add support to add an offset to the hash generator, eg.

 ct mark set hash ip saddr mod 10 offset 100

This will generate marks with series between 100-109.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: make hash seed attribute optional
Laura Garcia Liebana [Fri, 4 Nov 2016 12:59:31 +0000 (13:59 +0100)] 
src: make hash seed attribute optional

The hash expression requires a seed attribute to call the jhash
operation, eg.

 # nft add rule x y meta mark set jhash ip saddr . ip daddr mod 2 \
seed 0xdeadbeef

With this patch the seed attribute is optional and it's generated by a
random function from userspace, eg.

 # nft add rule x y meta mark set jhash ip saddr . ip daddr mod 2

The kernel will take care of generate a random seed.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add a new testcase for ruleset loading bug
Arturo Borrero Gonzalez [Thu, 3 Nov 2016 12:04:47 +0000 (13:04 +0100)] 
tests: shell: add a new testcase for ruleset loading bug

There seems to be a bug that prevent loading a ruleset twice in a row
if the ruleset contains sets with intervals. This seems related to the
nft cache.

By the time of this commit, the bug is not fixed yet.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: introduce the cache testcases directory
Arturo Borrero Gonzalez [Thu, 3 Nov 2016 12:04:42 +0000 (13:04 +0100)] 
tests: shell: introduce the cache testcases directory

This directory is for testcases related to the nft cache.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: delete useless stderr output in testcase
Arturo Borrero Gonzalez [Thu, 3 Nov 2016 12:04:36 +0000 (13:04 +0100)] 
tests: shell: delete useless stderr output in testcase

This stderr output is expected.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoxt: update Arturo Borrero Gonzalez email address
Arturo Borrero Gonzalez [Tue, 18 Oct 2016 08:32:21 +0000 (10:32 +0200)] 
xt: update Arturo Borrero Gonzalez email address

Update email address to a new one in the copyright notice.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink_linearize: skip set element expression in flow table key
Pablo Neira Ayuso [Mon, 31 Oct 2016 13:29:58 +0000 (14:29 +0100)] 
netlink_linearize: skip set element expression in flow table key

Anders reports that:

 # nft add rule ip6 filter postrouting \
flow table acct_out \{ meta iif . ip6 saddr timeout 600s counter \}

while the opposite doesn't work:

 # nft add rule ip6 filter postrouting \
flow table acct_out \{ ip6 saddr . meta iif timeout 600s counter \}

netlink_gen_flow_stmt() relies on the flow table key, that is expressed
as a set element. Use the set element key instead to skip the set
element wrap, otherwise get_register() abort execution:

 nft: netlink_linearize.c:650: netlink_gen_expr: Assertion `dreg < ctx->reg_low' failed.

Reported-by: Anders K. Pedersen <akp@cohaesio.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agodoc: fix synopsis for ct expression
Anders K. Pedersen [Fri, 28 Oct 2016 19:25:30 +0000 (19:25 +0000)] 
doc: fix synopsis for ct expression

The nft man page command synopsis for the ct expression is currently:

       ct {state | direction | status | mark | expiration | helper | label |
          bytes | packets} {original | reply | {l3proto | protocol | saddr |
          daddr | proto-src | proto-dst | bytes | packets}}

which is not correct for the arguments that should be prefixed with
"original" or "reply". Change this to

       ct {state | direction | status | mark | expiration | helper | label |
          l3proto | protocol | bytes | packets}
       ct {original | reply} {l3proto | protocol | saddr | daddr | proto-src |
          proto-dst | bytes | packets}

Signed-off-by: Anders K. Pedersen <akp@cohaesio.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Allow concatenation of rt nexthop etc.
Anders K. Pedersen [Sat, 29 Oct 2016 09:49:09 +0000 (09:49 +0000)] 
evaluate: Allow concatenation of rt nexthop etc.

Concatenations of rt nexthop or ct {orignal | reply} {saddr | daddr} fail
due to

 # nft add rule ip filter postrouting flow table acct \{ ip saddr . rt nexthop counter \}
 <cmdline>:1:61-70: Error: can not use variable sized data types (invalid) in concat expressions
 add rule ip filter postrouting flow table acct { ip saddr . rt nexthop counter }
                                                  ~~~~~~~~~~~^^^^^^^^^^

Fix this by reordering the check for variable size data types in
expr_evaluate_concat() to happen after expr_evaluate() has been called (via
list_member_evaluate()) for the sub expression. This allows
expr_evaluate_[cr]t() to call [cr]t_expr_update_type() and set the data type
before the check.

Signed-off-by: Anders K. Pedersen <akp@cohaesio.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests/py: add missing payload test for numgen offset
Pablo Neira Ayuso [Mon, 31 Oct 2016 12:33:34 +0000 (13:33 +0100)] 
tests/py: add missing payload test for numgen offset

The new cover test for:

ct mark set numgen inc mod 2 offset 100

was lacking the payload file chunk.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agolog: rename the log level "warning" to "warn"
Liping Zhang [Sun, 30 Oct 2016 12:24:27 +0000 (20:24 +0800)] 
log: rename the log level "warning" to "warn"

This is to keep compatibility. The original keyword in grammer is
"warn" instead of "warning".

Fixes: 0423caa91ad2 ("src: don't need keyword for log level")
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: fix "ct l3proto/protocol" syntax broken
Liping Zhang [Sun, 30 Oct 2016 12:36:14 +0000 (20:36 +0800)] 
ct: fix "ct l3proto/protocol" syntax broken

"l3proto" and "protocol" are still keywords in our grammer, they are not
STRING, so if the user input the following rule, nft will complain that
the syntax is error:
  # nft add t c ct l3proto ipv4
  <cmdline>:1:12-18: Error: syntax error, unexpected l3proto, expecting
  string or mark or packets or bytes
  add t c ct l3proto ipv4
             ^^^^^^^

Fixes: c992153402c7 ("ct: allow resolving ct keys at run time")
Signed-off-by: Liping Zhang <zlpnobody@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoCorrect description of -n/--numeric option
Jon Jensen [Sat, 29 Oct 2016 04:18:11 +0000 (22:18 -0600)] 
Correct description of -n/--numeric option

"When used twice" was used twice in the manpage. :)

And as Florian Westphal pointed out, it was also incorrect
for the -nn case.

Update the manpage and built-in help in main.c to match actual behavior.

Signed-off-by: Jon Jensen <jon@endpoint.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoReplace tests/files/expr-rt with Python based tests, and replace ether type
Anders K. Pedersen [Sat, 29 Oct 2016 01:12:41 +0000 (03:12 +0200)] 
Replace tests/files/expr-rt with Python based tests, and replace ether type
with meta nfproto, which generates a bit fewer instructions.

Signed-off-by: Anders K. Pedersen <akp@cohaesio.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agosrc: add fib expression
Florian Westphal [Thu, 15 Sep 2016 15:28:00 +0000 (17:28 +0200)] 
src: add fib expression

This adds the 'fib' expression which can be used to
obtain the output interface from the route table based on either
source or destination address of a packet.

This can be used to e.g. add reverse path filtering:

 # drop if not coming from the same interface packet
 # arrived on
 # nft add rule x prerouting fib saddr . iif oif eq 0 drop

 # accept only if from eth0
 # nft add rule x prerouting fib saddr . iif oif eq "eth0" accept

 # accept if from any valid interface
 # nft add rule x prerouting fib saddr oif accept

Querying of address type is also supported.  This can be used
to e.g. only accept packets to addresses configured in the same
interface:
 # fib daddr . iif type local

Its also possible to use mark and verdict map, e.g.:
 # nft add rule x prerouting meta mark set 0xdead fib daddr . mark type vmap {
   blackhole : drop,
   prohibit : drop,
   unicast : accept
 }

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agort: introduce routing expression
Anders K. Pedersen [Fri, 28 Oct 2016 05:56:32 +0000 (05:56 +0000)] 
rt: introduce routing expression

Introduce rt expression for routing related data with support for nexthop
(i.e. the directly connected IP address that an outgoing packet is sent
to), which can be used either for matching or accounting, eg.

 # nft add rule filter postrouting \
ip daddr 192.168.1.0/24 rt nexthop != 192.168.0.1 drop

This will drop any traffic to 192.168.1.0/24 that is not routed via
192.168.0.1.

 # nft add rule filter postrouting \
flow table acct { rt nexthop timeout 600s counter }
 # nft add rule ip6 filter postrouting \
flow table acct { rt nexthop timeout 600s counter }

These rules count outgoing traffic per nexthop. Note that the timeout
releases an entry if no traffic is seen for this nexthop within 10 minutes.

 # nft add rule inet filter postrouting \
ether type ip \
flow table acct { rt nexthop timeout 600s counter }
 # nft add rule inet filter postrouting \
ether type ip6 \
flow table acct { rt nexthop timeout 600s counter }

Same as above, but via the inet family, where the ether type must be
specified explicitly.

"rt classid" is also implemented identical to "meta rtclassid", since it
is more logical to have this match in the routing expression going forward.

Signed-off-by: Anders K. Pedersen <akp@cohaesio.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agometa: allow resolving meta keys at run time
Florian Westphal [Wed, 27 Jul 2016 07:42:38 +0000 (09:42 +0200)] 
meta: allow resolving meta keys at run time

use the meta template to translate the textual token to the enum value.
This allows to remove two keywords from the scanner and also means we do
not need to introduce new keywords when more meta keys get added.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: allow resolving ct keys at run time
Florian Westphal [Wed, 27 Jul 2016 12:34:53 +0000 (14:34 +0200)] 
ct: allow resolving ct keys at run time

... and remove those keywords we no longer need.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoutils: provide snprintf helper macro
Florian Westphal [Wed, 27 Jul 2016 13:22:25 +0000 (15:22 +0200)] 
utils: provide snprintf helper macro

lifted from libnftnl, except that we will abort on snprintf errors.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser: add offset keyword and parser rule
Pablo Neira Ayuso [Thu, 27 Oct 2016 20:11:40 +0000 (22:11 +0200)] 
parser: add offset keyword and parser rule

This is required by the numgen and jhash expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: fix linearize numgen type
Laura Garcia Liebana [Sat, 22 Oct 2016 21:36:47 +0000 (23:36 +0200)] 
netlink: fix linearize numgen type

Avoid to treat numgen type attribute as a register.

Fixes: 345236211715 ("src: add hash expression")
Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add offset attribute for numgen expression
Laura Garcia Liebana [Sat, 22 Oct 2016 21:36:07 +0000 (23:36 +0200)] 
src: add offset attribute for numgen expression

Add support to add an offset to the numgen generated value.

Example:

 ct mark set numgen inc mod 2 offset 100

This will generate marks with serie like 100, 101, 100, ...

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agometa: fix pkttype name and add 'other' symbol
Florian Westphal [Thu, 27 Oct 2016 12:31:34 +0000 (14:31 +0200)] 
meta: fix pkttype name and add 'other' symbol

'unicast' doesn't check for unicast packets; it checks for PACKET_HOST,
i.e. a packet coming in for this host.

A unicast address to some other machine (e.g. because nic is
in promisc mode) will have PACKET_OTHER.

So at best this is misleading, so this patch changes it
to 'host'.  The unicast entry is retained for compat purpose.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: don't need keyword for log level
Pablo Neira Ayuso [Fri, 21 Oct 2016 11:06:12 +0000 (12:06 +0100)] 
src: don't need keyword for log level

We can handle log levels without keywords in our grammar, use string
instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: update kernel modules to clean
Arturo Borrero Gonzalez [Tue, 18 Oct 2016 08:49:34 +0000 (10:49 +0200)] 
tests: shell: update kernel modules to clean

Let's keep the kernel_cleanup() function updated with latest
kernel changes:

 * added nft_quota, nft_queue, nft_numgen, nft_range
 * rename nft_hash to nft_set_hash
 * keep nft_hash as well
 * rename nft_rbtree to nft_set_rbtree

The idea is to run each test in a clean evironment.

Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: allow to use variable to add/create/delete elements
Pablo Neira Ayuso [Fri, 21 Oct 2016 10:17:55 +0000 (11:17 +0100)] 
parser_bison: allow to use variable to add/create/delete elements

Using variable definitions from element command doesn't work, eg.

-test.nft-
 define whitelist_v4 = { 1.1.1.1 }
 table inet filter {
set whitelist_v4 { type ipv4_addr; }
 }
 add element inet filter whitelist_v4 $whitelist_v4
-EOF-

 # nft -f test.nft
 test.nft:7:38-38: Error: syntax error, unexpected '$', expecting '{'
 add element inet filter whitelist_v4 $whitelist_v4
                                      ^

Fix this by using set_block_expr rule for every element command.
This patch also comes with a new regression test.

Reported-by: Leon Merten Lohse <leon@green-side.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: fix monitor trace crash with netdev family
Florian Westphal [Thu, 20 Oct 2016 23:10:34 +0000 (01:10 +0200)] 
netlink: fix monitor trace crash with netdev family

nft monitor trace crashes on first packet with:

table netdev filter {
  chain foobar {
    type filter hook ingress device eth0 priority 0;
    udp sport 53 meta nftrace set 1
  }
}

We did not handle netdev family and thus generated bogus payload
statements without data types.

Netfilter Bugzilla: http://bugzilla.netfilter.org/show_bug.cgi?id=1092
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agosrc: support ct l3proto/protocol without direction syntax
Liping Zhang [Thu, 22 Sep 2016 14:34:52 +0000 (22:34 +0800)] 
src: support ct l3proto/protocol without direction syntax

Acctually, ct l3proto and ct protocol are unrelated to direction, so
it's unnecessary that we must specify dir if we want to use them.

Now add support that we can match ct l3proto/protocol without direction:
  # nft add rule filter input ct l3proto ipv4
  # nft add rule filter output ct protocol 17

Note: existing syntax is still preserved, so "ct reply l3proto ipv6"
is still fine.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: use new range expression for != [a,b] intervals
Pablo Neira Ayuso [Tue, 20 Sep 2016 17:25:25 +0000 (19:25 +0200)] 
src: use new range expression for != [a,b] intervals

Use new range expression in the kernel to fix wrong bytecode generation.
This patch also adjust tests so we don't hit problems there.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agometa: permit numeric interface type
Florian Westphal [Sun, 16 Oct 2016 20:08:25 +0000 (22:08 +0200)] 
meta: permit numeric interface type

If we can't translate an interface index back to a name
we just print the number.

This change allows using a number instead of an interface index to
make this symmetric.

If we can't find an interface with the given name check
if its a numeric string and then use it instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>