]> git.ipfire.org Git - thirdparty/nftables.git/log
thirdparty/nftables.git
9 days agodoc: remove queue from verdict list
Florian Westphal [Sun, 26 Oct 2025 08:54:36 +0000 (09:54 +0100)] 
doc: remove queue from verdict list

While its correct that the queue statement is internally implemented
via the queue verdict, this is an implementation detail.
We don't list "stolen" as a verdict either.

nft ... queue will always use the nft_queue statement, so move the
reinject detail from statements to queue statement and remove this.

Signed-off-by: Florian Westphal <fw@strlen.de>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
11 days agotests: shell: fix typo in vmap_timeout test script
Gyorgy Sarvari [Sun, 26 Oct 2025 20:41:07 +0000 (21:41 +0100)] 
tests: shell: fix typo in vmap_timeout test script

While executing the test suite from tests/shell folder, the following error
is displayed many times:

tests/shell/testcases/maps/vmap_timeout: line 48: [: : integer expected

Looking at the script, a non-existing variable (expires) is tested instead of
the existing one (expire).

Reproduction:
tests/shell/run-tests.sh -v

Fixes: db80037c0279 ("tests: shell: extend vmap test with updates")
Signed-off-by: Gyorgy Sarvari <skandigraun@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
11 days agodoc: add more documentation on bitmasks and sets
Christoph Anton Mitterer [Fri, 24 Oct 2025 01:36:47 +0000 (03:36 +0200)] 
doc: add more documentation on bitmasks and sets

Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
11 days agodoc: fix/improve documentation of jump/goto/return
Christoph Anton Mitterer [Fri, 24 Oct 2025 01:36:45 +0000 (03:36 +0200)] 
doc: fix/improve documentation of jump/goto/return

Overhaul the description of `jump`/`goto`/`return`.
`jump` only explains what the statement causes from the point of view of the
new chain (that is: not, how the returning works), which includes that an
implicit `return` is issued at the end of the chain.
`goto` is explained in reference to `jump`.
`return` describes abstractly how the return position is determined and what
happens if there’s no position to return to (but not for example where an
implicit `return` is issued).

List and explain verdict-like statements like `reject` which internally imply
`accept` or `drop`.
Further explain that with respect to evaluation these behave like their
respectively implied verdicts.

Link: https://lore.kernel.org/netfilter-devel/3c7ddca7029fa04baa2402d895f3a594a6480a3a.camel@scientia.org/T/#t
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
13 days agoevaluate: follow prefix expression recursively if needed
Florian Westphal [Fri, 17 Oct 2025 11:38:34 +0000 (13:38 +0200)] 
evaluate: follow prefix expression recursively if needed

Included bogons assert:
Assertion `!expr_is_constant(*expr) || expr_is_singleton(*expr)' failed

This is because the "foo*" + prefix combination causes expr_evaluate
to replace the binop + string expression with another prefix that
gets allocated while handling "foo*" (wildcard).

This causes expr_evaluate_prefix to build
a prefix -> prefix -> binop chain.

After this, we get:

Error: Right hand side of relational expression ((null)) must be constant
a b ct helper "2.2.2.2.3*1"/80
    ~~~~~~~~~~^^^^^^^^^^^^^^^^
Error: Binary operation (&) is undefined for prefix expressions
a b ct helper "2.2.2.****02"/80
              ^^^^^^^^^^^^^^^^^

for those inputs rather than hitting assert() in byteorder_conversion()
later on.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 weeks agonetlink: Zero nft_data_linearize objects when populating
Phil Sutter [Fri, 17 Oct 2025 07:29:47 +0000 (09:29 +0200)] 
netlink: Zero nft_data_linearize objects when populating

Callers of netlink_gen_{key,data}() pass an uninitialized auto-variable,
avoid misinterpreting garbage in fields "left blank".

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: ip6/vmap.t: Drop double whitespace in rule
Phil Sutter [Thu, 16 Oct 2025 10:30:47 +0000 (12:30 +0200)] 
tests: py: ip6/vmap.t: Drop double whitespace in rule

Just a harmless typo.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agodatatype: Increase symbolic constant printer robustness
Phil Sutter [Fri, 10 Oct 2025 12:14:29 +0000 (14:14 +0200)] 
datatype: Increase symbolic constant printer robustness

Do not segfault if passed symbol table is NULL.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agonetlink: No need to reference array when passing as pointer
Phil Sutter [Fri, 17 Oct 2025 14:11:14 +0000 (16:11 +0200)] 
netlink: No need to reference array when passing as pointer

Struct nft_data_linearize::value is an array, drop the reference
operator.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: Do not rely upon '[end]' marker
Phil Sutter [Wed, 15 Oct 2025 22:00:05 +0000 (00:00 +0200)] 
tests: py: Do not rely upon '[end]' marker

Set element lines reliably start with whitespace followed by the word "element"
and are separated by the same pattern. Use it instead of '[end]' (or anything
enclosed in brackets).

While at it, recognize payload lines as starting with '  [ ' and avoid
searching for the closing bracket.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: Implement payload_record()
Phil Sutter [Wed, 10 Sep 2025 13:14:23 +0000 (15:14 +0200)] 
tests: py: Implement payload_record()

This is a helper function to store payload records (and JSON
equivalents) in .got files. The code it replaces missed to insert a
newline before the new entry and also did not check for existing records
in all spots.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: Fix for using wrong payload path
Phil Sutter [Thu, 2 Oct 2025 11:22:56 +0000 (13:22 +0200)] 
tests: py: Fix for using wrong payload path

If one family has a per-family payload record, following families used
it by accident for a .got file when they actually should use the generic
name.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: inet/osf.t: Fix element ordering in JSON equivalents
Phil Sutter [Thu, 9 Oct 2025 00:06:54 +0000 (02:06 +0200)] 
tests: py: inet/osf.t: Fix element ordering in JSON equivalents

The original rules order set elements differently. Stick to that and add
entries to inet/osf.t.json.output to cover for nftables reordering
entries.

Fixes: 92029c1282958 ("src: osf: add json support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: any/ct.t.json.output: Drop leftover entry
Phil Sutter [Thu, 16 Oct 2025 14:23:12 +0000 (16:23 +0200)] 
tests: py: any/ct.t.json.output: Drop leftover entry

The rule with single element anonymous set was replaced, drop this
leftover.

Fixes: 27f6a4c68b4fd ("tests: replace single element sets")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agotests: py: any/tcpopt.t.json: Fix JSON equivalent
Phil Sutter [Wed, 8 Oct 2025 21:46:34 +0000 (23:46 +0200)] 
tests: py: any/tcpopt.t.json: Fix JSON equivalent

Set element ordering differed from the rule in standard syntax.

Fixes: d199cca92f9eb ("expression: expr_build_udata_recurse should recurse")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agooptimize: Fix verdict expression comparison
Phil Sutter [Wed, 22 Oct 2025 12:03:37 +0000 (14:03 +0200)] 
optimize: Fix verdict expression comparison

In verdict expression, 'chain' points at a constant expression of
verdict_type, not a symbol expression. Therefore 'chain->identifier'
points eight bytes (on 64bit systems) into the mpz_t 'value' holding the
chain name. This matches the '_mp_d' data pointer, so works by accident.

Fix this by copying what verdict_jump_chain_print() does and export
chain names before comparing.

Fixes: fb298877ece27 ("src: add ruleset optimization infrastructure")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agodatatype: Fix boolean type on Big Endian
Phil Sutter [Wed, 8 Oct 2025 21:19:08 +0000 (23:19 +0200)] 
datatype: Fix boolean type on Big Endian

Pass a reference to a variable with correct size when creating the
expression, otherwise mpz_import_data() will read only the always zero
upper byte on Big Endian hosts.

Fixes: afb6a8e66a111 ("datatype: clamp boolean value to 0 and 1")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 weeks agosrc: parser_json: fix format string bugs
Florian Westphal [Thu, 23 Oct 2025 12:17:00 +0000 (14:17 +0200)] 
src: parser_json: fix format string bugs

After adding fmt attribute annotation:
warning: format not a string literal and no format arguments [-Wformat-security]
  131 |         erec_queue(error(&loc, err->text), ctx->msgs);
In function 'json_events_cb':
warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type '__u32' {aka 'unsigned int'} [-Wformat=]

Fix that up too.

Fixes: 586ad210368b ("libnftables: Implement JSON parser")
Signed-off-by: Florian Westphal <fw@strlen.de>
2 weeks agosrc: fix fmt string warnings
Florian Westphal [Fri, 17 Oct 2025 08:38:25 +0000 (10:38 +0200)] 
src: fix fmt string warnings

for some reason several functions had a __gmp_fmtstring annotation,
but that was an empty macro.

After fixing it up, we get several new warnings:

In file included from src/datatype.c:28:
src/datatype.c:174:24: note: in expansion of macro 'error'
  174 |                 return error(&sym->location,
      |                        ^~~~~
src/datatype.c:405:24: note: in expansion of macro 'error'
  405 |                 return error(&sym->location, "Could not parse %s; did you mean `%s'?",
      |                        ^~~~~

Fmt string says '%s', but unqailified void *, add 'const char *' cast,
it is safe in both cases.

In file included from src/evaluate.c:29:
src/evaluate.c: In function 'byteorder_conversion':
src/evaluate.c:232:35: warning: format '%s' expects a matching 'char *' argument [-Wformat=]
  232 |                                   "Byteorder mismatch: %s expected %s, %s got %s",
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Actual bug, fmt string has one '%s' too many, remove it.

All other warnings were due to '%u' instead of '%lu' / '%zu'.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 weeks agodoc: describe include’s collation order to be that of the C locale
Christoph Anton Mitterer [Mon, 20 Oct 2025 23:49:04 +0000 (01:49 +0200)] 
doc: describe include’s collation order to be that of the C locale

Currently, `nft` doesn’t call `setlocale(3)` and thus `glob(3)` uses the `C`
locale.

Document this as it’s possibly relevant to the ordering of included rules.

This also makes the collation order “official” so any future localisation would
need to adhere to that.

Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 weeks agodoc: minor improvements with respect to the term “ruleset”
Christoph Anton Mitterer [Sun, 19 Oct 2025 01:38:09 +0000 (03:38 +0200)] 
doc: minor improvements with respect to the term “ruleset”

Statements are elements of rules. Non-terminal statement are in particular
passive with respect to their rules (and thus automatically with respect to the
whole ruleset).

In “Continue ruleset evaluation”, it’s not necessary to mention the ruleset as
it’s obvious that the evaluation of the current chain will be continued.

Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 weeks agoevaluate: reject tunnel section if another one is already present
Florian Westphal [Thu, 16 Oct 2025 14:59:36 +0000 (16:59 +0200)] 
evaluate: reject tunnel section if another one is already present

Included bogon causes a crash because the list head isn't initialised
due to tunnel->type == VXLAN.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
2 weeks agosrc: parser_bison: prevent multiple ip daddr/saddr definitions
Florian Westphal [Thu, 16 Oct 2025 14:59:35 +0000 (16:59 +0200)] 
src: parser_bison: prevent multiple ip daddr/saddr definitions

minor change to the bogon makes it assert because symbolic expression
will have wrong refcount (2) at scope teardown.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
2 weeks agosrc: tunnel src/dst must be a symbolic expression
Florian Westphal [Thu, 16 Oct 2025 14:59:34 +0000 (16:59 +0200)] 
src: tunnel src/dst must be a symbolic expression

Included bogons crash with segfault and assertion.  After fix:

tunnel_with_garbage_dst:3:12-14: Error: syntax error, unexpected tcp, expecting string or quoted string or string with a trailing asterisk or '$'
  ip saddr tcp dport { }
           ^^^
The parser change restricts the grammar to no longer allow this,
we would crash here because we enter payload evaluation path that
tries to insert a dependency into the rule, but we don't have one
(ctx->rule and ctx->stmt are NULL as expected here).

The eval stage change makes sure we will reject non-value symbols:

tunnel_with_anon_set_assert:1:12-31: Error: must be a value, not set
define s = { 1.2.3.4, 5.6.7.8 }
           ^^^^^^^^^^^^^^^^^^^^

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
2 weeks agoevaluate: tunnel: don't assume src is set
Florian Westphal [Thu, 16 Oct 2025 14:59:33 +0000 (16:59 +0200)] 
evaluate: tunnel: don't assume src is set

Included bogon crashes, after fix:

empty_geneve_definition_crash:2:9-16: Error: Could not process rule: Invalid argument

Since this feature is undocumented (hint, hint) I don't know
if there are cases where ip daddr can be elided.

If not, a followup patch should reject empty dst upfront
so users get a more verbose error message.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
3 weeks agotests: shell: add packetpath test for reject statement
Florian Westphal [Wed, 20 Aug 2025 13:20:18 +0000 (15:20 +0200)] 
tests: shell: add packetpath test for reject statement

Test case for:
91a79b792204 ("netfilter: nf_reject: don't leak dst refcount for loopback packets")
and
db99b2f2b3e2 ("netfilter: nf_reject: don't reply to icmp error messages")

Signed-off-by: Florian Westphal <fw@strlen.de>
3 weeks agodoc: clarify evaluation of chains
Christoph Anton Mitterer [Sat, 11 Oct 2025 00:23:57 +0000 (02:23 +0200)] 
doc: clarify evaluation of chains

In particular:
- Mention that grouping of chains in tables is irrelevant to the evaluation
  order.
- Clarify that priorities only define the ordering of chains per hook.
- Improved potentially ambiguous wording “lower priority values have precedence
  over higher ones”, which could be mistaken as that rules from lower priority
  chains might “win” over such from higher ones (which is however only the case
  if they drop/reject packets).
  The new wording merely describes which chains are evaluated first, implicitly
  referring the question which verdict “wins” to the section where verdicts are
  described, and also should work when lower priority chains mangle packets (in
  which case they might actually be considered as having “precedence”).

Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 weeks agotests: shell: add packetpath test for meta ibrhwaddr
Fernando Fernandez Mancera [Tue, 14 Oct 2025 12:21:28 +0000 (14:21 +0200)] 
tests: shell: add packetpath test for meta ibrhwaddr

The test checks that the packets are processed by the bridge device and
not forwarded.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 weeks agometa: introduce meta ibrhwaddr support
Fernando Fernandez Mancera [Tue, 14 Oct 2025 12:20:42 +0000 (14:20 +0200)] 
meta: introduce meta ibrhwaddr support

Can be used in bridge prerouting hook to redirect the packet to the
receiving physical device for processing.

table bridge nat {
        chain PREROUTING {
                type filter hook prerouting priority 0; policy accept;
                ether daddr de:ad:00:00:be:ef meta pkttype set host ether daddr set meta ibrhwaddr accept
        }
}

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 weeks agodoc: fix tcpdump example
Georg Pfuetzenreuter [Mon, 13 Oct 2025 17:17:31 +0000 (19:17 +0200)] 
doc: fix tcpdump example

The expression needs to be enclosed in a single string and combined with
a logical AND to have the desired effect.

Fixes: 1188a69604c3 ("src: introduce SYNPROXY matching")
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
Reviewed-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agotests: shell: type_route_chain: use in-tree nftables, not system-wide one
Florian Westphal [Tue, 7 Oct 2025 20:26:51 +0000 (22:26 +0200)] 
tests: shell: type_route_chain: use in-tree nftables, not system-wide one

Switch this to $NFT, which contains the locally-compiled binary.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agotests: shell: fix name based checks with CONFIG_MODULES=n
Florian Westphal [Tue, 7 Oct 2025 16:21:13 +0000 (18:21 +0200)] 
tests: shell: fix name based checks with CONFIG_MODULES=n

Don't include a trailing space, its only there if nftables is a module:

  hook ingress device foo2 {
     0000000000 chain netdev t c [nf_tables]
  }

with CONFIG_NF_TABLES=y, this gets listed as:
'0000000000 chain netdev t c\n'.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agomnl: Drop asterisk from end of NFTA_DEVICE_PREFIX strings
Phil Sutter [Tue, 7 Oct 2025 15:51:32 +0000 (17:51 +0200)] 
mnl: Drop asterisk from end of NFTA_DEVICE_PREFIX strings

The asterisk left in place becomes part of the prefix by accident and is thus
both included when matching interface names as well as dumped back to user
space.

Fixes: c31e887504a90 ("mnl: Support simple wildcards in netdev hooks")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agotests: shell: add regression tests for set flush+add bugs
Florian Westphal [Tue, 16 Sep 2025 16:52:53 +0000 (18:52 +0200)] 
tests: shell: add regression tests for set flush+add bugs

Create a helper file to:
1. create client <-> router <-> server topology
2. floodping from client to server
3. add a chain + set that contains both client and server
   addresses
4. a control counter that should never match
5. then, flush the set (not the ruleset) and re-add the
   addresses in one transaction

Report failure when counter had a match.

The test cases for the set types are done in separate files to take
advantage of run-tests.sh parallelization.

The expected behavior is that every ping packet is matched by the set.
The packet path should either match the old state, right before flush,
or the new state, after re-add.

As the flushed addresses are re-added in the same transaction we must
not observe in-limbo state where existing elements are deactivated but
new elements are not found.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 weeks agosrc: tunnel: handle tunnel delete command
Florian Westphal [Tue, 7 Oct 2025 11:06:31 +0000 (13:06 +0200)] 
src: tunnel: handle tunnel delete command

'delete tunnel foo bar' causes nft to bug out.

Fixes: 35d9c77c5745 ("src: add tunnel template support")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 weeks agotests: py: objects.t: must use input, not output
Florian Westphal [Tue, 7 Oct 2025 10:48:49 +0000 (12:48 +0200)] 
tests: py: objects.t: must use input, not output

synproxy must never be used in output rules, doing so results in kernel
crash due to infinite recursive calls back to nf_hook_slow() for the
emitted reply packet.

Up until recently kernel lacked this validation, and now that the kernel
rejects this the test fails.  Use input to make this pass again.

A new test to ensure we reject synproxy in ouput should be added
in the near future.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 weeks agotests: shell: Test ifname-based hooks
Phil Sutter [Tue, 15 Jul 2025 13:26:33 +0000 (15:26 +0200)] 
tests: shell: Test ifname-based hooks

Assert that:
- Non-matching interface specs are accepted
- Existing interfaces are hooked into upon flowtable/chain creation
- A new device matching the spec is hooked into immediately
- No stale hooks remain in 'nft list hooks' output
- Wildcard hooks basically work

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 weeks agoparser_bison: Accept ASTERISK_STRING in flowtable_expr_member
Phil Sutter [Tue, 13 Aug 2024 18:36:36 +0000 (20:36 +0200)] 
parser_bison: Accept ASTERISK_STRING in flowtable_expr_member

All clauses are identical, so instead of adding a third one for
ASTERISK_STRING, use a single one for 'string' (which combines all three
variants).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 weeks agomnl: Support simple wildcards in netdev hooks
Phil Sutter [Tue, 13 Aug 2024 18:26:08 +0000 (20:26 +0200)] 
mnl: Support simple wildcards in netdev hooks

When building NFTA_{FLOWTABLE_,}HOOK_DEVS attributes, detect trailing
asterisks in interface names and transmit the leading part in a
NFTA_DEVICE_PREFIX attribute.

Deserialization (i.e., appending asterisk to interface prefixes returned
in NFTA_DEVICE_PREFIX atributes happens in libnftnl.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
6 weeks agolibnftables: do not re-add default include directory in include search path
Pablo Neira Ayuso [Wed, 24 Sep 2025 21:54:12 +0000 (23:54 +0200)] 
libnftables: do not re-add default include directory in include search path

Otherwise globbing might duplicate included files because
include_path_glob() is called twice.

Fixes: 7eb950a8e8fa ("libnftables: include canonical path to avoid duplicates")
Tested-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agofib: Fix for existence check on Big Endian
Phil Sutter [Tue, 9 Sep 2025 20:27:19 +0000 (22:27 +0200)] 
fib: Fix for existence check on Big Endian

Adjust the expression size to 1B so cmp expression value is correct.
Without this, the rule 'fib saddr . iif check exists' generates
following byte code on BE:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x00000001 ]

Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first
byte of reg 1 only (using nft_reg_store8()). With this patch in place,
byte code is correct:

|  [ fib saddr . iif oif present => reg 1 ]
|  [ cmp eq reg 1 0x01000000 ]

Fixes: f686a17eafa0b ("fib: Support existence check")
Cc: Yi Chen <yiche@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agoMakefile: Enable support for 'make check'
Phil Sutter [Fri, 8 Sep 2023 16:16:29 +0000 (18:16 +0200)] 
Makefile: Enable support for 'make check'

With all test suites running all variants by default, add the various
testsuite runners to TESTS variable so 'make check' will execute them.

Introduce --enable-distcheck configure flag for internal use during
builds triggered by 'make distcheck'. This flag will force TESTS
variable to remain empty, so 'make check' run as part of distcheck will
not call any test suite: Most of the test suites require privileged
execution, 'make distcheck' usually doesn't and probably shouldn't.
Assuming the latter is used during the release process, it may even not
run on a machine which is up to date enough to generate meaningful test
suite results. Hence spare the release process from the likely pointless
delay imposed by 'make check'.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: build: Avoid a recursive 'make check' run
Phil Sutter [Thu, 4 Sep 2025 11:47:21 +0000 (13:47 +0200)] 
tests: build: Avoid a recursive 'make check' run

When called by 'make check', the test suite runs with a MAKEFLAGS
variable in environment which defines TEST_LOGS variable with the test
suites' corresponding logs as value. This in turn causes the called
'make distcheck' to run test suites although it is not supposed to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: build: Do not assume caller's CWD
Phil Sutter [Tue, 26 Aug 2025 20:57:36 +0000 (22:57 +0200)] 
tests: build: Do not assume caller's CWD

Cover for being called from a different directory by changing into the
test suite's directory first.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: shell: Skip packetpath/nat_ftp in fake root env
Phil Sutter [Fri, 1 Aug 2025 15:21:41 +0000 (17:21 +0200)] 
tests: shell: Skip packetpath/nat_ftp in fake root env

The script relies upon a call to modprobe which does not work in
fake root environments.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: json_echo: Skip if run as non-root
Phil Sutter [Thu, 31 Aug 2023 10:50:54 +0000 (12:50 +0200)] 
tests: json_echo: Skip if run as non-root

The test suite manipulates the kernel ruleset. Use the well-known return
code 77 to indicate test execution being skipped.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: Prepare exit codes for automake
Phil Sutter [Thu, 31 Aug 2023 10:44:55 +0000 (12:44 +0200)] 
tests: Prepare exit codes for automake

Make the test suite runners exit 77 when requiring root and running as
regular user, exit 99 for internal errors (unrelated to test cases) and
exit 1 (or any free non-zero value) to indicate test failures.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: py: Enable JSON and JSON schema by default
Phil Sutter [Wed, 3 Sep 2025 16:20:42 +0000 (18:20 +0200)] 
tests: py: Enable JSON and JSON schema by default

Introduce -J/--disable-json and -S/--no-schema to explicitly disable
them if desired.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: monitor: Excercise all syntaxes and variants by default
Phil Sutter [Wed, 3 Sep 2025 15:41:23 +0000 (17:41 +0200)] 
tests: monitor: Excercise all syntaxes and variants by default

Introduce -s/--standard flag to restrict execution to standard syntax
and let users select a specific variant by means of -e/--echo and
-m/--monitor flags. Run all four possible combinations by default.

To keep indenting sane, introduce run_testcase() executing tests in a
single test case for a given syntax and variant.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agotests: monitor: Extend testcases a bit
Phil Sutter [Thu, 28 Aug 2025 23:49:01 +0000 (01:49 +0200)] 
tests: monitor: Extend testcases a bit

Try to cover for reduced table and chain deletion notifications by
creating them with data which is omitted by the kernel during deletion.

Also try to expose the difference in reported flowtable hook deletion
vs. flowtable deletion.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agomonitor: Inform JSON printer when reporting an object delete event
Phil Sutter [Thu, 28 Aug 2025 23:07:05 +0000 (01:07 +0200)] 
monitor: Inform JSON printer when reporting an object delete event

Since kernel commit a1050dd07168 ("netfilter: nf_tables: Reintroduce
shortened deletion notifications"), type-specific data is no longer
dumped when notifying for a deleted object. JSON output was not aware of
this and tried to print bogus data.

Fixes: 9e88aae28e9f4 ("monitor: Use libnftables JSON output")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agomnl: Allow for updating devices on existing inet ingress hook chains
Phil Sutter [Thu, 28 Aug 2025 16:01:29 +0000 (18:01 +0200)] 
mnl: Allow for updating devices on existing inet ingress hook chains

Complete commit a66b5ad9540dd ("src: allow for updating devices on
existing netdev chain") in supporting inet family ingress hook chains as
well. The kernel does already but nft has to add a proper hooknum
attribute to pass the checks.

Calling chain_evaluate() for populating the hook.num field is a bit over
the top and has potentially unwanted side-effects. Introduce a minimal
chain_del_evaluate() for this purpose.

Signed-off-by: Phil Sutter <phil@nwl.cc>
8 weeks agoMakefile: Fix for 'make CFLAGS=...'
Phil Sutter [Mon, 8 Sep 2025 22:14:16 +0000 (00:14 +0200)] 
Makefile: Fix for 'make CFLAGS=...'

Appending to CFLAGS from configure.ac like this was too naive, passing
custom CFLAGS in make arguments overwrites it. Extend AM_CFLAGS instead.

Fixes: 64c07e38f0494 ("table: Embed creating nft version into userdata")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agotests: shell: skip two bitwise tests if multi-register support isn't available
Florian Westphal [Mon, 8 Sep 2025 09:06:35 +0000 (11:06 +0200)] 
tests: shell: skip two bitwise tests if multi-register support isn't available

These tests fail in case kernel requires bitwise RHS to be a constant
value.

Fixes: 67d2a8d4c86f ("tests: shell: add parser and packetpath test")
Reported-by: Yi Chen <yiche@redhat.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: monitor: Extend debug output a bit
Phil Sutter [Wed, 3 Sep 2025 13:46:45 +0000 (15:46 +0200)] 
tests: monitor: Extend debug output a bit

Dump echo output and output file, surrounded by markers to highlight
empty files and extra newlines.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: monitor: Test JSON echo mode as well
Phil Sutter [Wed, 3 Sep 2025 13:30:31 +0000 (15:30 +0200)] 
tests: monitor: Test JSON echo mode as well

Reuse the expected JSON monitor output for --echo testing as it is
supposed to be "identical" - apart from formatting differences. To match
lines of commands (monitor output) against a single line of JSON object
(echo output), join the former's lines and drop the surrounding object
in the latter since this seems to be the simplest way.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: monitor: Fix regex collecting expected echo output
Phil Sutter [Wed, 3 Sep 2025 13:23:12 +0000 (15:23 +0200)] 
tests: monitor: Fix regex collecting expected echo output

No input triggered this bug, but the match would accept "insert" and
"replace" keywords anywhere in the line not just at the beginning as was
intended.

Fixes: b2506e5504fed ("tests: Merge monitor and echo test suites")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotests: monitor: Label diffs to help users
Phil Sutter [Wed, 3 Sep 2025 13:01:09 +0000 (15:01 +0200)] 
tests: monitor: Label diffs to help users

Clarify what was expected and what was actually received.

Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agomonitor: Quote device names in chain declarations, too
Phil Sutter [Thu, 28 Aug 2025 14:47:03 +0000 (16:47 +0200)] 
monitor: Quote device names in chain declarations, too

Fixed commit missed the fact that there are two routines printing chain
declarations.

Fixes: eb30f236d91a8 ("rule: print chain and flowtable devices in quotes")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agotools: gitignore nftables.service file
Phil Sutter [Thu, 28 Aug 2025 23:51:01 +0000 (01:51 +0200)] 
tools: gitignore nftables.service file

Fixes: c4b17cf830510 ("tools: add a systemd unit for static rulesets")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agoparser_bison: remove leftover utf-8 character in error
Pablo Neira Ayuso [Sun, 31 Aug 2025 21:37:51 +0000 (23:37 +0200)] 
parser_bison: remove leftover utf-8 character in error

replace "‘" (UTF-8, 0xe280 0x98) with "'" (ASCII 0x27).

Fixes: c92ec3b21979 ("src: remove utf-8 character in printf lines")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotable: Embed creating nft version into userdata
Phil Sutter [Mon, 12 May 2025 20:59:26 +0000 (22:59 +0200)] 
table: Embed creating nft version into userdata

Upon listing a table which was created by a newer version of nftables,
warn about the potentially incomplete content.

Suggested-by: Florian Westphal <fw@strlen.de>
Cc: Dan Winship <danwinship@redhat.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: shell: combine flowtable devices with variable expression
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:17:41 +0000 (11:17 +0200)] 
tests: shell: combine flowtable devices with variable expression

Expand test with flowtable devices defined with variables to improve
coverage.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoevaluate: simplify set to list normalisation for device expressions
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:17:40 +0000 (11:17 +0200)] 
evaluate: simplify set to list normalisation for device expressions

When evaluating the list of devices, two expressions are possible:

- EXPR_LIST, which is the expected expression type to store the list of
  chain/flowtable devices.

- EXPR_SET, in case that a variable is used to express the device list.
  This is because it is not possible to know if the variable defines
  set elements or devices. Since sets are more common, EXPR_SET is used.

In the latter case, this list expressed as EXPR_SET gets translated to
EXPR_LIST. Before such translation, the EXPR_VARIABLE is evaluated,
therefore all variables are gone and only EXPR_SET_ELEM are possible in
expr_set_to_list().

Remove the EXPR_VALUE and EXPR_VARIABLE cases in expr_set_to_list()
since those are never seen. Add BUG() in case any other expressions than
EXPR_SET_ELEM is seen.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: replace compound_expr_alloc() by type safe function
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:21:23 +0000 (15:21 +0200)] 
src: replace compound_expr_alloc() by type safe function

