]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
9 years agopayload: fix stacked headers protocol context tracking
Patrick McHardy [Sun, 24 Apr 2016 10:22:30 +0000 (11:22 +0100)] 
payload: fix stacked headers protocol context tracking

The code contains multiple scattered around fragments to fiddle with the
protocol contexts to work around the fact that stacked headers update the
context for the incorrect layer.

Fix this by updating the correct layer in payload_expr_pctx_update() and
also take care of offset adjustments there and only there. Remove all
manual protocol context fiddling and change protocol context debugging to
also print the offset for stacked headers.

All previously successful testcases pass.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agopayload: only merge if adjacent and combined size fits into a register
Florian Westphal [Fri, 15 Apr 2016 13:09:39 +0000 (15:09 +0200)] 
payload: only merge if adjacent and combined size fits into a register

add rule ip6 filter input ip6 saddr ::1/128 ip6 daddr ::1/128 fails,
we ask to compare a 32byte immediate which is not supported:

  [ payload load 32b @ network header + 8 => reg 1 ]
  [ cmp eq reg 1 0x00000000 0x00000000 0x00000000 0x01000000 0x00000000 0x00000000 0x00000000 0x02000000 ]

We would need to use two cmps in this case, i.e.:

  [ payload load 32b @ network header + 8 => reg 1 ]
  [ cmp eq reg 1 0x00000000 0x00000000 0x00000000 0x01000000 ]
  [ cmp eq reg 2 0x00000000 0x00000000 0x00000000 0x02000000 ]

Seems however that this requires a bit more changes to how nft
handles register allocations, we'd also need to undo the constant merge.

Lets disable merging for now so that we generate

  [ payload load 16b @ network header + 8 => reg 1 ]
  [ cmp eq reg 1 0x00000000 0x00000000 0x00000000 0x01000000 ]
  [ payload load 16b @ network header + 24 => reg 1 ]
  [ cmp eq reg 1 0x00000000 0x00000000 0x00000000 0x02000000 ]

... if merge would bring us over the 128 bit register size.

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=1032
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: delete tempfile failover in testcases
Arturo Borrero [Thu, 14 Apr 2016 19:47:05 +0000 (21:47 +0200)] 
tests/shell: delete tempfile failover in testcases

It seems both Debian/Fedora (and derivates) contains mktemp (from the coreutils
package) so it makes no sense to have this failover, which looks buggy also.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add testcases for Netfilter bug #965
Arturo Borrero [Thu, 14 Apr 2016 07:58:56 +0000 (09:58 +0200)] 
tests/shell: add testcases for Netfilter bug #965

Testscases for Netfilter bug #965:
 * add rule at position
 * insert rule at position
 * replace rule with given handle
 * delete rule with given handle
 * don't allow to delete rules with position keyword

Netfilter Bugzilla: http://bugzilla.netfilter.org/show_bug.cgi?id=965
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: Use libnftnl user data TLV infrastructure
Carlos Falgueras García [Tue, 22 Mar 2016 19:46:27 +0000 (20:46 +0100)] 
rule: Use libnftnl user data TLV infrastructure

Now it is possible to store multiple variable length user data into rule.
Modify the parser in order to fill the nftnl_udata with the comment, and
the print function for extract these commentary and print it to user.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotest: shell: also unload NAT modules
Arturo Borrero [Fri, 8 Apr 2016 08:47:15 +0000 (10:47 +0200)] 
test: shell: also unload NAT modules

Also unload NAT modules between tests.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: evaluate: Show error for fanout without balance
Shivani Bhardwaj [Thu, 7 Apr 2016 17:28:54 +0000 (22:58 +0530)] 
src: evaluate: Show error for fanout without balance

The idea of fanout option is to improve the performance by indexing CPU
ID to map packets to the queues. This is used for load balancing.
Fanout option is not required when there is a single queue specified.

According to iptables, queue balance should be specified in order to use
fanout. Following that, throw an error in nftables if the range of
queues for load balancing is not specified with the fanout option.

After this patch,

$ sudo nft add rule ip filter forward counter queue num 0 fanout
<cmdline>:1:46-46: Error: fanout requires a range to be specified
add rule ip filter forward counter queue num 0 fanout
                                         ^^^^^

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: improve rule management checks
Arturo Borrero [Tue, 12 Apr 2016 09:44:17 +0000 (11:44 +0200)] 
evaluate: improve rule management checks

Improve checks (and error reporting) for basic rule management operations.
This includes a fix for netfilter bug #965.

Netfilter bug: http://bugzilla.netfilter.org/show_bug.cgi?id=965
Reported-by: Jesper Sander Lindgren <sander.contrib@gmail.com>
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add some tests for network namespaces
Arturo Borrero [Wed, 6 Apr 2016 11:00:10 +0000 (13:00 +0200)] 
tests/shell: add some tests for network namespaces

A basic tests to check we can perform operations in different network
namespaces.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add new testcases for commit/rollback
Arturo Borrero [Thu, 31 Mar 2016 08:01:12 +0000 (10:01 +0200)] 
tests/shell: add new testcases for commit/rollback

New simple testcases for kernel commit/rollback operations.

* ruleset A is loaded (good ruleset)
* ruleset B is loaded (bad ruleset): fail is expected
* ruleset A should remain in the kernel

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: store parser location for handle and position specifiers
Pablo Neira Ayuso [Wed, 30 Mar 2016 11:25:10 +0000 (13:25 +0200)] 
src: store parser location for handle and position specifiers

Store the parser location structure for handle and position IDs so we
can use this information from the evaluation step, to provide better
error reporting.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agorule: don't print trailing statement whitespace
Arturo Borrero [Wed, 23 Mar 2016 12:51:33 +0000 (13:51 +0100)] 
rule: don't print trailing statement whitespace

