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