]> git.ipfire.org Git - thirdparty/iptables.git/log
thirdparty/iptables.git
3 years agoxshared: Store optstring in xtables_globals
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
xshared: Store optstring in xtables_globals

Preparing for a common option parser, store the string of options for
each family inside the respective xtables_globals object. The
array of long option definitions sitting in there already indicates it's
the right place.

While being at it, drop '-m' support from arptables-nft.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Introduce builtin_tables_lookup()
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
nft: Introduce builtin_tables_lookup()

The set of builtin tables to use is fully determined by the given family
so just look it up instead of having callers pass it explicitly.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: fix bashism
Jeremy Sowden [Fri, 1 Oct 2021 19:12:28 +0000 (20:12 +0100)] 
tests: shell: fix bashism

The `<(cmd)` redirection is specific to Bash.  Update the shebang
accordingly.

Fixes: 63ab4fe3a191 ("ebtables: Avoid dropping policy when flushing")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft: Delete builtin chains compatibly
Phil Sutter [Tue, 14 Sep 2021 10:15:29 +0000 (12:15 +0200)] 
nft: Delete builtin chains compatibly

Attempting to delete all chains if --delete-chain is called without
argument has unwanted side-effects especially legacy iptables users are
not aware of and won't expect:

* Non-default policies are ignored, a previously dropping firewall may
  start accepting traffic.

* The kernel refuses to remove non-empty chains, causing program abort
  even if no user-defined chain exists.

Fix this by requiring a rule cache in that situation and make builtin
chain deletion depend on its policy and number of rules. Since this may
change concurrently, check again when having to refresh the transaction.

Also, hide builtin chains from verbose output - their creation is
implicit, so treat their removal as implicit, too.

When deleting a specific chain, do not allow to skip the job though.
Otherwise deleting a builtin chain which is still in use will succeed
although not executed.

Fixes: 61e85e3192dea ("iptables-nft: allow removal of empty builtin chains")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft-chain: Introduce base_slot field
Phil Sutter [Fri, 17 Sep 2021 14:51:33 +0000 (16:51 +0200)] 
nft-chain: Introduce base_slot field

For builtin chains, record the base_chains array slot they are assigned
to. This simplifies removing that reference if they are being deleted
later.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Check base-chain compatibility when adding to cache
Phil Sutter [Tue, 21 Sep 2021 14:42:36 +0000 (16:42 +0200)] 
nft: Check base-chain compatibility when adding to cache

With introduction of dedicated base-chain slots, a selection process was
established as no longer all base-chains ended in the same chain list
for later searching/checking but only the first one found for each hook
matching criteria is kept and the rest discarded.

A side-effect of the above is that table compatibility checking started
to omit consecutive base-chains, making iptables-nft less restrictive as
long as the expected base-chains were returned first from kernel when
populating the cache.

Make behaviour consistent and warn users about the possibly disturbing
chains found by:

* Run all base-chain checks from nft_is_chain_compatible() before
  allowing a base-chain to occupy its slot.
* If an unfit base-chain was found (and discarded), flag the table's
  cache as tainted and warn about it if the remaining ruleset is
  otherwise compatible.

Since base-chains that remain in cache would pass
nft_is_chain_compatible() checking, remove that and reduce it to rule
inspection.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: cache: Avoid double free of unrecognized base-chains
Phil Sutter [Tue, 21 Sep 2021 09:39:45 +0000 (11:39 +0200)] 
nft: cache: Avoid double free of unrecognized base-chains

On error, nft_cache_add_chain() frees the allocated nft_chain object
along with the nftnl_chain it points at. Fix nftnl_chain_list_cb() to
not free the nftnl_chain again in that case.

Fixes: 176c92c26bfc9 ("nft: Introduce a dedicated base chain array")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables-translate: add missing argument and option to usage
mizuta.takeshi@fujitsu.com [Wed, 22 Sep 2021 14:26:00 +0000 (14:26 +0000)] 
xtables-translate: add missing argument and option to usage

In xtables-restore-translate usage, the argument <FILE> for the -f option and
the -V|--version option are missing, so added them.

Signed-off-by: MIZUTA Takeshi <mizuta.takeshi@fujitsu.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: iptables-test: Fix conditional colors on stderr
Phil Sutter [Wed, 15 Sep 2021 15:47:15 +0000 (17:47 +0200)] 
tests: iptables-test: Fix conditional colors on stderr

Štěpán's patch to make colored output depend on whether output is a TTY
clashed with my change to print errors to stderr instead of stdout.

Fix this by telling maybe_colored() if it should print colors or not as
only caller knows where output is sent to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoebtables: Avoid dropping policy when flushing
Phil Sutter [Wed, 15 Sep 2021 15:37:51 +0000 (17:37 +0200)] 
ebtables: Avoid dropping policy when flushing

Unlike nftables, ebtables' user-defined chains have policies -
ebtables-nft implements those internally as invisible last rule. In
order to recreate them after a flush command, a rule cache is needed.

https://bugzilla.netfilter.org/show_bug.cgi?id=1558

3 years agoiptables-test.py: print with color escapes only when stdout isatty
Štěpán Němec [Thu, 2 Sep 2021 11:33:07 +0000 (13:33 +0200)] 
iptables-test.py: print with color escapes only when stdout isatty

When the output doesn't go to a terminal (typical case: log files),
the escape sequences are just noise.

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: shell: Return non-zero on error
Phil Sutter [Mon, 6 Sep 2021 15:28:30 +0000 (17:28 +0200)] 
tests: shell: Return non-zero on error

If any test fails, return a non-zero exit code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: iptables-test: Exit non-zero on error
Phil Sutter [Mon, 6 Sep 2021 11:49:34 +0000 (13:49 +0200)] 
tests: iptables-test: Exit non-zero on error

If any test fails, return a non-zero exit code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: xlate-test: Exit non-zero on error
Phil Sutter [Mon, 6 Sep 2021 11:07:43 +0000 (13:07 +0200)] 
tests: xlate-test: Exit non-zero on error

