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