Replace compound_expr_alloc() by {set,list,concat}_expr_alloc() to
validate expression type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: replace compound_expr_print() by type safe function
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:20:43 +0000 (15:20 +0200)] 
expression: replace compound_expr_print() by type safe function

Replace compound_expr_print() by {list,set,concat}_expr_print() to
validate expression type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: replace compound_expr_destroy() by type safe funtion
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:20:30 +0000 (15:20 +0200)] 
expression: replace compound_expr_destroy() by type safe funtion

Replace it by {set,list,concat}_expr_destroy() to validate type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: replace compound_expr_remove() by type safe function
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:19:56 +0000 (15:19 +0200)] 
expression: replace compound_expr_remove() by type safe function

Replace this function by {list,concat,set}_expr_remove() to validate
expression type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: remove compound_expr_add()
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:16:22 +0000 (15:16 +0200)] 
expression: remove compound_expr_add()

No more users of this function after conversion to type safe variant,
remove it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoexpression: replace compound_expr_clone() by type safe function
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:15:48 +0000 (15:15 +0200)] 
expression: replace compound_expr_clone() by type safe function

Replace compound_expr_clone() by:

- concat_expr_clone()
- list_expr_clone()
- set_expr_clone()

to validate type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosegtree: rename set_compound_expr_add() to set_expr_add_splice()
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:15:24 +0000 (15:15 +0200)] 
segtree: rename set_compound_expr_add() to set_expr_add_splice()

