From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper
Date: Wed, 8 Nov 2023 19:24:25 +0100 [thread overview]
Message-ID: <20231108182431.4005745-2-thaller@redhat.com> (raw)
In-Reply-To: <20231108182431.4005745-1-thaller@redhat.com>
We don't want to handle allocation errors, but crash via memory_allocation_error().
Also, we usually just allocate NFT_USERDATA_MAXLEN buffers.
Add a helper for that and use it.
Signed-off-by: Thomas Haller <thaller@redhat.com>
---
include/netlink.h | 3 +++
src/mnl.c | 16 ++++------------
src/netlink.c | 7 ++-----
src/netlink_linearize.c | 4 +---
4 files changed, 10 insertions(+), 20 deletions(-)
diff --git a/include/netlink.h b/include/netlink.h
index 6766d7e8563f..15cbb332c8dd 100644
--- a/include/netlink.h
+++ b/include/netlink.h
@@ -260,4 +260,7 @@ struct nft_expr_loc *nft_expr_loc_find(const struct nftnl_expr *nle,
struct dl_proto_ctx *dl_proto_ctx(struct rule_pp_ctx *ctx);
+#define _nftnl_udata_buf_alloc() \
+ memory_allocation_check(nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN))
+
#endif /* NFTABLES_NETLINK_H */
diff --git a/src/mnl.c b/src/mnl.c
index 0fb36bd588ee..1263c611cd20 100644
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -823,9 +823,7 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd,
CHAIN_F_HW_OFFLOAD);
}
if (cmd->chain->comment) {
- udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udbuf)
- memory_allocation_error();
+ udbuf = _nftnl_udata_buf_alloc();
if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_CHAIN_COMMENT, cmd->chain->comment))
memory_allocation_error();
nftnl_chain_set_data(nlc, NFTNL_CHAIN_USERDATA, nftnl_udata_buf_data(udbuf),
@@ -1057,9 +1055,7 @@ int mnl_nft_table_add(struct netlink_ctx *ctx, struct cmd *cmd,
nftnl_table_set_u32(nlt, NFTNL_TABLE_FLAGS, cmd->table->flags);
if (cmd->table->comment) {
- udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udbuf)
- memory_allocation_error();
+ udbuf = _nftnl_udata_buf_alloc();
if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_TABLE_COMMENT, cmd->table->comment))
memory_allocation_error();
nftnl_table_set_data(nlt, NFTNL_TABLE_USERDATA, nftnl_udata_buf_data(udbuf),
@@ -1256,9 +1252,7 @@ int mnl_nft_set_add(struct netlink_ctx *ctx, struct cmd *cmd,
nftnl_set_set_u32(nls, NFTNL_SET_DESC_SIZE, set->init->size);
}
- udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udbuf)
- memory_allocation_error();
+ udbuf = _nftnl_udata_buf_alloc();
if (!nftnl_udata_put_u32(udbuf, NFTNL_UDATA_SET_KEYBYTEORDER,
set->key->byteorder))
memory_allocation_error();
@@ -1453,9 +1447,7 @@ int mnl_nft_obj_add(struct netlink_ctx *ctx, struct cmd *cmd,
nftnl_obj_set_u32(nlo, NFTNL_OBJ_TYPE, obj->type);
if (obj->comment) {
- udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udbuf)
- memory_allocation_error();
+ udbuf = _nftnl_udata_buf_alloc();
if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_OBJ_COMMENT, obj->comment))
memory_allocation_error();
nftnl_obj_set_data(nlo, NFTNL_OBJ_USERDATA, nftnl_udata_buf_data(udbuf),
diff --git a/src/netlink.c b/src/netlink.c
index 120a8ba9ceb1..0c858065ca15 100644
--- a/src/netlink.c
+++ b/src/netlink.c
@@ -175,11 +175,8 @@ struct nftnl_set_elem *alloc_nftnl_setelem(const struct expr *set,
netlink_gen_stmt_stateful(stmt));
}
}
- if (elem->comment || expr->elem_flags) {
- udbuf = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udbuf)
- memory_allocation_error();
- }
+ if (elem->comment || expr->elem_flags)
+ udbuf = _nftnl_udata_buf_alloc();
if (elem->comment) {
if (!nftnl_udata_put_strz(udbuf, NFTNL_UDATA_SET_ELEM_COMMENT,
elem->comment))
diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c
index 0c62341112d8..b5adc4d186c8 100644
--- a/src/netlink_linearize.c
+++ b/src/netlink_linearize.c
@@ -1760,9 +1760,7 @@ void netlink_linearize_rule(struct netlink_ctx *ctx,
if (rule->comment) {
struct nftnl_udata_buf *udata;
- udata = nftnl_udata_buf_alloc(NFT_USERDATA_MAXLEN);
- if (!udata)
- memory_allocation_error();
+ udata = _nftnl_udata_buf_alloc();
if (!nftnl_udata_put_strz(udata, NFTNL_UDATA_RULE_COMMENT,
rule->comment))
--
2.41.0
next prev parent reply other threads:[~2023-11-08 18:25 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-08 18:24 [PATCH nft 1/2] utils: add memory_allocation_check() helper Thomas Haller
2023-11-08 18:24 ` Thomas Haller [this message]
2023-11-09 15:14 ` [PATCH nft 2/2] netlink: add and use _nftnl_udata_buf_alloc() helper Pablo Neira Ayuso
2023-11-09 15:19 ` Thomas Haller
2023-11-09 15:32 ` Pablo Neira Ayuso
2023-11-09 16:48 ` Thomas Haller
2023-11-09 19:12 ` Pablo Neira Ayuso
2023-11-09 15:24 ` [PATCH nft 1/2] utils: add memory_allocation_check() helper Pablo Neira Ayuso
2023-11-09 17:02 ` Thomas Haller
2023-11-15 8:52 ` Florian Westphal
2023-11-15 9:06 ` Thomas Haller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231108182431.4005745-2-thaller@redhat.com \
--to=thaller@redhat.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).