]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
10 years agostmt: rename nat "random-fully" option to "fully-random"
Patrick McHardy [Thu, 11 Dec 2014 22:06:48 +0000 (22:06 +0000)] 
stmt: rename nat "random-fully" option to "fully-random"

Use proper english for full randomization option.

Signed-off-by: Patrick McHardy
10 years agodtype: remove unnecessary icmp* parse/print functions
Patrick McHardy [Wed, 10 Dec 2014 22:13:51 +0000 (22:13 +0000)] 
dtype: remove unnecessary icmp* parse/print functions

Just setting the .sym_tbl correctly is all we need.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agorule: fix segmentation faults on kernels without nftables support
Pablo Neira Ayuso [Mon, 8 Dec 2014 19:03:42 +0000 (20:03 +0100)] 
rule: fix segmentation faults on kernels without nftables support

 # nft list sets
 Segmentation fault

 # nft list sets
 <cmdline>:1:1-9: Error: Could not receive sets from kernel: Protocol error
 list sets
 ^^^^^^^^^

Fix same bug in `nft list tables'.

Don't cleanup the table object for these commands since it is NULL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink: fix listing of range set elements in host byteorder
Pablo Neira Ayuso [Sun, 7 Dec 2014 23:25:13 +0000 (00:25 +0100)] 
netlink: fix listing of range set elements in host byteorder

We have to switch the byteorder of the element in
netlink_delinearize_setelem() for non-range values only.

This fixes the listing of:

  nft add rule filter input ct mark { 0x10-0x20 } counter

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink_delinearize: fix listing of set members in host byteorder using integer_type
Pablo Neira Ayuso [Mon, 8 Dec 2014 21:14:09 +0000 (22:14 +0100)] 
netlink_delinearize: fix listing of set members in host byteorder using integer_type

 nft list table filter
 ...
        cpu { 5033164833554432, 0, 16777216} counter packets 8 bytes 344

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: generate set members using integer_type in the appropriate byteorder
Pablo Neira Ayuso [Mon, 8 Dec 2014 19:26:30 +0000 (20:26 +0100)] 
src: generate set members using integer_type in the appropriate byteorder

Rules with header fields that rely on the generic integer datatype
from sets are not matching, eg.

 nft add rule filter input udp length { 9 } counter

This set member is an integer represented in host byte order, which
obviously doesn't match the header field (in network byte order).

Since the integer datatype has no specific byteorder, we have to rely
on the expression byteorder instead when configuring the context,
before we evaluate the list of set members.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: fix endianess issue in payload_expr_pctx_update()
Pablo Neira Ayuso [Thu, 4 Dec 2014 13:18:59 +0000 (14:18 +0100)] 
payload: fix endianess issue in payload_expr_pctx_update()

Use constant_data_ptr() to point to the right memory position on
big endian when exporting data that is stored in a larger variable.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Tested-by: Christophe Leroy <christophe.leroy@c-s.fr>
10 years agodatatype: fix name of icmp* code
Eric Leblond [Tue, 2 Dec 2014 23:02:51 +0000 (00:02 +0100)] 
datatype: fix name of icmp* code

The name of datatypes can be used in set definition so they should
follow the same logic (and maybe not contain space to avoid problem
with parsing).

This patch adds an underscore to the name of the icmp* code
datatype.

Signed-off-by: Eric Leblond <eric@regit.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoscanner: don't bug on too large values
Pablo Neira Ayuso [Sun, 30 Nov 2014 21:52:20 +0000 (22:52 +0100)] 
scanner: don't bug on too large values

Add a new ERROR symbol to handle scanning of too large values.

 <cmdline>:1:36-99: Error: bad value '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
 add rule ip test-ip4 input ct mark 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
instead of:

 BUG: nft: scanner.l:470: nft_lex: Assertion `0' failed.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: any/ct: remove wrong output
Pablo Neira Ayuso [Sun, 30 Nov 2014 20:56:46 +0000 (21:56 +0100)] 
tests: regression: any/ct: remove wrong output

ct mark 0x32-0x45

displays:

ct mark >= 0x00000032 ct mark <= 0x00000045
                                 ^^^^^^^^^^
instead of ct mark <= 0x45000000
                      ^^^^^^^^^^

Remove the custom output so this displays a warning. nft should
(at some point) merge the two statements into one single to express
the range from the netlink_delinearize step.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: fix "Listing is broken" instead of output mismatch
Pablo Neira Ayuso [Sun, 30 Nov 2014 20:39:24 +0000 (21:39 +0100)] 
tests: regression: fix "Listing is broken" instead of output mismatch

If the output string doesn't match the input, indicate that the output
mismatches instead of the misleading "Listing is broken".

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agometa: set base field on clones
Pablo Neira Ayuso [Sun, 30 Nov 2014 21:27:27 +0000 (22:27 +0100)] 
meta: set base field on clones

Set missing field on meta expression clone.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink_delinearize: clone on netlink_get_register(), release previous on _set()
Pablo Neira Ayuso [Mon, 24 Nov 2014 18:25:23 +0000 (19:25 +0100)] 
netlink_delinearize: clone on netlink_get_register(), release previous on _set()

If we add this rule:

  nft add rule filter input meta length 33-55

the listing shows:

  meta length >= 33 meta length <= 754974720

The two meta statements share the same left-hand side, thus, only the
first one is converted from network byte order to host byte order.

Update netlink_get_register() to return a clone so each left-hand side
has its own left-hand side.

Moreover, release the existing register before overriding it with fresh
expressions in netlink_set_register().

Thefore, if you manipulate a register from any of the existing parse
functions, you have to re-set it again to place fresh modified clone.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoscanner: fix reading of really long line
Eric Leblond [Sat, 29 Nov 2014 16:24:38 +0000 (17:24 +0100)] 
scanner: fix reading of really long line

Current code is causing a failure in adding a set containing
a really long list of elements. The failure occurs as soon as
the line is longer than flex read buffer.

When a line is longer than scanner buffer size, the code in YY_INPUT
forces a rewind to the beginning of the string because it does not
find a end of line. The result is that the string is never parsed.

This patch updates the code by rewinding till we found a space.

