]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
4 weeks agopy: use setup.cfg to configure setuptools
Jeremy Sowden [Mon, 31 Jul 2023 11:40:23 +0000 (12:40 +0100)] 
py: use setup.cfg to configure setuptools

commit 8ae4dc1f40aa04e499d941faca45fe7e914f0b4d upstream.

Setuptools has had support for declarative configuration for several
years.  To quote their documentation:

  Setuptools allows using configuration files (usually setup.cfg) to
  define a package’s metadata and other options that are normally
  supplied to the setup() function (declarative config).

  This approach not only allows automation scenarios but also reduces
  boilerplate code in some cases.

Additionally, this allows us to introduce support for PEP-517-compatible
build-systems.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agopy: move package source into src directory
Jeremy Sowden [Mon, 31 Jul 2023 11:40:22 +0000 (12:40 +0100)] 
py: move package source into src directory

commit ce443afc214553b9fa6f02a640a3cd2f71a23ec9 upstream.

Separate the actual package source from the build files.  In addition
to being a bit tidier, this will prevent setup.py being erroneously
installed when we introduce PEP-517 support in a later commit.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agorule: allow src/dstnat prios in input and output
Florian Westphal [Fri, 28 Jul 2023 17:43:16 +0000 (19:43 +0200)] 
rule: allow src/dstnat prios in input and output

commit 8beafab74c391130fbb9111bfccab8613644e3b9 upstream.

Dan Winship says:

The "dnat" command is usable from either "prerouting" or "output", but the
"dstnat" priority is only usable from "prerouting". (Likewise, "snat" is usable
from either "postrouting" or "input", but "srcnat" is only usable from
"postrouting".)

No need to restrict those priorities to pre/postrouting.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1694
Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agopy: remove setup.py integration with autotools
Pablo Neira Ayuso [Tue, 18 Jul 2023 12:01:19 +0000 (14:01 +0200)] 
py: remove setup.py integration with autotools

commit b3def33efecb2f7be39fc9aefc9546907202056c upstream.

With Python distutils and setuptools going deprecated, remove
integration with autotools. This integration is causing issues
in modern environments.

Note that setup.py is still left in place under the py/ folder.

Update INSTALL file to refer to Python support and setup.py.

Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agoupdate INSTALL file
Pablo Neira Ayuso [Tue, 18 Jul 2023 11:42:59 +0000 (13:42 +0200)] 
update INSTALL file

commit 8e339bae3c9918b38bd72ddacf7765a12c1dcda9 upstream.

Update it to current library dependencies and existing options.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agomnl: set SO_SNDBUF before SO_SNDBUFFORCE
Pablo Neira Ayuso [Fri, 7 Apr 2023 22:21:57 +0000 (16:21 -0600)] 
mnl: set SO_SNDBUF before SO_SNDBUFFORCE

commit 375505a4a8068bf7cb623e18c3aedb831c17fd0e upstream.

Set SO_SNDBUF before SO_SNDBUFFORCE: Unpriviledged user namespace does
not have CAP_NET_ADMIN on the host (user_init_ns) namespace.

SO_SNDBUF always succeeds in Linux, always try SO_SNDBUFFORCE after it.

Moreover, suggest the user to bump socket limits if EMSGSIZE after
having see EPERM previously, when calling SO_SNDBUFFORCE.

Provide a hint to the user too:

 # nft -f test.nft
 netlink: Error: Could not process rule: Message too long
 Please, rise /proc/sys/net/core/wmem_max on the host namespace. Hint: 4194304 bytes

Dave Pfike says:

 Prior to this patch, nft inside a systemd-nspawn container was failing
 to install my ruleset (which includes a large-ish map), with the error

 netlink: Error: Could not process rule: Message too long

 strace reveals:

 setsockopt(3, SOL_SOCKET, SO_SNDBUFFORCE, [524288], 4) = -1 EPERM (Operation not permitted)

 This is despite the nspawn process supposedly having CAP_NET_ADMIN.

 A web search reveals at least one other user having the same issue:

 https://old.reddit.com/r/Proxmox/comments/scnoav/lxc_container_debian_11_nftables_geoblocking/

Reported-by: Dave Pifke <dave@pifke.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agopy: replace distutils with setuptools
Jose M. Guisado Gomez [Wed, 22 Feb 2023 10:20:55 +0000 (11:20 +0100)] 
py: replace distutils with setuptools

commit 1acc2fd48c755a8931fa87b8d0560b750316059f upstream.

Removes a deprecation warning when using distutils and python >=3.10.

Python distutils module is formally marked as deprecated since python
3.10 and will be removed from the standard library from Python 3.12.
(https://peps.python.org/pep-0632/)

From https://setuptools.pypa.io/en/latest/setuptools.html

"""
Packages built and distributed using setuptools look to the user like
ordinary Python packages based on the distutils.
"""

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agosrc: Add GPLv2+ header to .c files of recent creation
Pablo Neira Ayuso [Wed, 7 Dec 2022 15:08:15 +0000 (16:08 +0100)] 
src: Add GPLv2+ header to .c files of recent creation

commit 77fd4fa2827087dc00615137da78730500823259 upstream.

This patch comes after a proposal of mine at NFWS 2022 that resulted in
agreement to license recent .c files under GPLv2+ by the attendees at this
meeting:

- Stefano Brivio
- Fernando F. Mancera
- Phil Sutter
- Jozsef Kadlecsik
- Florian Westphal
- Laura Garcia
- Arturo Borrero
- Pablo Neira

It has already happened that one of the external library dependencies
was moved to GPLv3+ (libreadline), resulting in a change to libedit by
default in b4dded0ca78d ("configure: default to libedit for cli").

I have added the GPLv2+ header to the following files:

                        Authors
                        -------
src/cmd.c               Pablo
src/fib.c               Florian
src/hash.c              Pablo
src/iface.c             Pablo
src/json.c              Phil + fixes from occasional contributors
src/libnftables.c       Eric Leblond and Phil
src/mergesort.c         Elise Lenion
src/misspell.c          Pablo
src/mnl.c               Pablo + fixes from occasional contributors
src/monitor.c           Arturo
src/numgen.c            Pablo
src/osf.c               Fernando
src/owner.c             Pablo
src/parser_json.c       Phil + fixes from occasional contributors
src/print.c             Phil
src/xfrm.c              Florian
src/xt.c                Pablo

Eric Leblond and Elise Lennion did not attend NFWS 2022, but they
acknowledged this license update already in the past when I proposed
this to them in private emails.

Update COPYING file too to refer that we are now moving towards GPLv2 or
any later.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agosrc: netlink: netlink_delinearize_table() may return NULL
Phil Sutter [Fri, 8 Aug 2025 12:21:41 +0000 (14:21 +0200)] 
src: netlink: netlink_delinearize_table() may return NULL

commit a69d552a005ba467d37e225032e35d01d9491241 upstream.

Catch the error condition in callers to avoid crashes.

Fixes: c156232a530b3 ("src: add comment support when adding tables")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agosegtree: incorrect type when aggregating concatenated set ranges
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:19:23 +0000 (15:19 +0200)] 
segtree: incorrect type when aggregating concatenated set ranges

commit 87f23fe0357da8f951faebbe2fa0b306048c2394 upstream.

Uncovered by the compound_expr_remove() replacement by type safe function
coming after this patch.

Add expression to the concatenation which is reachable via expr_value().

This bug is subtle, I could not spot any reproducible buggy behaviour
when using the wrong type when running the existing tests.

Fixes: 8ac2f3b2fca3 ("src: Add support for concatenated set ranges")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agodoc: nft.8: Minor NAT STATEMENTS section review
Phil Sutter [Thu, 31 Jul 2025 10:40:11 +0000 (12:40 +0200)] 
doc: nft.8: Minor NAT STATEMENTS section review

