blob: d46fa2794522ba50f69050185a331adb76efc248 [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 Tarreau92b4f132020-06-01 11:05:15 +020039#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020041#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020042
William Lallemand2fe7dd02018-09-11 16:51:29 +020043
44static void sockpair_add_listener(struct listener *listener, int port);
45static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020046static int sockpair_connect_server(struct connection *conn, int flags);
William Lallemand2fe7dd02018-09-11 16:51:29 +020047
Willy Tarreaub0254cb2020-09-04 08:07:11 +020048struct proto_fam proto_fam_sockpair = {
49 .name = "sockpair",
50 .sock_domain = AF_CUST_SOCKPAIR,
51 .sock_family = AF_UNIX,
52 .sock_addrlen = sizeof(struct sockaddr_un),
53 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
54 .addrcmp = NULL,
55 .bind = sockpair_bind_receiver,
56 .get_src = NULL,
57 .get_dst = NULL,
58};
59
William Lallemand2fe7dd02018-09-11 16:51:29 +020060/* Note: must not be declared <const> as its list will be overwritten */
61static struct protocol proto_sockpair = {
62 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020063 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020064 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020065 .sock_domain = AF_CUST_SOCKPAIR,
66 .sock_type = SOCK_STREAM,
67 .sock_prot = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020068 .accept = &listener_accept,
69 .connect = &sockpair_connect_server,
Willy Tarreaub3580b12020-09-01 10:26:22 +020070 .listen = sockpair_bind_listener,
William Lallemand2fe7dd02018-09-11 16:51:29 +020071 .pause = NULL,
72 .add = sockpair_add_listener,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020073 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
74 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020075};
76
Willy Tarreau0108d902018-11-25 19:14:37 +010077INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
78
William Lallemand2fe7dd02018-09-11 16:51:29 +020079/* Add <listener> to the list of sockpair listeners (port is ignored). The
80 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
81 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020082 *
83 * Must be called with proto_lock held.
84 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020085 */
86static void sockpair_add_listener(struct listener *listener, int port)
87{
88 if (listener->state != LI_INIT)
89 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +020090 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +020091 listener->rx.proto = &proto_sockpair;
Willy Tarreaud7f331c2020-09-25 17:01:43 +020092 LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
93 proto_sockpair.nb_receivers++;
William Lallemand2fe7dd02018-09-11 16:51:29 +020094}
95
Willy Tarreau62292b22020-09-02 17:52:23 +020096/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
97 * context, respectively, with <tm> as the thread mask. Returns and error code
98 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
99 * message may be passed into <errmsg>. Note that the binding address is only
100 * an FD to receive the incoming FDs on. Thus by definition there is no real
101 * "bind" operation, this only completes the receiver. Such FDs are not
102 * inherited upon reload.
103 */
104int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
105{
106 int err;
107
108 /* ensure we never return garbage */
109 if (errmsg)
110 *errmsg = 0;
111
112 err = ERR_NONE;
113
114 if (rx->flags & RX_F_BOUND)
115 return ERR_NONE;
116
117 if (rx->fd == -1) {
118 err |= ERR_FATAL | ERR_ALERT;
119 memprintf(errmsg, "sockpair may be only used with inherited FDs");
120 goto bind_return;
121 }
122
123 if (rx->fd >= global.maxsock) {
124 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
125 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
126 goto bind_close_return;
127 }
128
129 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
130 err |= ERR_FATAL | ERR_ALERT;
131 memprintf(errmsg, "cannot make socket non-blocking");
132 goto bind_close_return;
133 }
134
135 rx->flags |= RX_F_BOUND;
136
137 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
138 return err;
139
140 bind_return:
141 if (errmsg && *errmsg)
142 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
143
144 return err;
145
146 bind_close_return:
147 close(rx->fd);
148 goto bind_return;
149}
150
William Lallemand2fe7dd02018-09-11 16:51:29 +0200151/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
152 * enabled for polling. The return value is composed from ERR_NONE,
153 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
154 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
155 * Note that <errmsg> may be NULL if <errlen> is also zero.
156 */
157static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
158{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200159 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200160 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200161
162 err = ERR_NONE;
163
164 /* ensure we never return garbage */
165 if (errlen)
166 *errmsg = 0;
167
168 if (listener->state != LI_ASSIGNED)
169 return ERR_NONE; /* already bound */
170
Willy Tarreauad33acf2020-09-02 18:40:02 +0200171 if (!(listener->rx.flags & RX_F_BOUND)) {
172 msg = "receiving socket not bound";
173 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200174 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200175
Willy Tarreaua37b2442020-09-24 07:23:45 +0200176 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200177 return err;
178
179 err_return:
180 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200181 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200182 return err;
183}
184
185/*
186 * Send FD over a unix socket
187 *
188 * <send_fd> is the FD to send
189 * <fd> is the fd of the unix socket to use for the transfer
190 *
191 * The iobuf variable could be use in the future to enhance the protocol.
192 */
193int send_fd_uxst(int fd, int send_fd)
194{
195 char iobuf[2];
196 struct iovec iov;
197 struct msghdr msghdr;
198
199 char cmsgbuf[CMSG_SPACE(sizeof(int))];
200 char buf[CMSG_SPACE(sizeof(int))];
201 struct cmsghdr *cmsg = (void *)buf;
202
203 int *fdptr;
204
205 iov.iov_base = iobuf;
206 iov.iov_len = sizeof(iobuf);
207
208 memset(&msghdr, 0, sizeof(msghdr));
209 msghdr.msg_iov = &iov;
210 msghdr.msg_iovlen = 1;
211
212 /* Now send the fds */
213 msghdr.msg_control = cmsgbuf;
214 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
215
216 cmsg = CMSG_FIRSTHDR(&msghdr);
217 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
218 cmsg->cmsg_level = SOL_SOCKET;
219 cmsg->cmsg_type = SCM_RIGHTS;
220
221 fdptr = (int *)CMSG_DATA(cmsg);
222 memcpy(fdptr, &send_fd, sizeof(send_fd));
223
224 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
225 ha_warning("Failed to transfer socket\n");
226 return 1;
227 }
228
229 return 0;
230}
231
232/*
233 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800234 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200235 * socket and establishing a connection, it creates a pair of connected
236 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200237 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200238 *
239 * conn->target may point either to a valid server or to a backend, depending
240 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
241 * <data> parameter is a boolean indicating whether there are data waiting for
242 * being sent or not, in order to adjust data write polling and on some
243 * platforms. The <delack> argument is ignored.
244 *
245 * Note that a pending send_proxy message accounts for data.
246 *
247 * It can return one of :
248 * - SF_ERR_NONE if everything's OK
249 * - SF_ERR_SRVTO if there are no more servers
250 * - SF_ERR_SRVCL if the connection was refused by the server
251 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
252 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
253 * - SF_ERR_INTERNAL for any other purely internal errors
254 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
255 *
256 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
257 * it's invalid and the caller has nothing to do.
258 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200259static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200260{
261 int sv[2], fd, dst_fd = -1;
262
263 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200264 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200265
William Lallemand2fe7dd02018-09-11 16:51:29 +0200266 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
267 obj_type(conn->target) != OBJ_TYPE_SERVER) {
268 conn->flags |= CO_FL_ERROR;
269 return SF_ERR_INTERNAL;
270 }
271
272 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
273 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
274 conn->flags |= CO_FL_ERROR;
275 return SF_ERR_RESOURCE;
276 }
277
278 fd = conn->handle.fd = sv[1];
279
280 if (fd >= global.maxsock) {
281 /* do not log anything there, it's a normal condition when this option
282 * is used to serialize connections to a server !
283 */
284 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
285 close(sv[0]);
286 close(sv[1]);
287 conn->err_code = CO_ER_CONF_FDLIM;
288 conn->flags |= CO_FL_ERROR;
289 return SF_ERR_PRXCOND; /* it is a configuration limit */
290 }
291
292 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
293 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
294 close(sv[0]);
295 close(sv[1]);
296 conn->err_code = CO_ER_SOCK_ERR;
297 conn->flags |= CO_FL_ERROR;
298 return SF_ERR_INTERNAL;
299 }
300
William Lallemandc03eb012018-11-27 12:02:37 +0100301 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
302 ha_alert("Cannot set CLOEXEC on client socket.\n");
303 close(sv[0]);
304 close(sv[1]);
305 conn->err_code = CO_ER_SOCK_ERR;
306 conn->flags |= CO_FL_ERROR;
307 return SF_ERR_INTERNAL;
308 }
309
William Lallemand2fe7dd02018-09-11 16:51:29 +0200310 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200311 if (conn->send_proxy_ofs)
312 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200313
314 if (global.tune.server_sndbuf)
315 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
316
317 if (global.tune.server_rcvbuf)
318 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
319
320 /* The new socket is sent on the other side, it should be retrieved and
321 * considered as an 'accept' socket on the server side */
322 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
323 close(sv[0]);
324 close(sv[1]);
325 conn->err_code = CO_ER_SOCK_ERR;
326 conn->flags |= CO_FL_ERROR;
327 return SF_ERR_INTERNAL;
328 }
329
330 close(sv[0]); /* we don't need this side anymore */
331
332 conn->flags &= ~CO_FL_WAIT_L4_CONN;
333
334 conn->flags |= CO_FL_ADDR_TO_SET;
335
336 /* Prepare to send a few handshakes related to the on-wire protocol. */
337 if (conn->send_proxy_ofs)
338 conn->flags |= CO_FL_SEND_PROXY;
339
340 conn_ctrl_init(conn); /* registers the FD */
341 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
342
343 if (conn_xprt_init(conn) < 0) {
344 conn_full_close(conn);
345 conn->flags |= CO_FL_ERROR;
346 return SF_ERR_RESOURCE;
347 }
348
William Lallemand2fe7dd02018-09-11 16:51:29 +0200349 return SF_ERR_NONE; /* connection is OK */
350}
351
352
353/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800354 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200355 *
356 * Return -1 or a socket fd;
357 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800358 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200359 */
360int recv_fd_uxst(int sock)
361{
362 struct msghdr msghdr;
363 struct iovec iov;
364 char iobuf[2];
365
366 char cmsgbuf[CMSG_SPACE(sizeof(int))];
367 char buf[CMSG_SPACE(sizeof(int))];
368 struct cmsghdr *cmsg = (void *)buf;
369
370
371 int recv_fd = -1;
372 int ret = -1;
373
374 memset(&msghdr, 0, sizeof(msghdr));
375
376 iov.iov_base = iobuf;
377 iov.iov_len = sizeof(iobuf);
378
379 msghdr.msg_iov = &iov;
380 msghdr.msg_iovlen = 1;
381
382 msghdr.msg_control = cmsgbuf;
383 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
384
385 iov.iov_len = sizeof(iobuf);
386 iov.iov_base = iobuf;
387
388 while (1) {
389 ret = recvmsg(sock, &msghdr, 0);
390 if (ret == -1 && errno == EINTR)
391 continue;
392 else
393 break;
394 }
395
396 if (ret == -1)
397 return ret;
398
399 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200400 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200401 cmsg->cmsg_type == SCM_RIGHTS) {
402 size_t totlen = cmsg->cmsg_len -
403 CMSG_LEN(0);
404 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
405 }
406 return recv_fd;
407}
408
William Lallemand2fe7dd02018-09-11 16:51:29 +0200409/*
410 * Local variables:
411 * c-indent-level: 8
412 * c-basic-offset: 8
413 * End:
414 */