To avoid confusion when perfoming git grep to search for compound_expr_add()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: replace compound_expr_add() by type safe list_expr_add()
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:15:08 +0000 (15:15 +0200)] 
src: replace compound_expr_add() by type safe list_expr_add()

Replace compound_expr_add() by list_expr_add() to validate type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: replace compound_expr_add() by type safe concat_expr_add()
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:14:48 +0000 (15:14 +0200)] 
src: replace compound_expr_add() by type safe concat_expr_add()

Replace compound_expr_add by concat_expr_add() to validate type.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: replace compound_expr_add() by type safe set_expr_add()
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:14:13 +0000 (15:14 +0200)] 
src: replace compound_expr_add() by type safe set_expr_add()

Replace compound_expr_add() by set_expr_add() to validate type.

Add __set_expr_add() to skip size updates in src/intervals.c

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: add expr_type_catchall() helper and use it
Pablo Neira Ayuso [Wed, 13 Aug 2025 13:11:09 +0000 (15:11 +0200)] 
src: add expr_type_catchall() helper and use it

Add helper function to check if this is a catchall expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: add tunnel shell and python tests
Fernando Fernandez Mancera [Thu, 21 Aug 2025 09:13:02 +0000 (11:13 +0200)] 
tests: add tunnel shell and python tests

