blob: 3e29e871a02977663a5ba7716e3faeb38ef9074b [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 Tarreau31794892017-09-15 07:59:31 +020046static int uxst_pause_listener(struct listener *l);
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",
51 .sock_domain = PF_UNIX,
52 .sock_type = SOCK_STREAM,
53 .sock_prot = 0,
54 .sock_family = AF_UNIX,
55 .sock_addrlen = sizeof(struct sockaddr_un),
56 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020057 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020058 .connect = &uxst_connect_server,
Willy Tarreau1e0a8602020-09-02 17:14:29 +020059 .bind = sock_unix_bind_receiver,
Willy Tarreaub3580b12020-09-01 10:26:22 +020060 .listen = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010061 .enable_all = enable_all_listeners,
62 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020063 .get_src = sock_get_src,
64 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020065 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020066 .add = uxst_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020067 .addrcmp = sock_unix_addrcmp,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010068 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
69 .nb_listeners = 0,
70};
71
Willy Tarreau0108d902018-11-25 19:14:37 +010072INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
73
Willy Tarreaudabf2e22007-10-28 21:59:24 +010074/********************************
75 * 1) low-level socket functions
76 ********************************/
77
78
Cyril Bonté1f5848a2010-11-14 17:03:19 +010079/********************************
80 * 2) listener-oriented functions
81 ********************************/
82
Cyril Bonté1f5848a2010-11-14 17:03:19 +010083/* This function creates a UNIX socket associated to the listener. It changes
84 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +010085 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
86 * may return a warning or an error message in <errmsg> if the message is at
87 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
88 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +020089 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +010090static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +020091{
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +020092 int fd, err;
93 int ready;
Willy Tarreau40aa0702013-03-10 23:51:38 +010094 socklen_t ready_len;
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +020095 char *msg = NULL;
Willy Tarreau92fb9832007-10-16 17:34:28 +020096
Willy Tarreau3c5efa22014-07-07 18:36:45 +020097 err = ERR_NONE;
98
Cyril Bonté1f5848a2010-11-14 17:03:19 +010099 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100100 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100101 *errmsg = 0;
102
103 if (listener->state != LI_ASSIGNED)
104 return ERR_NONE; /* already bound */
Willy Tarreau0b915012020-09-01 10:47:07 +0200105
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200106 err = sock_unix_bind_receiver(&listener->rx, listener->rx.proto->accept, &msg);
107 if (err != ERR_NONE) {
108 snprintf(errmsg, errlen, "%s", msg);
109 free(msg); msg = NULL;
110 return err;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200111 }
112
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200113 fd = listener->rx.fd;
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200114
Willy Tarreau40aa0702013-03-10 23:51:38 +0100115 ready = 0;
116 ready_len = sizeof(ready);
117 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
118 ready = 0;
119
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200120 if (!ready && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100121 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200122 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100123 msg = "cannot listen to UNIX socket";
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200124 goto uxst_close_return;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200125 }
126
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100127 /* the socket is now listening */
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100128 listener->state = LI_LISTEN;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200129 return err;
130
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200131 uxst_close_return:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100132 close(fd);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100133 if (msg && errlen) {
Willy Tarreaucd5e5ea2020-09-02 17:21:02 +0200134 const char *path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path;
135 snprintf(errmsg, errlen, "%s [%s]", msg, path);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100136 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200137 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100138}
139
Willy Tarreau32282382017-09-15 07:44:44 +0200140/* Add <listener> to the list of unix stream listeners (port is ignored). The
141 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
142 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200143 *
144 * Must be called with proto_lock held.
145 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100146 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200147static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100148{
149 if (listener->state != LI_INIT)
150 return;
151 listener->state = LI_ASSIGNED;
Willy Tarreaub7436612020-08-28 19:51:44 +0200152 listener->rx.proto = &proto_unix;
153 LIST_ADDQ(&proto_unix.listeners, &listener->rx.proto_list);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100154 proto_unix.nb_listeners++;
155}
156
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200157/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
158 * was totally stopped, or > 0 if correctly paused. Nothing is done for
159 * plain unix sockets since currently it's the new process which handles
160 * the renaming. Abstract sockets are completely unbound.
161 */
Willy Tarreau31794892017-09-15 07:59:31 +0200162static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200163{
Willy Tarreau37159062020-08-27 07:48:42 +0200164 if (((struct sockaddr_un *)&l->rx.addr)->sun_path[0])
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200165 return 1;
166
Christopher Faulet510c0d62018-03-16 10:04:47 +0100167 /* Listener's lock already held. Call lockless version of
168 * unbind_listener. */
169 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200170 return 0;
171}
172
Willy Tarreau47f48c42014-05-09 22:57:47 +0200173
174/*
175 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200176 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200177 * and will be selected by the system. conn->target may point either to a valid
178 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
179 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
180 * whether there are data waiting for being sent or not, in order to adjust data
181 * write polling and on some platforms. The <delack> argument is ignored.
182 *
183 * Note that a pending send_proxy message accounts for data.
184 *
185 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200186 * - SF_ERR_NONE if everything's OK
187 * - SF_ERR_SRVTO if there are no more servers
188 * - SF_ERR_SRVCL if the connection was refused by the server
189 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
190 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
191 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100192 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200193 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200194 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200195 * it's invalid and the caller has nothing to do.
196 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200197static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200198{
199 int fd;
200 struct server *srv;
201 struct proxy *be;
202
Willy Tarreau47f48c42014-05-09 22:57:47 +0200203 switch (obj_type(conn->target)) {
204 case OBJ_TYPE_PROXY:
205 be = objt_proxy(conn->target);
206 srv = NULL;
207 break;
208 case OBJ_TYPE_SERVER:
209 srv = objt_server(conn->target);
210 be = srv->proxy;
211 break;
212 default:
213 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200214 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200215 }
216
Willy Tarreau585744b2017-08-24 14:31:19 +0200217 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200218 qfprintf(stderr, "Cannot get a server socket.\n");
219
220 if (errno == ENFILE) {
221 conn->err_code = CO_ER_SYS_FDLIM;
222 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100223 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
224 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200225 }
226 else if (errno == EMFILE) {
227 conn->err_code = CO_ER_PROC_FDLIM;
228 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100229 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
230 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200231 }
232 else if (errno == ENOBUFS || errno == ENOMEM) {
233 conn->err_code = CO_ER_SYS_MEMLIM;
234 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100235 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
236 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200237 }
238 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
239 conn->err_code = CO_ER_NOPROTO;
240 }
241 else
242 conn->err_code = CO_ER_SOCK_ERR;
243
244 /* this is a resource error */
245 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200246 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200247 }
248
249 if (fd >= global.maxsock) {
250 /* do not log anything there, it's a normal condition when this option
251 * is used to serialize connections to a server !
252 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100253 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200254 close(fd);
255 conn->err_code = CO_ER_CONF_FDLIM;
256 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200257 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200258 }
259
260 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
261 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
262 close(fd);
263 conn->err_code = CO_ER_SOCK_ERR;
264 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200265 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200266 }
267
William Lallemandc03eb012018-11-27 12:02:37 +0100268 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
269 ha_alert("Cannot set CLOEXEC on client socket.\n");
270 close(fd);
271 conn->err_code = CO_ER_SOCK_ERR;
272 conn->flags |= CO_FL_ERROR;
273 return SF_ERR_INTERNAL;
274 }
275
Willy Tarreau47f48c42014-05-09 22:57:47 +0200276 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200277 if (conn->send_proxy_ofs)
278 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200279
280 if (global.tune.server_sndbuf)
281 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
282
283 if (global.tune.server_rcvbuf)
284 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
285
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200286 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100287 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200288 conn->flags |= CO_FL_WAIT_L4_CONN;
289 }
Willy Tarreau94841792017-01-25 14:27:38 +0100290 else if (errno == EISCONN) {
291 conn->flags &= ~CO_FL_WAIT_L4_CONN;
292 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200293 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200294 char *msg;
295 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100296 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200297 conn->err_code = CO_ER_FREE_PORTS;
298 }
299 else {
300 msg = "local address already in use";
301 conn->err_code = CO_ER_ADDR_INUSE;
302 }
303
304 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
305 close(fd);
306 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
307 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200308 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200309 }
310 else if (errno == ETIMEDOUT) {
311 close(fd);
312 conn->err_code = CO_ER_SOCK_ERR;
313 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200314 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200315 }
316 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
317 close(fd);
318 conn->err_code = CO_ER_SOCK_ERR;
319 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200320 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200321 }
322 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200323 else {
324 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100325 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200326 */
327 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200328 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200329
330 conn->flags |= CO_FL_ADDR_TO_SET;
331
332 /* Prepare to send a few handshakes related to the on-wire protocol. */
333 if (conn->send_proxy_ofs)
334 conn->flags |= CO_FL_SEND_PROXY;
335
336 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200337 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200338
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100339 if (conn->flags & CO_FL_WAIT_L4_CONN) {
340 fd_want_send(fd);
341 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200342 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100343 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200344
Willy Tarreau47f48c42014-05-09 22:57:47 +0200345 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200346 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200347 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200348 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200349 }
350
Willy Tarreaue7dff022015-04-03 01:14:29 +0200351 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200352}
353
Willy Tarreau92fb9832007-10-16 17:34:28 +0200354/*
355 * Local variables:
356 * c-indent-level: 8
357 * c-basic-offset: 8
358 * End:
359 */