blob: 9105dd2f9e0234c9ad302d6466eb069ae8f366f6 [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,
Willy Tarreau3d957172019-02-02 18:00:17 +0100156 thread_mask(listener->bind_conf->bind_thread[relative_pid-1]));
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 */
240static int sockpair_connect_server(struct connection *conn, int data, int delack)
241{
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 */
292 data |= conn->send_proxy_ofs;
293
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
329 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_WAIT_L4_CONN)) {
330 conn_sock_want_send(conn); /* for connect status, proxy protocol or SSL */
331 }
332 else {
333 /* If there's no more handshake, we need to notify the data
334 * layer when the connection is already OK otherwise we'll have
335 * no other opportunity to do it later (eg: health checks).
336 */
337 data = 1;
338 }
339
340 if (data)
341 conn_xprt_want_send(conn); /* prepare to send data if any */
342
343 return SF_ERR_NONE; /* connection is OK */
344}
345
346
347/*
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800348 * Receives a file descriptor transferred from a unix socket.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200349 *
350 * Return -1 or a socket fd;
351 *
Joseph Herlant8bb32ae2018-11-25 11:43:27 -0800352 * The iobuf variable could be used in the future to enhance the protocol.
William Lallemand2fe7dd02018-09-11 16:51:29 +0200353 */
354int recv_fd_uxst(int sock)
355{
356 struct msghdr msghdr;
357 struct iovec iov;
358 char iobuf[2];
359
360 char cmsgbuf[CMSG_SPACE(sizeof(int))];
361 char buf[CMSG_SPACE(sizeof(int))];
362 struct cmsghdr *cmsg = (void *)buf;
363
364
365 int recv_fd = -1;
366 int ret = -1;
367
368 memset(&msghdr, 0, sizeof(msghdr));
369
370 iov.iov_base = iobuf;
371 iov.iov_len = sizeof(iobuf);
372
373 msghdr.msg_iov = &iov;
374 msghdr.msg_iovlen = 1;
375
376 msghdr.msg_control = cmsgbuf;
377 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
378
379 iov.iov_len = sizeof(iobuf);
380 iov.iov_base = iobuf;
381
382 while (1) {
383 ret = recvmsg(sock, &msghdr, 0);
384 if (ret == -1 && errno == EINTR)
385 continue;
386 else
387 break;
388 }
389
390 if (ret == -1)
391 return ret;
392
393 cmsg = CMSG_FIRSTHDR(&msghdr);
Willy Tarreau7d7ab432018-09-20 11:39:39 +0200394 if (cmsg && cmsg->cmsg_level == SOL_SOCKET &&
William Lallemand2fe7dd02018-09-11 16:51:29 +0200395 cmsg->cmsg_type == SCM_RIGHTS) {
396 size_t totlen = cmsg->cmsg_len -
397 CMSG_LEN(0);
398 memcpy(&recv_fd, CMSG_DATA(cmsg), totlen);
399 }
400 return recv_fd;
401}
402
William Lallemand2fe7dd02018-09-11 16:51:29 +0200403/*
404 * Local variables:
405 * c-indent-level: 8
406 * c-basic-offset: 8
407 * End:
408 */