Add tests for tunnel statement and object support. Shell and python
tests both cover standard nft output and json.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotunnel: add tunnel object and statement json support
Fernando Fernandez Mancera [Thu, 21 Aug 2025 09:13:01 +0000 (11:13 +0200)] 
tunnel: add tunnel object and statement json support

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotunnel: add geneve support
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:13:00 +0000 (11:13 +0200)] 
tunnel: add geneve support

This patch extends the tunnel metadata object to define geneve tunnel
specific configurations:

table netdev x {
tunnel y {
id 10
ip saddr 192.168.2.10
ip daddr 192.168.2.11
sport 10
dport 20
ttl 10
geneve {
class 0x1010 opt-type 0x1 data "0x12345678"
class 0x1020 opt-type 0x2 data "0x87654321"
class 0x2020 opt-type 0x3 data "0x87654321abcdeffe"
}
}
}

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotunnel: add vxlan support
Fernando Fernandez Mancera [Thu, 21 Aug 2025 09:12:59 +0000 (11:12 +0200)] 
tunnel: add vxlan support

This patch extends the tunnel metadata object to define vxlan tunnel
specific configurations:

table netdev x {
tunnel y {
id 10
ip saddr 192.168.2.10
ip daddr 192.168.2.11
sport 10
dport 20
ttl 10
vxlan {
gbp 200
}
}
}

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: add tunnel statement and expression support
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:12:58 +0000 (11:12 +0200)] 
src: add tunnel statement and expression support