commit 9e1cbf667da2b9c30b41ff887de212b2c38b2eb7 upstream.

Synopsis insinuates an IP address argument is mandatory in snat/dnat
statements although specifying ports alone is perfectly fine. Adjust it
accordingly and add a paragraph briefly describing the behaviour.

While at it, update the redirect statement description with more
relevant examples, the current one is wrong: To *only* alter the
destination port, dnat statement must be used, not redirect.

Fixes: 6908a677ba04c ("nft.8: Enhance NAT documentation")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agoparser_bison: fix memory leak when parsing flowtable hook declaration
Florian Westphal [Wed, 23 Jul 2025 15:00:11 +0000 (17:00 +0200)] 
parser_bison: fix memory leak when parsing flowtable hook declaration

commit 7265bf1252f66d5ca5b5dc4aa06df43f60f551a6 upstream.

When the hook location is invalid we error out but we do leak both
the priority expression and the flowtable name.  Example:

valgrind --leak-check=full nft -f flowtable-parser-err-memleak
[..] Error: unknown chain hook
hook enoent priority filter + 10
     ^^^^^^
[..]
2 bytes in 1 blocks are definitely lost in loss record 1 of 3
   at: malloc (vg_replace_malloc.c:446)
   by: strdup (in libc.so.6)
   by: xstrdup (in libnftables.so.1.1.0)
   by: nft_lex (in libnftables.so.1.1.0)
   by: nft_parse (in libnftables.so.1.1.0)
   by: __nft_run_cmd_from_filename (in libnftables.so.1.1.0)
   by: nft_run_cmd_from_filename (in libnftables.so.1.1.0)

First two reports are due to the priority expression: this needs to call
expr_free().  Third report is due to the flowtable name, the destructor
was missing so add one.

After fix:
All heap blocks were freed -- no leaks are possible

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: fix assert due to empty interface name
Florian Westphal [Mon, 21 Jul 2025 11:36:03 +0000 (13:36 +0200)] 
parser_json: fix assert due to empty interface name

commit 26f6ac378a49b3151a8c7e4bb0a94211b54708cc upstream.

Before:
nft: src/mnl.c:744: nft_dev_add: Assertion `ifname_len > 0' failed.

After:
internal:0:0-0: Error: empty interface name

Bison checks this upfront, do same in json.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: reject non-concat expression
Florian Westphal [Mon, 21 Jul 2025 11:09:55 +0000 (13:09 +0200)] 
parser_json: reject non-concat expression

commit f4d3e5e2f6595b6628b2aa948ff45ffaec40fb65 upstream.

Before "src: detach set, list and concatenation expression layout":
internal:0:0-0: Error: Concatenation with 0 elements is illegal

After this change, expr->size access triggers assert() failure, add
explicit test for etype to avoid this and error out:

internal:0:0-0: Error: Expected concat element, got symbol.

Fixes: e0d92243be1c ("src: detach set, list and concatenation expression layout")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: maps: check element data mapping matches set data definition
Florian Westphal [Mon, 21 Jul 2025 10:57:07 +0000 (12:57 +0200)] 
evaluate: maps: check element data mapping matches set data definition

commit bc1eeb8fe709b2c0322a6b0e447517256cc9c18b upstream.

This change is similar to
7f4d7fef31bd ("evaluate: check element key vs. set definition")

but this time for data mappings.

The included bogon asserts with:
BUG: invalid data expression type catch-all set element
nft: src/netlink.c:596: __netlink_gen_data: Assertion `0' failed.

after:
internal:0:0-0: Error: Element mapping mismatches map definition, expected packet mark, not 'invalid'

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agojson: BASECHAIN flag no longer implies presence of priority expression
Florian Westphal [Mon, 14 Jul 2025 11:48:24 +0000 (13:48 +0200)] 
json: BASECHAIN flag no longer implies presence of priority expression

commit 715010c61ba25627b57d95d096138013e7c0e194 upstream.

This is a followup to
44ea19364637 ("src: BASECHAIN flag no longer implies presence of priority expression"):
feeding the same bogon file into nft -j we get a very similar crash.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: fix crash with invalid elements in set
Florian Westphal [Sun, 13 Jul 2025 21:59:30 +0000 (23:59 +0200)] 
evaluate: fix crash with invalid elements in set

commit 8cb7cfc2d8c7f2d8dec804ab028883c1d260e717 upstream.

ctx->ectx.key can be cleared, causing a crash:

src/nft --check -f tests/shell/testcases/bogons/nft-f/set_with_bad_elem
AddressSanitizer:DEADLYSIGNAL
    #0 0x7ffb57098c0d in elem_key_compatible src/evaluate.c:1934
    #1 0x7ffb5709926d in expr_evaluate_set_elem src/evaluate.c:1979
    #2 0x7ffb570a540f in expr_evaluate src/evaluate.c:3159
    #3 0x7ffb57095f33 in list_member_evaluate src/evaluate.c:1652
    #4 0x7ffb57099f92 in expr_evaluate_set src/evaluate.c:2066
    #5 0x7ffb570a53f7 in expr_evaluate src/evaluate.c:3157
    ..
AddressSanitizer: SEGV src/evaluate.c:1934 in elem_key_compatible

After:
set_with_bad_elem:4:39-46: Error: Element mismatches set definition, expected IPv4 address, not 'integer'
  elements = { 1.2.3.4, tcp << 8 }
                        ^^^^^^^^

Use ctx->set->key instead.

Fixes: 7f4d7fef31bd ("evaluate: check element key vs. set definition")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agotests: bogons: fix missing file name when logging
Florian Westphal [Mon, 14 Jul 2025 18:37:57 +0000 (20:37 +0200)] 
tests: bogons: fix missing file name when logging

commit 85b9124868886fc1015ca3f37da5c138123819a4 upstream.

When the json is parsed without returning an error the test
fails.  Its supposed to log the name of the failed input which
it does for -f but not for -j -f.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: check element key vs. set definition
Florian Westphal [Thu, 26 Jun 2025 14:52:31 +0000 (16:52 +0200)] 
evaluate: check element key vs. set definition

commit 7f4d7fef31bd282b8e37d6d401208535a1e74d17 upstream.

