]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
8 years agotests: py: any: Make tests more generic by using other interfaces
Manuel Johannes Messner [Mon, 5 Sep 2016 18:20:13 +0000 (20:20 +0200)] 
tests: py: any: Make tests more generic by using other interfaces

Some tests use hard coded interface names and interface indexes.
This commit removes these cases by exchanging "eth0" with "dummy0" and
"lo" (depending on the test) in all ifname tests and by using "lo"
instead of "eth0" in all interface index tests (because we can assume
"lo" ifindex is 1).

Signed-off-by: Manuel Johannes Messner <manuel.johannes.messner@hs-furtwangen.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agotests: py: nft-tests.py: Add function for loading and removing kernel modules
Manuel Johannes Messner [Mon, 5 Sep 2016 18:20:12 +0000 (20:20 +0200)] 
tests: py: nft-tests.py: Add function for loading and removing kernel modules

Some tests use the dummy kernel module. This commit adds a function to
automatically load that module and remove it afterwards.

Signed-off-by: Manuel Johannes Messner <manuel.johannes.messner@hs-furtwangen.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agoevaluate: display expression, statement and command name on debug
Pablo Neira Ayuso [Wed, 31 Aug 2016 16:27:13 +0000 (18:27 +0200)] 
evaluate: display expression, statement and command name on debug

Extend debugging knob for evaluation to display the command, the
expression and statement names.

 # nft --debug=eval add rule x y ip saddr 1.1.1.1 counter
 <cmdline>:1:1-37: Evaluate add
 add rule x y ip saddr 1.1.1.1 counter
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 <cmdline>:1:14-29: Evaluate expression
 add rule x y ip saddr 1.1.1.1 counter
              ^^^^^^^^^^^^^^^^
 ip saddr $1.1.1.1

 <cmdline>:1:14-29: Evaluate relational
 add rule x y ip saddr 1.1.1.1 counter
              ^^^^^^^^^^^^^^^^
 ip saddr $1.1.1.1

 <cmdline>:1:14-21: Evaluate payload
 add rule x y ip saddr 1.1.1.1 counter
              ^^^^^^^^
 ip saddr

 <cmdline>:1:23-29: Evaluate symbol
 add rule x y ip saddr 1.1.1.1 counter
                       ^^^^^^^

 <cmdline>:1:23-29: Evaluate value
 add rule x y ip saddr 1.1.1.1 counter
                       ^^^^^^^
 1.1.1.1

 <cmdline>:1:31-37: Evaluate counter
 add rule x y ip saddr 1.1.1.1 counter
                               ^^^^^^^
 counter packets 0 bytes 0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Avoid undefined behaviour in concat_subtype_id()
Phil Sutter [Tue, 30 Aug 2016 17:39:52 +0000 (19:39 +0200)] 
evaluate: Avoid undefined behaviour in concat_subtype_id()

For the left side of a concat expression, dtype is NULL and therefore
off is 0. In that case the code expects to get a datatype of
TYPE_INVALID, but this is fragile as the output of concat_subtype_id()
is undefined for n > 32 / TYPE_BITS.

To fix this, call datatype_lookup() directly passing the expected
TYPE_INVALID as argument if off is 0.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: reject: Have a generic fix for missing network context
Phil Sutter [Tue, 30 Aug 2016 17:39:51 +0000 (19:39 +0200)] 
evaluate: reject: Have a generic fix for missing network context

Commit 17b495957b29e ("evaluate: reject: fix crash if we have transport
protocol conflict from inet") took care of a crash when using inet or
bridge families, but since then netdev family has been added which also
does not implicitly define the network context. Therefore the crash can
be reproduced again using the following example:

nft add rule netdev filter e1000-ingress \
meta l4proto udp reject with tcp reset

In order to fix this in a more generic way, have stmt_evaluate_reset()
fall back to the generic proto_inet_service irrespective of the actual
proto context.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: Fix datalen checks in expr_evaluate_string()
Phil Sutter [Tue, 30 Aug 2016 17:39:49 +0000 (19:39 +0200)] 
evaluate: Fix datalen checks in expr_evaluate_string()

I have been told that the flex scanner won't return empty strings, so
strlen(data) should always be greater 0. To avoid a hard to debug issue
though, add an assert() to make sure this is always the case before
risking an unsigned variable underrun.

A real issue though is the check for 'datalen - 1 >= 0', which will
never fail due to datalen being unsigned. Fix this by incrementing both
sides by one, hence checking 'datalen >= 1'.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agometa: fix memory leak in tc classid parser
Liping Zhang [Sun, 28 Aug 2016 08:36:22 +0000 (16:36 +0800)] 
meta: fix memory leak in tc classid parser

We forgot to free the str which was allocated by xstrdup,
so memory leak will happen.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: adapt netlink bytecode output of numgen and hash
Pablo Neira Ayuso [Tue, 30 Aug 2016 10:22:47 +0000 (12:22 +0200)] 
tests: py: adapt netlink bytecode output of numgen and hash

Adapt them to the revisited output string now in libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: allow variable references in set elements definition
Pablo Neira Ayuso [Mon, 29 Aug 2016 15:16:35 +0000 (17:16 +0200)] 
parser_bison: allow variable references in set elements definition

Andreas reports that he cannot use variables in set definitions:

define s-ext-2-int = 10.10.10.10 . 25, 10.10.10.10 . 143

        set s-ext-2-int {
                type ipv4_addr . inet_service
                elements =  { $s-ext-2-int }
        }

This syntax is not correct though, since the curly braces should be
placed in the variable definition itself, so we have context to handle
this variable as a list of set elements.

The correct syntax that works after this patch is:

define s-ext-2-int = { 10.10.10.10 . 25, 10.10.10.10 . 143 }

table inet forward {
         set s-ext-2-int {
                type ipv4_addr . inet_service
                elements = $s-ext-2-int
        }
}

Reported-by: Andreas Hainke <andreas.hainke@foteviken.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: add variable_expr rule
Pablo Neira Ayuso [Mon, 29 Aug 2016 15:15:47 +0000 (17:15 +0200)] 
parser_bison: add variable_expr rule

This patch adds a rule for variable expression so we can reuse it in a
follow up patch to allow set element initialization from variable.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: validate maximum hash and numgen value
Pablo Neira Ayuso [Mon, 29 Aug 2016 11:20:08 +0000 (13:20 +0200)] 
evaluate: validate maximum hash and numgen value

We can validate that values don't get over the maximum datatype
length, this is expressed in number of bits, so the maximum value
is always power of 2.

However, since we got the hash and numgen expressions, the user should
not set a value higher that what the specified modulus option, which
may not be power of 2. This patch extends the expression context with
a new optional field to store the maximum value.

After this patch, nft bails out if the user specifies non-sense rules
like those below:

 # nft add rule x y jhash ip saddr mod 10 seed 0xa 10
 <cmdline>:1:45-46: Error: Value 10 exceeds valid range 0-9
 add rule x y jhash ip saddr mod 10 seed 0xa 10
                                             ^^

The modulus sets a valid value range of [0, n), so n is out of the valid
value range.

 # nft add rule x y numgen inc mod 10 eq 12
 <cmdline>:1:35-36: Error: Value 12 exceeds valid range 0-9
 add rule x y numgen inc mod 10 eq 12
                                   ^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: add expr_evaluate_integer()
Pablo Neira Ayuso [Mon, 29 Aug 2016 10:49:35 +0000 (12:49 +0200)] 
evaluate: add expr_evaluate_integer()

Add a helper function to wrap the integer evaluation code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add hash expression
Pablo Neira Ayuso [Fri, 26 Aug 2016 16:00:00 +0000 (18:00 +0200)] 
src: add hash expression

This is special expression that transforms an input expression into a
32-bit unsigned integer. This expression takes a modulus parameter to
scale the result and the random seed so the hash result becomes harder
to predict.

You can use it to set the packet mark, eg.

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

You can combine this with maps too, eg.

 # nft add rule x y dnat to jhash ip saddr mod 2 seed 0xdeadbeef map { \
0 : 192.168.20.100, \
1 : 192.168.30.100 \
   }

