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