]> git.ipfire.org Git - thirdparty/libnftnl.git/log
thirdparty/libnftnl.git
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>
22 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>
22 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>
22 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>
22 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>
3 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>
3 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>
4 years agoexpr: socket: add cgroups v2 support
Pablo Neira Ayuso [Tue, 20 Apr 2021 23:25:49 +0000 (01:25 +0200)] 
expr: socket: add cgroups v2 support

Add NFT_SOCKET_CGROUPSV2 key type and NFTA_SOCKET_LEVEL attribute.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agosrc: incorrect header refers to GPLv2 only
Pablo Neira Ayuso [Mon, 19 Apr 2021 23:57:20 +0000 (01:57 +0200)] 
src: incorrect header refers to GPLv2 only

libnftnl is GPLv2+, fix incorrect headers.

Acked-by: Arturo Borrero Gonzalez <arturo@netfilter.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agoruleset: Eliminate tag and separator helpers
Phil Sutter [Tue, 9 Mar 2021 15:03:58 +0000 (16:03 +0100)] 
ruleset: Eliminate tag and separator helpers

Those were used for JSON and XML output only with the exception of
separator helper which at the same time served as conditional nop if no
"previous" object as passed. Replace it by a string variable updated at
the end of each loop iteration and just drop the other helpers.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoGet rid of single option switch statements
Phil Sutter [Tue, 9 Mar 2021 12:41:44 +0000 (13:41 +0100)] 
Get rid of single option switch statements

Replace each by a conditional testing the only valid case.

There is one odd example, namely src/set.c: When printing a set with
type NFTNL_OUTPUT_XML, the relevant function would return 0 instead of
-1 like all others. Just drop it assuming nothing depends on that
(faulty) behaviour.

Cc: Arturo Borrero <arturo.borrero.glez@gmail.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoDrop pointless local variable in snprintf callbacks
Phil Sutter [Tue, 9 Mar 2021 10:26:47 +0000 (11:26 +0100)] 
Drop pointless local variable in snprintf callbacks

A common idiom among snprintf callbacks was to copy the unsigned
parameter 'size' (or 'len') into a signed variable for further use.
Though since snprintf() itself casts it to unsigned and
SNPRINTF_BUFFER_SIZE() does not allow it to become negative, this is not
needed. Drop the local variable and rename the parameter accordingly.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoobj: Drop type parameter from snprintf callback
Phil Sutter [Tue, 9 Mar 2021 11:01:19 +0000 (12:01 +0100)] 
obj: Drop type parameter from snprintf callback

Objects don't support any other output type than NFTNL_OUTPUT_DEFAULT,
so just drop the parameter.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr/data_reg: Drop output_format parameter
Phil Sutter [Tue, 9 Mar 2021 10:36:03 +0000 (11:36 +0100)] 
expr/data_reg: Drop output_format parameter

The function nftnl_data_reg_snprintf is exclusively called with
NFTNL_OUTPUT_DEFAULT as parameter, others are not supported - just drop
it.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr: Check output type once and for all
Phil Sutter [Tue, 9 Mar 2021 09:40:16 +0000 (10:40 +0100)] 
expr: Check output type once and for all

There is but a single supported output type left, so check it in expr.c
and drop all the single option switch statements in individual
expressions.

Since the parameter is now unused (and to ensure code correctness), drop
'type' parameter from struct expr_ops' snprintf callback.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoobject: Fix for wrong parameter passed to snprintf callback
Phil Sutter [Tue, 9 Mar 2021 12:29:30 +0000 (13:29 +0100)] 
object: Fix for wrong parameter passed to snprintf callback

Instead of the remaining buffer length, the used buffer length was
passed to object's snprintf callback (and the final snprintf call).

Fixes: 5573d0146c1ae ("src: support for stateful objects")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoobj/ct_timeout: Fix snprintf buffer length updates
Phil Sutter [Tue, 9 Mar 2021 11:16:12 +0000 (12:16 +0100)] 
obj/ct_timeout: Fix snprintf buffer length updates

Have to pass shrinking 'remain' variable to consecutive snprintf calls
instead of the unchanged 'len' parameter.

