]> git.ipfire.org Git - thirdparty/iptables.git/log
thirdparty/iptables.git
6 years agonft: ensure cache consistency
Pablo Neira Ayuso [Mon, 20 May 2019 09:16:21 +0000 (11:16 +0200)] 
nft: ensure cache consistency

Check for generation ID before and after fetching the cache to ensure
consistency.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: cache table list
Pablo Neira Ayuso [Mon, 20 May 2019 08:51:26 +0000 (10:51 +0200)] 
nft: cache table list

nft_table_find() uses the table list cache to look up for existing
tables.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: add flush_cache()
Pablo Neira Ayuso [Sun, 19 May 2019 11:25:23 +0000 (13:25 +0200)] 
nft: add flush_cache()

This new function takes a struct nft_cache as parameter.

This patch also introduces __nft_table_builtin_find() which is required
to look up for built-in tables without the nft_handle structure.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: add __nft_table_builtin_find()
Pablo Neira Ayuso [Sun, 19 May 2019 16:35:02 +0000 (18:35 +0200)] 
nft: add __nft_table_builtin_find()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: statify nft_rebuild_cache()
Pablo Neira Ayuso [Sun, 19 May 2019 11:04:13 +0000 (13:04 +0200)] 
nft: statify nft_rebuild_cache()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: add struct nft_cache
Pablo Neira Ayuso [Sun, 19 May 2019 10:54:19 +0000 (12:54 +0200)] 
nft: add struct nft_cache

Add new structure that encloses the cache and update the code to use it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoman: refer to iptables-translate and ip6tables
Pablo Neira Ayuso [Tue, 14 May 2019 12:46:41 +0000 (14:46 +0200)] 
man: refer to iptables-translate and ip6tables

Instead of xtables-translate. Remove old reference to xtables-compat.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: Fix ipt-restore/0004-restore-race_0 testcase
Phil Sutter [Tue, 14 May 2019 11:46:00 +0000 (13:46 +0200)] 
tests: Fix ipt-restore/0004-restore-race_0 testcase

Two issues fixed:

* XTABLES_LIBDIR was set wrong (CWD is not topdir but tests/). Drop the
  export altogether, the testscript does this already.

* $LINES is a variable set by bash, so initial dump sanity check failed
  all the time complaining about a spurious initial dump line count. Use
  $LINES1 instead.

Fixes: 4000b4cf2ea38 ("tests: add test script for race-free restore")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Don't leak iter in error path of __nft_chain_zero_counters()
Phil Sutter [Mon, 13 May 2019 17:12:24 +0000 (19:12 +0200)] 
xtables: Don't leak iter in error path of __nft_chain_zero_counters()

If batch_rule_add() fails, this function leaked the rule iterator
object.

Fixes: 4c54c892443c2 ("xtables: Catch errors when zeroing rule rounters")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: SYNPROXY: should not be needed anymore on current kernels
Florian Westphal [Fri, 3 May 2019 10:35:38 +0000 (12:35 +0200)] 
extensions: SYNPROXY: should not be needed anymore on current kernels

SYN packets do not require taking the listener socket lock anymore
as of 4.4 kernel, i.e. this target should not be needed anymore.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxshared: check for maximum buffer length in add_param_to_argv()
Pablo Neira Ayuso [Mon, 22 Apr 2019 21:17:27 +0000 (23:17 +0200)] 
xshared: check for maximum buffer length in add_param_to_argv()

Bail out if we go over the boundary, based on patch from Sebastian.

Reported-by: Sebastian Neef <contact@0day.work>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: add test script for race-free restore
Florian Westphal [Tue, 23 Apr 2019 13:16:25 +0000 (15:16 +0200)] 
tests: add test script for race-free restore

xtables-nft-restore ignores -w, check that we don't add
duplicate rules when parallel restores happen.

With a slightly older iptables-nft version this ususally fails with:
I: [EXECUTING] iptables/tests/shell/testcases/ipt-restore/0004-restore-race_0
iptables-restore v1.8.2 (nf_tables):
line 5: CHAIN_USER_ADD failed (File exists): chain UC-0
line 6: CHAIN_USER_ADD failed (File exists): chain UC-1
W: [FAILED] ipt-restore/0004-restore-race_0: expected 0 but got 4

or
I: [EXECUTING]   iptables/tests/shell/testcases/ipt-restore/0004-restore-race_0
iptables-restore v1.8.2 (nf_tables):
line 1: TABLE_FLUSH failed (No such file or directory): table filter

or
/tmp/tmp.SItN4URxxF /tmp/tmp.P1y4LIxhTl differ: byte 7159, line 137

As the legacy version should not have such race (due to nature
of full-table-replace), only do one iteration for legacy case.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: handle concurrent ruleset modifications
Florian Westphal [Tue, 23 Apr 2019 13:16:24 +0000 (15:16 +0200)] 
xtables: handle concurrent ruleset modifications

We currently race when several xtables-nft-restore processes attempt to
handle rules in parallel.  For instance, when no rules are present at
all, then

iptables-nft-restore < X & iptables-nft-restore < X

... can cause rules to be restored twice.

Reason is that both processes might detect 'no rules exist', so
neither issues a flush operation.

We can't unconditionally issue the flush, because it would
cause kernel to fail with -ENOENT unless the to-be-flushed table
exists.

This change passes the generation id that was used to build
the transaction to the kernel.

In case another process changed *any* rule somewhere, the transaction
will now fail with -ERESTART.

We then flush the cache, re-fetch the ruleset and refresh
our transaction.

