blob: 9eac2cc2c16bc9e17e75f180964fe8cf1fee7739 [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);
William Lallemand2fe7dd02018-09-11 16:51:29 +020050
Willy Tarreaub0254cb2020-09-04 08:07:11 +020051struct proto_fam proto_fam_sockpair = {
52 .name = "sockpair",
53 .sock_domain = AF_CUST_SOCKPAIR,
54 .sock_family = AF_UNIX,
55 .sock_addrlen = sizeof(struct sockaddr_un),
56 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
57 .addrcmp = NULL,
58 .bind = sockpair_bind_receiver,
59 .get_src = NULL,
60 .get_dst = NULL,
61};
62
William Lallemand2fe7dd02018-09-11 16:51:29 +020063/* Note: must not be declared <const> as its list will be overwritten */
64static struct protocol proto_sockpair = {
65 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020066 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020067 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020068 .sock_domain = AF_CUST_SOCKPAIR,
69 .sock_type = SOCK_STREAM,
70 .sock_prot = 0,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020071 .add = sockpair_add_listener,
72 .listen = sockpair_bind_listener,
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +020073 .enable = sockpair_enable_listener,
74 .disable = sockpair_disable_listener,
Willy Tarreauf58b8db2020-10-09 16:32:08 +020075 .rx_unbind = sock_unbind,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020076 .rx_enable = sock_enable,
77 .rx_disable = sock_disable,
William Lallemand2fe7dd02018-09-11 16:51:29 +020078 .accept = &listener_accept,
79 .connect = &sockpair_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020080 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
81 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020082};
83
Willy Tarreau0108d902018-11-25 19:14:37 +010084INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
85
William Lallemand2fe7dd02018-09-11 16:51:29 +020086/* Add <listener> to the list of sockpair listeners (port is ignored). The
87 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
88 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020089 *
90 * Must be called with proto_lock held.
91 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020092 */
93static void sockpair_add_listener(struct listener *listener, int port)
94{
95 if (listener->state != LI_INIT)
96 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +020097 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +020098 listener->rx.proto = &proto_sockpair;
Willy Tarreaud7f331c2020-09-25 17:01:43 +020099 LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
100 proto_sockpair.nb_receivers++;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200101}
102
Willy Tarreau5ddf1ce2020-09-25 19:27:39 +0200103/* Enable receipt of incoming connections for listener <l>. The receiver must
104 * still be valid. Does nothing in early boot (needs fd_updt).
105 */
106static void sockpair_enable_listener(struct listener *l)
107{
108 if (fd_updt)
109 fd_want_recv(l->rx.fd);
110}
111
112/* Disable receipt of incoming connections for listener <l>. The receiver must
113 * still be valid. Does nothing in early boot (needs fd_updt).
114 */
115static void sockpair_disable_listener(struct listener *l)
116{
117 if (fd_updt)
118 fd_stop_recv(l->rx.fd);
119}
120
Willy Tarreau62292b22020-09-02 17:52:23 +0200121/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
122 * context, respectively, with <tm> as the thread mask. Returns and error code
123 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
124 * message may be passed into <errmsg>. Note that the binding address is only
125 * an FD to receive the incoming FDs on. Thus by definition there is no real
126 * "bind" operation, this only completes the receiver. Such FDs are not
127 * inherited upon reload.
128 */
129int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
130{
131 int err;
132
133 /* ensure we never return garbage */
134 if (errmsg)
135 *errmsg = 0;
136
137 err = ERR_NONE;
138
139 if (rx->flags & RX_F_BOUND)
140 return ERR_NONE;
141
142 if (rx->fd == -1) {
143 err |= ERR_FATAL | ERR_ALERT;
144 memprintf(errmsg, "sockpair may be only used with inherited FDs");
145 goto bind_return;
146 }
147
148 if (rx->fd >= global.maxsock) {
149 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
150 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
151 goto bind_close_return;
152 }
153
154 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
155 err |= ERR_FATAL | ERR_ALERT;
156 memprintf(errmsg, "cannot make socket non-blocking");
157 goto bind_close_return;
158 }
159
160 rx->flags |= RX_F_BOUND;
161
162 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
163 return err;
164
165 bind_return:
166 if (errmsg && *errmsg)
167 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
168
169 return err;
170
171 bind_close_return:
172 close(rx->fd);
173 goto bind_return;
174}
175
William Lallemand2fe7dd02018-09-11 16:51:29 +0200176/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
177 * enabled for polling. The return value is composed from ERR_NONE,
178 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
179 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
180 * Note that <errmsg> may be NULL if <errlen> is also zero.
181 */
182static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
183{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200184 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200185 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200186
187 err = ERR_NONE;
188
189 /* ensure we never return garbage */
190 if (errlen)
191 *errmsg = 0;
192
193 if (listener->state != LI_ASSIGNED)
194 return ERR_NONE; /* already bound */
195
Willy Tarreauad33acf2020-09-02 18:40:02 +0200196 if (!(listener->rx.flags & RX_F_BOUND)) {
197 msg = "receiving socket not bound";
198 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200199 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200200
Willy Tarreaua37b2442020-09-24 07:23:45 +0200201 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200202 return err;
203
204 err_return:
205 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200206 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200207 return err;
208}
209
210/*
211 * Send FD over a unix socket
212 *
213 * <send_fd> is the FD to send
214 * <fd> is the fd of the unix socket to use for the transfer
215 *
216 * The iobuf variable could be use in the future to enhance the protocol.
217 */
218int send_fd_uxst(int fd, int send_fd)
219{
220 char iobuf[2];
221 struct iovec iov;
222 struct msghdr msghdr;
223
224 char cmsgbuf[CMSG_SPACE(sizeof(int))];
225 char buf[CMSG_SPACE(sizeof(int))];
226 struct cmsghdr *cmsg = (void *)buf;
227
228 int *fdptr;
229
230 iov.iov_base = iobuf;
231 iov.iov_len = sizeof(iobuf);
232
233 memset(&msghdr, 0, sizeof(msghdr));
234 msghdr.msg_iov = &iov;
235 msghdr.msg_iovlen = 1;
236
237 /* Now send the fds */
238 msghdr.msg_control = cmsgbuf;
239 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
240
241 cmsg = CMSG_FIRSTHDR(&msghdr);
242 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
243 cmsg->cmsg_level = SOL_SOCKET;
244 cmsg->cmsg_type = SCM_RIGHTS;
245
246 fdptr = (int *)CMSG_DATA(cmsg);
247 memcpy(fdptr, &send_fd, sizeof(send_fd));
248
249 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
250 ha_warning("Failed to transfer socket\n");
251 return 1;
252 }
253
254 return 0;
255}
256
257/*
258 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800259 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200260 * socket and establishing a connection, it creates a pair of connected
261 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200262 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200263 *
264 * conn->target may point either to a valid server or to a backend, depending
265 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
266 * <data> parameter is a boolean indicating whether there are data waiting for
267 * being sent or not, in order to adjust data write polling and on some
268 * platforms. The <delack> argument is ignored.
269 *
270 * Note that a pending send_proxy message accounts for data.
271 *
272 * It can return one of :
273 * - SF_ERR_NONE if everything's OK
274 * - SF_ERR_SRVTO if there are no more servers
275 * - SF_ERR_SRVCL if the connection was refused by the server
276 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
277 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
278 * - SF_ERR_INTERNAL for any other purely internal errors
279 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
280 *
281 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
282 * it's invalid and the caller has nothing to do.
283 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200284static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200285{
286 int sv[2], fd, dst_fd = -1;
287
288 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200289 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200290
William Lallemand2fe7dd02018-09-11 16:51:29 +0200291 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
292 obj_type(conn->target) != OBJ_TYPE_SERVER) {
293 conn->flags |= CO_FL_ERROR;
294 return SF_ERR_INTERNAL;
295 }
296
297 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
298 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
299 conn->flags |= CO_FL_ERROR;
300 return SF_ERR_RESOURCE;
301 }
302
303 fd = conn->handle.fd = sv[1];
304
305 if (fd >= global.maxsock) {
306 /* do not log anything there, it's a normal condition when this option
307 * is used to serialize connections to a server !
308 */
309 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
310 close(sv[0]);
311 close(sv[1]);
312 conn->err_code = CO_ER_CONF_FDLIM;
313 conn->flags |= CO_FL_ERROR;
314 return SF_ERR_PRXCOND; /* it is a configuration limit */
315 }
316
317 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
318 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
319 close(sv[0]);
320 close(sv[1]);
321 conn->err_code = CO_ER_SOCK_ERR;
322 conn->flags |= CO_FL_ERROR;
323 return SF_ERR_INTERNAL;
324 }
325
William Lallemandc03eb012018-11-27 12:02:37 +0100326 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
327 ha_alert("Cannot set CLOEXEC on client socket.\n");
328 close(sv[0]);
329 close(sv[1]);
330 conn->err_code = CO_ER_SOCK_ERR;
331 conn->flags |= CO_FL_ERROR;
332 return SF_ERR_INTERNAL;
333 }
334
William Lallemand2fe7dd02018-09-11 16:51:29 +0200335 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200336 if (conn->send_proxy_ofs)
337 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200338
339 if (global.tune.server_sndbuf)
340 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
341
342 if (global.tune.server_rcvbuf)
343 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
344
345 /* The new socket is sent on the other side, it should be retrieved and
346 * considered as an 'accept' socket on the server side */
347 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
348 close(sv[0]);
349 close(sv[1]);
350 conn->err_code = CO_ER_SOCK_ERR;
351 conn->flags |= CO_FL_ERROR;
352 return SF_ERR_INTERNAL;
353 }
354
355 close(sv[0]); /* we don't need this side anymore */
356
357 conn->flags &= ~CO_FL_WAIT_L4_CONN;
358
359 conn->flags |= CO_FL_ADDR_TO_SET;
360
361 /* Prepare to send a few handshakes related to the on-wire protocol. */
362 if (conn->send_proxy_ofs)
363 conn->flags |= CO_FL_SEND_PROXY;
364
365 conn_ctrl_init(conn); /* registers the FD */
366 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
367
368 if (conn_xprt_init(conn) < 0) {
369 conn_full_close(conn);
370 conn->flags |= CO_FL_ERROR;
371 return SF_ERR_RESOURCE;
372 }
373
William Lallemand2fe7dd02018-09-11 16:51:29 +0200374 return SF_ERR_NONE; /* connection is OK */
375}
376
377
378/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800379 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200380 *
381 * Return -1 or a socket fd;
382 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800383 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200384 */
385int recv_fd_uxst(int sock)
386{
387 struct msghdr msghdr;
388 struct iovec iov;
389 char iobuf[2];
390
391 char cmsgbuf[CMSG_SPACE(sizeof(int))];
392 char buf[CMSG_SPACE(sizeof(int))];
393 struct cmsghdr *cmsg = (void *)buf;
394
395
396 int recv_fd = -1;
397 int ret = -1;
398
399 memset(&msghdr, 0, sizeof(msghdr));
400
401 iov.iov_base = iobuf;
402 iov.iov_len = sizeof(iobuf);
403
404 msghdr.msg_iov = &iov;
405 msghdr.msg_iovlen = 1;
406
407 msghdr.msg_control = cmsgbuf;
408 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
409
410 iov.iov_len = sizeof(iobuf);
411 iov.iov_base = iobuf;
412
413 while (1) {
414 ret = recvmsg(sock, &msghdr, 0);
415 if (ret == -1 && errno == EINTR)
416 continue;
417 else
418 break;
419 }
420
421 if (ret == -1)
422 return ret;
423
424 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200425 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200426 cmsg->cmsg_type == SCM_RIGHTS) {
427 size_t totlen = cmsg->cmsg_len -
428 CMSG_LEN(0);
429 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
430 }
431 return recv_fd;
432}
433
William Lallemand2fe7dd02018-09-11 16:51:29 +0200434/*
435 * Local variables:
436 * c-indent-level: 8
437 * c-basic-offset: 8
438 * End:
439 */