Currently, this expression implements the jenkins hash implementation
available in the Linux kernel:

 http://lxr.free-electrons.com/source/include/linux/jhash.h

But it should be possible to extend it to support any other hash
function type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add numgen expression
Pablo Neira Ayuso [Fri, 26 Aug 2016 12:41:41 +0000 (14:41 +0200)] 
src: add numgen expression

This new expression allows us to generate incremental and random numbers
bound to a specified modulus value.

The following rule sets the conntrack mark of 0 to the first packet seen,
then 1 to second packet, then 0 again to the third packet and so on:

 # nft add rule x y ct mark set numgen inc mod 2

A more useful example is a simple load balancing scenario, where you can
also use maps to set the destination NAT address based on this new numgen
expression:

 # nft add rule nat prerouting \
dnat to numgen inc mod 2 map { 0 : 192.168.10.100, 1 : 192.168.20.200 }

So this is distributing new connections in a round-robin fashion between
192.168.10.100 and 192.168.20.200. Don't forget the special NAT chain
semantics: Only the first packet evaluates the rule, follow up packets
rely on conntrack to apply the NAT information.

You can also emulate flow distribution with different backend weights
using intervals:

 # nft add rule nat prerouting \
dnat to numgen inc mod 10 map { 0-5 : 192.168.10.100, 6-9 : 192.168.20.200 }

So 192.168.10.100 gets 60% of the workload, while 192.168.20.200 gets 40%.

We can also be mixed with dynamic sets, thus weight can be updated in
runtime.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add quota statement
Pablo Neira Ayuso [Fri, 26 Aug 2016 09:19:18 +0000 (11:19 +0200)] 
src: add quota statement

This new statement is stateful, so it can be used from flow tables, eg.

 # nft add rule filter input \
        flow table http { ip saddr timeout 60s quota over 50 mbytes } drop

This basically sets a quota per source IP address of 50 mbytes after
which packets are dropped. Note that the timeout releases the entry if
no traffic is seen from this IP after 60 seconds.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: py: adapt it to new add element command semantics
Pablo Neira Ayuso [Mon, 29 Aug 2016 17:50:45 +0000 (19:50 +0200)] 
tests: py: adapt it to new add element command semantics

Since fd33d96 ("src: create element command"), add element doesn't
fail anymore if the element exists, you have to use create instead in
case you want to check if the element already exists.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoinclude: refresh uapi/linux/netfilter/nf_tables.h copy
Pablo Neira Ayuso [Fri, 26 Aug 2016 11:22:00 +0000 (13:22 +0200)] 
include: refresh uapi/linux/netfilter/nf_tables.h copy

Fetch incremental incremental updates on this file.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: cover add and create set command
Pablo Neira Ayuso [Wed, 24 Aug 2016 14:45:07 +0000 (16:45 +0200)] 
tests: shell: cover add and create set command

This patch validates that creation of an already existing element
bails out with EEXIST.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: create element command
Pablo Neira Ayuso [Wed, 24 Aug 2016 14:45:06 +0000 (16:45 +0200)] 
src: create element command

This patch adds the create command, that send the NLM_F_EXCL flag so
nf_tables bails out if the element already exists, eg.

 # nft add element x y { 1.1.1.1 }
 # nft create element x y { 1.1.1.1 }
 <cmdline>:1:1-31: Error: Could not process rule: File exists
 create element x y { 1.1.1.1 }
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This update requires nf_tables kernel patches to honor the NLM_F_EXCL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: cover add and create set command
Pablo Neira Ayuso [Wed, 24 Aug 2016 14:14:51 +0000 (16:14 +0200)] 
tests: shell: cover add and create set command