Signed-off-by: Eric Leblond <eric@regit.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: relax datatype check in integer_type_parse()
Pablo Neira Ayuso [Fri, 28 Nov 2014 20:20:59 +0000 (21:20 +0100)] 
datatype: relax datatype check in integer_type_parse()

Otherwise parsing with basetypes doesn't work. Now nft displays
an error when the symbolic constant is not correct:

 <cmdline>:1:29-31: Error: Could not parse conntrack state
 add rule test test ct state xxx accept
                             ^^^

Use .sym_tbl instead and default on the symbol_constant_parse()
function from the ethertype and pkttype, this simplifies the code and
(more importantly) it avoids a breakage after the change in
integer_type_parse().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: fix crash when using basetype instead of symbolic constants
Pablo Neira Ayuso [Fri, 28 Nov 2014 18:04:21 +0000 (19:04 +0100)] 
datatype: fix crash when using basetype instead of symbolic constants

The following example:

 # nft add rule filter input ct state 8 accept
 Segmentation fault

leads to a crash because we have the following datatype relation:

 ct_state -> bitmask -> integer

The bitmask, which is an intermediate basetype, has no parse()
function, this leads to a crash in symbolic_constant_parse().

Patrick suggested to walk down the chain until we find a parser
function.

Reported-by: leroy christophe <christophe.leroy@c-s.fr>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: fix crash on NULL location with bridge and tcp reset
Alvaro Neira [Wed, 26 Nov 2014 11:07:51 +0000 (12:07 +0100)] 
evaluate: reject: fix crash on NULL location with bridge and tcp reset

If we use tcp reset with a network protocol that tcp is not supported,
we display an error. This error use the reject.expr location which is NULL,
therefore we have a crash. This patch replaces it using the reject statement
to display the error like:

Rule:
 nft add bridge filter input ether type vlan reject with tcp reset
Output:
 <cmdline>:1:46-51: Error: cannot reject this ether type
 add rule bridge filter input ether type vlan reject with tcp reset
                              ~~~~~~~~~~~~~~~ ^^^^^^

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: add missing \ in src/Makefile.am (AM_CPPFLAGS)
David Kozub [Tue, 25 Nov 2014 17:51:43 +0000 (18:51 +0100)] 
build: add missing \ in src/Makefile.am (AM_CPPFLAGS)

The missing \ at the end of the line causes LIBMNL_CFLAGS and LIBNFTNL_CFLAGS
to be ignored. This causes build failure if the libmnl or libnftnl headers are
not in a path that's already searched by the C compiler.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: test masquerade from nat/postrouting too
Pablo Neira Ayuso [Mon, 24 Nov 2014 11:49:52 +0000 (12:49 +0100)] 
tests: regression: test masquerade from nat/postrouting too

We can specify several chains in the tests, so test this from
postrouting too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: fix bogus error due to bash
Pablo Neira Ayuso [Sun, 23 Nov 2014 21:05:12 +0000 (22:05 +0100)] 
tests: regression: fix bogus error due to bash

This suppresses several superfluous errors:

 any/meta.t: ERROR: line 168: nft add rule ip test-ip4 input meta iifgroup {11,33}: This rule should not have failed.
 any/meta.t: ERROR: line 178: nft add rule ip test-ip4 input meta oifgroup {11,33}: This rule should not have failed.
 ip/masquerade.t: ERROR: line 23: nft add rule ip4 test-ip4 output tcp dport {1,2,3,4,5,6,7,8,101,202,303,1001,2002,3003} masquerade: This rule should not have failed.
 ip6/masquerade.t: ERROR: line 23: nft add rule ip6 test-ip6 output tcp dport {1,2,3,4,5,6,7,8,101,202,303,1001,2002,3003} masquerade: This rule should not have failed.

This needs a space before the list of elements in the set, otherwise
bash here misinterprets the set.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: don't use -nnn for non-list commands
Pablo Neira Ayuso [Sun, 23 Nov 2014 20:48:05 +0000 (21:48 +0100)] 
tests: regression: don't use -nnn for non-list commands

Not useful, they just bloat the nft-tests.py output.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: any/queue.t: use new syntax
Pablo Neira Ayuso [Sun, 23 Nov 2014 20:46:30 +0000 (21:46 +0100)] 
tests: regression: any/queue.t: use new syntax

queue options are now expressed as flags, so you have to use
comma separated values.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: restore named vmap
Pablo Neira Ayuso [Sun, 23 Nov 2014 19:13:03 +0000 (20:13 +0100)] 
parser: restore named vmap

For example:

nft add map filter my_vmap { type ipv4_addr : verdict\; }
nft add element filter my_vmap { 1.0.0.0 : drop}
nft add rule filter input ip saddr vmap @my_vmap

Reported-by: Bjørnar Ness <bjornar.ness@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: restore nft --debug
Pablo Neira Ayuso [Sun, 23 Nov 2014 19:55:24 +0000 (20:55 +0100)] 
src: restore nft --debug

Add -DDEBUG to enable --debug option by default as it used to be before
the autotools conversion.

Fixes: 5fa8e49 ("build: autotools conversion")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink: don't bug on unknown events
Pablo Neira Ayuso [Fri, 14 Nov 2014 17:36:14 +0000 (18:36 +0100)] 
netlink: don't bug on unknown events

We will likely have new ones in the future, silently skip them. If the
user wants to see them, it just needs to upgrade.

This is currently causing us problems with kernels that deliver the
generation counter event.

term1# nft add table test