This patch allows you to attach tunnel metadata through the tunnel
statement.

The following example shows how to redirect traffic to the erspan0
tunnel device which will take the tunnel configuration that is
specified by the ruleset.

     table netdev x {
            tunnel y {
                    id 10
                    ip saddr 192.168.2.10
                    ip daddr 192.168.2.11
                    sport 10
                    dport 20
                    ttl 10
                    erspan {
                            version 1
                            index 2
                    }
            }

    chain x {
    type filter hook ingress device veth0 priority 0;

    ip daddr 10.141.10.123 tunnel name y fwd to erspan0
    }
     }

This patch also allows to match on tunnel metadata via tunnel expression.

Joint work with Fernando.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotunnel: add erspan support
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:12:57 +0000 (11:12 +0200)] 
tunnel: add erspan support

This patch extends the tunnel metadata object to define erspan tunnel
specific configurations:

 table netdev x {
        tunnel y {
                id 10
                ip saddr 192.168.2.10
                ip daddr 192.168.2.11
                sport 10
                dport 20
                ttl 10
                erspan {
                        version 1
                        index 2
                }
        }
 }

Joint work with Fernando.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: add tunnel template support
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:12:56 +0000 (11:12 +0200)] 
src: add tunnel template support

This patch adds tunnel template support, this allows to attach a
metadata template that provides the configuration for the tunnel driver.

