blob: d25ab5a4248d38f8eecd885d847bd56cc40813c9 [file] [log] [blame]
William Lallemand2fe7dd02018-09-11 16:51:29 +02001/*
2 * Socket Pair protocol layer (sockpair)
3 *
4 * Copyright HAProxy Technologies - William Lallemand <wlallemand@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <pwd.h>
17#include <grp.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <syslog.h>
22#include <time.h>
23
24#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <sys/un.h>
28
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020029#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020030#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/fd.h>
33#include <haproxy/freq_ctr.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020034#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020035#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020036#include <haproxy/listener.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/protocol.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020041
William Lallemand2fe7dd02018-09-11 16:51:29 +020042
43static void sockpair_add_listener(struct listener *listener, int port);
44static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020045static int sockpair_connect_server(struct connection *conn, int flags);
William Lallemand2fe7dd02018-09-11 16:51:29 +020046
47/* Note: must not be declared <const> as its list will be overwritten */
48static struct protocol proto_sockpair = {
49 .name = "sockpair",
50 .sock_domain = AF_CUST_SOCKPAIR,
51 .sock_type = SOCK_STREAM,
52 .sock_prot = 0,
53 .sock_family = AF_UNIX,
54 .sock_addrlen = sizeof(struct sockaddr_un),
55 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
56 .accept = &listener_accept,
57 .connect = &sockpair_connect_server,
58 .bind = sockpair_bind_listener,
William Lallemand2fe7dd02018-09-11 16:51:29 +020059 .enable_all = enable_all_listeners,
60 .disable_all = disable_all_listeners,
61 .get_src = NULL,
62 .get_dst = NULL,
63 .pause = NULL,
64 .add = sockpair_add_listener,
65 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
66 .nb_listeners = 0,
67};
68
Willy Tarreau0108d902018-11-25 19:14:37 +010069INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
70
William Lallemand2fe7dd02018-09-11 16:51:29 +020071/* Add <listener> to the list of sockpair listeners (port is ignored). The
72 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
73 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020074 *
75 * Must be called with proto_lock held.
76 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020077 */
78static void sockpair_add_listener(struct listener *listener, int port)
79{
80 if (listener->state != LI_INIT)
81 return;
82 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +020083 listener->rx.proto = &proto_sockpair;
84 LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list);
William Lallemand2fe7dd02018-09-11 16:51:29 +020085 proto_sockpair.nb_listeners++;
86}
87
William Lallemand2fe7dd02018-09-11 16:51:29 +020088/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
89 * enabled for polling. The return value is composed from ERR_NONE,
90 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
91 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
92 * Note that <errmsg> may be NULL if <errlen> is also zero.
93 */
94static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
95{
Willy Tarreau38ba6472020-08-27 08:16:52 +020096 int fd = listener->rx.fd;
William Lallemand2fe7dd02018-09-11 16:51:29 +020097 int err;
98 const char *msg = NULL;
99
100 err = ERR_NONE;
101
102 /* ensure we never return garbage */
103 if (errlen)
104 *errmsg = 0;
105
106 if (listener->state != LI_ASSIGNED)
107 return ERR_NONE; /* already bound */
108
Willy Tarreau0b915012020-09-01 10:47:07 +0200109 if (listener->rx.flags & RX_F_BOUND)
110 goto bound;
111
Willy Tarreau38ba6472020-08-27 08:16:52 +0200112 if (listener->rx.fd == -1) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200113 err |= ERR_FATAL | ERR_ALERT;
114 msg = "sockpair can be only used with inherited FDs";
115 goto err_return;
116 }
117
118 if (fd >= global.maxsock) {
119 err |= ERR_FATAL | ERR_ALERT;
120 msg = "socket(): not enough free sockets, raise -n argument";
121 goto err_return;
122 }
123 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
124 err |= ERR_FATAL | ERR_ALERT;
125 msg = "cannot make sockpair non-blocking";
126 goto err_return;
127 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200128 listener->rx.flags |= RX_F_BOUND;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200129
Willy Tarreau0b915012020-09-01 10:47:07 +0200130 bound:
William Lallemand2fe7dd02018-09-11 16:51:29 +0200131 listener->state = LI_LISTEN;
132
Willy Tarreaub7436612020-08-28 19:51:44 +0200133 fd_insert(fd, listener, listener->rx.proto->accept,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200134 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200135
136 return err;
137
138 err_return:
139 if (msg && errlen)
140 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
141 return err;
142}
143
144/*
145 * Send FD over a unix socket
146 *
147 * <send_fd> is the FD to send
148 * <fd> is the fd of the unix socket to use for the transfer
149 *
150 * The iobuf variable could be use in the future to enhance the protocol.
151 */
152int send_fd_uxst(int fd, int send_fd)
153{
154 char iobuf[2];
155 struct iovec iov;
156 struct msghdr msghdr;
157
158 char cmsgbuf[CMSG_SPACE(sizeof(int))];
159 char buf[CMSG_SPACE(sizeof(int))];
160 struct cmsghdr *cmsg = (void *)buf;
161
162 int *fdptr;
163
164 iov.iov_base = iobuf;
165 iov.iov_len = sizeof(iobuf);
166
167 memset(&msghdr, 0, sizeof(msghdr));
168 msghdr.msg_iov = &iov;
169 msghdr.msg_iovlen = 1;
170
171 /* Now send the fds */
172 msghdr.msg_control = cmsgbuf;
173 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
174
175 cmsg = CMSG_FIRSTHDR(&msghdr);
176 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
177 cmsg->cmsg_level = SOL_SOCKET;
178 cmsg->cmsg_type = SCM_RIGHTS;
179
180 fdptr = (int *)CMSG_DATA(cmsg);
181 memcpy(fdptr, &send_fd, sizeof(send_fd));
182
183 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
184 ha_warning("Failed to transfer socket\n");
185 return 1;
186 }
187
188 return 0;
189}
190
191/*
192 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800193 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200194 * socket and establishing a connection, it creates a pair of connected
195 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200196 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200197 *
198 * conn->target may point either to a valid server or to a backend, depending
199 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
200 * <data> parameter is a boolean indicating whether there are data waiting for
201 * being sent or not, in order to adjust data write polling and on some
202 * platforms. The <delack> argument is ignored.
203 *
204 * Note that a pending send_proxy message accounts for data.
205 *
206 * It can return one of :
207 * - SF_ERR_NONE if everything's OK
208 * - SF_ERR_SRVTO if there are no more servers
209 * - SF_ERR_SRVCL if the connection was refused by the server
210 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
211 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
212 * - SF_ERR_INTERNAL for any other purely internal errors
213 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
214 *
215 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
216 * it's invalid and the caller has nothing to do.
217 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200218static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200219{
220 int sv[2], fd, dst_fd = -1;
221
222 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200223 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200224
William Lallemand2fe7dd02018-09-11 16:51:29 +0200225 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
226 obj_type(conn->target) != OBJ_TYPE_SERVER) {
227 conn->flags |= CO_FL_ERROR;
228 return SF_ERR_INTERNAL;
229 }
230
231 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
232 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
233 conn->flags |= CO_FL_ERROR;
234 return SF_ERR_RESOURCE;
235 }
236
237 fd = conn->handle.fd = sv[1];
238
239 if (fd >= global.maxsock) {
240 /* do not log anything there, it's a normal condition when this option
241 * is used to serialize connections to a server !
242 */
243 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
244 close(sv[0]);
245 close(sv[1]);
246 conn->err_code = CO_ER_CONF_FDLIM;
247 conn->flags |= CO_FL_ERROR;
248 return SF_ERR_PRXCOND; /* it is a configuration limit */
249 }
250
251 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
252 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
253 close(sv[0]);
254 close(sv[1]);
255 conn->err_code = CO_ER_SOCK_ERR;
256 conn->flags |= CO_FL_ERROR;
257 return SF_ERR_INTERNAL;
258 }
259
William Lallemandc03eb012018-11-27 12:02:37 +0100260 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
261 ha_alert("Cannot set CLOEXEC on client socket.\n");
262 close(sv[0]);
263 close(sv[1]);
264 conn->err_code = CO_ER_SOCK_ERR;
265 conn->flags |= CO_FL_ERROR;
266 return SF_ERR_INTERNAL;
267 }
268
William Lallemand2fe7dd02018-09-11 16:51:29 +0200269 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200270 if (conn->send_proxy_ofs)
271 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200272
273 if (global.tune.server_sndbuf)
274 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
275
276 if (global.tune.server_rcvbuf)
277 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
278
279 /* The new socket is sent on the other side, it should be retrieved and
280 * considered as an 'accept' socket on the server side */
281 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
282 close(sv[0]);
283 close(sv[1]);
284 conn->err_code = CO_ER_SOCK_ERR;
285 conn->flags |= CO_FL_ERROR;
286 return SF_ERR_INTERNAL;
287 }
288
289 close(sv[0]); /* we don't need this side anymore */
290
291 conn->flags &= ~CO_FL_WAIT_L4_CONN;
292
293 conn->flags |= CO_FL_ADDR_TO_SET;
294
295 /* Prepare to send a few handshakes related to the on-wire protocol. */
296 if (conn->send_proxy_ofs)
297 conn->flags |= CO_FL_SEND_PROXY;
298
299 conn_ctrl_init(conn); /* registers the FD */
300 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
301
302 if (conn_xprt_init(conn) < 0) {
303 conn_full_close(conn);
304 conn->flags |= CO_FL_ERROR;
305 return SF_ERR_RESOURCE;
306 }
307
William Lallemand2fe7dd02018-09-11 16:51:29 +0200308 return SF_ERR_NONE; /* connection is OK */
309}
310
311
312/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800313 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200314 *
315 * Return -1 or a socket fd;
316 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800317 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200318 */
319int recv_fd_uxst(int sock)
320{
321 struct msghdr msghdr;
322 struct iovec iov;
323 char iobuf[2];
324
325 char cmsgbuf[CMSG_SPACE(sizeof(int))];
326 char buf[CMSG_SPACE(sizeof(int))];
327 struct cmsghdr *cmsg = (void *)buf;
328
329
330 int recv_fd = -1;
331 int ret = -1;
332
333 memset(&msghdr, 0, sizeof(msghdr));
334
335 iov.iov_base = iobuf;
336 iov.iov_len = sizeof(iobuf);
337
338 msghdr.msg_iov = &iov;
339 msghdr.msg_iovlen = 1;
340
341 msghdr.msg_control = cmsgbuf;
342 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
343
344 iov.iov_len = sizeof(iobuf);
345 iov.iov_base = iobuf;
346
347 while (1) {
348 ret = recvmsg(sock, &msghdr, 0);
349 if (ret == -1 && errno == EINTR)
350 continue;
351 else
352 break;
353 }
354
355 if (ret == -1)
356 return ret;
357
358 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200359 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200360 cmsg->cmsg_type == SCM_RIGHTS) {
361 size_t totlen = cmsg->cmsg_len -
362 CMSG_LEN(0);
363 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
364 }
365 return recv_fd;
366}
367
William Lallemand2fe7dd02018-09-11 16:51:29 +0200368/*
369 * Local variables:
370 * c-indent-level: 8
371 * c-basic-offset: 8
372 * End:
373 */