]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
10 years agonetlink: handle concat expressions in set data
Patrick McHardy [Sun, 12 Apr 2015 20:10:42 +0000 (21:10 +0100)] 
netlink: handle concat expressions in set data

Reconstruct the concat expressions in set data by splicing off the
subtype values based on the keytype of the set.

Signed-off-by: Patrick McHardy
10 years agonetlink_delinearize: handle relational and lookup concat expressions
Patrick McHardy [Sun, 12 Apr 2015 20:10:42 +0000 (21:10 +0100)] 
netlink_delinearize: handle relational and lookup concat expressions

When the RHS length differs from the LHS length (which is only the
first expression), both expressions are assumed to be concat expressions.

The LHS concat expression is reconstructed from the available register
values, advancing by the number of registers required by the subexpressions'
register space, until the RHS length has been reached.

The RHS concat expression is reconstructed by splitting the data value
into multiple subexpressions based on the LHS concat expressions types.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinearize: introduce register translation helper
Patrick McHardy [Sun, 12 Apr 2015 20:10:41 +0000 (21:10 +0100)] 
netlink_delinearize: introduce register translation helper

Introduce a helper function to translate register numbers from the kernel
from the compat values to the NFT_REG32 values.

Internally we use the register numbers 0-16:

* 0 is the verdict register in both old and new addressing modes.
* 1-16 are the 32 bit data registers

The NFT_REG32_00 values are mapped to 1-16, the NFT_REG_1-NFT_REG_4
values are each use up 4 registers starting at 1 (1, 5, 9, 13).

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: pad constant concat sub-expressions
Patrick McHardy [Sun, 12 Apr 2015 20:10:42 +0000 (21:10 +0100)] 
netlink: pad constant concat sub-expressions

Pad all but the last sub-expressions of a concat expressions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_linearize: generate concat expressions
Patrick McHardy [Mon, 13 Apr 2015 11:19:12 +0000 (12:19 +0100)] 
netlink_linearize: generate concat expressions

Use the real length for reserving/releasing registers when generating
concat expressions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_linearize: use NFT_REG32 values internally
Patrick McHardy [Sun, 12 Apr 2015 20:10:41 +0000 (21:10 +0100)] 
netlink_linearize: use NFT_REG32 values internally

Prepare netlink_linearize for 32 bit register usage:

Switch to use 16 data registers of 32 bit each. A helper function takes
care of mapping the registers to the NFT_REG32 values and, if the
register refers to the beginning of an 128 bit area, the old NFT_REG_1-4
values for compatibility.

New register reservation and release helper function take the size into
account and reserve the required amount of registers.

The reservation and release functions will so far still always allocate
128 bit. If no other expression in a rule uses a 32 bit register directly,
these will be mapped to the old register values, meaning everything
continues to work with old kernel versions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: pass expression to register allocation/release functions
Patrick McHardy [Mon, 20 Apr 2015 12:45:40 +0000 (14:45 +0200)] 
netlink: pass expression to register allocation/release functions

Prepare for taking the expression size into account.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoheaders: sync headers for new register values
Patrick McHardy [Sun, 12 Apr 2015 20:10:41 +0000 (21:10 +0100)] 
headers: sync headers for new register values

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoeval: prohibit variable sized types in concat expressions
Patrick McHardy [Tue, 2 Jun 2015 10:16:42 +0000 (12:16 +0200)] 
eval: prohibit variable sized types in concat expressions

Since we need to calculate the length of the entire concat type, we can
not support variable sized types where the length can't be determined
by the type.

This only affects base types since all higher types include a length.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoMerge remote-tracking branch 'origin/master' into next-4.1
Patrick McHardy [Tue, 2 Jun 2015 11:03:33 +0000 (13:03 +0200)] 
Merge remote-tracking branch 'origin/master' into next-4.1

10 years agonetlink_delinearize: remove obsolete fixme
Patrick McHardy [Tue, 2 Jun 2015 10:53:11 +0000 (12:53 +0200)] 
netlink_delinearize: remove obsolete fixme

The FIXME was related to exclusion of string types from cmp length checks.
Since with fixed sized helper names the last case where this could happen
is gone, remove the FIXME and perform length checks on strings as well.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoct: add maximum helper length value
Patrick McHardy [Tue, 2 Jun 2015 10:53:10 +0000 (12:53 +0200)] 
ct: add maximum helper length value

