[PATCH 08/10] tests: check decoding of NETLINK_SOCK_DIAG AF_SMC diag attributes

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


* tests/nlattr_smc_diag.c: New file.
* tests/gen_tests.in (nlattr_smc_diag): New entry.
* tests/pure_executables.list: Add nlattr_smc_diag.
* tests/.gitignore: Likewise.
---
 tests/.gitignore            |   1 +
 tests/gen_tests.in          |   1 +
 tests/nlattr_smc_diag.c     | 421 ++++++++++++++++++++++++++++++++++++++++++++
 tests/pure_executables.list |   1 +
 4 files changed, 424 insertions(+)
 create mode 100644 tests/nlattr_smc_diag.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 7093830..ba9a172 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -213,6 +213,7 @@ nlattr
 nlattr_inet_diag
 nlattr_netlink_diag
 nlattr_packet_diag
+nlattr_smc_diag
 nlattr_unix_diag
 nsyscalls
 old_mmap
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 4ac7d98..ab07b92 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -196,6 +196,7 @@ nlattr		+netlink_sock_diag.test
 nlattr_inet_diag	+netlink_sock_diag.test
 nlattr_netlink_diag	+netlink_sock_diag.test
 nlattr_packet_diag	+netlink_sock_diag.test
+nlattr_smc_diag		+netlink_sock_diag.test
 nlattr_unix_diag	+netlink_sock_diag.test
 old_mmap	-a11 -e trace=mmap
 oldfstat	-a18 -v -P stat.sample
