1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include <stdio.h>
11 #include <sys/socket.h>
12 #include "netlink.h"
13 #include <linux/netfilter/nfnetlink.h>
14 #include <linux/netfilter/nf_tables_compat.h>
15
16 static void
17 test_nlmsg_type(const int fd)
18 {
19 long rc;
20 struct nlmsghdr nlh = {
21 .nlmsg_len = sizeof(nlh),
22 .nlmsg_flags = NLM_F_REQUEST,
23 };
24
25 nlh.nlmsg_type = NFNL_SUBSYS_NFT_COMPAT << 8 | NFNL_MSG_COMPAT_GET;
26 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
27 printf("sendto(%d, {nlmsg_len=%u"
28 ", nlmsg_type=NFNL_SUBSYS_NFT_COMPAT<<8|NFNL_MSG_COMPAT_GET"
29 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
30 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
31 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
32
33 nlh.nlmsg_type = NFNL_SUBSYS_NFT_COMPAT << 8 | 0xff;
34 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
35 printf("sendto(%d, {nlmsg_len=%u"
36 ", nlmsg_type=NFNL_SUBSYS_NFT_COMPAT<<8|0xff"
37 " /* NFNL_MSG_COMPAT_??? */"
38 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
39 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
40 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
41 }
42
43 static void
44 test_nlmsg_flags(const int fd)
45 {
46 long rc;
47 struct nlmsghdr nlh = {
48 .nlmsg_len = sizeof(nlh),
49 };
50
51 nlh.nlmsg_type = NFNL_SUBSYS_NFT_COMPAT << 8 | NFNL_MSG_COMPAT_GET;
52 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
53 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
54 printf("sendto(%d, {nlmsg_len=%u"
55 ", nlmsg_type=NFNL_SUBSYS_NFT_COMPAT<<8|NFNL_MSG_COMPAT_GET"
56 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_DUMP, nlmsg_seq=0"
57 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
58 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
59 }
60
61 int
62 main(void)
63 {
64 skip_if_unavailable("/proc/self/fd/");
65
66 int fd = create_nl_socket(NETLINK_NETFILTER);
67
68 test_nlmsg_type(fd);
69 test_nlmsg_flags(fd);
70
71 puts("+++ exited with 0 +++");
72
73 return 0;
74 }