Example of generic tunnel configuration:

 table netdev x {
        tunnel y {
                id 10
                ip saddr 192.168.2.10
                ip daddr 192.168.2.11
                sport 10
                dport 20
                ttl 10
        }
 }

This still requires the tunnel statement to attach this metadata
template, this comes in a follow up patch.

Joint work with Fernando.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agobuild: Bump version to 1.1.5 v1.1.5
Pablo Neira Ayuso [Wed, 27 Aug 2025 10:59:13 +0000 (12:59 +0200)] 
build: Bump version to 1.1.5

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agobuild: disable --with-unitdir by default
Pablo Neira Ayuso [Wed, 27 Aug 2025 13:51:10 +0000 (15:51 +0200)] 
build: disable --with-unitdir by default

Same behaviour as in the original patch:

  --with-unitdir auto-detects the systemd unit path.
  --with-unitdir=PATH uses the PATH

no --with-unitdir means this does not install the systemd unit file.

INSTALL file description looks fine for what this does after this
patch.

While at this, extend tests/build/ to cover for this new option.

Fixes: c4b17cf830510 ("tools: add a systemd unit for static rulesets")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agoMakefile: Fix for 'make distcheck'
Phil Sutter [Tue, 26 Aug 2025 17:05:17 +0000 (19:05 +0200)] 
Makefile: Fix for 'make distcheck'

