]> git.ipfire.org Git - thirdparty/iptables.git/log
thirdparty/iptables.git
3 years agoxshared: Prefer xtables_chain_protos lookup over getprotoent
Phil Sutter [Tue, 1 Mar 2022 22:05:29 +0000 (23:05 +0100)] 
xshared: Prefer xtables_chain_protos lookup over getprotoent

When dumping a large ruleset, common protocol matches such as for TCP
port number significantly slow down rule printing due to repeated calls
for getprotobynumber(). The latter does not involve any caching, so
/etc/protocols is consulted over and over again.

As a simple countermeasure, make functions converting between proto
number and name prefer the built-in list of "well-known" protocols. This
is not a perfect solution, repeated rules for protocol names libxtables
does not cache (e.g. igmp or dccp) will still be slow. Implementing
getprotoent() result caching could solve this.

As a side-effect, explicit check for pseudo-protocol "all" may be
dropped as it is contained in the built-in list and therefore immutable.

Also update xtables_chain_protos entries a bit to align with typical
/etc/protocols contents. The testsuite assumes those names, so the
preferred ones prior to this patch are indeed uncommon nowadays.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
3 years agonft: Speed up immediate parsing
Phil Sutter [Tue, 1 Mar 2022 18:46:21 +0000 (19:46 +0100)] 
nft: Speed up immediate parsing

Parsing of rules which jump to a chain pointlessly causes a call to
xtables_find_target() despite the code already knowing the outcome.

Avoid the significant delay for rulesets with many chain jumps by
performing the (standard) target lookup only for accept/drop/return
verdicts.

From a biased test-case on my VM:

| # iptables-nft-save | grep -c -- '-j'
| 133943
| # time ./old/iptables-nft-save >/dev/null
| real 0m45.566s
| user 0m1.308s
| sys 0m8.430s
| # time ./new/iptables-nft-save >/dev/null
| real 0m3.547s
| user 0m0.762s
| sys 0m2.476s

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
3 years agonft: Simplify immediate parsing
Phil Sutter [Tue, 1 Mar 2022 17:59:31 +0000 (18:59 +0100)] 
nft: Simplify immediate parsing

Implementations of parse_immediate callback are mostly trivial, the only
relevant part is access to family-specific parts of struct
iptables_command_state when setting goto flag for iptables and
ip6tables. Refactor them into simple set_goto_flag callbacks.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
3 years agoImprove error messages for unsupported extensions
Phil Sutter [Fri, 11 Feb 2022 16:47:22 +0000 (17:47 +0100)] 
Improve error messages for unsupported extensions

If a given extension was not supported by the kernel, iptables would
print a rather confusing error message if extension parameters were
given:

| # rm /lib/modules/$(uname -r)/kernel/net/netfilter/xt_LOG.ko
| # iptables -A FORWARD -j LOG --log-prefix foo
| iptables v1.8.7 (legacy): unknown option "--log-prefix"

Avoid this by pretending extension revision 0 is always supported. It is
the same hack as used to successfully print extension help texts as
unprivileged user, extended to all error codes to serve privileged ones
as well.

In addition, print a warning if kernel rejected revision 0 and it's not
a permissions problem. This helps users find out which extension in a
rule the kernel didn't like.

Finally, the above commands result in these messages:

| Warning: Extension LOG revision 0 not supported, missing kernel module?
| iptables: No chain/target/match by that name.

Or, for iptables-nft:

| Warning: Extension LOG revision 0 not supported, missing kernel module?
| iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain FORWARD

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agolibxtables: Register only the highest revision extension
Phil Sutter [Fri, 11 Feb 2022 16:39:24 +0000 (17:39 +0100)] 
libxtables: Register only the highest revision extension

