]> granicus.if.org Git - strace/blob - tests/netlink_protocol.c
tests: use sprintrc in tests/netlink_protocol.c
[strace] / tests / netlink_protocol.c
1 /*
2  * Check decoding of netlink protocol.
3  *
4  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "tests.h"
32
33 #ifdef HAVE_SYS_XATTR_H
34
35 # include <stdio.h>
36 # include <stdlib.h>
37 # include <string.h>
38 # include <unistd.h>
39 # include <sys/xattr.h>
40 # include <netinet/in.h>
41 # include <linux/netlink.h>
42 # include <linux/sock_diag.h>
43 # include <linux/netlink_diag.h>
44
45 # if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
46 #  define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
47 # endif
48
49 static void
50 send_query(const int fd)
51 {
52         static const struct req {
53                 struct nlmsghdr nlh;
54                 const char magic[4];
55         } c_req = {
56                 .nlh = {
57                         .nlmsg_len = sizeof(struct req),
58                         .nlmsg_type = NLMSG_NOOP,
59                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
60                 },
61                 .magic = "abcd"
62         };
63         struct req *const req = tail_memdup(&c_req, sizeof(c_req));
64         long rc;
65         const char *errstr;
66
67         /* zero address */
68         rc = sendto(fd, NULL, sizeof(*req), MSG_DONTWAIT, NULL, 0);
69         printf("sendto(%d, NULL, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
70                fd, (unsigned) sizeof(*req), sprintrc(rc));
71
72         /* zero length */
73         rc = sendto(fd, req, 0, MSG_DONTWAIT, NULL, 0);
74         printf("sendto(%d, \"\", 0, MSG_DONTWAIT, NULL, 0) = %s\n",
75                fd, sprintrc(rc));
76
77         /* zero address and length */
78         rc = sendto(fd, NULL, 0, MSG_DONTWAIT, NULL, 0);
79         printf("sendto(%d, NULL, 0, MSG_DONTWAIT, NULL, 0) = %s\n",
80                fd, sprintrc(rc));
81
82         /* unfetchable struct nlmsghdr */
83         const void *const efault = tail_alloc(sizeof(struct nlmsghdr) - 1);
84         rc = sendto(fd, efault, sizeof(struct nlmsghdr), MSG_DONTWAIT, NULL, 0);
85         printf("sendto(%d, %p, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
86                fd, efault, (unsigned) sizeof(struct nlmsghdr), sprintrc(rc));
87
88         /* whole message length < sizeof(struct nlmsghdr) */
89         rc = sendto(fd, req->magic, sizeof(req->magic), MSG_DONTWAIT, NULL, 0);
90         printf("sendto(%d, \"abcd\", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
91                fd, (unsigned) sizeof(req->magic), sprintrc(rc));
92
93         /* a single message with some data */
94         rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
95         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
96                ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
97                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
98                (unsigned) sizeof(*req), sprintrc(rc));
99
100         /* a single message without data */
101         req->nlh.nlmsg_len = sizeof(req->nlh);
102         rc = sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0);
103         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
104                ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
105                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
106                (unsigned) sizeof(req->nlh), sprintrc(rc));
107
108         /* nlmsg_len > whole message length */
109         req->nlh.nlmsg_len = sizeof(*req) + 8;
110         rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
111         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
112                ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
113                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
114                (unsigned) sizeof(*req), sprintrc(rc));
115
116         /* nlmsg_len < sizeof(struct nlmsghdr) */
117         req->nlh.nlmsg_len = 8;
118         rc = sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0);
119         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
120                ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
121                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
122                (unsigned) sizeof(*req), sprintrc(rc));
123
124         /* a sequence of two nlmsg objects */
125         struct reqs {
126                 struct req req1;
127                 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
128                 struct req req2;
129         } *const reqs = tail_alloc(sizeof(*reqs));
130         memcpy(&reqs->req1, &c_req, sizeof(c_req));
131         memcpy(&reqs->req2, &c_req, sizeof(c_req));
132
133         rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
134         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
135                ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
136                ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}, \"abcd\"}]"
137                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
138                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
139                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
140                (unsigned) sizeof(*reqs), sprintrc(rc));
141
142         /* unfetchable second struct nlmsghdr */
143         void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
144         rc = sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
145         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
146                ", seq=0, pid=0}, \"abcd\"}, %p], %u, MSG_DONTWAIT, NULL, 0)"
147                " = %s\n",
148                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
149                &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs),
150                sprintrc(rc));
151
152         /* message length is not enough for the second struct nlmsghdr */
153         rc = sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT,
154                     NULL, 0);
155         errstr = sprintrc(rc);
156         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
157                ", seq=0, pid=0}, \"abcd\"}, \"",
158                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
159         print_quoted_memory((void *) &reqs->req2.nlh,
160                             sizeof(reqs->req2) - sizeof(req->nlh));
161         printf("\"], %u, MSG_DONTWAIT, NULL, 0) = %s\n",
162                (unsigned) (sizeof(*reqs) - sizeof(req->nlh)), errstr);
163
164         /* second nlmsg_len < sizeof(struct nlmsghdr) */
165         reqs->req2.nlh.nlmsg_len = 4;
166         rc = sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
167         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
168                ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
169                ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}}], %u"
170                ", MSG_DONTWAIT, NULL, 0) = %s\n",
171                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
172                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
173                (unsigned) sizeof(*reqs), sprintrc(rc));
174
175         /* abbreviated output */
176 # define DEFAULT_STRLEN 32
177 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
178         const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
179         struct nlmsghdr *const msgs = tail_alloc(msg_len);
180         unsigned int i;
181         for (i = 0; i < ABBREV_LEN; ++i) {
182                 msgs[i].nlmsg_len = sizeof(*msgs);
183                 msgs[i].nlmsg_type = NLMSG_NOOP;
184                 msgs[i].nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
185                 msgs[i].nlmsg_seq = i;
186                 msgs[i].nlmsg_pid = 0;
187         }
188
189         rc = sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0);
190         errstr = sprintrc(rc);
191         printf("sendto(%d, [", fd);
192         for (i = 0; i < DEFAULT_STRLEN; ++i) {
193                 if (i)
194                         printf(", ");
195                 printf("{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
196                        ", seq=%u, pid=0}}",
197                        msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
198         }
199         printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %s\n", msg_len, errstr);
200 }
201
202 int main(void)
203 {
204         struct sockaddr_nl addr;
205         socklen_t len = sizeof(addr);
206         int fd;
207
208         memset(&addr, 0, sizeof(addr));
209         addr.nl_family = AF_NETLINK;
210
211         if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG)) == -1)
212                 perror_msg_and_skip("socket AF_NETLINK");
213
214         printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = %d\n",
215                fd);
216         if (bind(fd, (struct sockaddr *) &addr, len))
217                 perror_msg_and_skip("bind");
218         printf("bind(%d, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}"
219                ", %u) = 0\n", fd, len);
220
221         char *path;
222         if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
223                 perror_msg_and_fail("asprintf");
224         char buf[256];
225         if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
226                 perror_msg_and_skip("getxattr");
227         free(path);
228
229         send_query(fd);
230
231         printf("+++ exited with 0 +++\n");
232
233         return 0;
234 }
235
236 #else
237
238 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
239
240 #endif