]> git.ipfire.org Git - thirdparty/iptables.git/log
thirdparty/iptables.git
6 years agoxtables: add 'printf' attribute to xlate_add
Florian Westphal [Mon, 12 Nov 2018 13:40:41 +0000 (14:40 +0100)] 
xtables: add 'printf' attribute to xlate_add

This allows gcc to check format string vs. passed arguments.
Fix the fallout from this as well, typical warning produced is:

libebt_mark_m.c:112:28: warning: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Wformat=]
   xt_xlate_add(xl, "and 0x%x %s0 ", info->mask, ...
                           ~^        ~~~~~~~~~~

so add the required casts or fixup format strings as needed.
libxt_conntrack also passed an unneeded argument (port), so remove that.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: xlate: init buffer to zero
Florian Westphal [Mon, 12 Nov 2018 17:04:45 +0000 (18:04 +0100)] 
libxtables: xlate: init buffer to zero

Doesn't affect iptables-xlate, but nft (when built w. xtables support).

Without this, nft can print random content if an extension doesn't
add anything to the output xlate buffer, e.g.

-p mh -m mh

can cause nft to print random data after "meta l4proto mobility",
as mh ->xlate doesn't do anything in this case.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agotests: shell: fix expected arptables-save output
Florian Westphal [Mon, 5 Nov 2018 16:51:18 +0000 (17:51 +0100)] 
tests: shell: fix expected arptables-save output

forgot to squash this before pushing arptables fixes.

Fixes: 5aecb2d8bfd ("arptables: pre-init hlen and ethertype")
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: fix --version info
Florian Westphal [Wed, 7 Nov 2018 09:32:42 +0000 (10:32 +0100)] 
arptables: fix --version info

old: arptables vlibxtables.so.12 (nf_tables)
now: arptables 1.8.1 (nf_tables)

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: ignore --table argument.
Florian Westphal [Wed, 7 Nov 2018 09:16:24 +0000 (10:16 +0100)] 
arptables: ignore --table argument.

You can run 'arptables-legacy -t foobar' and commands work fine,
as it still operates on filter table (the only table that exists).

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: make uni/multicast mac masks static
Florian Westphal [Tue, 6 Nov 2018 17:39:16 +0000 (18:39 +0100)] 
arptables: make uni/multicast mac masks static

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: add test cases
Florian Westphal [Mon, 5 Nov 2018 16:51:37 +0000 (17:51 +0100)] 
arptables: add test cases

Unicast being shown as '00:00:00:00:00:00/01:00:00:00:00:00' looks like
broken output, however, arptables classic did not pretty-print either.

Also add test cases for all targets supported by the original
arptables tool:

-j CLASSIFY
-j MARK
-j mangle

[ yes, mangle target is lower-case 8-( ]

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: pre-init hlen and ethertype
Florian Westphal [Mon, 5 Nov 2018 16:51:18 +0000 (17:51 +0100)] 
arptables: pre-init hlen and ethertype

to check -s 1.2.3.4, we need to add the size of the hardware address
to the arp header to obtain the offset where the ipv4 address begins:

base_arphdr
HW_ADDR
IP_ADDR (src)
IP_ADDR (target)

In arptables-classic, the kernel will add dev->addr_len to the
arp header base address to obtain the correct location, but we cannot
do this in nf_tables, at least not at this time (we need a fixed offset
value).

code does:

  op = nft_invflags2cmp(fw->arp.invflags, ARPT_INV_TGTIP);
  add_addr(r, sizeof(struct arphdr) + fw->arp.arhln + ...

but if user did not provide "--h-length 6" argument, then this won't
work even for ethernet, as the payload expression will be told to load
the first 4 bytes of arp header source mac address (sender hw address).

Fix this by pre-initialising arhlen to 6.
We also need to set up arhrd.  Otherwise, src/dst mac can't be used:

arptables -A INPUT -i lo --destination-mac 11:22:33:44:55:66
arptables v1.8.1 (nf_tables):  RULE_APPEND failed (Invalid argument): rule in chain INPUT

This means that matching won't work for AX25, NETROM etc, however,
arptables "classic" can't  parse non-ethernet addresses, and makes
ETH_ALEN assumptions in several spots, so this should be fine from
compatibility point of view.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: fix src/dst mac handling
Florian Westphal [Tue, 6 Nov 2018 16:52:10 +0000 (17:52 +0100)] 
arptables: fix src/dst mac handling

1. check both address and mask, not just first byte of mac
2. use add_addr() for this so mask is also handled via bitwise expr.
3. use the correct offsets.
4. add dissector so we can reverse translate the payload expressions
   generated for this.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: fix target ip offset
Florian Westphal [Tue, 6 Nov 2018 16:48:24 +0000 (17:48 +0100)] 
arptables: fix target ip offset

--dst-ip checks the first four octets of the target mac.

Format of ipv4 arp is:
arphdr (htype, ptype...)
src mac
src ip
target mac
target ip

So we need to add hlen (6 bytes) a second time
(arphdr + 6 + 4 + 6) to get correct offset.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: fix -s/-d handling for negation and mask
Florian Westphal [Mon, 5 Nov 2018 16:05:12 +0000 (17:05 +0100)] 
arptables: fix -s/-d handling for negation and mask

also handle negations in other cases.
Still to be resolved: mask handling for other options such as hlen.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: add basic test infra for arptables-nft
Florian Westphal [Mon, 5 Nov 2018 16:03:07 +0000 (17:03 +0100)] 
arptables: add basic test infra for arptables-nft

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: fix rule deletion/compare
Florian Westphal [Mon, 5 Nov 2018 16:01:36 +0000 (17:01 +0100)] 
arptables: fix rule deletion/compare

arptables -D fails most of the time, as we compared
source mask with target mask.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: remove code that is also commented-out in original arptables
Florian Westphal [Mon, 5 Nov 2018 22:53:31 +0000 (23:53 +0100)] 
arptables: remove code that is also commented-out in original arptables

This isn't a missing feature in the -nft version,
neither plen and -m were ever implemented in arptables-legacy.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables-save: add -c option, like xtables-save
Florian Westphal [Mon, 5 Nov 2018 12:38:08 +0000 (13:38 +0100)] 
arptables-save: add -c option, like xtables-save

arptables classic doesn't have arptables-save, it only has a perl
script that attempts to emulate iptables-save.  It supports no options,
and thus has no way to dump counters.  Add -c option, like iptables to
enable this.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: use ->save for arptables-save, like xtables
Florian Westphal [Wed, 7 Nov 2018 12:57:16 +0000 (13:57 +0100)] 
arptables: use ->save for arptables-save, like xtables

arptables-save will show
-A OUTPUT --h-length 6 --h-type 1 -j MARK --set-xmark 0x1/0xffffffff
as
--h-length 6 --h-type Ethernet -j MARK MARK set 0x1

Because it uses ->print() instead of ->save().
Switch it to use ->save, we can then also drop special handling of
CLASSIFY target.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: test protocol and interface negation
Florian Westphal [Mon, 5 Nov 2018 17:58:42 +0000 (18:58 +0100)] 
extensions: test protocol and interface negation

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix error return code in nft_chain_user_rename()
Phil Sutter [Mon, 12 Nov 2018 13:29:47 +0000 (14:29 +0100)] 
xtables: Fix error return code in nft_chain_user_rename()

If the chain to rename wasn't found, the function would return -1 which
got interpreted as success.

Signed-off-by: Phil Sutter <phil@nwl.cc>
6 years agoxtables: Clarify error message when deleting by index
Phil Sutter [Mon, 12 Nov 2018 13:29:46 +0000 (14:29 +0100)] 
xtables: Clarify error message when deleting by index

Trying to delete a rule by index from a non-existent chain leads to a
somewhat confusing error message:

| # iptables-nft -D foobar 1
| iptables: Index of deletion too big.

Fix this by performing chain existence checks for CMD_DELETE_NUM, too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
6 years agoxtables: Fix typo in do_command() error message
Phil Sutter [Mon, 12 Nov 2018 13:29:45 +0000 (14:29 +0100)] 
xtables: Fix typo in do_command() error message

This checks p->chain for existence, not cs->jumpto. Fixes this bogus
error message:

| # iptables-nft -t nat -A FORWARD -j ACCEPT
| iptables v1.8.1 (nf_tables): Chain 'ACCEPT' does not exist

Fixes: b6a06c1a215f8 ("xtables: Align return codes with legacy iptables")
Signed-off-by: Phil Sutter <phil@nwl.cc>
6 years agoebtables: use extrapositioned negation consistently
Florian Westphal [Mon, 12 Nov 2018 11:49:11 +0000 (12:49 +0100)] 
ebtables: use extrapositioned negation consistently

in the iptables universe, we enforce extrapositioned negation:

! -i foo

"-i ! foo" is not even supported anymore.

At least make sure that ebtables prints the former syntax everywhere as
well so we don't have a mix of both ways.
Parsing of --option ! 42 will still work for backwards compat reasons.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables-save: add -c option, using xtables-style counters
Florian Westphal [Mon, 12 Nov 2018 11:49:10 +0000 (12:49 +0100)] 
ebtables-save: add -c option, using xtables-style counters

The 'original' ebtables-save was a perl script that supported no option.
Add minimal options, like ip(6)tables save.

Retain the old way of formatiing counters via environment variable,
but allow overriding this using the -c option.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft: add NFT_TABLE_* enumeration
Pablo Neira Ayuso [Mon, 12 Nov 2018 11:44:56 +0000 (12:44 +0100)] 
nft: add NFT_TABLE_* enumeration

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft: replace nft_chain_dump() by nft_chain_list_get()
Pablo Neira Ayuso [Mon, 12 Nov 2018 11:03:57 +0000 (12:03 +0100)] 
nft: replace nft_chain_dump() by nft_chain_list_get()

So we can remove nft_chain_dump() and replace nftnl_chain_get_list().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoiptables-nft: fix -f fragment option
Florian Westphal [Sun, 11 Nov 2018 21:02:39 +0000 (22:02 +0100)] 
iptables-nft: fix -f fragment option

This needs to be passed in network byte order.

Reported-by: Arno van Amersfoort <arnova@rocky.eld.leidenuniv.nl>
Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1292
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: add and use mac print helpers
Florian Westphal [Sat, 3 Nov 2018 22:43:49 +0000 (23:43 +0100)] 
libxtables: add and use mac print helpers

This changes ebtables-nft to consistently print mac
address with two characters, i.e.
00:01:02:03:04:0a, not 0:1:2:3:4:a.

Will require another bump of vcurrent/vage.

Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: libebt_ip: fix tos negation
Florian Westphal [Mon, 5 Nov 2018 10:46:02 +0000 (11:46 +0100)] 
extensions: libebt_ip: fix tos negation

passing ->tos as uintmax_t will clear adjacent fields in the structure,
including invflags.

Fixes: 49479aa12a15 ("ebtables-compat: add 'ip' match extension")
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: libebt_ip6: fix ip6-dport negation
Florian Westphal [Mon, 5 Nov 2018 10:30:57 +0000 (11:30 +0100)] 
extensions: libebt_ip6: fix ip6-dport negation

Fixes: 5c8ce9c6aede0 ("ebtables-compat: add 'ip6' match extension")
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables-nft: make -Z option work
Florian Westphal [Mon, 5 Nov 2018 09:44:20 +0000 (10:44 +0100)] 
xtables-nft: make -Z option work

-Z doesn't just zero base counters, it zeroes out all rule
counters, or, optionally, all counters of a chain (-Z FOO).

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1286
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft: add missing error string
Florian Westphal [Sat, 3 Nov 2018 22:45:13 +0000 (23:45 +0100)] 
nft: add missing error string

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables-tests: add % to run iptables commands
Pablo Neira Ayuso [Sat, 3 Nov 2018 13:40:26 +0000 (14:40 +0100)] 
iptables-tests: add % to run iptables commands

Lines starting by % allows you to run iptables commands, use it for
rateest test.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoiptables-tests: do not append xtables-multi to external commands
Pablo Neira Ayuso [Sat, 3 Nov 2018 13:40:26 +0000 (14:40 +0100)] 
iptables-tests: do not append xtables-multi to external commands

Lines starting by @ can be used to invoke an external command of any
kind. Do not add xtables-multi here since we may want to execute a
non-iptables command.

Fixes: 9ff99156b63e ("iptables-test: fix netns test")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoebtables-nft: add arpreply target
Florian Westphal [Tue, 9 Oct 2018 15:21:37 +0000 (17:21 +0200)] 
ebtables-nft: add arpreply target

Unfortunately no nft translation available so far.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: add redirect test case
Florian Westphal [Fri, 2 Nov 2018 15:19:42 +0000 (16:19 +0100)] 
ebtables: add redirect test case

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: add test cases
Florian Westphal [Fri, 2 Nov 2018 15:19:20 +0000 (16:19 +0100)] 
ebtables: add test cases

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: relax -t table restriction, add snat/dnat test cases
Florian Westphal [Fri, 2 Nov 2018 15:24:24 +0000 (16:24 +0100)] 
ebtables: relax -t table restriction, add snat/dnat test cases

Its artificial and prevents test cases that need to add rules
to a different table from working.

The test script generates:
-A PREROUTING -t nat

... which works fine for iptables and ip6tables.
Just accept it for ebtables too and add test cases
for snat and dnat.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: fix -j CONTINUE handling for add/delete
Florian Westphal [Fri, 2 Nov 2018 13:36:54 +0000 (14:36 +0100)] 
ebtables: fix -j CONTINUE handling for add/delete

-j CONTINUE can be added, but it can't be removed:
extensions/libebt_standard.t: ERROR: line 5 (cannot find: ebtables -I INPUT -d de:ad:be:ef:00:00 -j CONTINUE)

This problem stems from silly ambiguity in ebtables-nft vs. iptables.

In iptables, you can do
iptables -A INPUT
(no -j)
in ebtables, you can do either
ebtables -A INPUT
or
ebtables -A INPUT -j CONTINUE

both are *supposed* to be the same (and they do the same even
in ebtables-nft on netlink side).

However, the temprary binary representation within ebtables-nft is not
the same: when parsing -j CONTINUE, we add a standard target, then omit
it later in _add_target().

When translating netlink representation to ebt binary one,
we do not add a standard target and instead just print '-j CONTINUE'
when listing rules.

So when doing
-I INPUT -j CONTINUE
-D INPUT -j CONTINUE

the -D operation fails because it has a standard target in the binary
representation, whereas the rule we obtained from translating
nftables netlink back to ebtables' binary represenation doesn't.

Fix it by ignoring 'CONTINUE' on parser side.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agotests: add basic ebtables test support
Florian Westphal [Fri, 2 Nov 2018 11:06:30 +0000 (12:06 +0100)] 
tests: add basic ebtables test support

now that we have ebtables-save, lets add test cases for ebtables-nft
as well.

Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables-nft: fix bogus handling of zero saddr/daddr
Florian Westphal [Fri, 2 Nov 2018 09:47:25 +0000 (10:47 +0100)] 
iptables-nft: fix bogus handling of zero saddr/daddr

rule for 0.0.0.0/8 is added as 0.0.0.0/0, because we did not check
mask (or negation, for that matter).

Fix this and add test cases too.

This also revealed an ip6tables-nft-save bug, it would print
' !-d', not '! -d'.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1287
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables-test: fix netns test
Taehee Yoo [Thu, 1 Nov 2018 14:32:50 +0000 (23:32 +0900)] 
iptables-test: fix netns test

The libxt_rateest test always fails because dependent command is not
executed in netns.

(@iptables -I INPUT -j RATEEST --rateest-name RE1 --rateest-interval \
 250.0ms --rateest-ewmalog 500.0ms)

After this path, adding netns command is executed first.
Then test commands are executed.

Fixes: 0123183f43a9 ("iptables-test: add -N option to exercise netns removal path")
Reported-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Taehee Yoo <ap420073@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables: Fix for matching rules with wildcard interfaces
Phil Sutter [Wed, 31 Oct 2018 19:13:34 +0000 (20:13 +0100)] 
xtables: Fix for matching rules with wildcard interfaces

Due to xtables_parse_interface() and parse_ifname() being misaligned
regarding interface mask setting, rules containing a wildcard interface
added with iptables-nft could neither be checked nor deleted.

As suggested, introduce extensions/iptables.t to hold checks for
built-in selectors. This file is picked up by iptables-test.py as-is.
The only limitation is that iptables is being used for it, so no
ip6tables-specific things can be tested with it (for now).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: limit: unbreak build without libnftnl
Florian Westphal [Wed, 24 Oct 2018 10:00:11 +0000 (12:00 +0200)] 
extensions: limit: unbreak build without libnftnl

Lars Wendler reported 1.8.1 build failure when trying to build without nft backend:

  In file included from ../iptables/nft.h:5, from libxt_limit.c:18: libnftnl/rule.h: No such file or directory

Reported-by: Lars Wendler <polynomial-c@gentoo.org>
Fixes: 02b80972c43 ("ebtables: Merge libebt_limit.c into libxt_limit.c")
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix for spurious errors from iptables-translate
Phil Sutter [Tue, 23 Oct 2018 14:59:14 +0000 (16:59 +0200)] 
xtables: Fix for spurious errors from iptables-translate

When aligning iptables-nft error messages with legacy ones, I missed
that translate tools shouldn't check for missing or duplicated chains.

Introduce a boolean in struct nft_xt_cmd_parse indicating we're "just"
translating and do_parse() should skip the checks.

Fixes: b6a06c1a215f8 ("xtables: Align return codes with legacy iptables")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoconfigure: bump versions for 1.8.1 release v1.8.1
Florian Westphal [Mon, 22 Oct 2018 16:48:53 +0000 (18:48 +0200)] 
configure: bump versions for 1.8.1 release

this release also adds xtables_getether* functions to libxtables, so
current and age are incremented as well.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoiptables-test: add -N option to exercise netns removal path
Pablo Neira Ayuso [Fri, 19 Oct 2018 10:13:37 +0000 (12:13 +0200)] 
iptables-test: add -N option to exercise netns removal path

We are getting bug reports lately from the netns path, add a new option
to exercise this path.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agolibxtables: expose new etherdb lookup function through libxtables API
Pablo Neira Ayuso [Fri, 19 Oct 2018 11:06:53 +0000 (13:06 +0200)] 
libxtables: expose new etherdb lookup function through libxtables API

This is used from extensions and included in libxtables, so we have to
make them public.

Fixes: 31f1434dfe37 ("libxtables: Integrate getethertype.c from xtables core")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Phil Sutter <phil@nwl.cc>
6 years agolibxtables: prefix exported new functions for etherdb lookups
Pablo Neira Ayuso [Fri, 19 Oct 2018 10:55:16 +0000 (12:55 +0200)] 
libxtables: prefix exported new functions for etherdb lookups

To avoid symbol pollution, place them under the xt_ and xtables_ prefix
name.

Fixes: 31f1434dfe37 ("libxtables: Integrate getethertype.c from xtables core")
Reported-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Acked-by: Phil Sutter <phil@nwl.cc>
6 years agoRevert "extensions: libxt_quota: Allow setting the remaining quota"
Pablo Neira Ayuso [Fri, 19 Oct 2018 09:48:59 +0000 (11:48 +0200)] 
Revert "extensions: libxt_quota: Allow setting the remaining quota"

This reverts commit 0a8f2bcadff157489a737f8cc8846adcb750b91f.

Google folks are reporting some issues with 32-bits arch, let's revert
this until we have a new version for this.

6 years agoxtables: Remove target_maxnamelen field
Phil Sutter [Thu, 11 Oct 2018 11:30:38 +0000 (13:30 +0200)] 
xtables: Remove target_maxnamelen field

This is a partial revert of commit 9f075031a1973 ("Combine
parse_target() and command_jump() implementations"): Upstream prefers to
reduce max chain name length of arptables by two characters instead of
the introduced struct xtables_globals field which requires to bump
library API version.

Fixes: 9f075031a1973 ("Combine parse_target() and command_jump() implementations")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: cgroup: fix option parsing for v2
Pablo Neira Ayuso [Tue, 9 Oct 2018 18:36:36 +0000 (20:36 +0200)] 
extensions: cgroup: fix option parsing for v2

Structure layout is different, therefore a new struct xt_option_entry is
needed.

Fixes: f9efc8cb79c0 ("extensions: add cgroup revision 2")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: libxt_quota: Allow setting the remaining quota
Chenbo Feng [Tue, 2 Oct 2018 01:23:07 +0000 (18:23 -0700)] 
extensions: libxt_quota: Allow setting the remaining quota

The current xt_quota module cannot track the current remaining quota
of a specific rule. Everytime an unrelated rule is updated in the same
iptables table, the quota will be reset. This is not a very useful
function for iptables that get changed at run time. This patch fixes the
above problem by adding a new field in the struct that records the
current remaining quota.

Fixed a print out bug in verbose print out wrt. inversion.

Signed-off-by: Chenbo Feng <fengc@google.com>
Suggested-by: Maciej Żenczykowski <maze@google.com>
Reviewed-by: Maciej Żenczykowski <maze@google.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agonft-shared: Use xtables_calloc()
Phil Sutter [Mon, 24 Sep 2018 17:25:26 +0000 (19:25 +0200)] 
nft-shared: Use xtables_calloc()

This simplifies code a bit since it takes care of checking for
out-of-memory conditions.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: Use the shared nft_ipv46_parse_target()
Phil Sutter [Mon, 24 Sep 2018 17:25:25 +0000 (19:25 +0200)] 
arptables: Use the shared nft_ipv46_parse_target()

No point in having a dedicated implementation for 'parse_target'
callback since it is identical with the shared one.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoCombine parse_target() and command_jump() implementations
Phil Sutter [Mon, 24 Sep 2018 17:25:24 +0000 (19:25 +0200)] 
Combine parse_target() and command_jump() implementations

Merge these two functions from xtables, iptables, ip6tables and
arptables. Both functions were basically identical in the first three,
only the last one required a bit more attention.

To eliminate access to 'invflags' in variant-specific location, move the
call to set_option() into callers. This is actually consistent with
parsing of other options in them.

As with command_match(), use xt_params instead of the different
*_globals objects to refer to 'opts' and 'orig_opts'.

It was necessary to rename parse_target() as it otherwise clashes with a
static function of same name in libxt_SET.

In arptables, the maximum allowed target name is a bit larger, so
introduce xtables_globals.target_maxnamelen defining the value. It is
used in the shared xt_parse_target() implementation.

Implementation of command_jump() in arptables diverted from the others
for no obvious reason. The call to parse_target() was done outside of it
and a pointer to cs->arp was passed but not used inside.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoCombine command_match() implementations
Phil Sutter [Mon, 24 Sep 2018 17:25:23 +0000 (19:25 +0200)] 
Combine command_match() implementations

This merges the basically identical implementations of command_match()
from xtables, iptables and ip6tables into one. The only required
adjustment was to make use of xt_params instead of the different
*_globals objects.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibiptc: NULL-terminate errorname
Phil Sutter [Mon, 24 Sep 2018 17:25:22 +0000 (19:25 +0200)] 
libiptc: NULL-terminate errorname

In struct chain_head, field 'name' is of size TABLE_MAXNAMELEN, hence
copying its content into 'error_name' field of struct xt_error_target
which is two bytes shorter may overflow. Make sure this doesn't happen
by using strncpy() and set the last byte to zero.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Check extension real_name length
Phil Sutter [Mon, 24 Sep 2018 17:25:21 +0000 (19:25 +0200)] 
libxtables: Check extension real_name length

Just like with 'name', if given check 'real_name' to not exceed max length.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables: Gitignore xtables-{legacy, nft}-multi scripts
Phil Sutter [Wed, 19 Sep 2018 13:17:09 +0000 (15:17 +0200)] 
iptables: Gitignore xtables-{legacy, nft}-multi scripts

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Drop pointless check
Phil Sutter [Wed, 19 Sep 2018 13:17:08 +0000 (15:17 +0200)] 
xtables: Drop pointless check

All commands this block handles set p->chain. Also the pointer is
dereferenced before, so no point in checking for it to be non-NULL.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoarptables: Fix incorrect strcmp() in nft_arp_rule_find()
Phil Sutter [Wed, 19 Sep 2018 13:17:07 +0000 (15:17 +0200)] 
arptables: Fix incorrect strcmp() in nft_arp_rule_find()

Since nft_arp_rule_to_cs() may not set cs->jumpto, later call to
strcmp() may be passed a NULL pointer. Therefore check if the pointer is
valid before doing so.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Don't read garbage in nft_ipv4_parse_payload()
Phil Sutter [Wed, 19 Sep 2018 13:17:06 +0000 (15:17 +0200)] 
xtables: Don't read garbage in nft_ipv4_parse_payload()

The problem here is that get_frag() does not set 'inv' in any case, so
when later checking its value, garbage may be read. Sanitize this case
by setting 'inv' to false before calling get_frag().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Use posix_spawn() instead of vfork()
Phil Sutter [Wed, 19 Sep 2018 13:17:05 +0000 (15:17 +0200)] 
libxtables: Use posix_spawn() instead of vfork()

According to covscan, vfork() may lead to a deadlock in the parent
process. It suggests to use posix_spawn() instead. Since the latter
combines vfork() and exec() calls, use it for xtables_insmod().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoFix a few cases of pointless assignments
Phil Sutter [Wed, 19 Sep 2018 13:17:04 +0000 (15:17 +0200)] 
Fix a few cases of pointless assignments

This gets rid of a number of assignments which are either redundant or
not used afterwards.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoextensions: libebt_ip{, 6}: Drop pointless error checking
Phil Sutter [Wed, 19 Sep 2018 13:17:03 +0000 (15:17 +0200)] 
extensions: libebt_ip{, 6}: Drop pointless error checking

Since info->protocol is of type __u8, its value will never become -1.
Apart from that, xtables_parse_protocol() calls xt_params->exit_err() in
case of error, so this code is dead anyway.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonft-arp: Drop ineffective conditional
Phil Sutter [Wed, 19 Sep 2018 13:17:02 +0000 (15:17 +0200)] 
nft-arp: Drop ineffective conditional

Since fw->arp.arhln is of type __u8, its value will never become less
than zero.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables: Use print_ifaces() from xtables
Phil Sutter [Wed, 19 Sep 2018 13:17:00 +0000 (15:17 +0200)] 
iptables: Use print_ifaces() from xtables

Move the function to xshared.c for common use between legacy and xtables
sources. While being at it, silence a covscan warning triggered by that
function as it couldn't verify input buffers won't exceed IFNAMSIZ.
Therefore use snprintf() when writing to the local buffer.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoShare print_ipv{4,6}_addr() from xtables
Phil Sutter [Wed, 19 Sep 2018 13:16:59 +0000 (15:16 +0200)] 
Share print_ipv{4,6}_addr() from xtables

These functions contain code which occurs in legacy's print_firewall()
functions, so use them there.

Rename them to at least make clear they print more than a single
address.

Also introduce ipv{4,6}_addr_to_string() which take care of converting
an address/netmask pair into string representation in a way which
doesn't upset covscan (since that didn't detect that 'buf' may not be
exceeded by the strings written into it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables-apply: Replace signal numbers by names
Phil Sutter [Wed, 19 Sep 2018 13:16:58 +0000 (15:16 +0200)] 
iptables-apply: Replace signal numbers by names

As covscan stated: "Trapping signals by number is not well defined.
Prefer signal names."

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoiptables-apply: Quote strings passed to echo
Phil Sutter [Wed, 19 Sep 2018 13:16:57 +0000 (15:16 +0200)] 
iptables-apply: Quote strings passed to echo

Not a real problem here, but covscan got confused by one string
containing 'then' keyword.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonfnl_osf: Replace deprecated nfnl_talk() by nfnl_query()
Phil Sutter [Wed, 19 Sep 2018 13:16:56 +0000 (15:16 +0200)] 
nfnl_osf: Replace deprecated nfnl_talk() by nfnl_query()

This eliminates the deprecation warning when compiling the sources.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Don't read garbage in xtables_strtoui()
Phil Sutter [Wed, 19 Sep 2018 13:16:55 +0000 (15:16 +0200)] 
libxtables: Don't read garbage in xtables_strtoui()

If xtables_strtoul() fails, it returns false and data pointed to by
parameter 'value' is undefined. Hence avoid copying that data in
xtables_strtoui() if the call failed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Avoid calling memcpy() with NULL source
Phil Sutter [Wed, 19 Sep 2018 13:16:54 +0000 (15:16 +0200)] 
libxtables: Avoid calling memcpy() with NULL source

Both affected functions check if 'oldopts' is NULL once but later seem
to ignore that possibility. To catch up on that, increment the pointer
only if it isn't NULL, also don't copy its content into the merged
options buffer in that case.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibiptc: Simplify alloc_handle() function signature
Phil Sutter [Wed, 19 Sep 2018 13:16:53 +0000 (15:16 +0200)] 
libiptc: Simplify alloc_handle() function signature

This change originated from covscan complaining about the strcpy() call
with an unknown size source buffer. But in fact, the size is known (and
equal to the destination size), so pass a pointer to STRUCT_GETINFO to
alloc_handle() instead of it's fields separately. Hopefully this will
silence covscan.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_time: Drop initialization of variable 'year'
Phil Sutter [Wed, 19 Sep 2018 13:16:52 +0000 (15:16 +0200)] 
libxt_time: Drop initialization of variable 'year'

The variable is not read before being assigned the return value of
strtoul(), thefore the initialization is useless. And since after this
change parameter 'end' becomes unused, drop it as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_ipvs: Avoid potential buffer overrun
Phil Sutter [Wed, 19 Sep 2018 13:16:51 +0000 (15:16 +0200)] 
libxt_ipvs: Avoid potential buffer overrun

Just like with libxt_conntrack, get rid of the temporary buffer. The
comment even states that it was copied from there, so just make them
identical again.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_conntrack: Avoid potential buffer overrun
Phil Sutter [Wed, 19 Sep 2018 13:16:50 +0000 (15:16 +0200)] 
libxt_conntrack: Avoid potential buffer overrun

In print_addr(), a resolved hostname is written into a buffer without
size check. Since BUFSIZ is typically 8192 bytes, this shouldn't be an
issue, though covscan complained about it. Fix the code by using
conntrack_dump_addr() as an example.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_conntrack: Version 0 does not support XT_CONNTRACK_DIRECTION
Phil Sutter [Wed, 19 Sep 2018 13:16:49 +0000 (15:16 +0200)] 
libxt_conntrack: Version 0 does not support XT_CONNTRACK_DIRECTION

Since sinfo->flags is only 8 bytes large, checking for
XT_CONNTRACK_DIRECTION bit (which has value 1 << 12) will always return
false, so drop this dead code.

Fixes: c7fc1dae1e8f8 ("libxt_conntrack: dump ctdir")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_LED: Avoid string overrun while parsing led-trigger-id
Phil Sutter [Wed, 19 Sep 2018 13:16:48 +0000 (15:16 +0200)] 
libxt_LED: Avoid string overrun while parsing led-trigger-id

Instead of using strcat() and assuming the name will fit, print into the
buffer using snprintf() which truncates the string as needed.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Remove unused variable in nft_is_table_compatible()
Phil Sutter [Wed, 19 Sep 2018 13:16:47 +0000 (15:16 +0200)] 
xtables: Remove unused variable in nft_is_table_compatible()

This is a leftover from previous cleanup.

Fixes: 098ee2e91756c ("xtables-save: Ignore uninteresting tables")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoip{, 6}tables-restore: Fix for uninitialized array 'curtable'
Phil Sutter [Wed, 19 Sep 2018 13:16:46 +0000 (15:16 +0200)] 
ip{, 6}tables-restore: Fix for uninitialized array 'curtable'

When reading sufficiently malformed input, parser might hit end of
loop without having written the current table name into curtable and
therefore calling strcmp() with uninitialized buffer. Avoid this by
setting curtable to zero upon declaration.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoMark fall through cases in switch() statements
Phil Sutter [Wed, 19 Sep 2018 13:16:45 +0000 (15:16 +0200)] 
Mark fall through cases in switch() statements

Typical covscan complaint, non-empty fall throughs should be marked as
such. There was but a single case which should break instead, namely in
libebt_log.c: It is not critical, since the next case merely asserts
'invert' being zero (which can't be as it was checked before). But while
being at it, introduce log_chk_inv() to consolidate the semantically
equal cases for the various log types.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Integrate getethertype.c from xtables core
Phil Sutter [Wed, 19 Sep 2018 13:16:44 +0000 (15:16 +0200)] 
libxtables: Integrate getethertype.c from xtables core

This moves getethertype.c into libxtables so that both extensions and
xtables-nft-multi may use the implementations therein. New users are
libebt_arp and libebt_vlan which drop their own duplicated
implementations of getethertypebyname() for the shared one.

This change originated from a covscan report of extensions'
implementations not checking fopen() return value which should be
implicitly fixed by this as well.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix for wrong assert() in __nft_table_flush()
Phil Sutter [Wed, 19 Sep 2018 13:16:43 +0000 (15:16 +0200)] 
xtables: Fix for wrong assert() in __nft_table_flush()

The code obviously tries to assert that nft_table_builtin_find()
returned a valid pointer before dereferencing it, but the wrong argument
was given. Assume this is just a typo and insert the missing underscore.

Fixes: 9b896224e0bfc ("xtables: rework rule cache logic")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agonfnl_osf: Drop pointless check in xt_osf_strchr()
Phil Sutter [Wed, 19 Sep 2018 13:16:42 +0000 (15:16 +0200)] 
nfnl_osf: Drop pointless check in xt_osf_strchr()

Although it remains unclear what the original intention behind the
affected code was, but 'tmp + 1' always evaluates true since 'tmp' is a
pointer value.

Cc: Evgeniy Polyakov <johnpol@2ka.mxt.ru>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_string: Fix array out of bounds check
Phil Sutter [Mon, 17 Sep 2018 11:38:33 +0000 (13:38 +0200)] 
libxt_string: Fix array out of bounds check

Commit 56d7ab42f3782 ("libxt_string: Avoid potential array out of bounds
access") tried to fix parse_hex_string() for overlong strings but the
change still allowed for 'sindex' to become XT_STRING_MAX_PATTERN_SIZE
which leads to access of first byte after info->pattern. This is not
really a problem because it merely overwrites info->patlen before
calling xtables_error() later, but covscan still detects it so it's
still worth fixing.

The crucial bit here is that 'sindex' has to be incremented at end of
the last iteration since its value is used for info->patlen. Hence just
move the overflow check to the beginning of the loop.

Fixes: 56d7ab42f3782 ("libxt_string: Avoid potential array out of bounds access")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoxtables-save: Ignore uninteresting tables
Phil Sutter [Mon, 10 Sep 2018 21:32:34 +0000 (23:32 +0200)] 
xtables-save: Ignore uninteresting tables

When running iptables-nft-save with other tables present, the dump
succeeded but the tool complained about those other tables. In an
environment where iptables-nft and nftables are uses in parallel, this
is an expected situation, so only complain about incompatible builtin
tables.

While being at it, move the table existence check from __do_output()
into do_output() since the former may be called from
nft_for_each_table() in which case the table is guaranteed to exist.

Also use nft_table_builtin_find() in nft_is_table_compatible() instead
of open-coding the search by name in h->tables.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: add cgroup revision 2
Pablo Neira Ayuso [Tue, 4 Sep 2018 09:49:15 +0000 (11:49 +0200)] 
extensions: add cgroup revision 2

Just like revision v1, but cgroup path field is smaller.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 years agoextensions: REJECT: Merge reject tables
Phil Sutter [Mon, 10 Sep 2018 21:35:17 +0000 (23:35 +0200)] 
extensions: REJECT: Merge reject tables

Initial motivation for this was a covscan report for potential array out
of bounds access in REJECT_xlate (a false-positive, because all possible
values of reject->with occur in reject_table_xlate).

Use reject types as array indices of reject_table so that reject->with
serves as array index. Also merge reject_table_xlate into it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxt_string: Avoid potential array out of bounds access
Phil Sutter [Mon, 10 Sep 2018 21:35:16 +0000 (23:35 +0200)] 
libxt_string: Avoid potential array out of bounds access

The pattern index variable 'sindex' is bounds checked before
incrementing it, which means in the next loop iteration it might already
match the bounds check condition but is used anyway.

Fix this by incrementing the index before performing the bounds check.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoebtables: Fix for potential array boundary overstep
Phil Sutter [Mon, 10 Sep 2018 21:35:15 +0000 (23:35 +0200)] 
ebtables: Fix for potential array boundary overstep

Fix the parameter check in nft_ebt_standard_target() to avoid an array
out of bounds access in ebt_standard_targets.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibiptc: Avoid side-effect in memset() calls
Phil Sutter [Mon, 10 Sep 2018 21:35:14 +0000 (23:35 +0200)] 
libiptc: Avoid side-effect in memset() calls

These calls to memset() are passed a length argument which exceeds
t->target.u.user.name's length by one byte and hence overwrite
t->target.u.user.revision as well (relying upon no padding to happen
between both).

Avoid this obscure behaviour by passing the correct field size and
explicitly overwriting 'revision' field.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agolibxtables: Fix potential array overrun in xtables_option_parse()
Phil Sutter [Mon, 10 Sep 2018 21:35:13 +0000 (23:35 +0200)] 
libxtables: Fix potential array overrun in xtables_option_parse()

If entry->type is to be used as array index, it needs to be at max one
less than that array's size.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Accept --wait in iptables-nft-restore
Phil Sutter [Wed, 5 Sep 2018 17:14:40 +0000 (19:14 +0200)] 
xtables: Accept --wait in iptables-nft-restore

Passing --wait option to iptables-nft-restore led to program abort
because the flag parameter was not skipped. Mimick iptables-restore
behaviour when encountering --wait or --wait-interval options (but still
ignore the parameter).

Fixes: b9d7b49d84bc2 ("xtables-compat: restore: sync options with iptables-restore")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Don't check all rules for being compatible
Phil Sutter [Fri, 7 Sep 2018 15:06:21 +0000 (17:06 +0200)] 
xtables: Don't check all rules for being compatible

Commit f8e29a13fed8d ("xtables: avoid bogus 'is incompatible' warning")
fixed for compatibility checking to extend over all chains, not just the
relevant ones. This patch does the same for rules: Make sure only rules
belonging to the relevant table are being considered.

Note that comparing the rule's table name is sufficient here since the
table family is already considered when populating the rule cache.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agodoc: Improve layout of u32 instructions
Joseph C. Sible [Sun, 2 Sep 2018 18:09:15 +0000 (14:09 -0400)] 
doc: Improve layout of u32 instructions

Make it more clear where the instruction ends, and where
what it does begins.

Signed-off-by: Joseph C. Sible <josephcsible@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables-restore: Fix flushing referenced custom chains
Phil Sutter [Thu, 6 Sep 2018 17:33:20 +0000 (19:33 +0200)] 
xtables-restore: Fix flushing referenced custom chains

The logic to replicate 'iptables-restore --noflush' behaviour of
flushing custom chains if listed in the dump was broken for chains being
referenced. A minimal dump reproducing the issue is:

| *filter
| :foobar - [0:0]
| -I INPUT -j foobar
| -A foobar -j ACCEPT
| COMMIT

With --noflush, this can be restored just once in iptables-nft-restore.
Consecutive attempts return an error since xtables tries to delete the
referenced chain and recreate it instead of performing a real flush.

Fix this by really flushing the custom chain in 'chain_user_flush'
callback and running 'chain_user_add' callback only if the chain doesn't
exist already.

Fixes: df3d92bec6007 ("xtables-compat-restore: flush user-defined chains with -n")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Drop use of IP6T_F_PROTO
Phil Sutter [Fri, 31 Aug 2018 20:30:58 +0000 (22:30 +0200)] 
xtables: Drop use of IP6T_F_PROTO

Setting this bit in cs->fw6.ipv6.flags was done only for rules parsed
from command line, not for those read from kernel. As a result,
appropriate rules could not be deleted. A simple test case is:

| # ip6tables-nft -A INPUT -p tcp -j ACCEPT
| # ip6tables-nft -D INPUT -p tcp -j ACCEPT
| iptables: Bad rule (does a matching rule exist in that chain?).

Since the flag is not used anywhere in xtables-nft, dropping its use fixes
the bug as well as setting it in both cases.

Fixes: 5ee03e6df4172 ("xtables: Use meta l4proto for -p match")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Align return codes with legacy iptables
Phil Sutter [Fri, 31 Aug 2018 10:29:57 +0000 (12:29 +0200)] 
xtables: Align return codes with legacy iptables

Make sure return codes match legacy ones at least for a few selected
commands typically used to check ruleset state.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoxtables: Fix for deleting rules with comment
Phil Sutter [Tue, 28 Aug 2018 19:44:16 +0000 (21:44 +0200)] 
xtables: Fix for deleting rules with comment

Comment match allocation in command_match() and
nft_rule_to_iptables_command_state() were misaligned in that the latter
set match_size to just what is required instead of what the match needs
at maximum like the further. This led to failure when comparing them
later and therefore a rule with a comment could not be deleted.

For comments of a specific length, the udata buffer is padded by
libnftnl so nftnl_rule_get_data() returns a length value which is larger
than the string (including NULL-byte). The trailing data is supposed to
be ignored, but compare_matches() can't not know about that detail and
therefore returns a false-negative if trailing data contains junk. To
overcome this, use strncpy() when populating match data in
nft_rule_to_iptables_command_state(). While being at it, make sure
comment match allocation in that function is identical to what
command_match() does with regards to data allocation size. Also use
xtables_calloc() which does the required error checking.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
6 years agoip6tables-translate: Fix libip6t_mh.txlate test
Phil Sutter [Thu, 23 Aug 2018 15:43:29 +0000 (17:43 +0200)] 
ip6tables-translate: Fix libip6t_mh.txlate test

Layer 4 protocol name "mobility-header" is not known by nft, so it's
neither printed nor accepted on input. Hence fix the test instead of
code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>