The current kernel restricts ct helper names to 16 bytes length. Specify
this limit in the ct expression table to catch oversized strings in userspace.
Since older versions of nft didn't support larger values, this does not
negatively affect interaction with old kernel versions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_linearize: fix range cmp instruction generation
Patrick McHardy [Tue, 2 Jun 2015 10:53:10 +0000 (12:53 +0200)] 
netlink_linearize: fix range cmp instruction generation

The LHS expression is generated twice and the register not properly released.
Fix by calling netlink_gen_range() before generating the LHS.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agomnl: use new libnftnl batch API
Pablo Neira Ayuso [Tue, 14 Apr 2015 12:59:15 +0000 (14:59 +0200)] 
mnl: use new libnftnl batch API

Each batch page has a size of 320 Kbytes, and the limit has been set to 256
KBytes, so the overrun area is 64 KBytes long to accomodate the largest netlink
message (sets).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonftables: add set statemet
Patrick McHardy [Thu, 19 Mar 2015 13:34:18 +0000 (13:34 +0000)] 
nftables: add set statemet

The set statement is used to dynamically add or update elements in a set.

Syntax:

# nft filter input set add tcp dport @myset
# nft filter input set add ip saddr timeout 10s @myset
# nft filter input set update ip saddr timeout 10s @myset

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosetelem: add support for attaching comments to set elements
Patrick McHardy [Fri, 20 Mar 2015 06:30:29 +0000 (06:30 +0000)] 
setelem: add support for attaching comments to set elements

Syntax:

# nft add element filter test { 192.168.0.1 comment "some host" }

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosetelem: add timeout support for set elements
Patrick McHardy [Thu, 19 Mar 2015 13:34:18 +0000 (13:34 +0000)] 
setelem: add timeout support for set elements

Support specifying per element timeout values and displaying the expiration
time.

If an element should not use the default timeout value of the set, an
element specific value can be specified as follows:

# nft add element filter test { 192.168.0.1, 192.168.0.2 timeout 10m}

For listing of elements that use the default timeout value, just the
expiration time is shown, otherwise the element specific timeout value
is also displayed:

set test {
type ipv4_addr
timeout 1h
elements = { 192.168.0.2 timeout 10m expires 9m59s, 192.168.0.1 expires 59m59s}
}

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset: add timeout support for sets
Patrick McHardy [Mon, 23 Mar 2015 21:34:57 +0000 (21:34 +0000)] 
set: add timeout support for sets

Timeout support can be enabled in one of two ways:

1. Using a default timeout value:

set test {
type ipv4_addr;
timeout 1h;
}

2. Using the timeout flag without a default:

set test {
type ipv4_addr;
flags timeout;
}

Optionally a garbage collection interval can be specified using

gc-interval <interval>;

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: add set_elem_expr as container for set element attributes
Patrick McHardy [Sat, 11 Apr 2015 16:02:13 +0000 (17:02 +0100)] 
expr: add set_elem_expr as container for set element attributes

Add a new expression type "set_elem_expr" that is used as container for
the key in order to attach different attributes, such as timeout values,
to the key.

The expression hierarchy is as follows:

Sets:

    elem
     |
    key

Maps:

   mapping
   /      \
 elem    data
  |
 key

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoparser: fix inconsistencies in set expression rules
Patrick McHardy [Sun, 12 Apr 2015 09:41:56 +0000 (10:41 +0100)] 
parser: fix inconsistencies in set expression rules

Set keys are currently defined as a regular expr for pure sets and
map_lhs_expr for maps. map_lhs_expr is what can actually be used for
a single member, namely a concat_expr or a multiton_expr. The reason
why pure sets use expr for the key is to allow recursive set specifications,
which doesn't make sense for maps since every element needs a mapping.

However, the rule is too wide and also allows map expressions as a key,
which obviously doesn't make sense.

Rearrange the rules so we have:

set_lhs_expr: concat or multiton
set_rhs_expr: concat or verdict

and special case the recursive set specifications, as they deserve.

Besides making it a lot easier to understand what is actually supported,
this will be used by the following patch to support timeouts and comments
for keys in a uniform way.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoparser: add a time_spec rule
Patrick McHardy [Sat, 11 Apr 2015 14:41:32 +0000 (15:41 +0100)] 
parser: add a time_spec rule

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: seperate time parsing/printing from time_type
Patrick McHardy [Sat, 11 Apr 2015 14:59:44 +0000 (15:59 +0100)] 
datatype: seperate time parsing/printing from time_type

Seperate relative time parsing and printing from the time_type to make
it usable for set and set element time related parameters.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: less strict time parsing
Patrick McHardy [Sat, 11 Apr 2015 13:56:16 +0000 (14:56 +0100)] 
datatype: less strict time parsing

