blob: bc8557a0d0b2302cc0fd5a63da484a428328b565 [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>
33#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/time.h>
36#include <common/version.h>
37
38#include <types/global.h>
39
40#include <proto/connection.h>
41#include <proto/fd.h>
42#include <proto/freq_ctr.h>
43#include <proto/listener.h>
44#include <proto/log.h>
45#include <proto/protocol.h>
46#include <proto/task.h>
47
48static void sockpair_add_listener(struct listener *listener, int port);
49static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen);
50static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
51static int sockpair_connect_server(struct connection *conn, int data, int delack);
52
53/* Note: must not be declared <const> as its list will be overwritten */
54static struct protocol proto_sockpair = {
55 .name = "sockpair",
56 .sock_domain = AF_CUST_SOCKPAIR,
57 .sock_type = SOCK_STREAM,
58 .sock_prot = 0,
59 .sock_family = AF_UNIX,
60 .sock_addrlen = sizeof(struct sockaddr_un),
61 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
62 .accept = &listener_accept,
63 .connect = &sockpair_connect_server,
64 .bind = sockpair_bind_listener,
65 .bind_all = sockpair_bind_listeners,
66 .unbind_all = NULL,
67 .enable_all = enable_all_listeners,
68 .disable_all = disable_all_listeners,
69 .get_src = NULL,
70 .get_dst = NULL,
71 .pause = NULL,
72 .add = sockpair_add_listener,
73 .listeners = LIST_HEAD_INIT(proto_sockpair.listeners),
74 .nb_listeners = 0,
75};
76
77/* 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.
80 */
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 *
97 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
98 */
99static int sockpair_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
100{
101 struct listener *listener;
102 int err = ERR_NONE;
103
104 list_for_each_entry(listener, &proto->listeners, proto_list) {
105 err |= sockpair_bind_listener(listener, errmsg, errlen);
106 if (err & ERR_ABORT)
107 break;
108 }
109 return err;
110}
111
112/* This function changes the state from ASSIGNED to LISTEN. The socket is NOT
113 * enabled for polling. The return value is composed from ERR_NONE,
114 * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in
115 * <errmsg> if the message is at most <errlen> bytes long (including '\0').
116 * Note that <errmsg> may be NULL if <errlen> is also zero.
117 */
118static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen)
119{
120 int fd = listener->fd;
121 int err;
122 const char *msg = NULL;
123
124 err = ERR_NONE;
125
126 /* ensure we never return garbage */
127 if (errlen)
128 *errmsg = 0;
129
130 if (listener->state != LI_ASSIGNED)
131 return ERR_NONE; /* already bound */
132
133 if (listener->fd == -1) {
134 err |= ERR_FATAL | ERR_ALERT;
135 msg = "sockpair can be only used with inherited FDs";
136 goto err_return;
137 }
138
139 if (fd >= global.maxsock) {
140 err |= ERR_FATAL | ERR_ALERT;
141 msg = "socket(): not enough free sockets, raise -n argument";
142 goto err_return;
143 }
144 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
145 err |= ERR_FATAL | ERR_ALERT;
146 msg = "cannot make sockpair non-blocking";
147 goto err_return;
148 }
149
150 listener->state = LI_LISTEN;
151
152 fd_insert(fd, listener, listener->proto->accept,
153 listener->bind_conf->bind_thread[relative_pid-1] ?
154 listener->bind_conf->bind_thread[relative_pid-1] : MAX_THREADS_MASK);
155
156 return err;
157
158 err_return:
159 if (msg && errlen)
160 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
161 return err;
162}
163
164/*
165 * Send FD over a unix socket
166 *
167 * <send_fd> is the FD to send
168 * <fd> is the fd of the unix socket to use for the transfer
169 *
170 * The iobuf variable could be use in the future to enhance the protocol.
171 */
172int send_fd_uxst(int fd, int send_fd)
173{
174 char iobuf[2];
175 struct iovec iov;
176 struct msghdr msghdr;
177
178 char cmsgbuf[CMSG_SPACE(sizeof(int))];
179 char buf[CMSG_SPACE(sizeof(int))];
180 struct cmsghdr *cmsg = (void *)buf;
181
182 int *fdptr;
183
184 iov.iov_base = iobuf;
185 iov.iov_len = sizeof(iobuf);
186
187 memset(&msghdr, 0, sizeof(msghdr));
188 msghdr.msg_iov = &iov;
189 msghdr.msg_iovlen = 1;
190
191 /* Now send the fds */
192 msghdr.msg_control = cmsgbuf;
193 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
194
195 cmsg = CMSG_FIRSTHDR(&msghdr);
196 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
197 cmsg->cmsg_level = SOL_SOCKET;
198 cmsg->cmsg_type = SCM_RIGHTS;
199
200 fdptr = (int *)CMSG_DATA(cmsg);
201 memcpy(fdptr, &send_fd, sizeof(send_fd));
202
203 if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) {
204 ha_warning("Failed to transfer socket\n");
205 return 1;
206 }
207
208 return 0;
209}
210
211/*
212 *
213 * This function works like uxst_connect_server but insteads of creating a
214 * socket and establishing a connection, it creates a pair of connected
215 * sockets, and send one of them through the destination FD. The destination FD
216 * is stored in addr.to->sin_addr.s_addr during configuration parsing.
217 *
218 * conn->target may point either to a valid server or to a backend, depending
219 * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The
220 * <data> parameter is a boolean indicating whether there are data waiting for
221 * being sent or not, in order to adjust data write polling and on some
222 * platforms. The <delack> argument is ignored.
223 *
224 * Note that a pending send_proxy message accounts for data.
225 *
226 * It can return one of :
227 * - SF_ERR_NONE if everything's OK
228 * - SF_ERR_SRVTO if there are no more servers
229 * - SF_ERR_SRVCL if the connection was refused by the server
230 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
231 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
232 * - SF_ERR_INTERNAL for any other purely internal errors
233 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
234 *
235 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
236 * it's invalid and the caller has nothing to do.
237 */
238static int sockpair_connect_server(struct connection *conn, int data, int delack)
239{
240 int sv[2], fd, dst_fd = -1;
241
242 /* the FD is stored in the sockaddr struct */
243 dst_fd = ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr;
244
245 conn->flags = 0;
246
247 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
282 /* if a send_proxy is there, there are data */
283 data |= conn->send_proxy_ofs;
284
285 if (global.tune.server_sndbuf)
286 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
287
288 if (global.tune.server_rcvbuf)
289 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
290
291 /* The new socket is sent on the other side, it should be retrieved and
292 * considered as an 'accept' socket on the server side */
293 if (send_fd_uxst(dst_fd, sv[0]) == -1) {
294 close(sv[0]);
295 close(sv[1]);
296 conn->err_code = CO_ER_SOCK_ERR;
297 conn->flags |= CO_FL_ERROR;
298 return SF_ERR_INTERNAL;
299 }
300
301 close(sv[0]); /* we don't need this side anymore */
302
303 conn->flags &= ~CO_FL_WAIT_L4_CONN;
304
305 conn->flags |= CO_FL_ADDR_TO_SET;
306
307 /* Prepare to send a few handshakes related to the on-wire protocol. */
308 if (conn->send_proxy_ofs)
309 conn->flags |= CO_FL_SEND_PROXY;
310
311 conn_ctrl_init(conn); /* registers the FD */
312 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
313
314 if (conn_xprt_init(conn) < 0) {
315 conn_full_close(conn);
316 conn->flags |= CO_FL_ERROR;
317 return SF_ERR_RESOURCE;
318 }
319
320 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
321 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
322 }
323 else {
324 /* If there's no more handshake, we need to notify the data
325 * layer when the connection is already OK otherwise we'll have
326 * no other opportunity to do it later (eg: health checks).
327 */
328 data = 1;
329 }
330
331 if (data)
332 conn_xprt_want_send(conn); /* prepare to send data if any */
333
334 return SF_ERR_NONE; /* connection is OK */
335}
336
337
338/*
339 * Receive a file descriptor transfered from a unix socket.
340 *
341 * Return -1 or a socket fd;
342 *
343 * The iobuf variable could be use in the future to enhance the protocol.
344 */
345int recv_fd_uxst(int sock)
346{
347 struct msghdr msghdr;
348 struct iovec iov;
349 char iobuf[2];
350
351 char cmsgbuf[CMSG_SPACE(sizeof(int))];
352 char buf[CMSG_SPACE(sizeof(int))];
353 struct cmsghdr *cmsg = (void *)buf;
354
355
356 int recv_fd = -1;
357 int ret = -1;
358
359 memset(&msghdr, 0, sizeof(msghdr));
360
361 iov.iov_base = iobuf;
362 iov.iov_len = sizeof(iobuf);
363
364 msghdr.msg_iov = &iov;
365 msghdr.msg_iovlen = 1;
366
367 msghdr.msg_control = cmsgbuf;
368 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
369
370 iov.iov_len = sizeof(iobuf);
371 iov.iov_base = iobuf;
372
373 while (1) {
374 ret = recvmsg(sock, &msghdr, 0);
375 if (ret == -1 && errno == EINTR)
376 continue;
377 else
378 break;
379 }
380
381 if (ret == -1)
382 return ret;
383
384 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200385 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200386 cmsg->cmsg_type == SCM_RIGHTS) {
387 size_t totlen = cmsg->cmsg_len -
388 CMSG_LEN(0);
389 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
390 }
391 return recv_fd;
392}
393
394__attribute__((constructor))
395static void __uxst_protocol_init(void)
396{
397 protocol_register(&proto_sockpair);
398}
399
400/*
401 * Local variables:
402 * c-indent-level: 8
403 * c-basic-offset: 8
404 * End:
405 */