For example, in the above 'parallel restore' case, the iptables-restore
instance that lost the race would detect that the table has been created
already, and would add the needed flush.

In a similar vein, in case --noflush is used, we will add the flush
op for user-defined chains that were created in the mean-time.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: add and set "implict" flag on transaction objects
Florian Westphal [Tue, 23 Apr 2019 13:16:23 +0000 (15:16 +0200)] 
xtables: add and set "implict" flag on transaction objects

Its used to flag the rule flushes that get added in user-defined-chains
that get redefined with --noflush.

IOW, those objects that are added not by explicit instruction but
to keep semantics.

With --noflush, iptables-legacy-restore will behave as if
-F USERCHAIN was given, in case USERCHAIN exists and USERCHAIN gets
redefined, i.e.:

 iptables-save v1.8.2 on Thu Apr 18 17:11:05 2019
*filter
:USERCHAIN - [0:0]
COMMIT

... will remove all existing rules from USERCHAIN.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: add and use nft_build_cache
Florian Westphal [Tue, 23 Apr 2019 13:16:22 +0000 (15:16 +0200)] 
xtables: add and use nft_build_cache

Will be used with the "generation id" infrastructure.
When we're told that the commit failed because someone else made
changes, we can use this to re-initialize the cache and then
revalidate the transaction list (e.g. to detect that we now have
to flush the user-defined chain 'foo' that we wanted to create, but
was added just now by someone else).

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: add skip flag to objects
Florian Westphal [Tue, 23 Apr 2019 13:16:21 +0000 (15:16 +0200)] 
xtables: add skip flag to objects

This will be used to skip transaction objects when committing to
kernel.  This is needed for example when we restore a table that
doesn't exist yet.  In such a case we would already build a flush
operation so we can just enable it when we hit problem with the
generation id and we find that the table/chain was already created
in the mean time.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: unify user chain add/flush for restore case
Florian Westphal [Tue, 23 Apr 2019 13:16:20 +0000 (15:16 +0200)] 
xtables: unify user chain add/flush for restore case

The idea here is to move the 'flush' decision into the core, rather than
have the decision in the frontend.

This will be required later when "generation id" is passed to kernel.
In this case, we might have to add the flush when re-trying the
transaction.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: return-codes script is bash specific
Florian Westphal [Fri, 19 Apr 2019 20:20:10 +0000 (22:20 +0200)] 
tests: return-codes script is bash specific

The script fails on systems where sh is not bash.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: libxt_osf.: Typo in manpage
Sam Banks [Thu, 21 Mar 2019 23:22:47 +0000 (12:22 +1300)] 
extensions: libxt_osf.: Typo in manpage

Signed-off-by: Sam Banks <sam.banks.nz@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables-legacy: add missing config.h include
Lucas Stach [Fri, 8 Mar 2019 14:37:09 +0000 (15:37 +0100)] 
xtables-legacy: add missing config.h include

This fixes a IPv4 only build, where this file would have references to
functions that aren't built in this case. I'm not sure how it ends up
with ENABLE_IPV6 defined without the config.h include, but since this
was clearly missing and fixed my issue, I didn't bother tracking down
the chain.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoman: iptables-save: Add note about module autoloading
Phil Sutter [Tue, 26 Mar 2019 18:03:43 +0000 (19:03 +0100)] 
man: iptables-save: Add note about module autoloading

Using '-t' parameter in iptables-save might lead to kernel module
loading, just like with iptables itself. Copy the hint from iptables.8
to inform users.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: Install symlinks as such
Phil Sutter [Fri, 22 Mar 2019 18:31:06 +0000 (19:31 +0100)] 
extensions: Install symlinks as such

Fake shared objects which are actually symlinks to others are installed
using 'install' tool which follows them and therefore installs a copy of
the file they point at. Fix this by introducing special handling for
them in install target.

Reported-by: Wenle Chen <solachenclever@hotmail.com>
Fixes: 269655d54e22f ("build: remove symlink-only extensions from static object list")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables-save: Point at existing man page in help text
Phil Sutter [Wed, 13 Mar 2019 19:46:17 +0000 (20:46 +0100)] 
xtables-save: Point at existing man page in help text

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables-legacy.8: Remove stray colon
Phil Sutter [Wed, 13 Mar 2019 19:46:16 +0000 (20:46 +0100)] 
xtables-legacy.8: Remove stray colon

This obviously doesn't belong there.

Fixes: be70918eab26e ("xtables: rename xt-multi binaries to -nft, -legacy")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Adjust ebtables man page
Phil Sutter [Wed, 13 Mar 2019 19:46:15 +0000 (20:46 +0100)] 
doc: Adjust ebtables man page

Change content to match nft-variant, most notably:

* There is no broute table, drop all references to it
* Comment out description of among and string matches, we don't support
  them (yet)

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Add ebtables man page
Phil Sutter [Wed, 13 Mar 2019 19:46:14 +0000 (20:46 +0100)] 
doc: Add ebtables man page

This is a 1:1 copy from legacy ebtables repository.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Adjust arptables man pages
Phil Sutter [Wed, 13 Mar 2019 19:46:13 +0000 (20:46 +0100)] 
doc: Adjust arptables man pages

Change content to suit the shipped nft-based variant. Most relevant
changes:

* FORWARD chain is not supported
* arptables-nft-save supports a few parameters

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Add arptables-nft man pages
Phil Sutter [Wed, 13 Mar 2019 19:46:12 +0000 (20:46 +0100)] 
doc: Add arptables-nft man pages

