]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
7 years agolibnftables: Fix for input without trailing newline
Phil Sutter [Tue, 10 Apr 2018 17:00:20 +0000 (19:00 +0200)] 
libnftables: Fix for input without trailing newline

Input parser implementation requires a newline at end of input,
otherwise the last pattern may not be recognized correctly.

If input comes from a file, the culprit was YY_INPUT macro not expecting
the last line not ending with a newline, so the last word wasn't
accepted. This is easily fixed by checking for feof(yyin) in there. A
simple test case for that is:

| echo -en "table ip t {\nchain c {\n}\n}" >/tmp/foo
| nft -f /tmp/foo

Input from a string buffer is a bit more tricky: The culprit here is
that detection of classid pattern is done by checking the character
following it which makes it impossible to sit right at end of input and
I haven't found an alternative to that. After dropping the manual
newline appending when combining argv into a single buffer in main(),
a rule like this won't be recognized anymore:

| nft add rule ip t c meta priority feed:babe

Since a direct call to run_cmd_from_buffer() via libnftables bypasses
the sanitizing done in main() entirely, it has to happen in libnftables
instead which means creating a newline-terminated duplicate of the input
buffer.

Note that main() created a buffer one byte longer than needed since it
accounts for whitespace at end of each argv but doesn't add it to the
buffer for the last one, so buffer length is reduced by two bytes
instead of just one although only one less character is printed into it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoct: Fix output_fp bypass in ct_print()
Phil Sutter [Tue, 10 Apr 2018 17:00:19 +0000 (19:00 +0200)] 
ct: Fix output_fp bypass in ct_print()

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure: don't enable xtables when --without-xtables is passed
Florian Westphal [Wed, 4 Apr 2018 11:50:08 +0000 (13:50 +0200)] 
configure: don't enable xtables when --without-xtables is passed

AC_ARG_WITH runs this when EITHER --with-foo or --without-foo is given,
so use 'withval'.

After this patch:
./configure -> xtables off
./configure --with-xtables -> xtables on
./configure --without-xtables -> xtables off (was on).

Reported-by: Alexander Dahl <ada@thorsis.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: use recursive call for SET_REF handling
Florian Westphal [Mon, 2 Apr 2018 19:26:07 +0000 (21:26 +0200)] 
evaluate: use recursive call for SET_REF handling

We can now call the helper again, with set->init as new RHS expression.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: handle EXPR_MAPPING
Florian Westphal [Tue, 3 Apr 2018 11:16:31 +0000 (13:16 +0200)] 
evaluate: handle EXPR_MAPPING

Needed by followup patch.  EXPR_SET_REF handling is bonkers, it
"works" when using { key : value } because ->key and ->left are aliased
in struct expr to the same location.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: split binop xfer to separate function
Florian Westphal [Mon, 2 Apr 2018 18:36:56 +0000 (20:36 +0200)] 
evaluate: split binop xfer to separate function

to reuse this in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: move lhs fixup to a helper
Florian Westphal [Mon, 2 Apr 2018 18:34:23 +0000 (20:34 +0200)] 
evaluate: move lhs fixup to a helper

... to reuse this in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonetlink: floating point exception with concatenations
Pablo Neira Ayuso [Tue, 3 Apr 2018 11:03:39 +0000 (13:03 +0200)] 
netlink: floating point exception with concatenations

If the length is less than 1 byte, this codepath hits a floating point
exception, use div_round_up() as everywhere else in the code.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: propagate binop_transfer() adjustment to set key size
Pablo Neira Ayuso [Tue, 3 Apr 2018 11:00:31 +0000 (13:00 +0200)] 
evaluate: propagate binop_transfer() adjustment to set key size

The right shift transfer may be result in adjusting the set key size,
eg. ip6 dscp results in fetching 6 bits that are splitted between two
bytes, hence the set element ends up being 16 bytes long.

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoevaluate: do not inconditionally update cache from flush command
Pablo Neira Ayuso [Sun, 1 Apr 2018 18:10:08 +0000 (20:10 +0200)] 
evaluate: do not inconditionally update cache from flush command

This is only required by sets, maps and meters, skip cache.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: use mini-gmp 6.1.2 stable release
Pablo Neira Ayuso [Sat, 31 Mar 2018 19:52:35 +0000 (21:52 +0200)] 
src: use mini-gmp 6.1.2 stable release

Instead of a snapshot from the gmp hg repository.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: ip6: fix 'missing payload' error
Florian Westphal [Sat, 31 Mar 2018 22:24:19 +0000 (00:24 +0200)] 
tests: ip6: fix 'missing payload' error

ERROR: line 2: did not find payload information for rule 'iifname "eth0" ct state established,new tcp dport vmap {22 : drop, 222 : drop } masquerade'

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonft-test: check start of rule with sets too
Florian Westphal [Sat, 31 Mar 2018 09:08:33 +0000 (11:08 +0200)] 
nft-test: check start of rule with sets too

We special case rules with sets ({}) so set members are sorted properly.

But we failed to check start of rule:
input:  meta mark { 1, 2 } bar
expect: meta mark { 1, 2 } bar

