[PATCH v2 1/4] netlink: decode netlink message ack flags

JingPiao Chen chenjingpiao at gmail.com
Wed Jul 19 02:21:09 UTC 2017


* netlink.c: Include "xlat/netlink_ack_flags.h".
(decode_nlmsg_flags): Decode ack flags when type == NLMSG_ERROR.
* xlat/netlink_ack_flags.in: New file.
* NEWS: Mention this.
* tests/netlink_protocol.c (test_ack_flags): New function, check this.
(main): Use it.
---
 NEWS                      |  1 +
 netlink.c                 |  6 +++++-
 tests/netlink_protocol.c  | 42 ++++++++++++++++++++++++++++++++++++++++++
 xlat/netlink_ack_flags.in |  2 ++
 4 files changed, 50 insertions(+), 1 deletion(-)
 create mode 100644 xlat/netlink_ack_flags.in

diff --git a/NEWS b/NEWS
index 6d621e7..e09e10f 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,7 @@ Noteworthy changes in release ?.?? (????-??-??)
   * Implemented decoding of inet_diag_req_v2, inet_diag_req_compat,
     packet_diag_msg, and smc_diag_msg netlink attributes of NETLINK_SOCK_DIAG.
   * Implemented NETLINK_SELINUX protocol specific decoding.
+  * Implemented decoding of netlink message ack flags.
   * Updated lists of BPF_*, RWF_*, SCM_*, and SO_* constants.
 
 * Bug fixes
diff --git a/netlink.c b/netlink.c
index e054ae3..d3ad8b0 100644
--- a/netlink.c
+++ b/netlink.c
@@ -32,6 +32,7 @@
 #include <linux/audit.h>
 #include <linux/rtnetlink.h>
 #include <linux/xfrm.h>
+#include "xlat/netlink_ack_flags.h"
 #include "xlat/netlink_flags.h"
 #include "xlat/netlink_get_flags.h"
 #include "xlat/netlink_new_flags.h"
@@ -196,8 +197,11 @@ decode_nlmsg_flags(const uint16_t flags, const uint16_t type, const int family)
 {
 	const struct xlat *table = NULL;
 
-	if (type == NLMSG_DONE)
+	if (type < NLMSG_MIN_TYPE) {
+		if (type == NLMSG_ERROR)
+			table = netlink_ack_flags;
 		goto end;
+	}
 
 	switch (family) {
 	case NETLINK_CRYPTO:
diff --git a/tests/netlink_protocol.c b/tests/netlink_protocol.c
index ee5bf0f..be1a652 100644
--- a/tests/netlink_protocol.c
+++ b/tests/netlink_protocol.c
@@ -363,6 +363,45 @@ test_nlmsg_done(const int fd)
 	       fd, nlh->nlmsg_len, num, nlh->nlmsg_len, sprintrc(rc));
 }
 
+#if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
+static void
+test_ack_flags(const int fd)
+{
+	long rc;
+	struct nlmsghdr nlh = {
+		.nlmsg_len = sizeof(nlh),
+		.nlmsg_type = NLMSG_ERROR,
+	};
+
+#ifdef NLM_F_CAPPED
+	nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED,
+	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+	printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+	       ", flags=NLM_F_REQUEST|NLM_F_CAPPED, seq=0, pid=0}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
+
+#ifdef NLM_F_ACK_TLVS
+	nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK_TLVS;
+	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+	printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+	       ", flags=NLM_F_REQUEST|NLM_F_ACK_TLVS, seq=0, pid=0}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
+
+#if defined NLM_F_CAPPED && defined NLM_F_ACK_TLVS
+	nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED | NLM_F_ACK_TLVS;
+	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+	printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+	       ", flags=NLM_F_REQUEST|NLM_F_CAPPED|NLM_F_ACK_TLVS, seq=0, pid=0}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
+}
+#endif
+
 int main(void)
 {
 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
@@ -378,6 +417,9 @@ int main(void)
 	send_query(fd);
 	test_nlmsgerr(fd);
 	test_nlmsg_done(fd);
+#if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
+	test_ack_flags(fd);
+#endif
 
 	puts("+++ exited with 0 +++");
 	return 0;
diff --git a/xlat/netlink_ack_flags.in b/xlat/netlink_ack_flags.in
new file mode 100644
index 0000000..2aabb84
--- /dev/null
+++ b/xlat/netlink_ack_flags.in
@@ -0,0 +1,2 @@
+NLM_F_CAPPED
+NLM_F_ACK_TLVS
-- 
2.7.4





More information about the Strace-devel mailing list