[PATCH 3/8] netlink: add a basic socket diag parser of AF_PACKET messages

JingPiao Chen chenjingpiao at gmail.com
Tue Jun 13 14:13:20 UTC 2017


* linux/packet_diag.h: New file.
* Makefile.am (EXTRA_DIST): Add linux/packet_diag.h.
* defs.h (ethernet_protocols): New xlat prototype.
* netlink_sock_diag.c: Include <linux/packet_diag.h>
and "xlat/packet_diag_show.h".
(decode_packet_diag_req, decode_packet_diag_msg): New functions.
(diag_decoders): Add AF_PACKET.
* xlat/packet_diag_show.in: New file.

Co-authored-by: Fabien Siron <fabien.siron at epita.fr>
---
 Makefile.am              |  1 +
 defs.h                   |  1 +
 linux/packet_diag.h      | 29 ++++++++++++++++++++++++++++
 netlink_sock_diag.c      | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 xlat/packet_diag_show.in |  6 ++++++
 5 files changed, 87 insertions(+)
 create mode 100644 linux/packet_diag.h
 create mode 100644 xlat/packet_diag_show.in

diff --git a/Makefile.am b/Makefile.am
index 2266fa2..75a9828 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -564,6 +564,7 @@ EXTRA_DIST =				\
 	linux/or1k/set_scno.c		\
 	linux/or1k/syscallent.h		\
 	linux/or1k/userent.h		\
+	linux/packet_diag.h		\
 	linux/personality.h		\
 	linux/powerpc/arch_regs.c	\
 	linux/powerpc/arch_regs.h	\
diff --git a/defs.h b/defs.h
index 3b60d3d..f7d25f0 100644
--- a/defs.h
+++ b/defs.h
@@ -291,6 +291,7 @@ extern const struct xlat addrfams[];
 extern const struct xlat at_flags[];
 extern const struct xlat clocknames[];
 extern const struct xlat dirent_types[];
+extern const struct xlat ethernet_protocols[];
 extern const struct xlat evdev_abs[];
 extern const struct xlat msg_flags[];
 extern const struct xlat netlink_protocols[];
diff --git a/linux/packet_diag.h b/linux/packet_diag.h
new file mode 100644
index 0000000..24d0694
--- /dev/null
+++ b/linux/packet_diag.h
@@ -0,0 +1,29 @@
+#ifndef STRACE_LINUX_PACKET_DIAG_H
+#define STRACE_LINUX_PACKET_DIAG_H
+
+struct packet_diag_req {
+	uint8_t sdiag_family;
+	uint8_t sdiag_protocol;
+	uint16_t pad;
+	uint32_t pdiag_ino;
+	uint32_t pdiag_show;
+	uint32_t pdiag_cookie[2];
+};
+
+#define PACKET_SHOW_INFO	0x00000001 /* Basic packet_sk information */
+#define PACKET_SHOW_MCLIST	0x00000002 /* A set of packet_diag_mclist-s */
+#define PACKET_SHOW_RING_CFG	0x00000004 /* Rings configuration parameters */
+#define PACKET_SHOW_FANOUT	0x00000008
+#define PACKET_SHOW_MEMINFO	0x00000010
+#define PACKET_SHOW_FILTER	0x00000020
+
+struct packet_diag_msg {
+	uint8_t pdiag_family;
+	uint8_t pdiag_type;
+	uint16_t pdiag_num;
+
+	uint32_t pdiag_ino;
+	uint32_t pdiag_cookie[2];
+};
+
+#endif /* !STRACE_LINUX_PACKET_DIAG_H */
diff --git a/netlink_sock_diag.c b/netlink_sock_diag.c
index 0b37803..3f153a8 100644
--- a/netlink_sock_diag.c
+++ b/netlink_sock_diag.c
@@ -32,6 +32,7 @@
 #include <sys/socket.h>
 #include <linux/netlink.h>
 #include <linux/netlink_diag.h>
+#include <linux/packet_diag.h>
 #include <linux/unix_diag.h>
 
 #include "xlat/tcp_states.h"
@@ -40,6 +41,8 @@
 #include "xlat/netlink_diag_show.h"
 #include "xlat/netlink_states.h"
 
+#include "xlat/packet_diag_show.h"
+
 #include "xlat/unix_diag_show.h"
 
 static void
@@ -161,10 +164,57 @@ decode_netlink_diag_msg(struct tcb *const tcp,
 	return true;
 }
 
+static bool
+decode_packet_diag_req(struct tcb *const tcp,
+		       const struct nlmsghdr *const nlmsghdr,
+		       const kernel_ulong_t addr,
+		       const kernel_ulong_t len)
+{
+	struct packet_diag_req req;
+
+	if (len < sizeof(req) || umove(tcp, addr, &req) < 0)
+		return false;
+
+	tprints("{sdiag_family=");
+	printxval(addrfams, req.sdiag_family, "AF_???");
+	tprints(", sdiag_protocol=");
+	printxval(ethernet_protocols, req.sdiag_protocol, "ETH_P_???");
+	tprintf(", pdiag_ino=%" PRIu32 ", pdiag_show=", req.pdiag_ino);
+	printflags(packet_diag_show, req.pdiag_show, "PACKET_SHOW_???");
+	tprintf(", pdiag_cookie=[%" PRIu32 ", %" PRIu32 "]}",
+		req.pdiag_cookie[0], req.pdiag_cookie[1]);
+
+	return true;
+}
+
+static bool
+decode_packet_diag_msg(struct tcb *const tcp,
+		       const struct nlmsghdr *const nlmsghdr,
+		       const kernel_ulong_t addr,
+		       const kernel_ulong_t len)
+{
+	struct packet_diag_msg msg;
+
+	if (len < sizeof(msg) || umove(tcp, addr, &msg) < 0)
+		return false;
+
+	tprints("{pdiag_family=");
+	printxval(addrfams, msg.pdiag_family, "AF_???");
+	tprints(", pdiag_type=");
+	printxval(socktypes, msg.pdiag_type, "SOCK_???");
+	tprintf(", pdiag_num=%" PRIu16 ", pdiag_ino=%" PRIu32
+		", pdiag_cookie=[%" PRIu32 ", %" PRIu32 "]}",
+		msg.pdiag_num, msg.pdiag_ino, msg.pdiag_cookie[0],
+		msg.pdiag_cookie[1]);
+
+	return true;
+}
+
 static const struct {
 	const netlink_decoder_t request, response;
 } diag_decoders[] = {
 	[AF_NETLINK] = { decode_netlink_diag_req, decode_netlink_diag_msg },
+	[AF_PACKET] = { decode_packet_diag_req, decode_packet_diag_msg },
 	[AF_UNIX] = { decode_unix_diag_req, decode_unix_diag_msg }
 };
 
diff --git a/xlat/packet_diag_show.in b/xlat/packet_diag_show.in
new file mode 100644
index 0000000..b495af8
--- /dev/null
+++ b/xlat/packet_diag_show.in
@@ -0,0 +1,6 @@
+PACKET_SHOW_INFO
+PACKET_SHOW_MCLIST
+PACKET_SHOW_RING_CFG
+PACKET_SHOW_FANOUT
+PACKET_SHOW_MEMINFO
+PACKET_SHOW_FILTER
-- 
2.7.4





More information about the Strace-devel mailing list