Don't require hours to be in range 0-23 and minutes/seconds in range 0-59.
The time_type is used for relative times where it is entirely reasonable
to specify 180s instead of 3m.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: fix parsing of time type
Patrick McHardy [Sat, 11 Apr 2015 13:54:19 +0000 (14:54 +0100)] 
datatype: fix parsing of time type

Properly detect time strings in the lexer without quotation marks.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: restore interface to index cache
Pablo Neira Ayuso [Thu, 9 Apr 2015 12:15:15 +0000 (14:15 +0200)] 
src: restore interface to index cache

nftables used to have a cache to speed up interface name <-> index lookup,
restore it using libmnl.

This reduces netlink traffic since if_nametoindex() and if_indextoname() open,
send a request, receive the list of interface and close a netlink socket for
each call.  I think this is also good for consistency since nft -f will operate
with the same index number when reloading the ruleset.

The cache is populated by when nft_if_nametoindex() and nft_if_indextoname()
are used for first time. Then, it it released in the output path. In the
interactive mode, it is invalidated after each command.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: introduce netlink_init_error()
Pablo Neira Ayuso [Mon, 6 Apr 2015 11:57:10 +0000 (13:57 +0200)] 
src: introduce netlink_init_error()

Based on the existing netlink_open_error(), but indicate file and line
where the error happens. This will help us to diagnose what is going
wrong when users can back to us to report problems.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agomonitor: fix missing space after chain name
Arturo Borrero [Tue, 7 Apr 2015 15:05:14 +0000 (17:05 +0200)] 
monitor: fix missing space after chain name

Due to change f3ff9e9 ("rule: delete extra space in rule indentation") in
function rule_print(), a missing space happens in monitor.

before this patch:
 % nft monitor
 add rule ip test-table test-chainip protocol tcp

after this patch:
 % nft monitor
 add rule ip test-table test-chain ip protocol tcp

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: remove duplicated grammar for chain policy
Patrick McHardy [Tue, 24 Mar 2015 10:59:26 +0000 (10:59 +0000)] 
parser: remove duplicated grammar for chain policy

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinarize: fix payload dependency killing of link layer dependencies
Patrick McHardy [Tue, 24 Mar 2015 14:20:22 +0000 (14:20 +0000)] 
netlink_delinarize: fix payload dependency killing of link layer dependencies

payload_dependency_kill() does not properly handle dependencies for link
layer expressions. Since those dependencies are logically defined on an
even lower layer (device layer), we don't have a payload base for them,
meaning they will use PROTO_BASE_INVALID, which is skipped.

So instead of storing the payload base on which the dependency is defined,
we store the base of the layer for which the dependency applies, meaning
dependencies defined by the device layer will properly work.

This fixes killing the dependency of ether saddr, instead of

iiftype ether ether ether saddr ...

we now only display

ether saddr ...

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: fix use after free in netlink_get_table()
Patrick McHardy [Tue, 24 Mar 2015 11:51:45 +0000 (11:51 +0000)] 
netlink: fix use after free in netlink_get_table()

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonft-test: don't use colors if output is not a tty
Patrick McHardy [Wed, 25 Mar 2015 07:24:58 +0000 (07:24 +0000)] 
nft-test: don't use colors if output is not a tty

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset_elem: convert flag value to inclusive-OR binops during delinearize
Patrick McHardy [Mon, 23 Mar 2015 23:36:35 +0000 (23:36 +0000)] 
set_elem: convert flag value to inclusive-OR binops during delinearize

When a set contains combined flag values, they are displayed as numeric
value since they don't match any of the single flag values:

tcp flags { 18, psh, syn}

Convert to a series of inclusive-OR binops of the single flag values
for improved readability:

tcp flags { syn | ack, psh, syn}

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agorule: fix chain details align indentations
Arturo Borrero [Thu, 19 Mar 2015 12:37:59 +0000 (13:37 +0100)] 
rule: fix chain details align indentations

f.i:
 type filter hook output priority 0; policy accept;
ip daddr @test counter packets 14 bytes 1176

Reported-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: fix bogus warnings in any/mark.t
Pablo Neira Ayuso [Wed, 18 Mar 2015 19:33:29 +0000 (20:33 +0100)] 
tests: regression: fix bogus warnings in any/mark.t

'meta mark set 10' is a valid input, but the expected output is
'mark set 10'.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: masquerade is only allowed from postrouting
Pablo Neira Ayuso [Wed, 18 Mar 2015 19:25:27 +0000 (20:25 +0100)] 
tests: regression: masquerade is only allowed from postrouting

