[PATCH 8/8] tests: check decoding of NETLINK_SOCK_DIAG AF_SMC messages

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


* tests/netlink_sock_diag.c: Include <linux/smc.h>
and <linux/smc_diag.h>.
(test_smc_diag_req, test_smc_diag_msg): New functions.
(main): Use them.
---
 tests/netlink_sock_diag.c | 266 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 266 insertions(+)

diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c
index 8171e2e..50ef507 100644
--- a/tests/netlink_sock_diag.c
+++ b/tests/netlink_sock_diag.c
@@ -40,6 +40,10 @@
 #include <linux/netlink.h>
 #include <linux/netlink_diag.h>
 #include <linux/packet_diag.h>
+#ifdef AF_SMC
+# include <linux/smc.h>
+# include <linux/smc_diag.h>
+#endif
 #include <linux/sock_diag.h>
 #include <linux/unix_diag.h>
 
@@ -1186,6 +1190,264 @@ test_inet_diag_msg(const int fd)
 	       sprintrc(rc));
 }
 
+#ifdef AF_SMC
+static void
+test_smc_diag_req(const int fd)
+{
+	const char address[] = "8.8.8.8";
+	struct nlmsghdr *nlh;
+	struct smc_diag_req *req;
+	uint8_t *family;
+	void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+	long rc;
+
+	/* print family only */
+	nlh = nlh0 - sizeof(*family);
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_REQUEST,
+	};
+
+	family = NLMSG_DATA(nlh);
+	*family = AF_SMC;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_SMC}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* print unknown family */
+	*family = 0xff;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=0xff /* AF_??? */}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* short read of family */
+	memmove(nlh0, nlh, NLMSG_HDRLEN);
+	nlh = nlh0;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, %p}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_DATA(nlh),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* print family and string */
+	nlh = nlh0 - sizeof(*family) - 4;
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_REQUEST,
+	};
+
+	family = NLMSG_DATA(nlh);
+	*family = AF_SMC;
+	memcpy(family + 1, "1234", 4);
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+	       ", {family=AF_SMC, \"1234\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+	       sprintrc(rc));
+
+	/* print smc_diag_req */
+	nlh = nlh0 - sizeof(*req);
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_REQUEST,
+	};
+
+	req = NLMSG_DATA(nlh);
+	*req = (struct smc_diag_req) {
+		.diag_family = AF_SMC,
+		.diag_ext = 1 << (SMC_DIAG_CONNINFO - 1),
+	};
+
+	if (!inet_pton(AF_INET, address, req->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, req->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {diag_family=AF_SMC"
+	       ", diag_ext=(1<<(SMC_DIAG_CONNINFO-1)), id={idiag_sport=htons(0)"
+	       ", idiag_dport=htons(0), inet_pton(AF_INET, \"%s\", idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", idiag_dst), idiag_if=0"
+	       ", idiag_cookie=[0, 0]}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address,
+	       nlh->nlmsg_len, sprintrc(rc));
+
+	/* short read of smc_diag_req */
+	nlh = nlh0 - sizeof(*family);
+	memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*family));
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
+		    NULL, 0);
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_SMC, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+	       NLMSG_DATA(nlh) + 1,
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*req),
+	       sprintrc(rc));
+}
+
+static void
+test_smc_diag_msg(const int fd)
+{
+	const char address[] = "8.8.8.8";
+	struct nlmsghdr *nlh;
+	struct smc_diag_msg *msg;
+	uint8_t *family;
+	void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+	long rc;
+
+	/* print family only */
+	nlh = nlh0 - sizeof(*family);
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP,
+	};
+
+	family = NLMSG_DATA(nlh);
+	*family = AF_SMC;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_SMC}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* print unknown family */
+	*family = 0xff;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=0xff /* AF_??? */}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* short read of family */
+	memmove(nlh0, nlh, NLMSG_HDRLEN);
+	nlh = nlh0;
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, %p}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       NLMSG_DATA(nlh),
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family),
+	       sprintrc(rc));
+
+	/* print family and string */
+	nlh = nlh0 - sizeof(*family) - 4;
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP,
+	};
+
+	family = NLMSG_DATA(nlh);
+	*family = AF_SMC;
+	memcpy(family + 1, "1234", 4);
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
+		    NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}"
+	       ", {family=AF_SMC, \"1234\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
+	       sprintrc(rc));
+
+	/* print smc_diag_msg */
+	nlh = nlh0 - sizeof(*msg);
+	*nlh = (struct nlmsghdr) {
+		.nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
+		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
+		.nlmsg_flags = NLM_F_DUMP,
+	};
+
+	msg = NLMSG_DATA(nlh);
+	*msg = (struct smc_diag_msg) {
+		.diag_family = AF_SMC,
+		.diag_state = SMC_ACTIVE,
+	};
+
+	if (!inet_pton(AF_INET, address, msg->id.idiag_src))
+		perror_msg_and_skip("sendto");
+	if (!inet_pton(AF_INET, address, msg->id.idiag_dst))
+		perror_msg_and_skip("sendto");
+
+	rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {diag_family=AF_SMC"
+	       ", diag_state=SMC_ACTIVE, diag_fallback=0, diag_shutdown=0"
+	       ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+	       ", inet_pton(AF_INET, \"%s\", idiag_src)"
+	       ", inet_pton(AF_INET, \"%s\", idiag_dst), idiag_if=0"
+	       ", idiag_cookie=[0, 0]}, diag_uid=0, diag_inode=0}}, %u"
+	       ", MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, nlh->nlmsg_len, address, address,
+	       nlh->nlmsg_len, sprintrc(rc));
+
+	/* short read of smc_diag_msg */
+	nlh = nlh0 - sizeof(*family);
+	memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*family));
+
+	rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
+		    NULL, 0);
+	printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_SMC, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
+	       NLMSG_DATA(nlh) + 1,
+	       NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
+	       sprintrc(rc));
+}
+#endif
+
 int main(void)
 {
 	skip_if_unavailable("/proc/self/fd/");
@@ -1203,6 +1465,10 @@ int main(void)
 	test_inet_diag_req(fd);
 	test_inet_diag_req_v2(fd);
 	test_inet_diag_msg(fd);
+#ifdef AF_SMC
+	test_smc_diag_req(fd);
+	test_smc_diag_msg(fd);
+#endif
 
 	printf("+++ exited with 0 +++\n");
 
-- 
2.7.4





More information about the Strace-devel mailing list