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