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