term2# nft monitor
nft: netlink.c:2063: netlink_events_cb: Assertion `0' failed.
Aborted

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: add autotools support for the 'files' subdir
Giorgio Dal Molin [Wed, 12 Nov 2014 16:41:06 +0000 (17:41 +0100)] 
build: add autotools support for the 'files' subdir

Added support to install some 'nft' scripts under
'${sysconfdir}/nftables', typically '/etc/nftables'.

Signed-off-by: Giorgio Dal Molin <giorgio.nicole@arcor.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: add autotools support for the 'doc' subdir
Giorgio Dal Molin [Wed, 12 Nov 2014 16:41:05 +0000 (17:41 +0100)] 
build: add autotools support for the 'doc' subdir

'nft' documentation is originally contained in the XML file 'doc/nft.xml'.
Processing this file with the proper tools we can obtain a PDF document,
 'nft.pdf', and a unix man page, 'nft.8'.

To produce the PDF we need the tool 'dblatex' (current release
pypi.python.org/pypi/dblatex/0.3.5).

To produce the man page we use the tool 'docbook2man'; it is part of the
package 'docbook2X' (docbook2x.sourceforge.net). On some linux
distributions the tool can have slightly different names as 'docbook2x-man'
or 'db2x_docbook2man' so we search for all three names and use the first
one found and issue the command:

 # ${DB2MAN} --xinclude $<

Signed-off-by: Giorgio Dal Molin <giorgio.nicole@arcor.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: autotools conversion
Pablo Neira Ayuso [Thu, 6 Nov 2014 16:09:44 +0000 (17:09 +0100)] 
build: autotools conversion

1) This removes former Makefiles and install-sh (which is now
   automagically imported via autoreconf).

 Makefile.defs.in
 Makefile.in
 Makefile.rules.in
 src/Makefile.in
 install-sh (now automagically imported via autoreconf).

2) CFLAGS are left almost same, they are integrated into Make_global.am.
   Use AM_CPPFLAGS to set the CFLAGS set by pkgconfig.

3) Add m4 directory to the tree which only contains the .gitignore
   file. Update .gitignore file to skip autogenerated files.

4) include <config.h> whenever required.

5) Minor adjustments to scanner.l and parser_bison.y to compile cleanly
   with autotools.

6) Add %option outfile=lex.yy.c to scanner.l, otherwise I hit this error
   here:

        gcc -DHAVE_CONFIG_H -I. -I..  -I../include -DDEFAULT_INCLUDE_PATH="\"/usr/etc\""  -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wdeclaration-after-statement -Wsign-compare -Winit-self -Wformat-nonliteral -Wformat-security -Wmissing-format-attribute -Wcast-align -Wundef -Wbad-function-cast -g -O2 -MT mnl.o -MD -MP -MF $depbase.Tpo -c -o mnl.o mnl.c &&\
        mv -f $depbase.Tpo $depbase.Po
/bin/sh ../build-aux/ylwrap scanner.l lex.yy.c scanner.c -- flex
make[3]: *** [scanner.c] Error 1
make[3]: Leaving directory `/home/pablo/devel/scm/git-netfilter/nftables/src'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/home/pablo/devel/scm/git-netfilter/nftables/src'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/pablo/devel/scm/git-netfilter/nftables'
make: *** [all] Error 2

7) Add Makefile.am for include/ (contributed by Giorgio Dal Molin).

The doc/ and files/ conversion to automake will come in follow up
patches but 'make distcheck' already works.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoinclude: add cli.h
Pablo Neira Ayuso [Thu, 6 Nov 2014 17:28:33 +0000 (18:28 +0100)] 
include: add cli.h

Needed by follow up patches to use autotools.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorename parser.y to parser_bison.y
Pablo Neira Ayuso [Thu, 6 Nov 2014 12:34:48 +0000 (13:34 +0100)] 
rename parser.y to parser_bison.y

The conversion to the autotools need this.

Make sure you remove the autogenerated parser.c and parser.h from
your tree.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: use AC_PROG_YACC and AM_PROG_LEX
Pablo Neira Ayuso [Thu, 6 Nov 2014 11:45:32 +0000 (12:45 +0100)] 
build: use AC_PROG_YACC and AM_PROG_LEX

To check for bison and flex installed on the system.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: use PKG_CHECK_MODULES to check for libmnl and libnftnl
Pablo Neira Ayuso [Thu, 6 Nov 2014 11:43:43 +0000 (12:43 +0100)] 
build: use PKG_CHECK_MODULES to check for libmnl and libnftnl

Also check for required library versions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add cgroup support in meta expresion
Ana Rey [Mon, 3 Nov 2014 17:10:51 +0000 (18:10 +0100)] 
src: Add cgroup support in meta expresion

The new attribute of meta is "cgroup".

Example of use in nft:

 # nft add rule ip test output meta cgroup != 0x100001 counter drop

Moreover, this adds tests to the meta.t test file.

The kernel support is addedin the commit:
ce67417 ("netfilter: nft_meta: add cgroup support")

The libnftnl  support is add in the commit:
1d4a480 ("expr: meta: Add cgroup support")

More information about the steps to use cgroup:
https://www.kernel.org/doc/Documentation/cgroups/net_cls.txt

More info about cgroup in iptables:
http://git.kernel.org/cgit/linux/kernel/git/pablo/nftables.git/commit/net/netfilter/xt_cgroup.c?id=82a37132f300ea53bdcd812917af5a6329ec80c3

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoinclude: refresh cached copy of nf_tables.h
Pablo Neira Ayuso [Mon, 10 Nov 2014 17:07:24 +0000 (18:07 +0100)] 
include: refresh cached copy of nf_tables.h

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: allow both nat_flags and port specification in redirect
Arturo Borrero [Fri, 7 Nov 2014 11:39:35 +0000 (12:39 +0100)] 
parser: allow both nat_flags and port specification in redirect

This patch changes the parser to permit both nat_flags and port specification
in the redirect expression.

The resulting syntax is:
 % nft add rule nat prerouting redirect [port] [nat_flags]

The port specification requires a bit of context regardin the transport
protocol. Some examples:
 % nft add rule nat prerouting tcp dport 22 redirect :23
 % nft add rule add prerouting udp dport 53 redirect :5353

The nat_flags argument is the last argument:
 % nft add rule nat prerouting tdp dport 80 redirect :8080 random

The port specification can be a range:
 % nft add rule nat prerouting tcp dport 80 redirect :8080-8090 random

While at it, the regression tests files are updated.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests/regression: redirect: fix invalid syntax
Arturo Borrero [Fri, 7 Nov 2014 11:39:30 +0000 (12:39 +0100)] 
tests/regression: redirect: fix invalid syntax

This patch fixes invalid syntax in the redirect test files.

I used ' ;ok' instead of ';ok', and ' ;nok' instead of ';fail'.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests/regression: masquerade: fix invalid syntax
Arturo Borrero [Fri, 7 Nov 2014 11:39:24 +0000 (12:39 +0100)] 
tests/regression: masquerade: fix invalid syntax

