]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
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>
9 years agotests/py: update for changed set name in payload
Pablo Neira Ayuso [Fri, 13 May 2016 17:13:12 +0000 (19:13 +0200)] 
tests/py: update for changed set name in payload

Original patch posted in the mailing list from Patrick, I have refreshed
this so it applies on top of current HEAD.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset: explicitly supply name to implicit set declarations
Patrick McHardy [Wed, 27 Apr 2016 11:29:46 +0000 (12:29 +0100)] 
set: explicitly supply name to implicit set declarations

Support explicitly names implicitly declared sets.

Also change the template names for literal sets and maps to use identifiers
that can not clash with user supplied identifiers.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoset: allow non-constant implicit set declarations
Patrick McHardy [Wed, 27 Apr 2016 11:29:45 +0000 (12:29 +0100)] 
set: allow non-constant implicit set declarations

Currently all implicitly declared sets are marked as constant. The flow
statement needs to implicitly declare non-constant sets, so instead of
unconditionally marking the set as constant, only do so if the declaring
expression is itself a constant set.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: declare interval_map_decompose() from header file
Pablo Neira Ayuso [Fri, 13 May 2016 10:40:52 +0000 (12:40 +0200)] 
src: declare interval_map_decompose() from header file

Instead of having several extern function declarations.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonetlink: several function constifications
Pablo Neira Ayuso [Fri, 13 May 2016 10:36:00 +0000 (12:36 +0200)] 
netlink: several function constifications

Constify:

* netlink_dump*()
* netlink_delinearize_*()
* netlink_add_rule_list()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell/run-tests.sh: print hint about testcase being executed
Arturo Borrero [Wed, 11 May 2016 12:39:38 +0000 (14:39 +0200)] 
tests/shell/run-tests.sh: print hint about testcase being executed

Print a line with the name of the testcase being executed, and then delete it
with the result.

There are tests which may take a long time and its good to know what is doing
the testsuite.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/shell/run-tests.sh: execute tests in sorted order
Arturo Borrero [Wed, 11 May 2016 12:39:33 +0000 (14:39 +0200)] 
tests/shell/run-tests.sh: execute tests in sorted order

Let's sort tests files before iterating over them.

Put the find string in a separated function so it's more readable.

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 testcase to catch segfault if invalid syntax was used
Arturo Borrero [Wed, 11 May 2016 11:30:13 +0000 (13:30 +0200)] 
tests/shell: add testcase to catch segfault if invalid syntax was used

