blob: 4a5701c8cf8e3e656d12e95c934df32e5cfde6d7 [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);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020047static int sockpair_connect_server(struct connection *conn, int flags);
William Lallemand2fe7dd02018-09-11 16:51:29 +020048
Willy Tarreaub0254cb2020-09-04 08:07:11 +020049struct proto_fam proto_fam_sockpair = {
50 .name = "sockpair",
51 .sock_domain = AF_CUST_SOCKPAIR,
52 .sock_family = AF_UNIX,
53 .sock_addrlen = sizeof(struct sockaddr_un),
54 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
55 .addrcmp = NULL,
56 .bind = sockpair_bind_receiver,
57 .get_src = NULL,
58 .get_dst = NULL,
59};
60
William Lallemand2fe7dd02018-09-11 16:51:29 +020061/* Note: must not be declared <const> as its list will be overwritten */
62static struct protocol proto_sockpair = {
63 .name = "sockpair",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020064 .fam = &proto_fam_sockpair,
Willy Tarreaua54553f2020-09-16 17:50:45 +020065 .ctrl_type = SOCK_STREAM,
William Lallemand2fe7dd02018-09-11 16:51:29 +020066 .sock_domain = AF_CUST_SOCKPAIR,
67 .sock_type = SOCK_STREAM,
68 .sock_prot = 0,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020069 .add = sockpair_add_listener,
70 .listen = sockpair_bind_listener,
Willy Tarreau686fa3d2020-09-25 19:09:53 +020071 .rx_enable = sock_enable,
72 .rx_disable = sock_disable,
William Lallemand2fe7dd02018-09-11 16:51:29 +020073 .accept = &listener_accept,
74 .connect = &sockpair_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020075 .receivers = LIST_HEAD_INIT(proto_sockpair.receivers),
76 .nb_receivers = 0,
William Lallemand2fe7dd02018-09-11 16:51:29 +020077};
78
Willy Tarreau0108d902018-11-25 19:14:37 +010079INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
80
William Lallemand2fe7dd02018-09-11 16:51:29 +020081/* Add <listener> to the list of sockpair listeners (port is ignored). The
82 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
83 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020084 *
85 * Must be called with proto_lock held.
86 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020087 */
88static void sockpair_add_listener(struct listener *listener, int port)
89{
90 if (listener->state != LI_INIT)
91 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +020092 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +020093 listener->rx.proto = &proto_sockpair;
Willy Tarreaud7f331c2020-09-25 17:01:43 +020094 LIST_ADDQ(&proto_sockpair.receivers, &listener->rx.proto_list);
95 proto_sockpair.nb_receivers++;
William Lallemand2fe7dd02018-09-11 16:51:29 +020096}
97
Willy Tarreau62292b22020-09-02 17:52:23 +020098/* Binds receiver <rx>, and assigns <handler> and rx->owner as the callback and
99 * context, respectively, with <tm> as the thread mask. Returns and error code
100 * made of ERR_* bits on failure or ERR_NONE on success. On failure, an error
101 * message may be passed into <errmsg>. Note that the binding address is only
102 * an FD to receive the incoming FDs on. Thus by definition there is no real
103 * "bind" operation, this only completes the receiver. Such FDs are not
104 * inherited upon reload.
105 */
106int sockpair_bind_receiver(struct receiver *rx, void (*handler)(int fd), char **errmsg)
107{
108 int err;
109
110 /* ensure we never return garbage */
111 if (errmsg)
112 *errmsg = 0;
113
114 err = ERR_NONE;
115
116 if (rx->flags & RX_F_BOUND)
117 return ERR_NONE;
118
119 if (rx->fd == -1) {
120 err |= ERR_FATAL | ERR_ALERT;
121 memprintf(errmsg, "sockpair may be only used with inherited FDs");
122 goto bind_return;
123 }
124
125 if (rx->fd >= global.maxsock) {
126 err |= ERR_FATAL | ERR_ABORT | ERR_ALERT;
127 memprintf(errmsg, "not enough free sockets (raise '-n' parameter)");
128 goto bind_close_return;
129 }
130
131 if (fcntl(rx->fd, F_SETFL, O_NONBLOCK) == -1) {
132 err |= ERR_FATAL | ERR_ALERT;
133 memprintf(errmsg, "cannot make socket non-blocking");
134 goto bind_close_return;
135 }
136
137 rx->flags |= RX_F_BOUND;
138
139 fd_insert(rx->fd, rx->owner, handler, thread_mask(rx->settings->bind_thread) & all_threads_mask);
140 return err;
141
142 bind_return:
143 if (errmsg && *errmsg)
144 memprintf(errmsg, "%s [fd %d]", *errmsg, rx->fd);
145
146 return err;
147
148 bind_close_return:
149 close(rx->fd);
150 goto bind_return;
151}
152
William Lallemand2fe7dd02018-09-11 16:51:29 +0200153/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
154 * enabled for polling. The return value is composed from ERR_NONE,
155 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
156 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
157 * Note that <errmsg> may be NULL if <errlen> is also zero.
158 */
159static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
160{
William Lallemand2fe7dd02018-09-11 16:51:29 +0200161 int err;
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200162 char *msg = NULL;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200163
164 err = ERR_NONE;
165
166 /* ensure we never return garbage */
167 if (errlen)
168 *errmsg = 0;
169
170 if (listener->state != LI_ASSIGNED)
171 return ERR_NONE; /* already bound */
172
Willy Tarreauad33acf2020-09-02 18:40:02 +0200173 if (!(listener->rx.flags & RX_F_BOUND)) {
174 msg = "receiving socket not bound";
175 goto err_return;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200176 }
Willy Tarreauad33acf2020-09-02 18:40:02 +0200177
Willy Tarreaua37b2442020-09-24 07:23:45 +0200178 listener_set_state(listener, LI_LISTEN);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200179 return err;
180
181 err_return:
182 if (msg && errlen)
Willy Tarreau9eda7a62020-09-02 18:02:00 +0200183 snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200184 return err;
185}
186
187/*
188 * Send FD over a unix socket
189 *
190 * <send_fd> is the FD to send
191 * <fd> is the fd of the unix socket to use for the transfer
192 *
193 * The iobuf variable could be use in the future to enhance the protocol.
194 */
195int send_fd_uxst(int fd, int send_fd)
196{
197 char iobuf[2];
198 struct iovec iov;
199 struct msghdr msghdr;
200
201 char cmsgbuf[CMSG_SPACE(sizeof(int))];
202 char buf[CMSG_SPACE(sizeof(int))];
203 struct cmsghdr *cmsg = (void *)buf;
204
205 int *fdptr;
206
207 iov.iov_base = iobuf;
208 iov.iov_len = sizeof(iobuf);
209
210 memset(&msghdr, 0, sizeof(msghdr));
211 msghdr.msg_iov = &iov;
212 msghdr.msg_iovlen = 1;
213
214 /* Now send the fds */
215 msghdr.msg_control = cmsgbuf;
216 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
217
218 cmsg = CMSG_FIRSTHDR(&msghdr);
219 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
220 cmsg->cmsg_level = SOL_SOCKET;
221 cmsg->cmsg_type = SCM_RIGHTS;
222
223 fdptr = (int *)CMSG_DATA(cmsg);
224 memcpy(fdptr, &send_fd, sizeof(send_fd));
225
226 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
227 ha_warning("Failed to transfer socket\n");
228 return 1;
229 }
230
231 return 0;
232}
233
234/*
235 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800236 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200237 * socket and establishing a connection, it creates a pair of connected
238 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200239 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200240 *
241 * conn->target may point either to a valid server or to a backend, depending
242 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
243 * <data> parameter is a boolean indicating whether there are data waiting for
244 * being sent or not, in order to adjust data write polling and on some
245 * platforms. The <delack> argument is ignored.
246 *
247 * Note that a pending send_proxy message accounts for data.
248 *
249 * It can return one of :
250 * - SF_ERR_NONE if everything's OK
251 * - SF_ERR_SRVTO if there are no more servers
252 * - SF_ERR_SRVCL if the connection was refused by the server
253 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
254 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
255 * - SF_ERR_INTERNAL for any other purely internal errors
256 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
257 *
258 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
259 * it's invalid and the caller has nothing to do.
260 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200261static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200262{
263 int sv[2], fd, dst_fd = -1;
264
265 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200266 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200267
William Lallemand2fe7dd02018-09-11 16:51:29 +0200268 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
269 obj_type(conn->target) != OBJ_TYPE_SERVER) {
270 conn->flags |= CO_FL_ERROR;
271 return SF_ERR_INTERNAL;
272 }
273
274 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
275 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
276 conn->flags |= CO_FL_ERROR;
277 return SF_ERR_RESOURCE;
278 }
279
280 fd = conn->handle.fd = sv[1];
281
282 if (fd >= global.maxsock) {
283 /* do not log anything there, it's a normal condition when this option
284 * is used to serialize connections to a server !
285 */
286 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
287 close(sv[0]);
288 close(sv[1]);
289 conn->err_code = CO_ER_CONF_FDLIM;
290 conn->flags |= CO_FL_ERROR;
291 return SF_ERR_PRXCOND; /* it is a configuration limit */
292 }
293
294 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
295 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
296 close(sv[0]);
297 close(sv[1]);
298 conn->err_code = CO_ER_SOCK_ERR;
299 conn->flags |= CO_FL_ERROR;
300 return SF_ERR_INTERNAL;
301 }
302
William Lallemandc03eb012018-11-27 12:02:37 +0100303 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
304 ha_alert("Cannot set CLOEXEC on client socket.\n");
305 close(sv[0]);
306 close(sv[1]);
307 conn->err_code = CO_ER_SOCK_ERR;
308 conn->flags |= CO_FL_ERROR;
309 return SF_ERR_INTERNAL;
310 }
311
William Lallemand2fe7dd02018-09-11 16:51:29 +0200312 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200313 if (conn->send_proxy_ofs)
314 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200315
316 if (global.tune.server_sndbuf)
317 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
318
319 if (global.tune.server_rcvbuf)
320 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
321
322 /* The new socket is sent on the other side, it should be retrieved and
323 * considered as an 'accept' socket on the server side */
324 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
325 close(sv[0]);
326 close(sv[1]);
327 conn->err_code = CO_ER_SOCK_ERR;
328 conn->flags |= CO_FL_ERROR;
329 return SF_ERR_INTERNAL;
330 }
331
332 close(sv[0]); /* we don't need this side anymore */
333
334 conn->flags &= ~CO_FL_WAIT_L4_CONN;
335
336 conn->flags |= CO_FL_ADDR_TO_SET;
337
338 /* Prepare to send a few handshakes related to the on-wire protocol. */
339 if (conn->send_proxy_ofs)
340 conn->flags |= CO_FL_SEND_PROXY;
341
342 conn_ctrl_init(conn); /* registers the FD */
343 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
344
345 if (conn_xprt_init(conn) < 0) {
346 conn_full_close(conn);
347 conn->flags |= CO_FL_ERROR;
348 return SF_ERR_RESOURCE;
349 }
350
William Lallemand2fe7dd02018-09-11 16:51:29 +0200351 return SF_ERR_NONE; /* connection is OK */
352}
353
354
355/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800356 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200357 *
358 * Return -1 or a socket fd;
359 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800360 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200361 */
362int recv_fd_uxst(int sock)
363{
364 struct msghdr msghdr;
365 struct iovec iov;
366 char iobuf[2];
367
368 char cmsgbuf[CMSG_SPACE(sizeof(int))];
369 char buf[CMSG_SPACE(sizeof(int))];
370 struct cmsghdr *cmsg = (void *)buf;
371
372
373 int recv_fd = -1;
374 int ret = -1;
375
376 memset(&msghdr, 0, sizeof(msghdr));
377
378 iov.iov_base = iobuf;
379 iov.iov_len = sizeof(iobuf);
380
381 msghdr.msg_iov = &iov;
382 msghdr.msg_iovlen = 1;
383
384 msghdr.msg_control = cmsgbuf;
385 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
386
387 iov.iov_len = sizeof(iobuf);
388 iov.iov_base = iobuf;
389
390 while (1) {
391 ret = recvmsg(sock, &msghdr, 0);
392 if (ret == -1 && errno == EINTR)
393 continue;
394 else
395 break;
396 }
397
398 if (ret == -1)
399 return ret;
400
401 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200402 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200403 cmsg->cmsg_type == SCM_RIGHTS) {
404 size_t totlen = cmsg->cmsg_len -
405 CMSG_LEN(0);
406 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
407 }
408 return recv_fd;
409}
410
William Lallemand2fe7dd02018-09-11 16:51:29 +0200411/*
412 * Local variables:
413 * c-indent-level: 8
414 * c-basic-offset: 8
415 * End:
416 */