This patch fixes invalid syntax in the masquerade test files.

I used ' ;ok' instead of ';ok', and ' ;nok' instead of ';fail'.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonft: don't resolve hostnames by default
Arturo Borrero [Thu, 6 Nov 2014 08:05:28 +0000 (09:05 +0100)] 
nft: don't resolve hostnames by default

This patch changes the default behaviour of nft to not translate IP
addresses to hostnames when printing rules if no options are passed.

The options regarding translations after this patch are:

 <no -n/-N>             show IP addresses numerically (default behaviour)
 -n                     show IP addresses numerically
 -nn                    show Internet services and uid/gid numerically
 -nnn                   show protocols numerically
 -N (--reversedns)      translate IP addresses to names

The idea is to avoid breaking existing scripts that most likely rely on
'-n' to save the ruleset, so we reduce the impact of this patch and
provide a default behaviour that doesn't generate network traffic when
listing / saving the ruleset.

Joint work with Pablo.

Suggested-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoutils: indicate file and line on memory allocation errors
Pablo Neira Ayuso [Tue, 4 Nov 2014 13:27:22 +0000 (14:27 +0100)] 
utils: indicate file and line on memory allocation errors

For example:

 src/netlink.c:179: Memory allocation failure

This shouldn't happen, so this allows us to identify at what point the
memory allocation failure has happened. It may be helpful to identify
bugs.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add redirect support
Arturo Borrero [Mon, 3 Nov 2014 20:20:11 +0000 (21:20 +0100)] 
src: add redirect support

This patch adds redirect support for nft.

The syntax is:

 % nft add rule nat prerouting redirect [port] [nat_flags]

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agometa: Add support for datatype devgroup
Ana Rey [Thu, 23 Oct 2014 12:44:19 +0000 (14:44 +0200)] 
meta: Add support for datatype devgroup

This adds the new devgroup datatype to get the group name from
/etc/iproute2/group file.

Example of use:

nft add rule ip test input meta iifgroup 0 counter
nft add rule ip test input meta iifgroup default counter

Moreover, It adds tests in meta.t test file.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: enhance the error support throwing message with more details
Alvaro Neira [Thu, 23 Oct 2014 17:36:57 +0000 (19:36 +0200)] 
evaluate: reject: enhance the error support throwing message with more details

If we add a rule like:

nft add rule bridge filter input ether type ip reject with icmpv6 type no-route

We throw an error like:

<cmdline>:1:44-49: Error: conflicting protocols specified: ip vs ip6
add rule bridge filter input ether type ip reject with icmpv6 type no-route

Now, we are going to show in which part of the rule, we have the conflict:

<cmdline>:1:51-75: Error: conflicting protocols specified: ip vs ip6
add rule bridge filter input ether type ip reject with icmpv6 type no-route
                             ~~~~~~~~~~~~~        ^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: check the context in reject without reason for bridge and inet...
Alvaro Neira [Thu, 23 Oct 2014 17:36:56 +0000 (19:36 +0200)] 
evaluate: reject: check the context in reject without reason for bridge and inet tables

In rules like:

  nft add rule inet filter input reject
or
  nft add rule bridge filter input reject

we use icmpx to reject it. But if we have network context, we also use type of
reject. With this patch, we check the network context. If we don't have context,
we still use icmpx. However, if we have rules with network context like:

  nft add rule inet meta nfproto ipv4 reject
or
  nft add rule bridge ether type ipv6 reject

We are going to use icmp or icmpv6 to reject it taking into account the network
context.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: check in bridge and inet the network context in reject
Alvaro Neira [Wed, 22 Oct 2014 13:25:53 +0000 (15:25 +0200)] 
evaluate: reject: check in bridge and inet the network context in reject

In Inet tables, we have to check the network context in rules that we use
icmp or icmpv6 reason in reject. To be sure that the context is the correct.
However, for icmpx and tcp reject, we don't need to check it.

In Bridge tables, ee have vlan and arp traffic and they are not supported.
For this things, we have to check the network context. For example:

  nft add rule bridge test-bridge input \
ether type arp reject with icmp type host-unreachable
or
  nft add rule bridge test-bridge input \
   ether type vlan reject with tcp reset

In that cases, we have to throw an error. Moreover, we have to accept rules
that the network context is Ipv4 and Ipv6. For example:

  nft add rule -nnn bridge test-bridge input \
ip protocol tcp reject with tcp reset

Moreover, this patch refactor the code for check the family for bridge and inet
tables.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: accept a reject reason with incorrect network context
Alvaro Neira [Wed, 22 Oct 2014 13:25:52 +0000 (15:25 +0200)] 
evaluate: reject: accept a reject reason with incorrect network context

nft add rule bridge test-bridge input ether type ip \
reject with icmpv6 type no-route

This rule pass the evaluation step but the network context is incompatible with
the reject reason. In that cases, we have to throw an error like "conflicting
protocols specified: ip vs ip6"

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agomnl: delete useless parameter nf_sock in batch functions
Arturo Borrero [Tue, 21 Oct 2014 09:47:24 +0000 (11:47 +0200)] 
mnl: delete useless parameter nf_sock in batch functions

The 'struct mnl_socket *nf_sock' parameter is useless and perturbing.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotest: update and add the reject tests for ip, ip6, bridge and inet.
Alvaro Neira [Tue, 21 Oct 2014 14:15:46 +0000 (16:15 +0200)] 
test: update and add the reject tests for ip, ip6, bridge and inet.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: fix crash if we have transport protocol conflict from inet
Alvaro Neira [Mon, 20 Oct 2014 23:29:40 +0000 (01:29 +0200)] 
evaluate: reject: fix crash if we have transport protocol conflict from inet

Example:

nft add rule inet filter input meta l4proto udp reject with tcp reset

If we try to check if the transport protocol is tcp, we use the network context.
If we don't have this network context, we have a crash.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: fix crash if we specify ether type or meta nfproto
Alvaro Neira [Mon, 20 Oct 2014 13:32:09 +0000 (15:32 +0200)] 
evaluate: reject: fix crash if we specify ether type or meta nfproto

If we use this rule:

nft add rule bridge filter input \
ether type ip reject with icmp type host-unreachable

or that:

