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/nfnetlink_cttimeout.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 =
26 NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_NEW;
27 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
28 printf("sendto(%d, {nlmsg_len=%u"
29 ", nlmsg_type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8"
30 "|IPCTNL_MSG_TIMEOUT_NEW"
31 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
32 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
33 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
34
35 nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | 0xff;
36 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
37 printf("sendto(%d, {nlmsg_len=%u"
38 ", nlmsg_type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8|0xff"
39 " /* IPCTNL_MSG_TIMEOUT_??? */"
40 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
41 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
42 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
43 }
44
45 static void
46 test_nlmsg_flags(const int fd)
47 {
48 long rc;
49 struct nlmsghdr nlh = {
50 .nlmsg_len = sizeof(nlh),
51 };
52
53 nlh.nlmsg_type =
54 NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_NEW;
55 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_APPEND;
56 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
57 printf("sendto(%d, {nlmsg_len=%u"
58 ", nlmsg_type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8"
59 "|IPCTNL_MSG_TIMEOUT_NEW"
60 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_APPEND, nlmsg_seq=0"
61 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
62 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
63
64 nlh.nlmsg_type =
65 NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_GET;
66 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ATOMIC;
67 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
68 printf("sendto(%d, {nlmsg_len=%u"
69 ", nlmsg_type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8"
70 "|IPCTNL_MSG_TIMEOUT_GET"
71 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_ATOMIC, nlmsg_seq=0"
72 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
73 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
74
75 nlh.nlmsg_type =
76 NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8 | IPCTNL_MSG_TIMEOUT_DELETE;
77 nlh.nlmsg_flags = NLM_F_REQUEST | NLM_F_NONREC;
78 rc = sendto(fd, &nlh, nlh.nlmsg_len, MSG_DONTWAIT, NULL, 0);
79 printf("sendto(%d, {nlmsg_len=%u"
80 ", nlmsg_type=NFNL_SUBSYS_CTNETLINK_TIMEOUT<<8"
81 "|IPCTNL_MSG_TIMEOUT_DELETE"
82 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_NONREC, nlmsg_seq=0"
83 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
84 fd, nlh.nlmsg_len, nlh.nlmsg_len, sprintrc(rc));
85 }
86
87 int
88 main(void)
89 {
90 skip_if_unavailable("/proc/self/fd/");
91
92 int fd = create_nl_socket(NETLINK_NETFILTER);
93
94 test_nlmsg_type(fd);
95 test_nlmsg_flags(fd);
96
97 puts("+++ exited with 0 +++");
98
99 return 0;
100 }