Included bogon asserts with:
 src/datatype.c:253: symbolic_constant_print: Assertion `expr->len / BITS_PER_BYTE <= sizeof(val)' failed.

Resolve this by validating that the set element key matches the set key
definition.

After this, loading the bogon file gives:
Error: Element mismatches set definition, expected concatenation of (IPv4 address, integer), not 'ICMP type'
elements = {redirect }
           ^^^^^^^^

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agotests: monitor: enclose device names in quotes
Pablo Neira Ayuso [Thu, 10 Jul 2025 00:53:50 +0000 (02:53 +0200)] 
tests: monitor: enclose device names in quotes

commit 26746952952bba8c19aebbd03a55decbc0d0c5fc upstream.

Update test to enclose flowtable device names in quotes, otherwise,
it reports a spurious issue:

@@ -1,2 +1,3 @@
 add table ip t
-add flowtable ip t ft { hook ingress priority 0; devices = { lo }; }
+add flowtable ip t ft { hook ingress priority 0; devices = { "lo" }; }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: validate set expression type before accessing flags
Pablo Neira Ayuso [Tue, 8 Jul 2025 22:21:49 +0000 (00:21 +0200)] 
evaluate: validate set expression type before accessing flags

commit 2022e8bb5cf0e0fa81ab0a5087bd1ab6e20280ee upstream.

Validate set->init is of EXPR_SET expression type before accessing
set_flags.

Fixes: 81e36530fcac ("src: replace interval segment tree overlap and automerge")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agorule: print chain and flowtable devices in quotes
Pablo Neira Ayuso [Tue, 8 Jul 2025 22:13:56 +0000 (00:13 +0200)] 
rule: print chain and flowtable devices in quotes

commit eb30f236d91a8d61ece789e28e6540b3a3fa2a6a upstream.

Print devices in quotes, for consistency with:

- the existing chain listing with single device:

  type filter hook ingress device "lo" priority filter; policy accept

- the ifname datatype used in sets.

In general, tokens that are user-defined, not coming in the datatype
symbol list, are enclosed in quotes.

Fixes: 3fdc7541fba0 ("src: add multidevice support for netdev chain")
Acked-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: prevent merge of sets with incompatible keys
Florian Westphal [Thu, 26 Jun 2025 00:52:48 +0000 (02:52 +0200)] 
evaluate: prevent merge of sets with incompatible keys

commit c9d6f089f0eb2cb615cbca3e4c99b07c5639960f upstream.

Its not enough to check for interval flag, this would assert in interval
code due to concat being passed to the interval code:
BUG: unhandled key type 13

After fix:
same_set_name_but_different_keys_assert:8:6-7: Error: set already exists with
different datatype (concatenation of (IPv4 address, network interface index) vs
network interface index)
        set s4 {
            ^^

This also improves error verbosity when mixing datamap and objref maps:

invalid_transcation_merge_map_and_objref_map:9:13-13:
Error: map already exists with different datatype (IPv4 address vs string)

.. instead of 'Cannot merge map with incompatible existing map of same name'.
The 'Cannot merge map with incompatible existing map of same name' check
is kept in place to catch when ruleset contains a set and map with same name
and same key definition.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: check that set type is identical before merging
Florian Westphal [Mon, 23 Jun 2025 19:37:31 +0000 (21:37 +0200)] 
evaluate: check that set type is identical before merging

commit 5335452966c4e5da2f3a5cf617cf431d711b215e upstream.

Reject maps and sets of the same name:
 BUG: invalid range expression type catch-all set element
 nft: src/expression.c:1704: range_expr_value_low: Assertion `0' failed.

