[PATCH 02/10] tests: check decoding of NETLINK_SOCK_DIAG AF_NETLINK diag attributes

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


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

diff --git a/tests/.gitignore b/tests/.gitignore
index af7cad7..04d75cd 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -211,6 +211,7 @@ netlink_xfrm
 newfstatat
 nlattr
 nlattr_inet_diag
+nlattr_netlink_diag
 nsyscalls
 old_mmap
 oldfstat
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index ad7d0b8..b5cad20 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -194,6 +194,7 @@ netlink_xfrm	+netlink_sock_diag.test
 newfstatat	-a32 -v -P stat.sample -P /dev/full
 nlattr		+netlink_sock_diag.test
 nlattr_inet_diag	+netlink_sock_diag.test
+nlattr_netlink_diag	+netlink_sock_diag.test
 old_mmap	-a11 -e trace=mmap
 oldfstat	-a18 -v -P stat.sample
 oldlstat	-a32 -v -P stat.sample -P /dev/full
diff --git a/tests/nlattr_netlink_diag.c b/tests/nlattr_netlink_diag.c
new file mode 100644
index 0000000..0493dc1
--- /dev/null
+++ b/tests/nlattr_netlink_diag.c
@@ -0,0 +1,239 @@
+/*
+ * 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 <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+#include <linux/netlink_diag.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+
+#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
+# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
+#endif
+
+static void
+test_netlink_diag_groups(const int fd)
+{
+	struct nlmsghdr *nlh;
+	struct netlink_diag_msg *msg;
+	struct nlattr *nla;
+	unsigned long *groups;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	nla_len = NLA_HDRLEN + sizeof(*groups) * 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 netlink_diag_msg) {
+		.ndiag_family = AF_NETLINK,
+		.ndiag_type = SOCK_RAW,
+		.ndiag_protocol = NETLINK_ROUTE,
+		.ndiag_state = NETLINK_CONNECTED,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = NETLINK_DIAG_GROUPS
+	};
+	groups = RTA_DATA(nla);
+	groups[0] = (unsigned long) 0xdeadbeefbadc0dedULL;
+	groups[1] = (unsigned long) 0xdeadbeefbadc0dedULL;
+
+	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}, {ndiag_family=AF_NETLINK"
+	       ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
+	       ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=0"
+	       ", ndiag_dst_portid=0, ndiag_dst_group=0, ndiag_ino=0"
+	       ", ndiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=NETLINK_DIAG_GROUPS}, [%#lx, %#lx]}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, groups[0], groups[1],
+	       msg_len, sprintrc(rc));
+}
+
+static void
+test_netlink_diag_rx_ring(const int fd)
+{
+	struct nlmsghdr *nlh;
+	struct netlink_diag_msg *msg;
+	struct nlattr *nla;
+	struct netlink_diag_ring *ndr;
+	int nla_len;
+	unsigned int msg_len;
+	void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*msg)));
+	long rc;
+
+	/* len < sizeof(*ndr) */
+	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 netlink_diag_msg) {
+		.ndiag_family = AF_NETLINK,
+		.ndiag_type = SOCK_RAW,
+		.ndiag_protocol = NETLINK_ROUTE,
+		.ndiag_state = NETLINK_CONNECTED,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = NETLINK_DIAG_RX_RING
+	};
+	memcpy(RTA_DATA(nla), "12", 2);
+
+	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}, {ndiag_family=AF_NETLINK"
+	       ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
+	       ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=0"
+	       ", ndiag_dst_portid=0, ndiag_dst_group=0, ndiag_ino=0"
+	       ", ndiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=NETLINK_DIAG_RX_RING}, \"12\"}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, msg_len, sprintrc(rc));
+
+	/* short read of netlink_diag_ring */
+	nla_len = NLA_HDRLEN + sizeof(*ndr);
+	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 netlink_diag_msg) {
+		.ndiag_family = AF_NETLINK,
+		.ndiag_type = SOCK_RAW,
+		.ndiag_protocol = NETLINK_ROUTE,
+		.ndiag_state = NETLINK_CONNECTED,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = NETLINK_DIAG_RX_RING
+	};
+
+	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}, {ndiag_family=AF_NETLINK"
+	       ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
+	       ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=0"
+	       ", ndiag_dst_portid=0, ndiag_dst_group=0, ndiag_ino=0"
+	       ", ndiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=NETLINK_DIAG_RX_RING}, %p}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, RTA_DATA(nla),
+	       msg_len, sprintrc(rc));
+
+	/* netlink_diag_ring */
+	nla_len = NLA_HDRLEN + sizeof(*ndr);
+	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 netlink_diag_msg) {
+		.ndiag_family = AF_NETLINK,
+		.ndiag_type = SOCK_RAW,
+		.ndiag_protocol = NETLINK_ROUTE,
+		.ndiag_state = NETLINK_CONNECTED,
+	};
+
+	nla = (void *) nlh + NLMSG_SPACE(sizeof(*msg));
+	*nla = (struct nlattr) {
+		.nla_len = nla_len,
+		.nla_type = NETLINK_DIAG_RX_RING
+	};
+	ndr = RTA_DATA(nla);
+	*ndr = (struct netlink_diag_ring) {
+		.ndr_block_size = 0xfabfabdc,
+		.ndr_block_nr = 0xabcdabda,
+		.ndr_frame_size = 0xcbadbafa,
+		.ndr_frame_nr = 0xdbcafadb
+	};
+
+	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}, {ndiag_family=AF_NETLINK"
+	       ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
+	       ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=0"
+	       ", ndiag_dst_portid=0, ndiag_dst_group=0, ndiag_ino=0"
+	       ", ndiag_cookie=[0, 0]}, {{nla_len=%u"
+	       ", nla_type=NETLINK_DIAG_RX_RING}, {ndr_block_size=%u"
+	       ", ndr_block_nr=%u, ndr_frame_size=%u, ndr_frame_nr=%u}}}"
+	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       fd, msg_len, nla->nla_len, 0xfabfabdc, 0xabcdabda,
+	       0xcbadbafa, 0xdbcafadb, msg_len, sprintrc(rc));
+}
+
+int main(void)
+{
+	skip_if_unavailable("/proc/self/fd/");
+
+	int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+
+	test_netlink_diag_groups(fd);
+	test_netlink_diag_rx_ring(fd);
+
+	printf("+++ exited with 0 +++\n");
+
+	return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 9bbbac7..92c8feb 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -176,6 +176,7 @@ netlink_xfrm
 newfstatat
 nlattr
 nlattr_inet_diag
+nlattr_netlink_diag
 old_mmap
 oldfstat
 oldlstat
-- 
2.7.4





More information about the Strace-devel mailing list