We made sure 'bar' is equal in both input and expected output,
but we did not check start (before {).

This causes a gazillion of warnings, will fix in followup commit.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: ct, meta: fixes for upcoming nft-test.py fix
Florian Westphal [Sat, 31 Mar 2018 13:34:35 +0000 (15:34 +0200)] 
tests: ct, meta: fixes for upcoming nft-test.py fix

nft-test.py currently fails to properly compare tests involving a set,
after that bug is fixed these lines would fail, so fix this up before.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: mh: fix expected test output
Florian Westphal [Sat, 31 Mar 2018 10:10:53 +0000 (12:10 +0200)] 
tests: mh: fix expected test output

Forgot to include '!=', this doesn't trigger at the moment due to
a bug in nft-test.py, so fix this before fixing our test script.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: redirect: fix tests for upcoming nft-test.py fix
Florian Westphal [Sat, 31 Mar 2018 14:18:55 +0000 (16:18 +0200)] 
tests: redirect: fix tests for upcoming nft-test.py fix

nft-test.py currently fails to properly compare tests involving a set,
after that bug is fixed these lines would fail, so fix this up before.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agonetlink_delinearize: kill dependency before eval of 'redirect' stmt
Florian Westphal [Sat, 31 Mar 2018 14:19:07 +0000 (16:19 +0200)] 
netlink_delinearize: kill dependency before eval of 'redirect' stmt

ip protocol 6 redirect to :tcp dport map { 22 : 8000, 80 : 8080}
   is printed as
redirect to :tcp dport map { 22 : 8000, 80 : 8080}

but that input yields:
Error: transport protocol mapping is only valid after transport protocol match

so kill dependencies beforehand so nft won't remove it.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agostatement: print space before "{" in set add
Florian Westphal [Sat, 31 Mar 2018 13:51:33 +0000 (15:51 +0200)] 
statement: print space before "{" in set add

old:
add @set5{ ip6 saddr . ip6 daddr}
new:
add @set5 { ip6 saddr . ip6 daddr}

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Updates for mini-gmp.{c,h}
Harsha Sharma [Sat, 31 Mar 2018 14:49:41 +0000 (20:19 +0530)] 
src: Updates for mini-gmp.{c,h}

updates from latest stable release of libgmp to get in sync with them

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoRevert "payload: don't remove icmp family dependency in special cases"
Florian Westphal [Wed, 28 Mar 2018 08:38:12 +0000 (10:38 +0200)] 
Revert "payload: don't remove icmp family dependency in special cases"

This reverts commit 126706c23c0458b07d54550dc27561b30f8a43f2.

As its now ok to use icmp-in-ip6 family we can remove the dependency.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoproto: permit icmp-in-ipv6 and icmpv6-in-ipv4
Florian Westphal [Wed, 28 Mar 2018 08:38:06 +0000 (10:38 +0200)] 
proto: permit icmp-in-ipv6 and icmpv6-in-ipv4

Jozsef points out that
 meta l4proto icmp icmp type destination-unreachable

is hard to read.  So, lets just add icmp/icmpv6 to
ip/ip6 protocol base so users can just go with

 icmp type destination-unreachable

and let nft fill in needed dependency.
After this patch, the recent patch to not remove the
dependency can be reverted again.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agopayload: don't remove icmp family dependency in special cases
Florian Westphal [Tue, 27 Mar 2018 08:18:18 +0000 (10:18 +0200)] 
payload: don't remove icmp family dependency in special cases

When using nftables to filter icmp-in-ipv6 or icmpv6-in-ipv4 we
erronously removed the dependency, i.e. "lis ruleset" shows

table ip6 filter { chain output {
type filter hook output priority 0; policy accept;
icmp type destination-unreachable
} }

but that won't restore because of ip vs ipv6 conflict.

After this patch, this lists as

 meta l4proto icmp icmp type destination-unreachable

instead.  We still remove the dependency in "ip" family.

Same applies to icmpv6-in-ip.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: meta: icmp, icmpv6: don't kill required dependencies
Florian Westphal [Tue, 27 Mar 2018 10:11:04 +0000 (12:11 +0200)] 
tests: meta: icmp, icmpv6: don't kill required dependencies

when explicitly filtering icmp-in-ipv6 and icmp6-in-ip don't remove the
required l3 protocol dependency, else "nft list ruleset" can't be read
via nft -f anymore.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: avoid errouneous assert with map+concat
Florian Westphal [Tue, 27 Mar 2018 07:29:54 +0000 (09:29 +0200)] 
src: avoid errouneous assert with map+concat

Phil reported following assert:

add rule ip6 f o mark set ip6 saddr . ip6 daddr . tcp dport \
                        map { dead::beef . f00::. 22 : 1 }
nft: netlink_linearize.c:655: netlink_gen_expr: Assertion `dreg < ctx->reg_low' failed.

This happens because "mark set" will allocate one register (the dreg),
but netlink_gen_concat_expr will populate a lot more register space if
the concat expression strings a lot of expressions together.

As the assert is useful pseudo-reserve the register space as per
concat->len and undo after generating the expressions.

Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: nft.8 more spelling fixes
Duncan Roe [Tue, 27 Mar 2018 04:17:01 +0000 (15:17 +1100)] 
doc: nft.8 more spelling fixes

I ran the following command:

ispell -p ./ispell_nft -H nft.xml

to create the local dictionary ispell_nft.
ispell_nft contains almost every special word in nft.xml.
The idea is that anyone can run ispell the same way and only have to accept:

 - alpha strings in hexadecimal numbers
 - "FIXME" : that has to be fixed eventually
 - "differv" : I don't know what that is or whether it's correct

You need to use the English (i.e. American) dictionary, and you want the screen
to be about 100 chars wide (at least).

The patch enforces consistent capitalisation of words, e.g. IPv4 is always that
way but ipv4_addr stays as before. The existing dictionary suggested capital
Ethernet so that is in there too.

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoremove todo list
Florian Westphal [Mon, 26 Mar 2018 06:51:55 +0000 (08:51 +0200)] 
remove todo list

its outdated.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoExport libnftables (again)
Phil Sutter [Wed, 21 Mar 2018 13:16:40 +0000 (14:16 +0100)] 
Export libnftables (again)

This reverts commits d572d59788143945c2a638f12a5227d9b21ce489 and
9f5e49e147219cd161de8cd80cadf8a444969ef0.

Current libnftables API should be stable enough to release it into the
public, and after 4aba100e593f ("rule: reset cache iff there is an
existing cache") we have a simple way to batch commands through this
API.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Test flush and nat chain recreate in one go
Phil Sutter [Wed, 21 Mar 2018 11:59:11 +0000 (12:59 +0100)] 
tests/shell: Test flush and nat chain recreate in one go

This tests what kernel commit ae6153b50f9bf ("netfilter: nf_tables:
permit second nat hook if colliding hook is going away") fixed for.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests/shell: Allow to specify multiple testcases
Phil Sutter [Mon, 19 Mar 2018 17:02:07 +0000 (18:02 +0100)] 
tests/shell: Allow to specify multiple testcases

Extend run-tests.sh a bit so that all remaining arguments after option
parsing are treated as filenames to test and complain if one doesn't
seem like such. This allows for doing stuff like:

| ./run-tests.sh testcases/include/000*

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix sporadic fail of include/0007glob_double_0
Phil Sutter [Mon, 19 Mar 2018 17:02:06 +0000 (18:02 +0100)] 
tests/shell: Fix sporadic fail of include/0007glob_double_0

Since ruleset listing shows tables sorted by handle (which in turn
depends on table creation ordering), using random filenames here
guarantees to make the test fail randomly.

Since the include files reside in a temporary directory anyway, there is
no need to randomize their names so simplify the whole test a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoflowtable: Make parsing a little more robust
Phil Sutter [Mon, 19 Mar 2018 17:02:05 +0000 (18:02 +0100)] 
flowtable: Make parsing a little more robust

It was surprisingly easy to crash nft with invalid syntax in 'add
flowtable' command. Catch at least three possible ways (illustrated in
provided test case) by making evaluation phase survive so that bison
gets a chance to complain.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix flowtable test cases
Phil Sutter [Mon, 19 Mar 2018 17:02:04 +0000 (18:02 +0100)] 
tests/shell: Fix flowtable test cases

The major problem here was that existence of network interfaces 'eth0'
and 'wlan0' was assumed. Overcome this by just using 'lo' instead, which
exists even in newly created netns by default.

Another minor issue was false naming of 0004delete_after_add0 - the
expected return code is supposed to be separated by '_' from the
remaining filename.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Fix dump of chains/0016delete_handle_0
Phil Sutter [Mon, 19 Mar 2018 17:02:03 +0000 (18:02 +0100)] 
tests/shell: Fix dump of chains/0016delete_handle_0

The purpose of this test is to delete some chains by their handle and
that is supposed to succeed. So the respective dump should not contain
them anymore.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoSupport 'nft -f -' to read from stdin
Phil Sutter [Mon, 19 Mar 2018 17:02:02 +0000 (18:02 +0100)] 
Support 'nft -f -' to read from stdin

In libnftables, detect if given filename is '-' and treat it as the
common way of requesting to read from stdin, then open /dev/stdin
instead. (Calling 'nft -f /dev/stdin' worked before as well, but this
makes it official.)

With this in place and bash's support for here strings, review all tests
in tests/shell for needless use of temp files. Note that two categories
of test cases were intentionally left unchanged:

- Tests creating potentially large rulesets to avoid running into shell
  parameter length limits.
- Tests for 'include' directive for obvious reasons.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: reset cache iff there is an existing cache
Pablo Neira Ayuso [Sun, 18 Mar 2018 20:31:29 +0000 (21:31 +0100)] 
rule: reset cache iff there is an existing cache

If genid is unset, then do not reset existing cache. In the kernel,
generation ID is assumed to be always != zero.

This patch fixes:

nft 'add table x; add chain x y;'

that allow us to send several commands in one single batch from the
command line.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: nft.8 aim for consistent synopses throughout (again)
Duncan Roe [Mon, 19 Mar 2018 03:12:50 +0000 (14:12 +1100)] 
doc: nft.8 aim for consistent synopses throughout (again)

Fix a few more items as per commit f9cb9580b924f6320005f429f7d59e52a38aff82

Also insert a missing space I noticed along the way

Signed-off-by: Duncan Roe <duncan_roe@optusnet.com.au>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agodoc: describe table dormant flag
Florian Westphal [Sat, 17 Mar 2018 11:00:27 +0000 (12:00 +0100)] 
doc: describe table dormant flag

also mention how to quit interactive mode and provide
small table add example.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoCombine redir and masq statements into nat
Phil Sutter [Sat, 17 Mar 2018 09:39:27 +0000 (10:39 +0100)] 
Combine redir and masq statements into nat

All these statements are very similar, handling them with the same code
is obvious. The only thing required here is a custom extension of enum
nft_nat_types which is used in nat_stmt to distinguish between snat and
dnat already. Though since enum nft_nat_types is part of kernel uAPI,
create a local extended version containing the additional fields.

Note that nat statement printing got a bit more complicated to get the
number of spaces right for every possible combination of attributes.

Note also that there wasn't a case for STMT_MASQ in
rule_parse_postprocess(), which seems like a bug. Since STMT_MASQ became
just a variant of STMT_NAT, postprocessing will take place for it now
anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: evaluate: add preliminary binop transfer support for vmaps
Florian Westphal [Thu, 11 Jan 2018 15:30:23 +0000 (16:30 +0100)] 
src: evaluate: add preliminary binop transfer support for vmaps

nftables doesn't support vmap with bit-sized headers, such as flow label or dscp:

nft add rule ip filter input ip dscp vmap \{ 4 : accept, 63 : continue \}
BUG: invalid binary operation 5

Unlike plain "ip dscp { 4, 63 }", we don't have a relational operation in
case of vmap. Binop fixups need to be done when evaluating map statements.

This patch is incomplete. 'ip dscp' works, but this won't:
  nft add rule --debug=netlink ip6 test-ip6 input ip6 dscp vmap { 0x04 : accept, 0x3f : continue }

The generated expressions look sane, however there is disagreement on
the sets key size vs. the sizes of the individual elements in the set.
This is because ip6 dscp spans a byte boundary.

Key set size is still set to one byte (dscp type is 6bits).
However, binop expansion requirements result in 2 byte loads, i.e.
set members will be 2 bytes in size as well.

This can hopefully get addressed in an incremental patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoevaluate: handle binop adjustment recursively
Florian Westphal [Thu, 11 Jan 2018 15:30:22 +0000 (16:30 +0100)] 
evaluate: handle binop adjustment recursively

currently this is fine, but a followup commit will add
EXPR_SET_ELEM handling.

And unlike RANGE we cannot assume the key is a value.
Therefore make binop_can_transfer and binop_transfer_one handle
right hand recursively if needed.  For RANGE, call it again with
from/to.

For future SET_ELEM, we can then just call the function recursively
again with right->key as new RHS.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: segtree: use value expression length
Florian Westphal [Thu, 11 Jan 2018 15:30:20 +0000 (16:30 +0100)] 
src: segtree: use value expression length

In case of EXPR_MAPPING, expr->len is 0, we need to use
the length of the key instead.

Without this we can get assertion failure later on:
nft: netlink_delinearize.c:1484: binop_adjust_one: Assertion `value->len >= binop->right->len' failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: netlink_delinearize: don't assume element contains a value
Florian Westphal [Thu, 11 Jan 2018 15:30:21 +0000 (16:30 +0100)] 
src: netlink_delinearize: don't assume element contains a value

We cannot assume i->key->key is EXPR_VALUE, we could look e.g.  at a range,
which will trigger an assertion failure in binop_adjust_one().

We should call __binop_adjust recursively again in the EXPR_SET_ELEM case,
using key as new input.

Fixes: b8b8e7b6ae10 ("evaluate: transfer right shifts to set reference side")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: add test cases for vmap binop transfer
Florian Westphal [Thu, 11 Jan 2018 15:30:24 +0000 (16:30 +0100)] 
tests: add test cases for vmap binop transfer

they fail with 'BUG: invalid binary operation 5'.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: add srh test cases
Florian Westphal [Fri, 16 Mar 2018 19:15:50 +0000 (20:15 +0100)] 
tests: add srh test cases

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: fix routing header support
Florian Westphal [Fri, 16 Mar 2018 16:39:29 +0000 (17:39 +0100)] 
src: fix routing header support

We can't use nft_exthdr_op to encode routing header, it breaks
ipv6 extension header support.

When encountering RT header, userspace did now set a new ipv6 exthdr mode,
but old kernel doesn't know about this, so this failed with -EOPNOTSUPP.

Revert that part and use NFT_EXTHDR_OP_IPV6.
When decoding a routing extension header, try the various route
types until we find a match.

Note this patch isn't complete:

'srh tag 127' creates following expressions:
  [ exthdr load 2b @ 43 + 6 => reg 1 ]
  [ cmp eq reg 1 0x00007f00 ]

It should instead insert a dependency test ("rt type 4"):
  [ exthdr load 1b @ 43 + 2 => reg 1 ]
  [ cmp eq reg 1 0x00000004 ]
  [ exthdr load 2b @ 43 + 6 => reg 1 ]
  [ cmp eq reg 1 0x00007e00 ]

nft should then use this to infer the routing header type.

While add it, document the srh option.

Fixes: 1400288f6d39d ("src: handle rt0 and rt2 properly")
Reported-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Ahmed Abdelsalam <amsalam20@gmail.com>
7 years agotests: update to new syntax to add/update set from packet path
Pablo Neira Ayuso [Fri, 16 Mar 2018 16:09:12 +0000 (17:09 +0100)] 
tests: update to new syntax to add/update set from packet path

Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: revisit syntax to update sets and maps from packet path
Pablo Neira Ayuso [Fri, 16 Mar 2018 09:14:47 +0000 (10:14 +0100)] 
src: revisit syntax to update sets and maps from packet path

For sets, we allow this:

nft add rule x y ip protocol tcp update @y { ip saddr}

For maps:

table ip nftlb {
        map persistencia {
            type ipv4_addr : mark
            timeout 1h
            elements = { 192.168.1.132 expires 59m55s : 0x00000064,
                         192.168.56.101 expires 59m24s : 0x00000065 }
        }

        chain pre {
            type nat hook prerouting priority 0; policy accept;
            update @persistencia \
                { @nh,96,32 : numgen inc mod 2 offset 100 }
        }
    }

nft --debug=netlink add rule ip nftlb pre add @persistencia \
        { ip saddr : numgen inc mod 2 offset 100 }

More compact and it doesn't gets it confused with a simple map update
command (interesting that bison didn't spew any conflict error).

Former syntax for sets is preserved.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: Fold netlink_gen_cmp() into netlink_gen_relational()
Phil Sutter [Thu, 15 Mar 2018 23:03:20 +0000 (00:03 +0100)] 
netlink: Fold netlink_gen_cmp() into netlink_gen_relational()

Since netlink_gen_relational() didn't do much anymore after meta OP
treating had been removed, it makes sense to merge it with the only
function it dispached to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorelational: Eliminate meta OPs
Phil Sutter [Thu, 15 Mar 2018 23:03:19 +0000 (00:03 +0100)] 
relational: Eliminate meta OPs

With a bit of code reorganization, relational meta OPs OP_RANGE,
OP_FLAGCMP and OP_LOOKUP become unused and can be removed. The only meta
OP left is OP_IMPLICIT which is usually treated as alias to OP_EQ.
Though it needs to stay in place for one reason: When matching against a
bitmask (e.g. TCP flags or conntrack states), it has a different
meaning:

| nft --debug=netlink add rule ip t c tcp flags syn
| ip t c
|   [ meta load l4proto => reg 1 ]
|   [ cmp eq reg 1 0x00000006 ]
|   [ payload load 1b @ transport header + 13 => reg 1 ]
|   [ bitwise reg 1 = (reg=1 & 0x00000002 ) ^ 0x00000000 ]
|   [ cmp neq reg 1 0x00000000 ]

| nft --debug=netlink add rule ip t c tcp flags == syn
| ip t c
|   [ meta load l4proto => reg 1 ]
|   [ cmp eq reg 1 0x00000006 ]
|   [ payload load 1b @ transport header + 13 => reg 1 ]
|   [ cmp eq reg 1 0x00000002 ]

OP_IMPLICIT creates a match which just checks the given flag is present,
while OP_EQ creates a match which ensures the given flag and no other is
present.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: Use custom nft binary for ruleset listing
Phil Sutter [Thu, 15 Mar 2018 22:58:38 +0000 (23:58 +0100)] 
tests/shell: Use custom nft binary for ruleset listing

Don't assume the system's nft binary is able to correctly list rulesets
generated in tests.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoRevert "src: update dynamic set updates from packet path syntax"
Pablo Neira Ayuso [Fri, 16 Mar 2018 08:55:57 +0000 (09:55 +0100)] 
Revert "src: update dynamic set updates from packet path syntax"

This reverts commit 9047cc7ae746b1c9abd4e11ed476e37d8716d400, this is breaking
tests.

7 years agosrc: update dynamic set updates from packet path syntax
Pablo Neira Ayuso [Thu, 15 Mar 2018 18:38:59 +0000 (19:38 +0100)] 
src: update dynamic set updates from packet path syntax

New prefered syntax is:

{add,update} set { key } @name

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

        chain y {
                ip protocol tcp add set { ip saddr} @y
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: support of dynamic map addition and update of elements
Laura Garcia Liebana [Thu, 15 Mar 2018 08:23:21 +0000 (09:23 +0100)] 
src: support of dynamic map addition and update of elements

The support of dynamic adds and updates are only available for sets
and meters. This patch gives such abilities to maps as well.

This patch is useful in cases where dynamic population of maps are
required, for example, to maintain a persistence during some period
of time.

Example:

table ip nftlb {
    map persistencia {
        type ipv4_addr : mark
        timeout 1h
        elements = { 192.168.1.132 expires 59m55s : 0x00000064,
                     192.168.56.101 expires 59m24s : 0x00000065 }
    }

    chain pre {
        type nat hook prerouting priority 0; policy accept;
        map update \
            { @nh,96,32 : numgen inc mod 2 offset 100 } @persistencia
    }
}

An example of the netlink generated sequence:

 nft --debug=netlink add rule ip nftlb pre map add \
    { ip saddr : numgen inc mod 2 offset 100 } @persistencia
ip nftlb pre
  [ payload load 4b @ network header + 12 => reg 1 ]
  [ numgen reg 2 = inc mod 2 offset 100 ]
  [ dynset add reg_key 1 set persistencia sreg_data 2 ]

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: use nftnl_flowtable_get/set
Florian Westphal [Mon, 12 Mar 2018 11:58:59 +0000 (12:58 +0100)] 
netlink: use nftnl_flowtable_get/set

the '_array' variant is just a wrapper for get/set api; this
allows the array variant to be removed from libnftnl.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Adding support for segment routing header 'srh'
Ahmed Abdelsalam [Tue, 27 Feb 2018 10:04:14 +0000 (11:04 +0100)] 
src: Adding support for segment routing header 'srh'

Segment Routing Header "SRH" is new type of IPv6 Routing extension
header (type 4).

SRH contains a list of segments (each is represented as an IPv6 address)
to be visited by packets during the journey from source to destination.

The SRH specification are defined in the below IETF SRH draft.
https://tools.ietf.org/html/draft-ietf-6man-segment-routing-header-07

Signed-off-by: Ahmed Abdelsalam <amsalam20@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: handle rt0 and rt2 properly
Ahmed Abdelsalam [Tue, 27 Feb 2018 06:25:14 +0000 (07:25 +0100)] 
src: handle rt0 and rt2 properly

Type 0 and 2 of the IPv6 Routing extension header are not handled
properly by exthdr_init_raw() in src/exthdr.c

In order to fix the bug, we extended the "enum nft_exthdr_op" to
differentiate between rt, rt0, and rt2.

This patch should fix the bug. We tested the patch against the
same configuration reported in the bug and the output is as
shown below.

table ip6 filter {
chain input {
type filter hook input priority 0; policy accept;
rt0 addr[1] a::2
}
}

Fixes: Bugzilla #1219
Signed-off-by: Ahmed Abdelsalam <amsalam20@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: add set information and example for run-time blackhole
Florian Westphal [Sun, 11 Mar 2018 16:47:05 +0000 (17:47 +0100)] 
doc: add set information and example for run-time blackhole

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agotests: shell: fix tests for deletion via handle attribute
Harsha Sharma [Fri, 9 Mar 2018 14:22:28 +0000 (19:52 +0530)] 
tests: shell: fix tests for deletion via handle attribute

Fetch object, chain and set handles and with '-a' option and then delete
them.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: print handle attribute in more clearer manner
Harsha Sharma [Thu, 8 Mar 2018 16:46:13 +0000 (22:16 +0530)] 
rule: print handle attribute in more clearer manner

Print handles in this way:

table ip filter { # handle 2
}
Similarly, for chain, set and object handles

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: autogenerate dump verification
Laura Garcia Liebana [Wed, 7 Mar 2018 21:51:10 +0000 (22:51 +0100)] 
tests: shell: autogenerate dump verification

Complete the automated shell tests with the verification of
the test file dump, only for positive tests and if the test
execution was successful.

It's able to generate the dump file with the -g option.
Example:

 # ./run-tests.sh -g testcases/chains/0001jumps_0

The dump files are generated in the same path in the folder named
dumps/ with .nft extension.

It has been avoided the dump verification code in every test
file.

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: don't not dump set content from netlink_get_setelems()
Pablo Neira Ayuso [Wed, 7 Mar 2018 14:51:35 +0000 (15:51 +0100)] 
src: don't not dump set content from netlink_get_setelems()

This causes python tests to report payload mismatching errors.

Fixes: a43cc8d53096 ("src: support for get element command")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: remove unused batch support checks
Pablo Neira Ayuso [Wed, 7 Mar 2018 14:23:33 +0000 (15:23 +0100)] 
src: remove unused batch support checks

Follow up after cc8c5fd02448 ("netlink: remove non-batching routine").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: move monitor code to src/monitor.c
Pablo Neira Ayuso [Wed, 7 Mar 2018 12:08:30 +0000 (13:08 +0100)] 
src: move monitor code to src/monitor.c

netlink.c is rather large file, move the monitor code to its own file.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Phil Sutter <phil@nwl.cc>
Acked-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
7 years agosrc: support for get element command
Pablo Neira Ayuso [Tue, 6 Mar 2018 17:58:29 +0000 (18:58 +0100)] 
src: support for get element command

You need a Linux kernel >= 4.15 to use this feature.

This patch allows us to dump the content of an existing set.

 # nft list ruleset
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2, 3.3.3.3,
                             5.5.5.5-6.6.6.6 }
        }
 }