If a test fails, return a non-zero exit code. To do so, propagate the
pass/fail statistics up to main() for evaluation. While being at it,
move the statistics printing into there as well and get rid of that
redundant assignment to 'test_passed'.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: iptables-test: Print errors to stderr
Phil Sutter [Thu, 12 Aug 2021 17:14:13 +0000 (19:14 +0200)] 
tests: iptables-test: Print errors to stderr

No big deal, just pass the extra parameter to the four error print
calls.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: xlate-test: Print errors to stderr
Phil Sutter [Thu, 12 Aug 2021 13:12:30 +0000 (15:12 +0200)] 
tests: xlate-test: Print errors to stderr

Return code is always zero, so grepping for output on stderr is a
simple way to detect testsuite failures.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: xlate-test: Don't skip any input after the first empty line
Phil Sutter [Mon, 6 Sep 2021 10:52:22 +0000 (12:52 +0200)] 
tests: xlate-test: Don't skip any input after the first empty line

In conditionals, testing the empty string evaluates to false. This is
dumb but seems intentional, as readline() method returns an empty string
at EOF. This is distinct from reading an empty line as the latter
contains the newline character - unless it is stripped in between
readline() and conditional. The fixed commit introduced just that by
accident, effectively reducing any test file to the first contained
test:

| $ ./xlate-test.py
| [...]
| 81 test files, 84 tests, 84 tests passed, 0 tests failed, 0 errors

With this change in place, the summary looks much better:

| 81 test files, 368 tests, 368 tests passed, 0 tests failed, 0 errors

Fixes: 62828a6aff231 ("tests: xlate-test: support multiline expectation")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: iptables-test: Fix missing chain case
Phil Sutter [Thu, 12 Aug 2021 17:11:59 +0000 (19:11 +0200)] 
tests: iptables-test: Fix missing chain case

If a chain line was really missing, Python complained about reference
before assignment of 'chain_array' variable. While being at it, reuse
print_error() function for reporting and allow to continue with the next
input file instead of exiting.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoiptables-nft: allow removal of empty builtin chains
Florian Westphal [Sat, 14 Aug 2021 17:46:43 +0000 (19:46 +0200)] 
iptables-nft: allow removal of empty builtin chains

The only reason why this is prohibited is that you cannot do it
in iptables-legacy.

This removes the artifical limitation.

"iptables-nft -X" will leave the builtin chains alone;
Also, deletion is only permitted if the chain is empty.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoFix a few doc typos
Štěpán Němec [Thu, 2 Sep 2021 11:33:59 +0000 (13:33 +0200)] 
Fix a few doc typos

Just a few minor language fixes.

Signed-off-by: Štěpán Němec <snemec@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Use xtables_{m,c}alloc() everywhere
Phil Sutter [Tue, 31 Aug 2021 10:29:43 +0000 (12:29 +0200)] 
nft: Use xtables_{m,c}alloc() everywhere

Make use of libxtables allocators where sensible to have implicit error
checking. Leave library-internal calls in place to not create unexpected
program exit points for users, apart from xt_xlate_alloc() as that
function called xtables_error() in error case which exits by itself
already.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Use xtables_malloc() in mnl_err_list_node_add()
Phil Sutter [Tue, 31 Aug 2021 10:26:20 +0000 (12:26 +0200)] 
nft: Use xtables_malloc() in mnl_err_list_node_add()

The function called malloc() without checking for memory allocation
failure. Simply replace the call by xtables_malloc() to fix that.

Fixes: 4e2020952d6f9 ("xtables: use libnftnl batch API")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoextensions: libxt_mac: Fix for missing space in listing
Phil Sutter [Mon, 30 Aug 2021 21:48:54 +0000 (23:48 +0200)] 
extensions: libxt_mac: Fix for missing space in listing

Listing the extension using 'iptables -L', there was no space between
'MAC' and the following Address.

