[PATCH 01/10] netlink: decode AF_NETLINK diag attributes

JingPiao Chen chenjingpiao at gmail.com
Wed Jun 21 02:42:14 UTC 2017


* netlink_sock_diag.c (netlink_decoders): New array.
(decode_netlink_diag_msg): Use it.
* linux/netlink_diag.h (netlink_diag_ring): New structure.
---
 linux/netlink_diag.h |  7 +++++++
 netlink_sock_diag.c  | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/linux/netlink_diag.h b/linux/netlink_diag.h
index ad13aa9..11995a0 100644
--- a/linux/netlink_diag.h
+++ b/linux/netlink_diag.h
@@ -23,6 +23,13 @@ struct netlink_diag_msg {
 	uint32_t ndiag_cookie[2];
 };
 
+struct netlink_diag_ring {
+	uint32_t ndr_block_size;
+	uint32_t ndr_block_nr;
+	uint32_t ndr_frame_size;
+	uint32_t ndr_frame_nr;
+};
+
 enum {
 	NETLINK_DIAG_MEMINFO,
 	NETLINK_DIAG_GROUPS,
diff --git a/netlink_sock_diag.c b/netlink_sock_diag.c
index dab0f1a..f5bdf84 100644
--- a/netlink_sock_diag.c
+++ b/netlink_sock_diag.c
@@ -216,6 +216,60 @@ decode_netlink_diag_req(struct tcb *const tcp,
 	tprints("}");
 }
 
+static bool
+print_group(struct tcb *tcp, void *elem_buf,
+	    size_t elem_size, void *opaque_data)
+{
+	if (elem_size < sizeof(kernel_ulong_t))
+		tprintf("%#0*x", (int) elem_size * 2 + 2,
+			*(unsigned int *) elem_buf);
+	else
+		tprintf("%#0*" PRI_klx, (int) elem_size * 2 + 2,
+			*(kernel_ulong_t *) elem_buf);
+
+	return true;
+}
+
+static bool
+decode_netlink_diag_groups(struct tcb *tcp, kernel_ulong_t addr,
+			   kernel_ulong_t len, void *const opaque_data)
+{
+	int nmemb = len / current_wordsize;
+	kernel_ulong_t buf;
+
+	print_array(tcp, addr, nmemb, &buf, current_wordsize,
+		    umoven_or_printaddr, print_group, 0);
+
+	return true;
+}
+
+static bool
+decode_netlink_diag_ring(struct tcb *tcp, kernel_ulong_t addr,
+			 kernel_ulong_t len, void *const opaque_data)
+{
+	struct netlink_diag_ring ndr;
+
+	if (len < sizeof(ndr))
+		return false;
+	if (umove_or_printaddr(tcp, addr, &ndr))
+		return true;
+
+	tprintf("{ndr_block_size=%" PRIu32 ", ndr_block_nr=%" PRIu32
+		", ndr_frame_size=%" PRIu32 ", ndr_frame_nr=%" PRIu32,
+		ndr.ndr_block_size, ndr.ndr_block_nr,
+		ndr.ndr_frame_size, ndr.ndr_frame_nr);
+	tprints("}");
+
+	return true;
+}
+
+static nla_decoder_t netlink_decoders[] = {
+	[NETLINK_DIAG_MEMINFO]	= decode_meminfo,
+	[NETLINK_DIAG_GROUPS]	= decode_netlink_diag_groups,
+	[NETLINK_DIAG_RX_RING]	= decode_netlink_diag_ring,
+	[NETLINK_DIAG_TX_RING]	= decode_netlink_diag_ring
+};
+
 static void
 decode_netlink_diag_msg(struct tcb *const tcp,
 			const struct nlmsghdr *const nlmsghdr,
@@ -266,7 +320,8 @@ decode_netlink_diag_msg(struct tcb *const tcp,
 		tprints(", ");
 		decode_nlattr(tcp, addr + offset, len - offset,
 			      netlink_diag_attrs, "NETLINK_DIAG_???",
-			      NULL, 0, NULL);
+			      netlink_decoders,
+			      ARRAY_SIZE(netlink_decoders), NULL);
 	}
 }
 
-- 
2.7.4





More information about the Strace-devel mailing list