These are 1:1 copies from legacy arptables repository.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: connlabel: Fallback on missing connlabel.conf
Phil Sutter [Mon, 4 Mar 2019 15:53:46 +0000 (16:53 +0100)] 
extensions: connlabel: Fallback on missing connlabel.conf

If connlabel.conf was not found, fall back to manually parsing arguments
as plain numbers.

If nfct_labelmap_new() has failed, nfct_labelmap_get_name() segfaults.
Therefore make sure it is not called in connlabel_get_name() if that's
the case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: Add testcase for libxt_ipvs
Phil Sutter [Thu, 21 Feb 2019 19:09:32 +0000 (20:09 +0100)] 
extensions: Add testcase for libxt_ipvs

Given that it is fixed now, make it stay.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: Fix ipvs vproto option printing
Phil Sutter [Thu, 21 Feb 2019 19:09:31 +0000 (20:09 +0100)] 
extensions: Fix ipvs vproto option printing

This was broken since day 1: vproto option was printed as 'proto' which
in turn iptables wouldn't accept anymore.

Fixes: c36d05e424069 ("libxt_ipvs: user-space lib for netfilter matcher xt_ipvs")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: Fix ipvs vproto parsing
Phil Sutter [Thu, 21 Feb 2019 19:09:30 +0000 (20:09 +0100)] 
extensions: Fix ipvs vproto parsing

This was broken by integration into guided option parser:

* Make 'vproto' option XTTYPE_PROTOCOL, otherwise its arguments are
  parsed as garbage only.

* Drop O_VPROTO case from ipvs_mt_parse(), due to XTOPT_POINTER() and
  above change there is nothing to do for it in there.

Fixes: 372203af4c70f ("libxt_ipvs: use guided option parser")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: AUDIT: Document ineffective --type option
Phil Sutter [Thu, 21 Feb 2019 14:38:47 +0000 (15:38 +0100)] 
extensions: AUDIT: Document ineffective --type option

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Install ip{6,}tables-translate.8 manpages
Phil Sutter [Wed, 20 Feb 2019 13:02:55 +0000 (14:02 +0100)] 
doc: Install ip{6,}tables-translate.8 manpages

These are just semantic links to xtables-translate.8.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agotests/shell: Support testing host binaries
Phil Sutter [Tue, 19 Feb 2019 19:39:50 +0000 (20:39 +0100)] 
tests/shell: Support testing host binaries

Add -H/--host parameter to run the testsuite against host system's
binaries.

While being at it, rewrite parameter parsing:

* Parse all parameters in a loop, this frees any ordering constraints.
* Set extglob option so strict pattern matching for single testcase mode
  can be done via bash globbing.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxlate-test: Support testing host binaries
Phil Sutter [Tue, 19 Feb 2019 19:39:49 +0000 (20:39 +0100)] 
xlate-test: Support testing host binaries

Introduce --host parameter to run the testsuite against host's binaries
instead of built ones.

Apparently, extending PATH variable in main() was redundant with
explicit full path call in run_test() so drop the former.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: fix decoding of hlen on bigendian platforms
Florian Westphal [Fri, 22 Feb 2019 12:26:05 +0000 (13:26 +0100)] 
arptables-nft: fix decoding of hlen on bigendian platforms

The existing test fail with:
extensions/libarpt_standard.t: ERROR: line 2 (cannot find: arptables -I INPUT -s 192.168.0.1)

... because hlen is 0 instead of expected "6".
The rule is correct, i.e. this is a decode/display bug: arp_hlen is
specified as 'unsigned short' instead of uint8_t.

On LSB systems, this doesn't matter but on MSB the value then is '0x600'
instead of '0x006' which becomes 0 when assignment to the u8 header field.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
6 years agoarptables: Print space before comma and counters
Phil Sutter [Fri, 15 Feb 2019 14:27:43 +0000 (15:27 +0100)] 
arptables: Print space before comma and counters

Legacy arptables separates counters from rest of rule by ' , '. Assuming
that scripts scraping 'arptables -vL' output match on this, make
arptables-nft output conformant.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: Extend return codes check by error messages
Phil Sutter [Wed, 13 Feb 2019 10:11:27 +0000 (11:11 +0100)] 
tests: Extend return codes check by error messages

Check that error messages match between legacy and nft code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix error message for chain renaming
Phil Sutter [Wed, 13 Feb 2019 10:11:26 +0000 (11:11 +0100)] 
xtables: Fix error message for chain renaming

If the new name already exists, legacy iptables prints "File exists.".
This is a bit exotic, but more appropriate than "No chain/target/match
by that name." printed by iptables-nft without this patch.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix error messages in commands with rule number
Phil Sutter [Wed, 13 Feb 2019 10:11:25 +0000 (11:11 +0100)] 
xtables: Fix error messages in commands with rule number

Use E2BIG if rule identified by given number is not found. ENOENT is
used if referenced chain is not found. Without this, a command
specifying a non-existing chain in combination with a rule number like
e.g.: 'iptables-nft -I nonexist 23 -j ACCEPT' returns "Index of
insertion too big." instead of "No chain/target/match by that name."
like legacy iptables does.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Move new chain check to where it belongs
Phil Sutter [Wed, 13 Feb 2019 10:11:24 +0000 (11:11 +0100)] 
xtables: Move new chain check to where it belongs

Instead of checking chain existence in xtables.c, do it in
nft_chain_user_add() and reuse predefined error message.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix error message when zeroing a non-existent chain
Phil Sutter [Wed, 13 Feb 2019 10:11:23 +0000 (11:11 +0100)] 
xtables: Fix error message when zeroing a non-existent chain