This patch validates that creation of an already existing set bails out
with EEXIST.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add create set command
Pablo Neira Ayuso [Wed, 24 Aug 2016 13:58:57 +0000 (15:58 +0200)] 
src: add create set command

Add support for the 'create' command, we already support this in other
existing objects, so support this for sets too, eg.

 # nft add set x y { type ipv4_addr\; }
 # nft create set x y { type ipv4_addr\; }
 <cmdline>:1:1-35: Error: Could not process rule: File exists
 create set x y { type ipv4_addr; }
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 # nft add set x y { type ipv4_addr\; }
 #

This command sets the NLM_F_EXCL netlink flag, so if the object already
exists, nf_tables returns -EEXIST.

This is changing the existing behaviour of 'nft add set' which was
setting this flag, this is inconsistent with regards to the way other
objects behave.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: explicit indication on export ruleset
Pablo Neira Ayuso [Wed, 27 Jul 2016 10:57:57 +0000 (12:57 +0200)] 
parser_bison: explicit indication on export ruleset

This patch modifies the grammar to explicitly indicate what you want to
export, eg.

 # nft export ruleset json

This leaves room to extend this later on to support other object types,
such as integrating conntrack into nft.

This also leaves the syntax in consistent state wrt. other existing
objects. The existing syntax is still preserved.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: Simplify parser rule_spec tree
Carlos Falgueras García [Tue, 23 Aug 2016 07:40:45 +0000 (09:40 +0200)] 
src: Simplify parser rule_spec tree

This patch separates the rule identification from the rule localization,
so the logic moves from the evaluator to the parser. This allows to
revert the patch "evaluate: improve rule managment checks"
(4176c7d30c2ff1b3f52468fc9c08b8df83f979a8) and saves a lot of code.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: display bit number instead of raw value
Florian Westphal [Sun, 21 Aug 2016 22:27:48 +0000 (00:27 +0200)] 
ct: display bit number instead of raw value

... and add test cases for ct label.
Currently this dumped 'label 0x2', now 'label 1' would be shown.

This makes add/list behave the same.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: allow numeric conntrack labels
Florian Westphal [Wed, 17 Aug 2016 16:19:26 +0000 (18:19 +0200)] 
ct: allow numeric conntrack labels

When dumping labels in rule list we try to print a symbolic name.
If we don't find one, we print the bit number instead.

This changes nft to also allow use of the number instead of a name
when adding ct label rules so that such dumps can also be restored
again.

This is similar to other cases, e.g. skuid root vs skuid 0 which
are both valid.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: shell: add testcase for reject expr
Liping Zhang [Mon, 22 Aug 2016 15:43:53 +0000 (23:43 +0800)] 
tests: shell: add testcase for reject expr

Reject expr is only valid in input/forward/output chain,
and if user can add reject expr in prerouting chain, kernel
panic will happen.

So add a simple test case to cover this situation.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: keep snat/dnat existing syntax unchanged
Liping Zhang [Mon, 22 Aug 2016 15:34:15 +0000 (23:34 +0800)] 
parser_bison: keep snat/dnat existing syntax unchanged

We should keep existing syntax unchanged, and this was emphasized
in the commit 850f0a56b6ad ("src: add 'to' for snat and dnat")'s
commit log: "Existing syntax is still preserved, but the listing
shows the one including 'to'."

This problem was found by running shell test:
  # ./run-tests.sh
  [ ... ]
  W: [FAILED] ./testcases/maps/anonymous_snat_map_0
  I: [OK] ./testcases/maps/map_with_flags_0
  W: [FAILED] ./testcases/maps/named_snat_map_0
  [ ... ]

Fixes: 850f0a56b6ad ("src: add 'to' for snat and dnat")
Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: redirect to :port for consistency with nat/masq statement
Pablo Neira Ayuso [Tue, 16 Aug 2016 21:36:54 +0000 (23:36 +0200)] 
parser_bison: redirect to :port for consistency with nat/masq statement

Use the colon port syntax for consistency with other statements.
Existing syntax is still preserved but the output displays the colon.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: meta priority support using tc classid
Pablo Neira Ayuso [Tue, 16 Aug 2016 21:30:18 +0000 (23:30 +0200)] 
src: meta priority support using tc classid

This patch adds the missing bits to scan and parse the meta priority
handle as expressed by tc classid major:minor syntax.

The :minor syntax is not support for two reason: major is always >= 1
and this clashes with port syntax in nat.

Here below, several example on how to match the packet priority field:

   nft add rule filter forward meta priority abcd:0
   nft add rule filter forward meta priority abcd:1234

and to set it, you have to:

   nft add rule filter forward meta priority set abcd:1234

The priority expression in flex looks ahead to restrict the pattern to
avoid problems with mappings:

{classid}/[ \t\n:\-},]

So the following doesn't break:

   ... vmap { 25:accept }
              ^^^^^

The lookahead expression requires a slight change to extend the input
string in one byte.

This patch is conservative as you always have to explicity indicate
major and minor numbers even if zero.

We could consider supporting this shortcut in the future:

abcd:

However, with regards to this:

:abcd

We don't need to support it since major number is assumed to be >= 1.
However, if we ever decide to support this, we'll have problems since
this clashes with our port representation in redirect and mangle.

So let's keep this simple and start with this approach.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: simplify classid printing using %x instead of %04x
Pablo Neira Ayuso [Fri, 22 Jul 2016 14:45:57 +0000 (16:45 +0200)] 
src: simplify classid printing using %x instead of %04x

No need to print this in iptables CLASSIFY target format,
eg. 0004:1230, this is innecessarily large.

And always print major and minor numbers.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: rename datatype name from tc_handle to classid
Pablo Neira Ayuso [Fri, 22 Jul 2016 14:43:13 +0000 (16:43 +0200)] 
src: rename datatype name from tc_handle to classid

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: remove range expression
Pablo Neira Ayuso [Tue, 16 Aug 2016 21:29:11 +0000 (23:29 +0200)] 
scanner: remove range expression

