blob: 2c3e202de65758f8fe52fcfd425d5608187e8354 [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);
44static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010045static int uxst_unbind_listeners(struct protocol *proto);
Olivier Houchardfdcb0072019-05-06 18:32:29 +020046static int uxst_connect_server(struct connection *conn, int flags);
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020047static void uxst_add_listener(struct listener *listener, int port);
Willy Tarreau31794892017-09-15 07:59:31 +020048static int uxst_pause_listener(struct listener *l);
Willy Tarreaudabf2e22007-10-28 21:59:24 +010049
50/* Note: must not be declared <const> as its list will be overwritten */
51static struct protocol proto_unix = {
52 .name = "unix_stream",
53 .sock_domain = PF_UNIX,
54 .sock_type = SOCK_STREAM,
55 .sock_prot = 0,
56 .sock_family = AF_UNIX,
57 .sock_addrlen = sizeof(struct sockaddr_un),
58 .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */
Willy Tarreaubbebbbf2012-05-07 21:22:09 +020059 .accept = &listener_accept,
Willy Tarreau47f48c42014-05-09 22:57:47 +020060 .connect = &uxst_connect_server,
Emeric Bruncf20bf12010-10-22 16:06:11 +020061 .bind = uxst_bind_listener,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010062 .bind_all = uxst_bind_listeners,
63 .unbind_all = uxst_unbind_listeners,
64 .enable_all = enable_all_listeners,
65 .disable_all = disable_all_listeners,
Willy Tarreau18b7df72020-08-28 12:07:22 +020066 .get_src = sock_get_src,
67 .get_dst = sock_get_dst,
Willy Tarreaufd0e0082014-07-07 21:07:51 +020068 .pause = uxst_pause_listener,
Willy Tarreau9d5be5c2017-09-15 07:55:51 +020069 .add = uxst_add_listener,
Willy Tarreauf1725582020-08-28 15:30:11 +020070 .addrcmp = sock_unix_addrcmp,
Willy Tarreaudabf2e22007-10-28 21:59:24 +010071 .listeners = LIST_HEAD_INIT(proto_unix.listeners),
72 .nb_listeners = 0,
73};
74
Willy Tarreau0108d902018-11-25 19:14:37 +010075INITCALL1(STG_REGISTER, protocol_register, &proto_unix);
76
Willy Tarreaudabf2e22007-10-28 21:59:24 +010077/********************************
78 * 1) low-level socket functions
79 ********************************/
80
81
Cyril Bonté1f5848a2010-11-14 17:03:19 +010082/********************************
83 * 2) listener-oriented functions
84 ********************************/
85
Cyril Bonté1f5848a2010-11-14 17:03:19 +010086/* This function creates a UNIX socket associated to the listener. It changes
87 * the state from ASSIGNED to LISTEN. The socket is NOT enabled for polling.
Willy Tarreau8ab505b2013-01-24 01:41:38 +010088 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL. It
89 * may return a warning or an error message in <errmsg> if the message is at
90 * most <errlen> bytes long (including '\0'). Note that <errmsg> may be NULL if
91 * <errlen> is also zero.
Willy Tarreau92fb9832007-10-16 17:34:28 +020092 */
Cyril Bonté1f5848a2010-11-14 17:03:19 +010093static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +020094{
Cyril Bonté1f5848a2010-11-14 17:03:19 +010095 int fd;
Willy Tarreau92fb9832007-10-16 17:34:28 +020096 char tempname[MAXPATHLEN];
97 char backname[MAXPATHLEN];
98 struct sockaddr_un addr;
Willy Tarreaub40dc942010-11-07 12:10:51 +010099 const char *msg = NULL;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100100 const char *path;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100101 int maxpathlen;
Willy Tarreau40aa0702013-03-10 23:51:38 +0100102 int ext, ready;
103 socklen_t ready_len;
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200104 int err;
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100105 int ret;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200106
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200107 err = ERR_NONE;
108
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100109 /* ensure we never return garbage */
Willy Tarreau8ab505b2013-01-24 01:41:38 +0100110 if (errlen)
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100111 *errmsg = 0;
112
113 if (listener->state != LI_ASSIGNED)
114 return ERR_NONE; /* already bound */
115
Olivier Houchardf886e342017-04-05 22:24:59 +0200116 if (listener->fd == -1)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200117 listener->fd = sock_find_compatible_fd(listener);
118
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100119 path = ((struct sockaddr_un *)&listener->addr)->sun_path;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200120
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100121 maxpathlen = MIN(MAXPATHLEN, sizeof(addr.sun_path));
122
Willy Tarreau40aa0702013-03-10 23:51:38 +0100123 /* if the listener already has an fd assigned, then we were offered the
124 * fd by an external process (most likely the parent), and we don't want
125 * to create a new socket. However we still want to set a few flags on
126 * the socket.
127 */
128 fd = listener->fd;
129 ext = (fd >= 0);
130 if (ext)
131 goto fd_ready;
132
Willy Tarreauccfccef2014-05-10 01:49:15 +0200133 if (path[0]) {
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100134 ret = snprintf(tempname, maxpathlen, "%s.%d.tmp", path, pid);
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200135 if (ret < 0 || ret >= sizeof(addr.sun_path)) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200136 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100137 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200138 goto err_return;
139 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200140
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100141 ret = snprintf(backname, maxpathlen, "%s.%d.bak", path, pid);
142 if (ret < 0 || ret >= maxpathlen) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200143 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreau327ea5a2020-02-11 06:43:37 +0100144 msg = "name too long for UNIX socket (limit usually 97)";
Willy Tarreauccfccef2014-05-10 01:49:15 +0200145 goto err_return;
146 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200147
Willy Tarreauccfccef2014-05-10 01:49:15 +0200148 /* 2. clean existing orphaned entries */
149 if (unlink(tempname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200150 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200151 msg = "error when trying to unlink previous UNIX socket";
152 goto err_return;
153 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200154
Willy Tarreauccfccef2014-05-10 01:49:15 +0200155 if (unlink(backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200156 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200157 msg = "error when trying to unlink previous UNIX socket";
158 goto err_return;
159 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200160
Willy Tarreauccfccef2014-05-10 01:49:15 +0200161 /* 3. backup existing socket */
162 if (link(path, backname) < 0 && errno != ENOENT) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200163 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200164 msg = "error when trying to preserve previous UNIX socket";
165 goto err_return;
166 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200167
Willy Tarreauf28d5c92020-06-12 15:58:19 +0200168 /* Note: this test is redundant with the snprintf one above and
169 * will never trigger, it's just added as the only way to shut
170 * gcc's painfully dumb warning about possibly truncated output
171 * during strncpy(). Don't move it above or smart gcc will not
172 * see it!
173 */
174 if (strlen(tempname) >= sizeof(addr.sun_path)) {
175 err |= ERR_FATAL | ERR_ALERT;
176 msg = "name too long for UNIX socket (limit usually 97)";
177 goto err_return;
178 }
179
Willy Tarreau719e07c2019-12-11 16:29:10 +0100180 strncpy(addr.sun_path, tempname, sizeof(addr.sun_path) - 1);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200181 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200182 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200183 else {
184 /* first char is zero, it's an abstract socket whose address
185 * is defined by all the bytes past this zero.
186 */
187 memcpy(addr.sun_path, path, sizeof(addr.sun_path));
188 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200189 addr.sun_family = AF_UNIX;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200190
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100191 fd = socket(PF_UNIX, SOCK_STREAM, 0);
192 if (fd < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200193 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100194 msg = "cannot create UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200195 goto err_unlink_back;
196 }
197
Willy Tarreau40aa0702013-03-10 23:51:38 +0100198 fd_ready:
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100199 if (fd >= global.maxsock) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200200 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100201 msg = "socket(): not enough free sockets, raise -n argument";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200202 goto err_unlink_temp;
203 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100204
205 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200206 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100207 msg = "cannot make UNIX socket non-blocking";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200208 goto err_unlink_temp;
209 }
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100210
Willy Tarreau40aa0702013-03-10 23:51:38 +0100211 if (!ext && bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
Willy Tarreau92fb9832007-10-16 17:34:28 +0200212 /* note that bind() creates the socket <tempname> on the file system */
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200213 if (errno == EADDRINUSE) {
214 /* the old process might still own it, let's retry */
215 err |= ERR_RETRYABLE | ERR_ALERT;
216 msg = "cannot listen to socket";
217 }
218 else {
219 err |= ERR_FATAL | ERR_ALERT;
220 msg = "cannot bind UNIX socket";
221 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200222 goto err_unlink_temp;
223 }
224
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100225 /* <uid> and <gid> different of -1 will be used to change the socket owner.
226 * If <mode> is not 0, it will be used to restrict access to the socket.
227 * While it is known not to be portable on every OS, it's still useful
Willy Tarreauccfccef2014-05-10 01:49:15 +0200228 * where it works. We also don't change permissions on abstract sockets.
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100229 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200230 if (!ext && path[0] &&
Willy Tarreau40aa0702013-03-10 23:51:38 +0100231 (((listener->bind_conf->ux.uid != -1 || listener->bind_conf->ux.gid != -1) &&
232 (chown(tempname, listener->bind_conf->ux.uid, listener->bind_conf->ux.gid) == -1)) ||
233 (listener->bind_conf->ux.mode != 0 && chmod(tempname, listener->bind_conf->ux.mode) == -1))) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200234 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100235 msg = "cannot change UNIX socket ownership";
Willy Tarreaue6ad2b12007-10-18 12:45:54 +0200236 goto err_unlink_temp;
237 }
238
Willy Tarreau40aa0702013-03-10 23:51:38 +0100239 ready = 0;
240 ready_len = sizeof(ready);
241 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &ready, &ready_len) == -1)
242 ready = 0;
243
244 if (!(ext && ready) && /* only listen if not already done by external process */
Willy Tarreaue2711c72019-02-27 15:39:41 +0100245 listen(fd, listener_backlog(listener)) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200246 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100247 msg = "cannot listen to UNIX socket";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200248 goto err_unlink_temp;
249 }
250
Willy Tarreauccfccef2014-05-10 01:49:15 +0200251 /* Point of no return: we are ready, we'll switch the sockets. We don't
Ilya Shipitsin47d17182020-06-21 21:42:57 +0500252 * fear losing the socket <path> because we have a copy of it in
Willy Tarreauccfccef2014-05-10 01:49:15 +0200253 * backname. Abstract sockets are not renamed.
Willy Tarreau92fb9832007-10-16 17:34:28 +0200254 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200255 if (!ext && path[0] && rename(tempname, path) < 0) {
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200256 err |= ERR_FATAL | ERR_ALERT;
Willy Tarreaub40dc942010-11-07 12:10:51 +0100257 msg = "cannot switch final and temporary UNIX sockets";
Willy Tarreau92fb9832007-10-16 17:34:28 +0200258 goto err_rename;
259 }
260
Willy Tarreau68986ab2017-06-16 10:34:20 +0200261 /* Cleanup: only unlink if we didn't inherit the fd from the parent */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200262 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100263 unlink(backname);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200264
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100265 /* the socket is now listening */
266 listener->fd = fd;
267 listener->state = LI_LISTEN;
268
Willy Tarreaua9786b62018-01-25 07:22:13 +0100269 fd_insert(fd, listener, listener->proto->accept,
Willy Tarreau0948a782020-02-12 10:15:34 +0100270 thread_mask(listener->bind_conf->bind_thread) & all_threads_mask);
Willy Tarreaua9786b62018-01-25 07:22:13 +0100271
Willy Tarreaubb1caff2020-08-19 10:00:57 +0200272 /* for now, all regularly bound UNIX listeners are exportable */
273 if (!(listener->options & LI_O_INHERITED))
274 fdtab[fd].exported = 1;
275
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200276 return err;
277
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100278 err_rename:
279 ret = rename(backname, path);
280 if (ret < 0 && errno == ENOENT)
281 unlink(path);
282 err_unlink_temp:
Jan Seda7319b642014-06-26 20:44:05 +0200283 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100284 unlink(tempname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100285 close(fd);
286 err_unlink_back:
Jan Seda7319b642014-06-26 20:44:05 +0200287 if (!ext && path[0])
Willy Tarreau40aa0702013-03-10 23:51:38 +0100288 unlink(backname);
Cyril Bonté1f5848a2010-11-14 17:03:19 +0100289 err_return:
Willy Tarreau40aa0702013-03-10 23:51:38 +0100290 if (msg && errlen) {
291 if (!ext)
292 snprintf(errmsg, errlen, "%s [%s]", msg, path);
293 else
294 snprintf(errmsg, errlen, "%s [fd %d]", msg, fd);
295 }
Willy Tarreau3c5efa22014-07-07 18:36:45 +0200296 return err;
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100297}
298
299/* This function closes the UNIX sockets for the specified listener.
300 * The listener enters the LI_ASSIGNED state. It always returns ERR_NONE.
301 */
302static int uxst_unbind_listener(struct listener *listener)
303{
Willy Tarreaube58c382011-07-24 18:28:10 +0200304 if (listener->state > LI_ASSIGNED) {
305 unbind_listener(listener);
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100306 }
307 return ERR_NONE;
308}
309
Willy Tarreau32282382017-09-15 07:44:44 +0200310/* Add <listener> to the list of unix stream listeners (port is ignored). The
311 * listener's state is automatically updated from LI_INIT to LI_ASSIGNED.
312 * The number of listeners for the protocol is updated.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200313 *
314 * Must be called with proto_lock held.
315 *
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100316 */
Willy Tarreau9d5be5c2017-09-15 07:55:51 +0200317static void uxst_add_listener(struct listener *listener, int port)
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100318{
319 if (listener->state != LI_INIT)
320 return;
321 listener->state = LI_ASSIGNED;
322 listener->proto = &proto_unix;
323 LIST_ADDQ(&proto_unix.listeners, &listener->proto_list);
324 proto_unix.nb_listeners++;
325}
326
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200327/* Pause a listener. Returns < 0 in case of failure, 0 if the listener
328 * was totally stopped, or > 0 if correctly paused. Nothing is done for
329 * plain unix sockets since currently it's the new process which handles
330 * the renaming. Abstract sockets are completely unbound.
331 */
Willy Tarreau31794892017-09-15 07:59:31 +0200332static int uxst_pause_listener(struct listener *l)
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200333{
334 if (((struct sockaddr_un *)&l->addr)->sun_path[0])
335 return 1;
336
Christopher Faulet510c0d62018-03-16 10:04:47 +0100337 /* Listener's lock already held. Call lockless version of
338 * unbind_listener. */
339 do_unbind_listener(l, 1);
Willy Tarreaufd0e0082014-07-07 21:07:51 +0200340 return 0;
341}
342
Willy Tarreau47f48c42014-05-09 22:57:47 +0200343
344/*
345 * This function initiates a UNIX connection establishment to the target assigned
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200346 * to connection <conn> using (si->{target,dst}). The source address is ignored
Willy Tarreau47f48c42014-05-09 22:57:47 +0200347 * and will be selected by the system. conn->target may point either to a valid
348 * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and
349 * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating
350 * whether there are data waiting for being sent or not, in order to adjust data
351 * write polling and on some platforms. The <delack> argument is ignored.
352 *
353 * Note that a pending send_proxy message accounts for data.
354 *
355 * It can return one of :
Willy Tarreaue7dff022015-04-03 01:14:29 +0200356 * - SF_ERR_NONE if everything's OK
357 * - SF_ERR_SRVTO if there are no more servers
358 * - SF_ERR_SRVCL if the connection was refused by the server
359 * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn)
360 * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...)
361 * - SF_ERR_INTERNAL for any other purely internal errors
Tim Düsterhus4896c442016-11-29 02:15:19 +0100362 * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted.
Willy Tarreau47f48c42014-05-09 22:57:47 +0200363 *
Willy Tarreaue7dff022015-04-03 01:14:29 +0200364 * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise
Willy Tarreau47f48c42014-05-09 22:57:47 +0200365 * it's invalid and the caller has nothing to do.
366 */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200367static int uxst_connect_server(struct connection *conn, int flags)
Willy Tarreau47f48c42014-05-09 22:57:47 +0200368{
369 int fd;
370 struct server *srv;
371 struct proxy *be;
372
Willy Tarreau47f48c42014-05-09 22:57:47 +0200373 switch (obj_type(conn->target)) {
374 case OBJ_TYPE_PROXY:
375 be = objt_proxy(conn->target);
376 srv = NULL;
377 break;
378 case OBJ_TYPE_SERVER:
379 srv = objt_server(conn->target);
380 be = srv->proxy;
381 break;
382 default:
383 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200384 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200385 }
386
Willy Tarreau585744b2017-08-24 14:31:19 +0200387 if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200388 qfprintf(stderr, "Cannot get a server socket.\n");
389
390 if (errno == ENFILE) {
391 conn->err_code = CO_ER_SYS_FDLIM;
392 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100393 "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n",
394 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200395 }
396 else if (errno == EMFILE) {
397 conn->err_code = CO_ER_PROC_FDLIM;
398 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100399 "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n",
400 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200401 }
402 else if (errno == ENOBUFS || errno == ENOMEM) {
403 conn->err_code = CO_ER_SYS_MEMLIM;
404 send_log(be, LOG_EMERG,
Willy Tarreauc5532ac2018-01-29 15:06:04 +0100405 "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n",
406 be->id, global.maxsock);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200407 }
408 else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) {
409 conn->err_code = CO_ER_NOPROTO;
410 }
411 else
412 conn->err_code = CO_ER_SOCK_ERR;
413
414 /* this is a resource error */
415 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200416 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200417 }
418
419 if (fd >= global.maxsock) {
420 /* do not log anything there, it's a normal condition when this option
421 * is used to serialize connections to a server !
422 */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100423 ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau47f48c42014-05-09 22:57:47 +0200424 close(fd);
425 conn->err_code = CO_ER_CONF_FDLIM;
426 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200427 return SF_ERR_PRXCOND; /* it is a configuration limit */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200428 }
429
430 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
431 qfprintf(stderr,"Cannot set client socket to non blocking mode.\n");
432 close(fd);
433 conn->err_code = CO_ER_SOCK_ERR;
434 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200435 return SF_ERR_INTERNAL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200436 }
437
William Lallemandc03eb012018-11-27 12:02:37 +0100438 if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) {
439 ha_alert("Cannot set CLOEXEC on client socket.\n");
440 close(fd);
441 conn->err_code = CO_ER_SOCK_ERR;
442 conn->flags |= CO_FL_ERROR;
443 return SF_ERR_INTERNAL;
444 }
445
Willy Tarreau47f48c42014-05-09 22:57:47 +0200446 /* if a send_proxy is there, there are data */
Olivier Houchardfdcb0072019-05-06 18:32:29 +0200447 if (conn->send_proxy_ofs)
448 flags |= CONNECT_HAS_DATA;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200449
450 if (global.tune.server_sndbuf)
451 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf));
452
453 if (global.tune.server_rcvbuf)
454 setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf));
455
Willy Tarreauca9f5a92019-07-17 16:40:37 +0200456 if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) {
Willy Tarreau94841792017-01-25 14:27:38 +0100457 if (errno == EINPROGRESS || errno == EALREADY) {
Willy Tarreau7bb21532014-05-10 09:48:28 +0200458 conn->flags |= CO_FL_WAIT_L4_CONN;
459 }
Willy Tarreau94841792017-01-25 14:27:38 +0100460 else if (errno == EISCONN) {
461 conn->flags &= ~CO_FL_WAIT_L4_CONN;
462 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200463 else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) {
Willy Tarreau47f48c42014-05-09 22:57:47 +0200464 char *msg;
465 if (errno == EAGAIN || errno == EADDRNOTAVAIL) {
Lukas Tribus9f256d42016-01-26 20:33:14 +0100466 msg = "can't connect to destination unix socket, check backlog size on the server";
Willy Tarreau47f48c42014-05-09 22:57:47 +0200467 conn->err_code = CO_ER_FREE_PORTS;
468 }
469 else {
470 msg = "local address already in use";
471 conn->err_code = CO_ER_ADDR_INUSE;
472 }
473
474 qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg);
475 close(fd);
476 send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg);
477 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200478 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200479 }
480 else if (errno == ETIMEDOUT) {
481 close(fd);
482 conn->err_code = CO_ER_SOCK_ERR;
483 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200484 return SF_ERR_SRVTO;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200485 }
486 else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM)
487 close(fd);
488 conn->err_code = CO_ER_SOCK_ERR;
489 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200490 return SF_ERR_SRVCL;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200491 }
492 }
Willy Tarreau7bb21532014-05-10 09:48:28 +0200493 else {
494 /* connect() already succeeded, which is quite usual for unix
Willy Tarreau94841792017-01-25 14:27:38 +0100495 * sockets. Let's avoid a second connect() probe to complete it.
Willy Tarreau7bb21532014-05-10 09:48:28 +0200496 */
497 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau7bb21532014-05-10 09:48:28 +0200498 }
Willy Tarreau47f48c42014-05-09 22:57:47 +0200499
500 conn->flags |= CO_FL_ADDR_TO_SET;
501
502 /* Prepare to send a few handshakes related to the on-wire protocol. */
503 if (conn->send_proxy_ofs)
504 conn->flags |= CO_FL_SEND_PROXY;
505
506 conn_ctrl_init(conn); /* registers the FD */
Willy Tarreau7bb21532014-05-10 09:48:28 +0200507 fdtab[fd].linger_risk = 0; /* no need to disable lingering */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200508
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100509 if (conn->flags & CO_FL_WAIT_L4_CONN) {
510 fd_want_send(fd);
511 fd_cant_send(fd);
Willy Tarreau8dbd1a22020-07-31 08:59:09 +0200512 fd_cant_recv(fd);
Willy Tarreau4c69cff2020-03-04 16:38:00 +0100513 }
Willy Tarreauccf3f6d2019-09-05 17:05:05 +0200514
Willy Tarreau47f48c42014-05-09 22:57:47 +0200515 if (conn_xprt_init(conn) < 0) {
Willy Tarreau8c829012017-10-05 18:02:11 +0200516 conn_full_close(conn);
Willy Tarreau47f48c42014-05-09 22:57:47 +0200517 conn->flags |= CO_FL_ERROR;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200518 return SF_ERR_RESOURCE;
Willy Tarreau47f48c42014-05-09 22:57:47 +0200519 }
520
Willy Tarreaue7dff022015-04-03 01:14:29 +0200521 return SF_ERR_NONE; /* connection is OK */
Willy Tarreau47f48c42014-05-09 22:57:47 +0200522}
523
524
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100525/********************************
526 * 3) protocol-oriented functions
527 ********************************/
528
529
Willy Tarreau92fb9832007-10-16 17:34:28 +0200530/* This function creates all UNIX sockets bound to the protocol entry <proto>.
531 * It is intended to be used as the protocol's bind_all() function.
532 * The sockets will be registered but not added to any fd_set, in order not to
533 * loose them across the fork(). A call to uxst_enable_listeners() is needed
534 * to complete initialization.
535 *
Willy Tarreaudaacf362019-07-24 16:45:02 +0200536 * Must be called with proto_lock held.
537 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200538 * The return value is composed from ERR_NONE, ERR_RETRYABLE and ERR_FATAL.
539 */
Emeric Bruncf20bf12010-10-22 16:06:11 +0200540static int uxst_bind_listeners(struct protocol *proto, char *errmsg, int errlen)
Willy Tarreau92fb9832007-10-16 17:34:28 +0200541{
542 struct listener *listener;
543 int err = ERR_NONE;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200544
545 list_for_each_entry(listener, &proto->listeners, proto_list) {
Emeric Bruncf20bf12010-10-22 16:06:11 +0200546 err |= uxst_bind_listener(listener, errmsg, errlen);
547 if (err & ERR_ABORT)
548 break;
Willy Tarreau92fb9832007-10-16 17:34:28 +0200549 }
Willy Tarreau92fb9832007-10-16 17:34:28 +0200550 return err;
551}
552
Willy Tarreau92fb9832007-10-16 17:34:28 +0200553
554/* This function stops all listening UNIX sockets bound to the protocol
555 * <proto>. It does not detaches them from the protocol.
556 * It always returns ERR_NONE.
Willy Tarreaudaacf362019-07-24 16:45:02 +0200557 *
558 * Must be called with proto_lock held.
559 *
Willy Tarreau92fb9832007-10-16 17:34:28 +0200560 */
561static int uxst_unbind_listeners(struct protocol *proto)
562{
563 struct listener *listener;
564
Willy Tarreaudabf2e22007-10-28 21:59:24 +0100565 list_for_each_entry(listener, &proto->listeners, proto_list)
566 uxst_unbind_listener(listener);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200567 return ERR_NONE;
568}
569
Willy Tarreau92fb9832007-10-16 17:34:28 +0200570/*
571 * Local variables:
572 * c-indent-level: 8
573 * c-basic-offset: 8
574 * End:
575 */