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