[PATCH v6 03/18] netlink: add a basic netlink attribute parser of AF_INET diag

JingPiao Chen chenjingpiao at gmail.com
Tue Jun 27 10:10:05 UTC 2017


* linux/inet_diag.h (INET_DIAG_REQ_*): New enum.
* netlink_sock_diag.c: Include "xlat/inet_diag_attrs.h"
and "xlat/inet_diag_req_attrs.h".
(decode_inet_diag_req_compat, decode_inet_diag_req_v2,
 decode_inet_diag_msg): Use decode_nlattr.
* xlat/inet_diag_attrs.in: New file.
* xlat/inet_diag_req_attrs.in: Likewise.

Co-authored-by: Fabien Siron <fabien.siron at epita.fr>
---
 linux/inet_diag.h           |  5 +++++
 netlink_sock_diag.c         | 35 ++++++++++++++++++++++++++++++++---
 xlat/inet_diag_attrs.in     | 18 ++++++++++++++++++
 xlat/inet_diag_req_attrs.in |  3 +++
 4 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 xlat/inet_diag_attrs.in
 create mode 100644 xlat/inet_diag_req_attrs.in

diff --git a/linux/inet_diag.h b/linux/inet_diag.h
index 245e476..e1df3bb 100644
--- a/linux/inet_diag.h
+++ b/linux/inet_diag.h
@@ -34,6 +34,11 @@ struct inet_diag_req_v2 {
 	struct inet_diag_sockid id;
 };
 
+enum {
+	INET_DIAG_REQ_NONE,
+	INET_DIAG_REQ_BYTECODE,
+};
+
 /* Info structure */
 struct inet_diag_msg {
 	uint8_t idiag_family;
diff --git a/netlink_sock_diag.c b/netlink_sock_diag.c
index 82d89b2..59d6240 100644
--- a/netlink_sock_diag.c
+++ b/netlink_sock_diag.c
@@ -40,7 +40,9 @@
 #endif
 #include <linux/unix_diag.h>
 
+#include "xlat/inet_diag_attrs.h"
 #include "xlat/inet_diag_extended_flags.h"
+#include "xlat/inet_diag_req_attrs.h"
 
 #include "xlat/tcp_states.h"
 #include "xlat/tcp_state_flags.h"
@@ -335,7 +337,8 @@ decode_inet_diag_req_compat(struct tcb *const tcp,
 			    const kernel_ulong_t len)
 {
 	struct inet_diag_req req = { .idiag_family = family };
-	const size_t offset = sizeof(req.idiag_family);
+	size_t offset = sizeof(req.idiag_family);
+	bool decode_nla = false;
 
 	tprints("{idiag_family=");
 	printxval(addrfams, req.idiag_family, "AF_???");
@@ -358,10 +361,18 @@ decode_inet_diag_req_compat(struct tcb *const tcp,
 			printflags(tcp_state_flags, req.idiag_states,
 				   "1<<TCP_???");
 			tprintf(", idiag_dbs=%" PRIu32, req.idiag_dbs);
+			decode_nla = true;
 		}
 	} else
 		tprints("...");
 	tprints("}");
+
+	offset = NLA_ALIGN(sizeof(req));
+	if (decode_nla && len > offset) {
+		tprints(", ");
+		decode_nlattr(tcp, addr + offset, len - offset,
+			      inet_diag_req_attrs, "INET_DIAG_REQ_???");
+	}
 }
 
 static void
@@ -372,7 +383,8 @@ decode_inet_diag_req_v2(struct tcb *const tcp,
 			const kernel_ulong_t len)
 {
 	struct inet_diag_req_v2 req = { .sdiag_family = family };
-	const size_t offset = sizeof(req.sdiag_family);
+	size_t offset = sizeof(req.sdiag_family);
+	bool decode_nla = false;
 
 	tprints("{sdiag_family=");
 	printxval(addrfams, req.sdiag_family, "AF_???");
@@ -393,10 +405,18 @@ decode_inet_diag_req_v2(struct tcb *const tcp,
 				   "1<<TCP_???");
 			tprints(", id=");
 			print_inet_diag_sockid(&req.id, req.sdiag_family);
+			decode_nla = true;
 		}
 	} else
 		tprints("...");
 	tprints("}");
+
+	offset = NLA_ALIGN(sizeof(req));
+	if (decode_nla && len > offset) {
+		tprints(", ");
+		decode_nlattr(tcp, addr + offset, len - offset,
+			      inet_diag_req_attrs, "INET_DIAG_REQ_???");
+	}
 }
 
 static void
@@ -423,7 +443,8 @@ decode_inet_diag_msg(struct tcb *const tcp,
 		     const kernel_ulong_t len)
 {
 	struct inet_diag_msg msg = { .idiag_family = family };
-	const size_t offset = sizeof(msg.idiag_family);
+	size_t offset = sizeof(msg.idiag_family);
+	bool decode_nla = false;
 
 	tprints("{idiag_family=");
 	printxval(addrfams, msg.idiag_family, "AF_???");
@@ -450,10 +471,18 @@ decode_inet_diag_msg(struct tcb *const tcp,
 				msg.idiag_wqueue,
 				msg.idiag_uid,
 				msg.idiag_inode);
+			decode_nla = true;
 		}
 	} else
 		tprints("...");
 	tprints("}");
+
+	offset = NLA_ALIGN(sizeof(msg));
+	if (decode_nla && len > offset) {
+		tprints(", ");
+		decode_nlattr(tcp, addr + offset, len - offset,
+			      inet_diag_attrs, "INET_DIAG_???");
+	}
 }
 
 #ifdef AF_SMC
diff --git a/xlat/inet_diag_attrs.in b/xlat/inet_diag_attrs.in
new file mode 100644
index 0000000..17519c8
--- /dev/null
+++ b/xlat/inet_diag_attrs.in
@@ -0,0 +1,18 @@
+#unconditional
+INET_DIAG_NONE
+INET_DIAG_MEMINFO
+INET_DIAG_INFO
+INET_DIAG_VEGASINFO
+INET_DIAG_CONG
+INET_DIAG_TOS
+INET_DIAG_TCLASS
+INET_DIAG_SKMEMINFO
+INET_DIAG_SHUTDOWN
+INET_DIAG_DCTCPINFO
+INET_DIAG_PROTOCOL
+INET_DIAG_SKV6ONLY
+INET_DIAG_LOCALS
+INET_DIAG_PEERS
+INET_DIAG_PAD
+INET_DIAG_MARK
+INET_DIAG_BBRINFO
diff --git a/xlat/inet_diag_req_attrs.in b/xlat/inet_diag_req_attrs.in
new file mode 100644
index 0000000..c36369e
--- /dev/null
+++ b/xlat/inet_diag_req_attrs.in
@@ -0,0 +1,3 @@
+#unconditional
+INET_DIAG_REQ_NONE
+INET_DIAG_REQ_BYTECODE
-- 
2.7.4





More information about the Strace-devel mailing list