]> git.ipfire.org Git - thirdparty/libnftnl.git/log
thirdparty/libnftnl.git
11 days agobuild: libnftnl 1.3.1 release master libnftnl-1.3.1
Pablo Neira Ayuso [Wed, 3 Dec 2025 11:56:23 +0000 (11:56 +0000)] 
build: libnftnl 1.3.1 release

Bump libversion, because this release adds new nftnl_tunnel_opt API.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 weeks agoutils: Introduce nftnl_parse_str_attr()
Phil Sutter [Wed, 2 Oct 2024 14:44:49 +0000 (16:44 +0200)] 
utils: Introduce nftnl_parse_str_attr()

Wrap the common parsing of string attributes in a function. Apart from
slightly reducing code size, this unifies callers in conditional freeing
of the field in case it was set before (missing in twelve spots) and
error checking for failing strdup()-calls (missing in four spots).

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
8 weeks agoutils: Drop asterisk from end of NFTA_DEVICE_PREFIX strings
Phil Sutter [Tue, 7 Oct 2025 15:45:25 +0000 (17:45 +0200)] 
utils: 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: f30eae26d813e ("utils: Add helpers for interface name wildcards")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 months agoexpr: meta: introduce ibrhwaddr meta expression
Fernando Fernandez Mancera [Tue, 14 Oct 2025 12:18:51 +0000 (14:18 +0200)] 
expr: meta: introduce ibrhwaddr meta expression

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
2 months agoutils: Add helpers for interface name wildcards
Phil Sutter [Tue, 1 Oct 2024 10:59:29 +0000 (12:59 +0200)] 
utils: Add helpers for interface name wildcards

Support simple (suffix) wildcards in NFTNL_{CHAIN,FLOWTABLE}_DEVICES
identified by NFTA_DEVICE_PREFIX attribute. Add helpers converting to
and from the human-readable asterisk-suffix notation.

Signed-off-by: Phil Sutter <phil@nwl.cc>
3 months agoudata: Introduce NFTNL_UDATA_TABLE_NFT{VER,BLD}
Phil Sutter [Tue, 12 Aug 2025 16:47:14 +0000 (18:47 +0200)] 
udata: Introduce NFTNL_UDATA_TABLE_NFT{VER,BLD}

Register these table udata types here to avoid accidental overlaps.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 months agotunnel: add support to geneve options
Fernando Fernandez Mancera [Thu, 21 Aug 2025 09:17:18 +0000 (11:17 +0200)] 
tunnel: add support to geneve options

In addition, modifies the netlink parsing to loop through the nested
array of NFTA_TUNNEL_KEY_OPTS.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 months agotunnel: rework options
Pablo Neira Ayuso [Thu, 21 Aug 2025 09:17:17 +0000 (11:17 +0200)] 
tunnel: rework options

Only vxlan gbp can work before this patch because
NFTNL_OBJ_TUNNEL_ERSPAN_V2_DIR is off by one in the internal object
flags.

Replace them by NFTNL_OBJ_TUNNEL_OPTS and add two new opaque
nftnl_tunnel_opts and nftnl_tunnel_opt structs to represent tunnel
options.

- nftnl_tunnel_opt_alloc() allocates one tunnel option.
- nftnl_tunnel_opt_set() to sets it up.
- nftnl_tunnel_opt_get() to get the option attribute.

Then, to manage the list of options:

- nftnl_tunnel_opts_alloc() allocates a list of tunnel options.
- nftnl_tunnel_opts_add() adds a option to the list.

Although vxlan and erspan support for a single tunnel option at this
stage, this API prepares for supporting gevene which allows for more
tunnel options.

Joint work with Fernando.

Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 months agobuild: libnftnl 1.3.0 release libnftnl-1.3.0
Pablo Neira Ayuso [Wed, 6 Aug 2025 07:45:36 +0000 (09:45 +0200)] 
build: libnftnl 1.3.0 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 months agoinclude: utils.h needs errno.h
Phil Sutter [Wed, 2 Oct 2024 19:19:38 +0000 (21:19 +0200)] 
include: utils.h needs errno.h

Otherwise includers may face 'errno' undeclared errors.

Fixes: 26c945057d742 ("src: split internal.h is smaller files")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Florian Westphal <fw@strlen.de>
5 months agotrace: add support for TRACE_CT information
Florian Westphal [Thu, 22 May 2025 13:51:15 +0000 (15:51 +0200)] 
trace: add support for TRACE_CT information

Decode direction/id/state/status information.
This will be used by 'nftables monitor trace' to print a packets
conntrack state.

Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 months agoset: dump set backend name (hash, rbtree...) and elem count, if available
Florian Westphal [Tue, 8 Apr 2025 13:58:42 +0000 (15:58 +0200)] 
set: dump set backend name (hash, rbtree...) and elem count, if available

In case kernel provided the information do include it in debug dump:

nft --debug=netlink list ruleset
family 2 s t 0 backend nft_set_rhash_type
family 2 __set0 t 3 size 3 backend nft_set_hash_fast_type count 3
family 2 __set1 t 3 size 2 backend nft_set_bitmap_type count 2
[..]