You check if a single element exists in the set:

 # nft get element x x { 1.1.1.5 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2 }
        }
 }

Output means '1.1.1.5' belongs to the '1.1.1.1-2.2.2.2' interval.

You can also check for intervals:

 # nft get element x x { 1.1.1.1-2.2.2.2 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2 }
        }
 }

If you try to check for an element that doesn't exist, an error is
displayed.

 # nft get element x x { 1.1.1.0 }
 Error: Could not receive set elements: No such file or directory
 get element x x { 1.1.1.0 }
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can also check for multiple elements in one go:

 # nft get element x x { 1.1.1.5, 5.5.5.10 }
 table ip x {
        set x {
                type ipv4_addr
                flags interval
                elements = { 1.1.1.1-2.2.2.2, 5.5.5.5-6.6.6.6 }
        }
 }

You can also use this to fetch the existing timeout for specific
elements, in case you have a set with timeouts in place:

 # nft get element w z { 2.2.2.2 }
 table ip w {
        set z {
                type ipv4_addr
                timeout 30s
                elements = { 2.2.2.2 expires 17s }
        }
 }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoconfigure: bump libnftnl dependency
Pablo Neira Ayuso [Tue, 6 Mar 2018 11:02:53 +0000 (12:02 +0100)] 
configure: bump libnftnl dependency

