]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
9 years agoparser: restore bitwise operations from the rhs of relational expressions
Pablo Neira Ayuso [Tue, 29 Dec 2015 16:21:24 +0000 (17:21 +0100)] 
parser: restore bitwise operations from the rhs of relational expressions

Reintroduce bitwise operation on constants that was removed in ("parser:
restrict relational rhs expression recursion") since we support this
since the beginning.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: rename multiton_expr to multiton_rhs_expr
Pablo Neira Ayuso [Tue, 29 Dec 2015 19:21:43 +0000 (20:21 +0100)] 
parser: rename multiton_expr to multiton_rhs_expr

This rule catches occurrences from the constant rhs, rename it for
readability reasons.

Note that this rule is still used from the set lhs definition that is
always constant (as it represents the key to look up for the
corresponding element).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: get rid of multiton_expr from lhs relational expression
Pablo Neira Ayuso [Tue, 29 Dec 2015 17:14:36 +0000 (18:14 +0100)] 
parser: get rid of multiton_expr from lhs relational expression

The multiton_expr rule matches range, prefix and wildcard expressions
which don't make sense from the non-constant lhs. This rule is there to
handle the nat statement case, whose expression may be composed of
address and port ranges (hence range expressions).

To resolve this, this patch adds the stmt_expr rule to handle the
possible occurrences of map, multiton and primary expressions from
statements.

This results in more rules but it narrows down what we can find from
expressions that are part of action statements.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: add redirect constant to rhs_expr rule
Pablo Neira Ayuso [Mon, 28 Dec 2015 09:51:43 +0000 (10:51 +0100)] 
parser: add redirect constant to rhs_expr rule

So we can use the 'redirect' reserve word as constant from the rhs
expression. Thus, we can use it as icmp type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: restrict relational rhs expression recursion
Pablo Neira Ayuso [Sun, 27 Dec 2015 21:15:17 +0000 (22:15 +0100)] 
parser: restrict relational rhs expression recursion

The relational expression allows recursion from both sides, this doesn't
allow us to know what hand side the input is coming from. This patch
adds a new expr_rhs rule that specifies what can be found on the
constant side of the relational.

Besides making it easier to understand what is actually supported, this
allows us to use reserve words both as constant and statements. This is
used by the following patch to allow to use redirect as constant from
the icmp payload match.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: py: check set value from selector and map
Pablo Neira Ayuso [Mon, 28 Dec 2015 12:07:43 +0000 (13:07 +0100)] 
tests: py: check set value from selector and map

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add new netdev protocol description
Pablo Neira Ayuso [Fri, 25 Dec 2015 18:19:18 +0000 (19:19 +0100)] 
src: add new netdev protocol description

This relies on NFT_META_PROTOCOL instead of ethernet protocol type
header field to prepare support for non-ethernet protocols in the
future.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell: add tests for handles and comments
Arturo Borrero [Wed, 23 Dec 2015 13:19:22 +0000 (14:19 +0100)] 
tests/shell: add tests for handles and comments

Here some tests for optional things like rule handles and comments.

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 test case for cache bug
Arturo Borrero [Fri, 11 Dec 2015 10:10:35 +0000 (11:10 +0100)] 
tests/shell: add test case for cache bug

This testcase for sets catch a cache bug.

By the time of this commit this test is failing, so the test suite shows:

% sudo ./run-tests.sh
I: using nft binary /usr/local/sbin/nft

I: [OK] ./testcases/maps/anonymous_snat_map_0
I: [OK] ./testcases/maps/named_snat_map_0
W: [FAILED] ./testcases/sets/cache_handling_0
I: [OK] ./testcases/optionals/comments_0
I: [OK] ./testcases/optionals/comments_handles_monitor_0
I: [OK] ./testcases/optionals/handles_1
I: [OK] ./testcases/optionals/handles_0
I: [OK] ./testcases/optionals/comments_handles_0

I: results: [OK] 7 [FAILED] 1 [TOTAL] 8

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 maps tests cases
Arturo Borrero [Fri, 11 Dec 2015 10:10:24 +0000 (11:10 +0100)] 
tests/shell: add maps tests cases

Let's add some tests cases for maps.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/: add shell test-suite
Arturo Borrero [Fri, 11 Dec 2015 10:10:19 +0000 (11:10 +0100)] 
tests/: add shell test-suite

This new test-suite is intended to perform tests of higher level than
the other reggresion test-suite.

It can run arbitrary executables which can perform any test apart of testing
the nft syntax or netlink code (which is what the regression tests does).

To run the test suite (as root):
 % cd tests/shell
 % ./run-tests.sh

Test files are executables files with the pattern <<name_N>>, where N is the
expected return code of the executable. Since they are located with `find',
test-files can be spreaded in any sub-directories.

You can turn on a verbose execution by calling:
 % ./run-tests.sh -v

Before each call to the test-files, `nft flush ruleset' will be called.
Also, test-files will receive the environment variable $NFT which contains the
path to the nftables binary being tested.

You can pass an arbitrary $NFT value as well:
 % NFT=../../src/nft ./run-tests.sh

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: add previous statement to rule_pp_ctx
Pablo Neira Ayuso [Wed, 16 Dec 2015 22:33:28 +0000 (23:33 +0100)] 
netlink_delinearize: add previous statement to rule_pp_ctx

564b0e7c13f9 ("netlink_delinearize: postprocess expression before range
merge") crashes nft when the previous statement is removed via
payload_dependency_kill() as this pointer is not valid anymore.

Move the pointer to the previous statement to rule_pp_ctx and invalidate
it when required.

Reported-by: "Pablo M. Bermudo Garay" <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Reported-by: "Pablo M. Bermudo Garay" <pablombg@gmail.com>
9 years agotests/: rearrange tests directory
Arturo Borrero [Fri, 11 Dec 2015 10:10:14 +0000 (11:10 +0100)] 
tests/: rearrange tests directory

Rearrange the directory to obtain a better organization of files and
tests-suites.

We end with a tree like this:

tests
  |
  .--- py
  .--- shell
  .--- files

This was suggested by Pablo.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: postprocess expression before range merge
Pablo Neira Ayuso [Sat, 5 Dec 2015 19:04:22 +0000 (20:04 +0100)] 
netlink_delinearize: postprocess expression before range merge

Dependency statement go away after postprocess, so we should consider
them for possible range merges.

This problem was uncovered when adding support for sub-byte payload
ranges.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: fix sub-byte protocol header definitions
Pablo Neira Ayuso [Sat, 5 Dec 2015 19:04:21 +0000 (20:04 +0100)] 
src: fix sub-byte protocol header definitions

Update bitfield definitions to match according to the way they are
expressed in RFC and IEEE specifications.

This required a bit of update for c3f0501 ("src: netlink_linearize:
handle sub-byte lengths").

>From the linearize step, to calculate the shift based on the bitfield
offset, we need to obtain the length of the word in bytes:

len = round_up(expr->len, BITS_PER_BYTE);

Then, we substract the offset bits and the bitfield length.

        shift = len - (offset + expr->len);

From the delinearize, payload_expr_trim() needs to obtain the real
offset through:

off = round_up(mask->len, BITS_PER_BYTE) - mask_len;

For vlan id (offset 12), this gets the position of the last bit set in
the mask (ie. 12), then we substract the length we fetch in bytes (16),
so we obtain the real bitfield offset (4).

Then, we add that to the original payload offset that was expressed in
bytes:

payload_offset += off;

Note that payload_expr_trim() now also adjusts the payload expression to
its real length and offset so we don't need to propagate the mask
expression.

Reported-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: vlan pcp and cfi are located in the first byte
Pablo Neira Ayuso [Sat, 5 Dec 2015 19:04:20 +0000 (20:04 +0100)] 
tests: vlan pcp and cfi are located in the first byte

Adjust tests to fix wrong payloads, both pcp and cfi are located in the
first nibble of the first byte.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: fix crash when rule test is malformed
Pablo M. Bermudo Garay [Tue, 1 Dec 2015 22:45:23 +0000 (23:45 +0100)] 
tests: fix crash when rule test is malformed

The tests script suffers a crash when a rule test line is malformed
(e.g. if expected result is missing). This commit fixes these crashes
and now the line is skipped and a warning is printed.

While at it, fix a malformed test line too.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: remove useless logic
Pablo M. Bermudo Garay [Tue, 1 Dec 2015 22:45:22 +0000 (23:45 +0100)] 
tests: remove useless logic

In the test files, some lines defining tables was commented out with a
minus "-" sign, also used to mark broken rules. This commit replaces
these signs with actual comments "#" and removes the code that handled
the situation.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add *.got files to .gitignore
Pablo M. Bermudo Garay [Tue, 1 Dec 2015 22:45:21 +0000 (23:45 +0100)] 
tests: add *.got files to .gitignore

During tests execution, some *.payload.got files may be generated. To
avoid annoyances, this commit adds the pattern to .gitignore. Also, the
file "dup.t.payload.got", that was presumably included by mistake, has
been deleted.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: fix use-after-free
Pablo Neira Ayuso [Thu, 26 Nov 2015 15:20:55 +0000 (16:20 +0100)] 
netlink_delinearize: fix use-after-free

We have to clone the payload expression before attaching it to the lhs
of the relational expression, this payload expression is located at the
lhs of the binary operation that is released thereafter.

Fixes: 39f15c2 ("nft: support listing expressions that use non-byte header fields")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: fix up indentation damage
Patrick McHardy [Fri, 27 Nov 2015 17:57:54 +0000 (17:57 +0000)] 
netlink: fix up indentation damage

The conversion to the net libnftnl API has left a lot of indentation damage
in the netlink functions. Fix it up.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agotests: regression: fix arp.t expected payload
Florian Westphal [Fri, 27 Nov 2015 09:57:43 +0000 (10:57 +0100)] 
tests: regression: fix arp.t expected payload

previous commit fixed arp header definition so fix payload as well.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoproto: fix arpop symbol table endianess
Patrick McHardy [Wed, 25 Nov 2015 16:51:10 +0000 (16:51 +0000)] 
proto: fix arpop symbol table endianess

The symbols need to be in big endian.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agopayload: add payload statement
Patrick McHardy [Wed, 25 Nov 2015 16:50:19 +0000 (16:50 +0000)] 
payload: add payload statement

Add support for payload mangling using the payload statement. The syntax
is similar to the other data changing statements:

nft filter output tcp dport set 25

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agoproto: add checksum key information to struct proto_desc
Patrick McHardy [Wed, 25 Nov 2015 16:50:19 +0000 (16:50 +0000)] 
proto: add checksum key information to struct proto_desc

The checksum key is used to determine the correct position where to update
the checksum for the payload statement.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agotests: regression: allow to run tests from anywhere
Pablo M. Bermudo Garay [Thu, 19 Nov 2015 18:51:49 +0000 (19:51 +0100)] 
tests: regression: allow to run tests from anywhere

Since 357d8cf "tests: use the src/nft binary instead of $PATH one", the
tests script needs to be executed from nftables repository root. Now
the script can be run from any location and also checks the binary
existence.

To run a single test file, the path must be relative from the directory
where you launch the script.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agotests: regression: homogenize indentation style
Pablo M. Bermudo Garay [Thu, 19 Nov 2015 18:51:48 +0000 (19:51 +0100)] 
tests: regression: homogenize indentation style

Python interpreter doesn't like mixed indentation. So in order to
prevent future problems, this commit replace some tabs found in the
script with space indentation.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agorule: move comment out of handle
Patrick McHardy [Sun, 15 Nov 2015 16:10:45 +0000 (16:10 +0000)] 
rule: move comment out of handle

The comment does not belong to the handle, it belongs to the rule.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agoevaluate: fix string matching on big endian
Pablo Neira Ayuso [Mon, 9 Nov 2015 14:47:37 +0000 (15:47 +0100)] 
evaluate: fix string matching on big endian

We need to reallocate the constant expression with the right expression
length when evaluating the string. Otherwise the linearization step
generates a wrong comparison on big endian. We cannot do this any
earlier since we don't know the maximum string length for this datatype
at the parsing stage.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add inet test for ip/ether concatenation
Florian Westphal [Tue, 10 Nov 2015 10:55:20 +0000 (11:55 +0100)] 
tests: add inet test for ip/ether concatenation

Test rule from Pablo, it caused assertion failure with earlier
versions of nft (caused by 7ead4932f9ab, later fixed via
775e7ff1f5ddaa32).

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agotests: add test cases for ethernet header matching
Florian Westphal [Thu, 24 Sep 2015 22:41:36 +0000 (00:41 +0200)] 
tests: add test cases for ethernet header matching

Adds ether saddr statements for inet, bridge and ip/ip6 families.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agorule: don't reorder protocol payload expressions when merging
Florian Westphal [Fri, 25 Sep 2015 23:21:14 +0000 (01:21 +0200)] 
rule: don't reorder protocol payload expressions when merging

An instruction like

 bridge filter input ip saddr 1.2.3.4 ether saddr a:b:c:d:e:f

is displayed as

unknown unknown 0x1020304 [invalid type] ether saddr 00:0f:54:0c:11:04 ether type ip

.. because the (implicit) 'ether type ip' that is injected before the
network header match gets merged into the ether saddr instruction.

This inverts the merge in case the merge candidate contains
a next header protocol field.

After this change, the rule will be displayed as

bridge filter input ether saddr a:b:c:d:e:f ip saddr 1.2.3.4

Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agotests: add tests matching on ether saddr for inet, bridge, ip, ip6
Florian Westphal [Thu, 24 Sep 2015 20:39:20 +0000 (22:39 +0200)] 
tests: add tests matching on ether saddr for inet, bridge, ip, ip6

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agosrc: allow filtering on L2 header in inet family
Florian Westphal [Thu, 24 Sep 2015 20:38:06 +0000 (22:38 +0200)] 
src: allow filtering on L2 header in inet family

Error: conflicting protocols specified: inet vs. ether
tcp dport 22 iiftype ether ether saddr 00:0f:54:0c:11:4
                                   ^^^^^^^^^^^

This allows the implicit inet proto dependency to get replaced
by an ethernet one.

This is possible since by the time we detect the conflict the
meta dependency for the network protocol has already been added.

So we only need to add another dependency on the Linklayer frame type.

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=981
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agosrc: add interface wildcard matching
Pablo Neira Ayuso [Sun, 18 Oct 2015 18:02:16 +0000 (20:02 +0200)] 
src: add interface wildcard matching

Contrary to iptables, we use the asterisk character '*' as wildcard.

 # nft --debug=netlink add rule test test iifname eth\*
 ip test test
  [ meta load iifname => reg 1 ]
  [ cmp eq reg 1 0x00687465 ]

Note that this generates an optimized comparison without bitwise.

In case you want to match a device that contains an asterisk, you have
to escape the asterisk, ie.

 # nft add rule test test iifname eth\\*

The wildcard string handling occurs from the evaluation step, where we
convert from:

          relational
             /  \
            /    \
         meta   value
       oifname   eth*

to:
          relational
           /    \
          /      \
        meta    prefix
      ofiname

As Patrick suggested, this not actually a wildcard but a prefix since it
only applies to the string when placed at the end.

More comments:

* This relaxes the left->size > right->size from netlink_parse_cmp()
  for strings since the optimization that this patch applies may now
  result in bogus errors.

* This patch can be later on extended to apply a similar optimization to
  payload expressions when:

expr->len % BITS_PER_BYTE == 0

  For meta and ct, the kernel checks for the exact length of the attributes
  (it expects integer 32 bits) so we can't do it unless we relax that.

* Wildcard strings are not supported from sets and maps yet. Error
  reporting is not very good at this stage since expr_evaluate_prefix()
  doesn't have enough context (ctx->set is NULL, the set object is
  currently created later after evaluating the lhs and rhs of the
  relational). I'll be following up on this later.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: Add command "replace" for rules
Carlos Falgueras García [Tue, 27 Oct 2015 11:58:07 +0000 (12:58 +0100)] 
src: Add command "replace" for rules

Modify the parser and add necessary functions to provide the command "nft
replace rule <ruleid_spec> <new_rule>"

Example of use:

# nft list ruleset -a
table ip filter {
chain output {
ip daddr 8.8.8.7 counter packets 0 bytes 0 # handle 3
}
}
# nft replace rule filter output handle 3 ip daddr 8.8.8.8 counter
# nft list ruleset -a
table ip filter {
chain output {
ip daddr 8.8.8.8 counter packets 0 bytes 0 # handle 3
}
}

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: fix mapping evaluation
Pablo Neira Ayuso [Fri, 23 Oct 2015 11:52:13 +0000 (13:52 +0200)] 
evaluate: fix mapping evaluation

 # cat ruleset.file
 table ip mangle {
        map CLASS05 {
                type ipv4_addr : mark
                elements = { 192.168.0.10 : 0x00000001}
        }

        chain OUTPUT {
                type route hook output priority 0; policy accept;
                mark set ip saddr map @CLASS05
        }
 }
 # nft -f ruleset.file
 ruleset.file:4:28-54: Error: mapping outside of map context
                elements = { 192.168.0.10 : 0x00000001}
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^

This actually is fixing two problems:

1) Validate datatype of the rhs before evaluating the map definition,
   this is also setting set->datalen which is needed for the element
   evaluation.

2) Add missing set context.

