]> granicus.if.org Git - strace/blob - tests/netlink_protocol.c
tests: skip netlink_protocol.test on old systems
[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
65         /* zero address */
66         if (sendto(fd, NULL, sizeof(*req), MSG_DONTWAIT, NULL, 0) != -1)
67                 perror_msg_and_skip("sendto");
68
69         printf("sendto(%d, NULL, %u, MSG_DONTWAIT, NULL, 0) = -1 %s (%m)\n",
70                fd, (unsigned) sizeof(*req), errno2name());
71
72         /* zero length */
73         if (sendto(fd, req, 0, MSG_DONTWAIT, NULL, 0) != 0)
74                 perror_msg_and_skip("sendto");
75
76         printf("sendto(%d, \"\", 0, MSG_DONTWAIT, NULL, 0) = 0\n", fd);
77
78         /* zero address and length */
79         if (sendto(fd, NULL, 0, MSG_DONTWAIT, NULL, 0) != 0)
80                 perror_msg_and_skip("sendto");
81
82         printf("sendto(%d, NULL, 0, MSG_DONTWAIT, NULL, 0) = 0\n", fd);
83
84         /* unfetchable struct nlmsghdr */
85         const void *const efault = tail_alloc(sizeof(struct nlmsghdr) - 1);
86         sendto(fd, efault, sizeof(struct nlmsghdr), MSG_DONTWAIT, NULL, 0);
87
88         printf("sendto(%d, %p, %u, MSG_DONTWAIT, NULL, 0) = -1 EFAULT (%m)\n",
89                fd, efault, (unsigned) sizeof(struct nlmsghdr));
90
91         /* whole message length < sizeof(struct nlmsghdr) */
92         if (sendto(fd, req->magic, sizeof(req->magic), MSG_DONTWAIT, NULL, 0)
93             != (unsigned) sizeof(req->magic))
94                 perror_msg_and_skip("sendto");
95
96         printf("sendto(%d, \"abcd\", %u, MSG_DONTWAIT, NULL, 0) = %u\n",
97                fd, (unsigned) sizeof(req->magic), (unsigned) sizeof(req->magic));
98
99         /* a single message with some data */
100         if (sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0) !=
101             (unsigned) sizeof(*req))
102                 perror_msg_and_skip("sendto");
103
104         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
105                ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %u\n",
106                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
107                (unsigned) sizeof(*req), (unsigned) sizeof(*req));
108
109         /* a single message without data */
110         req->nlh.nlmsg_len = sizeof(req->nlh);
111
112         if (sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0)
113             != (unsigned) sizeof(req->nlh))
114                 perror_msg_and_skip("sendto");
115
116         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
117                ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %u\n",
118                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
119                (unsigned) sizeof(req->nlh), (unsigned) sizeof(req->nlh));
120
121         /* nlmsg_len > whole message length */
122         req->nlh.nlmsg_len = sizeof(*req) + 8;
123         if (sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0) !=
124             (unsigned) sizeof(*req))
125                 perror_msg_and_skip("sendto");
126
127         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
128                ", seq=0, pid=0}, \"abcd\"}, %u, MSG_DONTWAIT, NULL, 0) = %u\n",
129                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
130                (unsigned) sizeof(*req), (unsigned) sizeof(*req));
131
132         /* nlmsg_len < sizeof(struct nlmsghdr) */
133         req->nlh.nlmsg_len = 8;
134         if (sendto(fd, req, sizeof(*req), MSG_DONTWAIT, NULL, 0) != sizeof(*req))
135                 perror_msg_and_skip("sendto");
136
137         printf("sendto(%d, {{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
138                ", seq=0, pid=0}}, %u, MSG_DONTWAIT, NULL, 0) = %u\n",
139                fd, req->nlh.nlmsg_len, NLM_F_DUMP,
140                (unsigned) sizeof(*req), (unsigned) sizeof(*req));
141
142         /* a sequence of two nlmsg objects */
143         struct reqs {
144                 struct req req1;
145                 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
146                 struct req req2;
147         } *const reqs = tail_alloc(sizeof(*reqs));
148         memcpy(&reqs->req1, &c_req, sizeof(c_req));
149         memcpy(&reqs->req2, &c_req, sizeof(c_req));
150
151         sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
152
153         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
154                ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
155                ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}, \"abcd\"}]"
156                ", %u, MSG_DONTWAIT, NULL, 0) = %u\n",
157                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
158                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
159                (unsigned) sizeof(*reqs), (unsigned) sizeof(*reqs));
160
161         /* unfetchable second struct nlmsghdr */
162         void *const efault2 = tail_memdup(&reqs->req1, sizeof(reqs->req1));
163         sendto(fd, efault2, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
164
165         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
166                ", seq=0, pid=0}, \"abcd\"}, %p], %u, MSG_DONTWAIT, NULL, 0)"
167                " = -1 EFAULT (%m)\n",
168                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
169                &((struct reqs *) efault2)->req2, (unsigned) sizeof(*reqs));
170
171         /* message length is not enough for the second struct nlmsghdr */
172         if (sendto(fd, reqs, sizeof(*reqs) - sizeof(req->nlh), MSG_DONTWAIT, NULL, 0)
173             != sizeof(*reqs) - sizeof(req->nlh))
174                 perror_msg_and_skip("sendto");
175
176         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
177                ", seq=0, pid=0}, \"abcd\"}, \"",
178                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP);
179         print_quoted_memory((void *) &reqs->req2.nlh,
180                             sizeof(reqs->req2) - sizeof(req->nlh));
181         printf("\"], %u, MSG_DONTWAIT, NULL, 0) = %u\n",
182                (unsigned) (sizeof(*reqs) - sizeof(req->nlh)),
183                (unsigned) (sizeof(*reqs) - sizeof(req->nlh)));
184
185         /* second nlmsg_len < sizeof(struct nlmsghdr) */
186         reqs->req2.nlh.nlmsg_len = 4;
187         if (sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0)
188             != sizeof(*reqs))
189                 perror_msg_and_skip("sendto");
190
191         printf("sendto(%d, [{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
192                ", seq=0, pid=0}, \"abcd\"}, {{len=%u, type=NLMSG_NOOP"
193                ", flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}}], %u"
194                ", MSG_DONTWAIT, NULL, 0) = %u\n",
195                fd, reqs->req1.nlh.nlmsg_len, NLM_F_DUMP,
196                reqs->req2.nlh.nlmsg_len, NLM_F_DUMP,
197                (unsigned) sizeof(*reqs), (unsigned) sizeof(*reqs));
198
199         /* abbreviated output */
200 # define DEFAULT_STRLEN 32
201 # define ABBREV_LEN (DEFAULT_STRLEN + 1)
202         const unsigned int msg_len = sizeof(struct nlmsghdr) * ABBREV_LEN;
203         struct nlmsghdr *const msgs = tail_alloc(msg_len);
204         unsigned int i;
205         for (i = 0; i < ABBREV_LEN; ++i) {
206                 msgs[i].nlmsg_len = sizeof(*msgs);
207                 msgs[i].nlmsg_type = NLMSG_NOOP;
208                 msgs[i].nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
209                 msgs[i].nlmsg_seq = i;
210                 msgs[i].nlmsg_pid = 0;
211         }
212
213         if (sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0) != (int) msg_len)
214                 perror_msg_and_skip("sendto");
215
216         printf("sendto(%d, [", fd);
217         for (i = 0; i < DEFAULT_STRLEN; ++i) {
218                 if (i)
219                         printf(", ");
220                 printf("{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
221                        ", seq=%u, pid=0}}",
222                        msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
223         }
224         printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %u\n", msg_len, msg_len);
225 }
226
227 int main(void)
228 {
229         struct sockaddr_nl addr;
230         socklen_t len = sizeof(addr);
231         int fd;
232
233         memset(&addr, 0, sizeof(addr));
234         addr.nl_family = AF_NETLINK;
235
236         if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG)) == -1)
237                 perror_msg_and_skip("socket AF_NETLINK");
238
239         printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = %d\n",
240                fd);
241         if (bind(fd, (struct sockaddr *) &addr, len))
242                 perror_msg_and_skip("bind");
243         printf("bind(%d, {sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}"
244                ", %u) = 0\n", fd, len);
245
246         char *path;
247         if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
248                 perror_msg_and_fail("asprintf");
249         char buf[256];
250         if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
251                 perror_msg_and_skip("getxattr");
252         free(path);
253
254         send_query(fd);
255
256         printf("+++ exited with 0 +++\n");
257
258         return 0;
259 }
260
261 #else
262
263 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")
264
265 #endif