This helps to catch 5afa5a1 ("evaluate: check for NULL datatype in rhs
in lookup expr").

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 testcase for 'nft -f' load with actions
Arturo Borrero [Wed, 11 May 2016 11:30:08 +0000 (13:30 +0200)] 
tests/shell: add testcase for 'nft -f' load with actions

Let's tests loading a ruleset with actions.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: check for NULL datatype in rhs in lookup expr
Arturo Borrero [Wed, 11 May 2016 11:30:02 +0000 (13:30 +0200)] 
evaluate: check for NULL datatype in rhs in lookup expr

If we are evaluating an EXPR_SET_REF, check if right->dtype is not NULL.
We can hit SEGFAULT if for whatever reason the referenced object does not
exist.

Using this testfile (note the invalid set syntax):

% cat test.nft
flush ruleset
add table t
add chain t c
add set t s {type ipv4_addr\;}
add rule t c ip saddr @s

Without this patch:

% nft -f test.nft
Segmentation fault

With this patch:

% nft -f test.nft
t.nft:4:28-28: Error: syntax error, unexpected junk, expecting newline or semicolon
add set t s {type ipv4_addr\;}
                           ^
t.nft:4:13-29: Error: set definition does not specify key data type
add set t s {type ipv4_addr\;}
            ^^^^^^^^^^^^^^^^^
t.nft:5:23-24: Error: the referenced set does not exist
add rule t c ip saddr @s
             ~~~~~~~~ ^^

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: fix payload of dccp type in set elements
Pablo Neira Ayuso [Wed, 11 May 2016 21:01:35 +0000 (23:01 +0200)] 
tests/py: fix payload of dccp type in set elements

This value needs to be lshift one bit to be correct.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: fix fragment-offset field
Pablo Neira Ayuso [Wed, 11 May 2016 18:22:48 +0000 (20:22 +0200)] 
tests/py: fix fragment-offset field

Set elements were miscalculated.

After this patch:

element 00000801  : 0 [end]
            ^^^^

Which looks correct according to my calculations:

>>> print hex(socket.htons(33 << 3))
0x801
^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: add missing netdev ip dscp payload tests
Pablo Neira Ayuso [Tue, 10 May 2016 22:52:39 +0000 (00:52 +0200)] 
tests/py: add missing netdev ip dscp payload tests

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add ecn support
Pablo Neira Ayuso [Tue, 10 May 2016 22:22:11 +0000 (00:22 +0200)] 
src: add ecn support

This supports both IPv4:

 # nft --debug=netlink add rule ip filter forward ip ecn ce counter
 ip filter forward
  [ payload load 1b @ network header + 1 => reg 1 ]
  [ bitwise reg 1 = (reg=1 & 0x00000003 ) ^ 0x00000000 ]
  [ cmp eq reg 1 0x00000003 ]
  [ counter pkts 0 bytes 0 ]

For IPv6:

 # nft --debug=netlink add rule ip6 filter forward ip6 ecn ce counter
 ip6 filter forward
  [ payload load 1b @ network header + 1 => reg 1 ]
  [ bitwise reg 1 = (reg=1 & 0x00000030 ) ^ 0x00000000 ]
  [ cmp eq reg 1 0x00000030 ]
  [ counter pkts 0 bytes 0 ]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: add dscp support
Pablo Neira Ayuso [Tue, 10 May 2016 22:21:24 +0000 (00:21 +0200)] 
src: add dscp support

This supports both IPv4:

 # nft --debug=netlink add rule filter forward ip dscp cs1 counter
 ip filter forward
  [ payload load 1b @ network header + 1 => reg 1 ]
  [ bitwise reg 1 = (reg=1 & 0x000000fc ) ^ 0x00000000 ]
  [ cmp neq reg 1 0x00000080 ]
  [ counter pkts 0 bytes 0 ]

And also IPv6, note that in this case we take two bytes from the payload:

 # nft --debug=netlink add rule ip6 filter input ip6 dscp cs4 counter
 ip6 filter input
  [ payload load 2b @ network header + 0 => reg 1 ]
  [ bitwise reg 1 = (reg=1 & 0x0000c00f ) ^ 0x00000000 ]
  [ cmp eq reg 1 0x00000008 ]
  [ counter pkts 0 bytes 0 ]

Given the DSCP is split in two bytes, the less significant nibble
of the first byte and the two most significant 2 bits of the second
byte.

The 8 bit traffic class in RFC2460 after the version field are used for
DSCP (6 bit) and ECN (2 bit). Support for ECN comes in a follow up
patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoproto: remove priority field definition from IPv6 header
Pablo Neira Ayuso [Mon, 7 Dec 2015 17:44:44 +0000 (18:44 +0100)] 
proto: remove priority field definition from IPv6 header

This is actually part of the traffic class field according to RFC2460.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoproto: update IPv6 flowlabel offset and length according to RFC2460
Pablo Neira Ayuso [Mon, 7 Dec 2015 13:12:20 +0000 (14:12 +0100)] 
proto: update IPv6 flowlabel offset and length according to RFC2460

This is a 20 bit field according to Section 3. IPv6 Header Format.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: handle payload matching split in two bytes
Pablo Neira Ayuso [Mon, 7 Dec 2015 11:48:59 +0000 (12:48 +0100)] 
evaluate: handle payload matching split in two bytes

When the bits are split between two bytes and the payload field is
smaller than one byte, we need to extend the expression length on both
sides (payload and constant) of the relational expression.

The existing trimming from the delinerization step handles the listing
for us, so no changes on that front.

This patch allows us to match the IPv6 DSCP field which falls into the
case that is described above.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosrc: move payload sub-byte matching to the evaluation step
Pablo Neira Ayuso [Tue, 10 May 2016 22:17:47 +0000 (00:17 +0200)] 
src: move payload sub-byte matching to the evaluation step

Generating the bitwise logic to match sub-byte payload fields from the
linearize step has several problems:

1) When the bits are split between two bytes and the payload field is
   smaller than one byte, we need to extend the expression length on
   both sides (payload and constant) of the relational expression.