Reported-by: Andreas Schultz <aschultz@tpip.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_linearize: factor out prefix generation
Pablo Neira Ayuso [Wed, 21 Oct 2015 10:57:37 +0000 (12:57 +0200)] 
netlink_linearize: factor out prefix generation

Add a new netlink_gen_prefix() function that encapsulates the prefix
generation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: check if table and chain exists when adding rules
Pablo Neira Ayuso [Sun, 18 Oct 2015 18:18:05 +0000 (20:18 +0200)] 
evaluate: check if table and chain exists when adding rules

Assuming a table 'test' that contains a chain 'test':

 # nft add rule test1 test2 counter
 <cmdline>:1:1-28: Error: Could not process rule: Table 'test1' does not exist
 add rule test1 test2 counter
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 # nft add rule test test2 counter
 <cmdline>:1:1-27: Error: Could not process rule: Chain 'test2' does not exist
 add rule test test2 counter
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: show all sets via list sets with no family
Pablo Neira Ayuso [Fri, 25 Sep 2015 10:44:42 +0000 (12:44 +0200)] 
parser_bison: show all sets via list sets with no family

Default to the same behaviour that we get through `list ruleset', ie.

 # nft list sets
 table ip test1 {
        set foo {
                type ipv4_addr
        }
 }
 table ip6 test2 {
        set bar {
                type ipv6_addr
        }
 }

 # nft list sets ip
 table ip test1 {
        set foo {
                type ipv4_addr
        }
 }

 # nft list sets ip6
 table ip6 test2 {
        set bar {
                type ipv6_addr
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agorule: rework list chain
Pablo Neira Ayuso [Wed, 30 Sep 2015 15:25:43 +0000 (17:25 +0200)] 
rule: rework list chain

After this patch:

 # nft list chain inet filter forward
 table inet filter {
        chain forward {
                type filter hook forward priority 0; policy drop;
                ct state established,related counter packets 39546074 bytes 11566126287 accept
        }
 }

Before this patch, this was showing the full table definition, including
all chains, which is not what the user is asking for.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agorule: `list sets' only displays declaration, not definition
Pablo Neira Ayuso [Wed, 30 Sep 2015 15:17:12 +0000 (17:17 +0200)] 
rule: `list sets' only displays declaration, not definition

 # nft list sets
 table ip nat {
        set libssh {
                type ipv4_addr
        }
 }
 table inet filter {
        set set0 {
                type inet_service
                flags constant
        }
        set set1 {
                type inet_service
                flags constant
        }
        set set2 {
                type icmpv6_type
                flags constant
        }
 }

So in case you want to inspect the definition, you have to use `list set'
and the specific set that you want to inspect:

 # nft list set inet filter set0
 table inet filter {
        set set0 {
                type inet_service
                flags constant
                elements = { 2200, ssh}
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agoevaluate: check if set exists before listing it
Pablo Neira Ayuso [Fri, 25 Sep 2015 10:53:40 +0000 (12:53 +0200)] 
evaluate: check if set exists before listing it

After this patch, we obtain:

 # nft list set ip6 test pepe
 <cmdline>:1:1-22: Error: Could not process rule: Set 'foo' does not exist
 list set ip6 test foo
 ^^^^^^^^^^^^^^^^^^^^^

So we get things aligned with table and chain listing commands.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agorule: display table when listing one set
Pablo Neira Ayuso [Fri, 25 Sep 2015 10:51:13 +0000 (12:51 +0200)] 
rule: display table when listing one set

After:

 # nft list set ip6 test foo
 table ip6 test {
        set foo {
                type ipv4_addr
        }
 }

Before:

  # nft list set ip6 test foo
        set foo {
                type ipv4_addr
        }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agosrc: add `list chains' command
Pablo Neira Ayuso [Wed, 23 Sep 2015 17:00:33 +0000 (19:00 +0200)] 
src: add `list chains' command

 # nft list chains
 table ip filter {
        chain test1 {
        }
        chain test2 {
        }
        chain input {
                type filter hook input priority 0; policy accept;
        }
 }
 table ip6 filter {
        chain test1 {
        }
        chain input {
                type filter hook input priority 0; policy accept;
        }
 }

You can also filter out per family:

 # nft list chains ip
 table ip x {
        chain y {
        }
        chain xz {
        }
        chain input {
                type filter hook input priority 0; policy accept;
        }
 }

 # nft list chains ip6
 table ip6 filter {
        chain x {
        }
        chain input {
                type filter hook input priority 0; policy accept;
        }
 }

This command only shows the chain declarations, so the content (the
definition) is omitted.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agorule: display table when listing sets
Pablo Neira Ayuso [Fri, 25 Sep 2015 10:43:35 +0000 (12:43 +0200)] 
rule: display table when listing sets

After this patch:

 # nft list sets ip
 table ip test {
        set pepe {
                type ipv4_addr
        }
 }

Before:

 # nft list sets ip
        set pepe {
                type ipv4_addr
        }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
10 years agorule: fix printing of rule comments
Arturo Borrero Gonzalez [Thu, 8 Oct 2015 09:27:24 +0000 (11:27 +0200)] 
rule: fix printing of rule comments

Several fixes:
 * handles are printed last
 * simplify space games (an extra space was being printed)
 * comments are shown with `nft monitor' as well (missing before this patch)

Before this patch:

% nft list ruleset -a
[...]
chain test {
iifname eth0 # handle 1 comment "test"

}
[...]

% nft list ruleset
[...]
chain test {
iifname eth0  comment "test"
    ^^
}
[...]

% nft monitor &
% nft add rule test test iifname eth0 comment "test"
add rule test test iifname eth0

After this patch:

% nft list ruleset -a
chain test {
iifname eth0 comment "test" # handle 1
    ^
}

% nft monitor -a &
% nft add rule test test iifname eth0 comment "test"
add rule test test iifname eth0 comment "test" # handle 1

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoexpression: provide clone operation for set element ops
Florian Westphal [Wed, 30 Sep 2015 22:13:02 +0000 (00:13 +0200)] 
expression: provide clone operation for set element ops

define addrs={ 1.2.3.4 }
table ip filter {
chain input {
type filter hook input priority 0;
ip saddr $addrs accept
}
}

segfaults. Using saddr { 1.2.3.4 } instead of $addrs works.

Link: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=801087
Tested-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agotests: add tests for dup
Pablo Neira Ayuso [Tue, 29 Sep 2015 18:27:48 +0000 (20:27 +0200)] 
tests: add tests for dup

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add dup statement support
Pablo Neira Ayuso [Tue, 29 Sep 2015 16:21:54 +0000 (18:21 +0200)] 
src: add dup statement support

This allows you to clone packets to destination address, eg.

... dup to 172.20.0.2
... dup to 172.20.0.2 device eth1
... dup to ip saddr map { 192.168.0.2 : 172.20.0.2, ... } device eth1

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: show all tables via list tables with no family
Pablo Neira Ayuso [Tue, 22 Sep 2015 22:14:33 +0000 (00:14 +0200)] 
parser: show all tables via list tables with no family

Default to the same behaviour that we get through `list ruleset', ie.

 # nft list tables
 table ip filter
 table ip6 filter

 # nft list tables ip
 table ip filter

 # nft list tables ip6
 table ip6 filter

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=1033
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: filter out tables depending on family
Pablo Neira Ayuso [Tue, 22 Sep 2015 22:06:42 +0000 (00:06 +0200)] 
rule: filter out tables depending on family

 # nft list tables ip
 table ip filter

 # nft list tables ip6
 table ip6 filter

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=1033
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: limit: extend them to validate new bytes/second and burst parameters
Pablo Neira Ayuso [Tue, 22 Sep 2015 10:49:15 +0000 (12:49 +0200)] 
tests: limit: extend them to validate new bytes/second and burst parameters

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add burst parameter to limit
Pablo Neira Ayuso [Mon, 3 Aug 2015 13:56:27 +0000 (15:56 +0200)] 
src: add burst parameter to limit

... limit rate 1024 mbytes/second burst 10240 bytes
... limit rate 1/second burst 3 packets

This parameter is optional.

You need a Linux kernel >= 4.3-rc1.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add per-bytes limit
Pablo Neira Ayuso [Mon, 3 Aug 2015 13:50:03 +0000 (15:50 +0200)] 
src: add per-bytes limit

This example show how to accept packets below the ratelimit:

... limit rate 1024 mbytes/second counter accept

You need a Linux kernel >= 4.3-rc1.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agovlan: make != tests work
Florian Westphal [Wed, 12 Aug 2015 09:27:35 +0000 (11:27 +0200)] 
vlan: make != tests work

10 years agotests: vlan tests
Florian Westphal [Mon, 10 Aug 2015 23:19:44 +0000 (01:19 +0200)] 
tests: vlan tests

add a few regression tests that match vlan id/dei/pcp fields
of the vlan header.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agonft: support listing expressions that use non-byte header fields
Florian Westphal [Fri, 7 Aug 2015 10:09:08 +0000 (12:09 +0200)] 
nft: support listing expressions that use non-byte header fields

This allows to list rules that check fields that are not aligned on byte
boundary.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agotests: add tests for ip version/hdrlength/tcp doff
Florian Westphal [Wed, 12 Aug 2015 01:23:40 +0000 (03:23 +0200)] 
tests: add tests for ip version/hdrlength/tcp doff

Header fields of 4 bit lengths.  Requires implicit masks and
shifting of RHS constant.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agonetlink: cmp: shift rhs constant if lhs offset doesn't start on byte boundary
Florian Westphal [Tue, 4 Aug 2015 12:11:03 +0000 (14:11 +0200)] 
netlink: cmp: shift rhs constant if lhs offset doesn't start on byte boundary

if we have payload(someoffset) == 42, then shift 42 in case someoffset
doesn't start on a byte boundary.

We already insert a mask instruction to only load those bits into
the register that we were interested in, but the cmp will fail without
also adjusting rhs accordingly.

Needs additional patch in reverse direction to undo the shift again
when dumping ruleset.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agonft: fill in doff and fix ihl/version template entries
Florian Westphal [Sun, 26 Jan 2014 13:52:47 +0000 (14:52 +0100)] 
nft: fill in doff and fix ihl/version template entries

This allows to use

nft add rule ip filter input tcp doff 8

or similar.

Furhermore, ip version looked at hdrlen and vice versa.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agosrc: netlink: don't truncate set key lengths
Florian Westphal [Fri, 14 Aug 2015 01:16:32 +0000 (03:16 +0200)] 
src: netlink: don't truncate set key lengths

If key is e.g. 12 bits, pretend its 16 instead of 8.  This is needed
to make sets work with header fields with size not divisible by 8.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agosrc: netlink_linearize: handle sub-byte lengths
Florian Westphal [Tue, 17 Sep 2013 12:29:29 +0000 (14:29 +0200)] 
src: netlink_linearize: handle sub-byte lengths

Currently length is expr->len / BITS_PER_BYTE, i.e. expr->len
has to be a multiple of 8.

When core asks for e.g. '9 bits', we truncate this to 8.
Round up to 16 and inject a 9-bit mask to zero out the parts we're not
interested in.

This will also need change to the delinarization step to
remove the extra op when dumping rules from kernel.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agopayload: disable payload merge if offsets are not on byte boundary.
Florian Westphal [Tue, 4 Aug 2015 10:29:50 +0000 (12:29 +0200)] 
payload: disable payload merge if offsets are not on byte boundary.

... because it doesn't work, we attempt to merge it into wrong
place, we would have to merge the second value at a specific location.

F.e. vlan hdr 4094 gives us

0xfe0f

Merging in the CFI should yield 0xfe1f, but the constant merging
doesn't know how to achive that; at the moment 'vlan id 4094'
and 'vlan id 4094 vlan cfi 1' give same result -- 0xfe0f.

For now just turn off the optimization step unless everything is
byte divisible (the common case).

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agonft: allow stacking vlan header on top of ethernet
Florian Westphal [Sun, 5 Jul 2015 18:27:28 +0000 (20:27 +0200)] 
nft: allow stacking vlan header on top of ethernet

currently 'vlan id 42' or even 'vlan type ip' doesn't work since
we expect ethernet header but get vlan.

So if we want to add another protocol header to the same base, we
attempt to figure out if the new header can fit on top of the existing
one (i.e. proto_find_num gives a protocol number when asking to find
link between the two).

We also annotate protocol description for eth and vlan with the full
header size and track the offset from the current base.

Otherwise, 'vlan type ip' fetches the protocol field from mac header
offset 0, which is some mac address.

Instead, we must consider full size of ethernet header.

Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agotests: don't depend on set element order
Florian Westphal [Thu, 17 Sep 2015 21:23:25 +0000 (23:23 +0200)] 
tests: don't depend on set element order

Pablo reported test failures because the order of returned set entries
is not deterministic.

This sorts set elements before comparision.
Patrick suggested to move ordering into libnftnl (since we could f.e.
also get duplicate entries due to how netlink dumps work), but thats a bit
more work.  Hence this quick workaround.

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
10 years agoBump version to v0.5 v0.5
Pablo Neira Ayuso [Wed, 16 Sep 2015 14:44:40 +0000 (16:44 +0200)] 
Bump version to v0.5

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