nft add rule inet filter input \
meta nfproto ipv4 reject with icmp type host-unreachable

we have a segfault because we add a network dependency when we already have
network context.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: add tests for masquerade
Arturo Borrero [Wed, 15 Oct 2014 10:01:27 +0000 (12:01 +0200)] 
tests: add tests for masquerade

Let's test the new masquerade option in nftables.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodelinearize: list the icmpx reason with the string associated
Alvaro Neira [Fri, 17 Oct 2014 12:24:36 +0000 (14:24 +0200)] 
delinearize: list the icmpx reason with the string associated

If you add the rule:
  nft add rule inet filter input reject with icmpx type host-unreachable
  nft list table inet filter

shows:
  table inet filter {
chain input {
reject with icmpx type 2
}
  }

We have to attach the icmpx datatype when we list the rules that use it. With
this patch if we list the ruleset, the output is:

  table inet filter {
chain input {
reject with icmpx type host-unreachable
}
  }

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: fix a crash if we specify ether type or meta nfproto in reject
Alvaro Neira [Sat, 11 Oct 2014 14:11:10 +0000 (16:11 +0200)] 
evaluate: fix a crash if we specify ether type or meta nfproto in reject

If we use a rule:
nft add rule bridge filter input \
ether type ip reject with icmp type host-unreachable

or this:

nft add rule inet filter input \
meta nfproto ipv4 reject with icmp type host-unreachable

we have a segfault because we add a network dependency when we already have
network context.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: remove unnecessary libintl.h check
Steven Barth [Fri, 10 Oct 2014 11:01:41 +0000 (13:01 +0200)] 
build: remove unnecessary libintl.h check

configure.ac checks for libintl.h which is not used and
may cause unnecessary trouble with e.g. embedded toolchains.

The only reference to libintl.h can be found in parser.c
as generated by bison however an include does not happen
as parser.h defines YYENABLE_NLS to be 0.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: allow disabling libreadline-support
Steven Barth [Thu, 9 Oct 2014 20:48:27 +0000 (22:48 +0200)] 
build: allow disabling libreadline-support

This makes nftables a bit more embedded-friendly.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: Delete an unnecessary whitespace in an output messages
Ana Rey [Wed, 8 Oct 2014 12:52:10 +0000 (14:52 +0200)] 
tests: regression: Delete an unnecessary whitespace in an output messages

If the script is run with the -e option, the output messages show an
unnecessary white-space. This path fixes this mistake.

sudo ./nft-test.py -e
[...] "line 34: nft add rule -nnn arp test-arp  input arp plen != {33-55} " [...]
                                             ^^^^

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: Delete all reference to wlan0 in test files
Ana Rey [Wed, 8 Oct 2014 12:52:09 +0000 (14:52 +0200)] 
tests: regression: Delete all reference to wlan0 in test files

There are false errors if you run the automated regression testing
without a wlan0 device in the system.

Delete references to 'wlan0' in test files or replaces 'wlan0' by 'lo'
or 'eth0' in the test files if it is possible.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add masquerade support
Arturo Borrero [Mon, 6 Oct 2014 19:51:24 +0000 (21:51 +0200)] 
src: add masquerade support

This patch adds masquerade support for nft.

The syntax is:

 % nft add rule nat postrouting masquerade [flags]

Currently, flags are:
 random, random-fully, persistent

Example:
 % nft add rule nat postrouting masquerade random,persistent

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink: use switch whenever possible in the monitor code
Pablo Neira Ayuso [Mon, 6 Oct 2014 16:25:16 +0000 (18:25 +0200)] 
netlink: use switch whenever possible in the monitor code

This is more robust than the current 'else' fallback. If we run a
newer kernel with old nft binaries, unknown messages will be
misinterpreted as deletions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: interpret the event type from the evaluation step
Pablo Neira Ayuso [Mon, 6 Oct 2014 16:03:27 +0000 (18:03 +0200)] 
src: interpret the event type from the evaluation step

Postpone the event type interpretation to the evaluation step.
This patch also fixes the combination of event and object types,
which was broken. The export code needed to be adjusted too.

The new and destroy are not tokens that can be recognized by
the scanner anymore, so this also implicitly restores 'ct state'.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add nat persistent and random options
Arturo Borrero [Fri, 3 Oct 2014 12:46:41 +0000 (14:46 +0200)] 
src: add nat persistent and random options

This patch adds more configuration options to the nat expression.

The syntax is as follow:
 % nft add rule nat postrouting <snat|dnat> <nat_arguments> [flags]

Flags are: random, persistent, random-fully.
Example:

 % nft add rule nat postrouting dnat 1.1.1.1 random,persistent

A requirement is to cache some [recent] copies of kernel headers.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonft: complete reject support
Alvaro Neira [Tue, 30 Sep 2014 15:21:40 +0000 (17:21 +0200)] 
nft: complete reject support

This patch allows to use the reject action in rules. For example:

  nft add rule filter input udp dport 22 reject

In this rule, we assume that the reason is network unreachable. Also
we can specify the reason with the option "with" and the reason. For example:

  nft add rule filter input tcp dport 22 reject with icmp type host-unreachable

In the bridge tables and inet tables, we can use this action too. For example:

  nft add rule inet filter input reject with icmp type host-unreachable

In this rule above, this generates a meta nfproto dependency to match
ipv4 traffic because we use a icmpv4 reason to reject.

If the reason is not specified, we infer it from the context.

Moreover, we have the new icmpx datatype. You can use this datatype for
the bridge and the inet tables to simplify your ruleset. For example:

  nft add rule inet filter input reject with icmpx type host-unreachable

We have four icmpx reason and the mapping is:

ICMPX reason            |       ICMPv6          |      ICMPv4
                        |                       |
admin-prohibited        | admin-prohibited      | admin-prohibited
port-unreachable        | port-unreachable      | port-unreachable
no-route                | no-route              | net-unreachable
host-unreachable        | addr-unreachable      | host-unreachable

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: Enhance symbolic_constant_parse()
Alvaro Neira [Tue, 30 Sep 2014 15:21:39 +0000 (17:21 +0200)] 
datatype: Enhance symbolic_constant_parse()

With this patch, this function finds the symbol inside the table. If the symbol
doesn't exist we use the basetype to parse it and create the constant
expression. Otherwise, return an error message.