Previously, error message was a bit misleading:

| # iptables-nft -Z noexist
| iptables: Incompatible with this kernel.

Set errno value so that the typical "No chain/target/match by that
name." is printed instead.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Eliminate dead code in __nft_rule_list
Phil Sutter [Thu, 7 Feb 2019 08:20:10 +0000 (09:20 +0100)] 
nft: Eliminate dead code in __nft_rule_list

If passed a rulenum > 0, the function uses nftnl_rule_lookup_byindex()
and returns early. Negative rulenum values are not supposed to happen,
so the remaining code which iterates over the full list of rules does
not need to respect rulenum anymore.

Fixes: 039b048965210 ("nft: Make use of nftnl_rule_lookup_byindex()")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables-nft: Support user-defined chain policies
Phil Sutter [Thu, 7 Feb 2019 21:08:55 +0000 (22:08 +0100)] 
ebtables-nft: Support user-defined chain policies

Legacy ebtables supports policies for user-defined chains - and what's
worse, they default to ACCEPT unlike anywhere else. So lack of support
for this braindead feature in ebtables-nft is actually a change of
behaviour which very likely affects all ebtables users out there.

The solution implemented here uses an implicit (and transparent) last
rule in all user-defined ebtables-nft chains with policy other than
RETURN. This rule is identified by an nft comment
"XTABLES_EB_INTERNAL_POLICY_RULE" (since commit ccf154d7420c0 ("xtables:
Don't use native nftables comments") nft comments are not used
otherwise).

To minimize interference with existing code, this policy rule is removed
from chains during cache population and the policy is saved in
NFTNL_CHAIN_POLICY attribute. When committing changes to the kernel,
nft_commit() traverses through the list of chains and (re-)creates
policy rules if required.

In ebtables-nft-restore, table flushes are problematic. To avoid weird
kernel error responses, introduce a custom 'table_flush' callback which
removes any pending policy rule add/remove jobs prior to creating the
NFT_COMPAT_TABLE_FLUSH one.

I've hidden all this mess behind checks for h->family, so hopefully
impact on {ip,ip6,arp}tables-nft should be negligible.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft: Introduce UDATA_TYPE_EBTABLES_POLICY
Phil Sutter [Thu, 7 Feb 2019 21:08:54 +0000 (22:08 +0100)] 
nft: Introduce UDATA_TYPE_EBTABLES_POLICY

This will be used later to identify ebtables user-defined chain policy
rules.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft: Don't assume NFTNL_RULE_USERDATA holds a comment
Phil Sutter [Thu, 7 Feb 2019 21:08:53 +0000 (22:08 +0100)] 
nft: Don't assume NFTNL_RULE_USERDATA holds a comment

If this rule attribute is present but does not contain a comment,
get_comment() returns NULL which is then fed into strncpy() causing a
crash.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables-save: Fix table not found error message
Phil Sutter [Thu, 7 Feb 2019 21:13:31 +0000 (22:13 +0100)] 
xtables-save: Fix table not found error message

First of all, this error message should not appear on stdout, otherwise
it may end in dump files. Next, with completely empty ruleset, even
valid table names cause errors. To avoid this, continue operation if the
not found table is a builtin one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxshared: Explicitly pass target to command_jump()
Phil Sutter [Tue, 5 Feb 2019 16:01:42 +0000 (17:01 +0100)] 
xshared: Explicitly pass target to command_jump()

The use of global 'optarg' variable inside that function is a mess, but
most importantly it limits its applicability to input parsers. Fix this
by having it take the option argument as a parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoRevert "ebtables: use extrapositioned negation consistently"
Phil Sutter [Tue, 5 Feb 2019 17:18:02 +0000 (18:18 +0100)] 
Revert "ebtables: use extrapositioned negation consistently"

This reverts commit 5f508b76a0cebaf91965ffa678089222e2d47964.

While attempts at unifying syntax between arp-, eb- and iptables-nft
increase the opportunity for more code-sharing, they are problematic
when it comes to compatibility. Accepting the old syntax on input helps,
but due to the fact that neither arptables nor ebtables support --check
command we must expect for users to test existence of a rule by
comparing input with output. If that happens in a script, deviating from
the old syntax in output has a high chance of breaking it.

Therefore revert Florian's patch changing inversion character position
in output and review the old code for consistency - the only thing
changed on top of the actual revert is ebtables' own copy of
print_iface() to make it adhere to the intrapositioned negation scheme
used throughout ebtables.

Added extension tests by the reverted commit have been kept.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix for false-positive rule matching
Phil Sutter [Mon, 4 Feb 2019 20:52:53 +0000 (21:52 +0100)] 
xtables: Fix for false-positive rule matching

When comparing two rules with non-standard targets, differences in
targets' payloads wasn't respected.

The cause is a rather hideous one: Unlike xtables_find_match(),
xtables_find_target() did not care whether the found target was already
in use or not, so the same target instance was assigned to both rules
and therefore payload comparison happened over the same memory location.

With legacy iptables it is not possible to reuse a target: The only case
where two rules (i.e., iptables_command_state instances) could exist at
the same time is when comparing rules, but that's handled using libiptc.

The above change clashes with ebtables-nft's reuse of target objects:
While input parsing still just assigns the object from xtables_targets
list, rule conversion from nftnl to iptables_command_state allocates new
data. To fix this, make ebtables-nft input parsing use the common
command_jump() routine instead of its own simplified copy. In turn, this
also eliminates the ebtables-nft-specific variants of parse_target(),
though with a slight change of behaviour: Names of user-defined chains
are no longer allowed to contain up to 31 but merely 28 characters.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix for crash when comparing rules with standard target
Phil Sutter [Fri, 1 Feb 2019 18:17:50 +0000 (19:17 +0100)] 
xtables: Fix for crash when comparing rules with standard target

When parsing an nftnl_rule with a standard verdict,
nft_rule_to_iptables_command_state() initialized cs->target but didn't
care about cs->target->t. When later comparing that rule to another,
compare_targets() crashed due to unconditional access to t's fields.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: Fix arptables extension tests
Phil Sutter [Fri, 1 Feb 2019 16:06:19 +0000 (17:06 +0100)] 
extensions: Fix arptables extension tests

With changes to arptables-nft output, many of these tests fail because
rules are not printed as expected anymore. Since most of the tests with
explicitly defined output did so just because of added --h-length and
--h-type options, adjust input a little more (typically reordering of
arguments) to make output match input.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Set h-type/h-length masks by default, too
Phil Sutter [Fri, 1 Feb 2019 16:06:18 +0000 (17:06 +0100)] 
arptables-nft: Set h-type/h-length masks by default, too

These masks are not used in nftables backend, but mangle extension
checks arhln_mask value to make sure --h-length was given (which is
implicitly the case).

Fixes: 5aecb2d8bfdda ("arptables: pre-init hlen and ethertype")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft: Fix potential memleaks in nft_*_rule_find()
Phil Sutter [Tue, 22 Jan 2019 10:14:21 +0000 (11:14 +0100)] 
nft: Fix potential memleaks in nft_*_rule_find()

These functions parse an nftnl_rule into a local instance of
iptables_command_state which potentially allocates memory (for matches
or target), so call ops->clear_cs() before returning to caller.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: Fix rule listing with counters
Phil Sutter [Mon, 21 Jan 2019 16:43:34 +0000 (17:43 +0100)] 
ebtables: Fix rule listing with counters

This is a partial revert of commit 583b27eabcad6 ("ebtables-save: add -c
option, using xtables-style counters") which broke ruleset listing with
'--Lc' flag turned on:

| # ebtables-nft -L --Lc
| Bridge table: filter
|
| Bridge chain: INPUT, entries: 0, policy: ACCEPT
|
| Bridge chain: FORWARD, entries: 2, policy: ACCEPT
| -j foo
|  , pcnt = 0 -- bcnt = 0-j ACCEPT
|  , pcnt = 0 -- bcnt = 0
| Bridge chain: OUTPUT, entries: 0, policy: ACCEPT
|
| Bridge chain: foo, entries: 1, policy: RETURN
| -j ACCEPT
|  , pcnt = 0 -- bcnt = 0%

(That percentage sign means no newline after last line of output and
doesn't belong to ebtables-nft's output.)

Problem was that nft_bridge_print_rule() printed the counters after
nft_bridge_save_rule() had already printed the newline character.

Note also that there is no need to remove FMT_EBT_SAVE bit from 'format'
variable: It is set only by ebtables-nft-save which doesn't call
nft_bridge_print_rule().

Fixes: 583b27eabcad6 ("ebtables-save: add -c option, using xtables-style counters")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Catch errors when zeroing rule rounters
Phil Sutter [Sat, 15 Dec 2018 18:25:04 +0000 (19:25 +0100)] 
xtables: Catch errors when zeroing rule rounters

Covscan complained about call to batch_rule_add() not being checked.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agotests: shell: Add arptables-nft verbose output test
Phil Sutter [Thu, 31 Jan 2019 15:12:56 +0000 (16:12 +0100)] 
tests: shell: Add arptables-nft verbose output test

With arptables-nft output being in a very good state now, add a test to
ensure it stays that way.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Don't print default h-len/h-type values
Phil Sutter [Thu, 31 Jan 2019 15:12:55 +0000 (16:12 +0100)] 
arptables-nft: Don't print default h-len/h-type values

Default values for --h-len and --h-type being printed for rules where
user didn't provide them is unexpected and confusing. The drawback is
the opposite: If user provided either of them with their default value,
they are later omitted when listing rules. Though since unlike legacy
arptables we can't distinguish between not specified and specified with
default value, we can't fix both - so choose to optimize for the more
likely case.

Fixes: 5aecb2d8bfdda ("arptables: pre-init hlen and ethertype")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft-save: Fix position of -j option
Phil Sutter [Thu, 31 Jan 2019 15:12:54 +0000 (16:12 +0100)] 
arptables-nft-save: Fix position of -j option

Legacy arptables-save (just like arptables itself) prints verdict as
first option, then matches and finally any target options.

To achieve this without introducing double/trailing spaces everywhere,
integrate target ('-j') option printing into
nft_arp_print_rule_details() and make it print separating whitespace
before each option.

In nft_arp_save_rule(), replace the call to save_matches_and_target() by
by a direct call to cs->target->save() since the former prints '-j'
option itself. Since there are no match extensions in arptables, any
other code from that function is not needed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Remove space between *cnt= and value
Phil Sutter [Thu, 31 Jan 2019 15:12:53 +0000 (16:12 +0100)] 
arptables-nft: Remove space between *cnt= and value

When printing rule counters, call xtables_print_num() with FMT_NOTABLE
bit set to avoid spaces between equal sign and value.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Fix CLASSIFY target printing
Phil Sutter [Thu, 31 Jan 2019 15:12:52 +0000 (16:12 +0100)] 
arptables-nft: Fix CLASSIFY target printing

In legacy arptables, CLASSIFY target is not printed with fixed hex
number lengths. Counter this by introducing a dedicated target
definition for NFPROTO_ARP only having own print/save callbacks.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Fix MARK target parsing and printing
Phil Sutter [Thu, 31 Jan 2019 15:12:51 +0000 (16:12 +0100)] 
arptables-nft: Fix MARK target parsing and printing

Legacy arptables parses mark values in hex no matter if prefixed with
'0x' or not. Sadly, this is not easily achievable with guided option
parser. Hence fall back to the old 'parse' callback. The introduced
target definition is valid only for revision 2, but that's consistent
with legacy arptables.

When printing, use --set-mark option instead of --set-xmark.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-nft: Fix listing rules without target
Phil Sutter [Thu, 31 Jan 2019 15:12:50 +0000 (16:12 +0100)] 
arptables-nft: Fix listing rules without target

Don't try to print cs.jumpto if it is an empty string, otherwise listing
(and verbose output) contains '-j' flag without argument.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Speed up chain deletion in large rulesets
Phil Sutter [Wed, 12 Dec 2018 19:04:12 +0000 (20:04 +0100)] 
xtables: Speed up chain deletion in large rulesets

Kernel prefers to identify chain by handle if it was given which causes
manual traversal of the chain list. In contrast, chain lookup by name in
kernel makes use of a hash table so is considerably faster. Force this
code path by removing the cached chain's handle when removing it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix for inserting rule at wrong position
Phil Sutter [Tue, 15 Jan 2019 22:23:05 +0000 (23:23 +0100)] 
xtables: Fix for inserting rule at wrong position

iptables-restore allows to insert rules at a certain position which is
problematic for iptables-nft to realize since rule position is not
determined by number but handle of previous or following rule and in
case the rules surrounding the new one are new as well, they don't have
a handle to refer to yet.

Fix this by making use of NFTNL_RULE_POSITION_ID attribute: When
inserting before a rule which does not have a handle, refer to it using
its NFTNL_RULE_ID value. If the latter doesn't exist either, assign a
new one to it.

The last used rule ID value is tracked in a new field of struct
nft_handle which is incremented before each use.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoutils: Add a manpage for nfbpf_compile
Phil Sutter [Wed, 16 Jan 2019 21:47:59 +0000 (22:47 +0100)] 
utils: Add a manpage for nfbpf_compile

Content is rather sparse, but still better than no manpage at all.

Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix position of replaced rules in cache
Phil Sutter [Tue, 15 Jan 2019 22:23:04 +0000 (23:23 +0100)] 
xtables: Fix position of replaced rules in cache

When replacing a rule, the replacement was simply appended to the
chain's rule list. Instead, insert it where the rule it replaces was.

This also fixes for zero counters command to remove the old rule from
cache.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Add new builtin chains to cache immediately
Phil Sutter [Tue, 15 Jan 2019 22:23:03 +0000 (23:23 +0100)] 
nft: Add new builtin chains to cache immediately

Newly created builtin chains missing from cache was the sole reason for
the immediate calls to nft_commit(). With nft_chain_builtin_add()
inserting the new chain into the table's chain list, this is not needed
anymore. Just make sure batch_obj_del() doesn't free the payload of
NFT_COMPAT_CHAIN_ADD jobs since it contains the new chain which has
been added to cache.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Set errno in nft_rule_check() if chain not found
Phil Sutter [Sun, 30 Dec 2018 19:06:10 +0000 (20:06 +0100)] 
xtables: Set errno in nft_rule_check() if chain not found

With this, the explicit check for chain existence can be removed from
xtables.c since all related commands do this now.

Note that this effectively changes the error message printed by
iptables-nft when given a non-existing chain, but the new error
message(s) conform with those printed by legacy iptables.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Simplify flush_chain_cache()
Phil Sutter [Sun, 30 Dec 2018 19:06:09 +0000 (20:06 +0100)] 
nft: Simplify flush_chain_cache()

With all the checks for 'tablename' being non-NULL, this code was rather
stupid and really hard to read. And the fix is indeed quite simple: If a
table name was given, use nft_table_builtin_find() and just flush its
chain cache. Otherwise iterate over all builtin tables without any
conditionals for 'tablename'.

Fixes: d4b0d248cc057 ("nft: Reduce indenting level in flush_chain_cache()")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Simplify nft_is_chain_compatible()
Phil Sutter [Sun, 30 Dec 2018 19:06:08 +0000 (20:06 +0100)] 
nft: Simplify nft_is_chain_compatible()

Make use of nft_{table,chain}_builtin_find() instead of open-coding the
list traversal. Since code is pretty obvious now, drop the comments
added earlier.

Fixes: e774b15299c27 ("nft: Review is_*_compatible() routines")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Make use of nftnl_rule_lookup_byindex()
Phil Sutter [Thu, 20 Dec 2018 15:09:20 +0000 (16:09 +0100)] 
nft: Make use of nftnl_rule_lookup_byindex()

Use the function where suitable to potentially speedup rule cache lookup
by rule number.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Optimize list rules command with given chain
Phil Sutter [Thu, 20 Dec 2018 15:09:19 +0000 (16:09 +0100)] 
xtables: Optimize list rules command with given chain

If a chain name was given, make use of nftnl_chain_list_lookup_byname().

Likewise in nftnl_rule_list_chain_save(), but introduce
__nftnl_rule_list_chain_save() suitable for passing to
nftnl_chain_list_foreach().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Optimize list command with given chain
Phil Sutter [Thu, 20 Dec 2018 15:09:18 +0000 (16:09 +0100)] 
xtables: Optimize list command with given chain

Make use of nftnl_chain_list_lookup_byname() even if not listing a
specific rule. Introduce __nft_print_header() to consolidate chain value
extraction for printing with ops->print_header().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Optimize user-defined chain deletion
Phil Sutter [Thu, 20 Dec 2018 15:09:17 +0000 (16:09 +0100)] 
xtables: Optimize user-defined chain deletion

Make use of nftnl_chain_list_lookup_byname() if a chain name was given.
Move the actual chain deleting code into a callback suitable for passing
to nftnl_chain_list_foreach().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agotests: Extend verbose output and return code tests
Phil Sutter [Thu, 20 Dec 2018 15:09:16 +0000 (16:09 +0100)] 
tests: Extend verbose output and return code tests

Recent changes to chain flush and zero routines incorporate proper error
propagation so trying to flush or zero a non-existent chain results in
an error. This is consistent with iptables-legacy, extend tests to make
sure it stays this way.

Also extend verbose output test to make these recent changes didn't mess
it up.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Optimize nft_chain_zero_counters()
Phil Sutter [Thu, 20 Dec 2018 15:09:15 +0000 (16:09 +0100)] 
xtables: Optimize nft_chain_zero_counters()

If a chain name was given, make use of nftnl_chain_list_lookup_byname().
Streamline nft_chain_zero_rule_counters() to be suitable for calling
from nftnl_chain_list_foreach().

There is an unrelated optimization in here, too: Add batch job
NFT_COMPAT_CHAIN_ZERO only if it is a base chain. Since user-defined
chains don't have counters, there is no need to do anything for them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Optimize flushing a specific chain
Phil Sutter [Thu, 20 Dec 2018 15:09:14 +0000 (16:09 +0100)] 
xtables: Optimize flushing a specific chain

If a chain name is given to nft_rule_flush(), make use of
nftnl_chain_list_lookup_byname().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Drop nft_chain_list_find()
Phil Sutter [Thu, 20 Dec 2018 15:09:13 +0000 (16:09 +0100)] 
nft: Drop nft_chain_list_find()

Replace the function by nftnl_chain_list_lookup_byname() as provided by
libnftnl.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Implement per chain rule cache
Phil Sutter [Thu, 20 Dec 2018 15:09:12 +0000 (16:09 +0100)] 
xtables: Implement per chain rule cache

Use recently introduced support for rules inside chains in libnftnl to
introduce a rule cache per chain instead of a global one.

A tricky bit is to decide if cache should be updated or not. Previously,
the global rule cache was populated just once and then reused unless
being flushed completely (via call to flush_rule_cache() with
NULL-pointer table argument). Resemble this behaviour by introducing a
boolean indicating cache status and fetch rules for all chains when
updating the chain cache in nft_chain_list_get().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Move nft_rule_list_get() above nft_chain_list_get()
Phil Sutter [Thu, 20 Dec 2018 15:09:11 +0000 (16:09 +0100)] 
nft: Move nft_rule_list_get() above nft_chain_list_get()

Later when introducing per chain rule caches, nft_rule_list_get() will
be removed. But nftnl_rule_list_cb() which it uses will be reused to
update each chain's rule cache from inside nftnl_chain_list_get(), so
move both into position.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Introduce fetch_chain_cache()
Phil Sutter [Thu, 20 Dec 2018 15:09:10 +0000 (16:09 +0100)] 
nft: Introduce fetch_chain_cache()

Move chain cache population from nft_chain_list_get() into a dedicated
function.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Simplify nft_rule_insert() a bit
Phil Sutter [Thu, 20 Dec 2018 15:09:09 +0000 (16:09 +0100)] 
nft: Simplify nft_rule_insert() a bit

Fetch rule list right on top instead of in each branch separately.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Simplify per table chain cache update
Phil Sutter [Thu, 20 Dec 2018 15:09:08 +0000 (16:09 +0100)] 
nft: Simplify per table chain cache update

Previously, each table's chain cache was potentially unallocated until
nftnl_chain_list_cb() saw a chain for it. This means such callback had to
check the chain_cache pointer for each chain belonging to that table.

In addition to the above, nft_chain_list_get() had to cover for the
possibility that a given table didn't have any chains at all in kernel,
so check requested table's chain cache once more and allocate it if
NULL.

Instead, simply iterate over all tables and preallocate their chain
caches prior to requesting the chain list from kernel. The only caveat
is to flush the chain cache completely before retrying in case of EINTR.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Reduce indenting level in flush_chain_cache()
Phil Sutter [Thu, 20 Dec 2018 15:09:07 +0000 (16:09 +0100)] 
nft: Reduce indenting level in flush_chain_cache()

Instead of doing all in one go, make two separate decisions:

1) If table has no chain cache, either continue or return depending on
   whether we're flushing for a specific table.

2) With chain cache present, flushing strategy once more depends on
   whether we're flushing for a specific table: If given, just remove
   all rules and return. If not, free the cache and set to NULL (so that
   it will be repopulated later), then continue the loop.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Reduce __nft_rule_del() signature
