blob: 97a93480af6488d2595f7422c4c2d56c622d2604 [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
29#include <common/compat.h>
30#include <common/config.h>
31#include <common/debug.h>
32#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand2fe7dd02018-09-11 16:51:29 +020034#include <common/mini-clist.h>
35#include <common/standard.h>
36#include <common/time.h>
37#include <common/version.h>
38
39#include <types/global.h>
40
41#include <proto/connection.h>
42#include <proto/fd.h>
43#include <proto/freq_ctr.h>
44#include <proto/listener.h>
45#include <proto/log.h>
46#include <proto/protocol.h>
47#include <proto/task.h>
48
49static void sockpair_add_listener(struct listener *listener, int port);
50static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
51static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020052static int sockpair_connect_server(struct connection *conn, int flags);
William Lallemand2fe7dd02018-09-11 16:51:29 +020053
54/* Note: must not be declared <const> as its list will be overwritten */
55static struct protocol proto_sockpair = {
56 .name = "sockpair",
57 .sock_domain = AF_CUST_SOCKPAIR,
58 .sock_type = SOCK_STREAM,
59 .sock_prot = 0,
60 .sock_family = AF_UNIX,
61 .sock_addrlen = sizeof(struct sockaddr_un),
62 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
63 .accept = &listener_accept,
64 .connect = &sockpair_connect_server,
65 .bind = sockpair_bind_listener,
66 .bind_all = sockpair_bind_listeners,
67 .unbind_all = NULL,
68 .enable_all = enable_all_listeners,
69 .disable_all = disable_all_listeners,
70 .get_src = NULL,
71 .get_dst = NULL,
72 .pause = NULL,
73 .add = sockpair_add_listener,
74 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
75 .nb_listeners = 0,
76};
77
Willy Tarreau0108d902018-11-25 19:14:37 +010078INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair);
79
William Lallemand2fe7dd02018-09-11 16:51:29 +020080/* Add <listener> to the list of sockpair listeners (port is ignored). The
81 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
82 * The number of listeners for the protocol is updated.
83 */
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 *
100 * 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 Tarreaua36b3242019-02-02 13:14:34 +0100156 thread_mask(listener->bind_conf->bind_thread));
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
218 * is stored in addr.to->sin_addr.s_addr during configuration parsing.
219 *
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 */
245 dst_fd = ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr;
246
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
330 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
331 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
332 }
333 else {
334 /* If there's no more handshake, we need to notify the data
335 * layer when the connection is already OK otherwise we'll have
336 * no other opportunity to do it later (eg: health checks).
337 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200338 flags |= CONNECT_HAS_DATA;
William Lallemand2fe7dd02018-09-11 16:51:29 +0200339 }
340
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200341 if (flags & CONNECT_HAS_DATA)
William Lallemand2fe7dd02018-09-11 16:51:29 +0200342 conn_xprt_want_send(conn); /* prepare to send data if any */
343
344 return SF_ERR_NONE; /* connection is OK */
345}
346
347
348/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800349 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200350 *
351 * Return -1 or a socket fd;
352 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800353 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200354 */
355int recv_fd_uxst(int sock)
356{
357 struct msghdr msghdr;
358 struct iovec iov;
359 char iobuf[2];
360
361 char cmsgbuf[CMSG_SPACE(sizeof(int))];
362 char buf[CMSG_SPACE(sizeof(int))];
363 struct cmsghdr *cmsg = (void *)buf;
364
365
366 int recv_fd = -1;
367 int ret = -1;
368
369 memset(&msghdr, 0, sizeof(msghdr));
370
371 iov.iov_base = iobuf;
372 iov.iov_len = sizeof(iobuf);
373
374 msghdr.msg_iov = &iov;
375 msghdr.msg_iovlen = 1;
376
377 msghdr.msg_control = cmsgbuf;
378 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
379
380 iov.iov_len = sizeof(iobuf);
381 iov.iov_base = iobuf;
382
383 while (1) {
384 ret = recvmsg(sock, &msghdr, 0);
385 if (ret == -1 && errno == EINTR)
386 continue;
387 else
388 break;
389 }
390
391 if (ret == -1)
392 return ret;
393
394 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200395 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200396 cmsg->cmsg_type == SCM_RIGHTS) {
397 size_t totlen = cmsg->cmsg_len -
398 CMSG_LEN(0);
399 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
400 }
401 return recv_fd;
402}
403
William Lallemand2fe7dd02018-09-11 16:51:29 +0200404/*
405 * Local variables:
406 * c-indent-level: 8
407 * c-basic-offset: 8
408 * End:
409 */