Skip to contentSkip to conversation

src: Add nfq_nlmsg_put2() - user specifies header flags

PATCH Superseded Duncan Roe 2 participants 5 messages Nov 20, 2023 – Dec 6, 2023 netfilter

lore
Share & tools
lore

Duncan Roe

author
#0 lore raw

PATCH v4 1/1 src: Add nfq_nlmsg_put2() - user specifies header flags

Enable mnl programs to check whether a config request was accepted. (nfnl programs do this already).

v4: other requested changes

v3: force on NLM_F_REQUEST

v2: take flags as an arg (Pablo request)

---
 .../libnetfilter_queue/libnetfilter_queue.h   |  1 +
 src/nlmsg.c                                   | 54 ++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)
include/libnetfilter_queue/libnetfilter_queue.h +1 -0
index 3d8e444..f254984 100644
@@ -151,6 +151,7 @@ void nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt, uint32_t p
 
 int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
 struct nlmsghdr *nfq_nlmsg_put(char *buf, int type, uint32_t queue_num);
+struct nlmsghdr *nfq_nlmsg_put2(char *buf, int type, uint32_t queue_num, uint16_t flags);
 
 #ifdef __cplusplus
 } /* extern "C" */
src/nlmsg.c +53 -1

63 lines not on this page · raw message

Signature · 1 line
2.35.8
#1 lore raw
Quoted 33 lines · Duncan Roe

Enable mnl programs to check whether a config request was accepted. (nfnl programs do this already).

v4: other requested changes

v3: force on NLM_F_REQUEST

v2: take flags as an arg (Pablo request)

---
 .../libnetfilter_queue/libnetfilter_queue.h   |  1 +
 src/nlmsg.c                                   | 54 ++++++++++++++++++-
 2 files changed, 54 insertions(+), 1 deletion(-)
include/libnetfilter_queue/libnetfilter_queue.h +1 -0
index 3d8e444..f254984 100644
@@ -151,6 +151,7 @@ void nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt, uint32_t p
 
 int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
 struct nlmsghdr *nfq_nlmsg_put(char *buf, int type, uint32_t queue_num);
+struct nlmsghdr *nfq_nlmsg_put2(char *buf, int type, uint32_t queue_num, uint16_t flags);
 
 #ifdef __cplusplus
 } /* extern "C" */
src/nlmsg.c +0 -0
index 5400dd7..0c6229e 100644
@@ -309,10 +309,62 @@ int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
  */
 EXPORT_SYMBOL

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 320

Quoted 10 lines
src/nlmsg.c +9 -0
 struct nlmsghdr *nfq_nlmsg_put(char *buf, int type, uint32_t queue_num)