Phil Sutter [Thu, 20 Dec 2018 15:09:06 +0000 (16:09 +0100)] 
nft: Reduce __nft_rule_del() signature

The function does not use passed struct nftnl_rule_list, so remove it
from its parameters.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Review is_*_compatible() routines
Phil Sutter [Thu, 20 Dec 2018 15:09:05 +0000 (16:09 +0100)] 
nft: Review is_*_compatible() routines

- Call to nft_table_builtin_find() in nft_is_table_compatible() is not
  needed, as it is repeated in the latter call to nft_chain_list_get()
  by nft_are_chains_compatible().

- Turn nft_is_chain_compatible(), nft_is_rule_compatible() and
  nft_is_expr_compatible() into callbacks for use with respective
  foreach functions.

- nft_are_chains_compatible() is not needed anymore due to foreach
  function use.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables-restore: Review chain handling
Phil Sutter [Thu, 20 Dec 2018 15:09:04 +0000 (16:09 +0100)] 
xtables-restore: Review chain handling

There is no need to "delete" (actually, remove from cache) a chain if
noflush wasn't given: While handling the corresponding table line,
'table_flush' callback has already taken care of that.

This .chain_del indirection is not required since d1eb4d587297
("iptables-compat: chains are purge out already from table flush").

Streamlining the code further, move syntax checks to the top. If these
concede, there are three cases to distinguish:

