]> granicus.if.org Git - strace/blobdiff - tests/netlink_protocol.c
tests: extend TEST_NETLINK_OBJECT macro
[strace] / tests / netlink_protocol.c
index 03cff929e541bb046abbef16f3279c96d7b61a01..be1a6528a95a335e302d93b640bbcd56815ed0c8 100644 (file)
 # include <unistd.h>
 # include <sys/xattr.h>
 # include <netinet/in.h>
-# include <linux/netlink.h>
+# include "netlink.h"
 # include <linux/sock_diag.h>
 # include <linux/netlink_diag.h>
 
-# if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
-#  define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
-# endif
-
 static void
 send_query(const int fd)
 {
@@ -87,21 +83,23 @@ send_query(const int fd)
 
        /* whole message length < sizeof(struct nlmsghdr) */
        rc = sendto(fd, req->magic, sizeof(req->magic), MSG_DONTWAIT, NULL, 0);
-       printf("sendto(%d, \"abcd\", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+       printf("sendto(%d, \"\\x61\\x62\\x63\\x64\""
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, (unsigned) sizeof(req->magic), sprintrc(rc));
 
        /* a single message with some data */
        rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, req->nlh.nlmsg_len, NLM_F_DUMP,
               (unsigned) sizeof(*req), sprintrc(rc));
 
        /* a single message without data */
        req->nlh.nlmsg_len = sizeof(req->nlh);
        rc = sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0);
-       printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+       printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+              ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, req->nlh.nlmsg_len, NLM_F_DUMP,
               (unsigned) sizeof(req->nlh), sprintrc(rc));
 
@@ -109,15 +107,16 @@ send_query(const int fd)
        req->nlh.nlmsg_len = sizeof(*req) + 8;
        rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, req->nlh.nlmsg_len, NLM_F_DUMP,
               (unsigned) sizeof(*req), sprintrc(rc));
 
        /* nlmsg_len < sizeof(struct nlmsghdr) */
        req->nlh.nlmsg_len = 8;
        rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
-       printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+       printf("sendto(%d, {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+              ", seq=0, pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, req->nlh.nlmsg_len, NLM_F_DUMP,
               (unsigned) sizeof(*req), sprintrc(rc));
 
@@ -132,8 +131,9 @@ send_query(const int fd)
 
        rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
-              ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}, \"abcd\"}]"
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
+              ", {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}]"
               ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
               reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
@@ -143,8 +143,8 @@ send_query(const int fd)
        void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
        rc = sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, %p], %u, MSG_DONTWAIT, NULL, 0)"
-              " = %s\n",
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
+              ", %p], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
               &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs),
               sprintrc(rc));
@@ -154,26 +154,25 @@ send_query(const int fd)
                    NULL, 0);
        errstr = sprintrc(rc);
        printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, \"",
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}, ",
               fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
-       print_quoted_memory((void *) &reqs->req2.nlh,
-                           sizeof(reqs->req2) - sizeof(req->nlh));
-       printf("\"], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+       print_quoted_hex(&reqs->req2.nlh,
+                        sizeof(reqs->req2) - sizeof(req->nlh));
+       printf("], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               (unsigned) (sizeof(*reqs) - sizeof(req->nlh)), errstr);
 
        /* second nlmsg_len < sizeof(struct nlmsghdr) */
        reqs->req2.nlh.nlmsg_len = 4;
        rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
-              ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}}], %u"
-              ", MSG_DONTWAIT, NULL, 0) = %s\n",
+              ", seq=0, pid=0}, \"\\x61\\x62\\x63\\x64\"}"
+              ", {len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+              ", seq=0, pid=0}], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
               reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
               (unsigned) sizeof(*reqs), sprintrc(rc));
 
        /* abbreviated output */
-# define DEFAULT_STRLEN 32
 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
        const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
        struct nlmsghdr *const msgs = tail_alloc(msg_len);
@@ -192,8 +191,8 @@ send_query(const int fd)
        for (i = 0; i < DEFAULT_STRLEN; ++i) {
                if (i)
                        printf(", ");
-               printf("{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-                      ", seq=%u, pid=0}}",
+               printf("{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+                      ", seq=%u, pid=0}",
                       msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
        }
        printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
@@ -221,18 +220,16 @@ test_nlmsgerr(const int fd)
               fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
               nlh->nlmsg_len, sprintrc(rc));
 
-       nlh = nlh0 - 2;
        nlh->nlmsg_len = NLMSG_HDRLEN + 2;
-       nlh->nlmsg_type = NLMSG_ERROR;
-       nlh->nlmsg_flags = NLM_F_REQUEST;
-       nlh->nlmsg_seq = 0;
-       nlh->nlmsg_pid = 0;
+       nlh = nlh0 - 2;
+       memmove(nlh, nlh0, sizeof(*nlh));
        memcpy(NLMSG_DATA(nlh), "42", 2);
 
-       rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
-              ", seq=0, pid=0}, \"42\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
-              fd, nlh->nlmsg_len, nlh->nlmsg_len, sprintrc(rc));
+              ", seq=0, pid=0}, \"\\x34\\x32\"}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
 
        /* error message with room for the error code only */
        nlh = nlh0 - sizeof(err->error);
@@ -284,8 +281,8 @@ test_nlmsgerr(const int fd)
        rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
        printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
               ", seq=0, pid=0}, {error=-EACCES"