+{
+	return nfq_nlmsg_put2(buf, type, queue_num, 0);
+}
+
+/**
+ * nfq_nlmsg_put2 - Set up a netlink header with user-specified flags
+ *                  in a memory buffer
+ * \param *buf Pointer to memory buffer
+ * \param type Either NFQNL_MSG_CONFIG or NFQNL_MSG_VERDICT

This can be any value in enum nfqnl_msg_types.

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 322

Quoted 2 lines
src/nlmsg.c +2 -0
+ * \param queue_num Queue number
+ * \param flags additional flags to put in message header, commonly NLM_F_ACK

This can be any NLM_F_* flag, as define in include/linux/netlink.h

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 326

Quoted 4 lines
src/nlmsg.c +4 -0
+ * \returns Pointer to netlink header
+ *
+ * Use NLM_F_ACK before performing an action that might fail, e.g.
+ * attempt to configure NFQA_CFG_F_SECCTX on a system not runnine SELinux.

typo: 'runnine'

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 331

Quoted 5 lines
src/nlmsg.c +5 -0
+ * \n
+ * The kernel always sends a message in response to a failed command.
+ * NLM_F_ACK instructs the kernel to also send a message in response
+ * to a successful command.
+ * \n

Please, also specify that recommended buffer size in this case is MNL_SOCKET_BUFFER_SIZE.

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 333

Quoted 2 lines
src/nlmsg.c +2 -0
+ * This code snippet demonstrates reading these responses:
+ * \verbatim

I'd suggest to add:

        char nltxbuf[MNL_SOCKET_BUFFER_SIZE];
Quoted 13 lines
src/nlmsg.c +13 -0
+	nlh = nfq_nlmsg_put2(nltxbuf, NFQNL_MSG_CONFIG, queue_num, NLM_F_ACK);
+	mnl_attr_put_u32(nlh, NFQA_CFG_FLAGS, NFQA_CFG_F_SECCTX);
+	mnl_attr_put_u32(nlh, NFQA_CFG_MASK, NFQA_CFG_F_SECCTX);
+
+	if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
+		perror("mnl_socket_send");
+		exit(EXIT_FAILURE);
+	}
+
+	ret = mnl_socket_recvfrom(nl, nlrxbuf, sizeof nlrxbuf);
+	if (ret == -1) {
+		perror("mnl_socket_recvfrom");
+		exit(EXIT_FAILURE);

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 356

Quoted 10 lines
src/nlmsg.c +10 -0
+	}
+
+	ret = mnl_cb_run(nlrxbuf, ret, 0, portid, NULL, NULL);
+	if (ret == -1)
+		perror("configure NFQA_CFG_F_SECCTX");
+\endverbatim
+ *
+ * \note
+ * The program above can continue after the error because NFQA_CFG_F_SECCTX
+ * was the only item in the preceding **mnl_socket_sendto**.

Not sure what you mean in this sentence. The program above can continue because you do not bail out mnl_cb_run().

Suggestion: To keep it simpler, I would simply do exit(EXIT_FAILURE) in the example above and remove these two sentences.

Here above you are requesting to toggle this flag, if kernel reports an error (which one? EOPNOTSUPP?) then this means such NFAQ_CFG_F_SECCTX feature is not available.

Please, send v5, this is looking better and better, thanks!

author
#2 lore raw

v5 cover src: Add nfq_nlmsg_put2() - user specifies header flags

Hi Pablo,

This is as you requested, except as below and the sample prints the message

Quoted 1 line

"This kernel version does not allow to retrieve security context.\n"

(i.e. same as what utils/nfqnl_test.c does) instead of exit(EXIT_FAILURE) as you suggested.
Looking at an 80cc man page, I think NLM_F_xxx looks better than NLM_F_*. Also the code sample wrapped on the nfq_nlmsg_put2() line, so split it. The code sample mixed nlrxbuf and nltxbuf. Now all just buf.

Quoted 1 line · Pablo Neira Ayuso

On Mon, Nov 20, 2023 at 12:08:49PM +1100, Duncan Roe wrote:

[...]

Quoted 9 lines

src/nlmsg.c int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr) · line 320

Quoted 6 lines
src/nlmsg.c +6 -0
+
+/**
+ * nfq_nlmsg_put2 - Set up a netlink header with user-specified flags
+ *                  in a memory buffer
+ * \param *buf Pointer to memory buffer
+ * \param type Either NFQNL_MSG_CONFIG or NFQNL_MSG_VERDICT

This can be any value in enum nfqnl_msg_types.

I think not. NFQNL_MSG_PACKET is from kernel to userspace. Have added NFQNL_MSG_VERDICT_BATCH to complete the list of allowed values.

Cheers ... Duncan.

Duncan Roe (1):
src: Add nfq_nlmsg_put2() - user specifies header flags

 .../libnetfilter_queue/libnetfilter_queue.h   |  1 +
 src/nlmsg.c                                   | 57 ++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
Signature · 1 line
2.35.8
author
#3 lore raw

PATCH v5 1/1 src: Add nfq_nlmsg_put2() - user specifies header flags

In reply to Pablo Neira Ayuso

Enable mnl programs to check whether a config request was accepted. (nfnl programs do this already).

v5: documentation tweaks

v4: other requested changes

v3: force on NLM_F_REQUEST

v2: take flags as an arg (Pablo request)

---
 .../libnetfilter_queue/libnetfilter_queue.h   |  1 +
 src/nlmsg.c                                   | 57 ++++++++++++++++++-
 2 files changed, 57 insertions(+), 1 deletion(-)
include/libnetfilter_queue/libnetfilter_queue.h +1 -0
index 3d8e444..f254984 100644
@@ -151,6 +151,7 @@ void nfq_nlmsg_verdict_put_pkt(struct nlmsghdr *nlh, const void *pkt, uint32_t p
 
 int nfq_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr);
 struct nlmsghdr *nfq_nlmsg_put(char *buf, int type, uint32_t queue_num);
+struct nlmsghdr *nfq_nlmsg_put2(char *buf, int type, uint32_t queue_num, uint16_t flags);
 
 #ifdef __cplusplus
 } /* extern "C" */
src/nlmsg.c +56 -1

66 lines not on this page · raw message

Signature · 1 line
2.35.8
maintainer Applied
#4 lore raw
Quoted 2 lines · Duncan Roe

Enable mnl programs to check whether a config request was accepted. (nfnl programs do this already).

Applied, thanks