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