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 <string.h>
12 #include <stdint.h>
13 #include <unistd.h>
14 #include <sys/socket.h>
15 #include "netlink.h"
16 #include <linux/xfrm.h>
17
18 static void
19 test_nlmsg_type(const int fd)
20 {
21 long rc;
22 struct nlmsghdr nlh = {
23 .nlmsg_len = sizeof(nlh),
24 .nlmsg_type = XFRM_MSG_NEWSA,
25 .nlmsg_flags = NLM_F_REQUEST,
26 };
27
28 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
29 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=XFRM_MSG_NEWSA"
30 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}"
31 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
32 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
33 }
34
35 static void
36 test_nlmsg_flags(const int fd)
37 {
38 long rc;
39 struct nlmsghdr nlh = {
40 .nlmsg_len = sizeof(nlh),
41 };
42
43 nlh.nlmsg_type = XFRM_MSG_GETSA;
44 nlh.nlmsg_flags = NLM_F_DUMP;
45 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
46 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=XFRM_MSG_GETSA"
47 ", nlmsg_flags=NLM_F_DUMP, nlmsg_seq=0, nlmsg_pid=0}"
48 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
49 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
50
51 nlh.nlmsg_type = XFRM_MSG_NEWSA;
52 nlh.nlmsg_flags = NLM_F_REPLACE;
53 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
54 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=XFRM_MSG_NEWSA"
55 ", nlmsg_flags=NLM_F_REPLACE, nlmsg_seq=0, nlmsg_pid=0}"
56 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
57 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
58
59 nlh.nlmsg_type = XFRM_MSG_DELSA;
60 nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_NONREC;
61 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
62 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=XFRM_MSG_DELSA"
63 ", nlmsg_flags=NLM_F_ECHO|NLM_F_NONREC, nlmsg_seq=0"
64 ", nlmsg_pid=0}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
65 fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
66
67 nlh.nlmsg_type = XFRM_MSG_ALLOCSPI;
68 nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_REPLACE;
69 rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
70 printf("sendto(%d, {nlmsg_len=%u, nlmsg_type=XFRM_MSG_ALLOCSPI"
71 ", nlmsg_flags=NLM_F_ECHO|%#x, nlmsg_seq=0, nlmsg_pid=0}"
72 ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
73 fd, nlh.nlmsg_len, NLM_F_REPLACE,
74 (unsigned) sizeof(nlh), sprintrc(rc));
75 }
76
77 int main(void)
78 {
79 skip_if_unavailable("/proc/self/fd/");
80
81 int fd = create_nl_socket(NETLINK_XFRM);
82
83 test_nlmsg_type(fd);
84 test_nlmsg_flags(fd);
85
86 printf("+++ exited with 0 +++\n");
87
88 return 0;
89 }