Check for 1.1.0, which includes flowtable symbols.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: missing redirection to file
Pablo Neira Ayuso [Mon, 5 Mar 2018 17:52:51 +0000 (18:52 +0100)] 
tests: shell: missing redirection to file

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: broken handle listing of table and named objects
Pablo Neira Ayuso [Mon, 5 Mar 2018 17:22:00 +0000 (18:22 +0100)] 
rule: broken handle listing of table and named objects

Now listing looks good:

 table ip test-ip {
        counter https-traffic {
                packets 0 bytes 0
        } # handle 1
 } # handle 847

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of objects via object handle
Harsha Sharma [Thu, 18 Jan 2018 20:45:14 +0000 (02:15 +0530)] 
tests/shell: add tests for deletion of objects via object handle

Delete objects with given object handle

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc/nft.xml: Add deletion for objects via handles
Harsha Sharma [Tue, 23 Jan 2018 22:03:10 +0000 (03:33 +0530)] 
doc/nft.xml: Add deletion for objects via handles

Add documentation for deletion of tables, chains, sets and objects
via unique handles.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: extend nft to list object handle and delete objects via handle
Harsha Sharma [Thu, 18 Jan 2018 18:52:56 +0000 (00:22 +0530)] 
src: extend nft to list object handle and delete objects via handle