diff --git a/tests/nlattr_smc_diag.c b/tests/nlattr_smc_diag.c
new file mode 100644
index 0000000..b8e2a93
--- /dev/null
+++ b/tests/nlattr_smc_diag.c
@@ -0,0 +1,421 @@
+/*
+ * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+#include <sys/socket.h>
+
+#ifdef AF_SMC
+
+# include <stdint.h>
+# include <stdio.h>
+# include <string.h>
+# include <arpa/inet.h>
+# include <linux/netlink.h>
+# include <linux/rtnetlink.h>
+# include <linux/smc_diag.h>
+# include <linux/sock_diag.h>
+
+# define SMC_CLNT 0
+# define SMC_ACTIVE 1
+
+# if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
+#  define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
+# endif
+
+static void
+test_smc_diag_conninfo(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct smc_diag_msg *msg;
+	struct nlattr *nla;
+	struct smc_diag_conninfo *cinfo;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*cinfo) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_CONNINFO
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_CONNINFO}, \"12\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address,
+	       nla->nla_len, msg_len, sprintrc(rc));
+
+	/* short read of smc_diag_conninfo */
+	nla_len = NLA_HDRLEN + sizeof(*cinfo);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_CONNINFO
+	};
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_CONNINFO}, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla->nla_len,
+	       RTA_DATA(nla), msg_len, sprintrc(rc));
+
+	/* smc_diag_conninfo */
+	nla_len = NLA_HDRLEN + sizeof(*cinfo);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_CONNINFO
+	};
+	cinfo = RTA_DATA(nla);
+	cinfo->token = 0;
+	*cinfo = (struct smc_diag_conninfo) {
+		.token = 0xabcdefac,
+		.sndbuf_size = 0xbcdaefad,
+		.rmbe_size = 0xcdbaefab,
+		.peer_rmbe_size = 0xdbcdedaf,
+		.rx_prod = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.rx_cons = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.tx_prod = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.tx_cons = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.rx_prod_flags = 0xff,
+		.rx_conn_state_flags = 0xff,
+		.tx_prod_flags = 0xff,
+		.tx_conn_state_flags = 0xff,
+		.tx_prep = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.tx_sent = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		},
+		.tx_fin = {
+			.reserved = 0xabcd,
+			.wrap = 0xbcad,
+			.count = 0xcdedbade
+		}
+	};
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_CONNINFO}, {token=%u"
+	       ", sndbuf_size=%u, rmbe_size=%u, peer_rmbe_size=%u"
+	       ", rx_prod={reserved=%u, wrap=%u, count=%u}"
+	       ", rx_cons={reserved=%u, wrap=%u, count=%u}"
+	       ", tx_prod={reserved=%u, wrap=%u, count=%u}"
+	       ", tx_cons={reserved=%u, wrap=%u, count=%u}"
+	       ", rx_prod_flags=0xff, rx_conn_state_flags=0xff"
+	       ", tx_prod_flags=0xff, tx_conn_state_flags=0xff"
+	       ", tx_prep={reserved=%u, wrap=%u, count=%u}"
+	       ", tx_sent={reserved=%u, wrap=%u, count=%u}"
+	       ", tx_fin={reserved=%u, wrap=%u, count=%u}}}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla->nla_len,
+	       0xabcdefac, 0xbcdaefad, 0xcdbaefab, 0xdbcdedaf,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       0xabcd, 0xbcad, 0xcdedbade,
+	       msg_len, sprintrc(rc));
+}
+
+static void
+test_smc_diag_lgrinfo(const int fd)
+{
+	const char address[] = "12.34.56.78";
+	struct nlmsghdr *nlh;
+	struct smc_diag_msg *msg;
+	struct nlattr *nla;
+	struct smc_diag_lgrinfo *linfo;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*linfo) */
+	nla_len = NLA_HDRLEN + 2;
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_LGRINFO
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_LGRINFO}"
+	       ", \"12\"}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address,
+	       nla->nla_len, msg_len, sprintrc(rc));
+
+	/* short read of smc_diag_lgrinfo */
+	nla_len = NLA_HDRLEN + sizeof(*linfo);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - (nla_len - 1);
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_LGRINFO
+	};
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_LGRINFO}"
+	       ", %p}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla->nla_len,
+	       RTA_DATA(nla), msg_len, sprintrc(rc));
+
+	/* smc_diag_lgrinfo */
+	nla_len = NLA_HDRLEN + sizeof(*linfo);
+	msg_len = NLMSG_SPACE(sizeof(*msg)) + nla_len;
+	nlh = nlh0 - nla_len;
+	SET_STRUCT(struct nlmsghdr, nlh,
+		.nlmsg_len = msg_len,
+		.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,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = SMC_DIAG_LGRINFO
+	};
+	linfo = RTA_DATA(nla);
+	*linfo = (struct smc_diag_lgrinfo) {
+		.lnk[0] = {
+			.link_id = 0xaf,
+			.ibport = 0xfa,
+		},
+		.role = SMC_CLNT
+	};
+	memcpy(linfo->lnk[0].ibname, "123", 4);
+	memcpy(linfo->lnk[0].gid, "123", 4);
+	memcpy(linfo->lnk[0].peer_gid, "123", 4);
+
+	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, msg_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}"
+	       ", {{nla_len=%u, nla_type=SMC_DIAG_LGRINFO}"
+	       ", {lnk[0]={link_id=%u, ibname=\"123\", ibport=%u"
+	       ", gid=\"123\", peer_gid=\"123\"}, role=SMC_CLNT}}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, address, address, nla->nla_len,
+	       0xaf, 0xfa, msg_len, sprintrc(rc));
+}
+
+int main(void)
+{
+	skip_if_unavailable("/proc/self/fd/");
+
+	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+
+	test_smc_diag_conninfo(fd);
+	test_smc_diag_lgrinfo(fd);
+
+	printf("+++ exited with 0 +++\n");
+
+	return 0;
+}
+#else
+
+SKIP_MAIN_UNDEFINED("AF_SMC")
+
+#endif
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index ce3ac72..40c00c0 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -178,6 +178,7 @@ nlattr
 nlattr_inet_diag
 nlattr_netlink_diag
 nlattr_packet_diag
+nlattr_smc_diag
 nlattr_unix_diag
 old_mmap
 oldfstat
-- 
2.7.4





More information about the Strace-devel mailing list