blob: cb1853681b7badb31054c7efc0c448d9a09619fe [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 Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020033#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020034#include <haproxy/listener.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020035#include <haproxy/tools.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020036#include <haproxy/time.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020037#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020038
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020039#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020040#include <haproxy/freq_ctr.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020041#include <proto/log.h>
Willy Tarreau2dd7c352020-06-03 15:26:55 +020042#include <haproxy/protocol.h>
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);
46static int sockpair_bind_listeners(struct protocol *proto, 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
49/* Note: must not be declared <const> as its list will be overwritten */
50static struct protocol proto_sockpair = {
51 .name = "sockpair",
52 .sock_domain = AF_CUST_SOCKPAIR,
53 .sock_type = SOCK_STREAM,
54 .sock_prot = 0,
55 .sock_family = AF_UNIX,
56 .sock_addrlen = sizeof(struct sockaddr_un),
57 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
58 .accept = &listener_accept,
59 .connect = &sockpair_connect_server,
60 .bind = sockpair_bind_listener,
61 .bind_all = sockpair_bind_listeners,
62 .unbind_all = NULL,
63 .enable_all = enable_all_listeners,
64 .disable_all = disable_all_listeners,
65 .get_src = NULL,
66 .get_dst = NULL,
67 .pause = NULL,
68 .add = sockpair_add_listener,
69 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
70 .nb_listeners = 0,
71};
72
Willy Tarreau0108d902018-11-25 19:14:37 +010073INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
74
William Lallemand2fe7dd02018-09-11 16:51:29 +020075/* Add <listener> to the list of sockpair listeners (port is ignored). The
76 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
77 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020078 *
79 * Must be called with proto_lock held.
80 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020081 */
82static void sockpair_add_listener(struct listener *listener, int port)
83{
84 if (listener->state != LI_INIT)
85 return;
86 listener->state = LI_ASSIGNED;
87 listener->proto = &proto_sockpair;
88 LIST_ADDQ(&proto_sockpair.listeners, &listener->proto_list);
89 proto_sockpair.nb_listeners++;
90}
91
92/* This function creates all UNIX sockets bound to the protocol entry <proto>.
93 * It is intended to be used as the protocol's bind_all() function.
94 * The sockets will be registered but not added to any fd_set, in order not to
95 * loose them across the fork(). A call to uxst_enable_listeners() is needed
96 * to complete initialization.
97 *
Willy Tarreaudaacf362019-07-24 16:45:02 +020098 * Must be called with proto_lock held.
99 *
William Lallemand2fe7dd02018-09-11 16:51:29 +0200100 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
101 */
102static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
103{
104 struct listener *listener;
105 int err = ERR_NONE;
106
107 list_for_each_entry(listener, &proto->listeners, proto_list) {
108 err |= sockpair_bind_listener(listener, errmsg, errlen);
109 if (err & ERR_ABORT)
110 break;
111 }
112 return err;
113}
114
115/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
116 * enabled for polling. The return value is composed from ERR_NONE,
117 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
118 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
119 * Note that <errmsg> may be NULL if <errlen> is also zero.
120 */
121static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
122{
123 int fd = listener->fd;
124 int err;
125 const char *msg = NULL;
126
127 err = ERR_NONE;
128
129 /* ensure we never return garbage */
130 if (errlen)
131 *errmsg = 0;
132
133 if (listener->state != LI_ASSIGNED)
134 return ERR_NONE; /* already bound */
135
136 if (listener->fd == -1) {
137 err |= ERR_FATAL | ERR_ALERT;
138 msg = "sockpair can be only used with inherited FDs";
139 goto err_return;
140 }
141
142 if (fd >= global.maxsock) {
143 err |= ERR_FATAL | ERR_ALERT;
144 msg = "socket(): not enough free sockets, raise -n argument";
145 goto err_return;
146 }
147 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
148 err |= ERR_FATAL | ERR_ALERT;
149 msg = "cannot make sockpair non-blocking";
150 goto err_return;
151 }
152
153 listener->state = LI_LISTEN;
154
155 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100156 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200157
158 return err;
159
160 err_return:
161 if (msg && errlen)
162 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
163 return err;
164}
165
166/*
167 * Send FD over a unix socket
168 *
169 * <send_fd> is the FD to send
170 * <fd> is the fd of the unix socket to use for the transfer
171 *
172 * The iobuf variable could be use in the future to enhance the protocol.
173 */
174int send_fd_uxst(int fd, int send_fd)
175{
176 char iobuf[2];
177 struct iovec iov;
178 struct msghdr msghdr;
179
180 char cmsgbuf[CMSG_SPACE(sizeof(int))];
181 char buf[CMSG_SPACE(sizeof(int))];
182 struct cmsghdr *cmsg = (void *)buf;
183
184 int *fdptr;
185
186 iov.iov_base = iobuf;
187 iov.iov_len = sizeof(iobuf);
188
189 memset(&msghdr, 0, sizeof(msghdr));
190 msghdr.msg_iov = &iov;
191 msghdr.msg_iovlen = 1;
192
193 /* Now send the fds */
194 msghdr.msg_control = cmsgbuf;
195 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
196
197 cmsg = CMSG_FIRSTHDR(&msghdr);
198 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
199 cmsg->cmsg_level = SOL_SOCKET;
200 cmsg->cmsg_type = SCM_RIGHTS;
201
202 fdptr = (int *)CMSG_DATA(cmsg);
203 memcpy(fdptr, &send_fd, sizeof(send_fd));
204
205 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
206 ha_warning("Failed to transfer socket\n");
207 return 1;
208 }
209
210 return 0;
211}
212
213/*
214 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800215 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200216 * socket and establishing a connection, it creates a pair of connected
217 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200218 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200219 *
220 * conn->target may point either to a valid server or to a backend, depending
221 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
222 * <data> parameter is a boolean indicating whether there are data waiting for
223 * being sent or not, in order to adjust data write polling and on some
224 * platforms. The <delack> argument is ignored.
225 *
226 * Note that a pending send_proxy message accounts for data.
227 *
228 * It can return one of :
229 * - SF_ERR_NONE if everything's OK
230 * - SF_ERR_SRVTO if there are no more servers
231 * - SF_ERR_SRVCL if the connection was refused by the server
232 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
233 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
234 * - SF_ERR_INTERNAL for any other purely internal errors
235 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
236 *
237 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
238 * it's invalid and the caller has nothing to do.
239 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200240static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200241{
242 int sv[2], fd, dst_fd = -1;
243
244 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200245 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200246
William Lallemand2fe7dd02018-09-11 16:51:29 +0200247 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
248 obj_type(conn->target) != OBJ_TYPE_SERVER) {
249 conn->flags |= CO_FL_ERROR;
250 return SF_ERR_INTERNAL;
251 }
252
253 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
254 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
255 conn->flags |= CO_FL_ERROR;
256 return SF_ERR_RESOURCE;
257 }
258
259 fd = conn->handle.fd = sv[1];
260
261 if (fd >= global.maxsock) {
262 /* do not log anything there, it's a normal condition when this option
263 * is used to serialize connections to a server !
264 */
265 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
266 close(sv[0]);
267 close(sv[1]);
268 conn->err_code = CO_ER_CONF_FDLIM;
269 conn->flags |= CO_FL_ERROR;
270 return SF_ERR_PRXCOND; /* it is a configuration limit */
271 }
272
273 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
274 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
275 close(sv[0]);
276 close(sv[1]);
277 conn->err_code = CO_ER_SOCK_ERR;
278 conn->flags |= CO_FL_ERROR;
279 return SF_ERR_INTERNAL;
280 }
281
William Lallemandc03eb012018-11-27 12:02:37 +0100282 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
283 ha_alert("Cannot set CLOEXEC on client socket.\n");
284 close(sv[0]);
285 close(sv[1]);
286 conn->err_code = CO_ER_SOCK_ERR;
287 conn->flags |= CO_FL_ERROR;
288 return SF_ERR_INTERNAL;
289 }
290
William Lallemand2fe7dd02018-09-11 16:51:29 +0200291 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200292 if (conn->send_proxy_ofs)
293 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200294
295 if (global.tune.server_sndbuf)
296 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
297
298 if (global.tune.server_rcvbuf)
299 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
300
301 /* The new socket is sent on the other side, it should be retrieved and
302 * considered as an 'accept' socket on the server side */
303 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
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
311 close(sv[0]); /* we don't need this side anymore */
312
313 conn->flags &= ~CO_FL_WAIT_L4_CONN;
314
315 conn->flags |= CO_FL_ADDR_TO_SET;
316
317 /* Prepare to send a few handshakes related to the on-wire protocol. */
318 if (conn->send_proxy_ofs)
319 conn->flags |= CO_FL_SEND_PROXY;
320
321 conn_ctrl_init(conn); /* registers the FD */
322 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
323
324 if (conn_xprt_init(conn) < 0) {
325 conn_full_close(conn);
326 conn->flags |= CO_FL_ERROR;
327 return SF_ERR_RESOURCE;
328 }
329
William Lallemand2fe7dd02018-09-11 16:51:29 +0200330 return SF_ERR_NONE; /* connection is OK */
331}
332
333
334/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800335 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200336 *
337 * Return -1 or a socket fd;
338 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800339 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200340 */
341int recv_fd_uxst(int sock)
342{
343 struct msghdr msghdr;
344 struct iovec iov;
345 char iobuf[2];
346
347 char cmsgbuf[CMSG_SPACE(sizeof(int))];
348 char buf[CMSG_SPACE(sizeof(int))];
349 struct cmsghdr *cmsg = (void *)buf;
350
351
352 int recv_fd = -1;
353 int ret = -1;
354
355 memset(&msghdr, 0, sizeof(msghdr));
356
357 iov.iov_base = iobuf;
358 iov.iov_len = sizeof(iobuf);
359
360 msghdr.msg_iov = &iov;
361 msghdr.msg_iovlen = 1;
362
363 msghdr.msg_control = cmsgbuf;
364 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
365
366 iov.iov_len = sizeof(iobuf);
367 iov.iov_base = iobuf;
368
369 while (1) {
370 ret = recvmsg(sock, &msghdr, 0);
371 if (ret == -1 && errno == EINTR)
372 continue;
373 else
374 break;
375 }
376
377 if (ret == -1)
378 return ret;
379
380 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200381 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200382 cmsg->cmsg_type == SCM_RIGHTS) {
383 size_t totlen = cmsg->cmsg_len -
384 CMSG_LEN(0);
385 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
386 }
387 return recv_fd;
388}
389
William Lallemand2fe7dd02018-09-11 16:51:29 +0200390/*
391 * Local variables:
392 * c-indent-level: 8
393 * c-basic-offset: 8
394 * End:
395 */