Print handle attributes in objects when listing via '-a' option and
delete objects via their unique object handles.
For e.g.

nft delete [<object-type>] [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agorule: print object handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:25:45 +0000 (17:25 +0100)] 
rule: print object handle with --echo --handle

 # nft --echo --handle add counter x y
 add counter ip x y { packets 0 bytes 0 }  # handle 0

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: print table handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:06:51 +0000 (17:06 +0100)] 
netlink: print table handle with --echo --handle

 # nft --echo --handle add table x
 add table ip x # handle 80

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: print chain handle with --echo --handle
Pablo Neira Ayuso [Mon, 5 Mar 2018 16:01:19 +0000 (17:01 +0100)] 
netlink: print chain handle with --echo --handle

 # nft add --echo --handle chain x y
 add chain ip x y # handle 1

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of sets via set handle
Harsha Sharma [Mon, 15 Jan 2018 11:51:33 +0000 (17:21 +0530)] 
tests/shell: add tests for deletion of sets via set handle

Delete set with given unique set handle.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests/shell: add tests for deletion of chains via chain handle
Harsha Sharma [Sun, 14 Jan 2018 19:33:07 +0000 (01:03 +0530)] 
tests/shell: add tests for deletion of chains via chain handle

Delete chain with given unique handle for a table.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: list set handle and delete set via set handle
Harsha Sharma [Sun, 14 Jan 2018 16:18:46 +0000 (21:48 +0530)] 
src: list set handle and delete set via set handle

