]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
11 years agosrc: add events reporting
Arturo Borrero [Wed, 16 Apr 2014 16:43:17 +0000 (18:43 +0200)] 
src: add events reporting

This patch adds a basic events reporting option to nft.

The syntax is:
 % nft monitor [new|destroy] [tables|chains|rules|sets|elements] [xml|json]

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add socket error reporting helper function
Arturo Borrero [Mon, 14 Apr 2014 10:17:41 +0000 (12:17 +0200)] 
netlink: add socket error reporting helper function

This patch adds a simple helper function to report errors while
opening the Netlink socket.

To help users to diagnose problems, a new NFT_EXIT_NONL exit code is included,
which is 3.

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>
11 years agonetlink: refactorize set_elem conversion from netlink
Arturo Borrero [Mon, 14 Apr 2014 10:17:35 +0000 (12:17 +0200)] 
netlink: refactorize set_elem conversion from netlink

Lets refactorize set_elem handling.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_table() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:30 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_table() func

This code is suitable to be reusable.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_chain() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:24 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_chain() func

Let's make this code reusable.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agorule: generalize chain_print()
Arturo Borrero [Mon, 14 Apr 2014 10:17:19 +0000 (12:17 +0200)] 
rule: generalize chain_print()

Lest generalize the chain_print() function, so we can print a plain chain
as the user typed in the basic CLI.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: add netlink_delinearize_set() func
Arturo Borrero [Mon, 14 Apr 2014 10:17:14 +0000 (12:17 +0200)] 
netlink: add netlink_delinearize_set() func

Let's factorize this code, so it can be reused.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agorule: allow to print sets in plain format
Arturo Borrero [Mon, 14 Apr 2014 10:17:08 +0000 (12:17 +0200)] 
rule: allow to print sets in plain format

Allow to print sets with or without format.

This is useful in situations where we want to print more or less the same
the user typed (IOW, in one single line, and with family/table info).

While at it, make family2str() function public, so it can be used in
other places.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agometa: Add support for input and output bridge interface name
Tomasz Bursztyka [Mon, 14 Apr 2014 12:41:30 +0000 (15:41 +0300)] 
meta: Add support for input and output bridge interface name

Add support to get an input or output bridge interface name through the
relevant meta keys.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoBump version to v0.2 v0.2
Patrick McHardy [Mon, 14 Apr 2014 10:56:03 +0000 (12:56 +0200)] 
Bump version to v0.2

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: fix make install problems
Patrick McHardy [Mon, 14 Apr 2014 10:04:31 +0000 (12:04 +0200)] 
doc: fix make install problems

-e   INSTALL doc
/usr/bin/install: cannot stat 'doc/nftables.8': No such file or directory
make[1]: *** [install] Error 1
make: *** [doc] Error 2

Rename everything to nft.* to fix this up.

Reported-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: change documentation license to CC BY-SA 4.0
Patrick McHardy [Mon, 14 Apr 2014 06:52:43 +0000 (08:52 +0200)] 
doc: change documentation license to CC BY-SA 4.0

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: fix programlisting indentation
Patrick McHardy [Mon, 14 Apr 2014 06:37:26 +0000 (08:37 +0200)] 
doc: fix programlisting indentation

Since programlistings are used literally, the should not be indented.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodatatypes: rename some types for more consistency
Patrick McHardy [Sun, 13 Apr 2014 10:58:25 +0000 (12:58 +0200)] 
datatypes: rename some types for more consistency

Add some more consistency by using _addr for all address types, _proto
for all protocol types and iface_ for all interface types.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agodoc: documentation update
Patrick McHardy [Wed, 26 Mar 2014 12:28:59 +0000 (12:28 +0000)] 
doc: documentation update

Signed-off-by: Patrick McHardy
11 years agobuild: fix documentation build
Patrick McHardy [Mon, 14 Apr 2014 06:18:47 +0000 (08:18 +0200)] 
build: fix documentation build

Handle the docbook2x-man mess that is called differently on different distributions.
Also switch to dblatex since db2pdf is unable to handle XML on Fedora (and probably
other distributions).

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: fix length value of concat data
Patrick McHardy [Mon, 14 Apr 2014 05:41:04 +0000 (07:41 +0200)] 
netlink: fix length value of concat data

The length is measured in bytes, not bits.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agogmputil: use MSF/LSF in import/export functions dependant on host byte order
Patrick McHardy [Sat, 12 Apr 2014 08:40:08 +0000 (10:40 +0200)] 
gmputil: use MSF/LSF in import/export functions dependant on host byte order

For data of byteorder BYTEORDER_HOST_ENDIAN we need to set the word order
dependant on the host byte order.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoexpression: fix constant expression allocation on big endian
Arturo Borrero Gonzalez [Sat, 12 Apr 2014 08:40:08 +0000 (10:40 +0200)] 
expression: fix constant expression allocation on big endian

When allocating a constant expression, a pointer to the data is passed
to the allocation function. When the variable used to store the data
is larger than the size of the data type, this fails on big endian since
the most significant bytes (being zero) come first.

