* tests/netlink_sock_diag.c: Include <linux/netlink_diag.h>. (test_netlink_diag_req, test_netlink_diag_msg): New functions. (main): Use them.
Co-authored-by: Fabien Siron <fabien.si...@epita.fr> --- tests/netlink_sock_diag.c | 242 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/tests/netlink_sock_diag.c b/tests/netlink_sock_diag.c index 63e7c3f..87a6d00 100644 --- a/tests/netlink_sock_diag.c +++ b/tests/netlink_sock_diag.c @@ -35,6 +35,7 @@ #include <sys/socket.h> #include <netinet/tcp.h> #include <linux/netlink.h> +#include <linux/netlink_diag.h> #include <linux/sock_diag.h> #include <linux/unix_diag.h> @@ -314,6 +315,245 @@ test_unix_diag_msg(const int fd) sprintrc(rc)); } +static void +test_netlink_diag_req(const int fd) +{ + struct nlmsghdr *nlh; + struct netlink_diag_req *req; + uint8_t *family; + void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + long rc; + + /* print family only */ + nlh = nlh0 - sizeof(*family); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*family), + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_REQUEST, + }; + family = NLMSG_DATA(nlh); + *family = AF_NETLINK; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_NETLINK}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* print unknown family */ + *family = 0xff; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=0xff /* AF_??? */}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* short read of family */ + memmove(nlh0, nlh, NLMSG_HDRLEN); + nlh = nlh0; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}, %p}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_DATA(nlh), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* print family and string */ + nlh = nlh0 - (sizeof(*family) + 4); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4, + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_REQUEST, + }; + family = NLMSG_DATA(nlh); + *family = AF_NETLINK; + memcpy(family + 1, "1234", 4); + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}" + ", {family=AF_NETLINK, \"1234\"}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4, + NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4, + sprintrc(rc)); + + /* print netlink_diag_req */ + nlh = nlh0 - sizeof(*req); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*req), + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_REQUEST, + }; + req = NLMSG_DATA(nlh); + *req = (struct netlink_diag_req) { + .sdiag_family = AF_NETLINK, + .sdiag_protocol = NDIAG_PROTO_ALL, + .ndiag_show = NDIAG_SHOW_MEMINFO, + }; + + + rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}" + ", {sdiag_family=AF_NETLINK, sdiag_protocol=NDIAG_PROTO_ALL" + ", ndiag_ino=0, ndiag_show=NDIAG_SHOW_MEMINFO" + ", ndiag_cookie=[0, 0]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc)); + + /* short read of netlink_diag_req */ + nlh = nlh0 - sizeof(*family); + memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*family)); + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_NETLINK, %p}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req), + NLMSG_DATA(nlh) + 1, + NLMSG_HDRLEN + (unsigned int) sizeof(*req), + sprintrc(rc)); +} + +static void +test_netlink_diag_msg(const int fd) +{ + struct nlmsghdr *nlh; + struct netlink_diag_msg *msg; + uint8_t *family; + void *const nlh0 = tail_alloc(NLMSG_HDRLEN); + long rc; + + /* print family only */ + nlh = nlh0 - sizeof(*family); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*family), + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_DUMP, + }; + family = NLMSG_DATA(nlh); + *family = AF_NETLINK; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_NETLINK}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* print unknown family */ + *family = 0xff; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=0xff /* AF_??? */}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* short read of family */ + memmove(nlh0, nlh, NLMSG_HDRLEN); + nlh = nlh0; + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}, %p}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family), + NLMSG_DATA(nlh), + NLMSG_HDRLEN + (unsigned int) sizeof(*family), + sprintrc(rc)); + + /* print family and string */ + nlh = nlh0 - (sizeof(*family) + 4); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4, + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_DUMP, + }; + family = NLMSG_DATA(nlh); + *family = AF_NETLINK; + memcpy(family + 1, "1234", 4); + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}" + ", {family=AF_NETLINK, \"1234\"}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4, + NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4, + sprintrc(rc)); + + /* print netlink_diag_msg */ + nlh = nlh0 - sizeof(*msg); + *nlh = (struct nlmsghdr) { + .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg), + .nlmsg_type = SOCK_DIAG_BY_FAMILY, + .nlmsg_flags = NLM_F_DUMP, + }; + msg = NLMSG_DATA(nlh); + *msg = (struct netlink_diag_msg) { + .ndiag_family = AF_NETLINK, + .ndiag_type = SOCK_RAW, + .ndiag_protocol = NETLINK_ROUTE, + .ndiag_state = NETLINK_CONNECTED, + }; + + rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}, {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]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc)); + + /* short read of netlink_diag_msg */ + nlh = nlh0 - sizeof(*family); + memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*family)); + + rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT, + NULL, 0); + + printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY" + ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_NETLINK, %p}}" + ", %u, MSG_DONTWAIT, NULL, 0) = %s\n", + fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg), + NLMSG_DATA(nlh) + 1, + NLMSG_HDRLEN + (unsigned int) sizeof(*msg), + sprintrc(rc)); +} + int main(void) { skip_if_unavailable("/proc/self/fd/"); @@ -324,6 +564,8 @@ int main(void) test_nlmsg_flags(fd); test_unix_diag_req(fd); test_unix_diag_msg(fd); + test_netlink_diag_req(fd); + test_netlink_diag_msg(fd); printf("+++ exited with 0 +++\n"); -- 2.7.4 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot _______________________________________________ Strace-devel mailing list Strace-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/strace-devel