blob: 48b598bbfc72ca05bbbcd3e89fecf1e7b55ffd82 [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 Tarreau62292b22020-09-02 17:52:23 +020038#include <haproxy/proto_sockpair.h>
Willy Tarreau686fa3d2020-09-25 19:09:53 +020039#include <haproxy/sock.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020040#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020042#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020043
William Lallemand2fe7dd02018-09-11 16:51:29 +020044
45static void sockpair_add_listener(struct listener *listener, int port);
46static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020047static void sockpair_enable_listener(struct listener *listener);
48static void sockpair_disable_listener(struct listener *listener);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020049static int sockpair_connect_server(struct connection *conn, int flags);
Willy Tarreaucc8b6532020-10-13 17:27:34 +020050static int sockpair_accept_conn(const struct receiver *rx);
William Lallemand2fe7dd02018-09-11 16:51:29 +020051
Willy Tarreaub0254cb2020-09-04 08:07:11 +020052struct proto_fam proto_fam_sockpair = {
53 .name = "sockpair",
54 .sock_domain = AF_CUST_SOCKPAIR,
55 .sock_family = AF_UNIX,
56 .sock_addrlen = sizeof(struct sockaddr_un),
57 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
58 .addrcmp = NULL,
59 .bind = sockpair_bind_receiver,
60 .get_src = NULL,
61 .get_dst = NULL,
62};
63
William Lallemand2fe7dd02018-09-11 16:51:29 +020064/* Note: must not be declared <const> as its list will be overwritten */
65static struct protocol proto_sockpair = {
66 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020067 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020068 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020069 .sock_domain = AF_CUST_SOCKPAIR,
70 .sock_type = SOCK_STREAM,
71 .sock_prot = 0,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020072 .add = sockpair_add_listener,
73 .listen = sockpair_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020074 .enable = sockpair_enable_listener,
75 .disable = sockpair_disable_listener,
Willy Tarreau7b2febd2020-10-09 17:18:29 +020076 .unbind = default_unbind_listener,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020077 .rx_unbind = sock_unbind,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020078 .rx_enable = sock_enable,
79 .rx_disable = sock_disable,
Willy Tarreaucc8b6532020-10-13 17:27:34 +020080 .rx_listening = sockpair_accept_conn,
William Lallemand2fe7dd02018-09-11 16:51:29 +020081 .accept = &listener_accept,
82 .connect = &sockpair_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020083 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
84 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020085};
86
Willy Tarreau0108d902018-11-25 19:14:37 +010087INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
88
William Lallemand2fe7dd02018-09-11 16:51:29 +020089/* Add <listener> to the list of sockpair listeners (port is ignored). The
90 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
91 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020092 *
93 * Must be called with proto_lock held.
94 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020095 */
96static void sockpair_add_listener(struct listener *listener, int port)
97{
98 if (listener->state != LI_INIT)
99 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200100 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200101 listener->rx.proto = &proto_sockpair;
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200102 LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
103 proto_sockpair.nb_receivers++;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200104}
105
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200106/* Enable receipt of incoming connections for listener <l>. The receiver must
107 * still be valid. Does nothing in early boot (needs fd_updt).
108 */
109static void sockpair_enable_listener(struct listener *l)
110{
111 if (fd_updt)
112 fd_want_recv(l->rx.fd);
113}
114
115/* Disable receipt of incoming connections for listener <l>. The receiver must
116 * still be valid. Does nothing in early boot (needs fd_updt).
117 */
118static void sockpair_disable_listener(struct listener *l)
119{
120 if (fd_updt)
121 fd_stop_recv(l->rx.fd);
122}
123
Willy Tarreau62292b22020-09-02 17:52:23 +0200124/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
125 * context, respectively, with <tm> as the thread mask. Returns and error code
126 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
127 * message may be passed into <errmsg>. Note that the binding address is only
128 * an FD to receive the incoming FDs on. Thus by definition there is no real
129 * "bind" operation, this only completes the receiver. Such FDs are not
130 * inherited upon reload.
131 */
132int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
133{
134 int err;
135
136 /* ensure we never return garbage */
137 if (errmsg)
138 *errmsg = 0;
139
140 err = ERR_NONE;
141
142 if (rx->flags & RX_F_BOUND)
143 return ERR_NONE;
144
145 if (rx->fd == -1) {
146 err |= ERR_FATAL | ERR_ALERT;
147 memprintf(errmsg, "sockpair may be only used with inherited FDs");
148 goto bind_return;
149 }
150
151 if (rx->fd >= global.maxsock) {
152 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
153 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
154 goto bind_close_return;
155 }
156
157 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
158 err |= ERR_FATAL | ERR_ALERT;
159 memprintf(errmsg, "cannot make socket non-blocking");
160 goto bind_close_return;
161 }
162
163 rx->flags |= RX_F_BOUND;
164
165 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
166 return err;
167
168 bind_return:
169 if (errmsg && *errmsg)
170 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
171
172 return err;
173
174 bind_close_return:
175 close(rx->fd);
176 goto bind_return;
177}
178
William Lallemand2fe7dd02018-09-11 16:51:29 +0200179/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
180 * enabled for polling. The return value is composed from ERR_NONE,
181 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
182 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
183 * Note that <errmsg> may be NULL if <errlen> is also zero.
184 */
185static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
186{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200187 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200188 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200189
190 err = ERR_NONE;
191
192 /* ensure we never return garbage */
193 if (errlen)
194 *errmsg = 0;
195
196 if (listener->state != LI_ASSIGNED)
197 return ERR_NONE; /* already bound */
198
Willy Tarreauad33acf2020-09-02 18:40:02 +0200199 if (!(listener->rx.flags & RX_F_BOUND)) {
200 msg = "receiving socket not bound";
201 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200202 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200203
Willy Tarreaua37b2442020-09-24 07:23:45 +0200204 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200205 return err;
206
207 err_return:
208 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200209 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200210 return err;
211}
212
213/*
214 * Send FD over a unix socket
215 *
216 * <send_fd> is the FD to send
217 * <fd> is the fd of the unix socket to use for the transfer
218 *
219 * The iobuf variable could be use in the future to enhance the protocol.
220 */
221int send_fd_uxst(int fd, int send_fd)
222{
223 char iobuf[2];
224 struct iovec iov;
225 struct msghdr msghdr;
226
227 char cmsgbuf[CMSG_SPACE(sizeof(int))];
228 char buf[CMSG_SPACE(sizeof(int))];
229 struct cmsghdr *cmsg = (void *)buf;
230
231 int *fdptr;
232
233 iov.iov_base = iobuf;
234 iov.iov_len = sizeof(iobuf);
235
236 memset(&msghdr, 0, sizeof(msghdr));
237 msghdr.msg_iov = &iov;
238 msghdr.msg_iovlen = 1;
239
240 /* Now send the fds */
241 msghdr.msg_control = cmsgbuf;
242 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
243
244 cmsg = CMSG_FIRSTHDR(&msghdr);
245 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
246 cmsg->cmsg_level = SOL_SOCKET;
247 cmsg->cmsg_type = SCM_RIGHTS;
248
249 fdptr = (int *)CMSG_DATA(cmsg);
250 memcpy(fdptr, &send_fd, sizeof(send_fd));
251
252 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
253 ha_warning("Failed to transfer socket\n");
254 return 1;
255 }
256
257 return 0;
258}
259
260/*
261 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800262 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200263 * socket and establishing a connection, it creates a pair of connected
264 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200265 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200266 *
267 * conn->target may point either to a valid server or to a backend, depending
268 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
269 * <data> parameter is a boolean indicating whether there are data waiting for
270 * being sent or not, in order to adjust data write polling and on some
271 * platforms. The <delack> argument is ignored.
272 *
273 * Note that a pending send_proxy message accounts for data.
274 *
275 * It can return one of :
276 * - SF_ERR_NONE if everything's OK
277 * - SF_ERR_SRVTO if there are no more servers
278 * - SF_ERR_SRVCL if the connection was refused by the server
279 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
280 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
281 * - SF_ERR_INTERNAL for any other purely internal errors
282 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
283 *
284 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
285 * it's invalid and the caller has nothing to do.
286 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200287static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200288{
289 int sv[2], fd, dst_fd = -1;
290
291 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200292 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200293
William Lallemand2fe7dd02018-09-11 16:51:29 +0200294 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
295 obj_type(conn->target) != OBJ_TYPE_SERVER) {
296 conn->flags |= CO_FL_ERROR;
297 return SF_ERR_INTERNAL;
298 }
299
300 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
301 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
302 conn->flags |= CO_FL_ERROR;
303 return SF_ERR_RESOURCE;
304 }
305
306 fd = conn->handle.fd = sv[1];
307
308 if (fd >= global.maxsock) {
309 /* do not log anything there, it's a normal condition when this option
310 * is used to serialize connections to a server !
311 */
312 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
313 close(sv[0]);
314 close(sv[1]);
315 conn->err_code = CO_ER_CONF_FDLIM;
316 conn->flags |= CO_FL_ERROR;
317 return SF_ERR_PRXCOND; /* it is a configuration limit */
318 }
319
320 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
321 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
322 close(sv[0]);
323 close(sv[1]);
324 conn->err_code = CO_ER_SOCK_ERR;
325 conn->flags |= CO_FL_ERROR;
326 return SF_ERR_INTERNAL;
327 }
328
William Lallemandc03eb012018-11-27 12:02:37 +0100329 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
330 ha_alert("Cannot set CLOEXEC on client socket.\n");
331 close(sv[0]);
332 close(sv[1]);
333 conn->err_code = CO_ER_SOCK_ERR;
334 conn->flags |= CO_FL_ERROR;
335 return SF_ERR_INTERNAL;
336 }
337
William Lallemand2fe7dd02018-09-11 16:51:29 +0200338 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200339 if (conn->send_proxy_ofs)
340 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200341
342 if (global.tune.server_sndbuf)
343 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
344
345 if (global.tune.server_rcvbuf)
346 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
347
348 /* The new socket is sent on the other side, it should be retrieved and
349 * considered as an 'accept' socket on the server side */
350 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
351 close(sv[0]);
352 close(sv[1]);
353 conn->err_code = CO_ER_SOCK_ERR;
354 conn->flags |= CO_FL_ERROR;
355 return SF_ERR_INTERNAL;
356 }
357
358 close(sv[0]); /* we don't need this side anymore */
359
360 conn->flags &= ~CO_FL_WAIT_L4_CONN;
361
362 conn->flags |= CO_FL_ADDR_TO_SET;
363
364 /* Prepare to send a few handshakes related to the on-wire protocol. */
365 if (conn->send_proxy_ofs)
366 conn->flags |= CO_FL_SEND_PROXY;
367
368 conn_ctrl_init(conn); /* registers the FD */
369 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
370
371 if (conn_xprt_init(conn) < 0) {
372 conn_full_close(conn);
373 conn->flags |= CO_FL_ERROR;
374 return SF_ERR_RESOURCE;
375 }
376
William Lallemand2fe7dd02018-09-11 16:51:29 +0200377 return SF_ERR_NONE; /* connection is OK */
378}
379
380
381/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800382 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200383 *
384 * Return -1 or a socket fd;
385 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800386 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200387 */
388int recv_fd_uxst(int sock)
389{
390 struct msghdr msghdr;
391 struct iovec iov;
392 char iobuf[2];
393
394 char cmsgbuf[CMSG_SPACE(sizeof(int))];
395 char buf[CMSG_SPACE(sizeof(int))];
396 struct cmsghdr *cmsg = (void *)buf;
397
398
399 int recv_fd = -1;
400 int ret = -1;
401
402 memset(&msghdr, 0, sizeof(msghdr));
403
404 iov.iov_base = iobuf;
405 iov.iov_len = sizeof(iobuf);
406
407 msghdr.msg_iov = &iov;
408 msghdr.msg_iovlen = 1;
409
410 msghdr.msg_control = cmsgbuf;
411 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
412
413 iov.iov_len = sizeof(iobuf);
414 iov.iov_base = iobuf;
415
416 while (1) {
417 ret = recvmsg(sock, &msghdr, 0);
418 if (ret == -1 && errno == EINTR)
419 continue;
420 else
421 break;
422 }
423
424 if (ret == -1)
425 return ret;
426
427 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200428 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200429 cmsg->cmsg_type == SCM_RIGHTS) {
430 size_t totlen = cmsg->cmsg_len -
431 CMSG_LEN(0);
432 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
433 }
434 return recv_fd;
435}
436
Willy Tarreaucc8b6532020-10-13 17:27:34 +0200437/* Tests if the receiver supports accepting connections. Returns positive on
438 * success, 0 if not possible, negative if the socket is non-recoverable. In
439 * practice zero is never returned since we don't support suspending sockets.
440 * The real test consists in verifying we have a connected SOCK_STREAM of
441 * family AF_UNIX.
442 */
443static int sockpair_accept_conn(const struct receiver *rx)
444{
445 struct sockaddr sa;
446 socklen_t len;
447 int val;
448
449 len = sizeof(val);
450 if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1)
451 return -1;
452
453 if (val != SOCK_STREAM)
454 return -1;
455
456 len = sizeof(sa);
457 if (getsockname(rx->fd, &sa, &len) != 0)
458 return -1;
459
460 if (sa.sa_family != AF_UNIX)
461 return -1;
462
463 len = sizeof(val);
464 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1)
465 return -1;
466
467 /* Note: cannot be a listening socket, must be established */
468 if (val)
469 return -1;
470
471 return 1;
472}
473
William Lallemand2fe7dd02018-09-11 16:51:29 +0200474/*
475 * Local variables:
476 * c-indent-level: 8
477 * c-basic-offset: 8
478 * End:
479 */