When fully registering extensions, ignore all consecutive ones with same
name and family value. Since commit b3ac87038f4e4 ("libxtables: Make
sure extensions register in revision order"), one may safely assume the
list of pending extensions has highest revision numbers first. Since
iptables is only interested in the highest revision the kernel supports,
registration and compatibility checks may be skipped once the first
matching extension in pending list has validated.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Implement xtables lock timeout using signals
Jethro Beekman [Mon, 14 Feb 2022 09:35:56 +0000 (10:35 +0100)] 
xshared: Implement xtables lock timeout using signals

Previously, if a lock timeout is specified using `-wN `, flock() is
called using LOCK_NB in a loop with a sleep. This results in two issues.

The first issue is that the process may wait longer than necessary when
the lock becomes available. For this the `-W` option was added, but this
requires fine-tuning.

The second issue is that if lock contention is high, invocations using
`-w` (without a timeout) will always win lock acquisition from
invocations that use `-w N`. This is because invocations using `-w` are
actively waiting on the lock whereas those using `-w N` only check from
time to time whether the lock is free, which will never be the case.

This patch removes the sleep loop and deprecates the `-W` option (making
it non-functional). Instead, flock() is always called in a blocking
fashion, but the alarm() function is used with a non-SA_RESTART signal
handler to cancel the system call.

Signed-off-by: Jethro Beekman <jethro@fortanix.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: NFLOG: enable `--nflog-range` tests
Jeremy Sowden [Sat, 12 Feb 2022 16:58:32 +0000 (16:58 +0000)] 
tests: NFLOG: enable `--nflog-range` tests

iptables-legacy and iptable-nft have different results for these tests.
Now that it is possible to specify the expected results correctly, we
can enable the tests.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: support explicit variant test result
Jeremy Sowden [Sat, 12 Feb 2022 16:58:31 +0000 (16:58 +0000)] 
tests: support explicit variant test result

Now that there are more than two test results, add support for
explicitly indicating which result to expect if the variants differ.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: add `NOMATCH` test result
Jeremy Sowden [Sat, 12 Feb 2022 16:58:30 +0000 (16:58 +0000)] 
tests: add `NOMATCH` test result

Currently, there are two supported test results: `OK` and `FAIL`.  It is
expected that either the iptables command fails, or it succeeds and
dumping the rule has the correct output.  However, it is possible that
the command may succeed but the output may not be correct.  Add a
`NOMATCH` result to cover this outcome.

Make a few white-space improvements at the same time.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agotests: iptables-test: rename variable
Jeremy Sowden [Sat, 12 Feb 2022 16:58:29 +0000 (16:58 +0000)] 
tests: iptables-test: rename variable

"Splitted" hasn't been current since the seventeenth century.  Replace it with
"tokens".

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoiptables.8: Describe the effect of multiple -v flags
Phil Sutter [Tue, 8 Feb 2022 14:06:28 +0000 (15:06 +0100)] 
iptables.8: Describe the effect of multiple -v flags

Functionality differs between legacy and nft variants, detail the
effects a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agotests: iptables-test: Support variant deviation
Phil Sutter [Fri, 4 Feb 2022 17:45:22 +0000 (18:45 +0100)] 
tests: iptables-test: Support variant deviation

Some test results are not consistent between variants:

* CLUSTERIP is not supported with nft_compat, so all related tests fail
  with iptables-nft.
* iptables-legacy mandates TCPMSS be combined with SYN flag match,
  iptables-nft does not care. (Or precisely, xt_TCPMSS.ko can't validate
  match presence.)

Introduce an optional fourth test spec field to specify the variant it
applies to. Consequently, the opposite result is expected with the other
variant.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: cache: Dump rules if debugging
Phil Sutter [Fri, 4 Feb 2022 13:44:26 +0000 (14:44 +0100)] 
nft: cache: Dump rules if debugging

If verbose flag was given twice, dump rules while populating the cache.
This not only applies to list commands, but all requiring a rule cache -
e.g. insert with position.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Add debug output to table creation
Phil Sutter [Fri, 4 Feb 2022 12:38:48 +0000 (13:38 +0100)] 
nft: Add debug output to table creation

This at least allows to inspect how tables are created on demand.
Also requires setting NFTNL_TABLE_FAMILY for clean output.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoebtables: Support verbose mode
Phil Sutter [Fri, 4 Feb 2022 12:27:51 +0000 (13:27 +0100)] 
ebtables: Support verbose mode

Accept '-v' flag in both ebtables-nft and ebtables-nft-restore. Mostly
interesting because it allows for netlink debug output when specified
multiple times.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Set NFTNL_CHAIN_FAMILY in new chains
Phil Sutter [Fri, 4 Feb 2022 09:16:19 +0000 (10:16 +0100)] 
nft: Set NFTNL_CHAIN_FAMILY in new chains

Kernel doesn't need it, but debug output improves significantly. Before
this patch:

| # iptables-nft -vv -A INPUT
| [...]
| unknown filter INPUT use 0 type filter hook unknown prio 0 policy accept packets 0 bytes 0
| [...]

and after:

| # iptables-nft -vv -A INPUT
| [...]
| ip filter INPUT use 0 type filter hook input prio 0 policy accept packets 0 bytes 0
| [...]

While being at it, make nft_chain_builtin_alloc() take only the builtin
table's name as parameter - it's the only field it accesses.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoiptables-restore: Support for extra debug output
Phil Sutter [Fri, 28 Jan 2022 14:32:10 +0000 (15:32 +0100)] 
iptables-restore: Support for extra debug output

Treat --verbose just like iptables itself, increasing debug level with
number of invocations.

To propagate the level into do_command() callback, insert virtual '-v'
flags into rule lines.

The only downside of this is that simple verbose output is changed and
now also prints the rules as they are added - which would be useful if
the lines contained the chain they apply to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Use verbose flag to toggle debug output
Phil Sutter [Fri, 28 Jan 2022 18:35:54 +0000 (19:35 +0100)] 
nft: Use verbose flag to toggle debug output

Copy legacy iptables' behaviour, printing debug output if verbose flag
is given more than once.

Since nft debug output applies to netlink messages which are not created
until nft_action() phase, carrying verbose value is non-trivial -
introduce a field in struct nft_handle for that.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: add support for native tcp flag matching
Florian Westphal [Tue, 25 Jan 2022 16:53:01 +0000 (17:53 +0100)] 
nft: add support for native tcp flag matching

prefer payload + bitwise + cmp to nft_compat match.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft-shared: add tcp flag dissection
Florian Westphal [Tue, 25 Jan 2022 16:53:00 +0000 (17:53 +0100)] 
nft-shared: add tcp flag dissection

Detect payload load of th->flags and convert it to xt tcp match
structure.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft: prefer native expressions instead of tcp match
Florian Westphal [Tue, 25 Jan 2022 16:52:59 +0000 (17:52 +0100)] 
nft: prefer native expressions instead of tcp match

Instead of using nft_compat+xtables tcp match, prefer to
emit payload+cmp or payload+range expression.

Unlike udp, tcp has flag bits that can be matched too but
we have to fall back to the xt expression for now.
We also don't support tcp option match, but thats a rarely
used feature anyway.

Delinearization support for ports was added in previous patches.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft: prefer native expressions instead of udp match
Florian Westphal [Tue, 25 Jan 2022 16:52:58 +0000 (17:52 +0100)] 
nft: prefer native expressions instead of udp match

Instead of using nft_compat+xtables udp match, prefer to
emit payload+cmp or payload+range expression.

Delinearization support was added in previous patches.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft-shared: support native udp port delinearize
Florian Westphal [Tue, 25 Jan 2022 16:52:57 +0000 (17:52 +0100)] 
nft-shared: support native udp port delinearize

same as previous patch, but for udp.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft-shared: support native tcp port range delinearize
Florian Westphal [Tue, 25 Jan 2022 16:52:56 +0000 (17:52 +0100)] 
nft-shared: support native tcp port range delinearize

adds support for
nft ... tcp dport != min-max

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft-shared: support native tcp port delinearize
Florian Westphal [Tue, 25 Jan 2022 16:52:55 +0000 (17:52 +0100)] 
nft-shared: support native tcp port delinearize

This extends iptables-nft dissector to decode native tcp
port matching.  nft ruleset:

table ip filter {
        chain INPUT {
                type filter hook input priority filter; policy accept;
                tcp sport 12345
                tcp sport 12345 tcp dport 6789
                tcp sport < 1024
                tcp dport >= 1024
        }
}

$ iptables-nft-save
-A INPUT -p tcp -m tcp --sport 12345
-A INPUT -p tcp -m tcp --sport 12345 --dport 6789
-A INPUT -p tcp -m tcp --sport 0:1023
-A INPUT -p tcp -m tcp --dport 1024:65535

This would allow to extend iptables-nft to prefer
native payload expressions for --sport,dport in the future.

Also, parse_cmp must not clear the "payload" flag, this is because
cmp-based range expressions will contain following sequence:

payload => reg1
cmp reg1 > minv
cmp reg1 < maxv

... so second cmp would work.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: fix typo
Jeremy Sowden [Thu, 20 Jan 2022 10:54:05 +0000 (10:54 +0000)] 
extensions: libxt_NFLOG: fix typo

The deprecation warning for `--nflog-range` contains a spelling mistake.
Fix it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoxshared: Fix response to unprivileged users
Phil Sutter [Tue, 18 Jan 2022 21:39:08 +0000 (22:39 +0100)] 
xshared: Fix response to unprivileged users

Expected behaviour in both variants is:

* Print help without error, append extension help if -m and/or -j
  options are present
* Indicate lack of permissions in an error message for anything else

With iptables-nft, this was broken basically from day 1. Shared use of
do_parse() then somewhat broke legacy: it started complaining about
inability to create a lock file.

Fix this by making iptables-nft assume extension revision 0 is present
if permissions don't allow to verify. This is consistent with legacy.

Second part is to exit directly after printing help - this avoids having
to make the following code "nop-aware" to prevent privileged actions.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Florian Westphal <fw@strlen.de>
3 years agobuild: replace `AM_PROG_LIBTOOL` and `AC_DISABLE_STATIC` with `LT_INIT`
Jeremy Sowden [Fri, 1 Oct 2021 17:41:41 +0000 (18:41 +0100)] 
build: replace `AM_PROG_LIBTOOL` and `AC_DISABLE_STATIC` with `LT_INIT`

`AM_PROG_LIBTOOL` is superseded by `LT_INIT`, which also accepts options
to control the defaults for creating shared or static libraries.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: remove extra space when saving targets with prefixes
Jeremy Sowden [Fri, 1 Oct 2021 17:41:40 +0000 (18:41 +0100)] 
extensions: libxt_NFLOG: remove extra space when saving targets with prefixes

When printing out NFLOG targets an extra space was inserted between
`--nflog-prefix` and the prefix itself:

  $ sudo /usr/sbin/iptables -A INPUT -j NFLOG --nflog-prefix test
  $ sudo /usr/sbin/iptables-save | grep NFLOG
  -A INPUT -j NFLOG --nflog-prefix  test
                                  ^^
Fixes: 73866357e4a7 ("iptables: do not print trailing whitespaces")
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: fix `--nflog-prefix` Python test-cases
Jeremy Sowden [Fri, 1 Oct 2021 17:41:39 +0000 (18:41 +0100)] 
extensions: libxt_NFLOG: fix `--nflog-prefix` Python test-cases

The `iptables-save` includes an extra space between `--nflog-prefix` and
the prefix.

The maximum length of prefixes includes the trailing NUL character.

NFLOG silently truncates prefixes which exceed the maximum length.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: disable `--nflog-range` Python test-cases
Kyle Bowman [Fri, 1 Oct 2021 17:41:38 +0000 (18:41 +0100)] 
extensions: libxt_NFLOG: disable `--nflog-range` Python test-cases

nft has no equivalent to `--nflog-range`, so we cannot emulate it and
the Python unit-tests for it fail.  However, since `--nflog-range` is
broken and doesn't do anything, the tests are not testing anything
useful.

Signed-off-by: Kyle Bowman <kbowman@cloudflare.com>
Signed-off-by: Alex Forster <aforster@cloudflare.com>
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: don't truncate log prefix on print/save
Kyle Bowman [Fri, 1 Oct 2021 17:41:37 +0000 (18:41 +0100)] 
extensions: libxt_NFLOG: don't truncate log prefix on print/save

When parsing the rule, use a struct with a layout compatible to that of
struct xt_nflog_info, but with a buffer large enough to contain the
whole 128-character nft prefix.

We always send the nflog-group to the kernel since, for nft, log and
nflog targets are handled by the same kernel module, and are
distinguished by whether they define an nflog-group. Therefore, we must
send the group even if it is zero, or the kernel will configure the
target as a log, not an nflog.

Changes to nft_is_expr_compatible were made since only targets which
have an `nflog-group` are compatible. Since nflog targets are
distinguished by having an nflog-group, we ignore targets without one.

We also set the copy-len flag if the snap-len is set since without this,
iptables will mistake `nflog-size` for `nflog-range`.

Signed-off-by: Kyle Bowman <kbowman@cloudflare.com>
Signed-off-by: Alex Forster <aforster@cloudflare.com>
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: libxt_NFLOG: use nft built-in logging instead of xt_NFLOG
Kyle Bowman [Fri, 1 Oct 2021 17:41:36 +0000 (18:41 +0100)] 
extensions: libxt_NFLOG: use nft built-in logging instead of xt_NFLOG

Replaces the use of xt_NFLOG with the nft built-in log statement.

This additionally adds support for using longer log prefixes of 128
characters in size. Until now NFLOG has truncated the log-prefix to the
64-character limit supported by iptables-legacy. We now use the struct
xtables_target's udata member to store the longer 128-character prefix
supported by iptables-nft.

Signed-off-by: Kyle Bowman <kbowman@cloudflare.com>
Signed-off-by: Alex Forster <aforster@cloudflare.com>
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoextensions: *NAT: Kill multiple IPv4 range support
Phil Sutter [Sat, 8 Jan 2022 12:15:20 +0000 (13:15 +0100)] 
extensions: *NAT: Kill multiple IPv4 range support

It is the year of the great revolution, nobody cares about kernel
versions below 2.6.11 anymore. Time to get rid of the cruft.

While being at it, drop the explicit duplicate argument check and
instead just remove XTOPT_MULTI flag from the respective
xt_option_entry.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
3 years agotests: iptables-test: correct misspelt variable
Jeremy Sowden [Fri, 1 Oct 2021 17:41:42 +0000 (18:41 +0100)] 
tests: iptables-test: correct misspelt variable

"EXECUTEABLE" -> "EXECUTABLE"

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agonft: fix indentation error.
Jeremy Sowden [Fri, 1 Oct 2021 17:41:35 +0000 (18:41 +0100)] 
nft: fix indentation error.

`add_action` was indented with 7 spaces.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoip6tables: Use the shared do_parse, too
Phil Sutter [Fri, 26 Nov 2021 22:56:57 +0000 (23:56 +0100)] 
ip6tables: Use the shared do_parse, too

Same change as with iptables, merely have to set IP6T_F_PROTO flag in
ipv6_proto_parse().

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoiptables: Use xtables' do_parse() function
Phil Sutter [Thu, 4 Nov 2021 14:42:16 +0000 (15:42 +0100)] 
iptables: Use xtables' do_parse() function

To do so, a few conversions are needed:

- Make use of xt_params->optstring
- Make use of xt_params->print_help callback
- Switch to using a proto_parse callback

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Move proto_parse and post_parse callbacks to xshared
Phil Sutter [Fri, 24 Dec 2021 13:55:28 +0000 (14:55 +0100)] 
nft: Move proto_parse and post_parse callbacks to xshared

They are not nft-variant-specific and may therefore be shared with
legacy.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Store parsed wait and wait_interval in xtables_args
Phil Sutter [Fri, 24 Dec 2021 13:17:50 +0000 (14:17 +0100)] 
xshared: Store parsed wait and wait_interval in xtables_args

While nft-variants don't care, legacy ones do.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Move do_parse to shared space
Phil Sutter [Thu, 23 Dec 2021 18:03:37 +0000 (19:03 +0100)] 
xshared: Move do_parse to shared space

Small adjustments were needed:

- Pass line variable via xt_cmd_parse, xshared.c does not have it in
  namespace.
- Replace opts, prog_name and prog_vers defines by the respective
  xt_params field reference.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Do not pass nft_handle to do_parse()
Phil Sutter [Thu, 23 Dec 2021 17:55:53 +0000 (18:55 +0100)] 
xtables: Do not pass nft_handle to do_parse()

Make it fit for sharing with legacy iptables, drop nft-specific
parameter. This requires to mirror proto_parse and post_parse callbacks
from family_ops somewhere reachable - use xt_cmd_parse, it holds other
"parser setup data" as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Pass xtables_args to check_inverse()
Phil Sutter [Thu, 23 Dec 2021 17:29:40 +0000 (18:29 +0100)] 
xtables: Pass xtables_args to check_inverse()

It holds the accessed family field as well and is more generic than
nft_handle.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Pass xtables_args to check_empty_interface()
Phil Sutter [Thu, 23 Dec 2021 17:27:31 +0000 (18:27 +0100)] 
xtables: Pass xtables_args to check_empty_interface()

It holds the accessed family field as well and is more generic than
nft_handle.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Move struct nft_xt_cmd_parse to xshared.h
Phil Sutter [Wed, 22 Dec 2021 20:58:18 +0000 (21:58 +0100)] 
xtables: Move struct nft_xt_cmd_parse to xshared.h

Preparing for shared use with legacy variants, move it to "neutral
ground" and give it a more generic name.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Pull table validity check out of do_parse()
Phil Sutter [Fri, 26 Nov 2021 14:53:14 +0000 (15:53 +0100)] 
xtables: Pull table validity check out of do_parse()

Makes do_parse() more generic, error codes don't change so this should
be safe.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Drop xtables' family on demand feature
Phil Sutter [Fri, 26 Nov 2021 17:26:07 +0000 (18:26 +0100)] 
xtables: Drop xtables' family on demand feature

This conditional h->family assignment was added by commit 3f7877e6be987
("xtables-restore: add -4 and -6 support") with the intention to support
something like 'xtables-restore -6 <ip6tables.dump', i.e. having
family-agnostic commands which accept flags to set the family. Yet
commit be70918eab26e ("xtables: rename xt-multi binaries to -nft,
-legacy") removed support for such command names back in 2018 and nobody
has complained so far. Therefore drop this leftover as it makes
do_parse() more generic.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft-shared: set correct register value
Florian Westphal [Sat, 18 Dec 2021 20:14:15 +0000 (21:14 +0100)] 
nft-shared: set correct register value

NFTNL_EXPR_META_DREG equals NFTNL_EXPR_PAYLOAD_BASE, so we set
dreg to the payload base instead.

It "works" because the simple nft rules currently generated via
ipables-nft have base == register-number but this is a
coincidence.

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoiptables-*-restore: Drop pointless line reference
Phil Sutter [Fri, 26 Nov 2021 20:45:12 +0000 (21:45 +0100)] 
iptables-*-restore: Drop pointless line reference

There's no need to mention the offending line number in error message
when calling xtables_error() with a status of PARAMETER_PROBLEM as that
will cause a call to xtables_exit_tryhelp() which in turn prints "Error
occurred at line: N".

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agolibxtables: Extend basic_exit_err()
Phil Sutter [Fri, 26 Nov 2021 20:11:25 +0000 (21:11 +0100)] 
libxtables: Extend basic_exit_err()

Basically merge the function with xtables_exit_error,
printing a status-specific footer for parameter or version problems.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables_globals: Embed variant name in .program_version
Phil Sutter [Fri, 26 Nov 2021 15:53:31 +0000 (16:53 +0100)] 
xtables_globals: Embed variant name in .program_version

Both are constant strings, so precompiler may concat them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share exit_tryhelp()
Phil Sutter [Fri, 26 Nov 2021 23:18:28 +0000 (00:18 +0100)] 
xshared: Share exit_tryhelp()

The function existed three times in identical form. Avoid having to
declare extern int line in xshared.c by making it a parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share a common printhelp function
Phil Sutter [Thu, 21 Oct 2021 11:04:45 +0000 (13:04 +0200)] 
xshared: Share a common printhelp function

Help texts in legacy and nft variants are supposed to be identical, but
those of iptables and ip6tables largely overlapped already. By referring
to xt_params and afinfo pointers, it is relatively trivial to craft a
suitable help text on demand, so duplicated help texts can be
eliminated.

As a side-effect, this fixes ip6tables-nft help text - it was identical
to that of iptables-nft.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share print_match_save() between legacy ip*tables
Phil Sutter [Thu, 21 Oct 2021 01:00:57 +0000 (03:00 +0200)] 
xshared: Share print_match_save() between legacy ip*tables

The only difference between the former two copies was the type of
ip*_entry parameter. But since it is treated opaque, just hide that
detail by casting to void.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoextensions: tcpmss: add iptables-translate support
Florian Westphal [Sun, 28 Nov 2021 17:25:53 +0000 (18:25 +0100)] 
extensions: tcpmss: add iptables-translate support

Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agoxshared: Make load_proto() static
Phil Sutter [Sat, 6 Nov 2021 00:32:47 +0000 (01:32 +0100)] 
xshared: Make load_proto() static

The last outside users vanished ten years ago.

Fixes: 449cdd6bcc8d1 ("src: combine default_command functions")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft-shared: Drop unused function print_proto()
Phil Sutter [Sat, 6 Nov 2021 00:09:37 +0000 (01:09 +0100)] 
nft-shared: Drop unused function print_proto()

The last users vanished back in 2013. There is identical code in
save_rule_details(), but with only a single user there's not much point
in keeping the function.

Fixes: cdc78b1d6bd7b ("nft: convert rule into a command state structure")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share print_header() with legacy iptables
Phil Sutter [Tue, 17 Nov 2020 01:49:32 +0000 (02:49 +0100)] 
xshared: Share print_header() with legacy iptables

Legacy iptables fetches the relevant data via libiptc before calling the
shared routine which merely prints data as requested.

Drop the 'basechain' parameter, instead make sure a policy name is
passed only with base chains. Since the function is not shared with
ebtables (which uses a very rudimental header instead), this is safe.

In order to support legacy iptables' checking of iptc_get_references()
return code (printing an error message instead of the reference count),
make refs parameter signed and print the error message if it's negative.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share print_fragment() with legacy
Phil Sutter [Tue, 22 Oct 2019 18:06:11 +0000 (20:06 +0200)] 
xshared: Share print_fragment() with legacy

Also add a fake mode to make it suitable for ip6tables. This is required
because IPT_F_FRAG value clashes with IP6T_F_PROTO, so ip6tables rules
might seem to have IPT_F_FRAG bit set.

While being at it, drop the local variable 'flags' from
print_firewall().

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share print_rule_details() with legacy
Phil Sutter [Fri, 5 Nov 2021 21:02:45 +0000 (22:02 +0100)] 
xshared: Share print_rule_details() with legacy

Have to pass pointer to counters directly since different fields are
being used for some reason.

Since proto_to_name() is not used outside of xshared.c anymore, make it
static.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share save_ipv{4,6}_addr() with legacy
Phil Sutter [Fri, 5 Nov 2021 17:50:21 +0000 (18:50 +0100)] 
xshared: Share save_ipv{4,6}_addr() with legacy

While being at it, make save_ipv4_addr() accept an in_addr* as mask -
mask_to_str() needs it anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share save_rule_details() with legacy
Phil Sutter [Fri, 5 Nov 2021 17:27:53 +0000 (18:27 +0100)] 
xshared: Share save_rule_details() with legacy

The function combines printing of input and output interfaces and
protocol parameter, all being IP family independent. Extend the function
to print fragment option ('-f'), too if requested. While being at it,
drop unused iptables_command_state parameter and reorder the remaining
ones a bit.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Share print_iface() function
Phil Sutter [Fri, 5 Nov 2021 17:02:13 +0000 (18:02 +0100)] 
xshared: Share print_iface() function

Merge the three identical copies into one and name it 'save_iface' (as
the printed syntax is for "save"-format). Leave arptables alone for now,
its rather complicated whitespace printing doesn't allow for use of the
shared function. Also keep ebtables' custom implementation, it is used
for the --logical-in/--logical-out long-options, too. Apart from that,
ebtables-nft does not use a mask, at all.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Change whitespace printing in save_rule callback
Phil Sutter [Fri, 5 Nov 2021 16:45:48 +0000 (17:45 +0100)] 
nft: Change whitespace printing in save_rule callback

This aligns whitespace printing with legacy iptables' print_rule4() in
order to prepare for further code-sharing.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxshared: Merge and share parse_chain()
Phil Sutter [Fri, 5 Apr 2019 11:21:19 +0000 (13:21 +0200)] 
xshared: Merge and share parse_chain()

Have a common routine to perform chain name checks, combining all
variants' requirements.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoextensions: hashlimit: Fix tests with HZ=1000
Phil Sutter [Mon, 8 Nov 2021 16:03:21 +0000 (17:03 +0100)] 
extensions: hashlimit: Fix tests with HZ=1000

In an attempt to fix for failing hashlimit tests with HZ=100, the
expected failures were changed so they are expected to pass and the
parameters changed to seemingly fix them. Yet while the new parameters
worked on HZ=100 systems, with higher tick rates they didn't so the
observed problem moved from the test failing on HZ=100 to failing on
HZ=1000 instead.

Kernel's error message "try lower: 864000000/5" turned out to be a red
herring: The burst value does not act as a dividor but a multiplier
instead, so in order to lower the overflow-checked value, a lower burst
value must be chosen. Inded, using a burst value of 1 makes the kernel
accept the rule in both HZ=100 and HZ=1000 configurations.

Fixes: bef9dc575625a ("extensions: hashlimit: Fix tests with HZ=100")
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxlate-test: Print full path if testing all files
Phil Sutter [Sat, 6 Nov 2021 20:10:45 +0000 (21:10 +0100)] 
xlate-test: Print full path if testing all files

Lines won't become too long and it's more clear to users where test
input comes from this way.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Tested-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoUnbreak xtables-translate
Phil Sutter [Sat, 6 Nov 2021 20:38:14 +0000 (21:38 +0100)] 
Unbreak xtables-translate

Fixed commit broke xtables-translate which still relied upon do_parse()
to properly initialize the passed iptables_command_state reference. To
allow for callers to preset fields, this doesn't happen anymore so
do_command_xlate() has to initialize itself. Otherwise garbage from
stack is read leading to segfaults and program aborts.

Although init_cs callback is used by arptables only and
arptables-translate has not been implemented, do call it if set just to
avoid future issues.

Fixes: cfdda18044d81 ("nft-shared: Introduce init_cs family ops callback")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Tested-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agonft: Merge xtables-arp-standalone.c into xtables-standalone.c
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
nft: Merge xtables-arp-standalone.c into xtables-standalone.c

By declaring the relevant family_ops callbacks for arptables, the code
becomes ready to just use do_commandx() instead of a dedicated parser.

As a side-effect, this enables a bunch of new features in arptables-nft:

* Support '-C' command
* Support '-S' command
* Support rule indexes just like xtables, e.g. in '-I' or '-R' commands
* Reject chain names starting with '!'
* Support '-c N,M' counter syntax

Since arptables still accepts intrapositioned negations, add code to
cover that but print a warning like iptables did 12 years ago prior to
removing the functionality.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: arptables accepts empty interface names
Phil Sutter [Sat, 14 Nov 2020 14:22:09 +0000 (15:22 +0100)] 
xtables: arptables accepts empty interface names

The empty string passed as interface name is simply ignored by legacy
arptables. Make the new common parser print a warning but accept it.
Calling xtables_parse_interface() with an empty string is safe.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Derive xtables_globals from family
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
xtables: Derive xtables_globals from family

Prepare xtables_main() for use with other families than IPV4 or IPV6
which both use the same xtables_globals object. Therefore introduce a
function to map from family value to xtables_globals object pointer.

In do_parse(), use xt_params pointer as well instead of direct
reference.

While being at it, Declare arptables_globals and ebtables_globals in
xtables_multi.h which seems to be the proper place for that.

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

Of course there is no such thing as *_tables_names for ebtables, so no
legacy tables checking for ebtables-nft.

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

This accomplishes the same tasks as e.g. nft_ipv4_post_parse() plus some
arptables-specific bits.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoarptables: Use standard data structures when parsing
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
arptables: Use standard data structures when parsing

Use the compound data structures introduced for dedicated parsing
routines in other families instead of the many local variables. This
allows to standardize code a bit for sharing a common parser later.

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

With optstring being stored in struct xtables_globals as well, it is a
natural choice to store a pointer to a help printer also which matches
the supported options.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables-standalone: Drop version number from init errors
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
xtables-standalone: Drop version number from init errors

Aside from the rather unconventional formatting, if those initialization
functions fail we've either released a completely broken iptables or
the wrong libraries are chosen by the loader. In both cases, the version
number is not really interesting.

While being at it, fix indenting of the first exit() call.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agonft: Add family ops callbacks wrapping different nft_cmd_* functions
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
nft: Add family ops callbacks wrapping different nft_cmd_* functions

Commands supporting multiple source/destination addresses need to
iterate over them and call the respective nft_cmd_* function multiple
times. These loops are family-specific though as each family uses a
different data structure within struct iptables_command_state to store
the addresses.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agoxtables: Simplify addr_mask freeing
Phil Sutter [Mon, 27 Sep 2021 14:59:49 +0000 (16:59 +0200)] 
xtables: Simplify addr_mask freeing

Introduce a generic 'ptr' union field to pass to free().

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

Arptables sets a few defaults in struct iptables_command_state upon
initialization. Introduce a callback to do that.

Signed-off-by: Phil Sutter <phil@nwl.cc>
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>