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.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_NFTABLES << 8 | NFT_MSG_NEWTABLE;
26 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
27 printf("sendto(%d, {nlmsg_len=%u"
28 ", nlmsg_type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE"
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_NFTABLES << 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_NFTABLES<<8|0xff /* NFT_MSG_??? */"
37 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
38 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
39 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
40 }
41
42 static void
43 test_nlmsg_flags(const int fd)
44 {
45 long rc;
46 struct nlmsghdr nlh = {
47 .nlmsg_len = sizeof(nlh),
48 };
49
50 nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE;
51 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE;
52 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
53 printf("sendto(%d, {nlmsg_len=%u"
54 ", nlmsg_type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE"
55 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_REPLACE, nlmsg_seq=0"
56 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
57 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
58
59 nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_GETTABLE;
60 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
61 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
62 printf("sendto(%d, {nlmsg_len=%u"
63 ", nlmsg_type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_GETTABLE"
64 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_DUMP, nlmsg_seq=0"
65 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
66 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
67
68 nlh.nlmsg_type = NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_DELTABLE;
69 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_NONREC;
70 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
71 printf("sendto(%d, {nlmsg_len=%u"
72 ", nlmsg_type=NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_DELTABLE"
73 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_NONREC, nlmsg_seq=0"
74 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
75 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
76 }
77
78 int
79 main(void)
80 {
81 skip_if_unavailable("/proc/self/fd/");
82
83 int fd = create_nl_socket(NETLINK_NETFILTER);
84
85 test_nlmsg_type(fd);
86 test_nlmsg_flags(fd);
87
88 puts("+++ exited with 0 +++");
89
90 return 0;
91 }