2) Explicit bitmask operations on sub-byte payload fields need to be
   merge to the implicit bitmask operation, otherwise we generate two
   bitwise instructions. This is not resolved by this patch, but we
   should have a look at some point to this.

With this approach, we can benefit from the binary operation transfer
for shifts to provide a generic way to adjust the constant side of the
expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: transfer right shifts to set reference side
Pablo Neira Ayuso [Tue, 10 May 2016 22:12:09 +0000 (00:12 +0200)] 
evaluate: transfer right shifts to set reference side

This provides a generic way to transfer shifts from the left hand side
to the right hand range side of a relational expression when performing
transformations from the evaluation step.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: transfer right shifts to range side
Pablo Neira Ayuso [Sat, 5 Dec 2015 17:42:40 +0000 (18:42 +0100)] 
evaluate: transfer right shifts to range side

This provides a generic way to transfer shifts from the left hand side
to the right hand range side of a relational expression when performing
transformations from the evaluation step.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: transfer right shifts to constant side
Patrick McHardy [Sat, 28 Nov 2015 13:20:23 +0000 (13:20 +0000)] 
evaluate: transfer right shifts to constant side

This provides a generic way to transfer shifts from the left hand side
to the right hand constant side of a relational expression when
performing transformations from the evaluation step.

Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agodist: include tests/ directory and files in tarball
Arturo Borrero [Thu, 28 Apr 2016 09:14:38 +0000 (11:14 +0200)] 
dist: include tests/ directory and files in tarball

If we include tests/ in the release tarball, downstream distributors
can run the testsuites themselves while developing the packages.

This way, tests can be run in a more integrated environment and they can
discover errors related to the integration with the given distribution itself.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: py: allow to run tests with other nft binaries
Arturo Borrero [Thu, 21 Apr 2016 09:43:54 +0000 (11:43 +0200)] 
tests: py: allow to run tests with other nft binaries

Allow to run tests with other nft binaries by reading a 'NFT'
environment variable, allowing arbitrary locations for the nft binary.

This is what the tests/shell/run-tests.sh script does.