-              ", msg={{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=%u, pid=%u}}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              ", msg={len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
+              ", seq=%u, pid=%u}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
               err->msg.nlmsg_seq, err->msg.nlmsg_pid,
               nlh->nlmsg_len, sprintrc(rc));
@@ -310,31 +307,104 @@ test_nlmsgerr(const int fd)
        printf("sendto(%d, {{len=%u, type=NLMSG_ERROR, flags=NLM_F_REQUEST"
               ", seq=0, pid=0}, {error=-EACCES"
               ", msg={{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
-              ", seq=%u, pid=%u}, \"abcd\"}}}, %u, MSG_DONTWAIT, NULL, 0)"
-              " = %s\n",
+              ", seq=%u, pid=%u}, \"\\x61\\x62\\x63\\x64\"}}}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
               fd, nlh->nlmsg_len, err->msg.nlmsg_len, NLM_F_DUMP,
               err->msg.nlmsg_seq, err->msg.nlmsg_pid,
               nlh->nlmsg_len, sprintrc(rc));
 }
 
-int main(void)
+static void
+test_nlmsg_done(const int fd)
+{
+       struct nlmsghdr *nlh;
+       void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+       long rc;
+       const int num = 0xfacefeed;
+
+       /* NLMSG_DONE message without enough room for an integer payload */
+       nlh = nlh0;
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
+               .nlmsg_type = NLMSG_DONE,
+               .nlmsg_flags = NLM_F_MULTI
+       };
+
+       rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
+              ", seq=0, pid=0}, %p}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh->nlmsg_len, nlh0 + NLMSG_HDRLEN,
+              nlh->nlmsg_len, sprintrc(rc));
+
+       /* NLMSG_DONE message with enough room for an oddly short payload */
+       nlh->nlmsg_len = NLMSG_HDRLEN + 2;
+       nlh = nlh0 - 2;
+       /* Beware of unaligned access to nlh members. */
+       memmove(nlh, nlh0, sizeof(*nlh));
+       memcpy(NLMSG_DATA(nlh), "42", 2);
+
+       rc = sendto(fd, nlh, NLMSG_HDRLEN + 2, MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI, seq=0"
+              ", pid=0}, \"\\x34\\x32\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, NLMSG_HDRLEN + 2, NLMSG_HDRLEN + 2, sprintrc(rc));
+
+       /* NLMSG_DONE message with enough room for an integer payload */
+       nlh = nlh0 - sizeof(num);
+       *nlh = (struct nlmsghdr) {
+               .nlmsg_len = NLMSG_HDRLEN + sizeof(num),
+               .nlmsg_type = NLMSG_DONE,
+               .nlmsg_flags = NLM_F_MULTI
+       };
+       memcpy(NLMSG_DATA(nlh), &num, sizeof(num));
+
+       rc = sendto(fd, nlh, nlh->nlmsg_len, MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {{len=%u, type=NLMSG_DONE, flags=NLM_F_MULTI"
+              ", seq=0, pid=0}, %d}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh->nlmsg_len, num, nlh->nlmsg_len, sprintrc(rc));
+}
+
+#if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
+static void
+test_ack_flags(const int fd)
 {
-       struct sockaddr_nl addr;
-       socklen_t len = sizeof(addr);
-       int fd;
+       long rc;
+       struct nlmsghdr nlh = {
+               .nlmsg_len = sizeof(nlh),
+               .nlmsg_type = NLMSG_ERROR,
+       };
 
-       memset(&addr, 0, sizeof(addr));
-       addr.nl_family = AF_NETLINK;
+#ifdef NLM_F_CAPPED
+       nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED,
+       rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+              ", flags=NLM_F_REQUEST|NLM_F_CAPPED, seq=0, pid=0}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
 
-       if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG)) == -1)
-               perror_msg_and_skip("socket AF_NETLINK");
+#ifdef NLM_F_ACK_TLVS
+       nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK_TLVS;
+       rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+              ", flags=NLM_F_REQUEST|NLM_F_ACK_TLVS, seq=0, pid=0}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
+
+#if defined NLM_F_CAPPED && defined NLM_F_ACK_TLVS
+       nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED | NLM_F_ACK_TLVS;
+       rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
+       printf("sendto(%d, {len=%u, type=NLMSG_ERROR"
+              ", flags=NLM_F_REQUEST|NLM_F_CAPPED|NLM_F_ACK_TLVS, seq=0, pid=0}"
+              ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+              fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
+#endif
+}
+#endif
 
-       printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = %d\n",
-              fd);
-       if (bind(fd, (struct sockaddr *) &addr, len))
-               perror_msg_and_skip("bind");
-       printf("bind(%d, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}"
-              ", %u) = 0\n", fd, len);
+int main(void)
+{
+       const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
 
        char *path;
        if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
@@ -346,9 +416,12 @@ int main(void)
 
        send_query(fd);
        test_nlmsgerr(fd);
+       test_nlmsg_done(fd);
+#if defined NLM_F_CAPPED || defined NLM_F_ACK_TLVS
+       test_ack_flags(fd);
+#endif
 
-       printf("+++ exited with 0 +++\n");
-
+       puts("+++ exited with 0 +++");
        return 0;
 }