blob: f83c51b86c9b1062cf0bc8275cc9e39000a1b726 [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);
52static int sockpair_connect_server(struct connection *conn, int data, int delack);
53
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,
156 listener->bind_conf->bind_thread[relative_pid-1] ?
157 listener->bind_conf->bind_thread[relative_pid-1] : MAX_THREADS_MASK);
158
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 *
216 * This function works like uxst_connect_server but insteads of creating a
217 * 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
219 * is stored in addr.to->sin_addr.s_addr during configuration parsing.
220 *
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 */
241static int sockpair_connect_server(struct connection *conn, int data, int delack)
242{
243 int sv[2], fd, dst_fd = -1;
244
245 /* the FD is stored in the sockaddr struct */
246 dst_fd = ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr;
247
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 */
293 data |= conn->send_proxy_ofs;
294
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 */
338 data = 1;
339 }
340
341 if (data)
342 conn_xprt_want_send(conn); /* prepare to send data if any */
343
344 return SF_ERR_NONE; /* connection is OK */
345}
346
347
348/*
349 * Receive a file descriptor transfered from a unix socket.
350 *
351 * Return -1 or a socket fd;
352 *
353 * The iobuf variable could be use in the future to enhance the protocol.
354 */
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 */