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