This trailing whitespace is annoying when working with the textual output
of nft.

Before:

table t {
chain c {
ct state new
    ^
}
}

After:

table t {
chain c {
ct state new
}
}

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add chain validations tests
Arturo Borrero [Tue, 22 Mar 2016 13:06:09 +0000 (14:06 +0100)] 
tests/shell: add chain validations tests

Some basic test regarding chains: jumps and validations.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell/run-tests.sh: tune kernel cleanup
Arturo Borrero [Fri, 18 Mar 2016 08:41:31 +0000 (09:41 +0100)] 
tests/shell/run-tests.sh: tune kernel cleanup

The modprobe call can return != 0 if, for example, a module was builtin and
we are triying to remove it, so force return code of 0 at the end of the
script.

This patch also adds the '-a' switch to modprobe so it doesn't stop unloading
modules if one of them fails (for example, it was builtin).

While at it, fix several module names, for example: 'nft_bridge_reject' vs
'nft_reject_bridge', delete bogus module names.

Reported-by: Piyush Pangtey <gokuvsvegita@gmail.com>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Tested-by: Piyush Pangtey <gokuvsvegita@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: unload modules between tests
Arturo Borrero [Thu, 17 Mar 2016 08:34:47 +0000 (09:34 +0100)] 
tests/shell: unload modules between tests

This patch adjusts the main test script so it unload all nftables
kernel modules between tests.

This way we achieve two interesting things:
 * avoid false errors in some testcases due to module loading order
 * test the module loading/unloading path itself

The false positives is for example, listing ruleset per families, which depends
on the loading order of nf_tables_xx modules.

We can later add more modules to unload incrementally (for
example nf_tables_switchdev).

This patch assumes we are working with a kernel which is compiled with
nf_tables =m, the case using =y is not supported and can still produce false
positives in some testcases due to module ordering.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agobuild: include/mini-gmp.h is not included at "make dist"
Magnus Öberg [Thu, 17 Mar 2016 10:07:04 +0000 (11:07 +0100)] 
build: include/mini-gmp.h is not included at "make dist"

Added missing dist. file mini-gmp.h in include/Makefile.am

Signed-off-by: Magnus Öberg <magnus.oberg@westermo.se>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: release parsed type and hook name strings
Pablo Neira Ayuso [Tue, 15 Mar 2016 16:15:10 +0000 (17:15 +0100)] 
parser_bison: release parsed type and hook name strings

The scanner allocates memory for this, so release them given that we
don't attach them to any object.

==6277== 42 bytes in 6 blocks are definitely lost in loss record 2 of 4
==6277==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==6277==    by 0x57AC9D9: strdup (strdup.c:42)
==6277==    by 0x41B82D: xstrdup (utils.c:64)
==6277==    by 0x41F510: nft_lex (scanner.l:511)
==6277==    by 0x427FD1: nft_parse (parser_bison.c:3690)
==6277==    by 0x4063AC: nft_run (main.c:231)
==6277==    by 0x40600C: main (main.c:361)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: duplicate string returned by chain_type_name_lookup()
Pablo Neira Ayuso [Tue, 15 Mar 2016 16:11:03 +0000 (17:11 +0100)] 
parser_bison: duplicate string returned by chain_type_name_lookup()

This chain type string is released via chain_free() since b7cb6915a88f,
so duplicate it so we don't try to release statically allocated memory.

Fixes: b7cb6915a88f ("rule: Remove memory leak")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: simplify hook_spec rule
Pablo Neira Ayuso [Tue, 15 Mar 2016 16:25:22 +0000 (17:25 +0100)] 
parser_bison: simplify hook_spec rule

Consolidate this rule by introducing the dev_spec and prio_spec, we save
50 LOC with this patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: Remove memory leak
Piyush Pangtey [Tue, 15 Mar 2016 03:07:41 +0000 (08:37 +0530)] 
rule: Remove memory leak

Added matching xfree calls in chain_free(), for the chain members 'type' and
'dev'.

It can be reproduced by :
nft add chain x y { type filter hook input priority 0; }

Then:
$ sudo valgrind --leak-check=full nft list tables

==2899== HEAP SUMMARY:
==2899==     in use at exit: 327 bytes in 10 blocks
==2899==   total heap usage: 145 allocs, 135 frees, 211,462 bytes allocated
==2899==
==2899== 63 bytes in 9 blocks are definitely lost in loss record 1 of 2
==2899==    at 0x4C2AB80: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2899==    by 0x57A3839: strdup (strdup.c:42)
==2899==    by 0x41C05D: xstrdup (utils.c:64)
==2899==    by 0x411E9B: netlink_delinearize_chain.isra.3 (netlink.c:717)
==2899==    by 0x411F70: list_chain_cb (netlink.c:748)
==2899==    by 0x504A943: nft_chain_list_foreach (chain.c:1015)
==2899==    by 0x4145AE: netlink_list_chains (netlink.c:771)
==2899==    by 0x40793F: cache_init_objects (rule.c:90)
==2899==    by 0x40793F: cache_init (rule.c:130)
==2899==    by 0x40793F: cache_update (rule.c:147)
==2899==    by 0x40FB59: cmd_evaluate (evaluate.c:2475)
==2899==    by 0x429A1C: nft_parse (parser_bison.y:655)
==2899==    by 0x40651C: nft_run (main.c:231)
==2899==    by 0x40618C: main (main.c:357)
==2899==
==2899== LEAK SUMMARY:
==2899==    definitely lost: 63 bytes in 9 blocks
==2899==    indirectly lost: 0 bytes in 0 blocks
==2899==      possibly lost: 0 bytes in 0 blocks
==2899==    still reachable: 264 bytes in 1 blocks
==2899==         suppressed: 0 bytes in 0 blocks
==2899== Reachable blocks (those to which a pointer was found) are not shown.
==2899== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2899==
==2899== For counts of detected and suppressed errors, rerun with: -v
==2899== Use --track-origins=yes to see where uninitialised values come from
==2899== ERROR SUMMARY: 4 errors from 2 contexts (suppressed: 0 from 0)