A) Given chain name matches a builtin one in current table, so assume it
   exists already and just set policy and counters.

B) Noflush was given and the (custom) chain exists already, flush it.

C) Custom chain was either flushed (noflush not given) or didn't exist
   before, create it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Review unclear return points
Phil Sutter [Thu, 20 Dec 2018 15:09:03 +0000 (16:09 +0100)] 
nft: Review unclear return points

When converting to per table chain caches, these two error returns were
marked for review but apparently forgotten. Make sure error condition is
propagated when returning at those points.

Fixes: c58ecf9f8bcb7 ("xtables: Introduce per table chain caches")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: Simplify nftnl_rule_list_chain_save()
Phil Sutter [Thu, 20 Dec 2018 15:09:02 +0000 (16:09 +0100)] 
nft: Simplify nftnl_rule_list_chain_save()

Since there are per table chain caches, The chain list passed to that
function is comprised of chains belonging to the right table only.
Therefore the table name check can safely be skipped.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: TRACE: Point at xtables-monitor in documentation
Phil Sutter [Tue, 18 Dec 2018 11:16:30 +0000 (12:16 +0100)] 
extensions: TRACE: Point at xtables-monitor in documentation

With iptables-nft, logging of trace events is different from legacy.
Explain why and hint at how to receive events in this case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoinclude: extend the headers conflict workaround to in6.h
Baruch Siach [Sun, 2 Dec 2018 16:56:34 +0000 (18:56 +0200)] 
include: extend the headers conflict workaround to in6.h