Add a helper function to calculate the proper address for the cases
where this is needed.

This currently affects symbolic tables for values < u64 and payload
dependency generation for protocol values < u32.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge branch 'master' of git.netfilter.org:nftables
Patrick McHardy [Fri, 11 Apr 2014 13:55:24 +0000 (15:55 +0200)] 
Merge branch 'master' of git.netfilter.org:nftables

11 years agoparser: fix ether keyword clash
Patrick McHardy [Fri, 11 Apr 2014 13:50:15 +0000 (15:50 +0200)] 
parser: fix ether keyword clash

Due to the renaming of the "eth" keyword to "ether", parsing of arphrd
type "ether" fails.

Fix similar to TCP, UDP etc by allocating a constant arphrd expression for
the ether keyword without a following key.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agobuild: drop AC_FUNC_MALLOC/REALLOC
Florian Westphal [Thu, 10 Apr 2014 10:27:07 +0000 (12:27 +0200)] 
build: drop AC_FUNC_MALLOC/REALLOC

Two issues with these:
1. They compile & run a test program, which won't work when cross-compiling
2. When libnftnl has just been installed and is not (yet) in linker path, the
test fails since loader won't find libnftnl.

In that case configure will succeed without obvious errors, but config.h
re-defines malloc/realloc with rpl_ prefix, which then results in a
linker error ("undefined reference to `rpl_realloc'") on 'make'.