Signed-off-by: Piyush Pangtey <gokuvsvegita@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: use table_lookup_global() from expr_evaluate_symbol()
Pablo Neira Ayuso [Mon, 14 Mar 2016 18:40:29 +0000 (19:40 +0100)] 
evaluate: use table_lookup_global() from expr_evaluate_symbol()

If there's already a table 'test' defined in the kernel and you load
another table 'test' via `nft -f', table_lookup() returns the table
that already exists in the kernel, so if you look up for objects that
are defined in the file, nft bails out with 'Set does not exist'.

Use table_lookup_global() function returns the existing table that is
defined in the file and that it is set as context via
ctx->handle->table.

This is not a complete fix, we should splice the existing kernel objects
into the userspace declaration. We just need some way to identify what
objects are already in the kernel so we don't send them again (otherwise
we will hit EEXIST errors). I'll follow up with this full fix asap.

Anyway, this patch fixes this shell test:

I: [OK]         ./testcases/sets/cache_handling_0

So at least by now we have all shell test returning OK. I'll add more
tests to catch the case I describe above once it is fixed too.

Cc: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: revisit cache population logic
Pablo Neira Ayuso [Mon, 14 Mar 2016 12:30:56 +0000 (13:30 +0100)] 
src: revisit cache population logic

We get a partial cache (tables, chains and sets) when:

* We see a set reference from a rule, since this set object may be
  already defined in kernelspace and we need to fetch the datatype
  for evaluation.

* We add/delete a set element, we need this to evaluate if the
  element datatype is correct.

* We rename a chain, since we need to know the chain handle.

* We add a chain/set. This isn't needed for simple command line
  invocations. However, since the existing codepath is also exercised
  from `nft -f' context, we need to know if the object exists in the
  kernel. Thus, if this a newly declared object (not yet in the kernel) we
  add it to the cache, otherwise, we will not find follow up references to
  this object in our cache.

We get a full cache when:

* We list the ruleset. We can provide finer grain listing though,
  via partial cache, later.

* We monitor updates, since this displays incremental updates based on
  the existing objects.

* We export the ruleset, since this dumps all of the existing objects.

