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