This expression is not used anywhere in this scanner code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: allow strings starting by underscores and dots
Pablo Neira Ayuso [Tue, 16 Aug 2016 21:22:51 +0000 (23:22 +0200)] 
scanner: allow strings starting by underscores and dots

POSIX.1-2008 (which is simultaneously IEEE Std 1003.1-2008) says:

"The set of characters from which portable filenames are constructed.

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 . _ -"

On top of that it says:

"The <hyphen> character should not be used as the first character of a
portable user name."

This allows a bit more things that NAME_REGEX though, but this still
looks fine to me.

For more info, see:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_431
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoparser_bison: missing token string in QUOTED_ASTERISK and ASTERISK_STRING
Pablo Neira Ayuso [Mon, 15 Aug 2016 15:28:04 +0000 (17:28 +0200)] 
parser_bison: missing token string in QUOTED_ASTERISK and ASTERISK_STRING

<cmdline>:1:24-24: Error: syntax error, unexpected newline, expecting string or QUOTED_STRING or ASTERISK_STRING
add rule x y log prefix
                       ^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: support for RFC2732 IPv6 address format with brackets
Pablo Neira Ayuso [Tue, 16 Aug 2016 16:07:21 +0000 (18:07 +0200)] 
src: support for RFC2732 IPv6 address format with brackets

The statement:

dnat to 2001:838:35f:1:::80

is very confusing as it is not so easy to identify where address ends
and the port starts. This even harder to read with ranges.

So this patch adds squared brackets as RFC2732 to enclose the IPv6
address.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: add 'to' for snat and dnat
Pablo Neira Ayuso [Thu, 28 Jul 2016 18:03:53 +0000 (20:03 +0200)] 
src: add 'to' for snat and dnat

This is extra syntaxtic sugar to get this consistent with other
statements such as redirect, masquerade, dup and fwd that indicates
where to go.

Existing syntax is still preserved, but the listing shows the one
including 'to'.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agosrc: quote user-defined strings when used from rule selectors
Pablo Neira Ayuso [Mon, 15 Aug 2016 11:14:18 +0000 (13:14 +0200)] 
src: quote user-defined strings when used from rule selectors

The following selectors display strings using quotes:

* meta iifname
* meta oifname
* meta ibriport
* meta obriport

However, the following do not:

* meta oif
* meta iif
* meta skuid
* meta skgid
* meta iifgroup
* meta oifgroup
* meta rtclassid
* ct label

Given they refer to user-defined values, neither keywords nor internal
built-in known values, let's quote the output of this.

This patch modifies symbolic_constant_print() so we can signal this to
indicate if the string needs to be quoted.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoct: release ct_label table on exit
Pablo Neira Ayuso [Wed, 17 Aug 2016 11:13:03 +0000 (13:13 +0200)] 
ct: release ct_label table on exit

Just like we do with other symbol tables.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Florian Westphal <fw@strlen.de>
8 years agoct: add missing slash to connlabel path
Pablo Neira Ayuso [Wed, 17 Aug 2016 11:13:02 +0000 (13:13 +0200)] 
ct: add missing slash to connlabel path

If I configure nftables via:

./configure --prefix=/usr

the connlabel path breaks due to missing slash, so append this after
DEFAULT_INCLUDE_PATH.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Florian Westphal <fw@strlen.de>
8 years agotests: tests to include files
Pablo Neira Ayuso [Wed, 10 Aug 2016 15:46:16 +0000 (17:46 +0200)] 
tests: tests to include files

Four tests to cover file inclusion using:

1) Absolute path.
2) Relative path.
3) Default include directory path.

And one more test to cover endless file inclusion loop.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: don't break line on include error message
Pablo Neira Ayuso [Mon, 1 Aug 2016 21:41:27 +0000 (23:41 +0200)] 
scanner: don't break line on include error message

For consistency with other error messages in this codebase, don't add a
line break.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: don't fall back on current directory if include is not found
Pablo Neira Ayuso [Mon, 1 Aug 2016 21:26:22 +0000 (23:26 +0200)] 
scanner: don't fall back on current directory if include is not found

This resolves an ambiguity if the same file name is used both under
sysconfdir and the current working directory. You can use dot slash
./ to explicitly refer to files in the current working directory.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1040
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoscanner: honor absolute and relative paths via include file
Pablo Neira Ayuso [Mon, 1 Aug 2016 21:09:39 +0000 (23:09 +0200)] 
scanner: honor absolute and relative paths via include file

If the path refers to an absolute or relative path, do not check for the
default include paths, eg. /etc/nftables/.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1040
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: ip payload set support for ecn and dscp
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
tests: ip payload set support for ecn and dscp

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agonetlink: make checksum fixup work with odd-sized header fields
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
netlink: make checksum fixup work with odd-sized header fields

The kernel checksum functions want even-sized lengths except for
the last block at the end of the data.

This means that

nft --debug=netlink add rule filter output ip ecn set 1

must generate a two byte read and a two byte write:

[ payload load 2b @ network header + 0 => reg 1 ]
[ bitwise reg 1 = (reg=1 & 0x0000fcff ) ^ 0x00000100 ]
[ payload write reg 1 => 2b @ network header + 0 csum_type 1 csum_off 10 ]

Otherwise, while a one-byte write is enough, the kernel will
generate invalid checksums (unless checksum is offloaded).

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agotests: ip6 dscp, flowlabel and ecn test cases
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
tests: ip6 dscp, flowlabel and ecn test cases

Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agonetlink: decode payload statment
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
netlink: decode payload statment

This allows nft to display payload set operations if the
header isn't byte aligned or has non-byte divisible sizes.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: add support to set IPv6 non-byte header fields
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
evaluate: add support to set IPv6 non-byte header fields