Signed-off-by: Florian Westphal <fw@strlen.de>
8 months agobuild: libnftnl 1.2.9 release libnftnl-1.2.9
Pablo Neira Ayuso [Mon, 14 Apr 2025 16:47:24 +0000 (18:47 +0200)] 
build: libnftnl 1.2.9 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 months agotunnel: add missing inner nested netlink attribute for vxlan options
Fernando Fernandez Mancera [Mon, 14 Apr 2025 14:36:04 +0000 (16:36 +0200)] 
tunnel: add missing inner nested netlink attribute for vxlan options

The VXLAN options must be nested inside the NFTA_TUNNEL_KEY_OPTS_VXLAN
netlink attribute.

Fixes: ea63a05272f5 ("obj: add tunnel support")
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
8 months agosrc: remove unused str2XXX helpers
Zhongqiu Duan [Wed, 19 Mar 2025 14:20:53 +0000 (14:20 +0000)] 
src: remove unused str2XXX helpers

After commit 80077787f8f2 ("src: remove json support"), these internal
functions are no longer used:

nftnl_str2hooknum
nftnl_str2ntoh
nftnl_str2cmp
str2ctkey
str2ctdir
str2exthdr_op
str2exthdr_type
str2meta_key
nftnl_str2nat
nftnl_str2range
str2rt_key
nftnl_str2hooknum

Signed-off-by: Zhongqiu Duan <dzq.aishenghu0@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 months agoexpr: ct: print key name of id field
Zhongqiu Duan [Wed, 19 Mar 2025 14:29:27 +0000 (14:29 +0000)] 
expr: ct: print key name of id field

Fixes: 005369151ed5 ("include: updated nf_tables.h")
Signed-off-by: Zhongqiu Duan <dzq.aishenghu0@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
8 months agoexpr: payload: print tunnel header
Zhongqiu Duan [Tue, 18 Mar 2025 16:02:04 +0000 (16:02 +0000)] 
expr: payload: print tunnel header

The debug printing of the payload expr shows the tunnel header name as
unknown. Since after the first version we added two new payload bases
inner and tunnel, I prefer to make this change to meet possible future
extensions rather than setting NFT_PAYLOAD_TUN_HEADER as the new bound.

Reproduce:

nft --debug netlink add rule inet t c meta l4proto udp vxlan vni 0x123456

Before patch:
  ...
  [ inner type 1 hdrsize 8 flags f [ payload load 3b @ unknown header + 4 => reg 1 ] ]
  ...

After patch:
  ...
  [ inner type 1 hdrsize 8 flags f [ payload load 3b @ tunnel header + 4 => reg 1 ] ]
  ...

[ Remove defensive check in original patch --pablo ]

Fixes: 3f3909afd76d ("expr: add inner support")
Signed-off-by: Zhongqiu Duan <dzq.aishenghu0@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agotests: Fix for ASAN
Phil Sutter [Wed, 27 Nov 2024 17:54:53 +0000 (18:54 +0100)] 
tests: Fix for ASAN

The 'data' arrays in match and target expression tests were undersized
as they did not cover for the terminating NUL-char of the string used to
initialize them. When passing such array to strdup(), the latter reads
until after the defined array boundary.

Fixes: 93483364369d8 ("src: get rid of cached copies of x_tables.h and xt_LOG.h")
Signed-off-by: Phil Sutter <phil@nwl.cc>
12 months agotests: Extend set test by NFTNL_SET_DESC_CONCAT
Phil Sutter [Wed, 27 Nov 2024 17:52:10 +0000 (18:52 +0100)] 
tests: Extend set test by NFTNL_SET_DESC_CONCAT

Just to cover setter and getter code for that attribute.

Signed-off-by: Phil Sutter <phil@nwl.cc>
12 months agoset: Fix for array overrun when setting NFTNL_SET_DESC_CONCAT
Phil Sutter [Wed, 27 Nov 2024 15:30:08 +0000 (16:30 +0100)] 
set: Fix for array overrun when setting NFTNL_SET_DESC_CONCAT

Assuming max data_len of 16 * 4B and no zero bytes in 'data':
The while loop will increment field_count, use it as index for the
field_len array and afterwards make sure it hasn't increased to
NFT_REG32_COUNT. Thus a value of NFT_REG32_COUNT - 1 (= 15) will pass
the check, get incremented to 16 and used as index to the 16 fields long
array.
Use a less fancy for-loop to avoid the increment vs. check problem.

Fixes: 407f616ea5318 ("set: buffer overflow in NFTNL_SET_DESC_CONCAT setter")
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agotests: bitwise: add tests for new boolean operations
Jeremy Sowden [Tue, 19 Nov 2024 15:08:47 +0000 (16:08 +0100)] 
tests: bitwise: add tests for new boolean operations

We already have tests for mask-and-xor operations with constant RHS
operands.  Add tests for new operations with variable RHS operands.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agotests: bitwise: refactor shift tests
Jeremy Sowden [Tue, 19 Nov 2024 15:08:36 +0000 (16:08 +0100)] 
tests: bitwise: refactor shift tests