* We push updates via `nft -f'. We need to know what objects are
  already in the kernel for incremental updates. Otherwise,
  cache_update() hits a bogus 'set doesn't exist' error message for
  just declared set in this batch.  To avoid this problem, we need a
  way to differentiate between what objects in the lists that are
  already defined in the kernel and what are just declared in this
  batch (hint: the location structure information is set for just
  declared objects).

We don't get a cache at all when:

* We flush the ruleset, this is important in case of delinearize
  bugs, so you don't need to reboot or manually flush the ruleset via
  libnftnl examples/nft-table-flush.

* We delete any object, except for set elements (as we describe above).

* We add a rule, so you can generate via --debug=netlink the expression
  without requiring a table and chain in place.

* We describe a expression.

This patch also includes some intentional adjustments to the shell tests
to we don't get bogus errors due to changes in the list printing.

BTW, this patch also includes a revert for 97493717e738 ("evaluate: check
if table and chain exists when adding rules") since that check is not
possible anymore with this logic.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: frag: enable more tests
Florian Westphal [Mon, 7 Mar 2016 16:39:36 +0000 (17:39 +0100)] 
tests: frag: enable more tests

a couple of tests were disabled since nft did not support this.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agonetlink_delinarize: shift constant for ranges too
Florian Westphal [Mon, 7 Mar 2016 17:28:33 +0000 (18:28 +0100)] 
netlink_delinarize: shift constant for ranges too

... else rule like vlan pcp 1-3 won't work and will be displayed
as 0-0 (reverse direction already works since range is represented
as two lte/gte compare expressions).

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonft-test: don't zap remainder of rule after handling a set
Florian Westphal [Mon, 7 Mar 2016 20:05:56 +0000 (21:05 +0100)] 
nft-test: don't zap remainder of rule after handling a set

Don't delete the part after the set, i.e. given

 chain input {
     type filter hook input priority 0; policy accept;
     vlan id { 1, 2, 4, 100, 4095} vlan pcp 1-3
 }

don't remove the vlan pcp 1-3 part.

This exposes following bug:

bridge/vlan.t: WARNING: line: 32:
'nft add rule --debug=netlink bridge test-bridge input vlan id { 1, 2, 4, 100, 4095 } vlan pcp 1-3': 'vlan id { 1, 2, 4, 100, 4095 } vlan pcp 1-3' mismatches 'vlan id { 4, 1, 2, 4095, 100} vlan pcp 0-0'

We do not shift the range, so on reverse translation we get a 0-0 output.
The bug will be fixes in a followup commit.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: fix bogus offset w exthdr expressions
Florian Westphal [Mon, 7 Mar 2016 16:38:23 +0000 (17:38 +0100)] 
netlink_delinearize: fix bogus offset w exthdr expressions

Need to fetch the offset from the exthdr template.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: frag: enable more tests
Florian Westphal [Mon, 7 Mar 2016 16:27:53 +0000 (17:27 +0100)] 
tests: frag: enable more tests

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: add tests for router-advertisement and router-solicitation icmp types
Pablo Neira Ayuso [Thu, 10 Mar 2016 11:14:23 +0000 (12:14 +0100)] 
tests/py: add tests for router-advertisement and router-solicitation icmp types

Introduced by 039f818fc88010 ("proto: Add router advertisement and solicitation
icmp types").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoproto: Add router advertisement and solicitation icmp types
Laura Garcia Liebana [Wed, 9 Mar 2016 18:53:07 +0000 (19:53 +0100)] 
proto: Add router advertisement and solicitation icmp types

Enable support for router-advertisement and router-solicitation icmp types in nft.

Example:
$ sudo nft add rule ip filter input icmp type router-advertisement counter accept
$ sudo nft add rule ip filter input icmp type router-solicitation counter accept

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: allow 'snat' and 'dnat' keywords from the right-hand side
Pablo Neira Ayuso [Wed, 9 Mar 2016 11:23:00 +0000 (12:23 +0100)] 
parser_bison: allow 'snat' and 'dnat' keywords from the right-hand side

Parse 'snat' and 'dnat' reserved keywords from the right-hand side as
symbols. Thus, we can use them as values from ct status.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=950
Reported-by: Ana Rey <anarey@gmail.com>
Reported-by: Karol Babioch <karol@babioch.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonft: Modified punctuation used in nft's show_help
Piyush Pangtey [Wed, 9 Mar 2016 07:16:53 +0000 (12:46 +0530)] 
nft: Modified punctuation used in nft's show_help

Replaced '/' between shortopt and longopt with ',' , as used by other utilities.

Signed-off-by: Piyush Pangtey <gokuvsvegita@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodoc: nft: Fixed a typo and added/changed punctuation
Piyush Pangtey [Tue, 8 Mar 2016 14:27:15 +0000 (19:57 +0530)] 
doc: nft: Fixed a typo and added/changed punctuation

In nft's man page , instead of using '/' between shortopt and longopt in the
"SYNOPSIS" and "OPTIONS" section , use '|' and ',' respectively.
(just like the man pages of iptables, etc.)

Fixed a typo and added missing ',' .

Signed-off-by: Piyush Pangtey <gokuvsvegita@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: enable tests for dccp types
Pablo Neira Ayuso [Mon, 7 Mar 2016 12:30:09 +0000 (13:30 +0100)] 
tests/py: enable tests for dccp types

This patch make sure we test dccp type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: remove 'reset' as reserve keyword
Pablo Neira Ayuso [Mon, 7 Mar 2016 12:17:24 +0000 (13:17 +0100)] 
parser: remove 'reset' as reserve keyword

The 'reset' keyword can be used as dccp type, so don't qualify it as
reserve keyword to avoid a conflict with this.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1055
Reported-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: simplify ("rule: delete extra space in sets printing")
Pablo Neira Ayuso [Fri, 4 Mar 2016 09:23:21 +0000 (10:23 +0100)] 
rule: simplify ("rule: delete extra space in sets printing")

This simplifies bd23f7628570 ("rule: delete extra space in sets printing")
by passing the whitespace from set_print_plain() called from the monitoring
path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agotests/py: extend masquerade to cover ports too
Pablo Neira Ayuso [Thu, 3 Mar 2016 19:07:07 +0000 (20:07 +0100)] 
tests/py: extend masquerade to cover ports too

Tests new masquerade port range support (available since 4.6-rc).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/listing: add some listing tests
Arturo Borrero [Wed, 13 Jan 2016 18:29:09 +0000 (19:29 +0100)] 
tests/listing: add some listing tests

Let's test what is shown with the 'list' command, for ruleset, tables and sets.

In order to ease debug in case of failure, if the diff tool is in the system,
then a textual diff is printed.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add first `nft -f' tests
Arturo Borrero [Wed, 13 Jan 2016 18:25:46 +0000 (19:25 +0100)] 
tests/shell: add first `nft -f' tests

This patch add some basic initial tests.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: delete extra space in sets printing
Arturo Borrero [Wed, 13 Jan 2016 17:21:41 +0000 (18:21 +0100)] 
rule: delete extra space in sets printing

The extra space is printed when sets are printed in tabulated format.

table inet test {
set test {
  ^
type ipv4_addr
}
}

However, the space is still required in printing in plain format (ie, monitor).

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: Add support for masquerade port selection
Shivani Bhardwaj [Fri, 22 Jan 2016 20:55:55 +0000 (02:25 +0530)] 
src: Add support for masquerade port selection

Provide full support for masquerading by allowing port range selection, eg.

 # nft add rule nat postrouting ip protocol tcp masquerade to :1024-10024

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: handle extension header templates with odd sizes
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
netlink_delinearize: handle extension header templates with odd sizes

This enables nft to display
frag frag-off 33

... by considering a mask during binop postprocess in case
the initial template lookup done when the exthdr expression was
created did not yield a match.

In the above example, kernel netlink data specifies 16bits,
but the frag field is only 13bits wide.

We use the implicit binop mask to re-do the template lookup with
corrected offset and size information.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: prepare binop_postprocess for exthdr demux
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
netlink_delinearize: prepare binop_postprocess for exthdr demux

binop_postprocess takes care of removing masks if we're dealing
with payload expressions that have non-byte divisible sizes
or offsets.

Same can happen when matching some extension header fields, i.e.
this also needs to handle exthdr expression, not just payload.

So rename payload to left and move test for left type to
binop_postprocess.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexthdr: store offset for later use
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
exthdr: store offset for later use

Its possible that we cannot find the template without also
considering an implicit mask.  For this we need to store the offset.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexthdr: remove implicit dependencies
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
exthdr: remove implicit dependencies

exthdr expression requires a dependency on ipv6; we can
thus remove an ipv6 protocol test if its present.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add/fix inet+exthdr tests
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
tests: add/fix inet+exthdr tests

exhdr needs to be treated as if we'd test an ipv6 header field, i.e.
inet, bridge, netdev need to add a dependency on ipv6 protocol.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexthdr: generate dependencies for inet/bridge/netdev family
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
exthdr: generate dependencies for inet/bridge/netdev family

Should treat this as if user would have asked to match ipv6 header field.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agopayload: move payload_gen_dependency generic part to helper
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
payload: move payload_gen_dependency generic part to helper

We should treat exthdr just as if user asked for e.g. ip6 saddr
and inject the needed dependency statement.

payload_gen_dependency cannot be used since the *expr needs
to be a payload expression, but the actual dependency generation
doesn't depend on a particular expression type.

In order to reuse this part for future exthdr dependency injection
move it to a helper.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: add and use netlink_gen_exthdr_mask
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
netlink: add and use netlink_gen_exthdr_mask

rule ip6 filter input frag frag-off 33

before patch:
 [ exthdr load 1b @ 44 + 2 => reg 1 ]
 [ cmp eq reg 1 0x00002100 ]

We truncated 13bit field to 1 byte.

after patch:
 [ exthdr load 2b @ 44 + 2 => reg 1 ]
 [ bitwise reg 1 = (reg=1 & 0x0000f8ff ) ^ 0x00000000 ]
 [ cmp eq reg 1 0x00000801 ]

- ask for 2 bytes
- mask out the 3 lower bits
- shift the value by 3 so equality test will pass for 33

This causes test failures, will be fixed up in a later patch
(the test suite expects the old, broken input).

It also misses the reverse translation to remove the binop,
find the right template and undo the shift of the value.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: split generic part of netlink_gen_payload_mask into helper
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
netlink: split generic part of netlink_gen_payload_mask into helper

netlink_gen_payload_mask assumes expr is a payload expression,
but most of this function would work fine with exthdr too.

So split the gernic part into a helper, followup patch will
add netlink_gen_exthdr_mask.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: enforce ip6 proto with exthdr expression
Florian Westphal [Wed, 2 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
evaluate: enforce ip6 proto with exthdr expression

Don't allow use of exthdr with e.g. ip family.
Move frag.t to ip6 directory and don't use it with ipv4 anymore.

This change causes major test failures for all exthdr users
since they now fail with inet/bridge/netdev families.

Will be resolved in a later patch -- we need to add
an ipv6 dependency for them.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: reject set references in set elements
Florian Westphal [Mon, 29 Feb 2016 16:50:39 +0000 (17:50 +0100)] 
evaluate: reject set references in set elements

given

table filter {
  set local {
    type iface_index
    elements = { lo }
  }
  chain input {
    type filter hook input priority 0;
    iif { @lan, } accept;
  }
}

nft BUG()s.  I don't see how we could support sets-in-set; add a sanity
check and error out instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoexamples: use current type names
Florian Westphal [Thu, 25 Feb 2016 14:45:08 +0000 (15:45 +0100)] 
examples: use current type names

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agometa: fix error checks in tc handle parser
Florian Westphal [Mon, 1 Feb 2016 11:02:36 +0000 (12:02 +0100)] 
meta: fix error checks in tc handle parser

'meta priority foobar' did not return an error -- instead
we used min/max values with undefined content.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoproto: use parameter-problem for icmpv6 type
Pablo Neira Ayuso [Mon, 1 Feb 2016 13:51:02 +0000 (14:51 +0100)] 
proto: use parameter-problem for icmpv6 type

To keep it consistent with icmpv4 naming.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=911
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: prune implicit binop before payload_match_postprocess()
Pablo Neira Ayuso [Mon, 1 Feb 2016 11:40:40 +0000 (12:40 +0100)] 
netlink_delinearize: prune implicit binop before payload_match_postprocess()

payload_match_postprocess() expects a relational with payload of his lhs
and value on the rhs.

Moreover, payload_match_expand() releases the previous expression so
valgrind reports an use-after-free when pruning the implicit binop.

Fix this by calling payload_match_postprocess() in first place.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: test vlan on ingress
Pablo Neira Ayuso [Thu, 28 Jan 2016 20:57:29 +0000 (21:57 +0100)] 
tests/py: test vlan on ingress

This generates the same code as bridge does, but it includes this check
in first place.

  [ meta load iiftype => reg 1 ]
  [ cmp eq reg 1 0x00000001 ]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: annotate follow up dependency just after killing another
Pablo Neira Ayuso [Thu, 28 Jan 2016 17:09:50 +0000 (18:09 +0100)] 
src: annotate follow up dependency just after killing another

The inet and netdev families generate two implicit dependencies to check
for the interface type, so we have to check just after killing an implicit
dependency if there is another that we should annotate to kill it as well.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoproto: proto_dev_type() returns interface type for base protocols too
Pablo Neira Ayuso [Thu, 28 Jan 2016 19:53:48 +0000 (20:53 +0100)] 
proto: proto_dev_type() returns interface type for base protocols too

The device protocol definition provides a mapping between the interface
type, ie. ARPHDR_*, and the overlying protocol base definition, eg.
proto_eth.

This patch updates proto_dev_type() so it also returns a mapping for
these overlying ethernet protocol definitions, ie. ip, ip6, vlan, ip,
arp.

This patch required to resolve problems with automatic dependency
generation for vlan in the netdev and inet families.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: generate ether type payload after meta iiftype
Pablo Neira Ayuso [Thu, 28 Jan 2016 20:07:15 +0000 (21:07 +0100)] 
evaluate: generate ether type payload after meta iiftype

Once the meta iiftype is generated, we shouldn't return from
resolve_protocol_conflict() since we also need to generate the ether
type payload implicit match after it.

This gets rid of the manual proto-ctx update from
meta_iiftype_gen_dependency() that we don't need since stmt_evaluate()
already handles this for us.

Moreover, skip error reporting once we verify that the protocol conflict
has been resolved.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: wrap protocol context debunk into function
Pablo Neira Ayuso [Thu, 28 Jan 2016 12:22:34 +0000 (13:22 +0100)] 
evaluate: wrap protocol context debunk into function

ether type vlan sets the network layer protocol context to vlan. This
function debunks the existing link layer protocol context by setting it
to vlan.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: assert on invalid base in resolve_protocol_conflict()
Pablo Neira Ayuso [Thu, 28 Jan 2016 12:03:03 +0000 (13:03 +0100)] 
evaluate: assert on invalid base in resolve_protocol_conflict()

We already have similar code in the tree, we shouldn't see bases over
transport yet.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: only try to replace dummy protocol from link-layer context
Pablo Neira Ayuso [Thu, 28 Jan 2016 11:58:19 +0000 (12:58 +0100)] 
evaluate: only try to replace dummy protocol from link-layer context

Add proto_is_dummy() that returns true for netdev and inet family, the
only two using a dummy link-layer protocol base definition.

Rename supersede_dep() to meta_iiftype_gen_dependency() since this is
generating the implicit meta iiftype check for netdev and inet.

This patch also gets rid of the have->length check. The tests pass fine
without this so I suspect this is superfluos.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: don't adjust offset from resolve_protocol_conflict()
Pablo Neira Ayuso [Thu, 28 Jan 2016 11:52:47 +0000 (12:52 +0100)] 
evaluate: don't adjust offset from resolve_protocol_conflict()

This is not itself a conflict, move this check out of this function.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: check if we have to resolve a conflict in first place
Pablo Neira Ayuso [Thu, 28 Jan 2016 11:48:30 +0000 (12:48 +0100)] 
evaluate: check if we have to resolve a conflict in first place

So we enter resolve_protocol_conflict() only when we really have a
conflict that we want to try to resolve.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: move inet/netdev protocol context supersede logic to supersede_dep()
Pablo Neira Ayuso [Tue, 26 Jan 2016 16:22:45 +0000 (17:22 +0100)] 
evaluate: move inet/netdev protocol context supersede logic to supersede_dep()

This is a cleanup to untangle this logic a bit.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: resolve_protocol_conflict() should return int
Pablo Neira Ayuso [Tue, 26 Jan 2016 16:01:27 +0000 (17:01 +0100)] 
evaluate: resolve_protocol_conflict() should return int

Instead of bool, expr_error() returns -1 if we fail to create
dependencies. We need to propagate this error value.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: test port ranges and maps for redirect
Pablo Neira Ayuso [Sun, 31 Jan 2016 22:17:20 +0000 (23:17 +0100)] 
tests/py: test port ranges and maps for redirect

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: netlink_linearize: Fix bug for redirect target
Shivani Bhardwaj [Thu, 28 Jan 2016 19:35:37 +0000 (01:05 +0530)] 
src: netlink_linearize: Fix bug for redirect target

Before this patch,
$ sudo nft --debug=netlink add rule ip nat post ip protocol tcp redirect to 100-200
ip nat post
  [ payload load 1b @ network header + 9 => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ immediate reg 1 0x00006400 ]
  [ immediate reg 2 0x0000c800 ]
  [ redir proto_min reg 1 proto_max reg 5 ]

<cmdline>:1:1-56: Error: Could not process rule: Invalid argument
add rule ip nat post ip protocol tcp redirect to 100-200
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

After this patch,
$ sudo nft --debug=netlink add rule ip nat post ip protocol tcp redirect to 100-200
ip nat post
  [ payload load 1b @ network header + 9 => reg 1 ]
  [ cmp eq reg 1 0x00000006 ]
  [ immediate reg 1 0x00006400 ]
  [ immediate reg 2 0x0000c800 ]
  [ redir proto_min reg 1 proto_max reg 2 ]

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add fwd statement for netdev
Pablo Neira Ayuso [Tue, 24 Nov 2015 20:26:27 +0000 (21:26 +0100)] 
src: add fwd statement for netdev

This patch add support for the forward statement, only available at the
netdev family.

 # nft add table netdev filter
 # nft add chain netdev filter ingress { type filter hook ingress device eth0 priority 0\; }
 # nft add rule netdev filter ingress fwd to dummy0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add dup statement for netdev
Pablo Neira Ayuso [Thu, 19 Nov 2015 11:49:53 +0000 (12:49 +0100)] 
src: add dup statement for netdev

This patch contains the missing chunk to add support for the netdev
family. Part of the support slipped through in the original patch to
add the dup statement for IPv4 and IPv6.

 # nft add table netdev filter
 # nft add chain netdev filter ingress { type filter hook ingress device eth0 priority 0\; }
 # nft add rule netdev filter ingress dup to dummy0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoip6: Add tests for icmpv6 packet types
Shivani Bhardwaj [Mon, 18 Jan 2016 19:14:48 +0000 (00:44 +0530)] 
ip6: Add tests for icmpv6 packet types

Add tests for the icmpv6 packet types policy-fail and reject-route to be
used with REJECT target.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: datatype: Modify symbol table for icmpv6 packet types
Shivani Bhardwaj [Tue, 5 Jan 2016 12:20:23 +0000 (17:50 +0530)] 
src: datatype: Modify symbol table for icmpv6 packet types

Add the missing symbols and correct the macros corresponding to the
existing symbols.

Signed-off-by: Shivani Bhardwaj <shivanib134@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: only remove protocol if equal cmp is used
Florian Westphal [Wed, 27 Jan 2016 01:53:51 +0000 (02:53 +0100)] 
netlink_delinearize: only remove protocol if equal cmp is used

Check for OP_EQ before removing a dependency, else we may zap wrong one,
changing the meaning of the rule.

Listing without patch:
ip protocol udp udp dport ssh
ip protocol udp udp dport ssh
counter packets 1 bytes 308 ip protocol udp udp dport ssh

With patch:
ip protocol != tcp udp dport ssh
ip protocol != udp udp dport ssh
ip protocol != tcp counter packets 1 bytes 308 udp dport ssh

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: do binop postprocessing also for map lookups
Florian Westphal [Sun, 24 Jan 2016 12:37:10 +0000 (13:37 +0100)] 
netlink: do binop postprocessing also for map lookups

old nft list:
mark set unknown unknown & 0xfff [invalid type] map { 3 : 0x00000017, 1 : 0x0000002a}
new:
mark set vlan id map { 3 : 0x00000017, 1 : 0x0000002a}

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add two map test cases
Florian Westphal [Sun, 24 Jan 2016 12:29:55 +0000 (13:29 +0100)] 
tests: add two map test cases

One normal map lookup, one with an explicit binop.
The latter is supposed to also work with the followup patch applied.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: move binop postprocess to extra function
Florian Westphal [Sun, 24 Jan 2016 12:01:04 +0000 (13:01 +0100)] 
netlink: move binop postprocess to extra function

Just move the payload trim part to a separate function.
Next patch will add a second call site to deal with map ops
that use a lookup based on a binop result.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodoc: update ct expression
Florian Westphal [Thu, 14 Jan 2016 03:56:32 +0000 (04:56 +0100)] 
doc: update ct expression

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: support limit rate over value
Pablo Neira Ayuso [Wed, 9 Dec 2015 21:55:30 +0000 (22:55 +0100)] 
src: support limit rate over value

So far it was only possible to match packet under a rate limit, this
patch allows you to explicitly indicate if you want to match packets
that goes over or until the rate limit, eg.

... limit rate over 3/second counter log prefix "OVERLIMIT: " drop
... limit rate over 3 mbytes/second counter log prefix "OVERLIMIT: " drop
... ct state invalid limit rate until 1/second counter log prefix "INVALID: "

When listing rate limit until, this shows:

... ct state invalid limit rate 1/second counter log prefix "INVALID: "

thus, the existing syntax is still valid (i.e. default to rate limit until).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: ct: remove BUG cases that work with current master
Florian Westphal [Thu, 7 Jan 2016 15:31:58 +0000 (16:31 +0100)] 
tests: ct: remove BUG cases that work with current master

We have tests for these in ip/ct.t.
(We cannot use ipv4 addresses e.g. in ipv6 family).

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoct regression tests for bytes, packets
Florian Westphal [Mon, 14 Dec 2015 18:49:20 +0000 (19:49 +0100)] 
ct regression tests for bytes, packets

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_linearize: use u64 conversion for 64bit quantities
Florian Westphal [Fri, 8 Jan 2016 00:44:39 +0000 (01:44 +0100)] 
netlink_linearize: use u64 conversion for 64bit quantities

nft generated two 4-byte swaps for conntrack byte/packet counters,
which are 64bit host-endian values:

byteorder reg 1 = hton(reg 1, 4, 8) ]

This makes the kernel perform two htonl() calls, but we need
a cpu_to_be64 conversion instead (reg 1, 8, 8).

Without this a rule like 'ct original packets > 10'
matched when counter was between 1 and 10.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoct: add packet/byte counter support
Florian Westphal [Sat, 12 Dec 2015 00:10:04 +0000 (01:10 +0100)] 
ct: add packet/byte counter support

packets and bytes need special treatment -- we want to be able to get
packet/byte counter in either direction, but also express
'fetch in *BOTH* directions', i.e.

ct packets original + ct packets reply > 1000

This either requires a '+' expression, a new 'both' direction, or
keys where direction is optional, i.e.

ct packets > 12345 ; original + reply
ct original packets > 12345 ; original

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonft: swap key and direction in ct_dir syntax
Florian Westphal [Thu, 7 Jan 2016 12:23:33 +0000 (13:23 +0100)] 
nft: swap key and direction in ct_dir syntax

old: ct saddr original 1.2.3.4
new: ct original saddr 1.2.3.4

The advantage is that this allows to add ct keys where direction is optional
without creating ambiguities in the parser.

So we can have
ct packets gt 42
ct original packets gt 42

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: netdev family with ingress chain
Pablo Neira Ayuso [Tue, 12 Jan 2016 14:12:22 +0000 (15:12 +0100)] 
tests/py: netdev family with ingress chain

This patch enables tests for the new netdev family and its ingress
chain.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: test udp from ip and ip6 families
Pablo Neira Ayuso [Tue, 12 Jan 2016 22:32:15 +0000 (23:32 +0100)] 
tests/py: test udp from ip and ip6 families

The existing test was checking for the ip family three times, fix it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: don't test log statement from protocol match
Pablo Neira Ayuso [Tue, 12 Jan 2016 15:38:19 +0000 (16:38 +0100)] 
tests/py: don't test log statement from protocol match

I think this unit tests should be self-contained at some level. The
shell/ directory should be used to catch regressions at ruleset level,
ie. these kind of combinations.

Another motivation is that I want that netdev/ingress gets tested
(coming in a follow up patch), and we don't support log there yet, so I
would need to skip this test for that case.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: initializer_expr must use rhs_expr
Pablo Neira Ayuso [Tue, 12 Jan 2016 12:43:05 +0000 (13:43 +0100)] 
parser_bison: initializer_expr must use rhs_expr

Use rhs_expr and list_rhs_expr as possible occurrences of
initializer_expr since we may only find constant expressions on the
right hand side of the assignment.

Fixes: 2a5d44d8b3c (parser: get rid of multiton_expr from lhs relational expression)
Reported-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Tested-by: Florian Westphal <fw@strlen.de>
Tested-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agotests/py: update test files syntax
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:57 +0000 (01:26 +0100)] 
tests/py: update test files syntax

The test files have been adapted to the syntax defined in the previous
commit "tests/py: modify supported test file syntax"

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: modify supported test file syntax
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:56 +0000 (01:26 +0100)] 
tests/py: modify supported test file syntax

Until now, the syntax to represent tables and chains in test files was:

*ip;test-ip4
*ip6;test-ip6
*inet;test-inet
:input;type filter hook input priority 0

Where lines starting with * are tables and lines starting with : are
chains.

This commit change the test script to deal with new syntax:

:input;type filter hook input priority 0
*ip;test-ip4;input
*ip6;test-ip6;input
*inet;test-inet;input

Now the chains should be included before tables. Also, lines defining
tables have a new third part (delimited by semicolon) where the chains
needed by the table are declared. If table needs to include more than
one chain, those must be separated by commas:

:input;type filter hook input priority 0
:forward;type filter hook forward priority 0
:output;type filter hook output priority 0
*arp;test-arp;input,forward,output

This new syntax allow to include in the same test file chains not
supported by all families of tables tested.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: convert chains and tables to objects
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:55 +0000 (01:26 +0100)] 
tests/py: convert chains and tables to objects

Now these concepts are represented by objects instead of lists or sparse
parameters.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: simplify use of globals
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:54 +0000 (01:26 +0100)] 
tests/py: simplify use of globals

The script made a messy use of globals, these was sometimes accessed
directly and passed as parameter in other cases.

Since is not expected having to deal with threads in the script, now the
globals are always accessed directly for the sake of simplicity.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: fix style
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:53 +0000 (01:26 +0100)] 
tests/py: fix style

- Adjust lines to 80 columns style
- Add two lines of separation between functions
- Remove redundant parentheses and semicolons
- Apply other minor style fixes

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: remove unused variables
Pablo M. Bermudo Garay [Tue, 12 Jan 2016 00:26:52 +0000 (01:26 +0100)] 
tests/py: remove unused variables

Only that. The script had a lot of unused variables.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: don't list anonymous sets
Arturo Borrero [Mon, 4 Jan 2016 12:18:26 +0000 (13:18 +0100)] 
rule: don't list anonymous sets

Don't list anonymous sets when listing all sets.

For example, using this ruleset:

==== 8< ====
table inet test {
set set1 {
type ipv4_addr
}

chain test {
tcp dport { 80 } accept
}
}
==== 8< ====

Before this patch:

% nft list sets
table inet test {
set set0 {
type inet_service
flags constant
}

set set1 {
type ipv4_addr
}
}

After this patch:

% nft list sets
table inet test {
set set1 {
type ipv4_addr
}
}

Fixes: 8f297010 ("rule: `list sets' only displays declaration, not definition")
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add ct tests for ip family
Florian Westphal [Mon, 4 Jan 2016 19:53:52 +0000 (20:53 +0100)] 
tests: add ct tests for ip family