'ip6 ecn set 1' will generate a zero-sized write operation.
Just like when matching on bit-sized header fields we need to
round up to a byte-sized quantity and add a mask to retain those
bits outside of the header bits that we want to change.

Example:

ip6 ecn set ce
  [ payload load 1b @ network header + 1 => reg 1 ]
  [ bitwise reg 1 = (reg=1 & 0x000000cf ) ^ 0x00000030 ]
  [ payload write reg 1 => 1b @ network header + 1 csum_type 0 csum_off 0 ]

1. Load the full byte containing the ecn bits
2. Mask out everything *BUT* the ecn bits
3. Set the CE mark

This patch only works if the protocol doesn't need a checksum fixup.
Will address this in a followup patch.

This also doesn't yet include the needed reverse translation.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agoevaluate: add small helper to check if payload expr needs binop adjustment
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
evaluate: add small helper to check if payload expr needs binop adjustment

kernel can only deal with byte-sized and byte-aligned payload
expressions.

If the payload expression doesn't fit this requirement userspace
has to add explicit binop masks to remove the unwanted part(s).

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 years agopayload: print base and raw values for unknown payloads
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
payload: print base and raw values for unknown payloads

We currently print 'unknown' rather than the raw offset values
for unrecognized header values.

If its unknown, prefer to print

payload @nh,0,16 set payload @nh,0,16

rather than 'unknown'.

Also add a helper to check if payload expression has a description
assigned to it.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 years agonetlink: add __binop_adjust helper
Florian Westphal [Mon, 1 Aug 2016 15:11:41 +0000 (17:11 +0200)] 
netlink: add __binop_adjust helper

binop_adjust takes an expression whose LHS is expected to be
the binop expression that we use to adjust a payload expression
based on a mask (to match sub-byte headers like iphdr->version).

A followup patch has to pass the binop directly, so add
add a helper for it.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: keep map flag around when flags are specified
Pablo Neira Ayuso [Wed, 27 Jul 2016 11:36:01 +0000 (13:36 +0200)] 
parser_bison: keep map flag around when flags are specified

If you add a map with timeouts, eg.

  # nft add table x
  # nft add map x y { type ipv4_addr : ipv4_addr\; flags timeout\; }

The listing shows a set instead of a map:

  # nft list ruleset
  table ip x {
          set y {
                  type ipv4_addr
                  flags timeout
          }
  }

This patch fixes the parser to keep the map flag around when timeout
flag (or any other flags) are specified.

This patch also comes with a regression test.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoxt: use struct xt_xlate_{mt,tg}_params
Pablo Neira Ayuso [Sun, 24 Jul 2016 10:54:27 +0000 (12:54 +0200)] 
xt: use struct xt_xlate_{mt,tg}_params

Adapt this code to the new interface that introduces
struct xt_xlate_{mt,tg}_params.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: add ether payload set test
Florian Westphal [Fri, 22 Jul 2016 10:08:47 +0000 (12:08 +0200)] 
tests: add ether payload set test

... and fix missing line in ip6 test.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agotests: add basic payload tests
Florian Westphal [Thu, 21 Jul 2016 13:57:15 +0000 (15:57 +0200)] 
tests: add basic payload tests

payload set operations should work at least for byte-sized
quantities >= 2 byte.

Before adding support for odd-sized writes (ecn, dscp, ip6 flowlabel
...) add a bunch of tests to cover current state.

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agoct: use nftables sysconf location for connlabel configuration
Florian Westphal [Wed, 20 Jul 2016 10:14:06 +0000 (12:14 +0200)] 
ct: use nftables sysconf location for connlabel configuration

Instead of using /etc/xtables use the nftables syconfdir.
Also update error message to tell which label failed translation
and which config file was used for this:

nft add filter input ct label foo
<cmdline>:1:27-29: Error: /etc/nftables/connlabel.conf: could not parse conntrack label "foo"

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agometa: add tests for meta random
Florian Westphal [Mon, 18 Jul 2016 22:06:12 +0000 (00:06 +0200)] 
meta: add tests for meta random

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agometa: add random support
Florian Westphal [Mon, 18 Jul 2016 22:06:08 +0000 (00:06 +0200)] 
meta: add random support

"meta random" fills a register with a 32bit pseudo-random number.

For instance one can now use

meta random <= 2147483647

... to match every 2nd packet on average.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: make sure split table definition works via nft -f
Pablo Neira Ayuso [Wed, 13 Jul 2016 14:53:12 +0000 (16:53 +0200)] 
tests: shell: make sure split table definition works via nft -f

Add test to cover split table definition in one single file.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: fix typo in symbol redefinition error reporting
Pablo Neira Ayuso [Wed, 13 Jul 2016 14:45:52 +0000 (16:45 +0200)] 
parser_bison: fix typo in symbol redefinition error reporting

"redefinition" instead of "redfinition".

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add xt compat support
Pablo Neira Ayuso [Tue, 12 Jul 2016 20:04:17 +0000 (22:04 +0200)] 
src: add xt compat support

At compilation time, you have to pass this option.

  # ./configure --with-xtables

And libxtables needs to be installed in your system.

This patch allows to list a ruleset containing xt extensions loaded
through iptables-compat-restore tool.

Example:

$ iptables-save > ruleset

$ cat ruleset
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -p tcp -m multiport --dports 80,81 -j REJECT
COMMIT

$ sudo iptables-compat-restore ruleset

$ sudo nft list rulseset
table ip filter {
    chain INPUT {
        type filter hook input priority 0; policy accept;
        ip protocol tcp tcp dport { 80,81} counter packets 0 bytes 0 reject
    }

    chain FORWARD {
        type filter hook forward priority 0; policy drop;
    }

    chain OUTPUT {
        type filter hook output priority 0; policy accept;
    }
}

A translation of the extension is shown if this is available. In other
case, match or target definition is preceded by a hash. For example,
classify target has not translation:

$ sudo nft list chain mangle POSTROUTING
table ip mangle {
    chain POSTROUTING {
        type filter hook postrouting priority -150; policy accept;
        ip protocol tcp tcp dport 80 counter packets 0 bytes 0 # CLASSIFY set 20:10
                                                              ^^^
    }
}

If the whole ruleset is translatable, the users can (re)load it using
"nft -f" and get nft native support for all their rules.

This patch is joint work by the authors listed below.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: expose delinearize/linearize structures and stmt_error()
Pablo Neira [Tue, 12 Jul 2016 20:04:16 +0000 (22:04 +0200)] 
src: expose delinearize/linearize structures and stmt_error()

Needed by the follow up xt compatibility layer patch.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoinclude: cache ip_tables.h, ip6_tables.h, arp_tables.h and ebtables.h
Pablo Neira Ayuso [Tue, 12 Jul 2016 20:04:15 +0000 (22:04 +0200)] 
include: cache ip_tables.h, ip6_tables.h, arp_tables.h and ebtables.h

The xt over nft support that comes in follow up patches need this, and update
the corresponding Makefile.am.

Based on patch from Arturo Borrero Gonzalez.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_linearize: skip NFTNL_EXPR_DYNSET_TIMEOUT attribute if timeout is unset
Pablo Neira Ayuso [Tue, 12 Jul 2016 15:00:43 +0000 (17:00 +0200)] 
netlink_linearize: skip NFTNL_EXPR_DYNSET_TIMEOUT attribute if timeout is unset

Otherwise kernel bails out with EINVAL in case that the sets got no
timeout flag.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: restore parsing of dynamic set element updates
Pablo Neira Ayuso [Tue, 12 Jul 2016 14:41:59 +0000 (16:41 +0200)] 
parser_bison: restore parsing of dynamic set element updates

Add a new set_elem_expr_stmt production to handle dynamic set element
updates from rules.

Quickly tested this here through:

 # nft add table filter
 # nft add chain filter input { type filter hook input priority 0\; }
 # nft add set filter myset { type inet_service\; flags timeout\; }
 # nft add rule filter input set add tcp sport timeout 60s @myset
 # nft list ruleset
 table ip filter {
        set myset {
                type inet_service
                flags timeout
                elements = { http expires 9s}
        }

        chain input {
                type filter hook input priority 0; policy accept;
                set add tcp dport timeout 1m @myset
        }
 }

Fixes: a3e60492a684 ("parser: restrict relational rhs expression recursion")
Reported-by: Anders K. Pedersen <akp@akp.dk>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodatatype: time_type should send milliseconds to userspace
Pablo Neira Ayuso [Fri, 8 Jul 2016 13:12:31 +0000 (15:12 +0200)] 
datatype: time_type should send milliseconds to userspace

Kernel expects milliseconds, so fix this datatype to use
milliseconds instead of seconds.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonft: configure.ac: Replace magic dblatex dep.
Nicholas Vinson [Sat, 2 Jul 2016 18:34:20 +0000 (11:34 -0700)] 
nft: configure.ac: Replace magic dblatex dep.

Add a configure switch to enable and disable PDF document generation.
This switch is to replace the current method of automatically detecting
dblatex and building the PDF documentation when present.

Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: run-tests.sh: use src/nft binary by default
Arturo Borrero [Thu, 23 Jun 2016 11:45:44 +0000 (13:45 +0200)] 
tests: shell: run-tests.sh: use src/nft binary by default

Better use the local tree nft binary rather than the installed one.

Requested-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: cover transactions via nft -f using flat syntax
Pablo Neira Ayuso [Wed, 22 Jun 2016 11:18:56 +0000 (13:18 +0200)] 
tests: shell: cover transactions via nft -f using flat syntax

This patch covers transactions using the flat syntax representation, eg.

add table x
add chain x y { type filter hook forward priority 0; }
add chain x y { policy drop; }

This also covers things like:

add element x whitelist { 1.1.1.1 }
delete element x whitelist { 1.1.1.1 }

The one above may look silly from a human behaviour point of view, but
silly robots may very well behave like this.

These tests require several kernel patches though in order to pass
successfully.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
9 years agosegtree: don't check for overlaps if set definition is empty
Pablo Neira Ayuso [Tue, 21 Jun 2016 15:32:52 +0000 (17:32 +0200)] 
segtree: don't check for overlaps if set definition is empty

If the set comes without definition (ie. no elements) then skip check
for overlaps since set->init is NULL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: use new definitions from libnftnl
Pablo Neira Ayuso [Wed, 15 Jun 2016 10:10:40 +0000 (12:10 +0200)] 
src: use new definitions from libnftnl

Use new definitions in libnftnl, so we can consider getting rid of them
at some point.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: cleanup tempfile handling in testcases/sets/cache_handling_0
Arturo Borrero [Sat, 11 Jun 2016 10:25:59 +0000 (12:25 +0200)] 
tests: shell: cleanup tempfile handling in testcases/sets/cache_handling_0

It uses a bogus pattern which was cleaned up already in others testscases,
and this is a leftover.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: delete unused variable in run-tests.sh
Arturo Borrero [Sat, 11 Jun 2016 10:19:17 +0000 (12:19 +0200)] 
tests: shell: delete unused variable in run-tests.sh

This ${TESTS_OUTPUT} variable is empty. Delete it.

It was probably an idea about dinamically redirecting testscases output.

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 endless jump loop tests
Liping Zhang [Mon, 13 Jun 2016 12:53:55 +0000 (20:53 +0800)] 
tests: shell: add endless jump loop tests

Add some tests for endless jump loop validation.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: make testcases which using tcp/udp port more rubost
Liping Zhang [Sat, 11 Jun 2016 05:05:14 +0000 (13:05 +0800)] 
tests: shell: make testcases which using tcp/udp port more rubost