This a refactorization to reuse this code in a follow up patch.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Enhance payload_gen_dependency()
Alvaro Neira [Tue, 30 Sep 2014 15:21:38 +0000 (17:21 +0200)] 
src: Enhance payload_gen_dependency()

With this patch, this function returns a statement with the new dependency
that we want to add, instead of an expression.

This change is needed in a follow up patch.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: generate dependency in the appropriate byteorder
Alvaro Neira [Tue, 30 Sep 2014 15:21:37 +0000 (17:21 +0200)] 
payload: generate dependency in the appropriate byteorder

If we add a dependency, the constant expression on the right
hand side must be represented in the appropriate order.

Example without this patch:

  nft add rule bridge filter input reject with icmp-host-unreach --debug netlink

  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000800 ]
  [ reject type 0 code 1 ]

When we create the payload expression we have the right value in host endian but
this has to be in big endian.

With this patch, if we add the same rule:

  nft add rule bridge filter input reject with icmp-host-unreach --debug netlink

  [ payload load 2b @ link header + 12 => reg 1 ]
  [ cmp eq reg 1 0x00000008 ]
  [ reject type 0 code 1 ]

The new dependency is converted to big endian.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agomnl: use nft_batch_begin and nft_batch_end from libnftnl
Pablo Neira Ayuso [Tue, 30 Sep 2014 15:42:36 +0000 (17:42 +0200)] 
mnl: use nft_batch_begin and nft_batch_end from libnftnl

Use the existing functions in libnftnl to begin and end a batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add list ruleset command
Arturo Borrero [Fri, 26 Sep 2014 16:30:08 +0000 (18:30 +0200)] 
src: add list ruleset command

This patch adds a new command to nft:
% nft list ruleset [family]

Which list the entire ruleset.
If no family is specified, all tables of all families are listed.

Users can now make several operations at ruleset level:

 % nft list ruleset > ruleset.nft
 % nft -f ruleset.nft
 % nft flush ruleset

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: factorize chain and table listing code
Arturo Borrero [Wed, 24 Sep 2014 10:32:19 +0000 (12:32 +0200)] 
rule: factorize chain and table listing code

Let's factorize common code. This is also useful in follow-up patches.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: rename do_command_list_cleanup() to table_cleanup()
Arturo Borrero [Wed, 24 Sep 2014 10:32:18 +0000 (12:32 +0200)] 
rule: rename do_command_list_cleanup() to table_cleanup()

Let's use a more generic name for this functions, since it has nothing to do
with commands.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add set optimization options
Arturo Borrero [Tue, 23 Sep 2014 12:05:15 +0000 (14:05 +0200)] 
src: add set optimization options

This patch adds options to choose set optimization mechanisms.

Two new statements are added to the set syntax, and they can be mixed:

 nft add set filter set1 { type ipv4_addr ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; policy memory ; }
 nft add set filter set1 { type ipv4_addr ; policy performance ; }
 nft add set filter set1 { type ipv4_addr ; policy memory ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; size 1024 ; policy memory ; }
 nft add set filter set1 { type ipv4_addr ; policy performance ; size 1024 ; }
 nft add set filter set1 { type ipv4_addr ; size 1024 ; policy performance ; }

Also valid for maps:

 nft add map filter map1 { type ipv4_addr : verdict ; policy performace ; }
 [...]