Among other thing, this allow us to properly hook this testsuite
from the Debian CI environment (https://ci.debian.net) where we can perform
tests for packages 'as installed'.

Examples:

 # run with default config (ie src/nft)
 % ./nft-test.py

 # run with installed binary (ie /usr/sbin/nft)
 % NFT=/usr/sbin/nft ./nft-test.py

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: add interval tests
Pablo Neira Ayuso [Wed, 27 Apr 2016 12:46:52 +0000 (14:46 +0200)] 
tests/py: add interval tests

Add some initial tests to cover dynamic interval sets.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: explicitly indication of set type and flags from test definitions
Pablo Neira Ayuso [Wed, 27 Apr 2016 12:34:52 +0000 (14:34 +0200)] 
tests/py: explicitly indication of set type and flags from test definitions

This patch adds explicit set type in test definitions, as well as flags.

This has triggered a rework that starts by introducing a Set class to
make this whole code more extensible and maintainable.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests/py: add more interval tests for anonymous sets
Pablo Neira Ayuso [Wed, 27 Apr 2016 10:25:07 +0000 (12:25 +0200)] 
tests/py: add more interval tests for anonymous sets

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agotests: shell: add testcases for named sets with intervals
Arturo Borrero [Mon, 25 Apr 2016 10:20:57 +0000 (12:20 +0200)] 
tests: shell: add testcases for named sets with intervals

Let's add some testcases for named sets with intervals and ranges.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: add interval overlap detection for dynamic updates
Pablo Neira Ayuso [Fri, 22 Apr 2016 17:15:34 +0000 (19:15 +0200)] 
segtree: add interval overlap detection for dynamic updates

Make sure the new intervals that we want to add are not overlapping with
any of the existing ones.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: rename set expression set_to_segtree()
Pablo Neira Ayuso [Fri, 22 Apr 2016 16:05:26 +0000 (18:05 +0200)] 
segtree: rename set expression set_to_segtree()

This function is modified by a follow up patch to take the set object,
so rename it to init.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: add expr_to_intervals()
Pablo Neira Ayuso [Fri, 22 Apr 2016 16:02:18 +0000 (18:02 +0200)] 
segtree: add expr_to_intervals()

Refactor code to add the new expr_to_intervals(). This function takes
the list of set element expressions and convert them to a list of
half-closed intervals.

This is useful for different purposes, such as interval overlap
and conflicts detection.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: set expr->len for prefix expression from interval_map_decompose()
Pablo Neira Ayuso [Sat, 23 Apr 2016 13:47:30 +0000 (15:47 +0200)] 
segtree: set expr->len for prefix expression from interval_map_decompose()

This field needs to be set for the new interval overlap detection.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agoevaluate: bail out on prefix or range to non-interval set
Pablo Neira Ayuso [Mon, 18 Apr 2016 17:10:28 +0000 (19:10 +0200)] 
evaluate: bail out on prefix or range to non-interval set

If you declare a set with no interval flag, you get this bug message:

 # nft add element filter myset { 192.168.1.100/24 }
 BUG: invalid data expression type prefix
 nft: netlink.c:323: netlink_gen_data: Assertion `0' failed.
 Aborted

After this patch, we provide a clue to the user:

 # nft add element filter myset { 192.168.1.100/24 }
 <cmdline>:1:23-38: Error: Set member cannot be prefix, missing interval flag on declaration
 add element filter myset { 192.168.1.100/24 }
                            ^^^^^^^^^^^^^^^^

 # nft add element filter myset { 192.168.1.100-192.168.1.200 }
 <cmdline>:1:23-49: Error: Set member cannot be range, missing interval flag on declaration
 add element filter myset { 192.168.1.100-192.168.1.200 }
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: special handling for the first non-matching segment
Pablo Neira Ayuso [Mon, 18 Apr 2016 13:06:09 +0000 (15:06 +0200)] 
segtree: special handling for the first non-matching segment

Add the first non-matching segment if the set is empty or if the set
becomes empty after the element removal.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agorule: support for incremental set interval element updates
Pablo Neira Ayuso [Mon, 18 Apr 2016 11:03:41 +0000 (13:03 +0200)] 
rule: support for incremental set interval element updates

Introduce __do_add_setelems() and do_delete_setelems() to support
incremental set interval element updates.

From do_add_set(), use netlink_add_setelems() not to try to re-add the
same elements again

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: explicit initialization via set_to_intervals()
Pablo Neira Ayuso [Mon, 18 Apr 2016 10:36:46 +0000 (12:36 +0200)] 
segtree: explicit initialization via set_to_intervals()

Allow explicit compound expression to initialize the set intervals.
Incremental updates to interval sets require this.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: handle adjacent interval nodes from expr_value_cmp()
Pablo Neira Ayuso [Mon, 18 Apr 2016 10:08:18 +0000 (12:08 +0200)] 
segtree: handle adjacent interval nodes from expr_value_cmp()

Named sets may contain adjacent interval nodes, when equal in key, look
at the flags. Those with EXPR_F_INTERVAL_END should come in first place.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: clone full expression from interval_map_decompose()
Pablo Neira Ayuso [Mon, 18 Apr 2016 10:00:39 +0000 (12:00 +0200)] 
segtree: clone full expression from interval_map_decompose()

Instead of cloning just its value, expr_value() expects a set element or
mapping.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agosegtree: perform stricter expression type validation from expr_value()
Pablo Neira Ayuso [Mon, 18 Apr 2016 09:59:56 +0000 (11:59 +0200)] 
segtree: perform stricter expression type validation from expr_value()

This helper function returns a expression value type that represents the
set element key. This functions currently expects two kind of
expressions: set elements and mappings.

Bail out otherwise, if we see anything else, we have to fix our code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
9 years agonft monitor [ trace ]
Patrick McHardy [Sat, 26 Mar 2016 10:04:48 +0000 (10:04 +0000)] 
nft monitor [ trace ]

... can now display nftables nftrace debug information.

$ nft filter input tcp dport 10000 nftrace set 1
$ nft filter input icmp type echo-request nftrace set 1
$ nft -nn monitor trace
trace id e1f5055f ip filter input packet: iif eth0 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 ip saddr 192.168.122.1 ip daddr 192.168.122.83 ip tos 0 ip ttl 64 ip id 32315 ip length 84 icmp type echo-request icmp code 0 icmp id 10087 icmp sequence 1
trace id e1f5055f ip filter input rule icmp type echo-request nftrace set 1 (verdict continue)
trace id e1f5055f ip filter input verdict continue
trace id e1f5055f ip filter input
trace id 74e47ad2 ip filter input packet: iif vlan0 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 vlan pcp 0 vlan cfi 1 vlan id 1000 ip saddr 10.0.0.1 ip daddr 10.0.0.2 ip tos 0 ip ttl 64 ip id 49030 ip length 84 icmp type echo-request icmp code 0 icmp id 10095 icmp sequence 1
trace id 74e47ad2 ip filter input rule icmp type echo-request nftrace set 1 (verdict continue)
trace id 74e47ad2 ip filter input verdict continue
trace id 74e47ad2 ip filter input
trace id 3030de23 ip filter input packet: iif vlan0 ether saddr 63:f6:4b:00:54:52 ether daddr c9:4b:a9:00:54:52 vlan pcp 0 vlan cfi 1 vlan id 1000 ip saddr 10.0.0.1 ip daddr 10.0.0.2 ip tos 16 ip ttl 64 ip id 59062 ip length 60 tcp sport 55438 tcp dport 10000 tcp flags == syn tcp window 29200
trace id 3030de23 ip filter input rule tcp dport 10000 nftrace set 1 (verdict continue)
trace id 3030de23 ip filter input verdict continue
trace id 3030de23 ip filter input

Based on a patch from Florian Westphal, which again was based on a patch
from Markus Kötter.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agoproto: add protocol header fields filter and ordering for packet decoding
Patrick McHardy [Sat, 26 Mar 2016 10:04:48 +0000 (10:04 +0000)] 
proto: add protocol header fields filter and ordering for packet decoding

The next patch introduces packet decoding for tracing messages based on
the proto definitions. In order to provide a readable output, add a filter
to surpress uninteresting header fields and allow to specify and explicit
output order.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agopayload: add payload_is_stacked()
Patrick McHardy [Sun, 24 Apr 2016 21:15:08 +0000 (22:15 +0100)] 
payload: add payload_is_stacked()

Add payload_is_stacked() to determine whether a protocol expression match defines
a stacked protocol on the same layer.

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agopayload: move payload depedency tracking to payload.c
Patrick McHardy [Sat, 26 Mar 2016 10:04:48 +0000 (10:04 +0000)] 
payload: move payload depedency tracking to payload.c

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agonft: resync kernel header files
Patrick McHardy [Fri, 22 Apr 2016 11:33:33 +0000 (12:33 +0100)] 
nft: resync kernel header files

Signed-off-by: Patrick McHardy <kaber@trash.net>
9 years agopayload: fix stacked headers protocol context tracking
Patrick McHardy [Sun, 24 Apr 2016 10:22:30 +0000 (11:22 +0100)] 
payload: fix stacked headers protocol context tracking

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

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

All previously successful testcases pass.

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

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

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

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

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

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

Lets disable merging for now so that we generate

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

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

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

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

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

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

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

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

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

Also unload NAT modules between tests.

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

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

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

After this patch,

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

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

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

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

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

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

New simple testcases for kernel commit/rollback operations.

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

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

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

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

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

Before:

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

After:

table t {
chain c {
ct state new
}
}

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

Some basic test regarding chains: jumps and validations.

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

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

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

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

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

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

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

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

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

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

Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>