Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * UNIX SOCK_STREAM protocol layer (uxst) |
| 3 | * |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 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 <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
| 20 | #include <time.h> |
| 21 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 28 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 29 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 30 | #include <haproxy/fd.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 31 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 32 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 33 | #include <haproxy/listener.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 34 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 35 | #include <haproxy/protocol.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 36 | #include <haproxy/sock.h> |
Willy Tarreau | f172558 | 2020-08-28 15:30:11 +0200 | [diff] [blame] | 37 | #include <haproxy/sock_unix.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 40 | #include <haproxy/version.h> |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 42 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 43 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 44 | static int uxst_connect_server(struct connection *conn, int flags); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 45 | static void uxst_enable_listener(struct listener *listener); |
| 46 | static void uxst_disable_listener(struct listener *listener); |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 47 | static int uxst_suspend_receiver(struct receiver *rx); |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 48 | |
| 49 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 50 | static struct protocol proto_unix = { |
| 51 | .name = "unix_stream", |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 52 | .fam = &proto_fam_unix, |
Willy Tarreau | a54553f | 2020-09-16 17:50:45 +0200 | [diff] [blame] | 53 | .ctrl_type = SOCK_STREAM, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 54 | .sock_domain = PF_UNIX, |
| 55 | .sock_type = SOCK_STREAM, |
| 56 | .sock_prot = 0, |
Willy Tarreau | efcf138 | 2020-12-04 15:03:36 +0100 | [diff] [blame] | 57 | .add = default_add_listener, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 58 | .listen = uxst_bind_listener, |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 59 | .enable = uxst_enable_listener, |
| 60 | .disable = uxst_disable_listener, |
Willy Tarreau | 7b2febd | 2020-10-09 17:18:29 +0200 | [diff] [blame] | 61 | .unbind = default_unbind_listener, |
Willy Tarreau | e03204c | 2020-10-09 17:02:21 +0200 | [diff] [blame] | 62 | .suspend = default_suspend_listener, |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 63 | .accept_conn = sock_accept_conn, |
Willy Tarreau | 686fa3d | 2020-09-25 19:09:53 +0200 | [diff] [blame] | 64 | .rx_enable = sock_enable, |
| 65 | .rx_disable = sock_disable, |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 66 | .rx_unbind = sock_unbind, |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 67 | .rx_suspend = uxst_suspend_receiver, |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 68 | .rx_listening = sock_accepting_conn, |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 69 | .default_iocb = &sock_accept_iocb, |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 70 | .connect = &uxst_connect_server, |
Willy Tarreau | d7f331c | 2020-09-25 17:01:43 +0200 | [diff] [blame] | 71 | .receivers = LIST_HEAD_INIT(proto_unix.receivers), |
| 72 | .nb_receivers = 0, |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 75 | INITCALL1(STG_REGISTER, protocol_register, &proto_unix); |
| 76 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 77 | /******************************** |
| 78 | * 1) low-level socket functions |
| 79 | ********************************/ |
| 80 | |
| 81 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 82 | /******************************** |
| 83 | * 2) listener-oriented functions |
| 84 | ********************************/ |
| 85 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 86 | /* 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 Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 88 | * 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 Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 92 | */ |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 93 | static int uxst_bind_listener(struct listener *listener, char *errmsg, int errlen) |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 95 | int fd, err; |
| 96 | int ready; |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 97 | char *msg = NULL; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 99 | err = ERR_NONE; |
| 100 | |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 101 | /* ensure we never return garbage */ |
Willy Tarreau | 8ab505b | 2013-01-24 01:41:38 +0100 | [diff] [blame] | 102 | if (errlen) |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 103 | *errmsg = 0; |
| 104 | |
| 105 | if (listener->state != LI_ASSIGNED) |
| 106 | return ERR_NONE; /* already bound */ |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 108 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 109 | msg = "receiving socket not bound"; |
| 110 | goto uxst_return; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 113 | fd = listener->rx.fd; |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 114 | ready = sock_accepting_conn(&listener->rx) > 0; |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 116 | if (!ready && /* only listen if not already done by external process */ |
Willy Tarreau | e2711c7 | 2019-02-27 15:39:41 +0100 | [diff] [blame] | 117 | listen(fd, listener_backlog(listener)) < 0) { |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 118 | err |= ERR_FATAL | ERR_ALERT; |
Willy Tarreau | b40dc94 | 2010-11-07 12:10:51 +0100 | [diff] [blame] | 119 | msg = "cannot listen to UNIX socket"; |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 120 | goto uxst_close_return; |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 123 | /* the socket is now listening */ |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 124 | listener_set_state(listener, LI_LISTEN); |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 125 | return err; |
| 126 | |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 127 | uxst_close_return: |
Cyril Bonté | 1f5848a | 2010-11-14 17:03:19 +0100 | [diff] [blame] | 128 | close(fd); |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 129 | uxst_return: |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 130 | if (msg && errlen) { |
Willy Tarreau | cd5e5ea | 2020-09-02 17:21:02 +0200 | [diff] [blame] | 131 | const char *path = ((struct sockaddr_un *)&listener->rx.addr)->sun_path; |
| 132 | snprintf(errmsg, errlen, "%s [%s]", msg, path); |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 133 | } |
Willy Tarreau | 3c5efa2 | 2014-07-07 18:36:45 +0200 | [diff] [blame] | 134 | return err; |
Willy Tarreau | dabf2e2 | 2007-10-28 21:59:24 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 137 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 138 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 139 | */ |
| 140 | static void uxst_enable_listener(struct listener *l) |
| 141 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 142 | fd_want_recv_safe(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 146 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 147 | */ |
| 148 | static void uxst_disable_listener(struct listener *l) |
| 149 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 150 | fd_stop_recv(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 153 | /* Suspend a receiver. Returns < 0 in case of failure, 0 if the receiver |
| 154 | * was totally stopped, or > 0 if correctly suspended. Nothing is done for |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 155 | * plain unix sockets since currently it's the new process which handles |
Willy Tarreau | e53608b | 2020-09-24 18:20:37 +0200 | [diff] [blame] | 156 | * the renaming. Abstract sockets are completely unbound and closed so |
| 157 | * there's no need to stop the poller. |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 158 | */ |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 159 | static int uxst_suspend_receiver(struct receiver *rx) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 160 | { |
Willy Tarreau | cb66ea6 | 2020-09-25 17:12:32 +0200 | [diff] [blame] | 161 | struct listener *l = LIST_ELEM(rx, struct listener *, rx); |
| 162 | |
| 163 | if (((struct sockaddr_un *)&rx->addr)->sun_path[0]) |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 164 | return 1; |
| 165 | |
Christopher Faulet | 510c0d6 | 2018-03-16 10:04:47 +0100 | [diff] [blame] | 166 | /* Listener's lock already held. Call lockless version of |
| 167 | * unbind_listener. */ |
Willy Tarreau | 75c98d1 | 2020-10-09 15:55:23 +0200 | [diff] [blame] | 168 | do_unbind_listener(l); |
Willy Tarreau | fd0e008 | 2014-07-07 21:07:51 +0200 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * This function initiates a UNIX connection establishment to the target assigned |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 175 | * to connection <conn> using (si->{target,dst}). The source address is ignored |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 176 | * and will be selected by the system. conn->target may point either to a valid |
| 177 | * server or to a backend, depending on conn->target. Only OBJ_TYPE_PROXY and |
| 178 | * OBJ_TYPE_SERVER are supported. The <data> parameter is a boolean indicating |
| 179 | * whether there are data waiting for being sent or not, in order to adjust data |
| 180 | * write polling and on some platforms. The <delack> argument is ignored. |
| 181 | * |
| 182 | * Note that a pending send_proxy message accounts for data. |
| 183 | * |
| 184 | * It can return one of : |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 185 | * - SF_ERR_NONE if everything's OK |
| 186 | * - SF_ERR_SRVTO if there are no more servers |
| 187 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 188 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 189 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 190 | * - SF_ERR_INTERNAL for any other purely internal errors |
Tim Düsterhus | 4896c44 | 2016-11-29 02:15:19 +0100 | [diff] [blame] | 191 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 192 | * |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 193 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 194 | * it's invalid and the caller has nothing to do. |
| 195 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 196 | static int uxst_connect_server(struct connection *conn, int flags) |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 197 | { |
| 198 | int fd; |
| 199 | struct server *srv; |
| 200 | struct proxy *be; |
| 201 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 202 | switch (obj_type(conn->target)) { |
| 203 | case OBJ_TYPE_PROXY: |
| 204 | be = objt_proxy(conn->target); |
| 205 | srv = NULL; |
| 206 | break; |
| 207 | case OBJ_TYPE_SERVER: |
| 208 | srv = objt_server(conn->target); |
| 209 | be = srv->proxy; |
| 210 | break; |
| 211 | default: |
| 212 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 213 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 216 | if ((fd = conn->handle.fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 217 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 218 | |
| 219 | if (errno == ENFILE) { |
| 220 | conn->err_code = CO_ER_SYS_FDLIM; |
| 221 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 222 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 223 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 224 | } |
| 225 | else if (errno == EMFILE) { |
| 226 | conn->err_code = CO_ER_PROC_FDLIM; |
| 227 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 228 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 229 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 230 | } |
| 231 | else if (errno == ENOBUFS || errno == ENOMEM) { |
| 232 | conn->err_code = CO_ER_SYS_MEMLIM; |
| 233 | send_log(be, LOG_EMERG, |
Willy Tarreau | c5532ac | 2018-01-29 15:06:04 +0100 | [diff] [blame] | 234 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 235 | be->id, global.maxsock); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 236 | } |
| 237 | else if (errno == EAFNOSUPPORT || errno == EPROTONOSUPPORT) { |
| 238 | conn->err_code = CO_ER_NOPROTO; |
| 239 | } |
| 240 | else |
| 241 | conn->err_code = CO_ER_SOCK_ERR; |
| 242 | |
| 243 | /* this is a resource error */ |
| 244 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 245 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | if (fd >= global.maxsock) { |
| 249 | /* do not log anything there, it's a normal condition when this option |
| 250 | * is used to serialize connections to a server ! |
| 251 | */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 252 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 253 | close(fd); |
| 254 | conn->err_code = CO_ER_CONF_FDLIM; |
| 255 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 256 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 260 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 261 | close(fd); |
| 262 | conn->err_code = CO_ER_SOCK_ERR; |
| 263 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 264 | return SF_ERR_INTERNAL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 265 | } |
| 266 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 267 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 268 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 269 | close(fd); |
| 270 | conn->err_code = CO_ER_SOCK_ERR; |
| 271 | conn->flags |= CO_FL_ERROR; |
| 272 | return SF_ERR_INTERNAL; |
| 273 | } |
| 274 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 275 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 276 | if (conn->send_proxy_ofs) |
| 277 | flags |= CONNECT_HAS_DATA; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 278 | |
| 279 | if (global.tune.server_sndbuf) |
| 280 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 281 | |
| 282 | if (global.tune.server_rcvbuf) |
| 283 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 284 | |
Willy Tarreau | ca9f5a9 | 2019-07-17 16:40:37 +0200 | [diff] [blame] | 285 | if (connect(fd, (struct sockaddr *)conn->dst, get_addr_len(conn->dst)) == -1) { |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 286 | if (errno == EINPROGRESS || errno == EALREADY) { |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 287 | conn->flags |= CO_FL_WAIT_L4_CONN; |
| 288 | } |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 289 | else if (errno == EISCONN) { |
| 290 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 291 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 292 | else if (errno == EAGAIN || errno == EADDRINUSE || errno == EADDRNOTAVAIL) { |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 293 | char *msg; |
| 294 | if (errno == EAGAIN || errno == EADDRNOTAVAIL) { |
Lukas Tribus | 9f256d4 | 2016-01-26 20:33:14 +0100 | [diff] [blame] | 295 | msg = "can't connect to destination unix socket, check backlog size on the server"; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 296 | conn->err_code = CO_ER_FREE_PORTS; |
| 297 | } |
| 298 | else { |
| 299 | msg = "local address already in use"; |
| 300 | conn->err_code = CO_ER_ADDR_INUSE; |
| 301 | } |
| 302 | |
| 303 | qfprintf(stderr,"Connect() failed for backend %s: %s.\n", be->id, msg); |
| 304 | close(fd); |
| 305 | send_log(be, LOG_ERR, "Connect() failed for backend %s: %s.\n", be->id, msg); |
| 306 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 307 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 308 | } |
| 309 | else if (errno == ETIMEDOUT) { |
| 310 | close(fd); |
| 311 | conn->err_code = CO_ER_SOCK_ERR; |
| 312 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 313 | return SF_ERR_SRVTO; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 314 | } |
| 315 | else { // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 316 | close(fd); |
| 317 | conn->err_code = CO_ER_SOCK_ERR; |
| 318 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 319 | return SF_ERR_SRVCL; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 320 | } |
| 321 | } |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 322 | else { |
| 323 | /* connect() already succeeded, which is quite usual for unix |
Willy Tarreau | 9484179 | 2017-01-25 14:27:38 +0100 | [diff] [blame] | 324 | * sockets. Let's avoid a second connect() probe to complete it. |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 325 | */ |
| 326 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 327 | } |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 328 | |
| 329 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 330 | |
| 331 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 332 | if (conn->send_proxy_ofs) |
| 333 | conn->flags |= CO_FL_SEND_PROXY; |
| 334 | |
| 335 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | 7bb2153 | 2014-05-10 09:48:28 +0200 | [diff] [blame] | 336 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 337 | |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 338 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 339 | fd_want_send(fd); |
| 340 | fd_cant_send(fd); |
Willy Tarreau | 8dbd1a2 | 2020-07-31 08:59:09 +0200 | [diff] [blame] | 341 | fd_cant_recv(fd); |
Willy Tarreau | 4c69cff | 2020-03-04 16:38:00 +0100 | [diff] [blame] | 342 | } |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 343 | |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 344 | if (conn_xprt_init(conn) < 0) { |
Willy Tarreau | 8c82901 | 2017-10-05 18:02:11 +0200 | [diff] [blame] | 345 | conn_full_close(conn); |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 346 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 347 | return SF_ERR_RESOURCE; |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 350 | return SF_ERR_NONE; /* connection is OK */ |
Willy Tarreau | 47f48c4 | 2014-05-09 22:57:47 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Willy Tarreau | 92fb983 | 2007-10-16 17:34:28 +0200 | [diff] [blame] | 353 | /* |
| 354 | * Local variables: |
| 355 | * c-indent-level: 8 |
| 356 | * c-basic-offset: 8 |
| 357 | * End: |
| 358 | */ |