blob: b8d019040472dbc3dcfb34fea3572e4889220b45 [file] [log] [blame]
Elliott Hughesdc75b012017-07-05 13:54:44 -07001/*
2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2017 The strace developers.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "tests.h"
31#include <stdio.h>
32#include <string.h>
33#include <stdint.h>
34#include <unistd.h>
35#include <sys/socket.h>
36#include <arpa/inet.h>
37#include <netinet/tcp.h>
38#include "netlink.h"
39#include <linux/if_ether.h>
40#include <linux/inet_diag.h>
41#include <linux/netlink_diag.h>
42#include <linux/packet_diag.h>
43#ifdef AF_SMC
44# include <linux/smc_diag.h>
45#endif
46#include <linux/sock_diag.h>
47#include <linux/unix_diag.h>
48
49#define SMC_ACTIVE 1
50
51static void
52test_nlmsg_type(const int fd)
53{
54 long rc;
55 struct nlmsghdr nlh = {
56 .nlmsg_len = sizeof(nlh),
57 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
58 .nlmsg_flags = NLM_F_REQUEST,
59 };
60
61 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
62 printf("sendto(%d, {len=%u, type=SOCK_DIAG_BY_FAMILY"
63 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
64 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
65 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
66}
67
68static void
69test_nlmsg_flags(const int fd)
70{
71 long rc;
72 struct nlmsghdr nlh = {
73 .nlmsg_len = sizeof(nlh),
74 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
75 .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP,
76 };
77
78 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
79 printf("sendto(%d, {len=%u, type=SOCK_DIAG_BY_FAMILY"
80 ", flags=NLM_F_REQUEST|NLM_F_DUMP, seq=0, pid=0}"
81 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
82 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
83}
84
85static void
86test_odd_family_req(const int fd)
87{
88 struct nlmsghdr *nlh;
89 uint8_t *family;
90 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
91 long rc;
92
93 /* unspecified family only */
94 nlh = nlh0 - sizeof(*family);
95 /* beware of unaligned access to nlh members */
96 SET_STRUCT(struct nlmsghdr, nlh,
97 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
98 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
99 .nlmsg_flags = NLM_F_REQUEST
100 );
101 family = NLMSG_DATA(nlh);
102 *family = 0;
103
104 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
105 NULL, 0);
106
107 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
108 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
109 ", {family=AF_UNSPEC}}"
110 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
111 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
112 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
113 sprintrc(rc));
114
115 /* unknown family only */
116 *family = 0xff;
117
118 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
119 NULL, 0);
120
121 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
122 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
123 ", {family=0xff /* AF_??? */}}"
124 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
125 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
126 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
127 sprintrc(rc));
128
129 /* short read of family */
130 memmove(nlh0, nlh, NLMSG_HDRLEN);
131 nlh = nlh0;
132
133 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
134 NULL, 0);
135
136 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
137 ", flags=NLM_F_REQUEST, seq=0, pid=0}, %p}"
138 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
139 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
140 NLMSG_DATA(nlh),
141 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
142 sprintrc(rc));
143
144 /* unspecified family and string */
145 nlh = nlh0 - (sizeof(*family) + 4);
146 /* beware of unaligned access to nlh members */
147 SET_STRUCT(struct nlmsghdr, nlh,
148 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
149 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
150 .nlmsg_flags = NLM_F_REQUEST
151 );
152 family = NLMSG_DATA(nlh);
153 *family = 0;
154 memcpy(family + 1, "1234", 4);
155
156 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
157 NULL, 0);
158
159 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
160 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
161 ", {family=AF_UNSPEC, \"1234\"}}"
162 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
163 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
164 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
165 sprintrc(rc));
166
167 /* unknown family and string */
168 *family = 0xfd;
169
170 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
171 NULL, 0);
172
173 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
174 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
175 ", {family=0xfd /* AF_??? */, \"1234\"}}"
176 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
177 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
178 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
179 sprintrc(rc));
180}
181
182static void
183test_odd_family_msg(const int fd)
184{
185 struct nlmsghdr *nlh;
186 uint8_t *family;
187 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
188 long rc;
189
190 /* unspecified family only */
191 nlh = nlh0 - sizeof(*family);
192 /* beware of unaligned access to nlh members */
193 SET_STRUCT(struct nlmsghdr, nlh,
194 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
195 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
196 .nlmsg_flags = NLM_F_DUMP
197 );
198 family = NLMSG_DATA(nlh);
199 *family = 0;
200
201 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
202 NULL, 0);
203
204 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
205 ", flags=NLM_F_DUMP, seq=0, pid=0}"
206 ", {family=AF_UNSPEC}}"
207 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
208 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
209 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
210 sprintrc(rc));
211
212 /* unknown family only */
213 *family = 0xff;
214
215 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
216 NULL, 0);
217
218 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
219 ", flags=NLM_F_DUMP, seq=0, pid=0}"
220 ", {family=0xff /* AF_??? */}}"
221 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
222 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
223 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
224 sprintrc(rc));
225
226 /* short read of family */
227 memmove(nlh0, nlh, NLMSG_HDRLEN);
228 nlh = nlh0;
229
230 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
231 NULL, 0);
232
233 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
234 ", flags=NLM_F_DUMP, seq=0, pid=0}, %p}"
235 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
236 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
237 NLMSG_DATA(nlh),
238 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
239 sprintrc(rc));
240
241 /* unspecified family and string */
242 nlh = nlh0 - (sizeof(*family) + 4);
243 /* beware of unaligned access to nlh members */
244 SET_STRUCT(struct nlmsghdr, nlh,
245 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
246 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
247 .nlmsg_flags = NLM_F_DUMP
248 );
249 family = NLMSG_DATA(nlh);
250 *family = 0;
251 memcpy(family + 1, "1234", 4);
252
253 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
254 NULL, 0);
255
256 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
257 ", flags=NLM_F_DUMP, seq=0, pid=0}"
258 ", {family=AF_UNSPEC, \"1234\"}}"
259 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
260 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
261 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
262 sprintrc(rc));
263
264 /* unknown family and string */
265 *family = 0xfb;
266
267 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
268 NULL, 0);
269
270 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
271 ", flags=NLM_F_DUMP, seq=0, pid=0}"
272 ", {family=0xfb /* AF_??? */, \"1234\"}}"
273 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
274 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
275 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
276 sprintrc(rc));
277}
278
279static void
280test_unix_diag_req(const int fd)
281{
282 struct nlmsghdr *nlh;
283 struct unix_diag_req *req;
284 uint8_t *family;
285 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
286 long rc;
287
288 /* family only */
289 nlh = nlh0 - sizeof(*family);
290 /* beware of unaligned access to nlh members */
291 SET_STRUCT(struct nlmsghdr, nlh,
292 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
293 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
294 .nlmsg_flags = NLM_F_REQUEST
295 );
296 family = NLMSG_DATA(nlh);
297 *family = AF_UNIX;
298
299 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
300 NULL, 0);
301
302 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
303 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_UNIX}}"
304 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
305 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
306 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
307 sprintrc(rc));
308
309 /* family and string */
310 nlh = nlh0 - (sizeof(*family) + 4);
311 /* beware of unaligned access to nlh members */
312 SET_STRUCT(struct nlmsghdr, nlh,
313 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
314 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
315 .nlmsg_flags = NLM_F_REQUEST
316 );
317 family = NLMSG_DATA(nlh);
318 *family = AF_UNIX;
319 memcpy(family + 1, "1234", 4);
320
321 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
322 NULL, 0);
323
324 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
325 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
326 ", {sdiag_family=AF_UNIX, ...}}"
327 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
328 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
329 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
330 sprintrc(rc));
331
332 /* unix_diag_req */
333 nlh = nlh0 - sizeof(*req);
334 SET_STRUCT(struct nlmsghdr, nlh,
335 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
336 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
337 .nlmsg_flags = NLM_F_REQUEST
338 );
339 req = NLMSG_DATA(nlh);
340 *req = (struct unix_diag_req) {
341 .sdiag_family = AF_UNIX,
342 .sdiag_protocol = 253,
343 .udiag_states = 1 << TCP_ESTABLISHED | 1 << TCP_LISTEN,
344 .udiag_ino = 0xfacefeed,
345 .udiag_show = UDIAG_SHOW_NAME,
346 .udiag_cookie = { 0xdeadbeef, 0xbadc0ded }
347 };
348
349 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
350 NULL, 0);
351
352 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
353 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
354 ", {sdiag_family=AF_UNIX, sdiag_protocol=%u"
355 ", udiag_states=1<<TCP_ESTABLISHED|1<<TCP_LISTEN, udiag_ino=%u"
356 ", udiag_show=UDIAG_SHOW_NAME, udiag_cookie=[%u, %u]}}"
357 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
358 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
359 253, 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
360 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
361 sprintrc(rc));
362
363 /* short read of unix_diag_req */
364 nlh = nlh0 - (sizeof(*req) - 1);
365 /* beware of unaligned access to nlh members */
366 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
367
368 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
369 NULL, 0);
370
371 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
372 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {sdiag_family=AF_UNIX, %p}}"
373 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
374 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
375 NLMSG_DATA(nlh) + 1,
376 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
377 sprintrc(rc));
378}
379
380static void
381test_unix_diag_msg(const int fd)
382{
383 struct nlmsghdr *nlh;
384 struct unix_diag_msg *msg;
385 uint8_t *family;
386 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
387 long rc;
388
389 /* family only */
390 nlh = nlh0 - sizeof(*family);
391 /* beware of unaligned access to nlh members */
392 SET_STRUCT(struct nlmsghdr, nlh,
393 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
394 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
395 .nlmsg_flags = NLM_F_DUMP
396 );
397 family = NLMSG_DATA(nlh);
398 *family = AF_UNIX;
399
400 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
401 NULL, 0);
402
403 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
404 ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_UNIX}}"
405 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
406 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
407 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
408 sprintrc(rc));
409
410 /* family and string */
411 nlh = nlh0 - (sizeof(*family) + 4);
412 /* beware of unaligned access to nlh members */
413 SET_STRUCT(struct nlmsghdr, nlh,
414 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
415 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
416 .nlmsg_flags = NLM_F_DUMP
417 );
418 family = NLMSG_DATA(nlh);
419 *family = AF_UNIX;
420 memcpy(family + 1, "1234", 4);
421
422 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
423 NULL, 0);
424
425 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
426 ", flags=NLM_F_DUMP, seq=0, pid=0}"
427 ", {udiag_family=AF_UNIX, ...}}"
428 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
429 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
430 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
431 sprintrc(rc));
432
433 /* unix_diag_msg */
434 nlh = nlh0 - sizeof(*msg);
435 SET_STRUCT(struct nlmsghdr, nlh,
436 .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
437 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
438 .nlmsg_flags = NLM_F_DUMP
439 );
440 msg = NLMSG_DATA(nlh);
441 *msg = (struct unix_diag_msg) {
442 .udiag_family = AF_UNIX,
443 .udiag_type = SOCK_STREAM,
444 .udiag_state = TCP_FIN_WAIT1,
445 .udiag_ino = 0xfacefeed,
446 .udiag_cookie = { 0xdeadbeef, 0xbadc0ded }
447 };
448
449 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
450 NULL, 0);
451
452 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
453 ", flags=NLM_F_DUMP, seq=0, pid=0}"
454 ", {udiag_family=AF_UNIX, udiag_type=SOCK_STREAM"
455 ", udiag_state=TCP_FIN_WAIT1"
456 ", udiag_ino=%u, udiag_cookie=[%u, %u]}}"
457 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
458 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
459 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
460 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
461 sprintrc(rc));
462
463 /* short read of unix_diag_msg */
464 nlh = nlh0 - (sizeof(*msg) - 1);
465 /* beware of unaligned access to nlh members */
466 memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*msg) - 1);
467
468 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
469 NULL, 0);
470
471 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
472 ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX, %p}}"
473 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
474 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
475 NLMSG_DATA(nlh) + 1,
476 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
477 sprintrc(rc));
478}
479
480static void
481test_netlink_diag_req(const int fd)
482{
483 struct nlmsghdr *nlh;
484 struct netlink_diag_req *req;
485 uint8_t *family;
486 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
487 long rc;
488
489 /* family only */
490 nlh = nlh0 - sizeof(*family);
491 /* beware of unaligned access to nlh members */
492 SET_STRUCT(struct nlmsghdr, nlh,
493 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
494 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
495 .nlmsg_flags = NLM_F_REQUEST
496 );
497 family = NLMSG_DATA(nlh);
498 *family = AF_NETLINK;
499
500 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
501 NULL, 0);
502
503 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
504 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_NETLINK}}"
505 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
506 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
507 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
508 sprintrc(rc));
509
510 /* family and string */
511 nlh = nlh0 - (sizeof(*family) + 4);
512 /* beware of unaligned access to nlh members */
513 SET_STRUCT(struct nlmsghdr, nlh,
514 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
515 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
516 .nlmsg_flags = NLM_F_REQUEST
517 );
518 family = NLMSG_DATA(nlh);
519 *family = AF_NETLINK;
520 memcpy(family + 1, "1234", 4);
521
522 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
523 NULL, 0);
524
525 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
526 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
527 ", {sdiag_family=AF_NETLINK, ...}}"
528 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
529 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
530 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
531 sprintrc(rc));
532
533 /* netlink_diag_req */
534 nlh = nlh0 - sizeof(*req);
535 SET_STRUCT(struct nlmsghdr, nlh,
536 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
537 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
538 .nlmsg_flags = NLM_F_REQUEST
539 );
540 req = NLMSG_DATA(nlh);
541 *req = (struct netlink_diag_req) {
542 .sdiag_family = AF_NETLINK,
543 .sdiag_protocol = NDIAG_PROTO_ALL,
544 .ndiag_ino = 0xfacefeed,
545 .ndiag_show = NDIAG_SHOW_MEMINFO,
546 .ndiag_cookie = { 0xdeadbeef, 0xbadc0ded }
547 };
548
549
550 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
551 NULL, 0);
552
553 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
554 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
555 ", {sdiag_family=AF_NETLINK, sdiag_protocol=NDIAG_PROTO_ALL"
556 ", ndiag_ino=%u, ndiag_show=NDIAG_SHOW_MEMINFO"
557 ", ndiag_cookie=[%u, %u]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
558 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
559 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
560 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
561 sprintrc(rc));
562
563 req->sdiag_protocol = NETLINK_ROUTE;
564
565 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
566 NULL, 0);
567
568 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
569 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
570 ", {sdiag_family=AF_NETLINK, sdiag_protocol=NETLINK_ROUTE"
571 ", ndiag_ino=%u, ndiag_show=NDIAG_SHOW_MEMINFO"
572 ", ndiag_cookie=[%u, %u]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
573 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
574 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
575 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
576 sprintrc(rc));
577
578 /* short read of netlink_diag_req */
579 nlh = nlh0 - (sizeof(*req) - 1);
580 /* beware of unaligned access to nlh members */
581 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
582
583 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
584 NULL, 0);
585
586 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
587 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
588 ", {sdiag_family=AF_NETLINK, %p}}"
589 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
590 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
591 NLMSG_DATA(nlh) + 1,
592 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
593 sprintrc(rc));
594}
595
596static void
597test_netlink_diag_msg(const int fd)
598{
599 struct nlmsghdr *nlh;
600 struct netlink_diag_msg *msg;
601 uint8_t *family;
602 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
603 long rc;
604
605 /* family only */
606 nlh = nlh0 - sizeof(*family);
607 /* beware of unaligned access to nlh members */
608 SET_STRUCT(struct nlmsghdr, nlh,
609 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
610 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
611 .nlmsg_flags = NLM_F_DUMP
612 );
613 family = NLMSG_DATA(nlh);
614 *family = AF_NETLINK;
615
616 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
617 NULL, 0);
618
619 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
620 ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_NETLINK}}"
621 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
622 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
623 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
624 sprintrc(rc));
625
626 /* family and string */
627 nlh = nlh0 - (sizeof(*family) + 4);
628 /* beware of unaligned access to nlh members */
629 SET_STRUCT(struct nlmsghdr, nlh,
630 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
631 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
632 .nlmsg_flags = NLM_F_DUMP
633 );
634 family = NLMSG_DATA(nlh);
635 *family = AF_NETLINK;
636 memcpy(family + 1, "1234", 4);
637
638 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
639 NULL, 0);
640
641 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
642 ", flags=NLM_F_DUMP, seq=0, pid=0}"
643 ", {ndiag_family=AF_NETLINK, ...}}"
644 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
645 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
646 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
647 sprintrc(rc));
648
649 /* netlink_diag_msg */
650 nlh = nlh0 - sizeof(*msg);
651 SET_STRUCT(struct nlmsghdr, nlh,
652 .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
653 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
654 .nlmsg_flags = NLM_F_DUMP
655 );
656 msg = NLMSG_DATA(nlh);
657 *msg = (struct netlink_diag_msg) {
658 .ndiag_family = AF_NETLINK,
659 .ndiag_type = SOCK_RAW,
660 .ndiag_protocol = NETLINK_ROUTE,
661 .ndiag_state = NETLINK_CONNECTED,
662 .ndiag_portid = 0xbadc0ded,
663 .ndiag_dst_portid = 0xdeadbeef,
664 .ndiag_dst_group = 0xfacefeed,
665 .ndiag_ino = 0xdaeefacd,
666 .ndiag_cookie = { 0xbadc0ded, 0xdeadbeef }
667 };
668
669 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
670 NULL, 0);
671
672 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
673 ", flags=NLM_F_DUMP, seq=0, pid=0}, {ndiag_family=AF_NETLINK"
674 ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
675 ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=%u"
676 ", ndiag_dst_portid=%u, ndiag_dst_group=%u, ndiag_ino=%u"
677 ", ndiag_cookie=[%u, %u]}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
678 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
679 0xbadc0ded, 0xdeadbeef, 0xfacefeed,
680 0xdaeefacd, 0xbadc0ded, 0xdeadbeef,
681 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
682 sprintrc(rc));
683
684 /* short read of netlink_diag_msg */
685 nlh = nlh0 - (sizeof(*msg) - 1);
686 /* beware of unaligned access to nlh members */
687 memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*msg) - 1);
688
689 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
690 NULL, 0);
691
692 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
693 ", flags=NLM_F_DUMP, seq=0, pid=0}"
694 ", {ndiag_family=AF_NETLINK, %p}}"
695 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
696 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
697 NLMSG_DATA(nlh) + 1,
698 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
699 sprintrc(rc));
700}
701
702static void
703test_packet_diag_req(const int fd)
704{
705 struct nlmsghdr *nlh;
706 struct packet_diag_req *req;
707 uint8_t *family;
708 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
709 long rc;
710
711 /* family only */
712 nlh = nlh0 - sizeof(*family);
713 SET_STRUCT(struct nlmsghdr, nlh,
714 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
715 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
716 .nlmsg_flags = NLM_F_REQUEST
717 );
718 family = NLMSG_DATA(nlh);
719 *family = AF_PACKET;
720
721 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
722 NULL, 0);
723
724 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
725 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_PACKET}}"
726 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
727 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
728 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
729 sprintrc(rc));
730
731 /* family and string */
732 nlh = nlh0 - (sizeof(*family) + 4);
733 SET_STRUCT(struct nlmsghdr, nlh,
734 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
735 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
736 .nlmsg_flags = NLM_F_REQUEST
737 );
738 family = NLMSG_DATA(nlh);
739 *family = AF_PACKET;
740 memcpy(family + 1, "1234", 4);
741
742 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
743 NULL, 0);
744
745 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
746 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
747 ", {sdiag_family=AF_PACKET, ...}}"
748 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
749 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
750 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
751 sprintrc(rc));
752
753 /* packet_diag_req */
754 nlh = nlh0 - sizeof(*req);
755 SET_STRUCT(struct nlmsghdr, nlh,
756 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
757 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
758 .nlmsg_flags = NLM_F_REQUEST
759 );
760 req = NLMSG_DATA(nlh);
761 *req = (struct packet_diag_req) {
762 .sdiag_family = AF_PACKET,
763 .sdiag_protocol = ETH_P_LOOP,
764 .pdiag_ino = 0xfacefeed,
765 .pdiag_show = PACKET_SHOW_INFO,
766 .pdiag_cookie = { 0xdeadbeef, 0xbadc0ded }
767 };
768
769 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
770 NULL, 0);
771
772 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
773 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
774 ", {sdiag_family=AF_PACKET, sdiag_protocol=ETH_P_LOOP"
775 ", pdiag_ino=%u, pdiag_show=PACKET_SHOW_INFO"
776 ", pdiag_cookie=[%u, %u]}}, %u"
777 ", MSG_DONTWAIT, NULL, 0) = %s\n",
778 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
779 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
780 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
781 sprintrc(rc));
782
783 /* short read of packet_diag_req */
784 nlh = nlh0 - (sizeof(*req) - 1);
785 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
786
787 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
788 NULL, 0);
789
790 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
791 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
792 ", {sdiag_family=AF_PACKET, %p}}"
793 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
794 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
795 NLMSG_DATA(nlh) + 1,
796 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
797 sprintrc(rc));
798}
799
800static void
801test_packet_diag_msg(const int fd)
802{
803 struct nlmsghdr *nlh;
804 struct packet_diag_msg *msg;
805 uint8_t *family;
806 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
807 long rc;
808
809 /* family only */
810 nlh = nlh0 - sizeof(*family);
811 SET_STRUCT(struct nlmsghdr, nlh,
812 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
813 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
814 .nlmsg_flags = NLM_F_DUMP
815 );
816 family = NLMSG_DATA(nlh);
817 *family = AF_PACKET;
818
819 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
820 NULL, 0);
821
822 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
823 ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_PACKET}}"
824 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
825 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
826 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
827 sprintrc(rc));
828
829 /* family and string */
830 nlh = nlh0 - (sizeof(*family) + 4);
831 SET_STRUCT(struct nlmsghdr, nlh,
832 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
833 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
834 .nlmsg_flags = NLM_F_DUMP
835 );
836 family = NLMSG_DATA(nlh);
837 *family = AF_PACKET;
838 memcpy(family + 1, "1234", 4);
839
840 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
841 NULL, 0);
842
843 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
844 ", flags=NLM_F_DUMP, seq=0, pid=0}"
845 ", {pdiag_family=AF_PACKET, ...}}"
846 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
847 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
848 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
849 sprintrc(rc));
850
851 /* packet_diag_msg */
852 nlh = nlh0 - sizeof(*msg);
853 SET_STRUCT(struct nlmsghdr, nlh,
854 .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
855 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
856 .nlmsg_flags = NLM_F_DUMP
857 );
858 msg = NLMSG_DATA(nlh);
859 *msg = (struct packet_diag_msg) {
860 .pdiag_family = AF_PACKET,
861 .pdiag_type = SOCK_STREAM,
862 .pdiag_num = 0xbadc,
863 .pdiag_ino = 0xfacefeed,
864 .pdiag_cookie = { 0xdeadbeef, 0xbadc0ded }
865 };
866
867 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
868 NULL, 0);
869
870 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
871 ", flags=NLM_F_DUMP, seq=0, pid=0}"
872 ", {pdiag_family=AF_PACKET, pdiag_type=SOCK_STREAM"
873 ", pdiag_num=%u, pdiag_ino=%u, pdiag_cookie=[%u, %u]}}"
874 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
875 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
876 0xbadc, 0xfacefeed, 0xdeadbeef, 0xbadc0ded,
877 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
878 sprintrc(rc));
879
880 /* short read of packet_diag_msg */
881 nlh = nlh0 - (sizeof(*msg) - 1);
882 memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*msg) - 1);
883
884 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
885 NULL, 0);
886
887 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
888 ", flags=NLM_F_DUMP, seq=0, pid=0}"
889 ", {pdiag_family=AF_PACKET, %p}}"
890 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
891 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
892 NLMSG_DATA(nlh) + 1,
893 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
894 sprintrc(rc));
895}
896
897static void
898test_inet_diag_sockid(const int fd)
899{
900 const char address[] = "12.34.56.78";
901 const char address6[] = "12:34:56:78:90:ab:cd:ef";
902 struct nlmsghdr *nlh;
903 struct inet_diag_req_v2 *req;
904 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
905 long rc;
906
907 nlh = nlh0 - sizeof(*req);
908 /* beware of unaligned access to nlh members */
909 SET_STRUCT(struct nlmsghdr, nlh,
910 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
911 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
912 .nlmsg_flags = NLM_F_REQUEST
913 );
914
915 req = NLMSG_DATA(nlh);
916 *req = (struct inet_diag_req_v2) {
917 .sdiag_family = AF_INET,
918 .idiag_ext = 1 << (INET_DIAG_CONG - 1),
919 .sdiag_protocol = IPPROTO_TCP,
920 .idiag_states = 1 << TCP_CLOSE,
921 .id = {
922 .idiag_sport = 0xfacd,
923 .idiag_dport = 0xdead,
924 .idiag_if = 0xadcdfafc,
925 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
926 },
927 };
928
929 if (!inet_pton(AF_INET, address, &req->id.idiag_src))
930 perror_msg_and_skip("sendto");
931 if (!inet_pton(AF_INET, address, &req->id.idiag_dst))
932 perror_msg_and_skip("sendto");
933
934 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
935 NULL, 0);
936
937 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
938 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
939 ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
940 ", idiag_ext=1<<(INET_DIAG_CONG-1)"
941 ", idiag_states=1<<TCP_CLOSE, id={idiag_sport=htons(%u)"
942 ", idiag_dport=htons(%u), inet_pton(AF_INET, \"%s\", &idiag_src)"
943 ", inet_pton(AF_INET, \"%s\", &idiag_dst), idiag_if=%u"
944 ", idiag_cookie=[%u, %u]}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
945 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
946 ntohs(0xfacd), ntohs(0xdead), address, address,
947 0xadcdfafc, 0xdeadbeef, 0xbadc0ded,
948 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
949 sprintrc(rc));
950
951 req->sdiag_family = AF_INET6;
952 if (!inet_pton(AF_INET6, address6, &req->id.idiag_src))
953 perror_msg_and_skip("sendto");
954 if (!inet_pton(AF_INET6, address6, &req->id.idiag_dst))
955 perror_msg_and_skip("sendto");
956
957 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
958 NULL, 0);
959
960 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
961 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
962 ", {sdiag_family=AF_INET6, sdiag_protocol=IPPROTO_TCP"
963 ", idiag_ext=1<<(INET_DIAG_CONG-1)"
964 ", idiag_states=1<<TCP_CLOSE, id={idiag_sport=htons(%u)"
965 ", idiag_dport=htons(%u), inet_pton(AF_INET6, \"%s\", &idiag_src)"
966 ", inet_pton(AF_INET6, \"%s\", &idiag_dst), idiag_if=%u"
967 ", idiag_cookie=[%u, %u]}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
968 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
969 ntohs(0xfacd), ntohs(0xdead), address6, address6,
970 0xadcdfafc, 0xdeadbeef, 0xbadc0ded,
971 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
972 sprintrc(rc));
973}
974
975static void
976test_inet_diag_req(const int fd)
977{
978 const char address[] = "12.34.56.78";
979 struct nlmsghdr *nlh;
980 struct inet_diag_req *req;
981 uint8_t *family;
982 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
983 long rc;
984
985 /* family only */
986 nlh = nlh0 - sizeof(*family);
987 /* beware of unaligned access to nlh members */
988 SET_STRUCT(struct nlmsghdr, nlh,
989 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
990 .nlmsg_type = TCPDIAG_GETSOCK,
991 .nlmsg_flags = NLM_F_REQUEST
992 );
993
994 family = NLMSG_DATA(nlh);
995 *family = AF_INET;
996
997 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
998 NULL, 0);
999
1000 printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
1001 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET}}"
1002 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1003 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1004 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1005 sprintrc(rc));
1006
1007 /* family and string */
1008 nlh = nlh0 - (sizeof(*family) + 4);
1009 /* beware of unaligned access to nlh members */
1010 SET_STRUCT(struct nlmsghdr, nlh,
1011 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
1012 .nlmsg_type = TCPDIAG_GETSOCK,
1013 .nlmsg_flags = NLM_F_REQUEST
1014 );
1015
1016 family = NLMSG_DATA(nlh);
1017 *family = AF_INET;
1018 memcpy(family + 1, "1234", 4);
1019
1020 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
1021 NULL, 0);
1022
1023 printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
1024 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1025 ", {idiag_family=AF_INET, ...}}"
1026 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1027 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1028 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1029 sprintrc(rc));
1030
1031 /* inet_diag_req */
1032 nlh = nlh0 - sizeof(*req);
1033 SET_STRUCT(struct nlmsghdr, nlh,
1034 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
1035 .nlmsg_type = TCPDIAG_GETSOCK,
1036 .nlmsg_flags = NLM_F_REQUEST
1037 );
1038
1039 req = NLMSG_DATA(nlh);
1040 *req = (struct inet_diag_req) {
1041 .idiag_family = AF_INET,
1042 .idiag_ext = 1 << (INET_DIAG_TOS - 1),
1043 .idiag_src_len = 0xde,
1044 .idiag_dst_len = 0xba,
1045 .id = {
1046 .idiag_sport = 0xdead,
1047 .idiag_dport = 0xadcd,
1048 .idiag_if = 0xadcdfafc,
1049 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
1050 },
1051 .idiag_states = 1 << TCP_LAST_ACK,
1052 .idiag_dbs = 0xfacefeed,
1053 };
1054
1055 if (!inet_pton(AF_INET, address, &req->id.idiag_src))
1056 perror_msg_and_skip("sendto");
1057 if (!inet_pton(AF_INET, address, &req->id.idiag_dst))
1058 perror_msg_and_skip("sendto");
1059
1060 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1061 NULL, 0);
1062
1063 printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
1064 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1065 ", {idiag_family=AF_INET, idiag_src_len=%u"
1066 ", idiag_dst_len=%u, idiag_ext=1<<(INET_DIAG_TOS-1)"
1067 ", id={idiag_sport=htons(%u), idiag_dport=htons(%u)"
1068 ", inet_pton(AF_INET, \"%s\", &idiag_src)"
1069 ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
1070 ", idiag_if=%u, idiag_cookie=[%u, %u]}"
1071 ", idiag_states=1<<TCP_LAST_ACK, idiag_dbs=%u}}"
1072 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1073 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1074 0xde, 0xba, ntohs(0xdead), ntohs(0xadcd), address, address,
1075 0xadcdfafc, 0xdeadbeef, 0xbadc0ded, 0xfacefeed,
1076 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1077 sprintrc(rc));
1078
1079 /* short read of inet_diag_req */
1080 nlh = nlh0 - (sizeof(*req) - 1);
1081 /* beware of unaligned access to nlh members */
1082 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
1083
1084 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1085 NULL, 0);
1086
1087 printf("sendto(%d, {{len=%u, type=TCPDIAG_GETSOCK"
1088 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1089 ", {idiag_family=AF_INET, %p}}"
1090 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1091 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1092 NLMSG_DATA(nlh) + 1,
1093 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1094 sprintrc(rc));
1095}
1096
1097static void
1098test_inet_diag_req_v2(const int fd)
1099{
1100 const char address[] = "87.65.43.21";
1101 struct nlmsghdr *nlh;
1102 struct inet_diag_req_v2 *req;
1103 uint8_t *family;
1104 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
1105 long rc;
1106
1107 /* family only */
1108 nlh = nlh0 - sizeof(*family);
1109 /* beware of unaligned access to nlh members */
1110 SET_STRUCT(struct nlmsghdr, nlh,
1111 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
1112 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1113 .nlmsg_flags = NLM_F_REQUEST
1114 );
1115
1116 family = NLMSG_DATA(nlh);
1117 *family = AF_INET;
1118
1119 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
1120 NULL, 0);
1121
1122 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1123 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_INET}}"
1124 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1125 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1126 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1127 sprintrc(rc));
1128
1129 /* family and string */
1130 nlh = nlh0 - sizeof(*family) - 4;
1131 SET_STRUCT(struct nlmsghdr, nlh,
1132 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
1133 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1134 .nlmsg_flags = NLM_F_REQUEST
1135 );
1136
1137 family = NLMSG_DATA(nlh);
1138 *family = AF_INET;
1139 memcpy(family + 1, "1234", 4);
1140
1141 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
1142 NULL, 0);
1143
1144 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1145 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1146 ", {sdiag_family=AF_INET, ...}}"
1147 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1148 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1149 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1150 sprintrc(rc));
1151
1152 /* inet_diag_req_v2 */
1153 nlh = nlh0 - sizeof(*req);
1154 /* beware of unaligned access to nlh members */
1155 SET_STRUCT(struct nlmsghdr, nlh,
1156 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
1157 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1158 .nlmsg_flags = NLM_F_REQUEST
1159 );
1160
1161 req = NLMSG_DATA(nlh);
1162 *req = (struct inet_diag_req_v2) {
1163 .sdiag_family = AF_INET,
1164 .idiag_ext = 1 << (INET_DIAG_CONG - 1),
1165 .sdiag_protocol = IPPROTO_TCP,
1166 .idiag_states = 1 << TCP_CLOSE,
1167 .id = {
1168 .idiag_sport = 0xfacd,
1169 .idiag_dport = 0xdead,
1170 .idiag_if = 0xadcdfafc,
1171 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
1172 },
1173 };
1174
1175 if (!inet_pton(AF_INET, address, &req->id.idiag_src))
1176 perror_msg_and_skip("sendto");
1177 if (!inet_pton(AF_INET, address, &req->id.idiag_dst))
1178 perror_msg_and_skip("sendto");
1179
1180 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1181 NULL, 0);
1182
1183 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1184 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1185 ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
1186 ", idiag_ext=1<<(INET_DIAG_CONG-1)"
1187 ", idiag_states=1<<TCP_CLOSE, id={idiag_sport=htons(%u)"
1188 ", idiag_dport=htons(%u), inet_pton(AF_INET, \"%s\", &idiag_src)"
1189 ", inet_pton(AF_INET, \"%s\", &idiag_dst), idiag_if=%u"
1190 ", idiag_cookie=[%u, %u]}}}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1191 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1192 ntohs(0xfacd), ntohs(0xdead), address, address,
1193 0xadcdfafc, 0xdeadbeef, 0xbadc0ded,
1194 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1195 sprintrc(rc));
1196
1197 /* short read of inet_diag_req_v2 */
1198 nlh = nlh0 - (sizeof(*req) - 1);
1199 /* beware of unaligned access to nlh members */
1200 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
1201
1202 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1203 NULL, 0);
1204 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1205 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1206 ", {sdiag_family=AF_INET, %p}}"
1207 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1208 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1209 NLMSG_DATA(nlh) + 1,
1210 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1211 sprintrc(rc));
1212}
1213
1214static void
1215test_inet_diag_msg(const int fd)
1216{
1217 const char address[] = "11.22.33.44";
1218 struct nlmsghdr *nlh;
1219 struct inet_diag_msg *msg;
1220 uint8_t *family;
1221 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
1222 long rc;
1223
1224 /* family only */
1225 nlh = nlh0 - sizeof(*family);
1226 /* beware of unaligned access to nlh members */
1227 SET_STRUCT(struct nlmsghdr, nlh,
1228 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
1229 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1230 .nlmsg_flags = NLM_F_DUMP
1231 );
1232
1233 family = NLMSG_DATA(nlh);
1234 *family = AF_INET;
1235
1236 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
1237 NULL, 0);
1238
1239 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1240 ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_INET}}"
1241 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1242 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1243 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1244 sprintrc(rc));
1245
1246 /* family and string */
1247 nlh = nlh0 - sizeof(*family) - 4;
1248 /* beware of unaligned access to nlh members */
1249 SET_STRUCT(struct nlmsghdr, nlh,
1250 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
1251 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1252 .nlmsg_flags = NLM_F_DUMP
1253 );
1254
1255 family = NLMSG_DATA(nlh);
1256 *family = AF_INET;
1257 memcpy(family + 1, "1234", 4);
1258
1259 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
1260 NULL, 0);
1261
1262 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1263 ", flags=NLM_F_DUMP, seq=0, pid=0}"
1264 ", {idiag_family=AF_INET, ...}}"
1265 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1266 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1267 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1268 sprintrc(rc));
1269
1270 /* inet_diag_msg */
1271 nlh = nlh0 - sizeof(*msg);
1272 SET_STRUCT(struct nlmsghdr, nlh,
1273 .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
1274 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1275 .nlmsg_flags = NLM_F_DUMP
1276 );
1277
1278 msg = NLMSG_DATA(nlh);
1279 *msg = (struct inet_diag_msg) {
1280 .idiag_family = AF_INET,
1281 .idiag_state = TCP_LISTEN,
1282 .idiag_timer = 0xfa,
1283 .idiag_retrans = 0xde,
1284 .id = {
1285 .idiag_sport = 0xfacf,
1286 .idiag_dport = 0xdead,
1287 .idiag_if = 0xadcdfafc,
1288 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded }
1289 },
1290 .idiag_expires = 0xfacefeed,
1291 .idiag_rqueue = 0xdeadbeef,
1292 .idiag_wqueue = 0xadcdfafc,
1293 .idiag_uid = 0xdecefaeb,
1294 .idiag_inode = 0xbadc0ded,
1295 };
1296
1297 if (!inet_pton(AF_INET, address, &msg->id.idiag_src))
1298 perror_msg_and_skip("sendto");
1299 if (!inet_pton(AF_INET, address, &msg->id.idiag_dst))
1300 perror_msg_and_skip("sendto");
1301
1302 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
1303 NULL, 0);
1304
1305 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1306 ", flags=NLM_F_DUMP, seq=0, pid=0}"
1307 ", {idiag_family=AF_INET, idiag_state=TCP_LISTEN"
1308 ", idiag_timer=%u, idiag_retrans=%u"
1309 ", id={idiag_sport=htons(%u), idiag_dport=htons(%u)"
1310 ", inet_pton(AF_INET, \"%s\", &idiag_src)"
1311 ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
1312 ", idiag_if=%u, idiag_cookie=[%u, %u]}"
1313 ", idiag_expires=%u, idiag_rqueue=%u, idiag_wqueue=%u"
1314 ", idiag_uid=%u, idiag_inode=%u}}"
1315 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1316 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1317 0xfa, 0xde, ntohs(0xfacf), ntohs(0xdead),
1318 address, address, 0xadcdfafc, 0xdeadbeef, 0xbadc0ded,
1319 0xfacefeed, 0xdeadbeef, 0xadcdfafc, 0xdecefaeb, 0xbadc0ded,
1320 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1321 sprintrc(rc));
1322
1323 /* short read of inet_diag_msg */
1324 nlh = nlh0 - (sizeof(*msg) - 1);
1325 /* beware of unaligned access to nlh members */
1326 memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*msg) - 1);
1327
1328 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
1329 NULL, 0);
1330 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1331 ", flags=NLM_F_DUMP, seq=0, pid=0}"
1332 ", {idiag_family=AF_INET, %p}}"
1333 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1334 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1335 NLMSG_DATA(nlh) + 1,
1336 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1337 sprintrc(rc));
1338}
1339
1340#ifdef AF_SMC
1341static void
1342test_smc_diag_req(const int fd)
1343{
1344 const char address[] = "43.21.56.78";
1345 struct nlmsghdr *nlh;
1346 struct smc_diag_req *req;
1347 uint8_t *family;
1348 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
1349 long rc;
1350
1351 /* family only */
1352 nlh = nlh0 - sizeof(*family);
1353 /* beware of unaligned access to nlh members */
1354 SET_STRUCT(struct nlmsghdr, nlh,
1355 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
1356 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1357 .nlmsg_flags = NLM_F_REQUEST
1358 );
1359
1360 family = NLMSG_DATA(nlh);
1361 *family = AF_SMC;
1362
1363 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
1364 NULL, 0);
1365
1366 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1367 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {family=AF_SMC}}"
1368 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1369 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1370 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1371 sprintrc(rc));
1372
1373 /* family and string */
1374 nlh = nlh0 - sizeof(*family) - 4;
1375 /* beware of unaligned access to nlh members */
1376 SET_STRUCT(struct nlmsghdr, nlh,
1377 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
1378 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1379 .nlmsg_flags = NLM_F_REQUEST
1380 );
1381
1382 family = NLMSG_DATA(nlh);
1383 *family = AF_SMC;
1384 memcpy(family + 1, "1234", 4);
1385
1386 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
1387 NULL, 0);
1388
1389 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1390 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1391 ", {diag_family=AF_SMC, ...}}"
1392 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1393 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1394 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1395 sprintrc(rc));
1396
1397 /* smc_diag_req */
1398 nlh = nlh0 - sizeof(*req);
1399 SET_STRUCT(struct nlmsghdr, nlh,
1400 .nlmsg_len = NLMSG_HDRLEN + sizeof(*req),
1401 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1402 .nlmsg_flags = NLM_F_REQUEST
1403 );
1404
1405 req = NLMSG_DATA(nlh);
1406 *req = (struct smc_diag_req) {
1407 .diag_family = AF_SMC,
1408 .diag_ext = 1 << (SMC_DIAG_CONNINFO - 1),
1409 .id = {
1410 .idiag_sport = 0xdead,
1411 .idiag_dport = 0xadcd,
1412 .idiag_if = 0xadcdfafc,
1413 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded },
1414 },
1415 };
1416
1417 if (!inet_pton(AF_INET, address, &req->id.idiag_src))
1418 perror_msg_and_skip("sendto");
1419 if (!inet_pton(AF_INET, address, &req->id.idiag_dst))
1420 perror_msg_and_skip("sendto");
1421
1422 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1423 NULL, 0);
1424
1425 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1426 ", flags=NLM_F_REQUEST, seq=0, pid=0}, {diag_family=AF_SMC"
1427 ", diag_ext=1<<(SMC_DIAG_CONNINFO-1)"
1428 ", id={idiag_sport=htons(%u), idiag_dport=htons(%u)"
1429 ", inet_pton(AF_INET, \"%s\", &idiag_src)"
1430 ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
1431 ", idiag_if=%u, idiag_cookie=[%u, %u]}}}"
1432 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1433 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1434 htons(0xdead), htons(0xadcd), address, address,
1435 0xadcdfafc, 0xdeadbeef, 0xbadc0ded,
1436 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1437 sprintrc(rc));
1438
1439 /* short read of smc_diag_req */
1440 nlh = nlh0 - (sizeof(*req) - 1);
1441 /* beware of unaligned access to nlh members */
1442 memmove(nlh, nlh0 - sizeof(*req), NLMSG_HDRLEN + sizeof(*req) - 1);
1443
1444 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*req), MSG_DONTWAIT,
1445 NULL, 0);
1446 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1447 ", flags=NLM_F_REQUEST, seq=0, pid=0}"
1448 ", {diag_family=AF_SMC, %p}}"
1449 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1450 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1451 NLMSG_DATA(nlh) + 1,
1452 NLMSG_HDRLEN + (unsigned int) sizeof(*req),
1453 sprintrc(rc));
1454}
1455
1456static void
1457test_smc_diag_msg(const int fd)
1458{
1459 const char address[] = "34.87.12.90";
1460 struct nlmsghdr *nlh;
1461 struct smc_diag_msg *msg;
1462 uint8_t *family;
1463 void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
1464 long rc;
1465
1466 /* family only */
1467 nlh = nlh0 - sizeof(*family);
1468 /* beware of unaligned access to nlh members */
1469 SET_STRUCT(struct nlmsghdr, nlh,
1470 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family),
1471 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1472 .nlmsg_flags = NLM_F_DUMP
1473 );
1474
1475 family = NLMSG_DATA(nlh);
1476 *family = AF_SMC;
1477
1478 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family), MSG_DONTWAIT,
1479 NULL, 0);
1480
1481 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1482 ", flags=NLM_F_DUMP, seq=0, pid=0}, {family=AF_SMC}}"
1483 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1484 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1485 NLMSG_HDRLEN + (unsigned int) sizeof(*family),
1486 sprintrc(rc));
1487
1488 /* family and string */
1489 nlh = nlh0 - sizeof(*family) - 4;
1490 /* beware of unaligned access to nlh members */
1491 SET_STRUCT(struct nlmsghdr, nlh,
1492 .nlmsg_len = NLMSG_HDRLEN + sizeof(*family) + 4,
1493 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1494 .nlmsg_flags = NLM_F_DUMP
1495 );
1496
1497 family = NLMSG_DATA(nlh);
1498 *family = AF_SMC;
1499 memcpy(family + 1, "1234", 4);
1500
1501 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*family) + 4, MSG_DONTWAIT,
1502 NULL, 0);
1503
1504 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1505 ", flags=NLM_F_DUMP, seq=0, pid=0}"
1506 ", {diag_family=AF_SMC, ...}}"
1507 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1508 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1509 NLMSG_HDRLEN + (unsigned int) sizeof(*family) + 4,
1510 sprintrc(rc));
1511
1512 /* smc_diag_msg */
1513 nlh = nlh0 - sizeof(*msg);
1514 SET_STRUCT(struct nlmsghdr, nlh,
1515 .nlmsg_len = NLMSG_HDRLEN + sizeof(*msg),
1516 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
1517 .nlmsg_flags = NLM_F_DUMP
1518 );
1519
1520 msg = NLMSG_DATA(nlh);
1521 *msg = (struct smc_diag_msg) {
1522 .diag_family = AF_SMC,
1523 .diag_state = SMC_ACTIVE,
1524 .diag_fallback = 0xde,
1525 .diag_shutdown = 0xba,
1526 .id = {
1527 .idiag_sport = 0xdead,
1528 .idiag_dport = 0xadcd,
1529 .idiag_if = 0xadcdfafc,
1530 .idiag_cookie = { 0xdeadbeef, 0xbadc0ded },
1531 },
1532 .diag_uid = 0xadcdfafc,
1533 .diag_inode = 0xbadc0ded,
1534 };
1535
1536 if (!inet_pton(AF_INET, address, &msg->id.idiag_src))
1537 perror_msg_and_skip("sendto");
1538 if (!inet_pton(AF_INET, address, &msg->id.idiag_dst))
1539 perror_msg_and_skip("sendto");
1540
1541 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
1542 NULL, 0);
1543
1544 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1545 ", flags=NLM_F_DUMP, seq=0, pid=0}, {diag_family=AF_SMC"
1546 ", diag_state=SMC_ACTIVE, diag_fallback=%u, diag_shutdown=%u"
1547 ", id={idiag_sport=htons(%u), idiag_dport=htons(%u)"
1548 ", inet_pton(AF_INET, \"%s\", &idiag_src)"
1549 ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
1550 ", idiag_if=%u, idiag_cookie=[%u, %u]}"
1551 ", diag_uid=%u, diag_inode=%u}}, %u"
1552 ", MSG_DONTWAIT, NULL, 0) = %s\n",
1553 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1554 0xde, 0xba, htons(0xdead), htons(0xadcd), address, address,
1555 0xadcdfafc, 0xdeadbeef, 0xbadc0ded, 0xadcdfafc, 0xbadc0ded,
1556 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1557 sprintrc(rc));
1558
1559 /* short read of smc_diag_msg */
1560 nlh = nlh0 - (sizeof(*msg) - 1);
1561 /* beware of unaligned access to nlh members */
1562 memmove(nlh, nlh0 - sizeof(*msg), NLMSG_HDRLEN + sizeof(*msg) - 1);
1563
1564 rc = sendto(fd, nlh, NLMSG_HDRLEN + sizeof(*msg), MSG_DONTWAIT,
1565 NULL, 0);
1566 printf("sendto(%d, {{len=%u, type=SOCK_DIAG_BY_FAMILY"
1567 ", flags=NLM_F_DUMP, seq=0, pid=0}"
1568 ", {diag_family=AF_SMC, %p}}"
1569 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
1570 fd, NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1571 NLMSG_DATA(nlh) + 1,
1572 NLMSG_HDRLEN + (unsigned int) sizeof(*msg),
1573 sprintrc(rc));
1574}
1575#endif
1576
1577int
1578main(void)
1579{
1580 skip_if_unavailable("/proc/self/fd/");
1581
1582 int fd = create_nl_socket(NETLINK_SOCK_DIAG);
1583
1584 test_nlmsg_type(fd);
1585 test_nlmsg_flags(fd);
1586 test_odd_family_req(fd);
1587 test_odd_family_msg(fd);
1588 test_unix_diag_req(fd);
1589 test_unix_diag_msg(fd);
1590 test_netlink_diag_req(fd);
1591 test_netlink_diag_msg(fd);
1592 test_packet_diag_req(fd);
1593 test_packet_diag_msg(fd);
1594 test_inet_diag_sockid(fd);
1595 test_inet_diag_req(fd);
1596 test_inet_diag_req_v2(fd);
1597 test_inet_diag_msg(fd);
1598#ifdef AF_SMC
1599 test_smc_diag_req(fd);
1600 test_smc_diag_msg(fd);
1601#endif
1602
1603 printf("+++ exited with 0 +++\n");
1604
1605 return 0;
1606}