Deduplicate shift tests: instead of having separate implementations
for left- and right-shifts, have one and pass the operation to it.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agoexpr: bitwise: add support for kernel space AND, OR and XOR operations
Jeremy Sowden [Wed, 13 Nov 2024 21:27:08 +0000 (22:27 +0100)] 
expr: bitwise: add support for kernel space AND, OR and XOR operations

Hitherto, the kernel has only supported boolean operations of the form:

  dst = (src & mask) ^ xor

where `src` is held in a register, and `mask` and `xor` are immediate
values.  User space has converted AND, OR and XOR operations to this
form, and so one operand has had to be immediate.  The kernel now
supports performing AND, OR and XOR operations directly, on one register
and an immediate value or on two registers, so we make that support
available to user space.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agoexpr: bitwise: rename some boolean operation functions
Jeremy Sowden [Wed, 13 Nov 2024 21:39:12 +0000 (22:39 +0100)] 
expr: bitwise: rename some boolean operation functions

In the next patch we add support for doing AND, OR and XOR operations
directly in the kernel, so rename some functions and an enum constant
related to mask-and-xor boolean operations.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 months agoinclude: add new bitwise boolean attributes to nf_tables.h
Jeremy Sowden [Wed, 13 Nov 2024 21:25:47 +0000 (22:25 +0100)] 
include: add new bitwise boolean attributes to nf_tables.h

The kernel now has native support for AND, OR and XOR bitwise
operations.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
13 months agoUse SPDX License Identifiers in headers
Phil Sutter [Wed, 16 Oct 2024 16:47:32 +0000 (18:47 +0200)] 
Use SPDX License Identifiers in headers

Replace the copyright notice in header comments by an equivalent
SPDX-License-Identifier string. Drop a following empty line if at the
bottom of the comment. Leave any other header comment content in place.