Commit 8d9d7e4b9ef ("include: fix build with kernel headers before 4.2")
introduced a kernel/user headers conflict workaround that allows build
of iptables with kernel headers older than 4.2. This minor extension
allows build with kernel headers older than 3.12, which is the version
that introduced explicit IP headers synchronization.

Fixes: 8d9d7e4b9ef4 ("include: fix build with kernel headers before 4.2")
Cc: Florian Westphal <fw@strlen.de>
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: libipt_realm: Document allowed realm values
Phil Sutter [Mon, 3 Dec 2018 13:52:28 +0000 (14:52 +0100)] 
extensions: libipt_realm: Document allowed realm values

Older versions of iptables allowed for negative realm values by accident
(they would be cast to unsigned). While this was clearly a bug, document
the fixed behaviour.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Don't use native nftables comments
Phil Sutter [Tue, 27 Nov 2018 19:07:11 +0000 (20:07 +0100)] 
xtables: Don't use native nftables comments

The problem with converting libxt_comment into nftables comment is that
rules change when parsing from kernel due to comment match being moved
to the end of the match list. And since match ordering matters, the rule
may not be found anymore when checking or deleting. Apart from that,
iptables-nft didn't support multiple comments per rule anymore. This is
a compatibility issue without technical reason.

Leave conversion from nftables comment to libxt_comment in place so we
don't break running systems during an update.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoebtables: Use xtables_exit_err()
Phil Sutter [Fri, 23 Nov 2018 14:32:53 +0000 (15:32 +0100)] 
ebtables: Use xtables_exit_err()