This is the output format, which can be imported later with `nft -f':

table filter {
set set1 {
type ipv4_addr
policy memory
size 1024
}
}

In this approach the parser accepts default options such as 'performance',
given they are a valid configurations, but aren't sent to the kernel.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: rearrange monitor/export rules
Patrick McHardy [Thu, 18 Sep 2014 13:58:22 +0000 (15:58 +0200)] 
parser: rearrange monitor/export rules

Move the output format rules next to the monitor and export command rules,
format them similar to other simple value mappings and unify their naming.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoqueue: clean up queue statement
Patrick McHardy [Wed, 24 Sep 2014 11:49:05 +0000 (13:49 +0200)] 
queue: clean up queue statement

- Rename keyword tokens to their actual keyword
- Change the grammar to follow the standard schema for statements and arguments
- Use actual expression for the queue numbers to support using normal range
  expressions, symbolic expression and so on.
- restore comma seperation of flag keywords

The result is that its possible to use standard ranges, prefix expressions,
symbolic expressions etc for the queue number. We get checks for overflow,
negative ranges and so on automatically.

The comma seperation of flags is more similar to what we have for other
flag values. It is still possible to use spaces, however this could be
removed since we never had a release supporting that.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: make range_low()/range_high() usable outside of segtree
Patrick McHardy [Wed, 24 Sep 2014 11:49:04 +0000 (13:49 +0200)] 
expr: make range_low()/range_high() usable outside of segtree

Their functionality is also needed for set descriptions, move the functions
to expressions.c and give them a more suitable name for global functions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agomnl: consistency checks across several netlink dumps
Pablo Neira Ayuso [Tue, 16 Sep 2014 16:58:11 +0000 (18:58 +0200)] 
mnl: consistency checks across several netlink dumps

Obtain the generation ID before dumping the object lists. Then,
check for generation ID updates when dumping the several lists that
this needs. In case of interference, nft has to remove the stale
objects and retry from scratch.

This is complementary to the NLM_F_DUMP_INTR flag which is local
to one single netlink dump.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add any folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:21 +0000 (12:39 +0200)] 
tests: Add any folder with test files.

"any" folder contains the test files that are executed in ipv4, ipv6,
inet, arp, bridge family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add bridge folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:20 +0000 (12:39 +0200)] 
tests: Add bridge folder with test files.

"bridge" folder contains the test files that are executed in bridge tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add arp folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:19 +0000 (12:39 +0200)] 
tests: Add arp folder with test files.

"arp" folder contains the test files that are executed in arp tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add inet folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:18 +0000 (12:39 +0200)] 
tests: Add inet folder with test files.

"inet" folder contains the test files that are executed in ipv4, ipv6
and inet family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add ip6 folder with test files.
Ana Rey [Thu, 18 Sep 2014 10:39:17 +0000 (12:39 +0200)] 
tests: Add ip6 folder with test files.

"ip6" folder contains the test files that are executed in ip6 and inet
family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add ip folder with test files
Ana Rey [Thu, 18 Sep 2014 10:39:16 +0000 (12:39 +0200)] 
tests: Add ip folder with test files

"ip" folder contains the test files that are executed in ip and inet
family of tables.

These test files are executed with nft-tests.py

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: Add automated regression testing
Ana Rey [Thu, 18 Sep 2014 10:39:15 +0000 (12:39 +0200)] 
tests: Add automated regression testing

Here, the automated regression testing for nftables and some test
files.

This script checks that the rule input and output of nft matches.
More details here below.

A) What is this testing?

This script tests two different paths:

* The rule input from the command-line. This checks the different steps
  from the command line to the kernel. This includes the parsing,
  evaluation and netlink generation steps.

* The output listing that is obtained from the kernel. This checks the
  different steps from the kernel to the command line: The netlink
  message parsing, postprocess and textify steps to display the rule
  listing.

As a final step, this script compares that the rule that is added can
be listed by nft.

B) What options are available?

The script offers the following options:

* Execute test files:

./nft-test.py                           # Run all test files
./nft-test.py path/file.t               # Run this test file

If there is a problem, it shows the differences between the rule that
is added and the rule that is listed by nft.

In case you hit an error, the script doesn't keep testing for more
families. Unless you specify the --force-family option.

* Execute broken tests:

./nft-test.sh -e

This runs tests for rules that need a fix: This mode runs the lines that
that start with a "-" symbol.

* Debugging:

./nft-test.sh -d

This shows all the commands that the script executes, so you can watch
its internal behaviour.

* Keep testing all families on error.

./nft-test.sh -f

Don't stop testing for more families in case of error.

C) What is the structure of the test file?

A test file contains a set of rules that are added in the system.

Here, an example of a test file:

   *ip;test-ipv4                               # line 1
   *ip6;test-ipv6                              # line 2
   *inet;test-inet                             # line 3

   :input;type filter hook input priority 0    # line 4

   ah hdrlength != 11-23;ok;ah hdrlength < 11 ah hdrlength > 23   # line 5
   - tcp dport != {22-25}                                         # line 6

   !set1 ipv4_addr;ok                          # line 7
   ?set1 192.168.3.8 192.168.3.9;ok            # line 8
   # This is a commented-line.                 # line 9

Line 1 defines a table. The name of the table is 'test-ip' and the
family is ip. Lines 2 and 3 defines more tables for different families
so the rules in this test file are also tested there.

Line 4 defines the chain. The name of this chain is "input". The type is
"filter", the hook is "input" and the priority is 0.

Line 5 defines the rule, the ";" character is used as separator of several
parts:

* Part 1: "ah hdrlength != 11-23" is the rule to check.
* Part 2: "ok" is the result expected with the execute of this rule.
* Part 3: "ah hdrlength < 11 ah hdrlength > 23". This is the expected
  output. You can leave this empty if the output is the same as the
  input.

Line 6 is a marked line. This means that this rule is tested if
'-e' is passed as argument to nft-test.py.

Line 7 adds a new set. The name of this set is "set1" and the type
of this set is "ipv4_add".

Line 8 adds two elements into the 'set1' set: "192.168.3.8" and
"192.168.3.9". A whitespace separates the elements of the set.

Line 9 uses the "#" symbol that means that this line is commented out.

D) The test folders

The test files are divided in several directories: ip, ip6, inet, arp,
bridge and any.

 * "ip" folder contains the test files that are executed in ip and inet
   table.

 * "ip" folder contains the test files that are executed in ip6 and inet
   table.

 * "inet" folder contains the test files that are executed in the ip, ip6
    and inet table.

 * "arp" folder contains the test files that are executed in the arp
   table.

 * "bridge" folder: Here are the test files are executed in bridge
   tables.

 * "any" folder: Here are the test files are executed in ip, ip6, inet,
   arp and bridge tables.

E) Meaning of messages:

* A warning message means the rule input and output of nft mismatches.
* An error message means the nft-tool shows an error when we add it or
  the listing is broken after the rule is added.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: compact log level grammar
Patrick McHardy [Wed, 17 Sep 2014 10:22:38 +0000 (12:22 +0200)] 
parser: compact log level grammar

Put rule and action on a single line as for other simple mappings.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: fix 'describe' command when passing wrong expressions
Pablo Neira Ayuso [Tue, 16 Sep 2014 09:03:57 +0000 (11:03 +0200)] 
src: fix 'describe' command when passing wrong expressions

Before this patch:

 # nft describe tcp foo
 value expression, datatype inet_proto (Internet protocol) (basetype integer), 8 bits
 Segmentation fault

After this patch:

 # nft describe tcp foo
 <cmdline>:1:14-16: Error: syntax error, unexpected string, expecting end of file or newline or semicolon
 describe tcp foo
              ^^^

Reported-by: Kevin Fenzi <kevin@scrye.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: simplify monitor command parsing
Patrick McHardy [Wed, 17 Sep 2014 07:43:52 +0000 (09:43 +0200)] 
parser: simplify monitor command parsing

Add tokens for "new" and "destroy". Split up the monitor flags into an
event and an object to avoid lots of duplicated code.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: add specific byteorder to the struct proto_hdr_template
Alvaro Neira Ayuso [Wed, 17 Sep 2014 07:20:28 +0000 (09:20 +0200)] 
src: add specific byteorder to the struct proto_hdr_template

If we try to add a rule like:

nft add rule filter input udp length {55-9999}

nftable shows:

BUG: invalid byte order conversion 0 => 2
nft: src/evaluate.c:153: byteorder_conversion_op: Assertion `0' failed.

Some of the existing payload fields rely on BYTEORDER_INVALID. Therefore, if we
try to convert it in evaluation step, we hit this bug.

This patch allows to add a specific byteorder to the struct proto_hdr_template. If
we create a expression with a invalid byteorder, we will use the byteorder
added to the proto_hdr_template structure.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: include file and line in netlink ABI errors
Arturo Borrero [Mon, 15 Sep 2014 12:10:45 +0000 (14:10 +0200)] 
netlink: include file and line in netlink ABI errors