Make sure the files in tools/ are added to the tarball and that the
created nftables.service file is removed upon 'make clean'.

Fixes: c4b17cf830510 ("tools: add a systemd unit for static rulesets")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agomnl: continue on ENOBUFS errors when processing batch
Pablo Neira Ayuso [Tue, 26 Aug 2025 08:09:13 +0000 (10:09 +0200)] 
mnl: continue on ENOBUFS errors when processing batch

A user reports that:

  nft -f ruleset.nft

fails with:

  netlink: Error: Could not process rule: No buffer space available

This was triggered by:

table ip6 fule {
  set domestic_ip6 {
    type ipv6_addr
    flags dynamic,interval
    elements = $domestic_ip6
  }
  chain prerouting {
    type filter hook prerouting priority 0;
    ip6 daddr @domestic_ip6 counter
  }
}

where $domestic_ip6 contains a large number of IPv6 addresses.

This set declaration is not supported currently, because dynamic sets
with intervals are not supported, then every IPv6 address that is added
triggers an error, overruning the userspace socket buffer with lots of
NLMSG_ERROR messages (or too big NLMSG_ERROR message to fit into the
socket buffer).

In the particular context of batch processing, ENOBUFS is just an
indication that too many errors have occurred. The kernel cannot store
any more NLMSG_ERROR messages into the userspace socket buffer.

However, there are still NLMSG_ERROR messages in the socket buffer to be
processed that can provide a hint on what is going on.

Instead of breaking on ENOBUFS in batches, continue error processing.

After this patch, the ruleset above displays:

