blob: c73699511ae606f6f66314859451338ebd1cd207 [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 Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020041
William Lallemand2fe7dd02018-09-11 16:51:29 +020042
43static void sockpair_add_listener(struct listener *listener, int port);
44static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020045static int sockpair_connect_server(struct connection *conn, int flags);
William Lallemand2fe7dd02018-09-11 16:51:29 +020046
47/* Note: must not be declared <const> as its list will be overwritten */
48static struct protocol proto_sockpair = {
49 .name = "sockpair",
50 .sock_domain = AF_CUST_SOCKPAIR,
51 .sock_type = SOCK_STREAM,
52 .sock_prot = 0,
53 .sock_family = AF_UNIX,
54 .sock_addrlen = sizeof(struct sockaddr_un),
55 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
56 .accept = &listener_accept,
57 .connect = &sockpair_connect_server,
58 .bind = sockpair_bind_listener,
William Lallemand2fe7dd02018-09-11 16:51:29 +020059 .enable_all = enable_all_listeners,
60 .disable_all = disable_all_listeners,
61 .get_src = NULL,
62 .get_dst = NULL,
63 .pause = NULL,
64 .add = sockpair_add_listener,
65 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
66 .nb_listeners = 0,
67};
68
Willy Tarreau0108d902018-11-25 19:14:37 +010069INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
70
William Lallemand2fe7dd02018-09-11 16:51:29 +020071/* Add <listener> to the list of sockpair listeners (port is ignored). The
72 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
73 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +020074 *
75 * Must be called with proto_lock held.
76 *
William Lallemand2fe7dd02018-09-11 16:51:29 +020077 */
78static void sockpair_add_listener(struct listener *listener, int port)
79{
80 if (listener->state != LI_INIT)
81 return;
82 listener->state = LI_ASSIGNED;
83 listener->proto = &proto_sockpair;
84 LIST_ADDQ(&proto_sockpair.listeners, &listener->proto_list);
85 proto_sockpair.nb_listeners++;
86}
87
William Lallemand2fe7dd02018-09-11 16:51:29 +020088/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
89 * enabled for polling. The return value is composed from ERR_NONE,
90 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
91 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
92 * Note that <errmsg> may be NULL if <errlen> is also zero.
93 */
94static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
95{
Willy Tarreau38ba6472020-08-27 08:16:52 +020096 int fd = listener->rx.fd;
William Lallemand2fe7dd02018-09-11 16:51:29 +020097 int err;
98 const char *msg = NULL;
99
100 err = ERR_NONE;
101
102 /* ensure we never return garbage */
103 if (errlen)
104 *errmsg = 0;
105
106 if (listener->state != LI_ASSIGNED)
107 return ERR_NONE; /* already bound */
108
Willy Tarreau38ba6472020-08-27 08:16:52 +0200109 if (listener->rx.fd == -1) {
William Lallemand2fe7dd02018-09-11 16:51:29 +0200110 err |= ERR_FATAL | ERR_ALERT;
111 msg = "sockpair can be only used with inherited FDs";
112 goto err_return;
113 }
114
115 if (fd >= global.maxsock) {
116 err |= ERR_FATAL | ERR_ALERT;
117 msg = "socket(): not enough free sockets, raise -n argument";
118 goto err_return;
119 }
120 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
121 err |= ERR_FATAL | ERR_ALERT;
122 msg = "cannot make sockpair non-blocking";
123 goto err_return;
124 }
125
126 listener->state = LI_LISTEN;
127
128 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreaue26993c2020-09-03 07:18:55 +0200129 thread_mask(listener->bind_conf->settings.bind_thread) & all_threads_mask);
William Lallemand2fe7dd02018-09-11 16:51:29 +0200130
131 return err;
132
133 err_return:
134 if (msg && errlen)
135 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
136 return err;
137}
138
139/*
140 * Send FD over a unix socket
141 *
142 * <send_fd> is the FD to send
143 * <fd> is the fd of the unix socket to use for the transfer
144 *
145 * The iobuf variable could be use in the future to enhance the protocol.
146 */
147int send_fd_uxst(int fd, int send_fd)
148{
149 char iobuf[2];
150 struct iovec iov;
151 struct msghdr msghdr;
152
153 char cmsgbuf[CMSG_SPACE(sizeof(int))];
154 char buf[CMSG_SPACE(sizeof(int))];
155 struct cmsghdr *cmsg = (void *)buf;
156
157 int *fdptr;
158
159 iov.iov_base = iobuf;
160 iov.iov_len = sizeof(iobuf);
161
162 memset(&msghdr, 0, sizeof(msghdr));
163 msghdr.msg_iov = &iov;
164 msghdr.msg_iovlen = 1;
165
166 /* Now send the fds */
167 msghdr.msg_control = cmsgbuf;
168 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
169
170 cmsg = CMSG_FIRSTHDR(&msghdr);
171 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
172 cmsg->cmsg_level = SOL_SOCKET;
173 cmsg->cmsg_type = SCM_RIGHTS;
174
175 fdptr = (int *)CMSG_DATA(cmsg);
176 memcpy(fdptr, &send_fd, sizeof(send_fd));
177
178 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
179 ha_warning("Failed to transfer socket\n");
180 return 1;
181 }
182
183 return 0;
184}
185
186/*
187 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800188 * This function works like uxst_connect_server but instead of creating a
William Lallemand2fe7dd02018-09-11 16:51:29 +0200189 * socket and establishing a connection, it creates a pair of connected
190 * sockets, and send one of them through the destination FD. The destination FD
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200191 * is stored in conn->dst->sin_addr.s_addr during configuration parsing.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200192 *
193 * conn->target may point either to a valid server or to a backend, depending
194 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
195 * <data> parameter is a boolean indicating whether there are data waiting for
196 * being sent or not, in order to adjust data write polling and on some
197 * platforms. The <delack> argument is ignored.
198 *
199 * Note that a pending send_proxy message accounts for data.
200 *
201 * It can return one of :
202 * - SF_ERR_NONE if everything's OK
203 * - SF_ERR_SRVTO if there are no more servers
204 * - SF_ERR_SRVCL if the connection was refused by the server
205 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
206 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
207 * - SF_ERR_INTERNAL for any other purely internal errors
208 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
209 *
210 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
211 * it's invalid and the caller has nothing to do.
212 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200213static int sockpair_connect_server(struct connection *conn, int flags)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200214{
215 int sv[2], fd, dst_fd = -1;
216
217 /* the FD is stored in the sockaddr struct */
Willy Tarreau3f4fa092019-07-17 16:42:04 +0200218 dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200219
William Lallemand2fe7dd02018-09-11 16:51:29 +0200220 if (obj_type(conn->target) != OBJ_TYPE_PROXY &&
221 obj_type(conn->target) != OBJ_TYPE_SERVER) {
222 conn->flags |= CO_FL_ERROR;
223 return SF_ERR_INTERNAL;
224 }
225
226 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
227 ha_alert("socketpair(): Cannot create socketpair. Giving up.\n");
228 conn->flags |= CO_FL_ERROR;
229 return SF_ERR_RESOURCE;
230 }
231
232 fd = conn->handle.fd = sv[1];
233
234 if (fd >= global.maxsock) {
235 /* do not log anything there, it's a normal condition when this option
236 * is used to serialize connections to a server !
237 */
238 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
239 close(sv[0]);
240 close(sv[1]);
241 conn->err_code = CO_ER_CONF_FDLIM;
242 conn->flags |= CO_FL_ERROR;
243 return SF_ERR_PRXCOND; /* it is a configuration limit */
244 }
245
246 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
247 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
248 close(sv[0]);
249 close(sv[1]);
250 conn->err_code = CO_ER_SOCK_ERR;
251 conn->flags |= CO_FL_ERROR;
252 return SF_ERR_INTERNAL;
253 }
254
William Lallemandc03eb012018-11-27 12:02:37 +0100255 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
256 ha_alert("Cannot set CLOEXEC on client socket.\n");
257 close(sv[0]);
258 close(sv[1]);
259 conn->err_code = CO_ER_SOCK_ERR;
260 conn->flags |= CO_FL_ERROR;
261 return SF_ERR_INTERNAL;
262 }
263
William Lallemand2fe7dd02018-09-11 16:51:29 +0200264 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200265 if (conn->send_proxy_ofs)
266 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200267
268 if (global.tune.server_sndbuf)
269 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
270
271 if (global.tune.server_rcvbuf)
272 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
273
274 /* The new socket is sent on the other side, it should be retrieved and
275 * considered as an 'accept' socket on the server side */
276 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
277 close(sv[0]);
278 close(sv[1]);
279 conn->err_code = CO_ER_SOCK_ERR;
280 conn->flags |= CO_FL_ERROR;
281 return SF_ERR_INTERNAL;
282 }
283
284 close(sv[0]); /* we don't need this side anymore */
285
286 conn->flags &= ~CO_FL_WAIT_L4_CONN;
287
288 conn->flags |= CO_FL_ADDR_TO_SET;
289
290 /* Prepare to send a few handshakes related to the on-wire protocol. */
291 if (conn->send_proxy_ofs)
292 conn->flags |= CO_FL_SEND_PROXY;
293
294 conn_ctrl_init(conn); /* registers the FD */
295 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
296
297 if (conn_xprt_init(conn) < 0) {
298 conn_full_close(conn);
299 conn->flags |= CO_FL_ERROR;
300 return SF_ERR_RESOURCE;
301 }
302
William Lallemand2fe7dd02018-09-11 16:51:29 +0200303 return SF_ERR_NONE; /* connection is OK */
304}
305
306
307/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800308 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200309 *
310 * Return -1 or a socket fd;
311 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800312 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200313 */
314int recv_fd_uxst(int sock)
315{
316 struct msghdr msghdr;
317 struct iovec iov;
318 char iobuf[2];
319
320 char cmsgbuf[CMSG_SPACE(sizeof(int))];
321 char buf[CMSG_SPACE(sizeof(int))];
322 struct cmsghdr *cmsg = (void *)buf;
323
324
325 int recv_fd = -1;
326 int ret = -1;
327
328 memset(&msghdr, 0, sizeof(msghdr));
329
330 iov.iov_base = iobuf;
331 iov.iov_len = sizeof(iobuf);
332
333 msghdr.msg_iov = &iov;
334 msghdr.msg_iovlen = 1;
335
336 msghdr.msg_control = cmsgbuf;
337 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
338
339 iov.iov_len = sizeof(iobuf);
340 iov.iov_base = iobuf;
341
342 while (1) {
343 ret = recvmsg(sock, &msghdr, 0);
344 if (ret == -1 && errno == EINTR)
345 continue;
346 else
347 break;
348 }
349
350 if (ret == -1)
351 return ret;
352
353 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200354 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200355 cmsg->cmsg_type == SCM_RIGHTS) {
356 size_t totlen = cmsg->cmsg_len -
357 CMSG_LEN(0);
358 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
359 }
360 return recv_fd;
361}
362
William Lallemand2fe7dd02018-09-11 16:51:29 +0200363/*
364 * Local variables:
365 * c-indent-level: 8
366 * c-basic-offset: 8
367 * End:
368 */