blob: a2a9607a822e9f34b3b838cd6e55fddb6dcb3a0c [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,
William Lallemand2fe7dd02018-09-11 16:51:29 +020064 .sock_domain = AF_CUST_SOCKPAIR,
65 .sock_type = SOCK_STREAM,
66 .sock_prot = 0,
67 .sock_family = AF_UNIX,
68 .sock_addrlen = sizeof(struct sockaddr_un),
69 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
70 .accept = &listener_accept,
71 .connect = &sockpair_connect_server,
Willy Tarreau62292b22020-09-02 17:52:23 +020072 .bind = sockpair_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020073 .listen = sockpair_bind_listener,
William Lallemand2fe7dd02018-09-11 16:51:29 +020074 .enable_all = enable_all_listeners,
75 .disable_all = disable_all_listeners,
76 .get_src = NULL,
77 .get_dst = NULL,
78 .pause = NULL,
79 .add = sockpair_add_listener,
80 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
81 .nb_listeners = 0,
82};
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;
97 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +020098 listener->rx.proto = &proto_sockpair;
99 LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200100 proto_sockpair.nb_listeners++;
101}
102
Willy Tarreau62292b22020-09-02 17:52:23 +0200103/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
104 * context, respectively, with <tm> as the thread mask. Returns and error code
105 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
106 * message may be passed into <errmsg>. Note that the binding address is only
107 * an FD to receive the incoming FDs on. Thus by definition there is no real
108 * "bind" operation, this only completes the receiver. Such FDs are not
109 * inherited upon reload.
110 */
111int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
112{
113 int err;
114
115 /* ensure we never return garbage */
116 if (errmsg)
117 *errmsg = 0;
118
119 err = ERR_NONE;
120
121 if (rx->flags & RX_F_BOUND)
122 return ERR_NONE;
123
124 if (rx->fd == -1) {
125 err |= ERR_FATAL | ERR_ALERT;
126 memprintf(errmsg, "sockpair may be only used with inherited FDs");
127 goto bind_return;
128 }
129
130 if (rx->fd >= global.maxsock) {
131 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
132 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
133 goto bind_close_return;
134 }
135
136 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
137 err |= ERR_FATAL | ERR_ALERT;
138 memprintf(errmsg, "cannot make socket non-blocking");
139 goto bind_close_return;
140 }
141
142 rx->flags |= RX_F_BOUND;
143
144 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
145 return err;
146
147 bind_return:
148 if (errmsg && *errmsg)
149 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
150
151 return err;
152
153 bind_close_return:
154 close(rx->fd);
155 goto bind_return;
156}
157
William Lallemand2fe7dd02018-09-11 16:51:29 +0200158/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
159 * enabled for polling. The return value is composed from ERR_NONE,
160 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
161 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
162 * Note that <errmsg> may be NULL if <errlen> is also zero.
163 */
164static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
165{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200166 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200167 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200168
169 err = ERR_NONE;
170
171 /* ensure we never return garbage */
172 if (errlen)
173 *errmsg = 0;
174
175 if (listener->state != LI_ASSIGNED)
176 return ERR_NONE; /* already bound */
177
Willy Tarreauad33acf2020-09-02 18:40:02 +0200178 if (!(listener->rx.flags & RX_F_BOUND)) {
179 msg = "receiving socket not bound";
180 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200181 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200182
William Lallemand2fe7dd02018-09-11 16:51:29 +0200183 listener->state = LI_LISTEN;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200184 return err;
185
186 err_return:
187 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200188 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200189 return err;
190}
191
192/*
193 * Send FD over a unix socket
194 *
195 * <send_fd> is the FD to send
196 * <fd> is the fd of the unix socket to use for the transfer
197 *
198 * The iobuf variable could be use in the future to enhance the protocol.
199 */
200int send_fd_uxst(int fd, int send_fd)
201{
202 char iobuf[2];
203 struct iovec iov;
204 struct msghdr msghdr;
205
206 char cmsgbuf[CMSG_SPACE(sizeof(int))];
207 char buf[CMSG_SPACE(sizeof(int))];
208 struct cmsghdr *cmsg = (void *)buf;
209
210 int *fdptr;
211
212 iov.iov_base = iobuf;
213 iov.iov_len = sizeof(iobuf);
214
215 memset(&msghdr, 0, sizeof(msghdr));
216 msghdr.msg_iov = &iov;
217 msghdr.msg_iovlen = 1;
218
219 /* Now send the fds */
220 msghdr.msg_control = cmsgbuf;
221 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
222
223 cmsg = CMSG_FIRSTHDR(&msghdr);
224 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
225 cmsg->cmsg_level = SOL_SOCKET;
226 cmsg->cmsg_type = SCM_RIGHTS;
227
228 fdptr = (int *)CMSG_DATA(cmsg);
229 memcpy(fdptr, &send_fd, sizeof(send_fd));
230
231 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
232 ha_warning("Failed to transfer socket\n");
233 return 1;
234 }
235
236 return 0;
237}
238
239/*
240 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800241 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200242 * socket and establishing a connection, it creates a pair of connected
243 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200244 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200245 *
246 * conn->target may point either to a valid server or to a backend, depending
247 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
248 * <data> parameter is a boolean indicating whether there are data waiting for
249 * being sent or not, in order to adjust data write polling and on some
250 * platforms. The <delack> argument is ignored.
251 *
252 * Note that a pending send_proxy message accounts for data.
253 *
254 * It can return one of :
255 * - SF_ERR_NONE if everything's OK
256 * - SF_ERR_SRVTO if there are no more servers
257 * - SF_ERR_SRVCL if the connection was refused by the server
258 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
259 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
260 * - SF_ERR_INTERNAL for any other purely internal errors
261 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
262 *
263 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
264 * it's invalid and the caller has nothing to do.
265 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200266static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200267{
268 int sv[2], fd, dst_fd = -1;
269
270 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200271 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200272
William Lallemand2fe7dd02018-09-11 16:51:29 +0200273 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
274 obj_type(conn->target) != OBJ_TYPE_SERVER) {
275 conn->flags |= CO_FL_ERROR;
276 return SF_ERR_INTERNAL;
277 }
278
279 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
280 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
281 conn->flags |= CO_FL_ERROR;
282 return SF_ERR_RESOURCE;
283 }
284
285 fd = conn->handle.fd = sv[1];
286
287 if (fd >= global.maxsock) {
288 /* do not log anything there, it's a normal condition when this option
289 * is used to serialize connections to a server !
290 */
291 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
292 close(sv[0]);
293 close(sv[1]);
294 conn->err_code = CO_ER_CONF_FDLIM;
295 conn->flags |= CO_FL_ERROR;
296 return SF_ERR_PRXCOND; /* it is a configuration limit */
297 }
298
299 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
300 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
301 close(sv[0]);
302 close(sv[1]);
303 conn->err_code = CO_ER_SOCK_ERR;
304 conn->flags |= CO_FL_ERROR;
305 return SF_ERR_INTERNAL;
306 }
307
William Lallemandc03eb012018-11-27 12:02:37 +0100308 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
309 ha_alert("Cannot set CLOEXEC on client socket.\n");
310 close(sv[0]);
311 close(sv[1]);
312 conn->err_code = CO_ER_SOCK_ERR;
313 conn->flags |= CO_FL_ERROR;
314 return SF_ERR_INTERNAL;
315 }
316
William Lallemand2fe7dd02018-09-11 16:51:29 +0200317 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200318 if (conn->send_proxy_ofs)
319 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200320
321 if (global.tune.server_sndbuf)
322 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
323
324 if (global.tune.server_rcvbuf)
325 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
326
327 /* The new socket is sent on the other side, it should be retrieved and
328 * considered as an 'accept' socket on the server side */
329 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
330 close(sv[0]);
331 close(sv[1]);
332 conn->err_code = CO_ER_SOCK_ERR;
333 conn->flags |= CO_FL_ERROR;
334 return SF_ERR_INTERNAL;
335 }
336
337 close(sv[0]); /* we don't need this side anymore */
338
339 conn->flags &= ~CO_FL_WAIT_L4_CONN;
340
341 conn->flags |= CO_FL_ADDR_TO_SET;
342
343 /* Prepare to send a few handshakes related to the on-wire protocol. */
344 if (conn->send_proxy_ofs)
345 conn->flags |= CO_FL_SEND_PROXY;
346
347 conn_ctrl_init(conn); /* registers the FD */
348 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
349
350 if (conn_xprt_init(conn) < 0) {
351 conn_full_close(conn);
352 conn->flags |= CO_FL_ERROR;
353 return SF_ERR_RESOURCE;
354 }
355
William Lallemand2fe7dd02018-09-11 16:51:29 +0200356 return SF_ERR_NONE; /* connection is OK */
357}
358
359
360/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800361 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200362 *
363 * Return -1 or a socket fd;
364 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800365 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200366 */
367int recv_fd_uxst(int sock)
368{
369 struct msghdr msghdr;
370 struct iovec iov;
371 char iobuf[2];
372
373 char cmsgbuf[CMSG_SPACE(sizeof(int))];
374 char buf[CMSG_SPACE(sizeof(int))];
375 struct cmsghdr *cmsg = (void *)buf;
376
377
378 int recv_fd = -1;
379 int ret = -1;
380
381 memset(&msghdr, 0, sizeof(msghdr));
382
383 iov.iov_base = iobuf;
384 iov.iov_len = sizeof(iobuf);
385
386 msghdr.msg_iov = &iov;
387 msghdr.msg_iovlen = 1;
388
389 msghdr.msg_control = cmsgbuf;
390 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
391
392 iov.iov_len = sizeof(iobuf);
393 iov.iov_base = iobuf;
394
395 while (1) {
396 ret = recvmsg(sock, &msghdr, 0);
397 if (ret == -1 && errno == EINTR)
398 continue;
399 else
400 break;
401 }
402
403 if (ret == -1)
404 return ret;
405
406 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200407 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200408 cmsg->cmsg_type == SCM_RIGHTS) {
409 size_t totlen = cmsg->cmsg_len -
410 CMSG_LEN(0);
411 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
412 }
413 return recv_fd;
414}
415
William Lallemand2fe7dd02018-09-11 16:51:29 +0200416/*
417 * Local variables:
418 * c-indent-level: 8
419 * c-basic-offset: 8
420 * End:
421 */