This also fixes for an incomplete notice in examples/nft-ruleset-get.c
since commit c335442eefcca ("src: incorrect header refers to GPLv2
only").

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
13 months agoIntroduce struct nftnl_str_array
Phil Sutter [Mon, 30 Aug 2021 12:38:27 +0000 (14:38 +0200)] 
Introduce struct nftnl_str_array

This data structure holds an array of allocated strings for use in
nftnl_chain and nftnl_flowtable structs. For convenience, implement
functions to clear, populate and iterate over contents.

While at it, extend chain and flowtable tests to cover these attributes,
too.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
14 months agoinclude: refresh nf_tables.h copy
Pablo Neira Ayuso [Thu, 10 Oct 2024 12:56:46 +0000 (14:56 +0200)] 
include: refresh nf_tables.h copy

Fetch what we have in the kernel tree.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
14 months agobuild: libnftnl 1.2.8 release libnftnl-1.2.8
Pablo Neira Ayuso [Wed, 2 Oct 2024 20:41:48 +0000 (22:41 +0200)] 
build: libnftnl 1.2.8 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
14 months agorule: Don't append a newline when printing a rule
Phil Sutter [Tue, 1 Oct 2024 16:01:43 +0000 (18:01 +0200)] 
rule: Don't append a newline when printing a rule

Since commit c759027a526ac, printed rules may or may not end with a
newline depending on whether userdata was present or not. Deal with this
inconsistency by avoiding the trailing newline in all cases.

Fixes: c759027a526ac ("rule, set_elem: remove trailing \n in userdata snprintf")
Signed-off-by: Phil Sutter <phil@nwl.cc>
14 months agosrc: remove scaffolding around deprecated parser functions
Pablo Neira Ayuso [Wed, 25 Sep 2024 10:42:08 +0000 (12:42 +0200)] 
src: remove scaffolding around deprecated parser functions

nftnl_.*_do_parse() are of no use anymore, remove them.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
14 months agoexpr: dynset: validate expressions are of nested type
Florian Westphal [Sun, 22 Sep 2024 22:16:02 +0000 (00:16 +0200)] 
expr: dynset: validate expressions are of nested type

This was not handled in the switch statement so far.
Also, use proper max value, SET_MAX is larger than what is needed for
parsing.

Signed-off-by: Florian Westphal <fw@strlen.de>
17 months agobuild: libnftnl 1.2.7 release libnftnl-1.2.7
Pablo Neira Ayuso [Mon, 15 Jul 2024 11:48:01 +0000 (13:48 +0200)] 
build: libnftnl 1.2.7 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
17 months agotable: Support unsetting NFTNL_TABLE_USERDATA attribute
Phil Sutter [Wed, 10 Jul 2024 15:29:52 +0000 (17:29 +0200)] 
table: Support unsetting NFTNL_TABLE_USERDATA attribute

Cosmetics, but support unsetting anything that may be set.

Fixes: 99be0e6d066d7 ("table: add userdata support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
17 months agochain: Support unsetting NFTNL_CHAIN_USERDATA attribute
Phil Sutter [Wed, 10 Jul 2024 15:22:16 +0000 (17:22 +0200)] 
chain: Support unsetting NFTNL_CHAIN_USERDATA attribute

Cosmetics, but support unsetting anything that may be set.

Fixes: 76b82c425818e ("chain: add userdata and comment support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
17 months agoexpr: limit: Prepare for odd time units
Phil Sutter [Wed, 10 Apr 2024 12:29:07 +0000 (14:29 +0200)] 
expr: limit: Prepare for odd time units

When limit->unit is not a known timespan, use the largest possible unit
and print the value along with it. This enables libnftnl debug output to
correctly print arbitrary quotients, like '3/5 minutes' for instance.

Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1214
Suggested-by: Cyril <boite.pour.spam@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
17 months agoexpr: use NFTA_* netlink attributes to build fields, not NFTNL_EXPR_*
Pablo Neira Ayuso [Sun, 23 Jun 2024 11:27:00 +0000 (13:27 +0200)] 
expr: use NFTA_* netlink attributes to build fields, not NFTNL_EXPR_*

Coincidentally NFTNL_EXPR_BASE starts at 1 which comes right after
NFTA_*_UNSPEC which is zero. And NFTNL_EXPR_ attribute values were
mapping to NFTA_* attributes.

Use NFTA_* for netlink attribute types instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
20 months agoexpr: Respect data_len when setting attributes
Phil Sutter [Thu, 7 Mar 2024 13:49:08 +0000 (14:49 +0100)] 
expr: Respect data_len when setting attributes

With attr_policy in place, data_len has an upper boundary but it may be
lower than the attribute's storage area in which case memcpy() would
read garbage.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Respect data_len when setting attributes
Phil Sutter [Thu, 7 Mar 2024 13:34:18 +0000 (14:34 +0100)] 
obj: Respect data_len when setting attributes

With attr_policy in place, data_len has an upper boundary. Use it for
memcpy() calls to cover for caller passing data with lower size than the
attribute's storage.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoutils: Introduce and use nftnl_set_str_attr()
Phil Sutter [Thu, 7 Mar 2024 13:07:21 +0000 (14:07 +0100)] 
utils: Introduce and use nftnl_set_str_attr()

The function consolidates the necessary code when assigning to string
pointer attributes, namely:

* Conditional free of the previous value
* Allocation of new value
* Checking for memory allocation errors
* Setting respective flag bit

A new feature previously missing in all call sites is respecting
data_len in case the buffer up to that point did not contain a NUL-char.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Enforce attr_policy compliance in nftnl_obj_set_data()
Phil Sutter [Thu, 7 Mar 2024 12:56:14 +0000 (13:56 +0100)] 
obj: Enforce attr_policy compliance in nftnl_obj_set_data()

Every object type defines an attr_policy array, so deny setting
attributes for object types which don't have it present or if it
specifies a non-zero maxlen which is lower than the given data_len.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Introduce struct obj_ops::attr_policy
Phil Sutter [Thu, 7 Mar 2024 12:46:26 +0000 (13:46 +0100)] 
obj: Introduce struct obj_ops::attr_policy

Just like with struct expr_ops::attr_policy, enable object types to
inform about restrictions on attribute use. This way generic object code
may perform sanity checks before dispatching to object ops.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Call obj_ops::set with legal attributes only
Phil Sutter [Thu, 7 Mar 2024 12:25:31 +0000 (13:25 +0100)] 
obj: Call obj_ops::set with legal attributes only

Refer to obj_ops::nftnl_max_attr field value for the maximum supported
attribute value to reject invalid ones upfront.

Consequently drop default cases from callbacks' switches which handle
all supported attributes.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Repurpose struct obj_ops::max_attr field
Phil Sutter [Thu, 7 Mar 2024 12:15:22 +0000 (13:15 +0100)] 
obj: Repurpose struct obj_ops::max_attr field

Just like with struct expr_ops::max_attr, make it hold the maximum
object attribute (NFTNL_OBJ_*) value supported by this object type.

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Return value on setters
Phil Sutter [Thu, 14 Mar 2024 15:54:55 +0000 (16:54 +0100)] 
obj: Return value on setters

Similar to other setters, let callers know if memory allocation fails.
Though return value with all setters, as all of them may be used to set
object type-specific attributes which may fail (e.g. if NFTNL_OBJ_TYPE
was not set before).

Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoutils: Fix for wrong variable use in nftnl_assert_validate()
Phil Sutter [Thu, 7 Mar 2024 12:59:00 +0000 (13:59 +0100)] 
utils: Fix for wrong variable use in nftnl_assert_validate()

This worked by accident as all callers passed a local variable 'attr' as
parameter '_attr'.

Fixes: 7756d31990cd4 ("src: add assertion infrastructure to validate attribute types")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: synproxy: Use memcpy() to handle potentially unaligned data
Phil Sutter [Thu, 7 Mar 2024 13:16:05 +0000 (14:16 +0100)] 
obj: synproxy: Use memcpy() to handle potentially unaligned data

Analogous to commit dc240913458d5 ("src: Use memcpy() to handle
potentially unaligned data").

Fixes: 609a13fc2999e ("src: synproxy stateful object support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Do not call nftnl_obj_set_data() with zero data_len
Phil Sutter [Thu, 14 Mar 2024 15:44:34 +0000 (16:44 +0100)] 
obj: Do not call nftnl_obj_set_data() with zero data_len

Pass 'strlen() + 1' as length parameter when setting string attributes,
just like other string setters do.

Fixes: 5573d0146c1ae ("src: support for stateful objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agotable: Validate NFTNL_TABLE_OWNER, too
Phil Sutter [Thu, 14 Mar 2024 16:30:30 +0000 (17:30 +0100)] 
table: Validate NFTNL_TABLE_OWNER, too

Fixes: 985955fe41f53 ("table: add table owner support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoset: Validate NFTNL_SET_ID, too
Phil Sutter [Thu, 14 Mar 2024 16:29:51 +0000 (17:29 +0100)] 
set: Validate NFTNL_SET_ID, too

Fixes: 26298a9ffc2e2 ("set: add set ID support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoobj: Validate NFTNL_OBJ_TYPE, too
Phil Sutter [Thu, 14 Mar 2024 16:28:15 +0000 (17:28 +0100)] 
obj: Validate NFTNL_OBJ_TYPE, too

Fixes: 5573d0146c1ae ("src: support for stateful objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agoflowtable: Validate NFTNL_FLOWTABLE_SIZE, too
Phil Sutter [Thu, 14 Mar 2024 16:26:33 +0000 (17:26 +0100)] 
flowtable: Validate NFTNL_FLOWTABLE_SIZE, too

Fixes: cdaea7f1ced05 ("flowtable: allow to specify size")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agotable: Validate NFTNL_TABLE_USE, too
Phil Sutter [Thu, 14 Mar 2024 16:25:05 +0000 (17:25 +0100)] 
table: Validate NFTNL_TABLE_USE, too

Fixes: 53c0ff324598c ("src: add nft_*_attr_{set|get}_data interface")
Signed-off-by: Phil Sutter <phil@nwl.cc>
20 months agochain: Validate NFTNL_CHAIN_USE, too
Phil Sutter [Thu, 14 Mar 2024 16:22:14 +0000 (17:22 +0100)] 
chain: Validate NFTNL_CHAIN_USE, too

Fixes: 53c0ff324598c ("src: add nft_*_attr_{set|get}_data interface")
Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoexpr: Enforce attr_policy compliance in nftnl_expr_set()
Phil Sutter [Fri, 15 Dec 2023 15:32:30 +0000 (16:32 +0100)] 
expr: Enforce attr_policy compliance in nftnl_expr_set()

Every expression type defines an attr_policy array, so deny setting
attributes if not present. Also deny if maxlen field is non-zero and
lower than the given data_len.

Some attributes' max length is not fixed (e.g. NFTNL_EXPR_{TG,MT}_INFO )
or is not sensible to check (e.g.  NFTNL_EXPR_DYNSET_EXPR). The zero
maxlen "nop" is also used for deprecated attributes, just to not
silently ignore them.

Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoexpr: Introduce struct expr_ops::attr_policy
Phil Sutter [Fri, 15 Dec 2023 15:30:52 +0000 (16:30 +0100)] 
expr: Introduce struct expr_ops::attr_policy

Similar to kernel's nla_policy, enable expressions to inform about
restrictions on attribute use. This allows the generic expression code
to perform sanity checks before dispatching to expression ops.

For now, this holds only the maximum data len which may be passed to
nftnl_expr_set().

While one may debate whether accepting e.g. uint32_t for sreg/dreg
attributes is correct, it is necessary to not break nftables.

Note that this introduces artificial restrictions on name lengths which
were caught by the kernel (if nftables didn't).

Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoinclude: Sync nf_log.h with kernel headers
Phil Sutter [Fri, 15 Dec 2023 15:15:35 +0000 (16:15 +0100)] 
include: Sync nf_log.h with kernel headers

Next patch needs NF_LOG_PREFIXLEN define.

Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoexpr: Call expr_ops::set with legal types only
Phil Sutter [Wed, 13 Dec 2023 22:49:53 +0000 (23:49 +0100)] 
expr: Call expr_ops::set with legal types only

Having the new expr_ops::nftnl_max_attr field in place, the valid range
of attribute type values is known now. Reject illegal ones upfront.

Consequently drop the default case from callbacks' switches which handle
all supported attributes.

Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoexpr: Repurpose struct expr_ops::max_attr field
Phil Sutter [Wed, 13 Dec 2023 13:56:49 +0000 (14:56 +0100)] 
expr: Repurpose struct expr_ops::max_attr field

Instead of holding the maximum kernel space (NFTA_*) attribute value,
use it to hold the maximum expression attribute (NFTNL_EXPR_*) value
instead. This will be used for index boundary checks in an attribute
policy array later.

Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agotests: Fix objref test case
Phil Sutter [Fri, 15 Dec 2023 15:10:49 +0000 (16:10 +0100)] 
tests: Fix objref test case

Probably a c'n'p bug, the test would allocate a lookup expression
instead of the objref one to be tested.

Fixes: b4edb4fc558ac ("expr: add stateful object reference expression")
Signed-off-by: Phil Sutter <phil@nwl.cc>
21 months agoutils: remove unused code
Pablo Neira Ayuso [Mon, 26 Feb 2024 16:38:42 +0000 (17:38 +0100)] 
utils: remove unused code

Remove several internal code that have no use these days:

- nftnl_str2family
- nftnl_strtoi
- nftnl_get_value
- enum nftnl_type

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
21 months agoudata: incorrect userdata buffer size validation
Pablo Neira Ayuso [Mon, 26 Feb 2024 16:31:19 +0000 (17:31 +0100)] 
udata: incorrect userdata buffer size validation

Use the current remaining space in the buffer to ensure more userdata
attributes still fit in, buf->size is the total size of the userdata
buffer.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
21 months agoexpr: immediate: check for chain attribute to release chain name
Pablo Neira Ayuso [Sat, 24 Feb 2024 09:14:54 +0000 (10:14 +0100)] 
expr: immediate: check for chain attribute to release chain name

Check for chain name attribute to release chain name, for consistency
with other existing attributes.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
21 months agoobj: ct_timeout: setter checks for timeout array boundaries
Pablo Neira Ayuso [Thu, 25 Jan 2024 16:34:40 +0000 (17:34 +0100)] 
obj: ct_timeout: setter checks for timeout array boundaries

Use _MAX definitions for timeout attribute arrays and check that
timeout array is not larger than NFTNL_CTTIMEOUT_ARRAY_MAX.

Fixes: 0adceeab1597 ("src: add ct timeout support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agoset_elem: use nftnl_data_cpy() in NFTNL_SET_ELEM_{KEY,KEY_END,DATA}
Pablo Neira Ayuso [Fri, 12 Jan 2024 11:33:38 +0000 (12:33 +0100)] 
set_elem: use nftnl_data_cpy() in NFTNL_SET_ELEM_{KEY,KEY_END,DATA}

Use safe nftnl_data_cpy() to copy key into union nftnl_data_reg.

Follow up for commit:

bc2afbde9eae ("expr: fix buffer overflows in data value setters")

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agoset: buffer overflow in NFTNL_SET_DESC_CONCAT setter
Pablo Neira Ayuso [Thu, 11 Jan 2024 00:13:37 +0000 (01:13 +0100)] 
set: buffer overflow in NFTNL_SET_DESC_CONCAT setter

Allow to set a maximum limit of sizeof(s->desc.field_len) which is 16
bytes, otherwise, bail out. Ensure s->desc.field_count does not go over
the array boundary.

Fixes: 7cd41b5387ac ("set: Add support for NFTA_SET_DESC_CONCAT attributes")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agochain: Removed non-defined functions
Nicholas Vinson [Wed, 27 Dec 2023 03:05:17 +0000 (22:05 -0500)] 
chain: Removed non-defined functions

The functions nftnl_chain_parse(), nftnl_chain_parse_file(),
nftnl_set_elems_foreach(), and nftnl_obj_unset() are no longer defined
and removed from the code.

The functions nftnl_chain_parse(), nftnl_chain_parse_file() were removed
with commit 80077787f8f2 ("src: remove json support").

The function nftnl_set_elems_foreach() does not appear to have ever been
defined.

However, libnftnl.map still lists these functions which causes libnftnl
to fail to link with ld.lld as the option --undefined-version is
disabled by default.

Fixes Gentoo bug 914710 (https://bugs.gentoo.org/914710)

Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
23 months agoobject: define nftnl_obj_unset()
Pablo Neira Ayuso [Tue, 2 Jan 2024 13:22:50 +0000 (14:22 +0100)] 
object: define nftnl_obj_unset()

For consistency with existing objects, implement this interface.
This is already defined in libnftnl.map so the intention was to
provide it.

Fixes: 5573d0146c1a ("src: support for stateful objects")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agoexpr: fix buffer overflows in data value setters
Florian Westphal [Tue, 12 Dec 2023 14:01:17 +0000 (15:01 +0100)] 
expr: fix buffer overflows in data value setters

The data value setters memcpy() to a fixed-size buffer, but its very easy
to make nft pass too-larger values.  Example:
  @th,160,1272 gt 0

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60b000[..]

Truncate the copy instead of corrupting the heap.
This needs additional fixes on nft side to reject such statements with a
proper error message.

Signed-off-by: Florian Westphal <fw@strlen.de>
2 years agoobject: getters take const struct
corubba [Sat, 9 Dec 2023 22:03:01 +0000 (23:03 +0100)] 
object: getters take const struct

As with all the other entities (like table or set), the getter functions
for objects now take a `const struct nftnl_obj*` as first parameter.
The getters for all specific object types (like counter or limit), which
are called in the default switch-case, already do.

Signed-off-by: corubba <corubba@gmx.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agoset: Do not leave free'd expr_list elements in place
Phil Sutter [Wed, 31 May 2023 12:09:09 +0000 (14:09 +0200)] 
set: Do not leave free'd expr_list elements in place

When freeing elements, remove them also to prevent a potential UAF.

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1685
Fixes: 3469f09286cee ("src: add NFTNL_SET_EXPRESSIONS")
Signed-off-by: Phil Sutter <phil@nwl.cc>
2 years agobuild: libnftnl 1.2.6 release libnftnl-1.2.6
Pablo Neira Ayuso [Tue, 11 Jul 2023 16:28:31 +0000 (18:28 +0200)] 
build: libnftnl 1.2.6 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agotests: nft-rule-test: Add test cases to improve code coverage
shixuantong [Thu, 1 Jun 2023 16:05:37 +0000 (00:05 +0800)] 
tests: nft-rule-test: Add test cases to improve code coverage

Signed-off-by: shixuantong <tongxiaoge1001@126.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agotests: nft-table-test: fix typo
shixuantong [Thu, 1 Jun 2023 15:53:23 +0000 (23:53 +0800)] 
tests: nft-table-test: fix typo

Signed-off-by: shixuantong <tongxiaoge1001@126.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agoexpr: meta: introduce broute meta expression
Sriram Yagnaraman [Sun, 26 Feb 2023 09:53:08 +0000 (10:53 +0100)] 
expr: meta: introduce broute meta expression

libnftnl support for broute meta statement introduced in:
https://patchwork.ozlabs.org/project/netfilter-devel/patch/20230224095251.11249-1-sriram.yagnaraman@est.tech/

Signed-off-by: Sriram Yagnaraman <sriram.yagnaraman@est.tech>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agobuild: libnftnl 1.2.5 release libnftnl-1.2.5
Pablo Neira Ayuso [Thu, 9 Mar 2023 19:38:24 +0000 (20:38 +0100)] 
build: libnftnl 1.2.5 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agochain: relax logic to build NFTA_CHAIN_HOOK
Pablo Neira Ayuso [Tue, 3 Jan 2023 15:23:15 +0000 (16:23 +0100)] 
chain: relax logic to build NFTA_CHAIN_HOOK

The logic to build NFTA_CHAIN_HOOK enforces the presence of the hook
number and priority to include the devices. Relax this to allow for
incremental device updates.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2 years agoexpr: add inner support
Pablo Neira Ayuso [Mon, 2 Jan 2023 14:15:48 +0000 (15:15 +0100)] 
expr: add inner support

This patch adds support for the inner expression which allows you to
match on the inner tunnel headers, eg. VxLAN.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoMakefile: Create LZMA-compressed dist-files
Phil Sutter [Wed, 7 Dec 2022 23:48:26 +0000 (00:48 +0100)] 
Makefile: Create LZMA-compressed dist-files

Use a more modern alternative to bzip2.

Suggested-by: Jan Engelhardt <jengelh@inai.de>
Suggested-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Phil Sutter <phil@nwl.cc>
3 years agosrc: replace nftnl_*_nlmsg_build_hdr() by nftnl_nlmsg_build_hdr()
Pablo Neira Ayuso [Tue, 15 Nov 2022 10:12:03 +0000 (11:12 +0100)] 
src: replace nftnl_*_nlmsg_build_hdr() by nftnl_nlmsg_build_hdr()

Use nftnl_nlmsg_build_hdr() instead of nftnl_*_nlmsg_build_hdr().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexamples: remove nftnl_batch_is_supported() call
Pablo Neira Ayuso [Mon, 14 Nov 2022 16:34:36 +0000 (17:34 +0100)] 
examples: remove nftnl_batch_is_supported() call

Linux kernel <= 3.13 needs for this check, remove it from examples.

Kernel commit:

  958bee14d071 ("netfilter: nf_tables: use new transaction infrastructure to handle sets")

added support for set into the batch.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agobuild: libnftnl 1.2.4 release libnftnl-1.2.4
Pablo Neira Ayuso [Thu, 10 Nov 2022 15:01:48 +0000 (16:01 +0100)] 
build: libnftnl 1.2.4 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agolibnftnl: Fix res_id byte order
Ian Pilcher [Tue, 18 Oct 2022 16:45:28 +0000 (11:45 -0500)] 
libnftnl: Fix res_id byte order

The res_id member of struct nfgenmsg is supposed to be in network
byte order (big endian).  Call htons() in __nftnl_nlmsg_build_hdr()
to ensure that this is true on little endian systems.

There is a kernel workaround that was introduced in 4.3 to address
this issue:

 commit a9de9777d613500b089a7416f936bf3ae5f070d2
 Author: Pablo Neira Ayuso <pablo@netfilter.org>
 Date:   Fri Aug 28 21:01:43 2015 +0200

    netfilter: nfnetlink: work around wrong endianess in res_id field

And current oldest stable kernel branch in 4.9, merge this to fix
the incorrect endianness from userspace.

Signed-off-by: Ian Pilcher <arequipeno@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexpr: payload: remove unused function
Pablo Neira Ayuso [Wed, 7 Sep 2022 15:22:04 +0000 (17:22 +0200)] 
expr: payload: remove unused function

nftnl_str2base() not used anymore, remove it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agorule, set_elem: remove trailing \n in userdata snprintf
Pablo Neira Ayuso [Wed, 31 Aug 2022 14:52:51 +0000 (16:52 +0200)] 
rule, set_elem: remove trailing \n in userdata snprintf

212479ad2c92 ("rule, set_elem: fix printing of user data") uncovered
another an extra line break in the userdata printing, remove it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agorule, set_elem: fix printing of user data
Jeremy Sowden [Sat, 27 Aug 2022 17:17:17 +0000 (18:17 +0100)] 
rule, set_elem: fix printing of user data

Hitherto, alphanumeric characters have been printed as-is, but anything
else was replaced by '\0'.  However, this effectively truncates the
output.  Instead, print any printable character as-is and print anything
else as a hexadecimal escape sequence:

  userdata = { \x01\x04\x01\x00\x00\x00 }

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agobuild: libnftnl 1.2.3 release libnftnl-1.2.3
Pablo Neira Ayuso [Tue, 9 Aug 2022 18:23:55 +0000 (20:23 +0200)] 
build: libnftnl 1.2.3 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agobuild: fix clang+glibc snprintf substitution error
Nicholas Vinson [Wed, 15 Jun 2022 11:35:28 +0000 (07:35 -0400)] 
build: fix clang+glibc snprintf substitution error

When building with clang and glibc and -D_FORTIFY_SOURCE=2 is passed to
clang, the snprintf member of the expr_ops and obj_ops structures will
be incorrectly replaced with __builtin_snprintf_chk() which results in
"error: no member named '__builtin___snprintf_chk'" errors at build
time.

This patch changes the member name from 'snprintf' to 'output' to
prevent the replacement.

This bug can be emulated using GCC by undefining the __va_arg_pack macro
before stdio.h is included.

This patch is based on the notes provided in
https://bugs.gentoo.org/807766.

Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
3 years agobuild: libnftnl 1.2.2 release libnftnl-1.2.2
Pablo Neira Ayuso [Tue, 7 Jun 2022 14:10:40 +0000 (16:10 +0200)] 
build: libnftnl 1.2.2 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoset_elem: missing export symbol
Pablo Neira Ayuso [Wed, 18 May 2022 21:54:03 +0000 (23:54 +0200)] 
set_elem: missing export symbol

nftnl_set_elem_nlmsg_build_payload() is already available through .map
file and headers, add missing EXPORT_SYMBOL.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
3 years agoexthdr: tcp option reset support
Florian Westphal [Sat, 19 Feb 2022 13:39:04 +0000 (14:39 +0100)] 
exthdr: tcp option reset support

Adds print debug support for tcp reset feature.

Signed-off-by: Florian Westphal <fw@strlen.de>
4 years agobuild: libnftnl 1.2.1 release libnftnl-1.2.1
Pablo Neira Ayuso [Wed, 17 Nov 2021 14:08:46 +0000 (15:08 +0100)] 
build: libnftnl 1.2.1 release

Bump libversion, because this release adds new API:
nftnl_set_elem_nlmsg_build().

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpr: payload: print inner header base offset
Pablo Neira Ayuso [Wed, 17 Nov 2021 10:05:11 +0000 (11:05 +0100)] 
expr: payload: print inner header base offset

Update string array to print the "inner" header string, instead of
printing "unknown".

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoset: use NFTNL_SET_ELEM_VERDICT to print verdict
Pablo Neira Ayuso [Thu, 4 Nov 2021 14:30:48 +0000 (15:30 +0100)] 
set: use NFTNL_SET_ELEM_VERDICT to print verdict

This patch reworks 228e8b174627 ("set_elem: Fix printing of verdict map
elements"), check if NFTNL_SET_ELEM_VERDICT is set then print the set
element verdict.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoset: expose nftnl_set_elem_nlmsg_build()
Pablo Neira Ayuso [Thu, 4 Nov 2021 13:09:44 +0000 (14:09 +0100)] 
set: expose nftnl_set_elem_nlmsg_build()

Expose a function to build one single set element netlink message.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpr: last: add NFTNL_EXPR_LAST_SET
Pablo Neira Ayuso [Mon, 5 Jul 2021 17:56:24 +0000 (19:56 +0200)] 
expr: last: add NFTNL_EXPR_LAST_SET

Honor NFTA_LAST_SET netlink attribute, it tells us if a packet has ever
updated this expression.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpr: missing netlink attribute in last expression
Pablo Neira Ayuso [Thu, 10 Jun 2021 18:46:09 +0000 (20:46 +0200)] 
expr: missing netlink attribute in last expression

NFTA_LAST_SET is missing, add it.

Fixes: ed7c442c2d04 ("expr: add last match time support")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoexpr: add last match time support
Pablo Neira Ayuso [Tue, 8 Jun 2021 18:00:05 +0000 (20:00 +0200)] 
expr: add last match time support

This new expression displays when last matching has happened.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoinclude: update nf_tables.h
Pablo Neira Ayuso [Tue, 8 Jun 2021 17:58:58 +0000 (19:58 +0200)] 
include: update nf_tables.h

Get header in sync with 5.13.0-rc.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agobuild: libnftnl 1.2.0 release libnftnl-1.2.0
Pablo Neira Ayuso [Tue, 25 May 2021 11:13:15 +0000 (13:13 +0200)] 
build: libnftnl 1.2.0 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>