blob: 7278d51b3ec03061c2b6a40abd478e78a12599c1 [file] [log] [blame]
Willy Tarreau92fb9832007-10-16 17:34:28 +02001/*
2 * UNIX SOCK_STREAM protocol layer (uxst)
3 *
Willy Tarreaueb472682010-05-28 18:46:57 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreau92fb9832007-10-16 17:34:28 +02005 *
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 <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
20#include <time.h>
21
Willy Tarreau92fb9832007-10-16 17:34:28 +020022#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/un.h>
26
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020027#include <haproxy/api.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/fd.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020032#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020034#include <haproxy/log.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020035#include <haproxy/protocol.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020036#include <haproxy/sock.h>
Willy Tarreauf1725582020-08-28 15:30:11 +020037#include <haproxy/sock_unix.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau92fb9832007-10-16 17:34:28 +020041
Willy Tarreau92fb9832007-10-16 17:34:28 +020042
Emeric Bruncf20bf12010-10-22 16:06:11 +020043static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020044static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020045static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreaucb66ea62020-09-25 17:12:32 +020046static int uxst_suspend_receiver(struct receiver *rx);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010047
48/* Note: must not be declared <const> as its list will be overwritten */
49static struct protocol proto_unix = {
50 .name = "unix_stream",
Willy Tarreaub0254cb2020-09-04 08:07:11 +020051 .fam = &proto_fam_unix,
Willy Tarreaua54553f2020-09-16 17:50:45 +020052 .ctrl_type = SOCK_STREAM,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010053 .sock_domain = PF_UNIX,
54 .sock_type = SOCK_STREAM,
55 .sock_prot = 0,
Willy Tarreaucb66ea62020-09-25 17:12:32 +020056 .add = uxst_add_listener,
57 .listen = uxst_bind_listener,
58 .rx_suspend = uxst_suspend_receiver,
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020059 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020060 .connect = &uxst_connect_server,
Willy Tarreaud7f331c2020-09-25 17:01:43 +020061 .receivers = LIST_HEAD_INIT(proto_unix.receivers),
62 .nb_receivers = 0,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010063};
64
Willy Tarreau0108d902018-11-25 19:14:37 +010065INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
66
Willy Tarreaudabf2e22007-10-28 21:59:24 +010067/********************************
68 * 1) low-level socket functions
69 ********************************/
70
71
Cyril Bonté1f5848a2010-11-14 17:03:19 +010072/********************************
73 * 2) listener-oriented functions
74 ********************************/
75
Cyril Bonté1f5848a2010-11-14 17:03:19 +010076/* This function creates a UNIX socket associated to the listener. It changes
77 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +010078 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
79 * may return a warning or an error message in <errmsg> if the message is at
80 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
81 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +020082 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +010083static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +020084{
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +020085 int fd, err;
86 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +010087 socklen_t ready_len;
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +020088 char *msg = NULL;
Willy Tarreau92fb9832007-10-16 17:34:28 +020089
Willy Tarreau3c5efa22014-07-07 18:36:45 +020090 err = ERR_NONE;
91
Cyril Bonté1f5848a2010-11-14 17:03:19 +010092 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +010093 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +010094 *errmsg = 0;
95
96 if (listener->state != LI_ASSIGNED)
97 return ERR_NONE; /* already bound */
Willy Tarreau0b915012020-09-01 10:47:07 +020098
Willy Tarreauad33acf2020-09-02 18:40:02 +020099 if (!(listener->rx.flags & RX_F_BOUND)) {
100 msg = "receiving socket not bound";
101 goto uxst_return;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200102 }
103
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200104 fd = listener->rx.fd;
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200105
Willy Tarreau40aa0702013-03-10 23:51:38 +0100106 ready = 0;
107 ready_len = sizeof(ready);
108 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
109 ready = 0;
110
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200111 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100112 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200113 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100114 msg = "cannot listen to UNIX socket";
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200115 goto uxst_close_return;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200116 }
117
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100118 /* the socket is now listening */
Willy Tarreaua37b2442020-09-24 07:23:45 +0200119 listener_set_state(listener, LI_LISTEN);
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200120 return err;
121
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200122 uxst_close_return:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100123 close(fd);
Willy Tarreauad33acf2020-09-02 18:40:02 +0200124 uxst_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100125 if (msg && errlen) {
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200126 const char *path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
127 snprintf(errmsg, errlen, "%s [%s]", msg, path);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100128 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200129 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100130}
131
Willy Tarreau32282382017-09-15 07:44:44 +0200132/* Add <listener> to the list of unix stream listeners (port is ignored). The
133 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
134 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200135 *
136 * Must be called with proto_lock held.
137 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100138 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200139static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100140{
141 if (listener->state != LI_INIT)
142 return;
Willy Tarreaua37b2442020-09-24 07:23:45 +0200143 listener_set_state(listener, LI_ASSIGNED);
Willy Tarreaub7436612020-08-28 19:51:44 +0200144 listener->rx.proto = &proto_unix;
Willy Tarreaud7f331c2020-09-25 17:01:43 +0200145 LIST_ADDQ(&proto_unix.receivers, &listener->rx.proto_list);
146 proto_unix.nb_receivers++;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100147}
148
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200149/* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver
150 * was totally stopped, or > 0 if correctly suspended. Nothing is done for
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200151 * plain unix sockets since currently it's the new process which handles
Willy Tarreaue53608b2020-09-24 18:20:37 +0200152 * the renaming. Abstract sockets are completely unbound and closed so
153 * there's no need to stop the poller.
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200154 */
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200155static int uxst_suspend_receiver(struct receiver *rx)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200156{
Willy Tarreaucb66ea62020-09-25 17:12:32 +0200157 struct listener *l = LIST_ELEM(rx, struct listener *, rx);
158
159 if (((struct sockaddr_un *)&rx->addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200160 return 1;
161
Christopher Faulet510c0d62018-03-16 10:04:47 +0100162 /* Listener's lock already held. Call lockless version of
163 * unbind_listener. */
164 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200165 return 0;
166}
167
Willy Tarreau47f48c42014-05-09 22:57:47 +0200168
169/*
170 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200171 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200172 * and will be selected by the system. conn->target may point either to a valid
173 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
174 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
175 * whether there are data waiting for being sent or not, in order to adjust data
176 * write polling and on some platforms. The <delack> argument is ignored.
177 *
178 * Note that a pending send_proxy message accounts for data.
179 *
180 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200181 * - SF_ERR_NONE if everything's OK
182 * - SF_ERR_SRVTO if there are no more servers
183 * - SF_ERR_SRVCL if the connection was refused by the server
184 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
185 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
186 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100187 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200188 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200189 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200190 * it's invalid and the caller has nothing to do.
191 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200192static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200193{
194 int fd;
195 struct server *srv;
196 struct proxy *be;
197
Willy Tarreau47f48c42014-05-09 22:57:47 +0200198 switch (obj_type(conn->target)) {
199 case OBJ_TYPE_PROXY:
200 be = objt_proxy(conn->target);
201 srv = NULL;
202 break;
203 case OBJ_TYPE_SERVER:
204 srv = objt_server(conn->target);
205 be = srv->proxy;
206 break;
207 default:
208 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200209 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200210 }
211
Willy Tarreau585744b2017-08-24 14:31:19 +0200212 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200213 qfprintf(stderr, "Cannot get a server socket.\n");
214
215 if (errno == ENFILE) {
216 conn->err_code = CO_ER_SYS_FDLIM;
217 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100218 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
219 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200220 }
221 else if (errno == EMFILE) {
222 conn->err_code = CO_ER_PROC_FDLIM;
223 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100224 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
225 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200226 }
227 else if (errno == ENOBUFS || errno == ENOMEM) {
228 conn->err_code = CO_ER_SYS_MEMLIM;
229 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100230 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
231 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200232 }
233 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
234 conn->err_code = CO_ER_NOPROTO;
235 }
236 else
237 conn->err_code = CO_ER_SOCK_ERR;
238
239 /* this is a resource error */
240 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200241 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200242 }
243
244 if (fd >= global.maxsock) {
245 /* do not log anything there, it's a normal condition when this option
246 * is used to serialize connections to a server !
247 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100248 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200249 close(fd);
250 conn->err_code = CO_ER_CONF_FDLIM;
251 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200252 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200253 }
254
255 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
256 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
257 close(fd);
258 conn->err_code = CO_ER_SOCK_ERR;
259 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200260 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200261 }
262
William Lallemandc03eb012018-11-27 12:02:37 +0100263 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
264 ha_alert("Cannot set CLOEXEC on client socket.\n");
265 close(fd);
266 conn->err_code = CO_ER_SOCK_ERR;
267 conn->flags |= CO_FL_ERROR;
268 return SF_ERR_INTERNAL;
269 }
270
Willy Tarreau47f48c42014-05-09 22:57:47 +0200271 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200272 if (conn->send_proxy_ofs)
273 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200274
275 if (global.tune.server_sndbuf)
276 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
277
278 if (global.tune.server_rcvbuf)
279 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
280
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200281 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100282 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200283 conn->flags |= CO_FL_WAIT_L4_CONN;
284 }
Willy Tarreau94841792017-01-25 14:27:38 +0100285 else if (errno == EISCONN) {
286 conn->flags &= ~CO_FL_WAIT_L4_CONN;
287 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200288 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200289 char *msg;
290 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100291 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200292 conn->err_code = CO_ER_FREE_PORTS;
293 }
294 else {
295 msg = "local address already in use";
296 conn->err_code = CO_ER_ADDR_INUSE;
297 }
298
299 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
300 close(fd);
301 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
302 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200303 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200304 }
305 else if (errno == ETIMEDOUT) {
306 close(fd);
307 conn->err_code = CO_ER_SOCK_ERR;
308 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200309 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200310 }
311 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
312 close(fd);
313 conn->err_code = CO_ER_SOCK_ERR;
314 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200315 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200316 }
317 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200318 else {
319 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100320 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200321 */
322 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200323 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200324
325 conn->flags |= CO_FL_ADDR_TO_SET;
326
327 /* Prepare to send a few handshakes related to the on-wire protocol. */
328 if (conn->send_proxy_ofs)
329 conn->flags |= CO_FL_SEND_PROXY;
330
331 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200332 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200333
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100334 if (conn->flags & CO_FL_WAIT_L4_CONN) {
335 fd_want_send(fd);
336 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200337 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100338 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200339
Willy Tarreau47f48c42014-05-09 22:57:47 +0200340 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200341 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200342 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200343 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200344 }
345
Willy Tarreaue7dff022015-04-03 01:14:29 +0200346 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200347}
348
Willy Tarreau92fb9832007-10-16 17:34:28 +0200349/*
350 * Local variables:
351 * c-indent-level: 8
352 * c-basic-offset: 8
353 * End:
354 */