In my mechain, port 12345 is mapped to italk in /etc/services:
  italk           12345/tcp               # Italk Chat System

So when we add nft rule with udp port "12345", nft list ruleset
will displayed it as "italk", that cause the result is not same
with expected, then testcase fail.

Add "-nn" option when dump the rulesets from the kernel, make
testcases which using tcp/udp port more rubost.

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Acked-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agopayload: don't update protocol context if we can't find a description
Florian Westphal [Mon, 6 Jun 2016 19:52:28 +0000 (21:52 +0200)] 
payload: don't update protocol context if we can't find a description

Since commit
20b1131c07acd2fc ("payload: fix stacked headers protocol context tracking")
we deref null pointer if we can't find a description for the desired
protocol, so "ip protocol 254" crashes while testing protocols 6 or 17
(tcp, udp) works.

Also add a test case for this.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1072
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodoc: Update datatypes
Laura Garcia Liebana [Sat, 4 Jun 2016 22:33:31 +0000 (00:33 +0200)] 
doc: Update datatypes

Fix references to obsolete datatypes in the manpage. We renamed them at
some point in the development but we forgot to update this
documentation.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodoc: fix old parameters and update datatypes v0.6
Laura Garcia Liebana [Wed, 1 Jun 2016 22:25:13 +0000 (00:25 +0200)] 
doc: fix old parameters and update datatypes

Fix old identifiers like 'ipcomp' and 'op' with 'comp' and 'operation'
instead. Update some FIXME datatypes.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoBump version to v0.6
Pablo Neira Ayuso [Wed, 1 Jun 2016 09:37:30 +0000 (11:37 +0200)] 
Bump version to v0.6

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: fix "list set" unexpected behaviour
Pablo M. Bermudo Garay [Wed, 1 Jun 2016 10:16:51 +0000 (12:16 +0200)] 
evaluate: fix "list set" unexpected behaviour

Special sets like maps and flow tables have their own commands to be
listed and inspected.

Before this patch, "nft list set" was able to display these special sets
content:

  # nft list set filter test
  table ip filter {
          map test {
                  type ipv4_addr : inet_service
                  elements = { 192.168.1.101 : http-alt}
          }
  }

Now an error is shown:

  # nft list set filter test
  <cmdline>:1:1-20: Error: Could not process rule: Set 'test' does not exist
  list set filter test
  ^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodoc: fix compression parameter index
Laura Garcia Liebana [Tue, 31 May 2016 19:16:32 +0000 (21:16 +0200)] 
doc: fix compression parameter index

Fix the compression parameter index 'cpi' instead of 'cfi'.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add support for display maps content
Pablo M. Bermudo Garay [Tue, 31 May 2016 10:37:07 +0000 (12:37 +0200)] 
src: add support for display maps content

This commit adds a new command that displays the definition of a single
map:

  # nft list map [family] <table> <map>

If no family is specified, ip is assumed.

Example:

  # nft list map ip6 filter test

  table ip6 filter {
          map test {
                  type ipv6_addr : inet_service
                  elements = { 2001:db8::ff00:42:8329 : http}
          }
  }

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add 'list maps' support
Pablo M. Bermudo Garay [Tue, 31 May 2016 10:37:06 +0000 (12:37 +0200)] 
src: add 'list maps' support

This commit adds a new command that lists maps:

  # nft list maps [family]

Only the declaration is displayed. If no family is specified, all maps
of all families are listed.

Example:

  # nft list maps

  table ip filter {
          map test {
                  type ipv4_addr : inet_service
          }
  }
  table ip6 filter {
          map test {
                  type ipv6_addr : inet_service
          }
  }

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: cap comment length to 128 bytes
Carlos Falgueras García [Mon, 30 May 2016 16:35:40 +0000 (18:35 +0200)] 
parser: cap comment length to 128 bytes

Pablo rewrites this description to:

"The user data area available is 256 bytes (NFT_USERDATA_MAXLEN). We plan
to allow storing other useful information such as datatypes in set
elements, so make sure there is room for this."

Example:
> nft add table t
> nft add chain t c
> nft add rule t c ip saddr 1.1.1.1 counter comment "abc...xyz" # len > 128
<cmdline>:1:47-N: Error: Comment too long. 128 characters maximum allowed
add rule t c ip saddr 1.1.1.1 counter comment abc...xyz
                                              ^^^^^^^^^

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: Consolidate comment production
Carlos Falgueras García [Mon, 30 May 2016 16:35:39 +0000 (18:35 +0200)] 
parser: Consolidate comment production

Use comment_spec both from rule and set element productions.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser: fix crash if we add a chain with an error chain type
Liping Zhang [Sun, 29 May 2016 11:25:37 +0000 (19:25 +0800)] 
parser: fix crash if we add a chain with an error chain type

