blob: 00fce76cbff73b03e2ce65db2ba3bd9707daa30b [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{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200153 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200154 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200155
156 err = ERR_NONE;
157
158 /* ensure we never return garbage */
159 if (errlen)
160 *errmsg = 0;
161
162 if (listener->state != LI_ASSIGNED)
163 return ERR_NONE; /* already bound */
164
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200165 err = sockpair_bind_receiver(&listener->rx, listener->rx.proto->accept, &msg);
166 if (err != ERR_NONE) {
167 snprintf(errmsg, errlen, "%s", msg);
168 free(msg); msg = NULL;
169 return err;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200170 }
William Lallemand2fe7dd02018-09-11 16:51:29 +0200171 listener->state = LI_LISTEN;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200172 return err;
173
174 err_return:
175 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200176 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200177 return err;
178}
179
180/*
181 * Send FD over a unix socket
182 *
183 * <send_fd> is the FD to send
184 * <fd> is the fd of the unix socket to use for the transfer
185 *
186 * The iobuf variable could be use in the future to enhance the protocol.
187 */
188int send_fd_uxst(int fd, int send_fd)
189{
190 char iobuf[2];
191 struct iovec iov;
192 struct msghdr msghdr;
193
194 char cmsgbuf[CMSG_SPACE(sizeof(int))];
195 char buf[CMSG_SPACE(sizeof(int))];
196 struct cmsghdr *cmsg = (void *)buf;
197
198 int *fdptr;
199
200 iov.iov_base = iobuf;
201 iov.iov_len = sizeof(iobuf);
202
203 memset(&msghdr, 0, sizeof(msghdr));
204 msghdr.msg_iov = &iov;
205 msghdr.msg_iovlen = 1;
206
207 /* Now send the fds */
208 msghdr.msg_control = cmsgbuf;
209 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
210
211 cmsg = CMSG_FIRSTHDR(&msghdr);
212 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
213 cmsg->cmsg_level = SOL_SOCKET;
214 cmsg->cmsg_type = SCM_RIGHTS;
215
216 fdptr = (int *)CMSG_DATA(cmsg);
217 memcpy(fdptr, &send_fd, sizeof(send_fd));
218
219 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
220 ha_warning("Failed to transfer socket\n");
221 return 1;
222 }
223
224 return 0;
225}
226
227/*
228 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800229 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200230 * socket and establishing a connection, it creates a pair of connected
231 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200232 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200233 *
234 * conn->target may point either to a valid server or to a backend, depending
235 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
236 * <data> parameter is a boolean indicating whether there are data waiting for
237 * being sent or not, in order to adjust data write polling and on some
238 * platforms. The <delack> argument is ignored.
239 *
240 * Note that a pending send_proxy message accounts for data.
241 *
242 * It can return one of :
243 * - SF_ERR_NONE if everything's OK
244 * - SF_ERR_SRVTO if there are no more servers
245 * - SF_ERR_SRVCL if the connection was refused by the server
246 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
247 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
248 * - SF_ERR_INTERNAL for any other purely internal errors
249 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
250 *
251 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
252 * it's invalid and the caller has nothing to do.
253 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200254static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200255{
256 int sv[2], fd, dst_fd = -1;
257
258 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200259 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200260
William Lallemand2fe7dd02018-09-11 16:51:29 +0200261 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
262 obj_type(conn->target) != OBJ_TYPE_SERVER) {
263 conn->flags |= CO_FL_ERROR;
264 return SF_ERR_INTERNAL;
265 }
266
267 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
268 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
269 conn->flags |= CO_FL_ERROR;
270 return SF_ERR_RESOURCE;
271 }
272
273 fd = conn->handle.fd = sv[1];
274
275 if (fd >= global.maxsock) {
276 /* do not log anything there, it's a normal condition when this option
277 * is used to serialize connections to a server !
278 */
279 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
280 close(sv[0]);
281 close(sv[1]);
282 conn->err_code = CO_ER_CONF_FDLIM;
283 conn->flags |= CO_FL_ERROR;
284 return SF_ERR_PRXCOND; /* it is a configuration limit */
285 }
286
287 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
288 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
289 close(sv[0]);
290 close(sv[1]);
291 conn->err_code = CO_ER_SOCK_ERR;
292 conn->flags |= CO_FL_ERROR;
293 return SF_ERR_INTERNAL;
294 }
295
William Lallemandc03eb012018-11-27 12:02:37 +0100296 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
297 ha_alert("Cannot set CLOEXEC on client socket.\n");
298 close(sv[0]);
299 close(sv[1]);
300 conn->err_code = CO_ER_SOCK_ERR;
301 conn->flags |= CO_FL_ERROR;
302 return SF_ERR_INTERNAL;
303 }
304
William Lallemand2fe7dd02018-09-11 16:51:29 +0200305 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200306 if (conn->send_proxy_ofs)
307 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200308
309 if (global.tune.server_sndbuf)
310 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
311
312 if (global.tune.server_rcvbuf)
313 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
314
315 /* The new socket is sent on the other side, it should be retrieved and
316 * considered as an 'accept' socket on the server side */
317 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
318 close(sv[0]);
319 close(sv[1]);
320 conn->err_code = CO_ER_SOCK_ERR;
321 conn->flags |= CO_FL_ERROR;
322 return SF_ERR_INTERNAL;
323 }
324
325 close(sv[0]); /* we don't need this side anymore */
326
327 conn->flags &= ~CO_FL_WAIT_L4_CONN;
328
329 conn->flags |= CO_FL_ADDR_TO_SET;
330
331 /* Prepare to send a few handshakes related to the on-wire protocol. */
332 if (conn->send_proxy_ofs)
333 conn->flags |= CO_FL_SEND_PROXY;
334
335 conn_ctrl_init(conn); /* registers the FD */
336 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
337
338 if (conn_xprt_init(conn) < 0) {
339 conn_full_close(conn);
340 conn->flags |= CO_FL_ERROR;
341 return SF_ERR_RESOURCE;
342 }
343
William Lallemand2fe7dd02018-09-11 16:51:29 +0200344 return SF_ERR_NONE; /* connection is OK */
345}
346
347
348/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800349 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200350 *
351 * Return -1 or a socket fd;
352 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800353 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200354 */
355int recv_fd_uxst(int sock)
356{
357 struct msghdr msghdr;
358 struct iovec iov;
359 char iobuf[2];
360
361 char cmsgbuf[CMSG_SPACE(sizeof(int))];
362 char buf[CMSG_SPACE(sizeof(int))];
363 struct cmsghdr *cmsg = (void *)buf;
364
365
366 int recv_fd = -1;
367 int ret = -1;
368
369 memset(&msghdr, 0, sizeof(msghdr));
370
371 iov.iov_base = iobuf;
372 iov.iov_len = sizeof(iobuf);
373
374 msghdr.msg_iov = &iov;
375 msghdr.msg_iovlen = 1;
376
377 msghdr.msg_control = cmsgbuf;
378 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
379
380 iov.iov_len = sizeof(iobuf);
381 iov.iov_base = iobuf;
382
383 while (1) {
384 ret = recvmsg(sock, &msghdr, 0);
385 if (ret == -1 && errno == EINTR)
386 continue;
387 else
388 break;
389 }
390
391 if (ret == -1)
392 return ret;
393
394 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200395 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200396 cmsg->cmsg_type == SCM_RIGHTS) {
397 size_t totlen = cmsg->cmsg_len -
398 CMSG_LEN(0);
399 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
400 }
401 return recv_fd;
402}
403
William Lallemand2fe7dd02018-09-11 16:51:29 +0200404/*
405 * Local variables:
406 * c-indent-level: 8
407 * c-basic-offset: 8
408 * End:
409 */