Reported-by: Adam Wójcik <a.wojcik@hyp.home.pl>
Fixes: 1bdb5535f561a ("libxtables: Extend MAC address printing/parsing support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoiptables-test: Make netns spawning more robust
Phil Sutter [Wed, 11 Aug 2021 12:46:22 +0000 (14:46 +0200)] 
iptables-test: Make netns spawning more robust

On systems without unshare Python module, try to call unshare binary
with oneself as parameters.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoextensions: hashlimit: Fix tests with HZ=100
Phil Sutter [Mon, 9 Aug 2021 16:48:58 +0000 (18:48 +0200)] 
extensions: hashlimit: Fix tests with HZ=100

With the kernel ticking at 100Hz, a limit of 1/day with burst 5 does not
overflow in kernel, making the test unstable depending on kernel config.
Change it to not overflow with 1000Hz either by increasing the burst
value by a factor of 100.

Fixes: fcf9f6f25db11 ("extensions: libxt_hashlimit: add unit test")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoip6tables: masquerade: use fully-random so that nft can understand the rule
Pavel Tikhomirov [Wed, 4 Aug 2021 15:50:57 +0000 (18:50 +0300)] 
ip6tables: masquerade: use fully-random so that nft can understand the rule

Here is the problem:

[]# nft -v
nftables v0.9.8 (E.D.S.)
[]# iptables-nft -v
iptables v1.8.7 (nf_tables): no command specified
Try `iptables -h' or 'iptables --help' for more information.
[]# nft flush ruleset
[]# ip6tables-nft -t nat -A POSTROUTING  -j MASQUERADE --random-full
[]# nft list ruleset
table ip6 nat {
chain POSTROUTING {
type nat hook postrouting priority srcnat; policy accept;
counter packets 0 bytes 0 masquerade  random-fully
}
}
[]# nft list ruleset > /tmp/ruleset
[]# nft flush ruleset
[]# nft -f /tmp/ruleset
/tmp/ruleset:4:54-54: Error: syntax error, unexpected newline
counter packets 0 bytes 0 masquerade  random-fully

That's because nft list ruleset saves "random-fully" which is wrong
format for nft -f, right should be "fully-random".

We face this problem because we run k8s in Virtuozzo container, and k8s
creates those "random-fully" rules by iptables(nft) and then CRIU can't
restore those rules using nft.

Signed-off-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agolibxtables: exit if called by setuid executeable
Florian Westphal [Mon, 19 Jul 2021 14:35:09 +0000 (16:35 +0200)] 
libxtables: exit if called by setuid executeable

iptables (legacy or nft, doesn't matter) cannot be safely used with
setuid binaries.

Add a safety check for this.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests/shell: Assert non-verbose mode is silent
Phil Sutter [Tue, 3 Aug 2021 09:32:34 +0000 (11:32 +0200)] 
tests/shell: Assert non-verbose mode is silent

Unexpected output from iptables commands might mess up error-checking in
scripts for instance, so do a quick test of the most common commands.

Note: Test adds two rules to make sure flush command operates on a
non-empty chain.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Fix for non-verbose check command
Phil Sutter [Tue, 3 Aug 2021 08:55:20 +0000 (10:55 +0200)] 
nft: Fix for non-verbose check command

Check command was unconditionally verbose since v1.8.5. Make it respect
--verbose option again.

Fixes: a7f1e208cdf9c ("nft: split parsing from netlink commands")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoebtables: Dump atomic waste
Phil Sutter [Fri, 30 Jul 2021 10:25:10 +0000 (12:25 +0200)] 
ebtables: Dump atomic waste

With ebtables-nft.8 now educating people about the missing
functionality, get rid of atomic remains in source code. This eliminates
mostly comments except for --atomic-commit which was treated as alias of
--init-table. People not using the latter are probably trying to
atomic-commit from an atomic-file which in turn is not supported, so no
point keeping it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agodoc: ebtables-nft.8: Adjust for missing atomic-options
Phil Sutter [Wed, 28 Jul 2021 15:53:53 +0000 (17:53 +0200)] 
doc: ebtables-nft.8: Adjust for missing atomic-options

Drop any reference to them (and the environment variable) but list them
in BUGS section hinting at ebtables-save and -restore tools.

Fixes: 1939cbc25e6f5 ("doc: Adjust ebtables man page")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoxtables: Call init_extensions6() for static builds
Erik Wilson [Tue, 13 Jul 2021 23:48:23 +0000 (16:48 -0700)] 
xtables: Call init_extensions6() for static builds

Initialize extensions from libext6 for cases where xtables is built statically.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1550
Signed-off-by: Erik Wilson <Erik.E.Wilson@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoextensions: libxt_multiport: add translation for -m multiport --ports
Pablo Neira Ayuso [Fri, 25 Jun 2021 21:05:12 +0000 (23:05 +0200)] 
extensions: libxt_multiport: add translation for -m multiport --ports

Add a translation for -m multiport --ports. Extend the existing testcase.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoextensions: libxt_conntrack: simplify translation using negation
Pablo Neira Ayuso [Wed, 2 Jun 2021 23:58:43 +0000 (01:58 +0200)] 
extensions: libxt_conntrack: simplify translation using negation

Available since nftables 0.9.9. For example:

 # iptables-translate -I INPUT -m state ! --state NEW,INVALID
 nft insert rule ip filter INPUT ct state ! invalid,new  counter

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoextensions: libxt_tcp: rework translation to use flags match representation
Pablo Neira Ayuso [Wed, 2 Jun 2021 23:46:06 +0000 (01:46 +0200)] 
extensions: libxt_tcp: rework translation to use flags match representation

Use the new flags match representation available since nftables 0.9.9
to simplify the translation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoextensions: libxt_connlimit: add translation
Pablo Neira Ayuso [Wed, 2 Jun 2021 17:57:37 +0000 (19:57 +0200)] 
extensions: libxt_connlimit: add translation

This patch adds a translation for connlimit matches which requires
the definition of a set and the family context (either IPv4 or IPv6)
which is required to display the netmask accordingly.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: xlate-test: support multiline expectation
Pablo Neira Ayuso [Thu, 3 Jun 2021 22:19:40 +0000 (00:19 +0200)] 
tests: xlate-test: support multiline expectation

Extend translation test to deal with multiline translation, e.g.

iptables-translate -A INPUT -m connlimit --connlimit-above 2
nft add set ip filter connlimit0 { type ipv4_addr; flags dynamic; }
nft add rule ip filter INPUT add @connlimit0 { ip saddr ct count over 2 } counter

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agolibxtables: extend xlate infrastructure
Pablo Neira Ayuso [Wed, 2 Jun 2021 22:06:15 +0000 (00:06 +0200)] 
libxtables: extend xlate infrastructure

This infrastructure extends the existing xlate infrastructure:

- Extensions can define set dependencies through .xlate. The resulting
  set definition can be obtained through xt_xlate_set_get().
- Add xl_xlate_set_family() and xl_xlate_get_family() to store/fetch
  the family.

The first client of this new xlate API is the connlimit extension,
which is added in a follow up patch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoextensions: libxt_string: Avoid buffer size warning for strncpy()
Phil Sutter [Wed, 2 Jun 2021 13:15:37 +0000 (15:15 +0200)] 
extensions: libxt_string: Avoid buffer size warning for strncpy()

If the target buffer does not need to be null-terminated, one may simply
use memcpy() and thereby avoid any compiler warnings.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agolibxtables: Introduce xtables_strdup() and use it everywhere
Phil Sutter [Wed, 2 Jun 2021 10:56:06 +0000 (12:56 +0200)] 
libxtables: Introduce xtables_strdup() and use it everywhere

This wraps strdup(), checking for errors.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: libebt_ip6: Use xtables_ip6parse_any()
Phil Sutter [Wed, 2 Jun 2021 12:04:43 +0000 (14:04 +0200)] 
extensions: libebt_ip6: Use xtables_ip6parse_any()

The code was almost identical and suffered from the same problem as
fixed in commit a76a5c997a235 ("libxtables: fix two off-by-one memory
corruption bugs").

The only functional change this involves is ebt_parse_ip6_address() will
now accept hostnames as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoiptables-apply: Drop unused variable
Phil Sutter [Wed, 2 Jun 2021 10:50:57 +0000 (12:50 +0200)] 
iptables-apply: Drop unused variable

It was assigned to but never read.

Fixes: b45b4e3903414 ("iptables-apply: script and manpage update")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Avoid buffer size warnings copying iface names
Phil Sutter [Wed, 2 Jun 2021 09:58:06 +0000 (11:58 +0200)] 
nft: Avoid buffer size warnings copying iface names

The call to strncpy() is actually not needed: source buffer is only
IFNAMSIZ bytes large and guaranteed to be null-terminated. Use this to
avoid compiler warnings due to size parameter matching the destination
buffer size by performing the copy using (dumb) memcpy() instead.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Avoid memleak in error path of nft_cmd_new()
Phil Sutter [Wed, 2 Jun 2021 09:55:20 +0000 (11:55 +0200)] 
nft: Avoid memleak in error path of nft_cmd_new()

If rule allocation fails, free the allocated 'cmd' before returning to
caller.

Fixes: a7f1e208cdf9c ("nft: split parsing from netlink commands")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agolibxtables: Fix memleak in xtopt_parse_hostmask()
Phil Sutter [Wed, 2 Jun 2021 09:04:30 +0000 (11:04 +0200)] 
libxtables: Fix memleak in xtopt_parse_hostmask()

The allocated hostmask duplicate needs to be freed again.

Fixes: 66266abd17adc ("libxtables: XTTYPE_HOSTMASK support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: libebt_ip6: Drop unused variables
Phil Sutter [Fri, 13 Nov 2020 20:13:50 +0000 (21:13 +0100)] 
extensions: libebt_ip6: Drop unused variables

They are being assigned to but never read.

Fixes: 5c8ce9c6aede0 ("ebtables-compat: add 'ip6' match extension")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agolibxtables: Drop leftover variable in xtables_numeric_to_ip6addr()
Phil Sutter [Fri, 13 Nov 2020 20:04:39 +0000 (21:04 +0100)] 
libxtables: Drop leftover variable in xtables_numeric_to_ip6addr()

Variable 'err' was only used in removed debug code, so drop it as well.

Fixes: 7f526c9373c17 ("libxtables: xtables: remove unnecessary debug code")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: sctp: Translate --chunk-types option
Phil Sutter [Tue, 4 May 2021 14:26:42 +0000 (16:26 +0200)] 
extensions: sctp: Translate --chunk-types option

The translation is not fully complete as it is not possible to map 'any'
match type into nft syntax with a single rule. Also, 'only' match type
translation is a bit poor as it explicitly lists all chunk types that
are supposed to be missing.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: sctp: Fix nftables translation
Phil Sutter [Tue, 4 May 2021 14:03:24 +0000 (16:03 +0200)] 
extensions: sctp: Fix nftables translation

If both sport and dport was present, incorrect nft syntax was generated.

Fixes: defc7bd2bac89 ("extensions: libxt_sctp: Add translation to nft")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoUse proto_to_name() from xshared in more places
Phil Sutter [Mon, 16 Nov 2020 23:57:10 +0000 (00:57 +0100)] 
Use proto_to_name() from xshared in more places

Share the common proto name lookup code. While being at it, make proto
number variable 16bit, values may exceed 256.

This aligns iptables-nft '-p' argument printing with legacy iptables. In
practice, this should make a difference only in corner cases.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoebtables-translate: Use shared ebt_get_current_chain() function
Phil Sutter [Sat, 20 Jun 2020 08:11:52 +0000 (10:11 +0200)] 
ebtables-translate: Use shared ebt_get_current_chain() function

Drop the local reimplementation. It was barely different enough to
be buggy:

| % ebtables-nft -A foo -o eth0 -j ACCEPT
| % xtables-nft-multi ebtables-translate -A foo -o eth0 -j ACCEPT
| ebtables-translate v1.8.5 (nf_tables): Use -o only in OUTPUT, FORWARD and POSTROUTING chains
| Try `ebtables-translate -h' or 'ebtables-translate --help' for more information.

With this change, output is as expected:

| % xtables-nft-multi ebtables-translate -A foo -o eth0 -j ACCEPT
| nft add rule bridge filter foo oifname "eth0" counter accept

This is roughly the same issue fixed in commit e1ccd979e6849 ("ebtables:
fix over-eager -o checks on custom chains").

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxshared: Merge invflags handling code
Phil Sutter [Mon, 16 Nov 2020 16:20:57 +0000 (17:20 +0100)] 
xshared: Merge invflags handling code

Join invflags handling between iptables, ip6tables, xtables and
arptables. Ebtables still has its own code which differs quite a bit.

In order to use a shared set_option() routine, iptables and ip6tables
need to provide a local 'invflags' variable which is 16bits wide.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxshared: Eliminate iptables_command_state->invert
Phil Sutter [Wed, 11 Nov 2020 16:16:40 +0000 (17:16 +0100)] 
xshared: Eliminate iptables_command_state->invert

This field is not used by routines working with struct
iptables_command_state: It is merely a temporary flag used by parsers to
carry the '!' prefix until invflags have been populated (or error
checking done if unsupported).

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxtables: Make invflags 16bit wide
Phil Sutter [Mon, 2 Nov 2020 11:05:44 +0000 (12:05 +0100)] 
xtables: Make invflags 16bit wide

This is needed to merge with xtables-arp which has more builtin
options and hence needs more bits in invflags.

The only adjustment needed is the set_option() call for option '-j'
which passed a pointer to cs->fw.ip.invflags. That field can't be
changed, it belongs to uAPI. Though using args->invflags instead works
fine, aside from that '-j' doesn't support inverting so this is merely a
sanity check and no real invflag value assignment will happen.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: SECMARK: Implement revision 1
Phil Sutter [Thu, 29 Apr 2021 13:28:59 +0000 (15:28 +0200)] 
extensions: SECMARK: Implement revision 1

The changed data structure for communication with kernel allows to
exclude the field 'secid' which is populated on kernel side. Thus
this fixes the formerly always failing extension comparison breaking
rule check and rule delete by content.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft-arp: Make use of ipv4_addr_to_string()
Phil Sutter [Tue, 27 Apr 2021 08:02:34 +0000 (10:02 +0200)] 
nft-arp: Make use of ipv4_addr_to_string()

This eliminates quite a bit of redundant code apart from also dropping
use of obsolete function gethostbyaddr().

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoEliminate inet_aton() and inet_ntoa()
Phil Sutter [Tue, 27 Apr 2021 07:12:53 +0000 (09:12 +0200)] 
Eliminate inet_aton() and inet_ntoa()

Both functions are obsolete, replace them by equivalent calls to
inet_pton() and inet_ntop().

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoextensions: sctp: Explain match types in man page
Phil Sutter [Wed, 24 Mar 2021 15:18:58 +0000 (16:18 +0100)] 
extensions: sctp: Explain match types in man page

They weren't mentioned at all.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Increase BATCH_PAGE_SIZE to support huge rulesets
Phil Sutter [Tue, 6 Apr 2021 08:51:20 +0000 (10:51 +0200)] 
nft: Increase BATCH_PAGE_SIZE to support huge rulesets

In order to support the same ruleset sizes as legacy iptables, the
kernel's limit of 1024 iovecs has to be overcome. Therefore increase
each iovec's size from 128KB to 2MB.

While being at it, add a log message for failing sendmsg() call. This is
not supposed to happen, even if the transaction fails. Yet if it does,
users are left with only a "line XXX failed" message (with line number
being the COMMIT line).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agonft: cache: Sort chains on demand only
Phil Sutter [Thu, 25 Mar 2021 15:24:39 +0000 (16:24 +0100)] 
nft: cache: Sort chains on demand only

Mandatory sorted insert of chains into cache significantly slows down
restoring of large rulesets. Since the sorted list of user-defined
chains is needed for listing and verbose output only, introduce
nft_cache_sort_chains() and call it where needed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agofix build for missing ETH_ALEN definition
Maciej Żenczykowski [Fri, 2 Apr 2021 20:26:28 +0000 (13:26 -0700)] 
fix build for missing ETH_ALEN definition

(this is needed at least with bionic)

Signed-off-by: Maciej Żenczykowski <maze@google.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoextensions: libxt_conntrack: use bitops for status negation
Alexander Mikhalitsyn [Thu, 1 Apr 2021 13:47:08 +0000 (16:47 +0300)] 
extensions: libxt_conntrack: use bitops for status negation

At the moment, status_xlate_print function prints statusmask as comma-separated
sequence of enabled statusmask flags. But if we have inverted conntrack ctstatus
condition then we have to use more complex expression (if more than one flag enabled)
because nft not supports syntax like "ct status != expected,assured".

Examples:
! --ctstatus CONFIRMED,ASSURED
should be translated as
ct status & (assured|confirmed) == 0

! --ctstatus CONFIRMED
can be translated as
ct status & confirmed == 0

See also netfilter/xt_conntrack.c (conntrack_mt() function as a reference).

Reproducer:
$ iptables -A INPUT -d 127.0.0.1/32 -p tcp -m conntrack ! --ctstatus expected,assured -j DROP
$ nft list ruleset
...
meta l4proto tcp ip daddr 127.0.0.1 ct status != expected,assured counter packets 0 bytes 0 drop
...

it will fail if we try to load this rule:
$ nft -f nft_test
../nft_test:6:97-97: Error: syntax error, unexpected comma, expecting newline or semicolon

Cc: Florian Westphal <fw@strlen.de>
Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn@virtuozzo.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoextensions: libxt_conntrack: use bitops for state negation
Alexander Mikhalitsyn [Thu, 1 Apr 2021 13:47:07 +0000 (16:47 +0300)] 
extensions: libxt_conntrack: use bitops for state negation

Currently, state_xlate_print function prints statemask as comma-separated sequence of enabled
statemask flags. But if we have inverted conntrack ctstate condition then we have to use more
complex expression because nft not supports syntax like "ct state != related,established".

Reproducer:
$ iptables -A INPUT -d 127.0.0.1/32 -p tcp -m conntrack ! --ctstate RELATED,ESTABLISHED -j DROP
$ nft list ruleset
...
meta l4proto tcp ip daddr 127.0.0.1 ct state != related,established counter packets 0 bytes 0 drop
...

it will fail if we try to load this rule:
$ nft -f nft_test
../nft_test:6:97-97: Error: syntax error, unexpected comma, expecting newline or semicolon

Cc: Florian Westphal <fw@strlen.de>
Signed-off-by: Alexander Mikhalitsyn <alexander.mikhalitsyn@virtuozzo.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agolibxtables: Simplify xtables_ipmask_to_cidr() a bit
Phil Sutter [Tue, 2 Mar 2021 14:07:13 +0000 (15:07 +0100)] 
libxtables: Simplify xtables_ipmask_to_cidr() a bit

Reduce the whole mask matching into a single for-loop. No need for a
shortcut, /32 masks will match in the first iteration.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxtables-translate: Fix translation of odd netmasks
Phil Sutter [Tue, 2 Mar 2021 13:50:07 +0000 (14:50 +0100)] 
xtables-translate: Fix translation of odd netmasks

Iptables supports netmasks which are not prefixes to match on (or
ignore) arbitrary bits in an address. Yet nftables' prefix notation is
available for real prefixes only, so translation is not as trivial -
print bitmask syntax for those cases.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Fix bitwise expression avoidance detection
Phil Sutter [Fri, 19 Feb 2021 15:54:57 +0000 (16:54 +0100)] 
nft: Fix bitwise expression avoidance detection

Byte-boundary prefix detection was too sloppy: Any data following the
first zero-byte was ignored. Add a follow-up loop making sure there are
no stray bits in the designated host part.

Fixes: 323259001d617 ("nft: Optimize class-based IP prefix matches")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoiptables-nft: fix -Z option
Florian Westphal [Wed, 24 Feb 2021 10:08:02 +0000 (11:08 +0100)] 
iptables-nft: fix -Z option

it zeroes the rule counters, so it needs fully populated cache.
Add a test case to cover this.

Fixes: 9d07514ac5c7a ("nft: calculate cache requirements from list of commands")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
4 years agoinclude: Drop libipulog.h
Phil Sutter [Tue, 16 Feb 2021 22:17:02 +0000 (23:17 +0100)] 
include: Drop libipulog.h

The file is not included anywhere, also it seems outdated compared to
the one in libnetfilter_log (which also holds the implementation).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoebtables: Exit gracefully on invalid table names
Phil Sutter [Thu, 28 Jan 2021 00:09:56 +0000 (01:09 +0100)] 
ebtables: Exit gracefully on invalid table names

Users are able to cause program abort by passing a table name that
doesn't exist:

| # ebtables-nft -t dummy -P INPUT ACCEPT
| ebtables: nft-cache.c:455: fetch_chain_cache: Assertion `t' failed.
| Aborted

Avoid this by checking table existence just like iptables-nft does upon
parsing '-t' optarg. Since the list of tables is known and fixed,
checking the given name's length is pointless. So just drop that check
in return.

With this patch in place, output looks much better:

| # ebtables-nft -t dummy -P INPUT ACCEPT
| ebtables v1.8.7 (nf_tables): table 'dummy' does not exist
| Perhaps iptables or your kernel needs to be upgraded.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoconfigure: bump version for 1.8.7 release v1.8.7
Pablo Neira Ayuso [Wed, 13 Jan 2021 15:47:07 +0000 (16:47 +0100)] 
configure: bump version for 1.8.7 release

Update libtool version for libxtables since two new interfaces have been
added:

- xtables_parse_mac_and_mask()
- xtables_print_well_known_mac_and_mask()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests/shell: Fix nft-only/0009-needless-bitwise_0
Phil Sutter [Fri, 15 Jan 2021 20:58:48 +0000 (21:58 +0100)] 
tests/shell: Fix nft-only/0009-needless-bitwise_0

For whatever reason, stored expected output contains false handles. To
overcome this, filter the rule data lines from both expected and stored
output before comparing.

Fixes: 81a2e12851283 ("tests/shell: Add test for bitwise avoidance fixes")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Avoid pointless table/chain creation
Phil Sutter [Fri, 10 Jul 2020 16:23:50 +0000 (18:23 +0200)] 
nft: Avoid pointless table/chain creation

Accept a chain name in nft_xt_builtin_init() to limit the base chain
creation to that specific chain only.

Introduce nft_xt_builtin_table_init() to create just the table for
situations where no builtin chains are needed but the command may still
succeed in an empty ruleset, particularly when creating a custom chain,
restoring base chains or adding a set for ebtables among match.

Introduce nft_xt_fake_builtin_chains(), a function to call after cache
has been populated to fill empty base chain slots. This keeps ruleset
listing output intact if some base chains do not exist (or even the
whole ruleset is completely empty).

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: shell: Drop any dump sorting in place
Phil Sutter [Mon, 6 Jul 2020 16:40:16 +0000 (18:40 +0200)] 
tests: shell: Drop any dump sorting in place

With iptables-nft-save output now sorted just like legacy one, no
sorting to unify them is needed anymore.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: cache: Sort custom chains by name
Phil Sutter [Thu, 30 Jul 2020 08:24:10 +0000 (10:24 +0200)] 
nft: cache: Sort custom chains by name

With base chains no longer residing in the tables' chain lists, they can
easily be sorted upon insertion. This on one hand aligns custom chain
ordering with legacy iptables and on the other makes it predictable,
which is very helpful when manually comparing ruleset dumps for
instance.

Adjust the one ebtables-nft test case this change breaks (as wrong
ordering is expected in there). The manual output sorting done for tests
which apply to legacy as well as nft is removed in a separate patch.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Introduce a dedicated base chain array
Phil Sutter [Tue, 7 Jul 2020 09:43:26 +0000 (11:43 +0200)] 
nft: Introduce a dedicated base chain array

Preparing for sorted chain output, introduce a per-table array holding
base chains indexed by nf_inet_hooks value. Since the latter is ordered
correctly, iterating over the array will return base chains in expected
order.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Introduce struct nft_chain
Phil Sutter [Wed, 29 Jul 2020 12:33:33 +0000 (14:33 +0200)] 
nft: Introduce struct nft_chain

Preparing for ordered output of user-defined chains, introduce a local
datatype wrapping nftnl_chain. In order to maintain the chain name hash
table, introduce nft_chain_list as well and use it instead of
nftnl_chain_list.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: cache: Move nft_chain_find() over
Phil Sutter [Thu, 10 Sep 2020 14:19:44 +0000 (16:19 +0200)] 
nft: cache: Move nft_chain_find() over

It is basically just a cache lookup, hence fits better in here.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Implement nft_chain_foreach()
Phil Sutter [Wed, 8 Jul 2020 14:09:52 +0000 (16:09 +0200)] 
nft: Implement nft_chain_foreach()

This is just a fancy wrapper around nftnl_chain_list_foreach() with the
added benefit of detecting invalid table names or uninitialized chain
lists. This in turn allows to drop the checks in flush_rule_cache() and
ignore the return code of nft_chain_foreach() as it fails only if the
dropped checks had failed, too.

Since this wrapper does the chain list lookup by itself, use of
nft_chain_list_get() shrinks down to a single place, namely inside
nft_chain_find(). Therefore fold it into the latter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: cache: Introduce nft_cache_add_chain()
Phil Sutter [Fri, 10 Jul 2020 19:37:38 +0000 (21:37 +0200)] 
nft: cache: Introduce nft_cache_add_chain()

This is a convenience function for adding a chain to cache, for now just
a simple wrapper around nftnl_chain_list_add_tail().

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Fix selective chain compatibility checks
Phil Sutter [Wed, 23 Sep 2020 17:13:45 +0000 (19:13 +0200)] 
nft: Fix selective chain compatibility checks

Since commit 80251bc2a56ed ("nft: remove cache build calls"), 'chain'
parameter passed to nft_chain_list_get() is no longer effective.
Before, it was used to fetch only that single chain from kernel when
populating the cache. So the returned list of chains for which
compatibility checks are done would contain only that single chain.

Re-establish the single chain compat checking by introducing a dedicated
code path to nft_is_chain_compatible() doing so.

Fixes: 80251bc2a56ed ("nft: remove cache build calls")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxtables-monitor:
Florian Westphal [Mon, 14 Dec 2020 16:11:23 +0000 (17:11 +0100)] 
xtables-monitor:

'LL=0x304' is not very convenient, print LOOPBACK instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoxtables-monitor: print packet first
Florian Westphal [Sat, 12 Dec 2020 15:15:34 +0000 (16:15 +0100)] 
xtables-monitor: print packet first

The trace mode should first print the packet that was received and
then the rule/verdict.

Furthermore, the monitor did sometimes print an extra newline.

After this patch, output is more consistent with nft monitor.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoxtables-monitor: fix packet family protocol
Florian Westphal [Sat, 12 Dec 2020 15:15:33 +0000 (16:15 +0100)] 
xtables-monitor: fix packet family protocol

This prints the family passed on the command line (which might be 0).
Print the table family instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoxtables-monitor: fix rule printing
Florian Westphal [Sat, 12 Dec 2020 15:15:32 +0000 (16:15 +0100)] 
xtables-monitor: fix rule printing

trace_print_rule does a rule dump.  This prints unrelated rules
in the same chain.  Instead the function should only request the
specific handle.

Furthermore, flush output buffer afterwards so this plays nice when
output isn't a terminal.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agoextensions: dccp: Fix for DCCP type 'INVALID'
Phil Sutter [Wed, 2 Dec 2020 12:37:06 +0000 (13:37 +0100)] 
extensions: dccp: Fix for DCCP type 'INVALID'

Support for matching on invalid DCCP type field values was pretty
broken: While RFC4340 declares any type value from 10 to 15 invalid, the
extension's type name 'INVALID' mapped to type value 10 only. Fix this
by introduction of INVALID_OTHER_TYPE_MASK which has the remaining
invalid type's bits set and apply it if bit 10 is set after parsing the
type list. When printing, stop searching type names after printing
'INVALID' - unless numeric output was requested. The latter prints all
actual type values. Since parsing types in numeric form is not
supported, changing the output should not break existing scripts.

When translating into nftables syntax, the code returned prematurely if
'INVALID' was among the list of types - thereby emitting invalid syntax.
Instead print a real match for invalid types by use of a range
expression.

While being at it, fix syntax of translator output: If only
'--dccp-types' was translated, the output contained an extra 'dccp'. On
the other hand, if '--sport' and '--dport' was present, a required
'dccp' between the translations of both was missing.

Fixes: e40b11d7ef827 ("add support for new 'dccp' protocol match")
Fixes: c94a998724143 ("extensions: libxt_dccp: Add translation to nft")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests/shell: Test for fixed extension registration
Phil Sutter [Fri, 4 Dec 2020 16:44:51 +0000 (17:44 +0100)] 
tests/shell: Test for fixed extension registration

Use strace to look at iptables-restore behaviour with typically
problematic input (conntrack revision 0 is no longer supported by
current kernels) to make sure the fix in commit a1eaaceb0460b
("libxtables: Simplify pending extension registration") is still
effective.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxshared: Merge some command option-related code
Phil Sutter [Fri, 30 Oct 2020 11:42:57 +0000 (12:42 +0100)] 
xshared: Merge some command option-related code

Add OPT_FRAGMENT define into the enum of other OPT_* defines at the
right position and adjust the arptables-specific ones that follow
accordingly. Appropriately adjust inverse_for_options array in
xtables-arp.c.

Extend optflags from iptables.c by the arptables values for the sake of
completeness, then move it to xshared.h along with NUMBER_OF_OPT
definition. As a side-effect, this fixes for wrong ordering of entries
in arptables' 'optflags' copy.

Add arptables-specific bits to commands_v_options table (the speicific
options are matches on ARP header fields, just treat them like '-s'
option. This is also just a cosmetic change, arptables doesn't have a
generic_opt_check() implementation and hence doesn't use such a table.

With things potentially ready for common use, move commands_v_options
table along with generic_opt_check() and opt2char() into xshared.c and
drop the local (identical) implementations from iptables.c, ip6tables.c
xtables.c and xtables-arp.c. While doing so, fix ordering of entries in
that table: the row for CMD_ZERO_NUM was in the wrong position. Since
all moved rows though are identical, this had no effect in practice.

Fixes: d960a991350ca ("xtables-arp: Integrate OPT_* defines into xshared.h")
Fixes: 384958620abab ("use nf_tables and nf_tables compatibility interface")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoxtables-arp: Don't use ARPT_INV_*
Phil Sutter [Tue, 3 Nov 2020 11:21:29 +0000 (12:21 +0100)] 
xtables-arp: Don't use ARPT_INV_*

Arptables invflags are partly identical to IPT_INV_* ones but the bits
are differently assigned. Eliminate this incompatibility by definition
of the unique invflags in nft-arp.h on bits that don't collide with
IPT_INV_* ones, then use those in combination with IPT_INV_* ones in
arptables-specific code.

Note that ARPT_INV_ARPPRO is replaced by IPT_INV_PROTO although these
are in fact different options - yet since '-p' option is not supported
by arptables, this does not lead to a collision.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agolibxtables: Extend MAC address printing/parsing support
Phil Sutter [Tue, 3 Nov 2020 11:08:39 +0000 (12:08 +0100)] 
libxtables: Extend MAC address printing/parsing support

Adding a parser which supports common names for special MAC/mask
combinations and a print routine detecting those special addresses and
printing the respective name allows to consolidate all the various
duplicated implementations.

The side-effects of this change are manageable:

* arptables now accepts "BGA" as alias for the bridge group address
* "mac" match now prints MAC addresses in lower-case which is consistent
  with the remaining code at least

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoiptables-test.py: Try to unshare netns by default
Phil Sutter [Tue, 17 Nov 2020 18:00:45 +0000 (19:00 +0100)] 
iptables-test.py: Try to unshare netns by default

If user did not explicitly requst to "test netnamespace path", try an
import of 'unshare' module and call unshare() to avoid killing the local
host's network by accident.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoiptables-test.py: Accept multiple test files on commandline
Phil Sutter [Tue, 17 Nov 2020 15:18:33 +0000 (16:18 +0100)] 
iptables-test.py: Accept multiple test files on commandline

This allows to call the script like so:

| # ./iptables-test.py -n extensions/libebt_*.t

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoebtables: Fix for broken chain renaming
Phil Sutter [Tue, 17 Nov 2020 10:38:27 +0000 (11:38 +0100)] 
ebtables: Fix for broken chain renaming

Loading extensions pollutes 'errno' value, hence before using it to
indicate failure it should be sanitized. This was done by the called
function before the parsing/netlink split and not migrated by accident.
Move it into calling code to clarify the connection.

Fixes: a7f1e208cdf9c ("nft: split parsing from netlink commands")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotests: shell: update format of registers in bitwise payloads.
Pablo Neira Ayuso [Mon, 16 Nov 2020 09:37:41 +0000 (10:37 +0100)] 
tests: shell: update format of registers in bitwise payloads.

libnftnl has been changed to bring the format of registers in bitwise
dumps in line with those in other types of expression.  Update the
expected output of Python test-cases.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests/shell: Add test for bitwise avoidance fixes
Phil Sutter [Tue, 10 Nov 2020 13:50:46 +0000 (14:50 +0100)] 
tests/shell: Add test for bitwise avoidance fixes

Masked address matching was recently improved to avoid bitwise
expression if the given mask covers full bytes. Make use of nft netlink
debug output to assert iptables-nft generates the right bytecode for
each situation.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoebtables: Optimize masked MAC address matches
Phil Sutter [Fri, 30 Oct 2020 13:08:33 +0000 (14:08 +0100)] 
ebtables: Optimize masked MAC address matches

Just like with class-based prefix matches in iptables-nft, optimize
masked MAC address matches if the mask is on a byte-boundary.

To reuse the logic in add_addr(), extend it to accept the payload base
value via parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Optimize class-based IP prefix matches
Phil Sutter [Fri, 2 Oct 2020 07:44:38 +0000 (09:44 +0200)] 
nft: Optimize class-based IP prefix matches

Payload expression works on byte-boundaries, leverage this with suitable
prefix lengths.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoconfigure: bump version for 1.8.6 release v1.8.6
Pablo Neira Ayuso [Sat, 31 Oct 2020 17:26:57 +0000 (18:26 +0100)] 
configure: bump version for 1.8.6 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agotests: shell: Improve concurrent noflush restore test a bit
Phil Sutter [Mon, 26 Oct 2020 16:25:03 +0000 (17:25 +0100)] 
tests: shell: Improve concurrent noflush restore test a bit

The described issue happens only if chain FOO does not exist at program
start so flush the ruleset after each iteration to make sure this is the
case. Sadly the bug is still not 100% reproducible on my testing VM.

While being at it, add a paragraph describing what exact situation the
test is trying to provoke.

Fixes: dac904bdcd9a1 ("nft: Fix for concurrent noflush restore calls")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agonft: Fix for concurrent noflush restore calls
Phil Sutter [Mon, 5 Oct 2020 14:06:49 +0000 (16:06 +0200)] 
nft: Fix for concurrent noflush restore calls

Transaction refresh was broken with regards to nft_chain_restore(): It
created a rule flush batch object only if the chain was found in cache
and a chain add object only if the chain was not found. Yet with
concurrent ruleset updates, one has to expect both situations:

* If a chain vanishes, the rule flush job must be skipped and instead
  the chain add job become active.

* If a chain appears, the chain add job must be skipped and instead
  rules flushed.

Change the code accordingly: Create both batch objects and set their
'skip' field depending on the situation in cache and adjust both in
nft_refresh_transaction().

As a side-effect, the implicit rule flush becomes explicit and all
handling of implicit batch jobs is dropped along with the related field
indicating such.

Reuse the 'implicit' parameter of __nft_rule_flush() to control the
initial 'skip' field value instead.

A subtle caveat is vanishing of existing chains: Creating the chain add
job based on the chain in cache causes a netlink message containing that
chain's handle which the kernel dislikes. Therefore unset the chain's
handle in that case.

Fixes: 58d7de0181f61 ("xtables: handle concurrent ruleset modifications")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agolibiptc: Avoid gcc-10 zero-length array warning
Phil Sutter [Thu, 8 Oct 2020 12:51:52 +0000 (14:51 +0200)] 
libiptc: Avoid gcc-10 zero-length array warning

Gcc-10 doesn't like the use of zero-length arrays as last struct member
to denote variable sized objects. The suggested alternative, namely to
use a flexible array member as defined by C99, is problematic as that
doesn't allow for said struct to be embedded into others. With the
relevant structs being part of kernel UAPI, this can't be precluded
though.

The call to memcpy() which triggers the warning copies data from one
struct xt_counters to another. Since this struct is flat and merely
contains two u64 fields, One can use direct assignment instead which
avoids the warning.

Signed-off-by: Phil Sutter <phil@nwl.cc>