Cannot check e.g. saddr for 192.168.0.1 for 'any' protocol, nft
needs to expect arguments of a specific address type.

So e.g. when using 'inet' we need to add a rule that makes the expected
family explicit, e.g. 'meta nfproto ipv4'.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agosrc: ct: make ct l3proto work
Florian Westphal [Mon, 4 Jan 2016 19:53:52 +0000 (20:53 +0100)] 
src: ct: make ct l3proto work

ct l3proto original == ipv6
<cmdline>:1:56-59: Error: Can't parse symbolic invalid expressions

Its just the nf protocol number -- no dependencies.  Just set right type.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agonetlink: only drop mask if it matches left known-size operand
Florian Westphal [Mon, 4 Jan 2016 19:53:52 +0000 (20:53 +0100)] 
netlink: only drop mask if it matches left known-size operand

During delinearization we attempt to remove masks, for instance
ip saddr $x/32. (mask matches the entire size).

However, in some special cases the lhs size is unknown (0), this
happens f.e. with

'ct saddr original 1.2.3.4/24' which had its '/24' chopped off.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agonetlink: don't handle lhs zero-length expression as concat type
Florian Westphal [Mon, 4 Jan 2016 19:53:52 +0000 (20:53 +0100)] 
netlink: don't handle lhs zero-length expression as concat type

expr->len 0 can appear for some data types whose size can be different
based on some external state, e.g. the conntrack src/dst addresses.

The nft type is 'invalid/0-length' in the template definition, the
size is set (on linearization) based on the network base family,
i.e. the type is changed to ip or ipv6 address at a later stage.

For delinarization, skip zero-length expression as concat type
and give expr_postprocess a chance to fix the types.

Without this change the previous patch will result in nft consuming all
available memory when trying to display e.g. a 'ct saddr' rule.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoct: add support for directional keys
Florian Westphal [Mon, 4 Jan 2016 19:53:43 +0000 (20:53 +0100)] 
ct: add support for directional keys

A few keys in the ct expression are directional, i.e.
we need to tell kernel if it should fetch REPLY or ORIGINAL direction.

Split ct_keys into ct_keys & ct_keys_dir, the latter are those keys
that the kernel rejects unless also given a direction.

During postprocessing we also need to invoke ct_expr_update_type,
problem is that e.g. ct saddr can be any family (ip, ipv6) so we need
to update the expected data type based on the network base.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agodoc: update meta and ct expression keyword lists
Florian Westphal [Mon, 4 Jan 2016 18:47:15 +0000 (19:47 +0100)] 
doc: update meta and ct expression keyword lists

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