These macros are only useful to check that malloc(0) returns non-NULL
and that realloc(NULL, ... works.

For nftables the former is irrelevant and the latter a safe assumption,
so lets just remove them.

Signed-off-by: Florian Westphal <fw@strlen.de>
11 years agoutils: fix -Wcast-align warnings on sparc
Patrick McHardy [Wed, 9 Apr 2014 14:51:21 +0000 (16:51 +0200)] 
utils: fix -Wcast-align warnings on sparc

The cast to char * in the container_of() marcro causes warnings for all
list iteration helpers on sparc:

warning: cast increases required alignment of target type [-Wcast-align]

Fix by using a void * for address calculations.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agorule: fix crash in set listing
Ana Rey [Tue, 8 Apr 2014 08:19:41 +0000 (10:19 +0200)] 
rule: fix crash in set listing

It fixes an invalid read that is shown by valgrind.

==3962== Invalid read of size 4
==3962==    at 0x407040: do_command (rule.c:692)
==3962==    by 0x40588C: nft_run (main.c:183)
==3962==    by 0x405469: main (main.c:334)
==3962==  Address 0x10 is not stack'd, malloc'd or (recently) free'd

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoexpr: do not suppress OP_EQ when RHS is bitmask type
Florian Westphal [Mon, 17 Mar 2014 22:03:43 +0000 (23:03 +0100)] 
expr: do not suppress OP_EQ when RHS is bitmask type

bitmask types default to flagcmp now, thus do not suppress OP_EQ.  Else,

rule filter output tcp flags syn
rule filter output tcp flags == syn

are both displayed as 'flags syn'.

Signed-off-by: Florian Westphal <fw@strlen.de>
11 years agonftables: Fix list of sets by family
Ana Rey [Fri, 28 Mar 2014 12:30:27 +0000 (12:30 +0000)] 
nftables: Fix list of sets by family

Fix the result of command line 'nft list sets FAMILY'. It shows the
following error message:

"Error: syntax error, unexpected end of file, expecting string"

Now, it is possible shows right this information:

$ sudo nft -nna list sets ip
        set set_test {
                type ipv4_address
                elements = { 192.168.3.45, 192.168.3.43, 192.168.3.42, 192.168.3.4}
        }
set set_test2 {
                type ipv4_address
                elements = { 192.168.3.43, 192.168.3.42, 192.168.3.4}
        }
set set0 {
                type ipv4_address
                flags constant
                elements = { 127.0.0.12, 12.11.11.11}
        }

Signed-off-by: Ana Rey <anarey@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoct: add support for setting ct mark
Arturo Borrero [Wed, 12 Mar 2014 18:03:19 +0000 (19:03 +0100)] 
ct: add support for setting ct mark

This patch adds the possibility to set ct keys using nft. Currently, the
connection mark is supported. This functionality enables creating rules
performing the same action as iptables -j CONNMARK --save-mark. For example:

table ip filter {
chain postrouting {
type filter hook postrouting priority 0;
ip protocol icmp ip daddr 8.8.8.8 ct mark set meta mark
}
}

My patch is based on the original http://patchwork.ozlabs.org/patch/307677/
by Kristian Evensen <kristian.evensen@gmail.com>.

I simply did a rebase and some testing. To test, I added rules like these:
 counter meta mark set 1 counter
 counter ct mark set mark counter
 counter ct mark 1 counter

The last matching worked as expected, which means the second rule is also
working as expected.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Acked-by: Kristian Evensen <kristian.evensen@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: fix expr_binary_error()-related compilation warnings
Arturo Borrero [Wed, 12 Mar 2014 15:53:22 +0000 (16:53 +0100)] 
src: fix expr_binary_error()-related compilation warnings

The commit e7b43ec0 [expr: make expr_binary_error() usable outside of evaluation]
seem to change expr_binary_error() interface.

Later, several compilation warning appears.

The expr_binary_error() function and expr_error() macro both expect
`struct list_head *', so I simply changed callers to send `ctx->msgs'.

[...]
src/evaluate.c: In function ‘byteorder_conversion’:
src/evaluate.c:166:3: warning: passing argument 1 of ‘expr_binary_error’ from incompatible pointer type [enabled by default]
In file included from src/evaluate.c:21:0:
include/expression.h:275:12: note: expected ‘struct list_head *’ but argument is of type ‘struct eval_ctx *’
src/evaluate.c: In function ‘expr_evaluate_symbol’:
src/evaluate.c:204:4: warning: passing argument 1 of ‘expr_binary_error’ from incompatible pointer type [enabled by default]
In file included from src/evaluate.c:21:0:
include/expression.h:275:12: note: expected ‘struct list_head *’ but argument is of type ‘struct eval_ctx *’
[...]

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agodatatype: don't leak file ptr on error
Florian Westphal [Sun, 9 Mar 2014 10:57:46 +0000 (11:57 +0100)] 
datatype: don't leak file ptr on error

Signed-off-by: Florian Westphal <fw@strlen.de>
11 years agosegtree: sort set elements before decomposition
Patrick McHardy [Fri, 7 Mar 2014 10:30:10 +0000 (11:30 +0100)] 
segtree: sort set elements before decomposition

The decomposition phase currently depends on the kernel returning elements
in sorted order. This is a fragile assumption, change the code to sort the
elements itself.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoset: properly account set size when merging recursive set definitions
Patrick McHardy [Fri, 7 Mar 2014 10:20:26 +0000 (11:20 +0100)] 
set: properly account set size when merging recursive set definitions

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: add grammatical distinction for verdict maps
Patrick McHardy [Mon, 17 Feb 2014 19:43:36 +0000 (19:43 +0000)] 
parser: add grammatical distinction for verdict maps

Currently the parser accepts verdicts in regular maps and non-verdicts
in verdict maps and we have to check matching types during evaluation.
Add grammar rules for verdict maps and seperate them from regular maps.
This has a couple of advantages:

- We recognize verdict maps completely in the parser and any attempt to
  mix verdicts and other expressions will result in a syntax error.
  So far this hasn't actually been checked.

- Using verdicts in non-verdict mappings will also result in a syntax
  error instead of a datatype mismatch.

- There's a grammatical distinction between dictionaries and verdict
  maps, which are actually statements.

This is needed as preparation for a following patch to turn verdicts
into pure statements, which in turn is needed to reinstate support for
using the queue verdict in maps, which was broken by the introduction
of the queue statement.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: use set location for IO errors
Patrick McHardy [Fri, 7 Mar 2014 09:57:08 +0000 (10:57 +0100)] 
netlink: use set location for IO errors

We currently crash when reporting a permission denied error for set additions.
This is due to using the wrong location, fix by passing in the set location.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoset: abort on interval conflicts
Patrick McHardy [Thu, 6 Mar 2014 15:26:09 +0000 (16:26 +0100)] 
set: abort on interval conflicts

We currently print a debug message (with debugging) and continue. Output
a proper error message and abort.

While at it, make sure we only report a conflict if there actually is one.
This is not the case similar actions, IOW in case of sets, never, in case
of maps, only if the mapping differs.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoexpr: add comparison function for singleton expressions
Patrick McHardy [Thu, 6 Mar 2014 16:03:19 +0000 (17:03 +0100)] 
expr: add comparison function for singleton expressions

Singed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoexpr: make expr_binary_error() usable outside of evaluation
Patrick McHardy [Thu, 6 Mar 2014 15:29:52 +0000 (16:29 +0100)] 
expr: make expr_binary_error() usable outside of evaluation

Turn the eval_ctx argument into a list_head to queue the error to.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agosrc: add support for rule human-readable comments
Pablo Neira Ayuso [Wed, 26 Feb 2014 00:51:31 +0000 (01:51 +0100)] 
src: add support for rule human-readable comments

This patch adds support for human-readable comments:

  nft add rule filter input accept comment \"accept all traffic\"

Note that comments *always* come at the end of the rule. This uses
the new data area that allows you to attach information to the rule
via netlink.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: fix chain attribute parsing
Arturo Borrero [Wed, 26 Feb 2014 17:46:59 +0000 (18:46 +0100)] 
netlink: fix chain attribute parsing

The handle's table was being set to the chain name instead of the
chain table attribute.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoct: connlabel matching support
Florian Westphal [Tue, 22 Oct 2013 13:03:52 +0000 (15:03 +0200)] 
ct: connlabel matching support

Takes advantage of the fact that the current maximum label storage area
is 128 bits, i.e. the dynamically allocated extension area in the
kernel will always fit into a nft register.

Currently this re-uses rt_symbol_table_init() to read connlabel.conf.
This works since the format is pretty much the same.

Signed-off-by: Florian Westphal <fw@strlen.de>
11 years agonetlink: delete unused variable
Arturo Borrero [Mon, 17 Feb 2014 10:10:57 +0000 (11:10 +0100)] 
netlink: delete unused variable

The table object that is allocated is unused.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoct: direction should be integer, not bitmask
Florian Westphal [Mon, 17 Feb 2014 00:05:37 +0000 (01:05 +0100)] 
ct: direction should be integer, not bitmask

should always generate cmp op (its enum 0, 1 in kernel).

Note: 'original,reply' will no longer work after this patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink_delinearize: meta: fix wrong type in attributes
Florian Westphal [Mon, 17 Feb 2014 15:48:05 +0000 (16:48 +0100)] 
netlink_delinearize: meta: fix wrong type in attributes

We segfault on 'list filter' when meta expr is used as _u8
returns invalid register 0.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agonetlink: fix prefix expression handling
Patrick McHardy [Mon, 17 Feb 2014 14:06:44 +0000 (14:06 +0000)] 
netlink: fix prefix expression handling

The prefix expression handling is full of bugs:

- netlink_gen_data() is used to construct the prefix mask from the full
  prefix expression. This is both conceptually wrong, the prefix expression
  is *not* data, and buggy, it only assumes network masks and thus only
  handles big endian types.

- Prefix expression reconstruction doesn't check whether the mask is a
  valid prefix and reconstructs crap otherwise. It doesn't reconstruct
  prefixes for anything but network addresses. On top of that its
  needlessly complicated, using the mpz values directly its a simple
  matter of finding the sequence of 1's that extend up to the full width.

- Unnecessary cloning of expressions where a simple refcount increase would
  suffice.

Rewrite that code properly.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink_delinarize: convert *all* bitmask values into individual bit values
Patrick McHardy [Sun, 16 Feb 2014 22:45:19 +0000 (22:45 +0000)] 
netlink_delinarize: convert *all* bitmask values into individual bit values

We're currently only converting bitmask types as direct argument to a
relational expression in the form of a flagcmp (expr & mask neq 0) back
into a list of bit values. This means expressions like:

tcp flags & (syn | ack) == syn | ack

won't be shown symbolically. Convert *all* bitmask values back to a sequence
of inclusive or expressions of the individual bits. In case of a flagcmp,
this sequence is further converted to a list (tcp flags syn,ack).

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agobinop: take care of operator precedence when printing binop arguments
Patrick McHardy [Sun, 16 Feb 2014 20:35:37 +0000 (20:35 +0000)] 
binop: take care of operator precedence when printing binop arguments

When the argument of a binop is a binop itself, we may need to add parens
if the precedence of the argument is lower then the binop.

Before:

tcp flags & syn | ack == syn | ack
tcp flags & syn | ack != syn | ack

After:

tcp flags & (syn | ack) == syn | ack
tcp flags & (syn | ack) != syn | ack

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoevaluate: use flagcmp for single RHS bitmask expression
Patrick McHardy [Sun, 16 Feb 2014 22:47:40 +0000 (22:47 +0000)] 
evaluate: use flagcmp for single RHS bitmask expression

Always use flagcmp for RHS bitmask expressions, independant of whether
only one or an entire list of bitmask expression is specified.

This makes sure that f.i. "tcp flags ack" will match any combinations
of ACK instead of ACK and only ACK.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge branch 'next-3.14' of git.netfilter.org:nftables into next-3.14
Patrick McHardy [Mon, 17 Feb 2014 10:47:32 +0000 (10:47 +0000)] 
Merge branch 'next-3.14' of git.netfilter.org:nftables into next-3.14

11 years agosrc: proto: fixed a rreply symbol
Ana Rey [Mon, 17 Feb 2014 10:46:11 +0000 (10:46 +0000)] 
src: proto: fixed a rreply symbol

There is a bug with rreply symbol. The rreply and reply symbol were the same.

There is a reproduction of this bug here:
$ sudo nft add rule arp art-t filter arp operation reply
$ sudo nft list table arp art-t
table arp art-t {
        chain filter {
 type filter hook input priority 0;
                 arp operation 512
        }
}

$ sudo nft add rule arp art-t filter arp operation rreply
$ sudo nft list table arp art-t
table arp art-t {
        chain filter {
 type filter hook input priority 0;
                 arp operation 512  <=====
                 arp operation 512  <=====
        }
}

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agosrc: proto: fixed a rreply symbol
Ana Rey [Mon, 17 Feb 2014 10:46:11 +0000 (10:46 +0000)] 
src: proto: fixed a rreply symbol

There is a bug with rreply symbol. The rreply and reply symbol were the same.

There is a reproduction of this bug here:
$ sudo nft add rule arp art-t filter arp operation reply
$ sudo nft list table arp art-t
table arp art-t {
        chain filter {
 type filter hook input priority 0;
                 arp operation 512
        }
}

$ sudo nft add rule arp art-t filter arp operation rreply
$ sudo nft list table arp art-t
table arp art-t {
        chain filter {
 type filter hook input priority 0;
                 arp operation 512  <=====
                 arp operation 512  <=====
        }
}

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agometa: remove line break when printing priority
Pablo Neira Ayuso [Wed, 12 Feb 2014 12:47:43 +0000 (13:47 +0100)] 
meta: remove line break when printing priority

The line break is added after printing the rule.

11 years agonetlink_linearize: fix flagcmp op
Patrick McHardy [Sun, 16 Feb 2014 18:33:16 +0000 (18:33 +0000)] 
netlink_linearize: fix flagcmp op

Florian reports that flag comparisons generate incorrect instructions:

$ nft --debug=netlink add rule filter output ct labels foo
ip filter output 0 0
 [ ct load labels => reg 1 ]
 [ bitwise reg 1 = (reg=1 & 0x00000001 0x00000000 0x00000000 0x00000000 ) ^ 0x00000000 0x00000000 0x00000000 0x00000000 ]
 [ cmp neq reg 1 0x00000001 0x00000000 0x00000000 0x00000000 ]

The "cmp new" should compare to zero. This was broken by commit aae836a7
(src: use libnftables by using expr->right instead of zero.

Slightly rearrange the code as well to prevent similar problems in the
future.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoeval: use list_splice_tail() properly
Patrick McHardy [Thu, 6 Feb 2014 17:20:11 +0000 (17:20 +0000)] 
eval: use list_splice_tail() properly

We need a real list_head to splice both the command and potential new commands
added during evaluation.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: add netlink specific location
Patrick McHardy [Wed, 5 Feb 2014 19:50:43 +0000 (19:50 +0000)] 
netlink: add netlink specific location

Add a netlink_location and use it for error messages instead of internal_location.

internal:0:0-0: Error: Could not add set: Operation not permitted
=>
netlink: Error: Could not add set: Operation not permitted

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agocmd: initialize cmd list and use list_splice_tail() for adding to command list
Patrick McHardy [Wed, 5 Feb 2014 19:03:40 +0000 (19:03 +0000)] 
cmd: initialize cmd list and use list_splice_tail() for adding to command list

With incremental evaluation we're first evaluating the command before
adding it to the global command list, so the command's list_head is
uninitialized during evaluation. We need to initialize it to handle the
case that an implicit set declaration will prepend a command to the list.

Also list_splice_tail() needs to be used instead of list_add_tail() to
add the entire list of commands.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agofiles: add inet filter table definition
Patrick McHardy [Wed, 5 Feb 2014 13:37:12 +0000 (13:37 +0000)] 
files: add inet filter table definition

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: prohibit redefinitions of symbols and verify existance on use
Patrick McHardy [Tue, 4 Feb 2014 11:22:31 +0000 (11:22 +0000)] 
parser: prohibit redefinitions of symbols and verify existance on use

This patch fixes some incorrect behaviour of symbolic variables:

- prohibit redefining an existing symbol
- verify existance of a symbolic variable during parsing instead of evaluation

The second point makes sure we don't allow recursive definitions (var = $var),
which lead to a crash due to stack exhaustion.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agotests: add two tests for error reporting
Patrick McHardy [Tue, 4 Feb 2014 08:27:22 +0000 (08:27 +0000)] 
tests: add two tests for error reporting

Mixed syntactical and non-syntactical errors in individual commands and
blocks.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: evaluate commands immediately after parsing
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
parser: evaluate commands immediately after parsing

We currently do parsing and evaluation in two seperate stages. This means
that if any error occurs during parsing, we won't evaluate the syntactical
correct commands and detect possible evaluation errors in them.

In order to improve error reporting, change this to evaluate every command
as soon as it is fully parsed.

With this in place, the ruleset can be fully validated and all errors
reported in one step:

tests/error.1:6:23-23: Error: syntax error, unexpected newline
filter input tcp dport
                      ^
tests/error.1:7:24-26: Error: datatype mismatch, expected internet network service, expression has type Internet protocol
filter input tcp dport tcp
             ~~~~~~~~~ ^^^
tests/error.1:8:24-32: Error: Right hand side of relational expression (==) must be constant
filter input tcp dport tcp dport
             ~~~~~~~~~~^^^^^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: recover from errors in any block
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
parser: recover from errors in any block

Move error recovery to the common_block definition to handle errors
in any block. Queue those errors and abort parsing once a threshold
is reached.

With this in place, we can continue parsing when errors occur and
show all of them to the user at once.

tests/error.1:3:8-8: Error: syntax error, unexpected '{', expecting string
filter {
       ^
tests/error.1:4:13-13: Error: syntax error, unexpected newline
filter input
            ^
tests/error.1:5:17-17: Error: syntax error, unexpected newline
filter input tcp
                ^
tests/error.1:6:23-23: Error: syntax error, unexpected newline
filter input tcp dport

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: close scope when encountering an error in a table or chain block
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
parser: close scope when encountering an error in a table or chain block

Close the scopes when destroying a table_block/chain_block. Also add
assertions to open_scope()/close_scope() to catch memory corruption
early.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoerec: skip includes with INDESC_INTERNAL
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
erec: skip includes with INDESC_INTERNAL

Don't display "In file included from internal:0:0-0:" for errors occuring
in a parsed file.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoscanner: update last_line in struct location
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
scanner: update last_line in struct location

Currently always has the value 0.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoscanner: don't update location's line_offset for newlines
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
scanner: don't update location's line_offset for newlines

When reset_pos() is invoked, YY_USER_ACTION() has already advanced the
line offset to the next line. This causes errors for unexpected newlines
to incorrectly show the following line when reading from files.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoevaluate: determine implicit relational op before RHS constant checks
Patrick McHardy [Tue, 4 Feb 2014 08:09:27 +0000 (08:09 +0000)] 
evaluate: determine implicit relational op before RHS constant checks

The symbol for the relational op is shown in the error message. Since
OP_IMPLICIT doesn't have a symbol, (null) is shown. Fix by determining
the implicit op before checking for constants.

Error: Right hand side of relational expression ((null)) must be constant
=>
Error: Right hand side of relational expression (==) must be constant

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge branch 'master' into next-3.14
Pablo Neira Ayuso [Mon, 3 Feb 2014 18:54:07 +0000 (19:54 +0100)] 
Merge branch 'master' into next-3.14

11 years agomnl: fix inclusion of last rule in batch page
Pablo Neira Ayuso [Thu, 30 Jan 2014 15:44:58 +0000 (16:44 +0100)] 
mnl: fix inclusion of last rule in batch page

This patch fixes the inclusion of the last rule that didn't fit
into a batch page.

When using sets this has manifested with the -EBUSY error when deleting
the table (it was still containing unused sets after the flush).

The following command line works fine here:

nft -f test ; nft flush table filter ; nft delete chain filter output; nft delete table filter

Tested using this kernel patch: http://patchwork.ozlabs.org/patch/314143/

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoRevert "set: add abstract set descriptions"
Patrick McHardy [Thu, 23 Jan 2014 17:47:06 +0000 (17:47 +0000)] 
Revert "set: add abstract set descriptions"

This reverts commit 2f61f093c3149465f2a68764b25c817adbe87fcd.

Crap, accidentally committed this.

11 years agoruleset: add XML/JSON export
Arturo Borrero Gonzalez [Thu, 23 Jan 2014 17:42:34 +0000 (17:42 +0000)] 
ruleset: add XML/JSON export

This patch adds the following operation:

 :~# nft export <xml|json>

The XML/JSON output is provided raw by libnftnl, thus without format.

In case of XML, you can give format with the `xmllint' tool from libxml2-tools:
 :~# nft list ruleset xml | xmllint --format -

In case of JSON, you can use `json_pp' from perl standar package:
 :~# nft list ruleset json | json_pp

A format field is added in struct cmd, and it will be reused in the import
operation.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoset: add abstract set descriptions
Patrick McHardy [Wed, 22 Jan 2014 20:53:12 +0000 (20:53 +0000)] 
set: add abstract set descriptions

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agocmd: add create command for tables and chains
Patrick McHardy [Tue, 21 Jan 2014 12:39:31 +0000 (12:39 +0000)] 
cmd: add create command for tables and chains

We currently always use NLM_F_EXCL for add, which makes adding existing
chains or tables fail. There's usually no reason why you would care about
this, so change "add" to not use NLM_F_EXCL and add a new "create" command
in case you do care.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agobump release number to 0.100
Pablo Neira Ayuso [Mon, 20 Jan 2014 14:09:36 +0000 (15:09 +0100)] 
bump release number to 0.100

Signed-off-by: Pablo Neira Ayuso <pablo@gnumonks.org>
11 years agobuild: use libnftnl instead of libnftables in configure.in
Pablo Neira Ayuso [Mon, 20 Jan 2014 14:02:50 +0000 (15:02 +0100)] 
build: use libnftnl instead of libnftables in configure.in

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoMerge remote-tracking branch 'origin/master' into next-3.14
Patrick McHardy [Mon, 20 Jan 2014 13:38:40 +0000 (13:38 +0000)] 
Merge remote-tracking branch 'origin/master' into next-3.14

11 years agonftables: version 0.099 v0.099
Patrick McHardy [Mon, 20 Jan 2014 09:56:05 +0000 (09:56 +0000)] 
nftables: version 0.099

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agouse new libnftnl library name
Pablo Neira Ayuso [Mon, 20 Jan 2014 09:49:21 +0000 (10:49 +0100)] 
use new libnftnl library name

Adapt the current code to use the new library name libnftnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agomnl: fix chain type autoloading
Pablo Neira Ayuso [Fri, 17 Jan 2014 12:20:39 +0000 (13:20 +0100)] 
mnl: fix chain type autoloading

Add missing NLM_F_CREATE flag when creating new chains to trigger
module autoloading in the kernel.

Reported-by: Ana Rey Botello <anarey@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agodatatype: add time type parser and adapt output
Pablo Neira Ayuso [Thu, 16 Jan 2014 21:01:02 +0000 (22:01 +0100)] 
datatype: add time type parser and adapt output

This patch allows to specify a string to indicate the time, eg.

nft add rule filter output ct expiration \"1d2h3m4s\" counter

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoMerge remote-tracking branch 'origin/master' into next-3.14
Patrick McHardy [Thu, 16 Jan 2014 20:39:20 +0000 (20:39 +0000)] 
Merge remote-tracking branch 'origin/master' into next-3.14

Signed-off-by: Patrick McHardy <kaber@trash.net>
Conflicts:
include/nftables.h
src/main.c

11 years agoset: make set initializer parsable
Patrick McHardy [Thu, 16 Jan 2014 20:01:56 +0000 (20:01 +0000)] 
set: make set initializer parsable

If a set contains elements, the output is not parsable since the
elements = { ... } is not understood by the parser. Fix this and
also add support for creating constant sets (which only makes sense
when using an initializer).

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoset: make set flags output parsable
Patrick McHardy [Thu, 16 Jan 2014 20:01:56 +0000 (20:01 +0000)] 
set: make set flags output parsable

This patch fixes two problems:

- the output of "nft list table ..." is not parsable if sets are included
  because the parser can't parse the flags.

- set flags can't be specified during set creation.

To fix this, the set output is changed to:

- not print each flag on a single line
- prefix the flags with "flags "
- only show the interval flag since all others are for internal use only

The parser is changed to parse the flags specified in a set declaration.
This allows to parse empty sets. The following patch will take care of
parsing sets that are already populated.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agosrc: use ':' instead of '=>' in dictionaries
Pablo Neira Ayuso [Thu, 16 Jan 2014 17:31:16 +0000 (18:31 +0100)] 
src: use ':' instead of '=>' in dictionaries

Replace => by : to make it easier for most shell users, as
> implies a redirection, let's avoid possible confusion that
may result if you forget to escape it.

This works fine if you don't forget to add space between the
key and the value. If you forget to add the space, depending
on the case, the scanner may recognize it correctly or process
it as a string.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosrc: add new --debug=mnl option to enable libmnl debugging
Pablo Neira Ayuso [Thu, 16 Jan 2014 16:42:02 +0000 (17:42 +0100)] 
src: add new --debug=mnl option to enable libmnl debugging

This allows you to dump the netlink message that is send via
libmnl.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agobuild: fix recursive parser.h inclusion
Patrick McHardy [Thu, 16 Jan 2014 18:12:06 +0000 (18:12 +0000)] 
build: fix recursive parser.h inclusion

Ocassionally when regenerating the scanner or parser (not sure which),
recursive inclusion of the parser.h file occurs. The reason is that
bison doesn't generate a header sandwich to protect against this (*sigh*).

Change the makefile to do this manually.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: use symbolic expressions for parsing keywords as protocol values
Patrick McHardy [Thu, 16 Jan 2014 16:54:18 +0000 (16:54 +0000)] 
parser: use symbolic expressions for parsing keywords as protocol values

For "meta protocol" and the "meta nfproto" expressions, we need to be
able to parse "ip", "ip6", "vlan" and "arp" as protocol values.

Since the interpretation depends on the LHS of the relaltional expression,
we need to use symbolic expressions instead of constants to defer parsing
to the evaluation phase.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge remote-tracking branch 'origin/master' into next-3.14
Patrick McHardy [Thu, 16 Jan 2014 17:22:56 +0000 (17:22 +0000)] 
Merge remote-tracking branch 'origin/master' into next-3.14

Signed-off-by: Patrick McHardy <kaber@trash.net>
Conflicts:
src/payload.c

11 years agosegtree: fix decomposition of unclosed intervals
Patrick McHardy [Thu, 16 Jan 2014 17:11:12 +0000 (17:11 +0000)] 
segtree: fix decomposition of unclosed intervals

If intervals are directly adjacent or extend to the right end of the dimension,
they are not closed by a EXPR_F_INTERVAL_END entry. This leads to multiple
errors when decomposing the intervals:

- the last unclosed interval is not shown at all.

- if a range is unclosed and the set is a map, the starting point of the
  next interval is set to the data, not the key, leading to nonsensical
  output.

- if a prefix is unclosed, the interval is assumed to be a prefix as well
  and the same starting point is kept. This makes sense for cases like
  192.168.0.0/24, 192.168.0.0/16, but leads to hard to understand
  results if the next interval is not representable as a prefix.

Fix this by doing two things:

- add an EXPR_F_INTERVAL_END element for each unclosed interval during
  preprocessing.

- process the final unclosed interval extending to the right end of the
  dimension, if present.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agosegtree: only use prefix expressions for ranges for selected datatypes
Patrick McHardy [Thu, 16 Jan 2014 17:11:12 +0000 (17:11 +0000)] 
segtree: only use prefix expressions for ranges for selected datatypes

It is uncommon to represent f.i. port number ranges as prefix expressions.

Introduce a datatype DTYPE_F_PREFIX flag to indicate that the preferred
representation of a range is a prefix and use it for segtree decomposition
to decide whether to use a range or prefix expression.

The ipaddr, ip6addr, mark and realm datatypes are changed to include the
DTYPE_F_PREFIX flag.

This fixes completely unreadable output in cases where the ranges are
representable as prefixes, f.i. in case of port number:

{ 0/6 => jump chain1, 0/5 => jump chain2, 0/4 => continue}

becomes:

{ 0-1023 => jump chain1, 1024-2047 => jump chain2, 2048-4095 => continue}

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoparser: fix compilation breakage
Patrick McHardy [Thu, 16 Jan 2014 17:11:12 +0000 (17:11 +0000)] 
parser: fix compilation breakage

Commit 0bd59cf4da (parser: fix parsing of ethernet protocol types) broke
compilation:

src/parser.y:26:22: fatal error: if_ether.h: No such file or directory

Should be netinet/if_ether.h.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoexpression: fix output of verdict maps
Pablo Neira [Mon, 13 Jan 2014 12:39:16 +0000 (13:39 +0100)] 
expression: fix output of verdict maps

% nft list table filter
table ip filter {
...
chain output {
...
ip saddr map { 1.1.1.1 => accept}
}
}

It displays 'map' instead of 'vmap'. Fix it by checking the mapping
type in map_expr_print().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agopayload: fix inconsistency in ethertype output
Pablo Neira Ayuso [Wed, 15 Jan 2014 20:20:37 +0000 (21:20 +0100)] 
payload: fix inconsistency in ethertype output

Use ip6 instead of ipv6.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agopayload: fix crash when wrong ethernet protocol type is used
Pablo Neira Ayuso [Wed, 15 Jan 2014 20:19:02 +0000 (21:19 +0100)] 
payload: fix crash when wrong ethernet protocol type is used

nft add rule ip filter output meta protocol xyz counter
                                            ^^^

This fix is similar to 4097ad7 ("meta: fix crash when parsing
unresolvable mark values").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoparser: fix parsing of ethernet protocol types
Pablo Neira Ayuso [Wed, 15 Jan 2014 20:12:38 +0000 (21:12 +0100)] 
parser: fix parsing of ethernet protocol types

This allows us to use the protocol type keyword, eg.

nft add rule ip filter output meta protocol ip6 counter
                                            ^^^

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoMerge remote-tracking branch 'origin/master' into next-3.14
Patrick McHardy [Wed, 15 Jan 2014 15:45:02 +0000 (15:45 +0000)] 
Merge remote-tracking branch 'origin/master' into next-3.14

Signed-off-by: Patrick McHardy <kaber@trash.net>
Conflicts:
include/nftables.h
src/main.c

11 years agoexpr: relational: don't surpress '==' for LHS binops in output
Patrick McHardy [Wed, 15 Jan 2014 15:39:10 +0000 (15:39 +0000)] 
expr: relational: don't surpress '==' for LHS binops in output

This patch changes the output of relational expressions to not surpress
the '==' when the LHS is a binop, f.i.

... meta mark & 0x00000003 0x00000001

becomes

... meta mark & 0x00000003 == 0x00000001

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agonetlink: use stdout for debugging
Pablo Neira Ayuso [Wed, 15 Jan 2014 14:56:57 +0000 (15:56 +0100)] 
netlink: use stdout for debugging

Suggested-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agosegtree: add new segtree debugging option
Pablo Neira Ayuso [Wed, 15 Jan 2014 12:14:25 +0000 (13:14 +0100)] 
segtree: add new segtree debugging option

Currently, nft displays the debugging information if it's compiled with
--enable-debug (which seems a good idea) and when intervals are used
in maps. Add a new option to enable debugging to segtree, so we only
get this information when explicitly requested.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 years agoMerge branch 'master' into next-3.14
Patrick McHardy [Wed, 15 Jan 2014 11:59:16 +0000 (11:59 +0000)] 
Merge branch 'master' into next-3.14

Signed-off-by: Patrick McHardy <kaber@trash.net>
Conflicts:
src/payload.c

11 years agopayload: fix name of eth_proto
Patrick McHardy [Wed, 15 Jan 2014 11:57:28 +0000 (11:57 +0000)] 
payload: fix name of eth_proto

It's "ether" now since we've changed the keyword.

Signed-off-by: Patrick McHardy <kaber@trash.net>
11 years agoMerge branch 'master' into next-3.14
Patrick McHardy [Wed, 15 Jan 2014 11:55:39 +0000 (11:55 +0000)] 
Merge branch 'master' into next-3.14

Signed-off-by: Patrick McHardy <kaber@trash.net>
Conflicts:
src/meta.c
src/parser.y

11 years agometa: fix mismerge
Patrick McHardy [Wed, 15 Jan 2014 11:53:23 +0000 (11:53 +0000)] 
meta: fix mismerge

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