Fixes: 0adceeab1597a ("src: add ct timeout support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoobj/ct_expect: Fix snprintf buffer length updates
Phil Sutter [Tue, 9 Mar 2021 11:13:23 +0000 (12:13 +0100)] 
obj/ct_expect: Fix snprintf buffer length updates

Have to pass shrinking 'remain' variable to consecutive snprintf calls
instead of the unchanged 'len' parameter.

Fixes: c4b6aa09b85d2 ("src: add ct expectation support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr: Fix snprintf buffer length updates
Phil Sutter [Tue, 9 Mar 2021 10:14:29 +0000 (11:14 +0100)] 
expr: Fix snprintf buffer length updates

Subsequent calls to snprintf() sometimes reuse 'len' variable although
they should refer to the updated value in 'remain' instead.

Fixes: 676ea569bbe5a ("src: Change parameters of SNPRINTF_BUFFER_SIZE macro.")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoset_elem: Fix printing of verdict map elements
Phil Sutter [Sat, 26 Dec 2020 18:44:48 +0000 (19:44 +0100)] 
set_elem: Fix printing of verdict map elements

Elements' data was printed as type DATA_VALUE no matter the actual type.
For verdicts, this meant no printing at all (because reg->len is either
zero or garbage).

To fix this, nftnl_set_elem_snprintf_default() needs type info held in
struct nftnl_set. Pass it via parameter to that function, make it
non-static and call it from nftnl_set_snprintf_default() instead of the
generic nftnl_set_elem_snprintf(). This way no changes have to be done
to exported functions, also the output type is already defined when
nftnl_set_snprintf_default() runs so checking type value again is
pointless.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr/{masq,nat}: Don't print unused regs
Phil Sutter [Sat, 19 Dec 2020 01:01:23 +0000 (02:01 +0100)] 
expr/{masq,nat}: Don't print unused regs

No point in printing the unset register value (which is zero then).

Fixes: af0c182670837 ("expr: masq: Add support for port selection")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agorule: Avoid printing trailing spaces
Phil Sutter [Sat, 19 Dec 2020 01:24:16 +0000 (02:24 +0100)] 
rule: Avoid printing trailing spaces

Introduce 'sep' variable to track whether something was printed already.
While being at it, introduce PRIu64 for 'handle' and 'position'
attributes.

Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr/xfrm: Kill dead code
Phil Sutter [Sat, 19 Dec 2020 01:22:56 +0000 (02:22 +0100)] 
expr/xfrm: Kill dead code

These functions were used by removed JSON parser only.

Fixes: 80077787f8f21 ("src: remove json support")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr/tunnel: Kill dead code
Phil Sutter [Sat, 19 Dec 2020 01:21:44 +0000 (02:21 +0100)] 
expr/tunnel: Kill dead code

Function str2tunnel_key() was never used.

Fixes: 42468fb6df61a ("expr: add support for matching tunnel metadata")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agoexpr/socket: Kill dead code
Phil Sutter [Sat, 19 Dec 2020 01:20:40 +0000 (02:20 +0100)] 
expr/socket: Kill dead code

Function str2socket_key() was never used.

Fixes: 038d226f2e6cc ("src: Add support for native socket matching")
Signed-off-by: Phil Sutter <phil@nwl.cc>
4 years agotable: add table owner support
Pablo Neira Ayuso [Sat, 20 Feb 2021 15:18:13 +0000 (16:18 +0100)] 
table: add table owner support

Add support for NFTA_TABLE_OWNER.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agobuild: libnftnl 1.1.9 release libnftnl-1.1.9
Pablo Neira Ayuso [Wed, 13 Jan 2021 15:30:48 +0000 (16:30 +0100)] 
build: libnftnl 1.1.9 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
4 years agodynset: add NFTNL_EXPR_DYNSET_FLAGS
Pablo Neira Ayuso [Wed, 13 Jan 2021 15:27:50 +0000 (16:27 +0100)] 
dynset: add NFTNL_EXPR_DYNSET_FLAGS

This patch adds support for the dynset flags.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add NFTNL_EXPR_DYNSET_EXPRESSIONS
Pablo Neira Ayuso [Sat, 12 Dec 2020 19:17:31 +0000 (20:17 +0100)] 
src: add NFTNL_EXPR_DYNSET_EXPRESSIONS

NFTNL_EXPR_DYNSET_EXPR defines the stateful expression type that
an element stores when added from the packet path.

This patch adds support for the set expression list, which generalizes
NFTNL_EXPR_DYNSET_EXPR.

This patch also adds nftnl_expr_add_expr() to add new expressions to
elements and nftnl_set_expr_expr_foreach() to iterate over the list of
expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add NFTNL_SET_EXPRESSIONS
Pablo Neira Ayuso [Sat, 12 Dec 2020 19:16:53 +0000 (20:16 +0100)] 
src: add NFTNL_SET_EXPRESSIONS

NFTNL_SET_EXPR defines the stateful expression type that this set stores
in each element. This provides the set definition in terms of stateful
expressions. The expression that is passed via NFNTL_SET_ELEM_EXPR must
equal to this set stateful expression type, otherwise the kernel bails
out.

This patch adds support for the set expression list, which generalizes
NFTNL_SET_EXPR.

This patch also adds nftnl_set_add_expr() to add new expressions to a set
and nftnl_set_elem_expr_foreach() to iterate over the list of expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add NFTNL_SET_ELEM_EXPRESSIONS
Pablo Neira Ayuso [Sat, 12 Dec 2020 19:13:07 +0000 (20:13 +0100)] 
src: add NFTNL_SET_ELEM_EXPRESSIONS

NFTNL_SET_ELEM_EXPR defines the stateful expression type that this
element stores. This is useful to restore runtime set element stateful
expressions (when saving, then reboot and restore).

This patch adds support for the set element expression list, which
generalizes NFTNL_SET_ELEM_EXPR.

This patch also adds nftnl_set_elem_add_expr() to add new expressions to
set elements and nftnl_set_elem_expr_foreach() to iterate over the list
of expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoset_elem: Include key_end data reg in print output
Phil Sutter [Mon, 14 Dec 2020 17:52:07 +0000 (18:52 +0100)] 
set_elem: Include key_end data reg in print output

Elements of concatenated range sets hold the upper boundary in an extra
data_reg, print it using dash as a somewhat intuitive separator.

Fixes: 04cc28d8d6923 ("set_elem: Introduce support for NFTNL_SET_ELEM_KEY_END")
Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agoset_elem: Use nftnl_data_reg_snprintf()
Phil Sutter [Mon, 14 Dec 2020 16:53:47 +0000 (17:53 +0100)] 
set_elem: Use nftnl_data_reg_snprintf()

Introduce a flag to allow toggling the '0x' prefix when printing data
values, then use the existing routines to print data registers from
set_elem code.

Signed-off-by: Phil Sutter <phil@nwl.cc>
5 years agobitwise: improve formatting of registers in bitwise dumps.
Jeremy Sowden [Sat, 14 Nov 2020 17:36:05 +0000 (17:36 +0000)] 
bitwise: improve formatting of registers in bitwise dumps.

Registers are formatted as 'reg %u' everywhere apart from in bitwise
expressions where they are formatted as 'reg=%u'.  Change bitwise to
match.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: libnftnl 1.1.8 release libnftnl-1.1.8
Pablo Neira Ayuso [Mon, 26 Oct 2020 12:54:19 +0000 (13:54 +0100)] 
build: libnftnl 1.1.8 release

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpr: add nftnl_rule_del_expr()
Pablo Neira Ayuso [Mon, 19 Oct 2020 11:24:37 +0000 (13:24 +0200)] 
expr: add nftnl_rule_del_expr()

Add a function to remove expression from the rule list.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpr: expose nftnl_expr_build_payload()
Pablo Neira Ayuso [Fri, 16 Oct 2020 16:53:37 +0000 (18:53 +0200)] 
expr: expose nftnl_expr_build_payload()

This function allows you to build the netlink attributes for
expressions.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpr: socket: add wildcard support
Pablo Neira Ayuso [Mon, 12 Oct 2020 01:21:34 +0000 (03:21 +0200)] 
expr: socket: add wildcard support

Add missing NFT_SOCKET_WILDCARD definition.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agochain: add userdata and comment support
Jose M. Guisado Gomez [Mon, 21 Sep 2020 13:28:22 +0000 (15:28 +0200)] 
chain: add userdata and comment support

Adds NFTNL_CHAIN_USERDATA, in order to support userdata for chains.

Adds NFTNL_UDATA_CHAIN_COMMENT chain userdata type to support storing a
comment.

Relies on NFTA_CHAIN_USERDATA.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoobject: add userdata and comment support
Jose M. Guisado Gomez [Wed, 2 Sep 2020 09:12:40 +0000 (11:12 +0200)] 
object: add userdata and comment support

This patch adds NFTNL_OBJ_USERDATA to support userdata for objects.

Also adds NFTNL_UDATA_OBJ_COMMENT to support comments for objects,
stored in userdata space.

Bumps libnftnl.map to 15 as nftnl_obj_get_data needs to be exported to
enable getting object attributes/data.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agotable: add userdata support
Jose M. Guisado Gomez [Thu, 20 Aug 2020 08:19:02 +0000 (10:19 +0200)] 
table: add userdata support

This patch adds NFT_TABLE_USERDATA and NFTNL_UDATA_TABLE_COMMENT to
support for table comments.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agolibnftnl: export nftnl_set_elem_fprintf
Florian Westphal [Thu, 20 Aug 2020 15:15:05 +0000 (17:15 +0200)] 
libnftnl: export nftnl_set_elem_fprintf

Was not exported so far due to a typo.  While at it, add const qualifier
to element structure.

Will be used to optionally dump set contents / elements from nft
frontend.

Signed-off-by: Florian Westphal <fw@strlen.de>
5 years agoudata: add NFTNL_UDATA_SET_COMMENT
Jose M. Guisado Gomez [Tue, 11 Aug 2020 14:27:19 +0000 (16:27 +0200)] 
udata: add NFTNL_UDATA_SET_COMMENT

This field is used to store an optional comment of a set.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexamples: add support for NF_PROTO_INET family
Jose M. Guisado Gomez [Mon, 27 Jul 2020 10:31:08 +0000 (12:31 +0200)] 
examples: add support for NF_PROTO_INET family

Add missing support for "inet" family for a handful of examples where
applicable.

Signed-off-by: Jose M. Guisado Gomez <guigom@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexamples: unbreak nft-set-elem-del
Pablo Neira Ayuso [Fri, 24 Jul 2020 13:48:29 +0000 (15:48 +0200)] 
examples: unbreak nft-set-elem-del

This code is missing the batch netlink routines. There was another bug,
the set element key size was not correct.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agosrc: add support for chain ID attribute
Pablo Neira Ayuso [Wed, 24 Jun 2020 14:27:00 +0000 (16:27 +0200)] 
src: add support for chain ID attribute

his patch allows you to refer to chains via the chain ID. The semantics
are similar to the NFTA_RULE_ID attribute.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agobuild: libnftnl 1.1.7 release libnftnl-1.1.7
Pablo Neira Ayuso [Fri, 5 Jun 2020 11:14:40 +0000 (13:14 +0200)] 
build: libnftnl 1.1.7 release

bump libmnl dependency too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoflowtable: relax logic to build NFTA_FLOWTABLE_HOOK
Pablo Neira Ayuso [Mon, 25 May 2020 15:57:15 +0000 (17:57 +0200)] 
flowtable: relax logic to build NFTA_FLOWTABLE_HOOK

The logic to build NFTA_FLOWTABLE_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>
5 years agoexpr: dynset: release stateful expression from .free path
Pablo Neira Ayuso [Wed, 6 May 2020 18:45:35 +0000 (20:45 +0200)] 
expr: dynset: release stateful expression from .free path

==22778==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x7f3212406518 in calloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9518)
    #1 0x7f321041703e in nftnl_expr_alloc /home/pablo/devel/scm/git-netfilter/libnftnl/src/expr.c:37
    #2 0x7f3211d51c16 in netlink_gen_limit_stmt /home/pablo/devel/scm/git-netfilter/nftables/src/netlink_linearize.c:859
    #3 0x7f3211d5220c in netlink_gen_stmt_stateful /home/pablo/devel/scm/git-netfilter/nftables/src/netlink_linearize.c:891
    #4 0x7f3211d58630 in netlink_gen_meter_stmt /home/pablo/devel/scm/git-netfilter/nftables/src/netlink_linearize.c:1441
[...]

SUMMARY: AddressSanitizer: 64 byte(s) leaked in 1 allocation(s).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoexpr: objref: add nftnl_expr_objref_free() to release object name
Pablo Neira Ayuso [Tue, 5 May 2020 18:49:51 +0000 (20:49 +0200)] 
expr: objref: add nftnl_expr_objref_free() to release object name

==4876==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x7f4e2c16b810 in strdup (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x3a810)
    #1 0x7f4e2a39906f in nftnl_expr_objref_set expr/objref.c:45
    #2 0x7f4e2a39906f in nftnl_expr_objref_set expr/objref.c:35

Direct leak of 16 byte(s) in 2 object(s) allocated from:
    #0 0x7f4018aa0810 in strdup (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x3a810)
    #1 0x7f4016b660af in nftnl_expr_objref_set expr/objref.c:53

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
5 years agoudata: add NFTNL_UDATA_SET_DATA_INTERVAL
Pablo Neira Ayuso [Mon, 27 Apr 2020 17:23:20 +0000 (19:23 +0200)] 
udata: add NFTNL_UDATA_SET_DATA_INTERVAL

Use this field to specify that set element data specifies an interval.

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