2 * Check decoding of netlink protocol.
4 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
33 #ifdef HAVE_SYS_XATTR_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>
45 # if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
46 # define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
50 send_query(const int fd)
52 static const struct req {
57 .nlmsg_len = sizeof(struct req),
58 .nlmsg_type = NLMSG_NOOP,
59 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
63 struct req *const req = tail_memdup(&c_req, sizeof(c_req));
66 if (sendto(fd, NULL, sizeof(*req), MSG_DONTWAIT, NULL, 0) != -1)
67 perror_msg_and_skip("sendto");
69 printf("sendto(%d, NULL, %u, MSG_DONTWAIT, NULL, 0) = -1 %s (%m)\n",
70 fd, (unsigned) sizeof(*req), errno2name());
73 if (sendto(fd, req, 0, MSG_DONTWAIT, NULL, 0) != 0)
74 perror_msg_and_skip("sendto");
76 printf("sendto(%d, \"\", 0, MSG_DONTWAIT, NULL, 0) = 0\n", fd);
78 /* zero address and length */
79 if (sendto(fd, NULL, 0, MSG_DONTWAIT, NULL, 0) != 0)
80 perror_msg_and_skip("sendto");
82 printf("sendto(%d, NULL, 0, MSG_DONTWAIT, NULL, 0) = 0\n", fd);
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);
88 printf("sendto(%d, %p, %u, MSG_DONTWAIT, NULL, 0) = -1 EFAULT (%m)\n",
89 fd, efault, (unsigned) sizeof(struct nlmsghdr));
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");
96 printf("sendto(%d, \"abcd\", %u, MSG_DONTWAIT, NULL, 0) = %u\n",
97 fd, (unsigned) sizeof(req->magic), (unsigned) sizeof(req->magic));
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");
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));
109 /* a single message without data */
110 req->nlh.nlmsg_len = sizeof(req->nlh);
112 if (sendto(fd, &req->nlh, sizeof(req->nlh), MSG_DONTWAIT, NULL, 0)
113 != (unsigned) sizeof(req->nlh))
114 perror_msg_and_skip("sendto");
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));
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");
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));
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");
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));
142 /* a sequence of two nlmsg objects */
145 char padding[NLMSG_ALIGN(sizeof(struct req)) - sizeof(struct req)];
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));
151 sendto(fd, reqs, sizeof(*reqs), MSG_DONTWAIT, NULL, 0);
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));
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);
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));
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");
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)));
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)
189 perror_msg_and_skip("sendto");
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));
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);
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;
213 if (sendto(fd, msgs, msg_len, MSG_DONTWAIT, NULL, 0) != (int) msg_len)
214 perror_msg_and_skip("sendto");
216 printf("sendto(%d, [", fd);
217 for (i = 0; i < DEFAULT_STRLEN; ++i) {
220 printf("{{len=%u, type=NLMSG_NOOP, flags=NLM_F_REQUEST|0x%x"
222 msgs[i].nlmsg_len, NLM_F_DUMP, msgs[i].nlmsg_seq);
224 printf(", ...], %u, MSG_DONTWAIT, NULL, 0) = %u\n", msg_len, msg_len);
229 struct sockaddr_nl addr;
230 socklen_t len = sizeof(addr);
233 memset(&addr, 0, sizeof(addr));
234 addr.nl_family = AF_NETLINK;
236 if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG)) == -1)
237 perror_msg_and_skip("socket AF_NETLINK");
239 printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = %d\n",
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);
247 if (asprintf(&path, "/proc/self/fd/%u", fd) < 0)
248 perror_msg_and_fail("asprintf");
250 if (getxattr(path, "system.sockprotoname", buf, sizeof(buf) - 1) < 0)
251 perror_msg_and_skip("getxattr");
256 printf("+++ exited with 0 +++\n");
263 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")