1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright(c) 2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6
7 /*
8 * Test Name: recvmsg03
9 *
10 * This test needs that rds socket is supported by system.
11 * If the size of address for receiving data is set larger than
12 * actaul size, recvmsg() will set msg_namelen incorrectly.
13 *
14 * Description:
15 * This is a regression test and has been fixed by kernel commit:
16 * 06b6a1cf6e776426766298d055bb3991957d90a7 (rds: set correct msg_namelen)
17 */
18
19 #include <errno.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include "tst_safe_net.h"
24
25 #include "tst_test.h"
26
27 #ifndef AF_RDS
28 # define AF_RDS 21
29 #endif
30
setup(void)31 static void setup(void)
32 {
33 int res;
34
35 res = socket(AF_RDS, SOCK_SEQPACKET, 0);
36 if (res == -1) {
37 if (errno == EAFNOSUPPORT)
38 tst_brk(TCONF, "rds was not supported");
39 else
40 tst_brk(TBROK | TERRNO, "socket() failed with rds");
41 }
42
43 SAFE_CLOSE(res);
44 }
45
client(void)46 static void client(void)
47 {
48 int sock_fd1;
49 char send_buf[128] = "hello world";
50 struct sockaddr_in server_addr;
51 struct sockaddr_in to_addr;
52 struct msghdr msg;
53 struct iovec iov;
54
55 sock_fd1 = SAFE_SOCKET(AF_RDS, SOCK_SEQPACKET, 0);
56
57 memset(&server_addr, 0, sizeof(server_addr));
58 server_addr.sin_family = AF_INET;
59 server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
60 server_addr.sin_port = htons(4001);
61
62 SAFE_BIND(sock_fd1, (struct sockaddr *) &server_addr, sizeof(server_addr));
63
64 memset(&to_addr, 0, sizeof(to_addr));
65
66 to_addr.sin_family = AF_INET;
67 to_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
68 to_addr.sin_port = htons(4000);
69 msg.msg_name = &to_addr;
70 msg.msg_namelen = sizeof(to_addr);
71 msg.msg_iov = &iov;
72 msg.msg_iovlen = 1;
73 msg.msg_iov->iov_base = send_buf;
74 msg.msg_iov->iov_len = strlen(send_buf) + 1;
75 msg.msg_control = 0;
76 msg.msg_controllen = 0;
77 msg.msg_flags = 0;
78
79 if (sendmsg(sock_fd1, &msg, 0) == -1) {
80 tst_brk(TBROK | TERRNO,
81 "sendmsg() failed to send data to server");
82 }
83
84 TST_CHECKPOINT_WAIT(1);
85
86 SAFE_CLOSE(sock_fd1);
87 }
88
server(void)89 static void server(void)
90 {
91 int sock_fd2;
92 static char recv_buf[128];
93 struct sockaddr_in server_addr;
94 struct sockaddr_in from_addr;
95 struct msghdr msg;
96 struct iovec iov;
97
98 sock_fd2 = SAFE_SOCKET(AF_RDS, SOCK_SEQPACKET, 0);
99
100 memset(&server_addr, 0, sizeof(server_addr));
101 server_addr.sin_family = AF_INET;
102 server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
103 server_addr.sin_port = htons(4000);
104
105 SAFE_BIND(sock_fd2, (struct sockaddr *) &server_addr, sizeof(server_addr));
106
107 msg.msg_name = &from_addr;
108 msg.msg_namelen = sizeof(from_addr) + 16;
109 msg.msg_iov = &iov;
110 msg.msg_iovlen = 1;
111 msg.msg_iov->iov_base = recv_buf;
112 msg.msg_iov->iov_len = 128;
113 msg.msg_control = 0;
114 msg.msg_controllen = 0;
115 msg.msg_flags = 0;
116
117 TST_CHECKPOINT_WAKE(0);
118
119 SAFE_RECVMSG(0, sock_fd2, &msg, 0);
120 if (msg.msg_namelen != sizeof(from_addr)) {
121 tst_res(TFAIL, "msg_namelen was set to %u incorrectly, "
122 "expected %lu", msg.msg_namelen, sizeof(from_addr));
123 } else {
124 tst_res(TPASS, "msg_namelen was set to %u correctly",
125 msg.msg_namelen);
126 }
127
128 TST_CHECKPOINT_WAKE(1);
129
130 SAFE_CLOSE(sock_fd2);
131 }
132
verify_recvmsg(void)133 static void verify_recvmsg(void)
134 {
135 pid_t pid;
136
137 pid = SAFE_FORK();
138 if (pid == 0) {
139 TST_CHECKPOINT_WAIT(0);
140 client();
141 } else {
142 server();
143 tst_reap_children();
144 }
145 }
146
147 static struct tst_test test = {
148 .forks_child = 1,
149 .needs_checkpoints = 1,
150 .setup = setup,
151 .test_all = verify_recvmsg,
152 .tags = (const struct tst_tag[]) {
153 {"linux-git", "06b6a1cf6e77"},
154 {}
155 }
156 };
157