If we add a chain and specify the nonexistent chain type, chain_type_name_lookup
will return a NULL pointer, and meet the assert condition in xstrdup.
Fix crash like this:

  # nft add chain filter input {type none hook input priority 0\;}
  nft: utils.c:63: xstrdup: Assertion `s != ((void *)0)' failed.
  Aborted (core dumped)

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agometa: fix a format error display when we set priority to root or none
Liping Zhang [Sun, 29 May 2016 10:08:09 +0000 (18:08 +0800)] 
meta: fix a format error display when we set priority to root or none

Also delete the redundant '\n'.
This fixes:

  # nft add rule filter test meta priority set root
  # nft list chain filter test
  table ip filter {
      chain test {
          meta priority set root
  none
  ffff:ffff
      }
  }

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agometa: fix endianness in priority
Liping Zhang [Sun, 29 May 2016 10:08:08 +0000 (18:08 +0800)] 
meta: fix endianness in priority

For example, after we add rule to set priority 1:2, it will be displayed in network
byte order as 0200:0100, this is wrong:

  # nft add rule filter test meta priority set 1:2
  # nft list chain filter test
  table ip filter {
      chain test {
          meta priority set 0200:0100
      }
  }

Signed-off-by: Liping Zhang <liping.zhang@spreadtrum.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset_elem: Use libnftnl/udata to store set element comment
Carlos Falgueras García [Fri, 27 May 2016 14:56:55 +0000 (16:56 +0200)] 
set_elem: Use libnftnl/udata to store set element comment

The set element comment is stored in nftnl_set_elem->user.data using
libnftnl/udata infrastructure. This allows store multiple variable length
user data into set element.

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinerize: don't use meta_match_postprocess for ct pp
Florian Westphal [Mon, 14 Mar 2016 12:56:43 +0000 (13:56 +0100)] 
netlink_delinerize: don't use meta_match_postprocess for ct pp

meta_match_postprocess uses meta.base which is only accessible if
left expression has EXPR_META type, so we can't use it to handle ct postprocessing.

To reduce copy-pastry factor the common part into ct_meta_common_postprocess(),
then call that from both meta and ct postprocessing.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_linearize: do not duplicate user data when linearizing user data
Carlos Falgueras García [Tue, 17 May 2016 16:00:16 +0000 (18:00 +0200)] 
netlink_linearize: do not duplicate user data when linearizing user data

Otherwise, we memory leak this area since nftnl_rule_set_data() now
makes a copy of the user data which receives. This is happening since
libnftnl's ("rule: Fix segfault due to invalid free of rule user data"),
it is not necessary make a copy before call it.

Note: Carlos originally posted this patch under the name of ("nftables:
Fix memory leak linearizing user data").

Signed-off-by: Carlos Falgueras García <carlosfg@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: add support for display flow tables content
Pablo M. Bermudo Garay [Fri, 20 May 2016 12:55:12 +0000 (14:55 +0200)] 
rule: add support for display flow tables content

This commit adds a new command that displays the definition of a single
flow table:

If no family is specified, ip is assumed.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: add 'list flow tables' support
Pablo M. Bermudo Garay [Thu, 19 May 2016 12:46:03 +0000 (14:46 +0200)] 
rule: add 'list flow tables' support

This commit adds a new command that lists flow tables:

    # nft list flow tables [family]

Only the declaration is displayed. If no family is specified, all flow
tables of all families are listed.

Signed-off-by: Pablo M. Bermudo Garay <pablombg@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: fix crash if we add an error format rule
Liping Zhang [Sat, 14 May 2016 12:43:35 +0000 (20:43 +0800)] 
evaluate: fix crash if we add an error format rule

If we add a such nft rule:
  nft add rule filter input ip protocol icmp tcp dport 0

we will always meet the assert condition:
  nft: evaluate.c:536: resolve_protocol_conflict: Assertion `base < (__PROTO_BASE_MAX - 1)' failed.
  Aborted (core dumped)

Signed-off-by: Florian Westphal <fw@strlen.de>
9 years agotests/py: add tests for frag more-fragments and frag reserved2
Pablo Neira Ayuso [Fri, 13 May 2016 20:00:00 +0000 (22:00 +0200)] 
tests/py: add tests for frag more-fragments and frag reserved2

While at it, get rid of bug comments on ip6/frag.t, since they are not
valid anymore.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoinclude: constify nlexpr field in location structure
Pablo Neira Ayuso [Fri, 13 May 2016 17:38:33 +0000 (19:38 +0200)] 
include: constify nlexpr field in location structure

The location shouldn't ever alter the expression.

And this fixes this compilation warning:

netlink_delinearize.c: In function ‘netlink_parse_expr’:
netlink_delinearize.c:1008:10: warning: assignment discards ‘const’ qualifier from pointer target type
  loc.nle = nle;
          ^
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoparser_bison: update flow table syntax
Pablo Neira Ayuso [Fri, 13 May 2016 17:28:44 +0000 (19:28 +0200)] 
parser_bison: update flow table syntax

Before we release next nft version, update the syntax to wrap the flow
table definition between brackets, eg.

 # nft add rule filter input tcp dport 22 ct state new \
flow table ssh { ip saddr limit rate 10/second }

 # nft add rule filter input \
        flow table acct { iif . ip saddr timeout 60s counter }

When playing around with this in your initial patchset I found very
confusing that it may not look obvious to users that they can only use
one single statement.

For example:

 # nft add rule filter input \
        flow table acct iif . ip saddr timeout 60s counter limit rate 10/second
                                                           ~~~~~~~~~~~~~~~~~~~~

Note that this limit rate applies globally, so this patch resolves this
ambiguity.

This may cause us problems in the future too if we extend this to
support more than one single statement per flowtable entry (Not
telling we need this now, but if someone comes up with a useful
usecase, we should be capable of extending this).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add flow statement
Patrick McHardy [Wed, 27 Apr 2016 11:29:50 +0000 (12:29 +0100)] 
src: add flow statement

The flow statement allows to instantiate per flow statements for user
defined flows. This can so far be used for per flow accounting or limiting,
similar to what the iptables hashlimit provides. Flows can be aged using
the timeout option.

Examples:

 # nft filter input flow ip saddr . tcp dport limit rate 10/second
 # nft filter input flow table acct iif . ip saddr timeout 60s counter

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agostmt: support generating stateful statements outside of rule context
Patrick McHardy [Wed, 27 Apr 2016 11:29:49 +0000 (12:29 +0100)] 
stmt: support generating stateful statements outside of rule context

The flow statement contains a stateful per flow statement, which is not
directly part of the rule. Allow generating these statements without adding
them to the rule and mark the supported statements using a new flag
STMT_F_STATEFUL.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink_delinearize: support parsing statements not contained within a rule
Patrick McHardy [Wed, 27 Apr 2016 11:29:48 +0000 (12:29 +0100)] 
netlink_delinearize: support parsing statements not contained within a rule

Return the parsed statement instead of adding it to the rule in order to
parse statements contained in the flow statement.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>