[PATCH 10/10] tests: check decoding of NETLINK_SOCK_DIAG AF_INET request diag attributes
JingPiao Chen
chenjingpiao at gmail.com
Wed Jun 21 02:42:23 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 | 1496 ++++++++++++++++++++++++++++++++++++++++++
tests/pure_executables.list | 1 +
4 files changed, 1499 insertions(+)
create mode 100644 tests/nlattr_inet_diag_req.c
diff --git a/tests/.gitignore b/tests/.gitignore
index ba9a172..42e202d 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -211,6 +211,7 @@ netlink_xfrm
newfstatat
nlattr
nlattr_inet_diag
+nlattr_inet_diag_req
nlattr_netlink_diag
nlattr_packet_diag
nlattr_smc_diag
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index ab07b92..981b0d3 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_inet_diag_req +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
diff --git a/tests/nlattr_inet_diag_req.c b/tests/nlattr_inet_diag_req.c
new file mode 100644
index 0000000..ff66f71
--- /dev/null
+++ b/tests/nlattr_inet_diag_req.c
@@ -0,0 +1,1496 @@
+/*
+ * 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 <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <linux/inet_diag.h>
+#include <linux/netlink.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_inet_diag_req_none_compat(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req *req;
+ struct nlattr *nla;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ nla_len = NLA_HDRLEN + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + nla_len;
+ nlh = nlh0 - nla_len;
+ SET_STRUCT(struct nlmsghdr, nlh,
+ .nlmsg_len = msg_len,
+ .nlmsg_type = TCPDIAG_GETSOCK,
+ .nlmsg_flags = NLM_F_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req) {
+ .idiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_TOS - 1),
+ .idiag_states = 1 << TCP_LAST_ACK,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE + 1
+ };
+ memcpy(RTA_DATA(nla), "12", 2);
+
+ 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, 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_??? */}, \"12\"}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla->nla_len,
+ INET_DIAG_REQ_BYTECODE + 1, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_req_none(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ nla_len = NLA_HDRLEN + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE + 1
+ };
+ memcpy(RTA_DATA(nla), "12", 2);
+
+ 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, 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_??? */}, \"12\"}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla->nla_len,
+ INET_DIAG_REQ_BYTECODE + 1, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_op(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*op) */
+ nla_len = NLA_HDRLEN + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ memcpy(RTA_DATA(nla), "12", 2);
+
+ 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, 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->nla_len,
+ msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(*op);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+
+ 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, 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->nla_len, RTA_DATA(nla),
+ msg_len, sprintrc(rc));
+
+ /* inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(*op);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ .yes = 0xaf,
+ .no = 0xafcd
+ };
+
+ 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, 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->nla_len, 0xaf, 0xafcd,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_s_cond(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_hostcond *cond;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*cond) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ memcpy(cond, "12", 2);
+
+ 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, 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->nla_len,
+ msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_hostcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 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, 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->nla_len, cond,
+ msg_len, sprintrc(rc));
+
+ /* inet_diag_hostcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET,
+ .prefix_len = 0xad,
+ .port = 0xadfa,
+ };
+
+ 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, 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=%u, port=%u}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla->nla_len, 0xad, 0xadfa,
+ msg_len, sprintrc(rc));
+
+ /* inet_diag_hostcond and some data */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + 4;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_UNSPEC,
+ .prefix_len = 0xad,
+ .port = 0xadfa,
+ };
+ memcpy(cond->addr, "1234", 4);
+
+ 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, 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->nla_len, 0xad, 0xadfa,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_in_addr(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_hostcond *cond;
+ struct in_addr *in_addr;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*in_addr) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET,
+ };
+ in_addr = (struct in_addr *) cond->addr;
+
+ memcpy(in_addr, "12", 2);
+
+ 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, 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->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(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET,
+ };
+ in_addr = (struct in_addr *) cond->addr;
+
+ 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, 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->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(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET,
+ };
+ in_addr = (struct in_addr *) cond->addr;
+
+ if (!inet_pton(AF_INET, address, &in_addr->s_addr))
+ perror_msg_and_skip("sendto");
+
+ 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, 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->nla_len, address,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_in6_addr(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ const char address6[] = "12:34:56:78:90:ab:cd:ef";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_hostcond *cond;
+ struct in6_addr *in6_addr;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*in6_addr) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*cond) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET6,
+ };
+ in6_addr = (struct in6_addr *) cond->addr;
+
+ memcpy(in6_addr, "12", 2);
+
+ 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, 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->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(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET6,
+ };
+ in6_addr = (struct in6_addr *) cond->addr;
+
+ 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, 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->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(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ cond = (struct inet_diag_hostcond *)(op + 1);
+ *cond = (struct inet_diag_hostcond) {
+ .family = AF_INET6,
+ };
+ in6_addr = (struct in6_addr *) cond->addr;
+
+ if (!inet_pton(AF_INET6, address6, in6_addr->s6_addr))
+ perror_msg_and_skip("sendto");
+
+ 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, 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->nla_len, address6,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_dev_cond(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ uint32_t *ifindex;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*ifindex) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_DEV_COND,
+ };
+ ifindex = (uint32_t *)(op + 1);
+ memcpy(ifindex, "12", 2);
+
+ 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, 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->nla_len,
+ msg_len, sprintrc(rc));
+
+ /* short read of ifindex */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*ifindex);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_DEV_COND,
+ };
+ ifindex = (uint32_t *)(op + 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, 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->nla_len, ifindex,
+ msg_len, sprintrc(rc));
+
+ /* ifindex */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*ifindex);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_DEV_COND,
+ };
+ ifindex = (uint32_t *)(op + 1);
+ *ifindex = 0xabafefaf;
+
+ 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, 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->nla_len, 0xabafefaf,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_s_le(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*op) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_LE,
+ };
+ op = op + 1;
+ memcpy(op, "12", 2);
+
+ 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, 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->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(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_LE,
+ };
+
+ 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, 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->nla_len, op + 1,
+ msg_len, sprintrc(rc));
+
+ /* inet_diag_bc_op */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*op);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_S_LE,
+ };
+ op = op + 1;
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_DEV_COND,
+ };
+
+ 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, 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=0, no=0}}}}"
+ ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+ fd, msg_len, address, address, nla->nla_len,
+ msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_mark_cond(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ struct inet_diag_markcond *markcond;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ /* len < sizeof(*markcond) */
+ nla_len = NLA_HDRLEN + sizeof(*op) + 2;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_MARK_COND,
+ };
+ markcond = (struct inet_diag_markcond *)(op + 1);
+ memcpy(markcond, "12", 2);
+
+ 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, 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->nla_len,
+ msg_len, sprintrc(rc));
+
+ /* short read of inet_diag_markcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*markcond);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_MARK_COND,
+ };
+ markcond = (struct inet_diag_markcond *)(op + 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, 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->nla_len, markcond,
+ msg_len, sprintrc(rc));
+
+ /* inet_diag_markcond */
+ nla_len = NLA_HDRLEN + sizeof(*op) + sizeof(*markcond);
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_MARK_COND,
+ };
+ markcond = (struct inet_diag_markcond *)(op + 1);
+ *markcond = (struct inet_diag_markcond) {
+ .mark = 0xafbcafcd,
+ .mask = 0xbafaacda,
+ };
+
+ 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, 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->nla_len, 0xafbcafcd,
+ 0xbafaacda, msg_len, sprintrc(rc));
+}
+
+static void
+test_inet_diag_bc_nop(const int fd)
+{
+ const char address[] = "12.34.56.78";
+ struct nlmsghdr *nlh;
+ struct inet_diag_req_v2 *req;
+ struct nlattr *nla;
+ struct inet_diag_bc_op *op;
+ int nla_len;
+ unsigned int msg_len;
+ void *const nlh0 = tail_alloc(NLMSG_SPACE(sizeof(*req)));
+ long rc;
+
+ nla_len = NLA_HDRLEN + sizeof(*op) + 4;
+ msg_len = NLMSG_SPACE(sizeof(*req)) + 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_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ *req = (struct inet_diag_req_v2) {
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE,
+ };
+
+ nla = (void *) nlh + NLMSG_SPACE(sizeof(*req));
+ *nla = (struct nlattr) {
+ .nla_len = nla_len,
+ .nla_type = INET_DIAG_REQ_BYTECODE
+ };
+ op = RTA_DATA(nla);
+ *op = (struct inet_diag_bc_op) {
+ .code = INET_DIAG_BC_AUTO,
+ };
+ memcpy(op + 1, "1234", 4);
+
+ 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, 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->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 40c00c0..552a677 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -176,6 +176,7 @@ netlink_xfrm
newfstatat
nlattr
nlattr_inet_diag
+nlattr_inet_diag_req
nlattr_netlink_diag
nlattr_packet_diag
nlattr_smc_diag
--
2.7.4
More information about the Strace-devel
mailing list