[PATCH v2 18/18] tests: check decoding of inet_diag_req attributes
JingPiao Chen
chenjingpiao at gmail.com
Tue Jun 27 10:10:20 UTC 2017
* tests/nlattr_inet_diag_req.c: New file.
* tests/gen_tests.in (nlattr_inet_diag_req): New entry.
* tests/pure_executables.list: Add nlattr_inet_diag_req.
* tests/.gitignore: Likewise.
---
tests/.gitignore | 1 +
tests/gen_tests.in | 1 +
tests/nlattr_inet_diag_req.c | 1098 ++++++++++++++++++++++++++++++++++++++++++
tests/pure_executables.list | 1 +
4 files changed, 1101 insertions(+)
create mode 100644 tests/nlattr_inet_diag_req.c
diff --git a/tests/.gitignore b/tests/.gitignore
index 149f7c2..82896e9 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -211,6 +211,7 @@ netlink_xfrm
newfstatat
nlattr
nlattr_inet_diag_msg
+nlattr_inet_diag_req
nlattr_netlink_diag_msg
nlattr_packet_diag_msg
nlattr_smc_diag_msg
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 347dca0..40e84d5 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_msg +netlink_sock_diag.test
+nlattr_inet_diag_req +netlink_sock_diag.test
nlattr_netlink_diag_msg +netlink_sock_diag.test
nlattr_packet_diag_msg +netlink_sock_diag.test
nlattr_smc_diag_msg +netlink_sock_diag.test
diff --git a/tests/nlattr_inet_diag_req.c b/tests/nlattr_inet_diag_req.c
new file mode 100644
index 0000000..e1f8c0b
--- /dev/null
+++ b/tests/nlattr_inet_diag_req.c
@@ -0,0 +1,1098 @@
+/*
+ * 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 "netlink.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <linux/inet_diag.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+
+static void
+init_inet_diag_req(struct nlmsghdr *nlh, unsigned int msg_len,
+ const char *address)
+{
+ struct inet_diag_req *req;
+
+ SET_STRUCT(struct nlmsghdr, nlh,
+ .nlmsg_len = msg_len,
+ .nlmsg_type = TCPDIAG_GETSOCK,
+ .nlmsg_flags = NLM_F_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ SET_STRUCT(struct inet_diag_req, req,
+ .idiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_TOS - 1),
+ .idiag_states = 1 << TCP_LAST_ACK
+ );
+
+ if (!inet_pton(AF_INET, address, req->id.idiag_src))
+ perror_msg_and_skip("inet_pton");
+ if (!inet_pton(AF_INET, address, req->id.idiag_dst))
+ perror_msg_and_skip("inet_pton");
+}
+
+static void
+test_inet_diag_req_none_compat(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ nla_len = NLA_HDRLEN + 4;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE + 1
+ );
+ memcpy(RTA_DATA(nla), "1234", 4);
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}, {idiag_family=AF_INET"
+ ", idiag_src_len=0, idiag_dst_len=0"
+ ", idiag_ext=1<<(INET_DIAG_TOS-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]}, idiag_states=1<<TCP_LAST_ACK"
+ ", idiag_dbs=0}, {{nla_len=%u"
+ ", nla_type=%#x /* INET_DIAG_REQ_??? */}, \"1234\"}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ INET_DIAG_REQ_BYTECODE + 1, msg_len, sprintrc(rc));
+}
+
+static void
+init_inet_diag_req_v2(struct nlmsghdr *nlh, unsigned int msg_len,
+ const char *address)
+{
+ struct inet_diag_req_v2 *req;
+
+ SET_STRUCT(struct nlmsghdr, nlh,
+ .nlmsg_len = msg_len,
+ .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+ .nlmsg_flags = NLM_F_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ SET_STRUCT(struct inet_diag_req_v2, req,
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE
+ );
+
+ if (!inet_pton(AF_INET, address, req->id.idiag_src))
+ perror_msg_and_skip("inet_pton");
+ if (!inet_pton(AF_INET, address, req->id.idiag_dst))
+ perror_msg_and_skip("inet_pton");
+}
+
+static void
+test_inet_diag_req_none(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ nla_len = NLA_HDRLEN + 4;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE + 1
+ );
+ memcpy(RTA_DATA(nla), "1234", 4);
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=%#x /* INET_DIAG_REQ_??? */}, \"1234\"}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ INET_DIAG_REQ_BYTECODE + 1, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_op(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct inet_diag_bc_op) */
+ nla_len = NLA_HDRLEN + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ 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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, \"12\"}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(struct inet_diag_bc_op);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, %p}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ RTA_DATA(nla), msg_len, sprintrc(rc));
+
+ /* inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(struct inet_diag_bc_op);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_S_COND,
+ .yes = 0xaf,
+ .no = 0xafcd
+ };
+ memcpy(RTA_DATA(nla), &op, sizeof(op));
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=%u, no=%u}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, op.yes,
+ op.no, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_s_cond(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct inet_diag_hostcond) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ memcpy(op + 1, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_S_COND, yes=0, no=0}, \"12\"}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_hostcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(struct inet_diag_hostcond);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_S_COND, yes=0, no=0}, %p}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ op + 1, msg_len, sprintrc(rc));
+
+ /* inet_diag_hostcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(struct inet_diag_hostcond);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ static const struct inet_diag_hostcond cond = {
+ .family = AF_UNSPEC,
+ .prefix_len = 0xad,
+ .port = 0xadfa,
+ };
+ memcpy(op + 1, &cond, sizeof(cond));
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_UNSPEC, prefix_len=%u, port=%u}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ cond.prefix_len, cond.port, msg_len, sprintrc(rc));
+
+ /* inet_diag_hostcond and some data */
+ nla_len += 4;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ memcpy(op + 1, &cond, sizeof(cond));
+ memcpy(((struct inet_diag_hostcond *)(op + 1))->addr, "1234", 4);
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_UNSPEC, prefix_len=%u, port=%u"
+ ", addr=\"1234\"}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ cond.prefix_len, cond.port, msg_len, sprintrc(rc));
+}
+
+static void
+test_in_addr(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_hostcond *cond;
+ struct in_addr *in_addr;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct in_addr) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET
+ );
+ in_addr = (struct in_addr *) cond->addr;
+ memcpy(in_addr, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET, prefix_len=0, port=0"
+ ", \"12\"}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ msg_len, sprintrc(rc));
+
+ /* short read of in_addr */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + sizeof(*in_addr);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET
+ );
+ in_addr = (struct in_addr *) cond->addr;
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET, prefix_len=0, port=0"
+ ", %p}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, in_addr,
+ msg_len, sprintrc(rc));
+
+ /* in_addr */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + sizeof(*in_addr);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET
+ );
+ in_addr = (struct in_addr *) cond->addr;
+
+ if (!inet_pton(AF_INET, address, &in_addr->s_addr))
+ perror_msg_and_skip("inet_pton");
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET, prefix_len=0, port=0"
+ ", inet_pton(AF_INET, \"%s\", &addr.s_addr)}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ address, msg_len, sprintrc(rc));
+}
+
+static void
+test_in6_addr(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ const char address6[] = "12:34:56:78:90:ab:cd:ef";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_hostcond *cond;
+ struct in6_addr *in6_addr;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct in6_addr) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET6
+ );
+ in6_addr = (struct in6_addr *) cond->addr;
+ memcpy(in6_addr, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET6, prefix_len=0, port=0"
+ ", \"12\"}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of in6_addr */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + sizeof(*in6_addr);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET6
+ );
+ in6_addr = (struct in6_addr *) cond->addr;
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET6, prefix_len=0, port=0"
+ ", %p}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ in6_addr, msg_len, sprintrc(rc));
+
+ /* in6_addr */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + sizeof(*in6_addr);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_COND
+ );
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ SET_STRUCT(struct inet_diag_hostcond, cond,
+ .family = AF_INET6
+ );
+ in6_addr = (struct in6_addr *) cond->addr;
+
+ if (!inet_pton(AF_INET6, address6, in6_addr->s6_addr))
+ perror_msg_and_skip("inet_pton");
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}, {{code=INET_DIAG_BC_S_COND"
+ ", yes=0, no=0}, {family=AF_INET6, prefix_len=0, port=0"
+ ", inet_pton(AF_INET6, \"%s\", &addr.s6_addr)}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ address6, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_dev_cond(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ uint32_t *ifindex;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(uint32_t) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_DEV_COND
+ );
+ memcpy(op + 1, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_DEV_COND, yes=0, no=0}"
+ ", \"12\"}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of ifindex */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*ifindex);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_DEV_COND
+ );
+ ifindex = (uint32_t *)(op + 1);
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_DEV_COND, yes=0, no=0}"
+ ", %p}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ op + 1, msg_len, sprintrc(rc));
+
+ /* ifindex */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*ifindex);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_DEV_COND
+ );
+ ifindex = (uint32_t *)(op + 1);
+ *ifindex = 0xabafefaf;
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_DEV_COND, yes=0, no=0}"
+ ", %u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ *ifindex, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_s_le(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct inet_diag_bc_op) */
+ nla_len = NLA_HDRLEN + sizeof(struct inet_diag_bc_op) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_LE
+ );
+ memcpy(op + 1, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_S_LE, yes=0, no=0}"
+ ", \"12\"}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*op);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_LE
+ );
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_S_LE, yes=0, no=0}"
+ ", %p}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ op + 1, msg_len, sprintrc(rc));
+
+ /* inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*op);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_S_LE
+ );
+ static const struct inet_diag_bc_op data = {
+ .code = INET_DIAG_BC_DEV_COND,
+ .yes = 0xaf,
+ .no = 0xafcd
+ };
+ memcpy(op + 1, &data, sizeof(data));
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_S_LE, yes=0, no=0}"
+ ", {code=INET_DIAG_BC_DEV_COND, yes=%u, no=%u}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ data.yes, data.no, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_mark_cond(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ /* len < sizeof(struct inet_diag_markcond) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_MARK_COND,
+ );
+ memcpy(op + 1, "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_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}"
+ ", {{nla_len=%u, nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_MARK_COND, yes=0, no=0}"
+ ", \"12\"}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_markcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(struct inet_diag_markcond);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - (nla_len - 1);
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_MARK_COND
+ );
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}"
+ ", {{nla_len=%u, nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_MARK_COND, yes=0, no=0}"
+ ", %p}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ op + 1, msg_len, sprintrc(rc));
+
+ /* inet_diag_markcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(struct inet_diag_markcond);
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_MARK_COND
+ );
+ static const struct inet_diag_markcond markcond = {
+ .mark = 0xafbcafcd,
+ .mask = 0xbafaacda,
+ };
+ memcpy(op + 1, &markcond, sizeof(markcond));
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}"
+ ", {{nla_len=%u, nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_MARK_COND, yes=0, no=0}"
+ ", {mark=%u, mask=%u}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len,
+ markcond.mark, markcond.mask, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_nop(const int fd)
+{
+ const int hdrlen = sizeof(struct inet_diag_req_v2);
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ unsigned int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ long rc;
+
+ nla_len = NLA_HDRLEN + sizeof(*op) + 4;
+ msg_len = NLMSG_SPACE(hdrlen) + nla_len;
+ nlh = nlh0 - nla_len;
+ init_inet_diag_req_v2(nlh, msg_len, address);
+
+ nla = NLMSG_ATTR(nlh, hdrlen);
+ SET_STRUCT(struct nlattr, nla,
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ );
+ op = RTA_DATA(nla);
+ SET_STRUCT(struct inet_diag_bc_op, op,
+ .code = INET_DIAG_BC_AUTO,
+ );
+ memcpy(op + 1, "1234", 4);
+
+ rc = sendto(fd, nlh, msg_len, MSG_DONTWAIT, NULL, 0);
+
+ printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1), idiag_states=1<<TCP_CLOSE"
+ ", 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]}}, {{nla_len=%u"
+ ", nla_type=INET_DIAG_REQ_BYTECODE}"
+ ", {{code=INET_DIAG_BC_AUTO, yes=0, no=0}"
+ ", \"1234\"}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla_len, msg_len, sprintrc(rc));
+}
+
+int main(void)
+{
+ skip_if_unavailable("/proc/self/fd/");
+
+ int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+
+ test_inet_diag_req_none_compat(fd);
+ test_inet_diag_req_none(fd);
+
+ test_inet_diag_bc_op(fd);
+ test_inet_diag_bc_s_cond(fd);
+ test_in_addr(fd);
+ test_in6_addr(fd);
+ test_inet_diag_bc_dev_cond(fd);
+ test_inet_diag_bc_s_le(fd);
+ test_inet_diag_bc_mark_cond(fd);
+ test_inet_diag_bc_nop(fd);
+
+ printf("+++ exited with 0 +++\n");
+
+ return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 6a279bd..01ca65b 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -176,6 +176,7 @@ netlink_xfrm
newfstatat
nlattr
nlattr_inet_diag_msg
+nlattr_inet_diag_req
nlattr_netlink_diag_msg
nlattr_packet_diag_msg
nlattr_smc_diag_msg
--
2.7.4
More information about the Strace-devel
mailing list