Disable the tests from the output chain, the kernel rejects this with
operation not supported.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: consider policy in base chain
Arturo Borrero [Wed, 18 Mar 2015 19:13:30 +0000 (20:13 +0100)] 
tests: regression: consider policy in base chain

nft now prints the default chain policy, consider this when parsing
the output to find mismatches.

Signed-off-by: Arturo Borrero <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agonetlink: fix crash when adding new non-base chain
Pablo Neira Ayuso [Wed, 18 Mar 2015 17:11:10 +0000 (18:11 +0100)] 
netlink: fix crash when adding new non-base chain

Fix crash when adding a non-base chain introduced by acdfae9 ("src:
allow to specify the default policy for base chains").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: delete extra space in rule indentation
Arturo Borrero [Wed, 18 Mar 2015 10:43:24 +0000 (11:43 +0100)] 
rule: delete extra space in rule indentation

Annoying extra space in rule indentation:

Example before this patch:

table ip6 test_table {
chain test_chain {
 counter tcp dport { 22, 80, 443} accept # handle 1
^
}
}

Example after this patch:

table ip6 test_table {
chain test_chain {
counter tcp dport { 22, 80, 443} accept # handle 1
}
}

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: missing break; in str2hooknum()
Pablo Neira Ayuso [Thu, 12 Mar 2015 13:11:57 +0000 (14:11 +0100)] 
evaluate: missing break; in str2hooknum()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: allow to specify the default policy for base chains
Pablo Neira Ayuso [Tue, 17 Mar 2015 15:36:15 +0000 (16:36 +0100)] 
src: allow to specify the default policy for base chains

The new syntax is:

 nft add chain filter input { hook input type filter priority 0\; policy accept\; }

but the previous syntax is still allowed:

 nft add chain filter input { hook input type filter priority 0\; }

this assumes default policy to accept.

If the base chain already exists, you can update the policy via:

 nft add chain filter input { policy drop\; }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agosrc: expose table flags
Pablo Neira Ayuso [Thu, 12 Mar 2015 14:15:14 +0000 (15:15 +0100)] 
src: expose table flags

The nf_tables kernel API provides a way to disable a table using the
dormant flag. This patch adds the missing code to expose this feature
through nft.

Basically, if you want to disable a table and all its chains from seen
any traffic, you have to type:

 nft add table filter { flags dormant\; }

to re-enable the table, you have to:

 nft add table filter

this clears the flags.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: fix typo in README
Eric Leblond [Wed, 25 Feb 2015 23:51:08 +0000 (00:51 +0100)] 
tests: regression: fix typo in README

Signed-off-by: Eric Leblond <eric@regit.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agomain: display errors through stderr
Pablo Neira Ayuso [Tue, 24 Feb 2015 17:48:16 +0000 (18:48 +0100)] 
main: display errors through stderr

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1000
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agorule: fix object order via nft -f
Pablo Neira Ayuso [Sat, 14 Feb 2015 20:41:23 +0000 (21:41 +0100)] 
rule: fix object order via nft -f

The objects need to be loaded in the following order:

#1 tables
#2 chains
#3 sets
#4 rules

We have to make sure that chains are in place by when we add rules with
jumps/gotos. Similarly, we have to make sure that the sets are in place
by when rules reference them.

Without this patch, you may hit ENOENT errors depending on your ruleset
configuration.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agometa: register pkttype_type datatype
Pablo Neira Ayuso [Sun, 1 Feb 2015 20:54:00 +0000 (21:54 +0100)] 
meta: register pkttype_type datatype

Closes: http://bugzilla.netfilter.org/show_bug.cgi?id=995
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: use stmt_evaluate_arg() in all cases
Patrick McHardy [Mon, 12 Jan 2015 10:10:42 +0000 (10:10 +0000)] 
evaluate: use stmt_evaluate_arg() in all cases

When using a symbolic vmap expression, we fail to verify that the map
actually contains verdicts.

Use stmt_evaluate_arg() everywhere to fix this.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoevaluate: check that map expressions' datatype matches mappings
Patrick McHardy [Mon, 12 Jan 2015 12:09:17 +0000 (12:09 +0000)] 
evaluate: check that map expressions' datatype matches mappings

Catch type errors in map expressions using named maps:

# nft add map filter test { type ipv4_addr : inet_service; }
# nft filter output mark set tcp dport map @test
<cmdline>:1:38-42: Error: datatype mismatch, map expects IPv4 address, mapping expression has type internet network service
filter output mark set tcp dport map @test
                       ~~~~~~~~~     ^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoevaluate: properly set datatype of map expression
Patrick McHardy [Mon, 12 Jan 2015 11:13:44 +0000 (11:13 +0000)] 
evaluate: properly set datatype of map expression

The datatype of the map expression is the datatype of the mappings.

# nft add map filter test { type ipv4_addr : inet_service; }
# nft filter output mark set ip daddr map @test

Before:

<cmdline>:1:24-41: Error: datatype mismatch: expected packet mark, expression has type IPv4 address
filter output mark set ip daddr map @test
              ~~~~~~~~~^^^^^^^^^^^^^^^^^^

After:

<cmdline>:1:24-41: Error: datatype mismatch: expected packet mark, expression has type internet network service
filter output mark set ip daddr map @test
              ~~~~~~~~~^^^^^^^^^^^^^^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoevaluate: verify named map is actually a map
Patrick McHardy [Mon, 12 Jan 2015 11:06:17 +0000 (11:06 +0000)] 
evaluate: verify named map is actually a map

# nft add set filter test { type ipv4_addr; }
# nft filter input ip daddr vmap @test

Before:

<cmdline>:0:0-32: Error: Could not process rule: Invalid argument
filter input ip daddr vmap @test
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

After:

<cmdline>:1:28-32: Error: Expression is not a map
filter input ip daddr vmap @test
                           ^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agometa: don't print meta keyword for unqualified meta stmts
Patrick McHardy [Mon, 12 Jan 2015 10:19:19 +0000 (10:19 +0000)] 
meta: don't print meta keyword for unqualified meta stmts

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoevaluate: clean up unused variables (pctx)
Alvaro Neira Ayuso [Mon, 12 Jan 2015 10:03:27 +0000 (10:03 +0000)] 
evaluate: clean up unused variables (pctx)

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoexpr: fix crash when listing non-verdict mappings
Patrick McHardy [Mon, 12 Jan 2015 09:48:39 +0000 (09:48 +0000)] 
expr: fix crash when listing non-verdict mappings

Fix regression introduced by commit 87c2a2205:

  netlink_delinearize: clone on netlink_get_register(), release previous on _set()

When using a non-verdict mapping, the set ref expression is assigned to the
destination register. The next get_register() will attempt to clone it and
crash because of the missing ->clone() callback.

# nft filter input meta mark set ip daddr map { 192.168.0.1 : 123 }
# nft list table filter
Segmentation fault (core dumped)

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoset: remove unused set_clone() function
Patrick McHardy [Mon, 12 Jan 2015 09:51:05 +0000 (09:51 +0000)] 
set: remove unused set_clone() function

The set_clone() function was added by the event monitor patchset and is
unused. It is also broken since it simply initializes the list head to
the list of the original set, so remove it.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoparser: properly fix handling of large integer values
Patrick McHardy [Sun, 11 Jan 2015 23:59:10 +0000 (23:59 +0000)] 
parser: properly fix handling of large integer values

Introduction of the ERROR symbol is an ugly hack. There's no reason
to special case large integer values, the NUM token only exists for
small values that are needed immediately, everything else is passed
as EXPR_SYMBOL to evaluation anyways.

Additionally the error reporting is different from what we'd usually
report, the token is easy to confuse with the bison internal error
token and it even has a name, messing up bison internal diagnostics.

Simply return values to large to be handled by strtoull as STRING.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_linearize: add register dumping helper function
Patrick McHardy [Sun, 11 Jan 2015 08:34:46 +0000 (08:34 +0000)] 
netlink_linearize: add register dumping helper function

Add a helper function to dump netlink register numbers in preparation
of concat support.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinearize: add register parsing helper function
Patrick McHardy [Sun, 11 Jan 2015 08:28:42 +0000 (08:28 +0000)] 
netlink_delinearize: add register parsing helper function

Add a helper function to parse netlink register numbers in preparation
of concat support.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoconcat: add concat subtype lookup/id helpers
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
concat: add concat subtype lookup/id helpers

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinearize: cleanup hard to read code
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
netlink_delinearize: cleanup hard to read code

The netlink parsing code is full of long function calls spawning multiple
lines and in some cases parses netlink attributes multiple times.

Use local variables for the registers and other data required to
reconstruct the expressions and statements and reorder the code in
some cases to move related processing next to each other.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinearize: rename netlink_parse_*_sreg/dreg functions
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
netlink_delinearize: rename netlink_parse_*_sreg/dreg functions

These are really badly chosen names, use parse_expr and parse_stmt instead.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: readability fixes
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
netlink: readability fixes

Improve readability by using local variables for netlink attributes,
ordering variables more logically, don't arbitrarily initialize
some variables in the definition section and in the body and generally
make similar functions look similar.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: style fixes
Patrick McHardy [Sun, 11 Jan 2015 07:52:49 +0000 (07:52 +0000)] 
netlink: style fixes

Remove style discrepancies between different netlink I/O functions:

- we don't use brackets for single line statements
- most functions don't have a newline between error reporting and exit

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: style fixes
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
netlink: style fixes

We include an empty line between variable definitions and code.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: remove unnecessary temporary variable
Patrick McHardy [Mon, 22 Dec 2014 16:45:48 +0000 (17:45 +0100)] 
netlink: remove unnecessary temporary variable

Waste less space and return the results of the batch/compat
functions directly.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink: fix memory leaks
Patrick McHardy [Sun, 11 Jan 2015 07:54:55 +0000 (07:54 +0000)] 
netlink: fix memory leaks

Fix two memory leaks in netlink event monitor. Also fix a leak related
to all sets, the ->init expression is not freed.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agonetlink_delinearize: fix error handling for invalid registers
Patrick McHardy [Fri, 26 Dec 2014 13:55:41 +0000 (14:55 +0100)] 
netlink_delinearize: fix error handling for invalid registers

netlink_delinearize is prepared to deal with malformed expressions from
the kernel that it doesn't understand. However since expressions are now
cloned unconditionally by netlink_get_register(), we crash before such
errors can be detected for invalid inputs.

Fix by only cloning non-NULL expressions.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoevaluate: add missing datatype compat checks for statement arguments
Patrick McHardy [Sat, 10 Jan 2015 09:23:00 +0000 (09:23 +0000)] 
evaluate: add missing datatype compat checks for statement arguments

Add a helper function to evaluate expressions used as arguments for
statements and report datatype mismatches.

Fixes acceptance of mismatching expressions like:

$ nft filter output meta mark set ip daddr
<cmdline>:1:29-36: Error: datatype mismatch: expected packet mark. expression has type IPv4 address
filter output meta mark set ip daddr
              ~~~~~~~~~~~~~~^^^^^^^^

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoeval: refactor NAT evaluation functions
Patrick McHardy [Sat, 10 Jan 2015 14:41:55 +0000 (14:41 +0000)] 
eval: refactor NAT evaluation functions

The redir and masq evaluation functions include some useless context
updates and checks.

Refactor the NAT code to have a single instance of address and transport
evaluation functions for simplicity and unified error reporting.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agosrc: modify pr_debug() to use printf and introduce to pr_gmp_debug()
Pablo Neira Ayuso [Thu, 8 Jan 2015 12:07:35 +0000 (13:07 +0100)] 
src: modify pr_debug() to use printf and introduce to pr_gmp_debug()

Modify pr_debug() to use printf so we get debugging traces for
proto-ctx when --with-mini-gmp is enabled.

Add pr_gmp_debug(), this is disabled with --with-mini-gmp since it
relies on the gmp_printf(), which is not available in the mini-gmp
implementation.

Suggested by Patrick.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: use -Wno-sign-compare to avoid compilation warning in mini-gmp.c
Pablo Neira Ayuso [Thu, 8 Jan 2015 12:28:26 +0000 (13:28 +0100)] 
build: use -Wno-sign-compare to avoid compilation warning in mini-gmp.c

  CC     mini-gmp.o
mini-gmp.c: In function ‘mpn_get_str_bits’:
mini-gmp.c:1176:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
mini-gmp.c: In function ‘mpz_and’:
mini-gmp.c:3650:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
mini-gmp.c: In function ‘mpz_ior’:
mini-gmp.c:3723:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
mini-gmp.c: In function ‘mpz_xor’:
mini-gmp.c:3792:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
mini-gmp.c: In function ‘mpz_set_str’:
mini-gmp.c:4167:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: add --with-mini-gmp switch to disable linking libgmp
Steven Barth [Thu, 8 Jan 2015 06:54:34 +0000 (07:54 +0100)] 
build: add --with-mini-gmp switch to disable linking libgmp

This allows to disable linking the >400 KB big libgmp and replace it
with the builtin mini-gmp which only increases size by ~30KB.

Enabling this selectively decreases debugging verbosity (pr_debug).

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodoc: add a reference to the wiki page in the man page
Arturo Borrero [Wed, 7 Jan 2015 16:03:09 +0000 (17:03 +0100)] 
doc: add a reference to the wiki page in the man page

Let's add a pointer in the man page to the wiki page.

Requested-by: Dan Jacobson <jidanni@jidanni.org>
Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoerec: use stdio vasprintf instead of gmp_vasprintf
Steven Barth [Tue, 6 Jan 2015 22:40:24 +0000 (23:40 +0100)] 
erec: use stdio vasprintf instead of gmp_vasprintf

Use stdio's vasprintf instead of gmp_vasprintf which is not part
of the mini-gmp function subset. Furthermore convert the only
gmp-specific user and allow the compiler to verify format-strings.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: use mpz_set_str instead of gmp_sscanf
Steven Barth [Tue, 6 Jan 2015 22:40:23 +0000 (23:40 +0100)] 
datatype: use mpz_set_str instead of gmp_sscanf

This simplifies the integer parsing logic and restricts it to
functions being part of the mini-gmp subset.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: rename VERSION token to HDRVERSION
Steven Barth [Tue, 6 Jan 2015 22:40:22 +0000 (23:40 +0100)] 
parser: rename VERSION token to HDRVERSION

A token name of VERSION results in a macro being defined
with the same name. This prevents inclusion of config.h
in commonly used headers.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoevaluate: reject: fix dependency generation from nft -f
Pablo Neira Ayuso [Tue, 6 Jan 2015 20:28:53 +0000 (21:28 +0100)] 
evaluate: reject: fix dependency generation from nft -f

When nft -f is used, ctx->cmd points to the table object, which
contains the corresponding chain, set and rule lists. The reject
statement evaluator relies on ctx->cmd->rule to add the payload
dependencies, which is doesn't point to the rule in that case.

This patch adds the rule context to the eval_ctx structure to update
the rule list of statements when generating dependencies, as the reject
statement needs.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=993
Reported-by: Ting-Wei Lan <lantw44@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agopayload: assert when accessing inner transport header
Pablo Neira Ayuso [Sun, 4 Jan 2015 22:22:57 +0000 (23:22 +0100)] 
payload: assert when accessing inner transport header

Instead of segfaulting due to out of bound access access to protocol
context array ctx->protocol[base].location from proto_ctx_update().

 # nft add rule filter input ah nexthdr tcp
 nft: payload.c:88: payload_expr_pctx_update: Assertion `left->payload.base + 1 <= (__PROTO_BASE_MAX - 1)' failed.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: revisit chain tests
Pablo Neira Ayuso [Thu, 18 Dec 2014 12:36:24 +0000 (13:36 +0100)] 
tests: regression: revisit chain tests

Make sure support chain don't stop working.

Remove some minor mistakes and out of scope tests from chain*.t

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: named sets work
Pablo Neira Ayuso [Thu, 18 Dec 2014 12:30:59 +0000 (13:30 +0100)] 
tests: regression: named sets work

Those tests work already, enable them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: alloc specifying concat types in set declarations
Patrick McHardy [Sat, 13 Dec 2014 07:50:36 +0000 (07:50 +0000)] 
parser: alloc specifying concat types in set declarations

Support specification of concat types in set declarations:

add set filter test {
type ipv4_addr . inet_service
}

Netlink delinearization is changed to reconstruct the type from the id.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: change concat_type_alloc() to construct type from id
Patrick McHardy [Sat, 13 Dec 2014 07:50:35 +0000 (07:50 +0000)] 
datatype: change concat_type_alloc() to construct type from id

The kernel only stored the id so we need to be able to reconstruct
the datatype from the id only.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoutils: add fls()
Patrick McHardy [Sat, 13 Dec 2014 07:50:35 +0000 (07:50 +0000)] 
utils: add fls()

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: add define for maximum number of bits and mask of datatype id
Patrick McHardy [Sat, 13 Dec 2014 07:50:35 +0000 (07:50 +0000)] 
datatype: add define for maximum number of bits and mask of datatype id

The id of concat datatypes is composed of the ids of the individual
datatypes. Add a define for the number of bits for each datatype id
and a mask.

The number of bits is chosen as 6, allowing for 63 datatypes, or twice
as much as we currently have. This allows for concatenations of 5
types using 32 bits.

The value is statically chosen instead of basing it on the current
numbers of datatypes since we don't want the maximum concatenation
size to vary between versions, also new versions are supposed to be
able to propery parse a ruleset generated by an older version.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: add new subtypes field to account number of concat data types
Patrick McHardy [Sat, 13 Dec 2014 07:50:34 +0000 (07:50 +0000)] 
datatype: add new subtypes field to account number of concat data types

Using the size is confusing since it usually holds the size of
the data. Add a new "subtypes" member, which holds the number
of datatypes the concat type is made of.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodatatype: generate name for concat types
Patrick McHardy [Sat, 13 Dec 2014 07:50:34 +0000 (07:50 +0000)] 
datatype: generate name for concat types

The name of a concat type is the names of the individual types concatenated
using a '.'.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agoBump version to v0.4 v0.4
Pablo Neira Ayuso [Mon, 15 Dec 2014 22:49:59 +0000 (23:49 +0100)] 
Bump version to v0.4

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: missing byteorder in string_type
Pablo Neira Ayuso [Sun, 14 Dec 2014 17:01:00 +0000 (18:01 +0100)] 
datatype: missing byteorder in string_type

nft add rule filter input iifname { "lo", "eth0" } counter

Now the listing shows:

iifname { "lo", "eth0"}

instead of:

iifname { "", ""}

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agobuild: restore --disable-debug
Pablo Neira Ayuso [Sun, 14 Dec 2014 19:59:16 +0000 (20:59 +0100)] 
build: restore --disable-debug

Fix fallout from the automake conversion. Display after configuration
if it is enabled or not.

Reported-by: Steven Barth <cyrus@openwrt.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: log.t: this works for bridge and arp since 3.17
Pablo Neira Ayuso [Sat, 13 Dec 2014 18:11:57 +0000 (19:11 +0100)] 
tests: regression: log.t: this works for bridge and arp since 3.17

So tests log statement for those two families too and remove the
tests/selectors that are ip and ip6 specific, they don't belong here.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: simplify run_test_file() in case `-e' is used
Pablo Neira Ayuso [Sat, 13 Dec 2014 18:05:38 +0000 (19:05 +0100)] 
tests: regression: simplify run_test_file() in case `-e' is used

Avoid copy&paste coding style pattern by simplifying the code that
handles the `-e' option that allows us to run known broken tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: fix wrong number of test files
Pablo Neira Ayuso [Sat, 13 Dec 2014 17:57:06 +0000 (18:57 +0100)] 
tests: regression: fix wrong number of test files

Always increment the test file counter for each test file in the list.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agoparser: use 'redirect to PORT' instead of 'redirect :PORT'
Pablo Neira Ayuso [Fri, 12 Dec 2014 18:26:46 +0000 (19:26 +0100)] 
parser: use 'redirect to PORT' instead of 'redirect :PORT'

Small syntax update suggested by Patrick.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: redirect.t: fix bogus errors
Pablo Neira Ayuso [Fri, 12 Dec 2014 18:11:05 +0000 (19:11 +0100)] 
tests: regression: redirect.t: fix bogus errors

Separate values in set, otherwise bash interprets the brackets
and the test reports an error.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agotests: regression: adapt nat tests to use random-fully
Pablo Neira Ayuso [Fri, 12 Dec 2014 17:45:03 +0000 (18:45 +0100)] 
tests: regression: adapt nat tests to use random-fully

This adapts test to the change that happened in d9a9a79 ('stmt: rename
nat "random-fully" option to "fully-random"').

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
10 years agodatatype: print datatype name in datatype_print() BUG message
Patrick McHardy [Thu, 11 Dec 2014 10:05:37 +0000 (10:05 +0000)] 
datatype: print datatype name in datatype_print() BUG message

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agodtype: fix memory leak in concat_type_destroy()
Patrick McHardy [Sat, 13 Sep 2014 09:07:49 +0000 (10:07 +0100)] 
dtype: fix memory leak in concat_type_destroy()

Free allocated memory for ->desc.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agometa: properly align types in meta_template table
Patrick McHardy [Thu, 11 Dec 2014 20:33:39 +0000 (20:33 +0000)] 
meta: properly align types in meta_template table

Don't use arbitrary amounts of spaces. The remaining table is
properly aligned, fix the new types.

Signed-off-by: Patrick McHardy <kaber@trash.net>
10 years agostmt: rename nat "random-fully" option to "fully-random"
Patrick McHardy [Thu, 11 Dec 2014 22:06:48 +0000 (22:06 +0000)] 
stmt: rename nat "random-fully" option to "fully-random"

Use proper english for full randomization option.

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

Just setting the .sym_tbl correctly is all we need.

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

 # nft list sets
 Segmentation fault

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

Fix same bug in `nft list tables'.

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

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

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

This fixes the listing of:

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

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