blob: de28faf7acdecfcbd68c1dcd051b5d80f4bb9f46 [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
48/* Note: must not be declared <const> as its list will be overwritten */
49static struct protocol proto_sockpair = {
50 .name = "sockpair",
51 .sock_domain = AF_CUST_SOCKPAIR,
52 .sock_type = SOCK_STREAM,
53 .sock_prot = 0,
54 .sock_family = AF_UNIX,
55 .sock_addrlen = sizeof(struct sockaddr_un),
56 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
57 .accept = &listener_accept,
58 .connect = &sockpair_connect_server,
Willy Tarreau62292b22020-09-02 17:52:23 +020059 .bind = sockpair_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020060 .listen = sockpair_bind_listener,
William Lallemand2fe7dd02018-09-11 16:51:29 +020061 .enable_all = enable_all_listeners,
62 .disable_all = disable_all_listeners,
63 .get_src = NULL,
64 .get_dst = NULL,
65 .pause = NULL,
66 .add = sockpair_add_listener,
67 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
68 .nb_listeners = 0,
69};
70
Willy Tarreau0108d902018-11-25 19:14:37 +010071INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
72
William Lallemand2fe7dd02018-09-11 16:51:29 +020073/* Add <listener> to the list of sockpair listeners (port is ignored). The
74 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
75 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020076 *
77 * Must be called with proto_lock held.
78 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020079 */
80static void sockpair_add_listener(struct listener *listener, int port)
81{
82 if (listener->state != LI_INIT)
83 return;
84 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +020085 listener->rx.proto = &proto_sockpair;
86 LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list);
William Lallemand2fe7dd02018-09-11 16:51:29 +020087 proto_sockpair.nb_listeners++;
88}
89
Willy Tarreau62292b22020-09-02 17:52:23 +020090/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
91 * context, respectively, with <tm> as the thread mask. Returns and error code
92 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
93 * message may be passed into <errmsg>. Note that the binding address is only
94 * an FD to receive the incoming FDs on. Thus by definition there is no real
95 * "bind" operation, this only completes the receiver. Such FDs are not
96 * inherited upon reload.
97 */
98int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
99{
100 int err;
101
102 /* ensure we never return garbage */
103 if (errmsg)
104 *errmsg = 0;
105
106 err = ERR_NONE;
107
108 if (rx->flags & RX_F_BOUND)
109 return ERR_NONE;
110
111 if (rx->fd == -1) {
112 err |= ERR_FATAL | ERR_ALERT;
113 memprintf(errmsg, "sockpair may be only used with inherited FDs");
114 goto bind_return;
115 }
116
117 if (rx->fd >= global.maxsock) {
118 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
119 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
120 goto bind_close_return;
121 }
122
123 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
124 err |= ERR_FATAL | ERR_ALERT;
125 memprintf(errmsg, "cannot make socket non-blocking");
126 goto bind_close_return;
127 }
128
129 rx->flags |= RX_F_BOUND;
130
131 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
132 return err;
133
134 bind_return:
135 if (errmsg && *errmsg)
136 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
137
138 return err;
139
140 bind_close_return:
141 close(rx->fd);
142 goto bind_return;
143}
144
William Lallemand2fe7dd02018-09-11 16:51:29 +0200145/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
146 * enabled for polling. The return value is composed from ERR_NONE,
147 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
148 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
149 * Note that <errmsg> may be NULL if <errlen> is also zero.
150 */
151static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
152{
Willy Tarreau38ba6472020-08-27 08:16:52 +0200153 int fd = listener->rx.fd;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200154 int err;
155 const char *msg = NULL;
156
157 err = ERR_NONE;
158
159 /* ensure we never return garbage */
160 if (errlen)
161 *errmsg = 0;
162
163 if (listener->state != LI_ASSIGNED)
164 return ERR_NONE; /* already bound */
165
Willy Tarreau0b915012020-09-01 10:47:07 +0200166 if (listener->rx.flags & RX_F_BOUND)
167 goto bound;
168
Willy Tarreau38ba6472020-08-27 08:16:52 +0200169 if (listener->rx.fd == -1) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200170 err |= ERR_FATAL | ERR_ALERT;
171 msg = "sockpair can be only used with inherited FDs";
172 goto err_return;
173 }
174
175 if (fd >= global.maxsock) {
176 err |= ERR_FATAL | ERR_ALERT;
177 msg = "socket(): not enough free sockets, raise -n argument";
178 goto err_return;
179 }
180 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
181 err |= ERR_FATAL | ERR_ALERT;
182 msg = "cannot make sockpair non-blocking";
183 goto err_return;
184 }
Willy Tarreau0b915012020-09-01 10:47:07 +0200185 listener->rx.flags |= RX_F_BOUND;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200186
Willy Tarreau0b915012020-09-01 10:47:07 +0200187 bound:
William Lallemand2fe7dd02018-09-11 16:51:29 +0200188 listener->state = LI_LISTEN;
189
Willy Tarreaub7436612020-08-28 19:51:44 +0200190 fd_insert(fd, listener, listener->rx.proto->accept,
Willy Tarreau818a92e2020-09-03 07:50:19 +0200191 thread_mask(listener->rx.settings->bind_thread) & all_threads_mask);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200192
193 return err;
194
195 err_return:
196 if (msg && errlen)
197 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
198 return err;
199}
200
201/*
202 * Send FD over a unix socket
203 *
204 * <send_fd> is the FD to send
205 * <fd> is the fd of the unix socket to use for the transfer
206 *
207 * The iobuf variable could be use in the future to enhance the protocol.
208 */
209int send_fd_uxst(int fd, int send_fd)
210{
211 char iobuf[2];
212 struct iovec iov;
213 struct msghdr msghdr;
214
215 char cmsgbuf[CMSG_SPACE(sizeof(int))];
216 char buf[CMSG_SPACE(sizeof(int))];
217 struct cmsghdr *cmsg = (void *)buf;
218
219 int *fdptr;
220
221 iov.iov_base = iobuf;
222 iov.iov_len = sizeof(iobuf);
223
224 memset(&msghdr, 0, sizeof(msghdr));
225 msghdr.msg_iov = &iov;
226 msghdr.msg_iovlen = 1;
227
228 /* Now send the fds */
229 msghdr.msg_control = cmsgbuf;
230 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
231
232 cmsg = CMSG_FIRSTHDR(&msghdr);
233 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
234 cmsg->cmsg_level = SOL_SOCKET;
235 cmsg->cmsg_type = SCM_RIGHTS;
236
237 fdptr = (int *)CMSG_DATA(cmsg);
238 memcpy(fdptr, &send_fd, sizeof(send_fd));
239
240 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
241 ha_warning("Failed to transfer socket\n");
242 return 1;
243 }
244
245 return 0;
246}
247
248/*
249 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800250 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200251 * socket and establishing a connection, it creates a pair of connected
252 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200253 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200254 *
255 * conn->target may point either to a valid server or to a backend, depending
256 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
257 * <data> parameter is a boolean indicating whether there are data waiting for
258 * being sent or not, in order to adjust data write polling and on some
259 * platforms. The <delack> argument is ignored.
260 *
261 * Note that a pending send_proxy message accounts for data.
262 *
263 * It can return one of :
264 * - SF_ERR_NONE if everything's OK
265 * - SF_ERR_SRVTO if there are no more servers
266 * - SF_ERR_SRVCL if the connection was refused by the server
267 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
268 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
269 * - SF_ERR_INTERNAL for any other purely internal errors
270 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
271 *
272 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
273 * it's invalid and the caller has nothing to do.
274 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200275static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200276{
277 int sv[2], fd, dst_fd = -1;
278
279 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200280 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200281
William Lallemand2fe7dd02018-09-11 16:51:29 +0200282 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
283 obj_type(conn->target) != OBJ_TYPE_SERVER) {
284 conn->flags |= CO_FL_ERROR;
285 return SF_ERR_INTERNAL;
286 }
287
288 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
289 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
290 conn->flags |= CO_FL_ERROR;
291 return SF_ERR_RESOURCE;
292 }
293
294 fd = conn->handle.fd = sv[1];
295
296 if (fd >= global.maxsock) {
297 /* do not log anything there, it's a normal condition when this option
298 * is used to serialize connections to a server !
299 */
300 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
301 close(sv[0]);
302 close(sv[1]);
303 conn->err_code = CO_ER_CONF_FDLIM;
304 conn->flags |= CO_FL_ERROR;
305 return SF_ERR_PRXCOND; /* it is a configuration limit */
306 }
307
308 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
309 qfprintf(stderr,"Cannot set client socket to non blocking mode.\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 Lallemandc03eb012018-11-27 12:02:37 +0100317 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
318 ha_alert("Cannot set CLOEXEC on client socket.\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 Lallemand2fe7dd02018-09-11 16:51:29 +0200326 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200327 if (conn->send_proxy_ofs)
328 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200329
330 if (global.tune.server_sndbuf)
331 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
332
333 if (global.tune.server_rcvbuf)
334 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
335
336 /* The new socket is sent on the other side, it should be retrieved and
337 * considered as an 'accept' socket on the server side */
338 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
339 close(sv[0]);
340 close(sv[1]);
341 conn->err_code = CO_ER_SOCK_ERR;
342 conn->flags |= CO_FL_ERROR;
343 return SF_ERR_INTERNAL;
344 }
345
346 close(sv[0]); /* we don't need this side anymore */
347
348 conn->flags &= ~CO_FL_WAIT_L4_CONN;
349
350 conn->flags |= CO_FL_ADDR_TO_SET;
351
352 /* Prepare to send a few handshakes related to the on-wire protocol. */
353 if (conn->send_proxy_ofs)
354 conn->flags |= CO_FL_SEND_PROXY;
355
356 conn_ctrl_init(conn); /* registers the FD */
357 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
358
359 if (conn_xprt_init(conn) < 0) {
360 conn_full_close(conn);
361 conn->flags |= CO_FL_ERROR;
362 return SF_ERR_RESOURCE;
363 }
364
William Lallemand2fe7dd02018-09-11 16:51:29 +0200365 return SF_ERR_NONE; /* connection is OK */
366}
367
368
369/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800370 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200371 *
372 * Return -1 or a socket fd;
373 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800374 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200375 */
376int recv_fd_uxst(int sock)
377{
378 struct msghdr msghdr;
379 struct iovec iov;
380 char iobuf[2];
381
382 char cmsgbuf[CMSG_SPACE(sizeof(int))];
383 char buf[CMSG_SPACE(sizeof(int))];
384 struct cmsghdr *cmsg = (void *)buf;
385
386
387 int recv_fd = -1;
388 int ret = -1;
389
390 memset(&msghdr, 0, sizeof(msghdr));
391
392 iov.iov_base = iobuf;
393 iov.iov_len = sizeof(iobuf);
394
395 msghdr.msg_iov = &iov;
396 msghdr.msg_iovlen = 1;
397
398 msghdr.msg_control = cmsgbuf;
399 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
400
401 iov.iov_len = sizeof(iobuf);
402 iov.iov_base = iobuf;
403
404 while (1) {
405 ret = recvmsg(sock, &msghdr, 0);
406 if (ret == -1 && errno == EINTR)
407 continue;
408 else
409 break;
410 }
411
412 if (ret == -1)
413 return ret;
414
415 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200416 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200417 cmsg->cmsg_type == SCM_RIGHTS) {
418 size_t totlen = cmsg->cmsg_len -
419 CMSG_LEN(0);
420 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
421 }
422 return recv_fd;
423}
424
William Lallemand2fe7dd02018-09-11 16:51:29 +0200425/*
426 * Local variables:
427 * c-indent-level: 8
428 * c-basic-offset: 8
429 * End:
430 */