When e.g. ebtables-nft detects an incompatible table, a stray '.' was
printed as last line of output:

| # ebtables-nft -L
| table `filter' is incompatible, use 'nft' tool.
| .

This comes from ebtables' own exit_err callback. Instead use the common
one which also provides useful version information.

While being at it, align the final error message in xtables_eb_main()
with how the others print it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
6 years agoarptables: Support --set-counters option
Phil Sutter [Thu, 22 Nov 2018 19:50:13 +0000 (20:50 +0100)] 
arptables: Support --set-counters option

Relevant code for this was already present (short option '-c'), just the
long option definition was missing.

While being at it, add '-c' to help text.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: libip6t_mh: fix bogus translation error
Pablo Neira Ayuso [Mon, 19 Nov 2018 13:09:20 +0000 (14:09 +0100)] 
extensions: libip6t_mh: fix bogus translation error

 libip6t_mh.txlate: Fail
 src: ip6tables-translate -A INPUT -p mh --mh-type 1 -j ACCEPT
 exp: nft add rule ip6 filter INPUT meta l4proto 135 mh type 1 counter accept
 res: nft add rule ip6 filter INPUT meta l4proto mobility-header mh type 1 counter accept

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: make all nft_parse_ helpers static
Florian Westphal [Sun, 18 Nov 2018 18:26:51 +0000 (19:26 +0100)] 
xtables: make all nft_parse_ helpers static

no more external callers.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: rename opcodes to arp_opcodes
Florian Westphal [Fri, 8 Jun 2018 13:19:18 +0000 (15:19 +0200)] 
xtables: rename opcodes to arp_opcodes

way too generic name.

Signed-off-by: Florian Westphal <fw@strlen.de>