Let's give some more information when netlink ABI errors happens.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agolog: netlink_linearize: don't set level if user didn't specify
Pablo Neira Ayuso [Thu, 11 Sep 2014 13:17:17 +0000 (15:17 +0200)] 
log: netlink_linearize: don't set level if user didn't specify

The kernel will default on LOG_WARNING, to mimic iptables behaviour.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoinclude: refresh include/linux/nf_tables.h cached copy
Pablo Neira Ayuso [Thu, 11 Sep 2014 12:10:48 +0000 (14:10 +0200)] 
include: refresh include/linux/nf_tables.h cached copy

Keep in sync with nf_tables.h header file in net-next tree.

Rename NFT_CT_LABEL to NFT_CT_LABELS, this probably slipped through
when editing the header file by hand.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: add `flush ruleset'
Arturo Borrero [Tue, 2 Sep 2014 14:42:22 +0000 (16:42 +0200)] 
src: add `flush ruleset'

This patch adds the `flush ruleset' operation to nft.

The syntax is:
 % nft flush ruleset [family]

To flush all the ruleset (all families):
 % nft flush ruleset

To flush the ruleset of a given family:
 % nft flush ruleset ip
 % nft flush ruleset inet

This flush is a shortcut operation which deletes all rules, sets, tables
and chains.
It's possible since the modifications in the kernel to the NFT_MSG_DELTABLE
API call.

Users can benefit of this operation when doing an atomic replacement of the
entire ruleset, loading a file like this:

 =========
 flush ruleset
 table ip filter {
  chain input {
counter accept
}
 }
 =========

Also, users who want to simply clean the ruleset for whatever reason can do it now
without having to iterate families/tables.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoFix typo in chain hook parsing
Yanchuan Nian [Thu, 4 Sep 2014 09:26:36 +0000 (17:26 +0800)] 
Fix typo in chain hook parsing

Just a typo in chain hook parsing

Signed-off-by: Yanchuan Nian <ycnian@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add devgroup support in meta expresion
Ana Rey [Tue, 2 Sep 2014 18:37:17 +0000 (20:37 +0200)] 
src: Add devgroup support in meta expresion

This adds device group support in meta expresion.

The new attributes of meta are "iffgroup" and "oifgroup"
- iffgroup: Match device group of incoming device.
- oifgroup: Match device group of outcoming device.

Example of use:
nft add rule ip test input meta iifgroup 2 counter
nft add rule ip test output meta oifgroup 2 counter

The kernel and libnftnl support were added in these commits:
netfilter: nf_tables: add devgroup support in meta expresion
src: meta: Add devgroup support to meta expresion

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: meta: Fix the size of cpu attribute
Ana Rey [Tue, 2 Sep 2014 18:13:25 +0000 (20:13 +0200)] 
src: meta: Fix the size of cpu attribute

Fix the size of cpu attribute in meta_template struct.

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodoc: nft: Fix trivial error in man page where flush should be rename
Kevin Fenzi [Sat, 23 Aug 2014 15:59:52 +0000 (09:59 -0600)] 
doc: nft: Fix trivial error in man page where flush should be rename

Trivial fix, but someone filed a bug on it, and it should be fixed. ;)
https://bugzilla.redhat.com/show_bug.cgi?id=1132917

Signed-off-by: Kevin Fenzi <kevin@scrye.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add support for cpu in meta expresion
Ana Rey [Sun, 24 Aug 2014 12:21:00 +0000 (14:21 +0200)] 
src: Add support for cpu in meta expresion

This allows you to match cpu handling with a packet.

This is an example of the syntax for this new attribute:

nft add rule ip test input meta cpu 1 counter
nft add rule ip test input meta cpu 1-3 counter
nft add rule ip test input meta cpu { 1, 3} counter

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: Add support for pkttype in meta expresion
Ana Rey [Tue, 5 Aug 2014 18:33:39 +0000 (20:33 +0200)] 
src: Add support for pkttype in meta expresion

If you want to match the pkttype field of the skbuff, you have to
use the following syntax:

 nft add rule ip filter input meta pkttype PACKET_TYPE

where PACKET_TYPE can be: unicast, broadcast and multicast.

Joint work with Alvaro Neira Ayuso <alvaroneay@gmail.com>

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: use proto_unknown for raw protocol header
Yuxuan Shui [Sat, 9 Aug 2014 13:01:17 +0000 (21:01 +0800)] 
payload: use proto_unknown for raw protocol header

Otherwise payload.desc would be NULL, which causes the crash in bug 915.

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: don't return error in netlink_linearize_rule()
Pablo Neira Ayuso [Mon, 18 Aug 2014 15:43:28 +0000 (17:43 +0200)] 
src: don't return error in netlink_linearize_rule()

This function converts the rule from the list of statements to the
netlink message format. The only two possible errors that can make
this function to fail are memory exhaustion and malformed statements
which inmediately stop the execution of nft.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoverdict type: handle verdict flags and encoded additional information
Patrick McHardy [Mon, 18 Aug 2014 23:35:36 +0000 (00:35 +0100)] 
verdict type: handle verdict flags and encoded additional information

The kernel can handle this, nftables should also.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoproto: fix byteorder of ETH_P_* values
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
proto: fix byteorder of ETH_P_* values

The ethernet header type is in big endian byte order, the ETH_P_* values
are in host byte order however. Fix this using __constant_htons().

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: take endianess into account in symbolic_constant_print()
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
datatype: take endianess into account in symbolic_constant_print()

symbolic_constant_print() uses mpz_cmp_ui() to find the matching symbol.
Since GMP internally treats all values as being in host byte, this
doesn't work when the constant value is non-host byteorder, such as
the ethernet protocol type.

Export the expression's value in its original byteorder for comparison
to fix this.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agopayload: take endianess into account when updating the payload context
Patrick McHardy [Mon, 18 Aug 2014 23:21:59 +0000 (00:21 +0100)] 
payload: take endianess into account when updating the payload context

payload_expr_pctx_update() uses the numeric protocol value in host byte
order to find the upper layer protocol. This obviously doesn't work for
protocol expressions in other byte orders, such as the ethernet protocol
on little endian.

Export the protocol value in the correct byte order and use that value
to look up the upper layer protocol.

Signed-off-by: Patrick McHardy <kaber@trash.net>