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