Print 'handle' attribute in sets when listing via '-a' option and
delete sets via their unique set handles listed with '-a' option.
For e.g.

nft delete set [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: delete chain via chain handle
Harsha Sharma [Tue, 9 Jan 2018 18:59:42 +0000 (00:29 +0530)] 
parser_bison: delete chain via chain handle

This patch allows deletion of chains via unique chain handles which
can be listed with '-a' option and table name and family.
For eg.

nft delete chain [<family>] <table-name> [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: Print handle attribute in chains
Harsha Sharma [Tue, 9 Jan 2018 18:59:01 +0000 (00:29 +0530)] 
src: Print handle attribute in chains

Print handle attribute in chains when listing via '-a' option.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agoparser_bison: delete table via table handle
Harsha Sharma [Mon, 8 Jan 2018 17:57:07 +0000 (23:27 +0530)] 
parser_bison: delete table via table handle

This patch allows deletion of table via unique table handles and table
family which can be listed with '-a' option.
For.eg.
nft delete table [<family>] [handle <handle>]

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: print 'handle' attribute in tables
Harsha Sharma [Sat, 23 Dec 2017 19:45:25 +0000 (11:45 -0800)] 
src: print 'handle' attribute in tables

Print 'handle' attribute in tables, when listing via '-a' option

For eg.
nft list ruleset -a

table ip test-ip4 {
chain input {
ip saddr 8.8.8.8 counter packets 0 bytes 0 # handle 3
}
 # handle 1}
table ip filter {
chain output {
tcp dport ssh counter packets 0 bytes 0 # handle 4
}
 # handle 2}
table ip xyz {
 # handle 3}

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agodoc: nft: document flowtable
Pablo Neira Ayuso [Tue, 23 Jan 2018 11:58:30 +0000 (12:58 +0100)] 
doc: nft: document flowtable

Document the new flowtable objects available since Linux kernel 4.16-rc.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: add flowtable tests
Pablo Neira Ayuso [Mon, 22 Jan 2018 18:54:36 +0000 (19:54 +0100)] 
tests: shell: add flowtable tests

Add basic flowtable tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: flow offload support
Pablo Neira Ayuso [Sun, 3 Dec 2017 20:27:03 +0000 (21:27 +0100)] 
src: flow offload support

This patch allows us to refer to existing flowtables:

 # nft add rule x x flow offload @m

Packets matching this rule create an entry in the flow table 'm', hence,
follow up packets that get to the flowtable at ingress bypass the
classic forwarding path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: delete flowtable
Pablo Neira Ayuso [Fri, 19 Jan 2018 00:41:38 +0000 (01:41 +0100)] 
src: delete flowtable

This patch allows you to delete an existing flowtable:

 # nft delete flowtable x m

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add support to add flowtables
Pablo Neira Ayuso [Thu, 18 Jan 2018 07:43:23 +0000 (08:43 +0100)] 
src: add support to add flowtables

This patch allows you to create flowtable:

 # nft add table x
 # nft add flowtable x m { hook ingress priority 10\; devices = { eth0, wlan0 }\; }

You have to specify hook and priority. So far, only the ingress hook is
supported. The priority represents where this flowtable is placed in the
ingress hook, which is registered to the devices that the user
specifies.

You can also use the 'create' command instead to bail out in case that
there is an existing flowtable with this name.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: support for flowtable listing
Pablo Neira Ayuso [Mon, 4 Dec 2017 12:28:25 +0000 (13:28 +0100)] 
src: support for flowtable listing

This patch allows you to dump existing flowtable.

 # nft list ruleset
 table ip x {
        flowtable x {
                hook ingress priority 10
                devices = { eth0, tap0 }
        }
 }

You can also list existing flowtables via:

 # nft list flowtables
 table ip x {
        flowtable x {
                hook ingress priority 10
                devices = { eth0, tap0 }
        }
 }

 You need a Linux kernel >= 4.16-rc to test this new feature.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: set timeout and size combination coverage
Pablo Neira Ayuso [Mon, 5 Mar 2018 15:19:02 +0000 (16:19 +0100)] 
tests: shell: set timeout and size combination coverage

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agotests: shell: redefine and undefine
Pablo Neira Ayuso [Sat, 3 Mar 2018 22:45:11 +0000 (23:45 +0100)] 
tests: shell: redefine and undefine

This tests cover the new redefine and undefine scripting feature.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agosrc: add variable expression and use it to allow redefinitions
Pablo Neira Ayuso [Sat, 3 Mar 2018 21:52:35 +0000 (22:52 +0100)] 
src: add variable expression and use it to allow redefinitions

Add new variable expression that we can use to attach symbols in
runtime, this allows us to redefine variables via new keyword, eg.

 table ip x {
        chain y {
                define address = { 1.1.1.1, 2.2.2.2 }
                ip saddr $address
                redefine address = { 3.3.3.3 }
                ip saddr $address
        }
 }

 # nft list ruleset
 table ip x {
        chain y {
                ip saddr { 1.1.1.1, 2.2.2.2 }
                ip saddr { 3.3.3.3 }
        }
 }

Note that redefinition just places a new symbol version before the
existing one, so symbol lookups always find the latest version. The
undefine keyword decrements the reference counter and removes the symbol
from the list, so it cannot be used anymore. Still, previous references
to this symbol via variable expression are still valid.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agobuild: Bump version to v0.8.3 v0.8.3
Florian Westphal [Sat, 3 Mar 2018 10:50:23 +0000 (11:50 +0100)] 
build: Bump version to v0.8.3

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: datatype: prefer sscanf, avoid strncpy
Florian Westphal [Sat, 3 Mar 2018 09:57:54 +0000 (10:57 +0100)] 
src: datatype: prefer sscanf, avoid strncpy

similar to previous patch, but replace strncpy+atoi by sscanf.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agosrc: Use snprintf() over strncpy()
Harsha Sharma [Sun, 8 Oct 2017 19:23:43 +0000 (00:53 +0530)] 
src: Use snprintf() over strncpy()

Use snprintf() over strncpy() functions as the buffer is not null
terminated in strncpy().

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
7 years agonetlink: remove non-batching routines
Pablo Neira Ayuso [Sat, 3 Mar 2018 09:18:33 +0000 (10:18 +0100)] 
netlink: remove non-batching routines

This is only needed by 3.16, which was released 8 months after nftables
was merged upstream. That kernel version supports a reduced featureset.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoconfigure: misc updates
Pablo Neira Ayuso [Sat, 3 Mar 2018 09:02:31 +0000 (10:02 +0100)] 
configure: misc updates

This patch removes the following macros:

* AC_PREREQ checks for 2.61, which is not supported any contemporary
  distribution.

* AC_COPYRIGHT, autoconf documentation states "in addition to the Free
  Software Foundation's copyright on the Autoconf macros, parts of your
  configure are covered by the copyright-notice.".

  This only refers to the autoconf infrastructure: we are doing simple
  and standard usage of autoconf infrastructure, we also don't use this
  macro in other existing userspace software available at netfilter.org.
  The comment above at the beginning of this file shows text that is
  available in many configure.ac templates on the Internet.

* AC_CANONICAL_HOST, we don't need the canonical host-system type to
  build this software.

* AC_CONFIG_SRCDIR is not used in other userspace software in the tree.

* AC_DEFINE _GNU_SOURCE, define this where it's needed instead.

* AC_DEFINE _STDC_FORMAT_MACROS is not used in this codebase.

* AC_HEADER_STDC checks for ANSI C89 headers, however, we need more than
  just this C standard, so this doesn't guarantee anything at all.

* Remove "Checks for libraries" comment, it's obvious.

* AC_HEADER_ASSERT allows us to disable assertions, this is bad because
  this is helping us to diagnose bugs and incomplete features.

* AC_CHECK_HEADERS is checking for an arbitrary list of headers,
  this still doesn't even guarantee that we can actually do a successful
  compilation in a broken system.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser: use nf_key_proto
Florian Westphal [Sat, 18 Nov 2017 20:00:58 +0000 (21:00 +0100)] 
parser: use nf_key_proto

Consolidate into one so it can be reused by new users.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agolibnftables: don't crash when no commands are specified
Harsha Sharma [Sat, 3 Mar 2018 08:03:58 +0000 (13:33 +0530)] 
libnftables: don't crash when no commands are specified

For e.g.  nft -c "  "
Without this patch it segfaults.

Signed-off-by: Harsha Sharma <harshasharmaiitr@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 years agoparser: support of maps with timeout
Laura Garcia Liebana [Fri, 2 Mar 2018 09:50:18 +0000 (10:50 +0100)] 
parser: support of maps with timeout

Support of key and value association with a certain timeout.

Example:

nft add map nftlb mapa { type inet_service: ipv4_addr\;
 timeout 5s\; }

Results in:

table ip nftlb {
map mapa {
type inet_service : ipv4_addr
timeout 5s
}
}

Signed-off-by: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink_delinearize: Fix resource leaks
Phil Sutter [Thu, 1 Mar 2018 14:00:32 +0000 (15:00 +0100)] 
netlink_delinearize: Fix resource leaks

Most of the cases are basically the same: Error path fails to free the
previously allocated statement or expression. A few cases received
special treatment though:

- In netlink_parse_payload_stmt(), the leak is easily avoided by code
  reordering.

- In netlink_parse_exthdr(), there's no point in introducing a goto
  label since there is but a single affected error check.

- In netlink_parse_hash() non-error path leaked as well if sreg
  contained a concatenated expression.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agonetlink: Complain if setting O_NONBLOCK fails
Phil Sutter [Thu, 1 Mar 2018 14:00:31 +0000 (15:00 +0100)] 
netlink: Complain if setting O_NONBLOCK fails

Assuming that code is not aware that reads from netlink socket may
block, treat inability to set O_NONBLOCK flag as fatal initialization
error aborting program execution.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 years agohash: Fix potential null-pointer dereference in hash_expr_cmp()
Phil Sutter [Thu, 1 Mar 2018 14:00:30 +0000 (15:00 +0100)] 
hash: Fix potential null-pointer dereference in hash_expr_cmp()

The first part of the conditional:

| (e1->hash.expr || expr_cmp(e1->hash.expr, e2->hash.expr))

will call expr_cmp() in case e1->hash.expr is NULL, causing null-pointer
dereference. This is probably a typo, the intention when introducing
this was to avoid the call to expr_cmp() for symmetric hash expressions
which don't use expr->hash.expr. Inverting the existence check should
fix this.

Fixes: 3a86406729782 ("src: hash: support of symmetric hash")
Cc: Laura Garcia Liebana <nevola@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>