ruleset.nft:2367:7-18: Error: Could not process rule: Operation not supported
  set domestic_ip6 {
      ^^^^^^^^^^^^
ruleset.nft:2367:7-18: Error: Could not process rule: No such file or directory
  set domestic_ip6 {
      ^^^^^^^^^^^^

Fixes: a72315d2bad4 ("src: add rule batching support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotrace: Fix for memleak in trace_alloc_list() error path
Phil Sutter [Tue, 26 Aug 2025 10:57:37 +0000 (12:57 +0200)] 
trace: Fix for memleak in trace_alloc_list() error path

The allocated 'list_expr' may leak.

Fixes: cfd768615235b ("src: add conntrack information to trace monitor mode")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agotests: monitor: Fix for flag arrays in JSON output
Phil Sutter [Sat, 23 Aug 2025 21:57:28 +0000 (23:57 +0200)] 
tests: monitor: Fix for flag arrays in JSON output

Missed to adjust the expected JSON output in this test suite, too.

Fixes: 5e492307c2c93 ("json: Do not reduce single-item arrays on output")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agodoc: Add a note about route_localnet sysctl
Łukasz Stelmach [Thu, 21 Aug 2025 10:38:40 +0000 (12:38 +0200)] 
doc: Add a note about route_localnet sysctl

See ip_route_input_slow() in net/ipv4/route.c in the Linux
kernel sources.

Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agomnl: silence compiler warning
Florian Westphal [Wed, 20 Aug 2025 12:44:43 +0000 (14:44 +0200)] 
mnl: silence compiler warning

gcc 14.3.0 reports this:

src/mnl.c: In function 'mnl_nft_chain_add':
src/mnl.c:916:25: warning: 'nest' may be used uninitialized [-Wmaybe-uninitialized]
  916 |                         mnl_attr_nest_end(nlh, nest);

I guess its because compiler can't know that the conditions cannot change
in-between and assumes nest_end() can be called without nest_start().

Fixes: 01277922fede ("src: ensure chain policy evaluation when specified")
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: shell: coverage for simple verdict map merger
Pablo Neira Ayuso [Wed, 20 Aug 2025 11:17:22 +0000 (13:17 +0200)] 
tests: shell: coverage for simple verdict map merger

Add a testcase to cover for merging two rules into verdict map, added by

  345d9260f7fe ("optimize: merge several selectors with different verdict into verdict map").

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: shell: cover sets as set elems evaluation
Pablo Neira Ayuso [Wed, 20 Aug 2025 11:06:30 +0000 (13:06 +0200)] 
tests: shell: cover sets as set elems evaluation

Extend tests/shell coverage to exercise merging nested sets, provided
by fixes such as:

  a6b75b837f5e ("evaluate: set: Allow for set elems to be sets")

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agofib: restore JSON output for relational expressions
Pablo Neira Ayuso [Tue, 19 Aug 2025 09:23:42 +0000 (11:23 +0200)] 
fib: restore JSON output for relational expressions

JSON output for the fib expression changed:

-                    "result": "check"
+                    "result": "oif"

This breaks third party JSON parsers, revert this change for relational
expressions only via workaround until there are clear rules on how to
proceed with JSON schema updates.

As for set and map statements, keep this new "check" result type since
it is not possible to peek on rhs in such case to guess if the
NFT_FIB_F_PRESENT flag needs to be set on.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1806
Fixes: f4b646032acf ("fib: allow to check if route exists in maps")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotools: add a systemd unit for static rulesets
Jan Engelhardt [Thu, 17 Apr 2025 14:48:33 +0000 (16:48 +0200)] 
tools: add a systemd unit for static rulesets

There is a customer request (bugreport) for wanting to trivially load a ruleset
from a well-known location on boot, forwarded to me by M. Gerstner. A systemd
service unit is hereby added to provide that functionality. This is based on
various distributions attempting to do same, for example,

https://src.fedoraproject.org/rpms/nftables/tree/rawhide
https://gitlab.alpinelinux.org/alpine/aports/-/blob/master/main/nftables/nftables.initd
https://gitlab.archlinux.org/archlinux/packaging/packages/nftables
Acked-by: Eric Garver <eric@garver.life>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: fix memory leak in anon chain error handling
Florian Westphal [Thu, 24 Jul 2025 10:22:02 +0000 (12:22 +0200)] 
src: fix memory leak in anon chain error handling

chain_stmt_destroy is called from bison destructor, but it turns out
this function won't free the associated chain.

There is no memory leak when bison can parse the input because the chain
statement evaluation step queues the embedded anon chain via cmd_alloc.
Then, a later cmd_free() releases the chain and the embedded statements.

In case of a parser error, the evaluation step is never reached and the
chain object leaks, e.g. in

  foo bar jump { return }

Bison calls the right destructor but the anonon chain and all
statements/expressions in it are not released:

HEAP SUMMARY:
    in use at exit: 1,136 bytes in 4 blocks
  total heap usage: 98 allocs, 94 frees, 840,255 bytes allocated

1,136 (568 direct, 568 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
   at: calloc (vg_replace_malloc.c:1675)
   by: xzalloc (in libnftables.so.1.1.0)
   by: chain_alloc (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)

To resolve this, make chain_stmt_destroy also release the embedded
chain.  This in turn requires chain refcount increases whenever a chain
is assocated with a chain statement, else we get double-free of the
chain.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agosrc: ensure chain policy evaluation when specified
Pablo Neira Ayuso [Sun, 17 Aug 2025 19:01:30 +0000 (21:01 +0200)] 
src: ensure chain policy evaluation when specified

Set on CHAIN_F_BASECHAIN when policy is specified in chain, otherwise
chain priority is not evaluated.

Toggling this flag requires needs three adjustments to work though:

1) chain_evaluate() needs skip evaluation of hook name and priority if
   not specified to allow for updating the default chain policy, e.g.

chain ip x y { policy accept; }

2) update netlink bytecode generation for chain to skip NFTA_CHAIN_HOOK
   so update path is exercised in the kernel.

3) error reporting needs to check if basechain priority and type is
   set on, otherwise skip further hints.

Fixes: acdfae9c3126 ("src: allow to specify the default policy for base chains")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months 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

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>
2 months agojson: Do not reduce single-item arrays on output
Phil Sutter [Tue, 12 Aug 2025 15:31:47 +0000 (17:31 +0200)] 
json: Do not reduce single-item arrays on output

This is a partial revert of commit a740f2036ad0d ("json: Introduce
json_add_array_new()"), keeping the function but eliminating its primary
task which is to replace arrays of size 1 by their only item. While
support for this on input is convenient for users, it means extra casing
in JSON output parsers to cover for it. The minor reduction in output
size does not justify that.

Fixes: a740f2036ad0d ("json: Introduce json_add_array_new()")
Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1806
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Fix tests added for 'icmpv6 taddr' support
Phil Sutter [Wed, 13 Aug 2025 14:14:08 +0000 (16:14 +0200)] 
tests: py: Fix tests added for 'icmpv6 taddr' support

There was a duplicate test, also stored JSON equivalents should match
input as much as possible. The expected deviation in output (just like
with standard syntax) is stored in the .json.output file instead.

Fixes: 2e86f45d0260a ("icmpv6: Allow matching target address in NS/NA, redirect and MLD")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 months agotests: py: Drop stale entry from ip/snat.t.payload
Phil Sutter [Wed, 13 Aug 2025 14:06:46 +0000 (16:06 +0200)] 
tests: py: Drop stale entry from ip/snat.t.payload

This payload actually belongs to ip/dnat.t.payload, fixed commit added
it to the wrong file.

Fixes: 8f3048954d40d ("evaluate: postpone transport protocol match check after nat expression evaluation")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>