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