After:
Error: Cannot merge set with existing datamap of same name
  set z {
      ^

v2:
Pablo points out that we shouldn't merge datamaps (plain value) and objref
maps either, catch this too and add another test:

nft --check -f invalid_transcation_merge_map_and_objref_map
invalid_transcation_merge_map_and_objref_map:9:13-13: Error: Cannot merge map with incompatible existing map of same name

We should also make sure that both data (for map case) and
set keys are identical, this is added in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: avoid double-free on error handling of bogus objref maps
Florian Westphal [Tue, 24 Jun 2025 21:20:58 +0000 (23:20 +0200)] 
evaluate: avoid double-free on error handling of bogus objref maps

commit ab1139f807f0d0519a25704e75c442ccb71f7a60 upstream.

commit 98c51aaac42b ("evaluate: bail out if anonymous concat set defines a non concat expression")
clears set->init to avoid a double-free.

Extend this to also handle object maps.
The included bogon triggers a double-free of set->init expression:

Error: unqualified type invalid specified in map definition. Try "typeof expression" instead of "type datatype".
ct helper set ct  saddr map { 1c3:: : "p", dead::beef : "myftp" }
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This might not crash, depending on libc/malloc, but ASAN reports this:
==17728==ERROR: AddressSanitizer: heap-use-after-free on address 0x50b0000005e8 at ..
READ of size 4 at 0x50b0000005e8 thread T0
    #0 0x7f1be3cb7526 in expr_free src/expression.c:87
    #1 0x7f1be3cbdf29 in map_expr_destroy src/expression.c:1488
    #2 0x7f1be3cb74d5 in expr_destroy src/expression.c:80
    #3 0x7f1be3cb75c6 in expr_free src/expression.c:96
    #4 0x7f1be3d5925e in objref_stmt_destroy src/statement.c:331
    #5 0x7f1be3d5831f in stmt_free src/statement.c:56
    #6 0x7f1be3d583c2 in stmt_list_free src/statement.c:66
    #7 0x7f1be3d42805 in rule_free src/rule.c:495
    #8 0x7f1be3d48329 in cmd_free src/rule.c:1417
    #9 0x7f1be3cd2c7c in __nft_run_cmd_from_filename src/libnftables.c:759
    #10 0x7f1be3cd340c in nft_run_cmd_from_filename src/libnftables.c:847
    #11 0x55dcde0440be in main src/main.c:535

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: make sure chain jump name comes with a null byte
Florian Westphal [Tue, 24 Jun 2025 21:01:13 +0000 (23:01 +0200)] 
evaluate: make sure chain jump name comes with a null byte

commit ca0c49d1bdb944534851c3dcb4c8ce16f1675074 upstream.

There is a stack oob read access in netlink_gen_chain():

mpz_export_data(chain, expr->chain->value,
BYTEORDER_HOST_ENDIAN, len);
snprintf(data->chain, NFT_CHAIN_MAXNAMELEN, "%s", chain);

There is no guarantee that chain[] is null terminated, so snprintf
can read past chain[] array.  ASAN report is:

AddressSanitizer: stack-buffer-overflow on address 0x7ffff5f00520 at ..
READ of size 257 at 0x7ffff5f00520 thread T0
    #0 0x00000032ffb6 in printf_common(void*, char const*, __va_list_tag*) (src/nft+0x32ffb6)
    #1 0x00000033055d in vsnprintf (src/nft+0x33055d)
    #2 0x000000332071 in snprintf (src/nft+0x332071)
    #3 0x0000004eef03 in netlink_gen_chain src/netlink.c:454:2
    #4 0x0000004eef03 in netlink_gen_verdict src/netlink.c:467:4

Reject chain jumps that exceed 255 characters, which matches the netlink
policy on the kernel side.

The included reproducer fails without asan too because the kernel will
reject the too-long chain name. But that happens after the asan detected
bogus read.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agojson: reject too long interface names
Florian Westphal [Tue, 24 Jun 2025 21:46:59 +0000 (23:46 +0200)] 
json: reject too long interface names

commit bed99830c4c63eae205c28a7ff914737bedb199d upstream.

Blamed commit added a length check on ifnames to the bison parser.
Unfortunately that wasn't enough, json parser has the same issue.

Bogon results in:
BUG: Interface length 44 exceeds limit
nft: src/mnl.c:742: nft_dev_add: Assertion `0' failed.

After patch, included bogon results in:
Error: Invalid device at index 0. name d2345678999999999999999999999999999999012345 too long

I intentionally did not extend evaluate.c to catch this, past sentiment
was that frontends should not send garbage.

I'll send a followup patch to also catch this from eval stage in case there
are further reports for frontends passing in such long names.

Fixes: fa52bc225806 ("parser: reject zero-length interface names")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_bison: allow delete command with map via handle
Pablo Neira Ayuso [Sun, 15 Jun 2025 09:36:28 +0000 (11:36 +0200)] 
parser_bison: allow delete command with map via handle

commit 640312b1529c548790117635c91886a6c83e83f2 upstream.

For consistency with sets, allow delete via handle for maps too.

Fixes: f4a34d25f6d5 ("src: list set handle and delete set via set handle")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agocache: assert name is non-nul when looking up
Pablo Neira Ayuso [Sun, 15 Jun 2025 09:33:49 +0000 (11:33 +0200)] 
cache: assert name is non-nul when looking up

commit f15bc7d368b7c1d897fd830f91e7db6929175b27 upstream.

{table,chain,set,obj,flowtable}_cache_find() should not be called when
handles are used

Fixes: 5ec5c706d993 ("cache: add hashtable cache for table")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agorule: skip fuzzy lookup if object name is not available
Pablo Neira Ayuso [Sun, 15 Jun 2025 09:33:42 +0000 (11:33 +0200)] 
rule: skip fuzzy lookup if object name is not available

commit de8396358f869d6d7640eae6d6287c2f7fb0d3dc upstream.

Skip fuzzy lookup for suggestions when handles are used.

Note that 4cf97abfee61 ("rule: Avoid segfault with anonymous chains")
already skips it for chain.

Fixes: 285bb67a11ad ("src: introduce simple hints on incorrect set")
Fixes: 9f7817a4e022 ("src: introduce simple hints on incorrect chain")
Fixes: d7476ddd5f7d ("src: introduce simple hints on incorrect table")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agosrc: BASECHAIN flag no longer implies presence of priority expression
Florian Westphal [Thu, 12 Jun 2025 18:17:15 +0000 (20:17 +0200)] 
src: BASECHAIN flag no longer implies presence of priority expression

commit 44ea1936463728475768861073ca4ba34a5c2f75 upstream.

The included bogon will crash nft because print side assumes that BASECHAIN
flag presence also means that priority expression is available.

Make the print side conditional.

Fixes: a66b5ad9540d ("src: allow for updating devices on existing netdev chain")
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: restrict allowed subtypes of concatenations
Florian Westphal [Fri, 6 Jun 2025 12:12:37 +0000 (14:12 +0200)] 
evaluate: restrict allowed subtypes of concatenations

commit 3cec07327ea2b91ac8395e0c0ee2a635a5e9fcd5 upstream.

We need to restrict this, included bogon asserts with:
BUG: unknown expression type prefix
nft: src/netlink_linearize.c:940: netlink_gen_expr: Assertion `0' failed.

Prefix expressions are only allowed if the concatenation is used within
a set element, not when specifying the lookup key.

For the former, anything that represents a value is allowed.
For the latter, only what will generate data (fill a register) is
permitted.

At this time we do not have an annotation that tells if the expression
is on the left hand side (lookup key) or right hand side (set element).

Add a new list recursion counter for this. If its 0 then we're building
the lookup key, if its the latter the concatenation is the RHS part
of a relational expression and prefix, ranges and so on are allowed.

IOW, we don't really need a recursion counter, another type of annotation
that would tell if the expression is placed on the left or right hand side
of another expression would work too.

v2: explicitly list all 'illegal' expression types instead of
using a default label for them.

This will raise a compiler warning to remind us to adjust the case
labels in case a new expression type gets added in the future.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: rename recursion counter to recursion.binop
Florian Westphal [Fri, 6 Jun 2025 12:12:36 +0000 (14:12 +0200)] 
evaluate: rename recursion counter to recursion.binop

commit 10b44319a53a131ed943e2b6eeb62d197178bf4d upstream.

The existing recursion counter is used by the binop expression to detect
if we've completely followed all the binops.

We can only chain up to NFT_MAX_EXPR_RECURSION binops, but the evaluation
step can perform constant-folding, so we must recurse until we found the
rightmost (last) binop in the chain.

Then we can check the post-eval chain to see if it is something that can
be serialized later (i.e., if we are within the NFT_MAX_EXPR_RECURSION
after constant folding) or not.

Thus we can't reuse the existing ctx->recursion counter for other
expressions; entering the initial expr_evaluate_binop with
ctx->recursion > 0 would break things.

Therefore rename this to an embedded structure.
This allows us to add a new recursion counter in a followup patch.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agotest: shell: Don't use system nft binary
Yi Chen [Sun, 22 Jun 2025 12:55:51 +0000 (20:55 +0800)] 
test: shell: Don't use system nft binary

commit c73eadca05c781ebad631331a6864fa8c54a5024 upstream.

Use the defined $NFT variable instead of calling the system nft binary directly.
Add a nat_ftp.nodump file to avoid the following check-tree.sh error:
ERR: "tests/shell/testcases/packetpath/nat_ftp" has no "tests/shell/testcases/packetpath/dumps/nat_ftp.{nft,nodump}" file.

Signed-off-by: Yi Chen <yiche@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: don't BUG on unexpected base datatype
Florian Westphal [Fri, 13 Jun 2025 14:46:06 +0000 (16:46 +0200)] 
evaluate: don't BUG on unexpected base datatype

commit 845b8d7208077310e77560a64b698973fb047ef2 upstream.

Included bogon will cause a crash but this is the evaluation stage where
we can just emit an error instead.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agojson: Dump flowtable hook spec only if present
Phil Sutter [Wed, 11 Jun 2025 14:45:48 +0000 (16:45 +0200)] 
json: Dump flowtable hook spec only if present

commit 9fa3fbed73a36111e308c9856514a032f5444564 upstream.

If there is no priority.expr set, assume hook.num is bogus, too.

While this is fixing JSON output, it's hard to tell what commit this is
actually fixing: Before commit 627c451b23513 ("src: allow variables in
the chain priority specification"), there was no way to detect
flowtables missing hook specs (e.g. when printing flowtable delete
monitor event).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomonitor: Recognize flowtable add/del events
Phil Sutter [Wed, 15 May 2024 14:01:20 +0000 (16:01 +0200)] 
monitor: Recognize flowtable add/del events

commit 73a8adfc2432ec8337288cc90e7c9f4509139846 upstream.

These were entirely ignored before, add the necessary code analogous to
e.g. objects.

Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agomnl: catch bogus expressions before crashing
Florian Westphal [Thu, 5 Jun 2025 22:20:28 +0000 (00:20 +0200)] 
mnl: catch bogus expressions before crashing

commit d3339f9e35ee4dddf290fcc3e9cc63dac8cb836a upstream.

We can't recover from errors here, but we can abort with a more
precise reason than 'segmentation fault', or stack corruptions
that get caught way later, or not at all.

expr->value is going to be read, we can't cope with other expression
types here.

We will copy to stack buffer of IFNAMSIZ size, abort if we would
overflow.

Check there is a NUL byte present too.
This is a preemptive patch, I've seen one crash in this area but
no reproducer yet.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomnl: handle singleton element in netdevice set
Pablo Neira Ayuso [Tue, 25 Apr 2023 09:48:52 +0000 (11:48 +0200)] 
mnl: handle singleton element in netdevice set

commit 89b60bc18ca76e45b1682d8c56a0d01fffedebad upstream.

expr_evaluate_set() turns sets with singleton element into value,
nft_dev_add() expects a list of expression, so it crashes.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1676
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agomnl: flowtable support for extended netlink error reporting
Pablo Neira Ayuso [Wed, 19 Apr 2023 13:38:04 +0000 (15:38 +0200)] 
mnl: flowtable support for extended netlink error reporting

commit 99b56d4ee1442ccfa0aadde3cb3ecee74a4de815 upstream.

This patch extends existing flowtable support to improve error
reporting:

 # nft add flowtable inet x y '{ devices = { x } ; }'
 Error: Could not process rule: No such file or directory
 add flowtable inet x y { devices = { x } ; }
                                      ^
 # nft delete flowtable inet x y '{ devices = { x } ; }'
 Error: Could not process rule: No such file or directory
 delete flowtable inet x y { devices = { x } ; }
                                         ^
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agocache: Tolerate object deserialization failures
Phil Sutter [Fri, 16 May 2025 17:36:37 +0000 (19:36 +0200)] 
cache: Tolerate object deserialization failures

commit 610089f273c968c983d965f3cac42e4d9784cde8 upstream.

If netlink_delinearize_obj() fails, it will print an error message. Skip
this object and keep going.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Keep going after set element parsing failures
Phil Sutter [Fri, 16 May 2025 17:33:25 +0000 (19:33 +0200)] 
netlink: Keep going after set element parsing failures

commit 31732dea34a2a4c484df13c4003728eca396e573 upstream.

Print an error message and try to deserialize the remaining elements
instead of calling BUG().

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Pass netlink_ctx to netlink_delinearize_setelem()
Phil Sutter [Fri, 16 May 2025 17:17:00 +0000 (19:17 +0200)] 
netlink: Pass netlink_ctx to netlink_delinearize_setelem()

commit 6296f09b8b8ecb6d46cfcd90fd955a39bd86c6c9 upstream.

Prepare for calling netlink_io_error() which needs the context pointer.
Trade this in for the cache pointer since no caller uses a special one.
No functional change intended.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink_delinearize: Replace some BUG()s by error messages
Phil Sutter [Fri, 16 May 2025 11:28:19 +0000 (13:28 +0200)] 
netlink_delinearize: Replace some BUG()s by error messages

commit 9f2a11aa15422333d1812501dd4f294348d6e0d5 upstream.

Netlink parser tries to keep going despite errors. Faced with an
incompatible ruleset, this is much more user-friendly than exiting the
program upon the first obstacle. This patch fixes three more spots to
support this.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agonetlink: Catch unknown types when deserializing objects
Phil Sutter [Fri, 16 May 2025 17:41:19 +0000 (19:41 +0200)] 
netlink: Catch unknown types when deserializing objects

commit 690f19eadde5cb607ec3d8d471c86d558c7229bd upstream.

Print an error message and discard the object instead of returning it to
the caller. At least when trying to print it, we would hit an assert()
in obj_type_name() anyway.

Fixes: 4756d92e517ae ("src: listing of stateful objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agonetlink: Avoid potential NULL-ptr deref parsing set elem expressions
Phil Sutter [Fri, 16 May 2025 18:08:05 +0000 (20:08 +0200)] 
netlink: Avoid potential NULL-ptr deref parsing set elem expressions

commit 89645d04106d45d5e23b6d5206777dad1fb3e6bf upstream.

Since netlink_parse_set_expr() may return NULL, the following deref must
be guarded.

Fixes: e6d1d0d611958 ("src: add set element multi-statement support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agodoc: Fix typo in nat statement 'prefix' description
Phil Sutter [Thu, 8 May 2025 14:35:47 +0000 (16:35 +0200)] 
doc: Fix typo in nat statement 'prefix' description

commit 1b3c33a7931967fb7afe4efdf68deb7781786489 upstream.

No point in repeating 'to map' here.

Fixes: 19d73ccdd39fa ("doc: add nat examples")
Signed-off-by: Phil Sutter <phil@nwl.cc>
7 weeks agodoc: add nat examples
Florian Westphal [Mon, 1 May 2023 10:10:09 +0000 (12:10 +0200)] 
doc: add nat examples

commit 19d73ccdd39fae49b44330097ecd2bc507e63d1b upstream.

nftables nat is much more capable than what the existing
documentation describes.

In particular, nftables can fully emulate iptables
NETMAP target and can perform n:m address mapping.

Add a new example section extracted from commit log
messages when those features got added.

Cc: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agosrc: netlink: fix crash when ops doesn't support udata
Florian Westphal [Thu, 8 May 2025 14:29:04 +0000 (16:29 +0200)] 
src: netlink: fix crash when ops doesn't support udata

commit be737a1986bfee0ddea4bee7863dca0123a2bcbc upstream.

Whenever a new version adds udata support to an expression, then old
versions of nft will crash when trying to list such a ruleset generated
by a more recent version of nftables.

Fix this by falling back to 'type' format.

Fixes: 6e48df5329ea ('src: add "typeof" build/parse/print support')
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoexpression: cleanup expr_ops_by_type() and handle u32 input
Thomas Haller [Wed, 20 Sep 2023 14:26:08 +0000 (16:26 +0200)] 
expression: cleanup expr_ops_by_type() and handle u32 input

commit 3d0ce3c19d319a5aae806b617905cfa1ee7f87f4 upstream.

Make fewer assumptions about the underlying integer type of the enum.
Instead, be clear about where we have an untrusted uint32_t from netlink
and an enum. Rename expr_ops_by_type() to expr_ops_by_type_u32() to make
this clearer. Later we might make the enum as packed, when this starts
to matter more.

Also, only the code path expr_ops() wants strict validation and assert
against valid enum values. Move the assertion out of
__expr_ops_by_type(). Then expr_ops_by_type_u32() does not need to
duplicate the handling of EXPR_INVALID. We still need to duplicate the
check against EXPR_MAX, to ensure that the uint32_t value can be cast to
an enum value.

[ Remove cast on EXPR_MAX. --pablo ]

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agosrc: remove bogus empty file
Florian Westphal [Fri, 25 Apr 2025 06:20:19 +0000 (08:20 +0200)] 
src: remove bogus empty file

commit f7d932bff5c9550905659dd4a5df23ca46f84935 upstream.

No idea how this happened, remove it.

Reported-by: Sunny73Cr <Sunny73Cr@protonmail.com>
Fixes: 058246016188 ("src: allow to map key to nfqueue number")
Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: bail out if ct saddr/daddr dependency cannot be inserted
Florian Westphal [Wed, 2 Apr 2025 23:09:22 +0000 (01:09 +0200)] 
evaluate: bail out if ct saddr/daddr dependency cannot be inserted

commit 7f60519e356833ca007b138c00b9f5de09f21b56 upstream.

If we have an incomplete rule like "ct original saddr" in inet
family, this function generates an error because it can't determine the required protocol
dependency, hinting at missing ip/ip6 keyword.

We should not go on in this case to avoid a redundant followup error:

nft add rule inet f c ct original saddr 1.2.3.4
Error: cannot determine ip protocol version, use "ip saddr" or "ip6 saddr" instead
add rule inet f c ct original saddr 1.2.3.4
                  ^^^^^^^^^^^^^^^^^
Error: Could not parse symbolic invalid expression
add rule inet f c ct original saddr 1.2.3.4

After this change only the first error is shown.

Fixes: 2b29ea5f3c3e ("src: ct: add eval part to inject dependencies for ct saddr/daddr")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: bail out on malformed statement in set
Pablo Neira Ayuso [Tue, 1 Apr 2025 07:57:59 +0000 (09:57 +0200)] 
parser_json: bail out on malformed statement in set

commit cc7a7d025a60dced5f5e894aa1688014c9deef3d upstream.

Propagate error to caller so it bails out on malformed set statements.

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: allow statement stateful statement only in set elements
Pablo Neira Ayuso [Tue, 1 Apr 2025 07:49:48 +0000 (09:49 +0200)] 
parser_json: allow statement stateful statement only in set elements

commit 85b5b051f0e64f106cee078fc6d66d0872770e79 upstream.

Upfront reject of non stateful statements in set elements.

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoparser_json: reject empty jump/goto chain
Pablo Neira Ayuso [Mon, 31 Mar 2025 15:55:45 +0000 (17:55 +0200)] 
parser_json: reject empty jump/goto chain

commit 9cce81701a033c4ff5e804fbf7a1149acb9e115a upstream.

When parsing a verdict map json where element jumps to chain represented
as empty string.

internal:0:0-0: Error: Parsing list expression item at index 0 failed.
internal:0:0-0: Error: Invalid set elem at index 0.
internal:0:0-0: Error: Invalid set elem expression.
internal:0:0-0: Error: Parsing command array at index 2 failed.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agojson: don't BUG when asked to list synproxies
Florian Westphal [Thu, 27 Mar 2025 16:32:00 +0000 (17:32 +0100)] 
json: don't BUG when asked to list synproxies

commit 40b0708ca6dee4829a9b6e1c2f4677ff2c206b43 upstream.

"-j list synproxys" triggers a BUG().

Rewrite this so that all enum values are handled so the compiler can alert
us to a missing value in case there are more commands in the future.

While at it, implement a few low-hanging fruites as well.

Not-yet-supported cases are simply ignored.

v2: return EOPNOTSUPP for unsupported commands (Pablo Neira Ayuso)

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoct expectation: fix 'list object x' vs. 'list objects in table' confusion
Florian Westphal [Fri, 28 Jul 2023 19:04:13 +0000 (21:04 +0200)] 
ct expectation: fix 'list object x' vs. 'list objects in table' confusion

commit a8ff324dc64fd76f7d218d3d94c5885250951258 upstream.

Just like "ct timeout", "ct expectation" is in need of the same fix,
we get segfault on "nft list ct expectation table t", if table t exists.

This is the exact same pattern as resolved for "ct timeout" in commit
1d2e22fc0521 ("ct timeout: fix 'list object x' vs. 'list objects in table' confusion").

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoct timeout: fix 'list object x' vs. 'list objects in table' confusion
Florian Westphal [Mon, 19 Jun 2023 20:43:06 +0000 (22:43 +0200)] 
ct timeout: fix 'list object x' vs. 'list objects in table' confusion

commit 1d2e22fc0521bcf73ee1f891c291dc1bde47a6bb upstream.

<empty ruleset>
$ nft list ct timeout table t
Error: No such file or directory
list ct timeout table t
                      ^
This is expected to list all 'ct timeout' objects.
The failure is correct, the table 't' does not exist.

But now lets add one:
$ nft add table t
$ nft list ct timeout  table t
Segmentation fault (core dumped)

... and thats not expected, nothing should be shown
and nft should exit normally.

Because of missing TIMEOUTS command enum, the backend thinks
it should do an object lookup, but as frontend asked for
'list of objects' rather than 'show this object',
handle.obj.name is NULL, which then results in this crash.

Update the command enums so that backend knows what the
frontend asked for.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agojson: return error if table does not exist
Florian Westphal [Thu, 27 Mar 2025 08:07:52 +0000 (09:07 +0100)] 
json: return error if table does not exist

commit 2200a105d4befc15b0cfa6cc24282e493888dc82 upstream.

Identical bug and thus same fix as
853d3a2d3cbd ("rule: return error if table does not exist"),
but this time for json.

Signed-off-by: Florian Westphal <fw@strlen.de>
7 weeks agoevaluate: reject unsupported expressions in payload statement for bitfields
Pablo Neira Ayuso [Fri, 28 Feb 2025 14:57:18 +0000 (15:57 +0100)] 
evaluate: reject unsupported expressions in payload statement for bitfields

commit de447e248cce3ca3fd7040c386ac798c35721415 upstream.

The payload statement evaluation pretends that it can handle any
expression for bitfields, but the existing evaluation code only knows
how to handle value expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoevaluate: simplify payload statement evaluation for bitfields
Pablo Neira Ayuso [Fri, 28 Feb 2025 14:55:04 +0000 (15:55 +0100)] 
evaluate: simplify payload statement evaluation for bitfields

commit 6d9297f278290888e7e26762ee72ed6afa22a97e upstream.

Instead of allocating a lshift expression and relying on the binary
operation transfer propagate this to the mask value, lshift the mask
value immediately.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agotests: py: fix WARNING with JSON
Pablo Neira Ayuso [Tue, 12 Aug 2025 19:52:31 +0000 (21:52 +0200)] 
tests: py: fix WARNING with JSON

backport commit c4c740117f6fbf39dd67dd87635ea8b497718ad7 upstream.

To remove the following WARNING when running JSON tests:

WARNING: Recorded JSON output matches input for: meter acct_out size 4096 { meta iif . ip6 saddr timeout 600s counter }
WARNING: Recorded JSON output matches input for: meter acct_out size 12345 { ip6 saddr . meta iif timeout 600s counter }

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
7 weeks agoRevert "tests: py: fix json single-flag output for fib & synproxy"
Pablo Neira Ayuso [Tue, 12 Aug 2025 19:57:43 +0000 (21:57 +0200)] 
Revert "tests: py: fix json single-flag output for fib & synproxy"

This reverts commit 4725f4887da84b1b6bbfd0c138bc48a707301fa1.

This 1.0.6.y branch does not contain the follow upstream commits:

  38f99ee84fe6 ("json: Print single synproxy flags as non-array")
  dbe5c44f2b89 ("json: Print single fib flag as non-array")

Therefore, such commit above is not required.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: fix json single-flag output for fib & synproxy
Florian Westphal [Mon, 2 Jun 2025 12:12:16 +0000 (14:12 +0200)] 
tests: py: fix json single-flag output for fib & synproxy

commit c2ee1d5978bf6ea10e3b1a74125da5b5e8468f26 upstream.

Blamed commits change output format but did not adjust existing tests:
  inet/fib.t: WARNING: line 16: '{"nftables": ..

Fixes: 38f99ee84fe6 ("json: Print single synproxy flags as non-array")
Fixes: dbe5c44f2b89 ("json: Print single fib flag as non-array")
Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Phil Sutter <phil@nwl.cc>
2 months agonetlink_delinerize: add more restrictions on meta nfproto removal
Florian Westphal [Sun, 16 Mar 2025 13:10:26 +0000 (14:10 +0100)] 
netlink_delinerize: add more restrictions on meta nfproto removal

commit 7b3ee497040ff8efb131c566e1c6b466e16f45cc upstream.

We can't remove 'meta nfproto' dependencies for all cases.
Its removed for ip/ip6 families, this works fine.

But for others, e.g. inet, removal is not as simple.
For example

   meta nfproto ipv4 ct protocol tcp

is listed as 'ct protocol tcp', even when this is uses in the inet
table.

Meta L4PROTO removal checks were correct, but refactor this
into a helper function to split meta/ct checks from the common
calling function.

Ct check was lacking, we need to examine ct keys more closely
to figure out if they need to retain the network protocol depenency
or not.  Elide for NFT_CT_SRC/DST and its variants, as those imply
the network protocol to use, all others must keep it as-is.

Also extend test coverage for this.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1783
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agopayload: don't kill dependency for proto_th
Florian Westphal [Thu, 27 Feb 2025 10:47:02 +0000 (11:47 +0100)] 
payload: don't kill dependency for proto_th

commit c0843dec745433ac36aafb5726c580449e522d1d upstream.

proto_th carries no information about the proto number, we need to
preserve the L4 protocol expression unless we can be sure that

For example, if "meta l4proto 91 @th,0,16 0" is simplified to
"th sport 0", the information of protocol number is lost.

Based on initial patch from Xiao Liang.

Signed-off-by: Xiao Liang <shaw.leon@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Properly fix JSON equivalents for netdev/reject.t
Phil Sutter [Thu, 12 Jun 2025 10:59:29 +0000 (12:59 +0200)] 
tests: py: Properly fix JSON equivalents for netdev/reject.t

commit b7a11c691d459d06a801a28ae9a52bc564584a1a upstream.

Revert commit d1a7b9e19fe65 ("tests: py: update netdev reject test
file"), the stored JSON equivalents were correct in that they matched
the standard syntax input.

In fact, we missed a .json.output file recording the expected deviation
in JSON output.

Fixes: d1a7b9e19fe65 ("tests: py: update netdev reject test file")
Fixes: 7ca3368cd7575 ("reject: Unify inet, netdev and bridge delinearization")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: extend raw payload match tests
Florian Westphal [Thu, 30 Jan 2025 17:47:14 +0000 (18:47 +0100)] 
tests: py: extend raw payload match tests

commit 857904bdfaf7aa624d974ed6533fd4d4cd39babd upstream.

Add more test cases to exercise binop elimination for raw
payload matches.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Fix for storing payload into missing file
Phil Sutter [Wed, 2 Oct 2024 17:55:49 +0000 (19:55 +0200)] 
tests: py: Fix for storing payload into missing file

commit c1c0c54e237c880adaa8172b93d7450e6c617cfc upstream.

When running a test for which no corresponding *.payload file exists,
the *.payload.got file name was incorrectly constructed due to
'payload_path' variable not being set.

Fixes: 2cfab7a3e10fc ("tests/py: Write dissenting payload into the right file")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: fix up udp csum fixup output
Florian Westphal [Wed, 11 Sep 2024 12:23:01 +0000 (14:23 +0200)] 
tests: py: fix up udp csum fixup output

commit 036a1eb3304995e4e000c552b9dfd33b1073ddb6 upstream.

Preceeding commit switched udp to use the inkernel csum parser, so tests
warn:

WARNING: line 7: 'add rule ip test-ip4 input iif "lo" udp checksum set 0':
'[ payload write reg 1 => 2b @ transport header + 6 csum_type 1 csum_off 6 csum_flags 0x0 ]' mismatches
'[ payload write reg 1 => 2b @ transport header + 6 csum_type 0 csum_off 0 csum_flags 0x1 ]'

Fixes: f89abfb4068d ("proto: use NFT_PAYLOAD_L4CSUM_PSEUDOHDR flag to mangle UDP checksum")
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: drop redundant JSON outputs
Pablo Neira Ayuso [Tue, 18 Jun 2024 15:31:46 +0000 (17:31 +0200)] 
tests: py: drop redundant JSON outputs

commit f13fd62ef1e2755ed5eef11adbe0e8a479528f72 upstream.

8abe71f862e6 ("tests: py: Warn if recorded JSON output matches the input")
adds a warning on duplicated JSON outputs.

Remove them when running tests with -j:

  WARNING: Recorded JSON output matches input for: icmp code { 2, 4, 54, 33, 56}

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: complete icmp and icmpv6 update
Pablo Neira Ayuso [Thu, 4 Apr 2024 11:56:39 +0000 (13:56 +0200)] 
tests: py: complete icmp and icmpv6 update

commit 1702bfd70b39fe955d5e2c4b93f7dc07acb79027 upstream.

Update json update and leftover payload update to complete
5fecd2a6ef61 ("src: disentangle ICMP code types").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Drop needless recorded JSON outputs
Phil Sutter [Fri, 22 Mar 2024 14:18:50 +0000 (15:18 +0100)] 
tests: py: Drop needless recorded JSON outputs

commit 07c9733a4b4ab5b6f6fb94f305d323528a058823 upstream.

These match the input already, no need to track them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: Warn if recorded JSON output matches the input
Phil Sutter [Fri, 22 Mar 2024 14:04:40 +0000 (15:04 +0100)] 
tests: py: Warn if recorded JSON output matches the input

commit 8abe71f862e6248517365bec4bac31d85141cf69 upstream.

Actively support spring-cleaning by nagging callers.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: Fix some JSON equivalents
Phil Sutter [Fri, 22 Mar 2024 13:00:26 +0000 (14:00 +0100)] 
tests: py: Fix some JSON equivalents

commit 0c17b910b3e19c4eacf5567f90b6545ed1fd8aac upstream.

Make sure they match the standard syntax input as much as possible.

For some reason inet/tcp.t.json was using plain arrays in place of
binary OR expressions in many cases. These arrays are interpreted as
list expressions, which seems to be semantically identical but the goal
here is to present an accurate equivalent to the rule in standard
syntax.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: py: add payload merging test cases
Florian Westphal [Fri, 8 Mar 2024 13:40:12 +0000 (14:40 +0100)] 
tests: py: add payload merging test cases

commit 04fc306359a49bc9f314ae82520218f5dfebfef1 upstream.

Add a test case that would fail without preceeding fix.

[ NB. Precending fix is f35a0d78fe870737fa39d859bd2e3ac25bf1b12e ]

Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: add missing json.output data
Florian Westphal [Mon, 26 Feb 2024 08:45:43 +0000 (09:45 +0100)] 
tests: py: add missing json.output data

commit f58e72a2f5aa134653e7ee8b5432b23eb0674c3c upstream.

Fixes: bridge/vlan.t: WARNING: line 56: ...
Fixes: 8b9ae77598b4 ("tests: never merge across non-expression statements redux 2")
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: remove huge-limit test cases
Florian Westphal [Thu, 18 Jan 2024 12:24:04 +0000 (13:24 +0100)] 
tests: py: remove huge-limit test cases

commit 1ee84db0360db0da336d5b1ee92807d29fcfbbe1 upstream.

These tests will fail once the kernel checks for overflow
in the internal token bucken counter, so drop them.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: py: missing json output in meta.t with vlan mapping
Pablo Neira Ayuso [Mon, 11 Dec 2023 11:54:35 +0000 (12:54 +0100)] 
tests: py: missing json output in meta.t with vlan mapping

commit 5fec559727ffd2c6c8958748beab782096385758 upstream.

Fix this warning due to missing coverage:

 tests/py/any/meta.t.json.got: WARNING: line 2: Wrote JSON equivalent for rule meta mark set vlan id map { 1 : 0x00000001, 4095 : 0x00004095 }
 ERROR: did not find JSON equivalent for rule 'meta mark set vlan id map @map1

Fixes: 8d3de823b622 ("evaluate: reset statement length context before evaluating statement")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: missing json output in never merge across non-expression statements
Pablo Neira Ayuso [Wed, 6 Dec 2023 18:30:44 +0000 (19:30 +0100)] 
tests: py: missing json output in never merge across non-expression statements

commit 94fd162ea4d25fe6b0b4d58dcb7ff66dc55f3247 upstream.

Add missing json output.

Fixes: 99ab1b8feb16 ("rule: never merge across non-expression statements")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: debloat frag.t.payload.netdev
Pablo Neira Ayuso [Tue, 29 Aug 2023 17:30:14 +0000 (19:30 +0200)] 
tests: py: debloat frag.t.payload.netdev

commit 998c09a2cb011735cbe96c8182ef34e32603b4fc upstream.

This bytecode output file contains many duplicated entries, remove them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: extend ip frag-off coverage
Pablo Neira Ayuso [Tue, 29 Aug 2023 17:04:07 +0000 (19:04 +0200)] 
tests: py: extend ip frag-off coverage

commit 3e22505e470966635facc0395541ec80c587c063 upstream.

Cover matching on DF and MF bits and fragments.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Document JSON mode in README
Phil Sutter [Tue, 27 Jun 2023 15:50:07 +0000 (17:50 +0200)] 
tests: py: Document JSON mode in README

commit 50bebfaf9d558d539f06c0710781a49e8b58f25f upstream.

Mostly identify the various files that (may) appear or exist already and
how to deal with them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agoAmend "tests: shell: Fix ifname_based_hooks feature check"
Pablo Neira Ayuso [Mon, 28 Jul 2025 22:52:55 +0000 (00:52 +0200)] 
Amend "tests: shell: Fix ifname_based_hooks feature check"

commit 646acfaceb1f550c982c31ba6e60996b0bb012d7 upstream.

The following syntax:

  type filter hook ingress priority 0; devices = { d0 };

is not parsed in 1.0.6.y.

And remove device deletion, this is only available since 1.0.8 in:

commit a66b5ad9540dd64c7c67006201b8b3ccf8e4316b
Author: Pablo Neira Ayuso <pablo@netfilter.org>
Date:   Wed Apr 19 11:50:01 2023 +0200

    src: allow for updating devices on existing netdev chain

skip netdev_chain_dormant_autoremove for new kernels.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoRevert "tests: shell: move flowtable with bogus priority to correct location"
Pablo Neira Ayuso [Mon, 28 Jul 2025 21:36:43 +0000 (23:36 +0200)] 
Revert "tests: shell: move flowtable with bogus priority to correct location"

Revert commit 156b5936b3b7a0b1ee590a02874beaba5235f758 upstream.

It fails for unknown reason to me.

2 months agoPartial revert "tests: py: move meter tests to tests/shell"
Pablo Neira Ayuso [Mon, 28 Jul 2025 20:34:39 +0000 (22:34 +0200)] 
Partial revert "tests: py: move meter tests to tests/shell"

Partial revert of commit c4c740117f6fbf39dd67dd87635ea8b497718ad7 upstream.

Only tests/shell, translation of meter to dynamic set is not
available in 1.0.6.y

2 months agoRevert "evaluate: translate meter into dynamic set"
Pablo Neira Ayuso [Mon, 28 Jul 2025 20:15:16 +0000 (22:15 +0200)] 
Revert "evaluate: translate meter into dynamic set"

This reverts commit b8f8ddfff7335d3a8bebf5d85085974ae36f4099 upstream.

Only tests/shell, since translation of meter to dynamic set is not
available in 1.0.6.y

2 months agoPartial revert in testcase/sets/set_stmt to remove last statement coverage
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:50:57 +0000 (23:50 +0200)] 
Partial revert in testcase/sets/set_stmt to remove last statement coverage

Partial revert of commit 1f3d0b9cf9cc823c1f747650a84f1b43817d460f upstream.

This last statement is not supported in 1.0.6.y, remove it from

  1f3d0b9cf9cc ("tests: shell: extend coverage for set element statements")

to make this test pass.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoRevert mptcp tests for sets/typeof_sets_0
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:45:43 +0000 (23:45 +0200)] 
Revert mptcp tests for sets/typeof_sets_0

Revert upstream commits:

 9832c2c347eb ("tcpopt: add symbol table for mptcp suboptions")
 01fe0f07a0ed ("netlink_delinearize: also consider exthdr type when trimming binops")

These commits are not backported, revert these tests.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoRevert "evaluate: allow to re-use existing metered set"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:57:46 +0000 (21:57 +0200)] 
Revert "evaluate: allow to re-use existing metered set"

This reverts commit 639a111e91341cffdc6d86b847aa654646c799cf upstream.

Conversion to dynamic set is not available in 1.0.6.y

2 months agoRevert "src: print set element with multi-word description in single one line"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:56:29 +0000 (21:56 +0200)] 
Revert "src: print set element with multi-word description in single one line"

This reverts commit f133180ee0c72f4a0de4a7acad8a9d8476914bd4 upstream.

2 months agoRevert "json: Print single set flag as non-array"
Pablo Neira Ayuso [Wed, 18 Jun 2025 19:55:51 +0000 (21:55 +0200)] 
Revert "json: Print single set flag as non-array"

This reverts commit 6bedb12af1658562f277ca68d74cf1e9e7433a08 upstream.

This more compact json representation for flags is not present in 1.0.6.y

2 months agoBackport nftables tests/shell from 2a38f458f12b
Pablo Neira Ayuso [Sun, 27 Jul 2025 21:27:14 +0000 (23:27 +0200)] 
Backport nftables tests/shell from 2a38f458f12b

Originally, the plan was to use tests/shell from git HEAD, but this
does not work well in practise because slight changes in the test
output break checks resulting in lots of [DUMP FAIL] errors.

It is easier to test infrastructure self-contained in this 1.0.6.y
branch.

However, backporting the tests/shell into 1.0.6.y turns out to be more
complicated than expected, so I decided to follow the opposite, which is
to (brute) force a copy of tests/shell from
2a38f458f12bc032dac1b3ba63f95ca5a3c03fbd into this branch.

This also requires a number follow up partial reverts on tests/shell
updates to work with 1.0.6.y.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoevalute: make vlan pcp updates work
Florian Westphal [Sat, 19 Apr 2025 11:44:39 +0000 (13:44 +0200)] 
evalute: make vlan pcp updates work

commit 2c41c82296c70203f4781fb976ee0f05629fba44 upstream.

On kernel side, nft_payload_set_vlan() requires a 2 or 4 byte
write to the vlan header.

As-is, nft emits a 1 byte write:
  [ payload load 1b @ link header + 14 => reg 1 ]
  [ bitwise reg 1 = ( reg 1 & 0x0000001f ) ^ 0x00000020 ]

... which the kernel doesn't support.  Expand all vlan header updates to
a 2 or 4 byte write and update the existing vlan id test case.

Reported-by: Kevin Vigouroux <ke.vigouroux@laposte.net>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoevaluate: only allow stateful statements in set and map definitions
Florian Westphal [Mon, 31 Mar 2025 15:23:20 +0000 (17:23 +0200)] 
evaluate: only allow stateful statements in set and map definitions

commit 0acd81559ec9efe2cc3d869bfc8e5a0b4d888456 upstream.

The bison parser doesn't allow this to happen due to grammar
restrictions, but the json input has no such issues.

The bogon input assigns 'notrack' which triggers:
BUG: unknown stateful statement type 19
nft: src/netlink_linearize.c:1061: netlink_gen_stmt_stateful: Assertion `0' failed.

After patch, we get:
Error: map statement must be stateful

Fixes: 07958ec53830 ("json: add set statement list support")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoevaluate: compact STMT_F_STATEFUL checks
Florian Westphal [Mon, 31 Mar 2025 15:23:19 +0000 (17:23 +0200)] 
evaluate: compact STMT_F_STATEFUL checks

commit 36bd6d0088bca1087aeccfe14aaa786200d755bc upstream.

We'll gain another F_STATEFUL check in a followup patch,
so lets condense the pattern into a helper to reduce copypaste.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoparser_json: only allow concatenations with 2 or more expressions
Florian Westphal [Wed, 2 Apr 2025 05:18:18 +0000 (07:18 +0200)] 
parser_json: only allow concatenations with 2 or more expressions

commit 713592c6008a8c589a00d3d3d2e49709ff2de62c upstream.

The bison parser enforces this implicitly by grammar rules.
Because subkeys have to be conatenated via ".", notation, e.g.
"mark . ip saddr", all concatenation expressions always consist of at
least two elements.

But this doesn't apply to the json frontend which just uses an array:
it can be empty or only contain one element.

The included reproducer makes the eval stage set the "concatenation" flag
on the interval set.  This prevents the needed conversion code to turn the
element values into ranges from getting run.

The reproducer asserts with:
nft: src/intervals.c:786: setelem_to_interval: Assertion `key->etype == EXPR_RANGE_VALUE' failed.

Convert the assertion to BUG() so we can see what element type got passed
to the set interval code in case we have further issues in this area.

Reject 0-or-1-element concatenations from the json parser.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoevaluate: fix crash when generating reject statement error
Florian Westphal [Mon, 31 Mar 2025 12:43:34 +0000 (14:43 +0200)] 
evaluate: fix crash when generating reject statement error

commit 9b7346d1eac2eb90a2baf589affafec5b1a033b6 upstream.

After patch, this gets rejected with:
internal:0:0-0: Error: conflicting protocols specified: ip vs ip6

Without patch, we crash with a NULL dereference: we cannot use
reject.expr->location unconditionally.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agojson: disallow empty concatenation
Pablo Neira Ayuso [Thu, 27 Mar 2025 15:32:16 +0000 (16:32 +0100)] 
json: disallow empty concatenation

commit ab4ad2e12a059b31c4e65a8900e33bc88f2f5531 upstream.

Disallow empty concatenation in set declaration in json.

 internal:0:0-0: Error: Empty concatenation
 internal:0:0-0: Error: Invalid set type.
 internal:0:0-0: Error: Parsing command array at index 1 failed.

Joint work with Florian Westphal.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: don't try to import empty string
Florian Westphal [Thu, 27 Mar 2025 15:17:11 +0000 (16:17 +0100)] 
expression: don't try to import empty string

commit 8a9581d5d66ca2f920730441c82ac14fe9d9a427 upstream.

The bogon will trigger the assertion in mpz_import_data:
src/expression.c:418: constant_expr_alloc: Assertion `(((len) + (8) - 1) / (8)) > 0' failed.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>