4 * Copyright 2016 6WIND S.A.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of 6WIND S.A. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/socket.h>
43 #include "mlx5_utils.h"
46 * Initialise the socket to communicate with the secondary process
49 * Pointer to private structure.
52 * 0 on success, errno value on failure.
55 priv_socket_init(struct priv *priv)
57 struct sockaddr_un sun = {
58 .sun_family = AF_UNIX,
62 struct stat file_stat;
65 * Initialise the socket to communicate with the secondary
68 ret = socket(AF_UNIX, SOCK_STREAM, 0);
70 WARN("secondary process not supported: %s", strerror(errno));
73 priv->primary_socket = ret;
74 flags = fcntl(priv->primary_socket, F_GETFL, 0);
77 ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
80 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
81 MLX5_DRIVER_NAME, priv->primary_socket);
82 ret = stat(sun.sun_path, &file_stat);
84 claim_zero(remove(sun.sun_path));
85 ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
88 WARN("cannot bind socket, secondary process not supported: %s",
92 ret = listen(priv->primary_socket, 0);
94 WARN("Secondary process not supported: %s", strerror(errno));
101 claim_zero(close(priv->primary_socket));
102 priv->primary_socket = 0;
107 * Un-Initialise the socket to communicate with the secondary process
110 * Pointer to private structure.
113 * 0 on success, errno value on failure.
116 priv_socket_uninit(struct priv *priv)
118 MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
119 claim_zero(close(priv->primary_socket));
120 priv->primary_socket = 0;
121 claim_zero(remove(path));
126 * Handle socket interrupts.
129 * Pointer to private structure.
132 priv_socket_handle(struct priv *priv)
136 struct cmsghdr *cmsg = NULL;
137 struct ucred *cred = NULL;
138 char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
139 char vbuf[1024] = { 0 };
142 .iov_len = sizeof(*vbuf),
144 struct msghdr msg = {
148 .msg_controllen = sizeof(buf),
152 /* Accept the connection from the client. */
153 conn_sock = accept(priv->primary_socket, NULL, NULL);
155 WARN("connection failed: %s", strerror(errno));
158 ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
161 WARN("cannot change socket options");
164 ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
166 WARN("received an empty message: %s", strerror(errno));
169 /* Expect to receive credentials only. */
170 cmsg = CMSG_FIRSTHDR(&msg);
175 if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
176 (cmsg->cmsg_len >= sizeof(*cred))) {
177 cred = (struct ucred *)CMSG_DATA(cmsg);
178 assert(cred != NULL);
180 cmsg = CMSG_NXTHDR(&msg, cmsg);
182 WARN("Message wrongly formatted");
185 /* Make sure all the ancillary data was received and valid. */
186 if ((cred == NULL) || (cred->uid != getuid()) ||
187 (cred->gid != getgid())) {
188 WARN("wrong credentials");
191 /* Set-up the ancillary data. */
192 cmsg = CMSG_FIRSTHDR(&msg);
193 assert(cmsg != NULL);
194 cmsg->cmsg_level = SOL_SOCKET;
195 cmsg->cmsg_type = SCM_RIGHTS;
196 cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
197 fd = (int *)CMSG_DATA(cmsg);
198 *fd = priv->ctx->cmd_fd;
199 ret = sendmsg(conn_sock, &msg, 0);
201 WARN("cannot send response");
207 * Connect to the primary process.
210 * Pointer to private structure.
213 * fd on success, negative errno value on failure.
216 priv_socket_connect(struct priv *priv)
218 struct sockaddr_un sun = {
219 .sun_family = AF_UNIX,
225 char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
226 char vbuf[1024] = { 0 };
229 .iov_len = sizeof(*vbuf),
231 struct msghdr msg = {
233 .msg_controllen = sizeof(buf),
237 struct cmsghdr *cmsg;
239 ret = socket(AF_UNIX, SOCK_STREAM, 0);
241 WARN("cannot connect to primary");
245 snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
246 MLX5_DRIVER_NAME, priv->primary_socket);
247 ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
249 WARN("cannot connect to primary");
252 cmsg = CMSG_FIRSTHDR(&msg);
254 DEBUG("cannot get first message");
257 cmsg->cmsg_level = SOL_SOCKET;
258 cmsg->cmsg_type = SCM_CREDENTIALS;
259 cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
260 cred = (struct ucred *)CMSG_DATA(cmsg);
262 DEBUG("no credentials received");
265 cred->pid = getpid();
266 cred->uid = getuid();
267 cred->gid = getgid();
268 ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
270 WARN("cannot send credentials to primary: %s",
274 ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
276 WARN("no message from primary: %s", strerror(errno));
279 cmsg = CMSG_FIRSTHDR(&msg);
281 WARN("No file descriptor received");
284 fd = (int *)CMSG_DATA(cmsg);
286 WARN("no file descriptor received: %s", strerror(errno));